Debian Codegraph @

ext/misc/sqlite3_stdio.c

raw
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 <windows.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <assert.h>
27 #include "sqlite3.h"
28 #include <ctype.h>
29 #include <stdarg.h>
30 #include <io.h>
31 #include <fcntl.h>
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<sz && b1[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<sz && b1[i]<0x80; i++){
206 fwrite(&b1[i], 1, 1, out);
207 }
208 fflush(out);
209 _setmode(_fileno(out), _O_U8TEXT);
210 b1 += i;
211 sz -= i;
212 }
213 }
214 }
215
216 /*
217 ** Work-alike for fputs() from the standard C library.
218 */
219 int sqlite3_fputs(const char *z, FILE *out){
220 if( !UseWtextForOutput(out) ){
221 /* Writing to a file or other destination, just write bytes without
222 ** any translation. */
223 return fputs(z, out);
224 }else{
225 /* One must use UTF16 in order to get unicode support when writing
226 ** to the console on Windows.
227 */
228 int sz = (int)strlen(z);
229 wchar_t *b1 = sqlite3_malloc64( (sz+1)*sizeof(wchar_t) );
230 if( b1==0 ) return 0;
231 sz = MultiByteToWideChar(CP_UTF8, 0, z, sz, b1, sz);
232 b1[sz] = 0;
233
234 #ifdef SQLITE_USE_W32_FOR_CONSOLE_IO
235 DWORD nWr = 0;
236 if( IsConsole(out)
237 && WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE),b1,sz,&nWr,0)
238 ){
239 /* If writing to the console, then the WriteConsoleW() is all we
240 ** need to do. */
241 }else
242 #endif
243 {
244 /* As long as SQLITE_USE_W32_FOR_CONSOLE_IO is not defined, or for
245 ** non-console I/O even if that macro is defined, write using the
246 ** standard library. */
247 _setmode(_fileno(out), _O_U8TEXT);
248 if( UseBinaryWText(out) ){
249 piecemealOutput(b1, sz, out);
250 }else{
251 fputws(b1, out);
252 }
253 }
254 sqlite3_free(b1);
255 return 0;
256 }
257 }
258
259
260 /*
261 ** Work-alikes for fprintf() and vfprintf() from the standard C library.
262 */
263 int sqlite3_fprintf(FILE *out, const char *zFormat, ...){
264 int rc;
265 if( UseWtextForOutput(out) ){
266 /* When writing to the command-prompt in Windows, it is necessary
267 ** to use _O_WTEXT input mode and write UTF-16 characters.
268 */
269 char *z;
270 va_list ap;
271
272 va_start(ap, zFormat);
273 z = sqlite3_vmprintf(zFormat, ap);
274 va_end(ap);
275 sqlite3_fputs(z, out);
276 rc = (int)strlen(z);
277 sqlite3_free(z);
278 }else{
279 /* Writing to a file or other destination, just write bytes without
280 ** any translation. */
281 va_list ap;
282 va_start(ap, zFormat);
283 rc = vfprintf(out, zFormat, ap);
284 va_end(ap);
285 }
286 return rc;
287 }
288 int sqlite3_vfprintf(FILE *out, const char *zFormat, va_list ap){
289 int rc;
290 if( UseWtextForOutput(out) ){
291 /* When writing to the command-prompt in Windows, it is necessary
292 ** to use _O_WTEXT input mode and write UTF-16 characters.
293 */
294 char *z;
295 z = sqlite3_vmprintf(zFormat, ap);
296 sqlite3_fputs(z, out);
297 rc = (int)strlen(z);
298 sqlite3_free(z);
299 }else{
300 /* Writing to a file or other destination, just write bytes without
301 ** any translation. */
302 rc = vfprintf(out, zFormat, ap);
303 }
304 return rc;
305 }
306
307 /*
308 ** Set the mode for an output stream. mode argument is typically _O_BINARY or
309 ** _O_TEXT.
310 */
311 void sqlite3_fsetmode(FILE *fp, int mode){
312 if( !UseWtextForOutput(fp) ){
313 fflush(fp);
314 _setmode(_fileno(fp), mode);
315 }else if( fp==stdout || fp==stderr ){
316 simBinaryStdout = (mode==_O_BINARY);
317 }else{
318 simBinaryOther = (mode==_O_BINARY);
319 }
320 }
321
322 #endif /* defined(_WIN32) */