1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
#!/bin/sh
# the next line restarts using the interpreter \
exec tclsh "$0" "$@"
# $Id: source.tcl,v 1.18 2004/04/29 23:22:52 jfontain Exp $
# This utility can be used to combine all sourced files making a Tcl
# program into a single file, so that the source command is no longer
# required for the program to run.
# This is most useful for creating tclets (Tcl applets) running within
# the Tcl plug-in, which, as a safe interpreter, does not allow files to
# be sourced by default (read the Tcl plugin faq for ways to allow
# sourcing files).
# type "source.tcl -h" for usage.
proc sourceCode {fileName} {
global sourced directories duplicates
if {!$duplicates && [info exists sourced($fileName)]} {
return {} ;# do not process the same file more than once
}
set sourced($fileName) {}
set code {}
if {[string length $fileName] == 0} {
set file stdin
} else {
if {[string match /* $fileName]} {
set file [open $fileName]
} else {
foreach directory $directories { ;# search multiple directories
if {![catch {set file [open $directory/$fileName]}]} break
}
}
if {![info exists file]} {
puts stderr "could not find file $fileName"
exit 1
}
}
# look for own RCS identification in file (use ascii code for $ so that RCS
# does not mess up regular expression when checking in this file
set strip\
[regexp -line {\044Id: [^\044]+ jfontain [^\044]+ \044} [read $file]]
seek $file 0
while {[gets $file line] >= 0} {
if {[string match {*\\} $line]} {
append continued [string trimright $line \\]
continue
}
if {[info exists continued]} {
append continued $line
set line $continued
unset continued
}
if {$strip} {
# since this is one of our own source files, it is safe to strip
# comments from it
if {[regexp {^[ ]*#} $line]} continue
# remove trailing comments or blanks on terminated lines
regsub {;#.*$} $line {} line
regsub {\s+$} $line {} line
}
if {[scan $line { source %s} name] > 0} {
append code [sourceCode [subst $name]]
continue
}
append code $line\n
}
if {[string length $fileName] > 0} {
close $file
}
return $code
}
if {[string match -h* [lindex $argv 0]]} {
puts "usage: $argv0 \[-o outputFile\] \[inputFile\]"
exit
}
set duplicates 0
set output stdout
# always start with current directory for files included through the source
# command facility
set directories .
foreach {option value} $argv {
switch -- $option {
-d {
set duplicates $value
}
-I {
lappend directories $value
}
-o {
set output [open $value w]
}
}
}
puts $output [sourceCode $option]
close $output
|