chiark / gitweb /
tuntap seems to sort of work
[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/^\.\.\.\s+(\w+)$/ && 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             ($type, $xtypeargs) = split_type_args($type);
81             push @{ $tables{$c_table}{$c_entry}{A} },
82                 { N => $var, T => $type, A => $xtypeargs, O => ($opt eq '?') };
83         } elsif (@i==2 && m/^\=\>\s*(\S.*)$/ && defined $c_entry) {
84             ($type, $xtypeargs) = split_type_args($1);
85             $tables{$c_table}{$c_entry}{R}= $type;
86             $tables{$c_table}{$c_entry}{X}= $xtypeargs;
87         } elsif (@i==0 && m/^Type\s+([^\:]+)\:\s+(\S.*)$/) {
88             ($typename,$ctype)= ($1,$2);
89             $ctype .= ' @' unless $ctype =~ m/\@/;
90             ($typename,$xtypeargs) = split_type_args($typename);
91             $types{$typename}= { C => $ctype, X => $xtypeargs };
92         } elsif (@i==0 && s/^Init\s+(\w+)\s+(\S.*)//) {
93             $type_init{$1}= $2;
94         } elsif (@i==0 && s/^Fini\s+(\w+)\s+(\S.*)//) {
95             $type_fini{$1}= $2;
96         } else {
97             badsyntax($wh,$., sprintf
98                       "bad directive (indent level %d)", scalar @i);
99         }
100     }
101     $f->error and die $!;
102     $f->close;
103 }
104
105 #print Dumper(\%tables),"\n";
106 #print Dumper(\%types),"\n";
107
108 foreach $t (sort keys %types) {
109     $type= $types{$t};
110     $c= $type->{C};
111     $xta= $type->{X};
112     $decl= "int pat_$t(Tcl_Interp *ip, Tcl_Obj *obj, ";
113     $decl .= subst_in_decl('*val', $c, "type $t");
114     $decl .= ", $xta",  if length $xta;
115     $decl .= ");\n";
116     o('h',160, $decl);
117
118     $decl= "Tcl_Obj *ret_$t(Tcl_Interp *ip, ".subst_in_decl('val',$c);
119     $decl .= ", $xta" if length $xta;
120     $decl .= ");\n";
121     o('h',170, $decl);
122 }
123
124 foreach $c_entrytype (sort keys %entrytype_x) {
125     o('h', 20, "typedef struct $c_entrytype $c_entrytype;\n");
126     o('h', 100,
127       "struct $c_entrytype {\n".
128       "  const char *name;\n".
129       "  Tcl_ObjCmdProc *func;\n".
130       $entrytype_x{$c_entrytype}.
131       "};\n\n");
132 }
133
134 foreach $c_table (sort keys %tables) {
135     $r_table= $tables{$c_table};
136     $x_table= $table_x{$c_table};
137     $op_tab= '';
138
139     foreach $c_entry (sort keys %$r_table) {
140         $c_entry_c= $c_entry; $c_entry_c =~ y/-/_/;
141         $r_entry= $r_table->{$c_entry};
142         $pa_decl= "int pa_${c_table}_${c_entry_c}(ClientData cd,".
143             " Tcl_Interp *ip, int objc, Tcl_Obj *const *objv)";
144         $do_decl= "int do_${c_table}_${c_entry_c}(";
145         @do_al= ('ClientData cd', 'Tcl_Interp *ip');
146         @do_aa= qw(cd ip);
147         $pa_init= '';
148         $pa_argc= "  objc--; objv++;\n";
149         $pa_vars= "  int rc;\n";
150         $pa_body= '';
151         $pa_rslt= '';
152         $pa_free= '';
153         $pa_fini= '';
154         $any_mand= 0;
155         $any_optl= 0;
156         $any_eerr= 0;
157         $any_eargc= 0;
158         $pa_hint= '';
159         $pa_hint .= "$c_table " if length $c_table;
160         $pa_hint.= $c_entry;
161         foreach $arg (@{ $r_entry->{A} }) {
162             $n= $arg->{N};
163             $t= $arg->{T};
164             $a= $arg->{A};
165             push @do_al, make_decl($n, $t, $arg->{A},
166                                    "table $c_table entry $c_entry arg $n");
167             $pa_vars .= make_decl_init("a_$n", $t, $a, \$pa_init, "pa_vars");
168             if ($arg->{O}) {
169                 $pa_hint .= " ?$n?";
170                 if ($any_mand) {
171                     $any_mand= 0;
172                     $any_eerr= 1;
173                 }
174                 $pa_body .= "  if (!objc--) goto end_optional;\n";
175                 $any_optl= 1;
176             } else {
177                 $pa_hint .= " $n";
178                 $pa_body .= "  if (!objc--) goto wrong_count_args;\n";
179                 $any_mand++;
180                 $any_eargc= 1;
181                 die if $any_optl;
182             }
183             $paarg= "&a_$n";
184             $pafin= '';
185             if ($t eq 'enum') {
186                 $pa_vars .= "  const void *v_$n= 0;\n";
187                 $paarg= "&v_$n";
188                 $pafin= "\n  a_$n= v_$n; ";
189                 $a =~ m/\,/ or die "invalid enum type \`$a'\n";
190                 $a_tab = lc($`).'s';
191                 $a = "$a_tab, sizeof($`), $'";
192                 o('h', 210, "extern const $` $a_tab".'[]'.";\n");
193             }
194             if (exists $type_fini{$t}) {
195                 $pa_fini .= '  '.subst_in("a_$n", $type_fini{$t})."\n";
196             }
197             $pa_body .= "  rc= pat_$t(ip, *objv++, $paarg";
198             $pa_body .= ", ".$a if length $a;
199             $pa_body .= ");$pafin if (rc) goto rc_err;\n";
200             push @do_aa, "a_$n";
201         }
202         if (exists $r_entry->{V}) {
203             $pa_hint .= " ...";
204             $va= $r_entry->{V};
205             push @do_al, subst_in_decl("${va}c", 'int @');
206             push @do_al, subst_in_decl("${va}v", 'Tcl_Obj *const *@');
207             push @do_aa, "objc+1", "objv-1";
208         } else {
209             if (!$any_optl) {
210                 $pa_body .= "  if (objc) goto wrong_count_args;\n";
211                 $any_eargc= 1;
212             }
213         }
214         if ($any_optl) {
215             $pa_body .= "end_optional:\n";
216         }
217         if (exists $r_entry->{R}) {
218             $t= $r_entry->{R};
219             $xta= $r_entry->{X};
220             push @do_al, make_decl("*result", $t, "do_al result");
221             $pa_vars .= make_decl_init("result", $t, $xta, \$pa_init,
222                                        "pa_vars result");
223             push @do_aa, "&result";
224             $pa_rslt .= "  Tcl_SetObjResult(ip, ret_$t(ip, result";
225             $pa_rslt .= ", $xta" if length $xta;
226             $pa_rslt .= "));\n";
227         }
228         $pa_body .= "\n";
229         $pa_body .= "  rc= do_${c_table}_${c_entry_c}(";
230         $pa_body .= join ', ', @do_aa;
231         $pa_body .= ");\n";
232         $pa_body .= "  if (rc) goto rc_err;\n";
233
234         $pa_rslt .= "  rc= TCL_OK;\n\n";
235         $pa_rslt .= "rc_err:\n";
236         
237         $pa_fini .= "  return rc;\n";
238         if ($any_eargc) {
239             $pa_fini .= "\nwrong_count_args:\n";
240             $pa_fini .= "  e=\"wrong # args: should be \\\"$pa_hint\\\"\";\n";
241             $pa_fini .= "  goto e_err;";
242             $any_eerr= 1;
243         }
244         if ($any_eerr) {
245             $pa_vars .= "  const char *e;\n";
246             $pa_fini .= "\n";
247             $pa_fini .= "e_err:\n";
248             $pa_fini .= "  setstringresult(ip,e);\n";
249             $pa_fini .= "  rc= TCL_ERROR; goto rc_err;\n";
250         }
251         $pa_vars .= "\n";
252         $pa_init .= "\n" if length $pa_init;
253         $pa_fini .= "}\n\n";
254
255         if (length $c_table) {
256             $static= 'static ';
257         } else {
258             $static= '';
259             o('h',90, "$pa_decl;\n");
260         }
261         o('c',100,
262           $static.$pa_decl." {\n".
263           $pa_vars.
264           $pa_init.
265           $pa_argc.
266           $pa_body.
267           $pa_rslt.
268           $pa_free.
269           $pa_fini);
270         $do_decl .= join ', ', @do_al;
271         $do_decl .= ")";
272         o('h',100, $do_decl.";\n") or die $!;
273         
274
275         $op_tab .= sprintf("  { %-20s %-40s%s },\n",
276                            "\"$c_entry\",",
277                            "pa_${c_table}_${c_entry_c}",
278                            $r_entry->{I});
279     }
280     if (length $c_table) {
281         $decl= "const $x_table->{C} ".lc($x_table->{C}).'s[]';
282         o('h', 500, "extern $decl;\n");
283         o('c', 100,
284           "$decl = {\n".
285           $op_tab.
286           "  { 0 }\n".
287           "};\n\n");
288     }
289 }
290
291 o(c, 0, "#include \"$prefix.h\"\n");
292
293 o(h, 0,
294   "#ifndef INCLUDED_\U${prefix}_H\n".
295   "#define INCLUDED_\U${prefix}_H\n\n".
296   "#include <tcl.h>\n");
297
298 o(h, 400,
299   "void setstringresult(Tcl_Interp*, const char*);\n".
300   "int pat_enum(Tcl_Interp*, Tcl_Obj*, const void**,".
301   "             const void*, size_t, const char *what);\n");
302
303 o(h, 999,
304   "#endif /*INCLUDED_\U${prefix}_H*/\n");
305
306 if (defined $output) {
307     $oh= new IO::File "$output.tmp", 'w' or die "$output.tmp: $!\n";
308 } else {
309     $oh= 'STDOUT';
310 }
311
312 print $oh "/* AUTOGENERATED - DO NOT EDIT */\n" or die $!;
313 foreach $pr (sort keys %{ $o{$write} }) {
314     print $oh "\n" or die $!;
315     print $oh $o{$write}{$pr} or die $!;
316 }
317
318 die if $oh->error;
319 die $! unless $oh->close;
320
321 if (defined $output) {
322     rename "$output.tmp", $output or die $!;
323 }
324
325 sub o ($$) {
326     my ($wh,$pr,$s) = @_;
327     $o{$wh}{sprintf "%010d", $pr} .= $s;
328 }
329
330 sub split_type_args ($) {
331     my ($type) = @_;
332     my ($xtypeargs);
333     if ($type =~ m/^\w+$/) {
334         $xtypeargs='';
335     } elsif ($type =~ m/^(\w+)\((.+)\)$/) {
336         $type= $1;
337         $xtypeargs= $2;
338     } else {
339         badsyntax($wh,$.,"bad type name/args \`$type'\n");
340     }
341     return ($type,$xtypeargs);
342 }
343
344 sub make_decl_init ($$$$$) {
345     my ($n, $t, $a, $initcode, $why) = @_;
346     my ($o,$init);
347     $o= make_decl($n,$t,$a,"$why _init");
348     if (exists $type_init{$t}) {
349         $init= $type_init{$t};
350         $$initcode .= "  ".subst_in("$n", $init)."\n"
351             if length $init;
352     } else {
353         $o .= ' =0';
354     }
355     return "  ".$o.";\n";
356 }
357
358 sub make_decl ($$$$) {
359     my ($n, $t, $ta, $why) = @_;
360     my ($type);
361     if ($t eq 'enum') {
362         $ta =~ m/\,/ or die "invalid enum type \`$t' ($why)\n";
363         $c= "const $` *@";
364     } else { 
365         defined $types{$t} or die "unknown type $t ($why)\n";
366         $c= $types{$t}{C};
367     }
368     return subst_in_decl($n,$c);
369 }
370
371 sub subst_in_decl ($$$) {
372     my ($val, $pat, $why) = @_;
373     local ($_) = subst_in($val, $pat, $why);
374     s/ *(\**) *$/$1/;
375     return $_;
376 }
377     
378 sub subst_in ($$$) {
379     my ($val, $pat, $why) = @_;
380     $pat =~ m/\@/ or die "$pat for $val in $why ?";
381     $pat =~ s/\@/$val/g;
382     return $pat;
383 }
384
385 sub badsyntax ($$$) {
386     die "$_[0]:$_[1]: $_[2]\n";
387 }
388
389 __DATA__
390 Type int:       int
391 Type obj:       Tcl_Obj *@
392
393 Type charfrom(const char *opts, const char *what):      int