chiark / gitweb /
dgram sock references fixed. can close
[chiark-tcl.git] / base / tcmdifgen
1 #!/usr/bin/perl
2
3 use IO;
4 use Data::Dumper;
5
6 parse('builtins','DATA');
7
8 while (@ARGV) {
9     $_= shift @ARGV;
10     if (m/^\-p(\w+)/) {
11         $prefix= $1;
12     } elsif (m/^\-w(c|h)$/) {
13         $write= $1;
14     } elsif (m/^\-o(.+)$/) {
15         $output= $1;
16     } elsif (m/^\-/) {
17         die "unknown option $_\n";
18     } else {
19         if (!defined $prefix) { $prefix= $_;  $prefix =~ s/\.[^.]+$//; }
20         $x= new IO::File $_,'r' or die "$_: $!\n";
21         parse($_,$x);
22     }
23 }
24
25 die "must say -w<something>\n" if !defined $write;
26
27 sub zilch () {
28     undef $c_table;
29     undef $c_entryextra;
30     undef $c_of;
31 }
32
33 sub parse ($$) {
34     my ($wh,$f) = @_;
35     while (defined($_= $f->getline)) {
36         chomp; s/\s+$//;
37         next if m/^\s*\#/;
38         next if !m/\S/;
39         while (s/\t/ ' 'x(8 - (length $`) % 8) /e) { }
40
41         s/^\s*//;
42         $this_indent= length $&;
43         while (@i && $this_indent < $i[0]) {
44             shift @i;
45         }
46         if ($this_indent && (!@i || $this_indent > $i[0])) {
47             unshift @i, $this_indent;
48         }
49
50         if (@i==0 && m/^Table\s+(\w+)\s+(\w+)$/) {
51             zilch();
52             $c_table= $1;
53             $table_x{$c_table}{C}= $2;
54             $entrytype_x{$2}= '';
55         } elsif (@i==0 && m/^Untabled$/) {
56             zilch();
57             $c_table= '';
58         } elsif (@i==0 && m/^(C|H)\-Include\s+(\S.*)$/) {
59             o(lc $1, 30, "#include $2\n");
60         } elsif (@i==0 && m/^EntryExtra\s+(\w+)$/) {
61             zilch();
62             $c_entryextra= $1;
63         } elsif (@i>=1 && defined $c_entryextra) {
64             $entrytype_x{$c_entryextra} .= "  $_\n";
65         } elsif (@i==1 && m/^[a-z].*$/ && defined $c_table) {
66             if (m/^[-_0-9A-Za-z]+$/) {
67                 $c_entry= $_;
68             } elsif (m/^([-_0-9A-Za-z]+)\s+(\S.*)$/) {
69                 $c_entry= $1;
70                 $tables{$c_table}{$c_entry}{I} .= ", $2";
71             } else {
72                 badsyntax($wh,$.,"bad entry");
73             }
74             $tables{$c_table}{$c_entry}{A} = [ ];
75         } elsif (@i==2 && m/^(\w+)\s+\.\.\.$/ && defined $c_entry) {
76             $tables{$c_table}{$c_entry}{V}= $1;
77         } elsif (@i==2 && m/^(\??)([a-z]\w*)\s*(\S.*)/
78                  && defined $c_entry) {
79             ($opt, $var, $type) = ($1,$2,$3);
80             if ($type =~ m/^\w+$/) {
81                 $xtypeargs='';
82             } elsif ($type =~ m/^(\w+)\((.+)\)$/) {
83                 $type= $1;
84                 $xtypeargs= $2;
85             }
86             push @{ $tables{$c_table}{$c_entry}{A} },
87                 { N => $var, T => $type, A => $xtypeargs, O => ($opt eq '?') };
88         } elsif (@i==2 && m/^\=\>\s*(\S.*)$/ && defined $c_entry) {
89             $tables{$c_table}{$c_entry}{R}= $1;
90         } elsif (@i==0 && m/^Type\s+([^\:]+)\:\s+(\S.*)$/) {
91             ($typename,$ctype)= ($1,$2);
92             $ctype .= ' @' unless $ctype =~ m/\@/;
93             if ($typename =~ m/^\w+$/) {
94                 $xtypeargs='';
95             } elsif ($typename =~ m/^(\w+)\((.+)\)$/) {
96                 $typename=$1;
97                 $xtypeargs=$2;
98             } else {
99                 badsyntax($wh,$.,"bad type name/args");
100             }
101             $types{$typename}= { C => $ctype, X => $xtypeargs };
102         } elsif (@i==0 && s/^Init\s+(\w+)\s+(\S.*)//) {
103             $type_init{$1}= $2;
104         } elsif (@i==0 && s/^Fini\s+(\w+)\s+(\S.*)//) {
105             $type_fini{$1}= $2;
106         } else {
107             badsyntax($wh,$., sprintf
108                       "bad directive (indent level %d)", scalar @i);
109         }
110     }
111     $f->error and die $!;
112     $f->close;
113 }
114
115 #print Dumper(\%tables),"\n";
116 #print Dumper(\%types),"\n";
117
118 foreach $t (sort keys %types) {
119     $type= $types{$t};
120     $c= $type->{C};
121     $xta= $type->{X};
122     $decl= "int pat_$t(Tcl_Interp *ip, Tcl_Obj *obj, ";
123     $decl .= subst_in_decl('*val', $c, "type $t");
124     $decl .= ", $xta",  if length $xta;
125     $decl .= ");\n";
126     o('h',160, $decl);
127
128     $decl= "Tcl_Obj *ret_$t(Tcl_Interp *ip, ".subst_in_decl('val',$c).");\n";
129     o('h',170, $decl);
130 }
131
132 foreach $c_entrytype (sort keys %entrytype_x) {
133     o('h', 20, "typedef struct $c_entrytype $c_entrytype;\n");
134     o('h', 100,
135       "struct $c_entrytype {\n".
136       "  const char *name;\n".
137       "  Tcl_ObjCmdProc *func;\n".
138       $entrytype_x{$c_entrytype}.
139       "};\n\n");
140 }
141
142 foreach $c_table (sort keys %tables) {
143     $r_table= $tables{$c_table};
144     $x_table= $table_x{$c_table};
145     $op_tab= '';
146
147     foreach $c_entry (sort keys %$r_table) {
148         $c_entry_c= $c_entry; $c_entry_c =~ y/-/_/;
149         $r_entry= $r_table->{$c_entry};
150         $pa_decl= "int pa_${c_table}_${c_entry_c}(ClientData cd,".
151             " Tcl_Interp *ip, int objc, Tcl_Obj *const *objv)";
152         $do_decl= "int do_${c_table}_${c_entry_c}(";
153         @do_al= ('ClientData cd', 'Tcl_Interp *ip');
154         @do_aa= qw(cd ip);
155         $pa_init= '';
156         $pa_argc= "  objc--; objv++;\n";
157         $pa_vars= "  int rc;\n";
158         $pa_body= '';
159         $pa_rslt= '';
160         $pa_free= '';
161         $pa_fini= '';
162         $any_mand= 0;
163         $any_optl= 0;
164         $any_eerr= 0;
165         $any_eargc= 0;
166         $pa_hint= '';
167         $pa_hint .= "$c_table " if length $c_table;
168         $pa_hint.= $c_entry;
169         foreach $arg (@{ $r_entry->{A} }) {
170             $n= $arg->{N};
171             $t= $arg->{T};
172             $a= $arg->{A};
173             push @do_al, make_decl($n, $t, $arg->{A});
174             $pa_vars .= make_decl_init("a_$n", $t, $a, \$pa_init);
175             if ($arg->{O}) {
176                 $pa_hint .= " ?$n?";
177                 if ($any_mand) {
178                     $any_mand= 0;
179                     $any_eerr= 1;
180                 }
181                 $pa_body .= "  if (!objc--) goto end_optional;\n";
182                 $any_optl= 1;
183             } else {
184                 $pa_hint .= " $n";
185                 $pa_body .= "  if (!objc--) goto wrong_count_args;\n";
186                 $any_mand++;
187                 $any_eargc= 1;
188                 die if $any_optl;
189             }
190             $paarg= "&a_$n";
191             $pafin= '';
192             if ($t eq 'enum') {
193                 $pa_vars .= "  const void *v_$n= 0;\n";
194                 $paarg= "&v_$n";
195                 $pafin= "\n  a_$n= v_$n; ";
196                 $a =~ m/\,/ or die "invalid enum type \`$a'\n";
197                 $a_tab = lc($`).'s';
198                 $a = "$a_tab, sizeof($`), $'";
199                 o('h', 210, "extern const $` $a_tab".'[]'.";\n");
200             }
201             if (exists $type_fini{$t}) {
202                 $pa_fini .= '  '.subst_in("a_$n", $type_fini{$t})."\n";
203             }
204             $pa_body .= "  rc= pat_$t(ip, *objv++, $paarg";
205             $pa_body .= ", ".$a if length $a;
206             $pa_body .= ");$pafin if (rc) goto rc_err;\n";
207             push @do_aa, "a_$n";
208         }
209         if (exists $r_entry->{V}) {
210             $pa_hint .= " ...";
211             $va= $r_entry->{V};
212             push @do_al, subst_in_decl("${va}c", 'int @');
213             push @do_al, subst_in_decl("${va}v", 'Tcl_Obj *const *@');
214             push @do_aa, "objc+1", "objv-1";
215         } else {
216             if (!$any_optl) {
217                 $pa_body .= "  if (objc) goto wrong_count_args;\n";
218                 $any_eargc= 1;
219             }
220         }
221         if ($any_optl) {
222             $pa_body .= "end_optional:\n";
223         }
224         if (exists $r_entry->{R}) {
225             $t= $r_entry->{R};
226             push @do_al, make_decl("*result", $t);
227             $pa_vars .= make_decl_init("result", $t, '', \$pa_init);
228             push @do_aa, "&result";
229             $pa_rslt .= "  Tcl_SetObjResult(ip, ret_$t(ip, result));\n";
230         }
231         $pa_body .= "\n";
232         $pa_body .= "  rc= do_${c_table}_${c_entry_c}(";
233         $pa_body .= join ', ', @do_aa;
234         $pa_body .= ");\n";
235         $pa_body .= "  if (rc) goto rc_err;\n";
236
237         $pa_rslt .= "  rc= TCL_OK;\n\n";
238         $pa_rslt .= "rc_err:\n";
239         
240         $pa_fini .= "  return rc;\n";
241         if ($any_eargc) {
242             $pa_fini .= "\nwrong_count_args:\n";
243             $pa_fini .= "  e=\"wrong # args: should be \\\"$pa_hint\\\"\";\n";
244             $pa_fini .= "  goto e_err;";
245             $any_eerr= 1;
246         }
247         if ($any_eerr) {
248             $pa_vars .= "  const char *e;\n";
249             $pa_fini .= "\n";
250             $pa_fini .= "e_err:\n";
251             $pa_fini .= "  setstringresult(ip,e);";
252             $pa_fini .= "  rc= TCL_ERROR; goto rc_err;\n";
253         }
254         $pa_vars .= "\n";
255         $pa_init .= "\n" if length $pa_init;
256         $pa_fini .= "}\n\n";
257
258         if (length $c_table) {
259             $static= 'static ';
260         } else {
261             $static= '';
262             o('h',90, "$pa_decl;\n");
263         }
264         o('c',100,
265           $static.$pa_decl." {\n".
266           $pa_vars.
267           $pa_init.
268           $pa_argc.
269           $pa_body.
270           $pa_rslt.
271           $pa_free.
272           $pa_fini);
273         $do_decl .= join ', ', @do_al;
274         $do_decl .= ")";
275         o('h',100, $do_decl.";\n") or die $!;
276         
277
278         $op_tab .= sprintf("  { %-20s %-40s%s },\n",
279                            "\"$c_entry\",",
280                            "pa_${c_table}_${c_entry_c}",
281                            $r_entry->{I});
282     }
283     if (length $c_table) {
284         $decl= "const $x_table->{C} ".lc($x_table->{C}).'s[]';
285         o('h', 500, "extern $decl;\n");
286         o('c', 100,
287           "$decl = {\n".
288           $op_tab.
289           "  { 0 }\n".
290           "};\n\n");
291     }
292 }
293
294 o(c, 0, "#include \"$prefix.h\"\n");
295
296 o(h, 0,
297   "#ifndef INCLUDED_\U${prefix}_H\n".
298   "#define INCLUDED_\U${prefix}_H\n\n".
299   "#include <tcl.h>\n");
300
301 o(h, 400,
302   "void setstringresult(Tcl_Interp*, const char*);\n".
303   "int pat_enum(Tcl_Interp*, Tcl_Obj*, const void**,".
304   "             const void*, size_t, const char *what);\n");
305
306 o(h, 999,
307   "#endif /*INCLUDED_\U${prefix}_H*/\n");
308
309 if (defined $output) {
310     $oh= new IO::File "$output.tmp", 'w' or die "$output.tmp: $!\n";
311 } else {
312     $oh= 'STDOUT';
313 }
314
315 print $oh "/* AUTOGENERATED - DO NOT EDIT */\n" or die $!;
316 foreach $pr (sort keys %{ $o{$write} }) {
317     print $oh "\n" or die $!;
318     print $oh $o{$write}{$pr} or die $!;
319 }
320
321 die if $oh->error;
322 die $! unless $oh->close;
323
324 if (defined $output) {
325     rename "$output.tmp", $output or die $!;
326 }
327
328 sub o ($$) {
329     my ($wh,$pr,$s) = @_;
330     $o{$wh}{sprintf "%010d", $pr} .= $s;
331 }
332
333 sub make_decl_init ($$$$) {
334     my ($n, $t, $a, $initcode) = @_;
335     my ($o,$init);
336     $o= make_decl($n,$t,$a);
337     if (exists $type_init{$t}) {
338         $init= $type_init{$t};
339         $$initcode .= "  ".subst_in("$n", $init)."\n"
340             if length $init;
341     } else {
342         $o .= ' =0';
343     }
344     return "  ".$o.";\n";
345 }
346
347 sub make_decl ($$$) {
348     my ($n, $t, $ta) = @_;
349     my ($type);
350     if ($t eq 'enum') {
351         $ta =~ m/\,/ or die "invalid enum type \`$t'\n";
352         $c= "const $` *@";
353     } else { 
354         defined $types{$t} or die "unknown type $t\n";
355         $c= $types{$t}{C};
356     }
357     return subst_in_decl($n,$c);
358 }
359
360 sub subst_in_decl ($$$) {
361     my ($val, $pat, $why) = @_;
362     local ($_) = subst_in($val, $pat, $why);
363     s/ *(\**) *$/$1/;
364     return $_;
365 }
366     
367 sub subst_in ($$$) {
368     my ($val, $pat, $why) = @_;
369     $pat =~ m/\@/ or die "$pat for $val in $why ?";
370     $pat =~ s/\@/$val/g;
371     return $pat;
372 }
373
374 sub badsyntax ($$$) {
375     die "$_[0]:$_[1]: $_[2]\n";
376 }
377
378 __DATA__
379 Type int:       int
380 Type obj:       Tcl_Obj *@
381
382 Type charfrom(const char *opts, const char *what):      int