Add ARGNUM and ARGNUMF(base)

This is useful to support the obsolete syntax -NUM for tools like
head, tail and fold.
This commit is contained in:
dsp 2013-11-11 19:53:01 +00:00 committed by sin
parent 2046778bc9
commit 3f9e501f6b
4 changed files with 23 additions and 3 deletions

15
arg.h
View File

@ -29,11 +29,25 @@ extern char *argv0;
_argc = argv[0][0];\ _argc = argv[0][0];\
switch (_argc) switch (_argc)
/* Handles obsolete -NUM syntax */
#define ARGNUM case '0':\
case '1':\
case '2':\
case '3':\
case '4':\
case '5':\
case '6':\
case '7':\
case '8':\
case '9'
#define ARGEND }\ #define ARGEND }\
} }
#define ARGC() _argc #define ARGC() _argc
#define ARGNUMF(base) (brk = 1, estrtol(argv[0], (base)))
#define EARGF(x) ((argv[0][1] == '\0' && argv[1] == NULL)?\ #define EARGF(x) ((argv[0][1] == '\0' && argv[1] == NULL)?\
((x), abort(), (char *)0) :\ ((x), abort(), (char *)0) :\
(brk = 1, (argv[0][1] != '\0')?\ (brk = 1, (argv[0][1] != '\0')?\
@ -47,4 +61,3 @@ extern char *argv0;
(argc--, argv++, argv[0]))) (argc--, argv++, argv[0])))
#endif #endif

4
fold.c
View File

@ -35,6 +35,9 @@ main(int argc, char *argv[])
case 'w': case 'w':
width = estrtol(EARGF(usage()), 0); width = estrtol(EARGF(usage()), 0);
break; break;
ARGNUM:
width = ARGNUMF(0);
break;
default: default:
usage(); usage();
} ARGEND; } ARGEND;
@ -102,4 +105,3 @@ foldline(const char *str, long width)
putchar('\n'); putchar('\n');
} while(str[i = n] && str[i] != '\n'); } while(str[i = n] && str[i] != '\n');
} }

4
head.c
View File

@ -23,6 +23,9 @@ main(int argc, char *argv[])
case 'n': case 'n':
n = estrtol(EARGF(usage()), 0); n = estrtol(EARGF(usage()), 0);
break; break;
ARGNUM:
n = ARGNUMF(0);
break;
default: default:
usage(); usage();
} ARGEND; } ARGEND;
@ -53,4 +56,3 @@ head(FILE *fp, const char *str, long n)
if(ferror(fp)) if(ferror(fp))
eprintf("%s: read error:", str); eprintf("%s: read error:", str);
} }

3
tail.c
View File

@ -30,6 +30,9 @@ main(int argc, char *argv[])
if(lines[0] == '+') if(lines[0] == '+')
tail = dropinit; tail = dropinit;
break; break;
ARGNUM:
n = ARGNUMF(0);
break;
default: default:
usage(); usage();
} ARGEND; } ARGEND;