Deleted Added
full compact
MKlib_gen.sh (76726) MKlib_gen.sh (97049)
1#!/bin/sh
2#
3# MKlib_gen.sh -- generate sources from curses.h macro definitions
4#
1#!/bin/sh
2#
3# MKlib_gen.sh -- generate sources from curses.h macro definitions
4#
5# ($Id: MKlib_gen.sh,v 1.13 2000/12/10 00:30:25 tom Exp $)
5# ($Id: MKlib_gen.sh,v 1.18 2002/04/30 00:37:55 tom Exp $)
6#
6#
7##############################################################################
8# Copyright (c) 1998-2001,2002 Free Software Foundation, Inc. #
9# #
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: #
17# #
18# The above copyright notice and this permission notice shall be included in #
19# all copies or substantial portions of the Software. #
20# #
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. #
28# #
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 #
32# authorization. #
33##############################################################################
34#
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.
11#
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

--- 8 unchanged lines hidden (view full) ---

23# them.
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
27#
28
29preprocessor="$1 -I../include"
30AWK="$2"
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.
39#
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

--- 8 unchanged lines hidden (view full) ---

51# them.
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
55#
56
57preprocessor="$1 -I../include"
58AWK="$2"
31ED1=sed1$$.sed
32ED2=sed2$$.sed
33ED3=sed3$$.sed
34AW1=awk1$$.awk
35TMP=gen$$.c
36trap "rm -f $ED1 $ED2 $ED3 $AW1 $TMP" 0 1 2 5 15
59USE="$3"
37
60
38(cat <<EOF
39#include <ncurses_cfg.h>
40#include <curses.h>
61PID=$$
62ED1=sed1_${PID}.sed
63ED2=sed2_${PID}.sed
64ED3=sed3_${PID}.sed
65ED4=sed4_${PID}.sed
66AW1=awk1_${PID}.awk
67AW2=awk2_${PID}.awk
68TMP=gen__${PID}.c
69trap "rm -f $ED1 $ED2 $ED3 $ED4 $AW1 $AW2 $TMP" 0 1 2 5 15
41
70
42DECLARATIONS
43
44EOF
45cat >$ED1 <
71ALL=$USE
72if test "$USE" = implemented ; then
73 CALL="call_"
74 cat >$ED1 <<EOF1
75/^extern.*implemented/{
76 h
77 s/^.*implemented:\([^ *]*\).*/P_#if_USE_\1_SUPPORT/p
78 g
79 s/^extern \([^;]*\);.*/\1/p
80 g
81 s/^.*implemented:\([^ *]*\).*/P_#endif/p
82}
46/^extern.*generated/{
47 h
48 s/^.*generated:\([^ *]*\).*/P_#if_USE_\1_SUPPORT/p
49 g
50 s/^extern \([^;]*\);.*/\1/p
51 g
52 s/^.*generated:\([^ *]*\).*/P_#endif/p
53}
54EOF1
83/^extern.*generated/{
84 h
85 s/^.*generated:\([^ *]*\).*/P_#if_USE_\1_SUPPORT/p
86 g
87 s/^extern \([^;]*\);.*/\1/p
88 g
89 s/^.*generated:\([^ *]*\).*/P_#endif/p
90}
91EOF1
92else
93 CALL=""
94 cat >$ED1 <<EOF1
95/^extern.*${ALL}/{
96 h
97 s/^.*${ALL}:\([^ *]*\).*/P_#if_USE_\1_SUPPORT/p
98 g
99 s/^extern \([^;]*\);.*/\1/p
100 g
101 s/^.*${ALL}:\([^ *]*\).*/P_#endif/p
102}
103EOF1
104fi
55
56cat >$ED2 <<EOF2
57/^P_/b nc
58/(void)/b nc
59 s/,/ a1% /
60 s/,/ a2% /
61 s/,/ a3% /
62 s/,/ a4% /

--- 6 unchanged lines hidden (view full) ---

69 s/,/ a11% /
70 s/,/ a12% /
71 s/,/ a13% /
72 s/,/ a14% /
73 s/,/ a15% /
74 s/*/ * /g
75 s/%/ , /g
76 s/)/ z)/
105
106cat >$ED2 <<EOF2
107/^P_/b nc
108/(void)/b nc
109 s/,/ a1% /
110 s/,/ a2% /
111 s/,/ a3% /
112 s/,/ a4% /

--- 6 unchanged lines hidden (view full) ---

119 s/,/ a11% /
120 s/,/ a12% /
121 s/,/ a13% /
122 s/,/ a14% /
123 s/,/ a15% /
124 s/*/ * /g
125 s/%/ , /g
126 s/)/ z)/
127 s/\.\.\. z)/...)/
77:nc
78 /(/s// ( /
79 s/)/ )/
80EOF2
81
82cat >$ED3 <<EOF3
83/^P_/{
84 s/^P_#if_/#if /
85 s/^P_//
86 b done
87}
88 s/ */ /g
89 s/ */ /g
90 s/ ,/,/g
128:nc
129 /(/s// ( /
130 s/)/ )/
131EOF2
132
133cat >$ED3 <<EOF3
134/^P_/{
135 s/^P_#if_/#if /
136 s/^P_//
137 b done
138}
139 s/ */ /g
140 s/ */ /g
141 s/ ,/,/g
142 s/( /(/g
91 s/ )/)/g
92 s/ gen_/ /
93 s/^M_/#undef /
94 /^%%/s// /
95:done
96EOF3
97
143 s/ )/)/g
144 s/ gen_/ /
145 s/^M_/#undef /
146 /^%%/s// /
147:done
148EOF3
149
150if test "$USE" = generated ; then
151cat >$ED4 <<EOF
152 s/^\(.*\) \(.*\) (\(.*\))\$/NCURSES_EXPORT(\1) \2 (\3)/
153EOF
154else
155cat >$ED4 <<EOF
156/^\(.*\) \(.*\) (\(.*\))\$/ {
157 h
158 s/^\(.*\) \(.*\) (\(.*\))\$/extern \1 call_\2 (\3);/
159 p
160 g
161 s/^\(.*\) \(.*\) (\(.*\))\$/\1 call_\2 (\3)/
162 }
163EOF
164fi
165
98cat >$AW1 <<\EOF1
99BEGIN {
100 skip=0;
101 }
166cat >$AW1 <<\EOF1
167BEGIN {
168 skip=0;
169 }
102 /^P_#if/ {
170/^P_#if/ {
103 print "\n"
104 print $0
105 skip=0;
171 print "\n"
172 print $0
173 skip=0;
106 }
107 /^P_#endif/ {
174}
175/^P_#endif/ {
108 print $0
109 skip=1;
176 print $0
177 skip=1;
110 }
111 $0 !~ /^P_/ {
178}
179$0 !~ /^P_/ {
112 if (skip)
113 print "\n"
114 skip=1;
115
180 if (skip)
181 print "\n"
182 skip=1;
183
116 if ( $1 == "chtype" ) {
184 first=$1
185 for (i = 1; i <= NF; i++) {
186 if ( $i != "NCURSES_CONST" ) {
187 first = i;
188 break;
189 }
190 }
191 second = first + 1;
192 if ( $first == "chtype" ) {
117 returnType = "Char";
193 returnType = "Char";
194 } else if ( $first == "SCREEN" ) {
195 returnType = "SP";
196 } else if ( $first == "WINDOW" ) {
197 returnType = "Win";
198 } else if ( $second == "*" ) {
199 returnType = "Ptr";
118 } else {
119 returnType = "Code";
120 }
200 } else {
201 returnType = "Code";
202 }
121 print "M_" $2
203 myfunc = second;
204 for (i = second; i <= NF; i++) {
205 if ($i != "*") {
206 myfunc = i;
207 break;
208 }
209 }
210 if (using == "generated") {
211 print "M_" $myfunc
212 }
122 print $0;
123 print "{";
124 argcount = 1;
213 print $0;
214 print "{";
215 argcount = 1;
125 if (NF == 5 && $4 == "void")
216 check = NF - 1;
217 if ($check == "void")
126 argcount = 0;
127 if (argcount != 0) {
128 for (i = 1; i <= NF; i++)
129 if ($i == ",")
130 argcount++;
131 }
132
133 # suppress trace-code for functions that we cannot do properly here,
134 # since they return data.
135 dotrace = 1;
218 argcount = 0;
219 if (argcount != 0) {
220 for (i = 1; i <= NF; i++)
221 if ($i == ",")
222 argcount++;
223 }
224
225 # suppress trace-code for functions that we cannot do properly here,
226 # since they return data.
227 dotrace = 1;
136 if ($2 == "innstr")
228 if ($myfunc ~ /innstr/)
137 dotrace = 0;
229 dotrace = 0;
230 if ($myfunc ~ /innwstr/)
231 dotrace = 0;
138
232
233 # workaround functions that we do not parse properly
234 if ($myfunc ~ /ripoffline/) {
235 dotrace = 0;
236 argcount = 2;
237 }
238 if ($myfunc ~ /wunctrl/) {
239 dotrace = 0;
240 }
241
139 call = "%%T((T_CALLED(\""
140 args = ""
141 comma = ""
142 num = 0;
143 pointer = 0;
144 argtype = ""
242 call = "%%T((T_CALLED(\""
243 args = ""
244 comma = ""
245 num = 0;
246 pointer = 0;
247 argtype = ""
145 for (i = 1; i <= NF; i++) {
248 for (i = myfunc; i <= NF; i++) {
146 ch = $i;
147 if ( ch == "*" )
148 pointer = 1;
149 else if ( ch == "va_list" )
150 pointer = 1;
151 else if ( ch == "char" )
152 argtype = "char";
153 else if ( ch == "int" )

--- 22 unchanged lines hidden (view full) ---

176 comma = comma "_trace" argtype "2(" num ","
177 } else {
178 call = call "%#lx"
179 comma = comma "(long)"
180 }
181 }
182 if (ch == ",")
183 args = args comma "a" ++num;
249 ch = $i;
250 if ( ch == "*" )
251 pointer = 1;
252 else if ( ch == "va_list" )
253 pointer = 1;
254 else if ( ch == "char" )
255 argtype = "char";
256 else if ( ch == "int" )

--- 22 unchanged lines hidden (view full) ---

279 comma = comma "_trace" argtype "2(" num ","
280 } else {
281 call = call "%#lx"
282 comma = comma "(long)"
283 }
284 }
285 if (ch == ",")
286 args = args comma "a" ++num;
184 else if (argcount != 0)
287 else if ( argcount != 0 && $check != "..." )
185 args = args comma "z"
186 call = call ch
187 if (pointer == 0 && argcount != 0 && argtype != "" )
188 args = args ")"
189 if (args != "")
190 comma = ", "
191 pointer = 0;
192 argtype = ""

--- 11 unchanged lines hidden (view full) ---

204
205 if (match($0, "^void"))
206 call = ""
207 else if (dotrace)
208 call = sprintf("return%s( ", returnType);
209 else
210 call = "%%return ";
211
288 args = args comma "z"
289 call = call ch
290 if (pointer == 0 && argcount != 0 && argtype != "" )
291 args = args ")"
292 if (args != "")
293 comma = ", "
294 pointer = 0;
295 argtype = ""

--- 11 unchanged lines hidden (view full) ---

307
308 if (match($0, "^void"))
309 call = ""
310 else if (dotrace)
311 call = sprintf("return%s( ", returnType);
312 else
313 call = "%%return ";
314
212 call = call $2 "(";
213 for (i = 1; i < argcount; i++)
214 call = call "a" i ", ";
215 if (argcount != 0)
315 call = call $myfunc "(";
316 for (i = 1; i < argcount; i++) {
317 if (i != 1)
318 call = call ", ";
319 call = call "a" i;
320 }
321 if ( argcount != 0 && $check != "..." ) {
322 if (argcount != 1)
323 call = call ", ";
216 call = call "z";
324 call = call "z";
325 }
217 if (!match($0, "^void"))
218 call = call ") ";
219 if (dotrace)
220 call = call ")";
221 print call ";"
222
223 if (match($0, "^void"))
224 print "%%returnVoid;"
225 print "}";
226}
227EOF1
228
326 if (!match($0, "^void"))
327 call = call ") ";
328 if (dotrace)
329 call = call ")";
330 print call ";"
331
332 if (match($0, "^void"))
333 print "%%returnVoid;"
334 print "}";
335}
336EOF1
337
229sed -n -f $ED1 \
230| sed -e 's/NCURSES_EXPORT(\(.*\)) \(.*\) (\(.*\))/\1 \2(\3)/' \
231| sed -f $ED2 \
232| $AWK -f $AW1 ) \
233| sed \
234 -e '/^\([a-z_][a-z_]*\) /s//\1 gen_/' >$TMP
235 $preprocessor $TMP 2>/dev/null \
236| $AWK '
338cat >$AW2 <<EOF1
237BEGIN {
339BEGIN {
238 print "/*"
239 print " * DO NOT EDIT THIS FILE BY HAND!"
240 print " * It is generated by MKlib_gen.sh."
241 print " *"
242 print " * This is a file of trivial functions generated from macro"
243 print " * definitions in curses.h to satisfy the XSI Curses requirement"
244 print " * that every macro also exist as a callable function."
245 print " *"
246 print " * It will never be linked unless you call one of the entry"
247 print " * points with its normal macro definition disabled. In that"
248 print " * case, if you have no shared libraries, it will indirectly"
249 print " * pull most of the rest of the library into your link image."
250 print " */"
251 print "#include <curses.priv.h>"
252 print ""
340 print "/*"
341 print " * DO NOT EDIT THIS FILE BY HAND!"
342 printf " * It is generated by $0 %s.\n", "$USE"
343 if ( "$USE" == "generated" ) {
344 print " *"
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."
348 print " *"
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."
253 }
353 }
354 print " */"
355 print "#include <curses.priv.h>"
356 print ""
357 }
254/^DECLARATIONS/ {start = 1; next;}
358/^DECLARATIONS/ {start = 1; next;}
255 {if (start) print $0;}
256' \
359 {if (start) print \$0;}
360END {
361 if ( "$USE" != "generated" ) {
362 print "int main(void) { return 0; }"
363 }
364 }
365EOF1
366
367cat >$TMP <<EOF
368#include <ncurses_cfg.h>
369#include <curses.h>
370
371DECLARATIONS
372
373EOF
374
375sed -n -f $ED1 \
376| sed -e 's/NCURSES_EXPORT(\(.*\)) \(.*\) (\(.*\))/\1 \2(\3)/' \
377| sed -f $ED2 \
378| $AWK -f $AW1 using=$USE \
379| sed -e 's/^\([a-z_][a-z_]*[ *]*\)/\1 gen_/' -e 's/ / /g' >>$TMP
380
381$preprocessor $TMP 2>/dev/null \
382| sed -e 's/ / /g' -e 's/^ //' \
383| $AWK -f $AW2 \
257| sed -f $ED3 \
258| sed \
259 -e 's/^.*T_CALLED.*returnCode( \([a-z].*) \));/ return \1;/' \
260 -e 's/^.*T_CALLED.*returnCode( \((wmove.*) \));/ return \1;/' \
384| sed -f $ED3 \
385| sed \
386 -e 's/^.*T_CALLED.*returnCode( \([a-z].*) \));/ return \1;/' \
387 -e 's/^.*T_CALLED.*returnCode( \((wmove.*) \));/ return \1;/' \
261| sed \
262 -e 's/^\(.*\) \(.*\) (\(.*\))$/NCURSES_EXPORT(\1) \2 (\3)/'
263
388| sed -f $ED4