tr: add dflag, error with usage() on invalid flag combination

Signed-off-by: Hiltjo Posthuma <hiltjo@codemadness.org>
This commit is contained in:
Hiltjo Posthuma 2014-04-09 14:12:34 +02:00 committed by sin
parent 3e49e946b7
commit ff474a8cbc

18
tr.c
View File

@ -12,7 +12,7 @@
static void static void
usage(void) usage(void)
{ {
eprintf("usage: %s set1 [set2]\n", argv0); eprintf("usage: %s [-d] set1 [set2]\n", argv0);
} }
static void static void
@ -117,6 +117,7 @@ main(int argc, char *argv[])
char *buf = NULL; char *buf = NULL;
size_t size = 0; size_t size = 0;
void (*mapfunc)(const wchar_t*, char*); void (*mapfunc)(const wchar_t*, char*);
int dflag = 0;
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
@ -126,6 +127,9 @@ main(int argc, char *argv[])
eprintf("mmap:"); eprintf("mmap:");
ARGBEGIN { ARGBEGIN {
case 'd':
dflag = 1;
break;
default: default:
usage(); usage();
} ARGEND; } ARGEND;
@ -133,12 +137,16 @@ main(int argc, char *argv[])
if(argc == 0) if(argc == 0)
usage(); usage();
if(argc >= 2) { if(dflag) {
parsemapping(argv[0], argv[1], mappings); if(argc >= 2)
mapfunc = maptoset; usage();
} else {
parsemapping(argv[0], NULL, mappings); parsemapping(argv[0], NULL, mappings);
mapfunc = maptonull; mapfunc = maptonull;
} else {
if(argc != 2)
usage();
parsemapping(argv[0], argv[1], mappings);
mapfunc = maptoset;
} }
while(afgets(&buf, &size, stdin)) while(afgets(&buf, &size, stdin))