blob: 61d00f1df269c353c674dbab0a4c29ef6b19b1b6 [file] [log] [blame]
drhdd2a43a2017-12-14 19:24:00 +00001# Try to open the executable as a database and read the "scripts.data"
2# field where "scripts.name" is 'main.tcl'
3#
4catch {
drh6b9986e2018-01-06 13:33:21 +00005 if {![file exists $argv0] && [file exists $argv0.exe]} {
6 append argv0 .exe
7 }
drhdd2a43a2017-12-14 19:24:00 +00008 sqlite3 db $argv0 -vfs apndvfs -create 0
9 set mainscript [db one {SELECT data FROM scripts WHERE name='main.tcl'}]
10}
11if {[info exists mainscript]} {
12 eval $mainscript
13 return
14} else {
15 catch {db close}
16}
17
18# Try to open file named in the first argument as a database and
19# read the "scripts.data" field where "scripts.name" is 'main.tcl'
20#
21if {[llength $argv]>0 && [file readable [lindex $argv 0]]} {
22 catch {
23 sqlite3 db [lindex $argv 0] -vfs apndvfs -create 0
24 set mainscript [db one {SELECT data FROM scripts WHERE name='main.tcl'}]
25 set argv0 [lindex $argv 0]
26 set argv [lrange $argv 1 end]
27 }
28 if {[info exists mainscript]} {
29 eval $mainscript
30 return
31 } else {
32 catch {db close}
33 }
34 if {[string match *.tcl [lindex $argv 0]]} {
35 set fd [open [lindex $argv 0] rb]
36 set mainscript [read $fd]
37 close $fd
38 unset fd
39 set argv0 [lindex $argv 0]
40 set argv [lrange $argv 1 end]
41 }
42 if {[info exists mainscript]} {
43 eval $mainscript
44 return
45 }
46}
47
48# If all else fails, do an interactive loop
49#
50set line {}
51while {![eof stdin]} {
52 if {$line!=""} {
53 puts -nonewline "> "
54 } else {
55 puts -nonewline "% "
56 }
57 flush stdout
58 append line [gets stdin]
59 if {[info complete $line]} {
60 if {[catch {uplevel #0 $line} result]} {
61 puts stderr "Error: $result"
62 } elseif {$result!=""} {
63 puts $result
64 }
65 set line {}
66 } else {
67 append line \\n"
68 }
69}