3 # MKlib_gen.sh -- generate sources from curses.h macro definitions
5 # ($Id: MKlib_gen.sh,v 1.30 2008/01/05 23:21:10 tom Exp $)
7 ##############################################################################
8 # Copyright (c) 1998-2007,2008 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 # keep the editing independent of locale:
58 if test "${LANGUAGE+set}" = set; then LANGUAGE=C; export LANGUAGE; fi
59 if test "${LANG+set}" = set; then LANG=C; export LANG; fi
60 if test "${LC_ALL+set}" = set; then LC_ALL=C; export LC_ALL; fi
61 if test "${LC_MESSAGES+set}" = set; then LC_MESSAGES=C; export LC_MESSAGES; fi
62 if test "${LC_CTYPE+set}" = set; then LC_CTYPE=C; export LC_CTYPE; fi
63 if test "${LC_COLLATE+set}" = set; then LC_COLLATE=C; export LC_COLLATE; fi
65 preprocessor="$1 -DNCURSES_INTERNALS -I../include"
77 trap "rm -f $ED1 $ED2 $ED3 $ED4 $AW1 $AW2 $TMP" 0 1 2 5 15
80 if test "$USE" = implemented ; then
83 /^extern.*implemented/{
85 s/^.*implemented:\([^ *]*\).*/P_POUNDCif_USE_\1_SUPPORT/p
87 s/^extern \([^;]*\);.*/\1/p
89 s/^.*implemented:\([^ *]*\).*/P_POUNDCendif/p
93 s/^.*generated:\([^ *]*\).*/P_POUNDCif_USE_\1_SUPPORT/p
95 s/^extern \([^;]*\);.*/\1/p
97 s/^.*generated:\([^ *]*\).*/P_POUNDCendif/p
105 s/^.*${ALL}:\([^ *]*\).*/P_POUNDCif_USE_\1_SUPPORT/p
107 s/^extern \([^;]*\);.*/\1/p
109 s/^.*${ALL}:\([^ *]*\).*/P_POUNDCendif/p
144 s/^P_POUNDCendif/#endif/
159 if test "$USE" = generated ; then
161 s/^\(.*\) \(.*\) (\(.*\))\$/NCURSES_EXPORT(\1) \2 (\3)/
165 /^\(.*\) \(.*\) (\(.*\))\$/ {
167 s/^\(.*\) \(.*\) (\(.*\))\$/extern \1 call_\2 (\3);/
170 s/^\(.*\) \(.*\) (\(.*\))\$/\1 call_\2 (\3)/
194 for (i = 1; i <= NF; i++) {
195 if ( $i != "NCURSES_CONST" ) {
201 if ( $first == "chtype" ) {
203 } else if ( $first == "SCREEN" ) {
205 } else if ( $first == "WINDOW" ) {
207 } else if ( $first == "attr_t" || $second == "attrset" || $second == "standout" || $second == "standend" || $second == "wattrset" || $second == "wstandout" || $second == "wstandend" ) {
209 } else if ( $first == "bool" || $first == "NCURSES_BOOL" ) {
211 } else if ( $second == "*" ) {
217 for (i = second; i <= NF; i++) {
223 if (using == "generated") {
230 if ($check == "void")
233 for (i = 1; i <= NF; i++)
238 # suppress trace-code for functions that we cannot do properly here,
239 # since they return data.
241 if ($myfunc ~ /innstr/)
243 if ($myfunc ~ /innwstr/)
246 # workaround functions that we do not parse properly
247 if ($myfunc ~ /ripoffline/) {
251 if ($myfunc ~ /wunctrl/) {
255 call = "%%T((T_CALLED(\""
263 for (i = myfunc; i <= NF; i++) {
267 else if ( ch == "va_list" )
269 else if ( ch == "..." )
271 else if ( ch == "char" )
273 else if ( ch == "int" )
275 else if ( ch == "short" )
277 else if ( ch == "chtype" )
279 else if ( ch == "attr_t" || ch == "NCURSES_ATTR_T" )
282 if ( ch == "," || ch == ")" ) {
285 } else if (varargs) {
287 } else if (pointer) {
288 if ( argtype == "char" ) {
290 comma = comma "_nc_visbuf2(" num ","
294 } else if (argcount != 0) {
295 if ( argtype == "int" || argtype == "short" ) {
298 } else if ( argtype != "" ) {
300 comma = comma "_trace" argtype "2(" num ","
303 comma = comma "(long)"
307 args = args comma "a" ++num;
308 } else if ( argcount != 0 ) {
310 args = args comma "\"va_list\""
311 } else if ( varargs ) {
312 args = args comma "\"...\""
314 args = args comma "z"
318 if (pointer == 0 && argcount != 0 && argtype != "" )
325 if ( i == 2 || ch == "(" )
330 call = call ", " args
336 if (match($0, "^void"))
339 call = sprintf("return%s( ", returnType);
343 call = call $myfunc "(";
344 for (i = 1; i < argcount; i++) {
349 if ( argcount != 0 && $check != "..." ) {
354 if (!match($0, "^void"))
360 if (match($0, "^void"))
361 print "%%returnVoid;"
369 print " * DO NOT EDIT THIS FILE BY HAND!"
370 printf " * It is generated by $0 %s.\n", "$USE"
371 if ( "$USE" == "generated" ) {
373 print " * This is a file of trivial functions generated from macro"
374 print " * definitions in curses.h to satisfy the XSI Curses requirement"
375 print " * that every macro also exist as a callable function."
377 print " * It will never be linked unless you call one of the entry"
378 print " * points with its normal macro definition disabled. In that"
379 print " * case, if you have no shared libraries, it will indirectly"
380 print " * pull most of the rest of the library into your link image."
383 print "#define NCURSES_ATTR_T int"
384 print "#include <curses.priv.h>"
387 /^DECLARATIONS/ {start = 1; next;}
388 {if (start) print \$0;}
390 if ( "$USE" != "generated" ) {
391 print "int main(void) { return 0; }"
397 #include <ncurses_cfg.h>
398 #undef NCURSES_NOMACROS
406 | sed -e 's/NCURSES_EXPORT(\(.*\)) \(.*\) (\(.*\))/\1 \2(\3)/' \
408 | $AWK -f $AW1 using=$USE \
411 -e 's/^\([a-zA-Z_][a-zA-Z_]*[ *]*\)/\1 gen_/' \
415 $preprocessor $TMP 2>/dev/null \
419 -e 's/^_Bool/bool/' \
423 -e 's/^.*T_CALLED.*returnCode( \([a-z].*) \));/ return \1;/' \
424 -e 's/^.*T_CALLED.*returnCode( \((wmove.*) \));/ return \1;/' \