Fossil SCM

Merge the latest SQLite enhancements, and in particular the new ".www" dot-command available to "fossil sql".

drh 2024-09-26 19:49 trunk
Commit c20aa86727773e3a8a87ec576ec646302f364cdc6d4a4ac76c2100afe96a89c0
3 files changed +1342 -1789 +32 -20 +7 -3
+1342 -1789
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -208,12 +208,10 @@
208208
# define unlink _unlink
209209
# endif
210210
# ifndef strdup
211211
# define strdup _strdup
212212
# endif
213
-# undef popen
214
-# define popen _popen
215213
# undef pclose
216214
# define pclose _pclose
217215
# endif
218216
#else
219217
/* Make sure isatty() has a prototype. */
@@ -253,10 +251,283 @@
253251
/* string conversion routines only needed on Win32 */
254252
extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
255253
extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
256254
#endif
257255
256
+/************************* Begin ../ext/misc/sqlite3_stdio.h ******************/
257
+/*
258
+** 2024-09-24
259
+**
260
+** The author disclaims copyright to this source code. In place of
261
+** a legal notice, here is a blessing:
262
+**
263
+** May you do good and not evil.
264
+** May you find forgiveness for yourself and forgive others.
265
+** May you share freely, never taking more than you give.
266
+**
267
+*************************************************************************
268
+**
269
+** This header file contains definitions of interfaces that provide
270
+** cross-platform I/O for UTF-8 content.
271
+**
272
+** On most platforms, the interfaces definitions in this file are
273
+** just #defines. For example sqlite3_fopen() is a macro that resolves
274
+** to the standard fopen() in the C-library.
275
+**
276
+** But Windows does not have a standard C-library, at least not one that
277
+** can handle UTF-8. So for windows build, the interfaces resolve to new
278
+** C-language routines contained in the separate sqlite3_stdio.c source file.
279
+**
280
+** So on all non-Windows platforms, simply #include this header file and
281
+** use the interfaces defined herein. Then to run your application on Windows,
282
+** also link in the accompanying sqlite3_stdio.c source file when compiling
283
+** to get compatible interfaces.
284
+*/
285
+#ifndef _SQLITE3_STDIO_H_
286
+#define _SQLITE3_STDIO_H_ 1
287
+#ifdef _WIN32
288
+/**** Definitions For Windows ****/
289
+#include <stdio.h>
290
+#include <windows.h>
291
+
292
+FILE *sqlite3_fopen(const char *zFilename, const char *zMode);
293
+FILE *sqlite3_popen(const char *zCommand, const char *type);
294
+char *sqlite3_fgets(char *s, int size, FILE *stream);
295
+int sqlite3_fputs(const char *s, FILE *stream);
296
+int sqlite3_fprintf(FILE *stream, const char *format, ...);
297
+void sqlite3_fsetmode(FILE *stream, int mode);
298
+
299
+
300
+#else
301
+/**** Definitions For All Other Platforms ****/
302
+#include <stdio.h>
303
+#define sqlite3_fopen fopen
304
+#define sqlite3_popen popen
305
+#define sqlite3_fgets fgets
306
+#define sqlite3_fputs fputs
307
+#define sqlite3_fprintf fprintf
308
+#define sqlite3_fsetmode(F,X) /*no-op*/
309
+
310
+#endif
311
+#endif /* _SQLITE3_STDIO_H_ */
312
+
313
+/************************* End ../ext/misc/sqlite3_stdio.h ********************/
314
+/************************* Begin ../ext/misc/sqlite3_stdio.c ******************/
315
+/*
316
+** 2024-09-24
317
+**
318
+** The author disclaims copyright to this source code. In place of
319
+** a legal notice, here is a blessing:
320
+**
321
+** May you do good and not evil.
322
+** May you find forgiveness for yourself and forgive others.
323
+** May you share freely, never taking more than you give.
324
+**
325
+*************************************************************************
326
+**
327
+** Implementation of standard I/O interfaces for UTF-8 that are missing
328
+** on Windows.
329
+*/
330
+#ifdef _WIN32 /* This file is a no-op on all platforms except Windows */
331
+#ifndef _SQLITE3_STDIO_H_
332
+/* #include "sqlite3_stdio.h" */
333
+#endif
334
+#undef WIN32_LEAN_AND_MEAN
335
+#define WIN32_LEAN_AND_MEAN
336
+#include <windows.h>
337
+#include <stdlib.h>
338
+#include <string.h>
339
+#include <stdio.h>
340
+#include <assert.h>
341
+/* #include "sqlite3.h" */
342
+#include <ctype.h>
343
+#include <stdarg.h>
344
+#include <io.h>
345
+#include <fcntl.h>
346
+
347
+/*
348
+** If the SQLITE_U8TEXT_ONLY option is defined, then only use
349
+** _O_U8TEXT, _O_WTEXT, and similar together with the UTF-16
350
+** interfaces to the Windows CRT. The use of ANSI-only routines
351
+** like fputs() and ANSI modes like _O_TEXT and _O_BINARY is
352
+** avoided.
353
+**
354
+** The downside of using SQLITE_U8TEXT_ONLY is that it becomes
355
+** impossible to output a bare newline character (0x0a) - that is,
356
+** a newline that is not preceded by a carriage return (0x0d).
357
+** And without that capability, sometimes the output will be slightly
358
+** incorrect, as extra 0x0d characters will have been inserted where
359
+** they do not belong.
360
+**
361
+** The SQLITE_U8TEXT_STDIO compile-time option is a compromise.
362
+** It always enables _O_WTEXT or similar for stdin, stdout, stderr,
363
+** but allows other streams to be _O_TEXT and/or O_BINARY. The
364
+** SQLITE_U8TEXT_STDIO option has the same downside as SQLITE_U8TEXT_ONLY
365
+** in that stray 0x0d characters might appear where they ought not, but
366
+** at least with this option those characters only appear on standard
367
+** I/O streams, and not on new streams that might be created by the
368
+** application using sqlite3_fopen() or sqlite3_popen().
369
+*/
370
+#if defined(SQLITE_U8TEXT_ONLY)
371
+# define UseWtextForOutput(fd) 1
372
+# define UseWtextForInput(fd) 1
373
+# define IsConsole(fd) _isatty(_fileno(fd))
374
+#elif defined(SQLITE_U8TEXT_STDIO)
375
+# define UseWtextForOutput(fd) ((fd)==stdout || (fd)==stderr)
376
+# define UseWtextForInput(fd) ((fd)==stdin)
377
+# define IsConsole(fd) _isatty(_fileno(fd))
378
+#else
379
+# define UseWtextForOutput(fd) _isatty(_fileno(fd))
380
+# define UseWtextForInput(fd) _isatty(_fileno(fd))
381
+# define IsConsole(fd) 1
382
+#endif
383
+
384
+/*
385
+** Work-alike for the fopen() routine from the standard C library.
386
+*/
387
+FILE *sqlite3_fopen(const char *zFilename, const char *zMode){
388
+ FILE *fp = 0;
389
+ wchar_t *b1, *b2;
390
+ int sz1, sz2;
391
+
392
+ sz1 = (int)strlen(zFilename);
393
+ sz2 = (int)strlen(zMode);
394
+ b1 = malloc( (sz1+1)*sizeof(b1[0]) );
395
+ b2 = malloc( (sz2+1)*sizeof(b1[0]) );
396
+ if( b1 && b2 ){
397
+ sz1 = MultiByteToWideChar(CP_UTF8, 0, zFilename, sz1, b1, sz1);
398
+ b1[sz1] = 0;
399
+ sz2 = MultiByteToWideChar(CP_UTF8, 0, zMode, sz2, b2, sz2);
400
+ b2[sz2] = 0;
401
+ fp = _wfopen(b1, b2);
402
+ }
403
+ free(b1);
404
+ free(b2);
405
+ return fp;
406
+}
407
+
408
+
409
+/*
410
+** Work-alike for the popen() routine from the standard C library.
411
+*/
412
+FILE *sqlite3_popen(const char *zCommand, const char *zMode){
413
+ FILE *fp = 0;
414
+ wchar_t *b1, *b2;
415
+ int sz1, sz2;
416
+
417
+ sz1 = (int)strlen(zCommand);
418
+ sz2 = (int)strlen(zMode);
419
+ b1 = malloc( (sz1+1)*sizeof(b1[0]) );
420
+ b2 = malloc( (sz2+1)*sizeof(b1[0]) );
421
+ if( b1 && b2 ){
422
+ sz1 = MultiByteToWideChar(CP_UTF8, 0, zCommand, sz1, b1, sz1);
423
+ b1[sz1] = 0;
424
+ sz2 = MultiByteToWideChar(CP_UTF8, 0, zMode, sz2, b2, sz2);
425
+ b2[sz2] = 0;
426
+ fp = _wpopen(b1, b2);
427
+ }
428
+ free(b1);
429
+ free(b2);
430
+ return fp;
431
+}
432
+
433
+/*
434
+** Work-alike for fgets() from the standard C library.
435
+*/
436
+char *sqlite3_fgets(char *buf, int sz, FILE *in){
437
+ if( UseWtextForInput(in) ){
438
+ /* When reading from the command-prompt in Windows, it is necessary
439
+ ** to use _O_WTEXT input mode to read UTF-16 characters, then translate
440
+ ** that into UTF-8. Otherwise, non-ASCII characters all get translated
441
+ ** into '?'.
442
+ */
443
+ wchar_t *b1 = malloc( sz*sizeof(wchar_t) );
444
+ if( b1==0 ) return 0;
445
+ _setmode(_fileno(in), IsConsole(in) ? _O_WTEXT : _O_U8TEXT);
446
+ if( fgetws(b1, sz/4, in)==0 ){
447
+ sqlite3_free(b1);
448
+ return 0;
449
+ }
450
+ WideCharToMultiByte(CP_UTF8, 0, b1, -1, buf, sz, 0, 0);
451
+ sqlite3_free(b1);
452
+ return buf;
453
+ }else{
454
+ /* Reading from a file or other input source, just read bytes without
455
+ ** any translation. */
456
+ return fgets(buf, sz, in);
457
+ }
458
+}
459
+
460
+/*
461
+** Work-alike for fputs() from the standard C library.
462
+*/
463
+int sqlite3_fputs(const char *z, FILE *out){
464
+ if( UseWtextForOutput(out) ){
465
+ /* When writing to the command-prompt in Windows, it is necessary
466
+ ** to use _O_WTEXT input mode and write UTF-16 characters.
467
+ */
468
+ int sz = (int)strlen(z);
469
+ wchar_t *b1 = malloc( (sz+1)*sizeof(wchar_t) );
470
+ if( b1==0 ) return 0;
471
+ sz = MultiByteToWideChar(CP_UTF8, 0, z, sz, b1, sz);
472
+ b1[sz] = 0;
473
+ _setmode(_fileno(out), _O_U8TEXT);
474
+ fputws(b1, out);
475
+ sqlite3_free(b1);
476
+ return 0;
477
+ }else{
478
+ /* Writing to a file or other destination, just write bytes without
479
+ ** any translation. */
480
+ return fputs(z, out);
481
+ }
482
+}
483
+
484
+
485
+/*
486
+** Work-alike for fprintf() from the standard C library.
487
+*/
488
+int sqlite3_fprintf(FILE *out, const char *zFormat, ...){
489
+ int rc;
490
+ if( UseWtextForOutput(out) ){
491
+ /* When writing to the command-prompt in Windows, it is necessary
492
+ ** to use _O_WTEXT input mode and write UTF-16 characters.
493
+ */
494
+ char *z;
495
+ va_list ap;
496
+
497
+ va_start(ap, zFormat);
498
+ z = sqlite3_vmprintf(zFormat, ap);
499
+ va_end(ap);
500
+ sqlite3_fputs(z, out);
501
+ rc = (int)strlen(z);
502
+ sqlite3_free(z);
503
+ }else{
504
+ /* Writing to a file or other destination, just write bytes without
505
+ ** any translation. */
506
+ va_list ap;
507
+ va_start(ap, zFormat);
508
+ rc = vfprintf(out, zFormat, ap);
509
+ va_end(ap);
510
+ }
511
+ return rc;
512
+}
513
+
514
+/*
515
+** Set the mode for an output stream. mode argument is typically _O_BINARY or
516
+** _O_TEXT.
517
+*/
518
+void sqlite3_fsetmode(FILE *fp, int mode){
519
+ if( !UseWtextForOutput(fp) ){
520
+ fflush(fp);
521
+ _setmode(_fileno(fp), mode);
522
+ }
523
+}
524
+
525
+#endif /* defined(_WIN32) */
526
+
527
+/************************* End ../ext/misc/sqlite3_stdio.c ********************/
528
+
258529
/* Use console I/O package as a direct INCLUDE. */
259530
#define SQLITE_INTERNAL_LINKAGE static
260531
261532
#ifdef SQLITE_SHELL_FIDDLE
262533
/* Deselect most features from the console I/O package for Fiddle. */
@@ -264,1125 +535,13 @@
264535
# define SQLITE_CIO_NO_CLASSIFY
265536
# define SQLITE_CIO_NO_TRANSLATE
266537
# define SQLITE_CIO_NO_SETMODE
267538
# define SQLITE_CIO_NO_FLUSH
268539
#endif
269
-/************************* Begin ../ext/consio/console_io.h ******************/
270
-/*
271
-** 2023 November 1
272
-**
273
-** The author disclaims copyright to this source code. In place of
274
-** a legal notice, here is a blessing:
275
-**
276
-** May you do good and not evil.
277
-** May you find forgiveness for yourself and forgive others.
278
-** May you share freely, never taking more than you give.
279
-**
280
-********************************************************************************
281
-** This file exposes various interfaces used for console and other I/O
282
-** by the SQLite project command-line tools. These interfaces are used
283
-** at either source conglomeration time, compilation time, or run time.
284
-** This source provides for either inclusion into conglomerated,
285
-** "single-source" forms or separate compilation then linking.
286
-**
287
-** Platform dependencies are "hidden" here by various stratagems so
288
-** that, provided certain conditions are met, the programs using this
289
-** source or object code compiled from it need no explicit conditional
290
-** compilation in their source for their console and stream I/O.
291
-**
292
-** The symbols and functionality exposed here are not a public API.
293
-** This code may change in tandem with other project code as needed.
294
-**
295
-** When this .h file and its companion .c are directly incorporated into
296
-** a source conglomeration (such as shell.c), the preprocessor symbol
297
-** CIO_WIN_WC_XLATE is defined as 0 or 1, reflecting whether console I/O
298
-** translation for Windows is effected for the build.
299
-*/
300
-#define HAVE_CONSOLE_IO_H 1
301
-#ifndef SQLITE_INTERNAL_LINKAGE
302
-# define SQLITE_INTERNAL_LINKAGE extern /* external to translation unit */
303
-# include <stdio.h>
304
-#else
305
-# define SHELL_NO_SYSINC /* Better yet, modify mkshellc.tcl for this. */
306
-#endif
307
-
308
-#ifndef SQLITE3_H
309
-/* # include "sqlite3.h" */
310
-#endif
311
-
312
-#ifndef SQLITE_CIO_NO_CLASSIFY
313
-
314
-/* Define enum for use with following function. */
315
-typedef enum StreamsAreConsole {
316
- SAC_NoConsole = 0,
317
- SAC_InConsole = 1, SAC_OutConsole = 2, SAC_ErrConsole = 4,
318
- SAC_AnyConsole = 0x7
319
-} StreamsAreConsole;
320
-
321
-/*
322
-** Classify the three standard I/O streams according to whether
323
-** they are connected to a console attached to the process.
324
-**
325
-** Returns the bit-wise OR of SAC_{In,Out,Err}Console values,
326
-** or SAC_NoConsole if none of the streams reaches a console.
327
-**
328
-** This function should be called before any I/O is done with
329
-** the given streams. As a side-effect, the given inputs are
330
-** recorded so that later I/O operations on them may be done
331
-** differently than the C library FILE* I/O would be done,
332
-** iff the stream is used for the I/O functions that follow,
333
-** and to support the ones that use an implicit stream.
334
-**
335
-** On some platforms, stream or console mode alteration (aka
336
-** "Setup") may be made which is undone by consoleRestore().
337
-*/
338
-SQLITE_INTERNAL_LINKAGE StreamsAreConsole
339
-consoleClassifySetup( FILE *pfIn, FILE *pfOut, FILE *pfErr );
340
-/* A usual call for convenience: */
341
-#define SQLITE_STD_CONSOLE_INIT() consoleClassifySetup(stdin,stdout,stderr)
342
-
343
-/*
344
-** After an initial call to consoleClassifySetup(...), renew
345
-** the same setup it effected. (A call not after is an error.)
346
-** This will restore state altered by consoleRestore();
347
-**
348
-** Applications which run an inferior (child) process which
349
-** inherits the same I/O streams may call this function after
350
-** such a process exits to guard against console mode changes.
351
-*/
352
-SQLITE_INTERNAL_LINKAGE void consoleRenewSetup(void);
353
-
354
-/*
355
-** Undo any side-effects left by consoleClassifySetup(...).
356
-**
357
-** This should be called after consoleClassifySetup() and
358
-** before the process terminates normally. It is suitable
359
-** for use with the atexit() C library procedure. After
360
-** this call, no console I/O should be done until one of
361
-** console{Classify or Renew}Setup(...) is called again.
362
-**
363
-** Applications which run an inferior (child) process that
364
-** inherits the same I/O streams might call this procedure
365
-** before so that said process will have a console setup
366
-** however users have configured it or come to expect.
367
-*/
368
-SQLITE_INTERNAL_LINKAGE void SQLITE_CDECL consoleRestore( void );
369
-
370
-#else /* defined(SQLITE_CIO_NO_CLASSIFY) */
371
-# define consoleClassifySetup(i,o,e)
372
-# define consoleRenewSetup()
373
-# define consoleRestore()
374
-#endif /* defined(SQLITE_CIO_NO_CLASSIFY) */
375
-
376
-#ifndef SQLITE_CIO_NO_REDIRECT
377
-/*
378
-** Set stream to be used for the functions below which write
379
-** to "the designated X stream", where X is Output or Error.
380
-** Returns the previous value.
381
-**
382
-** Alternatively, pass the special value, invalidFileStream,
383
-** to get the designated stream value without setting it.
384
-**
385
-** Before the designated streams are set, they default to
386
-** those passed to consoleClassifySetup(...), and before
387
-** that is called they default to stdout and stderr.
388
-**
389
-** It is error to close a stream so designated, then, without
390
-** designating another, use the corresponding {o,e}Emit(...).
391
-*/
392
-SQLITE_INTERNAL_LINKAGE FILE *invalidFileStream;
393
-SQLITE_INTERNAL_LINKAGE FILE *setOutputStream(FILE *pf);
394
-# ifdef CONSIO_SET_ERROR_STREAM
395
-SQLITE_INTERNAL_LINKAGE FILE *setErrorStream(FILE *pf);
396
-# endif
397
-#else
398
-# define setOutputStream(pf)
399
-# define setErrorStream(pf)
400
-#endif /* !defined(SQLITE_CIO_NO_REDIRECT) */
401
-
402
-#ifndef SQLITE_CIO_NO_TRANSLATE
403
-/*
404
-** Emit output like fprintf(). If the output is going to the
405
-** console and translation from UTF-8 is necessary, perform
406
-** the needed translation. Otherwise, write formatted output
407
-** to the provided stream almost as-is, possibly with newline
408
-** translation as specified by set{Binary,Text}Mode().
409
-*/
410
-SQLITE_INTERNAL_LINKAGE int fPrintfUtf8(FILE *pfO, const char *zFormat, ...);
411
-/* Like fPrintfUtf8 except stream is always the designated output. */
412
-SQLITE_INTERNAL_LINKAGE int oPrintfUtf8(const char *zFormat, ...);
413
-/* Like fPrintfUtf8 except stream is always the designated error. */
414
-SQLITE_INTERNAL_LINKAGE int ePrintfUtf8(const char *zFormat, ...);
415
-
416
-/*
417
-** Emit output like fputs(). If the output is going to the
418
-** console and translation from UTF-8 is necessary, perform
419
-** the needed translation. Otherwise, write given text to the
420
-** provided stream almost as-is, possibly with newline
421
-** translation as specified by set{Binary,Text}Mode().
422
-*/
423
-SQLITE_INTERNAL_LINKAGE int fPutsUtf8(const char *z, FILE *pfO);
424
-/* Like fPutsUtf8 except stream is always the designated output. */
425
-SQLITE_INTERNAL_LINKAGE int oPutsUtf8(const char *z);
426
-/* Like fPutsUtf8 except stream is always the designated error. */
427
-SQLITE_INTERNAL_LINKAGE int ePutsUtf8(const char *z);
428
-
429
-/*
430
-** Emit output like fPutsUtf8(), except that the length of the
431
-** accepted char or character sequence is limited by nAccept.
432
-**
433
-** Returns the number of accepted char values.
434
-*/
435
-#ifdef CONSIO_SPUTB
436
-SQLITE_INTERNAL_LINKAGE int
437
-fPutbUtf8(FILE *pfOut, const char *cBuf, int nAccept);
438
-/* Like fPutbUtf8 except stream is always the designated output. */
439
-#endif
440
-SQLITE_INTERNAL_LINKAGE int
441
-oPutbUtf8(const char *cBuf, int nAccept);
442
-/* Like fPutbUtf8 except stream is always the designated error. */
443
-#ifdef CONSIO_EPUTB
444
-SQLITE_INTERNAL_LINKAGE int
445
-ePutbUtf8(const char *cBuf, int nAccept);
446
-#endif
447
-
448
-/*
449
-** Flush the given output stream. Return non-zero for success, else 0.
450
-*/
451
-#if !defined(SQLITE_CIO_NO_FLUSH) && !defined(SQLITE_CIO_NO_SETMODE)
452
-SQLITE_INTERNAL_LINKAGE int
453
-fFlushBuffer(FILE *pfOut);
454
-#endif
455
-
456
-/*
457
-** Collect input like fgets(...) with special provisions for input
458
-** from the console on such platforms as require same. Newline
459
-** translation may be done as set by set{Binary,Text}Mode().
460
-** As a convenience, pfIn==NULL is treated as stdin.
461
-*/
462
-SQLITE_INTERNAL_LINKAGE char* fGetsUtf8(char *cBuf, int ncMax, FILE *pfIn);
463
-/* Like fGetsUtf8 except stream is always the designated input. */
464
-/* SQLITE_INTERNAL_LINKAGE char* iGetsUtf8(char *cBuf, int ncMax); */
465
-
466
-#endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
467
-
468
-#ifndef SQLITE_CIO_NO_SETMODE
469
-/*
470
-** Set given stream for binary mode, where newline translation is
471
-** not done, or for text mode where, for some platforms, newlines
472
-** are translated to the platform's conventional char sequence.
473
-** If bFlush true, flush the stream.
474
-**
475
-** An additional side-effect is that if the stream is one passed
476
-** to consoleClassifySetup() as an output, it is flushed first.
477
-**
478
-** Note that binary/text mode has no effect on console I/O
479
-** translation. On all platforms, newline to the console starts
480
-** a new line and CR,LF chars from the console become a newline.
481
-*/
482
-SQLITE_INTERNAL_LINKAGE void setBinaryMode(FILE *, short bFlush);
483
-SQLITE_INTERNAL_LINKAGE void setTextMode(FILE *, short bFlush);
484
-#endif
485
-
486
-#ifdef SQLITE_CIO_PROMPTED_IN
487
-typedef struct Prompts {
488
- int numPrompts;
489
- const char **azPrompts;
490
-} Prompts;
491
-
492
-/*
493
-** Macros for use of a line editor.
494
-**
495
-** The following macros define operations involving use of a
496
-** line-editing library or simple console interaction.
497
-** A "T" argument is a text (char *) buffer or filename.
498
-** A "N" argument is an integer.
499
-**
500
-** SHELL_ADD_HISTORY(T) // Record text as line(s) of history.
501
-** SHELL_READ_HISTORY(T) // Read history from file named by T.
502
-** SHELL_WRITE_HISTORY(T) // Write history to file named by T.
503
-** SHELL_STIFLE_HISTORY(N) // Limit history to N entries.
504
-**
505
-** A console program which does interactive console input is
506
-** expected to call:
507
-** SHELL_READ_HISTORY(T) before collecting such input;
508
-** SHELL_ADD_HISTORY(T) as record-worthy input is taken;
509
-** SHELL_STIFLE_HISTORY(N) after console input ceases; then
510
-** SHELL_WRITE_HISTORY(T) before the program exits.
511
-*/
512
-
513
-/*
514
-** Retrieve a single line of input text from an input stream.
515
-**
516
-** If pfIn is the input stream passed to consoleClassifySetup(),
517
-** and azPrompt is not NULL, then a prompt is issued before the
518
-** line is collected, as selected by the isContinuation flag.
519
-** Array azPrompt[{0,1}] holds the {main,continuation} prompt.
520
-**
521
-** If zBufPrior is not NULL then it is a buffer from a prior
522
-** call to this routine that can be reused, or will be freed.
523
-**
524
-** The result is stored in space obtained from malloc() and
525
-** must either be freed by the caller or else passed back to
526
-** this function as zBufPrior for reuse.
527
-**
528
-** This function may call upon services of a line-editing
529
-** library to interactively collect line edited input.
530
-*/
531
-SQLITE_INTERNAL_LINKAGE char *
532
-shellGetLine(FILE *pfIn, char *zBufPrior, int nLen,
533
- short isContinuation, Prompts azPrompt);
534
-#endif /* defined(SQLITE_CIO_PROMPTED_IN) */
535
-/*
536
-** TBD: Define an interface for application(s) to generate
537
-** completion candidates for use by the line-editor.
538
-**
539
-** This may be premature; the CLI is the only application
540
-** that does this. Yet, getting line-editing melded into
541
-** console I/O is desirable because a line-editing library
542
-** may have to establish console operating mode, possibly
543
-** in a way that interferes with the above functionality.
544
-*/
545
-
546
-#if !(defined(SQLITE_CIO_NO_UTF8SCAN)&&defined(SQLITE_CIO_NO_TRANSLATE))
547
-/* Skip over as much z[] input char sequence as is valid UTF-8,
548
-** limited per nAccept char's or whole characters and containing
549
-** no char cn such that ((1<<cn) & ccm)!=0. On return, the
550
-** sequence z:return (inclusive:exclusive) is validated UTF-8.
551
-** Limit: nAccept>=0 => char count, nAccept<0 => character
552
- */
553
-SQLITE_INTERNAL_LINKAGE const char*
554
-zSkipValidUtf8(const char *z, int nAccept, long ccm);
555
-
556
-#endif
557
-
558
-/************************* End ../ext/consio/console_io.h ********************/
559
-/************************* Begin ../ext/consio/console_io.c ******************/
560
-/*
561
-** 2023 November 4
562
-**
563
-** The author disclaims copyright to this source code. In place of
564
-** a legal notice, here is a blessing:
565
-**
566
-** May you do good and not evil.
567
-** May you find forgiveness for yourself and forgive others.
568
-** May you share freely, never taking more than you give.
569
-**
570
-********************************************************************************
571
-** This file implements various interfaces used for console and stream I/O
572
-** by the SQLite project command-line tools, as explained in console_io.h .
573
-** Functions prefixed by "SQLITE_INTERNAL_LINKAGE" behave as described there.
574
-*/
575
-
576
-#ifndef SQLITE_CDECL
577
-# define SQLITE_CDECL
578
-#endif
579
-
580
-#ifndef SHELL_NO_SYSINC
581
-# include <stdarg.h>
582
-# include <string.h>
583
-# include <stdlib.h>
584
-# include <limits.h>
585
-# include <assert.h>
586
-/* # include "sqlite3.h" */
587
-#endif
588
-#ifndef HAVE_CONSOLE_IO_H
589
-# include "console_io.h"
590
-#endif
591
-#if defined(_MSC_VER)
592
-# pragma warning(disable : 4204)
593
-#endif
594
-
595
-#ifndef SQLITE_CIO_NO_TRANSLATE
596
-# if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
597
-# ifndef SHELL_NO_SYSINC
598
-# include <io.h>
599
-# include <fcntl.h>
600
-# undef WIN32_LEAN_AND_MEAN
601
-# define WIN32_LEAN_AND_MEAN
602
-# include <windows.h>
603
-# endif
604
-# define CIO_WIN_WC_XLATE 1 /* Use WCHAR Windows APIs for console I/O */
605
-# else
606
-# ifndef SHELL_NO_SYSINC
607
-# include <unistd.h>
608
-# endif
609
-# define CIO_WIN_WC_XLATE 0 /* Use plain C library stream I/O at console */
610
-# endif
611
-#else
612
-# define CIO_WIN_WC_XLATE 0 /* Not exposing translation routines at all */
613
-#endif
614
-
615
-#if CIO_WIN_WC_XLATE
616
-static HANDLE handleOfFile(FILE *pf){
617
- int fileDesc = _fileno(pf);
618
- union { intptr_t osfh; HANDLE fh; } fid = {
619
- (fileDesc>=0)? _get_osfhandle(fileDesc) : (intptr_t)INVALID_HANDLE_VALUE
620
- };
621
- return fid.fh;
622
-}
623
-#endif
624
-
625
-#ifndef SQLITE_CIO_NO_TRANSLATE
626
-typedef struct PerStreamTags {
627
-# if CIO_WIN_WC_XLATE
628
- HANDLE hx;
629
- DWORD consMode;
630
- char acIncomplete[4];
631
-# else
632
- short reachesConsole;
633
-# endif
634
- FILE *pf;
635
-} PerStreamTags;
636
-
637
-/* Define NULL-like value for things which can validly be 0. */
638
-# define SHELL_INVALID_FILE_PTR ((FILE *)~0)
639
-# if CIO_WIN_WC_XLATE
640
-# define SHELL_INVALID_CONS_MODE 0xFFFF0000
641
-# endif
642
-
643
-# if CIO_WIN_WC_XLATE
644
-# define PST_INITIALIZER { INVALID_HANDLE_VALUE, SHELL_INVALID_CONS_MODE, \
645
- {0,0,0,0}, SHELL_INVALID_FILE_PTR }
646
-# else
647
-# define PST_INITIALIZER { 0, SHELL_INVALID_FILE_PTR }
648
-# endif
649
-
650
-/* Quickly say whether a known output is going to the console. */
651
-# if CIO_WIN_WC_XLATE
652
-static short pstReachesConsole(PerStreamTags *ppst){
653
- return (ppst->hx != INVALID_HANDLE_VALUE);
654
-}
655
-# else
656
-# define pstReachesConsole(ppst) 0
657
-# endif
658
-
659
-# if CIO_WIN_WC_XLATE
660
-static void restoreConsoleArb(PerStreamTags *ppst){
661
- if( pstReachesConsole(ppst) ) SetConsoleMode(ppst->hx, ppst->consMode);
662
-}
663
-# else
664
-# define restoreConsoleArb(ppst)
665
-# endif
666
-
667
-/* Say whether FILE* appears to be a console, collect associated info. */
668
-static short streamOfConsole(FILE *pf, /* out */ PerStreamTags *ppst){
669
-# if CIO_WIN_WC_XLATE
670
- short rv = 0;
671
- DWORD dwCM = SHELL_INVALID_CONS_MODE;
672
- HANDLE fh = handleOfFile(pf);
673
- ppst->pf = pf;
674
- if( INVALID_HANDLE_VALUE != fh ){
675
- rv = (GetFileType(fh) == FILE_TYPE_CHAR && GetConsoleMode(fh,&dwCM));
676
- }
677
- ppst->hx = (rv)? fh : INVALID_HANDLE_VALUE;
678
- ppst->consMode = dwCM;
679
- return rv;
680
-# else
681
- ppst->pf = pf;
682
- ppst->reachesConsole = ( (short)isatty(fileno(pf)) );
683
- return ppst->reachesConsole;
684
-# endif
685
-}
686
-
687
-# ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
688
-# define ENABLE_VIRTUAL_TERMINAL_PROCESSING (0x4)
689
-# endif
690
-
691
-# if CIO_WIN_WC_XLATE
692
-/* Define console modes for use with the Windows Console API. */
693
-# define SHELL_CONI_MODE \
694
- (ENABLE_ECHO_INPUT | ENABLE_INSERT_MODE | ENABLE_LINE_INPUT | 0x80 \
695
- | ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS | ENABLE_PROCESSED_INPUT)
696
-# define SHELL_CONO_MODE (ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT \
697
- | ENABLE_VIRTUAL_TERMINAL_PROCESSING)
698
-# endif
699
-
700
-typedef struct ConsoleInfo {
701
- PerStreamTags pstSetup[3];
702
- PerStreamTags pstDesignated[3];
703
- StreamsAreConsole sacSetup;
704
-} ConsoleInfo;
705
-
706
-static short isValidStreamInfo(PerStreamTags *ppst){
707
- return (ppst->pf != SHELL_INVALID_FILE_PTR);
708
-}
709
-
710
-static ConsoleInfo consoleInfo = {
711
- { /* pstSetup */ PST_INITIALIZER, PST_INITIALIZER, PST_INITIALIZER },
712
- { /* pstDesignated[] */ PST_INITIALIZER, PST_INITIALIZER, PST_INITIALIZER },
713
- SAC_NoConsole /* sacSetup */
714
-};
715
-
716
-SQLITE_INTERNAL_LINKAGE FILE* invalidFileStream = (FILE *)~0;
717
-
718
-# if CIO_WIN_WC_XLATE
719
-static void maybeSetupAsConsole(PerStreamTags *ppst, short odir){
720
- if( pstReachesConsole(ppst) ){
721
- DWORD cm = odir? SHELL_CONO_MODE : SHELL_CONI_MODE;
722
- SetConsoleMode(ppst->hx, cm);
723
- }
724
-}
725
-# else
726
-# define maybeSetupAsConsole(ppst,odir)
727
-# endif
728
-
729
-SQLITE_INTERNAL_LINKAGE void consoleRenewSetup(void){
730
-# if CIO_WIN_WC_XLATE
731
- int ix = 0;
732
- while( ix < 6 ){
733
- PerStreamTags *ppst = (ix<3)?
734
- &consoleInfo.pstSetup[ix] : &consoleInfo.pstDesignated[ix-3];
735
- maybeSetupAsConsole(ppst, (ix % 3)>0);
736
- ++ix;
737
- }
738
-# endif
739
-}
740
-
741
-SQLITE_INTERNAL_LINKAGE StreamsAreConsole
742
-consoleClassifySetup( FILE *pfIn, FILE *pfOut, FILE *pfErr ){
743
- StreamsAreConsole rv = SAC_NoConsole;
744
- FILE* apf[3] = { pfIn, pfOut, pfErr };
745
- int ix;
746
- for( ix = 2; ix >= 0; --ix ){
747
- PerStreamTags *ppst = &consoleInfo.pstSetup[ix];
748
- if( streamOfConsole(apf[ix], ppst) ){
749
- rv |= (SAC_InConsole<<ix);
750
- }
751
- consoleInfo.pstDesignated[ix] = *ppst;
752
- if( ix > 0 ) fflush(apf[ix]);
753
- }
754
- consoleInfo.sacSetup = rv;
755
- consoleRenewSetup();
756
- return rv;
757
-}
758
-
759
-SQLITE_INTERNAL_LINKAGE void SQLITE_CDECL consoleRestore( void ){
760
-# if CIO_WIN_WC_XLATE
761
- static ConsoleInfo *pci = &consoleInfo;
762
- if( pci->sacSetup ){
763
- int ix;
764
- for( ix=0; ix<3; ++ix ){
765
- if( pci->sacSetup & (SAC_InConsole<<ix) ){
766
- PerStreamTags *ppst = &pci->pstSetup[ix];
767
- SetConsoleMode(ppst->hx, ppst->consMode);
768
- }
769
- }
770
- }
771
-# endif
772
-}
773
-#endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
774
-
775
-#ifdef SQLITE_CIO_INPUT_REDIR
776
-/* Say whether given FILE* is among those known, via either
777
-** consoleClassifySetup() or set{Output,Error}Stream, as
778
-** readable, and return an associated PerStreamTags pointer
779
-** if so. Otherwise, return 0.
780
-*/
781
-static PerStreamTags * isKnownReadable(FILE *pf){
782
- static PerStreamTags *apst[] = {
783
- &consoleInfo.pstDesignated[0], &consoleInfo.pstSetup[0], 0
784
- };
785
- int ix = 0;
786
- do {
787
- if( apst[ix]->pf == pf ) break;
788
- } while( apst[++ix] != 0 );
789
- return apst[ix];
790
-}
791
-#endif
792
-
793
-#ifndef SQLITE_CIO_NO_TRANSLATE
794
-/* Say whether given FILE* is among those known, via either
795
-** consoleClassifySetup() or set{Output,Error}Stream, as
796
-** writable, and return an associated PerStreamTags pointer
797
-** if so. Otherwise, return 0.
798
-*/
799
-static PerStreamTags * isKnownWritable(FILE *pf){
800
- static PerStreamTags *apst[] = {
801
- &consoleInfo.pstDesignated[1], &consoleInfo.pstDesignated[2],
802
- &consoleInfo.pstSetup[1], &consoleInfo.pstSetup[2], 0
803
- };
804
- int ix = 0;
805
- do {
806
- if( apst[ix]->pf == pf ) break;
807
- } while( apst[++ix] != 0 );
808
- return apst[ix];
809
-}
810
-
811
-static FILE *designateEmitStream(FILE *pf, unsigned chix){
812
- FILE *rv = consoleInfo.pstDesignated[chix].pf;
813
- if( pf == invalidFileStream ) return rv;
814
- else{
815
- /* Setting a possibly new output stream. */
816
- PerStreamTags *ppst = isKnownWritable(pf);
817
- if( ppst != 0 ){
818
- PerStreamTags pst = *ppst;
819
- consoleInfo.pstDesignated[chix] = pst;
820
- }else streamOfConsole(pf, &consoleInfo.pstDesignated[chix]);
821
- }
822
- return rv;
823
-}
824
-
825
-SQLITE_INTERNAL_LINKAGE FILE *setOutputStream(FILE *pf){
826
- return designateEmitStream(pf, 1);
827
-}
828
-# ifdef CONSIO_SET_ERROR_STREAM
829
-SQLITE_INTERNAL_LINKAGE FILE *setErrorStream(FILE *pf){
830
- return designateEmitStream(pf, 2);
831
-}
832
-# endif
833
-#endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
834
-
835
-#ifndef SQLITE_CIO_NO_SETMODE
836
-# if CIO_WIN_WC_XLATE
837
-static void setModeFlushQ(FILE *pf, short bFlush, int mode){
838
- if( bFlush ) fflush(pf);
839
- _setmode(_fileno(pf), mode);
840
-}
841
-# else
842
-# define setModeFlushQ(f, b, m) if(b) fflush(f)
843
-# endif
844
-
845
-SQLITE_INTERNAL_LINKAGE void setBinaryMode(FILE *pf, short bFlush){
846
- setModeFlushQ(pf, bFlush, _O_BINARY);
847
-}
848
-SQLITE_INTERNAL_LINKAGE void setTextMode(FILE *pf, short bFlush){
849
- setModeFlushQ(pf, bFlush, _O_TEXT);
850
-}
851
-# undef setModeFlushQ
852
-
853
-#else /* defined(SQLITE_CIO_NO_SETMODE) */
854
-# define setBinaryMode(f, bFlush) do{ if((bFlush)) fflush(f); }while(0)
855
-# define setTextMode(f, bFlush) do{ if((bFlush)) fflush(f); }while(0)
856
-#endif /* defined(SQLITE_CIO_NO_SETMODE) */
857
-
858
-#ifndef SQLITE_CIO_NO_TRANSLATE
859
-# if CIO_WIN_WC_XLATE
860
-/* Write buffer cBuf as output to stream known to reach console,
861
-** limited to ncTake char's. Return ncTake on success, else 0. */
862
-static int conZstrEmit(PerStreamTags *ppst, const char *z, int ncTake){
863
- int rv = 0;
864
- if( z!=NULL ){
865
- int nwc = MultiByteToWideChar(CP_UTF8,0, z,ncTake, 0,0);
866
- if( nwc > 0 ){
867
- WCHAR *zw = sqlite3_malloc64(nwc*sizeof(WCHAR));
868
- if( zw!=NULL ){
869
- nwc = MultiByteToWideChar(CP_UTF8,0, z,ncTake, zw,nwc);
870
- if( nwc > 0 ){
871
- /* Translation from UTF-8 to UTF-16, then WCHARs out. */
872
- if( WriteConsoleW(ppst->hx, zw,nwc, 0, NULL) ){
873
- rv = ncTake;
874
- }
875
- }
876
- sqlite3_free(zw);
877
- }
878
- }
879
- }
880
- return rv;
881
-}
882
-
883
-/* For {f,o,e}PrintfUtf8() when stream is known to reach console. */
884
-static int conioVmPrintf(PerStreamTags *ppst, const char *zFormat, va_list ap){
885
- char *z = sqlite3_vmprintf(zFormat, ap);
886
- if( z ){
887
- int rv = conZstrEmit(ppst, z, (int)strlen(z));
888
- sqlite3_free(z);
889
- return rv;
890
- }else return 0;
891
-}
892
-# endif /* CIO_WIN_WC_XLATE */
893
-
894
-# ifdef CONSIO_GET_EMIT_STREAM
895
-static PerStreamTags * getDesignatedEmitStream(FILE *pf, unsigned chix,
896
- PerStreamTags *ppst){
897
- PerStreamTags *rv = isKnownWritable(pf);
898
- short isValid = (rv!=0)? isValidStreamInfo(rv) : 0;
899
- if( rv != 0 && isValid ) return rv;
900
- streamOfConsole(pf, ppst);
901
- return ppst;
902
-}
903
-# endif
904
-
905
-/* Get stream info, either for designated output or error stream when
906
-** chix equals 1 or 2, or for an arbitrary stream when chix == 0.
907
-** In either case, ppst references a caller-owned PerStreamTags
908
-** struct which may be filled in if none of the known writable
909
-** streams is being held by consoleInfo. The ppf parameter is a
910
-** byref output when chix!=0 and a byref input when chix==0.
911
- */
912
-static PerStreamTags *
913
-getEmitStreamInfo(unsigned chix, PerStreamTags *ppst,
914
- /* in/out */ FILE **ppf){
915
- PerStreamTags *ppstTry;
916
- FILE *pfEmit;
917
- if( chix > 0 ){
918
- ppstTry = &consoleInfo.pstDesignated[chix];
919
- if( !isValidStreamInfo(ppstTry) ){
920
- ppstTry = &consoleInfo.pstSetup[chix];
921
- pfEmit = ppst->pf;
922
- }else pfEmit = ppstTry->pf;
923
- if( !isValidStreamInfo(ppstTry) ){
924
- pfEmit = (chix > 1)? stderr : stdout;
925
- ppstTry = ppst;
926
- streamOfConsole(pfEmit, ppstTry);
927
- }
928
- *ppf = pfEmit;
929
- }else{
930
- ppstTry = isKnownWritable(*ppf);
931
- if( ppstTry != 0 ) return ppstTry;
932
- streamOfConsole(*ppf, ppst);
933
- return ppst;
934
- }
935
- return ppstTry;
936
-}
937
-
938
-SQLITE_INTERNAL_LINKAGE int oPrintfUtf8(const char *zFormat, ...){
939
- va_list ap;
940
- int rv;
941
- FILE *pfOut;
942
- PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
943
-# if CIO_WIN_WC_XLATE
944
- PerStreamTags *ppst = getEmitStreamInfo(1, &pst, &pfOut);
945
-# else
946
- getEmitStreamInfo(1, &pst, &pfOut);
947
-# endif
948
- assert(zFormat!=0);
949
- va_start(ap, zFormat);
950
-# if CIO_WIN_WC_XLATE
951
- if( pstReachesConsole(ppst) ){
952
- rv = conioVmPrintf(ppst, zFormat, ap);
953
- }else{
954
-# endif
955
- rv = vfprintf(pfOut, zFormat, ap);
956
-# if CIO_WIN_WC_XLATE
957
- }
958
-# endif
959
- va_end(ap);
960
- return rv;
961
-}
962
-
963
-SQLITE_INTERNAL_LINKAGE int ePrintfUtf8(const char *zFormat, ...){
964
- va_list ap;
965
- int rv;
966
- FILE *pfErr;
967
- PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
968
-# if CIO_WIN_WC_XLATE
969
- PerStreamTags *ppst = getEmitStreamInfo(2, &pst, &pfErr);
970
-# else
971
- getEmitStreamInfo(2, &pst, &pfErr);
972
-# endif
973
- assert(zFormat!=0);
974
- va_start(ap, zFormat);
975
-# if CIO_WIN_WC_XLATE
976
- if( pstReachesConsole(ppst) ){
977
- rv = conioVmPrintf(ppst, zFormat, ap);
978
- }else{
979
-# endif
980
- rv = vfprintf(pfErr, zFormat, ap);
981
-# if CIO_WIN_WC_XLATE
982
- }
983
-# endif
984
- va_end(ap);
985
- return rv;
986
-}
987
-
988
-SQLITE_INTERNAL_LINKAGE int fPrintfUtf8(FILE *pfO, const char *zFormat, ...){
989
- va_list ap;
990
- int rv;
991
- PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
992
-# if CIO_WIN_WC_XLATE
993
- PerStreamTags *ppst = getEmitStreamInfo(0, &pst, &pfO);
994
-# else
995
- getEmitStreamInfo(0, &pst, &pfO);
996
-# endif
997
- assert(zFormat!=0);
998
- va_start(ap, zFormat);
999
-# if CIO_WIN_WC_XLATE
1000
- if( pstReachesConsole(ppst) ){
1001
- maybeSetupAsConsole(ppst, 1);
1002
- rv = conioVmPrintf(ppst, zFormat, ap);
1003
- if( 0 == isKnownWritable(ppst->pf) ) restoreConsoleArb(ppst);
1004
- }else{
1005
-# endif
1006
- rv = vfprintf(pfO, zFormat, ap);
1007
-# if CIO_WIN_WC_XLATE
1008
- }
1009
-# endif
1010
- va_end(ap);
1011
- return rv;
1012
-}
1013
-
1014
-SQLITE_INTERNAL_LINKAGE int fPutsUtf8(const char *z, FILE *pfO){
1015
- PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1016
-# if CIO_WIN_WC_XLATE
1017
- PerStreamTags *ppst = getEmitStreamInfo(0, &pst, &pfO);
1018
-# else
1019
- getEmitStreamInfo(0, &pst, &pfO);
1020
-# endif
1021
- assert(z!=0);
1022
-# if CIO_WIN_WC_XLATE
1023
- if( pstReachesConsole(ppst) ){
1024
- int rv;
1025
- maybeSetupAsConsole(ppst, 1);
1026
- rv = conZstrEmit(ppst, z, (int)strlen(z));
1027
- if( 0 == isKnownWritable(ppst->pf) ) restoreConsoleArb(ppst);
1028
- return rv;
1029
- }else {
1030
-# endif
1031
- return (fputs(z, pfO)<0)? 0 : (int)strlen(z);
1032
-# if CIO_WIN_WC_XLATE
1033
- }
1034
-# endif
1035
-}
1036
-
1037
-SQLITE_INTERNAL_LINKAGE int ePutsUtf8(const char *z){
1038
- FILE *pfErr;
1039
- PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1040
-# if CIO_WIN_WC_XLATE
1041
- PerStreamTags *ppst = getEmitStreamInfo(2, &pst, &pfErr);
1042
-# else
1043
- getEmitStreamInfo(2, &pst, &pfErr);
1044
-# endif
1045
- assert(z!=0);
1046
-# if CIO_WIN_WC_XLATE
1047
- if( pstReachesConsole(ppst) ) return conZstrEmit(ppst, z, (int)strlen(z));
1048
- else {
1049
-# endif
1050
- return (fputs(z, pfErr)<0)? 0 : (int)strlen(z);
1051
-# if CIO_WIN_WC_XLATE
1052
- }
1053
-# endif
1054
-}
1055
-
1056
-SQLITE_INTERNAL_LINKAGE int oPutsUtf8(const char *z){
1057
- FILE *pfOut;
1058
- PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1059
-# if CIO_WIN_WC_XLATE
1060
- PerStreamTags *ppst = getEmitStreamInfo(1, &pst, &pfOut);
1061
-# else
1062
- getEmitStreamInfo(1, &pst, &pfOut);
1063
-# endif
1064
- assert(z!=0);
1065
-# if CIO_WIN_WC_XLATE
1066
- if( pstReachesConsole(ppst) ) return conZstrEmit(ppst, z, (int)strlen(z));
1067
- else {
1068
-# endif
1069
- return (fputs(z, pfOut)<0)? 0 : (int)strlen(z);
1070
-# if CIO_WIN_WC_XLATE
1071
- }
1072
-# endif
1073
-}
1074
-
1075
-#endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
1076
-
1077
-#if !(defined(SQLITE_CIO_NO_UTF8SCAN) && defined(SQLITE_CIO_NO_TRANSLATE))
1078
-/* Skip over as much z[] input char sequence as is valid UTF-8,
1079
-** limited per nAccept char's or whole characters and containing
1080
-** no char cn such that ((1<<cn) & ccm)!=0. On return, the
1081
-** sequence z:return (inclusive:exclusive) is validated UTF-8.
1082
-** Limit: nAccept>=0 => char count, nAccept<0 => character
1083
- */
1084
-SQLITE_INTERNAL_LINKAGE const char*
1085
-zSkipValidUtf8(const char *z, int nAccept, long ccm){
1086
- int ng = (nAccept<0)? -nAccept : 0;
1087
- const char *pcLimit = (nAccept>=0)? z+nAccept : 0;
1088
- assert(z!=0);
1089
- while( (pcLimit)? (z<pcLimit) : (ng-- != 0) ){
1090
- char c = *z;
1091
- if( (c & 0x80) == 0 ){
1092
- if( ccm != 0L && c < 0x20 && ((1L<<c) & ccm) != 0 ) return z;
1093
- ++z; /* ASCII */
1094
- }else if( (c & 0xC0) != 0xC0 ) return z; /* not a lead byte */
1095
- else{
1096
- const char *zt = z+1; /* Got lead byte, look at trail bytes.*/
1097
- do{
1098
- if( pcLimit && zt >= pcLimit ) return z;
1099
- else{
1100
- char ct = *zt++;
1101
- if( ct==0 || (zt-z)>4 || (ct & 0xC0)!=0x80 ){
1102
- /* Trailing bytes are too few, too many, or invalid. */
1103
- return z;
1104
- }
1105
- }
1106
- } while( ((c <<= 1) & 0x40) == 0x40 ); /* Eat lead byte's count. */
1107
- z = zt;
1108
- }
1109
- }
1110
- return z;
1111
-}
1112
-#endif /*!(defined(SQLITE_CIO_NO_UTF8SCAN)&&defined(SQLITE_CIO_NO_TRANSLATE))*/
1113
-
1114
-#ifndef SQLITE_CIO_NO_TRANSLATE
1115
-# ifdef CONSIO_SPUTB
1116
-SQLITE_INTERNAL_LINKAGE int
1117
-fPutbUtf8(FILE *pfO, const char *cBuf, int nAccept){
1118
- assert(pfO!=0);
1119
-# if CIO_WIN_WC_XLATE
1120
- PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1121
- PerStreamTags *ppst = getEmitStreamInfo(0, &pst, &pfO);
1122
- if( pstReachesConsole(ppst) ){
1123
- int rv;
1124
- maybeSetupAsConsole(ppst, 1);
1125
- rv = conZstrEmit(ppst, cBuf, nAccept);
1126
- if( 0 == isKnownWritable(ppst->pf) ) restoreConsoleArb(ppst);
1127
- return rv;
1128
- }else {
1129
-# endif
1130
- return (int)fwrite(cBuf, 1, nAccept, pfO);
1131
-# if CIO_WIN_WC_XLATE
1132
- }
1133
-# endif
1134
-}
1135
-# endif
1136
-
1137
-SQLITE_INTERNAL_LINKAGE int
1138
-oPutbUtf8(const char *cBuf, int nAccept){
1139
- FILE *pfOut;
1140
- PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1141
-# if CIO_WIN_WC_XLATE
1142
- PerStreamTags *ppst = getEmitStreamInfo(1, &pst, &pfOut);
1143
-# else
1144
- getEmitStreamInfo(1, &pst, &pfOut);
1145
-# endif
1146
-# if CIO_WIN_WC_XLATE
1147
- if( pstReachesConsole(ppst) ){
1148
- return conZstrEmit(ppst, cBuf, nAccept);
1149
- }else {
1150
-# endif
1151
- return (int)fwrite(cBuf, 1, nAccept, pfOut);
1152
-# if CIO_WIN_WC_XLATE
1153
- }
1154
-# endif
1155
-}
1156
-
1157
-/*
1158
-** Flush the given output stream. Return non-zero for success, else 0.
1159
-*/
1160
-#if !defined(SQLITE_CIO_NO_FLUSH) && !defined(SQLITE_CIO_NO_SETMODE)
1161
-SQLITE_INTERNAL_LINKAGE int
1162
-fFlushBuffer(FILE *pfOut){
1163
-# if CIO_WIN_WC_XLATE && !defined(SHELL_OMIT_FIO_DUPE)
1164
- return FlushFileBuffers(handleOfFile(pfOut))? 1 : 0;
1165
-# else
1166
- return fflush(pfOut);
1167
-# endif
1168
-}
1169
-#endif
1170
-
1171
-#if CIO_WIN_WC_XLATE \
1172
- && !defined(SHELL_OMIT_FIO_DUPE) \
1173
- && defined(SQLITE_USE_ONLY_WIN32)
1174
-static struct FileAltIds {
1175
- int fd;
1176
- HANDLE fh;
1177
-} altIdsOfFile(FILE *pf){
1178
- struct FileAltIds rv = { _fileno(pf) };
1179
- union { intptr_t osfh; HANDLE fh; } fid = {
1180
- (rv.fd>=0)? _get_osfhandle(rv.fd) : (intptr_t)INVALID_HANDLE_VALUE
1181
- };
1182
- rv.fh = fid.fh;
1183
- return rv;
1184
-}
1185
-
1186
-SQLITE_INTERNAL_LINKAGE size_t
1187
-cfWrite(const void *buf, size_t osz, size_t ocnt, FILE *pf){
1188
- size_t rv = 0;
1189
- struct FileAltIds fai = altIdsOfFile(pf);
1190
- int fmode = _setmode(fai.fd, _O_BINARY);
1191
- _setmode(fai.fd, fmode);
1192
- while( rv < ocnt ){
1193
- size_t nbo = osz;
1194
- while( nbo > 0 ){
1195
- DWORD dwno = (nbo>(1L<<24))? 1L<<24 : (DWORD)nbo;
1196
- BOOL wrc = TRUE;
1197
- BOOL genCR = (fmode & _O_TEXT)!=0;
1198
- if( genCR ){
1199
- const char *pnl = (const char*)memchr(buf, '\n', nbo);
1200
- if( pnl ) nbo = pnl - (const char*)buf;
1201
- else genCR = 0;
1202
- }
1203
- if( dwno>0 ) wrc = WriteFile(fai.fh, buf, dwno, 0,0);
1204
- if( genCR && wrc ){
1205
- wrc = WriteFile(fai.fh, "\r\n", 2, 0,0);
1206
- ++dwno; /* Skip over the LF */
1207
- }
1208
- if( !wrc ) return rv;
1209
- buf = (const char*)buf + dwno;
1210
- nbo += dwno;
1211
- }
1212
- ++rv;
1213
- }
1214
- return rv;
1215
-}
1216
-
1217
-/* An fgets() equivalent, using Win32 file API for actual input.
1218
-** Input ends when given buffer is filled or a newline is read.
1219
-** If the FILE object is in text mode, swallows 0x0D. (ASCII CR)
1220
-*/
1221
-SQLITE_INTERNAL_LINKAGE char *
1222
-cfGets(char *cBuf, int n, FILE *pf){
1223
- int nci = 0;
1224
- struct FileAltIds fai = altIdsOfFile(pf);
1225
- int fmode = _setmode(fai.fd, _O_BINARY);
1226
- BOOL eatCR = (fmode & _O_TEXT)!=0;
1227
- _setmode(fai.fd, fmode);
1228
- while( nci < n-1 ){
1229
- DWORD nr;
1230
- if( !ReadFile(fai.fh, cBuf+nci, 1, &nr, 0) || nr==0 ) break;
1231
- if( nr>0 && (!eatCR || cBuf[nci]!='\r') ){
1232
- nci += nr;
1233
- if( cBuf[nci-nr]=='\n' ) break;
1234
- }
1235
- }
1236
- if( nci < n ) cBuf[nci] = 0;
1237
- return (nci>0)? cBuf : 0;
1238
-}
1239
-# else
1240
-# define cfWrite(b,os,no,f) fwrite(b,os,no,f)
1241
-# define cfGets(b,n,f) fgets(b,n,f)
1242
-# endif
1243
-
1244
-# ifdef CONSIO_EPUTB
1245
-SQLITE_INTERNAL_LINKAGE int
1246
-ePutbUtf8(const char *cBuf, int nAccept){
1247
- FILE *pfErr;
1248
- PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1249
- PerStreamTags *ppst = getEmitStreamInfo(2, &pst, &pfErr);
1250
-# if CIO_WIN_WC_XLATE
1251
- if( pstReachesConsole(ppst) ){
1252
- return conZstrEmit(ppst, cBuf, nAccept);
1253
- }else {
1254
-# endif
1255
- return (int)cfWrite(cBuf, 1, nAccept, pfErr);
1256
-# if CIO_WIN_WC_XLATE
1257
- }
1258
-# endif
1259
-}
1260
-# endif /* defined(CONSIO_EPUTB) */
1261
-
1262
-SQLITE_INTERNAL_LINKAGE char* fGetsUtf8(char *cBuf, int ncMax, FILE *pfIn){
1263
- if( pfIn==0 ) pfIn = stdin;
1264
-# if CIO_WIN_WC_XLATE
1265
- if( pfIn == consoleInfo.pstSetup[0].pf
1266
- && (consoleInfo.sacSetup & SAC_InConsole)!=0 ){
1267
-# if CIO_WIN_WC_XLATE==1
1268
-# define SHELL_GULP 150 /* Count of WCHARS to be gulped at a time */
1269
- WCHAR wcBuf[SHELL_GULP+1];
1270
- int lend = 0, noc = 0;
1271
- if( ncMax > 0 ) cBuf[0] = 0;
1272
- while( noc < ncMax-8-1 && !lend ){
1273
- /* There is room for at least 2 more characters and a 0-terminator. */
1274
- int na = (ncMax > SHELL_GULP*4+1 + noc)? SHELL_GULP : (ncMax-1 - noc)/4;
1275
-# undef SHELL_GULP
1276
- DWORD nbr = 0;
1277
- BOOL bRC = ReadConsoleW(consoleInfo.pstSetup[0].hx, wcBuf, na, &nbr, 0);
1278
- if( bRC && nbr>0 && (wcBuf[nbr-1]&0xF800)==0xD800 ){
1279
- /* Last WHAR read is first of a UTF-16 surrogate pair. Grab its mate. */
1280
- DWORD nbrx;
1281
- bRC &= ReadConsoleW(consoleInfo.pstSetup[0].hx, wcBuf+nbr, 1, &nbrx, 0);
1282
- if( bRC ) nbr += nbrx;
1283
- }
1284
- if( !bRC || (noc==0 && nbr==0) ) return 0;
1285
- if( nbr > 0 ){
1286
- int nmb = WideCharToMultiByte(CP_UTF8, 0, wcBuf,nbr,0,0,0,0);
1287
- if( nmb != 0 && noc+nmb <= ncMax ){
1288
- int iseg = noc;
1289
- nmb = WideCharToMultiByte(CP_UTF8, 0, wcBuf,nbr,cBuf+noc,nmb,0,0);
1290
- noc += nmb;
1291
- /* Fixup line-ends as coded by Windows for CR (or "Enter".)
1292
- ** This is done without regard for any setMode{Text,Binary}()
1293
- ** call that might have been done on the interactive input.
1294
- */
1295
- if( noc > 0 ){
1296
- if( cBuf[noc-1]=='\n' ){
1297
- lend = 1;
1298
- if( noc > 1 && cBuf[noc-2]=='\r' ) cBuf[--noc-1] = '\n';
1299
- }
1300
- }
1301
- /* Check for ^Z (anywhere in line) too, to act as EOF. */
1302
- while( iseg < noc ){
1303
- if( cBuf[iseg]=='\x1a' ){
1304
- noc = iseg; /* Chop ^Z and anything following. */
1305
- lend = 1; /* Counts as end of line too. */
1306
- break;
1307
- }
1308
- ++iseg;
1309
- }
1310
- }else break; /* Drop apparent garbage in. (Could assert.) */
1311
- }else break;
1312
- }
1313
- /* If got nothing, (after ^Z chop), must be at end-of-file. */
1314
- if( noc > 0 ){
1315
- cBuf[noc] = 0;
1316
- return cBuf;
1317
- }else return 0;
1318
-# endif
1319
- }else{
1320
-# endif
1321
- return cfGets(cBuf, ncMax, pfIn);
1322
-# if CIO_WIN_WC_XLATE
1323
- }
1324
-# endif
1325
-}
1326
-#endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
1327
-
1328
-#if defined(_MSC_VER)
1329
-# pragma warning(default : 4204)
1330
-#endif
1331
-
1332
-#undef SHELL_INVALID_FILE_PTR
1333
-
1334
-/************************* End ../ext/consio/console_io.c ********************/
1335
-
1336
-#ifndef SQLITE_SHELL_FIDDLE
1337
-
1338
-/* From here onward, fgets() is redirected to the console_io library. */
1339
-# define fgets(b,n,f) fGetsUtf8(b,n,f)
1340
-/*
1341
- * Define macros for emitting output text in various ways:
1342
- * sputz(s, z) => emit 0-terminated string z to given stream s
1343
- * sputf(s, f, ...) => emit varargs per format f to given stream s
1344
- * oputz(z) => emit 0-terminated string z to default stream
1345
- * oputf(f, ...) => emit varargs per format f to default stream
1346
- * eputz(z) => emit 0-terminated string z to error stream
1347
- * eputf(f, ...) => emit varargs per format f to error stream
1348
- * oputb(b, n) => emit char buffer b[0..n-1] to default stream
1349
- *
1350
- * Note that the default stream is whatever has been last set via:
1351
- * setOutputStream(FILE *pf)
1352
- * This is normally the stream that CLI normal output goes to.
1353
- * For the stand-alone CLI, it is stdout with no .output redirect.
1354
- *
1355
- * The ?putz(z) forms are required for the Fiddle builds for string literal
1356
- * output, in aid of enforcing format string to argument correspondence.
1357
- */
1358
-# define sputz(s,z) fPutsUtf8(z,s)
1359
-# define sputf fPrintfUtf8
1360
-# define oputz(z) oPutsUtf8(z)
1361
-# define oputf oPrintfUtf8
1362
-# define eputz(z) ePutsUtf8(z)
1363
-# define eputf ePrintfUtf8
1364
-# define oputb(buf,na) oPutbUtf8(buf,na)
1365
-# define fflush(s) fFlushBuffer(s);
1366
-
1367
-#else
1368
-/* For Fiddle, all console handling and emit redirection is omitted. */
1369
-/* These next 3 macros are for emitting formatted output. When complaints
1370
- * from the WASM build are issued for non-formatted output, when a mere
1371
- * string literal is to be emitted, the ?putz(z) forms should be used.
1372
- * (This permits compile-time checking of format string / argument mismatch.)
1373
- */
1374
-# define oputf(fmt, ...) printf(fmt,__VA_ARGS__)
1375
-# define eputf(fmt, ...) fprintf(stderr,fmt,__VA_ARGS__)
1376
-# define sputf(fp,fmt, ...) fprintf(fp,fmt,__VA_ARGS__)
1377
-/* These next 3 macros are for emitting simple string literals. */
1378
-# define oputz(z) fputs(z,stdout)
1379
-# define eputz(z) fputs(z,stderr)
1380
-# define sputz(fp,z) fputs(z,fp)
1381
-# define oputb(buf,na) fwrite(buf,1,na,stdout)
1382
-# undef fflush
1383
-#endif
540
+
541
+#define eputz(z) sqlite3_fputs(z,stderr)
542
+#define sputz(fp,z) sqlite3_fputs(z,fp)
1384543
1385544
/* True if the timer is enabled */
1386545
static int enableTimer = 0;
1387546
1388547
/* A version of strcmp() that works with NULL values */
@@ -1423,10 +582,11 @@
1423582
struct timeval ru_utime; /* user CPU time used */
1424583
struct timeval ru_stime; /* system CPU time used */
1425584
};
1426585
#define getrusage(A,B) memset(B,0,sizeof(*B))
1427586
#endif
587
+
1428588
1429589
/* Saved resource information for the beginning of an operation */
1430590
static struct rusage sBegin; /* CPU time at start */
1431591
static sqlite3_int64 iBegin; /* Wall-clock time at start */
1432592
@@ -1447,24 +607,24 @@
1447607
}
1448608
1449609
/*
1450610
** Print the timing results.
1451611
*/
1452
-static void endTimer(void){
612
+static void endTimer(FILE *out){
1453613
if( enableTimer ){
1454614
sqlite3_int64 iEnd = timeOfDay();
1455615
struct rusage sEnd;
1456616
getrusage(RUSAGE_SELF, &sEnd);
1457
- sputf(stdout, "Run Time: real %.3f user %f sys %f\n",
617
+ sqlite3_fprintf(out, "Run Time: real %.3f user %f sys %f\n",
1458618
(iEnd - iBegin)*0.001,
1459619
timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
1460620
timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
1461621
}
1462622
}
1463623
1464624
#define BEGIN_TIMER beginTimer()
1465
-#define END_TIMER endTimer()
625
+#define END_TIMER(X) endTimer(X)
1466626
#define HAS_TIMER 1
1467627
1468628
#elif (defined(_WIN32) || defined(WIN32))
1469629
1470630
/* Saved resource information for the beginning of an operation */
@@ -1526,29 +686,29 @@
1526686
}
1527687
1528688
/*
1529689
** Print the timing results.
1530690
*/
1531
-static void endTimer(void){
691
+static void endTimer(FILE *out){
1532692
if( enableTimer && getProcessTimesAddr){
1533693
FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
1534694
sqlite3_int64 ftWallEnd = timeOfDay();
1535695
getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
1536
- sputf(stdout, "Run Time: real %.3f user %f sys %f\n",
696
+ sqlite3_fprintf(out, "Run Time: real %.3f user %f sys %f\n",
1537697
(ftWallEnd - ftWallBegin)*0.001,
1538698
timeDiff(&ftUserBegin, &ftUserEnd),
1539699
timeDiff(&ftKernelBegin, &ftKernelEnd));
1540700
}
1541701
}
1542702
1543703
#define BEGIN_TIMER beginTimer()
1544
-#define END_TIMER endTimer()
704
+#define END_TIMER(X) endTimer(X)
1545705
#define HAS_TIMER hasTimer()
1546706
1547707
#else
1548708
#define BEGIN_TIMER
1549
-#define END_TIMER
709
+#define END_TIMER(X) /*no-op*/
1550710
#define HAS_TIMER 0
1551711
#endif
1552712
1553713
/*
1554714
** Used to prevent warnings about unused parameters
@@ -1745,41 +905,219 @@
1745905
char *z;
1746906
if( iotrace==0 ) return;
1747907
va_start(ap, zFormat);
1748908
z = sqlite3_vmprintf(zFormat, ap);
1749909
va_end(ap);
1750
- sputf(iotrace, "%s", z);
910
+ sqlite3_fprintf(iotrace, "%s", z);
1751911
sqlite3_free(z);
1752912
}
1753913
#endif
1754914
915
+/* Lookup table to estimate the number of columns consumed by a Unicode
916
+** character.
917
+*/
918
+static const struct {
919
+ unsigned char w; /* Width of the character in columns */
920
+ int iFirst; /* First character in a span having this width */
921
+} aUWidth[] = {
922
+ /* {0, 0x00000}, {1, 0x00020}, {0, 0x0007f}, {1, 0x000a0}, */
923
+ {0, 0x00300}, {1, 0x00370}, {0, 0x00483}, {1, 0x00487}, {0, 0x00488},
924
+ {1, 0x0048a}, {0, 0x00591}, {1, 0x005be}, {0, 0x005bf}, {1, 0x005c0},
925
+ {0, 0x005c1}, {1, 0x005c3}, {0, 0x005c4}, {1, 0x005c6}, {0, 0x005c7},
926
+ {1, 0x005c8}, {0, 0x00600}, {1, 0x00604}, {0, 0x00610}, {1, 0x00616},
927
+ {0, 0x0064b}, {1, 0x0065f}, {0, 0x00670}, {1, 0x00671}, {0, 0x006d6},
928
+ {1, 0x006e5}, {0, 0x006e7}, {1, 0x006e9}, {0, 0x006ea}, {1, 0x006ee},
929
+ {0, 0x0070f}, {1, 0x00710}, {0, 0x00711}, {1, 0x00712}, {0, 0x00730},
930
+ {1, 0x0074b}, {0, 0x007a6}, {1, 0x007b1}, {0, 0x007eb}, {1, 0x007f4},
931
+ {0, 0x00901}, {1, 0x00903}, {0, 0x0093c}, {1, 0x0093d}, {0, 0x00941},
932
+ {1, 0x00949}, {0, 0x0094d}, {1, 0x0094e}, {0, 0x00951}, {1, 0x00955},
933
+ {0, 0x00962}, {1, 0x00964}, {0, 0x00981}, {1, 0x00982}, {0, 0x009bc},
934
+ {1, 0x009bd}, {0, 0x009c1}, {1, 0x009c5}, {0, 0x009cd}, {1, 0x009ce},
935
+ {0, 0x009e2}, {1, 0x009e4}, {0, 0x00a01}, {1, 0x00a03}, {0, 0x00a3c},
936
+ {1, 0x00a3d}, {0, 0x00a41}, {1, 0x00a43}, {0, 0x00a47}, {1, 0x00a49},
937
+ {0, 0x00a4b}, {1, 0x00a4e}, {0, 0x00a70}, {1, 0x00a72}, {0, 0x00a81},
938
+ {1, 0x00a83}, {0, 0x00abc}, {1, 0x00abd}, {0, 0x00ac1}, {1, 0x00ac6},
939
+ {0, 0x00ac7}, {1, 0x00ac9}, {0, 0x00acd}, {1, 0x00ace}, {0, 0x00ae2},
940
+ {1, 0x00ae4}, {0, 0x00b01}, {1, 0x00b02}, {0, 0x00b3c}, {1, 0x00b3d},
941
+ {0, 0x00b3f}, {1, 0x00b40}, {0, 0x00b41}, {1, 0x00b44}, {0, 0x00b4d},
942
+ {1, 0x00b4e}, {0, 0x00b56}, {1, 0x00b57}, {0, 0x00b82}, {1, 0x00b83},
943
+ {0, 0x00bc0}, {1, 0x00bc1}, {0, 0x00bcd}, {1, 0x00bce}, {0, 0x00c3e},
944
+ {1, 0x00c41}, {0, 0x00c46}, {1, 0x00c49}, {0, 0x00c4a}, {1, 0x00c4e},
945
+ {0, 0x00c55}, {1, 0x00c57}, {0, 0x00cbc}, {1, 0x00cbd}, {0, 0x00cbf},
946
+ {1, 0x00cc0}, {0, 0x00cc6}, {1, 0x00cc7}, {0, 0x00ccc}, {1, 0x00cce},
947
+ {0, 0x00ce2}, {1, 0x00ce4}, {0, 0x00d41}, {1, 0x00d44}, {0, 0x00d4d},
948
+ {1, 0x00d4e}, {0, 0x00dca}, {1, 0x00dcb}, {0, 0x00dd2}, {1, 0x00dd5},
949
+ {0, 0x00dd6}, {1, 0x00dd7}, {0, 0x00e31}, {1, 0x00e32}, {0, 0x00e34},
950
+ {1, 0x00e3b}, {0, 0x00e47}, {1, 0x00e4f}, {0, 0x00eb1}, {1, 0x00eb2},
951
+ {0, 0x00eb4}, {1, 0x00eba}, {0, 0x00ebb}, {1, 0x00ebd}, {0, 0x00ec8},
952
+ {1, 0x00ece}, {0, 0x00f18}, {1, 0x00f1a}, {0, 0x00f35}, {1, 0x00f36},
953
+ {0, 0x00f37}, {1, 0x00f38}, {0, 0x00f39}, {1, 0x00f3a}, {0, 0x00f71},
954
+ {1, 0x00f7f}, {0, 0x00f80}, {1, 0x00f85}, {0, 0x00f86}, {1, 0x00f88},
955
+ {0, 0x00f90}, {1, 0x00f98}, {0, 0x00f99}, {1, 0x00fbd}, {0, 0x00fc6},
956
+ {1, 0x00fc7}, {0, 0x0102d}, {1, 0x01031}, {0, 0x01032}, {1, 0x01033},
957
+ {0, 0x01036}, {1, 0x01038}, {0, 0x01039}, {1, 0x0103a}, {0, 0x01058},
958
+ {1, 0x0105a}, {2, 0x01100}, {0, 0x01160}, {1, 0x01200}, {0, 0x0135f},
959
+ {1, 0x01360}, {0, 0x01712}, {1, 0x01715}, {0, 0x01732}, {1, 0x01735},
960
+ {0, 0x01752}, {1, 0x01754}, {0, 0x01772}, {1, 0x01774}, {0, 0x017b4},
961
+ {1, 0x017b6}, {0, 0x017b7}, {1, 0x017be}, {0, 0x017c6}, {1, 0x017c7},
962
+ {0, 0x017c9}, {1, 0x017d4}, {0, 0x017dd}, {1, 0x017de}, {0, 0x0180b},
963
+ {1, 0x0180e}, {0, 0x018a9}, {1, 0x018aa}, {0, 0x01920}, {1, 0x01923},
964
+ {0, 0x01927}, {1, 0x01929}, {0, 0x01932}, {1, 0x01933}, {0, 0x01939},
965
+ {1, 0x0193c}, {0, 0x01a17}, {1, 0x01a19}, {0, 0x01b00}, {1, 0x01b04},
966
+ {0, 0x01b34}, {1, 0x01b35}, {0, 0x01b36}, {1, 0x01b3b}, {0, 0x01b3c},
967
+ {1, 0x01b3d}, {0, 0x01b42}, {1, 0x01b43}, {0, 0x01b6b}, {1, 0x01b74},
968
+ {0, 0x01dc0}, {1, 0x01dcb}, {0, 0x01dfe}, {1, 0x01e00}, {0, 0x0200b},
969
+ {1, 0x02010}, {0, 0x0202a}, {1, 0x0202f}, {0, 0x02060}, {1, 0x02064},
970
+ {0, 0x0206a}, {1, 0x02070}, {0, 0x020d0}, {1, 0x020f0}, {2, 0x02329},
971
+ {1, 0x0232b}, {2, 0x02e80}, {0, 0x0302a}, {2, 0x03030}, {1, 0x0303f},
972
+ {2, 0x03040}, {0, 0x03099}, {2, 0x0309b}, {1, 0x0a4d0}, {0, 0x0a806},
973
+ {1, 0x0a807}, {0, 0x0a80b}, {1, 0x0a80c}, {0, 0x0a825}, {1, 0x0a827},
974
+ {2, 0x0ac00}, {1, 0x0d7a4}, {2, 0x0f900}, {1, 0x0fb00}, {0, 0x0fb1e},
975
+ {1, 0x0fb1f}, {0, 0x0fe00}, {2, 0x0fe10}, {1, 0x0fe1a}, {0, 0x0fe20},
976
+ {1, 0x0fe24}, {2, 0x0fe30}, {1, 0x0fe70}, {0, 0x0feff}, {2, 0x0ff00},
977
+ {1, 0x0ff61}, {2, 0x0ffe0}, {1, 0x0ffe7}, {0, 0x0fff9}, {1, 0x0fffc},
978
+ {0, 0x10a01}, {1, 0x10a04}, {0, 0x10a05}, {1, 0x10a07}, {0, 0x10a0c},
979
+ {1, 0x10a10}, {0, 0x10a38}, {1, 0x10a3b}, {0, 0x10a3f}, {1, 0x10a40},
980
+ {0, 0x1d167}, {1, 0x1d16a}, {0, 0x1d173}, {1, 0x1d183}, {0, 0x1d185},
981
+ {1, 0x1d18c}, {0, 0x1d1aa}, {1, 0x1d1ae}, {0, 0x1d242}, {1, 0x1d245},
982
+ {2, 0x20000}, {1, 0x2fffe}, {2, 0x30000}, {1, 0x3fffe}, {0, 0xe0001},
983
+ {1, 0xe0002}, {0, 0xe0020}, {1, 0xe0080}, {0, 0xe0100}, {1, 0xe01f0}
984
+};
985
+
1755986
/*
1756
-** Output string zUtf to Out stream as w characters. If w is negative,
987
+** Return an estimate of the width, in columns, for the single Unicode
988
+** character c. For normal characters, the answer is always 1. But the
989
+** estimate might be 0 or 2 for zero-width and double-width characters.
990
+**
991
+** Different display devices display unicode using different widths. So
992
+** it is impossible to know that true display width with 100% accuracy.
993
+** Inaccuracies in the width estimates might cause columns to be misaligned.
994
+** Unfortunately, there is nothing we can do about that.
995
+*/
996
+int cli_wcwidth(int c){
997
+ int iFirst, iLast;
998
+
999
+ /* Fast path for common characters */
1000
+ if( c<0x20 ) return 0;
1001
+ if( c<0x7f ) return 1;
1002
+ if( c<0xa0 ) return 0;
1003
+ if( c<=0x300 ) return 1;
1004
+
1005
+ /* The general case */
1006
+ iFirst = 0;
1007
+ iLast = sizeof(aUWidth)/sizeof(aUWidth[0]) - 1;
1008
+ while( iFirst<iLast-1 ){
1009
+ int iMid = (iFirst+iLast)/2;
1010
+ int cMid = aUWidth[iMid].iFirst;
1011
+ if( cMid < c ){
1012
+ iFirst = iMid;
1013
+ }else if( cMid > c ){
1014
+ iLast = iMid - 1;
1015
+ }else{
1016
+ return aUWidth[iMid].w;
1017
+ }
1018
+ }
1019
+ if( aUWidth[iLast].iFirst > c ) return aUWidth[iFirst].w;
1020
+ return aUWidth[iLast].w;
1021
+}
1022
+
1023
+/*
1024
+** Compute the value and length of a multi-byte UTF-8 character that
1025
+** begins at z[0]. Return the length. Write the Unicode value into *pU.
1026
+**
1027
+** This routine only works for *multi-byte* UTF-8 characters.
1028
+*/
1029
+static int decodeUtf8(const unsigned char *z, int *pU){
1030
+ if( (z[0] & 0xe0)==0xc0 && (z[1] & 0xc0)==0x80 ){
1031
+ *pU = ((z[0] & 0x1f)<<6) | (z[1] & 0x3f);
1032
+ return 2;
1033
+ }
1034
+ if( (z[0] & 0xf0)==0xe0 && (z[1] & 0xc0)==0x80 && (z[2] & 0xc0)==0x80 ){
1035
+ *pU = ((z[0] & 0x0f)<<12) | ((z[1] & 0x3f)<<6) | (z[2] & 0x3f);
1036
+ return 3;
1037
+ }
1038
+ if( (z[0] & 0xf8)==0xf0 && (z[1] & 0xc0)==0x80 && (z[2] & 0xc0)==0x80
1039
+ && (z[3] & 0xc0)==0x80
1040
+ ){
1041
+ *pU = ((z[0] & 0x0f)<<18) | ((z[1] & 0x3f)<<12) | ((z[2] & 0x3f))<<6
1042
+ | (z[4] & 0x3f);
1043
+ return 4;
1044
+ }
1045
+ *pU = 0;
1046
+ return 1;
1047
+}
1048
+
1049
+
1050
+#if 0 /* NOT USED */
1051
+/*
1052
+** Return the width, in display columns, of a UTF-8 string.
1053
+**
1054
+** Each normal character counts as 1. Zero-width characters count
1055
+** as zero, and double-width characters count as 2.
1056
+*/
1057
+int cli_wcswidth(const char *z){
1058
+ const unsigned char *a = (const unsigned char*)z;
1059
+ int n = 0;
1060
+ int i = 0;
1061
+ unsigned char c;
1062
+ while( (c = a[i])!=0 ){
1063
+ if( c>=0xc0 ){
1064
+ int u;
1065
+ int len = decodeUtf8(&a[i], &u);
1066
+ i += len;
1067
+ n += cli_wcwidth(u);
1068
+ }else if( c>=' ' ){
1069
+ n++;
1070
+ i++;
1071
+ }else{
1072
+ i++;
1073
+ }
1074
+ }
1075
+ return n;
1076
+}
1077
+#endif
1078
+
1079
+/*
1080
+** Output string zUtf to stdout as w characters. If w is negative,
17571081
** then right-justify the text. W is the width in UTF-8 characters, not
17581082
** in bytes. This is different from the %*.*s specification in printf
17591083
** since with %*.*s the width is measured in bytes, not characters.
1084
+**
1085
+** Take into account zero-width and double-width Unicode characters.
1086
+** In other words, a zero-width character does not count toward the
1087
+** the w limit. A double-width character counts as two.
17601088
*/
1761
-static void utf8_width_print(int w, const char *zUtf){
1762
- int i;
1763
- int n;
1089
+static void utf8_width_print(FILE *out, int w, const char *zUtf){
1090
+ const unsigned char *a = (const unsigned char*)zUtf;
1091
+ unsigned char c;
1092
+ int i = 0;
1093
+ int n = 0;
17641094
int aw = w<0 ? -w : w;
17651095
if( zUtf==0 ) zUtf = "";
1766
- for(i=n=0; zUtf[i]; i++){
1767
- if( (zUtf[i]&0xc0)!=0x80 ){
1096
+ while( (c = a[i])!=0 ){
1097
+ if( (c&0xc0)==0xc0 ){
1098
+ int u;
1099
+ int len = decodeUtf8(a+i, &u);
1100
+ int x = cli_wcwidth(u);
1101
+ if( x+n>aw ){
1102
+ break;
1103
+ }
1104
+ i += len;
1105
+ n += x;
1106
+ }else if( n>=aw ){
1107
+ break;
1108
+ }else{
17681109
n++;
1769
- if( n==aw ){
1770
- do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
1771
- break;
1772
- }
1110
+ i++;
17731111
}
17741112
}
17751113
if( n>=aw ){
1776
- oputf("%.*s", i, zUtf);
1114
+ sqlite3_fprintf(out, "%.*s", i, zUtf);
17771115
}else if( w<0 ){
1778
- oputf("%*s%s", aw-n, "", zUtf);
1116
+ sqlite3_fprintf(out, "%*s%s", aw-n, "", zUtf);
17791117
}else{
1780
- oputf("%s%*s", zUtf, aw-n, "");
1118
+ sqlite3_fprintf(out, "%s%*s", zUtf, aw-n, "");
17811119
}
17821120
}
17831121
17841122
17851123
/*
@@ -1841,11 +1179,11 @@
18411179
struct __stat64 x = {0};
18421180
# define STAT_CHR_SRC(mode) ((mode & (_S_IFCHR|_S_IFIFO|_S_IFREG))!=0)
18431181
/* On Windows, open first, then check the stream nature. This order
18441182
** is necessary because _stat() and sibs, when checking a named pipe,
18451183
** effectively break the pipe as its supplier sees it. */
1846
- FILE *rv = fopen(zFile, "rb");
1184
+ FILE *rv = sqlite3_fopen(zFile, "rb");
18471185
if( rv==0 ) return 0;
18481186
if( _fstat64(_fileno(rv), &x) != 0
18491187
|| !STAT_CHR_SRC(x.st_mode)){
18501188
fclose(rv);
18511189
rv = 0;
@@ -1855,11 +1193,11 @@
18551193
struct stat x = {0};
18561194
int rc = stat(zFile, &x);
18571195
# define STAT_CHR_SRC(mode) (S_ISREG(mode)||S_ISFIFO(mode)||S_ISCHR(mode))
18581196
if( rc!=0 ) return 0;
18591197
if( STAT_CHR_SRC(x.st_mode) ){
1860
- return fopen(zFile, "rb");
1198
+ return sqlite3_fopen(zFile, "rb");
18611199
}else{
18621200
return 0;
18631201
}
18641202
#endif
18651203
#undef STAT_CHR_SRC
@@ -1882,11 +1220,11 @@
18821220
if( n+100>nLine ){
18831221
nLine = nLine*2 + 100;
18841222
zLine = realloc(zLine, nLine);
18851223
shell_check_oom(zLine);
18861224
}
1887
- if( fgets(&zLine[n], nLine - n, in)==0 ){
1225
+ if( sqlite3_fgets(&zLine[n], nLine - n, in)==0 ){
18881226
if( n==0 ){
18891227
free(zLine);
18901228
return 0;
18911229
}
18921230
zLine[n] = 0;
@@ -8604,10 +7942,17 @@
86047942
# define lstat(path,buf) stat(path,buf)
86057943
#endif
86067944
#include <time.h>
86077945
#include <errno.h>
86087946
7947
+/* When used as part of the CLI, the sqlite3_stdio.h module will have
7948
+** been included before this one. In that case use the sqlite3_stdio.h
7949
+** #defines. If not, create our own for fopen().
7950
+*/
7951
+#ifndef _SQLITE3_STDIO_H_
7952
+# define sqlite3_fopen fopen
7953
+#endif
86097954
86107955
/*
86117956
** Structure of the fsdir() table-valued function
86127957
*/
86137958
/* 0 1 2 3 4 5 */
@@ -8636,11 +7981,11 @@
86367981
sqlite3_int64 nIn;
86377982
void *pBuf;
86387983
sqlite3 *db;
86397984
int mxBlob;
86407985
8641
- in = fopen(zName, "rb");
7986
+ in = sqlite3_fopen(zName, "rb");
86427987
if( in==0 ){
86437988
/* File does not exist or is unreadable. Leave the result set to NULL. */
86447989
return;
86457990
}
86467991
fseek(in, 0, SEEK_END);
@@ -8891,11 +8236,11 @@
88918236
}
88928237
}else{
88938238
sqlite3_int64 nWrite = 0;
88948239
const char *z;
88958240
int rc = 0;
8896
- FILE *out = fopen(zFile, "wb");
8241
+ FILE *out = sqlite3_fopen(zFile, "wb");
88978242
if( out==0 ) return 1;
88988243
z = (const char*)sqlite3_value_blob(pData);
88998244
if( z ){
89008245
sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
89018246
nWrite = sqlite3_value_bytes(pData);
@@ -10752,10 +10097,18 @@
1075210097
#ifndef SQLITE_NO_STDINT
1075310098
# include <stdint.h>
1075410099
#endif
1075510100
1075610101
#include <zlib.h>
10102
+
10103
+/* When used as part of the CLI, the sqlite3_stdio.h module will have
10104
+** been included before this one. In that case use the sqlite3_stdio.h
10105
+** #defines. If not, create our own for fopen().
10106
+*/
10107
+#ifndef _SQLITE3_STDIO_H_
10108
+# define sqlite3_fopen fopen
10109
+#endif
1075710110
1075810111
#ifndef SQLITE_OMIT_VIRTUALTABLE
1075910112
1076010113
#ifndef SQLITE_AMALGAMATION
1076110114
@@ -12009,11 +11362,11 @@
1200911362
}else{
1201011363
zFile = (const char*)sqlite3_value_text(argv[0]);
1201111364
}
1201211365
1201311366
if( 0==pTab->pWriteFd && 0==bInMemory ){
12014
- pCsr->pFile = zFile ? fopen(zFile, "rb") : 0;
11367
+ pCsr->pFile = zFile ? sqlite3_fopen(zFile, "rb") : 0;
1201511368
if( pCsr->pFile==0 ){
1201611369
zipfileCursorErr(pCsr, "cannot open file: %s", zFile);
1201711370
rc = SQLITE_ERROR;
1201811371
}else{
1201911372
rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd);
@@ -12199,11 +11552,11 @@
1219911552
1220011553
/* Open a write fd on the file. Also load the entire central directory
1220111554
** structure into memory. During the transaction any new file data is
1220211555
** appended to the archive file, but the central directory is accumulated
1220311556
** in main-memory until the transaction is committed. */
12204
- pTab->pWriteFd = fopen(pTab->zFile, "ab+");
11557
+ pTab->pWriteFd = sqlite3_fopen(pTab->zFile, "ab+");
1220511558
if( pTab->pWriteFd==0 ){
1220611559
pTab->base.zErrMsg = sqlite3_mprintf(
1220711560
"zipfile: failed to open file %s for writing", pTab->zFile
1220811561
);
1220911562
rc = SQLITE_ERROR;
@@ -14883,10 +14236,16 @@
1488314236
sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC);
1488414237
while( SQLITE_OK==rc && SQLITE_ROW==sqlite3_step(pIndexXInfo) ){
1488514238
const char *zComma = zCols==0 ? "" : ", ";
1488614239
const char *zName = (const char*)sqlite3_column_text(pIndexXInfo, 0);
1488714240
const char *zColl = (const char*)sqlite3_column_text(pIndexXInfo, 1);
14241
+ if( zName==0 ){
14242
+ /* This index contains an expression. Ignore it. */
14243
+ sqlite3_free(zCols);
14244
+ sqlite3_free(zOrder);
14245
+ return sqlite3_reset(pIndexXInfo);
14246
+ }
1488814247
zCols = idxAppendText(&rc, zCols,
1488914248
"%sx.%Q IS sqlite_expert_rem(%d, x.%Q) COLLATE %s",
1489014249
zComma, zName, nCol, zName, zColl
1489114250
);
1489214251
zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol);
@@ -21991,10 +21350,11 @@
2199121350
#define MODE_Table 15 /* MySQL-style table formatting */
2199221351
#define MODE_Box 16 /* Unicode box-drawing characters */
2199321352
#define MODE_Count 17 /* Output only a count of the rows of output */
2199421353
#define MODE_Off 18 /* No query output shown */
2199521354
#define MODE_ScanExp 19 /* Like MODE_Explain, but for ".scanstats vm" */
21355
+#define MODE_Www 20 /* Full web-page output */
2199621356
2199721357
static const char *modeDescr[] = {
2199821358
"line",
2199921359
"column",
2200021360
"list",
@@ -22011,11 +21371,13 @@
2201121371
"json",
2201221372
"markdown",
2201321373
"table",
2201421374
"box",
2201521375
"count",
22016
- "off"
21376
+ "off",
21377
+ "scanexp",
21378
+ "www",
2201721379
};
2201821380
2201921381
/*
2202021382
** These are the column/row/line separators used by the various
2202121383
** import/export modes.
@@ -22023,11 +21385,17 @@
2202321385
#define SEP_Column "|"
2202421386
#define SEP_Row "\n"
2202521387
#define SEP_Tab "\t"
2202621388
#define SEP_Space " "
2202721389
#define SEP_Comma ","
22028
-#define SEP_CrLf "\r\n"
21390
+#ifdef SQLITE_U8TEXT_ONLY
21391
+ /* With the SQLITE_U8TEXT_ONLY option, the output will always be in
21392
+ ** text mode. The \r will be inserted automatically. */
21393
+# define SEP_CrLf "\n"
21394
+#else
21395
+# define SEP_CrLf "\r\n"
21396
+#endif
2202921397
#define SEP_Unit "\x1F"
2203021398
#define SEP_Record "\x1E"
2203121399
2203221400
/*
2203321401
** Limit input nesting via .read or any other input redirect.
@@ -22039,11 +21407,11 @@
2203921407
** A callback for the sqlite3_log() interface.
2204021408
*/
2204121409
static void shellLog(void *pArg, int iErrCode, const char *zMsg){
2204221410
ShellState *p = (ShellState*)pArg;
2204321411
if( p->pLog==0 ) return;
22044
- sputf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
21412
+ sqlite3_fprintf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
2204521413
fflush(p->pLog);
2204621414
}
2204721415
2204821416
/*
2204921417
** SQL function: shell_putsnl(X)
@@ -22054,13 +21422,13 @@
2205421422
static void shellPutsFunc(
2205521423
sqlite3_context *pCtx,
2205621424
int nVal,
2205721425
sqlite3_value **apVal
2205821426
){
22059
- /* Unused: (ShellState*)sqlite3_user_data(pCtx); */
21427
+ ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
2206021428
(void)nVal;
22061
- oputf("%s\n", sqlite3_value_text(apVal[0]));
21429
+ sqlite3_fprintf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
2206221430
sqlite3_result_value(pCtx, apVal[0]);
2206321431
}
2206421432
2206521433
/*
2206621434
** If in safe mode, print an error message described by the arguments
@@ -22075,11 +21443,11 @@
2207521443
va_list ap;
2207621444
char *zMsg;
2207721445
va_start(ap, zErrMsg);
2207821446
zMsg = sqlite3_vmprintf(zErrMsg, ap);
2207921447
va_end(ap);
22080
- eputf("line %d: %s\n", p->lineno, zMsg);
21448
+ sqlite3_fprintf(stderr, "line %d: %s\n", p->lineno, zMsg);
2208121449
exit(1);
2208221450
}
2208321451
}
2208421452
2208521453
/*
@@ -22142,11 +21510,11 @@
2214221510
}
2214321511
}
2214421512
bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
2214521513
/* When writing the file to be edited, do \n to \r\n conversions on systems
2214621514
** that want \r\n line endings */
22147
- f = fopen(zTempFile, bBin ? "wb" : "w");
21515
+ f = sqlite3_fopen(zTempFile, bBin ? "wb" : "w");
2214821516
if( f==0 ){
2214921517
sqlite3_result_error(context, "edit() cannot open temp file", -1);
2215021518
goto edit_func_end;
2215121519
}
2215221520
sz = sqlite3_value_bytes(argv[0]);
@@ -22173,11 +21541,11 @@
2217321541
sqlite3_free(zCmd);
2217421542
if( rc ){
2217521543
sqlite3_result_error(context, "EDITOR returned non-zero", -1);
2217621544
goto edit_func_end;
2217721545
}
22178
- f = fopen(zTempFile, "rb");
21546
+ f = sqlite3_fopen(zTempFile, "rb");
2217921547
if( f==0 ){
2218021548
sqlite3_result_error(context,
2218121549
"edit() cannot reopen temp file after edit", -1);
2218221550
goto edit_func_end;
2218321551
}
@@ -22243,11 +21611,11 @@
2224321611
}
2224421612
2224521613
/*
2224621614
** Output the given string as a hex-encoded blob (eg. X'1234' )
2224721615
*/
22248
-static void output_hex_blob(const void *pBlob, int nBlob){
21616
+static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
2224921617
int i;
2225021618
unsigned char *aBlob = (unsigned char*)pBlob;
2225121619
2225221620
char *zStr = sqlite3_malloc(nBlob*2 + 1);
2225321621
shell_check_oom(zStr);
@@ -22260,11 +21628,11 @@
2226021628
zStr[i*2] = aHex[ (aBlob[i] >> 4) ];
2226121629
zStr[i*2+1] = aHex[ (aBlob[i] & 0x0F) ];
2226221630
}
2226321631
zStr[i*2] = '\0';
2226421632
22265
- oputf("X'%s'", zStr);
21633
+ sqlite3_fprintf(out, "X'%s'", zStr);
2226621634
sqlite3_free(zStr);
2226721635
}
2226821636
2226921637
/*
2227021638
** Find a string that is not found anywhere in z[]. Return a pointer
@@ -22290,46 +21658,39 @@
2229021658
/*
2229121659
** Output the given string as a quoted string using SQL quoting conventions.
2229221660
**
2229321661
** See also: output_quoted_escaped_string()
2229421662
*/
22295
-static void output_quoted_string(const char *z){
21663
+static void output_quoted_string(FILE *out, const char *z){
2229621664
int i;
2229721665
char c;
22298
-#ifndef SQLITE_SHELL_FIDDLE
22299
- FILE *pfO = setOutputStream(invalidFileStream);
22300
- setBinaryMode(pfO, 1);
22301
-#endif
21666
+ sqlite3_fsetmode(out, _O_BINARY);
2230221667
if( z==0 ) return;
2230321668
for(i=0; (c = z[i])!=0 && c!='\''; i++){}
2230421669
if( c==0 ){
22305
- oputf("'%s'",z);
21670
+ sqlite3_fprintf(out, "'%s'",z);
2230621671
}else{
22307
- oputz("'");
21672
+ sqlite3_fputs("'", out);
2230821673
while( *z ){
2230921674
for(i=0; (c = z[i])!=0 && c!='\''; i++){}
2231021675
if( c=='\'' ) i++;
2231121676
if( i ){
22312
- oputf("%.*s", i, z);
21677
+ sqlite3_fprintf(out, "%.*s", i, z);
2231321678
z += i;
2231421679
}
2231521680
if( c=='\'' ){
22316
- oputz("'");
21681
+ sqlite3_fputs("'", out);
2231721682
continue;
2231821683
}
2231921684
if( c==0 ){
2232021685
break;
2232121686
}
2232221687
z++;
2232321688
}
22324
- oputz("'");
21689
+ sqlite3_fputs("'", out);
2232521690
}
22326
-#ifndef SQLITE_SHELL_FIDDLE
22327
- setTextMode(pfO, 1);
22328
-#else
22329
- setTextMode(stdout, 1);
22330
-#endif
21691
+ sqlite3_fsetmode(out, _O_TEXT);
2233121692
}
2233221693
2233321694
/*
2233421695
** Output the given string as a quoted string using SQL quoting conventions.
2233521696
** Additionallly , escape the "\n" and "\r" characters so that they do not
@@ -22337,20 +21698,17 @@
2233721698
** systems.
2233821699
**
2233921700
** This is like output_quoted_string() but with the addition of the \r\n
2234021701
** escape mechanism.
2234121702
*/
22342
-static void output_quoted_escaped_string(const char *z){
21703
+static void output_quoted_escaped_string(FILE *out, const char *z){
2234321704
int i;
2234421705
char c;
22345
-#ifndef SQLITE_SHELL_FIDDLE
22346
- FILE *pfO = setOutputStream(invalidFileStream);
22347
- setBinaryMode(pfO, 1);
22348
-#endif
21706
+ sqlite3_fsetmode(out, _O_BINARY);
2234921707
for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
2235021708
if( c==0 ){
22351
- oputf("'%s'",z);
21709
+ sqlite3_fprintf(out, "'%s'",z);
2235221710
}else{
2235321711
const char *zNL = 0;
2235421712
const char *zCR = 0;
2235521713
int nNL = 0;
2235621714
int nCR = 0;
@@ -22358,52 +21716,48 @@
2235821716
for(i=0; z[i]; i++){
2235921717
if( z[i]=='\n' ) nNL++;
2236021718
if( z[i]=='\r' ) nCR++;
2236121719
}
2236221720
if( nNL ){
22363
- oputz("replace(");
21721
+ sqlite3_fputs("replace(", out);
2236421722
zNL = unused_string(z, "\\n", "\\012", zBuf1);
2236521723
}
2236621724
if( nCR ){
22367
- oputz("replace(");
21725
+ sqlite3_fputs("replace(", out);
2236821726
zCR = unused_string(z, "\\r", "\\015", zBuf2);
2236921727
}
22370
- oputz("'");
21728
+ sqlite3_fputs("'", out);
2237121729
while( *z ){
2237221730
for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
2237321731
if( c=='\'' ) i++;
2237421732
if( i ){
22375
- oputf("%.*s", i, z);
21733
+ sqlite3_fprintf(out, "%.*s", i, z);
2237621734
z += i;
2237721735
}
2237821736
if( c=='\'' ){
22379
- oputz("'");
21737
+ sqlite3_fputs("'", out);
2238021738
continue;
2238121739
}
2238221740
if( c==0 ){
2238321741
break;
2238421742
}
2238521743
z++;
2238621744
if( c=='\n' ){
22387
- oputz(zNL);
21745
+ sqlite3_fputs(zNL, out);
2238821746
continue;
2238921747
}
22390
- oputz(zCR);
21748
+ sqlite3_fputs(zCR, out);
2239121749
}
22392
- oputz("'");
21750
+ sqlite3_fputs("'", out);
2239321751
if( nCR ){
22394
- oputf(",'%s',char(13))", zCR);
21752
+ sqlite3_fprintf(out, ",'%s',char(13))", zCR);
2239521753
}
2239621754
if( nNL ){
22397
- oputf(",'%s',char(10))", zNL);
21755
+ sqlite3_fprintf(out, ",'%s',char(10))", zNL);
2239821756
}
2239921757
}
22400
-#ifndef SQLITE_SHELL_FIDDLE
22401
- setTextMode(pfO, 1);
22402
-#else
22403
- setTextMode(stdout, 1);
22404
-#endif
21758
+ sqlite3_fsetmode(stdout, _O_TEXT);
2240521759
}
2240621760
2240721761
/*
2240821762
** Find earliest of chars within s specified in zAny.
2240921763
** With ns == ~0, is like strpbrk(s,zAny) and s must be 0-terminated.
@@ -22419,26 +21773,64 @@
2241921773
}
2242021774
++zAny;
2242121775
}
2242221776
return pcFirst;
2242321777
}
21778
+
21779
+/* Skip over as much z[] input char sequence as is valid UTF-8,
21780
+** limited per nAccept char's or whole characters and containing
21781
+** no char cn such that ((1<<cn) & ccm)!=0. On return, the
21782
+** sequence z:return (inclusive:exclusive) is validated UTF-8.
21783
+** Limit: nAccept>=0 => char count, nAccept<0 => character
21784
+ */
21785
+const char *zSkipValidUtf8(const char *z, int nAccept, long ccm){
21786
+ int ng = (nAccept<0)? -nAccept : 0;
21787
+ const char *pcLimit = (nAccept>=0)? z+nAccept : 0;
21788
+ assert(z!=0);
21789
+ while( (pcLimit)? (z<pcLimit) : (ng-- != 0) ){
21790
+ char c = *z;
21791
+ if( (c & 0x80) == 0 ){
21792
+ if( ccm != 0L && c < 0x20 && ((1L<<c) & ccm) != 0 ) return z;
21793
+ ++z; /* ASCII */
21794
+ }else if( (c & 0xC0) != 0xC0 ) return z; /* not a lead byte */
21795
+ else{
21796
+ const char *zt = z+1; /* Got lead byte, look at trail bytes.*/
21797
+ do{
21798
+ if( pcLimit && zt >= pcLimit ) return z;
21799
+ else{
21800
+ char ct = *zt++;
21801
+ if( ct==0 || (zt-z)>4 || (ct & 0xC0)!=0x80 ){
21802
+ /* Trailing bytes are too few, too many, or invalid. */
21803
+ return z;
21804
+ }
21805
+ }
21806
+ } while( ((c <<= 1) & 0x40) == 0x40 ); /* Eat lead byte's count. */
21807
+ z = zt;
21808
+ }
21809
+ }
21810
+ return z;
21811
+}
21812
+
21813
+
2242421814
/*
2242521815
** Output the given string as a quoted according to C or TCL quoting rules.
2242621816
*/
22427
-static void output_c_string(const char *z){
21817
+static void output_c_string(FILE *out, const char *z){
2242821818
char c;
2242921819
static const char *zq = "\"";
2243021820
static long ctrlMask = ~0L;
2243121821
static const char *zDQBSRO = "\"\\\x7f"; /* double-quote, backslash, rubout */
2243221822
char ace[3] = "\\?";
2243321823
char cbsSay;
22434
- oputz(zq);
21824
+ sqlite3_fputs(zq, out);
2243521825
while( *z!=0 ){
2243621826
const char *pcDQBSRO = anyOfInStr(z, zDQBSRO, ~(size_t)0);
2243721827
const char *pcPast = zSkipValidUtf8(z, INT_MAX, ctrlMask);
2243821828
const char *pcEnd = (pcDQBSRO && pcDQBSRO < pcPast)? pcDQBSRO : pcPast;
22439
- if( pcEnd > z ) oputb(z, (int)(pcEnd-z));
21829
+ if( pcEnd > z ){
21830
+ sqlite3_fprintf(out, "%.*s", (int)(pcEnd-z), z);
21831
+ }
2244021832
if( (c = *pcEnd)==0 ) break;
2244121833
++pcEnd;
2244221834
switch( c ){
2244321835
case '\\': case '"':
2244421836
cbsSay = (char)c;
@@ -22449,26 +21841,26 @@
2244921841
case '\f': cbsSay = 'f'; break;
2245021842
default: cbsSay = 0; break;
2245121843
}
2245221844
if( cbsSay ){
2245321845
ace[1] = cbsSay;
22454
- oputz(ace);
21846
+ sqlite3_fputs(ace, out);
2245521847
}else if( !isprint(c&0xff) ){
22456
- oputf("\\%03o", c&0xff);
21848
+ sqlite3_fprintf(out, "\\%03o", c&0xff);
2245721849
}else{
2245821850
ace[1] = (char)c;
22459
- oputz(ace+1);
21851
+ sqlite3_fputs(ace+1, out);
2246021852
}
2246121853
z = pcEnd;
2246221854
}
22463
- oputz(zq);
21855
+ sqlite3_fputs(zq, out);
2246421856
}
2246521857
2246621858
/*
2246721859
** Output the given string as a quoted according to JSON quoting rules.
2246821860
*/
22469
-static void output_json_string(const char *z, i64 n){
21861
+static void output_json_string(FILE *out, const char *z, i64 n){
2247021862
char c;
2247121863
static const char *zq = "\"";
2247221864
static long ctrlMask = ~0L;
2247321865
static const char *zDQBS = "\"\\";
2247421866
const char *pcLimit;
@@ -22475,17 +21867,17 @@
2247521867
char ace[3] = "\\?";
2247621868
char cbsSay;
2247721869
2247821870
if( z==0 ) z = "";
2247921871
pcLimit = z + ((n<0)? strlen(z) : (size_t)n);
22480
- oputz(zq);
21872
+ sqlite3_fputs(zq, out);
2248121873
while( z < pcLimit ){
2248221874
const char *pcDQBS = anyOfInStr(z, zDQBS, pcLimit-z);
2248321875
const char *pcPast = zSkipValidUtf8(z, (int)(pcLimit-z), ctrlMask);
2248421876
const char *pcEnd = (pcDQBS && pcDQBS < pcPast)? pcDQBS : pcPast;
2248521877
if( pcEnd > z ){
22486
- oputb(z, (int)(pcEnd-z));
21878
+ sqlite3_fprintf(out, "%.*s", (int)(pcEnd-z), z);
2248721879
z = pcEnd;
2248821880
}
2248921881
if( z >= pcLimit ) break;
2249021882
c = *(z++);
2249121883
switch( c ){
@@ -22499,26 +21891,26 @@
2249921891
case '\t': cbsSay = 't'; break;
2250021892
default: cbsSay = 0; break;
2250121893
}
2250221894
if( cbsSay ){
2250321895
ace[1] = cbsSay;
22504
- oputz(ace);
21896
+ sqlite3_fputs(ace, out);
2250521897
}else if( c<=0x1f ){
22506
- oputf("u%04x", c);
21898
+ sqlite3_fprintf(out, "u%04x", c);
2250721899
}else{
2250821900
ace[1] = (char)c;
22509
- oputz(ace+1);
21901
+ sqlite3_fputs(ace+1, out);
2251021902
}
2251121903
}
22512
- oputz(zq);
21904
+ sqlite3_fputs(zq, out);
2251321905
}
2251421906
2251521907
/*
2251621908
** Output the given string with characters that are special to
2251721909
** HTML escaped.
2251821910
*/
22519
-static void output_html_string(const char *z){
21911
+static void output_html_string(FILE *out, const char *z){
2252021912
int i;
2252121913
if( z==0 ) z = "";
2252221914
while( *z ){
2252321915
for(i=0; z[i]
2252421916
&& z[i]!='<'
@@ -22526,22 +21918,22 @@
2252621918
&& z[i]!='>'
2252721919
&& z[i]!='\"'
2252821920
&& z[i]!='\'';
2252921921
i++){}
2253021922
if( i>0 ){
22531
- oputf("%.*s",i,z);
21923
+ sqlite3_fprintf(out, "%.*s",i,z);
2253221924
}
2253321925
if( z[i]=='<' ){
22534
- oputz("&lt;");
21926
+ sqlite3_fputs("&lt;", out);
2253521927
}else if( z[i]=='&' ){
22536
- oputz("&amp;");
21928
+ sqlite3_fputs("&amp;", out);
2253721929
}else if( z[i]=='>' ){
22538
- oputz("&gt;");
21930
+ sqlite3_fputs("&gt;", out);
2253921931
}else if( z[i]=='\"' ){
22540
- oputz("&quot;");
21932
+ sqlite3_fputs("&quot;", out);
2254121933
}else if( z[i]=='\'' ){
22542
- oputz("&#39;");
21934
+ sqlite3_fputs("&#39;", out);
2254321935
}else{
2254421936
break;
2254521937
}
2254621938
z += i + 1;
2254721939
}
@@ -22576,11 +21968,11 @@
2257621968
** the null value. Strings are quoted if necessary. The separator
2257721969
** is only issued if bSep is true.
2257821970
*/
2257921971
static void output_csv(ShellState *p, const char *z, int bSep){
2258021972
if( z==0 ){
22581
- oputf("%s",p->nullValue);
21973
+ sqlite3_fprintf(p->out, "%s",p->nullValue);
2258221974
}else{
2258321975
unsigned i;
2258421976
for(i=0; z[i]; i++){
2258521977
if( needCsvQuote[((unsigned char*)z)[i]] ){
2258621978
i = 0;
@@ -22588,18 +21980,18 @@
2258821980
}
2258921981
}
2259021982
if( i==0 || strstr(z, p->colSeparator)!=0 ){
2259121983
char *zQuoted = sqlite3_mprintf("\"%w\"", z);
2259221984
shell_check_oom(zQuoted);
22593
- oputz(zQuoted);
21985
+ sqlite3_fputs(zQuoted, p->out);
2259421986
sqlite3_free(zQuoted);
2259521987
}else{
22596
- oputz(z);
21988
+ sqlite3_fputs(z, p->out);
2259721989
}
2259821990
}
2259921991
if( bSep ){
22600
- oputz(p->colSeparator);
21992
+ sqlite3_fputs(p->colSeparator, p->out);
2260121993
}
2260221994
}
2260321995
2260421996
/*
2260521997
** This routine runs when the user presses Ctrl-C
@@ -22703,20 +22095,20 @@
2270322095
const char *az[4];
2270422096
az[0] = zA1;
2270522097
az[1] = zA2;
2270622098
az[2] = zA3;
2270722099
az[3] = zA4;
22708
- oputf("authorizer: %s", azAction[op]);
22100
+ sqlite3_fprintf(p->out, "authorizer: %s", azAction[op]);
2270922101
for(i=0; i<4; i++){
22710
- oputz(" ");
22102
+ sqlite3_fputs(" ", p->out);
2271122103
if( az[i] ){
22712
- output_c_string(az[i]);
22104
+ output_c_string(p->out, az[i]);
2271322105
}else{
22714
- oputz("NULL");
22106
+ sqlite3_fputs("NULL", p->out);
2271522107
}
2271622108
}
22717
- oputz("\n");
22109
+ sqlite3_fputs("\n", p->out);
2271822110
if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4);
2271922111
return SQLITE_OK;
2272022112
}
2272122113
#endif
2272222114
@@ -22728,11 +22120,11 @@
2272822120
**
2272922121
** If the schema statement in z[] contains a start-of-comment and if
2273022122
** sqlite3_complete() returns false, try to terminate the comment before
2273122123
** printing the result. https://sqlite.org/forum/forumpost/d7be961c5c
2273222124
*/
22733
-static void printSchemaLine(const char *z, const char *zTail){
22125
+static void printSchemaLine(FILE *out, const char *z, const char *zTail){
2273422126
char *zToFree = 0;
2273522127
if( z==0 ) return;
2273622128
if( zTail==0 ) return;
2273722129
if( zTail[0]==';' && (strstr(z, "/*")!=0 || strstr(z,"--")!=0) ){
2273822130
const char *zOrig = z;
@@ -22750,20 +22142,20 @@
2275022142
}
2275122143
sqlite3_free(zNew);
2275222144
}
2275322145
}
2275422146
if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
22755
- oputf("CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
22147
+ sqlite3_fprintf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
2275622148
}else{
22757
- oputf("%s%s", z, zTail);
22149
+ sqlite3_fprintf(out, "%s%s", z, zTail);
2275822150
}
2275922151
sqlite3_free(zToFree);
2276022152
}
22761
-static void printSchemaLineN(char *z, int n, const char *zTail){
22153
+static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
2276222154
char c = z[n];
2276322155
z[n] = 0;
22764
- printSchemaLine(z, zTail);
22156
+ printSchemaLine(out, z, zTail);
2276522157
z[n] = c;
2276622158
}
2276722159
2276822160
/*
2276922161
** Return true if string z[] has nothing but whitespace and comments to the
@@ -22787,11 +22179,11 @@
2278722179
EQPGraphRow *pNew;
2278822180
i64 nText;
2278922181
if( zText==0 ) return;
2279022182
nText = strlen(zText);
2279122183
if( p->autoEQPtest ){
22792
- oputf("%d,%d,%s\n", iEqpId, p2, zText);
22184
+ sqlite3_fprintf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
2279322185
}
2279422186
pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
2279522187
shell_check_oom(pNew);
2279622188
pNew->iEqpId = iEqpId;
2279722189
pNew->iParentId = p2;
@@ -22835,11 +22227,12 @@
2283522227
i64 n = strlen(p->sGraph.zPrefix);
2283622228
char *z;
2283722229
for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
2283822230
pNext = eqp_next_row(p, iEqpId, pRow);
2283922231
z = pRow->zText;
22840
- oputf("%s%s%s\n", p->sGraph.zPrefix, pNext ? "|--" : "`--", z);
22232
+ sqlite3_fprintf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
22233
+ pNext ? "|--" : "`--", z);
2284122234
if( n<(i64)sizeof(p->sGraph.zPrefix)-7 ){
2284222235
memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4);
2284322236
eqp_render_level(p, pRow->iEqpId);
2284422237
p->sGraph.zPrefix[n] = 0;
2284522238
}
@@ -22855,17 +22248,17 @@
2285522248
if( pRow->zText[0]=='-' ){
2285622249
if( pRow->pNext==0 ){
2285722250
eqp_reset(p);
2285822251
return;
2285922252
}
22860
- oputf("%s\n", pRow->zText+3);
22253
+ sqlite3_fprintf(p->out, "%s\n", pRow->zText+3);
2286122254
p->sGraph.pRow = pRow->pNext;
2286222255
sqlite3_free(pRow);
2286322256
}else if( nCycle>0 ){
22864
- oputf("QUERY PLAN (cycles=%lld [100%%])\n", nCycle);
22257
+ sqlite3_fprintf(p->out, "QUERY PLAN (cycles=%lld [100%%])\n", nCycle);
2286522258
}else{
22866
- oputz("QUERY PLAN\n");
22259
+ sqlite3_fputs("QUERY PLAN\n", p->out);
2286722260
}
2286822261
p->sGraph.zPrefix[0] = 0;
2286922262
eqp_render_level(p, 0);
2287022263
eqp_reset(p);
2287122264
}
@@ -22877,33 +22270,33 @@
2287722270
*/
2287822271
static int progress_handler(void *pClientData) {
2287922272
ShellState *p = (ShellState*)pClientData;
2288022273
p->nProgress++;
2288122274
if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
22882
- oputf("Progress limit reached (%u)\n", p->nProgress);
22275
+ sqlite3_fprintf(p->out, "Progress limit reached (%u)\n", p->nProgress);
2288322276
if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
2288422277
if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
2288522278
return 1;
2288622279
}
2288722280
if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
22888
- oputf("Progress %u\n", p->nProgress);
22281
+ sqlite3_fprintf(p->out, "Progress %u\n", p->nProgress);
2288922282
}
2289022283
return 0;
2289122284
}
2289222285
#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
2289322286
2289422287
/*
2289522288
** Print N dashes
2289622289
*/
22897
-static void print_dashes(int N){
22290
+static void print_dashes(FILE *out, int N){
2289822291
const char zDash[] = "--------------------------------------------------";
2289922292
const int nDash = sizeof(zDash) - 1;
2290022293
while( N>nDash ){
22901
- oputz(zDash);
22294
+ sqlite3_fputs(zDash, out);
2290222295
N -= nDash;
2290322296
}
22904
- oputf("%.*s", N, zDash);
22297
+ sqlite3_fprintf(out, "%.*s", N, zDash);
2290522298
}
2290622299
2290722300
/*
2290822301
** Print a markdown or table-style row separator using ascii-art
2290922302
*/
@@ -22912,19 +22305,19 @@
2291222305
int nArg,
2291322306
const char *zSep
2291422307
){
2291522308
int i;
2291622309
if( nArg>0 ){
22917
- oputz(zSep);
22918
- print_dashes(p->actualWidth[0]+2);
22310
+ sqlite3_fputs(zSep, p->out);
22311
+ print_dashes(p->out, p->actualWidth[0]+2);
2291922312
for(i=1; i<nArg; i++){
22920
- oputz(zSep);
22921
- print_dashes(p->actualWidth[i]+2);
22313
+ sqlite3_fputs(zSep, p->out);
22314
+ print_dashes(p->out, p->actualWidth[i]+2);
2292222315
}
22923
- oputz(zSep);
22316
+ sqlite3_fputs(zSep, p->out);
2292422317
}
22925
- oputz("\n");
22318
+ sqlite3_fputs("\n", p->out);
2292622319
}
2292722320
2292822321
/*
2292922322
** This is the callback routine that the shell
2293022323
** invokes for each row of a query result.
@@ -22950,13 +22343,13 @@
2295022343
if( azArg==0 ) break;
2295122344
for(i=0; i<nArg; i++){
2295222345
int len = strlen30(azCol[i] ? azCol[i] : "");
2295322346
if( len>w ) w = len;
2295422347
}
22955
- if( p->cnt++>0 ) oputz(p->rowSeparator);
22348
+ if( p->cnt++>0 ) sqlite3_fputs(p->rowSeparator, p->out);
2295622349
for(i=0; i<nArg; i++){
22957
- oputf("%*s = %s%s", w, azCol[i],
22350
+ sqlite3_fprintf(p->out, "%*s = %s%s", w, azCol[i],
2295822351
azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
2295922352
}
2296022353
break;
2296122354
}
2296222355
case MODE_ScanExp:
@@ -22980,16 +22373,16 @@
2298022373
if( nArg>nWidth ) nArg = nWidth;
2298122374
2298222375
/* If this is the first row seen, print out the headers */
2298322376
if( p->cnt++==0 ){
2298422377
for(i=0; i<nArg; i++){
22985
- utf8_width_print(aWidth[i], azCol[ aMap[i] ]);
22986
- oputz(i==nArg-1 ? "\n" : " ");
22378
+ utf8_width_print(p->out, aWidth[i], azCol[ aMap[i] ]);
22379
+ sqlite3_fputs(i==nArg-1 ? "\n" : " ", p->out);
2298722380
}
2298822381
for(i=0; i<nArg; i++){
22989
- print_dashes(aWidth[i]);
22990
- oputz(i==nArg-1 ? "\n" : " ");
22382
+ print_dashes(p->out, aWidth[i]);
22383
+ sqlite3_fputs(i==nArg-1 ? "\n" : " ", p->out);
2299122384
}
2299222385
}
2299322386
2299422387
/* If there is no data, exit early. */
2299522388
if( azArg==0 ) break;
@@ -23003,21 +22396,21 @@
2300322396
w = strlenChar(zVal);
2300422397
zSep = " ";
2300522398
}
2300622399
if( i==iIndent && p->aiIndent && p->pStmt ){
2300722400
if( p->iIndent<p->nIndent ){
23008
- oputf("%*.s", p->aiIndent[p->iIndent], "");
22401
+ sqlite3_fprintf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
2300922402
}
2301022403
p->iIndent++;
2301122404
}
23012
- utf8_width_print(w, zVal ? zVal : p->nullValue);
23013
- oputz(i==nArg-1 ? "\n" : zSep);
22405
+ utf8_width_print(p->out, w, zVal ? zVal : p->nullValue);
22406
+ sqlite3_fputs(i==nArg-1 ? "\n" : zSep, p->out);
2301422407
}
2301522408
break;
2301622409
}
2301722410
case MODE_Semi: { /* .schema and .fullschema output */
23018
- printSchemaLine(azArg[0], ";\n");
22411
+ printSchemaLine(p->out, azArg[0], ";\n");
2301922412
break;
2302022413
}
2302122414
case MODE_Pretty: { /* .schema and .fullschema with --indent */
2302222415
char *z;
2302322416
int j;
@@ -23028,11 +22421,11 @@
2302822421
assert( nArg==1 );
2302922422
if( azArg[0]==0 ) break;
2303022423
if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
2303122424
|| sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
2303222425
){
23033
- oputf("%s;\n", azArg[0]);
22426
+ sqlite3_fprintf(p->out, "%s;\n", azArg[0]);
2303422427
break;
2303522428
}
2303622429
z = sqlite3_mprintf("%s", azArg[0]);
2303722430
shell_check_oom(z);
2303822431
j = 0;
@@ -23061,255 +22454,265 @@
2306122454
}else if( c=='(' ){
2306222455
nParen++;
2306322456
}else if( c==')' ){
2306422457
nParen--;
2306522458
if( nLine>0 && nParen==0 && j>0 ){
23066
- printSchemaLineN(z, j, "\n");
22459
+ printSchemaLineN(p->out, z, j, "\n");
2306722460
j = 0;
2306822461
}
2306922462
}
2307022463
z[j++] = c;
2307122464
if( nParen==1 && cEnd==0
2307222465
&& (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
2307322466
){
2307422467
if( c=='\n' ) j--;
23075
- printSchemaLineN(z, j, "\n ");
22468
+ printSchemaLineN(p->out, z, j, "\n ");
2307622469
j = 0;
2307722470
nLine++;
2307822471
while( IsSpace(z[i+1]) ){ i++; }
2307922472
}
2308022473
}
2308122474
z[j] = 0;
2308222475
}
23083
- printSchemaLine(z, ";\n");
22476
+ printSchemaLine(p->out, z, ";\n");
2308422477
sqlite3_free(z);
2308522478
break;
2308622479
}
2308722480
case MODE_List: {
2308822481
if( p->cnt++==0 && p->showHeader ){
2308922482
for(i=0; i<nArg; i++){
23090
- oputf("%s%s",azCol[i], i==nArg-1 ? p->rowSeparator : p->colSeparator);
22483
+ sqlite3_fprintf(p->out, "%s%s", azCol[i],
22484
+ i==nArg-1 ? p->rowSeparator : p->colSeparator);
2309122485
}
2309222486
}
2309322487
if( azArg==0 ) break;
2309422488
for(i=0; i<nArg; i++){
2309522489
char *z = azArg[i];
2309622490
if( z==0 ) z = p->nullValue;
23097
- oputz(z);
23098
- oputz((i<nArg-1)? p->colSeparator : p->rowSeparator);
22491
+ sqlite3_fputs(z, p->out);
22492
+ sqlite3_fputs((i<nArg-1)? p->colSeparator : p->rowSeparator, p->out);
2309922493
}
2310022494
break;
2310122495
}
22496
+ case MODE_Www:
2310222497
case MODE_Html: {
23103
- if( p->cnt++==0 && p->showHeader ){
23104
- oputz("<TR>");
22498
+ if( p->cnt==0 && p->cMode==MODE_Www ){
22499
+ sqlite3_fputs(
22500
+ "</PRE>\n"
22501
+ "<TABLE border='1' cellspacing='0' cellpadding='2'>\n"
22502
+ ,p->out
22503
+ );
22504
+ }
22505
+ if( p->cnt==0 && (p->showHeader || p->cMode==MODE_Www) ){
22506
+ sqlite3_fputs("<TR>", p->out);
2310522507
for(i=0; i<nArg; i++){
23106
- oputz("<TH>");
23107
- output_html_string(azCol[i]);
23108
- oputz("</TH>\n");
22508
+ sqlite3_fputs("<TH>", p->out);
22509
+ output_html_string(p->out, azCol[i]);
22510
+ sqlite3_fputs("</TH>\n", p->out);
2310922511
}
23110
- oputz("</TR>\n");
22512
+ sqlite3_fputs("</TR>\n", p->out);
2311122513
}
22514
+ p->cnt++;
2311222515
if( azArg==0 ) break;
23113
- oputz("<TR>");
22516
+ sqlite3_fputs("<TR>", p->out);
2311422517
for(i=0; i<nArg; i++){
23115
- oputz("<TD>");
23116
- output_html_string(azArg[i] ? azArg[i] : p->nullValue);
23117
- oputz("</TD>\n");
22518
+ sqlite3_fputs("<TD>", p->out);
22519
+ output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
22520
+ sqlite3_fputs("</TD>\n", p->out);
2311822521
}
23119
- oputz("</TR>\n");
22522
+ sqlite3_fputs("</TR>\n", p->out);
2312022523
break;
2312122524
}
2312222525
case MODE_Tcl: {
2312322526
if( p->cnt++==0 && p->showHeader ){
2312422527
for(i=0; i<nArg; i++){
23125
- output_c_string(azCol[i] ? azCol[i] : "");
23126
- if(i<nArg-1) oputz(p->colSeparator);
22528
+ output_c_string(p->out, azCol[i] ? azCol[i] : "");
22529
+ if(i<nArg-1) sqlite3_fputs(p->colSeparator, p->out);
2312722530
}
23128
- oputz(p->rowSeparator);
22531
+ sqlite3_fputs(p->rowSeparator, p->out);
2312922532
}
2313022533
if( azArg==0 ) break;
2313122534
for(i=0; i<nArg; i++){
23132
- output_c_string(azArg[i] ? azArg[i] : p->nullValue);
23133
- if(i<nArg-1) oputz(p->colSeparator);
22535
+ output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
22536
+ if(i<nArg-1) sqlite3_fputs(p->colSeparator, p->out);
2313422537
}
23135
- oputz(p->rowSeparator);
22538
+ sqlite3_fputs(p->rowSeparator, p->out);
2313622539
break;
2313722540
}
2313822541
case MODE_Csv: {
23139
- setBinaryMode(p->out, 1);
22542
+ sqlite3_fsetmode(p->out, _O_BINARY);
2314022543
if( p->cnt++==0 && p->showHeader ){
2314122544
for(i=0; i<nArg; i++){
2314222545
output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
2314322546
}
23144
- oputz(p->rowSeparator);
22547
+ sqlite3_fputs(p->rowSeparator, p->out);
2314522548
}
2314622549
if( nArg>0 ){
2314722550
for(i=0; i<nArg; i++){
2314822551
output_csv(p, azArg[i], i<nArg-1);
2314922552
}
23150
- oputz(p->rowSeparator);
22553
+ sqlite3_fputs(p->rowSeparator, p->out);
2315122554
}
23152
- setTextMode(p->out, 1);
22555
+ sqlite3_fsetmode(p->out, _O_TEXT);
2315322556
break;
2315422557
}
2315522558
case MODE_Insert: {
2315622559
if( azArg==0 ) break;
23157
- oputf("INSERT INTO %s",p->zDestTable);
22560
+ sqlite3_fprintf(p->out, "INSERT INTO %s",p->zDestTable);
2315822561
if( p->showHeader ){
23159
- oputz("(");
22562
+ sqlite3_fputs("(", p->out);
2316022563
for(i=0; i<nArg; i++){
23161
- if( i>0 ) oputz(",");
22564
+ if( i>0 ) sqlite3_fputs(",", p->out);
2316222565
if( quoteChar(azCol[i]) ){
2316322566
char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
2316422567
shell_check_oom(z);
23165
- oputz(z);
22568
+ sqlite3_fputs(z, p->out);
2316622569
sqlite3_free(z);
2316722570
}else{
23168
- oputf("%s", azCol[i]);
22571
+ sqlite3_fprintf(p->out, "%s", azCol[i]);
2316922572
}
2317022573
}
23171
- oputz(")");
22574
+ sqlite3_fputs(")", p->out);
2317222575
}
2317322576
p->cnt++;
2317422577
for(i=0; i<nArg; i++){
23175
- oputz(i>0 ? "," : " VALUES(");
22578
+ sqlite3_fputs(i>0 ? "," : " VALUES(", p->out);
2317622579
if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
23177
- oputz("NULL");
22580
+ sqlite3_fputs("NULL", p->out);
2317822581
}else if( aiType && aiType[i]==SQLITE_TEXT ){
2317922582
if( ShellHasFlag(p, SHFLG_Newlines) ){
23180
- output_quoted_string(azArg[i]);
22583
+ output_quoted_string(p->out, azArg[i]);
2318122584
}else{
23182
- output_quoted_escaped_string(azArg[i]);
22585
+ output_quoted_escaped_string(p->out, azArg[i]);
2318322586
}
2318422587
}else if( aiType && aiType[i]==SQLITE_INTEGER ){
23185
- oputz(azArg[i]);
22588
+ sqlite3_fputs(azArg[i], p->out);
2318622589
}else if( aiType && aiType[i]==SQLITE_FLOAT ){
2318722590
char z[50];
2318822591
double r = sqlite3_column_double(p->pStmt, i);
2318922592
sqlite3_uint64 ur;
2319022593
memcpy(&ur,&r,sizeof(r));
2319122594
if( ur==0x7ff0000000000000LL ){
23192
- oputz("9.0e+999");
22595
+ sqlite3_fputs("9.0e+999", p->out);
2319322596
}else if( ur==0xfff0000000000000LL ){
23194
- oputz("-9.0e+999");
22597
+ sqlite3_fputs("-9.0e+999", p->out);
2319522598
}else{
2319622599
sqlite3_int64 ir = (sqlite3_int64)r;
2319722600
if( r==(double)ir ){
2319822601
sqlite3_snprintf(50,z,"%lld.0", ir);
2319922602
}else{
2320022603
sqlite3_snprintf(50,z,"%!.20g", r);
2320122604
}
23202
- oputz(z);
22605
+ sqlite3_fputs(z, p->out);
2320322606
}
2320422607
}else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2320522608
const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2320622609
int nBlob = sqlite3_column_bytes(p->pStmt, i);
23207
- output_hex_blob(pBlob, nBlob);
22610
+ output_hex_blob(p->out, pBlob, nBlob);
2320822611
}else if( isNumber(azArg[i], 0) ){
23209
- oputz(azArg[i]);
22612
+ sqlite3_fputs(azArg[i], p->out);
2321022613
}else if( ShellHasFlag(p, SHFLG_Newlines) ){
23211
- output_quoted_string(azArg[i]);
22614
+ output_quoted_string(p->out, azArg[i]);
2321222615
}else{
23213
- output_quoted_escaped_string(azArg[i]);
22616
+ output_quoted_escaped_string(p->out, azArg[i]);
2321422617
}
2321522618
}
23216
- oputz(");\n");
22619
+ sqlite3_fputs(");\n", p->out);
2321722620
break;
2321822621
}
2321922622
case MODE_Json: {
2322022623
if( azArg==0 ) break;
2322122624
if( p->cnt==0 ){
23222
- fputs("[{", p->out);
22625
+ sqlite3_fputs("[{", p->out);
2322322626
}else{
23224
- fputs(",\n{", p->out);
22627
+ sqlite3_fputs(",\n{", p->out);
2322522628
}
2322622629
p->cnt++;
2322722630
for(i=0; i<nArg; i++){
23228
- output_json_string(azCol[i], -1);
23229
- oputz(":");
22631
+ output_json_string(p->out, azCol[i], -1);
22632
+ sqlite3_fputs(":", p->out);
2323022633
if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
23231
- oputz("null");
22634
+ sqlite3_fputs("null", p->out);
2323222635
}else if( aiType && aiType[i]==SQLITE_FLOAT ){
2323322636
char z[50];
2323422637
double r = sqlite3_column_double(p->pStmt, i);
2323522638
sqlite3_uint64 ur;
2323622639
memcpy(&ur,&r,sizeof(r));
2323722640
if( ur==0x7ff0000000000000LL ){
23238
- oputz("9.0e+999");
22641
+ sqlite3_fputs("9.0e+999", p->out);
2323922642
}else if( ur==0xfff0000000000000LL ){
23240
- oputz("-9.0e+999");
22643
+ sqlite3_fputs("-9.0e+999", p->out);
2324122644
}else{
2324222645
sqlite3_snprintf(50,z,"%!.20g", r);
23243
- oputz(z);
22646
+ sqlite3_fputs(z, p->out);
2324422647
}
2324522648
}else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2324622649
const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2324722650
int nBlob = sqlite3_column_bytes(p->pStmt, i);
23248
- output_json_string(pBlob, nBlob);
22651
+ output_json_string(p->out, pBlob, nBlob);
2324922652
}else if( aiType && aiType[i]==SQLITE_TEXT ){
23250
- output_json_string(azArg[i], -1);
22653
+ output_json_string(p->out, azArg[i], -1);
2325122654
}else{
23252
- oputz(azArg[i]);
22655
+ sqlite3_fputs(azArg[i], p->out);
2325322656
}
2325422657
if( i<nArg-1 ){
23255
- oputz(",");
22658
+ sqlite3_fputs(",", p->out);
2325622659
}
2325722660
}
23258
- oputz("}");
22661
+ sqlite3_fputs("}", p->out);
2325922662
break;
2326022663
}
2326122664
case MODE_Quote: {
2326222665
if( azArg==0 ) break;
2326322666
if( p->cnt==0 && p->showHeader ){
2326422667
for(i=0; i<nArg; i++){
23265
- if( i>0 ) fputs(p->colSeparator, p->out);
23266
- output_quoted_string(azCol[i]);
22668
+ if( i>0 ) sqlite3_fputs(p->colSeparator, p->out);
22669
+ output_quoted_string(p->out, azCol[i]);
2326722670
}
23268
- fputs(p->rowSeparator, p->out);
22671
+ sqlite3_fputs(p->rowSeparator, p->out);
2326922672
}
2327022673
p->cnt++;
2327122674
for(i=0; i<nArg; i++){
23272
- if( i>0 ) fputs(p->colSeparator, p->out);
22675
+ if( i>0 ) sqlite3_fputs(p->colSeparator, p->out);
2327322676
if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
23274
- oputz("NULL");
22677
+ sqlite3_fputs("NULL", p->out);
2327522678
}else if( aiType && aiType[i]==SQLITE_TEXT ){
23276
- output_quoted_string(azArg[i]);
22679
+ output_quoted_string(p->out, azArg[i]);
2327722680
}else if( aiType && aiType[i]==SQLITE_INTEGER ){
23278
- oputz(azArg[i]);
22681
+ sqlite3_fputs(azArg[i], p->out);
2327922682
}else if( aiType && aiType[i]==SQLITE_FLOAT ){
2328022683
char z[50];
2328122684
double r = sqlite3_column_double(p->pStmt, i);
2328222685
sqlite3_snprintf(50,z,"%!.20g", r);
23283
- oputz(z);
22686
+ sqlite3_fputs(z, p->out);
2328422687
}else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2328522688
const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2328622689
int nBlob = sqlite3_column_bytes(p->pStmt, i);
23287
- output_hex_blob(pBlob, nBlob);
22690
+ output_hex_blob(p->out, pBlob, nBlob);
2328822691
}else if( isNumber(azArg[i], 0) ){
23289
- oputz(azArg[i]);
22692
+ sqlite3_fputs(azArg[i], p->out);
2329022693
}else{
23291
- output_quoted_string(azArg[i]);
22694
+ output_quoted_string(p->out, azArg[i]);
2329222695
}
2329322696
}
23294
- fputs(p->rowSeparator, p->out);
22697
+ sqlite3_fputs(p->rowSeparator, p->out);
2329522698
break;
2329622699
}
2329722700
case MODE_Ascii: {
2329822701
if( p->cnt++==0 && p->showHeader ){
2329922702
for(i=0; i<nArg; i++){
23300
- if( i>0 ) oputz(p->colSeparator);
23301
- oputz(azCol[i] ? azCol[i] : "");
22703
+ if( i>0 ) sqlite3_fputs(p->colSeparator, p->out);
22704
+ sqlite3_fputs(azCol[i] ? azCol[i] : "", p->out);
2330222705
}
23303
- oputz(p->rowSeparator);
22706
+ sqlite3_fputs(p->rowSeparator, p->out);
2330422707
}
2330522708
if( azArg==0 ) break;
2330622709
for(i=0; i<nArg; i++){
23307
- if( i>0 ) oputz(p->colSeparator);
23308
- oputz(azArg[i] ? azArg[i] : p->nullValue);
22710
+ if( i>0 ) sqlite3_fputs(p->colSeparator, p->out);
22711
+ sqlite3_fputs(azArg[i] ? azArg[i] : p->nullValue, p->out);
2330922712
}
23310
- oputz(p->rowSeparator);
22713
+ sqlite3_fputs(p->rowSeparator, p->out);
2331122714
break;
2331222715
}
2331322716
case MODE_EQP: {
2331422717
eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
2331522718
break;
@@ -23384,11 +22787,11 @@
2338422787
"INSERT INTO selftest(tno,op,cmd,ans)"
2338522788
" SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
2338622789
"DROP TABLE [_shell$self];"
2338722790
,0,0,&zErrMsg);
2338822791
if( zErrMsg ){
23389
- eputf("SELFTEST initialization failure: %s\n", zErrMsg);
22792
+ sqlite3_fprintf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
2339022793
sqlite3_free(zErrMsg);
2339122794
}
2339222795
sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
2339322796
}
2339422797
@@ -23487,36 +22890,37 @@
2348722890
int i;
2348822891
const char *z;
2348922892
rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
2349022893
if( rc!=SQLITE_OK || !pSelect ){
2349122894
char *zContext = shell_error_context(zSelect, p->db);
23492
- oputf("/**** ERROR: (%d) %s *****/\n%s",
22895
+ sqlite3_fprintf(p->out, "/**** ERROR: (%d) %s *****/\n%s",
2349322896
rc, sqlite3_errmsg(p->db), zContext);
2349422897
sqlite3_free(zContext);
2349522898
if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2349622899
return rc;
2349722900
}
2349822901
rc = sqlite3_step(pSelect);
2349922902
nResult = sqlite3_column_count(pSelect);
2350022903
while( rc==SQLITE_ROW ){
2350122904
z = (const char*)sqlite3_column_text(pSelect, 0);
23502
- oputf("%s", z);
22905
+ sqlite3_fprintf(p->out, "%s", z);
2350322906
for(i=1; i<nResult; i++){
23504
- oputf(",%s", sqlite3_column_text(pSelect, i));
22907
+ sqlite3_fprintf(p->out, ",%s", sqlite3_column_text(pSelect, i));
2350522908
}
2350622909
if( z==0 ) z = "";
2350722910
while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
2350822911
if( z[0] ){
23509
- oputz("\n;\n");
22912
+ sqlite3_fputs("\n;\n", p->out);
2351022913
}else{
23511
- oputz(";\n");
22914
+ sqlite3_fputs(";\n", p->out);
2351222915
}
2351322916
rc = sqlite3_step(pSelect);
2351422917
}
2351522918
rc = sqlite3_finalize(pSelect);
2351622919
if( rc!=SQLITE_OK ){
23517
- oputf("/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db));
22920
+ sqlite3_fprintf(p->out, "/**** ERROR: (%d) %s *****/\n",
22921
+ rc, sqlite3_errmsg(p->db));
2351822922
if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2351922923
}
2352022924
return rc;
2352122925
}
2352222926
@@ -23548,17 +22952,17 @@
2354822952
2354922953
#ifdef __linux__
2355022954
/*
2355122955
** Attempt to display I/O stats on Linux using /proc/PID/io
2355222956
*/
23553
-static void displayLinuxIoStats(void){
22957
+static void displayLinuxIoStats(FILE *out){
2355422958
FILE *in;
2355522959
char z[200];
2355622960
sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
23557
- in = fopen(z, "rb");
22961
+ in = sqlite3_fopen(z, "rb");
2355822962
if( in==0 ) return;
23559
- while( fgets(z, sizeof(z), in)!=0 ){
22963
+ while( sqlite3_fgets(z, sizeof(z), in)!=0 ){
2356022964
static const struct {
2356122965
const char *zPattern;
2356222966
const char *zDesc;
2356322967
} aTrans[] = {
2356422968
{ "rchar: ", "Bytes received by read():" },
@@ -23571,11 +22975,11 @@
2357122975
};
2357222976
int i;
2357322977
for(i=0; i<ArraySize(aTrans); i++){
2357422978
int n = strlen30(aTrans[i].zPattern);
2357522979
if( cli_strncmp(aTrans[i].zPattern, z, n)==0 ){
23576
- oputf("%-36s %s", aTrans[i].zDesc, &z[n]);
22980
+ sqlite3_fprintf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
2357722981
break;
2357822982
}
2357922983
}
2358022984
}
2358122985
fclose(in);
@@ -23584,10 +22988,11 @@
2358422988
2358522989
/*
2358622990
** Display a single line of status using 64-bit values.
2358722991
*/
2358822992
static void displayStatLine(
22993
+ FILE *out, /* Write to this channel */
2358922994
char *zLabel, /* Label for this one line */
2359022995
char *zFormat, /* Format for the result */
2359122996
int iStatusCtrl, /* Which status to display */
2359222997
int bReset /* True to reset the stats */
2359322998
){
@@ -23602,11 +23007,11 @@
2360223007
if( nPercent>1 ){
2360323008
sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
2360423009
}else{
2360523010
sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
2360623011
}
23607
- oputf("%-36s %s\n", zLabel, zLine);
23012
+ sqlite3_fprintf(out, "%-36s %s\n", zLabel, zLine);
2360823013
}
2360923014
2361023015
/*
2361123016
** Display memory stats.
2361223017
*/
@@ -23615,130 +23020,152 @@
2361523020
ShellState *pArg, /* Pointer to ShellState */
2361623021
int bReset /* True to reset the stats */
2361723022
){
2361823023
int iCur;
2361923024
int iHiwtr;
23025
+ FILE *out;
2362023026
if( pArg==0 || pArg->out==0 ) return 0;
23027
+ out = pArg->out;
2362123028
2362223029
if( pArg->pStmt && pArg->statsOn==2 ){
2362323030
int nCol, i, x;
2362423031
sqlite3_stmt *pStmt = pArg->pStmt;
2362523032
char z[100];
2362623033
nCol = sqlite3_column_count(pStmt);
23627
- oputf("%-36s %d\n", "Number of output columns:", nCol);
23034
+ sqlite3_fprintf(out, "%-36s %d\n", "Number of output columns:", nCol);
2362823035
for(i=0; i<nCol; i++){
2362923036
sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
23630
- oputf("%-36s %s\n", z, sqlite3_column_name(pStmt,i));
23037
+ sqlite3_fprintf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
2363123038
#ifndef SQLITE_OMIT_DECLTYPE
2363223039
sqlite3_snprintf(30, z+x, "declared type:");
23633
- oputf("%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
23040
+ sqlite3_fprintf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
2363423041
#endif
2363523042
#ifdef SQLITE_ENABLE_COLUMN_METADATA
2363623043
sqlite3_snprintf(30, z+x, "database name:");
23637
- oputf("%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
23044
+ sqlite3_fprintf(out, "%-36s %s\n", z,
23045
+ sqlite3_column_database_name(pStmt,i));
2363823046
sqlite3_snprintf(30, z+x, "table name:");
23639
- oputf("%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
23047
+ sqlite3_fprintf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
2364023048
sqlite3_snprintf(30, z+x, "origin name:");
23641
- oputf("%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
23049
+ sqlite3_fprintf(out, "%-36s %s\n", z,sqlite3_column_origin_name(pStmt,i));
2364223050
#endif
2364323051
}
2364423052
}
2364523053
2364623054
if( pArg->statsOn==3 ){
2364723055
if( pArg->pStmt ){
2364823056
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP,bReset);
23649
- oputf("VM-steps: %d\n", iCur);
23057
+ sqlite3_fprintf(out, "VM-steps: %d\n", iCur);
2365023058
}
2365123059
return 0;
2365223060
}
2365323061
23654
- displayStatLine("Memory Used:",
23062
+ displayStatLine(out, "Memory Used:",
2365523063
"%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
23656
- displayStatLine("Number of Outstanding Allocations:",
23064
+ displayStatLine(out, "Number of Outstanding Allocations:",
2365723065
"%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
2365823066
if( pArg->shellFlgs & SHFLG_Pagecache ){
23659
- displayStatLine("Number of Pcache Pages Used:",
23067
+ displayStatLine(out, "Number of Pcache Pages Used:",
2366023068
"%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
2366123069
}
23662
- displayStatLine("Number of Pcache Overflow Bytes:",
23070
+ displayStatLine(out, "Number of Pcache Overflow Bytes:",
2366323071
"%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
23664
- displayStatLine("Largest Allocation:",
23072
+ displayStatLine(out, "Largest Allocation:",
2366523073
"%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
23666
- displayStatLine("Largest Pcache Allocation:",
23074
+ displayStatLine(out, "Largest Pcache Allocation:",
2366723075
"%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
2366823076
#ifdef YYTRACKMAXSTACKDEPTH
23669
- displayStatLine("Deepest Parser Stack:",
23077
+ displayStatLine(out, "Deepest Parser Stack:",
2367023078
"%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
2367123079
#endif
2367223080
2367323081
if( db ){
2367423082
if( pArg->shellFlgs & SHFLG_Lookaside ){
2367523083
iHiwtr = iCur = -1;
2367623084
sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
2367723085
&iCur, &iHiwtr, bReset);
23678
- oputf("Lookaside Slots Used: %d (max %d)\n", iCur, iHiwtr);
23086
+ sqlite3_fprintf(out,
23087
+ "Lookaside Slots Used: %d (max %d)\n", iCur, iHiwtr);
2367923088
sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
2368023089
&iCur, &iHiwtr, bReset);
23681
- oputf("Successful lookaside attempts: %d\n", iHiwtr);
23090
+ sqlite3_fprintf(out,
23091
+ "Successful lookaside attempts: %d\n", iHiwtr);
2368223092
sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
2368323093
&iCur, &iHiwtr, bReset);
23684
- oputf("Lookaside failures due to size: %d\n", iHiwtr);
23094
+ sqlite3_fprintf(out,
23095
+ "Lookaside failures due to size: %d\n", iHiwtr);
2368523096
sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
2368623097
&iCur, &iHiwtr, bReset);
23687
- oputf("Lookaside failures due to OOM: %d\n", iHiwtr);
23098
+ sqlite3_fprintf(out,
23099
+ "Lookaside failures due to OOM: %d\n", iHiwtr);
2368823100
}
2368923101
iHiwtr = iCur = -1;
2369023102
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
23691
- oputf("Pager Heap Usage: %d bytes\n", iCur);
23103
+ sqlite3_fprintf(out,
23104
+ "Pager Heap Usage: %d bytes\n", iCur);
2369223105
iHiwtr = iCur = -1;
2369323106
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
23694
- oputf("Page cache hits: %d\n", iCur);
23107
+ sqlite3_fprintf(out,
23108
+ "Page cache hits: %d\n", iCur);
2369523109
iHiwtr = iCur = -1;
2369623110
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
23697
- oputf("Page cache misses: %d\n", iCur);
23111
+ sqlite3_fprintf(out,
23112
+ "Page cache misses: %d\n", iCur);
2369823113
iHiwtr = iCur = -1;
2369923114
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
23700
- oputf("Page cache writes: %d\n", iCur);
23115
+ sqlite3_fprintf(out,
23116
+ "Page cache writes: %d\n", iCur);
2370123117
iHiwtr = iCur = -1;
2370223118
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
23703
- oputf("Page cache spills: %d\n", iCur);
23119
+ sqlite3_fprintf(out,
23120
+ "Page cache spills: %d\n", iCur);
2370423121
iHiwtr = iCur = -1;
2370523122
sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
23706
- oputf("Schema Heap Usage: %d bytes\n", iCur);
23123
+ sqlite3_fprintf(out,
23124
+ "Schema Heap Usage: %d bytes\n", iCur);
2370723125
iHiwtr = iCur = -1;
2370823126
sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
23709
- oputf("Statement Heap/Lookaside Usage: %d bytes\n", iCur);
23127
+ sqlite3_fprintf(out,
23128
+ "Statement Heap/Lookaside Usage: %d bytes\n", iCur);
2371023129
}
2371123130
2371223131
if( pArg->pStmt ){
2371323132
int iHit, iMiss;
2371423133
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
2371523134
bReset);
23716
- oputf("Fullscan Steps: %d\n", iCur);
23135
+ sqlite3_fprintf(out,
23136
+ "Fullscan Steps: %d\n", iCur);
2371723137
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
23718
- oputf("Sort Operations: %d\n", iCur);
23138
+ sqlite3_fprintf(out,
23139
+ "Sort Operations: %d\n", iCur);
2371923140
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
23720
- oputf("Autoindex Inserts: %d\n", iCur);
23141
+ sqlite3_fprintf(out,
23142
+ "Autoindex Inserts: %d\n", iCur);
2372123143
iHit = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_HIT,
2372223144
bReset);
2372323145
iMiss = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_MISS,
2372423146
bReset);
2372523147
if( iHit || iMiss ){
23726
- oputf("Bloom filter bypass taken: %d/%d\n", iHit, iHit+iMiss);
23148
+ sqlite3_fprintf(out,
23149
+ "Bloom filter bypass taken: %d/%d\n", iHit, iHit+iMiss);
2372723150
}
2372823151
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
23729
- oputf("Virtual Machine Steps: %d\n", iCur);
23152
+ sqlite3_fprintf(out,
23153
+ "Virtual Machine Steps: %d\n", iCur);
2373023154
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
23731
- oputf("Reprepare operations: %d\n", iCur);
23155
+ sqlite3_fprintf(out,
23156
+ "Reprepare operations: %d\n", iCur);
2373223157
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
23733
- oputf("Number of times run: %d\n", iCur);
23158
+ sqlite3_fprintf(out,
23159
+ "Number of times run: %d\n", iCur);
2373423160
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
23735
- oputf("Memory used by prepared stmt: %d\n", iCur);
23161
+ sqlite3_fprintf(out,
23162
+ "Memory used by prepared stmt: %d\n", iCur);
2373623163
}
2373723164
2373823165
#ifdef __linux__
23739
- displayLinuxIoStats();
23166
+ displayLinuxIoStats(pArg->out);
2374023167
#endif
2374123168
2374223169
/* Do not remove this machine readable comment: extra-stats-output-here */
2374323170
2374423171
return 0;
@@ -24130,21 +23557,21 @@
2413023557
#define BOX_1234 "\342\224\274" /* U+253c -|- */
2413123558
2413223559
/* Draw horizontal line N characters long using unicode box
2413323560
** characters
2413423561
*/
24135
-static void print_box_line(int N){
23562
+static void print_box_line(FILE *out, int N){
2413623563
const char zDash[] =
2413723564
BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
2413823565
BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
2413923566
const int nDash = sizeof(zDash) - 1;
2414023567
N *= 3;
2414123568
while( N>nDash ){
24142
- oputz(zDash);
23569
+ sqlite3_fputs(zDash, out);
2414323570
N -= nDash;
2414423571
}
24145
- oputf("%.*s", N, zDash);
23572
+ sqlite3_fprintf(out, "%.*s", N, zDash);
2414623573
}
2414723574
2414823575
/*
2414923576
** Draw a horizontal separator for a MODE_Box table.
2415023577
*/
@@ -24155,19 +23582,19 @@
2415523582
const char *zSep2,
2415623583
const char *zSep3
2415723584
){
2415823585
int i;
2415923586
if( nArg>0 ){
24160
- oputz(zSep1);
24161
- print_box_line(p->actualWidth[0]+2);
23587
+ sqlite3_fputs(zSep1, p->out);
23588
+ print_box_line(p->out, p->actualWidth[0]+2);
2416223589
for(i=1; i<nArg; i++){
24163
- oputz(zSep2);
24164
- print_box_line(p->actualWidth[i]+2);
23590
+ sqlite3_fputs(zSep2, p->out);
23591
+ print_box_line(p->out, p->actualWidth[i]+2);
2416523592
}
24166
- oputz(zSep3);
23593
+ sqlite3_fputs(zSep3, p->out);
2416723594
}
24168
- oputz("\n");
23595
+ sqlite3_fputs("\n", p->out);
2416923596
}
2417023597
2417123598
/*
2417223599
** z[] is a line of text that is to be displayed the .mode box or table or
2417323600
** similar tabular formats. z[] might contain control characters such
@@ -24197,16 +23624,26 @@
2419723624
}
2419823625
if( mxWidth<0 ) mxWidth = -mxWidth;
2419923626
if( mxWidth==0 ) mxWidth = 1000000;
2420023627
i = j = n = 0;
2420123628
while( n<mxWidth ){
24202
- if( z[i]>=' ' ){
23629
+ unsigned char c = z[i];
23630
+ if( c>=0xc0 ){
23631
+ int u;
23632
+ int len = decodeUtf8(&z[i], &u);
23633
+ i += len;
23634
+ j += len;
23635
+ n += cli_wcwidth(u);
23636
+ continue;
23637
+ }
23638
+ if( c>=' ' ){
2420323639
n++;
24204
- do{ i++; j++; }while( (z[i]&0xc0)==0x80 );
23640
+ i++;
23641
+ j++;
2420523642
continue;
2420623643
}
24207
- if( z[i]=='\t' ){
23644
+ if( c=='\t' ){
2420823645
do{
2420923646
n++;
2421023647
j++;
2421123648
}while( (n&7)!=0 && n<mxWidth );
2421223649
i++;
@@ -24244,13 +23681,21 @@
2424423681
}
2424523682
zOut = malloc( j+1 );
2424623683
shell_check_oom(zOut);
2424723684
i = j = n = 0;
2424823685
while( i<k ){
24249
- if( z[i]>=' ' ){
23686
+ unsigned char c = z[i];
23687
+ if( c>=0xc0 ){
23688
+ int u;
23689
+ int len = decodeUtf8(&z[i], &u);
23690
+ do{ zOut[j++] = z[i++]; }while( (--len)>0 );
23691
+ n += cli_wcwidth(u);
23692
+ continue;
23693
+ }
23694
+ if( c>=' ' ){
2425023695
n++;
24251
- do{ zOut[j++] = z[i++]; }while( (z[i]&0xc0)==0x80 );
23696
+ zOut[j++] = z[i++];
2425223697
continue;
2425323698
}
2425423699
if( z[i]=='\t' ){
2425523700
do{
2425623701
n++;
@@ -24426,97 +23871,99 @@
2442623871
rowSep = "\n";
2442723872
if( p->showHeader ){
2442823873
for(i=0; i<nColumn; i++){
2442923874
w = p->actualWidth[i];
2443023875
if( p->colWidth[i]<0 ) w = -w;
24431
- utf8_width_print(w, azData[i]);
24432
- fputs(i==nColumn-1?"\n":" ", p->out);
23876
+ utf8_width_print(p->out, w, azData[i]);
23877
+ sqlite3_fputs(i==nColumn-1?"\n":" ", p->out);
2443323878
}
2443423879
for(i=0; i<nColumn; i++){
24435
- print_dashes(p->actualWidth[i]);
24436
- fputs(i==nColumn-1?"\n":" ", p->out);
23880
+ print_dashes(p->out, p->actualWidth[i]);
23881
+ sqlite3_fputs(i==nColumn-1?"\n":" ", p->out);
2443723882
}
2443823883
}
2443923884
break;
2444023885
}
2444123886
case MODE_Table: {
2444223887
colSep = " | ";
2444323888
rowSep = " |\n";
2444423889
print_row_separator(p, nColumn, "+");
24445
- fputs("| ", p->out);
23890
+ sqlite3_fputs("| ", p->out);
2444623891
for(i=0; i<nColumn; i++){
2444723892
w = p->actualWidth[i];
2444823893
n = strlenChar(azData[i]);
24449
- oputf("%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
24450
- oputz(i==nColumn-1?" |\n":" | ");
23894
+ sqlite3_fprintf(p->out, "%*s%s%*s", (w-n)/2, "",
23895
+ azData[i], (w-n+1)/2, "");
23896
+ sqlite3_fputs(i==nColumn-1?" |\n":" | ", p->out);
2445123897
}
2445223898
print_row_separator(p, nColumn, "+");
2445323899
break;
2445423900
}
2445523901
case MODE_Markdown: {
2445623902
colSep = " | ";
2445723903
rowSep = " |\n";
24458
- fputs("| ", p->out);
23904
+ sqlite3_fputs("| ", p->out);
2445923905
for(i=0; i<nColumn; i++){
2446023906
w = p->actualWidth[i];
2446123907
n = strlenChar(azData[i]);
24462
- oputf("%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
24463
- oputz(i==nColumn-1?" |\n":" | ");
23908
+ sqlite3_fprintf(p->out, "%*s%s%*s", (w-n)/2, "",
23909
+ azData[i], (w-n+1)/2, "");
23910
+ sqlite3_fputs(i==nColumn-1?" |\n":" | ", p->out);
2446423911
}
2446523912
print_row_separator(p, nColumn, "|");
2446623913
break;
2446723914
}
2446823915
case MODE_Box: {
2446923916
colSep = " " BOX_13 " ";
2447023917
rowSep = " " BOX_13 "\n";
2447123918
print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
24472
- oputz(BOX_13 " ");
23919
+ sqlite3_fputs(BOX_13 " ", p->out);
2447323920
for(i=0; i<nColumn; i++){
2447423921
w = p->actualWidth[i];
2447523922
n = strlenChar(azData[i]);
24476
- oputf("%*s%s%*s%s",
23923
+ sqlite3_fprintf(p->out, "%*s%s%*s%s",
2447723924
(w-n)/2, "", azData[i], (w-n+1)/2, "",
2447823925
i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
2447923926
}
2448023927
print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
2448123928
break;
2448223929
}
2448323930
}
2448423931
for(i=nColumn, j=0; i<nTotal; i++, j++){
2448523932
if( j==0 && p->cMode!=MODE_Column ){
24486
- oputz(p->cMode==MODE_Box?BOX_13" ":"| ");
23933
+ sqlite3_fputs(p->cMode==MODE_Box?BOX_13" ":"| ", p->out);
2448723934
}
2448823935
z = azData[i];
2448923936
if( z==0 ) z = p->nullValue;
2449023937
w = p->actualWidth[j];
2449123938
if( p->colWidth[j]<0 ) w = -w;
24492
- utf8_width_print(w, z);
23939
+ utf8_width_print(p->out, w, z);
2449323940
if( j==nColumn-1 ){
24494
- oputz(rowSep);
23941
+ sqlite3_fputs(rowSep, p->out);
2449523942
if( bMultiLineRowExists && abRowDiv[i/nColumn-1] && i+1<nTotal ){
2449623943
if( p->cMode==MODE_Table ){
2449723944
print_row_separator(p, nColumn, "+");
2449823945
}else if( p->cMode==MODE_Box ){
2449923946
print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
2450023947
}else if( p->cMode==MODE_Column ){
24501
- oputz("\n");
23948
+ sqlite3_fputs("\n", p->out);
2450223949
}
2450323950
}
2450423951
j = -1;
2450523952
if( seenInterrupt ) goto columnar_end;
2450623953
}else{
24507
- oputz(colSep);
23954
+ sqlite3_fputs(colSep, p->out);
2450823955
}
2450923956
}
2451023957
if( p->cMode==MODE_Table ){
2451123958
print_row_separator(p, nColumn, "+");
2451223959
}else if( p->cMode==MODE_Box ){
2451323960
print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
2451423961
}
2451523962
columnar_end:
2451623963
if( seenInterrupt ){
24517
- oputz("Interrupt\n");
23964
+ sqlite3_fputs("Interrupt\n", p->out);
2451823965
}
2451923966
nData = (nRow+1)*nColumn;
2452023967
for(i=0; i<nData; i++){
2452123968
z = azData[i];
2452223969
if( z!=zEmpty && z!=zShowNull ) free(azData[i]);
@@ -24599,11 +24046,13 @@
2459924046
}
2460024047
}
2460124048
} while( SQLITE_ROW == rc );
2460224049
sqlite3_free(pData);
2460324050
if( pArg->cMode==MODE_Json ){
24604
- fputs("]\n", pArg->out);
24051
+ sqlite3_fputs("]\n", pArg->out);
24052
+ }else if( pArg->cMode==MODE_Www ){
24053
+ sqlite3_fputs("</TABLE>\n<PRE>\n", pArg->out);
2460524054
}else if( pArg->cMode==MODE_Count ){
2460624055
char zBuf[200];
2460724056
sqlite3_snprintf(sizeof(zBuf), zBuf, "%llu row%s\n",
2460824057
nRow, nRow!=1 ? "s" : "");
2460924058
printf("%s", zBuf);
@@ -24648,10 +24097,11 @@
2464824097
int bCancel,
2464924098
char **pzErr
2465024099
){
2465124100
int rc = SQLITE_OK;
2465224101
sqlite3expert *p = pState->expert.pExpert;
24102
+ FILE *out = pState->out;
2465324103
assert( p );
2465424104
assert( bCancel || pzErr==0 || *pzErr==0 );
2465524105
if( bCancel==0 ){
2465624106
int bVerbose = pState->expert.bVerbose;
2465724107
@@ -24660,24 +24110,25 @@
2466024110
int nQuery = sqlite3_expert_count(p);
2466124111
int i;
2466224112
2466324113
if( bVerbose ){
2466424114
const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
24665
- oputz("-- Candidates -----------------------------\n");
24666
- oputf("%s\n", zCand);
24115
+ sqlite3_fputs("-- Candidates -----------------------------\n", out);
24116
+ sqlite3_fprintf(out, "%s\n", zCand);
2466724117
}
2466824118
for(i=0; i<nQuery; i++){
2466924119
const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
2467024120
const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
2467124121
const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
2467224122
if( zIdx==0 ) zIdx = "(no new indexes)\n";
2467324123
if( bVerbose ){
24674
- oputf("-- Query %d --------------------------------\n",i+1);
24675
- oputf("%s\n\n", zSql);
24124
+ sqlite3_fprintf(out,
24125
+ "-- Query %d --------------------------------\n"
24126
+ "%s\n\n"
24127
+ ,i+1, zSql);
2467624128
}
24677
- oputf("%s\n", zIdx);
24678
- oputf("%s\n", zEQP);
24129
+ sqlite3_fprintf(out, "%s\n%s\n", zIdx, zEQP);
2467924130
}
2468024131
}
2468124132
}
2468224133
sqlite3_expert_destroy(p);
2468324134
pState->expert.pExpert = 0;
@@ -24708,30 +24159,31 @@
2470824159
if( n>=2 && 0==cli_strncmp(z, "-verbose", n) ){
2470924160
pState->expert.bVerbose = 1;
2471024161
}
2471124162
else if( n>=2 && 0==cli_strncmp(z, "-sample", n) ){
2471224163
if( i==(nArg-1) ){
24713
- eputf("option requires an argument: %s\n", z);
24164
+ sqlite3_fprintf(stderr, "option requires an argument: %s\n", z);
2471424165
rc = SQLITE_ERROR;
2471524166
}else{
2471624167
iSample = (int)integerValue(azArg[++i]);
2471724168
if( iSample<0 || iSample>100 ){
24718
- eputf("value out of range: %s\n", azArg[i]);
24169
+ sqlite3_fprintf(stderr,"value out of range: %s\n", azArg[i]);
2471924170
rc = SQLITE_ERROR;
2472024171
}
2472124172
}
2472224173
}
2472324174
else{
24724
- eputf("unknown option: %s\n", z);
24175
+ sqlite3_fprintf(stderr,"unknown option: %s\n", z);
2472524176
rc = SQLITE_ERROR;
2472624177
}
2472724178
}
2472824179
2472924180
if( rc==SQLITE_OK ){
2473024181
pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
2473124182
if( pState->expert.pExpert==0 ){
24732
- eputf("sqlite3_expert_new: %s\n", zErr ? zErr : "out of memory");
24183
+ sqlite3_fprintf(stderr,
24184
+ "sqlite3_expert_new: %s\n", zErr ? zErr : "out of memory");
2473324185
rc = SQLITE_ERROR;
2473424186
}else{
2473524187
sqlite3_expert_config(
2473624188
pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
2473724189
);
@@ -25056,33 +24508,33 @@
2505624508
if( zType==0 ) return 0;
2505724509
dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
2505824510
noSys = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
2505924511
2506024512
if( cli_strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
25061
- if( !dataOnly ) oputz("DELETE FROM sqlite_sequence;\n");
24513
+ if( !dataOnly ) sqlite3_fputs("DELETE FROM sqlite_sequence;\n", p->out);
2506224514
}else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
25063
- if( !dataOnly ) oputz("ANALYZE sqlite_schema;\n");
24515
+ if( !dataOnly ) sqlite3_fputs("ANALYZE sqlite_schema;\n", p->out);
2506424516
}else if( cli_strncmp(zTable, "sqlite_", 7)==0 ){
2506524517
return 0;
2506624518
}else if( dataOnly ){
2506724519
/* no-op */
2506824520
}else if( cli_strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
2506924521
char *zIns;
2507024522
if( !p->writableSchema ){
25071
- oputz("PRAGMA writable_schema=ON;\n");
24523
+ sqlite3_fputs("PRAGMA writable_schema=ON;\n", p->out);
2507224524
p->writableSchema = 1;
2507324525
}
2507424526
zIns = sqlite3_mprintf(
2507524527
"INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
2507624528
"VALUES('table','%q','%q',0,'%q');",
2507724529
zTable, zTable, zSql);
2507824530
shell_check_oom(zIns);
25079
- oputf("%s\n", zIns);
24531
+ sqlite3_fprintf(p->out, "%s\n", zIns);
2508024532
sqlite3_free(zIns);
2508124533
return 0;
2508224534
}else{
25083
- printSchemaLine(zSql, ";\n");
24535
+ printSchemaLine(p->out, zSql, ";\n");
2508424536
}
2508524537
2508624538
if( cli_strcmp(zType, "table")==0 ){
2508724539
ShellText sSelect;
2508824540
ShellText sTable;
@@ -25136,11 +24588,11 @@
2513624588
savedMode = p->mode;
2513724589
p->zDestTable = sTable.z;
2513824590
p->mode = p->cMode = MODE_Insert;
2513924591
rc = shell_exec(p, sSelect.z, 0);
2514024592
if( (rc&0xff)==SQLITE_CORRUPT ){
25141
- oputz("/****** CORRUPTION ERROR *******/\n");
24593
+ sqlite3_fputs("/****** CORRUPTION ERROR *******/\n", p->out);
2514224594
toggleSelectOrder(p->db);
2514324595
shell_exec(p, sSelect.z, 0);
2514424596
toggleSelectOrder(p->db);
2514524597
}
2514624598
p->zDestTable = savedDestTable;
@@ -25167,22 +24619,22 @@
2516724619
char *zErr = 0;
2516824620
rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
2516924621
if( rc==SQLITE_CORRUPT ){
2517024622
char *zQ2;
2517124623
int len = strlen30(zQuery);
25172
- oputz("/****** CORRUPTION ERROR *******/\n");
24624
+ sqlite3_fputs("/****** CORRUPTION ERROR *******/\n", p->out);
2517324625
if( zErr ){
25174
- oputf("/****** %s ******/\n", zErr);
24626
+ sqlite3_fprintf(p->out, "/****** %s ******/\n", zErr);
2517524627
sqlite3_free(zErr);
2517624628
zErr = 0;
2517724629
}
2517824630
zQ2 = malloc( len+100 );
2517924631
if( zQ2==0 ) return rc;
2518024632
sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
2518124633
rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
2518224634
if( rc ){
25183
- oputf("/****** ERROR: %s ******/\n", zErr);
24635
+ sqlite3_fprintf(p->out, "/****** ERROR: %s ******/\n", zErr);
2518424636
}else{
2518524637
rc = SQLITE_CORRUPT;
2518624638
}
2518724639
sqlite3_free(zErr);
2518824640
free(zQ2);
@@ -25241,13 +24693,14 @@
2524124693
#ifndef SQLITE_SHELL_FIDDLE
2524224694
".check GLOB Fail if output since .testcase does not match",
2524324695
".clone NEWDB Clone data into NEWDB from the existing database",
2524424696
#endif
2524524697
".connection [close] [#] Open or close an auxiliary database connection",
25246
-#if defined(_WIN32) || defined(WIN32)
24698
+#if defined(_WIN32) && !defined(SQLITE_U8TEXT_ONLY) \
24699
+ && !defined(SQLITE_U8TEXT_STDIO)
2524724700
".crnl on|off Translate \\n to \\r\\n. Default ON",
25248
-#endif
24701
+#endif /* _WIN32 && U8TEXT_ONLY && U8TEXT_STDIO */
2524924702
".databases List names and files of attached databases",
2525024703
".dbconfig ?op? ?val? List or change sqlite3_db_config() options",
2525124704
#if SQLITE_SHELL_HAVE_RECOVER
2525224705
".dbinfo ?DB? Show status information about the database",
2525324706
#endif
@@ -25349,13 +24802,15 @@
2534924802
#endif
2535024803
".nullvalue STRING Use STRING in place of NULL values",
2535124804
#ifndef SQLITE_SHELL_FIDDLE
2535224805
".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE",
2535324806
" If FILE begins with '|' then open as a pipe",
25354
- " --bom Put a UTF8 byte-order mark at the beginning",
25355
- " -e Send output to the system text editor",
25356
- " -x Send output as CSV to a spreadsheet (same as \".excel\")",
24807
+ " --bom Put a UTF8 byte-order mark at the beginning",
24808
+ " -e Send output to the system text editor",
24809
+ " --plain Use text/plain output instead of HTML for -w option",
24810
+ " -w Send output as HTML to a web browser (same as \".www\")",
24811
+ " -x Send output as CSV to a spreadsheet (same as \".excel\")",
2535724812
/* Note that .open is (partially) available in WASM builds but is
2535824813
** currently only intended to be used by the fiddle tool, not
2535924814
** end users, so is "undocumented." */
2536024815
".open ?OPTIONS? ?FILE? Close existing database and reopen FILE",
2536124816
" Options:",
@@ -25374,10 +24829,12 @@
2537424829
".output ?FILE? Send output to FILE or stdout if FILE is omitted",
2537524830
" If FILE begins with '|' then open it as a pipe.",
2537624831
" Options:",
2537724832
" --bom Prefix output with a UTF8 byte-order mark",
2537824833
" -e Send output to the system text editor",
24834
+ " --plain Use text/plain for -w option",
24835
+ " -w Send output to a web browser",
2537924836
" -x Send output as CSV to a spreadsheet",
2538024837
#endif
2538124838
".parameter CMD ... Manage SQL parameter bindings",
2538224839
" clear Erase all bindings",
2538324840
" init Initialize the TEMP table that holds bindings",
@@ -25487,10 +24944,14 @@
2548724944
".vfsinfo ?AUX? Information about the top-level VFS",
2548824945
".vfslist List all available VFSes",
2548924946
".vfsname ?AUX? Print the name of the VFS stack",
2549024947
".width NUM1 NUM2 ... Set minimum column widths for columnar output",
2549124948
" Negative values right-justify",
24949
+#ifndef SQLITE_SHELL_FIDDLE
24950
+ ".www Display output of the next command in web browser",
24951
+ " --plain Show results as text/plain, not as HTML",
24952
+#endif
2549224953
};
2549324954
2549424955
/*
2549524956
** Output help text.
2549624957
**
@@ -25535,24 +24996,24 @@
2553524996
hh &= ~HH_Summary;
2553624997
break;
2553724998
}
2553824999
if( ((hw^hh)&HH_Undoc)==0 ){
2553925000
if( (hh&HH_Summary)!=0 ){
25540
- sputf(out, ".%s\n", azHelp[i]+1);
25001
+ sqlite3_fprintf(out, ".%s\n", azHelp[i]+1);
2554125002
++n;
2554225003
}else if( (hw&HW_SummaryOnly)==0 ){
25543
- sputf(out, "%s\n", azHelp[i]);
25004
+ sqlite3_fprintf(out, "%s\n", azHelp[i]);
2554425005
}
2554525006
}
2554625007
}
2554725008
}else{
2554825009
/* Seek documented commands for which zPattern is an exact prefix */
2554925010
zPat = sqlite3_mprintf(".%s*", zPattern);
2555025011
shell_check_oom(zPat);
2555125012
for(i=0; i<ArraySize(azHelp); i++){
2555225013
if( sqlite3_strglob(zPat, azHelp[i])==0 ){
25553
- sputf(out, "%s\n", azHelp[i]);
25014
+ sqlite3_fprintf(out, "%s\n", azHelp[i]);
2555425015
j = i+1;
2555525016
n++;
2555625017
}
2555725018
}
2555825019
sqlite3_free(zPat);
@@ -25559,11 +25020,11 @@
2555925020
if( n ){
2556025021
if( n==1 ){
2556125022
/* when zPattern is a prefix of exactly one command, then include
2556225023
** the details of that command, which should begin at offset j */
2556325024
while( j<ArraySize(azHelp)-1 && azHelp[j][0]==' ' ){
25564
- sputf(out, "%s\n", azHelp[j]);
25025
+ sqlite3_fprintf(out, "%s\n", azHelp[j]);
2556525026
j++;
2556625027
}
2556725028
}
2556825029
return n;
2556925030
}
@@ -25576,14 +25037,14 @@
2557625037
while( i<ArraySize(azHelp)-1 && azHelp[i+1][0]==' ' ) ++i;
2557725038
continue;
2557825039
}
2557925040
if( azHelp[i][0]=='.' ) j = i;
2558025041
if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
25581
- sputf(out, "%s\n", azHelp[j]);
25042
+ sqlite3_fprintf(out, "%s\n", azHelp[j]);
2558225043
while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]==' ' ){
2558325044
j++;
25584
- sputf(out, "%s\n", azHelp[j]);
25045
+ sqlite3_fprintf(out, "%s\n", azHelp[j]);
2558525046
}
2558625047
i = j;
2558725048
n++;
2558825049
}
2558925050
}
@@ -25609,35 +25070,35 @@
2560925070
**
2561025071
** NULL is returned if any error is encountered. The final value of *pnByte
2561125072
** is undefined in this case.
2561225073
*/
2561325074
static char *readFile(const char *zName, int *pnByte){
25614
- FILE *in = fopen(zName, "rb");
25075
+ FILE *in = sqlite3_fopen(zName, "rb");
2561525076
long nIn;
2561625077
size_t nRead;
2561725078
char *pBuf;
2561825079
int rc;
2561925080
if( in==0 ) return 0;
2562025081
rc = fseek(in, 0, SEEK_END);
2562125082
if( rc!=0 ){
25622
- eputf("Error: '%s' not seekable\n", zName);
25083
+ sqlite3_fprintf(stderr,"Error: '%s' not seekable\n", zName);
2562325084
fclose(in);
2562425085
return 0;
2562525086
}
2562625087
nIn = ftell(in);
2562725088
rewind(in);
2562825089
pBuf = sqlite3_malloc64( nIn+1 );
2562925090
if( pBuf==0 ){
25630
- eputz("Error: out of memory\n");
25091
+ sqlite3_fputs("Error: out of memory\n", stderr);
2563125092
fclose(in);
2563225093
return 0;
2563325094
}
2563425095
nRead = fread(pBuf, nIn, 1, in);
2563525096
fclose(in);
2563625097
if( nRead!=1 ){
2563725098
sqlite3_free(pBuf);
25638
- eputf("Error: cannot read '%s'\n", zName);
25099
+ sqlite3_fprintf(stderr,"Error: cannot read '%s'\n", zName);
2563925100
return 0;
2564025101
}
2564125102
pBuf[nIn] = 0;
2564225103
if( pnByte ) *pnByte = nIn;
2564325104
return pBuf;
@@ -25699,11 +25160,11 @@
2569925160
** archive and the dfltZip flag is true, then assume it is a ZIP archive.
2570025161
** Otherwise, assume an ordinary database regardless of the filename if
2570125162
** the type cannot be determined from content.
2570225163
*/
2570325164
int deduceDatabaseType(const char *zName, int dfltZip){
25704
- FILE *f = fopen(zName, "rb");
25165
+ FILE *f = sqlite3_fopen(zName, "rb");
2570525166
size_t n;
2570625167
int rc = SHELL_OPEN_UNSPEC;
2570725168
char zBuf[100];
2570825169
if( f==0 ){
2570925170
if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
@@ -25752,13 +25213,13 @@
2575225213
FILE *in;
2575325214
const char *zDbFilename = p->pAuxDb->zDbFilename;
2575425215
unsigned int x[16];
2575525216
char zLine[1000];
2575625217
if( zDbFilename ){
25757
- in = fopen(zDbFilename, "r");
25218
+ in = sqlite3_fopen(zDbFilename, "r");
2575825219
if( in==0 ){
25759
- eputf("cannot open \"%s\" for reading\n", zDbFilename);
25220
+ sqlite3_fprintf(stderr,"cannot open \"%s\" for reading\n", zDbFilename);
2576025221
return 0;
2576125222
}
2576225223
nLine = 0;
2576325224
}else{
2576425225
in = p->in;
@@ -25765,24 +25226,24 @@
2576525226
nLine = p->lineno;
2576625227
if( in==0 ) in = stdin;
2576725228
}
2576825229
*pnData = 0;
2576925230
nLine++;
25770
- if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
25231
+ if( sqlite3_fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
2577125232
rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
2577225233
if( rc!=2 ) goto readHexDb_error;
2577325234
if( n<0 ) goto readHexDb_error;
2577425235
if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
2577525236
n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */
2577625237
a = sqlite3_malloc( n ? n : 1 );
2577725238
shell_check_oom(a);
2577825239
memset(a, 0, n);
2577925240
if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
25780
- eputz("invalid pagesize\n");
25241
+ sqlite3_fputs("invalid pagesize\n", stderr);
2578125242
goto readHexDb_error;
2578225243
}
25783
- for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
25244
+ for(nLine++; sqlite3_fgets(zLine, sizeof(zLine), in)!=0; nLine++){
2578425245
rc = sscanf(zLine, "| page %d offset %d", &j, &k);
2578525246
if( rc==2 ){
2578625247
iOffset = k;
2578725248
continue;
2578825249
}
@@ -25810,18 +25271,18 @@
2581025271
2581125272
readHexDb_error:
2581225273
if( in!=p->in ){
2581325274
fclose(in);
2581425275
}else{
25815
- while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
25276
+ while( sqlite3_fgets(zLine, sizeof(zLine), p->in)!=0 ){
2581625277
nLine++;
2581725278
if(cli_strncmp(zLine, "| end ", 6)==0 ) break;
2581825279
}
2581925280
p->lineno = nLine;
2582025281
}
2582125282
sqlite3_free(a);
25822
- eputf("Error on line %d of --hexdb input\n", nLine);
25283
+ sqlite3_fprintf(stderr,"Error on line %d of --hexdb input\n", nLine);
2582325284
return 0;
2582425285
}
2582525286
#endif /* SQLITE_OMIT_DESERIALIZE */
2582625287
2582725288
/*
@@ -25892,22 +25353,24 @@
2589225353
SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
2589325354
break;
2589425355
}
2589525356
}
2589625357
if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
25897
- eputf("Error: unable to open database \"%s\": %s\n",
25358
+ sqlite3_fprintf(stderr,"Error: unable to open database \"%s\": %s\n",
2589825359
zDbFilename, sqlite3_errmsg(p->db));
2589925360
if( (openFlags & OPEN_DB_KEEPALIVE)==0 ){
2590025361
exit(1);
2590125362
}
2590225363
sqlite3_close(p->db);
2590325364
sqlite3_open(":memory:", &p->db);
2590425365
if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
25905
- eputz("Also: unable to open substitute in-memory database.\n");
25366
+ sqlite3_fputs("Also: unable to open substitute in-memory database.\n",
25367
+ stderr);
2590625368
exit(1);
2590725369
}else{
25908
- eputf("Notice: using substitute in-memory database instead of \"%s\"\n",
25370
+ sqlite3_fprintf(stderr,
25371
+ "Notice: using substitute in-memory database instead of \"%s\"\n",
2590925372
zDbFilename);
2591025373
}
2591125374
}
2591225375
globalDb = p->db;
2591325376
sqlite3_db_config(p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, (int)0, (int*)0);
@@ -26015,11 +25478,11 @@
2601525478
}
2601625479
rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
2601725480
SQLITE_DESERIALIZE_RESIZEABLE |
2601825481
SQLITE_DESERIALIZE_FREEONCLOSE);
2601925482
if( rc ){
26020
- eputf("Error: sqlite3_deserialize() returns %d\n", rc);
25483
+ sqlite3_fprintf(stderr,"Error: sqlite3_deserialize() returns %d\n", rc);
2602125484
}
2602225485
if( p->szMax>0 ){
2602325486
sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
2602425487
}
2602525488
}
@@ -26039,11 +25502,12 @@
2603925502
** Attempt to close the database connection. Report errors.
2604025503
*/
2604125504
void close_db(sqlite3 *db){
2604225505
int rc = sqlite3_close(db);
2604325506
if( rc ){
26044
- eputf("Error: sqlite3_close() returns %d: %s\n", rc, sqlite3_errmsg(db));
25507
+ sqlite3_fprintf(stderr,
25508
+ "Error: sqlite3_close() returns %d: %s\n", rc, sqlite3_errmsg(db));
2604525509
}
2604625510
}
2604725511
2604825512
#if HAVE_READLINE || HAVE_EDITLINE
2604925513
/*
@@ -26203,11 +25667,12 @@
2620325667
return 1;
2620425668
}
2620525669
if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
2620625670
return 0;
2620725671
}
26208
- eputf("ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", zArg);
25672
+ sqlite3_fprintf(stderr,
25673
+ "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", zArg);
2620925674
return 0;
2621025675
}
2621125676
2621225677
/*
2621325678
** Set or clear a shell flag according to a boolean value.
@@ -26239,13 +25704,13 @@
2623925704
}else if( cli_strcmp(zFile, "stderr")==0 ){
2624025705
f = stderr;
2624125706
}else if( cli_strcmp(zFile, "off")==0 ){
2624225707
f = 0;
2624325708
}else{
26244
- f = fopen(zFile, bTextMode ? "w" : "wb");
25709
+ f = sqlite3_fopen(zFile, bTextMode ? "w" : "wb");
2624525710
if( f==0 ){
26246
- eputf("Error: cannot open \"%s\"\n", zFile);
25711
+ sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", zFile);
2624725712
}
2624825713
}
2624925714
return f;
2625025715
}
2625125716
@@ -26294,16 +25759,17 @@
2629425759
if( nSql>1000000000 ) nSql = 1000000000;
2629525760
while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
2629625761
switch( mType ){
2629725762
case SQLITE_TRACE_ROW:
2629825763
case SQLITE_TRACE_STMT: {
26299
- sputf(p->traceOut, "%.*s;\n", (int)nSql, zSql);
25764
+ sqlite3_fprintf(p->traceOut, "%.*s;\n", (int)nSql, zSql);
2630025765
break;
2630125766
}
2630225767
case SQLITE_TRACE_PROFILE: {
2630325768
sqlite3_int64 nNanosec = pX ? *(sqlite3_int64*)pX : 0;
26304
- sputf(p->traceOut, "%.*s; -- %lld ns\n", (int)nSql, zSql, nNanosec);
25769
+ sqlite3_fprintf(p->traceOut,
25770
+ "%.*s; -- %lld ns\n", (int)nSql, zSql, nNanosec);
2630525771
break;
2630625772
}
2630725773
}
2630825774
return 0;
2630925775
}
@@ -26406,14 +25872,15 @@
2640625872
do{ p->n--; }while( p->z[p->n]!=cQuote );
2640725873
p->cTerm = c;
2640825874
break;
2640925875
}
2641025876
if( pc==cQuote && c!='\r' ){
26411
- eputf("%s:%d: unescaped %c character\n", p->zFile, p->nLine, cQuote);
25877
+ sqlite3_fprintf(stderr,"%s:%d: unescaped %c character\n",
25878
+ p->zFile, p->nLine, cQuote);
2641225879
}
2641325880
if( c==EOF ){
26414
- eputf("%s:%d: unterminated %c-quoted field\n",
25881
+ sqlite3_fprintf(stderr,"%s:%d: unterminated %c-quoted field\n",
2641525882
p->zFile, startLine, cQuote);
2641625883
p->cTerm = c;
2641725884
break;
2641825885
}
2641925886
import_append_char(p, c);
@@ -26508,11 +25975,11 @@
2650825975
2650925976
zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
2651025977
shell_check_oom(zQuery);
2651125978
rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
2651225979
if( rc ){
26513
- eputf("Error %d: %s on [%s]\n",
25980
+ sqlite3_fprintf(stderr,"Error %d: %s on [%s]\n",
2651425981
sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zQuery);
2651525982
goto end_data_xfer;
2651625983
}
2651725984
n = sqlite3_column_count(pQuery);
2651825985
zInsert = sqlite3_malloc64(200 + nTable + n*3);
@@ -26525,11 +25992,11 @@
2652525992
i += 2;
2652625993
}
2652725994
memcpy(zInsert+i, ");", 3);
2652825995
rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
2652925996
if( rc ){
26530
- eputf("Error %d: %s on [%s]\n",
25997
+ sqlite3_fprintf(stderr,"Error %d: %s on [%s]\n",
2653125998
sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb), zInsert);
2653225999
goto end_data_xfer;
2653326000
}
2653426001
for(k=0; k<2; k++){
2653526002
while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
@@ -26561,11 +26028,11 @@
2656126028
}
2656226029
}
2656326030
} /* End for */
2656426031
rc = sqlite3_step(pInsert);
2656526032
if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
26566
- eputf("Error %d: %s\n",
26033
+ sqlite3_fprintf(stderr,"Error %d: %s\n",
2656726034
sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb));
2656826035
}
2656926036
sqlite3_reset(pInsert);
2657026037
cnt++;
2657126038
if( (cnt%spinRate)==0 ){
@@ -26579,11 +26046,11 @@
2657926046
zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
2658026047
zTable);
2658126048
shell_check_oom(zQuery);
2658226049
rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
2658326050
if( rc ){
26584
- eputf("Warning: cannot step \"%s\" backwards", zTable);
26051
+ sqlite3_fprintf(stderr,"Warning: cannot step \"%s\" backwards", zTable);
2658526052
break;
2658626053
}
2658726054
} /* End for(k=0...) */
2658826055
2658926056
end_data_xfer:
@@ -26616,23 +26083,24 @@
2661626083
zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
2661726084
" WHERE %s ORDER BY rowid ASC", zWhere);
2661826085
shell_check_oom(zQuery);
2661926086
rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
2662026087
if( rc ){
26621
- eputf("Error: (%d) %s on [%s]\n", sqlite3_extended_errcode(p->db),
26088
+ sqlite3_fprintf(stderr,
26089
+ "Error: (%d) %s on [%s]\n", sqlite3_extended_errcode(p->db),
2662226090
sqlite3_errmsg(p->db), zQuery);
2662326091
goto end_schema_xfer;
2662426092
}
2662526093
while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
2662626094
zName = sqlite3_column_text(pQuery, 0);
2662726095
zSql = sqlite3_column_text(pQuery, 1);
2662826096
if( zName==0 || zSql==0 ) continue;
2662926097
if( sqlite3_stricmp((char*)zName, "sqlite_sequence")!=0 ){
26630
- sputf(stdout, "%s... ", zName); fflush(stdout);
26098
+ sqlite3_fprintf(stdout, "%s... ", zName); fflush(stdout);
2663126099
sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
2663226100
if( zErrMsg ){
26633
- eputf("Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
26101
+ sqlite3_fprintf(stderr,"Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
2663426102
sqlite3_free(zErrMsg);
2663526103
zErrMsg = 0;
2663626104
}
2663726105
}
2663826106
if( xForEach ){
@@ -26646,23 +26114,23 @@
2664626114
zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
2664726115
" WHERE %s ORDER BY rowid DESC", zWhere);
2664826116
shell_check_oom(zQuery);
2664926117
rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
2665026118
if( rc ){
26651
- eputf("Error: (%d) %s on [%s]\n",
26119
+ sqlite3_fprintf(stderr,"Error: (%d) %s on [%s]\n",
2665226120
sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zQuery);
2665326121
goto end_schema_xfer;
2665426122
}
2665526123
while( sqlite3_step(pQuery)==SQLITE_ROW ){
2665626124
zName = sqlite3_column_text(pQuery, 0);
2665726125
zSql = sqlite3_column_text(pQuery, 1);
2665826126
if( zName==0 || zSql==0 ) continue;
2665926127
if( sqlite3_stricmp((char*)zName, "sqlite_sequence")==0 ) continue;
26660
- sputf(stdout, "%s... ", zName); fflush(stdout);
26128
+ sqlite3_fprintf(stdout, "%s... ", zName); fflush(stdout);
2666126129
sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
2666226130
if( zErrMsg ){
26663
- eputf("Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
26131
+ sqlite3_fprintf(stderr,"Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
2666426132
sqlite3_free(zErrMsg);
2666526133
zErrMsg = 0;
2666626134
}
2666726135
if( xForEach ){
2666826136
xForEach(p, newDb, (const char*)zName);
@@ -26682,16 +26150,17 @@
2668226150
*/
2668326151
static void tryToClone(ShellState *p, const char *zNewDb){
2668426152
int rc;
2668526153
sqlite3 *newDb = 0;
2668626154
if( access(zNewDb,0)==0 ){
26687
- eputf("File \"%s\" already exists.\n", zNewDb);
26155
+ sqlite3_fprintf(stderr,"File \"%s\" already exists.\n", zNewDb);
2668826156
return;
2668926157
}
2669026158
rc = sqlite3_open(zNewDb, &newDb);
2669126159
if( rc ){
26692
- eputf("Cannot create output database: %s\n", sqlite3_errmsg(newDb));
26160
+ sqlite3_fprintf(stderr,
26161
+ "Cannot create output database: %s\n", sqlite3_errmsg(newDb));
2669326162
}else{
2669426163
sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
2669526164
sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
2669626165
tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
2669726166
tryToCloneSchema(p, newDb, "type!='table'", 0);
@@ -26704,14 +26173,21 @@
2670426173
#ifndef SQLITE_SHELL_FIDDLE
2670526174
/*
2670626175
** Change the output stream (file or pipe or console) to something else.
2670726176
*/
2670826177
static void output_redir(ShellState *p, FILE *pfNew){
26709
- if( p->out != stdout ) eputz("Output already redirected.\n");
26710
- else{
26178
+ if( p->out != stdout ){
26179
+ sqlite3_fputs("Output already redirected.\n", stderr);
26180
+ }else{
2671126181
p->out = pfNew;
26712
- setOutputStream(pfNew);
26182
+ if( p->mode==MODE_Www ){
26183
+ sqlite3_fputs(
26184
+ "<!DOCTYPE html>\n"
26185
+ "<HTML><BODY><PRE>\n",
26186
+ p->out
26187
+ );
26188
+ }
2671326189
}
2671426190
}
2671526191
2671626192
/*
2671726193
** Change the output file back to stdout.
@@ -26724,10 +26200,13 @@
2672426200
if( p->outfile[0]=='|' ){
2672526201
#ifndef SQLITE_OMIT_POPEN
2672626202
pclose(p->out);
2672726203
#endif
2672826204
}else{
26205
+ if( p->mode==MODE_Www ){
26206
+ sqlite3_fputs("</PRE></BODY></HTML>\n", p->out);
26207
+ }
2672926208
output_file_close(p->out);
2673026209
#ifndef SQLITE_NOHAVE_SYSTEM
2673126210
if( p->doXdgOpen ){
2673226211
const char *zXdgOpenCmd =
2673326212
#if defined(_WIN32)
@@ -26738,11 +26217,11 @@
2673826217
"xdg-open";
2673926218
#endif
2674026219
char *zCmd;
2674126220
zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
2674226221
if( system(zCmd) ){
26743
- eputf("Failed: [%s]\n", zCmd);
26222
+ sqlite3_fprintf(stderr,"Failed: [%s]\n", zCmd);
2674426223
}else{
2674526224
/* Give the start/open/xdg-open command some time to get
2674626225
** going before we continue, and potential delete the
2674726226
** p->zTempFile data file out from under it */
2674826227
sqlite3_sleep(2000);
@@ -26753,11 +26232,10 @@
2675326232
}
2675426233
#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
2675526234
}
2675626235
p->outfile[0] = 0;
2675726236
p->out = stdout;
26758
- setOutputStream(stdout);
2675926237
}
2676026238
#else
2676126239
# define output_redir(SS,pfO)
2676226240
# define output_reset(SS)
2676326241
#endif
@@ -26829,11 +26307,11 @@
2682926307
if( p->db==0 ) return 1;
2683026308
rc = sqlite3_prepare_v2(p->db,
2683126309
"SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
2683226310
-1, &pStmt, 0);
2683326311
if( rc ){
26834
- eputf("error: %s\n", sqlite3_errmsg(p->db));
26312
+ sqlite3_fprintf(stderr,"error: %s\n", sqlite3_errmsg(p->db));
2683526313
sqlite3_finalize(pStmt);
2683626314
return 1;
2683726315
}
2683826316
sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
2683926317
if( sqlite3_step(pStmt)==SQLITE_ROW
@@ -26842,32 +26320,32 @@
2684226320
const u8 *pb = sqlite3_column_blob(pStmt,0);
2684326321
shell_check_oom(pb);
2684426322
memcpy(aHdr, pb, 100);
2684526323
sqlite3_finalize(pStmt);
2684626324
}else{
26847
- eputz("unable to read database header\n");
26325
+ sqlite3_fputs("unable to read database header\n", stderr);
2684826326
sqlite3_finalize(pStmt);
2684926327
return 1;
2685026328
}
2685126329
i = get2byteInt(aHdr+16);
2685226330
if( i==1 ) i = 65536;
26853
- oputf("%-20s %d\n", "database page size:", i);
26854
- oputf("%-20s %d\n", "write format:", aHdr[18]);
26855
- oputf("%-20s %d\n", "read format:", aHdr[19]);
26856
- oputf("%-20s %d\n", "reserved bytes:", aHdr[20]);
26331
+ sqlite3_fprintf(p->out, "%-20s %d\n", "database page size:", i);
26332
+ sqlite3_fprintf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
26333
+ sqlite3_fprintf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
26334
+ sqlite3_fprintf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
2685726335
for(i=0; i<ArraySize(aField); i++){
2685826336
int ofst = aField[i].ofst;
2685926337
unsigned int val = get4byteInt(aHdr + ofst);
26860
- oputf("%-20s %u", aField[i].zName, val);
26338
+ sqlite3_fprintf(p->out, "%-20s %u", aField[i].zName, val);
2686126339
switch( ofst ){
2686226340
case 56: {
26863
- if( val==1 ) oputz(" (utf8)");
26864
- if( val==2 ) oputz(" (utf16le)");
26865
- if( val==3 ) oputz(" (utf16be)");
26341
+ if( val==1 ) sqlite3_fputs(" (utf8)", p->out);
26342
+ if( val==2 ) sqlite3_fputs(" (utf16le)", p->out);
26343
+ if( val==3 ) sqlite3_fputs(" (utf16be)", p->out);
2686626344
}
2686726345
}
26868
- oputz("\n");
26346
+ sqlite3_fputs("\n", p->out);
2686926347
}
2687026348
if( zDb==0 ){
2687126349
zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
2687226350
}else if( cli_strcmp(zDb,"temp")==0 ){
2687326351
zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
@@ -26876,24 +26354,24 @@
2687626354
}
2687726355
for(i=0; i<ArraySize(aQuery); i++){
2687826356
char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
2687926357
int val = db_int(p->db, zSql);
2688026358
sqlite3_free(zSql);
26881
- oputf("%-20s %d\n", aQuery[i].zName, val);
26359
+ sqlite3_fprintf(p->out, "%-20s %d\n", aQuery[i].zName, val);
2688226360
}
2688326361
sqlite3_free(zSchemaTab);
2688426362
sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
26885
- oputf("%-20s %u\n", "data version", iDataVersion);
26363
+ sqlite3_fprintf(p->out, "%-20s %u\n", "data version", iDataVersion);
2688626364
return 0;
2688726365
}
2688826366
#endif /* SQLITE_SHELL_HAVE_RECOVER */
2688926367
2689026368
/*
2689126369
** Print the given string as an error message.
2689226370
*/
2689326371
static void shellEmitError(const char *zErr){
26894
- eputf("Error: %s\n", zErr);
26372
+ sqlite3_fprintf(stderr,"Error: %s\n", zErr);
2689526373
}
2689626374
/*
2689726375
** Print the current sqlite3_errmsg() value to stderr and return 1.
2689826376
*/
2689926377
static int shellDatabaseError(sqlite3 *db){
@@ -27136,10 +26614,11 @@
2713626614
int bGroupByParent = 0; /* If -groupbyparent is present */
2713726615
int i; /* To iterate through azArg[] */
2713826616
const char *zIndent = ""; /* How much to indent CREATE INDEX by */
2713926617
int rc; /* Return code */
2714026618
sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */
26619
+ FILE *out = pState->out; /* Send output here */
2714126620
2714226621
/*
2714326622
** This SELECT statement returns one row for each foreign key constraint
2714426623
** in the schema of the main database. The column values are:
2714526624
**
@@ -27211,11 +26690,12 @@
2721126690
else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
2721226691
bGroupByParent = 1;
2721326692
zIndent = " ";
2721426693
}
2721526694
else{
27216
- eputf("Usage: %s %s ?-verbose? ?-groupbyparent?\n", azArg[0], azArg[1]);
26695
+ sqlite3_fprintf(stderr,
26696
+ "Usage: %s %s ?-verbose? ?-groupbyparent?\n", azArg[0], azArg[1]);
2721726697
return SQLITE_ERROR;
2721826698
}
2721926699
}
2722026700
2722126701
/* Register the fkey_collate_clause() SQL function */
@@ -27255,44 +26735,45 @@
2725526735
}
2725626736
rc = sqlite3_finalize(pExplain);
2725726737
if( rc!=SQLITE_OK ) break;
2725826738
2725926739
if( res<0 ){
27260
- eputz("Error: internal error");
26740
+ sqlite3_fputs("Error: internal error", stderr);
2726126741
break;
2726226742
}else{
2726326743
if( bGroupByParent
2726426744
&& (bVerbose || res==0)
2726526745
&& (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
2726626746
){
27267
- oputf("-- Parent table %s\n", zParent);
26747
+ sqlite3_fprintf(out, "-- Parent table %s\n", zParent);
2726826748
sqlite3_free(zPrev);
2726926749
zPrev = sqlite3_mprintf("%s", zParent);
2727026750
}
2727126751
2727226752
if( res==0 ){
27273
- oputf("%s%s --> %s\n", zIndent, zCI, zTarget);
26753
+ sqlite3_fprintf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
2727426754
}else if( bVerbose ){
27275
- oputf("%s/* no extra indexes required for %s -> %s */\n",
26755
+ sqlite3_fprintf(out,
26756
+ "%s/* no extra indexes required for %s -> %s */\n",
2727626757
zIndent, zFrom, zTarget
2727726758
);
2727826759
}
2727926760
}
2728026761
}
2728126762
sqlite3_free(zPrev);
2728226763
2728326764
if( rc!=SQLITE_OK ){
27284
- eputf("%s\n", sqlite3_errmsg(db));
26765
+ sqlite3_fprintf(stderr,"%s\n", sqlite3_errmsg(db));
2728526766
}
2728626767
2728726768
rc2 = sqlite3_finalize(pSql);
2728826769
if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
2728926770
rc = rc2;
27290
- eputf("%s\n", sqlite3_errmsg(db));
26771
+ sqlite3_fprintf(stderr,"%s\n", sqlite3_errmsg(db));
2729126772
}
2729226773
}else{
27293
- eputf("%s\n", sqlite3_errmsg(db));
26774
+ sqlite3_fprintf(stderr,"%s\n", sqlite3_errmsg(db));
2729426775
}
2729526776
2729626777
return rc;
2729726778
}
2729826779
@@ -27308,13 +26789,13 @@
2730826789
n = (nArg>=2 ? strlen30(azArg[1]) : 0);
2730926790
if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
2731026791
return lintFkeyIndexes(pState, azArg, nArg);
2731126792
2731226793
usage:
27313
- eputf("Usage %s sub-command ?switches...?\n", azArg[0]);
27314
- eputz("Where sub-commands are:\n");
27315
- eputz(" fkey-indexes\n");
26794
+ sqlite3_fprintf(stderr,"Usage %s sub-command ?switches...?\n", azArg[0]);
26795
+ sqlite3_fprintf(stderr, "Where sub-commands are:\n");
26796
+ sqlite3_fprintf(stderr, " fkey-indexes\n");
2731626797
return SQLITE_ERROR;
2731726798
}
2731826799
2731926800
static void shellPrepare(
2732026801
sqlite3 *db,
@@ -27324,11 +26805,12 @@
2732426805
){
2732526806
*ppStmt = 0;
2732626807
if( *pRc==SQLITE_OK ){
2732726808
int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
2732826809
if( rc!=SQLITE_OK ){
27329
- eputf("sql error: %s (%d)\n", sqlite3_errmsg(db), sqlite3_errcode(db));
26810
+ sqlite3_fprintf(stderr,
26811
+ "sql error: %s (%d)\n", sqlite3_errmsg(db), sqlite3_errcode(db));
2733026812
*pRc = rc;
2733126813
}
2733226814
}
2733326815
}
2733426816
@@ -27368,11 +26850,11 @@
2736826850
if( pStmt ){
2736926851
sqlite3 *db = sqlite3_db_handle(pStmt);
2737026852
int rc = sqlite3_finalize(pStmt);
2737126853
if( *pRc==SQLITE_OK ){
2737226854
if( rc!=SQLITE_OK ){
27373
- eputf("SQL error: %s\n", sqlite3_errmsg(db));
26855
+ sqlite3_fprintf(stderr,"SQL error: %s\n", sqlite3_errmsg(db));
2737426856
}
2737526857
*pRc = rc;
2737626858
}
2737726859
}
2737826860
}
@@ -27390,11 +26872,11 @@
2739026872
){
2739126873
int rc = sqlite3_reset(pStmt);
2739226874
if( *pRc==SQLITE_OK ){
2739326875
if( rc!=SQLITE_OK ){
2739426876
sqlite3 *db = sqlite3_db_handle(pStmt);
27395
- eputf("SQL error: %s\n", sqlite3_errmsg(db));
26877
+ sqlite3_fprintf(stderr,"SQL error: %s\n", sqlite3_errmsg(db));
2739626878
}
2739726879
*pRc = rc;
2739826880
}
2739926881
}
2740026882
#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
@@ -27419,10 +26901,11 @@
2741926901
char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */
2742026902
const char *zFile; /* --file argument, or NULL */
2742126903
const char *zDir; /* --directory argument, or NULL */
2742226904
char **azArg; /* Array of command arguments */
2742326905
ShellState *p; /* Shell state */
26906
+ FILE *out; /* Output to this stream */
2742426907
sqlite3 *db; /* Database containing the archive */
2742526908
};
2742626909
2742726910
/*
2742826911
** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
@@ -27442,13 +26925,13 @@
2744226925
va_start(ap, zFmt);
2744326926
z = sqlite3_vmprintf(zFmt, ap);
2744426927
va_end(ap);
2744526928
shellEmitError(z);
2744626929
if( pAr->fromCmdLine ){
27447
- eputz("Use \"-A\" for more help\n");
26930
+ sqlite3_fputs("Use \"-A\" for more help\n", stderr);
2744826931
}else{
27449
- eputz("Use \".archive --help\" for more help\n");
26932
+ sqlite3_fputs("Use \".archive --help\" for more help\n", stderr);
2745026933
}
2745126934
sqlite3_free(z);
2745226935
return SQLITE_ERROR;
2745326936
}
2745426937
@@ -27544,11 +27027,11 @@
2754427027
};
2754527028
int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
2754627029
struct ArSwitch *pEnd = &aSwitch[nSwitch];
2754727030
2754827031
if( nArg<=1 ){
27549
- eputz("Wrong number of arguments. Usage:\n");
27032
+ sqlite3_fprintf(stderr, "Wrong number of arguments. Usage:\n");
2755027033
return arUsage(stderr);
2755127034
}else{
2755227035
char *z = azArg[1];
2755327036
if( z[0]!='-' ){
2755427037
/* Traditional style [tar] invocation */
@@ -27650,11 +27133,11 @@
2765027133
}
2765127134
}
2765227135
}
2765327136
}
2765427137
if( pAr->eCmd==0 ){
27655
- eputz("Required argument missing. Usage:\n");
27138
+ sqlite3_fprintf(stderr, "Required argument missing. Usage:\n");
2765627139
return arUsage(stderr);
2765727140
}
2765827141
return SQLITE_OK;
2765927142
}
2766027143
@@ -27693,11 +27176,11 @@
2769327176
if( SQLITE_ROW==sqlite3_step(pTest) ){
2769427177
bOk = 1;
2769527178
}
2769627179
shellReset(&rc, pTest);
2769727180
if( rc==SQLITE_OK && bOk==0 ){
27698
- eputf("not found in archive: %s\n", z);
27181
+ sqlite3_fprintf(stderr,"not found in archive: %s\n", z);
2769927182
rc = SQLITE_ERROR;
2770027183
}
2770127184
}
2770227185
shellFinalize(&rc, pTest);
2770327186
}
@@ -27760,19 +27243,19 @@
2776027243
arWhereClause(&rc, pAr, &zWhere);
2776127244
2776227245
shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
2776327246
pAr->zSrcTable, zWhere);
2776427247
if( pAr->bDryRun ){
27765
- oputf("%s\n", sqlite3_sql(pSql));
27248
+ sqlite3_fprintf(pAr->out, "%s\n", sqlite3_sql(pSql));
2776627249
}else{
2776727250
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
2776827251
if( pAr->bVerbose ){
27769
- oputf("%s % 10d %s %s\n",
27252
+ sqlite3_fprintf(pAr->out, "%s % 10d %s %s\n",
2777027253
sqlite3_column_text(pSql, 0), sqlite3_column_int(pSql, 1),
2777127254
sqlite3_column_text(pSql, 2),sqlite3_column_text(pSql, 3));
2777227255
}else{
27773
- oputf("%s\n", sqlite3_column_text(pSql, 0));
27256
+ sqlite3_fprintf(pAr->out, "%s\n", sqlite3_column_text(pSql, 0));
2777427257
}
2777527258
}
2777627259
}
2777727260
shellFinalize(&rc, pSql);
2777827261
sqlite3_free(zWhere);
@@ -27795,11 +27278,11 @@
2779527278
}
2779627279
if( rc==SQLITE_OK ){
2779727280
zSql = sqlite3_mprintf("DELETE FROM %s WHERE %s;",
2779827281
pAr->zSrcTable, zWhere);
2779927282
if( pAr->bDryRun ){
27800
- oputf("%s\n", zSql);
27283
+ sqlite3_fprintf(pAr->out, "%s\n", zSql);
2780127284
}else{
2780227285
char *zErr = 0;
2780327286
rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0);
2780427287
if( rc==SQLITE_OK ){
2780527288
rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
@@ -27808,11 +27291,11 @@
2780827291
}else{
2780927292
rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0);
2781027293
}
2781127294
}
2781227295
if( zErr ){
27813
- sputf(stdout, "ERROR: %s\n", zErr); /* stdout? */
27296
+ sqlite3_fprintf(stdout, "ERROR: %s\n", zErr); /* stdout? */
2781427297
sqlite3_free(zErr);
2781527298
}
2781627299
}
2781727300
}
2781827301
sqlite3_free(zWhere);
@@ -27872,15 +27355,15 @@
2787227355
** populating them changes the timestamp). */
2787327356
for(i=0; i<2; i++){
2787427357
j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
2787527358
sqlite3_bind_int(pSql, j, i);
2787627359
if( pAr->bDryRun ){
27877
- oputf("%s\n", sqlite3_sql(pSql));
27360
+ sqlite3_fprintf(pAr->out, "%s\n", sqlite3_sql(pSql));
2787827361
}else{
2787927362
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
2788027363
if( i==0 && pAr->bVerbose ){
27881
- oputf("%s\n", sqlite3_column_text(pSql, 0));
27364
+ sqlite3_fprintf(pAr->out, "%s\n", sqlite3_column_text(pSql, 0));
2788227365
}
2788327366
}
2788427367
}
2788527368
shellReset(&rc, pSql);
2788627369
}
@@ -27896,17 +27379,17 @@
2789627379
** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out.
2789727380
*/
2789827381
static int arExecSql(ArCommand *pAr, const char *zSql){
2789927382
int rc;
2790027383
if( pAr->bDryRun ){
27901
- oputf("%s\n", zSql);
27384
+ sqlite3_fprintf(pAr->out, "%s\n", zSql);
2790227385
rc = SQLITE_OK;
2790327386
}else{
2790427387
char *zErr = 0;
2790527388
rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
2790627389
if( zErr ){
27907
- sputf(stdout, "ERROR: %s\n", zErr);
27390
+ sqlite3_fprintf(stdout, "ERROR: %s\n", zErr);
2790827391
sqlite3_free(zErr);
2790927392
}
2791027393
}
2791127394
return rc;
2791227395
}
@@ -28051,10 +27534,11 @@
2805127534
cmd.fromCmdLine = fromCmdLine;
2805227535
rc = arParseCommand(azArg, nArg, &cmd);
2805327536
if( rc==SQLITE_OK ){
2805427537
int eDbType = SHELL_OPEN_UNSPEC;
2805527538
cmd.p = pState;
27539
+ cmd.out = pState->out;
2805627540
cmd.db = pState->db;
2805727541
if( cmd.zFile ){
2805827542
eDbType = deduceDatabaseType(cmd.zFile, 1);
2805927543
}else{
2806027544
eDbType = pState->openMode;
@@ -28077,17 +27561,18 @@
2807727561
}else{
2807827562
flags = SQLITE_OPEN_READONLY;
2807927563
}
2808027564
cmd.db = 0;
2808127565
if( cmd.bDryRun ){
28082
- oputf("-- open database '%s'%s\n", cmd.zFile,
27566
+ sqlite3_fprintf(cmd.out, "-- open database '%s'%s\n", cmd.zFile,
2808327567
eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
2808427568
}
2808527569
rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
2808627570
eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
2808727571
if( rc!=SQLITE_OK ){
28088
- eputf("cannot open file: %s (%s)\n", cmd.zFile, sqlite3_errmsg(cmd.db));
27572
+ sqlite3_fprintf(stderr, "cannot open file: %s (%s)\n",
27573
+ cmd.zFile, sqlite3_errmsg(cmd.db));
2808927574
goto end_ar_command;
2809027575
}
2809127576
sqlite3_fileio_init(cmd.db, 0, 0);
2809227577
sqlite3_sqlar_init(cmd.db, 0, 0);
2809327578
sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
@@ -28096,11 +27581,11 @@
2809627581
}
2809727582
if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
2809827583
if( cmd.eCmd!=AR_CMD_CREATE
2809927584
&& sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
2810027585
){
28101
- eputz("database does not contain an 'sqlar' table\n");
27586
+ sqlite3_fprintf(stderr, "database does not contain an 'sqlar' table\n");
2810227587
rc = SQLITE_ERROR;
2810327588
goto end_ar_command;
2810427589
}
2810527590
cmd.zSrcTable = sqlite3_mprintf("sqlar");
2810627591
}
@@ -28154,11 +27639,11 @@
2815427639
** This function is used as a callback by the recover extension. Simply
2815527640
** print the supplied SQL statement to stdout.
2815627641
*/
2815727642
static int recoverSqlCb(void *pCtx, const char *zSql){
2815827643
ShellState *pState = (ShellState*)pCtx;
28159
- sputf(pState->out, "%s;\n", zSql);
27644
+ sqlite3_fprintf(pState->out, "%s;\n", zSql);
2816027645
return SQLITE_OK;
2816127646
}
2816227647
2816327648
/*
2816427649
** This function is called to recover data from the database. A script
@@ -28197,11 +27682,11 @@
2819727682
}else
2819827683
if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
2819927684
bRowids = 0;
2820027685
}
2820127686
else{
28202
- eputf("unexpected option: %s\n", azArg[i]);
27687
+ sqlite3_fprintf(stderr,"unexpected option: %s\n", azArg[i]);
2820327688
showHelp(pState->out, azArg[0]);
2820427689
return 1;
2820527690
}
2820627691
}
2820727692
@@ -28216,11 +27701,11 @@
2821627701
2821727702
sqlite3_recover_run(p);
2821827703
if( sqlite3_recover_errcode(p)!=SQLITE_OK ){
2821927704
const char *zErr = sqlite3_recover_errmsg(p);
2822027705
int errCode = sqlite3_recover_errcode(p);
28221
- eputf("sql error: %s (%d)\n", zErr, errCode);
27706
+ sqlite3_fprintf(stderr,"sql error: %s (%d)\n", zErr, errCode);
2822227707
}
2822327708
rc = sqlite3_recover_finish(p);
2822427709
return rc;
2822527710
}
2822627711
#endif /* SQLITE_SHELL_HAVE_RECOVER */
@@ -28238,25 +27723,25 @@
2823827723
i64 nError = 0;
2823927724
const char *zErr = 0;
2824027725
while( SQLITE_OK==sqlite3_intck_step(p) ){
2824127726
const char *zMsg = sqlite3_intck_message(p);
2824227727
if( zMsg ){
28243
- oputf("%s\n", zMsg);
27728
+ sqlite3_fprintf(pState->out, "%s\n", zMsg);
2824427729
nError++;
2824527730
}
2824627731
nStep++;
2824727732
if( nStepPerUnlock && (nStep % nStepPerUnlock)==0 ){
2824827733
sqlite3_intck_unlock(p);
2824927734
}
2825027735
}
2825127736
rc = sqlite3_intck_error(p, &zErr);
2825227737
if( zErr ){
28253
- eputf("%s\n", zErr);
27738
+ sqlite3_fprintf(stderr,"%s\n", zErr);
2825427739
}
2825527740
sqlite3_intck_close(p);
2825627741
28257
- oputf("%lld steps, %lld errors\n", nStep, nError);
27742
+ sqlite3_fprintf(pState->out, "%lld steps, %lld errors\n", nStep, nError);
2825827743
}
2825927744
2826027745
return rc;
2826127746
}
2826227747
@@ -28275,11 +27760,11 @@
2827527760
*/
2827627761
#ifdef SHELL_DEBUG
2827727762
#define rc_err_oom_die(rc) \
2827827763
if( rc==SQLITE_NOMEM ) shell_check_oom(0); \
2827927764
else if(!(rc==SQLITE_OK||rc==SQLITE_DONE)) \
28280
- eputf("E:%d\n",rc), assert(0)
27765
+ sqlite3_fprintf(stderr,"E:%d\n",rc), assert(0)
2828127766
#else
2828227767
static void rc_err_oom_die(int rc){
2828327768
if( rc==SQLITE_NOMEM ) shell_check_oom(0);
2828427769
assert(rc==SQLITE_OK||rc==SQLITE_DONE);
2828527770
}
@@ -28492,12 +27977,13 @@
2849227977
shellPreparePrintf(p->db, &rc, &pStmt,
2849327978
"SELECT 1 FROM sqlite_schema o WHERE "
2849427979
"sql LIKE 'CREATE VIRTUAL TABLE%%' AND %s", zLike ? zLike : "true"
2849527980
);
2849627981
if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
28497
- oputz("/* WARNING: "
28498
- "Script requires that SQLITE_DBCONFIG_DEFENSIVE be disabled */\n"
27982
+ sqlite3_fputs("/* WARNING: "
27983
+ "Script requires that SQLITE_DBCONFIG_DEFENSIVE be disabled */\n",
27984
+ p->out
2849927985
);
2850027986
}
2850127987
shellFinalize(&rc, pStmt);
2850227988
return rc;
2850327989
}
@@ -28524,16 +28010,18 @@
2852428010
return SQLITE_OK;
2852528011
}
2852628012
if( faultsim_state.iCnt ){
2852728013
if( faultsim_state.iCnt>0 ) faultsim_state.iCnt--;
2852828014
if( faultsim_state.eVerbose>=2 ){
28529
- oputf("FAULT-SIM id=%d no-fault (cnt=%d)\n", iArg, faultsim_state.iCnt);
28015
+ sqlite3_fprintf(stdout,
28016
+ "FAULT-SIM id=%d no-fault (cnt=%d)\n", iArg, faultsim_state.iCnt);
2853028017
}
2853128018
return SQLITE_OK;
2853228019
}
2853328020
if( faultsim_state.eVerbose>=1 ){
28534
- oputf("FAULT-SIM id=%d returns %d\n", iArg, faultsim_state.iErr);
28021
+ sqlite3_fprintf(stdout,
28022
+ "FAULT-SIM id=%d returns %d\n", iArg, faultsim_state.iErr);
2853528023
}
2853628024
faultsim_state.iCnt = faultsim_state.iInterval;
2853728025
faultsim_state.nHit++;
2853828026
if( faultsim_state.nRepeat>0 && faultsim_state.nRepeat<=faultsim_state.nHit ){
2853928027
faultsim_state.iCnt = -1;
@@ -28592,11 +28080,11 @@
2859228080
clearTempFile(p);
2859328081
2859428082
#ifndef SQLITE_OMIT_AUTHORIZATION
2859528083
if( c=='a' && cli_strncmp(azArg[0], "auth", n)==0 ){
2859628084
if( nArg!=2 ){
28597
- eputz("Usage: .auth ON|OFF\n");
28085
+ sqlite3_fprintf(stderr, "Usage: .auth ON|OFF\n");
2859828086
rc = 1;
2859928087
goto meta_command_exit;
2860028088
}
2860128089
open_db(p, 0);
2860228090
if( booleanValue(azArg[1]) ){
@@ -28639,32 +28127,32 @@
2863928127
}else
2864028128
if( cli_strcmp(z, "-async")==0 ){
2864128129
bAsync = 1;
2864228130
}else
2864328131
{
28644
- eputf("unknown option: %s\n", azArg[j]);
28132
+ sqlite3_fprintf(stderr,"unknown option: %s\n", azArg[j]);
2864528133
return 1;
2864628134
}
2864728135
}else if( zDestFile==0 ){
2864828136
zDestFile = azArg[j];
2864928137
}else if( zDb==0 ){
2865028138
zDb = zDestFile;
2865128139
zDestFile = azArg[j];
2865228140
}else{
28653
- eputz("Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
28141
+ sqlite3_fprintf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
2865428142
return 1;
2865528143
}
2865628144
}
2865728145
if( zDestFile==0 ){
28658
- eputz("missing FILENAME argument on .backup\n");
28146
+ sqlite3_fprintf(stderr, "missing FILENAME argument on .backup\n");
2865928147
return 1;
2866028148
}
2866128149
if( zDb==0 ) zDb = "main";
2866228150
rc = sqlite3_open_v2(zDestFile, &pDest,
2866328151
SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
2866428152
if( rc!=SQLITE_OK ){
28665
- eputf("Error: cannot open \"%s\"\n", zDestFile);
28153
+ sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", zDestFile);
2866628154
close_db(pDest);
2866728155
return 1;
2866828156
}
2866928157
if( bAsync ){
2867028158
sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
@@ -28698,21 +28186,12 @@
2869828186
}
2869928187
}else
2870028188
2870128189
/* Undocumented. Legacy only. See "crnl" below */
2870228190
if( c=='b' && n>=3 && cli_strncmp(azArg[0], "binary", n)==0 ){
28703
- if( nArg==2 ){
28704
- if( booleanValue(azArg[1]) ){
28705
- setBinaryMode(p->out, 1);
28706
- }else{
28707
- setTextMode(p->out, 1);
28708
- }
28709
- }else{
28710
- eputz("The \".binary\" command is deprecated. Use \".crnl\" instead.\n"
28711
- "Usage: .binary on|off\n");
28712
- rc = 1;
28713
- }
28191
+ eputz("The \".binary\" command is deprecated. Use \".crnl\" instead.\n");
28192
+ rc = 1;
2871428193
}else
2871528194
2871628195
/* The undocumented ".breakpoint" command causes a call to the no-op
2871728196
** routine named test_breakpoint().
2871828197
*/
@@ -28730,11 +28209,11 @@
2873028209
sqlite3_free(z);
2873128210
#else
2873228211
rc = chdir(azArg[1]);
2873328212
#endif
2873428213
if( rc ){
28735
- eputf("Cannot change to directory \"%s\"\n", azArg[1]);
28214
+ sqlite3_fprintf(stderr,"Cannot change to directory \"%s\"\n", azArg[1]);
2873628215
rc = 1;
2873728216
}
2873828217
}else{
2873928218
eputz("Usage: .cd DIRECTORY\n");
2874028219
rc = 1;
@@ -28763,15 +28242,16 @@
2876328242
eputz("Usage: .check GLOB-PATTERN\n");
2876428243
rc = 2;
2876528244
}else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
2876628245
rc = 2;
2876728246
}else if( testcase_glob(azArg[1],zRes)==0 ){
28768
- eputf("testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
28247
+ sqlite3_fprintf(stderr,
28248
+ "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
2876928249
p->zTestcase, azArg[1], zRes);
2877028250
rc = 1;
2877128251
}else{
28772
- oputf("testcase-%s ok\n", p->zTestcase);
28252
+ sqlite3_fprintf(p->out, "testcase-%s ok\n", p->zTestcase);
2877328253
p->nCheck++;
2877428254
}
2877528255
sqlite3_free(zRes);
2877628256
}else
2877728257
#endif /* !defined(SQLITE_SHELL_FIDDLE) */
@@ -28800,13 +28280,13 @@
2880028280
zFile = "(memory)";
2880128281
}else if( zFile[0]==0 ){
2880228282
zFile = "(temporary-file)";
2880328283
}
2880428284
if( p->pAuxDb == &p->aAuxDb[i] ){
28805
- sputf(stdout, "ACTIVE %d: %s\n", i, zFile);
28285
+ sqlite3_fprintf(stdout, "ACTIVE %d: %s\n", i, zFile);
2880628286
}else if( p->aAuxDb[i].db!=0 ){
28807
- sputf(stdout, " %d: %s\n", i, zFile);
28287
+ sqlite3_fprintf(stdout, " %d: %s\n", i, zFile);
2880828288
}
2880928289
}
2881028290
}else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){
2881128291
int i = azArg[1][0] - '0';
2881228292
if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && i<ArraySize(p->aAuxDb) ){
@@ -28833,23 +28313,25 @@
2883328313
rc = 1;
2883428314
}
2883528315
}else
2883628316
2883728317
if( c=='c' && n==4 && cli_strncmp(azArg[0], "crnl", n)==0 ){
28318
+#if !defined(_WIN32) || defined(SQLITE_U8TEXT_ONLY) \
28319
+ || defined(SQLITE_U8TEXT_STDIO)
28320
+ sqlite3_fputs("The \".crnl\" command is disable in this build.\n", p->out);
28321
+#else
2883828322
if( nArg==2 ){
2883928323
if( booleanValue(azArg[1]) ){
28840
- setTextMode(p->out, 1);
28841
- }else{
28842
- setBinaryMode(p->out, 1);
28843
- }
28844
- }else{
28845
-#if !defined(_WIN32) && !defined(WIN32)
28846
- eputz("The \".crnl\" is a no-op on non-Windows machines.\n");
28847
-#endif
28324
+ sqlite3_fsetmode(p->out, _O_TEXT);
28325
+ }else{
28326
+ sqlite3_fsetmode(p->out, _O_BINARY);
28327
+ }
28328
+ }else{
2884828329
eputz("Usage: .crnl on|off\n");
2884928330
rc = 1;
2885028331
}
28332
+#endif
2885128333
}else
2885228334
2885328335
if( c=='d' && n>1 && cli_strncmp(azArg[0], "databases", n)==0 ){
2885428336
char **azName = 0;
2885528337
int nName = 0;
@@ -28875,11 +28357,11 @@
2887528357
sqlite3_finalize(pStmt);
2887628358
for(i=0; i<nName; i++){
2887728359
int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
2887828360
int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
2887928361
const char *z = azName[i*2+1];
28880
- oputf("%s: %s %s%s\n",
28362
+ sqlite3_fprintf(p->out, "%s: %s %s%s\n",
2888128363
azName[i*2], z && z[0] ? z : "\"\"", bRdonly ? "r/o" : "r/w",
2888228364
eTxn==SQLITE_TXN_NONE ? "" :
2888328365
eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
2888428366
free(azName[i*2]);
2888528367
free(azName[i*2+1]);
@@ -28917,15 +28399,16 @@
2891728399
if( nArg>1 && cli_strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
2891828400
if( nArg>=3 ){
2891928401
sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
2892028402
}
2892128403
sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
28922
- oputf("%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
28404
+ sqlite3_fprintf(p->out, "%19s %s\n",
28405
+ aDbConfig[ii].zName, v ? "on" : "off");
2892328406
if( nArg>1 ) break;
2892428407
}
2892528408
if( nArg>1 && ii==ArraySize(aDbConfig) ){
28926
- eputf("Error: unknown dbconfig \"%s\"\n", azArg[1]);
28409
+ sqlite3_fprintf(stderr,"Error: unknown dbconfig \"%s\"\n", azArg[1]);
2892728410
eputz("Enter \".dbconfig\" with no arguments for a list\n");
2892828411
}
2892928412
}else
2893028413
2893128414
#if SQLITE_SHELL_HAVE_RECOVER
@@ -28971,11 +28454,12 @@
2897128454
}else
2897228455
if( cli_strcmp(z,"nosys")==0 ){
2897328456
ShellSetFlag(p, SHFLG_DumpNoSys);
2897428457
}else
2897528458
{
28976
- eputf("Unknown option \"%s\" on \".dump\"\n", azArg[i]);
28459
+ sqlite3_fprintf(stderr,
28460
+ "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
2897728461
rc = 1;
2897828462
sqlite3_free(zLike);
2897928463
goto meta_command_exit;
2898028464
}
2898128465
}else{
@@ -29006,12 +28490,12 @@
2900628490
outputDumpWarning(p, zLike);
2900728491
if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
2900828492
/* When playing back a "dump", the content might appear in an order
2900928493
** which causes immediate foreign key constraints to be violated.
2901028494
** So disable foreign-key constraint enforcement to prevent problems. */
29011
- oputz("PRAGMA foreign_keys=OFF;\n");
29012
- oputz("BEGIN TRANSACTION;\n");
28495
+ sqlite3_fputs("PRAGMA foreign_keys=OFF;\n", p->out);
28496
+ sqlite3_fputs("BEGIN TRANSACTION;\n", p->out);
2901328497
}
2901428498
p->writableSchema = 0;
2901528499
p->showHeader = 0;
2901628500
/* Set writable_schema=ON since doing so forces SQLite to initialize
2901728501
** as much of the schema as it can even if the sqlite_schema table is
@@ -29039,17 +28523,17 @@
2903928523
run_table_dump_query(p, zSql);
2904028524
sqlite3_free(zSql);
2904128525
}
2904228526
sqlite3_free(zLike);
2904328527
if( p->writableSchema ){
29044
- oputz("PRAGMA writable_schema=OFF;\n");
28528
+ sqlite3_fputs("PRAGMA writable_schema=OFF;\n", p->out);
2904528529
p->writableSchema = 0;
2904628530
}
2904728531
sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
2904828532
sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
2904928533
if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
29050
- oputz(p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
28534
+ sqlite3_fputs(p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n", p->out);
2905128535
}
2905228536
p->showHeader = savedShowHeader;
2905328537
p->shellFlgs = savedShellFlags;
2905428538
}else
2905528539
@@ -29125,11 +28609,12 @@
2912528609
}else
2912628610
2912728611
#ifndef SQLITE_OMIT_VIRTUALTABLE
2912828612
if( c=='e' && cli_strncmp(azArg[0], "expert", n)==0 ){
2912928613
if( p->bSafeMode ){
29130
- eputf("Cannot run experimental commands such as \"%s\" in safe mode\n",
28614
+ sqlite3_fprintf(stderr,
28615
+ "Cannot run experimental commands such as \"%s\" in safe mode\n",
2913128616
azArg[0]);
2913228617
rc = 1;
2913328618
}else{
2913428619
open_db(p, 0);
2913528620
expertDotCommand(p, azArg, nArg);
@@ -29182,13 +28667,14 @@
2918228667
if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
2918328668
}
2918428669
2918528670
/* --help lists all file-controls */
2918628671
if( cli_strcmp(zCmd,"help")==0 ){
29187
- oputz("Available file-controls:\n");
28672
+ sqlite3_fputs("Available file-controls:\n", p->out);
2918828673
for(i=0; i<ArraySize(aCtrl); i++){
29189
- oputf(" .filectrl %s %s\n", aCtrl[i].zCtrlName, aCtrl[i].zUsage);
28674
+ sqlite3_fprintf(p->out,
28675
+ " .filectrl %s %s\n", aCtrl[i].zCtrlName, aCtrl[i].zUsage);
2919028676
}
2919128677
rc = 1;
2919228678
goto meta_command_exit;
2919328679
}
2919428680
@@ -29199,19 +28685,19 @@
2919928685
if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
2920028686
if( filectrl<0 ){
2920128687
filectrl = aCtrl[i].ctrlCode;
2920228688
iCtrl = i;
2920328689
}else{
29204
- eputf("Error: ambiguous file-control: \"%s\"\n"
28690
+ sqlite3_fprintf(stderr,"Error: ambiguous file-control: \"%s\"\n"
2920528691
"Use \".filectrl --help\" for help\n", zCmd);
2920628692
rc = 1;
2920728693
goto meta_command_exit;
2920828694
}
2920928695
}
2921028696
}
2921128697
if( filectrl<0 ){
29212
- eputf("Error: unknown file-control: %s\n"
28698
+ sqlite3_fprintf(stderr,"Error: unknown file-control: %s\n"
2921328699
"Use \".filectrl --help\" for help\n", zCmd);
2921428700
}else{
2921528701
switch(filectrl){
2921628702
case SQLITE_FCNTL_SIZE_LIMIT: {
2921728703
if( nArg!=2 && nArg!=3 ) break;
@@ -29251,11 +28737,11 @@
2925128737
case SQLITE_FCNTL_TEMPFILENAME: {
2925228738
char *z = 0;
2925328739
if( nArg!=2 ) break;
2925428740
sqlite3_file_control(p->db, zSchema, filectrl, &z);
2925528741
if( z ){
29256
- oputf("%s\n", z);
28742
+ sqlite3_fprintf(p->out, "%s\n", z);
2925728743
sqlite3_free(z);
2925828744
}
2925928745
isOk = 2;
2926028746
break;
2926128747
}
@@ -29265,23 +28751,24 @@
2926528751
x = atoi(azArg[2]);
2926628752
sqlite3_file_control(p->db, zSchema, filectrl, &x);
2926728753
}
2926828754
x = -1;
2926928755
sqlite3_file_control(p->db, zSchema, filectrl, &x);
29270
- oputf("%d\n", x);
28756
+ sqlite3_fprintf(p->out, "%d\n", x);
2927128757
isOk = 2;
2927228758
break;
2927328759
}
2927428760
}
2927528761
}
2927628762
if( isOk==0 && iCtrl>=0 ){
29277
- oputf("Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
28763
+ sqlite3_fprintf(p->out, "Usage: .filectrl %s %s\n",
28764
+ zCmd, aCtrl[iCtrl].zUsage);
2927828765
rc = 1;
2927928766
}else if( isOk==1 ){
2928028767
char zBuf[100];
2928128768
sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
29282
- oputf("%s\n", zBuf);
28769
+ sqlite3_fprintf(p->out, "%s\n", zBuf);
2928328770
}
2928428771
}else
2928528772
2928628773
if( c=='f' && cli_strncmp(azArg[0], "fullschema", n)==0 ){
2928728774
ShellState data;
@@ -29318,19 +28805,19 @@
2931828805
doStats = sqlite3_step(pStmt)==SQLITE_ROW;
2931928806
sqlite3_finalize(pStmt);
2932028807
}
2932128808
}
2932228809
if( doStats==0 ){
29323
- oputz("/* No STAT tables available */\n");
28810
+ sqlite3_fputs("/* No STAT tables available */\n", p->out);
2932428811
}else{
29325
- oputz("ANALYZE sqlite_schema;\n");
28812
+ sqlite3_fputs("ANALYZE sqlite_schema;\n", p->out);
2932628813
data.cMode = data.mode = MODE_Insert;
2932728814
data.zDestTable = "sqlite_stat1";
2932828815
shell_exec(&data, "SELECT * FROM sqlite_stat1", 0);
2932928816
data.zDestTable = "sqlite_stat4";
2933028817
shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
29331
- oputz("ANALYZE sqlite_schema;\n");
28818
+ sqlite3_fputs("ANALYZE sqlite_schema;\n", p->out);
2933228819
}
2933328820
}else
2933428821
2933528822
if( c=='h' && cli_strncmp(azArg[0], "headers", n)==0 ){
2933628823
if( nArg==2 ){
@@ -29344,11 +28831,11 @@
2934428831
2934528832
if( c=='h' && cli_strncmp(azArg[0], "help", n)==0 ){
2934628833
if( nArg>=2 ){
2934728834
n = showHelp(p->out, azArg[1]);
2934828835
if( n==0 ){
29349
- oputf("Nothing matches '%s'\n", azArg[1]);
28836
+ sqlite3_fprintf(p->out, "Nothing matches '%s'\n", azArg[1]);
2935028837
}
2935128838
}else{
2935228839
showHelp(p->out, 0);
2935328840
}
2935428841
}else
@@ -29387,11 +28874,11 @@
2938728874
if( zFile==0 ){
2938828875
zFile = z;
2938928876
}else if( zTable==0 ){
2939028877
zTable = z;
2939128878
}else{
29392
- oputf("ERROR: extra argument: \"%s\". Usage:\n", z);
28879
+ sqlite3_fprintf(p->out, "ERROR: extra argument: \"%s\". Usage:\n",z);
2939328880
showHelp(p->out, "import");
2939428881
goto meta_command_exit;
2939528882
}
2939628883
}else if( cli_strcmp(z,"-v")==0 ){
2939728884
eVerbose++;
@@ -29408,17 +28895,17 @@
2940828895
sCtx.cColSep = ',';
2940928896
sCtx.cRowSep = '\n';
2941028897
xRead = csv_read_one_field;
2941128898
useOutputMode = 0;
2941228899
}else{
29413
- oputf("ERROR: unknown option: \"%s\". Usage:\n", z);
28900
+ sqlite3_fprintf(p->out, "ERROR: unknown option: \"%s\". Usage:\n", z);
2941428901
showHelp(p->out, "import");
2941528902
goto meta_command_exit;
2941628903
}
2941728904
}
2941828905
if( zTable==0 ){
29419
- oputf("ERROR: missing %s argument. Usage:\n",
28906
+ sqlite3_fprintf(p->out, "ERROR: missing %s argument. Usage:\n",
2942028907
zFile==0 ? "FILE" : "TABLE");
2942128908
showHelp(p->out, "import");
2942228909
goto meta_command_exit;
2942328910
}
2942428911
seenInterrupt = 0;
@@ -29464,32 +28951,32 @@
2946428951
if( sCtx.zFile[0]=='|' ){
2946528952
#ifdef SQLITE_OMIT_POPEN
2946628953
eputz("Error: pipes are not supported in this OS\n");
2946728954
goto meta_command_exit;
2946828955
#else
29469
- sCtx.in = popen(sCtx.zFile+1, "r");
28956
+ sCtx.in = sqlite3_popen(sCtx.zFile+1, "r");
2947028957
sCtx.zFile = "<pipe>";
2947128958
sCtx.xCloser = pclose;
2947228959
#endif
2947328960
}else{
29474
- sCtx.in = fopen(sCtx.zFile, "rb");
28961
+ sCtx.in = sqlite3_fopen(sCtx.zFile, "rb");
2947528962
sCtx.xCloser = fclose;
2947628963
}
2947728964
if( sCtx.in==0 ){
29478
- eputf("Error: cannot open \"%s\"\n", zFile);
28965
+ sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", zFile);
2947928966
goto meta_command_exit;
2948028967
}
2948128968
if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
2948228969
char zSep[2];
2948328970
zSep[1] = 0;
2948428971
zSep[0] = sCtx.cColSep;
29485
- oputz("Column separator ");
29486
- output_c_string(zSep);
29487
- oputz(", row separator ");
28972
+ sqlite3_fputs("Column separator ", p->out);
28973
+ output_c_string(p->out, zSep);
28974
+ sqlite3_fputs(", row separator ", p->out);
2948828975
zSep[0] = sCtx.cRowSep;
29489
- output_c_string(zSep);
29490
- oputz("\n");
28976
+ output_c_string(p->out, zSep);
28977
+ sqlite3_fputs("\n", p->out);
2949128978
}
2949228979
sCtx.z = sqlite3_malloc64(120);
2949328980
if( sCtx.z==0 ){
2949428981
import_cleanup(&sCtx);
2949528982
shell_out_of_memory();
@@ -29510,18 +28997,18 @@
2951028997
zAutoColumn(sCtx.z, &dbCols, 0);
2951128998
if( sCtx.cTerm!=sCtx.cColSep ) break;
2951228999
}
2951329000
zColDefs = zAutoColumn(0, &dbCols, &zRenames);
2951429001
if( zRenames!=0 ){
29515
- sputf((stdin_is_interactive && p->in==stdin)? p->out : stderr,
29002
+ sqlite3_fprintf((stdin_is_interactive && p->in==stdin)? p->out : stderr,
2951629003
"Columns renamed during .import %s due to duplicates:\n"
2951729004
"%s\n", sCtx.zFile, zRenames);
2951829005
sqlite3_free(zRenames);
2951929006
}
2952029007
assert(dbCols==0);
2952129008
if( zColDefs==0 ){
29522
- eputf("%s: empty file\n", sCtx.zFile);
29009
+ sqlite3_fprintf(stderr,"%s: empty file\n", sCtx.zFile);
2952329010
import_cleanup(&sCtx);
2952429011
rc = 1;
2952529012
sqlite3_free(zCreate);
2952629013
goto meta_command_exit;
2952729014
}
@@ -29529,17 +29016,20 @@
2952929016
if( zCreate==0 ){
2953029017
import_cleanup(&sCtx);
2953129018
shell_out_of_memory();
2953229019
}
2953329020
if( eVerbose>=1 ){
29534
- oputf("%s\n", zCreate);
29021
+ sqlite3_fprintf(p->out, "%s\n", zCreate);
2953529022
}
2953629023
rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
29024
+ if( rc ){
29025
+ sqlite3_fprintf(stderr,
29026
+ "%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db));
29027
+ }
2953729028
sqlite3_free(zCreate);
2953829029
zCreate = 0;
2953929030
if( rc ){
29540
- eputf("%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db));
2954129031
import_cleanup(&sCtx);
2954229032
rc = 1;
2954329033
goto meta_command_exit;
2954429034
}
2954529035
}
@@ -29590,11 +29080,11 @@
2959029080
}
2959129081
zSql[j++] = ')';
2959229082
zSql[j] = 0;
2959329083
assert( j<nByte );
2959429084
if( eVerbose>=2 ){
29595
- oputf("Insert using: %s\n", zSql);
29085
+ sqlite3_fprintf(p->out, "Insert using: %s\n", zSql);
2959629086
}
2959729087
rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
2959829088
sqlite3_free(zSql);
2959929089
zSql = 0;
2960029090
if( rc ){
@@ -29629,11 +29119,11 @@
2962929119
if( z==0 && (xRead==csv_read_one_field) && i==nCol-1 && i>0 ){
2963029120
z = "";
2963129121
}
2963229122
sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
2963329123
if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
29634
- eputf("%s:%d: expected %d columns but found %d"
29124
+ sqlite3_fprintf(stderr,"%s:%d: expected %d columns but found %d"
2963529125
" - filling the rest with NULL\n",
2963629126
sCtx.zFile, startLine, nCol, i+1);
2963729127
i += 2;
2963829128
while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
2963929129
}
@@ -29641,18 +29131,19 @@
2964129131
if( sCtx.cTerm==sCtx.cColSep ){
2964229132
do{
2964329133
xRead(&sCtx);
2964429134
i++;
2964529135
}while( sCtx.cTerm==sCtx.cColSep );
29646
- eputf("%s:%d: expected %d columns but found %d - extras ignored\n",
29136
+ sqlite3_fprintf(stderr,
29137
+ "%s:%d: expected %d columns but found %d - extras ignored\n",
2964729138
sCtx.zFile, startLine, nCol, i);
2964829139
}
2964929140
if( i>=nCol ){
2965029141
sqlite3_step(pStmt);
2965129142
rc = sqlite3_reset(pStmt);
2965229143
if( rc!=SQLITE_OK ){
29653
- eputf("%s:%d: INSERT failed: %s\n",
29144
+ sqlite3_fprintf(stderr,"%s:%d: INSERT failed: %s\n",
2965429145
sCtx.zFile, startLine, sqlite3_errmsg(p->db));
2965529146
sCtx.nErr++;
2965629147
}else{
2965729148
sCtx.nRow++;
2965829149
}
@@ -29661,11 +29152,12 @@
2966129152
2966229153
import_cleanup(&sCtx);
2966329154
sqlite3_finalize(pStmt);
2966429155
if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
2966529156
if( eVerbose>0 ){
29666
- oputf("Added %d rows with %d errors using %d lines of input\n",
29157
+ sqlite3_fprintf(p->out,
29158
+ "Added %d rows with %d errors using %d lines of input\n",
2966729159
sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
2966829160
}
2966929161
}else
2967029162
#endif /* !defined(SQLITE_SHELL_FIDDLE) */
2967129163
@@ -29677,11 +29169,11 @@
2967729169
int tnum = 0;
2967829170
int isWO = 0; /* True if making an imposter of a WITHOUT ROWID table */
2967929171
int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
2968029172
int i;
2968129173
if( !ShellHasFlag(p,SHFLG_TestingMode) ){
29682
- eputf(".%s unavailable without --unsafe-testing\n",
29174
+ sqlite3_fprintf(stderr,".%s unavailable without --unsafe-testing\n",
2968329175
"imposter");
2968429176
rc = 1;
2968529177
goto meta_command_exit;
2968629178
}
2968729179
if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
@@ -29743,11 +29235,11 @@
2974329235
zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
2974429236
}
2974529237
}
2974629238
sqlite3_finalize(pStmt);
2974729239
if( i==0 || tnum==0 ){
29748
- eputf("no such index: \"%s\"\n", azArg[1]);
29240
+ sqlite3_fprintf(stderr,"no such index: \"%s\"\n", azArg[1]);
2974929241
rc = 1;
2975029242
sqlite3_free(zCollist);
2975129243
goto meta_command_exit;
2975229244
}
2975329245
if( lenPK==0 ) lenPK = 100000;
@@ -29758,18 +29250,20 @@
2975829250
rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
2975929251
if( rc==SQLITE_OK ){
2976029252
rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
2976129253
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
2976229254
if( rc ){
29763
- eputf("Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
29255
+ sqlite3_fprintf(stderr,
29256
+ "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
2976429257
}else{
29765
- sputf(stdout, "%s;\n", zSql);
29766
- sputf(stdout, "WARNING: writing to an imposter table will corrupt"
29258
+ sqlite3_fprintf(stdout, "%s;\n", zSql);
29259
+ sqlite3_fprintf(stdout,
29260
+ "WARNING: writing to an imposter table will corrupt"
2976729261
" the \"%s\" %s!\n", azArg[1], isWO ? "table" : "index");
2976829262
}
2976929263
}else{
29770
- eputf("SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
29264
+ sqlite3_fprintf(stderr,"SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
2977129265
rc = 1;
2977229266
}
2977329267
sqlite3_free(zSql);
2977429268
}else
2977529269
#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
@@ -29779,11 +29273,11 @@
2977929273
if( nArg==2 ){
2978029274
iArg = integerValue(azArg[1]);
2978129275
if( iArg==0 ) iArg = -1;
2978229276
}
2978329277
if( (nArg!=1 && nArg!=2) || iArg<0 ){
29784
- eputf("%s","Usage: .intck STEPS_PER_UNLOCK\n");
29278
+ sqlite3_fprintf(stderr,"%s","Usage: .intck STEPS_PER_UNLOCK\n");
2978529279
rc = 1;
2978629280
goto meta_command_exit;
2978729281
}
2978829282
open_db(p, 0);
2978929283
rc = intckDatabaseCmd(p, iArg);
@@ -29798,13 +29292,13 @@
2979829292
sqlite3IoTrace = 0;
2979929293
}else if( cli_strcmp(azArg[1], "-")==0 ){
2980029294
sqlite3IoTrace = iotracePrintf;
2980129295
iotrace = stdout;
2980229296
}else{
29803
- iotrace = fopen(azArg[1], "w");
29297
+ iotrace = sqlite3_fopen(azArg[1], "w");
2980429298
if( iotrace==0 ){
29805
- eputf("Error: cannot open \"%s\"\n", azArg[1]);
29299
+ sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
2980629300
sqlite3IoTrace = 0;
2980729301
rc = 1;
2980829302
}else{
2980929303
sqlite3IoTrace = iotracePrintf;
2981029304
}
@@ -29832,11 +29326,11 @@
2983229326
};
2983329327
int i, n2;
2983429328
open_db(p, 0);
2983529329
if( nArg==1 ){
2983629330
for(i=0; i<ArraySize(aLimit); i++){
29837
- sputf(stdout, "%20s %d\n", aLimit[i].zLimitName,
29331
+ sqlite3_fprintf(stdout, "%20s %d\n", aLimit[i].zLimitName,
2983829332
sqlite3_limit(p->db, aLimit[i].limitCode, -1));
2983929333
}
2984029334
}else if( nArg>3 ){
2984129335
eputz("Usage: .limit NAME ?NEW-VALUE?\n");
2984229336
rc = 1;
@@ -29847,28 +29341,28 @@
2984729341
for(i=0; i<ArraySize(aLimit); i++){
2984829342
if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
2984929343
if( iLimit<0 ){
2985029344
iLimit = i;
2985129345
}else{
29852
- eputf("ambiguous limit: \"%s\"\n", azArg[1]);
29346
+ sqlite3_fprintf(stderr,"ambiguous limit: \"%s\"\n", azArg[1]);
2985329347
rc = 1;
2985429348
goto meta_command_exit;
2985529349
}
2985629350
}
2985729351
}
2985829352
if( iLimit<0 ){
29859
- eputf("unknown limit: \"%s\"\n"
29353
+ sqlite3_fprintf(stderr,"unknown limit: \"%s\"\n"
2986029354
"enter \".limits\" with no arguments for a list.\n",
2986129355
azArg[1]);
2986229356
rc = 1;
2986329357
goto meta_command_exit;
2986429358
}
2986529359
if( nArg==3 ){
2986629360
sqlite3_limit(p->db, aLimit[iLimit].limitCode,
2986729361
(int)integerValue(azArg[2]));
2986829362
}
29869
- sputf(stdout, "%20s %d\n", aLimit[iLimit].zLimitName,
29363
+ sqlite3_fprintf(stdout, "%20s %d\n", aLimit[iLimit].zLimitName,
2987029364
sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
2987129365
}
2987229366
}else
2987329367
2987429368
if( c=='l' && n>2 && cli_strncmp(azArg[0], "lint", n)==0 ){
@@ -29947,35 +29441,37 @@
2994729441
cmOpts = cmo;
2994829442
}
2994929443
}else if( zTabname==0 ){
2995029444
zTabname = z;
2995129445
}else if( z[0]=='-' ){
29952
- eputf("unknown option: %s\n", z);
29446
+ sqlite3_fprintf(stderr,"unknown option: %s\n", z);
2995329447
eputz("options:\n"
2995429448
" --noquote\n"
2995529449
" --quote\n"
2995629450
" --wordwrap on/off\n"
2995729451
" --wrap N\n"
2995829452
" --ww\n");
2995929453
rc = 1;
2996029454
goto meta_command_exit;
2996129455
}else{
29962
- eputf("extra argument: \"%s\"\n", z);
29456
+ sqlite3_fprintf(stderr,"extra argument: \"%s\"\n", z);
2996329457
rc = 1;
2996429458
goto meta_command_exit;
2996529459
}
2996629460
}
2996729461
if( zMode==0 ){
2996829462
if( p->mode==MODE_Column
2996929463
|| (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
2997029464
){
29971
- oputf("current output mode: %s --wrap %d --wordwrap %s --%squote\n",
29465
+ sqlite3_fprintf(p->out,
29466
+ "current output mode: %s --wrap %d --wordwrap %s --%squote\n",
2997229467
modeDescr[p->mode], p->cmOpts.iWrap,
2997329468
p->cmOpts.bWordWrap ? "on" : "off",
2997429469
p->cmOpts.bQuote ? "" : "no");
2997529470
}else{
29976
- oputf("current output mode: %s\n", modeDescr[p->mode]);
29471
+ sqlite3_fprintf(p->out,
29472
+ "current output mode: %s\n", modeDescr[p->mode]);
2997729473
}
2997829474
zMode = modeDescr[p->mode];
2997929475
}
2998029476
n2 = strlen30(zMode);
2998129477
if( cli_strncmp(zMode,"lines",n2)==0 ){
@@ -30044,11 +29540,11 @@
3004429540
if( c=='n' && cli_strcmp(azArg[0], "nonce")==0 ){
3004529541
if( nArg!=2 ){
3004629542
eputz("Usage: .nonce NONCE\n");
3004729543
rc = 1;
3004829544
}else if( p->zNonce==0 || cli_strcmp(azArg[1],p->zNonce)!=0 ){
30049
- eputf("line %d: incorrect nonce: \"%s\"\n",
29545
+ sqlite3_fprintf(stderr,"line %d: incorrect nonce: \"%s\"\n",
3005029546
p->lineno, azArg[1]);
3005129547
exit(1);
3005229548
}else{
3005329549
p->bSafeMode = 0;
3005429550
return 0; /* Return immediately to bypass the safe mode reset
@@ -30099,15 +29595,15 @@
3009929595
p->szMax = integerValue(azArg[++iName]);
3010029596
#endif /* SQLITE_OMIT_DESERIALIZE */
3010129597
}else
3010229598
#endif /* !SQLITE_SHELL_FIDDLE */
3010329599
if( z[0]=='-' ){
30104
- eputf("unknown option: %s\n", z);
29600
+ sqlite3_fprintf(stderr,"unknown option: %s\n", z);
3010529601
rc = 1;
3010629602
goto meta_command_exit;
3010729603
}else if( zFN ){
30108
- eputf("extra argument: \"%s\"\n", z);
29604
+ sqlite3_fprintf(stderr,"extra argument: \"%s\"\n", z);
3010929605
rc = 1;
3011029606
goto meta_command_exit;
3011129607
}else{
3011229608
zFN = z;
3011329609
}
@@ -30145,11 +29641,11 @@
3014529641
zNewFilename = 0;
3014629642
}
3014729643
p->pAuxDb->zDbFilename = zNewFilename;
3014829644
open_db(p, OPEN_DB_KEEPALIVE);
3014929645
if( p->db==0 ){
30150
- eputf("Error: cannot open '%s'\n", zNewFilename);
29646
+ sqlite3_fprintf(stderr,"Error: cannot open '%s'\n", zNewFilename);
3015129647
sqlite3_free(zNewFilename);
3015229648
}else{
3015329649
p->pAuxDb->zFreeOnClose = zNewFilename;
3015429650
}
3015529651
}
@@ -30163,22 +29659,27 @@
3016329659
#ifndef SQLITE_SHELL_FIDDLE
3016429660
if( (c=='o'
3016529661
&& (cli_strncmp(azArg[0], "output", n)==0
3016629662
|| cli_strncmp(azArg[0], "once", n)==0))
3016729663
|| (c=='e' && n==5 && cli_strcmp(azArg[0],"excel")==0)
29664
+ || (c=='w' && n==3 && cli_strcmp(azArg[0],"www")==0)
3016829665
){
3016929666
char *zFile = 0;
3017029667
int bTxtMode = 0;
3017129668
int i;
3017229669
int eMode = 0;
30173
- int bOnce = 0; /* 0: .output, 1: .once, 2: .excel */
29670
+ int bOnce = 0; /* 0: .output, 1: .once, 2: .excel/.www */
29671
+ int bPlain = 0; /* --plain option */
3017429672
static const char *zBomUtf8 = "\xef\xbb\xbf";
3017529673
const char *zBom = 0;
3017629674
3017729675
failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
3017829676
if( c=='e' ){
3017929677
eMode = 'x';
29678
+ bOnce = 2;
29679
+ }else if( c=='w' ){
29680
+ eMode = 'w';
3018029681
bOnce = 2;
3018129682
}else if( cli_strncmp(azArg[0],"once",n)==0 ){
3018229683
bOnce = 1;
3018329684
}
3018429685
for(i=1; i<nArg; i++){
@@ -30185,28 +29686,34 @@
3018529686
char *z = azArg[i];
3018629687
if( z[0]=='-' ){
3018729688
if( z[1]=='-' ) z++;
3018829689
if( cli_strcmp(z,"-bom")==0 ){
3018929690
zBom = zBomUtf8;
30190
- }else if( c!='e' && cli_strcmp(z,"-x")==0 ){
29691
+ }else if( cli_strcmp(z,"-plain")==0 ){
29692
+ bPlain = 1;
29693
+ }else if( c=='o' && cli_strcmp(z,"-x")==0 ){
3019129694
eMode = 'x'; /* spreadsheet */
30192
- }else if( c!='e' && cli_strcmp(z,"-e")==0 ){
29695
+ }else if( c=='o' && cli_strcmp(z,"-e")==0 ){
3019329696
eMode = 'e'; /* text editor */
29697
+ }else if( c=='o' && cli_strcmp(z,"-w")==0 ){
29698
+ eMode = 'w'; /* Web browser */
3019429699
}else{
30195
- oputf("ERROR: unknown option: \"%s\". Usage:\n", azArg[i]);
29700
+ sqlite3_fprintf(p->out,
29701
+ "ERROR: unknown option: \"%s\". Usage:\n", azArg[i]);
3019629702
showHelp(p->out, azArg[0]);
3019729703
rc = 1;
3019829704
goto meta_command_exit;
3019929705
}
30200
- }else if( zFile==0 && eMode!='e' && eMode!='x' ){
29706
+ }else if( zFile==0 && eMode==0 ){
3020129707
zFile = sqlite3_mprintf("%s", z);
3020229708
if( zFile && zFile[0]=='|' ){
3020329709
while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
3020429710
break;
3020529711
}
3020629712
}else{
30207
- oputf("ERROR: extra parameter: \"%s\". Usage:\n", azArg[i]);
29713
+ sqlite3_fprintf(p->out,
29714
+ "ERROR: extra parameter: \"%s\". Usage:\n", azArg[i]);
3020829715
showHelp(p->out, azArg[0]);
3020929716
rc = 1;
3021029717
sqlite3_free(zFile);
3021129718
goto meta_command_exit;
3021229719
}
@@ -30219,20 +29726,29 @@
3021929726
}else{
3022029727
p->outCount = 0;
3022129728
}
3022229729
output_reset(p);
3022329730
#ifndef SQLITE_NOHAVE_SYSTEM
30224
- if( eMode=='e' || eMode=='x' ){
29731
+ if( eMode=='e' || eMode=='x' || eMode=='w' ){
3022529732
p->doXdgOpen = 1;
3022629733
outputModePush(p);
3022729734
if( eMode=='x' ){
3022829735
/* spreadsheet mode. Output as CSV. */
3022929736
newTempFile(p, "csv");
3023029737
ShellClearFlag(p, SHFLG_Echo);
3023129738
p->mode = MODE_Csv;
3023229739
sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
3023329740
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
29741
+#ifdef _WIN32
29742
+ zBom = zBomUtf8; /* Always include the BOM on Windows, as Excel does
29743
+ ** not work without it. */
29744
+#endif
29745
+ }else if( eMode=='w' ){
29746
+ /* web-browser mode. */
29747
+ newTempFile(p, "html");
29748
+ if( !bPlain ) p->mode = MODE_Www;
29749
+ bTxtMode = 1;
3023429750
}else{
3023529751
/* text editor mode */
3023629752
newTempFile(p, "txt");
3023729753
bTxtMode = 1;
3023829754
}
@@ -30245,30 +29761,36 @@
3024529761
#ifdef SQLITE_OMIT_POPEN
3024629762
eputz("Error: pipes are not supported in this OS\n");
3024729763
rc = 1;
3024829764
output_redir(p, stdout);
3024929765
#else
30250
- FILE *pfPipe = popen(zFile + 1, "w");
29766
+ FILE *pfPipe = sqlite3_popen(zFile + 1, "w");
3025129767
if( pfPipe==0 ){
30252
- eputf("Error: cannot open pipe \"%s\"\n", zFile + 1);
29768
+ sqlite3_fprintf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
3025329769
rc = 1;
3025429770
}else{
3025529771
output_redir(p, pfPipe);
30256
- if( zBom ) oputz(zBom);
29772
+ if( zBom ) sqlite3_fputs(zBom, pfPipe);
3025729773
sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
3025829774
}
3025929775
#endif
3026029776
}else{
3026129777
FILE *pfFile = output_file_open(zFile, bTxtMode);
3026229778
if( pfFile==0 ){
3026329779
if( cli_strcmp(zFile,"off")!=0 ){
30264
- eputf("Error: cannot write to \"%s\"\n", zFile);
29780
+ sqlite3_fprintf(stderr,"Error: cannot write to \"%s\"\n", zFile);
3026529781
}
3026629782
rc = 1;
3026729783
} else {
3026829784
output_redir(p, pfFile);
30269
- if( zBom ) oputz(zBom);
29785
+ if( bPlain && eMode=='w' ){
29786
+ sqlite3_fputs(
29787
+ "<!DOCTYPE html>\n<BODY>\n<PLAINTEXT>\n",
29788
+ pfFile
29789
+ );
29790
+ }
29791
+ if( zBom ) sqlite3_fputs(zBom, pfFile);
3027029792
sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
3027129793
}
3027229794
}
3027329795
sqlite3_free(zFile);
3027429796
}else
@@ -30305,11 +29827,12 @@
3030529827
if( len ){
3030629828
rx = sqlite3_prepare_v2(p->db,
3030729829
"SELECT key, quote(value) "
3030829830
"FROM temp.sqlite_parameters;", -1, &pStmt, 0);
3030929831
while( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
30310
- oputf("%-*s %s\n", len, sqlite3_column_text(pStmt,0),
29832
+ sqlite3_fprintf(p->out,
29833
+ "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
3031129834
sqlite3_column_text(pStmt,1));
3031229835
}
3031329836
sqlite3_finalize(pStmt);
3031429837
}
3031529838
}else
@@ -30350,11 +29873,11 @@
3035029873
"VALUES(%Q,%Q);", zKey, zValue);
3035129874
shell_check_oom(zSql);
3035229875
rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3035329876
sqlite3_free(zSql);
3035429877
if( rx!=SQLITE_OK ){
30355
- oputf("Error: %s\n", sqlite3_errmsg(p->db));
29878
+ sqlite3_fprintf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
3035629879
sqlite3_finalize(pStmt);
3035729880
pStmt = 0;
3035829881
rc = 1;
3035929882
}
3036029883
}
@@ -30379,14 +29902,14 @@
3037929902
}else
3038029903
3038129904
if( c=='p' && n>=3 && cli_strncmp(azArg[0], "print", n)==0 ){
3038229905
int i;
3038329906
for(i=1; i<nArg; i++){
30384
- if( i>1 ) oputz(" ");
30385
- oputz(azArg[i]);
29907
+ if( i>1 ) sqlite3_fputs(" ", p->out);
29908
+ sqlite3_fputs(azArg[i], p->out);
3038629909
}
30387
- oputz("\n");
29910
+ sqlite3_fputs("\n", p->out);
3038829911
}else
3038929912
3039029913
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
3039129914
if( c=='p' && n>=3 && cli_strncmp(azArg[0], "progress", n)==0 ){
3039229915
int i;
@@ -30419,11 +29942,11 @@
3041929942
}else{
3042029943
p->mxProgress = (int)integerValue(azArg[++i]);
3042129944
}
3042229945
continue;
3042329946
}
30424
- eputf("Error: unknown option: \"%s\"\n", azArg[i]);
29947
+ sqlite3_fprintf(stderr,"Error: unknown option: \"%s\"\n", azArg[i]);
3042529948
rc = 1;
3042629949
goto meta_command_exit;
3042729950
}else{
3042829951
nn = (int)integerValue(z);
3042929952
}
@@ -30462,21 +29985,21 @@
3046229985
#ifdef SQLITE_OMIT_POPEN
3046329986
eputz("Error: pipes are not supported in this OS\n");
3046429987
rc = 1;
3046529988
p->out = stdout;
3046629989
#else
30467
- p->in = popen(azArg[1]+1, "r");
29990
+ p->in = sqlite3_popen(azArg[1]+1, "r");
3046829991
if( p->in==0 ){
30469
- eputf("Error: cannot open \"%s\"\n", azArg[1]);
29992
+ sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
3047029993
rc = 1;
3047129994
}else{
3047229995
rc = process_input(p);
3047329996
pclose(p->in);
3047429997
}
3047529998
#endif
3047629999
}else if( (p->in = openChrSource(azArg[1]))==0 ){
30477
- eputf("Error: cannot open \"%s\"\n", azArg[1]);
30000
+ sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
3047830001
rc = 1;
3047930002
}else{
3048030003
rc = process_input(p);
3048130004
fclose(p->in);
3048230005
}
@@ -30505,11 +30028,11 @@
3050530028
rc = 1;
3050630029
goto meta_command_exit;
3050730030
}
3050830031
rc = sqlite3_open(zSrcFile, &pSrc);
3050930032
if( rc!=SQLITE_OK ){
30510
- eputf("Error: cannot open \"%s\"\n", zSrcFile);
30033
+ sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", zSrcFile);
3051130034
close_db(pSrc);
3051230035
return 1;
3051330036
}
3051430037
open_db(p, 0);
3051530038
pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
@@ -30588,11 +30111,11 @@
3058830111
}else if( optionMatch(azArg[ii],"debug") ){
3058930112
bDebug = 1;
3059030113
}else if( optionMatch(azArg[ii],"nosys") ){
3059130114
bNoSystemTabs = 1;
3059230115
}else if( azArg[ii][0]=='-' ){
30593
- eputf("Unknown option: \"%s\"\n", azArg[ii]);
30116
+ sqlite3_fprintf(stderr,"Unknown option: \"%s\"\n", azArg[ii]);
3059430117
rc = 1;
3059530118
goto meta_command_exit;
3059630119
}else if( zName==0 ){
3059730120
zName = azArg[ii];
3059830121
}else{
@@ -30689,11 +30212,11 @@
3068930212
appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
3069030213
}
3069130214
appendText(&sSelect, "sql IS NOT NULL"
3069230215
" ORDER BY snum, rowid", 0);
3069330216
if( bDebug ){
30694
- oputf("SQL: %s;\n", sSelect.z);
30217
+ sqlite3_fprintf(p->out, "SQL: %s;\n", sSelect.z);
3069530218
}else{
3069630219
rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
3069730220
}
3069830221
freeText(&sSelect);
3069930222
}
@@ -30750,11 +30273,12 @@
3075030273
session_not_open:
3075130274
eputz("ERROR: No sessions are open\n");
3075230275
}else{
3075330276
rc = sqlite3session_attach(pSession->p, azCmd[1]);
3075430277
if( rc ){
30755
- eputf("ERROR: sqlite3session_attach() returns %d\n",rc);
30278
+ sqlite3_fprintf(stderr,
30279
+ "ERROR: sqlite3session_attach() returns %d\n",rc);
3075630280
rc = 0;
3075730281
}
3075830282
}
3075930283
}else
3076030284
@@ -30767,13 +30291,13 @@
3076730291
){
3076830292
FILE *out = 0;
3076930293
failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]);
3077030294
if( nCmd!=2 ) goto session_syntax_error;
3077130295
if( pSession->p==0 ) goto session_not_open;
30772
- out = fopen(azCmd[1], "wb");
30296
+ out = sqlite3_fopen(azCmd[1], "wb");
3077330297
if( out==0 ){
30774
- eputf("ERROR: cannot open \"%s\" for writing\n",
30298
+ sqlite3_fprintf(stderr,"ERROR: cannot open \"%s\" for writing\n",
3077530299
azCmd[1]);
3077630300
}else{
3077730301
int szChng;
3077830302
void *pChng;
3077930303
if( azCmd[0][0]=='c' ){
@@ -30780,16 +30304,17 @@
3078030304
rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
3078130305
}else{
3078230306
rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
3078330307
}
3078430308
if( rc ){
30785
- sputf(stdout, "Error: error code %d\n", rc);
30309
+ sqlite3_fprintf(stdout, "Error: error code %d\n", rc);
3078630310
rc = 0;
3078730311
}
3078830312
if( pChng
3078930313
&& fwrite(pChng, szChng, 1, out)!=1 ){
30790
- eputf("ERROR: Failed to write entire %d-byte output\n", szChng);
30314
+ sqlite3_fprintf(stderr,
30315
+ "ERROR: Failed to write entire %d-byte output\n", szChng);
3079130316
}
3079230317
sqlite3_free(pChng);
3079330318
fclose(out);
3079430319
}
3079530320
}else
@@ -30812,11 +30337,12 @@
3081230337
int ii;
3081330338
if( nCmd>2 ) goto session_syntax_error;
3081430339
ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
3081530340
if( pAuxDb->nSession ){
3081630341
ii = sqlite3session_enable(pSession->p, ii);
30817
- oputf("session %s enable flag = %d\n", pSession->zName, ii);
30342
+ sqlite3_fprintf(p->out,
30343
+ "session %s enable flag = %d\n", pSession->zName, ii);
3081830344
}
3081930345
}else
3082030346
3082130347
/* .session filter GLOB ....
3082230348
** Set a list of GLOB patterns of table names to be excluded.
@@ -30847,11 +30373,12 @@
3084730373
int ii;
3084830374
if( nCmd>2 ) goto session_syntax_error;
3084930375
ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
3085030376
if( pAuxDb->nSession ){
3085130377
ii = sqlite3session_indirect(pSession->p, ii);
30852
- oputf("session %s indirect flag = %d\n", pSession->zName, ii);
30378
+ sqlite3_fprintf(p->out,
30379
+ "session %s indirect flag = %d\n", pSession->zName, ii);
3085330380
}
3085430381
}else
3085530382
3085630383
/* .session isempty
3085730384
** Determine if the session is empty
@@ -30859,20 +30386,21 @@
3085930386
if( cli_strcmp(azCmd[0], "isempty")==0 ){
3086030387
int ii;
3086130388
if( nCmd!=1 ) goto session_syntax_error;
3086230389
if( pAuxDb->nSession ){
3086330390
ii = sqlite3session_isempty(pSession->p);
30864
- oputf("session %s isempty flag = %d\n", pSession->zName, ii);
30391
+ sqlite3_fprintf(p->out,
30392
+ "session %s isempty flag = %d\n", pSession->zName, ii);
3086530393
}
3086630394
}else
3086730395
3086830396
/* .session list
3086930397
** List all currently open sessions
3087030398
*/
3087130399
if( cli_strcmp(azCmd[0],"list")==0 ){
3087230400
for(i=0; i<pAuxDb->nSession; i++){
30873
- oputf("%d %s\n", i, pAuxDb->aSession[i].zName);
30401
+ sqlite3_fprintf(p->out, "%d %s\n", i, pAuxDb->aSession[i].zName);
3087430402
}
3087530403
}else
3087630404
3087730405
/* .session open DB NAME
3087830406
** Open a new session called NAME on the attached database DB.
@@ -30883,22 +30411,23 @@
3088330411
if( nCmd!=3 ) goto session_syntax_error;
3088430412
zName = azCmd[2];
3088530413
if( zName[0]==0 ) goto session_syntax_error;
3088630414
for(i=0; i<pAuxDb->nSession; i++){
3088730415
if( cli_strcmp(pAuxDb->aSession[i].zName,zName)==0 ){
30888
- eputf("Session \"%s\" already exists\n", zName);
30416
+ sqlite3_fprintf(stderr,"Session \"%s\" already exists\n", zName);
3088930417
goto meta_command_exit;
3089030418
}
3089130419
}
3089230420
if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){
30893
- eputf("Maximum of %d sessions\n", ArraySize(pAuxDb->aSession));
30421
+ sqlite3_fprintf(stderr,
30422
+ "Maximum of %d sessions\n", ArraySize(pAuxDb->aSession));
3089430423
goto meta_command_exit;
3089530424
}
3089630425
pSession = &pAuxDb->aSession[pAuxDb->nSession];
3089730426
rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
3089830427
if( rc ){
30899
- eputf("Cannot open session: error code=%d\n", rc);
30428
+ sqlite3_fprintf(stderr,"Cannot open session: error code=%d\n", rc);
3090030429
rc = 0;
3090130430
goto meta_command_exit;
3090230431
}
3090330432
pSession->nFilter = 0;
3090430433
sqlite3session_table_filter(pSession->p, session_filter, pSession);
@@ -30918,20 +30447,20 @@
3091830447
if( c=='s' && n>=10 && cli_strncmp(azArg[0], "selftest-", 9)==0 ){
3091930448
if( cli_strncmp(azArg[0]+9, "boolean", n-9)==0 ){
3092030449
int i, v;
3092130450
for(i=1; i<nArg; i++){
3092230451
v = booleanValue(azArg[i]);
30923
- oputf("%s: %d 0x%x\n", azArg[i], v, v);
30452
+ sqlite3_fprintf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
3092430453
}
3092530454
}
3092630455
if( cli_strncmp(azArg[0]+9, "integer", n-9)==0 ){
3092730456
int i; sqlite3_int64 v;
3092830457
for(i=1; i<nArg; i++){
3092930458
char zBuf[200];
3093030459
v = integerValue(azArg[i]);
3093130460
sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
30932
- oputz(zBuf);
30461
+ sqlite3_fputs(zBuf, p->out);
3093330462
}
3093430463
}
3093530464
}else
3093630465
#endif
3093730466
@@ -30954,12 +30483,13 @@
3095430483
}else
3095530484
if( cli_strcmp(z,"-v")==0 ){
3095630485
bVerbose++;
3095730486
}else
3095830487
{
30959
- eputf("Unknown option \"%s\" on \"%s\"\n", azArg[i], azArg[0]);
30960
- eputz("Should be one of: --init -v\n");
30488
+ sqlite3_fprintf(stderr,
30489
+ "Unknown option \"%s\" on \"%s\"\n", azArg[i], azArg[0]);
30490
+ sqlite3_fputs("Should be one of: --init -v\n", stderr);
3096130491
rc = 1;
3096230492
goto meta_command_exit;
3096330493
}
3096430494
}
3096530495
if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
@@ -31000,46 +30530,47 @@
3100030530
if( zOp==0 ) continue;
3100130531
if( zSql==0 ) continue;
3100230532
if( zAns==0 ) continue;
3100330533
k = 0;
3100430534
if( bVerbose>0 ){
31005
- sputf(stdout, "%d: %s %s\n", tno, zOp, zSql);
30535
+ sqlite3_fprintf(stdout, "%d: %s %s\n", tno, zOp, zSql);
3100630536
}
3100730537
if( cli_strcmp(zOp,"memo")==0 ){
31008
- oputf("%s\n", zSql);
30538
+ sqlite3_fprintf(p->out, "%s\n", zSql);
3100930539
}else
3101030540
if( cli_strcmp(zOp,"run")==0 ){
3101130541
char *zErrMsg = 0;
3101230542
str.n = 0;
3101330543
str.z[0] = 0;
3101430544
rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
3101530545
nTest++;
3101630546
if( bVerbose ){
31017
- oputf("Result: %s\n", str.z);
30547
+ sqlite3_fprintf(p->out, "Result: %s\n", str.z);
3101830548
}
3101930549
if( rc || zErrMsg ){
3102030550
nErr++;
3102130551
rc = 1;
31022
- oputf("%d: error-code-%d: %s\n", tno, rc, zErrMsg);
30552
+ sqlite3_fprintf(p->out, "%d: error-code-%d: %s\n", tno, rc,zErrMsg);
3102330553
sqlite3_free(zErrMsg);
3102430554
}else if( cli_strcmp(zAns,str.z)!=0 ){
3102530555
nErr++;
3102630556
rc = 1;
31027
- oputf("%d: Expected: [%s]\n", tno, zAns);
31028
- oputf("%d: Got: [%s]\n", tno, str.z);
30557
+ sqlite3_fprintf(p->out, "%d: Expected: [%s]\n", tno, zAns);
30558
+ sqlite3_fprintf(p->out, "%d: Got: [%s]\n", tno, str.z);
3102930559
}
3103030560
}
3103130561
else{
31032
- eputf("Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
30562
+ sqlite3_fprintf(stderr,
30563
+ "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
3103330564
rc = 1;
3103430565
break;
3103530566
}
3103630567
} /* End loop over rows of content from SELFTEST */
3103730568
sqlite3_finalize(pStmt);
3103830569
} /* End loop over k */
3103930570
freeText(&str);
31040
- oputf("%d errors out of %d tests\n", nErr, nTest);
30571
+ sqlite3_fprintf(p->out, "%d errors out of %d tests\n", nErr, nTest);
3104130572
}else
3104230573
3104330574
if( c=='s' && cli_strncmp(azArg[0], "separator", n)==0 ){
3104430575
if( nArg<2 || nArg>3 ){
3104530576
eputz("Usage: .separator COL ?ROW?\n");
@@ -31083,11 +30614,12 @@
3108330614
}else
3108430615
if( cli_strcmp(z,"debug")==0 ){
3108530616
bDebug = 1;
3108630617
}else
3108730618
{
31088
- eputf("Unknown option \"%s\" on \"%s\"\n", azArg[i], azArg[0]);
30619
+ sqlite3_fprintf(stderr,
30620
+ "Unknown option \"%s\" on \"%s\"\n", azArg[i], azArg[0]);
3108930621
showHelp(p->out, azArg[0]);
3109030622
rc = 1;
3109130623
goto meta_command_exit;
3109230624
}
3109330625
}else if( zLike ){
@@ -31161,11 +30693,11 @@
3116130693
}
3116230694
shell_check_oom(zSql);
3116330695
freeText(&sQuery);
3116430696
freeText(&sSql);
3116530697
if( bDebug ){
31166
- oputf("%s\n", zSql);
30698
+ sqlite3_fprintf(p->out, "%s\n", zSql);
3116730699
}else{
3116830700
shell_exec(p, zSql, 0);
3116930701
}
3117030702
#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && !defined(SQLITE_OMIT_VIRTUALTABLE)
3117130703
{
@@ -31191,11 +30723,11 @@
3119130723
"||group_concat('CAST(CAST('||cname||' AS BLOB) AS TEXT)<>'||cname\n"
3119230724
"|| ' AND typeof('||cname||')=''text'' ',\n"
3119330725
"' OR ') as query, tname from tabcols group by tname)"
3119430726
, zRevText);
3119530727
shell_check_oom(zRevText);
31196
- if( bDebug ) oputf("%s\n", zRevText);
30728
+ if( bDebug ) sqlite3_fprintf(p->out, "%s\n", zRevText);
3119730729
lrc = sqlite3_prepare_v2(p->db, zRevText, -1, &pStmt, 0);
3119830730
if( lrc!=SQLITE_OK ){
3119930731
/* assert(lrc==SQLITE_NOMEM); // might also be SQLITE_ERROR if the
3120030732
** user does cruel and unnatural things like ".limit expr_depth 0". */
3120130733
rc = 1;
@@ -31204,19 +30736,20 @@
3120430736
lrc = SQLITE_ROW==sqlite3_step(pStmt);
3120530737
if( lrc ){
3120630738
const char *zGenQuery = (char*)sqlite3_column_text(pStmt,0);
3120730739
sqlite3_stmt *pCheckStmt;
3120830740
lrc = sqlite3_prepare_v2(p->db, zGenQuery, -1, &pCheckStmt, 0);
31209
- if( bDebug ) oputf("%s\n", zGenQuery);
30741
+ if( bDebug ) sqlite3_fprintf(p->out, "%s\n", zGenQuery);
3121030742
if( lrc!=SQLITE_OK ){
3121130743
rc = 1;
3121230744
}else{
3121330745
if( SQLITE_ROW==sqlite3_step(pCheckStmt) ){
3121430746
double countIrreversible = sqlite3_column_double(pCheckStmt, 0);
3121530747
if( countIrreversible>0 ){
3121630748
int sz = (int)(countIrreversible + 0.5);
31217
- eputf("Digest includes %d invalidly encoded text field%s.\n",
30749
+ sqlite3_fprintf(stderr,
30750
+ "Digest includes %d invalidly encoded text field%s.\n",
3121830751
sz, (sz>1)? "s": "");
3121930752
}
3122030753
}
3122130754
sqlite3_finalize(pCheckStmt);
3122230755
}
@@ -31246,15 +30779,15 @@
3124630779
zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
3124730780
for(i=2; i<nArg && zCmd!=0; i++){
3124830781
zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
3124930782
zCmd, azArg[i]);
3125030783
}
31251
- consoleRestore();
30784
+ /*consoleRestore();*/
3125230785
x = zCmd!=0 ? system(zCmd) : 1;
31253
- consoleRenewSetup();
30786
+ /*consoleRenewSetup();*/
3125430787
sqlite3_free(zCmd);
31255
- if( x ) eputf("System command returns %d\n", x);
30788
+ if( x ) sqlite3_fprintf(stderr,"System command returns %d\n", x);
3125630789
}else
3125730790
#endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) */
3125830791
3125930792
if( c=='s' && cli_strncmp(azArg[0], "show", n)==0 ){
3126030793
static const char *azBool[] = { "off", "on", "trigger", "full"};
@@ -31263,50 +30796,52 @@
3126330796
if( nArg!=1 ){
3126430797
eputz("Usage: .show\n");
3126530798
rc = 1;
3126630799
goto meta_command_exit;
3126730800
}
31268
- oputf("%12.12s: %s\n","echo",
30801
+ sqlite3_fprintf(p->out, "%12.12s: %s\n","echo",
3126930802
azBool[ShellHasFlag(p, SHFLG_Echo)]);
31270
- oputf("%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
31271
- oputf("%12.12s: %s\n","explain",
30803
+ sqlite3_fprintf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
30804
+ sqlite3_fprintf(p->out, "%12.12s: %s\n","explain",
3127230805
p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
31273
- oputf("%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
30806
+ sqlite3_fprintf(p->out, "%12.12s: %s\n","headers",
30807
+ azBool[p->showHeader!=0]);
3127430808
if( p->mode==MODE_Column
3127530809
|| (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
3127630810
){
31277
- oputf("%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode",
30811
+ sqlite3_fprintf(p->out,
30812
+ "%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode",
3127830813
modeDescr[p->mode], p->cmOpts.iWrap,
3127930814
p->cmOpts.bWordWrap ? "on" : "off",
3128030815
p->cmOpts.bQuote ? "" : "no");
3128130816
}else{
31282
- oputf("%12.12s: %s\n","mode", modeDescr[p->mode]);
30817
+ sqlite3_fprintf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
3128330818
}
31284
- oputf("%12.12s: ", "nullvalue");
31285
- output_c_string(p->nullValue);
31286
- oputz("\n");
31287
- oputf("%12.12s: %s\n","output",
30819
+ sqlite3_fprintf(p->out, "%12.12s: ", "nullvalue");
30820
+ output_c_string(p->out, p->nullValue);
30821
+ sqlite3_fputs("\n", p->out);
30822
+ sqlite3_fprintf(p->out, "%12.12s: %s\n","output",
3128830823
strlen30(p->outfile) ? p->outfile : "stdout");
31289
- oputf("%12.12s: ", "colseparator");
31290
- output_c_string(p->colSeparator);
31291
- oputz("\n");
31292
- oputf("%12.12s: ", "rowseparator");
31293
- output_c_string(p->rowSeparator);
31294
- oputz("\n");
30824
+ sqlite3_fprintf(p->out, "%12.12s: ", "colseparator");
30825
+ output_c_string(p->out, p->colSeparator);
30826
+ sqlite3_fputs("\n", p->out);
30827
+ sqlite3_fprintf(p->out, "%12.12s: ", "rowseparator");
30828
+ output_c_string(p->out, p->rowSeparator);
30829
+ sqlite3_fputs("\n", p->out);
3129530830
switch( p->statsOn ){
3129630831
case 0: zOut = "off"; break;
3129730832
default: zOut = "on"; break;
3129830833
case 2: zOut = "stmt"; break;
3129930834
case 3: zOut = "vmstep"; break;
3130030835
}
31301
- oputf("%12.12s: %s\n","stats", zOut);
31302
- oputf("%12.12s: ", "width");
30836
+ sqlite3_fprintf(p->out, "%12.12s: %s\n","stats", zOut);
30837
+ sqlite3_fprintf(p->out, "%12.12s: ", "width");
3130330838
for (i=0;i<p->nWidth;i++) {
31304
- oputf("%d ", p->colWidth[i]);
30839
+ sqlite3_fprintf(p->out, "%d ", p->colWidth[i]);
3130530840
}
31306
- oputz("\n");
31307
- oputf("%12.12s: %s\n", "filename",
30841
+ sqlite3_fputs("\n", p->out);
30842
+ sqlite3_fprintf(p->out, "%12.12s: %s\n", "filename",
3130830843
p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : "");
3130930844
}else
3131030845
3131130846
if( c=='s' && cli_strncmp(azArg[0], "stats", n)==0 ){
3131230847
if( nArg==2 ){
@@ -31420,13 +30955,14 @@
3142030955
if( nPrintCol<1 ) nPrintCol = 1;
3142130956
nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
3142230957
for(i=0; i<nPrintRow; i++){
3142330958
for(j=i; j<nRow; j+=nPrintRow){
3142430959
char *zSp = j<nPrintRow ? "" : " ";
31425
- oputf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j]:"");
30960
+ sqlite3_fprintf(p->out,
30961
+ "%s%-*s", zSp, maxlen, azResult[j] ? azResult[j]:"");
3142630962
}
31427
- oputz("\n");
30963
+ sqlite3_fputs("\n", p->out);
3142830964
}
3142930965
}
3143030966
3143130967
for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
3143230968
sqlite3_free(azResult);
@@ -31498,14 +31034,14 @@
3149831034
if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
3149931035
}
3150031036
3150131037
/* --help lists all test-controls */
3150231038
if( cli_strcmp(zCmd,"help")==0 ){
31503
- oputz("Available test-controls:\n");
31039
+ sqlite3_fputs("Available test-controls:\n", p->out);
3150431040
for(i=0; i<ArraySize(aCtrl); i++){
3150531041
if( aCtrl[i].unSafe && !ShellHasFlag(p,SHFLG_TestingMode) ) continue;
31506
- oputf(" .testctrl %s %s\n",
31042
+ sqlite3_fprintf(p->out, " .testctrl %s %s\n",
3150731043
aCtrl[i].zCtrlName, aCtrl[i].zUsage);
3150831044
}
3150931045
rc = 1;
3151031046
goto meta_command_exit;
3151131047
}
@@ -31518,19 +31054,19 @@
3151831054
if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
3151931055
if( testctrl<0 ){
3152031056
testctrl = aCtrl[i].ctrlCode;
3152131057
iCtrl = i;
3152231058
}else{
31523
- eputf("Error: ambiguous test-control: \"%s\"\n"
31059
+ sqlite3_fprintf(stderr,"Error: ambiguous test-control: \"%s\"\n"
3152431060
"Use \".testctrl --help\" for help\n", zCmd);
3152531061
rc = 1;
3152631062
goto meta_command_exit;
3152731063
}
3152831064
}
3152931065
}
3153031066
if( testctrl<0 ){
31531
- eputf("Error: unknown test-control: %s\n"
31067
+ sqlite3_fprintf(stderr,"Error: unknown test-control: %s\n"
3153231068
"Use \".testctrl --help\" for help\n", zCmd);
3153331069
}else{
3153431070
switch(testctrl){
3153531071
3153631072
/* Special processing for .testctrl opt MASK ...
@@ -31602,16 +31138,17 @@
3160231138
int jj;
3160331139
for(jj=0; jj<ArraySize(aLabel); jj++){
3160431140
if( sqlite3_stricmp(zLabel, aLabel[jj].zLabel)==0 ) break;
3160531141
}
3160631142
if( jj>=ArraySize(aLabel) ){
31607
- eputf("Error: no such optimization: \"%s\"\n", zLabel);
31608
- eputz("Should be one of:");
31143
+ sqlite3_fprintf(stderr,
31144
+ "Error: no such optimization: \"%s\"\n", zLabel);
31145
+ sqlite3_fputs("Should be one of:", stderr);
3160931146
for(jj=0; jj<ArraySize(aLabel); jj++){
31610
- eputf(" %s", aLabel[jj].zLabel);
31147
+ sqlite3_fprintf(stderr," %s", aLabel[jj].zLabel);
3161131148
}
31612
- eputz("\n");
31149
+ sqlite3_fputs("\n", stderr);
3161331150
rc = 1;
3161431151
goto meta_command_exit;
3161531152
}
3161631153
if( useLabel=='+' ){
3161731154
newOpt &= ~aLabel[jj].mask;
@@ -31624,20 +31161,20 @@
3162431161
sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,p->db,newOpt);
3162531162
}else if( nArg<3 ){
3162631163
curOpt = ~newOpt;
3162731164
}
3162831165
if( newOpt==0 ){
31629
- oputz("+All\n");
31166
+ sqlite3_fputs("+All\n", p->out);
3163031167
}else if( newOpt==0xffffffff ){
31631
- oputz("-All\n");
31168
+ sqlite3_fputs("-All\n", p->out);
3163231169
}else{
3163331170
int jj;
3163431171
for(jj=0; jj<ArraySize(aLabel); jj++){
3163531172
unsigned int m = aLabel[jj].mask;
3163631173
if( !aLabel[jj].bDsply ) continue;
3163731174
if( (curOpt&m)!=(newOpt&m) ){
31638
- oputf("%c%s\n", (newOpt & m)==0 ? '+' : '-',
31175
+ sqlite3_fprintf(p->out, "%c%s\n", (newOpt & m)==0 ? '+' : '-',
3163931176
aLabel[jj].zLabel);
3164031177
}
3164131178
}
3164231179
}
3164331180
rc2 = isOk = 3;
@@ -31677,11 +31214,11 @@
3167731214
if( nArg==3 || nArg==4 ){
3167831215
int ii = (int)integerValue(azArg[2]);
3167931216
sqlite3 *db;
3168031217
if( ii==0 && cli_strcmp(azArg[2],"random")==0 ){
3168131218
sqlite3_randomness(sizeof(ii),&ii);
31682
- sputf(stdout, "-- random seed: %d\n", ii);
31219
+ sqlite3_fprintf(stdout, "-- random seed: %d\n", ii);
3168331220
}
3168431221
if( nArg==3 ){
3168531222
db = 0;
3168631223
}else{
3168731224
db = p->db;
@@ -31745,11 +31282,11 @@
3174531282
break;
3174631283
3174731284
case SQLITE_TESTCTRL_SEEK_COUNT: {
3174831285
u64 x = 0;
3174931286
rc2 = sqlite3_test_control(testctrl, p->db, &x);
31750
- oputf("%llu\n", x);
31287
+ sqlite3_fprintf(p->out, "%llu\n", x);
3175131288
isOk = 3;
3175231289
break;
3175331290
}
3175431291
3175531292
#ifdef YYCOVERAGE
@@ -31776,15 +31313,15 @@
3177631313
int id = 1;
3177731314
while(1){
3177831315
int val = 0;
3177931316
rc2 = sqlite3_test_control(testctrl, -id, &val);
3178031317
if( rc2!=SQLITE_OK ) break;
31781
- if( id>1 ) oputz(" ");
31782
- oputf("%d: %d", id, val);
31318
+ if( id>1 ) sqlite3_fputs(" ", p->out);
31319
+ sqlite3_fprintf(p->out, "%d: %d", id, val);
3178331320
id++;
3178431321
}
31785
- if( id>1 ) oputz("\n");
31322
+ if( id>1 ) sqlite3_fputs("\n", p->out);
3178631323
isOk = 3;
3178731324
}
3178831325
break;
3178931326
}
3179031327
#endif
@@ -31822,18 +31359,26 @@
3182231359
}else if( cli_strcmp(z,"reset")==0 ){
3182331360
faultsim_state.iCnt = faultsim_state.nSkip;
3182431361
faultsim_state.nHit = 0;
3182531362
sqlite3_test_control(testctrl, faultsim_callback);
3182631363
}else if( cli_strcmp(z,"status")==0 ){
31827
- oputf("faultsim.iId: %d\n", faultsim_state.iId);
31828
- oputf("faultsim.iErr: %d\n", faultsim_state.iErr);
31829
- oputf("faultsim.iCnt: %d\n", faultsim_state.iCnt);
31830
- oputf("faultsim.nHit: %d\n", faultsim_state.nHit);
31831
- oputf("faultsim.iInterval: %d\n", faultsim_state.iInterval);
31832
- oputf("faultsim.eVerbose: %d\n", faultsim_state.eVerbose);
31833
- oputf("faultsim.nRepeat: %d\n", faultsim_state.nRepeat);
31834
- oputf("faultsim.nSkip: %d\n", faultsim_state.nSkip);
31364
+ sqlite3_fprintf(p->out, "faultsim.iId: %d\n",
31365
+ faultsim_state.iId);
31366
+ sqlite3_fprintf(p->out, "faultsim.iErr: %d\n",
31367
+ faultsim_state.iErr);
31368
+ sqlite3_fprintf(p->out, "faultsim.iCnt: %d\n",
31369
+ faultsim_state.iCnt);
31370
+ sqlite3_fprintf(p->out, "faultsim.nHit: %d\n",
31371
+ faultsim_state.nHit);
31372
+ sqlite3_fprintf(p->out, "faultsim.iInterval: %d\n",
31373
+ faultsim_state.iInterval);
31374
+ sqlite3_fprintf(p->out, "faultsim.eVerbose: %d\n",
31375
+ faultsim_state.eVerbose);
31376
+ sqlite3_fprintf(p->out, "faultsim.nRepeat: %d\n",
31377
+ faultsim_state.nRepeat);
31378
+ sqlite3_fprintf(p->out, "faultsim.nSkip: %d\n",
31379
+ faultsim_state.nSkip);
3183531380
}else if( cli_strcmp(z,"-v")==0 ){
3183631381
if( faultsim_state.eVerbose<2 ) faultsim_state.eVerbose++;
3183731382
}else if( cli_strcmp(z,"-q")==0 ){
3183831383
if( faultsim_state.eVerbose>0 ) faultsim_state.eVerbose--;
3183931384
}else if( cli_strcmp(z,"-id")==0 && kk+1<nArg ){
@@ -31847,19 +31392,20 @@
3184731392
}else if( cli_strcmp(z,"-skip")==0 && kk+1<nArg ){
3184831393
faultsim_state.nSkip = atoi(azArg[++kk]);
3184931394
}else if( cli_strcmp(z,"-?")==0 || sqlite3_strglob("*help*",z)==0){
3185031395
bShowHelp = 1;
3185131396
}else{
31852
- eputf("Unrecognized fault_install argument: \"%s\"\n",
31397
+ sqlite3_fprintf(stderr,
31398
+ "Unrecognized fault_install argument: \"%s\"\n",
3185331399
azArg[kk]);
3185431400
rc = 1;
3185531401
bShowHelp = 1;
3185631402
break;
3185731403
}
3185831404
}
3185931405
if( bShowHelp ){
31860
- oputz(
31406
+ sqlite3_fputs(
3186131407
"Usage: .testctrl fault_install ARGS\n"
3186231408
"Possible arguments:\n"
3186331409
" off Disable faultsim\n"
3186431410
" on Activate faultsim\n"
3186531411
" reset Reset the trigger counter\n"
@@ -31869,23 +31415,25 @@
3186931415
" --errcode N When triggered, return N as error code\n"
3187031416
" --id ID Trigger only for the ID specified\n"
3187131417
" --interval N Trigger only after every N-th call\n"
3187231418
" --repeat N Turn off after N hits. 0 means never\n"
3187331419
" --skip N Skip the first N encounters\n"
31420
+ ,p->out
3187431421
);
3187531422
}
3187631423
break;
3187731424
}
3187831425
}
3187931426
}
3188031427
if( isOk==0 && iCtrl>=0 ){
31881
- oputf("Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
31428
+ sqlite3_fprintf(p->out,
31429
+ "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
3188231430
rc = 1;
3188331431
}else if( isOk==1 ){
31884
- oputf("%d\n", rc2);
31432
+ sqlite3_fprintf(p->out, "%d\n", rc2);
3188531433
}else if( isOk==2 ){
31886
- oputf("0x%08x\n", rc2);
31434
+ sqlite3_fprintf(p->out, "0x%08x\n", rc2);
3188731435
}
3188831436
}else
3188931437
#endif /* !defined(SQLITE_UNTESTABLE) */
3189031438
3189131439
if( c=='t' && n>4 && cli_strncmp(azArg[0], "timeout", n)==0 ){
@@ -31936,11 +31484,11 @@
3193631484
}
3193731485
else if( optionMatch(z, "close") ){
3193831486
mType |= SQLITE_TRACE_CLOSE;
3193931487
}
3194031488
else {
31941
- eputf("Unknown option \"%s\" on \".trace\"\n", z);
31489
+ sqlite3_fprintf(stderr,"Unknown option \"%s\" on \".trace\"\n", z);
3194231490
rc = 1;
3194331491
goto meta_command_exit;
3194431492
}
3194531493
}else{
3194631494
output_file_close(p->traceOut);
@@ -31996,11 +31544,11 @@
3199631544
goto meta_command_exit;
3199731545
}
3199831546
rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
3199931547
strlen30(azArg[3]));
3200031548
if( rc ){
32001
- eputf("Authentication failed for user %s\n", azArg[2]);
31549
+ sqlite3_fprintf(stderr,"Authentication failed for user %s\n", azArg[2]);
3200231550
rc = 1;
3200331551
}
3200431552
}else if( cli_strcmp(azArg[1],"add")==0 ){
3200531553
if( nArg!=5 ){
3200631554
eputz("Usage: .user add USER PASSWORD ISADMIN\n");
@@ -32008,11 +31556,11 @@
3200831556
goto meta_command_exit;
3200931557
}
3201031558
rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
3201131559
booleanValue(azArg[4]));
3201231560
if( rc ){
32013
- eputf("User-Add failed: %d\n", rc);
31561
+ sqlite3_fprintf(stderr,"User-Add failed: %d\n", rc);
3201431562
rc = 1;
3201531563
}
3201631564
}else if( cli_strcmp(azArg[1],"edit")==0 ){
3201731565
if( nArg!=5 ){
3201831566
eputz("Usage: .user edit USER PASSWORD ISADMIN\n");
@@ -32020,11 +31568,11 @@
3202031568
goto meta_command_exit;
3202131569
}
3202231570
rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
3202331571
booleanValue(azArg[4]));
3202431572
if( rc ){
32025
- eputf("User-Edit failed: %d\n", rc);
31573
+ sqlite3_fprintf(stderr,"User-Edit failed: %d\n", rc);
3202631574
rc = 1;
3202731575
}
3202831576
}else if( cli_strcmp(azArg[1],"delete")==0 ){
3202931577
if( nArg!=3 ){
3203031578
eputz("Usage: .user delete USER\n");
@@ -32031,11 +31579,11 @@
3203131579
rc = 1;
3203231580
goto meta_command_exit;
3203331581
}
3203431582
rc = sqlite3_user_delete(p->db, azArg[2]);
3203531583
if( rc ){
32036
- eputf("User-Delete failed: %d\n", rc);
31584
+ sqlite3_fprintf(stderr,"User-Delete failed: %d\n", rc);
3203731585
rc = 1;
3203831586
}
3203931587
}else{
3204031588
eputz("Usage: .user login|add|edit|delete ...\n");
3204131589
rc = 1;
@@ -32044,38 +31592,38 @@
3204431592
}else
3204531593
#endif /* SQLITE_USER_AUTHENTICATION */
3204631594
3204731595
if( c=='v' && cli_strncmp(azArg[0], "version", n)==0 ){
3204831596
char *zPtrSz = sizeof(void*)==8 ? "64-bit" : "32-bit";
32049
- oputf("SQLite %s %s\n" /*extra-version-info*/,
31597
+ sqlite3_fprintf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
3205031598
sqlite3_libversion(), sqlite3_sourceid());
3205131599
#if SQLITE_HAVE_ZLIB
32052
- oputf("zlib version %s\n", zlibVersion());
31600
+ sqlite3_fprintf(p->out, "zlib version %s\n", zlibVersion());
3205331601
#endif
3205431602
#define CTIMEOPT_VAL_(opt) #opt
3205531603
#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
3205631604
#if defined(__clang__) && defined(__clang_major__)
32057
- oputf("clang-" CTIMEOPT_VAL(__clang_major__) "."
31605
+ sqlite3_fprintf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
3205831606
CTIMEOPT_VAL(__clang_minor__) "."
3205931607
CTIMEOPT_VAL(__clang_patchlevel__) " (%s)\n", zPtrSz);
3206031608
#elif defined(_MSC_VER)
32061
- oputf("msvc-" CTIMEOPT_VAL(_MSC_VER) " (%s)\n", zPtrSz);
31609
+ sqlite3_fprintf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) " (%s)\n", zPtrSz);
3206231610
#elif defined(__GNUC__) && defined(__VERSION__)
32063
- oputf("gcc-" __VERSION__ " (%s)\n", zPtrSz);
31611
+ sqlite3_fprintf(p->out, "gcc-" __VERSION__ " (%s)\n", zPtrSz);
3206431612
#endif
3206531613
}else
3206631614
3206731615
if( c=='v' && cli_strncmp(azArg[0], "vfsinfo", n)==0 ){
3206831616
const char *zDbName = nArg==2 ? azArg[1] : "main";
3206931617
sqlite3_vfs *pVfs = 0;
3207031618
if( p->db ){
3207131619
sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
3207231620
if( pVfs ){
32073
- oputf("vfs.zName = \"%s\"\n", pVfs->zName);
32074
- oputf("vfs.iVersion = %d\n", pVfs->iVersion);
32075
- oputf("vfs.szOsFile = %d\n", pVfs->szOsFile);
32076
- oputf("vfs.mxPathname = %d\n", pVfs->mxPathname);
31621
+ sqlite3_fprintf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName);
31622
+ sqlite3_fprintf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
31623
+ sqlite3_fprintf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
31624
+ sqlite3_fprintf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
3207731625
}
3207831626
}
3207931627
}else
3208031628
3208131629
if( c=='v' && cli_strncmp(azArg[0], "vfslist", n)==0 ){
@@ -32083,17 +31631,17 @@
3208331631
sqlite3_vfs *pCurrent = 0;
3208431632
if( p->db ){
3208531633
sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
3208631634
}
3208731635
for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
32088
- oputf("vfs.zName = \"%s\"%s\n", pVfs->zName,
31636
+ sqlite3_fprintf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName,
3208931637
pVfs==pCurrent ? " <--- CURRENT" : "");
32090
- oputf("vfs.iVersion = %d\n", pVfs->iVersion);
32091
- oputf("vfs.szOsFile = %d\n", pVfs->szOsFile);
32092
- oputf("vfs.mxPathname = %d\n", pVfs->mxPathname);
31638
+ sqlite3_fprintf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
31639
+ sqlite3_fprintf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
31640
+ sqlite3_fprintf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
3209331641
if( pVfs->pNext ){
32094
- oputz("-----------------------------------\n");
31642
+ sqlite3_fputs("-----------------------------------\n", p->out);
3209531643
}
3209631644
}
3209731645
}else
3209831646
3209931647
if( c=='v' && cli_strncmp(azArg[0], "vfsname", n)==0 ){
@@ -32100,11 +31648,11 @@
3210031648
const char *zDbName = nArg==2 ? azArg[1] : "main";
3210131649
char *zVfsName = 0;
3210231650
if( p->db ){
3210331651
sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
3210431652
if( zVfsName ){
32105
- oputf("%s\n", zVfsName);
31653
+ sqlite3_fprintf(p->out, "%s\n", zVfsName);
3210631654
sqlite3_free(zVfsName);
3210731655
}
3210831656
}
3210931657
}else
3211031658
@@ -32124,11 +31672,11 @@
3212431672
p->colWidth[j-1] = (int)integerValue(azArg[j]);
3212531673
}
3212631674
}else
3212731675
3212831676
{
32129
- eputf("Error: unknown command or invalid arguments: "
31677
+ sqlite3_fprintf(stderr,"Error: unknown command or invalid arguments: "
3213031678
" \"%s\". Enter \".help\" for help\n", azArg[0]);
3213131679
rc = 1;
3213231680
}
3213331681
3213431682
meta_command_exit:
@@ -32376,11 +31924,11 @@
3237631924
open_db(p, 0);
3237731925
if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
3237831926
if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
3237931927
BEGIN_TIMER;
3238031928
rc = shell_exec(p, zSql, &zErrMsg);
32381
- END_TIMER;
31929
+ END_TIMER(p->out);
3238231930
if( rc || zErrMsg ){
3238331931
char zPrefix[100];
3238431932
const char *zErrorTail;
3238531933
const char *zErrorType;
3238631934
if( zErrMsg==0 ){
@@ -32400,28 +31948,28 @@
3240031948
sqlite3_snprintf(sizeof(zPrefix), zPrefix,
3240131949
"%s near line %d:", zErrorType, startline);
3240231950
}else{
3240331951
sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:", zErrorType);
3240431952
}
32405
- eputf("%s %s\n", zPrefix, zErrorTail);
31953
+ sqlite3_fprintf(stderr,"%s %s\n", zPrefix, zErrorTail);
3240631954
sqlite3_free(zErrMsg);
3240731955
zErrMsg = 0;
3240831956
return 1;
3240931957
}else if( ShellHasFlag(p, SHFLG_CountChanges) ){
3241031958
char zLineBuf[2000];
3241131959
sqlite3_snprintf(sizeof(zLineBuf), zLineBuf,
3241231960
"changes: %lld total_changes: %lld",
3241331961
sqlite3_changes64(p->db), sqlite3_total_changes64(p->db));
32414
- oputf("%s\n", zLineBuf);
31962
+ sqlite3_fprintf(p->out, "%s\n", zLineBuf);
3241531963
}
3241631964
3241731965
if( doAutoDetectRestore(p, zSql) ) return 1;
3241831966
return 0;
3241931967
}
3242031968
3242131969
static void echo_group_input(ShellState *p, const char *zDo){
32422
- if( ShellHasFlag(p, SHFLG_Echo) ) oputf("%s\n", zDo);
31970
+ if( ShellHasFlag(p, SHFLG_Echo) ) sqlite3_fprintf(p->out, "%s\n", zDo);
3242331971
}
3242431972
3242531973
#ifdef SQLITE_SHELL_FIDDLE
3242631974
/*
3242731975
** Alternate one_input_line() impl for wasm mode. This is not in the primary
@@ -32475,11 +32023,11 @@
3247532023
i64 startline = 0; /* Line number for start of current input */
3247632024
QuickScanState qss = QSS_Start; /* Accumulated line status (so far) */
3247732025
3247832026
if( p->inputNesting==MAX_INPUT_NESTING ){
3247932027
/* This will be more informative in a later version. */
32480
- eputf("Input nesting limit (%d) reached at line %d."
32028
+ sqlite3_fprintf(stderr,"Input nesting limit (%d) reached at line %d."
3248132029
" Check recursion.\n", MAX_INPUT_NESTING, p->lineno);
3248232030
return 1;
3248332031
}
3248432032
++p->inputNesting;
3248532033
p->lineno = 0;
@@ -32487,11 +32035,11 @@
3248732035
while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
3248832036
fflush(p->out);
3248932037
zLine = one_input_line(p->in, zLine, nSql>0);
3249032038
if( zLine==0 ){
3249132039
/* End of input */
32492
- if( p->in==0 && stdin_is_interactive ) oputz("\n");
32040
+ if( p->in==0 && stdin_is_interactive ) sqlite3_fputs("\n", p->out);
3249332041
break;
3249432042
}
3249532043
if( seenInterrupt ){
3249632044
if( p->in!=0 ) break;
3249732045
seenInterrupt = 0;
@@ -32707,19 +32255,19 @@
3270732255
}
3270832256
zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
3270932257
shell_check_oom(zBuf);
3271032258
sqliterc = zBuf;
3271132259
}
32712
- p->in = fopen(sqliterc,"rb");
32260
+ p->in = sqlite3_fopen(sqliterc,"rb");
3271332261
if( p->in ){
3271432262
if( stdin_is_interactive ){
32715
- eputf("-- Loading resources from %s\n", sqliterc);
32263
+ sqlite3_fprintf(stderr,"-- Loading resources from %s\n", sqliterc);
3271632264
}
3271732265
if( process_input(p) && bail_on_error ) exit(1);
3271832266
fclose(p->in);
3271932267
}else if( sqliterc_override!=0 ){
32720
- eputf("cannot open: \"%s\"\n", sqliterc);
32268
+ sqlite3_fprintf(stderr,"cannot open: \"%s\"\n", sqliterc);
3272132269
if( bail_on_error ) exit(1);
3272232270
}
3272332271
p->in = inSaved;
3272432272
p->lineno = savedLineno;
3272532273
sqlite3_free(zBuf);
@@ -32790,15 +32338,15 @@
3279032338
#ifdef SQLITE_HAVE_ZLIB
3279132339
" -zip open the file as a ZIP Archive\n"
3279232340
#endif
3279332341
;
3279432342
static void usage(int showDetail){
32795
- eputf("Usage: %s [OPTIONS] [FILENAME [SQL]]\n"
32343
+ sqlite3_fprintf(stderr,"Usage: %s [OPTIONS] [FILENAME [SQL]]\n"
3279632344
"FILENAME is the name of an SQLite database. A new database is created\n"
3279732345
"if the file does not previously exist. Defaults to :memory:.\n", Argv0);
3279832346
if( showDetail ){
32799
- eputf("OPTIONS include:\n%s", zOptions);
32347
+ sqlite3_fprintf(stderr,"OPTIONS include:\n%s", zOptions);
3280032348
}else{
3280132349
eputz("Use the -help option for additional information\n");
3280232350
}
3280332351
exit(0);
3280432352
}
@@ -32854,21 +32402,22 @@
3285432402
SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
3285532403
#endif
3285632404
}
3285732405
#else
3285832406
static void printBold(const char *zText){
32859
- sputf(stdout, "\033[1m%s\033[0m", zText);
32407
+ sqlite3_fprintf(stdout, "\033[1m%s\033[0m", zText);
3286032408
}
3286132409
#endif
3286232410
3286332411
/*
3286432412
** Get the argument to an --option. Throw an error and die if no argument
3286532413
** is available.
3286632414
*/
3286732415
static char *cmdline_option_value(int argc, char **argv, int i){
3286832416
if( i==argc ){
32869
- eputf("%s: Error: missing argument to %s\n", argv[0], argv[argc-1]);
32417
+ sqlite3_fprintf(stderr,
32418
+ "%s: Error: missing argument to %s\n", argv[0], argv[argc-1]);
3287032419
exit(1);
3287132420
}
3287232421
return argv[i];
3287332422
}
3287432423
@@ -32901,11 +32450,10 @@
3290132450
char *zErrMsg = 0;
3290232451
#ifdef SQLITE_SHELL_FIDDLE
3290332452
# define data shellState
3290432453
#else
3290532454
ShellState data;
32906
- StreamsAreConsole consStreams = SAC_NoConsole;
3290732455
#endif
3290832456
const char *zInitFile = 0;
3290932457
int i;
3291032458
int rc = 0;
3291132459
int warnInmemoryDb = 0;
@@ -32924,25 +32472,25 @@
3292432472
#ifdef SQLITE_SHELL_FIDDLE
3292532473
stdin_is_interactive = 0;
3292632474
stdout_is_console = 1;
3292732475
data.wasm.zDefaultDbName = "/fiddle.sqlite3";
3292832476
#else
32929
- consStreams = consoleClassifySetup(stdin, stdout, stderr);
32930
- stdin_is_interactive = (consStreams & SAC_InConsole)!=0;
32931
- stdout_is_console = (consStreams & SAC_OutConsole)!=0;
32932
- atexit(consoleRestore);
32477
+ stdin_is_interactive = isatty(0);
32478
+ stdout_is_console = isatty(1);
3293332479
#endif
3293432480
atexit(sayAbnormalExit);
3293532481
#ifdef SQLITE_DEBUG
3293632482
mem_main_enter = sqlite3_memory_used();
3293732483
#endif
3293832484
#if !defined(_WIN32_WCE)
3293932485
if( getenv("SQLITE_DEBUG_BREAK") ){
3294032486
if( isatty(0) && isatty(2) ){
32941
- eputf("attach debugger to process %d and press any key to continue.\n",
32487
+ char zLine[100];
32488
+ sqlite3_fprintf(stderr,
32489
+ "attach debugger to process %d and press ENTER to continue...",
3294232490
GETPID());
32943
- fgetc(stdin);
32491
+ sqlite3_fgets(zLine, sizeof(zLine), stdin);
3294432492
}else{
3294532493
#if defined(_WIN32) || defined(WIN32)
3294632494
#if SQLITE_OS_WINRT
3294732495
__debugbreak();
3294832496
#else
@@ -32963,11 +32511,12 @@
3296332511
}
3296432512
#endif
3296532513
3296632514
#if USE_SYSTEM_SQLITE+0!=1
3296732515
if( cli_strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
32968
- eputf("SQLite header and source version mismatch\n%s\n%s\n",
32516
+ sqlite3_fprintf(stderr,
32517
+ "SQLite header and source version mismatch\n%s\n%s\n",
3296932518
sqlite3_sourceid(), SQLITE_SOURCE_ID);
3297032519
exit(1);
3297132520
}
3297232521
#endif
3297332522
main_init(&data);
@@ -33106,11 +32655,12 @@
3310632655
case 0: sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); break;
3310732656
case 2: sqlite3_config(SQLITE_CONFIG_MULTITHREAD); break;
3310832657
default: sqlite3_config(SQLITE_CONFIG_SERIALIZED); break;
3310932658
}
3311032659
}else if( cli_strcmp(z,"-vfstrace")==0 ){
33111
- vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
32660
+ vfstrace_register("trace",0,(int(*)(const char*,void*))sqlite3_fputs,
32661
+ stderr,1);
3311232662
bEnableVfstrace = 1;
3311332663
#ifdef SQLITE_ENABLE_MULTIPLEX
3311432664
}else if( cli_strcmp(z,"-multiplex")==0 ){
3311532665
extern int sqlite3_multiplex_initialize(const char*,int);
3311632666
sqlite3_multiplex_initialize(0, 1);
@@ -33187,21 +32737,22 @@
3318732737
if( zVfs ){
3318832738
sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
3318932739
if( pVfs ){
3319032740
sqlite3_vfs_register(pVfs, 1);
3319132741
}else{
33192
- eputf("no such VFS: \"%s\"\n", zVfs);
32742
+ sqlite3_fprintf(stderr,"no such VFS: \"%s\"\n", zVfs);
3319332743
exit(1);
3319432744
}
3319532745
}
3319632746
3319732747
if( data.pAuxDb->zDbFilename==0 ){
3319832748
#ifndef SQLITE_OMIT_MEMORYDB
3319932749
data.pAuxDb->zDbFilename = ":memory:";
3320032750
warnInmemoryDb = argc==1;
3320132751
#else
33202
- eputf("%s: Error: no database filename specified\n", Argv0);
32752
+ sqlite3_fprintf(stderr,
32753
+ "%s: Error: no database filename specified\n", Argv0);
3320332754
return 1;
3320432755
#endif
3320532756
}
3320632757
data.out = stdout;
3320732758
#ifndef SQLITE_SHELL_FIDDLE
@@ -33314,11 +32865,11 @@
3331432865
*/
3331532866
ShellSetFlag(&data, SHFLG_Backslash);
3331632867
}else if( cli_strcmp(z,"-bail")==0 ){
3331732868
/* No-op. The bail_on_error flag should already be set. */
3331832869
}else if( cli_strcmp(z,"-version")==0 ){
33319
- sputf(stdout, "%s %s (%d-bit)\n",
32870
+ sqlite3_fprintf(stdout, "%s %s (%d-bit)\n",
3332032871
sqlite3_libversion(), sqlite3_sourceid(), 8*(int)sizeof(char*));
3332132872
return 0;
3332232873
}else if( cli_strcmp(z,"-interactive")==0 ){
3332332874
/* Need to check for interactive override here to so that it can
3332432875
** affect console setup (for Windows only) and testing thereof.
@@ -33377,18 +32928,18 @@
3337732928
rc = shell_exec(&data, z, &zErrMsg);
3337832929
if( zErrMsg!=0 ){
3337932930
shellEmitError(zErrMsg);
3338032931
if( bail_on_error ) return rc!=0 ? rc : 1;
3338132932
}else if( rc!=0 ){
33382
- eputf("Error: unable to process SQL \"%s\"\n", z);
32933
+ sqlite3_fprintf(stderr,"Error: unable to process SQL \"%s\"\n", z);
3338332934
if( bail_on_error ) return rc;
3338432935
}
3338532936
}
3338632937
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
3338732938
}else if( cli_strncmp(z, "-A", 2)==0 ){
3338832939
if( nCmd>0 ){
33389
- eputf("Error: cannot mix regular SQL or dot-commands"
32940
+ sqlite3_fprintf(stderr,"Error: cannot mix regular SQL or dot-commands"
3339032941
" with \"%s\"\n", z);
3339132942
return 1;
3339232943
}
3339332944
open_db(&data, OPEN_DB_ZIPFILE);
3339432945
if( z[2] ){
@@ -33403,11 +32954,11 @@
3340332954
}else if( cli_strcmp(z,"-safe")==0 ){
3340432955
data.bSafeMode = data.bSafeModePersist = 1;
3340532956
}else if( cli_strcmp(z,"-unsafe-testing")==0 ){
3340632957
/* Acted upon in first pass. */
3340732958
}else{
33408
- eputf("%s: Error: unknown option: %s\n", Argv0, z);
32959
+ sqlite3_fprintf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
3340932960
eputz("Use -help for a list of options.\n");
3341032961
return 1;
3341132962
}
3341232963
data.cMode = data.mode;
3341332964
}
@@ -33430,11 +32981,12 @@
3343032981
rc = shell_exec(&data, azCmd[i], &zErrMsg);
3343132982
if( zErrMsg || rc ){
3343232983
if( zErrMsg!=0 ){
3343332984
shellEmitError(zErrMsg);
3343432985
}else{
33435
- eputf("Error: unable to process SQL: %s\n", azCmd[i]);
32986
+ sqlite3_fprintf(stderr,
32987
+ "Error: unable to process SQL: %s\n", azCmd[i]);
3343632988
}
3343732989
sqlite3_free(zErrMsg);
3343832990
if( rc==0 ) rc = 1;
3343932991
goto shell_main_exit;
3344032992
}
@@ -33450,11 +33002,12 @@
3345033002
#if CIO_WIN_WC_XLATE
3345133003
# define SHELL_CIO_CHAR_SET (stdout_is_console? " (UTF-16 console I/O)" : "")
3345233004
#else
3345333005
# define SHELL_CIO_CHAR_SET ""
3345433006
#endif
33455
- sputf(stdout, "SQLite version %s %.19s%s\n" /*extra-version-info*/
33007
+ sqlite3_fprintf(stdout,
33008
+ "SQLite version %s %.19s%s\n" /*extra-version-info*/
3345633009
"Enter \".help\" for usage hints.\n",
3345733010
sqlite3_libversion(), sqlite3_sourceid(), SHELL_CIO_CHAR_SET);
3345833011
if( warnInmemoryDb ){
3345933012
sputz(stdout, "Connected to a ");
3346033013
printBold("transient in-memory database");
@@ -33526,11 +33079,11 @@
3352633079
if( bEnableVfstrace ){
3352733080
vfstrace_unregister("trace");
3352833081
}
3352933082
#ifdef SQLITE_DEBUG
3353033083
if( sqlite3_memory_used()>mem_main_enter ){
33531
- eputf("Memory leaked: %u bytes\n",
33084
+ sqlite3_fprintf(stderr,"Memory leaked: %u bytes\n",
3353233085
(unsigned int)(sqlite3_memory_used()-mem_main_enter));
3353333086
}
3353433087
#endif
3353533088
#else /* SQLITE_SHELL_FIDDLE... */
3353633089
shell_main_exit:
@@ -33566,11 +33119,11 @@
3356633119
return pVfs;
3356733120
}
3356833121
3356933122
/* Only for emcc experimentation purposes. */
3357033123
sqlite3 * fiddle_db_arg(sqlite3 *arg){
33571
- oputf("fiddle_db_arg(%p)\n", (const void*)arg);
33124
+ sqlite3_fprintf(stdout, "fiddle_db_arg(%p)\n", (const void*)arg);
3357233125
return arg;
3357333126
}
3357433127
3357533128
/*
3357633129
** Intended to be called via a SharedWorker() while a separate
@@ -33603,11 +33156,11 @@
3360333156
while( sqlite3_txn_state(globalDb,0)>0 ){
3360433157
/*
3360533158
** Resolve problem reported in
3360633159
** https://sqlite.org/forum/forumpost/0b41a25d65
3360733160
*/
33608
- oputz("Rolling back in-progress transaction.\n");
33161
+ sqlite3_fputs("Rolling back in-progress transaction.\n", stdout);
3360933162
sqlite3_exec(globalDb,"ROLLBACK", 0, 0, 0);
3361033163
}
3361133164
rc = sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
3361233165
if( 0==rc ) sqlite3_exec(globalDb, "VACUUM", 0, 0, 0);
3361333166
sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
3361433167
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -208,12 +208,10 @@
208 # define unlink _unlink
209 # endif
210 # ifndef strdup
211 # define strdup _strdup
212 # endif
213 # undef popen
214 # define popen _popen
215 # undef pclose
216 # define pclose _pclose
217 # endif
218 #else
219 /* Make sure isatty() has a prototype. */
@@ -253,10 +251,283 @@
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. */
@@ -264,1125 +535,13 @@
264 # define SQLITE_CIO_NO_CLASSIFY
265 # define SQLITE_CIO_NO_TRANSLATE
266 # define SQLITE_CIO_NO_SETMODE
267 # define SQLITE_CIO_NO_FLUSH
268 #endif
269 /************************* Begin ../ext/consio/console_io.h ******************/
270 /*
271 ** 2023 November 1
272 **
273 ** The author disclaims copyright to this source code. In place of
274 ** a legal notice, here is a blessing:
275 **
276 ** May you do good and not evil.
277 ** May you find forgiveness for yourself and forgive others.
278 ** May you share freely, never taking more than you give.
279 **
280 ********************************************************************************
281 ** This file exposes various interfaces used for console and other I/O
282 ** by the SQLite project command-line tools. These interfaces are used
283 ** at either source conglomeration time, compilation time, or run time.
284 ** This source provides for either inclusion into conglomerated,
285 ** "single-source" forms or separate compilation then linking.
286 **
287 ** Platform dependencies are "hidden" here by various stratagems so
288 ** that, provided certain conditions are met, the programs using this
289 ** source or object code compiled from it need no explicit conditional
290 ** compilation in their source for their console and stream I/O.
291 **
292 ** The symbols and functionality exposed here are not a public API.
293 ** This code may change in tandem with other project code as needed.
294 **
295 ** When this .h file and its companion .c are directly incorporated into
296 ** a source conglomeration (such as shell.c), the preprocessor symbol
297 ** CIO_WIN_WC_XLATE is defined as 0 or 1, reflecting whether console I/O
298 ** translation for Windows is effected for the build.
299 */
300 #define HAVE_CONSOLE_IO_H 1
301 #ifndef SQLITE_INTERNAL_LINKAGE
302 # define SQLITE_INTERNAL_LINKAGE extern /* external to translation unit */
303 # include <stdio.h>
304 #else
305 # define SHELL_NO_SYSINC /* Better yet, modify mkshellc.tcl for this. */
306 #endif
307
308 #ifndef SQLITE3_H
309 /* # include "sqlite3.h" */
310 #endif
311
312 #ifndef SQLITE_CIO_NO_CLASSIFY
313
314 /* Define enum for use with following function. */
315 typedef enum StreamsAreConsole {
316 SAC_NoConsole = 0,
317 SAC_InConsole = 1, SAC_OutConsole = 2, SAC_ErrConsole = 4,
318 SAC_AnyConsole = 0x7
319 } StreamsAreConsole;
320
321 /*
322 ** Classify the three standard I/O streams according to whether
323 ** they are connected to a console attached to the process.
324 **
325 ** Returns the bit-wise OR of SAC_{In,Out,Err}Console values,
326 ** or SAC_NoConsole if none of the streams reaches a console.
327 **
328 ** This function should be called before any I/O is done with
329 ** the given streams. As a side-effect, the given inputs are
330 ** recorded so that later I/O operations on them may be done
331 ** differently than the C library FILE* I/O would be done,
332 ** iff the stream is used for the I/O functions that follow,
333 ** and to support the ones that use an implicit stream.
334 **
335 ** On some platforms, stream or console mode alteration (aka
336 ** "Setup") may be made which is undone by consoleRestore().
337 */
338 SQLITE_INTERNAL_LINKAGE StreamsAreConsole
339 consoleClassifySetup( FILE *pfIn, FILE *pfOut, FILE *pfErr );
340 /* A usual call for convenience: */
341 #define SQLITE_STD_CONSOLE_INIT() consoleClassifySetup(stdin,stdout,stderr)
342
343 /*
344 ** After an initial call to consoleClassifySetup(...), renew
345 ** the same setup it effected. (A call not after is an error.)
346 ** This will restore state altered by consoleRestore();
347 **
348 ** Applications which run an inferior (child) process which
349 ** inherits the same I/O streams may call this function after
350 ** such a process exits to guard against console mode changes.
351 */
352 SQLITE_INTERNAL_LINKAGE void consoleRenewSetup(void);
353
354 /*
355 ** Undo any side-effects left by consoleClassifySetup(...).
356 **
357 ** This should be called after consoleClassifySetup() and
358 ** before the process terminates normally. It is suitable
359 ** for use with the atexit() C library procedure. After
360 ** this call, no console I/O should be done until one of
361 ** console{Classify or Renew}Setup(...) is called again.
362 **
363 ** Applications which run an inferior (child) process that
364 ** inherits the same I/O streams might call this procedure
365 ** before so that said process will have a console setup
366 ** however users have configured it or come to expect.
367 */
368 SQLITE_INTERNAL_LINKAGE void SQLITE_CDECL consoleRestore( void );
369
370 #else /* defined(SQLITE_CIO_NO_CLASSIFY) */
371 # define consoleClassifySetup(i,o,e)
372 # define consoleRenewSetup()
373 # define consoleRestore()
374 #endif /* defined(SQLITE_CIO_NO_CLASSIFY) */
375
376 #ifndef SQLITE_CIO_NO_REDIRECT
377 /*
378 ** Set stream to be used for the functions below which write
379 ** to "the designated X stream", where X is Output or Error.
380 ** Returns the previous value.
381 **
382 ** Alternatively, pass the special value, invalidFileStream,
383 ** to get the designated stream value without setting it.
384 **
385 ** Before the designated streams are set, they default to
386 ** those passed to consoleClassifySetup(...), and before
387 ** that is called they default to stdout and stderr.
388 **
389 ** It is error to close a stream so designated, then, without
390 ** designating another, use the corresponding {o,e}Emit(...).
391 */
392 SQLITE_INTERNAL_LINKAGE FILE *invalidFileStream;
393 SQLITE_INTERNAL_LINKAGE FILE *setOutputStream(FILE *pf);
394 # ifdef CONSIO_SET_ERROR_STREAM
395 SQLITE_INTERNAL_LINKAGE FILE *setErrorStream(FILE *pf);
396 # endif
397 #else
398 # define setOutputStream(pf)
399 # define setErrorStream(pf)
400 #endif /* !defined(SQLITE_CIO_NO_REDIRECT) */
401
402 #ifndef SQLITE_CIO_NO_TRANSLATE
403 /*
404 ** Emit output like fprintf(). If the output is going to the
405 ** console and translation from UTF-8 is necessary, perform
406 ** the needed translation. Otherwise, write formatted output
407 ** to the provided stream almost as-is, possibly with newline
408 ** translation as specified by set{Binary,Text}Mode().
409 */
410 SQLITE_INTERNAL_LINKAGE int fPrintfUtf8(FILE *pfO, const char *zFormat, ...);
411 /* Like fPrintfUtf8 except stream is always the designated output. */
412 SQLITE_INTERNAL_LINKAGE int oPrintfUtf8(const char *zFormat, ...);
413 /* Like fPrintfUtf8 except stream is always the designated error. */
414 SQLITE_INTERNAL_LINKAGE int ePrintfUtf8(const char *zFormat, ...);
415
416 /*
417 ** Emit output like fputs(). If the output is going to the
418 ** console and translation from UTF-8 is necessary, perform
419 ** the needed translation. Otherwise, write given text to the
420 ** provided stream almost as-is, possibly with newline
421 ** translation as specified by set{Binary,Text}Mode().
422 */
423 SQLITE_INTERNAL_LINKAGE int fPutsUtf8(const char *z, FILE *pfO);
424 /* Like fPutsUtf8 except stream is always the designated output. */
425 SQLITE_INTERNAL_LINKAGE int oPutsUtf8(const char *z);
426 /* Like fPutsUtf8 except stream is always the designated error. */
427 SQLITE_INTERNAL_LINKAGE int ePutsUtf8(const char *z);
428
429 /*
430 ** Emit output like fPutsUtf8(), except that the length of the
431 ** accepted char or character sequence is limited by nAccept.
432 **
433 ** Returns the number of accepted char values.
434 */
435 #ifdef CONSIO_SPUTB
436 SQLITE_INTERNAL_LINKAGE int
437 fPutbUtf8(FILE *pfOut, const char *cBuf, int nAccept);
438 /* Like fPutbUtf8 except stream is always the designated output. */
439 #endif
440 SQLITE_INTERNAL_LINKAGE int
441 oPutbUtf8(const char *cBuf, int nAccept);
442 /* Like fPutbUtf8 except stream is always the designated error. */
443 #ifdef CONSIO_EPUTB
444 SQLITE_INTERNAL_LINKAGE int
445 ePutbUtf8(const char *cBuf, int nAccept);
446 #endif
447
448 /*
449 ** Flush the given output stream. Return non-zero for success, else 0.
450 */
451 #if !defined(SQLITE_CIO_NO_FLUSH) && !defined(SQLITE_CIO_NO_SETMODE)
452 SQLITE_INTERNAL_LINKAGE int
453 fFlushBuffer(FILE *pfOut);
454 #endif
455
456 /*
457 ** Collect input like fgets(...) with special provisions for input
458 ** from the console on such platforms as require same. Newline
459 ** translation may be done as set by set{Binary,Text}Mode().
460 ** As a convenience, pfIn==NULL is treated as stdin.
461 */
462 SQLITE_INTERNAL_LINKAGE char* fGetsUtf8(char *cBuf, int ncMax, FILE *pfIn);
463 /* Like fGetsUtf8 except stream is always the designated input. */
464 /* SQLITE_INTERNAL_LINKAGE char* iGetsUtf8(char *cBuf, int ncMax); */
465
466 #endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
467
468 #ifndef SQLITE_CIO_NO_SETMODE
469 /*
470 ** Set given stream for binary mode, where newline translation is
471 ** not done, or for text mode where, for some platforms, newlines
472 ** are translated to the platform's conventional char sequence.
473 ** If bFlush true, flush the stream.
474 **
475 ** An additional side-effect is that if the stream is one passed
476 ** to consoleClassifySetup() as an output, it is flushed first.
477 **
478 ** Note that binary/text mode has no effect on console I/O
479 ** translation. On all platforms, newline to the console starts
480 ** a new line and CR,LF chars from the console become a newline.
481 */
482 SQLITE_INTERNAL_LINKAGE void setBinaryMode(FILE *, short bFlush);
483 SQLITE_INTERNAL_LINKAGE void setTextMode(FILE *, short bFlush);
484 #endif
485
486 #ifdef SQLITE_CIO_PROMPTED_IN
487 typedef struct Prompts {
488 int numPrompts;
489 const char **azPrompts;
490 } Prompts;
491
492 /*
493 ** Macros for use of a line editor.
494 **
495 ** The following macros define operations involving use of a
496 ** line-editing library or simple console interaction.
497 ** A "T" argument is a text (char *) buffer or filename.
498 ** A "N" argument is an integer.
499 **
500 ** SHELL_ADD_HISTORY(T) // Record text as line(s) of history.
501 ** SHELL_READ_HISTORY(T) // Read history from file named by T.
502 ** SHELL_WRITE_HISTORY(T) // Write history to file named by T.
503 ** SHELL_STIFLE_HISTORY(N) // Limit history to N entries.
504 **
505 ** A console program which does interactive console input is
506 ** expected to call:
507 ** SHELL_READ_HISTORY(T) before collecting such input;
508 ** SHELL_ADD_HISTORY(T) as record-worthy input is taken;
509 ** SHELL_STIFLE_HISTORY(N) after console input ceases; then
510 ** SHELL_WRITE_HISTORY(T) before the program exits.
511 */
512
513 /*
514 ** Retrieve a single line of input text from an input stream.
515 **
516 ** If pfIn is the input stream passed to consoleClassifySetup(),
517 ** and azPrompt is not NULL, then a prompt is issued before the
518 ** line is collected, as selected by the isContinuation flag.
519 ** Array azPrompt[{0,1}] holds the {main,continuation} prompt.
520 **
521 ** If zBufPrior is not NULL then it is a buffer from a prior
522 ** call to this routine that can be reused, or will be freed.
523 **
524 ** The result is stored in space obtained from malloc() and
525 ** must either be freed by the caller or else passed back to
526 ** this function as zBufPrior for reuse.
527 **
528 ** This function may call upon services of a line-editing
529 ** library to interactively collect line edited input.
530 */
531 SQLITE_INTERNAL_LINKAGE char *
532 shellGetLine(FILE *pfIn, char *zBufPrior, int nLen,
533 short isContinuation, Prompts azPrompt);
534 #endif /* defined(SQLITE_CIO_PROMPTED_IN) */
535 /*
536 ** TBD: Define an interface for application(s) to generate
537 ** completion candidates for use by the line-editor.
538 **
539 ** This may be premature; the CLI is the only application
540 ** that does this. Yet, getting line-editing melded into
541 ** console I/O is desirable because a line-editing library
542 ** may have to establish console operating mode, possibly
543 ** in a way that interferes with the above functionality.
544 */
545
546 #if !(defined(SQLITE_CIO_NO_UTF8SCAN)&&defined(SQLITE_CIO_NO_TRANSLATE))
547 /* Skip over as much z[] input char sequence as is valid UTF-8,
548 ** limited per nAccept char's or whole characters and containing
549 ** no char cn such that ((1<<cn) & ccm)!=0. On return, the
550 ** sequence z:return (inclusive:exclusive) is validated UTF-8.
551 ** Limit: nAccept>=0 => char count, nAccept<0 => character
552 */
553 SQLITE_INTERNAL_LINKAGE const char*
554 zSkipValidUtf8(const char *z, int nAccept, long ccm);
555
556 #endif
557
558 /************************* End ../ext/consio/console_io.h ********************/
559 /************************* Begin ../ext/consio/console_io.c ******************/
560 /*
561 ** 2023 November 4
562 **
563 ** The author disclaims copyright to this source code. In place of
564 ** a legal notice, here is a blessing:
565 **
566 ** May you do good and not evil.
567 ** May you find forgiveness for yourself and forgive others.
568 ** May you share freely, never taking more than you give.
569 **
570 ********************************************************************************
571 ** This file implements various interfaces used for console and stream I/O
572 ** by the SQLite project command-line tools, as explained in console_io.h .
573 ** Functions prefixed by "SQLITE_INTERNAL_LINKAGE" behave as described there.
574 */
575
576 #ifndef SQLITE_CDECL
577 # define SQLITE_CDECL
578 #endif
579
580 #ifndef SHELL_NO_SYSINC
581 # include <stdarg.h>
582 # include <string.h>
583 # include <stdlib.h>
584 # include <limits.h>
585 # include <assert.h>
586 /* # include "sqlite3.h" */
587 #endif
588 #ifndef HAVE_CONSOLE_IO_H
589 # include "console_io.h"
590 #endif
591 #if defined(_MSC_VER)
592 # pragma warning(disable : 4204)
593 #endif
594
595 #ifndef SQLITE_CIO_NO_TRANSLATE
596 # if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
597 # ifndef SHELL_NO_SYSINC
598 # include <io.h>
599 # include <fcntl.h>
600 # undef WIN32_LEAN_AND_MEAN
601 # define WIN32_LEAN_AND_MEAN
602 # include <windows.h>
603 # endif
604 # define CIO_WIN_WC_XLATE 1 /* Use WCHAR Windows APIs for console I/O */
605 # else
606 # ifndef SHELL_NO_SYSINC
607 # include <unistd.h>
608 # endif
609 # define CIO_WIN_WC_XLATE 0 /* Use plain C library stream I/O at console */
610 # endif
611 #else
612 # define CIO_WIN_WC_XLATE 0 /* Not exposing translation routines at all */
613 #endif
614
615 #if CIO_WIN_WC_XLATE
616 static HANDLE handleOfFile(FILE *pf){
617 int fileDesc = _fileno(pf);
618 union { intptr_t osfh; HANDLE fh; } fid = {
619 (fileDesc>=0)? _get_osfhandle(fileDesc) : (intptr_t)INVALID_HANDLE_VALUE
620 };
621 return fid.fh;
622 }
623 #endif
624
625 #ifndef SQLITE_CIO_NO_TRANSLATE
626 typedef struct PerStreamTags {
627 # if CIO_WIN_WC_XLATE
628 HANDLE hx;
629 DWORD consMode;
630 char acIncomplete[4];
631 # else
632 short reachesConsole;
633 # endif
634 FILE *pf;
635 } PerStreamTags;
636
637 /* Define NULL-like value for things which can validly be 0. */
638 # define SHELL_INVALID_FILE_PTR ((FILE *)~0)
639 # if CIO_WIN_WC_XLATE
640 # define SHELL_INVALID_CONS_MODE 0xFFFF0000
641 # endif
642
643 # if CIO_WIN_WC_XLATE
644 # define PST_INITIALIZER { INVALID_HANDLE_VALUE, SHELL_INVALID_CONS_MODE, \
645 {0,0,0,0}, SHELL_INVALID_FILE_PTR }
646 # else
647 # define PST_INITIALIZER { 0, SHELL_INVALID_FILE_PTR }
648 # endif
649
650 /* Quickly say whether a known output is going to the console. */
651 # if CIO_WIN_WC_XLATE
652 static short pstReachesConsole(PerStreamTags *ppst){
653 return (ppst->hx != INVALID_HANDLE_VALUE);
654 }
655 # else
656 # define pstReachesConsole(ppst) 0
657 # endif
658
659 # if CIO_WIN_WC_XLATE
660 static void restoreConsoleArb(PerStreamTags *ppst){
661 if( pstReachesConsole(ppst) ) SetConsoleMode(ppst->hx, ppst->consMode);
662 }
663 # else
664 # define restoreConsoleArb(ppst)
665 # endif
666
667 /* Say whether FILE* appears to be a console, collect associated info. */
668 static short streamOfConsole(FILE *pf, /* out */ PerStreamTags *ppst){
669 # if CIO_WIN_WC_XLATE
670 short rv = 0;
671 DWORD dwCM = SHELL_INVALID_CONS_MODE;
672 HANDLE fh = handleOfFile(pf);
673 ppst->pf = pf;
674 if( INVALID_HANDLE_VALUE != fh ){
675 rv = (GetFileType(fh) == FILE_TYPE_CHAR && GetConsoleMode(fh,&dwCM));
676 }
677 ppst->hx = (rv)? fh : INVALID_HANDLE_VALUE;
678 ppst->consMode = dwCM;
679 return rv;
680 # else
681 ppst->pf = pf;
682 ppst->reachesConsole = ( (short)isatty(fileno(pf)) );
683 return ppst->reachesConsole;
684 # endif
685 }
686
687 # ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
688 # define ENABLE_VIRTUAL_TERMINAL_PROCESSING (0x4)
689 # endif
690
691 # if CIO_WIN_WC_XLATE
692 /* Define console modes for use with the Windows Console API. */
693 # define SHELL_CONI_MODE \
694 (ENABLE_ECHO_INPUT | ENABLE_INSERT_MODE | ENABLE_LINE_INPUT | 0x80 \
695 | ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS | ENABLE_PROCESSED_INPUT)
696 # define SHELL_CONO_MODE (ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT \
697 | ENABLE_VIRTUAL_TERMINAL_PROCESSING)
698 # endif
699
700 typedef struct ConsoleInfo {
701 PerStreamTags pstSetup[3];
702 PerStreamTags pstDesignated[3];
703 StreamsAreConsole sacSetup;
704 } ConsoleInfo;
705
706 static short isValidStreamInfo(PerStreamTags *ppst){
707 return (ppst->pf != SHELL_INVALID_FILE_PTR);
708 }
709
710 static ConsoleInfo consoleInfo = {
711 { /* pstSetup */ PST_INITIALIZER, PST_INITIALIZER, PST_INITIALIZER },
712 { /* pstDesignated[] */ PST_INITIALIZER, PST_INITIALIZER, PST_INITIALIZER },
713 SAC_NoConsole /* sacSetup */
714 };
715
716 SQLITE_INTERNAL_LINKAGE FILE* invalidFileStream = (FILE *)~0;
717
718 # if CIO_WIN_WC_XLATE
719 static void maybeSetupAsConsole(PerStreamTags *ppst, short odir){
720 if( pstReachesConsole(ppst) ){
721 DWORD cm = odir? SHELL_CONO_MODE : SHELL_CONI_MODE;
722 SetConsoleMode(ppst->hx, cm);
723 }
724 }
725 # else
726 # define maybeSetupAsConsole(ppst,odir)
727 # endif
728
729 SQLITE_INTERNAL_LINKAGE void consoleRenewSetup(void){
730 # if CIO_WIN_WC_XLATE
731 int ix = 0;
732 while( ix < 6 ){
733 PerStreamTags *ppst = (ix<3)?
734 &consoleInfo.pstSetup[ix] : &consoleInfo.pstDesignated[ix-3];
735 maybeSetupAsConsole(ppst, (ix % 3)>0);
736 ++ix;
737 }
738 # endif
739 }
740
741 SQLITE_INTERNAL_LINKAGE StreamsAreConsole
742 consoleClassifySetup( FILE *pfIn, FILE *pfOut, FILE *pfErr ){
743 StreamsAreConsole rv = SAC_NoConsole;
744 FILE* apf[3] = { pfIn, pfOut, pfErr };
745 int ix;
746 for( ix = 2; ix >= 0; --ix ){
747 PerStreamTags *ppst = &consoleInfo.pstSetup[ix];
748 if( streamOfConsole(apf[ix], ppst) ){
749 rv |= (SAC_InConsole<<ix);
750 }
751 consoleInfo.pstDesignated[ix] = *ppst;
752 if( ix > 0 ) fflush(apf[ix]);
753 }
754 consoleInfo.sacSetup = rv;
755 consoleRenewSetup();
756 return rv;
757 }
758
759 SQLITE_INTERNAL_LINKAGE void SQLITE_CDECL consoleRestore( void ){
760 # if CIO_WIN_WC_XLATE
761 static ConsoleInfo *pci = &consoleInfo;
762 if( pci->sacSetup ){
763 int ix;
764 for( ix=0; ix<3; ++ix ){
765 if( pci->sacSetup & (SAC_InConsole<<ix) ){
766 PerStreamTags *ppst = &pci->pstSetup[ix];
767 SetConsoleMode(ppst->hx, ppst->consMode);
768 }
769 }
770 }
771 # endif
772 }
773 #endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
774
775 #ifdef SQLITE_CIO_INPUT_REDIR
776 /* Say whether given FILE* is among those known, via either
777 ** consoleClassifySetup() or set{Output,Error}Stream, as
778 ** readable, and return an associated PerStreamTags pointer
779 ** if so. Otherwise, return 0.
780 */
781 static PerStreamTags * isKnownReadable(FILE *pf){
782 static PerStreamTags *apst[] = {
783 &consoleInfo.pstDesignated[0], &consoleInfo.pstSetup[0], 0
784 };
785 int ix = 0;
786 do {
787 if( apst[ix]->pf == pf ) break;
788 } while( apst[++ix] != 0 );
789 return apst[ix];
790 }
791 #endif
792
793 #ifndef SQLITE_CIO_NO_TRANSLATE
794 /* Say whether given FILE* is among those known, via either
795 ** consoleClassifySetup() or set{Output,Error}Stream, as
796 ** writable, and return an associated PerStreamTags pointer
797 ** if so. Otherwise, return 0.
798 */
799 static PerStreamTags * isKnownWritable(FILE *pf){
800 static PerStreamTags *apst[] = {
801 &consoleInfo.pstDesignated[1], &consoleInfo.pstDesignated[2],
802 &consoleInfo.pstSetup[1], &consoleInfo.pstSetup[2], 0
803 };
804 int ix = 0;
805 do {
806 if( apst[ix]->pf == pf ) break;
807 } while( apst[++ix] != 0 );
808 return apst[ix];
809 }
810
811 static FILE *designateEmitStream(FILE *pf, unsigned chix){
812 FILE *rv = consoleInfo.pstDesignated[chix].pf;
813 if( pf == invalidFileStream ) return rv;
814 else{
815 /* Setting a possibly new output stream. */
816 PerStreamTags *ppst = isKnownWritable(pf);
817 if( ppst != 0 ){
818 PerStreamTags pst = *ppst;
819 consoleInfo.pstDesignated[chix] = pst;
820 }else streamOfConsole(pf, &consoleInfo.pstDesignated[chix]);
821 }
822 return rv;
823 }
824
825 SQLITE_INTERNAL_LINKAGE FILE *setOutputStream(FILE *pf){
826 return designateEmitStream(pf, 1);
827 }
828 # ifdef CONSIO_SET_ERROR_STREAM
829 SQLITE_INTERNAL_LINKAGE FILE *setErrorStream(FILE *pf){
830 return designateEmitStream(pf, 2);
831 }
832 # endif
833 #endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
834
835 #ifndef SQLITE_CIO_NO_SETMODE
836 # if CIO_WIN_WC_XLATE
837 static void setModeFlushQ(FILE *pf, short bFlush, int mode){
838 if( bFlush ) fflush(pf);
839 _setmode(_fileno(pf), mode);
840 }
841 # else
842 # define setModeFlushQ(f, b, m) if(b) fflush(f)
843 # endif
844
845 SQLITE_INTERNAL_LINKAGE void setBinaryMode(FILE *pf, short bFlush){
846 setModeFlushQ(pf, bFlush, _O_BINARY);
847 }
848 SQLITE_INTERNAL_LINKAGE void setTextMode(FILE *pf, short bFlush){
849 setModeFlushQ(pf, bFlush, _O_TEXT);
850 }
851 # undef setModeFlushQ
852
853 #else /* defined(SQLITE_CIO_NO_SETMODE) */
854 # define setBinaryMode(f, bFlush) do{ if((bFlush)) fflush(f); }while(0)
855 # define setTextMode(f, bFlush) do{ if((bFlush)) fflush(f); }while(0)
856 #endif /* defined(SQLITE_CIO_NO_SETMODE) */
857
858 #ifndef SQLITE_CIO_NO_TRANSLATE
859 # if CIO_WIN_WC_XLATE
860 /* Write buffer cBuf as output to stream known to reach console,
861 ** limited to ncTake char's. Return ncTake on success, else 0. */
862 static int conZstrEmit(PerStreamTags *ppst, const char *z, int ncTake){
863 int rv = 0;
864 if( z!=NULL ){
865 int nwc = MultiByteToWideChar(CP_UTF8,0, z,ncTake, 0,0);
866 if( nwc > 0 ){
867 WCHAR *zw = sqlite3_malloc64(nwc*sizeof(WCHAR));
868 if( zw!=NULL ){
869 nwc = MultiByteToWideChar(CP_UTF8,0, z,ncTake, zw,nwc);
870 if( nwc > 0 ){
871 /* Translation from UTF-8 to UTF-16, then WCHARs out. */
872 if( WriteConsoleW(ppst->hx, zw,nwc, 0, NULL) ){
873 rv = ncTake;
874 }
875 }
876 sqlite3_free(zw);
877 }
878 }
879 }
880 return rv;
881 }
882
883 /* For {f,o,e}PrintfUtf8() when stream is known to reach console. */
884 static int conioVmPrintf(PerStreamTags *ppst, const char *zFormat, va_list ap){
885 char *z = sqlite3_vmprintf(zFormat, ap);
886 if( z ){
887 int rv = conZstrEmit(ppst, z, (int)strlen(z));
888 sqlite3_free(z);
889 return rv;
890 }else return 0;
891 }
892 # endif /* CIO_WIN_WC_XLATE */
893
894 # ifdef CONSIO_GET_EMIT_STREAM
895 static PerStreamTags * getDesignatedEmitStream(FILE *pf, unsigned chix,
896 PerStreamTags *ppst){
897 PerStreamTags *rv = isKnownWritable(pf);
898 short isValid = (rv!=0)? isValidStreamInfo(rv) : 0;
899 if( rv != 0 && isValid ) return rv;
900 streamOfConsole(pf, ppst);
901 return ppst;
902 }
903 # endif
904
905 /* Get stream info, either for designated output or error stream when
906 ** chix equals 1 or 2, or for an arbitrary stream when chix == 0.
907 ** In either case, ppst references a caller-owned PerStreamTags
908 ** struct which may be filled in if none of the known writable
909 ** streams is being held by consoleInfo. The ppf parameter is a
910 ** byref output when chix!=0 and a byref input when chix==0.
911 */
912 static PerStreamTags *
913 getEmitStreamInfo(unsigned chix, PerStreamTags *ppst,
914 /* in/out */ FILE **ppf){
915 PerStreamTags *ppstTry;
916 FILE *pfEmit;
917 if( chix > 0 ){
918 ppstTry = &consoleInfo.pstDesignated[chix];
919 if( !isValidStreamInfo(ppstTry) ){
920 ppstTry = &consoleInfo.pstSetup[chix];
921 pfEmit = ppst->pf;
922 }else pfEmit = ppstTry->pf;
923 if( !isValidStreamInfo(ppstTry) ){
924 pfEmit = (chix > 1)? stderr : stdout;
925 ppstTry = ppst;
926 streamOfConsole(pfEmit, ppstTry);
927 }
928 *ppf = pfEmit;
929 }else{
930 ppstTry = isKnownWritable(*ppf);
931 if( ppstTry != 0 ) return ppstTry;
932 streamOfConsole(*ppf, ppst);
933 return ppst;
934 }
935 return ppstTry;
936 }
937
938 SQLITE_INTERNAL_LINKAGE int oPrintfUtf8(const char *zFormat, ...){
939 va_list ap;
940 int rv;
941 FILE *pfOut;
942 PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
943 # if CIO_WIN_WC_XLATE
944 PerStreamTags *ppst = getEmitStreamInfo(1, &pst, &pfOut);
945 # else
946 getEmitStreamInfo(1, &pst, &pfOut);
947 # endif
948 assert(zFormat!=0);
949 va_start(ap, zFormat);
950 # if CIO_WIN_WC_XLATE
951 if( pstReachesConsole(ppst) ){
952 rv = conioVmPrintf(ppst, zFormat, ap);
953 }else{
954 # endif
955 rv = vfprintf(pfOut, zFormat, ap);
956 # if CIO_WIN_WC_XLATE
957 }
958 # endif
959 va_end(ap);
960 return rv;
961 }
962
963 SQLITE_INTERNAL_LINKAGE int ePrintfUtf8(const char *zFormat, ...){
964 va_list ap;
965 int rv;
966 FILE *pfErr;
967 PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
968 # if CIO_WIN_WC_XLATE
969 PerStreamTags *ppst = getEmitStreamInfo(2, &pst, &pfErr);
970 # else
971 getEmitStreamInfo(2, &pst, &pfErr);
972 # endif
973 assert(zFormat!=0);
974 va_start(ap, zFormat);
975 # if CIO_WIN_WC_XLATE
976 if( pstReachesConsole(ppst) ){
977 rv = conioVmPrintf(ppst, zFormat, ap);
978 }else{
979 # endif
980 rv = vfprintf(pfErr, zFormat, ap);
981 # if CIO_WIN_WC_XLATE
982 }
983 # endif
984 va_end(ap);
985 return rv;
986 }
987
988 SQLITE_INTERNAL_LINKAGE int fPrintfUtf8(FILE *pfO, const char *zFormat, ...){
989 va_list ap;
990 int rv;
991 PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
992 # if CIO_WIN_WC_XLATE
993 PerStreamTags *ppst = getEmitStreamInfo(0, &pst, &pfO);
994 # else
995 getEmitStreamInfo(0, &pst, &pfO);
996 # endif
997 assert(zFormat!=0);
998 va_start(ap, zFormat);
999 # if CIO_WIN_WC_XLATE
1000 if( pstReachesConsole(ppst) ){
1001 maybeSetupAsConsole(ppst, 1);
1002 rv = conioVmPrintf(ppst, zFormat, ap);
1003 if( 0 == isKnownWritable(ppst->pf) ) restoreConsoleArb(ppst);
1004 }else{
1005 # endif
1006 rv = vfprintf(pfO, zFormat, ap);
1007 # if CIO_WIN_WC_XLATE
1008 }
1009 # endif
1010 va_end(ap);
1011 return rv;
1012 }
1013
1014 SQLITE_INTERNAL_LINKAGE int fPutsUtf8(const char *z, FILE *pfO){
1015 PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1016 # if CIO_WIN_WC_XLATE
1017 PerStreamTags *ppst = getEmitStreamInfo(0, &pst, &pfO);
1018 # else
1019 getEmitStreamInfo(0, &pst, &pfO);
1020 # endif
1021 assert(z!=0);
1022 # if CIO_WIN_WC_XLATE
1023 if( pstReachesConsole(ppst) ){
1024 int rv;
1025 maybeSetupAsConsole(ppst, 1);
1026 rv = conZstrEmit(ppst, z, (int)strlen(z));
1027 if( 0 == isKnownWritable(ppst->pf) ) restoreConsoleArb(ppst);
1028 return rv;
1029 }else {
1030 # endif
1031 return (fputs(z, pfO)<0)? 0 : (int)strlen(z);
1032 # if CIO_WIN_WC_XLATE
1033 }
1034 # endif
1035 }
1036
1037 SQLITE_INTERNAL_LINKAGE int ePutsUtf8(const char *z){
1038 FILE *pfErr;
1039 PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1040 # if CIO_WIN_WC_XLATE
1041 PerStreamTags *ppst = getEmitStreamInfo(2, &pst, &pfErr);
1042 # else
1043 getEmitStreamInfo(2, &pst, &pfErr);
1044 # endif
1045 assert(z!=0);
1046 # if CIO_WIN_WC_XLATE
1047 if( pstReachesConsole(ppst) ) return conZstrEmit(ppst, z, (int)strlen(z));
1048 else {
1049 # endif
1050 return (fputs(z, pfErr)<0)? 0 : (int)strlen(z);
1051 # if CIO_WIN_WC_XLATE
1052 }
1053 # endif
1054 }
1055
1056 SQLITE_INTERNAL_LINKAGE int oPutsUtf8(const char *z){
1057 FILE *pfOut;
1058 PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1059 # if CIO_WIN_WC_XLATE
1060 PerStreamTags *ppst = getEmitStreamInfo(1, &pst, &pfOut);
1061 # else
1062 getEmitStreamInfo(1, &pst, &pfOut);
1063 # endif
1064 assert(z!=0);
1065 # if CIO_WIN_WC_XLATE
1066 if( pstReachesConsole(ppst) ) return conZstrEmit(ppst, z, (int)strlen(z));
1067 else {
1068 # endif
1069 return (fputs(z, pfOut)<0)? 0 : (int)strlen(z);
1070 # if CIO_WIN_WC_XLATE
1071 }
1072 # endif
1073 }
1074
1075 #endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
1076
1077 #if !(defined(SQLITE_CIO_NO_UTF8SCAN) && defined(SQLITE_CIO_NO_TRANSLATE))
1078 /* Skip over as much z[] input char sequence as is valid UTF-8,
1079 ** limited per nAccept char's or whole characters and containing
1080 ** no char cn such that ((1<<cn) & ccm)!=0. On return, the
1081 ** sequence z:return (inclusive:exclusive) is validated UTF-8.
1082 ** Limit: nAccept>=0 => char count, nAccept<0 => character
1083 */
1084 SQLITE_INTERNAL_LINKAGE const char*
1085 zSkipValidUtf8(const char *z, int nAccept, long ccm){
1086 int ng = (nAccept<0)? -nAccept : 0;
1087 const char *pcLimit = (nAccept>=0)? z+nAccept : 0;
1088 assert(z!=0);
1089 while( (pcLimit)? (z<pcLimit) : (ng-- != 0) ){
1090 char c = *z;
1091 if( (c & 0x80) == 0 ){
1092 if( ccm != 0L && c < 0x20 && ((1L<<c) & ccm) != 0 ) return z;
1093 ++z; /* ASCII */
1094 }else if( (c & 0xC0) != 0xC0 ) return z; /* not a lead byte */
1095 else{
1096 const char *zt = z+1; /* Got lead byte, look at trail bytes.*/
1097 do{
1098 if( pcLimit && zt >= pcLimit ) return z;
1099 else{
1100 char ct = *zt++;
1101 if( ct==0 || (zt-z)>4 || (ct & 0xC0)!=0x80 ){
1102 /* Trailing bytes are too few, too many, or invalid. */
1103 return z;
1104 }
1105 }
1106 } while( ((c <<= 1) & 0x40) == 0x40 ); /* Eat lead byte's count. */
1107 z = zt;
1108 }
1109 }
1110 return z;
1111 }
1112 #endif /*!(defined(SQLITE_CIO_NO_UTF8SCAN)&&defined(SQLITE_CIO_NO_TRANSLATE))*/
1113
1114 #ifndef SQLITE_CIO_NO_TRANSLATE
1115 # ifdef CONSIO_SPUTB
1116 SQLITE_INTERNAL_LINKAGE int
1117 fPutbUtf8(FILE *pfO, const char *cBuf, int nAccept){
1118 assert(pfO!=0);
1119 # if CIO_WIN_WC_XLATE
1120 PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1121 PerStreamTags *ppst = getEmitStreamInfo(0, &pst, &pfO);
1122 if( pstReachesConsole(ppst) ){
1123 int rv;
1124 maybeSetupAsConsole(ppst, 1);
1125 rv = conZstrEmit(ppst, cBuf, nAccept);
1126 if( 0 == isKnownWritable(ppst->pf) ) restoreConsoleArb(ppst);
1127 return rv;
1128 }else {
1129 # endif
1130 return (int)fwrite(cBuf, 1, nAccept, pfO);
1131 # if CIO_WIN_WC_XLATE
1132 }
1133 # endif
1134 }
1135 # endif
1136
1137 SQLITE_INTERNAL_LINKAGE int
1138 oPutbUtf8(const char *cBuf, int nAccept){
1139 FILE *pfOut;
1140 PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1141 # if CIO_WIN_WC_XLATE
1142 PerStreamTags *ppst = getEmitStreamInfo(1, &pst, &pfOut);
1143 # else
1144 getEmitStreamInfo(1, &pst, &pfOut);
1145 # endif
1146 # if CIO_WIN_WC_XLATE
1147 if( pstReachesConsole(ppst) ){
1148 return conZstrEmit(ppst, cBuf, nAccept);
1149 }else {
1150 # endif
1151 return (int)fwrite(cBuf, 1, nAccept, pfOut);
1152 # if CIO_WIN_WC_XLATE
1153 }
1154 # endif
1155 }
1156
1157 /*
1158 ** Flush the given output stream. Return non-zero for success, else 0.
1159 */
1160 #if !defined(SQLITE_CIO_NO_FLUSH) && !defined(SQLITE_CIO_NO_SETMODE)
1161 SQLITE_INTERNAL_LINKAGE int
1162 fFlushBuffer(FILE *pfOut){
1163 # if CIO_WIN_WC_XLATE && !defined(SHELL_OMIT_FIO_DUPE)
1164 return FlushFileBuffers(handleOfFile(pfOut))? 1 : 0;
1165 # else
1166 return fflush(pfOut);
1167 # endif
1168 }
1169 #endif
1170
1171 #if CIO_WIN_WC_XLATE \
1172 && !defined(SHELL_OMIT_FIO_DUPE) \
1173 && defined(SQLITE_USE_ONLY_WIN32)
1174 static struct FileAltIds {
1175 int fd;
1176 HANDLE fh;
1177 } altIdsOfFile(FILE *pf){
1178 struct FileAltIds rv = { _fileno(pf) };
1179 union { intptr_t osfh; HANDLE fh; } fid = {
1180 (rv.fd>=0)? _get_osfhandle(rv.fd) : (intptr_t)INVALID_HANDLE_VALUE
1181 };
1182 rv.fh = fid.fh;
1183 return rv;
1184 }
1185
1186 SQLITE_INTERNAL_LINKAGE size_t
1187 cfWrite(const void *buf, size_t osz, size_t ocnt, FILE *pf){
1188 size_t rv = 0;
1189 struct FileAltIds fai = altIdsOfFile(pf);
1190 int fmode = _setmode(fai.fd, _O_BINARY);
1191 _setmode(fai.fd, fmode);
1192 while( rv < ocnt ){
1193 size_t nbo = osz;
1194 while( nbo > 0 ){
1195 DWORD dwno = (nbo>(1L<<24))? 1L<<24 : (DWORD)nbo;
1196 BOOL wrc = TRUE;
1197 BOOL genCR = (fmode & _O_TEXT)!=0;
1198 if( genCR ){
1199 const char *pnl = (const char*)memchr(buf, '\n', nbo);
1200 if( pnl ) nbo = pnl - (const char*)buf;
1201 else genCR = 0;
1202 }
1203 if( dwno>0 ) wrc = WriteFile(fai.fh, buf, dwno, 0,0);
1204 if( genCR && wrc ){
1205 wrc = WriteFile(fai.fh, "\r\n", 2, 0,0);
1206 ++dwno; /* Skip over the LF */
1207 }
1208 if( !wrc ) return rv;
1209 buf = (const char*)buf + dwno;
1210 nbo += dwno;
1211 }
1212 ++rv;
1213 }
1214 return rv;
1215 }
1216
1217 /* An fgets() equivalent, using Win32 file API for actual input.
1218 ** Input ends when given buffer is filled or a newline is read.
1219 ** If the FILE object is in text mode, swallows 0x0D. (ASCII CR)
1220 */
1221 SQLITE_INTERNAL_LINKAGE char *
1222 cfGets(char *cBuf, int n, FILE *pf){
1223 int nci = 0;
1224 struct FileAltIds fai = altIdsOfFile(pf);
1225 int fmode = _setmode(fai.fd, _O_BINARY);
1226 BOOL eatCR = (fmode & _O_TEXT)!=0;
1227 _setmode(fai.fd, fmode);
1228 while( nci < n-1 ){
1229 DWORD nr;
1230 if( !ReadFile(fai.fh, cBuf+nci, 1, &nr, 0) || nr==0 ) break;
1231 if( nr>0 && (!eatCR || cBuf[nci]!='\r') ){
1232 nci += nr;
1233 if( cBuf[nci-nr]=='\n' ) break;
1234 }
1235 }
1236 if( nci < n ) cBuf[nci] = 0;
1237 return (nci>0)? cBuf : 0;
1238 }
1239 # else
1240 # define cfWrite(b,os,no,f) fwrite(b,os,no,f)
1241 # define cfGets(b,n,f) fgets(b,n,f)
1242 # endif
1243
1244 # ifdef CONSIO_EPUTB
1245 SQLITE_INTERNAL_LINKAGE int
1246 ePutbUtf8(const char *cBuf, int nAccept){
1247 FILE *pfErr;
1248 PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1249 PerStreamTags *ppst = getEmitStreamInfo(2, &pst, &pfErr);
1250 # if CIO_WIN_WC_XLATE
1251 if( pstReachesConsole(ppst) ){
1252 return conZstrEmit(ppst, cBuf, nAccept);
1253 }else {
1254 # endif
1255 return (int)cfWrite(cBuf, 1, nAccept, pfErr);
1256 # if CIO_WIN_WC_XLATE
1257 }
1258 # endif
1259 }
1260 # endif /* defined(CONSIO_EPUTB) */
1261
1262 SQLITE_INTERNAL_LINKAGE char* fGetsUtf8(char *cBuf, int ncMax, FILE *pfIn){
1263 if( pfIn==0 ) pfIn = stdin;
1264 # if CIO_WIN_WC_XLATE
1265 if( pfIn == consoleInfo.pstSetup[0].pf
1266 && (consoleInfo.sacSetup & SAC_InConsole)!=0 ){
1267 # if CIO_WIN_WC_XLATE==1
1268 # define SHELL_GULP 150 /* Count of WCHARS to be gulped at a time */
1269 WCHAR wcBuf[SHELL_GULP+1];
1270 int lend = 0, noc = 0;
1271 if( ncMax > 0 ) cBuf[0] = 0;
1272 while( noc < ncMax-8-1 && !lend ){
1273 /* There is room for at least 2 more characters and a 0-terminator. */
1274 int na = (ncMax > SHELL_GULP*4+1 + noc)? SHELL_GULP : (ncMax-1 - noc)/4;
1275 # undef SHELL_GULP
1276 DWORD nbr = 0;
1277 BOOL bRC = ReadConsoleW(consoleInfo.pstSetup[0].hx, wcBuf, na, &nbr, 0);
1278 if( bRC && nbr>0 && (wcBuf[nbr-1]&0xF800)==0xD800 ){
1279 /* Last WHAR read is first of a UTF-16 surrogate pair. Grab its mate. */
1280 DWORD nbrx;
1281 bRC &= ReadConsoleW(consoleInfo.pstSetup[0].hx, wcBuf+nbr, 1, &nbrx, 0);
1282 if( bRC ) nbr += nbrx;
1283 }
1284 if( !bRC || (noc==0 && nbr==0) ) return 0;
1285 if( nbr > 0 ){
1286 int nmb = WideCharToMultiByte(CP_UTF8, 0, wcBuf,nbr,0,0,0,0);
1287 if( nmb != 0 && noc+nmb <= ncMax ){
1288 int iseg = noc;
1289 nmb = WideCharToMultiByte(CP_UTF8, 0, wcBuf,nbr,cBuf+noc,nmb,0,0);
1290 noc += nmb;
1291 /* Fixup line-ends as coded by Windows for CR (or "Enter".)
1292 ** This is done without regard for any setMode{Text,Binary}()
1293 ** call that might have been done on the interactive input.
1294 */
1295 if( noc > 0 ){
1296 if( cBuf[noc-1]=='\n' ){
1297 lend = 1;
1298 if( noc > 1 && cBuf[noc-2]=='\r' ) cBuf[--noc-1] = '\n';
1299 }
1300 }
1301 /* Check for ^Z (anywhere in line) too, to act as EOF. */
1302 while( iseg < noc ){
1303 if( cBuf[iseg]=='\x1a' ){
1304 noc = iseg; /* Chop ^Z and anything following. */
1305 lend = 1; /* Counts as end of line too. */
1306 break;
1307 }
1308 ++iseg;
1309 }
1310 }else break; /* Drop apparent garbage in. (Could assert.) */
1311 }else break;
1312 }
1313 /* If got nothing, (after ^Z chop), must be at end-of-file. */
1314 if( noc > 0 ){
1315 cBuf[noc] = 0;
1316 return cBuf;
1317 }else return 0;
1318 # endif
1319 }else{
1320 # endif
1321 return cfGets(cBuf, ncMax, pfIn);
1322 # if CIO_WIN_WC_XLATE
1323 }
1324 # endif
1325 }
1326 #endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
1327
1328 #if defined(_MSC_VER)
1329 # pragma warning(default : 4204)
1330 #endif
1331
1332 #undef SHELL_INVALID_FILE_PTR
1333
1334 /************************* End ../ext/consio/console_io.c ********************/
1335
1336 #ifndef SQLITE_SHELL_FIDDLE
1337
1338 /* From here onward, fgets() is redirected to the console_io library. */
1339 # define fgets(b,n,f) fGetsUtf8(b,n,f)
1340 /*
1341 * Define macros for emitting output text in various ways:
1342 * sputz(s, z) => emit 0-terminated string z to given stream s
1343 * sputf(s, f, ...) => emit varargs per format f to given stream s
1344 * oputz(z) => emit 0-terminated string z to default stream
1345 * oputf(f, ...) => emit varargs per format f to default stream
1346 * eputz(z) => emit 0-terminated string z to error stream
1347 * eputf(f, ...) => emit varargs per format f to error stream
1348 * oputb(b, n) => emit char buffer b[0..n-1] to default stream
1349 *
1350 * Note that the default stream is whatever has been last set via:
1351 * setOutputStream(FILE *pf)
1352 * This is normally the stream that CLI normal output goes to.
1353 * For the stand-alone CLI, it is stdout with no .output redirect.
1354 *
1355 * The ?putz(z) forms are required for the Fiddle builds for string literal
1356 * output, in aid of enforcing format string to argument correspondence.
1357 */
1358 # define sputz(s,z) fPutsUtf8(z,s)
1359 # define sputf fPrintfUtf8
1360 # define oputz(z) oPutsUtf8(z)
1361 # define oputf oPrintfUtf8
1362 # define eputz(z) ePutsUtf8(z)
1363 # define eputf ePrintfUtf8
1364 # define oputb(buf,na) oPutbUtf8(buf,na)
1365 # define fflush(s) fFlushBuffer(s);
1366
1367 #else
1368 /* For Fiddle, all console handling and emit redirection is omitted. */
1369 /* These next 3 macros are for emitting formatted output. When complaints
1370 * from the WASM build are issued for non-formatted output, when a mere
1371 * string literal is to be emitted, the ?putz(z) forms should be used.
1372 * (This permits compile-time checking of format string / argument mismatch.)
1373 */
1374 # define oputf(fmt, ...) printf(fmt,__VA_ARGS__)
1375 # define eputf(fmt, ...) fprintf(stderr,fmt,__VA_ARGS__)
1376 # define sputf(fp,fmt, ...) fprintf(fp,fmt,__VA_ARGS__)
1377 /* These next 3 macros are for emitting simple string literals. */
1378 # define oputz(z) fputs(z,stdout)
1379 # define eputz(z) fputs(z,stderr)
1380 # define sputz(fp,z) fputs(z,fp)
1381 # define oputb(buf,na) fwrite(buf,1,na,stdout)
1382 # undef fflush
1383 #endif
1384
1385 /* True if the timer is enabled */
1386 static int enableTimer = 0;
1387
1388 /* A version of strcmp() that works with NULL values */
@@ -1423,10 +582,11 @@
1423 struct timeval ru_utime; /* user CPU time used */
1424 struct timeval ru_stime; /* system CPU time used */
1425 };
1426 #define getrusage(A,B) memset(B,0,sizeof(*B))
1427 #endif
 
1428
1429 /* Saved resource information for the beginning of an operation */
1430 static struct rusage sBegin; /* CPU time at start */
1431 static sqlite3_int64 iBegin; /* Wall-clock time at start */
1432
@@ -1447,24 +607,24 @@
1447 }
1448
1449 /*
1450 ** Print the timing results.
1451 */
1452 static void endTimer(void){
1453 if( enableTimer ){
1454 sqlite3_int64 iEnd = timeOfDay();
1455 struct rusage sEnd;
1456 getrusage(RUSAGE_SELF, &sEnd);
1457 sputf(stdout, "Run Time: real %.3f user %f sys %f\n",
1458 (iEnd - iBegin)*0.001,
1459 timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
1460 timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
1461 }
1462 }
1463
1464 #define BEGIN_TIMER beginTimer()
1465 #define END_TIMER endTimer()
1466 #define HAS_TIMER 1
1467
1468 #elif (defined(_WIN32) || defined(WIN32))
1469
1470 /* Saved resource information for the beginning of an operation */
@@ -1526,29 +686,29 @@
1526 }
1527
1528 /*
1529 ** Print the timing results.
1530 */
1531 static void endTimer(void){
1532 if( enableTimer && getProcessTimesAddr){
1533 FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
1534 sqlite3_int64 ftWallEnd = timeOfDay();
1535 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
1536 sputf(stdout, "Run Time: real %.3f user %f sys %f\n",
1537 (ftWallEnd - ftWallBegin)*0.001,
1538 timeDiff(&ftUserBegin, &ftUserEnd),
1539 timeDiff(&ftKernelBegin, &ftKernelEnd));
1540 }
1541 }
1542
1543 #define BEGIN_TIMER beginTimer()
1544 #define END_TIMER endTimer()
1545 #define HAS_TIMER hasTimer()
1546
1547 #else
1548 #define BEGIN_TIMER
1549 #define END_TIMER
1550 #define HAS_TIMER 0
1551 #endif
1552
1553 /*
1554 ** Used to prevent warnings about unused parameters
@@ -1745,41 +905,219 @@
1745 char *z;
1746 if( iotrace==0 ) return;
1747 va_start(ap, zFormat);
1748 z = sqlite3_vmprintf(zFormat, ap);
1749 va_end(ap);
1750 sputf(iotrace, "%s", z);
1751 sqlite3_free(z);
1752 }
1753 #endif
1754
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1755 /*
1756 ** Output string zUtf to Out stream as w characters. If w is negative,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1757 ** then right-justify the text. W is the width in UTF-8 characters, not
1758 ** in bytes. This is different from the %*.*s specification in printf
1759 ** since with %*.*s the width is measured in bytes, not characters.
 
 
 
 
1760 */
1761 static void utf8_width_print(int w, const char *zUtf){
1762 int i;
1763 int n;
 
 
1764 int aw = w<0 ? -w : w;
1765 if( zUtf==0 ) zUtf = "";
1766 for(i=n=0; zUtf[i]; i++){
1767 if( (zUtf[i]&0xc0)!=0x80 ){
 
 
 
 
 
 
 
 
 
 
 
1768 n++;
1769 if( n==aw ){
1770 do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
1771 break;
1772 }
1773 }
1774 }
1775 if( n>=aw ){
1776 oputf("%.*s", i, zUtf);
1777 }else if( w<0 ){
1778 oputf("%*s%s", aw-n, "", zUtf);
1779 }else{
1780 oputf("%s%*s", zUtf, aw-n, "");
1781 }
1782 }
1783
1784
1785 /*
@@ -1841,11 +1179,11 @@
1841 struct __stat64 x = {0};
1842 # define STAT_CHR_SRC(mode) ((mode & (_S_IFCHR|_S_IFIFO|_S_IFREG))!=0)
1843 /* On Windows, open first, then check the stream nature. This order
1844 ** is necessary because _stat() and sibs, when checking a named pipe,
1845 ** effectively break the pipe as its supplier sees it. */
1846 FILE *rv = fopen(zFile, "rb");
1847 if( rv==0 ) return 0;
1848 if( _fstat64(_fileno(rv), &x) != 0
1849 || !STAT_CHR_SRC(x.st_mode)){
1850 fclose(rv);
1851 rv = 0;
@@ -1855,11 +1193,11 @@
1855 struct stat x = {0};
1856 int rc = stat(zFile, &x);
1857 # define STAT_CHR_SRC(mode) (S_ISREG(mode)||S_ISFIFO(mode)||S_ISCHR(mode))
1858 if( rc!=0 ) return 0;
1859 if( STAT_CHR_SRC(x.st_mode) ){
1860 return fopen(zFile, "rb");
1861 }else{
1862 return 0;
1863 }
1864 #endif
1865 #undef STAT_CHR_SRC
@@ -1882,11 +1220,11 @@
1882 if( n+100>nLine ){
1883 nLine = nLine*2 + 100;
1884 zLine = realloc(zLine, nLine);
1885 shell_check_oom(zLine);
1886 }
1887 if( fgets(&zLine[n], nLine - n, in)==0 ){
1888 if( n==0 ){
1889 free(zLine);
1890 return 0;
1891 }
1892 zLine[n] = 0;
@@ -8604,10 +7942,17 @@
8604 # define lstat(path,buf) stat(path,buf)
8605 #endif
8606 #include <time.h>
8607 #include <errno.h>
8608
 
 
 
 
 
 
 
8609
8610 /*
8611 ** Structure of the fsdir() table-valued function
8612 */
8613 /* 0 1 2 3 4 5 */
@@ -8636,11 +7981,11 @@
8636 sqlite3_int64 nIn;
8637 void *pBuf;
8638 sqlite3 *db;
8639 int mxBlob;
8640
8641 in = fopen(zName, "rb");
8642 if( in==0 ){
8643 /* File does not exist or is unreadable. Leave the result set to NULL. */
8644 return;
8645 }
8646 fseek(in, 0, SEEK_END);
@@ -8891,11 +8236,11 @@
8891 }
8892 }else{
8893 sqlite3_int64 nWrite = 0;
8894 const char *z;
8895 int rc = 0;
8896 FILE *out = fopen(zFile, "wb");
8897 if( out==0 ) return 1;
8898 z = (const char*)sqlite3_value_blob(pData);
8899 if( z ){
8900 sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
8901 nWrite = sqlite3_value_bytes(pData);
@@ -10752,10 +10097,18 @@
10752 #ifndef SQLITE_NO_STDINT
10753 # include <stdint.h>
10754 #endif
10755
10756 #include <zlib.h>
 
 
 
 
 
 
 
 
10757
10758 #ifndef SQLITE_OMIT_VIRTUALTABLE
10759
10760 #ifndef SQLITE_AMALGAMATION
10761
@@ -12009,11 +11362,11 @@
12009 }else{
12010 zFile = (const char*)sqlite3_value_text(argv[0]);
12011 }
12012
12013 if( 0==pTab->pWriteFd && 0==bInMemory ){
12014 pCsr->pFile = zFile ? fopen(zFile, "rb") : 0;
12015 if( pCsr->pFile==0 ){
12016 zipfileCursorErr(pCsr, "cannot open file: %s", zFile);
12017 rc = SQLITE_ERROR;
12018 }else{
12019 rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd);
@@ -12199,11 +11552,11 @@
12199
12200 /* Open a write fd on the file. Also load the entire central directory
12201 ** structure into memory. During the transaction any new file data is
12202 ** appended to the archive file, but the central directory is accumulated
12203 ** in main-memory until the transaction is committed. */
12204 pTab->pWriteFd = fopen(pTab->zFile, "ab+");
12205 if( pTab->pWriteFd==0 ){
12206 pTab->base.zErrMsg = sqlite3_mprintf(
12207 "zipfile: failed to open file %s for writing", pTab->zFile
12208 );
12209 rc = SQLITE_ERROR;
@@ -14883,10 +14236,16 @@
14883 sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC);
14884 while( SQLITE_OK==rc && SQLITE_ROW==sqlite3_step(pIndexXInfo) ){
14885 const char *zComma = zCols==0 ? "" : ", ";
14886 const char *zName = (const char*)sqlite3_column_text(pIndexXInfo, 0);
14887 const char *zColl = (const char*)sqlite3_column_text(pIndexXInfo, 1);
 
 
 
 
 
 
14888 zCols = idxAppendText(&rc, zCols,
14889 "%sx.%Q IS sqlite_expert_rem(%d, x.%Q) COLLATE %s",
14890 zComma, zName, nCol, zName, zColl
14891 );
14892 zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol);
@@ -21991,10 +21350,11 @@
21991 #define MODE_Table 15 /* MySQL-style table formatting */
21992 #define MODE_Box 16 /* Unicode box-drawing characters */
21993 #define MODE_Count 17 /* Output only a count of the rows of output */
21994 #define MODE_Off 18 /* No query output shown */
21995 #define MODE_ScanExp 19 /* Like MODE_Explain, but for ".scanstats vm" */
 
21996
21997 static const char *modeDescr[] = {
21998 "line",
21999 "column",
22000 "list",
@@ -22011,11 +21371,13 @@
22011 "json",
22012 "markdown",
22013 "table",
22014 "box",
22015 "count",
22016 "off"
 
 
22017 };
22018
22019 /*
22020 ** These are the column/row/line separators used by the various
22021 ** import/export modes.
@@ -22023,11 +21385,17 @@
22023 #define SEP_Column "|"
22024 #define SEP_Row "\n"
22025 #define SEP_Tab "\t"
22026 #define SEP_Space " "
22027 #define SEP_Comma ","
22028 #define SEP_CrLf "\r\n"
 
 
 
 
 
 
22029 #define SEP_Unit "\x1F"
22030 #define SEP_Record "\x1E"
22031
22032 /*
22033 ** Limit input nesting via .read or any other input redirect.
@@ -22039,11 +21407,11 @@
22039 ** A callback for the sqlite3_log() interface.
22040 */
22041 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
22042 ShellState *p = (ShellState*)pArg;
22043 if( p->pLog==0 ) return;
22044 sputf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
22045 fflush(p->pLog);
22046 }
22047
22048 /*
22049 ** SQL function: shell_putsnl(X)
@@ -22054,13 +21422,13 @@
22054 static void shellPutsFunc(
22055 sqlite3_context *pCtx,
22056 int nVal,
22057 sqlite3_value **apVal
22058 ){
22059 /* Unused: (ShellState*)sqlite3_user_data(pCtx); */
22060 (void)nVal;
22061 oputf("%s\n", sqlite3_value_text(apVal[0]));
22062 sqlite3_result_value(pCtx, apVal[0]);
22063 }
22064
22065 /*
22066 ** If in safe mode, print an error message described by the arguments
@@ -22075,11 +21443,11 @@
22075 va_list ap;
22076 char *zMsg;
22077 va_start(ap, zErrMsg);
22078 zMsg = sqlite3_vmprintf(zErrMsg, ap);
22079 va_end(ap);
22080 eputf("line %d: %s\n", p->lineno, zMsg);
22081 exit(1);
22082 }
22083 }
22084
22085 /*
@@ -22142,11 +21510,11 @@
22142 }
22143 }
22144 bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
22145 /* When writing the file to be edited, do \n to \r\n conversions on systems
22146 ** that want \r\n line endings */
22147 f = fopen(zTempFile, bBin ? "wb" : "w");
22148 if( f==0 ){
22149 sqlite3_result_error(context, "edit() cannot open temp file", -1);
22150 goto edit_func_end;
22151 }
22152 sz = sqlite3_value_bytes(argv[0]);
@@ -22173,11 +21541,11 @@
22173 sqlite3_free(zCmd);
22174 if( rc ){
22175 sqlite3_result_error(context, "EDITOR returned non-zero", -1);
22176 goto edit_func_end;
22177 }
22178 f = fopen(zTempFile, "rb");
22179 if( f==0 ){
22180 sqlite3_result_error(context,
22181 "edit() cannot reopen temp file after edit", -1);
22182 goto edit_func_end;
22183 }
@@ -22243,11 +21611,11 @@
22243 }
22244
22245 /*
22246 ** Output the given string as a hex-encoded blob (eg. X'1234' )
22247 */
22248 static void output_hex_blob(const void *pBlob, int nBlob){
22249 int i;
22250 unsigned char *aBlob = (unsigned char*)pBlob;
22251
22252 char *zStr = sqlite3_malloc(nBlob*2 + 1);
22253 shell_check_oom(zStr);
@@ -22260,11 +21628,11 @@
22260 zStr[i*2] = aHex[ (aBlob[i] >> 4) ];
22261 zStr[i*2+1] = aHex[ (aBlob[i] & 0x0F) ];
22262 }
22263 zStr[i*2] = '\0';
22264
22265 oputf("X'%s'", zStr);
22266 sqlite3_free(zStr);
22267 }
22268
22269 /*
22270 ** Find a string that is not found anywhere in z[]. Return a pointer
@@ -22290,46 +21658,39 @@
22290 /*
22291 ** Output the given string as a quoted string using SQL quoting conventions.
22292 **
22293 ** See also: output_quoted_escaped_string()
22294 */
22295 static void output_quoted_string(const char *z){
22296 int i;
22297 char c;
22298 #ifndef SQLITE_SHELL_FIDDLE
22299 FILE *pfO = setOutputStream(invalidFileStream);
22300 setBinaryMode(pfO, 1);
22301 #endif
22302 if( z==0 ) return;
22303 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
22304 if( c==0 ){
22305 oputf("'%s'",z);
22306 }else{
22307 oputz("'");
22308 while( *z ){
22309 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
22310 if( c=='\'' ) i++;
22311 if( i ){
22312 oputf("%.*s", i, z);
22313 z += i;
22314 }
22315 if( c=='\'' ){
22316 oputz("'");
22317 continue;
22318 }
22319 if( c==0 ){
22320 break;
22321 }
22322 z++;
22323 }
22324 oputz("'");
22325 }
22326 #ifndef SQLITE_SHELL_FIDDLE
22327 setTextMode(pfO, 1);
22328 #else
22329 setTextMode(stdout, 1);
22330 #endif
22331 }
22332
22333 /*
22334 ** Output the given string as a quoted string using SQL quoting conventions.
22335 ** Additionallly , escape the "\n" and "\r" characters so that they do not
@@ -22337,20 +21698,17 @@
22337 ** systems.
22338 **
22339 ** This is like output_quoted_string() but with the addition of the \r\n
22340 ** escape mechanism.
22341 */
22342 static void output_quoted_escaped_string(const char *z){
22343 int i;
22344 char c;
22345 #ifndef SQLITE_SHELL_FIDDLE
22346 FILE *pfO = setOutputStream(invalidFileStream);
22347 setBinaryMode(pfO, 1);
22348 #endif
22349 for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
22350 if( c==0 ){
22351 oputf("'%s'",z);
22352 }else{
22353 const char *zNL = 0;
22354 const char *zCR = 0;
22355 int nNL = 0;
22356 int nCR = 0;
@@ -22358,52 +21716,48 @@
22358 for(i=0; z[i]; i++){
22359 if( z[i]=='\n' ) nNL++;
22360 if( z[i]=='\r' ) nCR++;
22361 }
22362 if( nNL ){
22363 oputz("replace(");
22364 zNL = unused_string(z, "\\n", "\\012", zBuf1);
22365 }
22366 if( nCR ){
22367 oputz("replace(");
22368 zCR = unused_string(z, "\\r", "\\015", zBuf2);
22369 }
22370 oputz("'");
22371 while( *z ){
22372 for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
22373 if( c=='\'' ) i++;
22374 if( i ){
22375 oputf("%.*s", i, z);
22376 z += i;
22377 }
22378 if( c=='\'' ){
22379 oputz("'");
22380 continue;
22381 }
22382 if( c==0 ){
22383 break;
22384 }
22385 z++;
22386 if( c=='\n' ){
22387 oputz(zNL);
22388 continue;
22389 }
22390 oputz(zCR);
22391 }
22392 oputz("'");
22393 if( nCR ){
22394 oputf(",'%s',char(13))", zCR);
22395 }
22396 if( nNL ){
22397 oputf(",'%s',char(10))", zNL);
22398 }
22399 }
22400 #ifndef SQLITE_SHELL_FIDDLE
22401 setTextMode(pfO, 1);
22402 #else
22403 setTextMode(stdout, 1);
22404 #endif
22405 }
22406
22407 /*
22408 ** Find earliest of chars within s specified in zAny.
22409 ** With ns == ~0, is like strpbrk(s,zAny) and s must be 0-terminated.
@@ -22419,26 +21773,64 @@
22419 }
22420 ++zAny;
22421 }
22422 return pcFirst;
22423 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22424 /*
22425 ** Output the given string as a quoted according to C or TCL quoting rules.
22426 */
22427 static void output_c_string(const char *z){
22428 char c;
22429 static const char *zq = "\"";
22430 static long ctrlMask = ~0L;
22431 static const char *zDQBSRO = "\"\\\x7f"; /* double-quote, backslash, rubout */
22432 char ace[3] = "\\?";
22433 char cbsSay;
22434 oputz(zq);
22435 while( *z!=0 ){
22436 const char *pcDQBSRO = anyOfInStr(z, zDQBSRO, ~(size_t)0);
22437 const char *pcPast = zSkipValidUtf8(z, INT_MAX, ctrlMask);
22438 const char *pcEnd = (pcDQBSRO && pcDQBSRO < pcPast)? pcDQBSRO : pcPast;
22439 if( pcEnd > z ) oputb(z, (int)(pcEnd-z));
 
 
22440 if( (c = *pcEnd)==0 ) break;
22441 ++pcEnd;
22442 switch( c ){
22443 case '\\': case '"':
22444 cbsSay = (char)c;
@@ -22449,26 +21841,26 @@
22449 case '\f': cbsSay = 'f'; break;
22450 default: cbsSay = 0; break;
22451 }
22452 if( cbsSay ){
22453 ace[1] = cbsSay;
22454 oputz(ace);
22455 }else if( !isprint(c&0xff) ){
22456 oputf("\\%03o", c&0xff);
22457 }else{
22458 ace[1] = (char)c;
22459 oputz(ace+1);
22460 }
22461 z = pcEnd;
22462 }
22463 oputz(zq);
22464 }
22465
22466 /*
22467 ** Output the given string as a quoted according to JSON quoting rules.
22468 */
22469 static void output_json_string(const char *z, i64 n){
22470 char c;
22471 static const char *zq = "\"";
22472 static long ctrlMask = ~0L;
22473 static const char *zDQBS = "\"\\";
22474 const char *pcLimit;
@@ -22475,17 +21867,17 @@
22475 char ace[3] = "\\?";
22476 char cbsSay;
22477
22478 if( z==0 ) z = "";
22479 pcLimit = z + ((n<0)? strlen(z) : (size_t)n);
22480 oputz(zq);
22481 while( z < pcLimit ){
22482 const char *pcDQBS = anyOfInStr(z, zDQBS, pcLimit-z);
22483 const char *pcPast = zSkipValidUtf8(z, (int)(pcLimit-z), ctrlMask);
22484 const char *pcEnd = (pcDQBS && pcDQBS < pcPast)? pcDQBS : pcPast;
22485 if( pcEnd > z ){
22486 oputb(z, (int)(pcEnd-z));
22487 z = pcEnd;
22488 }
22489 if( z >= pcLimit ) break;
22490 c = *(z++);
22491 switch( c ){
@@ -22499,26 +21891,26 @@
22499 case '\t': cbsSay = 't'; break;
22500 default: cbsSay = 0; break;
22501 }
22502 if( cbsSay ){
22503 ace[1] = cbsSay;
22504 oputz(ace);
22505 }else if( c<=0x1f ){
22506 oputf("u%04x", c);
22507 }else{
22508 ace[1] = (char)c;
22509 oputz(ace+1);
22510 }
22511 }
22512 oputz(zq);
22513 }
22514
22515 /*
22516 ** Output the given string with characters that are special to
22517 ** HTML escaped.
22518 */
22519 static void output_html_string(const char *z){
22520 int i;
22521 if( z==0 ) z = "";
22522 while( *z ){
22523 for(i=0; z[i]
22524 && z[i]!='<'
@@ -22526,22 +21918,22 @@
22526 && z[i]!='>'
22527 && z[i]!='\"'
22528 && z[i]!='\'';
22529 i++){}
22530 if( i>0 ){
22531 oputf("%.*s",i,z);
22532 }
22533 if( z[i]=='<' ){
22534 oputz("&lt;");
22535 }else if( z[i]=='&' ){
22536 oputz("&amp;");
22537 }else if( z[i]=='>' ){
22538 oputz("&gt;");
22539 }else if( z[i]=='\"' ){
22540 oputz("&quot;");
22541 }else if( z[i]=='\'' ){
22542 oputz("&#39;");
22543 }else{
22544 break;
22545 }
22546 z += i + 1;
22547 }
@@ -22576,11 +21968,11 @@
22576 ** the null value. Strings are quoted if necessary. The separator
22577 ** is only issued if bSep is true.
22578 */
22579 static void output_csv(ShellState *p, const char *z, int bSep){
22580 if( z==0 ){
22581 oputf("%s",p->nullValue);
22582 }else{
22583 unsigned i;
22584 for(i=0; z[i]; i++){
22585 if( needCsvQuote[((unsigned char*)z)[i]] ){
22586 i = 0;
@@ -22588,18 +21980,18 @@
22588 }
22589 }
22590 if( i==0 || strstr(z, p->colSeparator)!=0 ){
22591 char *zQuoted = sqlite3_mprintf("\"%w\"", z);
22592 shell_check_oom(zQuoted);
22593 oputz(zQuoted);
22594 sqlite3_free(zQuoted);
22595 }else{
22596 oputz(z);
22597 }
22598 }
22599 if( bSep ){
22600 oputz(p->colSeparator);
22601 }
22602 }
22603
22604 /*
22605 ** This routine runs when the user presses Ctrl-C
@@ -22703,20 +22095,20 @@
22703 const char *az[4];
22704 az[0] = zA1;
22705 az[1] = zA2;
22706 az[2] = zA3;
22707 az[3] = zA4;
22708 oputf("authorizer: %s", azAction[op]);
22709 for(i=0; i<4; i++){
22710 oputz(" ");
22711 if( az[i] ){
22712 output_c_string(az[i]);
22713 }else{
22714 oputz("NULL");
22715 }
22716 }
22717 oputz("\n");
22718 if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4);
22719 return SQLITE_OK;
22720 }
22721 #endif
22722
@@ -22728,11 +22120,11 @@
22728 **
22729 ** If the schema statement in z[] contains a start-of-comment and if
22730 ** sqlite3_complete() returns false, try to terminate the comment before
22731 ** printing the result. https://sqlite.org/forum/forumpost/d7be961c5c
22732 */
22733 static void printSchemaLine(const char *z, const char *zTail){
22734 char *zToFree = 0;
22735 if( z==0 ) return;
22736 if( zTail==0 ) return;
22737 if( zTail[0]==';' && (strstr(z, "/*")!=0 || strstr(z,"--")!=0) ){
22738 const char *zOrig = z;
@@ -22750,20 +22142,20 @@
22750 }
22751 sqlite3_free(zNew);
22752 }
22753 }
22754 if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
22755 oputf("CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
22756 }else{
22757 oputf("%s%s", z, zTail);
22758 }
22759 sqlite3_free(zToFree);
22760 }
22761 static void printSchemaLineN(char *z, int n, const char *zTail){
22762 char c = z[n];
22763 z[n] = 0;
22764 printSchemaLine(z, zTail);
22765 z[n] = c;
22766 }
22767
22768 /*
22769 ** Return true if string z[] has nothing but whitespace and comments to the
@@ -22787,11 +22179,11 @@
22787 EQPGraphRow *pNew;
22788 i64 nText;
22789 if( zText==0 ) return;
22790 nText = strlen(zText);
22791 if( p->autoEQPtest ){
22792 oputf("%d,%d,%s\n", iEqpId, p2, zText);
22793 }
22794 pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
22795 shell_check_oom(pNew);
22796 pNew->iEqpId = iEqpId;
22797 pNew->iParentId = p2;
@@ -22835,11 +22227,12 @@
22835 i64 n = strlen(p->sGraph.zPrefix);
22836 char *z;
22837 for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
22838 pNext = eqp_next_row(p, iEqpId, pRow);
22839 z = pRow->zText;
22840 oputf("%s%s%s\n", p->sGraph.zPrefix, pNext ? "|--" : "`--", z);
 
22841 if( n<(i64)sizeof(p->sGraph.zPrefix)-7 ){
22842 memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4);
22843 eqp_render_level(p, pRow->iEqpId);
22844 p->sGraph.zPrefix[n] = 0;
22845 }
@@ -22855,17 +22248,17 @@
22855 if( pRow->zText[0]=='-' ){
22856 if( pRow->pNext==0 ){
22857 eqp_reset(p);
22858 return;
22859 }
22860 oputf("%s\n", pRow->zText+3);
22861 p->sGraph.pRow = pRow->pNext;
22862 sqlite3_free(pRow);
22863 }else if( nCycle>0 ){
22864 oputf("QUERY PLAN (cycles=%lld [100%%])\n", nCycle);
22865 }else{
22866 oputz("QUERY PLAN\n");
22867 }
22868 p->sGraph.zPrefix[0] = 0;
22869 eqp_render_level(p, 0);
22870 eqp_reset(p);
22871 }
@@ -22877,33 +22270,33 @@
22877 */
22878 static int progress_handler(void *pClientData) {
22879 ShellState *p = (ShellState*)pClientData;
22880 p->nProgress++;
22881 if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
22882 oputf("Progress limit reached (%u)\n", p->nProgress);
22883 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
22884 if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
22885 return 1;
22886 }
22887 if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
22888 oputf("Progress %u\n", p->nProgress);
22889 }
22890 return 0;
22891 }
22892 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
22893
22894 /*
22895 ** Print N dashes
22896 */
22897 static void print_dashes(int N){
22898 const char zDash[] = "--------------------------------------------------";
22899 const int nDash = sizeof(zDash) - 1;
22900 while( N>nDash ){
22901 oputz(zDash);
22902 N -= nDash;
22903 }
22904 oputf("%.*s", N, zDash);
22905 }
22906
22907 /*
22908 ** Print a markdown or table-style row separator using ascii-art
22909 */
@@ -22912,19 +22305,19 @@
22912 int nArg,
22913 const char *zSep
22914 ){
22915 int i;
22916 if( nArg>0 ){
22917 oputz(zSep);
22918 print_dashes(p->actualWidth[0]+2);
22919 for(i=1; i<nArg; i++){
22920 oputz(zSep);
22921 print_dashes(p->actualWidth[i]+2);
22922 }
22923 oputz(zSep);
22924 }
22925 oputz("\n");
22926 }
22927
22928 /*
22929 ** This is the callback routine that the shell
22930 ** invokes for each row of a query result.
@@ -22950,13 +22343,13 @@
22950 if( azArg==0 ) break;
22951 for(i=0; i<nArg; i++){
22952 int len = strlen30(azCol[i] ? azCol[i] : "");
22953 if( len>w ) w = len;
22954 }
22955 if( p->cnt++>0 ) oputz(p->rowSeparator);
22956 for(i=0; i<nArg; i++){
22957 oputf("%*s = %s%s", w, azCol[i],
22958 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
22959 }
22960 break;
22961 }
22962 case MODE_ScanExp:
@@ -22980,16 +22373,16 @@
22980 if( nArg>nWidth ) nArg = nWidth;
22981
22982 /* If this is the first row seen, print out the headers */
22983 if( p->cnt++==0 ){
22984 for(i=0; i<nArg; i++){
22985 utf8_width_print(aWidth[i], azCol[ aMap[i] ]);
22986 oputz(i==nArg-1 ? "\n" : " ");
22987 }
22988 for(i=0; i<nArg; i++){
22989 print_dashes(aWidth[i]);
22990 oputz(i==nArg-1 ? "\n" : " ");
22991 }
22992 }
22993
22994 /* If there is no data, exit early. */
22995 if( azArg==0 ) break;
@@ -23003,21 +22396,21 @@
23003 w = strlenChar(zVal);
23004 zSep = " ";
23005 }
23006 if( i==iIndent && p->aiIndent && p->pStmt ){
23007 if( p->iIndent<p->nIndent ){
23008 oputf("%*.s", p->aiIndent[p->iIndent], "");
23009 }
23010 p->iIndent++;
23011 }
23012 utf8_width_print(w, zVal ? zVal : p->nullValue);
23013 oputz(i==nArg-1 ? "\n" : zSep);
23014 }
23015 break;
23016 }
23017 case MODE_Semi: { /* .schema and .fullschema output */
23018 printSchemaLine(azArg[0], ";\n");
23019 break;
23020 }
23021 case MODE_Pretty: { /* .schema and .fullschema with --indent */
23022 char *z;
23023 int j;
@@ -23028,11 +22421,11 @@
23028 assert( nArg==1 );
23029 if( azArg[0]==0 ) break;
23030 if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
23031 || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
23032 ){
23033 oputf("%s;\n", azArg[0]);
23034 break;
23035 }
23036 z = sqlite3_mprintf("%s", azArg[0]);
23037 shell_check_oom(z);
23038 j = 0;
@@ -23061,255 +22454,265 @@
23061 }else if( c=='(' ){
23062 nParen++;
23063 }else if( c==')' ){
23064 nParen--;
23065 if( nLine>0 && nParen==0 && j>0 ){
23066 printSchemaLineN(z, j, "\n");
23067 j = 0;
23068 }
23069 }
23070 z[j++] = c;
23071 if( nParen==1 && cEnd==0
23072 && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
23073 ){
23074 if( c=='\n' ) j--;
23075 printSchemaLineN(z, j, "\n ");
23076 j = 0;
23077 nLine++;
23078 while( IsSpace(z[i+1]) ){ i++; }
23079 }
23080 }
23081 z[j] = 0;
23082 }
23083 printSchemaLine(z, ";\n");
23084 sqlite3_free(z);
23085 break;
23086 }
23087 case MODE_List: {
23088 if( p->cnt++==0 && p->showHeader ){
23089 for(i=0; i<nArg; i++){
23090 oputf("%s%s",azCol[i], i==nArg-1 ? p->rowSeparator : p->colSeparator);
 
23091 }
23092 }
23093 if( azArg==0 ) break;
23094 for(i=0; i<nArg; i++){
23095 char *z = azArg[i];
23096 if( z==0 ) z = p->nullValue;
23097 oputz(z);
23098 oputz((i<nArg-1)? p->colSeparator : p->rowSeparator);
23099 }
23100 break;
23101 }
 
23102 case MODE_Html: {
23103 if( p->cnt++==0 && p->showHeader ){
23104 oputz("<TR>");
 
 
 
 
 
 
 
23105 for(i=0; i<nArg; i++){
23106 oputz("<TH>");
23107 output_html_string(azCol[i]);
23108 oputz("</TH>\n");
23109 }
23110 oputz("</TR>\n");
23111 }
 
23112 if( azArg==0 ) break;
23113 oputz("<TR>");
23114 for(i=0; i<nArg; i++){
23115 oputz("<TD>");
23116 output_html_string(azArg[i] ? azArg[i] : p->nullValue);
23117 oputz("</TD>\n");
23118 }
23119 oputz("</TR>\n");
23120 break;
23121 }
23122 case MODE_Tcl: {
23123 if( p->cnt++==0 && p->showHeader ){
23124 for(i=0; i<nArg; i++){
23125 output_c_string(azCol[i] ? azCol[i] : "");
23126 if(i<nArg-1) oputz(p->colSeparator);
23127 }
23128 oputz(p->rowSeparator);
23129 }
23130 if( azArg==0 ) break;
23131 for(i=0; i<nArg; i++){
23132 output_c_string(azArg[i] ? azArg[i] : p->nullValue);
23133 if(i<nArg-1) oputz(p->colSeparator);
23134 }
23135 oputz(p->rowSeparator);
23136 break;
23137 }
23138 case MODE_Csv: {
23139 setBinaryMode(p->out, 1);
23140 if( p->cnt++==0 && p->showHeader ){
23141 for(i=0; i<nArg; i++){
23142 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
23143 }
23144 oputz(p->rowSeparator);
23145 }
23146 if( nArg>0 ){
23147 for(i=0; i<nArg; i++){
23148 output_csv(p, azArg[i], i<nArg-1);
23149 }
23150 oputz(p->rowSeparator);
23151 }
23152 setTextMode(p->out, 1);
23153 break;
23154 }
23155 case MODE_Insert: {
23156 if( azArg==0 ) break;
23157 oputf("INSERT INTO %s",p->zDestTable);
23158 if( p->showHeader ){
23159 oputz("(");
23160 for(i=0; i<nArg; i++){
23161 if( i>0 ) oputz(",");
23162 if( quoteChar(azCol[i]) ){
23163 char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
23164 shell_check_oom(z);
23165 oputz(z);
23166 sqlite3_free(z);
23167 }else{
23168 oputf("%s", azCol[i]);
23169 }
23170 }
23171 oputz(")");
23172 }
23173 p->cnt++;
23174 for(i=0; i<nArg; i++){
23175 oputz(i>0 ? "," : " VALUES(");
23176 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
23177 oputz("NULL");
23178 }else if( aiType && aiType[i]==SQLITE_TEXT ){
23179 if( ShellHasFlag(p, SHFLG_Newlines) ){
23180 output_quoted_string(azArg[i]);
23181 }else{
23182 output_quoted_escaped_string(azArg[i]);
23183 }
23184 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
23185 oputz(azArg[i]);
23186 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
23187 char z[50];
23188 double r = sqlite3_column_double(p->pStmt, i);
23189 sqlite3_uint64 ur;
23190 memcpy(&ur,&r,sizeof(r));
23191 if( ur==0x7ff0000000000000LL ){
23192 oputz("9.0e+999");
23193 }else if( ur==0xfff0000000000000LL ){
23194 oputz("-9.0e+999");
23195 }else{
23196 sqlite3_int64 ir = (sqlite3_int64)r;
23197 if( r==(double)ir ){
23198 sqlite3_snprintf(50,z,"%lld.0", ir);
23199 }else{
23200 sqlite3_snprintf(50,z,"%!.20g", r);
23201 }
23202 oputz(z);
23203 }
23204 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
23205 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
23206 int nBlob = sqlite3_column_bytes(p->pStmt, i);
23207 output_hex_blob(pBlob, nBlob);
23208 }else if( isNumber(azArg[i], 0) ){
23209 oputz(azArg[i]);
23210 }else if( ShellHasFlag(p, SHFLG_Newlines) ){
23211 output_quoted_string(azArg[i]);
23212 }else{
23213 output_quoted_escaped_string(azArg[i]);
23214 }
23215 }
23216 oputz(");\n");
23217 break;
23218 }
23219 case MODE_Json: {
23220 if( azArg==0 ) break;
23221 if( p->cnt==0 ){
23222 fputs("[{", p->out);
23223 }else{
23224 fputs(",\n{", p->out);
23225 }
23226 p->cnt++;
23227 for(i=0; i<nArg; i++){
23228 output_json_string(azCol[i], -1);
23229 oputz(":");
23230 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
23231 oputz("null");
23232 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
23233 char z[50];
23234 double r = sqlite3_column_double(p->pStmt, i);
23235 sqlite3_uint64 ur;
23236 memcpy(&ur,&r,sizeof(r));
23237 if( ur==0x7ff0000000000000LL ){
23238 oputz("9.0e+999");
23239 }else if( ur==0xfff0000000000000LL ){
23240 oputz("-9.0e+999");
23241 }else{
23242 sqlite3_snprintf(50,z,"%!.20g", r);
23243 oputz(z);
23244 }
23245 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
23246 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
23247 int nBlob = sqlite3_column_bytes(p->pStmt, i);
23248 output_json_string(pBlob, nBlob);
23249 }else if( aiType && aiType[i]==SQLITE_TEXT ){
23250 output_json_string(azArg[i], -1);
23251 }else{
23252 oputz(azArg[i]);
23253 }
23254 if( i<nArg-1 ){
23255 oputz(",");
23256 }
23257 }
23258 oputz("}");
23259 break;
23260 }
23261 case MODE_Quote: {
23262 if( azArg==0 ) break;
23263 if( p->cnt==0 && p->showHeader ){
23264 for(i=0; i<nArg; i++){
23265 if( i>0 ) fputs(p->colSeparator, p->out);
23266 output_quoted_string(azCol[i]);
23267 }
23268 fputs(p->rowSeparator, p->out);
23269 }
23270 p->cnt++;
23271 for(i=0; i<nArg; i++){
23272 if( i>0 ) fputs(p->colSeparator, p->out);
23273 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
23274 oputz("NULL");
23275 }else if( aiType && aiType[i]==SQLITE_TEXT ){
23276 output_quoted_string(azArg[i]);
23277 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
23278 oputz(azArg[i]);
23279 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
23280 char z[50];
23281 double r = sqlite3_column_double(p->pStmt, i);
23282 sqlite3_snprintf(50,z,"%!.20g", r);
23283 oputz(z);
23284 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
23285 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
23286 int nBlob = sqlite3_column_bytes(p->pStmt, i);
23287 output_hex_blob(pBlob, nBlob);
23288 }else if( isNumber(azArg[i], 0) ){
23289 oputz(azArg[i]);
23290 }else{
23291 output_quoted_string(azArg[i]);
23292 }
23293 }
23294 fputs(p->rowSeparator, p->out);
23295 break;
23296 }
23297 case MODE_Ascii: {
23298 if( p->cnt++==0 && p->showHeader ){
23299 for(i=0; i<nArg; i++){
23300 if( i>0 ) oputz(p->colSeparator);
23301 oputz(azCol[i] ? azCol[i] : "");
23302 }
23303 oputz(p->rowSeparator);
23304 }
23305 if( azArg==0 ) break;
23306 for(i=0; i<nArg; i++){
23307 if( i>0 ) oputz(p->colSeparator);
23308 oputz(azArg[i] ? azArg[i] : p->nullValue);
23309 }
23310 oputz(p->rowSeparator);
23311 break;
23312 }
23313 case MODE_EQP: {
23314 eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
23315 break;
@@ -23384,11 +22787,11 @@
23384 "INSERT INTO selftest(tno,op,cmd,ans)"
23385 " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
23386 "DROP TABLE [_shell$self];"
23387 ,0,0,&zErrMsg);
23388 if( zErrMsg ){
23389 eputf("SELFTEST initialization failure: %s\n", zErrMsg);
23390 sqlite3_free(zErrMsg);
23391 }
23392 sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
23393 }
23394
@@ -23487,36 +22890,37 @@
23487 int i;
23488 const char *z;
23489 rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
23490 if( rc!=SQLITE_OK || !pSelect ){
23491 char *zContext = shell_error_context(zSelect, p->db);
23492 oputf("/**** ERROR: (%d) %s *****/\n%s",
23493 rc, sqlite3_errmsg(p->db), zContext);
23494 sqlite3_free(zContext);
23495 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
23496 return rc;
23497 }
23498 rc = sqlite3_step(pSelect);
23499 nResult = sqlite3_column_count(pSelect);
23500 while( rc==SQLITE_ROW ){
23501 z = (const char*)sqlite3_column_text(pSelect, 0);
23502 oputf("%s", z);
23503 for(i=1; i<nResult; i++){
23504 oputf(",%s", sqlite3_column_text(pSelect, i));
23505 }
23506 if( z==0 ) z = "";
23507 while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
23508 if( z[0] ){
23509 oputz("\n;\n");
23510 }else{
23511 oputz(";\n");
23512 }
23513 rc = sqlite3_step(pSelect);
23514 }
23515 rc = sqlite3_finalize(pSelect);
23516 if( rc!=SQLITE_OK ){
23517 oputf("/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db));
 
23518 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
23519 }
23520 return rc;
23521 }
23522
@@ -23548,17 +22952,17 @@
23548
23549 #ifdef __linux__
23550 /*
23551 ** Attempt to display I/O stats on Linux using /proc/PID/io
23552 */
23553 static void displayLinuxIoStats(void){
23554 FILE *in;
23555 char z[200];
23556 sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
23557 in = fopen(z, "rb");
23558 if( in==0 ) return;
23559 while( fgets(z, sizeof(z), in)!=0 ){
23560 static const struct {
23561 const char *zPattern;
23562 const char *zDesc;
23563 } aTrans[] = {
23564 { "rchar: ", "Bytes received by read():" },
@@ -23571,11 +22975,11 @@
23571 };
23572 int i;
23573 for(i=0; i<ArraySize(aTrans); i++){
23574 int n = strlen30(aTrans[i].zPattern);
23575 if( cli_strncmp(aTrans[i].zPattern, z, n)==0 ){
23576 oputf("%-36s %s", aTrans[i].zDesc, &z[n]);
23577 break;
23578 }
23579 }
23580 }
23581 fclose(in);
@@ -23584,10 +22988,11 @@
23584
23585 /*
23586 ** Display a single line of status using 64-bit values.
23587 */
23588 static void displayStatLine(
 
23589 char *zLabel, /* Label for this one line */
23590 char *zFormat, /* Format for the result */
23591 int iStatusCtrl, /* Which status to display */
23592 int bReset /* True to reset the stats */
23593 ){
@@ -23602,11 +23007,11 @@
23602 if( nPercent>1 ){
23603 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
23604 }else{
23605 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
23606 }
23607 oputf("%-36s %s\n", zLabel, zLine);
23608 }
23609
23610 /*
23611 ** Display memory stats.
23612 */
@@ -23615,130 +23020,152 @@
23615 ShellState *pArg, /* Pointer to ShellState */
23616 int bReset /* True to reset the stats */
23617 ){
23618 int iCur;
23619 int iHiwtr;
 
23620 if( pArg==0 || pArg->out==0 ) return 0;
 
23621
23622 if( pArg->pStmt && pArg->statsOn==2 ){
23623 int nCol, i, x;
23624 sqlite3_stmt *pStmt = pArg->pStmt;
23625 char z[100];
23626 nCol = sqlite3_column_count(pStmt);
23627 oputf("%-36s %d\n", "Number of output columns:", nCol);
23628 for(i=0; i<nCol; i++){
23629 sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
23630 oputf("%-36s %s\n", z, sqlite3_column_name(pStmt,i));
23631 #ifndef SQLITE_OMIT_DECLTYPE
23632 sqlite3_snprintf(30, z+x, "declared type:");
23633 oputf("%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
23634 #endif
23635 #ifdef SQLITE_ENABLE_COLUMN_METADATA
23636 sqlite3_snprintf(30, z+x, "database name:");
23637 oputf("%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
 
23638 sqlite3_snprintf(30, z+x, "table name:");
23639 oputf("%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
23640 sqlite3_snprintf(30, z+x, "origin name:");
23641 oputf("%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
23642 #endif
23643 }
23644 }
23645
23646 if( pArg->statsOn==3 ){
23647 if( pArg->pStmt ){
23648 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP,bReset);
23649 oputf("VM-steps: %d\n", iCur);
23650 }
23651 return 0;
23652 }
23653
23654 displayStatLine("Memory Used:",
23655 "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
23656 displayStatLine("Number of Outstanding Allocations:",
23657 "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
23658 if( pArg->shellFlgs & SHFLG_Pagecache ){
23659 displayStatLine("Number of Pcache Pages Used:",
23660 "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
23661 }
23662 displayStatLine("Number of Pcache Overflow Bytes:",
23663 "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
23664 displayStatLine("Largest Allocation:",
23665 "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
23666 displayStatLine("Largest Pcache Allocation:",
23667 "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
23668 #ifdef YYTRACKMAXSTACKDEPTH
23669 displayStatLine("Deepest Parser Stack:",
23670 "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
23671 #endif
23672
23673 if( db ){
23674 if( pArg->shellFlgs & SHFLG_Lookaside ){
23675 iHiwtr = iCur = -1;
23676 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
23677 &iCur, &iHiwtr, bReset);
23678 oputf("Lookaside Slots Used: %d (max %d)\n", iCur, iHiwtr);
 
23679 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
23680 &iCur, &iHiwtr, bReset);
23681 oputf("Successful lookaside attempts: %d\n", iHiwtr);
 
23682 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
23683 &iCur, &iHiwtr, bReset);
23684 oputf("Lookaside failures due to size: %d\n", iHiwtr);
 
23685 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
23686 &iCur, &iHiwtr, bReset);
23687 oputf("Lookaside failures due to OOM: %d\n", iHiwtr);
 
23688 }
23689 iHiwtr = iCur = -1;
23690 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
23691 oputf("Pager Heap Usage: %d bytes\n", iCur);
 
23692 iHiwtr = iCur = -1;
23693 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
23694 oputf("Page cache hits: %d\n", iCur);
 
23695 iHiwtr = iCur = -1;
23696 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
23697 oputf("Page cache misses: %d\n", iCur);
 
23698 iHiwtr = iCur = -1;
23699 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
23700 oputf("Page cache writes: %d\n", iCur);
 
23701 iHiwtr = iCur = -1;
23702 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
23703 oputf("Page cache spills: %d\n", iCur);
 
23704 iHiwtr = iCur = -1;
23705 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
23706 oputf("Schema Heap Usage: %d bytes\n", iCur);
 
23707 iHiwtr = iCur = -1;
23708 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
23709 oputf("Statement Heap/Lookaside Usage: %d bytes\n", iCur);
 
23710 }
23711
23712 if( pArg->pStmt ){
23713 int iHit, iMiss;
23714 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
23715 bReset);
23716 oputf("Fullscan Steps: %d\n", iCur);
 
23717 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
23718 oputf("Sort Operations: %d\n", iCur);
 
23719 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
23720 oputf("Autoindex Inserts: %d\n", iCur);
 
23721 iHit = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_HIT,
23722 bReset);
23723 iMiss = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_MISS,
23724 bReset);
23725 if( iHit || iMiss ){
23726 oputf("Bloom filter bypass taken: %d/%d\n", iHit, iHit+iMiss);
 
23727 }
23728 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
23729 oputf("Virtual Machine Steps: %d\n", iCur);
 
23730 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
23731 oputf("Reprepare operations: %d\n", iCur);
 
23732 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
23733 oputf("Number of times run: %d\n", iCur);
 
23734 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
23735 oputf("Memory used by prepared stmt: %d\n", iCur);
 
23736 }
23737
23738 #ifdef __linux__
23739 displayLinuxIoStats();
23740 #endif
23741
23742 /* Do not remove this machine readable comment: extra-stats-output-here */
23743
23744 return 0;
@@ -24130,21 +23557,21 @@
24130 #define BOX_1234 "\342\224\274" /* U+253c -|- */
24131
24132 /* Draw horizontal line N characters long using unicode box
24133 ** characters
24134 */
24135 static void print_box_line(int N){
24136 const char zDash[] =
24137 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
24138 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
24139 const int nDash = sizeof(zDash) - 1;
24140 N *= 3;
24141 while( N>nDash ){
24142 oputz(zDash);
24143 N -= nDash;
24144 }
24145 oputf("%.*s", N, zDash);
24146 }
24147
24148 /*
24149 ** Draw a horizontal separator for a MODE_Box table.
24150 */
@@ -24155,19 +23582,19 @@
24155 const char *zSep2,
24156 const char *zSep3
24157 ){
24158 int i;
24159 if( nArg>0 ){
24160 oputz(zSep1);
24161 print_box_line(p->actualWidth[0]+2);
24162 for(i=1; i<nArg; i++){
24163 oputz(zSep2);
24164 print_box_line(p->actualWidth[i]+2);
24165 }
24166 oputz(zSep3);
24167 }
24168 oputz("\n");
24169 }
24170
24171 /*
24172 ** z[] is a line of text that is to be displayed the .mode box or table or
24173 ** similar tabular formats. z[] might contain control characters such
@@ -24197,16 +23624,26 @@
24197 }
24198 if( mxWidth<0 ) mxWidth = -mxWidth;
24199 if( mxWidth==0 ) mxWidth = 1000000;
24200 i = j = n = 0;
24201 while( n<mxWidth ){
24202 if( z[i]>=' ' ){
 
 
 
 
 
 
 
 
 
24203 n++;
24204 do{ i++; j++; }while( (z[i]&0xc0)==0x80 );
 
24205 continue;
24206 }
24207 if( z[i]=='\t' ){
24208 do{
24209 n++;
24210 j++;
24211 }while( (n&7)!=0 && n<mxWidth );
24212 i++;
@@ -24244,13 +23681,21 @@
24244 }
24245 zOut = malloc( j+1 );
24246 shell_check_oom(zOut);
24247 i = j = n = 0;
24248 while( i<k ){
24249 if( z[i]>=' ' ){
 
 
 
 
 
 
 
 
24250 n++;
24251 do{ zOut[j++] = z[i++]; }while( (z[i]&0xc0)==0x80 );
24252 continue;
24253 }
24254 if( z[i]=='\t' ){
24255 do{
24256 n++;
@@ -24426,97 +23871,99 @@
24426 rowSep = "\n";
24427 if( p->showHeader ){
24428 for(i=0; i<nColumn; i++){
24429 w = p->actualWidth[i];
24430 if( p->colWidth[i]<0 ) w = -w;
24431 utf8_width_print(w, azData[i]);
24432 fputs(i==nColumn-1?"\n":" ", p->out);
24433 }
24434 for(i=0; i<nColumn; i++){
24435 print_dashes(p->actualWidth[i]);
24436 fputs(i==nColumn-1?"\n":" ", p->out);
24437 }
24438 }
24439 break;
24440 }
24441 case MODE_Table: {
24442 colSep = " | ";
24443 rowSep = " |\n";
24444 print_row_separator(p, nColumn, "+");
24445 fputs("| ", p->out);
24446 for(i=0; i<nColumn; i++){
24447 w = p->actualWidth[i];
24448 n = strlenChar(azData[i]);
24449 oputf("%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
24450 oputz(i==nColumn-1?" |\n":" | ");
 
24451 }
24452 print_row_separator(p, nColumn, "+");
24453 break;
24454 }
24455 case MODE_Markdown: {
24456 colSep = " | ";
24457 rowSep = " |\n";
24458 fputs("| ", p->out);
24459 for(i=0; i<nColumn; i++){
24460 w = p->actualWidth[i];
24461 n = strlenChar(azData[i]);
24462 oputf("%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
24463 oputz(i==nColumn-1?" |\n":" | ");
 
24464 }
24465 print_row_separator(p, nColumn, "|");
24466 break;
24467 }
24468 case MODE_Box: {
24469 colSep = " " BOX_13 " ";
24470 rowSep = " " BOX_13 "\n";
24471 print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
24472 oputz(BOX_13 " ");
24473 for(i=0; i<nColumn; i++){
24474 w = p->actualWidth[i];
24475 n = strlenChar(azData[i]);
24476 oputf("%*s%s%*s%s",
24477 (w-n)/2, "", azData[i], (w-n+1)/2, "",
24478 i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
24479 }
24480 print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
24481 break;
24482 }
24483 }
24484 for(i=nColumn, j=0; i<nTotal; i++, j++){
24485 if( j==0 && p->cMode!=MODE_Column ){
24486 oputz(p->cMode==MODE_Box?BOX_13" ":"| ");
24487 }
24488 z = azData[i];
24489 if( z==0 ) z = p->nullValue;
24490 w = p->actualWidth[j];
24491 if( p->colWidth[j]<0 ) w = -w;
24492 utf8_width_print(w, z);
24493 if( j==nColumn-1 ){
24494 oputz(rowSep);
24495 if( bMultiLineRowExists && abRowDiv[i/nColumn-1] && i+1<nTotal ){
24496 if( p->cMode==MODE_Table ){
24497 print_row_separator(p, nColumn, "+");
24498 }else if( p->cMode==MODE_Box ){
24499 print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
24500 }else if( p->cMode==MODE_Column ){
24501 oputz("\n");
24502 }
24503 }
24504 j = -1;
24505 if( seenInterrupt ) goto columnar_end;
24506 }else{
24507 oputz(colSep);
24508 }
24509 }
24510 if( p->cMode==MODE_Table ){
24511 print_row_separator(p, nColumn, "+");
24512 }else if( p->cMode==MODE_Box ){
24513 print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
24514 }
24515 columnar_end:
24516 if( seenInterrupt ){
24517 oputz("Interrupt\n");
24518 }
24519 nData = (nRow+1)*nColumn;
24520 for(i=0; i<nData; i++){
24521 z = azData[i];
24522 if( z!=zEmpty && z!=zShowNull ) free(azData[i]);
@@ -24599,11 +24046,13 @@
24599 }
24600 }
24601 } while( SQLITE_ROW == rc );
24602 sqlite3_free(pData);
24603 if( pArg->cMode==MODE_Json ){
24604 fputs("]\n", pArg->out);
 
 
24605 }else if( pArg->cMode==MODE_Count ){
24606 char zBuf[200];
24607 sqlite3_snprintf(sizeof(zBuf), zBuf, "%llu row%s\n",
24608 nRow, nRow!=1 ? "s" : "");
24609 printf("%s", zBuf);
@@ -24648,10 +24097,11 @@
24648 int bCancel,
24649 char **pzErr
24650 ){
24651 int rc = SQLITE_OK;
24652 sqlite3expert *p = pState->expert.pExpert;
 
24653 assert( p );
24654 assert( bCancel || pzErr==0 || *pzErr==0 );
24655 if( bCancel==0 ){
24656 int bVerbose = pState->expert.bVerbose;
24657
@@ -24660,24 +24110,25 @@
24660 int nQuery = sqlite3_expert_count(p);
24661 int i;
24662
24663 if( bVerbose ){
24664 const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
24665 oputz("-- Candidates -----------------------------\n");
24666 oputf("%s\n", zCand);
24667 }
24668 for(i=0; i<nQuery; i++){
24669 const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
24670 const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
24671 const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
24672 if( zIdx==0 ) zIdx = "(no new indexes)\n";
24673 if( bVerbose ){
24674 oputf("-- Query %d --------------------------------\n",i+1);
24675 oputf("%s\n\n", zSql);
 
 
24676 }
24677 oputf("%s\n", zIdx);
24678 oputf("%s\n", zEQP);
24679 }
24680 }
24681 }
24682 sqlite3_expert_destroy(p);
24683 pState->expert.pExpert = 0;
@@ -24708,30 +24159,31 @@
24708 if( n>=2 && 0==cli_strncmp(z, "-verbose", n) ){
24709 pState->expert.bVerbose = 1;
24710 }
24711 else if( n>=2 && 0==cli_strncmp(z, "-sample", n) ){
24712 if( i==(nArg-1) ){
24713 eputf("option requires an argument: %s\n", z);
24714 rc = SQLITE_ERROR;
24715 }else{
24716 iSample = (int)integerValue(azArg[++i]);
24717 if( iSample<0 || iSample>100 ){
24718 eputf("value out of range: %s\n", azArg[i]);
24719 rc = SQLITE_ERROR;
24720 }
24721 }
24722 }
24723 else{
24724 eputf("unknown option: %s\n", z);
24725 rc = SQLITE_ERROR;
24726 }
24727 }
24728
24729 if( rc==SQLITE_OK ){
24730 pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
24731 if( pState->expert.pExpert==0 ){
24732 eputf("sqlite3_expert_new: %s\n", zErr ? zErr : "out of memory");
 
24733 rc = SQLITE_ERROR;
24734 }else{
24735 sqlite3_expert_config(
24736 pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
24737 );
@@ -25056,33 +24508,33 @@
25056 if( zType==0 ) return 0;
25057 dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
25058 noSys = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
25059
25060 if( cli_strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
25061 if( !dataOnly ) oputz("DELETE FROM sqlite_sequence;\n");
25062 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
25063 if( !dataOnly ) oputz("ANALYZE sqlite_schema;\n");
25064 }else if( cli_strncmp(zTable, "sqlite_", 7)==0 ){
25065 return 0;
25066 }else if( dataOnly ){
25067 /* no-op */
25068 }else if( cli_strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
25069 char *zIns;
25070 if( !p->writableSchema ){
25071 oputz("PRAGMA writable_schema=ON;\n");
25072 p->writableSchema = 1;
25073 }
25074 zIns = sqlite3_mprintf(
25075 "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
25076 "VALUES('table','%q','%q',0,'%q');",
25077 zTable, zTable, zSql);
25078 shell_check_oom(zIns);
25079 oputf("%s\n", zIns);
25080 sqlite3_free(zIns);
25081 return 0;
25082 }else{
25083 printSchemaLine(zSql, ";\n");
25084 }
25085
25086 if( cli_strcmp(zType, "table")==0 ){
25087 ShellText sSelect;
25088 ShellText sTable;
@@ -25136,11 +24588,11 @@
25136 savedMode = p->mode;
25137 p->zDestTable = sTable.z;
25138 p->mode = p->cMode = MODE_Insert;
25139 rc = shell_exec(p, sSelect.z, 0);
25140 if( (rc&0xff)==SQLITE_CORRUPT ){
25141 oputz("/****** CORRUPTION ERROR *******/\n");
25142 toggleSelectOrder(p->db);
25143 shell_exec(p, sSelect.z, 0);
25144 toggleSelectOrder(p->db);
25145 }
25146 p->zDestTable = savedDestTable;
@@ -25167,22 +24619,22 @@
25167 char *zErr = 0;
25168 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
25169 if( rc==SQLITE_CORRUPT ){
25170 char *zQ2;
25171 int len = strlen30(zQuery);
25172 oputz("/****** CORRUPTION ERROR *******/\n");
25173 if( zErr ){
25174 oputf("/****** %s ******/\n", zErr);
25175 sqlite3_free(zErr);
25176 zErr = 0;
25177 }
25178 zQ2 = malloc( len+100 );
25179 if( zQ2==0 ) return rc;
25180 sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
25181 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
25182 if( rc ){
25183 oputf("/****** ERROR: %s ******/\n", zErr);
25184 }else{
25185 rc = SQLITE_CORRUPT;
25186 }
25187 sqlite3_free(zErr);
25188 free(zQ2);
@@ -25241,13 +24693,14 @@
25241 #ifndef SQLITE_SHELL_FIDDLE
25242 ".check GLOB Fail if output since .testcase does not match",
25243 ".clone NEWDB Clone data into NEWDB from the existing database",
25244 #endif
25245 ".connection [close] [#] Open or close an auxiliary database connection",
25246 #if defined(_WIN32) || defined(WIN32)
 
25247 ".crnl on|off Translate \\n to \\r\\n. Default ON",
25248 #endif
25249 ".databases List names and files of attached databases",
25250 ".dbconfig ?op? ?val? List or change sqlite3_db_config() options",
25251 #if SQLITE_SHELL_HAVE_RECOVER
25252 ".dbinfo ?DB? Show status information about the database",
25253 #endif
@@ -25349,13 +24802,15 @@
25349 #endif
25350 ".nullvalue STRING Use STRING in place of NULL values",
25351 #ifndef SQLITE_SHELL_FIDDLE
25352 ".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE",
25353 " If FILE begins with '|' then open as a pipe",
25354 " --bom Put a UTF8 byte-order mark at the beginning",
25355 " -e Send output to the system text editor",
25356 " -x Send output as CSV to a spreadsheet (same as \".excel\")",
 
 
25357 /* Note that .open is (partially) available in WASM builds but is
25358 ** currently only intended to be used by the fiddle tool, not
25359 ** end users, so is "undocumented." */
25360 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE",
25361 " Options:",
@@ -25374,10 +24829,12 @@
25374 ".output ?FILE? Send output to FILE or stdout if FILE is omitted",
25375 " If FILE begins with '|' then open it as a pipe.",
25376 " Options:",
25377 " --bom Prefix output with a UTF8 byte-order mark",
25378 " -e Send output to the system text editor",
 
 
25379 " -x Send output as CSV to a spreadsheet",
25380 #endif
25381 ".parameter CMD ... Manage SQL parameter bindings",
25382 " clear Erase all bindings",
25383 " init Initialize the TEMP table that holds bindings",
@@ -25487,10 +24944,14 @@
25487 ".vfsinfo ?AUX? Information about the top-level VFS",
25488 ".vfslist List all available VFSes",
25489 ".vfsname ?AUX? Print the name of the VFS stack",
25490 ".width NUM1 NUM2 ... Set minimum column widths for columnar output",
25491 " Negative values right-justify",
 
 
 
 
25492 };
25493
25494 /*
25495 ** Output help text.
25496 **
@@ -25535,24 +24996,24 @@
25535 hh &= ~HH_Summary;
25536 break;
25537 }
25538 if( ((hw^hh)&HH_Undoc)==0 ){
25539 if( (hh&HH_Summary)!=0 ){
25540 sputf(out, ".%s\n", azHelp[i]+1);
25541 ++n;
25542 }else if( (hw&HW_SummaryOnly)==0 ){
25543 sputf(out, "%s\n", azHelp[i]);
25544 }
25545 }
25546 }
25547 }else{
25548 /* Seek documented commands for which zPattern is an exact prefix */
25549 zPat = sqlite3_mprintf(".%s*", zPattern);
25550 shell_check_oom(zPat);
25551 for(i=0; i<ArraySize(azHelp); i++){
25552 if( sqlite3_strglob(zPat, azHelp[i])==0 ){
25553 sputf(out, "%s\n", azHelp[i]);
25554 j = i+1;
25555 n++;
25556 }
25557 }
25558 sqlite3_free(zPat);
@@ -25559,11 +25020,11 @@
25559 if( n ){
25560 if( n==1 ){
25561 /* when zPattern is a prefix of exactly one command, then include
25562 ** the details of that command, which should begin at offset j */
25563 while( j<ArraySize(azHelp)-1 && azHelp[j][0]==' ' ){
25564 sputf(out, "%s\n", azHelp[j]);
25565 j++;
25566 }
25567 }
25568 return n;
25569 }
@@ -25576,14 +25037,14 @@
25576 while( i<ArraySize(azHelp)-1 && azHelp[i+1][0]==' ' ) ++i;
25577 continue;
25578 }
25579 if( azHelp[i][0]=='.' ) j = i;
25580 if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
25581 sputf(out, "%s\n", azHelp[j]);
25582 while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]==' ' ){
25583 j++;
25584 sputf(out, "%s\n", azHelp[j]);
25585 }
25586 i = j;
25587 n++;
25588 }
25589 }
@@ -25609,35 +25070,35 @@
25609 **
25610 ** NULL is returned if any error is encountered. The final value of *pnByte
25611 ** is undefined in this case.
25612 */
25613 static char *readFile(const char *zName, int *pnByte){
25614 FILE *in = fopen(zName, "rb");
25615 long nIn;
25616 size_t nRead;
25617 char *pBuf;
25618 int rc;
25619 if( in==0 ) return 0;
25620 rc = fseek(in, 0, SEEK_END);
25621 if( rc!=0 ){
25622 eputf("Error: '%s' not seekable\n", zName);
25623 fclose(in);
25624 return 0;
25625 }
25626 nIn = ftell(in);
25627 rewind(in);
25628 pBuf = sqlite3_malloc64( nIn+1 );
25629 if( pBuf==0 ){
25630 eputz("Error: out of memory\n");
25631 fclose(in);
25632 return 0;
25633 }
25634 nRead = fread(pBuf, nIn, 1, in);
25635 fclose(in);
25636 if( nRead!=1 ){
25637 sqlite3_free(pBuf);
25638 eputf("Error: cannot read '%s'\n", zName);
25639 return 0;
25640 }
25641 pBuf[nIn] = 0;
25642 if( pnByte ) *pnByte = nIn;
25643 return pBuf;
@@ -25699,11 +25160,11 @@
25699 ** archive and the dfltZip flag is true, then assume it is a ZIP archive.
25700 ** Otherwise, assume an ordinary database regardless of the filename if
25701 ** the type cannot be determined from content.
25702 */
25703 int deduceDatabaseType(const char *zName, int dfltZip){
25704 FILE *f = fopen(zName, "rb");
25705 size_t n;
25706 int rc = SHELL_OPEN_UNSPEC;
25707 char zBuf[100];
25708 if( f==0 ){
25709 if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
@@ -25752,13 +25213,13 @@
25752 FILE *in;
25753 const char *zDbFilename = p->pAuxDb->zDbFilename;
25754 unsigned int x[16];
25755 char zLine[1000];
25756 if( zDbFilename ){
25757 in = fopen(zDbFilename, "r");
25758 if( in==0 ){
25759 eputf("cannot open \"%s\" for reading\n", zDbFilename);
25760 return 0;
25761 }
25762 nLine = 0;
25763 }else{
25764 in = p->in;
@@ -25765,24 +25226,24 @@
25765 nLine = p->lineno;
25766 if( in==0 ) in = stdin;
25767 }
25768 *pnData = 0;
25769 nLine++;
25770 if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
25771 rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
25772 if( rc!=2 ) goto readHexDb_error;
25773 if( n<0 ) goto readHexDb_error;
25774 if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
25775 n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */
25776 a = sqlite3_malloc( n ? n : 1 );
25777 shell_check_oom(a);
25778 memset(a, 0, n);
25779 if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
25780 eputz("invalid pagesize\n");
25781 goto readHexDb_error;
25782 }
25783 for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
25784 rc = sscanf(zLine, "| page %d offset %d", &j, &k);
25785 if( rc==2 ){
25786 iOffset = k;
25787 continue;
25788 }
@@ -25810,18 +25271,18 @@
25810
25811 readHexDb_error:
25812 if( in!=p->in ){
25813 fclose(in);
25814 }else{
25815 while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
25816 nLine++;
25817 if(cli_strncmp(zLine, "| end ", 6)==0 ) break;
25818 }
25819 p->lineno = nLine;
25820 }
25821 sqlite3_free(a);
25822 eputf("Error on line %d of --hexdb input\n", nLine);
25823 return 0;
25824 }
25825 #endif /* SQLITE_OMIT_DESERIALIZE */
25826
25827 /*
@@ -25892,22 +25353,24 @@
25892 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
25893 break;
25894 }
25895 }
25896 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
25897 eputf("Error: unable to open database \"%s\": %s\n",
25898 zDbFilename, sqlite3_errmsg(p->db));
25899 if( (openFlags & OPEN_DB_KEEPALIVE)==0 ){
25900 exit(1);
25901 }
25902 sqlite3_close(p->db);
25903 sqlite3_open(":memory:", &p->db);
25904 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
25905 eputz("Also: unable to open substitute in-memory database.\n");
 
25906 exit(1);
25907 }else{
25908 eputf("Notice: using substitute in-memory database instead of \"%s\"\n",
 
25909 zDbFilename);
25910 }
25911 }
25912 globalDb = p->db;
25913 sqlite3_db_config(p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, (int)0, (int*)0);
@@ -26015,11 +25478,11 @@
26015 }
26016 rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
26017 SQLITE_DESERIALIZE_RESIZEABLE |
26018 SQLITE_DESERIALIZE_FREEONCLOSE);
26019 if( rc ){
26020 eputf("Error: sqlite3_deserialize() returns %d\n", rc);
26021 }
26022 if( p->szMax>0 ){
26023 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
26024 }
26025 }
@@ -26039,11 +25502,12 @@
26039 ** Attempt to close the database connection. Report errors.
26040 */
26041 void close_db(sqlite3 *db){
26042 int rc = sqlite3_close(db);
26043 if( rc ){
26044 eputf("Error: sqlite3_close() returns %d: %s\n", rc, sqlite3_errmsg(db));
 
26045 }
26046 }
26047
26048 #if HAVE_READLINE || HAVE_EDITLINE
26049 /*
@@ -26203,11 +25667,12 @@
26203 return 1;
26204 }
26205 if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
26206 return 0;
26207 }
26208 eputf("ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", zArg);
 
26209 return 0;
26210 }
26211
26212 /*
26213 ** Set or clear a shell flag according to a boolean value.
@@ -26239,13 +25704,13 @@
26239 }else if( cli_strcmp(zFile, "stderr")==0 ){
26240 f = stderr;
26241 }else if( cli_strcmp(zFile, "off")==0 ){
26242 f = 0;
26243 }else{
26244 f = fopen(zFile, bTextMode ? "w" : "wb");
26245 if( f==0 ){
26246 eputf("Error: cannot open \"%s\"\n", zFile);
26247 }
26248 }
26249 return f;
26250 }
26251
@@ -26294,16 +25759,17 @@
26294 if( nSql>1000000000 ) nSql = 1000000000;
26295 while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
26296 switch( mType ){
26297 case SQLITE_TRACE_ROW:
26298 case SQLITE_TRACE_STMT: {
26299 sputf(p->traceOut, "%.*s;\n", (int)nSql, zSql);
26300 break;
26301 }
26302 case SQLITE_TRACE_PROFILE: {
26303 sqlite3_int64 nNanosec = pX ? *(sqlite3_int64*)pX : 0;
26304 sputf(p->traceOut, "%.*s; -- %lld ns\n", (int)nSql, zSql, nNanosec);
 
26305 break;
26306 }
26307 }
26308 return 0;
26309 }
@@ -26406,14 +25872,15 @@
26406 do{ p->n--; }while( p->z[p->n]!=cQuote );
26407 p->cTerm = c;
26408 break;
26409 }
26410 if( pc==cQuote && c!='\r' ){
26411 eputf("%s:%d: unescaped %c character\n", p->zFile, p->nLine, cQuote);
 
26412 }
26413 if( c==EOF ){
26414 eputf("%s:%d: unterminated %c-quoted field\n",
26415 p->zFile, startLine, cQuote);
26416 p->cTerm = c;
26417 break;
26418 }
26419 import_append_char(p, c);
@@ -26508,11 +25975,11 @@
26508
26509 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
26510 shell_check_oom(zQuery);
26511 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
26512 if( rc ){
26513 eputf("Error %d: %s on [%s]\n",
26514 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zQuery);
26515 goto end_data_xfer;
26516 }
26517 n = sqlite3_column_count(pQuery);
26518 zInsert = sqlite3_malloc64(200 + nTable + n*3);
@@ -26525,11 +25992,11 @@
26525 i += 2;
26526 }
26527 memcpy(zInsert+i, ");", 3);
26528 rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
26529 if( rc ){
26530 eputf("Error %d: %s on [%s]\n",
26531 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb), zInsert);
26532 goto end_data_xfer;
26533 }
26534 for(k=0; k<2; k++){
26535 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
@@ -26561,11 +26028,11 @@
26561 }
26562 }
26563 } /* End for */
26564 rc = sqlite3_step(pInsert);
26565 if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
26566 eputf("Error %d: %s\n",
26567 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb));
26568 }
26569 sqlite3_reset(pInsert);
26570 cnt++;
26571 if( (cnt%spinRate)==0 ){
@@ -26579,11 +26046,11 @@
26579 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
26580 zTable);
26581 shell_check_oom(zQuery);
26582 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
26583 if( rc ){
26584 eputf("Warning: cannot step \"%s\" backwards", zTable);
26585 break;
26586 }
26587 } /* End for(k=0...) */
26588
26589 end_data_xfer:
@@ -26616,23 +26083,24 @@
26616 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
26617 " WHERE %s ORDER BY rowid ASC", zWhere);
26618 shell_check_oom(zQuery);
26619 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
26620 if( rc ){
26621 eputf("Error: (%d) %s on [%s]\n", sqlite3_extended_errcode(p->db),
 
26622 sqlite3_errmsg(p->db), zQuery);
26623 goto end_schema_xfer;
26624 }
26625 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
26626 zName = sqlite3_column_text(pQuery, 0);
26627 zSql = sqlite3_column_text(pQuery, 1);
26628 if( zName==0 || zSql==0 ) continue;
26629 if( sqlite3_stricmp((char*)zName, "sqlite_sequence")!=0 ){
26630 sputf(stdout, "%s... ", zName); fflush(stdout);
26631 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
26632 if( zErrMsg ){
26633 eputf("Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
26634 sqlite3_free(zErrMsg);
26635 zErrMsg = 0;
26636 }
26637 }
26638 if( xForEach ){
@@ -26646,23 +26114,23 @@
26646 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
26647 " WHERE %s ORDER BY rowid DESC", zWhere);
26648 shell_check_oom(zQuery);
26649 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
26650 if( rc ){
26651 eputf("Error: (%d) %s on [%s]\n",
26652 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zQuery);
26653 goto end_schema_xfer;
26654 }
26655 while( sqlite3_step(pQuery)==SQLITE_ROW ){
26656 zName = sqlite3_column_text(pQuery, 0);
26657 zSql = sqlite3_column_text(pQuery, 1);
26658 if( zName==0 || zSql==0 ) continue;
26659 if( sqlite3_stricmp((char*)zName, "sqlite_sequence")==0 ) continue;
26660 sputf(stdout, "%s... ", zName); fflush(stdout);
26661 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
26662 if( zErrMsg ){
26663 eputf("Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
26664 sqlite3_free(zErrMsg);
26665 zErrMsg = 0;
26666 }
26667 if( xForEach ){
26668 xForEach(p, newDb, (const char*)zName);
@@ -26682,16 +26150,17 @@
26682 */
26683 static void tryToClone(ShellState *p, const char *zNewDb){
26684 int rc;
26685 sqlite3 *newDb = 0;
26686 if( access(zNewDb,0)==0 ){
26687 eputf("File \"%s\" already exists.\n", zNewDb);
26688 return;
26689 }
26690 rc = sqlite3_open(zNewDb, &newDb);
26691 if( rc ){
26692 eputf("Cannot create output database: %s\n", sqlite3_errmsg(newDb));
 
26693 }else{
26694 sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
26695 sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
26696 tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
26697 tryToCloneSchema(p, newDb, "type!='table'", 0);
@@ -26704,14 +26173,21 @@
26704 #ifndef SQLITE_SHELL_FIDDLE
26705 /*
26706 ** Change the output stream (file or pipe or console) to something else.
26707 */
26708 static void output_redir(ShellState *p, FILE *pfNew){
26709 if( p->out != stdout ) eputz("Output already redirected.\n");
26710 else{
 
26711 p->out = pfNew;
26712 setOutputStream(pfNew);
 
 
 
 
 
 
26713 }
26714 }
26715
26716 /*
26717 ** Change the output file back to stdout.
@@ -26724,10 +26200,13 @@
26724 if( p->outfile[0]=='|' ){
26725 #ifndef SQLITE_OMIT_POPEN
26726 pclose(p->out);
26727 #endif
26728 }else{
 
 
 
26729 output_file_close(p->out);
26730 #ifndef SQLITE_NOHAVE_SYSTEM
26731 if( p->doXdgOpen ){
26732 const char *zXdgOpenCmd =
26733 #if defined(_WIN32)
@@ -26738,11 +26217,11 @@
26738 "xdg-open";
26739 #endif
26740 char *zCmd;
26741 zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
26742 if( system(zCmd) ){
26743 eputf("Failed: [%s]\n", zCmd);
26744 }else{
26745 /* Give the start/open/xdg-open command some time to get
26746 ** going before we continue, and potential delete the
26747 ** p->zTempFile data file out from under it */
26748 sqlite3_sleep(2000);
@@ -26753,11 +26232,10 @@
26753 }
26754 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
26755 }
26756 p->outfile[0] = 0;
26757 p->out = stdout;
26758 setOutputStream(stdout);
26759 }
26760 #else
26761 # define output_redir(SS,pfO)
26762 # define output_reset(SS)
26763 #endif
@@ -26829,11 +26307,11 @@
26829 if( p->db==0 ) return 1;
26830 rc = sqlite3_prepare_v2(p->db,
26831 "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
26832 -1, &pStmt, 0);
26833 if( rc ){
26834 eputf("error: %s\n", sqlite3_errmsg(p->db));
26835 sqlite3_finalize(pStmt);
26836 return 1;
26837 }
26838 sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
26839 if( sqlite3_step(pStmt)==SQLITE_ROW
@@ -26842,32 +26320,32 @@
26842 const u8 *pb = sqlite3_column_blob(pStmt,0);
26843 shell_check_oom(pb);
26844 memcpy(aHdr, pb, 100);
26845 sqlite3_finalize(pStmt);
26846 }else{
26847 eputz("unable to read database header\n");
26848 sqlite3_finalize(pStmt);
26849 return 1;
26850 }
26851 i = get2byteInt(aHdr+16);
26852 if( i==1 ) i = 65536;
26853 oputf("%-20s %d\n", "database page size:", i);
26854 oputf("%-20s %d\n", "write format:", aHdr[18]);
26855 oputf("%-20s %d\n", "read format:", aHdr[19]);
26856 oputf("%-20s %d\n", "reserved bytes:", aHdr[20]);
26857 for(i=0; i<ArraySize(aField); i++){
26858 int ofst = aField[i].ofst;
26859 unsigned int val = get4byteInt(aHdr + ofst);
26860 oputf("%-20s %u", aField[i].zName, val);
26861 switch( ofst ){
26862 case 56: {
26863 if( val==1 ) oputz(" (utf8)");
26864 if( val==2 ) oputz(" (utf16le)");
26865 if( val==3 ) oputz(" (utf16be)");
26866 }
26867 }
26868 oputz("\n");
26869 }
26870 if( zDb==0 ){
26871 zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
26872 }else if( cli_strcmp(zDb,"temp")==0 ){
26873 zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
@@ -26876,24 +26354,24 @@
26876 }
26877 for(i=0; i<ArraySize(aQuery); i++){
26878 char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
26879 int val = db_int(p->db, zSql);
26880 sqlite3_free(zSql);
26881 oputf("%-20s %d\n", aQuery[i].zName, val);
26882 }
26883 sqlite3_free(zSchemaTab);
26884 sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
26885 oputf("%-20s %u\n", "data version", iDataVersion);
26886 return 0;
26887 }
26888 #endif /* SQLITE_SHELL_HAVE_RECOVER */
26889
26890 /*
26891 ** Print the given string as an error message.
26892 */
26893 static void shellEmitError(const char *zErr){
26894 eputf("Error: %s\n", zErr);
26895 }
26896 /*
26897 ** Print the current sqlite3_errmsg() value to stderr and return 1.
26898 */
26899 static int shellDatabaseError(sqlite3 *db){
@@ -27136,10 +26614,11 @@
27136 int bGroupByParent = 0; /* If -groupbyparent is present */
27137 int i; /* To iterate through azArg[] */
27138 const char *zIndent = ""; /* How much to indent CREATE INDEX by */
27139 int rc; /* Return code */
27140 sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */
 
27141
27142 /*
27143 ** This SELECT statement returns one row for each foreign key constraint
27144 ** in the schema of the main database. The column values are:
27145 **
@@ -27211,11 +26690,12 @@
27211 else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
27212 bGroupByParent = 1;
27213 zIndent = " ";
27214 }
27215 else{
27216 eputf("Usage: %s %s ?-verbose? ?-groupbyparent?\n", azArg[0], azArg[1]);
 
27217 return SQLITE_ERROR;
27218 }
27219 }
27220
27221 /* Register the fkey_collate_clause() SQL function */
@@ -27255,44 +26735,45 @@
27255 }
27256 rc = sqlite3_finalize(pExplain);
27257 if( rc!=SQLITE_OK ) break;
27258
27259 if( res<0 ){
27260 eputz("Error: internal error");
27261 break;
27262 }else{
27263 if( bGroupByParent
27264 && (bVerbose || res==0)
27265 && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
27266 ){
27267 oputf("-- Parent table %s\n", zParent);
27268 sqlite3_free(zPrev);
27269 zPrev = sqlite3_mprintf("%s", zParent);
27270 }
27271
27272 if( res==0 ){
27273 oputf("%s%s --> %s\n", zIndent, zCI, zTarget);
27274 }else if( bVerbose ){
27275 oputf("%s/* no extra indexes required for %s -> %s */\n",
 
27276 zIndent, zFrom, zTarget
27277 );
27278 }
27279 }
27280 }
27281 sqlite3_free(zPrev);
27282
27283 if( rc!=SQLITE_OK ){
27284 eputf("%s\n", sqlite3_errmsg(db));
27285 }
27286
27287 rc2 = sqlite3_finalize(pSql);
27288 if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
27289 rc = rc2;
27290 eputf("%s\n", sqlite3_errmsg(db));
27291 }
27292 }else{
27293 eputf("%s\n", sqlite3_errmsg(db));
27294 }
27295
27296 return rc;
27297 }
27298
@@ -27308,13 +26789,13 @@
27308 n = (nArg>=2 ? strlen30(azArg[1]) : 0);
27309 if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
27310 return lintFkeyIndexes(pState, azArg, nArg);
27311
27312 usage:
27313 eputf("Usage %s sub-command ?switches...?\n", azArg[0]);
27314 eputz("Where sub-commands are:\n");
27315 eputz(" fkey-indexes\n");
27316 return SQLITE_ERROR;
27317 }
27318
27319 static void shellPrepare(
27320 sqlite3 *db,
@@ -27324,11 +26805,12 @@
27324 ){
27325 *ppStmt = 0;
27326 if( *pRc==SQLITE_OK ){
27327 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
27328 if( rc!=SQLITE_OK ){
27329 eputf("sql error: %s (%d)\n", sqlite3_errmsg(db), sqlite3_errcode(db));
 
27330 *pRc = rc;
27331 }
27332 }
27333 }
27334
@@ -27368,11 +26850,11 @@
27368 if( pStmt ){
27369 sqlite3 *db = sqlite3_db_handle(pStmt);
27370 int rc = sqlite3_finalize(pStmt);
27371 if( *pRc==SQLITE_OK ){
27372 if( rc!=SQLITE_OK ){
27373 eputf("SQL error: %s\n", sqlite3_errmsg(db));
27374 }
27375 *pRc = rc;
27376 }
27377 }
27378 }
@@ -27390,11 +26872,11 @@
27390 ){
27391 int rc = sqlite3_reset(pStmt);
27392 if( *pRc==SQLITE_OK ){
27393 if( rc!=SQLITE_OK ){
27394 sqlite3 *db = sqlite3_db_handle(pStmt);
27395 eputf("SQL error: %s\n", sqlite3_errmsg(db));
27396 }
27397 *pRc = rc;
27398 }
27399 }
27400 #endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
@@ -27419,10 +26901,11 @@
27419 char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */
27420 const char *zFile; /* --file argument, or NULL */
27421 const char *zDir; /* --directory argument, or NULL */
27422 char **azArg; /* Array of command arguments */
27423 ShellState *p; /* Shell state */
 
27424 sqlite3 *db; /* Database containing the archive */
27425 };
27426
27427 /*
27428 ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
@@ -27442,13 +26925,13 @@
27442 va_start(ap, zFmt);
27443 z = sqlite3_vmprintf(zFmt, ap);
27444 va_end(ap);
27445 shellEmitError(z);
27446 if( pAr->fromCmdLine ){
27447 eputz("Use \"-A\" for more help\n");
27448 }else{
27449 eputz("Use \".archive --help\" for more help\n");
27450 }
27451 sqlite3_free(z);
27452 return SQLITE_ERROR;
27453 }
27454
@@ -27544,11 +27027,11 @@
27544 };
27545 int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
27546 struct ArSwitch *pEnd = &aSwitch[nSwitch];
27547
27548 if( nArg<=1 ){
27549 eputz("Wrong number of arguments. Usage:\n");
27550 return arUsage(stderr);
27551 }else{
27552 char *z = azArg[1];
27553 if( z[0]!='-' ){
27554 /* Traditional style [tar] invocation */
@@ -27650,11 +27133,11 @@
27650 }
27651 }
27652 }
27653 }
27654 if( pAr->eCmd==0 ){
27655 eputz("Required argument missing. Usage:\n");
27656 return arUsage(stderr);
27657 }
27658 return SQLITE_OK;
27659 }
27660
@@ -27693,11 +27176,11 @@
27693 if( SQLITE_ROW==sqlite3_step(pTest) ){
27694 bOk = 1;
27695 }
27696 shellReset(&rc, pTest);
27697 if( rc==SQLITE_OK && bOk==0 ){
27698 eputf("not found in archive: %s\n", z);
27699 rc = SQLITE_ERROR;
27700 }
27701 }
27702 shellFinalize(&rc, pTest);
27703 }
@@ -27760,19 +27243,19 @@
27760 arWhereClause(&rc, pAr, &zWhere);
27761
27762 shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
27763 pAr->zSrcTable, zWhere);
27764 if( pAr->bDryRun ){
27765 oputf("%s\n", sqlite3_sql(pSql));
27766 }else{
27767 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
27768 if( pAr->bVerbose ){
27769 oputf("%s % 10d %s %s\n",
27770 sqlite3_column_text(pSql, 0), sqlite3_column_int(pSql, 1),
27771 sqlite3_column_text(pSql, 2),sqlite3_column_text(pSql, 3));
27772 }else{
27773 oputf("%s\n", sqlite3_column_text(pSql, 0));
27774 }
27775 }
27776 }
27777 shellFinalize(&rc, pSql);
27778 sqlite3_free(zWhere);
@@ -27795,11 +27278,11 @@
27795 }
27796 if( rc==SQLITE_OK ){
27797 zSql = sqlite3_mprintf("DELETE FROM %s WHERE %s;",
27798 pAr->zSrcTable, zWhere);
27799 if( pAr->bDryRun ){
27800 oputf("%s\n", zSql);
27801 }else{
27802 char *zErr = 0;
27803 rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0);
27804 if( rc==SQLITE_OK ){
27805 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
@@ -27808,11 +27291,11 @@
27808 }else{
27809 rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0);
27810 }
27811 }
27812 if( zErr ){
27813 sputf(stdout, "ERROR: %s\n", zErr); /* stdout? */
27814 sqlite3_free(zErr);
27815 }
27816 }
27817 }
27818 sqlite3_free(zWhere);
@@ -27872,15 +27355,15 @@
27872 ** populating them changes the timestamp). */
27873 for(i=0; i<2; i++){
27874 j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
27875 sqlite3_bind_int(pSql, j, i);
27876 if( pAr->bDryRun ){
27877 oputf("%s\n", sqlite3_sql(pSql));
27878 }else{
27879 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
27880 if( i==0 && pAr->bVerbose ){
27881 oputf("%s\n", sqlite3_column_text(pSql, 0));
27882 }
27883 }
27884 }
27885 shellReset(&rc, pSql);
27886 }
@@ -27896,17 +27379,17 @@
27896 ** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out.
27897 */
27898 static int arExecSql(ArCommand *pAr, const char *zSql){
27899 int rc;
27900 if( pAr->bDryRun ){
27901 oputf("%s\n", zSql);
27902 rc = SQLITE_OK;
27903 }else{
27904 char *zErr = 0;
27905 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
27906 if( zErr ){
27907 sputf(stdout, "ERROR: %s\n", zErr);
27908 sqlite3_free(zErr);
27909 }
27910 }
27911 return rc;
27912 }
@@ -28051,10 +27534,11 @@
28051 cmd.fromCmdLine = fromCmdLine;
28052 rc = arParseCommand(azArg, nArg, &cmd);
28053 if( rc==SQLITE_OK ){
28054 int eDbType = SHELL_OPEN_UNSPEC;
28055 cmd.p = pState;
 
28056 cmd.db = pState->db;
28057 if( cmd.zFile ){
28058 eDbType = deduceDatabaseType(cmd.zFile, 1);
28059 }else{
28060 eDbType = pState->openMode;
@@ -28077,17 +27561,18 @@
28077 }else{
28078 flags = SQLITE_OPEN_READONLY;
28079 }
28080 cmd.db = 0;
28081 if( cmd.bDryRun ){
28082 oputf("-- open database '%s'%s\n", cmd.zFile,
28083 eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
28084 }
28085 rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
28086 eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
28087 if( rc!=SQLITE_OK ){
28088 eputf("cannot open file: %s (%s)\n", cmd.zFile, sqlite3_errmsg(cmd.db));
 
28089 goto end_ar_command;
28090 }
28091 sqlite3_fileio_init(cmd.db, 0, 0);
28092 sqlite3_sqlar_init(cmd.db, 0, 0);
28093 sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
@@ -28096,11 +27581,11 @@
28096 }
28097 if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
28098 if( cmd.eCmd!=AR_CMD_CREATE
28099 && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
28100 ){
28101 eputz("database does not contain an 'sqlar' table\n");
28102 rc = SQLITE_ERROR;
28103 goto end_ar_command;
28104 }
28105 cmd.zSrcTable = sqlite3_mprintf("sqlar");
28106 }
@@ -28154,11 +27639,11 @@
28154 ** This function is used as a callback by the recover extension. Simply
28155 ** print the supplied SQL statement to stdout.
28156 */
28157 static int recoverSqlCb(void *pCtx, const char *zSql){
28158 ShellState *pState = (ShellState*)pCtx;
28159 sputf(pState->out, "%s;\n", zSql);
28160 return SQLITE_OK;
28161 }
28162
28163 /*
28164 ** This function is called to recover data from the database. A script
@@ -28197,11 +27682,11 @@
28197 }else
28198 if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
28199 bRowids = 0;
28200 }
28201 else{
28202 eputf("unexpected option: %s\n", azArg[i]);
28203 showHelp(pState->out, azArg[0]);
28204 return 1;
28205 }
28206 }
28207
@@ -28216,11 +27701,11 @@
28216
28217 sqlite3_recover_run(p);
28218 if( sqlite3_recover_errcode(p)!=SQLITE_OK ){
28219 const char *zErr = sqlite3_recover_errmsg(p);
28220 int errCode = sqlite3_recover_errcode(p);
28221 eputf("sql error: %s (%d)\n", zErr, errCode);
28222 }
28223 rc = sqlite3_recover_finish(p);
28224 return rc;
28225 }
28226 #endif /* SQLITE_SHELL_HAVE_RECOVER */
@@ -28238,25 +27723,25 @@
28238 i64 nError = 0;
28239 const char *zErr = 0;
28240 while( SQLITE_OK==sqlite3_intck_step(p) ){
28241 const char *zMsg = sqlite3_intck_message(p);
28242 if( zMsg ){
28243 oputf("%s\n", zMsg);
28244 nError++;
28245 }
28246 nStep++;
28247 if( nStepPerUnlock && (nStep % nStepPerUnlock)==0 ){
28248 sqlite3_intck_unlock(p);
28249 }
28250 }
28251 rc = sqlite3_intck_error(p, &zErr);
28252 if( zErr ){
28253 eputf("%s\n", zErr);
28254 }
28255 sqlite3_intck_close(p);
28256
28257 oputf("%lld steps, %lld errors\n", nStep, nError);
28258 }
28259
28260 return rc;
28261 }
28262
@@ -28275,11 +27760,11 @@
28275 */
28276 #ifdef SHELL_DEBUG
28277 #define rc_err_oom_die(rc) \
28278 if( rc==SQLITE_NOMEM ) shell_check_oom(0); \
28279 else if(!(rc==SQLITE_OK||rc==SQLITE_DONE)) \
28280 eputf("E:%d\n",rc), assert(0)
28281 #else
28282 static void rc_err_oom_die(int rc){
28283 if( rc==SQLITE_NOMEM ) shell_check_oom(0);
28284 assert(rc==SQLITE_OK||rc==SQLITE_DONE);
28285 }
@@ -28492,12 +27977,13 @@
28492 shellPreparePrintf(p->db, &rc, &pStmt,
28493 "SELECT 1 FROM sqlite_schema o WHERE "
28494 "sql LIKE 'CREATE VIRTUAL TABLE%%' AND %s", zLike ? zLike : "true"
28495 );
28496 if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
28497 oputz("/* WARNING: "
28498 "Script requires that SQLITE_DBCONFIG_DEFENSIVE be disabled */\n"
 
28499 );
28500 }
28501 shellFinalize(&rc, pStmt);
28502 return rc;
28503 }
@@ -28524,16 +28010,18 @@
28524 return SQLITE_OK;
28525 }
28526 if( faultsim_state.iCnt ){
28527 if( faultsim_state.iCnt>0 ) faultsim_state.iCnt--;
28528 if( faultsim_state.eVerbose>=2 ){
28529 oputf("FAULT-SIM id=%d no-fault (cnt=%d)\n", iArg, faultsim_state.iCnt);
 
28530 }
28531 return SQLITE_OK;
28532 }
28533 if( faultsim_state.eVerbose>=1 ){
28534 oputf("FAULT-SIM id=%d returns %d\n", iArg, faultsim_state.iErr);
 
28535 }
28536 faultsim_state.iCnt = faultsim_state.iInterval;
28537 faultsim_state.nHit++;
28538 if( faultsim_state.nRepeat>0 && faultsim_state.nRepeat<=faultsim_state.nHit ){
28539 faultsim_state.iCnt = -1;
@@ -28592,11 +28080,11 @@
28592 clearTempFile(p);
28593
28594 #ifndef SQLITE_OMIT_AUTHORIZATION
28595 if( c=='a' && cli_strncmp(azArg[0], "auth", n)==0 ){
28596 if( nArg!=2 ){
28597 eputz("Usage: .auth ON|OFF\n");
28598 rc = 1;
28599 goto meta_command_exit;
28600 }
28601 open_db(p, 0);
28602 if( booleanValue(azArg[1]) ){
@@ -28639,32 +28127,32 @@
28639 }else
28640 if( cli_strcmp(z, "-async")==0 ){
28641 bAsync = 1;
28642 }else
28643 {
28644 eputf("unknown option: %s\n", azArg[j]);
28645 return 1;
28646 }
28647 }else if( zDestFile==0 ){
28648 zDestFile = azArg[j];
28649 }else if( zDb==0 ){
28650 zDb = zDestFile;
28651 zDestFile = azArg[j];
28652 }else{
28653 eputz("Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
28654 return 1;
28655 }
28656 }
28657 if( zDestFile==0 ){
28658 eputz("missing FILENAME argument on .backup\n");
28659 return 1;
28660 }
28661 if( zDb==0 ) zDb = "main";
28662 rc = sqlite3_open_v2(zDestFile, &pDest,
28663 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
28664 if( rc!=SQLITE_OK ){
28665 eputf("Error: cannot open \"%s\"\n", zDestFile);
28666 close_db(pDest);
28667 return 1;
28668 }
28669 if( bAsync ){
28670 sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
@@ -28698,21 +28186,12 @@
28698 }
28699 }else
28700
28701 /* Undocumented. Legacy only. See "crnl" below */
28702 if( c=='b' && n>=3 && cli_strncmp(azArg[0], "binary", n)==0 ){
28703 if( nArg==2 ){
28704 if( booleanValue(azArg[1]) ){
28705 setBinaryMode(p->out, 1);
28706 }else{
28707 setTextMode(p->out, 1);
28708 }
28709 }else{
28710 eputz("The \".binary\" command is deprecated. Use \".crnl\" instead.\n"
28711 "Usage: .binary on|off\n");
28712 rc = 1;
28713 }
28714 }else
28715
28716 /* The undocumented ".breakpoint" command causes a call to the no-op
28717 ** routine named test_breakpoint().
28718 */
@@ -28730,11 +28209,11 @@
28730 sqlite3_free(z);
28731 #else
28732 rc = chdir(azArg[1]);
28733 #endif
28734 if( rc ){
28735 eputf("Cannot change to directory \"%s\"\n", azArg[1]);
28736 rc = 1;
28737 }
28738 }else{
28739 eputz("Usage: .cd DIRECTORY\n");
28740 rc = 1;
@@ -28763,15 +28242,16 @@
28763 eputz("Usage: .check GLOB-PATTERN\n");
28764 rc = 2;
28765 }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
28766 rc = 2;
28767 }else if( testcase_glob(azArg[1],zRes)==0 ){
28768 eputf("testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
 
28769 p->zTestcase, azArg[1], zRes);
28770 rc = 1;
28771 }else{
28772 oputf("testcase-%s ok\n", p->zTestcase);
28773 p->nCheck++;
28774 }
28775 sqlite3_free(zRes);
28776 }else
28777 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
@@ -28800,13 +28280,13 @@
28800 zFile = "(memory)";
28801 }else if( zFile[0]==0 ){
28802 zFile = "(temporary-file)";
28803 }
28804 if( p->pAuxDb == &p->aAuxDb[i] ){
28805 sputf(stdout, "ACTIVE %d: %s\n", i, zFile);
28806 }else if( p->aAuxDb[i].db!=0 ){
28807 sputf(stdout, " %d: %s\n", i, zFile);
28808 }
28809 }
28810 }else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){
28811 int i = azArg[1][0] - '0';
28812 if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && i<ArraySize(p->aAuxDb) ){
@@ -28833,23 +28313,25 @@
28833 rc = 1;
28834 }
28835 }else
28836
28837 if( c=='c' && n==4 && cli_strncmp(azArg[0], "crnl", n)==0 ){
 
 
 
 
28838 if( nArg==2 ){
28839 if( booleanValue(azArg[1]) ){
28840 setTextMode(p->out, 1);
28841 }else{
28842 setBinaryMode(p->out, 1);
28843 }
28844 }else{
28845 #if !defined(_WIN32) && !defined(WIN32)
28846 eputz("The \".crnl\" is a no-op on non-Windows machines.\n");
28847 #endif
28848 eputz("Usage: .crnl on|off\n");
28849 rc = 1;
28850 }
 
28851 }else
28852
28853 if( c=='d' && n>1 && cli_strncmp(azArg[0], "databases", n)==0 ){
28854 char **azName = 0;
28855 int nName = 0;
@@ -28875,11 +28357,11 @@
28875 sqlite3_finalize(pStmt);
28876 for(i=0; i<nName; i++){
28877 int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
28878 int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
28879 const char *z = azName[i*2+1];
28880 oputf("%s: %s %s%s\n",
28881 azName[i*2], z && z[0] ? z : "\"\"", bRdonly ? "r/o" : "r/w",
28882 eTxn==SQLITE_TXN_NONE ? "" :
28883 eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
28884 free(azName[i*2]);
28885 free(azName[i*2+1]);
@@ -28917,15 +28399,16 @@
28917 if( nArg>1 && cli_strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
28918 if( nArg>=3 ){
28919 sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
28920 }
28921 sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
28922 oputf("%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
 
28923 if( nArg>1 ) break;
28924 }
28925 if( nArg>1 && ii==ArraySize(aDbConfig) ){
28926 eputf("Error: unknown dbconfig \"%s\"\n", azArg[1]);
28927 eputz("Enter \".dbconfig\" with no arguments for a list\n");
28928 }
28929 }else
28930
28931 #if SQLITE_SHELL_HAVE_RECOVER
@@ -28971,11 +28454,12 @@
28971 }else
28972 if( cli_strcmp(z,"nosys")==0 ){
28973 ShellSetFlag(p, SHFLG_DumpNoSys);
28974 }else
28975 {
28976 eputf("Unknown option \"%s\" on \".dump\"\n", azArg[i]);
 
28977 rc = 1;
28978 sqlite3_free(zLike);
28979 goto meta_command_exit;
28980 }
28981 }else{
@@ -29006,12 +28490,12 @@
29006 outputDumpWarning(p, zLike);
29007 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
29008 /* When playing back a "dump", the content might appear in an order
29009 ** which causes immediate foreign key constraints to be violated.
29010 ** So disable foreign-key constraint enforcement to prevent problems. */
29011 oputz("PRAGMA foreign_keys=OFF;\n");
29012 oputz("BEGIN TRANSACTION;\n");
29013 }
29014 p->writableSchema = 0;
29015 p->showHeader = 0;
29016 /* Set writable_schema=ON since doing so forces SQLite to initialize
29017 ** as much of the schema as it can even if the sqlite_schema table is
@@ -29039,17 +28523,17 @@
29039 run_table_dump_query(p, zSql);
29040 sqlite3_free(zSql);
29041 }
29042 sqlite3_free(zLike);
29043 if( p->writableSchema ){
29044 oputz("PRAGMA writable_schema=OFF;\n");
29045 p->writableSchema = 0;
29046 }
29047 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
29048 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
29049 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
29050 oputz(p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
29051 }
29052 p->showHeader = savedShowHeader;
29053 p->shellFlgs = savedShellFlags;
29054 }else
29055
@@ -29125,11 +28609,12 @@
29125 }else
29126
29127 #ifndef SQLITE_OMIT_VIRTUALTABLE
29128 if( c=='e' && cli_strncmp(azArg[0], "expert", n)==0 ){
29129 if( p->bSafeMode ){
29130 eputf("Cannot run experimental commands such as \"%s\" in safe mode\n",
 
29131 azArg[0]);
29132 rc = 1;
29133 }else{
29134 open_db(p, 0);
29135 expertDotCommand(p, azArg, nArg);
@@ -29182,13 +28667,14 @@
29182 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
29183 }
29184
29185 /* --help lists all file-controls */
29186 if( cli_strcmp(zCmd,"help")==0 ){
29187 oputz("Available file-controls:\n");
29188 for(i=0; i<ArraySize(aCtrl); i++){
29189 oputf(" .filectrl %s %s\n", aCtrl[i].zCtrlName, aCtrl[i].zUsage);
 
29190 }
29191 rc = 1;
29192 goto meta_command_exit;
29193 }
29194
@@ -29199,19 +28685,19 @@
29199 if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
29200 if( filectrl<0 ){
29201 filectrl = aCtrl[i].ctrlCode;
29202 iCtrl = i;
29203 }else{
29204 eputf("Error: ambiguous file-control: \"%s\"\n"
29205 "Use \".filectrl --help\" for help\n", zCmd);
29206 rc = 1;
29207 goto meta_command_exit;
29208 }
29209 }
29210 }
29211 if( filectrl<0 ){
29212 eputf("Error: unknown file-control: %s\n"
29213 "Use \".filectrl --help\" for help\n", zCmd);
29214 }else{
29215 switch(filectrl){
29216 case SQLITE_FCNTL_SIZE_LIMIT: {
29217 if( nArg!=2 && nArg!=3 ) break;
@@ -29251,11 +28737,11 @@
29251 case SQLITE_FCNTL_TEMPFILENAME: {
29252 char *z = 0;
29253 if( nArg!=2 ) break;
29254 sqlite3_file_control(p->db, zSchema, filectrl, &z);
29255 if( z ){
29256 oputf("%s\n", z);
29257 sqlite3_free(z);
29258 }
29259 isOk = 2;
29260 break;
29261 }
@@ -29265,23 +28751,24 @@
29265 x = atoi(azArg[2]);
29266 sqlite3_file_control(p->db, zSchema, filectrl, &x);
29267 }
29268 x = -1;
29269 sqlite3_file_control(p->db, zSchema, filectrl, &x);
29270 oputf("%d\n", x);
29271 isOk = 2;
29272 break;
29273 }
29274 }
29275 }
29276 if( isOk==0 && iCtrl>=0 ){
29277 oputf("Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
 
29278 rc = 1;
29279 }else if( isOk==1 ){
29280 char zBuf[100];
29281 sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
29282 oputf("%s\n", zBuf);
29283 }
29284 }else
29285
29286 if( c=='f' && cli_strncmp(azArg[0], "fullschema", n)==0 ){
29287 ShellState data;
@@ -29318,19 +28805,19 @@
29318 doStats = sqlite3_step(pStmt)==SQLITE_ROW;
29319 sqlite3_finalize(pStmt);
29320 }
29321 }
29322 if( doStats==0 ){
29323 oputz("/* No STAT tables available */\n");
29324 }else{
29325 oputz("ANALYZE sqlite_schema;\n");
29326 data.cMode = data.mode = MODE_Insert;
29327 data.zDestTable = "sqlite_stat1";
29328 shell_exec(&data, "SELECT * FROM sqlite_stat1", 0);
29329 data.zDestTable = "sqlite_stat4";
29330 shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
29331 oputz("ANALYZE sqlite_schema;\n");
29332 }
29333 }else
29334
29335 if( c=='h' && cli_strncmp(azArg[0], "headers", n)==0 ){
29336 if( nArg==2 ){
@@ -29344,11 +28831,11 @@
29344
29345 if( c=='h' && cli_strncmp(azArg[0], "help", n)==0 ){
29346 if( nArg>=2 ){
29347 n = showHelp(p->out, azArg[1]);
29348 if( n==0 ){
29349 oputf("Nothing matches '%s'\n", azArg[1]);
29350 }
29351 }else{
29352 showHelp(p->out, 0);
29353 }
29354 }else
@@ -29387,11 +28874,11 @@
29387 if( zFile==0 ){
29388 zFile = z;
29389 }else if( zTable==0 ){
29390 zTable = z;
29391 }else{
29392 oputf("ERROR: extra argument: \"%s\". Usage:\n", z);
29393 showHelp(p->out, "import");
29394 goto meta_command_exit;
29395 }
29396 }else if( cli_strcmp(z,"-v")==0 ){
29397 eVerbose++;
@@ -29408,17 +28895,17 @@
29408 sCtx.cColSep = ',';
29409 sCtx.cRowSep = '\n';
29410 xRead = csv_read_one_field;
29411 useOutputMode = 0;
29412 }else{
29413 oputf("ERROR: unknown option: \"%s\". Usage:\n", z);
29414 showHelp(p->out, "import");
29415 goto meta_command_exit;
29416 }
29417 }
29418 if( zTable==0 ){
29419 oputf("ERROR: missing %s argument. Usage:\n",
29420 zFile==0 ? "FILE" : "TABLE");
29421 showHelp(p->out, "import");
29422 goto meta_command_exit;
29423 }
29424 seenInterrupt = 0;
@@ -29464,32 +28951,32 @@
29464 if( sCtx.zFile[0]=='|' ){
29465 #ifdef SQLITE_OMIT_POPEN
29466 eputz("Error: pipes are not supported in this OS\n");
29467 goto meta_command_exit;
29468 #else
29469 sCtx.in = popen(sCtx.zFile+1, "r");
29470 sCtx.zFile = "<pipe>";
29471 sCtx.xCloser = pclose;
29472 #endif
29473 }else{
29474 sCtx.in = fopen(sCtx.zFile, "rb");
29475 sCtx.xCloser = fclose;
29476 }
29477 if( sCtx.in==0 ){
29478 eputf("Error: cannot open \"%s\"\n", zFile);
29479 goto meta_command_exit;
29480 }
29481 if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
29482 char zSep[2];
29483 zSep[1] = 0;
29484 zSep[0] = sCtx.cColSep;
29485 oputz("Column separator ");
29486 output_c_string(zSep);
29487 oputz(", row separator ");
29488 zSep[0] = sCtx.cRowSep;
29489 output_c_string(zSep);
29490 oputz("\n");
29491 }
29492 sCtx.z = sqlite3_malloc64(120);
29493 if( sCtx.z==0 ){
29494 import_cleanup(&sCtx);
29495 shell_out_of_memory();
@@ -29510,18 +28997,18 @@
29510 zAutoColumn(sCtx.z, &dbCols, 0);
29511 if( sCtx.cTerm!=sCtx.cColSep ) break;
29512 }
29513 zColDefs = zAutoColumn(0, &dbCols, &zRenames);
29514 if( zRenames!=0 ){
29515 sputf((stdin_is_interactive && p->in==stdin)? p->out : stderr,
29516 "Columns renamed during .import %s due to duplicates:\n"
29517 "%s\n", sCtx.zFile, zRenames);
29518 sqlite3_free(zRenames);
29519 }
29520 assert(dbCols==0);
29521 if( zColDefs==0 ){
29522 eputf("%s: empty file\n", sCtx.zFile);
29523 import_cleanup(&sCtx);
29524 rc = 1;
29525 sqlite3_free(zCreate);
29526 goto meta_command_exit;
29527 }
@@ -29529,17 +29016,20 @@
29529 if( zCreate==0 ){
29530 import_cleanup(&sCtx);
29531 shell_out_of_memory();
29532 }
29533 if( eVerbose>=1 ){
29534 oputf("%s\n", zCreate);
29535 }
29536 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
 
 
 
 
29537 sqlite3_free(zCreate);
29538 zCreate = 0;
29539 if( rc ){
29540 eputf("%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db));
29541 import_cleanup(&sCtx);
29542 rc = 1;
29543 goto meta_command_exit;
29544 }
29545 }
@@ -29590,11 +29080,11 @@
29590 }
29591 zSql[j++] = ')';
29592 zSql[j] = 0;
29593 assert( j<nByte );
29594 if( eVerbose>=2 ){
29595 oputf("Insert using: %s\n", zSql);
29596 }
29597 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
29598 sqlite3_free(zSql);
29599 zSql = 0;
29600 if( rc ){
@@ -29629,11 +29119,11 @@
29629 if( z==0 && (xRead==csv_read_one_field) && i==nCol-1 && i>0 ){
29630 z = "";
29631 }
29632 sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
29633 if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
29634 eputf("%s:%d: expected %d columns but found %d"
29635 " - filling the rest with NULL\n",
29636 sCtx.zFile, startLine, nCol, i+1);
29637 i += 2;
29638 while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
29639 }
@@ -29641,18 +29131,19 @@
29641 if( sCtx.cTerm==sCtx.cColSep ){
29642 do{
29643 xRead(&sCtx);
29644 i++;
29645 }while( sCtx.cTerm==sCtx.cColSep );
29646 eputf("%s:%d: expected %d columns but found %d - extras ignored\n",
 
29647 sCtx.zFile, startLine, nCol, i);
29648 }
29649 if( i>=nCol ){
29650 sqlite3_step(pStmt);
29651 rc = sqlite3_reset(pStmt);
29652 if( rc!=SQLITE_OK ){
29653 eputf("%s:%d: INSERT failed: %s\n",
29654 sCtx.zFile, startLine, sqlite3_errmsg(p->db));
29655 sCtx.nErr++;
29656 }else{
29657 sCtx.nRow++;
29658 }
@@ -29661,11 +29152,12 @@
29661
29662 import_cleanup(&sCtx);
29663 sqlite3_finalize(pStmt);
29664 if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
29665 if( eVerbose>0 ){
29666 oputf("Added %d rows with %d errors using %d lines of input\n",
 
29667 sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
29668 }
29669 }else
29670 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
29671
@@ -29677,11 +29169,11 @@
29677 int tnum = 0;
29678 int isWO = 0; /* True if making an imposter of a WITHOUT ROWID table */
29679 int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
29680 int i;
29681 if( !ShellHasFlag(p,SHFLG_TestingMode) ){
29682 eputf(".%s unavailable without --unsafe-testing\n",
29683 "imposter");
29684 rc = 1;
29685 goto meta_command_exit;
29686 }
29687 if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
@@ -29743,11 +29235,11 @@
29743 zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
29744 }
29745 }
29746 sqlite3_finalize(pStmt);
29747 if( i==0 || tnum==0 ){
29748 eputf("no such index: \"%s\"\n", azArg[1]);
29749 rc = 1;
29750 sqlite3_free(zCollist);
29751 goto meta_command_exit;
29752 }
29753 if( lenPK==0 ) lenPK = 100000;
@@ -29758,18 +29250,20 @@
29758 rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
29759 if( rc==SQLITE_OK ){
29760 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
29761 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
29762 if( rc ){
29763 eputf("Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
 
29764 }else{
29765 sputf(stdout, "%s;\n", zSql);
29766 sputf(stdout, "WARNING: writing to an imposter table will corrupt"
 
29767 " the \"%s\" %s!\n", azArg[1], isWO ? "table" : "index");
29768 }
29769 }else{
29770 eputf("SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
29771 rc = 1;
29772 }
29773 sqlite3_free(zSql);
29774 }else
29775 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
@@ -29779,11 +29273,11 @@
29779 if( nArg==2 ){
29780 iArg = integerValue(azArg[1]);
29781 if( iArg==0 ) iArg = -1;
29782 }
29783 if( (nArg!=1 && nArg!=2) || iArg<0 ){
29784 eputf("%s","Usage: .intck STEPS_PER_UNLOCK\n");
29785 rc = 1;
29786 goto meta_command_exit;
29787 }
29788 open_db(p, 0);
29789 rc = intckDatabaseCmd(p, iArg);
@@ -29798,13 +29292,13 @@
29798 sqlite3IoTrace = 0;
29799 }else if( cli_strcmp(azArg[1], "-")==0 ){
29800 sqlite3IoTrace = iotracePrintf;
29801 iotrace = stdout;
29802 }else{
29803 iotrace = fopen(azArg[1], "w");
29804 if( iotrace==0 ){
29805 eputf("Error: cannot open \"%s\"\n", azArg[1]);
29806 sqlite3IoTrace = 0;
29807 rc = 1;
29808 }else{
29809 sqlite3IoTrace = iotracePrintf;
29810 }
@@ -29832,11 +29326,11 @@
29832 };
29833 int i, n2;
29834 open_db(p, 0);
29835 if( nArg==1 ){
29836 for(i=0; i<ArraySize(aLimit); i++){
29837 sputf(stdout, "%20s %d\n", aLimit[i].zLimitName,
29838 sqlite3_limit(p->db, aLimit[i].limitCode, -1));
29839 }
29840 }else if( nArg>3 ){
29841 eputz("Usage: .limit NAME ?NEW-VALUE?\n");
29842 rc = 1;
@@ -29847,28 +29341,28 @@
29847 for(i=0; i<ArraySize(aLimit); i++){
29848 if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
29849 if( iLimit<0 ){
29850 iLimit = i;
29851 }else{
29852 eputf("ambiguous limit: \"%s\"\n", azArg[1]);
29853 rc = 1;
29854 goto meta_command_exit;
29855 }
29856 }
29857 }
29858 if( iLimit<0 ){
29859 eputf("unknown limit: \"%s\"\n"
29860 "enter \".limits\" with no arguments for a list.\n",
29861 azArg[1]);
29862 rc = 1;
29863 goto meta_command_exit;
29864 }
29865 if( nArg==3 ){
29866 sqlite3_limit(p->db, aLimit[iLimit].limitCode,
29867 (int)integerValue(azArg[2]));
29868 }
29869 sputf(stdout, "%20s %d\n", aLimit[iLimit].zLimitName,
29870 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
29871 }
29872 }else
29873
29874 if( c=='l' && n>2 && cli_strncmp(azArg[0], "lint", n)==0 ){
@@ -29947,35 +29441,37 @@
29947 cmOpts = cmo;
29948 }
29949 }else if( zTabname==0 ){
29950 zTabname = z;
29951 }else if( z[0]=='-' ){
29952 eputf("unknown option: %s\n", z);
29953 eputz("options:\n"
29954 " --noquote\n"
29955 " --quote\n"
29956 " --wordwrap on/off\n"
29957 " --wrap N\n"
29958 " --ww\n");
29959 rc = 1;
29960 goto meta_command_exit;
29961 }else{
29962 eputf("extra argument: \"%s\"\n", z);
29963 rc = 1;
29964 goto meta_command_exit;
29965 }
29966 }
29967 if( zMode==0 ){
29968 if( p->mode==MODE_Column
29969 || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
29970 ){
29971 oputf("current output mode: %s --wrap %d --wordwrap %s --%squote\n",
 
29972 modeDescr[p->mode], p->cmOpts.iWrap,
29973 p->cmOpts.bWordWrap ? "on" : "off",
29974 p->cmOpts.bQuote ? "" : "no");
29975 }else{
29976 oputf("current output mode: %s\n", modeDescr[p->mode]);
 
29977 }
29978 zMode = modeDescr[p->mode];
29979 }
29980 n2 = strlen30(zMode);
29981 if( cli_strncmp(zMode,"lines",n2)==0 ){
@@ -30044,11 +29540,11 @@
30044 if( c=='n' && cli_strcmp(azArg[0], "nonce")==0 ){
30045 if( nArg!=2 ){
30046 eputz("Usage: .nonce NONCE\n");
30047 rc = 1;
30048 }else if( p->zNonce==0 || cli_strcmp(azArg[1],p->zNonce)!=0 ){
30049 eputf("line %d: incorrect nonce: \"%s\"\n",
30050 p->lineno, azArg[1]);
30051 exit(1);
30052 }else{
30053 p->bSafeMode = 0;
30054 return 0; /* Return immediately to bypass the safe mode reset
@@ -30099,15 +29595,15 @@
30099 p->szMax = integerValue(azArg[++iName]);
30100 #endif /* SQLITE_OMIT_DESERIALIZE */
30101 }else
30102 #endif /* !SQLITE_SHELL_FIDDLE */
30103 if( z[0]=='-' ){
30104 eputf("unknown option: %s\n", z);
30105 rc = 1;
30106 goto meta_command_exit;
30107 }else if( zFN ){
30108 eputf("extra argument: \"%s\"\n", z);
30109 rc = 1;
30110 goto meta_command_exit;
30111 }else{
30112 zFN = z;
30113 }
@@ -30145,11 +29641,11 @@
30145 zNewFilename = 0;
30146 }
30147 p->pAuxDb->zDbFilename = zNewFilename;
30148 open_db(p, OPEN_DB_KEEPALIVE);
30149 if( p->db==0 ){
30150 eputf("Error: cannot open '%s'\n", zNewFilename);
30151 sqlite3_free(zNewFilename);
30152 }else{
30153 p->pAuxDb->zFreeOnClose = zNewFilename;
30154 }
30155 }
@@ -30163,22 +29659,27 @@
30163 #ifndef SQLITE_SHELL_FIDDLE
30164 if( (c=='o'
30165 && (cli_strncmp(azArg[0], "output", n)==0
30166 || cli_strncmp(azArg[0], "once", n)==0))
30167 || (c=='e' && n==5 && cli_strcmp(azArg[0],"excel")==0)
 
30168 ){
30169 char *zFile = 0;
30170 int bTxtMode = 0;
30171 int i;
30172 int eMode = 0;
30173 int bOnce = 0; /* 0: .output, 1: .once, 2: .excel */
 
30174 static const char *zBomUtf8 = "\xef\xbb\xbf";
30175 const char *zBom = 0;
30176
30177 failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
30178 if( c=='e' ){
30179 eMode = 'x';
 
 
 
30180 bOnce = 2;
30181 }else if( cli_strncmp(azArg[0],"once",n)==0 ){
30182 bOnce = 1;
30183 }
30184 for(i=1; i<nArg; i++){
@@ -30185,28 +29686,34 @@
30185 char *z = azArg[i];
30186 if( z[0]=='-' ){
30187 if( z[1]=='-' ) z++;
30188 if( cli_strcmp(z,"-bom")==0 ){
30189 zBom = zBomUtf8;
30190 }else if( c!='e' && cli_strcmp(z,"-x")==0 ){
 
 
30191 eMode = 'x'; /* spreadsheet */
30192 }else if( c!='e' && cli_strcmp(z,"-e")==0 ){
30193 eMode = 'e'; /* text editor */
 
 
30194 }else{
30195 oputf("ERROR: unknown option: \"%s\". Usage:\n", azArg[i]);
 
30196 showHelp(p->out, azArg[0]);
30197 rc = 1;
30198 goto meta_command_exit;
30199 }
30200 }else if( zFile==0 && eMode!='e' && eMode!='x' ){
30201 zFile = sqlite3_mprintf("%s", z);
30202 if( zFile && zFile[0]=='|' ){
30203 while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
30204 break;
30205 }
30206 }else{
30207 oputf("ERROR: extra parameter: \"%s\". Usage:\n", azArg[i]);
 
30208 showHelp(p->out, azArg[0]);
30209 rc = 1;
30210 sqlite3_free(zFile);
30211 goto meta_command_exit;
30212 }
@@ -30219,20 +29726,29 @@
30219 }else{
30220 p->outCount = 0;
30221 }
30222 output_reset(p);
30223 #ifndef SQLITE_NOHAVE_SYSTEM
30224 if( eMode=='e' || eMode=='x' ){
30225 p->doXdgOpen = 1;
30226 outputModePush(p);
30227 if( eMode=='x' ){
30228 /* spreadsheet mode. Output as CSV. */
30229 newTempFile(p, "csv");
30230 ShellClearFlag(p, SHFLG_Echo);
30231 p->mode = MODE_Csv;
30232 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
30233 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
 
 
 
 
 
 
 
 
 
30234 }else{
30235 /* text editor mode */
30236 newTempFile(p, "txt");
30237 bTxtMode = 1;
30238 }
@@ -30245,30 +29761,36 @@
30245 #ifdef SQLITE_OMIT_POPEN
30246 eputz("Error: pipes are not supported in this OS\n");
30247 rc = 1;
30248 output_redir(p, stdout);
30249 #else
30250 FILE *pfPipe = popen(zFile + 1, "w");
30251 if( pfPipe==0 ){
30252 eputf("Error: cannot open pipe \"%s\"\n", zFile + 1);
30253 rc = 1;
30254 }else{
30255 output_redir(p, pfPipe);
30256 if( zBom ) oputz(zBom);
30257 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
30258 }
30259 #endif
30260 }else{
30261 FILE *pfFile = output_file_open(zFile, bTxtMode);
30262 if( pfFile==0 ){
30263 if( cli_strcmp(zFile,"off")!=0 ){
30264 eputf("Error: cannot write to \"%s\"\n", zFile);
30265 }
30266 rc = 1;
30267 } else {
30268 output_redir(p, pfFile);
30269 if( zBom ) oputz(zBom);
 
 
 
 
 
 
30270 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
30271 }
30272 }
30273 sqlite3_free(zFile);
30274 }else
@@ -30305,11 +29827,12 @@
30305 if( len ){
30306 rx = sqlite3_prepare_v2(p->db,
30307 "SELECT key, quote(value) "
30308 "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
30309 while( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
30310 oputf("%-*s %s\n", len, sqlite3_column_text(pStmt,0),
 
30311 sqlite3_column_text(pStmt,1));
30312 }
30313 sqlite3_finalize(pStmt);
30314 }
30315 }else
@@ -30350,11 +29873,11 @@
30350 "VALUES(%Q,%Q);", zKey, zValue);
30351 shell_check_oom(zSql);
30352 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
30353 sqlite3_free(zSql);
30354 if( rx!=SQLITE_OK ){
30355 oputf("Error: %s\n", sqlite3_errmsg(p->db));
30356 sqlite3_finalize(pStmt);
30357 pStmt = 0;
30358 rc = 1;
30359 }
30360 }
@@ -30379,14 +29902,14 @@
30379 }else
30380
30381 if( c=='p' && n>=3 && cli_strncmp(azArg[0], "print", n)==0 ){
30382 int i;
30383 for(i=1; i<nArg; i++){
30384 if( i>1 ) oputz(" ");
30385 oputz(azArg[i]);
30386 }
30387 oputz("\n");
30388 }else
30389
30390 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
30391 if( c=='p' && n>=3 && cli_strncmp(azArg[0], "progress", n)==0 ){
30392 int i;
@@ -30419,11 +29942,11 @@
30419 }else{
30420 p->mxProgress = (int)integerValue(azArg[++i]);
30421 }
30422 continue;
30423 }
30424 eputf("Error: unknown option: \"%s\"\n", azArg[i]);
30425 rc = 1;
30426 goto meta_command_exit;
30427 }else{
30428 nn = (int)integerValue(z);
30429 }
@@ -30462,21 +29985,21 @@
30462 #ifdef SQLITE_OMIT_POPEN
30463 eputz("Error: pipes are not supported in this OS\n");
30464 rc = 1;
30465 p->out = stdout;
30466 #else
30467 p->in = popen(azArg[1]+1, "r");
30468 if( p->in==0 ){
30469 eputf("Error: cannot open \"%s\"\n", azArg[1]);
30470 rc = 1;
30471 }else{
30472 rc = process_input(p);
30473 pclose(p->in);
30474 }
30475 #endif
30476 }else if( (p->in = openChrSource(azArg[1]))==0 ){
30477 eputf("Error: cannot open \"%s\"\n", azArg[1]);
30478 rc = 1;
30479 }else{
30480 rc = process_input(p);
30481 fclose(p->in);
30482 }
@@ -30505,11 +30028,11 @@
30505 rc = 1;
30506 goto meta_command_exit;
30507 }
30508 rc = sqlite3_open(zSrcFile, &pSrc);
30509 if( rc!=SQLITE_OK ){
30510 eputf("Error: cannot open \"%s\"\n", zSrcFile);
30511 close_db(pSrc);
30512 return 1;
30513 }
30514 open_db(p, 0);
30515 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
@@ -30588,11 +30111,11 @@
30588 }else if( optionMatch(azArg[ii],"debug") ){
30589 bDebug = 1;
30590 }else if( optionMatch(azArg[ii],"nosys") ){
30591 bNoSystemTabs = 1;
30592 }else if( azArg[ii][0]=='-' ){
30593 eputf("Unknown option: \"%s\"\n", azArg[ii]);
30594 rc = 1;
30595 goto meta_command_exit;
30596 }else if( zName==0 ){
30597 zName = azArg[ii];
30598 }else{
@@ -30689,11 +30212,11 @@
30689 appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
30690 }
30691 appendText(&sSelect, "sql IS NOT NULL"
30692 " ORDER BY snum, rowid", 0);
30693 if( bDebug ){
30694 oputf("SQL: %s;\n", sSelect.z);
30695 }else{
30696 rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
30697 }
30698 freeText(&sSelect);
30699 }
@@ -30750,11 +30273,12 @@
30750 session_not_open:
30751 eputz("ERROR: No sessions are open\n");
30752 }else{
30753 rc = sqlite3session_attach(pSession->p, azCmd[1]);
30754 if( rc ){
30755 eputf("ERROR: sqlite3session_attach() returns %d\n",rc);
 
30756 rc = 0;
30757 }
30758 }
30759 }else
30760
@@ -30767,13 +30291,13 @@
30767 ){
30768 FILE *out = 0;
30769 failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]);
30770 if( nCmd!=2 ) goto session_syntax_error;
30771 if( pSession->p==0 ) goto session_not_open;
30772 out = fopen(azCmd[1], "wb");
30773 if( out==0 ){
30774 eputf("ERROR: cannot open \"%s\" for writing\n",
30775 azCmd[1]);
30776 }else{
30777 int szChng;
30778 void *pChng;
30779 if( azCmd[0][0]=='c' ){
@@ -30780,16 +30304,17 @@
30780 rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
30781 }else{
30782 rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
30783 }
30784 if( rc ){
30785 sputf(stdout, "Error: error code %d\n", rc);
30786 rc = 0;
30787 }
30788 if( pChng
30789 && fwrite(pChng, szChng, 1, out)!=1 ){
30790 eputf("ERROR: Failed to write entire %d-byte output\n", szChng);
 
30791 }
30792 sqlite3_free(pChng);
30793 fclose(out);
30794 }
30795 }else
@@ -30812,11 +30337,12 @@
30812 int ii;
30813 if( nCmd>2 ) goto session_syntax_error;
30814 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
30815 if( pAuxDb->nSession ){
30816 ii = sqlite3session_enable(pSession->p, ii);
30817 oputf("session %s enable flag = %d\n", pSession->zName, ii);
 
30818 }
30819 }else
30820
30821 /* .session filter GLOB ....
30822 ** Set a list of GLOB patterns of table names to be excluded.
@@ -30847,11 +30373,12 @@
30847 int ii;
30848 if( nCmd>2 ) goto session_syntax_error;
30849 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
30850 if( pAuxDb->nSession ){
30851 ii = sqlite3session_indirect(pSession->p, ii);
30852 oputf("session %s indirect flag = %d\n", pSession->zName, ii);
 
30853 }
30854 }else
30855
30856 /* .session isempty
30857 ** Determine if the session is empty
@@ -30859,20 +30386,21 @@
30859 if( cli_strcmp(azCmd[0], "isempty")==0 ){
30860 int ii;
30861 if( nCmd!=1 ) goto session_syntax_error;
30862 if( pAuxDb->nSession ){
30863 ii = sqlite3session_isempty(pSession->p);
30864 oputf("session %s isempty flag = %d\n", pSession->zName, ii);
 
30865 }
30866 }else
30867
30868 /* .session list
30869 ** List all currently open sessions
30870 */
30871 if( cli_strcmp(azCmd[0],"list")==0 ){
30872 for(i=0; i<pAuxDb->nSession; i++){
30873 oputf("%d %s\n", i, pAuxDb->aSession[i].zName);
30874 }
30875 }else
30876
30877 /* .session open DB NAME
30878 ** Open a new session called NAME on the attached database DB.
@@ -30883,22 +30411,23 @@
30883 if( nCmd!=3 ) goto session_syntax_error;
30884 zName = azCmd[2];
30885 if( zName[0]==0 ) goto session_syntax_error;
30886 for(i=0; i<pAuxDb->nSession; i++){
30887 if( cli_strcmp(pAuxDb->aSession[i].zName,zName)==0 ){
30888 eputf("Session \"%s\" already exists\n", zName);
30889 goto meta_command_exit;
30890 }
30891 }
30892 if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){
30893 eputf("Maximum of %d sessions\n", ArraySize(pAuxDb->aSession));
 
30894 goto meta_command_exit;
30895 }
30896 pSession = &pAuxDb->aSession[pAuxDb->nSession];
30897 rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
30898 if( rc ){
30899 eputf("Cannot open session: error code=%d\n", rc);
30900 rc = 0;
30901 goto meta_command_exit;
30902 }
30903 pSession->nFilter = 0;
30904 sqlite3session_table_filter(pSession->p, session_filter, pSession);
@@ -30918,20 +30447,20 @@
30918 if( c=='s' && n>=10 && cli_strncmp(azArg[0], "selftest-", 9)==0 ){
30919 if( cli_strncmp(azArg[0]+9, "boolean", n-9)==0 ){
30920 int i, v;
30921 for(i=1; i<nArg; i++){
30922 v = booleanValue(azArg[i]);
30923 oputf("%s: %d 0x%x\n", azArg[i], v, v);
30924 }
30925 }
30926 if( cli_strncmp(azArg[0]+9, "integer", n-9)==0 ){
30927 int i; sqlite3_int64 v;
30928 for(i=1; i<nArg; i++){
30929 char zBuf[200];
30930 v = integerValue(azArg[i]);
30931 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
30932 oputz(zBuf);
30933 }
30934 }
30935 }else
30936 #endif
30937
@@ -30954,12 +30483,13 @@
30954 }else
30955 if( cli_strcmp(z,"-v")==0 ){
30956 bVerbose++;
30957 }else
30958 {
30959 eputf("Unknown option \"%s\" on \"%s\"\n", azArg[i], azArg[0]);
30960 eputz("Should be one of: --init -v\n");
 
30961 rc = 1;
30962 goto meta_command_exit;
30963 }
30964 }
30965 if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
@@ -31000,46 +30530,47 @@
31000 if( zOp==0 ) continue;
31001 if( zSql==0 ) continue;
31002 if( zAns==0 ) continue;
31003 k = 0;
31004 if( bVerbose>0 ){
31005 sputf(stdout, "%d: %s %s\n", tno, zOp, zSql);
31006 }
31007 if( cli_strcmp(zOp,"memo")==0 ){
31008 oputf("%s\n", zSql);
31009 }else
31010 if( cli_strcmp(zOp,"run")==0 ){
31011 char *zErrMsg = 0;
31012 str.n = 0;
31013 str.z[0] = 0;
31014 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
31015 nTest++;
31016 if( bVerbose ){
31017 oputf("Result: %s\n", str.z);
31018 }
31019 if( rc || zErrMsg ){
31020 nErr++;
31021 rc = 1;
31022 oputf("%d: error-code-%d: %s\n", tno, rc, zErrMsg);
31023 sqlite3_free(zErrMsg);
31024 }else if( cli_strcmp(zAns,str.z)!=0 ){
31025 nErr++;
31026 rc = 1;
31027 oputf("%d: Expected: [%s]\n", tno, zAns);
31028 oputf("%d: Got: [%s]\n", tno, str.z);
31029 }
31030 }
31031 else{
31032 eputf("Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
 
31033 rc = 1;
31034 break;
31035 }
31036 } /* End loop over rows of content from SELFTEST */
31037 sqlite3_finalize(pStmt);
31038 } /* End loop over k */
31039 freeText(&str);
31040 oputf("%d errors out of %d tests\n", nErr, nTest);
31041 }else
31042
31043 if( c=='s' && cli_strncmp(azArg[0], "separator", n)==0 ){
31044 if( nArg<2 || nArg>3 ){
31045 eputz("Usage: .separator COL ?ROW?\n");
@@ -31083,11 +30614,12 @@
31083 }else
31084 if( cli_strcmp(z,"debug")==0 ){
31085 bDebug = 1;
31086 }else
31087 {
31088 eputf("Unknown option \"%s\" on \"%s\"\n", azArg[i], azArg[0]);
 
31089 showHelp(p->out, azArg[0]);
31090 rc = 1;
31091 goto meta_command_exit;
31092 }
31093 }else if( zLike ){
@@ -31161,11 +30693,11 @@
31161 }
31162 shell_check_oom(zSql);
31163 freeText(&sQuery);
31164 freeText(&sSql);
31165 if( bDebug ){
31166 oputf("%s\n", zSql);
31167 }else{
31168 shell_exec(p, zSql, 0);
31169 }
31170 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && !defined(SQLITE_OMIT_VIRTUALTABLE)
31171 {
@@ -31191,11 +30723,11 @@
31191 "||group_concat('CAST(CAST('||cname||' AS BLOB) AS TEXT)<>'||cname\n"
31192 "|| ' AND typeof('||cname||')=''text'' ',\n"
31193 "' OR ') as query, tname from tabcols group by tname)"
31194 , zRevText);
31195 shell_check_oom(zRevText);
31196 if( bDebug ) oputf("%s\n", zRevText);
31197 lrc = sqlite3_prepare_v2(p->db, zRevText, -1, &pStmt, 0);
31198 if( lrc!=SQLITE_OK ){
31199 /* assert(lrc==SQLITE_NOMEM); // might also be SQLITE_ERROR if the
31200 ** user does cruel and unnatural things like ".limit expr_depth 0". */
31201 rc = 1;
@@ -31204,19 +30736,20 @@
31204 lrc = SQLITE_ROW==sqlite3_step(pStmt);
31205 if( lrc ){
31206 const char *zGenQuery = (char*)sqlite3_column_text(pStmt,0);
31207 sqlite3_stmt *pCheckStmt;
31208 lrc = sqlite3_prepare_v2(p->db, zGenQuery, -1, &pCheckStmt, 0);
31209 if( bDebug ) oputf("%s\n", zGenQuery);
31210 if( lrc!=SQLITE_OK ){
31211 rc = 1;
31212 }else{
31213 if( SQLITE_ROW==sqlite3_step(pCheckStmt) ){
31214 double countIrreversible = sqlite3_column_double(pCheckStmt, 0);
31215 if( countIrreversible>0 ){
31216 int sz = (int)(countIrreversible + 0.5);
31217 eputf("Digest includes %d invalidly encoded text field%s.\n",
 
31218 sz, (sz>1)? "s": "");
31219 }
31220 }
31221 sqlite3_finalize(pCheckStmt);
31222 }
@@ -31246,15 +30779,15 @@
31246 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
31247 for(i=2; i<nArg && zCmd!=0; i++){
31248 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
31249 zCmd, azArg[i]);
31250 }
31251 consoleRestore();
31252 x = zCmd!=0 ? system(zCmd) : 1;
31253 consoleRenewSetup();
31254 sqlite3_free(zCmd);
31255 if( x ) eputf("System command returns %d\n", x);
31256 }else
31257 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) */
31258
31259 if( c=='s' && cli_strncmp(azArg[0], "show", n)==0 ){
31260 static const char *azBool[] = { "off", "on", "trigger", "full"};
@@ -31263,50 +30796,52 @@
31263 if( nArg!=1 ){
31264 eputz("Usage: .show\n");
31265 rc = 1;
31266 goto meta_command_exit;
31267 }
31268 oputf("%12.12s: %s\n","echo",
31269 azBool[ShellHasFlag(p, SHFLG_Echo)]);
31270 oputf("%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
31271 oputf("%12.12s: %s\n","explain",
31272 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
31273 oputf("%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
 
31274 if( p->mode==MODE_Column
31275 || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
31276 ){
31277 oputf("%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode",
 
31278 modeDescr[p->mode], p->cmOpts.iWrap,
31279 p->cmOpts.bWordWrap ? "on" : "off",
31280 p->cmOpts.bQuote ? "" : "no");
31281 }else{
31282 oputf("%12.12s: %s\n","mode", modeDescr[p->mode]);
31283 }
31284 oputf("%12.12s: ", "nullvalue");
31285 output_c_string(p->nullValue);
31286 oputz("\n");
31287 oputf("%12.12s: %s\n","output",
31288 strlen30(p->outfile) ? p->outfile : "stdout");
31289 oputf("%12.12s: ", "colseparator");
31290 output_c_string(p->colSeparator);
31291 oputz("\n");
31292 oputf("%12.12s: ", "rowseparator");
31293 output_c_string(p->rowSeparator);
31294 oputz("\n");
31295 switch( p->statsOn ){
31296 case 0: zOut = "off"; break;
31297 default: zOut = "on"; break;
31298 case 2: zOut = "stmt"; break;
31299 case 3: zOut = "vmstep"; break;
31300 }
31301 oputf("%12.12s: %s\n","stats", zOut);
31302 oputf("%12.12s: ", "width");
31303 for (i=0;i<p->nWidth;i++) {
31304 oputf("%d ", p->colWidth[i]);
31305 }
31306 oputz("\n");
31307 oputf("%12.12s: %s\n", "filename",
31308 p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : "");
31309 }else
31310
31311 if( c=='s' && cli_strncmp(azArg[0], "stats", n)==0 ){
31312 if( nArg==2 ){
@@ -31420,13 +30955,14 @@
31420 if( nPrintCol<1 ) nPrintCol = 1;
31421 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
31422 for(i=0; i<nPrintRow; i++){
31423 for(j=i; j<nRow; j+=nPrintRow){
31424 char *zSp = j<nPrintRow ? "" : " ";
31425 oputf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j]:"");
 
31426 }
31427 oputz("\n");
31428 }
31429 }
31430
31431 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
31432 sqlite3_free(azResult);
@@ -31498,14 +31034,14 @@
31498 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
31499 }
31500
31501 /* --help lists all test-controls */
31502 if( cli_strcmp(zCmd,"help")==0 ){
31503 oputz("Available test-controls:\n");
31504 for(i=0; i<ArraySize(aCtrl); i++){
31505 if( aCtrl[i].unSafe && !ShellHasFlag(p,SHFLG_TestingMode) ) continue;
31506 oputf(" .testctrl %s %s\n",
31507 aCtrl[i].zCtrlName, aCtrl[i].zUsage);
31508 }
31509 rc = 1;
31510 goto meta_command_exit;
31511 }
@@ -31518,19 +31054,19 @@
31518 if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
31519 if( testctrl<0 ){
31520 testctrl = aCtrl[i].ctrlCode;
31521 iCtrl = i;
31522 }else{
31523 eputf("Error: ambiguous test-control: \"%s\"\n"
31524 "Use \".testctrl --help\" for help\n", zCmd);
31525 rc = 1;
31526 goto meta_command_exit;
31527 }
31528 }
31529 }
31530 if( testctrl<0 ){
31531 eputf("Error: unknown test-control: %s\n"
31532 "Use \".testctrl --help\" for help\n", zCmd);
31533 }else{
31534 switch(testctrl){
31535
31536 /* Special processing for .testctrl opt MASK ...
@@ -31602,16 +31138,17 @@
31602 int jj;
31603 for(jj=0; jj<ArraySize(aLabel); jj++){
31604 if( sqlite3_stricmp(zLabel, aLabel[jj].zLabel)==0 ) break;
31605 }
31606 if( jj>=ArraySize(aLabel) ){
31607 eputf("Error: no such optimization: \"%s\"\n", zLabel);
31608 eputz("Should be one of:");
 
31609 for(jj=0; jj<ArraySize(aLabel); jj++){
31610 eputf(" %s", aLabel[jj].zLabel);
31611 }
31612 eputz("\n");
31613 rc = 1;
31614 goto meta_command_exit;
31615 }
31616 if( useLabel=='+' ){
31617 newOpt &= ~aLabel[jj].mask;
@@ -31624,20 +31161,20 @@
31624 sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,p->db,newOpt);
31625 }else if( nArg<3 ){
31626 curOpt = ~newOpt;
31627 }
31628 if( newOpt==0 ){
31629 oputz("+All\n");
31630 }else if( newOpt==0xffffffff ){
31631 oputz("-All\n");
31632 }else{
31633 int jj;
31634 for(jj=0; jj<ArraySize(aLabel); jj++){
31635 unsigned int m = aLabel[jj].mask;
31636 if( !aLabel[jj].bDsply ) continue;
31637 if( (curOpt&m)!=(newOpt&m) ){
31638 oputf("%c%s\n", (newOpt & m)==0 ? '+' : '-',
31639 aLabel[jj].zLabel);
31640 }
31641 }
31642 }
31643 rc2 = isOk = 3;
@@ -31677,11 +31214,11 @@
31677 if( nArg==3 || nArg==4 ){
31678 int ii = (int)integerValue(azArg[2]);
31679 sqlite3 *db;
31680 if( ii==0 && cli_strcmp(azArg[2],"random")==0 ){
31681 sqlite3_randomness(sizeof(ii),&ii);
31682 sputf(stdout, "-- random seed: %d\n", ii);
31683 }
31684 if( nArg==3 ){
31685 db = 0;
31686 }else{
31687 db = p->db;
@@ -31745,11 +31282,11 @@
31745 break;
31746
31747 case SQLITE_TESTCTRL_SEEK_COUNT: {
31748 u64 x = 0;
31749 rc2 = sqlite3_test_control(testctrl, p->db, &x);
31750 oputf("%llu\n", x);
31751 isOk = 3;
31752 break;
31753 }
31754
31755 #ifdef YYCOVERAGE
@@ -31776,15 +31313,15 @@
31776 int id = 1;
31777 while(1){
31778 int val = 0;
31779 rc2 = sqlite3_test_control(testctrl, -id, &val);
31780 if( rc2!=SQLITE_OK ) break;
31781 if( id>1 ) oputz(" ");
31782 oputf("%d: %d", id, val);
31783 id++;
31784 }
31785 if( id>1 ) oputz("\n");
31786 isOk = 3;
31787 }
31788 break;
31789 }
31790 #endif
@@ -31822,18 +31359,26 @@
31822 }else if( cli_strcmp(z,"reset")==0 ){
31823 faultsim_state.iCnt = faultsim_state.nSkip;
31824 faultsim_state.nHit = 0;
31825 sqlite3_test_control(testctrl, faultsim_callback);
31826 }else if( cli_strcmp(z,"status")==0 ){
31827 oputf("faultsim.iId: %d\n", faultsim_state.iId);
31828 oputf("faultsim.iErr: %d\n", faultsim_state.iErr);
31829 oputf("faultsim.iCnt: %d\n", faultsim_state.iCnt);
31830 oputf("faultsim.nHit: %d\n", faultsim_state.nHit);
31831 oputf("faultsim.iInterval: %d\n", faultsim_state.iInterval);
31832 oputf("faultsim.eVerbose: %d\n", faultsim_state.eVerbose);
31833 oputf("faultsim.nRepeat: %d\n", faultsim_state.nRepeat);
31834 oputf("faultsim.nSkip: %d\n", faultsim_state.nSkip);
 
 
 
 
 
 
 
 
31835 }else if( cli_strcmp(z,"-v")==0 ){
31836 if( faultsim_state.eVerbose<2 ) faultsim_state.eVerbose++;
31837 }else if( cli_strcmp(z,"-q")==0 ){
31838 if( faultsim_state.eVerbose>0 ) faultsim_state.eVerbose--;
31839 }else if( cli_strcmp(z,"-id")==0 && kk+1<nArg ){
@@ -31847,19 +31392,20 @@
31847 }else if( cli_strcmp(z,"-skip")==0 && kk+1<nArg ){
31848 faultsim_state.nSkip = atoi(azArg[++kk]);
31849 }else if( cli_strcmp(z,"-?")==0 || sqlite3_strglob("*help*",z)==0){
31850 bShowHelp = 1;
31851 }else{
31852 eputf("Unrecognized fault_install argument: \"%s\"\n",
 
31853 azArg[kk]);
31854 rc = 1;
31855 bShowHelp = 1;
31856 break;
31857 }
31858 }
31859 if( bShowHelp ){
31860 oputz(
31861 "Usage: .testctrl fault_install ARGS\n"
31862 "Possible arguments:\n"
31863 " off Disable faultsim\n"
31864 " on Activate faultsim\n"
31865 " reset Reset the trigger counter\n"
@@ -31869,23 +31415,25 @@
31869 " --errcode N When triggered, return N as error code\n"
31870 " --id ID Trigger only for the ID specified\n"
31871 " --interval N Trigger only after every N-th call\n"
31872 " --repeat N Turn off after N hits. 0 means never\n"
31873 " --skip N Skip the first N encounters\n"
 
31874 );
31875 }
31876 break;
31877 }
31878 }
31879 }
31880 if( isOk==0 && iCtrl>=0 ){
31881 oputf("Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
 
31882 rc = 1;
31883 }else if( isOk==1 ){
31884 oputf("%d\n", rc2);
31885 }else if( isOk==2 ){
31886 oputf("0x%08x\n", rc2);
31887 }
31888 }else
31889 #endif /* !defined(SQLITE_UNTESTABLE) */
31890
31891 if( c=='t' && n>4 && cli_strncmp(azArg[0], "timeout", n)==0 ){
@@ -31936,11 +31484,11 @@
31936 }
31937 else if( optionMatch(z, "close") ){
31938 mType |= SQLITE_TRACE_CLOSE;
31939 }
31940 else {
31941 eputf("Unknown option \"%s\" on \".trace\"\n", z);
31942 rc = 1;
31943 goto meta_command_exit;
31944 }
31945 }else{
31946 output_file_close(p->traceOut);
@@ -31996,11 +31544,11 @@
31996 goto meta_command_exit;
31997 }
31998 rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
31999 strlen30(azArg[3]));
32000 if( rc ){
32001 eputf("Authentication failed for user %s\n", azArg[2]);
32002 rc = 1;
32003 }
32004 }else if( cli_strcmp(azArg[1],"add")==0 ){
32005 if( nArg!=5 ){
32006 eputz("Usage: .user add USER PASSWORD ISADMIN\n");
@@ -32008,11 +31556,11 @@
32008 goto meta_command_exit;
32009 }
32010 rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
32011 booleanValue(azArg[4]));
32012 if( rc ){
32013 eputf("User-Add failed: %d\n", rc);
32014 rc = 1;
32015 }
32016 }else if( cli_strcmp(azArg[1],"edit")==0 ){
32017 if( nArg!=5 ){
32018 eputz("Usage: .user edit USER PASSWORD ISADMIN\n");
@@ -32020,11 +31568,11 @@
32020 goto meta_command_exit;
32021 }
32022 rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
32023 booleanValue(azArg[4]));
32024 if( rc ){
32025 eputf("User-Edit failed: %d\n", rc);
32026 rc = 1;
32027 }
32028 }else if( cli_strcmp(azArg[1],"delete")==0 ){
32029 if( nArg!=3 ){
32030 eputz("Usage: .user delete USER\n");
@@ -32031,11 +31579,11 @@
32031 rc = 1;
32032 goto meta_command_exit;
32033 }
32034 rc = sqlite3_user_delete(p->db, azArg[2]);
32035 if( rc ){
32036 eputf("User-Delete failed: %d\n", rc);
32037 rc = 1;
32038 }
32039 }else{
32040 eputz("Usage: .user login|add|edit|delete ...\n");
32041 rc = 1;
@@ -32044,38 +31592,38 @@
32044 }else
32045 #endif /* SQLITE_USER_AUTHENTICATION */
32046
32047 if( c=='v' && cli_strncmp(azArg[0], "version", n)==0 ){
32048 char *zPtrSz = sizeof(void*)==8 ? "64-bit" : "32-bit";
32049 oputf("SQLite %s %s\n" /*extra-version-info*/,
32050 sqlite3_libversion(), sqlite3_sourceid());
32051 #if SQLITE_HAVE_ZLIB
32052 oputf("zlib version %s\n", zlibVersion());
32053 #endif
32054 #define CTIMEOPT_VAL_(opt) #opt
32055 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
32056 #if defined(__clang__) && defined(__clang_major__)
32057 oputf("clang-" CTIMEOPT_VAL(__clang_major__) "."
32058 CTIMEOPT_VAL(__clang_minor__) "."
32059 CTIMEOPT_VAL(__clang_patchlevel__) " (%s)\n", zPtrSz);
32060 #elif defined(_MSC_VER)
32061 oputf("msvc-" CTIMEOPT_VAL(_MSC_VER) " (%s)\n", zPtrSz);
32062 #elif defined(__GNUC__) && defined(__VERSION__)
32063 oputf("gcc-" __VERSION__ " (%s)\n", zPtrSz);
32064 #endif
32065 }else
32066
32067 if( c=='v' && cli_strncmp(azArg[0], "vfsinfo", n)==0 ){
32068 const char *zDbName = nArg==2 ? azArg[1] : "main";
32069 sqlite3_vfs *pVfs = 0;
32070 if( p->db ){
32071 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
32072 if( pVfs ){
32073 oputf("vfs.zName = \"%s\"\n", pVfs->zName);
32074 oputf("vfs.iVersion = %d\n", pVfs->iVersion);
32075 oputf("vfs.szOsFile = %d\n", pVfs->szOsFile);
32076 oputf("vfs.mxPathname = %d\n", pVfs->mxPathname);
32077 }
32078 }
32079 }else
32080
32081 if( c=='v' && cli_strncmp(azArg[0], "vfslist", n)==0 ){
@@ -32083,17 +31631,17 @@
32083 sqlite3_vfs *pCurrent = 0;
32084 if( p->db ){
32085 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
32086 }
32087 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
32088 oputf("vfs.zName = \"%s\"%s\n", pVfs->zName,
32089 pVfs==pCurrent ? " <--- CURRENT" : "");
32090 oputf("vfs.iVersion = %d\n", pVfs->iVersion);
32091 oputf("vfs.szOsFile = %d\n", pVfs->szOsFile);
32092 oputf("vfs.mxPathname = %d\n", pVfs->mxPathname);
32093 if( pVfs->pNext ){
32094 oputz("-----------------------------------\n");
32095 }
32096 }
32097 }else
32098
32099 if( c=='v' && cli_strncmp(azArg[0], "vfsname", n)==0 ){
@@ -32100,11 +31648,11 @@
32100 const char *zDbName = nArg==2 ? azArg[1] : "main";
32101 char *zVfsName = 0;
32102 if( p->db ){
32103 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
32104 if( zVfsName ){
32105 oputf("%s\n", zVfsName);
32106 sqlite3_free(zVfsName);
32107 }
32108 }
32109 }else
32110
@@ -32124,11 +31672,11 @@
32124 p->colWidth[j-1] = (int)integerValue(azArg[j]);
32125 }
32126 }else
32127
32128 {
32129 eputf("Error: unknown command or invalid arguments: "
32130 " \"%s\". Enter \".help\" for help\n", azArg[0]);
32131 rc = 1;
32132 }
32133
32134 meta_command_exit:
@@ -32376,11 +31924,11 @@
32376 open_db(p, 0);
32377 if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
32378 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
32379 BEGIN_TIMER;
32380 rc = shell_exec(p, zSql, &zErrMsg);
32381 END_TIMER;
32382 if( rc || zErrMsg ){
32383 char zPrefix[100];
32384 const char *zErrorTail;
32385 const char *zErrorType;
32386 if( zErrMsg==0 ){
@@ -32400,28 +31948,28 @@
32400 sqlite3_snprintf(sizeof(zPrefix), zPrefix,
32401 "%s near line %d:", zErrorType, startline);
32402 }else{
32403 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:", zErrorType);
32404 }
32405 eputf("%s %s\n", zPrefix, zErrorTail);
32406 sqlite3_free(zErrMsg);
32407 zErrMsg = 0;
32408 return 1;
32409 }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
32410 char zLineBuf[2000];
32411 sqlite3_snprintf(sizeof(zLineBuf), zLineBuf,
32412 "changes: %lld total_changes: %lld",
32413 sqlite3_changes64(p->db), sqlite3_total_changes64(p->db));
32414 oputf("%s\n", zLineBuf);
32415 }
32416
32417 if( doAutoDetectRestore(p, zSql) ) return 1;
32418 return 0;
32419 }
32420
32421 static void echo_group_input(ShellState *p, const char *zDo){
32422 if( ShellHasFlag(p, SHFLG_Echo) ) oputf("%s\n", zDo);
32423 }
32424
32425 #ifdef SQLITE_SHELL_FIDDLE
32426 /*
32427 ** Alternate one_input_line() impl for wasm mode. This is not in the primary
@@ -32475,11 +32023,11 @@
32475 i64 startline = 0; /* Line number for start of current input */
32476 QuickScanState qss = QSS_Start; /* Accumulated line status (so far) */
32477
32478 if( p->inputNesting==MAX_INPUT_NESTING ){
32479 /* This will be more informative in a later version. */
32480 eputf("Input nesting limit (%d) reached at line %d."
32481 " Check recursion.\n", MAX_INPUT_NESTING, p->lineno);
32482 return 1;
32483 }
32484 ++p->inputNesting;
32485 p->lineno = 0;
@@ -32487,11 +32035,11 @@
32487 while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
32488 fflush(p->out);
32489 zLine = one_input_line(p->in, zLine, nSql>0);
32490 if( zLine==0 ){
32491 /* End of input */
32492 if( p->in==0 && stdin_is_interactive ) oputz("\n");
32493 break;
32494 }
32495 if( seenInterrupt ){
32496 if( p->in!=0 ) break;
32497 seenInterrupt = 0;
@@ -32707,19 +32255,19 @@
32707 }
32708 zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
32709 shell_check_oom(zBuf);
32710 sqliterc = zBuf;
32711 }
32712 p->in = fopen(sqliterc,"rb");
32713 if( p->in ){
32714 if( stdin_is_interactive ){
32715 eputf("-- Loading resources from %s\n", sqliterc);
32716 }
32717 if( process_input(p) && bail_on_error ) exit(1);
32718 fclose(p->in);
32719 }else if( sqliterc_override!=0 ){
32720 eputf("cannot open: \"%s\"\n", sqliterc);
32721 if( bail_on_error ) exit(1);
32722 }
32723 p->in = inSaved;
32724 p->lineno = savedLineno;
32725 sqlite3_free(zBuf);
@@ -32790,15 +32338,15 @@
32790 #ifdef SQLITE_HAVE_ZLIB
32791 " -zip open the file as a ZIP Archive\n"
32792 #endif
32793 ;
32794 static void usage(int showDetail){
32795 eputf("Usage: %s [OPTIONS] [FILENAME [SQL]]\n"
32796 "FILENAME is the name of an SQLite database. A new database is created\n"
32797 "if the file does not previously exist. Defaults to :memory:.\n", Argv0);
32798 if( showDetail ){
32799 eputf("OPTIONS include:\n%s", zOptions);
32800 }else{
32801 eputz("Use the -help option for additional information\n");
32802 }
32803 exit(0);
32804 }
@@ -32854,21 +32402,22 @@
32854 SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
32855 #endif
32856 }
32857 #else
32858 static void printBold(const char *zText){
32859 sputf(stdout, "\033[1m%s\033[0m", zText);
32860 }
32861 #endif
32862
32863 /*
32864 ** Get the argument to an --option. Throw an error and die if no argument
32865 ** is available.
32866 */
32867 static char *cmdline_option_value(int argc, char **argv, int i){
32868 if( i==argc ){
32869 eputf("%s: Error: missing argument to %s\n", argv[0], argv[argc-1]);
 
32870 exit(1);
32871 }
32872 return argv[i];
32873 }
32874
@@ -32901,11 +32450,10 @@
32901 char *zErrMsg = 0;
32902 #ifdef SQLITE_SHELL_FIDDLE
32903 # define data shellState
32904 #else
32905 ShellState data;
32906 StreamsAreConsole consStreams = SAC_NoConsole;
32907 #endif
32908 const char *zInitFile = 0;
32909 int i;
32910 int rc = 0;
32911 int warnInmemoryDb = 0;
@@ -32924,25 +32472,25 @@
32924 #ifdef SQLITE_SHELL_FIDDLE
32925 stdin_is_interactive = 0;
32926 stdout_is_console = 1;
32927 data.wasm.zDefaultDbName = "/fiddle.sqlite3";
32928 #else
32929 consStreams = consoleClassifySetup(stdin, stdout, stderr);
32930 stdin_is_interactive = (consStreams & SAC_InConsole)!=0;
32931 stdout_is_console = (consStreams & SAC_OutConsole)!=0;
32932 atexit(consoleRestore);
32933 #endif
32934 atexit(sayAbnormalExit);
32935 #ifdef SQLITE_DEBUG
32936 mem_main_enter = sqlite3_memory_used();
32937 #endif
32938 #if !defined(_WIN32_WCE)
32939 if( getenv("SQLITE_DEBUG_BREAK") ){
32940 if( isatty(0) && isatty(2) ){
32941 eputf("attach debugger to process %d and press any key to continue.\n",
 
 
32942 GETPID());
32943 fgetc(stdin);
32944 }else{
32945 #if defined(_WIN32) || defined(WIN32)
32946 #if SQLITE_OS_WINRT
32947 __debugbreak();
32948 #else
@@ -32963,11 +32511,12 @@
32963 }
32964 #endif
32965
32966 #if USE_SYSTEM_SQLITE+0!=1
32967 if( cli_strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
32968 eputf("SQLite header and source version mismatch\n%s\n%s\n",
 
32969 sqlite3_sourceid(), SQLITE_SOURCE_ID);
32970 exit(1);
32971 }
32972 #endif
32973 main_init(&data);
@@ -33106,11 +32655,12 @@
33106 case 0: sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); break;
33107 case 2: sqlite3_config(SQLITE_CONFIG_MULTITHREAD); break;
33108 default: sqlite3_config(SQLITE_CONFIG_SERIALIZED); break;
33109 }
33110 }else if( cli_strcmp(z,"-vfstrace")==0 ){
33111 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
 
33112 bEnableVfstrace = 1;
33113 #ifdef SQLITE_ENABLE_MULTIPLEX
33114 }else if( cli_strcmp(z,"-multiplex")==0 ){
33115 extern int sqlite3_multiplex_initialize(const char*,int);
33116 sqlite3_multiplex_initialize(0, 1);
@@ -33187,21 +32737,22 @@
33187 if( zVfs ){
33188 sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
33189 if( pVfs ){
33190 sqlite3_vfs_register(pVfs, 1);
33191 }else{
33192 eputf("no such VFS: \"%s\"\n", zVfs);
33193 exit(1);
33194 }
33195 }
33196
33197 if( data.pAuxDb->zDbFilename==0 ){
33198 #ifndef SQLITE_OMIT_MEMORYDB
33199 data.pAuxDb->zDbFilename = ":memory:";
33200 warnInmemoryDb = argc==1;
33201 #else
33202 eputf("%s: Error: no database filename specified\n", Argv0);
 
33203 return 1;
33204 #endif
33205 }
33206 data.out = stdout;
33207 #ifndef SQLITE_SHELL_FIDDLE
@@ -33314,11 +32865,11 @@
33314 */
33315 ShellSetFlag(&data, SHFLG_Backslash);
33316 }else if( cli_strcmp(z,"-bail")==0 ){
33317 /* No-op. The bail_on_error flag should already be set. */
33318 }else if( cli_strcmp(z,"-version")==0 ){
33319 sputf(stdout, "%s %s (%d-bit)\n",
33320 sqlite3_libversion(), sqlite3_sourceid(), 8*(int)sizeof(char*));
33321 return 0;
33322 }else if( cli_strcmp(z,"-interactive")==0 ){
33323 /* Need to check for interactive override here to so that it can
33324 ** affect console setup (for Windows only) and testing thereof.
@@ -33377,18 +32928,18 @@
33377 rc = shell_exec(&data, z, &zErrMsg);
33378 if( zErrMsg!=0 ){
33379 shellEmitError(zErrMsg);
33380 if( bail_on_error ) return rc!=0 ? rc : 1;
33381 }else if( rc!=0 ){
33382 eputf("Error: unable to process SQL \"%s\"\n", z);
33383 if( bail_on_error ) return rc;
33384 }
33385 }
33386 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
33387 }else if( cli_strncmp(z, "-A", 2)==0 ){
33388 if( nCmd>0 ){
33389 eputf("Error: cannot mix regular SQL or dot-commands"
33390 " with \"%s\"\n", z);
33391 return 1;
33392 }
33393 open_db(&data, OPEN_DB_ZIPFILE);
33394 if( z[2] ){
@@ -33403,11 +32954,11 @@
33403 }else if( cli_strcmp(z,"-safe")==0 ){
33404 data.bSafeMode = data.bSafeModePersist = 1;
33405 }else if( cli_strcmp(z,"-unsafe-testing")==0 ){
33406 /* Acted upon in first pass. */
33407 }else{
33408 eputf("%s: Error: unknown option: %s\n", Argv0, z);
33409 eputz("Use -help for a list of options.\n");
33410 return 1;
33411 }
33412 data.cMode = data.mode;
33413 }
@@ -33430,11 +32981,12 @@
33430 rc = shell_exec(&data, azCmd[i], &zErrMsg);
33431 if( zErrMsg || rc ){
33432 if( zErrMsg!=0 ){
33433 shellEmitError(zErrMsg);
33434 }else{
33435 eputf("Error: unable to process SQL: %s\n", azCmd[i]);
 
33436 }
33437 sqlite3_free(zErrMsg);
33438 if( rc==0 ) rc = 1;
33439 goto shell_main_exit;
33440 }
@@ -33450,11 +33002,12 @@
33450 #if CIO_WIN_WC_XLATE
33451 # define SHELL_CIO_CHAR_SET (stdout_is_console? " (UTF-16 console I/O)" : "")
33452 #else
33453 # define SHELL_CIO_CHAR_SET ""
33454 #endif
33455 sputf(stdout, "SQLite version %s %.19s%s\n" /*extra-version-info*/
 
33456 "Enter \".help\" for usage hints.\n",
33457 sqlite3_libversion(), sqlite3_sourceid(), SHELL_CIO_CHAR_SET);
33458 if( warnInmemoryDb ){
33459 sputz(stdout, "Connected to a ");
33460 printBold("transient in-memory database");
@@ -33526,11 +33079,11 @@
33526 if( bEnableVfstrace ){
33527 vfstrace_unregister("trace");
33528 }
33529 #ifdef SQLITE_DEBUG
33530 if( sqlite3_memory_used()>mem_main_enter ){
33531 eputf("Memory leaked: %u bytes\n",
33532 (unsigned int)(sqlite3_memory_used()-mem_main_enter));
33533 }
33534 #endif
33535 #else /* SQLITE_SHELL_FIDDLE... */
33536 shell_main_exit:
@@ -33566,11 +33119,11 @@
33566 return pVfs;
33567 }
33568
33569 /* Only for emcc experimentation purposes. */
33570 sqlite3 * fiddle_db_arg(sqlite3 *arg){
33571 oputf("fiddle_db_arg(%p)\n", (const void*)arg);
33572 return arg;
33573 }
33574
33575 /*
33576 ** Intended to be called via a SharedWorker() while a separate
@@ -33603,11 +33156,11 @@
33603 while( sqlite3_txn_state(globalDb,0)>0 ){
33604 /*
33605 ** Resolve problem reported in
33606 ** https://sqlite.org/forum/forumpost/0b41a25d65
33607 */
33608 oputz("Rolling back in-progress transaction.\n");
33609 sqlite3_exec(globalDb,"ROLLBACK", 0, 0, 0);
33610 }
33611 rc = sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
33612 if( 0==rc ) sqlite3_exec(globalDb, "VACUUM", 0, 0, 0);
33613 sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
33614
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -208,12 +208,10 @@
208 # define unlink _unlink
209 # endif
210 # ifndef strdup
211 # define strdup _strdup
212 # endif
 
 
213 # undef pclose
214 # define pclose _pclose
215 # endif
216 #else
217 /* Make sure isatty() has a prototype. */
@@ -253,10 +251,283 @@
251 /* string conversion routines only needed on Win32 */
252 extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
253 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
254 #endif
255
256 /************************* Begin ../ext/misc/sqlite3_stdio.h ******************/
257 /*
258 ** 2024-09-24
259 **
260 ** The author disclaims copyright to this source code. In place of
261 ** a legal notice, here is a blessing:
262 **
263 ** May you do good and not evil.
264 ** May you find forgiveness for yourself and forgive others.
265 ** May you share freely, never taking more than you give.
266 **
267 *************************************************************************
268 **
269 ** This header file contains definitions of interfaces that provide
270 ** cross-platform I/O for UTF-8 content.
271 **
272 ** On most platforms, the interfaces definitions in this file are
273 ** just #defines. For example sqlite3_fopen() is a macro that resolves
274 ** to the standard fopen() in the C-library.
275 **
276 ** But Windows does not have a standard C-library, at least not one that
277 ** can handle UTF-8. So for windows build, the interfaces resolve to new
278 ** C-language routines contained in the separate sqlite3_stdio.c source file.
279 **
280 ** So on all non-Windows platforms, simply #include this header file and
281 ** use the interfaces defined herein. Then to run your application on Windows,
282 ** also link in the accompanying sqlite3_stdio.c source file when compiling
283 ** to get compatible interfaces.
284 */
285 #ifndef _SQLITE3_STDIO_H_
286 #define _SQLITE3_STDIO_H_ 1
287 #ifdef _WIN32
288 /**** Definitions For Windows ****/
289 #include <stdio.h>
290 #include <windows.h>
291
292 FILE *sqlite3_fopen(const char *zFilename, const char *zMode);
293 FILE *sqlite3_popen(const char *zCommand, const char *type);
294 char *sqlite3_fgets(char *s, int size, FILE *stream);
295 int sqlite3_fputs(const char *s, FILE *stream);
296 int sqlite3_fprintf(FILE *stream, const char *format, ...);
297 void sqlite3_fsetmode(FILE *stream, int mode);
298
299
300 #else
301 /**** Definitions For All Other Platforms ****/
302 #include <stdio.h>
303 #define sqlite3_fopen fopen
304 #define sqlite3_popen popen
305 #define sqlite3_fgets fgets
306 #define sqlite3_fputs fputs
307 #define sqlite3_fprintf fprintf
308 #define sqlite3_fsetmode(F,X) /*no-op*/
309
310 #endif
311 #endif /* _SQLITE3_STDIO_H_ */
312
313 /************************* End ../ext/misc/sqlite3_stdio.h ********************/
314 /************************* Begin ../ext/misc/sqlite3_stdio.c ******************/
315 /*
316 ** 2024-09-24
317 **
318 ** The author disclaims copyright to this source code. In place of
319 ** a legal notice, here is a blessing:
320 **
321 ** May you do good and not evil.
322 ** May you find forgiveness for yourself and forgive others.
323 ** May you share freely, never taking more than you give.
324 **
325 *************************************************************************
326 **
327 ** Implementation of standard I/O interfaces for UTF-8 that are missing
328 ** on Windows.
329 */
330 #ifdef _WIN32 /* This file is a no-op on all platforms except Windows */
331 #ifndef _SQLITE3_STDIO_H_
332 /* #include "sqlite3_stdio.h" */
333 #endif
334 #undef WIN32_LEAN_AND_MEAN
335 #define WIN32_LEAN_AND_MEAN
336 #include <windows.h>
337 #include <stdlib.h>
338 #include <string.h>
339 #include <stdio.h>
340 #include <assert.h>
341 /* #include "sqlite3.h" */
342 #include <ctype.h>
343 #include <stdarg.h>
344 #include <io.h>
345 #include <fcntl.h>
346
347 /*
348 ** If the SQLITE_U8TEXT_ONLY option is defined, then only use
349 ** _O_U8TEXT, _O_WTEXT, and similar together with the UTF-16
350 ** interfaces to the Windows CRT. The use of ANSI-only routines
351 ** like fputs() and ANSI modes like _O_TEXT and _O_BINARY is
352 ** avoided.
353 **
354 ** The downside of using SQLITE_U8TEXT_ONLY is that it becomes
355 ** impossible to output a bare newline character (0x0a) - that is,
356 ** a newline that is not preceded by a carriage return (0x0d).
357 ** And without that capability, sometimes the output will be slightly
358 ** incorrect, as extra 0x0d characters will have been inserted where
359 ** they do not belong.
360 **
361 ** The SQLITE_U8TEXT_STDIO compile-time option is a compromise.
362 ** It always enables _O_WTEXT or similar for stdin, stdout, stderr,
363 ** but allows other streams to be _O_TEXT and/or O_BINARY. The
364 ** SQLITE_U8TEXT_STDIO option has the same downside as SQLITE_U8TEXT_ONLY
365 ** in that stray 0x0d characters might appear where they ought not, but
366 ** at least with this option those characters only appear on standard
367 ** I/O streams, and not on new streams that might be created by the
368 ** application using sqlite3_fopen() or sqlite3_popen().
369 */
370 #if defined(SQLITE_U8TEXT_ONLY)
371 # define UseWtextForOutput(fd) 1
372 # define UseWtextForInput(fd) 1
373 # define IsConsole(fd) _isatty(_fileno(fd))
374 #elif defined(SQLITE_U8TEXT_STDIO)
375 # define UseWtextForOutput(fd) ((fd)==stdout || (fd)==stderr)
376 # define UseWtextForInput(fd) ((fd)==stdin)
377 # define IsConsole(fd) _isatty(_fileno(fd))
378 #else
379 # define UseWtextForOutput(fd) _isatty(_fileno(fd))
380 # define UseWtextForInput(fd) _isatty(_fileno(fd))
381 # define IsConsole(fd) 1
382 #endif
383
384 /*
385 ** Work-alike for the fopen() routine from the standard C library.
386 */
387 FILE *sqlite3_fopen(const char *zFilename, const char *zMode){
388 FILE *fp = 0;
389 wchar_t *b1, *b2;
390 int sz1, sz2;
391
392 sz1 = (int)strlen(zFilename);
393 sz2 = (int)strlen(zMode);
394 b1 = malloc( (sz1+1)*sizeof(b1[0]) );
395 b2 = malloc( (sz2+1)*sizeof(b1[0]) );
396 if( b1 && b2 ){
397 sz1 = MultiByteToWideChar(CP_UTF8, 0, zFilename, sz1, b1, sz1);
398 b1[sz1] = 0;
399 sz2 = MultiByteToWideChar(CP_UTF8, 0, zMode, sz2, b2, sz2);
400 b2[sz2] = 0;
401 fp = _wfopen(b1, b2);
402 }
403 free(b1);
404 free(b2);
405 return fp;
406 }
407
408
409 /*
410 ** Work-alike for the popen() routine from the standard C library.
411 */
412 FILE *sqlite3_popen(const char *zCommand, const char *zMode){
413 FILE *fp = 0;
414 wchar_t *b1, *b2;
415 int sz1, sz2;
416
417 sz1 = (int)strlen(zCommand);
418 sz2 = (int)strlen(zMode);
419 b1 = malloc( (sz1+1)*sizeof(b1[0]) );
420 b2 = malloc( (sz2+1)*sizeof(b1[0]) );
421 if( b1 && b2 ){
422 sz1 = MultiByteToWideChar(CP_UTF8, 0, zCommand, sz1, b1, sz1);
423 b1[sz1] = 0;
424 sz2 = MultiByteToWideChar(CP_UTF8, 0, zMode, sz2, b2, sz2);
425 b2[sz2] = 0;
426 fp = _wpopen(b1, b2);
427 }
428 free(b1);
429 free(b2);
430 return fp;
431 }
432
433 /*
434 ** Work-alike for fgets() from the standard C library.
435 */
436 char *sqlite3_fgets(char *buf, int sz, FILE *in){
437 if( UseWtextForInput(in) ){
438 /* When reading from the command-prompt in Windows, it is necessary
439 ** to use _O_WTEXT input mode to read UTF-16 characters, then translate
440 ** that into UTF-8. Otherwise, non-ASCII characters all get translated
441 ** into '?'.
442 */
443 wchar_t *b1 = malloc( sz*sizeof(wchar_t) );
444 if( b1==0 ) return 0;
445 _setmode(_fileno(in), IsConsole(in) ? _O_WTEXT : _O_U8TEXT);
446 if( fgetws(b1, sz/4, in)==0 ){
447 sqlite3_free(b1);
448 return 0;
449 }
450 WideCharToMultiByte(CP_UTF8, 0, b1, -1, buf, sz, 0, 0);
451 sqlite3_free(b1);
452 return buf;
453 }else{
454 /* Reading from a file or other input source, just read bytes without
455 ** any translation. */
456 return fgets(buf, sz, in);
457 }
458 }
459
460 /*
461 ** Work-alike for fputs() from the standard C library.
462 */
463 int sqlite3_fputs(const char *z, FILE *out){
464 if( UseWtextForOutput(out) ){
465 /* When writing to the command-prompt in Windows, it is necessary
466 ** to use _O_WTEXT input mode and write UTF-16 characters.
467 */
468 int sz = (int)strlen(z);
469 wchar_t *b1 = malloc( (sz+1)*sizeof(wchar_t) );
470 if( b1==0 ) return 0;
471 sz = MultiByteToWideChar(CP_UTF8, 0, z, sz, b1, sz);
472 b1[sz] = 0;
473 _setmode(_fileno(out), _O_U8TEXT);
474 fputws(b1, out);
475 sqlite3_free(b1);
476 return 0;
477 }else{
478 /* Writing to a file or other destination, just write bytes without
479 ** any translation. */
480 return fputs(z, out);
481 }
482 }
483
484
485 /*
486 ** Work-alike for fprintf() from the standard C library.
487 */
488 int sqlite3_fprintf(FILE *out, const char *zFormat, ...){
489 int rc;
490 if( UseWtextForOutput(out) ){
491 /* When writing to the command-prompt in Windows, it is necessary
492 ** to use _O_WTEXT input mode and write UTF-16 characters.
493 */
494 char *z;
495 va_list ap;
496
497 va_start(ap, zFormat);
498 z = sqlite3_vmprintf(zFormat, ap);
499 va_end(ap);
500 sqlite3_fputs(z, out);
501 rc = (int)strlen(z);
502 sqlite3_free(z);
503 }else{
504 /* Writing to a file or other destination, just write bytes without
505 ** any translation. */
506 va_list ap;
507 va_start(ap, zFormat);
508 rc = vfprintf(out, zFormat, ap);
509 va_end(ap);
510 }
511 return rc;
512 }
513
514 /*
515 ** Set the mode for an output stream. mode argument is typically _O_BINARY or
516 ** _O_TEXT.
517 */
518 void sqlite3_fsetmode(FILE *fp, int mode){
519 if( !UseWtextForOutput(fp) ){
520 fflush(fp);
521 _setmode(_fileno(fp), mode);
522 }
523 }
524
525 #endif /* defined(_WIN32) */
526
527 /************************* End ../ext/misc/sqlite3_stdio.c ********************/
528
529 /* Use console I/O package as a direct INCLUDE. */
530 #define SQLITE_INTERNAL_LINKAGE static
531
532 #ifdef SQLITE_SHELL_FIDDLE
533 /* Deselect most features from the console I/O package for Fiddle. */
@@ -264,1125 +535,13 @@
535 # define SQLITE_CIO_NO_CLASSIFY
536 # define SQLITE_CIO_NO_TRANSLATE
537 # define SQLITE_CIO_NO_SETMODE
538 # define SQLITE_CIO_NO_FLUSH
539 #endif
540
541 #define eputz(z) sqlite3_fputs(z,stderr)
542 #define sputz(fp,z) sqlite3_fputs(z,fp)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
543
544 /* True if the timer is enabled */
545 static int enableTimer = 0;
546
547 /* A version of strcmp() that works with NULL values */
@@ -1423,10 +582,11 @@
582 struct timeval ru_utime; /* user CPU time used */
583 struct timeval ru_stime; /* system CPU time used */
584 };
585 #define getrusage(A,B) memset(B,0,sizeof(*B))
586 #endif
587
588
589 /* Saved resource information for the beginning of an operation */
590 static struct rusage sBegin; /* CPU time at start */
591 static sqlite3_int64 iBegin; /* Wall-clock time at start */
592
@@ -1447,24 +607,24 @@
607 }
608
609 /*
610 ** Print the timing results.
611 */
612 static void endTimer(FILE *out){
613 if( enableTimer ){
614 sqlite3_int64 iEnd = timeOfDay();
615 struct rusage sEnd;
616 getrusage(RUSAGE_SELF, &sEnd);
617 sqlite3_fprintf(out, "Run Time: real %.3f user %f sys %f\n",
618 (iEnd - iBegin)*0.001,
619 timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
620 timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
621 }
622 }
623
624 #define BEGIN_TIMER beginTimer()
625 #define END_TIMER(X) endTimer(X)
626 #define HAS_TIMER 1
627
628 #elif (defined(_WIN32) || defined(WIN32))
629
630 /* Saved resource information for the beginning of an operation */
@@ -1526,29 +686,29 @@
686 }
687
688 /*
689 ** Print the timing results.
690 */
691 static void endTimer(FILE *out){
692 if( enableTimer && getProcessTimesAddr){
693 FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
694 sqlite3_int64 ftWallEnd = timeOfDay();
695 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
696 sqlite3_fprintf(out, "Run Time: real %.3f user %f sys %f\n",
697 (ftWallEnd - ftWallBegin)*0.001,
698 timeDiff(&ftUserBegin, &ftUserEnd),
699 timeDiff(&ftKernelBegin, &ftKernelEnd));
700 }
701 }
702
703 #define BEGIN_TIMER beginTimer()
704 #define END_TIMER(X) endTimer(X)
705 #define HAS_TIMER hasTimer()
706
707 #else
708 #define BEGIN_TIMER
709 #define END_TIMER(X) /*no-op*/
710 #define HAS_TIMER 0
711 #endif
712
713 /*
714 ** Used to prevent warnings about unused parameters
@@ -1745,41 +905,219 @@
905 char *z;
906 if( iotrace==0 ) return;
907 va_start(ap, zFormat);
908 z = sqlite3_vmprintf(zFormat, ap);
909 va_end(ap);
910 sqlite3_fprintf(iotrace, "%s", z);
911 sqlite3_free(z);
912 }
913 #endif
914
915 /* Lookup table to estimate the number of columns consumed by a Unicode
916 ** character.
917 */
918 static const struct {
919 unsigned char w; /* Width of the character in columns */
920 int iFirst; /* First character in a span having this width */
921 } aUWidth[] = {
922 /* {0, 0x00000}, {1, 0x00020}, {0, 0x0007f}, {1, 0x000a0}, */
923 {0, 0x00300}, {1, 0x00370}, {0, 0x00483}, {1, 0x00487}, {0, 0x00488},
924 {1, 0x0048a}, {0, 0x00591}, {1, 0x005be}, {0, 0x005bf}, {1, 0x005c0},
925 {0, 0x005c1}, {1, 0x005c3}, {0, 0x005c4}, {1, 0x005c6}, {0, 0x005c7},
926 {1, 0x005c8}, {0, 0x00600}, {1, 0x00604}, {0, 0x00610}, {1, 0x00616},
927 {0, 0x0064b}, {1, 0x0065f}, {0, 0x00670}, {1, 0x00671}, {0, 0x006d6},
928 {1, 0x006e5}, {0, 0x006e7}, {1, 0x006e9}, {0, 0x006ea}, {1, 0x006ee},
929 {0, 0x0070f}, {1, 0x00710}, {0, 0x00711}, {1, 0x00712}, {0, 0x00730},
930 {1, 0x0074b}, {0, 0x007a6}, {1, 0x007b1}, {0, 0x007eb}, {1, 0x007f4},
931 {0, 0x00901}, {1, 0x00903}, {0, 0x0093c}, {1, 0x0093d}, {0, 0x00941},
932 {1, 0x00949}, {0, 0x0094d}, {1, 0x0094e}, {0, 0x00951}, {1, 0x00955},
933 {0, 0x00962}, {1, 0x00964}, {0, 0x00981}, {1, 0x00982}, {0, 0x009bc},
934 {1, 0x009bd}, {0, 0x009c1}, {1, 0x009c5}, {0, 0x009cd}, {1, 0x009ce},
935 {0, 0x009e2}, {1, 0x009e4}, {0, 0x00a01}, {1, 0x00a03}, {0, 0x00a3c},
936 {1, 0x00a3d}, {0, 0x00a41}, {1, 0x00a43}, {0, 0x00a47}, {1, 0x00a49},
937 {0, 0x00a4b}, {1, 0x00a4e}, {0, 0x00a70}, {1, 0x00a72}, {0, 0x00a81},
938 {1, 0x00a83}, {0, 0x00abc}, {1, 0x00abd}, {0, 0x00ac1}, {1, 0x00ac6},
939 {0, 0x00ac7}, {1, 0x00ac9}, {0, 0x00acd}, {1, 0x00ace}, {0, 0x00ae2},
940 {1, 0x00ae4}, {0, 0x00b01}, {1, 0x00b02}, {0, 0x00b3c}, {1, 0x00b3d},
941 {0, 0x00b3f}, {1, 0x00b40}, {0, 0x00b41}, {1, 0x00b44}, {0, 0x00b4d},
942 {1, 0x00b4e}, {0, 0x00b56}, {1, 0x00b57}, {0, 0x00b82}, {1, 0x00b83},
943 {0, 0x00bc0}, {1, 0x00bc1}, {0, 0x00bcd}, {1, 0x00bce}, {0, 0x00c3e},
944 {1, 0x00c41}, {0, 0x00c46}, {1, 0x00c49}, {0, 0x00c4a}, {1, 0x00c4e},
945 {0, 0x00c55}, {1, 0x00c57}, {0, 0x00cbc}, {1, 0x00cbd}, {0, 0x00cbf},
946 {1, 0x00cc0}, {0, 0x00cc6}, {1, 0x00cc7}, {0, 0x00ccc}, {1, 0x00cce},
947 {0, 0x00ce2}, {1, 0x00ce4}, {0, 0x00d41}, {1, 0x00d44}, {0, 0x00d4d},
948 {1, 0x00d4e}, {0, 0x00dca}, {1, 0x00dcb}, {0, 0x00dd2}, {1, 0x00dd5},
949 {0, 0x00dd6}, {1, 0x00dd7}, {0, 0x00e31}, {1, 0x00e32}, {0, 0x00e34},
950 {1, 0x00e3b}, {0, 0x00e47}, {1, 0x00e4f}, {0, 0x00eb1}, {1, 0x00eb2},
951 {0, 0x00eb4}, {1, 0x00eba}, {0, 0x00ebb}, {1, 0x00ebd}, {0, 0x00ec8},
952 {1, 0x00ece}, {0, 0x00f18}, {1, 0x00f1a}, {0, 0x00f35}, {1, 0x00f36},
953 {0, 0x00f37}, {1, 0x00f38}, {0, 0x00f39}, {1, 0x00f3a}, {0, 0x00f71},
954 {1, 0x00f7f}, {0, 0x00f80}, {1, 0x00f85}, {0, 0x00f86}, {1, 0x00f88},
955 {0, 0x00f90}, {1, 0x00f98}, {0, 0x00f99}, {1, 0x00fbd}, {0, 0x00fc6},
956 {1, 0x00fc7}, {0, 0x0102d}, {1, 0x01031}, {0, 0x01032}, {1, 0x01033},
957 {0, 0x01036}, {1, 0x01038}, {0, 0x01039}, {1, 0x0103a}, {0, 0x01058},
958 {1, 0x0105a}, {2, 0x01100}, {0, 0x01160}, {1, 0x01200}, {0, 0x0135f},
959 {1, 0x01360}, {0, 0x01712}, {1, 0x01715}, {0, 0x01732}, {1, 0x01735},
960 {0, 0x01752}, {1, 0x01754}, {0, 0x01772}, {1, 0x01774}, {0, 0x017b4},
961 {1, 0x017b6}, {0, 0x017b7}, {1, 0x017be}, {0, 0x017c6}, {1, 0x017c7},
962 {0, 0x017c9}, {1, 0x017d4}, {0, 0x017dd}, {1, 0x017de}, {0, 0x0180b},
963 {1, 0x0180e}, {0, 0x018a9}, {1, 0x018aa}, {0, 0x01920}, {1, 0x01923},
964 {0, 0x01927}, {1, 0x01929}, {0, 0x01932}, {1, 0x01933}, {0, 0x01939},
965 {1, 0x0193c}, {0, 0x01a17}, {1, 0x01a19}, {0, 0x01b00}, {1, 0x01b04},
966 {0, 0x01b34}, {1, 0x01b35}, {0, 0x01b36}, {1, 0x01b3b}, {0, 0x01b3c},
967 {1, 0x01b3d}, {0, 0x01b42}, {1, 0x01b43}, {0, 0x01b6b}, {1, 0x01b74},
968 {0, 0x01dc0}, {1, 0x01dcb}, {0, 0x01dfe}, {1, 0x01e00}, {0, 0x0200b},
969 {1, 0x02010}, {0, 0x0202a}, {1, 0x0202f}, {0, 0x02060}, {1, 0x02064},
970 {0, 0x0206a}, {1, 0x02070}, {0, 0x020d0}, {1, 0x020f0}, {2, 0x02329},
971 {1, 0x0232b}, {2, 0x02e80}, {0, 0x0302a}, {2, 0x03030}, {1, 0x0303f},
972 {2, 0x03040}, {0, 0x03099}, {2, 0x0309b}, {1, 0x0a4d0}, {0, 0x0a806},
973 {1, 0x0a807}, {0, 0x0a80b}, {1, 0x0a80c}, {0, 0x0a825}, {1, 0x0a827},
974 {2, 0x0ac00}, {1, 0x0d7a4}, {2, 0x0f900}, {1, 0x0fb00}, {0, 0x0fb1e},
975 {1, 0x0fb1f}, {0, 0x0fe00}, {2, 0x0fe10}, {1, 0x0fe1a}, {0, 0x0fe20},
976 {1, 0x0fe24}, {2, 0x0fe30}, {1, 0x0fe70}, {0, 0x0feff}, {2, 0x0ff00},
977 {1, 0x0ff61}, {2, 0x0ffe0}, {1, 0x0ffe7}, {0, 0x0fff9}, {1, 0x0fffc},
978 {0, 0x10a01}, {1, 0x10a04}, {0, 0x10a05}, {1, 0x10a07}, {0, 0x10a0c},
979 {1, 0x10a10}, {0, 0x10a38}, {1, 0x10a3b}, {0, 0x10a3f}, {1, 0x10a40},
980 {0, 0x1d167}, {1, 0x1d16a}, {0, 0x1d173}, {1, 0x1d183}, {0, 0x1d185},
981 {1, 0x1d18c}, {0, 0x1d1aa}, {1, 0x1d1ae}, {0, 0x1d242}, {1, 0x1d245},
982 {2, 0x20000}, {1, 0x2fffe}, {2, 0x30000}, {1, 0x3fffe}, {0, 0xe0001},
983 {1, 0xe0002}, {0, 0xe0020}, {1, 0xe0080}, {0, 0xe0100}, {1, 0xe01f0}
984 };
985
986 /*
987 ** Return an estimate of the width, in columns, for the single Unicode
988 ** character c. For normal characters, the answer is always 1. But the
989 ** estimate might be 0 or 2 for zero-width and double-width characters.
990 **
991 ** Different display devices display unicode using different widths. So
992 ** it is impossible to know that true display width with 100% accuracy.
993 ** Inaccuracies in the width estimates might cause columns to be misaligned.
994 ** Unfortunately, there is nothing we can do about that.
995 */
996 int cli_wcwidth(int c){
997 int iFirst, iLast;
998
999 /* Fast path for common characters */
1000 if( c<0x20 ) return 0;
1001 if( c<0x7f ) return 1;
1002 if( c<0xa0 ) return 0;
1003 if( c<=0x300 ) return 1;
1004
1005 /* The general case */
1006 iFirst = 0;
1007 iLast = sizeof(aUWidth)/sizeof(aUWidth[0]) - 1;
1008 while( iFirst<iLast-1 ){
1009 int iMid = (iFirst+iLast)/2;
1010 int cMid = aUWidth[iMid].iFirst;
1011 if( cMid < c ){
1012 iFirst = iMid;
1013 }else if( cMid > c ){
1014 iLast = iMid - 1;
1015 }else{
1016 return aUWidth[iMid].w;
1017 }
1018 }
1019 if( aUWidth[iLast].iFirst > c ) return aUWidth[iFirst].w;
1020 return aUWidth[iLast].w;
1021 }
1022
1023 /*
1024 ** Compute the value and length of a multi-byte UTF-8 character that
1025 ** begins at z[0]. Return the length. Write the Unicode value into *pU.
1026 **
1027 ** This routine only works for *multi-byte* UTF-8 characters.
1028 */
1029 static int decodeUtf8(const unsigned char *z, int *pU){
1030 if( (z[0] & 0xe0)==0xc0 && (z[1] & 0xc0)==0x80 ){
1031 *pU = ((z[0] & 0x1f)<<6) | (z[1] & 0x3f);
1032 return 2;
1033 }
1034 if( (z[0] & 0xf0)==0xe0 && (z[1] & 0xc0)==0x80 && (z[2] & 0xc0)==0x80 ){
1035 *pU = ((z[0] & 0x0f)<<12) | ((z[1] & 0x3f)<<6) | (z[2] & 0x3f);
1036 return 3;
1037 }
1038 if( (z[0] & 0xf8)==0xf0 && (z[1] & 0xc0)==0x80 && (z[2] & 0xc0)==0x80
1039 && (z[3] & 0xc0)==0x80
1040 ){
1041 *pU = ((z[0] & 0x0f)<<18) | ((z[1] & 0x3f)<<12) | ((z[2] & 0x3f))<<6
1042 | (z[4] & 0x3f);
1043 return 4;
1044 }
1045 *pU = 0;
1046 return 1;
1047 }
1048
1049
1050 #if 0 /* NOT USED */
1051 /*
1052 ** Return the width, in display columns, of a UTF-8 string.
1053 **
1054 ** Each normal character counts as 1. Zero-width characters count
1055 ** as zero, and double-width characters count as 2.
1056 */
1057 int cli_wcswidth(const char *z){
1058 const unsigned char *a = (const unsigned char*)z;
1059 int n = 0;
1060 int i = 0;
1061 unsigned char c;
1062 while( (c = a[i])!=0 ){
1063 if( c>=0xc0 ){
1064 int u;
1065 int len = decodeUtf8(&a[i], &u);
1066 i += len;
1067 n += cli_wcwidth(u);
1068 }else if( c>=' ' ){
1069 n++;
1070 i++;
1071 }else{
1072 i++;
1073 }
1074 }
1075 return n;
1076 }
1077 #endif
1078
1079 /*
1080 ** Output string zUtf to stdout as w characters. If w is negative,
1081 ** then right-justify the text. W is the width in UTF-8 characters, not
1082 ** in bytes. This is different from the %*.*s specification in printf
1083 ** since with %*.*s the width is measured in bytes, not characters.
1084 **
1085 ** Take into account zero-width and double-width Unicode characters.
1086 ** In other words, a zero-width character does not count toward the
1087 ** the w limit. A double-width character counts as two.
1088 */
1089 static void utf8_width_print(FILE *out, int w, const char *zUtf){
1090 const unsigned char *a = (const unsigned char*)zUtf;
1091 unsigned char c;
1092 int i = 0;
1093 int n = 0;
1094 int aw = w<0 ? -w : w;
1095 if( zUtf==0 ) zUtf = "";
1096 while( (c = a[i])!=0 ){
1097 if( (c&0xc0)==0xc0 ){
1098 int u;
1099 int len = decodeUtf8(a+i, &u);
1100 int x = cli_wcwidth(u);
1101 if( x+n>aw ){
1102 break;
1103 }
1104 i += len;
1105 n += x;
1106 }else if( n>=aw ){
1107 break;
1108 }else{
1109 n++;
1110 i++;
 
 
 
1111 }
1112 }
1113 if( n>=aw ){
1114 sqlite3_fprintf(out, "%.*s", i, zUtf);
1115 }else if( w<0 ){
1116 sqlite3_fprintf(out, "%*s%s", aw-n, "", zUtf);
1117 }else{
1118 sqlite3_fprintf(out, "%s%*s", zUtf, aw-n, "");
1119 }
1120 }
1121
1122
1123 /*
@@ -1841,11 +1179,11 @@
1179 struct __stat64 x = {0};
1180 # define STAT_CHR_SRC(mode) ((mode & (_S_IFCHR|_S_IFIFO|_S_IFREG))!=0)
1181 /* On Windows, open first, then check the stream nature. This order
1182 ** is necessary because _stat() and sibs, when checking a named pipe,
1183 ** effectively break the pipe as its supplier sees it. */
1184 FILE *rv = sqlite3_fopen(zFile, "rb");
1185 if( rv==0 ) return 0;
1186 if( _fstat64(_fileno(rv), &x) != 0
1187 || !STAT_CHR_SRC(x.st_mode)){
1188 fclose(rv);
1189 rv = 0;
@@ -1855,11 +1193,11 @@
1193 struct stat x = {0};
1194 int rc = stat(zFile, &x);
1195 # define STAT_CHR_SRC(mode) (S_ISREG(mode)||S_ISFIFO(mode)||S_ISCHR(mode))
1196 if( rc!=0 ) return 0;
1197 if( STAT_CHR_SRC(x.st_mode) ){
1198 return sqlite3_fopen(zFile, "rb");
1199 }else{
1200 return 0;
1201 }
1202 #endif
1203 #undef STAT_CHR_SRC
@@ -1882,11 +1220,11 @@
1220 if( n+100>nLine ){
1221 nLine = nLine*2 + 100;
1222 zLine = realloc(zLine, nLine);
1223 shell_check_oom(zLine);
1224 }
1225 if( sqlite3_fgets(&zLine[n], nLine - n, in)==0 ){
1226 if( n==0 ){
1227 free(zLine);
1228 return 0;
1229 }
1230 zLine[n] = 0;
@@ -8604,10 +7942,17 @@
7942 # define lstat(path,buf) stat(path,buf)
7943 #endif
7944 #include <time.h>
7945 #include <errno.h>
7946
7947 /* When used as part of the CLI, the sqlite3_stdio.h module will have
7948 ** been included before this one. In that case use the sqlite3_stdio.h
7949 ** #defines. If not, create our own for fopen().
7950 */
7951 #ifndef _SQLITE3_STDIO_H_
7952 # define sqlite3_fopen fopen
7953 #endif
7954
7955 /*
7956 ** Structure of the fsdir() table-valued function
7957 */
7958 /* 0 1 2 3 4 5 */
@@ -8636,11 +7981,11 @@
7981 sqlite3_int64 nIn;
7982 void *pBuf;
7983 sqlite3 *db;
7984 int mxBlob;
7985
7986 in = sqlite3_fopen(zName, "rb");
7987 if( in==0 ){
7988 /* File does not exist or is unreadable. Leave the result set to NULL. */
7989 return;
7990 }
7991 fseek(in, 0, SEEK_END);
@@ -8891,11 +8236,11 @@
8236 }
8237 }else{
8238 sqlite3_int64 nWrite = 0;
8239 const char *z;
8240 int rc = 0;
8241 FILE *out = sqlite3_fopen(zFile, "wb");
8242 if( out==0 ) return 1;
8243 z = (const char*)sqlite3_value_blob(pData);
8244 if( z ){
8245 sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
8246 nWrite = sqlite3_value_bytes(pData);
@@ -10752,10 +10097,18 @@
10097 #ifndef SQLITE_NO_STDINT
10098 # include <stdint.h>
10099 #endif
10100
10101 #include <zlib.h>
10102
10103 /* When used as part of the CLI, the sqlite3_stdio.h module will have
10104 ** been included before this one. In that case use the sqlite3_stdio.h
10105 ** #defines. If not, create our own for fopen().
10106 */
10107 #ifndef _SQLITE3_STDIO_H_
10108 # define sqlite3_fopen fopen
10109 #endif
10110
10111 #ifndef SQLITE_OMIT_VIRTUALTABLE
10112
10113 #ifndef SQLITE_AMALGAMATION
10114
@@ -12009,11 +11362,11 @@
11362 }else{
11363 zFile = (const char*)sqlite3_value_text(argv[0]);
11364 }
11365
11366 if( 0==pTab->pWriteFd && 0==bInMemory ){
11367 pCsr->pFile = zFile ? sqlite3_fopen(zFile, "rb") : 0;
11368 if( pCsr->pFile==0 ){
11369 zipfileCursorErr(pCsr, "cannot open file: %s", zFile);
11370 rc = SQLITE_ERROR;
11371 }else{
11372 rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd);
@@ -12199,11 +11552,11 @@
11552
11553 /* Open a write fd on the file. Also load the entire central directory
11554 ** structure into memory. During the transaction any new file data is
11555 ** appended to the archive file, but the central directory is accumulated
11556 ** in main-memory until the transaction is committed. */
11557 pTab->pWriteFd = sqlite3_fopen(pTab->zFile, "ab+");
11558 if( pTab->pWriteFd==0 ){
11559 pTab->base.zErrMsg = sqlite3_mprintf(
11560 "zipfile: failed to open file %s for writing", pTab->zFile
11561 );
11562 rc = SQLITE_ERROR;
@@ -14883,10 +14236,16 @@
14236 sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC);
14237 while( SQLITE_OK==rc && SQLITE_ROW==sqlite3_step(pIndexXInfo) ){
14238 const char *zComma = zCols==0 ? "" : ", ";
14239 const char *zName = (const char*)sqlite3_column_text(pIndexXInfo, 0);
14240 const char *zColl = (const char*)sqlite3_column_text(pIndexXInfo, 1);
14241 if( zName==0 ){
14242 /* This index contains an expression. Ignore it. */
14243 sqlite3_free(zCols);
14244 sqlite3_free(zOrder);
14245 return sqlite3_reset(pIndexXInfo);
14246 }
14247 zCols = idxAppendText(&rc, zCols,
14248 "%sx.%Q IS sqlite_expert_rem(%d, x.%Q) COLLATE %s",
14249 zComma, zName, nCol, zName, zColl
14250 );
14251 zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol);
@@ -21991,10 +21350,11 @@
21350 #define MODE_Table 15 /* MySQL-style table formatting */
21351 #define MODE_Box 16 /* Unicode box-drawing characters */
21352 #define MODE_Count 17 /* Output only a count of the rows of output */
21353 #define MODE_Off 18 /* No query output shown */
21354 #define MODE_ScanExp 19 /* Like MODE_Explain, but for ".scanstats vm" */
21355 #define MODE_Www 20 /* Full web-page output */
21356
21357 static const char *modeDescr[] = {
21358 "line",
21359 "column",
21360 "list",
@@ -22011,11 +21371,13 @@
21371 "json",
21372 "markdown",
21373 "table",
21374 "box",
21375 "count",
21376 "off",
21377 "scanexp",
21378 "www",
21379 };
21380
21381 /*
21382 ** These are the column/row/line separators used by the various
21383 ** import/export modes.
@@ -22023,11 +21385,17 @@
21385 #define SEP_Column "|"
21386 #define SEP_Row "\n"
21387 #define SEP_Tab "\t"
21388 #define SEP_Space " "
21389 #define SEP_Comma ","
21390 #ifdef SQLITE_U8TEXT_ONLY
21391 /* With the SQLITE_U8TEXT_ONLY option, the output will always be in
21392 ** text mode. The \r will be inserted automatically. */
21393 # define SEP_CrLf "\n"
21394 #else
21395 # define SEP_CrLf "\r\n"
21396 #endif
21397 #define SEP_Unit "\x1F"
21398 #define SEP_Record "\x1E"
21399
21400 /*
21401 ** Limit input nesting via .read or any other input redirect.
@@ -22039,11 +21407,11 @@
21407 ** A callback for the sqlite3_log() interface.
21408 */
21409 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
21410 ShellState *p = (ShellState*)pArg;
21411 if( p->pLog==0 ) return;
21412 sqlite3_fprintf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
21413 fflush(p->pLog);
21414 }
21415
21416 /*
21417 ** SQL function: shell_putsnl(X)
@@ -22054,13 +21422,13 @@
21422 static void shellPutsFunc(
21423 sqlite3_context *pCtx,
21424 int nVal,
21425 sqlite3_value **apVal
21426 ){
21427 ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
21428 (void)nVal;
21429 sqlite3_fprintf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
21430 sqlite3_result_value(pCtx, apVal[0]);
21431 }
21432
21433 /*
21434 ** If in safe mode, print an error message described by the arguments
@@ -22075,11 +21443,11 @@
21443 va_list ap;
21444 char *zMsg;
21445 va_start(ap, zErrMsg);
21446 zMsg = sqlite3_vmprintf(zErrMsg, ap);
21447 va_end(ap);
21448 sqlite3_fprintf(stderr, "line %d: %s\n", p->lineno, zMsg);
21449 exit(1);
21450 }
21451 }
21452
21453 /*
@@ -22142,11 +21510,11 @@
21510 }
21511 }
21512 bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
21513 /* When writing the file to be edited, do \n to \r\n conversions on systems
21514 ** that want \r\n line endings */
21515 f = sqlite3_fopen(zTempFile, bBin ? "wb" : "w");
21516 if( f==0 ){
21517 sqlite3_result_error(context, "edit() cannot open temp file", -1);
21518 goto edit_func_end;
21519 }
21520 sz = sqlite3_value_bytes(argv[0]);
@@ -22173,11 +21541,11 @@
21541 sqlite3_free(zCmd);
21542 if( rc ){
21543 sqlite3_result_error(context, "EDITOR returned non-zero", -1);
21544 goto edit_func_end;
21545 }
21546 f = sqlite3_fopen(zTempFile, "rb");
21547 if( f==0 ){
21548 sqlite3_result_error(context,
21549 "edit() cannot reopen temp file after edit", -1);
21550 goto edit_func_end;
21551 }
@@ -22243,11 +21611,11 @@
21611 }
21612
21613 /*
21614 ** Output the given string as a hex-encoded blob (eg. X'1234' )
21615 */
21616 static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
21617 int i;
21618 unsigned char *aBlob = (unsigned char*)pBlob;
21619
21620 char *zStr = sqlite3_malloc(nBlob*2 + 1);
21621 shell_check_oom(zStr);
@@ -22260,11 +21628,11 @@
21628 zStr[i*2] = aHex[ (aBlob[i] >> 4) ];
21629 zStr[i*2+1] = aHex[ (aBlob[i] & 0x0F) ];
21630 }
21631 zStr[i*2] = '\0';
21632
21633 sqlite3_fprintf(out, "X'%s'", zStr);
21634 sqlite3_free(zStr);
21635 }
21636
21637 /*
21638 ** Find a string that is not found anywhere in z[]. Return a pointer
@@ -22290,46 +21658,39 @@
21658 /*
21659 ** Output the given string as a quoted string using SQL quoting conventions.
21660 **
21661 ** See also: output_quoted_escaped_string()
21662 */
21663 static void output_quoted_string(FILE *out, const char *z){
21664 int i;
21665 char c;
21666 sqlite3_fsetmode(out, _O_BINARY);
 
 
 
21667 if( z==0 ) return;
21668 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
21669 if( c==0 ){
21670 sqlite3_fprintf(out, "'%s'",z);
21671 }else{
21672 sqlite3_fputs("'", out);
21673 while( *z ){
21674 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
21675 if( c=='\'' ) i++;
21676 if( i ){
21677 sqlite3_fprintf(out, "%.*s", i, z);
21678 z += i;
21679 }
21680 if( c=='\'' ){
21681 sqlite3_fputs("'", out);
21682 continue;
21683 }
21684 if( c==0 ){
21685 break;
21686 }
21687 z++;
21688 }
21689 sqlite3_fputs("'", out);
21690 }
21691 sqlite3_fsetmode(out, _O_TEXT);
 
 
 
 
21692 }
21693
21694 /*
21695 ** Output the given string as a quoted string using SQL quoting conventions.
21696 ** Additionallly , escape the "\n" and "\r" characters so that they do not
@@ -22337,20 +21698,17 @@
21698 ** systems.
21699 **
21700 ** This is like output_quoted_string() but with the addition of the \r\n
21701 ** escape mechanism.
21702 */
21703 static void output_quoted_escaped_string(FILE *out, const char *z){
21704 int i;
21705 char c;
21706 sqlite3_fsetmode(out, _O_BINARY);
 
 
 
21707 for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
21708 if( c==0 ){
21709 sqlite3_fprintf(out, "'%s'",z);
21710 }else{
21711 const char *zNL = 0;
21712 const char *zCR = 0;
21713 int nNL = 0;
21714 int nCR = 0;
@@ -22358,52 +21716,48 @@
21716 for(i=0; z[i]; i++){
21717 if( z[i]=='\n' ) nNL++;
21718 if( z[i]=='\r' ) nCR++;
21719 }
21720 if( nNL ){
21721 sqlite3_fputs("replace(", out);
21722 zNL = unused_string(z, "\\n", "\\012", zBuf1);
21723 }
21724 if( nCR ){
21725 sqlite3_fputs("replace(", out);
21726 zCR = unused_string(z, "\\r", "\\015", zBuf2);
21727 }
21728 sqlite3_fputs("'", out);
21729 while( *z ){
21730 for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
21731 if( c=='\'' ) i++;
21732 if( i ){
21733 sqlite3_fprintf(out, "%.*s", i, z);
21734 z += i;
21735 }
21736 if( c=='\'' ){
21737 sqlite3_fputs("'", out);
21738 continue;
21739 }
21740 if( c==0 ){
21741 break;
21742 }
21743 z++;
21744 if( c=='\n' ){
21745 sqlite3_fputs(zNL, out);
21746 continue;
21747 }
21748 sqlite3_fputs(zCR, out);
21749 }
21750 sqlite3_fputs("'", out);
21751 if( nCR ){
21752 sqlite3_fprintf(out, ",'%s',char(13))", zCR);
21753 }
21754 if( nNL ){
21755 sqlite3_fprintf(out, ",'%s',char(10))", zNL);
21756 }
21757 }
21758 sqlite3_fsetmode(stdout, _O_TEXT);
 
 
 
 
21759 }
21760
21761 /*
21762 ** Find earliest of chars within s specified in zAny.
21763 ** With ns == ~0, is like strpbrk(s,zAny) and s must be 0-terminated.
@@ -22419,26 +21773,64 @@
21773 }
21774 ++zAny;
21775 }
21776 return pcFirst;
21777 }
21778
21779 /* Skip over as much z[] input char sequence as is valid UTF-8,
21780 ** limited per nAccept char's or whole characters and containing
21781 ** no char cn such that ((1<<cn) & ccm)!=0. On return, the
21782 ** sequence z:return (inclusive:exclusive) is validated UTF-8.
21783 ** Limit: nAccept>=0 => char count, nAccept<0 => character
21784 */
21785 const char *zSkipValidUtf8(const char *z, int nAccept, long ccm){
21786 int ng = (nAccept<0)? -nAccept : 0;
21787 const char *pcLimit = (nAccept>=0)? z+nAccept : 0;
21788 assert(z!=0);
21789 while( (pcLimit)? (z<pcLimit) : (ng-- != 0) ){
21790 char c = *z;
21791 if( (c & 0x80) == 0 ){
21792 if( ccm != 0L && c < 0x20 && ((1L<<c) & ccm) != 0 ) return z;
21793 ++z; /* ASCII */
21794 }else if( (c & 0xC0) != 0xC0 ) return z; /* not a lead byte */
21795 else{
21796 const char *zt = z+1; /* Got lead byte, look at trail bytes.*/
21797 do{
21798 if( pcLimit && zt >= pcLimit ) return z;
21799 else{
21800 char ct = *zt++;
21801 if( ct==0 || (zt-z)>4 || (ct & 0xC0)!=0x80 ){
21802 /* Trailing bytes are too few, too many, or invalid. */
21803 return z;
21804 }
21805 }
21806 } while( ((c <<= 1) & 0x40) == 0x40 ); /* Eat lead byte's count. */
21807 z = zt;
21808 }
21809 }
21810 return z;
21811 }
21812
21813
21814 /*
21815 ** Output the given string as a quoted according to C or TCL quoting rules.
21816 */
21817 static void output_c_string(FILE *out, const char *z){
21818 char c;
21819 static const char *zq = "\"";
21820 static long ctrlMask = ~0L;
21821 static const char *zDQBSRO = "\"\\\x7f"; /* double-quote, backslash, rubout */
21822 char ace[3] = "\\?";
21823 char cbsSay;
21824 sqlite3_fputs(zq, out);
21825 while( *z!=0 ){
21826 const char *pcDQBSRO = anyOfInStr(z, zDQBSRO, ~(size_t)0);
21827 const char *pcPast = zSkipValidUtf8(z, INT_MAX, ctrlMask);
21828 const char *pcEnd = (pcDQBSRO && pcDQBSRO < pcPast)? pcDQBSRO : pcPast;
21829 if( pcEnd > z ){
21830 sqlite3_fprintf(out, "%.*s", (int)(pcEnd-z), z);
21831 }
21832 if( (c = *pcEnd)==0 ) break;
21833 ++pcEnd;
21834 switch( c ){
21835 case '\\': case '"':
21836 cbsSay = (char)c;
@@ -22449,26 +21841,26 @@
21841 case '\f': cbsSay = 'f'; break;
21842 default: cbsSay = 0; break;
21843 }
21844 if( cbsSay ){
21845 ace[1] = cbsSay;
21846 sqlite3_fputs(ace, out);
21847 }else if( !isprint(c&0xff) ){
21848 sqlite3_fprintf(out, "\\%03o", c&0xff);
21849 }else{
21850 ace[1] = (char)c;
21851 sqlite3_fputs(ace+1, out);
21852 }
21853 z = pcEnd;
21854 }
21855 sqlite3_fputs(zq, out);
21856 }
21857
21858 /*
21859 ** Output the given string as a quoted according to JSON quoting rules.
21860 */
21861 static void output_json_string(FILE *out, const char *z, i64 n){
21862 char c;
21863 static const char *zq = "\"";
21864 static long ctrlMask = ~0L;
21865 static const char *zDQBS = "\"\\";
21866 const char *pcLimit;
@@ -22475,17 +21867,17 @@
21867 char ace[3] = "\\?";
21868 char cbsSay;
21869
21870 if( z==0 ) z = "";
21871 pcLimit = z + ((n<0)? strlen(z) : (size_t)n);
21872 sqlite3_fputs(zq, out);
21873 while( z < pcLimit ){
21874 const char *pcDQBS = anyOfInStr(z, zDQBS, pcLimit-z);
21875 const char *pcPast = zSkipValidUtf8(z, (int)(pcLimit-z), ctrlMask);
21876 const char *pcEnd = (pcDQBS && pcDQBS < pcPast)? pcDQBS : pcPast;
21877 if( pcEnd > z ){
21878 sqlite3_fprintf(out, "%.*s", (int)(pcEnd-z), z);
21879 z = pcEnd;
21880 }
21881 if( z >= pcLimit ) break;
21882 c = *(z++);
21883 switch( c ){
@@ -22499,26 +21891,26 @@
21891 case '\t': cbsSay = 't'; break;
21892 default: cbsSay = 0; break;
21893 }
21894 if( cbsSay ){
21895 ace[1] = cbsSay;
21896 sqlite3_fputs(ace, out);
21897 }else if( c<=0x1f ){
21898 sqlite3_fprintf(out, "u%04x", c);
21899 }else{
21900 ace[1] = (char)c;
21901 sqlite3_fputs(ace+1, out);
21902 }
21903 }
21904 sqlite3_fputs(zq, out);
21905 }
21906
21907 /*
21908 ** Output the given string with characters that are special to
21909 ** HTML escaped.
21910 */
21911 static void output_html_string(FILE *out, const char *z){
21912 int i;
21913 if( z==0 ) z = "";
21914 while( *z ){
21915 for(i=0; z[i]
21916 && z[i]!='<'
@@ -22526,22 +21918,22 @@
21918 && z[i]!='>'
21919 && z[i]!='\"'
21920 && z[i]!='\'';
21921 i++){}
21922 if( i>0 ){
21923 sqlite3_fprintf(out, "%.*s",i,z);
21924 }
21925 if( z[i]=='<' ){
21926 sqlite3_fputs("&lt;", out);
21927 }else if( z[i]=='&' ){
21928 sqlite3_fputs("&amp;", out);
21929 }else if( z[i]=='>' ){
21930 sqlite3_fputs("&gt;", out);
21931 }else if( z[i]=='\"' ){
21932 sqlite3_fputs("&quot;", out);
21933 }else if( z[i]=='\'' ){
21934 sqlite3_fputs("&#39;", out);
21935 }else{
21936 break;
21937 }
21938 z += i + 1;
21939 }
@@ -22576,11 +21968,11 @@
21968 ** the null value. Strings are quoted if necessary. The separator
21969 ** is only issued if bSep is true.
21970 */
21971 static void output_csv(ShellState *p, const char *z, int bSep){
21972 if( z==0 ){
21973 sqlite3_fprintf(p->out, "%s",p->nullValue);
21974 }else{
21975 unsigned i;
21976 for(i=0; z[i]; i++){
21977 if( needCsvQuote[((unsigned char*)z)[i]] ){
21978 i = 0;
@@ -22588,18 +21980,18 @@
21980 }
21981 }
21982 if( i==0 || strstr(z, p->colSeparator)!=0 ){
21983 char *zQuoted = sqlite3_mprintf("\"%w\"", z);
21984 shell_check_oom(zQuoted);
21985 sqlite3_fputs(zQuoted, p->out);
21986 sqlite3_free(zQuoted);
21987 }else{
21988 sqlite3_fputs(z, p->out);
21989 }
21990 }
21991 if( bSep ){
21992 sqlite3_fputs(p->colSeparator, p->out);
21993 }
21994 }
21995
21996 /*
21997 ** This routine runs when the user presses Ctrl-C
@@ -22703,20 +22095,20 @@
22095 const char *az[4];
22096 az[0] = zA1;
22097 az[1] = zA2;
22098 az[2] = zA3;
22099 az[3] = zA4;
22100 sqlite3_fprintf(p->out, "authorizer: %s", azAction[op]);
22101 for(i=0; i<4; i++){
22102 sqlite3_fputs(" ", p->out);
22103 if( az[i] ){
22104 output_c_string(p->out, az[i]);
22105 }else{
22106 sqlite3_fputs("NULL", p->out);
22107 }
22108 }
22109 sqlite3_fputs("\n", p->out);
22110 if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4);
22111 return SQLITE_OK;
22112 }
22113 #endif
22114
@@ -22728,11 +22120,11 @@
22120 **
22121 ** If the schema statement in z[] contains a start-of-comment and if
22122 ** sqlite3_complete() returns false, try to terminate the comment before
22123 ** printing the result. https://sqlite.org/forum/forumpost/d7be961c5c
22124 */
22125 static void printSchemaLine(FILE *out, const char *z, const char *zTail){
22126 char *zToFree = 0;
22127 if( z==0 ) return;
22128 if( zTail==0 ) return;
22129 if( zTail[0]==';' && (strstr(z, "/*")!=0 || strstr(z,"--")!=0) ){
22130 const char *zOrig = z;
@@ -22750,20 +22142,20 @@
22142 }
22143 sqlite3_free(zNew);
22144 }
22145 }
22146 if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
22147 sqlite3_fprintf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
22148 }else{
22149 sqlite3_fprintf(out, "%s%s", z, zTail);
22150 }
22151 sqlite3_free(zToFree);
22152 }
22153 static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
22154 char c = z[n];
22155 z[n] = 0;
22156 printSchemaLine(out, z, zTail);
22157 z[n] = c;
22158 }
22159
22160 /*
22161 ** Return true if string z[] has nothing but whitespace and comments to the
@@ -22787,11 +22179,11 @@
22179 EQPGraphRow *pNew;
22180 i64 nText;
22181 if( zText==0 ) return;
22182 nText = strlen(zText);
22183 if( p->autoEQPtest ){
22184 sqlite3_fprintf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
22185 }
22186 pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
22187 shell_check_oom(pNew);
22188 pNew->iEqpId = iEqpId;
22189 pNew->iParentId = p2;
@@ -22835,11 +22227,12 @@
22227 i64 n = strlen(p->sGraph.zPrefix);
22228 char *z;
22229 for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
22230 pNext = eqp_next_row(p, iEqpId, pRow);
22231 z = pRow->zText;
22232 sqlite3_fprintf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
22233 pNext ? "|--" : "`--", z);
22234 if( n<(i64)sizeof(p->sGraph.zPrefix)-7 ){
22235 memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4);
22236 eqp_render_level(p, pRow->iEqpId);
22237 p->sGraph.zPrefix[n] = 0;
22238 }
@@ -22855,17 +22248,17 @@
22248 if( pRow->zText[0]=='-' ){
22249 if( pRow->pNext==0 ){
22250 eqp_reset(p);
22251 return;
22252 }
22253 sqlite3_fprintf(p->out, "%s\n", pRow->zText+3);
22254 p->sGraph.pRow = pRow->pNext;
22255 sqlite3_free(pRow);
22256 }else if( nCycle>0 ){
22257 sqlite3_fprintf(p->out, "QUERY PLAN (cycles=%lld [100%%])\n", nCycle);
22258 }else{
22259 sqlite3_fputs("QUERY PLAN\n", p->out);
22260 }
22261 p->sGraph.zPrefix[0] = 0;
22262 eqp_render_level(p, 0);
22263 eqp_reset(p);
22264 }
@@ -22877,33 +22270,33 @@
22270 */
22271 static int progress_handler(void *pClientData) {
22272 ShellState *p = (ShellState*)pClientData;
22273 p->nProgress++;
22274 if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
22275 sqlite3_fprintf(p->out, "Progress limit reached (%u)\n", p->nProgress);
22276 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
22277 if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
22278 return 1;
22279 }
22280 if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
22281 sqlite3_fprintf(p->out, "Progress %u\n", p->nProgress);
22282 }
22283 return 0;
22284 }
22285 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
22286
22287 /*
22288 ** Print N dashes
22289 */
22290 static void print_dashes(FILE *out, int N){
22291 const char zDash[] = "--------------------------------------------------";
22292 const int nDash = sizeof(zDash) - 1;
22293 while( N>nDash ){
22294 sqlite3_fputs(zDash, out);
22295 N -= nDash;
22296 }
22297 sqlite3_fprintf(out, "%.*s", N, zDash);
22298 }
22299
22300 /*
22301 ** Print a markdown or table-style row separator using ascii-art
22302 */
@@ -22912,19 +22305,19 @@
22305 int nArg,
22306 const char *zSep
22307 ){
22308 int i;
22309 if( nArg>0 ){
22310 sqlite3_fputs(zSep, p->out);
22311 print_dashes(p->out, p->actualWidth[0]+2);
22312 for(i=1; i<nArg; i++){
22313 sqlite3_fputs(zSep, p->out);
22314 print_dashes(p->out, p->actualWidth[i]+2);
22315 }
22316 sqlite3_fputs(zSep, p->out);
22317 }
22318 sqlite3_fputs("\n", p->out);
22319 }
22320
22321 /*
22322 ** This is the callback routine that the shell
22323 ** invokes for each row of a query result.
@@ -22950,13 +22343,13 @@
22343 if( azArg==0 ) break;
22344 for(i=0; i<nArg; i++){
22345 int len = strlen30(azCol[i] ? azCol[i] : "");
22346 if( len>w ) w = len;
22347 }
22348 if( p->cnt++>0 ) sqlite3_fputs(p->rowSeparator, p->out);
22349 for(i=0; i<nArg; i++){
22350 sqlite3_fprintf(p->out, "%*s = %s%s", w, azCol[i],
22351 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
22352 }
22353 break;
22354 }
22355 case MODE_ScanExp:
@@ -22980,16 +22373,16 @@
22373 if( nArg>nWidth ) nArg = nWidth;
22374
22375 /* If this is the first row seen, print out the headers */
22376 if( p->cnt++==0 ){
22377 for(i=0; i<nArg; i++){
22378 utf8_width_print(p->out, aWidth[i], azCol[ aMap[i] ]);
22379 sqlite3_fputs(i==nArg-1 ? "\n" : " ", p->out);
22380 }
22381 for(i=0; i<nArg; i++){
22382 print_dashes(p->out, aWidth[i]);
22383 sqlite3_fputs(i==nArg-1 ? "\n" : " ", p->out);
22384 }
22385 }
22386
22387 /* If there is no data, exit early. */
22388 if( azArg==0 ) break;
@@ -23003,21 +22396,21 @@
22396 w = strlenChar(zVal);
22397 zSep = " ";
22398 }
22399 if( i==iIndent && p->aiIndent && p->pStmt ){
22400 if( p->iIndent<p->nIndent ){
22401 sqlite3_fprintf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
22402 }
22403 p->iIndent++;
22404 }
22405 utf8_width_print(p->out, w, zVal ? zVal : p->nullValue);
22406 sqlite3_fputs(i==nArg-1 ? "\n" : zSep, p->out);
22407 }
22408 break;
22409 }
22410 case MODE_Semi: { /* .schema and .fullschema output */
22411 printSchemaLine(p->out, azArg[0], ";\n");
22412 break;
22413 }
22414 case MODE_Pretty: { /* .schema and .fullschema with --indent */
22415 char *z;
22416 int j;
@@ -23028,11 +22421,11 @@
22421 assert( nArg==1 );
22422 if( azArg[0]==0 ) break;
22423 if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
22424 || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
22425 ){
22426 sqlite3_fprintf(p->out, "%s;\n", azArg[0]);
22427 break;
22428 }
22429 z = sqlite3_mprintf("%s", azArg[0]);
22430 shell_check_oom(z);
22431 j = 0;
@@ -23061,255 +22454,265 @@
22454 }else if( c=='(' ){
22455 nParen++;
22456 }else if( c==')' ){
22457 nParen--;
22458 if( nLine>0 && nParen==0 && j>0 ){
22459 printSchemaLineN(p->out, z, j, "\n");
22460 j = 0;
22461 }
22462 }
22463 z[j++] = c;
22464 if( nParen==1 && cEnd==0
22465 && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
22466 ){
22467 if( c=='\n' ) j--;
22468 printSchemaLineN(p->out, z, j, "\n ");
22469 j = 0;
22470 nLine++;
22471 while( IsSpace(z[i+1]) ){ i++; }
22472 }
22473 }
22474 z[j] = 0;
22475 }
22476 printSchemaLine(p->out, z, ";\n");
22477 sqlite3_free(z);
22478 break;
22479 }
22480 case MODE_List: {
22481 if( p->cnt++==0 && p->showHeader ){
22482 for(i=0; i<nArg; i++){
22483 sqlite3_fprintf(p->out, "%s%s", azCol[i],
22484 i==nArg-1 ? p->rowSeparator : p->colSeparator);
22485 }
22486 }
22487 if( azArg==0 ) break;
22488 for(i=0; i<nArg; i++){
22489 char *z = azArg[i];
22490 if( z==0 ) z = p->nullValue;
22491 sqlite3_fputs(z, p->out);
22492 sqlite3_fputs((i<nArg-1)? p->colSeparator : p->rowSeparator, p->out);
22493 }
22494 break;
22495 }
22496 case MODE_Www:
22497 case MODE_Html: {
22498 if( p->cnt==0 && p->cMode==MODE_Www ){
22499 sqlite3_fputs(
22500 "</PRE>\n"
22501 "<TABLE border='1' cellspacing='0' cellpadding='2'>\n"
22502 ,p->out
22503 );
22504 }
22505 if( p->cnt==0 && (p->showHeader || p->cMode==MODE_Www) ){
22506 sqlite3_fputs("<TR>", p->out);
22507 for(i=0; i<nArg; i++){
22508 sqlite3_fputs("<TH>", p->out);
22509 output_html_string(p->out, azCol[i]);
22510 sqlite3_fputs("</TH>\n", p->out);
22511 }
22512 sqlite3_fputs("</TR>\n", p->out);
22513 }
22514 p->cnt++;
22515 if( azArg==0 ) break;
22516 sqlite3_fputs("<TR>", p->out);
22517 for(i=0; i<nArg; i++){
22518 sqlite3_fputs("<TD>", p->out);
22519 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
22520 sqlite3_fputs("</TD>\n", p->out);
22521 }
22522 sqlite3_fputs("</TR>\n", p->out);
22523 break;
22524 }
22525 case MODE_Tcl: {
22526 if( p->cnt++==0 && p->showHeader ){
22527 for(i=0; i<nArg; i++){
22528 output_c_string(p->out, azCol[i] ? azCol[i] : "");
22529 if(i<nArg-1) sqlite3_fputs(p->colSeparator, p->out);
22530 }
22531 sqlite3_fputs(p->rowSeparator, p->out);
22532 }
22533 if( azArg==0 ) break;
22534 for(i=0; i<nArg; i++){
22535 output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
22536 if(i<nArg-1) sqlite3_fputs(p->colSeparator, p->out);
22537 }
22538 sqlite3_fputs(p->rowSeparator, p->out);
22539 break;
22540 }
22541 case MODE_Csv: {
22542 sqlite3_fsetmode(p->out, _O_BINARY);
22543 if( p->cnt++==0 && p->showHeader ){
22544 for(i=0; i<nArg; i++){
22545 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
22546 }
22547 sqlite3_fputs(p->rowSeparator, p->out);
22548 }
22549 if( nArg>0 ){
22550 for(i=0; i<nArg; i++){
22551 output_csv(p, azArg[i], i<nArg-1);
22552 }
22553 sqlite3_fputs(p->rowSeparator, p->out);
22554 }
22555 sqlite3_fsetmode(p->out, _O_TEXT);
22556 break;
22557 }
22558 case MODE_Insert: {
22559 if( azArg==0 ) break;
22560 sqlite3_fprintf(p->out, "INSERT INTO %s",p->zDestTable);
22561 if( p->showHeader ){
22562 sqlite3_fputs("(", p->out);
22563 for(i=0; i<nArg; i++){
22564 if( i>0 ) sqlite3_fputs(",", p->out);
22565 if( quoteChar(azCol[i]) ){
22566 char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
22567 shell_check_oom(z);
22568 sqlite3_fputs(z, p->out);
22569 sqlite3_free(z);
22570 }else{
22571 sqlite3_fprintf(p->out, "%s", azCol[i]);
22572 }
22573 }
22574 sqlite3_fputs(")", p->out);
22575 }
22576 p->cnt++;
22577 for(i=0; i<nArg; i++){
22578 sqlite3_fputs(i>0 ? "," : " VALUES(", p->out);
22579 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
22580 sqlite3_fputs("NULL", p->out);
22581 }else if( aiType && aiType[i]==SQLITE_TEXT ){
22582 if( ShellHasFlag(p, SHFLG_Newlines) ){
22583 output_quoted_string(p->out, azArg[i]);
22584 }else{
22585 output_quoted_escaped_string(p->out, azArg[i]);
22586 }
22587 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
22588 sqlite3_fputs(azArg[i], p->out);
22589 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
22590 char z[50];
22591 double r = sqlite3_column_double(p->pStmt, i);
22592 sqlite3_uint64 ur;
22593 memcpy(&ur,&r,sizeof(r));
22594 if( ur==0x7ff0000000000000LL ){
22595 sqlite3_fputs("9.0e+999", p->out);
22596 }else if( ur==0xfff0000000000000LL ){
22597 sqlite3_fputs("-9.0e+999", p->out);
22598 }else{
22599 sqlite3_int64 ir = (sqlite3_int64)r;
22600 if( r==(double)ir ){
22601 sqlite3_snprintf(50,z,"%lld.0", ir);
22602 }else{
22603 sqlite3_snprintf(50,z,"%!.20g", r);
22604 }
22605 sqlite3_fputs(z, p->out);
22606 }
22607 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
22608 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
22609 int nBlob = sqlite3_column_bytes(p->pStmt, i);
22610 output_hex_blob(p->out, pBlob, nBlob);
22611 }else if( isNumber(azArg[i], 0) ){
22612 sqlite3_fputs(azArg[i], p->out);
22613 }else if( ShellHasFlag(p, SHFLG_Newlines) ){
22614 output_quoted_string(p->out, azArg[i]);
22615 }else{
22616 output_quoted_escaped_string(p->out, azArg[i]);
22617 }
22618 }
22619 sqlite3_fputs(");\n", p->out);
22620 break;
22621 }
22622 case MODE_Json: {
22623 if( azArg==0 ) break;
22624 if( p->cnt==0 ){
22625 sqlite3_fputs("[{", p->out);
22626 }else{
22627 sqlite3_fputs(",\n{", p->out);
22628 }
22629 p->cnt++;
22630 for(i=0; i<nArg; i++){
22631 output_json_string(p->out, azCol[i], -1);
22632 sqlite3_fputs(":", p->out);
22633 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
22634 sqlite3_fputs("null", p->out);
22635 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
22636 char z[50];
22637 double r = sqlite3_column_double(p->pStmt, i);
22638 sqlite3_uint64 ur;
22639 memcpy(&ur,&r,sizeof(r));
22640 if( ur==0x7ff0000000000000LL ){
22641 sqlite3_fputs("9.0e+999", p->out);
22642 }else if( ur==0xfff0000000000000LL ){
22643 sqlite3_fputs("-9.0e+999", p->out);
22644 }else{
22645 sqlite3_snprintf(50,z,"%!.20g", r);
22646 sqlite3_fputs(z, p->out);
22647 }
22648 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
22649 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
22650 int nBlob = sqlite3_column_bytes(p->pStmt, i);
22651 output_json_string(p->out, pBlob, nBlob);
22652 }else if( aiType && aiType[i]==SQLITE_TEXT ){
22653 output_json_string(p->out, azArg[i], -1);
22654 }else{
22655 sqlite3_fputs(azArg[i], p->out);
22656 }
22657 if( i<nArg-1 ){
22658 sqlite3_fputs(",", p->out);
22659 }
22660 }
22661 sqlite3_fputs("}", p->out);
22662 break;
22663 }
22664 case MODE_Quote: {
22665 if( azArg==0 ) break;
22666 if( p->cnt==0 && p->showHeader ){
22667 for(i=0; i<nArg; i++){
22668 if( i>0 ) sqlite3_fputs(p->colSeparator, p->out);
22669 output_quoted_string(p->out, azCol[i]);
22670 }
22671 sqlite3_fputs(p->rowSeparator, p->out);
22672 }
22673 p->cnt++;
22674 for(i=0; i<nArg; i++){
22675 if( i>0 ) sqlite3_fputs(p->colSeparator, p->out);
22676 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
22677 sqlite3_fputs("NULL", p->out);
22678 }else if( aiType && aiType[i]==SQLITE_TEXT ){
22679 output_quoted_string(p->out, azArg[i]);
22680 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
22681 sqlite3_fputs(azArg[i], p->out);
22682 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
22683 char z[50];
22684 double r = sqlite3_column_double(p->pStmt, i);
22685 sqlite3_snprintf(50,z,"%!.20g", r);
22686 sqlite3_fputs(z, p->out);
22687 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
22688 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
22689 int nBlob = sqlite3_column_bytes(p->pStmt, i);
22690 output_hex_blob(p->out, pBlob, nBlob);
22691 }else if( isNumber(azArg[i], 0) ){
22692 sqlite3_fputs(azArg[i], p->out);
22693 }else{
22694 output_quoted_string(p->out, azArg[i]);
22695 }
22696 }
22697 sqlite3_fputs(p->rowSeparator, p->out);
22698 break;
22699 }
22700 case MODE_Ascii: {
22701 if( p->cnt++==0 && p->showHeader ){
22702 for(i=0; i<nArg; i++){
22703 if( i>0 ) sqlite3_fputs(p->colSeparator, p->out);
22704 sqlite3_fputs(azCol[i] ? azCol[i] : "", p->out);
22705 }
22706 sqlite3_fputs(p->rowSeparator, p->out);
22707 }
22708 if( azArg==0 ) break;
22709 for(i=0; i<nArg; i++){
22710 if( i>0 ) sqlite3_fputs(p->colSeparator, p->out);
22711 sqlite3_fputs(azArg[i] ? azArg[i] : p->nullValue, p->out);
22712 }
22713 sqlite3_fputs(p->rowSeparator, p->out);
22714 break;
22715 }
22716 case MODE_EQP: {
22717 eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
22718 break;
@@ -23384,11 +22787,11 @@
22787 "INSERT INTO selftest(tno,op,cmd,ans)"
22788 " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
22789 "DROP TABLE [_shell$self];"
22790 ,0,0,&zErrMsg);
22791 if( zErrMsg ){
22792 sqlite3_fprintf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
22793 sqlite3_free(zErrMsg);
22794 }
22795 sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
22796 }
22797
@@ -23487,36 +22890,37 @@
22890 int i;
22891 const char *z;
22892 rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
22893 if( rc!=SQLITE_OK || !pSelect ){
22894 char *zContext = shell_error_context(zSelect, p->db);
22895 sqlite3_fprintf(p->out, "/**** ERROR: (%d) %s *****/\n%s",
22896 rc, sqlite3_errmsg(p->db), zContext);
22897 sqlite3_free(zContext);
22898 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
22899 return rc;
22900 }
22901 rc = sqlite3_step(pSelect);
22902 nResult = sqlite3_column_count(pSelect);
22903 while( rc==SQLITE_ROW ){
22904 z = (const char*)sqlite3_column_text(pSelect, 0);
22905 sqlite3_fprintf(p->out, "%s", z);
22906 for(i=1; i<nResult; i++){
22907 sqlite3_fprintf(p->out, ",%s", sqlite3_column_text(pSelect, i));
22908 }
22909 if( z==0 ) z = "";
22910 while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
22911 if( z[0] ){
22912 sqlite3_fputs("\n;\n", p->out);
22913 }else{
22914 sqlite3_fputs(";\n", p->out);
22915 }
22916 rc = sqlite3_step(pSelect);
22917 }
22918 rc = sqlite3_finalize(pSelect);
22919 if( rc!=SQLITE_OK ){
22920 sqlite3_fprintf(p->out, "/**** ERROR: (%d) %s *****/\n",
22921 rc, sqlite3_errmsg(p->db));
22922 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
22923 }
22924 return rc;
22925 }
22926
@@ -23548,17 +22952,17 @@
22952
22953 #ifdef __linux__
22954 /*
22955 ** Attempt to display I/O stats on Linux using /proc/PID/io
22956 */
22957 static void displayLinuxIoStats(FILE *out){
22958 FILE *in;
22959 char z[200];
22960 sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
22961 in = sqlite3_fopen(z, "rb");
22962 if( in==0 ) return;
22963 while( sqlite3_fgets(z, sizeof(z), in)!=0 ){
22964 static const struct {
22965 const char *zPattern;
22966 const char *zDesc;
22967 } aTrans[] = {
22968 { "rchar: ", "Bytes received by read():" },
@@ -23571,11 +22975,11 @@
22975 };
22976 int i;
22977 for(i=0; i<ArraySize(aTrans); i++){
22978 int n = strlen30(aTrans[i].zPattern);
22979 if( cli_strncmp(aTrans[i].zPattern, z, n)==0 ){
22980 sqlite3_fprintf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
22981 break;
22982 }
22983 }
22984 }
22985 fclose(in);
@@ -23584,10 +22988,11 @@
22988
22989 /*
22990 ** Display a single line of status using 64-bit values.
22991 */
22992 static void displayStatLine(
22993 FILE *out, /* Write to this channel */
22994 char *zLabel, /* Label for this one line */
22995 char *zFormat, /* Format for the result */
22996 int iStatusCtrl, /* Which status to display */
22997 int bReset /* True to reset the stats */
22998 ){
@@ -23602,11 +23007,11 @@
23007 if( nPercent>1 ){
23008 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
23009 }else{
23010 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
23011 }
23012 sqlite3_fprintf(out, "%-36s %s\n", zLabel, zLine);
23013 }
23014
23015 /*
23016 ** Display memory stats.
23017 */
@@ -23615,130 +23020,152 @@
23020 ShellState *pArg, /* Pointer to ShellState */
23021 int bReset /* True to reset the stats */
23022 ){
23023 int iCur;
23024 int iHiwtr;
23025 FILE *out;
23026 if( pArg==0 || pArg->out==0 ) return 0;
23027 out = pArg->out;
23028
23029 if( pArg->pStmt && pArg->statsOn==2 ){
23030 int nCol, i, x;
23031 sqlite3_stmt *pStmt = pArg->pStmt;
23032 char z[100];
23033 nCol = sqlite3_column_count(pStmt);
23034 sqlite3_fprintf(out, "%-36s %d\n", "Number of output columns:", nCol);
23035 for(i=0; i<nCol; i++){
23036 sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
23037 sqlite3_fprintf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
23038 #ifndef SQLITE_OMIT_DECLTYPE
23039 sqlite3_snprintf(30, z+x, "declared type:");
23040 sqlite3_fprintf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
23041 #endif
23042 #ifdef SQLITE_ENABLE_COLUMN_METADATA
23043 sqlite3_snprintf(30, z+x, "database name:");
23044 sqlite3_fprintf(out, "%-36s %s\n", z,
23045 sqlite3_column_database_name(pStmt,i));
23046 sqlite3_snprintf(30, z+x, "table name:");
23047 sqlite3_fprintf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
23048 sqlite3_snprintf(30, z+x, "origin name:");
23049 sqlite3_fprintf(out, "%-36s %s\n", z,sqlite3_column_origin_name(pStmt,i));
23050 #endif
23051 }
23052 }
23053
23054 if( pArg->statsOn==3 ){
23055 if( pArg->pStmt ){
23056 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP,bReset);
23057 sqlite3_fprintf(out, "VM-steps: %d\n", iCur);
23058 }
23059 return 0;
23060 }
23061
23062 displayStatLine(out, "Memory Used:",
23063 "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
23064 displayStatLine(out, "Number of Outstanding Allocations:",
23065 "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
23066 if( pArg->shellFlgs & SHFLG_Pagecache ){
23067 displayStatLine(out, "Number of Pcache Pages Used:",
23068 "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
23069 }
23070 displayStatLine(out, "Number of Pcache Overflow Bytes:",
23071 "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
23072 displayStatLine(out, "Largest Allocation:",
23073 "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
23074 displayStatLine(out, "Largest Pcache Allocation:",
23075 "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
23076 #ifdef YYTRACKMAXSTACKDEPTH
23077 displayStatLine(out, "Deepest Parser Stack:",
23078 "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
23079 #endif
23080
23081 if( db ){
23082 if( pArg->shellFlgs & SHFLG_Lookaside ){
23083 iHiwtr = iCur = -1;
23084 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
23085 &iCur, &iHiwtr, bReset);
23086 sqlite3_fprintf(out,
23087 "Lookaside Slots Used: %d (max %d)\n", iCur, iHiwtr);
23088 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
23089 &iCur, &iHiwtr, bReset);
23090 sqlite3_fprintf(out,
23091 "Successful lookaside attempts: %d\n", iHiwtr);
23092 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
23093 &iCur, &iHiwtr, bReset);
23094 sqlite3_fprintf(out,
23095 "Lookaside failures due to size: %d\n", iHiwtr);
23096 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
23097 &iCur, &iHiwtr, bReset);
23098 sqlite3_fprintf(out,
23099 "Lookaside failures due to OOM: %d\n", iHiwtr);
23100 }
23101 iHiwtr = iCur = -1;
23102 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
23103 sqlite3_fprintf(out,
23104 "Pager Heap Usage: %d bytes\n", iCur);
23105 iHiwtr = iCur = -1;
23106 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
23107 sqlite3_fprintf(out,
23108 "Page cache hits: %d\n", iCur);
23109 iHiwtr = iCur = -1;
23110 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
23111 sqlite3_fprintf(out,
23112 "Page cache misses: %d\n", iCur);
23113 iHiwtr = iCur = -1;
23114 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
23115 sqlite3_fprintf(out,
23116 "Page cache writes: %d\n", iCur);
23117 iHiwtr = iCur = -1;
23118 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
23119 sqlite3_fprintf(out,
23120 "Page cache spills: %d\n", iCur);
23121 iHiwtr = iCur = -1;
23122 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
23123 sqlite3_fprintf(out,
23124 "Schema Heap Usage: %d bytes\n", iCur);
23125 iHiwtr = iCur = -1;
23126 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
23127 sqlite3_fprintf(out,
23128 "Statement Heap/Lookaside Usage: %d bytes\n", iCur);
23129 }
23130
23131 if( pArg->pStmt ){
23132 int iHit, iMiss;
23133 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
23134 bReset);
23135 sqlite3_fprintf(out,
23136 "Fullscan Steps: %d\n", iCur);
23137 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
23138 sqlite3_fprintf(out,
23139 "Sort Operations: %d\n", iCur);
23140 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
23141 sqlite3_fprintf(out,
23142 "Autoindex Inserts: %d\n", iCur);
23143 iHit = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_HIT,
23144 bReset);
23145 iMiss = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_MISS,
23146 bReset);
23147 if( iHit || iMiss ){
23148 sqlite3_fprintf(out,
23149 "Bloom filter bypass taken: %d/%d\n", iHit, iHit+iMiss);
23150 }
23151 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
23152 sqlite3_fprintf(out,
23153 "Virtual Machine Steps: %d\n", iCur);
23154 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
23155 sqlite3_fprintf(out,
23156 "Reprepare operations: %d\n", iCur);
23157 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
23158 sqlite3_fprintf(out,
23159 "Number of times run: %d\n", iCur);
23160 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
23161 sqlite3_fprintf(out,
23162 "Memory used by prepared stmt: %d\n", iCur);
23163 }
23164
23165 #ifdef __linux__
23166 displayLinuxIoStats(pArg->out);
23167 #endif
23168
23169 /* Do not remove this machine readable comment: extra-stats-output-here */
23170
23171 return 0;
@@ -24130,21 +23557,21 @@
23557 #define BOX_1234 "\342\224\274" /* U+253c -|- */
23558
23559 /* Draw horizontal line N characters long using unicode box
23560 ** characters
23561 */
23562 static void print_box_line(FILE *out, int N){
23563 const char zDash[] =
23564 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
23565 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
23566 const int nDash = sizeof(zDash) - 1;
23567 N *= 3;
23568 while( N>nDash ){
23569 sqlite3_fputs(zDash, out);
23570 N -= nDash;
23571 }
23572 sqlite3_fprintf(out, "%.*s", N, zDash);
23573 }
23574
23575 /*
23576 ** Draw a horizontal separator for a MODE_Box table.
23577 */
@@ -24155,19 +23582,19 @@
23582 const char *zSep2,
23583 const char *zSep3
23584 ){
23585 int i;
23586 if( nArg>0 ){
23587 sqlite3_fputs(zSep1, p->out);
23588 print_box_line(p->out, p->actualWidth[0]+2);
23589 for(i=1; i<nArg; i++){
23590 sqlite3_fputs(zSep2, p->out);
23591 print_box_line(p->out, p->actualWidth[i]+2);
23592 }
23593 sqlite3_fputs(zSep3, p->out);
23594 }
23595 sqlite3_fputs("\n", p->out);
23596 }
23597
23598 /*
23599 ** z[] is a line of text that is to be displayed the .mode box or table or
23600 ** similar tabular formats. z[] might contain control characters such
@@ -24197,16 +23624,26 @@
23624 }
23625 if( mxWidth<0 ) mxWidth = -mxWidth;
23626 if( mxWidth==0 ) mxWidth = 1000000;
23627 i = j = n = 0;
23628 while( n<mxWidth ){
23629 unsigned char c = z[i];
23630 if( c>=0xc0 ){
23631 int u;
23632 int len = decodeUtf8(&z[i], &u);
23633 i += len;
23634 j += len;
23635 n += cli_wcwidth(u);
23636 continue;
23637 }
23638 if( c>=' ' ){
23639 n++;
23640 i++;
23641 j++;
23642 continue;
23643 }
23644 if( c=='\t' ){
23645 do{
23646 n++;
23647 j++;
23648 }while( (n&7)!=0 && n<mxWidth );
23649 i++;
@@ -24244,13 +23681,21 @@
23681 }
23682 zOut = malloc( j+1 );
23683 shell_check_oom(zOut);
23684 i = j = n = 0;
23685 while( i<k ){
23686 unsigned char c = z[i];
23687 if( c>=0xc0 ){
23688 int u;
23689 int len = decodeUtf8(&z[i], &u);
23690 do{ zOut[j++] = z[i++]; }while( (--len)>0 );
23691 n += cli_wcwidth(u);
23692 continue;
23693 }
23694 if( c>=' ' ){
23695 n++;
23696 zOut[j++] = z[i++];
23697 continue;
23698 }
23699 if( z[i]=='\t' ){
23700 do{
23701 n++;
@@ -24426,97 +23871,99 @@
23871 rowSep = "\n";
23872 if( p->showHeader ){
23873 for(i=0; i<nColumn; i++){
23874 w = p->actualWidth[i];
23875 if( p->colWidth[i]<0 ) w = -w;
23876 utf8_width_print(p->out, w, azData[i]);
23877 sqlite3_fputs(i==nColumn-1?"\n":" ", p->out);
23878 }
23879 for(i=0; i<nColumn; i++){
23880 print_dashes(p->out, p->actualWidth[i]);
23881 sqlite3_fputs(i==nColumn-1?"\n":" ", p->out);
23882 }
23883 }
23884 break;
23885 }
23886 case MODE_Table: {
23887 colSep = " | ";
23888 rowSep = " |\n";
23889 print_row_separator(p, nColumn, "+");
23890 sqlite3_fputs("| ", p->out);
23891 for(i=0; i<nColumn; i++){
23892 w = p->actualWidth[i];
23893 n = strlenChar(azData[i]);
23894 sqlite3_fprintf(p->out, "%*s%s%*s", (w-n)/2, "",
23895 azData[i], (w-n+1)/2, "");
23896 sqlite3_fputs(i==nColumn-1?" |\n":" | ", p->out);
23897 }
23898 print_row_separator(p, nColumn, "+");
23899 break;
23900 }
23901 case MODE_Markdown: {
23902 colSep = " | ";
23903 rowSep = " |\n";
23904 sqlite3_fputs("| ", p->out);
23905 for(i=0; i<nColumn; i++){
23906 w = p->actualWidth[i];
23907 n = strlenChar(azData[i]);
23908 sqlite3_fprintf(p->out, "%*s%s%*s", (w-n)/2, "",
23909 azData[i], (w-n+1)/2, "");
23910 sqlite3_fputs(i==nColumn-1?" |\n":" | ", p->out);
23911 }
23912 print_row_separator(p, nColumn, "|");
23913 break;
23914 }
23915 case MODE_Box: {
23916 colSep = " " BOX_13 " ";
23917 rowSep = " " BOX_13 "\n";
23918 print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
23919 sqlite3_fputs(BOX_13 " ", p->out);
23920 for(i=0; i<nColumn; i++){
23921 w = p->actualWidth[i];
23922 n = strlenChar(azData[i]);
23923 sqlite3_fprintf(p->out, "%*s%s%*s%s",
23924 (w-n)/2, "", azData[i], (w-n+1)/2, "",
23925 i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
23926 }
23927 print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
23928 break;
23929 }
23930 }
23931 for(i=nColumn, j=0; i<nTotal; i++, j++){
23932 if( j==0 && p->cMode!=MODE_Column ){
23933 sqlite3_fputs(p->cMode==MODE_Box?BOX_13" ":"| ", p->out);
23934 }
23935 z = azData[i];
23936 if( z==0 ) z = p->nullValue;
23937 w = p->actualWidth[j];
23938 if( p->colWidth[j]<0 ) w = -w;
23939 utf8_width_print(p->out, w, z);
23940 if( j==nColumn-1 ){
23941 sqlite3_fputs(rowSep, p->out);
23942 if( bMultiLineRowExists && abRowDiv[i/nColumn-1] && i+1<nTotal ){
23943 if( p->cMode==MODE_Table ){
23944 print_row_separator(p, nColumn, "+");
23945 }else if( p->cMode==MODE_Box ){
23946 print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
23947 }else if( p->cMode==MODE_Column ){
23948 sqlite3_fputs("\n", p->out);
23949 }
23950 }
23951 j = -1;
23952 if( seenInterrupt ) goto columnar_end;
23953 }else{
23954 sqlite3_fputs(colSep, p->out);
23955 }
23956 }
23957 if( p->cMode==MODE_Table ){
23958 print_row_separator(p, nColumn, "+");
23959 }else if( p->cMode==MODE_Box ){
23960 print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
23961 }
23962 columnar_end:
23963 if( seenInterrupt ){
23964 sqlite3_fputs("Interrupt\n", p->out);
23965 }
23966 nData = (nRow+1)*nColumn;
23967 for(i=0; i<nData; i++){
23968 z = azData[i];
23969 if( z!=zEmpty && z!=zShowNull ) free(azData[i]);
@@ -24599,11 +24046,13 @@
24046 }
24047 }
24048 } while( SQLITE_ROW == rc );
24049 sqlite3_free(pData);
24050 if( pArg->cMode==MODE_Json ){
24051 sqlite3_fputs("]\n", pArg->out);
24052 }else if( pArg->cMode==MODE_Www ){
24053 sqlite3_fputs("</TABLE>\n<PRE>\n", pArg->out);
24054 }else if( pArg->cMode==MODE_Count ){
24055 char zBuf[200];
24056 sqlite3_snprintf(sizeof(zBuf), zBuf, "%llu row%s\n",
24057 nRow, nRow!=1 ? "s" : "");
24058 printf("%s", zBuf);
@@ -24648,10 +24097,11 @@
24097 int bCancel,
24098 char **pzErr
24099 ){
24100 int rc = SQLITE_OK;
24101 sqlite3expert *p = pState->expert.pExpert;
24102 FILE *out = pState->out;
24103 assert( p );
24104 assert( bCancel || pzErr==0 || *pzErr==0 );
24105 if( bCancel==0 ){
24106 int bVerbose = pState->expert.bVerbose;
24107
@@ -24660,24 +24110,25 @@
24110 int nQuery = sqlite3_expert_count(p);
24111 int i;
24112
24113 if( bVerbose ){
24114 const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
24115 sqlite3_fputs("-- Candidates -----------------------------\n", out);
24116 sqlite3_fprintf(out, "%s\n", zCand);
24117 }
24118 for(i=0; i<nQuery; i++){
24119 const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
24120 const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
24121 const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
24122 if( zIdx==0 ) zIdx = "(no new indexes)\n";
24123 if( bVerbose ){
24124 sqlite3_fprintf(out,
24125 "-- Query %d --------------------------------\n"
24126 "%s\n\n"
24127 ,i+1, zSql);
24128 }
24129 sqlite3_fprintf(out, "%s\n%s\n", zIdx, zEQP);
 
24130 }
24131 }
24132 }
24133 sqlite3_expert_destroy(p);
24134 pState->expert.pExpert = 0;
@@ -24708,30 +24159,31 @@
24159 if( n>=2 && 0==cli_strncmp(z, "-verbose", n) ){
24160 pState->expert.bVerbose = 1;
24161 }
24162 else if( n>=2 && 0==cli_strncmp(z, "-sample", n) ){
24163 if( i==(nArg-1) ){
24164 sqlite3_fprintf(stderr, "option requires an argument: %s\n", z);
24165 rc = SQLITE_ERROR;
24166 }else{
24167 iSample = (int)integerValue(azArg[++i]);
24168 if( iSample<0 || iSample>100 ){
24169 sqlite3_fprintf(stderr,"value out of range: %s\n", azArg[i]);
24170 rc = SQLITE_ERROR;
24171 }
24172 }
24173 }
24174 else{
24175 sqlite3_fprintf(stderr,"unknown option: %s\n", z);
24176 rc = SQLITE_ERROR;
24177 }
24178 }
24179
24180 if( rc==SQLITE_OK ){
24181 pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
24182 if( pState->expert.pExpert==0 ){
24183 sqlite3_fprintf(stderr,
24184 "sqlite3_expert_new: %s\n", zErr ? zErr : "out of memory");
24185 rc = SQLITE_ERROR;
24186 }else{
24187 sqlite3_expert_config(
24188 pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
24189 );
@@ -25056,33 +24508,33 @@
24508 if( zType==0 ) return 0;
24509 dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
24510 noSys = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
24511
24512 if( cli_strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
24513 if( !dataOnly ) sqlite3_fputs("DELETE FROM sqlite_sequence;\n", p->out);
24514 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
24515 if( !dataOnly ) sqlite3_fputs("ANALYZE sqlite_schema;\n", p->out);
24516 }else if( cli_strncmp(zTable, "sqlite_", 7)==0 ){
24517 return 0;
24518 }else if( dataOnly ){
24519 /* no-op */
24520 }else if( cli_strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
24521 char *zIns;
24522 if( !p->writableSchema ){
24523 sqlite3_fputs("PRAGMA writable_schema=ON;\n", p->out);
24524 p->writableSchema = 1;
24525 }
24526 zIns = sqlite3_mprintf(
24527 "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
24528 "VALUES('table','%q','%q',0,'%q');",
24529 zTable, zTable, zSql);
24530 shell_check_oom(zIns);
24531 sqlite3_fprintf(p->out, "%s\n", zIns);
24532 sqlite3_free(zIns);
24533 return 0;
24534 }else{
24535 printSchemaLine(p->out, zSql, ";\n");
24536 }
24537
24538 if( cli_strcmp(zType, "table")==0 ){
24539 ShellText sSelect;
24540 ShellText sTable;
@@ -25136,11 +24588,11 @@
24588 savedMode = p->mode;
24589 p->zDestTable = sTable.z;
24590 p->mode = p->cMode = MODE_Insert;
24591 rc = shell_exec(p, sSelect.z, 0);
24592 if( (rc&0xff)==SQLITE_CORRUPT ){
24593 sqlite3_fputs("/****** CORRUPTION ERROR *******/\n", p->out);
24594 toggleSelectOrder(p->db);
24595 shell_exec(p, sSelect.z, 0);
24596 toggleSelectOrder(p->db);
24597 }
24598 p->zDestTable = savedDestTable;
@@ -25167,22 +24619,22 @@
24619 char *zErr = 0;
24620 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
24621 if( rc==SQLITE_CORRUPT ){
24622 char *zQ2;
24623 int len = strlen30(zQuery);
24624 sqlite3_fputs("/****** CORRUPTION ERROR *******/\n", p->out);
24625 if( zErr ){
24626 sqlite3_fprintf(p->out, "/****** %s ******/\n", zErr);
24627 sqlite3_free(zErr);
24628 zErr = 0;
24629 }
24630 zQ2 = malloc( len+100 );
24631 if( zQ2==0 ) return rc;
24632 sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
24633 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
24634 if( rc ){
24635 sqlite3_fprintf(p->out, "/****** ERROR: %s ******/\n", zErr);
24636 }else{
24637 rc = SQLITE_CORRUPT;
24638 }
24639 sqlite3_free(zErr);
24640 free(zQ2);
@@ -25241,13 +24693,14 @@
24693 #ifndef SQLITE_SHELL_FIDDLE
24694 ".check GLOB Fail if output since .testcase does not match",
24695 ".clone NEWDB Clone data into NEWDB from the existing database",
24696 #endif
24697 ".connection [close] [#] Open or close an auxiliary database connection",
24698 #if defined(_WIN32) && !defined(SQLITE_U8TEXT_ONLY) \
24699 && !defined(SQLITE_U8TEXT_STDIO)
24700 ".crnl on|off Translate \\n to \\r\\n. Default ON",
24701 #endif /* _WIN32 && U8TEXT_ONLY && U8TEXT_STDIO */
24702 ".databases List names and files of attached databases",
24703 ".dbconfig ?op? ?val? List or change sqlite3_db_config() options",
24704 #if SQLITE_SHELL_HAVE_RECOVER
24705 ".dbinfo ?DB? Show status information about the database",
24706 #endif
@@ -25349,13 +24802,15 @@
24802 #endif
24803 ".nullvalue STRING Use STRING in place of NULL values",
24804 #ifndef SQLITE_SHELL_FIDDLE
24805 ".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE",
24806 " If FILE begins with '|' then open as a pipe",
24807 " --bom Put a UTF8 byte-order mark at the beginning",
24808 " -e Send output to the system text editor",
24809 " --plain Use text/plain output instead of HTML for -w option",
24810 " -w Send output as HTML to a web browser (same as \".www\")",
24811 " -x Send output as CSV to a spreadsheet (same as \".excel\")",
24812 /* Note that .open is (partially) available in WASM builds but is
24813 ** currently only intended to be used by the fiddle tool, not
24814 ** end users, so is "undocumented." */
24815 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE",
24816 " Options:",
@@ -25374,10 +24829,12 @@
24829 ".output ?FILE? Send output to FILE or stdout if FILE is omitted",
24830 " If FILE begins with '|' then open it as a pipe.",
24831 " Options:",
24832 " --bom Prefix output with a UTF8 byte-order mark",
24833 " -e Send output to the system text editor",
24834 " --plain Use text/plain for -w option",
24835 " -w Send output to a web browser",
24836 " -x Send output as CSV to a spreadsheet",
24837 #endif
24838 ".parameter CMD ... Manage SQL parameter bindings",
24839 " clear Erase all bindings",
24840 " init Initialize the TEMP table that holds bindings",
@@ -25487,10 +24944,14 @@
24944 ".vfsinfo ?AUX? Information about the top-level VFS",
24945 ".vfslist List all available VFSes",
24946 ".vfsname ?AUX? Print the name of the VFS stack",
24947 ".width NUM1 NUM2 ... Set minimum column widths for columnar output",
24948 " Negative values right-justify",
24949 #ifndef SQLITE_SHELL_FIDDLE
24950 ".www Display output of the next command in web browser",
24951 " --plain Show results as text/plain, not as HTML",
24952 #endif
24953 };
24954
24955 /*
24956 ** Output help text.
24957 **
@@ -25535,24 +24996,24 @@
24996 hh &= ~HH_Summary;
24997 break;
24998 }
24999 if( ((hw^hh)&HH_Undoc)==0 ){
25000 if( (hh&HH_Summary)!=0 ){
25001 sqlite3_fprintf(out, ".%s\n", azHelp[i]+1);
25002 ++n;
25003 }else if( (hw&HW_SummaryOnly)==0 ){
25004 sqlite3_fprintf(out, "%s\n", azHelp[i]);
25005 }
25006 }
25007 }
25008 }else{
25009 /* Seek documented commands for which zPattern is an exact prefix */
25010 zPat = sqlite3_mprintf(".%s*", zPattern);
25011 shell_check_oom(zPat);
25012 for(i=0; i<ArraySize(azHelp); i++){
25013 if( sqlite3_strglob(zPat, azHelp[i])==0 ){
25014 sqlite3_fprintf(out, "%s\n", azHelp[i]);
25015 j = i+1;
25016 n++;
25017 }
25018 }
25019 sqlite3_free(zPat);
@@ -25559,11 +25020,11 @@
25020 if( n ){
25021 if( n==1 ){
25022 /* when zPattern is a prefix of exactly one command, then include
25023 ** the details of that command, which should begin at offset j */
25024 while( j<ArraySize(azHelp)-1 && azHelp[j][0]==' ' ){
25025 sqlite3_fprintf(out, "%s\n", azHelp[j]);
25026 j++;
25027 }
25028 }
25029 return n;
25030 }
@@ -25576,14 +25037,14 @@
25037 while( i<ArraySize(azHelp)-1 && azHelp[i+1][0]==' ' ) ++i;
25038 continue;
25039 }
25040 if( azHelp[i][0]=='.' ) j = i;
25041 if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
25042 sqlite3_fprintf(out, "%s\n", azHelp[j]);
25043 while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]==' ' ){
25044 j++;
25045 sqlite3_fprintf(out, "%s\n", azHelp[j]);
25046 }
25047 i = j;
25048 n++;
25049 }
25050 }
@@ -25609,35 +25070,35 @@
25070 **
25071 ** NULL is returned if any error is encountered. The final value of *pnByte
25072 ** is undefined in this case.
25073 */
25074 static char *readFile(const char *zName, int *pnByte){
25075 FILE *in = sqlite3_fopen(zName, "rb");
25076 long nIn;
25077 size_t nRead;
25078 char *pBuf;
25079 int rc;
25080 if( in==0 ) return 0;
25081 rc = fseek(in, 0, SEEK_END);
25082 if( rc!=0 ){
25083 sqlite3_fprintf(stderr,"Error: '%s' not seekable\n", zName);
25084 fclose(in);
25085 return 0;
25086 }
25087 nIn = ftell(in);
25088 rewind(in);
25089 pBuf = sqlite3_malloc64( nIn+1 );
25090 if( pBuf==0 ){
25091 sqlite3_fputs("Error: out of memory\n", stderr);
25092 fclose(in);
25093 return 0;
25094 }
25095 nRead = fread(pBuf, nIn, 1, in);
25096 fclose(in);
25097 if( nRead!=1 ){
25098 sqlite3_free(pBuf);
25099 sqlite3_fprintf(stderr,"Error: cannot read '%s'\n", zName);
25100 return 0;
25101 }
25102 pBuf[nIn] = 0;
25103 if( pnByte ) *pnByte = nIn;
25104 return pBuf;
@@ -25699,11 +25160,11 @@
25160 ** archive and the dfltZip flag is true, then assume it is a ZIP archive.
25161 ** Otherwise, assume an ordinary database regardless of the filename if
25162 ** the type cannot be determined from content.
25163 */
25164 int deduceDatabaseType(const char *zName, int dfltZip){
25165 FILE *f = sqlite3_fopen(zName, "rb");
25166 size_t n;
25167 int rc = SHELL_OPEN_UNSPEC;
25168 char zBuf[100];
25169 if( f==0 ){
25170 if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
@@ -25752,13 +25213,13 @@
25213 FILE *in;
25214 const char *zDbFilename = p->pAuxDb->zDbFilename;
25215 unsigned int x[16];
25216 char zLine[1000];
25217 if( zDbFilename ){
25218 in = sqlite3_fopen(zDbFilename, "r");
25219 if( in==0 ){
25220 sqlite3_fprintf(stderr,"cannot open \"%s\" for reading\n", zDbFilename);
25221 return 0;
25222 }
25223 nLine = 0;
25224 }else{
25225 in = p->in;
@@ -25765,24 +25226,24 @@
25226 nLine = p->lineno;
25227 if( in==0 ) in = stdin;
25228 }
25229 *pnData = 0;
25230 nLine++;
25231 if( sqlite3_fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
25232 rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
25233 if( rc!=2 ) goto readHexDb_error;
25234 if( n<0 ) goto readHexDb_error;
25235 if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
25236 n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */
25237 a = sqlite3_malloc( n ? n : 1 );
25238 shell_check_oom(a);
25239 memset(a, 0, n);
25240 if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
25241 sqlite3_fputs("invalid pagesize\n", stderr);
25242 goto readHexDb_error;
25243 }
25244 for(nLine++; sqlite3_fgets(zLine, sizeof(zLine), in)!=0; nLine++){
25245 rc = sscanf(zLine, "| page %d offset %d", &j, &k);
25246 if( rc==2 ){
25247 iOffset = k;
25248 continue;
25249 }
@@ -25810,18 +25271,18 @@
25271
25272 readHexDb_error:
25273 if( in!=p->in ){
25274 fclose(in);
25275 }else{
25276 while( sqlite3_fgets(zLine, sizeof(zLine), p->in)!=0 ){
25277 nLine++;
25278 if(cli_strncmp(zLine, "| end ", 6)==0 ) break;
25279 }
25280 p->lineno = nLine;
25281 }
25282 sqlite3_free(a);
25283 sqlite3_fprintf(stderr,"Error on line %d of --hexdb input\n", nLine);
25284 return 0;
25285 }
25286 #endif /* SQLITE_OMIT_DESERIALIZE */
25287
25288 /*
@@ -25892,22 +25353,24 @@
25353 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
25354 break;
25355 }
25356 }
25357 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
25358 sqlite3_fprintf(stderr,"Error: unable to open database \"%s\": %s\n",
25359 zDbFilename, sqlite3_errmsg(p->db));
25360 if( (openFlags & OPEN_DB_KEEPALIVE)==0 ){
25361 exit(1);
25362 }
25363 sqlite3_close(p->db);
25364 sqlite3_open(":memory:", &p->db);
25365 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
25366 sqlite3_fputs("Also: unable to open substitute in-memory database.\n",
25367 stderr);
25368 exit(1);
25369 }else{
25370 sqlite3_fprintf(stderr,
25371 "Notice: using substitute in-memory database instead of \"%s\"\n",
25372 zDbFilename);
25373 }
25374 }
25375 globalDb = p->db;
25376 sqlite3_db_config(p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, (int)0, (int*)0);
@@ -26015,11 +25478,11 @@
25478 }
25479 rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
25480 SQLITE_DESERIALIZE_RESIZEABLE |
25481 SQLITE_DESERIALIZE_FREEONCLOSE);
25482 if( rc ){
25483 sqlite3_fprintf(stderr,"Error: sqlite3_deserialize() returns %d\n", rc);
25484 }
25485 if( p->szMax>0 ){
25486 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
25487 }
25488 }
@@ -26039,11 +25502,12 @@
25502 ** Attempt to close the database connection. Report errors.
25503 */
25504 void close_db(sqlite3 *db){
25505 int rc = sqlite3_close(db);
25506 if( rc ){
25507 sqlite3_fprintf(stderr,
25508 "Error: sqlite3_close() returns %d: %s\n", rc, sqlite3_errmsg(db));
25509 }
25510 }
25511
25512 #if HAVE_READLINE || HAVE_EDITLINE
25513 /*
@@ -26203,11 +25667,12 @@
25667 return 1;
25668 }
25669 if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
25670 return 0;
25671 }
25672 sqlite3_fprintf(stderr,
25673 "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", zArg);
25674 return 0;
25675 }
25676
25677 /*
25678 ** Set or clear a shell flag according to a boolean value.
@@ -26239,13 +25704,13 @@
25704 }else if( cli_strcmp(zFile, "stderr")==0 ){
25705 f = stderr;
25706 }else if( cli_strcmp(zFile, "off")==0 ){
25707 f = 0;
25708 }else{
25709 f = sqlite3_fopen(zFile, bTextMode ? "w" : "wb");
25710 if( f==0 ){
25711 sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", zFile);
25712 }
25713 }
25714 return f;
25715 }
25716
@@ -26294,16 +25759,17 @@
25759 if( nSql>1000000000 ) nSql = 1000000000;
25760 while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
25761 switch( mType ){
25762 case SQLITE_TRACE_ROW:
25763 case SQLITE_TRACE_STMT: {
25764 sqlite3_fprintf(p->traceOut, "%.*s;\n", (int)nSql, zSql);
25765 break;
25766 }
25767 case SQLITE_TRACE_PROFILE: {
25768 sqlite3_int64 nNanosec = pX ? *(sqlite3_int64*)pX : 0;
25769 sqlite3_fprintf(p->traceOut,
25770 "%.*s; -- %lld ns\n", (int)nSql, zSql, nNanosec);
25771 break;
25772 }
25773 }
25774 return 0;
25775 }
@@ -26406,14 +25872,15 @@
25872 do{ p->n--; }while( p->z[p->n]!=cQuote );
25873 p->cTerm = c;
25874 break;
25875 }
25876 if( pc==cQuote && c!='\r' ){
25877 sqlite3_fprintf(stderr,"%s:%d: unescaped %c character\n",
25878 p->zFile, p->nLine, cQuote);
25879 }
25880 if( c==EOF ){
25881 sqlite3_fprintf(stderr,"%s:%d: unterminated %c-quoted field\n",
25882 p->zFile, startLine, cQuote);
25883 p->cTerm = c;
25884 break;
25885 }
25886 import_append_char(p, c);
@@ -26508,11 +25975,11 @@
25975
25976 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
25977 shell_check_oom(zQuery);
25978 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
25979 if( rc ){
25980 sqlite3_fprintf(stderr,"Error %d: %s on [%s]\n",
25981 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zQuery);
25982 goto end_data_xfer;
25983 }
25984 n = sqlite3_column_count(pQuery);
25985 zInsert = sqlite3_malloc64(200 + nTable + n*3);
@@ -26525,11 +25992,11 @@
25992 i += 2;
25993 }
25994 memcpy(zInsert+i, ");", 3);
25995 rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
25996 if( rc ){
25997 sqlite3_fprintf(stderr,"Error %d: %s on [%s]\n",
25998 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb), zInsert);
25999 goto end_data_xfer;
26000 }
26001 for(k=0; k<2; k++){
26002 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
@@ -26561,11 +26028,11 @@
26028 }
26029 }
26030 } /* End for */
26031 rc = sqlite3_step(pInsert);
26032 if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
26033 sqlite3_fprintf(stderr,"Error %d: %s\n",
26034 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb));
26035 }
26036 sqlite3_reset(pInsert);
26037 cnt++;
26038 if( (cnt%spinRate)==0 ){
@@ -26579,11 +26046,11 @@
26046 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
26047 zTable);
26048 shell_check_oom(zQuery);
26049 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
26050 if( rc ){
26051 sqlite3_fprintf(stderr,"Warning: cannot step \"%s\" backwards", zTable);
26052 break;
26053 }
26054 } /* End for(k=0...) */
26055
26056 end_data_xfer:
@@ -26616,23 +26083,24 @@
26083 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
26084 " WHERE %s ORDER BY rowid ASC", zWhere);
26085 shell_check_oom(zQuery);
26086 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
26087 if( rc ){
26088 sqlite3_fprintf(stderr,
26089 "Error: (%d) %s on [%s]\n", sqlite3_extended_errcode(p->db),
26090 sqlite3_errmsg(p->db), zQuery);
26091 goto end_schema_xfer;
26092 }
26093 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
26094 zName = sqlite3_column_text(pQuery, 0);
26095 zSql = sqlite3_column_text(pQuery, 1);
26096 if( zName==0 || zSql==0 ) continue;
26097 if( sqlite3_stricmp((char*)zName, "sqlite_sequence")!=0 ){
26098 sqlite3_fprintf(stdout, "%s... ", zName); fflush(stdout);
26099 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
26100 if( zErrMsg ){
26101 sqlite3_fprintf(stderr,"Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
26102 sqlite3_free(zErrMsg);
26103 zErrMsg = 0;
26104 }
26105 }
26106 if( xForEach ){
@@ -26646,23 +26114,23 @@
26114 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
26115 " WHERE %s ORDER BY rowid DESC", zWhere);
26116 shell_check_oom(zQuery);
26117 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
26118 if( rc ){
26119 sqlite3_fprintf(stderr,"Error: (%d) %s on [%s]\n",
26120 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zQuery);
26121 goto end_schema_xfer;
26122 }
26123 while( sqlite3_step(pQuery)==SQLITE_ROW ){
26124 zName = sqlite3_column_text(pQuery, 0);
26125 zSql = sqlite3_column_text(pQuery, 1);
26126 if( zName==0 || zSql==0 ) continue;
26127 if( sqlite3_stricmp((char*)zName, "sqlite_sequence")==0 ) continue;
26128 sqlite3_fprintf(stdout, "%s... ", zName); fflush(stdout);
26129 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
26130 if( zErrMsg ){
26131 sqlite3_fprintf(stderr,"Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
26132 sqlite3_free(zErrMsg);
26133 zErrMsg = 0;
26134 }
26135 if( xForEach ){
26136 xForEach(p, newDb, (const char*)zName);
@@ -26682,16 +26150,17 @@
26150 */
26151 static void tryToClone(ShellState *p, const char *zNewDb){
26152 int rc;
26153 sqlite3 *newDb = 0;
26154 if( access(zNewDb,0)==0 ){
26155 sqlite3_fprintf(stderr,"File \"%s\" already exists.\n", zNewDb);
26156 return;
26157 }
26158 rc = sqlite3_open(zNewDb, &newDb);
26159 if( rc ){
26160 sqlite3_fprintf(stderr,
26161 "Cannot create output database: %s\n", sqlite3_errmsg(newDb));
26162 }else{
26163 sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
26164 sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
26165 tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
26166 tryToCloneSchema(p, newDb, "type!='table'", 0);
@@ -26704,14 +26173,21 @@
26173 #ifndef SQLITE_SHELL_FIDDLE
26174 /*
26175 ** Change the output stream (file or pipe or console) to something else.
26176 */
26177 static void output_redir(ShellState *p, FILE *pfNew){
26178 if( p->out != stdout ){
26179 sqlite3_fputs("Output already redirected.\n", stderr);
26180 }else{
26181 p->out = pfNew;
26182 if( p->mode==MODE_Www ){
26183 sqlite3_fputs(
26184 "<!DOCTYPE html>\n"
26185 "<HTML><BODY><PRE>\n",
26186 p->out
26187 );
26188 }
26189 }
26190 }
26191
26192 /*
26193 ** Change the output file back to stdout.
@@ -26724,10 +26200,13 @@
26200 if( p->outfile[0]=='|' ){
26201 #ifndef SQLITE_OMIT_POPEN
26202 pclose(p->out);
26203 #endif
26204 }else{
26205 if( p->mode==MODE_Www ){
26206 sqlite3_fputs("</PRE></BODY></HTML>\n", p->out);
26207 }
26208 output_file_close(p->out);
26209 #ifndef SQLITE_NOHAVE_SYSTEM
26210 if( p->doXdgOpen ){
26211 const char *zXdgOpenCmd =
26212 #if defined(_WIN32)
@@ -26738,11 +26217,11 @@
26217 "xdg-open";
26218 #endif
26219 char *zCmd;
26220 zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
26221 if( system(zCmd) ){
26222 sqlite3_fprintf(stderr,"Failed: [%s]\n", zCmd);
26223 }else{
26224 /* Give the start/open/xdg-open command some time to get
26225 ** going before we continue, and potential delete the
26226 ** p->zTempFile data file out from under it */
26227 sqlite3_sleep(2000);
@@ -26753,11 +26232,10 @@
26232 }
26233 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
26234 }
26235 p->outfile[0] = 0;
26236 p->out = stdout;
 
26237 }
26238 #else
26239 # define output_redir(SS,pfO)
26240 # define output_reset(SS)
26241 #endif
@@ -26829,11 +26307,11 @@
26307 if( p->db==0 ) return 1;
26308 rc = sqlite3_prepare_v2(p->db,
26309 "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
26310 -1, &pStmt, 0);
26311 if( rc ){
26312 sqlite3_fprintf(stderr,"error: %s\n", sqlite3_errmsg(p->db));
26313 sqlite3_finalize(pStmt);
26314 return 1;
26315 }
26316 sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
26317 if( sqlite3_step(pStmt)==SQLITE_ROW
@@ -26842,32 +26320,32 @@
26320 const u8 *pb = sqlite3_column_blob(pStmt,0);
26321 shell_check_oom(pb);
26322 memcpy(aHdr, pb, 100);
26323 sqlite3_finalize(pStmt);
26324 }else{
26325 sqlite3_fputs("unable to read database header\n", stderr);
26326 sqlite3_finalize(pStmt);
26327 return 1;
26328 }
26329 i = get2byteInt(aHdr+16);
26330 if( i==1 ) i = 65536;
26331 sqlite3_fprintf(p->out, "%-20s %d\n", "database page size:", i);
26332 sqlite3_fprintf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
26333 sqlite3_fprintf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
26334 sqlite3_fprintf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
26335 for(i=0; i<ArraySize(aField); i++){
26336 int ofst = aField[i].ofst;
26337 unsigned int val = get4byteInt(aHdr + ofst);
26338 sqlite3_fprintf(p->out, "%-20s %u", aField[i].zName, val);
26339 switch( ofst ){
26340 case 56: {
26341 if( val==1 ) sqlite3_fputs(" (utf8)", p->out);
26342 if( val==2 ) sqlite3_fputs(" (utf16le)", p->out);
26343 if( val==3 ) sqlite3_fputs(" (utf16be)", p->out);
26344 }
26345 }
26346 sqlite3_fputs("\n", p->out);
26347 }
26348 if( zDb==0 ){
26349 zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
26350 }else if( cli_strcmp(zDb,"temp")==0 ){
26351 zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
@@ -26876,24 +26354,24 @@
26354 }
26355 for(i=0; i<ArraySize(aQuery); i++){
26356 char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
26357 int val = db_int(p->db, zSql);
26358 sqlite3_free(zSql);
26359 sqlite3_fprintf(p->out, "%-20s %d\n", aQuery[i].zName, val);
26360 }
26361 sqlite3_free(zSchemaTab);
26362 sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
26363 sqlite3_fprintf(p->out, "%-20s %u\n", "data version", iDataVersion);
26364 return 0;
26365 }
26366 #endif /* SQLITE_SHELL_HAVE_RECOVER */
26367
26368 /*
26369 ** Print the given string as an error message.
26370 */
26371 static void shellEmitError(const char *zErr){
26372 sqlite3_fprintf(stderr,"Error: %s\n", zErr);
26373 }
26374 /*
26375 ** Print the current sqlite3_errmsg() value to stderr and return 1.
26376 */
26377 static int shellDatabaseError(sqlite3 *db){
@@ -27136,10 +26614,11 @@
26614 int bGroupByParent = 0; /* If -groupbyparent is present */
26615 int i; /* To iterate through azArg[] */
26616 const char *zIndent = ""; /* How much to indent CREATE INDEX by */
26617 int rc; /* Return code */
26618 sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */
26619 FILE *out = pState->out; /* Send output here */
26620
26621 /*
26622 ** This SELECT statement returns one row for each foreign key constraint
26623 ** in the schema of the main database. The column values are:
26624 **
@@ -27211,11 +26690,12 @@
26690 else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
26691 bGroupByParent = 1;
26692 zIndent = " ";
26693 }
26694 else{
26695 sqlite3_fprintf(stderr,
26696 "Usage: %s %s ?-verbose? ?-groupbyparent?\n", azArg[0], azArg[1]);
26697 return SQLITE_ERROR;
26698 }
26699 }
26700
26701 /* Register the fkey_collate_clause() SQL function */
@@ -27255,44 +26735,45 @@
26735 }
26736 rc = sqlite3_finalize(pExplain);
26737 if( rc!=SQLITE_OK ) break;
26738
26739 if( res<0 ){
26740 sqlite3_fputs("Error: internal error", stderr);
26741 break;
26742 }else{
26743 if( bGroupByParent
26744 && (bVerbose || res==0)
26745 && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
26746 ){
26747 sqlite3_fprintf(out, "-- Parent table %s\n", zParent);
26748 sqlite3_free(zPrev);
26749 zPrev = sqlite3_mprintf("%s", zParent);
26750 }
26751
26752 if( res==0 ){
26753 sqlite3_fprintf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
26754 }else if( bVerbose ){
26755 sqlite3_fprintf(out,
26756 "%s/* no extra indexes required for %s -> %s */\n",
26757 zIndent, zFrom, zTarget
26758 );
26759 }
26760 }
26761 }
26762 sqlite3_free(zPrev);
26763
26764 if( rc!=SQLITE_OK ){
26765 sqlite3_fprintf(stderr,"%s\n", sqlite3_errmsg(db));
26766 }
26767
26768 rc2 = sqlite3_finalize(pSql);
26769 if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
26770 rc = rc2;
26771 sqlite3_fprintf(stderr,"%s\n", sqlite3_errmsg(db));
26772 }
26773 }else{
26774 sqlite3_fprintf(stderr,"%s\n", sqlite3_errmsg(db));
26775 }
26776
26777 return rc;
26778 }
26779
@@ -27308,13 +26789,13 @@
26789 n = (nArg>=2 ? strlen30(azArg[1]) : 0);
26790 if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
26791 return lintFkeyIndexes(pState, azArg, nArg);
26792
26793 usage:
26794 sqlite3_fprintf(stderr,"Usage %s sub-command ?switches...?\n", azArg[0]);
26795 sqlite3_fprintf(stderr, "Where sub-commands are:\n");
26796 sqlite3_fprintf(stderr, " fkey-indexes\n");
26797 return SQLITE_ERROR;
26798 }
26799
26800 static void shellPrepare(
26801 sqlite3 *db,
@@ -27324,11 +26805,12 @@
26805 ){
26806 *ppStmt = 0;
26807 if( *pRc==SQLITE_OK ){
26808 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
26809 if( rc!=SQLITE_OK ){
26810 sqlite3_fprintf(stderr,
26811 "sql error: %s (%d)\n", sqlite3_errmsg(db), sqlite3_errcode(db));
26812 *pRc = rc;
26813 }
26814 }
26815 }
26816
@@ -27368,11 +26850,11 @@
26850 if( pStmt ){
26851 sqlite3 *db = sqlite3_db_handle(pStmt);
26852 int rc = sqlite3_finalize(pStmt);
26853 if( *pRc==SQLITE_OK ){
26854 if( rc!=SQLITE_OK ){
26855 sqlite3_fprintf(stderr,"SQL error: %s\n", sqlite3_errmsg(db));
26856 }
26857 *pRc = rc;
26858 }
26859 }
26860 }
@@ -27390,11 +26872,11 @@
26872 ){
26873 int rc = sqlite3_reset(pStmt);
26874 if( *pRc==SQLITE_OK ){
26875 if( rc!=SQLITE_OK ){
26876 sqlite3 *db = sqlite3_db_handle(pStmt);
26877 sqlite3_fprintf(stderr,"SQL error: %s\n", sqlite3_errmsg(db));
26878 }
26879 *pRc = rc;
26880 }
26881 }
26882 #endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
@@ -27419,10 +26901,11 @@
26901 char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */
26902 const char *zFile; /* --file argument, or NULL */
26903 const char *zDir; /* --directory argument, or NULL */
26904 char **azArg; /* Array of command arguments */
26905 ShellState *p; /* Shell state */
26906 FILE *out; /* Output to this stream */
26907 sqlite3 *db; /* Database containing the archive */
26908 };
26909
26910 /*
26911 ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
@@ -27442,13 +26925,13 @@
26925 va_start(ap, zFmt);
26926 z = sqlite3_vmprintf(zFmt, ap);
26927 va_end(ap);
26928 shellEmitError(z);
26929 if( pAr->fromCmdLine ){
26930 sqlite3_fputs("Use \"-A\" for more help\n", stderr);
26931 }else{
26932 sqlite3_fputs("Use \".archive --help\" for more help\n", stderr);
26933 }
26934 sqlite3_free(z);
26935 return SQLITE_ERROR;
26936 }
26937
@@ -27544,11 +27027,11 @@
27027 };
27028 int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
27029 struct ArSwitch *pEnd = &aSwitch[nSwitch];
27030
27031 if( nArg<=1 ){
27032 sqlite3_fprintf(stderr, "Wrong number of arguments. Usage:\n");
27033 return arUsage(stderr);
27034 }else{
27035 char *z = azArg[1];
27036 if( z[0]!='-' ){
27037 /* Traditional style [tar] invocation */
@@ -27650,11 +27133,11 @@
27133 }
27134 }
27135 }
27136 }
27137 if( pAr->eCmd==0 ){
27138 sqlite3_fprintf(stderr, "Required argument missing. Usage:\n");
27139 return arUsage(stderr);
27140 }
27141 return SQLITE_OK;
27142 }
27143
@@ -27693,11 +27176,11 @@
27176 if( SQLITE_ROW==sqlite3_step(pTest) ){
27177 bOk = 1;
27178 }
27179 shellReset(&rc, pTest);
27180 if( rc==SQLITE_OK && bOk==0 ){
27181 sqlite3_fprintf(stderr,"not found in archive: %s\n", z);
27182 rc = SQLITE_ERROR;
27183 }
27184 }
27185 shellFinalize(&rc, pTest);
27186 }
@@ -27760,19 +27243,19 @@
27243 arWhereClause(&rc, pAr, &zWhere);
27244
27245 shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
27246 pAr->zSrcTable, zWhere);
27247 if( pAr->bDryRun ){
27248 sqlite3_fprintf(pAr->out, "%s\n", sqlite3_sql(pSql));
27249 }else{
27250 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
27251 if( pAr->bVerbose ){
27252 sqlite3_fprintf(pAr->out, "%s % 10d %s %s\n",
27253 sqlite3_column_text(pSql, 0), sqlite3_column_int(pSql, 1),
27254 sqlite3_column_text(pSql, 2),sqlite3_column_text(pSql, 3));
27255 }else{
27256 sqlite3_fprintf(pAr->out, "%s\n", sqlite3_column_text(pSql, 0));
27257 }
27258 }
27259 }
27260 shellFinalize(&rc, pSql);
27261 sqlite3_free(zWhere);
@@ -27795,11 +27278,11 @@
27278 }
27279 if( rc==SQLITE_OK ){
27280 zSql = sqlite3_mprintf("DELETE FROM %s WHERE %s;",
27281 pAr->zSrcTable, zWhere);
27282 if( pAr->bDryRun ){
27283 sqlite3_fprintf(pAr->out, "%s\n", zSql);
27284 }else{
27285 char *zErr = 0;
27286 rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0);
27287 if( rc==SQLITE_OK ){
27288 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
@@ -27808,11 +27291,11 @@
27291 }else{
27292 rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0);
27293 }
27294 }
27295 if( zErr ){
27296 sqlite3_fprintf(stdout, "ERROR: %s\n", zErr); /* stdout? */
27297 sqlite3_free(zErr);
27298 }
27299 }
27300 }
27301 sqlite3_free(zWhere);
@@ -27872,15 +27355,15 @@
27355 ** populating them changes the timestamp). */
27356 for(i=0; i<2; i++){
27357 j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
27358 sqlite3_bind_int(pSql, j, i);
27359 if( pAr->bDryRun ){
27360 sqlite3_fprintf(pAr->out, "%s\n", sqlite3_sql(pSql));
27361 }else{
27362 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
27363 if( i==0 && pAr->bVerbose ){
27364 sqlite3_fprintf(pAr->out, "%s\n", sqlite3_column_text(pSql, 0));
27365 }
27366 }
27367 }
27368 shellReset(&rc, pSql);
27369 }
@@ -27896,17 +27379,17 @@
27379 ** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out.
27380 */
27381 static int arExecSql(ArCommand *pAr, const char *zSql){
27382 int rc;
27383 if( pAr->bDryRun ){
27384 sqlite3_fprintf(pAr->out, "%s\n", zSql);
27385 rc = SQLITE_OK;
27386 }else{
27387 char *zErr = 0;
27388 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
27389 if( zErr ){
27390 sqlite3_fprintf(stdout, "ERROR: %s\n", zErr);
27391 sqlite3_free(zErr);
27392 }
27393 }
27394 return rc;
27395 }
@@ -28051,10 +27534,11 @@
27534 cmd.fromCmdLine = fromCmdLine;
27535 rc = arParseCommand(azArg, nArg, &cmd);
27536 if( rc==SQLITE_OK ){
27537 int eDbType = SHELL_OPEN_UNSPEC;
27538 cmd.p = pState;
27539 cmd.out = pState->out;
27540 cmd.db = pState->db;
27541 if( cmd.zFile ){
27542 eDbType = deduceDatabaseType(cmd.zFile, 1);
27543 }else{
27544 eDbType = pState->openMode;
@@ -28077,17 +27561,18 @@
27561 }else{
27562 flags = SQLITE_OPEN_READONLY;
27563 }
27564 cmd.db = 0;
27565 if( cmd.bDryRun ){
27566 sqlite3_fprintf(cmd.out, "-- open database '%s'%s\n", cmd.zFile,
27567 eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
27568 }
27569 rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
27570 eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
27571 if( rc!=SQLITE_OK ){
27572 sqlite3_fprintf(stderr, "cannot open file: %s (%s)\n",
27573 cmd.zFile, sqlite3_errmsg(cmd.db));
27574 goto end_ar_command;
27575 }
27576 sqlite3_fileio_init(cmd.db, 0, 0);
27577 sqlite3_sqlar_init(cmd.db, 0, 0);
27578 sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
@@ -28096,11 +27581,11 @@
27581 }
27582 if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
27583 if( cmd.eCmd!=AR_CMD_CREATE
27584 && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
27585 ){
27586 sqlite3_fprintf(stderr, "database does not contain an 'sqlar' table\n");
27587 rc = SQLITE_ERROR;
27588 goto end_ar_command;
27589 }
27590 cmd.zSrcTable = sqlite3_mprintf("sqlar");
27591 }
@@ -28154,11 +27639,11 @@
27639 ** This function is used as a callback by the recover extension. Simply
27640 ** print the supplied SQL statement to stdout.
27641 */
27642 static int recoverSqlCb(void *pCtx, const char *zSql){
27643 ShellState *pState = (ShellState*)pCtx;
27644 sqlite3_fprintf(pState->out, "%s;\n", zSql);
27645 return SQLITE_OK;
27646 }
27647
27648 /*
27649 ** This function is called to recover data from the database. A script
@@ -28197,11 +27682,11 @@
27682 }else
27683 if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
27684 bRowids = 0;
27685 }
27686 else{
27687 sqlite3_fprintf(stderr,"unexpected option: %s\n", azArg[i]);
27688 showHelp(pState->out, azArg[0]);
27689 return 1;
27690 }
27691 }
27692
@@ -28216,11 +27701,11 @@
27701
27702 sqlite3_recover_run(p);
27703 if( sqlite3_recover_errcode(p)!=SQLITE_OK ){
27704 const char *zErr = sqlite3_recover_errmsg(p);
27705 int errCode = sqlite3_recover_errcode(p);
27706 sqlite3_fprintf(stderr,"sql error: %s (%d)\n", zErr, errCode);
27707 }
27708 rc = sqlite3_recover_finish(p);
27709 return rc;
27710 }
27711 #endif /* SQLITE_SHELL_HAVE_RECOVER */
@@ -28238,25 +27723,25 @@
27723 i64 nError = 0;
27724 const char *zErr = 0;
27725 while( SQLITE_OK==sqlite3_intck_step(p) ){
27726 const char *zMsg = sqlite3_intck_message(p);
27727 if( zMsg ){
27728 sqlite3_fprintf(pState->out, "%s\n", zMsg);
27729 nError++;
27730 }
27731 nStep++;
27732 if( nStepPerUnlock && (nStep % nStepPerUnlock)==0 ){
27733 sqlite3_intck_unlock(p);
27734 }
27735 }
27736 rc = sqlite3_intck_error(p, &zErr);
27737 if( zErr ){
27738 sqlite3_fprintf(stderr,"%s\n", zErr);
27739 }
27740 sqlite3_intck_close(p);
27741
27742 sqlite3_fprintf(pState->out, "%lld steps, %lld errors\n", nStep, nError);
27743 }
27744
27745 return rc;
27746 }
27747
@@ -28275,11 +27760,11 @@
27760 */
27761 #ifdef SHELL_DEBUG
27762 #define rc_err_oom_die(rc) \
27763 if( rc==SQLITE_NOMEM ) shell_check_oom(0); \
27764 else if(!(rc==SQLITE_OK||rc==SQLITE_DONE)) \
27765 sqlite3_fprintf(stderr,"E:%d\n",rc), assert(0)
27766 #else
27767 static void rc_err_oom_die(int rc){
27768 if( rc==SQLITE_NOMEM ) shell_check_oom(0);
27769 assert(rc==SQLITE_OK||rc==SQLITE_DONE);
27770 }
@@ -28492,12 +27977,13 @@
27977 shellPreparePrintf(p->db, &rc, &pStmt,
27978 "SELECT 1 FROM sqlite_schema o WHERE "
27979 "sql LIKE 'CREATE VIRTUAL TABLE%%' AND %s", zLike ? zLike : "true"
27980 );
27981 if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
27982 sqlite3_fputs("/* WARNING: "
27983 "Script requires that SQLITE_DBCONFIG_DEFENSIVE be disabled */\n",
27984 p->out
27985 );
27986 }
27987 shellFinalize(&rc, pStmt);
27988 return rc;
27989 }
@@ -28524,16 +28010,18 @@
28010 return SQLITE_OK;
28011 }
28012 if( faultsim_state.iCnt ){
28013 if( faultsim_state.iCnt>0 ) faultsim_state.iCnt--;
28014 if( faultsim_state.eVerbose>=2 ){
28015 sqlite3_fprintf(stdout,
28016 "FAULT-SIM id=%d no-fault (cnt=%d)\n", iArg, faultsim_state.iCnt);
28017 }
28018 return SQLITE_OK;
28019 }
28020 if( faultsim_state.eVerbose>=1 ){
28021 sqlite3_fprintf(stdout,
28022 "FAULT-SIM id=%d returns %d\n", iArg, faultsim_state.iErr);
28023 }
28024 faultsim_state.iCnt = faultsim_state.iInterval;
28025 faultsim_state.nHit++;
28026 if( faultsim_state.nRepeat>0 && faultsim_state.nRepeat<=faultsim_state.nHit ){
28027 faultsim_state.iCnt = -1;
@@ -28592,11 +28080,11 @@
28080 clearTempFile(p);
28081
28082 #ifndef SQLITE_OMIT_AUTHORIZATION
28083 if( c=='a' && cli_strncmp(azArg[0], "auth", n)==0 ){
28084 if( nArg!=2 ){
28085 sqlite3_fprintf(stderr, "Usage: .auth ON|OFF\n");
28086 rc = 1;
28087 goto meta_command_exit;
28088 }
28089 open_db(p, 0);
28090 if( booleanValue(azArg[1]) ){
@@ -28639,32 +28127,32 @@
28127 }else
28128 if( cli_strcmp(z, "-async")==0 ){
28129 bAsync = 1;
28130 }else
28131 {
28132 sqlite3_fprintf(stderr,"unknown option: %s\n", azArg[j]);
28133 return 1;
28134 }
28135 }else if( zDestFile==0 ){
28136 zDestFile = azArg[j];
28137 }else if( zDb==0 ){
28138 zDb = zDestFile;
28139 zDestFile = azArg[j];
28140 }else{
28141 sqlite3_fprintf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
28142 return 1;
28143 }
28144 }
28145 if( zDestFile==0 ){
28146 sqlite3_fprintf(stderr, "missing FILENAME argument on .backup\n");
28147 return 1;
28148 }
28149 if( zDb==0 ) zDb = "main";
28150 rc = sqlite3_open_v2(zDestFile, &pDest,
28151 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
28152 if( rc!=SQLITE_OK ){
28153 sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", zDestFile);
28154 close_db(pDest);
28155 return 1;
28156 }
28157 if( bAsync ){
28158 sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
@@ -28698,21 +28186,12 @@
28186 }
28187 }else
28188
28189 /* Undocumented. Legacy only. See "crnl" below */
28190 if( c=='b' && n>=3 && cli_strncmp(azArg[0], "binary", n)==0 ){
28191 eputz("The \".binary\" command is deprecated. Use \".crnl\" instead.\n");
28192 rc = 1;
 
 
 
 
 
 
 
 
 
28193 }else
28194
28195 /* The undocumented ".breakpoint" command causes a call to the no-op
28196 ** routine named test_breakpoint().
28197 */
@@ -28730,11 +28209,11 @@
28209 sqlite3_free(z);
28210 #else
28211 rc = chdir(azArg[1]);
28212 #endif
28213 if( rc ){
28214 sqlite3_fprintf(stderr,"Cannot change to directory \"%s\"\n", azArg[1]);
28215 rc = 1;
28216 }
28217 }else{
28218 eputz("Usage: .cd DIRECTORY\n");
28219 rc = 1;
@@ -28763,15 +28242,16 @@
28242 eputz("Usage: .check GLOB-PATTERN\n");
28243 rc = 2;
28244 }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
28245 rc = 2;
28246 }else if( testcase_glob(azArg[1],zRes)==0 ){
28247 sqlite3_fprintf(stderr,
28248 "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
28249 p->zTestcase, azArg[1], zRes);
28250 rc = 1;
28251 }else{
28252 sqlite3_fprintf(p->out, "testcase-%s ok\n", p->zTestcase);
28253 p->nCheck++;
28254 }
28255 sqlite3_free(zRes);
28256 }else
28257 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
@@ -28800,13 +28280,13 @@
28280 zFile = "(memory)";
28281 }else if( zFile[0]==0 ){
28282 zFile = "(temporary-file)";
28283 }
28284 if( p->pAuxDb == &p->aAuxDb[i] ){
28285 sqlite3_fprintf(stdout, "ACTIVE %d: %s\n", i, zFile);
28286 }else if( p->aAuxDb[i].db!=0 ){
28287 sqlite3_fprintf(stdout, " %d: %s\n", i, zFile);
28288 }
28289 }
28290 }else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){
28291 int i = azArg[1][0] - '0';
28292 if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && i<ArraySize(p->aAuxDb) ){
@@ -28833,23 +28313,25 @@
28313 rc = 1;
28314 }
28315 }else
28316
28317 if( c=='c' && n==4 && cli_strncmp(azArg[0], "crnl", n)==0 ){
28318 #if !defined(_WIN32) || defined(SQLITE_U8TEXT_ONLY) \
28319 || defined(SQLITE_U8TEXT_STDIO)
28320 sqlite3_fputs("The \".crnl\" command is disable in this build.\n", p->out);
28321 #else
28322 if( nArg==2 ){
28323 if( booleanValue(azArg[1]) ){
28324 sqlite3_fsetmode(p->out, _O_TEXT);
28325 }else{
28326 sqlite3_fsetmode(p->out, _O_BINARY);
28327 }
28328 }else{
 
 
 
28329 eputz("Usage: .crnl on|off\n");
28330 rc = 1;
28331 }
28332 #endif
28333 }else
28334
28335 if( c=='d' && n>1 && cli_strncmp(azArg[0], "databases", n)==0 ){
28336 char **azName = 0;
28337 int nName = 0;
@@ -28875,11 +28357,11 @@
28357 sqlite3_finalize(pStmt);
28358 for(i=0; i<nName; i++){
28359 int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
28360 int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
28361 const char *z = azName[i*2+1];
28362 sqlite3_fprintf(p->out, "%s: %s %s%s\n",
28363 azName[i*2], z && z[0] ? z : "\"\"", bRdonly ? "r/o" : "r/w",
28364 eTxn==SQLITE_TXN_NONE ? "" :
28365 eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
28366 free(azName[i*2]);
28367 free(azName[i*2+1]);
@@ -28917,15 +28399,16 @@
28399 if( nArg>1 && cli_strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
28400 if( nArg>=3 ){
28401 sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
28402 }
28403 sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
28404 sqlite3_fprintf(p->out, "%19s %s\n",
28405 aDbConfig[ii].zName, v ? "on" : "off");
28406 if( nArg>1 ) break;
28407 }
28408 if( nArg>1 && ii==ArraySize(aDbConfig) ){
28409 sqlite3_fprintf(stderr,"Error: unknown dbconfig \"%s\"\n", azArg[1]);
28410 eputz("Enter \".dbconfig\" with no arguments for a list\n");
28411 }
28412 }else
28413
28414 #if SQLITE_SHELL_HAVE_RECOVER
@@ -28971,11 +28454,12 @@
28454 }else
28455 if( cli_strcmp(z,"nosys")==0 ){
28456 ShellSetFlag(p, SHFLG_DumpNoSys);
28457 }else
28458 {
28459 sqlite3_fprintf(stderr,
28460 "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
28461 rc = 1;
28462 sqlite3_free(zLike);
28463 goto meta_command_exit;
28464 }
28465 }else{
@@ -29006,12 +28490,12 @@
28490 outputDumpWarning(p, zLike);
28491 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
28492 /* When playing back a "dump", the content might appear in an order
28493 ** which causes immediate foreign key constraints to be violated.
28494 ** So disable foreign-key constraint enforcement to prevent problems. */
28495 sqlite3_fputs("PRAGMA foreign_keys=OFF;\n", p->out);
28496 sqlite3_fputs("BEGIN TRANSACTION;\n", p->out);
28497 }
28498 p->writableSchema = 0;
28499 p->showHeader = 0;
28500 /* Set writable_schema=ON since doing so forces SQLite to initialize
28501 ** as much of the schema as it can even if the sqlite_schema table is
@@ -29039,17 +28523,17 @@
28523 run_table_dump_query(p, zSql);
28524 sqlite3_free(zSql);
28525 }
28526 sqlite3_free(zLike);
28527 if( p->writableSchema ){
28528 sqlite3_fputs("PRAGMA writable_schema=OFF;\n", p->out);
28529 p->writableSchema = 0;
28530 }
28531 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
28532 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
28533 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
28534 sqlite3_fputs(p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n", p->out);
28535 }
28536 p->showHeader = savedShowHeader;
28537 p->shellFlgs = savedShellFlags;
28538 }else
28539
@@ -29125,11 +28609,12 @@
28609 }else
28610
28611 #ifndef SQLITE_OMIT_VIRTUALTABLE
28612 if( c=='e' && cli_strncmp(azArg[0], "expert", n)==0 ){
28613 if( p->bSafeMode ){
28614 sqlite3_fprintf(stderr,
28615 "Cannot run experimental commands such as \"%s\" in safe mode\n",
28616 azArg[0]);
28617 rc = 1;
28618 }else{
28619 open_db(p, 0);
28620 expertDotCommand(p, azArg, nArg);
@@ -29182,13 +28667,14 @@
28667 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
28668 }
28669
28670 /* --help lists all file-controls */
28671 if( cli_strcmp(zCmd,"help")==0 ){
28672 sqlite3_fputs("Available file-controls:\n", p->out);
28673 for(i=0; i<ArraySize(aCtrl); i++){
28674 sqlite3_fprintf(p->out,
28675 " .filectrl %s %s\n", aCtrl[i].zCtrlName, aCtrl[i].zUsage);
28676 }
28677 rc = 1;
28678 goto meta_command_exit;
28679 }
28680
@@ -29199,19 +28685,19 @@
28685 if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
28686 if( filectrl<0 ){
28687 filectrl = aCtrl[i].ctrlCode;
28688 iCtrl = i;
28689 }else{
28690 sqlite3_fprintf(stderr,"Error: ambiguous file-control: \"%s\"\n"
28691 "Use \".filectrl --help\" for help\n", zCmd);
28692 rc = 1;
28693 goto meta_command_exit;
28694 }
28695 }
28696 }
28697 if( filectrl<0 ){
28698 sqlite3_fprintf(stderr,"Error: unknown file-control: %s\n"
28699 "Use \".filectrl --help\" for help\n", zCmd);
28700 }else{
28701 switch(filectrl){
28702 case SQLITE_FCNTL_SIZE_LIMIT: {
28703 if( nArg!=2 && nArg!=3 ) break;
@@ -29251,11 +28737,11 @@
28737 case SQLITE_FCNTL_TEMPFILENAME: {
28738 char *z = 0;
28739 if( nArg!=2 ) break;
28740 sqlite3_file_control(p->db, zSchema, filectrl, &z);
28741 if( z ){
28742 sqlite3_fprintf(p->out, "%s\n", z);
28743 sqlite3_free(z);
28744 }
28745 isOk = 2;
28746 break;
28747 }
@@ -29265,23 +28751,24 @@
28751 x = atoi(azArg[2]);
28752 sqlite3_file_control(p->db, zSchema, filectrl, &x);
28753 }
28754 x = -1;
28755 sqlite3_file_control(p->db, zSchema, filectrl, &x);
28756 sqlite3_fprintf(p->out, "%d\n", x);
28757 isOk = 2;
28758 break;
28759 }
28760 }
28761 }
28762 if( isOk==0 && iCtrl>=0 ){
28763 sqlite3_fprintf(p->out, "Usage: .filectrl %s %s\n",
28764 zCmd, aCtrl[iCtrl].zUsage);
28765 rc = 1;
28766 }else if( isOk==1 ){
28767 char zBuf[100];
28768 sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
28769 sqlite3_fprintf(p->out, "%s\n", zBuf);
28770 }
28771 }else
28772
28773 if( c=='f' && cli_strncmp(azArg[0], "fullschema", n)==0 ){
28774 ShellState data;
@@ -29318,19 +28805,19 @@
28805 doStats = sqlite3_step(pStmt)==SQLITE_ROW;
28806 sqlite3_finalize(pStmt);
28807 }
28808 }
28809 if( doStats==0 ){
28810 sqlite3_fputs("/* No STAT tables available */\n", p->out);
28811 }else{
28812 sqlite3_fputs("ANALYZE sqlite_schema;\n", p->out);
28813 data.cMode = data.mode = MODE_Insert;
28814 data.zDestTable = "sqlite_stat1";
28815 shell_exec(&data, "SELECT * FROM sqlite_stat1", 0);
28816 data.zDestTable = "sqlite_stat4";
28817 shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
28818 sqlite3_fputs("ANALYZE sqlite_schema;\n", p->out);
28819 }
28820 }else
28821
28822 if( c=='h' && cli_strncmp(azArg[0], "headers", n)==0 ){
28823 if( nArg==2 ){
@@ -29344,11 +28831,11 @@
28831
28832 if( c=='h' && cli_strncmp(azArg[0], "help", n)==0 ){
28833 if( nArg>=2 ){
28834 n = showHelp(p->out, azArg[1]);
28835 if( n==0 ){
28836 sqlite3_fprintf(p->out, "Nothing matches '%s'\n", azArg[1]);
28837 }
28838 }else{
28839 showHelp(p->out, 0);
28840 }
28841 }else
@@ -29387,11 +28874,11 @@
28874 if( zFile==0 ){
28875 zFile = z;
28876 }else if( zTable==0 ){
28877 zTable = z;
28878 }else{
28879 sqlite3_fprintf(p->out, "ERROR: extra argument: \"%s\". Usage:\n",z);
28880 showHelp(p->out, "import");
28881 goto meta_command_exit;
28882 }
28883 }else if( cli_strcmp(z,"-v")==0 ){
28884 eVerbose++;
@@ -29408,17 +28895,17 @@
28895 sCtx.cColSep = ',';
28896 sCtx.cRowSep = '\n';
28897 xRead = csv_read_one_field;
28898 useOutputMode = 0;
28899 }else{
28900 sqlite3_fprintf(p->out, "ERROR: unknown option: \"%s\". Usage:\n", z);
28901 showHelp(p->out, "import");
28902 goto meta_command_exit;
28903 }
28904 }
28905 if( zTable==0 ){
28906 sqlite3_fprintf(p->out, "ERROR: missing %s argument. Usage:\n",
28907 zFile==0 ? "FILE" : "TABLE");
28908 showHelp(p->out, "import");
28909 goto meta_command_exit;
28910 }
28911 seenInterrupt = 0;
@@ -29464,32 +28951,32 @@
28951 if( sCtx.zFile[0]=='|' ){
28952 #ifdef SQLITE_OMIT_POPEN
28953 eputz("Error: pipes are not supported in this OS\n");
28954 goto meta_command_exit;
28955 #else
28956 sCtx.in = sqlite3_popen(sCtx.zFile+1, "r");
28957 sCtx.zFile = "<pipe>";
28958 sCtx.xCloser = pclose;
28959 #endif
28960 }else{
28961 sCtx.in = sqlite3_fopen(sCtx.zFile, "rb");
28962 sCtx.xCloser = fclose;
28963 }
28964 if( sCtx.in==0 ){
28965 sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", zFile);
28966 goto meta_command_exit;
28967 }
28968 if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
28969 char zSep[2];
28970 zSep[1] = 0;
28971 zSep[0] = sCtx.cColSep;
28972 sqlite3_fputs("Column separator ", p->out);
28973 output_c_string(p->out, zSep);
28974 sqlite3_fputs(", row separator ", p->out);
28975 zSep[0] = sCtx.cRowSep;
28976 output_c_string(p->out, zSep);
28977 sqlite3_fputs("\n", p->out);
28978 }
28979 sCtx.z = sqlite3_malloc64(120);
28980 if( sCtx.z==0 ){
28981 import_cleanup(&sCtx);
28982 shell_out_of_memory();
@@ -29510,18 +28997,18 @@
28997 zAutoColumn(sCtx.z, &dbCols, 0);
28998 if( sCtx.cTerm!=sCtx.cColSep ) break;
28999 }
29000 zColDefs = zAutoColumn(0, &dbCols, &zRenames);
29001 if( zRenames!=0 ){
29002 sqlite3_fprintf((stdin_is_interactive && p->in==stdin)? p->out : stderr,
29003 "Columns renamed during .import %s due to duplicates:\n"
29004 "%s\n", sCtx.zFile, zRenames);
29005 sqlite3_free(zRenames);
29006 }
29007 assert(dbCols==0);
29008 if( zColDefs==0 ){
29009 sqlite3_fprintf(stderr,"%s: empty file\n", sCtx.zFile);
29010 import_cleanup(&sCtx);
29011 rc = 1;
29012 sqlite3_free(zCreate);
29013 goto meta_command_exit;
29014 }
@@ -29529,17 +29016,20 @@
29016 if( zCreate==0 ){
29017 import_cleanup(&sCtx);
29018 shell_out_of_memory();
29019 }
29020 if( eVerbose>=1 ){
29021 sqlite3_fprintf(p->out, "%s\n", zCreate);
29022 }
29023 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
29024 if( rc ){
29025 sqlite3_fprintf(stderr,
29026 "%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db));
29027 }
29028 sqlite3_free(zCreate);
29029 zCreate = 0;
29030 if( rc ){
 
29031 import_cleanup(&sCtx);
29032 rc = 1;
29033 goto meta_command_exit;
29034 }
29035 }
@@ -29590,11 +29080,11 @@
29080 }
29081 zSql[j++] = ')';
29082 zSql[j] = 0;
29083 assert( j<nByte );
29084 if( eVerbose>=2 ){
29085 sqlite3_fprintf(p->out, "Insert using: %s\n", zSql);
29086 }
29087 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
29088 sqlite3_free(zSql);
29089 zSql = 0;
29090 if( rc ){
@@ -29629,11 +29119,11 @@
29119 if( z==0 && (xRead==csv_read_one_field) && i==nCol-1 && i>0 ){
29120 z = "";
29121 }
29122 sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
29123 if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
29124 sqlite3_fprintf(stderr,"%s:%d: expected %d columns but found %d"
29125 " - filling the rest with NULL\n",
29126 sCtx.zFile, startLine, nCol, i+1);
29127 i += 2;
29128 while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
29129 }
@@ -29641,18 +29131,19 @@
29131 if( sCtx.cTerm==sCtx.cColSep ){
29132 do{
29133 xRead(&sCtx);
29134 i++;
29135 }while( sCtx.cTerm==sCtx.cColSep );
29136 sqlite3_fprintf(stderr,
29137 "%s:%d: expected %d columns but found %d - extras ignored\n",
29138 sCtx.zFile, startLine, nCol, i);
29139 }
29140 if( i>=nCol ){
29141 sqlite3_step(pStmt);
29142 rc = sqlite3_reset(pStmt);
29143 if( rc!=SQLITE_OK ){
29144 sqlite3_fprintf(stderr,"%s:%d: INSERT failed: %s\n",
29145 sCtx.zFile, startLine, sqlite3_errmsg(p->db));
29146 sCtx.nErr++;
29147 }else{
29148 sCtx.nRow++;
29149 }
@@ -29661,11 +29152,12 @@
29152
29153 import_cleanup(&sCtx);
29154 sqlite3_finalize(pStmt);
29155 if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
29156 if( eVerbose>0 ){
29157 sqlite3_fprintf(p->out,
29158 "Added %d rows with %d errors using %d lines of input\n",
29159 sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
29160 }
29161 }else
29162 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
29163
@@ -29677,11 +29169,11 @@
29169 int tnum = 0;
29170 int isWO = 0; /* True if making an imposter of a WITHOUT ROWID table */
29171 int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
29172 int i;
29173 if( !ShellHasFlag(p,SHFLG_TestingMode) ){
29174 sqlite3_fprintf(stderr,".%s unavailable without --unsafe-testing\n",
29175 "imposter");
29176 rc = 1;
29177 goto meta_command_exit;
29178 }
29179 if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
@@ -29743,11 +29235,11 @@
29235 zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
29236 }
29237 }
29238 sqlite3_finalize(pStmt);
29239 if( i==0 || tnum==0 ){
29240 sqlite3_fprintf(stderr,"no such index: \"%s\"\n", azArg[1]);
29241 rc = 1;
29242 sqlite3_free(zCollist);
29243 goto meta_command_exit;
29244 }
29245 if( lenPK==0 ) lenPK = 100000;
@@ -29758,18 +29250,20 @@
29250 rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
29251 if( rc==SQLITE_OK ){
29252 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
29253 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
29254 if( rc ){
29255 sqlite3_fprintf(stderr,
29256 "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
29257 }else{
29258 sqlite3_fprintf(stdout, "%s;\n", zSql);
29259 sqlite3_fprintf(stdout,
29260 "WARNING: writing to an imposter table will corrupt"
29261 " the \"%s\" %s!\n", azArg[1], isWO ? "table" : "index");
29262 }
29263 }else{
29264 sqlite3_fprintf(stderr,"SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
29265 rc = 1;
29266 }
29267 sqlite3_free(zSql);
29268 }else
29269 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
@@ -29779,11 +29273,11 @@
29273 if( nArg==2 ){
29274 iArg = integerValue(azArg[1]);
29275 if( iArg==0 ) iArg = -1;
29276 }
29277 if( (nArg!=1 && nArg!=2) || iArg<0 ){
29278 sqlite3_fprintf(stderr,"%s","Usage: .intck STEPS_PER_UNLOCK\n");
29279 rc = 1;
29280 goto meta_command_exit;
29281 }
29282 open_db(p, 0);
29283 rc = intckDatabaseCmd(p, iArg);
@@ -29798,13 +29292,13 @@
29292 sqlite3IoTrace = 0;
29293 }else if( cli_strcmp(azArg[1], "-")==0 ){
29294 sqlite3IoTrace = iotracePrintf;
29295 iotrace = stdout;
29296 }else{
29297 iotrace = sqlite3_fopen(azArg[1], "w");
29298 if( iotrace==0 ){
29299 sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
29300 sqlite3IoTrace = 0;
29301 rc = 1;
29302 }else{
29303 sqlite3IoTrace = iotracePrintf;
29304 }
@@ -29832,11 +29326,11 @@
29326 };
29327 int i, n2;
29328 open_db(p, 0);
29329 if( nArg==1 ){
29330 for(i=0; i<ArraySize(aLimit); i++){
29331 sqlite3_fprintf(stdout, "%20s %d\n", aLimit[i].zLimitName,
29332 sqlite3_limit(p->db, aLimit[i].limitCode, -1));
29333 }
29334 }else if( nArg>3 ){
29335 eputz("Usage: .limit NAME ?NEW-VALUE?\n");
29336 rc = 1;
@@ -29847,28 +29341,28 @@
29341 for(i=0; i<ArraySize(aLimit); i++){
29342 if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
29343 if( iLimit<0 ){
29344 iLimit = i;
29345 }else{
29346 sqlite3_fprintf(stderr,"ambiguous limit: \"%s\"\n", azArg[1]);
29347 rc = 1;
29348 goto meta_command_exit;
29349 }
29350 }
29351 }
29352 if( iLimit<0 ){
29353 sqlite3_fprintf(stderr,"unknown limit: \"%s\"\n"
29354 "enter \".limits\" with no arguments for a list.\n",
29355 azArg[1]);
29356 rc = 1;
29357 goto meta_command_exit;
29358 }
29359 if( nArg==3 ){
29360 sqlite3_limit(p->db, aLimit[iLimit].limitCode,
29361 (int)integerValue(azArg[2]));
29362 }
29363 sqlite3_fprintf(stdout, "%20s %d\n", aLimit[iLimit].zLimitName,
29364 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
29365 }
29366 }else
29367
29368 if( c=='l' && n>2 && cli_strncmp(azArg[0], "lint", n)==0 ){
@@ -29947,35 +29441,37 @@
29441 cmOpts = cmo;
29442 }
29443 }else if( zTabname==0 ){
29444 zTabname = z;
29445 }else if( z[0]=='-' ){
29446 sqlite3_fprintf(stderr,"unknown option: %s\n", z);
29447 eputz("options:\n"
29448 " --noquote\n"
29449 " --quote\n"
29450 " --wordwrap on/off\n"
29451 " --wrap N\n"
29452 " --ww\n");
29453 rc = 1;
29454 goto meta_command_exit;
29455 }else{
29456 sqlite3_fprintf(stderr,"extra argument: \"%s\"\n", z);
29457 rc = 1;
29458 goto meta_command_exit;
29459 }
29460 }
29461 if( zMode==0 ){
29462 if( p->mode==MODE_Column
29463 || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
29464 ){
29465 sqlite3_fprintf(p->out,
29466 "current output mode: %s --wrap %d --wordwrap %s --%squote\n",
29467 modeDescr[p->mode], p->cmOpts.iWrap,
29468 p->cmOpts.bWordWrap ? "on" : "off",
29469 p->cmOpts.bQuote ? "" : "no");
29470 }else{
29471 sqlite3_fprintf(p->out,
29472 "current output mode: %s\n", modeDescr[p->mode]);
29473 }
29474 zMode = modeDescr[p->mode];
29475 }
29476 n2 = strlen30(zMode);
29477 if( cli_strncmp(zMode,"lines",n2)==0 ){
@@ -30044,11 +29540,11 @@
29540 if( c=='n' && cli_strcmp(azArg[0], "nonce")==0 ){
29541 if( nArg!=2 ){
29542 eputz("Usage: .nonce NONCE\n");
29543 rc = 1;
29544 }else if( p->zNonce==0 || cli_strcmp(azArg[1],p->zNonce)!=0 ){
29545 sqlite3_fprintf(stderr,"line %d: incorrect nonce: \"%s\"\n",
29546 p->lineno, azArg[1]);
29547 exit(1);
29548 }else{
29549 p->bSafeMode = 0;
29550 return 0; /* Return immediately to bypass the safe mode reset
@@ -30099,15 +29595,15 @@
29595 p->szMax = integerValue(azArg[++iName]);
29596 #endif /* SQLITE_OMIT_DESERIALIZE */
29597 }else
29598 #endif /* !SQLITE_SHELL_FIDDLE */
29599 if( z[0]=='-' ){
29600 sqlite3_fprintf(stderr,"unknown option: %s\n", z);
29601 rc = 1;
29602 goto meta_command_exit;
29603 }else if( zFN ){
29604 sqlite3_fprintf(stderr,"extra argument: \"%s\"\n", z);
29605 rc = 1;
29606 goto meta_command_exit;
29607 }else{
29608 zFN = z;
29609 }
@@ -30145,11 +29641,11 @@
29641 zNewFilename = 0;
29642 }
29643 p->pAuxDb->zDbFilename = zNewFilename;
29644 open_db(p, OPEN_DB_KEEPALIVE);
29645 if( p->db==0 ){
29646 sqlite3_fprintf(stderr,"Error: cannot open '%s'\n", zNewFilename);
29647 sqlite3_free(zNewFilename);
29648 }else{
29649 p->pAuxDb->zFreeOnClose = zNewFilename;
29650 }
29651 }
@@ -30163,22 +29659,27 @@
29659 #ifndef SQLITE_SHELL_FIDDLE
29660 if( (c=='o'
29661 && (cli_strncmp(azArg[0], "output", n)==0
29662 || cli_strncmp(azArg[0], "once", n)==0))
29663 || (c=='e' && n==5 && cli_strcmp(azArg[0],"excel")==0)
29664 || (c=='w' && n==3 && cli_strcmp(azArg[0],"www")==0)
29665 ){
29666 char *zFile = 0;
29667 int bTxtMode = 0;
29668 int i;
29669 int eMode = 0;
29670 int bOnce = 0; /* 0: .output, 1: .once, 2: .excel/.www */
29671 int bPlain = 0; /* --plain option */
29672 static const char *zBomUtf8 = "\xef\xbb\xbf";
29673 const char *zBom = 0;
29674
29675 failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
29676 if( c=='e' ){
29677 eMode = 'x';
29678 bOnce = 2;
29679 }else if( c=='w' ){
29680 eMode = 'w';
29681 bOnce = 2;
29682 }else if( cli_strncmp(azArg[0],"once",n)==0 ){
29683 bOnce = 1;
29684 }
29685 for(i=1; i<nArg; i++){
@@ -30185,28 +29686,34 @@
29686 char *z = azArg[i];
29687 if( z[0]=='-' ){
29688 if( z[1]=='-' ) z++;
29689 if( cli_strcmp(z,"-bom")==0 ){
29690 zBom = zBomUtf8;
29691 }else if( cli_strcmp(z,"-plain")==0 ){
29692 bPlain = 1;
29693 }else if( c=='o' && cli_strcmp(z,"-x")==0 ){
29694 eMode = 'x'; /* spreadsheet */
29695 }else if( c=='o' && cli_strcmp(z,"-e")==0 ){
29696 eMode = 'e'; /* text editor */
29697 }else if( c=='o' && cli_strcmp(z,"-w")==0 ){
29698 eMode = 'w'; /* Web browser */
29699 }else{
29700 sqlite3_fprintf(p->out,
29701 "ERROR: unknown option: \"%s\". Usage:\n", azArg[i]);
29702 showHelp(p->out, azArg[0]);
29703 rc = 1;
29704 goto meta_command_exit;
29705 }
29706 }else if( zFile==0 && eMode==0 ){
29707 zFile = sqlite3_mprintf("%s", z);
29708 if( zFile && zFile[0]=='|' ){
29709 while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
29710 break;
29711 }
29712 }else{
29713 sqlite3_fprintf(p->out,
29714 "ERROR: extra parameter: \"%s\". Usage:\n", azArg[i]);
29715 showHelp(p->out, azArg[0]);
29716 rc = 1;
29717 sqlite3_free(zFile);
29718 goto meta_command_exit;
29719 }
@@ -30219,20 +29726,29 @@
29726 }else{
29727 p->outCount = 0;
29728 }
29729 output_reset(p);
29730 #ifndef SQLITE_NOHAVE_SYSTEM
29731 if( eMode=='e' || eMode=='x' || eMode=='w' ){
29732 p->doXdgOpen = 1;
29733 outputModePush(p);
29734 if( eMode=='x' ){
29735 /* spreadsheet mode. Output as CSV. */
29736 newTempFile(p, "csv");
29737 ShellClearFlag(p, SHFLG_Echo);
29738 p->mode = MODE_Csv;
29739 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
29740 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
29741 #ifdef _WIN32
29742 zBom = zBomUtf8; /* Always include the BOM on Windows, as Excel does
29743 ** not work without it. */
29744 #endif
29745 }else if( eMode=='w' ){
29746 /* web-browser mode. */
29747 newTempFile(p, "html");
29748 if( !bPlain ) p->mode = MODE_Www;
29749 bTxtMode = 1;
29750 }else{
29751 /* text editor mode */
29752 newTempFile(p, "txt");
29753 bTxtMode = 1;
29754 }
@@ -30245,30 +29761,36 @@
29761 #ifdef SQLITE_OMIT_POPEN
29762 eputz("Error: pipes are not supported in this OS\n");
29763 rc = 1;
29764 output_redir(p, stdout);
29765 #else
29766 FILE *pfPipe = sqlite3_popen(zFile + 1, "w");
29767 if( pfPipe==0 ){
29768 sqlite3_fprintf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
29769 rc = 1;
29770 }else{
29771 output_redir(p, pfPipe);
29772 if( zBom ) sqlite3_fputs(zBom, pfPipe);
29773 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
29774 }
29775 #endif
29776 }else{
29777 FILE *pfFile = output_file_open(zFile, bTxtMode);
29778 if( pfFile==0 ){
29779 if( cli_strcmp(zFile,"off")!=0 ){
29780 sqlite3_fprintf(stderr,"Error: cannot write to \"%s\"\n", zFile);
29781 }
29782 rc = 1;
29783 } else {
29784 output_redir(p, pfFile);
29785 if( bPlain && eMode=='w' ){
29786 sqlite3_fputs(
29787 "<!DOCTYPE html>\n<BODY>\n<PLAINTEXT>\n",
29788 pfFile
29789 );
29790 }
29791 if( zBom ) sqlite3_fputs(zBom, pfFile);
29792 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
29793 }
29794 }
29795 sqlite3_free(zFile);
29796 }else
@@ -30305,11 +29827,12 @@
29827 if( len ){
29828 rx = sqlite3_prepare_v2(p->db,
29829 "SELECT key, quote(value) "
29830 "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
29831 while( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
29832 sqlite3_fprintf(p->out,
29833 "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
29834 sqlite3_column_text(pStmt,1));
29835 }
29836 sqlite3_finalize(pStmt);
29837 }
29838 }else
@@ -30350,11 +29873,11 @@
29873 "VALUES(%Q,%Q);", zKey, zValue);
29874 shell_check_oom(zSql);
29875 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
29876 sqlite3_free(zSql);
29877 if( rx!=SQLITE_OK ){
29878 sqlite3_fprintf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
29879 sqlite3_finalize(pStmt);
29880 pStmt = 0;
29881 rc = 1;
29882 }
29883 }
@@ -30379,14 +29902,14 @@
29902 }else
29903
29904 if( c=='p' && n>=3 && cli_strncmp(azArg[0], "print", n)==0 ){
29905 int i;
29906 for(i=1; i<nArg; i++){
29907 if( i>1 ) sqlite3_fputs(" ", p->out);
29908 sqlite3_fputs(azArg[i], p->out);
29909 }
29910 sqlite3_fputs("\n", p->out);
29911 }else
29912
29913 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
29914 if( c=='p' && n>=3 && cli_strncmp(azArg[0], "progress", n)==0 ){
29915 int i;
@@ -30419,11 +29942,11 @@
29942 }else{
29943 p->mxProgress = (int)integerValue(azArg[++i]);
29944 }
29945 continue;
29946 }
29947 sqlite3_fprintf(stderr,"Error: unknown option: \"%s\"\n", azArg[i]);
29948 rc = 1;
29949 goto meta_command_exit;
29950 }else{
29951 nn = (int)integerValue(z);
29952 }
@@ -30462,21 +29985,21 @@
29985 #ifdef SQLITE_OMIT_POPEN
29986 eputz("Error: pipes are not supported in this OS\n");
29987 rc = 1;
29988 p->out = stdout;
29989 #else
29990 p->in = sqlite3_popen(azArg[1]+1, "r");
29991 if( p->in==0 ){
29992 sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
29993 rc = 1;
29994 }else{
29995 rc = process_input(p);
29996 pclose(p->in);
29997 }
29998 #endif
29999 }else if( (p->in = openChrSource(azArg[1]))==0 ){
30000 sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
30001 rc = 1;
30002 }else{
30003 rc = process_input(p);
30004 fclose(p->in);
30005 }
@@ -30505,11 +30028,11 @@
30028 rc = 1;
30029 goto meta_command_exit;
30030 }
30031 rc = sqlite3_open(zSrcFile, &pSrc);
30032 if( rc!=SQLITE_OK ){
30033 sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", zSrcFile);
30034 close_db(pSrc);
30035 return 1;
30036 }
30037 open_db(p, 0);
30038 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
@@ -30588,11 +30111,11 @@
30111 }else if( optionMatch(azArg[ii],"debug") ){
30112 bDebug = 1;
30113 }else if( optionMatch(azArg[ii],"nosys") ){
30114 bNoSystemTabs = 1;
30115 }else if( azArg[ii][0]=='-' ){
30116 sqlite3_fprintf(stderr,"Unknown option: \"%s\"\n", azArg[ii]);
30117 rc = 1;
30118 goto meta_command_exit;
30119 }else if( zName==0 ){
30120 zName = azArg[ii];
30121 }else{
@@ -30689,11 +30212,11 @@
30212 appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
30213 }
30214 appendText(&sSelect, "sql IS NOT NULL"
30215 " ORDER BY snum, rowid", 0);
30216 if( bDebug ){
30217 sqlite3_fprintf(p->out, "SQL: %s;\n", sSelect.z);
30218 }else{
30219 rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
30220 }
30221 freeText(&sSelect);
30222 }
@@ -30750,11 +30273,12 @@
30273 session_not_open:
30274 eputz("ERROR: No sessions are open\n");
30275 }else{
30276 rc = sqlite3session_attach(pSession->p, azCmd[1]);
30277 if( rc ){
30278 sqlite3_fprintf(stderr,
30279 "ERROR: sqlite3session_attach() returns %d\n",rc);
30280 rc = 0;
30281 }
30282 }
30283 }else
30284
@@ -30767,13 +30291,13 @@
30291 ){
30292 FILE *out = 0;
30293 failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]);
30294 if( nCmd!=2 ) goto session_syntax_error;
30295 if( pSession->p==0 ) goto session_not_open;
30296 out = sqlite3_fopen(azCmd[1], "wb");
30297 if( out==0 ){
30298 sqlite3_fprintf(stderr,"ERROR: cannot open \"%s\" for writing\n",
30299 azCmd[1]);
30300 }else{
30301 int szChng;
30302 void *pChng;
30303 if( azCmd[0][0]=='c' ){
@@ -30780,16 +30304,17 @@
30304 rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
30305 }else{
30306 rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
30307 }
30308 if( rc ){
30309 sqlite3_fprintf(stdout, "Error: error code %d\n", rc);
30310 rc = 0;
30311 }
30312 if( pChng
30313 && fwrite(pChng, szChng, 1, out)!=1 ){
30314 sqlite3_fprintf(stderr,
30315 "ERROR: Failed to write entire %d-byte output\n", szChng);
30316 }
30317 sqlite3_free(pChng);
30318 fclose(out);
30319 }
30320 }else
@@ -30812,11 +30337,12 @@
30337 int ii;
30338 if( nCmd>2 ) goto session_syntax_error;
30339 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
30340 if( pAuxDb->nSession ){
30341 ii = sqlite3session_enable(pSession->p, ii);
30342 sqlite3_fprintf(p->out,
30343 "session %s enable flag = %d\n", pSession->zName, ii);
30344 }
30345 }else
30346
30347 /* .session filter GLOB ....
30348 ** Set a list of GLOB patterns of table names to be excluded.
@@ -30847,11 +30373,12 @@
30373 int ii;
30374 if( nCmd>2 ) goto session_syntax_error;
30375 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
30376 if( pAuxDb->nSession ){
30377 ii = sqlite3session_indirect(pSession->p, ii);
30378 sqlite3_fprintf(p->out,
30379 "session %s indirect flag = %d\n", pSession->zName, ii);
30380 }
30381 }else
30382
30383 /* .session isempty
30384 ** Determine if the session is empty
@@ -30859,20 +30386,21 @@
30386 if( cli_strcmp(azCmd[0], "isempty")==0 ){
30387 int ii;
30388 if( nCmd!=1 ) goto session_syntax_error;
30389 if( pAuxDb->nSession ){
30390 ii = sqlite3session_isempty(pSession->p);
30391 sqlite3_fprintf(p->out,
30392 "session %s isempty flag = %d\n", pSession->zName, ii);
30393 }
30394 }else
30395
30396 /* .session list
30397 ** List all currently open sessions
30398 */
30399 if( cli_strcmp(azCmd[0],"list")==0 ){
30400 for(i=0; i<pAuxDb->nSession; i++){
30401 sqlite3_fprintf(p->out, "%d %s\n", i, pAuxDb->aSession[i].zName);
30402 }
30403 }else
30404
30405 /* .session open DB NAME
30406 ** Open a new session called NAME on the attached database DB.
@@ -30883,22 +30411,23 @@
30411 if( nCmd!=3 ) goto session_syntax_error;
30412 zName = azCmd[2];
30413 if( zName[0]==0 ) goto session_syntax_error;
30414 for(i=0; i<pAuxDb->nSession; i++){
30415 if( cli_strcmp(pAuxDb->aSession[i].zName,zName)==0 ){
30416 sqlite3_fprintf(stderr,"Session \"%s\" already exists\n", zName);
30417 goto meta_command_exit;
30418 }
30419 }
30420 if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){
30421 sqlite3_fprintf(stderr,
30422 "Maximum of %d sessions\n", ArraySize(pAuxDb->aSession));
30423 goto meta_command_exit;
30424 }
30425 pSession = &pAuxDb->aSession[pAuxDb->nSession];
30426 rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
30427 if( rc ){
30428 sqlite3_fprintf(stderr,"Cannot open session: error code=%d\n", rc);
30429 rc = 0;
30430 goto meta_command_exit;
30431 }
30432 pSession->nFilter = 0;
30433 sqlite3session_table_filter(pSession->p, session_filter, pSession);
@@ -30918,20 +30447,20 @@
30447 if( c=='s' && n>=10 && cli_strncmp(azArg[0], "selftest-", 9)==0 ){
30448 if( cli_strncmp(azArg[0]+9, "boolean", n-9)==0 ){
30449 int i, v;
30450 for(i=1; i<nArg; i++){
30451 v = booleanValue(azArg[i]);
30452 sqlite3_fprintf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
30453 }
30454 }
30455 if( cli_strncmp(azArg[0]+9, "integer", n-9)==0 ){
30456 int i; sqlite3_int64 v;
30457 for(i=1; i<nArg; i++){
30458 char zBuf[200];
30459 v = integerValue(azArg[i]);
30460 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
30461 sqlite3_fputs(zBuf, p->out);
30462 }
30463 }
30464 }else
30465 #endif
30466
@@ -30954,12 +30483,13 @@
30483 }else
30484 if( cli_strcmp(z,"-v")==0 ){
30485 bVerbose++;
30486 }else
30487 {
30488 sqlite3_fprintf(stderr,
30489 "Unknown option \"%s\" on \"%s\"\n", azArg[i], azArg[0]);
30490 sqlite3_fputs("Should be one of: --init -v\n", stderr);
30491 rc = 1;
30492 goto meta_command_exit;
30493 }
30494 }
30495 if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
@@ -31000,46 +30530,47 @@
30530 if( zOp==0 ) continue;
30531 if( zSql==0 ) continue;
30532 if( zAns==0 ) continue;
30533 k = 0;
30534 if( bVerbose>0 ){
30535 sqlite3_fprintf(stdout, "%d: %s %s\n", tno, zOp, zSql);
30536 }
30537 if( cli_strcmp(zOp,"memo")==0 ){
30538 sqlite3_fprintf(p->out, "%s\n", zSql);
30539 }else
30540 if( cli_strcmp(zOp,"run")==0 ){
30541 char *zErrMsg = 0;
30542 str.n = 0;
30543 str.z[0] = 0;
30544 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
30545 nTest++;
30546 if( bVerbose ){
30547 sqlite3_fprintf(p->out, "Result: %s\n", str.z);
30548 }
30549 if( rc || zErrMsg ){
30550 nErr++;
30551 rc = 1;
30552 sqlite3_fprintf(p->out, "%d: error-code-%d: %s\n", tno, rc,zErrMsg);
30553 sqlite3_free(zErrMsg);
30554 }else if( cli_strcmp(zAns,str.z)!=0 ){
30555 nErr++;
30556 rc = 1;
30557 sqlite3_fprintf(p->out, "%d: Expected: [%s]\n", tno, zAns);
30558 sqlite3_fprintf(p->out, "%d: Got: [%s]\n", tno, str.z);
30559 }
30560 }
30561 else{
30562 sqlite3_fprintf(stderr,
30563 "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
30564 rc = 1;
30565 break;
30566 }
30567 } /* End loop over rows of content from SELFTEST */
30568 sqlite3_finalize(pStmt);
30569 } /* End loop over k */
30570 freeText(&str);
30571 sqlite3_fprintf(p->out, "%d errors out of %d tests\n", nErr, nTest);
30572 }else
30573
30574 if( c=='s' && cli_strncmp(azArg[0], "separator", n)==0 ){
30575 if( nArg<2 || nArg>3 ){
30576 eputz("Usage: .separator COL ?ROW?\n");
@@ -31083,11 +30614,12 @@
30614 }else
30615 if( cli_strcmp(z,"debug")==0 ){
30616 bDebug = 1;
30617 }else
30618 {
30619 sqlite3_fprintf(stderr,
30620 "Unknown option \"%s\" on \"%s\"\n", azArg[i], azArg[0]);
30621 showHelp(p->out, azArg[0]);
30622 rc = 1;
30623 goto meta_command_exit;
30624 }
30625 }else if( zLike ){
@@ -31161,11 +30693,11 @@
30693 }
30694 shell_check_oom(zSql);
30695 freeText(&sQuery);
30696 freeText(&sSql);
30697 if( bDebug ){
30698 sqlite3_fprintf(p->out, "%s\n", zSql);
30699 }else{
30700 shell_exec(p, zSql, 0);
30701 }
30702 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && !defined(SQLITE_OMIT_VIRTUALTABLE)
30703 {
@@ -31191,11 +30723,11 @@
30723 "||group_concat('CAST(CAST('||cname||' AS BLOB) AS TEXT)<>'||cname\n"
30724 "|| ' AND typeof('||cname||')=''text'' ',\n"
30725 "' OR ') as query, tname from tabcols group by tname)"
30726 , zRevText);
30727 shell_check_oom(zRevText);
30728 if( bDebug ) sqlite3_fprintf(p->out, "%s\n", zRevText);
30729 lrc = sqlite3_prepare_v2(p->db, zRevText, -1, &pStmt, 0);
30730 if( lrc!=SQLITE_OK ){
30731 /* assert(lrc==SQLITE_NOMEM); // might also be SQLITE_ERROR if the
30732 ** user does cruel and unnatural things like ".limit expr_depth 0". */
30733 rc = 1;
@@ -31204,19 +30736,20 @@
30736 lrc = SQLITE_ROW==sqlite3_step(pStmt);
30737 if( lrc ){
30738 const char *zGenQuery = (char*)sqlite3_column_text(pStmt,0);
30739 sqlite3_stmt *pCheckStmt;
30740 lrc = sqlite3_prepare_v2(p->db, zGenQuery, -1, &pCheckStmt, 0);
30741 if( bDebug ) sqlite3_fprintf(p->out, "%s\n", zGenQuery);
30742 if( lrc!=SQLITE_OK ){
30743 rc = 1;
30744 }else{
30745 if( SQLITE_ROW==sqlite3_step(pCheckStmt) ){
30746 double countIrreversible = sqlite3_column_double(pCheckStmt, 0);
30747 if( countIrreversible>0 ){
30748 int sz = (int)(countIrreversible + 0.5);
30749 sqlite3_fprintf(stderr,
30750 "Digest includes %d invalidly encoded text field%s.\n",
30751 sz, (sz>1)? "s": "");
30752 }
30753 }
30754 sqlite3_finalize(pCheckStmt);
30755 }
@@ -31246,15 +30779,15 @@
30779 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
30780 for(i=2; i<nArg && zCmd!=0; i++){
30781 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
30782 zCmd, azArg[i]);
30783 }
30784 /*consoleRestore();*/
30785 x = zCmd!=0 ? system(zCmd) : 1;
30786 /*consoleRenewSetup();*/
30787 sqlite3_free(zCmd);
30788 if( x ) sqlite3_fprintf(stderr,"System command returns %d\n", x);
30789 }else
30790 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) */
30791
30792 if( c=='s' && cli_strncmp(azArg[0], "show", n)==0 ){
30793 static const char *azBool[] = { "off", "on", "trigger", "full"};
@@ -31263,50 +30796,52 @@
30796 if( nArg!=1 ){
30797 eputz("Usage: .show\n");
30798 rc = 1;
30799 goto meta_command_exit;
30800 }
30801 sqlite3_fprintf(p->out, "%12.12s: %s\n","echo",
30802 azBool[ShellHasFlag(p, SHFLG_Echo)]);
30803 sqlite3_fprintf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
30804 sqlite3_fprintf(p->out, "%12.12s: %s\n","explain",
30805 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
30806 sqlite3_fprintf(p->out, "%12.12s: %s\n","headers",
30807 azBool[p->showHeader!=0]);
30808 if( p->mode==MODE_Column
30809 || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
30810 ){
30811 sqlite3_fprintf(p->out,
30812 "%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode",
30813 modeDescr[p->mode], p->cmOpts.iWrap,
30814 p->cmOpts.bWordWrap ? "on" : "off",
30815 p->cmOpts.bQuote ? "" : "no");
30816 }else{
30817 sqlite3_fprintf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
30818 }
30819 sqlite3_fprintf(p->out, "%12.12s: ", "nullvalue");
30820 output_c_string(p->out, p->nullValue);
30821 sqlite3_fputs("\n", p->out);
30822 sqlite3_fprintf(p->out, "%12.12s: %s\n","output",
30823 strlen30(p->outfile) ? p->outfile : "stdout");
30824 sqlite3_fprintf(p->out, "%12.12s: ", "colseparator");
30825 output_c_string(p->out, p->colSeparator);
30826 sqlite3_fputs("\n", p->out);
30827 sqlite3_fprintf(p->out, "%12.12s: ", "rowseparator");
30828 output_c_string(p->out, p->rowSeparator);
30829 sqlite3_fputs("\n", p->out);
30830 switch( p->statsOn ){
30831 case 0: zOut = "off"; break;
30832 default: zOut = "on"; break;
30833 case 2: zOut = "stmt"; break;
30834 case 3: zOut = "vmstep"; break;
30835 }
30836 sqlite3_fprintf(p->out, "%12.12s: %s\n","stats", zOut);
30837 sqlite3_fprintf(p->out, "%12.12s: ", "width");
30838 for (i=0;i<p->nWidth;i++) {
30839 sqlite3_fprintf(p->out, "%d ", p->colWidth[i]);
30840 }
30841 sqlite3_fputs("\n", p->out);
30842 sqlite3_fprintf(p->out, "%12.12s: %s\n", "filename",
30843 p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : "");
30844 }else
30845
30846 if( c=='s' && cli_strncmp(azArg[0], "stats", n)==0 ){
30847 if( nArg==2 ){
@@ -31420,13 +30955,14 @@
30955 if( nPrintCol<1 ) nPrintCol = 1;
30956 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
30957 for(i=0; i<nPrintRow; i++){
30958 for(j=i; j<nRow; j+=nPrintRow){
30959 char *zSp = j<nPrintRow ? "" : " ";
30960 sqlite3_fprintf(p->out,
30961 "%s%-*s", zSp, maxlen, azResult[j] ? azResult[j]:"");
30962 }
30963 sqlite3_fputs("\n", p->out);
30964 }
30965 }
30966
30967 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
30968 sqlite3_free(azResult);
@@ -31498,14 +31034,14 @@
31034 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
31035 }
31036
31037 /* --help lists all test-controls */
31038 if( cli_strcmp(zCmd,"help")==0 ){
31039 sqlite3_fputs("Available test-controls:\n", p->out);
31040 for(i=0; i<ArraySize(aCtrl); i++){
31041 if( aCtrl[i].unSafe && !ShellHasFlag(p,SHFLG_TestingMode) ) continue;
31042 sqlite3_fprintf(p->out, " .testctrl %s %s\n",
31043 aCtrl[i].zCtrlName, aCtrl[i].zUsage);
31044 }
31045 rc = 1;
31046 goto meta_command_exit;
31047 }
@@ -31518,19 +31054,19 @@
31054 if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
31055 if( testctrl<0 ){
31056 testctrl = aCtrl[i].ctrlCode;
31057 iCtrl = i;
31058 }else{
31059 sqlite3_fprintf(stderr,"Error: ambiguous test-control: \"%s\"\n"
31060 "Use \".testctrl --help\" for help\n", zCmd);
31061 rc = 1;
31062 goto meta_command_exit;
31063 }
31064 }
31065 }
31066 if( testctrl<0 ){
31067 sqlite3_fprintf(stderr,"Error: unknown test-control: %s\n"
31068 "Use \".testctrl --help\" for help\n", zCmd);
31069 }else{
31070 switch(testctrl){
31071
31072 /* Special processing for .testctrl opt MASK ...
@@ -31602,16 +31138,17 @@
31138 int jj;
31139 for(jj=0; jj<ArraySize(aLabel); jj++){
31140 if( sqlite3_stricmp(zLabel, aLabel[jj].zLabel)==0 ) break;
31141 }
31142 if( jj>=ArraySize(aLabel) ){
31143 sqlite3_fprintf(stderr,
31144 "Error: no such optimization: \"%s\"\n", zLabel);
31145 sqlite3_fputs("Should be one of:", stderr);
31146 for(jj=0; jj<ArraySize(aLabel); jj++){
31147 sqlite3_fprintf(stderr," %s", aLabel[jj].zLabel);
31148 }
31149 sqlite3_fputs("\n", stderr);
31150 rc = 1;
31151 goto meta_command_exit;
31152 }
31153 if( useLabel=='+' ){
31154 newOpt &= ~aLabel[jj].mask;
@@ -31624,20 +31161,20 @@
31161 sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,p->db,newOpt);
31162 }else if( nArg<3 ){
31163 curOpt = ~newOpt;
31164 }
31165 if( newOpt==0 ){
31166 sqlite3_fputs("+All\n", p->out);
31167 }else if( newOpt==0xffffffff ){
31168 sqlite3_fputs("-All\n", p->out);
31169 }else{
31170 int jj;
31171 for(jj=0; jj<ArraySize(aLabel); jj++){
31172 unsigned int m = aLabel[jj].mask;
31173 if( !aLabel[jj].bDsply ) continue;
31174 if( (curOpt&m)!=(newOpt&m) ){
31175 sqlite3_fprintf(p->out, "%c%s\n", (newOpt & m)==0 ? '+' : '-',
31176 aLabel[jj].zLabel);
31177 }
31178 }
31179 }
31180 rc2 = isOk = 3;
@@ -31677,11 +31214,11 @@
31214 if( nArg==3 || nArg==4 ){
31215 int ii = (int)integerValue(azArg[2]);
31216 sqlite3 *db;
31217 if( ii==0 && cli_strcmp(azArg[2],"random")==0 ){
31218 sqlite3_randomness(sizeof(ii),&ii);
31219 sqlite3_fprintf(stdout, "-- random seed: %d\n", ii);
31220 }
31221 if( nArg==3 ){
31222 db = 0;
31223 }else{
31224 db = p->db;
@@ -31745,11 +31282,11 @@
31282 break;
31283
31284 case SQLITE_TESTCTRL_SEEK_COUNT: {
31285 u64 x = 0;
31286 rc2 = sqlite3_test_control(testctrl, p->db, &x);
31287 sqlite3_fprintf(p->out, "%llu\n", x);
31288 isOk = 3;
31289 break;
31290 }
31291
31292 #ifdef YYCOVERAGE
@@ -31776,15 +31313,15 @@
31313 int id = 1;
31314 while(1){
31315 int val = 0;
31316 rc2 = sqlite3_test_control(testctrl, -id, &val);
31317 if( rc2!=SQLITE_OK ) break;
31318 if( id>1 ) sqlite3_fputs(" ", p->out);
31319 sqlite3_fprintf(p->out, "%d: %d", id, val);
31320 id++;
31321 }
31322 if( id>1 ) sqlite3_fputs("\n", p->out);
31323 isOk = 3;
31324 }
31325 break;
31326 }
31327 #endif
@@ -31822,18 +31359,26 @@
31359 }else if( cli_strcmp(z,"reset")==0 ){
31360 faultsim_state.iCnt = faultsim_state.nSkip;
31361 faultsim_state.nHit = 0;
31362 sqlite3_test_control(testctrl, faultsim_callback);
31363 }else if( cli_strcmp(z,"status")==0 ){
31364 sqlite3_fprintf(p->out, "faultsim.iId: %d\n",
31365 faultsim_state.iId);
31366 sqlite3_fprintf(p->out, "faultsim.iErr: %d\n",
31367 faultsim_state.iErr);
31368 sqlite3_fprintf(p->out, "faultsim.iCnt: %d\n",
31369 faultsim_state.iCnt);
31370 sqlite3_fprintf(p->out, "faultsim.nHit: %d\n",
31371 faultsim_state.nHit);
31372 sqlite3_fprintf(p->out, "faultsim.iInterval: %d\n",
31373 faultsim_state.iInterval);
31374 sqlite3_fprintf(p->out, "faultsim.eVerbose: %d\n",
31375 faultsim_state.eVerbose);
31376 sqlite3_fprintf(p->out, "faultsim.nRepeat: %d\n",
31377 faultsim_state.nRepeat);
31378 sqlite3_fprintf(p->out, "faultsim.nSkip: %d\n",
31379 faultsim_state.nSkip);
31380 }else if( cli_strcmp(z,"-v")==0 ){
31381 if( faultsim_state.eVerbose<2 ) faultsim_state.eVerbose++;
31382 }else if( cli_strcmp(z,"-q")==0 ){
31383 if( faultsim_state.eVerbose>0 ) faultsim_state.eVerbose--;
31384 }else if( cli_strcmp(z,"-id")==0 && kk+1<nArg ){
@@ -31847,19 +31392,20 @@
31392 }else if( cli_strcmp(z,"-skip")==0 && kk+1<nArg ){
31393 faultsim_state.nSkip = atoi(azArg[++kk]);
31394 }else if( cli_strcmp(z,"-?")==0 || sqlite3_strglob("*help*",z)==0){
31395 bShowHelp = 1;
31396 }else{
31397 sqlite3_fprintf(stderr,
31398 "Unrecognized fault_install argument: \"%s\"\n",
31399 azArg[kk]);
31400 rc = 1;
31401 bShowHelp = 1;
31402 break;
31403 }
31404 }
31405 if( bShowHelp ){
31406 sqlite3_fputs(
31407 "Usage: .testctrl fault_install ARGS\n"
31408 "Possible arguments:\n"
31409 " off Disable faultsim\n"
31410 " on Activate faultsim\n"
31411 " reset Reset the trigger counter\n"
@@ -31869,23 +31415,25 @@
31415 " --errcode N When triggered, return N as error code\n"
31416 " --id ID Trigger only for the ID specified\n"
31417 " --interval N Trigger only after every N-th call\n"
31418 " --repeat N Turn off after N hits. 0 means never\n"
31419 " --skip N Skip the first N encounters\n"
31420 ,p->out
31421 );
31422 }
31423 break;
31424 }
31425 }
31426 }
31427 if( isOk==0 && iCtrl>=0 ){
31428 sqlite3_fprintf(p->out,
31429 "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
31430 rc = 1;
31431 }else if( isOk==1 ){
31432 sqlite3_fprintf(p->out, "%d\n", rc2);
31433 }else if( isOk==2 ){
31434 sqlite3_fprintf(p->out, "0x%08x\n", rc2);
31435 }
31436 }else
31437 #endif /* !defined(SQLITE_UNTESTABLE) */
31438
31439 if( c=='t' && n>4 && cli_strncmp(azArg[0], "timeout", n)==0 ){
@@ -31936,11 +31484,11 @@
31484 }
31485 else if( optionMatch(z, "close") ){
31486 mType |= SQLITE_TRACE_CLOSE;
31487 }
31488 else {
31489 sqlite3_fprintf(stderr,"Unknown option \"%s\" on \".trace\"\n", z);
31490 rc = 1;
31491 goto meta_command_exit;
31492 }
31493 }else{
31494 output_file_close(p->traceOut);
@@ -31996,11 +31544,11 @@
31544 goto meta_command_exit;
31545 }
31546 rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
31547 strlen30(azArg[3]));
31548 if( rc ){
31549 sqlite3_fprintf(stderr,"Authentication failed for user %s\n", azArg[2]);
31550 rc = 1;
31551 }
31552 }else if( cli_strcmp(azArg[1],"add")==0 ){
31553 if( nArg!=5 ){
31554 eputz("Usage: .user add USER PASSWORD ISADMIN\n");
@@ -32008,11 +31556,11 @@
31556 goto meta_command_exit;
31557 }
31558 rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
31559 booleanValue(azArg[4]));
31560 if( rc ){
31561 sqlite3_fprintf(stderr,"User-Add failed: %d\n", rc);
31562 rc = 1;
31563 }
31564 }else if( cli_strcmp(azArg[1],"edit")==0 ){
31565 if( nArg!=5 ){
31566 eputz("Usage: .user edit USER PASSWORD ISADMIN\n");
@@ -32020,11 +31568,11 @@
31568 goto meta_command_exit;
31569 }
31570 rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
31571 booleanValue(azArg[4]));
31572 if( rc ){
31573 sqlite3_fprintf(stderr,"User-Edit failed: %d\n", rc);
31574 rc = 1;
31575 }
31576 }else if( cli_strcmp(azArg[1],"delete")==0 ){
31577 if( nArg!=3 ){
31578 eputz("Usage: .user delete USER\n");
@@ -32031,11 +31579,11 @@
31579 rc = 1;
31580 goto meta_command_exit;
31581 }
31582 rc = sqlite3_user_delete(p->db, azArg[2]);
31583 if( rc ){
31584 sqlite3_fprintf(stderr,"User-Delete failed: %d\n", rc);
31585 rc = 1;
31586 }
31587 }else{
31588 eputz("Usage: .user login|add|edit|delete ...\n");
31589 rc = 1;
@@ -32044,38 +31592,38 @@
31592 }else
31593 #endif /* SQLITE_USER_AUTHENTICATION */
31594
31595 if( c=='v' && cli_strncmp(azArg[0], "version", n)==0 ){
31596 char *zPtrSz = sizeof(void*)==8 ? "64-bit" : "32-bit";
31597 sqlite3_fprintf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
31598 sqlite3_libversion(), sqlite3_sourceid());
31599 #if SQLITE_HAVE_ZLIB
31600 sqlite3_fprintf(p->out, "zlib version %s\n", zlibVersion());
31601 #endif
31602 #define CTIMEOPT_VAL_(opt) #opt
31603 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
31604 #if defined(__clang__) && defined(__clang_major__)
31605 sqlite3_fprintf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
31606 CTIMEOPT_VAL(__clang_minor__) "."
31607 CTIMEOPT_VAL(__clang_patchlevel__) " (%s)\n", zPtrSz);
31608 #elif defined(_MSC_VER)
31609 sqlite3_fprintf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) " (%s)\n", zPtrSz);
31610 #elif defined(__GNUC__) && defined(__VERSION__)
31611 sqlite3_fprintf(p->out, "gcc-" __VERSION__ " (%s)\n", zPtrSz);
31612 #endif
31613 }else
31614
31615 if( c=='v' && cli_strncmp(azArg[0], "vfsinfo", n)==0 ){
31616 const char *zDbName = nArg==2 ? azArg[1] : "main";
31617 sqlite3_vfs *pVfs = 0;
31618 if( p->db ){
31619 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
31620 if( pVfs ){
31621 sqlite3_fprintf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName);
31622 sqlite3_fprintf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
31623 sqlite3_fprintf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
31624 sqlite3_fprintf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
31625 }
31626 }
31627 }else
31628
31629 if( c=='v' && cli_strncmp(azArg[0], "vfslist", n)==0 ){
@@ -32083,17 +31631,17 @@
31631 sqlite3_vfs *pCurrent = 0;
31632 if( p->db ){
31633 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
31634 }
31635 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
31636 sqlite3_fprintf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName,
31637 pVfs==pCurrent ? " <--- CURRENT" : "");
31638 sqlite3_fprintf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
31639 sqlite3_fprintf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
31640 sqlite3_fprintf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
31641 if( pVfs->pNext ){
31642 sqlite3_fputs("-----------------------------------\n", p->out);
31643 }
31644 }
31645 }else
31646
31647 if( c=='v' && cli_strncmp(azArg[0], "vfsname", n)==0 ){
@@ -32100,11 +31648,11 @@
31648 const char *zDbName = nArg==2 ? azArg[1] : "main";
31649 char *zVfsName = 0;
31650 if( p->db ){
31651 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
31652 if( zVfsName ){
31653 sqlite3_fprintf(p->out, "%s\n", zVfsName);
31654 sqlite3_free(zVfsName);
31655 }
31656 }
31657 }else
31658
@@ -32124,11 +31672,11 @@
31672 p->colWidth[j-1] = (int)integerValue(azArg[j]);
31673 }
31674 }else
31675
31676 {
31677 sqlite3_fprintf(stderr,"Error: unknown command or invalid arguments: "
31678 " \"%s\". Enter \".help\" for help\n", azArg[0]);
31679 rc = 1;
31680 }
31681
31682 meta_command_exit:
@@ -32376,11 +31924,11 @@
31924 open_db(p, 0);
31925 if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
31926 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
31927 BEGIN_TIMER;
31928 rc = shell_exec(p, zSql, &zErrMsg);
31929 END_TIMER(p->out);
31930 if( rc || zErrMsg ){
31931 char zPrefix[100];
31932 const char *zErrorTail;
31933 const char *zErrorType;
31934 if( zErrMsg==0 ){
@@ -32400,28 +31948,28 @@
31948 sqlite3_snprintf(sizeof(zPrefix), zPrefix,
31949 "%s near line %d:", zErrorType, startline);
31950 }else{
31951 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:", zErrorType);
31952 }
31953 sqlite3_fprintf(stderr,"%s %s\n", zPrefix, zErrorTail);
31954 sqlite3_free(zErrMsg);
31955 zErrMsg = 0;
31956 return 1;
31957 }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
31958 char zLineBuf[2000];
31959 sqlite3_snprintf(sizeof(zLineBuf), zLineBuf,
31960 "changes: %lld total_changes: %lld",
31961 sqlite3_changes64(p->db), sqlite3_total_changes64(p->db));
31962 sqlite3_fprintf(p->out, "%s\n", zLineBuf);
31963 }
31964
31965 if( doAutoDetectRestore(p, zSql) ) return 1;
31966 return 0;
31967 }
31968
31969 static void echo_group_input(ShellState *p, const char *zDo){
31970 if( ShellHasFlag(p, SHFLG_Echo) ) sqlite3_fprintf(p->out, "%s\n", zDo);
31971 }
31972
31973 #ifdef SQLITE_SHELL_FIDDLE
31974 /*
31975 ** Alternate one_input_line() impl for wasm mode. This is not in the primary
@@ -32475,11 +32023,11 @@
32023 i64 startline = 0; /* Line number for start of current input */
32024 QuickScanState qss = QSS_Start; /* Accumulated line status (so far) */
32025
32026 if( p->inputNesting==MAX_INPUT_NESTING ){
32027 /* This will be more informative in a later version. */
32028 sqlite3_fprintf(stderr,"Input nesting limit (%d) reached at line %d."
32029 " Check recursion.\n", MAX_INPUT_NESTING, p->lineno);
32030 return 1;
32031 }
32032 ++p->inputNesting;
32033 p->lineno = 0;
@@ -32487,11 +32035,11 @@
32035 while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
32036 fflush(p->out);
32037 zLine = one_input_line(p->in, zLine, nSql>0);
32038 if( zLine==0 ){
32039 /* End of input */
32040 if( p->in==0 && stdin_is_interactive ) sqlite3_fputs("\n", p->out);
32041 break;
32042 }
32043 if( seenInterrupt ){
32044 if( p->in!=0 ) break;
32045 seenInterrupt = 0;
@@ -32707,19 +32255,19 @@
32255 }
32256 zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
32257 shell_check_oom(zBuf);
32258 sqliterc = zBuf;
32259 }
32260 p->in = sqlite3_fopen(sqliterc,"rb");
32261 if( p->in ){
32262 if( stdin_is_interactive ){
32263 sqlite3_fprintf(stderr,"-- Loading resources from %s\n", sqliterc);
32264 }
32265 if( process_input(p) && bail_on_error ) exit(1);
32266 fclose(p->in);
32267 }else if( sqliterc_override!=0 ){
32268 sqlite3_fprintf(stderr,"cannot open: \"%s\"\n", sqliterc);
32269 if( bail_on_error ) exit(1);
32270 }
32271 p->in = inSaved;
32272 p->lineno = savedLineno;
32273 sqlite3_free(zBuf);
@@ -32790,15 +32338,15 @@
32338 #ifdef SQLITE_HAVE_ZLIB
32339 " -zip open the file as a ZIP Archive\n"
32340 #endif
32341 ;
32342 static void usage(int showDetail){
32343 sqlite3_fprintf(stderr,"Usage: %s [OPTIONS] [FILENAME [SQL]]\n"
32344 "FILENAME is the name of an SQLite database. A new database is created\n"
32345 "if the file does not previously exist. Defaults to :memory:.\n", Argv0);
32346 if( showDetail ){
32347 sqlite3_fprintf(stderr,"OPTIONS include:\n%s", zOptions);
32348 }else{
32349 eputz("Use the -help option for additional information\n");
32350 }
32351 exit(0);
32352 }
@@ -32854,21 +32402,22 @@
32402 SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
32403 #endif
32404 }
32405 #else
32406 static void printBold(const char *zText){
32407 sqlite3_fprintf(stdout, "\033[1m%s\033[0m", zText);
32408 }
32409 #endif
32410
32411 /*
32412 ** Get the argument to an --option. Throw an error and die if no argument
32413 ** is available.
32414 */
32415 static char *cmdline_option_value(int argc, char **argv, int i){
32416 if( i==argc ){
32417 sqlite3_fprintf(stderr,
32418 "%s: Error: missing argument to %s\n", argv[0], argv[argc-1]);
32419 exit(1);
32420 }
32421 return argv[i];
32422 }
32423
@@ -32901,11 +32450,10 @@
32450 char *zErrMsg = 0;
32451 #ifdef SQLITE_SHELL_FIDDLE
32452 # define data shellState
32453 #else
32454 ShellState data;
 
32455 #endif
32456 const char *zInitFile = 0;
32457 int i;
32458 int rc = 0;
32459 int warnInmemoryDb = 0;
@@ -32924,25 +32472,25 @@
32472 #ifdef SQLITE_SHELL_FIDDLE
32473 stdin_is_interactive = 0;
32474 stdout_is_console = 1;
32475 data.wasm.zDefaultDbName = "/fiddle.sqlite3";
32476 #else
32477 stdin_is_interactive = isatty(0);
32478 stdout_is_console = isatty(1);
 
 
32479 #endif
32480 atexit(sayAbnormalExit);
32481 #ifdef SQLITE_DEBUG
32482 mem_main_enter = sqlite3_memory_used();
32483 #endif
32484 #if !defined(_WIN32_WCE)
32485 if( getenv("SQLITE_DEBUG_BREAK") ){
32486 if( isatty(0) && isatty(2) ){
32487 char zLine[100];
32488 sqlite3_fprintf(stderr,
32489 "attach debugger to process %d and press ENTER to continue...",
32490 GETPID());
32491 sqlite3_fgets(zLine, sizeof(zLine), stdin);
32492 }else{
32493 #if defined(_WIN32) || defined(WIN32)
32494 #if SQLITE_OS_WINRT
32495 __debugbreak();
32496 #else
@@ -32963,11 +32511,12 @@
32511 }
32512 #endif
32513
32514 #if USE_SYSTEM_SQLITE+0!=1
32515 if( cli_strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
32516 sqlite3_fprintf(stderr,
32517 "SQLite header and source version mismatch\n%s\n%s\n",
32518 sqlite3_sourceid(), SQLITE_SOURCE_ID);
32519 exit(1);
32520 }
32521 #endif
32522 main_init(&data);
@@ -33106,11 +32655,12 @@
32655 case 0: sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); break;
32656 case 2: sqlite3_config(SQLITE_CONFIG_MULTITHREAD); break;
32657 default: sqlite3_config(SQLITE_CONFIG_SERIALIZED); break;
32658 }
32659 }else if( cli_strcmp(z,"-vfstrace")==0 ){
32660 vfstrace_register("trace",0,(int(*)(const char*,void*))sqlite3_fputs,
32661 stderr,1);
32662 bEnableVfstrace = 1;
32663 #ifdef SQLITE_ENABLE_MULTIPLEX
32664 }else if( cli_strcmp(z,"-multiplex")==0 ){
32665 extern int sqlite3_multiplex_initialize(const char*,int);
32666 sqlite3_multiplex_initialize(0, 1);
@@ -33187,21 +32737,22 @@
32737 if( zVfs ){
32738 sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
32739 if( pVfs ){
32740 sqlite3_vfs_register(pVfs, 1);
32741 }else{
32742 sqlite3_fprintf(stderr,"no such VFS: \"%s\"\n", zVfs);
32743 exit(1);
32744 }
32745 }
32746
32747 if( data.pAuxDb->zDbFilename==0 ){
32748 #ifndef SQLITE_OMIT_MEMORYDB
32749 data.pAuxDb->zDbFilename = ":memory:";
32750 warnInmemoryDb = argc==1;
32751 #else
32752 sqlite3_fprintf(stderr,
32753 "%s: Error: no database filename specified\n", Argv0);
32754 return 1;
32755 #endif
32756 }
32757 data.out = stdout;
32758 #ifndef SQLITE_SHELL_FIDDLE
@@ -33314,11 +32865,11 @@
32865 */
32866 ShellSetFlag(&data, SHFLG_Backslash);
32867 }else if( cli_strcmp(z,"-bail")==0 ){
32868 /* No-op. The bail_on_error flag should already be set. */
32869 }else if( cli_strcmp(z,"-version")==0 ){
32870 sqlite3_fprintf(stdout, "%s %s (%d-bit)\n",
32871 sqlite3_libversion(), sqlite3_sourceid(), 8*(int)sizeof(char*));
32872 return 0;
32873 }else if( cli_strcmp(z,"-interactive")==0 ){
32874 /* Need to check for interactive override here to so that it can
32875 ** affect console setup (for Windows only) and testing thereof.
@@ -33377,18 +32928,18 @@
32928 rc = shell_exec(&data, z, &zErrMsg);
32929 if( zErrMsg!=0 ){
32930 shellEmitError(zErrMsg);
32931 if( bail_on_error ) return rc!=0 ? rc : 1;
32932 }else if( rc!=0 ){
32933 sqlite3_fprintf(stderr,"Error: unable to process SQL \"%s\"\n", z);
32934 if( bail_on_error ) return rc;
32935 }
32936 }
32937 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
32938 }else if( cli_strncmp(z, "-A", 2)==0 ){
32939 if( nCmd>0 ){
32940 sqlite3_fprintf(stderr,"Error: cannot mix regular SQL or dot-commands"
32941 " with \"%s\"\n", z);
32942 return 1;
32943 }
32944 open_db(&data, OPEN_DB_ZIPFILE);
32945 if( z[2] ){
@@ -33403,11 +32954,11 @@
32954 }else if( cli_strcmp(z,"-safe")==0 ){
32955 data.bSafeMode = data.bSafeModePersist = 1;
32956 }else if( cli_strcmp(z,"-unsafe-testing")==0 ){
32957 /* Acted upon in first pass. */
32958 }else{
32959 sqlite3_fprintf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
32960 eputz("Use -help for a list of options.\n");
32961 return 1;
32962 }
32963 data.cMode = data.mode;
32964 }
@@ -33430,11 +32981,12 @@
32981 rc = shell_exec(&data, azCmd[i], &zErrMsg);
32982 if( zErrMsg || rc ){
32983 if( zErrMsg!=0 ){
32984 shellEmitError(zErrMsg);
32985 }else{
32986 sqlite3_fprintf(stderr,
32987 "Error: unable to process SQL: %s\n", azCmd[i]);
32988 }
32989 sqlite3_free(zErrMsg);
32990 if( rc==0 ) rc = 1;
32991 goto shell_main_exit;
32992 }
@@ -33450,11 +33002,12 @@
33002 #if CIO_WIN_WC_XLATE
33003 # define SHELL_CIO_CHAR_SET (stdout_is_console? " (UTF-16 console I/O)" : "")
33004 #else
33005 # define SHELL_CIO_CHAR_SET ""
33006 #endif
33007 sqlite3_fprintf(stdout,
33008 "SQLite version %s %.19s%s\n" /*extra-version-info*/
33009 "Enter \".help\" for usage hints.\n",
33010 sqlite3_libversion(), sqlite3_sourceid(), SHELL_CIO_CHAR_SET);
33011 if( warnInmemoryDb ){
33012 sputz(stdout, "Connected to a ");
33013 printBold("transient in-memory database");
@@ -33526,11 +33079,11 @@
33079 if( bEnableVfstrace ){
33080 vfstrace_unregister("trace");
33081 }
33082 #ifdef SQLITE_DEBUG
33083 if( sqlite3_memory_used()>mem_main_enter ){
33084 sqlite3_fprintf(stderr,"Memory leaked: %u bytes\n",
33085 (unsigned int)(sqlite3_memory_used()-mem_main_enter));
33086 }
33087 #endif
33088 #else /* SQLITE_SHELL_FIDDLE... */
33089 shell_main_exit:
@@ -33566,11 +33119,11 @@
33119 return pVfs;
33120 }
33121
33122 /* Only for emcc experimentation purposes. */
33123 sqlite3 * fiddle_db_arg(sqlite3 *arg){
33124 sqlite3_fprintf(stdout, "fiddle_db_arg(%p)\n", (const void*)arg);
33125 return arg;
33126 }
33127
33128 /*
33129 ** Intended to be called via a SharedWorker() while a separate
@@ -33603,11 +33156,11 @@
33156 while( sqlite3_txn_state(globalDb,0)>0 ){
33157 /*
33158 ** Resolve problem reported in
33159 ** https://sqlite.org/forum/forumpost/0b41a25d65
33160 */
33161 sqlite3_fputs("Rolling back in-progress transaction.\n", stdout);
33162 sqlite3_exec(globalDb,"ROLLBACK", 0, 0, 0);
33163 }
33164 rc = sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
33165 if( 0==rc ) sqlite3_exec(globalDb, "VACUUM", 0, 0, 0);
33166 sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
33167
+32 -20
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -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
-** 62e11a3a78edf9853b74d6495ccd8ae9ac19.
21
+** f97f9944b829a49da12786f934da0a5ad515.
2222
*/
2323
#define SQLITE_CORE 1
2424
#define SQLITE_AMALGAMATION 1
2525
#ifndef SQLITE_PRIVATE
2626
# define SQLITE_PRIVATE static
@@ -462,11 +462,11 @@
462462
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
463463
** [sqlite_version()] and [sqlite_source_id()].
464464
*/
465465
#define SQLITE_VERSION "3.47.0"
466466
#define SQLITE_VERSION_NUMBER 3047000
467
-#define SQLITE_SOURCE_ID "2024-09-21 15:57:06 62e11a3a78edf9853b74d6495ccd8ae9ac1966c7d78eb3682cf2d5885e3740ec"
467
+#define SQLITE_SOURCE_ID "2024-09-26 19:38:34 f97f9944b829a49da12786f934da0a5ad51591afd6d8a19a4a0835f51bbdbff2"
468468
469469
/*
470470
** CAPI3REF: Run-Time Library Version Numbers
471471
** KEYWORDS: sqlite3_version sqlite3_sourceid
472472
**
@@ -4536,17 +4536,21 @@
45364536
** and sqlite3_prepare_v3()
45374537
** interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(),
45384538
** and sqlite3_prepare16_v3() use UTF-16.
45394539
**
45404540
** ^If the nByte argument is negative, then zSql is read up to the
4541
-** first zero terminator. ^If nByte is positive, then it is the
4542
-** number of bytes read from zSql. ^If nByte is zero, then no prepared
4541
+** first zero terminator. ^If nByte is positive, then it is the maximum
4542
+** number of bytes read from zSql. When nByte is positive, zSql is read
4543
+** up to the first zero terminator or until the nByte bytes have been read,
4544
+** whichever comes first. ^If nByte is zero, then no prepared
45434545
** statement is generated.
45444546
** If the caller knows that the supplied string is nul-terminated, then
45454547
** there is a small performance advantage to passing an nByte parameter that
45464548
** is the number of bytes in the input string <i>including</i>
45474549
** the nul-terminator.
4550
+** Note that nByte measure the length of the input in bytes, not
4551
+** characters, even for the UTF-16 inferfaces.
45484552
**
45494553
** ^If pzTail is not NULL then *pzTail is made to point to the first byte
45504554
** past the end of the first SQL statement in zSql. These routines only
45514555
** compile the first statement in zSql, so *pzTail is left pointing to
45524556
** what remains uncompiled.
@@ -42789,19 +42793,19 @@
4278942793
0;
4279042794
}else if( !strcmp(fsInfo.f_basetype, "qnx4") ){
4279142795
pFile->sectorSize = fsInfo.f_bsize;
4279242796
pFile->deviceCharacteristics =
4279342797
/* full bitset of atomics from max sector size and smaller */
42794
- ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
42798
+ (((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2) |
4279542799
SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
4279642800
** so it is ordered */
4279742801
0;
4279842802
}else if( strstr(fsInfo.f_basetype, "dos") ){
4279942803
pFile->sectorSize = fsInfo.f_bsize;
4280042804
pFile->deviceCharacteristics =
4280142805
/* full bitset of atomics from max sector size and smaller */
42802
- ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
42806
+ (((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2) |
4280342807
SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
4280442808
** so it is ordered */
4280542809
0;
4280642810
}else{
4280742811
pFile->deviceCharacteristics =
@@ -107554,11 +107558,11 @@
107554107558
** non-VIEW candidate plus multiple VIEW candidates. In other
107555107559
** words non-VIEW candidate terms take precedence over VIEWs.
107556107560
*/
107557107561
if( cntTab==0
107558107562
|| (cntTab==1
107559
- && ALWAYS(pMatch!=0)
107563
+ && pMatch!=0
107560107564
&& ALWAYS(pMatch->pSTab!=0)
107561107565
&& (pMatch->pSTab->tabFlags & TF_Ephemeral)!=0
107562107566
&& (pTab->tabFlags & TF_Ephemeral)==0)
107563107567
){
107564107568
cntTab = 1;
@@ -226302,10 +226306,14 @@
226302226306
/*
226303226307
** An object of this type is used internally as an abstraction for
226304226308
** input data. Input data may be supplied either as a single large buffer
226305226309
** (e.g. sqlite3changeset_start()) or using a stream function (e.g.
226306226310
** sqlite3changeset_start_strm()).
226311
+**
226312
+** bNoDiscard:
226313
+** If true, then the only time data is discarded is as a result of explicit
226314
+** sessionDiscardData() calls. Not within every sessionInputBuffer() call.
226307226315
*/
226308226316
struct SessionInput {
226309226317
int bNoDiscard; /* If true, do not discard in InputBuffer() */
226310226318
int iCurrent; /* Offset in aData[] of current change */
226311226319
int iNext; /* Offset in aData[] of next change */
@@ -231355,19 +231363,25 @@
231355231363
int rc = SQLITE_OK; /* Return code */
231356231364
const char *zTab = 0; /* Name of current table */
231357231365
int nTab = 0; /* Result of sqlite3Strlen30(zTab) */
231358231366
SessionApplyCtx sApply; /* changeset_apply() context object */
231359231367
int bPatchset;
231368
+ u64 savedFlag = db->flags & SQLITE_FkNoAction;
231360231369
231361231370
assert( xConflict!=0 );
231371
+
231372
+ sqlite3_mutex_enter(sqlite3_db_mutex(db));
231373
+ if( flags & SQLITE_CHANGESETAPPLY_FKNOACTION ){
231374
+ db->flags |= ((u64)SQLITE_FkNoAction);
231375
+ db->aDb[0].pSchema->schema_cookie -= 32;
231376
+ }
231362231377
231363231378
pIter->in.bNoDiscard = 1;
231364231379
memset(&sApply, 0, sizeof(sApply));
231365231380
sApply.bRebase = (ppRebase && pnRebase);
231366231381
sApply.bInvertConstraints = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
231367231382
sApply.bIgnoreNoop = !!(flags & SQLITE_CHANGESETAPPLY_IGNORENOOP);
231368
- sqlite3_mutex_enter(sqlite3_db_mutex(db));
231369231383
if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT)==0 ){
231370231384
rc = sqlite3_exec(db, "SAVEPOINT changeset_apply", 0, 0, 0);
231371231385
}
231372231386
if( rc==SQLITE_OK ){
231373231387
rc = sqlite3_exec(db, "PRAGMA defer_foreign_keys = 1", 0, 0, 0);
@@ -231525,10 +231539,16 @@
231525231539
sqlite3_finalize(sApply.pDelete);
231526231540
sqlite3_finalize(sApply.pSelect);
231527231541
sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */
231528231542
sqlite3_free((char*)sApply.constraints.aBuf);
231529231543
sqlite3_free((char*)sApply.rebase.aBuf);
231544
+
231545
+ if( (flags & SQLITE_CHANGESETAPPLY_FKNOACTION) && savedFlag==0 ){
231546
+ assert( db->flags & SQLITE_FkNoAction );
231547
+ db->flags &= ~((u64)SQLITE_FkNoAction);
231548
+ db->aDb[0].pSchema->schema_cookie -= 32;
231549
+ }
231530231550
sqlite3_mutex_leave(sqlite3_db_mutex(db));
231531231551
return rc;
231532231552
}
231533231553
231534231554
/*
@@ -231553,28 +231573,17 @@
231553231573
int flags
231554231574
){
231555231575
sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
231556231576
int bInv = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
231557231577
int rc = sessionChangesetStart(&pIter, 0, 0, nChangeset, pChangeset, bInv, 1);
231558
- u64 savedFlag = db->flags & SQLITE_FkNoAction;
231559
-
231560
- if( flags & SQLITE_CHANGESETAPPLY_FKNOACTION ){
231561
- db->flags |= ((u64)SQLITE_FkNoAction);
231562
- db->aDb[0].pSchema->schema_cookie -= 32;
231563
- }
231564231578
231565231579
if( rc==SQLITE_OK ){
231566231580
rc = sessionChangesetApply(
231567231581
db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
231568231582
);
231569231583
}
231570231584
231571
- if( (flags & SQLITE_CHANGESETAPPLY_FKNOACTION) && savedFlag==0 ){
231572
- assert( db->flags & SQLITE_FkNoAction );
231573
- db->flags &= ~((u64)SQLITE_FkNoAction);
231574
- db->aDb[0].pSchema->schema_cookie -= 32;
231575
- }
231576231585
return rc;
231577231586
}
231578231587
231579231588
/*
231580231589
** Apply the changeset passed via pChangeset/nChangeset to the main database
@@ -232049,10 +232058,12 @@
232049232058
SessionChange *pExist = 0;
232050232059
SessionChange **pp = 0;
232051232060
SessionTable *pTab = 0;
232052232061
u8 *aRec = &pIter->in.aData[pIter->in.iCurrent + 2];
232053232062
int nRec = (pIter->in.iNext - pIter->in.iCurrent) - 2;
232063
+
232064
+ assert( nRec>0 );
232054232065
232055232066
/* Ensure that only changesets, or only patchsets, but not a mixture
232056232067
** of both, are being combined. It is an error to try to combine a
232057232068
** changeset and a patchset. */
232058232069
if( pGrp->pList==0 ){
@@ -232127,10 +232138,11 @@
232127232138
){
232128232139
u8 *aRec;
232129232140
int nRec;
232130232141
int rc = SQLITE_OK;
232131232142
232143
+ pIter->in.bNoDiscard = 1;
232132232144
while( SQLITE_ROW==(sessionChangesetNext(pIter, &aRec, &nRec, 0)) ){
232133232145
rc = sessionOneChangeToHash(pGrp, pIter, bRebase);
232134232146
if( rc!=SQLITE_OK ) break;
232135232147
}
232136232148
@@ -254725,11 +254737,11 @@
254725254737
int nArg, /* Number of args */
254726254738
sqlite3_value **apUnused /* Function arguments */
254727254739
){
254728254740
assert( nArg==0 );
254729254741
UNUSED_PARAM2(nArg, apUnused);
254730
- sqlite3_result_text(pCtx, "fts5: 2024-09-21 15:57:06 62e11a3a78edf9853b74d6495ccd8ae9ac1966c7d78eb3682cf2d5885e3740ec", -1, SQLITE_TRANSIENT);
254742
+ sqlite3_result_text(pCtx, "fts5: 2024-09-25 16:11:27 660ca5ce6600d897cc2b00b9d39e5d993c1c0e71ec0d5dc706246c053a163281", -1, SQLITE_TRANSIENT);
254731254743
}
254732254744
254733254745
/*
254734254746
** Implementation of fts5_locale(LOCALE, TEXT) function.
254735254747
**
254736254748
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -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 ** 62e11a3a78edf9853b74d6495ccd8ae9ac19.
22 */
23 #define SQLITE_CORE 1
24 #define SQLITE_AMALGAMATION 1
25 #ifndef SQLITE_PRIVATE
26 # define SQLITE_PRIVATE static
@@ -462,11 +462,11 @@
462 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
463 ** [sqlite_version()] and [sqlite_source_id()].
464 */
465 #define SQLITE_VERSION "3.47.0"
466 #define SQLITE_VERSION_NUMBER 3047000
467 #define SQLITE_SOURCE_ID "2024-09-21 15:57:06 62e11a3a78edf9853b74d6495ccd8ae9ac1966c7d78eb3682cf2d5885e3740ec"
468
469 /*
470 ** CAPI3REF: Run-Time Library Version Numbers
471 ** KEYWORDS: sqlite3_version sqlite3_sourceid
472 **
@@ -4536,17 +4536,21 @@
4536 ** and sqlite3_prepare_v3()
4537 ** interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(),
4538 ** and sqlite3_prepare16_v3() use UTF-16.
4539 **
4540 ** ^If the nByte argument is negative, then zSql is read up to the
4541 ** first zero terminator. ^If nByte is positive, then it is the
4542 ** number of bytes read from zSql. ^If nByte is zero, then no prepared
 
 
4543 ** statement is generated.
4544 ** If the caller knows that the supplied string is nul-terminated, then
4545 ** there is a small performance advantage to passing an nByte parameter that
4546 ** is the number of bytes in the input string <i>including</i>
4547 ** the nul-terminator.
 
 
4548 **
4549 ** ^If pzTail is not NULL then *pzTail is made to point to the first byte
4550 ** past the end of the first SQL statement in zSql. These routines only
4551 ** compile the first statement in zSql, so *pzTail is left pointing to
4552 ** what remains uncompiled.
@@ -42789,19 +42793,19 @@
42789 0;
42790 }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){
42791 pFile->sectorSize = fsInfo.f_bsize;
42792 pFile->deviceCharacteristics =
42793 /* full bitset of atomics from max sector size and smaller */
42794 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
42795 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
42796 ** so it is ordered */
42797 0;
42798 }else if( strstr(fsInfo.f_basetype, "dos") ){
42799 pFile->sectorSize = fsInfo.f_bsize;
42800 pFile->deviceCharacteristics =
42801 /* full bitset of atomics from max sector size and smaller */
42802 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
42803 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
42804 ** so it is ordered */
42805 0;
42806 }else{
42807 pFile->deviceCharacteristics =
@@ -107554,11 +107558,11 @@
107554 ** non-VIEW candidate plus multiple VIEW candidates. In other
107555 ** words non-VIEW candidate terms take precedence over VIEWs.
107556 */
107557 if( cntTab==0
107558 || (cntTab==1
107559 && ALWAYS(pMatch!=0)
107560 && ALWAYS(pMatch->pSTab!=0)
107561 && (pMatch->pSTab->tabFlags & TF_Ephemeral)!=0
107562 && (pTab->tabFlags & TF_Ephemeral)==0)
107563 ){
107564 cntTab = 1;
@@ -226302,10 +226306,14 @@
226302 /*
226303 ** An object of this type is used internally as an abstraction for
226304 ** input data. Input data may be supplied either as a single large buffer
226305 ** (e.g. sqlite3changeset_start()) or using a stream function (e.g.
226306 ** sqlite3changeset_start_strm()).
 
 
 
 
226307 */
226308 struct SessionInput {
226309 int bNoDiscard; /* If true, do not discard in InputBuffer() */
226310 int iCurrent; /* Offset in aData[] of current change */
226311 int iNext; /* Offset in aData[] of next change */
@@ -231355,19 +231363,25 @@
231355 int rc = SQLITE_OK; /* Return code */
231356 const char *zTab = 0; /* Name of current table */
231357 int nTab = 0; /* Result of sqlite3Strlen30(zTab) */
231358 SessionApplyCtx sApply; /* changeset_apply() context object */
231359 int bPatchset;
 
231360
231361 assert( xConflict!=0 );
 
 
 
 
 
 
231362
231363 pIter->in.bNoDiscard = 1;
231364 memset(&sApply, 0, sizeof(sApply));
231365 sApply.bRebase = (ppRebase && pnRebase);
231366 sApply.bInvertConstraints = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
231367 sApply.bIgnoreNoop = !!(flags & SQLITE_CHANGESETAPPLY_IGNORENOOP);
231368 sqlite3_mutex_enter(sqlite3_db_mutex(db));
231369 if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT)==0 ){
231370 rc = sqlite3_exec(db, "SAVEPOINT changeset_apply", 0, 0, 0);
231371 }
231372 if( rc==SQLITE_OK ){
231373 rc = sqlite3_exec(db, "PRAGMA defer_foreign_keys = 1", 0, 0, 0);
@@ -231525,10 +231539,16 @@
231525 sqlite3_finalize(sApply.pDelete);
231526 sqlite3_finalize(sApply.pSelect);
231527 sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */
231528 sqlite3_free((char*)sApply.constraints.aBuf);
231529 sqlite3_free((char*)sApply.rebase.aBuf);
 
 
 
 
 
 
231530 sqlite3_mutex_leave(sqlite3_db_mutex(db));
231531 return rc;
231532 }
231533
231534 /*
@@ -231553,28 +231573,17 @@
231553 int flags
231554 ){
231555 sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
231556 int bInv = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
231557 int rc = sessionChangesetStart(&pIter, 0, 0, nChangeset, pChangeset, bInv, 1);
231558 u64 savedFlag = db->flags & SQLITE_FkNoAction;
231559
231560 if( flags & SQLITE_CHANGESETAPPLY_FKNOACTION ){
231561 db->flags |= ((u64)SQLITE_FkNoAction);
231562 db->aDb[0].pSchema->schema_cookie -= 32;
231563 }
231564
231565 if( rc==SQLITE_OK ){
231566 rc = sessionChangesetApply(
231567 db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
231568 );
231569 }
231570
231571 if( (flags & SQLITE_CHANGESETAPPLY_FKNOACTION) && savedFlag==0 ){
231572 assert( db->flags & SQLITE_FkNoAction );
231573 db->flags &= ~((u64)SQLITE_FkNoAction);
231574 db->aDb[0].pSchema->schema_cookie -= 32;
231575 }
231576 return rc;
231577 }
231578
231579 /*
231580 ** Apply the changeset passed via pChangeset/nChangeset to the main database
@@ -232049,10 +232058,12 @@
232049 SessionChange *pExist = 0;
232050 SessionChange **pp = 0;
232051 SessionTable *pTab = 0;
232052 u8 *aRec = &pIter->in.aData[pIter->in.iCurrent + 2];
232053 int nRec = (pIter->in.iNext - pIter->in.iCurrent) - 2;
 
 
232054
232055 /* Ensure that only changesets, or only patchsets, but not a mixture
232056 ** of both, are being combined. It is an error to try to combine a
232057 ** changeset and a patchset. */
232058 if( pGrp->pList==0 ){
@@ -232127,10 +232138,11 @@
232127 ){
232128 u8 *aRec;
232129 int nRec;
232130 int rc = SQLITE_OK;
232131
 
232132 while( SQLITE_ROW==(sessionChangesetNext(pIter, &aRec, &nRec, 0)) ){
232133 rc = sessionOneChangeToHash(pGrp, pIter, bRebase);
232134 if( rc!=SQLITE_OK ) break;
232135 }
232136
@@ -254725,11 +254737,11 @@
254725 int nArg, /* Number of args */
254726 sqlite3_value **apUnused /* Function arguments */
254727 ){
254728 assert( nArg==0 );
254729 UNUSED_PARAM2(nArg, apUnused);
254730 sqlite3_result_text(pCtx, "fts5: 2024-09-21 15:57:06 62e11a3a78edf9853b74d6495ccd8ae9ac1966c7d78eb3682cf2d5885e3740ec", -1, SQLITE_TRANSIENT);
254731 }
254732
254733 /*
254734 ** Implementation of fts5_locale(LOCALE, TEXT) function.
254735 **
254736
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -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 ** f97f9944b829a49da12786f934da0a5ad515.
22 */
23 #define SQLITE_CORE 1
24 #define SQLITE_AMALGAMATION 1
25 #ifndef SQLITE_PRIVATE
26 # define SQLITE_PRIVATE static
@@ -462,11 +462,11 @@
462 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
463 ** [sqlite_version()] and [sqlite_source_id()].
464 */
465 #define SQLITE_VERSION "3.47.0"
466 #define SQLITE_VERSION_NUMBER 3047000
467 #define SQLITE_SOURCE_ID "2024-09-26 19:38:34 f97f9944b829a49da12786f934da0a5ad51591afd6d8a19a4a0835f51bbdbff2"
468
469 /*
470 ** CAPI3REF: Run-Time Library Version Numbers
471 ** KEYWORDS: sqlite3_version sqlite3_sourceid
472 **
@@ -4536,17 +4536,21 @@
4536 ** and sqlite3_prepare_v3()
4537 ** interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(),
4538 ** and sqlite3_prepare16_v3() use UTF-16.
4539 **
4540 ** ^If the nByte argument is negative, then zSql is read up to the
4541 ** first zero terminator. ^If nByte is positive, then it is the maximum
4542 ** number of bytes read from zSql. When nByte is positive, zSql is read
4543 ** up to the first zero terminator or until the nByte bytes have been read,
4544 ** whichever comes first. ^If nByte is zero, then no prepared
4545 ** statement is generated.
4546 ** If the caller knows that the supplied string is nul-terminated, then
4547 ** there is a small performance advantage to passing an nByte parameter that
4548 ** is the number of bytes in the input string <i>including</i>
4549 ** the nul-terminator.
4550 ** Note that nByte measure the length of the input in bytes, not
4551 ** characters, even for the UTF-16 inferfaces.
4552 **
4553 ** ^If pzTail is not NULL then *pzTail is made to point to the first byte
4554 ** past the end of the first SQL statement in zSql. These routines only
4555 ** compile the first statement in zSql, so *pzTail is left pointing to
4556 ** what remains uncompiled.
@@ -42789,19 +42793,19 @@
42793 0;
42794 }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){
42795 pFile->sectorSize = fsInfo.f_bsize;
42796 pFile->deviceCharacteristics =
42797 /* full bitset of atomics from max sector size and smaller */
42798 (((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2) |
42799 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
42800 ** so it is ordered */
42801 0;
42802 }else if( strstr(fsInfo.f_basetype, "dos") ){
42803 pFile->sectorSize = fsInfo.f_bsize;
42804 pFile->deviceCharacteristics =
42805 /* full bitset of atomics from max sector size and smaller */
42806 (((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2) |
42807 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
42808 ** so it is ordered */
42809 0;
42810 }else{
42811 pFile->deviceCharacteristics =
@@ -107554,11 +107558,11 @@
107558 ** non-VIEW candidate plus multiple VIEW candidates. In other
107559 ** words non-VIEW candidate terms take precedence over VIEWs.
107560 */
107561 if( cntTab==0
107562 || (cntTab==1
107563 && pMatch!=0
107564 && ALWAYS(pMatch->pSTab!=0)
107565 && (pMatch->pSTab->tabFlags & TF_Ephemeral)!=0
107566 && (pTab->tabFlags & TF_Ephemeral)==0)
107567 ){
107568 cntTab = 1;
@@ -226302,10 +226306,14 @@
226306 /*
226307 ** An object of this type is used internally as an abstraction for
226308 ** input data. Input data may be supplied either as a single large buffer
226309 ** (e.g. sqlite3changeset_start()) or using a stream function (e.g.
226310 ** sqlite3changeset_start_strm()).
226311 **
226312 ** bNoDiscard:
226313 ** If true, then the only time data is discarded is as a result of explicit
226314 ** sessionDiscardData() calls. Not within every sessionInputBuffer() call.
226315 */
226316 struct SessionInput {
226317 int bNoDiscard; /* If true, do not discard in InputBuffer() */
226318 int iCurrent; /* Offset in aData[] of current change */
226319 int iNext; /* Offset in aData[] of next change */
@@ -231355,19 +231363,25 @@
231363 int rc = SQLITE_OK; /* Return code */
231364 const char *zTab = 0; /* Name of current table */
231365 int nTab = 0; /* Result of sqlite3Strlen30(zTab) */
231366 SessionApplyCtx sApply; /* changeset_apply() context object */
231367 int bPatchset;
231368 u64 savedFlag = db->flags & SQLITE_FkNoAction;
231369
231370 assert( xConflict!=0 );
231371
231372 sqlite3_mutex_enter(sqlite3_db_mutex(db));
231373 if( flags & SQLITE_CHANGESETAPPLY_FKNOACTION ){
231374 db->flags |= ((u64)SQLITE_FkNoAction);
231375 db->aDb[0].pSchema->schema_cookie -= 32;
231376 }
231377
231378 pIter->in.bNoDiscard = 1;
231379 memset(&sApply, 0, sizeof(sApply));
231380 sApply.bRebase = (ppRebase && pnRebase);
231381 sApply.bInvertConstraints = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
231382 sApply.bIgnoreNoop = !!(flags & SQLITE_CHANGESETAPPLY_IGNORENOOP);
 
231383 if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT)==0 ){
231384 rc = sqlite3_exec(db, "SAVEPOINT changeset_apply", 0, 0, 0);
231385 }
231386 if( rc==SQLITE_OK ){
231387 rc = sqlite3_exec(db, "PRAGMA defer_foreign_keys = 1", 0, 0, 0);
@@ -231525,10 +231539,16 @@
231539 sqlite3_finalize(sApply.pDelete);
231540 sqlite3_finalize(sApply.pSelect);
231541 sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */
231542 sqlite3_free((char*)sApply.constraints.aBuf);
231543 sqlite3_free((char*)sApply.rebase.aBuf);
231544
231545 if( (flags & SQLITE_CHANGESETAPPLY_FKNOACTION) && savedFlag==0 ){
231546 assert( db->flags & SQLITE_FkNoAction );
231547 db->flags &= ~((u64)SQLITE_FkNoAction);
231548 db->aDb[0].pSchema->schema_cookie -= 32;
231549 }
231550 sqlite3_mutex_leave(sqlite3_db_mutex(db));
231551 return rc;
231552 }
231553
231554 /*
@@ -231553,28 +231573,17 @@
231573 int flags
231574 ){
231575 sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
231576 int bInv = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
231577 int rc = sessionChangesetStart(&pIter, 0, 0, nChangeset, pChangeset, bInv, 1);
 
 
 
 
 
 
231578
231579 if( rc==SQLITE_OK ){
231580 rc = sessionChangesetApply(
231581 db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
231582 );
231583 }
231584
 
 
 
 
 
231585 return rc;
231586 }
231587
231588 /*
231589 ** Apply the changeset passed via pChangeset/nChangeset to the main database
@@ -232049,10 +232058,12 @@
232058 SessionChange *pExist = 0;
232059 SessionChange **pp = 0;
232060 SessionTable *pTab = 0;
232061 u8 *aRec = &pIter->in.aData[pIter->in.iCurrent + 2];
232062 int nRec = (pIter->in.iNext - pIter->in.iCurrent) - 2;
232063
232064 assert( nRec>0 );
232065
232066 /* Ensure that only changesets, or only patchsets, but not a mixture
232067 ** of both, are being combined. It is an error to try to combine a
232068 ** changeset and a patchset. */
232069 if( pGrp->pList==0 ){
@@ -232127,10 +232138,11 @@
232138 ){
232139 u8 *aRec;
232140 int nRec;
232141 int rc = SQLITE_OK;
232142
232143 pIter->in.bNoDiscard = 1;
232144 while( SQLITE_ROW==(sessionChangesetNext(pIter, &aRec, &nRec, 0)) ){
232145 rc = sessionOneChangeToHash(pGrp, pIter, bRebase);
232146 if( rc!=SQLITE_OK ) break;
232147 }
232148
@@ -254725,11 +254737,11 @@
254737 int nArg, /* Number of args */
254738 sqlite3_value **apUnused /* Function arguments */
254739 ){
254740 assert( nArg==0 );
254741 UNUSED_PARAM2(nArg, apUnused);
254742 sqlite3_result_text(pCtx, "fts5: 2024-09-25 16:11:27 660ca5ce6600d897cc2b00b9d39e5d993c1c0e71ec0d5dc706246c053a163281", -1, SQLITE_TRANSIENT);
254743 }
254744
254745 /*
254746 ** Implementation of fts5_locale(LOCALE, TEXT) function.
254747 **
254748
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,11 +146,11 @@
146146
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147147
** [sqlite_version()] and [sqlite_source_id()].
148148
*/
149149
#define SQLITE_VERSION "3.47.0"
150150
#define SQLITE_VERSION_NUMBER 3047000
151
-#define SQLITE_SOURCE_ID "2024-09-21 15:57:06 62e11a3a78edf9853b74d6495ccd8ae9ac1966c7d78eb3682cf2d5885e3740ec"
151
+#define SQLITE_SOURCE_ID "2024-09-26 19:38:34 f97f9944b829a49da12786f934da0a5ad51591afd6d8a19a4a0835f51bbdbff2"
152152
153153
/*
154154
** CAPI3REF: Run-Time Library Version Numbers
155155
** KEYWORDS: sqlite3_version sqlite3_sourceid
156156
**
@@ -4220,17 +4220,21 @@
42204220
** and sqlite3_prepare_v3()
42214221
** interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(),
42224222
** and sqlite3_prepare16_v3() use UTF-16.
42234223
**
42244224
** ^If the nByte argument is negative, then zSql is read up to the
4225
-** first zero terminator. ^If nByte is positive, then it is the
4226
-** number of bytes read from zSql. ^If nByte is zero, then no prepared
4225
+** first zero terminator. ^If nByte is positive, then it is the maximum
4226
+** number of bytes read from zSql. When nByte is positive, zSql is read
4227
+** up to the first zero terminator or until the nByte bytes have been read,
4228
+** whichever comes first. ^If nByte is zero, then no prepared
42274229
** statement is generated.
42284230
** If the caller knows that the supplied string is nul-terminated, then
42294231
** there is a small performance advantage to passing an nByte parameter that
42304232
** is the number of bytes in the input string <i>including</i>
42314233
** the nul-terminator.
4234
+** Note that nByte measure the length of the input in bytes, not
4235
+** characters, even for the UTF-16 inferfaces.
42324236
**
42334237
** ^If pzTail is not NULL then *pzTail is made to point to the first byte
42344238
** past the end of the first SQL statement in zSql. These routines only
42354239
** compile the first statement in zSql, so *pzTail is left pointing to
42364240
** what remains uncompiled.
42374241
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,11 +146,11 @@
146 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147 ** [sqlite_version()] and [sqlite_source_id()].
148 */
149 #define SQLITE_VERSION "3.47.0"
150 #define SQLITE_VERSION_NUMBER 3047000
151 #define SQLITE_SOURCE_ID "2024-09-21 15:57:06 62e11a3a78edf9853b74d6495ccd8ae9ac1966c7d78eb3682cf2d5885e3740ec"
152
153 /*
154 ** CAPI3REF: Run-Time Library Version Numbers
155 ** KEYWORDS: sqlite3_version sqlite3_sourceid
156 **
@@ -4220,17 +4220,21 @@
4220 ** and sqlite3_prepare_v3()
4221 ** interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(),
4222 ** and sqlite3_prepare16_v3() use UTF-16.
4223 **
4224 ** ^If the nByte argument is negative, then zSql is read up to the
4225 ** first zero terminator. ^If nByte is positive, then it is the
4226 ** number of bytes read from zSql. ^If nByte is zero, then no prepared
 
 
4227 ** statement is generated.
4228 ** If the caller knows that the supplied string is nul-terminated, then
4229 ** there is a small performance advantage to passing an nByte parameter that
4230 ** is the number of bytes in the input string <i>including</i>
4231 ** the nul-terminator.
 
 
4232 **
4233 ** ^If pzTail is not NULL then *pzTail is made to point to the first byte
4234 ** past the end of the first SQL statement in zSql. These routines only
4235 ** compile the first statement in zSql, so *pzTail is left pointing to
4236 ** what remains uncompiled.
4237
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,11 +146,11 @@
146 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147 ** [sqlite_version()] and [sqlite_source_id()].
148 */
149 #define SQLITE_VERSION "3.47.0"
150 #define SQLITE_VERSION_NUMBER 3047000
151 #define SQLITE_SOURCE_ID "2024-09-26 19:38:34 f97f9944b829a49da12786f934da0a5ad51591afd6d8a19a4a0835f51bbdbff2"
152
153 /*
154 ** CAPI3REF: Run-Time Library Version Numbers
155 ** KEYWORDS: sqlite3_version sqlite3_sourceid
156 **
@@ -4220,17 +4220,21 @@
4220 ** and sqlite3_prepare_v3()
4221 ** interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(),
4222 ** and sqlite3_prepare16_v3() use UTF-16.
4223 **
4224 ** ^If the nByte argument is negative, then zSql is read up to the
4225 ** first zero terminator. ^If nByte is positive, then it is the maximum
4226 ** number of bytes read from zSql. When nByte is positive, zSql is read
4227 ** up to the first zero terminator or until the nByte bytes have been read,
4228 ** whichever comes first. ^If nByte is zero, then no prepared
4229 ** statement is generated.
4230 ** If the caller knows that the supplied string is nul-terminated, then
4231 ** there is a small performance advantage to passing an nByte parameter that
4232 ** is the number of bytes in the input string <i>including</i>
4233 ** the nul-terminator.
4234 ** Note that nByte measure the length of the input in bytes, not
4235 ** characters, even for the UTF-16 inferfaces.
4236 **
4237 ** ^If pzTail is not NULL then *pzTail is made to point to the first byte
4238 ** past the end of the first SQL statement in zSql. These routines only
4239 ** compile the first statement in zSql, so *pzTail is left pointing to
4240 ** what remains uncompiled.
4241

Keyboard Shortcuts

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