From: schwarze Date: Sat, 1 Jul 2017 09:47:23 +0000 (+0000) Subject: Basic reporting of .Xrs to manual pages that don't exist X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=19b6bef7d74b1a8576cd85e21cdc4dd35fd4528d;p=openbsd Basic reporting of .Xrs to manual pages that don't exist in the base system, inspired by mdoclint(1). We are able to do this because (1) the -mdoc parser, the -Tlint validator, and the man(1) manual page lookup code are all in the same program and (2) the mandoc.db(5) database format allows fast lookup. Feedback from, previous versions tested by, and OK jmc@. A few features will be added to this in the tree, step by step. --- diff --git a/usr.bin/mandoc/Makefile b/usr.bin/mandoc/Makefile index 7bc159a2cf9..e62fdec6323 100644 --- a/usr.bin/mandoc/Makefile +++ b/usr.bin/mandoc/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.110 2017/05/12 17:35:34 schwarze Exp $ +# $OpenBSD: Makefile,v 1.111 2017/07/01 09:47:23 schwarze Exp $ .include @@ -6,7 +6,8 @@ CFLAGS += -W -Wall -Wstrict-prototypes -Wno-unused-parameter DPADD += ${LIBUTIL} LDADD += -lutil -lz -SRCS= mandoc_aux.c mandoc_ohash.c mandoc.c chars.c preconv.c read.c \ +SRCS= mandoc_aux.c mandoc_ohash.c mandoc.c mandoc_xr.c \ + chars.c preconv.c read.c \ roff.c roff_validate.c tbl.c tbl_opts.c tbl_layout.c tbl_data.c eqn.c SRCS+= mdoc_macro.c mdoc.c \ mdoc_argv.c mdoc_state.c mdoc_validate.c att.c msec.c st.c @@ -49,7 +50,7 @@ LIBMAN_OBJS = man.o man_macro.o man_validate.o LIBROFF_OBJS = roff.o roff_validate.o eqn.o \ tbl.o tbl_data.o tbl_layout.o tbl_opts.o LIBMANDOC_OBJS = ${LIBMDOC_OBJS} ${LIBMAN_OBJS} ${LIBROFF_OBJS} \ - mandoc.o mandoc_aux.o mandoc_ohash.o \ + mandoc.o mandoc_aux.o mandoc_ohash.o mandoc_xr.o \ chars.o msec.o preconv.o read.o HTML_OBJS = html.o roff_html.o mdoc_html.o man_html.o \ tbl_html.o eqn_html.o out.o diff --git a/usr.bin/mandoc/main.c b/usr.bin/mandoc/main.c index 335ce887c49..221b01516ee 100644 --- a/usr.bin/mandoc/main.c +++ b/usr.bin/mandoc/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.196 2017/06/24 14:38:27 schwarze Exp $ */ +/* $OpenBSD: main.c,v 1.197 2017/07/01 09:47:23 schwarze Exp $ */ /* * Copyright (c) 2008-2012 Kristaps Dzonsons * Copyright (c) 2010-2012, 2014-2017 Ingo Schwarze @@ -37,6 +37,7 @@ #include "mandoc_aux.h" #include "mandoc.h" +#include "mandoc_xr.h" #include "roff.h" #include "mdoc.h" #include "man.h" @@ -80,6 +81,7 @@ struct curparse { int mandocdb(int, char *[]); +static void check_xr(const char *); static int fs_lookup(const struct manpaths *, size_t ipath, const char *, const char *, const char *, @@ -487,6 +489,7 @@ main(int argc, char *argv[]) break; } } + mandoc_xr_free(); mparse_free(curp.mp); mchars_free(); @@ -717,6 +720,8 @@ parse(struct curparse *curp, int fd, const char *file) if (man == NULL) return; + if (curp->mmin < MANDOCERR_STYLE) + mandoc_xr_reset(); if (man->macroset == MACROSET_MDOC) { if (curp->outtype != OUTT_TREE || !curp->outopts->noval) mdoc_validate(man); @@ -768,9 +773,37 @@ parse(struct curparse *curp, int fd, const char *file) break; } } + check_xr(file); mparse_updaterc(curp->mp, &rc); } +static void +check_xr(const char *file) +{ + static struct manpaths paths; + struct mansearch search; + struct mandoc_xr *xr; + char *cp; + size_t sz; + + if (paths.sz == 0) + manpath_base(&paths); + + for (xr = mandoc_xr_get(); xr != NULL; xr = xr->next) { + search.arch = NULL; + search.sec = xr->sec; + search.outkey = NULL; + search.argmode = ARG_NAME; + search.firstmatch = 1; + if (mansearch(&search, &paths, 1, &xr->name, NULL, &sz)) + continue; + mandoc_asprintf(&cp, "Xr %s %s", xr->name, xr->sec); + mmsg(MANDOCERR_XR_BAD, MANDOCLEVEL_STYLE, + file, xr->line, xr->pos + 1, cp); + free(cp); + } +} + static void outdata_alloc(struct curparse *curp) { diff --git a/usr.bin/mandoc/manconf.h b/usr.bin/mandoc/manconf.h index 239e222a4fb..2b3a2dde671 100644 --- a/usr.bin/mandoc/manconf.h +++ b/usr.bin/mandoc/manconf.h @@ -1,6 +1,6 @@ -/* $OpenBSD: manconf.h,v 1.4 2017/02/10 15:44:31 schwarze Exp $ */ +/* $OpenBSD: manconf.h,v 1.5 2017/07/01 09:47:23 schwarze Exp $ */ /* - * Copyright (c) 2011, 2015 Ingo Schwarze + * Copyright (c) 2011, 2015, 2017 Ingo Schwarze * Copyright (c) 2011 Kristaps Dzonsons * * Permission to use, copy, modify, and distribute this software for any @@ -47,3 +47,4 @@ struct manconf { void manconf_parse(struct manconf *, const char *, char *, char *); int manconf_output(struct manoutput *, const char *, int); void manconf_free(struct manconf *); +void manpath_base(struct manpaths *); diff --git a/usr.bin/mandoc/mandoc.1 b/usr.bin/mandoc/mandoc.1 index 88370fc601e..2e4ceffbe4e 100644 --- a/usr.bin/mandoc/mandoc.1 +++ b/usr.bin/mandoc/mandoc.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: mandoc.1,v 1.133 2017/06/29 15:21:46 schwarze Exp $ +.\" $OpenBSD: mandoc.1,v 1.134 2017/07/01 09:47:23 schwarze Exp $ .\" .\" Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons .\" Copyright (c) 2012, 2014-2017 Ingo Schwarze @@ -15,7 +15,7 @@ .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" -.Dd $Mdocdate: June 29 2017 $ +.Dd $Mdocdate: July 1 2017 $ .Dt MANDOC 1 .Os .Sh NAME @@ -836,6 +836,14 @@ generated by CVS or .Ic NetBSD keyword substitution as conventionally used in these operating systems. +.It Sy "referenced manual not found" +.Pq mdoc +An +.Ic \&Xr +macro references a manual page that is not found in the base system. +The path to look for base system manuals is configurable at compile +time and defaults to +.Pa /usr/share/man : /usr/X11R6/man . .El .Ss Style suggestions .Bl -ohang diff --git a/usr.bin/mandoc/mandoc.h b/usr.bin/mandoc/mandoc.h index 89ecbe9bb90..72c7fe7e83a 100644 --- a/usr.bin/mandoc/mandoc.h +++ b/usr.bin/mandoc/mandoc.h @@ -1,4 +1,4 @@ -/* $OpenBSD: mandoc.h,v 1.179 2017/06/29 15:21:46 schwarze Exp $ */ +/* $OpenBSD: mandoc.h,v 1.180 2017/07/01 09:47:23 schwarze Exp $ */ /* * Copyright (c) 2010, 2011, 2014 Kristaps Dzonsons * Copyright (c) 2010-2017 Ingo Schwarze @@ -51,6 +51,7 @@ enum mandocerr { MANDOCERR_ARCH_BAD, /* unknown architecture: Dt ... arch */ MANDOCERR_OS_ARG, /* operating system explicitly specified: Os ... */ MANDOCERR_RCS_MISSING, /* RCS id missing */ + MANDOCERR_XR_BAD, /* referenced manual not found: Xr name sec */ MANDOCERR_STYLE, /* ===== start of style suggestions ===== */ diff --git a/usr.bin/mandoc/mandoc_xr.c b/usr.bin/mandoc/mandoc_xr.c new file mode 100644 index 00000000000..178666319f6 --- /dev/null +++ b/usr.bin/mandoc/mandoc_xr.c @@ -0,0 +1,112 @@ +/* $OpenBSD: mandoc_xr.c,v 1.1 2017/07/01 09:47:23 schwarze Exp $ */ +/* + * Copyright (c) 2017 Ingo Schwarze + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ +#include + +#include +#include +#include +#include +#include + +#include "mandoc_aux.h" +#include "mandoc_ohash.h" +#include "mandoc_xr.h" + +static struct ohash *xr_hash = NULL; +static struct mandoc_xr *xr_first = NULL; +static struct mandoc_xr *xr_last = NULL; + +static void mandoc_xr_clear(void); + + +static void +mandoc_xr_clear(void) +{ + struct mandoc_xr *xr; + unsigned int slot; + + if (xr_hash == NULL) + return; + for (xr = ohash_first(xr_hash, &slot); xr != NULL; + xr = ohash_next(xr_hash, &slot)) + free(xr); + ohash_delete(xr_hash); +} + +void +mandoc_xr_reset(void) +{ + if (xr_hash == NULL) + xr_hash = mandoc_malloc(sizeof(*xr_hash)); + else + mandoc_xr_clear(); + mandoc_ohash_init(xr_hash, 5, + offsetof(struct mandoc_xr, hashkey)); + xr_first = xr_last = NULL; +} + +void +mandoc_xr_add(const char *sec, const char *name, int line, int pos) +{ + struct mandoc_xr *xr; + const char *pend; + size_t ssz, nsz, tsz; + unsigned int slot; + uint32_t hv; + + if (xr_hash == NULL) + return; + + ssz = strlen(sec) + 1; + nsz = strlen(name) + 1; + tsz = ssz + nsz; + xr = mandoc_malloc(sizeof(*xr) + tsz); + xr->next = NULL; + xr->sec = xr->hashkey; + xr->name = xr->hashkey + ssz; + xr->line = line; + xr->pos = pos; + memcpy(xr->sec, sec, ssz); + memcpy(xr->name, name, nsz); + + pend = xr->hashkey + tsz; + hv = ohash_interval(xr->hashkey, &pend); + slot = ohash_lookup_memory(xr_hash, xr->hashkey, tsz, hv); + if (ohash_find(xr_hash, slot) == NULL) { + ohash_insert(xr_hash, slot, xr); + if (xr_first == NULL) + xr_first = xr; + else + xr_last->next = xr; + xr_last = xr; + } else + free(xr); +} + +struct mandoc_xr * +mandoc_xr_get(void) +{ + return xr_first; +} + +void +mandoc_xr_free(void) +{ + mandoc_xr_clear(); + free(xr_hash); + xr_hash = NULL; +} diff --git a/usr.bin/mandoc/mandoc_xr.h b/usr.bin/mandoc/mandoc_xr.h new file mode 100644 index 00000000000..1c9b04345d6 --- /dev/null +++ b/usr.bin/mandoc/mandoc_xr.h @@ -0,0 +1,30 @@ +/* $OpenBSD: mandoc_xr.h,v 1.1 2017/07/01 09:47:23 schwarze Exp $ */ +/* + * Copyright (c) 2017 Ingo Schwarze + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +struct mandoc_xr { + struct mandoc_xr *next; + char *sec; + char *name; + int line; + int pos; + char hashkey[]; +}; + +void mandoc_xr_reset(void); +void mandoc_xr_add(const char *, const char *, int, int); +struct mandoc_xr *mandoc_xr_get(void); +void mandoc_xr_free(void); diff --git a/usr.bin/mandoc/manpath.c b/usr.bin/mandoc/manpath.c index 47ba19036d8..2c6e39b49f4 100644 --- a/usr.bin/mandoc/manpath.c +++ b/usr.bin/mandoc/manpath.c @@ -1,4 +1,4 @@ -/* $OpenBSD: manpath.c,v 1.21 2017/06/13 15:05:22 schwarze Exp $ */ +/* $OpenBSD: manpath.c,v 1.22 2017/07/01 09:47:23 schwarze Exp $ */ /* * Copyright (c) 2011, 2014, 2015, 2017 Ingo Schwarze * Copyright (c) 2011 Kristaps Dzonsons @@ -29,6 +29,7 @@ #include "manconf.h" #define MAN_CONF_FILE "/etc/man.conf" +#define MANPATH_BASE "/usr/share/man:/usr/X11R6/man" #define MANPATH_DEFAULT "/usr/share/man:/usr/X11R6/man:/usr/local/man" static void manconf_file(struct manconf *, const char *); @@ -90,6 +91,13 @@ manconf_parse(struct manconf *conf, const char *file, manpath_parseline(&conf->manpath, defp, 0); } +void +manpath_base(struct manpaths *dirs) +{ + char path_base[] = MANPATH_BASE; + manpath_parseline(dirs, path_base, 0); +} + /* * Parse a FULL pathname from a colon-separated list of arrays. */ diff --git a/usr.bin/mandoc/mansearch.c b/usr.bin/mandoc/mansearch.c index 6932f0d52f0..f9de5f515f8 100644 --- a/usr.bin/mandoc/mansearch.c +++ b/usr.bin/mandoc/mansearch.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mansearch.c,v 1.56 2017/05/17 21:18:41 schwarze Exp $ */ +/* $OpenBSD: mansearch.c,v 1.57 2017/07/01 09:47:23 schwarze Exp $ */ /* * Copyright (c) 2012 Kristaps Dzonsons * Copyright (c) 2013-2017 Ingo Schwarze @@ -101,7 +101,8 @@ mansearch(const struct mansearch *search, } cur = maxres = 0; - *res = NULL; + if (res != NULL) + *res = NULL; outkey = KEY_Nd; if (search->outkey != NULL) @@ -170,6 +171,10 @@ mansearch(const struct mansearch *search, lstmatch(search->arch, page->arch) == 0) continue; + if (res == NULL) { + cur = 1; + break; + } if (cur + 1 > maxres) { maxres += 1024; *res = mandoc_reallocarray(*res, @@ -201,12 +206,13 @@ mansearch(const struct mansearch *search, if (cur && search->firstmatch) break; } - qsort(*res, cur, sizeof(struct manpage), manpage_compare); + if (res != NULL) + qsort(*res, cur, sizeof(struct manpage), manpage_compare); if (chdir_status && getcwd_status && chdir(buf) == -1) warn("%s", buf); exprfree(e); *sz = cur; - return 1; + return res != NULL || cur; } /* diff --git a/usr.bin/mandoc/mdoc_validate.c b/usr.bin/mandoc/mdoc_validate.c index 573b6829133..f0b1d6fccc3 100644 --- a/usr.bin/mandoc/mdoc_validate.c +++ b/usr.bin/mandoc/mdoc_validate.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mdoc_validate.c,v 1.260 2017/06/29 15:21:46 schwarze Exp $ */ +/* $OpenBSD: mdoc_validate.c,v 1.261 2017/07/01 09:47:23 schwarze Exp $ */ /* * Copyright (c) 2008-2012 Kristaps Dzonsons * Copyright (c) 2010-2017 Ingo Schwarze @@ -31,6 +31,7 @@ #include "mandoc_aux.h" #include "mandoc.h" +#include "mandoc_xr.h" #include "roff.h" #include "mdoc.h" #include "libmandoc.h" @@ -2321,8 +2322,11 @@ post_xr(POST_ARGS) if (nch->next == NULL) { mandoc_vmsg(MANDOCERR_XR_NOSEC, mdoc->parse, n->line, n->pos, "Xr %s", nch->string); - } else + } else { assert(nch->next == n->last); + mandoc_xr_add(nch->next->string, nch->string, + nch->line, nch->pos); + } post_delim(mdoc); } diff --git a/usr.bin/mandoc/read.c b/usr.bin/mandoc/read.c index 63eaebf6df5..8c848f1a69e 100644 --- a/usr.bin/mandoc/read.c +++ b/usr.bin/mandoc/read.c @@ -1,4 +1,4 @@ -/* $OpenBSD: read.c,v 1.155 2017/06/29 15:21:46 schwarze Exp $ */ +/* $OpenBSD: read.c,v 1.156 2017/07/01 09:47:23 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2010-2017 Ingo Schwarze @@ -89,6 +89,7 @@ static const char * const mandocerrs[MANDOCERR_MAX] = { "unknown architecture", "operating system explicitly specified", "RCS id missing", + "referenced manual not found", "generic style suggestion",