apply 2 fixes from the glu repository - one to prevent a memleak when malloc fails...
[AROS.git] / scripts / cint.awk
blobbf5cf850d62365c2abaf9eaff200c7cd51917efe
2 BEGIN {
3 TYPES["ULONG"]=1;
4 TYPES["LONG"]=1;
5 TYPES["UBYTE"]=1;
6 TYPES["BYTE"]=1;
7 TYPES["UWORD"]=1;
8 TYPES["WORD"]=1;
9 TYPES["APTR"]=1;
10 TYPES["BPTR"]=1;
11 TYPES["STRPTR"]=1;
12 TYPES["BSTR"]=1;
13 TYPES["BOOL"]=1;
14 TYPES[""]=1;
15 TYPES[""]=1;
17 # 1- types
18 # 2- commands
19 KEYWORDS["struct"]=2;
20 KEYWORDS["int"]=1;
21 KEYWORDS["char"]=1;
22 KEYWORDS["double"]=1;
23 KEYWORDS["float"]=1;
24 KEYWORDS["for"]=10;
25 KEYWORDS["if"]=10;
26 KEYWORDS["else"]=10;
27 KEYWORDS["do"]=10;
28 KEYWORDS["while"]=10;
29 KEYWORDS["switch"]=10;
30 KEYWORDS["case"]=10;
31 KEYWORDS["break"]=10;
32 KEYWORDS["continue"]=10;
33 KEYWORDS["goto"]=10;
34 KEYWORDS["union"]=2;
35 KEYWORDS["enum"]=2;
36 KEYWORDS["typedef"]=1;
37 KEYWORDS["void"]=1;
38 KEYWORDS["unsigned"]=3;
39 KEYWORDS["signed"]=3;
40 KEYWORDS["const"]=3;
41 KEYWORDS["long"]=3;
42 KEYWORDS["short"]=3;
43 KEYWORDS[""]=0;
44 KEYWORDS[""]=0;
46 spaces=" ";
47 for (t=0; t<10; t++)
48 spaces=spaces spaces;
50 yyinit("");
51 rt_type="";
53 while ((token=yylex()) != "EOF")
55 #print "0 -" token "-" yyval "-"
56 if ((token=="keyword" && KEYWORDS[yyval]<10) || token=="typedef")
58 plevel=0;
59 token=read_type(token);
60 fname = yyval;
61 token=yylex();
63 if (token=="(")
65 plevel ++;
66 token=yylex();
68 if (ptr!="")
69 ret=rt_type " " rt_ptr;
70 else
71 ret=rt_type;
73 if (token==")")
75 npar=1;
76 par_type[1]="void";
77 par_ptr[1]="";
78 par_name[1]="";
80 else
82 npar=0;
83 while (token!=")")
85 if (token==",")
86 token=yylex();
87 token=read_type(token);
88 name=yyval;
89 token=yylex();
91 if (plevel != 1)
93 plevel --;
94 name=name ") "
95 while((token==yylex())!=")" || plevel!=1)
97 if (token=="(")
98 plevel ++;
99 else if (token==")")
100 plevel --;
101 name=name " " yyval;
105 npar++;
106 par_type[npar]=rt_type;
107 par_ptr[npar]=rt_ptr;
108 par_name[npar]=name;
109 print "-" rt_type "-" rt_ptr "-" name
113 print "Processing "fname"..." npar
114 file="test/" tolower(fname) ".c"
115 print "/*****************************************************************************\n"
116 print " NAME */"
117 print "\t"ret" "fname" (\n"
118 print "/* SYNOPSIS */"
120 if (npar==1 && par_type[1]=="void" && par_ptr[1] == "" &&
121 par_name[1]=="")
123 print "\tvoid)"
125 else
127 maxlen=0;
128 for (t=1; t<=npar; t++)
130 len=length(par_type[t]);
131 len2=length(par_ptr[t]);
132 len+=len2 ? len2+1 : 0;
133 if (len > maxlen)
134 maxlen = len;
137 for (t=1; t<=npar; t++)
139 len=length(par_type[t]);
140 len2=length(par_ptr[t]);
142 printf("\t%s", par_type[t]);
143 printf("%s", substr(spaces,1,maxlen-len-len2));
144 printf("%s %s",par_ptr[t],par_name[t]);
145 if (t==npar)
146 print ")"
147 else
148 print ","
151 print "\n*/"
157 function read_type(pretoken ,token) {
158 token=pretoken;
159 print "token3 " token "-" yyval
161 rt_type="";
162 while ((token=="keyword" && KEYWORDS[yyval]<10) || token=="typedef")
164 rt_type=rt_type " " yyval;
165 if (KEYWORDS[yyval]==2)
167 token=yylex();
168 rt_type=rt_type " " yyval;
170 token=yylex();
171 print "token2 " token
173 rt_type=substr(rt_type,2);
174 print "type " rt_type
176 rt_ptr="";
177 while (token=="*" || token=="(")
179 rt_ptr=rt_ptr yyval;
180 if (token=="(")
181 plevel++;
182 token=yylex();
183 print "token1 " token
185 print "ptr " rt_ptr
187 return token;
190 function yyinit(str) {
191 yyrest=str;
194 function yylex() {
195 for (yyagain=1; yyagain; )
197 yyagain=0;
198 if (yyrest=="" || match(yyrest,/^[ \t\014\r]+/))
200 while (1)
202 while (yyrest=="")
204 if (getline yyrest != 1)
205 return "EOF";
206 #print "1 -" yyrest "-"
207 if (substr(yyrest,1,1) == "#")
208 yyrest="";
210 if (!match(yyrest,"^[ \t\014\r]+"))
211 break;
212 yyrest=substr(yyrest,RSTART+RLENGTH);
215 if (match(yyrest,/^\/\*/))
217 yyrest=substr(yyrest,3);
218 while (!match(yyrest,"\*\/"))
220 if (getline yyrest != 1)
221 return "EOF";
222 #print "2 -" yyrest "-"
224 yyrest=substr(yyrest,RSTART+RLENGTH);
225 yyagain=1;
228 if (match(yyrest,"^[a-zA-Z_][a-zA-Z0-9_]*"))
230 yyval=substr(yyrest,RSTART,RLENGTH);
231 yyrest=substr(yyrest,RSTART+RLENGTH);
233 if (yyval in TYPES)
234 return "typedef";
235 else if (yyval in KEYWORDS)
236 return "keyword";
237 else
238 return "ident";
240 if (match(yyrest,"^0[xX][0-9a-f]+[lL]?"))
242 yyval=substr(yyrest,RSTART,RLENGTH);
243 yyrest=substr(yyrest,RSTART+RLENGTH);
244 return "int_const";
246 if (match(yyrest,/^([0-9]*\.[0-9]+|[0-9]+\.[0-9]*)([eE][0-9]+)?[fF]?/))
248 yyval=substr(yyrest,RSTART,RLENGTH);
249 yyrest=substr(yyrest,RSTART+RLENGTH);
250 return "float_const";
252 if (match(yyrest,"^0[0-7]+[lL]?"))
254 yyval=substr(yyrest,RSTART,RLENGTH);
255 yyrest=substr(yyrest,RSTART+RLENGTH);
256 return "int_const";
258 if (match(yyrest,"^[1-9][0-9]*[lL]?"))
260 yyval=substr(yyrest,RSTART,RLENGTH);
261 yyrest=substr(yyrest,RSTART+RLENGTH);
262 return "int_const";
264 if (match(yyrest,"^\""))
266 if (match(yyrest,"^\"(\\\"|[^\"])*\""))
268 yyval=substr(yyrest,RSTART,RLENGTH);
269 yyrest=substr(yyrest,RSTART+RLENGTH);
270 return "str_const";
272 else
274 yyval=substr(yyrest,1,length(yyrest)-1);
275 while (1)
277 if (getline yyrest != 1)
278 return "EOF";
279 if (match(yyrest,"^(\\\"|[^\"])*\""))
280 break;
281 yyval=yyval substr(yyrest,1,length(yyrest)-1);
283 yyval=yyval substr(yyrest,RSTART,RLENGTH);
284 yyrest=substr(yyrest,RSTART+RLENGTH);
285 return "str_const";
288 if (substr(yyrest,1,3)=="...")
290 yyval="...";
291 yyrest=substr(yyrest,4);
292 return yyval;
294 yyval=substr(yyrest,1,1);
295 yyrest=substr(yyrest,2);
296 return yyval;