22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
-
+
+
+
|
#include "main.h"
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h> /* atexit() */
#if !defined(_WIN32)
#if defined(_WIN32)
# include <windows.h>
#else
# include <errno.h> /* errno global */
#endif
#if INTERFACE
#ifdef FOSSIL_ENABLE_JSON
# include "cson_amalgamation.h" /* JSON API. Needed inside the INTERFACE block! */
# include "json_detail.h"
#endif
|
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
+
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
|
free(g.zErrMsg);
if(g.db){
db_close(0);
}
}
/*
** Convert all arguments from mbcs to UTF-8. Then
** Search g.argv for arguments "--args FILENAME". If found, 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; i++) g.argv[i] = fossil_mbcs_to_utf8(g.argv[i]);
#endif
for(i=1; i<g.argc-1; i++){
z = g.argv[i];
if( z[0]!='-' ) continue;
z++;
if( z[0]=='-' ) z++;
if( z[0]==0 ) return; /* Stop searching at "--" */
if( fossil_strcmp(z, "args")==0 ) break;
}
if( 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){
fclose(zInFile);
}
zInFile = NULL;
}
z = blob_str(&file);
for(k=0, nLine=1; z[k]; k++) if( z[k]=='\n' ) nLine++;
newArgv = fossil_malloc( sizeof(char*)*(g.argc + nLine*2) );
for(j=0; j<i; j++) newArgv[j] = g.argv[j];
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;
k++;
if( z[k] ) newArgv[j++] = &z[k];
|
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
|
-
-
-
-
+
-
|
/*
** This procedure runs first.
*/
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,
so we can't sql for it.*/;
#else
g.json.errorDetailParanoia = 0;
#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();
expand_args_option(argc, argv);
for(i=0; i<g.argc; i++) g.argv[i] = fossil_mbcs_to_utf8(g.argv[i]);
#ifdef FOSSIL_ENABLE_TCL
g.tcl.argc = g.argc;
g.tcl.argv = g.argv;
g.tcl.interp = 0;
#endif
if( fossil_getenv("GATEWAY_INTERFACE")!=0 && !find_option("nocgi", 0, 0)){
zCmdName = "cgi";
|
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
|
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
|
-
-
-
-
|
*/
static int mainInFatalError = 0;
/*
** Return the name of the current executable.
*/
const char *fossil_nameofexe(void){
#ifdef _WIN32
return _pgmptr;
#else
return g.argv[0];
#endif
}
/*
** Exit. Take care to close the database first.
*/
NORETURN void fossil_exit(int rc){
db_close(1);
|
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
|
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
|
-
+
|
fossil_binary_mode(g.httpIn);
g.cgiOutput = 1;
blob_read_from_file(&config, zFile);
while( blob_line(&config, &line) ){
if( !blob_token(&line, &key) ) continue;
if( blob_buffer(&key)[0]=='#' ) continue;
if( blob_eq(&key, "debug:") && blob_token(&line, &value) ){
g.fDebug = fopen(blob_str(&value), "a");
g.fDebug = fossil_fopen(blob_str(&value), "a");
blob_reset(&value);
continue;
}
if( blob_eq(&key, "HOME:") && blob_token(&line, &value) ){
cgi_setenv("HOME", blob_str(&value));
blob_reset(&value);
continue;
|