sqltclsh.tcl 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # Try to open the executable as a database and read the "scripts.data"
  2. # field where "scripts.name" is 'main.tcl'
  3. #
  4. catch {
  5. if {![file exists $argv0] && [file exists $argv0.exe]} {
  6. append argv0 .exe
  7. }
  8. sqlite3 db $argv0 -vfs apndvfs -create 0
  9. set mainscript [db one {
  10. SELECT sqlar_uncompress(data,sz) FROM sqlar WHERE name='main.tcl'
  11. }]
  12. }
  13. if {[info exists mainscript]} {
  14. eval $mainscript
  15. return
  16. } else {
  17. catch {db close}
  18. }
  19. # Try to open file named in the first argument as a database and
  20. # read the "scripts.data" field where "scripts.name" is 'main.tcl'
  21. #
  22. if {[llength $argv]>0 && [file readable [lindex $argv 0]]} {
  23. catch {
  24. sqlite3 db [lindex $argv 0] -vfs apndvfs -create 0
  25. set mainscript [db one {SELECT data FROM scripts WHERE name='main.tcl'}]
  26. set argv0 [lindex $argv 0]
  27. set argv [lrange $argv 1 end]
  28. }
  29. if {[info exists mainscript]} {
  30. eval $mainscript
  31. return
  32. } else {
  33. catch {db close}
  34. }
  35. if {[string match *.tcl [lindex $argv 0]]} {
  36. set fd [open [lindex $argv 0] rb]
  37. set mainscript [read $fd]
  38. close $fd
  39. unset fd
  40. set argv0 [lindex $argv 0]
  41. set argv [lrange $argv 1 end]
  42. }
  43. if {[info exists mainscript]} {
  44. eval $mainscript
  45. return
  46. }
  47. }
  48. # If all else fails, do an interactive loop
  49. #
  50. set line {}
  51. while {![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. }