static int is_bool_opt(const char *opt, const char *name, int *val) { const char *optval; if ( (optval=is_longopt(opt,name)) ) { if ( *optval == EOS || strcasecmp(optval, "true") == 0 || strcasecmp(optval, "yes") == 0 || strcasecmp(optval, "y") == 0 ) { *val = TRUE; return TRUE; } if ( strcasecmp(optval, "false") == 0 || strcasecmp(optval, "no") == 0 || strcasecmp(optval, "n") == 0 ) { *val = FALSE; return TRUE; } return -1; } else if ( strncmp(opt, "no-", 3) == 0 && (optval=is_longopt(opt+3,name)) ) { if ( *optval == EOS ) { *val = FALSE; return TRUE; } return -1; } else if ( strncmp(opt, "no", 2) == 0 && (optval=is_longopt(opt+2,name)) ) { if ( *optval == EOS ) { *val = FALSE; return TRUE; } return -1; } return FALSE; }
int optparse_long(struct optparse *options, const struct optparse_long *longopts, int *longindex) { char *option = options->argv[options->optind]; if (option == 0) { return -1; } else if (is_shortopt(option)) { return long_fallback(options, longopts, longindex); } else if (!is_longopt(option)) { if (options->permute) { int index = options->optind; options->optind++; int r = optparse_long(options, longopts, longindex); permute(options, index); options->optind--; return r; } else { return -1; } } /* Parse as long option. */ options->errmsg[0] = '\0'; options->optopt = 0; options->optarg = 0; option += 2; /* skip "--" */ options->optind++; for (int i = 0; !longopts_end(longopts, i); i++) { const char *name = longopts[i].longname; if (longopts_match(name, option)) { if (longindex) *longindex = i; options->optopt = longopts[i].shortname; char *arg = longopts_arg(option); if (longopts[i].argtype == OPTPARSE_NONE && arg != 0) { return opterror(options, MSG_TOOMANY, name); } if (arg != 0) { options->optarg = arg; } else if (longopts[i].argtype == OPTPARSE_REQUIRED) { options->optarg = options->argv[options->optind++]; if (options->optarg == 0) return opterror(options, MSG_MISSING, name); } return options->optopt; } } return opterror(options, MSG_INVALID, option); }
static int parseCommandLineOptions(int argc0, char **argv, int *compile) { GET_LD int argc = argc0; for( ; argc > 0 && (argv[0][0] == '-' || argv[0][0] == '+'); argc--, argv++ ) { char *s = &argv[0][1]; if ( streq(s, "-" ) ) /* swipl <plargs> -- <app-args> */ { break; } if ( streq(s, "tty") ) /* +/-tty */ { if ( s[-1] == '+' ) setPrologFlagMask(PLFLAG_TTY_CONTROL); else clearPrologFlagMask(PLFLAG_TTY_CONTROL); continue; } else if ( isoption(s, "nosignals") ) { clearPrologFlagMask(PLFLAG_SIGNALS); continue; } else if ( isoption(s, "nodebug") ) { clearPrologFlagMask(PLFLAG_DEBUGINFO); continue; } else if ( streq(s, "-quiet") ) { GD->options.silent = TRUE; continue; } if ( *s == '-' ) { const char *optval; s++; if ( (optval=is_longopt(s, "pldoc")) ) { GD->options.pldoc_server = store_string(optval); } else if ( is_longopt(s, "home") ) { /* already handled */ #ifdef __WINDOWS__ } else if ( (optval=is_longopt(s, "win_app")) ) { GD->options.win_app = TRUE; #endif } else if ( (optval=is_longopt(s, "traditional")) ) { setTraditional(); } continue; /* don't handle --long=value */ } while(*s) { switch(*s) { case 'd': if (argc > 1) { prolog_debug_from_string(argv[1], TRUE); argc--, argv++; } else return -1; break; case 'p': optionList(&GD->options.search_paths); break; case 'O': GD->cmdline.optimise = TRUE; /* see initFeatures() */ break; case 'x': case 'o': optionString(GD->options.compileOut); break; case 'f': optionString(GD->options.initFile); break; case 'F': optionString(GD->options.systemInitFile); break; case 'l': case 's': optionList(&GD->options.scriptFiles); break; case 'g': optionString(GD->options.goal); break; case 't': optionString(GD->options.topLevel); break; case 'c': *compile = TRUE; break; case 'b': GD->bootsession = TRUE; break; case 'q': GD->options.silent = TRUE; break; case 'L': case 'G': case 'T': case 'A': case 'H': { uintptr_t size = memarea_limit(&s[1]); if ( size == MEMAREA_INVALID_SIZE ) return -1; switch(*s) { case 'L': GD->options.localSize = size; goto next; case 'G': GD->options.globalSize = size; goto next; case 'T': GD->options.trailSize = size; goto next; case 'H': case 'A': Sdprintf("% Warning: -%csize is no longer supported\n", *s); goto next; } } } s++; } next:; } return argc0-argc; }