From acc3cf3e90c353461fa45612c64c4ea51eadf760 Mon Sep 17 00:00:00 2001 From: emma Date: Fri, 12 Jul 2024 15:43:00 -0600 Subject: [PATCH] swab(1): formatting, remove gotos --- src/npc.c | 51 +++++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/src/npc.c b/src/npc.c index 1f96668..d17f258 100644 --- a/src/npc.c +++ b/src/npc.c @@ -22,7 +22,12 @@ #include /* getopt(3) */ #include -int main(int argc, char *argv[]){ +int usage(char *s) { + fprintf(stderr, "Usage: %s [-et]\n", s); + return EX_USAGE; +} + +int main(int argc, char *argv[]) { int c; char showend; char showtab; @@ -30,32 +35,30 @@ int main(int argc, char *argv[]){ showend = 0; showtab = 0; - if(argc > 0) - while((c = getopt(argc, argv, "et")) != -1) - switch(c){ - case 'e': showend = 1; break; - case 't': showtab = 1; break; - default: goto usage; - } + if(!argc > 0) { usage(argv[0]); } - if(argc > optind){ -usage: fprintf(stderr, "Usage: %s [-et]\n", argv[0]); - return EX_USAGE; + while ((c = getopt(argc, argv, "et")) != -1) { + switch(c){ + case 'e': showend = 1; break; + case 't': showtab = 1; break; + default: return usage(argv[0]); + } } - while((c = getc(stdin)) != EOF){ - if((c & 0x80) != 0) - fputs("M-", stdout); - switch(c ^ 0x80 /* 0b 1000 0000 */){ - case 0x7f: fputs("^?", stdout); - break; - case '\n': if(showend) - putc('$', stdout); - default: - if(c >= ' ' || c == '\n' || (!showtab && c == '\t')) - putc(c, stdout); - else - fprintf(stdout, "^%c", c + '@'); + if(argc > optind) { return usage(argv[0]); } + + while ((c = getc(stdin)) != EOF) { + if ((c & 0x80) != 0) { fputs("M-", stdout); } + + switch (c ^ 0b10000000) { + case 0x7f: fputs("^?", stdout); break; + case '\n': if (showend) { putc('$', stdout); } + default: + if(c >= ' ' || c == '\n' || (!showtab && c == '\t')) { + putc(c, stdout); + } else { + fprintf(stdout, "^%c", c + '@'); + } } }