604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
|
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
|
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
-
-
-
-
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
-
-
-
-
+
+
+
+
+
+
+
+
+
-
-
+
-
+
-
+
+
+
-
-
-
+
+
+
-
-
-
+
+
+
-
+
-
+
+
-
+
-
+
-
+
|
}
}
return dynPrompt.dynamicPrompt;
}
#endif /* !defined(SQLITE_OMIT_DYNAPROMPT) */
#if SHELL_WIN_UTF8_OPT
/* Following struct is used for -utf8 operation. */
/* Following struct is used for UTF-8 operation. */
static struct ConsoleState {
int stdinEof; /* EOF has been seen on console input */
int infsMode; /* Input file stream mode upon shell start */
UINT inCodePage; /* Input code page upon shell start */
UINT outCodePage; /* Output code page upon shell start */
HANDLE hConsoleIn; /* Console input handle */
HANDLE hConsole; /* Console input or output handle */
DWORD consoleMode; /* Console mode upon shell start */
} conState = { 0, 0, 0, 0, INVALID_HANDLE_VALUE, 0 };
#ifndef _O_U16TEXT /* For build environments lacking this constant: */
# define _O_U16TEXT 0x20000
#endif
#if !SQLITE_OS_WINRT
/*
** Check Windows major version against given value, returning
** 1 if the OS major version is no less than the argument.
** This check uses very late binding to the registry access
** API so that it can operate gracefully on OS versions that
** do not have that API. The Windows NT registry, for versions
** through Windows 11 (at least, as of October 2023), keeps
** the actual major version number at registry key/value
** HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\CurrentMajorVersionNumber
** where it can be read more reliably than allowed by various
** version info APIs which "process" the result in a manner
** incompatible with the purpose of the CLI's version check.
**
** If the registry API is unavailable, or the location of
** the above registry value changes, or the OS major version
** is less than the argument, this function returns 0.
*/
static int CheckAtLeastWinX(DWORD major_version){
typedef LONG (WINAPI *REG_OPEN)(HKEY,LPCSTR,DWORD,REGSAM,PHKEY);
typedef LSTATUS (WINAPI *REG_READ)(HKEY,LPCSTR,LPCSTR,DWORD,
LPDWORD,PVOID,LPDWORD);
typedef LSTATUS (WINAPI *REG_CLOSE)(HKEY);
int rv = 0;
HINSTANCE hLib = LoadLibrary(TEXT("Advapi32.dll"));
if( NULL != hLib ){
REG_OPEN rkOpen = (REG_OPEN)GetProcAddress(hLib, "RegOpenKeyExA");
REG_READ rkRead = (REG_READ)GetProcAddress(hLib, "RegGetValueA");
REG_CLOSE rkFree = (REG_CLOSE)GetProcAddress(hLib, "RegCloseKey");
if( rkOpen != NULL && rkRead != NULL && rkFree != NULL ){
HKEY hk;
const char *zsk = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion";
if( ERROR_SUCCESS == rkOpen(HKEY_LOCAL_MACHINE, zsk, 0, KEY_READ, &hk) ){
DWORD kv = 0, kvsize = sizeof(kv);
if( ERROR_SUCCESS == rkRead(hk, 0, "CurrentMajorVersionNumber",
RRF_RT_REG_DWORD, 0, &kv, &kvsize) ){
rv = (kv >= major_version);
}
rkFree(hk);
}
}
FreeLibrary(hLib);
}
return rv;
}
# define IS_WIN10_OR_LATER() CheckAtLeastWinX(10)
#else /* defined(SQLITE_OS_WINRT) */
# define IS_WIN10_OR_LATER() 0
#endif
/*
** Prepare console, (if known to be a WIN32 console), for UTF-8
** input (from either typing or suitable paste operations) and for
** UTF-8 rendering. This may "fail" with a message to stderr, where
** Prepare console, (if known to be a WIN32 console), for UTF-8 input
** (from either typing or suitable paste operations) and/or for UTF-8
** output rendering. This may "fail" with a message to stderr, where
** the preparation is not done and common "code page" issues occur.
**
** The console state upon entry is preserved, in conState, so that
** console_restore() can later restore the same console state.
**
** The globals console_utf8_in and console_utf8_out are set, for
** later use in selecting UTF-8 or MBCS console I/O translations.
*/
static void console_prepare(void){
static void console_prepare_utf8(void){
HANDLE hCI = GetStdHandle(STD_INPUT_HANDLE);
HANDLE hCO = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE hCC = INVALID_HANDLE_VALUE;
DWORD consoleMode = 0;
u8 conI = 0, conO = 0;
struct ConsoleState csWork = { 0, 0, 0, 0, INVALID_HANDLE_VALUE, 0 };
console_utf8_in = console_utf8_out = 0;
if( isatty(0) && GetFileType(hCI)==FILE_TYPE_CHAR
&& GetConsoleMode( hCI, &consoleMode) ){
if( !IsValidCodePage(CP_UTF8) ){
fprintf(stderr, "Cannot use UTF-8 code page.\n");
if( isatty(0) && GetFileType(hCI)==FILE_TYPE_CHAR ) conI = 1;
if( isatty(1) && GetFileType(hCO)==FILE_TYPE_CHAR ) conO = 1;
if( (!conI && !conO) || mbcs_opted ) return;
if( conI ) hCC = hCI;
else hCC = hCO;
if( !IsValidCodePage(CP_UTF8) || !GetConsoleMode( hCC, &consoleMode) ){
bail:
fprintf(stderr, "Cannot use UTF-8 code page.\n");
console_utf8 = 0;
return;
}
conState.hConsoleIn = hCI;
conState.consoleMode = consoleMode;
conState.inCodePage = GetConsoleCP();
conState.outCodePage = GetConsoleOutputCP();
SetConsoleCP(CP_UTF8);
return;
}
csWork.hConsole = hCC;
csWork.consoleMode = consoleMode;
csWork.inCodePage = GetConsoleCP();
csWork.outCodePage = GetConsoleOutputCP();
if( conI ){
if( !SetConsoleCP(CP_UTF8) ) goto bail;
SetConsoleOutputCP(CP_UTF8);
consoleMode |= ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT;
SetConsoleMode(conState.hConsoleIn, consoleMode);
conState.infsMode = _setmode(_fileno(stdin), _O_U16TEXT);
console_utf8 = 1;
SetConsoleMode(conState.hConsole, consoleMode);
csWork.infsMode = _setmode(_fileno(stdin), _O_U16TEXT);
}
if( conO ){
/* Here, it is assumed that if conI is true, this call will also
** succeed, so there is no need to undo above setup upon failure. */
if( !SetConsoleOutputCP(CP_UTF8) ) goto bail;
}
console_utf8_in = conI;
}else{
console_utf8 = 0;
console_utf8_out = conO;
}
conState = csWork;
}
/*
** Undo the effects of console_prepare(), if any.
** Undo the effects of console_prepare_utf8(), if any.
*/
static void SQLITE_CDECL console_restore(void){
if( (console_utf8_in||console_utf8_out)
&& conState.hConsole!=INVALID_HANDLE_VALUE ){
if( console_utf8 && conState.inCodePage!=0
&& conState.hConsoleIn!=INVALID_HANDLE_VALUE ){
_setmode(_fileno(stdin), conState.infsMode);
if( console_utf8_in ){
SetConsoleCP(conState.inCodePage);
_setmode(_fileno(stdin), conState.infsMode);
SetConsoleCP(conState.inCodePage);
SetConsoleOutputCP(conState.outCodePage);
SetConsoleMode(conState.hConsoleIn, conState.consoleMode);
}
if( console_utf8_out ) SetConsoleOutputCP(conState.outCodePage);
SetConsoleMode(conState.hConsole, conState.consoleMode);
/* Avoid multiple calls. */
conState.hConsoleIn = INVALID_HANDLE_VALUE;
conState.hConsole = INVALID_HANDLE_VALUE;
conState.consoleMode = 0;
console_utf8 = 0;
console_utf8_in = 0;
console_utf8_out = 0;
}
}
/*
** Collect input like fgets(...) with special provisions for input
** from the Windows console to get around its strange coding issues.
** Defers to plain fgets() when input is not interactive or when the
** startup option, -utf8, has not been provided or taken effect.
** UTF-8 input is unavailable or opted out.
*/
static char* utf8_fgets(char *buf, int ncmax, FILE *fin){
if( fin==0 ) fin = stdin;
if( fin==stdin && stdin_is_interactive && console_utf8 ){
if( fin==stdin && stdin_is_interactive && console_utf8_in ){
# define SQLITE_IALIM 150
wchar_t wbuf[SQLITE_IALIM];
int lend = 0;
int noc = 0;
if( ncmax==0 || conState.stdinEof ) return 0;
buf[0] = 0;
while( noc<ncmax-7-1 && !lend ){
/* There is room for at least 2 more characters and a 0-terminator. */
int na = (ncmax > SQLITE_IALIM*4+1 + noc)
? SQLITE_IALIM : (ncmax-1 - noc)/4;
# undef SQLITE_IALIM
DWORD nbr = 0;
BOOL bRC = ReadConsoleW(conState.hConsoleIn, wbuf, na, &nbr, 0);
BOOL bRC = ReadConsoleW(conState.hConsole, wbuf, na, &nbr, 0);
if( !bRC || (noc==0 && nbr==0) ) return 0;
if( nbr > 0 ){
int nmb = WideCharToMultiByte(CP_UTF8,WC_COMPOSITECHECK|WC_DEFAULTCHAR,
wbuf,nbr,0,0,0,0);
if( nmb !=0 && noc+nmb <= ncmax ){
int iseg = noc;
nmb = WideCharToMultiByte(CP_UTF8,WC_COMPOSITECHECK|WC_DEFAULTCHAR,
|
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
|
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
|
-
-
+
+
-
+
-
+
|
}
# define fgets(b,n,f) utf8_fgets(b,n,f)
#endif /* SHELL_WIN_UTF8_OPT */
/*
** Render output like fprintf(). Except, if the output is going to the
** console and if this is running on a Windows machine, and if the -utf8
** option is unavailable or (available and inactive), translate the
** console and if this is running on a Windows machine, and if UTF-8
** output unavailable (or available but opted out), translate the
** output from UTF-8 into MBCS for output through 8-bit stdout stream.
** (With -utf8 active, no translation is needed and must not be done.)
** (Without -no-utf8, no translation is needed and must not be done.)
*/
#if defined(_WIN32) || defined(WIN32)
void utf8_printf(FILE *out, const char *zFormat, ...){
va_list ap;
va_start(ap, zFormat);
if( stdout_is_console && (out==stdout || out==stderr) && !console_utf8 ){
if( stdout_is_console && (out==stdout || out==stderr) && !console_utf8_out ){
char *z1 = sqlite3_vmprintf(zFormat, ap);
char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
sqlite3_free(z1);
fputs(z2, out);
sqlite3_free(z2);
}else{
vfprintf(out, zFormat, ap);
|
28111
28112
28113
28114
28115
28116
28117
28118
28119
28120
28121
28122
28123
28124
28125
28126
28127
28128
28129
28130
|
28205
28206
28207
28208
28209
28210
28211
28212
28213
28214
28215
28216
28217
28218
28219
28220
28221
28222
28223
28224
28225
28226
28227
28228
28229
28230
28231
28232
28233
28234
28235
28236
28237
28238
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
|| cli_strcmp(z,"-nullvalue")==0
|| cli_strcmp(z,"-newline")==0
|| cli_strcmp(z,"-cmd")==0
){
(void)cmdline_option_value(argc, argv, ++i);
}else if( cli_strcmp(z,"-init")==0 ){
zInitFile = cmdline_option_value(argc, argv, ++i);
}else if( cli_strcmp(z,"-interactive")==0 ){
/* Need to check for interactive override here to so that it can
** affect console setup (for Windows only) and testing thereof.
*/
stdin_is_interactive = 1;
}else if( cli_strcmp(z,"-batch")==0 ){
/* Need to check for batch mode here to so we can avoid printing
** informational messages (like from process_sqliterc) before
** we do the actual processing of arguments later in a second pass.
*/
stdin_is_interactive = 0;
}else if( cli_strcmp(z,"-utf8")==0 ){
#if SHELL_WIN_UTF8_OPT
/* Option accepted, but just specifies default UTF-8 console I/O. */
mbcs_opted = 0;
#endif /* SHELL_WIN_UTF8_OPT */
}else if( cli_strcmp(z,"-no-utf8")==0 ){
#if SHELL_WIN_UTF8_OPT
mbcs_opted = 1;
#endif /* SHELL_WIN_UTF8_OPT */
}else if( cli_strcmp(z,"-heap")==0 ){
#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
const char *zSize;
sqlite3_int64 szHeap;
zSize = cmdline_option_value(argc, argv, ++i);
szHeap = integerValue(zSize);
|
28382
28383
28384
28385
28386
28387
28388
28389
28390
28391
28392
28393
28394
28395
28396
28397
28398
28399
28400
28401
28402
28403
28404
28405
28406
|
28499
28500
28501
28502
28503
28504
28505
28506
28507
28508
28509
28510
28511
28512
28513
28514
28515
28516
28517
28518
28519
|
-
+
-
+
-
-
-
+
-
-
-
+
|
}else if( cli_strcmp(z,"-bail")==0 ){
/* No-op. The bail_on_error flag should already be set. */
}else if( cli_strcmp(z,"-version")==0 ){
printf("%s %s (%d-bit)\n", sqlite3_libversion(), sqlite3_sourceid(),
8*(int)sizeof(char*));
return 0;
}else if( cli_strcmp(z,"-interactive")==0 ){
stdin_is_interactive = 1;
/* already handled */
}else if( cli_strcmp(z,"-batch")==0 ){
stdin_is_interactive = 0;
/* already handled */
}else if( cli_strcmp(z,"-utf8")==0 ){
#if SHELL_WIN_UTF8_OPT
console_utf8 = 1;
#endif /* SHELL_WIN_UTF8_OPT */
/* already handled */
}else if( cli_strcmp(z,"-no-utf8")==0 ){
#if SHELL_WIN_UTF8_OPT
console_utf8 = 0;
#endif /* SHELL_WIN_UTF8_OPT */
/* already handled */
}else if( cli_strcmp(z,"-heap")==0 ){
i++;
}else if( cli_strcmp(z,"-pagecache")==0 ){
i+=2;
}else if( cli_strcmp(z,"-lookaside")==0 ){
i+=2;
}else if( cli_strcmp(z,"-threadsafe")==0 ){
|