9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] stub function generator
@ 2008-06-05 14:18 Steve Simon
  2008-06-05 15:14 ` erik quanstrom
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Steve Simon @ 2008-06-05 14:18 UTC (permalink / raw)
  To: 9fans

Anyone know of some nice simple code to parse C prototype definitions
and split them into nicely awk'able bits so I can generate stub functions:

I have been playing with mkptypes | awk which works well
for simple stuff, say my source contains:

	void
	func(int a, char *b)


and I want to generate

	void
	stub_func(int a, char *b)
	{
		func(a, b);
	}

however when you get into

	int syspipe(int fd[2])

let alone

	int sysnotify(void (*func)(void*, char*))

it is starting to push at the limits of what awk is good for.

I guess I am looking for some yacc and lex which
parses C and spits out somthing like this:

	func#a|int a#b|char *b
	syspipe#fd|int fd[2]
	sysnotify#func|void (*func)(void*, char*)

I understand knowledge of types is harder but if I use just basic types
this sounds doable to me. Before I write it, does anyone seem such a beast?

-Steve



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [9fans] stub function generator
  2008-06-05 14:18 [9fans] stub function generator Steve Simon
@ 2008-06-05 15:14 ` erik quanstrom
  2008-06-05 18:44 ` John Stalker
  2008-06-06  1:39 ` Pietro Gagliardi
  2 siblings, 0 replies; 6+ messages in thread
From: erik quanstrom @ 2008-06-05 15:14 UTC (permalink / raw)
  To: steve, 9fans

there are even worse cases.
typedef'd function types, for
example.

the compiler knows this stuff.
why not let the compiler emit
stubs?

acid support requires only 3
hooks in cc/dcl.c and a few in
cc/lex.c  cc/acid.c is only 300
lines.

- erik



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [9fans] stub function generator
  2008-06-05 14:18 [9fans] stub function generator Steve Simon
  2008-06-05 15:14 ` erik quanstrom
@ 2008-06-05 18:44 ` John Stalker
  2008-06-09  9:02   ` Steve Simon
  2008-06-06  1:39 ` Pietro Gagliardi
  2 siblings, 1 reply; 6+ messages in thread
From: John Stalker @ 2008-06-05 18:44 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

>From what I remember, tendra does something like what you want.  It's
been a while since I looked at it, though.

> Anyone know of some nice simple code to parse C prototype definitions
> and split them into nicely awk'able bits so I can generate stub functions:
>
> I have been playing with mkptypes | awk which works well
> for simple stuff, say my source contains:
>
> 	void
> 	func(int a, char *b)
>
>
> and I want to generate
>
> 	void
> 	stub_func(int a, char *b)
> 	{
> 		func(a, b);
> 	}
>
> however when you get into
>
> 	int syspipe(int fd[2])
>
> let alone
>
> 	int sysnotify(void (*func)(void*, char*))
>
> it is starting to push at the limits of what awk is good for.
>
> I guess I am looking for some yacc and lex which
> parses C and spits out somthing like this:
>
> 	func#a|int a#b|char *b
> 	syspipe#fd|int fd[2]
> 	sysnotify#func|void (*func)(void*, char*)
>
> I understand knowledge of types is harder but if I use just basic types
> this sounds doable to me. Before I write it, does anyone seem such a beast?
>
> -Steve
>
--
John Stalker
School of Mathematics
Trinity College Dublin
tel +353 1 896 1983
fax +353 1 896 2282



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [9fans] stub function generator
  2008-06-05 14:18 [9fans] stub function generator Steve Simon
  2008-06-05 15:14 ` erik quanstrom
  2008-06-05 18:44 ` John Stalker
@ 2008-06-06  1:39 ` Pietro Gagliardi
  2 siblings, 0 replies; 6+ messages in thread
From: Pietro Gagliardi @ 2008-06-06  1:39 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

If you are conforming to style(6), awk can generate stub functions
quite easily.

	# turns lines of the form
	# 		valid type name
	# 		name ( argument-list )
	# into
	#		valid type name
	# 		stub_ ## name ( argument-list )
	#		{
	#			[return] name ( argument-list );
	#		}
	# and skips other lines

	NF == 0 { next } # skip blank lines
	/^[^A-Za-z_]/ { next } # skip lines that begin badly
	/[;{}]/ { next } # skip declarations or blocks

	{
		typename = $0
		getline
		if (NF == 0) {
			print "bad function line" | "cat 1>&2" # awk is ape, use sh syntax,
not rc
			next
		}
		print typename # print type name
		$0 = "stub_" $0
		fname = $0
		print
		print "{"
		n = split(fname, fields, "[(),]+")
		printf "\t%s%s(", (typename != "void" ? "return " : ""), fields[1]
		for (i = 2; i < n; i++) {
			# drop non-alphanumeric characters
			m = split(fields[i], arg, "[^A-Za-z0-9_]+")
			printf "%s%s", arg[m], (i == n - 1 ? ");" : ",")
			delete arg # deletes whole array - added to one true awk after book
		}
		delete fields
		printf "\n}\n"
	}


This isn't perfect -- it doesn't handle function pointers or arrays as
arguments -- but it should work.

If you need to just split into the arguments, a simple modification:

	{
		typename = $0
		getline
		if (NF == 0) {
			print "bad function line" | "cat 1>&2" # awk is ape, use sh syntax,
not rc
			next
		}
		n = split($0, fields, "[(),]+")
		printf "%s#", fields[1]
		for (i = 2; i < n; i++) {
			# drop non-alphanumeric characters
			m = split(fields[i], arg, "[^A-Za-z0-9_]+")
			printf "%s|%s#", arg[m], fields[i]
			delete arg
		}
		delete fields
		print typename
	}


On Jun 5, 2008, at 10:18 AM, Steve Simon wrote:

> Anyone know of some nice simple code to parse C prototype definitions
> and split them into nicely awk'able bits so I can generate stub
> functions:
>
> I have been playing with mkptypes | awk which works well
> for simple stuff, say my source contains:
>
> 	void
> 	func(int a, char *b)
>
>
> and I want to generate
>
> 	void
> 	stub_func(int a, char *b)
> 	{
> 		func(a, b);
> 	}
>
> however when you get into
>
> 	int syspipe(int fd[2])
>
> let alone
>
> 	int sysnotify(void (*func)(void*, char*))
>
> it is starting to push at the limits of what awk is good for.
>
> I guess I am looking for some yacc and lex which
> parses C and spits out somthing like this:
>
> 	func#a|int a#b|char *b
> 	syspipe#fd|int fd[2]
> 	sysnotify#func|void (*func)(void*, char*)
>
> I understand knowledge of types is harder but if I use just basic
> types
> this sounds doable to me. Before I write it, does anyone seem such a
> beast?
>
> -Steve
>




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [9fans] stub function generator
  2008-06-05 18:44 ` John Stalker
@ 2008-06-09  9:02   ` Steve Simon
  2008-06-09 11:56     ` Bruce Ellis
  0 siblings, 1 reply; 6+ messages in thread
From: Steve Simon @ 2008-06-09  9:02 UTC (permalink / raw)
  To: 9fans

>  From what I remember, tendra does something like what you want.  It's
> been a while since I looked at it, though.

FWIW there was a port of tendra to plan9 in progress though I don't
know its status now, though it looks like a hugue task.

-Steve



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [9fans] stub function generator
  2008-06-09  9:02   ` Steve Simon
@ 2008-06-09 11:56     ` Bruce Ellis
  0 siblings, 0 replies; 6+ messages in thread
From: Bruce Ellis @ 2008-06-09 11:56 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

both cyntax(1) and cin(1) do this but i don't know where you can find them.

i think they are in 9&10 edition.

brucee

On Mon, Jun 9, 2008 at 7:02 PM, Steve Simon <steve@quintile.net> wrote:
>>  From what I remember, tendra does something like what you want.  It's
>> been a while since I looked at it, though.
>
> FWIW there was a port of tendra to plan9 in progress though I don't
> know its status now, though it looks like a hugue task.
>
> -Steve
>
>



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2008-06-09 11:56 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-06-05 14:18 [9fans] stub function generator Steve Simon
2008-06-05 15:14 ` erik quanstrom
2008-06-05 18:44 ` John Stalker
2008-06-09  9:02   ` Steve Simon
2008-06-09 11:56     ` Bruce Ellis
2008-06-06  1:39 ` Pietro Gagliardi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).