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 ($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.*)//) {
94 } elsif (@i==0 && s/^Fini\s+(\w+)\s+(\S.*)//) {
97 badsyntax($wh,$., sprintf
98 "bad directive (indent level %d)", scalar @i);
101 $f->error and die $!;
105 #print Dumper(\%tables),"\n";
106 #print Dumper(\%types),"\n";
108 foreach $t (sort keys %types) {
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;
118 $decl= "Tcl_Obj *ret_$t(Tcl_Interp *ip, ".subst_in_decl('val',$c);
119 $decl .= ", $xta" if length $xta;
124 foreach $c_entrytype (sort keys %entrytype_x) {
125 o('h', 20, "typedef struct $c_entrytype $c_entrytype;\n");
127 "struct $c_entrytype {\n".
128 " const char *name;\n".
129 " Tcl_ObjCmdProc *func;\n".
130 $entrytype_x{$c_entrytype}.
134 foreach $c_table (sort keys %tables) {
135 $r_table= $tables{$c_table};
136 $x_table= $table_x{$c_table};
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');
148 $pa_argc= " objc--; objv++;\n";
149 $pa_vars= " int rc;\n";
159 $pa_hint .= "$c_table " if length $c_table;
161 foreach $arg (@{ $r_entry->{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");
174 $pa_body .= " if (!objc--) goto end_optional;\n";
178 $pa_body .= " if (!objc--) goto wrong_count_args;\n";
186 $pa_vars .= " const void *v_$n= 0;\n";
188 $pafin= "\n a_$n= v_$n; ";
189 $a =~ m/\,/ or die "invalid enum type \`$a'\n";
191 $a = "$a_tab, sizeof($`), $'";
192 o('h', 210, "extern const $` $a_tab".'[]'.";\n");
194 if (exists $type_fini{$t}) {
195 $pa_fini .= ' '.subst_in("a_$n", $type_fini{$t})."\n";
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";
202 if (exists $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";
210 $pa_body .= " if (objc) goto wrong_count_args;\n";
215 $pa_body .= "end_optional:\n";
217 if (exists $r_entry->{R}) {
220 push @do_al, make_decl("*result", $t, "do_al result");
221 $pa_vars .= make_decl_init("result", $t, $xta, \$pa_init,
223 push @do_aa, "&result";
224 $pa_rslt .= " Tcl_SetObjResult(ip, ret_$t(ip, result";
225 $pa_rslt .= ", $xta" if length $xta;
229 $pa_body .= " rc= do_${c_table}_${c_entry_c}(";
230 $pa_body .= join ', ', @do_aa;
232 $pa_body .= " if (rc) goto rc_err;\n";
234 $pa_rslt .= " rc= TCL_OK;\n\n";
235 $pa_rslt .= "rc_err:\n";
237 $pa_fini .= " return rc;\n";
239 $pa_fini .= "\nwrong_count_args:\n";
240 $pa_fini .= " e=\"wrong # args: should be \\\"$pa_hint\\\"\";\n";
241 $pa_fini .= " goto e_err;";
245 $pa_vars .= " const char *e;\n";
247 $pa_fini .= "e_err:\n";
248 $pa_fini .= " setstringresult(ip,e);\n";
249 $pa_fini .= " rc= TCL_ERROR; goto rc_err;\n";
252 $pa_init .= "\n" if length $pa_init;
255 if (length $c_table) {
259 o('h',90, "$pa_decl;\n");
262 $static.$pa_decl." {\n".
270 $do_decl .= join ', ', @do_al;
272 o('h',100, $do_decl.";\n") or die $!;
275 $op_tab .= sprintf(" { %-20s %-40s%s },\n",
277 "pa_${c_table}_${c_entry_c}",
280 if (length $c_table) {
281 $decl= "const $x_table->{C} ".lc($x_table->{C}).'s[]';
282 o('h', 500, "extern $decl;\n");
291 o(c, 0, "#include \"$prefix.h\"\n");
294 "#ifndef INCLUDED_\U${prefix}_H\n".
295 "#define INCLUDED_\U${prefix}_H\n\n".
296 "#include <tcl.h>\n");
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");
304 "#endif /*INCLUDED_\U${prefix}_H*/\n");
306 if (defined $output) {
307 $oh= new IO::File "$output.tmp", 'w' or die "$output.tmp: $!\n";
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 $!;
319 die $! unless $oh->close;
321 if (defined $output) {
322 rename "$output.tmp", $output or die $!;
326 my ($wh,$pr,$s) = @_;
327 $o{$wh}{sprintf "%010d", $pr} .= $s;
330 sub split_type_args ($) {
333 if ($type =~ m/^\w+$/) {
335 } elsif ($type =~ m/^(\w+)\((.+)\)$/) {
339 badsyntax($wh,$.,"bad type name/args \`$type'\n");
341 return ($type,$xtypeargs);
344 sub make_decl_init ($$$$$) {
345 my ($n, $t, $a, $initcode, $why) = @_;
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"
358 sub make_decl ($$$$) {
359 my ($n, $t, $ta, $why) = @_;
362 $ta =~ m/\,/ or die "invalid enum type \`$t' ($why)\n";
365 defined $types{$t} or die "unknown type $t ($why)\n";
368 return subst_in_decl($n,$c);
371 sub subst_in_decl ($$$) {
372 my ($val, $pat, $why) = @_;
373 local ($_) = subst_in($val, $pat, $why);
379 my ($val, $pat, $why) = @_;
380 $pat =~ m/\@/ or die "$pat for $val in $why ?";
385 sub badsyntax ($$$) {
386 die "$_[0]:$_[1]: $_[2]\n";
393 Type charfrom(const char *opts, const char *what): int