gawk

[old] Sed-like interface to the Gopher protocol
Log | Files | Refs | LICENSE

commit 885f9e117bac816becba3a804f5dce27e123f168
parent cfd437523755fd2f185176c5065bd8209c68cad5
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date:   Tue,  5 Jan 2021 02:23:31 -0800

Implement costom error reporting functions

Diffstat:
Mcommand.c | 17++++++++---------
Mmain.c | 27+++++++++++++--------------
Mutil.c | 39++++++++++++++++++++++++++++++++-------
Mutil.h | 3+++
4 files changed, 56 insertions(+), 30 deletions(-)

diff --git a/command.c b/command.c @@ -1,4 +1,3 @@ -#include <err.h> #include <errno.h> #include <libgen.h> #include <limits.h> @@ -27,11 +26,11 @@ cextern(int argc, char const **argv, int ino, char const **item) switch (fork()) { case -1: - warn("unable to fork"); + warn(errno, "unable to fork"); return FAIL; case 0: execvp(*argv, argv); - warn("execvp '%s'", *argv); + warn(errno, "execvp '%s'", *argv); _exit(1); } @@ -43,7 +42,7 @@ timid_gopher(char const **addr, char const *path) { if (!exists(path)) return gopher(addr, path); - warnc(EEXIST, "'%s'", path); + warn(EEXIST, "'%s'", path); return 1; } @@ -60,14 +59,14 @@ cfetch(int argc, char const **argv, int ino, char const **item) else { path = basename(item[GI_PATH]); if (path == NULL) { - warn("Unable to get basename '%s'", item[GI_PATH]); + warn(errno, "Unable to get basename '%s'", item[GI_PATH]); return NEXT; } } if (timid_gopher(itemtoaddr(item), path)) return NEXT; - warnx("'%s%s' written to '%s'", item[GI_HOST], item[GI_PATH], path); + warn(0, "'%s%s' written to '%s'", item[GI_HOST], item[GI_PATH], path); return PASS; } @@ -79,7 +78,7 @@ cgoto7(int argc, char const **argv, int ino, char const **item) if (warg(2, 2, argc, argv)) return FAIL; if (**item != '7') { - warnc(EFTYPE, "%d", ino); + warn(EFTYPE, "%d", ino); return FAIL; } @@ -146,7 +145,7 @@ frange(int argc, char const **argv, int ino, char const **item) if (warg(3, -1, argc, argv)) return FAIL; if (argc % 2 != 1) { - warn("%s: Invalid ranges", *argv); + warn(errno, "%s: Invalid ranges", *argv); return FAIL; } @@ -155,7 +154,7 @@ frange(int argc, char const **argv, int ino, char const **item) if (strtorange(&range[j], 0, UINT_MAX, argv[i + j])) return FAIL; if (range[0] > range[1]) { - warn("%s: %d greater than %d", *argv, range[0], range[1]); + warn(errno, "%s: %d greater than %d", *argv, range[0], range[1]); return FAIL; } if (ino < range[0] || ino > range[1]) diff --git a/main.c b/main.c @@ -17,7 +17,6 @@ #include <sys/socket.h> #include <dirent.h> -#include <err.h> #include <errno.h> #include <netdb.h> #include <poll.h> @@ -73,7 +72,7 @@ resolve(char const *const host, char const *const port) error = getaddrinfo(host, port, &hints, &ai0); if (error) { - warnx("Unable to resolve: '%s' at '%s': %s", host, port, gai_strerror(error)); + warn(0, "Unable to resolve: '%s' at '%s': %s", host, port, gai_strerror(error)); return -1; } @@ -88,7 +87,7 @@ resolve(char const *const host, char const *const port) freeaddrinfo(ai0); if (s == -1) { - warnx("unable to connect to '%s' at '%s'", host, port); + warn(0, "unable to connect to '%s' at '%s'", host, port); return -1; } return s; @@ -123,7 +122,7 @@ refill_bufbuf: if (bb[next] == '\0') { if (len >= sizeof(buf) - 1) { too_big: - warnc(E2BIG, NULL); + warn(E2BIG, "input"); return 1; } goto refill_bufbuf; @@ -156,11 +155,11 @@ gph_send(int sock, char const *const message) } if (n == -1) { - warn("unable to send message"); + warn(errno, "unable to send message"); return 1; } if (shutdown(sock, SHUT_WR)) { - warn("shutdown"); + warn(errno, "shutdown"); return 1; } return 0; @@ -177,7 +176,7 @@ gph_recvto(int sock, char const *const path) pfd.fd = sock; pfd.events = POLLRDNORM; if (poll(&pfd, 1, timeout) < 1) { - warn("Unable to recieve response"); + warn(errno, "Unable to recieve response"); return 1; } @@ -282,7 +281,7 @@ run_command(int argc, char const **argv, int depth, char const **addr) c = get_command(*argv, commands, LEN(commands)); if (c == NULL) { - warnx("'%s': %s.", *argv, ENOCOM); + warn(0, "'%s': %s.", *argv, ENOCOM); return ERROR; } return c(argc, argv, depth, addr); @@ -304,12 +303,12 @@ execute(char const *cache, char *input, int depth, char const **addr) cbufslen = argsplit(cbufs, LEN(cbufs), input, IFS1, 0); for (i = 0; i < cbufslen; ++i) { if (i >= MY_PIPE_MAX) { - warnx("Pipeline too long."); + warn(0, "Pipeline too long."); return ERROR; } acs[i] = argsplit(avs[i], LEN(avs[i]), cbufs[i], IFS2, 1); if (acs[i] == 0) { - warnx("Empty pipe (#%d).", ++i); + warn(0, "Empty pipe (#%d).", ++i); return ERROR; } } @@ -320,7 +319,7 @@ execute(char const *cache, char *input, int depth, char const **addr) if (commands[i] == NULL) { if (i == 0) return run_command(acs[0], avs[0], depth, addr); - warn("'%s': %s.", avs[i][0], ENOCOM); + warn(errno, "'%s': %s.", avs[i][0], ENOCOM); return ERROR; } } @@ -378,7 +377,7 @@ execute: if (feof(stdin)) done = depth; else if (ferror(stdin) && !fatal) { - warn("stdin"); + warn(errno, "stdin"); return fatal = -1; } @@ -424,14 +423,14 @@ main(int argc, char const *argv[]) #ifdef __OpenBSD__ if (pledge("stdio rpath wpath cpath inet dns proc exec", NULL) == -1) - err(1, "pledge"); + die("pledge"); #endif /* __OpenBSD__ */ signal(SIGCHLD, SIG_IGN); signal(SIGINT, sighandler); if (mkdtemp(tmpdir) == NULL) - err(1, "mkdtemp '%s'", tmpdir); + die("mkdtemp"); status = sgoto(argc, argv, 0, default_address); if (wremove(tmpdir) == -1) return 1; diff --git a/util.c b/util.c @@ -1,10 +1,13 @@ #include <sys/stat.h> -#include <err.h> #include <errno.h> +#include <stdarg.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> #include <unistd.h> +#include "util.h" + FILE * wfopen(char const *path, char const *mode) { @@ -12,7 +15,7 @@ wfopen(char const *path, char const *mode) fp = fopen(path, mode); if (fp == NULL) - warn("unable to open '%s' ('%s')", path, mode); + warn(errno, "unable to open '%s' ('%s')", path, mode); return fp; } @@ -43,7 +46,7 @@ strtorange(unsigned int *r, unsigned int min, unsigned int max, char const *s) *r = n; return 0; } - warn("'%s'", s); + warn(errno, "'%s'", s); return 1; } @@ -51,10 +54,10 @@ int warg(int min, int max, int argc, char const **ap) { if (max > 0 && argc > max) { - warnx("%s: '%s' and %d more arguments unused.", + warn(0, "%s: '%s' and %d more arguments unused.", *ap, ap[max], argc - (max + 1)); } else if (argc < min) { - warnx("%s: Not enough arguments.", *ap); + warn(0, "%s: Not enough arguments.", *ap); return 1; } @@ -68,7 +71,7 @@ wfclose(FILE *fp) error = ferror(fp); if (fclose(fp) || error) { - warn("unable to close file"); + warn(errno, "unable to close file"); return EOF; } return 0; @@ -78,13 +81,20 @@ int wremove(char const *path) { if (remove(path)) { - warn("unable to remove '%s'", path); + warn(errno, "unable to remove '%s'", path); return -1; } return 0; } void +die(char const *s) +{ + fprintf(stderr, "%s: %s: %s\n", getprogname(), s, strerror(errno)); + exit(EXIT_FAILURE); +} + +void tr(char *r, char const *s, int orig, int repl) { for (; *s != '\0'; ++s, ++r) @@ -94,3 +104,18 @@ tr(char *r, char const *s, int orig, int repl) *r = *s; *r = '\0'; } + +void +warn(int error, char const *format, ...) +{ + va_list ap; + + fprintf(stderr, "%s: ", getprogname()); + va_start(ap, format); + vfprintf(stderr, format, ap); + va_end(ap); + + if (error) + fprintf(stderr, ": %s", strerror(error)); + fputc('\n', stderr); +} diff --git a/util.h b/util.h @@ -1,6 +1,7 @@ #ifndef _util_h #define _util_h +#include <stdarg.h> #include <stdio.h> FILE * wfopen(char const *, char const *); @@ -9,6 +10,8 @@ int strtorange(unsigned int *, unsigned int, unsigned int, char const *); int warg(int, int, int, char const **); int wfclose(FILE *); int wremove(char const *); +void die(char const *); void tr(char *, char const *, int, int); +void warn(int, char const *, ...); #endif /* _util_h */