From 677cd8cea214ef73bbbed965580575c9abaa048e Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sun, 10 Mar 2019 23:05:24 -0500 Subject: Add TOYFLAG_ARGFAIL() to allow argument parsing failures to exit with value. --- lib/args.c | 4 ++++ lib/toyflags.h | 3 +++ lib/xwrap.c | 2 +- 3 files changed, 8 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/args.c b/lib/args.c index 54106c25..1ebc4f2d 100644 --- a/lib/args.c +++ b/lib/args.c @@ -388,6 +388,8 @@ 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.exitval = toys.which->flags >> 24; + // Allocate memory for optargs saveflags = 0; while (toys.argv[saveflags++]); @@ -495,6 +497,8 @@ notflag: help_exit("Needs %s-%s", s[1] ? "one of " : "", needs); } + toys.exitval = 0; + if (CFG_TOYBOX_FREE) { llist_traverse(gof.opts, free); llist_traverse(gof.longopts, free); diff --git a/lib/toyflags.h b/lib/toyflags.h index bec8078b..408c3ec6 100644 --- a/lib/toyflags.h +++ b/lib/toyflags.h @@ -31,6 +31,9 @@ // Suppress default --help processing #define TOYFLAG_NOHELP (1<<9) +// Error code to return if argument parsing fails (default 1) +#define TOYFLAG_ARGFAIL(x) (x<<24) + #if CFG_TOYBOX_PEDANTIC_ARGS #define NO_ARGS ">0" #else diff --git a/lib/xwrap.c b/lib/xwrap.c index 223356b3..d7b06c5a 100644 --- a/lib/xwrap.c +++ b/lib/xwrap.c @@ -210,8 +210,8 @@ void xexec(char **argv) toy_exec(argv); execvp(argv[0], argv); + toys.exitval = 126+(errno == ENOENT); perror_msg("exec %s", argv[0]); - toys.exitval = 127; if (!toys.stacktop) _exit(toys.exitval); xexit(); } -- cgit v1.2.3