6 parse('builtins','DATA');
12 } elsif (m/^\-w(c|h)$/) {
14 } elsif (m/^\-o(.+)$/) {
17 die "unknown option $_\n";
19 if (!defined $prefix) { $prefix= $_; $prefix =~ s/\.[^.]+$//; }
20 $x= new IO::File $_,'r' or die "$_: $!\n";
25 die "must say -w<something>\n" if !defined $write;
35 while (defined($_= $f->getline)) {
39 while (s/\t/ ' 'x(8 - (length $`) % 8) /e) { }
42 $this_indent= length $&;
43 while (@i && $this_indent < $i[0]) {
46 if ($this_indent && (!@i || $this_indent > $i[0])) {
47 unshift @i, $this_indent;
50 if (@i==0 && m/^Table\s+(\w+)\s+(\w+)$/) {
53 $table_x{$c_table}{C}= $2;
55 } elsif (@i==0 && m/^Untabled$/) {
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+)$/) {
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]+$/) {
68 } elsif (m/^([-_0-9A-Za-z]+)\s+(\S.*)$/) {
70 $tables{$c_table}{$c_entry}{I} .= ", $2";
72 badsyntax($wh,$.,"bad entry");
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 if ($type =~ m/^\w+$/) {
82 } elsif ($type =~ m/^(\w+)\((.+)\)$/) {
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+$/) {
95 } elsif ($typename =~ m/^(\w+)\((.+)\)$/) {
99 badsyntax($wh,$.,"bad type name/args");
101 $types{$typename}= { C => $ctype, X => $xtypeargs };
102 } elsif (@i==0 && s/^Init\s+(\w+)\s+(\S.*)//) {
104 } elsif (@i==0 && s/^Fini\s+(\w+)\s+(\S.*)//) {
107 badsyntax($wh,$., sprintf
108 "bad directive (indent level %d)", scalar @i);
111 $f->error and die $!;
115 #print Dumper(\%tables),"\n";
116 #print Dumper(\%types),"\n";
118 foreach $t (sort keys %types) {
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;
128 $decl= "Tcl_Obj *ret_$t(Tcl_Interp *ip, ".subst_in_decl('val',$c).");\n";
132 foreach $c_entrytype (sort keys %entrytype_x) {
133 o('h', 20, "typedef struct $c_entrytype $c_entrytype;\n");
135 "struct $c_entrytype {\n".
136 " const char *name;\n".
137 " Tcl_ObjCmdProc *func;\n".
138 $entrytype_x{$c_entrytype}.
142 foreach $c_table (sort keys %tables) {
143 $r_table= $tables{$c_table};
144 $x_table= $table_x{$c_table};
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');
156 $pa_argc= " objc--; objv++;\n";
157 $pa_vars= " int rc;\n";
167 $pa_hint .= "$c_table " if length $c_table;
169 foreach $arg (@{ $r_entry->{A} }) {
173 push @do_al, make_decl($n, $t, $arg->{A});
174 $pa_vars .= make_decl_init("a_$n", $t, $a, \$pa_init);
181 $pa_body .= " if (!objc--) goto end_optional;\n";
185 $pa_body .= " if (!objc--) goto wrong_count_args;\n";
193 $pa_vars .= " const void *v_$n= 0;\n";
195 $pafin= "\n a_$n= v_$n; ";
196 $a =~ m/\,/ or die "invalid enum type \`$a'\n";
198 $a = "$a_tab, sizeof($`), $'";
199 o('h', 210, "extern const $` $a_tab".'[]'.";\n");
201 if (exists $type_fini{$t}) {
202 $pa_fini .= ' '.subst_in("a_$n", $type_fini{$t})."\n";
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";
209 if (exists $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";
217 $pa_body .= " if (objc) goto wrong_count_args;\n";
222 $pa_body .= "end_optional:\n";
224 if (exists $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";
232 $pa_body .= " rc= do_${c_table}_${c_entry_c}(";
233 $pa_body .= join ', ', @do_aa;
235 $pa_body .= " if (rc) goto rc_err;\n";
237 $pa_rslt .= " rc= TCL_OK;\n\n";
238 $pa_rslt .= "rc_err:\n";
240 $pa_fini .= " return rc;\n";
242 $pa_fini .= "\nwrong_count_args:\n";
243 $pa_fini .= " e=\"wrong # args: should be \\\"$pa_hint\\\"\";\n";
244 $pa_fini .= " goto e_err;";
248 $pa_vars .= " const char *e;\n";
250 $pa_fini .= "e_err:\n";
251 $pa_fini .= " setstringresult(ip,e);";
252 $pa_fini .= " rc= TCL_ERROR; goto rc_err;\n";
255 $pa_init .= "\n" if length $pa_init;
258 if (length $c_table) {
262 o('h',90, "$pa_decl;\n");
265 $static.$pa_decl." {\n".
273 $do_decl .= join ', ', @do_al;
275 o('h',100, $do_decl.";\n") or die $!;
278 $op_tab .= sprintf(" { %-20s %-40s%s },\n",
280 "pa_${c_table}_${c_entry_c}",
283 if (length $c_table) {
284 $decl= "const $x_table->{C} ".lc($x_table->{C}).'s[]';
285 o('h', 500, "extern $decl;\n");
294 o(c, 0, "#include \"$prefix.h\"\n");
297 "#ifndef INCLUDED_\U${prefix}_H\n".
298 "#define INCLUDED_\U${prefix}_H\n\n".
299 "#include <tcl.h>\n");
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");
307 "#endif /*INCLUDED_\U${prefix}_H*/\n");
309 if (defined $output) {
310 $oh= new IO::File "$output.tmp", 'w' or die "$output.tmp: $!\n";
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 $!;
322 die $! unless $oh->close;
324 if (defined $output) {
325 rename "$output.tmp", $output or die $!;
329 my ($wh,$pr,$s) = @_;
330 $o{$wh}{sprintf "%010d", $pr} .= $s;
333 sub make_decl_init ($$$$) {
334 my ($n, $t, $a, $initcode) = @_;
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"
347 sub make_decl ($$$) {
348 my ($n, $t, $ta) = @_;
351 $ta =~ m/\,/ or die "invalid enum type \`$t'\n";
354 defined $types{$t} or die "unknown type $t\n";
357 return subst_in_decl($n,$c);
360 sub subst_in_decl ($$$) {
361 my ($val, $pat, $why) = @_;
362 local ($_) = subst_in($val, $pat, $why);
368 my ($val, $pat, $why) = @_;
369 $pat =~ m/\@/ or die "$pat for $val in $why ?";
374 sub badsyntax ($$$) {
375 die "$_[0]:$_[1]: $_[2]\n";
382 Type charfrom(const char *opts, const char *what): int