3 # MKlib_gen.sh -- generate sources from curses.h macro definitions
5 # ($Id: MKlib_gen.sh,v 1.9 1997/05/10 23:48:19 tom Exp $)
7 # The XSI Curses standard requires all curses entry points to exist as
8 # functions, even though many definitions would normally be shadowed
9 # by macros. Rather than hand-hack all that code, we actually
10 # generate functions from the macros.
12 # This script accepts a file of prototypes on standard input. It discards
13 # any that don't have a `generated' comment attached. It then parses each
14 # prototype (relying on the fact that none of the macros take function
15 # pointer or array arguments) and generates C source from it.
17 # Here is what the pipeline stages are doing:
19 # 1. sed: extract prototypes of generated functions
20 # 2. sed: decorate prototypes with generated arguments a1. a2,...z
21 # 3. awk: generate the calls with args matching the formals
22 # 4. sed: prefix function names in prototypes so the preprocessor won't expand
24 # 5. cpp: macro-expand the file so the macro calls turn into C calls
25 # 6. awk: strip the expansion junk off the front and add the new header
26 # 7. sed: squeeze spaces, strip off gen_ prefix, create needed #undef
29 preprocessor="$1 -I../include"
32 trap "rm -f $TMP" 0 1 2 5 15
35 #include <ncurses_cfg.h>
41 sed -n -e "/^extern.*generated/s/^extern \([^;]*\);.*/\1/p" \
72 if (NF == 5 && $4 == "void")
75 for (i = 1; i <= NF; i++)
80 # suppress trace-code for functions that we cannot do properly here,
81 # since they return data.
86 call = "%%T((T_CALLED(\""
92 for (i = 1; i <= NF; i++) {
96 else if ( ch == "va_list" )
98 else if ( ch == "char" )
100 else if ( ch == "int" )
102 else if ( ch == "short" )
104 else if ( ch == "chtype" )
106 else if ( ch == "attr_t" || ch == "NCURSES_ATTR_T" )
109 if ( ch == "," || ch == ")" ) {
111 if ( argtype == "char" ) {
113 comma = comma "_nc_visbuf2(" num ","
117 } else if (argcount != 0) {
118 if ( argtype == "int" || argtype == "short" ) {
121 } else if ( argtype != "" ) {
123 comma = comma "_trace" argtype "2(" num ","
126 comma = comma "(long)"
130 args = args comma "a" ++num;
131 else if (argcount != 0)
132 args = args comma "z"
134 if (pointer == 0 && argcount != 0 && argtype != "" )
141 if ( i == 2 || ch == "(" )
146 call = call ", " args
152 if (match($0, "^void"))
155 call = "returnCode( ";
160 for (i = 1; i < argcount; i++)
161 call = call "a" i ", ";
164 if (!match($0, "^void"))
170 if (match($0, "^void"))
171 print "%%returnVoid;"
176 -e '/^\([a-z_][a-z_]*\) /s//\1 gen_/' >$TMP
177 $preprocessor $TMP 2>/dev/null \
181 print " * DO NOT EDIT THIS FILE BY HAND!"
182 print " * It is generated by MKlib_gen.sh."
184 print " * This is a file of trivial functions generated from macro"
185 print " * definitions in curses.h in order to satisfy the XSI Curses"
186 print " * requirement that every macro also exist as a callable"
189 print " * It will never be linked unless you call one of the entry"
190 print " * points with its normal macro definition disabled. In that"
191 print " * case, if you have no shared libraries, it will indirectly"
192 print " * pull most of the rest of the library into your link image."
193 print " * Cope with it."
195 print "#include <curses.priv.h>"
198 /^DECLARATIONS/ {start = 1; next;}
199 {if (start) print $0;}
207 -e 's/^M_/#undef /' \
210 -e 's/^.*T_CALLED.*returnCode( \([a-z].*) \));/ return \1;/' \
211 -e 's/^.*T_CALLED.*returnCode( \((wmove.*) \));/ return \1;/'