[BACK]Return to shell.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / sqlite3

Diff for /src/usr.bin/sqlite3/Attic/shell.c between version 1.1.1.1 and 1.1.1.2

version 1.1.1.1, 2012/04/14 13:22:40 version 1.1.1.2, 2012/05/22 09:18:29
Line 68 
Line 68 
 # include <io.h>  # include <io.h>
 #define isatty(h) _isatty(h)  #define isatty(h) _isatty(h)
 #define access(f,m) _access((f),(m))  #define access(f,m) _access((f),(m))
   #define popen(a,b) _popen((a),(b))
   #define pclose(x) _pclose(x)
 #else  #else
 /* Make sure isatty() has a prototype.  /* Make sure isatty() has a prototype.
 */  */
Line 419 
Line 421 
   int statsOn;           /* True to display memory stats before each finalize */    int statsOn;           /* True to display memory stats before each finalize */
   int cnt;               /* Number of records displayed so far */    int cnt;               /* Number of records displayed so far */
   FILE *out;             /* Write results here */    FILE *out;             /* Write results here */
     FILE *traceOut;        /* Output for sqlite3_trace() */
   int nErr;              /* Number of errors seen */    int nErr;              /* Number of errors seen */
   int mode;              /* An output mode setting */    int mode;              /* An output mode setting */
   int writableSchema;    /* True if PRAGMA writable_schema=ON */    int writableSchema;    /* True if PRAGMA writable_schema=ON */
Line 496 
Line 499 
   int i;    int i;
   char *zBlob = (char *)pBlob;    char *zBlob = (char *)pBlob;
   fprintf(out,"X'");    fprintf(out,"X'");
   for(i=0; i<nBlob; i++){ fprintf(out,"%02x",zBlob[i]); }    for(i=0; i<nBlob; i++){ fprintf(out,"%02x",zBlob[i]&0xff); }
   fprintf(out,"'");    fprintf(out,"'");
 }  }
   
Line 1077 
Line 1080 
     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);      sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
     fprintf(pArg->out, "Page cache misses:                   %d\n", iCur);      fprintf(pArg->out, "Page cache misses:                   %d\n", iCur);
     iHiwtr = iCur = -1;      iHiwtr = iCur = -1;
       sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
       fprintf(pArg->out, "Page cache writes:                   %d\n", iCur);
       iHiwtr = iCur = -1;
     sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);      sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
     fprintf(pArg->out, "Schema Heap Usage:                   %d bytes\n", iCur);      fprintf(pArg->out, "Schema Heap Usage:                   %d bytes\n", iCur);
     iHiwtr = iCur = -1;      iHiwtr = iCur = -1;
Line 1287 
Line 1293 
     char *zTableInfo = 0;      char *zTableInfo = 0;
     char *zTmp = 0;      char *zTmp = 0;
     int nRow = 0;      int nRow = 0;
     int kk;  
   
     zTableInfo = appendText(zTableInfo, "PRAGMA table_info(", 0);      zTableInfo = appendText(zTableInfo, "PRAGMA table_info(", 0);
     zTableInfo = appendText(zTableInfo, zTable, '"');      zTableInfo = appendText(zTableInfo, zTable, '"');
Line 1300 
Line 1305 
     }      }
   
     zSelect = appendText(zSelect, "SELECT 'INSERT INTO ' || ", 0);      zSelect = appendText(zSelect, "SELECT 'INSERT INTO ' || ", 0);
     if( !isalpha(zTable[0]) ){      /* Always quote the table name, even if it appears to be pure ascii,
       kk = 0;      ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
     }else{      zTmp = appendText(zTmp, zTable, '"');
       for(kk=1; isalnum(zTable[kk]); kk++){}  
     }  
     zTmp = appendText(zTmp, zTable, zTable[kk] ? '"' : 0);  
     if( zTmp ){      if( zTmp ){
       zSelect = appendText(zSelect, zTmp, '\'');        zSelect = appendText(zSelect, zTmp, '\'');
         free(zTmp);
     }      }
     zSelect = appendText(zSelect, " || ' VALUES(' || ", 0);      zSelect = appendText(zSelect, " || ' VALUES(' || ", 0);
     rc = sqlite3_step(pTableInfo);      rc = sqlite3_step(pTableInfo);
Line 1336 
Line 1339 
       zSelect = appendText(zSelect, " ORDER BY rowid DESC", 0);        zSelect = appendText(zSelect, " ORDER BY rowid DESC", 0);
       run_table_dump_query(p, zSelect, 0);        run_table_dump_query(p, zSelect, 0);
     }      }
     if( zSelect ) free(zSelect);      free(zSelect);
   }    }
   return 0;    return 0;
 }  }
Line 1366 
Line 1369 
     }      }
     zQ2 = malloc( len+100 );      zQ2 = malloc( len+100 );
     if( zQ2==0 ) return rc;      if( zQ2==0 ) return rc;
     sqlite3_snprintf(sizeof(zQ2), zQ2, "%s ORDER BY rowid DESC", zQuery);      sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
     rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);      rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
     if( rc ){      if( rc ){
       fprintf(p->out, "/****** ERROR: %s ******/\n", zErr);        fprintf(p->out, "/****** ERROR: %s ******/\n", zErr);
Line 1432 
Line 1435 
   "                         If TABLE specified, only list tables matching\n"    "                         If TABLE specified, only list tables matching\n"
   "                         LIKE pattern TABLE.\n"    "                         LIKE pattern TABLE.\n"
   ".timeout MS            Try opening locked tables for MS milliseconds\n"    ".timeout MS            Try opening locked tables for MS milliseconds\n"
     ".trace FILE|off        Output each SQL statement as it is run\n"
   ".vfsname ?AUX?         Print the name of the VFS stack\n"    ".vfsname ?AUX?         Print the name of the VFS stack\n"
   ".width NUM1 NUM2 ...   Set column widths for \"column\" mode\n"    ".width NUM1 NUM2 ...   Set column widths for \"column\" mode\n"
 ;  ;
Line 1522 
Line 1526 
 }  }
   
 /*  /*
   ** Close an output file, assuming it is not stderr or stdout
   */
   static void output_file_close(FILE *f){
     if( f && f!=stdout && f!=stderr ) fclose(f);
   }
   
   /*
   ** Try to open an output file.   The names "stdout" and "stderr" are
   ** recognized and do the right thing.  NULL is returned if the output
   ** filename is "off".
   */
   static FILE *output_file_open(const char *zFile){
     FILE *f;
     if( strcmp(zFile,"stdout")==0 ){
       f = stdout;
     }else if( strcmp(zFile, "stderr")==0 ){
       f = stderr;
     }else if( strcmp(zFile, "off")==0 ){
       f = 0;
     }else{
       f = fopen(zFile, "wb");
       if( f==0 ){
         fprintf(stderr, "Error: cannot open \"%s\"\n", zFile);
       }
     }
     return f;
   }
   
   /*
   ** A routine for handling output from sqlite3_trace().
   */
   static void sql_trace_callback(void *pArg, const char *z){
     FILE *f = (FILE*)pArg;
     if( f ) fprintf(f, "%s\n", z);
   }
   
   /*
   ** A no-op routine that runs with the ".breakpoint" doc-command.  This is
   ** a useful spot to set a debugger breakpoint.
   */
   static void test_breakpoint(void){
     static int nCall = 0;
     nCall++;
   }
   
   /*
 ** If an input line begins with "." then invoke this routine to  ** If an input line begins with "." then invoke this routine to
 ** process that line.  ** process that line.
 **  **
Line 1600 
Line 1650 
     bail_on_error = booleanValue(azArg[1]);      bail_on_error = booleanValue(azArg[1]);
   }else    }else
   
     /* The undocumented ".breakpoint" command causes a call to the no-op
     ** routine named test_breakpoint().
     */
     if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
       test_breakpoint();
     }else
   
   if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 && nArg==1 ){    if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 && nArg==1 ){
     struct callback_data data;      struct callback_data data;
     char *zErrMsg = 0;      char *zErrMsg = 0;
Line 1931 
Line 1988 
   
   if( c=='l' && strncmp(azArg[0], "log", n)==0 && nArg>=2 ){    if( c=='l' && strncmp(azArg[0], "log", n)==0 && nArg>=2 ){
     const char *zFile = azArg[1];      const char *zFile = azArg[1];
     if( p->pLog && p->pLog!=stdout && p->pLog!=stderr ){      output_file_close(p->pLog);
       fclose(p->pLog);      p->pLog = output_file_open(zFile);
       p->pLog = 0;  
     }  
     if( strcmp(zFile,"stdout")==0 ){  
       p->pLog = stdout;  
     }else if( strcmp(zFile, "stderr")==0 ){  
       p->pLog = stderr;  
     }else if( strcmp(zFile, "off")==0 ){  
       p->pLog = 0;  
     }else{  
       p->pLog = fopen(zFile, "w");  
       if( p->pLog==0 ){  
         fprintf(stderr, "Error: cannot open \"%s\"\n", zFile);  
       }  
     }  
   }else    }else
   
   if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg==2 ){    if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg==2 ){
Line 1999 
Line 2042 
   }else    }else
   
   if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){    if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){
     if( p->out!=stdout ){      if( p->outfile[0]=='|' ){
       fclose(p->out);        pclose(p->out);
       }else{
         output_file_close(p->out);
     }      }
     if( strcmp(azArg[1],"stdout")==0 ){      p->outfile[0] = 0;
       p->out = stdout;      if( azArg[1][0]=='|' ){
       sqlite3_snprintf(sizeof(p->outfile), p->outfile, "stdout");        p->out = popen(&azArg[1][1], "w");
         if( p->out==0 ){
           fprintf(stderr,"Error: cannot open pipe \"%s\"\n", &azArg[1][1]);
           p->out = stdout;
           rc = 1;
         }else{
           sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", azArg[1]);
         }
     }else{      }else{
       p->out = fopen(azArg[1], "wb");        p->out = output_file_open(azArg[1]);
       if( p->out==0 ){        if( p->out==0 ){
         fprintf(stderr,"Error: cannot write to \"%s\"\n", azArg[1]);          if( strcmp(azArg[1],"off")!=0 ){
             fprintf(stderr,"Error: cannot write to \"%s\"\n", azArg[1]);
           }
         p->out = stdout;          p->out = stdout;
         rc = 1;          rc = 1;
       } else {        } else {
          sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", azArg[1]);          sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", azArg[1]);
       }        }
     }      }
   }else    }else
Line 2194 
Line 2248 
   }else    }else
   
   if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 && nArg<3 ){    if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 && nArg<3 ){
       sqlite3_stmt *pStmt;
     char **azResult;      char **azResult;
     int nRow;      int nRow, nAlloc;
     char *zErrMsg;      char *zSql = 0;
       int ii;
     open_db(p);      open_db(p);
     if( nArg==1 ){      rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
       rc = sqlite3_get_table(p->db,      if( rc ) return rc;
         "SELECT name FROM sqlite_master "      zSql = sqlite3_mprintf(
         "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%' "          "SELECT name FROM sqlite_master"
         "UNION ALL "          " WHERE type IN ('table','view')"
         "SELECT name FROM sqlite_temp_master "          "   AND name NOT LIKE 'sqlite_%%'"
         "WHERE type IN ('table','view') "          "   AND name LIKE ?1");
         "ORDER BY 1",      while( sqlite3_step(pStmt)==SQLITE_ROW ){
         &azResult, &nRow, 0, &zErrMsg        const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
       );        if( zDbName==0 || strcmp(zDbName,"main")==0 ) continue;
     }else{        if( strcmp(zDbName,"temp")==0 ){
       zShellStatic = azArg[1];          zSql = sqlite3_mprintf(
       rc = sqlite3_get_table(p->db,                   "%z UNION ALL "
         "SELECT name FROM sqlite_master "                   "SELECT 'temp.' || name FROM sqlite_temp_master"
         "WHERE type IN ('table','view') AND name LIKE shellstatic() "                   " WHERE type IN ('table','view')"
         "UNION ALL "                   "   AND name NOT LIKE 'sqlite_%%'"
         "SELECT name FROM sqlite_temp_master "                   "   AND name LIKE ?1", zSql);
         "WHERE type IN ('table','view') AND name LIKE shellstatic() "        }else{
         "ORDER BY 1",          zSql = sqlite3_mprintf(
         &azResult, &nRow, 0, &zErrMsg                   "%z UNION ALL "
       );                   "SELECT '%q.' || name FROM \"%w\".sqlite_master"
       zShellStatic = 0;                   " WHERE type IN ('table','view')"
                    "   AND name NOT LIKE 'sqlite_%%'"
                    "   AND name LIKE ?1", zSql, zDbName, zDbName);
         }
     }      }
     if( zErrMsg ){      sqlite3_finalize(pStmt);
       fprintf(stderr,"Error: %s\n", zErrMsg);      zSql = sqlite3_mprintf("%z ORDER BY 1", zSql);
       sqlite3_free(zErrMsg);      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
       rc = 1;      sqlite3_free(zSql);
     }else if( rc != SQLITE_OK ){      if( rc ) return rc;
       fprintf(stderr,"Error: querying sqlite_master and sqlite_temp_master\n");      nRow = nAlloc = 0;
       rc = 1;      azResult = 0;
       if( nArg>1 ){
         sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
     }else{      }else{
         sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
       }
       while( sqlite3_step(pStmt)==SQLITE_ROW ){
         if( nRow>=nAlloc ){
           char **azNew;
           int n = nAlloc*2 + 10;
           azNew = sqlite3_realloc(azResult, sizeof(azResult[0])*n);
           if( azNew==0 ){
             fprintf(stderr, "Error: out of memory\n");
             break;
           }
           nAlloc = n;
           azResult = azNew;
         }
         azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
         if( azResult[nRow] ) nRow++;
       }
       sqlite3_finalize(pStmt);
       if( nRow>0 ){
       int len, maxlen = 0;        int len, maxlen = 0;
       int i, j;        int i, j;
       int nPrintCol, nPrintRow;        int nPrintCol, nPrintRow;
       for(i=1; i<=nRow; i++){        for(i=0; i<nRow; i++){
         if( azResult[i]==0 ) continue;  
         len = strlen30(azResult[i]);          len = strlen30(azResult[i]);
         if( len>maxlen ) maxlen = len;          if( len>maxlen ) maxlen = len;
       }        }
Line 2241 
Line 2320 
       if( nPrintCol<1 ) nPrintCol = 1;        if( nPrintCol<1 ) nPrintCol = 1;
       nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;        nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
       for(i=0; i<nPrintRow; i++){        for(i=0; i<nPrintRow; i++){
         for(j=i+1; j<=nRow; j+=nPrintRow){          for(j=i; j<nRow; j+=nPrintRow){
           char *zSp = j<=nPrintRow ? "" : "  ";            char *zSp = j<nPrintRow ? "" : "  ";
           printf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : "");            printf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : "");
         }          }
         printf("\n");          printf("\n");
       }        }
     }      }
     sqlite3_free_table(azResult);      for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
       sqlite3_free(azResult);
   }else    }else
   
   if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 && nArg>=2 ){    if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 && nArg>=2 ){
Line 2382 
Line 2462 
     enableTimer = booleanValue(azArg[1]);      enableTimer = booleanValue(azArg[1]);
   }else    }else
   
     if( c=='t' && strncmp(azArg[0], "trace", n)==0 && nArg>1 ){
       open_db(p);
       output_file_close(p->traceOut);
       p->traceOut = output_file_open(azArg[1]);
   #ifndef SQLITE_OMIT_TRACE
       if( p->traceOut==0 ){
         sqlite3_trace(p->db, 0, 0);
       }else{
         sqlite3_trace(p->db, sql_trace_callback, p->traceOut);
       }
   #endif
     }else
   
   if( c=='v' && strncmp(azArg[0], "version", n)==0 ){    if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
     printf("SQLite %s %s\n" /*extra-version-info*/,      printf("SQLite %s %s\n" /*extra-version-info*/,
         sqlite3_libversion(), sqlite3_sourceid());          sqlite3_libversion(), sqlite3_sourceid());
Line 2506 
Line 2599 
     free(zLine);      free(zLine);
     zLine = one_input_line(zSql, in);      zLine = one_input_line(zSql, in);
     if( zLine==0 ){      if( zLine==0 ){
       break;  /* We have reached EOF */        /* End of input */
         if( stdin_is_interactive ) printf("\n");
         break;
     }      }
     if( seenInterrupt ){      if( seenInterrupt ){
       if( in!=0 ) break;        if( in!=0 ) break;
Line 2593 
Line 2688 
   
 /*  /*
 ** Return a pathname which is the user's home directory.  A  ** Return a pathname which is the user's home directory.  A
 ** 0 return indicates an error of some kind.  Space to hold the  ** 0 return indicates an error of some kind.
 ** resulting string is obtained from malloc().  The calling  
 ** function should free the result.  
 */  */
 static char *find_home_dir(void){  static char *find_home_dir(void){
   char *home_dir = NULL;    static char *home_dir = NULL;
     if( home_dir ) return home_dir;
   
 #if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__) && !defined(_WIN32_WCE) && !defined(__RTP__) && !defined(_WRS_KERNEL)  #if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__) && !defined(_WIN32_WCE) && !defined(__RTP__) && !defined(_WRS_KERNEL)
   struct passwd *pwent;    struct passwd *pwent;
Line 2611 
Line 2705 
 #if defined(_WIN32_WCE)  #if defined(_WIN32_WCE)
   /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()    /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
    */     */
   home_dir = strdup("/");    home_dir = "/";
 #else  #else
   
 #if defined(_WIN32) || defined(WIN32) || defined(__OS2__)  #if defined(_WIN32) || defined(WIN32) || defined(__OS2__)
Line 2667 
Line 2761 
   const char *sqliterc = sqliterc_override;    const char *sqliterc = sqliterc_override;
   char *zBuf = 0;    char *zBuf = 0;
   FILE *in = NULL;    FILE *in = NULL;
   int nBuf;  
   int rc = 0;    int rc = 0;
   
   if (sqliterc == NULL) {    if (sqliterc == NULL) {
Line 2678 
Line 2771 
 #endif  #endif
       return 1;        return 1;
     }      }
     nBuf = strlen30(home_dir) + 16;      zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
     zBuf = malloc( nBuf );      sqliterc = zBuf;
     if( zBuf==0 ){  
       fprintf(stderr,"%s: Error: out of memory\n",Argv0);  
       return 1;  
     }  
     sqlite3_snprintf(nBuf, zBuf,"%s/.sqliterc",home_dir);  
     free(home_dir);  
     sqliterc = (const char*)zBuf;  
   }    }
   in = fopen(sqliterc,"rb");    in = fopen(sqliterc,"rb");
   if( in ){    if( in ){
Line 2696 
Line 2782 
     rc = process_input(p,in);      rc = process_input(p,in);
     fclose(in);      fclose(in);
   }    }
   free(zBuf);    sqlite3_free(zBuf);
   return rc;    return rc;
 }  }
   
Line 3037 
Line 3123 
         write_history(zHistory);          write_history(zHistory);
         free(zHistory);          free(zHistory);
       }        }
       free(zHome);  
     }else{      }else{
       rc = process_input(&data, stdin);        rc = process_input(&data, stdin);
     }      }

Legend:
Removed from v.1.1.1.1  
changed lines
  Added in v.1.1.1.2