Index: src/main.c ================================================================== --- src/main.c +++ src/main.c @@ -24,11 +24,13 @@ #include #include #include #include #include /* atexit() */ -#if !defined(_WIN32) +#if defined(_WIN32) +# include +#else # include /* errno global */ #endif #if INTERFACE #ifdef FOSSIL_ENABLE_JSON # include "cson_amalgamation.h" /* JSON API. Needed inside the INTERFACE block! */ @@ -329,29 +331,42 @@ db_close(0); } } /* -** Search g.argv for arguments "--args FILENAME". If found, then +** Convert all arguments from mbcs to UTF-8. Then +** search g.argv for arguments "--args FILENAME". If found, then ** (1) remove the two arguments from g.argv ** (2) Read the file FILENAME ** (3) Use the contents of FILE to replace the two removed arguments: ** (a) Ignore blank lines in the file ** (b) Each non-empty line of the file is an argument, except ** (c) If the line begins with "-" and contains a space, it is broken ** into two arguments at the space. */ -static void expand_args_option(void){ +static void expand_args_option(int argc, char *argv){ Blob file = empty_blob; /* Content of the file */ Blob line = empty_blob; /* One line of the file */ unsigned int nLine; /* Number of lines in the file*/ unsigned int i, j, k; /* Loop counters */ int n; /* Number of bytes in one line */ char *z; /* General use string pointer */ char **newArgv; /* New expanded g.argv under construction */ char const * zFileName; /* input file name */ FILE * zInFile; /* input FILE */ + int foundBom = -1; /* -1= not searched yet, 0 = no; 1=yes */ +#ifdef _WIN32 + wchar_t buf[PATH_MAX]; +#endif + + g.argc = argc; + g.argv = argv; +#ifdef _WIN32 + GetModuleFileNameW(NULL, buf, PATH_MAX); + g.argv[0] = fossil_unicode_to_utf8(buf); + for(i=1; i=g.argc-1 ) return; zFileName = g.argv[i+1]; zInFile = (0==strcmp("-",zFileName)) ? stdin - : fopen(zFileName,"rb"); + : fossil_fopen(zFileName,"rb"); if(!zInFile){ fossil_panic("Cannot open -args file [%s]", zFileName); }else{ blob_read_from_channel(&file, zInFile, -1); if(stdin != zInFile){ @@ -381,14 +396,24 @@ blob_rewind(&file); while( (n = blob_line(&file, &line))>0 ){ if( n<=1 ) continue; z = blob_buffer(&line); z[n-1] = 0; + if (foundBom == -1) { + static const char bom[] = { 0xEF, 0xBB, 0xBF }; + foundBom = memcmp(z, bom, 3)==0; + if( foundBom ) { + z += 3; n -= 3; + } + } if((n>1) && ('\r'==z[n-2])){ if(n==2) continue /*empty line*/; z[n-2] = 0; } + if (!foundBom) { + z = fossil_mbcs_to_utf8(z); + } newArgv[j++] = z; if( z[0]=='-' ){ for(k=1; z[k] && !fossil_isspace(z[k]); k++){} if( z[k] ){ z[k] = 0; @@ -409,17 +434,14 @@ */ int main(int argc, char **argv){ const char *zCmdName = "unknown"; int idx; int rc; - int i; sqlite3_config(SQLITE_CONFIG_LOG, fossil_sqlite_log, 0); memset(&g, 0, sizeof(g)); g.now = time(0); - g.argc = argc; - g.argv = argv; #ifdef FOSSIL_ENABLE_JSON #if defined(NDEBUG) g.json.errorDetailParanoia = 2 /* FIXME: make configurable One problem we have here is that this code is needed before the db is opened, @@ -429,12 +451,11 @@ #endif g.json.outOpt = cson_output_opt_empty; g.json.outOpt.addNewline = 1; g.json.outOpt.indentation = 1 /* in CGI/server mode this can be configured */; #endif /* FOSSIL_ENABLE_JSON */ - expand_args_option(); - for(i=0; i