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/^(\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+$/) {
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";
165 foreach $arg (@{ $r_entry->{A} }) {
169 push @do_al, make_decl($n, $t, $arg->{A});
170 $pa_vars .= make_decl_init("a_$n", $t, $a, \$pa_init);
173 $pa_argc .= " if (objc < $any_mand) {".
174 " e=\"too few args\"; goto e_err; }\n";
175 $pa_body .= " objc -= $any_mand;\n";
179 $pa_body .= " if (!objc--) goto end_optional;\n";
188 $pa_vars .= " const void *v_$n= 0;\n";
190 $pafin= "\n a_$n= v_$n; ";
191 $a =~ m/\,/ or die "invalid enum type \`$a'\n";
193 $a = "$a_tab, sizeof($`), $'";
194 o('h', 210, "extern const $` $a_tab".'[]'.";\n");
196 if (exists $type_fini{$t}) {
197 $pa_fini .= ' '.subst_in("a_$n", $type_fini{$t})."\n";
199 $pa_body .= " rc= pat_$t(ip, *objv++, $paarg";
200 $pa_body .= ", ".$a if length $a;
201 $pa_body .= ");$pafin if (rc) goto rc_err;\n";
204 if (exists $r_entry->{V}) {
206 $pa_body .= " objc -= $any_mand;\n";
209 push @do_al, subst_in_decl("${va}c", 'int @');
210 push @do_al, subst_in_decl("${va}v", 'Tcl_Obj *const *@');
211 push @do_aa, "objc+1", "objv-1";
214 $pa_argc .= " if (objc != $any_mand) {".
215 " e=\"wrong number of args\"; goto e_err; }\n";
220 $pa_body .= "end_optional:\n";
222 if (exists $r_entry->{R}) {
224 push @do_al, make_decl("*result", $t);
225 $pa_vars .= make_decl_init("result", $t, '', \$pa_init);
226 push @do_aa, "&result";
227 $pa_rslt .= " Tcl_SetObjResult(ip, ret_$t(ip, result));\n";
230 $pa_body .= " rc= do_${c_table}_${c_entry_c}(";
231 $pa_body .= join ', ', @do_aa;
233 $pa_body .= " if (rc) goto rc_err;\n";
235 $pa_rslt .= " rc= TCL_OK;\n\n";
236 $pa_rslt .= "rc_err:\n";
238 $pa_fini .= " return rc;\n";
240 $pa_vars .= " const char *e;\n";
242 $pa_fini .= "e_err:\n";
243 $pa_fini .= " setstringresult(ip,e);";
244 $pa_fini .= " rc= TCL_ERROR; goto rc_err;\n";
247 $pa_init .= "\n" if length $pa_init;
250 if (length $c_table) {
254 o('h',90, "$pa_decl;\n");
257 $static.$pa_decl." {\n".
265 $do_decl .= join ', ', @do_al;
267 o('h',100, $do_decl.";\n") or die $!;
270 $op_tab .= sprintf(" { %-20s %-40s%s },\n",
272 "pa_${c_table}_${c_entry_c}",
275 if (length $c_table) {
276 $decl= "const $x_table->{C} ".lc($x_table->{C}).'s[]';
277 o('h', 500, "extern $decl;\n");
286 o(c, 0, "#include \"$prefix.h\"\n");
289 "#ifndef INCLUDED_\U${prefix}_H\n".
290 "#define INCLUDED_\U${prefix}_H\n\n".
291 "#include <tcl.h>\n");
294 "void setstringresult(Tcl_Interp*, const char*);\n".
295 "int pat_enum(Tcl_Interp*, Tcl_Obj*, const void**,".
296 " const void*, size_t, const char *what);\n");
299 "#endif /*INCLUDED_\U${prefix}_H*/\n");
301 if (defined $output) {
302 $oh= new IO::File "$output.tmp", 'w' or die "$output.tmp: $!\n";
307 print $oh "/* AUTOGENERATED - DO NOT EDIT */\n" or die $!;
308 foreach $pr (sort keys %{ $o{$write} }) {
309 print $oh "\n" or die $!;
310 print $oh $o{$write}{$pr} or die $!;
314 die $! unless $oh->close;
316 if (defined $output) {
317 rename "$output.tmp", $output or die $!;
321 my ($wh,$pr,$s) = @_;
322 $o{$wh}{sprintf "%010d", $pr} .= $s;
325 sub make_decl_init ($$$$) {
326 my ($n, $t, $a, $initcode) = @_;
328 $o= make_decl($n,$t,$a);
329 if (exists $type_init{$t}) {
330 $init= $type_init{$t};
331 $$initcode .= " ".subst_in("$n", $init)."\n"
339 sub make_decl ($$$) {
340 my ($n, $t, $ta) = @_;
343 $ta =~ m/\,/ or die "invalid enum type \`$t'\n";
346 defined $types{$t} or die "unknown type $t\n";
349 return subst_in_decl($n,$c);
352 sub subst_in_decl ($$$) {
353 my ($val, $pat, $why) = @_;
354 local ($_) = subst_in($val, $pat, $why);
360 my ($val, $pat, $why) = @_;
361 $pat =~ m/\@/ or die "$pat for $val in $why ?";
366 sub badsyntax ($$$) {
367 die "$_[0]:$_[1]: $_[2]\n";
374 Type charfrom(const char *opts, const char *what): int