bool bcc_cmdlinet::parse(int argc, const char **argv) { assert(argc>0); add_arg(argv[0]); for(int i=1; i<argc; i++) { std::string argv_i=argv[i]; // file? if(argv_i=="-" || !has_prefix(argv_i, "-")) { add_infile_arg(argv_i); continue; } bool found=false; // separated only, and also allow concatenation with "=" for(const char **o=goto_bcc_options_with_argument; *o!=nullptr && !found; ++o) { std::string os(*o); if(argv_i==os) // separated { found=true; if(i!=argc-1) { set(argv_i, argv[i+1]); ++i; } else set(argv_i, ""); } else if(has_prefix(argv_i, os+"=")) // concatenated with "=" { found=true; set(os, argv_i.substr(os.size()+1)); } } // goto-bcc-only command line argument found if(found) continue; // add to new_argv add_arg(argv_i); // without argument; also store in cmdlinet if(in_list(argv_i.c_str(), bcc_options_without_argument)) { set(argv_i); continue; } for(const char **o=bcc_options_with_argument; *o!=nullptr && !found; ++o) { std::string os(*o); if(argv_i==os) // separated { found=true; if(i!=argc-1) { set(argv_i, argv[i+1]); add_arg(argv[i+1]); ++i; } else set(argv_i, ""); } else if(has_prefix(argv_i, os)) { found=true; set(os, argv[i]+os.size()); } } if(!found) { // unrecognized option std::cerr << "Warning: uninterpreted bcc option '" << argv_i << "'\n"; } } return false; }
bool gcc_cmdlinet::parse(int argc, const char **argv) { assert(argc>0); add_arg(argv[0]); for(int i=1; i<argc; i++) { std::string argv_i=argv[i]; // options file? if(has_prefix(argv_i, "@")) { // TODO continue; } // file? if(argv_i=="-" || !has_prefix(argv_i, "-")) { add_infile_arg(argv_i); continue; } // add to new_argv add_arg(argv_i); // also store in cmdlinet if(has_prefix(argv_i, "-f")) // f-options { set(argv_i); } else if(has_prefix(argv_i, "-W")) // W-options { // "Wp,..." is s special case. These are to pass stuff // to the preprocessor. if(has_prefix(argv_i, "-Wp,")) { std::string value=std::string(argv[i]+4); set("-WP,", value); } else set(argv_i); } else if(has_prefix(argv_i, "-m")) // m-options { set(argv_i); } else if(in_list(argv[i], gcc_options_without_argument)) // without argument { set(argv_i); } else { bool found=false; // separated only, and also allow concatenation with "=" for(const char **o=gcc_options_with_separated_argument; *o!=NULL && !found; o++) { if(argv_i==*o) // separated { found=true; if(i!=argc-1) { set(argv_i, argv[i+1]); add_arg(argv[i+1]); i++; } else set(argv_i, ""); } else if(has_prefix(argv_i, std::string(*o)+"=")) // concatenated with "=" { found=true; set(*o, argv[i]+strlen(*o)+1); } } // concatenated _or_ separated, e.g., -I for(const char **o=gcc_options_with_argument; *o!=NULL && !found; o++) { if(argv_i==*o) // separated { found=true; if(i!=argc-1) { set(argv_i, argv[i+1]); add_arg(argv[i+1]); i++; } else set(argv_i, ""); } else if(has_prefix(argv_i, *o)) // concatenated { found=true; set(*o, argv[i]+strlen(*o)); } } // concatenated only for(const char **o=gcc_options_with_concatenated_argument; *o!=NULL && !found; o++) { if(has_prefix(argv_i, *o)) // concatenated { found=true; set(*o, argv[i]+strlen(*o)); } } if(!found) { // unrecognized option std::cerr << "Warning: uninterpreted gcc option '" << argv[i] << "'" << std::endl; } } } return false; }
bool gcc_cmdlinet::parse_arguments( const argst &args, bool in_spec_file) { for(argst::const_iterator it=args.begin(); it!=args.end(); ++it) { const std::string &argv_i=*it; // options file? if(has_prefix(argv_i, "@")) { std::ifstream opts_file(argv_i.substr(1)); std::string line; while(std::getline(opts_file, line)) { // erase leading whitespace line.erase(0, line.find_first_not_of("\t ")); if(!line.empty()) parse_specs_line(line); } continue; } // file? if(argv_i=="-" || !has_prefix(argv_i, "-")) { if(!in_spec_file) add_infile_arg(argv_i); continue; } if(!in_spec_file) { argst::const_iterator next=it; ++next; bool found=false; if(in_list(argv_i.c_str(), goto_cc_options_without_argument)) // without argument { set(argv_i); found=true; } // separated only, and also allow concatenation with "=" for(const char **o=goto_cc_options_with_separated_argument; *o!=nullptr && !found; ++o) { if(argv_i==*o) // separated { found=true; if(next!=args.end()) { set(argv_i, *next); ++it; } else set(argv_i, ""); } // concatenated with "=" else if(has_prefix(argv_i, std::string(*o)+"=")) { found=true; set(*o, argv_i.substr(strlen(*o)+1)); } } if(found) continue; // add to new_argv add_arg(argv_i); } // also store in cmdlinet if(has_prefix(argv_i, "-f")) // f-options { set(argv_i); } else if(has_prefix(argv_i, "-W")) // W-options { // "Wp,..." is s special case. These are to pass stuff // to the preprocessor. if(has_prefix(argv_i, "-Wp,")) { std::string value=argv_i.substr(4); set("-WP,", value); } else set(argv_i); } else if(has_prefix(argv_i, "-m")) // m-options { // these sometimes come with a value separated by '=', e.g., // -march=cpu_type std::size_t equal_pos=argv_i.find('='); if(equal_pos==std::string::npos) set(argv_i); // no value else set(argv_i.substr(0, equal_pos), argv_i.substr(equal_pos+1)); } // without argument else if(in_list(argv_i.c_str(), gcc_options_without_argument)) { set(argv_i); } else { argst::const_iterator next=it; ++next; bool found=false; // separated only, and also allow concatenation with "=" for(const char **o=gcc_options_with_separated_argument; *o!=nullptr && !found; ++o) { if(argv_i==*o) // separated { found=true; if(next!=args.end()) { set(argv_i, *next); if(!in_spec_file) add_arg(*next); ++it; } else set(argv_i, ""); } // concatenated with "=" else if(has_prefix(argv_i, std::string(*o)+"=")) { found=true; set(*o, argv_i.substr(strlen(*o)+1)); } } // concatenated _or_ separated, e.g., -I for(const char **o=gcc_options_with_argument; *o!=nullptr && !found; ++o) { if(argv_i==*o) // separated { found=true; if(next!=args.end()) { set(argv_i, *next); if(!in_spec_file) add_arg(*next); ++it; } else set(argv_i, ""); } else if(has_prefix(argv_i, *o)) // concatenated { found=true; set(*o, argv_i.substr(strlen(*o))); } } // concatenated only for(const char **o=gcc_options_with_concatenated_argument; *o!=nullptr && !found; ++o) { if(has_prefix(argv_i, *o)) // concatenated { found=true; set(*o, argv_i.substr(strlen(*o))); } } if(!found) { // unrecognized option std::cerr << "Warning: uninterpreted gcc option '" << argv_i << "'\n"; } } } return false; }