diff options
author | Rob Landley <rob@landley.net> | 2015-09-11 16:35:14 -0500 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2015-09-11 16:35:14 -0500 |
commit | e5354ca12a232b3f97726214254a868771cb70d1 (patch) | |
tree | ba373c24ede64b8a18f11a02cf834ce99aa586bf | |
parent | d067571abb2b7934f3350c90ca891beb987c68fd (diff) | |
download | toybox-e5354ca12a232b3f97726214254a868771cb70d1.tar.gz |
Replace toys.exithelp with help_exit() in lib.
-rw-r--r-- | lib/args.c | 24 | ||||
-rw-r--r-- | lib/help.c | 6 | ||||
-rw-r--r-- | lib/lib.c | 18 | ||||
-rw-r--r-- | lib/lib.h | 3 | ||||
-rw-r--r-- | main.c | 7 | ||||
-rw-r--r-- | scripts/config2help.c | 2 | ||||
-rw-r--r-- | toys.h | 1 | ||||
-rw-r--r-- | toys/lsb/killall.c | 5 | ||||
-rw-r--r-- | toys/other/blockdev.c | 5 | ||||
-rw-r--r-- | toys/other/help.c | 4 | ||||
-rw-r--r-- | toys/other/ifconfig.c | 15 | ||||
-rw-r--r-- | toys/other/losetup.c | 6 | ||||
-rw-r--r-- | toys/other/lsattr.c | 19 | ||||
-rw-r--r-- | toys/other/netcat.c | 8 | ||||
-rw-r--r-- | toys/pending/brctl.c | 10 | ||||
-rw-r--r-- | toys/pending/crontab.c | 11 | ||||
-rw-r--r-- | toys/pending/dd.c | 5 | ||||
-rw-r--r-- | toys/pending/fdisk.c | 12 | ||||
-rw-r--r-- | toys/pending/groupadd.c | 6 | ||||
-rw-r--r-- | toys/pending/ip.c | 36 | ||||
-rw-r--r-- | toys/pending/ipcrm.c | 5 | ||||
-rw-r--r-- | toys/pending/ipcs.c | 5 | ||||
-rw-r--r-- | toys/pending/modprobe.c | 3 | ||||
-rw-r--r-- | toys/pending/pgrep.c | 12 | ||||
-rw-r--r-- | toys/pending/route.c | 29 | ||||
-rw-r--r-- | toys/pending/tftpd.c | 5 | ||||
-rw-r--r-- | toys/pending/useradd.c | 5 | ||||
-rw-r--r-- | toys/posix/kill.c | 5 | ||||
-rw-r--r-- | www/code.html | 3 |
29 files changed, 102 insertions, 173 deletions
@@ -136,7 +136,7 @@ static int gotflag(struct getoptflagstate *gof, struct opts *opt) // Did we recognize this option? if (!opt) { if (gof->noerror) return 1; - error_exit("Unknown option %s", gof->arg); + help_exit("Unknown option %s", gof->arg); } // Might enabling this switch off something else? @@ -163,7 +163,7 @@ static int gotflag(struct getoptflagstate *gof, struct opts *opt) if (opt == bad || !(i & toys.optflags)) continue; if (toys.optflags & bad->dex[2]) break; } - error_exit("No '%c' with '%c'", opt->c, bad->c); + help_exit("No '%c' with '%c'", opt->c, bad->c); } // Does this option take an argument? @@ -187,10 +187,10 @@ static int gotflag(struct getoptflagstate *gof, struct opts *opt) char *s = "Missing argument to "; struct longopts *lo; - if (opt->c != -1) error_exit("%s-%c", s, opt->c); + if (opt->c != -1) help_exit("%s-%c", s, opt->c); for (lo = gof->longopts; lo->opt != opt; lo = lo->next); - error_exit("%s--%.*s", s, lo->len, lo->str); + help_exit("%s--%.*s", s, lo->len, lo->str); } if (type == ':') *(opt->arg) = (long)arg; @@ -204,8 +204,8 @@ static int gotflag(struct getoptflagstate *gof, struct opts *opt) } else if (type == '#' || type == '-') { long l = atolx(arg); if (type == '-' && !ispunct(*arg)) l*=-1; - if (l < opt->val[0].l) error_exit("-%c < %ld", opt->c, opt->val[0].l); - if (l > opt->val[1].l) error_exit("-%c > %ld", opt->c, opt->val[1].l); + if (l < opt->val[0].l) help_exit("-%c < %ld", opt->c, opt->val[0].l); + if (l > opt->val[1].l) help_exit("-%c > %ld", opt->c, opt->val[1].l); *(opt->arg) = l; } else if (CFG_TOYBOX_FLOAT && type == '.') { @@ -213,9 +213,9 @@ static int gotflag(struct getoptflagstate *gof, struct opts *opt) *f = strtod(arg, &arg); if (opt->val[0].l != LONG_MIN && *f < opt->val[0].f) - error_exit("-%c < %lf", opt->c, (double)opt->val[0].f); + help_exit("-%c < %lf", opt->c, (double)opt->val[0].f); if (opt->val[1].l != LONG_MAX && *f > opt->val[1].f) - error_exit("-%c > %lf", opt->c, (double)opt->val[1].f); + help_exit("-%c > %lf", opt->c, (double)opt->val[1].f); } if (!gof->nodash_now) gof->arg = ""; @@ -384,7 +384,6 @@ void get_optflags(void) // Option parsing is a two stage process: parse the option string into // a struct opts list, then use that list to process argv[]; - toys.exithelp++; // Allocate memory for optargs saveflags = 0; while (toys.argv[saveflags++]); @@ -475,10 +474,10 @@ notflag: // Sanity check if (toys.optc<gof.minargs) - error_exit("Need%s %d argument%s", letters[!!(gof.minargs-1)], + help_exit("Need%s %d argument%s", letters[!!(gof.minargs-1)], gof.minargs, letters[!(gof.minargs-1)]); if (toys.optc>gof.maxargs) - error_exit("Max %d argument%s", gof.maxargs, letters[!(gof.maxargs-1)]); + help_exit("Max %d argument%s", gof.maxargs, letters[!(gof.maxargs-1)]); if (gof.requires && !(gof.requires & toys.optflags)) { struct opts *req; char needs[32], *s = needs; @@ -487,9 +486,8 @@ notflag: if (req->flags & 1) *(s++) = req->c; *s = 0; - error_exit("Needs %s-%s", s[1] ? "one of " : "", needs); + help_exit("Needs %s-%s", s[1] ? "one of " : "", needs); } - toys.exithelp = 0; if (CFG_TOYBOX_FREE) { llist_traverse(gof.opts, free); @@ -3,7 +3,7 @@ #include "toys.h" #if !CFG_TOYBOX_HELP -void show_help(void) {;} +void show_help(FILE *out) {;} #else #include "generated/help.h" @@ -15,7 +15,7 @@ static char *help_data = #include "generated/newtoys.h" ; -void show_help(void) +void show_help(FILE *out) { int i = toys.which-toy_list; char *s; @@ -33,6 +33,6 @@ void show_help(void) i = toy_find(++s)-toy_list; } - fprintf(toys.exithelp ? stderr : stdout, "%s", s); + fprintf(out, "%s", s); } #endif @@ -13,10 +13,12 @@ void verror_msg(char *msg, int err, va_list va) if (msg) vfprintf(stderr, msg, va); else s+=2; if (err) fprintf(stderr, s, strerror(err)); - putc('\n', stderr); + if (msg || err) putc('\n', stderr); if (!toys.exitval) toys.exitval++; } +// These functions don't collapse together because of the va_stuff. + void error_msg(char *msg, ...) { va_list va; @@ -40,7 +42,19 @@ void error_exit(char *msg, ...) { va_list va; - if (CFG_TOYBOX_HELP && toys.exithelp) show_help(); + va_start(va, msg); + verror_msg(msg, 0, va); + va_end(va); + + xexit(); +} + +// Exit with an error message after showing help text. +void help_exit(char *msg, ...) +{ + va_list va; + + if (CFG_TOYBOX_HELP) show_help(stderr); va_start(va, msg); verror_msg(msg, 0, va); @@ -80,7 +80,7 @@ struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node)); // help.c -void show_help(void); +void show_help(FILE *out); // xwrap.c void xstrncpy(char *dest, char *src, size_t size); @@ -141,6 +141,7 @@ void verror_msg(char *msg, int err, va_list va); void error_msg(char *msg, ...) printf_format; void perror_msg(char *msg, ...) printf_format; void error_exit(char *msg, ...) printf_format noreturn; +void help_exit(char *msg, ...) printf_format noreturn; void perror_exit(char *msg, ...) printf_format noreturn; ssize_t readall(int fd, void *buf, size_t len); ssize_t writeall(int fd, void *buf, size_t len); @@ -78,7 +78,7 @@ static void toy_singleinit(struct toy_list *which, char *argv[]) if (CFG_TOYBOX_HELP_DASHDASH && argv[1] && !strcmp(argv[1], "--help")) { if (CFG_TOYBOX && toys.which == toy_list && toys.argv[2]) if (!(toys.which = toy_find(toys.argv[2]))) return; - show_help(); + show_help(stdout); xexit(); } @@ -110,10 +110,7 @@ void toy_init(struct toy_list *which, char *argv[]) } else if (CFG_TOYBOX_DEBUG && uid && which != toy_list) error_msg("Not installed suid root"); - if ((which->flags & TOYFLAG_NEEDROOT) && euid) { - toys.exithelp++; - error_exit("Not root"); - } + if ((which->flags & TOYFLAG_NEEDROOT) && euid) help_exit("Not root"); } // Free old toys contents (to be reentrant), but leave rebound if any diff --git a/scripts/config2help.c b/scripts/config2help.c index 1c2bcdcb..2ed189aa 100644 --- a/scripts/config2help.c +++ b/scripts/config2help.c @@ -3,7 +3,7 @@ // Humor toys.h struct toy_context toys; char libbuf[4096], toybuf[4096]; -void show_help(void) {;} +void show_help(FILE *out) {;} void toy_exec(char *argv[]) {;} // Parse config files into data structures. @@ -125,7 +125,6 @@ extern struct toy_context { unsigned optflags; // Command line option flags from get_optflags() int exitval; // Value error_exit feeds to exit() int optc; // Count of optargs - int exithelp; // Should error_exit print a usage message first? int old_umask; // Old umask preserved by TOYFLAG_UMASK int toycount; // Total number of commands in this build int signal; // generic_signal() records what signal it saw here diff --git a/toys/lsb/killall.c b/toys/lsb/killall.c index e8927559..3b316ecc 100644 --- a/toys/lsb/killall.c +++ b/toys/lsb/killall.c @@ -83,10 +83,7 @@ void killall_main(void) } } - if (!(toys.optflags & FLAG_l) && !toys.optc) { - toys.exithelp++; - error_exit("no name"); - } + if (!(toys.optflags & FLAG_l) && !toys.optc) help_exit("no name"); TT.cur_pid = getpid(); diff --git a/toys/other/blockdev.c b/toys/other/blockdev.c index 79b4d6f9..e5a504e6 100644 --- a/toys/other/blockdev.c +++ b/toys/other/blockdev.c @@ -43,10 +43,7 @@ void blockdev_main(void) char **ss; long long val = 0; - if (!toys.optflags) { - toys.exithelp = 1; - error_exit("need --option"); - } + if (!toys.optflags) help_exit("need --option"); for (ss = toys.optargs; *ss; ss++) { int fd = xopen(*ss, O_RDONLY), i; diff --git a/toys/other/help.c b/toys/other/help.c index 15f8271e..4722528c 100644 --- a/toys/other/help.c +++ b/toys/other/help.c @@ -37,12 +37,12 @@ static void do_help(struct toy_list *t) xprintf("<a name=\"%s\"><h1>%s</h1><blockquote><pre>\n", t->name, t->name); toys.which = t; - show_help(); + show_help(stdout); if (toys.optflags & FLAG_h) xprintf("</blockquote></pre>\n"); } -// The simple help is just toys.which = toy_find("name"); show_help(); +// The simple help is just toys.which = toy_find("name"); show_help(stdout); // But iterating through html output and all commands is a big more void help_main(void) diff --git a/toys/other/ifconfig.c b/toys/other/ifconfig.c index 948043e2..bfd9a306 100644 --- a/toys/other/ifconfig.c +++ b/toys/other/ifconfig.c @@ -403,10 +403,7 @@ void ifconfig_main(void) p = ptr = toybuf; } } - if (!sock->sa_family || !argv[1]) { - toys.exithelp++; - error_exit("bad hw '%s'", *argv); - } + if (!sock->sa_family || !argv[1]) help_exit("bad hw '%s'", *argv); hw_addr = *++argv; // Parse and verify address. @@ -447,10 +444,7 @@ void ifconfig_main(void) } ifre6; int plen, fd6 = xsocket(AF_INET6, SOCK_DGRAM, 0); - if (!argv[1]) { - toys.exithelp++; - error_exit("%s", *argv); - } + if (!argv[1]) help_exit("%s", *argv); plen = get_addrinfo(argv[1], AF_INET6, &ifre6.addr); if (plen < 0) plen = 128; @@ -517,10 +511,7 @@ void ifconfig_main(void) break; } - if (i == sizeof(try)/sizeof(*try)) { - toys.exithelp++; - error_exit("bad argument '%s'", *argv); - } + if (i == sizeof(try)/sizeof(*try)) help_exit("bad argument '%s'", *argv); } close(TT.sockfd); } diff --git a/toys/other/losetup.c b/toys/other/losetup.c index 9568627e..a40d9e42 100644 --- a/toys/other/losetup.c +++ b/toys/other/losetup.c @@ -180,10 +180,8 @@ void losetup_main(void) } else { char *file = (toys.optflags & (FLAG_d|FLAG_c)) ? NULL : toys.optargs[1]; - if (!toys.optc || (file && toys.optc != 2)) { - toys.exithelp++; - perror_exit("needs %d arg%s", 1+!!file, file ? "s" : ""); - } + if (!toys.optc || (file && toys.optc != 2)) + help_exit("needs %d arg%s", 1+!!file, file ? "s" : ""); for (s = toys.optargs; *s; s++) { loopback_setup(*s, file); if (file) break; diff --git a/toys/other/lsattr.c b/toys/other/lsattr.c index 39945ef0..ceb14bdf 100644 --- a/toys/other/lsattr.c +++ b/toys/other/lsattr.c @@ -186,12 +186,6 @@ static struct _chattr { unsigned char vflag, recursive; } chattr; -static inline void chattr_help(void) -{ - toys.exithelp++; - error_exit("Invalid Argument"); -} - // Set file flags on a Linux second extended file system. static inline int ext2_setflag(int fd, struct stat *sb, unsigned long flag) { @@ -208,8 +202,7 @@ static unsigned long get_flag_val(char ch) for (; ptr->name; ptr++) if (ptr->opt == ch) return ptr->flag; - chattr_help(); // if no match found then Show help - return 0; // silent warning. + help_exit("bad '%c'", ch); } // Parse command line argument and fill the chattr structure. @@ -229,7 +222,7 @@ static void parse_cmdline_arg(char ***argv) errno = 0; arg = *(*argv += 1); - if (!arg) chattr_help(); + if (!arg) help_exit("bad -v"); if (*arg == '-') perror_exit("Invalid Number '%s'", arg); chattr.version = strtoul(arg, &endptr, 0); if (errno || *endptr) perror_exit("bad version '%s'", arg); @@ -309,12 +302,12 @@ void chattr_main(void) memset(&chattr, 0, sizeof(struct _chattr)); parse_cmdline_arg(&argv); - if (!*argv) chattr_help(); + if (!*argv) help_exit("no file"); if (chattr.set && (chattr.add || chattr.rm)) - error_exit("'=' is incompatible with '-' and '+'"); - if (chattr.rm & chattr.add) error_exit("Can't set and unset same flag."); + error_exit("no '=' with '-' or '+'"); + if (chattr.rm & chattr.add) error_exit("set/unset same flag"); if (!(chattr.add || chattr.rm || chattr.set || chattr.vflag)) - error_exit(("Must use '-v', '=', '-' or '+'")); + error_exit("need '-v', '=', '-' or '+'"); for (; *argv; argv++) dirtree_read(*argv, update_attr); toys.exitval = 0; //always set success at this point. } diff --git a/toys/other/netcat.c b/toys/other/netcat.c index 9d0c3cf3..3cc3f0a0 100644 --- a/toys/other/netcat.c +++ b/toys/other/netcat.c @@ -91,11 +91,9 @@ void netcat_main(void) // The argument parsing logic can't make "<2" conditional on other // arguments like -f and -l, so we do it by hand here. - if (toys.optflags&FLAG_f) { - if (toys.optc) toys.exithelp++; - } else if (!(toys.optflags&(FLAG_l|FLAG_L)) && toys.optc!=2) toys.exithelp++; - - if (toys.exithelp) error_exit("Argument count wrong"); + if ((toys.optflags&FLAG_f) ? toys.optc : + (!(toys.optflags&(FLAG_l|FLAG_L)) && toys.optc!=2)) + help_exit("Argument count wrong"); if (TT.filename) pollfds[0].fd = xopen(TT.filename, O_RDWR); else { diff --git a/toys/pending/brctl.c b/toys/pending/brctl.c index e3b1526c..60178518 100644 --- a/toys/pending/brctl.c +++ b/toys/pending/brctl.c @@ -320,20 +320,14 @@ void brctl_main(void) if (strcmp(t->cmd, *toys.optargs)) continue; toys.optargs++, toys.optc--; - if (toys.optc < t->nargs) { - toys.exithelp++; - error_exit("check args"); - } + if (toys.optc < t->nargs) help_exit("check args"); t->f(toys.optargs); toys.optargs += t->nargs; toys.optc -= t->nargs; break; } - if (i == ARRAY_LEN(cc)) { - toys.exithelp++; - error_exit("invalid option '%s'", *toys.optargs); - } + if (i == ARRAY_LEN(cc)) help_exit("invalid option '%s'", *toys.optargs); } xclose(TT.sockfd); } diff --git a/toys/pending/crontab.c b/toys/pending/crontab.c index 05c98f2b..67c8a543 100644 --- a/toys/pending/crontab.c +++ b/toys/pending/crontab.c @@ -346,20 +346,15 @@ void crontab_main(void) if (!toys.optc) { if (!FLAG_elr) { - if (toys.optflags & FLAG_u) { - toys.exithelp++; - error_exit("file name must be specified for replace"); - } + if (toys.optflags & FLAG_u) + help_exit("file name must be specified for replace"); do_replace(pwd->pw_name); } else if (toys.optflags & FLAG_e) do_edit(pwd); else if (toys.optflags & FLAG_l) do_list(pwd->pw_name); else if (toys.optflags & FLAG_r) do_remove(pwd->pw_name); } else { - if (FLAG_elr) { - toys.exithelp++; - error_exit("no arguments permitted after this option"); - } + if (FLAG_elr) help_exit("no arguments permitted after this option"); do_replace(pwd->pw_name); } if (!(toys.optflags & FLAG_c)) free(TT.cdir); diff --git a/toys/pending/dd.c b/toys/pending/dd.c index 366d3c54..24d95657 100644 --- a/toys/pending/dd.c +++ b/toys/pending/dd.c @@ -297,10 +297,7 @@ void dd_main() while (*toys.optargs) { if (!(arg = strchr(*toys.optargs, '='))) error_exit("unknown arg %s", *toys.optargs); *arg++ = '\0'; - if (!*arg) { - toys.exithelp = 1; - error_exit(""); - } + if (!*arg) help_exit(0); key.name = *toys.optargs; if (!(res = bsearch(&key, operands, ARRAY_LEN(operands), sizeof(struct pair), comp))) error_exit("unknown arg %s", key.name); diff --git a/toys/pending/fdisk.c b/toys/pending/fdisk.c index 3ee9f598..d000c055 100644 --- a/toys/pending/fdisk.c +++ b/toys/pending/fdisk.c @@ -151,9 +151,9 @@ static void read_sec_sz() if (ioctl(dev_fd, BLKSSZGET, &arg) == 0) g_sect_size = arg; if (toys.optflags & FLAG_b) { if (TT.sect_sz != 512 && TT.sect_sz != 1024 && TT.sect_sz != 2048 && - TT.sect_sz != 4096) { - toys.exithelp++; - error_exit("bad sector size"); + TT.sect_sz != 4096) + { + help_exit("bad sector size"); } g_sect_size = TT.sect_sz; } @@ -1486,11 +1486,7 @@ void fdisk_main(void) toys.exitval = 0; return; } else { - if (!toys.optc || toys.optc > 1 ) { - toys.exitval = toys.exithelp = 1; - show_help(); - return; - } + if (toys.optc != 1) help_exit(stdout); if (read_mbr(toys.optargs[0], 1)) return; while (1) { xputc('\n'); diff --git a/toys/pending/groupadd.c b/toys/pending/groupadd.c index 615c12f8..30468c94 100644 --- a/toys/pending/groupadd.c +++ b/toys/pending/groupadd.c @@ -62,10 +62,8 @@ void groupadd_main(void) struct group *grp = NULL; char *entry = NULL; - if (toys.optflags && toys.optc == 2) { - toys.exithelp = 1; - error_exit("options, user and group can't be together"); - } + if (toys.optflags && toys.optc == 2) + help_exit("options, user and group can't be together"); if (toys.optc == 2) { //add user to group //toys.optargs[0]- user, toys.optargs[1] - group diff --git a/toys/pending/ip.c b/toys/pending/ip.c index 9a01d953..90f73308 100644 --- a/toys/pending/ip.c +++ b/toys/pending/ip.c @@ -137,12 +137,6 @@ static char *idx_to_string(int idx, struct arglist *list) return NULL; } -static void iphelp(void) -{ - toys.exithelp = 1; - error_exit(NULL); -} - static void send_nlmesg(int type, int flags, int family, void *buf, int blen) { @@ -519,16 +513,16 @@ static void vlan_parse_opt(char **argv, struct nlmsghdr *n, unsigned int size) for (; *argv; argv++) { int param, proto; - if ((idx = substring_to_idx(*argv++, vlan_optlist)) == -1) iphelp(); + if ((idx = substring_to_idx(*argv++, vlan_optlist)) == -1) help_exit(0); switch (idx) { case 0: // ARG_id - if (!*argv) iphelp(); + if (!*argv) help_exit(0); param = atolx(*argv); add_string_to_rtattr(n, size, IFLA_VLAN_ID, ¶m, sizeof(param)); break; case 1: // ARG_protocol if (!*argv) error_exit("Invalid vlan id."); - if ((idx = substring_to_idx(*argv, vlan_protolist)) == -1) iphelp(); + if ((idx = substring_to_idx(*argv, vlan_protolist)) == -1) help_exit(0); if (!idx) proto = ETH_P_8021Q; // PROTO_8021Q - 0 else if (idx == 1) proto = 0x88A8; // ETH Protocol - 8021AD // IFLA VLAN PROTOCOL - 5 @@ -536,7 +530,7 @@ static void vlan_parse_opt(char **argv, struct nlmsghdr *n, unsigned int size) break; case 2: // ARG_reorder_hdr case 3: // ARG_gvrp - if ((param = substring_to_idx(*argv, on_off)) == -1) iphelp(); + if ((param = substring_to_idx(*argv, on_off)) == -1) help_exit(0); flags.mask |= (idx -1); // VLAN FLAG REORDER Header flags.flags &= ~(idx -1); // VLAN FLAG REORDER Header @@ -650,7 +644,7 @@ static int link_set(char **argv) fd = xsocket(AF_INET, SOCK_DGRAM, 0); xioctl(fd, SIOCGIFINDEX, &req); for (++argv; *argv;) { - if ((idx = substring_to_idx(*argv++, cmd_objectlist)) == -1) iphelp(); + if ((idx = substring_to_idx(*argv++, cmd_objectlist)) == -1) help_exit(0); switch(idx) { case 0: flags |= IFF_UP; break; @@ -659,7 +653,7 @@ static int link_set(char **argv) case 2: case 3: case 4: - if (!*argv) iphelp(); + if (!*argv) help_exit(0); else if (!strcmp(*argv, "on")) { if (idx == 2) { masks &= ~case_flags[idx-2]; @@ -670,7 +664,7 @@ static int link_set(char **argv) masks |= case_flags[idx-2]; flags |= case_flags[idx-2]; } else masks &= ~case_flags[idx-2]; - } else iphelp(); + } else help_exit(0); ++argv; break; case 5: @@ -941,7 +935,8 @@ static int iplink(char **argv) {"set", 1}, {"show", 2}, {"list", 2}, {"lst", 2}, {NULL,-1}}; if (!*argv) idx = 2; - else if ((idx = substring_to_idx(*argv++, cmd_objectlist)) == -1) iphelp(); + else if ((idx = substring_to_idx(*argv++, cmd_objectlist)) == -1) + help_exit(0); ipcmd = cmdobjlist[idx]; return ipcmd(argv); } @@ -1431,7 +1426,8 @@ static int ipaddr(char **argv) TT.is_addr++; if (!*argv) idx = 1; - else if ((idx = substring_to_idx(*argv++, cmd_objectlist)) == -1) iphelp(); + else if ((idx = substring_to_idx(*argv++, cmd_objectlist)) == -1) + help_exit(0); ipcmd = cmdobjlist[idx]; return ipcmd(argv); @@ -2757,7 +2753,7 @@ void ip_main(void) struct arglist ip_aflist[] = {{"inet", AF_INET}, {"inet6", AF_INET6}, {"link", AF_PACKET}, {NULL, -1}}; - if (!*++optargv) iphelp(); + if (!*++optargv) help_exit(0); if ((TT.addressfamily = string_to_idx(*optargv, ip_aflist)) == -1) error_exit("wrong family '%s'", *optargv); } @@ -2766,7 +2762,7 @@ void ip_main(void) case 2: TT.stats++; break; - default: iphelp(); + default: help_exit(0); break; // unreachable code. } } @@ -2778,15 +2774,15 @@ void ip_main(void) struct arglist ip_objectlist[] = { {"address", 0}, {"link", 1}, {"route", 2}, {"rule", 3}, {"tunnel", 4}, {"tunl", 4}, {NULL, -1}}; - if ((idx = substring_to_idx(*optargv, ip_objectlist)) == -1) iphelp(); + if ((idx = substring_to_idx(*optargv, ip_objectlist)) == -1) help_exit(0); ipcmd = cmdobjlist[idx]; toys.exitval = ipcmd(++optargv); - } else iphelp(); + } else help_exit(0); } else { struct arglist ip_objectlist[] = { {"ipaddr", 0}, {"iplink", 1}, {"iproute", 2}, {"iprule", 3}, {"iptunnel", 4}, {NULL, -1}}; if ((idx = string_to_idx(toys.which->name, ip_objectlist)) == -1) - iphelp(); + help_exit(0); ipcmd = cmdobjlist[idx]; toys.exitval = ipcmd(optargv); } diff --git a/toys/pending/ipcrm.c b/toys/pending/ipcrm.c index 21f436d3..cec1b12f 100644 --- a/toys/pending/ipcrm.c +++ b/toys/pending/ipcrm.c @@ -83,9 +83,6 @@ void ipcrm_main(void) for (tmp = TT.qid; tmp; tmp = tmp->next) do_ipcrm(0, 2, tmp->arg); for (tmp = TT.skey; tmp; tmp = tmp->next) do_ipcrm(1, 3, tmp->arg); for (tmp = TT.sid; tmp; tmp = tmp->next) do_ipcrm(0, 3, tmp->arg); - if (toys.optc) { - toys.exithelp++; - error_exit("unknown argument: %s", *toys.optargs); - } + if (toys.optc) help_exit("unknown argument: %s", *toys.optargs); } } diff --git a/toys/pending/ipcs.c b/toys/pending/ipcs.c index d974a519..b5986af7 100644 --- a/toys/pending/ipcs.c +++ b/toys/pending/ipcs.c @@ -425,10 +425,7 @@ void ipcs_main(void) if (flag(m)) show_shm_id(); else if (flag(s)) show_sem_id(); else if (flag(q)) show_msg_id(); - else { - toys.exithelp++; - error_exit(NULL); - } + else help_exit(0); return; } diff --git a/toys/pending/modprobe.c b/toys/pending/modprobe.c index 07c53fc2..6813dec3 100644 --- a/toys/pending/modprobe.c +++ b/toys/pending/modprobe.c @@ -495,8 +495,7 @@ void modprobe_main(void) if ((toys.optc < 1) && (((flags & FLAG_r) && (flags & FLAG_l)) ||(!((flags & FLAG_r)||(flags & FLAG_l))))) { - toys.exithelp++; - error_exit("bad syntax"); + help_exit("bad syntax"); } // Check for -r flag without arg if yes then do auto remove. if ((flags & FLAG_r) && !toys.optc) { diff --git a/toys/pending/pgrep.c b/toys/pending/pgrep.c index 59767b9f..9117a6d5 100644 --- a/toys/pending/pgrep.c +++ b/toys/pending/pgrep.c @@ -91,14 +91,10 @@ void pgrep_main(void) error_exit("Unknown signal '%s'", arg); } else signum = SIGTERM; } - if (!(flag_chk(FLAG_s) || flag_chk(FLAG_P)) && !*toys.optargs) { - toys.exithelp++; - error_exit("missing argument"); - } - if (*(toys.optargs+1) && !(flag_chk(FLAG_s) || flag_chk(FLAG_P))) { - toys.exithelp++; - error_exit("max argument > 1"); - } + if (!(flag_chk(FLAG_s) || flag_chk(FLAG_P)) && !*toys.optargs) + help_exit("missing argument"); + if (*(toys.optargs+1) && !(flag_chk(FLAG_s) || flag_chk(FLAG_P))) + help_exit("max argument > 1"); if (*toys.optargs) { /* compile regular expression(PATTERN) */ if ((eval = regcomp(&rp, *toys.optargs, REG_EXTENDED | REG_NOSUB)) != 0) { char errbuf[256]; diff --git a/toys/pending/route.c b/toys/pending/route.c index ac1bbef5..02b59cfb 100644 --- a/toys/pending/route.c +++ b/toys/pending/route.c @@ -32,7 +32,7 @@ GLOBALS( #define INVALID_ADDR 0xffffffffUL #define IPV6_ADDR_LEN 40 //32 + 7 (':') + 1 ('\0') -#define TEST_ARGV(argv) if (!*argv) show_route_help() +#define TEST_ARGV(argv) if (!*argv) help_exit(0) struct _arglist { char *arg; @@ -49,13 +49,6 @@ static struct _arglist arglist2[] = { { NULL, 0 } }; -// display help info and exit. -static void show_route_help(void) -{ - toys.exithelp = 1; - error_exit("Invalid Argument"); -} - // to get the host name from the given ip. static int get_hostname(char *ipstr, struct sockaddr_in *sockin) { @@ -205,7 +198,7 @@ static void get_next_params(char **argv, struct rtentry *rt, char **netmask) //when adding a network route, the netmask to be used. struct sockaddr sock; unsigned int addr_mask = (((struct sockaddr_in *)&((rt)->rt_genmask))->sin_addr.s_addr); - if (addr_mask) show_route_help(); + if (addr_mask) help_exit("dup netmask"); argv++; TEST_ARGV(argv); *netmask = *argv; @@ -224,7 +217,7 @@ static void get_next_params(char **argv, struct rtentry *rt, char **netmask) argv++; } else if (ishost < 0) perror_exit("resolving '%s'", *argv); else perror_exit("gateway '%s' is a NETWORK", *argv); - } else show_route_help(); + } else help_exit("dup gw"); } else if (!strcmp(*argv, "mss")) { //set the TCP Maximum Segment Size for connections over this route. argv++; @@ -265,7 +258,7 @@ static void get_next_params(char **argv, struct rtentry *rt, char **netmask) } else if (!strcmp(*argv, "reinstate")) { rt->rt_flags |= RTF_REINSTATE; argv++; - } else show_route_help(); //No match found; exit form the application. + } else help_exit("no '%s'", *argv); //No match found; exit form the application. }//end of while loop. if (!rt->rt_dev && (rt->rt_flags & RTF_REJECT)) rt->rt_dev = (char *)"lo"; @@ -295,10 +288,10 @@ static void setroute(char **argv) int is_net_or_host = 0, sokfd, arg2_action; int action = get_action(&argv, arglist1); //verify the arg for add/del. - if (!action || !*argv) show_route_help(); + if (!action || !*argv) help_exit("setroute"); arg2_action = get_action(&argv, arglist2); //verify the arg for -net or -host - if (!*argv) show_route_help(); + if (!*argv) help_exit("setroute"); memset(&rt, 0, sizeof(struct rtentry)); targetip = *argv++; @@ -364,7 +357,7 @@ static void get_next_params_inet6(char **argv, struct sockaddr_in6 *sock_in6, st rt->rtmsg_flags |= RTF_GATEWAY; argv++; } else perror_exit("resolving '%s'", *argv); - } else show_route_help(); + } else help_exit(0); } else if (!strcmp(*argv, "dev")) { argv++; TEST_ARGV(argv); @@ -376,7 +369,7 @@ static void get_next_params_inet6(char **argv, struct sockaddr_in6 *sock_in6, st } else if (!strcmp(*argv, "dyn")) { rt->rtmsg_flags |= RTF_DYNAMIC; argv++; - } else show_route_help(); + } else help_exit(0); }//end of while loop. } @@ -388,7 +381,7 @@ static void setroute_inet6(char **argv) char *targetip; int sockfd, action = get_action(&argv, arglist1); - if (!action || !*argv) show_route_help(); + if (!action || !*argv) help_exit(0); memset(&sock_in6, 0, sizeof(struct sockaddr_in6)); memset(&rt, 0, sizeof(struct in6_rtmsg)); targetip = *argv++; @@ -418,7 +411,7 @@ static void setroute_inet6(char **argv) if (action == 1) xioctl(sockfd, SIOCADDRT, &rt); else xioctl(sockfd, SIOCDELRT, &rt); xclose(sockfd); - } else show_route_help(); + } else help_exit(0); } /* @@ -489,7 +482,7 @@ void route_main(void) if (!*toys.optargs) { if (!strcmp(TT.family, "inet")) display_routes(); else if (!strcmp(TT.family, "inet6")) display_routes6(); - else show_route_help(); + else help_exit(0); return; }//End of if statement. diff --git a/toys/pending/tftpd.c b/toys/pending/tftpd.c index 31af44be..806326eb 100644 --- a/toys/pending/tftpd.c +++ b/toys/pending/tftpd.c @@ -242,10 +242,7 @@ void tftpd_main(void) char *buf = toybuf; memset(&srcaddr, 0, sizeof(srcaddr)); - if (getsockname(0, (struct sockaddr *)&srcaddr, &socklen)) { - toys.exithelp = 1; - error_exit(NULL); - } + if (getsockname(0, (struct sockaddr *)&srcaddr, &socklen)) help_exit(0); if (TT.user) TT.pw = xgetpwnam(TT.user); if (*toys.optargs) xchroot(*toys.optargs); diff --git a/toys/pending/useradd.c b/toys/pending/useradd.c index 4d4c671e..d0ad03ab 100644 --- a/toys/pending/useradd.c +++ b/toys/pending/useradd.c @@ -46,10 +46,7 @@ void useradd_main(void) // Act like groupadd? if (toys.optc == 2) { - if (toys.optflags) { - toys.exithelp = 1; - error_exit("options with USER GROUP"); - } + if (toys.optflags) help_exit("options with USER GROUP"); xexec((char *[]){"groupadd", toys.optargs[0], toys.optargs[1], 0}); } diff --git a/toys/posix/kill.c b/toys/posix/kill.c index 72113878..3fcd36a1 100644 --- a/toys/posix/kill.c +++ b/toys/posix/kill.c @@ -136,10 +136,7 @@ void kill_main(void) } else { // "<1" in optstr wouldn't cover this because "-SIGNAL" - if (!*args) { - toys.exithelp++; - error_exit("missing argument"); - } + if (!*args) help_exit("missing argument"); while (*args) { char *arg = *(args++); diff --git a/www/code.html b/www/code.html index a1c08efe..6050aa1a 100644 --- a/www/code.html +++ b/www/code.html @@ -404,9 +404,6 @@ the first argument, not the command name. Use toys.which->name for the command name.</p></li> <li><p>int <b>optc</b> - Optarg count, equivalent to argc but for optargs[].<p></li> -<li><p>int <b>exithelp</b> - Whether error_exit() should print a usage message -via help_main() before exiting. (True during option parsing, defaults to -false afterwards.)</p></li> </ul> <a name="toy_union" /> |