Fossil SCM

Update the built-in SQLite to version 3.44.1.

drh 2023-11-22 20:22 trunk
Commit 2edeeee4a8ae3028070590fc9a95e2c225153c2066f655577bd8d23b99497f26
3 files changed +1862 -1174 +176 -79 +55 -12
+1862 -1174
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -250,33 +250,1023 @@
250250
#define WIN32_LEAN_AND_MEAN
251251
#include <windows.h>
252252
253253
/* string conversion routines only needed on Win32 */
254254
extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
255
-extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
256
-extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
257255
extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
258256
#endif
259257
260
-/* On Windows, we normally run with output mode of TEXT so that \n characters
261
-** are automatically translated into \r\n. However, this behavior needs
262
-** to be disabled in some cases (ex: when generating CSV output and when
263
-** rendering quoted strings that contain \n characters). The following
264
-** routines take care of that.
265
-*/
266
-#if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
267
-static void setBinaryMode(FILE *file, int isOutput){
268
- if( isOutput ) fflush(file);
269
- _setmode(_fileno(file), _O_BINARY);
270
-}
271
-static void setTextMode(FILE *file, int isOutput){
272
- if( isOutput ) fflush(file);
273
- _setmode(_fileno(file), _O_TEXT);
274
-}
258
+/* Use console I/O package as a direct INCLUDE. */
259
+#define SQLITE_INTERNAL_LINKAGE static
260
+
261
+#ifdef SQLITE_SHELL_FIDDLE
262
+/* Deselect most features from the console I/O package for Fiddle. */
263
+# define SQLITE_CIO_NO_REDIRECT
264
+# define SQLITE_CIO_NO_CLASSIFY
265
+# define SQLITE_CIO_NO_TRANSLATE
266
+# define SQLITE_CIO_NO_SETMODE
267
+#endif
268
+/************************* Begin ../ext/consio/console_io.h ******************/
269
+/*
270
+** 2023 November 1
271
+**
272
+** The author disclaims copyright to this source code. In place of
273
+** a legal notice, here is a blessing:
274
+**
275
+** May you do good and not evil.
276
+** May you find forgiveness for yourself and forgive others.
277
+** May you share freely, never taking more than you give.
278
+**
279
+********************************************************************************
280
+** This file exposes various interfaces used for console and other I/O
281
+** by the SQLite project command-line tools. These interfaces are used
282
+** at either source conglomeration time, compilation time, or run time.
283
+** This source provides for either inclusion into conglomerated,
284
+** "single-source" forms or separate compilation then linking.
285
+**
286
+** Platform dependencies are "hidden" here by various stratagems so
287
+** that, provided certain conditions are met, the programs using this
288
+** source or object code compiled from it need no explicit conditional
289
+** compilation in their source for their console and stream I/O.
290
+**
291
+** The symbols and functionality exposed here are not a public API.
292
+** This code may change in tandem with other project code as needed.
293
+**
294
+** When this .h file and its companion .c are directly incorporated into
295
+** a source conglomeration (such as shell.c), the preprocessor symbol
296
+** CIO_WIN_WC_XLATE is defined as 0 or 1, reflecting whether console I/O
297
+** translation for Windows is effected for the build.
298
+*/
299
+
300
+#ifndef SQLITE_INTERNAL_LINKAGE
301
+# define SQLITE_INTERNAL_LINKAGE extern /* external to translation unit */
302
+# include <stdio.h>
303
+#else
304
+# define SHELL_NO_SYSINC /* Better yet, modify mkshellc.tcl for this. */
305
+#endif
306
+
307
+#ifndef SQLITE3_H
308
+/* # include "sqlite3.h" */
309
+#endif
310
+
311
+#ifndef SQLITE_CIO_NO_CLASSIFY
312
+
313
+/* Define enum for use with following function. */
314
+typedef enum StreamsAreConsole {
315
+ SAC_NoConsole = 0,
316
+ SAC_InConsole = 1, SAC_OutConsole = 2, SAC_ErrConsole = 4,
317
+ SAC_AnyConsole = 0x7
318
+} StreamsAreConsole;
319
+
320
+/*
321
+** Classify the three standard I/O streams according to whether
322
+** they are connected to a console attached to the process.
323
+**
324
+** Returns the bit-wise OR of SAC_{In,Out,Err}Console values,
325
+** or SAC_NoConsole if none of the streams reaches a console.
326
+**
327
+** This function should be called before any I/O is done with
328
+** the given streams. As a side-effect, the given inputs are
329
+** recorded so that later I/O operations on them may be done
330
+** differently than the C library FILE* I/O would be done,
331
+** iff the stream is used for the I/O functions that follow,
332
+** and to support the ones that use an implicit stream.
333
+**
334
+** On some platforms, stream or console mode alteration (aka
335
+** "Setup") may be made which is undone by consoleRestore().
336
+*/
337
+SQLITE_INTERNAL_LINKAGE StreamsAreConsole
338
+consoleClassifySetup( FILE *pfIn, FILE *pfOut, FILE *pfErr );
339
+/* A usual call for convenience: */
340
+#define SQLITE_STD_CONSOLE_INIT() consoleClassifySetup(stdin,stdout,stderr)
341
+
342
+/*
343
+** After an initial call to consoleClassifySetup(...), renew
344
+** the same setup it effected. (A call not after is an error.)
345
+** This will restore state altered by consoleRestore();
346
+**
347
+** Applications which run an inferior (child) process which
348
+** inherits the same I/O streams may call this function after
349
+** such a process exits to guard against console mode changes.
350
+*/
351
+SQLITE_INTERNAL_LINKAGE void consoleRenewSetup(void);
352
+
353
+/*
354
+** Undo any side-effects left by consoleClassifySetup(...).
355
+**
356
+** This should be called after consoleClassifySetup() and
357
+** before the process terminates normally. It is suitable
358
+** for use with the atexit() C library procedure. After
359
+** this call, no console I/O should be done until one of
360
+** console{Classify or Renew}Setup(...) is called again.
361
+**
362
+** Applications which run an inferior (child) process that
363
+** inherits the same I/O streams might call this procedure
364
+** before so that said process will have a console setup
365
+** however users have configured it or come to expect.
366
+*/
367
+SQLITE_INTERNAL_LINKAGE void SQLITE_CDECL consoleRestore( void );
368
+
369
+#else /* defined(SQLITE_CIO_NO_CLASSIFY) */
370
+# define consoleClassifySetup(i,o,e)
371
+# define consoleRenewSetup()
372
+# define consoleRestore()
373
+#endif /* defined(SQLITE_CIO_NO_CLASSIFY) */
374
+
375
+#ifndef SQLITE_CIO_NO_REDIRECT
376
+/*
377
+** Set stream to be used for the functions below which write
378
+** to "the designated X stream", where X is Output or Error.
379
+** Returns the previous value.
380
+**
381
+** Alternatively, pass the special value, invalidFileStream,
382
+** to get the designated stream value without setting it.
383
+**
384
+** Before the designated streams are set, they default to
385
+** those passed to consoleClassifySetup(...), and before
386
+** that is called they default to stdout and stderr.
387
+**
388
+** It is error to close a stream so designated, then, without
389
+** designating another, use the corresponding {o,e}Emit(...).
390
+*/
391
+SQLITE_INTERNAL_LINKAGE FILE *invalidFileStream;
392
+SQLITE_INTERNAL_LINKAGE FILE *setOutputStream(FILE *pf);
393
+# ifdef CONSIO_SET_ERROR_STREAM
394
+SQLITE_INTERNAL_LINKAGE FILE *setErrorStream(FILE *pf);
395
+# endif
396
+#else
397
+# define setOutputStream(pf)
398
+# define setErrorStream(pf)
399
+#endif /* !defined(SQLITE_CIO_NO_REDIRECT) */
400
+
401
+#ifndef SQLITE_CIO_NO_TRANSLATE
402
+/*
403
+** Emit output like fprintf(). If the output is going to the
404
+** console and translation from UTF-8 is necessary, perform
405
+** the needed translation. Otherwise, write formatted output
406
+** to the provided stream almost as-is, possibly with newline
407
+** translation as specified by set{Binary,Text}Mode().
408
+*/
409
+SQLITE_INTERNAL_LINKAGE int fPrintfUtf8(FILE *pfO, const char *zFormat, ...);
410
+/* Like fPrintfUtf8 except stream is always the designated output. */
411
+SQLITE_INTERNAL_LINKAGE int oPrintfUtf8(const char *zFormat, ...);
412
+/* Like fPrintfUtf8 except stream is always the designated error. */
413
+SQLITE_INTERNAL_LINKAGE int ePrintfUtf8(const char *zFormat, ...);
414
+
415
+/*
416
+** Emit output like fputs(). If the output is going to the
417
+** console and translation from UTF-8 is necessary, perform
418
+** the needed translation. Otherwise, write given text to the
419
+** provided stream almost as-is, possibly with newline
420
+** translation as specified by set{Binary,Text}Mode().
421
+*/
422
+SQLITE_INTERNAL_LINKAGE int fPutsUtf8(const char *z, FILE *pfO);
423
+/* Like fPutsUtf8 except stream is always the designated output. */
424
+SQLITE_INTERNAL_LINKAGE int oPutsUtf8(const char *z);
425
+/* Like fPutsUtf8 except stream is always the designated error. */
426
+SQLITE_INTERNAL_LINKAGE int ePutsUtf8(const char *z);
427
+
428
+/*
429
+** Emit output like fPutsUtf8(), except that the length of the
430
+** accepted char or character sequence is limited by nAccept.
431
+**
432
+** Returns the number of accepted char values.
433
+*/
434
+#ifdef CONSIO_SPUTB
435
+SQLITE_INTERNAL_LINKAGE int
436
+fPutbUtf8(FILE *pfOut, const char *cBuf, int nAccept);
437
+#endif
438
+/* Like fPutbUtf8 except stream is always the designated output. */
439
+SQLITE_INTERNAL_LINKAGE int
440
+oPutbUtf8(const char *cBuf, int nAccept);
441
+/* Like fPutbUtf8 except stream is always the designated error. */
442
+#ifdef CONSIO_EPUTB
443
+SQLITE_INTERNAL_LINKAGE int
444
+ePutbUtf8(const char *cBuf, int nAccept);
445
+#endif
446
+
447
+/*
448
+** Collect input like fgets(...) with special provisions for input
449
+** from the console on platforms that require same. Defers to the
450
+** C library fgets() when input is not from the console. Newline
451
+** translation may be done as set by set{Binary,Text}Mode(). As a
452
+** convenience, pfIn==NULL is treated as stdin.
453
+*/
454
+SQLITE_INTERNAL_LINKAGE char* fGetsUtf8(char *cBuf, int ncMax, FILE *pfIn);
455
+/* Like fGetsUtf8 except stream is always the designated input. */
456
+/* SQLITE_INTERNAL_LINKAGE char* iGetsUtf8(char *cBuf, int ncMax); */
457
+
458
+#endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
459
+
460
+#ifndef SQLITE_CIO_NO_SETMODE
461
+/*
462
+** Set given stream for binary mode, where newline translation is
463
+** not done, or for text mode where, for some platforms, newlines
464
+** are translated to the platform's conventional char sequence.
465
+** If bFlush true, flush the stream.
466
+**
467
+** An additional side-effect is that if the stream is one passed
468
+** to consoleClassifySetup() as an output, it is flushed first.
469
+**
470
+** Note that binary/text mode has no effect on console I/O
471
+** translation. On all platforms, newline to the console starts
472
+** a new line and CR,LF chars from the console become a newline.
473
+*/
474
+SQLITE_INTERNAL_LINKAGE void setBinaryMode(FILE *, short bFlush);
475
+SQLITE_INTERNAL_LINKAGE void setTextMode(FILE *, short bFlush);
476
+#endif
477
+
478
+#ifdef SQLITE_CIO_PROMPTED_IN
479
+typedef struct Prompts {
480
+ int numPrompts;
481
+ const char **azPrompts;
482
+} Prompts;
483
+
484
+/*
485
+** Macros for use of a line editor.
486
+**
487
+** The following macros define operations involving use of a
488
+** line-editing library or simple console interaction.
489
+** A "T" argument is a text (char *) buffer or filename.
490
+** A "N" argument is an integer.
491
+**
492
+** SHELL_ADD_HISTORY(T) // Record text as line(s) of history.
493
+** SHELL_READ_HISTORY(T) // Read history from file named by T.
494
+** SHELL_WRITE_HISTORY(T) // Write history to file named by T.
495
+** SHELL_STIFLE_HISTORY(N) // Limit history to N entries.
496
+**
497
+** A console program which does interactive console input is
498
+** expected to call:
499
+** SHELL_READ_HISTORY(T) before collecting such input;
500
+** SHELL_ADD_HISTORY(T) as record-worthy input is taken;
501
+** SHELL_STIFLE_HISTORY(N) after console input ceases; then
502
+** SHELL_WRITE_HISTORY(T) before the program exits.
503
+*/
504
+
505
+/*
506
+** Retrieve a single line of input text from an input stream.
507
+**
508
+** If pfIn is the input stream passed to consoleClassifySetup(),
509
+** and azPrompt is not NULL, then a prompt is issued before the
510
+** line is collected, as selected by the isContinuation flag.
511
+** Array azPrompt[{0,1}] holds the {main,continuation} prompt.
512
+**
513
+** If zBufPrior is not NULL then it is a buffer from a prior
514
+** call to this routine that can be reused, or will be freed.
515
+**
516
+** The result is stored in space obtained from malloc() and
517
+** must either be freed by the caller or else passed back to
518
+** this function as zBufPrior for reuse.
519
+**
520
+** This function may call upon services of a line-editing
521
+** library to interactively collect line edited input.
522
+*/
523
+SQLITE_INTERNAL_LINKAGE char *
524
+shellGetLine(FILE *pfIn, char *zBufPrior, int nLen,
525
+ short isContinuation, Prompts azPrompt);
526
+#endif /* defined(SQLITE_CIO_PROMPTED_IN) */
527
+/*
528
+** TBD: Define an interface for application(s) to generate
529
+** completion candidates for use by the line-editor.
530
+**
531
+** This may be premature; the CLI is the only application
532
+** that does this. Yet, getting line-editing melded into
533
+** console I/O is desirable because a line-editing library
534
+** may have to establish console operating mode, possibly
535
+** in a way that interferes with the above functionality.
536
+*/
537
+
538
+#if !(defined(SQLITE_CIO_NO_UTF8SCAN)&&defined(SQLITE_CIO_NO_TRANSLATE))
539
+/* Skip over as much z[] input char sequence as is valid UTF-8,
540
+** limited per nAccept char's or whole characters and containing
541
+** no char cn such that ((1<<cn) & ccm)!=0. On return, the
542
+** sequence z:return (inclusive:exclusive) is validated UTF-8.
543
+** Limit: nAccept>=0 => char count, nAccept<0 => character
544
+ */
545
+SQLITE_INTERNAL_LINKAGE const char*
546
+zSkipValidUtf8(const char *z, int nAccept, long ccm);
547
+
548
+#endif
549
+
550
+/************************* End ../ext/consio/console_io.h ********************/
551
+/************************* Begin ../ext/consio/console_io.c ******************/
552
+/*
553
+** 2023 November 4
554
+**
555
+** The author disclaims copyright to this source code. In place of
556
+** a legal notice, here is a blessing:
557
+**
558
+** May you do good and not evil.
559
+** May you find forgiveness for yourself and forgive others.
560
+** May you share freely, never taking more than you give.
561
+**
562
+********************************************************************************
563
+** This file implements various interfaces used for console and stream I/O
564
+** by the SQLite project command-line tools, as explained in console_io.h .
565
+** Functions prefixed by "SQLITE_INTERNAL_LINKAGE" behave as described there.
566
+*/
567
+
568
+#ifndef SQLITE_CDECL
569
+# define SQLITE_CDECL
570
+#endif
571
+
572
+#ifndef SHELL_NO_SYSINC
573
+# include <stdarg.h>
574
+# include <string.h>
575
+# include <stdlib.h>
576
+# include <limits.h>
577
+# include <assert.h>
578
+# include "console_io.h"
579
+/* # include "sqlite3.h" */
580
+#endif
581
+
582
+#ifndef SQLITE_CIO_NO_TRANSLATE
583
+# if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
584
+# ifndef SHELL_NO_SYSINC
585
+# include <io.h>
586
+# include <fcntl.h>
587
+# undef WIN32_LEAN_AND_MEAN
588
+# define WIN32_LEAN_AND_MEAN
589
+# include <windows.h>
590
+# endif
591
+# define CIO_WIN_WC_XLATE 1 /* Use WCHAR Windows APIs for console I/O */
592
+# else
593
+# ifndef SHELL_NO_SYSINC
594
+# include <unistd.h>
595
+# endif
596
+# define CIO_WIN_WC_XLATE 0 /* Use plain C library stream I/O at console */
597
+# endif
598
+#else
599
+# define CIO_WIN_WC_XLATE 0 /* Not exposing translation routines at all */
600
+#endif
601
+
602
+#if CIO_WIN_WC_XLATE
603
+/* Character used to represent a known-incomplete UTF-8 char group (�) */
604
+static WCHAR cBadGroup = 0xfffd;
605
+#endif
606
+
607
+#if CIO_WIN_WC_XLATE
608
+static HANDLE handleOfFile(FILE *pf){
609
+ int fileDesc = _fileno(pf);
610
+ union { intptr_t osfh; HANDLE fh; } fid = {
611
+ (fileDesc>=0)? _get_osfhandle(fileDesc) : (intptr_t)INVALID_HANDLE_VALUE
612
+ };
613
+ return fid.fh;
614
+}
615
+#endif
616
+
617
+#ifndef SQLITE_CIO_NO_TRANSLATE
618
+typedef struct PerStreamTags {
619
+# if CIO_WIN_WC_XLATE
620
+ HANDLE hx;
621
+ DWORD consMode;
622
+ char acIncomplete[4];
623
+# else
624
+ short reachesConsole;
625
+# endif
626
+ FILE *pf;
627
+} PerStreamTags;
628
+
629
+/* Define NULL-like value for things which can validly be 0. */
630
+# define SHELL_INVALID_FILE_PTR ((FILE *)~0)
631
+# if CIO_WIN_WC_XLATE
632
+# define SHELL_INVALID_CONS_MODE 0xFFFF0000
633
+# endif
634
+
635
+# if CIO_WIN_WC_XLATE
636
+# define PST_INITIALIZER { INVALID_HANDLE_VALUE, SHELL_INVALID_CONS_MODE, \
637
+ {0,0,0,0}, SHELL_INVALID_FILE_PTR }
638
+# else
639
+# define PST_INITIALIZER { 0, SHELL_INVALID_FILE_PTR }
640
+# endif
641
+
642
+/* Quickly say whether a known output is going to the console. */
643
+# if CIO_WIN_WC_XLATE
644
+static short pstReachesConsole(PerStreamTags *ppst){
645
+ return (ppst->hx != INVALID_HANDLE_VALUE);
646
+}
647
+# else
648
+# define pstReachesConsole(ppst) 0
649
+# endif
650
+
651
+# if CIO_WIN_WC_XLATE
652
+static void restoreConsoleArb(PerStreamTags *ppst){
653
+ if( pstReachesConsole(ppst) ) SetConsoleMode(ppst->hx, ppst->consMode);
654
+}
655
+# else
656
+# define restoreConsoleArb(ppst)
657
+# endif
658
+
659
+/* Say whether FILE* appears to be a console, collect associated info. */
660
+static short streamOfConsole(FILE *pf, /* out */ PerStreamTags *ppst){
661
+# if CIO_WIN_WC_XLATE
662
+ short rv = 0;
663
+ DWORD dwCM = SHELL_INVALID_CONS_MODE;
664
+ HANDLE fh = handleOfFile(pf);
665
+ ppst->pf = pf;
666
+ if( INVALID_HANDLE_VALUE != fh ){
667
+ rv = (GetFileType(fh) == FILE_TYPE_CHAR && GetConsoleMode(fh,&dwCM));
668
+ }
669
+ ppst->hx = (rv)? fh : INVALID_HANDLE_VALUE;
670
+ ppst->consMode = dwCM;
671
+ return rv;
672
+# else
673
+ ppst->pf = pf;
674
+ ppst->reachesConsole = ( (short)isatty(fileno(pf)) );
675
+ return ppst->reachesConsole;
676
+# endif
677
+}
678
+
679
+# if CIO_WIN_WC_XLATE
680
+/* Define console modes for use with the Windows Console API. */
681
+# define SHELL_CONI_MODE \
682
+ (ENABLE_ECHO_INPUT | ENABLE_INSERT_MODE | ENABLE_LINE_INPUT | 0x80 \
683
+ | ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS | ENABLE_PROCESSED_INPUT)
684
+# define SHELL_CONO_MODE (ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT \
685
+ | ENABLE_VIRTUAL_TERMINAL_PROCESSING)
686
+# endif
687
+
688
+typedef struct ConsoleInfo {
689
+ PerStreamTags pstSetup[3];
690
+ PerStreamTags pstDesignated[3];
691
+ StreamsAreConsole sacSetup;
692
+} ConsoleInfo;
693
+
694
+static short isValidStreamInfo(PerStreamTags *ppst){
695
+ return (ppst->pf != SHELL_INVALID_FILE_PTR);
696
+}
697
+
698
+static ConsoleInfo consoleInfo = {
699
+ { /* pstSetup */ PST_INITIALIZER, PST_INITIALIZER, PST_INITIALIZER },
700
+ { /* pstDesignated[] */ PST_INITIALIZER, PST_INITIALIZER, PST_INITIALIZER },
701
+ SAC_NoConsole /* sacSetup */
702
+};
703
+
704
+SQLITE_INTERNAL_LINKAGE FILE* invalidFileStream = (FILE *)~0;
705
+
706
+# if CIO_WIN_WC_XLATE
707
+static void maybeSetupAsConsole(PerStreamTags *ppst, short odir){
708
+ if( pstReachesConsole(ppst) ){
709
+ DWORD cm = odir? SHELL_CONO_MODE : SHELL_CONI_MODE;
710
+ SetConsoleMode(ppst->hx, cm);
711
+ }
712
+}
713
+# else
714
+# define maybeSetupAsConsole(ppst,odir)
715
+# endif
716
+
717
+SQLITE_INTERNAL_LINKAGE void consoleRenewSetup(void){
718
+# if CIO_WIN_WC_XLATE
719
+ int ix = 0;
720
+ while( ix < 6 ){
721
+ PerStreamTags *ppst = (ix<3)?
722
+ &consoleInfo.pstSetup[ix] : &consoleInfo.pstDesignated[ix-3];
723
+ maybeSetupAsConsole(ppst, (ix % 3)>0);
724
+ ++ix;
725
+ }
726
+# endif
727
+}
728
+
729
+SQLITE_INTERNAL_LINKAGE StreamsAreConsole
730
+consoleClassifySetup( FILE *pfIn, FILE *pfOut, FILE *pfErr ){
731
+ StreamsAreConsole rv = SAC_NoConsole;
732
+ FILE* apf[3] = { pfIn, pfOut, pfErr };
733
+ int ix;
734
+ for( ix = 2; ix >= 0; --ix ){
735
+ PerStreamTags *ppst = &consoleInfo.pstSetup[ix];
736
+ if( streamOfConsole(apf[ix], ppst) ){
737
+ rv |= (SAC_InConsole<<ix);
738
+ }
739
+ consoleInfo.pstDesignated[ix] = *ppst;
740
+ if( ix > 0 ) fflush(apf[ix]);
741
+ }
742
+ consoleInfo.sacSetup = rv;
743
+ consoleRenewSetup();
744
+ return rv;
745
+}
746
+
747
+SQLITE_INTERNAL_LINKAGE void SQLITE_CDECL consoleRestore( void ){
748
+# if CIO_WIN_WC_XLATE
749
+ static ConsoleInfo *pci = &consoleInfo;
750
+ if( pci->sacSetup ){
751
+ int ix;
752
+ for( ix=0; ix<3; ++ix ){
753
+ if( pci->sacSetup & (SAC_InConsole<<ix) ){
754
+ PerStreamTags *ppst = &pci->pstSetup[ix];
755
+ SetConsoleMode(ppst->hx, ppst->consMode);
756
+ }
757
+ }
758
+ }
759
+# endif
760
+}
761
+#endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
762
+
763
+#ifdef SQLITE_CIO_INPUT_REDIR
764
+/* Say whether given FILE* is among those known, via either
765
+** consoleClassifySetup() or set{Output,Error}Stream, as
766
+** readable, and return an associated PerStreamTags pointer
767
+** if so. Otherwise, return 0.
768
+*/
769
+static PerStreamTags * isKnownReadable(FILE *pf){
770
+ static PerStreamTags *apst[] = {
771
+ &consoleInfo.pstDesignated[0], &consoleInfo.pstSetup[0], 0
772
+ };
773
+ int ix = 0;
774
+ do {
775
+ if( apst[ix]->pf == pf ) break;
776
+ } while( apst[++ix] != 0 );
777
+ return apst[ix];
778
+}
779
+#endif
780
+
781
+#ifndef SQLITE_CIO_NO_TRANSLATE
782
+/* Say whether given FILE* is among those known, via either
783
+** consoleClassifySetup() or set{Output,Error}Stream, as
784
+** writable, and return an associated PerStreamTags pointer
785
+** if so. Otherwise, return 0.
786
+*/
787
+static PerStreamTags * isKnownWritable(FILE *pf){
788
+ static PerStreamTags *apst[] = {
789
+ &consoleInfo.pstDesignated[1], &consoleInfo.pstDesignated[2],
790
+ &consoleInfo.pstSetup[1], &consoleInfo.pstSetup[2], 0
791
+ };
792
+ int ix = 0;
793
+ do {
794
+ if( apst[ix]->pf == pf ) break;
795
+ } while( apst[++ix] != 0 );
796
+ return apst[ix];
797
+}
798
+
799
+static FILE *designateEmitStream(FILE *pf, unsigned chix){
800
+ FILE *rv = consoleInfo.pstDesignated[chix].pf;
801
+ if( pf == invalidFileStream ) return rv;
802
+ else{
803
+ /* Setting a possibly new output stream. */
804
+ PerStreamTags *ppst = isKnownWritable(pf);
805
+ if( ppst != 0 ){
806
+ PerStreamTags pst = *ppst;
807
+ consoleInfo.pstDesignated[chix] = pst;
808
+ }else streamOfConsole(pf, &consoleInfo.pstDesignated[chix]);
809
+ }
810
+ return rv;
811
+}
812
+
813
+SQLITE_INTERNAL_LINKAGE FILE *setOutputStream(FILE *pf){
814
+ return designateEmitStream(pf, 1);
815
+}
816
+# ifdef CONSIO_SET_ERROR_STREAM
817
+SQLITE_INTERNAL_LINKAGE FILE *setErrorStream(FILE *pf){
818
+ return designateEmitStream(pf, 2);
819
+}
820
+# endif
821
+#endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
822
+
823
+#ifndef SQLITE_CIO_NO_SETMODE
824
+# if CIO_WIN_WC_XLATE
825
+static void setModeFlushQ(FILE *pf, short bFlush, int mode){
826
+ if( bFlush ) fflush(pf);
827
+ _setmode(_fileno(pf), mode);
828
+}
829
+# else
830
+# define setModeFlushQ(f, b, m) if(b) fflush(f)
831
+# endif
832
+
833
+SQLITE_INTERNAL_LINKAGE void setBinaryMode(FILE *pf, short bFlush){
834
+ setModeFlushQ(pf, bFlush, _O_BINARY);
835
+}
836
+SQLITE_INTERNAL_LINKAGE void setTextMode(FILE *pf, short bFlush){
837
+ setModeFlushQ(pf, bFlush, _O_TEXT);
838
+}
839
+# undef setModeFlushQ
840
+
841
+#else /* defined(SQLITE_CIO_NO_SETMODE) */
842
+# define setBinaryMode(f, bFlush) do{ if((bFlush)) fflush(f); }while(0)
843
+# define setTextMode(f, bFlush) do{ if((bFlush)) fflush(f); }while(0)
844
+#endif /* defined(SQLITE_CIO_NO_SETMODE) */
845
+
846
+#ifndef SQLITE_CIO_NO_TRANSLATE
847
+# if CIO_WIN_WC_XLATE
848
+/* Write buffer cBuf as output to stream known to reach console,
849
+** limited to ncTake char's. Return ncTake on success, else 0. */
850
+static int conZstrEmit(PerStreamTags *ppst, const char *z, int ncTake){
851
+ int rv = 0;
852
+ if( z!=NULL ){
853
+ int nwc = MultiByteToWideChar(CP_UTF8,0, z,ncTake, 0,0);
854
+ if( nwc > 0 ){
855
+ WCHAR *zw = sqlite3_malloc64(nwc*sizeof(WCHAR));
856
+ if( zw!=NULL ){
857
+ nwc = MultiByteToWideChar(CP_UTF8,0, z,ncTake, zw,nwc);
858
+ if( nwc > 0 ){
859
+ /* Translation from UTF-8 to UTF-16, then WCHARs out. */
860
+ if( WriteConsoleW(ppst->hx, zw,nwc, 0, NULL) ){
861
+ rv = ncTake;
862
+ }
863
+ }
864
+ sqlite3_free(zw);
865
+ }
866
+ }
867
+ }
868
+ return rv;
869
+}
870
+
871
+/* For {f,o,e}PrintfUtf8() when stream is known to reach console. */
872
+static int conioVmPrintf(PerStreamTags *ppst, const char *zFormat, va_list ap){
873
+ char *z = sqlite3_vmprintf(zFormat, ap);
874
+ if( z ){
875
+ int rv = conZstrEmit(ppst, z, (int)strlen(z));
876
+ sqlite3_free(z);
877
+ return rv;
878
+ }else return 0;
879
+}
880
+# endif /* CIO_WIN_WC_XLATE */
881
+
882
+# ifdef CONSIO_GET_EMIT_STREAM
883
+static PerStreamTags * getDesignatedEmitStream(FILE *pf, unsigned chix,
884
+ PerStreamTags *ppst){
885
+ PerStreamTags *rv = isKnownWritable(pf);
886
+ short isValid = (rv!=0)? isValidStreamInfo(rv) : 0;
887
+ if( rv != 0 && isValid ) return rv;
888
+ streamOfConsole(pf, ppst);
889
+ return ppst;
890
+}
891
+# endif
892
+
893
+/* Get stream info, either for designated output or error stream when
894
+** chix equals 1 or 2, or for an arbitrary stream when chix == 0.
895
+** In either case, ppst references a caller-owned PerStreamTags
896
+** struct which may be filled in if none of the known writable
897
+** streams is being held by consoleInfo. The ppf parameter is an
898
+** output when chix!=0 and an input when chix==0.
899
+ */
900
+static PerStreamTags *
901
+getEmitStreamInfo(unsigned chix, PerStreamTags *ppst,
902
+ /* in/out */ FILE **ppf){
903
+ PerStreamTags *ppstTry;
904
+ FILE *pfEmit;
905
+ if( chix > 0 ){
906
+ ppstTry = &consoleInfo.pstDesignated[chix];
907
+ if( !isValidStreamInfo(ppstTry) ){
908
+ ppstTry = &consoleInfo.pstSetup[chix];
909
+ pfEmit = ppst->pf;
910
+ }else pfEmit = ppstTry->pf;
911
+ if( !isValidStreamInfo(ppst) ){
912
+ pfEmit = (chix > 1)? stderr : stdout;
913
+ ppstTry = ppst;
914
+ streamOfConsole(pfEmit, ppstTry);
915
+ }
916
+ *ppf = pfEmit;
917
+ }else{
918
+ ppstTry = isKnownWritable(*ppf);
919
+ if( ppstTry != 0 ) return ppstTry;
920
+ streamOfConsole(*ppf, ppst);
921
+ return ppst;
922
+ }
923
+ return ppstTry;
924
+}
925
+
926
+SQLITE_INTERNAL_LINKAGE int oPrintfUtf8(const char *zFormat, ...){
927
+ va_list ap;
928
+ int rv;
929
+ FILE *pfOut;
930
+ PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
931
+# if CIO_WIN_WC_XLATE
932
+ PerStreamTags *ppst = getEmitStreamInfo(1, &pst, &pfOut);
933
+# else
934
+ getEmitStreamInfo(1, &pst, &pfOut);
935
+# endif
936
+ assert(zFormat!=0);
937
+ va_start(ap, zFormat);
938
+# if CIO_WIN_WC_XLATE
939
+ if( pstReachesConsole(ppst) ){
940
+ rv = conioVmPrintf(ppst, zFormat, ap);
941
+ }else{
942
+# endif
943
+ rv = vfprintf(pfOut, zFormat, ap);
944
+# if CIO_WIN_WC_XLATE
945
+ }
946
+# endif
947
+ va_end(ap);
948
+ return rv;
949
+}
950
+
951
+SQLITE_INTERNAL_LINKAGE int ePrintfUtf8(const char *zFormat, ...){
952
+ va_list ap;
953
+ int rv;
954
+ FILE *pfErr;
955
+ PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
956
+# if CIO_WIN_WC_XLATE
957
+ PerStreamTags *ppst = getEmitStreamInfo(2, &pst, &pfErr);
958
+# else
959
+ getEmitStreamInfo(2, &pst, &pfErr);
960
+# endif
961
+ assert(zFormat!=0);
962
+ va_start(ap, zFormat);
963
+# if CIO_WIN_WC_XLATE
964
+ if( pstReachesConsole(ppst) ){
965
+ rv = conioVmPrintf(ppst, zFormat, ap);
966
+ }else{
967
+# endif
968
+ rv = vfprintf(pfErr, zFormat, ap);
969
+# if CIO_WIN_WC_XLATE
970
+ }
971
+# endif
972
+ va_end(ap);
973
+ return rv;
974
+}
975
+
976
+SQLITE_INTERNAL_LINKAGE int fPrintfUtf8(FILE *pfO, const char *zFormat, ...){
977
+ va_list ap;
978
+ int rv;
979
+ PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
980
+# if CIO_WIN_WC_XLATE
981
+ PerStreamTags *ppst = getEmitStreamInfo(0, &pst, &pfO);
982
+# else
983
+ getEmitStreamInfo(0, &pst, &pfO);
984
+# endif
985
+ assert(zFormat!=0);
986
+ va_start(ap, zFormat);
987
+# if CIO_WIN_WC_XLATE
988
+ if( pstReachesConsole(ppst) ){
989
+ maybeSetupAsConsole(ppst, 1);
990
+ rv = conioVmPrintf(ppst, zFormat, ap);
991
+ if( 0 == isKnownWritable(ppst->pf) ) restoreConsoleArb(ppst);
992
+ }else{
993
+# endif
994
+ rv = vfprintf(pfO, zFormat, ap);
995
+# if CIO_WIN_WC_XLATE
996
+ }
997
+# endif
998
+ va_end(ap);
999
+ return rv;
1000
+}
1001
+
1002
+SQLITE_INTERNAL_LINKAGE int fPutsUtf8(const char *z, FILE *pfO){
1003
+ PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1004
+# if CIO_WIN_WC_XLATE
1005
+ PerStreamTags *ppst = getEmitStreamInfo(0, &pst, &pfO);
1006
+# else
1007
+ getEmitStreamInfo(0, &pst, &pfO);
1008
+# endif
1009
+ assert(z!=0);
1010
+# if CIO_WIN_WC_XLATE
1011
+ if( pstReachesConsole(ppst) ){
1012
+ int rv;
1013
+ maybeSetupAsConsole(ppst, 1);
1014
+ rv = conZstrEmit(ppst, z, (int)strlen(z));
1015
+ if( 0 == isKnownWritable(ppst->pf) ) restoreConsoleArb(ppst);
1016
+ return rv;
1017
+ }else {
1018
+# endif
1019
+ return (fputs(z, pfO)<0)? 0 : (int)strlen(z);
1020
+# if CIO_WIN_WC_XLATE
1021
+ }
1022
+# endif
1023
+}
1024
+
1025
+SQLITE_INTERNAL_LINKAGE int ePutsUtf8(const char *z){
1026
+ FILE *pfErr;
1027
+ PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1028
+# if CIO_WIN_WC_XLATE
1029
+ PerStreamTags *ppst = getEmitStreamInfo(2, &pst, &pfErr);
1030
+# else
1031
+ getEmitStreamInfo(2, &pst, &pfErr);
1032
+# endif
1033
+ assert(z!=0);
1034
+# if CIO_WIN_WC_XLATE
1035
+ if( pstReachesConsole(ppst) ) return conZstrEmit(ppst, z, (int)strlen(z));
1036
+ else {
1037
+# endif
1038
+ return (fputs(z, pfErr)<0)? 0 : (int)strlen(z);
1039
+# if CIO_WIN_WC_XLATE
1040
+ }
1041
+# endif
1042
+}
1043
+
1044
+SQLITE_INTERNAL_LINKAGE int oPutsUtf8(const char *z){
1045
+ FILE *pfOut;
1046
+ PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1047
+# if CIO_WIN_WC_XLATE
1048
+ PerStreamTags *ppst = getEmitStreamInfo(1, &pst, &pfOut);
1049
+# else
1050
+ getEmitStreamInfo(1, &pst, &pfOut);
1051
+# endif
1052
+ assert(z!=0);
1053
+# if CIO_WIN_WC_XLATE
1054
+ if( pstReachesConsole(ppst) ) return conZstrEmit(ppst, z, (int)strlen(z));
1055
+ else {
1056
+# endif
1057
+ return (fputs(z, pfOut)<0)? 0 : (int)strlen(z);
1058
+# if CIO_WIN_WC_XLATE
1059
+ }
1060
+# endif
1061
+}
1062
+
1063
+#endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
1064
+
1065
+#if !(defined(SQLITE_CIO_NO_UTF8SCAN) && defined(SQLITE_CIO_NO_TRANSLATE))
1066
+/* Skip over as much z[] input char sequence as is valid UTF-8,
1067
+** limited per nAccept char's or whole characters and containing
1068
+** no char cn such that ((1<<cn) & ccm)!=0. On return, the
1069
+** sequence z:return (inclusive:exclusive) is validated UTF-8.
1070
+** Limit: nAccept>=0 => char count, nAccept<0 => character
1071
+ */
1072
+SQLITE_INTERNAL_LINKAGE const char*
1073
+zSkipValidUtf8(const char *z, int nAccept, long ccm){
1074
+ int ng = (nAccept<0)? -nAccept : 0;
1075
+ const char *pcLimit = (nAccept>=0)? z+nAccept : 0;
1076
+ assert(z!=0);
1077
+ while( (pcLimit)? (z<pcLimit) : (ng-- != 0) ){
1078
+ char c = *z;
1079
+ if( (c & 0x80) == 0 ){
1080
+ if( ccm != 0L && c < 0x20 && ((1L<<c) & ccm) != 0 ) return z;
1081
+ ++z; /* ASCII */
1082
+ }else if( (c & 0xC0) != 0xC0 ) return z; /* not a lead byte */
1083
+ else{
1084
+ const char *zt = z+1; /* Got lead byte, look at trail bytes.*/
1085
+ do{
1086
+ if( pcLimit && zt >= pcLimit ) return z;
1087
+ else{
1088
+ char ct = *zt++;
1089
+ if( ct==0 || (zt-z)>4 || (ct & 0xC0)!=0x80 ){
1090
+ /* Trailing bytes are too few, too many, or invalid. */
1091
+ return z;
1092
+ }
1093
+ }
1094
+ } while( ((c <<= 1) & 0x40) == 0x40 ); /* Eat lead byte's count. */
1095
+ z = zt;
1096
+ }
1097
+ }
1098
+ return z;
1099
+}
1100
+#endif /*!(defined(SQLITE_CIO_NO_UTF8SCAN)&&defined(SQLITE_CIO_NO_TRANSLATE))*/
1101
+
1102
+#ifndef SQLITE_CIO_NO_TRANSLATE
1103
+
1104
+#ifdef CONSIO_SPUTB
1105
+SQLITE_INTERNAL_LINKAGE int
1106
+fPutbUtf8(FILE *pfO, const char *cBuf, int nAccept){
1107
+ assert(pfO!=0);
1108
+# if CIO_WIN_WC_XLATE
1109
+ PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1110
+ PerStreamTags *ppst = getEmitStreamInfo(0, &pst, &pfO);
1111
+ if( pstReachesConsole(ppst) ){
1112
+ int rv;
1113
+ maybeSetupAsConsole(ppst, 1);
1114
+ rv = conZstrEmit(ppst, cBuf, nAccept);
1115
+ if( 0 == isKnownWritable(ppst->pf) ) restoreConsoleArb(ppst);
1116
+ return rv;
1117
+ }else {
1118
+# endif
1119
+ return (int)fwrite(cBuf, 1, nAccept, pfO);
1120
+# if CIO_WIN_WC_XLATE
1121
+ }
1122
+# endif
1123
+}
1124
+#endif /* defined(CONSIO_SPUTB) */
1125
+
1126
+SQLITE_INTERNAL_LINKAGE int
1127
+oPutbUtf8(const char *cBuf, int nAccept){
1128
+ FILE *pfOut;
1129
+ PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1130
+# if CIO_WIN_WC_XLATE
1131
+ PerStreamTags *ppst = getEmitStreamInfo(1, &pst, &pfOut);
1132
+# else
1133
+ getEmitStreamInfo(1, &pst, &pfOut);
1134
+# endif
1135
+# if CIO_WIN_WC_XLATE
1136
+ if( pstReachesConsole(ppst) ){
1137
+ return conZstrEmit(ppst, cBuf, nAccept);
1138
+ }else {
1139
+# endif
1140
+ return (int)fwrite(cBuf, 1, nAccept, pfOut);
1141
+# if CIO_WIN_WC_XLATE
1142
+ }
1143
+# endif
1144
+}
1145
+
1146
+# ifdef CONSIO_EPUTB
1147
+SQLITE_INTERNAL_LINKAGE int
1148
+ePutbUtf8(const char *cBuf, int nAccept){
1149
+ FILE *pfErr;
1150
+ PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1151
+ PerStreamTags *ppst = getEmitStreamInfo(2, &pst, &pfErr);
1152
+# if CIO_WIN_WC_XLATE
1153
+ if( pstReachesConsole(ppst) ){
1154
+ return conZstrEmit(ppst, cBuf, nAccept);
1155
+ }else {
1156
+# endif
1157
+ return (int)fwrite(cBuf, 1, nAccept, pfErr);
1158
+# if CIO_WIN_WC_XLATE
1159
+ }
1160
+# endif
1161
+}
1162
+# endif /* defined(CONSIO_EPUTB) */
1163
+
1164
+SQLITE_INTERNAL_LINKAGE char* fGetsUtf8(char *cBuf, int ncMax, FILE *pfIn){
1165
+ if( pfIn==0 ) pfIn = stdin;
1166
+# if CIO_WIN_WC_XLATE
1167
+ if( pfIn == consoleInfo.pstSetup[0].pf
1168
+ && (consoleInfo.sacSetup & SAC_InConsole)!=0 ){
1169
+# if CIO_WIN_WC_XLATE==1
1170
+# define SHELL_GULP 150 /* Count of WCHARS to be gulped at a time */
1171
+ WCHAR wcBuf[SHELL_GULP+1];
1172
+ int lend = 0, noc = 0;
1173
+ if( ncMax > 0 ) cBuf[0] = 0;
1174
+ while( noc < ncMax-8-1 && !lend ){
1175
+ /* There is room for at least 2 more characters and a 0-terminator. */
1176
+ int na = (ncMax > SHELL_GULP*4+1 + noc)? SHELL_GULP : (ncMax-1 - noc)/4;
1177
+# undef SHELL_GULP
1178
+ DWORD nbr = 0;
1179
+ BOOL bRC = ReadConsoleW(consoleInfo.pstSetup[0].hx, wcBuf, na, &nbr, 0);
1180
+ if( bRC && nbr>0 && (wcBuf[nbr-1]&0xF800)==0xD800 ){
1181
+ /* Last WHAR read is first of a UTF-16 surrogate pair. Grab its mate. */
1182
+ DWORD nbrx;
1183
+ bRC &= ReadConsoleW(consoleInfo.pstSetup[0].hx, wcBuf+nbr, 1, &nbrx, 0);
1184
+ if( bRC ) nbr += nbrx;
1185
+ }
1186
+ if( !bRC || (noc==0 && nbr==0) ) return 0;
1187
+ if( nbr > 0 ){
1188
+ int nmb = WideCharToMultiByte(CP_UTF8, 0, wcBuf,nbr,0,0,0,0);
1189
+ if( nmb != 0 && noc+nmb <= ncMax ){
1190
+ int iseg = noc;
1191
+ nmb = WideCharToMultiByte(CP_UTF8, 0, wcBuf,nbr,cBuf+noc,nmb,0,0);
1192
+ noc += nmb;
1193
+ /* Fixup line-ends as coded by Windows for CR (or "Enter".)
1194
+ ** This is done without regard for any setMode{Text,Binary}()
1195
+ ** call that might have been done on the interactive input.
1196
+ */
1197
+ if( noc > 0 ){
1198
+ if( cBuf[noc-1]=='\n' ){
1199
+ lend = 1;
1200
+ if( noc > 1 && cBuf[noc-2]=='\r' ) cBuf[--noc-1] = '\n';
1201
+ }
1202
+ }
1203
+ /* Check for ^Z (anywhere in line) too, to act as EOF. */
1204
+ while( iseg < noc ){
1205
+ if( cBuf[iseg]=='\x1a' ){
1206
+ noc = iseg; /* Chop ^Z and anything following. */
1207
+ lend = 1; /* Counts as end of line too. */
1208
+ break;
1209
+ }
1210
+ ++iseg;
1211
+ }
1212
+ }else break; /* Drop apparent garbage in. (Could assert.) */
1213
+ }else break;
1214
+ }
1215
+ /* If got nothing, (after ^Z chop), must be at end-of-file. */
1216
+ if( noc > 0 ){
1217
+ cBuf[noc] = 0;
1218
+ return cBuf;
1219
+ }else return 0;
1220
+# endif
1221
+ }else{
1222
+# endif
1223
+ return fgets(cBuf, ncMax, pfIn);
1224
+# if CIO_WIN_WC_XLATE
1225
+ }
1226
+# endif
1227
+}
1228
+#endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
1229
+
1230
+#undef SHELL_INVALID_FILE_PTR
1231
+
1232
+/************************* End ../ext/consio/console_io.c ********************/
1233
+
1234
+#ifndef SQLITE_SHELL_FIDDLE
1235
+/* From here onward, fgets() is redirected to the console_io library. */
1236
+# define fgets(b,n,f) fGetsUtf8(b,n,f)
1237
+/*
1238
+ * Define macros for emitting output text in various ways:
1239
+ * sputz(s, z) => emit 0-terminated string z to given stream s
1240
+ * sputf(s, f, ...) => emit varargs per format f to given stream s
1241
+ * oputz(z) => emit 0-terminated string z to default stream
1242
+ * oputf(f, ...) => emit varargs per format f to default stream
1243
+ * eputz(z) => emit 0-terminated string z to error stream
1244
+ * eputf(f, ...) => emit varargs per format f to error stream
1245
+ * oputb(b, n) => emit char buffer b[0..n-1] to default stream
1246
+ *
1247
+ * Note that the default stream is whatever has been last set via:
1248
+ * setOutputStream(FILE *pf)
1249
+ * This is normally the stream that CLI normal output goes to.
1250
+ * For the stand-alone CLI, it is stdout with no .output redirect.
1251
+ */
1252
+# define sputz(s,z) fPutsUtf8(z,s)
1253
+# define sputf fPrintfUtf8
1254
+# define oputz(z) oPutsUtf8(z)
1255
+# define oputf oPrintfUtf8
1256
+# define eputz(z) ePutsUtf8(z)
1257
+# define eputf ePrintfUtf8
1258
+# define oputb(buf,na) oPutbUtf8(buf,na)
2751259
#else
276
-# define setBinaryMode(X,Y)
277
-# define setTextMode(X,Y)
1260
+/* For Fiddle, all console handling and emit redirection is omitted. */
1261
+# define sputz(fp,z) fputs(z,fp)
1262
+# define sputf(fp,fmt, ...) fprintf(fp,fmt,__VA_ARGS__)
1263
+# define oputz(z) fputs(z,stdout)
1264
+# define oputf(fmt, ...) printf(fmt,__VA_ARGS__)
1265
+# define eputz(z) fputs(z,stderr)
1266
+# define eputf(fmt, ...) fprintf(stderr,fmt,__VA_ARGS__)
1267
+# define oputb(buf,na) fwrite(buf,1,na,stdout)
2781268
#endif
2791269
2801270
/* True if the timer is enabled */
2811271
static int enableTimer = 0;
2821272
@@ -347,14 +1337,14 @@
3471337
static void endTimer(void){
3481338
if( enableTimer ){
3491339
sqlite3_int64 iEnd = timeOfDay();
3501340
struct rusage sEnd;
3511341
getrusage(RUSAGE_SELF, &sEnd);
352
- printf("Run Time: real %.3f user %f sys %f\n",
353
- (iEnd - iBegin)*0.001,
354
- timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
355
- timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
1342
+ oputf("Run Time: real %.3f user %f sys %f\n",
1343
+ (iEnd - iBegin)*0.001,
1344
+ timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
1345
+ timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
3561346
}
3571347
}
3581348
3591349
#define BEGIN_TIMER beginTimer()
3601350
#define END_TIMER endTimer()
@@ -426,14 +1416,14 @@
4261416
static void endTimer(void){
4271417
if( enableTimer && getProcessTimesAddr){
4281418
FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
4291419
sqlite3_int64 ftWallEnd = timeOfDay();
4301420
getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
431
- printf("Run Time: real %.3f user %f sys %f\n",
432
- (ftWallEnd - ftWallBegin)*0.001,
433
- timeDiff(&ftUserBegin, &ftUserEnd),
434
- timeDiff(&ftKernelBegin, &ftKernelEnd));
1421
+ oputf("Run Time: real %.3f user %f sys %f\n",
1422
+ (ftWallEnd - ftWallBegin)*0.001,
1423
+ timeDiff(&ftUserBegin, &ftUserEnd),
1424
+ timeDiff(&ftKernelBegin, &ftKernelEnd));
4351425
}
4361426
}
4371427
4381428
#define BEGIN_TIMER beginTimer()
4391429
#define END_TIMER endTimer()
@@ -466,32 +1456,13 @@
4661456
** is true. Otherwise, assume stdin is connected to a file or pipe.
4671457
*/
4681458
static int stdin_is_interactive = 1;
4691459
4701460
/*
471
-** If build is for non-RT Windows, without 3rd-party line editing,
472
-** console input and output may be done in a UTF-8 compatible way,
473
-** if the OS is capable of it and the --no-utf8 option is not seen.
474
-*/
475
-#if (defined(_WIN32) || defined(WIN32)) && SHELL_USE_LOCAL_GETLINE \
476
- && !defined(SHELL_OMIT_WIN_UTF8) && !SQLITE_OS_WINRT
477
-# define SHELL_WIN_UTF8_OPT 1
478
-/* Record whether to do UTF-8 console I/O translation per stream. */
479
- static int console_utf8_in = 0;
480
- static int console_utf8_out = 0;
481
-/* Record whether can do UTF-8 or --no-utf8 seen in invocation. */
482
- static int mbcs_opted = 1; /* Assume cannot do until shown otherwise. */
483
-#else
484
-# define console_utf8_in 0
485
-# define console_utf8_out 0
486
-# define SHELL_WIN_UTF8_OPT 0
487
-#endif
488
-
489
-/*
490
-** On Windows systems we have to know if standard output is a console
491
-** in order to translate UTF-8 into MBCS. The following variable is
492
-** true if translation is required.
1461
+** On Windows systems we need to know if standard output is a console
1462
+** in order to show that UTF-16 translation is done in the sign-on
1463
+** banner. The following variable is true if it is the console.
4931464
*/
4941465
static int stdout_is_console = 1;
4951466
4961467
/*
4971468
** The following is the open SQLite database. We make a pointer
@@ -609,255 +1580,21 @@
6091580
shell_strncpy(dynPrompt.dynamicPrompt, ")x!", 4);
6101581
}else{
6111582
shell_strncpy(dynPrompt.dynamicPrompt, "(x.", 4);
6121583
dynPrompt.dynamicPrompt[2] = (char)('0'+dynPrompt.inParenLevel);
6131584
}
614
- shell_strncpy(dynPrompt.dynamicPrompt+3, continuePrompt+3, PROMPT_LEN_MAX-4);
1585
+ shell_strncpy(dynPrompt.dynamicPrompt+3, continuePrompt+3,
1586
+ PROMPT_LEN_MAX-4);
6151587
}
6161588
}
6171589
return dynPrompt.dynamicPrompt;
6181590
}
6191591
#endif /* !defined(SQLITE_OMIT_DYNAPROMPT) */
6201592
621
-#if SHELL_WIN_UTF8_OPT
622
-/* Following struct is used for UTF-8 console I/O. */
623
-static struct ConsoleState {
624
- int stdinEof; /* EOF has been seen on console input */
625
- int infsMode; /* Input file stream mode upon shell start */
626
- UINT inCodePage; /* Input code page upon shell start */
627
- UINT outCodePage; /* Output code page upon shell start */
628
- HANDLE hConsole; /* Console input or output handle */
629
- DWORD consoleMode; /* Console mode upon shell start */
630
-} conState = { 0, 0, 0, 0, INVALID_HANDLE_VALUE, 0 };
631
-
632
-#ifndef _O_U16TEXT /* For build environments lacking this constant: */
633
-# define _O_U16TEXT 0x20000
634
-#endif
635
-
636
-/*
637
-** If given stream number is a console, return 1 and get some attributes,
638
-** else return 0 and set the output attributes to invalid values.
639
-*/
640
-static short console_attrs(unsigned stnum, HANDLE *pH, DWORD *pConsMode){
641
- static int stid[3] = { STD_INPUT_HANDLE,STD_OUTPUT_HANDLE,STD_ERROR_HANDLE };
642
- HANDLE h;
643
- *pH = INVALID_HANDLE_VALUE;
644
- *pConsMode = 0;
645
- if( stnum > 2 ) return 0;
646
- h = GetStdHandle(stid[stnum]);
647
- if( h!=*pH && GetFileType(h)==FILE_TYPE_CHAR && GetConsoleMode(h,pConsMode) ){
648
- *pH = h;
649
- return 1;
650
- }
651
- return 0;
652
-}
653
-
654
-/*
655
-** Perform a runtime test of Windows console to determine if it can
656
-** do char-stream I/O correctly when the code page is set to CP_UTF8.
657
-** Returns are: 1 => yes it can, 0 => no it cannot
658
-**
659
-** The console's output code page is momentarily set, then restored.
660
-** So this should only be run when the process is given use of the
661
-** console for either input or output.
662
-*/
663
-static short ConsoleDoesUTF8(void){
664
- UINT ocp = GetConsoleOutputCP();
665
- const char TrialUtf8[] = { '\xC8', '\xAB' }; /* "ȫ" or 2 MBCS characters */
666
- WCHAR aReadBack[1] = { 0 }; /* Read back as 0x022B when decoded as UTF-8. */
667
- CONSOLE_SCREEN_BUFFER_INFO csbInfo = {0};
668
- /* Create an inactive screen buffer with which to do the experiment. */
669
- HANDLE hCSB = CreateConsoleScreenBuffer(GENERIC_READ|GENERIC_WRITE, 0, 0,
670
- CONSOLE_TEXTMODE_BUFFER, NULL);
671
- if( hCSB!=INVALID_HANDLE_VALUE ){
672
- COORD cpos = {0,0};
673
- DWORD rbc;
674
- SetConsoleCursorPosition(hCSB, cpos);
675
- SetConsoleOutputCP(CP_UTF8);
676
- /* Write 2 chars which are a single character in UTF-8 but more in MBCS. */
677
- WriteConsoleA(hCSB, TrialUtf8, sizeof(TrialUtf8), NULL, NULL);
678
- ReadConsoleOutputCharacterW(hCSB, &aReadBack[0], 1, cpos, &rbc);
679
- GetConsoleScreenBufferInfo(hCSB, &csbInfo);
680
- SetConsoleOutputCP(ocp);
681
- CloseHandle(hCSB);
682
- }
683
- /* Return 1 if cursor advanced by 1 position, else 0. */
684
- return (short)(csbInfo.dwCursorPosition.X == 1 && aReadBack[0] == 0x022B);
685
-}
686
-
687
-static short in_console = 0;
688
-static short out_console = 0;
689
-
690
-/*
691
-** Determine whether either normal I/O stream is the console,
692
-** and whether it can do UTF-8 translation, setting globals
693
-** in_console, out_console and mbcs_opted accordingly.
694
-*/
695
-static void probe_console(void){
696
- HANDLE h;
697
- DWORD cMode;
698
- in_console = console_attrs(0, &h, &cMode);
699
- out_console = console_attrs(1, &h, &cMode);
700
- if( in_console || out_console ) mbcs_opted = !ConsoleDoesUTF8();
701
-}
702
-
703
-/*
704
-** If console is used for normal I/O, absent a --no-utf8 option,
705
-** prepare console for UTF-8 input (from either typing or suitable
706
-** paste operations) and/or for UTF-8 output rendering.
707
-**
708
-** The console state upon entry is preserved, in conState, so that
709
-** console_restore() can later restore the same console state.
710
-**
711
-** The globals console_utf8_in and console_utf8_out are set, for
712
-** later use in selecting UTF-8 or MBCS console I/O translations.
713
-** This routine depends upon globals set by probe_console().
714
-*/
715
-static void console_prepare_utf8(void){
716
- struct ConsoleState csWork = { 0, 0, 0, 0, INVALID_HANDLE_VALUE, 0 };
717
-
718
- console_utf8_in = console_utf8_out = 0;
719
- if( (!in_console && !out_console) || mbcs_opted ) return;
720
- console_attrs((in_console)? 0 : 1, &conState.hConsole, &conState.consoleMode);
721
- conState.inCodePage = GetConsoleCP();
722
- conState.outCodePage = GetConsoleOutputCP();
723
- if( in_console ){
724
- SetConsoleCP(CP_UTF8);
725
- DWORD newConsoleMode = conState.consoleMode
726
- | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT;
727
- SetConsoleMode(conState.hConsole, newConsoleMode);
728
- conState.infsMode = _setmode(_fileno(stdin), _O_U16TEXT);
729
- console_utf8_in = 1;
730
- }
731
- if( out_console ){
732
- SetConsoleOutputCP(CP_UTF8);
733
- console_utf8_out = 1;
734
- }
735
-}
736
-
737
-/*
738
-** Undo the effects of console_prepare_utf8(), if any.
739
-*/
740
-static void SQLITE_CDECL console_restore(void){
741
- if( (console_utf8_in||console_utf8_out)
742
- && conState.hConsole!=INVALID_HANDLE_VALUE ){
743
- if( console_utf8_in ){
744
- SetConsoleCP(conState.inCodePage);
745
- _setmode(_fileno(stdin), conState.infsMode);
746
- }
747
- if( console_utf8_out ) SetConsoleOutputCP(conState.outCodePage);
748
- SetConsoleMode(conState.hConsole, conState.consoleMode);
749
- /* Avoid multiple calls. */
750
- conState.hConsole = INVALID_HANDLE_VALUE;
751
- conState.consoleMode = 0;
752
- console_utf8_in = 0;
753
- console_utf8_out = 0;
754
- }
755
-}
756
-
757
-/*
758
-** Collect input like fgets(...) with special provisions for input
759
-** from the Windows console to get around its strange coding issues.
760
-** Defers to plain fgets() when input is not interactive or when the
761
-** UTF-8 input is unavailable or opted out.
762
-*/
763
-static char* utf8_fgets(char *buf, int ncmax, FILE *fin){
764
- if( fin==0 ) fin = stdin;
765
- if( fin==stdin && stdin_is_interactive && console_utf8_in ){
766
-# define SQLITE_IALIM 150
767
- wchar_t wbuf[SQLITE_IALIM];
768
- int lend = 0;
769
- int noc = 0;
770
- if( ncmax==0 || conState.stdinEof ) return 0;
771
- buf[0] = 0;
772
- while( noc<ncmax-7-1 && !lend ){
773
- /* There is room for at least 2 more characters and a 0-terminator. */
774
- int na = (ncmax > SQLITE_IALIM*4+1 + noc)
775
- ? SQLITE_IALIM : (ncmax-1 - noc)/4;
776
-# undef SQLITE_IALIM
777
- DWORD nbr = 0;
778
- BOOL bRC = ReadConsoleW(conState.hConsole, wbuf, na, &nbr, 0);
779
- if( !bRC || (noc==0 && nbr==0) ) return 0;
780
- if( nbr > 0 ){
781
- int nmb = WideCharToMultiByte(CP_UTF8,WC_COMPOSITECHECK|WC_DEFAULTCHAR,
782
- wbuf,nbr,0,0,0,0);
783
- if( nmb !=0 && noc+nmb <= ncmax ){
784
- int iseg = noc;
785
- nmb = WideCharToMultiByte(CP_UTF8,WC_COMPOSITECHECK|WC_DEFAULTCHAR,
786
- wbuf,nbr,buf+noc,nmb,0,0);
787
- noc += nmb;
788
- /* Fixup line-ends as coded by Windows for CR (or "Enter".)*/
789
- if( noc > 0 ){
790
- if( buf[noc-1]=='\n' ){
791
- lend = 1;
792
- if( noc > 1 && buf[noc-2]=='\r' ){
793
- buf[noc-2] = '\n';
794
- --noc;
795
- }
796
- }
797
- }
798
- /* Check for ^Z (anywhere in line) too. */
799
- while( iseg < noc ){
800
- if( buf[iseg]==0x1a ){
801
- conState.stdinEof = 1;
802
- noc = iseg; /* Chop ^Z and anything following. */
803
- break;
804
- }
805
- ++iseg;
806
- }
807
- }else break; /* Drop apparent garbage in. (Could assert.) */
808
- }else break;
809
- }
810
- /* If got nothing, (after ^Z chop), must be at end-of-file. */
811
- if( noc == 0 ) return 0;
812
- buf[noc] = 0;
813
- return buf;
814
- }else{
815
- return fgets(buf, ncmax, fin);
816
- }
817
-}
818
-
819
-# define fgets(b,n,f) utf8_fgets(b,n,f)
820
-#endif /* SHELL_WIN_UTF8_OPT */
821
-
822
-/*
823
-** Render output like fprintf(). Except, if the output is going to the
824
-** console and if this is running on a Windows machine, and if UTF-8
825
-** output unavailable (or available but opted out), translate the
826
-** output from UTF-8 into MBCS for output through 8-bit stdout stream.
827
-** (Without -no-utf8, no translation is needed and must not be done.)
828
-*/
829
-#if defined(_WIN32) || defined(WIN32)
830
-void utf8_printf(FILE *out, const char *zFormat, ...){
831
- va_list ap;
832
- va_start(ap, zFormat);
833
- if( stdout_is_console && (out==stdout || out==stderr) && !console_utf8_out ){
834
- char *z1 = sqlite3_vmprintf(zFormat, ap);
835
- char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
836
- sqlite3_free(z1);
837
- fputs(z2, out);
838
- sqlite3_free(z2);
839
- }else{
840
- vfprintf(out, zFormat, ap);
841
- }
842
- va_end(ap);
843
-}
844
-#elif !defined(utf8_printf)
845
-# define utf8_printf fprintf
846
-#endif
847
-
848
-/*
849
-** Render output like fprintf(). This should not be used on anything that
850
-** includes string formatting (e.g. "%s").
851
-*/
852
-#if !defined(raw_printf)
853
-# define raw_printf fprintf
854
-#endif
855
-
8561593
/* Indicate out-of-memory and exit. */
8571594
static void shell_out_of_memory(void){
858
- raw_printf(stderr,"Error: out of memory\n");
1595
+ eputz("Error: out of memory\n");
8591596
exit(1);
8601597
}
8611598
8621599
/* Check a pointer to see if it is NULL. If it is NULL, exit with an
8631600
** out-of-memory error.
@@ -885,22 +1622,22 @@
8851622
char *z;
8861623
if( iotrace==0 ) return;
8871624
va_start(ap, zFormat);
8881625
z = sqlite3_vmprintf(zFormat, ap);
8891626
va_end(ap);
890
- utf8_printf(iotrace, "%s", z);
1627
+ sputf(iotrace, "%s", z);
8911628
sqlite3_free(z);
8921629
}
8931630
#endif
8941631
8951632
/*
896
-** Output string zUtf to stream pOut as w characters. If w is negative,
1633
+** Output string zUtf to Out stream as w characters. If w is negative,
8971634
** then right-justify the text. W is the width in UTF-8 characters, not
8981635
** in bytes. This is different from the %*.*s specification in printf
8991636
** since with %*.*s the width is measured in bytes, not characters.
9001637
*/
901
-static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
1638
+static void utf8_width_print(int w, const char *zUtf){
9021639
int i;
9031640
int n;
9041641
int aw = w<0 ? -w : w;
9051642
if( zUtf==0 ) zUtf = "";
9061643
for(i=n=0; zUtf[i]; i++){
@@ -911,15 +1648,15 @@
9111648
break;
9121649
}
9131650
}
9141651
}
9151652
if( n>=aw ){
916
- utf8_printf(pOut, "%.*s", i, zUtf);
1653
+ oputf("%.*s", i, zUtf);
9171654
}else if( w<0 ){
918
- utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
1655
+ oputf("%*s%s", aw-n, "", zUtf);
9191656
}else{
920
- utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
1657
+ oputf("%s%*s", zUtf, aw-n, "");
9211658
}
9221659
}
9231660
9241661
9251662
/*
@@ -975,11 +1712,11 @@
9751712
** Return open FILE * if zFile exists, can be opened for read
9761713
** and is an ordinary file or a character stream source.
9771714
** Otherwise return 0.
9781715
*/
9791716
static FILE * openChrSource(const char *zFile){
980
-#ifdef _WIN32
1717
+#if defined(_WIN32) || defined(WIN32)
9811718
struct _stat x = {0};
9821719
# define STAT_CHR_SRC(mode) ((mode & (_S_IFCHR|_S_IFIFO|_S_IFREG))!=0)
9831720
/* On Windows, open first, then check the stream nature. This order
9841721
** is necessary because _stat() and sibs, when checking a named pipe,
9851722
** effectively break the pipe as its supplier sees it. */
@@ -1038,27 +1775,10 @@
10381775
if( n>0 && zLine[n-1]=='\r' ) n--;
10391776
zLine[n] = 0;
10401777
break;
10411778
}
10421779
}
1043
-#if defined(_WIN32) || defined(WIN32)
1044
- /* For interactive input on Windows systems, with -no-utf8,
1045
- ** translate the multi-byte characterset characters into UTF-8.
1046
- ** This is the translation that predates console UTF-8 input. */
1047
- if( stdin_is_interactive && in==stdin && !console_utf8_in ){
1048
- char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
1049
- if( zTrans ){
1050
- i64 nTrans = strlen(zTrans)+1;
1051
- if( nTrans>nLine ){
1052
- zLine = realloc(zLine, nTrans);
1053
- shell_check_oom(zLine);
1054
- }
1055
- memcpy(zLine, zTrans, nTrans);
1056
- sqlite3_free(zTrans);
1057
- }
1058
- }
1059
-#endif /* defined(_WIN32) || defined(WIN32) */
10601780
return zLine;
10611781
}
10621782
10631783
/*
10641784
** Retrieve a single line of input text.
@@ -1081,11 +1801,11 @@
10811801
if( in!=0 ){
10821802
zResult = local_getline(zPrior, in);
10831803
}else{
10841804
zPrompt = isContinuation ? CONTINUATION_PROMPT : mainPrompt;
10851805
#if SHELL_USE_LOCAL_GETLINE
1086
- printf("%s", zPrompt);
1806
+ sputz(stdout, zPrompt);
10871807
fflush(stdout);
10881808
do{
10891809
zResult = local_getline(zPrior, stdin);
10901810
zPrior = 0;
10911811
/* ^C trap creates a false EOF, so let "interrupt" thread catch up. */
@@ -1328,11 +2048,11 @@
13282048
double r = sqlite3_value_double(apVal[0]);
13292049
int n = nVal>=2 ? sqlite3_value_int(apVal[1]) : 26;
13302050
char z[400];
13312051
if( n<1 ) n = 1;
13322052
if( n>350 ) n = 350;
1333
- snprintf(z, sizeof(z)-1, "%#+.*e", n, r);
2053
+ sqlite3_snprintf(sizeof(z), z, "%#+.*e", n, r);
13342054
sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
13352055
}
13362056
13372057
13382058
/*
@@ -17614,11 +18334,11 @@
1761418334
** A callback for the sqlite3_log() interface.
1761518335
*/
1761618336
static void shellLog(void *pArg, int iErrCode, const char *zMsg){
1761718337
ShellState *p = (ShellState*)pArg;
1761818338
if( p->pLog==0 ) return;
17619
- utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
18339
+ sputf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
1762018340
fflush(p->pLog);
1762118341
}
1762218342
1762318343
/*
1762418344
** SQL function: shell_putsnl(X)
@@ -17629,13 +18349,13 @@
1762918349
static void shellPutsFunc(
1763018350
sqlite3_context *pCtx,
1763118351
int nVal,
1763218352
sqlite3_value **apVal
1763318353
){
17634
- ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
18354
+ /* Unused: (ShellState*)sqlite3_user_data(pCtx); */
1763518355
(void)nVal;
17636
- utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
18356
+ oputf("%s\n", sqlite3_value_text(apVal[0]));
1763718357
sqlite3_result_value(pCtx, apVal[0]);
1763818358
}
1763918359
1764018360
/*
1764118361
** If in safe mode, print an error message described by the arguments
@@ -17650,12 +18370,11 @@
1765018370
va_list ap;
1765118371
char *zMsg;
1765218372
va_start(ap, zErrMsg);
1765318373
zMsg = sqlite3_vmprintf(zErrMsg, ap);
1765418374
va_end(ap);
17655
- raw_printf(stderr, "line %d: ", p->lineno);
17656
- utf8_printf(stderr, "%s\n", zMsg);
18375
+ eputf("line %d: %s\n", p->lineno, zMsg);
1765718376
exit(1);
1765818377
}
1765918378
}
1766018379
1766118380
/*
@@ -17819,11 +18538,11 @@
1781918538
}
1782018539
1782118540
/*
1782218541
** Output the given string as a hex-encoded blob (eg. X'1234' )
1782318542
*/
17824
-static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
18543
+static void output_hex_blob(const void *pBlob, int nBlob){
1782518544
int i;
1782618545
unsigned char *aBlob = (unsigned char*)pBlob;
1782718546
1782818547
char *zStr = sqlite3_malloc(nBlob*2 + 1);
1782918548
shell_check_oom(zStr);
@@ -17836,11 +18555,11 @@
1783618555
zStr[i*2] = aHex[ (aBlob[i] >> 4) ];
1783718556
zStr[i*2+1] = aHex[ (aBlob[i] & 0x0F) ];
1783818557
}
1783918558
zStr[i*2] = '\0';
1784018559
17841
- raw_printf(out,"X'%s'", zStr);
18560
+ oputf("X'%s'", zStr);
1784218561
sqlite3_free(zStr);
1784318562
}
1784418563
1784518564
/*
1784618565
** Find a string that is not found anywhere in z[]. Return a pointer
@@ -17866,39 +18585,46 @@
1786618585
/*
1786718586
** Output the given string as a quoted string using SQL quoting conventions.
1786818587
**
1786918588
** See also: output_quoted_escaped_string()
1787018589
*/
17871
-static void output_quoted_string(FILE *out, const char *z){
18590
+static void output_quoted_string(const char *z){
1787218591
int i;
1787318592
char c;
17874
- setBinaryMode(out, 1);
18593
+#ifndef SQLITE_SHELL_FIDDLE
18594
+ FILE *pfO = setOutputStream(invalidFileStream);
18595
+ setBinaryMode(pfO, 1);
18596
+#endif
1787518597
if( z==0 ) return;
1787618598
for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1787718599
if( c==0 ){
17878
- utf8_printf(out,"'%s'",z);
18600
+ oputf("'%s'",z);
1787918601
}else{
17880
- raw_printf(out, "'");
18602
+ oputz("'");
1788118603
while( *z ){
1788218604
for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1788318605
if( c=='\'' ) i++;
1788418606
if( i ){
17885
- utf8_printf(out, "%.*s", i, z);
18607
+ oputf("%.*s", i, z);
1788618608
z += i;
1788718609
}
1788818610
if( c=='\'' ){
17889
- raw_printf(out, "'");
18611
+ oputz("'");
1789018612
continue;
1789118613
}
1789218614
if( c==0 ){
1789318615
break;
1789418616
}
1789518617
z++;
1789618618
}
17897
- raw_printf(out, "'");
18619
+ oputz("'");
1789818620
}
17899
- setTextMode(out, 1);
18621
+#ifndef SQLITE_SHELL_FIDDLE
18622
+ setTextMode(pfO, 1);
18623
+#else
18624
+ setTextMode(stdout, 1);
18625
+#endif
1790018626
}
1790118627
1790218628
/*
1790318629
** Output the given string as a quoted string using SQL quoting conventions.
1790418630
** Additionallly , escape the "\n" and "\r" characters so that they do not
@@ -17906,17 +18632,20 @@
1790618632
** systems.
1790718633
**
1790818634
** This is like output_quoted_string() but with the addition of the \r\n
1790918635
** escape mechanism.
1791018636
*/
17911
-static void output_quoted_escaped_string(FILE *out, const char *z){
18637
+static void output_quoted_escaped_string(const char *z){
1791218638
int i;
1791318639
char c;
17914
- setBinaryMode(out, 1);
18640
+#ifndef SQLITE_SHELL_FIDDLE
18641
+ FILE *pfO = setOutputStream(invalidFileStream);
18642
+ setBinaryMode(pfO, 1);
18643
+#endif
1791518644
for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
1791618645
if( c==0 ){
17917
- utf8_printf(out,"'%s'",z);
18646
+ oputf("'%s'",z);
1791818647
}else{
1791918648
const char *zNL = 0;
1792018649
const char *zCR = 0;
1792118650
int nNL = 0;
1792218651
int nCR = 0;
@@ -17924,121 +18653,167 @@
1792418653
for(i=0; z[i]; i++){
1792518654
if( z[i]=='\n' ) nNL++;
1792618655
if( z[i]=='\r' ) nCR++;
1792718656
}
1792818657
if( nNL ){
17929
- raw_printf(out, "replace(");
18658
+ oputz("replace(");
1793018659
zNL = unused_string(z, "\\n", "\\012", zBuf1);
1793118660
}
1793218661
if( nCR ){
17933
- raw_printf(out, "replace(");
18662
+ oputz("replace(");
1793418663
zCR = unused_string(z, "\\r", "\\015", zBuf2);
1793518664
}
17936
- raw_printf(out, "'");
18665
+ oputz("'");
1793718666
while( *z ){
1793818667
for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
1793918668
if( c=='\'' ) i++;
1794018669
if( i ){
17941
- utf8_printf(out, "%.*s", i, z);
18670
+ oputf("%.*s", i, z);
1794218671
z += i;
1794318672
}
1794418673
if( c=='\'' ){
17945
- raw_printf(out, "'");
18674
+ oputz("'");
1794618675
continue;
1794718676
}
1794818677
if( c==0 ){
1794918678
break;
1795018679
}
1795118680
z++;
1795218681
if( c=='\n' ){
17953
- raw_printf(out, "%s", zNL);
18682
+ oputz(zNL);
1795418683
continue;
1795518684
}
17956
- raw_printf(out, "%s", zCR);
18685
+ oputz(zCR);
1795718686
}
17958
- raw_printf(out, "'");
18687
+ oputz("'");
1795918688
if( nCR ){
17960
- raw_printf(out, ",'%s',char(13))", zCR);
18689
+ oputf(",'%s',char(13))", zCR);
1796118690
}
1796218691
if( nNL ){
17963
- raw_printf(out, ",'%s',char(10))", zNL);
18692
+ oputf(",'%s',char(10))", zNL);
1796418693
}
1796518694
}
17966
- setTextMode(out, 1);
18695
+#ifndef SQLITE_SHELL_FIDDLE
18696
+ setTextMode(pfO, 1);
18697
+#else
18698
+ setTextMode(stdout, 1);
18699
+#endif
1796718700
}
1796818701
18702
+/*
18703
+** Find earliest of chars within s specified in zAny.
18704
+** With ns == ~0, is like strpbrk(s,zAny) and s must be 0-terminated.
18705
+*/
18706
+static const char *anyOfInStr(const char *s, const char *zAny, size_t ns){
18707
+ const char *pcFirst = 0;
18708
+ if( ns == ~(size_t)0 ) ns = strlen(s);
18709
+ while(*zAny){
18710
+ const char *pc = (const char*)memchr(s, *zAny&0xff, ns);
18711
+ if( pc ){
18712
+ pcFirst = pc;
18713
+ ns = pcFirst - s;
18714
+ }
18715
+ ++zAny;
18716
+ }
18717
+ return pcFirst;
18718
+}
1796918719
/*
1797018720
** Output the given string as a quoted according to C or TCL quoting rules.
1797118721
*/
17972
-static void output_c_string(FILE *out, const char *z){
17973
- unsigned int c;
17974
- fputc('"', out);
17975
- while( (c = *(z++))!=0 ){
17976
- if( c=='\\' ){
17977
- fputc(c, out);
17978
- fputc(c, out);
17979
- }else if( c=='"' ){
17980
- fputc('\\', out);
17981
- fputc('"', out);
17982
- }else if( c=='\t' ){
17983
- fputc('\\', out);
17984
- fputc('t', out);
17985
- }else if( c=='\n' ){
17986
- fputc('\\', out);
17987
- fputc('n', out);
17988
- }else if( c=='\r' ){
17989
- fputc('\\', out);
17990
- fputc('r', out);
18722
+static void output_c_string(const char *z){
18723
+ char c;
18724
+ static const char *zq = "\"";
18725
+ static long ctrlMask = ~0L;
18726
+ static const char *zDQBSRO = "\"\\\x7f"; /* double-quote, backslash, rubout */
18727
+ char ace[3] = "\\?";
18728
+ char cbsSay;
18729
+ oputz(zq);
18730
+ while( *z!=0 ){
18731
+ const char *pcDQBSRO = anyOfInStr(z, zDQBSRO, ~(size_t)0);
18732
+ const char *pcPast = zSkipValidUtf8(z, INT_MAX, ctrlMask);
18733
+ const char *pcEnd = (pcDQBSRO && pcDQBSRO < pcPast)? pcDQBSRO : pcPast;
18734
+ if( pcEnd > z ) oputb(z, (int)(pcEnd-z));
18735
+ if( (c = *pcEnd)==0 ) break;
18736
+ ++pcEnd;
18737
+ switch( c ){
18738
+ case '\\': case '"':
18739
+ cbsSay = (char)c;
18740
+ break;
18741
+ case '\t': cbsSay = 't'; break;
18742
+ case '\n': cbsSay = 'n'; break;
18743
+ case '\r': cbsSay = 'r'; break;
18744
+ case '\f': cbsSay = 'f'; break;
18745
+ default: cbsSay = 0; break;
18746
+ }
18747
+ if( cbsSay ){
18748
+ ace[1] = cbsSay;
18749
+ oputz(ace);
1799118750
}else if( !isprint(c&0xff) ){
17992
- raw_printf(out, "\\%03o", c&0xff);
18751
+ oputf("\\%03o", c&0xff);
1799318752
}else{
17994
- fputc(c, out);
18753
+ ace[1] = (char)c;
18754
+ oputz(ace+1);
1799518755
}
18756
+ z = pcEnd;
1799618757
}
17997
- fputc('"', out);
18758
+ oputz(zq);
1799818759
}
1799918760
1800018761
/*
1800118762
** Output the given string as a quoted according to JSON quoting rules.
1800218763
*/
18003
-static void output_json_string(FILE *out, const char *z, i64 n){
18004
- unsigned int c;
18764
+static void output_json_string(const char *z, i64 n){
18765
+ char c;
18766
+ static const char *zq = "\"";
18767
+ static long ctrlMask = ~0L;
18768
+ static const char *zDQBS = "\"\\";
18769
+ const char *pcLimit;
18770
+ char ace[3] = "\\?";
18771
+ char cbsSay;
18772
+
1800518773
if( z==0 ) z = "";
18006
- if( n<0 ) n = strlen(z);
18007
- fputc('"', out);
18008
- while( n-- ){
18774
+ pcLimit = z + ((n<0)? strlen(z) : (size_t)n);
18775
+ oputz(zq);
18776
+ while( z < pcLimit ){
18777
+ const char *pcDQBS = anyOfInStr(z, zDQBS, pcLimit-z);
18778
+ const char *pcPast = zSkipValidUtf8(z, (int)(pcLimit-z), ctrlMask);
18779
+ const char *pcEnd = (pcDQBS && pcDQBS < pcPast)? pcDQBS : pcPast;
18780
+ if( pcEnd > z ){
18781
+ oputb(z, (int)(pcEnd-z));
18782
+ z = pcEnd;
18783
+ }
18784
+ if( z >= pcLimit ) break;
1800918785
c = *(z++);
18010
- if( c=='\\' || c=='"' ){
18011
- fputc('\\', out);
18012
- fputc(c, out);
18786
+ switch( c ){
18787
+ case '"': case '\\':
18788
+ cbsSay = (char)c;
18789
+ break;
18790
+ case '\b': cbsSay = 'b'; break;
18791
+ case '\f': cbsSay = 'f'; break;
18792
+ case '\n': cbsSay = 'n'; break;
18793
+ case '\r': cbsSay = 'r'; break;
18794
+ case '\t': cbsSay = 't'; break;
18795
+ default: cbsSay = 0; break;
18796
+ }
18797
+ if( cbsSay ){
18798
+ ace[1] = cbsSay;
18799
+ oputz(ace);
1801318800
}else if( c<=0x1f ){
18014
- fputc('\\', out);
18015
- if( c=='\b' ){
18016
- fputc('b', out);
18017
- }else if( c=='\f' ){
18018
- fputc('f', out);
18019
- }else if( c=='\n' ){
18020
- fputc('n', out);
18021
- }else if( c=='\r' ){
18022
- fputc('r', out);
18023
- }else if( c=='\t' ){
18024
- fputc('t', out);
18025
- }else{
18026
- raw_printf(out, "u%04x",c);
18027
- }
18028
- }else{
18029
- fputc(c, out);
18030
- }
18031
- }
18032
- fputc('"', out);
18801
+ oputf("u%04x", c);
18802
+ }else{
18803
+ ace[1] = (char)c;
18804
+ oputz(ace+1);
18805
+ }
18806
+ }
18807
+ oputz(zq);
1803318808
}
1803418809
1803518810
/*
1803618811
** Output the given string with characters that are special to
1803718812
** HTML escaped.
1803818813
*/
18039
-static void output_html_string(FILE *out, const char *z){
18814
+static void output_html_string(const char *z){
1804018815
int i;
1804118816
if( z==0 ) z = "";
1804218817
while( *z ){
1804318818
for(i=0; z[i]
1804418819
&& z[i]!='<'
@@ -18046,22 +18821,22 @@
1804618821
&& z[i]!='>'
1804718822
&& z[i]!='\"'
1804818823
&& z[i]!='\'';
1804918824
i++){}
1805018825
if( i>0 ){
18051
- utf8_printf(out,"%.*s",i,z);
18826
+ oputf("%.*s",i,z);
1805218827
}
1805318828
if( z[i]=='<' ){
18054
- raw_printf(out,"&lt;");
18829
+ oputz("&lt;");
1805518830
}else if( z[i]=='&' ){
18056
- raw_printf(out,"&amp;");
18831
+ oputz("&amp;");
1805718832
}else if( z[i]=='>' ){
18058
- raw_printf(out,"&gt;");
18833
+ oputz("&gt;");
1805918834
}else if( z[i]=='\"' ){
18060
- raw_printf(out,"&quot;");
18835
+ oputz("&quot;");
1806118836
}else if( z[i]=='\'' ){
18062
- raw_printf(out,"&#39;");
18837
+ oputz("&#39;");
1806318838
}else{
1806418839
break;
1806518840
}
1806618841
z += i + 1;
1806718842
}
@@ -18095,13 +18870,12 @@
1809518870
** the separator, which may or may not be a comma. p->nullValue is
1809618871
** the null value. Strings are quoted if necessary. The separator
1809718872
** is only issued if bSep is true.
1809818873
*/
1809918874
static void output_csv(ShellState *p, const char *z, int bSep){
18100
- FILE *out = p->out;
1810118875
if( z==0 ){
18102
- utf8_printf(out,"%s",p->nullValue);
18876
+ oputf("%s",p->nullValue);
1810318877
}else{
1810418878
unsigned i;
1810518879
for(i=0; z[i]; i++){
1810618880
if( needCsvQuote[((unsigned char*)z)[i]] ){
1810718881
i = 0;
@@ -18109,18 +18883,18 @@
1810918883
}
1811018884
}
1811118885
if( i==0 || strstr(z, p->colSeparator)!=0 ){
1811218886
char *zQuoted = sqlite3_mprintf("\"%w\"", z);
1811318887
shell_check_oom(zQuoted);
18114
- utf8_printf(out, "%s", zQuoted);
18888
+ oputz(zQuoted);
1811518889
sqlite3_free(zQuoted);
1811618890
}else{
18117
- utf8_printf(out, "%s", z);
18891
+ oputz(z);
1811818892
}
1811918893
}
1812018894
if( bSep ){
18121
- utf8_printf(p->out, "%s", p->colSeparator);
18895
+ oputz(p->colSeparator);
1812218896
}
1812318897
}
1812418898
1812518899
/*
1812618900
** This routine runs when the user presses Ctrl-C
@@ -18224,20 +18998,20 @@
1822418998
const char *az[4];
1822518999
az[0] = zA1;
1822619000
az[1] = zA2;
1822719001
az[2] = zA3;
1822819002
az[3] = zA4;
18229
- utf8_printf(p->out, "authorizer: %s", azAction[op]);
19003
+ oputf("authorizer: %s", azAction[op]);
1823019004
for(i=0; i<4; i++){
18231
- raw_printf(p->out, " ");
19005
+ oputz(" ");
1823219006
if( az[i] ){
18233
- output_c_string(p->out, az[i]);
19007
+ output_c_string(az[i]);
1823419008
}else{
18235
- raw_printf(p->out, "NULL");
19009
+ oputz("NULL");
1823619010
}
1823719011
}
18238
- raw_printf(p->out, "\n");
19012
+ oputz("\n");
1823919013
if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4);
1824019014
return SQLITE_OK;
1824119015
}
1824219016
#endif
1824319017
@@ -18249,11 +19023,11 @@
1824919023
**
1825019024
** If the schema statement in z[] contains a start-of-comment and if
1825119025
** sqlite3_complete() returns false, try to terminate the comment before
1825219026
** printing the result. https://sqlite.org/forum/forumpost/d7be961c5c
1825319027
*/
18254
-static void printSchemaLine(FILE *out, const char *z, const char *zTail){
19028
+static void printSchemaLine(const char *z, const char *zTail){
1825519029
char *zToFree = 0;
1825619030
if( z==0 ) return;
1825719031
if( zTail==0 ) return;
1825819032
if( zTail[0]==';' && (strstr(z, "/*")!=0 || strstr(z,"--")!=0) ){
1825919033
const char *zOrig = z;
@@ -18271,20 +19045,20 @@
1827119045
}
1827219046
sqlite3_free(zNew);
1827319047
}
1827419048
}
1827519049
if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
18276
- utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
19050
+ oputf("CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
1827719051
}else{
18278
- utf8_printf(out, "%s%s", z, zTail);
19052
+ oputf("%s%s", z, zTail);
1827919053
}
1828019054
sqlite3_free(zToFree);
1828119055
}
18282
-static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
19056
+static void printSchemaLineN(char *z, int n, const char *zTail){
1828319057
char c = z[n];
1828419058
z[n] = 0;
18285
- printSchemaLine(out, z, zTail);
19059
+ printSchemaLine(z, zTail);
1828619060
z[n] = c;
1828719061
}
1828819062
1828919063
/*
1829019064
** Return true if string z[] has nothing but whitespace and comments to the
@@ -18308,11 +19082,11 @@
1830819082
EQPGraphRow *pNew;
1830919083
i64 nText;
1831019084
if( zText==0 ) return;
1831119085
nText = strlen(zText);
1831219086
if( p->autoEQPtest ){
18313
- utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
19087
+ oputf("%d,%d,%s\n", iEqpId, p2, zText);
1831419088
}
1831519089
pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
1831619090
shell_check_oom(pNew);
1831719091
pNew->iEqpId = iEqpId;
1831819092
pNew->iParentId = p2;
@@ -18356,12 +19130,11 @@
1835619130
i64 n = strlen(p->sGraph.zPrefix);
1835719131
char *z;
1835819132
for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
1835919133
pNext = eqp_next_row(p, iEqpId, pRow);
1836019134
z = pRow->zText;
18361
- utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
18362
- pNext ? "|--" : "`--", z);
19135
+ oputf("%s%s%s\n", p->sGraph.zPrefix, pNext ? "|--" : "`--", z);
1836319136
if( n<(i64)sizeof(p->sGraph.zPrefix)-7 ){
1836419137
memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4);
1836519138
eqp_render_level(p, pRow->iEqpId);
1836619139
p->sGraph.zPrefix[n] = 0;
1836719140
}
@@ -18377,17 +19150,17 @@
1837719150
if( pRow->zText[0]=='-' ){
1837819151
if( pRow->pNext==0 ){
1837919152
eqp_reset(p);
1838019153
return;
1838119154
}
18382
- utf8_printf(p->out, "%s\n", pRow->zText+3);
19155
+ oputf("%s\n", pRow->zText+3);
1838319156
p->sGraph.pRow = pRow->pNext;
1838419157
sqlite3_free(pRow);
1838519158
}else if( nCycle>0 ){
18386
- utf8_printf(p->out, "QUERY PLAN (cycles=%lld [100%%])\n", nCycle);
19159
+ oputf("QUERY PLAN (cycles=%lld [100%%])\n", nCycle);
1838719160
}else{
18388
- utf8_printf(p->out, "QUERY PLAN\n");
19161
+ oputz("QUERY PLAN\n");
1838919162
}
1839019163
p->sGraph.zPrefix[0] = 0;
1839119164
eqp_render_level(p, 0);
1839219165
eqp_reset(p);
1839319166
}
@@ -18399,33 +19172,33 @@
1839919172
*/
1840019173
static int progress_handler(void *pClientData) {
1840119174
ShellState *p = (ShellState*)pClientData;
1840219175
p->nProgress++;
1840319176
if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
18404
- raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
19177
+ oputf("Progress limit reached (%u)\n", p->nProgress);
1840519178
if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
1840619179
if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
1840719180
return 1;
1840819181
}
1840919182
if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
18410
- raw_printf(p->out, "Progress %u\n", p->nProgress);
19183
+ oputf("Progress %u\n", p->nProgress);
1841119184
}
1841219185
return 0;
1841319186
}
1841419187
#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
1841519188
1841619189
/*
1841719190
** Print N dashes
1841819191
*/
18419
-static void print_dashes(FILE *out, int N){
19192
+static void print_dashes(int N){
1842019193
const char zDash[] = "--------------------------------------------------";
1842119194
const int nDash = sizeof(zDash) - 1;
1842219195
while( N>nDash ){
18423
- fputs(zDash, out);
19196
+ oputz(zDash);
1842419197
N -= nDash;
1842519198
}
18426
- raw_printf(out, "%.*s", N, zDash);
19199
+ oputf("%.*s", N, zDash);
1842719200
}
1842819201
1842919202
/*
1843019203
** Print a markdown or table-style row separator using ascii-art
1843119204
*/
@@ -18434,19 +19207,19 @@
1843419207
int nArg,
1843519208
const char *zSep
1843619209
){
1843719210
int i;
1843819211
if( nArg>0 ){
18439
- fputs(zSep, p->out);
18440
- print_dashes(p->out, p->actualWidth[0]+2);
19212
+ oputz(zSep);
19213
+ print_dashes(p->actualWidth[0]+2);
1844119214
for(i=1; i<nArg; i++){
18442
- fputs(zSep, p->out);
18443
- print_dashes(p->out, p->actualWidth[i]+2);
19215
+ oputz(zSep);
19216
+ print_dashes(p->actualWidth[i]+2);
1844419217
}
18445
- fputs(zSep, p->out);
19218
+ oputz(zSep);
1844619219
}
18447
- fputs("\n", p->out);
19220
+ oputz("\n");
1844819221
}
1844919222
1845019223
/*
1845119224
** This is the callback routine that the shell
1845219225
** invokes for each row of a query result.
@@ -18472,14 +19245,14 @@
1847219245
if( azArg==0 ) break;
1847319246
for(i=0; i<nArg; i++){
1847419247
int len = strlen30(azCol[i] ? azCol[i] : "");
1847519248
if( len>w ) w = len;
1847619249
}
18477
- if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
19250
+ if( p->cnt++>0 ) oputz(p->rowSeparator);
1847819251
for(i=0; i<nArg; i++){
18479
- utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
18480
- azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
19252
+ oputf("%*s = %s%s", w, azCol[i],
19253
+ azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
1848119254
}
1848219255
break;
1848319256
}
1848419257
case MODE_ScanExp:
1848519258
case MODE_Explain: {
@@ -18502,16 +19275,16 @@
1850219275
if( nArg>nWidth ) nArg = nWidth;
1850319276
1850419277
/* If this is the first row seen, print out the headers */
1850519278
if( p->cnt++==0 ){
1850619279
for(i=0; i<nArg; i++){
18507
- utf8_width_print(p->out, aWidth[i], azCol[ aMap[i] ]);
18508
- fputs(i==nArg-1 ? "\n" : " ", p->out);
19280
+ utf8_width_print(aWidth[i], azCol[ aMap[i] ]);
19281
+ oputz(i==nArg-1 ? "\n" : " ");
1850919282
}
1851019283
for(i=0; i<nArg; i++){
18511
- print_dashes(p->out, aWidth[i]);
18512
- fputs(i==nArg-1 ? "\n" : " ", p->out);
19284
+ print_dashes(aWidth[i]);
19285
+ oputz(i==nArg-1 ? "\n" : " ");
1851319286
}
1851419287
}
1851519288
1851619289
/* If there is no data, exit early. */
1851719290
if( azArg==0 ) break;
@@ -18525,21 +19298,21 @@
1852519298
w = strlenChar(zVal);
1852619299
zSep = " ";
1852719300
}
1852819301
if( i==iIndent && p->aiIndent && p->pStmt ){
1852919302
if( p->iIndent<p->nIndent ){
18530
- utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
19303
+ oputf("%*.s", p->aiIndent[p->iIndent], "");
1853119304
}
1853219305
p->iIndent++;
1853319306
}
18534
- utf8_width_print(p->out, w, zVal ? zVal : p->nullValue);
18535
- fputs(i==nArg-1 ? "\n" : zSep, p->out);
19307
+ utf8_width_print(w, zVal ? zVal : p->nullValue);
19308
+ oputz(i==nArg-1 ? "\n" : zSep);
1853619309
}
1853719310
break;
1853819311
}
1853919312
case MODE_Semi: { /* .schema and .fullschema output */
18540
- printSchemaLine(p->out, azArg[0], ";\n");
19313
+ printSchemaLine(azArg[0], ";\n");
1854119314
break;
1854219315
}
1854319316
case MODE_Pretty: { /* .schema and .fullschema with --indent */
1854419317
char *z;
1854519318
int j;
@@ -18550,11 +19323,11 @@
1855019323
assert( nArg==1 );
1855119324
if( azArg[0]==0 ) break;
1855219325
if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
1855319326
|| sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
1855419327
){
18555
- utf8_printf(p->out, "%s;\n", azArg[0]);
19328
+ oputf("%s;\n", azArg[0]);
1855619329
break;
1855719330
}
1855819331
z = sqlite3_mprintf("%s", azArg[0]);
1855919332
shell_check_oom(z);
1856019333
j = 0;
@@ -18583,166 +19356,161 @@
1858319356
}else if( c=='(' ){
1858419357
nParen++;
1858519358
}else if( c==')' ){
1858619359
nParen--;
1858719360
if( nLine>0 && nParen==0 && j>0 ){
18588
- printSchemaLineN(p->out, z, j, "\n");
19361
+ printSchemaLineN(z, j, "\n");
1858919362
j = 0;
1859019363
}
1859119364
}
1859219365
z[j++] = c;
1859319366
if( nParen==1 && cEnd==0
1859419367
&& (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
1859519368
){
1859619369
if( c=='\n' ) j--;
18597
- printSchemaLineN(p->out, z, j, "\n ");
19370
+ printSchemaLineN(z, j, "\n ");
1859819371
j = 0;
1859919372
nLine++;
1860019373
while( IsSpace(z[i+1]) ){ i++; }
1860119374
}
1860219375
}
1860319376
z[j] = 0;
1860419377
}
18605
- printSchemaLine(p->out, z, ";\n");
19378
+ printSchemaLine(z, ";\n");
1860619379
sqlite3_free(z);
1860719380
break;
1860819381
}
1860919382
case MODE_List: {
1861019383
if( p->cnt++==0 && p->showHeader ){
1861119384
for(i=0; i<nArg; i++){
18612
- utf8_printf(p->out,"%s%s",azCol[i],
18613
- i==nArg-1 ? p->rowSeparator : p->colSeparator);
19385
+ oputf("%s%s",azCol[i], i==nArg-1 ? p->rowSeparator : p->colSeparator);
1861419386
}
1861519387
}
1861619388
if( azArg==0 ) break;
1861719389
for(i=0; i<nArg; i++){
1861819390
char *z = azArg[i];
1861919391
if( z==0 ) z = p->nullValue;
18620
- utf8_printf(p->out, "%s", z);
18621
- if( i<nArg-1 ){
18622
- utf8_printf(p->out, "%s", p->colSeparator);
18623
- }else{
18624
- utf8_printf(p->out, "%s", p->rowSeparator);
18625
- }
19392
+ oputz(z);
19393
+ oputz((i<nArg-1)? p->colSeparator : p->rowSeparator);
1862619394
}
1862719395
break;
1862819396
}
1862919397
case MODE_Html: {
1863019398
if( p->cnt++==0 && p->showHeader ){
18631
- raw_printf(p->out,"<TR>");
19399
+ oputz("<TR>");
1863219400
for(i=0; i<nArg; i++){
18633
- raw_printf(p->out,"<TH>");
18634
- output_html_string(p->out, azCol[i]);
18635
- raw_printf(p->out,"</TH>\n");
19401
+ oputz("<TH>");
19402
+ output_html_string(azCol[i]);
19403
+ oputz("</TH>\n");
1863619404
}
18637
- raw_printf(p->out,"</TR>\n");
19405
+ oputz("</TR>\n");
1863819406
}
1863919407
if( azArg==0 ) break;
18640
- raw_printf(p->out,"<TR>");
19408
+ oputz("<TR>");
1864119409
for(i=0; i<nArg; i++){
18642
- raw_printf(p->out,"<TD>");
18643
- output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
18644
- raw_printf(p->out,"</TD>\n");
19410
+ oputz("<TD>");
19411
+ output_html_string(azArg[i] ? azArg[i] : p->nullValue);
19412
+ oputz("</TD>\n");
1864519413
}
18646
- raw_printf(p->out,"</TR>\n");
19414
+ oputz("</TR>\n");
1864719415
break;
1864819416
}
1864919417
case MODE_Tcl: {
1865019418
if( p->cnt++==0 && p->showHeader ){
1865119419
for(i=0; i<nArg; i++){
18652
- output_c_string(p->out,azCol[i] ? azCol[i] : "");
18653
- if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
19420
+ output_c_string(azCol[i] ? azCol[i] : "");
19421
+ if(i<nArg-1) oputz(p->colSeparator);
1865419422
}
18655
- utf8_printf(p->out, "%s", p->rowSeparator);
19423
+ oputz(p->rowSeparator);
1865619424
}
1865719425
if( azArg==0 ) break;
1865819426
for(i=0; i<nArg; i++){
18659
- output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
18660
- if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
19427
+ output_c_string(azArg[i] ? azArg[i] : p->nullValue);
19428
+ if(i<nArg-1) oputz(p->colSeparator);
1866119429
}
18662
- utf8_printf(p->out, "%s", p->rowSeparator);
19430
+ oputz(p->rowSeparator);
1866319431
break;
1866419432
}
1866519433
case MODE_Csv: {
1866619434
setBinaryMode(p->out, 1);
1866719435
if( p->cnt++==0 && p->showHeader ){
1866819436
for(i=0; i<nArg; i++){
1866919437
output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
1867019438
}
18671
- utf8_printf(p->out, "%s", p->rowSeparator);
19439
+ oputz(p->rowSeparator);
1867219440
}
1867319441
if( nArg>0 ){
1867419442
for(i=0; i<nArg; i++){
1867519443
output_csv(p, azArg[i], i<nArg-1);
1867619444
}
18677
- utf8_printf(p->out, "%s", p->rowSeparator);
19445
+ oputz(p->rowSeparator);
1867819446
}
1867919447
setTextMode(p->out, 1);
1868019448
break;
1868119449
}
1868219450
case MODE_Insert: {
1868319451
if( azArg==0 ) break;
18684
- utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
19452
+ oputf("INSERT INTO %s",p->zDestTable);
1868519453
if( p->showHeader ){
18686
- raw_printf(p->out,"(");
19454
+ oputz("(");
1868719455
for(i=0; i<nArg; i++){
18688
- if( i>0 ) raw_printf(p->out, ",");
19456
+ if( i>0 ) oputz(",");
1868919457
if( quoteChar(azCol[i]) ){
1869019458
char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
1869119459
shell_check_oom(z);
18692
- utf8_printf(p->out, "%s", z);
19460
+ oputz(z);
1869319461
sqlite3_free(z);
1869419462
}else{
18695
- raw_printf(p->out, "%s", azCol[i]);
19463
+ oputf("%s", azCol[i]);
1869619464
}
1869719465
}
18698
- raw_printf(p->out,")");
19466
+ oputz(")");
1869919467
}
1870019468
p->cnt++;
1870119469
for(i=0; i<nArg; i++){
18702
- raw_printf(p->out, i>0 ? "," : " VALUES(");
19470
+ oputz(i>0 ? "," : " VALUES(");
1870319471
if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
18704
- utf8_printf(p->out,"NULL");
19472
+ oputz("NULL");
1870519473
}else if( aiType && aiType[i]==SQLITE_TEXT ){
1870619474
if( ShellHasFlag(p, SHFLG_Newlines) ){
18707
- output_quoted_string(p->out, azArg[i]);
19475
+ output_quoted_string(azArg[i]);
1870819476
}else{
18709
- output_quoted_escaped_string(p->out, azArg[i]);
19477
+ output_quoted_escaped_string(azArg[i]);
1871019478
}
1871119479
}else if( aiType && aiType[i]==SQLITE_INTEGER ){
18712
- utf8_printf(p->out,"%s", azArg[i]);
19480
+ oputz(azArg[i]);
1871319481
}else if( aiType && aiType[i]==SQLITE_FLOAT ){
1871419482
char z[50];
1871519483
double r = sqlite3_column_double(p->pStmt, i);
1871619484
sqlite3_uint64 ur;
1871719485
memcpy(&ur,&r,sizeof(r));
1871819486
if( ur==0x7ff0000000000000LL ){
18719
- raw_printf(p->out, "9.0e+999");
19487
+ oputz("9.0e+999");
1872019488
}else if( ur==0xfff0000000000000LL ){
18721
- raw_printf(p->out, "-9.0e+999");
19489
+ oputz("-9.0e+999");
1872219490
}else{
1872319491
sqlite3_int64 ir = (sqlite3_int64)r;
1872419492
if( r==(double)ir ){
1872519493
sqlite3_snprintf(50,z,"%lld.0", ir);
1872619494
}else{
1872719495
sqlite3_snprintf(50,z,"%!.20g", r);
1872819496
}
18729
- raw_printf(p->out, "%s", z);
19497
+ oputz(z);
1873019498
}
1873119499
}else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
1873219500
const void *pBlob = sqlite3_column_blob(p->pStmt, i);
1873319501
int nBlob = sqlite3_column_bytes(p->pStmt, i);
18734
- output_hex_blob(p->out, pBlob, nBlob);
19502
+ output_hex_blob(pBlob, nBlob);
1873519503
}else if( isNumber(azArg[i], 0) ){
18736
- utf8_printf(p->out,"%s", azArg[i]);
19504
+ oputz(azArg[i]);
1873719505
}else if( ShellHasFlag(p, SHFLG_Newlines) ){
18738
- output_quoted_string(p->out, azArg[i]);
19506
+ output_quoted_string(azArg[i]);
1873919507
}else{
18740
- output_quoted_escaped_string(p->out, azArg[i]);
19508
+ output_quoted_escaped_string(azArg[i]);
1874119509
}
1874219510
}
18743
- raw_printf(p->out,");\n");
19511
+ oputz(");\n");
1874419512
break;
1874519513
}
1874619514
case MODE_Json: {
1874719515
if( azArg==0 ) break;
1874819516
if( p->cnt==0 ){
@@ -18750,93 +19518,93 @@
1875019518
}else{
1875119519
fputs(",\n{", p->out);
1875219520
}
1875319521
p->cnt++;
1875419522
for(i=0; i<nArg; i++){
18755
- output_json_string(p->out, azCol[i], -1);
18756
- putc(':', p->out);
19523
+ output_json_string(azCol[i], -1);
19524
+ oputz(":");
1875719525
if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
18758
- fputs("null",p->out);
19526
+ oputz("null");
1875919527
}else if( aiType && aiType[i]==SQLITE_FLOAT ){
1876019528
char z[50];
1876119529
double r = sqlite3_column_double(p->pStmt, i);
1876219530
sqlite3_uint64 ur;
1876319531
memcpy(&ur,&r,sizeof(r));
1876419532
if( ur==0x7ff0000000000000LL ){
18765
- raw_printf(p->out, "9.0e+999");
19533
+ oputz("9.0e+999");
1876619534
}else if( ur==0xfff0000000000000LL ){
18767
- raw_printf(p->out, "-9.0e+999");
19535
+ oputz("-9.0e+999");
1876819536
}else{
1876919537
sqlite3_snprintf(50,z,"%!.20g", r);
18770
- raw_printf(p->out, "%s", z);
19538
+ oputz(z);
1877119539
}
1877219540
}else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
1877319541
const void *pBlob = sqlite3_column_blob(p->pStmt, i);
1877419542
int nBlob = sqlite3_column_bytes(p->pStmt, i);
18775
- output_json_string(p->out, pBlob, nBlob);
19543
+ output_json_string(pBlob, nBlob);
1877619544
}else if( aiType && aiType[i]==SQLITE_TEXT ){
18777
- output_json_string(p->out, azArg[i], -1);
19545
+ output_json_string(azArg[i], -1);
1877819546
}else{
18779
- utf8_printf(p->out,"%s", azArg[i]);
19547
+ oputz(azArg[i]);
1878019548
}
1878119549
if( i<nArg-1 ){
18782
- putc(',', p->out);
19550
+ oputz(",");
1878319551
}
1878419552
}
18785
- putc('}', p->out);
19553
+ oputz("}");
1878619554
break;
1878719555
}
1878819556
case MODE_Quote: {
1878919557
if( azArg==0 ) break;
1879019558
if( p->cnt==0 && p->showHeader ){
1879119559
for(i=0; i<nArg; i++){
1879219560
if( i>0 ) fputs(p->colSeparator, p->out);
18793
- output_quoted_string(p->out, azCol[i]);
19561
+ output_quoted_string(azCol[i]);
1879419562
}
1879519563
fputs(p->rowSeparator, p->out);
1879619564
}
1879719565
p->cnt++;
1879819566
for(i=0; i<nArg; i++){
1879919567
if( i>0 ) fputs(p->colSeparator, p->out);
1880019568
if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
18801
- utf8_printf(p->out,"NULL");
19569
+ oputz("NULL");
1880219570
}else if( aiType && aiType[i]==SQLITE_TEXT ){
18803
- output_quoted_string(p->out, azArg[i]);
19571
+ output_quoted_string(azArg[i]);
1880419572
}else if( aiType && aiType[i]==SQLITE_INTEGER ){
18805
- utf8_printf(p->out,"%s", azArg[i]);
19573
+ oputz(azArg[i]);
1880619574
}else if( aiType && aiType[i]==SQLITE_FLOAT ){
1880719575
char z[50];
1880819576
double r = sqlite3_column_double(p->pStmt, i);
1880919577
sqlite3_snprintf(50,z,"%!.20g", r);
18810
- raw_printf(p->out, "%s", z);
19578
+ oputz(z);
1881119579
}else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
1881219580
const void *pBlob = sqlite3_column_blob(p->pStmt, i);
1881319581
int nBlob = sqlite3_column_bytes(p->pStmt, i);
18814
- output_hex_blob(p->out, pBlob, nBlob);
19582
+ output_hex_blob(pBlob, nBlob);
1881519583
}else if( isNumber(azArg[i], 0) ){
18816
- utf8_printf(p->out,"%s", azArg[i]);
19584
+ oputz(azArg[i]);
1881719585
}else{
18818
- output_quoted_string(p->out, azArg[i]);
19586
+ output_quoted_string(azArg[i]);
1881919587
}
1882019588
}
1882119589
fputs(p->rowSeparator, p->out);
1882219590
break;
1882319591
}
1882419592
case MODE_Ascii: {
1882519593
if( p->cnt++==0 && p->showHeader ){
1882619594
for(i=0; i<nArg; i++){
18827
- if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
18828
- utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
19595
+ if( i>0 ) oputz(p->colSeparator);
19596
+ oputz(azCol[i] ? azCol[i] : "");
1882919597
}
18830
- utf8_printf(p->out, "%s", p->rowSeparator);
19598
+ oputz(p->rowSeparator);
1883119599
}
1883219600
if( azArg==0 ) break;
1883319601
for(i=0; i<nArg; i++){
18834
- if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
18835
- utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
19602
+ if( i>0 ) oputz(p->colSeparator);
19603
+ oputz(azArg[i] ? azArg[i] : p->nullValue);
1883619604
}
18837
- utf8_printf(p->out, "%s", p->rowSeparator);
19605
+ oputz(p->rowSeparator);
1883819606
break;
1883919607
}
1884019608
case MODE_EQP: {
1884119609
eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
1884219610
break;
@@ -18911,11 +19679,11 @@
1891119679
"INSERT INTO selftest(tno,op,cmd,ans)"
1891219680
" SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
1891319681
"DROP TABLE [_shell$self];"
1891419682
,0,0,&zErrMsg);
1891519683
if( zErrMsg ){
18916
- utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
19684
+ eputf("SELFTEST initialization failure: %s\n", zErrMsg);
1891719685
sqlite3_free(zErrMsg);
1891819686
}
1891919687
sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
1892019688
}
1892119689
@@ -19014,37 +19782,36 @@
1901419782
int i;
1901519783
const char *z;
1901619784
rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
1901719785
if( rc!=SQLITE_OK || !pSelect ){
1901819786
char *zContext = shell_error_context(zSelect, p->db);
19019
- utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n%s", rc,
19020
- sqlite3_errmsg(p->db), zContext);
19787
+ oputf("/**** ERROR: (%d) %s *****/\n%s",
19788
+ rc, sqlite3_errmsg(p->db), zContext);
1902119789
sqlite3_free(zContext);
1902219790
if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
1902319791
return rc;
1902419792
}
1902519793
rc = sqlite3_step(pSelect);
1902619794
nResult = sqlite3_column_count(pSelect);
1902719795
while( rc==SQLITE_ROW ){
1902819796
z = (const char*)sqlite3_column_text(pSelect, 0);
19029
- utf8_printf(p->out, "%s", z);
19797
+ oputf("%s", z);
1903019798
for(i=1; i<nResult; i++){
19031
- utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
19799
+ oputf(",%s", sqlite3_column_text(pSelect, i));
1903219800
}
1903319801
if( z==0 ) z = "";
1903419802
while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
1903519803
if( z[0] ){
19036
- raw_printf(p->out, "\n;\n");
19804
+ oputz("\n;\n");
1903719805
}else{
19038
- raw_printf(p->out, ";\n");
19806
+ oputz(";\n");
1903919807
}
1904019808
rc = sqlite3_step(pSelect);
1904119809
}
1904219810
rc = sqlite3_finalize(pSelect);
1904319811
if( rc!=SQLITE_OK ){
19044
- utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
19045
- sqlite3_errmsg(p->db));
19812
+ oputf("/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db));
1904619813
if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
1904719814
}
1904819815
return rc;
1904919816
}
1905019817
@@ -19076,11 +19843,11 @@
1907619843
1907719844
#ifdef __linux__
1907819845
/*
1907919846
** Attempt to display I/O stats on Linux using /proc/PID/io
1908019847
*/
19081
-static void displayLinuxIoStats(FILE *out){
19848
+static void displayLinuxIoStats(void){
1908219849
FILE *in;
1908319850
char z[200];
1908419851
sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
1908519852
in = fopen(z, "rb");
1908619853
if( in==0 ) return;
@@ -19099,11 +19866,11 @@
1909919866
};
1910019867
int i;
1910119868
for(i=0; i<ArraySize(aTrans); i++){
1910219869
int n = strlen30(aTrans[i].zPattern);
1910319870
if( cli_strncmp(aTrans[i].zPattern, z, n)==0 ){
19104
- utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
19871
+ oputf("%-36s %s", aTrans[i].zDesc, &z[n]);
1910519872
break;
1910619873
}
1910719874
}
1910819875
}
1910919876
fclose(in);
@@ -19112,11 +19879,10 @@
1911219879
1911319880
/*
1911419881
** Display a single line of status using 64-bit values.
1911519882
*/
1911619883
static void displayStatLine(
19117
- ShellState *p, /* The shell context */
1911819884
char *zLabel, /* Label for this one line */
1911919885
char *zFormat, /* Format for the result */
1912019886
int iStatusCtrl, /* Which status to display */
1912119887
int bReset /* True to reset the stats */
1912219888
){
@@ -19131,11 +19897,11 @@
1913119897
if( nPercent>1 ){
1913219898
sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
1913319899
}else{
1913419900
sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
1913519901
}
19136
- raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
19902
+ oputf("%-36s %s\n", zLabel, zLine);
1913719903
}
1913819904
1913919905
/*
1914019906
** Display memory stats.
1914119907
*/
@@ -19144,141 +19910,130 @@
1914419910
ShellState *pArg, /* Pointer to ShellState */
1914519911
int bReset /* True to reset the stats */
1914619912
){
1914719913
int iCur;
1914819914
int iHiwtr;
19149
- FILE *out;
1915019915
if( pArg==0 || pArg->out==0 ) return 0;
19151
- out = pArg->out;
1915219916
1915319917
if( pArg->pStmt && pArg->statsOn==2 ){
1915419918
int nCol, i, x;
1915519919
sqlite3_stmt *pStmt = pArg->pStmt;
1915619920
char z[100];
1915719921
nCol = sqlite3_column_count(pStmt);
19158
- raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
19922
+ oputf("%-36s %d\n", "Number of output columns:", nCol);
1915919923
for(i=0; i<nCol; i++){
1916019924
sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
19161
- utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
19925
+ oputf("%-36s %s\n", z, sqlite3_column_name(pStmt,i));
1916219926
#ifndef SQLITE_OMIT_DECLTYPE
1916319927
sqlite3_snprintf(30, z+x, "declared type:");
19164
- utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
19928
+ oputf("%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
1916519929
#endif
1916619930
#ifdef SQLITE_ENABLE_COLUMN_METADATA
1916719931
sqlite3_snprintf(30, z+x, "database name:");
19168
- utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
19932
+ oputf("%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
1916919933
sqlite3_snprintf(30, z+x, "table name:");
19170
- utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
19934
+ oputf("%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
1917119935
sqlite3_snprintf(30, z+x, "origin name:");
19172
- utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
19936
+ oputf("%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
1917319937
#endif
1917419938
}
1917519939
}
1917619940
1917719941
if( pArg->statsOn==3 ){
1917819942
if( pArg->pStmt ){
1917919943
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP,bReset);
19180
- raw_printf(pArg->out, "VM-steps: %d\n", iCur);
19944
+ oputf("VM-steps: %d\n", iCur);
1918119945
}
1918219946
return 0;
1918319947
}
1918419948
19185
- displayStatLine(pArg, "Memory Used:",
19949
+ displayStatLine("Memory Used:",
1918619950
"%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
19187
- displayStatLine(pArg, "Number of Outstanding Allocations:",
19951
+ displayStatLine("Number of Outstanding Allocations:",
1918819952
"%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
1918919953
if( pArg->shellFlgs & SHFLG_Pagecache ){
19190
- displayStatLine(pArg, "Number of Pcache Pages Used:",
19954
+ displayStatLine("Number of Pcache Pages Used:",
1919119955
"%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
1919219956
}
19193
- displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
19957
+ displayStatLine("Number of Pcache Overflow Bytes:",
1919419958
"%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
19195
- displayStatLine(pArg, "Largest Allocation:",
19959
+ displayStatLine("Largest Allocation:",
1919619960
"%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
19197
- displayStatLine(pArg, "Largest Pcache Allocation:",
19961
+ displayStatLine("Largest Pcache Allocation:",
1919819962
"%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
1919919963
#ifdef YYTRACKMAXSTACKDEPTH
19200
- displayStatLine(pArg, "Deepest Parser Stack:",
19964
+ displayStatLine("Deepest Parser Stack:",
1920119965
"%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
1920219966
#endif
1920319967
1920419968
if( db ){
1920519969
if( pArg->shellFlgs & SHFLG_Lookaside ){
1920619970
iHiwtr = iCur = -1;
1920719971
sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
1920819972
&iCur, &iHiwtr, bReset);
19209
- raw_printf(pArg->out,
19210
- "Lookaside Slots Used: %d (max %d)\n",
19211
- iCur, iHiwtr);
19973
+ oputf("Lookaside Slots Used: %d (max %d)\n", iCur, iHiwtr);
1921219974
sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
1921319975
&iCur, &iHiwtr, bReset);
19214
- raw_printf(pArg->out, "Successful lookaside attempts: %d\n",
19215
- iHiwtr);
19976
+ oputf("Successful lookaside attempts: %d\n", iHiwtr);
1921619977
sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
1921719978
&iCur, &iHiwtr, bReset);
19218
- raw_printf(pArg->out, "Lookaside failures due to size: %d\n",
19219
- iHiwtr);
19979
+ oputf("Lookaside failures due to size: %d\n", iHiwtr);
1922019980
sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
1922119981
&iCur, &iHiwtr, bReset);
19222
- raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n",
19223
- iHiwtr);
19982
+ oputf("Lookaside failures due to OOM: %d\n", iHiwtr);
1922419983
}
1922519984
iHiwtr = iCur = -1;
1922619985
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
19227
- raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n",
19228
- iCur);
19986
+ oputf("Pager Heap Usage: %d bytes\n", iCur);
1922919987
iHiwtr = iCur = -1;
1923019988
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
19231
- raw_printf(pArg->out, "Page cache hits: %d\n", iCur);
19989
+ oputf("Page cache hits: %d\n", iCur);
1923219990
iHiwtr = iCur = -1;
1923319991
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
19234
- raw_printf(pArg->out, "Page cache misses: %d\n", iCur);
19992
+ oputf("Page cache misses: %d\n", iCur);
1923519993
iHiwtr = iCur = -1;
1923619994
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
19237
- raw_printf(pArg->out, "Page cache writes: %d\n", iCur);
19995
+ oputf("Page cache writes: %d\n", iCur);
1923819996
iHiwtr = iCur = -1;
1923919997
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
19240
- raw_printf(pArg->out, "Page cache spills: %d\n", iCur);
19998
+ oputf("Page cache spills: %d\n", iCur);
1924119999
iHiwtr = iCur = -1;
1924220000
sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
19243
- raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n",
19244
- iCur);
20001
+ oputf("Schema Heap Usage: %d bytes\n", iCur);
1924520002
iHiwtr = iCur = -1;
1924620003
sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
19247
- raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n",
19248
- iCur);
20004
+ oputf("Statement Heap/Lookaside Usage: %d bytes\n", iCur);
1924920005
}
1925020006
1925120007
if( pArg->pStmt ){
1925220008
int iHit, iMiss;
1925320009
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
1925420010
bReset);
19255
- raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur);
20011
+ oputf("Fullscan Steps: %d\n", iCur);
1925620012
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
19257
- raw_printf(pArg->out, "Sort Operations: %d\n", iCur);
20013
+ oputf("Sort Operations: %d\n", iCur);
1925820014
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
19259
- raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur);
20015
+ oputf("Autoindex Inserts: %d\n", iCur);
1926020016
iHit = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_HIT,
1926120017
bReset);
1926220018
iMiss = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_MISS,
1926320019
bReset);
1926420020
if( iHit || iMiss ){
19265
- raw_printf(pArg->out, "Bloom filter bypass taken: %d/%d\n",
19266
- iHit, iHit+iMiss);
20021
+ oputf("Bloom filter bypass taken: %d/%d\n", iHit, iHit+iMiss);
1926720022
}
1926820023
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
19269
- raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur);
20024
+ oputf("Virtual Machine Steps: %d\n", iCur);
1927020025
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
19271
- raw_printf(pArg->out, "Reprepare operations: %d\n", iCur);
20026
+ oputf("Reprepare operations: %d\n", iCur);
1927220027
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
19273
- raw_printf(pArg->out, "Number of times run: %d\n", iCur);
20028
+ oputf("Number of times run: %d\n", iCur);
1927420029
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
19275
- raw_printf(pArg->out, "Memory used by prepared stmt: %d\n", iCur);
20030
+ oputf("Memory used by prepared stmt: %d\n", iCur);
1927620031
}
1927720032
1927820033
#ifdef __linux__
19279
- displayLinuxIoStats(pArg->out);
20034
+ displayLinuxIoStats();
1928020035
#endif
1928120036
1928220037
/* Do not remove this machine readable comment: extra-stats-output-here */
1928320038
1928420039
return 0;
@@ -19509,11 +20264,11 @@
1950920264
#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
1951020265
UNUSED_PARAMETER(db);
1951120266
UNUSED_PARAMETER(pArg);
1951220267
#else
1951320268
if( pArg->scanstatsOn==3 ){
19514
- const char *zSql =
20269
+ const char *zSql =
1951520270
" SELECT addr, opcode, p1, p2, p3, p4, p5, comment, nexec,"
1951620271
" round(ncycle*100.0 / (sum(ncycle) OVER ()), 2)||'%' AS cycles"
1951720272
" FROM bytecode(?)";
1951820273
1951920274
int rc = SQLITE_OK;
@@ -19655,21 +20410,21 @@
1965520410
#define BOX_1234 "\342\224\274" /* U+253c -|- */
1965620411
1965720412
/* Draw horizontal line N characters long using unicode box
1965820413
** characters
1965920414
*/
19660
-static void print_box_line(FILE *out, int N){
20415
+static void print_box_line(int N){
1966120416
const char zDash[] =
1966220417
BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
1966320418
BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
1966420419
const int nDash = sizeof(zDash) - 1;
1966520420
N *= 3;
1966620421
while( N>nDash ){
19667
- utf8_printf(out, zDash);
20422
+ oputz(zDash);
1966820423
N -= nDash;
1966920424
}
19670
- utf8_printf(out, "%.*s", N, zDash);
20425
+ oputf("%.*s", N, zDash);
1967120426
}
1967220427
1967320428
/*
1967420429
** Draw a horizontal separator for a MODE_Box table.
1967520430
*/
@@ -19680,19 +20435,19 @@
1968020435
const char *zSep2,
1968120436
const char *zSep3
1968220437
){
1968320438
int i;
1968420439
if( nArg>0 ){
19685
- utf8_printf(p->out, "%s", zSep1);
19686
- print_box_line(p->out, p->actualWidth[0]+2);
20440
+ oputz(zSep1);
20441
+ print_box_line(p->actualWidth[0]+2);
1968720442
for(i=1; i<nArg; i++){
19688
- utf8_printf(p->out, "%s", zSep2);
19689
- print_box_line(p->out, p->actualWidth[i]+2);
20443
+ oputz(zSep2);
20444
+ print_box_line(p->actualWidth[i]+2);
1969020445
}
19691
- utf8_printf(p->out, "%s", zSep3);
20446
+ oputz(zSep3);
1969220447
}
19693
- fputs("\n", p->out);
20448
+ oputz("\n");
1969420449
}
1969520450
1969620451
/*
1969720452
** z[] is a line of text that is to be displayed the .mode box or table or
1969820453
** similar tabular formats. z[] might contain control characters such
@@ -19951,15 +20706,15 @@
1995120706
rowSep = "\n";
1995220707
if( p->showHeader ){
1995320708
for(i=0; i<nColumn; i++){
1995420709
w = p->actualWidth[i];
1995520710
if( p->colWidth[i]<0 ) w = -w;
19956
- utf8_width_print(p->out, w, azData[i]);
20711
+ utf8_width_print(w, azData[i]);
1995720712
fputs(i==nColumn-1?"\n":" ", p->out);
1995820713
}
1995920714
for(i=0; i<nColumn; i++){
19960
- print_dashes(p->out, p->actualWidth[i]);
20715
+ print_dashes(p->actualWidth[i]);
1996120716
fputs(i==nColumn-1?"\n":" ", p->out);
1996220717
}
1996320718
}
1996420719
break;
1996520720
}
@@ -19969,12 +20724,12 @@
1996920724
print_row_separator(p, nColumn, "+");
1997020725
fputs("| ", p->out);
1997120726
for(i=0; i<nColumn; i++){
1997220727
w = p->actualWidth[i];
1997320728
n = strlenChar(azData[i]);
19974
- utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
19975
- fputs(i==nColumn-1?" |\n":" | ", p->out);
20729
+ oputf("%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
20730
+ oputz(i==nColumn-1?" |\n":" | ");
1997620731
}
1997720732
print_row_separator(p, nColumn, "+");
1997820733
break;
1997920734
}
1998020735
case MODE_Markdown: {
@@ -19982,66 +20737,66 @@
1998220737
rowSep = " |\n";
1998320738
fputs("| ", p->out);
1998420739
for(i=0; i<nColumn; i++){
1998520740
w = p->actualWidth[i];
1998620741
n = strlenChar(azData[i]);
19987
- utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
19988
- fputs(i==nColumn-1?" |\n":" | ", p->out);
20742
+ oputf("%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
20743
+ oputz(i==nColumn-1?" |\n":" | ");
1998920744
}
1999020745
print_row_separator(p, nColumn, "|");
1999120746
break;
1999220747
}
1999320748
case MODE_Box: {
1999420749
colSep = " " BOX_13 " ";
1999520750
rowSep = " " BOX_13 "\n";
1999620751
print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
19997
- utf8_printf(p->out, BOX_13 " ");
20752
+ oputz(BOX_13 " ");
1999820753
for(i=0; i<nColumn; i++){
1999920754
w = p->actualWidth[i];
2000020755
n = strlenChar(azData[i]);
20001
- utf8_printf(p->out, "%*s%s%*s%s",
20002
- (w-n)/2, "", azData[i], (w-n+1)/2, "",
20003
- i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
20756
+ oputf("%*s%s%*s%s",
20757
+ (w-n)/2, "", azData[i], (w-n+1)/2, "",
20758
+ i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
2000420759
}
2000520760
print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
2000620761
break;
2000720762
}
2000820763
}
2000920764
for(i=nColumn, j=0; i<nTotal; i++, j++){
2001020765
if( j==0 && p->cMode!=MODE_Column ){
20011
- utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| ");
20766
+ oputz(p->cMode==MODE_Box?BOX_13" ":"| ");
2001220767
}
2001320768
z = azData[i];
2001420769
if( z==0 ) z = p->nullValue;
2001520770
w = p->actualWidth[j];
2001620771
if( p->colWidth[j]<0 ) w = -w;
20017
- utf8_width_print(p->out, w, z);
20772
+ utf8_width_print(w, z);
2001820773
if( j==nColumn-1 ){
20019
- utf8_printf(p->out, "%s", rowSep);
20774
+ oputz(rowSep);
2002020775
if( bMultiLineRowExists && abRowDiv[i/nColumn-1] && i+1<nTotal ){
2002120776
if( p->cMode==MODE_Table ){
2002220777
print_row_separator(p, nColumn, "+");
2002320778
}else if( p->cMode==MODE_Box ){
2002420779
print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
2002520780
}else if( p->cMode==MODE_Column ){
20026
- raw_printf(p->out, "\n");
20781
+ oputz("\n");
2002720782
}
2002820783
}
2002920784
j = -1;
2003020785
if( seenInterrupt ) goto columnar_end;
2003120786
}else{
20032
- utf8_printf(p->out, "%s", colSep);
20787
+ oputz(colSep);
2003320788
}
2003420789
}
2003520790
if( p->cMode==MODE_Table ){
2003620791
print_row_separator(p, nColumn, "+");
2003720792
}else if( p->cMode==MODE_Box ){
2003820793
print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
2003920794
}
2004020795
columnar_end:
2004120796
if( seenInterrupt ){
20042
- utf8_printf(p->out, "Interrupt\n");
20797
+ oputz("Interrupt\n");
2004320798
}
2004420799
nData = (nRow+1)*nColumn;
2004520800
for(i=0; i<nData; i++){
2004620801
z = azData[i];
2004720802
if( z!=zEmpty && z!=zShowNull ) free(azData[i]);
@@ -20176,34 +20931,33 @@
2017620931
int rc = SQLITE_OK;
2017720932
sqlite3expert *p = pState->expert.pExpert;
2017820933
assert( p );
2017920934
assert( bCancel || pzErr==0 || *pzErr==0 );
2018020935
if( bCancel==0 ){
20181
- FILE *out = pState->out;
2018220936
int bVerbose = pState->expert.bVerbose;
2018320937
2018420938
rc = sqlite3_expert_analyze(p, pzErr);
2018520939
if( rc==SQLITE_OK ){
2018620940
int nQuery = sqlite3_expert_count(p);
2018720941
int i;
2018820942
2018920943
if( bVerbose ){
2019020944
const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
20191
- raw_printf(out, "-- Candidates -----------------------------\n");
20192
- raw_printf(out, "%s\n", zCand);
20945
+ oputz("-- Candidates -----------------------------\n");
20946
+ oputf("%s\n", zCand);
2019320947
}
2019420948
for(i=0; i<nQuery; i++){
2019520949
const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
2019620950
const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
2019720951
const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
2019820952
if( zIdx==0 ) zIdx = "(no new indexes)\n";
2019920953
if( bVerbose ){
20200
- raw_printf(out, "-- Query %d --------------------------------\n",i+1);
20201
- raw_printf(out, "%s\n\n", zSql);
20954
+ oputf("-- Query %d --------------------------------\n",i+1);
20955
+ oputf("%s\n\n", zSql);
2020220956
}
20203
- raw_printf(out, "%s\n", zIdx);
20204
- raw_printf(out, "%s\n", zEQP);
20957
+ oputf("%s\n", zIdx);
20958
+ oputf("%s\n", zEQP);
2020520959
}
2020620960
}
2020720961
}
2020820962
sqlite3_expert_destroy(p);
2020920963
pState->expert.pExpert = 0;
@@ -20234,31 +20988,30 @@
2023420988
if( n>=2 && 0==cli_strncmp(z, "-verbose", n) ){
2023520989
pState->expert.bVerbose = 1;
2023620990
}
2023720991
else if( n>=2 && 0==cli_strncmp(z, "-sample", n) ){
2023820992
if( i==(nArg-1) ){
20239
- raw_printf(stderr, "option requires an argument: %s\n", z);
20993
+ eputf("option requires an argument: %s\n", z);
2024020994
rc = SQLITE_ERROR;
2024120995
}else{
2024220996
iSample = (int)integerValue(azArg[++i]);
2024320997
if( iSample<0 || iSample>100 ){
20244
- raw_printf(stderr, "value out of range: %s\n", azArg[i]);
20998
+ eputf("value out of range: %s\n", azArg[i]);
2024520999
rc = SQLITE_ERROR;
2024621000
}
2024721001
}
2024821002
}
2024921003
else{
20250
- raw_printf(stderr, "unknown option: %s\n", z);
21004
+ eputf("unknown option: %s\n", z);
2025121005
rc = SQLITE_ERROR;
2025221006
}
2025321007
}
2025421008
2025521009
if( rc==SQLITE_OK ){
2025621010
pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
2025721011
if( pState->expert.pExpert==0 ){
20258
- raw_printf(stderr, "sqlite3_expert_new: %s\n",
20259
- zErr ? zErr : "out of memory");
21012
+ eputf("sqlite3_expert_new: %s\n", zErr ? zErr : "out of memory");
2026021013
rc = SQLITE_ERROR;
2026121014
}else{
2026221015
sqlite3_expert_config(
2026321016
pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
2026421017
);
@@ -20581,33 +21334,33 @@
2058121334
if( zType==0 ) return 0;
2058221335
dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
2058321336
noSys = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
2058421337
2058521338
if( cli_strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
20586
- if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
21339
+ if( !dataOnly ) oputz("DELETE FROM sqlite_sequence;\n");
2058721340
}else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
20588
- if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n");
21341
+ if( !dataOnly ) oputz("ANALYZE sqlite_schema;\n");
2058921342
}else if( cli_strncmp(zTable, "sqlite_", 7)==0 ){
2059021343
return 0;
2059121344
}else if( dataOnly ){
2059221345
/* no-op */
2059321346
}else if( cli_strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
2059421347
char *zIns;
2059521348
if( !p->writableSchema ){
20596
- raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
21349
+ oputz("PRAGMA writable_schema=ON;\n");
2059721350
p->writableSchema = 1;
2059821351
}
2059921352
zIns = sqlite3_mprintf(
2060021353
"INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
2060121354
"VALUES('table','%q','%q',0,'%q');",
2060221355
zTable, zTable, zSql);
2060321356
shell_check_oom(zIns);
20604
- utf8_printf(p->out, "%s\n", zIns);
21357
+ oputf("%s\n", zIns);
2060521358
sqlite3_free(zIns);
2060621359
return 0;
2060721360
}else{
20608
- printSchemaLine(p->out, zSql, ";\n");
21361
+ printSchemaLine(zSql, ";\n");
2060921362
}
2061021363
2061121364
if( cli_strcmp(zType, "table")==0 ){
2061221365
ShellText sSelect;
2061321366
ShellText sTable;
@@ -20661,11 +21414,11 @@
2066121414
savedMode = p->mode;
2066221415
p->zDestTable = sTable.z;
2066321416
p->mode = p->cMode = MODE_Insert;
2066421417
rc = shell_exec(p, sSelect.z, 0);
2066521418
if( (rc&0xff)==SQLITE_CORRUPT ){
20666
- raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
21419
+ oputz("/****** CORRUPTION ERROR *******/\n");
2066721420
toggleSelectOrder(p->db);
2066821421
shell_exec(p, sSelect.z, 0);
2066921422
toggleSelectOrder(p->db);
2067021423
}
2067121424
p->zDestTable = savedDestTable;
@@ -20692,22 +21445,22 @@
2069221445
char *zErr = 0;
2069321446
rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
2069421447
if( rc==SQLITE_CORRUPT ){
2069521448
char *zQ2;
2069621449
int len = strlen30(zQuery);
20697
- raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
21450
+ oputz("/****** CORRUPTION ERROR *******/\n");
2069821451
if( zErr ){
20699
- utf8_printf(p->out, "/****** %s ******/\n", zErr);
21452
+ oputf("/****** %s ******/\n", zErr);
2070021453
sqlite3_free(zErr);
2070121454
zErr = 0;
2070221455
}
2070321456
zQ2 = malloc( len+100 );
2070421457
if( zQ2==0 ) return rc;
2070521458
sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
2070621459
rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
2070721460
if( rc ){
20708
- utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
21461
+ oputf("/****** ERROR: %s ******/\n", zErr);
2070921462
}else{
2071021463
rc = SQLITE_CORRUPT;
2071121464
}
2071221465
sqlite3_free(zErr);
2071321466
free(zQ2);
@@ -21059,24 +21812,24 @@
2105921812
hh &= ~HH_Summary;
2106021813
break;
2106121814
}
2106221815
if( ((hw^hh)&HH_Undoc)==0 ){
2106321816
if( (hh&HH_Summary)!=0 ){
21064
- utf8_printf(out, ".%s\n", azHelp[i]+1);
21817
+ sputf(out, ".%s\n", azHelp[i]+1);
2106521818
++n;
2106621819
}else if( (hw&HW_SummaryOnly)==0 ){
21067
- utf8_printf(out, "%s\n", azHelp[i]);
21820
+ sputf(out, "%s\n", azHelp[i]);
2106821821
}
2106921822
}
2107021823
}
2107121824
}else{
2107221825
/* Seek documented commands for which zPattern is an exact prefix */
2107321826
zPat = sqlite3_mprintf(".%s*", zPattern);
2107421827
shell_check_oom(zPat);
2107521828
for(i=0; i<ArraySize(azHelp); i++){
2107621829
if( sqlite3_strglob(zPat, azHelp[i])==0 ){
21077
- utf8_printf(out, "%s\n", azHelp[i]);
21830
+ sputf(out, "%s\n", azHelp[i]);
2107821831
j = i+1;
2107921832
n++;
2108021833
}
2108121834
}
2108221835
sqlite3_free(zPat);
@@ -21083,11 +21836,11 @@
2108321836
if( n ){
2108421837
if( n==1 ){
2108521838
/* when zPattern is a prefix of exactly one command, then include
2108621839
** the details of that command, which should begin at offset j */
2108721840
while( j<ArraySize(azHelp)-1 && azHelp[j][0]==' ' ){
21088
- utf8_printf(out, "%s\n", azHelp[j]);
21841
+ sputf(out, "%s\n", azHelp[j]);
2108921842
j++;
2109021843
}
2109121844
}
2109221845
return n;
2109321846
}
@@ -21100,14 +21853,14 @@
2110021853
while( i<ArraySize(azHelp)-1 && azHelp[i+1][0]==' ' ) ++i;
2110121854
continue;
2110221855
}
2110321856
if( azHelp[i][0]=='.' ) j = i;
2110421857
if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
21105
- utf8_printf(out, "%s\n", azHelp[j]);
21858
+ sputf(out, "%s\n", azHelp[j]);
2110621859
while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]==' ' ){
2110721860
j++;
21108
- utf8_printf(out, "%s\n", azHelp[j]);
21861
+ sputf(out, "%s\n", azHelp[j]);
2110921862
}
2111021863
i = j;
2111121864
n++;
2111221865
}
2111321866
}
@@ -21141,27 +21894,27 @@
2114121894
char *pBuf;
2114221895
int rc;
2114321896
if( in==0 ) return 0;
2114421897
rc = fseek(in, 0, SEEK_END);
2114521898
if( rc!=0 ){
21146
- raw_printf(stderr, "Error: '%s' not seekable\n", zName);
21899
+ eputf("Error: '%s' not seekable\n", zName);
2114721900
fclose(in);
2114821901
return 0;
2114921902
}
2115021903
nIn = ftell(in);
2115121904
rewind(in);
2115221905
pBuf = sqlite3_malloc64( nIn+1 );
2115321906
if( pBuf==0 ){
21154
- raw_printf(stderr, "Error: out of memory\n");
21907
+ eputz("Error: out of memory\n");
2115521908
fclose(in);
2115621909
return 0;
2115721910
}
2115821911
nRead = fread(pBuf, nIn, 1, in);
2115921912
fclose(in);
2116021913
if( nRead!=1 ){
2116121914
sqlite3_free(pBuf);
21162
- raw_printf(stderr, "Error: cannot read '%s'\n", zName);
21915
+ eputf("Error: cannot read '%s'\n", zName);
2116321916
return 0;
2116421917
}
2116521918
pBuf[nIn] = 0;
2116621919
if( pnByte ) *pnByte = nIn;
2116721920
return pBuf;
@@ -21278,11 +22031,11 @@
2127822031
unsigned int x[16];
2127922032
char zLine[1000];
2128022033
if( zDbFilename ){
2128122034
in = fopen(zDbFilename, "r");
2128222035
if( in==0 ){
21283
- utf8_printf(stderr, "cannot open \"%s\" for reading\n", zDbFilename);
22036
+ eputf("cannot open \"%s\" for reading\n", zDbFilename);
2128422037
return 0;
2128522038
}
2128622039
nLine = 0;
2128722040
}else{
2128822041
in = p->in;
@@ -21299,11 +22052,11 @@
2129922052
n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */
2130022053
a = sqlite3_malloc( n ? n : 1 );
2130122054
shell_check_oom(a);
2130222055
memset(a, 0, n);
2130322056
if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
21304
- utf8_printf(stderr, "invalid pagesize\n");
22057
+ eputz("invalid pagesize\n");
2130522058
goto readHexDb_error;
2130622059
}
2130722060
for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
2130822061
rc = sscanf(zLine, "| page %d offset %d", &j, &k);
2130922062
if( rc==2 ){
@@ -21341,11 +22094,11 @@
2134122094
if(cli_strncmp(zLine, "| end ", 6)==0 ) break;
2134222095
}
2134322096
p->lineno = nLine;
2134422097
}
2134522098
sqlite3_free(a);
21346
- utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
22099
+ eputf("Error on line %d of --hexdb input\n", nLine);
2134722100
return 0;
2134822101
}
2134922102
#endif /* SQLITE_OMIT_DESERIALIZE */
2135022103
2135122104
/*
@@ -21417,26 +22170,23 @@
2141722170
break;
2141822171
}
2141922172
}
2142022173
globalDb = p->db;
2142122174
if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
21422
- utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
21423
- zDbFilename, sqlite3_errmsg(p->db));
22175
+ eputf("Error: unable to open database \"%s\": %s\n",
22176
+ zDbFilename, sqlite3_errmsg(p->db));
2142422177
if( (openFlags & OPEN_DB_KEEPALIVE)==0 ){
2142522178
exit(1);
2142622179
}
2142722180
sqlite3_close(p->db);
2142822181
sqlite3_open(":memory:", &p->db);
2142922182
if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
21430
- utf8_printf(stderr,
21431
- "Also: unable to open substitute in-memory database.\n"
21432
- );
22183
+ eputz("Also: unable to open substitute in-memory database.\n");
2143322184
exit(1);
2143422185
}else{
21435
- utf8_printf(stderr,
21436
- "Notice: using substitute in-memory database instead of \"%s\"\n",
21437
- zDbFilename);
22186
+ eputf("Notice: using substitute in-memory database instead of \"%s\"\n",
22187
+ zDbFilename);
2143822188
}
2143922189
}
2144022190
sqlite3_db_config(p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, (int)0, (int*)0);
2144122191
2144222192
/* Reflect the use or absence of --unsafe-testing invocation. */
@@ -21539,11 +22289,11 @@
2153922289
}
2154022290
rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
2154122291
SQLITE_DESERIALIZE_RESIZEABLE |
2154222292
SQLITE_DESERIALIZE_FREEONCLOSE);
2154322293
if( rc ){
21544
- utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
22294
+ eputf("Error: sqlite3_deserialize() returns %d\n", rc);
2154522295
}
2154622296
if( p->szMax>0 ){
2154722297
sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
2154822298
}
2154922299
}
@@ -21563,12 +22313,11 @@
2156322313
** Attempt to close the database connection. Report errors.
2156422314
*/
2156522315
void close_db(sqlite3 *db){
2156622316
int rc = sqlite3_close(db);
2156722317
if( rc ){
21568
- utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
21569
- rc, sqlite3_errmsg(db));
22318
+ eputf("Error: sqlite3_close() returns %d: %s\n", rc, sqlite3_errmsg(db));
2157022319
}
2157122320
}
2157222321
2157322322
#if HAVE_READLINE || HAVE_EDITLINE
2157422323
/*
@@ -21725,12 +22474,11 @@
2172522474
return 1;
2172622475
}
2172722476
if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
2172822477
return 0;
2172922478
}
21730
- utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
21731
- zArg);
22479
+ eputf("ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", zArg);
2173222480
return 0;
2173322481
}
2173422482
2173522483
/*
2173622484
** Set or clear a shell flag according to a boolean value.
@@ -21764,11 +22512,11 @@
2176422512
}else if( cli_strcmp(zFile, "off")==0 ){
2176522513
f = 0;
2176622514
}else{
2176722515
f = fopen(zFile, bTextMode ? "w" : "wb");
2176822516
if( f==0 ){
21769
- utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
22517
+ eputf("Error: cannot open \"%s\"\n", zFile);
2177022518
}
2177122519
}
2177222520
return f;
2177322521
}
2177422522
@@ -21786,11 +22534,11 @@
2178622534
sqlite3_stmt *pStmt;
2178722535
const char *zSql;
2178822536
i64 nSql;
2178922537
if( p->traceOut==0 ) return 0;
2179022538
if( mType==SQLITE_TRACE_CLOSE ){
21791
- utf8_printf(p->traceOut, "-- closing database connection\n");
22539
+ sputz(p->traceOut, "-- closing database connection\n");
2179222540
return 0;
2179322541
}
2179422542
if( mType!=SQLITE_TRACE_ROW && pX!=0 && ((const char*)pX)[0]=='-' ){
2179522543
zSql = (const char*)pX;
2179622544
}else{
@@ -21817,16 +22565,16 @@
2181722565
if( nSql>1000000000 ) nSql = 1000000000;
2181822566
while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
2181922567
switch( mType ){
2182022568
case SQLITE_TRACE_ROW:
2182122569
case SQLITE_TRACE_STMT: {
21822
- utf8_printf(p->traceOut, "%.*s;\n", (int)nSql, zSql);
22570
+ sputf(p->traceOut, "%.*s;\n", (int)nSql, zSql);
2182322571
break;
2182422572
}
2182522573
case SQLITE_TRACE_PROFILE: {
2182622574
sqlite3_int64 nNanosec = pX ? *(sqlite3_int64*)pX : 0;
21827
- utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", (int)nSql, zSql, nNanosec);
22575
+ sputf(p->traceOut, "%.*s; -- %lld ns\n", (int)nSql, zSql, nNanosec);
2182822576
break;
2182922577
}
2183022578
}
2183122579
return 0;
2183222580
}
@@ -21929,16 +22677,15 @@
2192922677
do{ p->n--; }while( p->z[p->n]!=cQuote );
2193022678
p->cTerm = c;
2193122679
break;
2193222680
}
2193322681
if( pc==cQuote && c!='\r' ){
21934
- utf8_printf(stderr, "%s:%d: unescaped %c character\n",
21935
- p->zFile, p->nLine, cQuote);
22682
+ eputf("%s:%d: unescaped %c character\n", p->zFile, p->nLine, cQuote);
2193622683
}
2193722684
if( c==EOF ){
21938
- utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
21939
- p->zFile, startLine, cQuote);
22685
+ eputf("%s:%d: unterminated %c-quoted field\n",
22686
+ p->zFile, startLine, cQuote);
2194022687
p->cTerm = c;
2194122688
break;
2194222689
}
2194322690
import_append_char(p, c);
2194422691
ppc = pc;
@@ -22032,13 +22779,12 @@
2203222779
2203322780
zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
2203422781
shell_check_oom(zQuery);
2203522782
rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
2203622783
if( rc ){
22037
- utf8_printf(stderr, "Error %d: %s on [%s]\n",
22038
- sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
22039
- zQuery);
22784
+ eputf("Error %d: %s on [%s]\n",
22785
+ sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zQuery);
2204022786
goto end_data_xfer;
2204122787
}
2204222788
n = sqlite3_column_count(pQuery);
2204322789
zInsert = sqlite3_malloc64(200 + nTable + n*3);
2204422790
shell_check_oom(zInsert);
@@ -22050,13 +22796,12 @@
2205022796
i += 2;
2205122797
}
2205222798
memcpy(zInsert+i, ");", 3);
2205322799
rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
2205422800
if( rc ){
22055
- utf8_printf(stderr, "Error %d: %s on [%s]\n",
22056
- sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
22057
- zInsert);
22801
+ eputf("Error %d: %s on [%s]\n",
22802
+ sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb), zInsert);
2205822803
goto end_data_xfer;
2205922804
}
2206022805
for(k=0; k<2; k++){
2206122806
while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
2206222807
for(i=0; i<n; i++){
@@ -22087,12 +22832,12 @@
2208722832
}
2208822833
}
2208922834
} /* End for */
2209022835
rc = sqlite3_step(pInsert);
2209122836
if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
22092
- utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
22093
- sqlite3_errmsg(newDb));
22837
+ eputf("Error %d: %s\n",
22838
+ sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb));
2209422839
}
2209522840
sqlite3_reset(pInsert);
2209622841
cnt++;
2209722842
if( (cnt%spinRate)==0 ){
2209822843
printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
@@ -22105,11 +22850,11 @@
2210522850
zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
2210622851
zTable);
2210722852
shell_check_oom(zQuery);
2210822853
rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
2210922854
if( rc ){
22110
- utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
22855
+ eputf("Warning: cannot step \"%s\" backwards", zTable);
2211122856
break;
2211222857
}
2211322858
} /* End for(k=0...) */
2211422859
2211522860
end_data_xfer:
@@ -22142,62 +22887,60 @@
2214222887
zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
2214322888
" WHERE %s ORDER BY rowid ASC", zWhere);
2214422889
shell_check_oom(zQuery);
2214522890
rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
2214622891
if( rc ){
22147
- utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
22148
- sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
22149
- zQuery);
22892
+ eputf("Error: (%d) %s on [%s]\n", sqlite3_extended_errcode(p->db),
22893
+ sqlite3_errmsg(p->db), zQuery);
2215022894
goto end_schema_xfer;
2215122895
}
2215222896
while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
2215322897
zName = sqlite3_column_text(pQuery, 0);
2215422898
zSql = sqlite3_column_text(pQuery, 1);
2215522899
if( zName==0 || zSql==0 ) continue;
2215622900
if( sqlite3_stricmp((char*)zName, "sqlite_sequence")!=0 ){
22157
- printf("%s... ", zName); fflush(stdout);
22901
+ sputf(stdout, "%s... ", zName); fflush(stdout);
2215822902
sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
2215922903
if( zErrMsg ){
22160
- utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
22904
+ eputf("Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
2216122905
sqlite3_free(zErrMsg);
2216222906
zErrMsg = 0;
2216322907
}
2216422908
}
2216522909
if( xForEach ){
2216622910
xForEach(p, newDb, (const char*)zName);
2216722911
}
22168
- printf("done\n");
22912
+ sputz(stdout, "done\n");
2216922913
}
2217022914
if( rc!=SQLITE_DONE ){
2217122915
sqlite3_finalize(pQuery);
2217222916
sqlite3_free(zQuery);
2217322917
zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
2217422918
" WHERE %s ORDER BY rowid DESC", zWhere);
2217522919
shell_check_oom(zQuery);
2217622920
rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
2217722921
if( rc ){
22178
- utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
22179
- sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
22180
- zQuery);
22922
+ eputf("Error: (%d) %s on [%s]\n",
22923
+ sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zQuery);
2218122924
goto end_schema_xfer;
2218222925
}
2218322926
while( sqlite3_step(pQuery)==SQLITE_ROW ){
2218422927
zName = sqlite3_column_text(pQuery, 0);
2218522928
zSql = sqlite3_column_text(pQuery, 1);
2218622929
if( zName==0 || zSql==0 ) continue;
2218722930
if( sqlite3_stricmp((char*)zName, "sqlite_sequence")==0 ) continue;
22188
- printf("%s... ", zName); fflush(stdout);
22931
+ sputf(stdout, "%s... ", zName); fflush(stdout);
2218922932
sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
2219022933
if( zErrMsg ){
22191
- utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
22934
+ eputf("Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
2219222935
sqlite3_free(zErrMsg);
2219322936
zErrMsg = 0;
2219422937
}
2219522938
if( xForEach ){
2219622939
xForEach(p, newDb, (const char*)zName);
2219722940
}
22198
- printf("done\n");
22941
+ sputz(stdout, "done\n");
2219922942
}
2220022943
}
2220122944
end_schema_xfer:
2220222945
sqlite3_finalize(pQuery);
2220322946
sqlite3_free(zQuery);
@@ -22210,17 +22953,16 @@
2221022953
*/
2221122954
static void tryToClone(ShellState *p, const char *zNewDb){
2221222955
int rc;
2221322956
sqlite3 *newDb = 0;
2221422957
if( access(zNewDb,0)==0 ){
22215
- utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
22958
+ eputf("File \"%s\" already exists.\n", zNewDb);
2221622959
return;
2221722960
}
2221822961
rc = sqlite3_open(zNewDb, &newDb);
2221922962
if( rc ){
22220
- utf8_printf(stderr, "Cannot create output database: %s\n",
22221
- sqlite3_errmsg(newDb));
22963
+ eputf("Cannot create output database: %s\n", sqlite3_errmsg(newDb));
2222222964
}else{
2222322965
sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
2222422966
sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
2222522967
tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
2222622968
tryToCloneSchema(p, newDb, "type!='table'", 0);
@@ -22227,10 +22969,22 @@
2222722969
sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
2222822970
sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
2222922971
}
2223022972
close_db(newDb);
2223122973
}
22974
+
22975
+#ifndef SQLITE_SHELL_FIDDLE
22976
+/*
22977
+** Change the output stream (file or pipe or console) to something else.
22978
+*/
22979
+static void output_redir(ShellState *p, FILE *pfNew){
22980
+ if( p->out != stdout ) eputz("Output already redirected.\n");
22981
+ else{
22982
+ p->out = pfNew;
22983
+ setOutputStream(pfNew);
22984
+ }
22985
+}
2223222986
2223322987
/*
2223422988
** Change the output file back to stdout.
2223522989
**
2223622990
** If the p->doXdgOpen flag is set, that means the output was being
@@ -22255,11 +23009,11 @@
2225523009
"xdg-open";
2225623010
#endif
2225723011
char *zCmd;
2225823012
zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
2225923013
if( system(zCmd) ){
22260
- utf8_printf(stderr, "Failed: [%s]\n", zCmd);
23014
+ eputf("Failed: [%s]\n", zCmd);
2226123015
}else{
2226223016
/* Give the start/open/xdg-open command some time to get
2226323017
** going before we continue, and potential delete the
2226423018
** p->zTempFile data file out from under it */
2226523019
sqlite3_sleep(2000);
@@ -22270,11 +23024,16 @@
2227023024
}
2227123025
#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
2227223026
}
2227323027
p->outfile[0] = 0;
2227423028
p->out = stdout;
23029
+ setOutputStream(stdout);
2227523030
}
23031
+#else
23032
+# define output_redir(SS,pfO)
23033
+# define output_reset(SS)
23034
+#endif
2227623035
2227723036
/*
2227823037
** Run an SQL command and return the single integer result.
2227923038
*/
2228023039
static int db_int(sqlite3 *db, const char *zSql){
@@ -22341,11 +23100,11 @@
2234123100
if( p->db==0 ) return 1;
2234223101
rc = sqlite3_prepare_v2(p->db,
2234323102
"SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
2234423103
-1, &pStmt, 0);
2234523104
if( rc ){
22346
- utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
23105
+ eputf("error: %s\n", sqlite3_errmsg(p->db));
2234723106
sqlite3_finalize(pStmt);
2234823107
return 1;
2234923108
}
2235023109
sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
2235123110
if( sqlite3_step(pStmt)==SQLITE_ROW
@@ -22354,32 +23113,32 @@
2235423113
const u8 *pb = sqlite3_column_blob(pStmt,0);
2235523114
shell_check_oom(pb);
2235623115
memcpy(aHdr, pb, 100);
2235723116
sqlite3_finalize(pStmt);
2235823117
}else{
22359
- raw_printf(stderr, "unable to read database header\n");
23118
+ eputz("unable to read database header\n");
2236023119
sqlite3_finalize(pStmt);
2236123120
return 1;
2236223121
}
2236323122
i = get2byteInt(aHdr+16);
2236423123
if( i==1 ) i = 65536;
22365
- utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
22366
- utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
22367
- utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
22368
- utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
23124
+ oputf("%-20s %d\n", "database page size:", i);
23125
+ oputf("%-20s %d\n", "write format:", aHdr[18]);
23126
+ oputf("%-20s %d\n", "read format:", aHdr[19]);
23127
+ oputf("%-20s %d\n", "reserved bytes:", aHdr[20]);
2236923128
for(i=0; i<ArraySize(aField); i++){
2237023129
int ofst = aField[i].ofst;
2237123130
unsigned int val = get4byteInt(aHdr + ofst);
22372
- utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
23131
+ oputf("%-20s %u", aField[i].zName, val);
2237323132
switch( ofst ){
2237423133
case 56: {
22375
- if( val==1 ) raw_printf(p->out, " (utf8)");
22376
- if( val==2 ) raw_printf(p->out, " (utf16le)");
22377
- if( val==3 ) raw_printf(p->out, " (utf16be)");
23134
+ if( val==1 ) oputz(" (utf8)");
23135
+ if( val==2 ) oputz(" (utf16le)");
23136
+ if( val==3 ) oputz(" (utf16be)");
2237823137
}
2237923138
}
22380
- raw_printf(p->out, "\n");
23139
+ oputz("\n");
2238123140
}
2238223141
if( zDb==0 ){
2238323142
zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
2238423143
}else if( cli_strcmp(zDb,"temp")==0 ){
2238523144
zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
@@ -22388,25 +23147,25 @@
2238823147
}
2238923148
for(i=0; i<ArraySize(aQuery); i++){
2239023149
char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
2239123150
int val = db_int(p->db, zSql);
2239223151
sqlite3_free(zSql);
22393
- utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
23152
+ oputf("%-20s %d\n", aQuery[i].zName, val);
2239423153
}
2239523154
sqlite3_free(zSchemaTab);
2239623155
sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
22397
- utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
23156
+ oputf("%-20s %u\n", "data version", iDataVersion);
2239823157
return 0;
2239923158
}
2240023159
#endif /* SQLITE_SHELL_HAVE_RECOVER */
2240123160
2240223161
/*
2240323162
** Print the current sqlite3_errmsg() value to stderr and return 1.
2240423163
*/
2240523164
static int shellDatabaseError(sqlite3 *db){
2240623165
const char *zErr = sqlite3_errmsg(db);
22407
- utf8_printf(stderr, "Error: %s\n", zErr);
23166
+ eputf("Error: %s\n", zErr);
2240823167
return 1;
2240923168
}
2241023169
2241123170
/*
2241223171
** Compare the pattern in zGlob[] against the text in z[]. Return TRUE
@@ -22637,11 +23396,10 @@
2263723396
ShellState *pState, /* Current shell tool state */
2263823397
char **azArg, /* Array of arguments passed to dot command */
2263923398
int nArg /* Number of entries in azArg[] */
2264023399
){
2264123400
sqlite3 *db = pState->db; /* Database handle to query "main" db of */
22642
- FILE *out = pState->out; /* Stream to write non-error output to */
2264323401
int bVerbose = 0; /* If -verbose is present */
2264423402
int bGroupByParent = 0; /* If -groupbyparent is present */
2264523403
int i; /* To iterate through azArg[] */
2264623404
const char *zIndent = ""; /* How much to indent CREATE INDEX by */
2264723405
int rc; /* Return code */
@@ -22719,13 +23477,11 @@
2271923477
else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
2272023478
bGroupByParent = 1;
2272123479
zIndent = " ";
2272223480
}
2272323481
else{
22724
- raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
22725
- azArg[0], azArg[1]
22726
- );
23482
+ eputf("Usage: %s %s ?-verbose? ?-groupbyparent?\n", azArg[0], azArg[1]);
2272723483
return SQLITE_ERROR;
2272823484
}
2272923485
}
2273023486
2273123487
/* Register the fkey_collate_clause() SQL function */
@@ -22765,44 +23521,44 @@
2276523521
}
2276623522
rc = sqlite3_finalize(pExplain);
2276723523
if( rc!=SQLITE_OK ) break;
2276823524
2276923525
if( res<0 ){
22770
- raw_printf(stderr, "Error: internal error");
23526
+ eputz("Error: internal error");
2277123527
break;
2277223528
}else{
2277323529
if( bGroupByParent
2277423530
&& (bVerbose || res==0)
2277523531
&& (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
2277623532
){
22777
- raw_printf(out, "-- Parent table %s\n", zParent);
23533
+ oputf("-- Parent table %s\n", zParent);
2277823534
sqlite3_free(zPrev);
2277923535
zPrev = sqlite3_mprintf("%s", zParent);
2278023536
}
2278123537
2278223538
if( res==0 ){
22783
- raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
23539
+ oputf("%s%s --> %s\n", zIndent, zCI, zTarget);
2278423540
}else if( bVerbose ){
22785
- raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
22786
- zIndent, zFrom, zTarget
23541
+ oputf("%s/* no extra indexes required for %s -> %s */\n",
23542
+ zIndent, zFrom, zTarget
2278723543
);
2278823544
}
2278923545
}
2279023546
}
2279123547
sqlite3_free(zPrev);
2279223548
2279323549
if( rc!=SQLITE_OK ){
22794
- raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
23550
+ eputf("%s\n", sqlite3_errmsg(db));
2279523551
}
2279623552
2279723553
rc2 = sqlite3_finalize(pSql);
2279823554
if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
2279923555
rc = rc2;
22800
- raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
23556
+ eputf("%s\n", sqlite3_errmsg(db));
2280123557
}
2280223558
}else{
22803
- raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
23559
+ eputf("%s\n", sqlite3_errmsg(db));
2280423560
}
2280523561
2280623562
return rc;
2280723563
}
2280823564
@@ -22818,13 +23574,13 @@
2281823574
n = (nArg>=2 ? strlen30(azArg[1]) : 0);
2281923575
if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
2282023576
return lintFkeyIndexes(pState, azArg, nArg);
2282123577
2282223578
usage:
22823
- raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
22824
- raw_printf(stderr, "Where sub-commands are:\n");
22825
- raw_printf(stderr, " fkey-indexes\n");
23579
+ eputf("Usage %s sub-command ?switches...?\n", azArg[0]);
23580
+ eputz("Where sub-commands are:\n");
23581
+ eputz(" fkey-indexes\n");
2282623582
return SQLITE_ERROR;
2282723583
}
2282823584
2282923585
#if !defined SQLITE_OMIT_VIRTUALTABLE
2283023586
static void shellPrepare(
@@ -22835,13 +23591,11 @@
2283523591
){
2283623592
*ppStmt = 0;
2283723593
if( *pRc==SQLITE_OK ){
2283823594
int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
2283923595
if( rc!=SQLITE_OK ){
22840
- raw_printf(stderr, "sql error: %s (%d)\n",
22841
- sqlite3_errmsg(db), sqlite3_errcode(db)
22842
- );
23596
+ eputf("sql error: %s (%d)\n", sqlite3_errmsg(db), sqlite3_errcode(db));
2284323597
*pRc = rc;
2284423598
}
2284523599
}
2284623600
}
2284723601
@@ -22888,11 +23642,11 @@
2288823642
if( pStmt ){
2288923643
sqlite3 *db = sqlite3_db_handle(pStmt);
2289023644
int rc = sqlite3_finalize(pStmt);
2289123645
if( *pRc==SQLITE_OK ){
2289223646
if( rc!=SQLITE_OK ){
22893
- raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
23647
+ eputf("SQL error: %s\n", sqlite3_errmsg(db));
2289423648
}
2289523649
*pRc = rc;
2289623650
}
2289723651
}
2289823652
}
@@ -22909,11 +23663,11 @@
2290923663
){
2291023664
int rc = sqlite3_reset(pStmt);
2291123665
if( *pRc==SQLITE_OK ){
2291223666
if( rc!=SQLITE_OK ){
2291323667
sqlite3 *db = sqlite3_db_handle(pStmt);
22914
- raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
23668
+ eputf("SQL error: %s\n", sqlite3_errmsg(db));
2291523669
}
2291623670
*pRc = rc;
2291723671
}
2291823672
}
2291923673
#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
@@ -22959,15 +23713,15 @@
2295923713
va_list ap;
2296023714
char *z;
2296123715
va_start(ap, zFmt);
2296223716
z = sqlite3_vmprintf(zFmt, ap);
2296323717
va_end(ap);
22964
- utf8_printf(stderr, "Error: %s\n", z);
23718
+ eputf("Error: %s\n", z);
2296523719
if( pAr->fromCmdLine ){
22966
- utf8_printf(stderr, "Use \"-A\" for more help\n");
23720
+ eputz("Use \"-A\" for more help\n");
2296723721
}else{
22968
- utf8_printf(stderr, "Use \".archive --help\" for more help\n");
23722
+ eputz("Use \".archive --help\" for more help\n");
2296923723
}
2297023724
sqlite3_free(z);
2297123725
return SQLITE_ERROR;
2297223726
}
2297323727
@@ -23063,11 +23817,11 @@
2306323817
};
2306423818
int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
2306523819
struct ArSwitch *pEnd = &aSwitch[nSwitch];
2306623820
2306723821
if( nArg<=1 ){
23068
- utf8_printf(stderr, "Wrong number of arguments. Usage:\n");
23822
+ eputz("Wrong number of arguments. Usage:\n");
2306923823
return arUsage(stderr);
2307023824
}else{
2307123825
char *z = azArg[1];
2307223826
if( z[0]!='-' ){
2307323827
/* Traditional style [tar] invocation */
@@ -23169,11 +23923,11 @@
2316923923
}
2317023924
}
2317123925
}
2317223926
}
2317323927
if( pAr->eCmd==0 ){
23174
- utf8_printf(stderr, "Required argument missing. Usage:\n");
23928
+ eputz("Required argument missing. Usage:\n");
2317523929
return arUsage(stderr);
2317623930
}
2317723931
return SQLITE_OK;
2317823932
}
2317923933
@@ -23212,11 +23966,11 @@
2321223966
if( SQLITE_ROW==sqlite3_step(pTest) ){
2321323967
bOk = 1;
2321423968
}
2321523969
shellReset(&rc, pTest);
2321623970
if( rc==SQLITE_OK && bOk==0 ){
23217
- utf8_printf(stderr, "not found in archive: %s\n", z);
23971
+ eputf("not found in archive: %s\n", z);
2321823972
rc = SQLITE_ERROR;
2321923973
}
2322023974
}
2322123975
shellFinalize(&rc, pTest);
2322223976
}
@@ -23279,30 +24033,26 @@
2327924033
arWhereClause(&rc, pAr, &zWhere);
2328024034
2328124035
shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
2328224036
pAr->zSrcTable, zWhere);
2328324037
if( pAr->bDryRun ){
23284
- utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
24038
+ oputf("%s\n", sqlite3_sql(pSql));
2328524039
}else{
2328624040
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
2328724041
if( pAr->bVerbose ){
23288
- utf8_printf(pAr->p->out, "%s % 10d %s %s\n",
23289
- sqlite3_column_text(pSql, 0),
23290
- sqlite3_column_int(pSql, 1),
23291
- sqlite3_column_text(pSql, 2),
23292
- sqlite3_column_text(pSql, 3)
23293
- );
24042
+ oputf("%s % 10d %s %s\n",
24043
+ sqlite3_column_text(pSql, 0), sqlite3_column_int(pSql, 1),
24044
+ sqlite3_column_text(pSql, 2),sqlite3_column_text(pSql, 3));
2329424045
}else{
23295
- utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
24046
+ oputf("%s\n", sqlite3_column_text(pSql, 0));
2329624047
}
2329724048
}
2329824049
}
2329924050
shellFinalize(&rc, pSql);
2330024051
sqlite3_free(zWhere);
2330124052
return rc;
2330224053
}
23303
-
2330424054
2330524055
/*
2330624056
** Implementation of .ar "Remove" command.
2330724057
*/
2330824058
static int arRemoveCommand(ArCommand *pAr){
@@ -23318,11 +24068,11 @@
2331824068
}
2331924069
if( rc==SQLITE_OK ){
2332024070
zSql = sqlite3_mprintf("DELETE FROM %s WHERE %s;",
2332124071
pAr->zSrcTable, zWhere);
2332224072
if( pAr->bDryRun ){
23323
- utf8_printf(pAr->p->out, "%s\n", zSql);
24073
+ oputf("%s\n", zSql);
2332424074
}else{
2332524075
char *zErr = 0;
2332624076
rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0);
2332724077
if( rc==SQLITE_OK ){
2332824078
rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
@@ -23331,11 +24081,11 @@
2333124081
}else{
2333224082
rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0);
2333324083
}
2333424084
}
2333524085
if( zErr ){
23336
- utf8_printf(stdout, "ERROR: %s\n", zErr);
24086
+ sputf(stdout, "ERROR: %s\n", zErr); /* stdout? */
2333724087
sqlite3_free(zErr);
2333824088
}
2333924089
}
2334024090
}
2334124091
sqlite3_free(zWhere);
@@ -23395,15 +24145,15 @@
2339524145
** populating them changes the timestamp). */
2339624146
for(i=0; i<2; i++){
2339724147
j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
2339824148
sqlite3_bind_int(pSql, j, i);
2339924149
if( pAr->bDryRun ){
23400
- utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
24150
+ oputf("%s\n", sqlite3_sql(pSql));
2340124151
}else{
2340224152
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
2340324153
if( i==0 && pAr->bVerbose ){
23404
- utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
24154
+ oputf("%s\n", sqlite3_column_text(pSql, 0));
2340524155
}
2340624156
}
2340724157
}
2340824158
shellReset(&rc, pSql);
2340924159
}
@@ -23419,17 +24169,17 @@
2341924169
** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out.
2342024170
*/
2342124171
static int arExecSql(ArCommand *pAr, const char *zSql){
2342224172
int rc;
2342324173
if( pAr->bDryRun ){
23424
- utf8_printf(pAr->p->out, "%s\n", zSql);
24174
+ oputf("%s\n", zSql);
2342524175
rc = SQLITE_OK;
2342624176
}else{
2342724177
char *zErr = 0;
2342824178
rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
2342924179
if( zErr ){
23430
- utf8_printf(stdout, "ERROR: %s\n", zErr);
24180
+ sputf(stdout, "ERROR: %s\n", zErr);
2343124181
sqlite3_free(zErr);
2343224182
}
2343324183
}
2343424184
return rc;
2343524185
}
@@ -23600,19 +24350,17 @@
2360024350
}else{
2360124351
flags = SQLITE_OPEN_READONLY;
2360224352
}
2360324353
cmd.db = 0;
2360424354
if( cmd.bDryRun ){
23605
- utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
23606
- eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
24355
+ oputf("-- open database '%s'%s\n", cmd.zFile,
24356
+ eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
2360724357
}
2360824358
rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
2360924359
eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
2361024360
if( rc!=SQLITE_OK ){
23611
- utf8_printf(stderr, "cannot open file: %s (%s)\n",
23612
- cmd.zFile, sqlite3_errmsg(cmd.db)
23613
- );
24361
+ eputf("cannot open file: %s (%s)\n", cmd.zFile, sqlite3_errmsg(cmd.db));
2361424362
goto end_ar_command;
2361524363
}
2361624364
sqlite3_fileio_init(cmd.db, 0, 0);
2361724365
sqlite3_sqlar_init(cmd.db, 0, 0);
2361824366
sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
@@ -23621,11 +24369,11 @@
2362124369
}
2362224370
if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
2362324371
if( cmd.eCmd!=AR_CMD_CREATE
2362424372
&& sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
2362524373
){
23626
- utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
24374
+ eputz("database does not contain an 'sqlar' table\n");
2362724375
rc = SQLITE_ERROR;
2362824376
goto end_ar_command;
2362924377
}
2363024378
cmd.zSrcTable = sqlite3_mprintf("sqlar");
2363124379
}
@@ -23679,11 +24427,11 @@
2367924427
** This function is used as a callback by the recover extension. Simply
2368024428
** print the supplied SQL statement to stdout.
2368124429
*/
2368224430
static int recoverSqlCb(void *pCtx, const char *zSql){
2368324431
ShellState *pState = (ShellState*)pCtx;
23684
- utf8_printf(pState->out, "%s;\n", zSql);
24432
+ sputf(pState->out, "%s;\n", zSql);
2368524433
return SQLITE_OK;
2368624434
}
2368724435
2368824436
/*
2368924437
** This function is called to recover data from the database. A script
@@ -23722,11 +24470,11 @@
2372224470
}else
2372324471
if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
2372424472
bRowids = 0;
2372524473
}
2372624474
else{
23727
- utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
24475
+ eputf("unexpected option: %s\n", azArg[i]);
2372824476
showHelp(pState->out, azArg[0]);
2372924477
return 1;
2373024478
}
2373124479
}
2373224480
@@ -23741,11 +24489,11 @@
2374124489
2374224490
sqlite3_recover_run(p);
2374324491
if( sqlite3_recover_errcode(p)!=SQLITE_OK ){
2374424492
const char *zErr = sqlite3_recover_errmsg(p);
2374524493
int errCode = sqlite3_recover_errcode(p);
23746
- raw_printf(stderr, "sql error: %s (%d)\n", zErr, errCode);
24494
+ eputf("sql error: %s (%d)\n", zErr, errCode);
2374724495
}
2374824496
rc = sqlite3_recover_finish(p);
2374924497
return rc;
2375024498
}
2375124499
#endif /* SQLITE_SHELL_HAVE_RECOVER */
@@ -23766,11 +24514,11 @@
2376624514
*/
2376724515
#ifdef SHELL_DEBUG
2376824516
#define rc_err_oom_die(rc) \
2376924517
if( rc==SQLITE_NOMEM ) shell_check_oom(0); \
2377024518
else if(!(rc==SQLITE_OK||rc==SQLITE_DONE)) \
23771
- fprintf(stderr,"E:%d\n",rc), assert(0)
24519
+ eputf("E:%d\n",rc), assert(0)
2377224520
#else
2377324521
static void rc_err_oom_die(int rc){
2377424522
if( rc==SQLITE_NOMEM ) shell_check_oom(0);
2377524523
assert(rc==SQLITE_OK||rc==SQLITE_DONE);
2377624524
}
@@ -23906,10 +24654,11 @@
2390624654
#ifdef SHELL_COLFIX_DB
2390724655
if(*zCOL_DB!=':')
2390824656
sqlite3_exec(*pDb,"drop table if exists ColNames;"
2390924657
"drop view if exists RepeatedNames;",0,0,0);
2391024658
#endif
24659
+#undef SHELL_COLFIX_DB
2391124660
rc = sqlite3_exec(*pDb, zTabMake, 0, 0, 0);
2391224661
rc_err_oom_die(rc);
2391324662
}
2391424663
assert(*pDb!=0);
2391524664
rc = sqlite3_prepare_v2(*pDb, zTabFill, -1, &pStmt, 0);
@@ -24019,11 +24768,11 @@
2401924768
clearTempFile(p);
2402024769
2402124770
#ifndef SQLITE_OMIT_AUTHORIZATION
2402224771
if( c=='a' && cli_strncmp(azArg[0], "auth", n)==0 ){
2402324772
if( nArg!=2 ){
24024
- raw_printf(stderr, "Usage: .auth ON|OFF\n");
24773
+ eputz("Usage: .auth ON|OFF\n");
2402524774
rc = 1;
2402624775
goto meta_command_exit;
2402724776
}
2402824777
open_db(p, 0);
2402924778
if( booleanValue(azArg[1]) ){
@@ -24066,32 +24815,32 @@
2406624815
}else
2406724816
if( cli_strcmp(z, "-async")==0 ){
2406824817
bAsync = 1;
2406924818
}else
2407024819
{
24071
- utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
24820
+ eputf("unknown option: %s\n", azArg[j]);
2407224821
return 1;
2407324822
}
2407424823
}else if( zDestFile==0 ){
2407524824
zDestFile = azArg[j];
2407624825
}else if( zDb==0 ){
2407724826
zDb = zDestFile;
2407824827
zDestFile = azArg[j];
2407924828
}else{
24080
- raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
24829
+ eputz("Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
2408124830
return 1;
2408224831
}
2408324832
}
2408424833
if( zDestFile==0 ){
24085
- raw_printf(stderr, "missing FILENAME argument on .backup\n");
24834
+ eputz("missing FILENAME argument on .backup\n");
2408624835
return 1;
2408724836
}
2408824837
if( zDb==0 ) zDb = "main";
2408924838
rc = sqlite3_open_v2(zDestFile, &pDest,
2409024839
SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
2409124840
if( rc!=SQLITE_OK ){
24092
- utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
24841
+ eputf("Error: cannot open \"%s\"\n", zDestFile);
2409324842
close_db(pDest);
2409424843
return 1;
2409524844
}
2409624845
if( bAsync ){
2409724846
sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
@@ -24098,20 +24847,20 @@
2409824847
0, 0, 0);
2409924848
}
2410024849
open_db(p, 0);
2410124850
pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
2410224851
if( pBackup==0 ){
24103
- utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
24852
+ eputf("Error: %s\n", sqlite3_errmsg(pDest));
2410424853
close_db(pDest);
2410524854
return 1;
2410624855
}
2410724856
while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
2410824857
sqlite3_backup_finish(pBackup);
2410924858
if( rc==SQLITE_DONE ){
2411024859
rc = 0;
2411124860
}else{
24112
- utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
24861
+ eputf("Error: %s\n", sqlite3_errmsg(pDest));
2411324862
rc = 1;
2411424863
}
2411524864
close_db(pDest);
2411624865
}else
2411724866
#endif /* !defined(SQLITE_SHELL_FIDDLE) */
@@ -24118,11 +24867,11 @@
2411824867
2411924868
if( c=='b' && n>=3 && cli_strncmp(azArg[0], "bail", n)==0 ){
2412024869
if( nArg==2 ){
2412124870
bail_on_error = booleanValue(azArg[1]);
2412224871
}else{
24123
- raw_printf(stderr, "Usage: .bail on|off\n");
24872
+ eputz("Usage: .bail on|off\n");
2412424873
rc = 1;
2412524874
}
2412624875
}else
2412724876
2412824877
/* Undocumented. Legacy only. See "crnl" below */
@@ -24132,13 +24881,12 @@
2413224881
setBinaryMode(p->out, 1);
2413324882
}else{
2413424883
setTextMode(p->out, 1);
2413524884
}
2413624885
}else{
24137
- raw_printf(stderr, "The \".binary\" command is deprecated."
24138
- " Use \".crnl\" instead.\n");
24139
- raw_printf(stderr, "Usage: .binary on|off\n");
24886
+ eputz("The \".binary\" command is deprecated. Use \".crnl\" instead.\n"
24887
+ "Usage: .binary on|off\n");
2414024888
rc = 1;
2414124889
}
2414224890
}else
2414324891
2414424892
/* The undocumented ".breakpoint" command causes a call to the no-op
@@ -24158,25 +24906,25 @@
2415824906
sqlite3_free(z);
2415924907
#else
2416024908
rc = chdir(azArg[1]);
2416124909
#endif
2416224910
if( rc ){
24163
- utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
24911
+ eputf("Cannot change to directory \"%s\"\n", azArg[1]);
2416424912
rc = 1;
2416524913
}
2416624914
}else{
24167
- raw_printf(stderr, "Usage: .cd DIRECTORY\n");
24915
+ eputz("Usage: .cd DIRECTORY\n");
2416824916
rc = 1;
2416924917
}
2417024918
}else
2417124919
#endif /* !defined(SQLITE_SHELL_FIDDLE) */
2417224920
2417324921
if( c=='c' && n>=3 && cli_strncmp(azArg[0], "changes", n)==0 ){
2417424922
if( nArg==2 ){
2417524923
setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
2417624924
}else{
24177
- raw_printf(stderr, "Usage: .changes on|off\n");
24925
+ eputz("Usage: .changes on|off\n");
2417824926
rc = 1;
2417924927
}
2418024928
}else
2418124929
2418224930
#ifndef SQLITE_SHELL_FIDDLE
@@ -24186,21 +24934,20 @@
2418624934
*/
2418724935
if( c=='c' && n>=3 && cli_strncmp(azArg[0], "check", n)==0 ){
2418824936
char *zRes = 0;
2418924937
output_reset(p);
2419024938
if( nArg!=2 ){
24191
- raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
24939
+ eputz("Usage: .check GLOB-PATTERN\n");
2419224940
rc = 2;
2419324941
}else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
2419424942
rc = 2;
2419524943
}else if( testcase_glob(azArg[1],zRes)==0 ){
24196
- utf8_printf(stderr,
24197
- "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
24198
- p->zTestcase, azArg[1], zRes);
24944
+ eputf("testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
24945
+ p->zTestcase, azArg[1], zRes);
2419924946
rc = 1;
2420024947
}else{
24201
- utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
24948
+ oputf("testcase-%s ok\n", p->zTestcase);
2420224949
p->nCheck++;
2420324950
}
2420424951
sqlite3_free(zRes);
2420524952
}else
2420624953
#endif /* !defined(SQLITE_SHELL_FIDDLE) */
@@ -24209,11 +24956,11 @@
2420924956
if( c=='c' && cli_strncmp(azArg[0], "clone", n)==0 ){
2421024957
failIfSafeMode(p, "cannot run .clone in safe mode");
2421124958
if( nArg==2 ){
2421224959
tryToClone(p, azArg[1]);
2421324960
}else{
24214
- raw_printf(stderr, "Usage: .clone FILENAME\n");
24961
+ eputz("Usage: .clone FILENAME\n");
2421524962
rc = 1;
2421624963
}
2421724964
}else
2421824965
#endif /* !defined(SQLITE_SHELL_FIDDLE) */
2421924966
@@ -24229,13 +24976,13 @@
2422924976
zFile = "(memory)";
2423024977
}else if( zFile[0]==0 ){
2423124978
zFile = "(temporary-file)";
2423224979
}
2423324980
if( p->pAuxDb == &p->aAuxDb[i] ){
24234
- utf8_printf(stdout, "ACTIVE %d: %s\n", i, zFile);
24981
+ sputf(stdout, "ACTIVE %d: %s\n", i, zFile);
2423524982
}else if( p->aAuxDb[i].db!=0 ){
24236
- utf8_printf(stdout, " %d: %s\n", i, zFile);
24983
+ sputf(stdout, " %d: %s\n", i, zFile);
2423724984
}
2423824985
}
2423924986
}else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){
2424024987
int i = azArg[1][0] - '0';
2424124988
if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && i<ArraySize(p->aAuxDb) ){
@@ -24248,19 +24995,19 @@
2424824995
&& IsDigit(azArg[2][0]) && azArg[2][1]==0 ){
2424924996
int i = azArg[2][0] - '0';
2425024997
if( i<0 || i>=ArraySize(p->aAuxDb) ){
2425124998
/* No-op */
2425224999
}else if( p->pAuxDb == &p->aAuxDb[i] ){
24253
- raw_printf(stderr, "cannot close the active database connection\n");
25000
+ eputz("cannot close the active database connection\n");
2425425001
rc = 1;
2425525002
}else if( p->aAuxDb[i].db ){
2425625003
session_close_all(p, i);
2425725004
close_db(p->aAuxDb[i].db);
2425825005
p->aAuxDb[i].db = 0;
2425925006
}
2426025007
}else{
24261
- raw_printf(stderr, "Usage: .connection [close] [CONNECTION-NUMBER]\n");
25008
+ eputz("Usage: .connection [close] [CONNECTION-NUMBER]\n");
2426225009
rc = 1;
2426325010
}
2426425011
}else
2426525012
2426625013
if( c=='c' && n==4 && cli_strncmp(azArg[0], "crnl", n)==0 ){
@@ -24270,13 +25017,13 @@
2427025017
}else{
2427125018
setBinaryMode(p->out, 1);
2427225019
}
2427325020
}else{
2427425021
#if !defined(_WIN32) && !defined(WIN32)
24275
- raw_printf(stderr, "The \".crnl\" is a no-op on non-Windows machines.\n");
25022
+ eputz("The \".crnl\" is a no-op on non-Windows machines.\n");
2427625023
#endif
24277
- raw_printf(stderr, "Usage: .crnl on|off\n");
25024
+ eputz("Usage: .crnl on|off\n");
2427825025
rc = 1;
2427925026
}
2428025027
}else
2428125028
2428225029
if( c=='d' && n>1 && cli_strncmp(azArg[0], "databases", n)==0 ){
@@ -24285,11 +25032,11 @@
2428525032
sqlite3_stmt *pStmt;
2428625033
int i;
2428725034
open_db(p, 0);
2428825035
rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
2428925036
if( rc ){
24290
- utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
25037
+ eputf("Error: %s\n", sqlite3_errmsg(p->db));
2429125038
rc = 1;
2429225039
}else{
2429325040
while( sqlite3_step(pStmt)==SQLITE_ROW ){
2429425041
const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
2429525042
const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
@@ -24304,15 +25051,13 @@
2430425051
sqlite3_finalize(pStmt);
2430525052
for(i=0; i<nName; i++){
2430625053
int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
2430725054
int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
2430825055
const char *z = azName[i*2+1];
24309
- utf8_printf(p->out, "%s: %s %s%s\n",
24310
- azName[i*2],
24311
- z && z[0] ? z : "\"\"",
24312
- bRdonly ? "r/o" : "r/w",
24313
- eTxn==SQLITE_TXN_NONE ? "" :
25056
+ oputf("%s: %s %s%s\n",
25057
+ azName[i*2], z && z[0] ? z : "\"\"", bRdonly ? "r/o" : "r/w",
25058
+ eTxn==SQLITE_TXN_NONE ? "" :
2431425059
eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
2431525060
free(azName[i*2]);
2431625061
free(azName[i*2+1]);
2431725062
}
2431825063
sqlite3_free(azName);
@@ -24348,16 +25093,16 @@
2434825093
if( nArg>1 && cli_strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
2434925094
if( nArg>=3 ){
2435025095
sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
2435125096
}
2435225097
sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
24353
- utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
25098
+ oputf("%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
2435425099
if( nArg>1 ) break;
2435525100
}
2435625101
if( nArg>1 && ii==ArraySize(aDbConfig) ){
24357
- utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
24358
- utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
25102
+ eputf("Error: unknown dbconfig \"%s\"\n", azArg[1]);
25103
+ eputz("Enter \".dbconfig\" with no arguments for a list\n");
2435925104
}
2436025105
}else
2436125106
2436225107
#if SQLITE_SHELL_HAVE_RECOVER
2436325108
if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbinfo", n)==0 ){
@@ -24383,12 +25128,12 @@
2438325128
if( azArg[i][0]=='-' ){
2438425129
const char *z = azArg[i]+1;
2438525130
if( z[0]=='-' ) z++;
2438625131
if( cli_strcmp(z,"preserve-rowids")==0 ){
2438725132
#ifdef SQLITE_OMIT_VIRTUALTABLE
24388
- raw_printf(stderr, "The --preserve-rowids option is not compatible"
24389
- " with SQLITE_OMIT_VIRTUALTABLE\n");
25133
+ eputz("The --preserve-rowids option is not compatible"
25134
+ " with SQLITE_OMIT_VIRTUALTABLE\n");
2439025135
rc = 1;
2439125136
sqlite3_free(zLike);
2439225137
goto meta_command_exit;
2439325138
#else
2439425139
ShellSetFlag(p, SHFLG_PreserveRowid);
@@ -24402,11 +25147,11 @@
2440225147
}else
2440325148
if( cli_strcmp(z,"nosys")==0 ){
2440425149
ShellSetFlag(p, SHFLG_DumpNoSys);
2440525150
}else
2440625151
{
24407
- raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
25152
+ eputf("Unknown option \"%s\" on \".dump\"\n", azArg[i]);
2440825153
rc = 1;
2440925154
sqlite3_free(zLike);
2441025155
goto meta_command_exit;
2441125156
}
2441225157
}else{
@@ -24436,12 +25181,12 @@
2443625181
2443725182
if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
2443825183
/* When playing back a "dump", the content might appear in an order
2443925184
** which causes immediate foreign key constraints to be violated.
2444025185
** So disable foreign-key constraint enforcement to prevent problems. */
24441
- raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
24442
- raw_printf(p->out, "BEGIN TRANSACTION;\n");
25186
+ oputz("PRAGMA foreign_keys=OFF;\n");
25187
+ oputz("BEGIN TRANSACTION;\n");
2444325188
}
2444425189
p->writableSchema = 0;
2444525190
p->showHeader = 0;
2444625191
/* Set writable_schema=ON since doing so forces SQLite to initialize
2444725192
** as much of the schema as it can even if the sqlite_schema table is
@@ -24468,27 +25213,27 @@
2446825213
run_table_dump_query(p, zSql);
2446925214
sqlite3_free(zSql);
2447025215
}
2447125216
sqlite3_free(zLike);
2447225217
if( p->writableSchema ){
24473
- raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
25218
+ oputz("PRAGMA writable_schema=OFF;\n");
2447425219
p->writableSchema = 0;
2447525220
}
2447625221
sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
2447725222
sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
2447825223
if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
24479
- raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
25224
+ oputz(p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
2448025225
}
2448125226
p->showHeader = savedShowHeader;
2448225227
p->shellFlgs = savedShellFlags;
2448325228
}else
2448425229
2448525230
if( c=='e' && cli_strncmp(azArg[0], "echo", n)==0 ){
2448625231
if( nArg==2 ){
2448725232
setOrClearFlag(p, SHFLG_Echo, azArg[1]);
2448825233
}else{
24489
- raw_printf(stderr, "Usage: .echo on|off\n");
25234
+ eputz("Usage: .echo on|off\n");
2449025235
rc = 1;
2449125236
}
2449225237
}else
2449325238
2449425239
if( c=='e' && cli_strncmp(azArg[0], "eqp", n)==0 ){
@@ -24515,11 +25260,11 @@
2451525260
#endif
2451625261
}else{
2451725262
p->autoEQP = (u8)booleanValue(azArg[1]);
2451825263
}
2451925264
}else{
24520
- raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
25265
+ eputz("Usage: .eqp off|on|trace|trigger|full\n");
2452125266
rc = 1;
2452225267
}
2452325268
}else
2452425269
2452525270
#ifndef SQLITE_SHELL_FIDDLE
@@ -24554,13 +25299,12 @@
2455425299
}else
2455525300
2455625301
#ifndef SQLITE_OMIT_VIRTUALTABLE
2455725302
if( c=='e' && cli_strncmp(azArg[0], "expert", n)==0 ){
2455825303
if( p->bSafeMode ){
24559
- raw_printf(stderr,
24560
- "Cannot run experimental commands such as \"%s\" in safe mode\n",
24561
- azArg[0]);
25304
+ eputf("Cannot run experimental commands such as \"%s\" in safe mode\n",
25305
+ azArg[0]);
2456225306
rc = 1;
2456325307
}else{
2456425308
open_db(p, 0);
2456525309
expertDotCommand(p, azArg, nArg);
2456625310
}
@@ -24612,14 +25356,13 @@
2461225356
if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
2461325357
}
2461425358
2461525359
/* --help lists all file-controls */
2461625360
if( cli_strcmp(zCmd,"help")==0 ){
24617
- utf8_printf(p->out, "Available file-controls:\n");
25361
+ oputz("Available file-controls:\n");
2461825362
for(i=0; i<ArraySize(aCtrl); i++){
24619
- utf8_printf(p->out, " .filectrl %s %s\n",
24620
- aCtrl[i].zCtrlName, aCtrl[i].zUsage);
25363
+ oputf(" .filectrl %s %s\n", aCtrl[i].zCtrlName, aCtrl[i].zUsage);
2462125364
}
2462225365
rc = 1;
2462325366
goto meta_command_exit;
2462425367
}
2462525368
@@ -24630,20 +25373,20 @@
2463025373
if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
2463125374
if( filectrl<0 ){
2463225375
filectrl = aCtrl[i].ctrlCode;
2463325376
iCtrl = i;
2463425377
}else{
24635
- utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
24636
- "Use \".filectrl --help\" for help\n", zCmd);
25378
+ eputf("Error: ambiguous file-control: \"%s\"\n"
25379
+ "Use \".filectrl --help\" for help\n", zCmd);
2463725380
rc = 1;
2463825381
goto meta_command_exit;
2463925382
}
2464025383
}
2464125384
}
2464225385
if( filectrl<0 ){
24643
- utf8_printf(stderr,"Error: unknown file-control: %s\n"
24644
- "Use \".filectrl --help\" for help\n", zCmd);
25386
+ eputf("Error: unknown file-control: %s\n"
25387
+ "Use \".filectrl --help\" for help\n", zCmd);
2464525388
}else{
2464625389
switch(filectrl){
2464725390
case SQLITE_FCNTL_SIZE_LIMIT: {
2464825391
if( nArg!=2 && nArg!=3 ) break;
2464925392
iRes = nArg==3 ? integerValue(azArg[2]) : -1;
@@ -24682,11 +25425,11 @@
2468225425
case SQLITE_FCNTL_TEMPFILENAME: {
2468325426
char *z = 0;
2468425427
if( nArg!=2 ) break;
2468525428
sqlite3_file_control(p->db, zSchema, filectrl, &z);
2468625429
if( z ){
24687
- utf8_printf(p->out, "%s\n", z);
25430
+ oputf("%s\n", z);
2468825431
sqlite3_free(z);
2468925432
}
2469025433
isOk = 2;
2469125434
break;
2469225435
}
@@ -24696,23 +25439,23 @@
2469625439
x = atoi(azArg[2]);
2469725440
sqlite3_file_control(p->db, zSchema, filectrl, &x);
2469825441
}
2469925442
x = -1;
2470025443
sqlite3_file_control(p->db, zSchema, filectrl, &x);
24701
- utf8_printf(p->out,"%d\n", x);
25444
+ oputf("%d\n", x);
2470225445
isOk = 2;
2470325446
break;
2470425447
}
2470525448
}
2470625449
}
2470725450
if( isOk==0 && iCtrl>=0 ){
24708
- utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
25451
+ oputf("Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
2470925452
rc = 1;
2471025453
}else if( isOk==1 ){
2471125454
char zBuf[100];
2471225455
sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
24713
- raw_printf(p->out, "%s\n", zBuf);
25456
+ oputf("%s\n", zBuf);
2471425457
}
2471525458
}else
2471625459
2471725460
if( c=='f' && cli_strncmp(azArg[0], "fullschema", n)==0 ){
2471825461
ShellState data;
@@ -24723,11 +25466,11 @@
2472325466
if( nArg==2 && optionMatch(azArg[1], "indent") ){
2472425467
data.cMode = data.mode = MODE_Pretty;
2472525468
nArg = 1;
2472625469
}
2472725470
if( nArg!=1 ){
24728
- raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
25471
+ eputz("Usage: .fullschema ?--indent?\n");
2472925472
rc = 1;
2473025473
goto meta_command_exit;
2473125474
}
2473225475
open_db(p, 0);
2473325476
rc = sqlite3_exec(p->db,
@@ -24749,37 +25492,37 @@
2474925492
doStats = sqlite3_step(pStmt)==SQLITE_ROW;
2475025493
sqlite3_finalize(pStmt);
2475125494
}
2475225495
}
2475325496
if( doStats==0 ){
24754
- raw_printf(p->out, "/* No STAT tables available */\n");
25497
+ oputz("/* No STAT tables available */\n");
2475525498
}else{
24756
- raw_printf(p->out, "ANALYZE sqlite_schema;\n");
25499
+ oputz("ANALYZE sqlite_schema;\n");
2475725500
data.cMode = data.mode = MODE_Insert;
2475825501
data.zDestTable = "sqlite_stat1";
2475925502
shell_exec(&data, "SELECT * FROM sqlite_stat1", 0);
2476025503
data.zDestTable = "sqlite_stat4";
2476125504
shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
24762
- raw_printf(p->out, "ANALYZE sqlite_schema;\n");
25505
+ oputz("ANALYZE sqlite_schema;\n");
2476325506
}
2476425507
}else
2476525508
2476625509
if( c=='h' && cli_strncmp(azArg[0], "headers", n)==0 ){
2476725510
if( nArg==2 ){
2476825511
p->showHeader = booleanValue(azArg[1]);
2476925512
p->shellFlgs |= SHFLG_HeaderSet;
2477025513
}else{
24771
- raw_printf(stderr, "Usage: .headers on|off\n");
25514
+ eputz("Usage: .headers on|off\n");
2477225515
rc = 1;
2477325516
}
2477425517
}else
2477525518
2477625519
if( c=='h' && cli_strncmp(azArg[0], "help", n)==0 ){
2477725520
if( nArg>=2 ){
2477825521
n = showHelp(p->out, azArg[1]);
2477925522
if( n==0 ){
24780
- utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
25523
+ oputf("Nothing matches '%s'\n", azArg[1]);
2478125524
}
2478225525
}else{
2478325526
showHelp(p->out, 0);
2478425527
}
2478525528
}else
@@ -24819,11 +25562,11 @@
2481925562
if( zFile==0 ){
2482025563
zFile = z;
2482125564
}else if( zTable==0 ){
2482225565
zTable = z;
2482325566
}else{
24824
- utf8_printf(p->out, "ERROR: extra argument: \"%s\". Usage:\n", z);
25567
+ oputf("ERROR: extra argument: \"%s\". Usage:\n", z);
2482525568
showHelp(p->out, "import");
2482625569
goto meta_command_exit;
2482725570
}
2482825571
}else if( cli_strcmp(z,"-v")==0 ){
2482925572
eVerbose++;
@@ -24840,18 +25583,18 @@
2484025583
sCtx.cColSep = ',';
2484125584
sCtx.cRowSep = '\n';
2484225585
xRead = csv_read_one_field;
2484325586
useOutputMode = 0;
2484425587
}else{
24845
- utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n", z);
25588
+ oputf("ERROR: unknown option: \"%s\". Usage:\n", z);
2484625589
showHelp(p->out, "import");
2484725590
goto meta_command_exit;
2484825591
}
2484925592
}
2485025593
if( zTable==0 ){
24851
- utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
24852
- zFile==0 ? "FILE" : "TABLE");
25594
+ oputf("ERROR: missing %s argument. Usage:\n",
25595
+ zFile==0 ? "FILE" : "TABLE");
2485325596
showHelp(p->out, "import");
2485425597
goto meta_command_exit;
2485525598
}
2485625599
seenInterrupt = 0;
2485725600
open_db(p, 0);
@@ -24858,24 +25601,21 @@
2485825601
if( useOutputMode ){
2485925602
/* If neither the --csv or --ascii options are specified, then set
2486025603
** the column and row separator characters from the output mode. */
2486125604
nSep = strlen30(p->colSeparator);
2486225605
if( nSep==0 ){
24863
- raw_printf(stderr,
24864
- "Error: non-null column separator required for import\n");
25606
+ eputz("Error: non-null column separator required for import\n");
2486525607
goto meta_command_exit;
2486625608
}
2486725609
if( nSep>1 ){
24868
- raw_printf(stderr,
24869
- "Error: multi-character column separators not allowed"
25610
+ eputz("Error: multi-character column separators not allowed"
2487025611
" for import\n");
2487125612
goto meta_command_exit;
2487225613
}
2487325614
nSep = strlen30(p->rowSeparator);
2487425615
if( nSep==0 ){
24875
- raw_printf(stderr,
24876
- "Error: non-null row separator required for import\n");
25616
+ eputz("Error: non-null row separator required for import\n");
2487725617
goto meta_command_exit;
2487825618
}
2487925619
if( nSep==2 && p->mode==MODE_Csv
2488025620
&& cli_strcmp(p->rowSeparator,SEP_CrLf)==0
2488125621
){
@@ -24885,22 +25625,22 @@
2488525625
** and output row separators. */
2488625626
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
2488725627
nSep = strlen30(p->rowSeparator);
2488825628
}
2488925629
if( nSep>1 ){
24890
- raw_printf(stderr, "Error: multi-character row separators not allowed"
24891
- " for import\n");
25630
+ eputz("Error: multi-character row separators not allowed"
25631
+ " for import\n");
2489225632
goto meta_command_exit;
2489325633
}
2489425634
sCtx.cColSep = (u8)p->colSeparator[0];
2489525635
sCtx.cRowSep = (u8)p->rowSeparator[0];
2489625636
}
2489725637
sCtx.zFile = zFile;
2489825638
sCtx.nLine = 1;
2489925639
if( sCtx.zFile[0]=='|' ){
2490025640
#ifdef SQLITE_OMIT_POPEN
24901
- raw_printf(stderr, "Error: pipes are not supported in this OS\n");
25641
+ eputz("Error: pipes are not supported in this OS\n");
2490225642
goto meta_command_exit;
2490325643
#else
2490425644
sCtx.in = popen(sCtx.zFile+1, "r");
2490525645
sCtx.zFile = "<pipe>";
2490625646
sCtx.xCloser = pclose;
@@ -24908,23 +25648,23 @@
2490825648
}else{
2490925649
sCtx.in = fopen(sCtx.zFile, "rb");
2491025650
sCtx.xCloser = fclose;
2491125651
}
2491225652
if( sCtx.in==0 ){
24913
- utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
25653
+ eputf("Error: cannot open \"%s\"\n", zFile);
2491425654
goto meta_command_exit;
2491525655
}
2491625656
if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
2491725657
char zSep[2];
2491825658
zSep[1] = 0;
2491925659
zSep[0] = sCtx.cColSep;
24920
- utf8_printf(p->out, "Column separator ");
24921
- output_c_string(p->out, zSep);
24922
- utf8_printf(p->out, ", row separator ");
25660
+ oputz("Column separator ");
25661
+ output_c_string(zSep);
25662
+ oputz(", row separator ");
2492325663
zSep[0] = sCtx.cRowSep;
24924
- output_c_string(p->out, zSep);
24925
- utf8_printf(p->out, "\n");
25664
+ output_c_string(zSep);
25665
+ oputz("\n");
2492625666
}
2492725667
sCtx.z = sqlite3_malloc64(120);
2492825668
if( sCtx.z==0 ){
2492925669
import_cleanup(&sCtx);
2493025670
shell_out_of_memory();
@@ -24955,18 +25695,18 @@
2495525695
zAutoColumn(sCtx.z, &dbCols, 0);
2495625696
if( sCtx.cTerm!=sCtx.cColSep ) break;
2495725697
}
2495825698
zColDefs = zAutoColumn(0, &dbCols, &zRenames);
2495925699
if( zRenames!=0 ){
24960
- utf8_printf((stdin_is_interactive && p->in==stdin)? p->out : stderr,
24961
- "Columns renamed during .import %s due to duplicates:\n"
24962
- "%s\n", sCtx.zFile, zRenames);
25700
+ sputf((stdin_is_interactive && p->in==stdin)? p->out : stderr,
25701
+ "Columns renamed during .import %s due to duplicates:\n"
25702
+ "%s\n", sCtx.zFile, zRenames);
2496325703
sqlite3_free(zRenames);
2496425704
}
2496525705
assert(dbCols==0);
2496625706
if( zColDefs==0 ){
24967
- utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
25707
+ eputf("%s: empty file\n", sCtx.zFile);
2496825708
import_fail:
2496925709
sqlite3_free(zCreate);
2497025710
sqlite3_free(zSql);
2497125711
sqlite3_free(zFullTabName);
2497225712
import_cleanup(&sCtx);
@@ -24973,24 +25713,24 @@
2497325713
rc = 1;
2497425714
goto meta_command_exit;
2497525715
}
2497625716
zCreate = sqlite3_mprintf("%z%z\n", zCreate, zColDefs);
2497725717
if( eVerbose>=1 ){
24978
- utf8_printf(p->out, "%s\n", zCreate);
25718
+ oputf("%s\n", zCreate);
2497925719
}
2498025720
rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
2498125721
if( rc ){
24982
- utf8_printf(stderr, "%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db));
25722
+ eputf("%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db));
2498325723
goto import_fail;
2498425724
}
2498525725
sqlite3_free(zCreate);
2498625726
zCreate = 0;
2498725727
rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
2498825728
}
2498925729
if( rc ){
2499025730
if (pStmt) sqlite3_finalize(pStmt);
24991
- utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
25731
+ eputf("Error: %s\n", sqlite3_errmsg(p->db));
2499225732
goto import_fail;
2499325733
}
2499425734
sqlite3_free(zSql);
2499525735
nCol = sqlite3_column_count(pStmt);
2499625736
sqlite3_finalize(pStmt);
@@ -25008,15 +25748,15 @@
2500825748
zSql[j++] = '?';
2500925749
}
2501025750
zSql[j++] = ')';
2501125751
zSql[j] = 0;
2501225752
if( eVerbose>=2 ){
25013
- utf8_printf(p->out, "Insert using: %s\n", zSql);
25753
+ oputf("Insert using: %s\n", zSql);
2501425754
}
2501525755
rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
2501625756
if( rc ){
25017
- utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
25757
+ eputf("Error: %s\n", sqlite3_errmsg(p->db));
2501825758
if (pStmt) sqlite3_finalize(pStmt);
2501925759
goto import_fail;
2502025760
}
2502125761
sqlite3_free(zSql);
2502225762
sqlite3_free(zFullTabName);
@@ -25045,32 +25785,31 @@
2504525785
if( z==0 && (xRead==csv_read_one_field) && i==nCol-1 && i>0 ){
2504625786
z = "";
2504725787
}
2504825788
sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
2504925789
if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
25050
- utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
25051
- "filling the rest with NULL\n",
25052
- sCtx.zFile, startLine, nCol, i+1);
25790
+ eputf("%s:%d: expected %d columns but found %d"
25791
+ " - filling the rest with NULL\n",
25792
+ sCtx.zFile, startLine, nCol, i+1);
2505325793
i += 2;
2505425794
while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
2505525795
}
2505625796
}
2505725797
if( sCtx.cTerm==sCtx.cColSep ){
2505825798
do{
2505925799
xRead(&sCtx);
2506025800
i++;
2506125801
}while( sCtx.cTerm==sCtx.cColSep );
25062
- utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
25063
- "extras ignored\n",
25064
- sCtx.zFile, startLine, nCol, i);
25802
+ eputf("%s:%d: expected %d columns but found %d - extras ignored\n",
25803
+ sCtx.zFile, startLine, nCol, i);
2506525804
}
2506625805
if( i>=nCol ){
2506725806
sqlite3_step(pStmt);
2506825807
rc = sqlite3_reset(pStmt);
2506925808
if( rc!=SQLITE_OK ){
25070
- utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
25071
- startLine, sqlite3_errmsg(p->db));
25809
+ eputf("%s:%d: INSERT failed: %s\n",
25810
+ sCtx.zFile, startLine, sqlite3_errmsg(p->db));
2507225811
sCtx.nErr++;
2507325812
}else{
2507425813
sCtx.nRow++;
2507525814
}
2507625815
}
@@ -25078,13 +25817,12 @@
2507825817
2507925818
import_cleanup(&sCtx);
2508025819
sqlite3_finalize(pStmt);
2508125820
if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
2508225821
if( eVerbose>0 ){
25083
- utf8_printf(p->out,
25084
- "Added %d rows with %d errors using %d lines of input\n",
25085
- sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
25822
+ oputf("Added %d rows with %d errors using %d lines of input\n",
25823
+ sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
2508625824
}
2508725825
}else
2508825826
#endif /* !defined(SQLITE_SHELL_FIDDLE) */
2508925827
2509025828
#ifndef SQLITE_UNTESTABLE
@@ -25095,18 +25833,18 @@
2509525833
int tnum = 0;
2509625834
int isWO = 0; /* True if making an imposter of a WITHOUT ROWID table */
2509725835
int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
2509825836
int i;
2509925837
if( !ShellHasFlag(p,SHFLG_TestingMode) ){
25100
- utf8_printf(stderr, ".%s unavailable without --unsafe-testing\n",
25101
- "imposter");
25838
+ eputf(".%s unavailable without --unsafe-testing\n",
25839
+ "imposter");
2510225840
rc = 1;
2510325841
goto meta_command_exit;
2510425842
}
2510525843
if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
25106
- utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
25107
- " .imposter off\n");
25844
+ eputz("Usage: .imposter INDEX IMPOSTER\n"
25845
+ " .imposter off\n");
2510825846
/* Also allowed, but not documented:
2510925847
**
2511025848
** .imposter TABLE IMPOSTER
2511125849
**
2511225850
** where TABLE is a WITHOUT ROWID table. In that case, the
@@ -25161,11 +25899,11 @@
2516125899
zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
2516225900
}
2516325901
}
2516425902
sqlite3_finalize(pStmt);
2516525903
if( i==0 || tnum==0 ){
25166
- utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
25904
+ eputf("no such index: \"%s\"\n", azArg[1]);
2516725905
rc = 1;
2516825906
sqlite3_free(zCollist);
2516925907
goto meta_command_exit;
2517025908
}
2517125909
if( lenPK==0 ) lenPK = 100000;
@@ -25176,20 +25914,18 @@
2517625914
rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
2517725915
if( rc==SQLITE_OK ){
2517825916
rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
2517925917
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
2518025918
if( rc ){
25181
- utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
25919
+ eputf("Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
2518225920
}else{
25183
- utf8_printf(stdout, "%s;\n", zSql);
25184
- raw_printf(stdout,
25185
- "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
25186
- azArg[1], isWO ? "table" : "index"
25187
- );
25921
+ sputf(stdout, "%s;\n", zSql);
25922
+ sputf(stdout, "WARNING: writing to an imposter table will corrupt"
25923
+ " the \"%s\" %s!\n", azArg[1], isWO ? "table" : "index");
2518825924
}
2518925925
}else{
25190
- raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
25926
+ eputf("SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
2519125927
rc = 1;
2519225928
}
2519325929
sqlite3_free(zSql);
2519425930
}else
2519525931
#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
@@ -25205,11 +25941,11 @@
2520525941
sqlite3IoTrace = iotracePrintf;
2520625942
iotrace = stdout;
2520725943
}else{
2520825944
iotrace = fopen(azArg[1], "w");
2520925945
if( iotrace==0 ){
25210
- utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
25946
+ eputf("Error: cannot open \"%s\"\n", azArg[1]);
2521125947
sqlite3IoTrace = 0;
2521225948
rc = 1;
2521325949
}else{
2521425950
sqlite3IoTrace = iotracePrintf;
2521525951
}
@@ -25237,15 +25973,15 @@
2523725973
};
2523825974
int i, n2;
2523925975
open_db(p, 0);
2524025976
if( nArg==1 ){
2524125977
for(i=0; i<ArraySize(aLimit); i++){
25242
- printf("%20s %d\n", aLimit[i].zLimitName,
25243
- sqlite3_limit(p->db, aLimit[i].limitCode, -1));
25978
+ sputf(stdout, "%20s %d\n", aLimit[i].zLimitName,
25979
+ sqlite3_limit(p->db, aLimit[i].limitCode, -1));
2524425980
}
2524525981
}else if( nArg>3 ){
25246
- raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
25982
+ eputz("Usage: .limit NAME ?NEW-VALUE?\n");
2524725983
rc = 1;
2524825984
goto meta_command_exit;
2524925985
}else{
2525025986
int iLimit = -1;
2525125987
n2 = strlen30(azArg[1]);
@@ -25252,29 +25988,29 @@
2525225988
for(i=0; i<ArraySize(aLimit); i++){
2525325989
if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
2525425990
if( iLimit<0 ){
2525525991
iLimit = i;
2525625992
}else{
25257
- utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
25993
+ eputf("ambiguous limit: \"%s\"\n", azArg[1]);
2525825994
rc = 1;
2525925995
goto meta_command_exit;
2526025996
}
2526125997
}
2526225998
}
2526325999
if( iLimit<0 ){
25264
- utf8_printf(stderr, "unknown limit: \"%s\"\n"
25265
- "enter \".limits\" with no arguments for a list.\n",
25266
- azArg[1]);
26000
+ eputf("unknown limit: \"%s\"\n"
26001
+ "enter \".limits\" with no arguments for a list.\n",
26002
+ azArg[1]);
2526726003
rc = 1;
2526826004
goto meta_command_exit;
2526926005
}
2527026006
if( nArg==3 ){
2527126007
sqlite3_limit(p->db, aLimit[iLimit].limitCode,
2527226008
(int)integerValue(azArg[2]));
2527326009
}
25274
- printf("%20s %d\n", aLimit[iLimit].zLimitName,
25275
- sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
26010
+ sputf(stdout, "%20s %d\n", aLimit[iLimit].zLimitName,
26011
+ sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
2527626012
}
2527726013
}else
2527826014
2527926015
if( c=='l' && n>2 && cli_strncmp(azArg[0], "lint", n)==0 ){
2528026016
open_db(p, 0);
@@ -25286,38 +26022,38 @@
2528626022
const char *zFile, *zProc;
2528726023
char *zErrMsg = 0;
2528826024
failIfSafeMode(p, "cannot run .load in safe mode");
2528926025
if( nArg<2 || azArg[1][0]==0 ){
2529026026
/* Must have a non-empty FILE. (Will not load self.) */
25291
- raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
26027
+ eputz("Usage: .load FILE ?ENTRYPOINT?\n");
2529226028
rc = 1;
2529326029
goto meta_command_exit;
2529426030
}
2529526031
zFile = azArg[1];
2529626032
zProc = nArg>=3 ? azArg[2] : 0;
2529726033
open_db(p, 0);
2529826034
rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
2529926035
if( rc!=SQLITE_OK ){
25300
- utf8_printf(stderr, "Error: %s\n", zErrMsg);
26036
+ eputf("Error: %s\n", zErrMsg);
2530126037
sqlite3_free(zErrMsg);
2530226038
rc = 1;
2530326039
}
2530426040
}else
2530526041
#endif
2530626042
2530726043
if( c=='l' && cli_strncmp(azArg[0], "log", n)==0 ){
2530826044
if( nArg!=2 ){
25309
- raw_printf(stderr, "Usage: .log FILENAME\n");
26045
+ eputz("Usage: .log FILENAME\n");
2531026046
rc = 1;
2531126047
}else{
2531226048
const char *zFile = azArg[1];
2531326049
if( p->bSafeMode
2531426050
&& cli_strcmp(zFile,"on")!=0
2531526051
&& cli_strcmp(zFile,"off")!=0
2531626052
){
25317
- raw_printf(stdout, "cannot set .log to anything other "
25318
- "than \"on\" or \"off\"\n");
26053
+ sputz(stdout, "cannot set .log to anything other"
26054
+ " than \"on\" or \"off\"\n");
2531926055
zFile = "off";
2532026056
}
2532126057
output_file_close(p->pLog);
2532226058
if( cli_strcmp(zFile,"on")==0 ) zFile = "stdout";
2532326059
p->pLog = output_file_open(zFile, 0);
@@ -25352,37 +26088,35 @@
2535226088
cmOpts = cmo;
2535326089
}
2535426090
}else if( zTabname==0 ){
2535526091
zTabname = z;
2535626092
}else if( z[0]=='-' ){
25357
- utf8_printf(stderr, "unknown option: %s\n", z);
25358
- utf8_printf(stderr, "options:\n"
25359
- " --noquote\n"
25360
- " --quote\n"
25361
- " --wordwrap on/off\n"
25362
- " --wrap N\n"
25363
- " --ww\n");
26093
+ eputf("unknown option: %s\n", z);
26094
+ eputz("options:\n"
26095
+ " --noquote\n"
26096
+ " --quote\n"
26097
+ " --wordwrap on/off\n"
26098
+ " --wrap N\n"
26099
+ " --ww\n");
2536426100
rc = 1;
2536526101
goto meta_command_exit;
2536626102
}else{
25367
- utf8_printf(stderr, "extra argument: \"%s\"\n", z);
26103
+ eputf("extra argument: \"%s\"\n", z);
2536826104
rc = 1;
2536926105
goto meta_command_exit;
2537026106
}
2537126107
}
2537226108
if( zMode==0 ){
2537326109
if( p->mode==MODE_Column
2537426110
|| (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
2537526111
){
25376
- raw_printf
25377
- (p->out,
25378
- "current output mode: %s --wrap %d --wordwrap %s --%squote\n",
25379
- modeDescr[p->mode], p->cmOpts.iWrap,
25380
- p->cmOpts.bWordWrap ? "on" : "off",
25381
- p->cmOpts.bQuote ? "" : "no");
26112
+ oputf("current output mode: %s --wrap %d --wordwrap %s --%squote\n",
26113
+ modeDescr[p->mode], p->cmOpts.iWrap,
26114
+ p->cmOpts.bWordWrap ? "on" : "off",
26115
+ p->cmOpts.bQuote ? "" : "no");
2538226116
}else{
25383
- raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
26117
+ oputf("current output mode: %s\n", modeDescr[p->mode]);
2538426118
}
2538526119
zMode = modeDescr[p->mode];
2538626120
}
2538726121
n2 = strlen30(zMode);
2538826122
if( cli_strncmp(zMode,"lines",n2)==0 ){
@@ -25437,26 +26171,26 @@
2543726171
}else if( cli_strncmp(zMode,"off",n2)==0 ){
2543826172
p->mode = MODE_Off;
2543926173
}else if( cli_strncmp(zMode,"json",n2)==0 ){
2544026174
p->mode = MODE_Json;
2544126175
}else{
25442
- raw_printf(stderr, "Error: mode should be one of: "
25443
- "ascii box column csv html insert json line list markdown "
25444
- "qbox quote table tabs tcl\n");
26176
+ eputz("Error: mode should be one of: "
26177
+ "ascii box column csv html insert json line list markdown "
26178
+ "qbox quote table tabs tcl\n");
2544526179
rc = 1;
2544626180
}
2544726181
p->cMode = p->mode;
2544826182
}else
2544926183
2545026184
#ifndef SQLITE_SHELL_FIDDLE
2545126185
if( c=='n' && cli_strcmp(azArg[0], "nonce")==0 ){
2545226186
if( nArg!=2 ){
25453
- raw_printf(stderr, "Usage: .nonce NONCE\n");
26187
+ eputz("Usage: .nonce NONCE\n");
2545426188
rc = 1;
2545526189
}else if( p->zNonce==0 || cli_strcmp(azArg[1],p->zNonce)!=0 ){
25456
- raw_printf(stderr, "line %d: incorrect nonce: \"%s\"\n",
25457
- p->lineno, azArg[1]);
26190
+ eputf("line %d: incorrect nonce: \"%s\"\n",
26191
+ p->lineno, azArg[1]);
2545826192
exit(1);
2545926193
}else{
2546026194
p->bSafeMode = 0;
2546126195
return 0; /* Return immediately to bypass the safe mode reset
2546226196
** at the end of this procedure */
@@ -25467,11 +26201,11 @@
2546726201
if( c=='n' && cli_strncmp(azArg[0], "nullvalue", n)==0 ){
2546826202
if( nArg==2 ){
2546926203
sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
2547026204
"%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
2547126205
}else{
25472
- raw_printf(stderr, "Usage: .nullvalue STRING\n");
26206
+ eputz("Usage: .nullvalue STRING\n");
2547326207
rc = 1;
2547426208
}
2547526209
}else
2547626210
2547726211
if( c=='o' && cli_strncmp(azArg[0], "open", n)==0 && n>=2 ){
@@ -25506,15 +26240,15 @@
2550626240
p->szMax = integerValue(azArg[++iName]);
2550726241
#endif /* SQLITE_OMIT_DESERIALIZE */
2550826242
}else
2550926243
#endif /* !SQLITE_SHELL_FIDDLE */
2551026244
if( z[0]=='-' ){
25511
- utf8_printf(stderr, "unknown option: %s\n", z);
26245
+ eputf("unknown option: %s\n", z);
2551226246
rc = 1;
2551326247
goto meta_command_exit;
2551426248
}else if( zFN ){
25515
- utf8_printf(stderr, "extra argument: \"%s\"\n", z);
26249
+ eputf("extra argument: \"%s\"\n", z);
2551626250
rc = 1;
2551726251
goto meta_command_exit;
2551826252
}else{
2551926253
zFN = z;
2552026254
}
@@ -25552,11 +26286,11 @@
2555226286
zNewFilename = 0;
2555326287
}
2555426288
p->pAuxDb->zDbFilename = zNewFilename;
2555526289
open_db(p, OPEN_DB_KEEPALIVE);
2555626290
if( p->db==0 ){
25557
- utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
26291
+ eputf("Error: cannot open '%s'\n", zNewFilename);
2555826292
sqlite3_free(zNewFilename);
2555926293
}else{
2556026294
p->pAuxDb->zFreeOnClose = zNewFilename;
2556126295
}
2556226296
}
@@ -25576,13 +26310,13 @@
2557626310
char *zFile = 0;
2557726311
int bTxtMode = 0;
2557826312
int i;
2557926313
int eMode = 0;
2558026314
int bOnce = 0; /* 0: .output, 1: .once, 2: .excel */
25581
- unsigned char zBOM[4]; /* Byte-order mark to using if --bom is present */
26315
+ static const char *zBomUtf8 = "\xef\xbb\xbf";
26316
+ const char *zBom = 0;
2558226317
25583
- zBOM[0] = 0;
2558426318
failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
2558526319
if( c=='e' ){
2558626320
eMode = 'x';
2558726321
bOnce = 2;
2558826322
}else if( cli_strncmp(azArg[0],"once",n)==0 ){
@@ -25591,21 +26325,17 @@
2559126325
for(i=1; i<nArg; i++){
2559226326
char *z = azArg[i];
2559326327
if( z[0]=='-' ){
2559426328
if( z[1]=='-' ) z++;
2559526329
if( cli_strcmp(z,"-bom")==0 ){
25596
- zBOM[0] = 0xef;
25597
- zBOM[1] = 0xbb;
25598
- zBOM[2] = 0xbf;
25599
- zBOM[3] = 0;
26330
+ zBom = zBomUtf8;
2560026331
}else if( c!='e' && cli_strcmp(z,"-x")==0 ){
2560126332
eMode = 'x'; /* spreadsheet */
2560226333
}else if( c!='e' && cli_strcmp(z,"-e")==0 ){
2560326334
eMode = 'e'; /* text editor */
2560426335
}else{
25605
- utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n",
25606
- azArg[i]);
26336
+ oputf("ERROR: unknown option: \"%s\". Usage:\n", azArg[i]);
2560726337
showHelp(p->out, azArg[0]);
2560826338
rc = 1;
2560926339
goto meta_command_exit;
2561026340
}
2561126341
}else if( zFile==0 && eMode!='e' && eMode!='x' ){
@@ -25613,12 +26343,11 @@
2561326343
if( zFile && zFile[0]=='|' ){
2561426344
while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
2561526345
break;
2561626346
}
2561726347
}else{
25618
- utf8_printf(p->out,"ERROR: extra parameter: \"%s\". Usage:\n",
25619
- azArg[i]);
26348
+ oputf("ERROR: extra parameter: \"%s\". Usage:\n", azArg[i]);
2562026349
showHelp(p->out, azArg[0]);
2562126350
rc = 1;
2562226351
sqlite3_free(zFile);
2562326352
goto meta_command_exit;
2562426353
}
@@ -25653,34 +26382,34 @@
2565326382
}
2565426383
#endif /* SQLITE_NOHAVE_SYSTEM */
2565526384
shell_check_oom(zFile);
2565626385
if( zFile[0]=='|' ){
2565726386
#ifdef SQLITE_OMIT_POPEN
25658
- raw_printf(stderr, "Error: pipes are not supported in this OS\n");
26387
+ eputz("Error: pipes are not supported in this OS\n");
2565926388
rc = 1;
25660
- p->out = stdout;
26389
+ output_redir(p, stdout);
2566126390
#else
25662
- p->out = popen(zFile + 1, "w");
25663
- if( p->out==0 ){
25664
- utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
25665
- p->out = stdout;
26391
+ FILE *pfPipe = popen(zFile + 1, "w");
26392
+ if( pfPipe==0 ){
26393
+ eputf("Error: cannot open pipe \"%s\"\n", zFile + 1);
2566626394
rc = 1;
2566726395
}else{
25668
- if( zBOM[0] ) fwrite(zBOM, 1, 3, p->out);
26396
+ output_redir(p, pfPipe);
26397
+ if( zBom ) oputz(zBom);
2566926398
sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
2567026399
}
2567126400
#endif
2567226401
}else{
25673
- p->out = output_file_open(zFile, bTxtMode);
25674
- if( p->out==0 ){
26402
+ FILE *pfFile = output_file_open(zFile, bTxtMode);
26403
+ if( pfFile==0 ){
2567526404
if( cli_strcmp(zFile,"off")!=0 ){
25676
- utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
26405
+ eputf("Error: cannot write to \"%s\"\n", zFile);
2567726406
}
25678
- p->out = stdout;
2567926407
rc = 1;
2568026408
} else {
25681
- if( zBOM[0] ) fwrite(zBOM, 1, 3, p->out);
26409
+ output_redir(p, pfFile);
26410
+ if( zBom ) oputz(zBom);
2568226411
sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
2568326412
}
2568426413
}
2568526414
sqlite3_free(zFile);
2568626415
}else
@@ -25717,12 +26446,12 @@
2571726446
if( len ){
2571826447
rx = sqlite3_prepare_v2(p->db,
2571926448
"SELECT key, quote(value) "
2572026449
"FROM temp.sqlite_parameters;", -1, &pStmt, 0);
2572126450
while( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
25722
- utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
25723
- sqlite3_column_text(pStmt,1));
26451
+ oputf("%-*s %s\n", len, sqlite3_column_text(pStmt,0),
26452
+ sqlite3_column_text(pStmt,1));
2572426453
}
2572526454
sqlite3_finalize(pStmt);
2572626455
}
2572726456
}else
2572826457
@@ -25762,11 +26491,11 @@
2576226491
"VALUES(%Q,%Q);", zKey, zValue);
2576326492
shell_check_oom(zSql);
2576426493
rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
2576526494
sqlite3_free(zSql);
2576626495
if( rx!=SQLITE_OK ){
25767
- utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
26496
+ oputf("Error: %s\n", sqlite3_errmsg(p->db));
2576826497
sqlite3_finalize(pStmt);
2576926498
pStmt = 0;
2577026499
rc = 1;
2577126500
}
2577226501
}
@@ -25791,14 +26520,14 @@
2579126520
}else
2579226521
2579326522
if( c=='p' && n>=3 && cli_strncmp(azArg[0], "print", n)==0 ){
2579426523
int i;
2579526524
for(i=1; i<nArg; i++){
25796
- if( i>1 ) raw_printf(p->out, " ");
25797
- utf8_printf(p->out, "%s", azArg[i]);
26525
+ if( i>1 ) oputz(" ");
26526
+ oputz(azArg[i]);
2579826527
}
25799
- raw_printf(p->out, "\n");
26528
+ oputz("\n");
2580026529
}else
2580126530
2580226531
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
2580326532
if( c=='p' && n>=3 && cli_strncmp(azArg[0], "progress", n)==0 ){
2580426533
int i;
@@ -25823,19 +26552,19 @@
2582326552
p->flgProgress |= SHELL_PROGRESS_ONCE;
2582426553
continue;
2582526554
}
2582626555
if( cli_strcmp(z,"limit")==0 ){
2582726556
if( i+1>=nArg ){
25828
- utf8_printf(stderr, "Error: missing argument on --limit\n");
26557
+ eputz("Error: missing argument on --limit\n");
2582926558
rc = 1;
2583026559
goto meta_command_exit;
2583126560
}else{
2583226561
p->mxProgress = (int)integerValue(azArg[++i]);
2583326562
}
2583426563
continue;
2583526564
}
25836
- utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
26565
+ eputf("Error: unknown option: \"%s\"\n", azArg[i]);
2583726566
rc = 1;
2583826567
goto meta_command_exit;
2583926568
}else{
2584026569
nn = (int)integerValue(z);
2584126570
}
@@ -25864,31 +26593,31 @@
2586426593
if( c=='r' && n>=3 && cli_strncmp(azArg[0], "read", n)==0 ){
2586526594
FILE *inSaved = p->in;
2586626595
int savedLineno = p->lineno;
2586726596
failIfSafeMode(p, "cannot run .read in safe mode");
2586826597
if( nArg!=2 ){
25869
- raw_printf(stderr, "Usage: .read FILE\n");
26598
+ eputz("Usage: .read FILE\n");
2587026599
rc = 1;
2587126600
goto meta_command_exit;
2587226601
}
2587326602
if( azArg[1][0]=='|' ){
2587426603
#ifdef SQLITE_OMIT_POPEN
25875
- raw_printf(stderr, "Error: pipes are not supported in this OS\n");
26604
+ eputz("Error: pipes are not supported in this OS\n");
2587626605
rc = 1;
2587726606
p->out = stdout;
2587826607
#else
2587926608
p->in = popen(azArg[1]+1, "r");
2588026609
if( p->in==0 ){
25881
- utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
26610
+ eputf("Error: cannot open \"%s\"\n", azArg[1]);
2588226611
rc = 1;
2588326612
}else{
2588426613
rc = process_input(p);
2588526614
pclose(p->in);
2588626615
}
2588726616
#endif
2588826617
}else if( (p->in = openChrSource(azArg[1]))==0 ){
25889
- utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
26618
+ eputf("Error: cannot open \"%s\"\n", azArg[1]);
2589026619
rc = 1;
2589126620
}else{
2589226621
rc = process_input(p);
2589326622
fclose(p->in);
2589426623
}
@@ -25911,24 +26640,24 @@
2591126640
zDb = "main";
2591226641
}else if( nArg==3 ){
2591326642
zSrcFile = azArg[2];
2591426643
zDb = azArg[1];
2591526644
}else{
25916
- raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
26645
+ eputz("Usage: .restore ?DB? FILE\n");
2591726646
rc = 1;
2591826647
goto meta_command_exit;
2591926648
}
2592026649
rc = sqlite3_open(zSrcFile, &pSrc);
2592126650
if( rc!=SQLITE_OK ){
25922
- utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
26651
+ eputf("Error: cannot open \"%s\"\n", zSrcFile);
2592326652
close_db(pSrc);
2592426653
return 1;
2592526654
}
2592626655
open_db(p, 0);
2592726656
pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
2592826657
if( pBackup==0 ){
25929
- utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
26658
+ eputf("Error: %s\n", sqlite3_errmsg(p->db));
2593026659
close_db(pSrc);
2593126660
return 1;
2593226661
}
2593326662
while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
2593426663
|| rc==SQLITE_BUSY ){
@@ -25939,14 +26668,14 @@
2593926668
}
2594026669
sqlite3_backup_finish(pBackup);
2594126670
if( rc==SQLITE_DONE ){
2594226671
rc = 0;
2594326672
}else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
25944
- raw_printf(stderr, "Error: source database is busy\n");
26673
+ eputz("Error: source database is busy\n");
2594526674
rc = 1;
2594626675
}else{
25947
- utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
26676
+ eputf("Error: %s\n", sqlite3_errmsg(p->db));
2594826677
rc = 1;
2594926678
}
2595026679
close_db(pSrc);
2595126680
}else
2595226681
#endif /* !defined(SQLITE_SHELL_FIDDLE) */
@@ -25963,15 +26692,19 @@
2596326692
}
2596426693
open_db(p, 0);
2596526694
sqlite3_db_config(
2596626695
p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, p->scanstatsOn, (int*)0
2596726696
);
25968
-#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
25969
- raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
26697
+#if !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
26698
+ eputz("Warning: .scanstats not available in this build.\n");
26699
+#elif !defined(SQLITE_ENABLE_BYTECODE_VTAB)
26700
+ if( p->scanstatsOn==3 ){
26701
+ eputz("Warning: \".scanstats vm\" not available in this build.\n");
26702
+ }
2597026703
#endif
2597126704
}else{
25972
- raw_printf(stderr, "Usage: .scanstats on|off|est\n");
26705
+ eputz("Usage: .scanstats on|off|est\n");
2597326706
rc = 1;
2597426707
}
2597526708
}else
2597626709
2597726710
if( c=='s' && cli_strncmp(azArg[0], "schema", n)==0 ){
@@ -25996,18 +26729,17 @@
2599626729
}else if( optionMatch(azArg[ii],"debug") ){
2599726730
bDebug = 1;
2599826731
}else if( optionMatch(azArg[ii],"nosys") ){
2599926732
bNoSystemTabs = 1;
2600026733
}else if( azArg[ii][0]=='-' ){
26001
- utf8_printf(stderr, "Unknown option: \"%s\"\n", azArg[ii]);
26734
+ eputf("Unknown option: \"%s\"\n", azArg[ii]);
2600226735
rc = 1;
2600326736
goto meta_command_exit;
2600426737
}else if( zName==0 ){
2600526738
zName = azArg[ii];
2600626739
}else{
26007
- raw_printf(stderr,
26008
- "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
26740
+ eputz("Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
2600926741
rc = 1;
2601026742
goto meta_command_exit;
2601126743
}
2601226744
}
2601326745
if( zName!=0 ){
@@ -26036,11 +26768,11 @@
2603626768
if( zDiv ){
2603726769
sqlite3_stmt *pStmt = 0;
2603826770
rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
2603926771
-1, &pStmt, 0);
2604026772
if( rc ){
26041
- utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
26773
+ eputf("Error: %s\n", sqlite3_errmsg(p->db));
2604226774
sqlite3_finalize(pStmt);
2604326775
rc = 1;
2604426776
goto meta_command_exit;
2604526777
}
2604626778
appendText(&sSelect, "SELECT sql FROM", 0);
@@ -26098,22 +26830,22 @@
2609826830
appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
2609926831
}
2610026832
appendText(&sSelect, "sql IS NOT NULL"
2610126833
" ORDER BY snum, rowid", 0);
2610226834
if( bDebug ){
26103
- utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
26835
+ oputf("SQL: %s;\n", sSelect.z);
2610426836
}else{
2610526837
rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
2610626838
}
2610726839
freeText(&sSelect);
2610826840
}
2610926841
if( zErrMsg ){
26110
- utf8_printf(stderr,"Error: %s\n", zErrMsg);
26842
+ eputf("Error: %s\n", zErrMsg);
2611126843
sqlite3_free(zErrMsg);
2611226844
rc = 1;
2611326845
}else if( rc != SQLITE_OK ){
26114
- raw_printf(stderr,"Error: querying schema information\n");
26846
+ eputz("Error: querying schema information\n");
2611526847
rc = 1;
2611626848
}else{
2611726849
rc = 0;
2611826850
}
2611926851
}else
@@ -26155,15 +26887,15 @@
2615526887
*/
2615626888
if( cli_strcmp(azCmd[0],"attach")==0 ){
2615726889
if( nCmd!=2 ) goto session_syntax_error;
2615826890
if( pSession->p==0 ){
2615926891
session_not_open:
26160
- raw_printf(stderr, "ERROR: No sessions are open\n");
26892
+ eputz("ERROR: No sessions are open\n");
2616126893
}else{
2616226894
rc = sqlite3session_attach(pSession->p, azCmd[1]);
2616326895
if( rc ){
26164
- raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
26896
+ eputf("ERROR: sqlite3session_attach() returns %d\n",rc);
2616526897
rc = 0;
2616626898
}
2616726899
}
2616826900
}else
2616926901
@@ -26178,28 +26910,27 @@
2617826910
failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]);
2617926911
if( nCmd!=2 ) goto session_syntax_error;
2618026912
if( pSession->p==0 ) goto session_not_open;
2618126913
out = fopen(azCmd[1], "wb");
2618226914
if( out==0 ){
26183
- utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
26184
- azCmd[1]);
26915
+ eputf("ERROR: cannot open \"%s\" for writing\n",
26916
+ azCmd[1]);
2618526917
}else{
2618626918
int szChng;
2618726919
void *pChng;
2618826920
if( azCmd[0][0]=='c' ){
2618926921
rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
2619026922
}else{
2619126923
rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
2619226924
}
2619326925
if( rc ){
26194
- printf("Error: error code %d\n", rc);
26926
+ sputf(stdout, "Error: error code %d\n", rc);
2619526927
rc = 0;
2619626928
}
2619726929
if( pChng
2619826930
&& fwrite(pChng, szChng, 1, out)!=1 ){
26199
- raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
26200
- szChng);
26931
+ eputf("ERROR: Failed to write entire %d-byte output\n", szChng);
2620126932
}
2620226933
sqlite3_free(pChng);
2620326934
fclose(out);
2620426935
}
2620526936
}else
@@ -26222,12 +26953,11 @@
2622226953
int ii;
2622326954
if( nCmd>2 ) goto session_syntax_error;
2622426955
ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
2622526956
if( pAuxDb->nSession ){
2622626957
ii = sqlite3session_enable(pSession->p, ii);
26227
- utf8_printf(p->out, "session %s enable flag = %d\n",
26228
- pSession->zName, ii);
26958
+ oputf("session %s enable flag = %d\n", pSession->zName, ii);
2622926959
}
2623026960
}else
2623126961
2623226962
/* .session filter GLOB ....
2623326963
** Set a list of GLOB patterns of table names to be excluded.
@@ -26240,14 +26970,11 @@
2624026970
sqlite3_free(pSession->azFilter[ii]);
2624126971
}
2624226972
sqlite3_free(pSession->azFilter);
2624326973
nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
2624426974
pSession->azFilter = sqlite3_malloc( nByte );
26245
- if( pSession->azFilter==0 ){
26246
- raw_printf(stderr, "Error: out or memory\n");
26247
- exit(1);
26248
- }
26975
+ shell_check_oom( pSession->azFilter );
2624926976
for(ii=1; ii<nCmd; ii++){
2625026977
char *x = pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
2625126978
shell_check_oom(x);
2625226979
}
2625326980
pSession->nFilter = ii-1;
@@ -26261,12 +26988,11 @@
2626126988
int ii;
2626226989
if( nCmd>2 ) goto session_syntax_error;
2626326990
ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
2626426991
if( pAuxDb->nSession ){
2626526992
ii = sqlite3session_indirect(pSession->p, ii);
26266
- utf8_printf(p->out, "session %s indirect flag = %d\n",
26267
- pSession->zName, ii);
26993
+ oputf("session %s indirect flag = %d\n", pSession->zName, ii);
2626826994
}
2626926995
}else
2627026996
2627126997
/* .session isempty
2627226998
** Determine if the session is empty
@@ -26274,21 +27000,20 @@
2627427000
if( cli_strcmp(azCmd[0], "isempty")==0 ){
2627527001
int ii;
2627627002
if( nCmd!=1 ) goto session_syntax_error;
2627727003
if( pAuxDb->nSession ){
2627827004
ii = sqlite3session_isempty(pSession->p);
26279
- utf8_printf(p->out, "session %s isempty flag = %d\n",
26280
- pSession->zName, ii);
27005
+ oputf("session %s isempty flag = %d\n", pSession->zName, ii);
2628127006
}
2628227007
}else
2628327008
2628427009
/* .session list
2628527010
** List all currently open sessions
2628627011
*/
2628727012
if( cli_strcmp(azCmd[0],"list")==0 ){
2628827013
for(i=0; i<pAuxDb->nSession; i++){
26289
- utf8_printf(p->out, "%d %s\n", i, pAuxDb->aSession[i].zName);
27014
+ oputf("%d %s\n", i, pAuxDb->aSession[i].zName);
2629027015
}
2629127016
}else
2629227017
2629327018
/* .session open DB NAME
2629427019
** Open a new session called NAME on the attached database DB.
@@ -26299,23 +27024,22 @@
2629927024
if( nCmd!=3 ) goto session_syntax_error;
2630027025
zName = azCmd[2];
2630127026
if( zName[0]==0 ) goto session_syntax_error;
2630227027
for(i=0; i<pAuxDb->nSession; i++){
2630327028
if( cli_strcmp(pAuxDb->aSession[i].zName,zName)==0 ){
26304
- utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
27029
+ eputf("Session \"%s\" already exists\n", zName);
2630527030
goto meta_command_exit;
2630627031
}
2630727032
}
2630827033
if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){
26309
- raw_printf(stderr,
26310
- "Maximum of %d sessions\n", ArraySize(pAuxDb->aSession));
27034
+ eputf("Maximum of %d sessions\n", ArraySize(pAuxDb->aSession));
2631127035
goto meta_command_exit;
2631227036
}
2631327037
pSession = &pAuxDb->aSession[pAuxDb->nSession];
2631427038
rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
2631527039
if( rc ){
26316
- raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
27040
+ eputf("Cannot open session: error code=%d\n", rc);
2631727041
rc = 0;
2631827042
goto meta_command_exit;
2631927043
}
2632027044
pSession->nFilter = 0;
2632127045
sqlite3session_table_filter(pSession->p, session_filter, pSession);
@@ -26335,20 +27059,20 @@
2633527059
if( c=='s' && n>=10 && cli_strncmp(azArg[0], "selftest-", 9)==0 ){
2633627060
if( cli_strncmp(azArg[0]+9, "boolean", n-9)==0 ){
2633727061
int i, v;
2633827062
for(i=1; i<nArg; i++){
2633927063
v = booleanValue(azArg[i]);
26340
- utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
27064
+ oputf("%s: %d 0x%x\n", azArg[i], v, v);
2634127065
}
2634227066
}
2634327067
if( cli_strncmp(azArg[0]+9, "integer", n-9)==0 ){
2634427068
int i; sqlite3_int64 v;
2634527069
for(i=1; i<nArg; i++){
2634627070
char zBuf[200];
2634727071
v = integerValue(azArg[i]);
2634827072
sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
26349
- utf8_printf(p->out, "%s", zBuf);
27073
+ oputz(zBuf);
2635027074
}
2635127075
}
2635227076
}else
2635327077
#endif
2635427078
@@ -26371,13 +27095,12 @@
2637127095
}else
2637227096
if( cli_strcmp(z,"-v")==0 ){
2637327097
bVerbose++;
2637427098
}else
2637527099
{
26376
- utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
26377
- azArg[i], azArg[0]);
26378
- raw_printf(stderr, "Should be one of: --init -v\n");
27100
+ eputf("Unknown option \"%s\" on \"%s\"\n", azArg[i], azArg[0]);
27101
+ eputz("Should be one of: --init -v\n");
2637927102
rc = 1;
2638027103
goto meta_command_exit;
2638127104
}
2638227105
}
2638327106
if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
@@ -26402,11 +27125,11 @@
2640227125
"VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
2640327126
" (1,'run','PRAGMA integrity_check','ok')",
2640427127
-1, &pStmt, 0);
2640527128
}
2640627129
if( rc ){
26407
- raw_printf(stderr, "Error querying the selftest table\n");
27130
+ eputz("Error querying the selftest table\n");
2640827131
rc = 1;
2640927132
sqlite3_finalize(pStmt);
2641027133
goto meta_command_exit;
2641127134
}
2641227135
for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
@@ -26418,52 +27141,51 @@
2641827141
if( zOp==0 ) continue;
2641927142
if( zSql==0 ) continue;
2642027143
if( zAns==0 ) continue;
2642127144
k = 0;
2642227145
if( bVerbose>0 ){
26423
- printf("%d: %s %s\n", tno, zOp, zSql);
27146
+ sputf(stdout, "%d: %s %s\n", tno, zOp, zSql);
2642427147
}
2642527148
if( cli_strcmp(zOp,"memo")==0 ){
26426
- utf8_printf(p->out, "%s\n", zSql);
27149
+ oputf("%s\n", zSql);
2642727150
}else
2642827151
if( cli_strcmp(zOp,"run")==0 ){
2642927152
char *zErrMsg = 0;
2643027153
str.n = 0;
2643127154
str.z[0] = 0;
2643227155
rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
2643327156
nTest++;
2643427157
if( bVerbose ){
26435
- utf8_printf(p->out, "Result: %s\n", str.z);
27158
+ oputf("Result: %s\n", str.z);
2643627159
}
2643727160
if( rc || zErrMsg ){
2643827161
nErr++;
2643927162
rc = 1;
26440
- utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
27163
+ oputf("%d: error-code-%d: %s\n", tno, rc, zErrMsg);
2644127164
sqlite3_free(zErrMsg);
2644227165
}else if( cli_strcmp(zAns,str.z)!=0 ){
2644327166
nErr++;
2644427167
rc = 1;
26445
- utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
26446
- utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z);
27168
+ oputf("%d: Expected: [%s]\n", tno, zAns);
27169
+ oputf("%d: Got: [%s]\n", tno, str.z);
2644727170
}
26448
- }else
26449
- {
26450
- utf8_printf(stderr,
26451
- "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
27171
+ }
27172
+ else{
27173
+ eputf("Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
2645227174
rc = 1;
2645327175
break;
2645427176
}
2645527177
} /* End loop over rows of content from SELFTEST */
2645627178
sqlite3_finalize(pStmt);
2645727179
} /* End loop over k */
2645827180
freeText(&str);
26459
- utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
27181
+ oputf("%d errors out of %d tests\n", nErr, nTest);
2646027182
}else
2646127183
2646227184
if( c=='s' && cli_strncmp(azArg[0], "separator", n)==0 ){
2646327185
if( nArg<2 || nArg>3 ){
26464
- raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
27186
+ eputz("Usage: .separator COL ?ROW?\n");
2646527187
rc = 1;
2646627188
}
2646727189
if( nArg>=2 ){
2646827190
sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
2646927191
"%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
@@ -26502,18 +27224,17 @@
2650227224
}else
2650327225
if( cli_strcmp(z,"debug")==0 ){
2650427226
bDebug = 1;
2650527227
}else
2650627228
{
26507
- utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
26508
- azArg[i], azArg[0]);
27229
+ eputf("Unknown option \"%s\" on \"%s\"\n", azArg[i], azArg[0]);
2650927230
showHelp(p->out, azArg[0]);
2651027231
rc = 1;
2651127232
goto meta_command_exit;
2651227233
}
2651327234
}else if( zLike ){
26514
- raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
27235
+ eputz("Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
2651527236
rc = 1;
2651627237
goto meta_command_exit;
2651727238
}else{
2651827239
zLike = z;
2651927240
bSeparate = 1;
@@ -26581,11 +27302,11 @@
2658127302
}
2658227303
shell_check_oom(zSql);
2658327304
freeText(&sQuery);
2658427305
freeText(&sSql);
2658527306
if( bDebug ){
26586
- utf8_printf(p->out, "%s\n", zSql);
27307
+ oputf("%s\n", zSql);
2658727308
}else{
2658827309
shell_exec(p, zSql, 0);
2658927310
}
2659027311
#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && !defined(SQLITE_OMIT_VIRTUALTABLE)
2659127312
{
@@ -26611,11 +27332,11 @@
2661127332
"||group_concat('CAST(CAST('||cname||' AS BLOB) AS TEXT)<>'||cname\n"
2661227333
"|| ' AND typeof('||cname||')=''text'' ',\n"
2661327334
"' OR ') as query, tname from tabcols group by tname)"
2661427335
, zRevText);
2661527336
shell_check_oom(zRevText);
26616
- if( bDebug ) utf8_printf(p->out, "%s\n", zRevText);
27337
+ if( bDebug ) oputf("%s\n", zRevText);
2661727338
lrc = sqlite3_prepare_v2(p->db, zRevText, -1, &pStmt, 0);
2661827339
if( lrc!=SQLITE_OK ){
2661927340
/* assert(lrc==SQLITE_NOMEM); // might also be SQLITE_ERROR if the
2662027341
** user does cruel and unnatural things like ".limit expr_depth 0". */
2662127342
rc = 1;
@@ -26624,29 +27345,28 @@
2662427345
lrc = SQLITE_ROW==sqlite3_step(pStmt);
2662527346
if( lrc ){
2662627347
const char *zGenQuery = (char*)sqlite3_column_text(pStmt,0);
2662727348
sqlite3_stmt *pCheckStmt;
2662827349
lrc = sqlite3_prepare_v2(p->db, zGenQuery, -1, &pCheckStmt, 0);
26629
- if( bDebug ) utf8_printf(p->out, "%s\n", zGenQuery);
27350
+ if( bDebug ) oputf("%s\n", zGenQuery);
2663027351
if( lrc!=SQLITE_OK ){
2663127352
rc = 1;
2663227353
}else{
2663327354
if( SQLITE_ROW==sqlite3_step(pCheckStmt) ){
2663427355
double countIrreversible = sqlite3_column_double(pCheckStmt, 0);
2663527356
if( countIrreversible>0 ){
2663627357
int sz = (int)(countIrreversible + 0.5);
26637
- utf8_printf(stderr,
26638
- "Digest includes %d invalidly encoded text field%s.\n",
26639
- sz, (sz>1)? "s": "");
27358
+ eputf("Digest includes %d invalidly encoded text field%s.\n",
27359
+ sz, (sz>1)? "s": "");
2664027360
}
2664127361
}
2664227362
sqlite3_finalize(pCheckStmt);
2664327363
}
2664427364
sqlite3_finalize(pStmt);
2664527365
}
2664627366
}
26647
- if( rc ) utf8_printf(stderr, ".sha3sum failed.\n");
27367
+ if( rc ) eputz(".sha3sum failed.\n");
2664827368
sqlite3_free(zRevText);
2664927369
}
2665027370
#endif /* !defined(*_OMIT_SCHEMA_PRAGMAS) && !defined(*_OMIT_VIRTUALTABLE) */
2665127371
sqlite3_free(zSql);
2665227372
}else
@@ -26658,76 +27378,77 @@
2665827378
){
2665927379
char *zCmd;
2666027380
int i, x;
2666127381
failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
2666227382
if( nArg<2 ){
26663
- raw_printf(stderr, "Usage: .system COMMAND\n");
27383
+ eputz("Usage: .system COMMAND\n");
2666427384
rc = 1;
2666527385
goto meta_command_exit;
2666627386
}
2666727387
zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
2666827388
for(i=2; i<nArg && zCmd!=0; i++){
2666927389
zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
2667027390
zCmd, azArg[i]);
2667127391
}
27392
+ consoleRestore();
2667227393
x = zCmd!=0 ? system(zCmd) : 1;
27394
+ consoleRenewSetup();
2667327395
sqlite3_free(zCmd);
26674
- if( x ) raw_printf(stderr, "System command returns %d\n", x);
27396
+ if( x ) eputf("System command returns %d\n", x);
2667527397
}else
2667627398
#endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) */
2667727399
2667827400
if( c=='s' && cli_strncmp(azArg[0], "show", n)==0 ){
2667927401
static const char *azBool[] = { "off", "on", "trigger", "full"};
2668027402
const char *zOut;
2668127403
int i;
2668227404
if( nArg!=1 ){
26683
- raw_printf(stderr, "Usage: .show\n");
27405
+ eputz("Usage: .show\n");
2668427406
rc = 1;
2668527407
goto meta_command_exit;
2668627408
}
26687
- utf8_printf(p->out, "%12.12s: %s\n","echo",
26688
- azBool[ShellHasFlag(p, SHFLG_Echo)]);
26689
- utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
26690
- utf8_printf(p->out, "%12.12s: %s\n","explain",
26691
- p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
26692
- utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
27409
+ oputf("%12.12s: %s\n","echo",
27410
+ azBool[ShellHasFlag(p, SHFLG_Echo)]);
27411
+ oputf("%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
27412
+ oputf("%12.12s: %s\n","explain",
27413
+ p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
27414
+ oputf("%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
2669327415
if( p->mode==MODE_Column
2669427416
|| (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
2669527417
){
26696
- utf8_printf
26697
- (p->out, "%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode",
26698
- modeDescr[p->mode], p->cmOpts.iWrap,
26699
- p->cmOpts.bWordWrap ? "on" : "off",
26700
- p->cmOpts.bQuote ? "" : "no");
27418
+ oputf("%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode",
27419
+ modeDescr[p->mode], p->cmOpts.iWrap,
27420
+ p->cmOpts.bWordWrap ? "on" : "off",
27421
+ p->cmOpts.bQuote ? "" : "no");
2670127422
}else{
26702
- utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
26703
- }
26704
- utf8_printf(p->out, "%12.12s: ", "nullvalue");
26705
- output_c_string(p->out, p->nullValue);
26706
- raw_printf(p->out, "\n");
26707
- utf8_printf(p->out,"%12.12s: %s\n","output",
26708
- strlen30(p->outfile) ? p->outfile : "stdout");
26709
- utf8_printf(p->out,"%12.12s: ", "colseparator");
26710
- output_c_string(p->out, p->colSeparator);
26711
- raw_printf(p->out, "\n");
26712
- utf8_printf(p->out,"%12.12s: ", "rowseparator");
26713
- output_c_string(p->out, p->rowSeparator);
26714
- raw_printf(p->out, "\n");
27423
+ oputf("%12.12s: %s\n","mode", modeDescr[p->mode]);
27424
+ }
27425
+ oputf("%12.12s: ", "nullvalue");
27426
+ output_c_string(p->nullValue);
27427
+ oputz("\n");
27428
+ oputf("%12.12s: %s\n","output",
27429
+ strlen30(p->outfile) ? p->outfile : "stdout");
27430
+ oputf("%12.12s: ", "colseparator");
27431
+ output_c_string(p->colSeparator);
27432
+ oputz("\n");
27433
+ oputf("%12.12s: ", "rowseparator");
27434
+ output_c_string(p->rowSeparator);
27435
+ oputz("\n");
2671527436
switch( p->statsOn ){
2671627437
case 0: zOut = "off"; break;
2671727438
default: zOut = "on"; break;
2671827439
case 2: zOut = "stmt"; break;
2671927440
case 3: zOut = "vmstep"; break;
2672027441
}
26721
- utf8_printf(p->out, "%12.12s: %s\n","stats", zOut);
26722
- utf8_printf(p->out, "%12.12s: ", "width");
27442
+ oputf("%12.12s: %s\n","stats", zOut);
27443
+ oputf("%12.12s: ", "width");
2672327444
for (i=0;i<p->nWidth;i++) {
26724
- raw_printf(p->out, "%d ", p->colWidth[i]);
27445
+ oputf("%d ", p->colWidth[i]);
2672527446
}
26726
- raw_printf(p->out, "\n");
26727
- utf8_printf(p->out, "%12.12s: %s\n", "filename",
26728
- p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : "");
27447
+ oputz("\n");
27448
+ oputf("%12.12s: %s\n", "filename",
27449
+ p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : "");
2672927450
}else
2673027451
2673127452
if( c=='s' && cli_strncmp(azArg[0], "stats", n)==0 ){
2673227453
if( nArg==2 ){
2673327454
if( cli_strcmp(azArg[1],"stmt")==0 ){
@@ -26738,11 +27459,11 @@
2673827459
p->statsOn = (u8)booleanValue(azArg[1]);
2673927460
}
2674027461
}else if( nArg==1 ){
2674127462
display_stats(p->db, p, 0);
2674227463
}else{
26743
- raw_printf(stderr, "Usage: .stats ?on|off|stmt|vmstep?\n");
27464
+ eputz("Usage: .stats ?on|off|stmt|vmstep?\n");
2674427465
rc = 1;
2674527466
}
2674627467
}else
2674727468
2674827469
if( (c=='t' && n>1 && cli_strncmp(azArg[0], "tables", n)==0)
@@ -26764,11 +27485,11 @@
2676427485
2676527486
if( nArg>2 && c=='i' ){
2676627487
/* It is an historical accident that the .indexes command shows an error
2676727488
** when called with the wrong number of arguments whereas the .tables
2676827489
** command does not. */
26769
- raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
27490
+ eputz("Usage: .indexes ?LIKE-PATTERN?\n");
2677027491
rc = 1;
2677127492
sqlite3_finalize(pStmt);
2677227493
goto meta_command_exit;
2677327494
}
2677427495
for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
@@ -26840,14 +27561,13 @@
2684027561
if( nPrintCol<1 ) nPrintCol = 1;
2684127562
nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
2684227563
for(i=0; i<nPrintRow; i++){
2684327564
for(j=i; j<nRow; j+=nPrintRow){
2684427565
char *zSp = j<nPrintRow ? "" : " ";
26845
- utf8_printf(p->out, "%s%-*s", zSp, maxlen,
26846
- azResult[j] ? azResult[j]:"");
27566
+ oputf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j]:"");
2684727567
}
26848
- raw_printf(p->out, "\n");
27568
+ oputz("\n");
2684927569
}
2685027570
}
2685127571
2685227572
for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
2685327573
sqlite3_free(azResult);
@@ -26857,11 +27577,11 @@
2685727577
/* Begin redirecting output to the file "testcase-out.txt" */
2685827578
if( c=='t' && cli_strcmp(azArg[0],"testcase")==0 ){
2685927579
output_reset(p);
2686027580
p->out = output_file_open("testcase-out.txt", 0);
2686127581
if( p->out==0 ){
26862
- raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
27582
+ eputz("Error: cannot open 'testcase-out.txt'\n");
2686327583
}
2686427584
if( nArg>=2 ){
2686527585
sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
2686627586
}else{
2686727587
sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
@@ -26898,11 +27618,11 @@
2689827618
{"prng_save", SQLITE_TESTCTRL_PRNG_SAVE, 0, "" },
2689927619
{"prng_seed", SQLITE_TESTCTRL_PRNG_SEED, 0, "SEED ?db?" },
2690027620
{"seek_count", SQLITE_TESTCTRL_SEEK_COUNT, 0, "" },
2690127621
{"sorter_mmap", SQLITE_TESTCTRL_SORTER_MMAP, 0, "NMAX" },
2690227622
{"tune", SQLITE_TESTCTRL_TUNE, 1, "ID VALUE" },
26903
- {"uselongdouble", SQLITE_TESTCTRL_USELONGDOUBLE,0,"?BOOLEAN|\"default\"?"},
27623
+ {"uselongdouble", SQLITE_TESTCTRL_USELONGDOUBLE,0,"?BOOLEAN|\"default\"?"},
2690427624
};
2690527625
int testctrl = -1;
2690627626
int iCtrl = -1;
2690727627
int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */
2690827628
int isOk = 0;
@@ -26918,15 +27638,15 @@
2691827638
if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
2691927639
}
2692027640
2692127641
/* --help lists all test-controls */
2692227642
if( cli_strcmp(zCmd,"help")==0 ){
26923
- utf8_printf(p->out, "Available test-controls:\n");
27643
+ oputz("Available test-controls:\n");
2692427644
for(i=0; i<ArraySize(aCtrl); i++){
2692527645
if( aCtrl[i].unSafe && !ShellHasFlag(p,SHFLG_TestingMode) ) continue;
26926
- utf8_printf(p->out, " .testctrl %s %s\n",
26927
- aCtrl[i].zCtrlName, aCtrl[i].zUsage);
27646
+ oputf(" .testctrl %s %s\n",
27647
+ aCtrl[i].zCtrlName, aCtrl[i].zUsage);
2692827648
}
2692927649
rc = 1;
2693027650
goto meta_command_exit;
2693127651
}
2693227652
@@ -26938,20 +27658,20 @@
2693827658
if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
2693927659
if( testctrl<0 ){
2694027660
testctrl = aCtrl[i].ctrlCode;
2694127661
iCtrl = i;
2694227662
}else{
26943
- utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
26944
- "Use \".testctrl --help\" for help\n", zCmd);
27663
+ eputf("Error: ambiguous test-control: \"%s\"\n"
27664
+ "Use \".testctrl --help\" for help\n", zCmd);
2694527665
rc = 1;
2694627666
goto meta_command_exit;
2694727667
}
2694827668
}
2694927669
}
2695027670
if( testctrl<0 ){
26951
- utf8_printf(stderr,"Error: unknown test-control: %s\n"
26952
- "Use \".testctrl --help\" for help\n", zCmd);
27671
+ eputf("Error: unknown test-control: %s\n"
27672
+ "Use \".testctrl --help\" for help\n", zCmd);
2695327673
}else{
2695427674
switch(testctrl){
2695527675
2695627676
/* sqlite3_test_control(int, db, int) */
2695727677
case SQLITE_TESTCTRL_OPTIMIZATIONS:
@@ -26987,11 +27707,11 @@
2698727707
if( nArg==3 || nArg==4 ){
2698827708
int ii = (int)integerValue(azArg[2]);
2698927709
sqlite3 *db;
2699027710
if( ii==0 && cli_strcmp(azArg[2],"random")==0 ){
2699127711
sqlite3_randomness(sizeof(ii),&ii);
26992
- printf("-- random seed: %d\n", ii);
27712
+ sputf(stdout, "-- random seed: %d\n", ii);
2699327713
}
2699427714
if( nArg==3 ){
2699527715
db = 0;
2699627716
}else{
2699727717
db = p->db;
@@ -27055,11 +27775,11 @@
2705527775
break;
2705627776
2705727777
case SQLITE_TESTCTRL_SEEK_COUNT: {
2705827778
u64 x = 0;
2705927779
rc2 = sqlite3_test_control(testctrl, p->db, &x);
27060
- utf8_printf(p->out, "%llu\n", x);
27780
+ oputf("%llu\n", x);
2706127781
isOk = 3;
2706227782
break;
2706327783
}
2706427784
2706527785
#ifdef YYCOVERAGE
@@ -27086,15 +27806,15 @@
2708627806
int id = 1;
2708727807
while(1){
2708827808
int val = 0;
2708927809
rc2 = sqlite3_test_control(testctrl, -id, &val);
2709027810
if( rc2!=SQLITE_OK ) break;
27091
- if( id>1 ) utf8_printf(p->out, " ");
27092
- utf8_printf(p->out, "%d: %d", id, val);
27811
+ if( id>1 ) oputz(" ");
27812
+ oputf("%d: %d", id, val);
2709327813
id++;
2709427814
}
27095
- if( id>1 ) utf8_printf(p->out, "\n");
27815
+ if( id>1 ) oputz("\n");
2709627816
isOk = 3;
2709727817
}
2709827818
break;
2709927819
}
2710027820
#endif
@@ -27106,16 +27826,16 @@
2710627826
}
2710727827
break;
2710827828
}
2710927829
}
2711027830
if( isOk==0 && iCtrl>=0 ){
27111
- utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
27831
+ oputf("Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
2711227832
rc = 1;
2711327833
}else if( isOk==1 ){
27114
- raw_printf(p->out, "%d\n", rc2);
27834
+ oputf("%d\n", rc2);
2711527835
}else if( isOk==2 ){
27116
- raw_printf(p->out, "0x%08x\n", rc2);
27836
+ oputf("0x%08x\n", rc2);
2711727837
}
2711827838
}else
2711927839
#endif /* !defined(SQLITE_UNTESTABLE) */
2712027840
2712127841
if( c=='t' && n>4 && cli_strncmp(azArg[0], "timeout", n)==0 ){
@@ -27125,15 +27845,15 @@
2712527845
2712627846
if( c=='t' && n>=5 && cli_strncmp(azArg[0], "timer", n)==0 ){
2712727847
if( nArg==2 ){
2712827848
enableTimer = booleanValue(azArg[1]);
2712927849
if( enableTimer && !HAS_TIMER ){
27130
- raw_printf(stderr, "Error: timer not available on this system.\n");
27850
+ eputz("Error: timer not available on this system.\n");
2713127851
enableTimer = 0;
2713227852
}
2713327853
}else{
27134
- raw_printf(stderr, "Usage: .timer on|off\n");
27854
+ eputz("Usage: .timer on|off\n");
2713527855
rc = 1;
2713627856
}
2713727857
}else
2713827858
2713927859
#ifndef SQLITE_OMIT_TRACE
@@ -27166,11 +27886,11 @@
2716627886
}
2716727887
else if( optionMatch(z, "close") ){
2716827888
mType |= SQLITE_TRACE_CLOSE;
2716927889
}
2717027890
else {
27171
- raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
27891
+ eputf("Unknown option \"%s\" on \".trace\"\n", z);
2717227892
rc = 1;
2717327893
goto meta_command_exit;
2717427894
}
2717527895
}else{
2717627896
output_file_close(p->traceOut);
@@ -27190,11 +27910,11 @@
2719027910
if( c=='u' && cli_strncmp(azArg[0], "unmodule", n)==0 ){
2719127911
int ii;
2719227912
int lenOpt;
2719327913
char *zOpt;
2719427914
if( nArg<2 ){
27195
- raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
27915
+ eputz("Usage: .unmodule [--allexcept] NAME ...\n");
2719627916
rc = 1;
2719727917
goto meta_command_exit;
2719827918
}
2719927919
open_db(p, 0);
2720027920
zOpt = azArg[1];
@@ -27212,100 +27932,100 @@
2721227932
#endif
2721327933
2721427934
#if SQLITE_USER_AUTHENTICATION
2721527935
if( c=='u' && cli_strncmp(azArg[0], "user", n)==0 ){
2721627936
if( nArg<2 ){
27217
- raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
27937
+ eputz("Usage: .user SUBCOMMAND ...\n");
2721827938
rc = 1;
2721927939
goto meta_command_exit;
2722027940
}
2722127941
open_db(p, 0);
2722227942
if( cli_strcmp(azArg[1],"login")==0 ){
2722327943
if( nArg!=4 ){
27224
- raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
27944
+ eputz("Usage: .user login USER PASSWORD\n");
2722527945
rc = 1;
2722627946
goto meta_command_exit;
2722727947
}
2722827948
rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
2722927949
strlen30(azArg[3]));
2723027950
if( rc ){
27231
- utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
27951
+ eputf("Authentication failed for user %s\n", azArg[2]);
2723227952
rc = 1;
2723327953
}
2723427954
}else if( cli_strcmp(azArg[1],"add")==0 ){
2723527955
if( nArg!=5 ){
27236
- raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
27956
+ eputz("Usage: .user add USER PASSWORD ISADMIN\n");
2723727957
rc = 1;
2723827958
goto meta_command_exit;
2723927959
}
2724027960
rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
2724127961
booleanValue(azArg[4]));
2724227962
if( rc ){
27243
- raw_printf(stderr, "User-Add failed: %d\n", rc);
27963
+ eputf("User-Add failed: %d\n", rc);
2724427964
rc = 1;
2724527965
}
2724627966
}else if( cli_strcmp(azArg[1],"edit")==0 ){
2724727967
if( nArg!=5 ){
27248
- raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
27968
+ eputz("Usage: .user edit USER PASSWORD ISADMIN\n");
2724927969
rc = 1;
2725027970
goto meta_command_exit;
2725127971
}
2725227972
rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
2725327973
booleanValue(azArg[4]));
2725427974
if( rc ){
27255
- raw_printf(stderr, "User-Edit failed: %d\n", rc);
27975
+ eputf("User-Edit failed: %d\n", rc);
2725627976
rc = 1;
2725727977
}
2725827978
}else if( cli_strcmp(azArg[1],"delete")==0 ){
2725927979
if( nArg!=3 ){
27260
- raw_printf(stderr, "Usage: .user delete USER\n");
27980
+ eputz("Usage: .user delete USER\n");
2726127981
rc = 1;
2726227982
goto meta_command_exit;
2726327983
}
2726427984
rc = sqlite3_user_delete(p->db, azArg[2]);
2726527985
if( rc ){
27266
- raw_printf(stderr, "User-Delete failed: %d\n", rc);
27986
+ eputf("User-Delete failed: %d\n", rc);
2726727987
rc = 1;
2726827988
}
2726927989
}else{
27270
- raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
27990
+ eputz("Usage: .user login|add|edit|delete ...\n");
2727127991
rc = 1;
2727227992
goto meta_command_exit;
2727327993
}
2727427994
}else
2727527995
#endif /* SQLITE_USER_AUTHENTICATION */
2727627996
2727727997
if( c=='v' && cli_strncmp(azArg[0], "version", n)==0 ){
2727827998
char *zPtrSz = sizeof(void*)==8 ? "64-bit" : "32-bit";
27279
- utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
27280
- sqlite3_libversion(), sqlite3_sourceid());
27999
+ oputf("SQLite %s %s\n" /*extra-version-info*/,
28000
+ sqlite3_libversion(), sqlite3_sourceid());
2728128001
#if SQLITE_HAVE_ZLIB
27282
- utf8_printf(p->out, "zlib version %s\n", zlibVersion());
28002
+ oputf("zlib version %s\n", zlibVersion());
2728328003
#endif
2728428004
#define CTIMEOPT_VAL_(opt) #opt
2728528005
#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
2728628006
#if defined(__clang__) && defined(__clang_major__)
27287
- utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
27288
- CTIMEOPT_VAL(__clang_minor__) "."
27289
- CTIMEOPT_VAL(__clang_patchlevel__) " (%s)\n", zPtrSz);
28007
+ oputf("clang-" CTIMEOPT_VAL(__clang_major__) "."
28008
+ CTIMEOPT_VAL(__clang_minor__) "."
28009
+ CTIMEOPT_VAL(__clang_patchlevel__) " (%s)\n", zPtrSz);
2729028010
#elif defined(_MSC_VER)
27291
- utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) " (%s)\n", zPtrSz);
28011
+ oputf("msvc-" CTIMEOPT_VAL(_MSC_VER) " (%s)\n", zPtrSz);
2729228012
#elif defined(__GNUC__) && defined(__VERSION__)
27293
- utf8_printf(p->out, "gcc-" __VERSION__ " (%s)\n", zPtrSz);
28013
+ oputf("gcc-" __VERSION__ " (%s)\n", zPtrSz);
2729428014
#endif
2729528015
}else
2729628016
2729728017
if( c=='v' && cli_strncmp(azArg[0], "vfsinfo", n)==0 ){
2729828018
const char *zDbName = nArg==2 ? azArg[1] : "main";
2729928019
sqlite3_vfs *pVfs = 0;
2730028020
if( p->db ){
2730128021
sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
2730228022
if( pVfs ){
27303
- utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName);
27304
- raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
27305
- raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
27306
- raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
28023
+ oputf("vfs.zName = \"%s\"\n", pVfs->zName);
28024
+ oputf("vfs.iVersion = %d\n", pVfs->iVersion);
28025
+ oputf("vfs.szOsFile = %d\n", pVfs->szOsFile);
28026
+ oputf("vfs.mxPathname = %d\n", pVfs->mxPathname);
2730728027
}
2730828028
}
2730928029
}else
2731028030
2731128031
if( c=='v' && cli_strncmp(azArg[0], "vfslist", n)==0 ){
@@ -27313,17 +28033,17 @@
2731328033
sqlite3_vfs *pCurrent = 0;
2731428034
if( p->db ){
2731528035
sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
2731628036
}
2731728037
for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
27318
- utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName,
27319
- pVfs==pCurrent ? " <--- CURRENT" : "");
27320
- raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
27321
- raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
27322
- raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
28038
+ oputf("vfs.zName = \"%s\"%s\n", pVfs->zName,
28039
+ pVfs==pCurrent ? " <--- CURRENT" : "");
28040
+ oputf("vfs.iVersion = %d\n", pVfs->iVersion);
28041
+ oputf("vfs.szOsFile = %d\n", pVfs->szOsFile);
28042
+ oputf("vfs.mxPathname = %d\n", pVfs->mxPathname);
2732328043
if( pVfs->pNext ){
27324
- raw_printf(p->out, "-----------------------------------\n");
28044
+ oputz("-----------------------------------\n");
2732528045
}
2732628046
}
2732728047
}else
2732828048
2732928049
if( c=='v' && cli_strncmp(azArg[0], "vfsname", n)==0 ){
@@ -27330,11 +28050,11 @@
2733028050
const char *zDbName = nArg==2 ? azArg[1] : "main";
2733128051
char *zVfsName = 0;
2733228052
if( p->db ){
2733328053
sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
2733428054
if( zVfsName ){
27335
- utf8_printf(p->out, "%s\n", zVfsName);
28055
+ oputf("%s\n", zVfsName);
2733628056
sqlite3_free(zVfsName);
2733728057
}
2733828058
}
2733928059
}else
2734028060
@@ -27354,12 +28074,12 @@
2735428074
p->colWidth[j-1] = (int)integerValue(azArg[j]);
2735528075
}
2735628076
}else
2735728077
2735828078
{
27359
- utf8_printf(stderr, "Error: unknown command or invalid arguments: "
27360
- " \"%s\". Enter \".help\" for help\n", azArg[0]);
28079
+ eputf("Error: unknown command or invalid arguments: "
28080
+ " \"%s\". Enter \".help\" for help\n", azArg[0]);
2736128081
rc = 1;
2736228082
}
2736328083
2736428084
meta_command_exit:
2736528085
if( p->outCount ){
@@ -27545,26 +28265,26 @@
2754528265
sqlite3_snprintf(sizeof(zPrefix), zPrefix,
2754628266
"%s near line %d:", zErrorType, startline);
2754728267
}else{
2754828268
sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:", zErrorType);
2754928269
}
27550
- utf8_printf(stderr, "%s %s\n", zPrefix, zErrorTail);
28270
+ eputf("%s %s\n", zPrefix, zErrorTail);
2755128271
sqlite3_free(zErrMsg);
2755228272
zErrMsg = 0;
2755328273
return 1;
2755428274
}else if( ShellHasFlag(p, SHFLG_CountChanges) ){
2755528275
char zLineBuf[2000];
2755628276
sqlite3_snprintf(sizeof(zLineBuf), zLineBuf,
2755728277
"changes: %lld total_changes: %lld",
2755828278
sqlite3_changes64(p->db), sqlite3_total_changes64(p->db));
27559
- raw_printf(p->out, "%s\n", zLineBuf);
28279
+ oputf("%s\n", zLineBuf);
2756028280
}
2756128281
return 0;
2756228282
}
2756328283
2756428284
static void echo_group_input(ShellState *p, const char *zDo){
27565
- if( ShellHasFlag(p, SHFLG_Echo) ) utf8_printf(p->out, "%s\n", zDo);
28285
+ if( ShellHasFlag(p, SHFLG_Echo) ) oputf("%s\n", zDo);
2756628286
}
2756728287
2756828288
#ifdef SQLITE_SHELL_FIDDLE
2756928289
/*
2757028290
** Alternate one_input_line() impl for wasm mode. This is not in the primary
@@ -27618,12 +28338,12 @@
2761828338
i64 startline = 0; /* Line number for start of current input */
2761928339
QuickScanState qss = QSS_Start; /* Accumulated line status (so far) */
2762028340
2762128341
if( p->inputNesting==MAX_INPUT_NESTING ){
2762228342
/* This will be more informative in a later version. */
27623
- utf8_printf(stderr,"Input nesting limit (%d) reached at line %d."
27624
- " Check recursion.\n", MAX_INPUT_NESTING, p->lineno);
28343
+ eputf("Input nesting limit (%d) reached at line %d."
28344
+ " Check recursion.\n", MAX_INPUT_NESTING, p->lineno);
2762528345
return 1;
2762628346
}
2762728347
++p->inputNesting;
2762828348
p->lineno = 0;
2762928349
CONTINUE_PROMPT_RESET;
@@ -27630,11 +28350,11 @@
2763028350
while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
2763128351
fflush(p->out);
2763228352
zLine = one_input_line(p->in, zLine, nSql>0);
2763328353
if( zLine==0 ){
2763428354
/* End of input */
27635
- if( p->in==0 && stdin_is_interactive ) printf("\n");
28355
+ if( p->in==0 && stdin_is_interactive ) oputz("\n");
2763628356
break;
2763728357
}
2763828358
if( seenInterrupt ){
2763928359
if( p->in!=0 ) break;
2764028360
seenInterrupt = 0;
@@ -27840,27 +28560,27 @@
2784028560
sqliterc = find_xdg_config();
2784128561
}
2784228562
if( sqliterc == NULL ){
2784328563
home_dir = find_home_dir(0);
2784428564
if( home_dir==0 ){
27845
- raw_printf(stderr, "-- warning: cannot find home directory;"
27846
- " cannot read ~/.sqliterc\n");
28565
+ eputz("-- warning: cannot find home directory;"
28566
+ " cannot read ~/.sqliterc\n");
2784728567
return;
2784828568
}
2784928569
zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
2785028570
shell_check_oom(zBuf);
2785128571
sqliterc = zBuf;
2785228572
}
2785328573
p->in = fopen(sqliterc,"rb");
2785428574
if( p->in ){
2785528575
if( stdin_is_interactive ){
27856
- utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
28576
+ eputf("-- Loading resources from %s\n", sqliterc);
2785728577
}
2785828578
if( process_input(p) && bail_on_error ) exit(1);
2785928579
fclose(p->in);
2786028580
}else if( sqliterc_override!=0 ){
27861
- utf8_printf(stderr,"cannot open: \"%s\"\n", sqliterc);
28581
+ eputf("cannot open: \"%s\"\n", sqliterc);
2786228582
if( bail_on_error ) exit(1);
2786328583
}
2786428584
p->in = inSaved;
2786528585
p->lineno = savedLineno;
2786628586
sqlite3_free(zBuf);
@@ -27906,13 +28626,10 @@
2790628626
" -mmap N default mmap size set to N\n"
2790728627
#ifdef SQLITE_ENABLE_MULTIPLEX
2790828628
" -multiplex enable the multiplexor VFS\n"
2790928629
#endif
2791028630
" -newline SEP set output row separator. Default: '\\n'\n"
27911
-#if SHELL_WIN_UTF8_OPT
27912
- " -no-utf8 do not try to set up UTF-8 output (for legacy)\n"
27913
-#endif
2791428631
" -nofollow refuse to open symbolic links to database files\n"
2791528632
" -nonce STRING set the safe-mode escape nonce\n"
2791628633
" -nullvalue TEXT set text string for NULL values. Default ''\n"
2791728634
" -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
2791828635
" -pcachetrace trace all page cache operations\n"
@@ -27925,13 +28642,10 @@
2792528642
#endif
2792628643
" -stats print memory stats before each finalize\n"
2792728644
" -table set output mode to 'table'\n"
2792828645
" -tabs set output mode to 'tabs'\n"
2792928646
" -unsafe-testing allow unsafe commands and modes for testing\n"
27930
-#if SHELL_WIN_UTF8_OPT && 0 /* Option is accepted, but is now the default. */
27931
- " -utf8 setup interactive console code page for UTF-8\n"
27932
-#endif
2793328647
" -version show SQLite version\n"
2793428648
" -vfs NAME use NAME as the default VFS\n"
2793528649
#ifdef SQLITE_ENABLE_VFSTRACE
2793628650
" -vfstrace enable tracing of all VFS calls\n"
2793728651
#endif
@@ -27938,18 +28652,17 @@
2793828652
#ifdef SQLITE_HAVE_ZLIB
2793928653
" -zip open the file as a ZIP Archive\n"
2794028654
#endif
2794128655
;
2794228656
static void usage(int showDetail){
27943
- utf8_printf(stderr,
27944
- "Usage: %s [OPTIONS] [FILENAME [SQL]]\n"
27945
- "FILENAME is the name of an SQLite database. A new database is created\n"
27946
- "if the file does not previously exist. Defaults to :memory:.\n", Argv0);
28657
+ eputf("Usage: %s [OPTIONS] [FILENAME [SQL]]\n"
28658
+ "FILENAME is the name of an SQLite database. A new database is created\n"
28659
+ "if the file does not previously exist. Defaults to :memory:.\n", Argv0);
2794728660
if( showDetail ){
27948
- utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
28661
+ eputf("OPTIONS include:\n%s", zOptions);
2794928662
}else{
27950
- raw_printf(stderr, "Use the -help option for additional information\n");
28663
+ eputz("Use the -help option for additional information\n");
2795128664
}
2795228665
exit(1);
2795328666
}
2795428667
2795528668
/*
@@ -27956,12 +28669,12 @@
2795628669
** Internal check: Verify that the SQLite is uninitialized. Print a
2795728670
** error message if it is initialized.
2795828671
*/
2795928672
static void verify_uninitialized(void){
2796028673
if( sqlite3_config(-1)==SQLITE_MISUSE ){
27961
- utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
27962
- " initialization.\n");
28674
+ sputz(stdout, "WARNING: attempt to configure SQLite after"
28675
+ " initialization.\n");
2796328676
}
2796428677
}
2796528678
2796628679
/*
2796728680
** Initialize the state information in data
@@ -27986,46 +28699,45 @@
2798628699
}
2798728700
2798828701
/*
2798928702
** Output text to the console in a font that attracts extra attention.
2799028703
*/
27991
-#ifdef _WIN32
28704
+#if defined(_WIN32) || defined(WIN32)
2799228705
static void printBold(const char *zText){
2799328706
#if !SQLITE_OS_WINRT
2799428707
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
2799528708
CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
2799628709
GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
2799728710
SetConsoleTextAttribute(out,
2799828711
FOREGROUND_RED|FOREGROUND_INTENSITY
2799928712
);
2800028713
#endif
28001
- printf("%s", zText);
28714
+ oputz(zText);
2800228715
#if !SQLITE_OS_WINRT
2800328716
SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
2800428717
#endif
2800528718
}
2800628719
#else
2800728720
static void printBold(const char *zText){
28008
- printf("\033[1m%s\033[0m", zText);
28721
+ oputf("\033[1m%s\033[0m", zText);
2800928722
}
2801028723
#endif
2801128724
2801228725
/*
2801328726
** Get the argument to an --option. Throw an error and die if no argument
2801428727
** is available.
2801528728
*/
2801628729
static char *cmdline_option_value(int argc, char **argv, int i){
2801728730
if( i==argc ){
28018
- utf8_printf(stderr, "%s: Error: missing argument to %s\n",
28019
- argv[0], argv[argc-1]);
28731
+ eputf("%s: Error: missing argument to %s\n", argv[0], argv[argc-1]);
2802028732
exit(1);
2802128733
}
2802228734
return argv[i];
2802328735
}
2802428736
2802528737
static void sayAbnormalExit(void){
28026
- if( seenInterrupt ) fprintf(stderr, "Program interrupted.\n");
28738
+ if( seenInterrupt ) eputz("Program interrupted.\n");
2802728739
}
2802828740
2802928741
#ifndef SQLITE_SHELL_IS_UTF8
2803028742
# if (defined(_WIN32) || defined(WIN32)) \
2803128743
&& (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
@@ -28051,10 +28763,11 @@
2805128763
char *zErrMsg = 0;
2805228764
#ifdef SQLITE_SHELL_FIDDLE
2805328765
# define data shellState
2805428766
#else
2805528767
ShellState data;
28768
+ StreamsAreConsole consStreams = SAC_NoConsole;
2805628769
#endif
2805728770
const char *zInitFile = 0;
2805828771
int i;
2805928772
int rc = 0;
2806028773
int warnInmemoryDb = 0;
@@ -28072,27 +28785,24 @@
2807228785
#ifdef SQLITE_SHELL_FIDDLE
2807328786
stdin_is_interactive = 0;
2807428787
stdout_is_console = 1;
2807528788
data.wasm.zDefaultDbName = "/fiddle.sqlite3";
2807628789
#else
28077
- stdin_is_interactive = isatty(0);
28078
- stdout_is_console = isatty(1);
28079
-#endif
28080
-#if SHELL_WIN_UTF8_OPT
28081
- probe_console(); /* Check for console I/O and UTF-8 capability. */
28082
- if( !mbcs_opted ) atexit(console_restore);
28790
+ consStreams = consoleClassifySetup(stdin, stdout, stderr);
28791
+ stdin_is_interactive = (consStreams & SAC_InConsole)!=0;
28792
+ stdout_is_console = (consStreams & SAC_OutConsole)!=0;
28793
+ atexit(consoleRestore);
2808328794
#endif
2808428795
atexit(sayAbnormalExit);
2808528796
#ifdef SQLITE_DEBUG
2808628797
mem_main_enter = sqlite3_memory_used();
2808728798
#endif
2808828799
#if !defined(_WIN32_WCE)
2808928800
if( getenv("SQLITE_DEBUG_BREAK") ){
2809028801
if( isatty(0) && isatty(2) ){
28091
- fprintf(stderr,
28092
- "attach debugger to process %d and press any key to continue.\n",
28093
- GETPID());
28802
+ eputf("attach debugger to process %d and press any key to continue.\n",
28803
+ GETPID());
2809428804
fgetc(stdin);
2809528805
}else{
2809628806
#if defined(_WIN32) || defined(WIN32)
2809728807
#if SQLITE_OS_WINRT
2809828808
__debugbreak();
@@ -28108,18 +28818,18 @@
2810828818
/* Register a valid signal handler early, before much else is done. */
2810928819
#ifdef SIGINT
2811028820
signal(SIGINT, interrupt_handler);
2811128821
#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
2811228822
if( !SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE) ){
28113
- fprintf(stderr, "No ^C handler.\n");
28823
+ eputz("No ^C handler.\n");
2811428824
}
2811528825
#endif
2811628826
2811728827
#if USE_SYSTEM_SQLITE+0!=1
2811828828
if( cli_strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
28119
- utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
28120
- sqlite3_sourceid(), SQLITE_SOURCE_ID);
28829
+ eputf("SQLite header and source version mismatch\n%s\n%s\n",
28830
+ sqlite3_sourceid(), SQLITE_SOURCE_ID);
2812128831
exit(1);
2812228832
}
2812328833
#endif
2812428834
main_init(&data);
2812528835
@@ -28211,18 +28921,11 @@
2821128921
** informational messages (like from process_sqliterc) before
2821228922
** we do the actual processing of arguments later in a second pass.
2821328923
*/
2821428924
stdin_is_interactive = 0;
2821528925
}else if( cli_strcmp(z,"-utf8")==0 ){
28216
-#if SHELL_WIN_UTF8_OPT
28217
- /* Option accepted, but is ignored except for this diagnostic. */
28218
- if( mbcs_opted ) fprintf(stderr, "Cannot do UTF-8 at this console.\n");
28219
-#endif /* SHELL_WIN_UTF8_OPT */
2822028926
}else if( cli_strcmp(z,"-no-utf8")==0 ){
28221
-#if SHELL_WIN_UTF8_OPT
28222
- mbcs_opted = 1;
28223
-#endif /* SHELL_WIN_UTF8_OPT */
2822428927
}else if( cli_strcmp(z,"-heap")==0 ){
2822528928
#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
2822628929
const char *zSize;
2822728930
sqlite3_int64 szHeap;
2822828931
@@ -28353,30 +29056,21 @@
2835329056
if( zVfs ){
2835429057
sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
2835529058
if( pVfs ){
2835629059
sqlite3_vfs_register(pVfs, 1);
2835729060
}else{
28358
- utf8_printf(stderr, "no such VFS: \"%s\"\n", zVfs);
29061
+ eputf("no such VFS: \"%s\"\n", zVfs);
2835929062
exit(1);
2836029063
}
2836129064
}
28362
-#if SHELL_WIN_UTF8_OPT
28363
- /* Get indicated Windows console setup done before running invocation commands. */
28364
- if( in_console || out_console ){
28365
- console_prepare_utf8();
28366
- }
28367
- if( !in_console ){
28368
- setBinaryMode(stdin, 0);
28369
- }
28370
-#endif
2837129065
2837229066
if( data.pAuxDb->zDbFilename==0 ){
2837329067
#ifndef SQLITE_OMIT_MEMORYDB
2837429068
data.pAuxDb->zDbFilename = ":memory:";
2837529069
warnInmemoryDb = argc==1;
2837629070
#else
28377
- utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
29071
+ eputf("%s: Error: no database filename specified\n", Argv0);
2837829072
return 1;
2837929073
#endif
2838029074
}
2838129075
data.out = stdout;
2838229076
#ifndef SQLITE_SHELL_FIDDLE
@@ -28489,12 +29183,12 @@
2848929183
*/
2849029184
ShellSetFlag(&data, SHFLG_Backslash);
2849129185
}else if( cli_strcmp(z,"-bail")==0 ){
2849229186
/* No-op. The bail_on_error flag should already be set. */
2849329187
}else if( cli_strcmp(z,"-version")==0 ){
28494
- printf("%s %s (%d-bit)\n", sqlite3_libversion(), sqlite3_sourceid(),
28495
- 8*(int)sizeof(char*));
29188
+ oputf("%s %s (%d-bit)\n", sqlite3_libversion(), sqlite3_sourceid(),
29189
+ 8*(int)sizeof(char*));
2849629190
return 0;
2849729191
}else if( cli_strcmp(z,"-interactive")==0 ){
2849829192
/* already handled */
2849929193
}else if( cli_strcmp(z,"-batch")==0 ){
2850029194
/* already handled */
@@ -28546,22 +29240,22 @@
2854629240
if( rc && bail_on_error ) return rc==2 ? 0 : rc;
2854729241
}else{
2854829242
open_db(&data, 0);
2854929243
rc = shell_exec(&data, z, &zErrMsg);
2855029244
if( zErrMsg!=0 ){
28551
- utf8_printf(stderr,"Error: %s\n", zErrMsg);
29245
+ eputf("Error: %s\n", zErrMsg);
2855229246
if( bail_on_error ) return rc!=0 ? rc : 1;
2855329247
}else if( rc!=0 ){
28554
- utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
29248
+ eputf("Error: unable to process SQL \"%s\"\n", z);
2855529249
if( bail_on_error ) return rc;
2855629250
}
2855729251
}
2855829252
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
2855929253
}else if( cli_strncmp(z, "-A", 2)==0 ){
2856029254
if( nCmd>0 ){
28561
- utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
28562
- " with \"%s\"\n", z);
29255
+ eputf("Error: cannot mix regular SQL or dot-commands"
29256
+ " with \"%s\"\n", z);
2856329257
return 1;
2856429258
}
2856529259
open_db(&data, OPEN_DB_ZIPFILE);
2856629260
if( z[2] ){
2856729261
argv[i] = &z[2];
@@ -28575,12 +29269,12 @@
2857529269
}else if( cli_strcmp(z,"-safe")==0 ){
2857629270
data.bSafeMode = data.bSafeModePersist = 1;
2857729271
}else if( cli_strcmp(z,"-unsafe-testing")==0 ){
2857829272
/* Acted upon in first pass. */
2857929273
}else{
28580
- utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
28581
- raw_printf(stderr,"Use -help for a list of options.\n");
29274
+ eputf("%s: Error: unknown option: %s\n", Argv0, z);
29275
+ eputz("Use -help for a list of options.\n");
2858229276
return 1;
2858329277
}
2858429278
data.cMode = data.mode;
2858529279
}
2858629280
@@ -28600,13 +29294,13 @@
2860029294
open_db(&data, 0);
2860129295
echo_group_input(&data, azCmd[i]);
2860229296
rc = shell_exec(&data, azCmd[i], &zErrMsg);
2860329297
if( zErrMsg || rc ){
2860429298
if( zErrMsg!=0 ){
28605
- utf8_printf(stderr,"Error: %s\n", zErrMsg);
29299
+ eputf("Error: %s\n", zErrMsg);
2860629300
}else{
28607
- utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
29301
+ eputf("Error: unable to process SQL: %s\n", azCmd[i]);
2860829302
}
2860929303
sqlite3_free(zErrMsg);
2861029304
free(azCmd);
2861129305
return rc!=0 ? rc : 1;
2861229306
}
@@ -28616,30 +29310,24 @@
2861629310
/* Run commands received from standard input
2861729311
*/
2861829312
if( stdin_is_interactive ){
2861929313
char *zHome;
2862029314
char *zHistory;
28621
- const char *zCharset = "";
2862229315
int nHistory;
28623
-#if SHELL_WIN_UTF8_OPT
28624
- switch( console_utf8_in+2*console_utf8_out ){
28625
- default: case 0: break;
28626
- case 1: zCharset = " (utf8 in)"; break;
28627
- case 2: zCharset = " (utf8 out)"; break;
28628
- case 3: zCharset = " (utf8 I/O)"; break;
28629
- }
29316
+#if CIO_WIN_WC_XLATE
29317
+# define SHELL_CIO_CHAR_SET (stdout_is_console? " (UTF-16 console I/O)" : "")
29318
+#else
29319
+# define SHELL_CIO_CHAR_SET ""
2863029320
#endif
28631
- printf(
28632
- "SQLite version %s %.19s%s\n" /*extra-version-info*/
28633
- "Enter \".help\" for usage hints.\n",
28634
- sqlite3_libversion(), sqlite3_sourceid(), zCharset
28635
- );
29321
+ oputf("SQLite version %s %.19s%s\n" /*extra-version-info*/
29322
+ "Enter \".help\" for usage hints.\n",
29323
+ sqlite3_libversion(), sqlite3_sourceid(), SHELL_CIO_CHAR_SET);
2863629324
if( warnInmemoryDb ){
28637
- printf("Connected to a ");
29325
+ oputz("Connected to a ");
2863829326
printBold("transient in-memory database");
28639
- printf(".\nUse \".open FILENAME\" to reopen on a "
28640
- "persistent database.\n");
29327
+ oputz(".\nUse \".open FILENAME\" to reopen on a"
29328
+ " persistent database.\n");
2864129329
}
2864229330
zHistory = getenv("SQLITE_HISTORY");
2864329331
if( zHistory ){
2864429332
zHistory = strdup(zHistory);
2864529333
}else if( (zHome = find_home_dir(0))!=0 ){
@@ -28695,12 +29383,12 @@
2869529383
/* Clear the global data structure so that valgrind will detect memory
2869629384
** leaks */
2869729385
memset(&data, 0, sizeof(data));
2869829386
#ifdef SQLITE_DEBUG
2869929387
if( sqlite3_memory_used()>mem_main_enter ){
28700
- utf8_printf(stderr, "Memory leaked: %u bytes\n",
28701
- (unsigned int)(sqlite3_memory_used()-mem_main_enter));
29388
+ eputf("Memory leaked: %u bytes\n",
29389
+ (unsigned int)(sqlite3_memory_used()-mem_main_enter));
2870229390
}
2870329391
#endif
2870429392
#endif /* !SQLITE_SHELL_FIDDLE */
2870529393
return rc;
2870629394
}
2870729395
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -250,33 +250,1023 @@
250 #define WIN32_LEAN_AND_MEAN
251 #include <windows.h>
252
253 /* string conversion routines only needed on Win32 */
254 extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
255 extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
256 extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
257 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
258 #endif
259
260 /* On Windows, we normally run with output mode of TEXT so that \n characters
261 ** are automatically translated into \r\n. However, this behavior needs
262 ** to be disabled in some cases (ex: when generating CSV output and when
263 ** rendering quoted strings that contain \n characters). The following
264 ** routines take care of that.
265 */
266 #if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
267 static void setBinaryMode(FILE *file, int isOutput){
268 if( isOutput ) fflush(file);
269 _setmode(_fileno(file), _O_BINARY);
270 }
271 static void setTextMode(FILE *file, int isOutput){
272 if( isOutput ) fflush(file);
273 _setmode(_fileno(file), _O_TEXT);
274 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
275 #else
276 # define setBinaryMode(X,Y)
277 # define setTextMode(X,Y)
 
 
 
 
 
 
278 #endif
279
280 /* True if the timer is enabled */
281 static int enableTimer = 0;
282
@@ -347,14 +1337,14 @@
347 static void endTimer(void){
348 if( enableTimer ){
349 sqlite3_int64 iEnd = timeOfDay();
350 struct rusage sEnd;
351 getrusage(RUSAGE_SELF, &sEnd);
352 printf("Run Time: real %.3f user %f sys %f\n",
353 (iEnd - iBegin)*0.001,
354 timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
355 timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
356 }
357 }
358
359 #define BEGIN_TIMER beginTimer()
360 #define END_TIMER endTimer()
@@ -426,14 +1416,14 @@
426 static void endTimer(void){
427 if( enableTimer && getProcessTimesAddr){
428 FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
429 sqlite3_int64 ftWallEnd = timeOfDay();
430 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
431 printf("Run Time: real %.3f user %f sys %f\n",
432 (ftWallEnd - ftWallBegin)*0.001,
433 timeDiff(&ftUserBegin, &ftUserEnd),
434 timeDiff(&ftKernelBegin, &ftKernelEnd));
435 }
436 }
437
438 #define BEGIN_TIMER beginTimer()
439 #define END_TIMER endTimer()
@@ -466,32 +1456,13 @@
466 ** is true. Otherwise, assume stdin is connected to a file or pipe.
467 */
468 static int stdin_is_interactive = 1;
469
470 /*
471 ** If build is for non-RT Windows, without 3rd-party line editing,
472 ** console input and output may be done in a UTF-8 compatible way,
473 ** if the OS is capable of it and the --no-utf8 option is not seen.
474 */
475 #if (defined(_WIN32) || defined(WIN32)) && SHELL_USE_LOCAL_GETLINE \
476 && !defined(SHELL_OMIT_WIN_UTF8) && !SQLITE_OS_WINRT
477 # define SHELL_WIN_UTF8_OPT 1
478 /* Record whether to do UTF-8 console I/O translation per stream. */
479 static int console_utf8_in = 0;
480 static int console_utf8_out = 0;
481 /* Record whether can do UTF-8 or --no-utf8 seen in invocation. */
482 static int mbcs_opted = 1; /* Assume cannot do until shown otherwise. */
483 #else
484 # define console_utf8_in 0
485 # define console_utf8_out 0
486 # define SHELL_WIN_UTF8_OPT 0
487 #endif
488
489 /*
490 ** On Windows systems we have to know if standard output is a console
491 ** in order to translate UTF-8 into MBCS. The following variable is
492 ** true if translation is required.
493 */
494 static int stdout_is_console = 1;
495
496 /*
497 ** The following is the open SQLite database. We make a pointer
@@ -609,255 +1580,21 @@
609 shell_strncpy(dynPrompt.dynamicPrompt, ")x!", 4);
610 }else{
611 shell_strncpy(dynPrompt.dynamicPrompt, "(x.", 4);
612 dynPrompt.dynamicPrompt[2] = (char)('0'+dynPrompt.inParenLevel);
613 }
614 shell_strncpy(dynPrompt.dynamicPrompt+3, continuePrompt+3, PROMPT_LEN_MAX-4);
 
615 }
616 }
617 return dynPrompt.dynamicPrompt;
618 }
619 #endif /* !defined(SQLITE_OMIT_DYNAPROMPT) */
620
621 #if SHELL_WIN_UTF8_OPT
622 /* Following struct is used for UTF-8 console I/O. */
623 static struct ConsoleState {
624 int stdinEof; /* EOF has been seen on console input */
625 int infsMode; /* Input file stream mode upon shell start */
626 UINT inCodePage; /* Input code page upon shell start */
627 UINT outCodePage; /* Output code page upon shell start */
628 HANDLE hConsole; /* Console input or output handle */
629 DWORD consoleMode; /* Console mode upon shell start */
630 } conState = { 0, 0, 0, 0, INVALID_HANDLE_VALUE, 0 };
631
632 #ifndef _O_U16TEXT /* For build environments lacking this constant: */
633 # define _O_U16TEXT 0x20000
634 #endif
635
636 /*
637 ** If given stream number is a console, return 1 and get some attributes,
638 ** else return 0 and set the output attributes to invalid values.
639 */
640 static short console_attrs(unsigned stnum, HANDLE *pH, DWORD *pConsMode){
641 static int stid[3] = { STD_INPUT_HANDLE,STD_OUTPUT_HANDLE,STD_ERROR_HANDLE };
642 HANDLE h;
643 *pH = INVALID_HANDLE_VALUE;
644 *pConsMode = 0;
645 if( stnum > 2 ) return 0;
646 h = GetStdHandle(stid[stnum]);
647 if( h!=*pH && GetFileType(h)==FILE_TYPE_CHAR && GetConsoleMode(h,pConsMode) ){
648 *pH = h;
649 return 1;
650 }
651 return 0;
652 }
653
654 /*
655 ** Perform a runtime test of Windows console to determine if it can
656 ** do char-stream I/O correctly when the code page is set to CP_UTF8.
657 ** Returns are: 1 => yes it can, 0 => no it cannot
658 **
659 ** The console's output code page is momentarily set, then restored.
660 ** So this should only be run when the process is given use of the
661 ** console for either input or output.
662 */
663 static short ConsoleDoesUTF8(void){
664 UINT ocp = GetConsoleOutputCP();
665 const char TrialUtf8[] = { '\xC8', '\xAB' }; /* "ȫ" or 2 MBCS characters */
666 WCHAR aReadBack[1] = { 0 }; /* Read back as 0x022B when decoded as UTF-8. */
667 CONSOLE_SCREEN_BUFFER_INFO csbInfo = {0};
668 /* Create an inactive screen buffer with which to do the experiment. */
669 HANDLE hCSB = CreateConsoleScreenBuffer(GENERIC_READ|GENERIC_WRITE, 0, 0,
670 CONSOLE_TEXTMODE_BUFFER, NULL);
671 if( hCSB!=INVALID_HANDLE_VALUE ){
672 COORD cpos = {0,0};
673 DWORD rbc;
674 SetConsoleCursorPosition(hCSB, cpos);
675 SetConsoleOutputCP(CP_UTF8);
676 /* Write 2 chars which are a single character in UTF-8 but more in MBCS. */
677 WriteConsoleA(hCSB, TrialUtf8, sizeof(TrialUtf8), NULL, NULL);
678 ReadConsoleOutputCharacterW(hCSB, &aReadBack[0], 1, cpos, &rbc);
679 GetConsoleScreenBufferInfo(hCSB, &csbInfo);
680 SetConsoleOutputCP(ocp);
681 CloseHandle(hCSB);
682 }
683 /* Return 1 if cursor advanced by 1 position, else 0. */
684 return (short)(csbInfo.dwCursorPosition.X == 1 && aReadBack[0] == 0x022B);
685 }
686
687 static short in_console = 0;
688 static short out_console = 0;
689
690 /*
691 ** Determine whether either normal I/O stream is the console,
692 ** and whether it can do UTF-8 translation, setting globals
693 ** in_console, out_console and mbcs_opted accordingly.
694 */
695 static void probe_console(void){
696 HANDLE h;
697 DWORD cMode;
698 in_console = console_attrs(0, &h, &cMode);
699 out_console = console_attrs(1, &h, &cMode);
700 if( in_console || out_console ) mbcs_opted = !ConsoleDoesUTF8();
701 }
702
703 /*
704 ** If console is used for normal I/O, absent a --no-utf8 option,
705 ** prepare console for UTF-8 input (from either typing or suitable
706 ** paste operations) and/or for UTF-8 output rendering.
707 **
708 ** The console state upon entry is preserved, in conState, so that
709 ** console_restore() can later restore the same console state.
710 **
711 ** The globals console_utf8_in and console_utf8_out are set, for
712 ** later use in selecting UTF-8 or MBCS console I/O translations.
713 ** This routine depends upon globals set by probe_console().
714 */
715 static void console_prepare_utf8(void){
716 struct ConsoleState csWork = { 0, 0, 0, 0, INVALID_HANDLE_VALUE, 0 };
717
718 console_utf8_in = console_utf8_out = 0;
719 if( (!in_console && !out_console) || mbcs_opted ) return;
720 console_attrs((in_console)? 0 : 1, &conState.hConsole, &conState.consoleMode);
721 conState.inCodePage = GetConsoleCP();
722 conState.outCodePage = GetConsoleOutputCP();
723 if( in_console ){
724 SetConsoleCP(CP_UTF8);
725 DWORD newConsoleMode = conState.consoleMode
726 | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT;
727 SetConsoleMode(conState.hConsole, newConsoleMode);
728 conState.infsMode = _setmode(_fileno(stdin), _O_U16TEXT);
729 console_utf8_in = 1;
730 }
731 if( out_console ){
732 SetConsoleOutputCP(CP_UTF8);
733 console_utf8_out = 1;
734 }
735 }
736
737 /*
738 ** Undo the effects of console_prepare_utf8(), if any.
739 */
740 static void SQLITE_CDECL console_restore(void){
741 if( (console_utf8_in||console_utf8_out)
742 && conState.hConsole!=INVALID_HANDLE_VALUE ){
743 if( console_utf8_in ){
744 SetConsoleCP(conState.inCodePage);
745 _setmode(_fileno(stdin), conState.infsMode);
746 }
747 if( console_utf8_out ) SetConsoleOutputCP(conState.outCodePage);
748 SetConsoleMode(conState.hConsole, conState.consoleMode);
749 /* Avoid multiple calls. */
750 conState.hConsole = INVALID_HANDLE_VALUE;
751 conState.consoleMode = 0;
752 console_utf8_in = 0;
753 console_utf8_out = 0;
754 }
755 }
756
757 /*
758 ** Collect input like fgets(...) with special provisions for input
759 ** from the Windows console to get around its strange coding issues.
760 ** Defers to plain fgets() when input is not interactive or when the
761 ** UTF-8 input is unavailable or opted out.
762 */
763 static char* utf8_fgets(char *buf, int ncmax, FILE *fin){
764 if( fin==0 ) fin = stdin;
765 if( fin==stdin && stdin_is_interactive && console_utf8_in ){
766 # define SQLITE_IALIM 150
767 wchar_t wbuf[SQLITE_IALIM];
768 int lend = 0;
769 int noc = 0;
770 if( ncmax==0 || conState.stdinEof ) return 0;
771 buf[0] = 0;
772 while( noc<ncmax-7-1 && !lend ){
773 /* There is room for at least 2 more characters and a 0-terminator. */
774 int na = (ncmax > SQLITE_IALIM*4+1 + noc)
775 ? SQLITE_IALIM : (ncmax-1 - noc)/4;
776 # undef SQLITE_IALIM
777 DWORD nbr = 0;
778 BOOL bRC = ReadConsoleW(conState.hConsole, wbuf, na, &nbr, 0);
779 if( !bRC || (noc==0 && nbr==0) ) return 0;
780 if( nbr > 0 ){
781 int nmb = WideCharToMultiByte(CP_UTF8,WC_COMPOSITECHECK|WC_DEFAULTCHAR,
782 wbuf,nbr,0,0,0,0);
783 if( nmb !=0 && noc+nmb <= ncmax ){
784 int iseg = noc;
785 nmb = WideCharToMultiByte(CP_UTF8,WC_COMPOSITECHECK|WC_DEFAULTCHAR,
786 wbuf,nbr,buf+noc,nmb,0,0);
787 noc += nmb;
788 /* Fixup line-ends as coded by Windows for CR (or "Enter".)*/
789 if( noc > 0 ){
790 if( buf[noc-1]=='\n' ){
791 lend = 1;
792 if( noc > 1 && buf[noc-2]=='\r' ){
793 buf[noc-2] = '\n';
794 --noc;
795 }
796 }
797 }
798 /* Check for ^Z (anywhere in line) too. */
799 while( iseg < noc ){
800 if( buf[iseg]==0x1a ){
801 conState.stdinEof = 1;
802 noc = iseg; /* Chop ^Z and anything following. */
803 break;
804 }
805 ++iseg;
806 }
807 }else break; /* Drop apparent garbage in. (Could assert.) */
808 }else break;
809 }
810 /* If got nothing, (after ^Z chop), must be at end-of-file. */
811 if( noc == 0 ) return 0;
812 buf[noc] = 0;
813 return buf;
814 }else{
815 return fgets(buf, ncmax, fin);
816 }
817 }
818
819 # define fgets(b,n,f) utf8_fgets(b,n,f)
820 #endif /* SHELL_WIN_UTF8_OPT */
821
822 /*
823 ** Render output like fprintf(). Except, if the output is going to the
824 ** console and if this is running on a Windows machine, and if UTF-8
825 ** output unavailable (or available but opted out), translate the
826 ** output from UTF-8 into MBCS for output through 8-bit stdout stream.
827 ** (Without -no-utf8, no translation is needed and must not be done.)
828 */
829 #if defined(_WIN32) || defined(WIN32)
830 void utf8_printf(FILE *out, const char *zFormat, ...){
831 va_list ap;
832 va_start(ap, zFormat);
833 if( stdout_is_console && (out==stdout || out==stderr) && !console_utf8_out ){
834 char *z1 = sqlite3_vmprintf(zFormat, ap);
835 char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
836 sqlite3_free(z1);
837 fputs(z2, out);
838 sqlite3_free(z2);
839 }else{
840 vfprintf(out, zFormat, ap);
841 }
842 va_end(ap);
843 }
844 #elif !defined(utf8_printf)
845 # define utf8_printf fprintf
846 #endif
847
848 /*
849 ** Render output like fprintf(). This should not be used on anything that
850 ** includes string formatting (e.g. "%s").
851 */
852 #if !defined(raw_printf)
853 # define raw_printf fprintf
854 #endif
855
856 /* Indicate out-of-memory and exit. */
857 static void shell_out_of_memory(void){
858 raw_printf(stderr,"Error: out of memory\n");
859 exit(1);
860 }
861
862 /* Check a pointer to see if it is NULL. If it is NULL, exit with an
863 ** out-of-memory error.
@@ -885,22 +1622,22 @@
885 char *z;
886 if( iotrace==0 ) return;
887 va_start(ap, zFormat);
888 z = sqlite3_vmprintf(zFormat, ap);
889 va_end(ap);
890 utf8_printf(iotrace, "%s", z);
891 sqlite3_free(z);
892 }
893 #endif
894
895 /*
896 ** Output string zUtf to stream pOut as w characters. If w is negative,
897 ** then right-justify the text. W is the width in UTF-8 characters, not
898 ** in bytes. This is different from the %*.*s specification in printf
899 ** since with %*.*s the width is measured in bytes, not characters.
900 */
901 static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
902 int i;
903 int n;
904 int aw = w<0 ? -w : w;
905 if( zUtf==0 ) zUtf = "";
906 for(i=n=0; zUtf[i]; i++){
@@ -911,15 +1648,15 @@
911 break;
912 }
913 }
914 }
915 if( n>=aw ){
916 utf8_printf(pOut, "%.*s", i, zUtf);
917 }else if( w<0 ){
918 utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
919 }else{
920 utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
921 }
922 }
923
924
925 /*
@@ -975,11 +1712,11 @@
975 ** Return open FILE * if zFile exists, can be opened for read
976 ** and is an ordinary file or a character stream source.
977 ** Otherwise return 0.
978 */
979 static FILE * openChrSource(const char *zFile){
980 #ifdef _WIN32
981 struct _stat x = {0};
982 # define STAT_CHR_SRC(mode) ((mode & (_S_IFCHR|_S_IFIFO|_S_IFREG))!=0)
983 /* On Windows, open first, then check the stream nature. This order
984 ** is necessary because _stat() and sibs, when checking a named pipe,
985 ** effectively break the pipe as its supplier sees it. */
@@ -1038,27 +1775,10 @@
1038 if( n>0 && zLine[n-1]=='\r' ) n--;
1039 zLine[n] = 0;
1040 break;
1041 }
1042 }
1043 #if defined(_WIN32) || defined(WIN32)
1044 /* For interactive input on Windows systems, with -no-utf8,
1045 ** translate the multi-byte characterset characters into UTF-8.
1046 ** This is the translation that predates console UTF-8 input. */
1047 if( stdin_is_interactive && in==stdin && !console_utf8_in ){
1048 char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
1049 if( zTrans ){
1050 i64 nTrans = strlen(zTrans)+1;
1051 if( nTrans>nLine ){
1052 zLine = realloc(zLine, nTrans);
1053 shell_check_oom(zLine);
1054 }
1055 memcpy(zLine, zTrans, nTrans);
1056 sqlite3_free(zTrans);
1057 }
1058 }
1059 #endif /* defined(_WIN32) || defined(WIN32) */
1060 return zLine;
1061 }
1062
1063 /*
1064 ** Retrieve a single line of input text.
@@ -1081,11 +1801,11 @@
1081 if( in!=0 ){
1082 zResult = local_getline(zPrior, in);
1083 }else{
1084 zPrompt = isContinuation ? CONTINUATION_PROMPT : mainPrompt;
1085 #if SHELL_USE_LOCAL_GETLINE
1086 printf("%s", zPrompt);
1087 fflush(stdout);
1088 do{
1089 zResult = local_getline(zPrior, stdin);
1090 zPrior = 0;
1091 /* ^C trap creates a false EOF, so let "interrupt" thread catch up. */
@@ -1328,11 +2048,11 @@
1328 double r = sqlite3_value_double(apVal[0]);
1329 int n = nVal>=2 ? sqlite3_value_int(apVal[1]) : 26;
1330 char z[400];
1331 if( n<1 ) n = 1;
1332 if( n>350 ) n = 350;
1333 snprintf(z, sizeof(z)-1, "%#+.*e", n, r);
1334 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
1335 }
1336
1337
1338 /*
@@ -17614,11 +18334,11 @@
17614 ** A callback for the sqlite3_log() interface.
17615 */
17616 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
17617 ShellState *p = (ShellState*)pArg;
17618 if( p->pLog==0 ) return;
17619 utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
17620 fflush(p->pLog);
17621 }
17622
17623 /*
17624 ** SQL function: shell_putsnl(X)
@@ -17629,13 +18349,13 @@
17629 static void shellPutsFunc(
17630 sqlite3_context *pCtx,
17631 int nVal,
17632 sqlite3_value **apVal
17633 ){
17634 ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
17635 (void)nVal;
17636 utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
17637 sqlite3_result_value(pCtx, apVal[0]);
17638 }
17639
17640 /*
17641 ** If in safe mode, print an error message described by the arguments
@@ -17650,12 +18370,11 @@
17650 va_list ap;
17651 char *zMsg;
17652 va_start(ap, zErrMsg);
17653 zMsg = sqlite3_vmprintf(zErrMsg, ap);
17654 va_end(ap);
17655 raw_printf(stderr, "line %d: ", p->lineno);
17656 utf8_printf(stderr, "%s\n", zMsg);
17657 exit(1);
17658 }
17659 }
17660
17661 /*
@@ -17819,11 +18538,11 @@
17819 }
17820
17821 /*
17822 ** Output the given string as a hex-encoded blob (eg. X'1234' )
17823 */
17824 static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
17825 int i;
17826 unsigned char *aBlob = (unsigned char*)pBlob;
17827
17828 char *zStr = sqlite3_malloc(nBlob*2 + 1);
17829 shell_check_oom(zStr);
@@ -17836,11 +18555,11 @@
17836 zStr[i*2] = aHex[ (aBlob[i] >> 4) ];
17837 zStr[i*2+1] = aHex[ (aBlob[i] & 0x0F) ];
17838 }
17839 zStr[i*2] = '\0';
17840
17841 raw_printf(out,"X'%s'", zStr);
17842 sqlite3_free(zStr);
17843 }
17844
17845 /*
17846 ** Find a string that is not found anywhere in z[]. Return a pointer
@@ -17866,39 +18585,46 @@
17866 /*
17867 ** Output the given string as a quoted string using SQL quoting conventions.
17868 **
17869 ** See also: output_quoted_escaped_string()
17870 */
17871 static void output_quoted_string(FILE *out, const char *z){
17872 int i;
17873 char c;
17874 setBinaryMode(out, 1);
 
 
 
17875 if( z==0 ) return;
17876 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
17877 if( c==0 ){
17878 utf8_printf(out,"'%s'",z);
17879 }else{
17880 raw_printf(out, "'");
17881 while( *z ){
17882 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
17883 if( c=='\'' ) i++;
17884 if( i ){
17885 utf8_printf(out, "%.*s", i, z);
17886 z += i;
17887 }
17888 if( c=='\'' ){
17889 raw_printf(out, "'");
17890 continue;
17891 }
17892 if( c==0 ){
17893 break;
17894 }
17895 z++;
17896 }
17897 raw_printf(out, "'");
17898 }
17899 setTextMode(out, 1);
 
 
 
 
17900 }
17901
17902 /*
17903 ** Output the given string as a quoted string using SQL quoting conventions.
17904 ** Additionallly , escape the "\n" and "\r" characters so that they do not
@@ -17906,17 +18632,20 @@
17906 ** systems.
17907 **
17908 ** This is like output_quoted_string() but with the addition of the \r\n
17909 ** escape mechanism.
17910 */
17911 static void output_quoted_escaped_string(FILE *out, const char *z){
17912 int i;
17913 char c;
17914 setBinaryMode(out, 1);
 
 
 
17915 for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
17916 if( c==0 ){
17917 utf8_printf(out,"'%s'",z);
17918 }else{
17919 const char *zNL = 0;
17920 const char *zCR = 0;
17921 int nNL = 0;
17922 int nCR = 0;
@@ -17924,121 +18653,167 @@
17924 for(i=0; z[i]; i++){
17925 if( z[i]=='\n' ) nNL++;
17926 if( z[i]=='\r' ) nCR++;
17927 }
17928 if( nNL ){
17929 raw_printf(out, "replace(");
17930 zNL = unused_string(z, "\\n", "\\012", zBuf1);
17931 }
17932 if( nCR ){
17933 raw_printf(out, "replace(");
17934 zCR = unused_string(z, "\\r", "\\015", zBuf2);
17935 }
17936 raw_printf(out, "'");
17937 while( *z ){
17938 for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
17939 if( c=='\'' ) i++;
17940 if( i ){
17941 utf8_printf(out, "%.*s", i, z);
17942 z += i;
17943 }
17944 if( c=='\'' ){
17945 raw_printf(out, "'");
17946 continue;
17947 }
17948 if( c==0 ){
17949 break;
17950 }
17951 z++;
17952 if( c=='\n' ){
17953 raw_printf(out, "%s", zNL);
17954 continue;
17955 }
17956 raw_printf(out, "%s", zCR);
17957 }
17958 raw_printf(out, "'");
17959 if( nCR ){
17960 raw_printf(out, ",'%s',char(13))", zCR);
17961 }
17962 if( nNL ){
17963 raw_printf(out, ",'%s',char(10))", zNL);
17964 }
17965 }
17966 setTextMode(out, 1);
 
 
 
 
17967 }
17968
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17969 /*
17970 ** Output the given string as a quoted according to C or TCL quoting rules.
17971 */
17972 static void output_c_string(FILE *out, const char *z){
17973 unsigned int c;
17974 fputc('"', out);
17975 while( (c = *(z++))!=0 ){
17976 if( c=='\\' ){
17977 fputc(c, out);
17978 fputc(c, out);
17979 }else if( c=='"' ){
17980 fputc('\\', out);
17981 fputc('"', out);
17982 }else if( c=='\t' ){
17983 fputc('\\', out);
17984 fputc('t', out);
17985 }else if( c=='\n' ){
17986 fputc('\\', out);
17987 fputc('n', out);
17988 }else if( c=='\r' ){
17989 fputc('\\', out);
17990 fputc('r', out);
 
 
 
 
 
 
 
 
 
17991 }else if( !isprint(c&0xff) ){
17992 raw_printf(out, "\\%03o", c&0xff);
17993 }else{
17994 fputc(c, out);
 
17995 }
 
17996 }
17997 fputc('"', out);
17998 }
17999
18000 /*
18001 ** Output the given string as a quoted according to JSON quoting rules.
18002 */
18003 static void output_json_string(FILE *out, const char *z, i64 n){
18004 unsigned int c;
 
 
 
 
 
 
 
18005 if( z==0 ) z = "";
18006 if( n<0 ) n = strlen(z);
18007 fputc('"', out);
18008 while( n-- ){
 
 
 
 
 
 
 
 
18009 c = *(z++);
18010 if( c=='\\' || c=='"' ){
18011 fputc('\\', out);
18012 fputc(c, out);
 
 
 
 
 
 
 
 
 
 
 
18013 }else if( c<=0x1f ){
18014 fputc('\\', out);
18015 if( c=='\b' ){
18016 fputc('b', out);
18017 }else if( c=='\f' ){
18018 fputc('f', out);
18019 }else if( c=='\n' ){
18020 fputc('n', out);
18021 }else if( c=='\r' ){
18022 fputc('r', out);
18023 }else if( c=='\t' ){
18024 fputc('t', out);
18025 }else{
18026 raw_printf(out, "u%04x",c);
18027 }
18028 }else{
18029 fputc(c, out);
18030 }
18031 }
18032 fputc('"', out);
18033 }
18034
18035 /*
18036 ** Output the given string with characters that are special to
18037 ** HTML escaped.
18038 */
18039 static void output_html_string(FILE *out, const char *z){
18040 int i;
18041 if( z==0 ) z = "";
18042 while( *z ){
18043 for(i=0; z[i]
18044 && z[i]!='<'
@@ -18046,22 +18821,22 @@
18046 && z[i]!='>'
18047 && z[i]!='\"'
18048 && z[i]!='\'';
18049 i++){}
18050 if( i>0 ){
18051 utf8_printf(out,"%.*s",i,z);
18052 }
18053 if( z[i]=='<' ){
18054 raw_printf(out,"&lt;");
18055 }else if( z[i]=='&' ){
18056 raw_printf(out,"&amp;");
18057 }else if( z[i]=='>' ){
18058 raw_printf(out,"&gt;");
18059 }else if( z[i]=='\"' ){
18060 raw_printf(out,"&quot;");
18061 }else if( z[i]=='\'' ){
18062 raw_printf(out,"&#39;");
18063 }else{
18064 break;
18065 }
18066 z += i + 1;
18067 }
@@ -18095,13 +18870,12 @@
18095 ** the separator, which may or may not be a comma. p->nullValue is
18096 ** the null value. Strings are quoted if necessary. The separator
18097 ** is only issued if bSep is true.
18098 */
18099 static void output_csv(ShellState *p, const char *z, int bSep){
18100 FILE *out = p->out;
18101 if( z==0 ){
18102 utf8_printf(out,"%s",p->nullValue);
18103 }else{
18104 unsigned i;
18105 for(i=0; z[i]; i++){
18106 if( needCsvQuote[((unsigned char*)z)[i]] ){
18107 i = 0;
@@ -18109,18 +18883,18 @@
18109 }
18110 }
18111 if( i==0 || strstr(z, p->colSeparator)!=0 ){
18112 char *zQuoted = sqlite3_mprintf("\"%w\"", z);
18113 shell_check_oom(zQuoted);
18114 utf8_printf(out, "%s", zQuoted);
18115 sqlite3_free(zQuoted);
18116 }else{
18117 utf8_printf(out, "%s", z);
18118 }
18119 }
18120 if( bSep ){
18121 utf8_printf(p->out, "%s", p->colSeparator);
18122 }
18123 }
18124
18125 /*
18126 ** This routine runs when the user presses Ctrl-C
@@ -18224,20 +18998,20 @@
18224 const char *az[4];
18225 az[0] = zA1;
18226 az[1] = zA2;
18227 az[2] = zA3;
18228 az[3] = zA4;
18229 utf8_printf(p->out, "authorizer: %s", azAction[op]);
18230 for(i=0; i<4; i++){
18231 raw_printf(p->out, " ");
18232 if( az[i] ){
18233 output_c_string(p->out, az[i]);
18234 }else{
18235 raw_printf(p->out, "NULL");
18236 }
18237 }
18238 raw_printf(p->out, "\n");
18239 if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4);
18240 return SQLITE_OK;
18241 }
18242 #endif
18243
@@ -18249,11 +19023,11 @@
18249 **
18250 ** If the schema statement in z[] contains a start-of-comment and if
18251 ** sqlite3_complete() returns false, try to terminate the comment before
18252 ** printing the result. https://sqlite.org/forum/forumpost/d7be961c5c
18253 */
18254 static void printSchemaLine(FILE *out, const char *z, const char *zTail){
18255 char *zToFree = 0;
18256 if( z==0 ) return;
18257 if( zTail==0 ) return;
18258 if( zTail[0]==';' && (strstr(z, "/*")!=0 || strstr(z,"--")!=0) ){
18259 const char *zOrig = z;
@@ -18271,20 +19045,20 @@
18271 }
18272 sqlite3_free(zNew);
18273 }
18274 }
18275 if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
18276 utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
18277 }else{
18278 utf8_printf(out, "%s%s", z, zTail);
18279 }
18280 sqlite3_free(zToFree);
18281 }
18282 static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
18283 char c = z[n];
18284 z[n] = 0;
18285 printSchemaLine(out, z, zTail);
18286 z[n] = c;
18287 }
18288
18289 /*
18290 ** Return true if string z[] has nothing but whitespace and comments to the
@@ -18308,11 +19082,11 @@
18308 EQPGraphRow *pNew;
18309 i64 nText;
18310 if( zText==0 ) return;
18311 nText = strlen(zText);
18312 if( p->autoEQPtest ){
18313 utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
18314 }
18315 pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
18316 shell_check_oom(pNew);
18317 pNew->iEqpId = iEqpId;
18318 pNew->iParentId = p2;
@@ -18356,12 +19130,11 @@
18356 i64 n = strlen(p->sGraph.zPrefix);
18357 char *z;
18358 for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
18359 pNext = eqp_next_row(p, iEqpId, pRow);
18360 z = pRow->zText;
18361 utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
18362 pNext ? "|--" : "`--", z);
18363 if( n<(i64)sizeof(p->sGraph.zPrefix)-7 ){
18364 memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4);
18365 eqp_render_level(p, pRow->iEqpId);
18366 p->sGraph.zPrefix[n] = 0;
18367 }
@@ -18377,17 +19150,17 @@
18377 if( pRow->zText[0]=='-' ){
18378 if( pRow->pNext==0 ){
18379 eqp_reset(p);
18380 return;
18381 }
18382 utf8_printf(p->out, "%s\n", pRow->zText+3);
18383 p->sGraph.pRow = pRow->pNext;
18384 sqlite3_free(pRow);
18385 }else if( nCycle>0 ){
18386 utf8_printf(p->out, "QUERY PLAN (cycles=%lld [100%%])\n", nCycle);
18387 }else{
18388 utf8_printf(p->out, "QUERY PLAN\n");
18389 }
18390 p->sGraph.zPrefix[0] = 0;
18391 eqp_render_level(p, 0);
18392 eqp_reset(p);
18393 }
@@ -18399,33 +19172,33 @@
18399 */
18400 static int progress_handler(void *pClientData) {
18401 ShellState *p = (ShellState*)pClientData;
18402 p->nProgress++;
18403 if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
18404 raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
18405 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
18406 if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
18407 return 1;
18408 }
18409 if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
18410 raw_printf(p->out, "Progress %u\n", p->nProgress);
18411 }
18412 return 0;
18413 }
18414 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
18415
18416 /*
18417 ** Print N dashes
18418 */
18419 static void print_dashes(FILE *out, int N){
18420 const char zDash[] = "--------------------------------------------------";
18421 const int nDash = sizeof(zDash) - 1;
18422 while( N>nDash ){
18423 fputs(zDash, out);
18424 N -= nDash;
18425 }
18426 raw_printf(out, "%.*s", N, zDash);
18427 }
18428
18429 /*
18430 ** Print a markdown or table-style row separator using ascii-art
18431 */
@@ -18434,19 +19207,19 @@
18434 int nArg,
18435 const char *zSep
18436 ){
18437 int i;
18438 if( nArg>0 ){
18439 fputs(zSep, p->out);
18440 print_dashes(p->out, p->actualWidth[0]+2);
18441 for(i=1; i<nArg; i++){
18442 fputs(zSep, p->out);
18443 print_dashes(p->out, p->actualWidth[i]+2);
18444 }
18445 fputs(zSep, p->out);
18446 }
18447 fputs("\n", p->out);
18448 }
18449
18450 /*
18451 ** This is the callback routine that the shell
18452 ** invokes for each row of a query result.
@@ -18472,14 +19245,14 @@
18472 if( azArg==0 ) break;
18473 for(i=0; i<nArg; i++){
18474 int len = strlen30(azCol[i] ? azCol[i] : "");
18475 if( len>w ) w = len;
18476 }
18477 if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
18478 for(i=0; i<nArg; i++){
18479 utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
18480 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
18481 }
18482 break;
18483 }
18484 case MODE_ScanExp:
18485 case MODE_Explain: {
@@ -18502,16 +19275,16 @@
18502 if( nArg>nWidth ) nArg = nWidth;
18503
18504 /* If this is the first row seen, print out the headers */
18505 if( p->cnt++==0 ){
18506 for(i=0; i<nArg; i++){
18507 utf8_width_print(p->out, aWidth[i], azCol[ aMap[i] ]);
18508 fputs(i==nArg-1 ? "\n" : " ", p->out);
18509 }
18510 for(i=0; i<nArg; i++){
18511 print_dashes(p->out, aWidth[i]);
18512 fputs(i==nArg-1 ? "\n" : " ", p->out);
18513 }
18514 }
18515
18516 /* If there is no data, exit early. */
18517 if( azArg==0 ) break;
@@ -18525,21 +19298,21 @@
18525 w = strlenChar(zVal);
18526 zSep = " ";
18527 }
18528 if( i==iIndent && p->aiIndent && p->pStmt ){
18529 if( p->iIndent<p->nIndent ){
18530 utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
18531 }
18532 p->iIndent++;
18533 }
18534 utf8_width_print(p->out, w, zVal ? zVal : p->nullValue);
18535 fputs(i==nArg-1 ? "\n" : zSep, p->out);
18536 }
18537 break;
18538 }
18539 case MODE_Semi: { /* .schema and .fullschema output */
18540 printSchemaLine(p->out, azArg[0], ";\n");
18541 break;
18542 }
18543 case MODE_Pretty: { /* .schema and .fullschema with --indent */
18544 char *z;
18545 int j;
@@ -18550,11 +19323,11 @@
18550 assert( nArg==1 );
18551 if( azArg[0]==0 ) break;
18552 if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
18553 || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
18554 ){
18555 utf8_printf(p->out, "%s;\n", azArg[0]);
18556 break;
18557 }
18558 z = sqlite3_mprintf("%s", azArg[0]);
18559 shell_check_oom(z);
18560 j = 0;
@@ -18583,166 +19356,161 @@
18583 }else if( c=='(' ){
18584 nParen++;
18585 }else if( c==')' ){
18586 nParen--;
18587 if( nLine>0 && nParen==0 && j>0 ){
18588 printSchemaLineN(p->out, z, j, "\n");
18589 j = 0;
18590 }
18591 }
18592 z[j++] = c;
18593 if( nParen==1 && cEnd==0
18594 && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
18595 ){
18596 if( c=='\n' ) j--;
18597 printSchemaLineN(p->out, z, j, "\n ");
18598 j = 0;
18599 nLine++;
18600 while( IsSpace(z[i+1]) ){ i++; }
18601 }
18602 }
18603 z[j] = 0;
18604 }
18605 printSchemaLine(p->out, z, ";\n");
18606 sqlite3_free(z);
18607 break;
18608 }
18609 case MODE_List: {
18610 if( p->cnt++==0 && p->showHeader ){
18611 for(i=0; i<nArg; i++){
18612 utf8_printf(p->out,"%s%s",azCol[i],
18613 i==nArg-1 ? p->rowSeparator : p->colSeparator);
18614 }
18615 }
18616 if( azArg==0 ) break;
18617 for(i=0; i<nArg; i++){
18618 char *z = azArg[i];
18619 if( z==0 ) z = p->nullValue;
18620 utf8_printf(p->out, "%s", z);
18621 if( i<nArg-1 ){
18622 utf8_printf(p->out, "%s", p->colSeparator);
18623 }else{
18624 utf8_printf(p->out, "%s", p->rowSeparator);
18625 }
18626 }
18627 break;
18628 }
18629 case MODE_Html: {
18630 if( p->cnt++==0 && p->showHeader ){
18631 raw_printf(p->out,"<TR>");
18632 for(i=0; i<nArg; i++){
18633 raw_printf(p->out,"<TH>");
18634 output_html_string(p->out, azCol[i]);
18635 raw_printf(p->out,"</TH>\n");
18636 }
18637 raw_printf(p->out,"</TR>\n");
18638 }
18639 if( azArg==0 ) break;
18640 raw_printf(p->out,"<TR>");
18641 for(i=0; i<nArg; i++){
18642 raw_printf(p->out,"<TD>");
18643 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
18644 raw_printf(p->out,"</TD>\n");
18645 }
18646 raw_printf(p->out,"</TR>\n");
18647 break;
18648 }
18649 case MODE_Tcl: {
18650 if( p->cnt++==0 && p->showHeader ){
18651 for(i=0; i<nArg; i++){
18652 output_c_string(p->out,azCol[i] ? azCol[i] : "");
18653 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
18654 }
18655 utf8_printf(p->out, "%s", p->rowSeparator);
18656 }
18657 if( azArg==0 ) break;
18658 for(i=0; i<nArg; i++){
18659 output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
18660 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
18661 }
18662 utf8_printf(p->out, "%s", p->rowSeparator);
18663 break;
18664 }
18665 case MODE_Csv: {
18666 setBinaryMode(p->out, 1);
18667 if( p->cnt++==0 && p->showHeader ){
18668 for(i=0; i<nArg; i++){
18669 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
18670 }
18671 utf8_printf(p->out, "%s", p->rowSeparator);
18672 }
18673 if( nArg>0 ){
18674 for(i=0; i<nArg; i++){
18675 output_csv(p, azArg[i], i<nArg-1);
18676 }
18677 utf8_printf(p->out, "%s", p->rowSeparator);
18678 }
18679 setTextMode(p->out, 1);
18680 break;
18681 }
18682 case MODE_Insert: {
18683 if( azArg==0 ) break;
18684 utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
18685 if( p->showHeader ){
18686 raw_printf(p->out,"(");
18687 for(i=0; i<nArg; i++){
18688 if( i>0 ) raw_printf(p->out, ",");
18689 if( quoteChar(azCol[i]) ){
18690 char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
18691 shell_check_oom(z);
18692 utf8_printf(p->out, "%s", z);
18693 sqlite3_free(z);
18694 }else{
18695 raw_printf(p->out, "%s", azCol[i]);
18696 }
18697 }
18698 raw_printf(p->out,")");
18699 }
18700 p->cnt++;
18701 for(i=0; i<nArg; i++){
18702 raw_printf(p->out, i>0 ? "," : " VALUES(");
18703 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
18704 utf8_printf(p->out,"NULL");
18705 }else if( aiType && aiType[i]==SQLITE_TEXT ){
18706 if( ShellHasFlag(p, SHFLG_Newlines) ){
18707 output_quoted_string(p->out, azArg[i]);
18708 }else{
18709 output_quoted_escaped_string(p->out, azArg[i]);
18710 }
18711 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
18712 utf8_printf(p->out,"%s", azArg[i]);
18713 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
18714 char z[50];
18715 double r = sqlite3_column_double(p->pStmt, i);
18716 sqlite3_uint64 ur;
18717 memcpy(&ur,&r,sizeof(r));
18718 if( ur==0x7ff0000000000000LL ){
18719 raw_printf(p->out, "9.0e+999");
18720 }else if( ur==0xfff0000000000000LL ){
18721 raw_printf(p->out, "-9.0e+999");
18722 }else{
18723 sqlite3_int64 ir = (sqlite3_int64)r;
18724 if( r==(double)ir ){
18725 sqlite3_snprintf(50,z,"%lld.0", ir);
18726 }else{
18727 sqlite3_snprintf(50,z,"%!.20g", r);
18728 }
18729 raw_printf(p->out, "%s", z);
18730 }
18731 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
18732 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
18733 int nBlob = sqlite3_column_bytes(p->pStmt, i);
18734 output_hex_blob(p->out, pBlob, nBlob);
18735 }else if( isNumber(azArg[i], 0) ){
18736 utf8_printf(p->out,"%s", azArg[i]);
18737 }else if( ShellHasFlag(p, SHFLG_Newlines) ){
18738 output_quoted_string(p->out, azArg[i]);
18739 }else{
18740 output_quoted_escaped_string(p->out, azArg[i]);
18741 }
18742 }
18743 raw_printf(p->out,");\n");
18744 break;
18745 }
18746 case MODE_Json: {
18747 if( azArg==0 ) break;
18748 if( p->cnt==0 ){
@@ -18750,93 +19518,93 @@
18750 }else{
18751 fputs(",\n{", p->out);
18752 }
18753 p->cnt++;
18754 for(i=0; i<nArg; i++){
18755 output_json_string(p->out, azCol[i], -1);
18756 putc(':', p->out);
18757 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
18758 fputs("null",p->out);
18759 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
18760 char z[50];
18761 double r = sqlite3_column_double(p->pStmt, i);
18762 sqlite3_uint64 ur;
18763 memcpy(&ur,&r,sizeof(r));
18764 if( ur==0x7ff0000000000000LL ){
18765 raw_printf(p->out, "9.0e+999");
18766 }else if( ur==0xfff0000000000000LL ){
18767 raw_printf(p->out, "-9.0e+999");
18768 }else{
18769 sqlite3_snprintf(50,z,"%!.20g", r);
18770 raw_printf(p->out, "%s", z);
18771 }
18772 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
18773 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
18774 int nBlob = sqlite3_column_bytes(p->pStmt, i);
18775 output_json_string(p->out, pBlob, nBlob);
18776 }else if( aiType && aiType[i]==SQLITE_TEXT ){
18777 output_json_string(p->out, azArg[i], -1);
18778 }else{
18779 utf8_printf(p->out,"%s", azArg[i]);
18780 }
18781 if( i<nArg-1 ){
18782 putc(',', p->out);
18783 }
18784 }
18785 putc('}', p->out);
18786 break;
18787 }
18788 case MODE_Quote: {
18789 if( azArg==0 ) break;
18790 if( p->cnt==0 && p->showHeader ){
18791 for(i=0; i<nArg; i++){
18792 if( i>0 ) fputs(p->colSeparator, p->out);
18793 output_quoted_string(p->out, azCol[i]);
18794 }
18795 fputs(p->rowSeparator, p->out);
18796 }
18797 p->cnt++;
18798 for(i=0; i<nArg; i++){
18799 if( i>0 ) fputs(p->colSeparator, p->out);
18800 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
18801 utf8_printf(p->out,"NULL");
18802 }else if( aiType && aiType[i]==SQLITE_TEXT ){
18803 output_quoted_string(p->out, azArg[i]);
18804 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
18805 utf8_printf(p->out,"%s", azArg[i]);
18806 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
18807 char z[50];
18808 double r = sqlite3_column_double(p->pStmt, i);
18809 sqlite3_snprintf(50,z,"%!.20g", r);
18810 raw_printf(p->out, "%s", z);
18811 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
18812 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
18813 int nBlob = sqlite3_column_bytes(p->pStmt, i);
18814 output_hex_blob(p->out, pBlob, nBlob);
18815 }else if( isNumber(azArg[i], 0) ){
18816 utf8_printf(p->out,"%s", azArg[i]);
18817 }else{
18818 output_quoted_string(p->out, azArg[i]);
18819 }
18820 }
18821 fputs(p->rowSeparator, p->out);
18822 break;
18823 }
18824 case MODE_Ascii: {
18825 if( p->cnt++==0 && p->showHeader ){
18826 for(i=0; i<nArg; i++){
18827 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
18828 utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
18829 }
18830 utf8_printf(p->out, "%s", p->rowSeparator);
18831 }
18832 if( azArg==0 ) break;
18833 for(i=0; i<nArg; i++){
18834 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
18835 utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
18836 }
18837 utf8_printf(p->out, "%s", p->rowSeparator);
18838 break;
18839 }
18840 case MODE_EQP: {
18841 eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
18842 break;
@@ -18911,11 +19679,11 @@
18911 "INSERT INTO selftest(tno,op,cmd,ans)"
18912 " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
18913 "DROP TABLE [_shell$self];"
18914 ,0,0,&zErrMsg);
18915 if( zErrMsg ){
18916 utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
18917 sqlite3_free(zErrMsg);
18918 }
18919 sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
18920 }
18921
@@ -19014,37 +19782,36 @@
19014 int i;
19015 const char *z;
19016 rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
19017 if( rc!=SQLITE_OK || !pSelect ){
19018 char *zContext = shell_error_context(zSelect, p->db);
19019 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n%s", rc,
19020 sqlite3_errmsg(p->db), zContext);
19021 sqlite3_free(zContext);
19022 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
19023 return rc;
19024 }
19025 rc = sqlite3_step(pSelect);
19026 nResult = sqlite3_column_count(pSelect);
19027 while( rc==SQLITE_ROW ){
19028 z = (const char*)sqlite3_column_text(pSelect, 0);
19029 utf8_printf(p->out, "%s", z);
19030 for(i=1; i<nResult; i++){
19031 utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
19032 }
19033 if( z==0 ) z = "";
19034 while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
19035 if( z[0] ){
19036 raw_printf(p->out, "\n;\n");
19037 }else{
19038 raw_printf(p->out, ";\n");
19039 }
19040 rc = sqlite3_step(pSelect);
19041 }
19042 rc = sqlite3_finalize(pSelect);
19043 if( rc!=SQLITE_OK ){
19044 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
19045 sqlite3_errmsg(p->db));
19046 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
19047 }
19048 return rc;
19049 }
19050
@@ -19076,11 +19843,11 @@
19076
19077 #ifdef __linux__
19078 /*
19079 ** Attempt to display I/O stats on Linux using /proc/PID/io
19080 */
19081 static void displayLinuxIoStats(FILE *out){
19082 FILE *in;
19083 char z[200];
19084 sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
19085 in = fopen(z, "rb");
19086 if( in==0 ) return;
@@ -19099,11 +19866,11 @@
19099 };
19100 int i;
19101 for(i=0; i<ArraySize(aTrans); i++){
19102 int n = strlen30(aTrans[i].zPattern);
19103 if( cli_strncmp(aTrans[i].zPattern, z, n)==0 ){
19104 utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
19105 break;
19106 }
19107 }
19108 }
19109 fclose(in);
@@ -19112,11 +19879,10 @@
19112
19113 /*
19114 ** Display a single line of status using 64-bit values.
19115 */
19116 static void displayStatLine(
19117 ShellState *p, /* The shell context */
19118 char *zLabel, /* Label for this one line */
19119 char *zFormat, /* Format for the result */
19120 int iStatusCtrl, /* Which status to display */
19121 int bReset /* True to reset the stats */
19122 ){
@@ -19131,11 +19897,11 @@
19131 if( nPercent>1 ){
19132 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
19133 }else{
19134 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
19135 }
19136 raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
19137 }
19138
19139 /*
19140 ** Display memory stats.
19141 */
@@ -19144,141 +19910,130 @@
19144 ShellState *pArg, /* Pointer to ShellState */
19145 int bReset /* True to reset the stats */
19146 ){
19147 int iCur;
19148 int iHiwtr;
19149 FILE *out;
19150 if( pArg==0 || pArg->out==0 ) return 0;
19151 out = pArg->out;
19152
19153 if( pArg->pStmt && pArg->statsOn==2 ){
19154 int nCol, i, x;
19155 sqlite3_stmt *pStmt = pArg->pStmt;
19156 char z[100];
19157 nCol = sqlite3_column_count(pStmt);
19158 raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
19159 for(i=0; i<nCol; i++){
19160 sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
19161 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
19162 #ifndef SQLITE_OMIT_DECLTYPE
19163 sqlite3_snprintf(30, z+x, "declared type:");
19164 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
19165 #endif
19166 #ifdef SQLITE_ENABLE_COLUMN_METADATA
19167 sqlite3_snprintf(30, z+x, "database name:");
19168 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
19169 sqlite3_snprintf(30, z+x, "table name:");
19170 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
19171 sqlite3_snprintf(30, z+x, "origin name:");
19172 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
19173 #endif
19174 }
19175 }
19176
19177 if( pArg->statsOn==3 ){
19178 if( pArg->pStmt ){
19179 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP,bReset);
19180 raw_printf(pArg->out, "VM-steps: %d\n", iCur);
19181 }
19182 return 0;
19183 }
19184
19185 displayStatLine(pArg, "Memory Used:",
19186 "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
19187 displayStatLine(pArg, "Number of Outstanding Allocations:",
19188 "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
19189 if( pArg->shellFlgs & SHFLG_Pagecache ){
19190 displayStatLine(pArg, "Number of Pcache Pages Used:",
19191 "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
19192 }
19193 displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
19194 "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
19195 displayStatLine(pArg, "Largest Allocation:",
19196 "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
19197 displayStatLine(pArg, "Largest Pcache Allocation:",
19198 "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
19199 #ifdef YYTRACKMAXSTACKDEPTH
19200 displayStatLine(pArg, "Deepest Parser Stack:",
19201 "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
19202 #endif
19203
19204 if( db ){
19205 if( pArg->shellFlgs & SHFLG_Lookaside ){
19206 iHiwtr = iCur = -1;
19207 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
19208 &iCur, &iHiwtr, bReset);
19209 raw_printf(pArg->out,
19210 "Lookaside Slots Used: %d (max %d)\n",
19211 iCur, iHiwtr);
19212 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
19213 &iCur, &iHiwtr, bReset);
19214 raw_printf(pArg->out, "Successful lookaside attempts: %d\n",
19215 iHiwtr);
19216 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
19217 &iCur, &iHiwtr, bReset);
19218 raw_printf(pArg->out, "Lookaside failures due to size: %d\n",
19219 iHiwtr);
19220 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
19221 &iCur, &iHiwtr, bReset);
19222 raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n",
19223 iHiwtr);
19224 }
19225 iHiwtr = iCur = -1;
19226 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
19227 raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n",
19228 iCur);
19229 iHiwtr = iCur = -1;
19230 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
19231 raw_printf(pArg->out, "Page cache hits: %d\n", iCur);
19232 iHiwtr = iCur = -1;
19233 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
19234 raw_printf(pArg->out, "Page cache misses: %d\n", iCur);
19235 iHiwtr = iCur = -1;
19236 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
19237 raw_printf(pArg->out, "Page cache writes: %d\n", iCur);
19238 iHiwtr = iCur = -1;
19239 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
19240 raw_printf(pArg->out, "Page cache spills: %d\n", iCur);
19241 iHiwtr = iCur = -1;
19242 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
19243 raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n",
19244 iCur);
19245 iHiwtr = iCur = -1;
19246 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
19247 raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n",
19248 iCur);
19249 }
19250
19251 if( pArg->pStmt ){
19252 int iHit, iMiss;
19253 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
19254 bReset);
19255 raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur);
19256 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
19257 raw_printf(pArg->out, "Sort Operations: %d\n", iCur);
19258 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
19259 raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur);
19260 iHit = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_HIT,
19261 bReset);
19262 iMiss = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_MISS,
19263 bReset);
19264 if( iHit || iMiss ){
19265 raw_printf(pArg->out, "Bloom filter bypass taken: %d/%d\n",
19266 iHit, iHit+iMiss);
19267 }
19268 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
19269 raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur);
19270 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
19271 raw_printf(pArg->out, "Reprepare operations: %d\n", iCur);
19272 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
19273 raw_printf(pArg->out, "Number of times run: %d\n", iCur);
19274 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
19275 raw_printf(pArg->out, "Memory used by prepared stmt: %d\n", iCur);
19276 }
19277
19278 #ifdef __linux__
19279 displayLinuxIoStats(pArg->out);
19280 #endif
19281
19282 /* Do not remove this machine readable comment: extra-stats-output-here */
19283
19284 return 0;
@@ -19509,11 +20264,11 @@
19509 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
19510 UNUSED_PARAMETER(db);
19511 UNUSED_PARAMETER(pArg);
19512 #else
19513 if( pArg->scanstatsOn==3 ){
19514 const char *zSql =
19515 " SELECT addr, opcode, p1, p2, p3, p4, p5, comment, nexec,"
19516 " round(ncycle*100.0 / (sum(ncycle) OVER ()), 2)||'%' AS cycles"
19517 " FROM bytecode(?)";
19518
19519 int rc = SQLITE_OK;
@@ -19655,21 +20410,21 @@
19655 #define BOX_1234 "\342\224\274" /* U+253c -|- */
19656
19657 /* Draw horizontal line N characters long using unicode box
19658 ** characters
19659 */
19660 static void print_box_line(FILE *out, int N){
19661 const char zDash[] =
19662 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
19663 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
19664 const int nDash = sizeof(zDash) - 1;
19665 N *= 3;
19666 while( N>nDash ){
19667 utf8_printf(out, zDash);
19668 N -= nDash;
19669 }
19670 utf8_printf(out, "%.*s", N, zDash);
19671 }
19672
19673 /*
19674 ** Draw a horizontal separator for a MODE_Box table.
19675 */
@@ -19680,19 +20435,19 @@
19680 const char *zSep2,
19681 const char *zSep3
19682 ){
19683 int i;
19684 if( nArg>0 ){
19685 utf8_printf(p->out, "%s", zSep1);
19686 print_box_line(p->out, p->actualWidth[0]+2);
19687 for(i=1; i<nArg; i++){
19688 utf8_printf(p->out, "%s", zSep2);
19689 print_box_line(p->out, p->actualWidth[i]+2);
19690 }
19691 utf8_printf(p->out, "%s", zSep3);
19692 }
19693 fputs("\n", p->out);
19694 }
19695
19696 /*
19697 ** z[] is a line of text that is to be displayed the .mode box or table or
19698 ** similar tabular formats. z[] might contain control characters such
@@ -19951,15 +20706,15 @@
19951 rowSep = "\n";
19952 if( p->showHeader ){
19953 for(i=0; i<nColumn; i++){
19954 w = p->actualWidth[i];
19955 if( p->colWidth[i]<0 ) w = -w;
19956 utf8_width_print(p->out, w, azData[i]);
19957 fputs(i==nColumn-1?"\n":" ", p->out);
19958 }
19959 for(i=0; i<nColumn; i++){
19960 print_dashes(p->out, p->actualWidth[i]);
19961 fputs(i==nColumn-1?"\n":" ", p->out);
19962 }
19963 }
19964 break;
19965 }
@@ -19969,12 +20724,12 @@
19969 print_row_separator(p, nColumn, "+");
19970 fputs("| ", p->out);
19971 for(i=0; i<nColumn; i++){
19972 w = p->actualWidth[i];
19973 n = strlenChar(azData[i]);
19974 utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
19975 fputs(i==nColumn-1?" |\n":" | ", p->out);
19976 }
19977 print_row_separator(p, nColumn, "+");
19978 break;
19979 }
19980 case MODE_Markdown: {
@@ -19982,66 +20737,66 @@
19982 rowSep = " |\n";
19983 fputs("| ", p->out);
19984 for(i=0; i<nColumn; i++){
19985 w = p->actualWidth[i];
19986 n = strlenChar(azData[i]);
19987 utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
19988 fputs(i==nColumn-1?" |\n":" | ", p->out);
19989 }
19990 print_row_separator(p, nColumn, "|");
19991 break;
19992 }
19993 case MODE_Box: {
19994 colSep = " " BOX_13 " ";
19995 rowSep = " " BOX_13 "\n";
19996 print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
19997 utf8_printf(p->out, BOX_13 " ");
19998 for(i=0; i<nColumn; i++){
19999 w = p->actualWidth[i];
20000 n = strlenChar(azData[i]);
20001 utf8_printf(p->out, "%*s%s%*s%s",
20002 (w-n)/2, "", azData[i], (w-n+1)/2, "",
20003 i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
20004 }
20005 print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
20006 break;
20007 }
20008 }
20009 for(i=nColumn, j=0; i<nTotal; i++, j++){
20010 if( j==0 && p->cMode!=MODE_Column ){
20011 utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| ");
20012 }
20013 z = azData[i];
20014 if( z==0 ) z = p->nullValue;
20015 w = p->actualWidth[j];
20016 if( p->colWidth[j]<0 ) w = -w;
20017 utf8_width_print(p->out, w, z);
20018 if( j==nColumn-1 ){
20019 utf8_printf(p->out, "%s", rowSep);
20020 if( bMultiLineRowExists && abRowDiv[i/nColumn-1] && i+1<nTotal ){
20021 if( p->cMode==MODE_Table ){
20022 print_row_separator(p, nColumn, "+");
20023 }else if( p->cMode==MODE_Box ){
20024 print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
20025 }else if( p->cMode==MODE_Column ){
20026 raw_printf(p->out, "\n");
20027 }
20028 }
20029 j = -1;
20030 if( seenInterrupt ) goto columnar_end;
20031 }else{
20032 utf8_printf(p->out, "%s", colSep);
20033 }
20034 }
20035 if( p->cMode==MODE_Table ){
20036 print_row_separator(p, nColumn, "+");
20037 }else if( p->cMode==MODE_Box ){
20038 print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
20039 }
20040 columnar_end:
20041 if( seenInterrupt ){
20042 utf8_printf(p->out, "Interrupt\n");
20043 }
20044 nData = (nRow+1)*nColumn;
20045 for(i=0; i<nData; i++){
20046 z = azData[i];
20047 if( z!=zEmpty && z!=zShowNull ) free(azData[i]);
@@ -20176,34 +20931,33 @@
20176 int rc = SQLITE_OK;
20177 sqlite3expert *p = pState->expert.pExpert;
20178 assert( p );
20179 assert( bCancel || pzErr==0 || *pzErr==0 );
20180 if( bCancel==0 ){
20181 FILE *out = pState->out;
20182 int bVerbose = pState->expert.bVerbose;
20183
20184 rc = sqlite3_expert_analyze(p, pzErr);
20185 if( rc==SQLITE_OK ){
20186 int nQuery = sqlite3_expert_count(p);
20187 int i;
20188
20189 if( bVerbose ){
20190 const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
20191 raw_printf(out, "-- Candidates -----------------------------\n");
20192 raw_printf(out, "%s\n", zCand);
20193 }
20194 for(i=0; i<nQuery; i++){
20195 const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
20196 const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
20197 const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
20198 if( zIdx==0 ) zIdx = "(no new indexes)\n";
20199 if( bVerbose ){
20200 raw_printf(out, "-- Query %d --------------------------------\n",i+1);
20201 raw_printf(out, "%s\n\n", zSql);
20202 }
20203 raw_printf(out, "%s\n", zIdx);
20204 raw_printf(out, "%s\n", zEQP);
20205 }
20206 }
20207 }
20208 sqlite3_expert_destroy(p);
20209 pState->expert.pExpert = 0;
@@ -20234,31 +20988,30 @@
20234 if( n>=2 && 0==cli_strncmp(z, "-verbose", n) ){
20235 pState->expert.bVerbose = 1;
20236 }
20237 else if( n>=2 && 0==cli_strncmp(z, "-sample", n) ){
20238 if( i==(nArg-1) ){
20239 raw_printf(stderr, "option requires an argument: %s\n", z);
20240 rc = SQLITE_ERROR;
20241 }else{
20242 iSample = (int)integerValue(azArg[++i]);
20243 if( iSample<0 || iSample>100 ){
20244 raw_printf(stderr, "value out of range: %s\n", azArg[i]);
20245 rc = SQLITE_ERROR;
20246 }
20247 }
20248 }
20249 else{
20250 raw_printf(stderr, "unknown option: %s\n", z);
20251 rc = SQLITE_ERROR;
20252 }
20253 }
20254
20255 if( rc==SQLITE_OK ){
20256 pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
20257 if( pState->expert.pExpert==0 ){
20258 raw_printf(stderr, "sqlite3_expert_new: %s\n",
20259 zErr ? zErr : "out of memory");
20260 rc = SQLITE_ERROR;
20261 }else{
20262 sqlite3_expert_config(
20263 pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
20264 );
@@ -20581,33 +21334,33 @@
20581 if( zType==0 ) return 0;
20582 dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
20583 noSys = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
20584
20585 if( cli_strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
20586 if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
20587 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
20588 if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n");
20589 }else if( cli_strncmp(zTable, "sqlite_", 7)==0 ){
20590 return 0;
20591 }else if( dataOnly ){
20592 /* no-op */
20593 }else if( cli_strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
20594 char *zIns;
20595 if( !p->writableSchema ){
20596 raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
20597 p->writableSchema = 1;
20598 }
20599 zIns = sqlite3_mprintf(
20600 "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
20601 "VALUES('table','%q','%q',0,'%q');",
20602 zTable, zTable, zSql);
20603 shell_check_oom(zIns);
20604 utf8_printf(p->out, "%s\n", zIns);
20605 sqlite3_free(zIns);
20606 return 0;
20607 }else{
20608 printSchemaLine(p->out, zSql, ";\n");
20609 }
20610
20611 if( cli_strcmp(zType, "table")==0 ){
20612 ShellText sSelect;
20613 ShellText sTable;
@@ -20661,11 +21414,11 @@
20661 savedMode = p->mode;
20662 p->zDestTable = sTable.z;
20663 p->mode = p->cMode = MODE_Insert;
20664 rc = shell_exec(p, sSelect.z, 0);
20665 if( (rc&0xff)==SQLITE_CORRUPT ){
20666 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
20667 toggleSelectOrder(p->db);
20668 shell_exec(p, sSelect.z, 0);
20669 toggleSelectOrder(p->db);
20670 }
20671 p->zDestTable = savedDestTable;
@@ -20692,22 +21445,22 @@
20692 char *zErr = 0;
20693 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
20694 if( rc==SQLITE_CORRUPT ){
20695 char *zQ2;
20696 int len = strlen30(zQuery);
20697 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
20698 if( zErr ){
20699 utf8_printf(p->out, "/****** %s ******/\n", zErr);
20700 sqlite3_free(zErr);
20701 zErr = 0;
20702 }
20703 zQ2 = malloc( len+100 );
20704 if( zQ2==0 ) return rc;
20705 sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
20706 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
20707 if( rc ){
20708 utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
20709 }else{
20710 rc = SQLITE_CORRUPT;
20711 }
20712 sqlite3_free(zErr);
20713 free(zQ2);
@@ -21059,24 +21812,24 @@
21059 hh &= ~HH_Summary;
21060 break;
21061 }
21062 if( ((hw^hh)&HH_Undoc)==0 ){
21063 if( (hh&HH_Summary)!=0 ){
21064 utf8_printf(out, ".%s\n", azHelp[i]+1);
21065 ++n;
21066 }else if( (hw&HW_SummaryOnly)==0 ){
21067 utf8_printf(out, "%s\n", azHelp[i]);
21068 }
21069 }
21070 }
21071 }else{
21072 /* Seek documented commands for which zPattern is an exact prefix */
21073 zPat = sqlite3_mprintf(".%s*", zPattern);
21074 shell_check_oom(zPat);
21075 for(i=0; i<ArraySize(azHelp); i++){
21076 if( sqlite3_strglob(zPat, azHelp[i])==0 ){
21077 utf8_printf(out, "%s\n", azHelp[i]);
21078 j = i+1;
21079 n++;
21080 }
21081 }
21082 sqlite3_free(zPat);
@@ -21083,11 +21836,11 @@
21083 if( n ){
21084 if( n==1 ){
21085 /* when zPattern is a prefix of exactly one command, then include
21086 ** the details of that command, which should begin at offset j */
21087 while( j<ArraySize(azHelp)-1 && azHelp[j][0]==' ' ){
21088 utf8_printf(out, "%s\n", azHelp[j]);
21089 j++;
21090 }
21091 }
21092 return n;
21093 }
@@ -21100,14 +21853,14 @@
21100 while( i<ArraySize(azHelp)-1 && azHelp[i+1][0]==' ' ) ++i;
21101 continue;
21102 }
21103 if( azHelp[i][0]=='.' ) j = i;
21104 if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
21105 utf8_printf(out, "%s\n", azHelp[j]);
21106 while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]==' ' ){
21107 j++;
21108 utf8_printf(out, "%s\n", azHelp[j]);
21109 }
21110 i = j;
21111 n++;
21112 }
21113 }
@@ -21141,27 +21894,27 @@
21141 char *pBuf;
21142 int rc;
21143 if( in==0 ) return 0;
21144 rc = fseek(in, 0, SEEK_END);
21145 if( rc!=0 ){
21146 raw_printf(stderr, "Error: '%s' not seekable\n", zName);
21147 fclose(in);
21148 return 0;
21149 }
21150 nIn = ftell(in);
21151 rewind(in);
21152 pBuf = sqlite3_malloc64( nIn+1 );
21153 if( pBuf==0 ){
21154 raw_printf(stderr, "Error: out of memory\n");
21155 fclose(in);
21156 return 0;
21157 }
21158 nRead = fread(pBuf, nIn, 1, in);
21159 fclose(in);
21160 if( nRead!=1 ){
21161 sqlite3_free(pBuf);
21162 raw_printf(stderr, "Error: cannot read '%s'\n", zName);
21163 return 0;
21164 }
21165 pBuf[nIn] = 0;
21166 if( pnByte ) *pnByte = nIn;
21167 return pBuf;
@@ -21278,11 +22031,11 @@
21278 unsigned int x[16];
21279 char zLine[1000];
21280 if( zDbFilename ){
21281 in = fopen(zDbFilename, "r");
21282 if( in==0 ){
21283 utf8_printf(stderr, "cannot open \"%s\" for reading\n", zDbFilename);
21284 return 0;
21285 }
21286 nLine = 0;
21287 }else{
21288 in = p->in;
@@ -21299,11 +22052,11 @@
21299 n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */
21300 a = sqlite3_malloc( n ? n : 1 );
21301 shell_check_oom(a);
21302 memset(a, 0, n);
21303 if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
21304 utf8_printf(stderr, "invalid pagesize\n");
21305 goto readHexDb_error;
21306 }
21307 for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
21308 rc = sscanf(zLine, "| page %d offset %d", &j, &k);
21309 if( rc==2 ){
@@ -21341,11 +22094,11 @@
21341 if(cli_strncmp(zLine, "| end ", 6)==0 ) break;
21342 }
21343 p->lineno = nLine;
21344 }
21345 sqlite3_free(a);
21346 utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
21347 return 0;
21348 }
21349 #endif /* SQLITE_OMIT_DESERIALIZE */
21350
21351 /*
@@ -21417,26 +22170,23 @@
21417 break;
21418 }
21419 }
21420 globalDb = p->db;
21421 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
21422 utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
21423 zDbFilename, sqlite3_errmsg(p->db));
21424 if( (openFlags & OPEN_DB_KEEPALIVE)==0 ){
21425 exit(1);
21426 }
21427 sqlite3_close(p->db);
21428 sqlite3_open(":memory:", &p->db);
21429 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
21430 utf8_printf(stderr,
21431 "Also: unable to open substitute in-memory database.\n"
21432 );
21433 exit(1);
21434 }else{
21435 utf8_printf(stderr,
21436 "Notice: using substitute in-memory database instead of \"%s\"\n",
21437 zDbFilename);
21438 }
21439 }
21440 sqlite3_db_config(p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, (int)0, (int*)0);
21441
21442 /* Reflect the use or absence of --unsafe-testing invocation. */
@@ -21539,11 +22289,11 @@
21539 }
21540 rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
21541 SQLITE_DESERIALIZE_RESIZEABLE |
21542 SQLITE_DESERIALIZE_FREEONCLOSE);
21543 if( rc ){
21544 utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
21545 }
21546 if( p->szMax>0 ){
21547 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
21548 }
21549 }
@@ -21563,12 +22313,11 @@
21563 ** Attempt to close the database connection. Report errors.
21564 */
21565 void close_db(sqlite3 *db){
21566 int rc = sqlite3_close(db);
21567 if( rc ){
21568 utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
21569 rc, sqlite3_errmsg(db));
21570 }
21571 }
21572
21573 #if HAVE_READLINE || HAVE_EDITLINE
21574 /*
@@ -21725,12 +22474,11 @@
21725 return 1;
21726 }
21727 if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
21728 return 0;
21729 }
21730 utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
21731 zArg);
21732 return 0;
21733 }
21734
21735 /*
21736 ** Set or clear a shell flag according to a boolean value.
@@ -21764,11 +22512,11 @@
21764 }else if( cli_strcmp(zFile, "off")==0 ){
21765 f = 0;
21766 }else{
21767 f = fopen(zFile, bTextMode ? "w" : "wb");
21768 if( f==0 ){
21769 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
21770 }
21771 }
21772 return f;
21773 }
21774
@@ -21786,11 +22534,11 @@
21786 sqlite3_stmt *pStmt;
21787 const char *zSql;
21788 i64 nSql;
21789 if( p->traceOut==0 ) return 0;
21790 if( mType==SQLITE_TRACE_CLOSE ){
21791 utf8_printf(p->traceOut, "-- closing database connection\n");
21792 return 0;
21793 }
21794 if( mType!=SQLITE_TRACE_ROW && pX!=0 && ((const char*)pX)[0]=='-' ){
21795 zSql = (const char*)pX;
21796 }else{
@@ -21817,16 +22565,16 @@
21817 if( nSql>1000000000 ) nSql = 1000000000;
21818 while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
21819 switch( mType ){
21820 case SQLITE_TRACE_ROW:
21821 case SQLITE_TRACE_STMT: {
21822 utf8_printf(p->traceOut, "%.*s;\n", (int)nSql, zSql);
21823 break;
21824 }
21825 case SQLITE_TRACE_PROFILE: {
21826 sqlite3_int64 nNanosec = pX ? *(sqlite3_int64*)pX : 0;
21827 utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", (int)nSql, zSql, nNanosec);
21828 break;
21829 }
21830 }
21831 return 0;
21832 }
@@ -21929,16 +22677,15 @@
21929 do{ p->n--; }while( p->z[p->n]!=cQuote );
21930 p->cTerm = c;
21931 break;
21932 }
21933 if( pc==cQuote && c!='\r' ){
21934 utf8_printf(stderr, "%s:%d: unescaped %c character\n",
21935 p->zFile, p->nLine, cQuote);
21936 }
21937 if( c==EOF ){
21938 utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
21939 p->zFile, startLine, cQuote);
21940 p->cTerm = c;
21941 break;
21942 }
21943 import_append_char(p, c);
21944 ppc = pc;
@@ -22032,13 +22779,12 @@
22032
22033 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
22034 shell_check_oom(zQuery);
22035 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
22036 if( rc ){
22037 utf8_printf(stderr, "Error %d: %s on [%s]\n",
22038 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
22039 zQuery);
22040 goto end_data_xfer;
22041 }
22042 n = sqlite3_column_count(pQuery);
22043 zInsert = sqlite3_malloc64(200 + nTable + n*3);
22044 shell_check_oom(zInsert);
@@ -22050,13 +22796,12 @@
22050 i += 2;
22051 }
22052 memcpy(zInsert+i, ");", 3);
22053 rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
22054 if( rc ){
22055 utf8_printf(stderr, "Error %d: %s on [%s]\n",
22056 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
22057 zInsert);
22058 goto end_data_xfer;
22059 }
22060 for(k=0; k<2; k++){
22061 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
22062 for(i=0; i<n; i++){
@@ -22087,12 +22832,12 @@
22087 }
22088 }
22089 } /* End for */
22090 rc = sqlite3_step(pInsert);
22091 if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
22092 utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
22093 sqlite3_errmsg(newDb));
22094 }
22095 sqlite3_reset(pInsert);
22096 cnt++;
22097 if( (cnt%spinRate)==0 ){
22098 printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
@@ -22105,11 +22850,11 @@
22105 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
22106 zTable);
22107 shell_check_oom(zQuery);
22108 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
22109 if( rc ){
22110 utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
22111 break;
22112 }
22113 } /* End for(k=0...) */
22114
22115 end_data_xfer:
@@ -22142,62 +22887,60 @@
22142 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
22143 " WHERE %s ORDER BY rowid ASC", zWhere);
22144 shell_check_oom(zQuery);
22145 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
22146 if( rc ){
22147 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
22148 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
22149 zQuery);
22150 goto end_schema_xfer;
22151 }
22152 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
22153 zName = sqlite3_column_text(pQuery, 0);
22154 zSql = sqlite3_column_text(pQuery, 1);
22155 if( zName==0 || zSql==0 ) continue;
22156 if( sqlite3_stricmp((char*)zName, "sqlite_sequence")!=0 ){
22157 printf("%s... ", zName); fflush(stdout);
22158 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
22159 if( zErrMsg ){
22160 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
22161 sqlite3_free(zErrMsg);
22162 zErrMsg = 0;
22163 }
22164 }
22165 if( xForEach ){
22166 xForEach(p, newDb, (const char*)zName);
22167 }
22168 printf("done\n");
22169 }
22170 if( rc!=SQLITE_DONE ){
22171 sqlite3_finalize(pQuery);
22172 sqlite3_free(zQuery);
22173 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
22174 " WHERE %s ORDER BY rowid DESC", zWhere);
22175 shell_check_oom(zQuery);
22176 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
22177 if( rc ){
22178 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
22179 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
22180 zQuery);
22181 goto end_schema_xfer;
22182 }
22183 while( sqlite3_step(pQuery)==SQLITE_ROW ){
22184 zName = sqlite3_column_text(pQuery, 0);
22185 zSql = sqlite3_column_text(pQuery, 1);
22186 if( zName==0 || zSql==0 ) continue;
22187 if( sqlite3_stricmp((char*)zName, "sqlite_sequence")==0 ) continue;
22188 printf("%s... ", zName); fflush(stdout);
22189 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
22190 if( zErrMsg ){
22191 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
22192 sqlite3_free(zErrMsg);
22193 zErrMsg = 0;
22194 }
22195 if( xForEach ){
22196 xForEach(p, newDb, (const char*)zName);
22197 }
22198 printf("done\n");
22199 }
22200 }
22201 end_schema_xfer:
22202 sqlite3_finalize(pQuery);
22203 sqlite3_free(zQuery);
@@ -22210,17 +22953,16 @@
22210 */
22211 static void tryToClone(ShellState *p, const char *zNewDb){
22212 int rc;
22213 sqlite3 *newDb = 0;
22214 if( access(zNewDb,0)==0 ){
22215 utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
22216 return;
22217 }
22218 rc = sqlite3_open(zNewDb, &newDb);
22219 if( rc ){
22220 utf8_printf(stderr, "Cannot create output database: %s\n",
22221 sqlite3_errmsg(newDb));
22222 }else{
22223 sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
22224 sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
22225 tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
22226 tryToCloneSchema(p, newDb, "type!='table'", 0);
@@ -22227,10 +22969,22 @@
22227 sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
22228 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
22229 }
22230 close_db(newDb);
22231 }
 
 
 
 
 
 
 
 
 
 
 
 
22232
22233 /*
22234 ** Change the output file back to stdout.
22235 **
22236 ** If the p->doXdgOpen flag is set, that means the output was being
@@ -22255,11 +23009,11 @@
22255 "xdg-open";
22256 #endif
22257 char *zCmd;
22258 zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
22259 if( system(zCmd) ){
22260 utf8_printf(stderr, "Failed: [%s]\n", zCmd);
22261 }else{
22262 /* Give the start/open/xdg-open command some time to get
22263 ** going before we continue, and potential delete the
22264 ** p->zTempFile data file out from under it */
22265 sqlite3_sleep(2000);
@@ -22270,11 +23024,16 @@
22270 }
22271 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
22272 }
22273 p->outfile[0] = 0;
22274 p->out = stdout;
 
22275 }
 
 
 
 
22276
22277 /*
22278 ** Run an SQL command and return the single integer result.
22279 */
22280 static int db_int(sqlite3 *db, const char *zSql){
@@ -22341,11 +23100,11 @@
22341 if( p->db==0 ) return 1;
22342 rc = sqlite3_prepare_v2(p->db,
22343 "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
22344 -1, &pStmt, 0);
22345 if( rc ){
22346 utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
22347 sqlite3_finalize(pStmt);
22348 return 1;
22349 }
22350 sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
22351 if( sqlite3_step(pStmt)==SQLITE_ROW
@@ -22354,32 +23113,32 @@
22354 const u8 *pb = sqlite3_column_blob(pStmt,0);
22355 shell_check_oom(pb);
22356 memcpy(aHdr, pb, 100);
22357 sqlite3_finalize(pStmt);
22358 }else{
22359 raw_printf(stderr, "unable to read database header\n");
22360 sqlite3_finalize(pStmt);
22361 return 1;
22362 }
22363 i = get2byteInt(aHdr+16);
22364 if( i==1 ) i = 65536;
22365 utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
22366 utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
22367 utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
22368 utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
22369 for(i=0; i<ArraySize(aField); i++){
22370 int ofst = aField[i].ofst;
22371 unsigned int val = get4byteInt(aHdr + ofst);
22372 utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
22373 switch( ofst ){
22374 case 56: {
22375 if( val==1 ) raw_printf(p->out, " (utf8)");
22376 if( val==2 ) raw_printf(p->out, " (utf16le)");
22377 if( val==3 ) raw_printf(p->out, " (utf16be)");
22378 }
22379 }
22380 raw_printf(p->out, "\n");
22381 }
22382 if( zDb==0 ){
22383 zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
22384 }else if( cli_strcmp(zDb,"temp")==0 ){
22385 zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
@@ -22388,25 +23147,25 @@
22388 }
22389 for(i=0; i<ArraySize(aQuery); i++){
22390 char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
22391 int val = db_int(p->db, zSql);
22392 sqlite3_free(zSql);
22393 utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
22394 }
22395 sqlite3_free(zSchemaTab);
22396 sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
22397 utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
22398 return 0;
22399 }
22400 #endif /* SQLITE_SHELL_HAVE_RECOVER */
22401
22402 /*
22403 ** Print the current sqlite3_errmsg() value to stderr and return 1.
22404 */
22405 static int shellDatabaseError(sqlite3 *db){
22406 const char *zErr = sqlite3_errmsg(db);
22407 utf8_printf(stderr, "Error: %s\n", zErr);
22408 return 1;
22409 }
22410
22411 /*
22412 ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE
@@ -22637,11 +23396,10 @@
22637 ShellState *pState, /* Current shell tool state */
22638 char **azArg, /* Array of arguments passed to dot command */
22639 int nArg /* Number of entries in azArg[] */
22640 ){
22641 sqlite3 *db = pState->db; /* Database handle to query "main" db of */
22642 FILE *out = pState->out; /* Stream to write non-error output to */
22643 int bVerbose = 0; /* If -verbose is present */
22644 int bGroupByParent = 0; /* If -groupbyparent is present */
22645 int i; /* To iterate through azArg[] */
22646 const char *zIndent = ""; /* How much to indent CREATE INDEX by */
22647 int rc; /* Return code */
@@ -22719,13 +23477,11 @@
22719 else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
22720 bGroupByParent = 1;
22721 zIndent = " ";
22722 }
22723 else{
22724 raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
22725 azArg[0], azArg[1]
22726 );
22727 return SQLITE_ERROR;
22728 }
22729 }
22730
22731 /* Register the fkey_collate_clause() SQL function */
@@ -22765,44 +23521,44 @@
22765 }
22766 rc = sqlite3_finalize(pExplain);
22767 if( rc!=SQLITE_OK ) break;
22768
22769 if( res<0 ){
22770 raw_printf(stderr, "Error: internal error");
22771 break;
22772 }else{
22773 if( bGroupByParent
22774 && (bVerbose || res==0)
22775 && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
22776 ){
22777 raw_printf(out, "-- Parent table %s\n", zParent);
22778 sqlite3_free(zPrev);
22779 zPrev = sqlite3_mprintf("%s", zParent);
22780 }
22781
22782 if( res==0 ){
22783 raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
22784 }else if( bVerbose ){
22785 raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
22786 zIndent, zFrom, zTarget
22787 );
22788 }
22789 }
22790 }
22791 sqlite3_free(zPrev);
22792
22793 if( rc!=SQLITE_OK ){
22794 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
22795 }
22796
22797 rc2 = sqlite3_finalize(pSql);
22798 if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
22799 rc = rc2;
22800 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
22801 }
22802 }else{
22803 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
22804 }
22805
22806 return rc;
22807 }
22808
@@ -22818,13 +23574,13 @@
22818 n = (nArg>=2 ? strlen30(azArg[1]) : 0);
22819 if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
22820 return lintFkeyIndexes(pState, azArg, nArg);
22821
22822 usage:
22823 raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
22824 raw_printf(stderr, "Where sub-commands are:\n");
22825 raw_printf(stderr, " fkey-indexes\n");
22826 return SQLITE_ERROR;
22827 }
22828
22829 #if !defined SQLITE_OMIT_VIRTUALTABLE
22830 static void shellPrepare(
@@ -22835,13 +23591,11 @@
22835 ){
22836 *ppStmt = 0;
22837 if( *pRc==SQLITE_OK ){
22838 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
22839 if( rc!=SQLITE_OK ){
22840 raw_printf(stderr, "sql error: %s (%d)\n",
22841 sqlite3_errmsg(db), sqlite3_errcode(db)
22842 );
22843 *pRc = rc;
22844 }
22845 }
22846 }
22847
@@ -22888,11 +23642,11 @@
22888 if( pStmt ){
22889 sqlite3 *db = sqlite3_db_handle(pStmt);
22890 int rc = sqlite3_finalize(pStmt);
22891 if( *pRc==SQLITE_OK ){
22892 if( rc!=SQLITE_OK ){
22893 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
22894 }
22895 *pRc = rc;
22896 }
22897 }
22898 }
@@ -22909,11 +23663,11 @@
22909 ){
22910 int rc = sqlite3_reset(pStmt);
22911 if( *pRc==SQLITE_OK ){
22912 if( rc!=SQLITE_OK ){
22913 sqlite3 *db = sqlite3_db_handle(pStmt);
22914 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
22915 }
22916 *pRc = rc;
22917 }
22918 }
22919 #endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
@@ -22959,15 +23713,15 @@
22959 va_list ap;
22960 char *z;
22961 va_start(ap, zFmt);
22962 z = sqlite3_vmprintf(zFmt, ap);
22963 va_end(ap);
22964 utf8_printf(stderr, "Error: %s\n", z);
22965 if( pAr->fromCmdLine ){
22966 utf8_printf(stderr, "Use \"-A\" for more help\n");
22967 }else{
22968 utf8_printf(stderr, "Use \".archive --help\" for more help\n");
22969 }
22970 sqlite3_free(z);
22971 return SQLITE_ERROR;
22972 }
22973
@@ -23063,11 +23817,11 @@
23063 };
23064 int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
23065 struct ArSwitch *pEnd = &aSwitch[nSwitch];
23066
23067 if( nArg<=1 ){
23068 utf8_printf(stderr, "Wrong number of arguments. Usage:\n");
23069 return arUsage(stderr);
23070 }else{
23071 char *z = azArg[1];
23072 if( z[0]!='-' ){
23073 /* Traditional style [tar] invocation */
@@ -23169,11 +23923,11 @@
23169 }
23170 }
23171 }
23172 }
23173 if( pAr->eCmd==0 ){
23174 utf8_printf(stderr, "Required argument missing. Usage:\n");
23175 return arUsage(stderr);
23176 }
23177 return SQLITE_OK;
23178 }
23179
@@ -23212,11 +23966,11 @@
23212 if( SQLITE_ROW==sqlite3_step(pTest) ){
23213 bOk = 1;
23214 }
23215 shellReset(&rc, pTest);
23216 if( rc==SQLITE_OK && bOk==0 ){
23217 utf8_printf(stderr, "not found in archive: %s\n", z);
23218 rc = SQLITE_ERROR;
23219 }
23220 }
23221 shellFinalize(&rc, pTest);
23222 }
@@ -23279,30 +24033,26 @@
23279 arWhereClause(&rc, pAr, &zWhere);
23280
23281 shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
23282 pAr->zSrcTable, zWhere);
23283 if( pAr->bDryRun ){
23284 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
23285 }else{
23286 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
23287 if( pAr->bVerbose ){
23288 utf8_printf(pAr->p->out, "%s % 10d %s %s\n",
23289 sqlite3_column_text(pSql, 0),
23290 sqlite3_column_int(pSql, 1),
23291 sqlite3_column_text(pSql, 2),
23292 sqlite3_column_text(pSql, 3)
23293 );
23294 }else{
23295 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
23296 }
23297 }
23298 }
23299 shellFinalize(&rc, pSql);
23300 sqlite3_free(zWhere);
23301 return rc;
23302 }
23303
23304
23305 /*
23306 ** Implementation of .ar "Remove" command.
23307 */
23308 static int arRemoveCommand(ArCommand *pAr){
@@ -23318,11 +24068,11 @@
23318 }
23319 if( rc==SQLITE_OK ){
23320 zSql = sqlite3_mprintf("DELETE FROM %s WHERE %s;",
23321 pAr->zSrcTable, zWhere);
23322 if( pAr->bDryRun ){
23323 utf8_printf(pAr->p->out, "%s\n", zSql);
23324 }else{
23325 char *zErr = 0;
23326 rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0);
23327 if( rc==SQLITE_OK ){
23328 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
@@ -23331,11 +24081,11 @@
23331 }else{
23332 rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0);
23333 }
23334 }
23335 if( zErr ){
23336 utf8_printf(stdout, "ERROR: %s\n", zErr);
23337 sqlite3_free(zErr);
23338 }
23339 }
23340 }
23341 sqlite3_free(zWhere);
@@ -23395,15 +24145,15 @@
23395 ** populating them changes the timestamp). */
23396 for(i=0; i<2; i++){
23397 j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
23398 sqlite3_bind_int(pSql, j, i);
23399 if( pAr->bDryRun ){
23400 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
23401 }else{
23402 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
23403 if( i==0 && pAr->bVerbose ){
23404 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
23405 }
23406 }
23407 }
23408 shellReset(&rc, pSql);
23409 }
@@ -23419,17 +24169,17 @@
23419 ** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out.
23420 */
23421 static int arExecSql(ArCommand *pAr, const char *zSql){
23422 int rc;
23423 if( pAr->bDryRun ){
23424 utf8_printf(pAr->p->out, "%s\n", zSql);
23425 rc = SQLITE_OK;
23426 }else{
23427 char *zErr = 0;
23428 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
23429 if( zErr ){
23430 utf8_printf(stdout, "ERROR: %s\n", zErr);
23431 sqlite3_free(zErr);
23432 }
23433 }
23434 return rc;
23435 }
@@ -23600,19 +24350,17 @@
23600 }else{
23601 flags = SQLITE_OPEN_READONLY;
23602 }
23603 cmd.db = 0;
23604 if( cmd.bDryRun ){
23605 utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
23606 eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
23607 }
23608 rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
23609 eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
23610 if( rc!=SQLITE_OK ){
23611 utf8_printf(stderr, "cannot open file: %s (%s)\n",
23612 cmd.zFile, sqlite3_errmsg(cmd.db)
23613 );
23614 goto end_ar_command;
23615 }
23616 sqlite3_fileio_init(cmd.db, 0, 0);
23617 sqlite3_sqlar_init(cmd.db, 0, 0);
23618 sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
@@ -23621,11 +24369,11 @@
23621 }
23622 if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
23623 if( cmd.eCmd!=AR_CMD_CREATE
23624 && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
23625 ){
23626 utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
23627 rc = SQLITE_ERROR;
23628 goto end_ar_command;
23629 }
23630 cmd.zSrcTable = sqlite3_mprintf("sqlar");
23631 }
@@ -23679,11 +24427,11 @@
23679 ** This function is used as a callback by the recover extension. Simply
23680 ** print the supplied SQL statement to stdout.
23681 */
23682 static int recoverSqlCb(void *pCtx, const char *zSql){
23683 ShellState *pState = (ShellState*)pCtx;
23684 utf8_printf(pState->out, "%s;\n", zSql);
23685 return SQLITE_OK;
23686 }
23687
23688 /*
23689 ** This function is called to recover data from the database. A script
@@ -23722,11 +24470,11 @@
23722 }else
23723 if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
23724 bRowids = 0;
23725 }
23726 else{
23727 utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
23728 showHelp(pState->out, azArg[0]);
23729 return 1;
23730 }
23731 }
23732
@@ -23741,11 +24489,11 @@
23741
23742 sqlite3_recover_run(p);
23743 if( sqlite3_recover_errcode(p)!=SQLITE_OK ){
23744 const char *zErr = sqlite3_recover_errmsg(p);
23745 int errCode = sqlite3_recover_errcode(p);
23746 raw_printf(stderr, "sql error: %s (%d)\n", zErr, errCode);
23747 }
23748 rc = sqlite3_recover_finish(p);
23749 return rc;
23750 }
23751 #endif /* SQLITE_SHELL_HAVE_RECOVER */
@@ -23766,11 +24514,11 @@
23766 */
23767 #ifdef SHELL_DEBUG
23768 #define rc_err_oom_die(rc) \
23769 if( rc==SQLITE_NOMEM ) shell_check_oom(0); \
23770 else if(!(rc==SQLITE_OK||rc==SQLITE_DONE)) \
23771 fprintf(stderr,"E:%d\n",rc), assert(0)
23772 #else
23773 static void rc_err_oom_die(int rc){
23774 if( rc==SQLITE_NOMEM ) shell_check_oom(0);
23775 assert(rc==SQLITE_OK||rc==SQLITE_DONE);
23776 }
@@ -23906,10 +24654,11 @@
23906 #ifdef SHELL_COLFIX_DB
23907 if(*zCOL_DB!=':')
23908 sqlite3_exec(*pDb,"drop table if exists ColNames;"
23909 "drop view if exists RepeatedNames;",0,0,0);
23910 #endif
 
23911 rc = sqlite3_exec(*pDb, zTabMake, 0, 0, 0);
23912 rc_err_oom_die(rc);
23913 }
23914 assert(*pDb!=0);
23915 rc = sqlite3_prepare_v2(*pDb, zTabFill, -1, &pStmt, 0);
@@ -24019,11 +24768,11 @@
24019 clearTempFile(p);
24020
24021 #ifndef SQLITE_OMIT_AUTHORIZATION
24022 if( c=='a' && cli_strncmp(azArg[0], "auth", n)==0 ){
24023 if( nArg!=2 ){
24024 raw_printf(stderr, "Usage: .auth ON|OFF\n");
24025 rc = 1;
24026 goto meta_command_exit;
24027 }
24028 open_db(p, 0);
24029 if( booleanValue(azArg[1]) ){
@@ -24066,32 +24815,32 @@
24066 }else
24067 if( cli_strcmp(z, "-async")==0 ){
24068 bAsync = 1;
24069 }else
24070 {
24071 utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
24072 return 1;
24073 }
24074 }else if( zDestFile==0 ){
24075 zDestFile = azArg[j];
24076 }else if( zDb==0 ){
24077 zDb = zDestFile;
24078 zDestFile = azArg[j];
24079 }else{
24080 raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
24081 return 1;
24082 }
24083 }
24084 if( zDestFile==0 ){
24085 raw_printf(stderr, "missing FILENAME argument on .backup\n");
24086 return 1;
24087 }
24088 if( zDb==0 ) zDb = "main";
24089 rc = sqlite3_open_v2(zDestFile, &pDest,
24090 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
24091 if( rc!=SQLITE_OK ){
24092 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
24093 close_db(pDest);
24094 return 1;
24095 }
24096 if( bAsync ){
24097 sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
@@ -24098,20 +24847,20 @@
24098 0, 0, 0);
24099 }
24100 open_db(p, 0);
24101 pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
24102 if( pBackup==0 ){
24103 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
24104 close_db(pDest);
24105 return 1;
24106 }
24107 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
24108 sqlite3_backup_finish(pBackup);
24109 if( rc==SQLITE_DONE ){
24110 rc = 0;
24111 }else{
24112 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
24113 rc = 1;
24114 }
24115 close_db(pDest);
24116 }else
24117 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
@@ -24118,11 +24867,11 @@
24118
24119 if( c=='b' && n>=3 && cli_strncmp(azArg[0], "bail", n)==0 ){
24120 if( nArg==2 ){
24121 bail_on_error = booleanValue(azArg[1]);
24122 }else{
24123 raw_printf(stderr, "Usage: .bail on|off\n");
24124 rc = 1;
24125 }
24126 }else
24127
24128 /* Undocumented. Legacy only. See "crnl" below */
@@ -24132,13 +24881,12 @@
24132 setBinaryMode(p->out, 1);
24133 }else{
24134 setTextMode(p->out, 1);
24135 }
24136 }else{
24137 raw_printf(stderr, "The \".binary\" command is deprecated."
24138 " Use \".crnl\" instead.\n");
24139 raw_printf(stderr, "Usage: .binary on|off\n");
24140 rc = 1;
24141 }
24142 }else
24143
24144 /* The undocumented ".breakpoint" command causes a call to the no-op
@@ -24158,25 +24906,25 @@
24158 sqlite3_free(z);
24159 #else
24160 rc = chdir(azArg[1]);
24161 #endif
24162 if( rc ){
24163 utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
24164 rc = 1;
24165 }
24166 }else{
24167 raw_printf(stderr, "Usage: .cd DIRECTORY\n");
24168 rc = 1;
24169 }
24170 }else
24171 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
24172
24173 if( c=='c' && n>=3 && cli_strncmp(azArg[0], "changes", n)==0 ){
24174 if( nArg==2 ){
24175 setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
24176 }else{
24177 raw_printf(stderr, "Usage: .changes on|off\n");
24178 rc = 1;
24179 }
24180 }else
24181
24182 #ifndef SQLITE_SHELL_FIDDLE
@@ -24186,21 +24934,20 @@
24186 */
24187 if( c=='c' && n>=3 && cli_strncmp(azArg[0], "check", n)==0 ){
24188 char *zRes = 0;
24189 output_reset(p);
24190 if( nArg!=2 ){
24191 raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
24192 rc = 2;
24193 }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
24194 rc = 2;
24195 }else if( testcase_glob(azArg[1],zRes)==0 ){
24196 utf8_printf(stderr,
24197 "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
24198 p->zTestcase, azArg[1], zRes);
24199 rc = 1;
24200 }else{
24201 utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
24202 p->nCheck++;
24203 }
24204 sqlite3_free(zRes);
24205 }else
24206 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
@@ -24209,11 +24956,11 @@
24209 if( c=='c' && cli_strncmp(azArg[0], "clone", n)==0 ){
24210 failIfSafeMode(p, "cannot run .clone in safe mode");
24211 if( nArg==2 ){
24212 tryToClone(p, azArg[1]);
24213 }else{
24214 raw_printf(stderr, "Usage: .clone FILENAME\n");
24215 rc = 1;
24216 }
24217 }else
24218 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
24219
@@ -24229,13 +24976,13 @@
24229 zFile = "(memory)";
24230 }else if( zFile[0]==0 ){
24231 zFile = "(temporary-file)";
24232 }
24233 if( p->pAuxDb == &p->aAuxDb[i] ){
24234 utf8_printf(stdout, "ACTIVE %d: %s\n", i, zFile);
24235 }else if( p->aAuxDb[i].db!=0 ){
24236 utf8_printf(stdout, " %d: %s\n", i, zFile);
24237 }
24238 }
24239 }else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){
24240 int i = azArg[1][0] - '0';
24241 if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && i<ArraySize(p->aAuxDb) ){
@@ -24248,19 +24995,19 @@
24248 && IsDigit(azArg[2][0]) && azArg[2][1]==0 ){
24249 int i = azArg[2][0] - '0';
24250 if( i<0 || i>=ArraySize(p->aAuxDb) ){
24251 /* No-op */
24252 }else if( p->pAuxDb == &p->aAuxDb[i] ){
24253 raw_printf(stderr, "cannot close the active database connection\n");
24254 rc = 1;
24255 }else if( p->aAuxDb[i].db ){
24256 session_close_all(p, i);
24257 close_db(p->aAuxDb[i].db);
24258 p->aAuxDb[i].db = 0;
24259 }
24260 }else{
24261 raw_printf(stderr, "Usage: .connection [close] [CONNECTION-NUMBER]\n");
24262 rc = 1;
24263 }
24264 }else
24265
24266 if( c=='c' && n==4 && cli_strncmp(azArg[0], "crnl", n)==0 ){
@@ -24270,13 +25017,13 @@
24270 }else{
24271 setBinaryMode(p->out, 1);
24272 }
24273 }else{
24274 #if !defined(_WIN32) && !defined(WIN32)
24275 raw_printf(stderr, "The \".crnl\" is a no-op on non-Windows machines.\n");
24276 #endif
24277 raw_printf(stderr, "Usage: .crnl on|off\n");
24278 rc = 1;
24279 }
24280 }else
24281
24282 if( c=='d' && n>1 && cli_strncmp(azArg[0], "databases", n)==0 ){
@@ -24285,11 +25032,11 @@
24285 sqlite3_stmt *pStmt;
24286 int i;
24287 open_db(p, 0);
24288 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
24289 if( rc ){
24290 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
24291 rc = 1;
24292 }else{
24293 while( sqlite3_step(pStmt)==SQLITE_ROW ){
24294 const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
24295 const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
@@ -24304,15 +25051,13 @@
24304 sqlite3_finalize(pStmt);
24305 for(i=0; i<nName; i++){
24306 int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
24307 int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
24308 const char *z = azName[i*2+1];
24309 utf8_printf(p->out, "%s: %s %s%s\n",
24310 azName[i*2],
24311 z && z[0] ? z : "\"\"",
24312 bRdonly ? "r/o" : "r/w",
24313 eTxn==SQLITE_TXN_NONE ? "" :
24314 eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
24315 free(azName[i*2]);
24316 free(azName[i*2+1]);
24317 }
24318 sqlite3_free(azName);
@@ -24348,16 +25093,16 @@
24348 if( nArg>1 && cli_strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
24349 if( nArg>=3 ){
24350 sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
24351 }
24352 sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
24353 utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
24354 if( nArg>1 ) break;
24355 }
24356 if( nArg>1 && ii==ArraySize(aDbConfig) ){
24357 utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
24358 utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
24359 }
24360 }else
24361
24362 #if SQLITE_SHELL_HAVE_RECOVER
24363 if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbinfo", n)==0 ){
@@ -24383,12 +25128,12 @@
24383 if( azArg[i][0]=='-' ){
24384 const char *z = azArg[i]+1;
24385 if( z[0]=='-' ) z++;
24386 if( cli_strcmp(z,"preserve-rowids")==0 ){
24387 #ifdef SQLITE_OMIT_VIRTUALTABLE
24388 raw_printf(stderr, "The --preserve-rowids option is not compatible"
24389 " with SQLITE_OMIT_VIRTUALTABLE\n");
24390 rc = 1;
24391 sqlite3_free(zLike);
24392 goto meta_command_exit;
24393 #else
24394 ShellSetFlag(p, SHFLG_PreserveRowid);
@@ -24402,11 +25147,11 @@
24402 }else
24403 if( cli_strcmp(z,"nosys")==0 ){
24404 ShellSetFlag(p, SHFLG_DumpNoSys);
24405 }else
24406 {
24407 raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
24408 rc = 1;
24409 sqlite3_free(zLike);
24410 goto meta_command_exit;
24411 }
24412 }else{
@@ -24436,12 +25181,12 @@
24436
24437 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
24438 /* When playing back a "dump", the content might appear in an order
24439 ** which causes immediate foreign key constraints to be violated.
24440 ** So disable foreign-key constraint enforcement to prevent problems. */
24441 raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
24442 raw_printf(p->out, "BEGIN TRANSACTION;\n");
24443 }
24444 p->writableSchema = 0;
24445 p->showHeader = 0;
24446 /* Set writable_schema=ON since doing so forces SQLite to initialize
24447 ** as much of the schema as it can even if the sqlite_schema table is
@@ -24468,27 +25213,27 @@
24468 run_table_dump_query(p, zSql);
24469 sqlite3_free(zSql);
24470 }
24471 sqlite3_free(zLike);
24472 if( p->writableSchema ){
24473 raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
24474 p->writableSchema = 0;
24475 }
24476 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
24477 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
24478 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
24479 raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
24480 }
24481 p->showHeader = savedShowHeader;
24482 p->shellFlgs = savedShellFlags;
24483 }else
24484
24485 if( c=='e' && cli_strncmp(azArg[0], "echo", n)==0 ){
24486 if( nArg==2 ){
24487 setOrClearFlag(p, SHFLG_Echo, azArg[1]);
24488 }else{
24489 raw_printf(stderr, "Usage: .echo on|off\n");
24490 rc = 1;
24491 }
24492 }else
24493
24494 if( c=='e' && cli_strncmp(azArg[0], "eqp", n)==0 ){
@@ -24515,11 +25260,11 @@
24515 #endif
24516 }else{
24517 p->autoEQP = (u8)booleanValue(azArg[1]);
24518 }
24519 }else{
24520 raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
24521 rc = 1;
24522 }
24523 }else
24524
24525 #ifndef SQLITE_SHELL_FIDDLE
@@ -24554,13 +25299,12 @@
24554 }else
24555
24556 #ifndef SQLITE_OMIT_VIRTUALTABLE
24557 if( c=='e' && cli_strncmp(azArg[0], "expert", n)==0 ){
24558 if( p->bSafeMode ){
24559 raw_printf(stderr,
24560 "Cannot run experimental commands such as \"%s\" in safe mode\n",
24561 azArg[0]);
24562 rc = 1;
24563 }else{
24564 open_db(p, 0);
24565 expertDotCommand(p, azArg, nArg);
24566 }
@@ -24612,14 +25356,13 @@
24612 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
24613 }
24614
24615 /* --help lists all file-controls */
24616 if( cli_strcmp(zCmd,"help")==0 ){
24617 utf8_printf(p->out, "Available file-controls:\n");
24618 for(i=0; i<ArraySize(aCtrl); i++){
24619 utf8_printf(p->out, " .filectrl %s %s\n",
24620 aCtrl[i].zCtrlName, aCtrl[i].zUsage);
24621 }
24622 rc = 1;
24623 goto meta_command_exit;
24624 }
24625
@@ -24630,20 +25373,20 @@
24630 if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
24631 if( filectrl<0 ){
24632 filectrl = aCtrl[i].ctrlCode;
24633 iCtrl = i;
24634 }else{
24635 utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
24636 "Use \".filectrl --help\" for help\n", zCmd);
24637 rc = 1;
24638 goto meta_command_exit;
24639 }
24640 }
24641 }
24642 if( filectrl<0 ){
24643 utf8_printf(stderr,"Error: unknown file-control: %s\n"
24644 "Use \".filectrl --help\" for help\n", zCmd);
24645 }else{
24646 switch(filectrl){
24647 case SQLITE_FCNTL_SIZE_LIMIT: {
24648 if( nArg!=2 && nArg!=3 ) break;
24649 iRes = nArg==3 ? integerValue(azArg[2]) : -1;
@@ -24682,11 +25425,11 @@
24682 case SQLITE_FCNTL_TEMPFILENAME: {
24683 char *z = 0;
24684 if( nArg!=2 ) break;
24685 sqlite3_file_control(p->db, zSchema, filectrl, &z);
24686 if( z ){
24687 utf8_printf(p->out, "%s\n", z);
24688 sqlite3_free(z);
24689 }
24690 isOk = 2;
24691 break;
24692 }
@@ -24696,23 +25439,23 @@
24696 x = atoi(azArg[2]);
24697 sqlite3_file_control(p->db, zSchema, filectrl, &x);
24698 }
24699 x = -1;
24700 sqlite3_file_control(p->db, zSchema, filectrl, &x);
24701 utf8_printf(p->out,"%d\n", x);
24702 isOk = 2;
24703 break;
24704 }
24705 }
24706 }
24707 if( isOk==0 && iCtrl>=0 ){
24708 utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
24709 rc = 1;
24710 }else if( isOk==1 ){
24711 char zBuf[100];
24712 sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
24713 raw_printf(p->out, "%s\n", zBuf);
24714 }
24715 }else
24716
24717 if( c=='f' && cli_strncmp(azArg[0], "fullschema", n)==0 ){
24718 ShellState data;
@@ -24723,11 +25466,11 @@
24723 if( nArg==2 && optionMatch(azArg[1], "indent") ){
24724 data.cMode = data.mode = MODE_Pretty;
24725 nArg = 1;
24726 }
24727 if( nArg!=1 ){
24728 raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
24729 rc = 1;
24730 goto meta_command_exit;
24731 }
24732 open_db(p, 0);
24733 rc = sqlite3_exec(p->db,
@@ -24749,37 +25492,37 @@
24749 doStats = sqlite3_step(pStmt)==SQLITE_ROW;
24750 sqlite3_finalize(pStmt);
24751 }
24752 }
24753 if( doStats==0 ){
24754 raw_printf(p->out, "/* No STAT tables available */\n");
24755 }else{
24756 raw_printf(p->out, "ANALYZE sqlite_schema;\n");
24757 data.cMode = data.mode = MODE_Insert;
24758 data.zDestTable = "sqlite_stat1";
24759 shell_exec(&data, "SELECT * FROM sqlite_stat1", 0);
24760 data.zDestTable = "sqlite_stat4";
24761 shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
24762 raw_printf(p->out, "ANALYZE sqlite_schema;\n");
24763 }
24764 }else
24765
24766 if( c=='h' && cli_strncmp(azArg[0], "headers", n)==0 ){
24767 if( nArg==2 ){
24768 p->showHeader = booleanValue(azArg[1]);
24769 p->shellFlgs |= SHFLG_HeaderSet;
24770 }else{
24771 raw_printf(stderr, "Usage: .headers on|off\n");
24772 rc = 1;
24773 }
24774 }else
24775
24776 if( c=='h' && cli_strncmp(azArg[0], "help", n)==0 ){
24777 if( nArg>=2 ){
24778 n = showHelp(p->out, azArg[1]);
24779 if( n==0 ){
24780 utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
24781 }
24782 }else{
24783 showHelp(p->out, 0);
24784 }
24785 }else
@@ -24819,11 +25562,11 @@
24819 if( zFile==0 ){
24820 zFile = z;
24821 }else if( zTable==0 ){
24822 zTable = z;
24823 }else{
24824 utf8_printf(p->out, "ERROR: extra argument: \"%s\". Usage:\n", z);
24825 showHelp(p->out, "import");
24826 goto meta_command_exit;
24827 }
24828 }else if( cli_strcmp(z,"-v")==0 ){
24829 eVerbose++;
@@ -24840,18 +25583,18 @@
24840 sCtx.cColSep = ',';
24841 sCtx.cRowSep = '\n';
24842 xRead = csv_read_one_field;
24843 useOutputMode = 0;
24844 }else{
24845 utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n", z);
24846 showHelp(p->out, "import");
24847 goto meta_command_exit;
24848 }
24849 }
24850 if( zTable==0 ){
24851 utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
24852 zFile==0 ? "FILE" : "TABLE");
24853 showHelp(p->out, "import");
24854 goto meta_command_exit;
24855 }
24856 seenInterrupt = 0;
24857 open_db(p, 0);
@@ -24858,24 +25601,21 @@
24858 if( useOutputMode ){
24859 /* If neither the --csv or --ascii options are specified, then set
24860 ** the column and row separator characters from the output mode. */
24861 nSep = strlen30(p->colSeparator);
24862 if( nSep==0 ){
24863 raw_printf(stderr,
24864 "Error: non-null column separator required for import\n");
24865 goto meta_command_exit;
24866 }
24867 if( nSep>1 ){
24868 raw_printf(stderr,
24869 "Error: multi-character column separators not allowed"
24870 " for import\n");
24871 goto meta_command_exit;
24872 }
24873 nSep = strlen30(p->rowSeparator);
24874 if( nSep==0 ){
24875 raw_printf(stderr,
24876 "Error: non-null row separator required for import\n");
24877 goto meta_command_exit;
24878 }
24879 if( nSep==2 && p->mode==MODE_Csv
24880 && cli_strcmp(p->rowSeparator,SEP_CrLf)==0
24881 ){
@@ -24885,22 +25625,22 @@
24885 ** and output row separators. */
24886 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
24887 nSep = strlen30(p->rowSeparator);
24888 }
24889 if( nSep>1 ){
24890 raw_printf(stderr, "Error: multi-character row separators not allowed"
24891 " for import\n");
24892 goto meta_command_exit;
24893 }
24894 sCtx.cColSep = (u8)p->colSeparator[0];
24895 sCtx.cRowSep = (u8)p->rowSeparator[0];
24896 }
24897 sCtx.zFile = zFile;
24898 sCtx.nLine = 1;
24899 if( sCtx.zFile[0]=='|' ){
24900 #ifdef SQLITE_OMIT_POPEN
24901 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
24902 goto meta_command_exit;
24903 #else
24904 sCtx.in = popen(sCtx.zFile+1, "r");
24905 sCtx.zFile = "<pipe>";
24906 sCtx.xCloser = pclose;
@@ -24908,23 +25648,23 @@
24908 }else{
24909 sCtx.in = fopen(sCtx.zFile, "rb");
24910 sCtx.xCloser = fclose;
24911 }
24912 if( sCtx.in==0 ){
24913 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
24914 goto meta_command_exit;
24915 }
24916 if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
24917 char zSep[2];
24918 zSep[1] = 0;
24919 zSep[0] = sCtx.cColSep;
24920 utf8_printf(p->out, "Column separator ");
24921 output_c_string(p->out, zSep);
24922 utf8_printf(p->out, ", row separator ");
24923 zSep[0] = sCtx.cRowSep;
24924 output_c_string(p->out, zSep);
24925 utf8_printf(p->out, "\n");
24926 }
24927 sCtx.z = sqlite3_malloc64(120);
24928 if( sCtx.z==0 ){
24929 import_cleanup(&sCtx);
24930 shell_out_of_memory();
@@ -24955,18 +25695,18 @@
24955 zAutoColumn(sCtx.z, &dbCols, 0);
24956 if( sCtx.cTerm!=sCtx.cColSep ) break;
24957 }
24958 zColDefs = zAutoColumn(0, &dbCols, &zRenames);
24959 if( zRenames!=0 ){
24960 utf8_printf((stdin_is_interactive && p->in==stdin)? p->out : stderr,
24961 "Columns renamed during .import %s due to duplicates:\n"
24962 "%s\n", sCtx.zFile, zRenames);
24963 sqlite3_free(zRenames);
24964 }
24965 assert(dbCols==0);
24966 if( zColDefs==0 ){
24967 utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
24968 import_fail:
24969 sqlite3_free(zCreate);
24970 sqlite3_free(zSql);
24971 sqlite3_free(zFullTabName);
24972 import_cleanup(&sCtx);
@@ -24973,24 +25713,24 @@
24973 rc = 1;
24974 goto meta_command_exit;
24975 }
24976 zCreate = sqlite3_mprintf("%z%z\n", zCreate, zColDefs);
24977 if( eVerbose>=1 ){
24978 utf8_printf(p->out, "%s\n", zCreate);
24979 }
24980 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
24981 if( rc ){
24982 utf8_printf(stderr, "%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db));
24983 goto import_fail;
24984 }
24985 sqlite3_free(zCreate);
24986 zCreate = 0;
24987 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
24988 }
24989 if( rc ){
24990 if (pStmt) sqlite3_finalize(pStmt);
24991 utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
24992 goto import_fail;
24993 }
24994 sqlite3_free(zSql);
24995 nCol = sqlite3_column_count(pStmt);
24996 sqlite3_finalize(pStmt);
@@ -25008,15 +25748,15 @@
25008 zSql[j++] = '?';
25009 }
25010 zSql[j++] = ')';
25011 zSql[j] = 0;
25012 if( eVerbose>=2 ){
25013 utf8_printf(p->out, "Insert using: %s\n", zSql);
25014 }
25015 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
25016 if( rc ){
25017 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
25018 if (pStmt) sqlite3_finalize(pStmt);
25019 goto import_fail;
25020 }
25021 sqlite3_free(zSql);
25022 sqlite3_free(zFullTabName);
@@ -25045,32 +25785,31 @@
25045 if( z==0 && (xRead==csv_read_one_field) && i==nCol-1 && i>0 ){
25046 z = "";
25047 }
25048 sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
25049 if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
25050 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
25051 "filling the rest with NULL\n",
25052 sCtx.zFile, startLine, nCol, i+1);
25053 i += 2;
25054 while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
25055 }
25056 }
25057 if( sCtx.cTerm==sCtx.cColSep ){
25058 do{
25059 xRead(&sCtx);
25060 i++;
25061 }while( sCtx.cTerm==sCtx.cColSep );
25062 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
25063 "extras ignored\n",
25064 sCtx.zFile, startLine, nCol, i);
25065 }
25066 if( i>=nCol ){
25067 sqlite3_step(pStmt);
25068 rc = sqlite3_reset(pStmt);
25069 if( rc!=SQLITE_OK ){
25070 utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
25071 startLine, sqlite3_errmsg(p->db));
25072 sCtx.nErr++;
25073 }else{
25074 sCtx.nRow++;
25075 }
25076 }
@@ -25078,13 +25817,12 @@
25078
25079 import_cleanup(&sCtx);
25080 sqlite3_finalize(pStmt);
25081 if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
25082 if( eVerbose>0 ){
25083 utf8_printf(p->out,
25084 "Added %d rows with %d errors using %d lines of input\n",
25085 sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
25086 }
25087 }else
25088 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
25089
25090 #ifndef SQLITE_UNTESTABLE
@@ -25095,18 +25833,18 @@
25095 int tnum = 0;
25096 int isWO = 0; /* True if making an imposter of a WITHOUT ROWID table */
25097 int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
25098 int i;
25099 if( !ShellHasFlag(p,SHFLG_TestingMode) ){
25100 utf8_printf(stderr, ".%s unavailable without --unsafe-testing\n",
25101 "imposter");
25102 rc = 1;
25103 goto meta_command_exit;
25104 }
25105 if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
25106 utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
25107 " .imposter off\n");
25108 /* Also allowed, but not documented:
25109 **
25110 ** .imposter TABLE IMPOSTER
25111 **
25112 ** where TABLE is a WITHOUT ROWID table. In that case, the
@@ -25161,11 +25899,11 @@
25161 zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
25162 }
25163 }
25164 sqlite3_finalize(pStmt);
25165 if( i==0 || tnum==0 ){
25166 utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
25167 rc = 1;
25168 sqlite3_free(zCollist);
25169 goto meta_command_exit;
25170 }
25171 if( lenPK==0 ) lenPK = 100000;
@@ -25176,20 +25914,18 @@
25176 rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
25177 if( rc==SQLITE_OK ){
25178 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
25179 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
25180 if( rc ){
25181 utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
25182 }else{
25183 utf8_printf(stdout, "%s;\n", zSql);
25184 raw_printf(stdout,
25185 "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
25186 azArg[1], isWO ? "table" : "index"
25187 );
25188 }
25189 }else{
25190 raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
25191 rc = 1;
25192 }
25193 sqlite3_free(zSql);
25194 }else
25195 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
@@ -25205,11 +25941,11 @@
25205 sqlite3IoTrace = iotracePrintf;
25206 iotrace = stdout;
25207 }else{
25208 iotrace = fopen(azArg[1], "w");
25209 if( iotrace==0 ){
25210 utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
25211 sqlite3IoTrace = 0;
25212 rc = 1;
25213 }else{
25214 sqlite3IoTrace = iotracePrintf;
25215 }
@@ -25237,15 +25973,15 @@
25237 };
25238 int i, n2;
25239 open_db(p, 0);
25240 if( nArg==1 ){
25241 for(i=0; i<ArraySize(aLimit); i++){
25242 printf("%20s %d\n", aLimit[i].zLimitName,
25243 sqlite3_limit(p->db, aLimit[i].limitCode, -1));
25244 }
25245 }else if( nArg>3 ){
25246 raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
25247 rc = 1;
25248 goto meta_command_exit;
25249 }else{
25250 int iLimit = -1;
25251 n2 = strlen30(azArg[1]);
@@ -25252,29 +25988,29 @@
25252 for(i=0; i<ArraySize(aLimit); i++){
25253 if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
25254 if( iLimit<0 ){
25255 iLimit = i;
25256 }else{
25257 utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
25258 rc = 1;
25259 goto meta_command_exit;
25260 }
25261 }
25262 }
25263 if( iLimit<0 ){
25264 utf8_printf(stderr, "unknown limit: \"%s\"\n"
25265 "enter \".limits\" with no arguments for a list.\n",
25266 azArg[1]);
25267 rc = 1;
25268 goto meta_command_exit;
25269 }
25270 if( nArg==3 ){
25271 sqlite3_limit(p->db, aLimit[iLimit].limitCode,
25272 (int)integerValue(azArg[2]));
25273 }
25274 printf("%20s %d\n", aLimit[iLimit].zLimitName,
25275 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
25276 }
25277 }else
25278
25279 if( c=='l' && n>2 && cli_strncmp(azArg[0], "lint", n)==0 ){
25280 open_db(p, 0);
@@ -25286,38 +26022,38 @@
25286 const char *zFile, *zProc;
25287 char *zErrMsg = 0;
25288 failIfSafeMode(p, "cannot run .load in safe mode");
25289 if( nArg<2 || azArg[1][0]==0 ){
25290 /* Must have a non-empty FILE. (Will not load self.) */
25291 raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
25292 rc = 1;
25293 goto meta_command_exit;
25294 }
25295 zFile = azArg[1];
25296 zProc = nArg>=3 ? azArg[2] : 0;
25297 open_db(p, 0);
25298 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
25299 if( rc!=SQLITE_OK ){
25300 utf8_printf(stderr, "Error: %s\n", zErrMsg);
25301 sqlite3_free(zErrMsg);
25302 rc = 1;
25303 }
25304 }else
25305 #endif
25306
25307 if( c=='l' && cli_strncmp(azArg[0], "log", n)==0 ){
25308 if( nArg!=2 ){
25309 raw_printf(stderr, "Usage: .log FILENAME\n");
25310 rc = 1;
25311 }else{
25312 const char *zFile = azArg[1];
25313 if( p->bSafeMode
25314 && cli_strcmp(zFile,"on")!=0
25315 && cli_strcmp(zFile,"off")!=0
25316 ){
25317 raw_printf(stdout, "cannot set .log to anything other "
25318 "than \"on\" or \"off\"\n");
25319 zFile = "off";
25320 }
25321 output_file_close(p->pLog);
25322 if( cli_strcmp(zFile,"on")==0 ) zFile = "stdout";
25323 p->pLog = output_file_open(zFile, 0);
@@ -25352,37 +26088,35 @@
25352 cmOpts = cmo;
25353 }
25354 }else if( zTabname==0 ){
25355 zTabname = z;
25356 }else if( z[0]=='-' ){
25357 utf8_printf(stderr, "unknown option: %s\n", z);
25358 utf8_printf(stderr, "options:\n"
25359 " --noquote\n"
25360 " --quote\n"
25361 " --wordwrap on/off\n"
25362 " --wrap N\n"
25363 " --ww\n");
25364 rc = 1;
25365 goto meta_command_exit;
25366 }else{
25367 utf8_printf(stderr, "extra argument: \"%s\"\n", z);
25368 rc = 1;
25369 goto meta_command_exit;
25370 }
25371 }
25372 if( zMode==0 ){
25373 if( p->mode==MODE_Column
25374 || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
25375 ){
25376 raw_printf
25377 (p->out,
25378 "current output mode: %s --wrap %d --wordwrap %s --%squote\n",
25379 modeDescr[p->mode], p->cmOpts.iWrap,
25380 p->cmOpts.bWordWrap ? "on" : "off",
25381 p->cmOpts.bQuote ? "" : "no");
25382 }else{
25383 raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
25384 }
25385 zMode = modeDescr[p->mode];
25386 }
25387 n2 = strlen30(zMode);
25388 if( cli_strncmp(zMode,"lines",n2)==0 ){
@@ -25437,26 +26171,26 @@
25437 }else if( cli_strncmp(zMode,"off",n2)==0 ){
25438 p->mode = MODE_Off;
25439 }else if( cli_strncmp(zMode,"json",n2)==0 ){
25440 p->mode = MODE_Json;
25441 }else{
25442 raw_printf(stderr, "Error: mode should be one of: "
25443 "ascii box column csv html insert json line list markdown "
25444 "qbox quote table tabs tcl\n");
25445 rc = 1;
25446 }
25447 p->cMode = p->mode;
25448 }else
25449
25450 #ifndef SQLITE_SHELL_FIDDLE
25451 if( c=='n' && cli_strcmp(azArg[0], "nonce")==0 ){
25452 if( nArg!=2 ){
25453 raw_printf(stderr, "Usage: .nonce NONCE\n");
25454 rc = 1;
25455 }else if( p->zNonce==0 || cli_strcmp(azArg[1],p->zNonce)!=0 ){
25456 raw_printf(stderr, "line %d: incorrect nonce: \"%s\"\n",
25457 p->lineno, azArg[1]);
25458 exit(1);
25459 }else{
25460 p->bSafeMode = 0;
25461 return 0; /* Return immediately to bypass the safe mode reset
25462 ** at the end of this procedure */
@@ -25467,11 +26201,11 @@
25467 if( c=='n' && cli_strncmp(azArg[0], "nullvalue", n)==0 ){
25468 if( nArg==2 ){
25469 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
25470 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
25471 }else{
25472 raw_printf(stderr, "Usage: .nullvalue STRING\n");
25473 rc = 1;
25474 }
25475 }else
25476
25477 if( c=='o' && cli_strncmp(azArg[0], "open", n)==0 && n>=2 ){
@@ -25506,15 +26240,15 @@
25506 p->szMax = integerValue(azArg[++iName]);
25507 #endif /* SQLITE_OMIT_DESERIALIZE */
25508 }else
25509 #endif /* !SQLITE_SHELL_FIDDLE */
25510 if( z[0]=='-' ){
25511 utf8_printf(stderr, "unknown option: %s\n", z);
25512 rc = 1;
25513 goto meta_command_exit;
25514 }else if( zFN ){
25515 utf8_printf(stderr, "extra argument: \"%s\"\n", z);
25516 rc = 1;
25517 goto meta_command_exit;
25518 }else{
25519 zFN = z;
25520 }
@@ -25552,11 +26286,11 @@
25552 zNewFilename = 0;
25553 }
25554 p->pAuxDb->zDbFilename = zNewFilename;
25555 open_db(p, OPEN_DB_KEEPALIVE);
25556 if( p->db==0 ){
25557 utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
25558 sqlite3_free(zNewFilename);
25559 }else{
25560 p->pAuxDb->zFreeOnClose = zNewFilename;
25561 }
25562 }
@@ -25576,13 +26310,13 @@
25576 char *zFile = 0;
25577 int bTxtMode = 0;
25578 int i;
25579 int eMode = 0;
25580 int bOnce = 0; /* 0: .output, 1: .once, 2: .excel */
25581 unsigned char zBOM[4]; /* Byte-order mark to using if --bom is present */
 
25582
25583 zBOM[0] = 0;
25584 failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
25585 if( c=='e' ){
25586 eMode = 'x';
25587 bOnce = 2;
25588 }else if( cli_strncmp(azArg[0],"once",n)==0 ){
@@ -25591,21 +26325,17 @@
25591 for(i=1; i<nArg; i++){
25592 char *z = azArg[i];
25593 if( z[0]=='-' ){
25594 if( z[1]=='-' ) z++;
25595 if( cli_strcmp(z,"-bom")==0 ){
25596 zBOM[0] = 0xef;
25597 zBOM[1] = 0xbb;
25598 zBOM[2] = 0xbf;
25599 zBOM[3] = 0;
25600 }else if( c!='e' && cli_strcmp(z,"-x")==0 ){
25601 eMode = 'x'; /* spreadsheet */
25602 }else if( c!='e' && cli_strcmp(z,"-e")==0 ){
25603 eMode = 'e'; /* text editor */
25604 }else{
25605 utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n",
25606 azArg[i]);
25607 showHelp(p->out, azArg[0]);
25608 rc = 1;
25609 goto meta_command_exit;
25610 }
25611 }else if( zFile==0 && eMode!='e' && eMode!='x' ){
@@ -25613,12 +26343,11 @@
25613 if( zFile && zFile[0]=='|' ){
25614 while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
25615 break;
25616 }
25617 }else{
25618 utf8_printf(p->out,"ERROR: extra parameter: \"%s\". Usage:\n",
25619 azArg[i]);
25620 showHelp(p->out, azArg[0]);
25621 rc = 1;
25622 sqlite3_free(zFile);
25623 goto meta_command_exit;
25624 }
@@ -25653,34 +26382,34 @@
25653 }
25654 #endif /* SQLITE_NOHAVE_SYSTEM */
25655 shell_check_oom(zFile);
25656 if( zFile[0]=='|' ){
25657 #ifdef SQLITE_OMIT_POPEN
25658 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
25659 rc = 1;
25660 p->out = stdout;
25661 #else
25662 p->out = popen(zFile + 1, "w");
25663 if( p->out==0 ){
25664 utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
25665 p->out = stdout;
25666 rc = 1;
25667 }else{
25668 if( zBOM[0] ) fwrite(zBOM, 1, 3, p->out);
 
25669 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
25670 }
25671 #endif
25672 }else{
25673 p->out = output_file_open(zFile, bTxtMode);
25674 if( p->out==0 ){
25675 if( cli_strcmp(zFile,"off")!=0 ){
25676 utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
25677 }
25678 p->out = stdout;
25679 rc = 1;
25680 } else {
25681 if( zBOM[0] ) fwrite(zBOM, 1, 3, p->out);
 
25682 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
25683 }
25684 }
25685 sqlite3_free(zFile);
25686 }else
@@ -25717,12 +26446,12 @@
25717 if( len ){
25718 rx = sqlite3_prepare_v2(p->db,
25719 "SELECT key, quote(value) "
25720 "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
25721 while( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
25722 utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
25723 sqlite3_column_text(pStmt,1));
25724 }
25725 sqlite3_finalize(pStmt);
25726 }
25727 }else
25728
@@ -25762,11 +26491,11 @@
25762 "VALUES(%Q,%Q);", zKey, zValue);
25763 shell_check_oom(zSql);
25764 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
25765 sqlite3_free(zSql);
25766 if( rx!=SQLITE_OK ){
25767 utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
25768 sqlite3_finalize(pStmt);
25769 pStmt = 0;
25770 rc = 1;
25771 }
25772 }
@@ -25791,14 +26520,14 @@
25791 }else
25792
25793 if( c=='p' && n>=3 && cli_strncmp(azArg[0], "print", n)==0 ){
25794 int i;
25795 for(i=1; i<nArg; i++){
25796 if( i>1 ) raw_printf(p->out, " ");
25797 utf8_printf(p->out, "%s", azArg[i]);
25798 }
25799 raw_printf(p->out, "\n");
25800 }else
25801
25802 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
25803 if( c=='p' && n>=3 && cli_strncmp(azArg[0], "progress", n)==0 ){
25804 int i;
@@ -25823,19 +26552,19 @@
25823 p->flgProgress |= SHELL_PROGRESS_ONCE;
25824 continue;
25825 }
25826 if( cli_strcmp(z,"limit")==0 ){
25827 if( i+1>=nArg ){
25828 utf8_printf(stderr, "Error: missing argument on --limit\n");
25829 rc = 1;
25830 goto meta_command_exit;
25831 }else{
25832 p->mxProgress = (int)integerValue(azArg[++i]);
25833 }
25834 continue;
25835 }
25836 utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
25837 rc = 1;
25838 goto meta_command_exit;
25839 }else{
25840 nn = (int)integerValue(z);
25841 }
@@ -25864,31 +26593,31 @@
25864 if( c=='r' && n>=3 && cli_strncmp(azArg[0], "read", n)==0 ){
25865 FILE *inSaved = p->in;
25866 int savedLineno = p->lineno;
25867 failIfSafeMode(p, "cannot run .read in safe mode");
25868 if( nArg!=2 ){
25869 raw_printf(stderr, "Usage: .read FILE\n");
25870 rc = 1;
25871 goto meta_command_exit;
25872 }
25873 if( azArg[1][0]=='|' ){
25874 #ifdef SQLITE_OMIT_POPEN
25875 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
25876 rc = 1;
25877 p->out = stdout;
25878 #else
25879 p->in = popen(azArg[1]+1, "r");
25880 if( p->in==0 ){
25881 utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
25882 rc = 1;
25883 }else{
25884 rc = process_input(p);
25885 pclose(p->in);
25886 }
25887 #endif
25888 }else if( (p->in = openChrSource(azArg[1]))==0 ){
25889 utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
25890 rc = 1;
25891 }else{
25892 rc = process_input(p);
25893 fclose(p->in);
25894 }
@@ -25911,24 +26640,24 @@
25911 zDb = "main";
25912 }else if( nArg==3 ){
25913 zSrcFile = azArg[2];
25914 zDb = azArg[1];
25915 }else{
25916 raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
25917 rc = 1;
25918 goto meta_command_exit;
25919 }
25920 rc = sqlite3_open(zSrcFile, &pSrc);
25921 if( rc!=SQLITE_OK ){
25922 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
25923 close_db(pSrc);
25924 return 1;
25925 }
25926 open_db(p, 0);
25927 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
25928 if( pBackup==0 ){
25929 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
25930 close_db(pSrc);
25931 return 1;
25932 }
25933 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
25934 || rc==SQLITE_BUSY ){
@@ -25939,14 +26668,14 @@
25939 }
25940 sqlite3_backup_finish(pBackup);
25941 if( rc==SQLITE_DONE ){
25942 rc = 0;
25943 }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
25944 raw_printf(stderr, "Error: source database is busy\n");
25945 rc = 1;
25946 }else{
25947 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
25948 rc = 1;
25949 }
25950 close_db(pSrc);
25951 }else
25952 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
@@ -25963,15 +26692,19 @@
25963 }
25964 open_db(p, 0);
25965 sqlite3_db_config(
25966 p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, p->scanstatsOn, (int*)0
25967 );
25968 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
25969 raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
 
 
 
 
25970 #endif
25971 }else{
25972 raw_printf(stderr, "Usage: .scanstats on|off|est\n");
25973 rc = 1;
25974 }
25975 }else
25976
25977 if( c=='s' && cli_strncmp(azArg[0], "schema", n)==0 ){
@@ -25996,18 +26729,17 @@
25996 }else if( optionMatch(azArg[ii],"debug") ){
25997 bDebug = 1;
25998 }else if( optionMatch(azArg[ii],"nosys") ){
25999 bNoSystemTabs = 1;
26000 }else if( azArg[ii][0]=='-' ){
26001 utf8_printf(stderr, "Unknown option: \"%s\"\n", azArg[ii]);
26002 rc = 1;
26003 goto meta_command_exit;
26004 }else if( zName==0 ){
26005 zName = azArg[ii];
26006 }else{
26007 raw_printf(stderr,
26008 "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
26009 rc = 1;
26010 goto meta_command_exit;
26011 }
26012 }
26013 if( zName!=0 ){
@@ -26036,11 +26768,11 @@
26036 if( zDiv ){
26037 sqlite3_stmt *pStmt = 0;
26038 rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
26039 -1, &pStmt, 0);
26040 if( rc ){
26041 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
26042 sqlite3_finalize(pStmt);
26043 rc = 1;
26044 goto meta_command_exit;
26045 }
26046 appendText(&sSelect, "SELECT sql FROM", 0);
@@ -26098,22 +26830,22 @@
26098 appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
26099 }
26100 appendText(&sSelect, "sql IS NOT NULL"
26101 " ORDER BY snum, rowid", 0);
26102 if( bDebug ){
26103 utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
26104 }else{
26105 rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
26106 }
26107 freeText(&sSelect);
26108 }
26109 if( zErrMsg ){
26110 utf8_printf(stderr,"Error: %s\n", zErrMsg);
26111 sqlite3_free(zErrMsg);
26112 rc = 1;
26113 }else if( rc != SQLITE_OK ){
26114 raw_printf(stderr,"Error: querying schema information\n");
26115 rc = 1;
26116 }else{
26117 rc = 0;
26118 }
26119 }else
@@ -26155,15 +26887,15 @@
26155 */
26156 if( cli_strcmp(azCmd[0],"attach")==0 ){
26157 if( nCmd!=2 ) goto session_syntax_error;
26158 if( pSession->p==0 ){
26159 session_not_open:
26160 raw_printf(stderr, "ERROR: No sessions are open\n");
26161 }else{
26162 rc = sqlite3session_attach(pSession->p, azCmd[1]);
26163 if( rc ){
26164 raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
26165 rc = 0;
26166 }
26167 }
26168 }else
26169
@@ -26178,28 +26910,27 @@
26178 failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]);
26179 if( nCmd!=2 ) goto session_syntax_error;
26180 if( pSession->p==0 ) goto session_not_open;
26181 out = fopen(azCmd[1], "wb");
26182 if( out==0 ){
26183 utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
26184 azCmd[1]);
26185 }else{
26186 int szChng;
26187 void *pChng;
26188 if( azCmd[0][0]=='c' ){
26189 rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
26190 }else{
26191 rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
26192 }
26193 if( rc ){
26194 printf("Error: error code %d\n", rc);
26195 rc = 0;
26196 }
26197 if( pChng
26198 && fwrite(pChng, szChng, 1, out)!=1 ){
26199 raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
26200 szChng);
26201 }
26202 sqlite3_free(pChng);
26203 fclose(out);
26204 }
26205 }else
@@ -26222,12 +26953,11 @@
26222 int ii;
26223 if( nCmd>2 ) goto session_syntax_error;
26224 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
26225 if( pAuxDb->nSession ){
26226 ii = sqlite3session_enable(pSession->p, ii);
26227 utf8_printf(p->out, "session %s enable flag = %d\n",
26228 pSession->zName, ii);
26229 }
26230 }else
26231
26232 /* .session filter GLOB ....
26233 ** Set a list of GLOB patterns of table names to be excluded.
@@ -26240,14 +26970,11 @@
26240 sqlite3_free(pSession->azFilter[ii]);
26241 }
26242 sqlite3_free(pSession->azFilter);
26243 nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
26244 pSession->azFilter = sqlite3_malloc( nByte );
26245 if( pSession->azFilter==0 ){
26246 raw_printf(stderr, "Error: out or memory\n");
26247 exit(1);
26248 }
26249 for(ii=1; ii<nCmd; ii++){
26250 char *x = pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
26251 shell_check_oom(x);
26252 }
26253 pSession->nFilter = ii-1;
@@ -26261,12 +26988,11 @@
26261 int ii;
26262 if( nCmd>2 ) goto session_syntax_error;
26263 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
26264 if( pAuxDb->nSession ){
26265 ii = sqlite3session_indirect(pSession->p, ii);
26266 utf8_printf(p->out, "session %s indirect flag = %d\n",
26267 pSession->zName, ii);
26268 }
26269 }else
26270
26271 /* .session isempty
26272 ** Determine if the session is empty
@@ -26274,21 +27000,20 @@
26274 if( cli_strcmp(azCmd[0], "isempty")==0 ){
26275 int ii;
26276 if( nCmd!=1 ) goto session_syntax_error;
26277 if( pAuxDb->nSession ){
26278 ii = sqlite3session_isempty(pSession->p);
26279 utf8_printf(p->out, "session %s isempty flag = %d\n",
26280 pSession->zName, ii);
26281 }
26282 }else
26283
26284 /* .session list
26285 ** List all currently open sessions
26286 */
26287 if( cli_strcmp(azCmd[0],"list")==0 ){
26288 for(i=0; i<pAuxDb->nSession; i++){
26289 utf8_printf(p->out, "%d %s\n", i, pAuxDb->aSession[i].zName);
26290 }
26291 }else
26292
26293 /* .session open DB NAME
26294 ** Open a new session called NAME on the attached database DB.
@@ -26299,23 +27024,22 @@
26299 if( nCmd!=3 ) goto session_syntax_error;
26300 zName = azCmd[2];
26301 if( zName[0]==0 ) goto session_syntax_error;
26302 for(i=0; i<pAuxDb->nSession; i++){
26303 if( cli_strcmp(pAuxDb->aSession[i].zName,zName)==0 ){
26304 utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
26305 goto meta_command_exit;
26306 }
26307 }
26308 if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){
26309 raw_printf(stderr,
26310 "Maximum of %d sessions\n", ArraySize(pAuxDb->aSession));
26311 goto meta_command_exit;
26312 }
26313 pSession = &pAuxDb->aSession[pAuxDb->nSession];
26314 rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
26315 if( rc ){
26316 raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
26317 rc = 0;
26318 goto meta_command_exit;
26319 }
26320 pSession->nFilter = 0;
26321 sqlite3session_table_filter(pSession->p, session_filter, pSession);
@@ -26335,20 +27059,20 @@
26335 if( c=='s' && n>=10 && cli_strncmp(azArg[0], "selftest-", 9)==0 ){
26336 if( cli_strncmp(azArg[0]+9, "boolean", n-9)==0 ){
26337 int i, v;
26338 for(i=1; i<nArg; i++){
26339 v = booleanValue(azArg[i]);
26340 utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
26341 }
26342 }
26343 if( cli_strncmp(azArg[0]+9, "integer", n-9)==0 ){
26344 int i; sqlite3_int64 v;
26345 for(i=1; i<nArg; i++){
26346 char zBuf[200];
26347 v = integerValue(azArg[i]);
26348 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
26349 utf8_printf(p->out, "%s", zBuf);
26350 }
26351 }
26352 }else
26353 #endif
26354
@@ -26371,13 +27095,12 @@
26371 }else
26372 if( cli_strcmp(z,"-v")==0 ){
26373 bVerbose++;
26374 }else
26375 {
26376 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
26377 azArg[i], azArg[0]);
26378 raw_printf(stderr, "Should be one of: --init -v\n");
26379 rc = 1;
26380 goto meta_command_exit;
26381 }
26382 }
26383 if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
@@ -26402,11 +27125,11 @@
26402 "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
26403 " (1,'run','PRAGMA integrity_check','ok')",
26404 -1, &pStmt, 0);
26405 }
26406 if( rc ){
26407 raw_printf(stderr, "Error querying the selftest table\n");
26408 rc = 1;
26409 sqlite3_finalize(pStmt);
26410 goto meta_command_exit;
26411 }
26412 for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
@@ -26418,52 +27141,51 @@
26418 if( zOp==0 ) continue;
26419 if( zSql==0 ) continue;
26420 if( zAns==0 ) continue;
26421 k = 0;
26422 if( bVerbose>0 ){
26423 printf("%d: %s %s\n", tno, zOp, zSql);
26424 }
26425 if( cli_strcmp(zOp,"memo")==0 ){
26426 utf8_printf(p->out, "%s\n", zSql);
26427 }else
26428 if( cli_strcmp(zOp,"run")==0 ){
26429 char *zErrMsg = 0;
26430 str.n = 0;
26431 str.z[0] = 0;
26432 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
26433 nTest++;
26434 if( bVerbose ){
26435 utf8_printf(p->out, "Result: %s\n", str.z);
26436 }
26437 if( rc || zErrMsg ){
26438 nErr++;
26439 rc = 1;
26440 utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
26441 sqlite3_free(zErrMsg);
26442 }else if( cli_strcmp(zAns,str.z)!=0 ){
26443 nErr++;
26444 rc = 1;
26445 utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
26446 utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z);
26447 }
26448 }else
26449 {
26450 utf8_printf(stderr,
26451 "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
26452 rc = 1;
26453 break;
26454 }
26455 } /* End loop over rows of content from SELFTEST */
26456 sqlite3_finalize(pStmt);
26457 } /* End loop over k */
26458 freeText(&str);
26459 utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
26460 }else
26461
26462 if( c=='s' && cli_strncmp(azArg[0], "separator", n)==0 ){
26463 if( nArg<2 || nArg>3 ){
26464 raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
26465 rc = 1;
26466 }
26467 if( nArg>=2 ){
26468 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
26469 "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
@@ -26502,18 +27224,17 @@
26502 }else
26503 if( cli_strcmp(z,"debug")==0 ){
26504 bDebug = 1;
26505 }else
26506 {
26507 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
26508 azArg[i], azArg[0]);
26509 showHelp(p->out, azArg[0]);
26510 rc = 1;
26511 goto meta_command_exit;
26512 }
26513 }else if( zLike ){
26514 raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
26515 rc = 1;
26516 goto meta_command_exit;
26517 }else{
26518 zLike = z;
26519 bSeparate = 1;
@@ -26581,11 +27302,11 @@
26581 }
26582 shell_check_oom(zSql);
26583 freeText(&sQuery);
26584 freeText(&sSql);
26585 if( bDebug ){
26586 utf8_printf(p->out, "%s\n", zSql);
26587 }else{
26588 shell_exec(p, zSql, 0);
26589 }
26590 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && !defined(SQLITE_OMIT_VIRTUALTABLE)
26591 {
@@ -26611,11 +27332,11 @@
26611 "||group_concat('CAST(CAST('||cname||' AS BLOB) AS TEXT)<>'||cname\n"
26612 "|| ' AND typeof('||cname||')=''text'' ',\n"
26613 "' OR ') as query, tname from tabcols group by tname)"
26614 , zRevText);
26615 shell_check_oom(zRevText);
26616 if( bDebug ) utf8_printf(p->out, "%s\n", zRevText);
26617 lrc = sqlite3_prepare_v2(p->db, zRevText, -1, &pStmt, 0);
26618 if( lrc!=SQLITE_OK ){
26619 /* assert(lrc==SQLITE_NOMEM); // might also be SQLITE_ERROR if the
26620 ** user does cruel and unnatural things like ".limit expr_depth 0". */
26621 rc = 1;
@@ -26624,29 +27345,28 @@
26624 lrc = SQLITE_ROW==sqlite3_step(pStmt);
26625 if( lrc ){
26626 const char *zGenQuery = (char*)sqlite3_column_text(pStmt,0);
26627 sqlite3_stmt *pCheckStmt;
26628 lrc = sqlite3_prepare_v2(p->db, zGenQuery, -1, &pCheckStmt, 0);
26629 if( bDebug ) utf8_printf(p->out, "%s\n", zGenQuery);
26630 if( lrc!=SQLITE_OK ){
26631 rc = 1;
26632 }else{
26633 if( SQLITE_ROW==sqlite3_step(pCheckStmt) ){
26634 double countIrreversible = sqlite3_column_double(pCheckStmt, 0);
26635 if( countIrreversible>0 ){
26636 int sz = (int)(countIrreversible + 0.5);
26637 utf8_printf(stderr,
26638 "Digest includes %d invalidly encoded text field%s.\n",
26639 sz, (sz>1)? "s": "");
26640 }
26641 }
26642 sqlite3_finalize(pCheckStmt);
26643 }
26644 sqlite3_finalize(pStmt);
26645 }
26646 }
26647 if( rc ) utf8_printf(stderr, ".sha3sum failed.\n");
26648 sqlite3_free(zRevText);
26649 }
26650 #endif /* !defined(*_OMIT_SCHEMA_PRAGMAS) && !defined(*_OMIT_VIRTUALTABLE) */
26651 sqlite3_free(zSql);
26652 }else
@@ -26658,76 +27378,77 @@
26658 ){
26659 char *zCmd;
26660 int i, x;
26661 failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
26662 if( nArg<2 ){
26663 raw_printf(stderr, "Usage: .system COMMAND\n");
26664 rc = 1;
26665 goto meta_command_exit;
26666 }
26667 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
26668 for(i=2; i<nArg && zCmd!=0; i++){
26669 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
26670 zCmd, azArg[i]);
26671 }
 
26672 x = zCmd!=0 ? system(zCmd) : 1;
 
26673 sqlite3_free(zCmd);
26674 if( x ) raw_printf(stderr, "System command returns %d\n", x);
26675 }else
26676 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) */
26677
26678 if( c=='s' && cli_strncmp(azArg[0], "show", n)==0 ){
26679 static const char *azBool[] = { "off", "on", "trigger", "full"};
26680 const char *zOut;
26681 int i;
26682 if( nArg!=1 ){
26683 raw_printf(stderr, "Usage: .show\n");
26684 rc = 1;
26685 goto meta_command_exit;
26686 }
26687 utf8_printf(p->out, "%12.12s: %s\n","echo",
26688 azBool[ShellHasFlag(p, SHFLG_Echo)]);
26689 utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
26690 utf8_printf(p->out, "%12.12s: %s\n","explain",
26691 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
26692 utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
26693 if( p->mode==MODE_Column
26694 || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
26695 ){
26696 utf8_printf
26697 (p->out, "%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode",
26698 modeDescr[p->mode], p->cmOpts.iWrap,
26699 p->cmOpts.bWordWrap ? "on" : "off",
26700 p->cmOpts.bQuote ? "" : "no");
26701 }else{
26702 utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
26703 }
26704 utf8_printf(p->out, "%12.12s: ", "nullvalue");
26705 output_c_string(p->out, p->nullValue);
26706 raw_printf(p->out, "\n");
26707 utf8_printf(p->out,"%12.12s: %s\n","output",
26708 strlen30(p->outfile) ? p->outfile : "stdout");
26709 utf8_printf(p->out,"%12.12s: ", "colseparator");
26710 output_c_string(p->out, p->colSeparator);
26711 raw_printf(p->out, "\n");
26712 utf8_printf(p->out,"%12.12s: ", "rowseparator");
26713 output_c_string(p->out, p->rowSeparator);
26714 raw_printf(p->out, "\n");
26715 switch( p->statsOn ){
26716 case 0: zOut = "off"; break;
26717 default: zOut = "on"; break;
26718 case 2: zOut = "stmt"; break;
26719 case 3: zOut = "vmstep"; break;
26720 }
26721 utf8_printf(p->out, "%12.12s: %s\n","stats", zOut);
26722 utf8_printf(p->out, "%12.12s: ", "width");
26723 for (i=0;i<p->nWidth;i++) {
26724 raw_printf(p->out, "%d ", p->colWidth[i]);
26725 }
26726 raw_printf(p->out, "\n");
26727 utf8_printf(p->out, "%12.12s: %s\n", "filename",
26728 p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : "");
26729 }else
26730
26731 if( c=='s' && cli_strncmp(azArg[0], "stats", n)==0 ){
26732 if( nArg==2 ){
26733 if( cli_strcmp(azArg[1],"stmt")==0 ){
@@ -26738,11 +27459,11 @@
26738 p->statsOn = (u8)booleanValue(azArg[1]);
26739 }
26740 }else if( nArg==1 ){
26741 display_stats(p->db, p, 0);
26742 }else{
26743 raw_printf(stderr, "Usage: .stats ?on|off|stmt|vmstep?\n");
26744 rc = 1;
26745 }
26746 }else
26747
26748 if( (c=='t' && n>1 && cli_strncmp(azArg[0], "tables", n)==0)
@@ -26764,11 +27485,11 @@
26764
26765 if( nArg>2 && c=='i' ){
26766 /* It is an historical accident that the .indexes command shows an error
26767 ** when called with the wrong number of arguments whereas the .tables
26768 ** command does not. */
26769 raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
26770 rc = 1;
26771 sqlite3_finalize(pStmt);
26772 goto meta_command_exit;
26773 }
26774 for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
@@ -26840,14 +27561,13 @@
26840 if( nPrintCol<1 ) nPrintCol = 1;
26841 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
26842 for(i=0; i<nPrintRow; i++){
26843 for(j=i; j<nRow; j+=nPrintRow){
26844 char *zSp = j<nPrintRow ? "" : " ";
26845 utf8_printf(p->out, "%s%-*s", zSp, maxlen,
26846 azResult[j] ? azResult[j]:"");
26847 }
26848 raw_printf(p->out, "\n");
26849 }
26850 }
26851
26852 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
26853 sqlite3_free(azResult);
@@ -26857,11 +27577,11 @@
26857 /* Begin redirecting output to the file "testcase-out.txt" */
26858 if( c=='t' && cli_strcmp(azArg[0],"testcase")==0 ){
26859 output_reset(p);
26860 p->out = output_file_open("testcase-out.txt", 0);
26861 if( p->out==0 ){
26862 raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
26863 }
26864 if( nArg>=2 ){
26865 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
26866 }else{
26867 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
@@ -26898,11 +27618,11 @@
26898 {"prng_save", SQLITE_TESTCTRL_PRNG_SAVE, 0, "" },
26899 {"prng_seed", SQLITE_TESTCTRL_PRNG_SEED, 0, "SEED ?db?" },
26900 {"seek_count", SQLITE_TESTCTRL_SEEK_COUNT, 0, "" },
26901 {"sorter_mmap", SQLITE_TESTCTRL_SORTER_MMAP, 0, "NMAX" },
26902 {"tune", SQLITE_TESTCTRL_TUNE, 1, "ID VALUE" },
26903 {"uselongdouble", SQLITE_TESTCTRL_USELONGDOUBLE,0,"?BOOLEAN|\"default\"?"},
26904 };
26905 int testctrl = -1;
26906 int iCtrl = -1;
26907 int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */
26908 int isOk = 0;
@@ -26918,15 +27638,15 @@
26918 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
26919 }
26920
26921 /* --help lists all test-controls */
26922 if( cli_strcmp(zCmd,"help")==0 ){
26923 utf8_printf(p->out, "Available test-controls:\n");
26924 for(i=0; i<ArraySize(aCtrl); i++){
26925 if( aCtrl[i].unSafe && !ShellHasFlag(p,SHFLG_TestingMode) ) continue;
26926 utf8_printf(p->out, " .testctrl %s %s\n",
26927 aCtrl[i].zCtrlName, aCtrl[i].zUsage);
26928 }
26929 rc = 1;
26930 goto meta_command_exit;
26931 }
26932
@@ -26938,20 +27658,20 @@
26938 if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
26939 if( testctrl<0 ){
26940 testctrl = aCtrl[i].ctrlCode;
26941 iCtrl = i;
26942 }else{
26943 utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
26944 "Use \".testctrl --help\" for help\n", zCmd);
26945 rc = 1;
26946 goto meta_command_exit;
26947 }
26948 }
26949 }
26950 if( testctrl<0 ){
26951 utf8_printf(stderr,"Error: unknown test-control: %s\n"
26952 "Use \".testctrl --help\" for help\n", zCmd);
26953 }else{
26954 switch(testctrl){
26955
26956 /* sqlite3_test_control(int, db, int) */
26957 case SQLITE_TESTCTRL_OPTIMIZATIONS:
@@ -26987,11 +27707,11 @@
26987 if( nArg==3 || nArg==4 ){
26988 int ii = (int)integerValue(azArg[2]);
26989 sqlite3 *db;
26990 if( ii==0 && cli_strcmp(azArg[2],"random")==0 ){
26991 sqlite3_randomness(sizeof(ii),&ii);
26992 printf("-- random seed: %d\n", ii);
26993 }
26994 if( nArg==3 ){
26995 db = 0;
26996 }else{
26997 db = p->db;
@@ -27055,11 +27775,11 @@
27055 break;
27056
27057 case SQLITE_TESTCTRL_SEEK_COUNT: {
27058 u64 x = 0;
27059 rc2 = sqlite3_test_control(testctrl, p->db, &x);
27060 utf8_printf(p->out, "%llu\n", x);
27061 isOk = 3;
27062 break;
27063 }
27064
27065 #ifdef YYCOVERAGE
@@ -27086,15 +27806,15 @@
27086 int id = 1;
27087 while(1){
27088 int val = 0;
27089 rc2 = sqlite3_test_control(testctrl, -id, &val);
27090 if( rc2!=SQLITE_OK ) break;
27091 if( id>1 ) utf8_printf(p->out, " ");
27092 utf8_printf(p->out, "%d: %d", id, val);
27093 id++;
27094 }
27095 if( id>1 ) utf8_printf(p->out, "\n");
27096 isOk = 3;
27097 }
27098 break;
27099 }
27100 #endif
@@ -27106,16 +27826,16 @@
27106 }
27107 break;
27108 }
27109 }
27110 if( isOk==0 && iCtrl>=0 ){
27111 utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
27112 rc = 1;
27113 }else if( isOk==1 ){
27114 raw_printf(p->out, "%d\n", rc2);
27115 }else if( isOk==2 ){
27116 raw_printf(p->out, "0x%08x\n", rc2);
27117 }
27118 }else
27119 #endif /* !defined(SQLITE_UNTESTABLE) */
27120
27121 if( c=='t' && n>4 && cli_strncmp(azArg[0], "timeout", n)==0 ){
@@ -27125,15 +27845,15 @@
27125
27126 if( c=='t' && n>=5 && cli_strncmp(azArg[0], "timer", n)==0 ){
27127 if( nArg==2 ){
27128 enableTimer = booleanValue(azArg[1]);
27129 if( enableTimer && !HAS_TIMER ){
27130 raw_printf(stderr, "Error: timer not available on this system.\n");
27131 enableTimer = 0;
27132 }
27133 }else{
27134 raw_printf(stderr, "Usage: .timer on|off\n");
27135 rc = 1;
27136 }
27137 }else
27138
27139 #ifndef SQLITE_OMIT_TRACE
@@ -27166,11 +27886,11 @@
27166 }
27167 else if( optionMatch(z, "close") ){
27168 mType |= SQLITE_TRACE_CLOSE;
27169 }
27170 else {
27171 raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
27172 rc = 1;
27173 goto meta_command_exit;
27174 }
27175 }else{
27176 output_file_close(p->traceOut);
@@ -27190,11 +27910,11 @@
27190 if( c=='u' && cli_strncmp(azArg[0], "unmodule", n)==0 ){
27191 int ii;
27192 int lenOpt;
27193 char *zOpt;
27194 if( nArg<2 ){
27195 raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
27196 rc = 1;
27197 goto meta_command_exit;
27198 }
27199 open_db(p, 0);
27200 zOpt = azArg[1];
@@ -27212,100 +27932,100 @@
27212 #endif
27213
27214 #if SQLITE_USER_AUTHENTICATION
27215 if( c=='u' && cli_strncmp(azArg[0], "user", n)==0 ){
27216 if( nArg<2 ){
27217 raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
27218 rc = 1;
27219 goto meta_command_exit;
27220 }
27221 open_db(p, 0);
27222 if( cli_strcmp(azArg[1],"login")==0 ){
27223 if( nArg!=4 ){
27224 raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
27225 rc = 1;
27226 goto meta_command_exit;
27227 }
27228 rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
27229 strlen30(azArg[3]));
27230 if( rc ){
27231 utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
27232 rc = 1;
27233 }
27234 }else if( cli_strcmp(azArg[1],"add")==0 ){
27235 if( nArg!=5 ){
27236 raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
27237 rc = 1;
27238 goto meta_command_exit;
27239 }
27240 rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
27241 booleanValue(azArg[4]));
27242 if( rc ){
27243 raw_printf(stderr, "User-Add failed: %d\n", rc);
27244 rc = 1;
27245 }
27246 }else if( cli_strcmp(azArg[1],"edit")==0 ){
27247 if( nArg!=5 ){
27248 raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
27249 rc = 1;
27250 goto meta_command_exit;
27251 }
27252 rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
27253 booleanValue(azArg[4]));
27254 if( rc ){
27255 raw_printf(stderr, "User-Edit failed: %d\n", rc);
27256 rc = 1;
27257 }
27258 }else if( cli_strcmp(azArg[1],"delete")==0 ){
27259 if( nArg!=3 ){
27260 raw_printf(stderr, "Usage: .user delete USER\n");
27261 rc = 1;
27262 goto meta_command_exit;
27263 }
27264 rc = sqlite3_user_delete(p->db, azArg[2]);
27265 if( rc ){
27266 raw_printf(stderr, "User-Delete failed: %d\n", rc);
27267 rc = 1;
27268 }
27269 }else{
27270 raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
27271 rc = 1;
27272 goto meta_command_exit;
27273 }
27274 }else
27275 #endif /* SQLITE_USER_AUTHENTICATION */
27276
27277 if( c=='v' && cli_strncmp(azArg[0], "version", n)==0 ){
27278 char *zPtrSz = sizeof(void*)==8 ? "64-bit" : "32-bit";
27279 utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
27280 sqlite3_libversion(), sqlite3_sourceid());
27281 #if SQLITE_HAVE_ZLIB
27282 utf8_printf(p->out, "zlib version %s\n", zlibVersion());
27283 #endif
27284 #define CTIMEOPT_VAL_(opt) #opt
27285 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
27286 #if defined(__clang__) && defined(__clang_major__)
27287 utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
27288 CTIMEOPT_VAL(__clang_minor__) "."
27289 CTIMEOPT_VAL(__clang_patchlevel__) " (%s)\n", zPtrSz);
27290 #elif defined(_MSC_VER)
27291 utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) " (%s)\n", zPtrSz);
27292 #elif defined(__GNUC__) && defined(__VERSION__)
27293 utf8_printf(p->out, "gcc-" __VERSION__ " (%s)\n", zPtrSz);
27294 #endif
27295 }else
27296
27297 if( c=='v' && cli_strncmp(azArg[0], "vfsinfo", n)==0 ){
27298 const char *zDbName = nArg==2 ? azArg[1] : "main";
27299 sqlite3_vfs *pVfs = 0;
27300 if( p->db ){
27301 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
27302 if( pVfs ){
27303 utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName);
27304 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
27305 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
27306 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
27307 }
27308 }
27309 }else
27310
27311 if( c=='v' && cli_strncmp(azArg[0], "vfslist", n)==0 ){
@@ -27313,17 +28033,17 @@
27313 sqlite3_vfs *pCurrent = 0;
27314 if( p->db ){
27315 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
27316 }
27317 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
27318 utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName,
27319 pVfs==pCurrent ? " <--- CURRENT" : "");
27320 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
27321 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
27322 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
27323 if( pVfs->pNext ){
27324 raw_printf(p->out, "-----------------------------------\n");
27325 }
27326 }
27327 }else
27328
27329 if( c=='v' && cli_strncmp(azArg[0], "vfsname", n)==0 ){
@@ -27330,11 +28050,11 @@
27330 const char *zDbName = nArg==2 ? azArg[1] : "main";
27331 char *zVfsName = 0;
27332 if( p->db ){
27333 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
27334 if( zVfsName ){
27335 utf8_printf(p->out, "%s\n", zVfsName);
27336 sqlite3_free(zVfsName);
27337 }
27338 }
27339 }else
27340
@@ -27354,12 +28074,12 @@
27354 p->colWidth[j-1] = (int)integerValue(azArg[j]);
27355 }
27356 }else
27357
27358 {
27359 utf8_printf(stderr, "Error: unknown command or invalid arguments: "
27360 " \"%s\". Enter \".help\" for help\n", azArg[0]);
27361 rc = 1;
27362 }
27363
27364 meta_command_exit:
27365 if( p->outCount ){
@@ -27545,26 +28265,26 @@
27545 sqlite3_snprintf(sizeof(zPrefix), zPrefix,
27546 "%s near line %d:", zErrorType, startline);
27547 }else{
27548 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:", zErrorType);
27549 }
27550 utf8_printf(stderr, "%s %s\n", zPrefix, zErrorTail);
27551 sqlite3_free(zErrMsg);
27552 zErrMsg = 0;
27553 return 1;
27554 }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
27555 char zLineBuf[2000];
27556 sqlite3_snprintf(sizeof(zLineBuf), zLineBuf,
27557 "changes: %lld total_changes: %lld",
27558 sqlite3_changes64(p->db), sqlite3_total_changes64(p->db));
27559 raw_printf(p->out, "%s\n", zLineBuf);
27560 }
27561 return 0;
27562 }
27563
27564 static void echo_group_input(ShellState *p, const char *zDo){
27565 if( ShellHasFlag(p, SHFLG_Echo) ) utf8_printf(p->out, "%s\n", zDo);
27566 }
27567
27568 #ifdef SQLITE_SHELL_FIDDLE
27569 /*
27570 ** Alternate one_input_line() impl for wasm mode. This is not in the primary
@@ -27618,12 +28338,12 @@
27618 i64 startline = 0; /* Line number for start of current input */
27619 QuickScanState qss = QSS_Start; /* Accumulated line status (so far) */
27620
27621 if( p->inputNesting==MAX_INPUT_NESTING ){
27622 /* This will be more informative in a later version. */
27623 utf8_printf(stderr,"Input nesting limit (%d) reached at line %d."
27624 " Check recursion.\n", MAX_INPUT_NESTING, p->lineno);
27625 return 1;
27626 }
27627 ++p->inputNesting;
27628 p->lineno = 0;
27629 CONTINUE_PROMPT_RESET;
@@ -27630,11 +28350,11 @@
27630 while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
27631 fflush(p->out);
27632 zLine = one_input_line(p->in, zLine, nSql>0);
27633 if( zLine==0 ){
27634 /* End of input */
27635 if( p->in==0 && stdin_is_interactive ) printf("\n");
27636 break;
27637 }
27638 if( seenInterrupt ){
27639 if( p->in!=0 ) break;
27640 seenInterrupt = 0;
@@ -27840,27 +28560,27 @@
27840 sqliterc = find_xdg_config();
27841 }
27842 if( sqliterc == NULL ){
27843 home_dir = find_home_dir(0);
27844 if( home_dir==0 ){
27845 raw_printf(stderr, "-- warning: cannot find home directory;"
27846 " cannot read ~/.sqliterc\n");
27847 return;
27848 }
27849 zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
27850 shell_check_oom(zBuf);
27851 sqliterc = zBuf;
27852 }
27853 p->in = fopen(sqliterc,"rb");
27854 if( p->in ){
27855 if( stdin_is_interactive ){
27856 utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
27857 }
27858 if( process_input(p) && bail_on_error ) exit(1);
27859 fclose(p->in);
27860 }else if( sqliterc_override!=0 ){
27861 utf8_printf(stderr,"cannot open: \"%s\"\n", sqliterc);
27862 if( bail_on_error ) exit(1);
27863 }
27864 p->in = inSaved;
27865 p->lineno = savedLineno;
27866 sqlite3_free(zBuf);
@@ -27906,13 +28626,10 @@
27906 " -mmap N default mmap size set to N\n"
27907 #ifdef SQLITE_ENABLE_MULTIPLEX
27908 " -multiplex enable the multiplexor VFS\n"
27909 #endif
27910 " -newline SEP set output row separator. Default: '\\n'\n"
27911 #if SHELL_WIN_UTF8_OPT
27912 " -no-utf8 do not try to set up UTF-8 output (for legacy)\n"
27913 #endif
27914 " -nofollow refuse to open symbolic links to database files\n"
27915 " -nonce STRING set the safe-mode escape nonce\n"
27916 " -nullvalue TEXT set text string for NULL values. Default ''\n"
27917 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
27918 " -pcachetrace trace all page cache operations\n"
@@ -27925,13 +28642,10 @@
27925 #endif
27926 " -stats print memory stats before each finalize\n"
27927 " -table set output mode to 'table'\n"
27928 " -tabs set output mode to 'tabs'\n"
27929 " -unsafe-testing allow unsafe commands and modes for testing\n"
27930 #if SHELL_WIN_UTF8_OPT && 0 /* Option is accepted, but is now the default. */
27931 " -utf8 setup interactive console code page for UTF-8\n"
27932 #endif
27933 " -version show SQLite version\n"
27934 " -vfs NAME use NAME as the default VFS\n"
27935 #ifdef SQLITE_ENABLE_VFSTRACE
27936 " -vfstrace enable tracing of all VFS calls\n"
27937 #endif
@@ -27938,18 +28652,17 @@
27938 #ifdef SQLITE_HAVE_ZLIB
27939 " -zip open the file as a ZIP Archive\n"
27940 #endif
27941 ;
27942 static void usage(int showDetail){
27943 utf8_printf(stderr,
27944 "Usage: %s [OPTIONS] [FILENAME [SQL]]\n"
27945 "FILENAME is the name of an SQLite database. A new database is created\n"
27946 "if the file does not previously exist. Defaults to :memory:.\n", Argv0);
27947 if( showDetail ){
27948 utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
27949 }else{
27950 raw_printf(stderr, "Use the -help option for additional information\n");
27951 }
27952 exit(1);
27953 }
27954
27955 /*
@@ -27956,12 +28669,12 @@
27956 ** Internal check: Verify that the SQLite is uninitialized. Print a
27957 ** error message if it is initialized.
27958 */
27959 static void verify_uninitialized(void){
27960 if( sqlite3_config(-1)==SQLITE_MISUSE ){
27961 utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
27962 " initialization.\n");
27963 }
27964 }
27965
27966 /*
27967 ** Initialize the state information in data
@@ -27986,46 +28699,45 @@
27986 }
27987
27988 /*
27989 ** Output text to the console in a font that attracts extra attention.
27990 */
27991 #ifdef _WIN32
27992 static void printBold(const char *zText){
27993 #if !SQLITE_OS_WINRT
27994 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
27995 CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
27996 GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
27997 SetConsoleTextAttribute(out,
27998 FOREGROUND_RED|FOREGROUND_INTENSITY
27999 );
28000 #endif
28001 printf("%s", zText);
28002 #if !SQLITE_OS_WINRT
28003 SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
28004 #endif
28005 }
28006 #else
28007 static void printBold(const char *zText){
28008 printf("\033[1m%s\033[0m", zText);
28009 }
28010 #endif
28011
28012 /*
28013 ** Get the argument to an --option. Throw an error and die if no argument
28014 ** is available.
28015 */
28016 static char *cmdline_option_value(int argc, char **argv, int i){
28017 if( i==argc ){
28018 utf8_printf(stderr, "%s: Error: missing argument to %s\n",
28019 argv[0], argv[argc-1]);
28020 exit(1);
28021 }
28022 return argv[i];
28023 }
28024
28025 static void sayAbnormalExit(void){
28026 if( seenInterrupt ) fprintf(stderr, "Program interrupted.\n");
28027 }
28028
28029 #ifndef SQLITE_SHELL_IS_UTF8
28030 # if (defined(_WIN32) || defined(WIN32)) \
28031 && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
@@ -28051,10 +28763,11 @@
28051 char *zErrMsg = 0;
28052 #ifdef SQLITE_SHELL_FIDDLE
28053 # define data shellState
28054 #else
28055 ShellState data;
 
28056 #endif
28057 const char *zInitFile = 0;
28058 int i;
28059 int rc = 0;
28060 int warnInmemoryDb = 0;
@@ -28072,27 +28785,24 @@
28072 #ifdef SQLITE_SHELL_FIDDLE
28073 stdin_is_interactive = 0;
28074 stdout_is_console = 1;
28075 data.wasm.zDefaultDbName = "/fiddle.sqlite3";
28076 #else
28077 stdin_is_interactive = isatty(0);
28078 stdout_is_console = isatty(1);
28079 #endif
28080 #if SHELL_WIN_UTF8_OPT
28081 probe_console(); /* Check for console I/O and UTF-8 capability. */
28082 if( !mbcs_opted ) atexit(console_restore);
28083 #endif
28084 atexit(sayAbnormalExit);
28085 #ifdef SQLITE_DEBUG
28086 mem_main_enter = sqlite3_memory_used();
28087 #endif
28088 #if !defined(_WIN32_WCE)
28089 if( getenv("SQLITE_DEBUG_BREAK") ){
28090 if( isatty(0) && isatty(2) ){
28091 fprintf(stderr,
28092 "attach debugger to process %d and press any key to continue.\n",
28093 GETPID());
28094 fgetc(stdin);
28095 }else{
28096 #if defined(_WIN32) || defined(WIN32)
28097 #if SQLITE_OS_WINRT
28098 __debugbreak();
@@ -28108,18 +28818,18 @@
28108 /* Register a valid signal handler early, before much else is done. */
28109 #ifdef SIGINT
28110 signal(SIGINT, interrupt_handler);
28111 #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
28112 if( !SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE) ){
28113 fprintf(stderr, "No ^C handler.\n");
28114 }
28115 #endif
28116
28117 #if USE_SYSTEM_SQLITE+0!=1
28118 if( cli_strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
28119 utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
28120 sqlite3_sourceid(), SQLITE_SOURCE_ID);
28121 exit(1);
28122 }
28123 #endif
28124 main_init(&data);
28125
@@ -28211,18 +28921,11 @@
28211 ** informational messages (like from process_sqliterc) before
28212 ** we do the actual processing of arguments later in a second pass.
28213 */
28214 stdin_is_interactive = 0;
28215 }else if( cli_strcmp(z,"-utf8")==0 ){
28216 #if SHELL_WIN_UTF8_OPT
28217 /* Option accepted, but is ignored except for this diagnostic. */
28218 if( mbcs_opted ) fprintf(stderr, "Cannot do UTF-8 at this console.\n");
28219 #endif /* SHELL_WIN_UTF8_OPT */
28220 }else if( cli_strcmp(z,"-no-utf8")==0 ){
28221 #if SHELL_WIN_UTF8_OPT
28222 mbcs_opted = 1;
28223 #endif /* SHELL_WIN_UTF8_OPT */
28224 }else if( cli_strcmp(z,"-heap")==0 ){
28225 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
28226 const char *zSize;
28227 sqlite3_int64 szHeap;
28228
@@ -28353,30 +29056,21 @@
28353 if( zVfs ){
28354 sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
28355 if( pVfs ){
28356 sqlite3_vfs_register(pVfs, 1);
28357 }else{
28358 utf8_printf(stderr, "no such VFS: \"%s\"\n", zVfs);
28359 exit(1);
28360 }
28361 }
28362 #if SHELL_WIN_UTF8_OPT
28363 /* Get indicated Windows console setup done before running invocation commands. */
28364 if( in_console || out_console ){
28365 console_prepare_utf8();
28366 }
28367 if( !in_console ){
28368 setBinaryMode(stdin, 0);
28369 }
28370 #endif
28371
28372 if( data.pAuxDb->zDbFilename==0 ){
28373 #ifndef SQLITE_OMIT_MEMORYDB
28374 data.pAuxDb->zDbFilename = ":memory:";
28375 warnInmemoryDb = argc==1;
28376 #else
28377 utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
28378 return 1;
28379 #endif
28380 }
28381 data.out = stdout;
28382 #ifndef SQLITE_SHELL_FIDDLE
@@ -28489,12 +29183,12 @@
28489 */
28490 ShellSetFlag(&data, SHFLG_Backslash);
28491 }else if( cli_strcmp(z,"-bail")==0 ){
28492 /* No-op. The bail_on_error flag should already be set. */
28493 }else if( cli_strcmp(z,"-version")==0 ){
28494 printf("%s %s (%d-bit)\n", sqlite3_libversion(), sqlite3_sourceid(),
28495 8*(int)sizeof(char*));
28496 return 0;
28497 }else if( cli_strcmp(z,"-interactive")==0 ){
28498 /* already handled */
28499 }else if( cli_strcmp(z,"-batch")==0 ){
28500 /* already handled */
@@ -28546,22 +29240,22 @@
28546 if( rc && bail_on_error ) return rc==2 ? 0 : rc;
28547 }else{
28548 open_db(&data, 0);
28549 rc = shell_exec(&data, z, &zErrMsg);
28550 if( zErrMsg!=0 ){
28551 utf8_printf(stderr,"Error: %s\n", zErrMsg);
28552 if( bail_on_error ) return rc!=0 ? rc : 1;
28553 }else if( rc!=0 ){
28554 utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
28555 if( bail_on_error ) return rc;
28556 }
28557 }
28558 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
28559 }else if( cli_strncmp(z, "-A", 2)==0 ){
28560 if( nCmd>0 ){
28561 utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
28562 " with \"%s\"\n", z);
28563 return 1;
28564 }
28565 open_db(&data, OPEN_DB_ZIPFILE);
28566 if( z[2] ){
28567 argv[i] = &z[2];
@@ -28575,12 +29269,12 @@
28575 }else if( cli_strcmp(z,"-safe")==0 ){
28576 data.bSafeMode = data.bSafeModePersist = 1;
28577 }else if( cli_strcmp(z,"-unsafe-testing")==0 ){
28578 /* Acted upon in first pass. */
28579 }else{
28580 utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
28581 raw_printf(stderr,"Use -help for a list of options.\n");
28582 return 1;
28583 }
28584 data.cMode = data.mode;
28585 }
28586
@@ -28600,13 +29294,13 @@
28600 open_db(&data, 0);
28601 echo_group_input(&data, azCmd[i]);
28602 rc = shell_exec(&data, azCmd[i], &zErrMsg);
28603 if( zErrMsg || rc ){
28604 if( zErrMsg!=0 ){
28605 utf8_printf(stderr,"Error: %s\n", zErrMsg);
28606 }else{
28607 utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
28608 }
28609 sqlite3_free(zErrMsg);
28610 free(azCmd);
28611 return rc!=0 ? rc : 1;
28612 }
@@ -28616,30 +29310,24 @@
28616 /* Run commands received from standard input
28617 */
28618 if( stdin_is_interactive ){
28619 char *zHome;
28620 char *zHistory;
28621 const char *zCharset = "";
28622 int nHistory;
28623 #if SHELL_WIN_UTF8_OPT
28624 switch( console_utf8_in+2*console_utf8_out ){
28625 default: case 0: break;
28626 case 1: zCharset = " (utf8 in)"; break;
28627 case 2: zCharset = " (utf8 out)"; break;
28628 case 3: zCharset = " (utf8 I/O)"; break;
28629 }
28630 #endif
28631 printf(
28632 "SQLite version %s %.19s%s\n" /*extra-version-info*/
28633 "Enter \".help\" for usage hints.\n",
28634 sqlite3_libversion(), sqlite3_sourceid(), zCharset
28635 );
28636 if( warnInmemoryDb ){
28637 printf("Connected to a ");
28638 printBold("transient in-memory database");
28639 printf(".\nUse \".open FILENAME\" to reopen on a "
28640 "persistent database.\n");
28641 }
28642 zHistory = getenv("SQLITE_HISTORY");
28643 if( zHistory ){
28644 zHistory = strdup(zHistory);
28645 }else if( (zHome = find_home_dir(0))!=0 ){
@@ -28695,12 +29383,12 @@
28695 /* Clear the global data structure so that valgrind will detect memory
28696 ** leaks */
28697 memset(&data, 0, sizeof(data));
28698 #ifdef SQLITE_DEBUG
28699 if( sqlite3_memory_used()>mem_main_enter ){
28700 utf8_printf(stderr, "Memory leaked: %u bytes\n",
28701 (unsigned int)(sqlite3_memory_used()-mem_main_enter));
28702 }
28703 #endif
28704 #endif /* !SQLITE_SHELL_FIDDLE */
28705 return rc;
28706 }
28707
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -250,33 +250,1023 @@
250 #define WIN32_LEAN_AND_MEAN
251 #include <windows.h>
252
253 /* string conversion routines only needed on Win32 */
254 extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
 
 
255 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
256 #endif
257
258 /* Use console I/O package as a direct INCLUDE. */
259 #define SQLITE_INTERNAL_LINKAGE static
260
261 #ifdef SQLITE_SHELL_FIDDLE
262 /* Deselect most features from the console I/O package for Fiddle. */
263 # define SQLITE_CIO_NO_REDIRECT
264 # define SQLITE_CIO_NO_CLASSIFY
265 # define SQLITE_CIO_NO_TRANSLATE
266 # define SQLITE_CIO_NO_SETMODE
267 #endif
268 /************************* Begin ../ext/consio/console_io.h ******************/
269 /*
270 ** 2023 November 1
271 **
272 ** The author disclaims copyright to this source code. In place of
273 ** a legal notice, here is a blessing:
274 **
275 ** May you do good and not evil.
276 ** May you find forgiveness for yourself and forgive others.
277 ** May you share freely, never taking more than you give.
278 **
279 ********************************************************************************
280 ** This file exposes various interfaces used for console and other I/O
281 ** by the SQLite project command-line tools. These interfaces are used
282 ** at either source conglomeration time, compilation time, or run time.
283 ** This source provides for either inclusion into conglomerated,
284 ** "single-source" forms or separate compilation then linking.
285 **
286 ** Platform dependencies are "hidden" here by various stratagems so
287 ** that, provided certain conditions are met, the programs using this
288 ** source or object code compiled from it need no explicit conditional
289 ** compilation in their source for their console and stream I/O.
290 **
291 ** The symbols and functionality exposed here are not a public API.
292 ** This code may change in tandem with other project code as needed.
293 **
294 ** When this .h file and its companion .c are directly incorporated into
295 ** a source conglomeration (such as shell.c), the preprocessor symbol
296 ** CIO_WIN_WC_XLATE is defined as 0 or 1, reflecting whether console I/O
297 ** translation for Windows is effected for the build.
298 */
299
300 #ifndef SQLITE_INTERNAL_LINKAGE
301 # define SQLITE_INTERNAL_LINKAGE extern /* external to translation unit */
302 # include <stdio.h>
303 #else
304 # define SHELL_NO_SYSINC /* Better yet, modify mkshellc.tcl for this. */
305 #endif
306
307 #ifndef SQLITE3_H
308 /* # include "sqlite3.h" */
309 #endif
310
311 #ifndef SQLITE_CIO_NO_CLASSIFY
312
313 /* Define enum for use with following function. */
314 typedef enum StreamsAreConsole {
315 SAC_NoConsole = 0,
316 SAC_InConsole = 1, SAC_OutConsole = 2, SAC_ErrConsole = 4,
317 SAC_AnyConsole = 0x7
318 } StreamsAreConsole;
319
320 /*
321 ** Classify the three standard I/O streams according to whether
322 ** they are connected to a console attached to the process.
323 **
324 ** Returns the bit-wise OR of SAC_{In,Out,Err}Console values,
325 ** or SAC_NoConsole if none of the streams reaches a console.
326 **
327 ** This function should be called before any I/O is done with
328 ** the given streams. As a side-effect, the given inputs are
329 ** recorded so that later I/O operations on them may be done
330 ** differently than the C library FILE* I/O would be done,
331 ** iff the stream is used for the I/O functions that follow,
332 ** and to support the ones that use an implicit stream.
333 **
334 ** On some platforms, stream or console mode alteration (aka
335 ** "Setup") may be made which is undone by consoleRestore().
336 */
337 SQLITE_INTERNAL_LINKAGE StreamsAreConsole
338 consoleClassifySetup( FILE *pfIn, FILE *pfOut, FILE *pfErr );
339 /* A usual call for convenience: */
340 #define SQLITE_STD_CONSOLE_INIT() consoleClassifySetup(stdin,stdout,stderr)
341
342 /*
343 ** After an initial call to consoleClassifySetup(...), renew
344 ** the same setup it effected. (A call not after is an error.)
345 ** This will restore state altered by consoleRestore();
346 **
347 ** Applications which run an inferior (child) process which
348 ** inherits the same I/O streams may call this function after
349 ** such a process exits to guard against console mode changes.
350 */
351 SQLITE_INTERNAL_LINKAGE void consoleRenewSetup(void);
352
353 /*
354 ** Undo any side-effects left by consoleClassifySetup(...).
355 **
356 ** This should be called after consoleClassifySetup() and
357 ** before the process terminates normally. It is suitable
358 ** for use with the atexit() C library procedure. After
359 ** this call, no console I/O should be done until one of
360 ** console{Classify or Renew}Setup(...) is called again.
361 **
362 ** Applications which run an inferior (child) process that
363 ** inherits the same I/O streams might call this procedure
364 ** before so that said process will have a console setup
365 ** however users have configured it or come to expect.
366 */
367 SQLITE_INTERNAL_LINKAGE void SQLITE_CDECL consoleRestore( void );
368
369 #else /* defined(SQLITE_CIO_NO_CLASSIFY) */
370 # define consoleClassifySetup(i,o,e)
371 # define consoleRenewSetup()
372 # define consoleRestore()
373 #endif /* defined(SQLITE_CIO_NO_CLASSIFY) */
374
375 #ifndef SQLITE_CIO_NO_REDIRECT
376 /*
377 ** Set stream to be used for the functions below which write
378 ** to "the designated X stream", where X is Output or Error.
379 ** Returns the previous value.
380 **
381 ** Alternatively, pass the special value, invalidFileStream,
382 ** to get the designated stream value without setting it.
383 **
384 ** Before the designated streams are set, they default to
385 ** those passed to consoleClassifySetup(...), and before
386 ** that is called they default to stdout and stderr.
387 **
388 ** It is error to close a stream so designated, then, without
389 ** designating another, use the corresponding {o,e}Emit(...).
390 */
391 SQLITE_INTERNAL_LINKAGE FILE *invalidFileStream;
392 SQLITE_INTERNAL_LINKAGE FILE *setOutputStream(FILE *pf);
393 # ifdef CONSIO_SET_ERROR_STREAM
394 SQLITE_INTERNAL_LINKAGE FILE *setErrorStream(FILE *pf);
395 # endif
396 #else
397 # define setOutputStream(pf)
398 # define setErrorStream(pf)
399 #endif /* !defined(SQLITE_CIO_NO_REDIRECT) */
400
401 #ifndef SQLITE_CIO_NO_TRANSLATE
402 /*
403 ** Emit output like fprintf(). If the output is going to the
404 ** console and translation from UTF-8 is necessary, perform
405 ** the needed translation. Otherwise, write formatted output
406 ** to the provided stream almost as-is, possibly with newline
407 ** translation as specified by set{Binary,Text}Mode().
408 */
409 SQLITE_INTERNAL_LINKAGE int fPrintfUtf8(FILE *pfO, const char *zFormat, ...);
410 /* Like fPrintfUtf8 except stream is always the designated output. */
411 SQLITE_INTERNAL_LINKAGE int oPrintfUtf8(const char *zFormat, ...);
412 /* Like fPrintfUtf8 except stream is always the designated error. */
413 SQLITE_INTERNAL_LINKAGE int ePrintfUtf8(const char *zFormat, ...);
414
415 /*
416 ** Emit output like fputs(). If the output is going to the
417 ** console and translation from UTF-8 is necessary, perform
418 ** the needed translation. Otherwise, write given text to the
419 ** provided stream almost as-is, possibly with newline
420 ** translation as specified by set{Binary,Text}Mode().
421 */
422 SQLITE_INTERNAL_LINKAGE int fPutsUtf8(const char *z, FILE *pfO);
423 /* Like fPutsUtf8 except stream is always the designated output. */
424 SQLITE_INTERNAL_LINKAGE int oPutsUtf8(const char *z);
425 /* Like fPutsUtf8 except stream is always the designated error. */
426 SQLITE_INTERNAL_LINKAGE int ePutsUtf8(const char *z);
427
428 /*
429 ** Emit output like fPutsUtf8(), except that the length of the
430 ** accepted char or character sequence is limited by nAccept.
431 **
432 ** Returns the number of accepted char values.
433 */
434 #ifdef CONSIO_SPUTB
435 SQLITE_INTERNAL_LINKAGE int
436 fPutbUtf8(FILE *pfOut, const char *cBuf, int nAccept);
437 #endif
438 /* Like fPutbUtf8 except stream is always the designated output. */
439 SQLITE_INTERNAL_LINKAGE int
440 oPutbUtf8(const char *cBuf, int nAccept);
441 /* Like fPutbUtf8 except stream is always the designated error. */
442 #ifdef CONSIO_EPUTB
443 SQLITE_INTERNAL_LINKAGE int
444 ePutbUtf8(const char *cBuf, int nAccept);
445 #endif
446
447 /*
448 ** Collect input like fgets(...) with special provisions for input
449 ** from the console on platforms that require same. Defers to the
450 ** C library fgets() when input is not from the console. Newline
451 ** translation may be done as set by set{Binary,Text}Mode(). As a
452 ** convenience, pfIn==NULL is treated as stdin.
453 */
454 SQLITE_INTERNAL_LINKAGE char* fGetsUtf8(char *cBuf, int ncMax, FILE *pfIn);
455 /* Like fGetsUtf8 except stream is always the designated input. */
456 /* SQLITE_INTERNAL_LINKAGE char* iGetsUtf8(char *cBuf, int ncMax); */
457
458 #endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
459
460 #ifndef SQLITE_CIO_NO_SETMODE
461 /*
462 ** Set given stream for binary mode, where newline translation is
463 ** not done, or for text mode where, for some platforms, newlines
464 ** are translated to the platform's conventional char sequence.
465 ** If bFlush true, flush the stream.
466 **
467 ** An additional side-effect is that if the stream is one passed
468 ** to consoleClassifySetup() as an output, it is flushed first.
469 **
470 ** Note that binary/text mode has no effect on console I/O
471 ** translation. On all platforms, newline to the console starts
472 ** a new line and CR,LF chars from the console become a newline.
473 */
474 SQLITE_INTERNAL_LINKAGE void setBinaryMode(FILE *, short bFlush);
475 SQLITE_INTERNAL_LINKAGE void setTextMode(FILE *, short bFlush);
476 #endif
477
478 #ifdef SQLITE_CIO_PROMPTED_IN
479 typedef struct Prompts {
480 int numPrompts;
481 const char **azPrompts;
482 } Prompts;
483
484 /*
485 ** Macros for use of a line editor.
486 **
487 ** The following macros define operations involving use of a
488 ** line-editing library or simple console interaction.
489 ** A "T" argument is a text (char *) buffer or filename.
490 ** A "N" argument is an integer.
491 **
492 ** SHELL_ADD_HISTORY(T) // Record text as line(s) of history.
493 ** SHELL_READ_HISTORY(T) // Read history from file named by T.
494 ** SHELL_WRITE_HISTORY(T) // Write history to file named by T.
495 ** SHELL_STIFLE_HISTORY(N) // Limit history to N entries.
496 **
497 ** A console program which does interactive console input is
498 ** expected to call:
499 ** SHELL_READ_HISTORY(T) before collecting such input;
500 ** SHELL_ADD_HISTORY(T) as record-worthy input is taken;
501 ** SHELL_STIFLE_HISTORY(N) after console input ceases; then
502 ** SHELL_WRITE_HISTORY(T) before the program exits.
503 */
504
505 /*
506 ** Retrieve a single line of input text from an input stream.
507 **
508 ** If pfIn is the input stream passed to consoleClassifySetup(),
509 ** and azPrompt is not NULL, then a prompt is issued before the
510 ** line is collected, as selected by the isContinuation flag.
511 ** Array azPrompt[{0,1}] holds the {main,continuation} prompt.
512 **
513 ** If zBufPrior is not NULL then it is a buffer from a prior
514 ** call to this routine that can be reused, or will be freed.
515 **
516 ** The result is stored in space obtained from malloc() and
517 ** must either be freed by the caller or else passed back to
518 ** this function as zBufPrior for reuse.
519 **
520 ** This function may call upon services of a line-editing
521 ** library to interactively collect line edited input.
522 */
523 SQLITE_INTERNAL_LINKAGE char *
524 shellGetLine(FILE *pfIn, char *zBufPrior, int nLen,
525 short isContinuation, Prompts azPrompt);
526 #endif /* defined(SQLITE_CIO_PROMPTED_IN) */
527 /*
528 ** TBD: Define an interface for application(s) to generate
529 ** completion candidates for use by the line-editor.
530 **
531 ** This may be premature; the CLI is the only application
532 ** that does this. Yet, getting line-editing melded into
533 ** console I/O is desirable because a line-editing library
534 ** may have to establish console operating mode, possibly
535 ** in a way that interferes with the above functionality.
536 */
537
538 #if !(defined(SQLITE_CIO_NO_UTF8SCAN)&&defined(SQLITE_CIO_NO_TRANSLATE))
539 /* Skip over as much z[] input char sequence as is valid UTF-8,
540 ** limited per nAccept char's or whole characters and containing
541 ** no char cn such that ((1<<cn) & ccm)!=0. On return, the
542 ** sequence z:return (inclusive:exclusive) is validated UTF-8.
543 ** Limit: nAccept>=0 => char count, nAccept<0 => character
544 */
545 SQLITE_INTERNAL_LINKAGE const char*
546 zSkipValidUtf8(const char *z, int nAccept, long ccm);
547
548 #endif
549
550 /************************* End ../ext/consio/console_io.h ********************/
551 /************************* Begin ../ext/consio/console_io.c ******************/
552 /*
553 ** 2023 November 4
554 **
555 ** The author disclaims copyright to this source code. In place of
556 ** a legal notice, here is a blessing:
557 **
558 ** May you do good and not evil.
559 ** May you find forgiveness for yourself and forgive others.
560 ** May you share freely, never taking more than you give.
561 **
562 ********************************************************************************
563 ** This file implements various interfaces used for console and stream I/O
564 ** by the SQLite project command-line tools, as explained in console_io.h .
565 ** Functions prefixed by "SQLITE_INTERNAL_LINKAGE" behave as described there.
566 */
567
568 #ifndef SQLITE_CDECL
569 # define SQLITE_CDECL
570 #endif
571
572 #ifndef SHELL_NO_SYSINC
573 # include <stdarg.h>
574 # include <string.h>
575 # include <stdlib.h>
576 # include <limits.h>
577 # include <assert.h>
578 # include "console_io.h"
579 /* # include "sqlite3.h" */
580 #endif
581
582 #ifndef SQLITE_CIO_NO_TRANSLATE
583 # if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
584 # ifndef SHELL_NO_SYSINC
585 # include <io.h>
586 # include <fcntl.h>
587 # undef WIN32_LEAN_AND_MEAN
588 # define WIN32_LEAN_AND_MEAN
589 # include <windows.h>
590 # endif
591 # define CIO_WIN_WC_XLATE 1 /* Use WCHAR Windows APIs for console I/O */
592 # else
593 # ifndef SHELL_NO_SYSINC
594 # include <unistd.h>
595 # endif
596 # define CIO_WIN_WC_XLATE 0 /* Use plain C library stream I/O at console */
597 # endif
598 #else
599 # define CIO_WIN_WC_XLATE 0 /* Not exposing translation routines at all */
600 #endif
601
602 #if CIO_WIN_WC_XLATE
603 /* Character used to represent a known-incomplete UTF-8 char group (�) */
604 static WCHAR cBadGroup = 0xfffd;
605 #endif
606
607 #if CIO_WIN_WC_XLATE
608 static HANDLE handleOfFile(FILE *pf){
609 int fileDesc = _fileno(pf);
610 union { intptr_t osfh; HANDLE fh; } fid = {
611 (fileDesc>=0)? _get_osfhandle(fileDesc) : (intptr_t)INVALID_HANDLE_VALUE
612 };
613 return fid.fh;
614 }
615 #endif
616
617 #ifndef SQLITE_CIO_NO_TRANSLATE
618 typedef struct PerStreamTags {
619 # if CIO_WIN_WC_XLATE
620 HANDLE hx;
621 DWORD consMode;
622 char acIncomplete[4];
623 # else
624 short reachesConsole;
625 # endif
626 FILE *pf;
627 } PerStreamTags;
628
629 /* Define NULL-like value for things which can validly be 0. */
630 # define SHELL_INVALID_FILE_PTR ((FILE *)~0)
631 # if CIO_WIN_WC_XLATE
632 # define SHELL_INVALID_CONS_MODE 0xFFFF0000
633 # endif
634
635 # if CIO_WIN_WC_XLATE
636 # define PST_INITIALIZER { INVALID_HANDLE_VALUE, SHELL_INVALID_CONS_MODE, \
637 {0,0,0,0}, SHELL_INVALID_FILE_PTR }
638 # else
639 # define PST_INITIALIZER { 0, SHELL_INVALID_FILE_PTR }
640 # endif
641
642 /* Quickly say whether a known output is going to the console. */
643 # if CIO_WIN_WC_XLATE
644 static short pstReachesConsole(PerStreamTags *ppst){
645 return (ppst->hx != INVALID_HANDLE_VALUE);
646 }
647 # else
648 # define pstReachesConsole(ppst) 0
649 # endif
650
651 # if CIO_WIN_WC_XLATE
652 static void restoreConsoleArb(PerStreamTags *ppst){
653 if( pstReachesConsole(ppst) ) SetConsoleMode(ppst->hx, ppst->consMode);
654 }
655 # else
656 # define restoreConsoleArb(ppst)
657 # endif
658
659 /* Say whether FILE* appears to be a console, collect associated info. */
660 static short streamOfConsole(FILE *pf, /* out */ PerStreamTags *ppst){
661 # if CIO_WIN_WC_XLATE
662 short rv = 0;
663 DWORD dwCM = SHELL_INVALID_CONS_MODE;
664 HANDLE fh = handleOfFile(pf);
665 ppst->pf = pf;
666 if( INVALID_HANDLE_VALUE != fh ){
667 rv = (GetFileType(fh) == FILE_TYPE_CHAR && GetConsoleMode(fh,&dwCM));
668 }
669 ppst->hx = (rv)? fh : INVALID_HANDLE_VALUE;
670 ppst->consMode = dwCM;
671 return rv;
672 # else
673 ppst->pf = pf;
674 ppst->reachesConsole = ( (short)isatty(fileno(pf)) );
675 return ppst->reachesConsole;
676 # endif
677 }
678
679 # if CIO_WIN_WC_XLATE
680 /* Define console modes for use with the Windows Console API. */
681 # define SHELL_CONI_MODE \
682 (ENABLE_ECHO_INPUT | ENABLE_INSERT_MODE | ENABLE_LINE_INPUT | 0x80 \
683 | ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS | ENABLE_PROCESSED_INPUT)
684 # define SHELL_CONO_MODE (ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT \
685 | ENABLE_VIRTUAL_TERMINAL_PROCESSING)
686 # endif
687
688 typedef struct ConsoleInfo {
689 PerStreamTags pstSetup[3];
690 PerStreamTags pstDesignated[3];
691 StreamsAreConsole sacSetup;
692 } ConsoleInfo;
693
694 static short isValidStreamInfo(PerStreamTags *ppst){
695 return (ppst->pf != SHELL_INVALID_FILE_PTR);
696 }
697
698 static ConsoleInfo consoleInfo = {
699 { /* pstSetup */ PST_INITIALIZER, PST_INITIALIZER, PST_INITIALIZER },
700 { /* pstDesignated[] */ PST_INITIALIZER, PST_INITIALIZER, PST_INITIALIZER },
701 SAC_NoConsole /* sacSetup */
702 };
703
704 SQLITE_INTERNAL_LINKAGE FILE* invalidFileStream = (FILE *)~0;
705
706 # if CIO_WIN_WC_XLATE
707 static void maybeSetupAsConsole(PerStreamTags *ppst, short odir){
708 if( pstReachesConsole(ppst) ){
709 DWORD cm = odir? SHELL_CONO_MODE : SHELL_CONI_MODE;
710 SetConsoleMode(ppst->hx, cm);
711 }
712 }
713 # else
714 # define maybeSetupAsConsole(ppst,odir)
715 # endif
716
717 SQLITE_INTERNAL_LINKAGE void consoleRenewSetup(void){
718 # if CIO_WIN_WC_XLATE
719 int ix = 0;
720 while( ix < 6 ){
721 PerStreamTags *ppst = (ix<3)?
722 &consoleInfo.pstSetup[ix] : &consoleInfo.pstDesignated[ix-3];
723 maybeSetupAsConsole(ppst, (ix % 3)>0);
724 ++ix;
725 }
726 # endif
727 }
728
729 SQLITE_INTERNAL_LINKAGE StreamsAreConsole
730 consoleClassifySetup( FILE *pfIn, FILE *pfOut, FILE *pfErr ){
731 StreamsAreConsole rv = SAC_NoConsole;
732 FILE* apf[3] = { pfIn, pfOut, pfErr };
733 int ix;
734 for( ix = 2; ix >= 0; --ix ){
735 PerStreamTags *ppst = &consoleInfo.pstSetup[ix];
736 if( streamOfConsole(apf[ix], ppst) ){
737 rv |= (SAC_InConsole<<ix);
738 }
739 consoleInfo.pstDesignated[ix] = *ppst;
740 if( ix > 0 ) fflush(apf[ix]);
741 }
742 consoleInfo.sacSetup = rv;
743 consoleRenewSetup();
744 return rv;
745 }
746
747 SQLITE_INTERNAL_LINKAGE void SQLITE_CDECL consoleRestore( void ){
748 # if CIO_WIN_WC_XLATE
749 static ConsoleInfo *pci = &consoleInfo;
750 if( pci->sacSetup ){
751 int ix;
752 for( ix=0; ix<3; ++ix ){
753 if( pci->sacSetup & (SAC_InConsole<<ix) ){
754 PerStreamTags *ppst = &pci->pstSetup[ix];
755 SetConsoleMode(ppst->hx, ppst->consMode);
756 }
757 }
758 }
759 # endif
760 }
761 #endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
762
763 #ifdef SQLITE_CIO_INPUT_REDIR
764 /* Say whether given FILE* is among those known, via either
765 ** consoleClassifySetup() or set{Output,Error}Stream, as
766 ** readable, and return an associated PerStreamTags pointer
767 ** if so. Otherwise, return 0.
768 */
769 static PerStreamTags * isKnownReadable(FILE *pf){
770 static PerStreamTags *apst[] = {
771 &consoleInfo.pstDesignated[0], &consoleInfo.pstSetup[0], 0
772 };
773 int ix = 0;
774 do {
775 if( apst[ix]->pf == pf ) break;
776 } while( apst[++ix] != 0 );
777 return apst[ix];
778 }
779 #endif
780
781 #ifndef SQLITE_CIO_NO_TRANSLATE
782 /* Say whether given FILE* is among those known, via either
783 ** consoleClassifySetup() or set{Output,Error}Stream, as
784 ** writable, and return an associated PerStreamTags pointer
785 ** if so. Otherwise, return 0.
786 */
787 static PerStreamTags * isKnownWritable(FILE *pf){
788 static PerStreamTags *apst[] = {
789 &consoleInfo.pstDesignated[1], &consoleInfo.pstDesignated[2],
790 &consoleInfo.pstSetup[1], &consoleInfo.pstSetup[2], 0
791 };
792 int ix = 0;
793 do {
794 if( apst[ix]->pf == pf ) break;
795 } while( apst[++ix] != 0 );
796 return apst[ix];
797 }
798
799 static FILE *designateEmitStream(FILE *pf, unsigned chix){
800 FILE *rv = consoleInfo.pstDesignated[chix].pf;
801 if( pf == invalidFileStream ) return rv;
802 else{
803 /* Setting a possibly new output stream. */
804 PerStreamTags *ppst = isKnownWritable(pf);
805 if( ppst != 0 ){
806 PerStreamTags pst = *ppst;
807 consoleInfo.pstDesignated[chix] = pst;
808 }else streamOfConsole(pf, &consoleInfo.pstDesignated[chix]);
809 }
810 return rv;
811 }
812
813 SQLITE_INTERNAL_LINKAGE FILE *setOutputStream(FILE *pf){
814 return designateEmitStream(pf, 1);
815 }
816 # ifdef CONSIO_SET_ERROR_STREAM
817 SQLITE_INTERNAL_LINKAGE FILE *setErrorStream(FILE *pf){
818 return designateEmitStream(pf, 2);
819 }
820 # endif
821 #endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
822
823 #ifndef SQLITE_CIO_NO_SETMODE
824 # if CIO_WIN_WC_XLATE
825 static void setModeFlushQ(FILE *pf, short bFlush, int mode){
826 if( bFlush ) fflush(pf);
827 _setmode(_fileno(pf), mode);
828 }
829 # else
830 # define setModeFlushQ(f, b, m) if(b) fflush(f)
831 # endif
832
833 SQLITE_INTERNAL_LINKAGE void setBinaryMode(FILE *pf, short bFlush){
834 setModeFlushQ(pf, bFlush, _O_BINARY);
835 }
836 SQLITE_INTERNAL_LINKAGE void setTextMode(FILE *pf, short bFlush){
837 setModeFlushQ(pf, bFlush, _O_TEXT);
838 }
839 # undef setModeFlushQ
840
841 #else /* defined(SQLITE_CIO_NO_SETMODE) */
842 # define setBinaryMode(f, bFlush) do{ if((bFlush)) fflush(f); }while(0)
843 # define setTextMode(f, bFlush) do{ if((bFlush)) fflush(f); }while(0)
844 #endif /* defined(SQLITE_CIO_NO_SETMODE) */
845
846 #ifndef SQLITE_CIO_NO_TRANSLATE
847 # if CIO_WIN_WC_XLATE
848 /* Write buffer cBuf as output to stream known to reach console,
849 ** limited to ncTake char's. Return ncTake on success, else 0. */
850 static int conZstrEmit(PerStreamTags *ppst, const char *z, int ncTake){
851 int rv = 0;
852 if( z!=NULL ){
853 int nwc = MultiByteToWideChar(CP_UTF8,0, z,ncTake, 0,0);
854 if( nwc > 0 ){
855 WCHAR *zw = sqlite3_malloc64(nwc*sizeof(WCHAR));
856 if( zw!=NULL ){
857 nwc = MultiByteToWideChar(CP_UTF8,0, z,ncTake, zw,nwc);
858 if( nwc > 0 ){
859 /* Translation from UTF-8 to UTF-16, then WCHARs out. */
860 if( WriteConsoleW(ppst->hx, zw,nwc, 0, NULL) ){
861 rv = ncTake;
862 }
863 }
864 sqlite3_free(zw);
865 }
866 }
867 }
868 return rv;
869 }
870
871 /* For {f,o,e}PrintfUtf8() when stream is known to reach console. */
872 static int conioVmPrintf(PerStreamTags *ppst, const char *zFormat, va_list ap){
873 char *z = sqlite3_vmprintf(zFormat, ap);
874 if( z ){
875 int rv = conZstrEmit(ppst, z, (int)strlen(z));
876 sqlite3_free(z);
877 return rv;
878 }else return 0;
879 }
880 # endif /* CIO_WIN_WC_XLATE */
881
882 # ifdef CONSIO_GET_EMIT_STREAM
883 static PerStreamTags * getDesignatedEmitStream(FILE *pf, unsigned chix,
884 PerStreamTags *ppst){
885 PerStreamTags *rv = isKnownWritable(pf);
886 short isValid = (rv!=0)? isValidStreamInfo(rv) : 0;
887 if( rv != 0 && isValid ) return rv;
888 streamOfConsole(pf, ppst);
889 return ppst;
890 }
891 # endif
892
893 /* Get stream info, either for designated output or error stream when
894 ** chix equals 1 or 2, or for an arbitrary stream when chix == 0.
895 ** In either case, ppst references a caller-owned PerStreamTags
896 ** struct which may be filled in if none of the known writable
897 ** streams is being held by consoleInfo. The ppf parameter is an
898 ** output when chix!=0 and an input when chix==0.
899 */
900 static PerStreamTags *
901 getEmitStreamInfo(unsigned chix, PerStreamTags *ppst,
902 /* in/out */ FILE **ppf){
903 PerStreamTags *ppstTry;
904 FILE *pfEmit;
905 if( chix > 0 ){
906 ppstTry = &consoleInfo.pstDesignated[chix];
907 if( !isValidStreamInfo(ppstTry) ){
908 ppstTry = &consoleInfo.pstSetup[chix];
909 pfEmit = ppst->pf;
910 }else pfEmit = ppstTry->pf;
911 if( !isValidStreamInfo(ppst) ){
912 pfEmit = (chix > 1)? stderr : stdout;
913 ppstTry = ppst;
914 streamOfConsole(pfEmit, ppstTry);
915 }
916 *ppf = pfEmit;
917 }else{
918 ppstTry = isKnownWritable(*ppf);
919 if( ppstTry != 0 ) return ppstTry;
920 streamOfConsole(*ppf, ppst);
921 return ppst;
922 }
923 return ppstTry;
924 }
925
926 SQLITE_INTERNAL_LINKAGE int oPrintfUtf8(const char *zFormat, ...){
927 va_list ap;
928 int rv;
929 FILE *pfOut;
930 PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
931 # if CIO_WIN_WC_XLATE
932 PerStreamTags *ppst = getEmitStreamInfo(1, &pst, &pfOut);
933 # else
934 getEmitStreamInfo(1, &pst, &pfOut);
935 # endif
936 assert(zFormat!=0);
937 va_start(ap, zFormat);
938 # if CIO_WIN_WC_XLATE
939 if( pstReachesConsole(ppst) ){
940 rv = conioVmPrintf(ppst, zFormat, ap);
941 }else{
942 # endif
943 rv = vfprintf(pfOut, zFormat, ap);
944 # if CIO_WIN_WC_XLATE
945 }
946 # endif
947 va_end(ap);
948 return rv;
949 }
950
951 SQLITE_INTERNAL_LINKAGE int ePrintfUtf8(const char *zFormat, ...){
952 va_list ap;
953 int rv;
954 FILE *pfErr;
955 PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
956 # if CIO_WIN_WC_XLATE
957 PerStreamTags *ppst = getEmitStreamInfo(2, &pst, &pfErr);
958 # else
959 getEmitStreamInfo(2, &pst, &pfErr);
960 # endif
961 assert(zFormat!=0);
962 va_start(ap, zFormat);
963 # if CIO_WIN_WC_XLATE
964 if( pstReachesConsole(ppst) ){
965 rv = conioVmPrintf(ppst, zFormat, ap);
966 }else{
967 # endif
968 rv = vfprintf(pfErr, zFormat, ap);
969 # if CIO_WIN_WC_XLATE
970 }
971 # endif
972 va_end(ap);
973 return rv;
974 }
975
976 SQLITE_INTERNAL_LINKAGE int fPrintfUtf8(FILE *pfO, const char *zFormat, ...){
977 va_list ap;
978 int rv;
979 PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
980 # if CIO_WIN_WC_XLATE
981 PerStreamTags *ppst = getEmitStreamInfo(0, &pst, &pfO);
982 # else
983 getEmitStreamInfo(0, &pst, &pfO);
984 # endif
985 assert(zFormat!=0);
986 va_start(ap, zFormat);
987 # if CIO_WIN_WC_XLATE
988 if( pstReachesConsole(ppst) ){
989 maybeSetupAsConsole(ppst, 1);
990 rv = conioVmPrintf(ppst, zFormat, ap);
991 if( 0 == isKnownWritable(ppst->pf) ) restoreConsoleArb(ppst);
992 }else{
993 # endif
994 rv = vfprintf(pfO, zFormat, ap);
995 # if CIO_WIN_WC_XLATE
996 }
997 # endif
998 va_end(ap);
999 return rv;
1000 }
1001
1002 SQLITE_INTERNAL_LINKAGE int fPutsUtf8(const char *z, FILE *pfO){
1003 PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1004 # if CIO_WIN_WC_XLATE
1005 PerStreamTags *ppst = getEmitStreamInfo(0, &pst, &pfO);
1006 # else
1007 getEmitStreamInfo(0, &pst, &pfO);
1008 # endif
1009 assert(z!=0);
1010 # if CIO_WIN_WC_XLATE
1011 if( pstReachesConsole(ppst) ){
1012 int rv;
1013 maybeSetupAsConsole(ppst, 1);
1014 rv = conZstrEmit(ppst, z, (int)strlen(z));
1015 if( 0 == isKnownWritable(ppst->pf) ) restoreConsoleArb(ppst);
1016 return rv;
1017 }else {
1018 # endif
1019 return (fputs(z, pfO)<0)? 0 : (int)strlen(z);
1020 # if CIO_WIN_WC_XLATE
1021 }
1022 # endif
1023 }
1024
1025 SQLITE_INTERNAL_LINKAGE int ePutsUtf8(const char *z){
1026 FILE *pfErr;
1027 PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1028 # if CIO_WIN_WC_XLATE
1029 PerStreamTags *ppst = getEmitStreamInfo(2, &pst, &pfErr);
1030 # else
1031 getEmitStreamInfo(2, &pst, &pfErr);
1032 # endif
1033 assert(z!=0);
1034 # if CIO_WIN_WC_XLATE
1035 if( pstReachesConsole(ppst) ) return conZstrEmit(ppst, z, (int)strlen(z));
1036 else {
1037 # endif
1038 return (fputs(z, pfErr)<0)? 0 : (int)strlen(z);
1039 # if CIO_WIN_WC_XLATE
1040 }
1041 # endif
1042 }
1043
1044 SQLITE_INTERNAL_LINKAGE int oPutsUtf8(const char *z){
1045 FILE *pfOut;
1046 PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1047 # if CIO_WIN_WC_XLATE
1048 PerStreamTags *ppst = getEmitStreamInfo(1, &pst, &pfOut);
1049 # else
1050 getEmitStreamInfo(1, &pst, &pfOut);
1051 # endif
1052 assert(z!=0);
1053 # if CIO_WIN_WC_XLATE
1054 if( pstReachesConsole(ppst) ) return conZstrEmit(ppst, z, (int)strlen(z));
1055 else {
1056 # endif
1057 return (fputs(z, pfOut)<0)? 0 : (int)strlen(z);
1058 # if CIO_WIN_WC_XLATE
1059 }
1060 # endif
1061 }
1062
1063 #endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
1064
1065 #if !(defined(SQLITE_CIO_NO_UTF8SCAN) && defined(SQLITE_CIO_NO_TRANSLATE))
1066 /* Skip over as much z[] input char sequence as is valid UTF-8,
1067 ** limited per nAccept char's or whole characters and containing
1068 ** no char cn such that ((1<<cn) & ccm)!=0. On return, the
1069 ** sequence z:return (inclusive:exclusive) is validated UTF-8.
1070 ** Limit: nAccept>=0 => char count, nAccept<0 => character
1071 */
1072 SQLITE_INTERNAL_LINKAGE const char*
1073 zSkipValidUtf8(const char *z, int nAccept, long ccm){
1074 int ng = (nAccept<0)? -nAccept : 0;
1075 const char *pcLimit = (nAccept>=0)? z+nAccept : 0;
1076 assert(z!=0);
1077 while( (pcLimit)? (z<pcLimit) : (ng-- != 0) ){
1078 char c = *z;
1079 if( (c & 0x80) == 0 ){
1080 if( ccm != 0L && c < 0x20 && ((1L<<c) & ccm) != 0 ) return z;
1081 ++z; /* ASCII */
1082 }else if( (c & 0xC0) != 0xC0 ) return z; /* not a lead byte */
1083 else{
1084 const char *zt = z+1; /* Got lead byte, look at trail bytes.*/
1085 do{
1086 if( pcLimit && zt >= pcLimit ) return z;
1087 else{
1088 char ct = *zt++;
1089 if( ct==0 || (zt-z)>4 || (ct & 0xC0)!=0x80 ){
1090 /* Trailing bytes are too few, too many, or invalid. */
1091 return z;
1092 }
1093 }
1094 } while( ((c <<= 1) & 0x40) == 0x40 ); /* Eat lead byte's count. */
1095 z = zt;
1096 }
1097 }
1098 return z;
1099 }
1100 #endif /*!(defined(SQLITE_CIO_NO_UTF8SCAN)&&defined(SQLITE_CIO_NO_TRANSLATE))*/
1101
1102 #ifndef SQLITE_CIO_NO_TRANSLATE
1103
1104 #ifdef CONSIO_SPUTB
1105 SQLITE_INTERNAL_LINKAGE int
1106 fPutbUtf8(FILE *pfO, const char *cBuf, int nAccept){
1107 assert(pfO!=0);
1108 # if CIO_WIN_WC_XLATE
1109 PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1110 PerStreamTags *ppst = getEmitStreamInfo(0, &pst, &pfO);
1111 if( pstReachesConsole(ppst) ){
1112 int rv;
1113 maybeSetupAsConsole(ppst, 1);
1114 rv = conZstrEmit(ppst, cBuf, nAccept);
1115 if( 0 == isKnownWritable(ppst->pf) ) restoreConsoleArb(ppst);
1116 return rv;
1117 }else {
1118 # endif
1119 return (int)fwrite(cBuf, 1, nAccept, pfO);
1120 # if CIO_WIN_WC_XLATE
1121 }
1122 # endif
1123 }
1124 #endif /* defined(CONSIO_SPUTB) */
1125
1126 SQLITE_INTERNAL_LINKAGE int
1127 oPutbUtf8(const char *cBuf, int nAccept){
1128 FILE *pfOut;
1129 PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1130 # if CIO_WIN_WC_XLATE
1131 PerStreamTags *ppst = getEmitStreamInfo(1, &pst, &pfOut);
1132 # else
1133 getEmitStreamInfo(1, &pst, &pfOut);
1134 # endif
1135 # if CIO_WIN_WC_XLATE
1136 if( pstReachesConsole(ppst) ){
1137 return conZstrEmit(ppst, cBuf, nAccept);
1138 }else {
1139 # endif
1140 return (int)fwrite(cBuf, 1, nAccept, pfOut);
1141 # if CIO_WIN_WC_XLATE
1142 }
1143 # endif
1144 }
1145
1146 # ifdef CONSIO_EPUTB
1147 SQLITE_INTERNAL_LINKAGE int
1148 ePutbUtf8(const char *cBuf, int nAccept){
1149 FILE *pfErr;
1150 PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1151 PerStreamTags *ppst = getEmitStreamInfo(2, &pst, &pfErr);
1152 # if CIO_WIN_WC_XLATE
1153 if( pstReachesConsole(ppst) ){
1154 return conZstrEmit(ppst, cBuf, nAccept);
1155 }else {
1156 # endif
1157 return (int)fwrite(cBuf, 1, nAccept, pfErr);
1158 # if CIO_WIN_WC_XLATE
1159 }
1160 # endif
1161 }
1162 # endif /* defined(CONSIO_EPUTB) */
1163
1164 SQLITE_INTERNAL_LINKAGE char* fGetsUtf8(char *cBuf, int ncMax, FILE *pfIn){
1165 if( pfIn==0 ) pfIn = stdin;
1166 # if CIO_WIN_WC_XLATE
1167 if( pfIn == consoleInfo.pstSetup[0].pf
1168 && (consoleInfo.sacSetup & SAC_InConsole)!=0 ){
1169 # if CIO_WIN_WC_XLATE==1
1170 # define SHELL_GULP 150 /* Count of WCHARS to be gulped at a time */
1171 WCHAR wcBuf[SHELL_GULP+1];
1172 int lend = 0, noc = 0;
1173 if( ncMax > 0 ) cBuf[0] = 0;
1174 while( noc < ncMax-8-1 && !lend ){
1175 /* There is room for at least 2 more characters and a 0-terminator. */
1176 int na = (ncMax > SHELL_GULP*4+1 + noc)? SHELL_GULP : (ncMax-1 - noc)/4;
1177 # undef SHELL_GULP
1178 DWORD nbr = 0;
1179 BOOL bRC = ReadConsoleW(consoleInfo.pstSetup[0].hx, wcBuf, na, &nbr, 0);
1180 if( bRC && nbr>0 && (wcBuf[nbr-1]&0xF800)==0xD800 ){
1181 /* Last WHAR read is first of a UTF-16 surrogate pair. Grab its mate. */
1182 DWORD nbrx;
1183 bRC &= ReadConsoleW(consoleInfo.pstSetup[0].hx, wcBuf+nbr, 1, &nbrx, 0);
1184 if( bRC ) nbr += nbrx;
1185 }
1186 if( !bRC || (noc==0 && nbr==0) ) return 0;
1187 if( nbr > 0 ){
1188 int nmb = WideCharToMultiByte(CP_UTF8, 0, wcBuf,nbr,0,0,0,0);
1189 if( nmb != 0 && noc+nmb <= ncMax ){
1190 int iseg = noc;
1191 nmb = WideCharToMultiByte(CP_UTF8, 0, wcBuf,nbr,cBuf+noc,nmb,0,0);
1192 noc += nmb;
1193 /* Fixup line-ends as coded by Windows for CR (or "Enter".)
1194 ** This is done without regard for any setMode{Text,Binary}()
1195 ** call that might have been done on the interactive input.
1196 */
1197 if( noc > 0 ){
1198 if( cBuf[noc-1]=='\n' ){
1199 lend = 1;
1200 if( noc > 1 && cBuf[noc-2]=='\r' ) cBuf[--noc-1] = '\n';
1201 }
1202 }
1203 /* Check for ^Z (anywhere in line) too, to act as EOF. */
1204 while( iseg < noc ){
1205 if( cBuf[iseg]=='\x1a' ){
1206 noc = iseg; /* Chop ^Z and anything following. */
1207 lend = 1; /* Counts as end of line too. */
1208 break;
1209 }
1210 ++iseg;
1211 }
1212 }else break; /* Drop apparent garbage in. (Could assert.) */
1213 }else break;
1214 }
1215 /* If got nothing, (after ^Z chop), must be at end-of-file. */
1216 if( noc > 0 ){
1217 cBuf[noc] = 0;
1218 return cBuf;
1219 }else return 0;
1220 # endif
1221 }else{
1222 # endif
1223 return fgets(cBuf, ncMax, pfIn);
1224 # if CIO_WIN_WC_XLATE
1225 }
1226 # endif
1227 }
1228 #endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
1229
1230 #undef SHELL_INVALID_FILE_PTR
1231
1232 /************************* End ../ext/consio/console_io.c ********************/
1233
1234 #ifndef SQLITE_SHELL_FIDDLE
1235 /* From here onward, fgets() is redirected to the console_io library. */
1236 # define fgets(b,n,f) fGetsUtf8(b,n,f)
1237 /*
1238 * Define macros for emitting output text in various ways:
1239 * sputz(s, z) => emit 0-terminated string z to given stream s
1240 * sputf(s, f, ...) => emit varargs per format f to given stream s
1241 * oputz(z) => emit 0-terminated string z to default stream
1242 * oputf(f, ...) => emit varargs per format f to default stream
1243 * eputz(z) => emit 0-terminated string z to error stream
1244 * eputf(f, ...) => emit varargs per format f to error stream
1245 * oputb(b, n) => emit char buffer b[0..n-1] to default stream
1246 *
1247 * Note that the default stream is whatever has been last set via:
1248 * setOutputStream(FILE *pf)
1249 * This is normally the stream that CLI normal output goes to.
1250 * For the stand-alone CLI, it is stdout with no .output redirect.
1251 */
1252 # define sputz(s,z) fPutsUtf8(z,s)
1253 # define sputf fPrintfUtf8
1254 # define oputz(z) oPutsUtf8(z)
1255 # define oputf oPrintfUtf8
1256 # define eputz(z) ePutsUtf8(z)
1257 # define eputf ePrintfUtf8
1258 # define oputb(buf,na) oPutbUtf8(buf,na)
1259 #else
1260 /* For Fiddle, all console handling and emit redirection is omitted. */
1261 # define sputz(fp,z) fputs(z,fp)
1262 # define sputf(fp,fmt, ...) fprintf(fp,fmt,__VA_ARGS__)
1263 # define oputz(z) fputs(z,stdout)
1264 # define oputf(fmt, ...) printf(fmt,__VA_ARGS__)
1265 # define eputz(z) fputs(z,stderr)
1266 # define eputf(fmt, ...) fprintf(stderr,fmt,__VA_ARGS__)
1267 # define oputb(buf,na) fwrite(buf,1,na,stdout)
1268 #endif
1269
1270 /* True if the timer is enabled */
1271 static int enableTimer = 0;
1272
@@ -347,14 +1337,14 @@
1337 static void endTimer(void){
1338 if( enableTimer ){
1339 sqlite3_int64 iEnd = timeOfDay();
1340 struct rusage sEnd;
1341 getrusage(RUSAGE_SELF, &sEnd);
1342 oputf("Run Time: real %.3f user %f sys %f\n",
1343 (iEnd - iBegin)*0.001,
1344 timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
1345 timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
1346 }
1347 }
1348
1349 #define BEGIN_TIMER beginTimer()
1350 #define END_TIMER endTimer()
@@ -426,14 +1416,14 @@
1416 static void endTimer(void){
1417 if( enableTimer && getProcessTimesAddr){
1418 FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
1419 sqlite3_int64 ftWallEnd = timeOfDay();
1420 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
1421 oputf("Run Time: real %.3f user %f sys %f\n",
1422 (ftWallEnd - ftWallBegin)*0.001,
1423 timeDiff(&ftUserBegin, &ftUserEnd),
1424 timeDiff(&ftKernelBegin, &ftKernelEnd));
1425 }
1426 }
1427
1428 #define BEGIN_TIMER beginTimer()
1429 #define END_TIMER endTimer()
@@ -466,32 +1456,13 @@
1456 ** is true. Otherwise, assume stdin is connected to a file or pipe.
1457 */
1458 static int stdin_is_interactive = 1;
1459
1460 /*
1461 ** On Windows systems we need to know if standard output is a console
1462 ** in order to show that UTF-16 translation is done in the sign-on
1463 ** banner. The following variable is true if it is the console.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1464 */
1465 static int stdout_is_console = 1;
1466
1467 /*
1468 ** The following is the open SQLite database. We make a pointer
@@ -609,255 +1580,21 @@
1580 shell_strncpy(dynPrompt.dynamicPrompt, ")x!", 4);
1581 }else{
1582 shell_strncpy(dynPrompt.dynamicPrompt, "(x.", 4);
1583 dynPrompt.dynamicPrompt[2] = (char)('0'+dynPrompt.inParenLevel);
1584 }
1585 shell_strncpy(dynPrompt.dynamicPrompt+3, continuePrompt+3,
1586 PROMPT_LEN_MAX-4);
1587 }
1588 }
1589 return dynPrompt.dynamicPrompt;
1590 }
1591 #endif /* !defined(SQLITE_OMIT_DYNAPROMPT) */
1592
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1593 /* Indicate out-of-memory and exit. */
1594 static void shell_out_of_memory(void){
1595 eputz("Error: out of memory\n");
1596 exit(1);
1597 }
1598
1599 /* Check a pointer to see if it is NULL. If it is NULL, exit with an
1600 ** out-of-memory error.
@@ -885,22 +1622,22 @@
1622 char *z;
1623 if( iotrace==0 ) return;
1624 va_start(ap, zFormat);
1625 z = sqlite3_vmprintf(zFormat, ap);
1626 va_end(ap);
1627 sputf(iotrace, "%s", z);
1628 sqlite3_free(z);
1629 }
1630 #endif
1631
1632 /*
1633 ** Output string zUtf to Out stream as w characters. If w is negative,
1634 ** then right-justify the text. W is the width in UTF-8 characters, not
1635 ** in bytes. This is different from the %*.*s specification in printf
1636 ** since with %*.*s the width is measured in bytes, not characters.
1637 */
1638 static void utf8_width_print(int w, const char *zUtf){
1639 int i;
1640 int n;
1641 int aw = w<0 ? -w : w;
1642 if( zUtf==0 ) zUtf = "";
1643 for(i=n=0; zUtf[i]; i++){
@@ -911,15 +1648,15 @@
1648 break;
1649 }
1650 }
1651 }
1652 if( n>=aw ){
1653 oputf("%.*s", i, zUtf);
1654 }else if( w<0 ){
1655 oputf("%*s%s", aw-n, "", zUtf);
1656 }else{
1657 oputf("%s%*s", zUtf, aw-n, "");
1658 }
1659 }
1660
1661
1662 /*
@@ -975,11 +1712,11 @@
1712 ** Return open FILE * if zFile exists, can be opened for read
1713 ** and is an ordinary file or a character stream source.
1714 ** Otherwise return 0.
1715 */
1716 static FILE * openChrSource(const char *zFile){
1717 #if defined(_WIN32) || defined(WIN32)
1718 struct _stat x = {0};
1719 # define STAT_CHR_SRC(mode) ((mode & (_S_IFCHR|_S_IFIFO|_S_IFREG))!=0)
1720 /* On Windows, open first, then check the stream nature. This order
1721 ** is necessary because _stat() and sibs, when checking a named pipe,
1722 ** effectively break the pipe as its supplier sees it. */
@@ -1038,27 +1775,10 @@
1775 if( n>0 && zLine[n-1]=='\r' ) n--;
1776 zLine[n] = 0;
1777 break;
1778 }
1779 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1780 return zLine;
1781 }
1782
1783 /*
1784 ** Retrieve a single line of input text.
@@ -1081,11 +1801,11 @@
1801 if( in!=0 ){
1802 zResult = local_getline(zPrior, in);
1803 }else{
1804 zPrompt = isContinuation ? CONTINUATION_PROMPT : mainPrompt;
1805 #if SHELL_USE_LOCAL_GETLINE
1806 sputz(stdout, zPrompt);
1807 fflush(stdout);
1808 do{
1809 zResult = local_getline(zPrior, stdin);
1810 zPrior = 0;
1811 /* ^C trap creates a false EOF, so let "interrupt" thread catch up. */
@@ -1328,11 +2048,11 @@
2048 double r = sqlite3_value_double(apVal[0]);
2049 int n = nVal>=2 ? sqlite3_value_int(apVal[1]) : 26;
2050 char z[400];
2051 if( n<1 ) n = 1;
2052 if( n>350 ) n = 350;
2053 sqlite3_snprintf(sizeof(z), z, "%#+.*e", n, r);
2054 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
2055 }
2056
2057
2058 /*
@@ -17614,11 +18334,11 @@
18334 ** A callback for the sqlite3_log() interface.
18335 */
18336 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
18337 ShellState *p = (ShellState*)pArg;
18338 if( p->pLog==0 ) return;
18339 sputf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
18340 fflush(p->pLog);
18341 }
18342
18343 /*
18344 ** SQL function: shell_putsnl(X)
@@ -17629,13 +18349,13 @@
18349 static void shellPutsFunc(
18350 sqlite3_context *pCtx,
18351 int nVal,
18352 sqlite3_value **apVal
18353 ){
18354 /* Unused: (ShellState*)sqlite3_user_data(pCtx); */
18355 (void)nVal;
18356 oputf("%s\n", sqlite3_value_text(apVal[0]));
18357 sqlite3_result_value(pCtx, apVal[0]);
18358 }
18359
18360 /*
18361 ** If in safe mode, print an error message described by the arguments
@@ -17650,12 +18370,11 @@
18370 va_list ap;
18371 char *zMsg;
18372 va_start(ap, zErrMsg);
18373 zMsg = sqlite3_vmprintf(zErrMsg, ap);
18374 va_end(ap);
18375 eputf("line %d: %s\n", p->lineno, zMsg);
 
18376 exit(1);
18377 }
18378 }
18379
18380 /*
@@ -17819,11 +18538,11 @@
18538 }
18539
18540 /*
18541 ** Output the given string as a hex-encoded blob (eg. X'1234' )
18542 */
18543 static void output_hex_blob(const void *pBlob, int nBlob){
18544 int i;
18545 unsigned char *aBlob = (unsigned char*)pBlob;
18546
18547 char *zStr = sqlite3_malloc(nBlob*2 + 1);
18548 shell_check_oom(zStr);
@@ -17836,11 +18555,11 @@
18555 zStr[i*2] = aHex[ (aBlob[i] >> 4) ];
18556 zStr[i*2+1] = aHex[ (aBlob[i] & 0x0F) ];
18557 }
18558 zStr[i*2] = '\0';
18559
18560 oputf("X'%s'", zStr);
18561 sqlite3_free(zStr);
18562 }
18563
18564 /*
18565 ** Find a string that is not found anywhere in z[]. Return a pointer
@@ -17866,39 +18585,46 @@
18585 /*
18586 ** Output the given string as a quoted string using SQL quoting conventions.
18587 **
18588 ** See also: output_quoted_escaped_string()
18589 */
18590 static void output_quoted_string(const char *z){
18591 int i;
18592 char c;
18593 #ifndef SQLITE_SHELL_FIDDLE
18594 FILE *pfO = setOutputStream(invalidFileStream);
18595 setBinaryMode(pfO, 1);
18596 #endif
18597 if( z==0 ) return;
18598 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
18599 if( c==0 ){
18600 oputf("'%s'",z);
18601 }else{
18602 oputz("'");
18603 while( *z ){
18604 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
18605 if( c=='\'' ) i++;
18606 if( i ){
18607 oputf("%.*s", i, z);
18608 z += i;
18609 }
18610 if( c=='\'' ){
18611 oputz("'");
18612 continue;
18613 }
18614 if( c==0 ){
18615 break;
18616 }
18617 z++;
18618 }
18619 oputz("'");
18620 }
18621 #ifndef SQLITE_SHELL_FIDDLE
18622 setTextMode(pfO, 1);
18623 #else
18624 setTextMode(stdout, 1);
18625 #endif
18626 }
18627
18628 /*
18629 ** Output the given string as a quoted string using SQL quoting conventions.
18630 ** Additionallly , escape the "\n" and "\r" characters so that they do not
@@ -17906,17 +18632,20 @@
18632 ** systems.
18633 **
18634 ** This is like output_quoted_string() but with the addition of the \r\n
18635 ** escape mechanism.
18636 */
18637 static void output_quoted_escaped_string(const char *z){
18638 int i;
18639 char c;
18640 #ifndef SQLITE_SHELL_FIDDLE
18641 FILE *pfO = setOutputStream(invalidFileStream);
18642 setBinaryMode(pfO, 1);
18643 #endif
18644 for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
18645 if( c==0 ){
18646 oputf("'%s'",z);
18647 }else{
18648 const char *zNL = 0;
18649 const char *zCR = 0;
18650 int nNL = 0;
18651 int nCR = 0;
@@ -17924,121 +18653,167 @@
18653 for(i=0; z[i]; i++){
18654 if( z[i]=='\n' ) nNL++;
18655 if( z[i]=='\r' ) nCR++;
18656 }
18657 if( nNL ){
18658 oputz("replace(");
18659 zNL = unused_string(z, "\\n", "\\012", zBuf1);
18660 }
18661 if( nCR ){
18662 oputz("replace(");
18663 zCR = unused_string(z, "\\r", "\\015", zBuf2);
18664 }
18665 oputz("'");
18666 while( *z ){
18667 for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
18668 if( c=='\'' ) i++;
18669 if( i ){
18670 oputf("%.*s", i, z);
18671 z += i;
18672 }
18673 if( c=='\'' ){
18674 oputz("'");
18675 continue;
18676 }
18677 if( c==0 ){
18678 break;
18679 }
18680 z++;
18681 if( c=='\n' ){
18682 oputz(zNL);
18683 continue;
18684 }
18685 oputz(zCR);
18686 }
18687 oputz("'");
18688 if( nCR ){
18689 oputf(",'%s',char(13))", zCR);
18690 }
18691 if( nNL ){
18692 oputf(",'%s',char(10))", zNL);
18693 }
18694 }
18695 #ifndef SQLITE_SHELL_FIDDLE
18696 setTextMode(pfO, 1);
18697 #else
18698 setTextMode(stdout, 1);
18699 #endif
18700 }
18701
18702 /*
18703 ** Find earliest of chars within s specified in zAny.
18704 ** With ns == ~0, is like strpbrk(s,zAny) and s must be 0-terminated.
18705 */
18706 static const char *anyOfInStr(const char *s, const char *zAny, size_t ns){
18707 const char *pcFirst = 0;
18708 if( ns == ~(size_t)0 ) ns = strlen(s);
18709 while(*zAny){
18710 const char *pc = (const char*)memchr(s, *zAny&0xff, ns);
18711 if( pc ){
18712 pcFirst = pc;
18713 ns = pcFirst - s;
18714 }
18715 ++zAny;
18716 }
18717 return pcFirst;
18718 }
18719 /*
18720 ** Output the given string as a quoted according to C or TCL quoting rules.
18721 */
18722 static void output_c_string(const char *z){
18723 char c;
18724 static const char *zq = "\"";
18725 static long ctrlMask = ~0L;
18726 static const char *zDQBSRO = "\"\\\x7f"; /* double-quote, backslash, rubout */
18727 char ace[3] = "\\?";
18728 char cbsSay;
18729 oputz(zq);
18730 while( *z!=0 ){
18731 const char *pcDQBSRO = anyOfInStr(z, zDQBSRO, ~(size_t)0);
18732 const char *pcPast = zSkipValidUtf8(z, INT_MAX, ctrlMask);
18733 const char *pcEnd = (pcDQBSRO && pcDQBSRO < pcPast)? pcDQBSRO : pcPast;
18734 if( pcEnd > z ) oputb(z, (int)(pcEnd-z));
18735 if( (c = *pcEnd)==0 ) break;
18736 ++pcEnd;
18737 switch( c ){
18738 case '\\': case '"':
18739 cbsSay = (char)c;
18740 break;
18741 case '\t': cbsSay = 't'; break;
18742 case '\n': cbsSay = 'n'; break;
18743 case '\r': cbsSay = 'r'; break;
18744 case '\f': cbsSay = 'f'; break;
18745 default: cbsSay = 0; break;
18746 }
18747 if( cbsSay ){
18748 ace[1] = cbsSay;
18749 oputz(ace);
18750 }else if( !isprint(c&0xff) ){
18751 oputf("\\%03o", c&0xff);
18752 }else{
18753 ace[1] = (char)c;
18754 oputz(ace+1);
18755 }
18756 z = pcEnd;
18757 }
18758 oputz(zq);
18759 }
18760
18761 /*
18762 ** Output the given string as a quoted according to JSON quoting rules.
18763 */
18764 static void output_json_string(const char *z, i64 n){
18765 char c;
18766 static const char *zq = "\"";
18767 static long ctrlMask = ~0L;
18768 static const char *zDQBS = "\"\\";
18769 const char *pcLimit;
18770 char ace[3] = "\\?";
18771 char cbsSay;
18772
18773 if( z==0 ) z = "";
18774 pcLimit = z + ((n<0)? strlen(z) : (size_t)n);
18775 oputz(zq);
18776 while( z < pcLimit ){
18777 const char *pcDQBS = anyOfInStr(z, zDQBS, pcLimit-z);
18778 const char *pcPast = zSkipValidUtf8(z, (int)(pcLimit-z), ctrlMask);
18779 const char *pcEnd = (pcDQBS && pcDQBS < pcPast)? pcDQBS : pcPast;
18780 if( pcEnd > z ){
18781 oputb(z, (int)(pcEnd-z));
18782 z = pcEnd;
18783 }
18784 if( z >= pcLimit ) break;
18785 c = *(z++);
18786 switch( c ){
18787 case '"': case '\\':
18788 cbsSay = (char)c;
18789 break;
18790 case '\b': cbsSay = 'b'; break;
18791 case '\f': cbsSay = 'f'; break;
18792 case '\n': cbsSay = 'n'; break;
18793 case '\r': cbsSay = 'r'; break;
18794 case '\t': cbsSay = 't'; break;
18795 default: cbsSay = 0; break;
18796 }
18797 if( cbsSay ){
18798 ace[1] = cbsSay;
18799 oputz(ace);
18800 }else if( c<=0x1f ){
18801 oputf("u%04x", c);
18802 }else{
18803 ace[1] = (char)c;
18804 oputz(ace+1);
18805 }
18806 }
18807 oputz(zq);
 
 
 
 
 
 
 
 
 
 
 
 
18808 }
18809
18810 /*
18811 ** Output the given string with characters that are special to
18812 ** HTML escaped.
18813 */
18814 static void output_html_string(const char *z){
18815 int i;
18816 if( z==0 ) z = "";
18817 while( *z ){
18818 for(i=0; z[i]
18819 && z[i]!='<'
@@ -18046,22 +18821,22 @@
18821 && z[i]!='>'
18822 && z[i]!='\"'
18823 && z[i]!='\'';
18824 i++){}
18825 if( i>0 ){
18826 oputf("%.*s",i,z);
18827 }
18828 if( z[i]=='<' ){
18829 oputz("&lt;");
18830 }else if( z[i]=='&' ){
18831 oputz("&amp;");
18832 }else if( z[i]=='>' ){
18833 oputz("&gt;");
18834 }else if( z[i]=='\"' ){
18835 oputz("&quot;");
18836 }else if( z[i]=='\'' ){
18837 oputz("&#39;");
18838 }else{
18839 break;
18840 }
18841 z += i + 1;
18842 }
@@ -18095,13 +18870,12 @@
18870 ** the separator, which may or may not be a comma. p->nullValue is
18871 ** the null value. Strings are quoted if necessary. The separator
18872 ** is only issued if bSep is true.
18873 */
18874 static void output_csv(ShellState *p, const char *z, int bSep){
 
18875 if( z==0 ){
18876 oputf("%s",p->nullValue);
18877 }else{
18878 unsigned i;
18879 for(i=0; z[i]; i++){
18880 if( needCsvQuote[((unsigned char*)z)[i]] ){
18881 i = 0;
@@ -18109,18 +18883,18 @@
18883 }
18884 }
18885 if( i==0 || strstr(z, p->colSeparator)!=0 ){
18886 char *zQuoted = sqlite3_mprintf("\"%w\"", z);
18887 shell_check_oom(zQuoted);
18888 oputz(zQuoted);
18889 sqlite3_free(zQuoted);
18890 }else{
18891 oputz(z);
18892 }
18893 }
18894 if( bSep ){
18895 oputz(p->colSeparator);
18896 }
18897 }
18898
18899 /*
18900 ** This routine runs when the user presses Ctrl-C
@@ -18224,20 +18998,20 @@
18998 const char *az[4];
18999 az[0] = zA1;
19000 az[1] = zA2;
19001 az[2] = zA3;
19002 az[3] = zA4;
19003 oputf("authorizer: %s", azAction[op]);
19004 for(i=0; i<4; i++){
19005 oputz(" ");
19006 if( az[i] ){
19007 output_c_string(az[i]);
19008 }else{
19009 oputz("NULL");
19010 }
19011 }
19012 oputz("\n");
19013 if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4);
19014 return SQLITE_OK;
19015 }
19016 #endif
19017
@@ -18249,11 +19023,11 @@
19023 **
19024 ** If the schema statement in z[] contains a start-of-comment and if
19025 ** sqlite3_complete() returns false, try to terminate the comment before
19026 ** printing the result. https://sqlite.org/forum/forumpost/d7be961c5c
19027 */
19028 static void printSchemaLine(const char *z, const char *zTail){
19029 char *zToFree = 0;
19030 if( z==0 ) return;
19031 if( zTail==0 ) return;
19032 if( zTail[0]==';' && (strstr(z, "/*")!=0 || strstr(z,"--")!=0) ){
19033 const char *zOrig = z;
@@ -18271,20 +19045,20 @@
19045 }
19046 sqlite3_free(zNew);
19047 }
19048 }
19049 if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
19050 oputf("CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
19051 }else{
19052 oputf("%s%s", z, zTail);
19053 }
19054 sqlite3_free(zToFree);
19055 }
19056 static void printSchemaLineN(char *z, int n, const char *zTail){
19057 char c = z[n];
19058 z[n] = 0;
19059 printSchemaLine(z, zTail);
19060 z[n] = c;
19061 }
19062
19063 /*
19064 ** Return true if string z[] has nothing but whitespace and comments to the
@@ -18308,11 +19082,11 @@
19082 EQPGraphRow *pNew;
19083 i64 nText;
19084 if( zText==0 ) return;
19085 nText = strlen(zText);
19086 if( p->autoEQPtest ){
19087 oputf("%d,%d,%s\n", iEqpId, p2, zText);
19088 }
19089 pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
19090 shell_check_oom(pNew);
19091 pNew->iEqpId = iEqpId;
19092 pNew->iParentId = p2;
@@ -18356,12 +19130,11 @@
19130 i64 n = strlen(p->sGraph.zPrefix);
19131 char *z;
19132 for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
19133 pNext = eqp_next_row(p, iEqpId, pRow);
19134 z = pRow->zText;
19135 oputf("%s%s%s\n", p->sGraph.zPrefix, pNext ? "|--" : "`--", z);
 
19136 if( n<(i64)sizeof(p->sGraph.zPrefix)-7 ){
19137 memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4);
19138 eqp_render_level(p, pRow->iEqpId);
19139 p->sGraph.zPrefix[n] = 0;
19140 }
@@ -18377,17 +19150,17 @@
19150 if( pRow->zText[0]=='-' ){
19151 if( pRow->pNext==0 ){
19152 eqp_reset(p);
19153 return;
19154 }
19155 oputf("%s\n", pRow->zText+3);
19156 p->sGraph.pRow = pRow->pNext;
19157 sqlite3_free(pRow);
19158 }else if( nCycle>0 ){
19159 oputf("QUERY PLAN (cycles=%lld [100%%])\n", nCycle);
19160 }else{
19161 oputz("QUERY PLAN\n");
19162 }
19163 p->sGraph.zPrefix[0] = 0;
19164 eqp_render_level(p, 0);
19165 eqp_reset(p);
19166 }
@@ -18399,33 +19172,33 @@
19172 */
19173 static int progress_handler(void *pClientData) {
19174 ShellState *p = (ShellState*)pClientData;
19175 p->nProgress++;
19176 if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
19177 oputf("Progress limit reached (%u)\n", p->nProgress);
19178 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
19179 if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
19180 return 1;
19181 }
19182 if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
19183 oputf("Progress %u\n", p->nProgress);
19184 }
19185 return 0;
19186 }
19187 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
19188
19189 /*
19190 ** Print N dashes
19191 */
19192 static void print_dashes(int N){
19193 const char zDash[] = "--------------------------------------------------";
19194 const int nDash = sizeof(zDash) - 1;
19195 while( N>nDash ){
19196 oputz(zDash);
19197 N -= nDash;
19198 }
19199 oputf("%.*s", N, zDash);
19200 }
19201
19202 /*
19203 ** Print a markdown or table-style row separator using ascii-art
19204 */
@@ -18434,19 +19207,19 @@
19207 int nArg,
19208 const char *zSep
19209 ){
19210 int i;
19211 if( nArg>0 ){
19212 oputz(zSep);
19213 print_dashes(p->actualWidth[0]+2);
19214 for(i=1; i<nArg; i++){
19215 oputz(zSep);
19216 print_dashes(p->actualWidth[i]+2);
19217 }
19218 oputz(zSep);
19219 }
19220 oputz("\n");
19221 }
19222
19223 /*
19224 ** This is the callback routine that the shell
19225 ** invokes for each row of a query result.
@@ -18472,14 +19245,14 @@
19245 if( azArg==0 ) break;
19246 for(i=0; i<nArg; i++){
19247 int len = strlen30(azCol[i] ? azCol[i] : "");
19248 if( len>w ) w = len;
19249 }
19250 if( p->cnt++>0 ) oputz(p->rowSeparator);
19251 for(i=0; i<nArg; i++){
19252 oputf("%*s = %s%s", w, azCol[i],
19253 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
19254 }
19255 break;
19256 }
19257 case MODE_ScanExp:
19258 case MODE_Explain: {
@@ -18502,16 +19275,16 @@
19275 if( nArg>nWidth ) nArg = nWidth;
19276
19277 /* If this is the first row seen, print out the headers */
19278 if( p->cnt++==0 ){
19279 for(i=0; i<nArg; i++){
19280 utf8_width_print(aWidth[i], azCol[ aMap[i] ]);
19281 oputz(i==nArg-1 ? "\n" : " ");
19282 }
19283 for(i=0; i<nArg; i++){
19284 print_dashes(aWidth[i]);
19285 oputz(i==nArg-1 ? "\n" : " ");
19286 }
19287 }
19288
19289 /* If there is no data, exit early. */
19290 if( azArg==0 ) break;
@@ -18525,21 +19298,21 @@
19298 w = strlenChar(zVal);
19299 zSep = " ";
19300 }
19301 if( i==iIndent && p->aiIndent && p->pStmt ){
19302 if( p->iIndent<p->nIndent ){
19303 oputf("%*.s", p->aiIndent[p->iIndent], "");
19304 }
19305 p->iIndent++;
19306 }
19307 utf8_width_print(w, zVal ? zVal : p->nullValue);
19308 oputz(i==nArg-1 ? "\n" : zSep);
19309 }
19310 break;
19311 }
19312 case MODE_Semi: { /* .schema and .fullschema output */
19313 printSchemaLine(azArg[0], ";\n");
19314 break;
19315 }
19316 case MODE_Pretty: { /* .schema and .fullschema with --indent */
19317 char *z;
19318 int j;
@@ -18550,11 +19323,11 @@
19323 assert( nArg==1 );
19324 if( azArg[0]==0 ) break;
19325 if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
19326 || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
19327 ){
19328 oputf("%s;\n", azArg[0]);
19329 break;
19330 }
19331 z = sqlite3_mprintf("%s", azArg[0]);
19332 shell_check_oom(z);
19333 j = 0;
@@ -18583,166 +19356,161 @@
19356 }else if( c=='(' ){
19357 nParen++;
19358 }else if( c==')' ){
19359 nParen--;
19360 if( nLine>0 && nParen==0 && j>0 ){
19361 printSchemaLineN(z, j, "\n");
19362 j = 0;
19363 }
19364 }
19365 z[j++] = c;
19366 if( nParen==1 && cEnd==0
19367 && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
19368 ){
19369 if( c=='\n' ) j--;
19370 printSchemaLineN(z, j, "\n ");
19371 j = 0;
19372 nLine++;
19373 while( IsSpace(z[i+1]) ){ i++; }
19374 }
19375 }
19376 z[j] = 0;
19377 }
19378 printSchemaLine(z, ";\n");
19379 sqlite3_free(z);
19380 break;
19381 }
19382 case MODE_List: {
19383 if( p->cnt++==0 && p->showHeader ){
19384 for(i=0; i<nArg; i++){
19385 oputf("%s%s",azCol[i], i==nArg-1 ? p->rowSeparator : p->colSeparator);
 
19386 }
19387 }
19388 if( azArg==0 ) break;
19389 for(i=0; i<nArg; i++){
19390 char *z = azArg[i];
19391 if( z==0 ) z = p->nullValue;
19392 oputz(z);
19393 oputz((i<nArg-1)? p->colSeparator : p->rowSeparator);
 
 
 
 
19394 }
19395 break;
19396 }
19397 case MODE_Html: {
19398 if( p->cnt++==0 && p->showHeader ){
19399 oputz("<TR>");
19400 for(i=0; i<nArg; i++){
19401 oputz("<TH>");
19402 output_html_string(azCol[i]);
19403 oputz("</TH>\n");
19404 }
19405 oputz("</TR>\n");
19406 }
19407 if( azArg==0 ) break;
19408 oputz("<TR>");
19409 for(i=0; i<nArg; i++){
19410 oputz("<TD>");
19411 output_html_string(azArg[i] ? azArg[i] : p->nullValue);
19412 oputz("</TD>\n");
19413 }
19414 oputz("</TR>\n");
19415 break;
19416 }
19417 case MODE_Tcl: {
19418 if( p->cnt++==0 && p->showHeader ){
19419 for(i=0; i<nArg; i++){
19420 output_c_string(azCol[i] ? azCol[i] : "");
19421 if(i<nArg-1) oputz(p->colSeparator);
19422 }
19423 oputz(p->rowSeparator);
19424 }
19425 if( azArg==0 ) break;
19426 for(i=0; i<nArg; i++){
19427 output_c_string(azArg[i] ? azArg[i] : p->nullValue);
19428 if(i<nArg-1) oputz(p->colSeparator);
19429 }
19430 oputz(p->rowSeparator);
19431 break;
19432 }
19433 case MODE_Csv: {
19434 setBinaryMode(p->out, 1);
19435 if( p->cnt++==0 && p->showHeader ){
19436 for(i=0; i<nArg; i++){
19437 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
19438 }
19439 oputz(p->rowSeparator);
19440 }
19441 if( nArg>0 ){
19442 for(i=0; i<nArg; i++){
19443 output_csv(p, azArg[i], i<nArg-1);
19444 }
19445 oputz(p->rowSeparator);
19446 }
19447 setTextMode(p->out, 1);
19448 break;
19449 }
19450 case MODE_Insert: {
19451 if( azArg==0 ) break;
19452 oputf("INSERT INTO %s",p->zDestTable);
19453 if( p->showHeader ){
19454 oputz("(");
19455 for(i=0; i<nArg; i++){
19456 if( i>0 ) oputz(",");
19457 if( quoteChar(azCol[i]) ){
19458 char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
19459 shell_check_oom(z);
19460 oputz(z);
19461 sqlite3_free(z);
19462 }else{
19463 oputf("%s", azCol[i]);
19464 }
19465 }
19466 oputz(")");
19467 }
19468 p->cnt++;
19469 for(i=0; i<nArg; i++){
19470 oputz(i>0 ? "," : " VALUES(");
19471 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
19472 oputz("NULL");
19473 }else if( aiType && aiType[i]==SQLITE_TEXT ){
19474 if( ShellHasFlag(p, SHFLG_Newlines) ){
19475 output_quoted_string(azArg[i]);
19476 }else{
19477 output_quoted_escaped_string(azArg[i]);
19478 }
19479 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
19480 oputz(azArg[i]);
19481 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
19482 char z[50];
19483 double r = sqlite3_column_double(p->pStmt, i);
19484 sqlite3_uint64 ur;
19485 memcpy(&ur,&r,sizeof(r));
19486 if( ur==0x7ff0000000000000LL ){
19487 oputz("9.0e+999");
19488 }else if( ur==0xfff0000000000000LL ){
19489 oputz("-9.0e+999");
19490 }else{
19491 sqlite3_int64 ir = (sqlite3_int64)r;
19492 if( r==(double)ir ){
19493 sqlite3_snprintf(50,z,"%lld.0", ir);
19494 }else{
19495 sqlite3_snprintf(50,z,"%!.20g", r);
19496 }
19497 oputz(z);
19498 }
19499 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
19500 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
19501 int nBlob = sqlite3_column_bytes(p->pStmt, i);
19502 output_hex_blob(pBlob, nBlob);
19503 }else if( isNumber(azArg[i], 0) ){
19504 oputz(azArg[i]);
19505 }else if( ShellHasFlag(p, SHFLG_Newlines) ){
19506 output_quoted_string(azArg[i]);
19507 }else{
19508 output_quoted_escaped_string(azArg[i]);
19509 }
19510 }
19511 oputz(");\n");
19512 break;
19513 }
19514 case MODE_Json: {
19515 if( azArg==0 ) break;
19516 if( p->cnt==0 ){
@@ -18750,93 +19518,93 @@
19518 }else{
19519 fputs(",\n{", p->out);
19520 }
19521 p->cnt++;
19522 for(i=0; i<nArg; i++){
19523 output_json_string(azCol[i], -1);
19524 oputz(":");
19525 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
19526 oputz("null");
19527 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
19528 char z[50];
19529 double r = sqlite3_column_double(p->pStmt, i);
19530 sqlite3_uint64 ur;
19531 memcpy(&ur,&r,sizeof(r));
19532 if( ur==0x7ff0000000000000LL ){
19533 oputz("9.0e+999");
19534 }else if( ur==0xfff0000000000000LL ){
19535 oputz("-9.0e+999");
19536 }else{
19537 sqlite3_snprintf(50,z,"%!.20g", r);
19538 oputz(z);
19539 }
19540 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
19541 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
19542 int nBlob = sqlite3_column_bytes(p->pStmt, i);
19543 output_json_string(pBlob, nBlob);
19544 }else if( aiType && aiType[i]==SQLITE_TEXT ){
19545 output_json_string(azArg[i], -1);
19546 }else{
19547 oputz(azArg[i]);
19548 }
19549 if( i<nArg-1 ){
19550 oputz(",");
19551 }
19552 }
19553 oputz("}");
19554 break;
19555 }
19556 case MODE_Quote: {
19557 if( azArg==0 ) break;
19558 if( p->cnt==0 && p->showHeader ){
19559 for(i=0; i<nArg; i++){
19560 if( i>0 ) fputs(p->colSeparator, p->out);
19561 output_quoted_string(azCol[i]);
19562 }
19563 fputs(p->rowSeparator, p->out);
19564 }
19565 p->cnt++;
19566 for(i=0; i<nArg; i++){
19567 if( i>0 ) fputs(p->colSeparator, p->out);
19568 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
19569 oputz("NULL");
19570 }else if( aiType && aiType[i]==SQLITE_TEXT ){
19571 output_quoted_string(azArg[i]);
19572 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
19573 oputz(azArg[i]);
19574 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
19575 char z[50];
19576 double r = sqlite3_column_double(p->pStmt, i);
19577 sqlite3_snprintf(50,z,"%!.20g", r);
19578 oputz(z);
19579 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
19580 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
19581 int nBlob = sqlite3_column_bytes(p->pStmt, i);
19582 output_hex_blob(pBlob, nBlob);
19583 }else if( isNumber(azArg[i], 0) ){
19584 oputz(azArg[i]);
19585 }else{
19586 output_quoted_string(azArg[i]);
19587 }
19588 }
19589 fputs(p->rowSeparator, p->out);
19590 break;
19591 }
19592 case MODE_Ascii: {
19593 if( p->cnt++==0 && p->showHeader ){
19594 for(i=0; i<nArg; i++){
19595 if( i>0 ) oputz(p->colSeparator);
19596 oputz(azCol[i] ? azCol[i] : "");
19597 }
19598 oputz(p->rowSeparator);
19599 }
19600 if( azArg==0 ) break;
19601 for(i=0; i<nArg; i++){
19602 if( i>0 ) oputz(p->colSeparator);
19603 oputz(azArg[i] ? azArg[i] : p->nullValue);
19604 }
19605 oputz(p->rowSeparator);
19606 break;
19607 }
19608 case MODE_EQP: {
19609 eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
19610 break;
@@ -18911,11 +19679,11 @@
19679 "INSERT INTO selftest(tno,op,cmd,ans)"
19680 " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
19681 "DROP TABLE [_shell$self];"
19682 ,0,0,&zErrMsg);
19683 if( zErrMsg ){
19684 eputf("SELFTEST initialization failure: %s\n", zErrMsg);
19685 sqlite3_free(zErrMsg);
19686 }
19687 sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
19688 }
19689
@@ -19014,37 +19782,36 @@
19782 int i;
19783 const char *z;
19784 rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
19785 if( rc!=SQLITE_OK || !pSelect ){
19786 char *zContext = shell_error_context(zSelect, p->db);
19787 oputf("/**** ERROR: (%d) %s *****/\n%s",
19788 rc, sqlite3_errmsg(p->db), zContext);
19789 sqlite3_free(zContext);
19790 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
19791 return rc;
19792 }
19793 rc = sqlite3_step(pSelect);
19794 nResult = sqlite3_column_count(pSelect);
19795 while( rc==SQLITE_ROW ){
19796 z = (const char*)sqlite3_column_text(pSelect, 0);
19797 oputf("%s", z);
19798 for(i=1; i<nResult; i++){
19799 oputf(",%s", sqlite3_column_text(pSelect, i));
19800 }
19801 if( z==0 ) z = "";
19802 while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
19803 if( z[0] ){
19804 oputz("\n;\n");
19805 }else{
19806 oputz(";\n");
19807 }
19808 rc = sqlite3_step(pSelect);
19809 }
19810 rc = sqlite3_finalize(pSelect);
19811 if( rc!=SQLITE_OK ){
19812 oputf("/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db));
 
19813 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
19814 }
19815 return rc;
19816 }
19817
@@ -19076,11 +19843,11 @@
19843
19844 #ifdef __linux__
19845 /*
19846 ** Attempt to display I/O stats on Linux using /proc/PID/io
19847 */
19848 static void displayLinuxIoStats(void){
19849 FILE *in;
19850 char z[200];
19851 sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
19852 in = fopen(z, "rb");
19853 if( in==0 ) return;
@@ -19099,11 +19866,11 @@
19866 };
19867 int i;
19868 for(i=0; i<ArraySize(aTrans); i++){
19869 int n = strlen30(aTrans[i].zPattern);
19870 if( cli_strncmp(aTrans[i].zPattern, z, n)==0 ){
19871 oputf("%-36s %s", aTrans[i].zDesc, &z[n]);
19872 break;
19873 }
19874 }
19875 }
19876 fclose(in);
@@ -19112,11 +19879,10 @@
19879
19880 /*
19881 ** Display a single line of status using 64-bit values.
19882 */
19883 static void displayStatLine(
 
19884 char *zLabel, /* Label for this one line */
19885 char *zFormat, /* Format for the result */
19886 int iStatusCtrl, /* Which status to display */
19887 int bReset /* True to reset the stats */
19888 ){
@@ -19131,11 +19897,11 @@
19897 if( nPercent>1 ){
19898 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
19899 }else{
19900 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
19901 }
19902 oputf("%-36s %s\n", zLabel, zLine);
19903 }
19904
19905 /*
19906 ** Display memory stats.
19907 */
@@ -19144,141 +19910,130 @@
19910 ShellState *pArg, /* Pointer to ShellState */
19911 int bReset /* True to reset the stats */
19912 ){
19913 int iCur;
19914 int iHiwtr;
 
19915 if( pArg==0 || pArg->out==0 ) return 0;
 
19916
19917 if( pArg->pStmt && pArg->statsOn==2 ){
19918 int nCol, i, x;
19919 sqlite3_stmt *pStmt = pArg->pStmt;
19920 char z[100];
19921 nCol = sqlite3_column_count(pStmt);
19922 oputf("%-36s %d\n", "Number of output columns:", nCol);
19923 for(i=0; i<nCol; i++){
19924 sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
19925 oputf("%-36s %s\n", z, sqlite3_column_name(pStmt,i));
19926 #ifndef SQLITE_OMIT_DECLTYPE
19927 sqlite3_snprintf(30, z+x, "declared type:");
19928 oputf("%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
19929 #endif
19930 #ifdef SQLITE_ENABLE_COLUMN_METADATA
19931 sqlite3_snprintf(30, z+x, "database name:");
19932 oputf("%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
19933 sqlite3_snprintf(30, z+x, "table name:");
19934 oputf("%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
19935 sqlite3_snprintf(30, z+x, "origin name:");
19936 oputf("%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
19937 #endif
19938 }
19939 }
19940
19941 if( pArg->statsOn==3 ){
19942 if( pArg->pStmt ){
19943 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP,bReset);
19944 oputf("VM-steps: %d\n", iCur);
19945 }
19946 return 0;
19947 }
19948
19949 displayStatLine("Memory Used:",
19950 "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
19951 displayStatLine("Number of Outstanding Allocations:",
19952 "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
19953 if( pArg->shellFlgs & SHFLG_Pagecache ){
19954 displayStatLine("Number of Pcache Pages Used:",
19955 "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
19956 }
19957 displayStatLine("Number of Pcache Overflow Bytes:",
19958 "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
19959 displayStatLine("Largest Allocation:",
19960 "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
19961 displayStatLine("Largest Pcache Allocation:",
19962 "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
19963 #ifdef YYTRACKMAXSTACKDEPTH
19964 displayStatLine("Deepest Parser Stack:",
19965 "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
19966 #endif
19967
19968 if( db ){
19969 if( pArg->shellFlgs & SHFLG_Lookaside ){
19970 iHiwtr = iCur = -1;
19971 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
19972 &iCur, &iHiwtr, bReset);
19973 oputf("Lookaside Slots Used: %d (max %d)\n", iCur, iHiwtr);
 
 
19974 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
19975 &iCur, &iHiwtr, bReset);
19976 oputf("Successful lookaside attempts: %d\n", iHiwtr);
 
19977 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
19978 &iCur, &iHiwtr, bReset);
19979 oputf("Lookaside failures due to size: %d\n", iHiwtr);
 
19980 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
19981 &iCur, &iHiwtr, bReset);
19982 oputf("Lookaside failures due to OOM: %d\n", iHiwtr);
 
19983 }
19984 iHiwtr = iCur = -1;
19985 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
19986 oputf("Pager Heap Usage: %d bytes\n", iCur);
 
19987 iHiwtr = iCur = -1;
19988 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
19989 oputf("Page cache hits: %d\n", iCur);
19990 iHiwtr = iCur = -1;
19991 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
19992 oputf("Page cache misses: %d\n", iCur);
19993 iHiwtr = iCur = -1;
19994 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
19995 oputf("Page cache writes: %d\n", iCur);
19996 iHiwtr = iCur = -1;
19997 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
19998 oputf("Page cache spills: %d\n", iCur);
19999 iHiwtr = iCur = -1;
20000 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
20001 oputf("Schema Heap Usage: %d bytes\n", iCur);
 
20002 iHiwtr = iCur = -1;
20003 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
20004 oputf("Statement Heap/Lookaside Usage: %d bytes\n", iCur);
 
20005 }
20006
20007 if( pArg->pStmt ){
20008 int iHit, iMiss;
20009 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
20010 bReset);
20011 oputf("Fullscan Steps: %d\n", iCur);
20012 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
20013 oputf("Sort Operations: %d\n", iCur);
20014 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
20015 oputf("Autoindex Inserts: %d\n", iCur);
20016 iHit = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_HIT,
20017 bReset);
20018 iMiss = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_MISS,
20019 bReset);
20020 if( iHit || iMiss ){
20021 oputf("Bloom filter bypass taken: %d/%d\n", iHit, iHit+iMiss);
 
20022 }
20023 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
20024 oputf("Virtual Machine Steps: %d\n", iCur);
20025 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
20026 oputf("Reprepare operations: %d\n", iCur);
20027 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
20028 oputf("Number of times run: %d\n", iCur);
20029 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
20030 oputf("Memory used by prepared stmt: %d\n", iCur);
20031 }
20032
20033 #ifdef __linux__
20034 displayLinuxIoStats();
20035 #endif
20036
20037 /* Do not remove this machine readable comment: extra-stats-output-here */
20038
20039 return 0;
@@ -19509,11 +20264,11 @@
20264 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
20265 UNUSED_PARAMETER(db);
20266 UNUSED_PARAMETER(pArg);
20267 #else
20268 if( pArg->scanstatsOn==3 ){
20269 const char *zSql =
20270 " SELECT addr, opcode, p1, p2, p3, p4, p5, comment, nexec,"
20271 " round(ncycle*100.0 / (sum(ncycle) OVER ()), 2)||'%' AS cycles"
20272 " FROM bytecode(?)";
20273
20274 int rc = SQLITE_OK;
@@ -19655,21 +20410,21 @@
20410 #define BOX_1234 "\342\224\274" /* U+253c -|- */
20411
20412 /* Draw horizontal line N characters long using unicode box
20413 ** characters
20414 */
20415 static void print_box_line(int N){
20416 const char zDash[] =
20417 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
20418 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
20419 const int nDash = sizeof(zDash) - 1;
20420 N *= 3;
20421 while( N>nDash ){
20422 oputz(zDash);
20423 N -= nDash;
20424 }
20425 oputf("%.*s", N, zDash);
20426 }
20427
20428 /*
20429 ** Draw a horizontal separator for a MODE_Box table.
20430 */
@@ -19680,19 +20435,19 @@
20435 const char *zSep2,
20436 const char *zSep3
20437 ){
20438 int i;
20439 if( nArg>0 ){
20440 oputz(zSep1);
20441 print_box_line(p->actualWidth[0]+2);
20442 for(i=1; i<nArg; i++){
20443 oputz(zSep2);
20444 print_box_line(p->actualWidth[i]+2);
20445 }
20446 oputz(zSep3);
20447 }
20448 oputz("\n");
20449 }
20450
20451 /*
20452 ** z[] is a line of text that is to be displayed the .mode box or table or
20453 ** similar tabular formats. z[] might contain control characters such
@@ -19951,15 +20706,15 @@
20706 rowSep = "\n";
20707 if( p->showHeader ){
20708 for(i=0; i<nColumn; i++){
20709 w = p->actualWidth[i];
20710 if( p->colWidth[i]<0 ) w = -w;
20711 utf8_width_print(w, azData[i]);
20712 fputs(i==nColumn-1?"\n":" ", p->out);
20713 }
20714 for(i=0; i<nColumn; i++){
20715 print_dashes(p->actualWidth[i]);
20716 fputs(i==nColumn-1?"\n":" ", p->out);
20717 }
20718 }
20719 break;
20720 }
@@ -19969,12 +20724,12 @@
20724 print_row_separator(p, nColumn, "+");
20725 fputs("| ", p->out);
20726 for(i=0; i<nColumn; i++){
20727 w = p->actualWidth[i];
20728 n = strlenChar(azData[i]);
20729 oputf("%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
20730 oputz(i==nColumn-1?" |\n":" | ");
20731 }
20732 print_row_separator(p, nColumn, "+");
20733 break;
20734 }
20735 case MODE_Markdown: {
@@ -19982,66 +20737,66 @@
20737 rowSep = " |\n";
20738 fputs("| ", p->out);
20739 for(i=0; i<nColumn; i++){
20740 w = p->actualWidth[i];
20741 n = strlenChar(azData[i]);
20742 oputf("%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
20743 oputz(i==nColumn-1?" |\n":" | ");
20744 }
20745 print_row_separator(p, nColumn, "|");
20746 break;
20747 }
20748 case MODE_Box: {
20749 colSep = " " BOX_13 " ";
20750 rowSep = " " BOX_13 "\n";
20751 print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
20752 oputz(BOX_13 " ");
20753 for(i=0; i<nColumn; i++){
20754 w = p->actualWidth[i];
20755 n = strlenChar(azData[i]);
20756 oputf("%*s%s%*s%s",
20757 (w-n)/2, "", azData[i], (w-n+1)/2, "",
20758 i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
20759 }
20760 print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
20761 break;
20762 }
20763 }
20764 for(i=nColumn, j=0; i<nTotal; i++, j++){
20765 if( j==0 && p->cMode!=MODE_Column ){
20766 oputz(p->cMode==MODE_Box?BOX_13" ":"| ");
20767 }
20768 z = azData[i];
20769 if( z==0 ) z = p->nullValue;
20770 w = p->actualWidth[j];
20771 if( p->colWidth[j]<0 ) w = -w;
20772 utf8_width_print(w, z);
20773 if( j==nColumn-1 ){
20774 oputz(rowSep);
20775 if( bMultiLineRowExists && abRowDiv[i/nColumn-1] && i+1<nTotal ){
20776 if( p->cMode==MODE_Table ){
20777 print_row_separator(p, nColumn, "+");
20778 }else if( p->cMode==MODE_Box ){
20779 print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
20780 }else if( p->cMode==MODE_Column ){
20781 oputz("\n");
20782 }
20783 }
20784 j = -1;
20785 if( seenInterrupt ) goto columnar_end;
20786 }else{
20787 oputz(colSep);
20788 }
20789 }
20790 if( p->cMode==MODE_Table ){
20791 print_row_separator(p, nColumn, "+");
20792 }else if( p->cMode==MODE_Box ){
20793 print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
20794 }
20795 columnar_end:
20796 if( seenInterrupt ){
20797 oputz("Interrupt\n");
20798 }
20799 nData = (nRow+1)*nColumn;
20800 for(i=0; i<nData; i++){
20801 z = azData[i];
20802 if( z!=zEmpty && z!=zShowNull ) free(azData[i]);
@@ -20176,34 +20931,33 @@
20931 int rc = SQLITE_OK;
20932 sqlite3expert *p = pState->expert.pExpert;
20933 assert( p );
20934 assert( bCancel || pzErr==0 || *pzErr==0 );
20935 if( bCancel==0 ){
 
20936 int bVerbose = pState->expert.bVerbose;
20937
20938 rc = sqlite3_expert_analyze(p, pzErr);
20939 if( rc==SQLITE_OK ){
20940 int nQuery = sqlite3_expert_count(p);
20941 int i;
20942
20943 if( bVerbose ){
20944 const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
20945 oputz("-- Candidates -----------------------------\n");
20946 oputf("%s\n", zCand);
20947 }
20948 for(i=0; i<nQuery; i++){
20949 const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
20950 const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
20951 const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
20952 if( zIdx==0 ) zIdx = "(no new indexes)\n";
20953 if( bVerbose ){
20954 oputf("-- Query %d --------------------------------\n",i+1);
20955 oputf("%s\n\n", zSql);
20956 }
20957 oputf("%s\n", zIdx);
20958 oputf("%s\n", zEQP);
20959 }
20960 }
20961 }
20962 sqlite3_expert_destroy(p);
20963 pState->expert.pExpert = 0;
@@ -20234,31 +20988,30 @@
20988 if( n>=2 && 0==cli_strncmp(z, "-verbose", n) ){
20989 pState->expert.bVerbose = 1;
20990 }
20991 else if( n>=2 && 0==cli_strncmp(z, "-sample", n) ){
20992 if( i==(nArg-1) ){
20993 eputf("option requires an argument: %s\n", z);
20994 rc = SQLITE_ERROR;
20995 }else{
20996 iSample = (int)integerValue(azArg[++i]);
20997 if( iSample<0 || iSample>100 ){
20998 eputf("value out of range: %s\n", azArg[i]);
20999 rc = SQLITE_ERROR;
21000 }
21001 }
21002 }
21003 else{
21004 eputf("unknown option: %s\n", z);
21005 rc = SQLITE_ERROR;
21006 }
21007 }
21008
21009 if( rc==SQLITE_OK ){
21010 pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
21011 if( pState->expert.pExpert==0 ){
21012 eputf("sqlite3_expert_new: %s\n", zErr ? zErr : "out of memory");
 
21013 rc = SQLITE_ERROR;
21014 }else{
21015 sqlite3_expert_config(
21016 pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
21017 );
@@ -20581,33 +21334,33 @@
21334 if( zType==0 ) return 0;
21335 dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
21336 noSys = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
21337
21338 if( cli_strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
21339 if( !dataOnly ) oputz("DELETE FROM sqlite_sequence;\n");
21340 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
21341 if( !dataOnly ) oputz("ANALYZE sqlite_schema;\n");
21342 }else if( cli_strncmp(zTable, "sqlite_", 7)==0 ){
21343 return 0;
21344 }else if( dataOnly ){
21345 /* no-op */
21346 }else if( cli_strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
21347 char *zIns;
21348 if( !p->writableSchema ){
21349 oputz("PRAGMA writable_schema=ON;\n");
21350 p->writableSchema = 1;
21351 }
21352 zIns = sqlite3_mprintf(
21353 "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
21354 "VALUES('table','%q','%q',0,'%q');",
21355 zTable, zTable, zSql);
21356 shell_check_oom(zIns);
21357 oputf("%s\n", zIns);
21358 sqlite3_free(zIns);
21359 return 0;
21360 }else{
21361 printSchemaLine(zSql, ";\n");
21362 }
21363
21364 if( cli_strcmp(zType, "table")==0 ){
21365 ShellText sSelect;
21366 ShellText sTable;
@@ -20661,11 +21414,11 @@
21414 savedMode = p->mode;
21415 p->zDestTable = sTable.z;
21416 p->mode = p->cMode = MODE_Insert;
21417 rc = shell_exec(p, sSelect.z, 0);
21418 if( (rc&0xff)==SQLITE_CORRUPT ){
21419 oputz("/****** CORRUPTION ERROR *******/\n");
21420 toggleSelectOrder(p->db);
21421 shell_exec(p, sSelect.z, 0);
21422 toggleSelectOrder(p->db);
21423 }
21424 p->zDestTable = savedDestTable;
@@ -20692,22 +21445,22 @@
21445 char *zErr = 0;
21446 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
21447 if( rc==SQLITE_CORRUPT ){
21448 char *zQ2;
21449 int len = strlen30(zQuery);
21450 oputz("/****** CORRUPTION ERROR *******/\n");
21451 if( zErr ){
21452 oputf("/****** %s ******/\n", zErr);
21453 sqlite3_free(zErr);
21454 zErr = 0;
21455 }
21456 zQ2 = malloc( len+100 );
21457 if( zQ2==0 ) return rc;
21458 sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
21459 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
21460 if( rc ){
21461 oputf("/****** ERROR: %s ******/\n", zErr);
21462 }else{
21463 rc = SQLITE_CORRUPT;
21464 }
21465 sqlite3_free(zErr);
21466 free(zQ2);
@@ -21059,24 +21812,24 @@
21812 hh &= ~HH_Summary;
21813 break;
21814 }
21815 if( ((hw^hh)&HH_Undoc)==0 ){
21816 if( (hh&HH_Summary)!=0 ){
21817 sputf(out, ".%s\n", azHelp[i]+1);
21818 ++n;
21819 }else if( (hw&HW_SummaryOnly)==0 ){
21820 sputf(out, "%s\n", azHelp[i]);
21821 }
21822 }
21823 }
21824 }else{
21825 /* Seek documented commands for which zPattern is an exact prefix */
21826 zPat = sqlite3_mprintf(".%s*", zPattern);
21827 shell_check_oom(zPat);
21828 for(i=0; i<ArraySize(azHelp); i++){
21829 if( sqlite3_strglob(zPat, azHelp[i])==0 ){
21830 sputf(out, "%s\n", azHelp[i]);
21831 j = i+1;
21832 n++;
21833 }
21834 }
21835 sqlite3_free(zPat);
@@ -21083,11 +21836,11 @@
21836 if( n ){
21837 if( n==1 ){
21838 /* when zPattern is a prefix of exactly one command, then include
21839 ** the details of that command, which should begin at offset j */
21840 while( j<ArraySize(azHelp)-1 && azHelp[j][0]==' ' ){
21841 sputf(out, "%s\n", azHelp[j]);
21842 j++;
21843 }
21844 }
21845 return n;
21846 }
@@ -21100,14 +21853,14 @@
21853 while( i<ArraySize(azHelp)-1 && azHelp[i+1][0]==' ' ) ++i;
21854 continue;
21855 }
21856 if( azHelp[i][0]=='.' ) j = i;
21857 if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
21858 sputf(out, "%s\n", azHelp[j]);
21859 while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]==' ' ){
21860 j++;
21861 sputf(out, "%s\n", azHelp[j]);
21862 }
21863 i = j;
21864 n++;
21865 }
21866 }
@@ -21141,27 +21894,27 @@
21894 char *pBuf;
21895 int rc;
21896 if( in==0 ) return 0;
21897 rc = fseek(in, 0, SEEK_END);
21898 if( rc!=0 ){
21899 eputf("Error: '%s' not seekable\n", zName);
21900 fclose(in);
21901 return 0;
21902 }
21903 nIn = ftell(in);
21904 rewind(in);
21905 pBuf = sqlite3_malloc64( nIn+1 );
21906 if( pBuf==0 ){
21907 eputz("Error: out of memory\n");
21908 fclose(in);
21909 return 0;
21910 }
21911 nRead = fread(pBuf, nIn, 1, in);
21912 fclose(in);
21913 if( nRead!=1 ){
21914 sqlite3_free(pBuf);
21915 eputf("Error: cannot read '%s'\n", zName);
21916 return 0;
21917 }
21918 pBuf[nIn] = 0;
21919 if( pnByte ) *pnByte = nIn;
21920 return pBuf;
@@ -21278,11 +22031,11 @@
22031 unsigned int x[16];
22032 char zLine[1000];
22033 if( zDbFilename ){
22034 in = fopen(zDbFilename, "r");
22035 if( in==0 ){
22036 eputf("cannot open \"%s\" for reading\n", zDbFilename);
22037 return 0;
22038 }
22039 nLine = 0;
22040 }else{
22041 in = p->in;
@@ -21299,11 +22052,11 @@
22052 n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */
22053 a = sqlite3_malloc( n ? n : 1 );
22054 shell_check_oom(a);
22055 memset(a, 0, n);
22056 if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
22057 eputz("invalid pagesize\n");
22058 goto readHexDb_error;
22059 }
22060 for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
22061 rc = sscanf(zLine, "| page %d offset %d", &j, &k);
22062 if( rc==2 ){
@@ -21341,11 +22094,11 @@
22094 if(cli_strncmp(zLine, "| end ", 6)==0 ) break;
22095 }
22096 p->lineno = nLine;
22097 }
22098 sqlite3_free(a);
22099 eputf("Error on line %d of --hexdb input\n", nLine);
22100 return 0;
22101 }
22102 #endif /* SQLITE_OMIT_DESERIALIZE */
22103
22104 /*
@@ -21417,26 +22170,23 @@
22170 break;
22171 }
22172 }
22173 globalDb = p->db;
22174 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
22175 eputf("Error: unable to open database \"%s\": %s\n",
22176 zDbFilename, sqlite3_errmsg(p->db));
22177 if( (openFlags & OPEN_DB_KEEPALIVE)==0 ){
22178 exit(1);
22179 }
22180 sqlite3_close(p->db);
22181 sqlite3_open(":memory:", &p->db);
22182 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
22183 eputz("Also: unable to open substitute in-memory database.\n");
 
 
22184 exit(1);
22185 }else{
22186 eputf("Notice: using substitute in-memory database instead of \"%s\"\n",
22187 zDbFilename);
 
22188 }
22189 }
22190 sqlite3_db_config(p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, (int)0, (int*)0);
22191
22192 /* Reflect the use or absence of --unsafe-testing invocation. */
@@ -21539,11 +22289,11 @@
22289 }
22290 rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
22291 SQLITE_DESERIALIZE_RESIZEABLE |
22292 SQLITE_DESERIALIZE_FREEONCLOSE);
22293 if( rc ){
22294 eputf("Error: sqlite3_deserialize() returns %d\n", rc);
22295 }
22296 if( p->szMax>0 ){
22297 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
22298 }
22299 }
@@ -21563,12 +22313,11 @@
22313 ** Attempt to close the database connection. Report errors.
22314 */
22315 void close_db(sqlite3 *db){
22316 int rc = sqlite3_close(db);
22317 if( rc ){
22318 eputf("Error: sqlite3_close() returns %d: %s\n", rc, sqlite3_errmsg(db));
 
22319 }
22320 }
22321
22322 #if HAVE_READLINE || HAVE_EDITLINE
22323 /*
@@ -21725,12 +22474,11 @@
22474 return 1;
22475 }
22476 if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
22477 return 0;
22478 }
22479 eputf("ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", zArg);
 
22480 return 0;
22481 }
22482
22483 /*
22484 ** Set or clear a shell flag according to a boolean value.
@@ -21764,11 +22512,11 @@
22512 }else if( cli_strcmp(zFile, "off")==0 ){
22513 f = 0;
22514 }else{
22515 f = fopen(zFile, bTextMode ? "w" : "wb");
22516 if( f==0 ){
22517 eputf("Error: cannot open \"%s\"\n", zFile);
22518 }
22519 }
22520 return f;
22521 }
22522
@@ -21786,11 +22534,11 @@
22534 sqlite3_stmt *pStmt;
22535 const char *zSql;
22536 i64 nSql;
22537 if( p->traceOut==0 ) return 0;
22538 if( mType==SQLITE_TRACE_CLOSE ){
22539 sputz(p->traceOut, "-- closing database connection\n");
22540 return 0;
22541 }
22542 if( mType!=SQLITE_TRACE_ROW && pX!=0 && ((const char*)pX)[0]=='-' ){
22543 zSql = (const char*)pX;
22544 }else{
@@ -21817,16 +22565,16 @@
22565 if( nSql>1000000000 ) nSql = 1000000000;
22566 while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
22567 switch( mType ){
22568 case SQLITE_TRACE_ROW:
22569 case SQLITE_TRACE_STMT: {
22570 sputf(p->traceOut, "%.*s;\n", (int)nSql, zSql);
22571 break;
22572 }
22573 case SQLITE_TRACE_PROFILE: {
22574 sqlite3_int64 nNanosec = pX ? *(sqlite3_int64*)pX : 0;
22575 sputf(p->traceOut, "%.*s; -- %lld ns\n", (int)nSql, zSql, nNanosec);
22576 break;
22577 }
22578 }
22579 return 0;
22580 }
@@ -21929,16 +22677,15 @@
22677 do{ p->n--; }while( p->z[p->n]!=cQuote );
22678 p->cTerm = c;
22679 break;
22680 }
22681 if( pc==cQuote && c!='\r' ){
22682 eputf("%s:%d: unescaped %c character\n", p->zFile, p->nLine, cQuote);
 
22683 }
22684 if( c==EOF ){
22685 eputf("%s:%d: unterminated %c-quoted field\n",
22686 p->zFile, startLine, cQuote);
22687 p->cTerm = c;
22688 break;
22689 }
22690 import_append_char(p, c);
22691 ppc = pc;
@@ -22032,13 +22779,12 @@
22779
22780 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
22781 shell_check_oom(zQuery);
22782 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
22783 if( rc ){
22784 eputf("Error %d: %s on [%s]\n",
22785 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zQuery);
 
22786 goto end_data_xfer;
22787 }
22788 n = sqlite3_column_count(pQuery);
22789 zInsert = sqlite3_malloc64(200 + nTable + n*3);
22790 shell_check_oom(zInsert);
@@ -22050,13 +22796,12 @@
22796 i += 2;
22797 }
22798 memcpy(zInsert+i, ");", 3);
22799 rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
22800 if( rc ){
22801 eputf("Error %d: %s on [%s]\n",
22802 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb), zInsert);
 
22803 goto end_data_xfer;
22804 }
22805 for(k=0; k<2; k++){
22806 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
22807 for(i=0; i<n; i++){
@@ -22087,12 +22832,12 @@
22832 }
22833 }
22834 } /* End for */
22835 rc = sqlite3_step(pInsert);
22836 if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
22837 eputf("Error %d: %s\n",
22838 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb));
22839 }
22840 sqlite3_reset(pInsert);
22841 cnt++;
22842 if( (cnt%spinRate)==0 ){
22843 printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
@@ -22105,11 +22850,11 @@
22850 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
22851 zTable);
22852 shell_check_oom(zQuery);
22853 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
22854 if( rc ){
22855 eputf("Warning: cannot step \"%s\" backwards", zTable);
22856 break;
22857 }
22858 } /* End for(k=0...) */
22859
22860 end_data_xfer:
@@ -22142,62 +22887,60 @@
22887 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
22888 " WHERE %s ORDER BY rowid ASC", zWhere);
22889 shell_check_oom(zQuery);
22890 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
22891 if( rc ){
22892 eputf("Error: (%d) %s on [%s]\n", sqlite3_extended_errcode(p->db),
22893 sqlite3_errmsg(p->db), zQuery);
 
22894 goto end_schema_xfer;
22895 }
22896 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
22897 zName = sqlite3_column_text(pQuery, 0);
22898 zSql = sqlite3_column_text(pQuery, 1);
22899 if( zName==0 || zSql==0 ) continue;
22900 if( sqlite3_stricmp((char*)zName, "sqlite_sequence")!=0 ){
22901 sputf(stdout, "%s... ", zName); fflush(stdout);
22902 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
22903 if( zErrMsg ){
22904 eputf("Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
22905 sqlite3_free(zErrMsg);
22906 zErrMsg = 0;
22907 }
22908 }
22909 if( xForEach ){
22910 xForEach(p, newDb, (const char*)zName);
22911 }
22912 sputz(stdout, "done\n");
22913 }
22914 if( rc!=SQLITE_DONE ){
22915 sqlite3_finalize(pQuery);
22916 sqlite3_free(zQuery);
22917 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
22918 " WHERE %s ORDER BY rowid DESC", zWhere);
22919 shell_check_oom(zQuery);
22920 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
22921 if( rc ){
22922 eputf("Error: (%d) %s on [%s]\n",
22923 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zQuery);
 
22924 goto end_schema_xfer;
22925 }
22926 while( sqlite3_step(pQuery)==SQLITE_ROW ){
22927 zName = sqlite3_column_text(pQuery, 0);
22928 zSql = sqlite3_column_text(pQuery, 1);
22929 if( zName==0 || zSql==0 ) continue;
22930 if( sqlite3_stricmp((char*)zName, "sqlite_sequence")==0 ) continue;
22931 sputf(stdout, "%s... ", zName); fflush(stdout);
22932 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
22933 if( zErrMsg ){
22934 eputf("Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
22935 sqlite3_free(zErrMsg);
22936 zErrMsg = 0;
22937 }
22938 if( xForEach ){
22939 xForEach(p, newDb, (const char*)zName);
22940 }
22941 sputz(stdout, "done\n");
22942 }
22943 }
22944 end_schema_xfer:
22945 sqlite3_finalize(pQuery);
22946 sqlite3_free(zQuery);
@@ -22210,17 +22953,16 @@
22953 */
22954 static void tryToClone(ShellState *p, const char *zNewDb){
22955 int rc;
22956 sqlite3 *newDb = 0;
22957 if( access(zNewDb,0)==0 ){
22958 eputf("File \"%s\" already exists.\n", zNewDb);
22959 return;
22960 }
22961 rc = sqlite3_open(zNewDb, &newDb);
22962 if( rc ){
22963 eputf("Cannot create output database: %s\n", sqlite3_errmsg(newDb));
 
22964 }else{
22965 sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
22966 sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
22967 tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
22968 tryToCloneSchema(p, newDb, "type!='table'", 0);
@@ -22227,10 +22969,22 @@
22969 sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
22970 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
22971 }
22972 close_db(newDb);
22973 }
22974
22975 #ifndef SQLITE_SHELL_FIDDLE
22976 /*
22977 ** Change the output stream (file or pipe or console) to something else.
22978 */
22979 static void output_redir(ShellState *p, FILE *pfNew){
22980 if( p->out != stdout ) eputz("Output already redirected.\n");
22981 else{
22982 p->out = pfNew;
22983 setOutputStream(pfNew);
22984 }
22985 }
22986
22987 /*
22988 ** Change the output file back to stdout.
22989 **
22990 ** If the p->doXdgOpen flag is set, that means the output was being
@@ -22255,11 +23009,11 @@
23009 "xdg-open";
23010 #endif
23011 char *zCmd;
23012 zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
23013 if( system(zCmd) ){
23014 eputf("Failed: [%s]\n", zCmd);
23015 }else{
23016 /* Give the start/open/xdg-open command some time to get
23017 ** going before we continue, and potential delete the
23018 ** p->zTempFile data file out from under it */
23019 sqlite3_sleep(2000);
@@ -22270,11 +23024,16 @@
23024 }
23025 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
23026 }
23027 p->outfile[0] = 0;
23028 p->out = stdout;
23029 setOutputStream(stdout);
23030 }
23031 #else
23032 # define output_redir(SS,pfO)
23033 # define output_reset(SS)
23034 #endif
23035
23036 /*
23037 ** Run an SQL command and return the single integer result.
23038 */
23039 static int db_int(sqlite3 *db, const char *zSql){
@@ -22341,11 +23100,11 @@
23100 if( p->db==0 ) return 1;
23101 rc = sqlite3_prepare_v2(p->db,
23102 "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
23103 -1, &pStmt, 0);
23104 if( rc ){
23105 eputf("error: %s\n", sqlite3_errmsg(p->db));
23106 sqlite3_finalize(pStmt);
23107 return 1;
23108 }
23109 sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
23110 if( sqlite3_step(pStmt)==SQLITE_ROW
@@ -22354,32 +23113,32 @@
23113 const u8 *pb = sqlite3_column_blob(pStmt,0);
23114 shell_check_oom(pb);
23115 memcpy(aHdr, pb, 100);
23116 sqlite3_finalize(pStmt);
23117 }else{
23118 eputz("unable to read database header\n");
23119 sqlite3_finalize(pStmt);
23120 return 1;
23121 }
23122 i = get2byteInt(aHdr+16);
23123 if( i==1 ) i = 65536;
23124 oputf("%-20s %d\n", "database page size:", i);
23125 oputf("%-20s %d\n", "write format:", aHdr[18]);
23126 oputf("%-20s %d\n", "read format:", aHdr[19]);
23127 oputf("%-20s %d\n", "reserved bytes:", aHdr[20]);
23128 for(i=0; i<ArraySize(aField); i++){
23129 int ofst = aField[i].ofst;
23130 unsigned int val = get4byteInt(aHdr + ofst);
23131 oputf("%-20s %u", aField[i].zName, val);
23132 switch( ofst ){
23133 case 56: {
23134 if( val==1 ) oputz(" (utf8)");
23135 if( val==2 ) oputz(" (utf16le)");
23136 if( val==3 ) oputz(" (utf16be)");
23137 }
23138 }
23139 oputz("\n");
23140 }
23141 if( zDb==0 ){
23142 zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
23143 }else if( cli_strcmp(zDb,"temp")==0 ){
23144 zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
@@ -22388,25 +23147,25 @@
23147 }
23148 for(i=0; i<ArraySize(aQuery); i++){
23149 char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
23150 int val = db_int(p->db, zSql);
23151 sqlite3_free(zSql);
23152 oputf("%-20s %d\n", aQuery[i].zName, val);
23153 }
23154 sqlite3_free(zSchemaTab);
23155 sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
23156 oputf("%-20s %u\n", "data version", iDataVersion);
23157 return 0;
23158 }
23159 #endif /* SQLITE_SHELL_HAVE_RECOVER */
23160
23161 /*
23162 ** Print the current sqlite3_errmsg() value to stderr and return 1.
23163 */
23164 static int shellDatabaseError(sqlite3 *db){
23165 const char *zErr = sqlite3_errmsg(db);
23166 eputf("Error: %s\n", zErr);
23167 return 1;
23168 }
23169
23170 /*
23171 ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE
@@ -22637,11 +23396,10 @@
23396 ShellState *pState, /* Current shell tool state */
23397 char **azArg, /* Array of arguments passed to dot command */
23398 int nArg /* Number of entries in azArg[] */
23399 ){
23400 sqlite3 *db = pState->db; /* Database handle to query "main" db of */
 
23401 int bVerbose = 0; /* If -verbose is present */
23402 int bGroupByParent = 0; /* If -groupbyparent is present */
23403 int i; /* To iterate through azArg[] */
23404 const char *zIndent = ""; /* How much to indent CREATE INDEX by */
23405 int rc; /* Return code */
@@ -22719,13 +23477,11 @@
23477 else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
23478 bGroupByParent = 1;
23479 zIndent = " ";
23480 }
23481 else{
23482 eputf("Usage: %s %s ?-verbose? ?-groupbyparent?\n", azArg[0], azArg[1]);
 
 
23483 return SQLITE_ERROR;
23484 }
23485 }
23486
23487 /* Register the fkey_collate_clause() SQL function */
@@ -22765,44 +23521,44 @@
23521 }
23522 rc = sqlite3_finalize(pExplain);
23523 if( rc!=SQLITE_OK ) break;
23524
23525 if( res<0 ){
23526 eputz("Error: internal error");
23527 break;
23528 }else{
23529 if( bGroupByParent
23530 && (bVerbose || res==0)
23531 && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
23532 ){
23533 oputf("-- Parent table %s\n", zParent);
23534 sqlite3_free(zPrev);
23535 zPrev = sqlite3_mprintf("%s", zParent);
23536 }
23537
23538 if( res==0 ){
23539 oputf("%s%s --> %s\n", zIndent, zCI, zTarget);
23540 }else if( bVerbose ){
23541 oputf("%s/* no extra indexes required for %s -> %s */\n",
23542 zIndent, zFrom, zTarget
23543 );
23544 }
23545 }
23546 }
23547 sqlite3_free(zPrev);
23548
23549 if( rc!=SQLITE_OK ){
23550 eputf("%s\n", sqlite3_errmsg(db));
23551 }
23552
23553 rc2 = sqlite3_finalize(pSql);
23554 if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
23555 rc = rc2;
23556 eputf("%s\n", sqlite3_errmsg(db));
23557 }
23558 }else{
23559 eputf("%s\n", sqlite3_errmsg(db));
23560 }
23561
23562 return rc;
23563 }
23564
@@ -22818,13 +23574,13 @@
23574 n = (nArg>=2 ? strlen30(azArg[1]) : 0);
23575 if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
23576 return lintFkeyIndexes(pState, azArg, nArg);
23577
23578 usage:
23579 eputf("Usage %s sub-command ?switches...?\n", azArg[0]);
23580 eputz("Where sub-commands are:\n");
23581 eputz(" fkey-indexes\n");
23582 return SQLITE_ERROR;
23583 }
23584
23585 #if !defined SQLITE_OMIT_VIRTUALTABLE
23586 static void shellPrepare(
@@ -22835,13 +23591,11 @@
23591 ){
23592 *ppStmt = 0;
23593 if( *pRc==SQLITE_OK ){
23594 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
23595 if( rc!=SQLITE_OK ){
23596 eputf("sql error: %s (%d)\n", sqlite3_errmsg(db), sqlite3_errcode(db));
 
 
23597 *pRc = rc;
23598 }
23599 }
23600 }
23601
@@ -22888,11 +23642,11 @@
23642 if( pStmt ){
23643 sqlite3 *db = sqlite3_db_handle(pStmt);
23644 int rc = sqlite3_finalize(pStmt);
23645 if( *pRc==SQLITE_OK ){
23646 if( rc!=SQLITE_OK ){
23647 eputf("SQL error: %s\n", sqlite3_errmsg(db));
23648 }
23649 *pRc = rc;
23650 }
23651 }
23652 }
@@ -22909,11 +23663,11 @@
23663 ){
23664 int rc = sqlite3_reset(pStmt);
23665 if( *pRc==SQLITE_OK ){
23666 if( rc!=SQLITE_OK ){
23667 sqlite3 *db = sqlite3_db_handle(pStmt);
23668 eputf("SQL error: %s\n", sqlite3_errmsg(db));
23669 }
23670 *pRc = rc;
23671 }
23672 }
23673 #endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
@@ -22959,15 +23713,15 @@
23713 va_list ap;
23714 char *z;
23715 va_start(ap, zFmt);
23716 z = sqlite3_vmprintf(zFmt, ap);
23717 va_end(ap);
23718 eputf("Error: %s\n", z);
23719 if( pAr->fromCmdLine ){
23720 eputz("Use \"-A\" for more help\n");
23721 }else{
23722 eputz("Use \".archive --help\" for more help\n");
23723 }
23724 sqlite3_free(z);
23725 return SQLITE_ERROR;
23726 }
23727
@@ -23063,11 +23817,11 @@
23817 };
23818 int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
23819 struct ArSwitch *pEnd = &aSwitch[nSwitch];
23820
23821 if( nArg<=1 ){
23822 eputz("Wrong number of arguments. Usage:\n");
23823 return arUsage(stderr);
23824 }else{
23825 char *z = azArg[1];
23826 if( z[0]!='-' ){
23827 /* Traditional style [tar] invocation */
@@ -23169,11 +23923,11 @@
23923 }
23924 }
23925 }
23926 }
23927 if( pAr->eCmd==0 ){
23928 eputz("Required argument missing. Usage:\n");
23929 return arUsage(stderr);
23930 }
23931 return SQLITE_OK;
23932 }
23933
@@ -23212,11 +23966,11 @@
23966 if( SQLITE_ROW==sqlite3_step(pTest) ){
23967 bOk = 1;
23968 }
23969 shellReset(&rc, pTest);
23970 if( rc==SQLITE_OK && bOk==0 ){
23971 eputf("not found in archive: %s\n", z);
23972 rc = SQLITE_ERROR;
23973 }
23974 }
23975 shellFinalize(&rc, pTest);
23976 }
@@ -23279,30 +24033,26 @@
24033 arWhereClause(&rc, pAr, &zWhere);
24034
24035 shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
24036 pAr->zSrcTable, zWhere);
24037 if( pAr->bDryRun ){
24038 oputf("%s\n", sqlite3_sql(pSql));
24039 }else{
24040 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
24041 if( pAr->bVerbose ){
24042 oputf("%s % 10d %s %s\n",
24043 sqlite3_column_text(pSql, 0), sqlite3_column_int(pSql, 1),
24044 sqlite3_column_text(pSql, 2),sqlite3_column_text(pSql, 3));
 
 
 
24045 }else{
24046 oputf("%s\n", sqlite3_column_text(pSql, 0));
24047 }
24048 }
24049 }
24050 shellFinalize(&rc, pSql);
24051 sqlite3_free(zWhere);
24052 return rc;
24053 }
 
24054
24055 /*
24056 ** Implementation of .ar "Remove" command.
24057 */
24058 static int arRemoveCommand(ArCommand *pAr){
@@ -23318,11 +24068,11 @@
24068 }
24069 if( rc==SQLITE_OK ){
24070 zSql = sqlite3_mprintf("DELETE FROM %s WHERE %s;",
24071 pAr->zSrcTable, zWhere);
24072 if( pAr->bDryRun ){
24073 oputf("%s\n", zSql);
24074 }else{
24075 char *zErr = 0;
24076 rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0);
24077 if( rc==SQLITE_OK ){
24078 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
@@ -23331,11 +24081,11 @@
24081 }else{
24082 rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0);
24083 }
24084 }
24085 if( zErr ){
24086 sputf(stdout, "ERROR: %s\n", zErr); /* stdout? */
24087 sqlite3_free(zErr);
24088 }
24089 }
24090 }
24091 sqlite3_free(zWhere);
@@ -23395,15 +24145,15 @@
24145 ** populating them changes the timestamp). */
24146 for(i=0; i<2; i++){
24147 j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
24148 sqlite3_bind_int(pSql, j, i);
24149 if( pAr->bDryRun ){
24150 oputf("%s\n", sqlite3_sql(pSql));
24151 }else{
24152 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
24153 if( i==0 && pAr->bVerbose ){
24154 oputf("%s\n", sqlite3_column_text(pSql, 0));
24155 }
24156 }
24157 }
24158 shellReset(&rc, pSql);
24159 }
@@ -23419,17 +24169,17 @@
24169 ** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out.
24170 */
24171 static int arExecSql(ArCommand *pAr, const char *zSql){
24172 int rc;
24173 if( pAr->bDryRun ){
24174 oputf("%s\n", zSql);
24175 rc = SQLITE_OK;
24176 }else{
24177 char *zErr = 0;
24178 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
24179 if( zErr ){
24180 sputf(stdout, "ERROR: %s\n", zErr);
24181 sqlite3_free(zErr);
24182 }
24183 }
24184 return rc;
24185 }
@@ -23600,19 +24350,17 @@
24350 }else{
24351 flags = SQLITE_OPEN_READONLY;
24352 }
24353 cmd.db = 0;
24354 if( cmd.bDryRun ){
24355 oputf("-- open database '%s'%s\n", cmd.zFile,
24356 eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
24357 }
24358 rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
24359 eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
24360 if( rc!=SQLITE_OK ){
24361 eputf("cannot open file: %s (%s)\n", cmd.zFile, sqlite3_errmsg(cmd.db));
 
 
24362 goto end_ar_command;
24363 }
24364 sqlite3_fileio_init(cmd.db, 0, 0);
24365 sqlite3_sqlar_init(cmd.db, 0, 0);
24366 sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
@@ -23621,11 +24369,11 @@
24369 }
24370 if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
24371 if( cmd.eCmd!=AR_CMD_CREATE
24372 && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
24373 ){
24374 eputz("database does not contain an 'sqlar' table\n");
24375 rc = SQLITE_ERROR;
24376 goto end_ar_command;
24377 }
24378 cmd.zSrcTable = sqlite3_mprintf("sqlar");
24379 }
@@ -23679,11 +24427,11 @@
24427 ** This function is used as a callback by the recover extension. Simply
24428 ** print the supplied SQL statement to stdout.
24429 */
24430 static int recoverSqlCb(void *pCtx, const char *zSql){
24431 ShellState *pState = (ShellState*)pCtx;
24432 sputf(pState->out, "%s;\n", zSql);
24433 return SQLITE_OK;
24434 }
24435
24436 /*
24437 ** This function is called to recover data from the database. A script
@@ -23722,11 +24470,11 @@
24470 }else
24471 if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
24472 bRowids = 0;
24473 }
24474 else{
24475 eputf("unexpected option: %s\n", azArg[i]);
24476 showHelp(pState->out, azArg[0]);
24477 return 1;
24478 }
24479 }
24480
@@ -23741,11 +24489,11 @@
24489
24490 sqlite3_recover_run(p);
24491 if( sqlite3_recover_errcode(p)!=SQLITE_OK ){
24492 const char *zErr = sqlite3_recover_errmsg(p);
24493 int errCode = sqlite3_recover_errcode(p);
24494 eputf("sql error: %s (%d)\n", zErr, errCode);
24495 }
24496 rc = sqlite3_recover_finish(p);
24497 return rc;
24498 }
24499 #endif /* SQLITE_SHELL_HAVE_RECOVER */
@@ -23766,11 +24514,11 @@
24514 */
24515 #ifdef SHELL_DEBUG
24516 #define rc_err_oom_die(rc) \
24517 if( rc==SQLITE_NOMEM ) shell_check_oom(0); \
24518 else if(!(rc==SQLITE_OK||rc==SQLITE_DONE)) \
24519 eputf("E:%d\n",rc), assert(0)
24520 #else
24521 static void rc_err_oom_die(int rc){
24522 if( rc==SQLITE_NOMEM ) shell_check_oom(0);
24523 assert(rc==SQLITE_OK||rc==SQLITE_DONE);
24524 }
@@ -23906,10 +24654,11 @@
24654 #ifdef SHELL_COLFIX_DB
24655 if(*zCOL_DB!=':')
24656 sqlite3_exec(*pDb,"drop table if exists ColNames;"
24657 "drop view if exists RepeatedNames;",0,0,0);
24658 #endif
24659 #undef SHELL_COLFIX_DB
24660 rc = sqlite3_exec(*pDb, zTabMake, 0, 0, 0);
24661 rc_err_oom_die(rc);
24662 }
24663 assert(*pDb!=0);
24664 rc = sqlite3_prepare_v2(*pDb, zTabFill, -1, &pStmt, 0);
@@ -24019,11 +24768,11 @@
24768 clearTempFile(p);
24769
24770 #ifndef SQLITE_OMIT_AUTHORIZATION
24771 if( c=='a' && cli_strncmp(azArg[0], "auth", n)==0 ){
24772 if( nArg!=2 ){
24773 eputz("Usage: .auth ON|OFF\n");
24774 rc = 1;
24775 goto meta_command_exit;
24776 }
24777 open_db(p, 0);
24778 if( booleanValue(azArg[1]) ){
@@ -24066,32 +24815,32 @@
24815 }else
24816 if( cli_strcmp(z, "-async")==0 ){
24817 bAsync = 1;
24818 }else
24819 {
24820 eputf("unknown option: %s\n", azArg[j]);
24821 return 1;
24822 }
24823 }else if( zDestFile==0 ){
24824 zDestFile = azArg[j];
24825 }else if( zDb==0 ){
24826 zDb = zDestFile;
24827 zDestFile = azArg[j];
24828 }else{
24829 eputz("Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
24830 return 1;
24831 }
24832 }
24833 if( zDestFile==0 ){
24834 eputz("missing FILENAME argument on .backup\n");
24835 return 1;
24836 }
24837 if( zDb==0 ) zDb = "main";
24838 rc = sqlite3_open_v2(zDestFile, &pDest,
24839 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
24840 if( rc!=SQLITE_OK ){
24841 eputf("Error: cannot open \"%s\"\n", zDestFile);
24842 close_db(pDest);
24843 return 1;
24844 }
24845 if( bAsync ){
24846 sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
@@ -24098,20 +24847,20 @@
24847 0, 0, 0);
24848 }
24849 open_db(p, 0);
24850 pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
24851 if( pBackup==0 ){
24852 eputf("Error: %s\n", sqlite3_errmsg(pDest));
24853 close_db(pDest);
24854 return 1;
24855 }
24856 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
24857 sqlite3_backup_finish(pBackup);
24858 if( rc==SQLITE_DONE ){
24859 rc = 0;
24860 }else{
24861 eputf("Error: %s\n", sqlite3_errmsg(pDest));
24862 rc = 1;
24863 }
24864 close_db(pDest);
24865 }else
24866 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
@@ -24118,11 +24867,11 @@
24867
24868 if( c=='b' && n>=3 && cli_strncmp(azArg[0], "bail", n)==0 ){
24869 if( nArg==2 ){
24870 bail_on_error = booleanValue(azArg[1]);
24871 }else{
24872 eputz("Usage: .bail on|off\n");
24873 rc = 1;
24874 }
24875 }else
24876
24877 /* Undocumented. Legacy only. See "crnl" below */
@@ -24132,13 +24881,12 @@
24881 setBinaryMode(p->out, 1);
24882 }else{
24883 setTextMode(p->out, 1);
24884 }
24885 }else{
24886 eputz("The \".binary\" command is deprecated. Use \".crnl\" instead.\n"
24887 "Usage: .binary on|off\n");
 
24888 rc = 1;
24889 }
24890 }else
24891
24892 /* The undocumented ".breakpoint" command causes a call to the no-op
@@ -24158,25 +24906,25 @@
24906 sqlite3_free(z);
24907 #else
24908 rc = chdir(azArg[1]);
24909 #endif
24910 if( rc ){
24911 eputf("Cannot change to directory \"%s\"\n", azArg[1]);
24912 rc = 1;
24913 }
24914 }else{
24915 eputz("Usage: .cd DIRECTORY\n");
24916 rc = 1;
24917 }
24918 }else
24919 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
24920
24921 if( c=='c' && n>=3 && cli_strncmp(azArg[0], "changes", n)==0 ){
24922 if( nArg==2 ){
24923 setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
24924 }else{
24925 eputz("Usage: .changes on|off\n");
24926 rc = 1;
24927 }
24928 }else
24929
24930 #ifndef SQLITE_SHELL_FIDDLE
@@ -24186,21 +24934,20 @@
24934 */
24935 if( c=='c' && n>=3 && cli_strncmp(azArg[0], "check", n)==0 ){
24936 char *zRes = 0;
24937 output_reset(p);
24938 if( nArg!=2 ){
24939 eputz("Usage: .check GLOB-PATTERN\n");
24940 rc = 2;
24941 }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
24942 rc = 2;
24943 }else if( testcase_glob(azArg[1],zRes)==0 ){
24944 eputf("testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
24945 p->zTestcase, azArg[1], zRes);
 
24946 rc = 1;
24947 }else{
24948 oputf("testcase-%s ok\n", p->zTestcase);
24949 p->nCheck++;
24950 }
24951 sqlite3_free(zRes);
24952 }else
24953 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
@@ -24209,11 +24956,11 @@
24956 if( c=='c' && cli_strncmp(azArg[0], "clone", n)==0 ){
24957 failIfSafeMode(p, "cannot run .clone in safe mode");
24958 if( nArg==2 ){
24959 tryToClone(p, azArg[1]);
24960 }else{
24961 eputz("Usage: .clone FILENAME\n");
24962 rc = 1;
24963 }
24964 }else
24965 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
24966
@@ -24229,13 +24976,13 @@
24976 zFile = "(memory)";
24977 }else if( zFile[0]==0 ){
24978 zFile = "(temporary-file)";
24979 }
24980 if( p->pAuxDb == &p->aAuxDb[i] ){
24981 sputf(stdout, "ACTIVE %d: %s\n", i, zFile);
24982 }else if( p->aAuxDb[i].db!=0 ){
24983 sputf(stdout, " %d: %s\n", i, zFile);
24984 }
24985 }
24986 }else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){
24987 int i = azArg[1][0] - '0';
24988 if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && i<ArraySize(p->aAuxDb) ){
@@ -24248,19 +24995,19 @@
24995 && IsDigit(azArg[2][0]) && azArg[2][1]==0 ){
24996 int i = azArg[2][0] - '0';
24997 if( i<0 || i>=ArraySize(p->aAuxDb) ){
24998 /* No-op */
24999 }else if( p->pAuxDb == &p->aAuxDb[i] ){
25000 eputz("cannot close the active database connection\n");
25001 rc = 1;
25002 }else if( p->aAuxDb[i].db ){
25003 session_close_all(p, i);
25004 close_db(p->aAuxDb[i].db);
25005 p->aAuxDb[i].db = 0;
25006 }
25007 }else{
25008 eputz("Usage: .connection [close] [CONNECTION-NUMBER]\n");
25009 rc = 1;
25010 }
25011 }else
25012
25013 if( c=='c' && n==4 && cli_strncmp(azArg[0], "crnl", n)==0 ){
@@ -24270,13 +25017,13 @@
25017 }else{
25018 setBinaryMode(p->out, 1);
25019 }
25020 }else{
25021 #if !defined(_WIN32) && !defined(WIN32)
25022 eputz("The \".crnl\" is a no-op on non-Windows machines.\n");
25023 #endif
25024 eputz("Usage: .crnl on|off\n");
25025 rc = 1;
25026 }
25027 }else
25028
25029 if( c=='d' && n>1 && cli_strncmp(azArg[0], "databases", n)==0 ){
@@ -24285,11 +25032,11 @@
25032 sqlite3_stmt *pStmt;
25033 int i;
25034 open_db(p, 0);
25035 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
25036 if( rc ){
25037 eputf("Error: %s\n", sqlite3_errmsg(p->db));
25038 rc = 1;
25039 }else{
25040 while( sqlite3_step(pStmt)==SQLITE_ROW ){
25041 const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
25042 const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
@@ -24304,15 +25051,13 @@
25051 sqlite3_finalize(pStmt);
25052 for(i=0; i<nName; i++){
25053 int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
25054 int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
25055 const char *z = azName[i*2+1];
25056 oputf("%s: %s %s%s\n",
25057 azName[i*2], z && z[0] ? z : "\"\"", bRdonly ? "r/o" : "r/w",
25058 eTxn==SQLITE_TXN_NONE ? "" :
 
 
25059 eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
25060 free(azName[i*2]);
25061 free(azName[i*2+1]);
25062 }
25063 sqlite3_free(azName);
@@ -24348,16 +25093,16 @@
25093 if( nArg>1 && cli_strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
25094 if( nArg>=3 ){
25095 sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
25096 }
25097 sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
25098 oputf("%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
25099 if( nArg>1 ) break;
25100 }
25101 if( nArg>1 && ii==ArraySize(aDbConfig) ){
25102 eputf("Error: unknown dbconfig \"%s\"\n", azArg[1]);
25103 eputz("Enter \".dbconfig\" with no arguments for a list\n");
25104 }
25105 }else
25106
25107 #if SQLITE_SHELL_HAVE_RECOVER
25108 if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbinfo", n)==0 ){
@@ -24383,12 +25128,12 @@
25128 if( azArg[i][0]=='-' ){
25129 const char *z = azArg[i]+1;
25130 if( z[0]=='-' ) z++;
25131 if( cli_strcmp(z,"preserve-rowids")==0 ){
25132 #ifdef SQLITE_OMIT_VIRTUALTABLE
25133 eputz("The --preserve-rowids option is not compatible"
25134 " with SQLITE_OMIT_VIRTUALTABLE\n");
25135 rc = 1;
25136 sqlite3_free(zLike);
25137 goto meta_command_exit;
25138 #else
25139 ShellSetFlag(p, SHFLG_PreserveRowid);
@@ -24402,11 +25147,11 @@
25147 }else
25148 if( cli_strcmp(z,"nosys")==0 ){
25149 ShellSetFlag(p, SHFLG_DumpNoSys);
25150 }else
25151 {
25152 eputf("Unknown option \"%s\" on \".dump\"\n", azArg[i]);
25153 rc = 1;
25154 sqlite3_free(zLike);
25155 goto meta_command_exit;
25156 }
25157 }else{
@@ -24436,12 +25181,12 @@
25181
25182 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
25183 /* When playing back a "dump", the content might appear in an order
25184 ** which causes immediate foreign key constraints to be violated.
25185 ** So disable foreign-key constraint enforcement to prevent problems. */
25186 oputz("PRAGMA foreign_keys=OFF;\n");
25187 oputz("BEGIN TRANSACTION;\n");
25188 }
25189 p->writableSchema = 0;
25190 p->showHeader = 0;
25191 /* Set writable_schema=ON since doing so forces SQLite to initialize
25192 ** as much of the schema as it can even if the sqlite_schema table is
@@ -24468,27 +25213,27 @@
25213 run_table_dump_query(p, zSql);
25214 sqlite3_free(zSql);
25215 }
25216 sqlite3_free(zLike);
25217 if( p->writableSchema ){
25218 oputz("PRAGMA writable_schema=OFF;\n");
25219 p->writableSchema = 0;
25220 }
25221 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
25222 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
25223 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
25224 oputz(p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
25225 }
25226 p->showHeader = savedShowHeader;
25227 p->shellFlgs = savedShellFlags;
25228 }else
25229
25230 if( c=='e' && cli_strncmp(azArg[0], "echo", n)==0 ){
25231 if( nArg==2 ){
25232 setOrClearFlag(p, SHFLG_Echo, azArg[1]);
25233 }else{
25234 eputz("Usage: .echo on|off\n");
25235 rc = 1;
25236 }
25237 }else
25238
25239 if( c=='e' && cli_strncmp(azArg[0], "eqp", n)==0 ){
@@ -24515,11 +25260,11 @@
25260 #endif
25261 }else{
25262 p->autoEQP = (u8)booleanValue(azArg[1]);
25263 }
25264 }else{
25265 eputz("Usage: .eqp off|on|trace|trigger|full\n");
25266 rc = 1;
25267 }
25268 }else
25269
25270 #ifndef SQLITE_SHELL_FIDDLE
@@ -24554,13 +25299,12 @@
25299 }else
25300
25301 #ifndef SQLITE_OMIT_VIRTUALTABLE
25302 if( c=='e' && cli_strncmp(azArg[0], "expert", n)==0 ){
25303 if( p->bSafeMode ){
25304 eputf("Cannot run experimental commands such as \"%s\" in safe mode\n",
25305 azArg[0]);
 
25306 rc = 1;
25307 }else{
25308 open_db(p, 0);
25309 expertDotCommand(p, azArg, nArg);
25310 }
@@ -24612,14 +25356,13 @@
25356 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
25357 }
25358
25359 /* --help lists all file-controls */
25360 if( cli_strcmp(zCmd,"help")==0 ){
25361 oputz("Available file-controls:\n");
25362 for(i=0; i<ArraySize(aCtrl); i++){
25363 oputf(" .filectrl %s %s\n", aCtrl[i].zCtrlName, aCtrl[i].zUsage);
 
25364 }
25365 rc = 1;
25366 goto meta_command_exit;
25367 }
25368
@@ -24630,20 +25373,20 @@
25373 if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
25374 if( filectrl<0 ){
25375 filectrl = aCtrl[i].ctrlCode;
25376 iCtrl = i;
25377 }else{
25378 eputf("Error: ambiguous file-control: \"%s\"\n"
25379 "Use \".filectrl --help\" for help\n", zCmd);
25380 rc = 1;
25381 goto meta_command_exit;
25382 }
25383 }
25384 }
25385 if( filectrl<0 ){
25386 eputf("Error: unknown file-control: %s\n"
25387 "Use \".filectrl --help\" for help\n", zCmd);
25388 }else{
25389 switch(filectrl){
25390 case SQLITE_FCNTL_SIZE_LIMIT: {
25391 if( nArg!=2 && nArg!=3 ) break;
25392 iRes = nArg==3 ? integerValue(azArg[2]) : -1;
@@ -24682,11 +25425,11 @@
25425 case SQLITE_FCNTL_TEMPFILENAME: {
25426 char *z = 0;
25427 if( nArg!=2 ) break;
25428 sqlite3_file_control(p->db, zSchema, filectrl, &z);
25429 if( z ){
25430 oputf("%s\n", z);
25431 sqlite3_free(z);
25432 }
25433 isOk = 2;
25434 break;
25435 }
@@ -24696,23 +25439,23 @@
25439 x = atoi(azArg[2]);
25440 sqlite3_file_control(p->db, zSchema, filectrl, &x);
25441 }
25442 x = -1;
25443 sqlite3_file_control(p->db, zSchema, filectrl, &x);
25444 oputf("%d\n", x);
25445 isOk = 2;
25446 break;
25447 }
25448 }
25449 }
25450 if( isOk==0 && iCtrl>=0 ){
25451 oputf("Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
25452 rc = 1;
25453 }else if( isOk==1 ){
25454 char zBuf[100];
25455 sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
25456 oputf("%s\n", zBuf);
25457 }
25458 }else
25459
25460 if( c=='f' && cli_strncmp(azArg[0], "fullschema", n)==0 ){
25461 ShellState data;
@@ -24723,11 +25466,11 @@
25466 if( nArg==2 && optionMatch(azArg[1], "indent") ){
25467 data.cMode = data.mode = MODE_Pretty;
25468 nArg = 1;
25469 }
25470 if( nArg!=1 ){
25471 eputz("Usage: .fullschema ?--indent?\n");
25472 rc = 1;
25473 goto meta_command_exit;
25474 }
25475 open_db(p, 0);
25476 rc = sqlite3_exec(p->db,
@@ -24749,37 +25492,37 @@
25492 doStats = sqlite3_step(pStmt)==SQLITE_ROW;
25493 sqlite3_finalize(pStmt);
25494 }
25495 }
25496 if( doStats==0 ){
25497 oputz("/* No STAT tables available */\n");
25498 }else{
25499 oputz("ANALYZE sqlite_schema;\n");
25500 data.cMode = data.mode = MODE_Insert;
25501 data.zDestTable = "sqlite_stat1";
25502 shell_exec(&data, "SELECT * FROM sqlite_stat1", 0);
25503 data.zDestTable = "sqlite_stat4";
25504 shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
25505 oputz("ANALYZE sqlite_schema;\n");
25506 }
25507 }else
25508
25509 if( c=='h' && cli_strncmp(azArg[0], "headers", n)==0 ){
25510 if( nArg==2 ){
25511 p->showHeader = booleanValue(azArg[1]);
25512 p->shellFlgs |= SHFLG_HeaderSet;
25513 }else{
25514 eputz("Usage: .headers on|off\n");
25515 rc = 1;
25516 }
25517 }else
25518
25519 if( c=='h' && cli_strncmp(azArg[0], "help", n)==0 ){
25520 if( nArg>=2 ){
25521 n = showHelp(p->out, azArg[1]);
25522 if( n==0 ){
25523 oputf("Nothing matches '%s'\n", azArg[1]);
25524 }
25525 }else{
25526 showHelp(p->out, 0);
25527 }
25528 }else
@@ -24819,11 +25562,11 @@
25562 if( zFile==0 ){
25563 zFile = z;
25564 }else if( zTable==0 ){
25565 zTable = z;
25566 }else{
25567 oputf("ERROR: extra argument: \"%s\". Usage:\n", z);
25568 showHelp(p->out, "import");
25569 goto meta_command_exit;
25570 }
25571 }else if( cli_strcmp(z,"-v")==0 ){
25572 eVerbose++;
@@ -24840,18 +25583,18 @@
25583 sCtx.cColSep = ',';
25584 sCtx.cRowSep = '\n';
25585 xRead = csv_read_one_field;
25586 useOutputMode = 0;
25587 }else{
25588 oputf("ERROR: unknown option: \"%s\". Usage:\n", z);
25589 showHelp(p->out, "import");
25590 goto meta_command_exit;
25591 }
25592 }
25593 if( zTable==0 ){
25594 oputf("ERROR: missing %s argument. Usage:\n",
25595 zFile==0 ? "FILE" : "TABLE");
25596 showHelp(p->out, "import");
25597 goto meta_command_exit;
25598 }
25599 seenInterrupt = 0;
25600 open_db(p, 0);
@@ -24858,24 +25601,21 @@
25601 if( useOutputMode ){
25602 /* If neither the --csv or --ascii options are specified, then set
25603 ** the column and row separator characters from the output mode. */
25604 nSep = strlen30(p->colSeparator);
25605 if( nSep==0 ){
25606 eputz("Error: non-null column separator required for import\n");
 
25607 goto meta_command_exit;
25608 }
25609 if( nSep>1 ){
25610 eputz("Error: multi-character column separators not allowed"
 
25611 " for import\n");
25612 goto meta_command_exit;
25613 }
25614 nSep = strlen30(p->rowSeparator);
25615 if( nSep==0 ){
25616 eputz("Error: non-null row separator required for import\n");
 
25617 goto meta_command_exit;
25618 }
25619 if( nSep==2 && p->mode==MODE_Csv
25620 && cli_strcmp(p->rowSeparator,SEP_CrLf)==0
25621 ){
@@ -24885,22 +25625,22 @@
25625 ** and output row separators. */
25626 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
25627 nSep = strlen30(p->rowSeparator);
25628 }
25629 if( nSep>1 ){
25630 eputz("Error: multi-character row separators not allowed"
25631 " for import\n");
25632 goto meta_command_exit;
25633 }
25634 sCtx.cColSep = (u8)p->colSeparator[0];
25635 sCtx.cRowSep = (u8)p->rowSeparator[0];
25636 }
25637 sCtx.zFile = zFile;
25638 sCtx.nLine = 1;
25639 if( sCtx.zFile[0]=='|' ){
25640 #ifdef SQLITE_OMIT_POPEN
25641 eputz("Error: pipes are not supported in this OS\n");
25642 goto meta_command_exit;
25643 #else
25644 sCtx.in = popen(sCtx.zFile+1, "r");
25645 sCtx.zFile = "<pipe>";
25646 sCtx.xCloser = pclose;
@@ -24908,23 +25648,23 @@
25648 }else{
25649 sCtx.in = fopen(sCtx.zFile, "rb");
25650 sCtx.xCloser = fclose;
25651 }
25652 if( sCtx.in==0 ){
25653 eputf("Error: cannot open \"%s\"\n", zFile);
25654 goto meta_command_exit;
25655 }
25656 if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
25657 char zSep[2];
25658 zSep[1] = 0;
25659 zSep[0] = sCtx.cColSep;
25660 oputz("Column separator ");
25661 output_c_string(zSep);
25662 oputz(", row separator ");
25663 zSep[0] = sCtx.cRowSep;
25664 output_c_string(zSep);
25665 oputz("\n");
25666 }
25667 sCtx.z = sqlite3_malloc64(120);
25668 if( sCtx.z==0 ){
25669 import_cleanup(&sCtx);
25670 shell_out_of_memory();
@@ -24955,18 +25695,18 @@
25695 zAutoColumn(sCtx.z, &dbCols, 0);
25696 if( sCtx.cTerm!=sCtx.cColSep ) break;
25697 }
25698 zColDefs = zAutoColumn(0, &dbCols, &zRenames);
25699 if( zRenames!=0 ){
25700 sputf((stdin_is_interactive && p->in==stdin)? p->out : stderr,
25701 "Columns renamed during .import %s due to duplicates:\n"
25702 "%s\n", sCtx.zFile, zRenames);
25703 sqlite3_free(zRenames);
25704 }
25705 assert(dbCols==0);
25706 if( zColDefs==0 ){
25707 eputf("%s: empty file\n", sCtx.zFile);
25708 import_fail:
25709 sqlite3_free(zCreate);
25710 sqlite3_free(zSql);
25711 sqlite3_free(zFullTabName);
25712 import_cleanup(&sCtx);
@@ -24973,24 +25713,24 @@
25713 rc = 1;
25714 goto meta_command_exit;
25715 }
25716 zCreate = sqlite3_mprintf("%z%z\n", zCreate, zColDefs);
25717 if( eVerbose>=1 ){
25718 oputf("%s\n", zCreate);
25719 }
25720 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
25721 if( rc ){
25722 eputf("%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db));
25723 goto import_fail;
25724 }
25725 sqlite3_free(zCreate);
25726 zCreate = 0;
25727 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
25728 }
25729 if( rc ){
25730 if (pStmt) sqlite3_finalize(pStmt);
25731 eputf("Error: %s\n", sqlite3_errmsg(p->db));
25732 goto import_fail;
25733 }
25734 sqlite3_free(zSql);
25735 nCol = sqlite3_column_count(pStmt);
25736 sqlite3_finalize(pStmt);
@@ -25008,15 +25748,15 @@
25748 zSql[j++] = '?';
25749 }
25750 zSql[j++] = ')';
25751 zSql[j] = 0;
25752 if( eVerbose>=2 ){
25753 oputf("Insert using: %s\n", zSql);
25754 }
25755 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
25756 if( rc ){
25757 eputf("Error: %s\n", sqlite3_errmsg(p->db));
25758 if (pStmt) sqlite3_finalize(pStmt);
25759 goto import_fail;
25760 }
25761 sqlite3_free(zSql);
25762 sqlite3_free(zFullTabName);
@@ -25045,32 +25785,31 @@
25785 if( z==0 && (xRead==csv_read_one_field) && i==nCol-1 && i>0 ){
25786 z = "";
25787 }
25788 sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
25789 if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
25790 eputf("%s:%d: expected %d columns but found %d"
25791 " - filling the rest with NULL\n",
25792 sCtx.zFile, startLine, nCol, i+1);
25793 i += 2;
25794 while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
25795 }
25796 }
25797 if( sCtx.cTerm==sCtx.cColSep ){
25798 do{
25799 xRead(&sCtx);
25800 i++;
25801 }while( sCtx.cTerm==sCtx.cColSep );
25802 eputf("%s:%d: expected %d columns but found %d - extras ignored\n",
25803 sCtx.zFile, startLine, nCol, i);
 
25804 }
25805 if( i>=nCol ){
25806 sqlite3_step(pStmt);
25807 rc = sqlite3_reset(pStmt);
25808 if( rc!=SQLITE_OK ){
25809 eputf("%s:%d: INSERT failed: %s\n",
25810 sCtx.zFile, startLine, sqlite3_errmsg(p->db));
25811 sCtx.nErr++;
25812 }else{
25813 sCtx.nRow++;
25814 }
25815 }
@@ -25078,13 +25817,12 @@
25817
25818 import_cleanup(&sCtx);
25819 sqlite3_finalize(pStmt);
25820 if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
25821 if( eVerbose>0 ){
25822 oputf("Added %d rows with %d errors using %d lines of input\n",
25823 sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
 
25824 }
25825 }else
25826 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
25827
25828 #ifndef SQLITE_UNTESTABLE
@@ -25095,18 +25833,18 @@
25833 int tnum = 0;
25834 int isWO = 0; /* True if making an imposter of a WITHOUT ROWID table */
25835 int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
25836 int i;
25837 if( !ShellHasFlag(p,SHFLG_TestingMode) ){
25838 eputf(".%s unavailable without --unsafe-testing\n",
25839 "imposter");
25840 rc = 1;
25841 goto meta_command_exit;
25842 }
25843 if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
25844 eputz("Usage: .imposter INDEX IMPOSTER\n"
25845 " .imposter off\n");
25846 /* Also allowed, but not documented:
25847 **
25848 ** .imposter TABLE IMPOSTER
25849 **
25850 ** where TABLE is a WITHOUT ROWID table. In that case, the
@@ -25161,11 +25899,11 @@
25899 zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
25900 }
25901 }
25902 sqlite3_finalize(pStmt);
25903 if( i==0 || tnum==0 ){
25904 eputf("no such index: \"%s\"\n", azArg[1]);
25905 rc = 1;
25906 sqlite3_free(zCollist);
25907 goto meta_command_exit;
25908 }
25909 if( lenPK==0 ) lenPK = 100000;
@@ -25176,20 +25914,18 @@
25914 rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
25915 if( rc==SQLITE_OK ){
25916 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
25917 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
25918 if( rc ){
25919 eputf("Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
25920 }else{
25921 sputf(stdout, "%s;\n", zSql);
25922 sputf(stdout, "WARNING: writing to an imposter table will corrupt"
25923 " the \"%s\" %s!\n", azArg[1], isWO ? "table" : "index");
 
 
25924 }
25925 }else{
25926 eputf("SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
25927 rc = 1;
25928 }
25929 sqlite3_free(zSql);
25930 }else
25931 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
@@ -25205,11 +25941,11 @@
25941 sqlite3IoTrace = iotracePrintf;
25942 iotrace = stdout;
25943 }else{
25944 iotrace = fopen(azArg[1], "w");
25945 if( iotrace==0 ){
25946 eputf("Error: cannot open \"%s\"\n", azArg[1]);
25947 sqlite3IoTrace = 0;
25948 rc = 1;
25949 }else{
25950 sqlite3IoTrace = iotracePrintf;
25951 }
@@ -25237,15 +25973,15 @@
25973 };
25974 int i, n2;
25975 open_db(p, 0);
25976 if( nArg==1 ){
25977 for(i=0; i<ArraySize(aLimit); i++){
25978 sputf(stdout, "%20s %d\n", aLimit[i].zLimitName,
25979 sqlite3_limit(p->db, aLimit[i].limitCode, -1));
25980 }
25981 }else if( nArg>3 ){
25982 eputz("Usage: .limit NAME ?NEW-VALUE?\n");
25983 rc = 1;
25984 goto meta_command_exit;
25985 }else{
25986 int iLimit = -1;
25987 n2 = strlen30(azArg[1]);
@@ -25252,29 +25988,29 @@
25988 for(i=0; i<ArraySize(aLimit); i++){
25989 if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
25990 if( iLimit<0 ){
25991 iLimit = i;
25992 }else{
25993 eputf("ambiguous limit: \"%s\"\n", azArg[1]);
25994 rc = 1;
25995 goto meta_command_exit;
25996 }
25997 }
25998 }
25999 if( iLimit<0 ){
26000 eputf("unknown limit: \"%s\"\n"
26001 "enter \".limits\" with no arguments for a list.\n",
26002 azArg[1]);
26003 rc = 1;
26004 goto meta_command_exit;
26005 }
26006 if( nArg==3 ){
26007 sqlite3_limit(p->db, aLimit[iLimit].limitCode,
26008 (int)integerValue(azArg[2]));
26009 }
26010 sputf(stdout, "%20s %d\n", aLimit[iLimit].zLimitName,
26011 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
26012 }
26013 }else
26014
26015 if( c=='l' && n>2 && cli_strncmp(azArg[0], "lint", n)==0 ){
26016 open_db(p, 0);
@@ -25286,38 +26022,38 @@
26022 const char *zFile, *zProc;
26023 char *zErrMsg = 0;
26024 failIfSafeMode(p, "cannot run .load in safe mode");
26025 if( nArg<2 || azArg[1][0]==0 ){
26026 /* Must have a non-empty FILE. (Will not load self.) */
26027 eputz("Usage: .load FILE ?ENTRYPOINT?\n");
26028 rc = 1;
26029 goto meta_command_exit;
26030 }
26031 zFile = azArg[1];
26032 zProc = nArg>=3 ? azArg[2] : 0;
26033 open_db(p, 0);
26034 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
26035 if( rc!=SQLITE_OK ){
26036 eputf("Error: %s\n", zErrMsg);
26037 sqlite3_free(zErrMsg);
26038 rc = 1;
26039 }
26040 }else
26041 #endif
26042
26043 if( c=='l' && cli_strncmp(azArg[0], "log", n)==0 ){
26044 if( nArg!=2 ){
26045 eputz("Usage: .log FILENAME\n");
26046 rc = 1;
26047 }else{
26048 const char *zFile = azArg[1];
26049 if( p->bSafeMode
26050 && cli_strcmp(zFile,"on")!=0
26051 && cli_strcmp(zFile,"off")!=0
26052 ){
26053 sputz(stdout, "cannot set .log to anything other"
26054 " than \"on\" or \"off\"\n");
26055 zFile = "off";
26056 }
26057 output_file_close(p->pLog);
26058 if( cli_strcmp(zFile,"on")==0 ) zFile = "stdout";
26059 p->pLog = output_file_open(zFile, 0);
@@ -25352,37 +26088,35 @@
26088 cmOpts = cmo;
26089 }
26090 }else if( zTabname==0 ){
26091 zTabname = z;
26092 }else if( z[0]=='-' ){
26093 eputf("unknown option: %s\n", z);
26094 eputz("options:\n"
26095 " --noquote\n"
26096 " --quote\n"
26097 " --wordwrap on/off\n"
26098 " --wrap N\n"
26099 " --ww\n");
26100 rc = 1;
26101 goto meta_command_exit;
26102 }else{
26103 eputf("extra argument: \"%s\"\n", z);
26104 rc = 1;
26105 goto meta_command_exit;
26106 }
26107 }
26108 if( zMode==0 ){
26109 if( p->mode==MODE_Column
26110 || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
26111 ){
26112 oputf("current output mode: %s --wrap %d --wordwrap %s --%squote\n",
26113 modeDescr[p->mode], p->cmOpts.iWrap,
26114 p->cmOpts.bWordWrap ? "on" : "off",
26115 p->cmOpts.bQuote ? "" : "no");
 
 
26116 }else{
26117 oputf("current output mode: %s\n", modeDescr[p->mode]);
26118 }
26119 zMode = modeDescr[p->mode];
26120 }
26121 n2 = strlen30(zMode);
26122 if( cli_strncmp(zMode,"lines",n2)==0 ){
@@ -25437,26 +26171,26 @@
26171 }else if( cli_strncmp(zMode,"off",n2)==0 ){
26172 p->mode = MODE_Off;
26173 }else if( cli_strncmp(zMode,"json",n2)==0 ){
26174 p->mode = MODE_Json;
26175 }else{
26176 eputz("Error: mode should be one of: "
26177 "ascii box column csv html insert json line list markdown "
26178 "qbox quote table tabs tcl\n");
26179 rc = 1;
26180 }
26181 p->cMode = p->mode;
26182 }else
26183
26184 #ifndef SQLITE_SHELL_FIDDLE
26185 if( c=='n' && cli_strcmp(azArg[0], "nonce")==0 ){
26186 if( nArg!=2 ){
26187 eputz("Usage: .nonce NONCE\n");
26188 rc = 1;
26189 }else if( p->zNonce==0 || cli_strcmp(azArg[1],p->zNonce)!=0 ){
26190 eputf("line %d: incorrect nonce: \"%s\"\n",
26191 p->lineno, azArg[1]);
26192 exit(1);
26193 }else{
26194 p->bSafeMode = 0;
26195 return 0; /* Return immediately to bypass the safe mode reset
26196 ** at the end of this procedure */
@@ -25467,11 +26201,11 @@
26201 if( c=='n' && cli_strncmp(azArg[0], "nullvalue", n)==0 ){
26202 if( nArg==2 ){
26203 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
26204 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
26205 }else{
26206 eputz("Usage: .nullvalue STRING\n");
26207 rc = 1;
26208 }
26209 }else
26210
26211 if( c=='o' && cli_strncmp(azArg[0], "open", n)==0 && n>=2 ){
@@ -25506,15 +26240,15 @@
26240 p->szMax = integerValue(azArg[++iName]);
26241 #endif /* SQLITE_OMIT_DESERIALIZE */
26242 }else
26243 #endif /* !SQLITE_SHELL_FIDDLE */
26244 if( z[0]=='-' ){
26245 eputf("unknown option: %s\n", z);
26246 rc = 1;
26247 goto meta_command_exit;
26248 }else if( zFN ){
26249 eputf("extra argument: \"%s\"\n", z);
26250 rc = 1;
26251 goto meta_command_exit;
26252 }else{
26253 zFN = z;
26254 }
@@ -25552,11 +26286,11 @@
26286 zNewFilename = 0;
26287 }
26288 p->pAuxDb->zDbFilename = zNewFilename;
26289 open_db(p, OPEN_DB_KEEPALIVE);
26290 if( p->db==0 ){
26291 eputf("Error: cannot open '%s'\n", zNewFilename);
26292 sqlite3_free(zNewFilename);
26293 }else{
26294 p->pAuxDb->zFreeOnClose = zNewFilename;
26295 }
26296 }
@@ -25576,13 +26310,13 @@
26310 char *zFile = 0;
26311 int bTxtMode = 0;
26312 int i;
26313 int eMode = 0;
26314 int bOnce = 0; /* 0: .output, 1: .once, 2: .excel */
26315 static const char *zBomUtf8 = "\xef\xbb\xbf";
26316 const char *zBom = 0;
26317
 
26318 failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
26319 if( c=='e' ){
26320 eMode = 'x';
26321 bOnce = 2;
26322 }else if( cli_strncmp(azArg[0],"once",n)==0 ){
@@ -25591,21 +26325,17 @@
26325 for(i=1; i<nArg; i++){
26326 char *z = azArg[i];
26327 if( z[0]=='-' ){
26328 if( z[1]=='-' ) z++;
26329 if( cli_strcmp(z,"-bom")==0 ){
26330 zBom = zBomUtf8;
 
 
 
26331 }else if( c!='e' && cli_strcmp(z,"-x")==0 ){
26332 eMode = 'x'; /* spreadsheet */
26333 }else if( c!='e' && cli_strcmp(z,"-e")==0 ){
26334 eMode = 'e'; /* text editor */
26335 }else{
26336 oputf("ERROR: unknown option: \"%s\". Usage:\n", azArg[i]);
 
26337 showHelp(p->out, azArg[0]);
26338 rc = 1;
26339 goto meta_command_exit;
26340 }
26341 }else if( zFile==0 && eMode!='e' && eMode!='x' ){
@@ -25613,12 +26343,11 @@
26343 if( zFile && zFile[0]=='|' ){
26344 while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
26345 break;
26346 }
26347 }else{
26348 oputf("ERROR: extra parameter: \"%s\". Usage:\n", azArg[i]);
 
26349 showHelp(p->out, azArg[0]);
26350 rc = 1;
26351 sqlite3_free(zFile);
26352 goto meta_command_exit;
26353 }
@@ -25653,34 +26382,34 @@
26382 }
26383 #endif /* SQLITE_NOHAVE_SYSTEM */
26384 shell_check_oom(zFile);
26385 if( zFile[0]=='|' ){
26386 #ifdef SQLITE_OMIT_POPEN
26387 eputz("Error: pipes are not supported in this OS\n");
26388 rc = 1;
26389 output_redir(p, stdout);
26390 #else
26391 FILE *pfPipe = popen(zFile + 1, "w");
26392 if( pfPipe==0 ){
26393 eputf("Error: cannot open pipe \"%s\"\n", zFile + 1);
 
26394 rc = 1;
26395 }else{
26396 output_redir(p, pfPipe);
26397 if( zBom ) oputz(zBom);
26398 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
26399 }
26400 #endif
26401 }else{
26402 FILE *pfFile = output_file_open(zFile, bTxtMode);
26403 if( pfFile==0 ){
26404 if( cli_strcmp(zFile,"off")!=0 ){
26405 eputf("Error: cannot write to \"%s\"\n", zFile);
26406 }
 
26407 rc = 1;
26408 } else {
26409 output_redir(p, pfFile);
26410 if( zBom ) oputz(zBom);
26411 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
26412 }
26413 }
26414 sqlite3_free(zFile);
26415 }else
@@ -25717,12 +26446,12 @@
26446 if( len ){
26447 rx = sqlite3_prepare_v2(p->db,
26448 "SELECT key, quote(value) "
26449 "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
26450 while( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
26451 oputf("%-*s %s\n", len, sqlite3_column_text(pStmt,0),
26452 sqlite3_column_text(pStmt,1));
26453 }
26454 sqlite3_finalize(pStmt);
26455 }
26456 }else
26457
@@ -25762,11 +26491,11 @@
26491 "VALUES(%Q,%Q);", zKey, zValue);
26492 shell_check_oom(zSql);
26493 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
26494 sqlite3_free(zSql);
26495 if( rx!=SQLITE_OK ){
26496 oputf("Error: %s\n", sqlite3_errmsg(p->db));
26497 sqlite3_finalize(pStmt);
26498 pStmt = 0;
26499 rc = 1;
26500 }
26501 }
@@ -25791,14 +26520,14 @@
26520 }else
26521
26522 if( c=='p' && n>=3 && cli_strncmp(azArg[0], "print", n)==0 ){
26523 int i;
26524 for(i=1; i<nArg; i++){
26525 if( i>1 ) oputz(" ");
26526 oputz(azArg[i]);
26527 }
26528 oputz("\n");
26529 }else
26530
26531 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
26532 if( c=='p' && n>=3 && cli_strncmp(azArg[0], "progress", n)==0 ){
26533 int i;
@@ -25823,19 +26552,19 @@
26552 p->flgProgress |= SHELL_PROGRESS_ONCE;
26553 continue;
26554 }
26555 if( cli_strcmp(z,"limit")==0 ){
26556 if( i+1>=nArg ){
26557 eputz("Error: missing argument on --limit\n");
26558 rc = 1;
26559 goto meta_command_exit;
26560 }else{
26561 p->mxProgress = (int)integerValue(azArg[++i]);
26562 }
26563 continue;
26564 }
26565 eputf("Error: unknown option: \"%s\"\n", azArg[i]);
26566 rc = 1;
26567 goto meta_command_exit;
26568 }else{
26569 nn = (int)integerValue(z);
26570 }
@@ -25864,31 +26593,31 @@
26593 if( c=='r' && n>=3 && cli_strncmp(azArg[0], "read", n)==0 ){
26594 FILE *inSaved = p->in;
26595 int savedLineno = p->lineno;
26596 failIfSafeMode(p, "cannot run .read in safe mode");
26597 if( nArg!=2 ){
26598 eputz("Usage: .read FILE\n");
26599 rc = 1;
26600 goto meta_command_exit;
26601 }
26602 if( azArg[1][0]=='|' ){
26603 #ifdef SQLITE_OMIT_POPEN
26604 eputz("Error: pipes are not supported in this OS\n");
26605 rc = 1;
26606 p->out = stdout;
26607 #else
26608 p->in = popen(azArg[1]+1, "r");
26609 if( p->in==0 ){
26610 eputf("Error: cannot open \"%s\"\n", azArg[1]);
26611 rc = 1;
26612 }else{
26613 rc = process_input(p);
26614 pclose(p->in);
26615 }
26616 #endif
26617 }else if( (p->in = openChrSource(azArg[1]))==0 ){
26618 eputf("Error: cannot open \"%s\"\n", azArg[1]);
26619 rc = 1;
26620 }else{
26621 rc = process_input(p);
26622 fclose(p->in);
26623 }
@@ -25911,24 +26640,24 @@
26640 zDb = "main";
26641 }else if( nArg==3 ){
26642 zSrcFile = azArg[2];
26643 zDb = azArg[1];
26644 }else{
26645 eputz("Usage: .restore ?DB? FILE\n");
26646 rc = 1;
26647 goto meta_command_exit;
26648 }
26649 rc = sqlite3_open(zSrcFile, &pSrc);
26650 if( rc!=SQLITE_OK ){
26651 eputf("Error: cannot open \"%s\"\n", zSrcFile);
26652 close_db(pSrc);
26653 return 1;
26654 }
26655 open_db(p, 0);
26656 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
26657 if( pBackup==0 ){
26658 eputf("Error: %s\n", sqlite3_errmsg(p->db));
26659 close_db(pSrc);
26660 return 1;
26661 }
26662 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
26663 || rc==SQLITE_BUSY ){
@@ -25939,14 +26668,14 @@
26668 }
26669 sqlite3_backup_finish(pBackup);
26670 if( rc==SQLITE_DONE ){
26671 rc = 0;
26672 }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
26673 eputz("Error: source database is busy\n");
26674 rc = 1;
26675 }else{
26676 eputf("Error: %s\n", sqlite3_errmsg(p->db));
26677 rc = 1;
26678 }
26679 close_db(pSrc);
26680 }else
26681 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
@@ -25963,15 +26692,19 @@
26692 }
26693 open_db(p, 0);
26694 sqlite3_db_config(
26695 p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, p->scanstatsOn, (int*)0
26696 );
26697 #if !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
26698 eputz("Warning: .scanstats not available in this build.\n");
26699 #elif !defined(SQLITE_ENABLE_BYTECODE_VTAB)
26700 if( p->scanstatsOn==3 ){
26701 eputz("Warning: \".scanstats vm\" not available in this build.\n");
26702 }
26703 #endif
26704 }else{
26705 eputz("Usage: .scanstats on|off|est\n");
26706 rc = 1;
26707 }
26708 }else
26709
26710 if( c=='s' && cli_strncmp(azArg[0], "schema", n)==0 ){
@@ -25996,18 +26729,17 @@
26729 }else if( optionMatch(azArg[ii],"debug") ){
26730 bDebug = 1;
26731 }else if( optionMatch(azArg[ii],"nosys") ){
26732 bNoSystemTabs = 1;
26733 }else if( azArg[ii][0]=='-' ){
26734 eputf("Unknown option: \"%s\"\n", azArg[ii]);
26735 rc = 1;
26736 goto meta_command_exit;
26737 }else if( zName==0 ){
26738 zName = azArg[ii];
26739 }else{
26740 eputz("Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
 
26741 rc = 1;
26742 goto meta_command_exit;
26743 }
26744 }
26745 if( zName!=0 ){
@@ -26036,11 +26768,11 @@
26768 if( zDiv ){
26769 sqlite3_stmt *pStmt = 0;
26770 rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
26771 -1, &pStmt, 0);
26772 if( rc ){
26773 eputf("Error: %s\n", sqlite3_errmsg(p->db));
26774 sqlite3_finalize(pStmt);
26775 rc = 1;
26776 goto meta_command_exit;
26777 }
26778 appendText(&sSelect, "SELECT sql FROM", 0);
@@ -26098,22 +26830,22 @@
26830 appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
26831 }
26832 appendText(&sSelect, "sql IS NOT NULL"
26833 " ORDER BY snum, rowid", 0);
26834 if( bDebug ){
26835 oputf("SQL: %s;\n", sSelect.z);
26836 }else{
26837 rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
26838 }
26839 freeText(&sSelect);
26840 }
26841 if( zErrMsg ){
26842 eputf("Error: %s\n", zErrMsg);
26843 sqlite3_free(zErrMsg);
26844 rc = 1;
26845 }else if( rc != SQLITE_OK ){
26846 eputz("Error: querying schema information\n");
26847 rc = 1;
26848 }else{
26849 rc = 0;
26850 }
26851 }else
@@ -26155,15 +26887,15 @@
26887 */
26888 if( cli_strcmp(azCmd[0],"attach")==0 ){
26889 if( nCmd!=2 ) goto session_syntax_error;
26890 if( pSession->p==0 ){
26891 session_not_open:
26892 eputz("ERROR: No sessions are open\n");
26893 }else{
26894 rc = sqlite3session_attach(pSession->p, azCmd[1]);
26895 if( rc ){
26896 eputf("ERROR: sqlite3session_attach() returns %d\n",rc);
26897 rc = 0;
26898 }
26899 }
26900 }else
26901
@@ -26178,28 +26910,27 @@
26910 failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]);
26911 if( nCmd!=2 ) goto session_syntax_error;
26912 if( pSession->p==0 ) goto session_not_open;
26913 out = fopen(azCmd[1], "wb");
26914 if( out==0 ){
26915 eputf("ERROR: cannot open \"%s\" for writing\n",
26916 azCmd[1]);
26917 }else{
26918 int szChng;
26919 void *pChng;
26920 if( azCmd[0][0]=='c' ){
26921 rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
26922 }else{
26923 rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
26924 }
26925 if( rc ){
26926 sputf(stdout, "Error: error code %d\n", rc);
26927 rc = 0;
26928 }
26929 if( pChng
26930 && fwrite(pChng, szChng, 1, out)!=1 ){
26931 eputf("ERROR: Failed to write entire %d-byte output\n", szChng);
 
26932 }
26933 sqlite3_free(pChng);
26934 fclose(out);
26935 }
26936 }else
@@ -26222,12 +26953,11 @@
26953 int ii;
26954 if( nCmd>2 ) goto session_syntax_error;
26955 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
26956 if( pAuxDb->nSession ){
26957 ii = sqlite3session_enable(pSession->p, ii);
26958 oputf("session %s enable flag = %d\n", pSession->zName, ii);
 
26959 }
26960 }else
26961
26962 /* .session filter GLOB ....
26963 ** Set a list of GLOB patterns of table names to be excluded.
@@ -26240,14 +26970,11 @@
26970 sqlite3_free(pSession->azFilter[ii]);
26971 }
26972 sqlite3_free(pSession->azFilter);
26973 nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
26974 pSession->azFilter = sqlite3_malloc( nByte );
26975 shell_check_oom( pSession->azFilter );
 
 
 
26976 for(ii=1; ii<nCmd; ii++){
26977 char *x = pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
26978 shell_check_oom(x);
26979 }
26980 pSession->nFilter = ii-1;
@@ -26261,12 +26988,11 @@
26988 int ii;
26989 if( nCmd>2 ) goto session_syntax_error;
26990 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
26991 if( pAuxDb->nSession ){
26992 ii = sqlite3session_indirect(pSession->p, ii);
26993 oputf("session %s indirect flag = %d\n", pSession->zName, ii);
 
26994 }
26995 }else
26996
26997 /* .session isempty
26998 ** Determine if the session is empty
@@ -26274,21 +27000,20 @@
27000 if( cli_strcmp(azCmd[0], "isempty")==0 ){
27001 int ii;
27002 if( nCmd!=1 ) goto session_syntax_error;
27003 if( pAuxDb->nSession ){
27004 ii = sqlite3session_isempty(pSession->p);
27005 oputf("session %s isempty flag = %d\n", pSession->zName, ii);
 
27006 }
27007 }else
27008
27009 /* .session list
27010 ** List all currently open sessions
27011 */
27012 if( cli_strcmp(azCmd[0],"list")==0 ){
27013 for(i=0; i<pAuxDb->nSession; i++){
27014 oputf("%d %s\n", i, pAuxDb->aSession[i].zName);
27015 }
27016 }else
27017
27018 /* .session open DB NAME
27019 ** Open a new session called NAME on the attached database DB.
@@ -26299,23 +27024,22 @@
27024 if( nCmd!=3 ) goto session_syntax_error;
27025 zName = azCmd[2];
27026 if( zName[0]==0 ) goto session_syntax_error;
27027 for(i=0; i<pAuxDb->nSession; i++){
27028 if( cli_strcmp(pAuxDb->aSession[i].zName,zName)==0 ){
27029 eputf("Session \"%s\" already exists\n", zName);
27030 goto meta_command_exit;
27031 }
27032 }
27033 if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){
27034 eputf("Maximum of %d sessions\n", ArraySize(pAuxDb->aSession));
 
27035 goto meta_command_exit;
27036 }
27037 pSession = &pAuxDb->aSession[pAuxDb->nSession];
27038 rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
27039 if( rc ){
27040 eputf("Cannot open session: error code=%d\n", rc);
27041 rc = 0;
27042 goto meta_command_exit;
27043 }
27044 pSession->nFilter = 0;
27045 sqlite3session_table_filter(pSession->p, session_filter, pSession);
@@ -26335,20 +27059,20 @@
27059 if( c=='s' && n>=10 && cli_strncmp(azArg[0], "selftest-", 9)==0 ){
27060 if( cli_strncmp(azArg[0]+9, "boolean", n-9)==0 ){
27061 int i, v;
27062 for(i=1; i<nArg; i++){
27063 v = booleanValue(azArg[i]);
27064 oputf("%s: %d 0x%x\n", azArg[i], v, v);
27065 }
27066 }
27067 if( cli_strncmp(azArg[0]+9, "integer", n-9)==0 ){
27068 int i; sqlite3_int64 v;
27069 for(i=1; i<nArg; i++){
27070 char zBuf[200];
27071 v = integerValue(azArg[i]);
27072 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
27073 oputz(zBuf);
27074 }
27075 }
27076 }else
27077 #endif
27078
@@ -26371,13 +27095,12 @@
27095 }else
27096 if( cli_strcmp(z,"-v")==0 ){
27097 bVerbose++;
27098 }else
27099 {
27100 eputf("Unknown option \"%s\" on \"%s\"\n", azArg[i], azArg[0]);
27101 eputz("Should be one of: --init -v\n");
 
27102 rc = 1;
27103 goto meta_command_exit;
27104 }
27105 }
27106 if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
@@ -26402,11 +27125,11 @@
27125 "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
27126 " (1,'run','PRAGMA integrity_check','ok')",
27127 -1, &pStmt, 0);
27128 }
27129 if( rc ){
27130 eputz("Error querying the selftest table\n");
27131 rc = 1;
27132 sqlite3_finalize(pStmt);
27133 goto meta_command_exit;
27134 }
27135 for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
@@ -26418,52 +27141,51 @@
27141 if( zOp==0 ) continue;
27142 if( zSql==0 ) continue;
27143 if( zAns==0 ) continue;
27144 k = 0;
27145 if( bVerbose>0 ){
27146 sputf(stdout, "%d: %s %s\n", tno, zOp, zSql);
27147 }
27148 if( cli_strcmp(zOp,"memo")==0 ){
27149 oputf("%s\n", zSql);
27150 }else
27151 if( cli_strcmp(zOp,"run")==0 ){
27152 char *zErrMsg = 0;
27153 str.n = 0;
27154 str.z[0] = 0;
27155 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
27156 nTest++;
27157 if( bVerbose ){
27158 oputf("Result: %s\n", str.z);
27159 }
27160 if( rc || zErrMsg ){
27161 nErr++;
27162 rc = 1;
27163 oputf("%d: error-code-%d: %s\n", tno, rc, zErrMsg);
27164 sqlite3_free(zErrMsg);
27165 }else if( cli_strcmp(zAns,str.z)!=0 ){
27166 nErr++;
27167 rc = 1;
27168 oputf("%d: Expected: [%s]\n", tno, zAns);
27169 oputf("%d: Got: [%s]\n", tno, str.z);
27170 }
27171 }
27172 else{
27173 eputf("Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
 
27174 rc = 1;
27175 break;
27176 }
27177 } /* End loop over rows of content from SELFTEST */
27178 sqlite3_finalize(pStmt);
27179 } /* End loop over k */
27180 freeText(&str);
27181 oputf("%d errors out of %d tests\n", nErr, nTest);
27182 }else
27183
27184 if( c=='s' && cli_strncmp(azArg[0], "separator", n)==0 ){
27185 if( nArg<2 || nArg>3 ){
27186 eputz("Usage: .separator COL ?ROW?\n");
27187 rc = 1;
27188 }
27189 if( nArg>=2 ){
27190 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
27191 "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
@@ -26502,18 +27224,17 @@
27224 }else
27225 if( cli_strcmp(z,"debug")==0 ){
27226 bDebug = 1;
27227 }else
27228 {
27229 eputf("Unknown option \"%s\" on \"%s\"\n", azArg[i], azArg[0]);
 
27230 showHelp(p->out, azArg[0]);
27231 rc = 1;
27232 goto meta_command_exit;
27233 }
27234 }else if( zLike ){
27235 eputz("Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
27236 rc = 1;
27237 goto meta_command_exit;
27238 }else{
27239 zLike = z;
27240 bSeparate = 1;
@@ -26581,11 +27302,11 @@
27302 }
27303 shell_check_oom(zSql);
27304 freeText(&sQuery);
27305 freeText(&sSql);
27306 if( bDebug ){
27307 oputf("%s\n", zSql);
27308 }else{
27309 shell_exec(p, zSql, 0);
27310 }
27311 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && !defined(SQLITE_OMIT_VIRTUALTABLE)
27312 {
@@ -26611,11 +27332,11 @@
27332 "||group_concat('CAST(CAST('||cname||' AS BLOB) AS TEXT)<>'||cname\n"
27333 "|| ' AND typeof('||cname||')=''text'' ',\n"
27334 "' OR ') as query, tname from tabcols group by tname)"
27335 , zRevText);
27336 shell_check_oom(zRevText);
27337 if( bDebug ) oputf("%s\n", zRevText);
27338 lrc = sqlite3_prepare_v2(p->db, zRevText, -1, &pStmt, 0);
27339 if( lrc!=SQLITE_OK ){
27340 /* assert(lrc==SQLITE_NOMEM); // might also be SQLITE_ERROR if the
27341 ** user does cruel and unnatural things like ".limit expr_depth 0". */
27342 rc = 1;
@@ -26624,29 +27345,28 @@
27345 lrc = SQLITE_ROW==sqlite3_step(pStmt);
27346 if( lrc ){
27347 const char *zGenQuery = (char*)sqlite3_column_text(pStmt,0);
27348 sqlite3_stmt *pCheckStmt;
27349 lrc = sqlite3_prepare_v2(p->db, zGenQuery, -1, &pCheckStmt, 0);
27350 if( bDebug ) oputf("%s\n", zGenQuery);
27351 if( lrc!=SQLITE_OK ){
27352 rc = 1;
27353 }else{
27354 if( SQLITE_ROW==sqlite3_step(pCheckStmt) ){
27355 double countIrreversible = sqlite3_column_double(pCheckStmt, 0);
27356 if( countIrreversible>0 ){
27357 int sz = (int)(countIrreversible + 0.5);
27358 eputf("Digest includes %d invalidly encoded text field%s.\n",
27359 sz, (sz>1)? "s": "");
 
27360 }
27361 }
27362 sqlite3_finalize(pCheckStmt);
27363 }
27364 sqlite3_finalize(pStmt);
27365 }
27366 }
27367 if( rc ) eputz(".sha3sum failed.\n");
27368 sqlite3_free(zRevText);
27369 }
27370 #endif /* !defined(*_OMIT_SCHEMA_PRAGMAS) && !defined(*_OMIT_VIRTUALTABLE) */
27371 sqlite3_free(zSql);
27372 }else
@@ -26658,76 +27378,77 @@
27378 ){
27379 char *zCmd;
27380 int i, x;
27381 failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
27382 if( nArg<2 ){
27383 eputz("Usage: .system COMMAND\n");
27384 rc = 1;
27385 goto meta_command_exit;
27386 }
27387 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
27388 for(i=2; i<nArg && zCmd!=0; i++){
27389 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
27390 zCmd, azArg[i]);
27391 }
27392 consoleRestore();
27393 x = zCmd!=0 ? system(zCmd) : 1;
27394 consoleRenewSetup();
27395 sqlite3_free(zCmd);
27396 if( x ) eputf("System command returns %d\n", x);
27397 }else
27398 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) */
27399
27400 if( c=='s' && cli_strncmp(azArg[0], "show", n)==0 ){
27401 static const char *azBool[] = { "off", "on", "trigger", "full"};
27402 const char *zOut;
27403 int i;
27404 if( nArg!=1 ){
27405 eputz("Usage: .show\n");
27406 rc = 1;
27407 goto meta_command_exit;
27408 }
27409 oputf("%12.12s: %s\n","echo",
27410 azBool[ShellHasFlag(p, SHFLG_Echo)]);
27411 oputf("%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
27412 oputf("%12.12s: %s\n","explain",
27413 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
27414 oputf("%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
27415 if( p->mode==MODE_Column
27416 || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
27417 ){
27418 oputf("%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode",
27419 modeDescr[p->mode], p->cmOpts.iWrap,
27420 p->cmOpts.bWordWrap ? "on" : "off",
27421 p->cmOpts.bQuote ? "" : "no");
 
27422 }else{
27423 oputf("%12.12s: %s\n","mode", modeDescr[p->mode]);
27424 }
27425 oputf("%12.12s: ", "nullvalue");
27426 output_c_string(p->nullValue);
27427 oputz("\n");
27428 oputf("%12.12s: %s\n","output",
27429 strlen30(p->outfile) ? p->outfile : "stdout");
27430 oputf("%12.12s: ", "colseparator");
27431 output_c_string(p->colSeparator);
27432 oputz("\n");
27433 oputf("%12.12s: ", "rowseparator");
27434 output_c_string(p->rowSeparator);
27435 oputz("\n");
27436 switch( p->statsOn ){
27437 case 0: zOut = "off"; break;
27438 default: zOut = "on"; break;
27439 case 2: zOut = "stmt"; break;
27440 case 3: zOut = "vmstep"; break;
27441 }
27442 oputf("%12.12s: %s\n","stats", zOut);
27443 oputf("%12.12s: ", "width");
27444 for (i=0;i<p->nWidth;i++) {
27445 oputf("%d ", p->colWidth[i]);
27446 }
27447 oputz("\n");
27448 oputf("%12.12s: %s\n", "filename",
27449 p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : "");
27450 }else
27451
27452 if( c=='s' && cli_strncmp(azArg[0], "stats", n)==0 ){
27453 if( nArg==2 ){
27454 if( cli_strcmp(azArg[1],"stmt")==0 ){
@@ -26738,11 +27459,11 @@
27459 p->statsOn = (u8)booleanValue(azArg[1]);
27460 }
27461 }else if( nArg==1 ){
27462 display_stats(p->db, p, 0);
27463 }else{
27464 eputz("Usage: .stats ?on|off|stmt|vmstep?\n");
27465 rc = 1;
27466 }
27467 }else
27468
27469 if( (c=='t' && n>1 && cli_strncmp(azArg[0], "tables", n)==0)
@@ -26764,11 +27485,11 @@
27485
27486 if( nArg>2 && c=='i' ){
27487 /* It is an historical accident that the .indexes command shows an error
27488 ** when called with the wrong number of arguments whereas the .tables
27489 ** command does not. */
27490 eputz("Usage: .indexes ?LIKE-PATTERN?\n");
27491 rc = 1;
27492 sqlite3_finalize(pStmt);
27493 goto meta_command_exit;
27494 }
27495 for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
@@ -26840,14 +27561,13 @@
27561 if( nPrintCol<1 ) nPrintCol = 1;
27562 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
27563 for(i=0; i<nPrintRow; i++){
27564 for(j=i; j<nRow; j+=nPrintRow){
27565 char *zSp = j<nPrintRow ? "" : " ";
27566 oputf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j]:"");
 
27567 }
27568 oputz("\n");
27569 }
27570 }
27571
27572 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
27573 sqlite3_free(azResult);
@@ -26857,11 +27577,11 @@
27577 /* Begin redirecting output to the file "testcase-out.txt" */
27578 if( c=='t' && cli_strcmp(azArg[0],"testcase")==0 ){
27579 output_reset(p);
27580 p->out = output_file_open("testcase-out.txt", 0);
27581 if( p->out==0 ){
27582 eputz("Error: cannot open 'testcase-out.txt'\n");
27583 }
27584 if( nArg>=2 ){
27585 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
27586 }else{
27587 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
@@ -26898,11 +27618,11 @@
27618 {"prng_save", SQLITE_TESTCTRL_PRNG_SAVE, 0, "" },
27619 {"prng_seed", SQLITE_TESTCTRL_PRNG_SEED, 0, "SEED ?db?" },
27620 {"seek_count", SQLITE_TESTCTRL_SEEK_COUNT, 0, "" },
27621 {"sorter_mmap", SQLITE_TESTCTRL_SORTER_MMAP, 0, "NMAX" },
27622 {"tune", SQLITE_TESTCTRL_TUNE, 1, "ID VALUE" },
27623 {"uselongdouble", SQLITE_TESTCTRL_USELONGDOUBLE,0,"?BOOLEAN|\"default\"?"},
27624 };
27625 int testctrl = -1;
27626 int iCtrl = -1;
27627 int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */
27628 int isOk = 0;
@@ -26918,15 +27638,15 @@
27638 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
27639 }
27640
27641 /* --help lists all test-controls */
27642 if( cli_strcmp(zCmd,"help")==0 ){
27643 oputz("Available test-controls:\n");
27644 for(i=0; i<ArraySize(aCtrl); i++){
27645 if( aCtrl[i].unSafe && !ShellHasFlag(p,SHFLG_TestingMode) ) continue;
27646 oputf(" .testctrl %s %s\n",
27647 aCtrl[i].zCtrlName, aCtrl[i].zUsage);
27648 }
27649 rc = 1;
27650 goto meta_command_exit;
27651 }
27652
@@ -26938,20 +27658,20 @@
27658 if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
27659 if( testctrl<0 ){
27660 testctrl = aCtrl[i].ctrlCode;
27661 iCtrl = i;
27662 }else{
27663 eputf("Error: ambiguous test-control: \"%s\"\n"
27664 "Use \".testctrl --help\" for help\n", zCmd);
27665 rc = 1;
27666 goto meta_command_exit;
27667 }
27668 }
27669 }
27670 if( testctrl<0 ){
27671 eputf("Error: unknown test-control: %s\n"
27672 "Use \".testctrl --help\" for help\n", zCmd);
27673 }else{
27674 switch(testctrl){
27675
27676 /* sqlite3_test_control(int, db, int) */
27677 case SQLITE_TESTCTRL_OPTIMIZATIONS:
@@ -26987,11 +27707,11 @@
27707 if( nArg==3 || nArg==4 ){
27708 int ii = (int)integerValue(azArg[2]);
27709 sqlite3 *db;
27710 if( ii==0 && cli_strcmp(azArg[2],"random")==0 ){
27711 sqlite3_randomness(sizeof(ii),&ii);
27712 sputf(stdout, "-- random seed: %d\n", ii);
27713 }
27714 if( nArg==3 ){
27715 db = 0;
27716 }else{
27717 db = p->db;
@@ -27055,11 +27775,11 @@
27775 break;
27776
27777 case SQLITE_TESTCTRL_SEEK_COUNT: {
27778 u64 x = 0;
27779 rc2 = sqlite3_test_control(testctrl, p->db, &x);
27780 oputf("%llu\n", x);
27781 isOk = 3;
27782 break;
27783 }
27784
27785 #ifdef YYCOVERAGE
@@ -27086,15 +27806,15 @@
27806 int id = 1;
27807 while(1){
27808 int val = 0;
27809 rc2 = sqlite3_test_control(testctrl, -id, &val);
27810 if( rc2!=SQLITE_OK ) break;
27811 if( id>1 ) oputz(" ");
27812 oputf("%d: %d", id, val);
27813 id++;
27814 }
27815 if( id>1 ) oputz("\n");
27816 isOk = 3;
27817 }
27818 break;
27819 }
27820 #endif
@@ -27106,16 +27826,16 @@
27826 }
27827 break;
27828 }
27829 }
27830 if( isOk==0 && iCtrl>=0 ){
27831 oputf("Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
27832 rc = 1;
27833 }else if( isOk==1 ){
27834 oputf("%d\n", rc2);
27835 }else if( isOk==2 ){
27836 oputf("0x%08x\n", rc2);
27837 }
27838 }else
27839 #endif /* !defined(SQLITE_UNTESTABLE) */
27840
27841 if( c=='t' && n>4 && cli_strncmp(azArg[0], "timeout", n)==0 ){
@@ -27125,15 +27845,15 @@
27845
27846 if( c=='t' && n>=5 && cli_strncmp(azArg[0], "timer", n)==0 ){
27847 if( nArg==2 ){
27848 enableTimer = booleanValue(azArg[1]);
27849 if( enableTimer && !HAS_TIMER ){
27850 eputz("Error: timer not available on this system.\n");
27851 enableTimer = 0;
27852 }
27853 }else{
27854 eputz("Usage: .timer on|off\n");
27855 rc = 1;
27856 }
27857 }else
27858
27859 #ifndef SQLITE_OMIT_TRACE
@@ -27166,11 +27886,11 @@
27886 }
27887 else if( optionMatch(z, "close") ){
27888 mType |= SQLITE_TRACE_CLOSE;
27889 }
27890 else {
27891 eputf("Unknown option \"%s\" on \".trace\"\n", z);
27892 rc = 1;
27893 goto meta_command_exit;
27894 }
27895 }else{
27896 output_file_close(p->traceOut);
@@ -27190,11 +27910,11 @@
27910 if( c=='u' && cli_strncmp(azArg[0], "unmodule", n)==0 ){
27911 int ii;
27912 int lenOpt;
27913 char *zOpt;
27914 if( nArg<2 ){
27915 eputz("Usage: .unmodule [--allexcept] NAME ...\n");
27916 rc = 1;
27917 goto meta_command_exit;
27918 }
27919 open_db(p, 0);
27920 zOpt = azArg[1];
@@ -27212,100 +27932,100 @@
27932 #endif
27933
27934 #if SQLITE_USER_AUTHENTICATION
27935 if( c=='u' && cli_strncmp(azArg[0], "user", n)==0 ){
27936 if( nArg<2 ){
27937 eputz("Usage: .user SUBCOMMAND ...\n");
27938 rc = 1;
27939 goto meta_command_exit;
27940 }
27941 open_db(p, 0);
27942 if( cli_strcmp(azArg[1],"login")==0 ){
27943 if( nArg!=4 ){
27944 eputz("Usage: .user login USER PASSWORD\n");
27945 rc = 1;
27946 goto meta_command_exit;
27947 }
27948 rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
27949 strlen30(azArg[3]));
27950 if( rc ){
27951 eputf("Authentication failed for user %s\n", azArg[2]);
27952 rc = 1;
27953 }
27954 }else if( cli_strcmp(azArg[1],"add")==0 ){
27955 if( nArg!=5 ){
27956 eputz("Usage: .user add USER PASSWORD ISADMIN\n");
27957 rc = 1;
27958 goto meta_command_exit;
27959 }
27960 rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
27961 booleanValue(azArg[4]));
27962 if( rc ){
27963 eputf("User-Add failed: %d\n", rc);
27964 rc = 1;
27965 }
27966 }else if( cli_strcmp(azArg[1],"edit")==0 ){
27967 if( nArg!=5 ){
27968 eputz("Usage: .user edit USER PASSWORD ISADMIN\n");
27969 rc = 1;
27970 goto meta_command_exit;
27971 }
27972 rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
27973 booleanValue(azArg[4]));
27974 if( rc ){
27975 eputf("User-Edit failed: %d\n", rc);
27976 rc = 1;
27977 }
27978 }else if( cli_strcmp(azArg[1],"delete")==0 ){
27979 if( nArg!=3 ){
27980 eputz("Usage: .user delete USER\n");
27981 rc = 1;
27982 goto meta_command_exit;
27983 }
27984 rc = sqlite3_user_delete(p->db, azArg[2]);
27985 if( rc ){
27986 eputf("User-Delete failed: %d\n", rc);
27987 rc = 1;
27988 }
27989 }else{
27990 eputz("Usage: .user login|add|edit|delete ...\n");
27991 rc = 1;
27992 goto meta_command_exit;
27993 }
27994 }else
27995 #endif /* SQLITE_USER_AUTHENTICATION */
27996
27997 if( c=='v' && cli_strncmp(azArg[0], "version", n)==0 ){
27998 char *zPtrSz = sizeof(void*)==8 ? "64-bit" : "32-bit";
27999 oputf("SQLite %s %s\n" /*extra-version-info*/,
28000 sqlite3_libversion(), sqlite3_sourceid());
28001 #if SQLITE_HAVE_ZLIB
28002 oputf("zlib version %s\n", zlibVersion());
28003 #endif
28004 #define CTIMEOPT_VAL_(opt) #opt
28005 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
28006 #if defined(__clang__) && defined(__clang_major__)
28007 oputf("clang-" CTIMEOPT_VAL(__clang_major__) "."
28008 CTIMEOPT_VAL(__clang_minor__) "."
28009 CTIMEOPT_VAL(__clang_patchlevel__) " (%s)\n", zPtrSz);
28010 #elif defined(_MSC_VER)
28011 oputf("msvc-" CTIMEOPT_VAL(_MSC_VER) " (%s)\n", zPtrSz);
28012 #elif defined(__GNUC__) && defined(__VERSION__)
28013 oputf("gcc-" __VERSION__ " (%s)\n", zPtrSz);
28014 #endif
28015 }else
28016
28017 if( c=='v' && cli_strncmp(azArg[0], "vfsinfo", n)==0 ){
28018 const char *zDbName = nArg==2 ? azArg[1] : "main";
28019 sqlite3_vfs *pVfs = 0;
28020 if( p->db ){
28021 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
28022 if( pVfs ){
28023 oputf("vfs.zName = \"%s\"\n", pVfs->zName);
28024 oputf("vfs.iVersion = %d\n", pVfs->iVersion);
28025 oputf("vfs.szOsFile = %d\n", pVfs->szOsFile);
28026 oputf("vfs.mxPathname = %d\n", pVfs->mxPathname);
28027 }
28028 }
28029 }else
28030
28031 if( c=='v' && cli_strncmp(azArg[0], "vfslist", n)==0 ){
@@ -27313,17 +28033,17 @@
28033 sqlite3_vfs *pCurrent = 0;
28034 if( p->db ){
28035 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
28036 }
28037 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
28038 oputf("vfs.zName = \"%s\"%s\n", pVfs->zName,
28039 pVfs==pCurrent ? " <--- CURRENT" : "");
28040 oputf("vfs.iVersion = %d\n", pVfs->iVersion);
28041 oputf("vfs.szOsFile = %d\n", pVfs->szOsFile);
28042 oputf("vfs.mxPathname = %d\n", pVfs->mxPathname);
28043 if( pVfs->pNext ){
28044 oputz("-----------------------------------\n");
28045 }
28046 }
28047 }else
28048
28049 if( c=='v' && cli_strncmp(azArg[0], "vfsname", n)==0 ){
@@ -27330,11 +28050,11 @@
28050 const char *zDbName = nArg==2 ? azArg[1] : "main";
28051 char *zVfsName = 0;
28052 if( p->db ){
28053 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
28054 if( zVfsName ){
28055 oputf("%s\n", zVfsName);
28056 sqlite3_free(zVfsName);
28057 }
28058 }
28059 }else
28060
@@ -27354,12 +28074,12 @@
28074 p->colWidth[j-1] = (int)integerValue(azArg[j]);
28075 }
28076 }else
28077
28078 {
28079 eputf("Error: unknown command or invalid arguments: "
28080 " \"%s\". Enter \".help\" for help\n", azArg[0]);
28081 rc = 1;
28082 }
28083
28084 meta_command_exit:
28085 if( p->outCount ){
@@ -27545,26 +28265,26 @@
28265 sqlite3_snprintf(sizeof(zPrefix), zPrefix,
28266 "%s near line %d:", zErrorType, startline);
28267 }else{
28268 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:", zErrorType);
28269 }
28270 eputf("%s %s\n", zPrefix, zErrorTail);
28271 sqlite3_free(zErrMsg);
28272 zErrMsg = 0;
28273 return 1;
28274 }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
28275 char zLineBuf[2000];
28276 sqlite3_snprintf(sizeof(zLineBuf), zLineBuf,
28277 "changes: %lld total_changes: %lld",
28278 sqlite3_changes64(p->db), sqlite3_total_changes64(p->db));
28279 oputf("%s\n", zLineBuf);
28280 }
28281 return 0;
28282 }
28283
28284 static void echo_group_input(ShellState *p, const char *zDo){
28285 if( ShellHasFlag(p, SHFLG_Echo) ) oputf("%s\n", zDo);
28286 }
28287
28288 #ifdef SQLITE_SHELL_FIDDLE
28289 /*
28290 ** Alternate one_input_line() impl for wasm mode. This is not in the primary
@@ -27618,12 +28338,12 @@
28338 i64 startline = 0; /* Line number for start of current input */
28339 QuickScanState qss = QSS_Start; /* Accumulated line status (so far) */
28340
28341 if( p->inputNesting==MAX_INPUT_NESTING ){
28342 /* This will be more informative in a later version. */
28343 eputf("Input nesting limit (%d) reached at line %d."
28344 " Check recursion.\n", MAX_INPUT_NESTING, p->lineno);
28345 return 1;
28346 }
28347 ++p->inputNesting;
28348 p->lineno = 0;
28349 CONTINUE_PROMPT_RESET;
@@ -27630,11 +28350,11 @@
28350 while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
28351 fflush(p->out);
28352 zLine = one_input_line(p->in, zLine, nSql>0);
28353 if( zLine==0 ){
28354 /* End of input */
28355 if( p->in==0 && stdin_is_interactive ) oputz("\n");
28356 break;
28357 }
28358 if( seenInterrupt ){
28359 if( p->in!=0 ) break;
28360 seenInterrupt = 0;
@@ -27840,27 +28560,27 @@
28560 sqliterc = find_xdg_config();
28561 }
28562 if( sqliterc == NULL ){
28563 home_dir = find_home_dir(0);
28564 if( home_dir==0 ){
28565 eputz("-- warning: cannot find home directory;"
28566 " cannot read ~/.sqliterc\n");
28567 return;
28568 }
28569 zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
28570 shell_check_oom(zBuf);
28571 sqliterc = zBuf;
28572 }
28573 p->in = fopen(sqliterc,"rb");
28574 if( p->in ){
28575 if( stdin_is_interactive ){
28576 eputf("-- Loading resources from %s\n", sqliterc);
28577 }
28578 if( process_input(p) && bail_on_error ) exit(1);
28579 fclose(p->in);
28580 }else if( sqliterc_override!=0 ){
28581 eputf("cannot open: \"%s\"\n", sqliterc);
28582 if( bail_on_error ) exit(1);
28583 }
28584 p->in = inSaved;
28585 p->lineno = savedLineno;
28586 sqlite3_free(zBuf);
@@ -27906,13 +28626,10 @@
28626 " -mmap N default mmap size set to N\n"
28627 #ifdef SQLITE_ENABLE_MULTIPLEX
28628 " -multiplex enable the multiplexor VFS\n"
28629 #endif
28630 " -newline SEP set output row separator. Default: '\\n'\n"
 
 
 
28631 " -nofollow refuse to open symbolic links to database files\n"
28632 " -nonce STRING set the safe-mode escape nonce\n"
28633 " -nullvalue TEXT set text string for NULL values. Default ''\n"
28634 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
28635 " -pcachetrace trace all page cache operations\n"
@@ -27925,13 +28642,10 @@
28642 #endif
28643 " -stats print memory stats before each finalize\n"
28644 " -table set output mode to 'table'\n"
28645 " -tabs set output mode to 'tabs'\n"
28646 " -unsafe-testing allow unsafe commands and modes for testing\n"
 
 
 
28647 " -version show SQLite version\n"
28648 " -vfs NAME use NAME as the default VFS\n"
28649 #ifdef SQLITE_ENABLE_VFSTRACE
28650 " -vfstrace enable tracing of all VFS calls\n"
28651 #endif
@@ -27938,18 +28652,17 @@
28652 #ifdef SQLITE_HAVE_ZLIB
28653 " -zip open the file as a ZIP Archive\n"
28654 #endif
28655 ;
28656 static void usage(int showDetail){
28657 eputf("Usage: %s [OPTIONS] [FILENAME [SQL]]\n"
28658 "FILENAME is the name of an SQLite database. A new database is created\n"
28659 "if the file does not previously exist. Defaults to :memory:.\n", Argv0);
 
28660 if( showDetail ){
28661 eputf("OPTIONS include:\n%s", zOptions);
28662 }else{
28663 eputz("Use the -help option for additional information\n");
28664 }
28665 exit(1);
28666 }
28667
28668 /*
@@ -27956,12 +28669,12 @@
28669 ** Internal check: Verify that the SQLite is uninitialized. Print a
28670 ** error message if it is initialized.
28671 */
28672 static void verify_uninitialized(void){
28673 if( sqlite3_config(-1)==SQLITE_MISUSE ){
28674 sputz(stdout, "WARNING: attempt to configure SQLite after"
28675 " initialization.\n");
28676 }
28677 }
28678
28679 /*
28680 ** Initialize the state information in data
@@ -27986,46 +28699,45 @@
28699 }
28700
28701 /*
28702 ** Output text to the console in a font that attracts extra attention.
28703 */
28704 #if defined(_WIN32) || defined(WIN32)
28705 static void printBold(const char *zText){
28706 #if !SQLITE_OS_WINRT
28707 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
28708 CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
28709 GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
28710 SetConsoleTextAttribute(out,
28711 FOREGROUND_RED|FOREGROUND_INTENSITY
28712 );
28713 #endif
28714 oputz(zText);
28715 #if !SQLITE_OS_WINRT
28716 SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
28717 #endif
28718 }
28719 #else
28720 static void printBold(const char *zText){
28721 oputf("\033[1m%s\033[0m", zText);
28722 }
28723 #endif
28724
28725 /*
28726 ** Get the argument to an --option. Throw an error and die if no argument
28727 ** is available.
28728 */
28729 static char *cmdline_option_value(int argc, char **argv, int i){
28730 if( i==argc ){
28731 eputf("%s: Error: missing argument to %s\n", argv[0], argv[argc-1]);
 
28732 exit(1);
28733 }
28734 return argv[i];
28735 }
28736
28737 static void sayAbnormalExit(void){
28738 if( seenInterrupt ) eputz("Program interrupted.\n");
28739 }
28740
28741 #ifndef SQLITE_SHELL_IS_UTF8
28742 # if (defined(_WIN32) || defined(WIN32)) \
28743 && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
@@ -28051,10 +28763,11 @@
28763 char *zErrMsg = 0;
28764 #ifdef SQLITE_SHELL_FIDDLE
28765 # define data shellState
28766 #else
28767 ShellState data;
28768 StreamsAreConsole consStreams = SAC_NoConsole;
28769 #endif
28770 const char *zInitFile = 0;
28771 int i;
28772 int rc = 0;
28773 int warnInmemoryDb = 0;
@@ -28072,27 +28785,24 @@
28785 #ifdef SQLITE_SHELL_FIDDLE
28786 stdin_is_interactive = 0;
28787 stdout_is_console = 1;
28788 data.wasm.zDefaultDbName = "/fiddle.sqlite3";
28789 #else
28790 consStreams = consoleClassifySetup(stdin, stdout, stderr);
28791 stdin_is_interactive = (consStreams & SAC_InConsole)!=0;
28792 stdout_is_console = (consStreams & SAC_OutConsole)!=0;
28793 atexit(consoleRestore);
 
 
28794 #endif
28795 atexit(sayAbnormalExit);
28796 #ifdef SQLITE_DEBUG
28797 mem_main_enter = sqlite3_memory_used();
28798 #endif
28799 #if !defined(_WIN32_WCE)
28800 if( getenv("SQLITE_DEBUG_BREAK") ){
28801 if( isatty(0) && isatty(2) ){
28802 eputf("attach debugger to process %d and press any key to continue.\n",
28803 GETPID());
 
28804 fgetc(stdin);
28805 }else{
28806 #if defined(_WIN32) || defined(WIN32)
28807 #if SQLITE_OS_WINRT
28808 __debugbreak();
@@ -28108,18 +28818,18 @@
28818 /* Register a valid signal handler early, before much else is done. */
28819 #ifdef SIGINT
28820 signal(SIGINT, interrupt_handler);
28821 #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
28822 if( !SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE) ){
28823 eputz("No ^C handler.\n");
28824 }
28825 #endif
28826
28827 #if USE_SYSTEM_SQLITE+0!=1
28828 if( cli_strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
28829 eputf("SQLite header and source version mismatch\n%s\n%s\n",
28830 sqlite3_sourceid(), SQLITE_SOURCE_ID);
28831 exit(1);
28832 }
28833 #endif
28834 main_init(&data);
28835
@@ -28211,18 +28921,11 @@
28921 ** informational messages (like from process_sqliterc) before
28922 ** we do the actual processing of arguments later in a second pass.
28923 */
28924 stdin_is_interactive = 0;
28925 }else if( cli_strcmp(z,"-utf8")==0 ){
 
 
 
 
28926 }else if( cli_strcmp(z,"-no-utf8")==0 ){
 
 
 
28927 }else if( cli_strcmp(z,"-heap")==0 ){
28928 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
28929 const char *zSize;
28930 sqlite3_int64 szHeap;
28931
@@ -28353,30 +29056,21 @@
29056 if( zVfs ){
29057 sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
29058 if( pVfs ){
29059 sqlite3_vfs_register(pVfs, 1);
29060 }else{
29061 eputf("no such VFS: \"%s\"\n", zVfs);
29062 exit(1);
29063 }
29064 }
 
 
 
 
 
 
 
 
 
29065
29066 if( data.pAuxDb->zDbFilename==0 ){
29067 #ifndef SQLITE_OMIT_MEMORYDB
29068 data.pAuxDb->zDbFilename = ":memory:";
29069 warnInmemoryDb = argc==1;
29070 #else
29071 eputf("%s: Error: no database filename specified\n", Argv0);
29072 return 1;
29073 #endif
29074 }
29075 data.out = stdout;
29076 #ifndef SQLITE_SHELL_FIDDLE
@@ -28489,12 +29183,12 @@
29183 */
29184 ShellSetFlag(&data, SHFLG_Backslash);
29185 }else if( cli_strcmp(z,"-bail")==0 ){
29186 /* No-op. The bail_on_error flag should already be set. */
29187 }else if( cli_strcmp(z,"-version")==0 ){
29188 oputf("%s %s (%d-bit)\n", sqlite3_libversion(), sqlite3_sourceid(),
29189 8*(int)sizeof(char*));
29190 return 0;
29191 }else if( cli_strcmp(z,"-interactive")==0 ){
29192 /* already handled */
29193 }else if( cli_strcmp(z,"-batch")==0 ){
29194 /* already handled */
@@ -28546,22 +29240,22 @@
29240 if( rc && bail_on_error ) return rc==2 ? 0 : rc;
29241 }else{
29242 open_db(&data, 0);
29243 rc = shell_exec(&data, z, &zErrMsg);
29244 if( zErrMsg!=0 ){
29245 eputf("Error: %s\n", zErrMsg);
29246 if( bail_on_error ) return rc!=0 ? rc : 1;
29247 }else if( rc!=0 ){
29248 eputf("Error: unable to process SQL \"%s\"\n", z);
29249 if( bail_on_error ) return rc;
29250 }
29251 }
29252 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
29253 }else if( cli_strncmp(z, "-A", 2)==0 ){
29254 if( nCmd>0 ){
29255 eputf("Error: cannot mix regular SQL or dot-commands"
29256 " with \"%s\"\n", z);
29257 return 1;
29258 }
29259 open_db(&data, OPEN_DB_ZIPFILE);
29260 if( z[2] ){
29261 argv[i] = &z[2];
@@ -28575,12 +29269,12 @@
29269 }else if( cli_strcmp(z,"-safe")==0 ){
29270 data.bSafeMode = data.bSafeModePersist = 1;
29271 }else if( cli_strcmp(z,"-unsafe-testing")==0 ){
29272 /* Acted upon in first pass. */
29273 }else{
29274 eputf("%s: Error: unknown option: %s\n", Argv0, z);
29275 eputz("Use -help for a list of options.\n");
29276 return 1;
29277 }
29278 data.cMode = data.mode;
29279 }
29280
@@ -28600,13 +29294,13 @@
29294 open_db(&data, 0);
29295 echo_group_input(&data, azCmd[i]);
29296 rc = shell_exec(&data, azCmd[i], &zErrMsg);
29297 if( zErrMsg || rc ){
29298 if( zErrMsg!=0 ){
29299 eputf("Error: %s\n", zErrMsg);
29300 }else{
29301 eputf("Error: unable to process SQL: %s\n", azCmd[i]);
29302 }
29303 sqlite3_free(zErrMsg);
29304 free(azCmd);
29305 return rc!=0 ? rc : 1;
29306 }
@@ -28616,30 +29310,24 @@
29310 /* Run commands received from standard input
29311 */
29312 if( stdin_is_interactive ){
29313 char *zHome;
29314 char *zHistory;
 
29315 int nHistory;
29316 #if CIO_WIN_WC_XLATE
29317 # define SHELL_CIO_CHAR_SET (stdout_is_console? " (UTF-16 console I/O)" : "")
29318 #else
29319 # define SHELL_CIO_CHAR_SET ""
 
 
 
29320 #endif
29321 oputf("SQLite version %s %.19s%s\n" /*extra-version-info*/
29322 "Enter \".help\" for usage hints.\n",
29323 sqlite3_libversion(), sqlite3_sourceid(), SHELL_CIO_CHAR_SET);
 
 
29324 if( warnInmemoryDb ){
29325 oputz("Connected to a ");
29326 printBold("transient in-memory database");
29327 oputz(".\nUse \".open FILENAME\" to reopen on a"
29328 " persistent database.\n");
29329 }
29330 zHistory = getenv("SQLITE_HISTORY");
29331 if( zHistory ){
29332 zHistory = strdup(zHistory);
29333 }else if( (zHome = find_home_dir(0))!=0 ){
@@ -28695,12 +29383,12 @@
29383 /* Clear the global data structure so that valgrind will detect memory
29384 ** leaks */
29385 memset(&data, 0, sizeof(data));
29386 #ifdef SQLITE_DEBUG
29387 if( sqlite3_memory_used()>mem_main_enter ){
29388 eputf("Memory leaked: %u bytes\n",
29389 (unsigned int)(sqlite3_memory_used()-mem_main_enter));
29390 }
29391 #endif
29392 #endif /* !SQLITE_SHELL_FIDDLE */
29393 return rc;
29394 }
29395
+176 -79
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -1,8 +1,8 @@
11
/******************************************************************************
22
** This file is an amalgamation of many separate C source files from SQLite
3
-** version 3.44.0. By combining all the individual C code files into this
3
+** version 3.44.1. By combining all the individual C code files into this
44
** single large file, the entire code can be compiled as a single translation
55
** unit. This allows many compilers to do optimizations that would not be
66
** possible if the files were compiled separately. Performance improvements
77
** of 5% or more are commonly seen when SQLite is compiled as a single
88
** translation unit.
@@ -16,11 +16,11 @@
1616
** if you want a wrapper to interface SQLite with your choice of programming
1717
** language. The code for the "sqlite3" command-line shell is also in a
1818
** separate file. This file contains only code for the core SQLite library.
1919
**
2020
** The content in this amalgamation comes from Fossil check-in
21
-** 17129ba1ff7f0daf37100ee82d507aef7827.
21
+** d295f48e8f367b066b881780c98bdf980a1d.
2222
*/
2323
#define SQLITE_CORE 1
2424
#define SQLITE_AMALGAMATION 1
2525
#ifndef SQLITE_PRIVATE
2626
# define SQLITE_PRIVATE static
@@ -457,13 +457,13 @@
457457
**
458458
** See also: [sqlite3_libversion()],
459459
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
460460
** [sqlite_version()] and [sqlite_source_id()].
461461
*/
462
-#define SQLITE_VERSION "3.44.0"
463
-#define SQLITE_VERSION_NUMBER 3044000
464
-#define SQLITE_SOURCE_ID "2023-11-01 11:23:50 17129ba1ff7f0daf37100ee82d507aef7827cf38de1866e2633096ae6ad81301"
462
+#define SQLITE_VERSION "3.44.1"
463
+#define SQLITE_VERSION_NUMBER 3044001
464
+#define SQLITE_SOURCE_ID "2023-11-22 14:18:12 d295f48e8f367b066b881780c98bdf980a1d550397d5ba0b0e49842c95b3e8b4"
465465
466466
/*
467467
** CAPI3REF: Run-Time Library Version Numbers
468468
** KEYWORDS: sqlite3_version sqlite3_sourceid
469469
**
@@ -5884,24 +5884,39 @@
58845884
** function has been carefully audited and found to be free of potentially
58855885
** security-adverse side-effects and information-leaks.
58865886
** </dd>
58875887
**
58885888
** [[SQLITE_SUBTYPE]] <dt>SQLITE_SUBTYPE</dt><dd>
5889
-** The SQLITE_SUBTYPE flag indicates to SQLite that a function may call
5889
+** The SQLITE_SUBTYPE flag indicates to SQLite that a function might call
58905890
** [sqlite3_value_subtype()] to inspect the sub-types of its arguments.
5891
-** Specifying this flag makes no difference for scalar or aggregate user
5892
-** functions. However, if it is not specified for a user-defined window
5893
-** function, then any sub-types belonging to arguments passed to the window
5894
-** function may be discarded before the window function is called (i.e.
5895
-** sqlite3_value_subtype() will always return 0).
5891
+** This flag instructs SQLite to omit some corner-case optimizations that
5892
+** might disrupt the operation of the [sqlite3_value_subtype()] function,
5893
+** causing it to return zero rather than the correct subtype().
5894
+** SQL functions that invokes [sqlite3_value_subtype()] should have this
5895
+** property. If the SQLITE_SUBTYPE property is omitted, then the return
5896
+** value from [sqlite3_value_subtype()] might sometimes be zero even though
5897
+** a non-zero subtype was specified by the function argument expression.
5898
+**
5899
+** [[SQLITE_RESULT_SUBTYPE]] <dt>SQLITE_RESULT_SUBTYPE</dt><dd>
5900
+** The SQLITE_RESULT_SUBTYPE flag indicates to SQLite that a function might call
5901
+** [sqlite3_result_subtype()] to cause a sub-type to be associated with its
5902
+** result.
5903
+** Every function that invokes [sqlite3_result_subtype()] should have this
5904
+** property. If it does not, then the call to [sqlite3_result_subtype()]
5905
+** might become a no-op if the function is used as term in an
5906
+** [expression index]. On the other hand, SQL functions that never invoke
5907
+** [sqlite3_result_subtype()] should avoid setting this property, as the
5908
+** purpose of this property is to disable certain optimizations that are
5909
+** incompatible with subtypes.
58965910
** </dd>
58975911
** </dl>
58985912
*/
58995913
#define SQLITE_DETERMINISTIC 0x000000800
59005914
#define SQLITE_DIRECTONLY 0x000080000
59015915
#define SQLITE_SUBTYPE 0x000100000
59025916
#define SQLITE_INNOCUOUS 0x000200000
5917
+#define SQLITE_RESULT_SUBTYPE 0x001000000
59035918
59045919
/*
59055920
** CAPI3REF: Deprecated Functions
59065921
** DEPRECATED
59075922
**
@@ -6094,10 +6109,16 @@
60946109
** The sqlite3_value_subtype(V) function returns the subtype for
60956110
** an [application-defined SQL function] argument V. The subtype
60966111
** information can be used to pass a limited amount of context from
60976112
** one SQL function to another. Use the [sqlite3_result_subtype()]
60986113
** routine to set the subtype for the return value of an SQL function.
6114
+**
6115
+** Every [application-defined SQL function] that invoke this interface
6116
+** should include the [SQLITE_SUBTYPE] property in the text
6117
+** encoding argument when the function is [sqlite3_create_function|registered].
6118
+** If the [SQLITE_SUBTYPE] property is omitted, then sqlite3_value_subtype()
6119
+** might return zero instead of the upstream subtype in some corner cases.
60996120
*/
61006121
SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value*);
61016122
61026123
/*
61036124
** CAPI3REF: Copy And Free SQL Values
@@ -6224,18 +6245,26 @@
62246245
** <li> ^(when [sqlite3_reset()] or [sqlite3_finalize()] is called for the
62256246
** SQL statement)^, or
62266247
** <li> ^(when sqlite3_set_auxdata() is invoked again on the same
62276248
** parameter)^, or
62286249
** <li> ^(during the original sqlite3_set_auxdata() call when a memory
6229
-** allocation error occurs.)^ </ul>
6250
+** allocation error occurs.)^
6251
+** <li> ^(during the original sqlite3_set_auxdata() call if the function
6252
+** is evaluated during query planning instead of during query execution,
6253
+** as sometimes happens with [SQLITE_ENABLE_STAT4].)^ </ul>
62306254
**
6231
-** Note the last bullet in particular. The destructor X in
6255
+** Note the last two bullets in particular. The destructor X in
62326256
** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the
62336257
** sqlite3_set_auxdata() interface even returns. Hence sqlite3_set_auxdata()
62346258
** should be called near the end of the function implementation and the
62356259
** function implementation should not make any use of P after
6236
-** sqlite3_set_auxdata() has been called.
6260
+** sqlite3_set_auxdata() has been called. Furthermore, a call to
6261
+** sqlite3_get_auxdata() that occurs immediately after a corresponding call
6262
+** to sqlite3_set_auxdata() might still return NULL if an out-of-memory
6263
+** condition occurred during the sqlite3_set_auxdata() call or if the
6264
+** function is being evaluated during query planning rather than during
6265
+** query execution.
62376266
**
62386267
** ^(In practice, auxiliary data is preserved between function calls for
62396268
** function parameters that are compile-time constants, including literal
62406269
** values and [parameters] and expressions composed from the same.)^
62416270
**
@@ -6505,10 +6534,24 @@
65056534
** [sqlite3_context] C to be the value T. Only the lower 8 bits
65066535
** of the subtype T are preserved in current versions of SQLite;
65076536
** higher order bits are discarded.
65086537
** The number of subtype bytes preserved by SQLite might increase
65096538
** in future releases of SQLite.
6539
+**
6540
+** Every [application-defined SQL function] that invokes this interface
6541
+** should include the [SQLITE_RESULT_SUBTYPE] property in its
6542
+** text encoding argument when the SQL function is
6543
+** [sqlite3_create_function|registered]. If the [SQLITE_RESULT_SUBTYPE]
6544
+** property is omitted from the function that invokes sqlite3_result_subtype(),
6545
+** then in some cases the sqlite3_result_subtype() might fail to set
6546
+** the result subtype.
6547
+**
6548
+** If SQLite is compiled with -DSQLITE_STRICT_SUBTYPE=1, then any
6549
+** SQL function that invokes the sqlite3_result_subtype() interface
6550
+** and that does not have the SQLITE_RESULT_SUBTYPE property will raise
6551
+** an error. Future versions of SQLite might enable -DSQLITE_STRICT_SUBTYPE=1
6552
+** by default.
65106553
*/
65116554
SQLITE_API void sqlite3_result_subtype(sqlite3_context*,unsigned int);
65126555
65136556
/*
65146557
** CAPI3REF: Define New Collating Sequences
@@ -17809,18 +17852,19 @@
1780917852
#define SQLITE_FUNC_CONSTANT 0x0800 /* Constant inputs give a constant output */
1781017853
#define SQLITE_FUNC_MINMAX 0x1000 /* True for min() and max() aggregates */
1781117854
#define SQLITE_FUNC_SLOCHNG 0x2000 /* "Slow Change". Value constant during a
1781217855
** single query - might change over time */
1781317856
#define SQLITE_FUNC_TEST 0x4000 /* Built-in testing functions */
17814
-/* 0x8000 -- available for reuse */
17857
+#define SQLITE_FUNC_RUNONLY 0x8000 /* Cannot be used by valueFromFunction */
1781517858
#define SQLITE_FUNC_WINDOW 0x00010000 /* Built-in window-only function */
1781617859
#define SQLITE_FUNC_INTERNAL 0x00040000 /* For use by NestedParse() only */
1781717860
#define SQLITE_FUNC_DIRECT 0x00080000 /* Not for use in TRIGGERs or VIEWs */
17818
-#define SQLITE_FUNC_SUBTYPE 0x00100000 /* Result likely to have sub-type */
17861
+/* SQLITE_SUBTYPE 0x00100000 // Consumer of subtypes */
1781917862
#define SQLITE_FUNC_UNSAFE 0x00200000 /* Function has side effects */
1782017863
#define SQLITE_FUNC_INLINE 0x00400000 /* Functions implemented in-line */
1782117864
#define SQLITE_FUNC_BUILTIN 0x00800000 /* This is a built-in function */
17865
+/* SQLITE_RESULT_SUBTYPE 0x01000000 // Generator of subtypes */
1782217866
#define SQLITE_FUNC_ANYORDER 0x08000000 /* count/min/max aggregate */
1782317867
1782417868
/* Identifier numbers for each in-line function */
1782517869
#define INLINEFUNC_coalesce 0
1782617870
#define INLINEFUNC_implies_nonnull_row 1
@@ -17908,13 +17952,14 @@
1790817952
{nArg, SQLITE_FUNC_BUILTIN|SQLITE_UTF8|SQLITE_DIRECTONLY|SQLITE_FUNC_UNSAFE, \
1790917953
SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
1791017954
#define MFUNCTION(zName, nArg, xPtr, xFunc) \
1791117955
{nArg, SQLITE_FUNC_BUILTIN|SQLITE_FUNC_CONSTANT|SQLITE_UTF8, \
1791217956
xPtr, 0, xFunc, 0, 0, 0, #zName, {0} }
17913
-#define JFUNCTION(zName, nArg, iArg, xFunc) \
17914
- {nArg, SQLITE_FUNC_BUILTIN|SQLITE_DETERMINISTIC|\
17915
- SQLITE_FUNC_CONSTANT|SQLITE_UTF8, \
17957
+#define JFUNCTION(zName, nArg, bUseCache, bWS, bRS, iArg, xFunc) \
17958
+ {nArg, SQLITE_FUNC_BUILTIN|SQLITE_DETERMINISTIC|SQLITE_FUNC_CONSTANT|\
17959
+ SQLITE_UTF8|((bUseCache)*SQLITE_FUNC_RUNONLY)|\
17960
+ ((bRS)*SQLITE_SUBTYPE)|((bWS)*SQLITE_RESULT_SUBTYPE), \
1791617961
SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
1791717962
#define INLINE_FUNC(zName, nArg, iArg, mFlags) \
1791817963
{nArg, SQLITE_FUNC_BUILTIN|\
1791917964
SQLITE_UTF8|SQLITE_FUNC_INLINE|SQLITE_FUNC_CONSTANT|(mFlags), \
1792017965
SQLITE_INT_TO_PTR(iArg), 0, noopFunc, 0, 0, 0, #zName, {0} }
@@ -29451,11 +29496,11 @@
2945129496
SQLITE_PRIVATE void sqlite3MemoryBarrier(void){
2945229497
#if defined(SQLITE_MEMORY_BARRIER)
2945329498
SQLITE_MEMORY_BARRIER;
2945429499
#elif defined(__GNUC__)
2945529500
__sync_synchronize();
29456
-#elif MSVC_VERSION>=1300
29501
+#elif MSVC_VERSION>=1400
2945729502
_ReadWriteBarrier();
2945829503
#elif defined(MemoryBarrier)
2945929504
MemoryBarrier();
2946029505
#endif
2946129506
}
@@ -61445,14 +61490,17 @@
6144561490
** of the corresponding WAL or Journal name as passed into
6144661491
** xOpen.
6144761492
*/
6144861493
SQLITE_API sqlite3_file *sqlite3_database_file_object(const char *zName){
6144961494
Pager *pPager;
61495
+ const char *p;
6145061496
while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){
6145161497
zName--;
6145261498
}
61453
- pPager = *(Pager**)(zName - 4 - sizeof(Pager*));
61499
+ p = zName - 4 - sizeof(Pager*);
61500
+ assert( EIGHT_BYTE_ALIGNMENT(p) );
61501
+ pPager = *(Pager**)p;
6145461502
return pPager->fd;
6145561503
}
6145661504
6145761505
6145861506
/*
@@ -83409,11 +83457,11 @@
8340983457
#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
8341083458
if( pFunc==0 ) return SQLITE_OK;
8341183459
#endif
8341283460
assert( pFunc );
8341383461
if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0
83414
- || (pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
83462
+ || (pFunc->funcFlags & (SQLITE_FUNC_NEEDCOLL|SQLITE_FUNC_RUNONLY))!=0
8341583463
){
8341683464
return SQLITE_OK;
8341783465
}
8341883466
8341983467
if( pList ){
@@ -89950,10 +89998,22 @@
8995089998
SQLITE_API void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){
8995189999
Mem *pOut;
8995290000
#ifdef SQLITE_ENABLE_API_ARMOR
8995390001
if( pCtx==0 ) return;
8995490002
#endif
90003
+#if defined(SQLITE_STRICT_SUBTYPE) && SQLITE_STRICT_SUBTYPE+0!=0
90004
+ if( pCtx->pFunc!=0
90005
+ && (pCtx->pFunc->funcFlags & SQLITE_RESULT_SUBTYPE)==0
90006
+ ){
90007
+ char zErr[200];
90008
+ sqlite3_snprintf(sizeof(zErr), zErr,
90009
+ "misuse of sqlite3_result_subtype() by %s()",
90010
+ pCtx->pFunc->zName);
90011
+ sqlite3_result_error(pCtx, zErr, -1);
90012
+ return;
90013
+ }
90014
+#endif /* SQLITE_STRICT_SUBTYPE */
8995590015
pOut = pCtx->pOut;
8995690016
assert( sqlite3_mutex_held(pOut->db->mutex) );
8995790017
pOut->eSubtype = eSubtype & 0xff;
8995890018
pOut->flags |= MEM_Subtype;
8995990019
}
@@ -100319,11 +100379,11 @@
100319100379
sqlite3VdbeMemSetNull(pOut); /* Innocent until proven guilty */
100320100380
assert( pOp->p4type==P4_TABLE );
100321100381
pTab = pOp->p4.pTab;
100322100382
assert( pTab!=0 );
100323100383
assert( IsVirtual(pTab) );
100324
- assert( pTab->u.vtab.p!=0 );
100384
+ if( pTab->u.vtab.p==0 ) break;
100325100385
pVtab = pTab->u.vtab.p->pVtab;
100326100386
assert( pVtab!=0 );
100327100387
pModule = pVtab->pModule;
100328100388
assert( pModule!=0 );
100329100389
assert( pModule->iVersion>=4 );
@@ -113915,12 +113975,12 @@
113915113975
** Like sqlite3ExprCompare() except COLLATE operators at the top-level
113916113976
** are ignored.
113917113977
*/
113918113978
SQLITE_PRIVATE int sqlite3ExprCompareSkip(Expr *pA,Expr *pB, int iTab){
113919113979
return sqlite3ExprCompare(0,
113920
- sqlite3ExprSkipCollateAndLikely(pA),
113921
- sqlite3ExprSkipCollateAndLikely(pB),
113980
+ sqlite3ExprSkipCollate(pA),
113981
+ sqlite3ExprSkipCollate(pB),
113922113982
iTab);
113923113983
}
113924113984
113925113985
/*
113926113986
** Return non-zero if Expr p can only be true if pNN is not NULL.
@@ -147603,14 +147663,15 @@
147603147663
Parse *pParse;
147604147664
int i;
147605147665
SrcList *pTabList;
147606147666
SrcItem *pFrom;
147607147667
147608
- assert( p->selFlags & SF_Resolved );
147609147668
if( p->selFlags & SF_HasTypeInfo ) return;
147610147669
p->selFlags |= SF_HasTypeInfo;
147611147670
pParse = pWalker->pParse;
147671
+ testcase( (p->selFlags & SF_Resolved)==0 );
147672
+ assert( (p->selFlags & SF_Resolved) || IN_RENAME_OBJECT );
147612147673
pTabList = p->pSrc;
147613147674
for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
147614147675
Table *pTab = pFrom->pTab;
147615147676
assert( pTab!=0 );
147616147677
if( (pTab->tabFlags & TF_Ephemeral)!=0 ){
@@ -148628,10 +148689,11 @@
148628148689
pItem->fg.jointype &= ~JT_LEFT;
148629148690
}else{
148630148691
TREETRACE(0x1000,pParse,p,
148631148692
("LEFT-JOIN simplifies to JOIN on term %d\n",i));
148632148693
pItem->fg.jointype &= ~(JT_LEFT|JT_OUTER);
148694
+ unsetJoinExpr(p->pWhere, pItem->iCursor, 0);
148633148695
}
148634148696
}
148635148697
if( pItem->fg.jointype & JT_LTORJ ){
148636148698
for(j=i+1; j<pTabList->nSrc; j++){
148637148699
SrcItem *pI2 = &pTabList->a[j];
@@ -148642,21 +148704,19 @@
148642148704
pI2->fg.jointype &= ~JT_RIGHT;
148643148705
}else{
148644148706
TREETRACE(0x1000,pParse,p,
148645148707
("RIGHT-JOIN simplifies to JOIN on term %d\n",j));
148646148708
pI2->fg.jointype &= ~(JT_RIGHT|JT_OUTER);
148709
+ unsetJoinExpr(p->pWhere, pI2->iCursor, 1);
148647148710
}
148648148711
}
148649148712
}
148650
- for(j=pTabList->nSrc-1; j>=i; j--){
148713
+ for(j=pTabList->nSrc-1; j>=0; j--){
148651148714
pTabList->a[j].fg.jointype &= ~JT_LTORJ;
148652148715
if( pTabList->a[j].fg.jointype & JT_RIGHT ) break;
148653148716
}
148654148717
}
148655
- assert( pItem->iCursor>=0 );
148656
- unsetJoinExpr(p->pWhere, pItem->iCursor,
148657
- pTabList->a[0].fg.jointype & JT_LTORJ);
148658148718
}
148659148719
148660148720
/* No further action if this term of the FROM clause is not a subquery */
148661148721
if( pSub==0 ) continue;
148662148722
@@ -166056,10 +166116,24 @@
166056166116
bMaybeNullRow = 0;
166057166117
}else{
166058166118
continue;
166059166119
}
166060166120
if( sqlite3ExprIsConstant(pExpr) ) continue;
166121
+ if( pExpr->op==TK_FUNCTION ){
166122
+ /* Functions that might set a subtype should not be replaced by the
166123
+ ** value taken from an expression index since the index omits the
166124
+ ** subtype. https://sqlite.org/forum/forumpost/68d284c86b082c3e */
166125
+ int n;
166126
+ FuncDef *pDef;
166127
+ sqlite3 *db = pParse->db;
166128
+ assert( ExprUseXList(pExpr) );
166129
+ n = pExpr->x.pList ? pExpr->x.pList->nExpr : 0;
166130
+ pDef = sqlite3FindFunction(db, pExpr->u.zToken, n, ENC(db), 0);
166131
+ if( pDef==0 || (pDef->funcFlags & SQLITE_RESULT_SUBTYPE)!=0 ){
166132
+ continue;
166133
+ }
166134
+ }
166061166135
p = sqlite3DbMallocRaw(pParse->db, sizeof(IndexedExpr));
166062166136
if( p==0 ) break;
166063166137
p->pIENext = pParse->pIdxEpr;
166064166138
#ifdef WHERETRACE_ENABLED
166065166139
if( sqlite3WhereTrace & 0x200 ){
@@ -168238,11 +168312,11 @@
168238168312
for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
168239168313
ExprList *pArgs;
168240168314
assert( ExprUseXList(pWin->pOwner) );
168241168315
assert( pWin->pWFunc!=0 );
168242168316
pArgs = pWin->pOwner->x.pList;
168243
- if( pWin->pWFunc->funcFlags & SQLITE_FUNC_SUBTYPE ){
168317
+ if( pWin->pWFunc->funcFlags & SQLITE_SUBTYPE ){
168244168318
selectWindowRewriteEList(pParse, pMWin, pSrc, pArgs, pTab, &pSublist);
168245168319
pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
168246168320
pWin->bExprArgs = 1;
168247168321
}else{
168248168322
pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
@@ -179412,11 +179486,11 @@
179412179486
}
179413179487
179414179488
assert( SQLITE_FUNC_CONSTANT==SQLITE_DETERMINISTIC );
179415179489
assert( SQLITE_FUNC_DIRECT==SQLITE_DIRECTONLY );
179416179490
extraFlags = enc & (SQLITE_DETERMINISTIC|SQLITE_DIRECTONLY|
179417
- SQLITE_SUBTYPE|SQLITE_INNOCUOUS);
179491
+ SQLITE_SUBTYPE|SQLITE_INNOCUOUS|SQLITE_RESULT_SUBTYPE);
179418179492
enc &= (SQLITE_FUNC_ENCMASK|SQLITE_ANY);
179419179493
179420179494
/* The SQLITE_INNOCUOUS flag is the same bit as SQLITE_FUNC_UNSAFE. But
179421179495
** the meaning is inverted. So flip the bit. */
179422179496
assert( SQLITE_FUNC_UNSAFE==SQLITE_INNOCUOUS );
@@ -202993,16 +203067,22 @@
202993203067
u32 i;
202994203068
jsonAppendChar(p, '"');
202995203069
zIn++;
202996203070
N -= 2;
202997203071
while( N>0 ){
202998
- for(i=0; i<N && zIn[i]!='\\'; i++){}
203072
+ for(i=0; i<N && zIn[i]!='\\' && zIn[i]!='"'; i++){}
202999203073
if( i>0 ){
203000203074
jsonAppendRawNZ(p, zIn, i);
203001203075
zIn += i;
203002203076
N -= i;
203003203077
if( N==0 ) break;
203078
+ }
203079
+ if( zIn[0]=='"' ){
203080
+ jsonAppendRawNZ(p, "\\\"", 2);
203081
+ zIn++;
203082
+ N--;
203083
+ continue;
203004203084
}
203005203085
assert( zIn[0]=='\\' );
203006203086
switch( (u8)zIn[1] ){
203007203087
case '\'':
203008203088
jsonAppendChar(p, '\'');
@@ -203394,11 +203474,12 @@
203394203474
*/
203395203475
static void jsonReturnJson(
203396203476
JsonParse *pParse, /* The complete JSON */
203397203477
JsonNode *pNode, /* Node to return */
203398203478
sqlite3_context *pCtx, /* Return value for this function */
203399
- int bGenerateAlt /* Also store the rendered text in zAlt */
203479
+ int bGenerateAlt, /* Also store the rendered text in zAlt */
203480
+ int omitSubtype /* Do not call sqlite3_result_subtype() */
203400203481
){
203401203482
JsonString s;
203402203483
if( pParse->oom ){
203403203484
sqlite3_result_error_nomem(pCtx);
203404203485
return;
@@ -203409,11 +203490,11 @@
203409203490
if( bGenerateAlt && pParse->zAlt==0 && jsonForceRCStr(&s) ){
203410203491
pParse->zAlt = sqlite3RCStrRef(s.zBuf);
203411203492
pParse->nAlt = s.nUsed;
203412203493
}
203413203494
jsonResult(&s);
203414
- sqlite3_result_subtype(pCtx, JSON_SUBTYPE);
203495
+ if( !omitSubtype ) sqlite3_result_subtype(pCtx, JSON_SUBTYPE);
203415203496
}
203416203497
}
203417203498
203418203499
/*
203419203500
** Translate a single byte of Hex into an integer.
@@ -203450,11 +203531,12 @@
203450203531
** Make the JsonNode the return value of the function.
203451203532
*/
203452203533
static void jsonReturn(
203453203534
JsonParse *pParse, /* Complete JSON parse tree */
203454203535
JsonNode *pNode, /* Node to return */
203455
- sqlite3_context *pCtx /* Return value for this function */
203536
+ sqlite3_context *pCtx, /* Return value for this function */
203537
+ int omitSubtype /* Do not call sqlite3_result_subtype() */
203456203538
){
203457203539
switch( pNode->eType ){
203458203540
default: {
203459203541
assert( pNode->eType==JSON_NULL );
203460203542
sqlite3_result_null(pCtx);
@@ -203596,11 +203678,11 @@
203596203678
}
203597203679
break;
203598203680
}
203599203681
case JSON_ARRAY:
203600203682
case JSON_OBJECT: {
203601
- jsonReturnJson(pParse, pNode, pCtx, 0);
203683
+ jsonReturnJson(pParse, pNode, pCtx, 0, omitSubtype);
203602203684
break;
203603203685
}
203604203686
}
203605203687
}
203606203688
@@ -204948,11 +205030,11 @@
204948205030
printf("hasMod = %u\n", p->hasMod);
204949205031
printf("nJPRef = %u\n", p->nJPRef);
204950205032
printf("iSubst = %u\n", p->iSubst);
204951205033
printf("iHold = %u\n", p->iHold);
204952205034
jsonDebugPrintNodeEntries(p->aNode, p->nNode);
204953
- jsonReturnJson(p, p->aNode, ctx, 1);
205035
+ jsonReturnJson(p, p->aNode, ctx, 1, 0);
204954205036
}
204955205037
204956205038
/*
204957205039
** The json_test1(JSON) function return true (1) if the input is JSON
204958205040
** text generated by another json function. It returns (0) if the input
@@ -205134,19 +205216,18 @@
205134205216
}else{
205135205217
pNode = jsonLookup(p, zPath, 0, ctx);
205136205218
}
205137205219
if( pNode ){
205138205220
if( flags & JSON_JSON ){
205139
- jsonReturnJson(p, pNode, ctx, 0);
205221
+ jsonReturnJson(p, pNode, ctx, 0, 0);
205140205222
}else{
205141
- jsonReturn(p, pNode, ctx);
205142
- sqlite3_result_subtype(ctx, 0);
205223
+ jsonReturn(p, pNode, ctx, 1);
205143205224
}
205144205225
}
205145205226
}else{
205146205227
pNode = jsonLookup(p, zPath, 0, ctx);
205147
- if( p->nErr==0 && pNode ) jsonReturn(p, pNode, ctx);
205228
+ if( p->nErr==0 && pNode ) jsonReturn(p, pNode, ctx, 0);
205148205229
}
205149205230
}else{
205150205231
/* Two or more PATH arguments results in a JSON array with each
205151205232
** element of the array being the value selected by one of the PATHs */
205152205233
int i;
@@ -205268,11 +205349,11 @@
205268205349
pResult = jsonMergePatch(pX, 0, pY->aNode);
205269205350
assert( pResult!=0 || pX->oom );
205270205351
if( pResult && pX->oom==0 ){
205271205352
jsonDebugPrintParse(pX);
205272205353
jsonDebugPrintNode(pResult);
205273
- jsonReturnJson(pX, pResult, ctx, 0);
205354
+ jsonReturnJson(pX, pResult, ctx, 0, 0);
205274205355
}else{
205275205356
sqlite3_result_error_nomem(ctx);
205276205357
}
205277205358
}
205278205359
@@ -205347,11 +205428,11 @@
205347205428
pParse->hasMod = 1;
205348205429
pParse->useMod = 1;
205349205430
}
205350205431
}
205351205432
if( (pParse->aNode[0].jnFlags & JNODE_REMOVE)==0 ){
205352
- jsonReturnJson(pParse, pParse->aNode, ctx, 1);
205433
+ jsonReturnJson(pParse, pParse->aNode, ctx, 1, 0);
205353205434
}
205354205435
remove_done:
205355205436
jsonDebugPrintParse(p);
205356205437
}
205357205438
@@ -205476,11 +205557,11 @@
205476205557
if( pParse->nErr ) goto replace_err;
205477205558
if( pNode ){
205478205559
jsonReplaceNode(ctx, pParse, (u32)(pNode - pParse->aNode), argv[i+1]);
205479205560
}
205480205561
}
205481
- jsonReturnJson(pParse, pParse->aNode, ctx, 1);
205562
+ jsonReturnJson(pParse, pParse->aNode, ctx, 1, 0);
205482205563
replace_err:
205483205564
jsonDebugPrintParse(pParse);
205484205565
jsonParseFree(pParse);
205485205566
}
205486205567
@@ -205530,11 +205611,11 @@
205530205611
}else if( pNode && (bApnd || bIsSet) ){
205531205612
jsonReplaceNode(ctx, pParse, (u32)(pNode - pParse->aNode), argv[i+1]);
205532205613
}
205533205614
}
205534205615
jsonDebugPrintParse(pParse);
205535
- jsonReturnJson(pParse, pParse->aNode, ctx, 1);
205616
+ jsonReturnJson(pParse, pParse->aNode, ctx, 1, 0);
205536205617
jsonSetDone:
205537205618
jsonParseFree(pParse);
205538205619
}
205539205620
205540205621
/*
@@ -206045,11 +206126,11 @@
206045206126
JsonNode *pThis = &p->sParse.aNode[p->i];
206046206127
switch( i ){
206047206128
case JEACH_KEY: {
206048206129
if( p->i==0 ) break;
206049206130
if( p->eType==JSON_OBJECT ){
206050
- jsonReturn(&p->sParse, pThis, ctx);
206131
+ jsonReturn(&p->sParse, pThis, ctx, 0);
206051206132
}else if( p->eType==JSON_ARRAY ){
206052206133
u32 iKey;
206053206134
if( p->bRecursive ){
206054206135
if( p->iRowid==0 ) break;
206055206136
assert( p->sParse.aNode[p->sParse.aUp[p->i]].eU==3 );
@@ -206061,11 +206142,11 @@
206061206142
}
206062206143
break;
206063206144
}
206064206145
case JEACH_VALUE: {
206065206146
if( pThis->jnFlags & JNODE_LABEL ) pThis++;
206066
- jsonReturn(&p->sParse, pThis, ctx);
206147
+ jsonReturn(&p->sParse, pThis, ctx, 0);
206067206148
break;
206068206149
}
206069206150
case JEACH_TYPE: {
206070206151
if( pThis->jnFlags & JNODE_LABEL ) pThis++;
206071206152
sqlite3_result_text(ctx, jsonType[pThis->eType], -1, SQLITE_STATIC);
@@ -206072,11 +206153,11 @@
206072206153
break;
206073206154
}
206074206155
case JEACH_ATOM: {
206075206156
if( pThis->jnFlags & JNODE_LABEL ) pThis++;
206076206157
if( pThis->eType>=JSON_ARRAY ) break;
206077
- jsonReturn(&p->sParse, pThis, ctx);
206158
+ jsonReturn(&p->sParse, pThis, ctx, 0);
206078206159
break;
206079206160
}
206080206161
case JEACH_ID: {
206081206162
sqlite3_result_int64(ctx,
206082206163
(sqlite3_int64)p->i + ((pThis->jnFlags & JNODE_LABEL)!=0));
@@ -206365,38 +206446,47 @@
206365206446
** Register JSON functions.
206366206447
*/
206367206448
SQLITE_PRIVATE void sqlite3RegisterJsonFunctions(void){
206368206449
#ifndef SQLITE_OMIT_JSON
206369206450
static FuncDef aJsonFunc[] = {
206370
- JFUNCTION(json, 1, 0, jsonRemoveFunc),
206371
- JFUNCTION(json_array, -1, 0, jsonArrayFunc),
206372
- JFUNCTION(json_array_length, 1, 0, jsonArrayLengthFunc),
206373
- JFUNCTION(json_array_length, 2, 0, jsonArrayLengthFunc),
206374
- JFUNCTION(json_error_position,1, 0, jsonErrorFunc),
206375
- JFUNCTION(json_extract, -1, 0, jsonExtractFunc),
206376
- JFUNCTION(->, 2, JSON_JSON, jsonExtractFunc),
206377
- JFUNCTION(->>, 2, JSON_SQL, jsonExtractFunc),
206378
- JFUNCTION(json_insert, -1, 0, jsonSetFunc),
206379
- JFUNCTION(json_object, -1, 0, jsonObjectFunc),
206380
- JFUNCTION(json_patch, 2, 0, jsonPatchFunc),
206381
- JFUNCTION(json_quote, 1, 0, jsonQuoteFunc),
206382
- JFUNCTION(json_remove, -1, 0, jsonRemoveFunc),
206383
- JFUNCTION(json_replace, -1, 0, jsonReplaceFunc),
206384
- JFUNCTION(json_set, -1, JSON_ISSET, jsonSetFunc),
206385
- JFUNCTION(json_type, 1, 0, jsonTypeFunc),
206386
- JFUNCTION(json_type, 2, 0, jsonTypeFunc),
206387
- JFUNCTION(json_valid, 1, 0, jsonValidFunc),
206388
-#if SQLITE_DEBUG
206389
- JFUNCTION(json_parse, 1, 0, jsonParseFunc),
206390
- JFUNCTION(json_test1, 1, 0, jsonTest1Func),
206451
+ /* calls sqlite3_result_subtype() */
206452
+ /* | */
206453
+ /* Uses cache ______ | __ calls sqlite3_value_subtype() */
206454
+ /* | | | */
206455
+ /* Num args _________ | | | ___ Flags */
206456
+ /* | | | | | */
206457
+ /* | | | | | */
206458
+ JFUNCTION(json, 1, 1, 1, 0, 0, jsonRemoveFunc),
206459
+ JFUNCTION(json_array, -1, 0, 1, 1, 0, jsonArrayFunc),
206460
+ JFUNCTION(json_array_length, 1, 1, 0, 0, 0, jsonArrayLengthFunc),
206461
+ JFUNCTION(json_array_length, 2, 1, 0, 0, 0, jsonArrayLengthFunc),
206462
+ JFUNCTION(json_error_position,1, 1, 0, 0, 0, jsonErrorFunc),
206463
+ JFUNCTION(json_extract, -1, 1, 1, 0, 0, jsonExtractFunc),
206464
+ JFUNCTION(->, 2, 1, 1, 0, JSON_JSON, jsonExtractFunc),
206465
+ JFUNCTION(->>, 2, 1, 0, 0, JSON_SQL, jsonExtractFunc),
206466
+ JFUNCTION(json_insert, -1, 1, 1, 1, 0, jsonSetFunc),
206467
+ JFUNCTION(json_object, -1, 0, 1, 1, 0, jsonObjectFunc),
206468
+ JFUNCTION(json_patch, 2, 1, 1, 0, 0, jsonPatchFunc),
206469
+ JFUNCTION(json_quote, 1, 0, 1, 1, 0, jsonQuoteFunc),
206470
+ JFUNCTION(json_remove, -1, 1, 1, 0, 0, jsonRemoveFunc),
206471
+ JFUNCTION(json_replace, -1, 1, 1, 1, 0, jsonReplaceFunc),
206472
+ JFUNCTION(json_set, -1, 1, 1, 1, JSON_ISSET, jsonSetFunc),
206473
+ JFUNCTION(json_type, 1, 1, 0, 0, 0, jsonTypeFunc),
206474
+ JFUNCTION(json_type, 2, 1, 0, 0, 0, jsonTypeFunc),
206475
+ JFUNCTION(json_valid, 1, 1, 0, 0, 0, jsonValidFunc),
206476
+#ifdef SQLITE_DEBUG
206477
+ JFUNCTION(json_parse, 1, 1, 1, 0, 0, jsonParseFunc),
206478
+ JFUNCTION(json_test1, 1, 1, 0, 1, 0, jsonTest1Func),
206391206479
#endif
206392206480
WAGGREGATE(json_group_array, 1, 0, 0,
206393206481
jsonArrayStep, jsonArrayFinal, jsonArrayValue, jsonGroupInverse,
206394
- SQLITE_SUBTYPE|SQLITE_UTF8|SQLITE_DETERMINISTIC),
206482
+ SQLITE_SUBTYPE|SQLITE_RESULT_SUBTYPE|SQLITE_UTF8|
206483
+ SQLITE_DETERMINISTIC),
206395206484
WAGGREGATE(json_group_object, 2, 0, 0,
206396206485
jsonObjectStep, jsonObjectFinal, jsonObjectValue, jsonGroupInverse,
206397
- SQLITE_SUBTYPE|SQLITE_UTF8|SQLITE_DETERMINISTIC)
206486
+ SQLITE_SUBTYPE|SQLITE_RESULT_SUBTYPE|SQLITE_UTF8|
206487
+ SQLITE_DETERMINISTIC)
206398206488
};
206399206489
sqlite3InsertBuiltinFuncs(aJsonFunc, ArraySize(aJsonFunc));
206400206490
#endif
206401206491
}
206402206492
@@ -236129,14 +236219,12 @@
236129236219
236130236220
return pRet;
236131236221
}
236132236222
236133236223
/*
236134
-** Extract all tokens from hash table iHash and link them into a list
236135
-** in sorted order. The hash table is cleared before returning. It is
236136
-** the responsibility of the caller to free the elements of the returned
236137
-** list.
236224
+** Link all tokens from hash table iHash into a list in sorted order. The
236225
+** tokens are not removed from the hash table.
236138236226
*/
236139236227
static int fts5HashEntrySort(
236140236228
Fts5Hash *pHash,
236141236229
const char *pTerm, int nTerm, /* Query prefix, if any */
236142236230
Fts5HashEntry **ppSorted
@@ -238998,10 +239086,18 @@
238998239086
pLeaf = fts5IdxMalloc(p, sizeof(Fts5Data));
238999239087
if( pLeaf ){
239000239088
pLeaf->p = (u8*)pList;
239001239089
}
239002239090
}
239091
+
239092
+ /* The call to sqlite3Fts5HashScanInit() causes the hash table to
239093
+ ** fill the size field of all existing position lists. This means they
239094
+ ** can no longer be appended to. Since the only scenario in which they
239095
+ ** can be appended to is if the previous operation on this table was
239096
+ ** a DELETE, by clearing the Fts5Index.bDelete flag we can avoid this
239097
+ ** possibility altogether. */
239098
+ p->bDelete = 0;
239003239099
}else{
239004239100
p->rc = sqlite3Fts5HashQuery(p->pHash, sizeof(Fts5Data),
239005239101
(const char*)pTerm, nTerm, (void**)&pLeaf, &nList
239006239102
);
239007239103
if( pLeaf ){
@@ -240675,11 +240771,11 @@
240675240771
){
240676240772
Fts5PageWriter *pPage = &pWriter->writer;
240677240773
const u8 *a = aData;
240678240774
int n = nData;
240679240775
240680
- assert( p->pConfig->pgsz>0 );
240776
+ assert( p->pConfig->pgsz>0 || p->rc!=SQLITE_OK );
240681240777
while( p->rc==SQLITE_OK
240682240778
&& (pPage->buf.n + pPage->pgidx.n + n)>=p->pConfig->pgsz
240683240779
){
240684240780
int nReq = p->pConfig->pgsz - pPage->buf.n - pPage->pgidx.n;
240685240781
int nCopy = 0;
@@ -241935,12 +242031,13 @@
241935242031
Fts5Structure *pStruct;
241936242032
Fts5Structure *pNew = 0;
241937242033
241938242034
assert( p->rc==SQLITE_OK );
241939242035
fts5IndexFlush(p);
241940
- assert( p->nContentlessDelete==0 );
242036
+ assert( p->rc!=SQLITE_OK || p->nContentlessDelete==0 );
241941242037
pStruct = fts5StructureRead(p);
242038
+ assert( p->rc!=SQLITE_OK || pStruct!=0 );
241942242039
fts5StructureInvalidate(p);
241943242040
241944242041
if( pStruct ){
241945242042
pNew = fts5IndexOptimizeStruct(p, pStruct);
241946242043
}
@@ -247513,11 +247610,11 @@
247513247610
int nArg, /* Number of args */
247514247611
sqlite3_value **apUnused /* Function arguments */
247515247612
){
247516247613
assert( nArg==0 );
247517247614
UNUSED_PARAM2(nArg, apUnused);
247518
- sqlite3_result_text(pCtx, "fts5: 2023-11-01 11:23:50 17129ba1ff7f0daf37100ee82d507aef7827cf38de1866e2633096ae6ad81301", -1, SQLITE_TRANSIENT);
247615
+ sqlite3_result_text(pCtx, "fts5: 2023-11-22 14:18:12 d295f48e8f367b066b881780c98bdf980a1d550397d5ba0b0e49842c95b3e8b4", -1, SQLITE_TRANSIENT);
247519247616
}
247520247617
247521247618
/*
247522247619
** Return true if zName is the extension on one of the shadow tables used
247523247620
** by this module.
247524247621
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -1,8 +1,8 @@
1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 ** version 3.44.0. By combining all the individual C code files into this
4 ** single large file, the entire code can be compiled as a single translation
5 ** unit. This allows many compilers to do optimizations that would not be
6 ** possible if the files were compiled separately. Performance improvements
7 ** of 5% or more are commonly seen when SQLite is compiled as a single
8 ** translation unit.
@@ -16,11 +16,11 @@
16 ** if you want a wrapper to interface SQLite with your choice of programming
17 ** language. The code for the "sqlite3" command-line shell is also in a
18 ** separate file. This file contains only code for the core SQLite library.
19 **
20 ** The content in this amalgamation comes from Fossil check-in
21 ** 17129ba1ff7f0daf37100ee82d507aef7827.
22 */
23 #define SQLITE_CORE 1
24 #define SQLITE_AMALGAMATION 1
25 #ifndef SQLITE_PRIVATE
26 # define SQLITE_PRIVATE static
@@ -457,13 +457,13 @@
457 **
458 ** See also: [sqlite3_libversion()],
459 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
460 ** [sqlite_version()] and [sqlite_source_id()].
461 */
462 #define SQLITE_VERSION "3.44.0"
463 #define SQLITE_VERSION_NUMBER 3044000
464 #define SQLITE_SOURCE_ID "2023-11-01 11:23:50 17129ba1ff7f0daf37100ee82d507aef7827cf38de1866e2633096ae6ad81301"
465
466 /*
467 ** CAPI3REF: Run-Time Library Version Numbers
468 ** KEYWORDS: sqlite3_version sqlite3_sourceid
469 **
@@ -5884,24 +5884,39 @@
5884 ** function has been carefully audited and found to be free of potentially
5885 ** security-adverse side-effects and information-leaks.
5886 ** </dd>
5887 **
5888 ** [[SQLITE_SUBTYPE]] <dt>SQLITE_SUBTYPE</dt><dd>
5889 ** The SQLITE_SUBTYPE flag indicates to SQLite that a function may call
5890 ** [sqlite3_value_subtype()] to inspect the sub-types of its arguments.
5891 ** Specifying this flag makes no difference for scalar or aggregate user
5892 ** functions. However, if it is not specified for a user-defined window
5893 ** function, then any sub-types belonging to arguments passed to the window
5894 ** function may be discarded before the window function is called (i.e.
5895 ** sqlite3_value_subtype() will always return 0).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5896 ** </dd>
5897 ** </dl>
5898 */
5899 #define SQLITE_DETERMINISTIC 0x000000800
5900 #define SQLITE_DIRECTONLY 0x000080000
5901 #define SQLITE_SUBTYPE 0x000100000
5902 #define SQLITE_INNOCUOUS 0x000200000
 
5903
5904 /*
5905 ** CAPI3REF: Deprecated Functions
5906 ** DEPRECATED
5907 **
@@ -6094,10 +6109,16 @@
6094 ** The sqlite3_value_subtype(V) function returns the subtype for
6095 ** an [application-defined SQL function] argument V. The subtype
6096 ** information can be used to pass a limited amount of context from
6097 ** one SQL function to another. Use the [sqlite3_result_subtype()]
6098 ** routine to set the subtype for the return value of an SQL function.
 
 
 
 
 
 
6099 */
6100 SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value*);
6101
6102 /*
6103 ** CAPI3REF: Copy And Free SQL Values
@@ -6224,18 +6245,26 @@
6224 ** <li> ^(when [sqlite3_reset()] or [sqlite3_finalize()] is called for the
6225 ** SQL statement)^, or
6226 ** <li> ^(when sqlite3_set_auxdata() is invoked again on the same
6227 ** parameter)^, or
6228 ** <li> ^(during the original sqlite3_set_auxdata() call when a memory
6229 ** allocation error occurs.)^ </ul>
 
 
 
6230 **
6231 ** Note the last bullet in particular. The destructor X in
6232 ** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the
6233 ** sqlite3_set_auxdata() interface even returns. Hence sqlite3_set_auxdata()
6234 ** should be called near the end of the function implementation and the
6235 ** function implementation should not make any use of P after
6236 ** sqlite3_set_auxdata() has been called.
 
 
 
 
 
6237 **
6238 ** ^(In practice, auxiliary data is preserved between function calls for
6239 ** function parameters that are compile-time constants, including literal
6240 ** values and [parameters] and expressions composed from the same.)^
6241 **
@@ -6505,10 +6534,24 @@
6505 ** [sqlite3_context] C to be the value T. Only the lower 8 bits
6506 ** of the subtype T are preserved in current versions of SQLite;
6507 ** higher order bits are discarded.
6508 ** The number of subtype bytes preserved by SQLite might increase
6509 ** in future releases of SQLite.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6510 */
6511 SQLITE_API void sqlite3_result_subtype(sqlite3_context*,unsigned int);
6512
6513 /*
6514 ** CAPI3REF: Define New Collating Sequences
@@ -17809,18 +17852,19 @@
17809 #define SQLITE_FUNC_CONSTANT 0x0800 /* Constant inputs give a constant output */
17810 #define SQLITE_FUNC_MINMAX 0x1000 /* True for min() and max() aggregates */
17811 #define SQLITE_FUNC_SLOCHNG 0x2000 /* "Slow Change". Value constant during a
17812 ** single query - might change over time */
17813 #define SQLITE_FUNC_TEST 0x4000 /* Built-in testing functions */
17814 /* 0x8000 -- available for reuse */
17815 #define SQLITE_FUNC_WINDOW 0x00010000 /* Built-in window-only function */
17816 #define SQLITE_FUNC_INTERNAL 0x00040000 /* For use by NestedParse() only */
17817 #define SQLITE_FUNC_DIRECT 0x00080000 /* Not for use in TRIGGERs or VIEWs */
17818 #define SQLITE_FUNC_SUBTYPE 0x00100000 /* Result likely to have sub-type */
17819 #define SQLITE_FUNC_UNSAFE 0x00200000 /* Function has side effects */
17820 #define SQLITE_FUNC_INLINE 0x00400000 /* Functions implemented in-line */
17821 #define SQLITE_FUNC_BUILTIN 0x00800000 /* This is a built-in function */
 
17822 #define SQLITE_FUNC_ANYORDER 0x08000000 /* count/min/max aggregate */
17823
17824 /* Identifier numbers for each in-line function */
17825 #define INLINEFUNC_coalesce 0
17826 #define INLINEFUNC_implies_nonnull_row 1
@@ -17908,13 +17952,14 @@
17908 {nArg, SQLITE_FUNC_BUILTIN|SQLITE_UTF8|SQLITE_DIRECTONLY|SQLITE_FUNC_UNSAFE, \
17909 SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
17910 #define MFUNCTION(zName, nArg, xPtr, xFunc) \
17911 {nArg, SQLITE_FUNC_BUILTIN|SQLITE_FUNC_CONSTANT|SQLITE_UTF8, \
17912 xPtr, 0, xFunc, 0, 0, 0, #zName, {0} }
17913 #define JFUNCTION(zName, nArg, iArg, xFunc) \
17914 {nArg, SQLITE_FUNC_BUILTIN|SQLITE_DETERMINISTIC|\
17915 SQLITE_FUNC_CONSTANT|SQLITE_UTF8, \
 
17916 SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
17917 #define INLINE_FUNC(zName, nArg, iArg, mFlags) \
17918 {nArg, SQLITE_FUNC_BUILTIN|\
17919 SQLITE_UTF8|SQLITE_FUNC_INLINE|SQLITE_FUNC_CONSTANT|(mFlags), \
17920 SQLITE_INT_TO_PTR(iArg), 0, noopFunc, 0, 0, 0, #zName, {0} }
@@ -29451,11 +29496,11 @@
29451 SQLITE_PRIVATE void sqlite3MemoryBarrier(void){
29452 #if defined(SQLITE_MEMORY_BARRIER)
29453 SQLITE_MEMORY_BARRIER;
29454 #elif defined(__GNUC__)
29455 __sync_synchronize();
29456 #elif MSVC_VERSION>=1300
29457 _ReadWriteBarrier();
29458 #elif defined(MemoryBarrier)
29459 MemoryBarrier();
29460 #endif
29461 }
@@ -61445,14 +61490,17 @@
61445 ** of the corresponding WAL or Journal name as passed into
61446 ** xOpen.
61447 */
61448 SQLITE_API sqlite3_file *sqlite3_database_file_object(const char *zName){
61449 Pager *pPager;
 
61450 while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){
61451 zName--;
61452 }
61453 pPager = *(Pager**)(zName - 4 - sizeof(Pager*));
 
 
61454 return pPager->fd;
61455 }
61456
61457
61458 /*
@@ -83409,11 +83457,11 @@
83409 #ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
83410 if( pFunc==0 ) return SQLITE_OK;
83411 #endif
83412 assert( pFunc );
83413 if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0
83414 || (pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
83415 ){
83416 return SQLITE_OK;
83417 }
83418
83419 if( pList ){
@@ -89950,10 +89998,22 @@
89950 SQLITE_API void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){
89951 Mem *pOut;
89952 #ifdef SQLITE_ENABLE_API_ARMOR
89953 if( pCtx==0 ) return;
89954 #endif
 
 
 
 
 
 
 
 
 
 
 
 
89955 pOut = pCtx->pOut;
89956 assert( sqlite3_mutex_held(pOut->db->mutex) );
89957 pOut->eSubtype = eSubtype & 0xff;
89958 pOut->flags |= MEM_Subtype;
89959 }
@@ -100319,11 +100379,11 @@
100319 sqlite3VdbeMemSetNull(pOut); /* Innocent until proven guilty */
100320 assert( pOp->p4type==P4_TABLE );
100321 pTab = pOp->p4.pTab;
100322 assert( pTab!=0 );
100323 assert( IsVirtual(pTab) );
100324 assert( pTab->u.vtab.p!=0 );
100325 pVtab = pTab->u.vtab.p->pVtab;
100326 assert( pVtab!=0 );
100327 pModule = pVtab->pModule;
100328 assert( pModule!=0 );
100329 assert( pModule->iVersion>=4 );
@@ -113915,12 +113975,12 @@
113915 ** Like sqlite3ExprCompare() except COLLATE operators at the top-level
113916 ** are ignored.
113917 */
113918 SQLITE_PRIVATE int sqlite3ExprCompareSkip(Expr *pA,Expr *pB, int iTab){
113919 return sqlite3ExprCompare(0,
113920 sqlite3ExprSkipCollateAndLikely(pA),
113921 sqlite3ExprSkipCollateAndLikely(pB),
113922 iTab);
113923 }
113924
113925 /*
113926 ** Return non-zero if Expr p can only be true if pNN is not NULL.
@@ -147603,14 +147663,15 @@
147603 Parse *pParse;
147604 int i;
147605 SrcList *pTabList;
147606 SrcItem *pFrom;
147607
147608 assert( p->selFlags & SF_Resolved );
147609 if( p->selFlags & SF_HasTypeInfo ) return;
147610 p->selFlags |= SF_HasTypeInfo;
147611 pParse = pWalker->pParse;
 
 
147612 pTabList = p->pSrc;
147613 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
147614 Table *pTab = pFrom->pTab;
147615 assert( pTab!=0 );
147616 if( (pTab->tabFlags & TF_Ephemeral)!=0 ){
@@ -148628,10 +148689,11 @@
148628 pItem->fg.jointype &= ~JT_LEFT;
148629 }else{
148630 TREETRACE(0x1000,pParse,p,
148631 ("LEFT-JOIN simplifies to JOIN on term %d\n",i));
148632 pItem->fg.jointype &= ~(JT_LEFT|JT_OUTER);
 
148633 }
148634 }
148635 if( pItem->fg.jointype & JT_LTORJ ){
148636 for(j=i+1; j<pTabList->nSrc; j++){
148637 SrcItem *pI2 = &pTabList->a[j];
@@ -148642,21 +148704,19 @@
148642 pI2->fg.jointype &= ~JT_RIGHT;
148643 }else{
148644 TREETRACE(0x1000,pParse,p,
148645 ("RIGHT-JOIN simplifies to JOIN on term %d\n",j));
148646 pI2->fg.jointype &= ~(JT_RIGHT|JT_OUTER);
 
148647 }
148648 }
148649 }
148650 for(j=pTabList->nSrc-1; j>=i; j--){
148651 pTabList->a[j].fg.jointype &= ~JT_LTORJ;
148652 if( pTabList->a[j].fg.jointype & JT_RIGHT ) break;
148653 }
148654 }
148655 assert( pItem->iCursor>=0 );
148656 unsetJoinExpr(p->pWhere, pItem->iCursor,
148657 pTabList->a[0].fg.jointype & JT_LTORJ);
148658 }
148659
148660 /* No further action if this term of the FROM clause is not a subquery */
148661 if( pSub==0 ) continue;
148662
@@ -166056,10 +166116,24 @@
166056 bMaybeNullRow = 0;
166057 }else{
166058 continue;
166059 }
166060 if( sqlite3ExprIsConstant(pExpr) ) continue;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166061 p = sqlite3DbMallocRaw(pParse->db, sizeof(IndexedExpr));
166062 if( p==0 ) break;
166063 p->pIENext = pParse->pIdxEpr;
166064 #ifdef WHERETRACE_ENABLED
166065 if( sqlite3WhereTrace & 0x200 ){
@@ -168238,11 +168312,11 @@
168238 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
168239 ExprList *pArgs;
168240 assert( ExprUseXList(pWin->pOwner) );
168241 assert( pWin->pWFunc!=0 );
168242 pArgs = pWin->pOwner->x.pList;
168243 if( pWin->pWFunc->funcFlags & SQLITE_FUNC_SUBTYPE ){
168244 selectWindowRewriteEList(pParse, pMWin, pSrc, pArgs, pTab, &pSublist);
168245 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
168246 pWin->bExprArgs = 1;
168247 }else{
168248 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
@@ -179412,11 +179486,11 @@
179412 }
179413
179414 assert( SQLITE_FUNC_CONSTANT==SQLITE_DETERMINISTIC );
179415 assert( SQLITE_FUNC_DIRECT==SQLITE_DIRECTONLY );
179416 extraFlags = enc & (SQLITE_DETERMINISTIC|SQLITE_DIRECTONLY|
179417 SQLITE_SUBTYPE|SQLITE_INNOCUOUS);
179418 enc &= (SQLITE_FUNC_ENCMASK|SQLITE_ANY);
179419
179420 /* The SQLITE_INNOCUOUS flag is the same bit as SQLITE_FUNC_UNSAFE. But
179421 ** the meaning is inverted. So flip the bit. */
179422 assert( SQLITE_FUNC_UNSAFE==SQLITE_INNOCUOUS );
@@ -202993,16 +203067,22 @@
202993 u32 i;
202994 jsonAppendChar(p, '"');
202995 zIn++;
202996 N -= 2;
202997 while( N>0 ){
202998 for(i=0; i<N && zIn[i]!='\\'; i++){}
202999 if( i>0 ){
203000 jsonAppendRawNZ(p, zIn, i);
203001 zIn += i;
203002 N -= i;
203003 if( N==0 ) break;
 
 
 
 
 
 
203004 }
203005 assert( zIn[0]=='\\' );
203006 switch( (u8)zIn[1] ){
203007 case '\'':
203008 jsonAppendChar(p, '\'');
@@ -203394,11 +203474,12 @@
203394 */
203395 static void jsonReturnJson(
203396 JsonParse *pParse, /* The complete JSON */
203397 JsonNode *pNode, /* Node to return */
203398 sqlite3_context *pCtx, /* Return value for this function */
203399 int bGenerateAlt /* Also store the rendered text in zAlt */
 
203400 ){
203401 JsonString s;
203402 if( pParse->oom ){
203403 sqlite3_result_error_nomem(pCtx);
203404 return;
@@ -203409,11 +203490,11 @@
203409 if( bGenerateAlt && pParse->zAlt==0 && jsonForceRCStr(&s) ){
203410 pParse->zAlt = sqlite3RCStrRef(s.zBuf);
203411 pParse->nAlt = s.nUsed;
203412 }
203413 jsonResult(&s);
203414 sqlite3_result_subtype(pCtx, JSON_SUBTYPE);
203415 }
203416 }
203417
203418 /*
203419 ** Translate a single byte of Hex into an integer.
@@ -203450,11 +203531,12 @@
203450 ** Make the JsonNode the return value of the function.
203451 */
203452 static void jsonReturn(
203453 JsonParse *pParse, /* Complete JSON parse tree */
203454 JsonNode *pNode, /* Node to return */
203455 sqlite3_context *pCtx /* Return value for this function */
 
203456 ){
203457 switch( pNode->eType ){
203458 default: {
203459 assert( pNode->eType==JSON_NULL );
203460 sqlite3_result_null(pCtx);
@@ -203596,11 +203678,11 @@
203596 }
203597 break;
203598 }
203599 case JSON_ARRAY:
203600 case JSON_OBJECT: {
203601 jsonReturnJson(pParse, pNode, pCtx, 0);
203602 break;
203603 }
203604 }
203605 }
203606
@@ -204948,11 +205030,11 @@
204948 printf("hasMod = %u\n", p->hasMod);
204949 printf("nJPRef = %u\n", p->nJPRef);
204950 printf("iSubst = %u\n", p->iSubst);
204951 printf("iHold = %u\n", p->iHold);
204952 jsonDebugPrintNodeEntries(p->aNode, p->nNode);
204953 jsonReturnJson(p, p->aNode, ctx, 1);
204954 }
204955
204956 /*
204957 ** The json_test1(JSON) function return true (1) if the input is JSON
204958 ** text generated by another json function. It returns (0) if the input
@@ -205134,19 +205216,18 @@
205134 }else{
205135 pNode = jsonLookup(p, zPath, 0, ctx);
205136 }
205137 if( pNode ){
205138 if( flags & JSON_JSON ){
205139 jsonReturnJson(p, pNode, ctx, 0);
205140 }else{
205141 jsonReturn(p, pNode, ctx);
205142 sqlite3_result_subtype(ctx, 0);
205143 }
205144 }
205145 }else{
205146 pNode = jsonLookup(p, zPath, 0, ctx);
205147 if( p->nErr==0 && pNode ) jsonReturn(p, pNode, ctx);
205148 }
205149 }else{
205150 /* Two or more PATH arguments results in a JSON array with each
205151 ** element of the array being the value selected by one of the PATHs */
205152 int i;
@@ -205268,11 +205349,11 @@
205268 pResult = jsonMergePatch(pX, 0, pY->aNode);
205269 assert( pResult!=0 || pX->oom );
205270 if( pResult && pX->oom==0 ){
205271 jsonDebugPrintParse(pX);
205272 jsonDebugPrintNode(pResult);
205273 jsonReturnJson(pX, pResult, ctx, 0);
205274 }else{
205275 sqlite3_result_error_nomem(ctx);
205276 }
205277 }
205278
@@ -205347,11 +205428,11 @@
205347 pParse->hasMod = 1;
205348 pParse->useMod = 1;
205349 }
205350 }
205351 if( (pParse->aNode[0].jnFlags & JNODE_REMOVE)==0 ){
205352 jsonReturnJson(pParse, pParse->aNode, ctx, 1);
205353 }
205354 remove_done:
205355 jsonDebugPrintParse(p);
205356 }
205357
@@ -205476,11 +205557,11 @@
205476 if( pParse->nErr ) goto replace_err;
205477 if( pNode ){
205478 jsonReplaceNode(ctx, pParse, (u32)(pNode - pParse->aNode), argv[i+1]);
205479 }
205480 }
205481 jsonReturnJson(pParse, pParse->aNode, ctx, 1);
205482 replace_err:
205483 jsonDebugPrintParse(pParse);
205484 jsonParseFree(pParse);
205485 }
205486
@@ -205530,11 +205611,11 @@
205530 }else if( pNode && (bApnd || bIsSet) ){
205531 jsonReplaceNode(ctx, pParse, (u32)(pNode - pParse->aNode), argv[i+1]);
205532 }
205533 }
205534 jsonDebugPrintParse(pParse);
205535 jsonReturnJson(pParse, pParse->aNode, ctx, 1);
205536 jsonSetDone:
205537 jsonParseFree(pParse);
205538 }
205539
205540 /*
@@ -206045,11 +206126,11 @@
206045 JsonNode *pThis = &p->sParse.aNode[p->i];
206046 switch( i ){
206047 case JEACH_KEY: {
206048 if( p->i==0 ) break;
206049 if( p->eType==JSON_OBJECT ){
206050 jsonReturn(&p->sParse, pThis, ctx);
206051 }else if( p->eType==JSON_ARRAY ){
206052 u32 iKey;
206053 if( p->bRecursive ){
206054 if( p->iRowid==0 ) break;
206055 assert( p->sParse.aNode[p->sParse.aUp[p->i]].eU==3 );
@@ -206061,11 +206142,11 @@
206061 }
206062 break;
206063 }
206064 case JEACH_VALUE: {
206065 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
206066 jsonReturn(&p->sParse, pThis, ctx);
206067 break;
206068 }
206069 case JEACH_TYPE: {
206070 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
206071 sqlite3_result_text(ctx, jsonType[pThis->eType], -1, SQLITE_STATIC);
@@ -206072,11 +206153,11 @@
206072 break;
206073 }
206074 case JEACH_ATOM: {
206075 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
206076 if( pThis->eType>=JSON_ARRAY ) break;
206077 jsonReturn(&p->sParse, pThis, ctx);
206078 break;
206079 }
206080 case JEACH_ID: {
206081 sqlite3_result_int64(ctx,
206082 (sqlite3_int64)p->i + ((pThis->jnFlags & JNODE_LABEL)!=0));
@@ -206365,38 +206446,47 @@
206365 ** Register JSON functions.
206366 */
206367 SQLITE_PRIVATE void sqlite3RegisterJsonFunctions(void){
206368 #ifndef SQLITE_OMIT_JSON
206369 static FuncDef aJsonFunc[] = {
206370 JFUNCTION(json, 1, 0, jsonRemoveFunc),
206371 JFUNCTION(json_array, -1, 0, jsonArrayFunc),
206372 JFUNCTION(json_array_length, 1, 0, jsonArrayLengthFunc),
206373 JFUNCTION(json_array_length, 2, 0, jsonArrayLengthFunc),
206374 JFUNCTION(json_error_position,1, 0, jsonErrorFunc),
206375 JFUNCTION(json_extract, -1, 0, jsonExtractFunc),
206376 JFUNCTION(->, 2, JSON_JSON, jsonExtractFunc),
206377 JFUNCTION(->>, 2, JSON_SQL, jsonExtractFunc),
206378 JFUNCTION(json_insert, -1, 0, jsonSetFunc),
206379 JFUNCTION(json_object, -1, 0, jsonObjectFunc),
206380 JFUNCTION(json_patch, 2, 0, jsonPatchFunc),
206381 JFUNCTION(json_quote, 1, 0, jsonQuoteFunc),
206382 JFUNCTION(json_remove, -1, 0, jsonRemoveFunc),
206383 JFUNCTION(json_replace, -1, 0, jsonReplaceFunc),
206384 JFUNCTION(json_set, -1, JSON_ISSET, jsonSetFunc),
206385 JFUNCTION(json_type, 1, 0, jsonTypeFunc),
206386 JFUNCTION(json_type, 2, 0, jsonTypeFunc),
206387 JFUNCTION(json_valid, 1, 0, jsonValidFunc),
206388 #if SQLITE_DEBUG
206389 JFUNCTION(json_parse, 1, 0, jsonParseFunc),
206390 JFUNCTION(json_test1, 1, 0, jsonTest1Func),
 
 
 
 
 
 
 
206391 #endif
206392 WAGGREGATE(json_group_array, 1, 0, 0,
206393 jsonArrayStep, jsonArrayFinal, jsonArrayValue, jsonGroupInverse,
206394 SQLITE_SUBTYPE|SQLITE_UTF8|SQLITE_DETERMINISTIC),
 
206395 WAGGREGATE(json_group_object, 2, 0, 0,
206396 jsonObjectStep, jsonObjectFinal, jsonObjectValue, jsonGroupInverse,
206397 SQLITE_SUBTYPE|SQLITE_UTF8|SQLITE_DETERMINISTIC)
 
206398 };
206399 sqlite3InsertBuiltinFuncs(aJsonFunc, ArraySize(aJsonFunc));
206400 #endif
206401 }
206402
@@ -236129,14 +236219,12 @@
236129
236130 return pRet;
236131 }
236132
236133 /*
236134 ** Extract all tokens from hash table iHash and link them into a list
236135 ** in sorted order. The hash table is cleared before returning. It is
236136 ** the responsibility of the caller to free the elements of the returned
236137 ** list.
236138 */
236139 static int fts5HashEntrySort(
236140 Fts5Hash *pHash,
236141 const char *pTerm, int nTerm, /* Query prefix, if any */
236142 Fts5HashEntry **ppSorted
@@ -238998,10 +239086,18 @@
238998 pLeaf = fts5IdxMalloc(p, sizeof(Fts5Data));
238999 if( pLeaf ){
239000 pLeaf->p = (u8*)pList;
239001 }
239002 }
 
 
 
 
 
 
 
 
239003 }else{
239004 p->rc = sqlite3Fts5HashQuery(p->pHash, sizeof(Fts5Data),
239005 (const char*)pTerm, nTerm, (void**)&pLeaf, &nList
239006 );
239007 if( pLeaf ){
@@ -240675,11 +240771,11 @@
240675 ){
240676 Fts5PageWriter *pPage = &pWriter->writer;
240677 const u8 *a = aData;
240678 int n = nData;
240679
240680 assert( p->pConfig->pgsz>0 );
240681 while( p->rc==SQLITE_OK
240682 && (pPage->buf.n + pPage->pgidx.n + n)>=p->pConfig->pgsz
240683 ){
240684 int nReq = p->pConfig->pgsz - pPage->buf.n - pPage->pgidx.n;
240685 int nCopy = 0;
@@ -241935,12 +242031,13 @@
241935 Fts5Structure *pStruct;
241936 Fts5Structure *pNew = 0;
241937
241938 assert( p->rc==SQLITE_OK );
241939 fts5IndexFlush(p);
241940 assert( p->nContentlessDelete==0 );
241941 pStruct = fts5StructureRead(p);
 
241942 fts5StructureInvalidate(p);
241943
241944 if( pStruct ){
241945 pNew = fts5IndexOptimizeStruct(p, pStruct);
241946 }
@@ -247513,11 +247610,11 @@
247513 int nArg, /* Number of args */
247514 sqlite3_value **apUnused /* Function arguments */
247515 ){
247516 assert( nArg==0 );
247517 UNUSED_PARAM2(nArg, apUnused);
247518 sqlite3_result_text(pCtx, "fts5: 2023-11-01 11:23:50 17129ba1ff7f0daf37100ee82d507aef7827cf38de1866e2633096ae6ad81301", -1, SQLITE_TRANSIENT);
247519 }
247520
247521 /*
247522 ** Return true if zName is the extension on one of the shadow tables used
247523 ** by this module.
247524
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -1,8 +1,8 @@
1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 ** version 3.44.1. By combining all the individual C code files into this
4 ** single large file, the entire code can be compiled as a single translation
5 ** unit. This allows many compilers to do optimizations that would not be
6 ** possible if the files were compiled separately. Performance improvements
7 ** of 5% or more are commonly seen when SQLite is compiled as a single
8 ** translation unit.
@@ -16,11 +16,11 @@
16 ** if you want a wrapper to interface SQLite with your choice of programming
17 ** language. The code for the "sqlite3" command-line shell is also in a
18 ** separate file. This file contains only code for the core SQLite library.
19 **
20 ** The content in this amalgamation comes from Fossil check-in
21 ** d295f48e8f367b066b881780c98bdf980a1d.
22 */
23 #define SQLITE_CORE 1
24 #define SQLITE_AMALGAMATION 1
25 #ifndef SQLITE_PRIVATE
26 # define SQLITE_PRIVATE static
@@ -457,13 +457,13 @@
457 **
458 ** See also: [sqlite3_libversion()],
459 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
460 ** [sqlite_version()] and [sqlite_source_id()].
461 */
462 #define SQLITE_VERSION "3.44.1"
463 #define SQLITE_VERSION_NUMBER 3044001
464 #define SQLITE_SOURCE_ID "2023-11-22 14:18:12 d295f48e8f367b066b881780c98bdf980a1d550397d5ba0b0e49842c95b3e8b4"
465
466 /*
467 ** CAPI3REF: Run-Time Library Version Numbers
468 ** KEYWORDS: sqlite3_version sqlite3_sourceid
469 **
@@ -5884,24 +5884,39 @@
5884 ** function has been carefully audited and found to be free of potentially
5885 ** security-adverse side-effects and information-leaks.
5886 ** </dd>
5887 **
5888 ** [[SQLITE_SUBTYPE]] <dt>SQLITE_SUBTYPE</dt><dd>
5889 ** The SQLITE_SUBTYPE flag indicates to SQLite that a function might call
5890 ** [sqlite3_value_subtype()] to inspect the sub-types of its arguments.
5891 ** This flag instructs SQLite to omit some corner-case optimizations that
5892 ** might disrupt the operation of the [sqlite3_value_subtype()] function,
5893 ** causing it to return zero rather than the correct subtype().
5894 ** SQL functions that invokes [sqlite3_value_subtype()] should have this
5895 ** property. If the SQLITE_SUBTYPE property is omitted, then the return
5896 ** value from [sqlite3_value_subtype()] might sometimes be zero even though
5897 ** a non-zero subtype was specified by the function argument expression.
5898 **
5899 ** [[SQLITE_RESULT_SUBTYPE]] <dt>SQLITE_RESULT_SUBTYPE</dt><dd>
5900 ** The SQLITE_RESULT_SUBTYPE flag indicates to SQLite that a function might call
5901 ** [sqlite3_result_subtype()] to cause a sub-type to be associated with its
5902 ** result.
5903 ** Every function that invokes [sqlite3_result_subtype()] should have this
5904 ** property. If it does not, then the call to [sqlite3_result_subtype()]
5905 ** might become a no-op if the function is used as term in an
5906 ** [expression index]. On the other hand, SQL functions that never invoke
5907 ** [sqlite3_result_subtype()] should avoid setting this property, as the
5908 ** purpose of this property is to disable certain optimizations that are
5909 ** incompatible with subtypes.
5910 ** </dd>
5911 ** </dl>
5912 */
5913 #define SQLITE_DETERMINISTIC 0x000000800
5914 #define SQLITE_DIRECTONLY 0x000080000
5915 #define SQLITE_SUBTYPE 0x000100000
5916 #define SQLITE_INNOCUOUS 0x000200000
5917 #define SQLITE_RESULT_SUBTYPE 0x001000000
5918
5919 /*
5920 ** CAPI3REF: Deprecated Functions
5921 ** DEPRECATED
5922 **
@@ -6094,10 +6109,16 @@
6109 ** The sqlite3_value_subtype(V) function returns the subtype for
6110 ** an [application-defined SQL function] argument V. The subtype
6111 ** information can be used to pass a limited amount of context from
6112 ** one SQL function to another. Use the [sqlite3_result_subtype()]
6113 ** routine to set the subtype for the return value of an SQL function.
6114 **
6115 ** Every [application-defined SQL function] that invoke this interface
6116 ** should include the [SQLITE_SUBTYPE] property in the text
6117 ** encoding argument when the function is [sqlite3_create_function|registered].
6118 ** If the [SQLITE_SUBTYPE] property is omitted, then sqlite3_value_subtype()
6119 ** might return zero instead of the upstream subtype in some corner cases.
6120 */
6121 SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value*);
6122
6123 /*
6124 ** CAPI3REF: Copy And Free SQL Values
@@ -6224,18 +6245,26 @@
6245 ** <li> ^(when [sqlite3_reset()] or [sqlite3_finalize()] is called for the
6246 ** SQL statement)^, or
6247 ** <li> ^(when sqlite3_set_auxdata() is invoked again on the same
6248 ** parameter)^, or
6249 ** <li> ^(during the original sqlite3_set_auxdata() call when a memory
6250 ** allocation error occurs.)^
6251 ** <li> ^(during the original sqlite3_set_auxdata() call if the function
6252 ** is evaluated during query planning instead of during query execution,
6253 ** as sometimes happens with [SQLITE_ENABLE_STAT4].)^ </ul>
6254 **
6255 ** Note the last two bullets in particular. The destructor X in
6256 ** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the
6257 ** sqlite3_set_auxdata() interface even returns. Hence sqlite3_set_auxdata()
6258 ** should be called near the end of the function implementation and the
6259 ** function implementation should not make any use of P after
6260 ** sqlite3_set_auxdata() has been called. Furthermore, a call to
6261 ** sqlite3_get_auxdata() that occurs immediately after a corresponding call
6262 ** to sqlite3_set_auxdata() might still return NULL if an out-of-memory
6263 ** condition occurred during the sqlite3_set_auxdata() call or if the
6264 ** function is being evaluated during query planning rather than during
6265 ** query execution.
6266 **
6267 ** ^(In practice, auxiliary data is preserved between function calls for
6268 ** function parameters that are compile-time constants, including literal
6269 ** values and [parameters] and expressions composed from the same.)^
6270 **
@@ -6505,10 +6534,24 @@
6534 ** [sqlite3_context] C to be the value T. Only the lower 8 bits
6535 ** of the subtype T are preserved in current versions of SQLite;
6536 ** higher order bits are discarded.
6537 ** The number of subtype bytes preserved by SQLite might increase
6538 ** in future releases of SQLite.
6539 **
6540 ** Every [application-defined SQL function] that invokes this interface
6541 ** should include the [SQLITE_RESULT_SUBTYPE] property in its
6542 ** text encoding argument when the SQL function is
6543 ** [sqlite3_create_function|registered]. If the [SQLITE_RESULT_SUBTYPE]
6544 ** property is omitted from the function that invokes sqlite3_result_subtype(),
6545 ** then in some cases the sqlite3_result_subtype() might fail to set
6546 ** the result subtype.
6547 **
6548 ** If SQLite is compiled with -DSQLITE_STRICT_SUBTYPE=1, then any
6549 ** SQL function that invokes the sqlite3_result_subtype() interface
6550 ** and that does not have the SQLITE_RESULT_SUBTYPE property will raise
6551 ** an error. Future versions of SQLite might enable -DSQLITE_STRICT_SUBTYPE=1
6552 ** by default.
6553 */
6554 SQLITE_API void sqlite3_result_subtype(sqlite3_context*,unsigned int);
6555
6556 /*
6557 ** CAPI3REF: Define New Collating Sequences
@@ -17809,18 +17852,19 @@
17852 #define SQLITE_FUNC_CONSTANT 0x0800 /* Constant inputs give a constant output */
17853 #define SQLITE_FUNC_MINMAX 0x1000 /* True for min() and max() aggregates */
17854 #define SQLITE_FUNC_SLOCHNG 0x2000 /* "Slow Change". Value constant during a
17855 ** single query - might change over time */
17856 #define SQLITE_FUNC_TEST 0x4000 /* Built-in testing functions */
17857 #define SQLITE_FUNC_RUNONLY 0x8000 /* Cannot be used by valueFromFunction */
17858 #define SQLITE_FUNC_WINDOW 0x00010000 /* Built-in window-only function */
17859 #define SQLITE_FUNC_INTERNAL 0x00040000 /* For use by NestedParse() only */
17860 #define SQLITE_FUNC_DIRECT 0x00080000 /* Not for use in TRIGGERs or VIEWs */
17861 /* SQLITE_SUBTYPE 0x00100000 // Consumer of subtypes */
17862 #define SQLITE_FUNC_UNSAFE 0x00200000 /* Function has side effects */
17863 #define SQLITE_FUNC_INLINE 0x00400000 /* Functions implemented in-line */
17864 #define SQLITE_FUNC_BUILTIN 0x00800000 /* This is a built-in function */
17865 /* SQLITE_RESULT_SUBTYPE 0x01000000 // Generator of subtypes */
17866 #define SQLITE_FUNC_ANYORDER 0x08000000 /* count/min/max aggregate */
17867
17868 /* Identifier numbers for each in-line function */
17869 #define INLINEFUNC_coalesce 0
17870 #define INLINEFUNC_implies_nonnull_row 1
@@ -17908,13 +17952,14 @@
17952 {nArg, SQLITE_FUNC_BUILTIN|SQLITE_UTF8|SQLITE_DIRECTONLY|SQLITE_FUNC_UNSAFE, \
17953 SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
17954 #define MFUNCTION(zName, nArg, xPtr, xFunc) \
17955 {nArg, SQLITE_FUNC_BUILTIN|SQLITE_FUNC_CONSTANT|SQLITE_UTF8, \
17956 xPtr, 0, xFunc, 0, 0, 0, #zName, {0} }
17957 #define JFUNCTION(zName, nArg, bUseCache, bWS, bRS, iArg, xFunc) \
17958 {nArg, SQLITE_FUNC_BUILTIN|SQLITE_DETERMINISTIC|SQLITE_FUNC_CONSTANT|\
17959 SQLITE_UTF8|((bUseCache)*SQLITE_FUNC_RUNONLY)|\
17960 ((bRS)*SQLITE_SUBTYPE)|((bWS)*SQLITE_RESULT_SUBTYPE), \
17961 SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
17962 #define INLINE_FUNC(zName, nArg, iArg, mFlags) \
17963 {nArg, SQLITE_FUNC_BUILTIN|\
17964 SQLITE_UTF8|SQLITE_FUNC_INLINE|SQLITE_FUNC_CONSTANT|(mFlags), \
17965 SQLITE_INT_TO_PTR(iArg), 0, noopFunc, 0, 0, 0, #zName, {0} }
@@ -29451,11 +29496,11 @@
29496 SQLITE_PRIVATE void sqlite3MemoryBarrier(void){
29497 #if defined(SQLITE_MEMORY_BARRIER)
29498 SQLITE_MEMORY_BARRIER;
29499 #elif defined(__GNUC__)
29500 __sync_synchronize();
29501 #elif MSVC_VERSION>=1400
29502 _ReadWriteBarrier();
29503 #elif defined(MemoryBarrier)
29504 MemoryBarrier();
29505 #endif
29506 }
@@ -61445,14 +61490,17 @@
61490 ** of the corresponding WAL or Journal name as passed into
61491 ** xOpen.
61492 */
61493 SQLITE_API sqlite3_file *sqlite3_database_file_object(const char *zName){
61494 Pager *pPager;
61495 const char *p;
61496 while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){
61497 zName--;
61498 }
61499 p = zName - 4 - sizeof(Pager*);
61500 assert( EIGHT_BYTE_ALIGNMENT(p) );
61501 pPager = *(Pager**)p;
61502 return pPager->fd;
61503 }
61504
61505
61506 /*
@@ -83409,11 +83457,11 @@
83457 #ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
83458 if( pFunc==0 ) return SQLITE_OK;
83459 #endif
83460 assert( pFunc );
83461 if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0
83462 || (pFunc->funcFlags & (SQLITE_FUNC_NEEDCOLL|SQLITE_FUNC_RUNONLY))!=0
83463 ){
83464 return SQLITE_OK;
83465 }
83466
83467 if( pList ){
@@ -89950,10 +89998,22 @@
89998 SQLITE_API void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){
89999 Mem *pOut;
90000 #ifdef SQLITE_ENABLE_API_ARMOR
90001 if( pCtx==0 ) return;
90002 #endif
90003 #if defined(SQLITE_STRICT_SUBTYPE) && SQLITE_STRICT_SUBTYPE+0!=0
90004 if( pCtx->pFunc!=0
90005 && (pCtx->pFunc->funcFlags & SQLITE_RESULT_SUBTYPE)==0
90006 ){
90007 char zErr[200];
90008 sqlite3_snprintf(sizeof(zErr), zErr,
90009 "misuse of sqlite3_result_subtype() by %s()",
90010 pCtx->pFunc->zName);
90011 sqlite3_result_error(pCtx, zErr, -1);
90012 return;
90013 }
90014 #endif /* SQLITE_STRICT_SUBTYPE */
90015 pOut = pCtx->pOut;
90016 assert( sqlite3_mutex_held(pOut->db->mutex) );
90017 pOut->eSubtype = eSubtype & 0xff;
90018 pOut->flags |= MEM_Subtype;
90019 }
@@ -100319,11 +100379,11 @@
100379 sqlite3VdbeMemSetNull(pOut); /* Innocent until proven guilty */
100380 assert( pOp->p4type==P4_TABLE );
100381 pTab = pOp->p4.pTab;
100382 assert( pTab!=0 );
100383 assert( IsVirtual(pTab) );
100384 if( pTab->u.vtab.p==0 ) break;
100385 pVtab = pTab->u.vtab.p->pVtab;
100386 assert( pVtab!=0 );
100387 pModule = pVtab->pModule;
100388 assert( pModule!=0 );
100389 assert( pModule->iVersion>=4 );
@@ -113915,12 +113975,12 @@
113975 ** Like sqlite3ExprCompare() except COLLATE operators at the top-level
113976 ** are ignored.
113977 */
113978 SQLITE_PRIVATE int sqlite3ExprCompareSkip(Expr *pA,Expr *pB, int iTab){
113979 return sqlite3ExprCompare(0,
113980 sqlite3ExprSkipCollate(pA),
113981 sqlite3ExprSkipCollate(pB),
113982 iTab);
113983 }
113984
113985 /*
113986 ** Return non-zero if Expr p can only be true if pNN is not NULL.
@@ -147603,14 +147663,15 @@
147663 Parse *pParse;
147664 int i;
147665 SrcList *pTabList;
147666 SrcItem *pFrom;
147667
 
147668 if( p->selFlags & SF_HasTypeInfo ) return;
147669 p->selFlags |= SF_HasTypeInfo;
147670 pParse = pWalker->pParse;
147671 testcase( (p->selFlags & SF_Resolved)==0 );
147672 assert( (p->selFlags & SF_Resolved) || IN_RENAME_OBJECT );
147673 pTabList = p->pSrc;
147674 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
147675 Table *pTab = pFrom->pTab;
147676 assert( pTab!=0 );
147677 if( (pTab->tabFlags & TF_Ephemeral)!=0 ){
@@ -148628,10 +148689,11 @@
148689 pItem->fg.jointype &= ~JT_LEFT;
148690 }else{
148691 TREETRACE(0x1000,pParse,p,
148692 ("LEFT-JOIN simplifies to JOIN on term %d\n",i));
148693 pItem->fg.jointype &= ~(JT_LEFT|JT_OUTER);
148694 unsetJoinExpr(p->pWhere, pItem->iCursor, 0);
148695 }
148696 }
148697 if( pItem->fg.jointype & JT_LTORJ ){
148698 for(j=i+1; j<pTabList->nSrc; j++){
148699 SrcItem *pI2 = &pTabList->a[j];
@@ -148642,21 +148704,19 @@
148704 pI2->fg.jointype &= ~JT_RIGHT;
148705 }else{
148706 TREETRACE(0x1000,pParse,p,
148707 ("RIGHT-JOIN simplifies to JOIN on term %d\n",j));
148708 pI2->fg.jointype &= ~(JT_RIGHT|JT_OUTER);
148709 unsetJoinExpr(p->pWhere, pI2->iCursor, 1);
148710 }
148711 }
148712 }
148713 for(j=pTabList->nSrc-1; j>=0; j--){
148714 pTabList->a[j].fg.jointype &= ~JT_LTORJ;
148715 if( pTabList->a[j].fg.jointype & JT_RIGHT ) break;
148716 }
148717 }
 
 
 
148718 }
148719
148720 /* No further action if this term of the FROM clause is not a subquery */
148721 if( pSub==0 ) continue;
148722
@@ -166056,10 +166116,24 @@
166116 bMaybeNullRow = 0;
166117 }else{
166118 continue;
166119 }
166120 if( sqlite3ExprIsConstant(pExpr) ) continue;
166121 if( pExpr->op==TK_FUNCTION ){
166122 /* Functions that might set a subtype should not be replaced by the
166123 ** value taken from an expression index since the index omits the
166124 ** subtype. https://sqlite.org/forum/forumpost/68d284c86b082c3e */
166125 int n;
166126 FuncDef *pDef;
166127 sqlite3 *db = pParse->db;
166128 assert( ExprUseXList(pExpr) );
166129 n = pExpr->x.pList ? pExpr->x.pList->nExpr : 0;
166130 pDef = sqlite3FindFunction(db, pExpr->u.zToken, n, ENC(db), 0);
166131 if( pDef==0 || (pDef->funcFlags & SQLITE_RESULT_SUBTYPE)!=0 ){
166132 continue;
166133 }
166134 }
166135 p = sqlite3DbMallocRaw(pParse->db, sizeof(IndexedExpr));
166136 if( p==0 ) break;
166137 p->pIENext = pParse->pIdxEpr;
166138 #ifdef WHERETRACE_ENABLED
166139 if( sqlite3WhereTrace & 0x200 ){
@@ -168238,11 +168312,11 @@
168312 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
168313 ExprList *pArgs;
168314 assert( ExprUseXList(pWin->pOwner) );
168315 assert( pWin->pWFunc!=0 );
168316 pArgs = pWin->pOwner->x.pList;
168317 if( pWin->pWFunc->funcFlags & SQLITE_SUBTYPE ){
168318 selectWindowRewriteEList(pParse, pMWin, pSrc, pArgs, pTab, &pSublist);
168319 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
168320 pWin->bExprArgs = 1;
168321 }else{
168322 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
@@ -179412,11 +179486,11 @@
179486 }
179487
179488 assert( SQLITE_FUNC_CONSTANT==SQLITE_DETERMINISTIC );
179489 assert( SQLITE_FUNC_DIRECT==SQLITE_DIRECTONLY );
179490 extraFlags = enc & (SQLITE_DETERMINISTIC|SQLITE_DIRECTONLY|
179491 SQLITE_SUBTYPE|SQLITE_INNOCUOUS|SQLITE_RESULT_SUBTYPE);
179492 enc &= (SQLITE_FUNC_ENCMASK|SQLITE_ANY);
179493
179494 /* The SQLITE_INNOCUOUS flag is the same bit as SQLITE_FUNC_UNSAFE. But
179495 ** the meaning is inverted. So flip the bit. */
179496 assert( SQLITE_FUNC_UNSAFE==SQLITE_INNOCUOUS );
@@ -202993,16 +203067,22 @@
203067 u32 i;
203068 jsonAppendChar(p, '"');
203069 zIn++;
203070 N -= 2;
203071 while( N>0 ){
203072 for(i=0; i<N && zIn[i]!='\\' && zIn[i]!='"'; i++){}
203073 if( i>0 ){
203074 jsonAppendRawNZ(p, zIn, i);
203075 zIn += i;
203076 N -= i;
203077 if( N==0 ) break;
203078 }
203079 if( zIn[0]=='"' ){
203080 jsonAppendRawNZ(p, "\\\"", 2);
203081 zIn++;
203082 N--;
203083 continue;
203084 }
203085 assert( zIn[0]=='\\' );
203086 switch( (u8)zIn[1] ){
203087 case '\'':
203088 jsonAppendChar(p, '\'');
@@ -203394,11 +203474,12 @@
203474 */
203475 static void jsonReturnJson(
203476 JsonParse *pParse, /* The complete JSON */
203477 JsonNode *pNode, /* Node to return */
203478 sqlite3_context *pCtx, /* Return value for this function */
203479 int bGenerateAlt, /* Also store the rendered text in zAlt */
203480 int omitSubtype /* Do not call sqlite3_result_subtype() */
203481 ){
203482 JsonString s;
203483 if( pParse->oom ){
203484 sqlite3_result_error_nomem(pCtx);
203485 return;
@@ -203409,11 +203490,11 @@
203490 if( bGenerateAlt && pParse->zAlt==0 && jsonForceRCStr(&s) ){
203491 pParse->zAlt = sqlite3RCStrRef(s.zBuf);
203492 pParse->nAlt = s.nUsed;
203493 }
203494 jsonResult(&s);
203495 if( !omitSubtype ) sqlite3_result_subtype(pCtx, JSON_SUBTYPE);
203496 }
203497 }
203498
203499 /*
203500 ** Translate a single byte of Hex into an integer.
@@ -203450,11 +203531,12 @@
203531 ** Make the JsonNode the return value of the function.
203532 */
203533 static void jsonReturn(
203534 JsonParse *pParse, /* Complete JSON parse tree */
203535 JsonNode *pNode, /* Node to return */
203536 sqlite3_context *pCtx, /* Return value for this function */
203537 int omitSubtype /* Do not call sqlite3_result_subtype() */
203538 ){
203539 switch( pNode->eType ){
203540 default: {
203541 assert( pNode->eType==JSON_NULL );
203542 sqlite3_result_null(pCtx);
@@ -203596,11 +203678,11 @@
203678 }
203679 break;
203680 }
203681 case JSON_ARRAY:
203682 case JSON_OBJECT: {
203683 jsonReturnJson(pParse, pNode, pCtx, 0, omitSubtype);
203684 break;
203685 }
203686 }
203687 }
203688
@@ -204948,11 +205030,11 @@
205030 printf("hasMod = %u\n", p->hasMod);
205031 printf("nJPRef = %u\n", p->nJPRef);
205032 printf("iSubst = %u\n", p->iSubst);
205033 printf("iHold = %u\n", p->iHold);
205034 jsonDebugPrintNodeEntries(p->aNode, p->nNode);
205035 jsonReturnJson(p, p->aNode, ctx, 1, 0);
205036 }
205037
205038 /*
205039 ** The json_test1(JSON) function return true (1) if the input is JSON
205040 ** text generated by another json function. It returns (0) if the input
@@ -205134,19 +205216,18 @@
205216 }else{
205217 pNode = jsonLookup(p, zPath, 0, ctx);
205218 }
205219 if( pNode ){
205220 if( flags & JSON_JSON ){
205221 jsonReturnJson(p, pNode, ctx, 0, 0);
205222 }else{
205223 jsonReturn(p, pNode, ctx, 1);
 
205224 }
205225 }
205226 }else{
205227 pNode = jsonLookup(p, zPath, 0, ctx);
205228 if( p->nErr==0 && pNode ) jsonReturn(p, pNode, ctx, 0);
205229 }
205230 }else{
205231 /* Two or more PATH arguments results in a JSON array with each
205232 ** element of the array being the value selected by one of the PATHs */
205233 int i;
@@ -205268,11 +205349,11 @@
205349 pResult = jsonMergePatch(pX, 0, pY->aNode);
205350 assert( pResult!=0 || pX->oom );
205351 if( pResult && pX->oom==0 ){
205352 jsonDebugPrintParse(pX);
205353 jsonDebugPrintNode(pResult);
205354 jsonReturnJson(pX, pResult, ctx, 0, 0);
205355 }else{
205356 sqlite3_result_error_nomem(ctx);
205357 }
205358 }
205359
@@ -205347,11 +205428,11 @@
205428 pParse->hasMod = 1;
205429 pParse->useMod = 1;
205430 }
205431 }
205432 if( (pParse->aNode[0].jnFlags & JNODE_REMOVE)==0 ){
205433 jsonReturnJson(pParse, pParse->aNode, ctx, 1, 0);
205434 }
205435 remove_done:
205436 jsonDebugPrintParse(p);
205437 }
205438
@@ -205476,11 +205557,11 @@
205557 if( pParse->nErr ) goto replace_err;
205558 if( pNode ){
205559 jsonReplaceNode(ctx, pParse, (u32)(pNode - pParse->aNode), argv[i+1]);
205560 }
205561 }
205562 jsonReturnJson(pParse, pParse->aNode, ctx, 1, 0);
205563 replace_err:
205564 jsonDebugPrintParse(pParse);
205565 jsonParseFree(pParse);
205566 }
205567
@@ -205530,11 +205611,11 @@
205611 }else if( pNode && (bApnd || bIsSet) ){
205612 jsonReplaceNode(ctx, pParse, (u32)(pNode - pParse->aNode), argv[i+1]);
205613 }
205614 }
205615 jsonDebugPrintParse(pParse);
205616 jsonReturnJson(pParse, pParse->aNode, ctx, 1, 0);
205617 jsonSetDone:
205618 jsonParseFree(pParse);
205619 }
205620
205621 /*
@@ -206045,11 +206126,11 @@
206126 JsonNode *pThis = &p->sParse.aNode[p->i];
206127 switch( i ){
206128 case JEACH_KEY: {
206129 if( p->i==0 ) break;
206130 if( p->eType==JSON_OBJECT ){
206131 jsonReturn(&p->sParse, pThis, ctx, 0);
206132 }else if( p->eType==JSON_ARRAY ){
206133 u32 iKey;
206134 if( p->bRecursive ){
206135 if( p->iRowid==0 ) break;
206136 assert( p->sParse.aNode[p->sParse.aUp[p->i]].eU==3 );
@@ -206061,11 +206142,11 @@
206142 }
206143 break;
206144 }
206145 case JEACH_VALUE: {
206146 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
206147 jsonReturn(&p->sParse, pThis, ctx, 0);
206148 break;
206149 }
206150 case JEACH_TYPE: {
206151 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
206152 sqlite3_result_text(ctx, jsonType[pThis->eType], -1, SQLITE_STATIC);
@@ -206072,11 +206153,11 @@
206153 break;
206154 }
206155 case JEACH_ATOM: {
206156 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
206157 if( pThis->eType>=JSON_ARRAY ) break;
206158 jsonReturn(&p->sParse, pThis, ctx, 0);
206159 break;
206160 }
206161 case JEACH_ID: {
206162 sqlite3_result_int64(ctx,
206163 (sqlite3_int64)p->i + ((pThis->jnFlags & JNODE_LABEL)!=0));
@@ -206365,38 +206446,47 @@
206446 ** Register JSON functions.
206447 */
206448 SQLITE_PRIVATE void sqlite3RegisterJsonFunctions(void){
206449 #ifndef SQLITE_OMIT_JSON
206450 static FuncDef aJsonFunc[] = {
206451 /* calls sqlite3_result_subtype() */
206452 /* | */
206453 /* Uses cache ______ | __ calls sqlite3_value_subtype() */
206454 /* | | | */
206455 /* Num args _________ | | | ___ Flags */
206456 /* | | | | | */
206457 /* | | | | | */
206458 JFUNCTION(json, 1, 1, 1, 0, 0, jsonRemoveFunc),
206459 JFUNCTION(json_array, -1, 0, 1, 1, 0, jsonArrayFunc),
206460 JFUNCTION(json_array_length, 1, 1, 0, 0, 0, jsonArrayLengthFunc),
206461 JFUNCTION(json_array_length, 2, 1, 0, 0, 0, jsonArrayLengthFunc),
206462 JFUNCTION(json_error_position,1, 1, 0, 0, 0, jsonErrorFunc),
206463 JFUNCTION(json_extract, -1, 1, 1, 0, 0, jsonExtractFunc),
206464 JFUNCTION(->, 2, 1, 1, 0, JSON_JSON, jsonExtractFunc),
206465 JFUNCTION(->>, 2, 1, 0, 0, JSON_SQL, jsonExtractFunc),
206466 JFUNCTION(json_insert, -1, 1, 1, 1, 0, jsonSetFunc),
206467 JFUNCTION(json_object, -1, 0, 1, 1, 0, jsonObjectFunc),
206468 JFUNCTION(json_patch, 2, 1, 1, 0, 0, jsonPatchFunc),
206469 JFUNCTION(json_quote, 1, 0, 1, 1, 0, jsonQuoteFunc),
206470 JFUNCTION(json_remove, -1, 1, 1, 0, 0, jsonRemoveFunc),
206471 JFUNCTION(json_replace, -1, 1, 1, 1, 0, jsonReplaceFunc),
206472 JFUNCTION(json_set, -1, 1, 1, 1, JSON_ISSET, jsonSetFunc),
206473 JFUNCTION(json_type, 1, 1, 0, 0, 0, jsonTypeFunc),
206474 JFUNCTION(json_type, 2, 1, 0, 0, 0, jsonTypeFunc),
206475 JFUNCTION(json_valid, 1, 1, 0, 0, 0, jsonValidFunc),
206476 #ifdef SQLITE_DEBUG
206477 JFUNCTION(json_parse, 1, 1, 1, 0, 0, jsonParseFunc),
206478 JFUNCTION(json_test1, 1, 1, 0, 1, 0, jsonTest1Func),
206479 #endif
206480 WAGGREGATE(json_group_array, 1, 0, 0,
206481 jsonArrayStep, jsonArrayFinal, jsonArrayValue, jsonGroupInverse,
206482 SQLITE_SUBTYPE|SQLITE_RESULT_SUBTYPE|SQLITE_UTF8|
206483 SQLITE_DETERMINISTIC),
206484 WAGGREGATE(json_group_object, 2, 0, 0,
206485 jsonObjectStep, jsonObjectFinal, jsonObjectValue, jsonGroupInverse,
206486 SQLITE_SUBTYPE|SQLITE_RESULT_SUBTYPE|SQLITE_UTF8|
206487 SQLITE_DETERMINISTIC)
206488 };
206489 sqlite3InsertBuiltinFuncs(aJsonFunc, ArraySize(aJsonFunc));
206490 #endif
206491 }
206492
@@ -236129,14 +236219,12 @@
236219
236220 return pRet;
236221 }
236222
236223 /*
236224 ** Link all tokens from hash table iHash into a list in sorted order. The
236225 ** tokens are not removed from the hash table.
 
 
236226 */
236227 static int fts5HashEntrySort(
236228 Fts5Hash *pHash,
236229 const char *pTerm, int nTerm, /* Query prefix, if any */
236230 Fts5HashEntry **ppSorted
@@ -238998,10 +239086,18 @@
239086 pLeaf = fts5IdxMalloc(p, sizeof(Fts5Data));
239087 if( pLeaf ){
239088 pLeaf->p = (u8*)pList;
239089 }
239090 }
239091
239092 /* The call to sqlite3Fts5HashScanInit() causes the hash table to
239093 ** fill the size field of all existing position lists. This means they
239094 ** can no longer be appended to. Since the only scenario in which they
239095 ** can be appended to is if the previous operation on this table was
239096 ** a DELETE, by clearing the Fts5Index.bDelete flag we can avoid this
239097 ** possibility altogether. */
239098 p->bDelete = 0;
239099 }else{
239100 p->rc = sqlite3Fts5HashQuery(p->pHash, sizeof(Fts5Data),
239101 (const char*)pTerm, nTerm, (void**)&pLeaf, &nList
239102 );
239103 if( pLeaf ){
@@ -240675,11 +240771,11 @@
240771 ){
240772 Fts5PageWriter *pPage = &pWriter->writer;
240773 const u8 *a = aData;
240774 int n = nData;
240775
240776 assert( p->pConfig->pgsz>0 || p->rc!=SQLITE_OK );
240777 while( p->rc==SQLITE_OK
240778 && (pPage->buf.n + pPage->pgidx.n + n)>=p->pConfig->pgsz
240779 ){
240780 int nReq = p->pConfig->pgsz - pPage->buf.n - pPage->pgidx.n;
240781 int nCopy = 0;
@@ -241935,12 +242031,13 @@
242031 Fts5Structure *pStruct;
242032 Fts5Structure *pNew = 0;
242033
242034 assert( p->rc==SQLITE_OK );
242035 fts5IndexFlush(p);
242036 assert( p->rc!=SQLITE_OK || p->nContentlessDelete==0 );
242037 pStruct = fts5StructureRead(p);
242038 assert( p->rc!=SQLITE_OK || pStruct!=0 );
242039 fts5StructureInvalidate(p);
242040
242041 if( pStruct ){
242042 pNew = fts5IndexOptimizeStruct(p, pStruct);
242043 }
@@ -247513,11 +247610,11 @@
247610 int nArg, /* Number of args */
247611 sqlite3_value **apUnused /* Function arguments */
247612 ){
247613 assert( nArg==0 );
247614 UNUSED_PARAM2(nArg, apUnused);
247615 sqlite3_result_text(pCtx, "fts5: 2023-11-22 14:18:12 d295f48e8f367b066b881780c98bdf980a1d550397d5ba0b0e49842c95b3e8b4", -1, SQLITE_TRANSIENT);
247616 }
247617
247618 /*
247619 ** Return true if zName is the extension on one of the shadow tables used
247620 ** by this module.
247621
+55 -12
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -144,13 +144,13 @@
144144
**
145145
** See also: [sqlite3_libversion()],
146146
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147147
** [sqlite_version()] and [sqlite_source_id()].
148148
*/
149
-#define SQLITE_VERSION "3.44.0"
150
-#define SQLITE_VERSION_NUMBER 3044000
151
-#define SQLITE_SOURCE_ID "2023-11-01 11:23:50 17129ba1ff7f0daf37100ee82d507aef7827cf38de1866e2633096ae6ad81301"
149
+#define SQLITE_VERSION "3.44.1"
150
+#define SQLITE_VERSION_NUMBER 3044001
151
+#define SQLITE_SOURCE_ID "2023-11-22 14:18:12 d295f48e8f367b066b881780c98bdf980a1d550397d5ba0b0e49842c95b3e8b4"
152152
153153
/*
154154
** CAPI3REF: Run-Time Library Version Numbers
155155
** KEYWORDS: sqlite3_version sqlite3_sourceid
156156
**
@@ -5571,24 +5571,39 @@
55715571
** function has been carefully audited and found to be free of potentially
55725572
** security-adverse side-effects and information-leaks.
55735573
** </dd>
55745574
**
55755575
** [[SQLITE_SUBTYPE]] <dt>SQLITE_SUBTYPE</dt><dd>
5576
-** The SQLITE_SUBTYPE flag indicates to SQLite that a function may call
5576
+** The SQLITE_SUBTYPE flag indicates to SQLite that a function might call
55775577
** [sqlite3_value_subtype()] to inspect the sub-types of its arguments.
5578
-** Specifying this flag makes no difference for scalar or aggregate user
5579
-** functions. However, if it is not specified for a user-defined window
5580
-** function, then any sub-types belonging to arguments passed to the window
5581
-** function may be discarded before the window function is called (i.e.
5582
-** sqlite3_value_subtype() will always return 0).
5578
+** This flag instructs SQLite to omit some corner-case optimizations that
5579
+** might disrupt the operation of the [sqlite3_value_subtype()] function,
5580
+** causing it to return zero rather than the correct subtype().
5581
+** SQL functions that invokes [sqlite3_value_subtype()] should have this
5582
+** property. If the SQLITE_SUBTYPE property is omitted, then the return
5583
+** value from [sqlite3_value_subtype()] might sometimes be zero even though
5584
+** a non-zero subtype was specified by the function argument expression.
5585
+**
5586
+** [[SQLITE_RESULT_SUBTYPE]] <dt>SQLITE_RESULT_SUBTYPE</dt><dd>
5587
+** The SQLITE_RESULT_SUBTYPE flag indicates to SQLite that a function might call
5588
+** [sqlite3_result_subtype()] to cause a sub-type to be associated with its
5589
+** result.
5590
+** Every function that invokes [sqlite3_result_subtype()] should have this
5591
+** property. If it does not, then the call to [sqlite3_result_subtype()]
5592
+** might become a no-op if the function is used as term in an
5593
+** [expression index]. On the other hand, SQL functions that never invoke
5594
+** [sqlite3_result_subtype()] should avoid setting this property, as the
5595
+** purpose of this property is to disable certain optimizations that are
5596
+** incompatible with subtypes.
55835597
** </dd>
55845598
** </dl>
55855599
*/
55865600
#define SQLITE_DETERMINISTIC 0x000000800
55875601
#define SQLITE_DIRECTONLY 0x000080000
55885602
#define SQLITE_SUBTYPE 0x000100000
55895603
#define SQLITE_INNOCUOUS 0x000200000
5604
+#define SQLITE_RESULT_SUBTYPE 0x001000000
55905605
55915606
/*
55925607
** CAPI3REF: Deprecated Functions
55935608
** DEPRECATED
55945609
**
@@ -5781,10 +5796,16 @@
57815796
** The sqlite3_value_subtype(V) function returns the subtype for
57825797
** an [application-defined SQL function] argument V. The subtype
57835798
** information can be used to pass a limited amount of context from
57845799
** one SQL function to another. Use the [sqlite3_result_subtype()]
57855800
** routine to set the subtype for the return value of an SQL function.
5801
+**
5802
+** Every [application-defined SQL function] that invoke this interface
5803
+** should include the [SQLITE_SUBTYPE] property in the text
5804
+** encoding argument when the function is [sqlite3_create_function|registered].
5805
+** If the [SQLITE_SUBTYPE] property is omitted, then sqlite3_value_subtype()
5806
+** might return zero instead of the upstream subtype in some corner cases.
57865807
*/
57875808
SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value*);
57885809
57895810
/*
57905811
** CAPI3REF: Copy And Free SQL Values
@@ -5911,18 +5932,26 @@
59115932
** <li> ^(when [sqlite3_reset()] or [sqlite3_finalize()] is called for the
59125933
** SQL statement)^, or
59135934
** <li> ^(when sqlite3_set_auxdata() is invoked again on the same
59145935
** parameter)^, or
59155936
** <li> ^(during the original sqlite3_set_auxdata() call when a memory
5916
-** allocation error occurs.)^ </ul>
5937
+** allocation error occurs.)^
5938
+** <li> ^(during the original sqlite3_set_auxdata() call if the function
5939
+** is evaluated during query planning instead of during query execution,
5940
+** as sometimes happens with [SQLITE_ENABLE_STAT4].)^ </ul>
59175941
**
5918
-** Note the last bullet in particular. The destructor X in
5942
+** Note the last two bullets in particular. The destructor X in
59195943
** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the
59205944
** sqlite3_set_auxdata() interface even returns. Hence sqlite3_set_auxdata()
59215945
** should be called near the end of the function implementation and the
59225946
** function implementation should not make any use of P after
5923
-** sqlite3_set_auxdata() has been called.
5947
+** sqlite3_set_auxdata() has been called. Furthermore, a call to
5948
+** sqlite3_get_auxdata() that occurs immediately after a corresponding call
5949
+** to sqlite3_set_auxdata() might still return NULL if an out-of-memory
5950
+** condition occurred during the sqlite3_set_auxdata() call or if the
5951
+** function is being evaluated during query planning rather than during
5952
+** query execution.
59245953
**
59255954
** ^(In practice, auxiliary data is preserved between function calls for
59265955
** function parameters that are compile-time constants, including literal
59275956
** values and [parameters] and expressions composed from the same.)^
59285957
**
@@ -6192,10 +6221,24 @@
61926221
** [sqlite3_context] C to be the value T. Only the lower 8 bits
61936222
** of the subtype T are preserved in current versions of SQLite;
61946223
** higher order bits are discarded.
61956224
** The number of subtype bytes preserved by SQLite might increase
61966225
** in future releases of SQLite.
6226
+**
6227
+** Every [application-defined SQL function] that invokes this interface
6228
+** should include the [SQLITE_RESULT_SUBTYPE] property in its
6229
+** text encoding argument when the SQL function is
6230
+** [sqlite3_create_function|registered]. If the [SQLITE_RESULT_SUBTYPE]
6231
+** property is omitted from the function that invokes sqlite3_result_subtype(),
6232
+** then in some cases the sqlite3_result_subtype() might fail to set
6233
+** the result subtype.
6234
+**
6235
+** If SQLite is compiled with -DSQLITE_STRICT_SUBTYPE=1, then any
6236
+** SQL function that invokes the sqlite3_result_subtype() interface
6237
+** and that does not have the SQLITE_RESULT_SUBTYPE property will raise
6238
+** an error. Future versions of SQLite might enable -DSQLITE_STRICT_SUBTYPE=1
6239
+** by default.
61976240
*/
61986241
SQLITE_API void sqlite3_result_subtype(sqlite3_context*,unsigned int);
61996242
62006243
/*
62016244
** CAPI3REF: Define New Collating Sequences
62026245
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -144,13 +144,13 @@
144 **
145 ** See also: [sqlite3_libversion()],
146 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147 ** [sqlite_version()] and [sqlite_source_id()].
148 */
149 #define SQLITE_VERSION "3.44.0"
150 #define SQLITE_VERSION_NUMBER 3044000
151 #define SQLITE_SOURCE_ID "2023-11-01 11:23:50 17129ba1ff7f0daf37100ee82d507aef7827cf38de1866e2633096ae6ad81301"
152
153 /*
154 ** CAPI3REF: Run-Time Library Version Numbers
155 ** KEYWORDS: sqlite3_version sqlite3_sourceid
156 **
@@ -5571,24 +5571,39 @@
5571 ** function has been carefully audited and found to be free of potentially
5572 ** security-adverse side-effects and information-leaks.
5573 ** </dd>
5574 **
5575 ** [[SQLITE_SUBTYPE]] <dt>SQLITE_SUBTYPE</dt><dd>
5576 ** The SQLITE_SUBTYPE flag indicates to SQLite that a function may call
5577 ** [sqlite3_value_subtype()] to inspect the sub-types of its arguments.
5578 ** Specifying this flag makes no difference for scalar or aggregate user
5579 ** functions. However, if it is not specified for a user-defined window
5580 ** function, then any sub-types belonging to arguments passed to the window
5581 ** function may be discarded before the window function is called (i.e.
5582 ** sqlite3_value_subtype() will always return 0).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5583 ** </dd>
5584 ** </dl>
5585 */
5586 #define SQLITE_DETERMINISTIC 0x000000800
5587 #define SQLITE_DIRECTONLY 0x000080000
5588 #define SQLITE_SUBTYPE 0x000100000
5589 #define SQLITE_INNOCUOUS 0x000200000
 
5590
5591 /*
5592 ** CAPI3REF: Deprecated Functions
5593 ** DEPRECATED
5594 **
@@ -5781,10 +5796,16 @@
5781 ** The sqlite3_value_subtype(V) function returns the subtype for
5782 ** an [application-defined SQL function] argument V. The subtype
5783 ** information can be used to pass a limited amount of context from
5784 ** one SQL function to another. Use the [sqlite3_result_subtype()]
5785 ** routine to set the subtype for the return value of an SQL function.
 
 
 
 
 
 
5786 */
5787 SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value*);
5788
5789 /*
5790 ** CAPI3REF: Copy And Free SQL Values
@@ -5911,18 +5932,26 @@
5911 ** <li> ^(when [sqlite3_reset()] or [sqlite3_finalize()] is called for the
5912 ** SQL statement)^, or
5913 ** <li> ^(when sqlite3_set_auxdata() is invoked again on the same
5914 ** parameter)^, or
5915 ** <li> ^(during the original sqlite3_set_auxdata() call when a memory
5916 ** allocation error occurs.)^ </ul>
 
 
 
5917 **
5918 ** Note the last bullet in particular. The destructor X in
5919 ** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the
5920 ** sqlite3_set_auxdata() interface even returns. Hence sqlite3_set_auxdata()
5921 ** should be called near the end of the function implementation and the
5922 ** function implementation should not make any use of P after
5923 ** sqlite3_set_auxdata() has been called.
 
 
 
 
 
5924 **
5925 ** ^(In practice, auxiliary data is preserved between function calls for
5926 ** function parameters that are compile-time constants, including literal
5927 ** values and [parameters] and expressions composed from the same.)^
5928 **
@@ -6192,10 +6221,24 @@
6192 ** [sqlite3_context] C to be the value T. Only the lower 8 bits
6193 ** of the subtype T are preserved in current versions of SQLite;
6194 ** higher order bits are discarded.
6195 ** The number of subtype bytes preserved by SQLite might increase
6196 ** in future releases of SQLite.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6197 */
6198 SQLITE_API void sqlite3_result_subtype(sqlite3_context*,unsigned int);
6199
6200 /*
6201 ** CAPI3REF: Define New Collating Sequences
6202
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -144,13 +144,13 @@
144 **
145 ** See also: [sqlite3_libversion()],
146 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147 ** [sqlite_version()] and [sqlite_source_id()].
148 */
149 #define SQLITE_VERSION "3.44.1"
150 #define SQLITE_VERSION_NUMBER 3044001
151 #define SQLITE_SOURCE_ID "2023-11-22 14:18:12 d295f48e8f367b066b881780c98bdf980a1d550397d5ba0b0e49842c95b3e8b4"
152
153 /*
154 ** CAPI3REF: Run-Time Library Version Numbers
155 ** KEYWORDS: sqlite3_version sqlite3_sourceid
156 **
@@ -5571,24 +5571,39 @@
5571 ** function has been carefully audited and found to be free of potentially
5572 ** security-adverse side-effects and information-leaks.
5573 ** </dd>
5574 **
5575 ** [[SQLITE_SUBTYPE]] <dt>SQLITE_SUBTYPE</dt><dd>
5576 ** The SQLITE_SUBTYPE flag indicates to SQLite that a function might call
5577 ** [sqlite3_value_subtype()] to inspect the sub-types of its arguments.
5578 ** This flag instructs SQLite to omit some corner-case optimizations that
5579 ** might disrupt the operation of the [sqlite3_value_subtype()] function,
5580 ** causing it to return zero rather than the correct subtype().
5581 ** SQL functions that invokes [sqlite3_value_subtype()] should have this
5582 ** property. If the SQLITE_SUBTYPE property is omitted, then the return
5583 ** value from [sqlite3_value_subtype()] might sometimes be zero even though
5584 ** a non-zero subtype was specified by the function argument expression.
5585 **
5586 ** [[SQLITE_RESULT_SUBTYPE]] <dt>SQLITE_RESULT_SUBTYPE</dt><dd>
5587 ** The SQLITE_RESULT_SUBTYPE flag indicates to SQLite that a function might call
5588 ** [sqlite3_result_subtype()] to cause a sub-type to be associated with its
5589 ** result.
5590 ** Every function that invokes [sqlite3_result_subtype()] should have this
5591 ** property. If it does not, then the call to [sqlite3_result_subtype()]
5592 ** might become a no-op if the function is used as term in an
5593 ** [expression index]. On the other hand, SQL functions that never invoke
5594 ** [sqlite3_result_subtype()] should avoid setting this property, as the
5595 ** purpose of this property is to disable certain optimizations that are
5596 ** incompatible with subtypes.
5597 ** </dd>
5598 ** </dl>
5599 */
5600 #define SQLITE_DETERMINISTIC 0x000000800
5601 #define SQLITE_DIRECTONLY 0x000080000
5602 #define SQLITE_SUBTYPE 0x000100000
5603 #define SQLITE_INNOCUOUS 0x000200000
5604 #define SQLITE_RESULT_SUBTYPE 0x001000000
5605
5606 /*
5607 ** CAPI3REF: Deprecated Functions
5608 ** DEPRECATED
5609 **
@@ -5781,10 +5796,16 @@
5796 ** The sqlite3_value_subtype(V) function returns the subtype for
5797 ** an [application-defined SQL function] argument V. The subtype
5798 ** information can be used to pass a limited amount of context from
5799 ** one SQL function to another. Use the [sqlite3_result_subtype()]
5800 ** routine to set the subtype for the return value of an SQL function.
5801 **
5802 ** Every [application-defined SQL function] that invoke this interface
5803 ** should include the [SQLITE_SUBTYPE] property in the text
5804 ** encoding argument when the function is [sqlite3_create_function|registered].
5805 ** If the [SQLITE_SUBTYPE] property is omitted, then sqlite3_value_subtype()
5806 ** might return zero instead of the upstream subtype in some corner cases.
5807 */
5808 SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value*);
5809
5810 /*
5811 ** CAPI3REF: Copy And Free SQL Values
@@ -5911,18 +5932,26 @@
5932 ** <li> ^(when [sqlite3_reset()] or [sqlite3_finalize()] is called for the
5933 ** SQL statement)^, or
5934 ** <li> ^(when sqlite3_set_auxdata() is invoked again on the same
5935 ** parameter)^, or
5936 ** <li> ^(during the original sqlite3_set_auxdata() call when a memory
5937 ** allocation error occurs.)^
5938 ** <li> ^(during the original sqlite3_set_auxdata() call if the function
5939 ** is evaluated during query planning instead of during query execution,
5940 ** as sometimes happens with [SQLITE_ENABLE_STAT4].)^ </ul>
5941 **
5942 ** Note the last two bullets in particular. The destructor X in
5943 ** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the
5944 ** sqlite3_set_auxdata() interface even returns. Hence sqlite3_set_auxdata()
5945 ** should be called near the end of the function implementation and the
5946 ** function implementation should not make any use of P after
5947 ** sqlite3_set_auxdata() has been called. Furthermore, a call to
5948 ** sqlite3_get_auxdata() that occurs immediately after a corresponding call
5949 ** to sqlite3_set_auxdata() might still return NULL if an out-of-memory
5950 ** condition occurred during the sqlite3_set_auxdata() call or if the
5951 ** function is being evaluated during query planning rather than during
5952 ** query execution.
5953 **
5954 ** ^(In practice, auxiliary data is preserved between function calls for
5955 ** function parameters that are compile-time constants, including literal
5956 ** values and [parameters] and expressions composed from the same.)^
5957 **
@@ -6192,10 +6221,24 @@
6221 ** [sqlite3_context] C to be the value T. Only the lower 8 bits
6222 ** of the subtype T are preserved in current versions of SQLite;
6223 ** higher order bits are discarded.
6224 ** The number of subtype bytes preserved by SQLite might increase
6225 ** in future releases of SQLite.
6226 **
6227 ** Every [application-defined SQL function] that invokes this interface
6228 ** should include the [SQLITE_RESULT_SUBTYPE] property in its
6229 ** text encoding argument when the SQL function is
6230 ** [sqlite3_create_function|registered]. If the [SQLITE_RESULT_SUBTYPE]
6231 ** property is omitted from the function that invokes sqlite3_result_subtype(),
6232 ** then in some cases the sqlite3_result_subtype() might fail to set
6233 ** the result subtype.
6234 **
6235 ** If SQLite is compiled with -DSQLITE_STRICT_SUBTYPE=1, then any
6236 ** SQL function that invokes the sqlite3_result_subtype() interface
6237 ** and that does not have the SQLITE_RESULT_SUBTYPE property will raise
6238 ** an error. Future versions of SQLite might enable -DSQLITE_STRICT_SUBTYPE=1
6239 ** by default.
6240 */
6241 SQLITE_API void sqlite3_result_subtype(sqlite3_context*,unsigned int);
6242
6243 /*
6244 ** CAPI3REF: Define New Collating Sequences
6245

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button