| | @@ -120,13 +120,10 @@ |
| 120 | 120 | #include <math.h> |
| 121 | 121 | #include "sqlite3.h" |
| 122 | 122 | typedef sqlite3_int64 i64; |
| 123 | 123 | typedef sqlite3_uint64 u64; |
| 124 | 124 | typedef unsigned char u8; |
| 125 | | -#if SQLITE_USER_AUTHENTICATION |
| 126 | | -# include "sqlite3userauth.h" |
| 127 | | -#endif |
| 128 | 125 | #include <ctype.h> |
| 129 | 126 | #include <stdarg.h> |
| 130 | 127 | |
| 131 | 128 | #if !defined(_WIN32) && !defined(WIN32) |
| 132 | 129 | # include <signal.h> |
| | @@ -208,12 +205,10 @@ |
| 208 | 205 | # define unlink _unlink |
| 209 | 206 | # endif |
| 210 | 207 | # ifndef strdup |
| 211 | 208 | # define strdup _strdup |
| 212 | 209 | # endif |
| 213 | | -# undef popen |
| 214 | | -# define popen _popen |
| 215 | 210 | # undef pclose |
| 216 | 211 | # define pclose _pclose |
| 217 | 212 | # endif |
| 218 | 213 | #else |
| 219 | 214 | /* Make sure isatty() has a prototype. */ |
| | @@ -253,10 +248,370 @@ |
| 253 | 248 | /* string conversion routines only needed on Win32 */ |
| 254 | 249 | extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR); |
| 255 | 250 | extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText); |
| 256 | 251 | #endif |
| 257 | 252 | |
| 253 | +/************************* Begin ../ext/misc/sqlite3_stdio.h ******************/ |
| 254 | +/* |
| 255 | +** 2024-09-24 |
| 256 | +** |
| 257 | +** The author disclaims copyright to this source code. In place of |
| 258 | +** a legal notice, here is a blessing: |
| 259 | +** |
| 260 | +** May you do good and not evil. |
| 261 | +** May you find forgiveness for yourself and forgive others. |
| 262 | +** May you share freely, never taking more than you give. |
| 263 | +** |
| 264 | +************************************************************************* |
| 265 | +** |
| 266 | +** This header file contains definitions of interfaces that provide |
| 267 | +** cross-platform I/O for UTF-8 content. |
| 268 | +** |
| 269 | +** On most platforms, the interfaces definitions in this file are |
| 270 | +** just #defines. For example sqlite3_fopen() is a macro that resolves |
| 271 | +** to the standard fopen() in the C-library. |
| 272 | +** |
| 273 | +** But Windows does not have a standard C-library, at least not one that |
| 274 | +** can handle UTF-8. So for windows build, the interfaces resolve to new |
| 275 | +** C-language routines contained in the separate sqlite3_stdio.c source file. |
| 276 | +** |
| 277 | +** So on all non-Windows platforms, simply #include this header file and |
| 278 | +** use the interfaces defined herein. Then to run your application on Windows, |
| 279 | +** also link in the accompanying sqlite3_stdio.c source file when compiling |
| 280 | +** to get compatible interfaces. |
| 281 | +*/ |
| 282 | +#ifndef _SQLITE3_STDIO_H_ |
| 283 | +#define _SQLITE3_STDIO_H_ 1 |
| 284 | +#ifdef _WIN32 |
| 285 | +/**** Definitions For Windows ****/ |
| 286 | +#include <stdio.h> |
| 287 | +#include <windows.h> |
| 288 | + |
| 289 | +FILE *sqlite3_fopen(const char *zFilename, const char *zMode); |
| 290 | +FILE *sqlite3_popen(const char *zCommand, const char *type); |
| 291 | +char *sqlite3_fgets(char *s, int size, FILE *stream); |
| 292 | +int sqlite3_fputs(const char *s, FILE *stream); |
| 293 | +int sqlite3_fprintf(FILE *stream, const char *format, ...); |
| 294 | +void sqlite3_fsetmode(FILE *stream, int mode); |
| 295 | + |
| 296 | + |
| 297 | +#else |
| 298 | +/**** Definitions For All Other Platforms ****/ |
| 299 | +#include <stdio.h> |
| 300 | +#define sqlite3_fopen fopen |
| 301 | +#define sqlite3_popen popen |
| 302 | +#define sqlite3_fgets fgets |
| 303 | +#define sqlite3_fputs fputs |
| 304 | +#define sqlite3_fprintf fprintf |
| 305 | +#define sqlite3_fsetmode(F,X) /*no-op*/ |
| 306 | + |
| 307 | +#endif |
| 308 | +#endif /* _SQLITE3_STDIO_H_ */ |
| 309 | + |
| 310 | +/************************* End ../ext/misc/sqlite3_stdio.h ********************/ |
| 311 | +/************************* Begin ../ext/misc/sqlite3_stdio.c ******************/ |
| 312 | +/* |
| 313 | +** 2024-09-24 |
| 314 | +** |
| 315 | +** The author disclaims copyright to this source code. In place of |
| 316 | +** a legal notice, here is a blessing: |
| 317 | +** |
| 318 | +** May you do good and not evil. |
| 319 | +** May you find forgiveness for yourself and forgive others. |
| 320 | +** May you share freely, never taking more than you give. |
| 321 | +** |
| 322 | +************************************************************************* |
| 323 | +** |
| 324 | +** Implementation of standard I/O interfaces for UTF-8 that are missing |
| 325 | +** on Windows. |
| 326 | +*/ |
| 327 | +#ifdef _WIN32 /* This file is a no-op on all platforms except Windows */ |
| 328 | +#ifndef _SQLITE3_STDIO_H_ |
| 329 | +/* #include "sqlite3_stdio.h" */ |
| 330 | +#endif |
| 331 | +#undef WIN32_LEAN_AND_MEAN |
| 332 | +#define WIN32_LEAN_AND_MEAN |
| 333 | +#include <windows.h> |
| 334 | +#include <stdlib.h> |
| 335 | +#include <string.h> |
| 336 | +#include <stdio.h> |
| 337 | +#include <assert.h> |
| 338 | +/* #include "sqlite3.h" */ |
| 339 | +#include <ctype.h> |
| 340 | +#include <stdarg.h> |
| 341 | +#include <io.h> |
| 342 | +#include <fcntl.h> |
| 343 | + |
| 344 | +/* |
| 345 | +** If the SQLITE_U8TEXT_ONLY option is defined, then use O_U8TEXT |
| 346 | +** when appropriate on all output. (Sometimes use O_BINARY when |
| 347 | +** rendering ASCII text in cases where NL-to-CRLF expansion would |
| 348 | +** not be correct.) |
| 349 | +** |
| 350 | +** If the SQLITE_U8TEXT_STDIO option is defined, then use O_U8TEXT |
| 351 | +** when appropriate when writing to stdout or stderr. Use O_BINARY |
| 352 | +** or O_TEXT (depending on things like the .mode and the .crlf setting |
| 353 | +** in the CLI, or other context clues in other applications) for all |
| 354 | +** other output channels. |
| 355 | +** |
| 356 | +** The default behavior, if neither of the above is defined is to |
| 357 | +** use O_U8TEXT when writing to the Windows console (or anything |
| 358 | +** else for which _isatty() returns true) and to use O_BINARY or O_TEXT |
| 359 | +** for all other output channels. |
| 360 | +*/ |
| 361 | +#if defined(SQLITE_U8TEXT_ONLY) |
| 362 | +# define UseWtextForOutput(fd) 1 |
| 363 | +# define UseWtextForInput(fd) 1 |
| 364 | +# define IsConsole(fd) _isatty(_fileno(fd)) |
| 365 | +#elif defined(SQLITE_U8TEXT_STDIO) |
| 366 | +# define UseWtextForOutput(fd) ((fd)==stdout || (fd)==stderr) |
| 367 | +# define UseWtextForInput(fd) ((fd)==stdin) |
| 368 | +# define IsConsole(fd) _isatty(_fileno(fd)) |
| 369 | +#else |
| 370 | +# define UseWtextForOutput(fd) _isatty(_fileno(fd)) |
| 371 | +# define UseWtextForInput(fd) _isatty(_fileno(fd)) |
| 372 | +# define IsConsole(fd) 1 |
| 373 | +#endif |
| 374 | + |
| 375 | +/* |
| 376 | +** Global variables determine if simulated O_BINARY mode is to be |
| 377 | +** used for stdout or other, respectively. Simulated O_BINARY mode |
| 378 | +** means the mode is usually O_BINARY, but switches to O_U8TEXT for |
| 379 | +** unicode characters U+0080 or greater (any character that has a |
| 380 | +** multi-byte representation in UTF-8). This is the only way we |
| 381 | +** have found to render Unicode characters on a Windows console while |
| 382 | +** at the same time avoiding undesirable \n to \r\n translation. |
| 383 | +*/ |
| 384 | +static int simBinaryStdout = 0; |
| 385 | +static int simBinaryOther = 0; |
| 386 | + |
| 387 | + |
| 388 | +/* |
| 389 | +** Determine if simulated binary mode should be used for output to fd |
| 390 | +*/ |
| 391 | +static int UseBinaryWText(FILE *fd){ |
| 392 | + if( fd==stdout || fd==stderr ){ |
| 393 | + return simBinaryStdout; |
| 394 | + }else{ |
| 395 | + return simBinaryOther; |
| 396 | + } |
| 397 | +} |
| 398 | + |
| 399 | + |
| 400 | +/* |
| 401 | +** Work-alike for the fopen() routine from the standard C library. |
| 402 | +*/ |
| 403 | +FILE *sqlite3_fopen(const char *zFilename, const char *zMode){ |
| 404 | + FILE *fp = 0; |
| 405 | + wchar_t *b1, *b2; |
| 406 | + int sz1, sz2; |
| 407 | + |
| 408 | + sz1 = (int)strlen(zFilename); |
| 409 | + sz2 = (int)strlen(zMode); |
| 410 | + b1 = sqlite3_malloc( (sz1+1)*sizeof(b1[0]) ); |
| 411 | + b2 = sqlite3_malloc( (sz2+1)*sizeof(b1[0]) ); |
| 412 | + if( b1 && b2 ){ |
| 413 | + sz1 = MultiByteToWideChar(CP_UTF8, 0, zFilename, sz1, b1, sz1); |
| 414 | + b1[sz1] = 0; |
| 415 | + sz2 = MultiByteToWideChar(CP_UTF8, 0, zMode, sz2, b2, sz2); |
| 416 | + b2[sz2] = 0; |
| 417 | + fp = _wfopen(b1, b2); |
| 418 | + } |
| 419 | + sqlite3_free(b1); |
| 420 | + sqlite3_free(b2); |
| 421 | + simBinaryOther = 0; |
| 422 | + return fp; |
| 423 | +} |
| 424 | + |
| 425 | + |
| 426 | +/* |
| 427 | +** Work-alike for the popen() routine from the standard C library. |
| 428 | +*/ |
| 429 | +FILE *sqlite3_popen(const char *zCommand, const char *zMode){ |
| 430 | + FILE *fp = 0; |
| 431 | + wchar_t *b1, *b2; |
| 432 | + int sz1, sz2; |
| 433 | + |
| 434 | + sz1 = (int)strlen(zCommand); |
| 435 | + sz2 = (int)strlen(zMode); |
| 436 | + b1 = sqlite3_malloc( (sz1+1)*sizeof(b1[0]) ); |
| 437 | + b2 = sqlite3_malloc( (sz2+1)*sizeof(b1[0]) ); |
| 438 | + if( b1 && b2 ){ |
| 439 | + sz1 = MultiByteToWideChar(CP_UTF8, 0, zCommand, sz1, b1, sz1); |
| 440 | + b1[sz1] = 0; |
| 441 | + sz2 = MultiByteToWideChar(CP_UTF8, 0, zMode, sz2, b2, sz2); |
| 442 | + b2[sz2] = 0; |
| 443 | + fp = _wpopen(b1, b2); |
| 444 | + } |
| 445 | + sqlite3_free(b1); |
| 446 | + sqlite3_free(b2); |
| 447 | + return fp; |
| 448 | +} |
| 449 | + |
| 450 | +/* |
| 451 | +** Work-alike for fgets() from the standard C library. |
| 452 | +*/ |
| 453 | +char *sqlite3_fgets(char *buf, int sz, FILE *in){ |
| 454 | + if( UseWtextForInput(in) ){ |
| 455 | + /* When reading from the command-prompt in Windows, it is necessary |
| 456 | + ** to use _O_WTEXT input mode to read UTF-16 characters, then translate |
| 457 | + ** that into UTF-8. Otherwise, non-ASCII characters all get translated |
| 458 | + ** into '?'. |
| 459 | + */ |
| 460 | + wchar_t *b1 = sqlite3_malloc( sz*sizeof(wchar_t) ); |
| 461 | + if( b1==0 ) return 0; |
| 462 | +#ifndef SQLITE_USE_STDIO_FOR_CONSOLE |
| 463 | + DWORD nRead = 0; |
| 464 | + if( IsConsole(in) |
| 465 | + && ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE), b1, sz, &nRead, 0) |
| 466 | + ){ |
| 467 | + b1[nRead] = 0; |
| 468 | + }else |
| 469 | +#endif |
| 470 | + { |
| 471 | + _setmode(_fileno(in), IsConsole(in) ? _O_WTEXT : _O_U8TEXT); |
| 472 | + if( fgetws(b1, sz/4, in)==0 ){ |
| 473 | + sqlite3_free(b1); |
| 474 | + return 0; |
| 475 | + } |
| 476 | + } |
| 477 | + WideCharToMultiByte(CP_UTF8, 0, b1, -1, buf, sz, 0, 0); |
| 478 | + sqlite3_free(b1); |
| 479 | + return buf; |
| 480 | + }else{ |
| 481 | + /* Reading from a file or other input source, just read bytes without |
| 482 | + ** any translation. */ |
| 483 | + return fgets(buf, sz, in); |
| 484 | + } |
| 485 | +} |
| 486 | + |
| 487 | +/* |
| 488 | +** Send ASCII text as O_BINARY. But for Unicode characters U+0080 and |
| 489 | +** greater, switch to O_U8TEXT. |
| 490 | +*/ |
| 491 | +static void piecemealOutput(wchar_t *b1, int sz, FILE *out){ |
| 492 | + int i; |
| 493 | + wchar_t c; |
| 494 | + while( sz>0 ){ |
| 495 | + for(i=0; i<sz && b1[i]>=0x80; i++){} |
| 496 | + if( i>0 ){ |
| 497 | + c = b1[i]; |
| 498 | + b1[i] = 0; |
| 499 | + fflush(out); |
| 500 | + _setmode(_fileno(out), _O_U8TEXT); |
| 501 | + fputws(b1, out); |
| 502 | + fflush(out); |
| 503 | + b1 += i; |
| 504 | + b1[0] = c; |
| 505 | + sz -= i; |
| 506 | + }else{ |
| 507 | + fflush(out); |
| 508 | + _setmode(_fileno(out), _O_TEXT); |
| 509 | + _setmode(_fileno(out), _O_BINARY); |
| 510 | + fwrite(&b1[0], 1, 1, out); |
| 511 | + for(i=1; i<sz && b1[i]<0x80; i++){ |
| 512 | + fwrite(&b1[i], 1, 1, out); |
| 513 | + } |
| 514 | + fflush(out); |
| 515 | + _setmode(_fileno(out), _O_U8TEXT); |
| 516 | + b1 += i; |
| 517 | + sz -= i; |
| 518 | + } |
| 519 | + } |
| 520 | +} |
| 521 | + |
| 522 | +/* |
| 523 | +** Work-alike for fputs() from the standard C library. |
| 524 | +*/ |
| 525 | +int sqlite3_fputs(const char *z, FILE *out){ |
| 526 | + if( !UseWtextForOutput(out) ){ |
| 527 | + /* Writing to a file or other destination, just write bytes without |
| 528 | + ** any translation. */ |
| 529 | + return fputs(z, out); |
| 530 | + }else{ |
| 531 | + /* One must use UTF16 in order to get unicode support when writing |
| 532 | + ** to the console on Windows. |
| 533 | + */ |
| 534 | + int sz = (int)strlen(z); |
| 535 | + wchar_t *b1 = sqlite3_malloc( (sz+1)*sizeof(wchar_t) ); |
| 536 | + if( b1==0 ) return 0; |
| 537 | + sz = MultiByteToWideChar(CP_UTF8, 0, z, sz, b1, sz); |
| 538 | + b1[sz] = 0; |
| 539 | + |
| 540 | +#ifndef SQLITE_STDIO_FOR_CONSOLE |
| 541 | + DWORD nWr = 0; |
| 542 | + if( IsConsole(out) |
| 543 | + && WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE),b1,sz,&nWr,0) |
| 544 | + ){ |
| 545 | + /* If writing to the console, then the WriteConsoleW() is all we |
| 546 | + ** need to do. */ |
| 547 | + }else |
| 548 | +#endif |
| 549 | + { |
| 550 | + /* For non-console I/O, or if SQLITE_USE_STDIO_FOR_CONSOLE is defined |
| 551 | + ** then write using the standard library. */ |
| 552 | + _setmode(_fileno(out), _O_U8TEXT); |
| 553 | + if( UseBinaryWText(out) ){ |
| 554 | + piecemealOutput(b1, sz, out); |
| 555 | + }else{ |
| 556 | + fputws(b1, out); |
| 557 | + } |
| 558 | + } |
| 559 | + sqlite3_free(b1); |
| 560 | + return 0; |
| 561 | + } |
| 562 | +} |
| 563 | + |
| 564 | + |
| 565 | +/* |
| 566 | +** Work-alike for fprintf() from the standard C library. |
| 567 | +*/ |
| 568 | +int sqlite3_fprintf(FILE *out, const char *zFormat, ...){ |
| 569 | + int rc; |
| 570 | + if( UseWtextForOutput(out) ){ |
| 571 | + /* When writing to the command-prompt in Windows, it is necessary |
| 572 | + ** to use _O_WTEXT input mode and write UTF-16 characters. |
| 573 | + */ |
| 574 | + char *z; |
| 575 | + va_list ap; |
| 576 | + |
| 577 | + va_start(ap, zFormat); |
| 578 | + z = sqlite3_vmprintf(zFormat, ap); |
| 579 | + va_end(ap); |
| 580 | + sqlite3_fputs(z, out); |
| 581 | + rc = (int)strlen(z); |
| 582 | + sqlite3_free(z); |
| 583 | + }else{ |
| 584 | + /* Writing to a file or other destination, just write bytes without |
| 585 | + ** any translation. */ |
| 586 | + va_list ap; |
| 587 | + va_start(ap, zFormat); |
| 588 | + rc = vfprintf(out, zFormat, ap); |
| 589 | + va_end(ap); |
| 590 | + } |
| 591 | + return rc; |
| 592 | +} |
| 593 | + |
| 594 | +/* |
| 595 | +** Set the mode for an output stream. mode argument is typically _O_BINARY or |
| 596 | +** _O_TEXT. |
| 597 | +*/ |
| 598 | +void sqlite3_fsetmode(FILE *fp, int mode){ |
| 599 | + if( !UseWtextForOutput(fp) ){ |
| 600 | + fflush(fp); |
| 601 | + _setmode(_fileno(fp), mode); |
| 602 | + }else if( fp==stdout || fp==stderr ){ |
| 603 | + simBinaryStdout = (mode==_O_BINARY); |
| 604 | + }else{ |
| 605 | + simBinaryOther = (mode==_O_BINARY); |
| 606 | + } |
| 607 | +} |
| 608 | + |
| 609 | +#endif /* defined(_WIN32) */ |
| 610 | + |
| 611 | +/************************* End ../ext/misc/sqlite3_stdio.c ********************/ |
| 612 | + |
| 258 | 613 | /* Use console I/O package as a direct INCLUDE. */ |
| 259 | 614 | #define SQLITE_INTERNAL_LINKAGE static |
| 260 | 615 | |
| 261 | 616 | #ifdef SQLITE_SHELL_FIDDLE |
| 262 | 617 | /* Deselect most features from the console I/O package for Fiddle. */ |
| | @@ -264,1125 +619,13 @@ |
| 264 | 619 | # define SQLITE_CIO_NO_CLASSIFY |
| 265 | 620 | # define SQLITE_CIO_NO_TRANSLATE |
| 266 | 621 | # define SQLITE_CIO_NO_SETMODE |
| 267 | 622 | # define SQLITE_CIO_NO_FLUSH |
| 268 | 623 | #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 |
| 624 | + |
| 625 | +#define eputz(z) sqlite3_fputs(z,stderr) |
| 626 | +#define sputz(fp,z) sqlite3_fputs(z,fp) |
| 1384 | 627 | |
| 1385 | 628 | /* True if the timer is enabled */ |
| 1386 | 629 | static int enableTimer = 0; |
| 1387 | 630 | |
| 1388 | 631 | /* A version of strcmp() that works with NULL values */ |
| | @@ -1423,10 +666,11 @@ |
| 1423 | 666 | struct timeval ru_utime; /* user CPU time used */ |
| 1424 | 667 | struct timeval ru_stime; /* system CPU time used */ |
| 1425 | 668 | }; |
| 1426 | 669 | #define getrusage(A,B) memset(B,0,sizeof(*B)) |
| 1427 | 670 | #endif |
| 671 | + |
| 1428 | 672 | |
| 1429 | 673 | /* Saved resource information for the beginning of an operation */ |
| 1430 | 674 | static struct rusage sBegin; /* CPU time at start */ |
| 1431 | 675 | static sqlite3_int64 iBegin; /* Wall-clock time at start */ |
| 1432 | 676 | |
| | @@ -1447,24 +691,24 @@ |
| 1447 | 691 | } |
| 1448 | 692 | |
| 1449 | 693 | /* |
| 1450 | 694 | ** Print the timing results. |
| 1451 | 695 | */ |
| 1452 | | -static void endTimer(void){ |
| 696 | +static void endTimer(FILE *out){ |
| 1453 | 697 | if( enableTimer ){ |
| 1454 | 698 | sqlite3_int64 iEnd = timeOfDay(); |
| 1455 | 699 | struct rusage sEnd; |
| 1456 | 700 | getrusage(RUSAGE_SELF, &sEnd); |
| 1457 | | - sputf(stdout, "Run Time: real %.3f user %f sys %f\n", |
| 701 | + sqlite3_fprintf(out, "Run Time: real %.3f user %f sys %f\n", |
| 1458 | 702 | (iEnd - iBegin)*0.001, |
| 1459 | 703 | timeDiff(&sBegin.ru_utime, &sEnd.ru_utime), |
| 1460 | 704 | timeDiff(&sBegin.ru_stime, &sEnd.ru_stime)); |
| 1461 | 705 | } |
| 1462 | 706 | } |
| 1463 | 707 | |
| 1464 | 708 | #define BEGIN_TIMER beginTimer() |
| 1465 | | -#define END_TIMER endTimer() |
| 709 | +#define END_TIMER(X) endTimer(X) |
| 1466 | 710 | #define HAS_TIMER 1 |
| 1467 | 711 | |
| 1468 | 712 | #elif (defined(_WIN32) || defined(WIN32)) |
| 1469 | 713 | |
| 1470 | 714 | /* Saved resource information for the beginning of an operation */ |
| | @@ -1526,29 +770,29 @@ |
| 1526 | 770 | } |
| 1527 | 771 | |
| 1528 | 772 | /* |
| 1529 | 773 | ** Print the timing results. |
| 1530 | 774 | */ |
| 1531 | | -static void endTimer(void){ |
| 775 | +static void endTimer(FILE *out){ |
| 1532 | 776 | if( enableTimer && getProcessTimesAddr){ |
| 1533 | 777 | FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd; |
| 1534 | 778 | sqlite3_int64 ftWallEnd = timeOfDay(); |
| 1535 | 779 | getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd); |
| 1536 | | - sputf(stdout, "Run Time: real %.3f user %f sys %f\n", |
| 780 | + sqlite3_fprintf(out, "Run Time: real %.3f user %f sys %f\n", |
| 1537 | 781 | (ftWallEnd - ftWallBegin)*0.001, |
| 1538 | 782 | timeDiff(&ftUserBegin, &ftUserEnd), |
| 1539 | 783 | timeDiff(&ftKernelBegin, &ftKernelEnd)); |
| 1540 | 784 | } |
| 1541 | 785 | } |
| 1542 | 786 | |
| 1543 | 787 | #define BEGIN_TIMER beginTimer() |
| 1544 | | -#define END_TIMER endTimer() |
| 788 | +#define END_TIMER(X) endTimer(X) |
| 1545 | 789 | #define HAS_TIMER hasTimer() |
| 1546 | 790 | |
| 1547 | 791 | #else |
| 1548 | 792 | #define BEGIN_TIMER |
| 1549 | | -#define END_TIMER |
| 793 | +#define END_TIMER(X) /*no-op*/ |
| 1550 | 794 | #define HAS_TIMER 0 |
| 1551 | 795 | #endif |
| 1552 | 796 | |
| 1553 | 797 | /* |
| 1554 | 798 | ** Used to prevent warnings about unused parameters |
| | @@ -1745,41 +989,216 @@ |
| 1745 | 989 | char *z; |
| 1746 | 990 | if( iotrace==0 ) return; |
| 1747 | 991 | va_start(ap, zFormat); |
| 1748 | 992 | z = sqlite3_vmprintf(zFormat, ap); |
| 1749 | 993 | va_end(ap); |
| 1750 | | - sputf(iotrace, "%s", z); |
| 994 | + sqlite3_fprintf(iotrace, "%s", z); |
| 1751 | 995 | sqlite3_free(z); |
| 1752 | 996 | } |
| 1753 | 997 | #endif |
| 1754 | 998 | |
| 999 | +/* Lookup table to estimate the number of columns consumed by a Unicode |
| 1000 | +** character. |
| 1001 | +*/ |
| 1002 | +static const struct { |
| 1003 | + unsigned char w; /* Width of the character in columns */ |
| 1004 | + int iFirst; /* First character in a span having this width */ |
| 1005 | +} aUWidth[] = { |
| 1006 | + /* {1, 0x00000}, */ |
| 1007 | + {0, 0x00300}, {1, 0x00370}, {0, 0x00483}, {1, 0x00487}, {0, 0x00488}, |
| 1008 | + {1, 0x0048a}, {0, 0x00591}, {1, 0x005be}, {0, 0x005bf}, {1, 0x005c0}, |
| 1009 | + {0, 0x005c1}, {1, 0x005c3}, {0, 0x005c4}, {1, 0x005c6}, {0, 0x005c7}, |
| 1010 | + {1, 0x005c8}, {0, 0x00600}, {1, 0x00604}, {0, 0x00610}, {1, 0x00616}, |
| 1011 | + {0, 0x0064b}, {1, 0x0065f}, {0, 0x00670}, {1, 0x00671}, {0, 0x006d6}, |
| 1012 | + {1, 0x006e5}, {0, 0x006e7}, {1, 0x006e9}, {0, 0x006ea}, {1, 0x006ee}, |
| 1013 | + {0, 0x0070f}, {1, 0x00710}, {0, 0x00711}, {1, 0x00712}, {0, 0x00730}, |
| 1014 | + {1, 0x0074b}, {0, 0x007a6}, {1, 0x007b1}, {0, 0x007eb}, {1, 0x007f4}, |
| 1015 | + {0, 0x00901}, {1, 0x00903}, {0, 0x0093c}, {1, 0x0093d}, {0, 0x00941}, |
| 1016 | + {1, 0x00949}, {0, 0x0094d}, {1, 0x0094e}, {0, 0x00951}, {1, 0x00955}, |
| 1017 | + {0, 0x00962}, {1, 0x00964}, {0, 0x00981}, {1, 0x00982}, {0, 0x009bc}, |
| 1018 | + {1, 0x009bd}, {0, 0x009c1}, {1, 0x009c5}, {0, 0x009cd}, {1, 0x009ce}, |
| 1019 | + {0, 0x009e2}, {1, 0x009e4}, {0, 0x00a01}, {1, 0x00a03}, {0, 0x00a3c}, |
| 1020 | + {1, 0x00a3d}, {0, 0x00a41}, {1, 0x00a43}, {0, 0x00a47}, {1, 0x00a49}, |
| 1021 | + {0, 0x00a4b}, {1, 0x00a4e}, {0, 0x00a70}, {1, 0x00a72}, {0, 0x00a81}, |
| 1022 | + {1, 0x00a83}, {0, 0x00abc}, {1, 0x00abd}, {0, 0x00ac1}, {1, 0x00ac6}, |
| 1023 | + {0, 0x00ac7}, {1, 0x00ac9}, {0, 0x00acd}, {1, 0x00ace}, {0, 0x00ae2}, |
| 1024 | + {1, 0x00ae4}, {0, 0x00b01}, {1, 0x00b02}, {0, 0x00b3c}, {1, 0x00b3d}, |
| 1025 | + {0, 0x00b3f}, {1, 0x00b40}, {0, 0x00b41}, {1, 0x00b44}, {0, 0x00b4d}, |
| 1026 | + {1, 0x00b4e}, {0, 0x00b56}, {1, 0x00b57}, {0, 0x00b82}, {1, 0x00b83}, |
| 1027 | + {0, 0x00bc0}, {1, 0x00bc1}, {0, 0x00bcd}, {1, 0x00bce}, {0, 0x00c3e}, |
| 1028 | + {1, 0x00c41}, {0, 0x00c46}, {1, 0x00c49}, {0, 0x00c4a}, {1, 0x00c4e}, |
| 1029 | + {0, 0x00c55}, {1, 0x00c57}, {0, 0x00cbc}, {1, 0x00cbd}, {0, 0x00cbf}, |
| 1030 | + {1, 0x00cc0}, {0, 0x00cc6}, {1, 0x00cc7}, {0, 0x00ccc}, {1, 0x00cce}, |
| 1031 | + {0, 0x00ce2}, {1, 0x00ce4}, {0, 0x00d41}, {1, 0x00d44}, {0, 0x00d4d}, |
| 1032 | + {1, 0x00d4e}, {0, 0x00dca}, {1, 0x00dcb}, {0, 0x00dd2}, {1, 0x00dd5}, |
| 1033 | + {0, 0x00dd6}, {1, 0x00dd7}, {0, 0x00e31}, {1, 0x00e32}, {0, 0x00e34}, |
| 1034 | + {1, 0x00e3b}, {0, 0x00e47}, {1, 0x00e4f}, {0, 0x00eb1}, {1, 0x00eb2}, |
| 1035 | + {0, 0x00eb4}, {1, 0x00eba}, {0, 0x00ebb}, {1, 0x00ebd}, {0, 0x00ec8}, |
| 1036 | + {1, 0x00ece}, {0, 0x00f18}, {1, 0x00f1a}, {0, 0x00f35}, {1, 0x00f36}, |
| 1037 | + {0, 0x00f37}, {1, 0x00f38}, {0, 0x00f39}, {1, 0x00f3a}, {0, 0x00f71}, |
| 1038 | + {1, 0x00f7f}, {0, 0x00f80}, {1, 0x00f85}, {0, 0x00f86}, {1, 0x00f88}, |
| 1039 | + {0, 0x00f90}, {1, 0x00f98}, {0, 0x00f99}, {1, 0x00fbd}, {0, 0x00fc6}, |
| 1040 | + {1, 0x00fc7}, {0, 0x0102d}, {1, 0x01031}, {0, 0x01032}, {1, 0x01033}, |
| 1041 | + {0, 0x01036}, {1, 0x01038}, {0, 0x01039}, {1, 0x0103a}, {0, 0x01058}, |
| 1042 | + {1, 0x0105a}, {2, 0x01100}, {0, 0x01160}, {1, 0x01200}, {0, 0x0135f}, |
| 1043 | + {1, 0x01360}, {0, 0x01712}, {1, 0x01715}, {0, 0x01732}, {1, 0x01735}, |
| 1044 | + {0, 0x01752}, {1, 0x01754}, {0, 0x01772}, {1, 0x01774}, {0, 0x017b4}, |
| 1045 | + {1, 0x017b6}, {0, 0x017b7}, {1, 0x017be}, {0, 0x017c6}, {1, 0x017c7}, |
| 1046 | + {0, 0x017c9}, {1, 0x017d4}, {0, 0x017dd}, {1, 0x017de}, {0, 0x0180b}, |
| 1047 | + {1, 0x0180e}, {0, 0x018a9}, {1, 0x018aa}, {0, 0x01920}, {1, 0x01923}, |
| 1048 | + {0, 0x01927}, {1, 0x01929}, {0, 0x01932}, {1, 0x01933}, {0, 0x01939}, |
| 1049 | + {1, 0x0193c}, {0, 0x01a17}, {1, 0x01a19}, {0, 0x01b00}, {1, 0x01b04}, |
| 1050 | + {0, 0x01b34}, {1, 0x01b35}, {0, 0x01b36}, {1, 0x01b3b}, {0, 0x01b3c}, |
| 1051 | + {1, 0x01b3d}, {0, 0x01b42}, {1, 0x01b43}, {0, 0x01b6b}, {1, 0x01b74}, |
| 1052 | + {0, 0x01dc0}, {1, 0x01dcb}, {0, 0x01dfe}, {1, 0x01e00}, {0, 0x0200b}, |
| 1053 | + {1, 0x02010}, {0, 0x0202a}, {1, 0x0202f}, {0, 0x02060}, {1, 0x02064}, |
| 1054 | + {0, 0x0206a}, {1, 0x02070}, {0, 0x020d0}, {1, 0x020f0}, {2, 0x02329}, |
| 1055 | + {1, 0x0232b}, {2, 0x02e80}, {0, 0x0302a}, {2, 0x03030}, {1, 0x0303f}, |
| 1056 | + {2, 0x03040}, {0, 0x03099}, {2, 0x0309b}, {1, 0x0a4d0}, {0, 0x0a806}, |
| 1057 | + {1, 0x0a807}, {0, 0x0a80b}, {1, 0x0a80c}, {0, 0x0a825}, {1, 0x0a827}, |
| 1058 | + {2, 0x0ac00}, {1, 0x0d7a4}, {2, 0x0f900}, {1, 0x0fb00}, {0, 0x0fb1e}, |
| 1059 | + {1, 0x0fb1f}, {0, 0x0fe00}, {2, 0x0fe10}, {1, 0x0fe1a}, {0, 0x0fe20}, |
| 1060 | + {1, 0x0fe24}, {2, 0x0fe30}, {1, 0x0fe70}, {0, 0x0feff}, {2, 0x0ff00}, |
| 1061 | + {1, 0x0ff61}, {2, 0x0ffe0}, {1, 0x0ffe7}, {0, 0x0fff9}, {1, 0x0fffc}, |
| 1062 | + {0, 0x10a01}, {1, 0x10a04}, {0, 0x10a05}, {1, 0x10a07}, {0, 0x10a0c}, |
| 1063 | + {1, 0x10a10}, {0, 0x10a38}, {1, 0x10a3b}, {0, 0x10a3f}, {1, 0x10a40}, |
| 1064 | + {0, 0x1d167}, {1, 0x1d16a}, {0, 0x1d173}, {1, 0x1d183}, {0, 0x1d185}, |
| 1065 | + {1, 0x1d18c}, {0, 0x1d1aa}, {1, 0x1d1ae}, {0, 0x1d242}, {1, 0x1d245}, |
| 1066 | + {2, 0x20000}, {1, 0x2fffe}, {2, 0x30000}, {1, 0x3fffe}, {0, 0xe0001}, |
| 1067 | + {1, 0xe0002}, {0, 0xe0020}, {1, 0xe0080}, {0, 0xe0100}, {1, 0xe01f0} |
| 1068 | +}; |
| 1069 | + |
| 1755 | 1070 | /* |
| 1756 | | -** Output string zUtf to Out stream as w characters. If w is negative, |
| 1071 | +** Return an estimate of the width, in columns, for the single Unicode |
| 1072 | +** character c. For normal characters, the answer is always 1. But the |
| 1073 | +** estimate might be 0 or 2 for zero-width and double-width characters. |
| 1074 | +** |
| 1075 | +** Different display devices display unicode using different widths. So |
| 1076 | +** it is impossible to know that true display width with 100% accuracy. |
| 1077 | +** Inaccuracies in the width estimates might cause columns to be misaligned. |
| 1078 | +** Unfortunately, there is nothing we can do about that. |
| 1079 | +*/ |
| 1080 | +int cli_wcwidth(int c){ |
| 1081 | + int iFirst, iLast; |
| 1082 | + |
| 1083 | + /* Fast path for common characters */ |
| 1084 | + if( c<=0x300 ) return 1; |
| 1085 | + |
| 1086 | + /* The general case */ |
| 1087 | + iFirst = 0; |
| 1088 | + iLast = sizeof(aUWidth)/sizeof(aUWidth[0]) - 1; |
| 1089 | + while( iFirst<iLast-1 ){ |
| 1090 | + int iMid = (iFirst+iLast)/2; |
| 1091 | + int cMid = aUWidth[iMid].iFirst; |
| 1092 | + if( cMid < c ){ |
| 1093 | + iFirst = iMid; |
| 1094 | + }else if( cMid > c ){ |
| 1095 | + iLast = iMid - 1; |
| 1096 | + }else{ |
| 1097 | + return aUWidth[iMid].w; |
| 1098 | + } |
| 1099 | + } |
| 1100 | + if( aUWidth[iLast].iFirst > c ) return aUWidth[iFirst].w; |
| 1101 | + return aUWidth[iLast].w; |
| 1102 | +} |
| 1103 | + |
| 1104 | +/* |
| 1105 | +** Compute the value and length of a multi-byte UTF-8 character that |
| 1106 | +** begins at z[0]. Return the length. Write the Unicode value into *pU. |
| 1107 | +** |
| 1108 | +** This routine only works for *multi-byte* UTF-8 characters. |
| 1109 | +*/ |
| 1110 | +static int decodeUtf8(const unsigned char *z, int *pU){ |
| 1111 | + if( (z[0] & 0xe0)==0xc0 && (z[1] & 0xc0)==0x80 ){ |
| 1112 | + *pU = ((z[0] & 0x1f)<<6) | (z[1] & 0x3f); |
| 1113 | + return 2; |
| 1114 | + } |
| 1115 | + if( (z[0] & 0xf0)==0xe0 && (z[1] & 0xc0)==0x80 && (z[2] & 0xc0)==0x80 ){ |
| 1116 | + *pU = ((z[0] & 0x0f)<<12) | ((z[1] & 0x3f)<<6) | (z[2] & 0x3f); |
| 1117 | + return 3; |
| 1118 | + } |
| 1119 | + if( (z[0] & 0xf8)==0xf0 && (z[1] & 0xc0)==0x80 && (z[2] & 0xc0)==0x80 |
| 1120 | + && (z[3] & 0xc0)==0x80 |
| 1121 | + ){ |
| 1122 | + *pU = ((z[0] & 0x0f)<<18) | ((z[1] & 0x3f)<<12) | ((z[2] & 0x3f))<<6 |
| 1123 | + | (z[4] & 0x3f); |
| 1124 | + return 4; |
| 1125 | + } |
| 1126 | + *pU = 0; |
| 1127 | + return 1; |
| 1128 | +} |
| 1129 | + |
| 1130 | + |
| 1131 | +#if 0 /* NOT USED */ |
| 1132 | +/* |
| 1133 | +** Return the width, in display columns, of a UTF-8 string. |
| 1134 | +** |
| 1135 | +** Each normal character counts as 1. Zero-width characters count |
| 1136 | +** as zero, and double-width characters count as 2. |
| 1137 | +*/ |
| 1138 | +int cli_wcswidth(const char *z){ |
| 1139 | + const unsigned char *a = (const unsigned char*)z; |
| 1140 | + int n = 0; |
| 1141 | + int i = 0; |
| 1142 | + unsigned char c; |
| 1143 | + while( (c = a[i])!=0 ){ |
| 1144 | + if( c>=0xc0 ){ |
| 1145 | + int u; |
| 1146 | + int len = decodeUtf8(&a[i], &u); |
| 1147 | + i += len; |
| 1148 | + n += cli_wcwidth(u); |
| 1149 | + }else if( c>=' ' ){ |
| 1150 | + n++; |
| 1151 | + i++; |
| 1152 | + }else{ |
| 1153 | + i++; |
| 1154 | + } |
| 1155 | + } |
| 1156 | + return n; |
| 1157 | +} |
| 1158 | +#endif |
| 1159 | + |
| 1160 | +/* |
| 1161 | +** Output string zUtf to stdout as w characters. If w is negative, |
| 1757 | 1162 | ** then right-justify the text. W is the width in UTF-8 characters, not |
| 1758 | 1163 | ** in bytes. This is different from the %*.*s specification in printf |
| 1759 | 1164 | ** since with %*.*s the width is measured in bytes, not characters. |
| 1165 | +** |
| 1166 | +** Take into account zero-width and double-width Unicode characters. |
| 1167 | +** In other words, a zero-width character does not count toward the |
| 1168 | +** the w limit. A double-width character counts as two. |
| 1760 | 1169 | */ |
| 1761 | | -static void utf8_width_print(int w, const char *zUtf){ |
| 1762 | | - int i; |
| 1763 | | - int n; |
| 1170 | +static void utf8_width_print(FILE *out, int w, const char *zUtf){ |
| 1171 | + const unsigned char *a = (const unsigned char*)zUtf; |
| 1172 | + unsigned char c; |
| 1173 | + int i = 0; |
| 1174 | + int n = 0; |
| 1764 | 1175 | int aw = w<0 ? -w : w; |
| 1765 | 1176 | if( zUtf==0 ) zUtf = ""; |
| 1766 | | - for(i=n=0; zUtf[i]; i++){ |
| 1767 | | - if( (zUtf[i]&0xc0)!=0x80 ){ |
| 1177 | + while( (c = a[i])!=0 ){ |
| 1178 | + if( (c&0xc0)==0xc0 ){ |
| 1179 | + int u; |
| 1180 | + int len = decodeUtf8(a+i, &u); |
| 1181 | + int x = cli_wcwidth(u); |
| 1182 | + if( x+n>aw ){ |
| 1183 | + break; |
| 1184 | + } |
| 1185 | + i += len; |
| 1186 | + n += x; |
| 1187 | + }else if( n>=aw ){ |
| 1188 | + break; |
| 1189 | + }else{ |
| 1768 | 1190 | n++; |
| 1769 | | - if( n==aw ){ |
| 1770 | | - do{ i++; }while( (zUtf[i]&0xc0)==0x80 ); |
| 1771 | | - break; |
| 1772 | | - } |
| 1191 | + i++; |
| 1773 | 1192 | } |
| 1774 | 1193 | } |
| 1775 | 1194 | if( n>=aw ){ |
| 1776 | | - oputf("%.*s", i, zUtf); |
| 1195 | + sqlite3_fprintf(out, "%.*s", i, zUtf); |
| 1777 | 1196 | }else if( w<0 ){ |
| 1778 | | - oputf("%*s%s", aw-n, "", zUtf); |
| 1197 | + sqlite3_fprintf(out, "%*s%s", aw-n, "", zUtf); |
| 1779 | 1198 | }else{ |
| 1780 | | - oputf("%s%*s", zUtf, aw-n, ""); |
| 1199 | + sqlite3_fprintf(out, "%s%*s", zUtf, aw-n, ""); |
| 1781 | 1200 | } |
| 1782 | 1201 | } |
| 1783 | 1202 | |
| 1784 | 1203 | |
| 1785 | 1204 | /* |
| | @@ -1841,11 +1260,11 @@ |
| 1841 | 1260 | struct __stat64 x = {0}; |
| 1842 | 1261 | # define STAT_CHR_SRC(mode) ((mode & (_S_IFCHR|_S_IFIFO|_S_IFREG))!=0) |
| 1843 | 1262 | /* On Windows, open first, then check the stream nature. This order |
| 1844 | 1263 | ** is necessary because _stat() and sibs, when checking a named pipe, |
| 1845 | 1264 | ** effectively break the pipe as its supplier sees it. */ |
| 1846 | | - FILE *rv = fopen(zFile, "rb"); |
| 1265 | + FILE *rv = sqlite3_fopen(zFile, "rb"); |
| 1847 | 1266 | if( rv==0 ) return 0; |
| 1848 | 1267 | if( _fstat64(_fileno(rv), &x) != 0 |
| 1849 | 1268 | || !STAT_CHR_SRC(x.st_mode)){ |
| 1850 | 1269 | fclose(rv); |
| 1851 | 1270 | rv = 0; |
| | @@ -1855,11 +1274,11 @@ |
| 1855 | 1274 | struct stat x = {0}; |
| 1856 | 1275 | int rc = stat(zFile, &x); |
| 1857 | 1276 | # define STAT_CHR_SRC(mode) (S_ISREG(mode)||S_ISFIFO(mode)||S_ISCHR(mode)) |
| 1858 | 1277 | if( rc!=0 ) return 0; |
| 1859 | 1278 | if( STAT_CHR_SRC(x.st_mode) ){ |
| 1860 | | - return fopen(zFile, "rb"); |
| 1279 | + return sqlite3_fopen(zFile, "rb"); |
| 1861 | 1280 | }else{ |
| 1862 | 1281 | return 0; |
| 1863 | 1282 | } |
| 1864 | 1283 | #endif |
| 1865 | 1284 | #undef STAT_CHR_SRC |
| | @@ -1882,11 +1301,11 @@ |
| 1882 | 1301 | if( n+100>nLine ){ |
| 1883 | 1302 | nLine = nLine*2 + 100; |
| 1884 | 1303 | zLine = realloc(zLine, nLine); |
| 1885 | 1304 | shell_check_oom(zLine); |
| 1886 | 1305 | } |
| 1887 | | - if( fgets(&zLine[n], nLine - n, in)==0 ){ |
| 1306 | + if( sqlite3_fgets(&zLine[n], nLine - n, in)==0 ){ |
| 1888 | 1307 | if( n==0 ){ |
| 1889 | 1308 | free(zLine); |
| 1890 | 1309 | return 0; |
| 1891 | 1310 | } |
| 1892 | 1311 | zLine[n] = 0; |
| | @@ -2948,11 +2367,11 @@ |
| 2948 | 2367 | ** |
| 2949 | 2368 | ****************************************************************************** |
| 2950 | 2369 | ** |
| 2951 | 2370 | ** This SQLite extension implements functions that compute SHA3 hashes |
| 2952 | 2371 | ** in the way described by the (U.S.) NIST FIPS 202 SHA-3 Standard. |
| 2953 | | -** Two SQL functions are implemented: |
| 2372 | +** Three SQL functions are implemented: |
| 2954 | 2373 | ** |
| 2955 | 2374 | ** sha3(X,SIZE) |
| 2956 | 2375 | ** sha3_agg(Y,SIZE) |
| 2957 | 2376 | ** sha3_query(Z,SIZE) |
| 2958 | 2377 | ** |
| | @@ -5671,14 +5090,14 @@ |
| 5671 | 5090 | char **pzErrMsg, |
| 5672 | 5091 | const sqlite3_api_routines *pApi |
| 5673 | 5092 | ){ |
| 5674 | 5093 | int rc = SQLITE_OK; |
| 5675 | 5094 | unsigned int i; |
| 5676 | | -#if defined(SQLITE3_H) || defined(SQLITE_STATIC_PERCENTILE) |
| 5677 | | - (void)pApi; /* Unused parameter */ |
| 5678 | | -#else |
| 5095 | +#ifdef SQLITE3EXT_H |
| 5679 | 5096 | SQLITE_EXTENSION_INIT2(pApi); |
| 5097 | +#else |
| 5098 | + (void)pApi; /* Unused parameter */ |
| 5680 | 5099 | #endif |
| 5681 | 5100 | (void)pzErrMsg; /* Unused parameter */ |
| 5682 | 5101 | for(i=0; i<sizeof(aPercentFunc)/sizeof(aPercentFunc[0]); i++){ |
| 5683 | 5102 | rc = sqlite3_create_window_function(db, |
| 5684 | 5103 | aPercentFunc[i].zName, |
| | @@ -7431,50 +6850,58 @@ |
| 7431 | 6850 | aIdx[4] = i; |
| 7432 | 6851 | idxNum |= 0x40; |
| 7433 | 6852 | } |
| 7434 | 6853 | continue; |
| 7435 | 6854 | } |
| 7436 | | - if( pConstraint->iColumn==SERIES_COLUMN_VALUE ){ |
| 7437 | | - switch( op ){ |
| 7438 | | - case SQLITE_INDEX_CONSTRAINT_EQ: |
| 7439 | | - case SQLITE_INDEX_CONSTRAINT_IS: { |
| 7440 | | - idxNum |= 0x0080; |
| 7441 | | - idxNum &= ~0x3300; |
| 7442 | | - aIdx[5] = i; |
| 7443 | | - aIdx[6] = -1; |
| 7444 | | - bStartSeen = 1; |
| 7445 | | - break; |
| 7446 | | - } |
| 7447 | | - case SQLITE_INDEX_CONSTRAINT_GE: { |
| 7448 | | - if( idxNum & 0x0080 ) break; |
| 7449 | | - idxNum |= 0x0100; |
| 7450 | | - idxNum &= ~0x0200; |
| 7451 | | - aIdx[5] = i; |
| 7452 | | - bStartSeen = 1; |
| 7453 | | - break; |
| 7454 | | - } |
| 7455 | | - case SQLITE_INDEX_CONSTRAINT_GT: { |
| 7456 | | - if( idxNum & 0x0080 ) break; |
| 7457 | | - idxNum |= 0x0200; |
| 7458 | | - idxNum &= ~0x0100; |
| 7459 | | - aIdx[5] = i; |
| 7460 | | - bStartSeen = 1; |
| 7461 | | - break; |
| 7462 | | - } |
| 7463 | | - case SQLITE_INDEX_CONSTRAINT_LE: { |
| 7464 | | - if( idxNum & 0x0080 ) break; |
| 7465 | | - idxNum |= 0x1000; |
| 7466 | | - idxNum &= ~0x2000; |
| 7467 | | - aIdx[6] = i; |
| 7468 | | - break; |
| 7469 | | - } |
| 7470 | | - case SQLITE_INDEX_CONSTRAINT_LT: { |
| 7471 | | - if( idxNum & 0x0080 ) break; |
| 7472 | | - idxNum |= 0x2000; |
| 7473 | | - idxNum &= ~0x1000; |
| 7474 | | - aIdx[6] = i; |
| 7475 | | - break; |
| 6855 | + if( pConstraint->iColumn<SERIES_COLUMN_START ){ |
| 6856 | + if( pConstraint->iColumn==SERIES_COLUMN_VALUE && pConstraint->usable ){ |
| 6857 | + switch( op ){ |
| 6858 | + case SQLITE_INDEX_CONSTRAINT_EQ: |
| 6859 | + case SQLITE_INDEX_CONSTRAINT_IS: { |
| 6860 | + idxNum |= 0x0080; |
| 6861 | + idxNum &= ~0x3300; |
| 6862 | + aIdx[5] = i; |
| 6863 | + aIdx[6] = -1; |
| 6864 | +#ifndef ZERO_ARGUMENT_GENERATE_SERIES |
| 6865 | + bStartSeen = 1; |
| 6866 | +#endif |
| 6867 | + break; |
| 6868 | + } |
| 6869 | + case SQLITE_INDEX_CONSTRAINT_GE: { |
| 6870 | + if( idxNum & 0x0080 ) break; |
| 6871 | + idxNum |= 0x0100; |
| 6872 | + idxNum &= ~0x0200; |
| 6873 | + aIdx[5] = i; |
| 6874 | +#ifndef ZERO_ARGUMENT_GENERATE_SERIES |
| 6875 | + bStartSeen = 1; |
| 6876 | +#endif |
| 6877 | + break; |
| 6878 | + } |
| 6879 | + case SQLITE_INDEX_CONSTRAINT_GT: { |
| 6880 | + if( idxNum & 0x0080 ) break; |
| 6881 | + idxNum |= 0x0200; |
| 6882 | + idxNum &= ~0x0100; |
| 6883 | + aIdx[5] = i; |
| 6884 | +#ifndef ZERO_ARGUMENT_GENERATE_SERIES |
| 6885 | + bStartSeen = 1; |
| 6886 | +#endif |
| 6887 | + break; |
| 6888 | + } |
| 6889 | + case SQLITE_INDEX_CONSTRAINT_LE: { |
| 6890 | + if( idxNum & 0x0080 ) break; |
| 6891 | + idxNum |= 0x1000; |
| 6892 | + idxNum &= ~0x2000; |
| 6893 | + aIdx[6] = i; |
| 6894 | + break; |
| 6895 | + } |
| 6896 | + case SQLITE_INDEX_CONSTRAINT_LT: { |
| 6897 | + if( idxNum & 0x0080 ) break; |
| 6898 | + idxNum |= 0x2000; |
| 6899 | + idxNum &= ~0x1000; |
| 6900 | + aIdx[6] = i; |
| 6901 | + break; |
| 6902 | + } |
| 7476 | 6903 | } |
| 7477 | 6904 | } |
| 7478 | 6905 | continue; |
| 7479 | 6906 | } |
| 7480 | 6907 | iCol = pConstraint->iColumn - SERIES_COLUMN_START; |
| | @@ -8534,11 +7961,11 @@ |
| 8534 | 7961 | ** If the optional MTIME argument is present, then it is interpreted |
| 8535 | 7962 | ** as an integer - the number of seconds since the unix epoch. The |
| 8536 | 7963 | ** modification-time of the target file is set to this value before |
| 8537 | 7964 | ** returning. |
| 8538 | 7965 | ** |
| 8539 | | -** If three or more arguments are passed to this function and an |
| 7966 | +** If five or more arguments are passed to this function and an |
| 8540 | 7967 | ** error is encountered, an exception is raised. |
| 8541 | 7968 | ** |
| 8542 | 7969 | ** READFILE(FILE): |
| 8543 | 7970 | ** |
| 8544 | 7971 | ** Read and return the contents of file FILE (type blob) from disk. |
| | @@ -8604,10 +8031,17 @@ |
| 8604 | 8031 | # define lstat(path,buf) stat(path,buf) |
| 8605 | 8032 | #endif |
| 8606 | 8033 | #include <time.h> |
| 8607 | 8034 | #include <errno.h> |
| 8608 | 8035 | |
| 8036 | +/* When used as part of the CLI, the sqlite3_stdio.h module will have |
| 8037 | +** been included before this one. In that case use the sqlite3_stdio.h |
| 8038 | +** #defines. If not, create our own for fopen(). |
| 8039 | +*/ |
| 8040 | +#ifndef _SQLITE3_STDIO_H_ |
| 8041 | +# define sqlite3_fopen fopen |
| 8042 | +#endif |
| 8609 | 8043 | |
| 8610 | 8044 | /* |
| 8611 | 8045 | ** Structure of the fsdir() table-valued function |
| 8612 | 8046 | */ |
| 8613 | 8047 | /* 0 1 2 3 4 5 */ |
| | @@ -8636,11 +8070,11 @@ |
| 8636 | 8070 | sqlite3_int64 nIn; |
| 8637 | 8071 | void *pBuf; |
| 8638 | 8072 | sqlite3 *db; |
| 8639 | 8073 | int mxBlob; |
| 8640 | 8074 | |
| 8641 | | - in = fopen(zName, "rb"); |
| 8075 | + in = sqlite3_fopen(zName, "rb"); |
| 8642 | 8076 | if( in==0 ){ |
| 8643 | 8077 | /* File does not exist or is unreadable. Leave the result set to NULL. */ |
| 8644 | 8078 | return; |
| 8645 | 8079 | } |
| 8646 | 8080 | fseek(in, 0, SEEK_END); |
| | @@ -8891,11 +8325,11 @@ |
| 8891 | 8325 | } |
| 8892 | 8326 | }else{ |
| 8893 | 8327 | sqlite3_int64 nWrite = 0; |
| 8894 | 8328 | const char *z; |
| 8895 | 8329 | int rc = 0; |
| 8896 | | - FILE *out = fopen(zFile, "wb"); |
| 8330 | + FILE *out = sqlite3_fopen(zFile, "wb"); |
| 8897 | 8331 | if( out==0 ) return 1; |
| 8898 | 8332 | z = (const char*)sqlite3_value_blob(pData); |
| 8899 | 8333 | if( z ){ |
| 8900 | 8334 | sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out); |
| 8901 | 8335 | nWrite = sqlite3_value_bytes(pData); |
| | @@ -10752,10 +10186,18 @@ |
| 10752 | 10186 | #ifndef SQLITE_NO_STDINT |
| 10753 | 10187 | # include <stdint.h> |
| 10754 | 10188 | #endif |
| 10755 | 10189 | |
| 10756 | 10190 | #include <zlib.h> |
| 10191 | + |
| 10192 | +/* When used as part of the CLI, the sqlite3_stdio.h module will have |
| 10193 | +** been included before this one. In that case use the sqlite3_stdio.h |
| 10194 | +** #defines. If not, create our own for fopen(). |
| 10195 | +*/ |
| 10196 | +#ifndef _SQLITE3_STDIO_H_ |
| 10197 | +# define sqlite3_fopen fopen |
| 10198 | +#endif |
| 10757 | 10199 | |
| 10758 | 10200 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 10759 | 10201 | |
| 10760 | 10202 | #ifndef SQLITE_AMALGAMATION |
| 10761 | 10203 | |
| | @@ -12009,11 +11451,11 @@ |
| 12009 | 11451 | }else{ |
| 12010 | 11452 | zFile = (const char*)sqlite3_value_text(argv[0]); |
| 12011 | 11453 | } |
| 12012 | 11454 | |
| 12013 | 11455 | if( 0==pTab->pWriteFd && 0==bInMemory ){ |
| 12014 | | - pCsr->pFile = zFile ? fopen(zFile, "rb") : 0; |
| 11456 | + pCsr->pFile = zFile ? sqlite3_fopen(zFile, "rb") : 0; |
| 12015 | 11457 | if( pCsr->pFile==0 ){ |
| 12016 | 11458 | zipfileCursorErr(pCsr, "cannot open file: %s", zFile); |
| 12017 | 11459 | rc = SQLITE_ERROR; |
| 12018 | 11460 | }else{ |
| 12019 | 11461 | rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd); |
| | @@ -12199,11 +11641,11 @@ |
| 12199 | 11641 | |
| 12200 | 11642 | /* Open a write fd on the file. Also load the entire central directory |
| 12201 | 11643 | ** structure into memory. During the transaction any new file data is |
| 12202 | 11644 | ** appended to the archive file, but the central directory is accumulated |
| 12203 | 11645 | ** in main-memory until the transaction is committed. */ |
| 12204 | | - pTab->pWriteFd = fopen(pTab->zFile, "ab+"); |
| 11646 | + pTab->pWriteFd = sqlite3_fopen(pTab->zFile, "ab+"); |
| 12205 | 11647 | if( pTab->pWriteFd==0 ){ |
| 12206 | 11648 | pTab->base.zErrMsg = sqlite3_mprintf( |
| 12207 | 11649 | "zipfile: failed to open file %s for writing", pTab->zFile |
| 12208 | 11650 | ); |
| 12209 | 11651 | rc = SQLITE_ERROR; |
| | @@ -14652,10 +14094,70 @@ |
| 14652 | 14094 | } |
| 14653 | 14095 | |
| 14654 | 14096 | return rc; |
| 14655 | 14097 | } |
| 14656 | 14098 | |
| 14099 | +/* |
| 14100 | +** This function tests if the schema of the main database of database handle |
| 14101 | +** db contains an object named zTab. Assuming no error occurs, output parameter |
| 14102 | +** (*pbContains) is set to true if zTab exists, or false if it does not. |
| 14103 | +** |
| 14104 | +** Or, if an error occurs, an SQLite error code is returned. The final value |
| 14105 | +** of (*pbContains) is undefined in this case. |
| 14106 | +*/ |
| 14107 | +static int expertDbContainsObject( |
| 14108 | + sqlite3 *db, |
| 14109 | + const char *zTab, |
| 14110 | + int *pbContains /* OUT: True if object exists */ |
| 14111 | +){ |
| 14112 | + const char *zSql = "SELECT 1 FROM sqlite_schema WHERE name = ?"; |
| 14113 | + sqlite3_stmt *pSql = 0; |
| 14114 | + int rc = SQLITE_OK; |
| 14115 | + int ret = 0; |
| 14116 | + |
| 14117 | + rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0); |
| 14118 | + if( rc==SQLITE_OK ){ |
| 14119 | + sqlite3_bind_text(pSql, 1, zTab, -1, SQLITE_STATIC); |
| 14120 | + if( SQLITE_ROW==sqlite3_step(pSql) ){ |
| 14121 | + ret = 1; |
| 14122 | + } |
| 14123 | + rc = sqlite3_finalize(pSql); |
| 14124 | + } |
| 14125 | + |
| 14126 | + *pbContains = ret; |
| 14127 | + return rc; |
| 14128 | +} |
| 14129 | + |
| 14130 | +/* |
| 14131 | +** Execute SQL command zSql using database handle db. If no error occurs, |
| 14132 | +** set (*pzErr) to NULL and return SQLITE_OK. |
| 14133 | +** |
| 14134 | +** If an error does occur, return an SQLite error code and set (*pzErr) to |
| 14135 | +** point to a buffer containing an English language error message. Except, |
| 14136 | +** if the error message begins with "no such module:", then ignore the |
| 14137 | +** error and return as if the SQL statement had succeeded. |
| 14138 | +** |
| 14139 | +** This is used to copy as much of the database schema as possible while |
| 14140 | +** ignoring any errors related to missing virtual table modules. |
| 14141 | +*/ |
| 14142 | +static int expertSchemaSql(sqlite3 *db, const char *zSql, char **pzErr){ |
| 14143 | + int rc = SQLITE_OK; |
| 14144 | + char *zErr = 0; |
| 14145 | + |
| 14146 | + rc = sqlite3_exec(db, zSql, 0, 0, &zErr); |
| 14147 | + if( rc!=SQLITE_OK && zErr ){ |
| 14148 | + int nErr = STRLEN(zErr); |
| 14149 | + if( nErr>=15 && memcmp(zErr, "no such module:", 15)==0 ){ |
| 14150 | + sqlite3_free(zErr); |
| 14151 | + rc = SQLITE_OK; |
| 14152 | + zErr = 0; |
| 14153 | + } |
| 14154 | + } |
| 14155 | + |
| 14156 | + *pzErr = zErr; |
| 14157 | + return rc; |
| 14158 | +} |
| 14657 | 14159 | |
| 14658 | 14160 | static int idxCreateVtabSchema(sqlite3expert *p, char **pzErrmsg){ |
| 14659 | 14161 | int rc = idxRegisterVtab(p); |
| 14660 | 14162 | sqlite3_stmt *pSchema = 0; |
| 14661 | 14163 | |
| | @@ -14663,30 +14165,39 @@ |
| 14663 | 14165 | ** |
| 14664 | 14166 | ** 1) Add an entry to the p->pTable list, and |
| 14665 | 14167 | ** 2) Create the equivalent virtual table in dbv. |
| 14666 | 14168 | */ |
| 14667 | 14169 | rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg, |
| 14668 | | - "SELECT type, name, sql, 1 FROM sqlite_schema " |
| 14669 | | - "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%%' " |
| 14170 | + "SELECT type, name, sql, 1, " |
| 14171 | + " substr(sql,1,14)=='create virtual' COLLATE nocase " |
| 14172 | + "FROM sqlite_schema " |
| 14173 | + "WHERE type IN ('table','view') AND " |
| 14174 | + " substr(name,1,7)!='sqlite_' COLLATE nocase " |
| 14670 | 14175 | " UNION ALL " |
| 14671 | | - "SELECT type, name, sql, 2 FROM sqlite_schema " |
| 14176 | + "SELECT type, name, sql, 2, 0 FROM sqlite_schema " |
| 14672 | 14177 | "WHERE type = 'trigger'" |
| 14673 | 14178 | " AND tbl_name IN(SELECT name FROM sqlite_schema WHERE type = 'view') " |
| 14674 | | - "ORDER BY 4, 1" |
| 14179 | + "ORDER BY 4, 5 DESC, 1" |
| 14675 | 14180 | ); |
| 14676 | 14181 | while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){ |
| 14677 | 14182 | const char *zType = (const char*)sqlite3_column_text(pSchema, 0); |
| 14678 | 14183 | const char *zName = (const char*)sqlite3_column_text(pSchema, 1); |
| 14679 | 14184 | const char *zSql = (const char*)sqlite3_column_text(pSchema, 2); |
| 14185 | + int bVirtual = sqlite3_column_int(pSchema, 4); |
| 14186 | + int bExists = 0; |
| 14680 | 14187 | |
| 14681 | 14188 | if( zType==0 || zName==0 ) continue; |
| 14682 | | - if( zType[0]=='v' || zType[1]=='r' ){ |
| 14683 | | - if( zSql ) rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg); |
| 14189 | + rc = expertDbContainsObject(p->dbv, zName, &bExists); |
| 14190 | + if( rc || bExists ) continue; |
| 14191 | + |
| 14192 | + if( zType[0]=='v' || zType[1]=='r' || bVirtual ){ |
| 14193 | + /* A view. Or a trigger on a view. */ |
| 14194 | + if( zSql ) rc = expertSchemaSql(p->dbv, zSql, pzErrmsg); |
| 14684 | 14195 | }else{ |
| 14685 | 14196 | IdxTable *pTab; |
| 14686 | 14197 | rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg); |
| 14687 | | - if( rc==SQLITE_OK ){ |
| 14198 | + if( rc==SQLITE_OK && ALWAYS(pTab!=0) ){ |
| 14688 | 14199 | int i; |
| 14689 | 14200 | char *zInner = 0; |
| 14690 | 14201 | char *zOuter = 0; |
| 14691 | 14202 | pTab->pNext = p->pTable; |
| 14692 | 14203 | p->pTable = pTab; |
| | @@ -14883,10 +14394,16 @@ |
| 14883 | 14394 | sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC); |
| 14884 | 14395 | while( SQLITE_OK==rc && SQLITE_ROW==sqlite3_step(pIndexXInfo) ){ |
| 14885 | 14396 | const char *zComma = zCols==0 ? "" : ", "; |
| 14886 | 14397 | const char *zName = (const char*)sqlite3_column_text(pIndexXInfo, 0); |
| 14887 | 14398 | const char *zColl = (const char*)sqlite3_column_text(pIndexXInfo, 1); |
| 14399 | + if( zName==0 ){ |
| 14400 | + /* This index contains an expression. Ignore it. */ |
| 14401 | + sqlite3_free(zCols); |
| 14402 | + sqlite3_free(zOrder); |
| 14403 | + return sqlite3_reset(pIndexXInfo); |
| 14404 | + } |
| 14888 | 14405 | zCols = idxAppendText(&rc, zCols, |
| 14889 | 14406 | "%sx.%Q IS sqlite_expert_rem(%d, x.%Q) COLLATE %s", |
| 14890 | 14407 | zComma, zName, nCol, zName, zColl |
| 14891 | 14408 | ); |
| 14892 | 14409 | zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol); |
| | @@ -15211,16 +14728,22 @@ |
| 15211 | 14728 | |
| 15212 | 14729 | /* Copy the entire schema of database [db] into [dbm]. */ |
| 15213 | 14730 | if( rc==SQLITE_OK ){ |
| 15214 | 14731 | sqlite3_stmt *pSql = 0; |
| 15215 | 14732 | rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg, |
| 15216 | | - "SELECT sql FROM sqlite_schema WHERE name NOT LIKE 'sqlite_%%'" |
| 15217 | | - " AND sql NOT LIKE 'CREATE VIRTUAL %%' ORDER BY rowid" |
| 14733 | + "SELECT sql, name, substr(sql,1,14)=='create virtual' COLLATE nocase" |
| 14734 | + " FROM sqlite_schema WHERE substr(name,1,7)!='sqlite_' COLLATE nocase" |
| 14735 | + " ORDER BY 3 DESC, rowid" |
| 15218 | 14736 | ); |
| 15219 | 14737 | while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){ |
| 15220 | 14738 | const char *zSql = (const char*)sqlite3_column_text(pSql, 0); |
| 15221 | | - if( zSql ) rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg); |
| 14739 | + const char *zName = (const char*)sqlite3_column_text(pSql, 1); |
| 14740 | + int bExists = 0; |
| 14741 | + rc = expertDbContainsObject(pNew->dbm, zName, &bExists); |
| 14742 | + if( rc==SQLITE_OK && zSql && bExists==0 ){ |
| 14743 | + rc = expertSchemaSql(pNew->dbm, zSql, pzErrmsg); |
| 14744 | + } |
| 15222 | 14745 | } |
| 15223 | 14746 | idxFinalize(&rc, pSql); |
| 15224 | 14747 | } |
| 15225 | 14748 | |
| 15226 | 14749 | /* Create the vtab schema */ |
| | @@ -16738,11 +16261,31 @@ |
| 16738 | 16261 | ** |
| 16739 | 16262 | ** Similar compiler commands will work on different systems. The key |
| 16740 | 16263 | ** invariants are (1) you must have -DSQLITE_ENABLE_VFSTRACE so that |
| 16741 | 16264 | ** the shell.c source file will know to include the -vfstrace command-line |
| 16742 | 16265 | ** option and (2) you must compile and link the three source files |
| 16743 | | -** shell,c, test_vfstrace.c, and sqlite3.c. |
| 16266 | +** shell,c, test_vfstrace.c, and sqlite3.c. |
| 16267 | +** |
| 16268 | +** RUNTIME CONTROL OF VFSTRACE OUTPUT |
| 16269 | +** |
| 16270 | +** The application can use the "vfstrace" pragma to control which VFS |
| 16271 | +** APIs are traced. To disable all output: |
| 16272 | +** |
| 16273 | +** PRAGMA vfstrace('-all'); |
| 16274 | +** |
| 16275 | +** To enable all output (which is the default setting): |
| 16276 | +** |
| 16277 | +** PRAGMA vfstrace('+all'); |
| 16278 | +** |
| 16279 | +** Individual APIs can be enabled or disabled by name, with or without |
| 16280 | +** the initial "x" character. For example, to set up for tracing lock |
| 16281 | +** primatives only: |
| 16282 | +** |
| 16283 | +** PRAGMA vfstrace('-all, +Lock,Unlock,ShmLock'); |
| 16284 | +** |
| 16285 | +** The argument to the vfstrace pragma ignores capitalization and any |
| 16286 | +** characters other than alphabetics, '+', and '-'. |
| 16744 | 16287 | */ |
| 16745 | 16288 | #include <stdlib.h> |
| 16746 | 16289 | #include <string.h> |
| 16747 | 16290 | /* #include "sqlite3.h" */ |
| 16748 | 16291 | |
| | @@ -16752,10 +16295,12 @@ |
| 16752 | 16295 | */ |
| 16753 | 16296 | typedef struct vfstrace_info vfstrace_info; |
| 16754 | 16297 | struct vfstrace_info { |
| 16755 | 16298 | sqlite3_vfs *pRootVfs; /* The underlying real VFS */ |
| 16756 | 16299 | int (*xOut)(const char*, void*); /* Send output here */ |
| 16300 | + unsigned int mTrace; /* Mask of interfaces to trace */ |
| 16301 | + u8 bOn; /* Tracing on/off */ |
| 16757 | 16302 | void *pOutArg; /* First argument to xOut */ |
| 16758 | 16303 | const char *zVfsName; /* Name of this trace-VFS */ |
| 16759 | 16304 | sqlite3_vfs *pTraceVfs; /* Pointer back to the trace VFS */ |
| 16760 | 16305 | }; |
| 16761 | 16306 | |
| | @@ -16768,10 +16313,42 @@ |
| 16768 | 16313 | vfstrace_info *pInfo; /* The trace-VFS to which this file belongs */ |
| 16769 | 16314 | const char *zFName; /* Base name of the file */ |
| 16770 | 16315 | sqlite3_file *pReal; /* The real underlying file */ |
| 16771 | 16316 | }; |
| 16772 | 16317 | |
| 16318 | +/* |
| 16319 | +** Bit values for vfstrace_info.mTrace. |
| 16320 | +*/ |
| 16321 | +#define VTR_CLOSE 0x00000001 |
| 16322 | +#define VTR_READ 0x00000002 |
| 16323 | +#define VTR_WRITE 0x00000004 |
| 16324 | +#define VTR_TRUNC 0x00000008 |
| 16325 | +#define VTR_SYNC 0x00000010 |
| 16326 | +#define VTR_FSIZE 0x00000020 |
| 16327 | +#define VTR_LOCK 0x00000040 |
| 16328 | +#define VTR_UNLOCK 0x00000080 |
| 16329 | +#define VTR_CRL 0x00000100 |
| 16330 | +#define VTR_FCTRL 0x00000200 |
| 16331 | +#define VTR_SECSZ 0x00000400 |
| 16332 | +#define VTR_DEVCHAR 0x00000800 |
| 16333 | +#define VTR_SHMLOCK 0x00001000 |
| 16334 | +#define VTR_SHMMAP 0x00002000 |
| 16335 | +#define VTR_SHMBAR 0x00004000 |
| 16336 | +#define VTR_SHMUNMAP 0x00008000 |
| 16337 | +#define VTR_OPEN 0x00010000 |
| 16338 | +#define VTR_DELETE 0x00020000 |
| 16339 | +#define VTR_ACCESS 0x00040000 |
| 16340 | +#define VTR_FULLPATH 0x00080000 |
| 16341 | +#define VTR_DLOPEN 0x00100000 |
| 16342 | +#define VTR_DLERR 0x00200000 |
| 16343 | +#define VTR_DLSYM 0x00400000 |
| 16344 | +#define VTR_DLCLOSE 0x00800000 |
| 16345 | +#define VTR_RAND 0x01000000 |
| 16346 | +#define VTR_SLEEP 0x02000000 |
| 16347 | +#define VTR_CURTIME 0x04000000 |
| 16348 | +#define VTR_LASTERR 0x08000000 |
| 16349 | + |
| 16773 | 16350 | /* |
| 16774 | 16351 | ** Method declarations for vfstrace_file. |
| 16775 | 16352 | */ |
| 16776 | 16353 | static int vfstraceClose(sqlite3_file*); |
| 16777 | 16354 | static int vfstraceRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); |
| | @@ -16832,15 +16409,17 @@ |
| 16832 | 16409 | const char *zFormat, |
| 16833 | 16410 | ... |
| 16834 | 16411 | ){ |
| 16835 | 16412 | va_list ap; |
| 16836 | 16413 | char *zMsg; |
| 16837 | | - va_start(ap, zFormat); |
| 16838 | | - zMsg = sqlite3_vmprintf(zFormat, ap); |
| 16839 | | - va_end(ap); |
| 16840 | | - pInfo->xOut(zMsg, pInfo->pOutArg); |
| 16841 | | - sqlite3_free(zMsg); |
| 16414 | + if( pInfo->bOn ){ |
| 16415 | + va_start(ap, zFormat); |
| 16416 | + zMsg = sqlite3_vmprintf(zFormat, ap); |
| 16417 | + va_end(ap); |
| 16418 | + pInfo->xOut(zMsg, pInfo->pOutArg); |
| 16419 | + sqlite3_free(zMsg); |
| 16420 | + } |
| 16842 | 16421 | } |
| 16843 | 16422 | |
| 16844 | 16423 | /* |
| 16845 | 16424 | ** Try to convert an error code into a symbolic name for that error code. |
| 16846 | 16425 | */ |
| | @@ -16934,18 +16513,26 @@ |
| 16934 | 16513 | int i = *pI; |
| 16935 | 16514 | while( zAppend[0] ){ z[i++] = *(zAppend++); } |
| 16936 | 16515 | z[i] = 0; |
| 16937 | 16516 | *pI = i; |
| 16938 | 16517 | } |
| 16518 | + |
| 16519 | +/* |
| 16520 | +** Turn tracing output on or off according to mMask. |
| 16521 | +*/ |
| 16522 | +static void vfstraceOnOff(vfstrace_info *pInfo, unsigned int mMask){ |
| 16523 | + pInfo->bOn = (pInfo->mTrace & mMask)!=0; |
| 16524 | +} |
| 16939 | 16525 | |
| 16940 | 16526 | /* |
| 16941 | 16527 | ** Close an vfstrace-file. |
| 16942 | 16528 | */ |
| 16943 | 16529 | static int vfstraceClose(sqlite3_file *pFile){ |
| 16944 | 16530 | vfstrace_file *p = (vfstrace_file *)pFile; |
| 16945 | 16531 | vfstrace_info *pInfo = p->pInfo; |
| 16946 | 16532 | int rc; |
| 16533 | + vfstraceOnOff(pInfo, VTR_CLOSE); |
| 16947 | 16534 | vfstrace_printf(pInfo, "%s.xClose(%s)", pInfo->zVfsName, p->zFName); |
| 16948 | 16535 | rc = p->pReal->pMethods->xClose(p->pReal); |
| 16949 | 16536 | vfstrace_print_errcode(pInfo, " -> %s\n", rc); |
| 16950 | 16537 | if( rc==SQLITE_OK ){ |
| 16951 | 16538 | sqlite3_free((void*)p->base.pMethods); |
| | @@ -16964,10 +16551,11 @@ |
| 16964 | 16551 | sqlite_int64 iOfst |
| 16965 | 16552 | ){ |
| 16966 | 16553 | vfstrace_file *p = (vfstrace_file *)pFile; |
| 16967 | 16554 | vfstrace_info *pInfo = p->pInfo; |
| 16968 | 16555 | int rc; |
| 16556 | + vfstraceOnOff(pInfo, VTR_READ); |
| 16969 | 16557 | vfstrace_printf(pInfo, "%s.xRead(%s,n=%d,ofst=%lld)", |
| 16970 | 16558 | pInfo->zVfsName, p->zFName, iAmt, iOfst); |
| 16971 | 16559 | rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst); |
| 16972 | 16560 | vfstrace_print_errcode(pInfo, " -> %s\n", rc); |
| 16973 | 16561 | return rc; |
| | @@ -16983,10 +16571,11 @@ |
| 16983 | 16571 | sqlite_int64 iOfst |
| 16984 | 16572 | ){ |
| 16985 | 16573 | vfstrace_file *p = (vfstrace_file *)pFile; |
| 16986 | 16574 | vfstrace_info *pInfo = p->pInfo; |
| 16987 | 16575 | int rc; |
| 16576 | + vfstraceOnOff(pInfo, VTR_WRITE); |
| 16988 | 16577 | vfstrace_printf(pInfo, "%s.xWrite(%s,n=%d,ofst=%lld)", |
| 16989 | 16578 | pInfo->zVfsName, p->zFName, iAmt, iOfst); |
| 16990 | 16579 | rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst); |
| 16991 | 16580 | vfstrace_print_errcode(pInfo, " -> %s\n", rc); |
| 16992 | 16581 | return rc; |
| | @@ -16997,10 +16586,11 @@ |
| 16997 | 16586 | */ |
| 16998 | 16587 | static int vfstraceTruncate(sqlite3_file *pFile, sqlite_int64 size){ |
| 16999 | 16588 | vfstrace_file *p = (vfstrace_file *)pFile; |
| 17000 | 16589 | vfstrace_info *pInfo = p->pInfo; |
| 17001 | 16590 | int rc; |
| 16591 | + vfstraceOnOff(pInfo, VTR_TRUNC); |
| 17002 | 16592 | vfstrace_printf(pInfo, "%s.xTruncate(%s,%lld)", pInfo->zVfsName, p->zFName, |
| 17003 | 16593 | size); |
| 17004 | 16594 | rc = p->pReal->pMethods->xTruncate(p->pReal, size); |
| 17005 | 16595 | vfstrace_printf(pInfo, " -> %d\n", rc); |
| 17006 | 16596 | return rc; |
| | @@ -17021,10 +16611,11 @@ |
| 17021 | 16611 | else if( flags & SQLITE_SYNC_NORMAL ) strappend(zBuf, &i, "|NORMAL"); |
| 17022 | 16612 | if( flags & SQLITE_SYNC_DATAONLY ) strappend(zBuf, &i, "|DATAONLY"); |
| 17023 | 16613 | if( flags & ~(SQLITE_SYNC_FULL|SQLITE_SYNC_DATAONLY) ){ |
| 17024 | 16614 | sqlite3_snprintf(sizeof(zBuf)-i, &zBuf[i], "|0x%x", flags); |
| 17025 | 16615 | } |
| 16616 | + vfstraceOnOff(pInfo, VTR_SYNC); |
| 17026 | 16617 | vfstrace_printf(pInfo, "%s.xSync(%s,%s)", pInfo->zVfsName, p->zFName, |
| 17027 | 16618 | &zBuf[1]); |
| 17028 | 16619 | rc = p->pReal->pMethods->xSync(p->pReal, flags); |
| 17029 | 16620 | vfstrace_printf(pInfo, " -> %d\n", rc); |
| 17030 | 16621 | return rc; |
| | @@ -17035,10 +16626,11 @@ |
| 17035 | 16626 | */ |
| 17036 | 16627 | static int vfstraceFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){ |
| 17037 | 16628 | vfstrace_file *p = (vfstrace_file *)pFile; |
| 17038 | 16629 | vfstrace_info *pInfo = p->pInfo; |
| 17039 | 16630 | int rc; |
| 16631 | + vfstraceOnOff(pInfo, VTR_FSIZE); |
| 17040 | 16632 | vfstrace_printf(pInfo, "%s.xFileSize(%s)", pInfo->zVfsName, p->zFName); |
| 17041 | 16633 | rc = p->pReal->pMethods->xFileSize(p->pReal, pSize); |
| 17042 | 16634 | vfstrace_print_errcode(pInfo, " -> %s,", rc); |
| 17043 | 16635 | vfstrace_printf(pInfo, " size=%lld\n", *pSize); |
| 17044 | 16636 | return rc; |
| | @@ -17049,11 +16641,11 @@ |
| 17049 | 16641 | */ |
| 17050 | 16642 | static const char *lockName(int eLock){ |
| 17051 | 16643 | const char *azLockNames[] = { |
| 17052 | 16644 | "NONE", "SHARED", "RESERVED", "PENDING", "EXCLUSIVE" |
| 17053 | 16645 | }; |
| 17054 | | - if( eLock<0 || eLock>=sizeof(azLockNames)/sizeof(azLockNames[0]) ){ |
| 16646 | + if( eLock<0 || eLock>=(int)(sizeof(azLockNames)/sizeof(azLockNames[0])) ){ |
| 17055 | 16647 | return "???"; |
| 17056 | 16648 | }else{ |
| 17057 | 16649 | return azLockNames[eLock]; |
| 17058 | 16650 | } |
| 17059 | 16651 | } |
| | @@ -17063,10 +16655,11 @@ |
| 17063 | 16655 | */ |
| 17064 | 16656 | static int vfstraceLock(sqlite3_file *pFile, int eLock){ |
| 17065 | 16657 | vfstrace_file *p = (vfstrace_file *)pFile; |
| 17066 | 16658 | vfstrace_info *pInfo = p->pInfo; |
| 17067 | 16659 | int rc; |
| 16660 | + vfstraceOnOff(pInfo, VTR_LOCK); |
| 17068 | 16661 | vfstrace_printf(pInfo, "%s.xLock(%s,%s)", pInfo->zVfsName, p->zFName, |
| 17069 | 16662 | lockName(eLock)); |
| 17070 | 16663 | rc = p->pReal->pMethods->xLock(p->pReal, eLock); |
| 17071 | 16664 | vfstrace_print_errcode(pInfo, " -> %s\n", rc); |
| 17072 | 16665 | return rc; |
| | @@ -17077,10 +16670,11 @@ |
| 17077 | 16670 | */ |
| 17078 | 16671 | static int vfstraceUnlock(sqlite3_file *pFile, int eLock){ |
| 17079 | 16672 | vfstrace_file *p = (vfstrace_file *)pFile; |
| 17080 | 16673 | vfstrace_info *pInfo = p->pInfo; |
| 17081 | 16674 | int rc; |
| 16675 | + vfstraceOnOff(pInfo, VTR_UNLOCK); |
| 17082 | 16676 | vfstrace_printf(pInfo, "%s.xUnlock(%s,%s)", pInfo->zVfsName, p->zFName, |
| 17083 | 16677 | lockName(eLock)); |
| 17084 | 16678 | rc = p->pReal->pMethods->xUnlock(p->pReal, eLock); |
| 17085 | 16679 | vfstrace_print_errcode(pInfo, " -> %s\n", rc); |
| 17086 | 16680 | return rc; |
| | @@ -17091,10 +16685,11 @@ |
| 17091 | 16685 | */ |
| 17092 | 16686 | static int vfstraceCheckReservedLock(sqlite3_file *pFile, int *pResOut){ |
| 17093 | 16687 | vfstrace_file *p = (vfstrace_file *)pFile; |
| 17094 | 16688 | vfstrace_info *pInfo = p->pInfo; |
| 17095 | 16689 | int rc; |
| 16690 | + vfstraceOnOff(pInfo, VTR_CRL); |
| 17096 | 16691 | vfstrace_printf(pInfo, "%s.xCheckReservedLock(%s,%d)", |
| 17097 | 16692 | pInfo->zVfsName, p->zFName); |
| 17098 | 16693 | rc = p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut); |
| 17099 | 16694 | vfstrace_print_errcode(pInfo, " -> %s", rc); |
| 17100 | 16695 | vfstrace_printf(pInfo, ", out=%d\n", *pResOut); |
| | @@ -17110,10 +16705,11 @@ |
| 17110 | 16705 | int rc; |
| 17111 | 16706 | char zBuf[100]; |
| 17112 | 16707 | char zBuf2[100]; |
| 17113 | 16708 | char *zOp; |
| 17114 | 16709 | char *zRVal = 0; |
| 16710 | + vfstraceOnOff(pInfo, VTR_FCTRL); |
| 17115 | 16711 | switch( op ){ |
| 17116 | 16712 | case SQLITE_FCNTL_LOCKSTATE: zOp = "LOCKSTATE"; break; |
| 17117 | 16713 | case SQLITE_GET_LOCKPROXYFILE: zOp = "GET_LOCKPROXYFILE"; break; |
| 17118 | 16714 | case SQLITE_SET_LOCKPROXYFILE: zOp = "SET_LOCKPROXYFILE"; break; |
| 17119 | 16715 | case SQLITE_LAST_ERRNO: zOp = "LAST_ERRNO"; break; |
| | @@ -17138,10 +16734,83 @@ |
| 17138 | 16734 | case SQLITE_FCNTL_OVERWRITE: zOp = "OVERWRITE"; break; |
| 17139 | 16735 | case SQLITE_FCNTL_VFSNAME: zOp = "VFSNAME"; break; |
| 17140 | 16736 | case SQLITE_FCNTL_POWERSAFE_OVERWRITE: zOp = "POWERSAFE_OVERWRITE"; break; |
| 17141 | 16737 | case SQLITE_FCNTL_PRAGMA: { |
| 17142 | 16738 | const char *const* a = (const char*const*)pArg; |
| 16739 | + if( a[1] && strcmp(a[1],"vfstrace")==0 && a[2] ){ |
| 16740 | + const u8 *zArg = (const u8*)a[2]; |
| 16741 | + if( zArg[0]>='0' && zArg[0]<=9 ){ |
| 16742 | + pInfo->mTrace = (sqlite3_uint64)strtoll(a[2], 0, 0); |
| 16743 | + }else{ |
| 16744 | + static const struct { |
| 16745 | + const char *z; |
| 16746 | + unsigned int m; |
| 16747 | + } aKw[] = { |
| 16748 | + { "all", 0xffffffff }, |
| 16749 | + { "close", VTR_CLOSE }, |
| 16750 | + { "read", VTR_READ }, |
| 16751 | + { "write", VTR_WRITE }, |
| 16752 | + { "truncate", VTR_TRUNC }, |
| 16753 | + { "sync", VTR_SYNC }, |
| 16754 | + { "filesize", VTR_FSIZE }, |
| 16755 | + { "lock", VTR_LOCK }, |
| 16756 | + { "unlock", VTR_UNLOCK }, |
| 16757 | + { "checkreservedlock", VTR_CRL }, |
| 16758 | + { "filecontrol", VTR_FCTRL }, |
| 16759 | + { "sectorsize", VTR_SECSZ }, |
| 16760 | + { "devicecharacteristics", VTR_DEVCHAR }, |
| 16761 | + { "shmlock", VTR_SHMLOCK }, |
| 16762 | + { "shmmap", VTR_SHMMAP }, |
| 16763 | + { "shmummap", VTR_SHMUNMAP }, |
| 16764 | + { "shmbarrier", VTR_SHMBAR }, |
| 16765 | + { "open", VTR_OPEN }, |
| 16766 | + { "delete", VTR_DELETE }, |
| 16767 | + { "access", VTR_ACCESS }, |
| 16768 | + { "fullpathname", VTR_FULLPATH }, |
| 16769 | + { "dlopen", VTR_DLOPEN }, |
| 16770 | + { "dlerror", VTR_DLERR }, |
| 16771 | + { "dlsym", VTR_DLSYM }, |
| 16772 | + { "dlclose", VTR_DLCLOSE }, |
| 16773 | + { "randomness", VTR_RAND }, |
| 16774 | + { "sleep", VTR_SLEEP }, |
| 16775 | + { "currenttime", VTR_CURTIME }, |
| 16776 | + { "currenttimeint64", VTR_CURTIME }, |
| 16777 | + { "getlasterror", VTR_LASTERR }, |
| 16778 | + }; |
| 16779 | + int onOff = 1; |
| 16780 | + while( zArg[0] ){ |
| 16781 | + int jj, n; |
| 16782 | + while( zArg[0]!=0 && zArg[0]!='-' && zArg[0]!='+' |
| 16783 | + && !isalpha(zArg[0]) ) zArg++; |
| 16784 | + if( zArg[0]==0 ) break; |
| 16785 | + if( zArg[0]=='-' ){ |
| 16786 | + onOff = 0; |
| 16787 | + zArg++; |
| 16788 | + }else if( zArg[0]=='+' ){ |
| 16789 | + onOff = 1; |
| 16790 | + zArg++; |
| 16791 | + } |
| 16792 | + while( !isalpha(zArg[0]) ){ |
| 16793 | + if( zArg[0]==0 ) break; |
| 16794 | + zArg++; |
| 16795 | + } |
| 16796 | + if( zArg[0]=='x' && isalpha(zArg[1]) ) zArg++; |
| 16797 | + for(n=0; isalpha(zArg[n]); n++){} |
| 16798 | + for(jj=0; jj<(int)(sizeof(aKw)/sizeof(aKw[0])); jj++){ |
| 16799 | + if( sqlite3_strnicmp(aKw[jj].z,(const char*)zArg,n)==0 ){ |
| 16800 | + if( onOff ){ |
| 16801 | + pInfo->mTrace |= aKw[jj].m; |
| 16802 | + }else{ |
| 16803 | + pInfo->mTrace &= ~aKw[jj].m; |
| 16804 | + } |
| 16805 | + break; |
| 16806 | + } |
| 16807 | + } |
| 16808 | + zArg += n; |
| 16809 | + } |
| 16810 | + } |
| 16811 | + } |
| 17143 | 16812 | sqlite3_snprintf(sizeof(zBuf), zBuf, "PRAGMA,[%s,%s]",a[1],a[2]); |
| 17144 | 16813 | zOp = zBuf; |
| 17145 | 16814 | break; |
| 17146 | 16815 | } |
| 17147 | 16816 | case SQLITE_FCNTL_BUSYHANDLER: zOp = "BUSYHANDLER"; break; |
| | @@ -17233,10 +16902,11 @@ |
| 17233 | 16902 | */ |
| 17234 | 16903 | static int vfstraceSectorSize(sqlite3_file *pFile){ |
| 17235 | 16904 | vfstrace_file *p = (vfstrace_file *)pFile; |
| 17236 | 16905 | vfstrace_info *pInfo = p->pInfo; |
| 17237 | 16906 | int rc; |
| 16907 | + vfstraceOnOff(pInfo, VTR_SECSZ); |
| 17238 | 16908 | vfstrace_printf(pInfo, "%s.xSectorSize(%s)", pInfo->zVfsName, p->zFName); |
| 17239 | 16909 | rc = p->pReal->pMethods->xSectorSize(p->pReal); |
| 17240 | 16910 | vfstrace_printf(pInfo, " -> %d\n", rc); |
| 17241 | 16911 | return rc; |
| 17242 | 16912 | } |
| | @@ -17246,10 +16916,11 @@ |
| 17246 | 16916 | */ |
| 17247 | 16917 | static int vfstraceDeviceCharacteristics(sqlite3_file *pFile){ |
| 17248 | 16918 | vfstrace_file *p = (vfstrace_file *)pFile; |
| 17249 | 16919 | vfstrace_info *pInfo = p->pInfo; |
| 17250 | 16920 | int rc; |
| 16921 | + vfstraceOnOff(pInfo, VTR_DEVCHAR); |
| 17251 | 16922 | vfstrace_printf(pInfo, "%s.xDeviceCharacteristics(%s)", |
| 17252 | 16923 | pInfo->zVfsName, p->zFName); |
| 17253 | 16924 | rc = p->pReal->pMethods->xDeviceCharacteristics(p->pReal); |
| 17254 | 16925 | vfstrace_printf(pInfo, " -> 0x%08x\n", rc); |
| 17255 | 16926 | return rc; |
| | @@ -17257,25 +16928,43 @@ |
| 17257 | 16928 | |
| 17258 | 16929 | /* |
| 17259 | 16930 | ** Shared-memory operations. |
| 17260 | 16931 | */ |
| 17261 | 16932 | static int vfstraceShmLock(sqlite3_file *pFile, int ofst, int n, int flags){ |
| 16933 | + static const char *azLockName[] = { |
| 16934 | + "WRITE", |
| 16935 | + "CKPT", |
| 16936 | + "RECOVER", |
| 16937 | + "READ0", |
| 16938 | + "READ1", |
| 16939 | + "READ2", |
| 16940 | + "READ3", |
| 16941 | + "READ4", |
| 16942 | + }; |
| 17262 | 16943 | vfstrace_file *p = (vfstrace_file *)pFile; |
| 17263 | 16944 | vfstrace_info *pInfo = p->pInfo; |
| 17264 | 16945 | int rc; |
| 17265 | 16946 | char zLck[100]; |
| 17266 | 16947 | int i = 0; |
| 16948 | + vfstraceOnOff(pInfo, VTR_SHMLOCK); |
| 17267 | 16949 | memcpy(zLck, "|0", 3); |
| 17268 | 16950 | if( flags & SQLITE_SHM_UNLOCK ) strappend(zLck, &i, "|UNLOCK"); |
| 17269 | 16951 | if( flags & SQLITE_SHM_LOCK ) strappend(zLck, &i, "|LOCK"); |
| 17270 | 16952 | if( flags & SQLITE_SHM_SHARED ) strappend(zLck, &i, "|SHARED"); |
| 17271 | 16953 | if( flags & SQLITE_SHM_EXCLUSIVE ) strappend(zLck, &i, "|EXCLUSIVE"); |
| 17272 | 16954 | if( flags & ~(0xf) ){ |
| 17273 | 16955 | sqlite3_snprintf(sizeof(zLck)-i, &zLck[i], "|0x%x", flags); |
| 17274 | 16956 | } |
| 17275 | | - vfstrace_printf(pInfo, "%s.xShmLock(%s,ofst=%d,n=%d,%s)", |
| 17276 | | - pInfo->zVfsName, p->zFName, ofst, n, &zLck[1]); |
| 16957 | + if( ofst>=0 && ofst<(int)(sizeof(azLockName)/sizeof(azLockName[0])) ){ |
| 16958 | + vfstrace_printf(pInfo, "%s.xShmLock(%s,ofst=%d(%s),n=%d,%s)", |
| 16959 | + pInfo->zVfsName, p->zFName, ofst, azLockName[ofst], |
| 16960 | + n, &zLck[1]); |
| 16961 | + }else{ |
| 16962 | + vfstrace_printf(pInfo, "%s.xShmLock(%s,ofst=5d,n=%d,%s)", |
| 16963 | + pInfo->zVfsName, p->zFName, ofst, |
| 16964 | + n, &zLck[1]); |
| 16965 | + } |
| 17277 | 16966 | rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags); |
| 17278 | 16967 | vfstrace_print_errcode(pInfo, " -> %s\n", rc); |
| 17279 | 16968 | return rc; |
| 17280 | 16969 | } |
| 17281 | 16970 | static int vfstraceShmMap( |
| | @@ -17286,26 +16975,29 @@ |
| 17286 | 16975 | void volatile **pp |
| 17287 | 16976 | ){ |
| 17288 | 16977 | vfstrace_file *p = (vfstrace_file *)pFile; |
| 17289 | 16978 | vfstrace_info *pInfo = p->pInfo; |
| 17290 | 16979 | int rc; |
| 16980 | + vfstraceOnOff(pInfo, VTR_SHMMAP); |
| 17291 | 16981 | vfstrace_printf(pInfo, "%s.xShmMap(%s,iRegion=%d,szRegion=%d,isWrite=%d,*)", |
| 17292 | 16982 | pInfo->zVfsName, p->zFName, iRegion, szRegion, isWrite); |
| 17293 | 16983 | rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp); |
| 17294 | 16984 | vfstrace_print_errcode(pInfo, " -> %s\n", rc); |
| 17295 | 16985 | return rc; |
| 17296 | 16986 | } |
| 17297 | 16987 | static void vfstraceShmBarrier(sqlite3_file *pFile){ |
| 17298 | 16988 | vfstrace_file *p = (vfstrace_file *)pFile; |
| 17299 | 16989 | vfstrace_info *pInfo = p->pInfo; |
| 16990 | + vfstraceOnOff(pInfo, VTR_SHMBAR); |
| 17300 | 16991 | vfstrace_printf(pInfo, "%s.xShmBarrier(%s)\n", pInfo->zVfsName, p->zFName); |
| 17301 | 16992 | p->pReal->pMethods->xShmBarrier(p->pReal); |
| 17302 | 16993 | } |
| 17303 | 16994 | static int vfstraceShmUnmap(sqlite3_file *pFile, int delFlag){ |
| 17304 | 16995 | vfstrace_file *p = (vfstrace_file *)pFile; |
| 17305 | 16996 | vfstrace_info *pInfo = p->pInfo; |
| 17306 | 16997 | int rc; |
| 16998 | + vfstraceOnOff(pInfo, VTR_SHMUNMAP); |
| 17307 | 16999 | vfstrace_printf(pInfo, "%s.xShmUnmap(%s,delFlag=%d)", |
| 17308 | 17000 | pInfo->zVfsName, p->zFName, delFlag); |
| 17309 | 17001 | rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag); |
| 17310 | 17002 | vfstrace_print_errcode(pInfo, " -> %s\n", rc); |
| 17311 | 17003 | return rc; |
| | @@ -17329,10 +17021,11 @@ |
| 17329 | 17021 | sqlite3_vfs *pRoot = pInfo->pRootVfs; |
| 17330 | 17022 | p->pInfo = pInfo; |
| 17331 | 17023 | p->zFName = zName ? fileTail(zName) : "<temp>"; |
| 17332 | 17024 | p->pReal = (sqlite3_file *)&p[1]; |
| 17333 | 17025 | rc = pRoot->xOpen(pRoot, zName, p->pReal, flags, pOutFlags); |
| 17026 | + vfstraceOnOff(pInfo, VTR_OPEN); |
| 17334 | 17027 | vfstrace_printf(pInfo, "%s.xOpen(%s,flags=0x%x)", |
| 17335 | 17028 | pInfo->zVfsName, p->zFName, flags); |
| 17336 | 17029 | if( p->pReal->pMethods ){ |
| 17337 | 17030 | sqlite3_io_methods *pNew = sqlite3_malloc( sizeof(*pNew) ); |
| 17338 | 17031 | const sqlite3_io_methods *pSub = p->pReal->pMethods; |
| | @@ -17374,10 +17067,11 @@ |
| 17374 | 17067 | */ |
| 17375 | 17068 | static int vfstraceDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ |
| 17376 | 17069 | vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; |
| 17377 | 17070 | sqlite3_vfs *pRoot = pInfo->pRootVfs; |
| 17378 | 17071 | int rc; |
| 17072 | + vfstraceOnOff(pInfo, VTR_DELETE); |
| 17379 | 17073 | vfstrace_printf(pInfo, "%s.xDelete(\"%s\",%d)", |
| 17380 | 17074 | pInfo->zVfsName, zPath, dirSync); |
| 17381 | 17075 | rc = pRoot->xDelete(pRoot, zPath, dirSync); |
| 17382 | 17076 | vfstrace_print_errcode(pInfo, " -> %s\n", rc); |
| 17383 | 17077 | return rc; |
| | @@ -17394,10 +17088,11 @@ |
| 17394 | 17088 | int *pResOut |
| 17395 | 17089 | ){ |
| 17396 | 17090 | vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; |
| 17397 | 17091 | sqlite3_vfs *pRoot = pInfo->pRootVfs; |
| 17398 | 17092 | int rc; |
| 17093 | + vfstraceOnOff(pInfo, VTR_ACCESS); |
| 17399 | 17094 | vfstrace_printf(pInfo, "%s.xAccess(\"%s\",%d)", |
| 17400 | 17095 | pInfo->zVfsName, zPath, flags); |
| 17401 | 17096 | rc = pRoot->xAccess(pRoot, zPath, flags, pResOut); |
| 17402 | 17097 | vfstrace_print_errcode(pInfo, " -> %s", rc); |
| 17403 | 17098 | vfstrace_printf(pInfo, ", out=%d\n", *pResOut); |
| | @@ -17416,10 +17111,11 @@ |
| 17416 | 17111 | char *zOut |
| 17417 | 17112 | ){ |
| 17418 | 17113 | vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; |
| 17419 | 17114 | sqlite3_vfs *pRoot = pInfo->pRootVfs; |
| 17420 | 17115 | int rc; |
| 17116 | + vfstraceOnOff(pInfo, VTR_FULLPATH); |
| 17421 | 17117 | vfstrace_printf(pInfo, "%s.xFullPathname(\"%s\")", |
| 17422 | 17118 | pInfo->zVfsName, zPath); |
| 17423 | 17119 | rc = pRoot->xFullPathname(pRoot, zPath, nOut, zOut); |
| 17424 | 17120 | vfstrace_print_errcode(pInfo, " -> %s", rc); |
| 17425 | 17121 | vfstrace_printf(pInfo, ", out=\"%.*s\"\n", nOut, zOut); |
| | @@ -17430,10 +17126,11 @@ |
| 17430 | 17126 | ** Open the dynamic library located at zPath and return a handle. |
| 17431 | 17127 | */ |
| 17432 | 17128 | static void *vfstraceDlOpen(sqlite3_vfs *pVfs, const char *zPath){ |
| 17433 | 17129 | vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; |
| 17434 | 17130 | sqlite3_vfs *pRoot = pInfo->pRootVfs; |
| 17131 | + vfstraceOnOff(pInfo, VTR_DLOPEN); |
| 17435 | 17132 | vfstrace_printf(pInfo, "%s.xDlOpen(\"%s\")\n", pInfo->zVfsName, zPath); |
| 17436 | 17133 | return pRoot->xDlOpen(pRoot, zPath); |
| 17437 | 17134 | } |
| 17438 | 17135 | |
| 17439 | 17136 | /* |
| | @@ -17442,10 +17139,11 @@ |
| 17442 | 17139 | ** with dynamic libraries. |
| 17443 | 17140 | */ |
| 17444 | 17141 | static void vfstraceDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){ |
| 17445 | 17142 | vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; |
| 17446 | 17143 | sqlite3_vfs *pRoot = pInfo->pRootVfs; |
| 17144 | + vfstraceOnOff(pInfo, VTR_DLERR); |
| 17447 | 17145 | vfstrace_printf(pInfo, "%s.xDlError(%d)", pInfo->zVfsName, nByte); |
| 17448 | 17146 | pRoot->xDlError(pRoot, nByte, zErrMsg); |
| 17449 | 17147 | vfstrace_printf(pInfo, " -> \"%s\"", zErrMsg); |
| 17450 | 17148 | } |
| 17451 | 17149 | |
| | @@ -17463,10 +17161,11 @@ |
| 17463 | 17161 | ** Close the dynamic library handle pHandle. |
| 17464 | 17162 | */ |
| 17465 | 17163 | static void vfstraceDlClose(sqlite3_vfs *pVfs, void *pHandle){ |
| 17466 | 17164 | vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; |
| 17467 | 17165 | sqlite3_vfs *pRoot = pInfo->pRootVfs; |
| 17166 | + vfstraceOnOff(pInfo, VTR_DLCLOSE); |
| 17468 | 17167 | vfstrace_printf(pInfo, "%s.xDlOpen()\n", pInfo->zVfsName); |
| 17469 | 17168 | pRoot->xDlClose(pRoot, pHandle); |
| 17470 | 17169 | } |
| 17471 | 17170 | |
| 17472 | 17171 | /* |
| | @@ -17474,10 +17173,11 @@ |
| 17474 | 17173 | ** random data. |
| 17475 | 17174 | */ |
| 17476 | 17175 | static int vfstraceRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ |
| 17477 | 17176 | vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; |
| 17478 | 17177 | sqlite3_vfs *pRoot = pInfo->pRootVfs; |
| 17178 | + vfstraceOnOff(pInfo, VTR_RAND); |
| 17479 | 17179 | vfstrace_printf(pInfo, "%s.xRandomness(%d)\n", pInfo->zVfsName, nByte); |
| 17480 | 17180 | return pRoot->xRandomness(pRoot, nByte, zBufOut); |
| 17481 | 17181 | } |
| 17482 | 17182 | |
| 17483 | 17183 | /* |
| | @@ -17485,34 +17185,52 @@ |
| 17485 | 17185 | ** actually slept. |
| 17486 | 17186 | */ |
| 17487 | 17187 | static int vfstraceSleep(sqlite3_vfs *pVfs, int nMicro){ |
| 17488 | 17188 | vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; |
| 17489 | 17189 | sqlite3_vfs *pRoot = pInfo->pRootVfs; |
| 17190 | + vfstraceOnOff(pInfo, VTR_SLEEP); |
| 17191 | + vfstrace_printf(pInfo, "%s.xSleep(%d)\n", pInfo->zVfsName, nMicro); |
| 17490 | 17192 | return pRoot->xSleep(pRoot, nMicro); |
| 17491 | 17193 | } |
| 17492 | 17194 | |
| 17493 | 17195 | /* |
| 17494 | 17196 | ** Return the current time as a Julian Day number in *pTimeOut. |
| 17495 | 17197 | */ |
| 17496 | 17198 | static int vfstraceCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){ |
| 17497 | 17199 | vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; |
| 17498 | 17200 | sqlite3_vfs *pRoot = pInfo->pRootVfs; |
| 17499 | | - return pRoot->xCurrentTime(pRoot, pTimeOut); |
| 17201 | + int rc; |
| 17202 | + vfstraceOnOff(pInfo, VTR_CURTIME); |
| 17203 | + vfstrace_printf(pInfo, "%s.xCurrentTime()", pInfo->zVfsName); |
| 17204 | + rc = pRoot->xCurrentTime(pRoot, pTimeOut); |
| 17205 | + vfstrace_printf(pInfo, " -> %.17g\n", *pTimeOut); |
| 17206 | + return rc; |
| 17500 | 17207 | } |
| 17501 | 17208 | static int vfstraceCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){ |
| 17502 | 17209 | vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; |
| 17503 | 17210 | sqlite3_vfs *pRoot = pInfo->pRootVfs; |
| 17504 | | - return pRoot->xCurrentTimeInt64(pRoot, pTimeOut); |
| 17211 | + int rc; |
| 17212 | + vfstraceOnOff(pInfo, VTR_CURTIME); |
| 17213 | + vfstrace_printf(pInfo, "%s.xCurrentTimeInt64()", pInfo->zVfsName); |
| 17214 | + rc = pRoot->xCurrentTimeInt64(pRoot, pTimeOut); |
| 17215 | + vfstrace_printf(pInfo, " -> %lld\n", *pTimeOut); |
| 17216 | + return rc; |
| 17505 | 17217 | } |
| 17506 | 17218 | |
| 17507 | 17219 | /* |
| 17508 | | -** Return th3 most recent error code and message |
| 17220 | +** Return the most recent error code and message |
| 17509 | 17221 | */ |
| 17510 | | -static int vfstraceGetLastError(sqlite3_vfs *pVfs, int iErr, char *zErr){ |
| 17222 | +static int vfstraceGetLastError(sqlite3_vfs *pVfs, int nErr, char *zErr){ |
| 17511 | 17223 | vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; |
| 17512 | 17224 | sqlite3_vfs *pRoot = pInfo->pRootVfs; |
| 17513 | | - return pRoot->xGetLastError(pRoot, iErr, zErr); |
| 17225 | + int rc; |
| 17226 | + vfstraceOnOff(pInfo, VTR_LASTERR); |
| 17227 | + vfstrace_printf(pInfo, "%s.xGetLastError(%d,zBuf)", pInfo->zVfsName, nErr); |
| 17228 | + if( nErr ) zErr[0] = 0; |
| 17229 | + rc = pRoot->xGetLastError(pRoot, nErr, zErr); |
| 17230 | + vfstrace_printf(pInfo, " -> zBuf[] = \"%s\", rc = %d\n", nErr?zErr:"", rc); |
| 17231 | + return rc; |
| 17514 | 17232 | } |
| 17515 | 17233 | |
| 17516 | 17234 | /* |
| 17517 | 17235 | ** Override system calls. |
| 17518 | 17236 | */ |
| | @@ -17602,10 +17320,12 @@ |
| 17602 | 17320 | pInfo->pRootVfs = pRoot; |
| 17603 | 17321 | pInfo->xOut = xOut; |
| 17604 | 17322 | pInfo->pOutArg = pOutArg; |
| 17605 | 17323 | pInfo->zVfsName = pNew->zName; |
| 17606 | 17324 | pInfo->pTraceVfs = pNew; |
| 17325 | + pInfo->mTrace = 0xffffffff; |
| 17326 | + pInfo->bOn = 1; |
| 17607 | 17327 | vfstrace_printf(pInfo, "%s.enabled_for(\"%s\")\n", |
| 17608 | 17328 | pInfo->zVfsName, pRoot->zName); |
| 17609 | 17329 | return sqlite3_vfs_register(pNew, makeDefault); |
| 17610 | 17330 | } |
| 17611 | 17331 | |
| | @@ -19650,11 +19370,11 @@ |
| 19650 | 19370 | }while( strstr(z,zBuf)!=0 ); |
| 19651 | 19371 | return zBuf; |
| 19652 | 19372 | } |
| 19653 | 19373 | |
| 19654 | 19374 | /* |
| 19655 | | -** Implementation of scalar SQL function "escape_crnl". The argument passed to |
| 19375 | +** Implementation of scalar SQL function "escape_crlf". The argument passed to |
| 19656 | 19376 | ** this function is the output of built-in function quote(). If the first |
| 19657 | 19377 | ** character of the input is "'", indicating that the value passed to quote() |
| 19658 | 19378 | ** was a text value, then this function searches the input for "\n" and "\r" |
| 19659 | 19379 | ** characters and adds a wrapper similar to the following: |
| 19660 | 19380 | ** |
| | @@ -19661,11 +19381,11 @@ |
| 19661 | 19381 | ** replace(replace(<input>, '\n', char(10), '\r', char(13)); |
| 19662 | 19382 | ** |
| 19663 | 19383 | ** Or, if the first character of the input is not "'", then a copy of the input |
| 19664 | 19384 | ** is returned. |
| 19665 | 19385 | */ |
| 19666 | | -static void recoverEscapeCrnl( |
| 19386 | +static void recoverEscapeCrlf( |
| 19667 | 19387 | sqlite3_context *context, |
| 19668 | 19388 | int argc, |
| 19669 | 19389 | sqlite3_value **argv |
| 19670 | 19390 | ){ |
| 19671 | 19391 | const char *zText = (const char*)sqlite3_value_text(argv[0]); |
| | @@ -19876,11 +19596,11 @@ |
| 19876 | 19596 | void (*xFunc)(sqlite3_context*,int,sqlite3_value **); |
| 19877 | 19597 | } aFunc[] = { |
| 19878 | 19598 | { "getpage", 1, recoverGetPage }, |
| 19879 | 19599 | { "page_is_used", 1, recoverPageIsUsed }, |
| 19880 | 19600 | { "read_i32", 2, recoverReadI32 }, |
| 19881 | | - { "escape_crnl", 1, recoverEscapeCrnl }, |
| 19601 | + { "escape_crlf", 1, recoverEscapeCrlf }, |
| 19882 | 19602 | }; |
| 19883 | 19603 | |
| 19884 | 19604 | const int flags = SQLITE_OPEN_URI|SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE; |
| 19885 | 19605 | sqlite3 *db = 0; /* New database handle */ |
| 19886 | 19606 | int ii; /* For iterating through aFunc[] */ |
| | @@ -20229,11 +19949,11 @@ |
| 20229 | 19949 | assert( pTab->aCol[ii].iField>=0 && pTab->aCol[ii].iBind>=1 ); |
| 20230 | 19950 | zSql = recoverMPrintf(p, "%z%s%Q", zSql, zSep, pTab->aCol[ii].zCol); |
| 20231 | 19951 | |
| 20232 | 19952 | if( bSql ){ |
| 20233 | 19953 | zBind = recoverMPrintf(p, |
| 20234 | | - "%z%sescape_crnl(quote(?%d))", zBind, zSqlSep, pTab->aCol[ii].iBind |
| 19954 | + "%z%sescape_crlf(quote(?%d))", zBind, zSqlSep, pTab->aCol[ii].iBind |
| 20235 | 19955 | ); |
| 20236 | 19956 | zSqlSep = "||', '||"; |
| 20237 | 19957 | }else{ |
| 20238 | 19958 | zBind = recoverMPrintf(p, "%z%s?%d", zBind, zSep, pTab->aCol[ii].iBind); |
| 20239 | 19959 | } |
| | @@ -20731,10 +20451,12 @@ |
| 20731 | 20451 | apVal[iField] = sqlite3_value_dup( pVal ); |
| 20732 | 20452 | if( apVal[iField]==0 ){ |
| 20733 | 20453 | recoverError(p, SQLITE_NOMEM, 0); |
| 20734 | 20454 | } |
| 20735 | 20455 | p1->nVal = iField+1; |
| 20456 | + }else if( pTab->nCol==0 ){ |
| 20457 | + p1->nVal = pTab->nCol; |
| 20736 | 20458 | } |
| 20737 | 20459 | p1->iPrevCell = iCell; |
| 20738 | 20460 | p1->iPrevPage = iPage; |
| 20739 | 20461 | } |
| 20740 | 20462 | }else{ |
| | @@ -21846,10 +21568,11 @@ |
| 21846 | 21568 | u8 nEqpLevel; /* Depth of the EQP output graph */ |
| 21847 | 21569 | u8 eTraceType; /* SHELL_TRACE_* value for type of trace */ |
| 21848 | 21570 | u8 bSafeMode; /* True to prohibit unsafe operations */ |
| 21849 | 21571 | u8 bSafeModePersist; /* The long-term value of bSafeMode */ |
| 21850 | 21572 | u8 eRestoreState; /* See comments above doAutoDetectRestore() */ |
| 21573 | + u8 crlfMode; /* Do NL-to-CRLF translations when enabled (maybe) */ |
| 21851 | 21574 | ColModeOpts cmOpts; /* Option values affecting columnar mode output */ |
| 21852 | 21575 | unsigned statsOn; /* True to display memory stats before each finalize */ |
| 21853 | 21576 | unsigned mEqpLines; /* Mask of vertical lines in the EQP output graph */ |
| 21854 | 21577 | int inputNesting; /* Track nesting level of .read and other redirects */ |
| 21855 | 21578 | int outCount; /* Revert to stdout when reaching zero */ |
| | @@ -21991,10 +21714,11 @@ |
| 21991 | 21714 | #define MODE_Table 15 /* MySQL-style table formatting */ |
| 21992 | 21715 | #define MODE_Box 16 /* Unicode box-drawing characters */ |
| 21993 | 21716 | #define MODE_Count 17 /* Output only a count of the rows of output */ |
| 21994 | 21717 | #define MODE_Off 18 /* No query output shown */ |
| 21995 | 21718 | #define MODE_ScanExp 19 /* Like MODE_Explain, but for ".scanstats vm" */ |
| 21719 | +#define MODE_Www 20 /* Full web-page output */ |
| 21996 | 21720 | |
| 21997 | 21721 | static const char *modeDescr[] = { |
| 21998 | 21722 | "line", |
| 21999 | 21723 | "column", |
| 22000 | 21724 | "list", |
| | @@ -22011,11 +21735,13 @@ |
| 22011 | 21735 | "json", |
| 22012 | 21736 | "markdown", |
| 22013 | 21737 | "table", |
| 22014 | 21738 | "box", |
| 22015 | 21739 | "count", |
| 22016 | | - "off" |
| 21740 | + "off", |
| 21741 | + "scanexp", |
| 21742 | + "www", |
| 22017 | 21743 | }; |
| 22018 | 21744 | |
| 22019 | 21745 | /* |
| 22020 | 21746 | ** These are the column/row/line separators used by the various |
| 22021 | 21747 | ** import/export modes. |
| | @@ -22039,11 +21765,11 @@ |
| 22039 | 21765 | ** A callback for the sqlite3_log() interface. |
| 22040 | 21766 | */ |
| 22041 | 21767 | static void shellLog(void *pArg, int iErrCode, const char *zMsg){ |
| 22042 | 21768 | ShellState *p = (ShellState*)pArg; |
| 22043 | 21769 | if( p->pLog==0 ) return; |
| 22044 | | - sputf(p->pLog, "(%d) %s\n", iErrCode, zMsg); |
| 21770 | + sqlite3_fprintf(p->pLog, "(%d) %s\n", iErrCode, zMsg); |
| 22045 | 21771 | fflush(p->pLog); |
| 22046 | 21772 | } |
| 22047 | 21773 | |
| 22048 | 21774 | /* |
| 22049 | 21775 | ** SQL function: shell_putsnl(X) |
| | @@ -22054,13 +21780,13 @@ |
| 22054 | 21780 | static void shellPutsFunc( |
| 22055 | 21781 | sqlite3_context *pCtx, |
| 22056 | 21782 | int nVal, |
| 22057 | 21783 | sqlite3_value **apVal |
| 22058 | 21784 | ){ |
| 22059 | | - /* Unused: (ShellState*)sqlite3_user_data(pCtx); */ |
| 21785 | + ShellState *p = (ShellState*)sqlite3_user_data(pCtx); |
| 22060 | 21786 | (void)nVal; |
| 22061 | | - oputf("%s\n", sqlite3_value_text(apVal[0])); |
| 21787 | + sqlite3_fprintf(p->out, "%s\n", sqlite3_value_text(apVal[0])); |
| 22062 | 21788 | sqlite3_result_value(pCtx, apVal[0]); |
| 22063 | 21789 | } |
| 22064 | 21790 | |
| 22065 | 21791 | /* |
| 22066 | 21792 | ** If in safe mode, print an error message described by the arguments |
| | @@ -22075,11 +21801,11 @@ |
| 22075 | 21801 | va_list ap; |
| 22076 | 21802 | char *zMsg; |
| 22077 | 21803 | va_start(ap, zErrMsg); |
| 22078 | 21804 | zMsg = sqlite3_vmprintf(zErrMsg, ap); |
| 22079 | 21805 | va_end(ap); |
| 22080 | | - eputf("line %d: %s\n", p->lineno, zMsg); |
| 21806 | + sqlite3_fprintf(stderr, "line %d: %s\n", p->lineno, zMsg); |
| 22081 | 21807 | exit(1); |
| 22082 | 21808 | } |
| 22083 | 21809 | } |
| 22084 | 21810 | |
| 22085 | 21811 | /* |
| | @@ -22108,11 +21834,11 @@ |
| 22108 | 21834 | char *zTempFile = 0; |
| 22109 | 21835 | sqlite3 *db; |
| 22110 | 21836 | char *zCmd = 0; |
| 22111 | 21837 | int bBin; |
| 22112 | 21838 | int rc; |
| 22113 | | - int hasCRNL = 0; |
| 21839 | + int hasCRLF = 0; |
| 22114 | 21840 | FILE *f = 0; |
| 22115 | 21841 | sqlite3_int64 sz; |
| 22116 | 21842 | sqlite3_int64 x; |
| 22117 | 21843 | unsigned char *p = 0; |
| 22118 | 21844 | |
| | @@ -22142,11 +21868,11 @@ |
| 22142 | 21868 | } |
| 22143 | 21869 | } |
| 22144 | 21870 | bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB; |
| 22145 | 21871 | /* When writing the file to be edited, do \n to \r\n conversions on systems |
| 22146 | 21872 | ** that want \r\n line endings */ |
| 22147 | | - f = fopen(zTempFile, bBin ? "wb" : "w"); |
| 21873 | + f = sqlite3_fopen(zTempFile, bBin ? "wb" : "w"); |
| 22148 | 21874 | if( f==0 ){ |
| 22149 | 21875 | sqlite3_result_error(context, "edit() cannot open temp file", -1); |
| 22150 | 21876 | goto edit_func_end; |
| 22151 | 21877 | } |
| 22152 | 21878 | sz = sqlite3_value_bytes(argv[0]); |
| | @@ -22153,11 +21879,11 @@ |
| 22153 | 21879 | if( bBin ){ |
| 22154 | 21880 | x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f); |
| 22155 | 21881 | }else{ |
| 22156 | 21882 | const char *z = (const char*)sqlite3_value_text(argv[0]); |
| 22157 | 21883 | /* Remember whether or not the value originally contained \r\n */ |
| 22158 | | - if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1; |
| 21884 | + if( z && strstr(z,"\r\n")!=0 ) hasCRLF = 1; |
| 22159 | 21885 | x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f); |
| 22160 | 21886 | } |
| 22161 | 21887 | fclose(f); |
| 22162 | 21888 | f = 0; |
| 22163 | 21889 | if( x!=sz ){ |
| | @@ -22173,11 +21899,11 @@ |
| 22173 | 21899 | sqlite3_free(zCmd); |
| 22174 | 21900 | if( rc ){ |
| 22175 | 21901 | sqlite3_result_error(context, "EDITOR returned non-zero", -1); |
| 22176 | 21902 | goto edit_func_end; |
| 22177 | 21903 | } |
| 22178 | | - f = fopen(zTempFile, "rb"); |
| 21904 | + f = sqlite3_fopen(zTempFile, "rb"); |
| 22179 | 21905 | if( f==0 ){ |
| 22180 | 21906 | sqlite3_result_error(context, |
| 22181 | 21907 | "edit() cannot reopen temp file after edit", -1); |
| 22182 | 21908 | goto edit_func_end; |
| 22183 | 21909 | } |
| | @@ -22198,11 +21924,11 @@ |
| 22198 | 21924 | } |
| 22199 | 21925 | if( bBin ){ |
| 22200 | 21926 | sqlite3_result_blob64(context, p, sz, sqlite3_free); |
| 22201 | 21927 | }else{ |
| 22202 | 21928 | sqlite3_int64 i, j; |
| 22203 | | - if( hasCRNL ){ |
| 21929 | + if( hasCRLF ){ |
| 22204 | 21930 | /* If the original contains \r\n then do no conversions back to \n */ |
| 22205 | 21931 | }else{ |
| 22206 | 21932 | /* If the file did not originally contain \r\n then convert any new |
| 22207 | 21933 | ** \r\n back into \n */ |
| 22208 | 21934 | p[sz] = 0; |
| | @@ -22239,15 +21965,30 @@ |
| 22239 | 21965 | p->mode = p->modePrior; |
| 22240 | 21966 | p->shellFlgs = p->priorShFlgs; |
| 22241 | 21967 | memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator)); |
| 22242 | 21968 | memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator)); |
| 22243 | 21969 | } |
| 21970 | + |
| 21971 | +/* |
| 21972 | +** Set output mode to text or binary for Windows. |
| 21973 | +*/ |
| 21974 | +static void setCrlfMode(ShellState *p){ |
| 21975 | +#ifdef _WIN32 |
| 21976 | + if( p->crlfMode ){ |
| 21977 | + sqlite3_fsetmode(p->out, _O_TEXT); |
| 21978 | + }else{ |
| 21979 | + sqlite3_fsetmode(p->out, _O_BINARY); |
| 21980 | + } |
| 21981 | +#else |
| 21982 | + UNUSED_PARAMETER(p); |
| 21983 | +#endif |
| 21984 | +} |
| 22244 | 21985 | |
| 22245 | 21986 | /* |
| 22246 | 21987 | ** Output the given string as a hex-encoded blob (eg. X'1234' ) |
| 22247 | 21988 | */ |
| 22248 | | -static void output_hex_blob(const void *pBlob, int nBlob){ |
| 21989 | +static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){ |
| 22249 | 21990 | int i; |
| 22250 | 21991 | unsigned char *aBlob = (unsigned char*)pBlob; |
| 22251 | 21992 | |
| 22252 | 21993 | char *zStr = sqlite3_malloc(nBlob*2 + 1); |
| 22253 | 21994 | shell_check_oom(zStr); |
| | @@ -22260,11 +22001,11 @@ |
| 22260 | 22001 | zStr[i*2] = aHex[ (aBlob[i] >> 4) ]; |
| 22261 | 22002 | zStr[i*2+1] = aHex[ (aBlob[i] & 0x0F) ]; |
| 22262 | 22003 | } |
| 22263 | 22004 | zStr[i*2] = '\0'; |
| 22264 | 22005 | |
| 22265 | | - oputf("X'%s'", zStr); |
| 22006 | + sqlite3_fprintf(out, "X'%s'", zStr); |
| 22266 | 22007 | sqlite3_free(zStr); |
| 22267 | 22008 | } |
| 22268 | 22009 | |
| 22269 | 22010 | /* |
| 22270 | 22011 | ** Find a string that is not found anywhere in z[]. Return a pointer |
| | @@ -22290,46 +22031,40 @@ |
| 22290 | 22031 | /* |
| 22291 | 22032 | ** Output the given string as a quoted string using SQL quoting conventions. |
| 22292 | 22033 | ** |
| 22293 | 22034 | ** See also: output_quoted_escaped_string() |
| 22294 | 22035 | */ |
| 22295 | | -static void output_quoted_string(const char *z){ |
| 22036 | +static void output_quoted_string(ShellState *p, const char *z){ |
| 22296 | 22037 | int i; |
| 22297 | 22038 | char c; |
| 22298 | | -#ifndef SQLITE_SHELL_FIDDLE |
| 22299 | | - FILE *pfO = setOutputStream(invalidFileStream); |
| 22300 | | - setBinaryMode(pfO, 1); |
| 22301 | | -#endif |
| 22039 | + FILE *out = p->out; |
| 22040 | + sqlite3_fsetmode(out, _O_BINARY); |
| 22302 | 22041 | if( z==0 ) return; |
| 22303 | 22042 | for(i=0; (c = z[i])!=0 && c!='\''; i++){} |
| 22304 | 22043 | if( c==0 ){ |
| 22305 | | - oputf("'%s'",z); |
| 22044 | + sqlite3_fprintf(out, "'%s'",z); |
| 22306 | 22045 | }else{ |
| 22307 | | - oputz("'"); |
| 22046 | + sqlite3_fputs("'", out); |
| 22308 | 22047 | while( *z ){ |
| 22309 | 22048 | for(i=0; (c = z[i])!=0 && c!='\''; i++){} |
| 22310 | 22049 | if( c=='\'' ) i++; |
| 22311 | 22050 | if( i ){ |
| 22312 | | - oputf("%.*s", i, z); |
| 22051 | + sqlite3_fprintf(out, "%.*s", i, z); |
| 22313 | 22052 | z += i; |
| 22314 | 22053 | } |
| 22315 | 22054 | if( c=='\'' ){ |
| 22316 | | - oputz("'"); |
| 22055 | + sqlite3_fputs("'", out); |
| 22317 | 22056 | continue; |
| 22318 | 22057 | } |
| 22319 | 22058 | if( c==0 ){ |
| 22320 | 22059 | break; |
| 22321 | 22060 | } |
| 22322 | 22061 | z++; |
| 22323 | 22062 | } |
| 22324 | | - oputz("'"); |
| 22063 | + sqlite3_fputs("'", out); |
| 22325 | 22064 | } |
| 22326 | | -#ifndef SQLITE_SHELL_FIDDLE |
| 22327 | | - setTextMode(pfO, 1); |
| 22328 | | -#else |
| 22329 | | - setTextMode(stdout, 1); |
| 22330 | | -#endif |
| 22065 | + setCrlfMode(p); |
| 22331 | 22066 | } |
| 22332 | 22067 | |
| 22333 | 22068 | /* |
| 22334 | 22069 | ** Output the given string as a quoted string using SQL quoting conventions. |
| 22335 | 22070 | ** Additionallly , escape the "\n" and "\r" characters so that they do not |
| | @@ -22337,20 +22072,18 @@ |
| 22337 | 22072 | ** systems. |
| 22338 | 22073 | ** |
| 22339 | 22074 | ** This is like output_quoted_string() but with the addition of the \r\n |
| 22340 | 22075 | ** escape mechanism. |
| 22341 | 22076 | */ |
| 22342 | | -static void output_quoted_escaped_string(const char *z){ |
| 22077 | +static void output_quoted_escaped_string(ShellState *p, const char *z){ |
| 22343 | 22078 | int i; |
| 22344 | 22079 | char c; |
| 22345 | | -#ifndef SQLITE_SHELL_FIDDLE |
| 22346 | | - FILE *pfO = setOutputStream(invalidFileStream); |
| 22347 | | - setBinaryMode(pfO, 1); |
| 22348 | | -#endif |
| 22080 | + FILE *out = p->out; |
| 22081 | + sqlite3_fsetmode(out, _O_BINARY); |
| 22349 | 22082 | for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){} |
| 22350 | 22083 | if( c==0 ){ |
| 22351 | | - oputf("'%s'",z); |
| 22084 | + sqlite3_fprintf(out, "'%s'",z); |
| 22352 | 22085 | }else{ |
| 22353 | 22086 | const char *zNL = 0; |
| 22354 | 22087 | const char *zCR = 0; |
| 22355 | 22088 | int nNL = 0; |
| 22356 | 22089 | int nCR = 0; |
| | @@ -22358,52 +22091,48 @@ |
| 22358 | 22091 | for(i=0; z[i]; i++){ |
| 22359 | 22092 | if( z[i]=='\n' ) nNL++; |
| 22360 | 22093 | if( z[i]=='\r' ) nCR++; |
| 22361 | 22094 | } |
| 22362 | 22095 | if( nNL ){ |
| 22363 | | - oputz("replace("); |
| 22096 | + sqlite3_fputs("replace(", out); |
| 22364 | 22097 | zNL = unused_string(z, "\\n", "\\012", zBuf1); |
| 22365 | 22098 | } |
| 22366 | 22099 | if( nCR ){ |
| 22367 | | - oputz("replace("); |
| 22100 | + sqlite3_fputs("replace(", out); |
| 22368 | 22101 | zCR = unused_string(z, "\\r", "\\015", zBuf2); |
| 22369 | 22102 | } |
| 22370 | | - oputz("'"); |
| 22103 | + sqlite3_fputs("'", out); |
| 22371 | 22104 | while( *z ){ |
| 22372 | 22105 | for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){} |
| 22373 | 22106 | if( c=='\'' ) i++; |
| 22374 | 22107 | if( i ){ |
| 22375 | | - oputf("%.*s", i, z); |
| 22108 | + sqlite3_fprintf(out, "%.*s", i, z); |
| 22376 | 22109 | z += i; |
| 22377 | 22110 | } |
| 22378 | 22111 | if( c=='\'' ){ |
| 22379 | | - oputz("'"); |
| 22112 | + sqlite3_fputs("'", out); |
| 22380 | 22113 | continue; |
| 22381 | 22114 | } |
| 22382 | 22115 | if( c==0 ){ |
| 22383 | 22116 | break; |
| 22384 | 22117 | } |
| 22385 | 22118 | z++; |
| 22386 | 22119 | if( c=='\n' ){ |
| 22387 | | - oputz(zNL); |
| 22120 | + sqlite3_fputs(zNL, out); |
| 22388 | 22121 | continue; |
| 22389 | 22122 | } |
| 22390 | | - oputz(zCR); |
| 22123 | + sqlite3_fputs(zCR, out); |
| 22391 | 22124 | } |
| 22392 | | - oputz("'"); |
| 22125 | + sqlite3_fputs("'", out); |
| 22393 | 22126 | if( nCR ){ |
| 22394 | | - oputf(",'%s',char(13))", zCR); |
| 22127 | + sqlite3_fprintf(out, ",'%s',char(13))", zCR); |
| 22395 | 22128 | } |
| 22396 | 22129 | if( nNL ){ |
| 22397 | | - oputf(",'%s',char(10))", zNL); |
| 22130 | + sqlite3_fprintf(out, ",'%s',char(10))", zNL); |
| 22398 | 22131 | } |
| 22399 | 22132 | } |
| 22400 | | -#ifndef SQLITE_SHELL_FIDDLE |
| 22401 | | - setTextMode(pfO, 1); |
| 22402 | | -#else |
| 22403 | | - setTextMode(stdout, 1); |
| 22404 | | -#endif |
| 22133 | + setCrlfMode(p); |
| 22405 | 22134 | } |
| 22406 | 22135 | |
| 22407 | 22136 | /* |
| 22408 | 22137 | ** Find earliest of chars within s specified in zAny. |
| 22409 | 22138 | ** With ns == ~0, is like strpbrk(s,zAny) and s must be 0-terminated. |
| | @@ -22419,26 +22148,64 @@ |
| 22419 | 22148 | } |
| 22420 | 22149 | ++zAny; |
| 22421 | 22150 | } |
| 22422 | 22151 | return pcFirst; |
| 22423 | 22152 | } |
| 22153 | + |
| 22154 | +/* Skip over as much z[] input char sequence as is valid UTF-8, |
| 22155 | +** limited per nAccept char's or whole characters and containing |
| 22156 | +** no char cn such that ((1<<cn) & ccm)!=0. On return, the |
| 22157 | +** sequence z:return (inclusive:exclusive) is validated UTF-8. |
| 22158 | +** Limit: nAccept>=0 => char count, nAccept<0 => character |
| 22159 | + */ |
| 22160 | +const char *zSkipValidUtf8(const char *z, int nAccept, long ccm){ |
| 22161 | + int ng = (nAccept<0)? -nAccept : 0; |
| 22162 | + const char *pcLimit = (nAccept>=0)? z+nAccept : 0; |
| 22163 | + assert(z!=0); |
| 22164 | + while( (pcLimit)? (z<pcLimit) : (ng-- != 0) ){ |
| 22165 | + unsigned char c = *(u8*)z; |
| 22166 | + if( c<0x7f ){ |
| 22167 | + if( ccm != 0L && c < 0x20 && ((1L<<c) & ccm) != 0 ) return z; |
| 22168 | + ++z; /* ASCII */ |
| 22169 | + }else if( (c & 0xC0) != 0xC0 ) return z; /* not a lead byte */ |
| 22170 | + else{ |
| 22171 | + const char *zt = z+1; /* Got lead byte, look at trail bytes.*/ |
| 22172 | + do{ |
| 22173 | + if( pcLimit && zt >= pcLimit ) return z; |
| 22174 | + else{ |
| 22175 | + char ct = *zt++; |
| 22176 | + if( ct==0 || (zt-z)>4 || (ct & 0xC0)!=0x80 ){ |
| 22177 | + /* Trailing bytes are too few, too many, or invalid. */ |
| 22178 | + return z; |
| 22179 | + } |
| 22180 | + } |
| 22181 | + } while( ((c <<= 1) & 0x40) == 0x40 ); /* Eat lead byte's count. */ |
| 22182 | + z = zt; |
| 22183 | + } |
| 22184 | + } |
| 22185 | + return z; |
| 22186 | +} |
| 22187 | + |
| 22188 | + |
| 22424 | 22189 | /* |
| 22425 | 22190 | ** Output the given string as a quoted according to C or TCL quoting rules. |
| 22426 | 22191 | */ |
| 22427 | | -static void output_c_string(const char *z){ |
| 22192 | +static void output_c_string(FILE *out, const char *z){ |
| 22428 | 22193 | char c; |
| 22429 | 22194 | static const char *zq = "\""; |
| 22430 | 22195 | static long ctrlMask = ~0L; |
| 22431 | 22196 | static const char *zDQBSRO = "\"\\\x7f"; /* double-quote, backslash, rubout */ |
| 22432 | 22197 | char ace[3] = "\\?"; |
| 22433 | 22198 | char cbsSay; |
| 22434 | | - oputz(zq); |
| 22199 | + sqlite3_fputs(zq, out); |
| 22435 | 22200 | while( *z!=0 ){ |
| 22436 | 22201 | const char *pcDQBSRO = anyOfInStr(z, zDQBSRO, ~(size_t)0); |
| 22437 | 22202 | const char *pcPast = zSkipValidUtf8(z, INT_MAX, ctrlMask); |
| 22438 | 22203 | const char *pcEnd = (pcDQBSRO && pcDQBSRO < pcPast)? pcDQBSRO : pcPast; |
| 22439 | | - if( pcEnd > z ) oputb(z, (int)(pcEnd-z)); |
| 22204 | + if( pcEnd > z ){ |
| 22205 | + sqlite3_fprintf(out, "%.*s", (int)(pcEnd-z), z); |
| 22206 | + } |
| 22440 | 22207 | if( (c = *pcEnd)==0 ) break; |
| 22441 | 22208 | ++pcEnd; |
| 22442 | 22209 | switch( c ){ |
| 22443 | 22210 | case '\\': case '"': |
| 22444 | 22211 | cbsSay = (char)c; |
| | @@ -22449,47 +22216,47 @@ |
| 22449 | 22216 | case '\f': cbsSay = 'f'; break; |
| 22450 | 22217 | default: cbsSay = 0; break; |
| 22451 | 22218 | } |
| 22452 | 22219 | if( cbsSay ){ |
| 22453 | 22220 | ace[1] = cbsSay; |
| 22454 | | - oputz(ace); |
| 22221 | + sqlite3_fputs(ace, out); |
| 22455 | 22222 | }else if( !isprint(c&0xff) ){ |
| 22456 | | - oputf("\\%03o", c&0xff); |
| 22223 | + sqlite3_fprintf(out, "\\%03o", c&0xff); |
| 22457 | 22224 | }else{ |
| 22458 | 22225 | ace[1] = (char)c; |
| 22459 | | - oputz(ace+1); |
| 22226 | + sqlite3_fputs(ace+1, out); |
| 22460 | 22227 | } |
| 22461 | 22228 | z = pcEnd; |
| 22462 | 22229 | } |
| 22463 | | - oputz(zq); |
| 22230 | + sqlite3_fputs(zq, out); |
| 22464 | 22231 | } |
| 22465 | 22232 | |
| 22466 | 22233 | /* |
| 22467 | | -** Output the given string as a quoted according to JSON quoting rules. |
| 22234 | +** Output the given string as quoted according to JSON quoting rules. |
| 22468 | 22235 | */ |
| 22469 | | -static void output_json_string(const char *z, i64 n){ |
| 22470 | | - char c; |
| 22236 | +static void output_json_string(FILE *out, const char *z, i64 n){ |
| 22237 | + unsigned char c; |
| 22471 | 22238 | static const char *zq = "\""; |
| 22472 | 22239 | static long ctrlMask = ~0L; |
| 22473 | 22240 | static const char *zDQBS = "\"\\"; |
| 22474 | 22241 | const char *pcLimit; |
| 22475 | 22242 | char ace[3] = "\\?"; |
| 22476 | 22243 | char cbsSay; |
| 22477 | 22244 | |
| 22478 | 22245 | if( z==0 ) z = ""; |
| 22479 | 22246 | pcLimit = z + ((n<0)? strlen(z) : (size_t)n); |
| 22480 | | - oputz(zq); |
| 22247 | + sqlite3_fputs(zq, out); |
| 22481 | 22248 | while( z < pcLimit ){ |
| 22482 | 22249 | const char *pcDQBS = anyOfInStr(z, zDQBS, pcLimit-z); |
| 22483 | 22250 | const char *pcPast = zSkipValidUtf8(z, (int)(pcLimit-z), ctrlMask); |
| 22484 | 22251 | const char *pcEnd = (pcDQBS && pcDQBS < pcPast)? pcDQBS : pcPast; |
| 22485 | 22252 | if( pcEnd > z ){ |
| 22486 | | - oputb(z, (int)(pcEnd-z)); |
| 22253 | + sqlite3_fprintf(out, "%.*s", (int)(pcEnd-z), z); |
| 22487 | 22254 | z = pcEnd; |
| 22488 | 22255 | } |
| 22489 | 22256 | if( z >= pcLimit ) break; |
| 22490 | | - c = *(z++); |
| 22257 | + c = (unsigned char)*(z++); |
| 22491 | 22258 | switch( c ){ |
| 22492 | 22259 | case '"': case '\\': |
| 22493 | 22260 | cbsSay = (char)c; |
| 22494 | 22261 | break; |
| 22495 | 22262 | case '\b': cbsSay = 'b'; break; |
| | @@ -22499,26 +22266,26 @@ |
| 22499 | 22266 | case '\t': cbsSay = 't'; break; |
| 22500 | 22267 | default: cbsSay = 0; break; |
| 22501 | 22268 | } |
| 22502 | 22269 | if( cbsSay ){ |
| 22503 | 22270 | ace[1] = cbsSay; |
| 22504 | | - oputz(ace); |
| 22505 | | - }else if( c<=0x1f ){ |
| 22506 | | - oputf("u%04x", c); |
| 22271 | + sqlite3_fputs(ace, out); |
| 22272 | + }else if( c<=0x1f || c>=0x7f ){ |
| 22273 | + sqlite3_fprintf(out, "\\u%04x", c); |
| 22507 | 22274 | }else{ |
| 22508 | 22275 | ace[1] = (char)c; |
| 22509 | | - oputz(ace+1); |
| 22276 | + sqlite3_fputs(ace+1, out); |
| 22510 | 22277 | } |
| 22511 | 22278 | } |
| 22512 | | - oputz(zq); |
| 22279 | + sqlite3_fputs(zq, out); |
| 22513 | 22280 | } |
| 22514 | 22281 | |
| 22515 | 22282 | /* |
| 22516 | 22283 | ** Output the given string with characters that are special to |
| 22517 | 22284 | ** HTML escaped. |
| 22518 | 22285 | */ |
| 22519 | | -static void output_html_string(const char *z){ |
| 22286 | +static void output_html_string(FILE *out, const char *z){ |
| 22520 | 22287 | int i; |
| 22521 | 22288 | if( z==0 ) z = ""; |
| 22522 | 22289 | while( *z ){ |
| 22523 | 22290 | for(i=0; z[i] |
| 22524 | 22291 | && z[i]!='<' |
| | @@ -22526,22 +22293,22 @@ |
| 22526 | 22293 | && z[i]!='>' |
| 22527 | 22294 | && z[i]!='\"' |
| 22528 | 22295 | && z[i]!='\''; |
| 22529 | 22296 | i++){} |
| 22530 | 22297 | if( i>0 ){ |
| 22531 | | - oputf("%.*s",i,z); |
| 22298 | + sqlite3_fprintf(out, "%.*s",i,z); |
| 22532 | 22299 | } |
| 22533 | 22300 | if( z[i]=='<' ){ |
| 22534 | | - oputz("<"); |
| 22301 | + sqlite3_fputs("<", out); |
| 22535 | 22302 | }else if( z[i]=='&' ){ |
| 22536 | | - oputz("&"); |
| 22303 | + sqlite3_fputs("&", out); |
| 22537 | 22304 | }else if( z[i]=='>' ){ |
| 22538 | | - oputz(">"); |
| 22305 | + sqlite3_fputs(">", out); |
| 22539 | 22306 | }else if( z[i]=='\"' ){ |
| 22540 | | - oputz("""); |
| 22307 | + sqlite3_fputs(""", out); |
| 22541 | 22308 | }else if( z[i]=='\'' ){ |
| 22542 | | - oputz("'"); |
| 22309 | + sqlite3_fputs("'", out); |
| 22543 | 22310 | }else{ |
| 22544 | 22311 | break; |
| 22545 | 22312 | } |
| 22546 | 22313 | z += i + 1; |
| 22547 | 22314 | } |
| | @@ -22576,11 +22343,11 @@ |
| 22576 | 22343 | ** the null value. Strings are quoted if necessary. The separator |
| 22577 | 22344 | ** is only issued if bSep is true. |
| 22578 | 22345 | */ |
| 22579 | 22346 | static void output_csv(ShellState *p, const char *z, int bSep){ |
| 22580 | 22347 | if( z==0 ){ |
| 22581 | | - oputf("%s",p->nullValue); |
| 22348 | + sqlite3_fprintf(p->out, "%s",p->nullValue); |
| 22582 | 22349 | }else{ |
| 22583 | 22350 | unsigned i; |
| 22584 | 22351 | for(i=0; z[i]; i++){ |
| 22585 | 22352 | if( needCsvQuote[((unsigned char*)z)[i]] ){ |
| 22586 | 22353 | i = 0; |
| | @@ -22588,18 +22355,18 @@ |
| 22588 | 22355 | } |
| 22589 | 22356 | } |
| 22590 | 22357 | if( i==0 || strstr(z, p->colSeparator)!=0 ){ |
| 22591 | 22358 | char *zQuoted = sqlite3_mprintf("\"%w\"", z); |
| 22592 | 22359 | shell_check_oom(zQuoted); |
| 22593 | | - oputz(zQuoted); |
| 22360 | + sqlite3_fputs(zQuoted, p->out); |
| 22594 | 22361 | sqlite3_free(zQuoted); |
| 22595 | 22362 | }else{ |
| 22596 | | - oputz(z); |
| 22363 | + sqlite3_fputs(z, p->out); |
| 22597 | 22364 | } |
| 22598 | 22365 | } |
| 22599 | 22366 | if( bSep ){ |
| 22600 | | - oputz(p->colSeparator); |
| 22367 | + sqlite3_fputs(p->colSeparator, p->out); |
| 22601 | 22368 | } |
| 22602 | 22369 | } |
| 22603 | 22370 | |
| 22604 | 22371 | /* |
| 22605 | 22372 | ** This routine runs when the user presses Ctrl-C |
| | @@ -22703,20 +22470,20 @@ |
| 22703 | 22470 | const char *az[4]; |
| 22704 | 22471 | az[0] = zA1; |
| 22705 | 22472 | az[1] = zA2; |
| 22706 | 22473 | az[2] = zA3; |
| 22707 | 22474 | az[3] = zA4; |
| 22708 | | - oputf("authorizer: %s", azAction[op]); |
| 22475 | + sqlite3_fprintf(p->out, "authorizer: %s", azAction[op]); |
| 22709 | 22476 | for(i=0; i<4; i++){ |
| 22710 | | - oputz(" "); |
| 22477 | + sqlite3_fputs(" ", p->out); |
| 22711 | 22478 | if( az[i] ){ |
| 22712 | | - output_c_string(az[i]); |
| 22479 | + output_c_string(p->out, az[i]); |
| 22713 | 22480 | }else{ |
| 22714 | | - oputz("NULL"); |
| 22481 | + sqlite3_fputs("NULL", p->out); |
| 22715 | 22482 | } |
| 22716 | 22483 | } |
| 22717 | | - oputz("\n"); |
| 22484 | + sqlite3_fputs("\n", p->out); |
| 22718 | 22485 | if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4); |
| 22719 | 22486 | return SQLITE_OK; |
| 22720 | 22487 | } |
| 22721 | 22488 | #endif |
| 22722 | 22489 | |
| | @@ -22728,11 +22495,11 @@ |
| 22728 | 22495 | ** |
| 22729 | 22496 | ** If the schema statement in z[] contains a start-of-comment and if |
| 22730 | 22497 | ** sqlite3_complete() returns false, try to terminate the comment before |
| 22731 | 22498 | ** printing the result. https://sqlite.org/forum/forumpost/d7be961c5c |
| 22732 | 22499 | */ |
| 22733 | | -static void printSchemaLine(const char *z, const char *zTail){ |
| 22500 | +static void printSchemaLine(FILE *out, const char *z, const char *zTail){ |
| 22734 | 22501 | char *zToFree = 0; |
| 22735 | 22502 | if( z==0 ) return; |
| 22736 | 22503 | if( zTail==0 ) return; |
| 22737 | 22504 | if( zTail[0]==';' && (strstr(z, "/*")!=0 || strstr(z,"--")!=0) ){ |
| 22738 | 22505 | const char *zOrig = z; |
| | @@ -22750,20 +22517,20 @@ |
| 22750 | 22517 | } |
| 22751 | 22518 | sqlite3_free(zNew); |
| 22752 | 22519 | } |
| 22753 | 22520 | } |
| 22754 | 22521 | if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){ |
| 22755 | | - oputf("CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail); |
| 22522 | + sqlite3_fprintf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail); |
| 22756 | 22523 | }else{ |
| 22757 | | - oputf("%s%s", z, zTail); |
| 22524 | + sqlite3_fprintf(out, "%s%s", z, zTail); |
| 22758 | 22525 | } |
| 22759 | 22526 | sqlite3_free(zToFree); |
| 22760 | 22527 | } |
| 22761 | | -static void printSchemaLineN(char *z, int n, const char *zTail){ |
| 22528 | +static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){ |
| 22762 | 22529 | char c = z[n]; |
| 22763 | 22530 | z[n] = 0; |
| 22764 | | - printSchemaLine(z, zTail); |
| 22531 | + printSchemaLine(out, z, zTail); |
| 22765 | 22532 | z[n] = c; |
| 22766 | 22533 | } |
| 22767 | 22534 | |
| 22768 | 22535 | /* |
| 22769 | 22536 | ** Return true if string z[] has nothing but whitespace and comments to the |
| | @@ -22787,11 +22554,11 @@ |
| 22787 | 22554 | EQPGraphRow *pNew; |
| 22788 | 22555 | i64 nText; |
| 22789 | 22556 | if( zText==0 ) return; |
| 22790 | 22557 | nText = strlen(zText); |
| 22791 | 22558 | if( p->autoEQPtest ){ |
| 22792 | | - oputf("%d,%d,%s\n", iEqpId, p2, zText); |
| 22559 | + sqlite3_fprintf(p->out, "%d,%d,%s\n", iEqpId, p2, zText); |
| 22793 | 22560 | } |
| 22794 | 22561 | pNew = sqlite3_malloc64( sizeof(*pNew) + nText ); |
| 22795 | 22562 | shell_check_oom(pNew); |
| 22796 | 22563 | pNew->iEqpId = iEqpId; |
| 22797 | 22564 | pNew->iParentId = p2; |
| | @@ -22835,11 +22602,12 @@ |
| 22835 | 22602 | i64 n = strlen(p->sGraph.zPrefix); |
| 22836 | 22603 | char *z; |
| 22837 | 22604 | for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){ |
| 22838 | 22605 | pNext = eqp_next_row(p, iEqpId, pRow); |
| 22839 | 22606 | z = pRow->zText; |
| 22840 | | - oputf("%s%s%s\n", p->sGraph.zPrefix, pNext ? "|--" : "`--", z); |
| 22607 | + sqlite3_fprintf(p->out, "%s%s%s\n", p->sGraph.zPrefix, |
| 22608 | + pNext ? "|--" : "`--", z); |
| 22841 | 22609 | if( n<(i64)sizeof(p->sGraph.zPrefix)-7 ){ |
| 22842 | 22610 | memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4); |
| 22843 | 22611 | eqp_render_level(p, pRow->iEqpId); |
| 22844 | 22612 | p->sGraph.zPrefix[n] = 0; |
| 22845 | 22613 | } |
| | @@ -22855,17 +22623,17 @@ |
| 22855 | 22623 | if( pRow->zText[0]=='-' ){ |
| 22856 | 22624 | if( pRow->pNext==0 ){ |
| 22857 | 22625 | eqp_reset(p); |
| 22858 | 22626 | return; |
| 22859 | 22627 | } |
| 22860 | | - oputf("%s\n", pRow->zText+3); |
| 22628 | + sqlite3_fprintf(p->out, "%s\n", pRow->zText+3); |
| 22861 | 22629 | p->sGraph.pRow = pRow->pNext; |
| 22862 | 22630 | sqlite3_free(pRow); |
| 22863 | 22631 | }else if( nCycle>0 ){ |
| 22864 | | - oputf("QUERY PLAN (cycles=%lld [100%%])\n", nCycle); |
| 22632 | + sqlite3_fprintf(p->out, "QUERY PLAN (cycles=%lld [100%%])\n", nCycle); |
| 22865 | 22633 | }else{ |
| 22866 | | - oputz("QUERY PLAN\n"); |
| 22634 | + sqlite3_fputs("QUERY PLAN\n", p->out); |
| 22867 | 22635 | } |
| 22868 | 22636 | p->sGraph.zPrefix[0] = 0; |
| 22869 | 22637 | eqp_render_level(p, 0); |
| 22870 | 22638 | eqp_reset(p); |
| 22871 | 22639 | } |
| | @@ -22877,33 +22645,33 @@ |
| 22877 | 22645 | */ |
| 22878 | 22646 | static int progress_handler(void *pClientData) { |
| 22879 | 22647 | ShellState *p = (ShellState*)pClientData; |
| 22880 | 22648 | p->nProgress++; |
| 22881 | 22649 | if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){ |
| 22882 | | - oputf("Progress limit reached (%u)\n", p->nProgress); |
| 22650 | + sqlite3_fprintf(p->out, "Progress limit reached (%u)\n", p->nProgress); |
| 22883 | 22651 | if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0; |
| 22884 | 22652 | if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0; |
| 22885 | 22653 | return 1; |
| 22886 | 22654 | } |
| 22887 | 22655 | if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){ |
| 22888 | | - oputf("Progress %u\n", p->nProgress); |
| 22656 | + sqlite3_fprintf(p->out, "Progress %u\n", p->nProgress); |
| 22889 | 22657 | } |
| 22890 | 22658 | return 0; |
| 22891 | 22659 | } |
| 22892 | 22660 | #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */ |
| 22893 | 22661 | |
| 22894 | 22662 | /* |
| 22895 | 22663 | ** Print N dashes |
| 22896 | 22664 | */ |
| 22897 | | -static void print_dashes(int N){ |
| 22665 | +static void print_dashes(FILE *out, int N){ |
| 22898 | 22666 | const char zDash[] = "--------------------------------------------------"; |
| 22899 | 22667 | const int nDash = sizeof(zDash) - 1; |
| 22900 | 22668 | while( N>nDash ){ |
| 22901 | | - oputz(zDash); |
| 22669 | + sqlite3_fputs(zDash, out); |
| 22902 | 22670 | N -= nDash; |
| 22903 | 22671 | } |
| 22904 | | - oputf("%.*s", N, zDash); |
| 22672 | + sqlite3_fprintf(out, "%.*s", N, zDash); |
| 22905 | 22673 | } |
| 22906 | 22674 | |
| 22907 | 22675 | /* |
| 22908 | 22676 | ** Print a markdown or table-style row separator using ascii-art |
| 22909 | 22677 | */ |
| | @@ -22912,19 +22680,19 @@ |
| 22912 | 22680 | int nArg, |
| 22913 | 22681 | const char *zSep |
| 22914 | 22682 | ){ |
| 22915 | 22683 | int i; |
| 22916 | 22684 | if( nArg>0 ){ |
| 22917 | | - oputz(zSep); |
| 22918 | | - print_dashes(p->actualWidth[0]+2); |
| 22685 | + sqlite3_fputs(zSep, p->out); |
| 22686 | + print_dashes(p->out, p->actualWidth[0]+2); |
| 22919 | 22687 | for(i=1; i<nArg; i++){ |
| 22920 | | - oputz(zSep); |
| 22921 | | - print_dashes(p->actualWidth[i]+2); |
| 22688 | + sqlite3_fputs(zSep, p->out); |
| 22689 | + print_dashes(p->out, p->actualWidth[i]+2); |
| 22922 | 22690 | } |
| 22923 | | - oputz(zSep); |
| 22691 | + sqlite3_fputs(zSep, p->out); |
| 22924 | 22692 | } |
| 22925 | | - oputz("\n"); |
| 22693 | + sqlite3_fputs("\n", p->out); |
| 22926 | 22694 | } |
| 22927 | 22695 | |
| 22928 | 22696 | /* |
| 22929 | 22697 | ** This is the callback routine that the shell |
| 22930 | 22698 | ** invokes for each row of a query result. |
| | @@ -22950,13 +22718,13 @@ |
| 22950 | 22718 | if( azArg==0 ) break; |
| 22951 | 22719 | for(i=0; i<nArg; i++){ |
| 22952 | 22720 | int len = strlen30(azCol[i] ? azCol[i] : ""); |
| 22953 | 22721 | if( len>w ) w = len; |
| 22954 | 22722 | } |
| 22955 | | - if( p->cnt++>0 ) oputz(p->rowSeparator); |
| 22723 | + if( p->cnt++>0 ) sqlite3_fputs(p->rowSeparator, p->out); |
| 22956 | 22724 | for(i=0; i<nArg; i++){ |
| 22957 | | - oputf("%*s = %s%s", w, azCol[i], |
| 22725 | + sqlite3_fprintf(p->out, "%*s = %s%s", w, azCol[i], |
| 22958 | 22726 | azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator); |
| 22959 | 22727 | } |
| 22960 | 22728 | break; |
| 22961 | 22729 | } |
| 22962 | 22730 | case MODE_ScanExp: |
| | @@ -22980,16 +22748,16 @@ |
| 22980 | 22748 | if( nArg>nWidth ) nArg = nWidth; |
| 22981 | 22749 | |
| 22982 | 22750 | /* If this is the first row seen, print out the headers */ |
| 22983 | 22751 | if( p->cnt++==0 ){ |
| 22984 | 22752 | for(i=0; i<nArg; i++){ |
| 22985 | | - utf8_width_print(aWidth[i], azCol[ aMap[i] ]); |
| 22986 | | - oputz(i==nArg-1 ? "\n" : " "); |
| 22753 | + utf8_width_print(p->out, aWidth[i], azCol[ aMap[i] ]); |
| 22754 | + sqlite3_fputs(i==nArg-1 ? "\n" : " ", p->out); |
| 22987 | 22755 | } |
| 22988 | 22756 | for(i=0; i<nArg; i++){ |
| 22989 | | - print_dashes(aWidth[i]); |
| 22990 | | - oputz(i==nArg-1 ? "\n" : " "); |
| 22757 | + print_dashes(p->out, aWidth[i]); |
| 22758 | + sqlite3_fputs(i==nArg-1 ? "\n" : " ", p->out); |
| 22991 | 22759 | } |
| 22992 | 22760 | } |
| 22993 | 22761 | |
| 22994 | 22762 | /* If there is no data, exit early. */ |
| 22995 | 22763 | if( azArg==0 ) break; |
| | @@ -23003,21 +22771,21 @@ |
| 23003 | 22771 | w = strlenChar(zVal); |
| 23004 | 22772 | zSep = " "; |
| 23005 | 22773 | } |
| 23006 | 22774 | if( i==iIndent && p->aiIndent && p->pStmt ){ |
| 23007 | 22775 | if( p->iIndent<p->nIndent ){ |
| 23008 | | - oputf("%*.s", p->aiIndent[p->iIndent], ""); |
| 22776 | + sqlite3_fprintf(p->out, "%*.s", p->aiIndent[p->iIndent], ""); |
| 23009 | 22777 | } |
| 23010 | 22778 | p->iIndent++; |
| 23011 | 22779 | } |
| 23012 | | - utf8_width_print(w, zVal ? zVal : p->nullValue); |
| 23013 | | - oputz(i==nArg-1 ? "\n" : zSep); |
| 22780 | + utf8_width_print(p->out, w, zVal ? zVal : p->nullValue); |
| 22781 | + sqlite3_fputs(i==nArg-1 ? "\n" : zSep, p->out); |
| 23014 | 22782 | } |
| 23015 | 22783 | break; |
| 23016 | 22784 | } |
| 23017 | 22785 | case MODE_Semi: { /* .schema and .fullschema output */ |
| 23018 | | - printSchemaLine(azArg[0], ";\n"); |
| 22786 | + printSchemaLine(p->out, azArg[0], ";\n"); |
| 23019 | 22787 | break; |
| 23020 | 22788 | } |
| 23021 | 22789 | case MODE_Pretty: { /* .schema and .fullschema with --indent */ |
| 23022 | 22790 | char *z; |
| 23023 | 22791 | int j; |
| | @@ -23028,11 +22796,11 @@ |
| 23028 | 22796 | assert( nArg==1 ); |
| 23029 | 22797 | if( azArg[0]==0 ) break; |
| 23030 | 22798 | if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0 |
| 23031 | 22799 | || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0 |
| 23032 | 22800 | ){ |
| 23033 | | - oputf("%s;\n", azArg[0]); |
| 22801 | + sqlite3_fprintf(p->out, "%s;\n", azArg[0]); |
| 23034 | 22802 | break; |
| 23035 | 22803 | } |
| 23036 | 22804 | z = sqlite3_mprintf("%s", azArg[0]); |
| 23037 | 22805 | shell_check_oom(z); |
| 23038 | 22806 | j = 0; |
| | @@ -23061,255 +22829,265 @@ |
| 23061 | 22829 | }else if( c=='(' ){ |
| 23062 | 22830 | nParen++; |
| 23063 | 22831 | }else if( c==')' ){ |
| 23064 | 22832 | nParen--; |
| 23065 | 22833 | if( nLine>0 && nParen==0 && j>0 ){ |
| 23066 | | - printSchemaLineN(z, j, "\n"); |
| 22834 | + printSchemaLineN(p->out, z, j, "\n"); |
| 23067 | 22835 | j = 0; |
| 23068 | 22836 | } |
| 23069 | 22837 | } |
| 23070 | 22838 | z[j++] = c; |
| 23071 | 22839 | if( nParen==1 && cEnd==0 |
| 23072 | 22840 | && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1))) |
| 23073 | 22841 | ){ |
| 23074 | 22842 | if( c=='\n' ) j--; |
| 23075 | | - printSchemaLineN(z, j, "\n "); |
| 22843 | + printSchemaLineN(p->out, z, j, "\n "); |
| 23076 | 22844 | j = 0; |
| 23077 | 22845 | nLine++; |
| 23078 | 22846 | while( IsSpace(z[i+1]) ){ i++; } |
| 23079 | 22847 | } |
| 23080 | 22848 | } |
| 23081 | 22849 | z[j] = 0; |
| 23082 | 22850 | } |
| 23083 | | - printSchemaLine(z, ";\n"); |
| 22851 | + printSchemaLine(p->out, z, ";\n"); |
| 23084 | 22852 | sqlite3_free(z); |
| 23085 | 22853 | break; |
| 23086 | 22854 | } |
| 23087 | 22855 | case MODE_List: { |
| 23088 | 22856 | if( p->cnt++==0 && p->showHeader ){ |
| 23089 | 22857 | for(i=0; i<nArg; i++){ |
| 23090 | | - oputf("%s%s",azCol[i], i==nArg-1 ? p->rowSeparator : p->colSeparator); |
| 22858 | + sqlite3_fprintf(p->out, "%s%s", azCol[i], |
| 22859 | + i==nArg-1 ? p->rowSeparator : p->colSeparator); |
| 23091 | 22860 | } |
| 23092 | 22861 | } |
| 23093 | 22862 | if( azArg==0 ) break; |
| 23094 | 22863 | for(i=0; i<nArg; i++){ |
| 23095 | 22864 | char *z = azArg[i]; |
| 23096 | 22865 | if( z==0 ) z = p->nullValue; |
| 23097 | | - oputz(z); |
| 23098 | | - oputz((i<nArg-1)? p->colSeparator : p->rowSeparator); |
| 22866 | + sqlite3_fputs(z, p->out); |
| 22867 | + sqlite3_fputs((i<nArg-1)? p->colSeparator : p->rowSeparator, p->out); |
| 23099 | 22868 | } |
| 23100 | 22869 | break; |
| 23101 | 22870 | } |
| 22871 | + case MODE_Www: |
| 23102 | 22872 | case MODE_Html: { |
| 23103 | | - if( p->cnt++==0 && p->showHeader ){ |
| 23104 | | - oputz("<TR>"); |
| 22873 | + if( p->cnt==0 && p->cMode==MODE_Www ){ |
| 22874 | + sqlite3_fputs( |
| 22875 | + "</PRE>\n" |
| 22876 | + "<TABLE border='1' cellspacing='0' cellpadding='2'>\n" |
| 22877 | + ,p->out |
| 22878 | + ); |
| 22879 | + } |
| 22880 | + if( p->cnt==0 && (p->showHeader || p->cMode==MODE_Www) ){ |
| 22881 | + sqlite3_fputs("<TR>", p->out); |
| 23105 | 22882 | for(i=0; i<nArg; i++){ |
| 23106 | | - oputz("<TH>"); |
| 23107 | | - output_html_string(azCol[i]); |
| 23108 | | - oputz("</TH>\n"); |
| 22883 | + sqlite3_fputs("<TH>", p->out); |
| 22884 | + output_html_string(p->out, azCol[i]); |
| 22885 | + sqlite3_fputs("</TH>\n", p->out); |
| 23109 | 22886 | } |
| 23110 | | - oputz("</TR>\n"); |
| 22887 | + sqlite3_fputs("</TR>\n", p->out); |
| 23111 | 22888 | } |
| 22889 | + p->cnt++; |
| 23112 | 22890 | if( azArg==0 ) break; |
| 23113 | | - oputz("<TR>"); |
| 22891 | + sqlite3_fputs("<TR>", p->out); |
| 23114 | 22892 | for(i=0; i<nArg; i++){ |
| 23115 | | - oputz("<TD>"); |
| 23116 | | - output_html_string(azArg[i] ? azArg[i] : p->nullValue); |
| 23117 | | - oputz("</TD>\n"); |
| 22893 | + sqlite3_fputs("<TD>", p->out); |
| 22894 | + output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue); |
| 22895 | + sqlite3_fputs("</TD>\n", p->out); |
| 23118 | 22896 | } |
| 23119 | | - oputz("</TR>\n"); |
| 22897 | + sqlite3_fputs("</TR>\n", p->out); |
| 23120 | 22898 | break; |
| 23121 | 22899 | } |
| 23122 | 22900 | case MODE_Tcl: { |
| 23123 | 22901 | if( p->cnt++==0 && p->showHeader ){ |
| 23124 | 22902 | for(i=0; i<nArg; i++){ |
| 23125 | | - output_c_string(azCol[i] ? azCol[i] : ""); |
| 23126 | | - if(i<nArg-1) oputz(p->colSeparator); |
| 22903 | + output_c_string(p->out, azCol[i] ? azCol[i] : ""); |
| 22904 | + if(i<nArg-1) sqlite3_fputs(p->colSeparator, p->out); |
| 23127 | 22905 | } |
| 23128 | | - oputz(p->rowSeparator); |
| 22906 | + sqlite3_fputs(p->rowSeparator, p->out); |
| 23129 | 22907 | } |
| 23130 | 22908 | if( azArg==0 ) break; |
| 23131 | 22909 | for(i=0; i<nArg; i++){ |
| 23132 | | - output_c_string(azArg[i] ? azArg[i] : p->nullValue); |
| 23133 | | - if(i<nArg-1) oputz(p->colSeparator); |
| 22910 | + output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue); |
| 22911 | + if(i<nArg-1) sqlite3_fputs(p->colSeparator, p->out); |
| 23134 | 22912 | } |
| 23135 | | - oputz(p->rowSeparator); |
| 22913 | + sqlite3_fputs(p->rowSeparator, p->out); |
| 23136 | 22914 | break; |
| 23137 | 22915 | } |
| 23138 | 22916 | case MODE_Csv: { |
| 23139 | | - setBinaryMode(p->out, 1); |
| 22917 | + sqlite3_fsetmode(p->out, _O_BINARY); |
| 23140 | 22918 | if( p->cnt++==0 && p->showHeader ){ |
| 23141 | 22919 | for(i=0; i<nArg; i++){ |
| 23142 | 22920 | output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1); |
| 23143 | 22921 | } |
| 23144 | | - oputz(p->rowSeparator); |
| 22922 | + sqlite3_fputs(p->rowSeparator, p->out); |
| 23145 | 22923 | } |
| 23146 | 22924 | if( nArg>0 ){ |
| 23147 | 22925 | for(i=0; i<nArg; i++){ |
| 23148 | 22926 | output_csv(p, azArg[i], i<nArg-1); |
| 23149 | 22927 | } |
| 23150 | | - oputz(p->rowSeparator); |
| 22928 | + sqlite3_fputs(p->rowSeparator, p->out); |
| 23151 | 22929 | } |
| 23152 | | - setTextMode(p->out, 1); |
| 22930 | + setCrlfMode(p); |
| 23153 | 22931 | break; |
| 23154 | 22932 | } |
| 23155 | 22933 | case MODE_Insert: { |
| 23156 | 22934 | if( azArg==0 ) break; |
| 23157 | | - oputf("INSERT INTO %s",p->zDestTable); |
| 22935 | + sqlite3_fprintf(p->out, "INSERT INTO %s",p->zDestTable); |
| 23158 | 22936 | if( p->showHeader ){ |
| 23159 | | - oputz("("); |
| 22937 | + sqlite3_fputs("(", p->out); |
| 23160 | 22938 | for(i=0; i<nArg; i++){ |
| 23161 | | - if( i>0 ) oputz(","); |
| 22939 | + if( i>0 ) sqlite3_fputs(",", p->out); |
| 23162 | 22940 | if( quoteChar(azCol[i]) ){ |
| 23163 | 22941 | char *z = sqlite3_mprintf("\"%w\"", azCol[i]); |
| 23164 | 22942 | shell_check_oom(z); |
| 23165 | | - oputz(z); |
| 22943 | + sqlite3_fputs(z, p->out); |
| 23166 | 22944 | sqlite3_free(z); |
| 23167 | 22945 | }else{ |
| 23168 | | - oputf("%s", azCol[i]); |
| 22946 | + sqlite3_fprintf(p->out, "%s", azCol[i]); |
| 23169 | 22947 | } |
| 23170 | 22948 | } |
| 23171 | | - oputz(")"); |
| 22949 | + sqlite3_fputs(")", p->out); |
| 23172 | 22950 | } |
| 23173 | 22951 | p->cnt++; |
| 23174 | 22952 | for(i=0; i<nArg; i++){ |
| 23175 | | - oputz(i>0 ? "," : " VALUES("); |
| 22953 | + sqlite3_fputs(i>0 ? "," : " VALUES(", p->out); |
| 23176 | 22954 | if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ |
| 23177 | | - oputz("NULL"); |
| 22955 | + sqlite3_fputs("NULL", p->out); |
| 23178 | 22956 | }else if( aiType && aiType[i]==SQLITE_TEXT ){ |
| 23179 | 22957 | if( ShellHasFlag(p, SHFLG_Newlines) ){ |
| 23180 | | - output_quoted_string(azArg[i]); |
| 22958 | + output_quoted_string(p, azArg[i]); |
| 23181 | 22959 | }else{ |
| 23182 | | - output_quoted_escaped_string(azArg[i]); |
| 22960 | + output_quoted_escaped_string(p, azArg[i]); |
| 23183 | 22961 | } |
| 23184 | 22962 | }else if( aiType && aiType[i]==SQLITE_INTEGER ){ |
| 23185 | | - oputz(azArg[i]); |
| 22963 | + sqlite3_fputs(azArg[i], p->out); |
| 23186 | 22964 | }else if( aiType && aiType[i]==SQLITE_FLOAT ){ |
| 23187 | 22965 | char z[50]; |
| 23188 | 22966 | double r = sqlite3_column_double(p->pStmt, i); |
| 23189 | 22967 | sqlite3_uint64 ur; |
| 23190 | 22968 | memcpy(&ur,&r,sizeof(r)); |
| 23191 | 22969 | if( ur==0x7ff0000000000000LL ){ |
| 23192 | | - oputz("9.0e+999"); |
| 22970 | + sqlite3_fputs("9.0e+999", p->out); |
| 23193 | 22971 | }else if( ur==0xfff0000000000000LL ){ |
| 23194 | | - oputz("-9.0e+999"); |
| 22972 | + sqlite3_fputs("-9.0e+999", p->out); |
| 23195 | 22973 | }else{ |
| 23196 | 22974 | sqlite3_int64 ir = (sqlite3_int64)r; |
| 23197 | 22975 | if( r==(double)ir ){ |
| 23198 | 22976 | sqlite3_snprintf(50,z,"%lld.0", ir); |
| 23199 | 22977 | }else{ |
| 23200 | 22978 | sqlite3_snprintf(50,z,"%!.20g", r); |
| 23201 | 22979 | } |
| 23202 | | - oputz(z); |
| 22980 | + sqlite3_fputs(z, p->out); |
| 23203 | 22981 | } |
| 23204 | 22982 | }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ |
| 23205 | 22983 | const void *pBlob = sqlite3_column_blob(p->pStmt, i); |
| 23206 | 22984 | int nBlob = sqlite3_column_bytes(p->pStmt, i); |
| 23207 | | - output_hex_blob(pBlob, nBlob); |
| 22985 | + output_hex_blob(p->out, pBlob, nBlob); |
| 23208 | 22986 | }else if( isNumber(azArg[i], 0) ){ |
| 23209 | | - oputz(azArg[i]); |
| 22987 | + sqlite3_fputs(azArg[i], p->out); |
| 23210 | 22988 | }else if( ShellHasFlag(p, SHFLG_Newlines) ){ |
| 23211 | | - output_quoted_string(azArg[i]); |
| 22989 | + output_quoted_string(p, azArg[i]); |
| 23212 | 22990 | }else{ |
| 23213 | | - output_quoted_escaped_string(azArg[i]); |
| 22991 | <