1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-07-30 19:03:16 +03:00

Fixes to the appendvfs.c extension. Add the "sqltclsh" application that

uses appendvfs.c to find its scripts.

FossilOrigin-Name: ee248b529c2396c5480fb99b0a1dc31032627ec8241eca4a8c0fff257bb4a088
This commit is contained in:
drh
2017-12-14 19:24:00 +00:00
parent 233ff96ec5
commit dd2a43a225
7 changed files with 138 additions and 11 deletions

42
tool/sqltclsh.c.in Normal file
View File

@ -0,0 +1,42 @@
/*
** This is the source code to a "tclsh" that has SQLite built-in.
**
** The startup script is located as follows:
**
** (1) Open the executable as an appended SQLite database and try to
** read the startup script out of that database.
**
** (2) If the first argument is a readable file, try to open that file
** as an SQLite database and read the startup script out of that
** database.
**
** (3) If the first argument is a readable file with a ".tcl" extension,
** then try to run that script directly.
**
** If none of the above steps work, then the program runs as an interactive
** tclsh.
*/
#define TCLSH_INIT_PROC sqlite3_tclapp_init_proc
#define SQLITE_ENABLE_DBSTAT_VTAB 1
#undef SQLITE_THREADSAFE
#define SQLITE_THREADSAFE 0
#undef SQLITE_ENABLE_COLUMN_METADATA
#define SQLITE_OMIT_DECLTYPE 1
#define SQLITE_OMIT_DEPRECATED 1
#define SQLITE_OMIT_PROGRESS_CALLBACK 1
#define SQLITE_OMIT_SHARED_CACHE 1
#define SQLITE_DEFAULT_MEMSTATUS 0
#define SQLITE_MAX_EXPR_DEPTH 0
INCLUDE sqlite3.c
INCLUDE $ROOT/ext/misc/appendvfs.c
INCLUDE $ROOT/src/tclsqlite.c
const char *sqlite3_tclapp_init_proc(Tcl_Interp *interp){
(void)interp;
sqlite3_appendvfs_init(0,0,0);
return
BEGIN_STRING
INCLUDE $ROOT/tool/sqltclsh.tcl
END_STRING
;
}

66
tool/sqltclsh.tcl Normal file
View File

@ -0,0 +1,66 @@
# Try to open the executable as a database and read the "scripts.data"
# field where "scripts.name" is 'main.tcl'
#
catch {
sqlite3 db $argv0 -vfs apndvfs -create 0
set mainscript [db one {SELECT data FROM scripts WHERE name='main.tcl'}]
}
if {[info exists mainscript]} {
eval $mainscript
return
} else {
catch {db close}
}
# Try to open file named in the first argument as a database and
# read the "scripts.data" field where "scripts.name" is 'main.tcl'
#
if {[llength $argv]>0 && [file readable [lindex $argv 0]]} {
catch {
sqlite3 db [lindex $argv 0] -vfs apndvfs -create 0
set mainscript [db one {SELECT data FROM scripts WHERE name='main.tcl'}]
set argv0 [lindex $argv 0]
set argv [lrange $argv 1 end]
}
if {[info exists mainscript]} {
eval $mainscript
return
} else {
catch {db close}
}
if {[string match *.tcl [lindex $argv 0]]} {
set fd [open [lindex $argv 0] rb]
set mainscript [read $fd]
close $fd
unset fd
set argv0 [lindex $argv 0]
set argv [lrange $argv 1 end]
}
if {[info exists mainscript]} {
eval $mainscript
return
}
}
# If all else fails, do an interactive loop
#
set line {}
while {![eof stdin]} {
if {$line!=""} {
puts -nonewline "> "
} else {
puts -nonewline "% "
}
flush stdout
append line [gets stdin]
if {[info complete $line]} {
if {[catch {uplevel #0 $line} result]} {
puts stderr "Error: $result"
} elseif {$result!=""} {
puts $result
}
set line {}
} else {
append line \\n"
}
}