diff options
Diffstat (limited to '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 |
4 files changed, 32 insertions, 19 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); |