Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/ext/misc/sqlite3_stdio.c # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / ext / misc / sqlite3_stdio.c` 324 lines ยท 9203 bytes Raw Blame History 1 /* 2 ** 2024-09-24 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ************************************************************************* 12 ** 13 ** Implementation of standard I/O interfaces for UTF-8 that are missing 14 ** on Windows. 15 */ 16 #ifdef _WIN32 /* This file is a no-op on all platforms except Windows */ 17 #ifndef _SQLITE3_STDIO_H_ 18 #include "sqlite3_stdio.h" 19 #endif 20 #undef WIN32_LEAN_AND_MEAN 21 #define WIN32_LEAN_AND_MEAN 22 #include 23 #include 24 #include 25 #include 26 #include 27 #include "sqlite3.h" 28 #include 29 #include 30 #include 31 #include 32 33 /* 34 ** If the SQLITE_U8TEXT_ONLY option is defined, then use O_U8TEXT 35 ** when appropriate on all output. (Sometimes use O_BINARY when 36 ** rendering ASCII text in cases where NL-to-CRLF expansion would 37 ** not be correct.) 38 ** 39 ** If the SQLITE_U8TEXT_STDIO option is defined, then use O_U8TEXT 40 ** when appropriate when writing to stdout or stderr. Use O_BINARY 41 ** or O_TEXT (depending on things like the .mode and the .crlf setting 42 ** in the CLI, or other context clues in other applications) for all 43 ** other output channels. 44 ** 45 ** The default behavior, if neither of the above is defined is to 46 ** use O_U8TEXT when writing to the Windows console (or anything 47 ** else for which _isatty() returns true) and to use O_BINARY or O_TEXT 48 ** for all other output channels. 49 ** 50 ** The SQLITE_USE_W32_FOR_CONSOLE_IO macro is also available. If 51 ** defined, it forces the use of Win32 APIs for all console I/O, both 52 ** input and output. This is necessary for some non-Microsoft run-times 53 ** that implement stdio differently from Microsoft/Visual-Studio. 54 */ 55 #if defined(SQLITE_U8TEXT_ONLY) 56 # define UseWtextForOutput(fd) 1 57 # define UseWtextForInput(fd) 1 58 # define IsConsole(fd) _isatty(_fileno(fd)) 59 #elif defined(SQLITE_U8TEXT_STDIO) 60 # define UseWtextForOutput(fd) ((fd)==stdout || (fd)==stderr) 61 # define UseWtextForInput(fd) ((fd)==stdin) 62 # define IsConsole(fd) _isatty(_fileno(fd)) 63 #else 64 # define UseWtextForOutput(fd) _isatty(_fileno(fd)) 65 # define UseWtextForInput(fd) _isatty(_fileno(fd)) 66 # define IsConsole(fd) 1 67 #endif 68 69 /* 70 ** Global variables determine if simulated O_BINARY mode is to be 71 ** used for stdout or other, respectively. Simulated O_BINARY mode 72 ** means the mode is usually O_BINARY, but switches to O_U8TEXT for 73 ** unicode characters U+0080 or greater (any character that has a 74 ** multi-byte representation in UTF-8). This is the only way we 75 ** have found to render Unicode characters on a Windows console while 76 ** at the same time avoiding undesirable \n to \r\n translation. 77 */ 78 static int simBinaryStdout = 0; 79 static int simBinaryOther = 0; 80 81 82 /* 83 ** Determine if simulated binary mode should be used for output to fd 84 */ 85 static int UseBinaryWText(FILE *fd){ 86 if( fd==stdout || fd==stderr ){ 87 return simBinaryStdout; 88 }else{ 89 return simBinaryOther; 90 } 91 } 92 93 94 /* 95 ** Work-alike for the fopen() routine from the standard C library. 96 */ 97 FILE *sqlite3_fopen(const char *zFilename, const char *zMode){ 98 FILE *fp = 0; 99 wchar_t *b1, *b2; 100 int sz1, sz2; 101 102 sz1 = (int)strlen(zFilename); 103 sz2 = (int)strlen(zMode); 104 b1 = sqlite3_malloc64( (sz1+1)*sizeof(b1[0]) ); 105 b2 = sqlite3_malloc64( (sz2+1)*sizeof(b1[0]) ); 106 if( b1 && b2 ){ 107 sz1 = MultiByteToWideChar(CP_UTF8, 0, zFilename, sz1, b1, sz1); 108 b1[sz1] = 0; 109 sz2 = MultiByteToWideChar(CP_UTF8, 0, zMode, sz2, b2, sz2); 110 b2[sz2] = 0; 111 fp = _wfopen(b1, b2); 112 } 113 sqlite3_free(b1); 114 sqlite3_free(b2); 115 simBinaryOther = 0; 116 return fp; 117 } 118 119 120 /* 121 ** Work-alike for the popen() routine from the standard C library. 122 */ 123 FILE *sqlite3_popen(const char *zCommand, const char *zMode){ 124 FILE *fp = 0; 125 wchar_t *b1, *b2; 126 int sz1, sz2; 127 128 sz1 = (int)strlen(zCommand); 129 sz2 = (int)strlen(zMode); 130 b1 = sqlite3_malloc64( (sz1+1)*sizeof(b1[0]) ); 131 b2 = sqlite3_malloc64( (sz2+1)*sizeof(b1[0]) ); 132 if( b1 && b2 ){ 133 sz1 = MultiByteToWideChar(CP_UTF8, 0, zCommand, sz1, b1, sz1); 134 b1[sz1] = 0; 135 sz2 = MultiByteToWideChar(CP_UTF8, 0, zMode, sz2, b2, sz2); 136 b2[sz2] = 0; 137 fp = _wpopen(b1, b2); 138 } 139 sqlite3_free(b1); 140 sqlite3_free(b2); 141 return fp; 142 } 143 144 /* 145 ** Work-alike for fgets() from the standard C library. 146 */ 147 char *sqlite3_fgets(char *buf, int sz, FILE *in){ 148 if( UseWtextForInput(in) ){ 149 /* When reading from the command-prompt in Windows, it is necessary 150 ** to use _O_WTEXT input mode to read UTF-16 characters, then translate 151 ** that into UTF-8. Otherwise, non-ASCII characters all get translated 152 ** into '?'. 153 */ 154 wchar_t *b1 = sqlite3_malloc64( sz*sizeof(wchar_t) ); 155 if( b1==0 ) return 0; 156 #ifdef SQLITE_USE_W32_FOR_CONSOLE_IO 157 DWORD nRead = 0; 158 if( IsConsole(in) 159 && ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE), b1, sz-1, &nRead, 0) 160 ){ 161 b1[nRead] = 0; 162 }else 163 #endif 164 { 165 _setmode(_fileno(in), IsConsole(in) ? _O_WTEXT : _O_U8TEXT); 166 if( fgetws(b1, sz/4, in)==0 ){ 167 sqlite3_free(b1); 168 return 0; 169 } 170 } 171 WideCharToMultiByte(CP_UTF8, 0, b1, -1, buf, sz, 0, 0); 172 sqlite3_free(b1); 173 return buf; 174 }else{ 175 /* Reading from a file or other input source, just read bytes without 176 ** any translation. */ 177 return fgets(buf, sz, in); 178 } 179 } 180 181 /* 182 ** Send ASCII text as O_BINARY. But for Unicode characters U+0080 and 183 ** greater, switch to O_U8TEXT. 184 */ 185 static void piecemealOutput(wchar_t *b1, int sz, FILE *out){ 186 int i; 187 wchar_t c; 188 while( sz>0 ){ 189 for(i=0; i=0x80; i++){} 190 if( i>0 ){ 191 c = b1[i]; 192 b1[i] = 0; 193 fflush(out); 194 _setmode(_fileno(out), _O_U8TEXT); 195 fputws(b1, out); 196 fflush(out); 197 b1 += i; 198 b1[0] = c; 199 sz -= i; 200 }else{ 201 fflush(out); 202 _setmode(_fileno(out), _O_TEXT); 203 _setmode(_fileno(out), _O_BINARY); 204 fwrite(&b1[0], 1, 1, out); 205 for(i=1; i