3 # MKlib_gen.sh -- generate sources from curses.h macro definitions
5 # ($Id: MKlib_gen.sh,v 1.12 2000/07/29 16:30:11 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"
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
116 if ( $1 == "chtype" ) {
125 if (NF == 5 && $4 == "void")
128 for (i = 1; i <= NF; i++)
133 # suppress trace-code for functions that we cannot do properly here,
134 # since they return data.
139 call = "%%T((T_CALLED(\""
145 for (i = 1; i <= NF; i++) {
149 else if ( ch == "va_list" )
151 else if ( ch == "char" )
153 else if ( ch == "int" )
155 else if ( ch == "short" )
157 else if ( ch == "chtype" )
159 else if ( ch == "attr_t" || ch == "NCURSES_ATTR_T" )
162 if ( ch == "," || ch == ")" ) {
164 if ( argtype == "char" ) {
166 comma = comma "_nc_visbuf2(" num ","
170 } else if (argcount != 0) {
171 if ( argtype == "int" || argtype == "short" ) {
174 } else if ( argtype != "" ) {
176 comma = comma "_trace" argtype "2(" num ","
179 comma = comma "(long)"
183 args = args comma "a" ++num;
184 else if (argcount != 0)
185 args = args comma "z"
187 if (pointer == 0 && argcount != 0 && argtype != "" )
194 if ( i == 2 || ch == "(" )
199 call = call ", " args
205 if (match($0, "^void"))
208 call = sprintf("return%s( ", returnType);
213 for (i = 1; i < argcount; i++)
214 call = call "a" i ", ";
217 if (!match($0, "^void"))
223 if (match($0, "^void"))
224 print "%%returnVoid;"
229 sed -n -f $ED1 | sed -f $ED2 \
232 -e '/^\([a-z_][a-z_]*\) /s//\1 gen_/' >$TMP
233 $preprocessor $TMP 2>/dev/null \
237 print " * DO NOT EDIT THIS FILE BY HAND!"
238 print " * It is generated by MKlib_gen.sh."
240 print " * This is a file of trivial functions generated from macro"
241 print " * definitions in curses.h to satisfy the XSI Curses requirement"
242 print " * that every macro also exist as a callable function."
244 print " * It will never be linked unless you call one of the entry"
245 print " * points with its normal macro definition disabled. In that"
246 print " * case, if you have no shared libraries, it will indirectly"
247 print " * pull most of the rest of the library into your link image."
249 print "#include <curses.priv.h>"
252 /^DECLARATIONS/ {start = 1; next;}
253 {if (start) print $0;}
257 -e 's/^.*T_CALLED.*returnCode( \([a-z].*) \));/ return \1;/' \
258 -e 's/^.*T_CALLED.*returnCode( \((wmove.*) \));/ return \1;/'