int cCommandlineParser::isSet( const char *name ) const { int n = findOpt( name ); if (n >= 0) { return opt[n].isSet; } return 0; }
const char * cCommandlineParser::getStr( const char *name ) const { int n = findOpt( name ); if (n >= 0) { if (opt[n].type != CMDOPT_STR) { COMP_ERR("requested commandline argument '%s' is not of type string!"); } return opt[n].dfltStr; } else { COMP_ERR("string commandline argument '%s' not found in commandline parser object!",name); } return 0; }
double cCommandlineParser::getDouble( const char *name ) const { int n = findOpt( name ); if (n >= 0) { if (opt[n].type != CMDOPT_DOUBLE) { COMP_ERR("requested commandline argument '%s' is not of type double!"); } return opt[n].dfltDouble; } else { COMP_ERR("floating point commandline argument '%s' not found in commandline parser object!",name); } return 0; }
int cCommandlineParser::getInt( const char *name ) const { int n = findOpt( name ); if (n >= 0) { if (opt[n].type != CMDOPT_INT) { COMP_ERR("requested commandline argument '%s' is not of type int!"); } return opt[n].dfltInt; } else { COMP_ERR("int commandline argument '%s' not found in commandline parser object!",name); } return 0; }
int cCommandlineParser::doParse(int ignDupl, int ignUnknown) { int expectOpt = 1; int expectNum = 0; int showusage = 0; // foreach argv element with(--) or (-), find the thing in opt int i,n; SMILE_DBG(3,"about to parse %i commandline options ",argc-1); for (i=1; i<argc; i++) { SMILE_DBG(5,"parsing option %i : '%s'",i,argv[i]); if (argv[i][0] == '-') { // option...? if ((argv[i][1] == 'h')&&(argv[i][2] == 0)) { // usage (-h) showusage = 1; } else { if (!expectNum) { char *o = argv[i]+1; if (o[0] == '-') o++; n = findOpt(o); if (n >= 0) { // found! if (opt[n].isSet == 1) { // option specified twice...! if (!ignDupl) SMILE_ERR(0,"duplicate option '%s' on commandline (ignoring duplicate!) ",o); } else { // now look for value.... if (i+1 < argc) { int optCand = 0; if (argv[i+1][0] == '-') optCand = 1; // argument might be an option... or a negative numeric int v; double vd; char *o2; char *ep = NULL; // parse value according to type... switch (opt[n].type) { case CMDOPT_BOOLEAN: if (!optCand) { // explicit value given if ((argv[i][0] == '0')||(argv[i][0] == 'N')||(argv[i][0] == 'n')||(argv[i][0] == 'F')||(argv[i][0] == 'f')) opt[n].dfltInt = 0; else if ((argv[i][0] == '1')||(argv[i][0] == 'Y')||(argv[i][0] == 'y')||(argv[i][0] == 'T')||(argv[i][0] == 't')) opt[n].dfltInt = 1; i++; // next parameter of commandline } else { if (opt[n].dfltInt == 0) opt[n].dfltInt = 1; // invert boolean default, if option is given else opt[n].dfltInt = 0; opt[n].isSet = 1; } break; case CMDOPT_INT: // parse value: errno=0; v = strtol(argv[i+1],NULL,10); // values base 10... TODO: support hex if ( (optCand) && ((v==0)&&(errno==EINVAL)) ) { // option... no value given if (opt[n].argMandatory) COMP_ERR("option '%s' requires an argument!",o); else { opt[n].isSet = 1; } } else { if ((v==0)&&(errno==EINVAL)) COMP_ERR("invalid value specified for commandline option '%s'",o); if ((v==0)&&(errno==ERANGE)) COMP_ERR("value specified for commandline option '%s' is out of range",o); opt[n].dfltInt = v; opt[n].dfltDouble = (double)v; opt[n].isSet = 1; i++; // next parameter of commandline } break; case CMDOPT_DOUBLE: // parse value: vd = strtod(argv[i+1],&ep); if ( (optCand) && ((vd==0.0)&&(ep==argv[i+1])) ) { // option... no value given if (opt[n].argMandatory) COMP_ERR("option '%s' requires an argument!",o); else { opt[n].isSet = 1; } } else { if ((vd==0)&&(ep==argv[i+1])) COMP_ERR("invalid value specified for commandline option '%s'",o); //if ((v==0)&&(errno==ERANGE)) COMP_ERR("value specified for commandline option '%s' is out of range",o); opt[n].dfltDouble = vd; opt[n].dfltInt = (int)vd; opt[n].isSet = 1; i++; // next parameter of commandline } break; case CMDOPT_STR: if (optCand) { // bad situation... look for option o2 = argv[i+1]+1; if (o2[0] == '-') o2++; int tmp = findOpt(o2); if (tmp >= 0) { // next option if (opt[n].argMandatory) COMP_ERR("option '%s' requires an argument!",o); else { opt[n].isSet = 1; } } else { // string value i++; // next parameter of commandline if (opt[n].dfltStr != NULL) free(opt[n].dfltStr); opt[n].dfltStr = strdup(argv[i]); opt[n].isSet = 1; } } else { // string value i++; // next parameter of commandline if (opt[n].dfltStr != NULL) free(opt[n].dfltStr); opt[n].dfltStr = strdup(argv[i]); opt[n].isSet = 1; } break; default: COMP_ERR("unknown option type (%i) encountered... this actually cannot be!",opt[n].type); } } else { // no value... check if one is required... if (opt[n].argMandatory) COMP_ERR("option '%s' requires an argument!",o); else { // process boolean option, if boolean, else ignore... if (opt[n].type == CMDOPT_BOOLEAN) { if (opt[n].dfltInt == 0) opt[n].dfltInt = 1; // invert boolean default, if option is given else opt[n].dfltInt = 0; } opt[n].isSet = 1; } } } } else { if (!ignUnknown) SMILE_ERR(0,"doParse: unknown option '%s' on commandline!",argv[i]); } } else { // parse as number...? // not required... not implemented... } } // if option == -h } } // TODO: from argv[0] get correct 'binname' to pass to showUsage if (showusage) { showUsage(); return -1; } // now check if all mandatory parameters have been specified... int err = 0; for (i=0; i<N; i++) { if ( (opt[i].isSet == 0)&&(opt[i].isMandatory == 1) ) { SMILE_ERR(0,"mandatory commandline option '%s' was not specified!",opt[i].name); err = 1; } } if (err) { showUsage(); SMILE_ERR(0,"missing options on the commandline!"); return -1; } return 0; }
uint32_t calc(int argc, char* argv[]) { calcErrFlag = 0; int now = 0; for(int i = 1; i < argc; ++i) for(int j = 0; argv[i][j]; ++j) buf[now++] = argv[i][j]; buf[now] = 0; cprintf("%s : ", buf); now = 0; for(int i = 0; buf[i]; ++i) { int flag = 0; for(int j = 0; j < NOPERATORS; ++j) { if(buf[i] == operator[j].symbol[0]) { flag = 1; calcBuf[now++] = buf[i]; if(buf[i + 1] == '<' || buf[i + 1] == '>' ) break; calcBuf[now++] = ' '; break; } } if(flag) continue; if('0' <= buf[i] && buf[i] <= '9') { while( buf[i] == 'x' || '0' <= buf[i] && buf[i] <= '9' || 'a' <= buf[i] && buf[i] <= 'f' || 'A' <= buf[i] && buf[i] <= 'F' ) { calcBuf[now++] = buf[i++]; } calcBuf[now++] = ' '; i --; continue; } flag = *strfind(reservedChars, buf[i]); if(flag) { cprintf("Invalid character: %c\n", buf[i]); calcErrFlag = -1; return -1; } while(1) { flag = *strfind(reservedChars, buf[i]); if(!flag && buf[i]) calcBuf[now++] = buf[i++]; else { calcBuf[now++] = ' ', i--; break; } } } calcBuf[now] = 0; // cprintf("%s\n", calcBuf); char** calcComp = split(calcBuf); stackn = 0; valuen = 0; int m = 0; for(int i = 0; calcComp[i]; ++i) { m = i; int j; if((j = findOpt(calcComp[i])) != -1) { if(operator[j].type == OPT_LBRACE || operator[j].type == OPT_RBRACE) calcMark[i] = operator[j].type; else if(i == 0 || calcMark[i-1] == OPT_UNO || calcMark[i-1] == OPT_BINARY || calcMark[i-1] == OPT_LBRACE) calcMark[i] = OPT_UNO; else calcMark[i] = OPT_BINARY; continue; } if('0' <= calcComp[i][0] && calcComp[i][0] <= '9') { calcMark[i] = OPT_VALUE; continue; } calcMark[i] = OPT_SYMBOL; } /* for(int i = 0; i <= m; ++i) cprintf("%d ", calcMark[i]); cprintf("\n"); */ for(int i = m; i >= 0; --i) { //printCalcStack(); int j; struct DebugInfo* p; switch(calcMark[i]) { case OPT_UNO: j = findSpecOpt(calcComp[i], OPT_UNO); if(j == -1) { cprintf("Invalid expression!\n"); calcErrFlag = -1; return -1; } calcStack[++stackn] = operator[j]; doCalc(); break; case OPT_BINARY: j = findSpecOpt(calcComp[i], OPT_BINARY); if(j == -1) { cprintf("Invalid expression!\n"); calcErrFlag = -1; return -1; } while( stackn > 0 && calcStack[stackn].type == OPT_BINARY && calcStack[stackn].priority > operator[j].priority) doCalc(); calcStack[++stackn] = operator[j]; break; case OPT_LBRACE: while(calcStack[stackn].type != OPT_RBRACE) doCalc(); stackn--; break; case OPT_RBRACE: calcStack[++stackn].type = OPT_RBRACE; break; case OPT_VALUE: calcValue[++valuen] = strToInt(calcComp[i]); break; case OPT_SYMBOL: p = findSymbol(pinfo.pc, calcComp[i]); if(p == 0) { cprintf("Cannot find symbol: %s\n", calcComp[i]); calcErrFlag = -1; return -1; } uint32_t vaddr = p->vaddr; int type = 0; if(p->type != N_GSYM) type = 1; subArgv[0] = vaddr; subArgv[1] = buf; subArgv[2] = type; subArgv[3] = 0; int result = doSysDebug(DEBUG_PRINT, subArgv); if(result < 0) { cprintf("%s", subArgv[1]); calcErrFlag = -1; return -1; } calcValue[++valuen] = strToInt(subArgv[1]); break; } } while(stackn) { //printCalcStack(); doCalc(); } cprintf("%d\n", calcValue[1]); return calcValue[1]; }
int main(int argc, char *argv[]) { int backup = YES; /* Backup files when opening? */ int i; char *fileName = NULL; for (i = 1; i < argc; i++) { if (argv[i][0] == '-') { switch (findOpt(argv[i] + 1)) { case HE_HELP: printf("he [<file>] [-nobackup] [-batch]\n"); help(); quit(0); break; case HE_BATCH: he_batch = YES; break; case HE_REMOTE: he_remote = YES; break; case HE_NOBACKUP: backup = NO; break; case HE_BACKUP: backup = YES; break; case HE_NOTFOUND: unkOpt(argv[i]); quit(1); /* does not return */ break; case HE_AMBIG: ambigOpt(argv[i]); quit(1); break; default: irrOpt(argv[i]); quit(1); /* does not return */ break; } } else { /* Must be a filename */ if (!fileName) fileName = argv[i]; else fprintf(stderr, "Single file only. %s not open.\n", argv[i]); } } /* if there is a file name given in the command line, open it */ if (fileName) he_status = openFile(fileName, backup); /* read, execute loop */ cmdLoop(); if (fileOpen()) closeFile(YES); /* close with keep */ quit(0); return 0; }
void ToyStream::loadFile(QString filePath) { QRegExp isLogoLine("(\\|*)(\\s*)\xff"); QRegExp isToyLine("^\\s*\\{(\\d+)\\}\\{(\\d+)\\}(.+)\n"); QRegExp findOpt("\\{(.*)\\}"); QRegExp findPipe("([^|]*)(\\|+)([^|]*)"); QRegExp OptColor("\\{c:\\$([0-9A-F]+):?\\$?([0-9A-F]*)\\}"); QRegExp OptPos("\\{o:(\\d+),(\\d+)\\}"); QFile toyfile(filePath); if (!toyfile.open(QIODevice::ReadOnly | QIODevice::Text)) { cerr << "Can't read : " << filePath << "\n"; exit(1); } while (!toyfile.atEnd()) { QByteArray line = toyfile.readLine(); if (isToyLine.exactMatch(line)) { QString optAndText = isToyLine.cap(3); // Syl if (isLogoLine.indexIn(optAndText) != -1) { ToySyl newsyl; newsyl.pos = isLogoLine.cap(2).size(); findPipe.exactMatch(optAndText); newsyl.nbpipe = findPipe.cap(2).size(); newsyl.length = findPipe.cap(3).size(); newsyl.start = isToyLine.cap(1).toInt(); newsyl.stop = isToyLine.cap(2).toInt(); syl.insert(newsyl); //show_ToySyl(newsyl); } //Text else { ToyText newtext; newtext.start = isToyLine.cap(1).toInt(); newtext.stop = isToyLine.cap(2).toInt(); newtext.nbpipe = 0; newtext.posx = -1; newtext.posy = -1; // Colors if (OptColor.indexIn(optAndText) != -1) { newtext.color1 = toycolor2QColor(OptColor.cap(1)); if (!OptColor.cap(2).isEmpty()) newtext.color2 = toycolor2QColor(OptColor.cap(2)); } // Pos if (OptPos.indexIn(optAndText) != -1) { newtext.posx = OptPos.cap(1).toInt(); newtext.posy = OptPos.cap(2).toInt(); } optAndText.replace(findOpt, ""); if (findPipe.exactMatch(optAndText)) { newtext.text = findPipe.cap(3); newtext.nbpipe = findPipe.cap(2).size(); } else { newtext.text = optAndText; } text.insert(newtext); } } } }