-/* $Id: main.c,v 1.246 2015/10/10 13:21:18 schwarze Exp $ */
+/* $Id: main.c,v 1.248 2015/10/12 00:08:15 schwarze Exp $ */
/*
* Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2010-2012, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
#include <assert.h>
#include <ctype.h>
-#include <errno.h>
+#include <err.h>
#include <fcntl.h>
#include <glob.h>
#include <signal.h>
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;
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
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;
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);
*/
if (search.argmode == ARG_NAME) {
- if (*progname == 'h') {
+ if (*__progname == 'h') {
if (argc == 0) {
argv = help_argv;
argc = 1;
fs_search(&search, &conf.manpath,
argc, argv, &res, &sz);
else
- fprintf(stderr,
- "%s: nothing appropriate\n",
- progname);
+ warnx("nothing appropriate");
}
if (sz == 0) {
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);
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);
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--;
curp->outman = man_man;
break;
case OUTT_PDF:
- /* FALLTHROUGH */
case OUTT_ASCII:
- /* FALLTHROUGH */
case OUTT_UTF8:
- /* FALLTHROUGH */
case OUTT_LOCALE:
- /* FALLTHROUGH */
case OUTT_PS:
curp->outman = terminal_man;
curp->outmdoc = terminal_mdoc;
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;
}
} 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;
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;
}
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;
}
curp->wstop = 1;
break;
case 1:
- /* FALLTHROUGH */
case 2:
curp->wlevel = MANDOCLEVEL_WARNING;
break;
curp->wlevel = MANDOCLEVEL_BADARG;
break;
default:
- fprintf(stderr, "%s: -W %s: Bad argument\n",
- progname, o);
+ warnx("-W %s: Bad argument", o);
return 0;
}
}
{
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);
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:
/* 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]);
}