Mercurial > hg > toybox
changeset 1634:5fac2769a159 draft
Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
This means the flag space is no longer packed, but leaves gaps where the zeroes
go. (Actual flag bit positions are the same for all configs.) Since the
option parsing needs to know where the holes are, the OPTSTR values are
now generated as part of flags.h with ascii 1 values for the disabled values.
(So generated/oldflags.h went away.)
This also means that the option string argument for OLDTOY() went away, it now
uses the same arguments as the NEWTOY() it references.
author | Rob Landley <rob@landley.net> |
---|---|
date | Wed, 31 Dec 2014 21:30:59 -0600 |
parents | 556d8cbfeb08 |
children | 93c5e6163f9b |
files | lib/help.c main.c scripts/make.sh scripts/mkflags.c toys.h toys/other/dos2unix.c toys/other/nbd_client.c toys/other/netcat.c toys/other/reboot.c toys/pending/ftpget.c toys/pending/groupadd.c toys/pending/groupdel.c toys/pending/ip.c toys/pending/pgrep.c toys/pending/sh.c toys/pending/tcpsvd.c toys/pending/traceroute.c toys/pending/useradd.c toys/pending/userdel.c toys/posix/chgrp.c toys/posix/grep.c toys/posix/id.c toys/posix/true.c |
diffstat | 23 files changed, 105 insertions(+), 51 deletions(-) [+] |
line wrap: on
line diff
--- a/lib/help.c Wed Dec 31 21:23:37 2014 -0600 +++ b/lib/help.c Wed Dec 31 21:30:59 2014 -0600 @@ -10,7 +10,7 @@ #undef NEWTOY #undef OLDTOY #define NEWTOY(name,opt,flags) help_##name "\0" -#define OLDTOY(name,oldname,opts,flags) "\xff" #oldname "\0" +#define OLDTOY(name,oldname,flags) "\xff" #oldname "\0" static char *help_data = #include "generated/newtoys.h" ;
--- a/main.c Wed Dec 31 21:23:37 2014 -0600 +++ b/main.c Wed Dec 31 21:30:59 2014 -0600 @@ -10,7 +10,8 @@ #undef NEWTOY #undef OLDTOY #define NEWTOY(name, opts, flags) {#name, name##_main, opts, flags}, -#define OLDTOY(name, oldname, opts, flags) {#name, oldname##_main, opts, flags}, +#define OLDTOY(name, oldname, flags) \ + {#name, oldname##_main, OPTSTR_##oldname, flags}, struct toy_list toy_list[] = { #include "generated/newtoys.h" @@ -57,7 +58,7 @@ #undef NEWTOY #undef OLDTOY #define NEWTOY(name, opts, flags) opts || -#define OLDTOY(name, oldname, opts, flags) opts || +#define OLDTOY(name, oldname, flags) OPTSTR_##oldname || static const int NEED_OPTIONS = #include "generated/newtoys.h" 0; // Ends the opts || opts || opts...
--- a/scripts/make.sh Wed Dec 31 21:23:37 2014 -0600 +++ b/scripts/make.sh Wed Dec 31 21:30:59 2014 -0600 @@ -50,9 +50,7 @@ echo "USE_TOYBOX(NEWTOY(toybox, NULL, TOYFLAG_STAYROOT))" > generated/newtoys.h sed -n -e 's/^USE_[A-Z0-9_]*(/&/p' toys/*/*.c \ | sed 's/\(.*TOY(\)\([^,]*\),\(.*\)/\2 \1\2,\3/' | sort -s -k 1,1 \ - | sed 's/[^ ]* //' >> generated/newtoys.h && - sed -n -e 's/.*(NEWTOY(\([^,]*\), *\(\("[^"]*"[^,]*\)*\),.*/#define OPTSTR_\1\t\2/p' \ - generated/newtoys.h > generated/oldtoys.h || exit 1 + | sed 's/[^ ]* //' >> generated/newtoys.h || exit 1 fi [ ! -z "$V" ] && echo "Which C files to build..."
--- a/scripts/mkflags.c Wed Dec 31 21:23:37 2014 -0600 +++ b/scripts/mkflags.c Wed Dec 31 21:30:59 2014 -0600 @@ -17,11 +17,42 @@ struct flag *lopt; }; +// replace chopped out USE_BLAH() sections with low-ascii characters +// showing how many flags got skipped + +char *mark_gaps(char *flags, char *all) +{ + char *n, *new, c; + + // Shell feeds in " " for blank args, leading space not meaningful. + while (isspace(*flags)) flags++; + while (isspace(*all)) all++; + + n = new = strdup(all); + while (*all) { + if (*flags == *all) { + *(new++) = *(all++); + *flags++; + continue; + } + + c = *(all++); + if (strchr("?&^-:#|@*; ", c)); + else if (strchr("=<>", c)) while (isdigit(*all)) all++; + else if (c == '(') while(*(all++) != ')'); + else *(new++) = 1; + } + *new = 0; + + return n; +} + // Break down a command string into struct flag list. struct flag *digest(char *string) { struct flag *list = NULL; + char *err = string; while (*string) { // Groups must be at end. @@ -52,6 +83,10 @@ if (strchr("?&^-:#|@*; ", *string)) string++; else if (strchr("=<>", *string)) { + if (!isdigit(string[1])) { + fprintf(stderr, "%c without number in '%s'", *string, err); + exit(1); + } while (isdigit(*++string)) { if (!list) { string++; @@ -79,8 +114,12 @@ // See "intentionally crappy", above. if (!(out = outbuf)) return 1; + printf("#ifdef FORCE_FLAGS\n#define FORCED_FLAG 1\n" + "#else\n#define FORCED_FLAG 0\n#endif\n\n"); + for (;;) { struct flag *flist, *aflist, *offlist; + char *gaps, *mgaps, c; unsigned bit; *command = 0; @@ -95,6 +134,16 @@ bit = 0; printf("// %s %s %s\n", command, flags, allflags); + mgaps = mark_gaps(flags, allflags); + for (gaps = mgaps; *gaps == 1; gaps++); + if (*gaps) c = '"'; + else { + c = ' '; + gaps = "0"; + } + printf("#undef OPTSTR_%s\n#define OPTSTR_%s %c%s%c\n", + command, command, c, gaps, c); + free(mgaps); flist = digest(flags); offlist = aflist = digest(allflags); @@ -124,29 +173,28 @@ { sprintf(out, "#define FLAG_%s (1<<%d)\n", flist->lopt->command, bit); flist->lopt = flist->lopt->next; - } else sprintf(out, "#define FLAG_%s 0\n", aflist->lopt->command); + } else sprintf(out, "#define FLAG_%s (FORCED_FLAG<<%d)\n", + aflist->lopt->command, bit); aflist->lopt = aflist->lopt->next; if (!aflist->command) { aflist = aflist->next; - if (flist) { - flist = flist->next; - bit++; - } + bit++; + if (flist) flist = flist->next; } } else if (aflist->command) { if (flist && (!aflist->command || *aflist->command == *flist->command)) { if (aflist->command) sprintf(out, "#define FLAG_%c (1<<%d)\n", *aflist->command, bit); - bit++; flist = flist->next; - } else sprintf(out, "#define FLAG_%c 0\n", *aflist->command); + } else sprintf(out, "#define FLAG_%c (FORCED_FLAG<<%d)\n", + *aflist->command, bit); + bit++; aflist = aflist->next; } out += strlen(out); } - sprintf(out, "#endif\n\n"); - out += strlen(out); + out = stpcpy(out, "#endif\n\n"); } if (fflush(0) && ferror(stdout)) return 1;
--- a/toys.h Wed Dec 31 21:23:37 2014 -0600 +++ b/toys.h Wed Dec 31 21:30:59 2014 -0600 @@ -73,9 +73,8 @@ // Get list of function prototypes for all enabled command_main() functions. #define NEWTOY(name, opts, flags) void name##_main(void); -#define OLDTOY(name, oldname, opts, flags) void oldname##_main(void); +#define OLDTOY(name, oldname, flags) void oldname##_main(void); #include "generated/newtoys.h" -#include "generated/oldtoys.h" #include "generated/flags.h" #include "generated/globals.h"
--- a/toys/other/dos2unix.c Wed Dec 31 21:23:37 2014 -0600 +++ b/toys/other/dos2unix.c Wed Dec 31 21:30:59 2014 -0600 @@ -3,7 +3,7 @@ * Copyright 2012 Rob Landley <rob@landley.net> USE_DOS2UNIX(NEWTOY(dos2unix, NULL, TOYFLAG_BIN)) -USE_DOS2UNIX(OLDTOY(unix2dos, dos2unix, NULL, TOYFLAG_BIN)) +USE_DOS2UNIX(OLDTOY(unix2dos, dos2unix, TOYFLAG_BIN)) config DOS2UNIX bool "dos2unix/unix2dos"
--- a/toys/other/nbd_client.c Wed Dec 31 21:23:37 2014 -0600 +++ b/toys/other/nbd_client.c Wed Dec 31 21:30:59 2014 -0600 @@ -8,7 +8,7 @@ // things like prototype "nbd-client_main" which isn't a valid symbol. So // we hide the underscore name and OLDTOY the name we want. USE_NBD_CLIENT(NEWTOY(nbd_client, "<3>3ns", 0)) -USE_NBD_CLIENT(OLDTOY(nbd-client, nbd_client, OPTSTR_nbd_client, TOYFLAG_USR|TOYFLAG_BIN)) +USE_NBD_CLIENT(OLDTOY(nbd-client, nbd_client, TOYFLAG_USR|TOYFLAG_BIN)) config NBD_CLIENT bool "nbd-client"
--- a/toys/other/netcat.c Wed Dec 31 21:23:37 2014 -0600 +++ b/toys/other/netcat.c Wed Dec 31 21:30:59 2014 -0600 @@ -4,7 +4,7 @@ * * TODO: udp, ipv6, genericize for telnet/microcom/tail-f -USE_NETCAT(OLDTOY(nc, netcat, USE_NETCAT_LISTEN("tl^L^")"w#p#s:q#f:", TOYFLAG_BIN)) +USE_NETCAT(OLDTOY(nc, netcat, TOYFLAG_BIN)) USE_NETCAT(NEWTOY(netcat, USE_NETCAT_LISTEN("tl^L^")"w#p#s:q#f:", TOYFLAG_BIN)) config NETCAT
--- a/toys/other/reboot.c Wed Dec 31 21:23:37 2014 -0600 +++ b/toys/other/reboot.c Wed Dec 31 21:30:59 2014 -0600 @@ -3,8 +3,8 @@ * Copyright 2013 Elie De Brauwer <eliedebrauwer@gmail.com> USE_REBOOT(NEWTOY(reboot, "n", TOYFLAG_BIN|TOYFLAG_NEEDROOT)) -USE_REBOOT(OLDTOY(halt, reboot, "n", TOYFLAG_BIN|TOYFLAG_NEEDROOT)) -USE_REBOOT(OLDTOY(poweroff, reboot, "n", TOYFLAG_BIN|TOYFLAG_NEEDROOT)) +USE_REBOOT(OLDTOY(halt, reboot, TOYFLAG_BIN|TOYFLAG_NEEDROOT)) +USE_REBOOT(OLDTOY(poweroff, reboot, TOYFLAG_BIN|TOYFLAG_NEEDROOT)) config REBOOT bool "reboot"
--- a/toys/pending/ftpget.c Wed Dec 31 21:23:37 2014 -0600 +++ b/toys/pending/ftpget.c Wed Dec 31 21:30:59 2014 -0600 @@ -6,7 +6,7 @@ * No Standard. * USE_FTPGET(NEWTOY(ftpget, "<2cvu:p:P#<0=21>65535", TOYFLAG_BIN)) -USE_FTPGET(OLDTOY(ftpput,ftpget, "<2vu:p:P#<0=21>65535", TOYFLAG_BIN)) +USE_FTPGET(OLDTOY(ftpput, ftpget, TOYFLAG_BIN)) config FTPGET bool "ftpget/ftpput"
--- a/toys/pending/groupadd.c Wed Dec 31 21:23:37 2014 -0600 +++ b/toys/pending/groupadd.c Wed Dec 31 21:30:59 2014 -0600 @@ -6,7 +6,7 @@ * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/groupadd.html USE_GROUPADD(NEWTOY(groupadd, "<1>2g#<0S", TOYFLAG_NEEDROOT|TOYFLAG_SBIN)) -USE_GROUPADD(OLDTOY(addgroup, groupadd, OPTSTR_groupadd, TOYFLAG_NEEDROOT|TOYFLAG_SBIN)) +USE_GROUPADD(OLDTOY(addgroup, groupadd, TOYFLAG_NEEDROOT|TOYFLAG_SBIN)) config GROUPADD bool "groupadd"
--- a/toys/pending/groupdel.c Wed Dec 31 21:23:37 2014 -0600 +++ b/toys/pending/groupdel.c Wed Dec 31 21:30:59 2014 -0600 @@ -6,7 +6,7 @@ * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/groupdel.html USE_GROUPDEL(NEWTOY(groupdel, "<1>2", TOYFLAG_NEEDROOT|TOYFLAG_SBIN)) -USE_GROUPDEL(OLDTOY(delgroup, groupdel, OPTSTR_groupdel, TOYFLAG_NEEDROOT|TOYFLAG_SBIN)) +USE_GROUPDEL(OLDTOY(delgroup, groupdel, TOYFLAG_NEEDROOT|TOYFLAG_SBIN)) config GROUPDEL bool "groupdel"
--- a/toys/pending/ip.c Wed Dec 31 21:23:37 2014 -0600 +++ b/toys/pending/ip.c Wed Dec 31 21:30:59 2014 -0600 @@ -8,11 +8,11 @@ * No Standard. * USE_IP(NEWTOY(ip, NULL, TOYFLAG_SBIN)) -USE_IP(OLDTOY(ipaddr, ip, NULL, TOYFLAG_SBIN)) -USE_IP(OLDTOY(iplink, ip, NULL, TOYFLAG_SBIN)) -USE_IP(OLDTOY(iproute, ip, NULL, TOYFLAG_SBIN)) -USE_IP(OLDTOY(iprule, ip, NULL, TOYFLAG_SBIN)) -USE_IP(OLDTOY(iptunnel, ip, NULL, TOYFLAG_SBIN)) +USE_IP(OLDTOY(ipaddr, ip, TOYFLAG_SBIN)) +USE_IP(OLDTOY(iplink, ip, TOYFLAG_SBIN)) +USE_IP(OLDTOY(iproute, ip, TOYFLAG_SBIN)) +USE_IP(OLDTOY(iprule, ip, TOYFLAG_SBIN)) +USE_IP(OLDTOY(iptunnel, ip, TOYFLAG_SBIN)) config IP bool "ip"
--- a/toys/pending/pgrep.c Wed Dec 31 21:23:37 2014 -0600 +++ b/toys/pending/pgrep.c Wed Dec 31 21:30:59 2014 -0600 @@ -5,7 +5,7 @@ * USE_PGREP(NEWTOY(pgrep, "?P# s# xvonlf[!sP]", TOYFLAG_USR|TOYFLAG_BIN)) -USE_PGREP(OLDTOY(pkill, pgrep, OPTSTR_pgrep, TOYFLAG_USR|TOYFLAG_BIN)) +USE_PGREP(OLDTOY(pkill, pgrep, TOYFLAG_USR|TOYFLAG_BIN)) config PGREP bool "pgrep"
--- a/toys/pending/sh.c Wed Dec 31 21:23:37 2014 -0600 +++ b/toys/pending/sh.c Wed Dec 31 21:30:59 2014 -0600 @@ -25,7 +25,7 @@ USE_SH(NEWTOY(exit, NULL, TOYFLAG_NOFORK)) USE_SH(NEWTOY(sh, "c:"USE_SH_INTERACTIVE("i"), TOYFLAG_BIN)) -USE_SH(OLDTOY(toysh, sh, OPTSTR_sh, TOYFLAG_BIN)) +USE_SH(OLDTOY(toysh, sh, TOYFLAG_BIN)) config SH bool "sh (toysh)"
--- a/toys/pending/tcpsvd.c Wed Dec 31 21:23:37 2014 -0600 +++ b/toys/pending/tcpsvd.c Wed Dec 31 21:30:59 2014 -0600 @@ -7,7 +7,7 @@ * No Standard. USE_TCPSVD(NEWTOY(tcpsvd, "^<3c#=30<1C:b#=20<0u:l:hEv", TOYFLAG_USR|TOYFLAG_BIN)) -USE_TCPSVD(OLDTOY(udpsvd, tcpsvd, OPTSTR_tcpsvd, TOYFLAG_USR|TOYFLAG_BIN)) +USE_TCPSVD(OLDTOY(udpsvd, tcpsvd, TOYFLAG_USR|TOYFLAG_BIN)) config TCPSVD bool "tcpsvd"
--- a/toys/pending/traceroute.c Wed Dec 31 21:23:37 2014 -0600 +++ b/toys/pending/traceroute.c Wed Dec 31 21:30:59 2014 -0600 @@ -8,7 +8,7 @@ * No Standard USE_TRACEROUTE(NEWTOY(traceroute, "<1>2i:f#<1>255=1z#<0>86400=0g*w#<0>86400=5t#<0>255=0s:q#<1>255=3p#<1>65535=33434m#<1>255=30rvndlIUF64", TOYFLAG_STAYROOT|TOYFLAG_USR|TOYFLAG_BIN)) -USE_TRACEROUTE(OLDTOY(traceroute6,traceroute, OPTSTR_traceroute, TOYFLAG_STAYROOT|TOYFLAG_USR|TOYFLAG_BIN)) +USE_TRACEROUTE(OLDTOY(traceroute6,traceroute, TOYFLAG_STAYROOT|TOYFLAG_USR|TOYFLAG_BIN)) config TRACEROUTE bool "traceroute" default n
--- a/toys/pending/useradd.c Wed Dec 31 21:23:37 2014 -0600 +++ b/toys/pending/useradd.c Wed Dec 31 21:30:59 2014 -0600 @@ -6,7 +6,7 @@ * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/useradd.html USE_USERADD(NEWTOY(useradd, "<1>2u#<0G:s:g:h:SDH", TOYFLAG_NEEDROOT|TOYFLAG_UMASK|TOYFLAG_SBIN)) -USE_USERADD(OLDTOY(adduser, useradd, OPTSTR_useradd, TOYFLAG_NEEDROOT|TOYFLAG_UMASK|TOYFLAG_SBIN)) +USE_USERADD(OLDTOY(adduser, useradd, TOYFLAG_NEEDROOT|TOYFLAG_UMASK|TOYFLAG_SBIN)) config USERADD bool "useradd"
--- a/toys/pending/userdel.c Wed Dec 31 21:23:37 2014 -0600 +++ b/toys/pending/userdel.c Wed Dec 31 21:30:59 2014 -0600 @@ -5,7 +5,7 @@ * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/userdel.html USE_USERDEL(NEWTOY(userdel, "<1>1r", TOYFLAG_NEEDROOT|TOYFLAG_SBIN)) -USE_USERDEL(OLDTOY(deluser, userdel, OPTSTR_userdel, TOYFLAG_NEEDROOT|TOYFLAG_SBIN)) +USE_USERDEL(OLDTOY(deluser, userdel, TOYFLAG_NEEDROOT|TOYFLAG_SBIN)) config USERDEL bool "userdel"
--- a/toys/posix/chgrp.c Wed Dec 31 21:23:37 2014 -0600 +++ b/toys/posix/chgrp.c Wed Dec 31 21:30:59 2014 -0600 @@ -8,7 +8,7 @@ * TODO: group only one of [HLP] USE_CHGRP(NEWTOY(chgrp, "<2hPLHRfv", TOYFLAG_BIN)) -USE_CHGRP(OLDTOY(chown, chgrp, OPTSTR_chgrp, TOYFLAG_BIN)) +USE_CHGRP(OLDTOY(chown, chgrp, TOYFLAG_BIN)) config CHGRP bool "chgrp/chown"
--- a/toys/posix/grep.c Wed Dec 31 21:23:37 2014 -0600 +++ b/toys/posix/grep.c Wed Dec 31 21:30:59 2014 -0600 @@ -5,8 +5,8 @@ * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/grep.html USE_GREP(NEWTOY(grep, "ZzEFHabhinorsvwclqe*f*m#x[!wx][!EFw]", TOYFLAG_BIN)) -USE_GREP(OLDTOY(egrep, grep, OPTSTR_grep, TOYFLAG_BIN)) -USE_GREP(OLDTOY(fgrep, grep, OPTSTR_grep, TOYFLAG_BIN)) +USE_GREP(OLDTOY(egrep, grep, TOYFLAG_BIN)) +USE_GREP(OLDTOY(fgrep, grep, TOYFLAG_BIN)) config GREP bool "grep"
--- a/toys/posix/id.c Wed Dec 31 21:23:37 2014 -0600 +++ b/toys/posix/id.c Wed Dec 31 21:30:59 2014 -0600 @@ -7,9 +7,9 @@ * See http://opengroup.org/onlinepubs/9699919799/utilities/id.html USE_ID(NEWTOY(id, ">1nGgru[!Ggu]", TOYFLAG_BIN)) -USE_GROUPS(OLDTOY(groups, id, NULL, TOYFLAG_USR|TOYFLAG_BIN)) -USE_LOGNAME(OLDTOY(logname, id, ">0", TOYFLAG_BIN)) -USE_WHOAMI(OLDTOY(whoami, id, ">0", TOYFLAG_BIN)) +USE_GROUPS(NEWTOY(groups, NULL, TOYFLAG_USR|TOYFLAG_BIN)) +USE_LOGNAME(NEWTOY(logname, ">0", TOYFLAG_BIN)) +USE_WHOAMI(OLDTOY(whoami, logname, TOYFLAG_BIN)) config ID bool "id" @@ -133,15 +133,23 @@ void id_main(void) { // FLAG macros can be 0 if "id" command not enabled, so snapshot them here. - if (FLAG_u) TT.do_u = toys.optflags & FLAG_u; - if (FLAG_n) TT.do_n = toys.optflags & FLAG_n; - if (FLAG_G) TT.do_G = toys.optflags & FLAG_G; - - // And set the variables for non-id commands. - TT.is_groups = toys.which->name[0] == 'g'; - if (TT.is_groups) TT.do_G = TT.do_n = 1; - else if (toys.which->name[0] != 'i') TT.do_u = TT.do_n = 1; + if (FLAG_u) TT.do_u |= toys.optflags & FLAG_u; + if (FLAG_n) TT.do_n |= toys.optflags & FLAG_n; + if (FLAG_G) TT.do_G |= toys.optflags & FLAG_G; if (toys.optc) while(*toys.optargs) do_id(*toys.optargs++); else do_id(NULL); } + +void groups_main(void) +{ + TT.is_groups = 1; + TT.do_G = TT.do_n = 1; + id_main(); +} + +void logname_main(void) +{ + TT.do_u = TT.do_n = 1; + id_main(); +}
--- a/toys/posix/true.c Wed Dec 31 21:23:37 2014 -0600 +++ b/toys/posix/true.c Wed Dec 31 21:30:59 2014 -0600 @@ -5,7 +5,7 @@ * See http://opengroup.org/onlinepubs/9699919799/utilities/true.html USE_TRUE(NEWTOY(true, NULL, TOYFLAG_BIN)) -USE_TRUE(OLDTOY(:, true, 0, TOYFLAG_NOFORK)) +USE_TRUE(OLDTOY(:, true, TOYFLAG_NOFORK)) config TRUE bool "true"