real dead key
[AROS.git] / scripts / cint2.awk
blob1a1881d54d866bb3754107277acbf1a3d00626b7
1 # Creates C files with function prototypes from FD and clib/*_proto.h files.
3 # Example usage:
5 # Required directory structure:
6 # amiga/include/fd/mylib_lib.fd
7 # amiga/include/clib/mylib_protos.h
9 # Usage: awk -f cint2.awk mylib
11 # Result:
12 # mylib/func1.c
13 # mylib/func2.c
14 # ...
16 BEGIN {
17 date="27-11-96";
19 RTYPES["long"]="LONG";
20 RTYPES["unsigned long"]="ULONG";
21 RTYPES["short"]="WORD";
22 RTYPES["unsigned short"]="UWORD";
23 RTYPES["char"]="BYTE";
24 RTYPES["unsigned char"]="UBYTE";
26 TYPES["ULONG"]=1;
27 TYPES["LONG"]=1;
28 TYPES["UBYTE"]=1;
29 TYPES["BYTE"]=1;
30 TYPES["UWORD"]=1;
31 TYPES["WORD"]=1;
32 TYPES["APTR"]=1;
33 TYPES["BPTR"]=1;
34 TYPES["STRPTR"]=1;
35 TYPES["BSTR"]=1;
36 TYPES["BOOL"]=1;
37 TYPES["Tag"]=1;
38 TYPES["FLOAT"]=1;
39 TYPES["DOUBLE"]=1;
40 TYPES[""]=1;
41 TYPES[""]=1;
43 # 1- types
44 # 2- commands
45 KEYWORDS["struct"]=2;
46 KEYWORDS["int"]=1;
47 KEYWORDS["char"]=1;
48 KEYWORDS["double"]=1;
49 KEYWORDS["float"]=1;
50 KEYWORDS["for"]=10;
51 KEYWORDS["if"]=10;
52 KEYWORDS["else"]=10;
53 KEYWORDS["do"]=10;
54 KEYWORDS["while"]=10;
55 KEYWORDS["switch"]=10;
56 KEYWORDS["case"]=10;
57 KEYWORDS["break"]=10;
58 KEYWORDS["continue"]=10;
59 KEYWORDS["goto"]=10;
60 KEYWORDS["union"]=2;
61 KEYWORDS["enum"]=2;
62 KEYWORDS["typedef"]=1;
63 KEYWORDS["void"]=1;
64 KEYWORDS["unsigned"]=3;
65 KEYWORDS["signed"]=3;
66 KEYWORDS["const"]=3;
67 KEYWORDS["long"]=3;
68 KEYWORDS["short"]=3;
69 KEYWORDS[""]=0;
70 KEYWORDS[""]=0;
72 SpecialStructs["exec"]="ExecBase";
73 SpecialStructs["graphics"]="GfxBase";
74 SpecialStructs["dos"]="DosLibrary";
75 SpecialStructs["intuition"]="IntuitionBase";
77 spaces=" ";
78 for (t=0; t<10; t++)
79 spaces=spaces spaces;
81 for (arg=1; arg<ARGC; arg++)
83 part_name=ARGV[arg];
84 Part_name=toupper(substr(part_name,1,1)) substr(part_name,2);
86 if (part_name in SpecialStructs)
87 struct_name=SpecialStructs[part_name];
88 else
89 struct_name="Library";
91 print "Working on " part_name "..."
93 dir="amiga/" part_name;
94 system("mkdir " dir);
95 print dir;
96 dir=dir"/";
97 INPUT="amiga/include/fd/" part_name "_lib.fd"
98 print INPUT
100 # Read LVO
101 delete LVO
102 while ((getline line < INPUT) > 0)
104 if (substr(line,1,6) == "##bias")
105 offset = int(substr(line,7))/6;
106 else if (substr(line,1,6) == "##base")
108 match(line,"[A-Za-z_][A-Za-z0-9_]*$");
109 Base = substr(line,RSTART+1,RLENGTH-1);
111 else if (!match (line,/[*#]/))
113 match(line,"[A-Za-z_][A-Za-z0-9_]*");
114 fname=substr(line,RSTART,RLENGTH);
115 line=substr(line,RSTART+RLENGTH);
116 if (match(line,"[(][^)]*[)]"))
118 if (RLENGTH!=2)
119 fargs=substr(line,RSTART+1,RLENGTH-2);
120 else
121 fargs="";
123 else
124 fargs="";
125 line=substr(line,RSTART+RLENGTH);
126 if (match(line,"[(][^)]*[)]"))
128 if (RLENGTH!=2)
129 fregs=substr(line,RSTART+1,RLENGTH-2);
130 else
131 fregs="";
133 else
134 fregs="";
135 #print fname " (" fargs ") (" fregs ") " offset
136 LVO[fname] = offset;
137 offset ++;
138 LVO_regs[fname] = toupper(fregs);
142 INPUT="amiga/include/clib/" part_name "_protos.h"
143 #print INPUT
144 getline line < INPUT
145 yyinit(line);
146 rt_type="";
148 #if (!match(FILENAME,"\/.*_"))
150 # print "Can't find libname"
151 # exit (10);
153 #dir=substr(FILENAME,RSTART+1,RLENGTH-2);
155 #if (dir!="")
157 # system("mkdir " dir);
158 # part_name=dir;
159 # dir=dir "/";
162 while ((token=yylex()) != "EOF")
164 #print "0 -" token "-" yyval "-"
165 if ((token=="keyword" && KEYWORDS[yyval]<10) || token=="typedef")
167 plevel=0;
168 token=read_type(token);
169 fname = yyval;
170 token=yylex();
172 if (token=="(")
174 plevel ++;
175 token=yylex();
177 if (rt_ptr!="")
178 ret=rt_type " " rt_ptr;
179 else
180 ret=rt_type;
182 if (token==")")
184 npar=1;
185 par_type[1]="void";
186 par_ptr[1]="";
187 par_name[1]="";
189 else
191 npar=0;
192 while (token!=")")
194 #print 1
195 if (token==",")
196 token=yylex();
197 token=read_type(token);
198 if (token=="ident")
200 name=yyval;
201 token=yylex();
203 else
204 name="";
206 # if (plevel != 1)
208 # plevel --;
209 # name=name ") "
210 # while((token==yylex())!=")" || plevel!=1)
212 #print 2
213 # if (token=="(")
214 # plevel ++;
215 # else if (token==")")
216 # plevel --;
217 # name=name " " yyval;
221 npar++;
222 par_type[npar]=rt_type;
223 par_ptr[npar]=rt_ptr;
224 par_name[npar]=name;
225 #print "npar " npar "-" rt_type "-" rt_ptr "-" name
229 print "Processing "fname"..."
230 #file="/dev/stdout"
231 file=dir tolower(fname) ".c"
233 if (fname in LVO)
235 offset=LVO[fname];
236 regs=LVO_regs[fname];
238 else
240 offset=-1;
241 regs="";
244 printf ("/*\n") > file;
245 print " Copyright © 2010, The AROS Development Team. All rights reserved." >> file
246 printf (" %sId$\n", "$") >> file;
247 printf ("\n") >> file;
248 printf (" Desc:\n") >> file;
249 printf (" Lang: english\n") >> file;
250 printf ("*/\n") >> file;
251 printf ("#include \"%s_intern.h\"\n\n", part_name) >> file;
252 print "/*****************************************************************************\n" >> file
253 print " NAME */" >>file
254 print "#include <proto/" part_name ".h>\n">>file
256 if (offset!=-1)
258 if (npar==1 && par_type[1]=="void" && par_ptr[1] == "" &&
259 par_name[1]=="")
260 print "\tAROS_LH0("ret", "fname",\n" >>file
261 else
262 print "\tAROS_LH"npar"("ret", "fname",\n" >>file
263 print "/* SYNOPSIS */" >>file
265 if (npar==1 && par_type[1]=="void" && par_ptr[1] == "" &&
266 par_name[1]=="")
268 print "\t/* void */" >>file
270 else
272 maxlen=0;
273 for (t=1; t<=npar; t++)
275 #print 3
276 len=length(par_type[t]);
277 len2=length(par_ptr[t]);
278 len+=len2 ? len2+1 : 0;
279 if (len > maxlen)
280 maxlen = len;
283 for (t=1; t<=npar; t++)
285 #print 4
286 len=length(par_type[t]);
287 len2=length(par_ptr[t]);
289 printf("\tAROS_LHA(%s", par_type[t])>>file;
290 printf("%s", substr(spaces,1,maxlen-len-len2))>>file;
291 printf("%s, %s, %s),\n",par_ptr[t],par_name[t],
292 substr(regs,1,2))>>file;
293 if (regs!="")
295 regs=substr(regs,4);
299 print "\n/* LOCATION */\n\tstruct "struct_name" *, "Base", "offset", "Part_name")">>file
301 else
303 print "\t"ret" "fname" (\n" >>file
304 print "/* SYNOPSIS */" >>file
306 if (npar==1 && par_type[1]=="void" && par_ptr[1] == "" &&
307 par_name[1]=="")
309 print "\tvoid)" >>file
311 else
313 maxlen=0;
314 for (t=1; t<=npar; t++)
316 #print 3
317 len=length(par_type[t]);
318 len2=length(par_ptr[t]);
319 len+=len2 ? len2+1 : 0;
320 if (len > maxlen)
321 maxlen = len;
324 for (t=1; t<=npar; t++)
326 #print 4
327 len=length(par_type[t]);
328 len2=length(par_ptr[t]);
330 printf("\t%s", par_type[t])>>file;
331 printf("%s", substr(spaces,1,maxlen-len-len2))>>file;
332 printf("%s %s",par_ptr[t],par_name[t])>>file;
333 if (t==npar)
334 printf(")\n")>>file;
335 else
336 printf(",\n")>>file;
340 print "\n/* FUNCTION\n\n INPUTS\n\n RESULT\n\n NOTES\n">>file
341 print " EXAMPLE\n\n BUGS\n\n SEE ALSO\n\n INTERNALS\n">>file
342 print " HISTORY\n\n">>file;
343 print "*****************************************************************************/" > file
344 print "{">>file;
345 print " AROS_LIBFUNC_INIT\n">>file;
346 print " extern void aros_print_not_implemented (char *);">>file;
347 print " aros_print_not_implemented (\"" fname "\");\n">>file;
348 print " AROS_LIBFUNC_EXIT">>file;
349 print "} /* " fname " */">>file;
350 fclose (file);
351 } # found "("
352 } # found keyword
353 } # while token != EOF
354 } # for all args
355 } # BEGIN
357 function read_type(pretoken ,token) {
358 token=pretoken;
359 #print "token3 " token "-" yyval
361 rt_type="";
362 while ((token=="keyword" && KEYWORDS[yyval]<10) || token=="typedef" ||
363 token=="...")
365 #print 5
366 rt_type=rt_type " " yyval;
367 if (KEYWORDS[yyval]==2)
369 token=yylex();
370 rt_type=rt_type " " yyval;
372 token=yylex();
373 #print "token2 " token
375 rt_type=substr(rt_type,2);
376 #print "type " rt_type
377 if (rt_type in RTYPES)
378 rt_type = RTYPES[rt_type];
380 rt_ptr="";
381 while (token=="*" || token=="(")
383 #print 6
384 rt_ptr=rt_ptr yyval;
385 if (token=="(")
386 plevel++;
387 token=yylex();
388 #print "token1 " token
390 #print "ptr " rt_ptr
392 return token;
395 function yyinit(str) {
396 yyrest=str;
399 function yylex() {
400 #print yylex
401 for (yyagain=1; yyagain; )
403 yyagain=0;
404 if (yyrest=="" || match(yyrest,/^[ \t\014\r]+/))
406 while (1)
408 while (yyrest=="")
410 if ((getline yyrest < INPUT) != 1)
411 return "EOF";
412 #print "1 -" yyrest "-"
413 if (substr(yyrest,1,1) == "#")
414 yyrest="";
416 if (!match(yyrest,"^[ \t\014\r]+"))
417 break;
418 yyrest=substr(yyrest,RSTART+RLENGTH);
421 if (match(yyrest,/^\/\*/))
423 yyrest=substr(yyrest,3);
424 while (!match(yyrest,"\*\/"))
426 if ((getline yyrest < INPUT) != 1)
427 return "EOF";
428 #print "2 -" yyrest "-"
430 yyrest=substr(yyrest,RSTART+RLENGTH);
431 yyagain=1;
434 if (match(yyrest,"^[a-zA-Z_][a-zA-Z0-9_]*"))
436 yyval=substr(yyrest,RSTART,RLENGTH);
437 yyrest=substr(yyrest,RSTART+RLENGTH);
439 if (yyval in TYPES)
440 return "typedef";
441 else if (yyval in KEYWORDS)
442 return "keyword";
443 else
444 return "ident";
446 if (match(yyrest,"^0[xX][0-9a-f]+[lL]?"))
448 yyval=substr(yyrest,RSTART,RLENGTH);
449 yyrest=substr(yyrest,RSTART+RLENGTH);
450 return "int_const";
452 if (match(yyrest,/^([0-9]*\.[0-9]+|[0-9]+\.[0-9]*)([eE][0-9]+)?[fF]?/))
454 yyval=substr(yyrest,RSTART,RLENGTH);
455 yyrest=substr(yyrest,RSTART+RLENGTH);
456 return "float_const";
458 if (match(yyrest,"^0[0-7]+[lL]?"))
460 yyval=substr(yyrest,RSTART,RLENGTH);
461 yyrest=substr(yyrest,RSTART+RLENGTH);
462 return "int_const";
464 if (match(yyrest,"^[1-9][0-9]*[lL]?"))
466 yyval=substr(yyrest,RSTART,RLENGTH);
467 yyrest=substr(yyrest,RSTART+RLENGTH);
468 return "int_const";
470 if (match(yyrest,"^\""))
472 if (match(yyrest,"^\"(\\\"|[^\"])*\""))
474 yyval=substr(yyrest,RSTART,RLENGTH);
475 yyrest=substr(yyrest,RSTART+RLENGTH);
476 return "str_const";
478 else
480 yyval=substr(yyrest,1,length(yyrest)-1);
481 while (1)
483 if ((getline yyrest < INPUT) != 1)
484 return "EOF";
485 if (match(yyrest,"^(\\\"|[^\"])*\""))
486 break;
487 yyval=yyval substr(yyrest,1,length(yyrest)-1);
489 yyval=yyval substr(yyrest,RSTART,RLENGTH);
490 yyrest=substr(yyrest,RSTART+RLENGTH);
491 return "str_const";
494 if (substr(yyrest,1,3)=="...")
496 yyval="...";
497 yyrest=substr(yyrest,4);
498 return yyval;
500 yyval=substr(yyrest,1,1);
501 yyrest=substr(yyrest,2);
502 return yyval;