diff options
author | Rob Landley <rob@landley.net> | 2012-10-08 00:02:30 -0500 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2012-10-08 00:02:30 -0500 |
commit | c0e56edaf256adb6c60c5a052525a1ffbb927901 (patch) | |
tree | d6bcc5c181ca46910a12d4dece4b26d6c71be3e1 /toys/posix | |
parent | dc7a77d1940858495f76998f4d13cac9f73e0226 (diff) | |
download | toybox-c0e56edaf256adb6c60c5a052525a1ffbb927901.tar.gz |
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Diffstat (limited to 'toys/posix')
-rw-r--r-- | toys/posix/chgrp.c | 13 | ||||
-rw-r--r-- | toys/posix/chmod.c | 8 | ||||
-rw-r--r-- | toys/posix/cksum.c | 5 | ||||
-rw-r--r-- | toys/posix/cmp.c | 8 | ||||
-rw-r--r-- | toys/posix/comm.c | 5 | ||||
-rw-r--r-- | toys/posix/cp.c | 107 | ||||
-rw-r--r-- | toys/posix/date.c | 8 | ||||
-rw-r--r-- | toys/posix/df.c | 13 | ||||
-rw-r--r-- | toys/posix/du.c | 17 | ||||
-rw-r--r-- | toys/posix/echo.c | 5 | ||||
-rw-r--r-- | toys/posix/env.c | 2 | ||||
-rw-r--r-- | toys/posix/head.c | 5 | ||||
-rw-r--r-- | toys/posix/id.c | 7 | ||||
-rw-r--r-- | toys/posix/kill.c | 7 | ||||
-rw-r--r-- | toys/posix/ln.c | 5 | ||||
-rw-r--r-- | toys/posix/ls.c | 32 | ||||
-rw-r--r-- | toys/posix/mkdir.c | 5 | ||||
-rw-r--r-- | toys/posix/mkfifo.c | 6 | ||||
-rw-r--r-- | toys/posix/nice.c | 5 | ||||
-rw-r--r-- | toys/posix/od.c | 16 | ||||
-rw-r--r-- | toys/posix/patch.c | 11 | ||||
-rw-r--r-- | toys/posix/sed.c | 5 | ||||
-rw-r--r-- | toys/posix/sh.c | 5 | ||||
-rw-r--r-- | toys/posix/sort.c | 22 | ||||
-rw-r--r-- | toys/posix/tail.c | 9 | ||||
-rw-r--r-- | toys/posix/tee.c | 11 | ||||
-rw-r--r-- | toys/posix/uname.c | 5 | ||||
-rw-r--r-- | toys/posix/uniq.c | 11 | ||||
-rw-r--r-- | toys/posix/wc.c | 5 | ||||
-rw-r--r-- | toys/posix/xargs.c | 7 |
30 files changed, 117 insertions, 253 deletions
diff --git a/toys/posix/chgrp.c b/toys/posix/chgrp.c index 892e78d7..48ce6751 100644 --- a/toys/posix/chgrp.c +++ b/toys/posix/chgrp.c @@ -30,25 +30,16 @@ config CHGRP -v verbose output. */ +#define FOR_chgrp #include "toys.h" -#define FLAG_v 1 -#define FLAG_f 2 -#define FLAG_R 4 -#define FLAG_H 8 -#define FLAG_L 16 -#define FLAG_P 32 -#define FLAG_h 64 - -DEFINE_GLOBALS( +GLOBALS( uid_t owner; gid_t group; char *owner_name, *group_name; int symfollow; ) -#define TT this.chgrp - static int do_chgrp(struct dirtree *node) { int fd, ret, flags = toys.optflags; diff --git a/toys/posix/chmod.c b/toys/posix/chmod.c index 2a32db10..dcef9751 100644 --- a/toys/posix/chmod.c +++ b/toys/posix/chmod.c @@ -34,17 +34,13 @@ config CHMOD chmod 744 file - user can read/write/execute, everyone else read only */ +#define FOR_chmod #include "toys.h" -DEFINE_GLOBALS( +GLOBALS( char *mode; ) -#define TT this.chmod - -#define FLAG_R 1 -#define FLAG_v 2 - int do_chmod(struct dirtree *try) { mode_t mode; diff --git a/toys/posix/cksum.c b/toys/posix/cksum.c index f0aedfd0..3e27b4cb 100644 --- a/toys/posix/cksum.c +++ b/toys/posix/cksum.c @@ -23,14 +23,13 @@ config CKSUM -N Do not include length in CRC calculation */ +#define FOR_cksum #include "toys.h" -DEFINE_GLOBALS( +GLOBALS( unsigned crc_table[256]; ) -#define TT this.cksum - static unsigned cksum_be(unsigned crc, unsigned char c) { return (crc<<8)^TT.crc_table[(crc>>24)^c]; diff --git a/toys/posix/cmp.c b/toys/posix/cmp.c index c41f98b8..4bbd3f30 100644 --- a/toys/posix/cmp.c +++ b/toys/posix/cmp.c @@ -20,18 +20,14 @@ config CMP -s silent */ +#define FOR_cmp #include "toys.h" -#define FLAG_s 1 -#define FLAG_l 2 - -DEFINE_GLOBALS( +GLOBALS( int fd; char *name; ) -#define TT this.cmp - // This handles opening the file and void do_cmp(int fd, char *name) diff --git a/toys/posix/comm.c b/toys/posix/comm.c index 761a62b2..477d5160 100644 --- a/toys/posix/comm.c +++ b/toys/posix/comm.c @@ -24,12 +24,9 @@ config COMM -3 suppress the output column of lines duplicated in FILE1 and FILE2 */ +#define FOR_comm #include "toys.h" -#define FLAG_1 1 -#define FLAG_2 2 -#define FLAG_3 4 - static void writeline(const char *line, int col) { if (col == 0 && toys.optflags & FLAG_1) return; diff --git a/toys/posix/cp.c b/toys/posix/cp.c index 232d8fab..b7834e86 100644 --- a/toys/posix/cp.c +++ b/toys/posix/cp.c @@ -6,53 +6,53 @@ * * See http://opengroup.org/onlinepubs/9699919799/utilities/cp.html * - * "R+ra+d+p+r" -USE_CP(NEWTOY(cp, "<2vslrRdpaHLPif", TOYFLAG_BIN)) + * TODO: "R+ra+d+p+r" sHLPR + +USE_CP(NEWTOY(cp, "<2"USE_CP_MORE("rdavsl")"RHLPfip", TOYFLAG_BIN)) config CP bool "cp (broken by dirtree changes)" default n help - usage: cp -fiprdal SOURCE... DEST + usage: cp [-fipRHLP] SOURCE... DEST - Copy files from SOURCE to DEST. If more than one SOURCE, DEST must - be a directory. + Copy files from SOURCE to DEST. If more than one SOURCE, DEST must + be a directory. -f force copy by deleting destination file -i interactive, prompt before overwriting existing DEST -p preserve timestamps, ownership, and permissions - -r recurse into subdirectories (DEST must be a directory) + -R recurse into subdirectories (DEST must be a directory) + -H Follow symlinks listed on command line + -L Follow all symlinks + -P Do not follow symlinks [default] + +config CP_MORE + bool "cp -rdavsl options" + default y + depends on CP + help + usage: cp [-rdavsl] + + -r synonym for -R -d don't dereference symlinks -a same as -dpr - -l hard link instead of copying + -l hard link instead of copy + -s symlink instead of copy -v verbose */ +#define FOR_cp #include "toys.h" -#define FLAG_f 1 -#define FLAG_i 2 -#define FLAG_P 4 // todo -#define FLAG_L 8 // todo -#define FLAG_H 16 // todo -#define FLAG_a 32 -#define FLAG_p 64 -#define FLAG_d 128 // todo -#define FLAG_R 256 -#define FLAG_r 512 -#define FLAG_l 1024 // todo -#define FLAG_s 2048 // todo -#define FLAG_v 4098 - -DEFINE_GLOBALS( +// TODO: PLHlsd + +GLOBALS( char *destname; int destisdir; - int destisnew; int keep_symlinks; ) -#define TT this.cp - // Copy an individual file or directory to target. void cp_file(char *src, char *dst, struct stat *srcst) @@ -156,42 +156,36 @@ int cp_node(struct dirtree *node) void cp_main(void) { - struct stat st; + char *dpath = NULL; + struct stat st, std; int i; - // Grab target argument. (Guaranteed to be there due to "<2" above.) - - TT.destname = toys.optargs[--toys.optc]; + // Identify destination - // If destination doesn't exist, are we ok with that? + if (!stat(TT.destname, &std) && S_ISDIR(std.st_mode)) TT.destisdir++; + else if (toys.optc>1) error_exit("'%s' not directory", TT.destname); - if (stat(TT.destname, &st)) { - if (toys.optc>1) goto error_notdir; - TT.destisnew++; + // TODO: This is too early: we haven't created it yet if we need to + if (toys.optflags & (FLAG_R|FLAG_r|FLAG_a)) + dpath = realpath(TT.destname = toys.optargs[--toys.optc], NULL); - // If destination exists... - - } else { - if (S_ISDIR(st.st_mode)) TT.destisdir++; - else if (toys.optc > 1) goto error_notdir; - } - - // Handle sources + // Loop through sources for (i=0; i<toys.optc; i++) { - char *src = toys.optargs[i]; - char *dst; + char *dst, *src = toys.optargs[i]; // Skip src==dest (TODO check inodes to catch "cp blah ./blah"). - if (!strcmp(src, TT.destname)) continue; + if (!strncmp(src, TT.destname)) continue; // Skip nonexistent sources. TT.keep_symlinks = toys.optflags & (FLAG_d|FLAG_a); - if (TT.keep_symlinks ? lstat(src, &st) : stat(src, &st)) + if (TT.keep_symlinks ? lstat(src, &st) : stat(src, &st) + || (st.st_dev = dst.st_dev && st.st_ino == dst.dst_ino)) { - perror_msg("'%s'", src); +objection: + perror_msg("bad '%s'", src); toys.exitval = 1; continue; } @@ -199,26 +193,35 @@ void cp_main(void) // Copy directory or file. if (TT.destisdir) { + char *s; + + // Catch "cp -R .. ." and friends that would go on forever + if (dpath && (s = realpath(src, NULL)) { + int i = strlen(s); + i = (!strncmp(s, dst, i) && (!s[i] || s[i]=='/')); + free(s); + + if (i) goto objection; + } + + // Create destination filename within directory dst = strrchr(src, '/'); if (dst) dst++; else dst=src; dst = xmsprintf("%s/%s", TT.destname, dst); } else dst = TT.destname; + if (S_ISDIR(st.st_mode)) { if (toys.optflags & (FLAG_r|FLAG_R|FLAG_a)) { cp_file(src, dst, &st); TT.keep_symlinks++; - strncpy(toybuf, src, sizeof(toybuf)-1); - toybuf[sizeof(toybuf)-1]=0; - dirtree_read(toybuf, cp_node); + dirtree_read(src, cp_node); } else error_msg("Skipped dir '%s'", src); } else cp_file(src, dst, &st); if (TT.destisdir) free(dst); } + if (CFG_TOYBOX_FREE) free(dpath); return; - -error_notdir: - error_exit("'%s' isn't a directory", TT.destname); } diff --git a/toys/posix/date.c b/toys/posix/date.c index 07d8c1d6..9beaaa1a 100644 --- a/toys/posix/date.c +++ b/toys/posix/date.c @@ -17,17 +17,13 @@ config DATE Set/get the current date/time */ +#define FOR_date #include "toys.h" -DEFINE_GLOBALS( +GLOBALS( char *file; ) -#define TT this.date - -#define FLAG_u 1 -#define FLAG_r 2 - void date_main(void) { const char *format_string = "%a %b %e %H:%M:%S %Z %Y"; diff --git a/toys/posix/df.c b/toys/posix/df.c index e889907d..dc87fda3 100644 --- a/toys/posix/df.c +++ b/toys/posix/df.c @@ -37,16 +37,15 @@ config DF_PEDANTIC -k Sets units back to 1024 bytes (the default without -P) */ +#define FOR_df #include "toys.h" -DEFINE_GLOBALS( +GLOBALS( struct arg_list *fstype; long units; ) -#define TT this.df - static void show_mt(struct mtab_list *mt) { int len; @@ -67,7 +66,7 @@ static void show_mt(struct mtab_list *mt) } // If we don't have -a, skip synthetic filesystems - if (!(toys.optflags & 1) && !mt->statvfs.f_blocks) return; + if (!(toys.optflags & FLAG_a) && !mt->statvfs.f_blocks) return; // Figure out how much total/used/free space this filesystem has, // forcing 64-bit math because filesystems are big now. @@ -83,7 +82,7 @@ static void show_mt(struct mtab_list *mt) // Figure out appropriate spacing len = 25 - strlen(mt->device); if (len < 1) len = 1; - if (CFG_DF_PEDANTIC && (toys.optflags & 8)) { + if (CFG_DF_PEDANTIC && (toys.optflags & FLAG_P)) { printf("%s %ld %ld %ld %ld%% %s\n", mt->device, size, used, avail, percent, mt->dir); } else { @@ -98,9 +97,9 @@ void df_main(void) // Handle -P and -k TT.units = 1024; - if (CFG_DF_PEDANTIC && (toys.optflags & 8)) { + if (CFG_DF_PEDANTIC && (toys.optflags & FLAG_P)) { // Units are 512 bytes if you select "pedantic" without "kilobytes". - if ((toys.optflags&3) == 1) TT.units = 512; + if ((toys.optflags&(FLAG_P|FLAG_k)) == FLAG_P) TT.units = 512; printf("Filesystem %ld-blocks Used Available Capacity Mounted on\n", TT.units); } else puts("Filesystem\t1K-blocks\tUsed Available Use% Mounted on"); diff --git a/toys/posix/du.c b/toys/posix/du.c index 83aa88ec..8013810c 100644 --- a/toys/posix/du.c +++ b/toys/posix/du.c @@ -28,9 +28,10 @@ config DU -m Sizes in megabytes */ +#define FOR_du #include "toys.h" -DEFINE_GLOBALS( +GLOBALS( long maxdepth; long depth; long *dirsum; @@ -39,20 +40,6 @@ DEFINE_GLOBALS( struct arg_list *inodes; ) -#define TT this.du - -#define FLAG_x 1 -#define FLAG_s 2 -#define FLAG_L 4 -#define FLAG_k 8 -#define FLAG_H 16 -#define FLAG_a 32 -#define FLAG_c 64 -#define FLAG_l 128 -#define FLAG_m 256 -#define FLAG_h 512 -#define FLAG_d 1024 - typedef struct node_size { struct dirtree *node; long size; diff --git a/toys/posix/echo.c b/toys/posix/echo.c index 576cad70..1bf4d399 100644 --- a/toys/posix/echo.c +++ b/toys/posix/echo.c @@ -32,12 +32,9 @@ config ECHO \xHH hexadecimal values (1 to 2 digits) */ -#define THIS echo +#define FOR_echo #include "toys.h" -#define FLAG_e (1<<1) -#define FLAG_n (1<<0) - void echo_main(void) { int i = 0, out; diff --git a/toys/posix/env.c b/toys/posix/env.c index 4427d01e..32272799 100644 --- a/toys/posix/env.c +++ b/toys/posix/env.c @@ -29,7 +29,7 @@ void env_main(void) char **command = NULL; char *del = "="; - if (toys.optflags & 1) clearenv(); + if (toys.optflags) clearenv(); for (ev = toys.optargs; *ev != NULL; ev++) { char *env, *val = NULL; diff --git a/toys/posix/head.c b/toys/posix/head.c index a7271cd5..77978f74 100644 --- a/toys/posix/head.c +++ b/toys/posix/head.c @@ -20,15 +20,14 @@ config HEAD -n Number of lines to copy. */ +#define FOR_head #include "toys.h" -DEFINE_GLOBALS( +GLOBALS( long lines; int file_no; ) -#define TT this.head - static void do_head(int fd, char *name) { int i, len, lines=TT.lines, size=sizeof(toybuf); diff --git a/toys/posix/id.c b/toys/posix/id.c index d93bc2bc..523d4f3a 100644 --- a/toys/posix/id.c +++ b/toys/posix/id.c @@ -25,14 +25,9 @@ config ID -u Show only the effective user ID */ +#define FOR_id #include "toys.h" -#define FLAG_n (1<<4) -#define FLAG_G (1<<3) -#define FLAG_g (1<<2) -#define FLAG_r (1<<1) -#define FLAG_u 1 - static void s_or_u(char *s, unsigned u, int done) { if (toys.optflags & FLAG_n) printf("%s", s); diff --git a/toys/posix/kill.c b/toys/posix/kill.c index e64a146b..7810b899 100644 --- a/toys/posix/kill.c +++ b/toys/posix/kill.c @@ -18,14 +18,13 @@ config KILL */ +#define FOR_kill #include "toys.h" -DEFINE_GLOBALS( +GLOBALS( char *signame; ) -#define TT this.kill - void kill_main(void) { int signum; @@ -33,7 +32,7 @@ void kill_main(void) pid_t pid; // list signal(s) - if (toys.optflags & 1) { + if (toys.optflags & FLAG_l) { if (*args) { int signum = sig_to_num(*args); char *s = NULL; diff --git a/toys/posix/ln.c b/toys/posix/ln.c index ec3317d2..a83df7f6 100644 --- a/toys/posix/ln.c +++ b/toys/posix/ln.c @@ -22,12 +22,9 @@ config LN -n Symlink at destination treated as file */ +#define FOR_ln #include "toys.h" -#define FLAG_s 1 -#define FLAG_f 2 -#define FLAG_n 4 - void ln_main(void) { char *dest = toys.optargs[--toys.optc], *new; diff --git a/toys/posix/ls.c b/toys/posix/ls.c index a68533ff..6492fb9e 100644 --- a/toys/posix/ls.c +++ b/toys/posix/ls.c @@ -50,40 +50,14 @@ config LS -S size */ +#define FOR_ls #include "toys.h" -#define FLAG_1 (1<<0) -#define FLAG_x (1<<1) -#define FLAG_u (1<<2) -#define FLAG_t (1<<3) -#define FLAG_s (1<<4) -#define FLAG_r (1<<5) -#define FLAG_q (1<<6) -#define FLAG_p (1<<7) -#define FLAG_n (1<<8) -#define FLAG_m (1<<9) -#define FLAG_l (1<<10) -#define FLAG_k (1<<11) -#define FLAG_i (1<<12) -#define FLAG_f (1<<13) -#define FLAG_d (1<<14) -#define FLAG_c (1<<15) -#define FLAG_a (1<<16) -#define FLAG_S (1<<17) -#define FLAG_R (1<<18) -#define FLAG_L (1<<19) -#define FLAG_H (1<<20) -#define FLAG_F (1<<21) -#define FLAG_C (1<<22) -#define FLAG_A (1<<23) -#define FLAG_o (1<<24) -#define FLAG_g (1<<25) - // test sst output (suid/sticky in ls flaglist) // ls -lR starts .: then ./subdir: -DEFINE_GLOBALS( +GLOBALS( struct dirtree *files; unsigned screen_width; @@ -93,8 +67,6 @@ DEFINE_GLOBALS( char uid_buf[12]; ) -#define TT this.ls - void dlist_to_dirtree(struct dirtree *parent) { // Turn double_list into dirtree diff --git a/toys/posix/mkdir.c b/toys/posix/mkdir.c index c3bbb636..e4e591d7 100644 --- a/toys/posix/mkdir.c +++ b/toys/posix/mkdir.c @@ -20,14 +20,13 @@ config MKDIR -p make parent directories as needed. */ +#define FOR_mkdir #include "toys.h" -DEFINE_GLOBALS( +GLOBALS( long mode; ) -#define TT this.mkdir - static int do_mkdir(char *dir) { struct stat buf; diff --git a/toys/posix/mkfifo.c b/toys/posix/mkfifo.c index e5c15389..44775788 100644 --- a/toys/posix/mkfifo.c +++ b/toys/posix/mkfifo.c @@ -19,16 +19,14 @@ config MKFIFO Create FIFOs (named pipes). */ +#define FOR_mkfifo #include "toys.h" -DEFINE_GLOBALS( +GLOBALS( char *m_string; mode_t mode; ) -#define TT this.mkfifo -#define FLAG_m (1) - void mkfifo_main(void) { char **s; diff --git a/toys/posix/nice.c b/toys/posix/nice.c index 4f522aa7..d45429f8 100644 --- a/toys/posix/nice.c +++ b/toys/posix/nice.c @@ -22,14 +22,13 @@ config NICE priority. Only root can set a negative niceness level. */ +#define FOR_nice #include "toys.h" -DEFINE_GLOBALS( +GLOBALS( long priority; ) -#define TT this.nice - void nice_main(void) { if (!toys.optflags) TT.priority = 10; diff --git a/toys/posix/od.c b/toys/posix/od.c index 3c84cb51..ef420b87 100644 --- a/toys/posix/od.c +++ b/toys/posix/od.c @@ -19,20 +19,10 @@ config OD -t output type(s) a (ascii) c (char) d (decimal) foux */ +#define FOR_od #include "toys.h" -#define FLAG_t (1 << 0) -#define FLAG_A (1 << 1) -#define FLAG_b (1 << 2) -#define FLAG_c (1 << 3) -#define FLAG_d (1 << 4) -#define FLAG_o (1 << 5) -#define FLAG_s (1 << 6) -#define FLAG_x (1 << 7) -#define FLAG_N (1 << 8) -#define FLAG_v (1 << 9) - -DEFINE_GLOBALS( +GLOBALS( struct arg_list *output_base; char *address_base; long max_count; @@ -44,8 +34,6 @@ DEFINE_GLOBALS( off_t pos; ) -#define TT this.od - static char *ascii = "nulsohstxetxeotenqackbel bs ht nl vt ff cr so si" "dledc1dc2dc3dc4naksynetbcan emsubesc fs gs rs us sp"; diff --git a/toys/posix/patch.c b/toys/posix/patch.c index a19cc8d3..fae908db 100644 --- a/toys/posix/patch.c +++ b/toys/posix/patch.c @@ -45,9 +45,10 @@ config PATCH created/deleted as appropriate. */ +#define FOR_patch #include "toys.h" -DEFINE_GLOBALS( +GLOBALS( char *infile; long prefix; @@ -58,14 +59,6 @@ DEFINE_GLOBALS( char *tempname; ) -#define TT this.patch - -#define FLAG_R (1<<0) -#define FLAG_i (1<<1) -#define FLAG_l (1<<2) -#define FLAG_p (1<<3) -#define FLAG_u (1<<4) - // Dispose of a line of input, either by writing it out or discarding it. // state < 2: just free diff --git a/toys/posix/sed.c b/toys/posix/sed.c index 4097585e..a3ba9e38 100644 --- a/toys/posix/sed.c +++ b/toys/posix/sed.c @@ -18,15 +18,14 @@ config SED of input. */ +#define FOR_sed #include "toys.h" #include "lib/xregcomp.h" -DEFINE_GLOBALS( +GLOBALS( struct arg_list *commands; ) -#define TT this.sed - struct sed_command { // Doubly linked list of commands. struct sed_command *next, *prev; diff --git a/toys/posix/sh.c b/toys/posix/sh.c index f0bcdc38..44c39119 100644 --- a/toys/posix/sh.c +++ b/toys/posix/sh.c @@ -176,14 +176,13 @@ config CD_P -L Cancel previous -P and restore default behavior. */ +#define FOR_sh #include "toys.h" -DEFINE_GLOBALS( +GLOBALS( char *command; ) -#define TT this.sh - // A single executable, its arguments, and other information we know about it. #define SH_FLAG_EXIT 1 #define SH_FLAG_SUSPEND 2 diff --git a/toys/posix/sort.c b/toys/posix/sort.c index 27aeb777..8f26f5d3 100644 --- a/toys/posix/sort.c +++ b/toys/posix/sort.c @@ -60,9 +60,10 @@ config SORT_FLOAT */ +#define FOR_sort #include "toys.h" -DEFINE_GLOBALS( +GLOBALS( char *key_separator; struct arg_list *raw_keys; char *outfile; @@ -73,30 +74,11 @@ DEFINE_GLOBALS( char **lines; ) -#define TT this.sort - // The sort types are n, g, and M. // u, c, s, and z apply to top level only, not to keys. // b at top level implies bb. // The remaining options can be applied to search keys. -#define FLAG_n (1<<0) // Sort type: numeric -#define FLAG_u (1<<1) // Unique -#define FLAG_r (1<<2) // Reverse output order - -#define FLAG_i (1<<3) // Ignore !isprint() -#define FLAG_f (1<<4) // Force uppercase -#define FLAG_d (1<<5) // Ignore !(isalnum()|isspace()) -#define FLAG_z (1<<6) // Input is null terminated, not \n -#define FLAG_s (1<<7) // Stable sort, no ascii fallback at end -#define FLAG_c (1<<8) // Check only. No output, exit(!ordered) -#define FLAG_M (1<<9) // Sort type: date -#define FLAG_b (1<<10) // Ignore leading blanks -#define FLAG_x (1<<11) // Hex sort -#define FLAG_g (1<<18) // Sort type: strtod() - -// Left off dealing with FLAG_b/FLAG_bb logic... - #define FLAG_bb (1<<31) // Ignore trailing blanks struct sort_key diff --git a/toys/posix/tail.c b/toys/posix/tail.c index 83a47674..d0711c0c 100644 --- a/toys/posix/tail.c +++ b/toys/posix/tail.c @@ -29,21 +29,16 @@ config TAIL_SEEK This version uses lseek, which is faster on large files. */ +#define FOR_tail #include "toys.h" -DEFINE_GLOBALS( +GLOBALS( long lines; long bytes; int file_no; ) -#define TT this.tail - -#define FLAG_n 1 -#define FLAG_c 2 -#define FLAG_f 4 - struct line_list { struct line_list *next, *prev; char *data; diff --git a/toys/posix/tee.c b/toys/posix/tee.c index 15a40fe7..e6342f4c 100644 --- a/toys/posix/tee.c +++ b/toys/posix/tee.c @@ -21,14 +21,13 @@ config TEE -i ignore SIGINT. */ +#define FOR_tee #include "toys.h" -DEFINE_GLOBALS( +GLOBALS( void *outputs; ) -#define TT this.tee - struct fd_list { struct fd_list *next; int fd; @@ -48,12 +47,12 @@ static void do_tee_open(int fd, char *name) void tee_main(void) { - if (toys.optflags&2) signal(SIGINT, SIG_IGN); + if (toys.optflags & FLAG_i) signal(SIGINT, SIG_IGN); // Open output files loopfiles_rw(toys.optargs, - O_RDWR|O_CREAT|((toys.optflags&1)?O_APPEND:O_TRUNC), 0666, 0, - do_tee_open); + O_RDWR|O_CREAT|((toys.optflags & FLAG_a)?O_APPEND:O_TRUNC), + 0666, 0, do_tee_open); for (;;) { struct fd_list *fdl; diff --git a/toys/posix/uname.c b/toys/posix/uname.c index c1f2b490..3c774b3c 100644 --- a/toys/posix/uname.c +++ b/toys/posix/uname.c @@ -24,6 +24,7 @@ config UNAME -a All of the above */ +#define FOR_uname #include "toys.h" // If a 32 bit x86 build environment working in a chroot under an x86-64 @@ -39,15 +40,13 @@ config UNAME #define GROSS "i386" #endif -#define FLAG_a (1<<5) - void uname_main(void) { int i, flags = toys.optflags, needspace=0; uname((void *)toybuf); - if (!flags) flags=1; + if (!flags) flags = FLAG_s; for (i=0; i<5; i++) { char *c = toybuf+(65*i); diff --git a/toys/posix/uniq.c b/toys/posix/uniq.c index 1418904a..f42b7293 100644 --- a/toys/posix/uniq.c +++ b/toys/posix/uniq.c @@ -26,23 +26,16 @@ config UNIQ -s ignore first X chars */ +#define FOR_uniq #include "toys.h" -DEFINE_GLOBALS( +GLOBALS( long maxchars; long nchars; long nfields; long repeats; ) -#define TT this.uniq - -#define FLAG_z 16 -#define FLAG_i 8 -#define FLAG_c 4 -#define FLAG_d 2 -#define FLAG_u 1 - static char *skip(char *str) { long nchars = TT.nchars, nfields; diff --git a/toys/posix/wc.c b/toys/posix/wc.c index 9d11577c..7a5e5a30 100644 --- a/toys/posix/wc.c +++ b/toys/posix/wc.c @@ -24,14 +24,13 @@ config WC argument (or from stdin if none). */ +#define FOR_wc #include "toys.h" -DEFINE_GLOBALS( +GLOBALS( unsigned long totals[3]; ) -#define TT this.wc - static void show_lengths(unsigned long *lengths, char *name) { int i, nospace = 1; diff --git a/toys/posix/xargs.c b/toys/posix/xargs.c index 0d513253..8f19d07f 100644 --- a/toys/posix/xargs.c +++ b/toys/posix/xargs.c @@ -29,9 +29,10 @@ config XARGS -E stop at line matching string */ +#define FOR_xargs #include "toys.h" -DEFINE_GLOBALS( +GLOBALS( long max_bytes; long max_entries; long L; @@ -42,8 +43,6 @@ DEFINE_GLOBALS( char delim; ) -#define TT this.xargs - // If out==NULL count TT.bytes and TT.entries, stopping at max. // Otherwise, fill out out[] @@ -105,7 +104,7 @@ void xargs_main(void) int entries, bytes, done = 0, status; char *data = NULL; - if (!(toys.optflags&1)) TT.delim = '\n'; + if (!(toys.optflags & FLAG_0)) TT.delim = '\n'; // If no optargs, call echo. if (!toys.optc) { |