int main(int argc,char **argv){ char **newargv = (char**) calloc(argc + 4, sizeof(char*)), ltsmin[256]; int unused = parse_options(options,argc,argv), j = 0, i; sprintf(ltsmin, "%s/%s", BINDIR, "ltsmin"); newargv[j++] = "ltscmp"; #ifdef TESTLTSMIN newargv[j++] = "-cmp"; #endif if ((unused+requiredargs)!=argc || requiredopts>1) { printoptions(options); exit(1); } if (requiredopts==0) newargv[j++]="-s"; for (i=1;i<argc;i++) { if (strcmp(argv[i],"-b")) newargv[j++]=argv[i]; else { newargv[j++]= "-B"; newargv[j++]= "set2"; } } newargv[j]=NULL; /* for (i=0;i<j;i++) fprintf(stderr,"%s %s:\n",ltsmin, newargv[i]); */ if (execv(ltsmin, newargv)<0) Fatal(1,1,"Cannot execute %s", ltsmin); }
void printitem(pItem *item) { printf("procesname:%s restartcmd:%s\n", item->pname, item->restartcmd); printoptions(item->optlist); if (item->next == NULL) { printf("next is NULL\n"); } else { printf("next isnot null\n"); } }
int main (int argc, char **argv) { char *progname = basename (argv[0]); int exit_status = EXIT_SUCCESS; struct options options; int argi; memset (&options, 0, sizeof options); // set all opts FALSE // // Scan arguments and set flags. // opterr = FALSE; for (;;) { int option = getopt (argc, argv, "mns"); if (option == EOF) break; switch (option) { case 'm': options.moretitles = TRUE; break; case 'n': options.numberlines = TRUE; break; case 's': options.squeeze = TRUE; break; default : fflush (NULL); fprintf (stderr, "%s: -%c: invalid option\n", progname, optopt); fflush (NULL); exit_status = EXIT_FAILURE; }; }; // // Print options and operands. // printoptions (&options); for (argi = optind; argi < argc; ++argi) { printf ("argv[%d] operand = \"%s\"\n", argi, argv[argi]); }; return exit_status; }
/* parse command line & set command arguments. returns the index of * non-option arguments, -1 if there is an error. */ int parse_args(char **argv, int what, /* OF_CMDLINE or OF_SET */ int *setargsp) { static char cmd_opts[NELEM(options) + 3]; /* o:\0 */ static char set_opts[NELEM(options) + 5]; /* Ao;s\0 */ char *opts; char *array = (char *) 0; Getopt go; int i, optc, set, sortargs = 0, arrayset = 0; /* First call? Build option strings... */ if (cmd_opts[0] == '\0') { char *p, *q; /* see cmd_opts[] declaration */ strlcpy(cmd_opts, "o:", sizeof cmd_opts); p = cmd_opts + strlen(cmd_opts); /* see set_opts[] declaration */ strlcpy(set_opts, "A:o;s", sizeof set_opts); q = set_opts + strlen(set_opts); for (i = 0; i < NELEM(options); i++) { if (options[i].c) { if (options[i].flags & OF_CMDLINE) *p++ = options[i].c; if (options[i].flags & OF_SET) *q++ = options[i].c; } } *p = '\0'; *q = '\0'; } if (what == OF_CMDLINE) { opts = cmd_opts; } else opts = set_opts; ksh_getopt_reset(&go, GF_ERROR|GF_PLUSOPT); while ((optc = ksh_getopt(argv, &go, opts)) != -1) { set = (go.info & GI_PLUS) ? 0 : 1; switch (optc) { case 'A': arrayset = set ? 1 : -1; array = go.optarg; break; case 'o': if (go.optarg == (char *) 0) { /* lone -o: print options * * Note that on the command line, -o requires * an option (ie, can't get here if what is * OF_CMDLINE). */ printoptions(set); break; } i = option(go.optarg); if (i >= 0 && set == Flag(i)) /* Don't check the context if the flag * isn't changing - makes "set -o interactive" * work if you're already interactive. Needed * if the output of "set +o" is to be used. */ ; else if (i >= 0 && (options[i].flags & what)) change_flag((enum sh_flag) i, what, set); else { bi_errorf("%s: bad option", go.optarg); return -1; } break; case '?': return -1; default: /* -s: sort positional params (at&t ksh stupidity) */ if (what == OF_SET && optc == 's') { sortargs = 1; break; } for (i = 0; i < NELEM(options); i++) if (optc == options[i].c && (what & options[i].flags)) { change_flag((enum sh_flag) i, what, set); break; } if (i == NELEM(options)) { internal_errorf(1, "parse_args: `%c'", optc); return -1; /* not reached */ } } } if (!(go.info & GI_MINUSMINUS) && argv[go.optind] && (argv[go.optind][0] == '-' || argv[go.optind][0] == '+') && argv[go.optind][1] == '\0') { /* lone - clears -v and -x flags */ if (argv[go.optind][0] == '-' && !Flag(FPOSIX)) Flag(FVERBOSE) = Flag(FXTRACE) = 0; /* set skips lone - or + option */ go.optind++; } if (setargsp) /* -- means set $#/$* even if there are no arguments */ *setargsp = !arrayset && ((go.info & GI_MINUSMINUS) || argv[go.optind]); if (arrayset && (!*array || *skip_varname(array, false))) { bi_errorf("%s: is not an identifier", array); return -1; } if (sortargs) { for (i = go.optind; argv[i]; i++) ; qsortp((void **) &argv[go.optind], (size_t) (i - go.optind), xstrcmp); } if (arrayset) { set_array(array, arrayset, argv + go.optind); for (; argv[go.optind]; go.optind++) ; } return go.optind; }
/* Parse command line & set command arguments. Returns the index of * non-option arguments, -1 if there is an error. */ int parse_args(const char **argv, int what, /* OF_CMDLINE or OF_SET */ bool *setargsp) { static char cmd_opts[NELEM(options) + 5]; /* o:T:\0 */ static char set_opts[NELEM(options) + 6]; /* A:o;s\0 */ char set, *opts; const char *array = NULL; Getopt go; size_t i; int optc, sortargs = 0, arrayset = 0; /* First call? Build option strings... */ if (cmd_opts[0] == '\0') { char *p = cmd_opts, *q = set_opts; /* see cmd_opts[] declaration */ *p++ = 'o'; *p++ = ':'; #if !defined(MKSH_SMALL) || defined(TIOCSCTTY) *p++ = 'T'; *p++ = ':'; #endif /* see set_opts[] declaration */ *q++ = 'A'; *q++ = ':'; *q++ = 'o'; *q++ = ';'; *q++ = 's'; for (i = 0; i < NELEM(options); i++) { if (options[i].c) { if (options[i].flags & OF_CMDLINE) *p++ = options[i].c; if (options[i].flags & OF_SET) *q++ = options[i].c; } } *p = '\0'; *q = '\0'; } if (what == OF_CMDLINE) { const char *p = argv[0], *q; /* Set FLOGIN before parsing options so user can clear * flag using +l. */ if (*p != '-') for (q = p; *q; ) if (*q++ == '/') p = q; Flag(FLOGIN) = (*p == '-'); opts = cmd_opts; } else if (what == OF_FIRSTTIME) { opts = cmd_opts; } else opts = set_opts; ksh_getopt_reset(&go, GF_ERROR|GF_PLUSOPT); while ((optc = ksh_getopt(argv, &go, opts)) != -1) { set = (go.info & GI_PLUS) ? 0 : 1; switch (optc) { case 'A': if (what == OF_FIRSTTIME) break; arrayset = set ? 1 : -1; array = go.optarg; break; case 'o': if (what == OF_FIRSTTIME) break; if (go.optarg == NULL) { /* lone -o: print options * * Note that on the command line, -o requires * an option (ie, can't get here if what is * OF_CMDLINE). */ printoptions(set); break; } i = option(go.optarg); if ((enum sh_flag)i == FARC4RANDOM) { warningf(true, "Do not use set ±o arc4random," " it will be removed in the next version" " of mksh!"); return (0); } if ((i != (size_t)-1) && set == Flag(i)) /* Don't check the context if the flag * isn't changing - makes "set -o interactive" * work if you're already interactive. Needed * if the output of "set +o" is to be used. */ ; else if ((i != (size_t)-1) && (options[i].flags & what)) change_flag((enum sh_flag)i, what, set); else { bi_errorf("%s: bad option", go.optarg); return (-1); } break; #if !defined(MKSH_SMALL) || defined(TIOCSCTTY) case 'T': if (what != OF_FIRSTTIME) break; #ifndef TIOCSCTTY errorf("no TIOCSCTTY ioctl"); #else change_flag(FTALKING, OF_CMDLINE, 1); chvt(go.optarg); break; #endif #endif case '?': return (-1); default: if (what == OF_FIRSTTIME) break; /* -s: sort positional params (AT&T ksh stupidity) */ if (what == OF_SET && optc == 's') { sortargs = 1; break; } for (i = 0; i < NELEM(options); i++) if (optc == options[i].c && (what & options[i].flags)) { change_flag((enum sh_flag)i, what, set); break; } if (i == NELEM(options)) internal_errorf("parse_args: '%c'", optc); } } if (!(go.info & GI_MINUSMINUS) && argv[go.optind] && (argv[go.optind][0] == '-' || argv[go.optind][0] == '+') && argv[go.optind][1] == '\0') { /* lone - clears -v and -x flags */ if (argv[go.optind][0] == '-') Flag(FVERBOSE) = Flag(FXTRACE) = 0; /* set skips lone - or + option */ go.optind++; } if (setargsp) /* -- means set $#/$* even if there are no arguments */ *setargsp = !arrayset && ((go.info & GI_MINUSMINUS) || argv[go.optind]); if (arrayset && (!*array || *skip_varname(array, false))) { bi_errorf("%s: is not an identifier", array); return (-1); } if (sortargs) { for (i = go.optind; argv[i]; i++) ; qsort(&argv[go.optind], i - go.optind, sizeof(void *), xstrcmp); } if (arrayset) go.optind += set_array(array, arrayset > 0 ? true : false, argv + go.optind); return (go.optind); }
/* * Parse command line and set command arguments. Returns the index of * non-option arguments, -1 if there is an error. */ int parse_args(const char **argv, /* OF_CMDLINE or OF_SET */ int what, bool *setargsp) { static const char cmd_opts[] = #define SHFLAGS_NOT_SET #define SHFLAGS_OPTCS #include "sh_flags.gen" #undef SHFLAGS_NOT_SET ; static const char set_opts[] = #define SHFLAGS_NOT_CMD #define SHFLAGS_OPTCS #include "sh_flags.gen" #undef SHFLAGS_NOT_CMD ; bool set; const char *opts; const char *array = NULL; Getopt go; size_t i; int optc, arrayset = 0; bool sortargs = false; bool fcompatseen = false; if (what == OF_CMDLINE) { const char *p = argv[0], *q; /* * Set FLOGIN before parsing options so user can clear * flag using +l. */ if (*p != '-') for (q = p; *q; ) if (*q++ == '/') p = q; Flag(FLOGIN) = (*p == '-'); opts = cmd_opts; } else if (what == OF_FIRSTTIME) { opts = cmd_opts; } else opts = set_opts; ksh_getopt_reset(&go, GF_ERROR|GF_PLUSOPT); while ((optc = ksh_getopt(argv, &go, opts)) != -1) { set = tobool(!(go.info & GI_PLUS)); switch (optc) { case 'A': if (what == OF_FIRSTTIME) break; arrayset = set ? 1 : -1; array = go.optarg; break; case 'o': if (what == OF_FIRSTTIME) break; if (go.optarg == NULL) { /* * lone -o: print options * * Note that on the command line, -o requires * an option (ie, can't get here if what is * OF_CMDLINE). */ printoptions(set); break; } i = option(go.optarg); if ((i == FPOSIX || i == FSH) && set && !fcompatseen) { /* * If running 'set -o posix' or * 'set -o sh', turn off the other; * if running 'set -o posix -o sh' * allow both to be set though. */ Flag(FPOSIX) = 0; Flag(FSH) = 0; fcompatseen = true; } if ((i != (size_t)-1) && (set ? 1U : 0U) == Flag(i)) /* * Don't check the context if the flag * isn't changing - makes "set -o interactive" * work if you're already interactive. Needed * if the output of "set +o" is to be used. */ ; else if ((i != (size_t)-1) && (OFF(i) & what)) change_flag((enum sh_flag)i, what, set); else { bi_errorf("%s: %s", go.optarg, "bad option"); return (-1); } break; #ifdef KSH_CHVT_FLAG case 'T': if (what != OF_FIRSTTIME) break; #ifndef KSH_CHVT_CODE errorf("no TIOCSCTTY ioctl"); #else change_flag(FTALKING, OF_CMDLINE, true); chvt(&go); break; #endif #endif case '?': return (-1); default: if (what == OF_FIRSTTIME) break; /* -s: sort positional params (AT&T ksh stupidity) */ if (what == OF_SET && optc == 's') { sortargs = true; break; } for (i = 0; i < NELEM(options); i++) if (optc == OFC(i) && (what & OFF(i))) { change_flag((enum sh_flag)i, what, set); break; } if (i == NELEM(options)) internal_errorf("parse_args: '%c'", optc); } } if (!(go.info & GI_MINUSMINUS) && argv[go.optind] && (argv[go.optind][0] == '-' || argv[go.optind][0] == '+') && argv[go.optind][1] == '\0') { /* lone - clears -v and -x flags */ if (argv[go.optind][0] == '-') { Flag(FVERBOSE) = 0; change_xtrace(0, false); } /* set skips lone - or + option */ go.optind++; } if (setargsp) /* -- means set $#/$* even if there are no arguments */ *setargsp = !arrayset && ((go.info & GI_MINUSMINUS) || argv[go.optind]); if (arrayset) { const char *ccp = NULL; if (*array) ccp = skip_varname(array, false); if (!ccp || !(!ccp[0] || (ccp[0] == '+' && !ccp[1]))) { bi_errorf("%s: %s", array, "is not an identifier"); return (-1); } } if (sortargs) { for (i = go.optind; argv[i]; i++) ; qsort(&argv[go.optind], i - go.optind, sizeof(void *), xstrcmp); } if (arrayset) go.optind += set_array(array, tobool(arrayset > 0), argv + go.optind); return (go.optind); }