⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shell.c

📁 SQLite 2.8.6 源代码,用来在Linux/Unix/Windows上编译安装.它是一个小型的数据库,但是非常好用,速度也快,一般的数据库查询之类的操作据统计比MySQL,PostgreSQL
💻 C
📖 第 1 页 / 共 3 页
字号:
/*** 2001 September 15**** The author disclaims copyright to this source code.  In place of** a legal notice, here is a blessing:****    May you do good and not evil.**    May you find forgiveness for yourself and forgive others.**    May you share freely, never taking more than you give.***************************************************************************** This file contains code to implement the "sqlite" command line** utility for accessing SQLite databases.**** $Id: shell.c,v 1.82 2003/07/18 01:30:59 drh Exp $*/#include <stdlib.h>#include <string.h>#include <stdio.h>#include "sqlite.h"#include <ctype.h>#if !defined(_WIN32) && !defined(WIN32) && !defined(__MACOS__)# include <signal.h># include <pwd.h># include <unistd.h># include <sys/types.h>#endif#ifdef __MACOS__# include <console.h># include <signal.h># include <unistd.h># include <extras.h># include <Files.h># include <Folders.h>#endif#if defined(HAVE_READLINE) && HAVE_READLINE==1# include <readline/readline.h># include <readline/history.h>#else# define readline(p) local_getline(p,stdin)# define add_history(X)# define read_history(X)# define write_history(X)# define stifle_history(X)#endif/* Make sure isatty() has a prototype.*/extern int isatty();/*** The following is the open SQLite database.  We make a pointer** to this database a static variable so that it can be accessed** by the SIGINT handler to interrupt database processing.*/static sqlite *db = 0;/*** True if an interrupt (Control-C) has been received.*/static int seenInterrupt = 0;/*** This is the name of our program. It is set in main(), used** in a number of other places, mostly for error messages.*/static char *Argv0;/*** Prompt strings. Initialized in main. Settable with**   .prompt main continue*/static char mainPrompt[20];     /* First line prompt. default: "sqlite> "*/static char continuePrompt[20]; /* Continuation prompt. default: "   ...> " *//*** Determines if a string is a number of not.*/extern int sqliteIsNumber(const char*);/*** This routine reads a line of text from standard input, stores** the text in memory obtained from malloc() and returns a pointer** to the text.  NULL is returned at end of file, or if malloc()** fails.**** The interface is like "readline" but no command-line editing** is done.*/static char *local_getline(char *zPrompt, FILE *in){  char *zLine;  int nLine;  int n;  int eol;  if( zPrompt && *zPrompt ){    printf("%s",zPrompt);    fflush(stdout);  }  nLine = 100;  zLine = malloc( nLine );  if( zLine==0 ) return 0;  n = 0;  eol = 0;  while( !eol ){    if( n+100>nLine ){      nLine = nLine*2 + 100;      zLine = realloc(zLine, nLine);      if( zLine==0 ) return 0;    }    if( fgets(&zLine[n], nLine - n, in)==0 ){      if( n==0 ){        free(zLine);        return 0;      }      zLine[n] = 0;      eol = 1;      break;    }    while( zLine[n] ){ n++; }    if( n>0 && zLine[n-1]=='\n' ){      n--;      zLine[n] = 0;      eol = 1;    }  }  zLine = realloc( zLine, n+1 );  return zLine;}/*** Retrieve a single line of input text.  "isatty" is true if text** is coming from a terminal.  In that case, we issue a prompt and** attempt to use "readline" for command-line editing.  If "isatty"** is false, use "local_getline" instead of "readline" and issue no prompt.**** zPrior is a string of prior text retrieved.  If not the empty** string, then issue a continuation prompt.*/static char *one_input_line(const char *zPrior, FILE *in){  char *zPrompt;  char *zResult;  if( in!=0 ){    return local_getline(0, in);  }  if( zPrior && zPrior[0] ){    zPrompt = continuePrompt;  }else{    zPrompt = mainPrompt;  }  zResult = readline(zPrompt);  if( zResult ) add_history(zResult);  return zResult;}struct previous_mode_data {  int valid;        /* Is there legit data in here? */  int mode;  int showHeader;  int colWidth[100];};/*** An pointer to an instance of this structure is passed from** the main program to the callback.  This is used to communicate** state and mode information.*/struct callback_data {  sqlite *db;            /* The database */  int echoOn;            /* True to echo input commands */  int cnt;               /* Number of records displayed so far */  FILE *out;             /* Write results here */  int mode;              /* An output mode setting */  int showHeader;        /* True to show column names in List or Column mode */  char *zDestTable;      /* Name of destination table when MODE_Insert */  char separator[20];    /* Separator character for MODE_List */  int colWidth[100];     /* Requested width of each column when in column mode*/  int actualWidth[100];  /* Actual width of each column */  char nullvalue[20];    /* The text to print when a NULL comes back from                         ** the database */  struct previous_mode_data explainPrev;                         /* Holds the mode information just before                         ** .explain ON */  char outfile[FILENAME_MAX]; /* Filename for *out */  const char *zDbFilename;    /* name of the database file */};/*** These are the allowed modes.*/#define MODE_Line     0  /* One column per line.  Blank line between records */#define MODE_Column   1  /* One record per line in neat columns */#define MODE_List     2  /* One record per line with a separator */#define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */#define MODE_Html     4  /* Generate an XHTML table */#define MODE_Insert   5  /* Generate SQL "insert" statements */#define MODE_NUM_OF   6  /* The number of modes (not a mode itself) */char *modeDescr[MODE_NUM_OF] = {  "line",  "column",  "list",  "semi",  "html",  "insert"};/*** Number of elements in an array*/#define ArraySize(X)  (sizeof(X)/sizeof(X[0]))/*** Output the given string as a quoted string using SQL quoting conventions.*/static void output_quoted_string(FILE *out, const char *z){  int i;  int nSingle = 0;  for(i=0; z[i]; i++){    if( z[i]=='\'' ) nSingle++;  }  if( nSingle==0 ){    fprintf(out,"'%s'",z);  }else{    fprintf(out,"'");    while( *z ){      for(i=0; z[i] && z[i]!='\''; i++){}      if( i==0 ){        fprintf(out,"''");        z++;      }else if( z[i]=='\'' ){        fprintf(out,"%.*s''",i,z);        z += i+1;      }else{        fprintf(out,"%s",z);        break;      }    }    fprintf(out,"'");  }}/*** Output the given string with characters that are special to** HTML escaped.*/static void output_html_string(FILE *out, const char *z){  int i;  while( *z ){    for(i=0; z[i] && z[i]!='<' && z[i]!='&'; i++){}    if( i>0 ){      fprintf(out,"%.*s",i,z);    }    if( z[i]=='<' ){      fprintf(out,"&lt;");    }else if( z[i]=='&' ){      fprintf(out,"&amp;");    }else{      break;    }    z += i + 1;  }}/*** This routine runs when the user presses Ctrl-C*/static void interrupt_handler(int NotUsed){  seenInterrupt = 1;  if( db ) sqlite_interrupt(db);}/*** This is the callback routine that the SQLite library** invokes for each row of a query result.*/static int callback(void *pArg, int nArg, char **azArg, char **azCol){  int i;  struct callback_data *p = (struct callback_data*)pArg;  switch( p->mode ){    case MODE_Line: {      int w = 5;      if( azArg==0 ) break;      for(i=0; i<nArg; i++){        int len = strlen(azCol[i]);        if( len>w ) w = len;      }      if( p->cnt++>0 ) fprintf(p->out,"\n");      for(i=0; i<nArg; i++){        fprintf(p->out,"%*s = %s\n", w, azCol[i],                 azArg[i] ? azArg[i] : p->nullvalue);      }      break;    }    case MODE_Column: {      if( p->cnt++==0 ){        for(i=0; i<nArg; i++){          int w, n;          if( i<ArraySize(p->colWidth) ){             w = p->colWidth[i];          }else{             w = 0;          }          if( w<=0 ){            w = strlen(azCol[i] ? azCol[i] : "");            if( w<10 ) w = 10;            n = strlen(azArg && azArg[i] ? azArg[i] : p->nullvalue);            if( w<n ) w = n;          }          if( i<ArraySize(p->actualWidth) ){            p->actualWidth[i] = w;          }          if( p->showHeader ){            fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": "  ");          }        }        if( p->showHeader ){          for(i=0; i<nArg; i++){            int w;            if( i<ArraySize(p->actualWidth) ){               w = p->actualWidth[i];            }else{               w = 10;            }            fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------"                   "----------------------------------------------------------",                    i==nArg-1 ? "\n": "  ");          }        }      }      if( azArg==0 ) break;      for(i=0; i<nArg; i++){        int w;        if( i<ArraySize(p->actualWidth) ){           w = p->actualWidth[i];        }else{           w = 10;        }        fprintf(p->out,"%-*.*s%s",w,w,            azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": "  ");      }      break;    }    case MODE_Semi:    case MODE_List: {      if( p->cnt++==0 && p->showHeader ){        for(i=0; i<nArg; i++){          fprintf(p->out,"%s%s",azCol[i], i==nArg-1 ? "\n" : p->separator);        }      }      if( azArg==0 ) break;      for(i=0; i<nArg; i++){        char *z = azArg[i];        if( z==0 ) z = p->nullvalue;        fprintf(p->out, "%s", z);        if( i<nArg-1 ){          fprintf(p->out, "%s", p->separator);        }else if( p->mode==MODE_Semi ){          fprintf(p->out, ";\n");        }else{          fprintf(p->out, "\n");        }      }      break;    }    case MODE_Html: {      if( p->cnt++==0 && p->showHeader ){        fprintf(p->out,"<TR>");        for(i=0; i<nArg; i++){          fprintf(p->out,"<TH>%s</TH>",azCol[i]);        }        fprintf(p->out,"</TR>\n");      }      if( azArg==0 ) break;      fprintf(p->out,"<TR>");      for(i=0; i<nArg; i++){        fprintf(p->out,"<TD>");        output_html_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);        fprintf(p->out,"</TD>\n");      }      fprintf(p->out,"</TR>\n");      break;    }    case MODE_Insert: {      if( azArg==0 ) break;      fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable);      for(i=0; i<nArg; i++){        char *zSep = i>0 ? ",": "";        if( azArg[i]==0 ){          fprintf(p->out,"%sNULL",zSep);        }else if( sqliteIsNumber(azArg[i]) ){          fprintf(p->out,"%s%s",zSep, azArg[i]);        }else{          if( zSep[0] ) fprintf(p->out,"%s",zSep);          output_quoted_string(p->out, azArg[i]);        }      }      fprintf(p->out,");\n");      break;    }  }  return 0;}/*** Set the destination table field of the callback_data structure to** the name of the table given.  Escape any quote characters in the** table name.*/static void set_table_name(struct callback_data *p, const char *zName){  int i, n;  int needQuote;  char *z;  if( p->zDestTable ){    free(p->zDestTable);    p->zDestTable = 0;  }  if( zName==0 ) return;  needQuote = !isalpha(*zName) && *zName!='_';  for(i=n=0; zName[i]; i++, n++){    if( !isalnum(zName[i]) && zName[i]!='_' ){      needQuote = 1;      if( zName[i]=='\'' ) n++;    }  }  if( needQuote ) n += 2;  z = p->zDestTable = malloc( n+1 );  if( z==0 ){    fprintf(stderr,"Out of memory!\n");    exit(1);  }  n = 0;  if( needQuote ) z[n++] = '\'';  for(i=0; zName[i]; i++){    z[n++] = zName[i];    if( zName[i]=='\'' ) z[n++] = '\'';  }  if( needQuote ) z[n++] = '\'';  z[n] = 0;}/*** This is a different callback routine used for dumping the database.** Each row received by this callback consists of a table name,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -