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
|
}
}
return dynPrompt.dynamicPrompt;
}
#endif /* !defined(SQLITE_OMIT_DYNAPROMPT) */
#if SHELL_WIN_UTF8_OPT
/* 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 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/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_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 ) 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");
return;
}
csWork.hConsole = hCC;
csWork.consoleMode = consoleMode;
csWork.inCodePage = GetConsoleCP();
csWork.outCodePage = GetConsoleOutputCP();
if( conI ){
if( !SetConsoleCP(CP_UTF8) ) goto bail;
consoleMode |= ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT;
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;
console_utf8_out = conO;
conState = csWork;
}
/*
** Undo the effects of console_prepare_utf8(), if any.
*/
static void SQLITE_CDECL console_restore(void){
if( (console_utf8_in||console_utf8_out)
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
|
|
|
<
|
<
<
<
<
<
<
<
<
<
<
>
>
>
|
<
<
<
<
|
>
>
>
>
>
|
|
|
<
<
<
<
<
<
|
>
>
>
>
>
>
|
|
<
|
>
|
|
|
|
>
|
>
>
>
>
>
>
|
>
>
>
>
<
<
<
<
|
|
|
<
>
<
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
|
|
|
|
|
>
|
|
|
>
|
<
<
|
>
<
<
<
|
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
|
}
}
return dynPrompt.dynamicPrompt;
}
#endif /* !defined(SQLITE_OMIT_DYNAPROMPT) */
#if SHELL_WIN_UTF8_OPT
/* Following struct is used for UTF-8 console I/O. */
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 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 given stream number is a console, return 1 and get some attributes,
** else return 0 and set the output attributes to invalid values.
*/
static short console_attrs(unsigned stnum, HANDLE *pH, DWORD *pConsMode){
static int stid[3] = { STD_INPUT_HANDLE,STD_OUTPUT_HANDLE,STD_ERROR_HANDLE };
HANDLE h;
*pH = INVALID_HANDLE_VALUE;
*pConsMode = 0;
if( stnum > 2 ) return 0;
h = GetStdHandle(stid[stnum]);
if( h!=*pH && GetFileType(h)==FILE_TYPE_CHAR && GetConsoleMode(h,pConsMode) ){
*pH = h;
return 1;
}
return 0;
}
/*
** Perform a runtime test of Windows console to determine if it can
** do char-stream I/O correctly when the code page is set to CP_UTF8.
** Returns are: 1 => yes it can, 0 => no it cannot
**
** The console's output code page is momentarily set, then restored.
** So this should only be run when the process is given use of the
** console for either input or output.
*/
static short ConsoleDoesUTF8(void){
UINT ocp = GetConsoleOutputCP();
const char TrialUtf8[] = { '\xC8', '\xAB' }; /* "ȫ" or 2 MBCS characters */
WCHAR aReadBack[1] = { 0 }; /* Read back as 0x022B when decoded as UTF-8. */
CONSOLE_SCREEN_BUFFER_INFO csbInfo = {0};
/* Create an inactive screen buffer with which to do the experiment. */
HANDLE hCSB = CreateConsoleScreenBuffer(GENERIC_READ|GENERIC_WRITE, 0, 0,
CONSOLE_TEXTMODE_BUFFER, NULL);
if( hCSB!=INVALID_HANDLE_VALUE ){
COORD cpos = {0,0};
DWORD rbc;
SetConsoleCursorPosition(hCSB, cpos);
SetConsoleOutputCP(CP_UTF8);
/* Write 2 chars which are a single character in UTF-8 but more in MBCS. */
WriteConsoleA(hCSB, TrialUtf8, sizeof(TrialUtf8), NULL, NULL);
ReadConsoleOutputCharacterW(hCSB, &aReadBack[0], 1, cpos, &rbc);
GetConsoleScreenBufferInfo(hCSB, &csbInfo);
SetConsoleOutputCP(ocp);
CloseHandle(hCSB);
}
/* Return 1 if cursor advanced by 1 position, else 0. */
return (short)(csbInfo.dwCursorPosition.X == 1 && aReadBack[0] == 0x022B);
}
static short in_console = 0;
static short out_console = 0;
/*
** Determine whether either normal I/O stream is the console,
** and whether it can do UTF-8 translation, setting globals
** in_console, out_console and mbcs_opted accordingly.
*/
static void probe_console(void){
HANDLE h;
DWORD cMode;
in_console = console_attrs(0, &h, &cMode);
out_console = console_attrs(1, &h, &cMode);
if( in_console || out_console ) mbcs_opted = !ConsoleDoesUTF8();
}
/*
** If console is used for normal I/O, absent a --no-utf8 option,
** prepare console for UTF-8 input (from either typing or suitable
** paste operations) and/or for UTF-8 output rendering.
**
** 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.
** This routine depends upon globals set by probe_console().
*/
static void console_prepare_utf8(void){
struct ConsoleState csWork = { 0, 0, 0, 0, INVALID_HANDLE_VALUE, 0 };
console_utf8_in = console_utf8_out = 0;
if( (!in_console && !out_console) || mbcs_opted ) return;
console_attrs((in_console)? 0 : 1, &conState.hConsole, &conState.consoleMode);
conState.inCodePage = GetConsoleCP();
conState.outCodePage = GetConsoleOutputCP();
if( in_console ){
SetConsoleCP(CP_UTF8);
DWORD newConsoleMode = conState.consoleMode
| ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT;
SetConsoleMode(conState.hConsole, newConsoleMode);
conState.infsMode = _setmode(_fileno(stdin), _O_U16TEXT);
console_utf8_in = 1;
}
if( out_console ){
SetConsoleOutputCP(CP_UTF8);
console_utf8_out = 1;
}
}
/*
** Undo the effects of console_prepare_utf8(), if any.
*/
static void SQLITE_CDECL console_restore(void){
if( (console_utf8_in||console_utf8_out)
|
19326
19327
19328
19329
19330
19331
19332
19333
19334
19335
19336
19337
19338
19339
19340
19341
19342
19343
19344
19345
19346
19347
19348
19349
19350
19351
19352
19353
19354
19355
19356
19357
19358
19359
19360
19361
19362
19363
19364
19365
19366
19367
19368
|
for(ii=0; 1; ii++){
const char *z = 0;
int n = 0;
if( sqlite3_stmt_scanstatus_v2(p,ii,SQLITE_SCANSTAT_EXPLAIN,f,(void*)&z) ){
break;
}
n = strlen(z) + scanStatsHeight(p, ii)*3;
if( n>nWidth ) nWidth = n;
}
nWidth += 4;
sqlite3_stmt_scanstatus_v2(p, -1, SQLITE_SCANSTAT_NCYCLE, f, (void*)&nTotal);
for(ii=0; 1; ii++){
i64 nLoop = 0;
i64 nRow = 0;
i64 nCycle = 0;
int iId = 0;
int iPid = 0;
const char *z = 0;
const char *zName = 0;
char *zText = 0;
double rEst = 0.0;
if( sqlite3_stmt_scanstatus_v2(p,ii,SQLITE_SCANSTAT_EXPLAIN,f,(void*)&z) ){
break;
}
sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_EST,f,(void*)&rEst);
sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NLOOP,f,(void*)&nLoop);
sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NVISIT,f,(void*)&nRow);
sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NCYCLE,f,(void*)&nCycle);
sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_SELECTID,f,(void*)&iId);
sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_PARENTID,f,(void*)&iPid);
sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NAME,f,(void*)&zName);
zText = sqlite3_mprintf("%s", z);
if( nCycle>=0 || nLoop>=0 || nRow>=0 ){
char *z = 0;
if( nCycle>=0 && nTotal>0 ){
z = sqlite3_mprintf("%zcycles=%lld [%d%%]", z,
nCycle, ((nCycle*100)+nTotal/2) / nTotal
);
}
|
|
|
|
|
|
19327
19328
19329
19330
19331
19332
19333
19334
19335
19336
19337
19338
19339
19340
19341
19342
19343
19344
19345
19346
19347
19348
19349
19350
19351
19352
19353
19354
19355
19356
19357
19358
19359
19360
19361
19362
19363
19364
19365
19366
19367
19368
19369
|
for(ii=0; 1; ii++){
const char *z = 0;
int n = 0;
if( sqlite3_stmt_scanstatus_v2(p,ii,SQLITE_SCANSTAT_EXPLAIN,f,(void*)&z) ){
break;
}
n = (int)strlen(z) + scanStatsHeight(p, ii)*3;
if( n>nWidth ) nWidth = n;
}
nWidth += 4;
sqlite3_stmt_scanstatus_v2(p, -1, SQLITE_SCANSTAT_NCYCLE, f, (void*)&nTotal);
for(ii=0; 1; ii++){
i64 nLoop = 0;
i64 nRow = 0;
i64 nCycle = 0;
int iId = 0;
int iPid = 0;
const char *zo = 0;
const char *zName = 0;
char *zText = 0;
double rEst = 0.0;
if( sqlite3_stmt_scanstatus_v2(p,ii,SQLITE_SCANSTAT_EXPLAIN,f,(void*)&zo) ){
break;
}
sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_EST,f,(void*)&rEst);
sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NLOOP,f,(void*)&nLoop);
sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NVISIT,f,(void*)&nRow);
sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NCYCLE,f,(void*)&nCycle);
sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_SELECTID,f,(void*)&iId);
sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_PARENTID,f,(void*)&iPid);
sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NAME,f,(void*)&zName);
zText = sqlite3_mprintf("%s", zo);
if( nCycle>=0 || nLoop>=0 || nRow>=0 ){
char *z = 0;
if( nCycle>=0 && nTotal>0 ){
z = sqlite3_mprintf("%zcycles=%lld [%d%%]", z,
nCycle, ((nCycle*100)+nTotal/2) / nTotal
);
}
|