summaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
authorIngo Schwarze <schwarze@openbsd.org>2018-12-14 01:18:25 +0000
committerIngo Schwarze <schwarze@openbsd.org>2018-12-14 01:18:25 +0000
commitf23ef2791d6fed38e2418e7816d449e955da3808 (patch)
treea0f0c7d4784735ec3a345d1726ce53d8315ba7d8 /main.c
parent89b87d6d86473296fb35daf3da277607a4811967 (diff)
downloadmandoc-f23ef2791d6fed38e2418e7816d449e955da3808.tar.gz
Major cleanup; may imply minor changes in edge cases of error reporting.
Finally, drop support for the run-time configurable mandocmsg() callback. It was over-engineered from the start, never used for anything in a decade, and repeatedly caused maintenance headaches. Consolidate reporting infrastructure into two files, mandoc.h and mandoc_msg.c, mopping up the bits and pieces that were scattered around main.c, read.c, mandoc_parse.h, libmandoc.h, the prototypes of four parsing-related functions, and both parser structs.
Diffstat (limited to 'main.c')
-rw-r--r--main.c110
1 files changed, 37 insertions, 73 deletions
diff --git a/main.c b/main.c
index d00baaa5..f74b84be 100644
--- a/main.c
+++ b/main.c
@@ -82,7 +82,6 @@ struct curparse {
void *outdata; /* data for output */
char *os_s; /* operating system for display */
int wstop; /* stop after a file with a warning */
- enum mandocerr mmin; /* ignore messages below this */
enum mandoc_os os_e; /* check base system conventions */
enum outt outtype; /* which output to use */
};
@@ -90,7 +89,7 @@ struct curparse {
int mandocdb(int, char *[]);
-static void check_xr(const char *);
+static void check_xr(void);
static int fs_lookup(const struct manpaths *,
size_t ipath, const char *,
const char *, const char *,
@@ -100,8 +99,6 @@ static int fs_search(const struct mansearch *,
struct manpage **, size_t *);
static int koptions(int *, char *);
static void moptions(int *, char *);
-static void mmsg(enum mandocerr, enum mandoclevel,
- const char *, int, int, const char *);
static void outdata_alloc(struct curparse *);
static void parse(struct curparse *, int, const char *);
static void passthrough(const char *, int, int);
@@ -113,8 +110,6 @@ static int woptions(struct curparse *, char *);
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 enum mandoclevel rc;
-static FILE *mmsg_stream;
int
@@ -192,10 +187,8 @@ main(int argc, char *argv[])
memset(&curp, 0, sizeof(struct curparse));
curp.outtype = OUTT_LOCALE;
- curp.mmin = MANDOCERR_MAX;
curp.outopts = &conf.output;
options = MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1;
- mmsg_stream = stderr;
use_pager = 1;
tag_files = NULL;
@@ -372,8 +365,6 @@ main(int argc, char *argv[])
#endif
}
- rc = MANDOCLEVEL_OK;
-
/* man(1), whatis(1), apropos(1) */
if (search.argmode != ARG_FILE) {
@@ -415,7 +406,7 @@ main(int argc, char *argv[])
if (sz == 0) {
if (search.argmode != ARG_NAME)
warnx("nothing appropriate");
- rc = MANDOCLEVEL_BADARG;
+ mandoc_msg_setrc(MANDOCLEVEL_BADARG);
goto out;
}
@@ -483,13 +474,15 @@ main(int argc, char *argv[])
moptions(&options, auxpaths);
mchars_alloc();
- curp.mp = mparse_alloc(options, curp.mmin, mmsg,
- curp.os_e, curp.os_s);
+ curp.mp = mparse_alloc(options, curp.os_e, curp.os_s);
if (argc < 1) {
if (use_pager)
tag_files = tag_init();
- parse(&curp, STDIN_FILENO, "<stdin>");
+ thisarg = "<stdin>";
+ mandoc_msg_setinfilename(thisarg);
+ parse(&curp, STDIN_FILENO, thisarg);
+ mandoc_msg_setinfilename(NULL);
}
/*
@@ -513,9 +506,12 @@ main(int argc, char *argv[])
(void)chdir(conf.manpath.paths[resp->ipath]);
else if (startdir != -1)
(void)fchdir(startdir);
- }
+ thisarg = resp->file;
+ } else
+ thisarg = *argv;
- fd = mparse_open(curp.mp, resp != NULL ? resp->file : *argv);
+ mandoc_msg_setinfilename(thisarg);
+ fd = mparse_open(curp.mp, thisarg);
if (fd != -1) {
if (use_pager) {
use_pager = 0;
@@ -527,10 +523,8 @@ main(int argc, char *argv[])
conf.output.tag : *argv;
}
- if (resp == NULL)
- parse(&curp, fd, *argv);
- else if (resp->form == FORM_SRC)
- parse(&curp, fd, resp->file);
+ if (resp == NULL || resp->form == FORM_SRC)
+ parse(&curp, fd, thisarg);
else
passthrough(resp->file, fd,
conf.output.synopsisonly);
@@ -542,7 +536,7 @@ main(int argc, char *argv[])
tag_files = NULL;
} else
warn("stdout");
- rc = MANDOCLEVEL_SYSERR;
+ mandoc_msg_setrc(MANDOCLEVEL_SYSERR);
break;
}
@@ -551,10 +545,10 @@ main(int argc, char *argv[])
outdata_alloc(&curp);
terminal_sepline(curp.outdata);
}
- } else if (rc < MANDOCLEVEL_ERROR)
- rc = MANDOCLEVEL_ERROR;
+ }
+ mandoc_msg_setinfilename(NULL);
- if (MANDOCLEVEL_OK != rc && curp.wstop)
+ if (curp.wstop && mandoc_msg_getrc() != MANDOCLEVEL_OK)
break;
if (resp != NULL)
@@ -645,7 +639,7 @@ out:
if (pid == -1) {
warn("wait");
- rc = MANDOCLEVEL_SYSERR;
+ mandoc_msg_setrc(MANDOCLEVEL_SYSERR);
break;
}
if (!WIFSTOPPED(status))
@@ -655,8 +649,7 @@ out:
}
tag_unlink();
}
-
- return (int)rc;
+ return (int)mandoc_msg_getrc();
}
static void
@@ -806,7 +799,6 @@ fs_search(const struct mansearch *cfg, const struct manpaths *paths,
static void
parse(struct curparse *curp, int fd, const char *file)
{
- enum mandoclevel rctmp;
struct roff_man *man;
/* Begin by parsing the file itself. */
@@ -814,18 +806,16 @@ parse(struct curparse *curp, int fd, const char *file)
assert(file);
assert(fd >= 0);
- rctmp = mparse_readfd(curp->mp, fd, file);
+ mparse_readfd(curp->mp, fd, file);
if (fd != STDIN_FILENO)
close(fd);
- if (rc < rctmp)
- rc = rctmp;
/*
* With -Wstop and warnings or errors of at least the requested
* level, do not produce output.
*/
- if (rctmp != MANDOCLEVEL_OK && curp->wstop)
+ if (curp->wstop && mandoc_msg_getrc() != MANDOCLEVEL_OK)
return;
if (curp->outdata == NULL)
@@ -889,13 +879,12 @@ parse(struct curparse *curp, int fd, const char *file)
break;
}
}
- if (curp->mmin < MANDOCERR_STYLE)
- check_xr(file);
- mparse_updaterc(curp->mp, &rc);
+ if (mandoc_msg_getmin() < MANDOCERR_STYLE)
+ check_xr();
}
static void
-check_xr(const char *file)
+check_xr(void)
{
static struct manpaths paths;
struct mansearch search;
@@ -923,8 +912,7 @@ check_xr(const char *file)
else
mandoc_asprintf(&cp, "Xr %s %s (%d times)",
xr->name, xr->sec, xr->count);
- mmsg(MANDOCERR_XR_BAD, MANDOCLEVEL_STYLE,
- file, xr->line, xr->pos + 1, cp);
+ mandoc_msg(MANDOCERR_XR_BAD, NULL, xr->line, xr->pos + 1, cp);
free(cp);
}
}
@@ -1024,8 +1012,7 @@ done:
fail:
free(line);
warn("%s: SYSERR: %s", file, syscall);
- if (rc < MANDOCLEVEL_SYSERR)
- rc = MANDOCLEVEL_SYSERR;
+ mandoc_msg_setrc(MANDOCLEVEL_SYSERR);
}
static int
@@ -1067,8 +1054,8 @@ toptions(struct curparse *curp, char *arg)
curp->outtype = OUTT_ASCII;
else if (0 == strcmp(arg, "lint")) {
curp->outtype = OUTT_LINT;
- curp->mmin = MANDOCERR_BASE;
- mmsg_stream = stdout;
+ mandoc_msg_setoutfile(stdout);
+ mandoc_msg_setmin(MANDOCERR_BASE);
} else if (0 == strcmp(arg, "tree"))
curp->outtype = OUTT_TREE;
else if (0 == strcmp(arg, "man"))
@@ -1119,29 +1106,29 @@ woptions(struct curparse *curp, char *arg)
break;
case 1:
case 2:
- curp->mmin = MANDOCERR_BASE;
+ mandoc_msg_setmin(MANDOCERR_BASE);
break;
case 3:
- curp->mmin = MANDOCERR_STYLE;
+ mandoc_msg_setmin(MANDOCERR_STYLE);
break;
case 4:
- curp->mmin = MANDOCERR_WARNING;
+ mandoc_msg_setmin(MANDOCERR_WARNING);
break;
case 5:
- curp->mmin = MANDOCERR_ERROR;
+ mandoc_msg_setmin(MANDOCERR_ERROR);
break;
case 6:
- curp->mmin = MANDOCERR_UNSUPP;
+ mandoc_msg_setmin(MANDOCERR_UNSUPP);
break;
case 7:
- curp->mmin = MANDOCERR_MAX;
+ mandoc_msg_setmin(MANDOCERR_MAX);
break;
case 8:
- curp->mmin = MANDOCERR_BASE;
+ mandoc_msg_setmin(MANDOCERR_BASE);
curp->os_e = MANDOC_OS_OPENBSD;
break;
case 9:
- curp->mmin = MANDOCERR_BASE;
+ mandoc_msg_setmin(MANDOCERR_BASE);
curp->os_e = MANDOC_OS_NETBSD;
break;
default:
@@ -1152,29 +1139,6 @@ woptions(struct curparse *curp, char *arg)
return 1;
}
-static void
-mmsg(enum mandocerr t, enum mandoclevel lvl,
- const char *file, int line, int col, const char *msg)
-{
- const char *mparse_msg;
-
- fprintf(mmsg_stream, "%s: %s:", getprogname(),
- file == NULL ? "<stdin>" : file);
-
- if (line)
- fprintf(mmsg_stream, "%d:%d:", line, col + 1);
-
- fprintf(mmsg_stream, " %s", mparse_strlevel(lvl));
-
- if ((mparse_msg = mparse_strerror(t)) != NULL)
- fprintf(mmsg_stream, ": %s", mparse_msg);
-
- if (msg)
- fprintf(mmsg_stream, ": %s", msg);
-
- fputc('\n', mmsg_stream);
-}
-
static pid_t
spawn_pager(struct tag_files *tag_files)
{