static int ParseVariable( const char *var, char **argv, char *buf ) /*****************************************************************/ { /* * Returns a count of the "command line" parameters in *var. * Unless argv is NULL, both argv and buf are completed. * * This function ought to be fairly similar to clib(initargv@_SplitParms). * Parameterisation does the same as _SplitParms with historical = 0. */ const char *start; int switchchar; int argc; char *bufend; char *bufstart; bool got_quote; bool output_data; switchchar = _dos_switch_char(); output_data = ( buf != NULL ) && ( argv != NULL ); bufstart = buf; bufend = buf; argc = 0; for( ;; ) { got_quote = false; while( isspace( *var ) && *var != '\0' ) var++; start = var; if( output_data ) { bufstart = bufend; } if( *var == switchchar || *var == '-' ) { if( output_data ) { *bufend++ = *var; } var++; } while( ( got_quote || !isspace( *var ) ) && *var != '\0' ) { if( *var == '\"' ) { got_quote = !got_quote; } if( output_data ) { *bufend++ = *var; } var++; } if( start != var ) { if( output_data ) { *bufend++ = '\0'; argv[argc] = bufstart; } argc++; } if( *var == '\0' ) { break; } } return( argc ); }
static unsigned ParseEnvVar( const char *env, char **argv, char *buf ) /********************************************************************/ { /* * Returns a count of the "command line" parameters in *env. * Unless argv is NULL, both argv and buf are completed. * * This function ought to be fairly similar to clib(initargv@_SplitParms). * Parameterisation does the same as _SplitParms with historical = 0. */ const char *start; int switchchar; unsigned argc; char *bufend; bool got_quote; switchchar = _dos_switch_char(); bufend = buf; argc = 0; for( ; *env != '\0'; ) { got_quote = FALSE; while( isspace( *env ) && *env != '\0' ) env++; start = env; if( buf != NULL ) { argv[ argc ] = bufend; } if( *env == switchchar || *env == '-' ) { if( buf != NULL ) { *bufend = *env; bufend++; } env ++; } while( ( got_quote || !isspace( *env ) ) && *env != '\0' ) { if( *env == '\"' ) { got_quote = !got_quote; } if( buf != NULL ) { *bufend = *env; bufend++; } env++; } if( start != env ) { argc++; if( buf != NULL ) { *bufend = '\0'; bufend++; } } } return( argc ); }
int main( int argc, char **argv ) /*******************************/ { argc = argc; #ifndef __WATCOMC__ _argv = argv; _argc = argc; #endif #else int main( void ) /**************/ { char *argv[2]; int len; char *buff; #endif main_init(); SwitchChar = _dos_switch_char(); #ifndef __UNIX__ len = _bgetcmd( NULL, INT_MAX ) + 1; buff = malloc( len ); if( buff != NULL ) { argv[0] = buff; argv[1] = NULL; _bgetcmd( buff, len ); } else { return( -1 ); } do_init_stuff( argv ); #else do_init_stuff( &argv[1] ); #endif SetMemoryModel(); WriteObjModule(); // main body: parse the source file MsgFini(); main_fini(); #ifndef __UNIX__ free( buff ); #endif return( Options.error_count ); /* zero if no errors */ }
bool ScanParams( int argc, const char * argv[] ) /**********************************************/ { int switchchar; int nofilenames; /* number of filename parms read so far */ bool contok; /* continue with main execution */ int currarg; nofilenames = 0; contok = true; switchchar = _dos_switch_char(); DefaultParms(); for (currarg = 1; currarg < argc && contok; currarg++) { if (*argv[ currarg ] == switchchar || *argv[ currarg ] == '-') { contok = ScanOptionsArg( argv[ currarg ] + 1 ) && contok; } else if (*argv[ currarg ] == '?') { CmdLineParms.PrintHelp = true; contok = false; } else if (nofilenames == 0) { strncpy( CmdLineParms.FileName, argv[ currarg ], _MAX_PATH ); nofilenames++; } else { puts( "Error: Too many arguments on command line" ); contok = false; } } if (contok) { switch (nofilenames) { case 0: puts( "Error: filename required." ); contok = false; break; default: CheckExtention( CmdLineParms.FileName ); break; } } return( contok ); }
int main( int argc, char *argv[] ) { //====================================== int rc; char *wfl_env; char *p; char *q; char *cmd; /* unused parameters */ (void)argc; __InitResource(); __ErrorInit( argv[0] ); CmpOpts[0] = NULL; SwitchChars[0] = '-'; SwitchChars[1] = _dos_switch_char(); SwitchChars[2] = '\0'; Word = MemAlloc( MAX_CMD ); cmd = MemAlloc( 2*MAX_CMD ); // for "WFL" environment variable and command line // add "WFL" environment variable to "cmd" unless "/y" is specified // in "cmd" or the "WFL" environment string wfl_env = getenv( WFLENV ); if( wfl_env != NULL ) { strcpy( cmd, wfl_env ); strcat( cmd, " " ); p = cmd + strlen( cmd ); getcmd( p ); for( q = cmd; (q = strpbrk( q, SwitchChars )) != NULL; ) { if( tolower( *(++q) ) == 'y' ) { getcmd( cmd ); p = cmd; break; } } } else { getcmd( cmd ); p = cmd; } p = SkipSpaces( p ); if( ( *p == '\0' ) || ( strncmp( p, "? ", 2 ) == 0 ) ) { Usage(); rc = 1; } else { Fp = fopen( TEMPFILE, "w" ); if( Fp == NULL ) { PrintMsg( CL_ERROR_OPENING_TMP_FILE ); rc = 1; } else { ObjName = NULL; rc = Parse( argc, argv ); if( rc == 0 ) { if( !Flags.quiet ) { PrtBanner(); } rc = CompLink(); } if( rc == 1 ) fclose( Fp ); if( LinkName != NULL ) { if( stricmp( LinkName, TEMPFILE ) != 0 ) { remove( LinkName ); rename( TEMPFILE, LinkName ); } } else { remove( TEMPFILE ); } } } free( Word ); free( cmd ); wfl_exit( rc == 0 ? 0 : 1 ); return( 0 ); }
int main( int argc, char **argv ) /********************************/ { int rc; char *wcl_env; char *p; char *q; char *Cmd; /* command line parameters */ #ifndef __WATCOMC__ _argc = argc; _argv = argv; #endif CC_Opts[0] = '\0'; Switch_Chars[0] = '-'; Switch_Chars[1] = _dos_switch_char(); Switch_Chars[2] = '\0'; MemInit(); ProcMemInit(); Word = MemAlloc( MAX_CMD ); Cmd = MemAlloc( MAX_CMD * 2 ); /* enough for cmd line & wcl variable */ /* add wcl environment variable to Cmd */ /* unless /y is specified in either Cmd or wcl */ wcl_env = getenv( WCLENV ); if( wcl_env != NULL ) { strcpy( Cmd, wcl_env ); strcat( Cmd, " " ); p = Cmd + strlen( Cmd ); getcmd( p ); q = Cmd; while( (q = strpbrk( q, Switch_Chars )) != NULL ) { if( tolower( *(++q) ) == 'y' ) { getcmd( Cmd ); p = Cmd; break; } } } else { getcmd( Cmd ); p = Cmd; } p = SkipSpaces( p ); if( *p == '\0' || p[0] == '?' && ( p[1] == '\0' || p[1] == ' ' ) || p[0] == '-' && p[1] == '?' ) { Usage(); rc = 1; } else { errno = 0; /* Standard C does not require fopen failure to set errno */ if( (Fp = fopen( TEMPFILE, "w" )) == NULL ) { /* Message before banner decision as '@' option uses Fp in Parse() */ PrintMsg( WclMsgs[UNABLE_TO_OPEN_TEMPORARY_FILE], TEMPFILE, strerror( errno ) ); rc = 1; } else { initialize_Flags(); rc = Parse( Cmd ); if( rc == 0 ) { if( !Flags.be_quiet ) { print_banner(); } rc = CompLink(); } if( rc == 1 ) { fclose( Fp ); } if( Link_Name != NULL ) { if( fname_cmp( Link_Name, TEMPFILE ) != 0 ) { remove( Link_Name ); rename( TEMPFILE, Link_Name ); } } else { remove( TEMPFILE ); } } } ProcMemFini(); MemFree( Cmd ); MemFree( Word ); MemFini(); return( rc == 0 ? 0 : 1 ); }
static bool scanEnvVarOrFile( const char *name ) /**********************************************/ { /* * Pass nofilenames and analysis of getenv(name) into argc and argv * to doScanParams. Return view on usability of data. (true is usable.) * * Recursion is supported but circularity is rejected. */ typedef struct VarInfo { struct VarInfo *next; char *name; char **argv; /* points into buf */ char buf[1]; /* dynamic array */ } VarInfo; int argc; VarInfo *info; static VarInfo *stack = NULL; // Needed to detect recursion. size_t argvsize; size_t argbufsize; const char *optstring; size_t varlen; // size to hold name copy. bool result; // doScanParams Result. char fbuf[512]; optstring = PP_GetEnv( name ); if( optstring == NULL ) { FILE *fh; fh = fopen( name, "rt" ); if( fh == NULL ) { // RcWarning( ERR_ENV_VAR_NOT_FOUND, name ); return( true ); } fgets( fbuf, sizeof( fbuf ), fh ); fclose( fh ); optstring = fbuf; } // This used to cause stack overflow: set foo=@foo && wrc @foo. for( info = stack; info != NULL; info = info->next ) { #if defined( __UNIX__ ) if( strcmp( name, info->name ) == 0 ) { // Case-sensitive #else if( stricmp( name, info->name ) == 0 ) { // Case-insensitive #endif // RcFatalError( ERR_RCVARIABLE_RECURSIVE, name ); } } argc = ParseVariable( optstring, NULL, NULL ); // count parameters. argbufsize = strlen( optstring ) + 1 + argc; // inter-parameter spaces map to 0 argvsize = argc * sizeof( char * ); // sizeof argv[argc+1] varlen = strlen( name ) + 1; // Copy taken to detect recursion. info = malloc( sizeof( *info ) + argbufsize + argvsize + varlen ); info->next = stack; stack = info; // push info on stack info->argv = (char **)info->buf; ParseVariable( optstring, info->argv, info->buf + argvsize ); info->name = info->buf + argvsize + argbufsize; strcpy( info->name, name ); result = doScanParams( argc, info->argv ); stack = info->next; // pop stack free( info ); return( result ); } static bool doScanParams( int argc, char *argv[] ) /************************************************/ { const char *arg; int switchchar; bool contok; /* continue with main execution */ int currarg; contok = true; switchchar = _dos_switch_char(); for( currarg = 0; currarg < argc && contok; currarg++ ) { arg = argv[currarg]; if( *arg == switchchar || *arg == '-' ) { contok = ScanOptionsArg( arg + 1 ) && contok; } else if( *arg == '@' ) { contok = scanEnvVarOrFile( arg + 1 ) && contok; } else if( *arg == '?' ) { wcpp_quit( usageMsg, NULL ); // contok = false; } else { filenames = realloc( (void *)filenames, ( nofilenames + 1 ) * sizeof( char * ) ); filenames[nofilenames++] = my_strdup( arg ); } } return( contok ); } int main( int argc, char *argv[] ) { int ch; int i; int j; int rc; FILE *fo; if( argc < 2 ) { wcpp_quit( usageMsg, "No filename specified\n" ); } else if( argc == 2 ) { if( !strcmp( argv[1], "?" ) ) { wcpp_quit( usageMsg, NULL ); } } PP_IncludePathInit(); rc = EXIT_FAILURE; if( doScanParams( argc - 1, argv + 1 ) && nofilenames != 0 ) { PP_Init( '#' ); fo = stdout; if( out_filename != NULL ) { fo = fopen( out_filename, "wb" ); } rc = EXIT_SUCCESS; for( i = 0; i < nofilenames; ++i ) { if( PP_FileInit( filenames[i], flags, NULL ) != 0 ) { fprintf( stderr, "Unable to open '%s'\n", filenames[i] ); rc = EXIT_FAILURE; break; } for( j = 0; j < numdefs; j++ ) { PP_Define( defines[j] ); } for( ;; ) { ch = PP_Char(); if( ch == EOF ) break; #ifndef __UNIX__ if( ch == '\n' ) fputc( '\r', fo ); #endif fputc( ch, fo ); } PP_FileFini(); } if( fo == stdout ) { fflush( fo ); } else if( fo != NULL ) { fclose( fo ); } PP_Fini(); } if( out_filename != NULL ) { free( out_filename ); } for( i = 0; i < nofilenames; ++i ) { free( filenames[i] ); } free( (void *)filenames ); for( i = 0; i < numdefs; i++ ) { free( defines[i] ); } free( (void *)defines ); PP_IncludePathFini(); if( rc == EXIT_FAILURE && nofilenames == 0 ) { wcpp_quit( usageMsg, "No filename specified\n" ); } return( rc ); }