# mdgen.awk # C/C++ module header file generator # Copyright (C) Horstmann Software Design Corp. 1988..1993 # All rights reserved. function bracecount( s, left, right ) { return( split( s, lsplit, left ) - split( s, rsplit, right ) ) } function strupper( s ) { for( _i = 1; _i <= 26; _i++ ) gsub( substr( "abcdefghijklmnopqrstuvwxyz", _i, 1 ), substr( "ABCDEFGHIJKLMNOPQRSTUVWXYZ", _i, 1 ), s ) return s } BEGIN { outname = ARGV[1]; sub( /^[A-Za-z]:/, "", outname ) sub( /^.*[\\\/]/, "", outname ) sub( /\..*/, ".H", outname ) print "[mdgen.awk] Copyright (C) HSDC 1988..1993. All Rights Reserved." print ARGV[1] " -> " outname for (i = ARGC-1; i >= 2; i--) { if (strupper(substr(ARGV[i],1,2)) == "-L") linenum = 1; else if (strupper(substr(ARGV[i],1,2)) == "=L") linenum = 1; else if (strupper(substr(ARGV[i],1,2)) == "-C") compare = 1; else if (strupper(substr(ARGV[i],1,2)) == "=C") compare = 1; ARGV[i] = ""; } ARGC = 2; if (compare) outfile = outname "$$"; else outfile = outname; if( match( ARGV[1], /[^\\\/]*\./ ) ) { hname = strupper( substr( ARGV[1], RSTART, RLENGTH-1 ) ) "_H" gsub( /[^A-Za-z]/, "_", hname); print "#ifndef " hname >outfile print "#define " hname >outfile print "" >outfile } getline # copy header comment oldStyle = substr( $0, 1, 2 ) == "/*" while( oldStyle || index( $0, "//" ) ) { sub( /\.[cC](xx|pp)?/, ".h" ) # change filename to filename.h print >outfile if( index( $0, "*/" ) ) oldStyle = 0 getline } keyword[ "typedef" ] = 1; keyword[ "enum" ] = 1; keyword[ "class" ] = 1; keyword[ "struct" ] = 1; keyword[ "union" ] = 1; keyword[ "inline" ] = 1; keyword[ "const" ] = 1; keyword[ "template" ] = 1; } $1 == "#ifndef" && $2 == "EXPORT" { print "" >outfile # separate items by blank line if (linenum) print "#line " NR " \"" ARGV[1] "\"" >outfile #print line number for error reporting print "" >outfile # we won't copy the #ifndef line b = 1; while (b > 0) { getline if (substr($1, 1, 3) == "#if") b++; else if ($1 == "#endif") b--; if ($1 == "EXPORT") $1 = "" # for compatibility if (b > 0) print >outfile } } $1 == "EXPORT" { # tag for items to be included print "" >outfile # separate items by blank line if (linenum) print "#line " NR " \"" ARGV[1] "\"" >outfile #print line number for error reporting if (match($2, "^[a-z]+") && keyword[substr($2,1,RLENGTH)]) { # if one of the standard keywords b = bracecount( $0, "{", "}" ); # skip past matching {} $1 = "" # delete EXPORT while( b != 0 || (index( $0, ";" )==0 && index( $0, "}" )==0) ) { print >outfile getline b += bracecount( $0, "{", "}" ) } print >outfile } else if (substr($2, 1, 2) == "/*" || substr($2, 1, 2) == "//" || $2 == "" ) { print "" >outfile # we won't copy the #ifndef line getline # test for preprocessor command if( $1 == "#include" ) { # check whether "simple.h" if( $2 ~ /\"[A-Za-z_][A-Za-z0-9_]*.[Hh]\"/ ) { print "#ifndef " strupper( substr( $2, 2, length( $2 ) - 4 ) ) "_H" >outfile print >outfile print "#endif" >outfile } else print >outfile } else if( substr( $1, 1, 1 ) == "#" ) { # on the next line print >outfile # could be #define, #include while( substr( $0, length( $0 ) ) == "\\" ) { getline # could be multiline #define print >outfile } } } else { # function or variable declaration if( $2 == "extern" ) $1 = "" # extern "C" else $1 = "extern" # replace EXPORT with extern b = bracecount( $0, "(", ")" ); # skip past matching () while( b ) { print >outfile getline b += bracecount( $0, "(", ")" ) } split( $0, out, "=" ) # split off initialization if( bracecount( out[1], "(", ")" ) || index( out[1], "operator" ) ) print $0 ";" >outfile # oops--"=" was for default arg else { # or operator?= split( out[1], out, ";" ) # split off semicolon (if exists) print out[1] ";" >outfile } } } END { # print #endif print "" >outfile print "#endif" >outfile close(outfile); if (compare) system( "mdgencmp " outname " " outfile ) }