Author | Jamozed <[email protected]> |
Date | 2020-07-06 11:15:03 |
Commit | 4540c2aed6d37e6f8391b3eb4c9b3cf1db0d32af |
Parent | 2e3f63023ca450ddc64b742fed1304922c8a214d |
od: Use libokv error reporting
Diffstat
M | src/od.c | | | 48 | ++++++++++++++++++++++-------------------------- |
1 files changed, 22 insertions, 26 deletions
diff --git a/src/od.c b/src/od.c index 4c3067d..5a3da6b 100644 --- a/src/od.c +++ b/src/od.c @@ -1,4 +1,4 @@ -// od.c, version 0.8.0 +// od.c, version 0.8.1 // OMKOV coreutils implementation of POSIX od // Copyright (C) 2020, Jakob Wakeling // All rights reserved. @@ -34,20 +34,24 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. */ /* + TODO Refactor and cleanup to bring code up to scratch + TODO Improve error handling TODO Implement [[+]offset[.][b]] operand as required by POSIX. TODO Fix segfault when using standard input. */ -#include "error.h" #include "optget.h" +#include <error.h> + #include <errno.h> #include <limits.h> #include <stdbool.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> -#include <string.h> + +#define VERSION "0.8.1" typedef struct { union { @@ -96,8 +100,6 @@ static const char *const lTABL[] = { "030", "031", "032", "033", "034", "035", "036", "037", }; -static const char *ARG0; - static char Aform[] = "%08jo"; static bool vflag; static bool Nflag; @@ -126,7 +128,7 @@ static inline void lpush(list_t *list, int i); static void help(void); static void version(void); -int main(int argc, char *argv[]) { ARG0 = argv[0]; +int main(int ac, char *av[]) { A0 = av[0]; lop_t lops[] = { { "help", ARG_NUL, 256 }, { "version", ARG_NUL, 257 }, @@ -135,23 +137,22 @@ int main(int argc, char *argv[]) { ARG0 = argv[0]; opt_t opt = OPTGET_INIT; opt.lops = lops; int o, ret = 0; opt.str = "A:bcdj:N:ost:vx"; tlist = linit(); - while ((o = optget(&opt, argv, 1)) != -1) switch (o) { + while ((o = optget(&opt, av, 1)) != -1) switch (o) { case 'A': switch(opt.arg[0]) { case 'd': { aprint = aprint_generic; Aform[4] = 'u'; continue; } case 'o': { aprint = aprint_generic; Aform[4] = 'o'; continue; } case 'x': { aprint = aprint_generic; Aform[4] = 'x'; continue; } case 'n': { aprint = aprint_none; continue; } - default: { fprintf(stderr, "%s: invalid address base '%c'\n", argv[0], - opt.arg[0]); ret = 1; goto end; }} + default: { warn("%c: invalid address base", opt.arg[0]); ret = 1; + goto end; }} case 'b': { lpush(&tlist, tb); break; } case 'c': { lpush(&tlist, tl); break; } case 'd': { lpush(&tlist, td); break; } case 'j': { errno = 0; start = bparse(opt.arg); if (errno) { - fprintf(stderr, "%s: invalid skip value '%s'\n", argv[0], opt.arg); - ret = 1; goto end; } break; } + warn("%s: invalid skip value", opt.arg); ret = 1; goto end; } break; } case 'N': { errno = 0; limit = bparse(opt.arg); if (errno) { - fprintf(stderr, "%s: invalid limit value '%s'\n", argv[0], opt.arg); - ret = 1; goto end; } Nflag = true; break; } + warn("%s: invalid limit value", opt.arg); ret = 1; goto end; } + Nflag = true; break; } case 'o': { lpush(&tlist, to); break; } case 's': { lpush(&tlist, ts); break; } case 't': { if (tparse(opt.arg)) { goto end; } break; } @@ -163,8 +164,8 @@ int main(int argc, char *argv[]) { ARG0 = argv[0]; } if (!tlist.len) { lpush(&tlist, to); } - if (opt.ind == argc) { file = stdin; } - else { files = argv + opt.ind; file = fnext(); } + if (opt.ind == ac) { file = stdin; } + else { files = av + opt.ind; file = fnext(); } for (size_t i = 0; i < tlist.len; ++i) { int t = tlist.data[i]; if (types[t].padm > mpad) { mpad = types[t].padm; } @@ -179,8 +180,7 @@ int main(int argc, char *argv[]) { ARG0 = argv[0]; block_t b0, b1; int c; bool btok = false, bskp = false; if (skip(start) == EOF) { - fprintf(stderr, "%s: cannot skip past end of input\n", ARG0); - ret = 1; goto end; + warn("cannot skip past end of input"); ret = 1; goto end; } while ((c = bread(btok ? &b0 : &b1)) > 0) { @@ -257,8 +257,7 @@ static inline int tparse(char *str) { register int c; else if ((b = atow(&p)) == -1) { goto fail; } ssize_t m = p - (s + 1); if (!m) { b = sizeof(int); } if (!(b == 1 || b == 2 || b == 4 || b == 8)) { - fprintf(stderr, "%s: no %d-byte integer type\n", ARG0, b); - return 1; + error(1, "no %d-byte integer type", b); } else { c = *s; s += m; } } else if (*s == 'f') { char *p = s + 1; @@ -268,14 +267,12 @@ static inline int tparse(char *str) { register int c; else if ((b = atow(&p)) == -1) { goto fail; } ssize_t m = p - (s + 1); if (!m) { b = sizeof(double); } if (!(b == 4 || b == 8 /*|| b == 16*/)) { - fprintf(stderr, "%s: no %d-byte float type\n", ARG0, b); - return 1; + error(1, "no %d-byte float type", b); } else { c = 'f'; s += m; } } else { -fail: fprintf(stderr, "%s: invalid type string '%s'\n", ARG0, str); - return 1; +fail: error(1, "%s: invalid type string", str); } for (int i = 0; types[i].type; ++i) { @@ -350,8 +347,7 @@ static inline FILE *fnext(void) { if (*files) { FILE *f; if (files[0][0] == '-' && files[0][1] == '\0') { f = stdin; } else if (!(f = fopen(*files, "r"))) { - fprintf(stderr, "%s: %s: %s\n", ARG0, *files, strerror(errno)); - return NULL; + warn("%s: %s\n", *files, strerror(errno)); return NULL; } ++files; return f; } else { return NULL; } } @@ -390,7 +386,7 @@ static void help(void) { } static void version(void) { - puts("OMKOV coreutils od, version 0.8.0"); + puts("OMKOV coreutils od, version " VERSION); puts("Copyright (C) 2020, Jakob Wakeling"); puts("All rights reserved."); puts("OMKOV Permissive Licence (https://www.omkov.net/OLPE)");