sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 1 | /* |
| 2 | ** 2025-12-26 |
| 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 | ** This file implements a substitute process entry point for windows |
| 14 | ** that correctly interprets command-line arguments as UTF-8. This is |
| 15 | ** a work-around for the (unfixed) Windows bug known as "WorstFit" and |
| 16 | ** described at: |
| 17 | ** |
| 18 | ** * https://news.ycombinator.com/item?id=42647101 |
| 19 | ** |
| 20 | ** USAGE: |
| 21 | ** |
| 22 | ** If you have a command-line program coded to C-language standard in which |
| 23 | ** the entry point is: |
| 24 | ** |
| 25 | ** int main(int argc, char **argv){...} |
| 26 | ** |
| 27 | ** That does not work on Windows if there are non-ASCII characters on the |
| 28 | ** command line. Programs are expected to use an alternative entry point: |
| 29 | ** |
| 30 | ** int wmain(int wargc, wchar_t **wargv){...} |
| 31 | ** |
| 32 | ** This file implements _wmain() but then converts all of the wargv elements |
| 33 | ** to UTF-8 and passes them off to utf8_main(). |
| 34 | ** |
| 35 | ** So, a command-line tool that implements the standard entry point can be |
| 36 | ** modified by adding the following lines just prior to main(): |
| 37 | ** |
| 38 | ** #ifdef _WIN32 |
| 39 | ** #define main utf8_main |
| 40 | ** #endif |
| 41 | ** |
| 42 | ** Then, include this file in the list of files that implement the program, |
| 43 | ** and the program will work correctly, even on Windows. |
| 44 | */ |
| 45 | #include <windows.h> |
| 46 | #include <stdio.h> |
| 47 | |
| 48 | extern int utf8_main(int,char**); |
| 49 | int wmain(int argc, wchar_t **wargv){ |
| 50 | int rc, i; |
| 51 | char **argv = malloc( sizeof(char*) * (argc+1) ); |
| 52 | char **orig = argv; |
| 53 | if( argv==0 ){ |
| 54 | fprintf(stderr, "malloc failed\n"); |
| 55 | exit(1); |
| 56 | } |
| 57 | for(i=0; i<argc; i++){ |
| 58 | int nByte = WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, 0, 0, 0, 0); |
| 59 | if( nByte==0 ){ |
| 60 | argv[i] = 0; |
| 61 | }else{ |
| 62 | argv[i] = malloc( nByte ); |
| 63 | if( argv[i]==0 ){ |
| 64 | fprintf(stderr, "malloc failed\n"); |
| 65 | exit(1); |
| 66 | } |
| 67 | nByte = WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, argv[i],nByte,0,0); |
| 68 | if( nByte==0 ){ |
| 69 | free(argv[i]); |
| 70 | argv[i] = 0; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | argv[argc] = 0; |
| 75 | rc = utf8_main(argc, argv); |
| 76 | for(i=0; i<argc; i++) free(orig[i]); |
| 77 | free(argv); |
| 78 | return rc; |
| 79 | } |