3 # Usage: make-c-ext.tcl source.tcl >jim-source.c
5 # Converts a Tcl source file into C source suitable
6 # for loading as a static extension.
10 if {![string match
*.tcl
$source]} {
11 error "Source $source is not a .tcl file"
14 # Read the Tcl source and convert to C
15 # Note that no lines are removed in order to preserve line numbering
18 while {[gets $f buf
] >= 0} {
19 # Remove comment lines
20 regsub {^
[ \t]*#.*$} $buf "" buf
21 # Escape quotes and backlashes
22 set buf
[string map
[list \\ \\\\ \" \\"] $buf]
23 lappend sourcelines \"$buf\\n\"
27 lappend lines {/* autogenerated - do not edit */}
28 lappend lines {#include <jim.h>}
29 set basename [file tail $source]
30 set pkgname [file rootname $basename]
32 lappend lines "int Jim_
${pkgname
}Init
(Jim_Interp
*interp)"
34 lappend lines "\tif
(Jim_PackageProvide
(interp, \"$pkgname\", \"1.0\", JIM_ERRMSG
)) return JIM_ERR
;"
35 lappend lines "\treturn Jim_EvalSource
(interp, \"$basename\", 1, [join $sourcelines \n]);"