blob: bf713e8e4e4ce9065df081e5ac3e1403c35adeb4 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh75897232000-05-29 14:26:00 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:00 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
drh75897232000-05-29 14:26:00 +000010**
11*************************************************************************
12** A TCL Interface to SQLite
13**
drh90ca9752001-09-28 17:47:14 +000014** $Id: tclsqlite.c,v 1.24 2001/09/28 17:47:14 drh Exp $
drh75897232000-05-29 14:26:00 +000015*/
drh6d313162000-09-21 13:01:35 +000016#ifndef NO_TCL /* Omit this whole file if TCL is unavailable */
17
drh75897232000-05-29 14:26:00 +000018#include "sqlite.h"
drh17a68932001-01-31 13:28:08 +000019#include "tcl.h"
drh75897232000-05-29 14:26:00 +000020#include <stdlib.h>
21#include <string.h>
22
23/*
drhbec3f402000-08-04 13:49:02 +000024** There is one instance of this structure for each SQLite database
25** that has been opened by the SQLite TCL interface.
26*/
27typedef struct SqliteDb SqliteDb;
28struct SqliteDb {
29 sqlite *db; /* The "real" database structure */
30 Tcl_Interp *interp; /* The interpreter used for this database */
drh6d313162000-09-21 13:01:35 +000031 char *zBusy; /* The busy callback routine */
drhbec3f402000-08-04 13:49:02 +000032};
33
34/*
drh75897232000-05-29 14:26:00 +000035** An instance of this structure passes information thru the sqlite
36** logic from the original TCL command into the callback routine.
37*/
38typedef struct CallbackData CallbackData;
39struct CallbackData {
40 Tcl_Interp *interp; /* The TCL interpreter */
41 char *zArray; /* The array into which data is written */
drh6d313162000-09-21 13:01:35 +000042 Tcl_Obj *pCode; /* The code to execute for each row */
drh75897232000-05-29 14:26:00 +000043 int once; /* Set only for the first invocation of callback */
drh960e8c62001-04-03 16:53:21 +000044 int tcl_rc; /* Return code from TCL script */
drh75897232000-05-29 14:26:00 +000045};
46
47/*
drh297ecf12001-04-05 15:57:13 +000048** If TCL uses UTF-8 and SQLite is configured to use iso8859, then we
49** have to do a translation when going between the two. Set the
50** UTF_TRANSLATION_NEEDED macro to indicate that we need to do
51** this translation.
52*/
53#if defined(TCL_UTF_MAX) && !defined(SQLITE_UTF8)
54# define UTF_TRANSLATION_NEEDED 1
55#endif
56
57/*
drh75897232000-05-29 14:26:00 +000058** Called for each row of the result.
59*/
60static int DbEvalCallback(
61 void *clientData, /* An instance of CallbackData */
62 int nCol, /* Number of columns in the result */
63 char ** azCol, /* Data for each column */
64 char ** azN /* Name for each column */
65){
66 CallbackData *cbData = (CallbackData*)clientData;
67 int i, rc;
drh297ecf12001-04-05 15:57:13 +000068#ifdef UTF_TRANSLATION_NEEDED
69 Tcl_DString dCol;
70#endif
drh75897232000-05-29 14:26:00 +000071 if( cbData->zArray[0] ){
72 if( cbData->once ){
drh9b0d0a82000-09-30 22:46:05 +000073 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0);
drh75897232000-05-29 14:26:00 +000074 for(i=0; i<nCol; i++){
75 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", azN[i],
76 TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
77 }
78 }
79 for(i=0; i<nCol; i++){
drhc61053b2000-06-04 12:58:36 +000080 char *z = azCol[i];
81 if( z==0 ) z = "";
drh297ecf12001-04-05 15:57:13 +000082#ifdef UTF_TRANSLATION_NEEDED
83 Tcl_DStringInit(&dCol);
84 Tcl_ExternalToUtfDString(NULL, z, -1, &dCol);
85 Tcl_SetVar2(cbData->interp, cbData->zArray, azN[i],
86 Tcl_DStringValue(&dCol), 0);
87 Tcl_DStringFree(&dCol);
88#else
drhc61053b2000-06-04 12:58:36 +000089 Tcl_SetVar2(cbData->interp, cbData->zArray, azN[i], z, 0);
drh297ecf12001-04-05 15:57:13 +000090#endif
drh75897232000-05-29 14:26:00 +000091 }
92 }else{
93 for(i=0; i<nCol; i++){
drhc61053b2000-06-04 12:58:36 +000094 char *z = azCol[i];
95 if( z==0 ) z = "";
drh297ecf12001-04-05 15:57:13 +000096#ifdef UTF_TRANSLATION_NEEDED
97 Tcl_DStringInit(&dCol);
98 Tcl_ExternalToUtfDString(NULL, z, -1, &dCol);
99 Tcl_SetVar(cbData->interp, azN[i], Tcl_DStringValue(&dCol), 0);
100 Tcl_DStringFree(&dCol);
101#else
drhc61053b2000-06-04 12:58:36 +0000102 Tcl_SetVar(cbData->interp, azN[i], z, 0);
drh297ecf12001-04-05 15:57:13 +0000103#endif
drh75897232000-05-29 14:26:00 +0000104 }
105 }
106 cbData->once = 0;
drh6d313162000-09-21 13:01:35 +0000107 rc = Tcl_EvalObj(cbData->interp, cbData->pCode);
drh960e8c62001-04-03 16:53:21 +0000108 if( rc==TCL_CONTINUE ) rc = TCL_OK;
109 cbData->tcl_rc = rc;
110 return rc!=TCL_OK;
drh75897232000-05-29 14:26:00 +0000111}
112
113/*
drh6d313162000-09-21 13:01:35 +0000114** This is an alternative callback for database queries. Instead
115** of invoking a TCL script to handle the result, this callback just
116** appends each column of the result to a list. After the query
117** is complete, the list is returned.
118*/
119static int DbEvalCallback2(
120 void *clientData, /* An instance of CallbackData */
121 int nCol, /* Number of columns in the result */
122 char ** azCol, /* Data for each column */
123 char ** azN /* Name for each column */
124){
125 Tcl_Obj *pList = (Tcl_Obj*)clientData;
126 int i;
127 for(i=0; i<nCol; i++){
128 Tcl_Obj *pElem;
129 if( azCol[i] && *azCol[i] ){
drh297ecf12001-04-05 15:57:13 +0000130#ifdef UTF_TRANSLATION_NEEDED
131 Tcl_DString dCol;
132 Tcl_DStringInit(&dCol);
133 Tcl_ExternalToUtfDString(NULL, azCol[i], -1, &dCol);
134 pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
135 Tcl_DStringFree(&dCol);
136#else
drh6d313162000-09-21 13:01:35 +0000137 pElem = Tcl_NewStringObj(azCol[i], -1);
drh297ecf12001-04-05 15:57:13 +0000138#endif
drh6d313162000-09-21 13:01:35 +0000139 }else{
140 pElem = Tcl_NewObj();
141 }
142 Tcl_ListObjAppendElement(0, pList, pElem);
143 }
144 return 0;
145}
146
147/*
drh75897232000-05-29 14:26:00 +0000148** Called when the command is deleted.
149*/
150static void DbDeleteCmd(void *db){
drhbec3f402000-08-04 13:49:02 +0000151 SqliteDb *pDb = (SqliteDb*)db;
152 sqlite_close(pDb->db);
153 if( pDb->zBusy ){
154 Tcl_Free(pDb->zBusy);
155 }
156 Tcl_Free((char*)pDb);
157}
158
159/*
160** This routine is called when a database file is locked while trying
161** to execute SQL.
162*/
163static int DbBusyHandler(void *cd, const char *zTable, int nTries){
164 SqliteDb *pDb = (SqliteDb*)cd;
165 int rc;
166 char zVal[30];
167 char *zCmd;
drhbec3f402000-08-04 13:49:02 +0000168 Tcl_DString cmd;
169
170 Tcl_DStringInit(&cmd);
171 Tcl_DStringAppend(&cmd, pDb->zBusy, -1);
172 Tcl_DStringAppendElement(&cmd, zTable);
173 sprintf(zVal, " %d", nTries);
174 Tcl_DStringAppend(&cmd, zVal, -1);
175 zCmd = Tcl_DStringValue(&cmd);
176 rc = Tcl_Eval(pDb->interp, zCmd);
177 Tcl_DStringFree(&cmd);
178 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
179 return 0;
180 }
181 return 1;
drh75897232000-05-29 14:26:00 +0000182}
183
184/*
185** The "sqlite" command below creates a new Tcl command for each
186** connection it opens to an SQLite database. This routine is invoked
187** whenever one of those connection-specific commands is executed
188** in Tcl. For example, if you run Tcl code like this:
189**
190** sqlite db1 "my_database"
191** db1 close
192**
193** The first command opens a connection to the "my_database" database
194** and calls that connection "db1". The second command causes this
195** subroutine to be invoked.
196*/
drh6d313162000-09-21 13:01:35 +0000197static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000198 SqliteDb *pDb = (SqliteDb*)cd;
drh6d313162000-09-21 13:01:35 +0000199 int choice;
200 static char *DB_optStrs[] = {
drh960e8c62001-04-03 16:53:21 +0000201 "busy", "close", "complete", "eval", "timeout", 0
drh6d313162000-09-21 13:01:35 +0000202 };
203 enum DB_opts {
204 DB_BUSY, DB_CLOSE, DB_COMPLETE, DB_EVAL, DB_TIMEOUT
205 };
206
207 if( objc<2 ){
208 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
drh75897232000-05-29 14:26:00 +0000209 return TCL_ERROR;
210 }
drh6d313162000-09-21 13:01:35 +0000211 if( Tcl_GetIndexFromObj(interp, objv[1], DB_optStrs, "option", 0, &choice) ){
212 return TCL_ERROR;
213 }
214
215 switch( (enum DB_opts)choice ){
drh75897232000-05-29 14:26:00 +0000216
drhbec3f402000-08-04 13:49:02 +0000217 /* $db busy ?CALLBACK?
218 **
219 ** Invoke the given callback if an SQL statement attempts to open
220 ** a locked database file.
221 */
drh6d313162000-09-21 13:01:35 +0000222 case DB_BUSY: {
223 if( objc>3 ){
224 Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
drhbec3f402000-08-04 13:49:02 +0000225 return TCL_ERROR;
drh6d313162000-09-21 13:01:35 +0000226 }else if( objc==2 ){
drhbec3f402000-08-04 13:49:02 +0000227 if( pDb->zBusy ){
228 Tcl_AppendResult(interp, pDb->zBusy, 0);
229 }
230 }else{
drh6d313162000-09-21 13:01:35 +0000231 char *zBusy;
232 int len;
drhbec3f402000-08-04 13:49:02 +0000233 if( pDb->zBusy ){
234 Tcl_Free(pDb->zBusy);
drhbec3f402000-08-04 13:49:02 +0000235 }
drh6d313162000-09-21 13:01:35 +0000236 zBusy = Tcl_GetStringFromObj(objv[2], &len);
237 if( zBusy && len>0 ){
238 pDb->zBusy = Tcl_Alloc( len + 1 );
239 strcpy(pDb->zBusy, zBusy);
240 }else{
241 pDb->zBusy = 0;
drhbec3f402000-08-04 13:49:02 +0000242 }
243 if( pDb->zBusy ){
244 pDb->interp = interp;
245 sqlite_busy_handler(pDb->db, DbBusyHandler, pDb);
drh6d313162000-09-21 13:01:35 +0000246 }else{
247 sqlite_busy_handler(pDb->db, 0, 0);
drhbec3f402000-08-04 13:49:02 +0000248 }
249 }
drh6d313162000-09-21 13:01:35 +0000250 break;
251 }
drhbec3f402000-08-04 13:49:02 +0000252
drh75897232000-05-29 14:26:00 +0000253 /* $db close
254 **
255 ** Shutdown the database
256 */
drh6d313162000-09-21 13:01:35 +0000257 case DB_CLOSE: {
258 Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0));
259 break;
260 }
drh75897232000-05-29 14:26:00 +0000261
262 /* $db complete SQL
263 **
264 ** Return TRUE if SQL is a complete SQL statement. Return FALSE if
265 ** additional lines of input are needed. This is similar to the
266 ** built-in "info complete" command of Tcl.
267 */
drh6d313162000-09-21 13:01:35 +0000268 case DB_COMPLETE: {
269 Tcl_Obj *pResult;
270 int isComplete;
271 if( objc!=3 ){
272 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
drh75897232000-05-29 14:26:00 +0000273 return TCL_ERROR;
274 }
drh6d313162000-09-21 13:01:35 +0000275 isComplete = sqlite_complete( Tcl_GetStringFromObj(objv[2], 0) );
276 pResult = Tcl_GetObjResult(interp);
277 Tcl_SetBooleanObj(pResult, isComplete);
278 break;
279 }
drh75897232000-05-29 14:26:00 +0000280
281 /*
282 ** $db eval $sql ?array { ...code... }?
283 **
284 ** The SQL statement in $sql is evaluated. For each row, the values are
drhbec3f402000-08-04 13:49:02 +0000285 ** placed in elements of the array named "array" and ...code... is executed.
drh75897232000-05-29 14:26:00 +0000286 ** If "array" and "code" are omitted, then no callback is every invoked.
287 ** If "array" is an empty string, then the values are placed in variables
288 ** that have the same name as the fields extracted by the query.
289 */
drh6d313162000-09-21 13:01:35 +0000290 case DB_EVAL: {
drh75897232000-05-29 14:26:00 +0000291 CallbackData cbData;
292 char *zErrMsg;
drh6d313162000-09-21 13:01:35 +0000293 char *zSql;
drh75897232000-05-29 14:26:00 +0000294 int rc;
drh297ecf12001-04-05 15:57:13 +0000295#ifdef UTF_TRANSLATION_NEEDED
296 Tcl_DString dSql;
297#endif
drh75897232000-05-29 14:26:00 +0000298
drh6d313162000-09-21 13:01:35 +0000299 if( objc!=5 && objc!=3 ){
300 Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?");
drh75897232000-05-29 14:26:00 +0000301 return TCL_ERROR;
302 }
drhbec3f402000-08-04 13:49:02 +0000303 pDb->interp = interp;
drh6d313162000-09-21 13:01:35 +0000304 zSql = Tcl_GetStringFromObj(objv[2], 0);
drh297ecf12001-04-05 15:57:13 +0000305#ifdef UTF_TRANSLATION_NEEDED
306 Tcl_DStringInit(&dSql);
307 Tcl_UtfToExternalDString(NULL, zSql, -1, &dSql);
308 zSql = Tcl_DStringValue(&dSql);
309#endif
drh6d313162000-09-21 13:01:35 +0000310 Tcl_IncrRefCount(objv[2]);
311 if( objc==5 ){
drh75897232000-05-29 14:26:00 +0000312 cbData.interp = interp;
drhdcc581c2000-05-30 13:44:19 +0000313 cbData.once = 1;
drh6d313162000-09-21 13:01:35 +0000314 cbData.zArray = Tcl_GetStringFromObj(objv[3], 0);
315 cbData.pCode = objv[4];
drh960e8c62001-04-03 16:53:21 +0000316 cbData.tcl_rc = TCL_OK;
drh75897232000-05-29 14:26:00 +0000317 zErrMsg = 0;
drh6d313162000-09-21 13:01:35 +0000318 Tcl_IncrRefCount(objv[3]);
319 Tcl_IncrRefCount(objv[4]);
320 rc = sqlite_exec(pDb->db, zSql, DbEvalCallback, &cbData, &zErrMsg);
321 Tcl_DecrRefCount(objv[4]);
322 Tcl_DecrRefCount(objv[3]);
drh960e8c62001-04-03 16:53:21 +0000323 if( cbData.tcl_rc==TCL_BREAK ){ cbData.tcl_rc = TCL_OK; }
drh75897232000-05-29 14:26:00 +0000324 }else{
drh6d313162000-09-21 13:01:35 +0000325 Tcl_Obj *pList = Tcl_NewObj();
drh960e8c62001-04-03 16:53:21 +0000326 cbData.tcl_rc = TCL_OK;
drh6d313162000-09-21 13:01:35 +0000327 rc = sqlite_exec(pDb->db, zSql, DbEvalCallback2, pList, &zErrMsg);
328 Tcl_SetObjResult(interp, pList);
drh75897232000-05-29 14:26:00 +0000329 }
330 if( zErrMsg ){
331 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
332 free(zErrMsg);
drh960e8c62001-04-03 16:53:21 +0000333 rc = TCL_ERROR;
334 }else{
335 rc = cbData.tcl_rc;
drh75897232000-05-29 14:26:00 +0000336 }
drh6d313162000-09-21 13:01:35 +0000337 Tcl_DecrRefCount(objv[2]);
drh297ecf12001-04-05 15:57:13 +0000338#ifdef UTF_TRANSLATION_NEEDED
339 Tcl_DStringFree(&dSql);
340#endif
drh75897232000-05-29 14:26:00 +0000341 return rc;
drh6d313162000-09-21 13:01:35 +0000342 }
drhbec3f402000-08-04 13:49:02 +0000343
344 /*
345 ** $db timeout MILLESECONDS
346 **
347 ** Delay for the number of milliseconds specified when a file is locked.
348 */
drh6d313162000-09-21 13:01:35 +0000349 case DB_TIMEOUT: {
drhbec3f402000-08-04 13:49:02 +0000350 int ms;
drh6d313162000-09-21 13:01:35 +0000351 if( objc!=3 ){
352 Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
drhbec3f402000-08-04 13:49:02 +0000353 return TCL_ERROR;
354 }
drh6d313162000-09-21 13:01:35 +0000355 if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
drhbec3f402000-08-04 13:49:02 +0000356 sqlite_busy_timeout(pDb->db, ms);
drh6d313162000-09-21 13:01:35 +0000357 break;
drh75897232000-05-29 14:26:00 +0000358 }
drh6d313162000-09-21 13:01:35 +0000359 } /* End of the SWITCH statement */
drh75897232000-05-29 14:26:00 +0000360 return TCL_OK;
361}
362
363/*
364** sqlite DBNAME FILENAME ?MODE?
365**
366** This is the main Tcl command. When the "sqlite" Tcl command is
367** invoked, this routine runs to process that command.
368**
369** The first argument, DBNAME, is an arbitrary name for a new
370** database connection. This command creates a new command named
371** DBNAME that is used to control that connection. The database
372** connection is deleted when the DBNAME command is deleted.
373**
374** The second argument is the name of the directory that contains
375** the sqlite database that is to be accessed.
drhfbc3eab2001-04-06 16:13:42 +0000376**
377** For testing purposes, we also support the following:
378**
379** sqlite -encoding
380**
381** Return the encoding used by LIKE and GLOB operators. Choices
382** are UTF-8 and iso8859.
383**
384** sqlite -tcl-uses-utf
385**
386** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if
387** not. Used by tests to make sure the library was compiled
388** correctly.
drh75897232000-05-29 14:26:00 +0000389*/
390static int DbMain(void *cd, Tcl_Interp *interp, int argc, char **argv){
391 int mode;
drhbec3f402000-08-04 13:49:02 +0000392 SqliteDb *p;
drh75897232000-05-29 14:26:00 +0000393 char *zErrMsg;
drhfbc3eab2001-04-06 16:13:42 +0000394 if( argc==2 ){
395 if( strcmp(argv[1],"-encoding")==0 ){
396 Tcl_AppendResult(interp,sqlite_encoding,0);
397 return TCL_OK;
398 }
399 if( strcmp(argv[1],"-tcl-uses-utf")==0 ){
400#ifdef TCL_UTF_MAX
401 Tcl_AppendResult(interp,"1",0);
402#else
403 Tcl_AppendResult(interp,"0",0);
404#endif
405 return TCL_OK;
406 }
407 }
drh75897232000-05-29 14:26:00 +0000408 if( argc!=3 && argc!=4 ){
409 Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0],
410 " HANDLE FILENAME ?MODE?\"", 0);
411 return TCL_ERROR;
412 }
413 if( argc==3 ){
drh58b95762000-06-02 01:17:37 +0000414 mode = 0666;
drh75897232000-05-29 14:26:00 +0000415 }else if( Tcl_GetInt(interp, argv[3], &mode)!=TCL_OK ){
416 return TCL_ERROR;
417 }
418 zErrMsg = 0;
drh4cdc9e82000-08-04 14:56:24 +0000419 p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
drh75897232000-05-29 14:26:00 +0000420 if( p==0 ){
drhbec3f402000-08-04 13:49:02 +0000421 Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
422 return TCL_ERROR;
423 }
424 memset(p, 0, sizeof(*p));
425 p->db = sqlite_open(argv[2], mode, &zErrMsg);
426 if( p->db==0 ){
drh75897232000-05-29 14:26:00 +0000427 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
drhbec3f402000-08-04 13:49:02 +0000428 Tcl_Free((char*)p);
drh75897232000-05-29 14:26:00 +0000429 free(zErrMsg);
430 return TCL_ERROR;
431 }
drh6d313162000-09-21 13:01:35 +0000432 Tcl_CreateObjCommand(interp, argv[1], DbObjCmd, (char*)p, DbDeleteCmd);
drh75897232000-05-29 14:26:00 +0000433 return TCL_OK;
434}
435
436/*
drh90ca9752001-09-28 17:47:14 +0000437** Provide a dummy Tcl_InitStubs if we are using this as a static
438** library.
439*/
440#ifndef USE_TCL_STUBS
441# undef Tcl_InitStubs
442# define Tcl_InitStubs(a,b,c)
443#endif
444
445/*
drh75897232000-05-29 14:26:00 +0000446** Initialize this module.
447**
448** This Tcl module contains only a single new Tcl command named "sqlite".
449** (Hence there is no namespace. There is no point in using a namespace
450** if the extension only supplies one new name!) The "sqlite" command is
451** used to open a new SQLite database. See the DbMain() routine above
452** for additional information.
453*/
454int Sqlite_Init(Tcl_Interp *interp){
drh90ca9752001-09-28 17:47:14 +0000455 Tcl_InitStubs(interp, "8.0", 0);
456 Tcl_CreateCommand(interp, "sqlite", DbMain, 0, 0);
457 Tcl_PkgProvide(interp, "sqlite", "1.0");
458 return TCL_OK;
459}
460int Tclsqlite_Init(Tcl_Interp *interp){
461 Tcl_InitStubs(interp, "8.0", 0);
drh75897232000-05-29 14:26:00 +0000462 Tcl_CreateCommand(interp, "sqlite", DbMain, 0, 0);
drh167a4b12000-08-17 09:49:59 +0000463 Tcl_PkgProvide(interp, "sqlite", "1.0");
drh75897232000-05-29 14:26:00 +0000464 return TCL_OK;
465}
466int Sqlite_SafeInit(Tcl_Interp *interp){
467 return TCL_OK;
468}
drh90ca9752001-09-28 17:47:14 +0000469int Tclsqlite_SafeInit(Tcl_Interp *interp){
470 return TCL_OK;
471}
drh75897232000-05-29 14:26:00 +0000472
drh3cebbde2000-10-19 14:59:27 +0000473#if 0
drh75897232000-05-29 14:26:00 +0000474/*
475** If compiled using mktclapp, this routine runs to initialize
476** everything.
477*/
478int Et_AppInit(Tcl_Interp *interp){
479 return Sqlite_Init(interp);
480}
drh3cebbde2000-10-19 14:59:27 +0000481#endif
drh348784e2000-05-29 20:41:49 +0000482
483/*
484** If the macro TCLSH is defined and is one, then put in code for the
485** "main" routine that will initialize Tcl.
486*/
487#if defined(TCLSH) && TCLSH==1
488static char zMainloop[] =
489 "set line {}\n"
490 "while {![eof stdin]} {\n"
491 "if {$line!=\"\"} {\n"
492 "puts -nonewline \"> \"\n"
493 "} else {\n"
494 "puts -nonewline \"% \"\n"
495 "}\n"
496 "flush stdout\n"
497 "append line [gets stdin]\n"
498 "if {[info complete $line]} {\n"
499 "if {[catch {uplevel #0 $line} result]} {\n"
500 "puts stderr \"Error: $result\"\n"
501 "} elseif {$result!=\"\"} {\n"
502 "puts $result\n"
503 "}\n"
504 "set line {}\n"
505 "} else {\n"
506 "append line \\n\n"
507 "}\n"
508 "}\n"
509;
510
511#define TCLSH_MAIN main /* Needed to fake out mktclapp */
512int TCLSH_MAIN(int argc, char **argv){
513 Tcl_Interp *interp;
drh297ecf12001-04-05 15:57:13 +0000514 Tcl_FindExecutable(argv[0]);
drh348784e2000-05-29 20:41:49 +0000515 interp = Tcl_CreateInterp();
516 Sqlite_Init(interp);
drhd9b02572001-04-15 00:37:09 +0000517#ifdef SQLITE_TEST
drhd1bf3512001-04-07 15:24:33 +0000518 {
519 extern int Sqlitetest1_Init(Tcl_Interp*);
drh5c4d9702001-08-20 00:33:58 +0000520 extern int Sqlitetest2_Init(Tcl_Interp*);
521 extern int Sqlitetest3_Init(Tcl_Interp*);
drhefc251d2001-07-01 22:12:01 +0000522 extern int Md5_Init(Tcl_Interp*);
drhd1bf3512001-04-07 15:24:33 +0000523 Sqlitetest1_Init(interp);
drh5c4d9702001-08-20 00:33:58 +0000524 Sqlitetest2_Init(interp);
525 Sqlitetest3_Init(interp);
drhefc251d2001-07-01 22:12:01 +0000526 Md5_Init(interp);
drhd1bf3512001-04-07 15:24:33 +0000527 }
528#endif
drh348784e2000-05-29 20:41:49 +0000529 if( argc>=2 ){
530 int i;
531 Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
532 Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
533 for(i=2; i<argc; i++){
534 Tcl_SetVar(interp, "argv", argv[i],
535 TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
536 }
537 if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
drhc61053b2000-06-04 12:58:36 +0000538 char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
539 if( zInfo==0 ) zInfo = interp->result;
540 fprintf(stderr,"%s: %s\n", *argv, zInfo);
drh348784e2000-05-29 20:41:49 +0000541 return 1;
542 }
543 }else{
544 Tcl_GlobalEval(interp, zMainloop);
545 }
546 return 0;
547}
548#endif /* TCLSH */
drh6d313162000-09-21 13:01:35 +0000549
550#endif /* !defined(NO_TCL) */