From eba1598bbc7f18d6171356c7024e7b6bb98fefbd Mon Sep 17 00:00:00 2001 From: schwarze Date: Sun, 11 Oct 2015 21:06:59 +0000 Subject: [PATCH] Finally use __progname, err(3) and warn(3). That's more readable and less error-prone than fumbling around with argv[0], fprintf(3), strerror(3), perror(3), and exit(3). It also shortens the code by 50 lines. It's a bad idea to boycott good interfaces merely because standards committees ignore them. Instead, it's the job of the portable distribution to provide compatibility modules for archaic systems (like commercial Solaris) that still don't have them. Actually, the compat code for the portable distribution already exists and will be committed right after this. --- usr.bin/mandoc/main.c | 82 +++++++++++++------------------------ usr.bin/mandoc/mandoc_aux.c | 39 +++++++----------- usr.bin/mandoc/mandocdb.c | 27 +++++------- usr.bin/mandoc/manpath.c | 15 +++---- usr.bin/mandoc/mansearch.c | 35 ++++++++-------- usr.bin/mandoc/read.c | 21 ++++------ usr.bin/mandoc/term_ps.c | 5 ++- 7 files changed, 86 insertions(+), 138 deletions(-) diff --git a/usr.bin/mandoc/main.c b/usr.bin/mandoc/main.c index c6d3c745bfd..41fb88516ee 100644 --- a/usr.bin/mandoc/main.c +++ b/usr.bin/mandoc/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.151 2015/10/10 13:20:25 schwarze Exp $ */ +/* $OpenBSD: main.c,v 1.152 2015/10/11 21:06:59 schwarze Exp $ */ /* * Copyright (c) 2008-2012 Kristaps Dzonsons * Copyright (c) 2010-2012, 2014, 2015 Ingo Schwarze @@ -23,7 +23,7 @@ #include #include -#include +#include #include #include #include @@ -100,10 +100,11 @@ static int toptions(struct curparse *, char *); static void usage(enum argmode) __attribute__((noreturn)); static int woptions(struct curparse *, char *); +extern char *__progname; + static const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9}; static char help_arg[] = "help"; static char *help_argv[] = {help_arg, NULL}; -static const char *progname; static enum mandoclevel rc; @@ -130,15 +131,8 @@ main(int argc, char *argv[]) int use_pager; int c; - if (argc < 1) - progname = "mandoc"; - else if ((progname = strrchr(argv[0], '/')) == NULL) - progname = argv[0]; - else - ++progname; - - if (0 == strncmp(progname, "mandocdb", 8) || - 0 == strncmp(progname, "makewhatis", 10)) + if (0 == strncmp(__progname, "mandocdb", 8) || + 0 == strncmp(__progname, "makewhatis", 10)) return mandocdb(argc, argv); /* Search options. */ @@ -150,13 +144,13 @@ main(int argc, char *argv[]) memset(&search, 0, sizeof(struct mansearch)); search.outkey = "Nd"; - if (strcmp(progname, "man") == 0) + if (strcmp(__progname, "man") == 0) search.argmode = ARG_NAME; - else if (strncmp(progname, "apropos", 7) == 0) + else if (strncmp(__progname, "apropos", 7) == 0) search.argmode = ARG_EXPR; - else if (strncmp(progname, "whatis", 6) == 0) + else if (strncmp(__progname, "whatis", 6) == 0) search.argmode = ARG_WORD; - else if (strncmp(progname, "help", 4) == 0) + else if (strncmp(__progname, "help", 4) == 0) search.argmode = ARG_NAME; else search.argmode = ARG_FILE; @@ -197,15 +191,11 @@ main(int argc, char *argv[]) break; case 'I': if (strncmp(optarg, "os=", 3)) { - fprintf(stderr, - "%s: -I %s: Bad argument\n", - progname, optarg); + warnx("-I %s: Bad argument", optarg); return (int)MANDOCLEVEL_BADARG; } if (defos) { - fprintf(stderr, - "%s: -I %s: Duplicate argument\n", - progname, optarg); + warnx("-I %s: Duplicate argument", optarg); return (int)MANDOCLEVEL_BADARG; } defos = mandoc_strdup(optarg + 3); @@ -298,7 +288,7 @@ main(int argc, char *argv[]) */ if (search.argmode == ARG_NAME) { - if (*progname == 'h') { + if (*__progname == 'h') { if (argc == 0) { argv = help_argv; argc = 1; @@ -343,9 +333,7 @@ main(int argc, char *argv[]) fs_search(&search, &conf.manpath, argc, argv, &res, &sz); else - fprintf(stderr, - "%s: nothing appropriate\n", - progname); + warnx("nothing appropriate"); } if (sz == 0) { @@ -554,8 +542,7 @@ fs_lookup(const struct manpaths *paths, size_t ipath, paths->paths[ipath], sec, name); globres = glob(file, 0, NULL, &globinfo); if (globres != 0 && globres != GLOB_NOMATCH) - fprintf(stderr, "%s: %s: glob: %s\n", - progname, file, strerror(errno)); + warn("%s: glob", file); free(file); if (globres == 0) file = mandoc_strdup(*globinfo.gl_pathv); @@ -564,8 +551,8 @@ fs_lookup(const struct manpaths *paths, size_t ipath, return 0; found: - fprintf(stderr, "%s: outdated mandoc.db lacks %s(%s) entry, run " - "makewhatis %s\n", progname, name, sec, paths->paths[ipath]); + warnx("outdated mandoc.db lacks %s(%s) entry, run makewhatis %s", + name, sec, paths->paths[ipath]); *res = mandoc_reallocarray(*res, ++*ressz, sizeof(struct manpage)); page = *res + (*ressz - 1); page->file = file; @@ -606,9 +593,7 @@ fs_search(const struct mansearch *cfg, const struct manpaths *paths, return; } if (*ressz == lastsz) - fprintf(stderr, - "%s: No entry for %s in the manual.\n", - progname, *argv); + warnx("No entry for %s in the manual.", *argv); lastsz = *ressz; argv++; argc--; @@ -779,8 +764,7 @@ done: return; fail: - fprintf(stderr, "%s: %s: SYSERR: %s: %s", - progname, file, syscall, strerror(errno)); + warn("%s: SYSERR: %s", file, syscall); if (rc < MANDOCLEVEL_SYSERR) rc = MANDOCLEVEL_SYSERR; } @@ -798,8 +782,7 @@ koptions(int *options, char *arg) } else if ( ! strcmp(arg, "us-ascii")) { *options &= ~(MPARSE_UTF8 | MPARSE_LATIN1); } else { - fprintf(stderr, "%s: -K %s: Bad argument\n", - progname, arg); + warnx("-K %s: Bad argument", arg); return 0; } return 1; @@ -818,8 +801,7 @@ moptions(int *options, char *arg) else if (0 == strcmp(arg, "an")) *options |= MPARSE_MAN; else { - fprintf(stderr, "%s: -m %s: Bad argument\n", - progname, arg); + warnx("-m %s: Bad argument", arg); return 0; } @@ -852,8 +834,7 @@ toptions(struct curparse *curp, char *arg) else if (0 == strcmp(arg, "pdf")) curp->outtype = OUTT_PDF; else { - fprintf(stderr, "%s: -T %s: Bad argument\n", - progname, arg); + warnx("-T %s: Bad argument", arg); return 0; } @@ -895,8 +876,7 @@ woptions(struct curparse *curp, char *arg) curp->wlevel = MANDOCLEVEL_BADARG; break; default: - fprintf(stderr, "%s: -W %s: Bad argument\n", - progname, o); + warnx("-W %s: Bad argument", o); return 0; } } @@ -910,7 +890,7 @@ mmsg(enum mandocerr t, enum mandoclevel lvl, { const char *mparse_msg; - fprintf(stderr, "%s: %s:", progname, file); + fprintf(stderr, "%s: %s:", __progname, file); if (line) fprintf(stderr, "%d:%d:", line, col + 1); @@ -976,9 +956,7 @@ spawn_pager(struct tag_files *tag_files) switch (pager_pid = fork()) { case -1: - fprintf(stderr, "%s: fork: %s\n", - progname, strerror(errno)); - exit((int)MANDOCLEVEL_SYSERR); + err((int)MANDOCLEVEL_SYSERR, "fork"); case 0: break; default: @@ -987,14 +965,10 @@ spawn_pager(struct tag_files *tag_files) /* The child process becomes the pager. */ - if (dup2(tag_files->ofd, STDOUT_FILENO) == -1) { - fprintf(stderr, "pager: stdout: %s\n", strerror(errno)); - exit((int)MANDOCLEVEL_SYSERR); - } + if (dup2(tag_files->ofd, STDOUT_FILENO) == -1) + err((int)MANDOCLEVEL_SYSERR, "pager stdout"); close(tag_files->ofd); close(tag_files->tfd); execvp(argv[0], argv); - fprintf(stderr, "%s: exec %s: %s\n", - progname, argv[0], strerror(errno)); - exit((int)MANDOCLEVEL_SYSERR); + err((int)MANDOCLEVEL_SYSERR, "exec %s", argv[0]); } diff --git a/usr.bin/mandoc/mandoc_aux.c b/usr.bin/mandoc/mandoc_aux.c index d0219b1e038..7e7e19d5729 100644 --- a/usr.bin/mandoc/mandoc_aux.c +++ b/usr.bin/mandoc/mandoc_aux.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mandoc_aux.c,v 1.5 2015/10/06 18:30:43 schwarze Exp $ */ +/* $OpenBSD: mandoc_aux.c,v 1.6 2015/10/11 21:06:59 schwarze Exp $ */ /* * Copyright (c) 2009, 2011 Kristaps Dzonsons * Copyright (c) 2014 Ingo Schwarze @@ -17,6 +17,7 @@ */ #include +#include #include #include #include @@ -35,10 +36,8 @@ mandoc_asprintf(char **dest, const char *fmt, ...) ret = vasprintf(dest, fmt, ap); va_end(ap); - if (-1 == ret) { - perror(NULL); - exit((int)MANDOCLEVEL_SYSERR); - } + if (ret == -1) + err((int)MANDOCLEVEL_SYSERR, NULL); return ret; } @@ -48,10 +47,8 @@ mandoc_calloc(size_t num, size_t size) void *ptr; ptr = calloc(num, size); - if (NULL == ptr) { - perror(NULL); - exit((int)MANDOCLEVEL_SYSERR); - } + if (ptr == NULL) + err((int)MANDOCLEVEL_SYSERR, NULL); return ptr; } @@ -61,10 +58,8 @@ mandoc_malloc(size_t size) void *ptr; ptr = malloc(size); - if (NULL == ptr) { - perror(NULL); - exit((int)MANDOCLEVEL_SYSERR); - } + if (ptr == NULL) + err((int)MANDOCLEVEL_SYSERR, NULL); return ptr; } @@ -73,10 +68,8 @@ mandoc_realloc(void *ptr, size_t size) { ptr = realloc(ptr, size); - if (NULL == ptr) { - perror(NULL); - exit((int)MANDOCLEVEL_SYSERR); - } + if (ptr == NULL) + err((int)MANDOCLEVEL_SYSERR, NULL); return ptr; } @@ -85,10 +78,8 @@ mandoc_reallocarray(void *ptr, size_t num, size_t size) { ptr = reallocarray(ptr, num, size); - if (NULL == ptr) { - perror(NULL); - exit((int)MANDOCLEVEL_SYSERR); - } + if (ptr == NULL) + err((int)MANDOCLEVEL_SYSERR, NULL); return ptr; } @@ -98,10 +89,8 @@ mandoc_strdup(const char *ptr) char *p; p = strdup(ptr); - if (NULL == p) { - perror(NULL); - exit((int)MANDOCLEVEL_SYSERR); - } + if (ptr == NULL) + err((int)MANDOCLEVEL_SYSERR, NULL); return p; } diff --git a/usr.bin/mandoc/mandocdb.c b/usr.bin/mandoc/mandocdb.c index 027ae57078f..1a5f838313a 100644 --- a/usr.bin/mandoc/mandocdb.c +++ b/usr.bin/mandoc/mandocdb.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mandocdb.c,v 1.150 2015/10/06 18:30:43 schwarze Exp $ */ +/* $OpenBSD: mandocdb.c,v 1.151 2015/10/11 21:06:59 schwarze Exp $ */ /* * Copyright (c) 2011, 2012 Kristaps Dzonsons * Copyright (c) 2011-2015 Ingo Schwarze @@ -21,6 +21,7 @@ #include #include +#include #include #include #include @@ -179,8 +180,9 @@ static int set_basedir(const char *, int); static int treescan(void); static size_t utf8(unsigned int, char [7]); +extern char *__progname; + static char tempfilename[32]; -static char *progname; static int nodb; /* no database changes */ static int mparse_options; /* abort the parse early */ static int use_all; /* use all found files */ @@ -347,12 +349,6 @@ mandocdb(int argc, char *argv[]) mpages_info.key_offset = offsetof(struct mpage, inodev); mlinks_info.key_offset = offsetof(struct mlink, file); - progname = strrchr(argv[0], '/'); - if (progname == NULL) - progname = argv[0]; - else - ++progname; - /* * We accept a few different invocations. * The CHECKOP macro makes sure that invocation styles don't @@ -360,8 +356,7 @@ mandocdb(int argc, char *argv[]) */ #define CHECKOP(_op, _ch) do \ if (OP_DEFAULT != (_op)) { \ - fprintf(stderr, "%s: -%c: Conflicting option\n", \ - progname, (_ch)); \ + warnx("-%c: Conflicting option", (_ch)); \ goto usage; \ } while (/*CONSTCOND*/0) @@ -397,9 +392,8 @@ mandocdb(int argc, char *argv[]) break; case 'T': if (strcmp(optarg, "utf8")) { - fprintf(stderr, "%s: -T%s: " - "Unsupported output format\n", - progname, optarg); + warnx("-T%s: Unsupported output format", + optarg); goto usage; } write_utf8 = 1; @@ -426,8 +420,7 @@ mandocdb(int argc, char *argv[]) argv += optind; if (OP_CONFFILE == op && argc > 0) { - fprintf(stderr, "%s: -C: Too many arguments\n", - progname); + warnx("-C: Too many arguments"); goto usage; } @@ -544,8 +537,8 @@ usage: " %s [-DnpQ] [-Tutf8] -d dir [file ...]\n" " %s [-Dnp] -u dir [file ...]\n" " %s [-Q] -t file ...\n", - progname, progname, progname, - progname, progname); + __progname, __progname, __progname, + __progname, __progname); return (int)MANDOCLEVEL_BADARG; } diff --git a/usr.bin/mandoc/manpath.c b/usr.bin/mandoc/manpath.c index 918f0064fcb..9788e60388a 100644 --- a/usr.bin/mandoc/manpath.c +++ b/usr.bin/mandoc/manpath.c @@ -1,4 +1,4 @@ -/* $OpenBSD: manpath.c,v 1.15 2015/05/07 12:07:29 schwarze Exp $ */ +/* $OpenBSD: manpath.c,v 1.16 2015/10/11 21:06:59 schwarze Exp $ */ /* * Copyright (c) 2011, 2014, 2015 Ingo Schwarze * Copyright (c) 2011 Kristaps Dzonsons @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -117,10 +118,8 @@ manpath_add(struct manpaths *dirs, const char *dir, int complain) size_t i; if (NULL == (cp = realpath(dir, buf))) { - if (complain) { - fputs("manpath: ", stderr); - perror(dir); - } + if (complain) + warn("manpath: %s", dir); return; } @@ -129,10 +128,8 @@ manpath_add(struct manpaths *dirs, const char *dir, int complain) return; if (stat(cp, &sb) == -1) { - if (complain) { - fputs("manpath: ", stderr); - perror(dir); - } + if (complain) + warn("manpath: %s", dir); return; } diff --git a/usr.bin/mandoc/mansearch.c b/usr.bin/mandoc/mansearch.c index 725b4e0920e..d4c9754fc3c 100644 --- a/usr.bin/mandoc/mansearch.c +++ b/usr.bin/mandoc/mansearch.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mansearch.c,v 1.45 2015/10/06 18:30:44 schwarze Exp $ */ +/* $OpenBSD: mansearch.c,v 1.46 2015/10/11 21:06:59 schwarze Exp $ */ /* * Copyright (c) 2012 Kristaps Dzonsons * Copyright (c) 2013, 2014, 2015 Ingo Schwarze @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -47,17 +48,17 @@ extern const char *const mansearch_keynames[]; #define SQL_BIND_TEXT(_db, _s, _i, _v) \ do { if (SQLITE_OK != sqlite3_bind_text \ ((_s), (_i)++, (_v), -1, SQLITE_STATIC)) \ - fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \ + warnx("%s", sqlite3_errmsg((_db))); \ } while (0) #define SQL_BIND_INT64(_db, _s, _i, _v) \ do { if (SQLITE_OK != sqlite3_bind_int64 \ ((_s), (_i)++, (_v))) \ - fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \ + warnx("%s", sqlite3_errmsg((_db))); \ } while (0) #define SQL_BIND_BLOB(_db, _s, _i, _v) \ do { if (SQLITE_OK != sqlite3_bind_blob \ ((_s), (_i)++, (&_v), sizeof(_v), SQLITE_STATIC)) \ - fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \ + warnx("%s", sqlite3_errmsg((_db))); \ } while (0) struct expr { @@ -112,7 +113,7 @@ mansearch_setup(int start) if (start) { if (NULL != pagecache) { - fprintf(stderr, "pagecache already enabled\n"); + warnx("pagecache already enabled"); return (int)MANDOCLEVEL_BADARG; } @@ -132,10 +133,10 @@ mansearch_setup(int start) if (SQLITE_OK == c) return (int)MANDOCLEVEL_OK; - fprintf(stderr, "pagecache: %s\n", sqlite3_errstr(c)); + warnx("pagecache: %s", sqlite3_errstr(c)); } else if (NULL == pagecache) { - fprintf(stderr, "pagecache missing\n"); + warnx("pagecache missing"); return (int)MANDOCLEVEL_BADARG; } @@ -225,8 +226,7 @@ mansearch(const struct mansearch *search, for (i = 0; i < paths->sz; i++) { if (chdir_status && paths->paths[i][0] != '/') { if ( ! getcwd_status) { - fprintf(stderr, "%s: getcwd: %s\n", - paths->paths[i], buf); + warnx("%s: getcwd: %s", paths->paths[i], buf); continue; } else if (chdir(buf) == -1) { perror(buf); @@ -243,8 +243,7 @@ mansearch(const struct mansearch *search, SQLITE_OPEN_READONLY, NULL); if (SQLITE_OK != c) { - fprintf(stderr, "%s/%s: %s\n", - paths->paths[i], MANDOC_DB, strerror(errno)); + warn("%s/%s", paths->paths[i], MANDOC_DB); sqlite3_close(db); continue; } @@ -266,7 +265,7 @@ mansearch(const struct mansearch *search, j = 1; c = sqlite3_prepare_v2(db, sql, -1, &s, NULL); if (SQLITE_OK != c) - fprintf(stderr, "%s\n", sqlite3_errmsg(db)); + warnx("%s", sqlite3_errmsg(db)); for (ep = e; NULL != ep; ep = ep->next) { if (NULL == ep->substr) { @@ -308,7 +307,7 @@ mansearch(const struct mansearch *search, } if (SQLITE_DONE != c) - fprintf(stderr, "%s\n", sqlite3_errmsg(db)); + warnx("%s", sqlite3_errmsg(db)); sqlite3_finalize(s); @@ -317,14 +316,14 @@ mansearch(const struct mansearch *search, "WHERE pageid=? ORDER BY sec, arch, name", -1, &s, NULL); if (SQLITE_OK != c) - fprintf(stderr, "%s\n", sqlite3_errmsg(db)); + warnx("%s", sqlite3_errmsg(db)); c = sqlite3_prepare_v2(db, "SELECT bits, key, pageid FROM keys " "WHERE pageid=? AND bits & ?", -1, &s2, NULL); if (SQLITE_OK != c) - fprintf(stderr, "%s\n", sqlite3_errmsg(db)); + warnx("%s", sqlite3_errmsg(db)); for (mp = ohash_first(&htab, &idx); NULL != mp; @@ -505,7 +504,7 @@ buildnames(const struct mansearch *search, struct manpage *mpage, globfree(&globinfo); } if (c != SQLITE_DONE) - fprintf(stderr, "%s\n", sqlite3_errmsg(db)); + warnx("%s", sqlite3_errmsg(db)); sqlite3_reset(s); /* If none of the files is usable, use the first name. */ @@ -555,7 +554,7 @@ buildoutput(sqlite3 *db, sqlite3_stmt *s, uint64_t pageid, uint64_t outbit) output = newoutput; } if (SQLITE_DONE != c) - fprintf(stderr, "%s\n", sqlite3_errmsg(db)); + warnx("%s", sqlite3_errmsg(db)); sqlite3_reset(s); return output; } @@ -797,7 +796,7 @@ exprterm(const struct mansearch *search, char *buf, int cs) free(val); if (irc) { regerror(irc, &e->regexp, errbuf, sizeof(errbuf)); - fprintf(stderr, "regcomp: %s\n", errbuf); + warnx("regcomp: %s", errbuf); free(e); return NULL; } diff --git a/usr.bin/mandoc/read.c b/usr.bin/mandoc/read.c index c8a0017696f..4f5f72240ef 100644 --- a/usr.bin/mandoc/read.c +++ b/usr.bin/mandoc/read.c @@ -1,4 +1,4 @@ -/* $OpenBSD: read.c,v 1.117 2015/10/06 18:30:44 schwarze Exp $ */ +/* $OpenBSD: read.c,v 1.118 2015/10/11 21:06:59 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2010-2015 Ingo Schwarze @@ -22,6 +22,7 @@ #include #include +#include #include #include #include @@ -606,10 +607,8 @@ read_whole_file(struct mparse *curp, const char *file, int fd, size_t off; ssize_t ssz; - if (-1 == fstat(fd, &st)) { - perror(file); - exit((int)MANDOCLEVEL_SYSERR); - } + if (fstat(fd, &st) == -1) + err((int)MANDOCLEVEL_SYSERR, "%s", file); /* * If we're a regular file, try just reading in the whole entry @@ -631,10 +630,8 @@ read_whole_file(struct mparse *curp, const char *file, int fd, } if (curp->gzip) { - if ((gz = gzdopen(fd, "rb")) == NULL) { - perror(file); - exit((int)MANDOCLEVEL_SYSERR); - } + if ((gz = gzdopen(fd, "rb")) == NULL) + err((int)MANDOCLEVEL_SYSERR, "%s", file); } else gz = NULL; @@ -663,10 +660,8 @@ read_whole_file(struct mparse *curp, const char *file, int fd, fb->sz = off; return 1; } - if (ssz == -1) { - perror(file); - exit((int)MANDOCLEVEL_SYSERR); - } + if (ssz == -1) + err((int)MANDOCLEVEL_SYSERR, "%s", file); off += (size_t)ssz; } diff --git a/usr.bin/mandoc/term_ps.c b/usr.bin/mandoc/term_ps.c index 22a25d6cfc1..9648cf3c8b5 100644 --- a/usr.bin/mandoc/term_ps.c +++ b/usr.bin/mandoc/term_ps.c @@ -1,4 +1,4 @@ -/* $OpenBSD: term_ps.c,v 1.40 2015/10/06 18:30:44 schwarze Exp $ */ +/* $OpenBSD: term_ps.c,v 1.41 2015/10/11 21:07:00 schwarze Exp $ */ /* * Copyright (c) 2010, 2011 Kristaps Dzonsons * Copyright (c) 2014, 2015 Ingo Schwarze @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -577,7 +578,7 @@ pspdf_alloc(const struct mchars *mchars, const struct manoutput *outopts) pagex = 216; pagey = 356; } else if (2 != sscanf(pp, "%ux%u", &pagex, &pagey)) - fprintf(stderr, "%s: Unknown paper\n", pp); + warnx("%s: Unknown paper", pp); } /* -- 2.20.1