3 # code generator to help with writing Tcl extensions
4 # Copyright 2006 Ian Jackson
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; either version 2 of the
9 # License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this library; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 # Input format is line-based, ws-significant, offside rule (some kind
25 # Type TYPE: C-TYPE-DECLARATOR
26 # Defines TYPE as a type (for arguments and return values)
27 # which corresponds to the C type specified. C-TYPE-DECLARATOR
28 # must contain one `@' where the identifier would go.
29 # The type may contain allocated memory, etc., in which case
30 # `Init' and `Fini' must be used.
32 # TYPE may be either TYPENAME or TYPENAME(ARGS) - in this case,
33 # ARGS should be C argument declarations as for in a function
34 # prototype, of extra arguments for the application-supplied
35 # parser/returner functions. Each time a TYPE is used elsewhere,
36 # the ARGS should be the actual arguments to pass, and will be
37 # textually copied into the calls to the parser/returner
40 # `Type' causes declarations in the .h file of these functions:
41 # int cht_pat_TYPENAME(Tcl_Interp*, Tcl_Obj *obj, C-TYPE *val, ARGS);
42 # Tcl_Obj *cht_ret_TYPENAME(Tcl_Interp*, C-TYPE val, ARGS);
44 # cht_pat_... must attempt to parse obj into the appropriate type.
45 # val will already have been initialised with `Init' statements if
46 # relevant. Whether cht_pat_... fails or succeeds it may allocate
47 # memory into the object and must leave the object valid (for
50 # cht_ret_... must convert the value back to a new Tcl_Obj. It may
53 # Init TYPENAME C-STATEMENTS
54 # Provides some statements which are used to initialise a variable
55 # of type TYPENAME. C-STATEMENTS should contain one or more `@',
56 # which will be replaced by the actual variable name. The
57 # variable will have been declared with the C declarator specified
58 # with `Type'. C-STATEMENTS may not fail or longjmp, and they may
59 # not allocate memory or other resources. If no `Init' is
60 # supplied then there is no invariant (so no `Fini' may be
61 # supplied either, and the type is `flat' - no memory, external
64 # Fini TYPENAME C-STATEMENTS
65 # Provides some statements (like `Init') which are used to free a
66 # variable of type TYPENAME. The variable will already have been
67 # initialised with the `Init' statements, and may have been
68 # modified since by application per-type or per-command code. Its
69 # invariant will be satisfied before C-STATEMENTS. Afterwards the
70 # invariant may or may not be satisfied, but it may not have any
71 # memory or other resources allocated. C-STATEMENTS may not fail
74 # H-Include C-INCLUDE-SPECIFIER
75 # Arranges for generated .h files to #include the specified
76 # file. C-INCLUDE-SPECIFIER should include the <..> or "..".
78 # Table [*]TABLENAME C-ENTRY-TYPE
79 # Starts a table of commands or subcommands. The generated .h
80 # will contain a definition of C-ENTRY-TYPE containing
82 # Tcl_ObjCmdProc *func;
83 # and the generated .c will contain
84 # const C-ENTRY-TYPE C-ARRAY-NAME[];
85 # where C-ARRAY-NAME is TABLENAME, with `_entries' appended
86 # and `cht_' prepended. The entries are indented one level (one
87 # or more spaces) and look like this:
88 # ENTRYNAME [ C-EXTRA-ENTRY-VALUES ]
92 # This will cause the declaration of
93 # int cht_do_TABLENAME_ENTRYNAME(ClientData cd, Tcl_Interp *ip,
94 # FORMAL-ARGUMENTS, RESULT-C-TYPE*);
95 # which is the procedure which the application must supply to
96 # implement the function. If the `=> RESULT-TYPE' is omitted, so
97 # is the result argument to the function. Each argument to the
98 # function is of the C type corresponding to the specified type.
99 # TYPE may be `...', in which case the C function will be passed
100 # two args (int objc, Tcl_Obj *const *objv) for the remaining
103 # The cht_do_... function should not eat any memory associated with
104 # the arguments. The result buffer (if any) will be initialised
105 # using the `Init' and should on success contain the relevant
106 # result. On failure it should leave the result unmodified (or at
107 # least, not in need of freeing).
109 # As an alternative, the arguments can be replaced with just
110 # dispatch(TYPE-ARGS-FOR-ENUM)
111 # which is a shorthand for
112 # subcmd enum(TYPE-ARGS-FOR-ENUM)
114 # and also generates and uses a standard dispatch function.
116 # There will be an entry in C-ARRAY-NAME for every table entry.
117 # The name will be ENTRYNAME, and the func will be a function
118 # suitable for use as a Tcl command procedure, which parses the
119 # arguments, processes the command, and sets any result, as
122 # `*' should be used if the table name is not useful for error
123 # messages. It suppresses `TABLENAME ' from the front of the
124 # autogenerated argument parsing error strings.
126 # EntryExtra C-ENTRY-TYPE
127 # Introduces a section of additional C code which will be inserted
128 # into the definition of C-ENTRY-TYPE by `Table'. The C
129 # code, which follows on several indented lines, should be
130 # structure member definitions.
132 # When EntryExtra is used, in the corresponding Table, each
133 # ENTRYNAME should be followed on the same line by whitespace and
134 # EXTRA-VALUES; the EXTRA-VALUES are used as initialisers for the
135 # additional structure elements.
137 # NoEntryDefine C-ENTRY-TYPE
138 # Prevents the definition of C-ENTRY-TYPE by Table.
139 # The C type must be defined elsewhere.
141 # Also expected are these functions:
142 # void cht_setstringresult(Tcl_Interp*, const char*);
143 # sets the Tcl result from the supplied string
144 # int cht_pat_enum(Tcl_Interp*, Tcl_Obj*, const void **c_e_t_array,
145 # const void *c_e_t_return, size_t c_e_t_sz, const char *what);
146 # scans a table of C-ENTRY-TYPEs looking for the
147 # string matching the string supplied by the script
148 # (as a Tcl_Obj). On error sets the result, using
149 # what (a noun phrase describing the type of thing).
150 # Assumes (unportably!) that the name and func members
151 # are in the same places no matter what the rest of
152 # the struct contains.
153 # and the two predefined types `int' (C `int') and `obj' (Tcl_Obj*,
154 # unmodified.) The corresponding definitions are in tcmdiflib.c.
159 parse('builtins','DATA');
163 if (m/^\-p([-_0-9a-z]+)$/) {
166 } elsif (m/^\-w(c|h)$/) {
168 } elsif (m/^\-o(.+)$/) {
171 die "unknown option $_\n";
173 if (!defined $prefix) { $prefix= $_; $prefix =~ s/\.[^.]+$//; }
174 $x= new IO::File $_,'r' or die "$_: $!\n";
179 die "must say -w<something>\n" if !defined $write;
189 $a =~ m:/(.*),: or die "invalid enum type \`$a'\n";
190 my ($a_tab, $ee_type, $estr) = ($`,$1,$');
191 if ($ee_type !~ m/^[^_]/) {
192 $ee_type= $a_tab.$ee_type;
193 $a_tab= lc($a_tab).'_entries';
195 return ($a_tab, $ee_type, $estr);
200 while (defined($_= $f->getline)) {
204 while (s/\t/ ' 'x(8 - (length $`) % 8) /e) { }
207 $this_indent= length $&;
208 while (@i && $this_indent < $i[0]) {
211 if ($this_indent && (!@i || $this_indent > $i[0])) {
212 unshift @i, $this_indent;
215 if (@i==0 && m/^Table\s+(\*?)(\w+)\s+(\w+)$/) {
218 $table_x{$c_table}{T}= $1;
219 $table_x{$c_table}{C}= $3;
220 $entrytype_x{$3}= '' unless exists $entrytype_x{$3};
221 } elsif (@i==0 && m/^Untabled$/) {
224 } elsif (@i==0 && m/^(C|H)\-Include\s+(\S.*)$/) {
225 o(lc $1, 30, "#include $2\n");
226 } elsif (@i==0 && m/^EntryExtra\s+(\w+)$/) {
229 } elsif (@i==0 && m/^NoEntryDefine\s+(\w+)$/) {
231 $entrytype_x{$1}= " ";
232 } elsif (@i>=1 && defined $c_entryextra) {
233 $entrytype_x{$c_entryextra} .= " $_\n";
234 } elsif (@i==1 && m/^[a-z].*$/ && defined $c_table) {
235 if (m/^[-_0-9A-Za-z]+$/) {
237 } elsif (m/^([-_0-9A-Za-z]+)\s+(\S.*)$/) {
239 $tables{$c_table}{$c_entry}{I} .= ", $2";
241 badsyntax($wh,$.,"bad entry");
243 $tables{$c_table}{$c_entry}{A} = [ ];
244 } elsif (@i==2 && m/^\.\.\.\s+(\w+)$/ && defined $c_entry) {
245 $tables{$c_table}{$c_entry}{V}= $1;
246 } elsif (@i==2 && m:^dispatch\(((.*)/(.*)\,.*)\)$: && defined $c_entry) {
248 my $subcmdtype= $2.$3;
249 $tables{$c_table}{$c_entry}{D}= $subcmdtype;
250 $tables{$c_table}{$c_entry}{V}= 'obj';
251 push @{ $tables{$c_table}{$c_entry}{A} },
252 { N => 'subcmd', T => 'enum', A => $enumargs, O => '' };
253 } elsif (@i==2 && m/^(\??)([a-z]\w*)\s*(\S.*)/
254 && defined $c_entry) {
255 ($opt, $var, $type) = ($1,$2,$3);
256 ($type, $xtypeargs) = split_type_args($type);
257 push @{ $tables{$c_table}{$c_entry}{A} },
258 { N => $var, T => $type, A => $xtypeargs, O => ($opt eq '?') };
259 } elsif (@i==2 && m/^\=\>\s*(\S.*)$/ && defined $c_entry) {
260 ($type, $xtypeargs) = split_type_args($1);
261 $tables{$c_table}{$c_entry}{R}= $type;
262 $tables{$c_table}{$c_entry}{X}= $xtypeargs;
263 } elsif (@i==0 && m/^Type\s+([^\:]+)\:\s+(\S.*)$/) {
264 ($typename,$ctype)= ($1,$2);
265 $ctype .= ' @' unless $ctype =~ m/\@/;
266 ($typename,$xtypeargs) = split_type_args($typename);
267 $types{$typename}= { C => $ctype, X => $xtypeargs };
268 } elsif (@i==0 && s/^Init\s+(\w+)\s+(\S.*)//) {
270 } elsif (@i==0 && s/^Fini\s+(\w+)\s+(\S.*)//) {
273 badsyntax($wh,$., sprintf
274 "bad directive (indent level %d)", scalar @i);
277 $f->error and die $!;
281 #print Dumper(\%tables),"\n";
282 #print Dumper(\%types),"\n";
284 foreach $t (sort keys %types) {
288 $decl= "int cht_pat_$t(Tcl_Interp *ip, Tcl_Obj *obj, ";
289 $decl .= subst_in_decl('*val', $c, "type $t");
290 $decl .= ", $xta", if length $xta;
294 $decl= "Tcl_Obj *cht_ret_$t(Tcl_Interp *ip, ".subst_in_decl('val',$c);
295 $decl .= ", $xta" if length $xta;
300 foreach $c_entrytype (sort keys %entrytype_x) {
301 next if $entrytype_x{$c_entrytype} =~ m/^\s$/;
302 o('h', 20, "typedef struct $c_entrytype $c_entrytype;\n");
304 "struct $c_entrytype {\n".
305 " const char *name;\n".
306 " Tcl_ObjCmdProc *func;\n".
307 $entrytype_x{$c_entrytype}.
311 foreach $c_table (sort keys %tables) {
312 $r_table= $tables{$c_table};
313 $x_table= $table_x{$c_table};
316 foreach $c_entry (sort keys %$r_table) {
317 $c_entry_c= $c_entry; $c_entry_c =~ y/-/_/;
318 $r_entry= $r_table->{$c_entry};
319 $pa_decl= "int pa_${c_table}_${c_entry_c}(ClientData cd,".
320 " Tcl_Interp *ip, int objc, Tcl_Obj *const *objv)";
321 $pa_func= "cht_do_${c_table}_${c_entry_c}";
322 if (exists $r_entry->{D}) {
323 $pa_func= "cht_dispatch_$r_entry->{D}";
325 $do_decl= "int $pa_func(";
326 @do_al= ('ClientData cd', 'Tcl_Interp *ip');
329 $pa_argc= " objc--; objv++;\n";
330 $pa_vars= " int rc;\n";
340 $pa_hint .= "$c_table " if length $c_table &&
341 !length $table_x{$c_table}{T};
343 foreach $arg (@{ $r_entry->{A} }) {
347 push @do_al, make_decl($n, $t, $arg->{A},
348 "table $c_table entry $c_entry arg $n");
349 $pa_vars .= make_decl_init("a_$n", $t, $a, \$pa_init, "pa_vars");
356 $pa_body .= " if (!objc--) goto end_optional;\n";
360 $pa_body .= " if (!objc--) goto wrong_count_args;\n";
368 $pa_vars .= " const void *v_$n= 0;\n";
370 $pafin= "\n a_$n= v_$n; ";
371 ($a_tab, $ee_type, $estr) = enumargs($a);
372 $a = "cht_$a_tab, sizeof($ee_type), $estr";
373 o('h', 210, "extern const $ee_type cht_$a_tab".'[]'.";\n");
375 if (exists $type_fini{$t}) {
376 $pa_fini .= ' '.subst_in("a_$n", $type_fini{$t})."\n";
378 $pa_body .= " rc= cht_pat_$t(ip, *objv++, $paarg";
379 $pa_body .= ", ".$a if length $a;
380 $pa_body .= ");$pafin if (rc) goto rc_err;\n";
383 if (exists $r_entry->{V}) {
386 push @do_al, subst_in_decl("${va}c", 'int @');
387 push @do_al, subst_in_decl("${va}v", 'Tcl_Obj *const *@');
388 push @do_aa, "objc+1", "objv-1";
391 $pa_body .= " if (objc) goto wrong_count_args;\n";
396 $pa_body .= "end_optional:\n";
398 if (exists $r_entry->{R}) {
401 push @do_al, make_decl("*result", $t, "cht_do_al result");
402 $pa_vars .= make_decl_init("result", $t, $xta, \$pa_init,
404 push @do_aa, "&result";
405 $pa_rslt .= " Tcl_SetObjResult(ip, cht_ret_$t(ip, result";
406 $pa_rslt .= ", $xta" if length $xta;
410 $pa_body .= " rc= $pa_func(";
411 $pa_body .= join ', ', @do_aa;
413 $pa_body .= " if (rc) goto rc_err;\n";
415 $pa_rslt .= " rc= TCL_OK;\n\n";
416 $pa_rslt .= "rc_err:\n";
418 $pa_fini .= " return rc;\n";
420 $pa_fini .= "\nwrong_count_args:\n";
421 $pa_fini .= " e=\"wrong # args: should be \\\"$pa_hint\\\"\";\n";
422 $pa_fini .= " goto e_err;";
426 $pa_vars .= " const char *e;\n";
428 $pa_fini .= "e_err:\n";
429 $pa_fini .= " cht_setstringresult(ip,e);\n";
430 $pa_fini .= " rc= TCL_ERROR; goto rc_err;\n";
433 $pa_init .= "\n" if length $pa_init;
436 if (length $c_table) {
440 o('h',90, "$pa_decl;\n");
443 $static.$pa_decl." {\n".
451 $do_decl .= join ', ', @do_al;
454 if (exists $r_entry->{D}) {
455 my $subcmdtype= $r_entry->{D};
456 if (!exists $dispatch_done{$subcmdtype}) {
458 $di_body .= "static $do_decl {\n";
459 $di_body .= " return subcmd->func(0,ip,objc,objv);\n";
461 o('c',50, $di_body) or die $!;
464 o('h',100, $do_decl.";\n") or die $!;
467 $op_tab .= sprintf(" { %-20s %-40s%s },\n",
469 "pa_${c_table}_${c_entry_c}",
472 if (length $c_table) {
473 $decl= "const $x_table->{C} cht_${c_table}_entries[]";
474 o('h', 500, "extern $decl;\n");
483 o(c, 0, "#include \"$prefix.h\"\n");
486 "#ifndef INCLUDED_\U${prefix}_H\n".
487 "#define INCLUDED_\U${prefix}_H\n\n");
490 "#endif /*INCLUDED_\U${prefix}_H*/\n");
492 if (defined $output) {
493 $oh= new IO::File "$output.tmp", 'w' or die "$output.tmp: $!\n";
498 print $oh "/* AUTOGENERATED - DO NOT EDIT */\n" or die $!;
499 foreach $pr (sort keys %{ $o{$write} }) {
500 print $oh "\n" or die $!;
501 print $oh $o{$write}{$pr} or die $!;
505 die $! unless $oh->close;
507 if (defined $output) {
508 rename "$output.tmp", $output or die $!;
512 my ($wh,$pr,$s) = @_;
513 $o{$wh}{sprintf "%010d", $pr} .= $s;
516 sub split_type_args ($) {
519 if ($type =~ m/^\w+$/) {
521 } elsif ($type =~ m/^(\w+)\((.+)\)$/) {
525 badsyntax($wh,$.,"bad type name/args \`$type'\n");
527 return ($type,$xtypeargs);
530 sub make_decl_init ($$$$$) {
531 my ($n, $t, $a, $initcode, $why) = @_;
533 $o= make_decl($n,$t,$a,"$why _init");
534 if (exists $type_init{$t}) {
535 $init= $type_init{$t};
536 $$initcode .= " ".subst_in("$n", $init)."\n"
544 sub make_decl ($$$$) {
545 my ($n, $t, $ta, $why) = @_;
548 ($a_tab, $ee_type, $estr) = enumargs($ta);
549 $c= "const $ee_type* @";
551 defined $types{$t} or die "unknown type $t ($why)\n";
554 return subst_in_decl($n,$c);
557 sub subst_in_decl ($$$) {
558 my ($val, $pat, $why) = @_;
559 local ($_) = subst_in($val, $pat, $why);
565 my ($val, $pat, $why) = @_;
566 $pat =~ m/\@/ or die "$pat for $val in $why ?";
571 sub badsyntax ($$$) {
572 die "$_[0]:$_[1]: $_[2]\n";