awk script to generate .h files?

DEPSTEIN at BBNG.ARPA DEPSTEIN at BBNG.ARPA
Wed Jun 13 05:52:00 AEST 1984


Here's one that I've been using recently.  As you can tell by some of the 
restrictions to rules it reflects my coding style.  It can be expanded
however I've not found it necessary to do so.  I'll send along any
enhancements that I make.

Dave Epstein
depstein at bbn-unix
decvax!bbncca!depstein

---------------------------------------


The file h.awk performs the following in converting .c to .h files:

1.  Copies blank lines to preserve spacing.

2.  Copies all compiler directives beginning with #.

3.  Copies all comments blocks subject to the following restrictions:
    a.  The comment delimiters must be white space padded from the text
    b.  The comment must begin at the start of a line.
    c.  The comment must end at the end of a line.
    Thus the following are comments that will be copied:
	/* this is a legal comment */
	/* this comment will
	   also be copied */
	/*
	   as will this one
	*/

4.  Copies global variable declarations of the form:
	type name;
    converting them to the form:
	extern type name;
    The declarations are subject to the following restrictions:
    a.  The type must be the first word on the line.
    b.  The ; must not be white space separated from the name.

5.  Copies initialized global variable declarations of the form:
	type name = value;
    converting them to:
	extern type name;
    The declarations are subject to the following restrictions:
    a.  The type must be the first word on the line.
    b.  The ; must not be white space separated from the value.
    c.  This does not work for string values with white space in them. (see 8)

6.  Similar to 4, only allows comments, i.e.:
	type foo;		/* comment about variable */
    is converted to:
	extern type foo;	/* comment about variable */
    The comment must begin after the name and terminate the line

7.  Similar to 5, only allows comments, i.e.:
	type foo = bar;		/* comment about initialization */
    is converted to:
	extern type foo;	/* comment about initialization */
    The comment must begin after the name and terminate the line

8.  Copies array declarations of the following form:
	type foo[];
	type bar[3];
	type xxx[3][MAX][MIN] =		/* comment */
	{
	    1, 2, 3			/* obviously slightly incorrect C here */
	};
    converting them to:
	extern type foo[];
	extern type bar[3];
	extern type xxx[3][MAX][MIN];	/* comment */

9.  Strips away above array initializations
    The }; must appear together without intervening white space, they must
    also appear at the beginning or ending of a line.


---------------------------------------



NF == 0

substr($1, 1, 1) == "#"

$1 == "/*", $NF == "*/"	{
    printf ("%s\n", $0)
}

NF == 2 && substr($2, length($2), 1) == ";"	{
    printf ("extern %s\n", $0)
}

NF == 4 && $3 == "=" && substr($4, length($4), 1) == ";"	{
    printf ("extern %s %s;\n", $1, $2)
}

substr($2, length($2), 1) == ";" && $3 == "/*" && $NF == "*/"	{
    printf ("extern %s\n", $0)
}

$3 == "=" && substr($4, length($4), 1) == ";" && $5 == "/*" && $NF == "*/"	{
    printf ("extern %s %s;", $1, $2)
    for (i = 5; i <= NF; i++)
	printf (" %s", $i)
    printf ("\n")
}

NF >= 3 && substr($2, length($2), 1) == "]" && $3 == "="	{
    printf ("extern %s %s;", $1, $2)

    if ($NF == "*/")
    {
	comment = 0;
	for (i =4; i <= NF; i++)
	{
	    if ($i == "/*")
		comment = 1

	    if (comment == 1)
		printf (" %s", $i)
	}
    }

    printf ("\n");
}

$1 == "{", $1 == "};" || $NF == "};"	{}



More information about the Comp.unix.wizards mailing list