3 # MKlib_gen.sh -- generate sources from curses.h macro definitions
5 # ($Id: MKlib_gen.sh,v 1.11 1998/01/17 14:16:52 Juan.Jose.Garcia.Ripoll 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"
36 trap "rm -f $ED1 $ED2 $ED3 $AW1 $TMP" 0 1 2 5 15
39 #include <ncurses_cfg.h>
48 s/^.*generated:\([^ *]*\).*/P_#if_USE_\1_SUPPORT/p
50 s/^extern \([^;]*\);.*/\1/p
52 s/^.*generated:\([^ *]*\).*/P_#endif/p
120 if (NF == 5 && $4 == "void")
123 for (i = 1; i <= NF; i++)
128 # suppress trace-code for functions that we cannot do properly here,
129 # since they return data.
134 call = "%%T((T_CALLED(\""
140 for (i = 1; i <= NF; i++) {
144 else if ( ch == "va_list" )
146 else if ( ch == "char" )
148 else if ( ch == "int" )
150 else if ( ch == "short" )
152 else if ( ch == "chtype" )
154 else if ( ch == "attr_t" || ch == "NCURSES_ATTR_T" )
157 if ( ch == "," || ch == ")" ) {
159 if ( argtype == "char" ) {
161 comma = comma "_nc_visbuf2(" num ","
165 } else if (argcount != 0) {
166 if ( argtype == "int" || argtype == "short" ) {
169 } else if ( argtype != "" ) {
171 comma = comma "_trace" argtype "2(" num ","
174 comma = comma "(long)"
178 args = args comma "a" ++num;
179 else if (argcount != 0)
180 args = args comma "z"
182 if (pointer == 0 && argcount != 0 && argtype != "" )
189 if ( i == 2 || ch == "(" )
194 call = call ", " args
200 if (match($0, "^void"))
203 call = "returnCode( ";
208 for (i = 1; i < argcount; i++)
209 call = call "a" i ", ";
212 if (!match($0, "^void"))
218 if (match($0, "^void"))
219 print "%%returnVoid;"
224 sed -n -f $ED1 | sed -f $ED2 \
227 -e '/^\([a-z_][a-z_]*\) /s//\1 gen_/' >$TMP
228 $preprocessor $TMP 2>/dev/null \
232 print " * DO NOT EDIT THIS FILE BY HAND!"
233 print " * It is generated by MKlib_gen.sh."
235 print " * This is a file of trivial functions generated from macro"
236 print " * definitions in curses.h to satisfy the XSI Curses requirement"
237 print " * that every macro also exist as a callable function."
239 print " * It will never be linked unless you call one of the entry"
240 print " * points with its normal macro definition disabled. In that"
241 print " * case, if you have no shared libraries, it will indirectly"
242 print " * pull most of the rest of the library into your link image."
244 print "#include <curses.priv.h>"
247 /^DECLARATIONS/ {start = 1; next;}
248 {if (start) print $0;}
252 -e 's/^.*T_CALLED.*returnCode( \([a-z].*) \));/ return \1;/' \
253 -e 's/^.*T_CALLED.*returnCode( \((wmove.*) \));/ return \1;/'