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;
29 while (defined($_= $f->getline)) {
33 s/\t/ ' 'x(8-(length $`) % 8) /eg;
36 $this_indent= length $&;
37 while (@i && $this_indent < $i[0]) {
40 if ($this_indent && (!@i || $this_indent > $i[0])) {
41 unshift @i, $this_indent;
44 if (@i==0 && m/^Table\s+(\w+)$/) {
47 } elsif (@i==0 && m/^(C|H)\-Include\s+(\S.*)$/) {
48 o(lc $1, 30, "#include $2\n");
49 } elsif (@i==1 && m/^([a-z]\w*)$/ && defined $c_table) {
51 $tables{$c_table}{$c_entry}{A} = [ ];
52 } elsif (@i==2 && m/^(\w+)\s+\.\.\.$/ && defined $c_entry) {
53 $tables{$c_table}{$c_entry}{V}= $1;
54 } elsif (@i==2 && m/^(\??)([a-z]\w*)\s*(\S.*)/
55 && defined $c_entry) {
56 ($opt, $var, $type) = ($1,$2,$3);
57 if ($type =~ m/^\w+$/) {
59 } elsif ($type =~ m/^(\w+)\((.+)\)$/) {
63 push @{ $tables{$c_table}{$c_entry}{A} },
64 { N => $var, T => $type, A => $xtypeargs, O => ($opt eq '?') };
65 } elsif (@i==2 && m/^\=\>\s*(\S.*)$/ && defined $c_entry) {
66 $tables{$c_table}{$c_entry}{R}= $1;
67 } elsif (@i==0 && m/^Type\s+([^\:]+)\:\s+(\S.*)$/) {
68 ($typename,$ctype)= ($1,$2);
69 $ctype .= ' @' unless $ctype =~ m/\@/;
70 if ($typename =~ m/^\w+$/) {
72 } elsif ($typename =~ m/^(\w+)\((.+)\)$/) {
76 badsyntax($wh,$.,"bad type name/args");
78 $types{$typename}= { C => $ctype, X => $xtypeargs };
79 } elsif (@i==0 && s/^Init\s+(\w+)\s+(\S.*)//) {
82 badsyntax($wh,$., sprintf
83 "bad directive (indent level %d)", scalar @i);
90 #print Dumper(\%tables),"\n";
91 #print Dumper(\%types),"\n";
93 foreach $t (sort keys %types) {
97 $decl= "int pat_$t(Tcl_Interp, Tcl_Obj*, ";
98 $decl .= subst_in('*', $c, "type $t");
99 $decl .= ", $xta", if length $xta;
103 $decl= "Tcl_Obj *ret_$t(Tcl_Interp, ".subst_in('',$c).");\n";
107 foreach $c_table (sort keys %tables) {
108 $r_table= $tables{$c_table};
109 foreach $c_entry (keys %$r_table) {
110 $r_entry= $r_table->{$c_entry};
111 $pa_decl= "int pa_${c_table}_${c_entry}(ClientData cd,".
112 " Tcl_Interp ip, int objc, Tcl_Obj *const *objv)";
113 $do_decl= "int do_${c_table}_${c_entry}(";
117 $pa_argc= " objc--; objv++;\n";
118 $pa_vars= " int rc;\n";
126 foreach $arg (@{ $r_entry->{A} }) {
130 push @do_al, make_decl($n, $t, $arg->{A});
131 $pa_vars .= make_decl_init("a_$n", $t, $a, \$pa_init);
134 $pa_argc .= " if (objc < $any_mand) {".
135 " e=\"too few args\"; goto e_err; }\n";
136 $pa_body .= " objc -= $any_mand;\n";
140 $pa_body .= " if (!objc--) goto end_optional;\n";
145 $pa_body .= " rc= pat_$t(ip, *objv++, &a_$n";
147 $a =~ m/\,/ or die; $a = "$', sizeof($`)";
149 $pa_body .= ", ".$a if length $a;
150 $pa_body .= "); if (rc) goto rc_err;\n";
153 if (exists $r_entry->{V}) {
155 push @do_al, subst_in("${va}c", 'int @');
156 push @do_al, subst_in("${va}v", 'Tcl_Obj *const *@');
157 push @do_aa, "objc-1", "objv+1";
159 $pa_body .= " if (--objc) { e=\"too many args\"; goto e_err; }\n";
163 $pa_body .= "end_optional:\n";
165 if (exists $r_entry->{R}) {
167 push @do_al, make_decl("*result", $t);
168 $pa_vars .= make_decl_init("result", $t, '', \$pa_init);
169 push @do_aa, "&result";
170 $pa_rslt .= " Tcl_SetObjResult(ip, ret_$t(ip, result));\n";
173 $pa_body .= " rc= do_${c_table}_${c_entry}(";
174 $pa_body .= join ', ', @do_aa;
176 $pa_body .= " if (rc) goto rc_err;\n";
178 $pa_rslt .= " rc= TCL_OK;\n\n";
179 $pa_rslt .= "rc_err:\n";
181 $pa_fini .= " return rc;\n";
183 $pa_vars .= " const char *e;\n";
185 $pa_fini .= "e_err:";
186 $pa_fini .= " setstringresult(ip,e);";
187 $pa_fini .= " rc= TCL_ERROR; goto rc_err;\n";
190 $pa_init .= "\n" if length $pa_init;
193 "static ".$pa_decl." {\n".
201 $do_decl .= join ', ', @do_al;
203 o('h',100, $do_decl.";\n") or die $!;
207 o(c, 0, "#include \"$prefix.h\"\n");
210 "#ifndef INCLUDED_\U${prefix}_H\n".
211 "#define INCLUDED_\U${prefix}_H\n\n".
212 "#include <tcl.h>\n");
215 "#endif /*INCLUDED_\U${prefix}_H*/\n");
217 if (defined $output) {
218 $oh= new IO::File "$output.tmp", 'w' or die "$output.tmp: $!\n";
223 print $oh "/* AUTOGENERATED - DO NOT EDIT */\n" or die $!;
224 foreach $pr (sort keys %{ $o{$write} }) {
225 print $oh "\n" or die $!;
226 print $oh $o{$write}{$pr} or die $!;
230 die $! unless $oh->close;
232 if (defined $output) {
233 rename "$output.tmp", $output or die $!;
237 my ($wh,$pr,$s) = @_;
238 $o{$wh}{sprintf "%010d", $pr} .= $s;
241 sub make_decl_init ($$$$) {
242 my ($n, $t, $a, $initcode) = @_;
244 $o= make_decl($n,$t,$a);
245 if (exists $type_init{$t}) {
246 $init= $type_init{$t};
247 $$initcode .= " ".subst_in("$n", $init)."\n"
255 sub make_decl ($$$) {
256 my ($n, $t, $ta) = @_;
259 $ta =~ m/\,/ or die "enum with bad args \`$ta'\n";
262 defined $types{$t} or die "unknown type $t\n";
265 return subst_in($n,$c);
269 my ($val, $pat, $why) = @_;
270 $pat =~ m/\@/ or die "$pat for $val in $why ?";
275 sub badsyntax ($$$) {
276 die "$_[0]:$_[1]: $_[2]\n";
282 Type charfrom(const char*): int