3 # Input format is line-based, ws-significant, offside rule (some kind
6 # Type TYPE: C-TYPE-DECLARATOR
7 # Defines TYPE as a type (for arguments and return values)
8 # which corresponds to the C type specified. C-TYPE-DECLARATOR
9 # must contain one `@' where the identifier would go.
10 # The type may contain allocated memory, etc., in which case
11 # `Init' and `Fini' must be used.
13 # TYPE may be either TYPENAME or TYPENAME(ARGS) - in this case,
14 # ARGS should be C argument declarations as for in a function
15 # prototype, of extra arguments for the application-supplied
16 # parser/returner functions. Each time a TYPE is used elsewhere,
17 # the ARGS should be the actual arguments to pass, and will be
18 # textually copied into the calls to the parser/returner
21 # `Type' causes declarations in the .h file of these functions:
22 # int cht_pat_TYPENAME(Tcl_Interp*, Tcl_Obj *obj, C-TYPE *val, ARGS);
23 # Tcl_Obj *cht_ret_TYPENAME(Tcl_Interp*, C-TYPE val, ARGS);
25 # cht_pat_... must attempt to parse obj into the appropriate type.
26 # val will already have been initialised with `Init' statements if
27 # relevant. Whether cht_pat_... fails or succeeds it may allocate
28 # memory into the object and must leave the object valid (for
31 # cht_ret_... must convert the value back to a new Tcl_Obj. It may
34 # Init TYPENAME C-STATEMENTS
35 # Provides some statements which are used to initialise a variable
36 # of type TYPENAME. C-STATEMENTS should contain one or more `@',
37 # which will be replaced by the actual variable name. The
38 # variable will have been declared with the C declarator specified
39 # with `Type'. C-STATEMENTS may not fail or longjmp, and they may
40 # not allocate memory or other resources. If no `Init' is
41 # supplied then there is no invariant (so no `Fini' may be
42 # supplied either, and the type is `flat' - no memory, external
45 # Fini TYPENAME C-STATEMENTS
46 # Provides some statements (like `Init') which are used to free a
47 # variable of type TYPENAME. The variable will already have been
48 # initialised with the `Init' statements, and may have been
49 # modified since by application per-type or per-command code. Its
50 # invariant will be satisfied before C-STATEMENTS. Afterwards the
51 # invariant may or may not be satisfied, but it may not have any
52 # memory or other resources allocated. C-STATEMENTS may not fail
55 # H-Include C-INCLUDE-SPECIFIER
56 # Arranges for generated .h files to #include the specified
57 # file. C-INCLUDE-SPECIFIER should include the <..> or "..".
59 # Table TABLENAME C-ENTRY-TYPE
60 # Starts a table of commands or subcommands. The generated .h
61 # will contain a definition of C-ENTRY-TYPE containing
63 # Tcl_ObjCmdProc *func;
64 # and the generated .c will contain
65 # const C-ENTRY-TYPE C-ARRAY-NAME[];
66 # where C-ARRAY-NAME is C-ENTRY-TYPE lowercased, with
67 # `s' appended. The entries are indented one level (one
68 # or more spaces) and look like this:
73 # This will cause the declaration of
74 # int cht_do_TABLENAME_ENTRYNAME(ClientData cd, Tcl_Interp *ip,
75 # FORMAL-ARGUMENTS, RESULT-C-TYPE*);
76 # which is the procedure which the application must supply to
77 # implement the function. If the `=> RESULT-TYPE' is omitted, so
78 # is the result argument to the function. Each argument to the
79 # function is of the C type corresponding to the specified type.
80 # The cht_do_... function should not eat any memory associated with
81 # the arguments. The result buffer (if any) will be initialised
82 # using the `Init' and should on success contain the relevant
83 # result. On failure it should leave the result unmodified (or at
84 # least, not in need of freeing).
86 # There will be an entry in C-ARRAY-NAME for every table entry.
87 # The name will be ENTRYNAME, and the func will be a function
88 # suitable for use as a Tcl command procedure, which parses the
89 # arguments, processes the command, and sets any result, as
92 # ExtraEntry C-ENTRY-TYPE
93 # Introduces a section of additional C code which will be inserted
94 # into the definition of C-ENTRY-TYPE by `Table'. The C
95 # code, which follows on several indented lines, should be
96 # structure member definitions.
98 # When ExtraEntry is used, in the corresponding Table, each
99 # ENTRYNAME should be followed on the same line by whitespace and
100 # EXTRA-VALUES; the EXTRA-VALUES are used as initialisers for the
101 # additional structure elements.
103 # NoEntryDefine C-ENTRY-TYPE
104 # Prevents the definition of C-ENTRY-TYPE by Table.
105 # The C type must be defined elsewhere.
107 # Also expected are these functions:
108 # void cht_setstringresult(Tcl_Interp*, const char*);
109 # sets the Tcl result from the supplied string
110 # int cht_pat_enum(Tcl_Interp*, Tcl_Obj*, const void **c_e_t_array,
111 # const void *c_e_t_return, size_t c_e_t_sz, const char *what);
112 # scans a table of C-ENTRY-TYPEs looking for the
113 # string matching the string supplied by the script
114 # (as a Tcl_Obj). On error sets the result, using
115 # what (a noun phrase describing the type of thing).
116 # Assumes (unportably!) that the name and func members
117 # are in the same places no matter what the rest of
118 # the struct contains.
119 # and the two predefined types `int' (C `int') and `obj' (Tcl_Obj*,
120 # unmodified.) The corresponding definitions are in tcmdiflib.c.
125 parse('builtins','DATA');
129 if (m/^\-p([-_0-9a-z]+)$/) {
132 } elsif (m/^\-w(c|h)$/) {
134 } elsif (m/^\-o(.+)$/) {
137 die "unknown option $_\n";
139 if (!defined $prefix) { $prefix= $_; $prefix =~ s/\.[^.]+$//; }
140 $x= new IO::File $_,'r' or die "$_: $!\n";
145 die "must say -w<something>\n" if !defined $write;
155 while (defined($_= $f->getline)) {
159 while (s/\t/ ' 'x(8 - (length $`) % 8) /e) { }
162 $this_indent= length $&;
163 while (@i && $this_indent < $i[0]) {
166 if ($this_indent && (!@i || $this_indent > $i[0])) {
167 unshift @i, $this_indent;
170 if (@i==0 && m/^Table\s+(\*toplevel\*|\w+)\s+(\w+)$/) {
173 $table_x{$c_table}{C}= $2;
174 $entrytype_x{$2}= '' unless exists $entrytype_x{$2};
175 } elsif (@i==0 && m/^Untabled$/) {
178 } elsif (@i==0 && m/^(C|H)\-Include\s+(\S.*)$/) {
179 o(lc $1, 30, "#include $2\n");
180 } elsif (@i==0 && m/^EntryExtra\s+(\w+)$/) {
183 } elsif (@i==0 && m/^NoEntryDefine\s+(\w+)$/) {
185 $entrytype_x{$1}= " ";
186 } elsif (@i>=1 && defined $c_entryextra) {
187 $entrytype_x{$c_entryextra} .= " $_\n";
188 } elsif (@i==1 && m/^[a-z].*$/ && defined $c_table) {
189 if (m/^[-_0-9A-Za-z]+$/) {
191 } elsif (m/^([-_0-9A-Za-z]+)\s+(\S.*)$/) {
193 $tables{$c_table}{$c_entry}{I} .= ", $2";
195 badsyntax($wh,$.,"bad entry");
197 $tables{$c_table}{$c_entry}{A} = [ ];
198 } elsif (@i==2 && m/^\.\.\.\s+(\w+)$/ && defined $c_entry) {
199 $tables{$c_table}{$c_entry}{V}= $1;
200 } elsif (@i==2 && m/^(\??)([a-z]\w*)\s*(\S.*)/
201 && defined $c_entry) {
202 ($opt, $var, $type) = ($1,$2,$3);
203 ($type, $xtypeargs) = split_type_args($type);
204 push @{ $tables{$c_table}{$c_entry}{A} },
205 { N => $var, T => $type, A => $xtypeargs, O => ($opt eq '?') };
206 } elsif (@i==2 && m/^\=\>\s*(\S.*)$/ && defined $c_entry) {
207 ($type, $xtypeargs) = split_type_args($1);
208 $tables{$c_table}{$c_entry}{R}= $type;
209 $tables{$c_table}{$c_entry}{X}= $xtypeargs;
210 } elsif (@i==0 && m/^Type\s+([^\:]+)\:\s+(\S.*)$/) {
211 ($typename,$ctype)= ($1,$2);
212 $ctype .= ' @' unless $ctype =~ m/\@/;
213 ($typename,$xtypeargs) = split_type_args($typename);
214 $types{$typename}= { C => $ctype, X => $xtypeargs };
215 } elsif (@i==0 && s/^Init\s+(\w+)\s+(\S.*)//) {
217 } elsif (@i==0 && s/^Fini\s+(\w+)\s+(\S.*)//) {
220 badsyntax($wh,$., sprintf
221 "bad directive (indent level %d)", scalar @i);
224 $f->error and die $!;
228 #print Dumper(\%tables),"\n";
229 #print Dumper(\%types),"\n";
231 foreach $t (sort keys %types) {
235 $decl= "int cht_pat_$t(Tcl_Interp *ip, Tcl_Obj *obj, ";
236 $decl .= subst_in_decl('*val', $c, "type $t");
237 $decl .= ", $xta", if length $xta;
241 $decl= "Tcl_Obj *cht_ret_$t(Tcl_Interp *ip, ".subst_in_decl('val',$c);
242 $decl .= ", $xta" if length $xta;
247 foreach $c_entrytype (sort keys %entrytype_x) {
248 next if $entrytype_x{$c_entrytype} =~ m/^\s$/;
249 o('h', 20, "typedef struct $c_entrytype $c_entrytype;\n");
251 "struct $c_entrytype {\n".
252 " const char *name;\n".
253 " Tcl_ObjCmdProc *func;\n".
254 $entrytype_x{$c_entrytype}.
258 foreach $c_table (sort keys %tables) {
259 $r_table= $tables{$c_table};
260 $x_table= $table_x{$c_table};
263 foreach $c_entry (sort keys %$r_table) {
264 $c_entry_c= $c_entry; $c_entry_c =~ y/-/_/;
265 $r_entry= $r_table->{$c_entry};
266 $pa_decl= "int pa_${c_table}_${c_entry_c}(ClientData cd,".
267 " Tcl_Interp *ip, int objc, Tcl_Obj *const *objv)";
268 $do_decl= "int cht_do_${c_table}_${c_entry_c}(";
269 @do_al= ('ClientData cd', 'Tcl_Interp *ip');
272 $pa_argc= " objc--; objv++;\n";
273 $pa_vars= " int rc;\n";
283 $pa_hint .= "$c_table " if length $c_table;
285 foreach $arg (@{ $r_entry->{A} }) {
289 push @do_al, make_decl($n, $t, $arg->{A},
290 "table $c_table entry $c_entry arg $n");
291 $pa_vars .= make_decl_init("a_$n", $t, $a, \$pa_init, "pa_vars");
298 $pa_body .= " if (!objc--) goto end_optional;\n";
302 $pa_body .= " if (!objc--) goto wrong_count_args;\n";
310 $pa_vars .= " const void *v_$n= 0;\n";
312 $pafin= "\n a_$n= v_$n; ";
313 $a =~ m/\,/ or die "invalid enum type \`$a'\n";
315 $a = "$a_tab, sizeof($`), $'";
316 o('h', 210, "extern const $` $a_tab".'[]'.";\n");
318 if (exists $type_fini{$t}) {
319 $pa_fini .= ' '.subst_in("a_$n", $type_fini{$t})."\n";
321 $pa_body .= " rc= cht_pat_$t(ip, *objv++, $paarg";
322 $pa_body .= ", ".$a if length $a;
323 $pa_body .= ");$pafin if (rc) goto rc_err;\n";
326 if (exists $r_entry->{V}) {
329 push @do_al, subst_in_decl("${va}c", 'int @');
330 push @do_al, subst_in_decl("${va}v", 'Tcl_Obj *const *@');
331 push @do_aa, "objc+1", "objv-1";
334 $pa_body .= " if (objc) goto wrong_count_args;\n";
339 $pa_body .= "end_optional:\n";
341 if (exists $r_entry->{R}) {
344 push @do_al, make_decl("*result", $t, "cht_do_al result");
345 $pa_vars .= make_decl_init("result", $t, $xta, \$pa_init,
347 push @do_aa, "&result";
348 $pa_rslt .= " Tcl_SetObjResult(ip, cht_ret_$t(ip, result";
349 $pa_rslt .= ", $xta" if length $xta;
353 $pa_body .= " rc= cht_do_${c_table}_${c_entry_c}(";
354 $pa_body .= join ', ', @do_aa;
356 $pa_body .= " if (rc) goto rc_err;\n";
358 $pa_rslt .= " rc= TCL_OK;\n\n";
359 $pa_rslt .= "rc_err:\n";
361 $pa_fini .= " return rc;\n";
363 $pa_fini .= "\nwrong_count_args:\n";
364 $pa_fini .= " e=\"wrong # args: should be \\\"$pa_hint\\\"\";\n";
365 $pa_fini .= " goto e_err;";
369 $pa_vars .= " const char *e;\n";
371 $pa_fini .= "e_err:\n";
372 $pa_fini .= " setstringresult(ip,e);\n";
373 $pa_fini .= " rc= TCL_ERROR; goto rc_err;\n";
376 $pa_init .= "\n" if length $pa_init;
379 if (length $c_table) {
383 o('h',90, "$pa_decl;\n");
386 $static.$pa_decl." {\n".
394 $do_decl .= join ', ', @do_al;
396 o('h',100, $do_decl.";\n") or die $!;
399 $op_tab .= sprintf(" { %-20s %-40s%s },\n",
401 "pa_${c_table}_${c_entry_c}",
404 if (length $c_table) {
405 $decl= "const $x_table->{C} ".lc($x_table->{C}).'s[]';
406 o('h', 500, "extern $decl;\n");
415 o(c, 0, "#include \"$prefix.h\"\n");
418 "#ifndef INCLUDED_\U${prefix}_H\n".
419 "#define INCLUDED_\U${prefix}_H\n\n".
420 "#include <tcl8.3/tcl.h>\n");
423 "#endif /*INCLUDED_\U${prefix}_H*/\n");
425 if (defined $output) {
426 $oh= new IO::File "$output.tmp", 'w' or die "$output.tmp: $!\n";
431 print $oh "/* AUTOGENERATED - DO NOT EDIT */\n" or die $!;
432 foreach $pr (sort keys %{ $o{$write} }) {
433 print $oh "\n" or die $!;
434 print $oh $o{$write}{$pr} or die $!;
438 die $! unless $oh->close;
440 if (defined $output) {
441 rename "$output.tmp", $output or die $!;
445 my ($wh,$pr,$s) = @_;
446 $o{$wh}{sprintf "%010d", $pr} .= $s;
449 sub split_type_args ($) {
452 if ($type =~ m/^\w+$/) {
454 } elsif ($type =~ m/^(\w+)\((.+)\)$/) {
458 badsyntax($wh,$.,"bad type name/args \`$type'\n");
460 return ($type,$xtypeargs);
463 sub make_decl_init ($$$$$) {
464 my ($n, $t, $a, $initcode, $why) = @_;
466 $o= make_decl($n,$t,$a,"$why _init");
467 if (exists $type_init{$t}) {
468 $init= $type_init{$t};
469 $$initcode .= " ".subst_in("$n", $init)."\n"
477 sub make_decl ($$$$) {
478 my ($n, $t, $ta, $why) = @_;
481 $ta =~ m/\,/ or die "invalid enum type \`$t' ($why)\n";
484 defined $types{$t} or die "unknown type $t ($why)\n";
487 return subst_in_decl($n,$c);
490 sub subst_in_decl ($$$) {
491 my ($val, $pat, $why) = @_;
492 local ($_) = subst_in($val, $pat, $why);
498 my ($val, $pat, $why) = @_;
499 $pat =~ m/\@/ or die "$pat for $val in $why ?";
504 sub badsyntax ($$$) {
505 die "$_[0]:$_[1]: $_[2]\n";