static TString ProcessCommands(const _TCHAR *file) { FILE *fin; #ifdef UNICODE int err = _wfopen_s(&fin, file, L"r,ccs=unicode"); #else int err = fopen_s(&fin, file, "r"); #endif if (err) return TString(file); FILE *fout; auto output_file = TempFile::Create(TEXT(".txt"), true); #ifdef UNICODE err = _wfopen_s(&fout, output_file.data(), L"w,ccs=unicode"); #else err = fopen_s(&fout, output_file.data(), "w"); #endif if (err) { perror("LinkWrapper:ProcessCommands"); exit(err); } _TINT ch = _gettc(fin); while (ch != _TEOF) { while (ch != _TEOF && _istspace(ch)) { _puttc(ch, fout); ch = _gettc(fin); } // FIXME: input files with spaces in them??? (are they quoted???) TString word; while (ch != _TEOF && !_istspace(ch)) { word.push_back(ch); ch = _gettc(fin); } // FIXME: handle comments (starting with ';') auto comment_pos = word.find(TCHAR(';')); assert(comment_pos == -1 && "Found comment in command file"); if (!word.empty()) { auto new_word = ProcessArg(word.data()); _fputts(new_word.data(), fout); } } fclose(fin); fclose(fout); return output_file; }
int _tmain(int argc, _TCHAR* argv[]) { // FIXME: MSDN says that the linker also parses arguments from the LINK environment variable std::vector<const _TCHAR*> linker_args; std::vector<TString> escaped_args; auto linker_exe = LocateMSVCLinker(); auto linker_exe_esc = QuoteSpaces(linker_exe.data()); linker_args.push_back(linker_exe_esc.data()); // Needed by _tspawnvp for (int i = 1; i < argc; i++) { auto trap_file = ProcessArg(argv[i]); escaped_args.push_back(QuoteSpaces(trap_file.data())); } for (auto &escaped_arg : escaped_args) linker_args.push_back(escaped_arg.data()); // Make a new linker arguments containing the following: // 1) The linker program name as argv[0] (required by _texecvp) // 2) The original arguments passed to the linker // 3) All additional arguments we add in (such as the path to RandoLib.lib) // 4) Terminating NULL pointer // When producing an executable/DLL, add in RandoLib.lib TString rando_lib_path, exports_file; if (!lib_mode) { exports_file = EmitExports(escaped_args); linker_args.push_back(exports_file.data()); linker_args.push_back(kLinkerExtraArg1); linker_args.push_back(kLinkerExtraArg2); linker_args.push_back(kRandoLib); // We need to disable incremental linking because it breaks our stuff // (for some reason, the linker adds an extra 0 byte to the end of each .txtrp entry) linker_args.push_back(kLinkerNoIncrementalArg); } linker_args.push_back(NULL); //PrintArgs(linker_args); auto errnum = _tspawnvp(_P_WAIT, linker_exe.data(), linker_args.data()); if (errnum) { perror("LinkWrapper:_tmain"); exit(errnum); } if (!lib_mode) CallPatchEntry(); return errnum; }
void readics( int argc, char* argv[] ) { // parses arguments from command line. Uses processArg to extract data printf("#reading ICs... "); int i; unsigned int j; char param[ 100 ]; /* should be enough space for the param! */ char value[ 200 ]; /* should be enough space for the value! */ char found_equals; /* Index of where the equals is */ /* Store the program name for future ref. * No need to copy argv[0] since it will always be in scope) */ ics.progName = argv[ 0 ]; /* Check if we just want to display the usage for the prog */ if( argc == 1 ) Usage( ); /* set the default parameter values */ strcpy(ics.filename, "out.txt"); strcpy(ics.run_control_file, ""); strcpy(ics.all_params, ""); ics.run_number=0; ics.Ninitial = 1000; ics.repeats = 1; ics.gens = 1500; ics.seed = 3141592; ics.growthModel = 'K'; ics.mostagg = 20; ics.muB = 0.001; ics.cancerProp = 0.1; ics.mutProp = 0.1; ics.Md = 50; ics.Mh = 50; ics.Mp = 50; ics.Mm = 50; ics.mut_max = 10; ics.num_passengers_needed_for_cancer = 0; ics.probD = 0.0001; ics.probH = 0.001; ics.probM = 0.00005; ics.sD = 0.01; ics.sH = 0.01;; ics.sM = 0.01; ics.beta = 0.01; ics.output_marginalized_counts = 0; // NOTE!!! This procedure does not do any range checking. This is dangerous // and can cause a buffer overflow. /* Parse the program arguments one at a time. * Note - all args should be of the form -param=value */ for( i = 1; i < argc; ++i ) { /* Ensure we start with a '-' */ if( argv[ i ][ 0 ] != '-' ) { printf( "Argument %d should start with a '-'\n", i ); exit(0); } /* Now find the equals */ found_equals = 0; for( j = 1; j < strlen( argv[ i ] ); ++j ) { if( argv[ i ][ j ] == '=' ) { found_equals = j; param[ j - 1 ] = '\0'; } else { /* If we haven't come across the '=' yet we are in the param * If we have already found the '=' we are in the value */ if( found_equals == 0 ) { param[ j - 1 ] = argv[ i ][ j ]; } else { value[ j - found_equals - 1 ] = argv[ i ][ j ]; } } } /* No '=' found therefore we have an invalid argument */ if( found_equals == 0 ) { printf( "Could not find '=' in arg %d\n", i ); exit(0); } /* Ensure nul string termination then process this param, value pair */ value[ j - found_equals - 1 ] = '\0'; ProcessArg( param, value); } // If we do have a run control file, read in command-line arguments from // there. These will override any that were on the invocation command line. if (ics.run_control_file[0] != '\0') { ReadInitialConditionsFromRunControlFile(); } printf("done.\n"); }
void ReadInitialConditionsFromRunControlFile( void ) { // The format of the run control file is a bunch of lines with sets of // command-line arguments // For instance: // -repeats 2000 -sD 0.1 -sH 0.01 // -repeats 1000 -sD 0.01 -sH 0.1 // -repeats 100 -sM 4e-5 -sD 0.01 -sH 0.01 // This means: the first batch of tasks should do 2000 repeats each with sD // 0.1 and sH 0.01, the next should do 1000 repeats each with sD 0.01 and sH // 0.1, the should do 100 repeats each with sD 0.01, sH 0.01, and sM 4e-5. // This procedure goes through the run control file and finds the line // corresponding to the ics.run_number argument. Then this procedure // processes that line just like command-line arguments. The procedure also // modifies the output filename to reflect these specific parameter values, // and the run number. // NOTE!!! This procedure does not do any range checking on strings. This is // dangerous and can cause a buffer overflow. FILE *fp; char buffer[256]; char param[100]; char value[200]; int line_number, arg_number; char found_equals; // Whether we have found the equals sign char *argptr, *cur_ptr, *param_ptr, *value_ptr, *all_params_ptr; if (!(fp = fopen(ics.run_control_file, "r"))) { printf("#*** error! couldn't open run control file %s for reading\n", ics.run_control_file); exit(0); } line_number = 0; while (fgets(buffer, 256, fp) != NULL) { ++line_number; if (line_number == ics.run_number) { // We have found the line number we want! Process this line. arg_number = 0; all_params_ptr = ics.all_params; // Start the parameter value specification string with a leading // underscore *all_params_ptr++ = '_'; // Start parsing space-delimited arguments from the rest of the line for (argptr = strtok(buffer, " \n"); argptr != NULL; argptr = strtok(NULL, " \n")) { ++arg_number; cur_ptr = argptr; param_ptr = param; value_ptr = value; if( *cur_ptr != '-' ) { printf( "Argument %d on line %d of %s should start with a '-'\n", arg_number, line_number, ics.run_control_file ); exit(0); } /* Now find the equals */ found_equals = 0; // The initialization of this loop will move past the dash for (++cur_ptr; *cur_ptr; ++cur_ptr) { if (*cur_ptr == '=') { // We've come to the end of the parameter name found_equals = 1; // Terminate the parameter name *param_ptr = '\0'; // Delimit the parameter name in the parameter value specification // string with an underscore *all_params_ptr++ = '_'; // Get out of the for loop for reading the parameter name break; } else { // Keep copying the parameter name *param_ptr++ = *cur_ptr; *all_params_ptr++ = *cur_ptr; } } if (!found_equals || !*cur_ptr) { printf( "Could not find '=' in arg %d on line %d of %s\n", arg_number, line_number, ics.run_control_file ); exit(0); } int i = 0; // The initialization of this loop will move past the equals sign for (++cur_ptr; *cur_ptr; ++cur_ptr) { ++i; // Keep copying the value string *value_ptr++ = *cur_ptr; *all_params_ptr++ = *cur_ptr; } // Terminate the value string *value_ptr = '\0'; // Delimit the value in the parameter value specification string with // an underscore (note that this will leave a trailing underscore at // the end) *all_params_ptr++ = '_'; // Process the argument ProcessArg( param, value ); } // Terminate the parameter value specification string *all_params_ptr = '\0'; // Loop to the end of ics.filename for (cur_ptr = ics.filename; *cur_ptr; ++cur_ptr) { ; } // Now cur_ptr is pointing to the '\0' ending ics.filename // Concatenate the parameter value specification and run number to the // end of the filename // NOTE!!! This is dangerous and can cause a buffer overflow sprintf(cur_ptr, "%srcline_%05d", ics.all_params, ics.run_number); // Stop reading the run control file break; } } // If we never find the current job number in the whole run control file, we // won't process any command line arguments from the file; we will just use // the initial conditions specified on this program's own command line, or // the default values. fclose(fp); }