blob: 19e7ddb279bd1610cba1ea8a046930202dbb253e [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**
drh6a535342001-10-19 16:44:56 +000014** $Id: tclsqlite.c,v 1.26 2001/10/19 16:44:57 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/*
drh98808ba2001-10-18 12:34:46 +000024** If TCL uses UTF-8 and SQLite is configured to use iso8859, then we
25** have to do a translation when going between the two. Set the
26** UTF_TRANSLATION_NEEDED macro to indicate that we need to do
27** this translation.
28*/
29#if defined(TCL_UTF_MAX) && !defined(SQLITE_UTF8)
30# define UTF_TRANSLATION_NEEDED 1
31#endif
32
33/*
drhbec3f402000-08-04 13:49:02 +000034** There is one instance of this structure for each SQLite database
35** that has been opened by the SQLite TCL interface.
36*/
37typedef struct SqliteDb SqliteDb;
38struct SqliteDb {
39 sqlite *db; /* The "real" database structure */
40 Tcl_Interp *interp; /* The interpreter used for this database */
drh6d313162000-09-21 13:01:35 +000041 char *zBusy; /* The busy callback routine */
drhbec3f402000-08-04 13:49:02 +000042};
43
44/*
drh75897232000-05-29 14:26:00 +000045** An instance of this structure passes information thru the sqlite
46** logic from the original TCL command into the callback routine.
47*/
48typedef struct CallbackData CallbackData;
49struct CallbackData {
50 Tcl_Interp *interp; /* The TCL interpreter */
51 char *zArray; /* The array into which data is written */
drh6d313162000-09-21 13:01:35 +000052 Tcl_Obj *pCode; /* The code to execute for each row */
drh75897232000-05-29 14:26:00 +000053 int once; /* Set only for the first invocation of callback */
drh960e8c62001-04-03 16:53:21 +000054 int tcl_rc; /* Return code from TCL script */
drh98808ba2001-10-18 12:34:46 +000055#ifdef UTF_TRANSLATION_NEEDED
56 int nColName; /* Number of entries in the azColName[] array */
57 char **azColName; /* Column names translated to UTF-8 */
drh297ecf12001-04-05 15:57:13 +000058#endif
drh98808ba2001-10-18 12:34:46 +000059};
drh297ecf12001-04-05 15:57:13 +000060
61/*
drh75897232000-05-29 14:26:00 +000062** Called for each row of the result.
63*/
64static int DbEvalCallback(
65 void *clientData, /* An instance of CallbackData */
66 int nCol, /* Number of columns in the result */
67 char ** azCol, /* Data for each column */
68 char ** azN /* Name for each column */
69){
70 CallbackData *cbData = (CallbackData*)clientData;
71 int i, rc;
drh297ecf12001-04-05 15:57:13 +000072#ifdef UTF_TRANSLATION_NEEDED
73 Tcl_DString dCol;
74#endif
drh6a535342001-10-19 16:44:56 +000075 if( azCol==0 || (cbData->once && cbData->zArray[0]) ){
76 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0);
77 for(i=0; i<nCol; i++){
78 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", azN[i],
79 TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
80 }
81 cbData->once = 0;
82 }
83 if( azCol!=0 ){
84 if( cbData->zArray[0] ){
drh75897232000-05-29 14:26:00 +000085 for(i=0; i<nCol; i++){
drh6a535342001-10-19 16:44:56 +000086 char *z = azCol[i];
87 if( z==0 ) z = "";
88#ifdef UTF_TRANSLATION_NEEDED
89 Tcl_DStringInit(&dCol);
90 Tcl_ExternalToUtfDString(NULL, z, -1, &dCol);
91 Tcl_SetVar2(cbData->interp, cbData->zArray, azN[i],
92 Tcl_DStringValue(&dCol), 0);
93 Tcl_DStringFree(&dCol);
94#else
95 Tcl_SetVar2(cbData->interp, cbData->zArray, azN[i], z, 0);
96#endif
97 }
98 }else{
99 for(i=0; i<nCol; i++){
100 char *z = azCol[i];
101 if( z==0 ) z = "";
102#ifdef UTF_TRANSLATION_NEEDED
103 Tcl_DStringInit(&dCol);
104 Tcl_ExternalToUtfDString(NULL, z, -1, &dCol);
105 Tcl_SetVar(cbData->interp, azN[i], Tcl_DStringValue(&dCol), 0);
106 Tcl_DStringFree(&dCol);
107#else
108 Tcl_SetVar(cbData->interp, azN[i], z, 0);
109#endif
drh75897232000-05-29 14:26:00 +0000110 }
111 }
drh75897232000-05-29 14:26:00 +0000112 }
drh6d313162000-09-21 13:01:35 +0000113 rc = Tcl_EvalObj(cbData->interp, cbData->pCode);
drh960e8c62001-04-03 16:53:21 +0000114 if( rc==TCL_CONTINUE ) rc = TCL_OK;
115 cbData->tcl_rc = rc;
116 return rc!=TCL_OK;
drh75897232000-05-29 14:26:00 +0000117}
118
119/*
drh6d313162000-09-21 13:01:35 +0000120** This is an alternative callback for database queries. Instead
121** of invoking a TCL script to handle the result, this callback just
122** appends each column of the result to a list. After the query
123** is complete, the list is returned.
124*/
125static int DbEvalCallback2(
126 void *clientData, /* An instance of CallbackData */
127 int nCol, /* Number of columns in the result */
128 char ** azCol, /* Data for each column */
129 char ** azN /* Name for each column */
130){
131 Tcl_Obj *pList = (Tcl_Obj*)clientData;
132 int i;
drh6a535342001-10-19 16:44:56 +0000133 if( azCol==0 ) return 0;
drh6d313162000-09-21 13:01:35 +0000134 for(i=0; i<nCol; i++){
135 Tcl_Obj *pElem;
136 if( azCol[i] && *azCol[i] ){
drh297ecf12001-04-05 15:57:13 +0000137#ifdef UTF_TRANSLATION_NEEDED
138 Tcl_DString dCol;
139 Tcl_DStringInit(&dCol);
140 Tcl_ExternalToUtfDString(NULL, azCol[i], -1, &dCol);
141 pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
142 Tcl_DStringFree(&dCol);
143#else
drh6d313162000-09-21 13:01:35 +0000144 pElem = Tcl_NewStringObj(azCol[i], -1);
drh297ecf12001-04-05 15:57:13 +0000145#endif
drh6d313162000-09-21 13:01:35 +0000146 }else{
147 pElem = Tcl_NewObj();
148 }
149 Tcl_ListObjAppendElement(0, pList, pElem);
150 }
151 return 0;
152}
153
154/*
drh75897232000-05-29 14:26:00 +0000155** Called when the command is deleted.
156*/
157static void DbDeleteCmd(void *db){
drhbec3f402000-08-04 13:49:02 +0000158 SqliteDb *pDb = (SqliteDb*)db;
159 sqlite_close(pDb->db);
160 if( pDb->zBusy ){
161 Tcl_Free(pDb->zBusy);
162 }
163 Tcl_Free((char*)pDb);
164}
165
166/*
167** This routine is called when a database file is locked while trying
168** to execute SQL.
169*/
170static int DbBusyHandler(void *cd, const char *zTable, int nTries){
171 SqliteDb *pDb = (SqliteDb*)cd;
172 int rc;
173 char zVal[30];
174 char *zCmd;
drhbec3f402000-08-04 13:49:02 +0000175 Tcl_DString cmd;
176
177 Tcl_DStringInit(&cmd);
178 Tcl_DStringAppend(&cmd, pDb->zBusy, -1);
179 Tcl_DStringAppendElement(&cmd, zTable);
180 sprintf(zVal, " %d", nTries);
181 Tcl_DStringAppend(&cmd, zVal, -1);
182 zCmd = Tcl_DStringValue(&cmd);
183 rc = Tcl_Eval(pDb->interp, zCmd);
184 Tcl_DStringFree(&cmd);
185 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
186 return 0;
187 }
188 return 1;
drh75897232000-05-29 14:26:00 +0000189}
190
191/*
192** The "sqlite" command below creates a new Tcl command for each
193** connection it opens to an SQLite database. This routine is invoked
194** whenever one of those connection-specific commands is executed
195** in Tcl. For example, if you run Tcl code like this:
196**
197** sqlite db1 "my_database"
198** db1 close
199**
200** The first command opens a connection to the "my_database" database
201** and calls that connection "db1". The second command causes this
202** subroutine to be invoked.
203*/
drh6d313162000-09-21 13:01:35 +0000204static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000205 SqliteDb *pDb = (SqliteDb*)cd;
drh6d313162000-09-21 13:01:35 +0000206 int choice;
207 static char *DB_optStrs[] = {
drh960e8c62001-04-03 16:53:21 +0000208 "busy", "close", "complete", "eval", "timeout", 0
drh6d313162000-09-21 13:01:35 +0000209 };
210 enum DB_opts {
211 DB_BUSY, DB_CLOSE, DB_COMPLETE, DB_EVAL, DB_TIMEOUT
212 };
213
214 if( objc<2 ){
215 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
drh75897232000-05-29 14:26:00 +0000216 return TCL_ERROR;
217 }
drh6d313162000-09-21 13:01:35 +0000218 if( Tcl_GetIndexFromObj(interp, objv[1], DB_optStrs, "option", 0, &choice) ){
219 return TCL_ERROR;
220 }
221
222 switch( (enum DB_opts)choice ){
drh75897232000-05-29 14:26:00 +0000223
drhbec3f402000-08-04 13:49:02 +0000224 /* $db busy ?CALLBACK?
225 **
226 ** Invoke the given callback if an SQL statement attempts to open
227 ** a locked database file.
228 */
drh6d313162000-09-21 13:01:35 +0000229 case DB_BUSY: {
230 if( objc>3 ){
231 Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
drhbec3f402000-08-04 13:49:02 +0000232 return TCL_ERROR;
drh6d313162000-09-21 13:01:35 +0000233 }else if( objc==2 ){
drhbec3f402000-08-04 13:49:02 +0000234 if( pDb->zBusy ){
235 Tcl_AppendResult(interp, pDb->zBusy, 0);
236 }
237 }else{
drh6d313162000-09-21 13:01:35 +0000238 char *zBusy;
239 int len;
drhbec3f402000-08-04 13:49:02 +0000240 if( pDb->zBusy ){
241 Tcl_Free(pDb->zBusy);
drhbec3f402000-08-04 13:49:02 +0000242 }
drh6d313162000-09-21 13:01:35 +0000243 zBusy = Tcl_GetStringFromObj(objv[2], &len);
244 if( zBusy && len>0 ){
245 pDb->zBusy = Tcl_Alloc( len + 1 );
246 strcpy(pDb->zBusy, zBusy);
247 }else{
248 pDb->zBusy = 0;
drhbec3f402000-08-04 13:49:02 +0000249 }
250 if( pDb->zBusy ){
251 pDb->interp = interp;
252 sqlite_busy_handler(pDb->db, DbBusyHandler, pDb);
drh6d313162000-09-21 13:01:35 +0000253 }else{
254 sqlite_busy_handler(pDb->db, 0, 0);
drhbec3f402000-08-04 13:49:02 +0000255 }
256 }
drh6d313162000-09-21 13:01:35 +0000257 break;
258 }
drhbec3f402000-08-04 13:49:02 +0000259
drh75897232000-05-29 14:26:00 +0000260 /* $db close
261 **
262 ** Shutdown the database
263 */
drh6d313162000-09-21 13:01:35 +0000264 case DB_CLOSE: {
265 Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0));
266 break;
267 }
drh75897232000-05-29 14:26:00 +0000268
269 /* $db complete SQL
270 **
271 ** Return TRUE if SQL is a complete SQL statement. Return FALSE if
272 ** additional lines of input are needed. This is similar to the
273 ** built-in "info complete" command of Tcl.
274 */
drh6d313162000-09-21 13:01:35 +0000275 case DB_COMPLETE: {
276 Tcl_Obj *pResult;
277 int isComplete;
278 if( objc!=3 ){
279 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
drh75897232000-05-29 14:26:00 +0000280 return TCL_ERROR;
281 }
drh6d313162000-09-21 13:01:35 +0000282 isComplete = sqlite_complete( Tcl_GetStringFromObj(objv[2], 0) );
283 pResult = Tcl_GetObjResult(interp);
284 Tcl_SetBooleanObj(pResult, isComplete);
285 break;
286 }
drh75897232000-05-29 14:26:00 +0000287
288 /*
289 ** $db eval $sql ?array { ...code... }?
290 **
291 ** The SQL statement in $sql is evaluated. For each row, the values are
drhbec3f402000-08-04 13:49:02 +0000292 ** placed in elements of the array named "array" and ...code... is executed.
drh75897232000-05-29 14:26:00 +0000293 ** If "array" and "code" are omitted, then no callback is every invoked.
294 ** If "array" is an empty string, then the values are placed in variables
295 ** that have the same name as the fields extracted by the query.
296 */
drh6d313162000-09-21 13:01:35 +0000297 case DB_EVAL: {
drh75897232000-05-29 14:26:00 +0000298 CallbackData cbData;
299 char *zErrMsg;
drh6d313162000-09-21 13:01:35 +0000300 char *zSql;
drh75897232000-05-29 14:26:00 +0000301 int rc;
drh297ecf12001-04-05 15:57:13 +0000302#ifdef UTF_TRANSLATION_NEEDED
303 Tcl_DString dSql;
304#endif
drh75897232000-05-29 14:26:00 +0000305
drh6d313162000-09-21 13:01:35 +0000306 if( objc!=5 && objc!=3 ){
307 Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?");
drh75897232000-05-29 14:26:00 +0000308 return TCL_ERROR;
309 }
drhbec3f402000-08-04 13:49:02 +0000310 pDb->interp = interp;
drh6d313162000-09-21 13:01:35 +0000311 zSql = Tcl_GetStringFromObj(objv[2], 0);
drh297ecf12001-04-05 15:57:13 +0000312#ifdef UTF_TRANSLATION_NEEDED
313 Tcl_DStringInit(&dSql);
314 Tcl_UtfToExternalDString(NULL, zSql, -1, &dSql);
315 zSql = Tcl_DStringValue(&dSql);
316#endif
drh6d313162000-09-21 13:01:35 +0000317 Tcl_IncrRefCount(objv[2]);
318 if( objc==5 ){
drh75897232000-05-29 14:26:00 +0000319 cbData.interp = interp;
drhdcc581c2000-05-30 13:44:19 +0000320 cbData.once = 1;
drh6d313162000-09-21 13:01:35 +0000321 cbData.zArray = Tcl_GetStringFromObj(objv[3], 0);
322 cbData.pCode = objv[4];
drh960e8c62001-04-03 16:53:21 +0000323 cbData.tcl_rc = TCL_OK;
drh75897232000-05-29 14:26:00 +0000324 zErrMsg = 0;
drh6d313162000-09-21 13:01:35 +0000325 Tcl_IncrRefCount(objv[3]);
326 Tcl_IncrRefCount(objv[4]);
327 rc = sqlite_exec(pDb->db, zSql, DbEvalCallback, &cbData, &zErrMsg);
328 Tcl_DecrRefCount(objv[4]);
329 Tcl_DecrRefCount(objv[3]);
drh960e8c62001-04-03 16:53:21 +0000330 if( cbData.tcl_rc==TCL_BREAK ){ cbData.tcl_rc = TCL_OK; }
drh75897232000-05-29 14:26:00 +0000331 }else{
drh6d313162000-09-21 13:01:35 +0000332 Tcl_Obj *pList = Tcl_NewObj();
drh960e8c62001-04-03 16:53:21 +0000333 cbData.tcl_rc = TCL_OK;
drh6d313162000-09-21 13:01:35 +0000334 rc = sqlite_exec(pDb->db, zSql, DbEvalCallback2, pList, &zErrMsg);
335 Tcl_SetObjResult(interp, pList);
drh75897232000-05-29 14:26:00 +0000336 }
337 if( zErrMsg ){
338 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
339 free(zErrMsg);
drh960e8c62001-04-03 16:53:21 +0000340 rc = TCL_ERROR;
341 }else{
342 rc = cbData.tcl_rc;
drh75897232000-05-29 14:26:00 +0000343 }
drh6d313162000-09-21 13:01:35 +0000344 Tcl_DecrRefCount(objv[2]);
drh297ecf12001-04-05 15:57:13 +0000345#ifdef UTF_TRANSLATION_NEEDED
346 Tcl_DStringFree(&dSql);
347#endif
drh75897232000-05-29 14:26:00 +0000348 return rc;
drh6d313162000-09-21 13:01:35 +0000349 }
drhbec3f402000-08-04 13:49:02 +0000350
351 /*
352 ** $db timeout MILLESECONDS
353 **
354 ** Delay for the number of milliseconds specified when a file is locked.
355 */
drh6d313162000-09-21 13:01:35 +0000356 case DB_TIMEOUT: {
drhbec3f402000-08-04 13:49:02 +0000357 int ms;
drh6d313162000-09-21 13:01:35 +0000358 if( objc!=3 ){
359 Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
drhbec3f402000-08-04 13:49:02 +0000360 return TCL_ERROR;
361 }
drh6d313162000-09-21 13:01:35 +0000362 if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
drhbec3f402000-08-04 13:49:02 +0000363 sqlite_busy_timeout(pDb->db, ms);
drh6d313162000-09-21 13:01:35 +0000364 break;
drh75897232000-05-29 14:26:00 +0000365 }
drh6d313162000-09-21 13:01:35 +0000366 } /* End of the SWITCH statement */
drh75897232000-05-29 14:26:00 +0000367 return TCL_OK;
368}
369
370/*
371** sqlite DBNAME FILENAME ?MODE?
372**
373** This is the main Tcl command. When the "sqlite" Tcl command is
374** invoked, this routine runs to process that command.
375**
376** The first argument, DBNAME, is an arbitrary name for a new
377** database connection. This command creates a new command named
378** DBNAME that is used to control that connection. The database
379** connection is deleted when the DBNAME command is deleted.
380**
381** The second argument is the name of the directory that contains
382** the sqlite database that is to be accessed.
drhfbc3eab2001-04-06 16:13:42 +0000383**
384** For testing purposes, we also support the following:
385**
386** sqlite -encoding
387**
388** Return the encoding used by LIKE and GLOB operators. Choices
389** are UTF-8 and iso8859.
390**
391** sqlite -tcl-uses-utf
392**
393** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if
394** not. Used by tests to make sure the library was compiled
395** correctly.
drh75897232000-05-29 14:26:00 +0000396*/
397static int DbMain(void *cd, Tcl_Interp *interp, int argc, char **argv){
398 int mode;
drhbec3f402000-08-04 13:49:02 +0000399 SqliteDb *p;
drh75897232000-05-29 14:26:00 +0000400 char *zErrMsg;
drhfbc3eab2001-04-06 16:13:42 +0000401 if( argc==2 ){
402 if( strcmp(argv[1],"-encoding")==0 ){
403 Tcl_AppendResult(interp,sqlite_encoding,0);
404 return TCL_OK;
405 }
406 if( strcmp(argv[1],"-tcl-uses-utf")==0 ){
407#ifdef TCL_UTF_MAX
408 Tcl_AppendResult(interp,"1",0);
409#else
410 Tcl_AppendResult(interp,"0",0);
411#endif
412 return TCL_OK;
413 }
414 }
drh75897232000-05-29 14:26:00 +0000415 if( argc!=3 && argc!=4 ){
416 Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0],
417 " HANDLE FILENAME ?MODE?\"", 0);
418 return TCL_ERROR;
419 }
420 if( argc==3 ){
drh58b95762000-06-02 01:17:37 +0000421 mode = 0666;
drh75897232000-05-29 14:26:00 +0000422 }else if( Tcl_GetInt(interp, argv[3], &mode)!=TCL_OK ){
423 return TCL_ERROR;
424 }
425 zErrMsg = 0;
drh4cdc9e82000-08-04 14:56:24 +0000426 p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
drh75897232000-05-29 14:26:00 +0000427 if( p==0 ){
drhbec3f402000-08-04 13:49:02 +0000428 Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
429 return TCL_ERROR;
430 }
431 memset(p, 0, sizeof(*p));
432 p->db = sqlite_open(argv[2], mode, &zErrMsg);
433 if( p->db==0 ){
drh75897232000-05-29 14:26:00 +0000434 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
drhbec3f402000-08-04 13:49:02 +0000435 Tcl_Free((char*)p);
drh75897232000-05-29 14:26:00 +0000436 free(zErrMsg);
437 return TCL_ERROR;
438 }
drh6d313162000-09-21 13:01:35 +0000439 Tcl_CreateObjCommand(interp, argv[1], DbObjCmd, (char*)p, DbDeleteCmd);
drh75897232000-05-29 14:26:00 +0000440 return TCL_OK;
441}
442
443/*
drh90ca9752001-09-28 17:47:14 +0000444** Provide a dummy Tcl_InitStubs if we are using this as a static
445** library.
446*/
447#ifndef USE_TCL_STUBS
448# undef Tcl_InitStubs
449# define Tcl_InitStubs(a,b,c)
450#endif
451
452/*
drh75897232000-05-29 14:26:00 +0000453** Initialize this module.
454**
455** This Tcl module contains only a single new Tcl command named "sqlite".
456** (Hence there is no namespace. There is no point in using a namespace
457** if the extension only supplies one new name!) The "sqlite" command is
458** used to open a new SQLite database. See the DbMain() routine above
459** for additional information.
460*/
461int Sqlite_Init(Tcl_Interp *interp){
drh90ca9752001-09-28 17:47:14 +0000462 Tcl_InitStubs(interp, "8.0", 0);
463 Tcl_CreateCommand(interp, "sqlite", DbMain, 0, 0);
464 Tcl_PkgProvide(interp, "sqlite", "1.0");
465 return TCL_OK;
466}
467int Tclsqlite_Init(Tcl_Interp *interp){
468 Tcl_InitStubs(interp, "8.0", 0);
drh75897232000-05-29 14:26:00 +0000469 Tcl_CreateCommand(interp, "sqlite", DbMain, 0, 0);
drh167a4b12000-08-17 09:49:59 +0000470 Tcl_PkgProvide(interp, "sqlite", "1.0");
drh75897232000-05-29 14:26:00 +0000471 return TCL_OK;
472}
473int Sqlite_SafeInit(Tcl_Interp *interp){
474 return TCL_OK;
475}
drh90ca9752001-09-28 17:47:14 +0000476int Tclsqlite_SafeInit(Tcl_Interp *interp){
477 return TCL_OK;
478}
drh75897232000-05-29 14:26:00 +0000479
drh3cebbde2000-10-19 14:59:27 +0000480#if 0
drh75897232000-05-29 14:26:00 +0000481/*
482** If compiled using mktclapp, this routine runs to initialize
483** everything.
484*/
485int Et_AppInit(Tcl_Interp *interp){
486 return Sqlite_Init(interp);
487}
drh3cebbde2000-10-19 14:59:27 +0000488#endif
drh348784e2000-05-29 20:41:49 +0000489
490/*
491** If the macro TCLSH is defined and is one, then put in code for the
492** "main" routine that will initialize Tcl.
493*/
494#if defined(TCLSH) && TCLSH==1
495static char zMainloop[] =
496 "set line {}\n"
497 "while {![eof stdin]} {\n"
498 "if {$line!=\"\"} {\n"
499 "puts -nonewline \"> \"\n"
500 "} else {\n"
501 "puts -nonewline \"% \"\n"
502 "}\n"
503 "flush stdout\n"
504 "append line [gets stdin]\n"
505 "if {[info complete $line]} {\n"
506 "if {[catch {uplevel #0 $line} result]} {\n"
507 "puts stderr \"Error: $result\"\n"
508 "} elseif {$result!=\"\"} {\n"
509 "puts $result\n"
510 "}\n"
511 "set line {}\n"
512 "} else {\n"
513 "append line \\n\n"
514 "}\n"
515 "}\n"
516;
517
518#define TCLSH_MAIN main /* Needed to fake out mktclapp */
519int TCLSH_MAIN(int argc, char **argv){
520 Tcl_Interp *interp;
drh297ecf12001-04-05 15:57:13 +0000521 Tcl_FindExecutable(argv[0]);
drh348784e2000-05-29 20:41:49 +0000522 interp = Tcl_CreateInterp();
523 Sqlite_Init(interp);
drhd9b02572001-04-15 00:37:09 +0000524#ifdef SQLITE_TEST
drhd1bf3512001-04-07 15:24:33 +0000525 {
526 extern int Sqlitetest1_Init(Tcl_Interp*);
drh5c4d9702001-08-20 00:33:58 +0000527 extern int Sqlitetest2_Init(Tcl_Interp*);
528 extern int Sqlitetest3_Init(Tcl_Interp*);
drhefc251d2001-07-01 22:12:01 +0000529 extern int Md5_Init(Tcl_Interp*);
drhd1bf3512001-04-07 15:24:33 +0000530 Sqlitetest1_Init(interp);
drh5c4d9702001-08-20 00:33:58 +0000531 Sqlitetest2_Init(interp);
532 Sqlitetest3_Init(interp);
drhefc251d2001-07-01 22:12:01 +0000533 Md5_Init(interp);
drhd1bf3512001-04-07 15:24:33 +0000534 }
535#endif
drh348784e2000-05-29 20:41:49 +0000536 if( argc>=2 ){
537 int i;
538 Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
539 Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
540 for(i=2; i<argc; i++){
541 Tcl_SetVar(interp, "argv", argv[i],
542 TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
543 }
544 if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
drhc61053b2000-06-04 12:58:36 +0000545 char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
546 if( zInfo==0 ) zInfo = interp->result;
547 fprintf(stderr,"%s: %s\n", *argv, zInfo);
drh348784e2000-05-29 20:41:49 +0000548 return 1;
549 }
550 }else{
551 Tcl_GlobalEval(interp, zMainloop);
552 }
553 return 0;
554}
555#endif /* TCLSH */
drh6d313162000-09-21 13:01:35 +0000556
557#endif /* !defined(NO_TCL) */