diff options
author | Ingo Schwarze <schwarze@openbsd.org> | 2015-10-11 21:12:54 +0000 |
---|---|---|
committer | Ingo Schwarze <schwarze@openbsd.org> | 2015-10-11 21:12:54 +0000 |
commit | 1efc62347acbd0d7c4f4e3e264fc1678f793560f (patch) | |
tree | f768bf7b681587553ac984a5599df4e4506f4f05 /main.c | |
parent | 4888f53396a76dbdeb8fa67b352672a0b231112f (diff) | |
download | mandoc-1efc62347acbd0d7c4f4e3e264fc1678f793560f.tar.gz |
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's a bad idea to boycott good interfaces merely because standards
committees ignore them. Instead, let's provide compatibility modules
for archaic systems (like commercial Solaris) that still don't have
them. The compat module has an UCB Copyright (c) 1993...
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 81 |
1 files changed, 32 insertions, 49 deletions
@@ -24,7 +24,7 @@ #include <assert.h> #include <ctype.h> -#include <errno.h> +#include <err.h> #include <fcntl.h> #include <glob.h> #include <signal.h> @@ -109,10 +109,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; @@ -139,15 +140,17 @@ main(int argc, char *argv[]) int use_pager; int c; +#if !HAVE_PROGNAME if (argc < 1) - progname = "mandoc"; - else if ((progname = strrchr(argv[0], '/')) == NULL) - progname = argv[0]; + __progname = mandoc_strdup("mandoc"); + else if ((__progname = strrchr(argv[0], '/')) == NULL) + __progname = argv[0]; else - ++progname; + ++__progname; +#endif #if HAVE_SQLITE3 - if (strcmp(progname, BINM_MAKEWHATIS) == 0) + if (strcmp(__progname, BINM_MAKEWHATIS) == 0) return mandocdb(argc, argv); #endif @@ -160,13 +163,13 @@ main(int argc, char *argv[]) memset(&search, 0, sizeof(struct mansearch)); search.outkey = "Nd"; - if (strcmp(progname, BINM_MAN) == 0) + if (strcmp(__progname, BINM_MAN) == 0) search.argmode = ARG_NAME; - else if (strcmp(progname, BINM_APROPOS) == 0) + else if (strcmp(__progname, BINM_APROPOS) == 0) search.argmode = ARG_EXPR; - else if (strcmp(progname, BINM_WHATIS) == 0) + else if (strcmp(__progname, BINM_WHATIS) == 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; @@ -207,15 +210,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); @@ -308,7 +307,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; @@ -364,9 +363,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) { @@ -577,8 +574,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); @@ -588,8 +584,8 @@ fs_lookup(const struct manpaths *paths, size_t ipath, found: #if HAVE_SQLITE3 - 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\n", + name, sec, paths->paths[ipath]); #endif *res = mandoc_reallocarray(*res, ++*ressz, sizeof(struct manpage)); page = *res + (*ressz - 1); @@ -631,9 +627,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--; @@ -804,8 +798,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; } @@ -823,8 +816,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; @@ -843,8 +835,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; } @@ -877,8 +868,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; } @@ -920,8 +910,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; } } @@ -935,7 +924,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); @@ -1001,9 +990,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: @@ -1012,14 +999,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]); } |