From 8324b89598b2aee0957a0378f0f63ff5755498be Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sun, 19 Nov 2006 02:49:22 -0500 Subject: New option parsing infrastructure (doesn't use getopt). Hook it up to existing applets. Still a bit buggy, but bits of it work. --- toys/Config.in | 2 +- toys/df.c | 21 +++++++-------------- toys/toylist.h | 27 ++++++++++++++------------- toys/toysh.c | 15 ++++++++++----- toys/which.c | 9 ++------- 5 files changed, 34 insertions(+), 40 deletions(-) (limited to 'toys') diff --git a/toys/Config.in b/toys/Config.in index 2120636c..77e59e4d 100644 --- a/toys/Config.in +++ b/toys/Config.in @@ -161,7 +161,7 @@ config TOYSH_BUILTINS unset, read, alias. config WHICH - bool "Which" + bool "which" default n help usage: which [-a] filename ... diff --git a/toys/df.c b/toys/df.c index cbebd157..d4528f96 100644 --- a/toys/df.c +++ b/toys/df.c @@ -34,14 +34,13 @@ static void show_mt(struct mtab_list *mt) // Figure out how much total/used/free space this filesystem has, // forcing 64-bit math because filesystems are big now. block = mt->statvfs.f_bsize ? : 1; - size = (long)((block * mt->statvfs.f_blocks) / toy.df.units); used = (long)((block * (mt->statvfs.f_blocks-mt->statvfs.f_bfree)) / toy.df.units); avail = (long)((block * (getuid() ? mt->statvfs.f_bavail : mt->statvfs.f_bfree)) / toy.df.units); - percent = 100-(long)((100*(uint64_t)avail)/size); + percent = size ? 100-(long)((100*(uint64_t)avail)/size) : 0; // Figure out appropriate spacing len = 25 - strlen(mt->device); @@ -58,10 +57,6 @@ static void show_mt(struct mtab_list *mt) int df_main(void) { struct mtab_list *mt, *mt2, *mtlist; - char **argv; - - // get_optflags("Pkt:a",&(toy.df.fstype)); - argv = NULL; // Handle -P and -k toy.df.units = 1024; @@ -75,14 +70,14 @@ int df_main(void) mtlist = getmountlist(1); // If we have a list of filesystems on the command line, loop through them. - if (argv) { - char *next; + if (*toys.optargs) { + char **next; - for(next = *argv; *next; next++) { + for(next = toys.optargs; *next; next++) { struct stat st; // Stat it (complain if we can't). - if(!stat(next, &st)) { + if(!stat(*next, &st)) { perror_msg("`%s'", next); toys.exitval = 1; continue; @@ -117,9 +112,7 @@ int df_main(void) } } - if (CFG_TOYS_FREE) { - llist_free(mtlist, NULL); - free(argv); - } + if (CFG_TOYS_FREE) llist_free(mtlist, NULL); + return 0; } diff --git a/toys/toylist.h b/toys/toylist.h index 87716b0f..a0eef1c1 100644 --- a/toys/toylist.h +++ b/toys/toylist.h @@ -10,15 +10,15 @@ #ifdef FROM_MAIN #undef NEWTOY #undef OLDTOY -#define NEWTOY(name, flags) {#name, name##_main, flags}, -#define OLDTOY(name, oldname, flags) {#name, oldname##_main, flags}, +#define NEWTOY(name, opts, flags) {#name, name##_main, opts, flags}, +#define OLDTOY(name, oldname, opts, flags) {#name, oldname##_main, opts, flags}, // When #included from toys.h, provide function declarations and structs. // The #else is because main.c #includes this file twice. #else -#define NEWTOY(name, flags) int name##_main(void); -#define OLDTOY(name, oldname, flags) +#define NEWTOY(name, opts, flags) int name##_main(void); +#define OLDTOY(name, oldname, opts, flags) struct df_data { struct string_list *fstype; @@ -39,6 +39,7 @@ union toy_union { extern struct toy_list { char *name; int (*toy_main)(void); + char *options; int flags; } toy_list[]; @@ -48,15 +49,15 @@ extern struct toy_list { // This one is out of order on purpose. -NEWTOY(toybox, 0) +NEWTOY(toybox, NULL, 0) // The rest of these are alphabetical, for binary search. -USE_TOYSH(NEWTOY(cd, TOYFLAG_NOFORK)) -USE_DF(NEWTOY(df, TOYFLAG_USR|TOYFLAG_SBIN)) -USE_TOYSH(NEWTOY(exit, TOYFLAG_NOFORK)) -USE_HELLO(NEWTOY(hello, TOYFLAG_NOFORK|TOYFLAG_USR)) -USE_PWD(NEWTOY(pwd, TOYFLAG_BIN)) -USE_TOYSH(OLDTOY(sh, toysh, TOYFLAG_BIN)) -USE_TOYSH(NEWTOY(toysh, TOYFLAG_BIN)) -USE_WHICH(NEWTOY(which, TOYFLAG_USR|TOYFLAG_BIN)) +USE_TOYSH(NEWTOY(cd, NULL, TOYFLAG_NOFORK)) +USE_DF(NEWTOY(df, "Pkt:a", TOYFLAG_USR|TOYFLAG_SBIN)) +USE_TOYSH(NEWTOY(exit, NULL, TOYFLAG_NOFORK)) +USE_HELLO(NEWTOY(hello, NULL, TOYFLAG_NOFORK|TOYFLAG_USR)) +USE_PWD(NEWTOY(pwd, NULL, TOYFLAG_BIN)) +USE_TOYSH(OLDTOY(sh, toysh, "c:i", TOYFLAG_BIN)) +USE_TOYSH(NEWTOY(toysh, "c:i", TOYFLAG_BIN)) +USE_WHICH(NEWTOY(which, "a", TOYFLAG_USR|TOYFLAG_BIN)) diff --git a/toys/toysh.c b/toys/toysh.c index 7ac430c9..074907ef 100644 --- a/toys/toysh.c +++ b/toys/toysh.c @@ -125,12 +125,15 @@ static void run_pipeline(struct pipeline *line) tl = toy_find(cmd->argv[0]); // Is this command a builtin that should run in this process? if (tl && (tl->flags & TOYFLAG_NOFORK)) { - struct toy_list *which = toys.which; - char **argv = toys.argv; + struct toy_context temp; + // This fakes lots of what toybox_main() does. + memcpy(&temp, &toys, sizeof(struct toy_context)); + bzero(&toys, sizeof(struct toy_context)); toy_init(tl, cmd->argv); cmd->pid = tl->toy_main(); - toy_init(which, argv); + free(toys.optargs); + memcpy(&toys, &temp, sizeof(struct toy_context)); } else { int status; @@ -196,8 +199,10 @@ int toysh_main(void) char *command=NULL; FILE *f; - // TODO get_optflags(argv, "c:", &command); - + // Set up signal handlers and grab control of this tty. + if (CFG_TOYSH_TTY) { + if (isatty(0)) toys.optflags |= 1; + } f = toys.argv[1] ? xfopen(toys.argv[1], "r") : NULL; if (command) handle(command); else { diff --git a/toys/which.c b/toys/which.c index 6d00bc82..16e19d00 100644 --- a/toys/which.c +++ b/toys/which.c @@ -7,7 +7,6 @@ #include "toys.h" -#define OPTIONS "a" #define OPT_a 1 // Find an exectuable file either at a path with a slash in it (absolute or @@ -55,16 +54,12 @@ static int which_in_path(char *filename) int which_main(void) { - char **argv; int rc = 0; - // get_optflags(OPTIONS); - argv = toys.argv+1; - - if (!*argv) rc++; + if (!*toys.optargs) rc++; else { int i; - for (i=0; argv[i]; i++) rc |= which_in_path(argv[i]); + for (i=0; toys.optargs[i]; i++) rc |= which_in_path(toys.optargs[i]); } // if (CFG_TOYS_FREE) free(argv); -- cgit v1.2.3