static int Parse( int argc, char **argv ) /*****************************************/ { int old_argc; char **old_argv; char *cmdbuf; char *env; int ret; int i; env = getenv( WCLENV ); if( env != NULL ) { old_argc = argc; old_argv = argv; argc = ParseEnvVar( env, NULL, NULL ) + 1; argv = MemAlloc( ( argc + old_argc ) * sizeof( char * ) ); cmdbuf = MemAlloc( strlen( env ) + argc ); argv[0] = old_argv[0]; ParseEnvVar( env, argv + 1, cmdbuf ); for( i = 1; i < old_argc; ++i, ++argc ) { argv[argc] = old_argv[i]; } argv[argc] = NULL; // last element of the array must be NULL ret = ParseArgs( argc, argv ); MemFree( argv ); MemFree( cmdbuf ); } else { ret = ParseArgs( argc, argv ); } return( ret ); }
int main( int argc, char *argv[] ) { const char *fs = NULL; char **eargv = NULL; int eargc; setlocale( LC_CTYPE, "" ); setlocale( LC_NUMERIC, "C" ); /* for parsing cmdline & prog */ cmdname = argv[0]; if( argc == 1 ) { fprintf( stderr, "usage: %s [-F fs] [-v var=value] [-f progfile | 'prog'] [file ...]\n", cmdname ); exit( 1 ); } signal( SIGFPE, fpecatch ); srand_seed = 1; srand( (unsigned)srand_seed ); yyin = NULL; symtab = makesymtab( NSYMTAB/NSYMTAB ); if( argc == 2 && argv[1][0] == '@' && argv[1][1] != '\0' ) { const char *env; env = getenv( &argv[1][1] ); if( env != NULL ) { eargc = ParseEnvVar( env, NULL, NULL ); // count parameters. eargv = malloc( eargc * sizeof( char * ) + strlen( env ) + 1 + eargc ); ParseEnvVar( env, eargv, (char *)( eargv + eargc ) ); argc = eargc; argv = eargv; } } while( argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0' ) { if( strcmp( argv[1], "-version" ) == 0 || strcmp( argv[1], "--version" ) == 0 ) { printf( "awk %s\n", version ); exit( 0 ); break; } if( strncmp( argv[1], "--", 2 ) == 0 ) { /* explicit end of args */ argc--; argv++; break; } switch( argv[1][1] ) { case 's': if( strcmp( argv[1], "-safe" ) == 0 ) safe = true; break; case 'f': /* next argument is program filename */ if( argv[1][2] != '\0' ) { /* arg is -fsomething */ if( npfile >= MAX_PFILE - 1 ) { FATAL( "too many -f options" ); } pfile[npfile++] = unquote( &argv[1][2] ); } else { /* arg is -f something */ argc--; argv++; if( argc <= 1 ) { FATAL( "no program filename" ); } if( npfile >= MAX_PFILE - 1 ) { FATAL( "too many -f options" ); } pfile[npfile++] = unquote( argv[1] ); } break; case 'F': /* set field separator */ if( argv[1][2] != '\0' ) { /* arg is -Fsomething */ if( argv[1][2] == 't' && argv[1][3] == '\0' ) { /* wart: t=>\t */ fs = "\t"; } else if( argv[1][2] != '\0' ) { fs = &argv[1][2]; } } else { /* arg is -F something */ argc--; argv++; if( argc > 1 && argv[1][0] == 't' && argv[1][1] == '\0' ) { /* wart: t=>\t */ fs = "\t"; } else if( argc > 1 && argv[1][0] != '\0' ) { fs = &argv[1][0]; } } if( fs == NULL || *fs == '\0' ) WARNING( "field separator FS is empty" ); break; case 'v': /* -v a=1 to be done NOW. one -v for each */ if( argv[1][2] != '\0' ) { /* arg is -vsomething */ char *p; p = unquote( &argv[1][2] ); if( isclvar( p ) ) { setclvar( p ); } else { FATAL( "invalid -v option argument: %s", p ); } } else { /* arg is -v something */ char *p; argc--; argv++; if( argc <= 1 ) { FATAL( "no variable name" ); } p = unquote( argv[1] ); if( isclvar( p ) ) { setclvar( p ); } else { FATAL( "invalid -v option argument: %s", p ); } } break; case 'd': dbg = atoi( &argv[1][2] ); if( dbg == 0 ) dbg = 1; printf( "awk %s\n", version ); break; default: WARNING( "unknown option %s ignored", argv[1] ); break; } argc--; argv++; } /* argv[1] is now the first argument */ if( npfile == 0 ) { /* no -f; first argument is program */ char *p; if( argc <= 1 ) { if( dbg ) exit( 0 ); FATAL( "no program given" ); } p = unquote( argv[1] ); dprintf(( "program = |%s|\n", p )); lexprog = p; argc--; argv++; } for( eargc = 1; eargc < argc; ++eargc ) { argv[eargc] = unquote( argv[eargc] ); } recinit( recsize ); syminit(); compile_time = 1; argv[0] = cmdname; /* put prog name at front of arglist */ dprintf(( "argc=%d, argv[0]=%s\n", argc, argv[0] )); arginit( argc, argv ); if( !safe ) envinit( environ ); yyparse(); setlocale( LC_NUMERIC, "" ); /* back to whatever it is locally */ if( fs ) *FS = qstring( fs, '\0' ); dprintf(( "errorflag=%d\n", errorflag )); if( errorflag == 0 ) { compile_time = 0; run( winner ); } else { bracecheck(); } return( errorflag ); }
static int RCMainLine( const char *opts, int argc, char **argv ) /**************************************************************/ { char *cmdbuf = NULL; const char *str; char infile[_MAX_PATH + 2]; // +2 for quotes char outfile[_MAX_PATH + 6]; // +6 for -fo="" or -fe="" bool pass1; int i; int rc; curBufPos = formatBuffer; RcMemInit(); InitGlobs(); rc = setjmp( jmpbuf_RCFatalError ); if( rc == 0 ) { InitRcMsgs(); if( opts != NULL ) { str = opts; argc = ParseEnvVar( str, NULL, NULL ); argv = RcMemMalloc( ( argc + 4 ) * sizeof( char * ) ); cmdbuf = RcMemMalloc( strlen( str ) + argc + 1 ); ParseEnvVar( str, argv, cmdbuf ); pass1 = false; for( i = 0; i < argc; i++ ) { if( argv[i] != NULL && !stricmp( argv[i], "-r" ) ) { pass1 = true; break; } } if( initInfo != NULL && initInfo->ver > 1 && initInfo->cmd_line_has_files ) { if( !ideCb->GetInfo( cbHandle, IDE_GET_SOURCE_FILE, 0, (IDEGetInfoLParam)( infile + 1 ) ) ) { infile[0] = '\"'; strcat( infile, "\"" ); argv[argc++] = infile; } if( !ideCb->GetInfo( cbHandle, IDE_GET_TARGET_FILE, 0, (IDEGetInfoLParam)( outfile + 5 ) ) ) { if( pass1 ) { strcpy( outfile, "-fo=\"" ); } else { strcpy( outfile, "-fe=\"" ); } strcat( outfile, "\"" ); argv[argc++] = outfile; } } argv[argc] = NULL; // last element of the array must be NULL } if( !ScanParams( argc, argv ) ) { rc = 1; } if( !CmdLineParms.Quiet ) { RcIoPrintBanner(); } if( CmdLineParms.PrintHelp ) { RcIoPrintHelp(); } if( rc == 0 ) { rc = RCSpawn( RCmain ); } if( opts != NULL ) { RcMemFree( argv ); RcMemFree( cmdbuf ); } FiniRcMsgs(); } FiniGlobs(); flushPrintf(); RcMemShutdown(); return( rc ); }