blob: 57df83b86d4698096eefa7d1c89e5149a0575f04 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
2** Copyright (c) 1999, 2000 D. Richard Hipp
3**
4** This program is free software; you can redistribute it and/or
5** modify it under the terms of the GNU General Public
6** License as published by the Free Software Foundation; either
7** version 2 of the License, or (at your option) any later version.
8**
9** This program is distributed in the hope that it will be useful,
10** but WITHOUT ANY WARRANTY; without even the implied warranty of
11** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12** General Public License for more details.
13**
14** You should have received a copy of the GNU General Public
15** License along with this library; if not, write to the
16** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17** Boston, MA 02111-1307, USA.
18**
19** Author contact information:
20** drh@hwaci.com
21** http://www.hwaci.com/drh/
22**
23*************************************************************************
24** A TCL Interface to SQLite
25**
drhfbc3eab2001-04-06 16:13:42 +000026** $Id: tclsqlite.c,v 1.16 2001/04/06 16:13:43 drh Exp $
drh75897232000-05-29 14:26:00 +000027*/
drh6d313162000-09-21 13:01:35 +000028#ifndef NO_TCL /* Omit this whole file if TCL is unavailable */
29
drh75897232000-05-29 14:26:00 +000030#include "sqlite.h"
drh17a68932001-01-31 13:28:08 +000031#include "tcl.h"
drh75897232000-05-29 14:26:00 +000032#include <stdlib.h>
33#include <string.h>
34
35/*
drhbec3f402000-08-04 13:49:02 +000036** There is one instance of this structure for each SQLite database
37** that has been opened by the SQLite TCL interface.
38*/
39typedef struct SqliteDb SqliteDb;
40struct SqliteDb {
41 sqlite *db; /* The "real" database structure */
42 Tcl_Interp *interp; /* The interpreter used for this database */
drh6d313162000-09-21 13:01:35 +000043 char *zBusy; /* The busy callback routine */
drhbec3f402000-08-04 13:49:02 +000044};
45
46/*
drh75897232000-05-29 14:26:00 +000047** An instance of this structure passes information thru the sqlite
48** logic from the original TCL command into the callback routine.
49*/
50typedef struct CallbackData CallbackData;
51struct CallbackData {
52 Tcl_Interp *interp; /* The TCL interpreter */
53 char *zArray; /* The array into which data is written */
drh6d313162000-09-21 13:01:35 +000054 Tcl_Obj *pCode; /* The code to execute for each row */
drh75897232000-05-29 14:26:00 +000055 int once; /* Set only for the first invocation of callback */
drh960e8c62001-04-03 16:53:21 +000056 int tcl_rc; /* Return code from TCL script */
drh75897232000-05-29 14:26:00 +000057};
58
59/*
drh297ecf12001-04-05 15:57:13 +000060** If TCL uses UTF-8 and SQLite is configured to use iso8859, then we
61** have to do a translation when going between the two. Set the
62** UTF_TRANSLATION_NEEDED macro to indicate that we need to do
63** this translation.
64*/
65#if defined(TCL_UTF_MAX) && !defined(SQLITE_UTF8)
66# define UTF_TRANSLATION_NEEDED 1
67#endif
68
69/*
drh75897232000-05-29 14:26:00 +000070** Called for each row of the result.
71*/
72static int DbEvalCallback(
73 void *clientData, /* An instance of CallbackData */
74 int nCol, /* Number of columns in the result */
75 char ** azCol, /* Data for each column */
76 char ** azN /* Name for each column */
77){
78 CallbackData *cbData = (CallbackData*)clientData;
79 int i, rc;
drh297ecf12001-04-05 15:57:13 +000080#ifdef UTF_TRANSLATION_NEEDED
81 Tcl_DString dCol;
82#endif
drh75897232000-05-29 14:26:00 +000083 if( cbData->zArray[0] ){
84 if( cbData->once ){
drh9b0d0a82000-09-30 22:46:05 +000085 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0);
drh75897232000-05-29 14:26:00 +000086 for(i=0; i<nCol; i++){
87 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", azN[i],
88 TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
89 }
90 }
91 for(i=0; i<nCol; i++){
drhc61053b2000-06-04 12:58:36 +000092 char *z = azCol[i];
93 if( z==0 ) z = "";
drh297ecf12001-04-05 15:57:13 +000094#ifdef UTF_TRANSLATION_NEEDED
95 Tcl_DStringInit(&dCol);
96 Tcl_ExternalToUtfDString(NULL, z, -1, &dCol);
97 Tcl_SetVar2(cbData->interp, cbData->zArray, azN[i],
98 Tcl_DStringValue(&dCol), 0);
99 Tcl_DStringFree(&dCol);
100#else
drhc61053b2000-06-04 12:58:36 +0000101 Tcl_SetVar2(cbData->interp, cbData->zArray, azN[i], z, 0);
drh297ecf12001-04-05 15:57:13 +0000102#endif
drh75897232000-05-29 14:26:00 +0000103 }
104 }else{
105 for(i=0; i<nCol; i++){
drhc61053b2000-06-04 12:58:36 +0000106 char *z = azCol[i];
107 if( z==0 ) z = "";
drh297ecf12001-04-05 15:57:13 +0000108#ifdef UTF_TRANSLATION_NEEDED
109 Tcl_DStringInit(&dCol);
110 Tcl_ExternalToUtfDString(NULL, z, -1, &dCol);
111 Tcl_SetVar(cbData->interp, azN[i], Tcl_DStringValue(&dCol), 0);
112 Tcl_DStringFree(&dCol);
113#else
drhc61053b2000-06-04 12:58:36 +0000114 Tcl_SetVar(cbData->interp, azN[i], z, 0);
drh297ecf12001-04-05 15:57:13 +0000115#endif
drh75897232000-05-29 14:26:00 +0000116 }
117 }
118 cbData->once = 0;
drh6d313162000-09-21 13:01:35 +0000119 rc = Tcl_EvalObj(cbData->interp, cbData->pCode);
drh960e8c62001-04-03 16:53:21 +0000120 if( rc==TCL_CONTINUE ) rc = TCL_OK;
121 cbData->tcl_rc = rc;
122 return rc!=TCL_OK;
drh75897232000-05-29 14:26:00 +0000123}
124
125/*
drh6d313162000-09-21 13:01:35 +0000126** This is an alternative callback for database queries. Instead
127** of invoking a TCL script to handle the result, this callback just
128** appends each column of the result to a list. After the query
129** is complete, the list is returned.
130*/
131static int DbEvalCallback2(
132 void *clientData, /* An instance of CallbackData */
133 int nCol, /* Number of columns in the result */
134 char ** azCol, /* Data for each column */
135 char ** azN /* Name for each column */
136){
137 Tcl_Obj *pList = (Tcl_Obj*)clientData;
138 int i;
139 for(i=0; i<nCol; i++){
140 Tcl_Obj *pElem;
141 if( azCol[i] && *azCol[i] ){
drh297ecf12001-04-05 15:57:13 +0000142#ifdef UTF_TRANSLATION_NEEDED
143 Tcl_DString dCol;
144 Tcl_DStringInit(&dCol);
145 Tcl_ExternalToUtfDString(NULL, azCol[i], -1, &dCol);
146 pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
147 Tcl_DStringFree(&dCol);
148#else
drh6d313162000-09-21 13:01:35 +0000149 pElem = Tcl_NewStringObj(azCol[i], -1);
drh297ecf12001-04-05 15:57:13 +0000150#endif
drh6d313162000-09-21 13:01:35 +0000151 }else{
152 pElem = Tcl_NewObj();
153 }
154 Tcl_ListObjAppendElement(0, pList, pElem);
155 }
156 return 0;
157}
158
159/*
drh75897232000-05-29 14:26:00 +0000160** Called when the command is deleted.
161*/
162static void DbDeleteCmd(void *db){
drhbec3f402000-08-04 13:49:02 +0000163 SqliteDb *pDb = (SqliteDb*)db;
164 sqlite_close(pDb->db);
165 if( pDb->zBusy ){
166 Tcl_Free(pDb->zBusy);
167 }
168 Tcl_Free((char*)pDb);
169}
170
171/*
172** This routine is called when a database file is locked while trying
173** to execute SQL.
174*/
175static int DbBusyHandler(void *cd, const char *zTable, int nTries){
176 SqliteDb *pDb = (SqliteDb*)cd;
177 int rc;
178 char zVal[30];
179 char *zCmd;
drhbec3f402000-08-04 13:49:02 +0000180 Tcl_DString cmd;
181
182 Tcl_DStringInit(&cmd);
183 Tcl_DStringAppend(&cmd, pDb->zBusy, -1);
184 Tcl_DStringAppendElement(&cmd, zTable);
185 sprintf(zVal, " %d", nTries);
186 Tcl_DStringAppend(&cmd, zVal, -1);
187 zCmd = Tcl_DStringValue(&cmd);
188 rc = Tcl_Eval(pDb->interp, zCmd);
189 Tcl_DStringFree(&cmd);
190 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
191 return 0;
192 }
193 return 1;
drh75897232000-05-29 14:26:00 +0000194}
195
196/*
197** The "sqlite" command below creates a new Tcl command for each
198** connection it opens to an SQLite database. This routine is invoked
199** whenever one of those connection-specific commands is executed
200** in Tcl. For example, if you run Tcl code like this:
201**
202** sqlite db1 "my_database"
203** db1 close
204**
205** The first command opens a connection to the "my_database" database
206** and calls that connection "db1". The second command causes this
207** subroutine to be invoked.
208*/
drh6d313162000-09-21 13:01:35 +0000209static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000210 SqliteDb *pDb = (SqliteDb*)cd;
drh6d313162000-09-21 13:01:35 +0000211 int choice;
212 static char *DB_optStrs[] = {
drh960e8c62001-04-03 16:53:21 +0000213 "busy", "close", "complete", "eval", "timeout", 0
drh6d313162000-09-21 13:01:35 +0000214 };
215 enum DB_opts {
216 DB_BUSY, DB_CLOSE, DB_COMPLETE, DB_EVAL, DB_TIMEOUT
217 };
218
219 if( objc<2 ){
220 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
drh75897232000-05-29 14:26:00 +0000221 return TCL_ERROR;
222 }
drh6d313162000-09-21 13:01:35 +0000223 if( Tcl_GetIndexFromObj(interp, objv[1], DB_optStrs, "option", 0, &choice) ){
224 return TCL_ERROR;
225 }
226
227 switch( (enum DB_opts)choice ){
drh75897232000-05-29 14:26:00 +0000228
drhbec3f402000-08-04 13:49:02 +0000229 /* $db busy ?CALLBACK?
230 **
231 ** Invoke the given callback if an SQL statement attempts to open
232 ** a locked database file.
233 */
drh6d313162000-09-21 13:01:35 +0000234 case DB_BUSY: {
235 if( objc>3 ){
236 Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
drhbec3f402000-08-04 13:49:02 +0000237 return TCL_ERROR;
drh6d313162000-09-21 13:01:35 +0000238 }else if( objc==2 ){
drhbec3f402000-08-04 13:49:02 +0000239 if( pDb->zBusy ){
240 Tcl_AppendResult(interp, pDb->zBusy, 0);
241 }
242 }else{
drh6d313162000-09-21 13:01:35 +0000243 char *zBusy;
244 int len;
drhbec3f402000-08-04 13:49:02 +0000245 if( pDb->zBusy ){
246 Tcl_Free(pDb->zBusy);
drhbec3f402000-08-04 13:49:02 +0000247 }
drh6d313162000-09-21 13:01:35 +0000248 zBusy = Tcl_GetStringFromObj(objv[2], &len);
249 if( zBusy && len>0 ){
250 pDb->zBusy = Tcl_Alloc( len + 1 );
251 strcpy(pDb->zBusy, zBusy);
252 }else{
253 pDb->zBusy = 0;
drhbec3f402000-08-04 13:49:02 +0000254 }
255 if( pDb->zBusy ){
256 pDb->interp = interp;
257 sqlite_busy_handler(pDb->db, DbBusyHandler, pDb);
drh6d313162000-09-21 13:01:35 +0000258 }else{
259 sqlite_busy_handler(pDb->db, 0, 0);
drhbec3f402000-08-04 13:49:02 +0000260 }
261 }
drh6d313162000-09-21 13:01:35 +0000262 break;
263 }
drhbec3f402000-08-04 13:49:02 +0000264
drh75897232000-05-29 14:26:00 +0000265 /* $db close
266 **
267 ** Shutdown the database
268 */
drh6d313162000-09-21 13:01:35 +0000269 case DB_CLOSE: {
270 Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0));
271 break;
272 }
drh75897232000-05-29 14:26:00 +0000273
274 /* $db complete SQL
275 **
276 ** Return TRUE if SQL is a complete SQL statement. Return FALSE if
277 ** additional lines of input are needed. This is similar to the
278 ** built-in "info complete" command of Tcl.
279 */
drh6d313162000-09-21 13:01:35 +0000280 case DB_COMPLETE: {
281 Tcl_Obj *pResult;
282 int isComplete;
283 if( objc!=3 ){
284 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
drh75897232000-05-29 14:26:00 +0000285 return TCL_ERROR;
286 }
drh6d313162000-09-21 13:01:35 +0000287 isComplete = sqlite_complete( Tcl_GetStringFromObj(objv[2], 0) );
288 pResult = Tcl_GetObjResult(interp);
289 Tcl_SetBooleanObj(pResult, isComplete);
290 break;
291 }
drh75897232000-05-29 14:26:00 +0000292
293 /*
294 ** $db eval $sql ?array { ...code... }?
295 **
296 ** The SQL statement in $sql is evaluated. For each row, the values are
drhbec3f402000-08-04 13:49:02 +0000297 ** placed in elements of the array named "array" and ...code... is executed.
drh75897232000-05-29 14:26:00 +0000298 ** If "array" and "code" are omitted, then no callback is every invoked.
299 ** If "array" is an empty string, then the values are placed in variables
300 ** that have the same name as the fields extracted by the query.
301 */
drh6d313162000-09-21 13:01:35 +0000302 case DB_EVAL: {
drh75897232000-05-29 14:26:00 +0000303 CallbackData cbData;
304 char *zErrMsg;
drh6d313162000-09-21 13:01:35 +0000305 char *zSql;
drh75897232000-05-29 14:26:00 +0000306 int rc;
drh297ecf12001-04-05 15:57:13 +0000307#ifdef UTF_TRANSLATION_NEEDED
308 Tcl_DString dSql;
309#endif
drh75897232000-05-29 14:26:00 +0000310
drh6d313162000-09-21 13:01:35 +0000311 if( objc!=5 && objc!=3 ){
312 Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?");
drh75897232000-05-29 14:26:00 +0000313 return TCL_ERROR;
314 }
drhbec3f402000-08-04 13:49:02 +0000315 pDb->interp = interp;
drh6d313162000-09-21 13:01:35 +0000316 zSql = Tcl_GetStringFromObj(objv[2], 0);
drh297ecf12001-04-05 15:57:13 +0000317#ifdef UTF_TRANSLATION_NEEDED
318 Tcl_DStringInit(&dSql);
319 Tcl_UtfToExternalDString(NULL, zSql, -1, &dSql);
320 zSql = Tcl_DStringValue(&dSql);
321#endif
drh6d313162000-09-21 13:01:35 +0000322 Tcl_IncrRefCount(objv[2]);
323 if( objc==5 ){
drh75897232000-05-29 14:26:00 +0000324 cbData.interp = interp;
drhdcc581c2000-05-30 13:44:19 +0000325 cbData.once = 1;
drh6d313162000-09-21 13:01:35 +0000326 cbData.zArray = Tcl_GetStringFromObj(objv[3], 0);
327 cbData.pCode = objv[4];
drh960e8c62001-04-03 16:53:21 +0000328 cbData.tcl_rc = TCL_OK;
drh75897232000-05-29 14:26:00 +0000329 zErrMsg = 0;
drh6d313162000-09-21 13:01:35 +0000330 Tcl_IncrRefCount(objv[3]);
331 Tcl_IncrRefCount(objv[4]);
332 rc = sqlite_exec(pDb->db, zSql, DbEvalCallback, &cbData, &zErrMsg);
333 Tcl_DecrRefCount(objv[4]);
334 Tcl_DecrRefCount(objv[3]);
drh960e8c62001-04-03 16:53:21 +0000335 if( cbData.tcl_rc==TCL_BREAK ){ cbData.tcl_rc = TCL_OK; }
drh75897232000-05-29 14:26:00 +0000336 }else{
drh6d313162000-09-21 13:01:35 +0000337 Tcl_Obj *pList = Tcl_NewObj();
drh960e8c62001-04-03 16:53:21 +0000338 cbData.tcl_rc = TCL_OK;
drh6d313162000-09-21 13:01:35 +0000339 rc = sqlite_exec(pDb->db, zSql, DbEvalCallback2, pList, &zErrMsg);
340 Tcl_SetObjResult(interp, pList);
drh75897232000-05-29 14:26:00 +0000341 }
342 if( zErrMsg ){
343 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
344 free(zErrMsg);
drh960e8c62001-04-03 16:53:21 +0000345 rc = TCL_ERROR;
346 }else{
347 rc = cbData.tcl_rc;
drh75897232000-05-29 14:26:00 +0000348 }
drh6d313162000-09-21 13:01:35 +0000349 Tcl_DecrRefCount(objv[2]);
drh297ecf12001-04-05 15:57:13 +0000350#ifdef UTF_TRANSLATION_NEEDED
351 Tcl_DStringFree(&dSql);
352#endif
drh75897232000-05-29 14:26:00 +0000353 return rc;
drh6d313162000-09-21 13:01:35 +0000354 }
drhbec3f402000-08-04 13:49:02 +0000355
356 /*
357 ** $db timeout MILLESECONDS
358 **
359 ** Delay for the number of milliseconds specified when a file is locked.
360 */
drh6d313162000-09-21 13:01:35 +0000361 case DB_TIMEOUT: {
drhbec3f402000-08-04 13:49:02 +0000362 int ms;
drh6d313162000-09-21 13:01:35 +0000363 if( objc!=3 ){
364 Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
drhbec3f402000-08-04 13:49:02 +0000365 return TCL_ERROR;
366 }
drh6d313162000-09-21 13:01:35 +0000367 if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
drhbec3f402000-08-04 13:49:02 +0000368 sqlite_busy_timeout(pDb->db, ms);
drh6d313162000-09-21 13:01:35 +0000369 break;
drh75897232000-05-29 14:26:00 +0000370 }
drh6d313162000-09-21 13:01:35 +0000371 } /* End of the SWITCH statement */
drh75897232000-05-29 14:26:00 +0000372 return TCL_OK;
373}
374
375/*
376** sqlite DBNAME FILENAME ?MODE?
377**
378** This is the main Tcl command. When the "sqlite" Tcl command is
379** invoked, this routine runs to process that command.
380**
381** The first argument, DBNAME, is an arbitrary name for a new
382** database connection. This command creates a new command named
383** DBNAME that is used to control that connection. The database
384** connection is deleted when the DBNAME command is deleted.
385**
386** The second argument is the name of the directory that contains
387** the sqlite database that is to be accessed.
drhfbc3eab2001-04-06 16:13:42 +0000388**
389** For testing purposes, we also support the following:
390**
391** sqlite -encoding
392**
393** Return the encoding used by LIKE and GLOB operators. Choices
394** are UTF-8 and iso8859.
395**
396** sqlite -tcl-uses-utf
397**
398** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if
399** not. Used by tests to make sure the library was compiled
400** correctly.
drh75897232000-05-29 14:26:00 +0000401*/
402static int DbMain(void *cd, Tcl_Interp *interp, int argc, char **argv){
403 int mode;
drhbec3f402000-08-04 13:49:02 +0000404 SqliteDb *p;
drh75897232000-05-29 14:26:00 +0000405 char *zErrMsg;
drhfbc3eab2001-04-06 16:13:42 +0000406 if( argc==2 ){
407 if( strcmp(argv[1],"-encoding")==0 ){
408 Tcl_AppendResult(interp,sqlite_encoding,0);
409 return TCL_OK;
410 }
411 if( strcmp(argv[1],"-tcl-uses-utf")==0 ){
412#ifdef TCL_UTF_MAX
413 Tcl_AppendResult(interp,"1",0);
414#else
415 Tcl_AppendResult(interp,"0",0);
416#endif
417 return TCL_OK;
418 }
419 }
drh75897232000-05-29 14:26:00 +0000420 if( argc!=3 && argc!=4 ){
421 Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0],
422 " HANDLE FILENAME ?MODE?\"", 0);
423 return TCL_ERROR;
424 }
425 if( argc==3 ){
drh58b95762000-06-02 01:17:37 +0000426 mode = 0666;
drh75897232000-05-29 14:26:00 +0000427 }else if( Tcl_GetInt(interp, argv[3], &mode)!=TCL_OK ){
428 return TCL_ERROR;
429 }
430 zErrMsg = 0;
drh4cdc9e82000-08-04 14:56:24 +0000431 p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
drh75897232000-05-29 14:26:00 +0000432 if( p==0 ){
drhbec3f402000-08-04 13:49:02 +0000433 Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
434 return TCL_ERROR;
435 }
436 memset(p, 0, sizeof(*p));
437 p->db = sqlite_open(argv[2], mode, &zErrMsg);
438 if( p->db==0 ){
drh75897232000-05-29 14:26:00 +0000439 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
drhbec3f402000-08-04 13:49:02 +0000440 Tcl_Free((char*)p);
drh75897232000-05-29 14:26:00 +0000441 free(zErrMsg);
442 return TCL_ERROR;
443 }
drh6d313162000-09-21 13:01:35 +0000444 Tcl_CreateObjCommand(interp, argv[1], DbObjCmd, (char*)p, DbDeleteCmd);
drh75897232000-05-29 14:26:00 +0000445 return TCL_OK;
446}
447
448/*
449** Initialize this module.
450**
451** This Tcl module contains only a single new Tcl command named "sqlite".
452** (Hence there is no namespace. There is no point in using a namespace
453** if the extension only supplies one new name!) The "sqlite" command is
454** used to open a new SQLite database. See the DbMain() routine above
455** for additional information.
456*/
457int Sqlite_Init(Tcl_Interp *interp){
458 Tcl_CreateCommand(interp, "sqlite", DbMain, 0, 0);
drh167a4b12000-08-17 09:49:59 +0000459 Tcl_PkgProvide(interp, "sqlite", "1.0");
drh75897232000-05-29 14:26:00 +0000460 return TCL_OK;
461}
462int Sqlite_SafeInit(Tcl_Interp *interp){
463 return TCL_OK;
464}
465
drh3cebbde2000-10-19 14:59:27 +0000466#if 0
drh75897232000-05-29 14:26:00 +0000467/*
468** If compiled using mktclapp, this routine runs to initialize
469** everything.
470*/
471int Et_AppInit(Tcl_Interp *interp){
472 return Sqlite_Init(interp);
473}
drh3cebbde2000-10-19 14:59:27 +0000474#endif
drh348784e2000-05-29 20:41:49 +0000475
476/*
477** If the macro TCLSH is defined and is one, then put in code for the
478** "main" routine that will initialize Tcl.
479*/
480#if defined(TCLSH) && TCLSH==1
481static char zMainloop[] =
482 "set line {}\n"
483 "while {![eof stdin]} {\n"
484 "if {$line!=\"\"} {\n"
485 "puts -nonewline \"> \"\n"
486 "} else {\n"
487 "puts -nonewline \"% \"\n"
488 "}\n"
489 "flush stdout\n"
490 "append line [gets stdin]\n"
491 "if {[info complete $line]} {\n"
492 "if {[catch {uplevel #0 $line} result]} {\n"
493 "puts stderr \"Error: $result\"\n"
494 "} elseif {$result!=\"\"} {\n"
495 "puts $result\n"
496 "}\n"
497 "set line {}\n"
498 "} else {\n"
499 "append line \\n\n"
500 "}\n"
501 "}\n"
502;
503
504#define TCLSH_MAIN main /* Needed to fake out mktclapp */
505int TCLSH_MAIN(int argc, char **argv){
506 Tcl_Interp *interp;
drh297ecf12001-04-05 15:57:13 +0000507 Tcl_FindExecutable(argv[0]);
drh348784e2000-05-29 20:41:49 +0000508 interp = Tcl_CreateInterp();
509 Sqlite_Init(interp);
510 if( argc>=2 ){
511 int i;
512 Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
513 Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
514 for(i=2; i<argc; i++){
515 Tcl_SetVar(interp, "argv", argv[i],
516 TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
517 }
518 if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
drhc61053b2000-06-04 12:58:36 +0000519 char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
520 if( zInfo==0 ) zInfo = interp->result;
521 fprintf(stderr,"%s: %s\n", *argv, zInfo);
drh348784e2000-05-29 20:41:49 +0000522 return 1;
523 }
524 }else{
525 Tcl_GlobalEval(interp, zMainloop);
526 }
527 return 0;
528}
529#endif /* TCLSH */
drh6d313162000-09-21 13:01:35 +0000530
531#endif /* !defined(NO_TCL) */