File: source.tcl

package info (click to toggle)
moodss 14.0-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,536 kB
  • ctags: 1,003
  • sloc: tcl: 25,371; ansic: 132; perl: 72; python: 64; sh: 63; makefile: 50
file content (84 lines) | stat: -rwxr-xr-x 2,629 bytes parent folder | download
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
#!/bin/sh
# the next line restarts using the interpreter \
exec tclsh "$0" "$@"

set rcsId {$Id: source.tcl,v 1.11 2001/01/25 20:56:18 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

    if {[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
        }
    }
    set skip 0
    while {[gets $file line]>=0} {
        set continues [string match {*\\} $line]
        if {$skip||[regexp {^[ 	]*#} $line]} {                                        ;# skip comments and eventually following line
            set skip $continues
            continue
        }
        if {!$continues} {
            regsub {[ 	]*;#.*$} $line {} line                  ;# eventually remove trailing comments or blanks on terminated lines
        }
        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 output stdout
set directories .                      ;# always start with current directory for files included through the source command facility

foreach {option value} $argv {
    switch -- $option {
        -I {
            lappend directories $value
        }
        -o {
            set output [open $value w]
        }
    }
}

puts $output [sourceCode $option]
close $output