From: larrybr Date: Thu, 16 Nov 2023 20:50:56 +0000 (+0000) Subject: Cherrypick changes to simplify and make more rational how console I/O package feature... X-Git-Tag: version-3.44.1~8^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=241677519aa1641779f8ec5a1ae37ebedcd09379;p=thirdparty%2Fsqlite.git Cherrypick changes to simplify and make more rational how console I/O package features are selected. FossilOrigin-Name: b20c9f1785e94af4b81dcbbc63c49a883ab4095ff251b19e814ab6ef7ff06f2b --- diff --git a/Makefile.msc b/Makefile.msc index 46675f120e..3a4d46b01f 100644 --- a/Makefile.msc +++ b/Makefile.msc @@ -2261,10 +2261,10 @@ keywordhash.h: $(TOP)\tool\mkkeywordhash.c mkkeywordhash.exe # Source files that go into making shell.c SHELL_SRC = \ $(TOP)\src\shell.c.in \ - $(TOP)\ext\misc\appendvfs.c \ - $(TOP)\ext\misc\completion.c \ $(TOP)\ext\consio\console_io.c \ $(TOP)\ext\consio\console_io.h \ + $(TOP)\ext\misc\appendvfs.c \ + $(TOP)\ext\misc\completion.c \ $(TOP)\ext\misc\base64.c \ $(TOP)\ext\misc\base85.c \ $(TOP)\ext\misc\decimal.c \ diff --git a/ext/consio/console_io.c b/ext/consio/console_io.c index 55b3f50d34..7ed06ccd31 100755 --- a/ext/consio/console_io.c +++ b/ext/consio/console_io.c @@ -18,8 +18,6 @@ # define SQLITE_CDECL #endif -#ifndef SQLITE_SHELL_FIDDLE - #ifndef SHELL_NO_SYSINC # include # include @@ -30,28 +28,32 @@ # include "sqlite3.h" #endif -#if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT -# ifndef SHELL_NO_SYSINC -# include -# include -# undef WIN32_LEAN_AND_MEAN -# define WIN32_LEAN_AND_MEAN -# include +#ifndef SQLITE_CIO_NO_TRANSLATE +# if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT +# ifndef SHELL_NO_SYSINC +# include +# include +# undef WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN +# include +# endif +# define CIO_WIN_WC_XLATE 1 /* Use WCHAR Windows APIs for console I/O */ +# else +# ifndef SHELL_NO_SYSINC +# include +# endif +# define CIO_WIN_WC_XLATE 0 /* Use plain C library stream I/O at console */ # endif -# define SHELL_CON_TRANSLATE 1 /* Use WCHAR Windows APIs for console I/O */ #else -# ifndef SHELL_NO_SYSINC -# include -# endif -# define SHELL_CON_TRANSLATE 0 /* Use plain C library stream I/O at console */ +# define CIO_WIN_WC_XLATE 0 /* Not exposing translation routines at all */ #endif -#if SHELL_CON_TRANSLATE +#if CIO_WIN_WC_XLATE /* Character used to represent a known-incomplete UTF-8 char group (�) */ static WCHAR cBadGroup = 0xfffd; #endif -#if SHELL_CON_TRANSLATE +#if CIO_WIN_WC_XLATE static HANDLE handleOfFile(FILE *pf){ int fileDesc = _fileno(pf); union { intptr_t osfh; HANDLE fh; } fid = { @@ -61,54 +63,51 @@ static HANDLE handleOfFile(FILE *pf){ } #endif +#ifndef SQLITE_CIO_NO_TRANSLATE typedef struct PerStreamTags { -#if SHELL_CON_TRANSLATE +# if CIO_WIN_WC_XLATE HANDLE hx; DWORD consMode; char acIncomplete[4]; -#else +# else short reachesConsole; -#endif +# endif FILE *pf; } PerStreamTags; /* Define NULL-like value for things which can validly be 0. */ -#define SHELL_INVALID_FILE_PTR ((FILE *)~0) -#if SHELL_CON_TRANSLATE -# define SHELL_INVALID_CONS_MODE 0xFFFF0000 -#endif +# define SHELL_INVALID_FILE_PTR ((FILE *)~0) +# if CIO_WIN_WC_XLATE +# define SHELL_INVALID_CONS_MODE 0xFFFF0000 +# endif -#if SHELL_CON_TRANSLATE -# define PST_INITIALIZER { INVALID_HANDLE_VALUE, SHELL_INVALID_CONS_MODE, \ +# 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 +# else +# define PST_INITIALIZER { 0, SHELL_INVALID_FILE_PTR } +# endif /* Quickly say whether a known output is going to the console. */ -#if SHELL_CON_TRANSLATE +# if CIO_WIN_WC_XLATE static short pstReachesConsole(PerStreamTags *ppst){ -# if SHELL_CON_TRANSLATE return (ppst->hx != INVALID_HANDLE_VALUE); +} # else - return (ppst->reachesConsole != 0); +# define pstReachesConsole(ppst) 0 # endif -} -#else -# define pstReachesConsole(ppst) 0 -#endif -#if SHELL_CON_TRANSLATE +# if CIO_WIN_WC_XLATE static void restoreConsoleArb(PerStreamTags *ppst){ if( pstReachesConsole(ppst) ) SetConsoleMode(ppst->hx, ppst->consMode); } -#else -# define restoreConsoleArb(ppst) -#endif +# 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 SHELL_CON_TRANSLATE +# if CIO_WIN_WC_XLATE short rv = 0; DWORD dwCM = SHELL_INVALID_CONS_MODE; HANDLE fh = handleOfFile(pf); @@ -119,21 +118,21 @@ static short streamOfConsole(FILE *pf, /* out */ PerStreamTags *ppst){ ppst->hx = (rv)? fh : INVALID_HANDLE_VALUE; ppst->consMode = dwCM; return rv; -#else +# else ppst->pf = pf; ppst->reachesConsole = ( (short)isatty(fileno(pf)) ); return ppst->reachesConsole; -#endif +# endif } -#if SHELL_CON_TRANSLATE +# if CIO_WIN_WC_XLATE /* Define console modes for use with the Windows Console API. */ -# define SHELL_CONI_MODE \ +# 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 \ +# define SHELL_CONO_MODE (ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT \ | ENABLE_VIRTUAL_TERMINAL_PROCESSING) -#endif +# endif typedef struct ConsoleInfo { PerStreamTags pstSetup[3]; @@ -153,19 +152,19 @@ static ConsoleInfo consoleInfo = { SQLITE_INTERNAL_LINKAGE FILE* invalidFileStream = (FILE *)~0; -#if SHELL_CON_TRANSLATE +# 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 +# else +# define maybeSetupAsConsole(ppst,odir) +# endif SQLITE_INTERNAL_LINKAGE void consoleRenewSetup(void){ -#if SHELL_CON_TRANSLATE +# if CIO_WIN_WC_XLATE int ix = 0; while( ix < 6 ){ PerStreamTags *ppst = (ix<3)? @@ -173,7 +172,7 @@ SQLITE_INTERNAL_LINKAGE void consoleRenewSetup(void){ maybeSetupAsConsole(ppst, (ix % 3)>0); ++ix; } -#endif +# endif } SQLITE_INTERNAL_LINKAGE StreamsAreConsole @@ -195,7 +194,7 @@ consoleClassifySetup( FILE *pfIn, FILE *pfOut, FILE *pfErr ){ } SQLITE_INTERNAL_LINKAGE void SQLITE_CDECL consoleRestore( void ){ -#if SHELL_CON_TRANSLATE +# if CIO_WIN_WC_XLATE static ConsoleInfo *pci = &consoleInfo; if( pci->sacSetup ){ int ix; @@ -206,10 +205,11 @@ SQLITE_INTERNAL_LINKAGE void SQLITE_CDECL consoleRestore( void ){ } } } -#endif +# endif } +#endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */ -#ifdef CONSIO_INPUT_REDIR +#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 @@ -227,6 +227,7 @@ static PerStreamTags * isKnownReadable(FILE *pf){ } #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 @@ -261,20 +262,22 @@ static FILE *designateEmitStream(FILE *pf, unsigned chix){ SQLITE_INTERNAL_LINKAGE FILE *setOutputStream(FILE *pf){ return designateEmitStream(pf, 1); } -#ifdef CONSIO_SET_ERROR_STREAM +# ifdef CONSIO_SET_ERROR_STREAM SQLITE_INTERNAL_LINKAGE FILE *setErrorStream(FILE *pf){ return designateEmitStream(pf, 2); } -#endif +# endif +#endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */ -#if SHELL_CON_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 +# 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); @@ -282,16 +285,15 @@ SQLITE_INTERNAL_LINKAGE void setBinaryMode(FILE *pf, short bFlush){ SQLITE_INTERNAL_LINKAGE void setTextMode(FILE *pf, short bFlush){ setModeFlushQ(pf, bFlush, _O_TEXT); } -#undef setModeFlushQ +# undef setModeFlushQ -#else /* defined(SQLITE_SHELL_FIDDLE) */ +#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_SHELL_FIDDLE) */ +#endif /* defined(SQLITE_CIO_NO_SETMODE) */ -#ifndef SQLITE_SHELL_FIDDLE - -#if SHELL_CON_TRANSLATE +#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){ @@ -324,9 +326,9 @@ static int conioVmPrintf(PerStreamTags *ppst, const char *zFormat, va_list ap){ return rv; }else return 0; } -#endif /* SHELL_CON_TRANSLATE */ +# endif /* CIO_WIN_WC_XLATE */ -#ifdef CONSIO_GET_EMIT_STREAM +# ifdef CONSIO_GET_EMIT_STREAM static PerStreamTags * getDesignatedEmitStream(FILE *pf, unsigned chix, PerStreamTags *ppst){ PerStreamTags *rv = isKnownWritable(pf); @@ -335,7 +337,7 @@ static PerStreamTags * getDesignatedEmitStream(FILE *pf, unsigned chix, streamOfConsole(pf, ppst); return ppst; } -#endif +# 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. @@ -375,22 +377,22 @@ SQLITE_INTERNAL_LINKAGE int oPrintfUtf8(const char *zFormat, ...){ int rv; FILE *pfOut; PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */ -#if SHELL_CON_TRANSLATE +# if CIO_WIN_WC_XLATE PerStreamTags *ppst = getEmitStreamInfo(1, &pst, &pfOut); -#else +# else getEmitStreamInfo(1, &pst, &pfOut); -#endif +# endif assert(zFormat!=0); va_start(ap, zFormat); -#if SHELL_CON_TRANSLATE +# if CIO_WIN_WC_XLATE if( pstReachesConsole(ppst) ){ rv = conioVmPrintf(ppst, zFormat, ap); }else{ -#endif +# endif rv = vfprintf(pfOut, zFormat, ap); -#if SHELL_CON_TRANSLATE +# if CIO_WIN_WC_XLATE } -#endif +# endif va_end(ap); return rv; } @@ -400,22 +402,22 @@ SQLITE_INTERNAL_LINKAGE int ePrintfUtf8(const char *zFormat, ...){ int rv; FILE *pfErr; PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */ -#if SHELL_CON_TRANSLATE +# if CIO_WIN_WC_XLATE PerStreamTags *ppst = getEmitStreamInfo(2, &pst, &pfErr); -#else +# else getEmitStreamInfo(2, &pst, &pfErr); -#endif +# endif assert(zFormat!=0); va_start(ap, zFormat); -#if SHELL_CON_TRANSLATE +# if CIO_WIN_WC_XLATE if( pstReachesConsole(ppst) ){ rv = conioVmPrintf(ppst, zFormat, ap); }else{ -#endif +# endif rv = vfprintf(pfErr, zFormat, ap); -#if SHELL_CON_TRANSLATE +# if CIO_WIN_WC_XLATE } -#endif +# endif va_end(ap); return rv; } @@ -424,37 +426,37 @@ SQLITE_INTERNAL_LINKAGE int fPrintfUtf8(FILE *pfO, const char *zFormat, ...){ va_list ap; int rv; PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */ -#if SHELL_CON_TRANSLATE +# if CIO_WIN_WC_XLATE PerStreamTags *ppst = getEmitStreamInfo(0, &pst, &pfO); -#else +# else getEmitStreamInfo(0, &pst, &pfO); -#endif +# endif assert(zFormat!=0); va_start(ap, zFormat); -#if SHELL_CON_TRANSLATE +# if CIO_WIN_WC_XLATE if( pstReachesConsole(ppst) ){ maybeSetupAsConsole(ppst, 1); rv = conioVmPrintf(ppst, zFormat, ap); if( 0 == isKnownWritable(ppst->pf) ) restoreConsoleArb(ppst); }else{ -#endif +# endif rv = vfprintf(pfO, zFormat, ap); -#if SHELL_CON_TRANSLATE +# if CIO_WIN_WC_XLATE } -#endif +# endif va_end(ap); return rv; } SQLITE_INTERNAL_LINKAGE int fPutsUtf8(const char *z, FILE *pfO){ PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */ -#if SHELL_CON_TRANSLATE +# if CIO_WIN_WC_XLATE PerStreamTags *ppst = getEmitStreamInfo(0, &pst, &pfO); -#else +# else getEmitStreamInfo(0, &pst, &pfO); -#endif +# endif assert(z!=0); -#if SHELL_CON_TRANSLATE +# if CIO_WIN_WC_XLATE if( pstReachesConsole(ppst) ){ int rv; maybeSetupAsConsole(ppst, 1); @@ -462,53 +464,54 @@ SQLITE_INTERNAL_LINKAGE int fPutsUtf8(const char *z, FILE *pfO){ if( 0 == isKnownWritable(ppst->pf) ) restoreConsoleArb(ppst); return rv; }else { -#endif +# endif return (fputs(z, pfO)<0)? 0 : (int)strlen(z); -#if SHELL_CON_TRANSLATE +# if CIO_WIN_WC_XLATE } -#endif +# endif } SQLITE_INTERNAL_LINKAGE int ePutsUtf8(const char *z){ FILE *pfErr; PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */ -#if SHELL_CON_TRANSLATE +# if CIO_WIN_WC_XLATE PerStreamTags *ppst = getEmitStreamInfo(2, &pst, &pfErr); -#else +# else getEmitStreamInfo(2, &pst, &pfErr); -#endif +# endif assert(z!=0); -#if SHELL_CON_TRANSLATE +# if CIO_WIN_WC_XLATE if( pstReachesConsole(ppst) ) return conZstrEmit(ppst, z, (int)strlen(z)); else { -#endif +# endif return (fputs(z, pfErr)<0)? 0 : (int)strlen(z); -#if SHELL_CON_TRANSLATE +# if CIO_WIN_WC_XLATE } -#endif +# endif } SQLITE_INTERNAL_LINKAGE int oPutsUtf8(const char *z){ FILE *pfOut; PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */ -#if SHELL_CON_TRANSLATE +# if CIO_WIN_WC_XLATE PerStreamTags *ppst = getEmitStreamInfo(1, &pst, &pfOut); -#else +# else getEmitStreamInfo(1, &pst, &pfOut); -#endif +# endif assert(z!=0); -#if SHELL_CON_TRANSLATE +# if CIO_WIN_WC_XLATE if( pstReachesConsole(ppst) ) return conZstrEmit(ppst, z, (int)strlen(z)); else { -#endif +# endif return (fputs(z, pfOut)<0)? 0 : (int)strlen(z); -#if SHELL_CON_TRANSLATE +# if CIO_WIN_WC_XLATE } -#endif +# endif } -#endif /* !defined(SQLITE_SHELL_FIDDLE) */ +#endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */ +#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<pf) ) restoreConsoleArb(ppst); return rv; }else { -#endif - return (int)fwrite(cBuf, 1, ncConsume, pfO); -#if SHELL_CON_TRANSLATE +# 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, long ctrlMask){ +oPutbUtf8(const char *cBuf, int nAccept){ FILE *pfOut; - const char *zPast = zSkipValidUtf8(cBuf, nAccept, ctrlMask); - int ncConsume = (int)(zPast - cBuf); PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */ -#if SHELL_CON_TRANSLATE +# if CIO_WIN_WC_XLATE PerStreamTags *ppst = getEmitStreamInfo(1, &pst, &pfOut); -#else +# else getEmitStreamInfo(1, &pst, &pfOut); -#endif -#if SHELL_CON_TRANSLATE +# endif +# if CIO_WIN_WC_XLATE if( pstReachesConsole(ppst) ){ - return conZstrEmit(ppst, cBuf, ncConsume); + return conZstrEmit(ppst, cBuf, nAccept); }else { -#endif - return (int)fwrite(cBuf, 1, ncConsume, pfOut); -#if SHELL_CON_TRANSLATE +# endif + return (int)fwrite(cBuf, 1, nAccept, pfOut); +# if CIO_WIN_WC_XLATE } -#endif +# endif } -#ifdef CONSIO_EPUTB +# ifdef CONSIO_EPUTB SQLITE_INTERNAL_LINKAGE int -ePutbUtf8(const char *cBuf, int nAccept, long ctrlMask){ +ePutbUtf8(const char *cBuf, int nAccept){ FILE *pfErr; - const char *zPast = zSkipValidUtf8(cBuf, nAccept, ctrlMask); - int ncConsume = (int)(zPast - cBuf); PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */ PerStreamTags *ppst = getEmitStreamInfo(2, &pst, &pfErr); -# if SHELL_CON_TRANSLATE +# if CIO_WIN_WC_XLATE if( pstReachesConsole(ppst) ){ - return conZstrEmit(ppst, cBuf, ncConsume); + return conZstrEmit(ppst, cBuf, nAccept); }else { -# endif - return (int)fwrite(cBuf, 1, ncConsume, pfErr); -# if SHELL_CON_TRANSLATE +# endif + return (int)fwrite(cBuf, 1, nAccept, pfErr); +# if CIO_WIN_WC_XLATE } -# endif +# endif } -#endif /* defined(CONSIO_EPUTB) */ +# endif /* defined(CONSIO_EPUTB) */ SQLITE_INTERNAL_LINKAGE char* fGetsUtf8(char *cBuf, int ncMax, FILE *pfIn){ if( pfIn==0 ) pfIn = stdin; -#if SHELL_CON_TRANSLATE +# if CIO_WIN_WC_XLATE if( pfIn == consoleInfo.pstSetup[0].pf && (consoleInfo.sacSetup & SAC_InConsole)!=0 ){ -# if SHELL_CON_TRANSLATE==1 -# define SHELL_GULP 150 /* Count of WCHARS to be gulped at a time */ +# 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 +# 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 ){ @@ -685,14 +663,15 @@ SQLITE_INTERNAL_LINKAGE char* fGetsUtf8(char *cBuf, int ncMax, FILE *pfIn){ cBuf[noc] = 0; return cBuf; }else return 0; -# endif +# endif }else{ -#endif +# endif return fgets(cBuf, ncMax, pfIn); -#if SHELL_CON_TRANSLATE +# if CIO_WIN_WC_XLATE } -#endif +# endif } +#endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */ +#undef CIO_WIN_WC_XLATE #undef SHELL_INVALID_FILE_PTR -#endif /* !defined(SQLITE_SHELL_FIDDLE) */ diff --git a/ext/consio/console_io.h b/ext/consio/console_io.h index de72cd945c..4fb51d24d8 100644 --- a/ext/consio/console_io.h +++ b/ext/consio/console_io.h @@ -35,6 +35,8 @@ # include "sqlite3.h" #endif +#ifndef SQLITE_CIO_NO_CLASSIFY + /* Define enum for use with following function. */ typedef enum StreamsAreConsole { SAC_NoConsole = 0, @@ -42,7 +44,6 @@ typedef enum StreamsAreConsole { SAC_AnyConsole = 0x7 } StreamsAreConsole; -#ifndef SQLITE_SHELL_FIDDLE /* ** Classify the three standard I/O streams according to whether ** they are connected to a console attached to the process. @@ -92,6 +93,13 @@ SQLITE_INTERNAL_LINKAGE void consoleRenewSetup(void); */ SQLITE_INTERNAL_LINKAGE void SQLITE_CDECL consoleRestore( void ); +#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. @@ -109,10 +117,15 @@ SQLITE_INTERNAL_LINKAGE void SQLITE_CDECL consoleRestore( void ); */ SQLITE_INTERNAL_LINKAGE FILE *invalidFileStream; SQLITE_INTERNAL_LINKAGE FILE *setOutputStream(FILE *pf); -#ifdef CONSIO_SET_ERROR_STREAM +# ifdef CONSIO_SET_ERROR_STREAM SQLITE_INTERNAL_LINKAGE FILE *setErrorStream(FILE *pf); -#endif +# 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 @@ -141,38 +154,19 @@ SQLITE_INTERNAL_LINKAGE int ePutsUtf8(const char *z); /* ** Emit output like fPutsUtf8(), except that the length of the -** accepted char or character sequence may be limited by nAccept. -** -** The magnitude and sign of nAccept control what nAccept limits. -** If positive, nAccept limits the number of char's accepted. -** If negative, it limits the number of valid input characters. -** Obtain the behavior of {f,o,e}PutsUtf8 with nAccept==INT_MAX. +** accepted char or character sequence is limited by nAccept. ** ** Returns the number of accepted char values. -** -** When ctrlMask!=0, it specifies a set of control characters not -** accepted as input, so that cBuf[abs(N)] on return will be one -** of the non-accepted characters unless nAccept limited the scan. -** Each bit in ctrlMask, 1< 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) @@ -266,15 +274,16 @@ INCLUDE ../ext/consio/console_io.c # 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. */ -# define sputz(s,z) fputs(z,s) -# define sputf fprintf +# define sputz(fp,z) fputs(z,fp) +# define sputf(fp,fmt, ...) fprintf(fp,fmt,__VA_ARGS__) # define oputz(z) fputs(z,stdout) -# define oputf oprintf +# define oputf(fmt, ...) printf(fmt,__VA_ARGS__) # define eputz(z) fputs(z,stderr) -# define eputf eprintf +# define eputf(fmt, ...) fprintf(stderr,fmt,__VA_ARGS__) +# define oputb(buf,na) fwrite(buf,1,na,stdout) #endif /* True if the timer is enabled */ @@ -1869,7 +1878,7 @@ static void output_c_string(const char *z){ 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 ) oPutbUtf8(z, (int)(pcEnd-z), 0); + if( pcEnd > z ) oputb(z, (int)(pcEnd-z)); if( (c = *pcEnd)==0 ) break; ++pcEnd; switch( c ){ @@ -1916,7 +1925,7 @@ static void output_json_string(const char *z, i64 n){ const char *pcPast = zSkipValidUtf8(z, (int)(pcLimit-z), ctrlMask); const char *pcEnd = (pcDQBS && pcDQBS < pcPast)? pcDQBS : pcPast; if( pcEnd > z ){ - oPutbUtf8(z, (int)(pcEnd-z), 0); + oputb(z, (int)(pcEnd-z)); z = pcEnd; } if( z >= pcLimit ) break; @@ -9188,7 +9197,7 @@ static int do_meta_command(char *zLine, ShellState *p){ && cli_strcmp(zFile,"on")!=0 && cli_strcmp(zFile,"off")!=0 ){ - sputf(stdout, "cannot set .log to anything other" + sputz(stdout, "cannot set .log to anything other" " than \"on\" or \"off\"\n"); zFile = "off"; } @@ -11875,7 +11884,7 @@ static char *cmdline_option_value(int argc, char **argv, int i){ } static void sayAbnormalExit(void){ - if( seenInterrupt ) eputf("Program interrupted.\n"); + if( seenInterrupt ) eputz("Program interrupted.\n"); } #ifndef SQLITE_SHELL_IS_UTF8