3 # MKlib_gen.sh -- generate sources from curses.h macro definitions
5 # ($Id: MKlib_gen.sh,v 1.18 2002/04/30 00:37:55 tom Exp $)
7 ##############################################################################
8 # Copyright (c) 1998-2001,2002 Free Software Foundation, Inc. #
10 # Permission is hereby granted, free of charge, to any person obtaining a #
11 # copy of this software and associated documentation files (the "Software"), #
12 # to deal in the Software without restriction, including without limitation #
13 # the rights to use, copy, modify, merge, publish, distribute, distribute #
14 # with modifications, sublicense, and/or sell copies of the Software, and to #
15 # permit persons to whom the Software is furnished to do so, subject to the #
16 # following conditions: #
18 # The above copyright notice and this permission notice shall be included in #
19 # all copies or substantial portions of the Software. #
21 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
22 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
23 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL #
24 # THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
25 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING #
26 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER #
27 # DEALINGS IN THE SOFTWARE. #
29 # Except as contained in this notice, the name(s) of the above copyright #
30 # holders shall not be used in advertising or otherwise to promote the sale, #
31 # use or other dealings in this Software without prior written #
33 ##############################################################################
35 # The XSI Curses standard requires all curses entry points to exist as
36 # functions, even though many definitions would normally be shadowed
37 # by macros. Rather than hand-hack all that code, we actually
38 # generate functions from the macros.
40 # This script accepts a file of prototypes on standard input. It discards
41 # any that don't have a `generated' comment attached. It then parses each
42 # prototype (relying on the fact that none of the macros take function
43 # pointer or array arguments) and generates C source from it.
45 # Here is what the pipeline stages are doing:
47 # 1. sed: extract prototypes of generated functions
48 # 2. sed: decorate prototypes with generated arguments a1. a2,...z
49 # 3. awk: generate the calls with args matching the formals
50 # 4. sed: prefix function names in prototypes so the preprocessor won't expand
52 # 5. cpp: macro-expand the file so the macro calls turn into C calls
53 # 6. awk: strip the expansion junk off the front and add the new header
54 # 7. sed: squeeze spaces, strip off gen_ prefix, create needed #undef
57 preprocessor="$1 -I../include"
69 trap "rm -f $ED1 $ED2 $ED3 $ED4 $AW1 $AW2 $TMP" 0 1 2 5 15
72 if test "$USE" = implemented ; then
75 /^extern.*implemented/{
77 s/^.*implemented:\([^ *]*\).*/P_#if_USE_\1_SUPPORT/p
79 s/^extern \([^;]*\);.*/\1/p
81 s/^.*implemented:\([^ *]*\).*/P_#endif/p
85 s/^.*generated:\([^ *]*\).*/P_#if_USE_\1_SUPPORT/p
87 s/^extern \([^;]*\);.*/\1/p
89 s/^.*generated:\([^ *]*\).*/P_#endif/p
97 s/^.*${ALL}:\([^ *]*\).*/P_#if_USE_\1_SUPPORT/p
99 s/^extern \([^;]*\);.*/\1/p
101 s/^.*${ALL}:\([^ *]*\).*/P_#endif/p
150 if test "$USE" = generated ; then
152 s/^\(.*\) \(.*\) (\(.*\))\$/NCURSES_EXPORT(\1) \2 (\3)/
156 /^\(.*\) \(.*\) (\(.*\))\$/ {
158 s/^\(.*\) \(.*\) (\(.*\))\$/extern \1 call_\2 (\3);/
161 s/^\(.*\) \(.*\) (\(.*\))\$/\1 call_\2 (\3)/
185 for (i = 1; i <= NF; i++) {
186 if ( $i != "NCURSES_CONST" ) {
192 if ( $first == "chtype" ) {
194 } else if ( $first == "SCREEN" ) {
196 } else if ( $first == "WINDOW" ) {
198 } else if ( $second == "*" ) {
204 for (i = second; i <= NF; i++) {
210 if (using == "generated") {
217 if ($check == "void")
220 for (i = 1; i <= NF; i++)
225 # suppress trace-code for functions that we cannot do properly here,
226 # since they return data.
228 if ($myfunc ~ /innstr/)
230 if ($myfunc ~ /innwstr/)
233 # workaround functions that we do not parse properly
234 if ($myfunc ~ /ripoffline/) {
238 if ($myfunc ~ /wunctrl/) {
242 call = "%%T((T_CALLED(\""
248 for (i = myfunc; i <= NF; i++) {
252 else if ( ch == "va_list" )
254 else if ( ch == "char" )
256 else if ( ch == "int" )
258 else if ( ch == "short" )
260 else if ( ch == "chtype" )
262 else if ( ch == "attr_t" || ch == "NCURSES_ATTR_T" )
265 if ( ch == "," || ch == ")" ) {
267 if ( argtype == "char" ) {
269 comma = comma "_nc_visbuf2(" num ","
273 } else if (argcount != 0) {
274 if ( argtype == "int" || argtype == "short" ) {
277 } else if ( argtype != "" ) {
279 comma = comma "_trace" argtype "2(" num ","
282 comma = comma "(long)"
286 args = args comma "a" ++num;
287 else if ( argcount != 0 && $check != "..." )
288 args = args comma "z"
290 if (pointer == 0 && argcount != 0 && argtype != "" )
297 if ( i == 2 || ch == "(" )
302 call = call ", " args
308 if (match($0, "^void"))
311 call = sprintf("return%s( ", returnType);
315 call = call $myfunc "(";
316 for (i = 1; i < argcount; i++) {
321 if ( argcount != 0 && $check != "..." ) {
326 if (!match($0, "^void"))
332 if (match($0, "^void"))
333 print "%%returnVoid;"
341 print " * DO NOT EDIT THIS FILE BY HAND!"
342 printf " * It is generated by $0 %s.\n", "$USE"
343 if ( "$USE" == "generated" ) {
345 print " * This is a file of trivial functions generated from macro"
346 print " * definitions in curses.h to satisfy the XSI Curses requirement"
347 print " * that every macro also exist as a callable function."
349 print " * It will never be linked unless you call one of the entry"
350 print " * points with its normal macro definition disabled. In that"
351 print " * case, if you have no shared libraries, it will indirectly"
352 print " * pull most of the rest of the library into your link image."
355 print "#include <curses.priv.h>"
358 /^DECLARATIONS/ {start = 1; next;}
359 {if (start) print \$0;}
361 if ( "$USE" != "generated" ) {
362 print "int main(void) { return 0; }"
368 #include <ncurses_cfg.h>
376 | sed -e 's/NCURSES_EXPORT(\(.*\)) \(.*\) (\(.*\))/\1 \2(\3)/' \
378 | $AWK -f $AW1 using=$USE \
379 | sed -e 's/^\([a-z_][a-z_]*[ *]*\)/\1 gen_/' -e 's/ / /g' >>$TMP
381 $preprocessor $TMP 2>/dev/null \
382 | sed -e 's/ / /g' -e 's/^ //' \
386 -e 's/^.*T_CALLED.*returnCode( \([a-z].*) \));/ return \1;/' \
387 -e 's/^.*T_CALLED.*returnCode( \((wmove.*) \));/ return \1;/' \