aboutsummaryrefslogtreecommitdiff
path: root/contrib/sqlite3/shell.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/sqlite3/shell.c')
-rw-r--r--contrib/sqlite3/shell.c7276
1 files changed, 5098 insertions, 2178 deletions
diff --git a/contrib/sqlite3/shell.c b/contrib/sqlite3/shell.c
index b08671e14de3..0d5bf19e74d5 100644
--- a/contrib/sqlite3/shell.c
+++ b/contrib/sqlite3/shell.c
@@ -122,9 +122,6 @@ typedef unsigned short int u16;
typedef sqlite3_int64 i64;
typedef sqlite3_uint64 u64;
typedef unsigned char u8;
-#if SQLITE_USER_AUTHENTICATION
-# include "sqlite3userauth.h"
-#endif
#include <ctype.h>
#include <stdarg.h>
@@ -210,8 +207,6 @@ typedef unsigned char u8;
# ifndef strdup
# define strdup _strdup
# endif
-# undef popen
-# define popen _popen
# undef pclose
# define pclose _pclose
# endif
@@ -241,6 +236,8 @@ typedef unsigned char u8;
#define IsSpace(X) isspace((unsigned char)X)
#define IsDigit(X) isdigit((unsigned char)X)
#define ToLower(X) (char)tolower((unsigned char)X)
+#define IsAlnum(X) isalnum((unsigned char)X)
+#define IsAlpha(X) isalpha((unsigned char)X)
#if defined(_WIN32) || defined(WIN32)
#if SQLITE_OS_WINRT
@@ -255,19 +252,9 @@ extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
#endif
-/* Use console I/O package as a direct INCLUDE. */
-#define SQLITE_INTERNAL_LINKAGE static
-
-#ifdef SQLITE_SHELL_FIDDLE
-/* Deselect most features from the console I/O package for Fiddle. */
-# define SQLITE_CIO_NO_REDIRECT
-# define SQLITE_CIO_NO_CLASSIFY
-# define SQLITE_CIO_NO_TRANSLATE
-# define SQLITE_CIO_NO_SETMODE
-#endif
-/************************* Begin ../ext/consio/console_io.h ******************/
+/************************* Begin ../ext/misc/sqlite3_stdio.h ******************/
/*
-** 2023 November 1
+** 2024-09-24
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
@@ -276,281 +263,56 @@ extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
-********************************************************************************
-** This file exposes various interfaces used for console and other I/O
-** by the SQLite project command-line tools. These interfaces are used
-** at either source conglomeration time, compilation time, or run time.
-** This source provides for either inclusion into conglomerated,
-** "single-source" forms or separate compilation then linking.
-**
-** Platform dependencies are "hidden" here by various stratagems so
-** that, provided certain conditions are met, the programs using this
-** source or object code compiled from it need no explicit conditional
-** compilation in their source for their console and stream I/O.
-**
-** The symbols and functionality exposed here are not a public API.
-** This code may change in tandem with other project code as needed.
-**
-** When this .h file and its companion .c are directly incorporated into
-** a source conglomeration (such as shell.c), the preprocessor symbol
-** CIO_WIN_WC_XLATE is defined as 0 or 1, reflecting whether console I/O
-** translation for Windows is effected for the build.
-*/
-#define HAVE_CONSOLE_IO_H 1
-#ifndef SQLITE_INTERNAL_LINKAGE
-# define SQLITE_INTERNAL_LINKAGE extern /* external to translation unit */
-# include <stdio.h>
-#else
-# define SHELL_NO_SYSINC /* Better yet, modify mkshellc.tcl for this. */
-#endif
-
-#ifndef SQLITE3_H
-/* # include "sqlite3.h" */
-#endif
-
-#ifndef SQLITE_CIO_NO_CLASSIFY
-
-/* Define enum for use with following function. */
-typedef enum StreamsAreConsole {
- SAC_NoConsole = 0,
- SAC_InConsole = 1, SAC_OutConsole = 2, SAC_ErrConsole = 4,
- SAC_AnyConsole = 0x7
-} StreamsAreConsole;
-
-/*
-** Classify the three standard I/O streams according to whether
-** they are connected to a console attached to the process.
+*************************************************************************
**
-** Returns the bit-wise OR of SAC_{In,Out,Err}Console values,
-** or SAC_NoConsole if none of the streams reaches a console.
+** This header file contains definitions of interfaces that provide
+** cross-platform I/O for UTF-8 content.
**
-** This function should be called before any I/O is done with
-** the given streams. As a side-effect, the given inputs are
-** recorded so that later I/O operations on them may be done
-** differently than the C library FILE* I/O would be done,
-** iff the stream is used for the I/O functions that follow,
-** and to support the ones that use an implicit stream.
+** On most platforms, the interfaces definitions in this file are
+** just #defines. For example sqlite3_fopen() is a macro that resolves
+** to the standard fopen() in the C-library.
**
-** On some platforms, stream or console mode alteration (aka
-** "Setup") may be made which is undone by consoleRestore().
-*/
-SQLITE_INTERNAL_LINKAGE StreamsAreConsole
-consoleClassifySetup( FILE *pfIn, FILE *pfOut, FILE *pfErr );
-/* A usual call for convenience: */
-#define SQLITE_STD_CONSOLE_INIT() consoleClassifySetup(stdin,stdout,stderr)
-
-/*
-** After an initial call to consoleClassifySetup(...), renew
-** the same setup it effected. (A call not after is an error.)
-** This will restore state altered by consoleRestore();
+** But Windows does not have a standard C-library, at least not one that
+** can handle UTF-8. So for windows build, the interfaces resolve to new
+** C-language routines contained in the separate sqlite3_stdio.c source file.
**
-** Applications which run an inferior (child) process which
-** inherits the same I/O streams may call this function after
-** such a process exits to guard against console mode changes.
+** So on all non-Windows platforms, simply #include this header file and
+** use the interfaces defined herein. Then to run your application on Windows,
+** also link in the accompanying sqlite3_stdio.c source file when compiling
+** to get compatible interfaces.
*/
-SQLITE_INTERNAL_LINKAGE void consoleRenewSetup(void);
+#ifndef _SQLITE3_STDIO_H_
+#define _SQLITE3_STDIO_H_ 1
+#ifdef _WIN32
+/**** Definitions For Windows ****/
+#include <stdio.h>
+#include <windows.h>
-/*
-** Undo any side-effects left by consoleClassifySetup(...).
-**
-** This should be called after consoleClassifySetup() and
-** before the process terminates normally. It is suitable
-** for use with the atexit() C library procedure. After
-** this call, no console I/O should be done until one of
-** console{Classify or Renew}Setup(...) is called again.
-**
-** Applications which run an inferior (child) process that
-** inherits the same I/O streams might call this procedure
-** before so that said process will have a console setup
-** however users have configured it or come to expect.
-*/
-SQLITE_INTERNAL_LINKAGE void SQLITE_CDECL consoleRestore( void );
+FILE *sqlite3_fopen(const char *zFilename, const char *zMode);
+FILE *sqlite3_popen(const char *zCommand, const char *type);
+char *sqlite3_fgets(char *s, int size, FILE *stream);
+int sqlite3_fputs(const char *s, FILE *stream);
+int sqlite3_fprintf(FILE *stream, const char *format, ...);
+void sqlite3_fsetmode(FILE *stream, int mode);
-#else /* defined(SQLITE_CIO_NO_CLASSIFY) */
-# define consoleClassifySetup(i,o,e)
-# define consoleRenewSetup()
-# define consoleRestore()
-#endif /* defined(SQLITE_CIO_NO_CLASSIFY) */
-#ifndef SQLITE_CIO_NO_REDIRECT
-/*
-** Set stream to be used for the functions below which write
-** to "the designated X stream", where X is Output or Error.
-** Returns the previous value.
-**
-** Alternatively, pass the special value, invalidFileStream,
-** to get the designated stream value without setting it.
-**
-** Before the designated streams are set, they default to
-** those passed to consoleClassifySetup(...), and before
-** that is called they default to stdout and stderr.
-**
-** It is error to close a stream so designated, then, without
-** designating another, use the corresponding {o,e}Emit(...).
-*/
-SQLITE_INTERNAL_LINKAGE FILE *invalidFileStream;
-SQLITE_INTERNAL_LINKAGE FILE *setOutputStream(FILE *pf);
-# ifdef CONSIO_SET_ERROR_STREAM
-SQLITE_INTERNAL_LINKAGE FILE *setErrorStream(FILE *pf);
-# endif
#else
-# define setOutputStream(pf)
-# define setErrorStream(pf)
-#endif /* !defined(SQLITE_CIO_NO_REDIRECT) */
-
-#ifndef SQLITE_CIO_NO_TRANSLATE
-/*
-** Emit output like fprintf(). If the output is going to the
-** console and translation from UTF-8 is necessary, perform
-** the needed translation. Otherwise, write formatted output
-** to the provided stream almost as-is, possibly with newline
-** translation as specified by set{Binary,Text}Mode().
-*/
-SQLITE_INTERNAL_LINKAGE int fPrintfUtf8(FILE *pfO, const char *zFormat, ...);
-/* Like fPrintfUtf8 except stream is always the designated output. */
-SQLITE_INTERNAL_LINKAGE int oPrintfUtf8(const char *zFormat, ...);
-/* Like fPrintfUtf8 except stream is always the designated error. */
-SQLITE_INTERNAL_LINKAGE int ePrintfUtf8(const char *zFormat, ...);
-
-/*
-** Emit output like fputs(). If the output is going to the
-** console and translation from UTF-8 is necessary, perform
-** the needed translation. Otherwise, write given text to the
-** provided stream almost as-is, possibly with newline
-** translation as specified by set{Binary,Text}Mode().
-*/
-SQLITE_INTERNAL_LINKAGE int fPutsUtf8(const char *z, FILE *pfO);
-/* Like fPutsUtf8 except stream is always the designated output. */
-SQLITE_INTERNAL_LINKAGE int oPutsUtf8(const char *z);
-/* Like fPutsUtf8 except stream is always the designated error. */
-SQLITE_INTERNAL_LINKAGE int ePutsUtf8(const char *z);
-
-/*
-** Emit output like fPutsUtf8(), except that the length of the
-** accepted char or character sequence is limited by nAccept.
-**
-** Returns the number of accepted char values.
-*/
-#ifdef CONSIO_SPUTB
-SQLITE_INTERNAL_LINKAGE int
-fPutbUtf8(FILE *pfOut, const char *cBuf, int nAccept);
-/* Like fPutbUtf8 except stream is always the designated output. */
-#endif
-SQLITE_INTERNAL_LINKAGE int
-oPutbUtf8(const char *cBuf, int nAccept);
-/* Like fPutbUtf8 except stream is always the designated error. */
-#ifdef CONSIO_EPUTB
-SQLITE_INTERNAL_LINKAGE int
-ePutbUtf8(const char *cBuf, int nAccept);
-#endif
-
-/*
-** Collect input like fgets(...) with special provisions for input
-** from the console on platforms that require same. Defers to the
-** C library fgets() when input is not from the console. Newline
-** translation may be done as set by set{Binary,Text}Mode(). As a
-** convenience, pfIn==NULL is treated as stdin.
-*/
-SQLITE_INTERNAL_LINKAGE char* fGetsUtf8(char *cBuf, int ncMax, FILE *pfIn);
-/* Like fGetsUtf8 except stream is always the designated input. */
-/* SQLITE_INTERNAL_LINKAGE char* iGetsUtf8(char *cBuf, int ncMax); */
-
-#endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
-
-#ifndef SQLITE_CIO_NO_SETMODE
-/*
-** Set given stream for binary mode, where newline translation is
-** not done, or for text mode where, for some platforms, newlines
-** are translated to the platform's conventional char sequence.
-** If bFlush true, flush the stream.
-**
-** An additional side-effect is that if the stream is one passed
-** to consoleClassifySetup() as an output, it is flushed first.
-**
-** Note that binary/text mode has no effect on console I/O
-** translation. On all platforms, newline to the console starts
-** a new line and CR,LF chars from the console become a newline.
-*/
-SQLITE_INTERNAL_LINKAGE void setBinaryMode(FILE *, short bFlush);
-SQLITE_INTERNAL_LINKAGE void setTextMode(FILE *, short bFlush);
-#endif
-
-#ifdef SQLITE_CIO_PROMPTED_IN
-typedef struct Prompts {
- int numPrompts;
- const char **azPrompts;
-} Prompts;
-
-/*
-** Macros for use of a line editor.
-**
-** The following macros define operations involving use of a
-** line-editing library or simple console interaction.
-** A "T" argument is a text (char *) buffer or filename.
-** A "N" argument is an integer.
-**
-** SHELL_ADD_HISTORY(T) // Record text as line(s) of history.
-** SHELL_READ_HISTORY(T) // Read history from file named by T.
-** SHELL_WRITE_HISTORY(T) // Write history to file named by T.
-** SHELL_STIFLE_HISTORY(N) // Limit history to N entries.
-**
-** A console program which does interactive console input is
-** expected to call:
-** SHELL_READ_HISTORY(T) before collecting such input;
-** SHELL_ADD_HISTORY(T) as record-worthy input is taken;
-** SHELL_STIFLE_HISTORY(N) after console input ceases; then
-** SHELL_WRITE_HISTORY(T) before the program exits.
-*/
-
-/*
-** Retrieve a single line of input text from an input stream.
-**
-** If pfIn is the input stream passed to consoleClassifySetup(),
-** and azPrompt is not NULL, then a prompt is issued before the
-** line is collected, as selected by the isContinuation flag.
-** Array azPrompt[{0,1}] holds the {main,continuation} prompt.
-**
-** If zBufPrior is not NULL then it is a buffer from a prior
-** call to this routine that can be reused, or will be freed.
-**
-** The result is stored in space obtained from malloc() and
-** must either be freed by the caller or else passed back to
-** this function as zBufPrior for reuse.
-**
-** This function may call upon services of a line-editing
-** library to interactively collect line edited input.
-*/
-SQLITE_INTERNAL_LINKAGE char *
-shellGetLine(FILE *pfIn, char *zBufPrior, int nLen,
- short isContinuation, Prompts azPrompt);
-#endif /* defined(SQLITE_CIO_PROMPTED_IN) */
-/*
-** TBD: Define an interface for application(s) to generate
-** completion candidates for use by the line-editor.
-**
-** This may be premature; the CLI is the only application
-** that does this. Yet, getting line-editing melded into
-** console I/O is desirable because a line-editing library
-** may have to establish console operating mode, possibly
-** in a way that interferes with the above functionality.
-*/
-
-#if !(defined(SQLITE_CIO_NO_UTF8SCAN)&&defined(SQLITE_CIO_NO_TRANSLATE))
-/* Skip over as much z[] input char sequence as is valid UTF-8,
-** limited per nAccept char's or whole characters and containing
-** no char cn such that ((1<<cn) & ccm)!=0. On return, the
-** sequence z:return (inclusive:exclusive) is validated UTF-8.
-** Limit: nAccept>=0 => char count, nAccept<0 => character
- */
-SQLITE_INTERNAL_LINKAGE const char*
-zSkipValidUtf8(const char *z, int nAccept, long ccm);
+/**** Definitions For All Other Platforms ****/
+#include <stdio.h>
+#define sqlite3_fopen fopen
+#define sqlite3_popen popen
+#define sqlite3_fgets fgets
+#define sqlite3_fputs fputs
+#define sqlite3_fprintf fprintf
+#define sqlite3_fsetmode(F,X) /*no-op*/
#endif
+#endif /* _SQLITE3_STDIO_H_ */
-/************************* End ../ext/consio/console_io.h ********************/
-/************************* Begin ../ext/consio/console_io.c ******************/
+/************************* End ../ext/misc/sqlite3_stdio.h ********************/
+/************************* Begin ../ext/misc/sqlite3_stdio.c ******************/
/*
-** 2023 November 4
+** 2024-09-24
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
@@ -559,731 +321,317 @@ zSkipValidUtf8(const char *z, int nAccept, long ccm);
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
-********************************************************************************
-** This file implements various interfaces used for console and stream I/O
-** by the SQLite project command-line tools, as explained in console_io.h .
-** Functions prefixed by "SQLITE_INTERNAL_LINKAGE" behave as described there.
+*************************************************************************
+**
+** Implementation of standard I/O interfaces for UTF-8 that are missing
+** on Windows.
*/
-
-#ifndef SQLITE_CDECL
-# define SQLITE_CDECL
-#endif
-
-#ifndef SHELL_NO_SYSINC
-# include <stdarg.h>
-# include <string.h>
-# include <stdlib.h>
-# include <limits.h>
-# include <assert.h>
-/* # include "sqlite3.h" */
-#endif
-#ifndef HAVE_CONSOLE_IO_H
-# include "console_io.h"
-#endif
-#if defined(_MSC_VER)
-# pragma warning(disable : 4204)
+#ifdef _WIN32 /* This file is a no-op on all platforms except Windows */
+#ifndef _SQLITE3_STDIO_H_
+/* #include "sqlite3_stdio.h" */
#endif
+#undef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <assert.h>
+/* #include "sqlite3.h" */
+#include <ctype.h>
+#include <stdarg.h>
+#include <io.h>
+#include <fcntl.h>
-#ifndef SQLITE_CIO_NO_TRANSLATE
-# if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
-# ifndef SHELL_NO_SYSINC
-# include <io.h>
-# include <fcntl.h>
-# undef WIN32_LEAN_AND_MEAN
-# define WIN32_LEAN_AND_MEAN
-# include <windows.h>
-# endif
-# define CIO_WIN_WC_XLATE 1 /* Use WCHAR Windows APIs for console I/O */
-# else
-# ifndef SHELL_NO_SYSINC
-# include <unistd.h>
-# endif
-# define CIO_WIN_WC_XLATE 0 /* Use plain C library stream I/O at console */
-# endif
+/*
+** If the SQLITE_U8TEXT_ONLY option is defined, then use O_U8TEXT
+** when appropriate on all output. (Sometimes use O_BINARY when
+** rendering ASCII text in cases where NL-to-CRLF expansion would
+** not be correct.)
+**
+** If the SQLITE_U8TEXT_STDIO option is defined, then use O_U8TEXT
+** when appropriate when writing to stdout or stderr. Use O_BINARY
+** or O_TEXT (depending on things like the .mode and the .crlf setting
+** in the CLI, or other context clues in other applications) for all
+** other output channels.
+**
+** The default behavior, if neither of the above is defined is to
+** use O_U8TEXT when writing to the Windows console (or anything
+** else for which _isatty() returns true) and to use O_BINARY or O_TEXT
+** for all other output channels.
+**
+** The SQLITE_USE_W32_FOR_CONSOLE_IO macro is also available. If
+** defined, it forces the use of Win32 APIs for all console I/O, both
+** input and output. This is necessary for some non-Microsoft run-times
+** that implement stdio differently from Microsoft/Visual-Studio.
+*/
+#if defined(SQLITE_U8TEXT_ONLY)
+# define UseWtextForOutput(fd) 1
+# define UseWtextForInput(fd) 1
+# define IsConsole(fd) _isatty(_fileno(fd))
+#elif defined(SQLITE_U8TEXT_STDIO)
+# define UseWtextForOutput(fd) ((fd)==stdout || (fd)==stderr)
+# define UseWtextForInput(fd) ((fd)==stdin)
+# define IsConsole(fd) _isatty(_fileno(fd))
#else
-# define CIO_WIN_WC_XLATE 0 /* Not exposing translation routines at all */
-#endif
-
-#if CIO_WIN_WC_XLATE
-static HANDLE handleOfFile(FILE *pf){
- int fileDesc = _fileno(pf);
- union { intptr_t osfh; HANDLE fh; } fid = {
- (fileDesc>=0)? _get_osfhandle(fileDesc) : (intptr_t)INVALID_HANDLE_VALUE
- };
- return fid.fh;
-}
+# define UseWtextForOutput(fd) _isatty(_fileno(fd))
+# define UseWtextForInput(fd) _isatty(_fileno(fd))
+# define IsConsole(fd) 1
#endif
-#ifndef SQLITE_CIO_NO_TRANSLATE
-typedef struct PerStreamTags {
-# if CIO_WIN_WC_XLATE
- HANDLE hx;
- DWORD consMode;
- char acIncomplete[4];
-# else
- short reachesConsole;
-# endif
- FILE *pf;
-} PerStreamTags;
-
-/* Define NULL-like value for things which can validly be 0. */
-# define SHELL_INVALID_FILE_PTR ((FILE *)~0)
-# if CIO_WIN_WC_XLATE
-# define SHELL_INVALID_CONS_MODE 0xFFFF0000
-# endif
-
-# if CIO_WIN_WC_XLATE
-# define PST_INITIALIZER { INVALID_HANDLE_VALUE, SHELL_INVALID_CONS_MODE, \
- {0,0,0,0}, SHELL_INVALID_FILE_PTR }
-# else
-# define PST_INITIALIZER { 0, SHELL_INVALID_FILE_PTR }
-# endif
-
-/* Quickly say whether a known output is going to the console. */
-# if CIO_WIN_WC_XLATE
-static short pstReachesConsole(PerStreamTags *ppst){
- return (ppst->hx != INVALID_HANDLE_VALUE);
-}
-# else
-# define pstReachesConsole(ppst) 0
-# endif
+/*
+** Global variables determine if simulated O_BINARY mode is to be
+** used for stdout or other, respectively. Simulated O_BINARY mode
+** means the mode is usually O_BINARY, but switches to O_U8TEXT for
+** unicode characters U+0080 or greater (any character that has a
+** multi-byte representation in UTF-8). This is the only way we
+** have found to render Unicode characters on a Windows console while
+** at the same time avoiding undesirable \n to \r\n translation.
+*/
+static int simBinaryStdout = 0;
+static int simBinaryOther = 0;
-# if CIO_WIN_WC_XLATE
-static void restoreConsoleArb(PerStreamTags *ppst){
- if( pstReachesConsole(ppst) ) SetConsoleMode(ppst->hx, ppst->consMode);
-}
-# else
-# define restoreConsoleArb(ppst)
-# endif
-/* Say whether FILE* appears to be a console, collect associated info. */
-static short streamOfConsole(FILE *pf, /* out */ PerStreamTags *ppst){
-# if CIO_WIN_WC_XLATE
- short rv = 0;
- DWORD dwCM = SHELL_INVALID_CONS_MODE;
- HANDLE fh = handleOfFile(pf);
- ppst->pf = pf;
- if( INVALID_HANDLE_VALUE != fh ){
- rv = (GetFileType(fh) == FILE_TYPE_CHAR && GetConsoleMode(fh,&dwCM));
- }
- ppst->hx = (rv)? fh : INVALID_HANDLE_VALUE;
- ppst->consMode = dwCM;
- return rv;
-# else
- ppst->pf = pf;
- ppst->reachesConsole = ( (short)isatty(fileno(pf)) );
- return ppst->reachesConsole;
-# endif
+/*
+** Determine if simulated binary mode should be used for output to fd
+*/
+static int UseBinaryWText(FILE *fd){
+ if( fd==stdout || fd==stderr ){
+ return simBinaryStdout;
+ }else{
+ return simBinaryOther;
+ }
}
-# ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
-# define ENABLE_VIRTUAL_TERMINAL_PROCESSING (0x4)
-# endif
-
-# if CIO_WIN_WC_XLATE
-/* Define console modes for use with the Windows Console API. */
-# define SHELL_CONI_MODE \
- (ENABLE_ECHO_INPUT | ENABLE_INSERT_MODE | ENABLE_LINE_INPUT | 0x80 \
- | ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS | ENABLE_PROCESSED_INPUT)
-# define SHELL_CONO_MODE (ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT \
- | ENABLE_VIRTUAL_TERMINAL_PROCESSING)
-# endif
-typedef struct ConsoleInfo {
- PerStreamTags pstSetup[3];
- PerStreamTags pstDesignated[3];
- StreamsAreConsole sacSetup;
-} ConsoleInfo;
+/*
+** Work-alike for the fopen() routine from the standard C library.
+*/
+FILE *sqlite3_fopen(const char *zFilename, const char *zMode){
+ FILE *fp = 0;
+ wchar_t *b1, *b2;
+ int sz1, sz2;
-static short isValidStreamInfo(PerStreamTags *ppst){
- return (ppst->pf != SHELL_INVALID_FILE_PTR);
+ sz1 = (int)strlen(zFilename);
+ sz2 = (int)strlen(zMode);
+ b1 = sqlite3_malloc( (sz1+1)*sizeof(b1[0]) );
+ b2 = sqlite3_malloc( (sz2+1)*sizeof(b1[0]) );
+ if( b1 && b2 ){
+ sz1 = MultiByteToWideChar(CP_UTF8, 0, zFilename, sz1, b1, sz1);
+ b1[sz1] = 0;
+ sz2 = MultiByteToWideChar(CP_UTF8, 0, zMode, sz2, b2, sz2);
+ b2[sz2] = 0;
+ fp = _wfopen(b1, b2);
+ }
+ sqlite3_free(b1);
+ sqlite3_free(b2);
+ simBinaryOther = 0;
+ return fp;
}
-static ConsoleInfo consoleInfo = {
- { /* pstSetup */ PST_INITIALIZER, PST_INITIALIZER, PST_INITIALIZER },
- { /* pstDesignated[] */ PST_INITIALIZER, PST_INITIALIZER, PST_INITIALIZER },
- SAC_NoConsole /* sacSetup */
-};
-SQLITE_INTERNAL_LINKAGE FILE* invalidFileStream = (FILE *)~0;
-
-# if CIO_WIN_WC_XLATE
-static void maybeSetupAsConsole(PerStreamTags *ppst, short odir){
- if( pstReachesConsole(ppst) ){
- DWORD cm = odir? SHELL_CONO_MODE : SHELL_CONI_MODE;
- SetConsoleMode(ppst->hx, cm);
- }
-}
-# else
-# define maybeSetupAsConsole(ppst,odir)
-# endif
+/*
+** Work-alike for the popen() routine from the standard C library.
+*/
+FILE *sqlite3_popen(const char *zCommand, const char *zMode){
+ FILE *fp = 0;
+ wchar_t *b1, *b2;
+ int sz1, sz2;
-SQLITE_INTERNAL_LINKAGE void consoleRenewSetup(void){
-# if CIO_WIN_WC_XLATE
- int ix = 0;
- while( ix < 6 ){
- PerStreamTags *ppst = (ix<3)?
- &consoleInfo.pstSetup[ix] : &consoleInfo.pstDesignated[ix-3];
- maybeSetupAsConsole(ppst, (ix % 3)>0);
- ++ix;
+ sz1 = (int)strlen(zCommand);
+ sz2 = (int)strlen(zMode);
+ b1 = sqlite3_malloc( (sz1+1)*sizeof(b1[0]) );
+ b2 = sqlite3_malloc( (sz2+1)*sizeof(b1[0]) );
+ if( b1 && b2 ){
+ sz1 = MultiByteToWideChar(CP_UTF8, 0, zCommand, sz1, b1, sz1);
+ b1[sz1] = 0;
+ sz2 = MultiByteToWideChar(CP_UTF8, 0, zMode, sz2, b2, sz2);
+ b2[sz2] = 0;
+ fp = _wpopen(b1, b2);
}
-# endif
+ sqlite3_free(b1);
+ sqlite3_free(b2);
+ return fp;
}
-SQLITE_INTERNAL_LINKAGE StreamsAreConsole
-consoleClassifySetup( FILE *pfIn, FILE *pfOut, FILE *pfErr ){
- StreamsAreConsole rv = SAC_NoConsole;
- FILE* apf[3] = { pfIn, pfOut, pfErr };
- int ix;
- for( ix = 2; ix >= 0; --ix ){
- PerStreamTags *ppst = &consoleInfo.pstSetup[ix];
- if( streamOfConsole(apf[ix], ppst) ){
- rv |= (SAC_InConsole<<ix);
+/*
+** Work-alike for fgets() from the standard C library.
+*/
+char *sqlite3_fgets(char *buf, int sz, FILE *in){
+ if( UseWtextForInput(in) ){
+ /* When reading from the command-prompt in Windows, it is necessary
+ ** to use _O_WTEXT input mode to read UTF-16 characters, then translate
+ ** that into UTF-8. Otherwise, non-ASCII characters all get translated
+ ** into '?'.
+ */
+ wchar_t *b1 = sqlite3_malloc( sz*sizeof(wchar_t) );
+ if( b1==0 ) return 0;
+#ifdef SQLITE_USE_W32_FOR_CONSOLE_IO
+ DWORD nRead = 0;
+ if( IsConsole(in)
+ && ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE), b1, sz-1, &nRead, 0)
+ ){
+ b1[nRead] = 0;
+ }else
+#endif
+ {
+ _setmode(_fileno(in), IsConsole(in) ? _O_WTEXT : _O_U8TEXT);
+ if( fgetws(b1, sz/4, in)==0 ){
+ sqlite3_free(b1);
+ return 0;
+ }
}
- consoleInfo.pstDesignated[ix] = *ppst;
- if( ix > 0 ) fflush(apf[ix]);
+ WideCharToMultiByte(CP_UTF8, 0, b1, -1, buf, sz, 0, 0);
+ sqlite3_free(b1);
+ return buf;
+ }else{
+ /* Reading from a file or other input source, just read bytes without
+ ** any translation. */
+ return fgets(buf, sz, in);
}
- consoleInfo.sacSetup = rv;
- consoleRenewSetup();
- return rv;
}
-SQLITE_INTERNAL_LINKAGE void SQLITE_CDECL consoleRestore( void ){
-# if CIO_WIN_WC_XLATE
- static ConsoleInfo *pci = &consoleInfo;
- if( pci->sacSetup ){
- int ix;
- for( ix=0; ix<3; ++ix ){
- if( pci->sacSetup & (SAC_InConsole<<ix) ){
- PerStreamTags *ppst = &pci->pstSetup[ix];
- SetConsoleMode(ppst->hx, ppst->consMode);
+/*
+** Send ASCII text as O_BINARY. But for Unicode characters U+0080 and
+** greater, switch to O_U8TEXT.
+*/
+static void piecemealOutput(wchar_t *b1, int sz, FILE *out){
+ int i;
+ wchar_t c;
+ while( sz>0 ){
+ for(i=0; i<sz && b1[i]>=0x80; i++){}
+ if( i>0 ){
+ c = b1[i];
+ b1[i] = 0;
+ fflush(out);
+ _setmode(_fileno(out), _O_U8TEXT);
+ fputws(b1, out);
+ fflush(out);
+ b1 += i;
+ b1[0] = c;
+ sz -= i;
+ }else{
+ fflush(out);
+ _setmode(_fileno(out), _O_TEXT);
+ _setmode(_fileno(out), _O_BINARY);
+ fwrite(&b1[0], 1, 1, out);
+ for(i=1; i<sz && b1[i]<0x80; i++){
+ fwrite(&b1[i], 1, 1, out);
}
+ fflush(out);
+ _setmode(_fileno(out), _O_U8TEXT);
+ b1 += i;
+ sz -= i;
}
}
-# endif
}
-#endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
-#ifdef SQLITE_CIO_INPUT_REDIR
-/* Say whether given FILE* is among those known, via either
-** consoleClassifySetup() or set{Output,Error}Stream, as
-** readable, and return an associated PerStreamTags pointer
-** if so. Otherwise, return 0.
+/*
+** Work-alike for fputs() from the standard C library.
*/
-static PerStreamTags * isKnownReadable(FILE *pf){
- static PerStreamTags *apst[] = {
- &consoleInfo.pstDesignated[0], &consoleInfo.pstSetup[0], 0
- };
- int ix = 0;
- do {
- if( apst[ix]->pf == pf ) break;
- } while( apst[++ix] != 0 );
- return apst[ix];
-}
+int sqlite3_fputs(const char *z, FILE *out){
+ if( !UseWtextForOutput(out) ){
+ /* Writing to a file or other destination, just write bytes without
+ ** any translation. */
+ return fputs(z, out);
+ }else{
+ /* One must use UTF16 in order to get unicode support when writing
+ ** to the console on Windows.
+ */
+ int sz = (int)strlen(z);
+ wchar_t *b1 = sqlite3_malloc( (sz+1)*sizeof(wchar_t) );
+ if( b1==0 ) return 0;
+ sz = MultiByteToWideChar(CP_UTF8, 0, z, sz, b1, sz);
+ b1[sz] = 0;
+
+#ifdef SQLITE_USE_W32_FOR_CONSOLE_IO
+ DWORD nWr = 0;
+ if( IsConsole(out)
+ && WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE),b1,sz,&nWr,0)
+ ){
+ /* If writing to the console, then the WriteConsoleW() is all we
+ ** need to do. */
+ }else
#endif
-
-#ifndef SQLITE_CIO_NO_TRANSLATE
-/* Say whether given FILE* is among those known, via either
-** consoleClassifySetup() or set{Output,Error}Stream, as
-** writable, and return an associated PerStreamTags pointer
-** if so. Otherwise, return 0.
-*/
-static PerStreamTags * isKnownWritable(FILE *pf){
- static PerStreamTags *apst[] = {
- &consoleInfo.pstDesignated[1], &consoleInfo.pstDesignated[2],
- &consoleInfo.pstSetup[1], &consoleInfo.pstSetup[2], 0
- };
- int ix = 0;
- do {
- if( apst[ix]->pf == pf ) break;
- } while( apst[++ix] != 0 );
- return apst[ix];
-}
-
-static FILE *designateEmitStream(FILE *pf, unsigned chix){
- FILE *rv = consoleInfo.pstDesignated[chix].pf;
- if( pf == invalidFileStream ) return rv;
- else{
- /* Setting a possibly new output stream. */
- PerStreamTags *ppst = isKnownWritable(pf);
- if( ppst != 0 ){
- PerStreamTags pst = *ppst;
- consoleInfo.pstDesignated[chix] = pst;
- }else streamOfConsole(pf, &consoleInfo.pstDesignated[chix]);
- }
- return rv;
-}
-
-SQLITE_INTERNAL_LINKAGE FILE *setOutputStream(FILE *pf){
- return designateEmitStream(pf, 1);
-}
-# ifdef CONSIO_SET_ERROR_STREAM
-SQLITE_INTERNAL_LINKAGE FILE *setErrorStream(FILE *pf){
- return designateEmitStream(pf, 2);
-}
-# endif
-#endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
-
-#ifndef SQLITE_CIO_NO_SETMODE
-# if CIO_WIN_WC_XLATE
-static void setModeFlushQ(FILE *pf, short bFlush, int mode){
- if( bFlush ) fflush(pf);
- _setmode(_fileno(pf), mode);
-}
-# else
-# define setModeFlushQ(f, b, m) if(b) fflush(f)
-# endif
-
-SQLITE_INTERNAL_LINKAGE void setBinaryMode(FILE *pf, short bFlush){
- setModeFlushQ(pf, bFlush, _O_BINARY);
-}
-SQLITE_INTERNAL_LINKAGE void setTextMode(FILE *pf, short bFlush){
- setModeFlushQ(pf, bFlush, _O_TEXT);
-}
-# undef setModeFlushQ
-
-#else /* defined(SQLITE_CIO_NO_SETMODE) */
-# define setBinaryMode(f, bFlush) do{ if((bFlush)) fflush(f); }while(0)
-# define setTextMode(f, bFlush) do{ if((bFlush)) fflush(f); }while(0)
-#endif /* defined(SQLITE_CIO_NO_SETMODE) */
-
-#ifndef SQLITE_CIO_NO_TRANSLATE
-# if CIO_WIN_WC_XLATE
-/* Write buffer cBuf as output to stream known to reach console,
-** limited to ncTake char's. Return ncTake on success, else 0. */
-static int conZstrEmit(PerStreamTags *ppst, const char *z, int ncTake){
- int rv = 0;
- if( z!=NULL ){
- int nwc = MultiByteToWideChar(CP_UTF8,0, z,ncTake, 0,0);
- if( nwc > 0 ){
- WCHAR *zw = sqlite3_malloc64(nwc*sizeof(WCHAR));
- if( zw!=NULL ){
- nwc = MultiByteToWideChar(CP_UTF8,0, z,ncTake, zw,nwc);
- if( nwc > 0 ){
- /* Translation from UTF-8 to UTF-16, then WCHARs out. */
- if( WriteConsoleW(ppst->hx, zw,nwc, 0, NULL) ){
- rv = ncTake;
- }
- }
- sqlite3_free(zw);
+ {
+ /* As long as SQLITE_USE_W32_FOR_CONSOLE_IO is not defined, or for
+ ** non-console I/O even if that macro is defined, write using the
+ ** standard library. */
+ _setmode(_fileno(out), _O_U8TEXT);
+ if( UseBinaryWText(out) ){
+ piecemealOutput(b1, sz, out);
+ }else{
+ fputws(b1, out);
}
}
+ sqlite3_free(b1);
+ return 0;
}
- return rv;
}
-/* For {f,o,e}PrintfUtf8() when stream is known to reach console. */
-static int conioVmPrintf(PerStreamTags *ppst, const char *zFormat, va_list ap){
- char *z = sqlite3_vmprintf(zFormat, ap);
- if( z ){
- int rv = conZstrEmit(ppst, z, (int)strlen(z));
- sqlite3_free(z);
- return rv;
- }else return 0;
-}
-# endif /* CIO_WIN_WC_XLATE */
-# ifdef CONSIO_GET_EMIT_STREAM
-static PerStreamTags * getDesignatedEmitStream(FILE *pf, unsigned chix,
- PerStreamTags *ppst){
- PerStreamTags *rv = isKnownWritable(pf);
- short isValid = (rv!=0)? isValidStreamInfo(rv) : 0;
- if( rv != 0 && isValid ) return rv;
- streamOfConsole(pf, ppst);
- return ppst;
-}
-# endif
-
-/* Get stream info, either for designated output or error stream when
-** chix equals 1 or 2, or for an arbitrary stream when chix == 0.
-** In either case, ppst references a caller-owned PerStreamTags
-** struct which may be filled in if none of the known writable
-** streams is being held by consoleInfo. The ppf parameter is a
-** byref output when chix!=0 and a byref input when chix==0.
- */
-static PerStreamTags *
-getEmitStreamInfo(unsigned chix, PerStreamTags *ppst,
- /* in/out */ FILE **ppf){
- PerStreamTags *ppstTry;
- FILE *pfEmit;
- if( chix > 0 ){
- ppstTry = &consoleInfo.pstDesignated[chix];
- if( !isValidStreamInfo(ppstTry) ){
- ppstTry = &consoleInfo.pstSetup[chix];
- pfEmit = ppst->pf;
- }else pfEmit = ppstTry->pf;
- if( !isValidStreamInfo(ppstTry) ){
- pfEmit = (chix > 1)? stderr : stdout;
- ppstTry = ppst;
- streamOfConsole(pfEmit, ppstTry);
- }
- *ppf = pfEmit;
- }else{
- ppstTry = isKnownWritable(*ppf);
- if( ppstTry != 0 ) return ppstTry;
- streamOfConsole(*ppf, ppst);
- return ppst;
- }
- return ppstTry;
-}
-
-SQLITE_INTERNAL_LINKAGE int oPrintfUtf8(const char *zFormat, ...){
- va_list ap;
- int rv;
- FILE *pfOut;
- PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
-# if CIO_WIN_WC_XLATE
- PerStreamTags *ppst = getEmitStreamInfo(1, &pst, &pfOut);
-# else
- getEmitStreamInfo(1, &pst, &pfOut);
-# endif
- assert(zFormat!=0);
- va_start(ap, zFormat);
-# if CIO_WIN_WC_XLATE
- if( pstReachesConsole(ppst) ){
- rv = conioVmPrintf(ppst, zFormat, ap);
- }else{
-# endif
- rv = vfprintf(pfOut, zFormat, ap);
-# if CIO_WIN_WC_XLATE
- }
-# endif
- va_end(ap);
- return rv;
-}
+/*
+** Work-alike for fprintf() from the standard C library.
+*/
+int sqlite3_fprintf(FILE *out, const char *zFormat, ...){
+ int rc;
+ if( UseWtextForOutput(out) ){
+ /* When writing to the command-prompt in Windows, it is necessary
+ ** to use _O_WTEXT input mode and write UTF-16 characters.
+ */
+ char *z;
+ va_list ap;
-SQLITE_INTERNAL_LINKAGE int ePrintfUtf8(const char *zFormat, ...){
- va_list ap;
- int rv;
- FILE *pfErr;
- PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
-# if CIO_WIN_WC_XLATE
- PerStreamTags *ppst = getEmitStreamInfo(2, &pst, &pfErr);
-# else
- getEmitStreamInfo(2, &pst, &pfErr);
-# endif
- assert(zFormat!=0);
- va_start(ap, zFormat);
-# if CIO_WIN_WC_XLATE
- if( pstReachesConsole(ppst) ){
- rv = conioVmPrintf(ppst, zFormat, ap);
+ va_start(ap, zFormat);
+ z = sqlite3_vmprintf(zFormat, ap);
+ va_end(ap);
+ sqlite3_fputs(z, out);
+ rc = (int)strlen(z);
+ sqlite3_free(z);
}else{
-# endif
- rv = vfprintf(pfErr, zFormat, ap);
-# if CIO_WIN_WC_XLATE
+ /* Writing to a file or other destination, just write bytes without
+ ** any translation. */
+ va_list ap;
+ va_start(ap, zFormat);
+ rc = vfprintf(out, zFormat, ap);
+ va_end(ap);
}
-# endif
- va_end(ap);
- return rv;
+ return rc;
}
-SQLITE_INTERNAL_LINKAGE int fPrintfUtf8(FILE *pfO, const char *zFormat, ...){
- va_list ap;
- int rv;
- PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
-# if CIO_WIN_WC_XLATE
- PerStreamTags *ppst = getEmitStreamInfo(0, &pst, &pfO);
-# else
- getEmitStreamInfo(0, &pst, &pfO);
-# endif
- assert(zFormat!=0);
- va_start(ap, zFormat);
-# if CIO_WIN_WC_XLATE
- if( pstReachesConsole(ppst) ){
- maybeSetupAsConsole(ppst, 1);
- rv = conioVmPrintf(ppst, zFormat, ap);
- if( 0 == isKnownWritable(ppst->pf) ) restoreConsoleArb(ppst);
+/*
+** Set the mode for an output stream. mode argument is typically _O_BINARY or
+** _O_TEXT.
+*/
+void sqlite3_fsetmode(FILE *fp, int mode){
+ if( !UseWtextForOutput(fp) ){
+ fflush(fp);
+ _setmode(_fileno(fp), mode);
+ }else if( fp==stdout || fp==stderr ){
+ simBinaryStdout = (mode==_O_BINARY);
}else{
-# endif
- rv = vfprintf(pfO, zFormat, ap);
-# if CIO_WIN_WC_XLATE
- }
-# endif
- va_end(ap);
- return rv;
-}
-
-SQLITE_INTERNAL_LINKAGE int fPutsUtf8(const char *z, FILE *pfO){
- PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
-# if CIO_WIN_WC_XLATE
- PerStreamTags *ppst = getEmitStreamInfo(0, &pst, &pfO);
-# else
- getEmitStreamInfo(0, &pst, &pfO);
-# endif
- assert(z!=0);
-# if CIO_WIN_WC_XLATE
- if( pstReachesConsole(ppst) ){
- int rv;
- maybeSetupAsConsole(ppst, 1);
- rv = conZstrEmit(ppst, z, (int)strlen(z));
- if( 0 == isKnownWritable(ppst->pf) ) restoreConsoleArb(ppst);
- return rv;
- }else {
-# endif
- return (fputs(z, pfO)<0)? 0 : (int)strlen(z);
-# if CIO_WIN_WC_XLATE
+ simBinaryOther = (mode==_O_BINARY);
}
-# endif
}
-SQLITE_INTERNAL_LINKAGE int ePutsUtf8(const char *z){
- FILE *pfErr;
- PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
-# if CIO_WIN_WC_XLATE
- PerStreamTags *ppst = getEmitStreamInfo(2, &pst, &pfErr);
-# else
- getEmitStreamInfo(2, &pst, &pfErr);
-# endif
- assert(z!=0);
-# if CIO_WIN_WC_XLATE
- if( pstReachesConsole(ppst) ) return conZstrEmit(ppst, z, (int)strlen(z));
- else {
-# endif
- return (fputs(z, pfErr)<0)? 0 : (int)strlen(z);
-# if CIO_WIN_WC_XLATE
- }
-# endif
-}
-
-SQLITE_INTERNAL_LINKAGE int oPutsUtf8(const char *z){
- FILE *pfOut;
- PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
-# if CIO_WIN_WC_XLATE
- PerStreamTags *ppst = getEmitStreamInfo(1, &pst, &pfOut);
-# else
- getEmitStreamInfo(1, &pst, &pfOut);
-# endif
- assert(z!=0);
-# if CIO_WIN_WC_XLATE
- if( pstReachesConsole(ppst) ) return conZstrEmit(ppst, z, (int)strlen(z));
- else {
-# endif
- return (fputs(z, pfOut)<0)? 0 : (int)strlen(z);
-# if CIO_WIN_WC_XLATE
- }
-# endif
-}
+#endif /* defined(_WIN32) */
-#endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
+/************************* End ../ext/misc/sqlite3_stdio.c ********************/
-#if !(defined(SQLITE_CIO_NO_UTF8SCAN) && defined(SQLITE_CIO_NO_TRANSLATE))
-/* Skip over as much z[] input char sequence as is valid UTF-8,
-** limited per nAccept char's or whole characters and containing
-** no char cn such that ((1<<cn) & ccm)!=0. On return, the
-** sequence z:return (inclusive:exclusive) is validated UTF-8.
-** Limit: nAccept>=0 => char count, nAccept<0 => character
- */
-SQLITE_INTERNAL_LINKAGE const char*
-zSkipValidUtf8(const char *z, int nAccept, long ccm){
- int ng = (nAccept<0)? -nAccept : 0;
- const char *pcLimit = (nAccept>=0)? z+nAccept : 0;
- assert(z!=0);
- while( (pcLimit)? (z<pcLimit) : (ng-- != 0) ){
- char c = *z;
- if( (c & 0x80) == 0 ){
- if( ccm != 0L && c < 0x20 && ((1L<<c) & ccm) != 0 ) return z;
- ++z; /* ASCII */
- }else if( (c & 0xC0) != 0xC0 ) return z; /* not a lead byte */
- else{
- const char *zt = z+1; /* Got lead byte, look at trail bytes.*/
- do{
- if( pcLimit && zt >= pcLimit ) return z;
- else{
- char ct = *zt++;
- if( ct==0 || (zt-z)>4 || (ct & 0xC0)!=0x80 ){
- /* Trailing bytes are too few, too many, or invalid. */
- return z;
- }
- }
- } while( ((c <<= 1) & 0x40) == 0x40 ); /* Eat lead byte's count. */
- z = zt;
- }
- }
- return z;
-}
-#endif /*!(defined(SQLITE_CIO_NO_UTF8SCAN)&&defined(SQLITE_CIO_NO_TRANSLATE))*/
-
-#ifndef SQLITE_CIO_NO_TRANSLATE
-# ifdef CONSIO_SPUTB
-SQLITE_INTERNAL_LINKAGE int
-fPutbUtf8(FILE *pfO, const char *cBuf, int nAccept){
- assert(pfO!=0);
-# if CIO_WIN_WC_XLATE
- PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
- PerStreamTags *ppst = getEmitStreamInfo(0, &pst, &pfO);
- if( pstReachesConsole(ppst) ){
- int rv;
- maybeSetupAsConsole(ppst, 1);
- rv = conZstrEmit(ppst, cBuf, nAccept);
- if( 0 == isKnownWritable(ppst->pf) ) restoreConsoleArb(ppst);
- return rv;
- }else {
-# endif
- return (int)fwrite(cBuf, 1, nAccept, pfO);
-# if CIO_WIN_WC_XLATE
- }
-# endif
-}
-# endif
-
-SQLITE_INTERNAL_LINKAGE int
-oPutbUtf8(const char *cBuf, int nAccept){
- FILE *pfOut;
- PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
-# if CIO_WIN_WC_XLATE
- PerStreamTags *ppst = getEmitStreamInfo(1, &pst, &pfOut);
-# else
- getEmitStreamInfo(1, &pst, &pfOut);
-# endif
-# if CIO_WIN_WC_XLATE
- if( pstReachesConsole(ppst) ){
- return conZstrEmit(ppst, cBuf, nAccept);
- }else {
-# endif
- return (int)fwrite(cBuf, 1, nAccept, pfOut);
-# if CIO_WIN_WC_XLATE
- }
-# endif
-}
-
-# ifdef CONSIO_EPUTB
-SQLITE_INTERNAL_LINKAGE int
-ePutbUtf8(const char *cBuf, int nAccept){
- FILE *pfErr;
- PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
- PerStreamTags *ppst = getEmitStreamInfo(2, &pst, &pfErr);
-# if CIO_WIN_WC_XLATE
- if( pstReachesConsole(ppst) ){
- return conZstrEmit(ppst, cBuf, nAccept);
- }else {
-# endif
- return (int)fwrite(cBuf, 1, nAccept, pfErr);
-# if CIO_WIN_WC_XLATE
- }
-# endif
-}
-# endif /* defined(CONSIO_EPUTB) */
-
-SQLITE_INTERNAL_LINKAGE char* fGetsUtf8(char *cBuf, int ncMax, FILE *pfIn){
- if( pfIn==0 ) pfIn = stdin;
-# if CIO_WIN_WC_XLATE
- if( pfIn == consoleInfo.pstSetup[0].pf
- && (consoleInfo.sacSetup & SAC_InConsole)!=0 ){
-# if CIO_WIN_WC_XLATE==1
-# define SHELL_GULP 150 /* Count of WCHARS to be gulped at a time */
- WCHAR wcBuf[SHELL_GULP+1];
- int lend = 0, noc = 0;
- if( ncMax > 0 ) cBuf[0] = 0;
- while( noc < ncMax-8-1 && !lend ){
- /* There is room for at least 2 more characters and a 0-terminator. */
- int na = (ncMax > SHELL_GULP*4+1 + noc)? SHELL_GULP : (ncMax-1 - noc)/4;
-# undef SHELL_GULP
- DWORD nbr = 0;
- BOOL bRC = ReadConsoleW(consoleInfo.pstSetup[0].hx, wcBuf, na, &nbr, 0);
- if( bRC && nbr>0 && (wcBuf[nbr-1]&0xF800)==0xD800 ){
- /* Last WHAR read is first of a UTF-16 surrogate pair. Grab its mate. */
- DWORD nbrx;
- bRC &= ReadConsoleW(consoleInfo.pstSetup[0].hx, wcBuf+nbr, 1, &nbrx, 0);
- if( bRC ) nbr += nbrx;
- }
- if( !bRC || (noc==0 && nbr==0) ) return 0;
- if( nbr > 0 ){
- int nmb = WideCharToMultiByte(CP_UTF8, 0, wcBuf,nbr,0,0,0,0);
- if( nmb != 0 && noc+nmb <= ncMax ){
- int iseg = noc;
- nmb = WideCharToMultiByte(CP_UTF8, 0, wcBuf,nbr,cBuf+noc,nmb,0,0);
- noc += nmb;
- /* Fixup line-ends as coded by Windows for CR (or "Enter".)
- ** This is done without regard for any setMode{Text,Binary}()
- ** call that might have been done on the interactive input.
- */
- if( noc > 0 ){
- if( cBuf[noc-1]=='\n' ){
- lend = 1;
- if( noc > 1 && cBuf[noc-2]=='\r' ) cBuf[--noc-1] = '\n';
- }
- }
- /* Check for ^Z (anywhere in line) too, to act as EOF. */
- while( iseg < noc ){
- if( cBuf[iseg]=='\x1a' ){
- noc = iseg; /* Chop ^Z and anything following. */
- lend = 1; /* Counts as end of line too. */
- break;
- }
- ++iseg;
- }
- }else break; /* Drop apparent garbage in. (Could assert.) */
- }else break;
- }
- /* If got nothing, (after ^Z chop), must be at end-of-file. */
- if( noc > 0 ){
- cBuf[noc] = 0;
- return cBuf;
- }else return 0;
-# endif
- }else{
-# endif
- return fgets(cBuf, ncMax, pfIn);
-# if CIO_WIN_WC_XLATE
- }
-# endif
-}
-#endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
+/* Use console I/O package as a direct INCLUDE. */
+#define SQLITE_INTERNAL_LINKAGE static
-#if defined(_MSC_VER)
-# pragma warning(default : 4204)
+#ifdef SQLITE_SHELL_FIDDLE
+/* Deselect most features from the console I/O package for Fiddle. */
+# define SQLITE_CIO_NO_REDIRECT
+# define SQLITE_CIO_NO_CLASSIFY
+# define SQLITE_CIO_NO_TRANSLATE
+# define SQLITE_CIO_NO_SETMODE
+# define SQLITE_CIO_NO_FLUSH
#endif
-#undef SHELL_INVALID_FILE_PTR
-
-/************************* End ../ext/consio/console_io.c ********************/
-
-#ifndef SQLITE_SHELL_FIDDLE
-
-/* From here onward, fgets() is redirected to the console_io library. */
-# define fgets(b,n,f) fGetsUtf8(b,n,f)
-/*
- * Define macros for emitting output text in various ways:
- * sputz(s, z) => emit 0-terminated string z to given stream s
- * sputf(s, f, ...) => emit varargs per format f to given stream s
- * oputz(z) => emit 0-terminated string z to default stream
- * oputf(f, ...) => emit varargs per format f to default stream
- * eputz(z) => emit 0-terminated string z to error stream
- * eputf(f, ...) => emit varargs per format f to error stream
- * oputb(b, n) => emit char buffer b[0..n-1] to default stream
- *
- * Note that the default stream is whatever has been last set via:
- * setOutputStream(FILE *pf)
- * This is normally the stream that CLI normal output goes to.
- * For the stand-alone CLI, it is stdout with no .output redirect.
- *
- * The ?putz(z) forms are required for the Fiddle builds for string literal
- * output, in aid of enforcing format string to argument correspondence.
- */
-# define sputz(s,z) fPutsUtf8(z,s)
-# define sputf fPrintfUtf8
-# define oputz(z) oPutsUtf8(z)
-# define oputf oPrintfUtf8
-# define eputz(z) ePutsUtf8(z)
-# define eputf ePrintfUtf8
-# define oputb(buf,na) oPutbUtf8(buf,na)
-
-#else
-/* For Fiddle, all console handling and emit redirection is omitted. */
-/* These next 3 macros are for emitting formatted output. When complaints
- * from the WASM build are issued for non-formatted output, (when a mere
- * string literal is to be emitted, the ?putz(z) forms should be used.
- * (This permits compile-time checking of format string / argument mismatch.)
- */
-# define oputf(fmt, ...) printf(fmt,__VA_ARGS__)
-# define eputf(fmt, ...) fprintf(stderr,fmt,__VA_ARGS__)
-# define sputf(fp,fmt, ...) fprintf(fp,fmt,__VA_ARGS__)
-/* These next 3 macros are for emitting simple string literals. */
-# define oputz(z) fputs(z,stdout)
-# define eputz(z) fputs(z,stderr)
-# define sputz(fp,z) fputs(z,fp)
-# define oputb(buf,na) fwrite(buf,1,na,stdout)
-#endif
+#define eputz(z) sqlite3_fputs(z,stderr)
+#define sputz(fp,z) sqlite3_fputs(z,fp)
/* True if the timer is enabled */
static int enableTimer = 0;
@@ -1329,6 +677,7 @@ struct rusage {
#define getrusage(A,B) memset(B,0,sizeof(*B))
#endif
+
/* Saved resource information for the beginning of an operation */
static struct rusage sBegin; /* CPU time at start */
static sqlite3_int64 iBegin; /* Wall-clock time at start */
@@ -1352,12 +701,12 @@ static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
/*
** Print the timing results.
*/
-static void endTimer(void){
+static void endTimer(FILE *out){
if( enableTimer ){
sqlite3_int64 iEnd = timeOfDay();
struct rusage sEnd;
getrusage(RUSAGE_SELF, &sEnd);
- sputf(stdout, "Run Time: real %.3f user %f sys %f\n",
+ sqlite3_fprintf(out, "Run Time: real %.3f user %f sys %f\n",
(iEnd - iBegin)*0.001,
timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
@@ -1365,7 +714,7 @@ static void endTimer(void){
}
#define BEGIN_TIMER beginTimer()
-#define END_TIMER endTimer()
+#define END_TIMER(X) endTimer(X)
#define HAS_TIMER 1
#elif (defined(_WIN32) || defined(WIN32))
@@ -1431,12 +780,12 @@ static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
/*
** Print the timing results.
*/
-static void endTimer(void){
+static void endTimer(FILE *out){
if( enableTimer && getProcessTimesAddr){
FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
sqlite3_int64 ftWallEnd = timeOfDay();
getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
- sputf(stdout, "Run Time: real %.3f user %f sys %f\n",
+ sqlite3_fprintf(out, "Run Time: real %.3f user %f sys %f\n",
(ftWallEnd - ftWallBegin)*0.001,
timeDiff(&ftUserBegin, &ftUserEnd),
timeDiff(&ftKernelBegin, &ftKernelEnd));
@@ -1444,12 +793,12 @@ static void endTimer(void){
}
#define BEGIN_TIMER beginTimer()
-#define END_TIMER endTimer()
+#define END_TIMER(X) endTimer(X)
#define HAS_TIMER hasTimer()
#else
#define BEGIN_TIMER
-#define END_TIMER
+#define END_TIMER(X) /*no-op*/
#define HAS_TIMER 0
#endif
@@ -1504,7 +853,7 @@ static char *Argv0;
** Prompt strings. Initialized in main. Settable with
** .prompt main continue
*/
-#define PROMPT_LEN_MAX 20
+#define PROMPT_LEN_MAX 128
/* First line prompt. default: "sqlite> " */
static char mainPrompt[PROMPT_LEN_MAX];
/* Continuation prompt. default: " ...> " */
@@ -1523,6 +872,14 @@ static char *shell_strncpy(char *dest, const char *src, size_t n){
}
/*
+** strcpy() workalike to squelch an unwarranted link-time warning
+** from OpenBSD.
+*/
+static void shell_strcpy(char *dest, const char *src){
+ while( (*(dest++) = *(src++))!=0 ){}
+}
+
+/*
** Optionally disable dynamic continuation prompt.
** Unless disabled, the continuation prompt shows open SQL lexemes if any,
** or open parentheses level if non-zero, or continuation prompt as set.
@@ -1587,7 +944,7 @@ static char *dynamicContinuePrompt(void){
size_t ncp = strlen(continuePrompt);
size_t ndp = strlen(dynPrompt.zScannerAwaits);
if( ndp > ncp-3 ) return continuePrompt;
- strcpy(dynPrompt.dynamicPrompt, dynPrompt.zScannerAwaits);
+ shell_strcpy(dynPrompt.dynamicPrompt, dynPrompt.zScannerAwaits);
while( ndp<3 ) dynPrompt.dynamicPrompt[ndp++] = ' ';
shell_strncpy(dynPrompt.dynamicPrompt+3, continuePrompt+3,
PROMPT_LEN_MAX-4);
@@ -1642,37 +999,232 @@ static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
va_start(ap, zFormat);
z = sqlite3_vmprintf(zFormat, ap);
va_end(ap);
- sputf(iotrace, "%s", z);
+ sqlite3_fprintf(iotrace, "%s", z);
sqlite3_free(z);
}
#endif
+/* Lookup table to estimate the number of columns consumed by a Unicode
+** character.
+*/
+static const struct {
+ unsigned char w; /* Width of the character in columns */
+ int iFirst; /* First character in a span having this width */
+} aUWidth[] = {
+ /* {1, 0x00000}, */
+ {0, 0x00300}, {1, 0x00370}, {0, 0x00483}, {1, 0x00487}, {0, 0x00488},
+ {1, 0x0048a}, {0, 0x00591}, {1, 0x005be}, {0, 0x005bf}, {1, 0x005c0},
+ {0, 0x005c1}, {1, 0x005c3}, {0, 0x005c4}, {1, 0x005c6}, {0, 0x005c7},
+ {1, 0x005c8}, {0, 0x00600}, {1, 0x00604}, {0, 0x00610}, {1, 0x00616},
+ {0, 0x0064b}, {1, 0x0065f}, {0, 0x00670}, {1, 0x00671}, {0, 0x006d6},
+ {1, 0x006e5}, {0, 0x006e7}, {1, 0x006e9}, {0, 0x006ea}, {1, 0x006ee},
+ {0, 0x0070f}, {1, 0x00710}, {0, 0x00711}, {1, 0x00712}, {0, 0x00730},
+ {1, 0x0074b}, {0, 0x007a6}, {1, 0x007b1}, {0, 0x007eb}, {1, 0x007f4},
+ {0, 0x00901}, {1, 0x00903}, {0, 0x0093c}, {1, 0x0093d}, {0, 0x00941},
+ {1, 0x00949}, {0, 0x0094d}, {1, 0x0094e}, {0, 0x00951}, {1, 0x00955},
+ {0, 0x00962}, {1, 0x00964}, {0, 0x00981}, {1, 0x00982}, {0, 0x009bc},
+ {1, 0x009bd}, {0, 0x009c1}, {1, 0x009c5}, {0, 0x009cd}, {1, 0x009ce},
+ {0, 0x009e2}, {1, 0x009e4}, {0, 0x00a01}, {1, 0x00a03}, {0, 0x00a3c},
+ {1, 0x00a3d}, {0, 0x00a41}, {1, 0x00a43}, {0, 0x00a47}, {1, 0x00a49},
+ {0, 0x00a4b}, {1, 0x00a4e}, {0, 0x00a70}, {1, 0x00a72}, {0, 0x00a81},
+ {1, 0x00a83}, {0, 0x00abc}, {1, 0x00abd}, {0, 0x00ac1}, {1, 0x00ac6},
+ {0, 0x00ac7}, {1, 0x00ac9}, {0, 0x00acd}, {1, 0x00ace}, {0, 0x00ae2},
+ {1, 0x00ae4}, {0, 0x00b01}, {1, 0x00b02}, {0, 0x00b3c}, {1, 0x00b3d},
+ {0, 0x00b3f}, {1, 0x00b40}, {0, 0x00b41}, {1, 0x00b44}, {0, 0x00b4d},
+ {1, 0x00b4e}, {0, 0x00b56}, {1, 0x00b57}, {0, 0x00b82}, {1, 0x00b83},
+ {0, 0x00bc0}, {1, 0x00bc1}, {0, 0x00bcd}, {1, 0x00bce}, {0, 0x00c3e},
+ {1, 0x00c41}, {0, 0x00c46}, {1, 0x00c49}, {0, 0x00c4a}, {1, 0x00c4e},
+ {0, 0x00c55}, {1, 0x00c57}, {0, 0x00cbc}, {1, 0x00cbd}, {0, 0x00cbf},
+ {1, 0x00cc0}, {0, 0x00cc6}, {1, 0x00cc7}, {0, 0x00ccc}, {1, 0x00cce},
+ {0, 0x00ce2}, {1, 0x00ce4}, {0, 0x00d41}, {1, 0x00d44}, {0, 0x00d4d},
+ {1, 0x00d4e}, {0, 0x00dca}, {1, 0x00dcb}, {0, 0x00dd2}, {1, 0x00dd5},
+ {0, 0x00dd6}, {1, 0x00dd7}, {0, 0x00e31}, {1, 0x00e32}, {0, 0x00e34},
+ {1, 0x00e3b}, {0, 0x00e47}, {1, 0x00e4f}, {0, 0x00eb1}, {1, 0x00eb2},
+ {0, 0x00eb4}, {1, 0x00eba}, {0, 0x00ebb}, {1, 0x00ebd}, {0, 0x00ec8},
+ {1, 0x00ece}, {0, 0x00f18}, {1, 0x00f1a}, {0, 0x00f35}, {1, 0x00f36},
+ {0, 0x00f37}, {1, 0x00f38}, {0, 0x00f39}, {1, 0x00f3a}, {0, 0x00f71},
+ {1, 0x00f7f}, {0, 0x00f80}, {1, 0x00f85}, {0, 0x00f86}, {1, 0x00f88},
+ {0, 0x00f90}, {1, 0x00f98}, {0, 0x00f99}, {1, 0x00fbd}, {0, 0x00fc6},
+ {1, 0x00fc7}, {0, 0x0102d}, {1, 0x01031}, {0, 0x01032}, {1, 0x01033},
+ {0, 0x01036}, {1, 0x01038}, {0, 0x01039}, {1, 0x0103a}, {0, 0x01058},
+ {1, 0x0105a}, {2, 0x01100}, {0, 0x01160}, {1, 0x01200}, {0, 0x0135f},
+ {1, 0x01360}, {0, 0x01712}, {1, 0x01715}, {0, 0x01732}, {1, 0x01735},
+ {0, 0x01752}, {1, 0x01754}, {0, 0x01772}, {1, 0x01774}, {0, 0x017b4},
+ {1, 0x017b6}, {0, 0x017b7}, {1, 0x017be}, {0, 0x017c6}, {1, 0x017c7},
+ {0, 0x017c9}, {1, 0x017d4}, {0, 0x017dd}, {1, 0x017de}, {0, 0x0180b},
+ {1, 0x0180e}, {0, 0x018a9}, {1, 0x018aa}, {0, 0x01920}, {1, 0x01923},
+ {0, 0x01927}, {1, 0x01929}, {0, 0x01932}, {1, 0x01933}, {0, 0x01939},
+ {1, 0x0193c}, {0, 0x01a17}, {1, 0x01a19}, {0, 0x01b00}, {1, 0x01b04},
+ {0, 0x01b34}, {1, 0x01b35}, {0, 0x01b36}, {1, 0x01b3b}, {0, 0x01b3c},
+ {1, 0x01b3d}, {0, 0x01b42}, {1, 0x01b43}, {0, 0x01b6b}, {1, 0x01b74},
+ {0, 0x01dc0}, {1, 0x01dcb}, {0, 0x01dfe}, {1, 0x01e00}, {0, 0x0200b},
+ {1, 0x02010}, {0, 0x0202a}, {1, 0x0202f}, {0, 0x02060}, {1, 0x02064},
+ {0, 0x0206a}, {1, 0x02070}, {0, 0x020d0}, {1, 0x020f0}, {2, 0x02329},
+ {1, 0x0232b}, {2, 0x02e80}, {0, 0x0302a}, {2, 0x03030}, {1, 0x0303f},
+ {2, 0x03040}, {0, 0x03099}, {2, 0x0309b}, {1, 0x0a4d0}, {0, 0x0a806},
+ {1, 0x0a807}, {0, 0x0a80b}, {1, 0x0a80c}, {0, 0x0a825}, {1, 0x0a827},
+ {2, 0x0ac00}, {1, 0x0d7a4}, {2, 0x0f900}, {1, 0x0fb00}, {0, 0x0fb1e},
+ {1, 0x0fb1f}, {0, 0x0fe00}, {2, 0x0fe10}, {1, 0x0fe1a}, {0, 0x0fe20},
+ {1, 0x0fe24}, {2, 0x0fe30}, {1, 0x0fe70}, {0, 0x0feff}, {2, 0x0ff00},
+ {1, 0x0ff61}, {2, 0x0ffe0}, {1, 0x0ffe7}, {0, 0x0fff9}, {1, 0x0fffc},
+ {0, 0x10a01}, {1, 0x10a04}, {0, 0x10a05}, {1, 0x10a07}, {0, 0x10a0c},
+ {1, 0x10a10}, {0, 0x10a38}, {1, 0x10a3b}, {0, 0x10a3f}, {1, 0x10a40},
+ {0, 0x1d167}, {1, 0x1d16a}, {0, 0x1d173}, {1, 0x1d183}, {0, 0x1d185},
+ {1, 0x1d18c}, {0, 0x1d1aa}, {1, 0x1d1ae}, {0, 0x1d242}, {1, 0x1d245},
+ {2, 0x20000}, {1, 0x2fffe}, {2, 0x30000}, {1, 0x3fffe}, {0, 0xe0001},
+ {1, 0xe0002}, {0, 0xe0020}, {1, 0xe0080}, {0, 0xe0100}, {1, 0xe01f0}
+};
+
+/*
+** Return an estimate of the width, in columns, for the single Unicode
+** character c. For normal characters, the answer is always 1. But the
+** estimate might be 0 or 2 for zero-width and double-width characters.
+**
+** Different display devices display unicode using different widths. So
+** it is impossible to know that true display width with 100% accuracy.
+** Inaccuracies in the width estimates might cause columns to be misaligned.
+** Unfortunately, there is nothing we can do about that.
+*/
+int cli_wcwidth(int c){
+ int iFirst, iLast;
+
+ /* Fast path for common characters */
+ if( c<=0x300 ) return 1;
+
+ /* The general case */
+ iFirst = 0;
+ iLast = sizeof(aUWidth)/sizeof(aUWidth[0]) - 1;
+ while( iFirst<iLast-1 ){
+ int iMid = (iFirst+iLast)/2;
+ int cMid = aUWidth[iMid].iFirst;
+ if( cMid < c ){
+ iFirst = iMid;
+ }else if( cMid > c ){
+ iLast = iMid - 1;
+ }else{
+ return aUWidth[iMid].w;
+ }
+ }
+ if( aUWidth[iLast].iFirst > c ) return aUWidth[iFirst].w;
+ return aUWidth[iLast].w;
+}
+
+/*
+** Compute the value and length of a multi-byte UTF-8 character that
+** begins at z[0]. Return the length. Write the Unicode value into *pU.
+**
+** This routine only works for *multi-byte* UTF-8 characters.
+*/
+static int decodeUtf8(const unsigned char *z, int *pU){
+ if( (z[0] & 0xe0)==0xc0 && (z[1] & 0xc0)==0x80 ){
+ *pU = ((z[0] & 0x1f)<<6) | (z[1] & 0x3f);
+ return 2;
+ }
+ if( (z[0] & 0xf0)==0xe0 && (z[1] & 0xc0)==0x80 && (z[2] & 0xc0)==0x80 ){
+ *pU = ((z[0] & 0x0f)<<12) | ((z[1] & 0x3f)<<6) | (z[2] & 0x3f);
+ return 3;
+ }
+ if( (z[0] & 0xf8)==0xf0 && (z[1] & 0xc0)==0x80 && (z[2] & 0xc0)==0x80
+ && (z[3] & 0xc0)==0x80
+ ){
+ *pU = ((z[0] & 0x0f)<<18) | ((z[1] & 0x3f)<<12) | ((z[2] & 0x3f))<<6
+ | (z[4] & 0x3f);
+ return 4;
+ }
+ *pU = 0;
+ return 1;
+}
+
+
+#if 0 /* NOT USED */
/*
-** Output string zUtf to Out stream as w characters. If w is negative,
+** Return the width, in display columns, of a UTF-8 string.
+**
+** Each normal character counts as 1. Zero-width characters count
+** as zero, and double-width characters count as 2.
+*/
+int cli_wcswidth(const char *z){
+ const unsigned char *a = (const unsigned char*)z;
+ int n = 0;
+ int i = 0;
+ unsigned char c;
+ while( (c = a[i])!=0 ){
+ if( c>=0xc0 ){
+ int u;
+ int len = decodeUtf8(&a[i], &u);
+ i += len;
+ n += cli_wcwidth(u);
+ }else if( c>=' ' ){
+ n++;
+ i++;
+ }else{
+ i++;
+ }
+ }
+ return n;
+}
+#endif
+
+/*
+** Check to see if z[] is a valid VT100 escape. If it is, then
+** return the number of bytes in the escape sequence. Return 0 if
+** z[] is not a VT100 escape.
+**
+** This routine assumes that z[0] is \033 (ESC).
+*/
+static int isVt100(const unsigned char *z){
+ int i;
+ if( z[1]!='[' ) return 0;
+ i = 2;
+ while( z[i]>=0x30 && z[i]<=0x3f ){ i++; }
+ while( z[i]>=0x20 && z[i]<=0x2f ){ i++; }
+ if( z[i]<0x40 || z[i]>0x7e ) return 0;
+ return i+1;
+}
+
+/*
+** Output string zUtf to stdout as w characters. If w is negative,
** then right-justify the text. W is the width in UTF-8 characters, not
** in bytes. This is different from the %*.*s specification in printf
** since with %*.*s the width is measured in bytes, not characters.
+**
+** Take into account zero-width and double-width Unicode characters.
+** In other words, a zero-width character does not count toward the
+** the w limit. A double-width character counts as two.
*/
-static void utf8_width_print(int w, const char *zUtf){
- int i;
- int n;
+static void utf8_width_print(FILE *out, int w, const char *zUtf){
+ const unsigned char *a = (const unsigned char*)zUtf;
+ unsigned char c;
+ int i = 0;
+ int n = 0;
+ int k;
int aw = w<0 ? -w : w;
if( zUtf==0 ) zUtf = "";
- for(i=n=0; zUtf[i]; i++){
- if( (zUtf[i]&0xc0)!=0x80 ){
- n++;
- if( n==aw ){
- do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
+ while( (c = a[i])!=0 ){
+ if( (c&0xc0)==0xc0 ){
+ int u;
+ int len = decodeUtf8(a+i, &u);
+ int x = cli_wcwidth(u);
+ if( x+n>aw ){
break;
}
+ i += len;
+ n += x;
+ }else if( c==0x1b && (k = isVt100(&a[i]))>0 ){
+ i += k;
+ }else if( n>=aw ){
+ break;
+ }else{
+ n++;
+ i++;
}
}
if( n>=aw ){
- oputf("%.*s", i, zUtf);
+ sqlite3_fprintf(out, "%.*s", i, zUtf);
}else if( w<0 ){
- oputf("%*s%s", aw-n, "", zUtf);
+ sqlite3_fprintf(out, "%*s%s", aw-n, "", zUtf);
}else{
- oputf("%s%*s", zUtf, aw-n, "");
+ sqlite3_fprintf(out, "%s%*s", zUtf, aw-n, "");
}
}
@@ -1738,7 +1290,7 @@ static FILE * openChrSource(const char *zFile){
/* On Windows, open first, then check the stream nature. This order
** is necessary because _stat() and sibs, when checking a named pipe,
** effectively break the pipe as its supplier sees it. */
- FILE *rv = fopen(zFile, "rb");
+ FILE *rv = sqlite3_fopen(zFile, "rb");
if( rv==0 ) return 0;
if( _fstat64(_fileno(rv), &x) != 0
|| !STAT_CHR_SRC(x.st_mode)){
@@ -1752,7 +1304,7 @@ static FILE * openChrSource(const char *zFile){
# define STAT_CHR_SRC(mode) (S_ISREG(mode)||S_ISFIFO(mode)||S_ISCHR(mode))
if( rc!=0 ) return 0;
if( STAT_CHR_SRC(x.st_mode) ){
- return fopen(zFile, "rb");
+ return sqlite3_fopen(zFile, "rb");
}else{
return 0;
}
@@ -1779,7 +1331,7 @@ static char *local_getline(char *zLine, FILE *in){
zLine = realloc(zLine, nLine);
shell_check_oom(zLine);
}
- if( fgets(&zLine[n], nLine - n, in)==0 ){
+ if( sqlite3_fgets(&zLine[n], nLine - n, in)==0 ){
if( n==0 ){
free(zLine);
return 0;
@@ -1978,9 +1530,9 @@ static void appendText(ShellText *p, const char *zAppend, char quote){
static char quoteChar(const char *zName){
int i;
if( zName==0 ) return '"';
- if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
+ if( !IsAlpha(zName[0]) && zName[0]!='_' ) return '"';
for(i=0; zName[i]; i++){
- if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
+ if( !IsAlnum(zName[i]) && zName[i]!='_' ) return '"';
}
return sqlite3_keyword_check(zName, i) ? '"' : 0;
}
@@ -2072,30 +1624,6 @@ static void shellDtostr(
sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
}
-
-/*
-** SQL function: shell_module_schema(X)
-**
-** Return a fake schema for the table-valued function or eponymous virtual
-** table X.
-*/
-static void shellModuleSchema(
- sqlite3_context *pCtx,
- int nVal,
- sqlite3_value **apVal
-){
- const char *zName;
- char *zFake;
- UNUSED_PARAMETER(nVal);
- zName = (const char*)sqlite3_value_text(apVal[0]);
- zFake = zName? shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName) : 0;
- if( zFake ){
- sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
- -1, sqlite3_free);
- free(zFake);
- }
-}
-
/*
** SQL function: shell_add_schema(S,X)
**
@@ -2270,6 +1798,7 @@ static void shellAddSchemaName(
# else
# define NAME_MAX (260)
# endif
+# define DIRENT_NAME_MAX (NAME_MAX)
#endif
/*
@@ -2313,8 +1842,7 @@ struct DIR {
/*
** Provide a macro, for use by the implementation, to determine if a
** particular directory entry should be skipped over when searching for
-** the next directory entry that should be returned by the readdir() or
-** readdir_r() functions.
+** the next directory entry that should be returned by the readdir().
*/
#ifndef is_filtered
@@ -2330,12 +1858,11 @@ extern const char *windirent_getenv(const char *name);
/*
** Finally, we can provide the function prototypes for the opendir(),
-** readdir(), readdir_r(), and closedir() POSIX functions.
+** readdir(), and closedir() POSIX functions.
*/
extern LPDIR opendir(const char *dirname);
extern LPDIRENT readdir(LPDIR dirp);
-extern INT readdir_r(LPDIR dirp, LPDIRENT entry, LPDIRENT *result);
extern INT closedir(LPDIR dirp);
#endif /* defined(WIN32) && defined(_MSC_VER) */
@@ -2392,11 +1919,13 @@ const char *windirent_getenv(
** Implementation of the POSIX opendir() function using the MSVCRT.
*/
LPDIR opendir(
- const char *dirname
+ const char *dirname /* Directory name, UTF8 encoding */
){
- struct _finddata_t data;
+ struct _wfinddata_t data;
LPDIR dirp = (LPDIR)sqlite3_malloc(sizeof(DIR));
SIZE_T namesize = sizeof(data.name) / sizeof(data.name[0]);
+ wchar_t *b1;
+ sqlite3_int64 sz;
if( dirp==NULL ) return NULL;
memset(dirp, 0, sizeof(DIR));
@@ -2406,9 +1935,25 @@ LPDIR opendir(
dirname = windirent_getenv("SystemDrive");
}
- memset(&data, 0, sizeof(struct _finddata_t));
- _snprintf(data.name, namesize, "%s\\*", dirname);
- dirp->d_handle = _findfirst(data.name, &data);
+ memset(&data, 0, sizeof(data));
+ sz = strlen(dirname);
+ b1 = sqlite3_malloc64( (sz+3)*sizeof(b1[0]) );
+ if( b1==0 ){
+ closedir(dirp);
+ return NULL;
+ }
+ sz = MultiByteToWideChar(CP_UTF8, 0, dirname, sz, b1, sz);
+ b1[sz++] = '\\';
+ b1[sz++] = '*';
+ b1[sz] = 0;
+ if( sz+1>(sqlite3_int64)namesize ){
+ closedir(dirp);
+ sqlite3_free(b1);
+ return NULL;
+ }
+ memcpy(data.name, b1, (sz+1)*sizeof(b1[0]));
+ sqlite3_free(b1);
+ dirp->d_handle = _wfindfirst(data.name, &data);
if( dirp->d_handle==BAD_INTPTR_T ){
closedir(dirp);
@@ -2419,8 +1964,8 @@ LPDIR opendir(
if( is_filtered(data) ){
next:
- memset(&data, 0, sizeof(struct _finddata_t));
- if( _findnext(dirp->d_handle, &data)==-1 ){
+ memset(&data, 0, sizeof(data));
+ if( _wfindnext(dirp->d_handle, &data)==-1 ){
closedir(dirp);
return NULL;
}
@@ -2430,9 +1975,8 @@ next:
}
dirp->d_first.d_attributes = data.attrib;
- strncpy(dirp->d_first.d_name, data.name, NAME_MAX);
- dirp->d_first.d_name[NAME_MAX] = '\0';
-
+ WideCharToMultiByte(CP_UTF8, 0, data.name, -1,
+ dirp->d_first.d_name, DIRENT_NAME_MAX, 0, 0);
return dirp;
}
@@ -2442,7 +1986,7 @@ next:
LPDIRENT readdir(
LPDIR dirp
){
- struct _finddata_t data;
+ struct _wfinddata_t data;
if( dirp==NULL ) return NULL;
@@ -2455,66 +1999,20 @@ LPDIRENT readdir(
next:
- memset(&data, 0, sizeof(struct _finddata_t));
- if( _findnext(dirp->d_handle, &data)==-1 ) return NULL;
+ memset(&data, 0, sizeof(data));
+ if( _wfindnext(dirp->d_handle, &data)==-1 ) return NULL;
/* TODO: Remove this block to allow hidden and/or system files. */
if( is_filtered(data) ) goto next;
dirp->d_next.d_ino++;
dirp->d_next.d_attributes = data.attrib;
- strncpy(dirp->d_next.d_name, data.name, NAME_MAX);
- dirp->d_next.d_name[NAME_MAX] = '\0';
-
+ WideCharToMultiByte(CP_UTF8, 0, data.name, -1,
+ dirp->d_next.d_name, DIRENT_NAME_MAX, 0, 0);
return &dirp->d_next;
}
/*
-** Implementation of the POSIX readdir_r() function using the MSVCRT.
-*/
-INT readdir_r(
- LPDIR dirp,
- LPDIRENT entry,
- LPDIRENT *result
-){
- struct _finddata_t data;
-
- if( dirp==NULL ) return EBADF;
-
- if( dirp->d_first.d_ino==0 ){
- dirp->d_first.d_ino++;
- dirp->d_next.d_ino++;
-
- entry->d_ino = dirp->d_first.d_ino;
- entry->d_attributes = dirp->d_first.d_attributes;
- strncpy(entry->d_name, dirp->d_first.d_name, NAME_MAX);
- entry->d_name[NAME_MAX] = '\0';
-
- *result = entry;
- return 0;
- }
-
-next:
-
- memset(&data, 0, sizeof(struct _finddata_t));
- if( _findnext(dirp->d_handle, &data)==-1 ){
- *result = NULL;
- return ENOENT;
- }
-
- /* TODO: Remove this block to allow hidden and/or system files. */
- if( is_filtered(data) ) goto next;
-
- entry->d_ino = (ino_t)-1; /* not available */
- entry->d_attributes = data.attrib;
- strncpy(entry->d_name, data.name, NAME_MAX);
- entry->d_name[NAME_MAX] = '\0';
-
- *result = entry;
- return 0;
-}
-
-/*
** Implementation of the POSIX closedir() function using the MSVCRT.
*/
INT closedir(
@@ -2845,13 +2343,23 @@ int sqlite3PcacheTraceDeactivate(void){
**
** This SQLite extension implements functions that compute SHA3 hashes
** in the way described by the (U.S.) NIST FIPS 202 SHA-3 Standard.
-** Two SQL functions are implemented:
+** Three SQL functions are implemented:
**
** sha3(X,SIZE)
-** sha3_query(Y,SIZE)
+** sha3_agg(Y,SIZE)
+** sha3_query(Z,SIZE)
**
** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
-** X is NULL.
+** X is NULL. If inputs X is text, the UTF-8 rendering of that text is
+** used to compute the hash. If X is a BLOB, then the binary data of the
+** blob is used to compute the hash. If X is an integer or real number,
+** then that number if converted into UTF-8 text and the hash is computed
+** over the text.
+**
+** The sha3_agg(Y) function computes the SHA3 hash of all Y inputs. Since
+** order is important for the hash, it is recommended that the Y expression
+** by followed by an ORDER BY clause to guarantee that the inputs occur
+** in the desired order.
**
** The sha3_query(Y) function evaluates all queries in the SQL statements of Y
** and returns a hash of their results.
@@ -2859,6 +2367,68 @@ int sqlite3PcacheTraceDeactivate(void){
** The SIZE argument is optional. If omitted, the SHA3-256 hash algorithm
** is used. If SIZE is included it must be one of the integers 224, 256,
** 384, or 512, to determine SHA3 hash variant that is computed.
+**
+** Because the sha3_agg() and sha3_query() functions compute a hash over
+** multiple values, the values are encode to use include type information.
+**
+** In sha3_agg(), the sequence of bytes that gets hashed for each input
+** Y depends on the datatype of Y:
+**
+** typeof(Y)='null' A single "N" is hashed. (One byte)
+**
+** typeof(Y)='integer' The data hash is the character "I" followed
+** by an 8-byte big-endian binary of the
+** 64-bit signed integer. (Nine bytes total.)
+**
+** typeof(Y)='real' The character "F" followed by an 8-byte
+** big-ending binary of the double. (Nine
+** bytes total.)
+**
+** typeof(Y)='text' The hash is over prefix "Tnnn:" followed
+** by the UTF8 encoding of the text. The "nnn"
+** in the prefix is the minimum-length decimal
+** representation of the octet_length of the text.
+** Notice the ":" at the end of the prefix, which
+** is needed to separate the prefix from the
+** content in cases where the content starts
+** with a digit.
+**
+** typeof(Y)='blob' The hash is taken over prefix "Bnnn:" followed
+** by the binary content of the blob. The "nnn"
+** in the prefix is the minimum-length decimal
+** representation of the byte-length of the blob.
+**
+** According to the rules above, all of the following SELECT statements
+** should return TRUE:
+**
+** SELECT sha3(1) = sha3('1');
+**
+** SELECT sha3('hello') = sha3(x'68656c6c6f');
+**
+** WITH a(x) AS (VALUES('xyzzy'))
+** SELECT sha3_agg(x) = sha3('T5:xyzzy') FROM a;
+**
+** WITH a(x) AS (VALUES(x'010203'))
+** SELECT sha3_agg(x) = sha3(x'42333a010203') FROM a;
+**
+** WITH a(x) AS (VALUES(0x123456))
+** SELECT sha3_agg(x) = sha3(x'490000000000123456') FROM a;
+**
+** WITH a(x) AS (VALUES(100.015625))
+** SELECT sha3_agg(x) = sha3(x'464059010000000000') FROM a;
+**
+** WITH a(x) AS (VALUES(NULL))
+** SELECT sha3_agg(x) = sha3('N') FROM a;
+**
+**
+** In sha3_query(), individual column values are encoded as with
+** sha3_agg(), but with the addition that a single "R" character is
+** inserted at the start of each row.
+**
+** Note that sha3_agg() hashes rows for which Y is NULL. Add a FILTER
+** clause if NULL rows should be excluded:
+**
+** SELECT sha3_agg(x ORDER BY rowid) FILTER(WHERE x NOT NULL) FROM t1;
*/
/* #include "sqlite3ext.h" */
SQLITE_EXTENSION_INIT1
@@ -2908,6 +2478,7 @@ struct SHA3Context {
unsigned nRate; /* Bytes of input accepted per Keccak iteration */
unsigned nLoaded; /* Input bytes loaded into u.x[] so far this cycle */
unsigned ixMask; /* Insert next input into u.x[nLoaded^ixMask]. */
+ unsigned iSize; /* 224, 256, 358, or 512 */
};
/*
@@ -3237,6 +2808,7 @@ static void KeccakF1600Step(SHA3Context *p){
*/
static void SHA3Init(SHA3Context *p, int iSize){
memset(p, 0, sizeof(*p));
+ p->iSize = iSize;
if( iSize>=128 && iSize<=512 ){
p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
}else{
@@ -3381,6 +2953,60 @@ static void sha3_step_vformat(
}
/*
+** Update a SHA3Context using a single sqlite3_value.
+*/
+static void sha3UpdateFromValue(SHA3Context *p, sqlite3_value *pVal){
+ switch( sqlite3_value_type(pVal) ){
+ case SQLITE_NULL: {
+ SHA3Update(p, (const unsigned char*)"N",1);
+ break;
+ }
+ case SQLITE_INTEGER: {
+ sqlite3_uint64 u;
+ int j;
+ unsigned char x[9];
+ sqlite3_int64 v = sqlite3_value_int64(pVal);
+ memcpy(&u, &v, 8);
+ for(j=8; j>=1; j--){
+ x[j] = u & 0xff;
+ u >>= 8;
+ }
+ x[0] = 'I';
+ SHA3Update(p, x, 9);
+ break;
+ }
+ case SQLITE_FLOAT: {
+ sqlite3_uint64 u;
+ int j;
+ unsigned char x[9];
+ double r = sqlite3_value_double(pVal);
+ memcpy(&u, &r, 8);
+ for(j=8; j>=1; j--){
+ x[j] = u & 0xff;
+ u >>= 8;
+ }
+ x[0] = 'F';
+ SHA3Update(p,x,9);
+ break;
+ }
+ case SQLITE_TEXT: {
+ int n2 = sqlite3_value_bytes(pVal);
+ const unsigned char *z2 = sqlite3_value_text(pVal);
+ sha3_step_vformat(p,"T%d:",n2);
+ SHA3Update(p, z2, n2);
+ break;
+ }
+ case SQLITE_BLOB: {
+ int n2 = sqlite3_value_bytes(pVal);
+ const unsigned char *z2 = sqlite3_value_blob(pVal);
+ sha3_step_vformat(p,"B%d:",n2);
+ SHA3Update(p, z2, n2);
+ break;
+ }
+ }
+}
+
+/*
** Implementation of the sha3_query(SQL,SIZE) function.
**
** This function compiles and runs the SQL statement(s) given in the
@@ -3469,9 +3095,425 @@ static void sha3QueryFunc(
while( SQLITE_ROW==sqlite3_step(pStmt) ){
SHA3Update(&cx,(const unsigned char*)"R",1);
for(i=0; i<nCol; i++){
+ sha3UpdateFromValue(&cx, sqlite3_column_value(pStmt,i));
+ }
+ }
+ sqlite3_finalize(pStmt);
+ }
+ sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
+}
+
+/*
+** xStep function for sha3_agg().
+*/
+static void sha3AggStep(
+ sqlite3_context *context,
+ int argc,
+ sqlite3_value **argv
+){
+ SHA3Context *p;
+ p = (SHA3Context*)sqlite3_aggregate_context(context, sizeof(*p));
+ if( p==0 ) return;
+ if( p->nRate==0 ){
+ int sz = 256;
+ if( argc==2 ){
+ sz = sqlite3_value_int(argv[1]);
+ if( sz!=224 && sz!=384 && sz!=512 ){
+ sz = 256;
+ }
+ }
+ SHA3Init(p, sz);
+ }
+ sha3UpdateFromValue(p, argv[0]);
+}
+
+
+/*
+** xFinal function for sha3_agg().
+*/
+static void sha3AggFinal(sqlite3_context *context){
+ SHA3Context *p;
+ p = (SHA3Context*)sqlite3_aggregate_context(context, sizeof(*p));
+ if( p==0 ) return;
+ if( p->iSize ){
+ sqlite3_result_blob(context, SHA3Final(p), p->iSize/8, SQLITE_TRANSIENT);
+ }
+}
+
+
+
+#ifdef _WIN32
+
+#endif
+int sqlite3_shathree_init(
+ sqlite3 *db,
+ char **pzErrMsg,
+ const sqlite3_api_routines *pApi
+){
+ int rc = SQLITE_OK;
+ SQLITE_EXTENSION_INIT2(pApi);
+ (void)pzErrMsg; /* Unused parameter */
+ rc = sqlite3_create_function(db, "sha3", 1,
+ SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
+ 0, sha3Func, 0, 0);
+ if( rc==SQLITE_OK ){
+ rc = sqlite3_create_function(db, "sha3", 2,
+ SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
+ 0, sha3Func, 0, 0);
+ }
+ if( rc==SQLITE_OK ){
+ rc = sqlite3_create_function(db, "sha3_agg", 1,
+ SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
+ 0, 0, sha3AggStep, sha3AggFinal);
+ }
+ if( rc==SQLITE_OK ){
+ rc = sqlite3_create_function(db, "sha3_agg", 2,
+ SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
+ 0, 0, sha3AggStep, sha3AggFinal);
+ }
+ if( rc==SQLITE_OK ){
+ rc = sqlite3_create_function(db, "sha3_query", 1,
+ SQLITE_UTF8 | SQLITE_DIRECTONLY,
+ 0, sha3QueryFunc, 0, 0);
+ }
+ if( rc==SQLITE_OK ){
+ rc = sqlite3_create_function(db, "sha3_query", 2,
+ SQLITE_UTF8 | SQLITE_DIRECTONLY,
+ 0, sha3QueryFunc, 0, 0);
+ }
+ return rc;
+}
+
+/************************* End ../ext/misc/shathree.c ********************/
+/************************* Begin ../ext/misc/sha1.c ******************/
+/*
+** 2017-01-27
+**
+** 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 SQLite extension implements functions that compute SHA1 hashes.
+** Two SQL functions are implemented:
+**
+** sha1(X)
+** sha1_query(Y)
+**
+** The sha1(X) function computes the SHA1 hash of the input X, or NULL if
+** X is NULL.
+**
+** The sha1_query(Y) function evalutes all queries in the SQL statements of Y
+** and returns a hash of their results.
+*/
+/* #include "sqlite3ext.h" */
+SQLITE_EXTENSION_INIT1
+#include <assert.h>
+#include <string.h>
+#include <stdarg.h>
+
+/******************************************************************************
+** The Hash Engine
+*/
+/* Context for the SHA1 hash */
+typedef struct SHA1Context SHA1Context;
+struct SHA1Context {
+ unsigned int state[5];
+ unsigned int count[2];
+ unsigned char buffer[64];
+};
+
+#define SHA_ROT(x,l,r) ((x) << (l) | (x) >> (r))
+#define rol(x,k) SHA_ROT(x,k,32-(k))
+#define ror(x,k) SHA_ROT(x,32-(k),k)
+
+#define blk0le(i) (block[i] = (ror(block[i],8)&0xFF00FF00) \
+ |(rol(block[i],8)&0x00FF00FF))
+#define blk0be(i) block[i]
+#define blk(i) (block[i&15] = rol(block[(i+13)&15]^block[(i+8)&15] \
+ ^block[(i+2)&15]^block[i&15],1))
+
+/*
+ * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1
+ *
+ * Rl0() for little-endian and Rb0() for big-endian. Endianness is
+ * determined at run-time.
+ */
+#define Rl0(v,w,x,y,z,i) \
+ z+=((w&(x^y))^y)+blk0le(i)+0x5A827999+rol(v,5);w=ror(w,2);
+#define Rb0(v,w,x,y,z,i) \
+ z+=((w&(x^y))^y)+blk0be(i)+0x5A827999+rol(v,5);w=ror(w,2);
+#define R1(v,w,x,y,z,i) \
+ z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=ror(w,2);
+#define R2(v,w,x,y,z,i) \
+ z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=ror(w,2);
+#define R3(v,w,x,y,z,i) \
+ z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=ror(w,2);
+#define R4(v,w,x,y,z,i) \
+ z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=ror(w,2);
+
+/*
+ * Hash a single 512-bit block. This is the core of the algorithm.
+ */
+static void SHA1Transform(unsigned int state[5], const unsigned char buffer[64]){
+ unsigned int qq[5]; /* a, b, c, d, e; */
+ static int one = 1;
+ unsigned int block[16];
+ memcpy(block, buffer, 64);
+ memcpy(qq,state,5*sizeof(unsigned int));
+
+#define a qq[0]
+#define b qq[1]
+#define c qq[2]
+#define d qq[3]
+#define e qq[4]
+
+ /* Copy p->state[] to working vars */
+ /*
+ a = state[0];
+ b = state[1];
+ c = state[2];
+ d = state[3];
+ e = state[4];
+ */
+
+ /* 4 rounds of 20 operations each. Loop unrolled. */
+ if( 1 == *(unsigned char*)&one ){
+ Rl0(a,b,c,d,e, 0); Rl0(e,a,b,c,d, 1); Rl0(d,e,a,b,c, 2); Rl0(c,d,e,a,b, 3);
+ Rl0(b,c,d,e,a, 4); Rl0(a,b,c,d,e, 5); Rl0(e,a,b,c,d, 6); Rl0(d,e,a,b,c, 7);
+ Rl0(c,d,e,a,b, 8); Rl0(b,c,d,e,a, 9); Rl0(a,b,c,d,e,10); Rl0(e,a,b,c,d,11);
+ Rl0(d,e,a,b,c,12); Rl0(c,d,e,a,b,13); Rl0(b,c,d,e,a,14); Rl0(a,b,c,d,e,15);
+ }else{
+ Rb0(a,b,c,d,e, 0); Rb0(e,a,b,c,d, 1); Rb0(d,e,a,b,c, 2); Rb0(c,d,e,a,b, 3);
+ Rb0(b,c,d,e,a, 4); Rb0(a,b,c,d,e, 5); Rb0(e,a,b,c,d, 6); Rb0(d,e,a,b,c, 7);
+ Rb0(c,d,e,a,b, 8); Rb0(b,c,d,e,a, 9); Rb0(a,b,c,d,e,10); Rb0(e,a,b,c,d,11);
+ Rb0(d,e,a,b,c,12); Rb0(c,d,e,a,b,13); Rb0(b,c,d,e,a,14); Rb0(a,b,c,d,e,15);
+ }
+ R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
+ R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
+ R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
+ R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
+ R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
+ R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
+ R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
+ R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
+ R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
+ R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
+ R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
+ R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
+ R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
+ R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
+ R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
+ R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
+
+ /* Add the working vars back into context.state[] */
+ state[0] += a;
+ state[1] += b;
+ state[2] += c;
+ state[3] += d;
+ state[4] += e;
+
+#undef a
+#undef b
+#undef c
+#undef d
+#undef e
+}
+
+
+/* Initialize a SHA1 context */
+static void hash_init(SHA1Context *p){
+ /* SHA1 initialization constants */
+ p->state[0] = 0x67452301;
+ p->state[1] = 0xEFCDAB89;
+ p->state[2] = 0x98BADCFE;
+ p->state[3] = 0x10325476;
+ p->state[4] = 0xC3D2E1F0;
+ p->count[0] = p->count[1] = 0;
+}
+
+/* Add new content to the SHA1 hash */
+static void hash_step(
+ SHA1Context *p, /* Add content to this context */
+ const unsigned char *data, /* Data to be added */
+ unsigned int len /* Number of bytes in data */
+){
+ unsigned int i, j;
+
+ j = p->count[0];
+ if( (p->count[0] += len << 3) < j ){
+ p->count[1] += (len>>29)+1;
+ }
+ j = (j >> 3) & 63;
+ if( (j + len) > 63 ){
+ (void)memcpy(&p->buffer[j], data, (i = 64-j));
+ SHA1Transform(p->state, p->buffer);
+ for(; i + 63 < len; i += 64){
+ SHA1Transform(p->state, &data[i]);
+ }
+ j = 0;
+ }else{
+ i = 0;
+ }
+ (void)memcpy(&p->buffer[j], &data[i], len - i);
+}
+
+/* Compute a string using sqlite3_vsnprintf() and hash it */
+static void hash_step_vformat(
+ SHA1Context *p, /* Add content to this context */
+ const char *zFormat,
+ ...
+){
+ va_list ap;
+ int n;
+ char zBuf[50];
+ va_start(ap, zFormat);
+ sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);
+ va_end(ap);
+ n = (int)strlen(zBuf);
+ hash_step(p, (unsigned char*)zBuf, n);
+}
+
+
+/* Add padding and compute the message digest. Render the
+** message digest as lower-case hexadecimal and put it into
+** zOut[]. zOut[] must be at least 41 bytes long. */
+static void hash_finish(
+ SHA1Context *p, /* The SHA1 context to finish and render */
+ char *zOut, /* Store hex or binary hash here */
+ int bAsBinary /* 1 for binary hash, 0 for hex hash */
+){
+ unsigned int i;
+ unsigned char finalcount[8];
+ unsigned char digest[20];
+ static const char zEncode[] = "0123456789abcdef";
+
+ for (i = 0; i < 8; i++){
+ finalcount[i] = (unsigned char)((p->count[(i >= 4 ? 0 : 1)]
+ >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */
+ }
+ hash_step(p, (const unsigned char *)"\200", 1);
+ while ((p->count[0] & 504) != 448){
+ hash_step(p, (const unsigned char *)"\0", 1);
+ }
+ hash_step(p, finalcount, 8); /* Should cause a SHA1Transform() */
+ for (i = 0; i < 20; i++){
+ digest[i] = (unsigned char)((p->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
+ }
+ if( bAsBinary ){
+ memcpy(zOut, digest, 20);
+ }else{
+ for(i=0; i<20; i++){
+ zOut[i*2] = zEncode[(digest[i]>>4)&0xf];
+ zOut[i*2+1] = zEncode[digest[i] & 0xf];
+ }
+ zOut[i*2]= 0;
+ }
+}
+/* End of the hashing logic
+*****************************************************************************/
+
+/*
+** Implementation of the sha1(X) function.
+**
+** Return a lower-case hexadecimal rendering of the SHA1 hash of the
+** argument X. If X is a BLOB, it is hashed as is. For all other
+** types of input, X is converted into a UTF-8 string and the string
+** is hash without the trailing 0x00 terminator. The hash of a NULL
+** value is NULL.
+*/
+static void sha1Func(
+ sqlite3_context *context,
+ int argc,
+ sqlite3_value **argv
+){
+ SHA1Context cx;
+ int eType = sqlite3_value_type(argv[0]);
+ int nByte = sqlite3_value_bytes(argv[0]);
+ char zOut[44];
+
+ assert( argc==1 );
+ if( eType==SQLITE_NULL ) return;
+ hash_init(&cx);
+ if( eType==SQLITE_BLOB ){
+ hash_step(&cx, sqlite3_value_blob(argv[0]), nByte);
+ }else{
+ hash_step(&cx, sqlite3_value_text(argv[0]), nByte);
+ }
+ if( sqlite3_user_data(context)!=0 ){
+ hash_finish(&cx, zOut, 1);
+ sqlite3_result_blob(context, zOut, 20, SQLITE_TRANSIENT);
+ }else{
+ hash_finish(&cx, zOut, 0);
+ sqlite3_result_blob(context, zOut, 40, SQLITE_TRANSIENT);
+ }
+}
+
+/*
+** Implementation of the sha1_query(SQL) function.
+**
+** This function compiles and runs the SQL statement(s) given in the
+** argument. The results are hashed using SHA1 and that hash is returned.
+**
+** The original SQL text is included as part of the hash.
+**
+** The hash is not just a concatenation of the outputs. Each query
+** is delimited and each row and value within the query is delimited,
+** with all values being marked with their datatypes.
+*/
+static void sha1QueryFunc(
+ sqlite3_context *context,
+ int argc,
+ sqlite3_value **argv
+){
+ sqlite3 *db = sqlite3_context_db_handle(context);
+ const char *zSql = (const char*)sqlite3_value_text(argv[0]);
+ sqlite3_stmt *pStmt = 0;
+ int nCol; /* Number of columns in the result set */
+ int i; /* Loop counter */
+ int rc;
+ int n;
+ const char *z;
+ SHA1Context cx;
+ char zOut[44];
+
+ assert( argc==1 );
+ if( zSql==0 ) return;
+ hash_init(&cx);
+ while( zSql[0] ){
+ rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
+ if( rc ){
+ char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",
+ zSql, sqlite3_errmsg(db));
+ sqlite3_finalize(pStmt);
+ sqlite3_result_error(context, zMsg, -1);
+ sqlite3_free(zMsg);
+ return;
+ }
+ if( !sqlite3_stmt_readonly(pStmt) ){
+ char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));
+ sqlite3_finalize(pStmt);
+ sqlite3_result_error(context, zMsg, -1);
+ sqlite3_free(zMsg);
+ return;
+ }
+ nCol = sqlite3_column_count(pStmt);
+ z = sqlite3_sql(pStmt);
+ n = (int)strlen(z);
+ hash_step_vformat(&cx,"S%d:",n);
+ hash_step(&cx,(unsigned char*)z,n);
+
+ /* Compute a hash over the result of the query */
+ while( SQLITE_ROW==sqlite3_step(pStmt) ){
+ hash_step(&cx,(const unsigned char*)"R",1);
+ for(i=0; i<nCol; i++){
switch( sqlite3_column_type(pStmt,i) ){
case SQLITE_NULL: {
- SHA3Update(&cx, (const unsigned char*)"N",1);
+ hash_step(&cx, (const unsigned char*)"N",1);
break;
}
case SQLITE_INTEGER: {
@@ -3485,7 +3527,7 @@ static void sha3QueryFunc(
u >>= 8;
}
x[0] = 'I';
- SHA3Update(&cx, x, 9);
+ hash_step(&cx, x, 9);
break;
}
case SQLITE_FLOAT: {
@@ -3499,21 +3541,21 @@ static void sha3QueryFunc(
u >>= 8;
}
x[0] = 'F';
- SHA3Update(&cx,x,9);
+ hash_step(&cx,x,9);
break;
}
case SQLITE_TEXT: {
int n2 = sqlite3_column_bytes(pStmt, i);
const unsigned char *z2 = sqlite3_column_text(pStmt, i);
- sha3_step_vformat(&cx,"T%d:",n2);
- SHA3Update(&cx, z2, n2);
+ hash_step_vformat(&cx,"T%d:",n2);
+ hash_step(&cx, z2, n2);
break;
}
case SQLITE_BLOB: {
int n2 = sqlite3_column_bytes(pStmt, i);
const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
- sha3_step_vformat(&cx,"B%d:",n2);
- SHA3Update(&cx, z2, n2);
+ hash_step_vformat(&cx,"B%d:",n2);
+ hash_step(&cx, z2, n2);
break;
}
}
@@ -3521,43 +3563,40 @@ static void sha3QueryFunc(
}
sqlite3_finalize(pStmt);
}
- sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
+ hash_finish(&cx, zOut, 0);
+ sqlite3_result_text(context, zOut, 40, SQLITE_TRANSIENT);
}
#ifdef _WIN32
#endif
-int sqlite3_shathree_init(
+int sqlite3_sha_init(
sqlite3 *db,
char **pzErrMsg,
const sqlite3_api_routines *pApi
){
int rc = SQLITE_OK;
+ static int one = 1;
SQLITE_EXTENSION_INIT2(pApi);
(void)pzErrMsg; /* Unused parameter */
- rc = sqlite3_create_function(db, "sha3", 1,
- SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
- 0, sha3Func, 0, 0);
+ rc = sqlite3_create_function(db, "sha1", 1,
+ SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
+ 0, sha1Func, 0, 0);
if( rc==SQLITE_OK ){
- rc = sqlite3_create_function(db, "sha3", 2,
- SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
- 0, sha3Func, 0, 0);
- }
- if( rc==SQLITE_OK ){
- rc = sqlite3_create_function(db, "sha3_query", 1,
- SQLITE_UTF8 | SQLITE_DIRECTONLY,
- 0, sha3QueryFunc, 0, 0);
+ rc = sqlite3_create_function(db, "sha1b", 1,
+ SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
+ (void*)&one, sha1Func, 0, 0);
}
if( rc==SQLITE_OK ){
- rc = sqlite3_create_function(db, "sha3_query", 2,
- SQLITE_UTF8 | SQLITE_DIRECTONLY,
- 0, sha3QueryFunc, 0, 0);
+ rc = sqlite3_create_function(db, "sha1_query", 1,
+ SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
+ sha1QueryFunc, 0, 0);
}
return rc;
}
-/************************* End ../ext/misc/shathree.c ********************/
+/************************* End ../ext/misc/sha1.c ********************/
/************************* Begin ../ext/misc/uint.c ******************/
/*
** 2020-04-14
@@ -3577,7 +3616,7 @@ int sqlite3_shathree_init(
** of digits compare in numeric order.
**
** * Leading zeros are handled properly, in the sense that
-** they do not mess of the maginitude comparison of embedded
+** they do not mess of the magnitude comparison of embedded
** strings of digits. "x00123y" is equal to "x123y".
**
** * Only unsigned integers are recognized. Plus and minus
@@ -3683,6 +3722,9 @@ SQLITE_EXTENSION_INIT1
# define UNUSED_PARAMETER(X) (void)(X)
#endif
+#ifndef IsSpace
+#define IsSpace(X) isspace((unsigned char)X)
+#endif
/* A decimal object */
typedef struct Decimal Decimal;
@@ -3732,7 +3774,7 @@ static Decimal *decimalNewFromText(const char *zIn, int n){
p->nFrac = 0;
p->a = sqlite3_malloc64( n+1 );
if( p->a==0 ) goto new_from_text_failed;
- for(i=0; isspace(zIn[i]); i++){}
+ for(i=0; IsSpace(zIn[i]); i++){}
if( zIn[i]=='-' ){
p->sign = 1;
i++;
@@ -4387,7 +4429,7 @@ static void decimalSubFunc(
decimal_free(pB);
}
-/* Aggregate funcion: decimal_sum(X)
+/* Aggregate function: decimal_sum(X)
**
** Works like sum() except that it uses decimal arithmetic for unlimited
** precision.
@@ -4540,6 +4582,512 @@ int sqlite3_decimal_init(
}
/************************* End ../ext/misc/decimal.c ********************/
+/************************* Begin ../ext/misc/percentile.c ******************/
+/*
+** 2013-05-28
+**
+** 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 percentile(Y,P) SQL function
+** and similar as described below:
+**
+** (1) The percentile(Y,P) function is an aggregate function taking
+** exactly two arguments.
+**
+** (2) If the P argument to percentile(Y,P) is not the same for every
+** row in the aggregate then an error is thrown. The word "same"
+** in the previous sentence means that the value differ by less
+** than 0.001.
+**
+** (3) If the P argument to percentile(Y,P) evaluates to anything other
+** than a number in the range of 0.0 to 100.0 inclusive then an
+** error is thrown.
+**
+** (4) If any Y argument to percentile(Y,P) evaluates to a value that
+** is not NULL and is not numeric then an error is thrown.
+**
+** (5) If any Y argument to percentile(Y,P) evaluates to plus or minus
+** infinity then an error is thrown. (SQLite always interprets NaN
+** values as NULL.)
+**
+** (6) Both Y and P in percentile(Y,P) can be arbitrary expressions,
+** including CASE WHEN expressions.
+**
+** (7) The percentile(Y,P) aggregate is able to handle inputs of at least
+** one million (1,000,000) rows.
+**
+** (8) If there are no non-NULL values for Y, then percentile(Y,P)
+** returns NULL.
+**
+** (9) If there is exactly one non-NULL value for Y, the percentile(Y,P)
+** returns the one Y value.
+**
+** (10) If there N non-NULL values of Y where N is two or more and
+** the Y values are ordered from least to greatest and a graph is
+** drawn from 0 to N-1 such that the height of the graph at J is
+** the J-th Y value and such that straight lines are drawn between
+** adjacent Y values, then the percentile(Y,P) function returns
+** the height of the graph at P*(N-1)/100.
+**
+** (11) The percentile(Y,P) function always returns either a floating
+** point number or NULL.
+**
+** (12) The percentile(Y,P) is implemented as a single C99 source-code
+** file that compiles into a shared-library or DLL that can be loaded
+** into SQLite using the sqlite3_load_extension() interface.
+**
+** (13) A separate median(Y) function is the equivalent percentile(Y,50).
+**
+** (14) A separate percentile_cont(Y,P) function is equivalent to
+** percentile(Y,P/100.0). In other words, the fraction value in
+** the second argument is in the range of 0 to 1 instead of 0 to 100.
+**
+** (15) A separate percentile_disc(Y,P) function is like
+** percentile_cont(Y,P) except that instead of returning the weighted
+** average of the nearest two input values, it returns the next lower
+** value. So the percentile_disc(Y,P) will always return a value
+** that was one of the inputs.
+**
+** (16) All of median(), percentile(Y,P), percentile_cont(Y,P) and
+** percentile_disc(Y,P) can be used as window functions.
+**
+** Differences from standard SQL:
+**
+** * The percentile_cont(X,P) function is equivalent to the following in
+** standard SQL:
+**
+** (percentile_cont(P) WITHIN GROUP (ORDER BY X))
+**
+** The SQLite syntax is much more compact. The standard SQL syntax
+** is also supported if SQLite is compiled with the
+** -DSQLITE_ENABLE_ORDERED_SET_AGGREGATES option.
+**
+** * No median(X) function exists in the SQL standard. App developers
+** are expected to write "percentile_cont(0.5)WITHIN GROUP(ORDER BY X)".
+**
+** * No percentile(Y,P) function exists in the SQL standard. Instead of
+** percential(Y,P), developers must write this:
+** "percentile_cont(P/100.0) WITHIN GROUP (ORDER BY Y)". Note that
+** the fraction parameter to percentile() goes from 0 to 100 whereas
+** the fraction parameter in SQL standard percentile_cont() goes from
+** 0 to 1.
+**
+** Implementation notes as of 2024-08-31:
+**
+** * The regular aggregate-function versions of these routines work
+** by accumulating all values in an array of doubles, then sorting
+** that array using quicksort before computing the answer. Thus
+** the runtime is O(NlogN) where N is the number of rows of input.
+**
+** * For the window-function versions of these routines, the array of
+** inputs is sorted as soon as the first value is computed. Thereafter,
+** the array is kept in sorted order using an insert-sort. This
+** results in O(N*K) performance where K is the size of the window.
+** One can imagine alternative implementations that give O(N*logN*logK)
+** performance, but they require more complex logic and data structures.
+** The developers have elected to keep the asymptotically slower
+** algorithm for now, for simplicity, under the theory that window
+** functions are seldom used and when they are, the window size K is
+** often small. The developers might revisit that decision later,
+** should the need arise.
+*/
+#if defined(SQLITE3_H)
+ /* no-op */
+#elif defined(SQLITE_STATIC_PERCENTILE)
+/* # include "sqlite3.h" */
+#else
+/* # include "sqlite3ext.h" */
+ SQLITE_EXTENSION_INIT1
+#endif
+#include <assert.h>
+#include <string.h>
+#include <stdlib.h>
+
+/* The following object is the group context for a single percentile()
+** aggregate. Remember all input Y values until the very end.
+** Those values are accumulated in the Percentile.a[] array.
+*/
+typedef struct Percentile Percentile;
+struct Percentile {
+ unsigned nAlloc; /* Number of slots allocated for a[] */
+ unsigned nUsed; /* Number of slots actually used in a[] */
+ char bSorted; /* True if a[] is already in sorted order */
+ char bKeepSorted; /* True if advantageous to keep a[] sorted */
+ char bPctValid; /* True if rPct is valid */
+ double rPct; /* Fraction. 0.0 to 1.0 */
+ double *a; /* Array of Y values */
+};
+
+/* Details of each function in the percentile family */
+typedef struct PercentileFunc PercentileFunc;
+struct PercentileFunc {
+ const char *zName; /* Function name */
+ char nArg; /* Number of arguments */
+ char mxFrac; /* Maximum value of the "fraction" input */
+ char bDiscrete; /* True for percentile_disc() */
+};
+static const PercentileFunc aPercentFunc[] = {
+ { "median", 1, 1, 0 },
+ { "percentile", 2, 100, 0 },
+ { "percentile_cont", 2, 1, 0 },
+ { "percentile_disc", 2, 1, 1 },
+};
+
+/*
+** Return TRUE if the input floating-point number is an infinity.
+*/
+static int percentIsInfinity(double r){
+ sqlite3_uint64 u;
+ assert( sizeof(u)==sizeof(r) );
+ memcpy(&u, &r, sizeof(u));
+ return ((u>>52)&0x7ff)==0x7ff;
+}
+
+/*
+** Return TRUE if two doubles differ by 0.001 or less.
+*/
+static int percentSameValue(double a, double b){
+ a -= b;
+ return a>=-0.001 && a<=0.001;
+}
+
+/*
+** Search p (which must have p->bSorted) looking for an entry with
+** value y. Return the index of that entry.
+**
+** If bExact is true, return -1 if the entry is not found.
+**
+** If bExact is false, return the index at which a new entry with
+** value y should be insert in order to keep the values in sorted
+** order. The smallest return value in this case will be 0, and
+** the largest return value will be p->nUsed.
+*/
+static int percentBinarySearch(Percentile *p, double y, int bExact){
+ int iFirst = 0; /* First element of search range */
+ int iLast = p->nUsed - 1; /* Last element of search range */
+ while( iLast>=iFirst ){
+ int iMid = (iFirst+iLast)/2;
+ double x = p->a[iMid];
+ if( x<y ){
+ iFirst = iMid + 1;
+ }else if( x>y ){
+ iLast = iMid - 1;
+ }else{
+ return iMid;
+ }
+ }
+ if( bExact ) return -1;
+ return iFirst;
+}
+
+/*
+** Generate an error for a percentile function.
+**
+** The error format string must have exactly one occurrence of "%%s()"
+** (with two '%' characters). That substring will be replaced by the name
+** of the function.
+*/
+static void percentError(sqlite3_context *pCtx, const char *zFormat, ...){
+ PercentileFunc *pFunc = (PercentileFunc*)sqlite3_user_data(pCtx);
+ char *zMsg1;
+ char *zMsg2;
+ va_list ap;
+
+ va_start(ap, zFormat);
+ zMsg1 = sqlite3_vmprintf(zFormat, ap);
+ va_end(ap);
+ zMsg2 = zMsg1 ? sqlite3_mprintf(zMsg1, pFunc->zName) : 0;
+ sqlite3_result_error(pCtx, zMsg2, -1);
+ sqlite3_free(zMsg1);
+ sqlite3_free(zMsg2);
+}
+
+/*
+** The "step" function for percentile(Y,P) is called once for each
+** input row.
+*/
+static void percentStep(sqlite3_context *pCtx, int argc, sqlite3_value **argv){
+ Percentile *p;
+ double rPct;
+ int eType;
+ double y;
+ assert( argc==2 || argc==1 );
+
+ if( argc==1 ){
+ /* Requirement 13: median(Y) is the same as percentile(Y,50). */
+ rPct = 0.5;
+ }else{
+ /* Requirement 3: P must be a number between 0 and 100 */
+ PercentileFunc *pFunc = (PercentileFunc*)sqlite3_user_data(pCtx);
+ eType = sqlite3_value_numeric_type(argv[1]);
+ rPct = sqlite3_value_double(argv[1])/(double)pFunc->mxFrac;
+ if( (eType!=SQLITE_INTEGER && eType!=SQLITE_FLOAT)
+ || rPct<0.0 || rPct>1.0
+ ){
+ percentError(pCtx, "the fraction argument to %%s()"
+ " is not between 0.0 and %.1f",
+ (double)pFunc->mxFrac);
+ return;
+ }
+ }
+
+ /* Allocate the session context. */
+ p = (Percentile*)sqlite3_aggregate_context(pCtx, sizeof(*p));
+ if( p==0 ) return;
+
+ /* Remember the P value. Throw an error if the P value is different
+ ** from any prior row, per Requirement (2). */
+ if( !p->bPctValid ){
+ p->rPct = rPct;
+ p->bPctValid = 1;
+ }else if( !percentSameValue(p->rPct,rPct) ){
+ percentError(pCtx, "the fraction argument to %%s()"
+ " is not the same for all input rows");
+ return;
+ }
+
+ /* Ignore rows for which Y is NULL */
+ eType = sqlite3_value_type(argv[0]);
+ if( eType==SQLITE_NULL ) return;
+
+ /* If not NULL, then Y must be numeric. Otherwise throw an error.
+ ** Requirement 4 */
+ if( eType!=SQLITE_INTEGER && eType!=SQLITE_FLOAT ){
+ percentError(pCtx, "input to %%s() is not numeric");
+ return;
+ }
+
+ /* Throw an error if the Y value is infinity or NaN */
+ y = sqlite3_value_double(argv[0]);
+ if( percentIsInfinity(y) ){
+ percentError(pCtx, "Inf input to %%s()");
+ return;
+ }
+
+ /* Allocate and store the Y */
+ if( p->nUsed>=p->nAlloc ){
+ unsigned n = p->nAlloc*2 + 250;
+ double *a = sqlite3_realloc64(p->a, sizeof(double)*n);
+ if( a==0 ){
+ sqlite3_free(p->a);
+ memset(p, 0, sizeof(*p));
+ sqlite3_result_error_nomem(pCtx);
+ return;
+ }
+ p->nAlloc = n;
+ p->a = a;
+ }
+ if( p->nUsed==0 ){
+ p->a[p->nUsed++] = y;
+ p->bSorted = 1;
+ }else if( !p->bSorted || y>=p->a[p->nUsed-1] ){
+ p->a[p->nUsed++] = y;
+ }else if( p->bKeepSorted ){
+ int i;
+ i = percentBinarySearch(p, y, 0);
+ if( i<(int)p->nUsed ){
+ memmove(&p->a[i+1], &p->a[i], (p->nUsed-i)*sizeof(p->a[0]));
+ }
+ p->a[i] = y;
+ p->nUsed++;
+ }else{
+ p->a[p->nUsed++] = y;
+ p->bSorted = 0;
+ }
+}
+
+/*
+** Interchange two doubles.
+*/
+#define SWAP_DOUBLE(X,Y) {double ttt=(X);(X)=(Y);(Y)=ttt;}
+
+/*
+** Sort an array of doubles.
+**
+** Algorithm: quicksort
+**
+** This is implemented separately rather than using the qsort() routine
+** from the standard library because:
+**
+** (1) To avoid a dependency on qsort()
+** (2) To avoid the function call to the comparison routine for each
+** comparison.
+*/
+static void percentSort(double *a, unsigned int n){
+ int iLt; /* Entries before a[iLt] are less than rPivot */
+ int iGt; /* Entries at or after a[iGt] are greater than rPivot */
+ int i; /* Loop counter */
+ double rPivot; /* The pivot value */
+
+ assert( n>=2 );
+ if( a[0]>a[n-1] ){
+ SWAP_DOUBLE(a[0],a[n-1])
+ }
+ if( n==2 ) return;
+ iGt = n-1;
+ i = n/2;
+ if( a[0]>a[i] ){
+ SWAP_DOUBLE(a[0],a[i])
+ }else if( a[i]>a[iGt] ){
+ SWAP_DOUBLE(a[i],a[iGt])
+ }
+ if( n==3 ) return;
+ rPivot = a[i];
+ iLt = i = 1;
+ do{
+ if( a[i]<rPivot ){
+ if( i>iLt ) SWAP_DOUBLE(a[i],a[iLt])
+ iLt++;
+ i++;
+ }else if( a[i]>rPivot ){
+ do{
+ iGt--;
+ }while( iGt>i && a[iGt]>rPivot );
+ SWAP_DOUBLE(a[i],a[iGt])
+ }else{
+ i++;
+ }
+ }while( i<iGt );
+ if( iLt>=2 ) percentSort(a, iLt);
+ if( n-iGt>=2 ) percentSort(a+iGt, n-iGt);
+
+/* Uncomment for testing */
+#if 0
+ for(i=0; i<n-1; i++){
+ assert( a[i]<=a[i+1] );
+ }
+#endif
+}
+
+
+/*
+** The "inverse" function for percentile(Y,P) is called to remove a
+** row that was previously inserted by "step".
+*/
+static void percentInverse(sqlite3_context *pCtx,int argc,sqlite3_value **argv){
+ Percentile *p;
+ int eType;
+ double y;
+ int i;
+ assert( argc==2 || argc==1 );
+
+ /* Allocate the session context. */
+ p = (Percentile*)sqlite3_aggregate_context(pCtx, sizeof(*p));
+ assert( p!=0 );
+
+ /* Ignore rows for which Y is NULL */
+ eType = sqlite3_value_type(argv[0]);
+ if( eType==SQLITE_NULL ) return;
+
+ /* If not NULL, then Y must be numeric. Otherwise throw an error.
+ ** Requirement 4 */
+ if( eType!=SQLITE_INTEGER && eType!=SQLITE_FLOAT ){
+ return;
+ }
+
+ /* Ignore the Y value if it is infinity or NaN */
+ y = sqlite3_value_double(argv[0]);
+ if( percentIsInfinity(y) ){
+ return;
+ }
+ if( p->bSorted==0 ){
+ assert( p->nUsed>1 );
+ percentSort(p->a, p->nUsed);
+ p->bSorted = 1;
+ }
+ p->bKeepSorted = 1;
+
+ /* Find and remove the row */
+ i = percentBinarySearch(p, y, 1);
+ if( i>=0 ){
+ p->nUsed--;
+ if( i<(int)p->nUsed ){
+ memmove(&p->a[i], &p->a[i+1], (p->nUsed - i)*sizeof(p->a[0]));
+ }
+ }
+}
+
+/*
+** Compute the final output of percentile(). Clean up all allocated
+** memory if and only if bIsFinal is true.
+*/
+static void percentCompute(sqlite3_context *pCtx, int bIsFinal){
+ Percentile *p;
+ PercentileFunc *pFunc = (PercentileFunc*)sqlite3_user_data(pCtx);
+ unsigned i1, i2;
+ double v1, v2;
+ double ix, vx;
+ p = (Percentile*)sqlite3_aggregate_context(pCtx, 0);
+ if( p==0 ) return;
+ if( p->a==0 ) return;
+ if( p->nUsed ){
+ if( p->bSorted==0 ){
+ assert( p->nUsed>1 );
+ percentSort(p->a, p->nUsed);
+ p->bSorted = 1;
+ }
+ ix = p->rPct*(p->nUsed-1);
+ i1 = (unsigned)ix;
+ if( pFunc->bDiscrete ){
+ vx = p->a[i1];
+ }else{
+ i2 = ix==(double)i1 || i1==p->nUsed-1 ? i1 : i1+1;
+ v1 = p->a[i1];
+ v2 = p->a[i2];
+ vx = v1 + (v2-v1)*(ix-i1);
+ }
+ sqlite3_result_double(pCtx, vx);
+ }
+ if( bIsFinal ){
+ sqlite3_free(p->a);
+ memset(p, 0, sizeof(*p));
+ }else{
+ p->bKeepSorted = 1;
+ }
+}
+static void percentFinal(sqlite3_context *pCtx){
+ percentCompute(pCtx, 1);
+}
+static void percentValue(sqlite3_context *pCtx){
+ percentCompute(pCtx, 0);
+}
+
+#if defined(_WIN32) && !defined(SQLITE3_H) && !defined(SQLITE_STATIC_PERCENTILE)
+
+#endif
+int sqlite3_percentile_init(
+ sqlite3 *db,
+ char **pzErrMsg,
+ const sqlite3_api_routines *pApi
+){
+ int rc = SQLITE_OK;
+ unsigned int i;
+#ifdef SQLITE3EXT_H
+ SQLITE_EXTENSION_INIT2(pApi);
+#else
+ (void)pApi; /* Unused parameter */
+#endif
+ (void)pzErrMsg; /* Unused parameter */
+ for(i=0; i<sizeof(aPercentFunc)/sizeof(aPercentFunc[0]); i++){
+ rc = sqlite3_create_window_function(db,
+ aPercentFunc[i].zName,
+ aPercentFunc[i].nArg,
+ SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_SELFORDER1,
+ (void*)&aPercentFunc[i],
+ percentStep, percentFinal, percentValue, percentInverse, 0);
+ if( rc ) break;
+ }
+ return rc;
+}
+
+/************************* End ../ext/misc/percentile.c ********************/
#undef sqlite3_base_init
#define sqlite3_base_init sqlite3_base64_init
/************************* Begin ../ext/misc/base64.c ******************/
@@ -4720,15 +5268,15 @@ static u8* fromBase64( char *pIn, int ncIn, u8 *pOut ){
case ND:
/* Treat dark non-digits as pad, but they terminate decode too. */
ncIn = 0;
- deliberate_fall_through;
+ deliberate_fall_through; /* FALLTHRU */
case WS:
/* Treat whitespace as pad and terminate this group.*/
nti = nac;
- deliberate_fall_through;
+ deliberate_fall_through; /* FALLTHRU */
case PC:
bdp = 0;
--nbo;
- deliberate_fall_through;
+ deliberate_fall_through; /* FALLTHRU */
default: /* bdp is the digit value. */
qv = qv<<6 | bdp;
break;
@@ -4737,10 +5285,13 @@ static u8* fromBase64( char *pIn, int ncIn, u8 *pOut ){
switch( nbo ){
case 3:
pOut[2] = (qv) & 0xff;
+ deliberate_fall_through; /* FALLTHRU */
case 2:
pOut[1] = (qv>>8) & 0xff;
+ deliberate_fall_through; /* FALLTHRU */
case 1:
pOut[0] = (qv>>16) & 0xff;
+ break;
}
pOut += nbo;
}
@@ -5075,12 +5626,16 @@ static u8* fromBase85( char *pIn, int ncIn, u8 *pOut ){
switch( nbo ){
case 4:
*pOut++ = (qv >> 24)&0xff;
+ /* FALLTHRU */
case 3:
*pOut++ = (qv >> 16)&0xff;
+ /* FALLTHRU */
case 2:
*pOut++ = (qv >> 8)&0xff;
+ /* FALLTHRU */
case 1:
*pOut++ = qv&0xff;
+ /* FALLTHRU */
case 0:
break;
}
@@ -5375,7 +5930,7 @@ int main(int na, char *av[]){
** WITH c(name,bin) AS (VALUES
** ('minimum positive value', x'0000000000000001'),
** ('maximum subnormal value', x'000fffffffffffff'),
-** ('mininum positive nornal value', x'0010000000000000'),
+** ('minimum positive normal value', x'0010000000000000'),
** ('maximum value', x'7fefffffffffffff'))
** SELECT c.name, decimal_mul(ieee754_mantissa(c.bin),pow2.v)
** FROM pow2, c WHERE pow2.x=ieee754_exponent(c.bin);
@@ -5683,8 +6238,7 @@ int sqlite3_ieee_init(
** step HIDDEN
** );
**
-** The virtual table also has a rowid, logically equivalent to n+1 where
-** "n" is the ascending integer in the aforesaid production definition.
+** The virtual table also has a rowid which is an alias for the value.
**
** Function arguments in queries against this virtual table are translated
** into equality constraints against successive hidden columns. In other
@@ -5713,12 +6267,33 @@ int sqlite3_ieee_init(
** and a very large cost if either start or stop are unavailable. This
** encourages the query planner to order joins such that the bounds of the
** series are well-defined.
+**
+** Update on 2024-08-22:
+** xBestIndex now also looks for equality and inequality constraints against
+** the value column and uses those constraints as additional bounds against
+** the sequence range. Thus, a query like this:
+**
+** SELECT value FROM generate_series($SA,$EA)
+** WHERE value BETWEEN $SB AND $EB;
+**
+** Is logically the same as:
+**
+** SELECT value FROM generate_series(max($SA,$SB),min($EA,$EB));
+**
+** Constraints on the value column can server as substitutes for constraints
+** on the hidden start and stop columns. So, the following two queries
+** are equivalent:
+**
+** SELECT value FROM generate_series($S,$E);
+** SELECT value FROM generate_series WHERE value BETWEEN $S and $E;
+**
*/
/* #include "sqlite3ext.h" */
SQLITE_EXTENSION_INIT1
#include <assert.h>
#include <string.h>
#include <limits.h>
+#include <math.h>
#ifndef SQLITE_OMIT_VIRTUALTABLE
/*
@@ -5742,7 +6317,7 @@ static sqlite3_int64 genSeqMember(
smBase += (mxI64 - mxI64/2) * smStep;
}
/* Under UBSAN (or on 1's complement machines), must do this last term
- * in steps to avoid the dreaded (and harmless) signed multiply overlow. */
+ * in steps to avoid the dreaded (and harmless) signed multiply overflow. */
if( ix>=2 ){
sqlite3_int64 ix2 = (sqlite3_int64)ix/2;
smBase += ix2*smStep;
@@ -5754,8 +6329,10 @@ static sqlite3_int64 genSeqMember(
/* typedef unsigned char u8; */
typedef struct SequenceSpec {
- sqlite3_int64 iBase; /* Starting value ("start") */
- sqlite3_int64 iTerm; /* Given terminal value ("stop") */
+ sqlite3_int64 iOBase; /* Original starting value ("start") */
+ sqlite3_int64 iOTerm; /* Original terminal value ("stop") */
+ sqlite3_int64 iBase; /* Starting value to actually use */
+ sqlite3_int64 iTerm; /* Terminal value to actually use */
sqlite3_int64 iStep; /* Increment ("step") */
sqlite3_uint64 uSeqIndexMax; /* maximum sequence index (aka "n") */
sqlite3_uint64 uSeqIndexNow; /* Current index during generation */
@@ -5877,6 +6454,7 @@ static int seriesConnect(
int rc;
/* Column numbers */
+#define SERIES_COLUMN_ROWID (-1)
#define SERIES_COLUMN_VALUE 0
#define SERIES_COLUMN_START 1
#define SERIES_COLUMN_STOP 2
@@ -5948,9 +6526,9 @@ static int seriesColumn(
series_cursor *pCur = (series_cursor*)cur;
sqlite3_int64 x = 0;
switch( i ){
- case SERIES_COLUMN_START: x = pCur->ss.iBase; break;
- case SERIES_COLUMN_STOP: x = pCur->ss.iTerm; break;
- case SERIES_COLUMN_STEP: x = pCur->ss.iStep; break;
+ case SERIES_COLUMN_START: x = pCur->ss.iOBase; break;
+ case SERIES_COLUMN_STOP: x = pCur->ss.iOTerm; break;
+ case SERIES_COLUMN_STEP: x = pCur->ss.iStep; break;
default: x = pCur->ss.iValueNow; break;
}
sqlite3_result_int64(ctx, x);
@@ -5958,17 +6536,17 @@ static int seriesColumn(
}
#ifndef LARGEST_UINT64
+#define LARGEST_INT64 (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32))
#define LARGEST_UINT64 (0xffffffff|(((sqlite3_uint64)0xffffffff)<<32))
+#define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
#endif
/*
-** Return the rowid for the current row, logically equivalent to n+1 where
-** "n" is the ascending integer in the aforesaid production definition.
+** The rowid is the same as the value.
*/
static int seriesRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
series_cursor *pCur = (series_cursor*)cur;
- sqlite3_uint64 n = pCur->ss.uSeqIndexNow;
- *pRowid = (sqlite3_int64)((n<LARGEST_UINT64)? n+1 : 0);
+ *pRowid = pCur->ss.iValueNow;
return SQLITE_OK;
}
@@ -5999,13 +6577,18 @@ static int seriesEof(sqlite3_vtab_cursor *cur){
** parameter. (idxStr is not used in this implementation.) idxNum
** is a bitmask showing which constraints are available:
**
-** 0x01: start=VALUE
-** 0x02: stop=VALUE
-** 0x04: step=VALUE
-** 0x08: descending order
-** 0x10: ascending order
-** 0x20: LIMIT VALUE
-** 0x40: OFFSET VALUE
+** 0x0001: start=VALUE
+** 0x0002: stop=VALUE
+** 0x0004: step=VALUE
+** 0x0008: descending order
+** 0x0010: ascending order
+** 0x0020: LIMIT VALUE
+** 0x0040: OFFSET VALUE
+** 0x0080: value=VALUE
+** 0x0100: value>=VALUE
+** 0x0200: value>VALUE
+** 0x1000: value<=VALUE
+** 0x2000: value<VALUE
**
** This routine should initialize the cursor and position it so that it
** is pointing at the first row, or pointing off the end of the table
@@ -6018,6 +6601,12 @@ static int seriesFilter(
){
series_cursor *pCur = (series_cursor *)pVtabCursor;
int i = 0;
+ int returnNoRows = 0;
+ sqlite3_int64 iMin = SMALLEST_INT64;
+ sqlite3_int64 iMax = LARGEST_INT64;
+ sqlite3_int64 iLimit = 0;
+ sqlite3_int64 iOffset = 0;
+
(void)idxStrUnused;
if( idxNum & 0x01 ){
pCur->ss.iBase = sqlite3_value_int64(argv[i++]);
@@ -6039,16 +6628,123 @@ static int seriesFilter(
}else{
pCur->ss.iStep = 1;
}
+
+ /* If there are constraints on the value column but there are
+ ** no constraints on the start, stop, and step columns, then
+ ** initialize the default range to be the entire range of 64-bit signed
+ ** integers. This range will contracted by the value column constraints
+ ** further below.
+ */
+ if( (idxNum & 0x05)==0 && (idxNum & 0x0380)!=0 ){
+ pCur->ss.iBase = SMALLEST_INT64;
+ }
+ if( (idxNum & 0x06)==0 && (idxNum & 0x3080)!=0 ){
+ pCur->ss.iTerm = LARGEST_INT64;
+ }
+ pCur->ss.iOBase = pCur->ss.iBase;
+ pCur->ss.iOTerm = pCur->ss.iTerm;
+
+ /* Extract the LIMIT and OFFSET values, but do not apply them yet.
+ ** The range must first be constrained by the limits on value.
+ */
if( idxNum & 0x20 ){
- sqlite3_int64 iLimit = sqlite3_value_int64(argv[i++]);
- sqlite3_int64 iTerm;
+ iLimit = sqlite3_value_int64(argv[i++]);
if( idxNum & 0x40 ){
- sqlite3_int64 iOffset = sqlite3_value_int64(argv[i++]);
- if( iOffset>0 ){
- pCur->ss.iBase += pCur->ss.iStep*iOffset;
+ iOffset = sqlite3_value_int64(argv[i++]);
+ }
+ }
+
+ if( idxNum & 0x3380 ){
+ /* Extract the maximum range of output values determined by
+ ** constraints on the "value" column.
+ */
+ if( idxNum & 0x0080 ){
+ if( sqlite3_value_numeric_type(argv[i])==SQLITE_FLOAT ){
+ double r = sqlite3_value_double(argv[i++]);
+ if( r==ceil(r) ){
+ iMin = iMax = (sqlite3_int64)r;
+ }else{
+ returnNoRows = 1;
+ }
+ }else{
+ iMin = iMax = sqlite3_value_int64(argv[i++]);
+ }
+ }else{
+ if( idxNum & 0x0300 ){
+ if( sqlite3_value_numeric_type(argv[i])==SQLITE_FLOAT ){
+ double r = sqlite3_value_double(argv[i++]);
+ if( idxNum & 0x0200 && r==ceil(r) ){
+ iMin = (sqlite3_int64)ceil(r+1.0);
+ }else{
+ iMin = (sqlite3_int64)ceil(r);
+ }
+ }else{
+ iMin = sqlite3_value_int64(argv[i++]);
+ if( idxNum & 0x0200 ){
+ if( iMin==LARGEST_INT64 ){
+ returnNoRows = 1;
+ }else{
+ iMin++;
+ }
+ }
+ }
}
+ if( idxNum & 0x3000 ){
+ if( sqlite3_value_numeric_type(argv[i])==SQLITE_FLOAT ){
+ double r = sqlite3_value_double(argv[i++]);
+ if( (idxNum & 0x2000)!=0 && r==floor(r) ){
+ iMax = (sqlite3_int64)(r-1.0);
+ }else{
+ iMax = (sqlite3_int64)floor(r);
+ }
+ }else{
+ iMax = sqlite3_value_int64(argv[i++]);
+ if( idxNum & 0x2000 ){
+ if( iMax==SMALLEST_INT64 ){
+ returnNoRows = 1;
+ }else{
+ iMax--;
+ }
+ }
+ }
+ }
+ if( iMin>iMax ){
+ returnNoRows = 1;
+ }
+ }
+
+ /* Try to reduce the range of values to be generated based on
+ ** constraints on the "value" column.
+ */
+ if( pCur->ss.iStep>0 ){
+ sqlite3_int64 szStep = pCur->ss.iStep;
+ if( pCur->ss.iBase<iMin ){
+ sqlite3_uint64 d = iMin - pCur->ss.iBase;
+ pCur->ss.iBase += ((d+szStep-1)/szStep)*szStep;
+ }
+ if( pCur->ss.iTerm>iMax ){
+ pCur->ss.iTerm = iMax;
+ }
+ }else{
+ sqlite3_int64 szStep = -pCur->ss.iStep;
+ assert( szStep>0 );
+ if( pCur->ss.iBase>iMax ){
+ sqlite3_uint64 d = pCur->ss.iBase - iMax;
+ pCur->ss.iBase -= ((d+szStep-1)/szStep)*szStep;
+ }
+ if( pCur->ss.iTerm<iMin ){
+ pCur->ss.iTerm = iMin;
+ }
+ }
+ }
+
+ /* Apply LIMIT and OFFSET constraints, if any */
+ if( idxNum & 0x20 ){
+ if( iOffset>0 ){
+ pCur->ss.iBase += pCur->ss.iStep*iOffset;
}
if( iLimit>=0 ){
+ sqlite3_int64 iTerm;
iTerm = pCur->ss.iBase + (iLimit - 1)*pCur->ss.iStep;
if( pCur->ss.iStep<0 ){
if( iTerm>pCur->ss.iTerm ) pCur->ss.iTerm = iTerm;
@@ -6057,16 +6753,21 @@ static int seriesFilter(
}
}
}
+
+
for(i=0; i<argc; i++){
if( sqlite3_value_type(argv[i])==SQLITE_NULL ){
/* If any of the constraints have a NULL value, then return no rows.
- ** See ticket https://www.sqlite.org/src/info/fac496b61722daf2 */
- pCur->ss.iBase = 1;
- pCur->ss.iTerm = 0;
- pCur->ss.iStep = 1;
+ ** See ticket https://sqlite.org/src/info/fac496b61722daf2 */
+ returnNoRows = 1;
break;
}
}
+ if( returnNoRows ){
+ pCur->ss.iBase = 1;
+ pCur->ss.iTerm = 0;
+ pCur->ss.iStep = 1;
+ }
if( idxNum & 0x08 ){
pCur->ss.isReversing = pCur->ss.iStep > 0;
}else{
@@ -6087,13 +6788,35 @@ static int seriesFilter(
**
** The query plan is represented by bits in idxNum:
**
-** 0x01 start = $value -- constraint exists
-** 0x02 stop = $value -- constraint exists
-** 0x04 step = $value -- constraint exists
-** 0x08 output is in descending order
-** 0x10 output is in ascending order
-** 0x20 LIMIT $value -- constraint exists
-** 0x40 OFFSET $value -- constraint exists
+** 0x0001 start = $num
+** 0x0002 stop = $num
+** 0x0004 step = $num
+** 0x0008 output is in descending order
+** 0x0010 output is in ascending order
+** 0x0020 LIMIT $num
+** 0x0040 OFFSET $num
+** 0x0080 value = $num
+** 0x0100 value >= $num
+** 0x0200 value > $num
+** 0x1000 value <= $num
+** 0x2000 value < $num
+**
+** Only one of 0x0100 or 0x0200 will be returned. Similarly, only
+** one of 0x1000 or 0x2000 will be returned. If the 0x0080 is set, then
+** none of the 0xff00 bits will be set.
+**
+** The order of parameters passed to xFilter is as follows:
+**
+** * The argument to start= if bit 0x0001 is in the idxNum mask
+** * The argument to stop= if bit 0x0002 is in the idxNum mask
+** * The argument to step= if bit 0x0004 is in the idxNum mask
+** * The argument to LIMIT if bit 0x0020 is in the idxNum mask
+** * The argument to OFFSET if bit 0x0040 is in the idxNum mask
+** * The argument to value=, or value>= or value> if any of
+** bits 0x0380 are in the idxNum mask
+** * The argument to value<= or value< if either of bits 0x3000
+** are in the mask
+**
*/
static int seriesBestIndex(
sqlite3_vtab *pVTab,
@@ -6106,7 +6829,9 @@ static int seriesBestIndex(
#endif
int unusableMask = 0; /* Mask of unusable constraints */
int nArg = 0; /* Number of arguments that seriesFilter() expects */
- int aIdx[5]; /* Constraints on start, stop, step, LIMIT, OFFSET */
+ int aIdx[7]; /* Constraints on start, stop, step, LIMIT, OFFSET,
+ ** and value. aIdx[5] covers value=, value>=, and
+ ** value>, aIdx[6] covers value<= and value< */
const struct sqlite3_index_constraint *pConstraint;
/* This implementation assumes that the start, stop, and step columns
@@ -6114,7 +6839,7 @@ static int seriesBestIndex(
assert( SERIES_COLUMN_STOP == SERIES_COLUMN_START+1 );
assert( SERIES_COLUMN_STEP == SERIES_COLUMN_START+2 );
- aIdx[0] = aIdx[1] = aIdx[2] = aIdx[3] = aIdx[4] = -1;
+ aIdx[0] = aIdx[1] = aIdx[2] = aIdx[3] = aIdx[4] = aIdx[5] = aIdx[6] = -1;
pConstraint = pIdxInfo->aConstraint;
for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
int iCol; /* 0 for start, 1 for stop, 2 for step */
@@ -6135,7 +6860,61 @@ static int seriesBestIndex(
}
continue;
}
- if( pConstraint->iColumn<SERIES_COLUMN_START ) continue;
+ if( pConstraint->iColumn<SERIES_COLUMN_START ){
+ if( (pConstraint->iColumn==SERIES_COLUMN_VALUE ||
+ pConstraint->iColumn==SERIES_COLUMN_ROWID)
+ && pConstraint->usable
+ ){
+ switch( op ){
+ case SQLITE_INDEX_CONSTRAINT_EQ:
+ case SQLITE_INDEX_CONSTRAINT_IS: {
+ idxNum |= 0x0080;
+ idxNum &= ~0x3300;
+ aIdx[5] = i;
+ aIdx[6] = -1;
+#ifndef ZERO_ARGUMENT_GENERATE_SERIES
+ bStartSeen = 1;
+#endif
+ break;
+ }
+ case SQLITE_INDEX_CONSTRAINT_GE: {
+ if( idxNum & 0x0080 ) break;
+ idxNum |= 0x0100;
+ idxNum &= ~0x0200;
+ aIdx[5] = i;
+#ifndef ZERO_ARGUMENT_GENERATE_SERIES
+ bStartSeen = 1;
+#endif
+ break;
+ }
+ case SQLITE_INDEX_CONSTRAINT_GT: {
+ if( idxNum & 0x0080 ) break;
+ idxNum |= 0x0200;
+ idxNum &= ~0x0100;
+ aIdx[5] = i;
+#ifndef ZERO_ARGUMENT_GENERATE_SERIES
+ bStartSeen = 1;
+#endif
+ break;
+ }
+ case SQLITE_INDEX_CONSTRAINT_LE: {
+ if( idxNum & 0x0080 ) break;
+ idxNum |= 0x1000;
+ idxNum &= ~0x2000;
+ aIdx[6] = i;
+ break;
+ }
+ case SQLITE_INDEX_CONSTRAINT_LT: {
+ if( idxNum & 0x0080 ) break;
+ idxNum |= 0x2000;
+ idxNum &= ~0x1000;
+ aIdx[6] = i;
+ break;
+ }
+ }
+ }
+ continue;
+ }
iCol = pConstraint->iColumn - SERIES_COLUMN_START;
assert( iCol>=0 && iCol<=2 );
iMask = 1 << iCol;
@@ -6157,7 +6936,7 @@ static int seriesBestIndex(
idxNum &= ~0x60;
aIdx[4] = 0;
}
- for(i=0; i<5; i++){
+ for(i=0; i<7; i++){
if( (j = aIdx[i])>=0 ){
pIdxInfo->aConstraintUsage[j].argvIndex = ++nArg;
pIdxInfo->aConstraintUsage[j].omit =
@@ -6205,6 +6984,9 @@ static int seriesBestIndex(
pIdxInfo->estimatedRows = 2147483647;
}
pIdxInfo->idxNum = idxNum;
+#ifdef SQLITE_INDEX_SCAN_HEX
+ pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_HEX;
+#endif
return SQLITE_OK;
}
@@ -6923,7 +7705,8 @@ static const char *re_subcompile_string(ReCompiled *p){
** regular expression. Applications should invoke this routine once
** for every call to re_compile() to avoid memory leaks.
*/
-static void re_free(ReCompiled *pRe){
+static void re_free(void *p){
+ ReCompiled *pRe = (ReCompiled*)p;
if( pRe ){
sqlite3_free(pRe->aOp);
sqlite3_free(pRe->aArg);
@@ -7192,7 +7975,7 @@ int sqlite3_regexp_init(
** modification-time of the target file is set to this value before
** returning.
**
-** If three or more arguments are passed to this function and an
+** If five or more arguments are passed to this function and an
** error is encountered, an exception is raised.
**
** READFILE(FILE):
@@ -7244,24 +8027,27 @@ SQLITE_EXTENSION_INIT1
# include <dirent.h>
# include <utime.h>
# include <sys/time.h>
+# define STRUCT_STAT struct stat
#else
# include "windows.h"
# include <io.h>
# include <direct.h>
/* # include "test_windirent.h" */
# define dirent DIRENT
-# ifndef chmod
-# define chmod _chmod
-# endif
-# ifndef stat
-# define stat _stat
-# endif
-# define mkdir(path,mode) _mkdir(path)
-# define lstat(path,buf) stat(path,buf)
+# define STRUCT_STAT struct _stat
+# define chmod(path,mode) fileio_chmod(path,mode)
+# define mkdir(path,mode) fileio_mkdir(path)
#endif
#include <time.h>
#include <errno.h>
+/* When used as part of the CLI, the sqlite3_stdio.h module will have
+** been included before this one. In that case use the sqlite3_stdio.h
+** #defines. If not, create our own for fopen().
+*/
+#ifndef _SQLITE3_STDIO_H_
+# define sqlite3_fopen fopen
+#endif
/*
** Structure of the fsdir() table-valued function
@@ -7275,6 +8061,40 @@ SQLITE_EXTENSION_INIT1
#define FSDIR_COLUMN_PATH 4 /* Path to top of search */
#define FSDIR_COLUMN_DIR 5 /* Path is relative to this directory */
+/*
+** UTF8 chmod() function for Windows
+*/
+#if defined(_WIN32) || defined(WIN32)
+static int fileio_chmod(const char *zPath, int pmode){
+ sqlite3_int64 sz = strlen(zPath);
+ wchar_t *b1 = sqlite3_malloc64( (sz+1)*sizeof(b1[0]) );
+ int rc;
+ if( b1==0 ) return -1;
+ sz = MultiByteToWideChar(CP_UTF8, 0, zPath, sz, b1, sz);
+ b1[sz] = 0;
+ rc = _wchmod(b1, pmode);
+ sqlite3_free(b1);
+ return rc;
+}
+#endif
+
+/*
+** UTF8 mkdir() function for Windows
+*/
+#if defined(_WIN32) || defined(WIN32)
+static int fileio_mkdir(const char *zPath){
+ sqlite3_int64 sz = strlen(zPath);
+ wchar_t *b1 = sqlite3_malloc64( (sz+1)*sizeof(b1[0]) );
+ int rc;
+ if( b1==0 ) return -1;
+ sz = MultiByteToWideChar(CP_UTF8, 0, zPath, sz, b1, sz);
+ b1[sz] = 0;
+ rc = _wmkdir(b1);
+ sqlite3_free(b1);
+ return rc;
+}
+#endif
+
/*
** Set the result stored by context ctx to a blob containing the
@@ -7294,7 +8114,7 @@ static void readFileContents(sqlite3_context *ctx, const char *zName){
sqlite3 *db;
int mxBlob;
- in = fopen(zName, "rb");
+ in = sqlite3_fopen(zName, "rb");
if( in==0 ){
/* File does not exist or is unreadable. Leave the result set to NULL. */
return;
@@ -7405,7 +8225,7 @@ LPWSTR utf8_to_utf16(const char *z){
*/
static void statTimesToUtc(
const char *zPath,
- struct stat *pStatBuf
+ STRUCT_STAT *pStatBuf
){
HANDLE hFindFile;
WIN32_FIND_DATAW fd;
@@ -7433,10 +8253,16 @@ static void statTimesToUtc(
*/
static int fileStat(
const char *zPath,
- struct stat *pStatBuf
+ STRUCT_STAT *pStatBuf
){
#if defined(_WIN32)
- int rc = stat(zPath, pStatBuf);
+ sqlite3_int64 sz = strlen(zPath);
+ wchar_t *b1 = sqlite3_malloc64( (sz+1)*sizeof(b1[0]) );
+ int rc;
+ if( b1==0 ) return 1;
+ sz = MultiByteToWideChar(CP_UTF8, 0, zPath, sz, b1, sz);
+ b1[sz] = 0;
+ rc = _wstat(b1, pStatBuf);
if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
return rc;
#else
@@ -7451,12 +8277,10 @@ static int fileStat(
*/
static int fileLinkStat(
const char *zPath,
- struct stat *pStatBuf
+ STRUCT_STAT *pStatBuf
){
#if defined(_WIN32)
- int rc = lstat(zPath, pStatBuf);
- if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
- return rc;
+ return fileStat(zPath, pStatBuf);
#else
return lstat(zPath, pStatBuf);
#endif
@@ -7486,7 +8310,7 @@ static int makeDirectory(
int i = 1;
while( rc==SQLITE_OK ){
- struct stat sStat;
+ STRUCT_STAT sStat;
int rc2;
for(; zCopy[i]!='/' && i<nCopy; i++);
@@ -7536,7 +8360,7 @@ static int writeFile(
** be an error though - if there is already a directory at the same
** path and either the permissions already match or can be changed
** to do so using chmod(), it is not an error. */
- struct stat sStat;
+ STRUCT_STAT sStat;
if( errno!=EEXIST
|| 0!=fileStat(zFile, &sStat)
|| !S_ISDIR(sStat.st_mode)
@@ -7549,7 +8373,7 @@ static int writeFile(
sqlite3_int64 nWrite = 0;
const char *z;
int rc = 0;
- FILE *out = fopen(zFile, "wb");
+ FILE *out = sqlite3_fopen(zFile, "wb");
if( out==0 ) return 1;
z = (const char*)sqlite3_value_blob(pData);
if( z ){
@@ -7582,7 +8406,7 @@ static int writeFile(
GetSystemTime(&currentTime);
SystemTimeToFileTime(&currentTime, &lastAccess);
- intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
+ intervals = (mtime*10000000) + 116444736000000000;
lastWrite.dwLowDateTime = (DWORD)intervals;
lastWrite.dwHighDateTime = intervals >> 32;
zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);
@@ -7738,7 +8562,7 @@ struct fsdir_cursor {
const char *zBase;
int nBase;
- struct stat sStat; /* Current lstat() results */
+ STRUCT_STAT sStat; /* Current lstat() results */
char *zPath; /* Path to current entry */
sqlite3_int64 iRowid; /* Current rowid */
};
@@ -8235,6 +9059,11 @@ SQLITE_EXTENSION_INIT1
#ifndef SQLITE_OMIT_VIRTUALTABLE
+#ifndef IsAlnum
+#define IsAlnum(X) isalnum((unsigned char)X)
+#endif
+
+
/* completion_vtab is a subclass of sqlite3_vtab which will
** serve as the underlying representation of a completion virtual table
*/
@@ -8445,7 +9274,7 @@ static int completionNext(sqlite3_vtab_cursor *cur){
zSql = sqlite3_mprintf(
"%z%s"
"SELECT pti.name FROM \"%w\".sqlite_schema AS sm"
- " JOIN pragma_table_info(sm.name,%Q) AS pti"
+ " JOIN pragma_table_xinfo(sm.name,%Q) AS pti"
" WHERE sm.type='table'",
zSql, zSep, zDb, zDb
);
@@ -8571,7 +9400,7 @@ static int completionFilter(
}
if( pCur->zLine!=0 && pCur->zPrefix==0 ){
int i = pCur->nLine;
- while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
+ while( i>0 && (IsAlnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
i--;
}
pCur->nPrefix = pCur->nLine - i;
@@ -9405,10 +10234,20 @@ SQLITE_EXTENSION_INIT1
#include <stdio.h>
#include <string.h>
#include <assert.h>
-#include <stdint.h>
+#ifndef SQLITE_NO_STDINT
+# include <stdint.h>
+#endif
#include <zlib.h>
+/* When used as part of the CLI, the sqlite3_stdio.h module will have
+** been included before this one. In that case use the sqlite3_stdio.h
+** #defines. If not, create our own for fopen().
+*/
+#ifndef _SQLITE3_STDIO_H_
+# define sqlite3_fopen fopen
+#endif
+
#ifndef SQLITE_OMIT_VIRTUALTABLE
#ifndef SQLITE_AMALGAMATION
@@ -10665,7 +11504,7 @@ static int zipfileFilter(
}
if( 0==pTab->pWriteFd && 0==bInMemory ){
- pCsr->pFile = zFile ? fopen(zFile, "rb") : 0;
+ pCsr->pFile = zFile ? sqlite3_fopen(zFile, "rb") : 0;
if( pCsr->pFile==0 ){
zipfileCursorErr(pCsr, "cannot open file: %s", zFile);
rc = SQLITE_ERROR;
@@ -10855,7 +11694,7 @@ static int zipfileBegin(sqlite3_vtab *pVtab){
** structure into memory. During the transaction any new file data is
** appended to the archive file, but the central directory is accumulated
** in main-memory until the transaction is committed. */
- pTab->pWriteFd = fopen(pTab->zFile, "ab+");
+ pTab->pWriteFd = sqlite3_fopen(pTab->zFile, "ab+");
if( pTab->pWriteFd==0 ){
pTab->base.zErrMsg = sqlite3_mprintf(
"zipfile: failed to open file %s for writing", pTab->zFile
@@ -13308,6 +14147,66 @@ static int idxProcessTriggers(sqlite3expert *p, char **pzErr){
return rc;
}
+/*
+** This function tests if the schema of the main database of database handle
+** db contains an object named zTab. Assuming no error occurs, output parameter
+** (*pbContains) is set to true if zTab exists, or false if it does not.
+**
+** Or, if an error occurs, an SQLite error code is returned. The final value
+** of (*pbContains) is undefined in this case.
+*/
+static int expertDbContainsObject(
+ sqlite3 *db,
+ const char *zTab,
+ int *pbContains /* OUT: True if object exists */
+){
+ const char *zSql = "SELECT 1 FROM sqlite_schema WHERE name = ?";
+ sqlite3_stmt *pSql = 0;
+ int rc = SQLITE_OK;
+ int ret = 0;
+
+ rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
+ if( rc==SQLITE_OK ){
+ sqlite3_bind_text(pSql, 1, zTab, -1, SQLITE_STATIC);
+ if( SQLITE_ROW==sqlite3_step(pSql) ){
+ ret = 1;
+ }
+ rc = sqlite3_finalize(pSql);
+ }
+
+ *pbContains = ret;
+ return rc;
+}
+
+/*
+** Execute SQL command zSql using database handle db. If no error occurs,
+** set (*pzErr) to NULL and return SQLITE_OK.
+**
+** If an error does occur, return an SQLite error code and set (*pzErr) to
+** point to a buffer containing an English language error message. Except,
+** if the error message begins with "no such module:", then ignore the
+** error and return as if the SQL statement had succeeded.
+**
+** This is used to copy as much of the database schema as possible while
+** ignoring any errors related to missing virtual table modules.
+*/
+static int expertSchemaSql(sqlite3 *db, const char *zSql, char **pzErr){
+ int rc = SQLITE_OK;
+ char *zErr = 0;
+
+ rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
+ if( rc!=SQLITE_OK && zErr ){
+ int nErr = STRLEN(zErr);
+ if( nErr>=15 && memcmp(zErr, "no such module:", 15)==0 ){
+ sqlite3_free(zErr);
+ rc = SQLITE_OK;
+ zErr = 0;
+ }
+ }
+
+ *pzErr = zErr;
+ return rc;
+}
static int idxCreateVtabSchema(sqlite3expert *p, char **pzErrmsg){
int rc = idxRegisterVtab(p);
@@ -13319,26 +14218,35 @@ static int idxCreateVtabSchema(sqlite3expert *p, char **pzErrmsg){
** 2) Create the equivalent virtual table in dbv.
*/
rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg,
- "SELECT type, name, sql, 1 FROM sqlite_schema "
- "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%%' "
+ "SELECT type, name, sql, 1, "
+ " substr(sql,1,14)=='create virtual' COLLATE nocase "
+ "FROM sqlite_schema "
+ "WHERE type IN ('table','view') AND "
+ " substr(name,1,7)!='sqlite_' COLLATE nocase "
" UNION ALL "
- "SELECT type, name, sql, 2 FROM sqlite_schema "
+ "SELECT type, name, sql, 2, 0 FROM sqlite_schema "
"WHERE type = 'trigger'"
" AND tbl_name IN(SELECT name FROM sqlite_schema WHERE type = 'view') "
- "ORDER BY 4, 1"
+ "ORDER BY 4, 5 DESC, 1"
);
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){
const char *zType = (const char*)sqlite3_column_text(pSchema, 0);
const char *zName = (const char*)sqlite3_column_text(pSchema, 1);
const char *zSql = (const char*)sqlite3_column_text(pSchema, 2);
+ int bVirtual = sqlite3_column_int(pSchema, 4);
+ int bExists = 0;
if( zType==0 || zName==0 ) continue;
- if( zType[0]=='v' || zType[1]=='r' ){
- if( zSql ) rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg);
+ rc = expertDbContainsObject(p->dbv, zName, &bExists);
+ if( rc || bExists ) continue;
+
+ if( zType[0]=='v' || zType[1]=='r' || bVirtual ){
+ /* A view. Or a trigger on a view. */
+ if( zSql ) rc = expertSchemaSql(p->dbv, zSql, pzErrmsg);
}else{
IdxTable *pTab;
rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg);
- if( rc==SQLITE_OK ){
+ if( rc==SQLITE_OK && ALWAYS(pTab!=0) ){
int i;
char *zInner = 0;
char *zOuter = 0;
@@ -13539,6 +14447,12 @@ static int idxPopulateOneStat1(
const char *zComma = zCols==0 ? "" : ", ";
const char *zName = (const char*)sqlite3_column_text(pIndexXInfo, 0);
const char *zColl = (const char*)sqlite3_column_text(pIndexXInfo, 1);
+ if( zName==0 ){
+ /* This index contains an expression. Ignore it. */
+ sqlite3_free(zCols);
+ sqlite3_free(zOrder);
+ return sqlite3_reset(pIndexXInfo);
+ }
zCols = idxAppendText(&rc, zCols,
"%sx.%Q IS sqlite_expert_rem(%d, x.%Q) COLLATE %s",
zComma, zName, nCol, zName, zColl
@@ -13867,12 +14781,18 @@ sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErrmsg){
if( rc==SQLITE_OK ){
sqlite3_stmt *pSql = 0;
rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg,
- "SELECT sql FROM sqlite_schema WHERE name NOT LIKE 'sqlite_%%'"
- " AND sql NOT LIKE 'CREATE VIRTUAL %%' ORDER BY rowid"
+ "SELECT sql, name, substr(sql,1,14)=='create virtual' COLLATE nocase"
+ " FROM sqlite_schema WHERE substr(name,1,7)!='sqlite_' COLLATE nocase"
+ " ORDER BY 3 DESC, rowid"
);
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
- if( zSql ) rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg);
+ const char *zName = (const char*)sqlite3_column_text(pSql, 1);
+ int bExists = 0;
+ rc = expertDbContainsObject(pNew->dbm, zName, &bExists);
+ if( rc==SQLITE_OK && zSql && bExists==0 ){
+ rc = expertSchemaSql(pNew->dbm, zSql, pzErrmsg);
+ }
}
idxFinalize(&rc, pSql);
}
@@ -13887,7 +14807,7 @@ sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErrmsg){
sqlite3_set_authorizer(pNew->dbv, idxAuthCallback, (void*)pNew);
}
- /* If an error has occurred, free the new object and reutrn NULL. Otherwise,
+ /* If an error has occurred, free the new object and return NULL. Otherwise,
** return the new sqlite3expert handle. */
if( rc!=SQLITE_OK ){
sqlite3_expert_destroy(pNew);
@@ -14069,7 +14989,6 @@ void sqlite3_expert_destroy(sqlite3expert *p){
#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
/************************* End ../ext/expert/sqlite3expert.c ********************/
-
/************************* Begin ../ext/intck/sqlite3intck.h ******************/
/*
** 2024-02-08
@@ -15187,6 +16106,1320 @@ const char *sqlite3_intck_test_sql(sqlite3_intck *p, const char *zObj){
}
/************************* End ../ext/intck/sqlite3intck.c ********************/
+/************************* Begin ../ext/misc/stmtrand.c ******************/
+/*
+** 2024-05-24
+**
+** 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.
+**
+******************************************************************************
+**
+** An SQL function that return pseudo-random non-negative integers.
+**
+** SELECT stmtrand(123);
+**
+** A special feature of this function is that the same sequence of random
+** integers is returned for each invocation of the statement. This makes
+** the results repeatable, and hence useful for testing. The argument is
+** an integer which is the seed for the random number sequence. The seed
+** is used by the first invocation of this function only and is ignored
+** for all subsequent calls within the same statement.
+**
+** Resetting a statement (sqlite3_reset()) also resets the random number
+** sequence.
+*/
+/* #include "sqlite3ext.h" */
+SQLITE_EXTENSION_INIT1
+#include <assert.h>
+#include <string.h>
+
+/* State of the pseudo-random number generator */
+typedef struct Stmtrand {
+ unsigned int x, y;
+} Stmtrand;
+
+/* auxdata key */
+#define STMTRAND_KEY (-4418371)
+
+/*
+** Function: stmtrand(SEED)
+**
+** Return a pseudo-random number.
+*/
+static void stmtrandFunc(
+ sqlite3_context *context,
+ int argc,
+ sqlite3_value **argv
+){
+ Stmtrand *p;
+
+ p = (Stmtrand*)sqlite3_get_auxdata(context, STMTRAND_KEY);
+ if( p==0 ){
+ unsigned int seed;
+ p = sqlite3_malloc( sizeof(*p) );
+ if( p==0 ){
+ sqlite3_result_error_nomem(context);
+ return;
+ }
+ if( argc>=1 ){
+ seed = (unsigned int)sqlite3_value_int(argv[0]);
+ }else{
+ seed = 0;
+ }
+ p->x = seed | 1;
+ p->y = seed;
+ sqlite3_set_auxdata(context, STMTRAND_KEY, p, sqlite3_free);
+ p = (Stmtrand*)sqlite3_get_auxdata(context, STMTRAND_KEY);
+ if( p==0 ){
+ sqlite3_result_error_nomem(context);
+ return;
+ }
+ }
+ p->x = (p->x>>1) ^ ((1+~(p->x&1)) & 0xd0000001);
+ p->y = p->y*1103515245 + 12345;
+ sqlite3_result_int(context, (int)((p->x ^ p->y)&0x7fffffff));
+}
+
+#ifdef _WIN32
+
+#endif
+int sqlite3_stmtrand_init(
+ sqlite3 *db,
+ char **pzErrMsg,
+ const sqlite3_api_routines *pApi
+){
+ int rc = SQLITE_OK;
+ SQLITE_EXTENSION_INIT2(pApi);
+ (void)pzErrMsg; /* Unused parameter */
+ rc = sqlite3_create_function(db, "stmtrand", 1, SQLITE_UTF8, 0,
+ stmtrandFunc, 0, 0);
+ if( rc==SQLITE_OK ){
+ rc = sqlite3_create_function(db, "stmtrand", 0, SQLITE_UTF8, 0,
+ stmtrandFunc, 0, 0);
+ }
+ return rc;
+}
+
+/************************* End ../ext/misc/stmtrand.c ********************/
+/************************* Begin ../ext/misc/vfstrace.c ******************/
+/*
+** 2011 March 16
+**
+** 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 implements a VFS shim that writes diagnostic
+** output for each VFS call, similar to "strace".
+**
+** USAGE:
+**
+** This source file exports a single symbol which is the name of a
+** function:
+**
+** int vfstrace_register(
+** const char *zTraceName, // Name of the newly constructed VFS
+** const char *zOldVfsName, // Name of the underlying VFS
+** int (*xOut)(const char*,void*), // Output routine. ex: fputs
+** void *pOutArg, // 2nd argument to xOut. ex: stderr
+** int makeDefault // Make the new VFS the default
+** );
+**
+** Applications that want to trace their VFS usage must provide a callback
+** function with this prototype:
+**
+** int traceOutput(const char *zMessage, void *pAppData);
+**
+** This function will "output" the trace messages, where "output" can
+** mean different things to different applications. The traceOutput function
+** for the command-line shell (see shell.c) is "fputs" from the standard
+** library, which means that all trace output is written on the stream
+** specified by the second argument. In the case of the command-line shell
+** the second argument is stderr. Other applications might choose to output
+** trace information to a file, over a socket, or write it into a buffer.
+**
+** The vfstrace_register() function creates a new "shim" VFS named by
+** the zTraceName parameter. A "shim" VFS is an SQLite backend that does
+** not really perform the duties of a true backend, but simply filters or
+** interprets VFS calls before passing them off to another VFS which does
+** the actual work. In this case the other VFS - the one that does the
+** real work - is identified by the second parameter, zOldVfsName. If
+** the 2nd parameter is NULL then the default VFS is used. The common
+** case is for the 2nd parameter to be NULL.
+**
+** The third and fourth parameters are the pointer to the output function
+** and the second argument to the output function. For the SQLite
+** command-line shell, when the -vfstrace option is used, these parameters
+** are fputs and stderr, respectively.
+**
+** The fifth argument is true (non-zero) to cause the newly created VFS
+** to become the default VFS. The common case is for the fifth parameter
+** to be true.
+**
+** The call to vfstrace_register() simply creates the shim VFS that does
+** tracing. The application must also arrange to use the new VFS for
+** all database connections that are created and for which tracing is
+** desired. This can be done by specifying the trace VFS using URI filename
+** notation, or by specifying the trace VFS as the 4th parameter to
+** sqlite3_open_v2() or by making the trace VFS be the default (by setting
+** the 5th parameter of vfstrace_register() to 1).
+**
+**
+** ENABLING VFSTRACE IN A COMMAND-LINE SHELL
+**
+** The SQLite command line shell implemented by the shell.c source file
+** can be used with this module. To compile in -vfstrace support, first
+** gather this file (test_vfstrace.c), the shell source file (shell.c),
+** and the SQLite amalgamation source files (sqlite3.c, sqlite3.h) into
+** the working directory. Then compile using a command like the following:
+**
+** gcc -o sqlite3 -Os -I. -DSQLITE_ENABLE_VFSTRACE \
+** -DSQLITE_THREADSAFE=0 -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_RTREE \
+** -DHAVE_READLINE -DHAVE_USLEEP=1 \
+** shell.c test_vfstrace.c sqlite3.c -ldl -lreadline -lncurses
+**
+** The gcc command above works on Linux and provides (in addition to the
+** -vfstrace option) support for FTS3 and FTS4, RTREE, and command-line
+** editing using the readline library. The command-line shell does not
+** use threads so we added -DSQLITE_THREADSAFE=0 just to make the code
+** run a little faster. For compiling on a Mac, you'll probably need
+** to omit the -DHAVE_READLINE, the -lreadline, and the -lncurses options.
+** The compilation could be simplified to just this:
+**
+** gcc -DSQLITE_ENABLE_VFSTRACE \
+** shell.c test_vfstrace.c sqlite3.c -ldl -lpthread
+**
+** In this second example, all unnecessary options have been removed
+** Note that since the code is now threadsafe, we had to add the -lpthread
+** option to pull in the pthreads library.
+**
+** To cross-compile for windows using MinGW, a command like this might
+** work:
+**
+** /opt/mingw/bin/i386-mingw32msvc-gcc -o sqlite3.exe -Os -I \
+** -DSQLITE_THREADSAFE=0 -DSQLITE_ENABLE_VFSTRACE \
+** shell.c test_vfstrace.c sqlite3.c
+**
+** Similar compiler commands will work on different systems. The key
+** invariants are (1) you must have -DSQLITE_ENABLE_VFSTRACE so that
+** the shell.c source file will know to include the -vfstrace command-line
+** option and (2) you must compile and link the three source files
+** shell,c, test_vfstrace.c, and sqlite3.c.
+**
+** RUNTIME CONTROL OF VFSTRACE OUTPUT
+**
+** The application can use the "vfstrace" pragma to control which VFS
+** APIs are traced. To disable all output:
+**
+** PRAGMA vfstrace('-all');
+**
+** To enable all output (which is the default setting):
+**
+** PRAGMA vfstrace('+all');
+**
+** Individual APIs can be enabled or disabled by name, with or without
+** the initial "x" character. For example, to set up for tracing lock
+** primitives only:
+**
+** PRAGMA vfstrace('-all, +Lock,Unlock,ShmLock');
+**
+** The argument to the vfstrace pragma ignores capitalization and any
+** characters other than alphabetics, '+', and '-'.
+*/
+#include <stdlib.h>
+#include <string.h>
+/* #include "sqlite3.h" */
+
+/*
+** An instance of this structure is attached to the each trace VFS to
+** provide auxiliary information.
+*/
+typedef struct vfstrace_info vfstrace_info;
+struct vfstrace_info {
+ sqlite3_vfs *pRootVfs; /* The underlying real VFS */
+ int (*xOut)(const char*, void*); /* Send output here */
+ unsigned int mTrace; /* Mask of interfaces to trace */
+ u8 bOn; /* Tracing on/off */
+ void *pOutArg; /* First argument to xOut */
+ const char *zVfsName; /* Name of this trace-VFS */
+ sqlite3_vfs *pTraceVfs; /* Pointer back to the trace VFS */
+};
+
+/*
+** The sqlite3_file object for the trace VFS
+*/
+typedef struct vfstrace_file vfstrace_file;
+struct vfstrace_file {
+ sqlite3_file base; /* Base class. Must be first */
+ vfstrace_info *pInfo; /* The trace-VFS to which this file belongs */
+ const char *zFName; /* Base name of the file */
+ sqlite3_file *pReal; /* The real underlying file */
+};
+
+/*
+** Bit values for vfstrace_info.mTrace.
+*/
+#define VTR_CLOSE 0x00000001
+#define VTR_READ 0x00000002
+#define VTR_WRITE 0x00000004
+#define VTR_TRUNC 0x00000008
+#define VTR_SYNC 0x00000010
+#define VTR_FSIZE 0x00000020
+#define VTR_LOCK 0x00000040
+#define VTR_UNLOCK 0x00000080
+#define VTR_CRL 0x00000100
+#define VTR_FCTRL 0x00000200
+#define VTR_SECSZ 0x00000400
+#define VTR_DEVCHAR 0x00000800
+#define VTR_SHMLOCK 0x00001000
+#define VTR_SHMMAP 0x00002000
+#define VTR_SHMBAR 0x00004000
+#define VTR_SHMUNMAP 0x00008000
+#define VTR_OPEN 0x00010000
+#define VTR_DELETE 0x00020000
+#define VTR_ACCESS 0x00040000
+#define VTR_FULLPATH 0x00080000
+#define VTR_DLOPEN 0x00100000
+#define VTR_DLERR 0x00200000
+#define VTR_DLSYM 0x00400000
+#define VTR_DLCLOSE 0x00800000
+#define VTR_RAND 0x01000000
+#define VTR_SLEEP 0x02000000
+#define VTR_CURTIME 0x04000000
+#define VTR_LASTERR 0x08000000
+#define VTR_FETCH 0x10000000 /* Also coverse xUnfetch */
+
+/*
+** Method declarations for vfstrace_file.
+*/
+static int vfstraceClose(sqlite3_file*);
+static int vfstraceRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
+static int vfstraceWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64);
+static int vfstraceTruncate(sqlite3_file*, sqlite3_int64 size);
+static int vfstraceSync(sqlite3_file*, int flags);
+static int vfstraceFileSize(sqlite3_file*, sqlite3_int64 *pSize);
+static int vfstraceLock(sqlite3_file*, int);
+static int vfstraceUnlock(sqlite3_file*, int);
+static int vfstraceCheckReservedLock(sqlite3_file*, int *);
+static int vfstraceFileControl(sqlite3_file*, int op, void *pArg);
+static int vfstraceSectorSize(sqlite3_file*);
+static int vfstraceDeviceCharacteristics(sqlite3_file*);
+static int vfstraceShmLock(sqlite3_file*,int,int,int);
+static int vfstraceShmMap(sqlite3_file*,int,int,int, void volatile **);
+static void vfstraceShmBarrier(sqlite3_file*);
+static int vfstraceShmUnmap(sqlite3_file*,int);
+
+/*
+** Method declarations for vfstrace_vfs.
+*/
+static int vfstraceOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
+static int vfstraceDelete(sqlite3_vfs*, const char *zName, int syncDir);
+static int vfstraceAccess(sqlite3_vfs*, const char *zName, int flags, int *);
+static int vfstraceFullPathname(sqlite3_vfs*, const char *zName, int, char *);
+static void *vfstraceDlOpen(sqlite3_vfs*, const char *zFilename);
+static void vfstraceDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
+static void (*vfstraceDlSym(sqlite3_vfs*,void*, const char *zSymbol))(void);
+static void vfstraceDlClose(sqlite3_vfs*, void*);
+static int vfstraceRandomness(sqlite3_vfs*, int nByte, char *zOut);
+static int vfstraceSleep(sqlite3_vfs*, int microseconds);
+static int vfstraceCurrentTime(sqlite3_vfs*, double*);
+static int vfstraceGetLastError(sqlite3_vfs*, int, char*);
+static int vfstraceCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
+static int vfstraceSetSystemCall(sqlite3_vfs*,const char*, sqlite3_syscall_ptr);
+static sqlite3_syscall_ptr vfstraceGetSystemCall(sqlite3_vfs*, const char *);
+static const char *vfstraceNextSystemCall(sqlite3_vfs*, const char *zName);
+
+/*
+** Return a pointer to the tail of the pathname. Examples:
+**
+** /home/drh/xyzzy.txt -> xyzzy.txt
+** xyzzy.txt -> xyzzy.txt
+*/
+static const char *fileTail(const char *z){
+ size_t i;
+ if( z==0 ) return 0;
+ i = strlen(z)-1;
+ while( i>0 && z[i-1]!='/' ){ i--; }
+ return &z[i];
+}
+
+/*
+** Send trace output defined by zFormat and subsequent arguments.
+*/
+static void vfstrace_printf(
+ vfstrace_info *pInfo,
+ const char *zFormat,
+ ...
+){
+ va_list ap;
+ char *zMsg;
+ if( pInfo->bOn ){
+ va_start(ap, zFormat);
+ zMsg = sqlite3_vmprintf(zFormat, ap);
+ va_end(ap);
+ pInfo->xOut(zMsg, pInfo->pOutArg);
+ sqlite3_free(zMsg);
+ }
+}
+
+/*
+** Try to convert an error code into a symbolic name for that error code.
+*/
+static const char *vfstrace_errcode_name(int rc ){
+ const char *zVal = 0;
+ switch( rc ){
+ case SQLITE_OK: zVal = "SQLITE_OK"; break;
+ case SQLITE_INTERNAL: zVal = "SQLITE_INTERNAL"; break;
+ case SQLITE_ERROR: zVal = "SQLITE_ERROR"; break;
+ case SQLITE_PERM: zVal = "SQLITE_PERM"; break;
+ case SQLITE_ABORT: zVal = "SQLITE_ABORT"; break;
+ case SQLITE_BUSY: zVal = "SQLITE_BUSY"; break;
+ case SQLITE_LOCKED: zVal = "SQLITE_LOCKED"; break;
+ case SQLITE_NOMEM: zVal = "SQLITE_NOMEM"; break;
+ case SQLITE_READONLY: zVal = "SQLITE_READONLY"; break;
+ case SQLITE_INTERRUPT: zVal = "SQLITE_INTERRUPT"; break;
+ case SQLITE_IOERR: zVal = "SQLITE_IOERR"; break;
+ case SQLITE_CORRUPT: zVal = "SQLITE_CORRUPT"; break;
+ case SQLITE_NOTFOUND: zVal = "SQLITE_NOTFOUND"; break;
+ case SQLITE_FULL: zVal = "SQLITE_FULL"; break;
+ case SQLITE_CANTOPEN: zVal = "SQLITE_CANTOPEN"; break;
+ case SQLITE_PROTOCOL: zVal = "SQLITE_PROTOCOL"; break;
+ case SQLITE_EMPTY: zVal = "SQLITE_EMPTY"; break;
+ case SQLITE_SCHEMA: zVal = "SQLITE_SCHEMA"; break;
+ case SQLITE_TOOBIG: zVal = "SQLITE_TOOBIG"; break;
+ case SQLITE_CONSTRAINT: zVal = "SQLITE_CONSTRAINT"; break;
+ case SQLITE_MISMATCH: zVal = "SQLITE_MISMATCH"; break;
+ case SQLITE_MISUSE: zVal = "SQLITE_MISUSE"; break;
+ case SQLITE_NOLFS: zVal = "SQLITE_NOLFS"; break;
+ case SQLITE_IOERR_READ: zVal = "SQLITE_IOERR_READ"; break;
+ case SQLITE_IOERR_SHORT_READ: zVal = "SQLITE_IOERR_SHORT_READ"; break;
+ case SQLITE_IOERR_WRITE: zVal = "SQLITE_IOERR_WRITE"; break;
+ case SQLITE_IOERR_FSYNC: zVal = "SQLITE_IOERR_FSYNC"; break;
+ case SQLITE_IOERR_DIR_FSYNC: zVal = "SQLITE_IOERR_DIR_FSYNC"; break;
+ case SQLITE_IOERR_TRUNCATE: zVal = "SQLITE_IOERR_TRUNCATE"; break;
+ case SQLITE_IOERR_FSTAT: zVal = "SQLITE_IOERR_FSTAT"; break;
+ case SQLITE_IOERR_UNLOCK: zVal = "SQLITE_IOERR_UNLOCK"; break;
+ case SQLITE_IOERR_RDLOCK: zVal = "SQLITE_IOERR_RDLOCK"; break;
+ case SQLITE_IOERR_DELETE: zVal = "SQLITE_IOERR_DELETE"; break;
+ case SQLITE_IOERR_BLOCKED: zVal = "SQLITE_IOERR_BLOCKED"; break;
+ case SQLITE_IOERR_NOMEM: zVal = "SQLITE_IOERR_NOMEM"; break;
+ case SQLITE_IOERR_ACCESS: zVal = "SQLITE_IOERR_ACCESS"; break;
+ case SQLITE_IOERR_CHECKRESERVEDLOCK:
+ zVal = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break;
+ case SQLITE_IOERR_LOCK: zVal = "SQLITE_IOERR_LOCK"; break;
+ case SQLITE_IOERR_CLOSE: zVal = "SQLITE_IOERR_CLOSE"; break;
+ case SQLITE_IOERR_DIR_CLOSE: zVal = "SQLITE_IOERR_DIR_CLOSE"; break;
+ case SQLITE_IOERR_SHMOPEN: zVal = "SQLITE_IOERR_SHMOPEN"; break;
+ case SQLITE_IOERR_SHMSIZE: zVal = "SQLITE_IOERR_SHMSIZE"; break;
+ case SQLITE_IOERR_SHMLOCK: zVal = "SQLITE_IOERR_SHMLOCK"; break;
+ case SQLITE_IOERR_SHMMAP: zVal = "SQLITE_IOERR_SHMMAP"; break;
+ case SQLITE_IOERR_SEEK: zVal = "SQLITE_IOERR_SEEK"; break;
+ case SQLITE_IOERR_GETTEMPPATH: zVal = "SQLITE_IOERR_GETTEMPPATH"; break;
+ case SQLITE_IOERR_CONVPATH: zVal = "SQLITE_IOERR_CONVPATH"; break;
+ case SQLITE_READONLY_DBMOVED: zVal = "SQLITE_READONLY_DBMOVED"; break;
+ case SQLITE_LOCKED_SHAREDCACHE: zVal = "SQLITE_LOCKED_SHAREDCACHE"; break;
+ case SQLITE_BUSY_RECOVERY: zVal = "SQLITE_BUSY_RECOVERY"; break;
+ case SQLITE_CANTOPEN_NOTEMPDIR: zVal = "SQLITE_CANTOPEN_NOTEMPDIR"; break;
+ }
+ return zVal;
+}
+
+/*
+** Convert value rc into a string and print it using zFormat. zFormat
+** should have exactly one %s
+*/
+static void vfstrace_print_errcode(
+ vfstrace_info *pInfo,
+ const char *zFormat,
+ int rc
+){
+ const char *zVal;
+ char zBuf[50];
+ zVal = vfstrace_errcode_name(rc);
+ if( zVal==0 ){
+ zVal = vfstrace_errcode_name(rc&0xff);
+ if( zVal ){
+ sqlite3_snprintf(sizeof(zBuf), zBuf, "%s | 0x%x", zVal, rc&0xffff00);
+ }else{
+ sqlite3_snprintf(sizeof(zBuf), zBuf, "%d (0x%x)", rc, rc);
+ }
+ zVal = zBuf;
+ }
+ vfstrace_printf(pInfo, zFormat, zVal);
+}
+
+/*
+** Append to a buffer.
+*/
+static void strappend(char *z, int *pI, const char *zAppend){
+ int i = *pI;
+ while( zAppend[0] ){ z[i++] = *(zAppend++); }
+ z[i] = 0;
+ *pI = i;
+}
+
+/*
+** Turn tracing output on or off according to mMask.
+*/
+static void vfstraceOnOff(vfstrace_info *pInfo, unsigned int mMask){
+ pInfo->bOn = (pInfo->mTrace & mMask)!=0;
+}
+
+/*
+** Close an vfstrace-file.
+*/
+static int vfstraceClose(sqlite3_file *pFile){
+ vfstrace_file *p = (vfstrace_file *)pFile;
+ vfstrace_info *pInfo = p->pInfo;
+ int rc;
+ vfstraceOnOff(pInfo, VTR_CLOSE);
+ vfstrace_printf(pInfo, "%s.xClose(%s)", pInfo->zVfsName, p->zFName);
+ rc = p->pReal->pMethods->xClose(p->pReal);
+ vfstrace_print_errcode(pInfo, " -> %s\n", rc);
+ if( rc==SQLITE_OK ){
+ sqlite3_free((void*)p->base.pMethods);
+ p->base.pMethods = 0;
+ }
+ return rc;
+}
+
+/*
+** Read data from an vfstrace-file.
+*/
+static int vfstraceRead(
+ sqlite3_file *pFile,
+ void *zBuf,
+ int iAmt,
+ sqlite_int64 iOfst
+){
+ vfstrace_file *p = (vfstrace_file *)pFile;
+ vfstrace_info *pInfo = p->pInfo;
+ int rc;
+ vfstraceOnOff(pInfo, VTR_READ);
+ vfstrace_printf(pInfo, "%s.xRead(%s,n=%d,ofst=%lld)",
+ pInfo->zVfsName, p->zFName, iAmt, iOfst);
+ rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
+ vfstrace_print_errcode(pInfo, " -> %s\n", rc);
+ return rc;
+}
+
+/*
+** Write data to an vfstrace-file.
+*/
+static int vfstraceWrite(
+ sqlite3_file *pFile,
+ const void *zBuf,
+ int iAmt,
+ sqlite_int64 iOfst
+){
+ vfstrace_file *p = (vfstrace_file *)pFile;
+ vfstrace_info *pInfo = p->pInfo;
+ int rc;
+ vfstraceOnOff(pInfo, VTR_WRITE);
+ vfstrace_printf(pInfo, "%s.xWrite(%s,n=%d,ofst=%lld)",
+ pInfo->zVfsName, p->zFName, iAmt, iOfst);
+ rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
+ vfstrace_print_errcode(pInfo, " -> %s\n", rc);
+ return rc;
+}
+
+/*
+** Truncate an vfstrace-file.
+*/
+static int vfstraceTruncate(sqlite3_file *pFile, sqlite_int64 size){
+ vfstrace_file *p = (vfstrace_file *)pFile;
+ vfstrace_info *pInfo = p->pInfo;
+ int rc;
+ vfstraceOnOff(pInfo, VTR_TRUNC);
+ vfstrace_printf(pInfo, "%s.xTruncate(%s,%lld)", pInfo->zVfsName, p->zFName,
+ size);
+ rc = p->pReal->pMethods->xTruncate(p->pReal, size);
+ vfstrace_printf(pInfo, " -> %d\n", rc);
+ return rc;
+}
+
+/*
+** Sync an vfstrace-file.
+*/
+static int vfstraceSync(sqlite3_file *pFile, int flags){
+ vfstrace_file *p = (vfstrace_file *)pFile;
+ vfstrace_info *pInfo = p->pInfo;
+ int rc;
+ int i;
+ char zBuf[100];
+ memcpy(zBuf, "|0", 3);
+ i = 0;
+ if( flags & SQLITE_SYNC_FULL ) strappend(zBuf, &i, "|FULL");
+ else if( flags & SQLITE_SYNC_NORMAL ) strappend(zBuf, &i, "|NORMAL");
+ if( flags & SQLITE_SYNC_DATAONLY ) strappend(zBuf, &i, "|DATAONLY");
+ if( flags & ~(SQLITE_SYNC_FULL|SQLITE_SYNC_DATAONLY) ){
+ sqlite3_snprintf(sizeof(zBuf)-i, &zBuf[i], "|0x%x", flags);
+ }
+ vfstraceOnOff(pInfo, VTR_SYNC);
+ vfstrace_printf(pInfo, "%s.xSync(%s,%s)", pInfo->zVfsName, p->zFName,
+ &zBuf[1]);
+ rc = p->pReal->pMethods->xSync(p->pReal, flags);
+ vfstrace_printf(pInfo, " -> %d\n", rc);
+ return rc;
+}
+
+/*
+** Return the current file-size of an vfstrace-file.
+*/
+static int vfstraceFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
+ vfstrace_file *p = (vfstrace_file *)pFile;
+ vfstrace_info *pInfo = p->pInfo;
+ int rc;
+ vfstraceOnOff(pInfo, VTR_FSIZE);
+ vfstrace_printf(pInfo, "%s.xFileSize(%s)", pInfo->zVfsName, p->zFName);
+ rc = p->pReal->pMethods->xFileSize(p->pReal, pSize);
+ vfstrace_print_errcode(pInfo, " -> %s,", rc);
+ vfstrace_printf(pInfo, " size=%lld\n", *pSize);
+ return rc;
+}
+
+/*
+** Return the name of a lock.
+*/
+static const char *lockName(int eLock){
+ const char *azLockNames[] = {
+ "NONE", "SHARED", "RESERVED", "PENDING", "EXCLUSIVE"
+ };
+ if( eLock<0 || eLock>=(int)(sizeof(azLockNames)/sizeof(azLockNames[0])) ){
+ return "???";
+ }else{
+ return azLockNames[eLock];
+ }
+}
+
+/*
+** Lock an vfstrace-file.
+*/
+static int vfstraceLock(sqlite3_file *pFile, int eLock){
+ vfstrace_file *p = (vfstrace_file *)pFile;
+ vfstrace_info *pInfo = p->pInfo;
+ int rc;
+ vfstraceOnOff(pInfo, VTR_LOCK);
+ vfstrace_printf(pInfo, "%s.xLock(%s,%s)", pInfo->zVfsName, p->zFName,
+ lockName(eLock));
+ rc = p->pReal->pMethods->xLock(p->pReal, eLock);
+ vfstrace_print_errcode(pInfo, " -> %s\n", rc);
+ return rc;
+}
+
+/*
+** Unlock an vfstrace-file.
+*/
+static int vfstraceUnlock(sqlite3_file *pFile, int eLock){
+ vfstrace_file *p = (vfstrace_file *)pFile;
+ vfstrace_info *pInfo = p->pInfo;
+ int rc;
+ vfstraceOnOff(pInfo, VTR_UNLOCK);
+ vfstrace_printf(pInfo, "%s.xUnlock(%s,%s)", pInfo->zVfsName, p->zFName,
+ lockName(eLock));
+ rc = p->pReal->pMethods->xUnlock(p->pReal, eLock);
+ vfstrace_print_errcode(pInfo, " -> %s\n", rc);
+ return rc;
+}
+
+/*
+** Check if another file-handle holds a RESERVED lock on an vfstrace-file.
+*/
+static int vfstraceCheckReservedLock(sqlite3_file *pFile, int *pResOut){
+ vfstrace_file *p = (vfstrace_file *)pFile;
+ vfstrace_info *pInfo = p->pInfo;
+ int rc;
+ vfstraceOnOff(pInfo, VTR_CRL);
+ vfstrace_printf(pInfo, "%s.xCheckReservedLock(%s,%d)",
+ pInfo->zVfsName, p->zFName);
+ rc = p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
+ vfstrace_print_errcode(pInfo, " -> %s", rc);
+ vfstrace_printf(pInfo, ", out=%d\n", *pResOut);
+ return rc;
+}
+
+/*
+** File control method. For custom operations on an vfstrace-file.
+*/
+static int vfstraceFileControl(sqlite3_file *pFile, int op, void *pArg){
+ vfstrace_file *p = (vfstrace_file *)pFile;
+ vfstrace_info *pInfo = p->pInfo;
+ int rc;
+ char zBuf[100];
+ char zBuf2[100];
+ char *zOp;
+ char *zRVal = 0;
+ vfstraceOnOff(pInfo, VTR_FCTRL);
+ switch( op ){
+ case SQLITE_FCNTL_LOCKSTATE: zOp = "LOCKSTATE"; break;
+ case SQLITE_GET_LOCKPROXYFILE: zOp = "GET_LOCKPROXYFILE"; break;
+ case SQLITE_SET_LOCKPROXYFILE: zOp = "SET_LOCKPROXYFILE"; break;
+ case SQLITE_LAST_ERRNO: zOp = "LAST_ERRNO"; break;
+ case SQLITE_FCNTL_SIZE_HINT: {
+ sqlite3_snprintf(sizeof(zBuf), zBuf, "SIZE_HINT,%lld",
+ *(sqlite3_int64*)pArg);
+ zOp = zBuf;
+ break;
+ }
+ case SQLITE_FCNTL_CHUNK_SIZE: {
+ sqlite3_snprintf(sizeof(zBuf), zBuf, "CHUNK_SIZE,%d", *(int*)pArg);
+ zOp = zBuf;
+ break;
+ }
+ case SQLITE_FCNTL_FILE_POINTER: zOp = "FILE_POINTER"; break;
+ case SQLITE_FCNTL_WIN32_AV_RETRY: zOp = "WIN32_AV_RETRY"; break;
+ case SQLITE_FCNTL_PERSIST_WAL: {
+ sqlite3_snprintf(sizeof(zBuf), zBuf, "PERSIST_WAL,%d", *(int*)pArg);
+ zOp = zBuf;
+ break;
+ }
+ case SQLITE_FCNTL_OVERWRITE: zOp = "OVERWRITE"; break;
+ case SQLITE_FCNTL_VFSNAME: zOp = "VFSNAME"; break;
+ case SQLITE_FCNTL_POWERSAFE_OVERWRITE: zOp = "POWERSAFE_OVERWRITE"; break;
+ case SQLITE_FCNTL_PRAGMA: {
+ const char *const* a = (const char*const*)pArg;
+ if( a[1] && strcmp(a[1],"vfstrace")==0 && a[2] ){
+ const u8 *zArg = (const u8*)a[2];
+ if( zArg[0]>='0' && zArg[0]<='9' ){
+ pInfo->mTrace = (sqlite3_uint64)strtoll(a[2], 0, 0);
+ }else{
+ static const struct {
+ const char *z;
+ unsigned int m;
+ } aKw[] = {
+ { "all", 0xffffffff },
+ { "close", VTR_CLOSE },
+ { "read", VTR_READ },
+ { "write", VTR_WRITE },
+ { "truncate", VTR_TRUNC },
+ { "sync", VTR_SYNC },
+ { "filesize", VTR_FSIZE },
+ { "lock", VTR_LOCK },
+ { "unlock", VTR_UNLOCK },
+ { "checkreservedlock", VTR_CRL },
+ { "filecontrol", VTR_FCTRL },
+ { "sectorsize", VTR_SECSZ },
+ { "devicecharacteristics", VTR_DEVCHAR },
+ { "shmlock", VTR_SHMLOCK },
+ { "shmmap", VTR_SHMMAP },
+ { "shmummap", VTR_SHMUNMAP },
+ { "shmbarrier", VTR_SHMBAR },
+ { "open", VTR_OPEN },
+ { "delete", VTR_DELETE },
+ { "access", VTR_ACCESS },
+ { "fullpathname", VTR_FULLPATH },
+ { "dlopen", VTR_DLOPEN },
+ { "dlerror", VTR_DLERR },
+ { "dlsym", VTR_DLSYM },
+ { "dlclose", VTR_DLCLOSE },
+ { "randomness", VTR_RAND },
+ { "sleep", VTR_SLEEP },
+ { "currenttime", VTR_CURTIME },
+ { "currenttimeint64", VTR_CURTIME },
+ { "getlasterror", VTR_LASTERR },
+ { "fetch", VTR_FETCH },
+ };
+ int onOff = 1;
+ while( zArg[0] ){
+ int jj, n;
+ while( zArg[0]!=0 && zArg[0]!='-' && zArg[0]!='+'
+ && !isalpha(zArg[0]) ) zArg++;
+ if( zArg[0]==0 ) break;
+ if( zArg[0]=='-' ){
+ onOff = 0;
+ zArg++;
+ }else if( zArg[0]=='+' ){
+ onOff = 1;
+ zArg++;
+ }
+ while( !isalpha(zArg[0]) ){
+ if( zArg[0]==0 ) break;
+ zArg++;
+ }
+ if( zArg[0]=='x' && isalpha(zArg[1]) ) zArg++;
+ for(n=0; isalpha(zArg[n]); n++){}
+ for(jj=0; jj<(int)(sizeof(aKw)/sizeof(aKw[0])); jj++){
+ if( sqlite3_strnicmp(aKw[jj].z,(const char*)zArg,n)==0 ){
+ if( onOff ){
+ pInfo->mTrace |= aKw[jj].m;
+ }else{
+ pInfo->mTrace &= ~aKw[jj].m;
+ }
+ break;
+ }
+ }
+ zArg += n;
+ }
+ }
+ }
+ sqlite3_snprintf(sizeof(zBuf), zBuf, "PRAGMA,[%s,%s]",a[1],a[2]);
+ zOp = zBuf;
+ break;
+ }
+ case SQLITE_FCNTL_BUSYHANDLER: zOp = "BUSYHANDLER"; break;
+ case SQLITE_FCNTL_TEMPFILENAME: zOp = "TEMPFILENAME"; break;
+ case SQLITE_FCNTL_MMAP_SIZE: {
+ sqlite3_int64 iMMap = *(sqlite3_int64*)pArg;
+ sqlite3_snprintf(sizeof(zBuf), zBuf, "MMAP_SIZE,%lld",iMMap);
+ zOp = zBuf;
+ break;
+ }
+ case SQLITE_FCNTL_TRACE: zOp = "TRACE"; break;
+ case SQLITE_FCNTL_HAS_MOVED: zOp = "HAS_MOVED"; break;
+ case SQLITE_FCNTL_SYNC: zOp = "SYNC"; break;
+ case SQLITE_FCNTL_COMMIT_PHASETWO: zOp = "COMMIT_PHASETWO"; break;
+ case SQLITE_FCNTL_WIN32_SET_HANDLE: zOp = "WIN32_SET_HANDLE"; break;
+ case SQLITE_FCNTL_WAL_BLOCK: zOp = "WAL_BLOCK"; break;
+ case SQLITE_FCNTL_ZIPVFS: zOp = "ZIPVFS"; break;
+ case SQLITE_FCNTL_RBU: zOp = "RBU"; break;
+ case SQLITE_FCNTL_VFS_POINTER: zOp = "VFS_POINTER"; break;
+ case SQLITE_FCNTL_JOURNAL_POINTER: zOp = "JOURNAL_POINTER"; break;
+ case SQLITE_FCNTL_WIN32_GET_HANDLE: zOp = "WIN32_GET_HANDLE"; break;
+ case SQLITE_FCNTL_PDB: zOp = "PDB"; break;
+ case SQLITE_FCNTL_BEGIN_ATOMIC_WRITE: zOp = "BEGIN_ATOMIC_WRITE"; break;
+ case SQLITE_FCNTL_COMMIT_ATOMIC_WRITE: zOp = "COMMIT_ATOMIC_WRITE"; break;
+ case SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE: {
+ zOp = "ROLLBACK_ATOMIC_WRITE";
+ break;
+ }
+ case SQLITE_FCNTL_LOCK_TIMEOUT: {
+ sqlite3_snprintf(sizeof(zBuf), zBuf, "LOCK_TIMEOUT,%d", *(int*)pArg);
+ zOp = zBuf;
+ break;
+ }
+ case SQLITE_FCNTL_DATA_VERSION: zOp = "DATA_VERSION"; break;
+ case SQLITE_FCNTL_SIZE_LIMIT: zOp = "SIZE_LIMIT"; break;
+ case SQLITE_FCNTL_CKPT_DONE: zOp = "CKPT_DONE"; break;
+ case SQLITE_FCNTL_RESERVE_BYTES: zOp = "RESERVED_BYTES"; break;
+ case SQLITE_FCNTL_CKPT_START: zOp = "CKPT_START"; break;
+ case SQLITE_FCNTL_EXTERNAL_READER: zOp = "EXTERNAL_READER"; break;
+ case SQLITE_FCNTL_CKSM_FILE: zOp = "CKSM_FILE"; break;
+ case SQLITE_FCNTL_RESET_CACHE: zOp = "RESET_CACHE"; break;
+ case 0xca093fa0: zOp = "DB_UNCHANGED"; break;
+ default: {
+ sqlite3_snprintf(sizeof zBuf, zBuf, "%d", op);
+ zOp = zBuf;
+ break;
+ }
+ }
+ vfstrace_printf(pInfo, "%s.xFileControl(%s,%s)",
+ pInfo->zVfsName, p->zFName, zOp);
+ rc = p->pReal->pMethods->xFileControl(p->pReal, op, pArg);
+ if( rc==SQLITE_OK ){
+ switch( op ){
+ case SQLITE_FCNTL_VFSNAME: {
+ *(char**)pArg = sqlite3_mprintf("vfstrace.%s/%z",
+ pInfo->zVfsName, *(char**)pArg);
+ zRVal = *(char**)pArg;
+ break;
+ }
+ case SQLITE_FCNTL_MMAP_SIZE: {
+ sqlite3_snprintf(sizeof(zBuf2), zBuf2, "%lld", *(sqlite3_int64*)pArg);
+ zRVal = zBuf2;
+ break;
+ }
+ case SQLITE_FCNTL_HAS_MOVED:
+ case SQLITE_FCNTL_PERSIST_WAL: {
+ sqlite3_snprintf(sizeof(zBuf2), zBuf2, "%d", *(int*)pArg);
+ zRVal = zBuf2;
+ break;
+ }
+ case SQLITE_FCNTL_PRAGMA:
+ case SQLITE_FCNTL_TEMPFILENAME: {
+ zRVal = *(char**)pArg;
+ break;
+ }
+ }
+ }
+ if( zRVal ){
+ vfstrace_print_errcode(pInfo, " -> %s", rc);
+ vfstrace_printf(pInfo, ", %s\n", zRVal);
+ }else{
+ vfstrace_print_errcode(pInfo, " -> %s\n", rc);
+ }
+ return rc;
+}
+
+/*
+** Return the sector-size in bytes for an vfstrace-file.
+*/
+static int vfstraceSectorSize(sqlite3_file *pFile){
+ vfstrace_file *p = (vfstrace_file *)pFile;
+ vfstrace_info *pInfo = p->pInfo;
+ int rc;
+ vfstraceOnOff(pInfo, VTR_SECSZ);
+ vfstrace_printf(pInfo, "%s.xSectorSize(%s)", pInfo->zVfsName, p->zFName);
+ rc = p->pReal->pMethods->xSectorSize(p->pReal);
+ vfstrace_printf(pInfo, " -> %d\n", rc);
+ return rc;
+}
+
+/*
+** Return the device characteristic flags supported by an vfstrace-file.
+*/
+static int vfstraceDeviceCharacteristics(sqlite3_file *pFile){
+ vfstrace_file *p = (vfstrace_file *)pFile;
+ vfstrace_info *pInfo = p->pInfo;
+ int rc;
+ vfstraceOnOff(pInfo, VTR_DEVCHAR);
+ vfstrace_printf(pInfo, "%s.xDeviceCharacteristics(%s)",
+ pInfo->zVfsName, p->zFName);
+ rc = p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
+ vfstrace_printf(pInfo, " -> 0x%08x\n", rc);
+ return rc;
+}
+
+/*
+** Shared-memory operations.
+*/
+static int vfstraceShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
+ static const char *azLockName[] = {
+ "WRITE",
+ "CKPT",
+ "RECOVER",
+ "READ0",
+ "READ1",
+ "READ2",
+ "READ3",
+ "READ4",
+ };
+ vfstrace_file *p = (vfstrace_file *)pFile;
+ vfstrace_info *pInfo = p->pInfo;
+ int rc;
+ char zLck[100];
+ int i = 0;
+ vfstraceOnOff(pInfo, VTR_SHMLOCK);
+ memcpy(zLck, "|0", 3);
+ if( flags & SQLITE_SHM_UNLOCK ) strappend(zLck, &i, "|UNLOCK");
+ if( flags & SQLITE_SHM_LOCK ) strappend(zLck, &i, "|LOCK");
+ if( flags & SQLITE_SHM_SHARED ) strappend(zLck, &i, "|SHARED");
+ if( flags & SQLITE_SHM_EXCLUSIVE ) strappend(zLck, &i, "|EXCLUSIVE");
+ if( flags & ~(0xf) ){
+ sqlite3_snprintf(sizeof(zLck)-i, &zLck[i], "|0x%x", flags);
+ }
+ if( ofst>=0 && ofst<(int)(sizeof(azLockName)/sizeof(azLockName[0])) ){
+ vfstrace_printf(pInfo, "%s.xShmLock(%s,ofst=%d(%s),n=%d,%s)",
+ pInfo->zVfsName, p->zFName, ofst, azLockName[ofst],
+ n, &zLck[1]);
+ }else{
+ vfstrace_printf(pInfo, "%s.xShmLock(%s,ofst=5d,n=%d,%s)",
+ pInfo->zVfsName, p->zFName, ofst,
+ n, &zLck[1]);
+ }
+ rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
+ vfstrace_print_errcode(pInfo, " -> %s\n", rc);
+ return rc;
+}
+static int vfstraceShmMap(
+ sqlite3_file *pFile,
+ int iRegion,
+ int szRegion,
+ int isWrite,
+ void volatile **pp
+){
+ vfstrace_file *p = (vfstrace_file *)pFile;
+ vfstrace_info *pInfo = p->pInfo;
+ int rc;
+ vfstraceOnOff(pInfo, VTR_SHMMAP);
+ vfstrace_printf(pInfo, "%s.xShmMap(%s,iRegion=%d,szRegion=%d,isWrite=%d,*)",
+ pInfo->zVfsName, p->zFName, iRegion, szRegion, isWrite);
+ rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp);
+ vfstrace_print_errcode(pInfo, " -> %s\n", rc);
+ return rc;
+}
+static void vfstraceShmBarrier(sqlite3_file *pFile){
+ vfstrace_file *p = (vfstrace_file *)pFile;
+ vfstrace_info *pInfo = p->pInfo;
+ vfstraceOnOff(pInfo, VTR_SHMBAR);
+ vfstrace_printf(pInfo, "%s.xShmBarrier(%s)\n", pInfo->zVfsName, p->zFName);
+ p->pReal->pMethods->xShmBarrier(p->pReal);
+}
+static int vfstraceShmUnmap(sqlite3_file *pFile, int delFlag){
+ vfstrace_file *p = (vfstrace_file *)pFile;
+ vfstrace_info *pInfo = p->pInfo;
+ int rc;
+ vfstraceOnOff(pInfo, VTR_SHMUNMAP);
+ vfstrace_printf(pInfo, "%s.xShmUnmap(%s,delFlag=%d)",
+ pInfo->zVfsName, p->zFName, delFlag);
+ rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
+ vfstrace_print_errcode(pInfo, " -> %s\n", rc);
+ return rc;
+}
+static int vfstraceFetch(sqlite3_file *pFile, i64 iOff, int nAmt, void **pptr){
+ vfstrace_file *p = (vfstrace_file *)pFile;
+ vfstrace_info *pInfo = p->pInfo;
+ int rc;
+ vfstraceOnOff(pInfo, VTR_FETCH);
+ vfstrace_printf(pInfo, "%s.xFetch(%s,iOff=%lld,nAmt=%d,p=%p)",
+ pInfo->zVfsName, p->zFName, iOff, nAmt, *pptr);
+ rc = p->pReal->pMethods->xFetch(p->pReal, iOff, nAmt, pptr);
+ vfstrace_print_errcode(pInfo, " -> %s\n", rc);
+ return rc;
+}
+static int vfstraceUnfetch(sqlite3_file *pFile, i64 iOff, void *ptr){
+ vfstrace_file *p = (vfstrace_file *)pFile;
+ vfstrace_info *pInfo = p->pInfo;
+ int rc;
+ vfstraceOnOff(pInfo, VTR_FETCH);
+ vfstrace_printf(pInfo, "%s.xUnfetch(%s,iOff=%lld,p=%p)",
+ pInfo->zVfsName, p->zFName, iOff, ptr);
+ rc = p->pReal->pMethods->xUnfetch(p->pReal, iOff, ptr);
+ vfstrace_print_errcode(pInfo, " -> %s\n", rc);
+ return rc;
+}
+
+
+/*
+** Open an vfstrace file handle.
+*/
+static int vfstraceOpen(
+ sqlite3_vfs *pVfs,
+ const char *zName,
+ sqlite3_file *pFile,
+ int flags,
+ int *pOutFlags
+){
+ int rc;
+ vfstrace_file *p = (vfstrace_file *)pFile;
+ vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
+ sqlite3_vfs *pRoot = pInfo->pRootVfs;
+ p->pInfo = pInfo;
+ p->zFName = zName ? fileTail(zName) : "<temp>";
+ p->pReal = (sqlite3_file *)&p[1];
+ rc = pRoot->xOpen(pRoot, zName, p->pReal, flags, pOutFlags);
+ vfstraceOnOff(pInfo, VTR_OPEN);
+ vfstrace_printf(pInfo, "%s.xOpen(%s,flags=0x%x)",
+ pInfo->zVfsName, p->zFName, flags);
+ if( p->pReal->pMethods ){
+ sqlite3_io_methods *pNew = sqlite3_malloc( sizeof(*pNew) );
+ const sqlite3_io_methods *pSub = p->pReal->pMethods;
+ memset(pNew, 0, sizeof(*pNew));
+ pNew->iVersion = pSub->iVersion;
+ pNew->xClose = vfstraceClose;
+ pNew->xRead = vfstraceRead;
+ pNew->xWrite = vfstraceWrite;
+ pNew->xTruncate = vfstraceTruncate;
+ pNew->xSync = vfstraceSync;
+ pNew->xFileSize = vfstraceFileSize;
+ pNew->xLock = vfstraceLock;
+ pNew->xUnlock = vfstraceUnlock;
+ pNew->xCheckReservedLock = vfstraceCheckReservedLock;
+ pNew->xFileControl = vfstraceFileControl;
+ pNew->xSectorSize = vfstraceSectorSize;
+ pNew->xDeviceCharacteristics = vfstraceDeviceCharacteristics;
+ if( pNew->iVersion>=2 ){
+ pNew->xShmMap = pSub->xShmMap ? vfstraceShmMap : 0;
+ pNew->xShmLock = pSub->xShmLock ? vfstraceShmLock : 0;
+ pNew->xShmBarrier = pSub->xShmBarrier ? vfstraceShmBarrier : 0;
+ pNew->xShmUnmap = pSub->xShmUnmap ? vfstraceShmUnmap : 0;
+ }
+ if( pNew->iVersion>=3 ){
+ pNew->xFetch = pSub->xFetch ? vfstraceFetch : 0;
+ pNew->xUnfetch = pSub->xUnfetch ? vfstraceUnfetch : 0;
+ }
+ pFile->pMethods = pNew;
+ }
+ vfstrace_print_errcode(pInfo, " -> %s", rc);
+ if( pOutFlags ){
+ vfstrace_printf(pInfo, ", outFlags=0x%x\n", *pOutFlags);
+ }else{
+ vfstrace_printf(pInfo, "\n");
+ }
+ return rc;
+}
+
+/*
+** Delete the file located at zPath. If the dirSync argument is true,
+** ensure the file-system modifications are synced to disk before
+** returning.
+*/
+static int vfstraceDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
+ vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
+ sqlite3_vfs *pRoot = pInfo->pRootVfs;
+ int rc;
+ vfstraceOnOff(pInfo, VTR_DELETE);
+ vfstrace_printf(pInfo, "%s.xDelete(\"%s\",%d)",
+ pInfo->zVfsName, zPath, dirSync);
+ rc = pRoot->xDelete(pRoot, zPath, dirSync);
+ vfstrace_print_errcode(pInfo, " -> %s\n", rc);
+ return rc;
+}
+
+/*
+** Test for access permissions. Return true if the requested permission
+** is available, or false otherwise.
+*/
+static int vfstraceAccess(
+ sqlite3_vfs *pVfs,
+ const char *zPath,
+ int flags,
+ int *pResOut
+){
+ vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
+ sqlite3_vfs *pRoot = pInfo->pRootVfs;
+ int rc;
+ vfstraceOnOff(pInfo, VTR_ACCESS);
+ vfstrace_printf(pInfo, "%s.xAccess(\"%s\",%d)",
+ pInfo->zVfsName, zPath, flags);
+ rc = pRoot->xAccess(pRoot, zPath, flags, pResOut);
+ vfstrace_print_errcode(pInfo, " -> %s", rc);
+ vfstrace_printf(pInfo, ", out=%d\n", *pResOut);
+ return rc;
+}
+
+/*
+** Populate buffer zOut with the full canonical pathname corresponding
+** to the pathname in zPath. zOut is guaranteed to point to a buffer
+** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
+*/
+static int vfstraceFullPathname(
+ sqlite3_vfs *pVfs,
+ const char *zPath,
+ int nOut,
+ char *zOut
+){
+ vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
+ sqlite3_vfs *pRoot = pInfo->pRootVfs;
+ int rc;
+ vfstraceOnOff(pInfo, VTR_FULLPATH);
+ vfstrace_printf(pInfo, "%s.xFullPathname(\"%s\")",
+ pInfo->zVfsName, zPath);
+ rc = pRoot->xFullPathname(pRoot, zPath, nOut, zOut);
+ vfstrace_print_errcode(pInfo, " -> %s", rc);
+ vfstrace_printf(pInfo, ", out=\"%.*s\"\n", nOut, zOut);
+ return rc;
+}
+
+/*
+** Open the dynamic library located at zPath and return a handle.
+*/
+static void *vfstraceDlOpen(sqlite3_vfs *pVfs, const char *zPath){
+ vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
+ sqlite3_vfs *pRoot = pInfo->pRootVfs;
+ vfstraceOnOff(pInfo, VTR_DLOPEN);
+ vfstrace_printf(pInfo, "%s.xDlOpen(\"%s\")\n", pInfo->zVfsName, zPath);
+ return pRoot->xDlOpen(pRoot, zPath);
+}
+
+/*
+** Populate the buffer zErrMsg (size nByte bytes) with a human readable
+** utf-8 string describing the most recent error encountered associated
+** with dynamic libraries.
+*/
+static void vfstraceDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
+ vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
+ sqlite3_vfs *pRoot = pInfo->pRootVfs;
+ vfstraceOnOff(pInfo, VTR_DLERR);
+ vfstrace_printf(pInfo, "%s.xDlError(%d)", pInfo->zVfsName, nByte);
+ pRoot->xDlError(pRoot, nByte, zErrMsg);
+ vfstrace_printf(pInfo, " -> \"%s\"", zErrMsg);
+}
+
+/*
+** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
+*/
+static void (*vfstraceDlSym(sqlite3_vfs *pVfs,void *p,const char *zSym))(void){
+ vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
+ sqlite3_vfs *pRoot = pInfo->pRootVfs;
+ vfstrace_printf(pInfo, "%s.xDlSym(\"%s\")\n", pInfo->zVfsName, zSym);
+ return pRoot->xDlSym(pRoot, p, zSym);
+}
+
+/*
+** Close the dynamic library handle pHandle.
+*/
+static void vfstraceDlClose(sqlite3_vfs *pVfs, void *pHandle){
+ vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
+ sqlite3_vfs *pRoot = pInfo->pRootVfs;
+ vfstraceOnOff(pInfo, VTR_DLCLOSE);
+ vfstrace_printf(pInfo, "%s.xDlClose()\n", pInfo->zVfsName);
+ pRoot->xDlClose(pRoot, pHandle);
+}
+
+/*
+** Populate the buffer pointed to by zBufOut with nByte bytes of
+** random data.
+*/
+static int vfstraceRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
+ vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
+ sqlite3_vfs *pRoot = pInfo->pRootVfs;
+ vfstraceOnOff(pInfo, VTR_RAND);
+ vfstrace_printf(pInfo, "%s.xRandomness(%d)\n", pInfo->zVfsName, nByte);
+ return pRoot->xRandomness(pRoot, nByte, zBufOut);
+}
+
+/*
+** Sleep for nMicro microseconds. Return the number of microseconds
+** actually slept.
+*/
+static int vfstraceSleep(sqlite3_vfs *pVfs, int nMicro){
+ vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
+ sqlite3_vfs *pRoot = pInfo->pRootVfs;
+ vfstraceOnOff(pInfo, VTR_SLEEP);
+ vfstrace_printf(pInfo, "%s.xSleep(%d)\n", pInfo->zVfsName, nMicro);
+ return pRoot->xSleep(pRoot, nMicro);
+}
+
+/*
+** Return the current time as a Julian Day number in *pTimeOut.
+*/
+static int vfstraceCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
+ vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
+ sqlite3_vfs *pRoot = pInfo->pRootVfs;
+ int rc;
+ vfstraceOnOff(pInfo, VTR_CURTIME);
+ vfstrace_printf(pInfo, "%s.xCurrentTime()", pInfo->zVfsName);
+ rc = pRoot->xCurrentTime(pRoot, pTimeOut);
+ vfstrace_printf(pInfo, " -> %.17g\n", *pTimeOut);
+ return rc;
+}
+static int vfstraceCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
+ vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
+ sqlite3_vfs *pRoot = pInfo->pRootVfs;
+ int rc;
+ vfstraceOnOff(pInfo, VTR_CURTIME);
+ vfstrace_printf(pInfo, "%s.xCurrentTimeInt64()", pInfo->zVfsName);
+ rc = pRoot->xCurrentTimeInt64(pRoot, pTimeOut);
+ vfstrace_printf(pInfo, " -> %lld\n", *pTimeOut);
+ return rc;
+}
+
+/*
+** Return the most recent error code and message
+*/
+static int vfstraceGetLastError(sqlite3_vfs *pVfs, int nErr, char *zErr){
+ vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
+ sqlite3_vfs *pRoot = pInfo->pRootVfs;
+ int rc;
+ vfstraceOnOff(pInfo, VTR_LASTERR);
+ vfstrace_printf(pInfo, "%s.xGetLastError(%d,zBuf)", pInfo->zVfsName, nErr);
+ if( nErr ) zErr[0] = 0;
+ rc = pRoot->xGetLastError(pRoot, nErr, zErr);
+ vfstrace_printf(pInfo, " -> zBuf[] = \"%s\", rc = %d\n", nErr?zErr:"", rc);
+ return rc;
+}
+
+/*
+** Override system calls.
+*/
+static int vfstraceSetSystemCall(
+ sqlite3_vfs *pVfs,
+ const char *zName,
+ sqlite3_syscall_ptr pFunc
+){
+ vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
+ sqlite3_vfs *pRoot = pInfo->pRootVfs;
+ return pRoot->xSetSystemCall(pRoot, zName, pFunc);
+}
+static sqlite3_syscall_ptr vfstraceGetSystemCall(
+ sqlite3_vfs *pVfs,
+ const char *zName
+){
+ vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
+ sqlite3_vfs *pRoot = pInfo->pRootVfs;
+ return pRoot->xGetSystemCall(pRoot, zName);
+}
+static const char *vfstraceNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
+ vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
+ sqlite3_vfs *pRoot = pInfo->pRootVfs;
+ return pRoot->xNextSystemCall(pRoot, zName);
+}
+
+
+/*
+** Clients invoke this routine to construct a new trace-vfs shim.
+**
+** Return SQLITE_OK on success.
+**
+** SQLITE_NOMEM is returned in the case of a memory allocation error.
+** SQLITE_NOTFOUND is returned if zOldVfsName does not exist.
+*/
+int vfstrace_register(
+ const char *zTraceName, /* Name of the newly constructed VFS */
+ const char *zOldVfsName, /* Name of the underlying VFS */
+ int (*xOut)(const char*,void*), /* Output routine. ex: fputs */
+ void *pOutArg, /* 2nd argument to xOut. ex: stderr */
+ int makeDefault /* True to make the new VFS the default */
+){
+ sqlite3_vfs *pNew;
+ sqlite3_vfs *pRoot;
+ vfstrace_info *pInfo;
+ size_t nName;
+ size_t nByte;
+
+ pRoot = sqlite3_vfs_find(zOldVfsName);
+ if( pRoot==0 ) return SQLITE_NOTFOUND;
+ nName = strlen(zTraceName);
+ nByte = sizeof(*pNew) + sizeof(*pInfo) + nName + 1;
+ pNew = sqlite3_malloc64( nByte );
+ if( pNew==0 ) return SQLITE_NOMEM;
+ memset(pNew, 0, nByte);
+ pInfo = (vfstrace_info*)&pNew[1];
+ pNew->iVersion = pRoot->iVersion;
+ pNew->szOsFile = pRoot->szOsFile + sizeof(vfstrace_file);
+ pNew->mxPathname = pRoot->mxPathname;
+ pNew->zName = (char*)&pInfo[1];
+ memcpy((char*)&pInfo[1], zTraceName, nName+1);
+ pNew->pAppData = pInfo;
+ pNew->xOpen = vfstraceOpen;
+ pNew->xDelete = vfstraceDelete;
+ pNew->xAccess = vfstraceAccess;
+ pNew->xFullPathname = vfstraceFullPathname;
+ pNew->xDlOpen = pRoot->xDlOpen==0 ? 0 : vfstraceDlOpen;
+ pNew->xDlError = pRoot->xDlError==0 ? 0 : vfstraceDlError;
+ pNew->xDlSym = pRoot->xDlSym==0 ? 0 : vfstraceDlSym;
+ pNew->xDlClose = pRoot->xDlClose==0 ? 0 : vfstraceDlClose;
+ pNew->xRandomness = vfstraceRandomness;
+ pNew->xSleep = vfstraceSleep;
+ pNew->xCurrentTime = vfstraceCurrentTime;
+ pNew->xGetLastError = pRoot->xGetLastError==0 ? 0 : vfstraceGetLastError;
+ if( pNew->iVersion>=2 ){
+ pNew->xCurrentTimeInt64 = pRoot->xCurrentTimeInt64==0 ? 0 :
+ vfstraceCurrentTimeInt64;
+ if( pNew->iVersion>=3 ){
+ pNew->xSetSystemCall = pRoot->xSetSystemCall==0 ? 0 :
+ vfstraceSetSystemCall;
+ pNew->xGetSystemCall = pRoot->xGetSystemCall==0 ? 0 :
+ vfstraceGetSystemCall;
+ pNew->xNextSystemCall = pRoot->xNextSystemCall==0 ? 0 :
+ vfstraceNextSystemCall;
+ }
+ }
+ pInfo->pRootVfs = pRoot;
+ pInfo->xOut = xOut;
+ pInfo->pOutArg = pOutArg;
+ pInfo->zVfsName = pNew->zName;
+ pInfo->pTraceVfs = pNew;
+ pInfo->mTrace = 0xffffffff;
+ pInfo->bOn = 1;
+ vfstrace_printf(pInfo, "%s.enabled_for(\"%s\")\n",
+ pInfo->zVfsName, pRoot->zName);
+ return sqlite3_vfs_register(pNew, makeDefault);
+}
+
+/*
+** Look for the named VFS. If it is a TRACEVFS, then unregister it
+** and delete it.
+*/
+void vfstrace_unregister(const char *zTraceName){
+ sqlite3_vfs *pVfs = sqlite3_vfs_find(zTraceName);
+ if( pVfs==0 ) return;
+ if( pVfs->xOpen!=vfstraceOpen ) return;
+ sqlite3_vfs_unregister(pVfs);
+ sqlite3_free(pVfs);
+}
+
+/************************* End ../ext/misc/vfstrace.c ********************/
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
#define SQLITE_SHELL_HAVE_RECOVER 1
@@ -16129,6 +18362,7 @@ static int dbdataNext(sqlite3_vtab_cursor *pCursor){
** near the end of a corrupt record. */
rc = dbdataBufferSize(&pCsr->rec, nPayload+DBDATA_PADDING_BYTES);
if( rc!=SQLITE_OK ) return rc;
+ assert( pCsr->rec.aBuf!=0 );
assert( nPayload!=0 );
/* Load the nLocal bytes of payload */
@@ -16457,6 +18691,9 @@ static int sqlite3DbdataRegister(sqlite3 *db){
return rc;
}
+#ifdef _WIN32
+
+#endif
int sqlite3_dbdata_init(
sqlite3 *db,
char **pzErrMsg,
@@ -16505,6 +18742,16 @@ int sqlite3_dbdata_init(sqlite3*, char**, const sqlite3_api_routines*);
/* typedef unsigned char u8; */
/* typedef sqlite3_int64 i64; */
+/*
+** Work around C99 "flex-array" syntax for pre-C99 compilers, so as
+** to avoid complaints from -fsanitize=strict-bounds.
+*/
+#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
+# define FLEXARRAY
+#else
+# define FLEXARRAY 1
+#endif
+
typedef struct RecoverTable RecoverTable;
typedef struct RecoverColumn RecoverColumn;
@@ -16612,9 +18859,12 @@ struct RecoverColumn {
typedef struct RecoverBitmap RecoverBitmap;
struct RecoverBitmap {
i64 nPg; /* Size of bitmap */
- u32 aElem[1]; /* Array of 32-bit bitmasks */
+ u32 aElem[FLEXARRAY]; /* Array of 32-bit bitmasks */
};
+/* Size in bytes of a RecoverBitmap object sufficient to cover 32 pages */
+#define SZ_RECOVERBITMAP_32 (16)
+
/*
** State variables (part of the sqlite3_recover structure) used while
** recovering data for tables identified in the recovered schema (state
@@ -16854,7 +19104,7 @@ static int recoverError(
*/
static RecoverBitmap *recoverBitmapAlloc(sqlite3_recover *p, i64 nPg){
int nElem = (nPg+1+31) / 32;
- int nByte = sizeof(RecoverBitmap) + nElem*sizeof(u32);
+ int nByte = SZ_RECOVERBITMAP_32 + nElem*sizeof(u32);
RecoverBitmap *pRet = (RecoverBitmap*)recoverMalloc(p, nByte);
if( pRet ){
@@ -17216,7 +19466,7 @@ static const char *recoverUnusedString(
}
/*
-** Implementation of scalar SQL function "escape_crnl". The argument passed to
+** Implementation of scalar SQL function "escape_crlf". The argument passed to
** this function is the output of built-in function quote(). If the first
** character of the input is "'", indicating that the value passed to quote()
** was a text value, then this function searches the input for "\n" and "\r"
@@ -17227,7 +19477,7 @@ static const char *recoverUnusedString(
** Or, if the first character of the input is not "'", then a copy of the input
** is returned.
*/
-static void recoverEscapeCrnl(
+static void recoverEscapeCrlf(
sqlite3_context *context,
int argc,
sqlite3_value **argv
@@ -17442,7 +19692,7 @@ static int recoverOpenOutput(sqlite3_recover *p){
{ "getpage", 1, recoverGetPage },
{ "page_is_used", 1, recoverPageIsUsed },
{ "read_i32", 2, recoverReadI32 },
- { "escape_crnl", 1, recoverEscapeCrnl },
+ { "escape_crlf", 1, recoverEscapeCrlf },
};
const int flags = SQLITE_OPEN_URI|SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE;
@@ -17795,7 +20045,7 @@ static sqlite3_stmt *recoverInsertStmt(
if( bSql ){
zBind = recoverMPrintf(p,
- "%z%sescape_crnl(quote(?%d))", zBind, zSqlSep, pTab->aCol[ii].iBind
+ "%z%sescape_crlf(quote(?%d))", zBind, zSqlSep, pTab->aCol[ii].iBind
);
zSqlSep = "||', '||";
}else{
@@ -18297,6 +20547,8 @@ static int recoverWriteDataStep(sqlite3_recover *p){
recoverError(p, SQLITE_NOMEM, 0);
}
p1->nVal = iField+1;
+ }else if( pTab->nCol==0 ){
+ p1->nVal = pTab->nCol;
}
p1->iPrevCell = iCell;
p1->iPrevPage = iPage;
@@ -19044,37 +21296,53 @@ static void recoverUninstallWrapper(sqlite3_recover *p){
static void recoverStep(sqlite3_recover *p){
assert( p && p->errCode==SQLITE_OK );
switch( p->eState ){
- case RECOVER_STATE_INIT:
+ case RECOVER_STATE_INIT: {
+ int bUseWrapper = 1;
/* This is the very first call to sqlite3_recover_step() on this object.
*/
recoverSqlCallback(p, "BEGIN");
recoverSqlCallback(p, "PRAGMA writable_schema = on");
+ recoverSqlCallback(p, "PRAGMA foreign_keys = off");
recoverEnterMutex();
- recoverInstallWrapper(p);
/* Open the output database. And register required virtual tables and
** user functions with the new handle. */
recoverOpenOutput(p);
- /* Open transactions on both the input and output databases. */
- sqlite3_file_control(p->dbIn, p->zDb, SQLITE_FCNTL_RESET_CACHE, 0);
- recoverExec(p, p->dbIn, "PRAGMA writable_schema = on");
- recoverExec(p, p->dbIn, "BEGIN");
- if( p->errCode==SQLITE_OK ) p->bCloseTransaction = 1;
- recoverExec(p, p->dbIn, "SELECT 1 FROM sqlite_schema");
- recoverTransferSettings(p);
- recoverOpenRecovery(p);
- recoverCacheSchema(p);
-
- recoverUninstallWrapper(p);
- recoverLeaveMutex();
+ /* Attempt to open a transaction and read page 1 of the input database.
+ ** Two attempts may be made - one with a wrapper installed to ensure
+ ** that the database header is sane, and then if that attempt returns
+ ** SQLITE_NOTADB, then again with no wrapper. The second attempt is
+ ** required for encrypted databases. */
+ if( p->errCode==SQLITE_OK ){
+ do{
+ p->errCode = SQLITE_OK;
+ if( bUseWrapper ) recoverInstallWrapper(p);
+
+ /* Open a transaction on the input database. */
+ sqlite3_file_control(p->dbIn, p->zDb, SQLITE_FCNTL_RESET_CACHE, 0);
+ recoverExec(p, p->dbIn, "PRAGMA writable_schema = on");
+ recoverExec(p, p->dbIn, "BEGIN");
+ if( p->errCode==SQLITE_OK ) p->bCloseTransaction = 1;
+ recoverExec(p, p->dbIn, "SELECT 1 FROM sqlite_schema");
+ recoverTransferSettings(p);
+ recoverOpenRecovery(p);
+ recoverCacheSchema(p);
+
+ if( bUseWrapper ) recoverUninstallWrapper(p);
+ }while( p->errCode==SQLITE_NOTADB
+ && (bUseWrapper--)
+ && SQLITE_OK==sqlite3_exec(p->dbIn, "ROLLBACK", 0, 0, 0)
+ );
+ }
+ recoverLeaveMutex();
recoverExec(p, p->dbOut, "BEGIN");
-
recoverWriteSchema1(p);
p->eState = RECOVER_STATE_WRITING;
break;
+ }
case RECOVER_STATE_WRITING: {
if( p->w1.pTbls==0 ){
@@ -19412,6 +21680,8 @@ struct ShellState {
u8 bSafeMode; /* True to prohibit unsafe operations */
u8 bSafeModePersist; /* The long-term value of bSafeMode */
u8 eRestoreState; /* See comments above doAutoDetectRestore() */
+ u8 crlfMode; /* Do NL-to-CRLF translations when enabled (maybe) */
+ u8 eEscMode; /* Escape mode for text output */
ColModeOpts cmOpts; /* Option values affecting columnar mode output */
unsigned statsOn; /* True to display memory stats before each finalize */
unsigned mEqpLines; /* Mask of vertical lines in the EQP output graph */
@@ -19512,6 +21782,15 @@ static ShellState shellState;
** top-level SQL statement */
#define SHELL_PROGRESS_ONCE 0x04 /* Cancel the --limit after firing once */
+/* Allowed values for ShellState.eEscMode. The default value should
+** be 0, so to change the default, reorder the names.
+*/
+#define SHELL_ESC_ASCII 0 /* Substitute ^Y for X where Y=X+0x40 */
+#define SHELL_ESC_SYMBOL 1 /* Substitute U+2400 graphics */
+#define SHELL_ESC_OFF 2 /* Send characters verbatim */
+
+static const char *shell_EscModeNames[] = { "ascii", "symbol", "off" };
+
/*
** These are the allowed shellFlgs values
*/
@@ -19557,6 +21836,7 @@ static ShellState shellState;
#define MODE_Count 17 /* Output only a count of the rows of output */
#define MODE_Off 18 /* No query output shown */
#define MODE_ScanExp 19 /* Like MODE_Explain, but for ".scanstats vm" */
+#define MODE_Www 20 /* Full web-page output */
static const char *modeDescr[] = {
"line",
@@ -19577,7 +21857,9 @@ static const char *modeDescr[] = {
"table",
"box",
"count",
- "off"
+ "off",
+ "scanexp",
+ "www",
};
/*
@@ -19605,7 +21887,7 @@ static const char *modeDescr[] = {
static void shellLog(void *pArg, int iErrCode, const char *zMsg){
ShellState *p = (ShellState*)pArg;
if( p->pLog==0 ) return;
- sputf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
+ sqlite3_fprintf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
fflush(p->pLog);
}
@@ -19620,9 +21902,9 @@ static void shellPutsFunc(
int nVal,
sqlite3_value **apVal
){
- /* Unused: (ShellState*)sqlite3_user_data(pCtx); */
+ ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
(void)nVal;
- oputf("%s\n", sqlite3_value_text(apVal[0]));
+ sqlite3_fprintf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
sqlite3_result_value(pCtx, apVal[0]);
}
@@ -19641,7 +21923,7 @@ static void failIfSafeMode(
va_start(ap, zErrMsg);
zMsg = sqlite3_vmprintf(zErrMsg, ap);
va_end(ap);
- eputf("line %d: %s\n", p->lineno, zMsg);
+ sqlite3_fprintf(stderr, "line %d: %s\n", p->lineno, zMsg);
exit(1);
}
}
@@ -19674,7 +21956,7 @@ static void editFunc(
char *zCmd = 0;
int bBin;
int rc;
- int hasCRNL = 0;
+ int hasCRLF = 0;
FILE *f = 0;
sqlite3_int64 sz;
sqlite3_int64 x;
@@ -19708,7 +21990,7 @@ static void editFunc(
bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
/* When writing the file to be edited, do \n to \r\n conversions on systems
** that want \r\n line endings */
- f = fopen(zTempFile, bBin ? "wb" : "w");
+ f = sqlite3_fopen(zTempFile, bBin ? "wb" : "w");
if( f==0 ){
sqlite3_result_error(context, "edit() cannot open temp file", -1);
goto edit_func_end;
@@ -19719,7 +22001,7 @@ static void editFunc(
}else{
const char *z = (const char*)sqlite3_value_text(argv[0]);
/* Remember whether or not the value originally contained \r\n */
- if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
+ if( z && strstr(z,"\r\n")!=0 ) hasCRLF = 1;
x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
}
fclose(f);
@@ -19739,7 +22021,7 @@ static void editFunc(
sqlite3_result_error(context, "EDITOR returned non-zero", -1);
goto edit_func_end;
}
- f = fopen(zTempFile, "rb");
+ f = sqlite3_fopen(zTempFile, "rb");
if( f==0 ){
sqlite3_result_error(context,
"edit() cannot reopen temp file after edit", -1);
@@ -19764,7 +22046,7 @@ static void editFunc(
sqlite3_result_blob64(context, p, sz, sqlite3_free);
}else{
sqlite3_int64 i, j;
- if( hasCRNL ){
+ if( hasCRLF ){
/* If the original contains \r\n then do no conversions back to \n */
}else{
/* If the file did not originally contain \r\n then convert any new
@@ -19807,9 +22089,24 @@ static void outputModePop(ShellState *p){
}
/*
+** Set output mode to text or binary for Windows.
+*/
+static void setCrlfMode(ShellState *p){
+#ifdef _WIN32
+ if( p->crlfMode ){
+ sqlite3_fsetmode(p->out, _O_TEXT);
+ }else{
+ sqlite3_fsetmode(p->out, _O_BINARY);
+ }
+#else
+ UNUSED_PARAMETER(p);
+#endif
+}
+
+/*
** Output the given string as a hex-encoded blob (eg. X'1234' )
*/
-static void output_hex_blob(const void *pBlob, int nBlob){
+static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
int i;
unsigned char *aBlob = (unsigned char*)pBlob;
@@ -19826,72 +22123,82 @@ static void output_hex_blob(const void *pBlob, int nBlob){
}
zStr[i*2] = '\0';
- oputf("X'%s'", zStr);
+ sqlite3_fprintf(out, "X'%s'", zStr);
sqlite3_free(zStr);
}
/*
-** Find a string that is not found anywhere in z[]. Return a pointer
-** to that string.
+** Output the given string as a quoted string using SQL quoting conventions:
**
-** Try to use zA and zB first. If both of those are already found in z[]
-** then make up some string and store it in the buffer zBuf.
-*/
-static const char *unused_string(
- const char *z, /* Result must not appear anywhere in z */
- const char *zA, const char *zB, /* Try these first */
- char *zBuf /* Space to store a generated string */
-){
- unsigned i = 0;
- if( strstr(z, zA)==0 ) return zA;
- if( strstr(z, zB)==0 ) return zB;
- do{
- sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
- }while( strstr(z,zBuf)!=0 );
- return zBuf;
-}
-
-/*
-** Output the given string as a quoted string using SQL quoting conventions.
+** (1) Single quotes (') within the string are doubled
+** (2) The whle string is enclosed in '...'
+** (3) Control characters other than \n, \t, and \r\n are escaped
+** using \u00XX notation and if such substitutions occur,
+** the whole string is enclosed in unistr('...') instead of '...'.
+**
+** Step (3) is omitted if the control-character escape mode is OFF.
**
-** See also: output_quoted_escaped_string()
+** See also: output_quoted_escaped_string() which does the same except
+** that it does not make exceptions for \n, \t, and \r\n in step (3).
*/
-static void output_quoted_string(const char *z){
+static void output_quoted_string(ShellState *p, const char *zInX){
int i;
- char c;
-#ifndef SQLITE_SHELL_FIDDLE
- FILE *pfO = setOutputStream(invalidFileStream);
- setBinaryMode(pfO, 1);
-#endif
+ int needUnistr = 0;
+ int needDblQuote = 0;
+ const unsigned char *z = (const unsigned char*)zInX;
+ unsigned char c;
+ FILE *out = p->out;
+ sqlite3_fsetmode(out, _O_BINARY);
if( z==0 ) return;
- for(i=0; (c = z[i])!=0 && c!='\''; i++){}
- if( c==0 ){
- oputf("'%s'",z);
+ for(i=0; (c = z[i])!=0; i++){
+ if( c=='\'' ){ needDblQuote = 1; }
+ if( c>0x1f ) continue;
+ if( c=='\t' || c=='\n' ) continue;
+ if( c=='\r' && z[i+1]=='\n' ) continue;
+ needUnistr = 1;
+ break;
+ }
+ if( (needDblQuote==0 && needUnistr==0)
+ || (needDblQuote==0 && p->eEscMode==SHELL_ESC_OFF)
+ ){
+ sqlite3_fprintf(out, "'%s'",z);
+ }else if( p->eEscMode==SHELL_ESC_OFF ){
+ char *zEncoded = sqlite3_mprintf("%Q", z);
+ sqlite3_fputs(zEncoded, out);
+ sqlite3_free(zEncoded);
}else{
- oputz("'");
+ if( needUnistr ){
+ sqlite3_fputs("unistr('", out);
+ }else{
+ sqlite3_fputs("'", out);
+ }
while( *z ){
- for(i=0; (c = z[i])!=0 && c!='\''; i++){}
- if( c=='\'' ) i++;
+ for(i=0; (c = z[i])!=0; i++){
+ if( c=='\'' ) break;
+ if( c>0x1f ) continue;
+ if( c=='\t' || c=='\n' ) continue;
+ if( c=='\r' && z[i+1]=='\n' ) continue;
+ break;
+ }
if( i ){
- oputf("%.*s", i, z);
+ sqlite3_fprintf(out, "%.*s", i, z);
z += i;
}
+ if( c==0 ) break;
if( c=='\'' ){
- oputz("'");
- continue;
- }
- if( c==0 ){
- break;
+ sqlite3_fputs("''", out);
+ }else{
+ sqlite3_fprintf(out, "\\u%04x", c);
}
z++;
}
- oputz("'");
+ if( needUnistr ){
+ sqlite3_fputs("')", out);
+ }else{
+ sqlite3_fputs("'", out);
+ }
}
-#ifndef SQLITE_SHELL_FIDDLE
- setTextMode(pfO, 1);
-#else
- setTextMode(stdout, 1);
-#endif
+ setCrlfMode(p);
}
/*
@@ -19903,69 +22210,17 @@ static void output_quoted_string(const char *z){
** This is like output_quoted_string() but with the addition of the \r\n
** escape mechanism.
*/
-static void output_quoted_escaped_string(const char *z){
- int i;
- char c;
-#ifndef SQLITE_SHELL_FIDDLE
- FILE *pfO = setOutputStream(invalidFileStream);
- setBinaryMode(pfO, 1);
-#endif
- for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
- if( c==0 ){
- oputf("'%s'",z);
+static void output_quoted_escaped_string(ShellState *p, const char *z){
+ char *zEscaped;
+ sqlite3_fsetmode(p->out, _O_BINARY);
+ if( p->eEscMode==SHELL_ESC_OFF ){
+ zEscaped = sqlite3_mprintf("%Q", z);
}else{
- const char *zNL = 0;
- const char *zCR = 0;
- int nNL = 0;
- int nCR = 0;
- char zBuf1[20], zBuf2[20];
- for(i=0; z[i]; i++){
- if( z[i]=='\n' ) nNL++;
- if( z[i]=='\r' ) nCR++;
- }
- if( nNL ){
- oputz("replace(");
- zNL = unused_string(z, "\\n", "\\012", zBuf1);
- }
- if( nCR ){
- oputz("replace(");
- zCR = unused_string(z, "\\r", "\\015", zBuf2);
- }
- oputz("'");
- while( *z ){
- for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
- if( c=='\'' ) i++;
- if( i ){
- oputf("%.*s", i, z);
- z += i;
- }
- if( c=='\'' ){
- oputz("'");
- continue;
- }
- if( c==0 ){
- break;
- }
- z++;
- if( c=='\n' ){
- oputz(zNL);
- continue;
- }
- oputz(zCR);
- }
- oputz("'");
- if( nCR ){
- oputf(",'%s',char(13))", zCR);
- }
- if( nNL ){
- oputf(",'%s',char(10))", zNL);
- }
+ zEscaped = sqlite3_mprintf("%#Q", z);
}
-#ifndef SQLITE_SHELL_FIDDLE
- setTextMode(pfO, 1);
-#else
- setTextMode(stdout, 1);
-#endif
+ sqlite3_fputs(zEscaped, p->out);
+ sqlite3_free(zEscaped);
+ setCrlfMode(p);
}
/*
@@ -19985,22 +22240,60 @@ static const char *anyOfInStr(const char *s, const char *zAny, size_t ns){
}
return pcFirst;
}
+
+/* Skip over as much z[] input char sequence as is valid UTF-8,
+** limited per nAccept char's or whole characters and containing
+** no char cn such that ((1<<cn) & ccm)!=0. On return, the
+** sequence z:return (inclusive:exclusive) is validated UTF-8.
+** Limit: nAccept>=0 => char count, nAccept<0 => character
+ */
+const char *zSkipValidUtf8(const char *z, int nAccept, long ccm){
+ int ng = (nAccept<0)? -nAccept : 0;
+ const char *pcLimit = (nAccept>=0)? z+nAccept : 0;
+ assert(z!=0);
+ while( (pcLimit)? (z<pcLimit) : (ng-- != 0) ){
+ unsigned char c = *(u8*)z;
+ if( c<0x7f ){
+ if( ccm != 0L && c < 0x20 && ((1L<<c) & ccm) != 0 ) return z;
+ ++z; /* ASCII */
+ }else if( (c & 0xC0) != 0xC0 ) return z; /* not a lead byte */
+ else{
+ const char *zt = z+1; /* Got lead byte, look at trail bytes.*/
+ do{
+ if( pcLimit && zt >= pcLimit ) return z;
+ else{
+ char ct = *zt++;
+ if( ct==0 || (zt-z)>4 || (ct & 0xC0)!=0x80 ){
+ /* Trailing bytes are too few, too many, or invalid. */
+ return z;
+ }
+ }
+ } while( ((c <<= 1) & 0x40) == 0x40 ); /* Eat lead byte's count. */
+ z = zt;
+ }
+ }
+ return z;
+}
+
+
/*
** Output the given string as a quoted according to C or TCL quoting rules.
*/
-static void output_c_string(const char *z){
+static void output_c_string(FILE *out, const char *z){
char c;
static const char *zq = "\"";
static long ctrlMask = ~0L;
static const char *zDQBSRO = "\"\\\x7f"; /* double-quote, backslash, rubout */
char ace[3] = "\\?";
char cbsSay;
- oputz(zq);
+ sqlite3_fputs(zq, out);
while( *z!=0 ){
const char *pcDQBSRO = anyOfInStr(z, zDQBSRO, ~(size_t)0);
const char *pcPast = zSkipValidUtf8(z, INT_MAX, ctrlMask);
const char *pcEnd = (pcDQBSRO && pcDQBSRO < pcPast)? pcDQBSRO : pcPast;
- if( pcEnd > z ) oputb(z, (int)(pcEnd-z));
+ if( pcEnd > z ){
+ sqlite3_fprintf(out, "%.*s", (int)(pcEnd-z), z);
+ }
if( (c = *pcEnd)==0 ) break;
++pcEnd;
switch( c ){
@@ -20015,23 +22308,23 @@ static void output_c_string(const char *z){
}
if( cbsSay ){
ace[1] = cbsSay;
- oputz(ace);
+ sqlite3_fputs(ace, out);
}else if( !isprint(c&0xff) ){
- oputf("\\%03o", c&0xff);
+ sqlite3_fprintf(out, "\\%03o", c&0xff);
}else{
ace[1] = (char)c;
- oputz(ace+1);
+ sqlite3_fputs(ace+1, out);
}
z = pcEnd;
}
- oputz(zq);
+ sqlite3_fputs(zq, out);
}
/*
-** Output the given string as a quoted according to JSON quoting rules.
+** Output the given string as quoted according to JSON quoting rules.
*/
-static void output_json_string(const char *z, i64 n){
- char c;
+static void output_json_string(FILE *out, const char *z, i64 n){
+ unsigned char c;
static const char *zq = "\"";
static long ctrlMask = ~0L;
static const char *zDQBS = "\"\\";
@@ -20041,17 +22334,17 @@ static void output_json_string(const char *z, i64 n){
if( z==0 ) z = "";
pcLimit = z + ((n<0)? strlen(z) : (size_t)n);
- oputz(zq);
+ sqlite3_fputs(zq, out);
while( z < pcLimit ){
const char *pcDQBS = anyOfInStr(z, zDQBS, pcLimit-z);
const char *pcPast = zSkipValidUtf8(z, (int)(pcLimit-z), ctrlMask);
const char *pcEnd = (pcDQBS && pcDQBS < pcPast)? pcDQBS : pcPast;
if( pcEnd > z ){
- oputb(z, (int)(pcEnd-z));
+ sqlite3_fprintf(out, "%.*s", (int)(pcEnd-z), z);
z = pcEnd;
}
if( z >= pcLimit ) break;
- c = *(z++);
+ c = (unsigned char)*(z++);
switch( c ){
case '"': case '\\':
cbsSay = (char)c;
@@ -20065,22 +22358,109 @@ static void output_json_string(const char *z, i64 n){
}
if( cbsSay ){
ace[1] = cbsSay;
- oputz(ace);
- }else if( c<=0x1f ){
- oputf("u%04x", c);
+ sqlite3_fputs(ace, out);
+ }else if( c<=0x1f || c>=0x7f ){
+ sqlite3_fprintf(out, "\\u%04x", c);
}else{
ace[1] = (char)c;
- oputz(ace+1);
+ sqlite3_fputs(ace+1, out);
+ }
+ }
+ sqlite3_fputs(zq, out);
+}
+
+/*
+** Escape the input string if it is needed and in accordance with
+** eEscMode.
+**
+** Escaping is needed if the string contains any control characters
+** other than \t, \n, and \r\n
+**
+** If no escaping is needed (the common case) then set *ppFree to NULL
+** and return the original string. If escapingn is needed, write the
+** escaped string into memory obtained from sqlite3_malloc64() or the
+** equivalent, and return the new string and set *ppFree to the new string
+** as well.
+**
+** The caller is responsible for freeing *ppFree if it is non-NULL in order
+** to reclaim memory.
+*/
+static const char *escapeOutput(
+ ShellState *p,
+ const char *zInX,
+ char **ppFree
+){
+ i64 i, j;
+ i64 nCtrl = 0;
+ unsigned char *zIn;
+ unsigned char c;
+ unsigned char *zOut;
+
+
+ /* No escaping if disabled */
+ if( p->eEscMode==SHELL_ESC_OFF ){
+ *ppFree = 0;
+ return zInX;
+ }
+
+ /* Count the number of control characters in the string. */
+ zIn = (unsigned char*)zInX;
+ for(i=0; (c = zIn[i])!=0; i++){
+ if( c<=0x1f
+ && c!='\t'
+ && c!='\n'
+ && (c!='\r' || zIn[i+1]!='\n')
+ ){
+ nCtrl++;
+ }
+ }
+ if( nCtrl==0 ){
+ *ppFree = 0;
+ return zInX;
+ }
+ if( p->eEscMode==SHELL_ESC_SYMBOL ) nCtrl *= 2;
+ zOut = sqlite3_malloc64( i + nCtrl + 1 );
+ shell_check_oom(zOut);
+ for(i=j=0; (c = zIn[i])!=0; i++){
+ if( c>0x1f
+ || c=='\t'
+ || c=='\n'
+ || (c=='\r' && zIn[i+1]=='\n')
+ ){
+ continue;
}
+ if( i>0 ){
+ memcpy(&zOut[j], zIn, i);
+ j += i;
+ }
+ zIn += i+1;
+ i = -1;
+ switch( p->eEscMode ){
+ case SHELL_ESC_SYMBOL:
+ zOut[j++] = 0xe2;
+ zOut[j++] = 0x90;
+ zOut[j++] = 0x80+c;
+ break;
+ case SHELL_ESC_ASCII:
+ zOut[j++] = '^';
+ zOut[j++] = 0x40+c;
+ break;
+ }
+ }
+ if( i>0 ){
+ memcpy(&zOut[j], zIn, i);
+ j += i;
}
- oputz(zq);
+ zOut[j] = 0;
+ *ppFree = (char*)zOut;
+ return (char*)zOut;
}
/*
** Output the given string with characters that are special to
** HTML escaped.
*/
-static void output_html_string(const char *z){
+static void output_html_string(FILE *out, const char *z){
int i;
if( z==0 ) z = "";
while( *z ){
@@ -20092,18 +22472,18 @@ static void output_html_string(const char *z){
&& z[i]!='\'';
i++){}
if( i>0 ){
- oputf("%.*s",i,z);
+ sqlite3_fprintf(out, "%.*s",i,z);
}
if( z[i]=='<' ){
- oputz("&lt;");
+ sqlite3_fputs("&lt;", out);
}else if( z[i]=='&' ){
- oputz("&amp;");
+ sqlite3_fputs("&amp;", out);
}else if( z[i]=='>' ){
- oputz("&gt;");
+ sqlite3_fputs("&gt;", out);
}else if( z[i]=='\"' ){
- oputz("&quot;");
+ sqlite3_fputs("&quot;", out);
}else if( z[i]=='\'' ){
- oputz("&#39;");
+ sqlite3_fputs("&#39;", out);
}else{
break;
}
@@ -20142,7 +22522,7 @@ static const char needCsvQuote[] = {
*/
static void output_csv(ShellState *p, const char *z, int bSep){
if( z==0 ){
- oputf("%s",p->nullValue);
+ sqlite3_fprintf(p->out, "%s",p->nullValue);
}else{
unsigned i;
for(i=0; z[i]; i++){
@@ -20154,14 +22534,14 @@ static void output_csv(ShellState *p, const char *z, int bSep){
if( i==0 || strstr(z, p->colSeparator)!=0 ){
char *zQuoted = sqlite3_mprintf("\"%w\"", z);
shell_check_oom(zQuoted);
- oputz(zQuoted);
+ sqlite3_fputs(zQuoted, p->out);
sqlite3_free(zQuoted);
}else{
- oputz(z);
+ sqlite3_fputs(z, p->out);
}
}
if( bSep ){
- oputz(p->colSeparator);
+ sqlite3_fputs(p->colSeparator, p->out);
}
}
@@ -20269,16 +22649,16 @@ static int shellAuth(
az[1] = zA2;
az[2] = zA3;
az[3] = zA4;
- oputf("authorizer: %s", azAction[op]);
+ sqlite3_fprintf(p->out, "authorizer: %s", azAction[op]);
for(i=0; i<4; i++){
- oputz(" ");
+ sqlite3_fputs(" ", p->out);
if( az[i] ){
- output_c_string(az[i]);
+ output_c_string(p->out, az[i]);
}else{
- oputz("NULL");
+ sqlite3_fputs("NULL", p->out);
}
}
- oputz("\n");
+ sqlite3_fputs("\n", p->out);
if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4);
return SQLITE_OK;
}
@@ -20294,7 +22674,7 @@ static int shellAuth(
** sqlite3_complete() returns false, try to terminate the comment before
** printing the result. https://sqlite.org/forum/forumpost/d7be961c5c
*/
-static void printSchemaLine(const char *z, const char *zTail){
+static void printSchemaLine(FILE *out, const char *z, const char *zTail){
char *zToFree = 0;
if( z==0 ) return;
if( zTail==0 ) return;
@@ -20316,16 +22696,16 @@ static void printSchemaLine(const char *z, const char *zTail){
}
}
if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
- oputf("CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
+ sqlite3_fprintf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
}else{
- oputf("%s%s", z, zTail);
+ sqlite3_fprintf(out, "%s%s", z, zTail);
}
sqlite3_free(zToFree);
}
-static void printSchemaLineN(char *z, int n, const char *zTail){
+static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
char c = z[n];
z[n] = 0;
- printSchemaLine(z, zTail);
+ printSchemaLine(out, z, zTail);
z[n] = c;
}
@@ -20353,7 +22733,7 @@ static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
if( zText==0 ) return;
nText = strlen(zText);
if( p->autoEQPtest ){
- oputf("%d,%d,%s\n", iEqpId, p2, zText);
+ sqlite3_fprintf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
}
pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
shell_check_oom(pNew);
@@ -20401,7 +22781,8 @@ static void eqp_render_level(ShellState *p, int iEqpId){
for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
pNext = eqp_next_row(p, iEqpId, pRow);
z = pRow->zText;
- oputf("%s%s%s\n", p->sGraph.zPrefix, pNext ? "|--" : "`--", z);
+ sqlite3_fprintf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
+ pNext ? "|--" : "`--", z);
if( n<(i64)sizeof(p->sGraph.zPrefix)-7 ){
memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4);
eqp_render_level(p, pRow->iEqpId);
@@ -20421,13 +22802,13 @@ static void eqp_render(ShellState *p, i64 nCycle){
eqp_reset(p);
return;
}
- oputf("%s\n", pRow->zText+3);
+ sqlite3_fprintf(p->out, "%s\n", pRow->zText+3);
p->sGraph.pRow = pRow->pNext;
sqlite3_free(pRow);
}else if( nCycle>0 ){
- oputf("QUERY PLAN (cycles=%lld [100%%])\n", nCycle);
+ sqlite3_fprintf(p->out, "QUERY PLAN (cycles=%lld [100%%])\n", nCycle);
}else{
- oputz("QUERY PLAN\n");
+ sqlite3_fputs("QUERY PLAN\n", p->out);
}
p->sGraph.zPrefix[0] = 0;
eqp_render_level(p, 0);
@@ -20443,13 +22824,13 @@ static int progress_handler(void *pClientData) {
ShellState *p = (ShellState*)pClientData;
p->nProgress++;
if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
- oputf("Progress limit reached (%u)\n", p->nProgress);
+ sqlite3_fprintf(p->out, "Progress limit reached (%u)\n", p->nProgress);
if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
return 1;
}
if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
- oputf("Progress %u\n", p->nProgress);
+ sqlite3_fprintf(p->out, "Progress %u\n", p->nProgress);
}
return 0;
}
@@ -20458,14 +22839,14 @@ static int progress_handler(void *pClientData) {
/*
** Print N dashes
*/
-static void print_dashes(int N){
+static void print_dashes(FILE *out, int N){
const char zDash[] = "--------------------------------------------------";
const int nDash = sizeof(zDash) - 1;
while( N>nDash ){
- oputz(zDash);
+ sqlite3_fputs(zDash, out);
N -= nDash;
}
- oputf("%.*s", N, zDash);
+ sqlite3_fprintf(out, "%.*s", N, zDash);
}
/*
@@ -20478,15 +22859,15 @@ static void print_row_separator(
){
int i;
if( nArg>0 ){
- oputz(zSep);
- print_dashes(p->actualWidth[0]+2);
+ sqlite3_fputs(zSep, p->out);
+ print_dashes(p->out, p->actualWidth[0]+2);
for(i=1; i<nArg; i++){
- oputz(zSep);
- print_dashes(p->actualWidth[i]+2);
+ sqlite3_fputs(zSep, p->out);
+ print_dashes(p->out, p->actualWidth[i]+2);
}
- oputz(zSep);
+ sqlite3_fputs(zSep, p->out);
}
- oputz("\n");
+ sqlite3_fputs("\n", p->out);
}
/*
@@ -20516,10 +22897,14 @@ static int shell_callback(
int len = strlen30(azCol[i] ? azCol[i] : "");
if( len>w ) w = len;
}
- if( p->cnt++>0 ) oputz(p->rowSeparator);
+ if( p->cnt++>0 ) sqlite3_fputs(p->rowSeparator, p->out);
for(i=0; i<nArg; i++){
- oputf("%*s = %s%s", w, azCol[i],
- azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
+ char *pFree = 0;
+ const char *pDisplay;
+ pDisplay = escapeOutput(p, azArg[i] ? azArg[i] : p->nullValue, &pFree);
+ sqlite3_fprintf(p->out, "%*s = %s%s", w, azCol[i],
+ pDisplay, p->rowSeparator);
+ if( pFree ) sqlite3_free(pFree);
}
break;
}
@@ -20527,7 +22912,7 @@ static int shell_callback(
case MODE_Explain: {
static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
static const int aExplainMap[] = {0, 1, 2, 3, 4, 5, 6, 7 };
- static const int aScanExpWidth[] = {4, 6, 6, 13, 4, 4, 4, 13, 2, 13};
+ static const int aScanExpWidth[] = {4, 15, 6, 13, 4, 4, 4, 13, 2, 13};
static const int aScanExpMap[] = {0, 9, 8, 1, 2, 3, 4, 5, 6, 7 };
const int *aWidth = aExplainWidth;
@@ -20546,12 +22931,12 @@ static int shell_callback(
/* If this is the first row seen, print out the headers */
if( p->cnt++==0 ){
for(i=0; i<nArg; i++){
- utf8_width_print(aWidth[i], azCol[ aMap[i] ]);
- oputz(i==nArg-1 ? "\n" : " ");
+ utf8_width_print(p->out, aWidth[i], azCol[ aMap[i] ]);
+ sqlite3_fputs(i==nArg-1 ? "\n" : " ", p->out);
}
for(i=0; i<nArg; i++){
- print_dashes(aWidth[i]);
- oputz(i==nArg-1 ? "\n" : " ");
+ print_dashes(p->out, aWidth[i]);
+ sqlite3_fputs(i==nArg-1 ? "\n" : " ", p->out);
}
}
@@ -20569,17 +22954,17 @@ static int shell_callback(
}
if( i==iIndent && p->aiIndent && p->pStmt ){
if( p->iIndent<p->nIndent ){
- oputf("%*.s", p->aiIndent[p->iIndent], "");
+ sqlite3_fprintf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
}
p->iIndent++;
}
- utf8_width_print(w, zVal ? zVal : p->nullValue);
- oputz(i==nArg-1 ? "\n" : zSep);
+ utf8_width_print(p->out, w, zVal ? zVal : p->nullValue);
+ sqlite3_fputs(i==nArg-1 ? "\n" : zSep, p->out);
}
break;
}
case MODE_Semi: { /* .schema and .fullschema output */
- printSchemaLine(azArg[0], ";\n");
+ printSchemaLine(p->out, azArg[0], ";\n");
break;
}
case MODE_Pretty: { /* .schema and .fullschema with --indent */
@@ -20589,14 +22974,18 @@ static int shell_callback(
char cEnd = 0;
char c;
int nLine = 0;
+ int isIndex;
+ int isWhere = 0;
assert( nArg==1 );
if( azArg[0]==0 ) break;
if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
|| sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
){
- oputf("%s;\n", azArg[0]);
+ sqlite3_fprintf(p->out, "%s;\n", azArg[0]);
break;
}
+ isIndex = sqlite3_strlike("CREATE INDEX%", azArg[0], 0)==0
+ || sqlite3_strlike("CREATE UNIQUE INDEX%", azArg[0], 0)==0;
z = sqlite3_mprintf("%s", azArg[0]);
shell_check_oom(z);
j = 0;
@@ -20626,17 +23015,29 @@ static int shell_callback(
nParen++;
}else if( c==')' ){
nParen--;
- if( nLine>0 && nParen==0 && j>0 ){
- printSchemaLineN(z, j, "\n");
+ if( nLine>0 && nParen==0 && j>0 && !isWhere ){
+ printSchemaLineN(p->out, z, j, "\n");
j = 0;
}
+ }else if( (c=='w' || c=='W')
+ && nParen==0 && isIndex
+ && sqlite3_strnicmp("WHERE",&z[i],5)==0
+ && !IsAlnum(z[i+5]) && z[i+5]!='_' ){
+ isWhere = 1;
+ }else if( isWhere && (c=='A' || c=='a')
+ && nParen==0
+ && sqlite3_strnicmp("AND",&z[i],3)==0
+ && !IsAlnum(z[i+3]) && z[i+3]!='_' ){
+ printSchemaLineN(p->out, z, j, "\n ");
+ j = 0;
}
z[j++] = c;
if( nParen==1 && cEnd==0
&& (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
+ && !isWhere
){
if( c=='\n' ) j--;
- printSchemaLineN(z, j, "\n ");
+ printSchemaLineN(p->out, z, j, "\n ");
j = 0;
nLine++;
while( IsSpace(z[i+1]) ){ i++; }
@@ -20644,118 +23045,136 @@ static int shell_callback(
}
z[j] = 0;
}
- printSchemaLine(z, ";\n");
+ printSchemaLine(p->out, z, ";\n");
sqlite3_free(z);
break;
}
case MODE_List: {
if( p->cnt++==0 && p->showHeader ){
for(i=0; i<nArg; i++){
- oputf("%s%s",azCol[i], i==nArg-1 ? p->rowSeparator : p->colSeparator);
+ char *z = azCol[i];
+ char *pFree;
+ const char *zOut = escapeOutput(p, z, &pFree);
+ sqlite3_fprintf(p->out, "%s%s", zOut,
+ i==nArg-1 ? p->rowSeparator : p->colSeparator);
+ if( pFree ) sqlite3_free(pFree);
}
}
if( azArg==0 ) break;
for(i=0; i<nArg; i++){
char *z = azArg[i];
+ char *pFree;
+ const char *zOut;
if( z==0 ) z = p->nullValue;
- oputz(z);
- oputz((i<nArg-1)? p->colSeparator : p->rowSeparator);
+ zOut = escapeOutput(p, z, &pFree);
+ sqlite3_fputs(zOut, p->out);
+ if( pFree ) sqlite3_free(pFree);
+ sqlite3_fputs((i<nArg-1)? p->colSeparator : p->rowSeparator, p->out);
}
break;
}
+ case MODE_Www:
case MODE_Html: {
- if( p->cnt++==0 && p->showHeader ){
- oputz("<TR>");
+ if( p->cnt==0 && p->cMode==MODE_Www ){
+ sqlite3_fputs(
+ "</PRE>\n"
+ "<TABLE border='1' cellspacing='0' cellpadding='2'>\n"
+ ,p->out
+ );
+ }
+ if( p->cnt==0 && (p->showHeader || p->cMode==MODE_Www) ){
+ sqlite3_fputs("<TR>", p->out);
for(i=0; i<nArg; i++){
- oputz("<TH>");
- output_html_string(azCol[i]);
- oputz("</TH>\n");
+ sqlite3_fputs("<TH>", p->out);
+ output_html_string(p->out, azCol[i]);
+ sqlite3_fputs("</TH>\n", p->out);
}
- oputz("</TR>\n");
+ sqlite3_fputs("</TR>\n", p->out);
}
+ p->cnt++;
if( azArg==0 ) break;
- oputz("<TR>");
+ sqlite3_fputs("<TR>", p->out);
for(i=0; i<nArg; i++){
- oputz("<TD>");
- output_html_string(azArg[i] ? azArg[i] : p->nullValue);
- oputz("</TD>\n");
+ sqlite3_fputs("<TD>", p->out);
+ output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
+ sqlite3_fputs("</TD>\n", p->out);
}
- oputz("</TR>\n");
+ sqlite3_fputs("</TR>\n", p->out);
break;
}
case MODE_Tcl: {
if( p->cnt++==0 && p->showHeader ){
for(i=0; i<nArg; i++){
- output_c_string(azCol[i] ? azCol[i] : "");
- if(i<nArg-1) oputz(p->colSeparator);
+ output_c_string(p->out, azCol[i] ? azCol[i] : "");
+ if(i<nArg-1) sqlite3_fputs(p->colSeparator, p->out);
}
- oputz(p->rowSeparator);
+ sqlite3_fputs(p->rowSeparator, p->out);
}
if( azArg==0 ) break;
for(i=0; i<nArg; i++){
- output_c_string(azArg[i] ? azArg[i] : p->nullValue);
- if(i<nArg-1) oputz(p->colSeparator);
+ output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
+ if(i<nArg-1) sqlite3_fputs(p->colSeparator, p->out);
}
- oputz(p->rowSeparator);
+ sqlite3_fputs(p->rowSeparator, p->out);
break;
}
case MODE_Csv: {
- setBinaryMode(p->out, 1);
+ sqlite3_fsetmode(p->out, _O_BINARY);
if( p->cnt++==0 && p->showHeader ){
for(i=0; i<nArg; i++){
output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
}
- oputz(p->rowSeparator);
+ sqlite3_fputs(p->rowSeparator, p->out);
}
if( nArg>0 ){
for(i=0; i<nArg; i++){
output_csv(p, azArg[i], i<nArg-1);
}
- oputz(p->rowSeparator);
+ sqlite3_fputs(p->rowSeparator, p->out);
}
- setTextMode(p->out, 1);
+ setCrlfMode(p);
break;
}
case MODE_Insert: {
if( azArg==0 ) break;
- oputf("INSERT INTO %s",p->zDestTable);
+ sqlite3_fprintf(p->out, "INSERT INTO %s",p->zDestTable);
if( p->showHeader ){
- oputz("(");
+ sqlite3_fputs("(", p->out);
for(i=0; i<nArg; i++){
- if( i>0 ) oputz(",");
+ if( i>0 ) sqlite3_fputs(",", p->out);
if( quoteChar(azCol[i]) ){
char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
shell_check_oom(z);
- oputz(z);
+ sqlite3_fputs(z, p->out);
sqlite3_free(z);
}else{
- oputf("%s", azCol[i]);
+ sqlite3_fprintf(p->out, "%s", azCol[i]);
}
}
- oputz(")");
+ sqlite3_fputs(")", p->out);
}
p->cnt++;
for(i=0; i<nArg; i++){
- oputz(i>0 ? "," : " VALUES(");
+ sqlite3_fputs(i>0 ? "," : " VALUES(", p->out);
if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
- oputz("NULL");
+ sqlite3_fputs("NULL", p->out);
}else if( aiType && aiType[i]==SQLITE_TEXT ){
if( ShellHasFlag(p, SHFLG_Newlines) ){
- output_quoted_string(azArg[i]);
+ output_quoted_string(p, azArg[i]);
}else{
- output_quoted_escaped_string(azArg[i]);
+ output_quoted_escaped_string(p, azArg[i]);
}
}else if( aiType && aiType[i]==SQLITE_INTEGER ){
- oputz(azArg[i]);
+ sqlite3_fputs(azArg[i], p->out);
}else if( aiType && aiType[i]==SQLITE_FLOAT ){
char z[50];
double r = sqlite3_column_double(p->pStmt, i);
sqlite3_uint64 ur;
memcpy(&ur,&r,sizeof(r));
if( ur==0x7ff0000000000000LL ){
- oputz("9.0e+999");
+ sqlite3_fputs("9.0e+999", p->out);
}else if( ur==0xfff0000000000000LL ){
- oputz("-9.0e+999");
+ sqlite3_fputs("-9.0e+999", p->out);
}else{
sqlite3_int64 ir = (sqlite3_int64)r;
if( r==(double)ir ){
@@ -20763,115 +23182,115 @@ static int shell_callback(
}else{
sqlite3_snprintf(50,z,"%!.20g", r);
}
- oputz(z);
+ sqlite3_fputs(z, p->out);
}
}else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
const void *pBlob = sqlite3_column_blob(p->pStmt, i);
int nBlob = sqlite3_column_bytes(p->pStmt, i);
- output_hex_blob(pBlob, nBlob);
+ output_hex_blob(p->out, pBlob, nBlob);
}else if( isNumber(azArg[i], 0) ){
- oputz(azArg[i]);
+ sqlite3_fputs(azArg[i], p->out);
}else if( ShellHasFlag(p, SHFLG_Newlines) ){
- output_quoted_string(azArg[i]);
+ output_quoted_string(p, azArg[i]);
}else{
- output_quoted_escaped_string(azArg[i]);
+ output_quoted_escaped_string(p, azArg[i]);
}
}
- oputz(");\n");
+ sqlite3_fputs(");\n", p->out);
break;
}
case MODE_Json: {
if( azArg==0 ) break;
if( p->cnt==0 ){
- fputs("[{", p->out);
+ sqlite3_fputs("[{", p->out);
}else{
- fputs(",\n{", p->out);
+ sqlite3_fputs(",\n{", p->out);
}
p->cnt++;
for(i=0; i<nArg; i++){
- output_json_string(azCol[i], -1);
- oputz(":");
+ output_json_string(p->out, azCol[i], -1);
+ sqlite3_fputs(":", p->out);
if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
- oputz("null");
+ sqlite3_fputs("null", p->out);
}else if( aiType && aiType[i]==SQLITE_FLOAT ){
char z[50];
double r = sqlite3_column_double(p->pStmt, i);
sqlite3_uint64 ur;
memcpy(&ur,&r,sizeof(r));
if( ur==0x7ff0000000000000LL ){
- oputz("9.0e+999");
+ sqlite3_fputs("9.0e+999", p->out);
}else if( ur==0xfff0000000000000LL ){
- oputz("-9.0e+999");
+ sqlite3_fputs("-9.0e+999", p->out);
}else{
sqlite3_snprintf(50,z,"%!.20g", r);
- oputz(z);
+ sqlite3_fputs(z, p->out);
}
}else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
const void *pBlob = sqlite3_column_blob(p->pStmt, i);
int nBlob = sqlite3_column_bytes(p->pStmt, i);
- output_json_string(pBlob, nBlob);
+ output_json_string(p->out, pBlob, nBlob);
}else if( aiType && aiType[i]==SQLITE_TEXT ){
- output_json_string(azArg[i], -1);
+ output_json_string(p->out, azArg[i], -1);
}else{
- oputz(azArg[i]);
+ sqlite3_fputs(azArg[i], p->out);
}
if( i<nArg-1 ){
- oputz(",");
+ sqlite3_fputs(",", p->out);
}
}
- oputz("}");
+ sqlite3_fputs("}", p->out);
break;
}
case MODE_Quote: {
if( azArg==0 ) break;
if( p->cnt==0 && p->showHeader ){
for(i=0; i<nArg; i++){
- if( i>0 ) fputs(p->colSeparator, p->out);
- output_quoted_string(azCol[i]);
+ if( i>0 ) sqlite3_fputs(p->colSeparator, p->out);
+ output_quoted_string(p, azCol[i]);
}
- fputs(p->rowSeparator, p->out);
+ sqlite3_fputs(p->rowSeparator, p->out);
}
p->cnt++;
for(i=0; i<nArg; i++){
- if( i>0 ) fputs(p->colSeparator, p->out);
+ if( i>0 ) sqlite3_fputs(p->colSeparator, p->out);
if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
- oputz("NULL");
+ sqlite3_fputs("NULL", p->out);
}else if( aiType && aiType[i]==SQLITE_TEXT ){
- output_quoted_string(azArg[i]);
+ output_quoted_string(p, azArg[i]);
}else if( aiType && aiType[i]==SQLITE_INTEGER ){
- oputz(azArg[i]);
+ sqlite3_fputs(azArg[i], p->out);
}else if( aiType && aiType[i]==SQLITE_FLOAT ){
char z[50];
double r = sqlite3_column_double(p->pStmt, i);
sqlite3_snprintf(50,z,"%!.20g", r);
- oputz(z);
+ sqlite3_fputs(z, p->out);
}else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
const void *pBlob = sqlite3_column_blob(p->pStmt, i);
int nBlob = sqlite3_column_bytes(p->pStmt, i);
- output_hex_blob(pBlob, nBlob);
+ output_hex_blob(p->out, pBlob, nBlob);
}else if( isNumber(azArg[i], 0) ){
- oputz(azArg[i]);
+ sqlite3_fputs(azArg[i], p->out);
}else{
- output_quoted_string(azArg[i]);
+ output_quoted_string(p, azArg[i]);
}
}
- fputs(p->rowSeparator, p->out);
+ sqlite3_fputs(p->rowSeparator, p->out);
break;
}
case MODE_Ascii: {
if( p->cnt++==0 && p->showHeader ){
for(i=0; i<nArg; i++){
- if( i>0 ) oputz(p->colSeparator);
- oputz(azCol[i] ? azCol[i] : "");
+ if( i>0 ) sqlite3_fputs(p->colSeparator, p->out);
+ sqlite3_fputs(azCol[i] ? azCol[i] : "", p->out);
}
- oputz(p->rowSeparator);
+ sqlite3_fputs(p->rowSeparator, p->out);
}
if( azArg==0 ) break;
for(i=0; i<nArg; i++){
- if( i>0 ) oputz(p->colSeparator);
- oputz(azArg[i] ? azArg[i] : p->nullValue);
+ if( i>0 ) sqlite3_fputs(p->colSeparator, p->out);
+ sqlite3_fputs(azArg[i] ? azArg[i] : p->nullValue, p->out);
}
- oputz(p->rowSeparator);
+ sqlite3_fputs(p->rowSeparator, p->out);
break;
}
case MODE_EQP: {
@@ -20950,7 +23369,7 @@ static void createSelftestTable(ShellState *p){
"DROP TABLE [_shell$self];"
,0,0,&zErrMsg);
if( zErrMsg ){
- eputf("SELFTEST initialization failure: %s\n", zErrMsg);
+ sqlite3_fprintf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
@@ -21053,7 +23472,7 @@ static int run_table_dump_query(
rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
if( rc!=SQLITE_OK || !pSelect ){
char *zContext = shell_error_context(zSelect, p->db);
- oputf("/**** ERROR: (%d) %s *****/\n%s",
+ sqlite3_fprintf(p->out, "/**** ERROR: (%d) %s *****/\n%s",
rc, sqlite3_errmsg(p->db), zContext);
sqlite3_free(zContext);
if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
@@ -21063,22 +23482,23 @@ static int run_table_dump_query(
nResult = sqlite3_column_count(pSelect);
while( rc==SQLITE_ROW ){
z = (const char*)sqlite3_column_text(pSelect, 0);
- oputf("%s", z);
+ sqlite3_fprintf(p->out, "%s", z);
for(i=1; i<nResult; i++){
- oputf(",%s", sqlite3_column_text(pSelect, i));
+ sqlite3_fprintf(p->out, ",%s", sqlite3_column_text(pSelect, i));
}
if( z==0 ) z = "";
while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
if( z[0] ){
- oputz("\n;\n");
+ sqlite3_fputs("\n;\n", p->out);
}else{
- oputz(";\n");
+ sqlite3_fputs(";\n", p->out);
}
rc = sqlite3_step(pSelect);
}
rc = sqlite3_finalize(pSelect);
if( rc!=SQLITE_OK ){
- oputf("/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db));
+ sqlite3_fprintf(p->out, "/**** ERROR: (%d) %s *****/\n",
+ rc, sqlite3_errmsg(p->db));
if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
}
return rc;
@@ -21114,13 +23534,13 @@ static char *save_err_msg(
/*
** Attempt to display I/O stats on Linux using /proc/PID/io
*/
-static void displayLinuxIoStats(void){
+static void displayLinuxIoStats(FILE *out){
FILE *in;
char z[200];
sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
- in = fopen(z, "rb");
+ in = sqlite3_fopen(z, "rb");
if( in==0 ) return;
- while( fgets(z, sizeof(z), in)!=0 ){
+ while( sqlite3_fgets(z, sizeof(z), in)!=0 ){
static const struct {
const char *zPattern;
const char *zDesc;
@@ -21137,7 +23557,7 @@ static void displayLinuxIoStats(void){
for(i=0; i<ArraySize(aTrans); i++){
int n = strlen30(aTrans[i].zPattern);
if( cli_strncmp(aTrans[i].zPattern, z, n)==0 ){
- oputf("%-36s %s", aTrans[i].zDesc, &z[n]);
+ sqlite3_fprintf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
break;
}
}
@@ -21150,6 +23570,7 @@ static void displayLinuxIoStats(void){
** Display a single line of status using 64-bit values.
*/
static void displayStatLine(
+ FILE *out, /* Write to this channel */
char *zLabel, /* Label for this one line */
char *zFormat, /* Format for the result */
int iStatusCtrl, /* Which status to display */
@@ -21168,7 +23589,7 @@ static void displayStatLine(
}else{
sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
}
- oputf("%-36s %s\n", zLabel, zLine);
+ sqlite3_fprintf(out, "%-36s %s\n", zLabel, zLine);
}
/*
@@ -21181,28 +23602,31 @@ static int display_stats(
){
int iCur;
int iHiwtr;
+ FILE *out;
if( pArg==0 || pArg->out==0 ) return 0;
+ out = pArg->out;
if( pArg->pStmt && pArg->statsOn==2 ){
int nCol, i, x;
sqlite3_stmt *pStmt = pArg->pStmt;
char z[100];
nCol = sqlite3_column_count(pStmt);
- oputf("%-36s %d\n", "Number of output columns:", nCol);
+ sqlite3_fprintf(out, "%-36s %d\n", "Number of output columns:", nCol);
for(i=0; i<nCol; i++){
sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
- oputf("%-36s %s\n", z, sqlite3_column_name(pStmt,i));
+ sqlite3_fprintf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
#ifndef SQLITE_OMIT_DECLTYPE
sqlite3_snprintf(30, z+x, "declared type:");
- oputf("%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
+ sqlite3_fprintf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
#endif
#ifdef SQLITE_ENABLE_COLUMN_METADATA
sqlite3_snprintf(30, z+x, "database name:");
- oputf("%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
+ sqlite3_fprintf(out, "%-36s %s\n", z,
+ sqlite3_column_database_name(pStmt,i));
sqlite3_snprintf(30, z+x, "table name:");
- oputf("%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
+ sqlite3_fprintf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
sqlite3_snprintf(30, z+x, "origin name:");
- oputf("%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
+ sqlite3_fprintf(out, "%-36s %s\n", z,sqlite3_column_origin_name(pStmt,i));
#endif
}
}
@@ -21210,27 +23634,27 @@ static int display_stats(
if( pArg->statsOn==3 ){
if( pArg->pStmt ){
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP,bReset);
- oputf("VM-steps: %d\n", iCur);
+ sqlite3_fprintf(out, "VM-steps: %d\n", iCur);
}
return 0;
}
- displayStatLine("Memory Used:",
+ displayStatLine(out, "Memory Used:",
"%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
- displayStatLine("Number of Outstanding Allocations:",
+ displayStatLine(out, "Number of Outstanding Allocations:",
"%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
if( pArg->shellFlgs & SHFLG_Pagecache ){
- displayStatLine("Number of Pcache Pages Used:",
+ displayStatLine(out, "Number of Pcache Pages Used:",
"%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
}
- displayStatLine("Number of Pcache Overflow Bytes:",
+ displayStatLine(out, "Number of Pcache Overflow Bytes:",
"%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
- displayStatLine("Largest Allocation:",
+ displayStatLine(out, "Largest Allocation:",
"%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
- displayStatLine("Largest Pcache Allocation:",
+ displayStatLine(out, "Largest Pcache Allocation:",
"%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
#ifdef YYTRACKMAXSTACKDEPTH
- displayStatLine("Deepest Parser Stack:",
+ displayStatLine(out, "Deepest Parser Stack:",
"%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
#endif
@@ -21239,68 +23663,87 @@ static int display_stats(
iHiwtr = iCur = -1;
sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
&iCur, &iHiwtr, bReset);
- oputf("Lookaside Slots Used: %d (max %d)\n", iCur, iHiwtr);
+ sqlite3_fprintf(out,
+ "Lookaside Slots Used: %d (max %d)\n", iCur, iHiwtr);
sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
&iCur, &iHiwtr, bReset);
- oputf("Successful lookaside attempts: %d\n", iHiwtr);
+ sqlite3_fprintf(out,
+ "Successful lookaside attempts: %d\n", iHiwtr);
sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
&iCur, &iHiwtr, bReset);
- oputf("Lookaside failures due to size: %d\n", iHiwtr);
+ sqlite3_fprintf(out,
+ "Lookaside failures due to size: %d\n", iHiwtr);
sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
&iCur, &iHiwtr, bReset);
- oputf("Lookaside failures due to OOM: %d\n", iHiwtr);
+ sqlite3_fprintf(out,
+ "Lookaside failures due to OOM: %d\n", iHiwtr);
}
iHiwtr = iCur = -1;
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
- oputf("Pager Heap Usage: %d bytes\n", iCur);
+ sqlite3_fprintf(out,
+ "Pager Heap Usage: %d bytes\n", iCur);
iHiwtr = iCur = -1;
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
- oputf("Page cache hits: %d\n", iCur);
+ sqlite3_fprintf(out,
+ "Page cache hits: %d\n", iCur);
iHiwtr = iCur = -1;
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
- oputf("Page cache misses: %d\n", iCur);
+ sqlite3_fprintf(out,
+ "Page cache misses: %d\n", iCur);
iHiwtr = iCur = -1;
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
- oputf("Page cache writes: %d\n", iCur);
+ sqlite3_fprintf(out,
+ "Page cache writes: %d\n", iCur);
iHiwtr = iCur = -1;
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
- oputf("Page cache spills: %d\n", iCur);
+ sqlite3_fprintf(out,
+ "Page cache spills: %d\n", iCur);
iHiwtr = iCur = -1;
sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
- oputf("Schema Heap Usage: %d bytes\n", iCur);
+ sqlite3_fprintf(out,
+ "Schema Heap Usage: %d bytes\n", iCur);
iHiwtr = iCur = -1;
sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
- oputf("Statement Heap/Lookaside Usage: %d bytes\n", iCur);
+ sqlite3_fprintf(out,
+ "Statement Heap/Lookaside Usage: %d bytes\n", iCur);
}
if( pArg->pStmt ){
int iHit, iMiss;
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
bReset);
- oputf("Fullscan Steps: %d\n", iCur);
+ sqlite3_fprintf(out,
+ "Fullscan Steps: %d\n", iCur);
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
- oputf("Sort Operations: %d\n", iCur);
+ sqlite3_fprintf(out,
+ "Sort Operations: %d\n", iCur);
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
- oputf("Autoindex Inserts: %d\n", iCur);
+ sqlite3_fprintf(out,
+ "Autoindex Inserts: %d\n", iCur);
iHit = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_HIT,
bReset);
iMiss = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_MISS,
bReset);
if( iHit || iMiss ){
- oputf("Bloom filter bypass taken: %d/%d\n", iHit, iHit+iMiss);
+ sqlite3_fprintf(out,
+ "Bloom filter bypass taken: %d/%d\n", iHit, iHit+iMiss);
}
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
- oputf("Virtual Machine Steps: %d\n", iCur);
+ sqlite3_fprintf(out,
+ "Virtual Machine Steps: %d\n", iCur);
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
- oputf("Reprepare operations: %d\n", iCur);
+ sqlite3_fprintf(out,
+ "Reprepare operations: %d\n", iCur);
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
- oputf("Number of times run: %d\n", iCur);
+ sqlite3_fprintf(out,
+ "Number of times run: %d\n", iCur);
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
- oputf("Memory used by prepared stmt: %d\n", iCur);
+ sqlite3_fprintf(out,
+ "Memory used by prepared stmt: %d\n", iCur);
}
#ifdef __linux__
- displayLinuxIoStats();
+ displayLinuxIoStats(pArg->out);
#endif
/* Do not remove this machine readable comment: extra-stats-output-here */
@@ -21537,7 +23980,13 @@ static void display_scanstats(
if( pArg->scanstatsOn==3 ){
const char *zSql =
" SELECT addr, opcode, p1, p2, p3, p4, p5, comment, nexec,"
- " round(ncycle*100.0 / (sum(ncycle) OVER ()), 2)||'%' AS cycles"
+ " format('% 6s (%.2f%%)',"
+ " CASE WHEN ncycle<100_000 THEN ncycle || ' '"
+ " WHEN ncycle<100_000_000 THEN (ncycle/1_000) || 'K'"
+ " WHEN ncycle<100_000_000_000 THEN (ncycle/1_000_000) || 'M'"
+ " ELSE (ncycle/1000_000_000) || 'G' END,"
+ " ncycle*100.0/(sum(ncycle) OVER ())"
+ " ) AS cycles"
" FROM bytecode(?)";
int rc = SQLITE_OK;
@@ -21645,6 +24094,15 @@ static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
}else if( sqlite3_strlike("_INF", zVar, 0)==0 ){
sqlite3_bind_double(pStmt, i, INFINITY);
#endif
+ }else if( strncmp(zVar, "$int_", 5)==0 ){
+ sqlite3_bind_int(pStmt, i, atoi(&zVar[5]));
+ }else if( strncmp(zVar, "$text_", 6)==0 ){
+ size_t szVar = strlen(zVar);
+ char *zBuf = sqlite3_malloc64( szVar-5 );
+ if( zBuf ){
+ memcpy(zBuf, &zVar[6], szVar-5);
+ sqlite3_bind_text64(pStmt, i, zBuf, szVar-6, sqlite3_free, SQLITE_UTF8);
+ }
}else{
sqlite3_bind_null(pStmt, i);
}
@@ -21681,17 +24139,17 @@ static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
/* Draw horizontal line N characters long using unicode box
** characters
*/
-static void print_box_line(int N){
+static void print_box_line(FILE *out, int N){
const char zDash[] =
BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
const int nDash = sizeof(zDash) - 1;
N *= 3;
while( N>nDash ){
- oputz(zDash);
+ sqlite3_fputs(zDash, out);
N -= nDash;
}
- oputf("%.*s", N, zDash);
+ sqlite3_fprintf(out, "%.*s", N, zDash);
}
/*
@@ -21706,15 +24164,15 @@ static void print_box_row_separator(
){
int i;
if( nArg>0 ){
- oputz(zSep1);
- print_box_line(p->actualWidth[0]+2);
+ sqlite3_fputs(zSep1, p->out);
+ print_box_line(p->out, p->actualWidth[0]+2);
for(i=1; i<nArg; i++){
- oputz(zSep2);
- print_box_line(p->actualWidth[i]+2);
+ sqlite3_fputs(zSep2, p->out);
+ print_box_line(p->out, p->actualWidth[i]+2);
}
- oputz(zSep3);
+ sqlite3_fputs(zSep3, p->out);
}
- oputz("\n");
+ sqlite3_fputs("\n", p->out);
}
/*
@@ -21729,6 +24187,7 @@ static void print_box_row_separator(
** the last line, write a NULL into *pzTail. (*pzTail is not allocated.)
*/
static char *translateForDisplayAndDup(
+ ShellState *p, /* To access current settings */
const unsigned char *z, /* Input text to be transformed */
const unsigned char **pzTail, /* OUT: Tail of the input for next line */
int mxWidth, /* Max width. 0 means no limit */
@@ -21748,12 +24207,23 @@ static char *translateForDisplayAndDup(
if( mxWidth==0 ) mxWidth = 1000000;
i = j = n = 0;
while( n<mxWidth ){
- if( z[i]>=' ' ){
+ unsigned char c = z[i];
+ if( c>=0xc0 ){
+ int u;
+ int len = decodeUtf8(&z[i], &u);
+ i += len;
+ j += len;
+ n += cli_wcwidth(u);
+ continue;
+ }
+ if( c>=' ' ){
n++;
- do{ i++; j++; }while( (z[i]&0xc0)==0x80 );
+ i++;
+ j++;
continue;
}
- if( z[i]=='\t' ){
+ if( c==0 || c=='\n' || (c=='\r' && z[i+1]=='\n') ) break;
+ if( c=='\t' ){
do{
n++;
j++;
@@ -21761,16 +24231,23 @@ static char *translateForDisplayAndDup(
i++;
continue;
}
- break;
+ if( c==0x1b && p->eEscMode==SHELL_ESC_OFF && (k = isVt100(&z[i]))>0 ){
+ i += k;
+ j += k;
+ }else{
+ n++;
+ j += 3;
+ i++;
+ }
}
if( n>=mxWidth && bWordWrap ){
/* Perhaps try to back up to a better place to break the line */
for(k=i; k>i/2; k--){
- if( isspace(z[k-1]) ) break;
+ if( IsSpace(z[k-1]) ) break;
}
if( k<=i/2 ){
for(k=i; k>i/2; k--){
- if( isalnum(z[k-1])!=isalnum(z[k]) && (z[k]&0xc0)!=0x80 ) break;
+ if( IsAlnum(z[k-1])!=IsAlnum(z[k]) && (z[k]&0xc0)!=0x80 ) break;
}
}
if( k<=i/2 ){
@@ -21795,11 +24272,20 @@ static char *translateForDisplayAndDup(
shell_check_oom(zOut);
i = j = n = 0;
while( i<k ){
- if( z[i]>=' ' ){
+ unsigned char c = z[i];
+ if( c>=0xc0 ){
+ int u;
+ int len = decodeUtf8(&z[i], &u);
+ do{ zOut[j++] = z[i++]; }while( (--len)>0 );
+ n += cli_wcwidth(u);
+ continue;
+ }
+ if( c>=' ' ){
n++;
- do{ zOut[j++] = z[i++]; }while( (z[i]&0xc0)==0x80 );
+ zOut[j++] = z[i++];
continue;
}
+ if( c==0 ) break;
if( z[i]=='\t' ){
do{
n++;
@@ -21808,12 +24294,44 @@ static char *translateForDisplayAndDup(
i++;
continue;
}
- break;
+ switch( p->eEscMode ){
+ case SHELL_ESC_SYMBOL:
+ zOut[j++] = 0xe2;
+ zOut[j++] = 0x90;
+ zOut[j++] = 0x80 + c;
+ break;
+ case SHELL_ESC_ASCII:
+ zOut[j++] = '^';
+ zOut[j++] = 0x40 + c;
+ break;
+ case SHELL_ESC_OFF: {
+ int nn;
+ if( c==0x1b && (nn = isVt100(&z[i]))>0 ){
+ memcpy(&zOut[j], &z[i], nn);
+ j += nn;
+ i += nn - 1;
+ }else{
+ zOut[j++] = c;
+ }
+ break;
+ }
+ }
+ i++;
}
zOut[j] = 0;
return (char*)zOut;
}
+/* Return true if the text string z[] contains characters that need
+** unistr() escaping.
+*/
+static int needUnistr(const unsigned char *z){
+ unsigned char c;
+ if( z==0 ) return 0;
+ while( (c = *z)>0x1f || c=='\t' || c=='\n' || (c=='\r' && z[1]=='\n') ){ z++; }
+ return c!=0;
+}
+
/* Extract the value of the i-th current column for pStmt as an SQL literal
** value. Memory is obtained from sqlite3_malloc64() and must be freed by
** the caller.
@@ -21828,7 +24346,8 @@ static char *quoted_column(sqlite3_stmt *pStmt, int i){
return sqlite3_mprintf("%s",sqlite3_column_text(pStmt,i));
}
case SQLITE_TEXT: {
- return sqlite3_mprintf("%Q",sqlite3_column_text(pStmt,i));
+ const unsigned char *zText = sqlite3_column_text(pStmt,i);
+ return sqlite3_mprintf(needUnistr(zText)?"%#Q":"%Q",zText);
}
case SQLITE_BLOB: {
int j;
@@ -21920,7 +24439,7 @@ static void exec_prepared_stmt_columnar(
if( wx<0 ) wx = -wx;
uz = (const unsigned char*)sqlite3_column_name(pStmt,i);
if( uz==0 ) uz = (u8*)"";
- azData[i] = translateForDisplayAndDup(uz, &zNotUsed, wx, bw);
+ azData[i] = translateForDisplayAndDup(p, uz, &zNotUsed, wx, bw);
}
do{
int useNextLine = bNextLine;
@@ -21944,6 +24463,7 @@ static void exec_prepared_stmt_columnar(
uz = azNextLine[i];
if( uz==0 ) uz = (u8*)zEmpty;
}else if( p->cmOpts.bQuote ){
+ assert( azQuoted!=0 );
sqlite3_free(azQuoted[i]);
azQuoted[i] = quoted_column(pStmt,i);
uz = (const unsigned char*)azQuoted[i];
@@ -21952,7 +24472,7 @@ static void exec_prepared_stmt_columnar(
if( uz==0 ) uz = (u8*)zShowNull;
}
azData[nRow*nColumn + i]
- = translateForDisplayAndDup(uz, &azNextLine[i], wx, bw);
+ = translateForDisplayAndDup(p, uz, &azNextLine[i], wx, bw);
if( azNextLine[i] ){
bNextLine = 1;
abRowDiv[nRow-1] = 0;
@@ -21977,12 +24497,12 @@ static void exec_prepared_stmt_columnar(
for(i=0; i<nColumn; i++){
w = p->actualWidth[i];
if( p->colWidth[i]<0 ) w = -w;
- utf8_width_print(w, azData[i]);
- fputs(i==nColumn-1?"\n":" ", p->out);
+ utf8_width_print(p->out, w, azData[i]);
+ sqlite3_fputs(i==nColumn-1?"\n":" ", p->out);
}
for(i=0; i<nColumn; i++){
- print_dashes(p->actualWidth[i]);
- fputs(i==nColumn-1?"\n":" ", p->out);
+ print_dashes(p->out, p->actualWidth[i]);
+ sqlite3_fputs(i==nColumn-1?"\n":" ", p->out);
}
}
break;
@@ -21991,12 +24511,13 @@ static void exec_prepared_stmt_columnar(
colSep = " | ";
rowSep = " |\n";
print_row_separator(p, nColumn, "+");
- fputs("| ", p->out);
+ sqlite3_fputs("| ", p->out);
for(i=0; i<nColumn; i++){
w = p->actualWidth[i];
n = strlenChar(azData[i]);
- oputf("%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
- oputz(i==nColumn-1?" |\n":" | ");
+ sqlite3_fprintf(p->out, "%*s%s%*s", (w-n)/2, "",
+ azData[i], (w-n+1)/2, "");
+ sqlite3_fputs(i==nColumn-1?" |\n":" | ", p->out);
}
print_row_separator(p, nColumn, "+");
break;
@@ -22004,12 +24525,13 @@ static void exec_prepared_stmt_columnar(
case MODE_Markdown: {
colSep = " | ";
rowSep = " |\n";
- fputs("| ", p->out);
+ sqlite3_fputs("| ", p->out);
for(i=0; i<nColumn; i++){
w = p->actualWidth[i];
n = strlenChar(azData[i]);
- oputf("%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
- oputz(i==nColumn-1?" |\n":" | ");
+ sqlite3_fprintf(p->out, "%*s%s%*s", (w-n)/2, "",
+ azData[i], (w-n+1)/2, "");
+ sqlite3_fputs(i==nColumn-1?" |\n":" | ", p->out);
}
print_row_separator(p, nColumn, "|");
break;
@@ -22018,11 +24540,11 @@ static void exec_prepared_stmt_columnar(
colSep = " " BOX_13 " ";
rowSep = " " BOX_13 "\n";
print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
- oputz(BOX_13 " ");
+ sqlite3_fputs(BOX_13 " ", p->out);
for(i=0; i<nColumn; i++){
w = p->actualWidth[i];
n = strlenChar(azData[i]);
- oputf("%*s%s%*s%s",
+ sqlite3_fprintf(p->out, "%*s%s%*s%s",
(w-n)/2, "", azData[i], (w-n+1)/2, "",
i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
}
@@ -22032,28 +24554,28 @@ static void exec_prepared_stmt_columnar(
}
for(i=nColumn, j=0; i<nTotal; i++, j++){
if( j==0 && p->cMode!=MODE_Column ){
- oputz(p->cMode==MODE_Box?BOX_13" ":"| ");
+ sqlite3_fputs(p->cMode==MODE_Box?BOX_13" ":"| ", p->out);
}
z = azData[i];
if( z==0 ) z = p->nullValue;
w = p->actualWidth[j];
if( p->colWidth[j]<0 ) w = -w;
- utf8_width_print(w, z);
+ utf8_width_print(p->out, w, z);
if( j==nColumn-1 ){
- oputz(rowSep);
+ sqlite3_fputs(rowSep, p->out);
if( bMultiLineRowExists && abRowDiv[i/nColumn-1] && i+1<nTotal ){
if( p->cMode==MODE_Table ){
print_row_separator(p, nColumn, "+");
}else if( p->cMode==MODE_Box ){
print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
}else if( p->cMode==MODE_Column ){
- oputz("\n");
+ sqlite3_fputs("\n", p->out);
}
}
j = -1;
if( seenInterrupt ) goto columnar_end;
}else{
- oputz(colSep);
+ sqlite3_fputs(colSep, p->out);
}
}
if( p->cMode==MODE_Table ){
@@ -22063,7 +24585,7 @@ static void exec_prepared_stmt_columnar(
}
columnar_end:
if( seenInterrupt ){
- oputz("Interrupt\n");
+ sqlite3_fputs("Interrupt\n", p->out);
}
nData = (nRow+1)*nColumn;
for(i=0; i<nData; i++){
@@ -22150,7 +24672,9 @@ static void exec_prepared_stmt(
} while( SQLITE_ROW == rc );
sqlite3_free(pData);
if( pArg->cMode==MODE_Json ){
- fputs("]\n", pArg->out);
+ sqlite3_fputs("]\n", pArg->out);
+ }else if( pArg->cMode==MODE_Www ){
+ sqlite3_fputs("</TABLE>\n<PRE>\n", pArg->out);
}else if( pArg->cMode==MODE_Count ){
char zBuf[200];
sqlite3_snprintf(sizeof(zBuf), zBuf, "%llu row%s\n",
@@ -22199,6 +24723,7 @@ static int expertFinish(
){
int rc = SQLITE_OK;
sqlite3expert *p = pState->expert.pExpert;
+ FILE *out = pState->out;
assert( p );
assert( bCancel || pzErr==0 || *pzErr==0 );
if( bCancel==0 ){
@@ -22211,8 +24736,8 @@ static int expertFinish(
if( bVerbose ){
const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
- oputz("-- Candidates -----------------------------\n");
- oputf("%s\n", zCand);
+ sqlite3_fputs("-- Candidates -----------------------------\n", out);
+ sqlite3_fprintf(out, "%s\n", zCand);
}
for(i=0; i<nQuery; i++){
const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
@@ -22220,11 +24745,12 @@ static int expertFinish(
const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
if( zIdx==0 ) zIdx = "(no new indexes)\n";
if( bVerbose ){
- oputf("-- Query %d --------------------------------\n",i+1);
- oputf("%s\n\n", zSql);
+ sqlite3_fprintf(out,
+ "-- Query %d --------------------------------\n"
+ "%s\n\n"
+ ,i+1, zSql);
}
- oputf("%s\n", zIdx);
- oputf("%s\n", zEQP);
+ sqlite3_fprintf(out, "%s\n%s\n", zIdx, zEQP);
}
}
}
@@ -22259,18 +24785,18 @@ static int expertDotCommand(
}
else if( n>=2 && 0==cli_strncmp(z, "-sample", n) ){
if( i==(nArg-1) ){
- eputf("option requires an argument: %s\n", z);
+ sqlite3_fprintf(stderr, "option requires an argument: %s\n", z);
rc = SQLITE_ERROR;
}else{
iSample = (int)integerValue(azArg[++i]);
if( iSample<0 || iSample>100 ){
- eputf("value out of range: %s\n", azArg[i]);
+ sqlite3_fprintf(stderr,"value out of range: %s\n", azArg[i]);
rc = SQLITE_ERROR;
}
}
}
else{
- eputf("unknown option: %s\n", z);
+ sqlite3_fprintf(stderr,"unknown option: %s\n", z);
rc = SQLITE_ERROR;
}
}
@@ -22278,7 +24804,8 @@ static int expertDotCommand(
if( rc==SQLITE_OK ){
pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
if( pState->expert.pExpert==0 ){
- eputf("sqlite3_expert_new: %s\n", zErr ? zErr : "out of memory");
+ sqlite3_fprintf(stderr,
+ "sqlite3_expert_new: %s\n", zErr ? zErr : "out of memory");
rc = SQLITE_ERROR;
}else{
sqlite3_expert_config(
@@ -22360,6 +24887,7 @@ static int shell_exec(
sqlite3_reset(pExplain);
rc = sqlite3_stmt_explain(pExplain, 2);
if( rc==SQLITE_OK ){
+ bind_prepared_stmt(pArg, pExplain);
while( sqlite3_step(pExplain)==SQLITE_ROW ){
const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
int iEqpId = sqlite3_column_int(pExplain, 0);
@@ -22377,6 +24905,7 @@ static int shell_exec(
if( rc==SQLITE_OK ){
pArg->cMode = MODE_Explain;
assert( sqlite3_stmt_isexplain(pExplain)==1 );
+ bind_prepared_stmt(pArg, pExplain);
explain_data_prepare(pArg, pExplain);
exec_prepared_stmt(pArg, pExplain);
explain_data_delete(pArg);
@@ -22605,9 +25134,9 @@ static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
noSys = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
if( cli_strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
- if( !dataOnly ) oputz("DELETE FROM sqlite_sequence;\n");
+ /* no-op */
}else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
- if( !dataOnly ) oputz("ANALYZE sqlite_schema;\n");
+ if( !dataOnly ) sqlite3_fputs("ANALYZE sqlite_schema;\n", p->out);
}else if( cli_strncmp(zTable, "sqlite_", 7)==0 ){
return 0;
}else if( dataOnly ){
@@ -22615,7 +25144,7 @@ static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
}else if( cli_strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
char *zIns;
if( !p->writableSchema ){
- oputz("PRAGMA writable_schema=ON;\n");
+ sqlite3_fputs("PRAGMA writable_schema=ON;\n", p->out);
p->writableSchema = 1;
}
zIns = sqlite3_mprintf(
@@ -22623,11 +25152,11 @@ static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
"VALUES('table','%q','%q',0,'%q');",
zTable, zTable, zSql);
shell_check_oom(zIns);
- oputf("%s\n", zIns);
+ sqlite3_fprintf(p->out, "%s\n", zIns);
sqlite3_free(zIns);
return 0;
}else{
- printSchemaLine(zSql, ";\n");
+ printSchemaLine(p->out, zSql, ";\n");
}
if( cli_strcmp(zType, "table")==0 ){
@@ -22685,7 +25214,7 @@ static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
p->mode = p->cMode = MODE_Insert;
rc = shell_exec(p, sSelect.z, 0);
if( (rc&0xff)==SQLITE_CORRUPT ){
- oputz("/****** CORRUPTION ERROR *******/\n");
+ sqlite3_fputs("/****** CORRUPTION ERROR *******/\n", p->out);
toggleSelectOrder(p->db);
shell_exec(p, sSelect.z, 0);
toggleSelectOrder(p->db);
@@ -22716,9 +25245,9 @@ static int run_schema_dump_query(
if( rc==SQLITE_CORRUPT ){
char *zQ2;
int len = strlen30(zQuery);
- oputz("/****** CORRUPTION ERROR *******/\n");
+ sqlite3_fputs("/****** CORRUPTION ERROR *******/\n", p->out);
if( zErr ){
- oputf("/****** %s ******/\n", zErr);
+ sqlite3_fprintf(p->out, "/****** %s ******/\n", zErr);
sqlite3_free(zErr);
zErr = 0;
}
@@ -22727,13 +25256,13 @@ static int run_schema_dump_query(
sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
if( rc ){
- oputf("/****** ERROR: %s ******/\n", zErr);
+ sqlite3_fprintf(p->out, "/****** ERROR: %s ******/\n", zErr);
}else{
rc = SQLITE_CORRUPT;
}
- sqlite3_free(zErr);
free(zQ2);
}
+ sqlite3_free(zErr);
return rc;
}
@@ -22790,14 +25319,13 @@ static const char *(azHelp[]) = {
".clone NEWDB Clone data into NEWDB from the existing database",
#endif
".connection [close] [#] Open or close an auxiliary database connection",
-#if defined(_WIN32) || defined(WIN32)
- ".crnl on|off Translate \\n to \\r\\n. Default ON",
-#endif
+ ".crlf ?on|off? Whether or not to use \\r\\n line endings",
".databases List names and files of attached databases",
".dbconfig ?op? ?val? List or change sqlite3_db_config() options",
#if SQLITE_SHELL_HAVE_RECOVER
".dbinfo ?DB? Show status information about the database",
#endif
+ ".dbtotxt Hex dump of the database file",
".dump ?OBJECTS? Render database content as SQL",
" Options:",
" --data-only Output only INSERT statements",
@@ -22867,7 +25395,7 @@ static const char *(azHelp[]) = {
#else
".log on|off Turn logging on or off.",
#endif
- ".mode MODE ?OPTIONS? Set output mode",
+ ".mode ?MODE? ?OPTIONS? Set output mode",
" MODE is one of:",
" ascii Columns/rows delimited by 0x1F and 0x1E",
" box Tables using unicode box-drawing characters",
@@ -22885,6 +25413,7 @@ static const char *(azHelp[]) = {
" tabs Tab-separated values",
" tcl TCL list elements",
" OPTIONS: (for columnar modes or insert mode):",
+ " --escape T ctrl-char escape; T is one of: symbol, ascii, off",
" --wrap N Wrap output lines to no longer than N characters",
" --wordwrap B Wrap or not at word boundaries per B (on/off)",
" --ww Shorthand for \"--wordwrap 1\"",
@@ -22898,9 +25427,11 @@ static const char *(azHelp[]) = {
#ifndef SQLITE_SHELL_FIDDLE
".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE",
" If FILE begins with '|' then open as a pipe",
- " --bom Put a UTF8 byte-order mark at the beginning",
- " -e Send output to the system text editor",
- " -x Send output as CSV to a spreadsheet (same as \".excel\")",
+ " --bom Put a UTF8 byte-order mark at the beginning",
+ " -e Send output to the system text editor",
+ " --plain Use text/plain output instead of HTML for -w option",
+ " -w Send output as HTML to a web browser (same as \".www\")",
+ " -x Send output as CSV to a spreadsheet (same as \".excel\")",
/* Note that .open is (partially) available in WASM builds but is
** currently only intended to be used by the fiddle tool, not
** end users, so is "undocumented." */
@@ -22920,9 +25451,12 @@ static const char *(azHelp[]) = {
#ifndef SQLITE_SHELL_FIDDLE
".output ?FILE? Send output to FILE or stdout if FILE is omitted",
" If FILE begins with '|' then open it as a pipe.",
+ " If FILE is 'off' then output is disabled.",
" Options:",
" --bom Prefix output with a UTF8 byte-order mark",
" -e Send output to the system text editor",
+ " --plain Use text/plain for -w option",
+ " -w Send output to a web browser",
" -x Send output as CSV to a spreadsheet",
#endif
".parameter CMD ... Manage SQL parameter bindings",
@@ -23036,106 +25570,111 @@ static const char *(azHelp[]) = {
".vfsname ?AUX? Print the name of the VFS stack",
".width NUM1 NUM2 ... Set minimum column widths for columnar output",
" Negative values right-justify",
+#ifndef SQLITE_SHELL_FIDDLE
+ ".www Display output of the next command in web browser",
+ " --plain Show results as text/plain, not as HTML",
+#endif
};
/*
-** Output help text.
+** Output help text for commands that match zPattern.
+**
+** * If zPattern is NULL, then show all documented commands, but
+** only give a one-line summary of each.
+**
+** * If zPattern is "-a" or "-all" or "--all" then show all help text
+** for all commands except undocumented commands.
+**
+** * If zPattern is "0" then show all help for undocumented commands.
+** Undocumented commands begin with "," instead of "." in the azHelp[]
+** array.
+**
+** * If zPattern is a prefix for one or more documented commands, then
+** show help for those commands. If only a single command matches the
+** prefix, show the full text of the help. If multiple commands match,
+** Only show just the first line of each.
**
-** zPattern describes the set of commands for which help text is provided.
-** If zPattern is NULL, then show all commands, but only give a one-line
-** description of each.
+** * Otherwise, show the complete text of any documented command for which
+** zPattern is a LIKE match for any text within that command help
+** text.
**
-** Return the number of matches.
+** Return the number commands that match zPattern.
*/
static int showHelp(FILE *out, const char *zPattern){
int i = 0;
int j = 0;
int n = 0;
char *zPat;
- if( zPattern==0
- || zPattern[0]=='0'
- || cli_strcmp(zPattern,"-a")==0
- || cli_strcmp(zPattern,"-all")==0
- || cli_strcmp(zPattern,"--all")==0
+ if( zPattern==0 ){
+ /* Show just the first line for all help topics */
+ zPattern = "[a-z]";
+ }else if( cli_strcmp(zPattern,"-a")==0
+ || cli_strcmp(zPattern,"-all")==0
+ || cli_strcmp(zPattern,"--all")==0
){
- enum HelpWanted { HW_NoCull = 0, HW_SummaryOnly = 1, HW_Undoc = 2 };
- enum HelpHave { HH_Undoc = 2, HH_Summary = 1, HH_More = 0 };
- /* Show all or most commands
- ** *zPattern==0 => summary of documented commands only
- ** *zPattern=='0' => whole help for undocumented commands
- ** Otherwise => whole help for documented commands
- */
- enum HelpWanted hw = HW_SummaryOnly;
- enum HelpHave hh = HH_More;
- if( zPattern!=0 ){
- hw = (*zPattern=='0')? HW_NoCull|HW_Undoc : HW_NoCull;
- }
- for(i=0; i<ArraySize(azHelp); i++){
- switch( azHelp[i][0] ){
- case ',':
- hh = HH_Summary|HH_Undoc;
- break;
- case '.':
- hh = HH_Summary;
- break;
- default:
- hh &= ~HH_Summary;
- break;
- }
- if( ((hw^hh)&HH_Undoc)==0 ){
- if( (hh&HH_Summary)!=0 ){
- sputf(out, ".%s\n", azHelp[i]+1);
- ++n;
- }else if( (hw&HW_SummaryOnly)==0 ){
- sputf(out, "%s\n", azHelp[i]);
- }
- }
- }
- }else{
- /* Seek documented commands for which zPattern is an exact prefix */
- zPat = sqlite3_mprintf(".%s*", zPattern);
- shell_check_oom(zPat);
+ /* Show everything except undocumented commands */
+ zPattern = ".";
+ }else if( cli_strcmp(zPattern,"0")==0 ){
+ /* Show complete help text of undocumented commands */
+ int show = 0;
for(i=0; i<ArraySize(azHelp); i++){
- if( sqlite3_strglob(zPat, azHelp[i])==0 ){
- sputf(out, "%s\n", azHelp[i]);
- j = i+1;
+ if( azHelp[i][0]=='.' ){
+ show = 0;
+ }else if( azHelp[i][0]==',' ){
+ show = 1;
+ sqlite3_fprintf(out, ".%s\n", &azHelp[i][1]);
n++;
+ }else if( show ){
+ sqlite3_fprintf(out, "%s\n", azHelp[i]);
}
}
- sqlite3_free(zPat);
- if( n ){
- if( n==1 ){
- /* when zPattern is a prefix of exactly one command, then include
- ** the details of that command, which should begin at offset j */
- while( j<ArraySize(azHelp)-1 && azHelp[j][0]==' ' ){
- sputf(out, "%s\n", azHelp[j]);
- j++;
- }
- }
- return n;
+ return n;
+ }
+
+ /* Seek documented commands for which zPattern is an exact prefix */
+ zPat = sqlite3_mprintf(".%s*", zPattern);
+ shell_check_oom(zPat);
+ for(i=0; i<ArraySize(azHelp); i++){
+ if( sqlite3_strglob(zPat, azHelp[i])==0 ){
+ sqlite3_fprintf(out, "%s\n", azHelp[i]);
+ j = i+1;
+ n++;
}
- /* Look for documented commands that contain zPattern anywhere.
- ** Show complete text of all documented commands that match. */
- zPat = sqlite3_mprintf("%%%s%%", zPattern);
- shell_check_oom(zPat);
- for(i=0; i<ArraySize(azHelp); i++){
- if( azHelp[i][0]==',' ){
- while( i<ArraySize(azHelp)-1 && azHelp[i+1][0]==' ' ) ++i;
- continue;
+ }
+ sqlite3_free(zPat);
+ if( n ){
+ if( n==1 ){
+ /* when zPattern is a prefix of exactly one command, then include
+ ** the details of that command, which should begin at offset j */
+ while( j<ArraySize(azHelp)-1 && azHelp[j][0]==' ' ){
+ sqlite3_fprintf(out, "%s\n", azHelp[j]);
+ j++;
}
- if( azHelp[i][0]=='.' ) j = i;
- if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
- sputf(out, "%s\n", azHelp[j]);
- while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]==' ' ){
- j++;
- sputf(out, "%s\n", azHelp[j]);
- }
- i = j;
- n++;
+ }
+ return n;
+ }
+
+ /* Look for documented commands that contain zPattern anywhere.
+ ** Show complete text of all documented commands that match. */
+ zPat = sqlite3_mprintf("%%%s%%", zPattern);
+ shell_check_oom(zPat);
+ for(i=0; i<ArraySize(azHelp); i++){
+ if( azHelp[i][0]==',' ){
+ while( i<ArraySize(azHelp)-1 && azHelp[i+1][0]==' ' ) ++i;
+ continue;
+ }
+ if( azHelp[i][0]=='.' ) j = i;
+ if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
+ sqlite3_fprintf(out, "%s\n", azHelp[j]);
+ while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]==' ' ){
+ j++;
+ sqlite3_fprintf(out, "%s\n", azHelp[j]);
}
+ i = j;
+ n++;
}
- sqlite3_free(zPat);
}
+ sqlite3_free(zPat);
return n;
}
@@ -23158,7 +25697,7 @@ static int process_input(ShellState *p);
** is undefined in this case.
*/
static char *readFile(const char *zName, int *pnByte){
- FILE *in = fopen(zName, "rb");
+ FILE *in = sqlite3_fopen(zName, "rb");
long nIn;
size_t nRead;
char *pBuf;
@@ -23166,7 +25705,7 @@ static char *readFile(const char *zName, int *pnByte){
if( in==0 ) return 0;
rc = fseek(in, 0, SEEK_END);
if( rc!=0 ){
- eputf("Error: '%s' not seekable\n", zName);
+ sqlite3_fprintf(stderr,"Error: '%s' not seekable\n", zName);
fclose(in);
return 0;
}
@@ -23174,7 +25713,7 @@ static char *readFile(const char *zName, int *pnByte){
rewind(in);
pBuf = sqlite3_malloc64( nIn+1 );
if( pBuf==0 ){
- eputz("Error: out of memory\n");
+ sqlite3_fputs("Error: out of memory\n", stderr);
fclose(in);
return 0;
}
@@ -23182,7 +25721,7 @@ static char *readFile(const char *zName, int *pnByte){
fclose(in);
if( nRead!=1 ){
sqlite3_free(pBuf);
- eputf("Error: cannot read '%s'\n", zName);
+ sqlite3_fprintf(stderr,"Error: cannot read '%s'\n", zName);
return 0;
}
pBuf[nIn] = 0;
@@ -23248,7 +25787,7 @@ static int session_filter(void *pCtx, const char *zTab){
** the type cannot be determined from content.
*/
int deduceDatabaseType(const char *zName, int dfltZip){
- FILE *f = fopen(zName, "rb");
+ FILE *f = sqlite3_fopen(zName, "rb");
size_t n;
int rc = SHELL_OPEN_UNSPEC;
char zBuf[100];
@@ -23301,9 +25840,9 @@ static unsigned char *readHexDb(ShellState *p, int *pnData){
unsigned int x[16];
char zLine[1000];
if( zDbFilename ){
- in = fopen(zDbFilename, "r");
+ in = sqlite3_fopen(zDbFilename, "r");
if( in==0 ){
- eputf("cannot open \"%s\" for reading\n", zDbFilename);
+ sqlite3_fprintf(stderr,"cannot open \"%s\" for reading\n", zDbFilename);
return 0;
}
nLine = 0;
@@ -23314,7 +25853,7 @@ static unsigned char *readHexDb(ShellState *p, int *pnData){
}
*pnData = 0;
nLine++;
- if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
+ if( sqlite3_fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
if( rc!=2 ) goto readHexDb_error;
if( n<0 ) goto readHexDb_error;
@@ -23324,10 +25863,10 @@ static unsigned char *readHexDb(ShellState *p, int *pnData){
shell_check_oom(a);
memset(a, 0, n);
if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
- eputz("invalid pagesize\n");
+ sqlite3_fputs("invalid pagesize\n", stderr);
goto readHexDb_error;
}
- for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
+ for(nLine++; sqlite3_fgets(zLine, sizeof(zLine), in)!=0; nLine++){
rc = sscanf(zLine, "| page %d offset %d", &j, &k);
if( rc==2 ){
iOffset = k;
@@ -23359,14 +25898,14 @@ readHexDb_error:
if( in!=p->in ){
fclose(in);
}else{
- while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
+ while( sqlite3_fgets(zLine, sizeof(zLine), p->in)!=0 ){
nLine++;
if(cli_strncmp(zLine, "| end ", 6)==0 ) break;
}
p->lineno = nLine;
}
sqlite3_free(a);
- eputf("Error on line %d of --hexdb input\n", nLine);
+ sqlite3_fprintf(stderr,"Error on line %d of --hexdb input\n", nLine);
return 0;
}
#endif /* SQLITE_OMIT_DESERIALIZE */
@@ -23385,6 +25924,39 @@ static void shellUSleepFunc(
sqlite3_result_int(context, sleep);
}
+/*
+** SQL function: shell_module_schema(X)
+**
+** Return a fake schema for the table-valued function or eponymous virtual
+** table X.
+*/
+static void shellModuleSchema(
+ sqlite3_context *pCtx,
+ int nVal,
+ sqlite3_value **apVal
+){
+ const char *zName;
+ char *zFake;
+ ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
+ FILE *pSavedLog = p->pLog;
+ UNUSED_PARAMETER(nVal);
+ zName = (const char*)sqlite3_value_text(apVal[0]);
+
+ /* Temporarily disable the ".log" when calling shellFakeSchema() because
+ ** shellFakeSchema() might generate failures for some ephemeral virtual
+ ** tables due to missing arguments. Example: fts4aux.
+ ** https://sqlite.org/forum/forumpost/42fe6520b803be51 */
+ p->pLog = 0;
+ zFake = zName? shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName) : 0;
+ p->pLog = pSavedLog;
+
+ if( zFake ){
+ sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
+ -1, sqlite3_free);
+ free(zFake);
+ }
+}
+
/* Flags for open_db().
**
** The default behavior of open_db() is to exit(1) if the database fails to
@@ -23441,7 +26013,7 @@ static void open_db(ShellState *p, int openFlags){
}
}
if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
- eputf("Error: unable to open database \"%s\": %s\n",
+ sqlite3_fprintf(stderr,"Error: unable to open database \"%s\": %s\n",
zDbFilename, sqlite3_errmsg(p->db));
if( (openFlags & OPEN_DB_KEEPALIVE)==0 ){
exit(1);
@@ -23449,10 +26021,12 @@ static void open_db(ShellState *p, int openFlags){
sqlite3_close(p->db);
sqlite3_open(":memory:", &p->db);
if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
- eputz("Also: unable to open substitute in-memory database.\n");
+ sqlite3_fputs("Also: unable to open substitute in-memory database.\n",
+ stderr);
exit(1);
}else{
- eputf("Notice: using substitute in-memory database instead of \"%s\"\n",
+ sqlite3_fprintf(stderr,
+ "Notice: using substitute in-memory database instead of \"%s\"\n",
zDbFilename);
}
}
@@ -23469,9 +26043,12 @@ static void open_db(ShellState *p, int openFlags){
#ifndef SQLITE_OMIT_LOAD_EXTENSION
sqlite3_enable_load_extension(p->db, 1);
#endif
+ sqlite3_sha_init(p->db, 0, 0);
sqlite3_shathree_init(p->db, 0, 0);
sqlite3_uint_init(p->db, 0, 0);
+ sqlite3_stmtrand_init(p->db, 0, 0);
sqlite3_decimal_init(p->db, 0, 0);
+ sqlite3_percentile_init(p->db, 0, 0);
sqlite3_base64_init(p->db, 0, 0);
sqlite3_base85_init(p->db, 0, 0);
sqlite3_regexp_init(p->db, 0, 0);
@@ -23523,7 +26100,7 @@ static void open_db(ShellState *p, int openFlags){
shellDtostr, 0, 0);
sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
shellAddSchemaName, 0, 0);
- sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
+ sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, p,
shellModuleSchema, 0, 0);
sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
shellPutsFunc, 0, 0);
@@ -23561,7 +26138,7 @@ static void open_db(ShellState *p, int openFlags){
SQLITE_DESERIALIZE_RESIZEABLE |
SQLITE_DESERIALIZE_FREEONCLOSE);
if( rc ){
- eputf("Error: sqlite3_deserialize() returns %d\n", rc);
+ sqlite3_fprintf(stderr,"Error: sqlite3_deserialize() returns %d\n", rc);
}
if( p->szMax>0 ){
sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
@@ -23585,11 +26162,13 @@ static void open_db(ShellState *p, int openFlags){
void close_db(sqlite3 *db){
int rc = sqlite3_close(db);
if( rc ){
- eputf("Error: sqlite3_close() returns %d: %s\n", rc, sqlite3_errmsg(db));
+ sqlite3_fprintf(stderr,
+ "Error: sqlite3_close() returns %d: %s\n", rc, sqlite3_errmsg(db));
}
}
-#if HAVE_READLINE || HAVE_EDITLINE
+#if (HAVE_READLINE || HAVE_EDITLINE) \
+ && !defined(SQLITE_OMIT_READLINE_COMPLETION)
/*
** Readline completion callbacks
*/
@@ -23624,18 +26203,28 @@ static char **readline_completion(const char *zText, int iStart, int iEnd){
#elif HAVE_LINENOISE
/*
-** Linenoise completion callback
+** Linenoise completion callback. Note that the 3rd argument is from
+** the "msteveb" version of linenoise, not the "antirez" version.
*/
-static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
+static void linenoise_completion(
+ const char *zLine,
+ linenoiseCompletions *lc
+#if HAVE_LINENOISE==2
+ ,void *pUserData
+#endif
+){
i64 nLine = strlen(zLine);
i64 i, iStart;
sqlite3_stmt *pStmt = 0;
char *zSql;
char zBuf[1000];
+#if HAVE_LINENOISE==2
+ UNUSED_PARAMETER(pUserData);
+#endif
if( nLine>(i64)sizeof(zBuf)-30 ) return;
if( zLine[0]=='.' || zLine[0]=='#') return;
- for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
+ for(i=nLine-1; i>=0 && (IsAlnum(zLine[i]) || zLine[i]=='_'); i--){}
if( i==nLine-1 ) return;
iStart = i+1;
memcpy(zBuf, zLine, iStart);
@@ -23746,7 +26335,8 @@ static int booleanValue(const char *zArg){
if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
return 0;
}
- eputf("ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", zArg);
+ sqlite3_fprintf(stderr,
+ "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", zArg);
return 0;
}
@@ -23773,7 +26363,7 @@ static void output_file_close(FILE *f){
** recognized and do the right thing. NULL is returned if the output
** filename is "off".
*/
-static FILE *output_file_open(const char *zFile, int bTextMode){
+static FILE *output_file_open(const char *zFile){
FILE *f;
if( cli_strcmp(zFile,"stdout")==0 ){
f = stdout;
@@ -23782,9 +26372,9 @@ static FILE *output_file_open(const char *zFile, int bTextMode){
}else if( cli_strcmp(zFile, "off")==0 ){
f = 0;
}else{
- f = fopen(zFile, bTextMode ? "w" : "wb");
+ f = sqlite3_fopen(zFile, "w");
if( f==0 ){
- eputf("Error: cannot open \"%s\"\n", zFile);
+ sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", zFile);
}
}
return f;
@@ -23837,12 +26427,13 @@ static int sql_trace_callback(
switch( mType ){
case SQLITE_TRACE_ROW:
case SQLITE_TRACE_STMT: {
- sputf(p->traceOut, "%.*s;\n", (int)nSql, zSql);
+ sqlite3_fprintf(p->traceOut, "%.*s;\n", (int)nSql, zSql);
break;
}
case SQLITE_TRACE_PROFILE: {
sqlite3_int64 nNanosec = pX ? *(sqlite3_int64*)pX : 0;
- sputf(p->traceOut, "%.*s; -- %lld ns\n", (int)nSql, zSql, nNanosec);
+ sqlite3_fprintf(p->traceOut,
+ "%.*s; -- %lld ns\n", (int)nSql, zSql, nNanosec);
break;
}
}
@@ -23949,10 +26540,11 @@ static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
break;
}
if( pc==cQuote && c!='\r' ){
- eputf("%s:%d: unescaped %c character\n", p->zFile, p->nLine, cQuote);
+ sqlite3_fprintf(stderr,"%s:%d: unescaped %c character\n",
+ p->zFile, p->nLine, cQuote);
}
if( c==EOF ){
- eputf("%s:%d: unterminated %c-quoted field\n",
+ sqlite3_fprintf(stderr,"%s:%d: unterminated %c-quoted field\n",
p->zFile, startLine, cQuote);
p->cTerm = c;
break;
@@ -24051,7 +26643,7 @@ static void tryToCloneData(
shell_check_oom(zQuery);
rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
if( rc ){
- eputf("Error %d: %s on [%s]\n",
+ sqlite3_fprintf(stderr,"Error %d: %s on [%s]\n",
sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zQuery);
goto end_data_xfer;
}
@@ -24068,7 +26660,7 @@ static void tryToCloneData(
memcpy(zInsert+i, ");", 3);
rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
if( rc ){
- eputf("Error %d: %s on [%s]\n",
+ sqlite3_fprintf(stderr,"Error %d: %s on [%s]\n",
sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb), zInsert);
goto end_data_xfer;
}
@@ -24104,7 +26696,7 @@ static void tryToCloneData(
} /* End for */
rc = sqlite3_step(pInsert);
if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
- eputf("Error %d: %s\n",
+ sqlite3_fprintf(stderr,"Error %d: %s\n",
sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb));
}
sqlite3_reset(pInsert);
@@ -24122,7 +26714,7 @@ static void tryToCloneData(
shell_check_oom(zQuery);
rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
if( rc ){
- eputf("Warning: cannot step \"%s\" backwards", zTable);
+ sqlite3_fprintf(stderr,"Warning: cannot step \"%s\" backwards", zTable);
break;
}
} /* End for(k=0...) */
@@ -24159,7 +26751,8 @@ static void tryToCloneSchema(
shell_check_oom(zQuery);
rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
if( rc ){
- eputf("Error: (%d) %s on [%s]\n", sqlite3_extended_errcode(p->db),
+ sqlite3_fprintf(stderr,
+ "Error: (%d) %s on [%s]\n", sqlite3_extended_errcode(p->db),
sqlite3_errmsg(p->db), zQuery);
goto end_schema_xfer;
}
@@ -24168,10 +26761,10 @@ static void tryToCloneSchema(
zSql = sqlite3_column_text(pQuery, 1);
if( zName==0 || zSql==0 ) continue;
if( sqlite3_stricmp((char*)zName, "sqlite_sequence")!=0 ){
- sputf(stdout, "%s... ", zName); fflush(stdout);
+ sqlite3_fprintf(stdout, "%s... ", zName); fflush(stdout);
sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
if( zErrMsg ){
- eputf("Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
+ sqlite3_fprintf(stderr,"Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
sqlite3_free(zErrMsg);
zErrMsg = 0;
}
@@ -24189,7 +26782,7 @@ static void tryToCloneSchema(
shell_check_oom(zQuery);
rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
if( rc ){
- eputf("Error: (%d) %s on [%s]\n",
+ sqlite3_fprintf(stderr,"Error: (%d) %s on [%s]\n",
sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zQuery);
goto end_schema_xfer;
}
@@ -24198,10 +26791,10 @@ static void tryToCloneSchema(
zSql = sqlite3_column_text(pQuery, 1);
if( zName==0 || zSql==0 ) continue;
if( sqlite3_stricmp((char*)zName, "sqlite_sequence")==0 ) continue;
- sputf(stdout, "%s... ", zName); fflush(stdout);
+ sqlite3_fprintf(stdout, "%s... ", zName); fflush(stdout);
sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
if( zErrMsg ){
- eputf("Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
+ sqlite3_fprintf(stderr,"Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
sqlite3_free(zErrMsg);
zErrMsg = 0;
}
@@ -24225,12 +26818,13 @@ static void tryToClone(ShellState *p, const char *zNewDb){
int rc;
sqlite3 *newDb = 0;
if( access(zNewDb,0)==0 ){
- eputf("File \"%s\" already exists.\n", zNewDb);
+ sqlite3_fprintf(stderr,"File \"%s\" already exists.\n", zNewDb);
return;
}
rc = sqlite3_open(zNewDb, &newDb);
if( rc ){
- eputf("Cannot create output database: %s\n", sqlite3_errmsg(newDb));
+ sqlite3_fprintf(stderr,
+ "Cannot create output database: %s\n", sqlite3_errmsg(newDb));
}else{
sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
@@ -24247,10 +26841,18 @@ static void tryToClone(ShellState *p, const char *zNewDb){
** Change the output stream (file or pipe or console) to something else.
*/
static void output_redir(ShellState *p, FILE *pfNew){
- if( p->out != stdout ) eputz("Output already redirected.\n");
- else{
+ if( p->out != stdout ){
+ sqlite3_fputs("Output already redirected.\n", stderr);
+ }else{
p->out = pfNew;
- setOutputStream(pfNew);
+ setCrlfMode(p);
+ if( p->mode==MODE_Www ){
+ sqlite3_fputs(
+ "<!DOCTYPE html>\n"
+ "<HTML><BODY><PRE>\n",
+ p->out
+ );
+ }
}
}
@@ -24267,6 +26869,9 @@ static void output_reset(ShellState *p){
pclose(p->out);
#endif
}else{
+ if( p->mode==MODE_Www ){
+ sqlite3_fputs("</PRE></BODY></HTML>\n", p->out);
+ }
output_file_close(p->out);
#ifndef SQLITE_NOHAVE_SYSTEM
if( p->doXdgOpen ){
@@ -24281,7 +26886,7 @@ static void output_reset(ShellState *p){
char *zCmd;
zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
if( system(zCmd) ){
- eputf("Failed: [%s]\n", zCmd);
+ sqlite3_fprintf(stderr,"Failed: [%s]\n", zCmd);
}else{
/* Give the start/open/xdg-open command some time to get
** going before we continue, and potential delete the
@@ -24296,7 +26901,7 @@ static void output_reset(ShellState *p){
}
p->outfile[0] = 0;
p->out = stdout;
- setOutputStream(stdout);
+ setCrlfMode(p);
}
#else
# define output_redir(SS,pfO)
@@ -24306,14 +26911,20 @@ static void output_reset(ShellState *p){
/*
** Run an SQL command and return the single integer result.
*/
-static int db_int(sqlite3 *db, const char *zSql){
+static int db_int(sqlite3 *db, const char *zSql, ...){
sqlite3_stmt *pStmt;
int res = 0;
- sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
+ char *z;
+ va_list ap;
+ va_start(ap, zSql);
+ z = sqlite3_vmprintf(zSql, ap);
+ va_end(ap);
+ sqlite3_prepare_v2(db, z, -1, &pStmt, 0);
if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
res = sqlite3_column_int(pStmt,0);
}
sqlite3_finalize(pStmt);
+ sqlite3_free(z);
return res;
}
@@ -24372,7 +26983,7 @@ static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
"SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
-1, &pStmt, 0);
if( rc ){
- eputf("error: %s\n", sqlite3_errmsg(p->db));
+ sqlite3_fprintf(stderr,"error: %s\n", sqlite3_errmsg(p->db));
sqlite3_finalize(pStmt);
return 1;
}
@@ -24385,28 +26996,28 @@ static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
memcpy(aHdr, pb, 100);
sqlite3_finalize(pStmt);
}else{
- eputz("unable to read database header\n");
+ sqlite3_fputs("unable to read database header\n", stderr);
sqlite3_finalize(pStmt);
return 1;
}
i = get2byteInt(aHdr+16);
if( i==1 ) i = 65536;
- oputf("%-20s %d\n", "database page size:", i);
- oputf("%-20s %d\n", "write format:", aHdr[18]);
- oputf("%-20s %d\n", "read format:", aHdr[19]);
- oputf("%-20s %d\n", "reserved bytes:", aHdr[20]);
+ sqlite3_fprintf(p->out, "%-20s %d\n", "database page size:", i);
+ sqlite3_fprintf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
+ sqlite3_fprintf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
+ sqlite3_fprintf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
for(i=0; i<ArraySize(aField); i++){
int ofst = aField[i].ofst;
unsigned int val = get4byteInt(aHdr + ofst);
- oputf("%-20s %u", aField[i].zName, val);
+ sqlite3_fprintf(p->out, "%-20s %u", aField[i].zName, val);
switch( ofst ){
case 56: {
- if( val==1 ) oputz(" (utf8)");
- if( val==2 ) oputz(" (utf16le)");
- if( val==3 ) oputz(" (utf16be)");
+ if( val==1 ) sqlite3_fputs(" (utf8)", p->out);
+ if( val==2 ) sqlite3_fputs(" (utf16le)", p->out);
+ if( val==3 ) sqlite3_fputs(" (utf16be)", p->out);
}
}
- oputz("\n");
+ sqlite3_fputs("\n", p->out);
}
if( zDb==0 ){
zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
@@ -24416,24 +27027,120 @@ static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
}
for(i=0; i<ArraySize(aQuery); i++){
- char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
- int val = db_int(p->db, zSql);
- sqlite3_free(zSql);
- oputf("%-20s %d\n", aQuery[i].zName, val);
+ int val = db_int(p->db, aQuery[i].zSql, zSchemaTab);
+ sqlite3_fprintf(p->out, "%-20s %d\n", aQuery[i].zName, val);
}
sqlite3_free(zSchemaTab);
sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
- oputf("%-20s %u\n", "data version", iDataVersion);
+ sqlite3_fprintf(p->out, "%-20s %u\n", "data version", iDataVersion);
return 0;
}
#endif /* SQLITE_SHELL_HAVE_RECOVER */
/*
+** Implementation of the ".dbtotxt" command.
+**
+** Return 1 on error, 2 to exit, and 0 otherwise.
+*/
+static int shell_dbtotxt_command(ShellState *p, int nArg, char **azArg){
+ sqlite3_stmt *pStmt = 0;
+ sqlite3_int64 nPage = 0;
+ int pgSz = 0;
+ const char *zTail;
+ char *zName = 0;
+ int rc, i, j;
+ unsigned char bShow[256]; /* Characters ok to display */
+
+ UNUSED_PARAMETER(nArg);
+ UNUSED_PARAMETER(azArg);
+ memset(bShow, '.', sizeof(bShow));
+ for(i=' '; i<='~'; i++){
+ if( i!='{' && i!='}' && i!='"' && i!='\\' ) bShow[i] = (unsigned char)i;
+ }
+ rc = sqlite3_prepare_v2(p->db, "PRAGMA page_size", -1, &pStmt, 0);
+ if( rc ) goto dbtotxt_error;
+ rc = 0;
+ if( sqlite3_step(pStmt)!=SQLITE_ROW ) goto dbtotxt_error;
+ pgSz = sqlite3_column_int(pStmt, 0);
+ sqlite3_finalize(pStmt);
+ pStmt = 0;
+ if( pgSz<512 || pgSz>65536 || (pgSz&(pgSz-1))!=0 ) goto dbtotxt_error;
+ rc = sqlite3_prepare_v2(p->db, "PRAGMA page_count", -1, &pStmt, 0);
+ if( rc ) goto dbtotxt_error;
+ rc = 0;
+ if( sqlite3_step(pStmt)!=SQLITE_ROW ) goto dbtotxt_error;
+ nPage = sqlite3_column_int64(pStmt, 0);
+ sqlite3_finalize(pStmt);
+ pStmt = 0;
+ if( nPage<1 ) goto dbtotxt_error;
+ rc = sqlite3_prepare_v2(p->db, "PRAGMA databases", -1, &pStmt, 0);
+ if( rc ) goto dbtotxt_error;
+ if( sqlite3_step(pStmt)!=SQLITE_ROW ){
+ zTail = "unk.db";
+ }else{
+ const char *zFilename = (const char*)sqlite3_column_text(pStmt, 2);
+ if( zFilename==0 || zFilename[0]==0 ) zFilename = "unk.db";
+ zTail = strrchr(zFilename, '/');
+#if defined(_WIN32)
+ if( zTail==0 ) zTail = strrchr(zFilename, '\\');
+#endif
+ }
+ zName = strdup(zTail);
+ shell_check_oom(zName);
+ sqlite3_fprintf(p->out, "| size %lld pagesize %d filename %s\n",
+ nPage*pgSz, pgSz, zName);
+ sqlite3_finalize(pStmt);
+ pStmt = 0;
+ rc = sqlite3_prepare_v2(p->db,
+ "SELECT pgno, data FROM sqlite_dbpage ORDER BY pgno", -1, &pStmt, 0);
+ if( rc ) goto dbtotxt_error;
+ while( sqlite3_step(pStmt)==SQLITE_ROW ){
+ sqlite3_int64 pgno = sqlite3_column_int64(pStmt, 0);
+ const u8 *aData = sqlite3_column_blob(pStmt, 1);
+ int seenPageLabel = 0;
+ for(i=0; i<pgSz; i+=16){
+ const u8 *aLine = aData+i;
+ for(j=0; j<16 && aLine[j]==0; j++){}
+ if( j==16 ) continue;
+ if( !seenPageLabel ){
+ sqlite3_fprintf(p->out, "| page %lld offset %lld\n",pgno,(pgno-1)*pgSz);
+ seenPageLabel = 1;
+ }
+ sqlite3_fprintf(p->out, "| %5d:", i);
+ for(j=0; j<16; j++) sqlite3_fprintf(p->out, " %02x", aLine[j]);
+ sqlite3_fprintf(p->out, " ");
+ for(j=0; j<16; j++){
+ unsigned char c = (unsigned char)aLine[j];
+ sqlite3_fprintf(p->out, "%c", bShow[c]);
+ }
+ sqlite3_fprintf(p->out, "\n");
+ }
+ }
+ sqlite3_finalize(pStmt);
+ sqlite3_fprintf(p->out, "| end %s\n", zName);
+ free(zName);
+ return 0;
+
+dbtotxt_error:
+ if( rc ){
+ sqlite3_fprintf(stderr, "ERROR: %s\n", sqlite3_errmsg(p->db));
+ }
+ sqlite3_finalize(pStmt);
+ free(zName);
+ return 1;
+}
+
+/*
+** Print the given string as an error message.
+*/
+static void shellEmitError(const char *zErr){
+ sqlite3_fprintf(stderr,"Error: %s\n", zErr);
+}
+/*
** Print the current sqlite3_errmsg() value to stderr and return 1.
*/
static int shellDatabaseError(sqlite3 *db){
- const char *zErr = sqlite3_errmsg(db);
- eputf("Error: %s\n", zErr);
+ shellEmitError(sqlite3_errmsg(db));
return 1;
}
@@ -24674,6 +27381,7 @@ static int lintFkeyIndexes(
const char *zIndent = ""; /* How much to indent CREATE INDEX by */
int rc; /* Return code */
sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */
+ FILE *out = pState->out; /* Send output here */
/*
** This SELECT statement returns one row for each foreign key constraint
@@ -24749,7 +27457,8 @@ static int lintFkeyIndexes(
zIndent = " ";
}
else{
- eputf("Usage: %s %s ?-verbose? ?-groupbyparent?\n", azArg[0], azArg[1]);
+ sqlite3_fprintf(stderr,
+ "Usage: %s %s ?-verbose? ?-groupbyparent?\n", azArg[0], azArg[1]);
return SQLITE_ERROR;
}
}
@@ -24793,22 +27502,23 @@ static int lintFkeyIndexes(
if( rc!=SQLITE_OK ) break;
if( res<0 ){
- eputz("Error: internal error");
+ sqlite3_fputs("Error: internal error", stderr);
break;
}else{
if( bGroupByParent
&& (bVerbose || res==0)
&& (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
){
- oputf("-- Parent table %s\n", zParent);
+ sqlite3_fprintf(out, "-- Parent table %s\n", zParent);
sqlite3_free(zPrev);
zPrev = sqlite3_mprintf("%s", zParent);
}
if( res==0 ){
- oputf("%s%s --> %s\n", zIndent, zCI, zTarget);
+ sqlite3_fprintf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
}else if( bVerbose ){
- oputf("%s/* no extra indexes required for %s -> %s */\n",
+ sqlite3_fprintf(out,
+ "%s/* no extra indexes required for %s -> %s */\n",
zIndent, zFrom, zTarget
);
}
@@ -24817,16 +27527,16 @@ static int lintFkeyIndexes(
sqlite3_free(zPrev);
if( rc!=SQLITE_OK ){
- eputf("%s\n", sqlite3_errmsg(db));
+ sqlite3_fprintf(stderr,"%s\n", sqlite3_errmsg(db));
}
rc2 = sqlite3_finalize(pSql);
if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
rc = rc2;
- eputf("%s\n", sqlite3_errmsg(db));
+ sqlite3_fprintf(stderr,"%s\n", sqlite3_errmsg(db));
}
}else{
- eputf("%s\n", sqlite3_errmsg(db));
+ sqlite3_fprintf(stderr,"%s\n", sqlite3_errmsg(db));
}
return rc;
@@ -24846,9 +27556,9 @@ static int lintDotCommand(
return lintFkeyIndexes(pState, azArg, nArg);
usage:
- eputf("Usage %s sub-command ?switches...?\n", azArg[0]);
- eputz("Where sub-commands are:\n");
- eputz(" fkey-indexes\n");
+ sqlite3_fprintf(stderr,"Usage %s sub-command ?switches...?\n", azArg[0]);
+ sqlite3_fprintf(stderr, "Where sub-commands are:\n");
+ sqlite3_fprintf(stderr, " fkey-indexes\n");
return SQLITE_ERROR;
}
@@ -24862,7 +27572,8 @@ static void shellPrepare(
if( *pRc==SQLITE_OK ){
int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
if( rc!=SQLITE_OK ){
- eputf("sql error: %s (%d)\n", sqlite3_errmsg(db), sqlite3_errcode(db));
+ sqlite3_fprintf(stderr,
+ "sql error: %s (%d)\n", sqlite3_errmsg(db), sqlite3_errcode(db));
*pRc = rc;
}
}
@@ -24906,7 +27617,7 @@ static void shellFinalize(
int rc = sqlite3_finalize(pStmt);
if( *pRc==SQLITE_OK ){
if( rc!=SQLITE_OK ){
- eputf("SQL error: %s\n", sqlite3_errmsg(db));
+ sqlite3_fprintf(stderr,"SQL error: %s\n", sqlite3_errmsg(db));
}
*pRc = rc;
}
@@ -24928,7 +27639,7 @@ void shellReset(
if( *pRc==SQLITE_OK ){
if( rc!=SQLITE_OK ){
sqlite3 *db = sqlite3_db_handle(pStmt);
- eputf("SQL error: %s\n", sqlite3_errmsg(db));
+ sqlite3_fprintf(stderr,"SQL error: %s\n", sqlite3_errmsg(db));
}
*pRc = rc;
}
@@ -24957,6 +27668,7 @@ struct ArCommand {
const char *zDir; /* --directory argument, or NULL */
char **azArg; /* Array of command arguments */
ShellState *p; /* Shell state */
+ FILE *out; /* Output to this stream */
sqlite3 *db; /* Database containing the archive */
};
@@ -24978,11 +27690,11 @@ static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
va_start(ap, zFmt);
z = sqlite3_vmprintf(zFmt, ap);
va_end(ap);
- eputf("Error: %s\n", z);
+ shellEmitError(z);
if( pAr->fromCmdLine ){
- eputz("Use \"-A\" for more help\n");
+ sqlite3_fputs("Use \"-A\" for more help\n", stderr);
}else{
- eputz("Use \".archive --help\" for more help\n");
+ sqlite3_fputs("Use \".archive --help\" for more help\n", stderr);
}
sqlite3_free(z);
return SQLITE_ERROR;
@@ -25035,7 +27747,7 @@ static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
break;
case AR_SWITCH_APPEND:
pAr->bAppend = 1;
- deliberate_fall_through;
+ deliberate_fall_through; /* FALLTHRU */
case AR_SWITCH_FILE:
pAr->zFile = zArg;
break;
@@ -25082,7 +27794,7 @@ static int arParseCommand(
struct ArSwitch *pEnd = &aSwitch[nSwitch];
if( nArg<=1 ){
- eputz("Wrong number of arguments. Usage:\n");
+ sqlite3_fprintf(stderr, "Wrong number of arguments. Usage:\n");
return arUsage(stderr);
}else{
char *z = azArg[1];
@@ -25188,7 +27900,7 @@ static int arParseCommand(
}
}
if( pAr->eCmd==0 ){
- eputz("Required argument missing. Usage:\n");
+ sqlite3_fprintf(stderr, "Required argument missing. Usage:\n");
return arUsage(stderr);
}
return SQLITE_OK;
@@ -25231,7 +27943,7 @@ static int arCheckEntries(ArCommand *pAr){
}
shellReset(&rc, pTest);
if( rc==SQLITE_OK && bOk==0 ){
- eputf("not found in archive: %s\n", z);
+ sqlite3_fprintf(stderr,"not found in archive: %s\n", z);
rc = SQLITE_ERROR;
}
}
@@ -25298,15 +28010,15 @@ static int arListCommand(ArCommand *pAr){
shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
pAr->zSrcTable, zWhere);
if( pAr->bDryRun ){
- oputf("%s\n", sqlite3_sql(pSql));
+ sqlite3_fprintf(pAr->out, "%s\n", sqlite3_sql(pSql));
}else{
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
if( pAr->bVerbose ){
- oputf("%s % 10d %s %s\n",
+ sqlite3_fprintf(pAr->out, "%s % 10d %s %s\n",
sqlite3_column_text(pSql, 0), sqlite3_column_int(pSql, 1),
sqlite3_column_text(pSql, 2),sqlite3_column_text(pSql, 3));
}else{
- oputf("%s\n", sqlite3_column_text(pSql, 0));
+ sqlite3_fprintf(pAr->out, "%s\n", sqlite3_column_text(pSql, 0));
}
}
}
@@ -25333,7 +28045,7 @@ static int arRemoveCommand(ArCommand *pAr){
zSql = sqlite3_mprintf("DELETE FROM %s WHERE %s;",
pAr->zSrcTable, zWhere);
if( pAr->bDryRun ){
- oputf("%s\n", zSql);
+ sqlite3_fprintf(pAr->out, "%s\n", zSql);
}else{
char *zErr = 0;
rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0);
@@ -25346,7 +28058,7 @@ static int arRemoveCommand(ArCommand *pAr){
}
}
if( zErr ){
- sputf(stdout, "ERROR: %s\n", zErr); /* stdout? */
+ sqlite3_fprintf(stdout, "ERROR: %s\n", zErr); /* stdout? */
sqlite3_free(zErr);
}
}
@@ -25410,11 +28122,11 @@ static int arExtractCommand(ArCommand *pAr){
j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
sqlite3_bind_int(pSql, j, i);
if( pAr->bDryRun ){
- oputf("%s\n", sqlite3_sql(pSql));
+ sqlite3_fprintf(pAr->out, "%s\n", sqlite3_sql(pSql));
}else{
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
if( i==0 && pAr->bVerbose ){
- oputf("%s\n", sqlite3_column_text(pSql, 0));
+ sqlite3_fprintf(pAr->out, "%s\n", sqlite3_column_text(pSql, 0));
}
}
}
@@ -25434,13 +28146,13 @@ static int arExtractCommand(ArCommand *pAr){
static int arExecSql(ArCommand *pAr, const char *zSql){
int rc;
if( pAr->bDryRun ){
- oputf("%s\n", zSql);
+ sqlite3_fprintf(pAr->out, "%s\n", zSql);
rc = SQLITE_OK;
}else{
char *zErr = 0;
rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
if( zErr ){
- sputf(stdout, "ERROR: %s\n", zErr);
+ sqlite3_fprintf(stdout, "ERROR: %s\n", zErr);
sqlite3_free(zErr);
}
}
@@ -25589,6 +28301,7 @@ static int arDotCommand(
if( rc==SQLITE_OK ){
int eDbType = SHELL_OPEN_UNSPEC;
cmd.p = pState;
+ cmd.out = pState->out;
cmd.db = pState->db;
if( cmd.zFile ){
eDbType = deduceDatabaseType(cmd.zFile, 1);
@@ -25615,13 +28328,14 @@ static int arDotCommand(
}
cmd.db = 0;
if( cmd.bDryRun ){
- oputf("-- open database '%s'%s\n", cmd.zFile,
+ sqlite3_fprintf(cmd.out, "-- open database '%s'%s\n", cmd.zFile,
eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
}
rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
if( rc!=SQLITE_OK ){
- eputf("cannot open file: %s (%s)\n", cmd.zFile, sqlite3_errmsg(cmd.db));
+ sqlite3_fprintf(stderr, "cannot open file: %s (%s)\n",
+ cmd.zFile, sqlite3_errmsg(cmd.db));
goto end_ar_command;
}
sqlite3_fileio_init(cmd.db, 0, 0);
@@ -25634,7 +28348,7 @@ static int arDotCommand(
if( cmd.eCmd!=AR_CMD_CREATE
&& sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
){
- eputz("database does not contain an 'sqlar' table\n");
+ sqlite3_fprintf(stderr, "database does not contain an 'sqlar' table\n");
rc = SQLITE_ERROR;
goto end_ar_command;
}
@@ -25692,7 +28406,7 @@ end_ar_command:
*/
static int recoverSqlCb(void *pCtx, const char *zSql){
ShellState *pState = (ShellState*)pCtx;
- sputf(pState->out, "%s;\n", zSql);
+ sqlite3_fprintf(pState->out, "%s;\n", zSql);
return SQLITE_OK;
}
@@ -25735,7 +28449,7 @@ static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
bRowids = 0;
}
else{
- eputf("unexpected option: %s\n", azArg[i]);
+ sqlite3_fprintf(stderr,"unexpected option: %s\n", azArg[i]);
showHelp(pState->out, azArg[0]);
return 1;
}
@@ -25750,11 +28464,12 @@ static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
sqlite3_recover_config(p, SQLITE_RECOVER_ROWIDS, (void*)&bRowids);
sqlite3_recover_config(p, SQLITE_RECOVER_FREELIST_CORRUPT,(void*)&bFreelist);
+ sqlite3_fprintf(pState->out, ".dbconfig defensive off\n");
sqlite3_recover_run(p);
if( sqlite3_recover_errcode(p)!=SQLITE_OK ){
const char *zErr = sqlite3_recover_errmsg(p);
int errCode = sqlite3_recover_errcode(p);
- eputf("sql error: %s (%d)\n", zErr, errCode);
+ sqlite3_fprintf(stderr,"sql error: %s (%d)\n", zErr, errCode);
}
rc = sqlite3_recover_finish(p);
return rc;
@@ -25776,7 +28491,7 @@ static int intckDatabaseCmd(ShellState *pState, i64 nStepPerUnlock){
while( SQLITE_OK==sqlite3_intck_step(p) ){
const char *zMsg = sqlite3_intck_message(p);
if( zMsg ){
- oputf("%s\n", zMsg);
+ sqlite3_fprintf(pState->out, "%s\n", zMsg);
nError++;
}
nStep++;
@@ -25786,11 +28501,11 @@ static int intckDatabaseCmd(ShellState *pState, i64 nStepPerUnlock){
}
rc = sqlite3_intck_error(p, &zErr);
if( zErr ){
- eputf("%s\n", zErr);
+ sqlite3_fprintf(stderr,"%s\n", zErr);
}
sqlite3_intck_close(p);
- oputf("%lld steps, %lld errors\n", nStep, nError);
+ sqlite3_fprintf(pState->out, "%lld steps, %lld errors\n", nStep, nError);
}
return rc;
@@ -25813,7 +28528,7 @@ static int intckDatabaseCmd(ShellState *pState, i64 nStepPerUnlock){
#define rc_err_oom_die(rc) \
if( rc==SQLITE_NOMEM ) shell_check_oom(0); \
else if(!(rc==SQLITE_OK||rc==SQLITE_DONE)) \
- eputf("E:%d\n",rc), assert(0)
+ sqlite3_fprintf(stderr,"E:%d\n",rc), assert(0)
#else
static void rc_err_oom_die(int rc){
if( rc==SQLITE_NOMEM ) shell_check_oom(0);
@@ -25971,8 +28686,8 @@ FROM (\
}else{
/* Formulate the columns spec, close the DB, zero *pDb. */
char *zColsSpec = 0;
- int hasDupes = db_int(*pDb, zHasDupes);
- int nDigits = (hasDupes)? db_int(*pDb, zColDigits) : 0;
+ int hasDupes = db_int(*pDb, "%s", zHasDupes);
+ int nDigits = (hasDupes)? db_int(*pDb, "%s", zColDigits) : 0;
if( hasDupes ){
#ifdef SHELL_COLUMN_RENAME_CLEAN
rc = sqlite3_exec(*pDb, zDedoctor, 0, 0, 0);
@@ -25987,7 +28702,7 @@ FROM (\
sqlite3_finalize(pStmt);
if( rc!=SQLITE_DONE ) rc_err_oom_die(SQLITE_NOMEM);
}
- assert(db_int(*pDb, zHasDupes)==0); /* Consider: remove this */
+ assert(db_int(*pDb, "%s", zHasDupes)==0); /* Consider: remove this */
rc = sqlite3_prepare_v2(*pDb, zCollectVar, -1, &pStmt, 0);
rc_err_oom_die(rc);
rc = sqlite3_step(pStmt);
@@ -26030,8 +28745,9 @@ static int outputDumpWarning(ShellState *p, const char *zLike){
"sql LIKE 'CREATE VIRTUAL TABLE%%' AND %s", zLike ? zLike : "true"
);
if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
- oputz("/* WARNING: "
- "Script requires that SQLITE_DBCONFIG_DEFENSIVE be disabled */\n"
+ sqlite3_fputs("/* WARNING: "
+ "Script requires that SQLITE_DBCONFIG_DEFENSIVE be disabled */\n",
+ p->out
);
}
shellFinalize(&rc, pStmt);
@@ -26062,12 +28778,14 @@ static int faultsim_callback(int iArg){
if( faultsim_state.iCnt ){
if( faultsim_state.iCnt>0 ) faultsim_state.iCnt--;
if( faultsim_state.eVerbose>=2 ){
- oputf("FAULT-SIM id=%d no-fault (cnt=%d)\n", iArg, faultsim_state.iCnt);
+ sqlite3_fprintf(stdout,
+ "FAULT-SIM id=%d no-fault (cnt=%d)\n", iArg, faultsim_state.iCnt);
}
return SQLITE_OK;
}
if( faultsim_state.eVerbose>=1 ){
- oputf("FAULT-SIM id=%d returns %d\n", iArg, faultsim_state.iErr);
+ sqlite3_fprintf(stdout,
+ "FAULT-SIM id=%d returns %d\n", iArg, faultsim_state.iErr);
}
faultsim_state.iCnt = faultsim_state.iInterval;
faultsim_state.nHit++;
@@ -26130,7 +28848,7 @@ static int do_meta_command(char *zLine, ShellState *p){
#ifndef SQLITE_OMIT_AUTHORIZATION
if( c=='a' && cli_strncmp(azArg[0], "auth", n)==0 ){
if( nArg!=2 ){
- eputz("Usage: .auth ON|OFF\n");
+ sqlite3_fprintf(stderr, "Usage: .auth ON|OFF\n");
rc = 1;
goto meta_command_exit;
}
@@ -26177,7 +28895,7 @@ static int do_meta_command(char *zLine, ShellState *p){
bAsync = 1;
}else
{
- eputf("unknown option: %s\n", azArg[j]);
+ sqlite3_fprintf(stderr,"unknown option: %s\n", azArg[j]);
return 1;
}
}else if( zDestFile==0 ){
@@ -26186,19 +28904,19 @@ static int do_meta_command(char *zLine, ShellState *p){
zDb = zDestFile;
zDestFile = azArg[j];
}else{
- eputz("Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
+ sqlite3_fprintf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
return 1;
}
}
if( zDestFile==0 ){
- eputz("missing FILENAME argument on .backup\n");
+ sqlite3_fprintf(stderr, "missing FILENAME argument on .backup\n");
return 1;
}
if( zDb==0 ) zDb = "main";
rc = sqlite3_open_v2(zDestFile, &pDest,
SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
if( rc!=SQLITE_OK ){
- eputf("Error: cannot open \"%s\"\n", zDestFile);
+ sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", zDestFile);
close_db(pDest);
return 1;
}
@@ -26209,7 +28927,7 @@ static int do_meta_command(char *zLine, ShellState *p){
open_db(p, 0);
pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
if( pBackup==0 ){
- eputf("Error: %s\n", sqlite3_errmsg(pDest));
+ shellDatabaseError(pDest);
close_db(pDest);
return 1;
}
@@ -26218,7 +28936,7 @@ static int do_meta_command(char *zLine, ShellState *p){
if( rc==SQLITE_DONE ){
rc = 0;
}else{
- eputf("Error: %s\n", sqlite3_errmsg(pDest));
+ shellDatabaseError(pDest);
rc = 1;
}
close_db(pDest);
@@ -26234,19 +28952,10 @@ static int do_meta_command(char *zLine, ShellState *p){
}
}else
- /* Undocumented. Legacy only. See "crnl" below */
+ /* Undocumented. Legacy only. See "crlf" below */
if( c=='b' && n>=3 && cli_strncmp(azArg[0], "binary", n)==0 ){
- if( nArg==2 ){
- if( booleanValue(azArg[1]) ){
- setBinaryMode(p->out, 1);
- }else{
- setTextMode(p->out, 1);
- }
- }else{
- eputz("The \".binary\" command is deprecated. Use \".crnl\" instead.\n"
- "Usage: .binary on|off\n");
- rc = 1;
- }
+ eputz("The \".binary\" command is deprecated.\n");
+ rc = 1;
}else
/* The undocumented ".breakpoint" command causes a call to the no-op
@@ -26268,7 +28977,7 @@ static int do_meta_command(char *zLine, ShellState *p){
rc = chdir(azArg[1]);
#endif
if( rc ){
- eputf("Cannot change to directory \"%s\"\n", azArg[1]);
+ sqlite3_fprintf(stderr,"Cannot change to directory \"%s\"\n", azArg[1]);
rc = 1;
}
}else{
@@ -26301,11 +29010,12 @@ static int do_meta_command(char *zLine, ShellState *p){
}else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
rc = 2;
}else if( testcase_glob(azArg[1],zRes)==0 ){
- eputf("testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
+ sqlite3_fprintf(stderr,
+ "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
p->zTestcase, azArg[1], zRes);
rc = 1;
}else{
- oputf("testcase-%s ok\n", p->zTestcase);
+ sqlite3_fprintf(p->out, "testcase-%s ok\n", p->zTestcase);
p->nCheck++;
}
sqlite3_free(zRes);
@@ -26338,9 +29048,9 @@ static int do_meta_command(char *zLine, ShellState *p){
zFile = "(temporary-file)";
}
if( p->pAuxDb == &p->aAuxDb[i] ){
- sputf(stdout, "ACTIVE %d: %s\n", i, zFile);
+ sqlite3_fprintf(stdout, "ACTIVE %d: %s\n", i, zFile);
}else if( p->aAuxDb[i].db!=0 ){
- sputf(stdout, " %d: %s\n", i, zFile);
+ sqlite3_fprintf(stdout, " %d: %s\n", i, zFile);
}
}
}else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){
@@ -26370,20 +29080,18 @@ static int do_meta_command(char *zLine, ShellState *p){
}
}else
- if( c=='c' && n==4 && cli_strncmp(azArg[0], "crnl", n)==0 ){
+ if( c=='c' && n==4
+ && (cli_strncmp(azArg[0], "crlf", n)==0
+ || cli_strncmp(azArg[0], "crnl",n)==0)
+ ){
if( nArg==2 ){
- if( booleanValue(azArg[1]) ){
- setTextMode(p->out, 1);
- }else{
- setBinaryMode(p->out, 1);
- }
- }else{
-#if !defined(_WIN32) && !defined(WIN32)
- eputz("The \".crnl\" is a no-op on non-Windows machines.\n");
+#ifdef _WIN32
+ p->crlfMode = booleanValue(azArg[1]);
+#else
+ p->crlfMode = 0;
#endif
- eputz("Usage: .crnl on|off\n");
- rc = 1;
}
+ sqlite3_fprintf(stderr, "crlf is %s\n", p->crlfMode ? "ON" : "OFF");
}else
if( c=='d' && n>1 && cli_strncmp(azArg[0], "databases", n)==0 ){
@@ -26394,7 +29102,7 @@ static int do_meta_command(char *zLine, ShellState *p){
open_db(p, 0);
rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
if( rc ){
- eputf("Error: %s\n", sqlite3_errmsg(p->db));
+ shellDatabaseError(p->db);
rc = 1;
}else{
while( sqlite3_step(pStmt)==SQLITE_ROW ){
@@ -26413,7 +29121,7 @@ static int do_meta_command(char *zLine, ShellState *p){
int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
const char *z = azName[i*2+1];
- oputf("%s: %s %s%s\n",
+ sqlite3_fprintf(p->out, "%s: %s %s%s\n",
azName[i*2], z && z[0] ? z : "\"\"", bRdonly ? "r/o" : "r/w",
eTxn==SQLITE_TXN_NONE ? "" :
eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
@@ -26428,6 +29136,9 @@ static int do_meta_command(char *zLine, ShellState *p){
const char *zName;
int op;
} aDbConfig[] = {
+ { "attach_create", SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE },
+ { "attach_write", SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE },
+ { "comments", SQLITE_DBCONFIG_ENABLE_COMMENTS },
{ "defensive", SQLITE_DBCONFIG_DEFENSIVE },
{ "dqs_ddl", SQLITE_DBCONFIG_DQS_DDL },
{ "dqs_dml", SQLITE_DBCONFIG_DQS_DML },
@@ -26455,11 +29166,12 @@ static int do_meta_command(char *zLine, ShellState *p){
sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
}
sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
- oputf("%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
+ sqlite3_fprintf(p->out, "%19s %s\n",
+ aDbConfig[ii].zName, v ? "on" : "off");
if( nArg>1 ) break;
}
if( nArg>1 && ii==ArraySize(aDbConfig) ){
- eputf("Error: unknown dbconfig \"%s\"\n", azArg[1]);
+ sqlite3_fprintf(stderr,"Error: unknown dbconfig \"%s\"\n", azArg[1]);
eputz("Enter \".dbconfig\" with no arguments for a list\n");
}
}else
@@ -26509,7 +29221,8 @@ static int do_meta_command(char *zLine, ShellState *p){
ShellSetFlag(p, SHFLG_DumpNoSys);
}else
{
- eputf("Unknown option \"%s\" on \".dump\"\n", azArg[i]);
+ sqlite3_fprintf(stderr,
+ "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
rc = 1;
sqlite3_free(zLike);
goto meta_command_exit;
@@ -26544,8 +29257,8 @@ static int do_meta_command(char *zLine, ShellState *p){
/* When playing back a "dump", the content might appear in an order
** which causes immediate foreign key constraints to be violated.
** So disable foreign-key constraint enforcement to prevent problems. */
- oputz("PRAGMA foreign_keys=OFF;\n");
- oputz("BEGIN TRANSACTION;\n");
+ sqlite3_fputs("PRAGMA foreign_keys=OFF;\n", p->out);
+ sqlite3_fputs("BEGIN TRANSACTION;\n", p->out);
}
p->writableSchema = 0;
p->showHeader = 0;
@@ -26577,13 +29290,13 @@ static int do_meta_command(char *zLine, ShellState *p){
}
sqlite3_free(zLike);
if( p->writableSchema ){
- oputz("PRAGMA writable_schema=OFF;\n");
+ sqlite3_fputs("PRAGMA writable_schema=OFF;\n", p->out);
p->writableSchema = 0;
}
sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
- oputz(p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
+ sqlite3_fputs(p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n", p->out);
}
p->showHeader = savedShowHeader;
p->shellFlgs = savedShellFlags;
@@ -26598,6 +29311,10 @@ static int do_meta_command(char *zLine, ShellState *p){
}
}else
+ if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbtotxt", n)==0 ){
+ rc = shell_dbtotxt_command(p, nArg, azArg);
+ }else
+
if( c=='e' && cli_strncmp(azArg[0], "eqp", n)==0 ){
if( nArg==2 ){
p->autoEQPtest = 0;
@@ -26663,7 +29380,8 @@ static int do_meta_command(char *zLine, ShellState *p){
#ifndef SQLITE_OMIT_VIRTUALTABLE
if( c=='e' && cli_strncmp(azArg[0], "expert", n)==0 ){
if( p->bSafeMode ){
- eputf("Cannot run experimental commands such as \"%s\" in safe mode\n",
+ sqlite3_fprintf(stderr,
+ "Cannot run experimental commands such as \"%s\" in safe mode\n",
azArg[0]);
rc = 1;
}else{
@@ -26720,9 +29438,10 @@ static int do_meta_command(char *zLine, ShellState *p){
/* --help lists all file-controls */
if( cli_strcmp(zCmd,"help")==0 ){
- oputz("Available file-controls:\n");
+ sqlite3_fputs("Available file-controls:\n", p->out);
for(i=0; i<ArraySize(aCtrl); i++){
- oputf(" .filectrl %s %s\n", aCtrl[i].zCtrlName, aCtrl[i].zUsage);
+ sqlite3_fprintf(p->out,
+ " .filectrl %s %s\n", aCtrl[i].zCtrlName, aCtrl[i].zUsage);
}
rc = 1;
goto meta_command_exit;
@@ -26737,7 +29456,7 @@ static int do_meta_command(char *zLine, ShellState *p){
filectrl = aCtrl[i].ctrlCode;
iCtrl = i;
}else{
- eputf("Error: ambiguous file-control: \"%s\"\n"
+ sqlite3_fprintf(stderr,"Error: ambiguous file-control: \"%s\"\n"
"Use \".filectrl --help\" for help\n", zCmd);
rc = 1;
goto meta_command_exit;
@@ -26745,7 +29464,7 @@ static int do_meta_command(char *zLine, ShellState *p){
}
}
if( filectrl<0 ){
- eputf("Error: unknown file-control: %s\n"
+ sqlite3_fprintf(stderr,"Error: unknown file-control: %s\n"
"Use \".filectrl --help\" for help\n", zCmd);
}else{
switch(filectrl){
@@ -26789,7 +29508,7 @@ static int do_meta_command(char *zLine, ShellState *p){
if( nArg!=2 ) break;
sqlite3_file_control(p->db, zSchema, filectrl, &z);
if( z ){
- oputf("%s\n", z);
+ sqlite3_fprintf(p->out, "%s\n", z);
sqlite3_free(z);
}
isOk = 2;
@@ -26803,19 +29522,20 @@ static int do_meta_command(char *zLine, ShellState *p){
}
x = -1;
sqlite3_file_control(p->db, zSchema, filectrl, &x);
- oputf("%d\n", x);
+ sqlite3_fprintf(p->out, "%d\n", x);
isOk = 2;
break;
}
}
}
if( isOk==0 && iCtrl>=0 ){
- oputf("Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
+ sqlite3_fprintf(p->out, "Usage: .filectrl %s %s\n",
+ zCmd, aCtrl[iCtrl].zUsage);
rc = 1;
}else if( isOk==1 ){
char zBuf[100];
sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
- oputf("%s\n", zBuf);
+ sqlite3_fprintf(p->out, "%s\n", zBuf);
}
}else
@@ -26856,15 +29576,15 @@ static int do_meta_command(char *zLine, ShellState *p){
}
}
if( doStats==0 ){
- oputz("/* No STAT tables available */\n");
+ sqlite3_fputs("/* No STAT tables available */\n", p->out);
}else{
- oputz("ANALYZE sqlite_schema;\n");
+ sqlite3_fputs("ANALYZE sqlite_schema;\n", p->out);
data.cMode = data.mode = MODE_Insert;
data.zDestTable = "sqlite_stat1";
shell_exec(&data, "SELECT * FROM sqlite_stat1", 0);
data.zDestTable = "sqlite_stat4";
shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
- oputz("ANALYZE sqlite_schema;\n");
+ sqlite3_fputs("ANALYZE sqlite_schema;\n", p->out);
}
}else
@@ -26882,7 +29602,7 @@ static int do_meta_command(char *zLine, ShellState *p){
if( nArg>=2 ){
n = showHelp(p->out, azArg[1]);
if( n==0 ){
- oputf("Nothing matches '%s'\n", azArg[1]);
+ sqlite3_fprintf(p->out, "Nothing matches '%s'\n", azArg[1]);
}
}else{
showHelp(p->out, 0);
@@ -26925,7 +29645,7 @@ static int do_meta_command(char *zLine, ShellState *p){
}else if( zTable==0 ){
zTable = z;
}else{
- oputf("ERROR: extra argument: \"%s\". Usage:\n", z);
+ sqlite3_fprintf(p->out, "ERROR: extra argument: \"%s\". Usage:\n",z);
showHelp(p->out, "import");
goto meta_command_exit;
}
@@ -26946,13 +29666,13 @@ static int do_meta_command(char *zLine, ShellState *p){
xRead = csv_read_one_field;
useOutputMode = 0;
}else{
- oputf("ERROR: unknown option: \"%s\". Usage:\n", z);
+ sqlite3_fprintf(p->out, "ERROR: unknown option: \"%s\". Usage:\n", z);
showHelp(p->out, "import");
goto meta_command_exit;
}
}
if( zTable==0 ){
- oputf("ERROR: missing %s argument. Usage:\n",
+ sqlite3_fprintf(p->out, "ERROR: missing %s argument. Usage:\n",
zFile==0 ? "FILE" : "TABLE");
showHelp(p->out, "import");
goto meta_command_exit;
@@ -27002,28 +29722,28 @@ static int do_meta_command(char *zLine, ShellState *p){
eputz("Error: pipes are not supported in this OS\n");
goto meta_command_exit;
#else
- sCtx.in = popen(sCtx.zFile+1, "r");
+ sCtx.in = sqlite3_popen(sCtx.zFile+1, "r");
sCtx.zFile = "<pipe>";
sCtx.xCloser = pclose;
#endif
}else{
- sCtx.in = fopen(sCtx.zFile, "rb");
+ sCtx.in = sqlite3_fopen(sCtx.zFile, "rb");
sCtx.xCloser = fclose;
}
if( sCtx.in==0 ){
- eputf("Error: cannot open \"%s\"\n", zFile);
+ sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", zFile);
goto meta_command_exit;
}
if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
char zSep[2];
zSep[1] = 0;
zSep[0] = sCtx.cColSep;
- oputz("Column separator ");
- output_c_string(zSep);
- oputz(", row separator ");
+ sqlite3_fputs("Column separator ", p->out);
+ output_c_string(p->out, zSep);
+ sqlite3_fputs(", row separator ", p->out);
zSep[0] = sCtx.cRowSep;
- output_c_string(zSep);
- oputz("\n");
+ output_c_string(p->out, zSep);
+ sqlite3_fputs("\n", p->out);
}
sCtx.z = sqlite3_malloc64(120);
if( sCtx.z==0 ){
@@ -27035,7 +29755,11 @@ static int do_meta_command(char *zLine, ShellState *p){
while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
}
import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */
- if( sqlite3_table_column_metadata(p->db, zSchema, zTable,0,0,0,0,0,0) ){
+ if( sqlite3_table_column_metadata(p->db, zSchema, zTable,0,0,0,0,0,0)
+ && 0==db_int(p->db, "SELECT count(*) FROM \"%w\".sqlite_schema"
+ " WHERE name=%Q AND type='view'",
+ zSchema ? zSchema : "main", zTable)
+ ){
/* Table does not exist. Create it. */
sqlite3 *dbCols = 0;
char *zRenames = 0;
@@ -27048,16 +29772,17 @@ static int do_meta_command(char *zLine, ShellState *p){
}
zColDefs = zAutoColumn(0, &dbCols, &zRenames);
if( zRenames!=0 ){
- sputf((stdin_is_interactive && p->in==stdin)? p->out : stderr,
+ sqlite3_fprintf((stdin_is_interactive && p->in==stdin)? p->out : stderr,
"Columns renamed during .import %s due to duplicates:\n"
"%s\n", sCtx.zFile, zRenames);
sqlite3_free(zRenames);
}
assert(dbCols==0);
if( zColDefs==0 ){
- eputf("%s: empty file\n", sCtx.zFile);
+ sqlite3_fprintf(stderr,"%s: empty file\n", sCtx.zFile);
import_cleanup(&sCtx);
rc = 1;
+ sqlite3_free(zCreate);
goto meta_command_exit;
}
zCreate = sqlite3_mprintf("%z%z\n", zCreate, zColDefs);
@@ -27066,13 +29791,16 @@ static int do_meta_command(char *zLine, ShellState *p){
shell_out_of_memory();
}
if( eVerbose>=1 ){
- oputf("%s\n", zCreate);
+ sqlite3_fprintf(p->out, "%s\n", zCreate);
}
rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
+ if( rc ){
+ sqlite3_fprintf(stderr,
+ "%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db));
+ }
sqlite3_free(zCreate);
zCreate = 0;
if( rc ){
- eputf("%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db));
import_cleanup(&sCtx);
rc = 1;
goto meta_command_exit;
@@ -27089,7 +29817,7 @@ static int do_meta_command(char *zLine, ShellState *p){
zSql = 0;
if( rc ){
if (pStmt) sqlite3_finalize(pStmt);
- eputf("Error: %s\n", sqlite3_errmsg(p->db));
+ shellDatabaseError(p->db);
import_cleanup(&sCtx);
rc = 1;
goto meta_command_exit;
@@ -27127,13 +29855,13 @@ static int do_meta_command(char *zLine, ShellState *p){
zSql[j] = 0;
assert( j<nByte );
if( eVerbose>=2 ){
- oputf("Insert using: %s\n", zSql);
+ sqlite3_fprintf(p->out, "Insert using: %s\n", zSql);
}
rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
sqlite3_free(zSql);
zSql = 0;
if( rc ){
- eputf("Error: %s\n", sqlite3_errmsg(p->db));
+ shellDatabaseError(p->db);
if (pStmt) sqlite3_finalize(pStmt);
import_cleanup(&sCtx);
rc = 1;
@@ -27166,7 +29894,7 @@ static int do_meta_command(char *zLine, ShellState *p){
}
sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
- eputf("%s:%d: expected %d columns but found %d"
+ sqlite3_fprintf(stderr,"%s:%d: expected %d columns but found %d"
" - filling the rest with NULL\n",
sCtx.zFile, startLine, nCol, i+1);
i += 2;
@@ -27178,14 +29906,15 @@ static int do_meta_command(char *zLine, ShellState *p){
xRead(&sCtx);
i++;
}while( sCtx.cTerm==sCtx.cColSep );
- eputf("%s:%d: expected %d columns but found %d - extras ignored\n",
+ sqlite3_fprintf(stderr,
+ "%s:%d: expected %d columns but found %d - extras ignored\n",
sCtx.zFile, startLine, nCol, i);
}
if( i>=nCol ){
sqlite3_step(pStmt);
rc = sqlite3_reset(pStmt);
if( rc!=SQLITE_OK ){
- eputf("%s:%d: INSERT failed: %s\n",
+ sqlite3_fprintf(stderr,"%s:%d: INSERT failed: %s\n",
sCtx.zFile, startLine, sqlite3_errmsg(p->db));
sCtx.nErr++;
}else{
@@ -27198,7 +29927,8 @@ static int do_meta_command(char *zLine, ShellState *p){
sqlite3_finalize(pStmt);
if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
if( eVerbose>0 ){
- oputf("Added %d rows with %d errors using %d lines of input\n",
+ sqlite3_fprintf(p->out,
+ "Added %d rows with %d errors using %d lines of input\n",
sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
}
}else
@@ -27214,7 +29944,7 @@ static int do_meta_command(char *zLine, ShellState *p){
int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
int i;
if( !ShellHasFlag(p,SHFLG_TestingMode) ){
- eputf(".%s unavailable without --unsafe-testing\n",
+ sqlite3_fprintf(stderr,".%s unavailable without --unsafe-testing\n",
"imposter");
rc = 1;
goto meta_command_exit;
@@ -27280,7 +30010,7 @@ static int do_meta_command(char *zLine, ShellState *p){
}
sqlite3_finalize(pStmt);
if( i==0 || tnum==0 ){
- eputf("no such index: \"%s\"\n", azArg[1]);
+ sqlite3_fprintf(stderr,"no such index: \"%s\"\n", azArg[1]);
rc = 1;
sqlite3_free(zCollist);
goto meta_command_exit;
@@ -27295,14 +30025,16 @@ static int do_meta_command(char *zLine, ShellState *p){
rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
if( rc ){
- eputf("Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
+ sqlite3_fprintf(stderr,
+ "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
}else{
- sputf(stdout, "%s;\n", zSql);
- sputf(stdout, "WARNING: writing to an imposter table will corrupt"
+ sqlite3_fprintf(stdout, "%s;\n", zSql);
+ sqlite3_fprintf(stdout,
+ "WARNING: writing to an imposter table will corrupt"
" the \"%s\" %s!\n", azArg[1], isWO ? "table" : "index");
}
}else{
- eputf("SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
+ sqlite3_fprintf(stderr,"SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
rc = 1;
}
sqlite3_free(zSql);
@@ -27316,7 +30048,7 @@ static int do_meta_command(char *zLine, ShellState *p){
if( iArg==0 ) iArg = -1;
}
if( (nArg!=1 && nArg!=2) || iArg<0 ){
- eputf("%s","Usage: .intck STEPS_PER_UNLOCK\n");
+ sqlite3_fprintf(stderr,"%s","Usage: .intck STEPS_PER_UNLOCK\n");
rc = 1;
goto meta_command_exit;
}
@@ -27335,9 +30067,9 @@ static int do_meta_command(char *zLine, ShellState *p){
sqlite3IoTrace = iotracePrintf;
iotrace = stdout;
}else{
- iotrace = fopen(azArg[1], "w");
+ iotrace = sqlite3_fopen(azArg[1], "w");
if( iotrace==0 ){
- eputf("Error: cannot open \"%s\"\n", azArg[1]);
+ sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
sqlite3IoTrace = 0;
rc = 1;
}else{
@@ -27369,7 +30101,7 @@ static int do_meta_command(char *zLine, ShellState *p){
open_db(p, 0);
if( nArg==1 ){
for(i=0; i<ArraySize(aLimit); i++){
- sputf(stdout, "%20s %d\n", aLimit[i].zLimitName,
+ sqlite3_fprintf(stdout, "%20s %d\n", aLimit[i].zLimitName,
sqlite3_limit(p->db, aLimit[i].limitCode, -1));
}
}else if( nArg>3 ){
@@ -27384,14 +30116,14 @@ static int do_meta_command(char *zLine, ShellState *p){
if( iLimit<0 ){
iLimit = i;
}else{
- eputf("ambiguous limit: \"%s\"\n", azArg[1]);
+ sqlite3_fprintf(stderr,"ambiguous limit: \"%s\"\n", azArg[1]);
rc = 1;
goto meta_command_exit;
}
}
}
if( iLimit<0 ){
- eputf("unknown limit: \"%s\"\n"
+ sqlite3_fprintf(stderr,"unknown limit: \"%s\"\n"
"enter \".limits\" with no arguments for a list.\n",
azArg[1]);
rc = 1;
@@ -27401,7 +30133,7 @@ static int do_meta_command(char *zLine, ShellState *p){
sqlite3_limit(p->db, aLimit[iLimit].limitCode,
(int)integerValue(azArg[2]));
}
- sputf(stdout, "%20s %d\n", aLimit[iLimit].zLimitName,
+ sqlite3_fprintf(stdout, "%20s %d\n", aLimit[iLimit].zLimitName,
sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
}
}else
@@ -27427,7 +30159,7 @@ static int do_meta_command(char *zLine, ShellState *p){
open_db(p, 0);
rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
if( rc!=SQLITE_OK ){
- eputf("Error: %s\n", zErrMsg);
+ shellEmitError(zErrMsg);
sqlite3_free(zErrMsg);
rc = 1;
}
@@ -27450,7 +30182,7 @@ static int do_meta_command(char *zLine, ShellState *p){
}
output_file_close(p->pLog);
if( cli_strcmp(zFile,"on")==0 ) zFile = "stdout";
- p->pLog = output_file_open(zFile, 0);
+ p->pLog = output_file_open(zFile);
}
}else
@@ -27458,24 +30190,52 @@ static int do_meta_command(char *zLine, ShellState *p){
const char *zMode = 0;
const char *zTabname = 0;
int i, n2;
+ int chng = 0; /* 0x01: change to cmopts. 0x02: Any other change */
ColModeOpts cmOpts = ColModeOpts_default;
for(i=1; i<nArg; i++){
const char *z = azArg[i];
if( optionMatch(z,"wrap") && i+1<nArg ){
cmOpts.iWrap = integerValue(azArg[++i]);
+ chng |= 1;
}else if( optionMatch(z,"ww") ){
cmOpts.bWordWrap = 1;
+ chng |= 1;
}else if( optionMatch(z,"wordwrap") && i+1<nArg ){
cmOpts.bWordWrap = (u8)booleanValue(azArg[++i]);
+ chng |= 1;
}else if( optionMatch(z,"quote") ){
cmOpts.bQuote = 1;
+ chng |= 1;
}else if( optionMatch(z,"noquote") ){
cmOpts.bQuote = 0;
+ chng |= 1;
+ }else if( optionMatch(z,"escape") && i+1<nArg ){
+ /* See similar code at tag-20250224-1 */
+ const char *zEsc = azArg[++i];
+ int k;
+ for(k=0; k<ArraySize(shell_EscModeNames); k++){
+ if( sqlite3_stricmp(zEsc,shell_EscModeNames[k])==0 ){
+ p->eEscMode = k;
+ chng |= 2;
+ break;
+ }
+ }
+ if( k>=ArraySize(shell_EscModeNames) ){
+ sqlite3_fprintf(stderr, "unknown control character escape mode \"%s\""
+ " - choices:", zEsc);
+ for(k=0; k<ArraySize(shell_EscModeNames); k++){
+ sqlite3_fprintf(stderr, " %s", shell_EscModeNames[k]);
+ }
+ sqlite3_fprintf(stderr, "\n");
+ rc = 1;
+ goto meta_command_exit;
+ }
}else if( zMode==0 ){
zMode = z;
/* Apply defaults for qbox pseudo-mode. If that
* overwrites already-set values, user was informed of this.
*/
+ chng |= 1;
if( cli_strcmp(z, "qbox")==0 ){
ColModeOpts cmo = ColModeOpts_default_qbox;
zMode = "box";
@@ -27484,8 +30244,9 @@ static int do_meta_command(char *zLine, ShellState *p){
}else if( zTabname==0 ){
zTabname = z;
}else if( z[0]=='-' ){
- eputf("unknown option: %s\n", z);
+ sqlite3_fprintf(stderr,"unknown option: %s\n", z);
eputz("options:\n"
+ " --escape MODE\n"
" --noquote\n"
" --quote\n"
" --wordwrap on/off\n"
@@ -27494,23 +30255,34 @@ static int do_meta_command(char *zLine, ShellState *p){
rc = 1;
goto meta_command_exit;
}else{
- eputf("extra argument: \"%s\"\n", z);
+ sqlite3_fprintf(stderr,"extra argument: \"%s\"\n", z);
rc = 1;
goto meta_command_exit;
}
}
- if( zMode==0 ){
+ if( !chng ){
if( p->mode==MODE_Column
|| (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
){
- oputf("current output mode: %s --wrap %d --wordwrap %s --%squote\n",
+ sqlite3_fprintf(p->out,
+ "current output mode: %s --wrap %d --wordwrap %s "
+ "--%squote --escape %s\n",
modeDescr[p->mode], p->cmOpts.iWrap,
p->cmOpts.bWordWrap ? "on" : "off",
- p->cmOpts.bQuote ? "" : "no");
+ p->cmOpts.bQuote ? "" : "no",
+ shell_EscModeNames[p->eEscMode]
+ );
}else{
- oputf("current output mode: %s\n", modeDescr[p->mode]);
+ sqlite3_fprintf(p->out,
+ "current output mode: %s --escape %s\n",
+ modeDescr[p->mode],
+ shell_EscModeNames[p->eEscMode]
+ );
}
+ }
+ if( zMode==0 ){
zMode = modeDescr[p->mode];
+ if( (chng&1)==0 ) cmOpts = p->cmOpts;
}
n2 = strlen30(zMode);
if( cli_strncmp(zMode,"lines",n2)==0 ){
@@ -27543,6 +30315,11 @@ static int do_meta_command(char *zLine, ShellState *p){
}else if( cli_strncmp(zMode,"insert",n2)==0 ){
p->mode = MODE_Insert;
set_table_name(p, zTabname ? zTabname : "table");
+ if( p->eEscMode==SHELL_ESC_OFF ){
+ ShellSetFlag(p, SHFLG_Newlines);
+ }else{
+ ShellClearFlag(p, SHFLG_Newlines);
+ }
}else if( cli_strncmp(zMode,"quote",n2)==0 ){
p->mode = MODE_Quote;
sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
@@ -27581,7 +30358,7 @@ static int do_meta_command(char *zLine, ShellState *p){
eputz("Usage: .nonce NONCE\n");
rc = 1;
}else if( p->zNonce==0 || cli_strcmp(azArg[1],p->zNonce)!=0 ){
- eputf("line %d: incorrect nonce: \"%s\"\n",
+ sqlite3_fprintf(stderr,"line %d: incorrect nonce: \"%s\"\n",
p->lineno, azArg[1]);
exit(1);
}else{
@@ -27636,11 +30413,11 @@ static int do_meta_command(char *zLine, ShellState *p){
}else
#endif /* !SQLITE_SHELL_FIDDLE */
if( z[0]=='-' ){
- eputf("unknown option: %s\n", z);
+ sqlite3_fprintf(stderr,"unknown option: %s\n", z);
rc = 1;
goto meta_command_exit;
}else if( zFN ){
- eputf("extra argument: \"%s\"\n", z);
+ sqlite3_fprintf(stderr,"extra argument: \"%s\"\n", z);
rc = 1;
goto meta_command_exit;
}else{
@@ -27682,7 +30459,7 @@ static int do_meta_command(char *zLine, ShellState *p){
p->pAuxDb->zDbFilename = zNewFilename;
open_db(p, OPEN_DB_KEEPALIVE);
if( p->db==0 ){
- eputf("Error: cannot open '%s'\n", zNewFilename);
+ sqlite3_fprintf(stderr,"Error: cannot open '%s'\n", zNewFilename);
sqlite3_free(zNewFilename);
}else{
p->pAuxDb->zFreeOnClose = zNewFilename;
@@ -27700,19 +30477,23 @@ static int do_meta_command(char *zLine, ShellState *p){
&& (cli_strncmp(azArg[0], "output", n)==0
|| cli_strncmp(azArg[0], "once", n)==0))
|| (c=='e' && n==5 && cli_strcmp(azArg[0],"excel")==0)
+ || (c=='w' && n==3 && cli_strcmp(azArg[0],"www")==0)
){
char *zFile = 0;
- int bTxtMode = 0;
int i;
- int eMode = 0;
- int bOnce = 0; /* 0: .output, 1: .once, 2: .excel */
- static const char *zBomUtf8 = "\xef\xbb\xbf";
+ int eMode = 0; /* 0: .outout/.once, 'x'=.excel, 'w'=.www */
+ int bOnce = 0; /* 0: .output, 1: .once, 2: .excel/.www */
+ int bPlain = 0; /* --plain option */
+ static const char *zBomUtf8 = "\357\273\277";
const char *zBom = 0;
failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
if( c=='e' ){
eMode = 'x';
bOnce = 2;
+ }else if( c=='w' ){
+ eMode = 'w';
+ bOnce = 2;
}else if( cli_strncmp(azArg[0],"once",n)==0 ){
bOnce = 1;
}
@@ -27722,24 +30503,38 @@ static int do_meta_command(char *zLine, ShellState *p){
if( z[1]=='-' ) z++;
if( cli_strcmp(z,"-bom")==0 ){
zBom = zBomUtf8;
- }else if( c!='e' && cli_strcmp(z,"-x")==0 ){
+ }else if( cli_strcmp(z,"-plain")==0 ){
+ bPlain = 1;
+ }else if( c=='o' && cli_strcmp(z,"-x")==0 ){
eMode = 'x'; /* spreadsheet */
- }else if( c!='e' && cli_strcmp(z,"-e")==0 ){
+ }else if( c=='o' && cli_strcmp(z,"-e")==0 ){
eMode = 'e'; /* text editor */
+ }else if( c=='o' && cli_strcmp(z,"-w")==0 ){
+ eMode = 'w'; /* Web browser */
}else{
- oputf("ERROR: unknown option: \"%s\". Usage:\n", azArg[i]);
+ sqlite3_fprintf(p->out,
+ "ERROR: unknown option: \"%s\". Usage:\n", azArg[i]);
showHelp(p->out, azArg[0]);
rc = 1;
goto meta_command_exit;
}
- }else if( zFile==0 && eMode!='e' && eMode!='x' ){
- zFile = sqlite3_mprintf("%s", z);
+ }else if( zFile==0 && eMode==0 ){
+ if( cli_strcmp(z, "off")==0 ){
+#ifdef _WIN32
+ zFile = sqlite3_mprintf("nul");
+#else
+ zFile = sqlite3_mprintf("/dev/null");
+#endif
+ }else{
+ zFile = sqlite3_mprintf("%s", z);
+ }
if( zFile && zFile[0]=='|' ){
while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
break;
}
}else{
- oputf("ERROR: extra parameter: \"%s\". Usage:\n", azArg[i]);
+ sqlite3_fprintf(p->out,
+ "ERROR: extra parameter: \"%s\". Usage:\n", azArg[i]);
showHelp(p->out, azArg[0]);
rc = 1;
sqlite3_free(zFile);
@@ -27749,6 +30544,7 @@ static int do_meta_command(char *zLine, ShellState *p){
if( zFile==0 ){
zFile = sqlite3_mprintf("stdout");
}
+ shell_check_oom(zFile);
if( bOnce ){
p->outCount = 2;
}else{
@@ -27756,7 +30552,7 @@ static int do_meta_command(char *zLine, ShellState *p){
}
output_reset(p);
#ifndef SQLITE_NOHAVE_SYSTEM
- if( eMode=='e' || eMode=='x' ){
+ if( eMode=='e' || eMode=='x' || eMode=='w' ){
p->doXdgOpen = 1;
outputModePush(p);
if( eMode=='x' ){
@@ -27766,10 +30562,17 @@ static int do_meta_command(char *zLine, ShellState *p){
p->mode = MODE_Csv;
sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
+#ifdef _WIN32
+ zBom = zBomUtf8; /* Always include the BOM on Windows, as Excel does
+ ** not work without it. */
+#endif
+ }else if( eMode=='w' ){
+ /* web-browser mode. */
+ newTempFile(p, "html");
+ if( !bPlain ) p->mode = MODE_Www;
}else{
/* text editor mode */
newTempFile(p, "txt");
- bTxtMode = 1;
}
sqlite3_free(zFile);
zFile = sqlite3_mprintf("%s", p->zTempFile);
@@ -27782,26 +30585,34 @@ static int do_meta_command(char *zLine, ShellState *p){
rc = 1;
output_redir(p, stdout);
#else
- FILE *pfPipe = popen(zFile + 1, "w");
+ FILE *pfPipe = sqlite3_popen(zFile + 1, "w");
if( pfPipe==0 ){
- eputf("Error: cannot open pipe \"%s\"\n", zFile + 1);
+ assert( stderr!=NULL );
+ sqlite3_fprintf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
rc = 1;
}else{
output_redir(p, pfPipe);
- if( zBom ) oputz(zBom);
+ if( zBom ) sqlite3_fputs(zBom, pfPipe);
sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
}
#endif
}else{
- FILE *pfFile = output_file_open(zFile, bTxtMode);
+ FILE *pfFile = output_file_open(zFile);
if( pfFile==0 ){
if( cli_strcmp(zFile,"off")!=0 ){
- eputf("Error: cannot write to \"%s\"\n", zFile);
+ assert( stderr!=NULL );
+ sqlite3_fprintf(stderr,"Error: cannot write to \"%s\"\n", zFile);
}
rc = 1;
} else {
output_redir(p, pfFile);
- if( zBom ) oputz(zBom);
+ if( zBom ) sqlite3_fputs(zBom, pfFile);
+ if( bPlain && eMode=='w' ){
+ sqlite3_fputs(
+ "<!DOCTYPE html>\n<BODY>\n<PLAINTEXT>\n",
+ pfFile
+ );
+ }
sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
}
}
@@ -27842,7 +30653,8 @@ static int do_meta_command(char *zLine, ShellState *p){
"SELECT key, quote(value) "
"FROM temp.sqlite_parameters;", -1, &pStmt, 0);
while( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
- oputf("%-*s %s\n", len, sqlite3_column_text(pStmt,0),
+ sqlite3_fprintf(p->out,
+ "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
sqlite3_column_text(pStmt,1));
}
sqlite3_finalize(pStmt);
@@ -27887,12 +30699,13 @@ static int do_meta_command(char *zLine, ShellState *p){
rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
sqlite3_free(zSql);
if( rx!=SQLITE_OK ){
- oputf("Error: %s\n", sqlite3_errmsg(p->db));
+ sqlite3_fprintf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
sqlite3_finalize(pStmt);
pStmt = 0;
rc = 1;
}
}
+ bind_prepared_stmt(p, pStmt);
sqlite3_step(pStmt);
sqlite3_finalize(pStmt);
}else
@@ -27916,10 +30729,10 @@ static int do_meta_command(char *zLine, ShellState *p){
if( c=='p' && n>=3 && cli_strncmp(azArg[0], "print", n)==0 ){
int i;
for(i=1; i<nArg; i++){
- if( i>1 ) oputz(" ");
- oputz(azArg[i]);
+ if( i>1 ) sqlite3_fputs(" ", p->out);
+ sqlite3_fputs(azArg[i], p->out);
}
- oputz("\n");
+ sqlite3_fputs("\n", p->out);
}else
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
@@ -27956,7 +30769,7 @@ static int do_meta_command(char *zLine, ShellState *p){
}
continue;
}
- eputf("Error: unknown option: \"%s\"\n", azArg[i]);
+ sqlite3_fprintf(stderr,"Error: unknown option: \"%s\"\n", azArg[i]);
rc = 1;
goto meta_command_exit;
}else{
@@ -27997,11 +30810,10 @@ static int do_meta_command(char *zLine, ShellState *p){
#ifdef SQLITE_OMIT_POPEN
eputz("Error: pipes are not supported in this OS\n");
rc = 1;
- p->out = stdout;
#else
- p->in = popen(azArg[1]+1, "r");
+ p->in = sqlite3_popen(azArg[1]+1, "r");
if( p->in==0 ){
- eputf("Error: cannot open \"%s\"\n", azArg[1]);
+ sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
rc = 1;
}else{
rc = process_input(p);
@@ -28009,7 +30821,7 @@ static int do_meta_command(char *zLine, ShellState *p){
}
#endif
}else if( (p->in = openChrSource(azArg[1]))==0 ){
- eputf("Error: cannot open \"%s\"\n", azArg[1]);
+ sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
rc = 1;
}else{
rc = process_input(p);
@@ -28042,14 +30854,14 @@ static int do_meta_command(char *zLine, ShellState *p){
}
rc = sqlite3_open(zSrcFile, &pSrc);
if( rc!=SQLITE_OK ){
- eputf("Error: cannot open \"%s\"\n", zSrcFile);
+ sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", zSrcFile);
close_db(pSrc);
return 1;
}
open_db(p, 0);
pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
if( pBackup==0 ){
- eputf("Error: %s\n", sqlite3_errmsg(p->db));
+ shellDatabaseError(p->db);
close_db(pSrc);
return 1;
}
@@ -28067,14 +30879,17 @@ static int do_meta_command(char *zLine, ShellState *p){
eputz("Error: source database is busy\n");
rc = 1;
}else{
- eputf("Error: %s\n", sqlite3_errmsg(p->db));
+ shellDatabaseError(p->db);
rc = 1;
}
close_db(pSrc);
}else
#endif /* !defined(SQLITE_SHELL_FIDDLE) */
- if( c=='s' && cli_strncmp(azArg[0], "scanstats", n)==0 ){
+ if( c=='s' &&
+ (cli_strncmp(azArg[0], "scanstats", n)==0 ||
+ cli_strncmp(azArg[0], "scanstatus", n)==0)
+ ){
if( nArg==2 ){
if( cli_strcmp(azArg[1], "vm")==0 ){
p->scanstatsOn = 3;
@@ -28125,7 +30940,7 @@ static int do_meta_command(char *zLine, ShellState *p){
}else if( optionMatch(azArg[ii],"nosys") ){
bNoSystemTabs = 1;
}else if( azArg[ii][0]=='-' ){
- eputf("Unknown option: \"%s\"\n", azArg[ii]);
+ sqlite3_fprintf(stderr,"Unknown option: \"%s\"\n", azArg[ii]);
rc = 1;
goto meta_command_exit;
}else if( zName==0 ){
@@ -28164,7 +30979,7 @@ static int do_meta_command(char *zLine, ShellState *p){
rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
-1, &pStmt, 0);
if( rc ){
- eputf("Error: %s\n", sqlite3_errmsg(p->db));
+ shellDatabaseError(p->db);
sqlite3_finalize(pStmt);
rc = 1;
goto meta_command_exit;
@@ -28226,14 +31041,14 @@ static int do_meta_command(char *zLine, ShellState *p){
appendText(&sSelect, "sql IS NOT NULL"
" ORDER BY snum, rowid", 0);
if( bDebug ){
- oputf("SQL: %s;\n", sSelect.z);
+ sqlite3_fprintf(p->out, "SQL: %s;\n", sSelect.z);
}else{
rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
}
freeText(&sSelect);
}
if( zErrMsg ){
- eputf("Error: %s\n", zErrMsg);
+ shellEmitError(zErrMsg);
sqlite3_free(zErrMsg);
rc = 1;
}else if( rc != SQLITE_OK ){
@@ -28287,7 +31102,8 @@ static int do_meta_command(char *zLine, ShellState *p){
}else{
rc = sqlite3session_attach(pSession->p, azCmd[1]);
if( rc ){
- eputf("ERROR: sqlite3session_attach() returns %d\n",rc);
+ sqlite3_fprintf(stderr,
+ "ERROR: sqlite3session_attach() returns %d\n",rc);
rc = 0;
}
}
@@ -28304,9 +31120,9 @@ static int do_meta_command(char *zLine, ShellState *p){
failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]);
if( nCmd!=2 ) goto session_syntax_error;
if( pSession->p==0 ) goto session_not_open;
- out = fopen(azCmd[1], "wb");
+ out = sqlite3_fopen(azCmd[1], "wb");
if( out==0 ){
- eputf("ERROR: cannot open \"%s\" for writing\n",
+ sqlite3_fprintf(stderr,"ERROR: cannot open \"%s\" for writing\n",
azCmd[1]);
}else{
int szChng;
@@ -28317,12 +31133,13 @@ static int do_meta_command(char *zLine, ShellState *p){
rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
}
if( rc ){
- sputf(stdout, "Error: error code %d\n", rc);
+ sqlite3_fprintf(stdout, "Error: error code %d\n", rc);
rc = 0;
}
if( pChng
&& fwrite(pChng, szChng, 1, out)!=1 ){
- eputf("ERROR: Failed to write entire %d-byte output\n", szChng);
+ sqlite3_fprintf(stderr,
+ "ERROR: Failed to write entire %d-byte output\n", szChng);
}
sqlite3_free(pChng);
fclose(out);
@@ -28349,7 +31166,8 @@ static int do_meta_command(char *zLine, ShellState *p){
ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
if( pAuxDb->nSession ){
ii = sqlite3session_enable(pSession->p, ii);
- oputf("session %s enable flag = %d\n", pSession->zName, ii);
+ sqlite3_fprintf(p->out,
+ "session %s enable flag = %d\n", pSession->zName, ii);
}
}else
@@ -28384,7 +31202,8 @@ static int do_meta_command(char *zLine, ShellState *p){
ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
if( pAuxDb->nSession ){
ii = sqlite3session_indirect(pSession->p, ii);
- oputf("session %s indirect flag = %d\n", pSession->zName, ii);
+ sqlite3_fprintf(p->out,
+ "session %s indirect flag = %d\n", pSession->zName, ii);
}
}else
@@ -28396,7 +31215,8 @@ static int do_meta_command(char *zLine, ShellState *p){
if( nCmd!=1 ) goto session_syntax_error;
if( pAuxDb->nSession ){
ii = sqlite3session_isempty(pSession->p);
- oputf("session %s isempty flag = %d\n", pSession->zName, ii);
+ sqlite3_fprintf(p->out,
+ "session %s isempty flag = %d\n", pSession->zName, ii);
}
}else
@@ -28405,7 +31225,7 @@ static int do_meta_command(char *zLine, ShellState *p){
*/
if( cli_strcmp(azCmd[0],"list")==0 ){
for(i=0; i<pAuxDb->nSession; i++){
- oputf("%d %s\n", i, pAuxDb->aSession[i].zName);
+ sqlite3_fprintf(p->out, "%d %s\n", i, pAuxDb->aSession[i].zName);
}
}else
@@ -28420,18 +31240,19 @@ static int do_meta_command(char *zLine, ShellState *p){
if( zName[0]==0 ) goto session_syntax_error;
for(i=0; i<pAuxDb->nSession; i++){
if( cli_strcmp(pAuxDb->aSession[i].zName,zName)==0 ){
- eputf("Session \"%s\" already exists\n", zName);
+ sqlite3_fprintf(stderr,"Session \"%s\" already exists\n", zName);
goto meta_command_exit;
}
}
if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){
- eputf("Maximum of %d sessions\n", ArraySize(pAuxDb->aSession));
+ sqlite3_fprintf(stderr,
+ "Maximum of %d sessions\n", ArraySize(pAuxDb->aSession));
goto meta_command_exit;
}
pSession = &pAuxDb->aSession[pAuxDb->nSession];
rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
if( rc ){
- eputf("Cannot open session: error code=%d\n", rc);
+ sqlite3_fprintf(stderr,"Cannot open session: error code=%d\n", rc);
rc = 0;
goto meta_command_exit;
}
@@ -28455,7 +31276,7 @@ static int do_meta_command(char *zLine, ShellState *p){
int i, v;
for(i=1; i<nArg; i++){
v = booleanValue(azArg[i]);
- oputf("%s: %d 0x%x\n", azArg[i], v, v);
+ sqlite3_fprintf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
}
}
if( cli_strncmp(azArg[0]+9, "integer", n-9)==0 ){
@@ -28464,7 +31285,7 @@ static int do_meta_command(char *zLine, ShellState *p){
char zBuf[200];
v = integerValue(azArg[i]);
sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
- oputz(zBuf);
+ sqlite3_fputs(zBuf, p->out);
}
}
}else
@@ -28491,8 +31312,9 @@ static int do_meta_command(char *zLine, ShellState *p){
bVerbose++;
}else
{
- eputf("Unknown option \"%s\" on \"%s\"\n", azArg[i], azArg[0]);
- eputz("Should be one of: --init -v\n");
+ sqlite3_fprintf(stderr,
+ "Unknown option \"%s\" on \"%s\"\n", azArg[i], azArg[0]);
+ sqlite3_fputs("Should be one of: --init -v\n", stderr);
rc = 1;
goto meta_command_exit;
}
@@ -28537,10 +31359,10 @@ static int do_meta_command(char *zLine, ShellState *p){
if( zAns==0 ) continue;
k = 0;
if( bVerbose>0 ){
- sputf(stdout, "%d: %s %s\n", tno, zOp, zSql);
+ sqlite3_fprintf(stdout, "%d: %s %s\n", tno, zOp, zSql);
}
if( cli_strcmp(zOp,"memo")==0 ){
- oputf("%s\n", zSql);
+ sqlite3_fprintf(p->out, "%s\n", zSql);
}else
if( cli_strcmp(zOp,"run")==0 ){
char *zErrMsg = 0;
@@ -28549,22 +31371,23 @@ static int do_meta_command(char *zLine, ShellState *p){
rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
nTest++;
if( bVerbose ){
- oputf("Result: %s\n", str.z);
+ sqlite3_fprintf(p->out, "Result: %s\n", str.z);
}
if( rc || zErrMsg ){
nErr++;
rc = 1;
- oputf("%d: error-code-%d: %s\n", tno, rc, zErrMsg);
+ sqlite3_fprintf(p->out, "%d: error-code-%d: %s\n", tno, rc,zErrMsg);
sqlite3_free(zErrMsg);
}else if( cli_strcmp(zAns,str.z)!=0 ){
nErr++;
rc = 1;
- oputf("%d: Expected: [%s]\n", tno, zAns);
- oputf("%d: Got: [%s]\n", tno, str.z);
+ sqlite3_fprintf(p->out, "%d: Expected: [%s]\n", tno, zAns);
+ sqlite3_fprintf(p->out, "%d: Got: [%s]\n", tno, str.z);
}
}
else{
- eputf("Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
+ sqlite3_fprintf(stderr,
+ "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
rc = 1;
break;
}
@@ -28572,7 +31395,7 @@ static int do_meta_command(char *zLine, ShellState *p){
sqlite3_finalize(pStmt);
} /* End loop over k */
freeText(&str);
- oputf("%d errors out of %d tests\n", nErr, nTest);
+ sqlite3_fprintf(p->out, "%d errors out of %d tests\n", nErr, nTest);
}else
if( c=='s' && cli_strncmp(azArg[0], "separator", n)==0 ){
@@ -28620,7 +31443,8 @@ static int do_meta_command(char *zLine, ShellState *p){
bDebug = 1;
}else
{
- eputf("Unknown option \"%s\" on \"%s\"\n", azArg[i], azArg[0]);
+ sqlite3_fprintf(stderr,
+ "Unknown option \"%s\" on \"%s\"\n", azArg[i], azArg[0]);
showHelp(p->out, azArg[0]);
rc = 1;
goto meta_command_exit;
@@ -28698,7 +31522,7 @@ static int do_meta_command(char *zLine, ShellState *p){
freeText(&sQuery);
freeText(&sSql);
if( bDebug ){
- oputf("%s\n", zSql);
+ sqlite3_fprintf(p->out, "%s\n", zSql);
}else{
shell_exec(p, zSql, 0);
}
@@ -28728,7 +31552,7 @@ static int do_meta_command(char *zLine, ShellState *p){
"' OR ') as query, tname from tabcols group by tname)"
, zRevText);
shell_check_oom(zRevText);
- if( bDebug ) oputf("%s\n", zRevText);
+ if( bDebug ) sqlite3_fprintf(p->out, "%s\n", zRevText);
lrc = sqlite3_prepare_v2(p->db, zRevText, -1, &pStmt, 0);
if( lrc!=SQLITE_OK ){
/* assert(lrc==SQLITE_NOMEM); // might also be SQLITE_ERROR if the
@@ -28741,7 +31565,7 @@ static int do_meta_command(char *zLine, ShellState *p){
const char *zGenQuery = (char*)sqlite3_column_text(pStmt,0);
sqlite3_stmt *pCheckStmt;
lrc = sqlite3_prepare_v2(p->db, zGenQuery, -1, &pCheckStmt, 0);
- if( bDebug ) oputf("%s\n", zGenQuery);
+ if( bDebug ) sqlite3_fprintf(p->out, "%s\n", zGenQuery);
if( lrc!=SQLITE_OK ){
rc = 1;
}else{
@@ -28749,7 +31573,8 @@ static int do_meta_command(char *zLine, ShellState *p){
double countIrreversible = sqlite3_column_double(pCheckStmt, 0);
if( countIrreversible>0 ){
int sz = (int)(countIrreversible + 0.5);
- eputf("Digest includes %d invalidly encoded text field%s.\n",
+ sqlite3_fprintf(stderr,
+ "Digest includes %d invalidly encoded text field%s.\n",
sz, (sz>1)? "s": "");
}
}
@@ -28783,11 +31608,11 @@ static int do_meta_command(char *zLine, ShellState *p){
zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
zCmd, azArg[i]);
}
- consoleRestore();
+ /*consoleRestore();*/
x = zCmd!=0 ? system(zCmd) : 1;
- consoleRenewSetup();
+ /*consoleRenewSetup();*/
sqlite3_free(zCmd);
- if( x ) eputf("System command returns %d\n", x);
+ if( x ) sqlite3_fprintf(stderr,"System command returns %d\n", x);
}else
#endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) */
@@ -28800,46 +31625,48 @@ static int do_meta_command(char *zLine, ShellState *p){
rc = 1;
goto meta_command_exit;
}
- oputf("%12.12s: %s\n","echo",
+ sqlite3_fprintf(p->out, "%12.12s: %s\n","echo",
azBool[ShellHasFlag(p, SHFLG_Echo)]);
- oputf("%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
- oputf("%12.12s: %s\n","explain",
+ sqlite3_fprintf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
+ sqlite3_fprintf(p->out, "%12.12s: %s\n","explain",
p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
- oputf("%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
+ sqlite3_fprintf(p->out, "%12.12s: %s\n","headers",
+ azBool[p->showHeader!=0]);
if( p->mode==MODE_Column
|| (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
){
- oputf("%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode",
+ sqlite3_fprintf(p->out,
+ "%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode",
modeDescr[p->mode], p->cmOpts.iWrap,
p->cmOpts.bWordWrap ? "on" : "off",
p->cmOpts.bQuote ? "" : "no");
}else{
- oputf("%12.12s: %s\n","mode", modeDescr[p->mode]);
+ sqlite3_fprintf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
}
- oputf("%12.12s: ", "nullvalue");
- output_c_string(p->nullValue);
- oputz("\n");
- oputf("%12.12s: %s\n","output",
+ sqlite3_fprintf(p->out, "%12.12s: ", "nullvalue");
+ output_c_string(p->out, p->nullValue);
+ sqlite3_fputs("\n", p->out);
+ sqlite3_fprintf(p->out, "%12.12s: %s\n","output",
strlen30(p->outfile) ? p->outfile : "stdout");
- oputf("%12.12s: ", "colseparator");
- output_c_string(p->colSeparator);
- oputz("\n");
- oputf("%12.12s: ", "rowseparator");
- output_c_string(p->rowSeparator);
- oputz("\n");
+ sqlite3_fprintf(p->out, "%12.12s: ", "colseparator");
+ output_c_string(p->out, p->colSeparator);
+ sqlite3_fputs("\n", p->out);
+ sqlite3_fprintf(p->out, "%12.12s: ", "rowseparator");
+ output_c_string(p->out, p->rowSeparator);
+ sqlite3_fputs("\n", p->out);
switch( p->statsOn ){
case 0: zOut = "off"; break;
default: zOut = "on"; break;
case 2: zOut = "stmt"; break;
case 3: zOut = "vmstep"; break;
}
- oputf("%12.12s: %s\n","stats", zOut);
- oputf("%12.12s: ", "width");
+ sqlite3_fprintf(p->out, "%12.12s: %s\n","stats", zOut);
+ sqlite3_fprintf(p->out, "%12.12s: ", "width");
for (i=0;i<p->nWidth;i++) {
- oputf("%d ", p->colWidth[i]);
+ sqlite3_fprintf(p->out, "%d ", p->colWidth[i]);
}
- oputz("\n");
- oputf("%12.12s: %s\n", "filename",
+ sqlite3_fputs("\n", p->out);
+ sqlite3_fprintf(p->out, "%12.12s: %s\n", "filename",
p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : "");
}else
@@ -28957,9 +31784,10 @@ static int do_meta_command(char *zLine, ShellState *p){
for(i=0; i<nPrintRow; i++){
for(j=i; j<nRow; j+=nPrintRow){
char *zSp = j<nPrintRow ? "" : " ";
- oputf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j]:"");
+ sqlite3_fprintf(p->out,
+ "%s%-*s", zSp, maxlen, azResult[j] ? azResult[j]:"");
}
- oputz("\n");
+ sqlite3_fputs("\n", p->out);
}
}
@@ -28971,7 +31799,7 @@ static int do_meta_command(char *zLine, ShellState *p){
/* Begin redirecting output to the file "testcase-out.txt" */
if( c=='t' && cli_strcmp(azArg[0],"testcase")==0 ){
output_reset(p);
- p->out = output_file_open("testcase-out.txt", 0);
+ p->out = output_file_open("testcase-out.txt");
if( p->out==0 ){
eputz("Error: cannot open 'testcase-out.txt'\n");
}
@@ -29004,18 +31832,17 @@ static int do_meta_command(char *zLine, ShellState *p){
{"json_selfcheck", SQLITE_TESTCTRL_JSON_SELFCHECK ,0,"BOOLEAN" },
{"localtime_fault", SQLITE_TESTCTRL_LOCALTIME_FAULT,0,"BOOLEAN" },
{"never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT,1, "BOOLEAN" },
- {"optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS,0,"DISABLE-MASK" },
+ {"optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS,0,"DISABLE-MASK ..."},
#ifdef YYCOVERAGE
{"parser_coverage", SQLITE_TESTCTRL_PARSER_COVERAGE,0,"" },
#endif
- {"pending_byte", SQLITE_TESTCTRL_PENDING_BYTE,0, "OFFSET " },
+ {"pending_byte", SQLITE_TESTCTRL_PENDING_BYTE,1, "OFFSET " },
{"prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE,0, "" },
{"prng_save", SQLITE_TESTCTRL_PRNG_SAVE, 0, "" },
{"prng_seed", SQLITE_TESTCTRL_PRNG_SEED, 0, "SEED ?db?" },
{"seek_count", SQLITE_TESTCTRL_SEEK_COUNT, 0, "" },
{"sorter_mmap", SQLITE_TESTCTRL_SORTER_MMAP, 0, "NMAX" },
{"tune", SQLITE_TESTCTRL_TUNE, 1, "ID VALUE" },
- {"uselongdouble", SQLITE_TESTCTRL_USELONGDOUBLE,0,"?BOOLEAN|\"default\"?"},
};
int testctrl = -1;
int iCtrl = -1;
@@ -29035,10 +31862,10 @@ static int do_meta_command(char *zLine, ShellState *p){
/* --help lists all test-controls */
if( cli_strcmp(zCmd,"help")==0 ){
- oputz("Available test-controls:\n");
+ sqlite3_fputs("Available test-controls:\n", p->out);
for(i=0; i<ArraySize(aCtrl); i++){
if( aCtrl[i].unSafe && !ShellHasFlag(p,SHFLG_TestingMode) ) continue;
- oputf(" .testctrl %s %s\n",
+ sqlite3_fprintf(p->out, " .testctrl %s %s\n",
aCtrl[i].zCtrlName, aCtrl[i].zUsage);
}
rc = 1;
@@ -29055,7 +31882,7 @@ static int do_meta_command(char *zLine, ShellState *p){
testctrl = aCtrl[i].ctrlCode;
iCtrl = i;
}else{
- eputf("Error: ambiguous test-control: \"%s\"\n"
+ sqlite3_fprintf(stderr,"Error: ambiguous test-control: \"%s\"\n"
"Use \".testctrl --help\" for help\n", zCmd);
rc = 1;
goto meta_command_exit;
@@ -29063,13 +31890,131 @@ static int do_meta_command(char *zLine, ShellState *p){
}
}
if( testctrl<0 ){
- eputf("Error: unknown test-control: %s\n"
+ sqlite3_fprintf(stderr,"Error: unknown test-control: %s\n"
"Use \".testctrl --help\" for help\n", zCmd);
}else{
switch(testctrl){
+ /* Special processing for .testctrl opt MASK ...
+ ** Each MASK argument can be one of:
+ **
+ ** +LABEL Enable the named optimization
+ **
+ ** -LABEL Disable the named optimization
+ **
+ ** INTEGER Mask of optimizations to disable
+ */
+ case SQLITE_TESTCTRL_OPTIMIZATIONS: {
+ static const struct {
+ unsigned int mask; /* Mask for this optimization */
+ unsigned int bDsply; /* Display this on output */
+ const char *zLabel; /* Name of optimization */
+ } aLabel[] = {
+ { 0x00000001, 1, "QueryFlattener" },
+ { 0x00000001, 0, "Flatten" },
+ { 0x00000002, 1, "WindowFunc" },
+ { 0x00000004, 1, "GroupByOrder" },
+ { 0x00000008, 1, "FactorOutConst" },
+ { 0x00000010, 1, "DistinctOpt" },
+ { 0x00000020, 1, "CoverIdxScan" },
+ { 0x00000040, 1, "OrderByIdxJoin" },
+ { 0x00000080, 1, "Transitive" },
+ { 0x00000100, 1, "OmitNoopJoin" },
+ { 0x00000200, 1, "CountOfView" },
+ { 0x00000400, 1, "CurosrHints" },
+ { 0x00000800, 1, "Stat4" },
+ { 0x00001000, 1, "PushDown" },
+ { 0x00002000, 1, "SimplifyJoin" },
+ { 0x00004000, 1, "SkipScan" },
+ { 0x00008000, 1, "PropagateConst" },
+ { 0x00010000, 1, "MinMaxOpt" },
+ { 0x00020000, 1, "SeekScan" },
+ { 0x00040000, 1, "OmitOrderBy" },
+ { 0x00080000, 1, "BloomFilter" },
+ { 0x00100000, 1, "BloomPulldown" },
+ { 0x00200000, 1, "BalancedMerge" },
+ { 0x00400000, 1, "ReleaseReg" },
+ { 0x00800000, 1, "FlttnUnionAll" },
+ { 0x01000000, 1, "IndexedEXpr" },
+ { 0x02000000, 1, "Coroutines" },
+ { 0x04000000, 1, "NullUnusedCols" },
+ { 0x08000000, 1, "OnePass" },
+ { 0x10000000, 1, "OrderBySubq" },
+ { 0x20000000, 1, "StarQuery" },
+ { 0xffffffff, 0, "All" },
+ };
+ unsigned int curOpt;
+ unsigned int newOpt;
+ unsigned int m;
+ int ii;
+ int nOff;
+ sqlite3_test_control(SQLITE_TESTCTRL_GETOPT, p->db, &curOpt);
+ newOpt = curOpt;
+ for(ii=2; ii<nArg; ii++){
+ const char *z = azArg[ii];
+ int useLabel = 0;
+ const char *zLabel = 0;
+ if( (z[0]=='+'|| z[0]=='-') && !IsDigit(z[1]) ){
+ useLabel = z[0];
+ zLabel = &z[1];
+ }else if( !IsDigit(z[0]) && z[0]!=0 && !IsDigit(z[1]) ){
+ useLabel = '+';
+ zLabel = z;
+ }else{
+ newOpt = (unsigned int)strtol(z,0,0);
+ }
+ if( useLabel ){
+ int jj;
+ for(jj=0; jj<ArraySize(aLabel); jj++){
+ if( sqlite3_stricmp(zLabel, aLabel[jj].zLabel)==0 ) break;
+ }
+ if( jj>=ArraySize(aLabel) ){
+ sqlite3_fprintf(stderr,
+ "Error: no such optimization: \"%s\"\n", zLabel);
+ sqlite3_fputs("Should be one of:", stderr);
+ for(jj=0; jj<ArraySize(aLabel); jj++){
+ sqlite3_fprintf(stderr," %s", aLabel[jj].zLabel);
+ }
+ sqlite3_fputs("\n", stderr);
+ rc = 1;
+ goto meta_command_exit;
+ }
+ if( useLabel=='+' ){
+ newOpt &= ~aLabel[jj].mask;
+ }else{
+ newOpt |= aLabel[jj].mask;
+ }
+ }
+ }
+ if( curOpt!=newOpt ){
+ sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,p->db,newOpt);
+ }
+ for(ii=nOff=0, m=1; ii<32; ii++, m <<= 1){
+ if( m & newOpt ) nOff++;
+ }
+ if( nOff<12 ){
+ sqlite3_fputs("+All", p->out);
+ for(ii=0; ii<ArraySize(aLabel); ii++){
+ if( !aLabel[ii].bDsply ) continue;
+ if( (newOpt & aLabel[ii].mask)!=0 ){
+ sqlite3_fprintf(p->out, " -%s", aLabel[ii].zLabel);
+ }
+ }
+ }else{
+ sqlite3_fputs("-All", p->out);
+ for(ii=0; ii<ArraySize(aLabel); ii++){
+ if( !aLabel[ii].bDsply ) continue;
+ if( (newOpt & aLabel[ii].mask)==0 ){
+ sqlite3_fprintf(p->out, " +%s", aLabel[ii].zLabel);
+ }
+ }
+ }
+ sqlite3_fputs("\n", p->out);
+ rc2 = isOk = 3;
+ break;
+ }
+
/* sqlite3_test_control(int, db, int) */
- case SQLITE_TESTCTRL_OPTIMIZATIONS:
case SQLITE_TESTCTRL_FK_NO_ACTION:
if( nArg==3 ){
unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0);
@@ -29104,7 +32049,7 @@ static int do_meta_command(char *zLine, ShellState *p){
sqlite3 *db;
if( ii==0 && cli_strcmp(azArg[2],"random")==0 ){
sqlite3_randomness(sizeof(ii),&ii);
- sputf(stdout, "-- random seed: %d\n", ii);
+ sqlite3_fprintf(stdout, "-- random seed: %d\n", ii);
}
if( nArg==3 ){
db = 0;
@@ -29138,21 +32083,6 @@ static int do_meta_command(char *zLine, ShellState *p){
}
break;
- /* sqlite3_test_control(int, int) */
- case SQLITE_TESTCTRL_USELONGDOUBLE: {
- int opt = -1;
- if( nArg==3 ){
- if( cli_strcmp(azArg[2],"default")==0 ){
- opt = 2;
- }else{
- opt = booleanValue(azArg[2]);
- }
- }
- rc2 = sqlite3_test_control(testctrl, opt);
- isOk = 1;
- break;
- }
-
/* sqlite3_test_control(sqlite3*) */
case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
rc2 = sqlite3_test_control(testctrl, p->db);
@@ -29172,7 +32102,7 @@ static int do_meta_command(char *zLine, ShellState *p){
case SQLITE_TESTCTRL_SEEK_COUNT: {
u64 x = 0;
rc2 = sqlite3_test_control(testctrl, p->db, &x);
- oputf("%llu\n", x);
+ sqlite3_fprintf(p->out, "%llu\n", x);
isOk = 3;
break;
}
@@ -29203,11 +32133,11 @@ static int do_meta_command(char *zLine, ShellState *p){
int val = 0;
rc2 = sqlite3_test_control(testctrl, -id, &val);
if( rc2!=SQLITE_OK ) break;
- if( id>1 ) oputz(" ");
- oputf("%d: %d", id, val);
+ if( id>1 ) sqlite3_fputs(" ", p->out);
+ sqlite3_fprintf(p->out, "%d: %d", id, val);
id++;
}
- if( id>1 ) oputz("\n");
+ if( id>1 ) sqlite3_fputs("\n", p->out);
isOk = 3;
}
break;
@@ -29249,14 +32179,22 @@ static int do_meta_command(char *zLine, ShellState *p){
faultsim_state.nHit = 0;
sqlite3_test_control(testctrl, faultsim_callback);
}else if( cli_strcmp(z,"status")==0 ){
- oputf("faultsim.iId: %d\n", faultsim_state.iId);
- oputf("faultsim.iErr: %d\n", faultsim_state.iErr);
- oputf("faultsim.iCnt: %d\n", faultsim_state.iCnt);
- oputf("faultsim.nHit: %d\n", faultsim_state.nHit);
- oputf("faultsim.iInterval: %d\n", faultsim_state.iInterval);
- oputf("faultsim.eVerbose: %d\n", faultsim_state.eVerbose);
- oputf("faultsim.nRepeat: %d\n", faultsim_state.nRepeat);
- oputf("faultsim.nSkip: %d\n", faultsim_state.nSkip);
+ sqlite3_fprintf(p->out, "faultsim.iId: %d\n",
+ faultsim_state.iId);
+ sqlite3_fprintf(p->out, "faultsim.iErr: %d\n",
+ faultsim_state.iErr);
+ sqlite3_fprintf(p->out, "faultsim.iCnt: %d\n",
+ faultsim_state.iCnt);
+ sqlite3_fprintf(p->out, "faultsim.nHit: %d\n",
+ faultsim_state.nHit);
+ sqlite3_fprintf(p->out, "faultsim.iInterval: %d\n",
+ faultsim_state.iInterval);
+ sqlite3_fprintf(p->out, "faultsim.eVerbose: %d\n",
+ faultsim_state.eVerbose);
+ sqlite3_fprintf(p->out, "faultsim.nRepeat: %d\n",
+ faultsim_state.nRepeat);
+ sqlite3_fprintf(p->out, "faultsim.nSkip: %d\n",
+ faultsim_state.nSkip);
}else if( cli_strcmp(z,"-v")==0 ){
if( faultsim_state.eVerbose<2 ) faultsim_state.eVerbose++;
}else if( cli_strcmp(z,"-q")==0 ){
@@ -29274,7 +32212,8 @@ static int do_meta_command(char *zLine, ShellState *p){
}else if( cli_strcmp(z,"-?")==0 || sqlite3_strglob("*help*",z)==0){
bShowHelp = 1;
}else{
- eputf("Unrecognized fault_install argument: \"%s\"\n",
+ sqlite3_fprintf(stderr,
+ "Unrecognized fault_install argument: \"%s\"\n",
azArg[kk]);
rc = 1;
bShowHelp = 1;
@@ -29282,7 +32221,7 @@ static int do_meta_command(char *zLine, ShellState *p){
}
}
if( bShowHelp ){
- oputz(
+ sqlite3_fputs(
"Usage: .testctrl fault_install ARGS\n"
"Possible arguments:\n"
" off Disable faultsim\n"
@@ -29296,6 +32235,7 @@ static int do_meta_command(char *zLine, ShellState *p){
" --interval N Trigger only after every N-th call\n"
" --repeat N Turn off after N hits. 0 means never\n"
" --skip N Skip the first N encounters\n"
+ ,p->out
);
}
break;
@@ -29303,12 +32243,13 @@ static int do_meta_command(char *zLine, ShellState *p){
}
}
if( isOk==0 && iCtrl>=0 ){
- oputf("Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
+ sqlite3_fprintf(p->out,
+ "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
rc = 1;
}else if( isOk==1 ){
- oputf("%d\n", rc2);
+ sqlite3_fprintf(p->out, "%d\n", rc2);
}else if( isOk==2 ){
- oputf("0x%08x\n", rc2);
+ sqlite3_fprintf(p->out, "0x%08x\n", rc2);
}
}else
#endif /* !defined(SQLITE_UNTESTABLE) */
@@ -29363,13 +32304,13 @@ static int do_meta_command(char *zLine, ShellState *p){
mType |= SQLITE_TRACE_CLOSE;
}
else {
- eputf("Unknown option \"%s\" on \".trace\"\n", z);
+ sqlite3_fprintf(stderr,"Unknown option \"%s\" on \".trace\"\n", z);
rc = 1;
goto meta_command_exit;
}
}else{
output_file_close(p->traceOut);
- p->traceOut = output_file_open(z, 0);
+ p->traceOut = output_file_open(z);
}
}
if( p->traceOut==0 ){
@@ -29406,86 +32347,23 @@ static int do_meta_command(char *zLine, ShellState *p){
}else
#endif
-#if SQLITE_USER_AUTHENTICATION
- if( c=='u' && cli_strncmp(azArg[0], "user", n)==0 ){
- if( nArg<2 ){
- eputz("Usage: .user SUBCOMMAND ...\n");
- rc = 1;
- goto meta_command_exit;
- }
- open_db(p, 0);
- if( cli_strcmp(azArg[1],"login")==0 ){
- if( nArg!=4 ){
- eputz("Usage: .user login USER PASSWORD\n");
- rc = 1;
- goto meta_command_exit;
- }
- rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
- strlen30(azArg[3]));
- if( rc ){
- eputf("Authentication failed for user %s\n", azArg[2]);
- rc = 1;
- }
- }else if( cli_strcmp(azArg[1],"add")==0 ){
- if( nArg!=5 ){
- eputz("Usage: .user add USER PASSWORD ISADMIN\n");
- rc = 1;
- goto meta_command_exit;
- }
- rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
- booleanValue(azArg[4]));
- if( rc ){
- eputf("User-Add failed: %d\n", rc);
- rc = 1;
- }
- }else if( cli_strcmp(azArg[1],"edit")==0 ){
- if( nArg!=5 ){
- eputz("Usage: .user edit USER PASSWORD ISADMIN\n");
- rc = 1;
- goto meta_command_exit;
- }
- rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
- booleanValue(azArg[4]));
- if( rc ){
- eputf("User-Edit failed: %d\n", rc);
- rc = 1;
- }
- }else if( cli_strcmp(azArg[1],"delete")==0 ){
- if( nArg!=3 ){
- eputz("Usage: .user delete USER\n");
- rc = 1;
- goto meta_command_exit;
- }
- rc = sqlite3_user_delete(p->db, azArg[2]);
- if( rc ){
- eputf("User-Delete failed: %d\n", rc);
- rc = 1;
- }
- }else{
- eputz("Usage: .user login|add|edit|delete ...\n");
- rc = 1;
- goto meta_command_exit;
- }
- }else
-#endif /* SQLITE_USER_AUTHENTICATION */
-
if( c=='v' && cli_strncmp(azArg[0], "version", n)==0 ){
char *zPtrSz = sizeof(void*)==8 ? "64-bit" : "32-bit";
- oputf("SQLite %s %s\n" /*extra-version-info*/,
+ sqlite3_fprintf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
sqlite3_libversion(), sqlite3_sourceid());
#if SQLITE_HAVE_ZLIB
- oputf("zlib version %s\n", zlibVersion());
+ sqlite3_fprintf(p->out, "zlib version %s\n", zlibVersion());
#endif
#define CTIMEOPT_VAL_(opt) #opt
#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
#if defined(__clang__) && defined(__clang_major__)
- oputf("clang-" CTIMEOPT_VAL(__clang_major__) "."
+ sqlite3_fprintf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
CTIMEOPT_VAL(__clang_minor__) "."
CTIMEOPT_VAL(__clang_patchlevel__) " (%s)\n", zPtrSz);
#elif defined(_MSC_VER)
- oputf("msvc-" CTIMEOPT_VAL(_MSC_VER) " (%s)\n", zPtrSz);
+ sqlite3_fprintf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) " (%s)\n", zPtrSz);
#elif defined(__GNUC__) && defined(__VERSION__)
- oputf("gcc-" __VERSION__ " (%s)\n", zPtrSz);
+ sqlite3_fprintf(p->out, "gcc-" __VERSION__ " (%s)\n", zPtrSz);
#endif
}else
@@ -29495,10 +32373,10 @@ static int do_meta_command(char *zLine, ShellState *p){
if( p->db ){
sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
if( pVfs ){
- oputf("vfs.zName = \"%s\"\n", pVfs->zName);
- oputf("vfs.iVersion = %d\n", pVfs->iVersion);
- oputf("vfs.szOsFile = %d\n", pVfs->szOsFile);
- oputf("vfs.mxPathname = %d\n", pVfs->mxPathname);
+ sqlite3_fprintf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName);
+ sqlite3_fprintf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
+ sqlite3_fprintf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
+ sqlite3_fprintf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
}
}
}else
@@ -29510,13 +32388,13 @@ static int do_meta_command(char *zLine, ShellState *p){
sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
}
for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
- oputf("vfs.zName = \"%s\"%s\n", pVfs->zName,
+ sqlite3_fprintf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName,
pVfs==pCurrent ? " <--- CURRENT" : "");
- oputf("vfs.iVersion = %d\n", pVfs->iVersion);
- oputf("vfs.szOsFile = %d\n", pVfs->szOsFile);
- oputf("vfs.mxPathname = %d\n", pVfs->mxPathname);
+ sqlite3_fprintf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
+ sqlite3_fprintf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
+ sqlite3_fprintf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
if( pVfs->pNext ){
- oputz("-----------------------------------\n");
+ sqlite3_fputs("-----------------------------------\n", p->out);
}
}
}else
@@ -29527,7 +32405,7 @@ static int do_meta_command(char *zLine, ShellState *p){
if( p->db ){
sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
if( zVfsName ){
- oputf("%s\n", zVfsName);
+ sqlite3_fprintf(p->out, "%s\n", zVfsName);
sqlite3_free(zVfsName);
}
}
@@ -29551,7 +32429,7 @@ static int do_meta_command(char *zLine, ShellState *p){
}else
{
- eputf("Error: unknown command or invalid arguments: "
+ sqlite3_fprintf(stderr,"Error: unknown command or invalid arguments: "
" \"%s\". Enter \".help\" for help\n", azArg[0]);
rc = 1;
}
@@ -29592,7 +32470,6 @@ static QuickScanState quickscan(char *zLine, QuickScanState qss,
char cWait = (char)qss; /* intentional narrowing loss */
if( cWait==0 ){
PlainScan:
- assert( cWait==0 );
while( (cin = *zLine++)!=0 ){
if( IsSpace(cin) )
continue;
@@ -29618,7 +32495,7 @@ static QuickScanState quickscan(char *zLine, QuickScanState qss,
break;
case '[':
cin = ']';
- deliberate_fall_through;
+ deliberate_fall_through; /* FALLTHRU */
case '`': case '\'': case '"':
cWait = cin;
qss = QSS_HasDark | cWait;
@@ -29644,7 +32521,6 @@ static QuickScanState quickscan(char *zLine, QuickScanState qss,
if( *zLine != '/' )
continue;
++zLine;
- cWait = 0;
CONTINUE_PROMPT_AWAITC(pst, 0);
qss = QSS_SETV(qss, 0);
goto PlainScan;
@@ -29654,9 +32530,8 @@ static QuickScanState quickscan(char *zLine, QuickScanState qss,
++zLine;
continue;
}
- deliberate_fall_through;
+ deliberate_fall_through; /* FALLTHRU */
case ']':
- cWait = 0;
CONTINUE_PROMPT_AWAITC(pst, 0);
qss = QSS_SETV(qss, 0);
goto PlainScan;
@@ -29689,7 +32564,7 @@ static int line_is_command_terminator(char *zLine){
** out of the build if compiling with SQLITE_OMIT_COMPLETE.
*/
#ifdef SQLITE_OMIT_COMPLETE
-# error the CLI application is imcompatable with SQLITE_OMIT_COMPLETE.
+# error the CLI application is incompatible with SQLITE_OMIT_COMPLETE.
#endif
/*
@@ -29732,7 +32607,10 @@ static int doAutoDetectRestore(ShellState *p, const char *zSql){
case 0: {
const char *zExpect = "PRAGMA foreign_keys=OFF;";
assert( strlen(zExpect)==24 );
- if( p->bSafeMode==0 && memcmp(zSql, zExpect, 25)==0 ){
+ if( p->bSafeMode==0
+ && strlen(zSql)>=24
+ && memcmp(zSql, zExpect, 25)==0
+ ){
p->eRestoreState = 1;
}else{
p->eRestoreState = 7;
@@ -29800,7 +32678,7 @@ static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
BEGIN_TIMER;
rc = shell_exec(p, zSql, &zErrMsg);
- END_TIMER;
+ END_TIMER(p->out);
if( rc || zErrMsg ){
char zPrefix[100];
const char *zErrorTail;
@@ -29824,7 +32702,7 @@ static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
}else{
sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:", zErrorType);
}
- eputf("%s %s\n", zPrefix, zErrorTail);
+ sqlite3_fprintf(stderr,"%s %s\n", zPrefix, zErrorTail);
sqlite3_free(zErrMsg);
zErrMsg = 0;
return 1;
@@ -29833,7 +32711,7 @@ static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
sqlite3_snprintf(sizeof(zLineBuf), zLineBuf,
"changes: %lld total_changes: %lld",
sqlite3_changes64(p->db), sqlite3_total_changes64(p->db));
- oputf("%s\n", zLineBuf);
+ sqlite3_fprintf(p->out, "%s\n", zLineBuf);
}
if( doAutoDetectRestore(p, zSql) ) return 1;
@@ -29841,7 +32719,10 @@ static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
}
static void echo_group_input(ShellState *p, const char *zDo){
- if( ShellHasFlag(p, SHFLG_Echo) ) oputf("%s\n", zDo);
+ if( ShellHasFlag(p, SHFLG_Echo) ){
+ sqlite3_fprintf(p->out, "%s\n", zDo);
+ fflush(p->out);
+ }
}
#ifdef SQLITE_SHELL_FIDDLE
@@ -29862,7 +32743,7 @@ static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
if(!z || !*z){
return 0;
}
- while(*z && isspace(*z)) ++z;
+ while(*z && IsSpace(*z)) ++z;
zBegin = z;
for(; *z && '\n'!=*z; ++nZ, ++z){}
if(nZ>0 && '\r'==zBegin[nZ-1]){
@@ -29899,7 +32780,7 @@ static int process_input(ShellState *p){
if( p->inputNesting==MAX_INPUT_NESTING ){
/* This will be more informative in a later version. */
- eputf("Input nesting limit (%d) reached at line %d."
+ sqlite3_fprintf(stderr,"Input nesting limit (%d) reached at line %d."
" Check recursion.\n", MAX_INPUT_NESTING, p->lineno);
return 1;
}
@@ -29911,7 +32792,7 @@ static int process_input(ShellState *p){
zLine = one_input_line(p->in, zLine, nSql>0);
if( zLine==0 ){
/* End of input */
- if( p->in==0 && stdin_is_interactive ) oputz("\n");
+ if( p->in==0 && stdin_is_interactive ) sqlite3_fputs("\n", p->out);
break;
}
if( seenInterrupt ){
@@ -30068,27 +32949,29 @@ static char *find_home_dir(int clearFlag){
/*
** On non-Windows platforms, look for $XDG_CONFIG_HOME.
** If ${XDG_CONFIG_HOME}/sqlite3/sqliterc is found, return
-** the path to it, else return 0. The result is cached for
-** subsequent calls.
+** the path to it. If there is no $(XDG_CONFIG_HOME) then
+** look for $(HOME)/.config/sqlite3/sqliterc and if found
+** return that. If none of these are found, return 0.
+**
+** The string returned is obtained from sqlite3_malloc() and
+** should be freed by the caller.
*/
-static const char *find_xdg_config(void){
+static char *find_xdg_config(void){
#if defined(_WIN32) || defined(WIN32) || defined(_WIN32_WCE) \
|| defined(__RTP__) || defined(_WRS_KERNEL)
return 0;
#else
- static int alreadyTried = 0;
- static char *zConfig = 0;
+ char *zConfig = 0;
const char *zXdgHome;
- if( alreadyTried!=0 ){
- return zConfig;
- }
- alreadyTried = 1;
zXdgHome = getenv("XDG_CONFIG_HOME");
if( zXdgHome==0 ){
- return 0;
+ const char *zHome = getenv("HOME");
+ if( zHome==0 ) return 0;
+ zConfig = sqlite3_mprintf("%s/.config/sqlite3/sqliterc", zHome);
+ }else{
+ zConfig = sqlite3_mprintf("%s/sqlite3/sqliterc", zXdgHome);
}
- zConfig = sqlite3_mprintf("%s/sqlite3/sqliterc", zXdgHome);
shell_check_oom(zConfig);
if( access(zConfig,0)!=0 ){
sqlite3_free(zConfig);
@@ -30116,7 +32999,7 @@ static void process_sqliterc(
int savedLineno = p->lineno;
if( sqliterc == NULL ){
- sqliterc = find_xdg_config();
+ sqliterc = zBuf = find_xdg_config();
}
if( sqliterc == NULL ){
home_dir = find_home_dir(0);
@@ -30129,15 +33012,15 @@ static void process_sqliterc(
shell_check_oom(zBuf);
sqliterc = zBuf;
}
- p->in = fopen(sqliterc,"rb");
+ p->in = sqlite3_fopen(sqliterc,"rb");
if( p->in ){
if( stdin_is_interactive ){
- eputf("-- Loading resources from %s\n", sqliterc);
+ sqlite3_fprintf(stderr,"-- Loading resources from %s\n", sqliterc);
}
if( process_input(p) && bail_on_error ) exit(1);
fclose(p->in);
}else if( sqliterc_override!=0 ){
- eputf("cannot open: \"%s\"\n", sqliterc);
+ sqlite3_fprintf(stderr,"cannot open: \"%s\"\n", sqliterc);
if( bail_on_error ) exit(1);
}
p->in = inSaved;
@@ -30165,6 +33048,7 @@ static const char zOptions[] =
" -deserialize open the database using sqlite3_deserialize()\n"
#endif
" -echo print inputs before execution\n"
+ " -escape T ctrl-char escape; T is one of: symbol, ascii, off\n"
" -init FILENAME read/process named file\n"
" -[no]header turn headers on or off\n"
#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
@@ -30206,19 +33090,17 @@ static const char zOptions[] =
" -unsafe-testing allow unsafe commands and modes for testing\n"
" -version show SQLite version\n"
" -vfs NAME use NAME as the default VFS\n"
-#ifdef SQLITE_ENABLE_VFSTRACE
" -vfstrace enable tracing of all VFS calls\n"
-#endif
#ifdef SQLITE_HAVE_ZLIB
" -zip open the file as a ZIP Archive\n"
#endif
;
static void usage(int showDetail){
- eputf("Usage: %s [OPTIONS] [FILENAME [SQL]]\n"
+ sqlite3_fprintf(stderr,"Usage: %s [OPTIONS] [FILENAME [SQL...]]\n"
"FILENAME is the name of an SQLite database. A new database is created\n"
"if the file does not previously exist. Defaults to :memory:.\n", Argv0);
if( showDetail ){
- eputf("OPTIONS include:\n%s", zOptions);
+ sqlite3_fprintf(stderr,"OPTIONS include:\n%s", zOptions);
}else{
eputz("Use the -help option for additional information\n");
}
@@ -30243,6 +33125,9 @@ static void main_init(ShellState *data) {
memset(data, 0, sizeof(*data));
data->normalMode = data->cMode = data->mode = MODE_List;
data->autoExplain = 1;
+#ifdef _WIN32
+ data->crlfMode = 1;
+#endif
data->pAuxDb = &data->aAuxDb[0];
memcpy(data->colSeparator,SEP_Column, 2);
memcpy(data->rowSeparator,SEP_Row, 2);
@@ -30278,7 +33163,7 @@ static void printBold(const char *zText){
}
#else
static void printBold(const char *zText){
- sputf(stdout, "\033[1m%s\033[0m", zText);
+ sqlite3_fprintf(stdout, "\033[1m%s\033[0m", zText);
}
#endif
@@ -30288,7 +33173,8 @@ static void printBold(const char *zText){
*/
static char *cmdline_option_value(int argc, char **argv, int i){
if( i==argc ){
- eputf("%s: Error: missing argument to %s\n", argv[0], argv[argc-1]);
+ sqlite3_fprintf(stderr,
+ "%s: Error: missing argument to %s\n", argv[0], argv[argc-1]);
exit(1);
}
return argv[i];
@@ -30298,6 +33184,15 @@ static void sayAbnormalExit(void){
if( seenInterrupt ) eputz("Program interrupted.\n");
}
+/* Routine to output from vfstrace
+*/
+static int vfstraceOut(const char *z, void *pArg){
+ ShellState *p = (ShellState*)pArg;
+ sqlite3_fputs(z, p->out);
+ fflush(p->out);
+ return 1;
+}
+
#ifndef SQLITE_SHELL_IS_UTF8
# if (defined(_WIN32) || defined(WIN32)) \
&& (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
@@ -30325,7 +33220,6 @@ int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
# define data shellState
#else
ShellState data;
- StreamsAreConsole consStreams = SAC_NoConsole;
#endif
const char *zInitFile = 0;
int i;
@@ -30334,6 +33228,7 @@ int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
int readStdin = 1;
int nCmd = 0;
int nOptsEnd = argc;
+ int bEnableVfstrace = 0;
char **azCmd = 0;
const char *zVfs = 0; /* Value of -vfs command-line option */
#if !SQLITE_SHELL_IS_UTF8
@@ -30347,10 +33242,8 @@ int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
stdout_is_console = 1;
data.wasm.zDefaultDbName = "/fiddle.sqlite3";
#else
- consStreams = consoleClassifySetup(stdin, stdout, stderr);
- stdin_is_interactive = (consStreams & SAC_InConsole)!=0;
- stdout_is_console = (consStreams & SAC_OutConsole)!=0;
- atexit(consoleRestore);
+ stdin_is_interactive = isatty(0);
+ stdout_is_console = isatty(1);
#endif
atexit(sayAbnormalExit);
#ifdef SQLITE_DEBUG
@@ -30359,9 +33252,15 @@ int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
#if !defined(_WIN32_WCE)
if( getenv("SQLITE_DEBUG_BREAK") ){
if( isatty(0) && isatty(2) ){
- eputf("attach debugger to process %d and press any key to continue.\n",
+ char zLine[100];
+ sqlite3_fprintf(stderr,
+ "attach debugger to process %d and press ENTER to continue...",
GETPID());
- fgetc(stdin);
+ if( sqlite3_fgets(zLine, sizeof(zLine), stdin)!=0
+ && cli_strcmp(zLine,"stop")==0
+ ){
+ exit(1);
+ }
}else{
#if defined(_WIN32) || defined(WIN32)
#if SQLITE_OS_WINRT
@@ -30386,7 +33285,8 @@ int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
#if USE_SYSTEM_SQLITE+0!=1
if( cli_strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
- eputf("SQLite header and source version mismatch\n%s\n%s\n",
+ sqlite3_fprintf(stderr,
+ "SQLite header and source version mismatch\n%s\n%s\n",
sqlite3_sourceid(), SQLITE_SOURCE_ID);
exit(1);
}
@@ -30528,17 +33428,8 @@ int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
case 2: sqlite3_config(SQLITE_CONFIG_MULTITHREAD); break;
default: sqlite3_config(SQLITE_CONFIG_SERIALIZED); break;
}
-#ifdef SQLITE_ENABLE_VFSTRACE
}else if( cli_strcmp(z,"-vfstrace")==0 ){
- extern int vfstrace_register(
- const char *zTraceName,
- const char *zOldVfsName,
- int (*xOut)(const char*,void*),
- void *pOutArg,
- int makeDefault
- );
- vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
-#endif
+ bEnableVfstrace = 1;
#ifdef SQLITE_ENABLE_MULTIPLEX
}else if( cli_strcmp(z,"-multiplex")==0 ){
extern int sqlite3_multiplex_initialize(const char*,int);
@@ -30591,10 +33482,13 @@ int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
ShellSetFlag(&data,SHFLG_TestingMode);
}else if( cli_strcmp(z,"-safe")==0 ){
/* no-op - catch this on the second pass */
+ }else if( cli_strcmp(z,"-escape")==0 && i+1<argc ){
+ /* skip over the argument */
+ i++;
}
}
#ifndef SQLITE_SHELL_FIDDLE
- verify_uninitialized();
+ if( !bEnableVfstrace ) verify_uninitialized();
#endif
@@ -30618,7 +33512,7 @@ int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
if( pVfs ){
sqlite3_vfs_register(pVfs, 1);
}else{
- eputf("no such VFS: \"%s\"\n", zVfs);
+ sqlite3_fprintf(stderr,"no such VFS: \"%s\"\n", zVfs);
exit(1);
}
}
@@ -30628,11 +33522,15 @@ int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
data.pAuxDb->zDbFilename = ":memory:";
warnInmemoryDb = argc==1;
#else
- eputf("%s: Error: no database filename specified\n", Argv0);
+ sqlite3_fprintf(stderr,
+ "%s: Error: no database filename specified\n", Argv0);
return 1;
#endif
}
data.out = stdout;
+ if( bEnableVfstrace ){
+ vfstrace_register("trace",0,vfstraceOut, &data, 1);
+ }
#ifndef SQLITE_SHELL_FIDDLE
sqlite3_appendvfs_init(0,0,0);
#endif
@@ -30686,6 +33584,25 @@ int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
}else if( cli_strcmp(z,"-csv")==0 ){
data.mode = MODE_Csv;
memcpy(data.colSeparator,",",2);
+ }else if( cli_strcmp(z,"-escape")==0 && i+1<argc ){
+ /* See similar code at tag-20250224-1 */
+ const char *zEsc = argv[++i];
+ int k;
+ for(k=0; k<ArraySize(shell_EscModeNames); k++){
+ if( sqlite3_stricmp(zEsc,shell_EscModeNames[k])==0 ){
+ data.eEscMode = k;
+ break;
+ }
+ }
+ if( k>=ArraySize(shell_EscModeNames) ){
+ sqlite3_fprintf(stderr, "unknown control character escape mode \"%s\""
+ " - choices:", zEsc);
+ for(k=0; k<ArraySize(shell_EscModeNames); k++){
+ sqlite3_fprintf(stderr, " %s", shell_EscModeNames[k]);
+ }
+ sqlite3_fprintf(stderr, "\n");
+ exit(1);
+ }
#ifdef SQLITE_HAVE_ZLIB
}else if( cli_strcmp(z,"-zip")==0 ){
data.openMode = SHELL_OPEN_ZIPFILE;
@@ -30745,7 +33662,7 @@ int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
}else if( cli_strcmp(z,"-bail")==0 ){
/* No-op. The bail_on_error flag should already be set. */
}else if( cli_strcmp(z,"-version")==0 ){
- sputf(stdout, "%s %s (%d-bit)\n",
+ sqlite3_fprintf(stdout, "%s %s (%d-bit)\n",
sqlite3_libversion(), sqlite3_sourceid(), 8*(int)sizeof(char*));
return 0;
}else if( cli_strcmp(z,"-interactive")==0 ){
@@ -30783,10 +33700,8 @@ int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
#endif
}else if( cli_strcmp(z,"-vfs")==0 ){
i++;
-#ifdef SQLITE_ENABLE_VFSTRACE
}else if( cli_strcmp(z,"-vfstrace")==0 ){
i++;
-#endif
#ifdef SQLITE_ENABLE_MULTIPLEX
}else if( cli_strcmp(z,"-multiplex")==0 ){
i++;
@@ -30807,17 +33722,17 @@ int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
open_db(&data, 0);
rc = shell_exec(&data, z, &zErrMsg);
if( zErrMsg!=0 ){
- eputf("Error: %s\n", zErrMsg);
+ shellEmitError(zErrMsg);
if( bail_on_error ) return rc!=0 ? rc : 1;
}else if( rc!=0 ){
- eputf("Error: unable to process SQL \"%s\"\n", z);
+ sqlite3_fprintf(stderr,"Error: unable to process SQL \"%s\"\n", z);
if( bail_on_error ) return rc;
}
}
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
}else if( cli_strncmp(z, "-A", 2)==0 ){
if( nCmd>0 ){
- eputf("Error: cannot mix regular SQL or dot-commands"
+ sqlite3_fprintf(stderr,"Error: cannot mix regular SQL or dot-commands"
" with \"%s\"\n", z);
return 1;
}
@@ -30836,7 +33751,7 @@ int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
}else if( cli_strcmp(z,"-unsafe-testing")==0 ){
/* Acted upon in first pass. */
}else{
- eputf("%s: Error: unknown option: %s\n", Argv0, z);
+ sqlite3_fprintf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
eputz("Use -help for a list of options.\n");
return 1;
}
@@ -30849,25 +33764,26 @@ int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
** the database filename.
*/
for(i=0; i<nCmd; i++){
+ echo_group_input(&data, azCmd[i]);
if( azCmd[i][0]=='.' ){
rc = do_meta_command(azCmd[i], &data);
if( rc ){
- free(azCmd);
- return rc==2 ? 0 : rc;
+ if( rc==2 ) rc = 0;
+ goto shell_main_exit;
}
}else{
open_db(&data, 0);
- echo_group_input(&data, azCmd[i]);
rc = shell_exec(&data, azCmd[i], &zErrMsg);
if( zErrMsg || rc ){
if( zErrMsg!=0 ){
- eputf("Error: %s\n", zErrMsg);
+ shellEmitError(zErrMsg);
}else{
- eputf("Error: unable to process SQL: %s\n", azCmd[i]);
+ sqlite3_fprintf(stderr,
+ "Error: unable to process SQL: %s\n", azCmd[i]);
}
sqlite3_free(zErrMsg);
- free(azCmd);
- return rc!=0 ? rc : 1;
+ if( rc==0 ) rc = 1;
+ goto shell_main_exit;
}
}
}
@@ -30878,14 +33794,10 @@ int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
char *zHome;
char *zHistory;
int nHistory;
-#if CIO_WIN_WC_XLATE
-# define SHELL_CIO_CHAR_SET (stdout_is_console? " (UTF-16 console I/O)" : "")
-#else
-# define SHELL_CIO_CHAR_SET ""
-#endif
- sputf(stdout, "SQLite version %s %.19s%s\n" /*extra-version-info*/
+ sqlite3_fprintf(stdout,
+ "SQLite version %s %.19s\n" /*extra-version-info*/
"Enter \".help\" for usage hints.\n",
- sqlite3_libversion(), sqlite3_sourceid(), SHELL_CIO_CHAR_SET);
+ sqlite3_libversion(), sqlite3_sourceid());
if( warnInmemoryDb ){
sputz(stdout, "Connected to a ");
printBold("transient in-memory database");
@@ -30902,10 +33814,12 @@ int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
}
}
if( zHistory ){ shell_read_history(zHistory); }
-#if HAVE_READLINE || HAVE_EDITLINE
+#if (HAVE_READLINE || HAVE_EDITLINE) && !defined(SQLITE_OMIT_READLINE_COMPLETION)
rl_attempted_completion_function = readline_completion;
-#elif HAVE_LINENOISE
+#elif HAVE_LINENOISE==1
linenoiseSetCompletionCallback(linenoise_completion);
+#elif HAVE_LINENOISE==2
+ linenoiseSetCompletionCallback(linenoise_completion, NULL);
#endif
data.in = 0;
rc = process_input(&data);
@@ -30927,6 +33841,7 @@ int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
expertFinish(&data, 1, 0);
}
#endif
+ shell_main_exit:
free(azCmd);
set_table_name(&data, 0);
if( data.db ){
@@ -30953,13 +33868,18 @@ int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
/* Clear the global data structure so that valgrind will detect memory
** leaks */
memset(&data, 0, sizeof(data));
+ if( bEnableVfstrace ){
+ vfstrace_unregister("trace");
+ }
#ifdef SQLITE_DEBUG
if( sqlite3_memory_used()>mem_main_enter ){
- eputf("Memory leaked: %u bytes\n",
+ sqlite3_fprintf(stderr,"Memory leaked: %u bytes\n",
(unsigned int)(sqlite3_memory_used()-mem_main_enter));
}
#endif
-#endif /* !SQLITE_SHELL_FIDDLE */
+#else /* SQLITE_SHELL_FIDDLE... */
+ shell_main_exit:
+#endif
return rc;
}
@@ -30993,7 +33913,7 @@ sqlite3_vfs * fiddle_db_vfs(const char *zDbName){
/* Only for emcc experimentation purposes. */
sqlite3 * fiddle_db_arg(sqlite3 *arg){
- oputf("fiddle_db_arg(%p)\n", (const void*)arg);
+ sqlite3_fprintf(stdout, "fiddle_db_arg(%p)\n", (const void*)arg);
return arg;
}
@@ -31030,7 +33950,7 @@ void fiddle_reset_db(void){
** Resolve problem reported in
** https://sqlite.org/forum/forumpost/0b41a25d65
*/
- oputz("Rolling back in-progress transaction.\n");
+ sqlite3_fputs("Rolling back in-progress transaction.\n", stdout);
sqlite3_exec(globalDb,"ROLLBACK", 0, 0, 0);
}
rc = sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);