xref: /sqlite-3.40.0/src/tclsqlite.c (revision 3cebbde3)
175897234Sdrh /*
275897234Sdrh ** Copyright (c) 1999, 2000 D. Richard Hipp
375897234Sdrh **
475897234Sdrh ** This program is free software; you can redistribute it and/or
575897234Sdrh ** modify it under the terms of the GNU General Public
675897234Sdrh ** License as published by the Free Software Foundation; either
775897234Sdrh ** version 2 of the License, or (at your option) any later version.
875897234Sdrh **
975897234Sdrh ** This program is distributed in the hope that it will be useful,
1075897234Sdrh ** but WITHOUT ANY WARRANTY; without even the implied warranty of
1175897234Sdrh ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1275897234Sdrh ** General Public License for more details.
1375897234Sdrh **
1475897234Sdrh ** You should have received a copy of the GNU General Public
1575897234Sdrh ** License along with this library; if not, write to the
1675897234Sdrh ** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
1775897234Sdrh ** Boston, MA  02111-1307, USA.
1875897234Sdrh **
1975897234Sdrh ** Author contact information:
2075897234Sdrh **   [email protected]
2175897234Sdrh **   http://www.hwaci.com/drh/
2275897234Sdrh **
2375897234Sdrh *************************************************************************
2475897234Sdrh ** A TCL Interface to SQLite
2575897234Sdrh **
26*3cebbde3Sdrh ** $Id: tclsqlite.c,v 1.12 2000/10/19 14:59:27 drh Exp $
2775897234Sdrh */
286d31316cSdrh #ifndef NO_TCL     /* Omit this whole file if TCL is unavailable */
296d31316cSdrh 
3075897234Sdrh #include "sqlite.h"
3175897234Sdrh #include <tcl.h>
3275897234Sdrh #include <stdlib.h>
3375897234Sdrh #include <string.h>
3475897234Sdrh 
3575897234Sdrh /*
36bec3f402Sdrh ** There is one instance of this structure for each SQLite database
37bec3f402Sdrh ** that has been opened by the SQLite TCL interface.
38bec3f402Sdrh */
39bec3f402Sdrh typedef struct SqliteDb SqliteDb;
40bec3f402Sdrh struct SqliteDb {
41bec3f402Sdrh   sqlite *db;           /* The "real" database structure */
42bec3f402Sdrh   Tcl_Interp *interp;   /* The interpreter used for this database */
436d31316cSdrh   char *zBusy;          /* The busy callback routine */
44bec3f402Sdrh };
45bec3f402Sdrh 
46bec3f402Sdrh /*
4775897234Sdrh ** An instance of this structure passes information thru the sqlite
4875897234Sdrh ** logic from the original TCL command into the callback routine.
4975897234Sdrh */
5075897234Sdrh typedef struct CallbackData CallbackData;
5175897234Sdrh struct CallbackData {
5275897234Sdrh   Tcl_Interp *interp;       /* The TCL interpreter */
5375897234Sdrh   char *zArray;             /* The array into which data is written */
546d31316cSdrh   Tcl_Obj *pCode;           /* The code to execute for each row */
5575897234Sdrh   int once;                 /* Set only for the first invocation of callback */
5675897234Sdrh };
5775897234Sdrh 
5875897234Sdrh /*
5975897234Sdrh ** Called for each row of the result.
6075897234Sdrh */
6175897234Sdrh static int DbEvalCallback(
6275897234Sdrh   void *clientData,      /* An instance of CallbackData */
6375897234Sdrh   int nCol,              /* Number of columns in the result */
6475897234Sdrh   char ** azCol,         /* Data for each column */
6575897234Sdrh   char ** azN            /* Name for each column */
6675897234Sdrh ){
6775897234Sdrh   CallbackData *cbData = (CallbackData*)clientData;
6875897234Sdrh   int i, rc;
6975897234Sdrh   if( cbData->zArray[0] ){
7075897234Sdrh     if( cbData->once ){
719b0d0a8bSdrh       Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0);
7275897234Sdrh       for(i=0; i<nCol; i++){
7375897234Sdrh         Tcl_SetVar2(cbData->interp, cbData->zArray, "*", azN[i],
7475897234Sdrh            TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
7575897234Sdrh       }
7675897234Sdrh     }
7775897234Sdrh     for(i=0; i<nCol; i++){
78c61053b7Sdrh       char *z = azCol[i];
79c61053b7Sdrh       if( z==0 ) z = "";
80c61053b7Sdrh       Tcl_SetVar2(cbData->interp, cbData->zArray, azN[i], z, 0);
8175897234Sdrh     }
8275897234Sdrh   }else{
8375897234Sdrh     for(i=0; i<nCol; i++){
84c61053b7Sdrh       char *z = azCol[i];
85c61053b7Sdrh       if( z==0 ) z = "";
86c61053b7Sdrh       Tcl_SetVar(cbData->interp, azN[i], z, 0);
8775897234Sdrh     }
8875897234Sdrh   }
8975897234Sdrh   cbData->once = 0;
906d31316cSdrh   rc = Tcl_EvalObj(cbData->interp, cbData->pCode);
919b0d0a8bSdrh   return rc!=TCL_OK && rc!=TCL_CONTINUE;
9275897234Sdrh }
9375897234Sdrh 
9475897234Sdrh /*
956d31316cSdrh ** This is an alternative callback for database queries.  Instead
966d31316cSdrh ** of invoking a TCL script to handle the result, this callback just
976d31316cSdrh ** appends each column of the result to a list.  After the query
986d31316cSdrh ** is complete, the list is returned.
996d31316cSdrh */
1006d31316cSdrh static int DbEvalCallback2(
1016d31316cSdrh   void *clientData,      /* An instance of CallbackData */
1026d31316cSdrh   int nCol,              /* Number of columns in the result */
1036d31316cSdrh   char ** azCol,         /* Data for each column */
1046d31316cSdrh   char ** azN            /* Name for each column */
1056d31316cSdrh ){
1066d31316cSdrh   Tcl_Obj *pList = (Tcl_Obj*)clientData;
1076d31316cSdrh   int i;
1086d31316cSdrh   for(i=0; i<nCol; i++){
1096d31316cSdrh     Tcl_Obj *pElem;
1106d31316cSdrh     if( azCol[i] && *azCol[i] ){
1116d31316cSdrh       pElem = Tcl_NewStringObj(azCol[i], -1);
1126d31316cSdrh     }else{
1136d31316cSdrh       pElem = Tcl_NewObj();
1146d31316cSdrh     }
1156d31316cSdrh     Tcl_ListObjAppendElement(0, pList, pElem);
1166d31316cSdrh   }
1176d31316cSdrh   return 0;
1186d31316cSdrh }
1196d31316cSdrh 
1206d31316cSdrh /*
12175897234Sdrh ** Called when the command is deleted.
12275897234Sdrh */
12375897234Sdrh static void DbDeleteCmd(void *db){
124bec3f402Sdrh   SqliteDb *pDb = (SqliteDb*)db;
125bec3f402Sdrh   sqlite_close(pDb->db);
126bec3f402Sdrh   if( pDb->zBusy ){
127bec3f402Sdrh     Tcl_Free(pDb->zBusy);
128bec3f402Sdrh   }
129bec3f402Sdrh   Tcl_Free((char*)pDb);
130bec3f402Sdrh }
131bec3f402Sdrh 
132bec3f402Sdrh /*
133bec3f402Sdrh ** This routine is called when a database file is locked while trying
134bec3f402Sdrh ** to execute SQL.
135bec3f402Sdrh */
136bec3f402Sdrh static int DbBusyHandler(void *cd, const char *zTable, int nTries){
137bec3f402Sdrh   SqliteDb *pDb = (SqliteDb*)cd;
138bec3f402Sdrh   int rc;
139bec3f402Sdrh   char zVal[30];
140bec3f402Sdrh   char *zCmd;
141bec3f402Sdrh   Tcl_DString cmd;
142bec3f402Sdrh 
143bec3f402Sdrh   Tcl_DStringInit(&cmd);
144bec3f402Sdrh   Tcl_DStringAppend(&cmd, pDb->zBusy, -1);
145bec3f402Sdrh   Tcl_DStringAppendElement(&cmd, zTable);
146bec3f402Sdrh   sprintf(zVal, " %d", nTries);
147bec3f402Sdrh   Tcl_DStringAppend(&cmd, zVal, -1);
148bec3f402Sdrh   zCmd = Tcl_DStringValue(&cmd);
149bec3f402Sdrh   rc = Tcl_Eval(pDb->interp, zCmd);
150bec3f402Sdrh   Tcl_DStringFree(&cmd);
151bec3f402Sdrh   if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
152bec3f402Sdrh     return 0;
153bec3f402Sdrh   }
154bec3f402Sdrh   return 1;
15575897234Sdrh }
15675897234Sdrh 
15775897234Sdrh /*
15875897234Sdrh ** The "sqlite" command below creates a new Tcl command for each
15975897234Sdrh ** connection it opens to an SQLite database.  This routine is invoked
16075897234Sdrh ** whenever one of those connection-specific commands is executed
16175897234Sdrh ** in Tcl.  For example, if you run Tcl code like this:
16275897234Sdrh **
16375897234Sdrh **       sqlite db1  "my_database"
16475897234Sdrh **       db1 close
16575897234Sdrh **
16675897234Sdrh ** The first command opens a connection to the "my_database" database
16775897234Sdrh ** and calls that connection "db1".  The second command causes this
16875897234Sdrh ** subroutine to be invoked.
16975897234Sdrh */
1706d31316cSdrh static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
171bec3f402Sdrh   SqliteDb *pDb = (SqliteDb*)cd;
1726d31316cSdrh   int choice;
1736d31316cSdrh   static char *DB_optStrs[] = {
1746d31316cSdrh      "busy",   "close",  "complete",  "eval",  "timeout"
1756d31316cSdrh   };
1766d31316cSdrh   enum DB_opts {
1776d31316cSdrh      DB_BUSY,  DB_CLOSE, DB_COMPLETE, DB_EVAL, DB_TIMEOUT
1786d31316cSdrh   };
1796d31316cSdrh 
1806d31316cSdrh   if( objc<2 ){
1816d31316cSdrh     Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
18275897234Sdrh     return TCL_ERROR;
18375897234Sdrh   }
1846d31316cSdrh   if( Tcl_GetIndexFromObj(interp, objv[1], DB_optStrs, "option", 0, &choice) ){
1856d31316cSdrh     return TCL_ERROR;
1866d31316cSdrh   }
1876d31316cSdrh 
1886d31316cSdrh   switch( (enum DB_opts)choice ){
18975897234Sdrh 
190bec3f402Sdrh   /*    $db busy ?CALLBACK?
191bec3f402Sdrh   **
192bec3f402Sdrh   ** Invoke the given callback if an SQL statement attempts to open
193bec3f402Sdrh   ** a locked database file.
194bec3f402Sdrh   */
1956d31316cSdrh   case DB_BUSY: {
1966d31316cSdrh     if( objc>3 ){
1976d31316cSdrh       Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
198bec3f402Sdrh       return TCL_ERROR;
1996d31316cSdrh     }else if( objc==2 ){
200bec3f402Sdrh       if( pDb->zBusy ){
201bec3f402Sdrh         Tcl_AppendResult(interp, pDb->zBusy, 0);
202bec3f402Sdrh       }
203bec3f402Sdrh     }else{
2046d31316cSdrh       char *zBusy;
2056d31316cSdrh       int len;
206bec3f402Sdrh       if( pDb->zBusy ){
207bec3f402Sdrh         Tcl_Free(pDb->zBusy);
2086d31316cSdrh       }
2096d31316cSdrh       zBusy = Tcl_GetStringFromObj(objv[2], &len);
2106d31316cSdrh       if( zBusy && len>0 ){
2116d31316cSdrh         pDb->zBusy = Tcl_Alloc( len + 1 );
2126d31316cSdrh         strcpy(pDb->zBusy, zBusy);
2136d31316cSdrh       }else{
214bec3f402Sdrh         pDb->zBusy = 0;
215bec3f402Sdrh       }
216bec3f402Sdrh       if( pDb->zBusy ){
217bec3f402Sdrh         pDb->interp = interp;
218bec3f402Sdrh         sqlite_busy_handler(pDb->db, DbBusyHandler, pDb);
2196d31316cSdrh       }else{
2206d31316cSdrh         sqlite_busy_handler(pDb->db, 0, 0);
221bec3f402Sdrh       }
222bec3f402Sdrh     }
2236d31316cSdrh     break;
2246d31316cSdrh   }
225bec3f402Sdrh 
22675897234Sdrh   /*    $db close
22775897234Sdrh   **
22875897234Sdrh   ** Shutdown the database
22975897234Sdrh   */
2306d31316cSdrh   case DB_CLOSE: {
2316d31316cSdrh     Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0));
2326d31316cSdrh     break;
2336d31316cSdrh   }
23475897234Sdrh 
23575897234Sdrh   /*    $db complete SQL
23675897234Sdrh   **
23775897234Sdrh   ** Return TRUE if SQL is a complete SQL statement.  Return FALSE if
23875897234Sdrh   ** additional lines of input are needed.  This is similar to the
23975897234Sdrh   ** built-in "info complete" command of Tcl.
24075897234Sdrh   */
2416d31316cSdrh   case DB_COMPLETE: {
2426d31316cSdrh     Tcl_Obj *pResult;
2436d31316cSdrh     int isComplete;
2446d31316cSdrh     if( objc!=3 ){
2456d31316cSdrh       Tcl_WrongNumArgs(interp, 2, objv, "SQL");
24675897234Sdrh       return TCL_ERROR;
24775897234Sdrh     }
2486d31316cSdrh     isComplete = sqlite_complete( Tcl_GetStringFromObj(objv[2], 0) );
2496d31316cSdrh     pResult = Tcl_GetObjResult(interp);
2506d31316cSdrh     Tcl_SetBooleanObj(pResult, isComplete);
2516d31316cSdrh     break;
2526d31316cSdrh   }
25375897234Sdrh 
25475897234Sdrh   /*
25575897234Sdrh   **    $db eval $sql ?array {  ...code... }?
25675897234Sdrh   **
25775897234Sdrh   ** The SQL statement in $sql is evaluated.  For each row, the values are
258bec3f402Sdrh   ** placed in elements of the array named "array" and ...code... is executed.
25975897234Sdrh   ** If "array" and "code" are omitted, then no callback is every invoked.
26075897234Sdrh   ** If "array" is an empty string, then the values are placed in variables
26175897234Sdrh   ** that have the same name as the fields extracted by the query.
26275897234Sdrh   */
2636d31316cSdrh   case DB_EVAL: {
26475897234Sdrh     CallbackData cbData;
26575897234Sdrh     char *zErrMsg;
2666d31316cSdrh     char *zSql;
26775897234Sdrh     int rc;
26875897234Sdrh 
2696d31316cSdrh     if( objc!=5 && objc!=3 ){
2706d31316cSdrh       Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?");
27175897234Sdrh       return TCL_ERROR;
27275897234Sdrh     }
273bec3f402Sdrh     pDb->interp = interp;
2746d31316cSdrh     zSql = Tcl_GetStringFromObj(objv[2], 0);
2756d31316cSdrh     Tcl_IncrRefCount(objv[2]);
2766d31316cSdrh     if( objc==5 ){
27775897234Sdrh       cbData.interp = interp;
278dcc581ccSdrh       cbData.once = 1;
2796d31316cSdrh       cbData.zArray = Tcl_GetStringFromObj(objv[3], 0);
2806d31316cSdrh       cbData.pCode = objv[4];
28175897234Sdrh       zErrMsg = 0;
2826d31316cSdrh       Tcl_IncrRefCount(objv[3]);
2836d31316cSdrh       Tcl_IncrRefCount(objv[4]);
2846d31316cSdrh       rc = sqlite_exec(pDb->db, zSql, DbEvalCallback, &cbData, &zErrMsg);
2856d31316cSdrh       Tcl_DecrRefCount(objv[4]);
2866d31316cSdrh       Tcl_DecrRefCount(objv[3]);
28775897234Sdrh     }else{
2886d31316cSdrh       Tcl_Obj *pList = Tcl_NewObj();
2896d31316cSdrh       rc = sqlite_exec(pDb->db, zSql, DbEvalCallback2, pList, &zErrMsg);
2906d31316cSdrh       Tcl_SetObjResult(interp, pList);
29175897234Sdrh     }
29275897234Sdrh     if( zErrMsg ){
29375897234Sdrh       Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
29475897234Sdrh       free(zErrMsg);
29575897234Sdrh     }
2966d31316cSdrh     Tcl_DecrRefCount(objv[2]);
29775897234Sdrh     return rc;
2986d31316cSdrh   }
299bec3f402Sdrh 
300bec3f402Sdrh   /*
301bec3f402Sdrh   **     $db timeout MILLESECONDS
302bec3f402Sdrh   **
303bec3f402Sdrh   ** Delay for the number of milliseconds specified when a file is locked.
304bec3f402Sdrh   */
3056d31316cSdrh   case DB_TIMEOUT: {
306bec3f402Sdrh     int ms;
3076d31316cSdrh     if( objc!=3 ){
3086d31316cSdrh       Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
309bec3f402Sdrh       return TCL_ERROR;
31075897234Sdrh     }
3116d31316cSdrh     if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
312bec3f402Sdrh     sqlite_busy_timeout(pDb->db, ms);
3136d31316cSdrh     break;
31475897234Sdrh   }
3156d31316cSdrh   } /* End of the SWITCH statement */
31675897234Sdrh   return TCL_OK;
31775897234Sdrh }
31875897234Sdrh 
31975897234Sdrh /*
32075897234Sdrh **   sqlite DBNAME FILENAME ?MODE?
32175897234Sdrh **
32275897234Sdrh ** This is the main Tcl command.  When the "sqlite" Tcl command is
32375897234Sdrh ** invoked, this routine runs to process that command.
32475897234Sdrh **
32575897234Sdrh ** The first argument, DBNAME, is an arbitrary name for a new
32675897234Sdrh ** database connection.  This command creates a new command named
32775897234Sdrh ** DBNAME that is used to control that connection.  The database
32875897234Sdrh ** connection is deleted when the DBNAME command is deleted.
32975897234Sdrh **
33075897234Sdrh ** The second argument is the name of the directory that contains
33175897234Sdrh ** the sqlite database that is to be accessed.
33275897234Sdrh */
33375897234Sdrh static int DbMain(void *cd, Tcl_Interp *interp, int argc, char **argv){
33475897234Sdrh   int mode;
335bec3f402Sdrh   SqliteDb *p;
33675897234Sdrh   char *zErrMsg;
33775897234Sdrh   if( argc!=3 && argc!=4 ){
33875897234Sdrh     Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0],
33975897234Sdrh        " HANDLE FILENAME ?MODE?\"", 0);
34075897234Sdrh     return TCL_ERROR;
34175897234Sdrh   }
34275897234Sdrh   if( argc==3 ){
34358b9576bSdrh     mode = 0666;
34475897234Sdrh   }else if( Tcl_GetInt(interp, argv[3], &mode)!=TCL_OK ){
34575897234Sdrh     return TCL_ERROR;
34675897234Sdrh   }
34775897234Sdrh   zErrMsg = 0;
3484cdc9e84Sdrh   p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
34975897234Sdrh   if( p==0 ){
350bec3f402Sdrh     Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
351bec3f402Sdrh     return TCL_ERROR;
352bec3f402Sdrh   }
353bec3f402Sdrh   memset(p, 0, sizeof(*p));
354bec3f402Sdrh   p->db = sqlite_open(argv[2], mode, &zErrMsg);
355bec3f402Sdrh   if( p->db==0 ){
35675897234Sdrh     Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
357bec3f402Sdrh     Tcl_Free((char*)p);
35875897234Sdrh     free(zErrMsg);
35975897234Sdrh     return TCL_ERROR;
36075897234Sdrh   }
3616d31316cSdrh   Tcl_CreateObjCommand(interp, argv[1], DbObjCmd, (char*)p, DbDeleteCmd);
36275897234Sdrh   return TCL_OK;
36375897234Sdrh }
36475897234Sdrh 
36575897234Sdrh /*
36675897234Sdrh ** Initialize this module.
36775897234Sdrh **
36875897234Sdrh ** This Tcl module contains only a single new Tcl command named "sqlite".
36975897234Sdrh ** (Hence there is no namespace.  There is no point in using a namespace
37075897234Sdrh ** if the extension only supplies one new name!)  The "sqlite" command is
37175897234Sdrh ** used to open a new SQLite database.  See the DbMain() routine above
37275897234Sdrh ** for additional information.
37375897234Sdrh */
37475897234Sdrh int Sqlite_Init(Tcl_Interp *interp){
37575897234Sdrh   Tcl_CreateCommand(interp, "sqlite", DbMain, 0, 0);
376167a4b1cSdrh   Tcl_PkgProvide(interp, "sqlite", "1.0");
37775897234Sdrh   return TCL_OK;
37875897234Sdrh }
37975897234Sdrh int Sqlite_SafeInit(Tcl_Interp *interp){
38075897234Sdrh   return TCL_OK;
38175897234Sdrh }
38275897234Sdrh 
383*3cebbde3Sdrh #if 0
38475897234Sdrh /*
38575897234Sdrh ** If compiled using mktclapp, this routine runs to initialize
38675897234Sdrh ** everything.
38775897234Sdrh */
38875897234Sdrh int Et_AppInit(Tcl_Interp *interp){
38975897234Sdrh   return Sqlite_Init(interp);
39075897234Sdrh }
391*3cebbde3Sdrh #endif
392348784efSdrh 
393348784efSdrh /*
394348784efSdrh ** If the macro TCLSH is defined and is one, then put in code for the
395348784efSdrh ** "main" routine that will initialize Tcl.
396348784efSdrh */
397348784efSdrh #if defined(TCLSH) && TCLSH==1
398348784efSdrh static char zMainloop[] =
399348784efSdrh   "set line {}\n"
400348784efSdrh   "while {![eof stdin]} {\n"
401348784efSdrh     "if {$line!=\"\"} {\n"
402348784efSdrh       "puts -nonewline \"> \"\n"
403348784efSdrh     "} else {\n"
404348784efSdrh       "puts -nonewline \"% \"\n"
405348784efSdrh     "}\n"
406348784efSdrh     "flush stdout\n"
407348784efSdrh     "append line [gets stdin]\n"
408348784efSdrh     "if {[info complete $line]} {\n"
409348784efSdrh       "if {[catch {uplevel #0 $line} result]} {\n"
410348784efSdrh         "puts stderr \"Error: $result\"\n"
411348784efSdrh       "} elseif {$result!=\"\"} {\n"
412348784efSdrh         "puts $result\n"
413348784efSdrh       "}\n"
414348784efSdrh       "set line {}\n"
415348784efSdrh     "} else {\n"
416348784efSdrh       "append line \\n\n"
417348784efSdrh     "}\n"
418348784efSdrh   "}\n"
419348784efSdrh ;
420348784efSdrh 
421348784efSdrh #define TCLSH_MAIN main   /* Needed to fake out mktclapp */
422348784efSdrh int TCLSH_MAIN(int argc, char **argv){
423348784efSdrh   Tcl_Interp *interp;
424348784efSdrh   interp = Tcl_CreateInterp();
425348784efSdrh   Sqlite_Init(interp);
426348784efSdrh   if( argc>=2 ){
427348784efSdrh     int i;
428348784efSdrh     Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
429348784efSdrh     Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
430348784efSdrh     for(i=2; i<argc; i++){
431348784efSdrh       Tcl_SetVar(interp, "argv", argv[i],
432348784efSdrh           TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
433348784efSdrh     }
434348784efSdrh     if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
435c61053b7Sdrh       char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
436c61053b7Sdrh       if( zInfo==0 ) zInfo = interp->result;
437c61053b7Sdrh       fprintf(stderr,"%s: %s\n", *argv, zInfo);
438348784efSdrh       return 1;
439348784efSdrh     }
440348784efSdrh   }else{
441348784efSdrh     Tcl_GlobalEval(interp, zMainloop);
442348784efSdrh   }
443348784efSdrh   return 0;
444348784efSdrh }
445348784efSdrh #endif /* TCLSH */
4466d31316cSdrh 
4476d31316cSdrh #endif /* !defined(NO_TCL) */
448