1 /* $Id: mandocdb.c,v 1.271 2022/04/14 16:43:44 schwarze Exp $ */
3 * Copyright (c) 2011-2021 Ingo Schwarze <schwarze@openbsd.org>
4 * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
5 * Copyright (c) 2016 Ed Maste <emaste@freebsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 * Implementation of the makewhatis(8) program.
23 #include <sys/types.h>
37 #include "compat_fts.h"
51 #include "mandoc_aux.h"
52 #include "mandoc_ohash.h"
57 #include "mandoc_parse.h"
59 #include "mansearch.h"
60 #include "dba_array.h"
63 extern const char *const mansearch_keynames
[];
66 OP_DEFAULT
= 0, /* new dbs from dir list or default config */
67 OP_CONFFILE
, /* new databases from custom config file */
68 OP_UPDATE
, /* delete/add entries in existing database */
69 OP_DELETE
, /* delete entries from existing database */
70 OP_TEST
/* change no databases, report potential problems */
74 const struct mpage
*mpage
; /* if set, the owning parse */
75 uint64_t mask
; /* bitmask in sequence */
76 char key
[]; /* rendered text */
85 struct inodev inodev
; /* used for hashing routine */
86 struct dba_array
*dba
;
87 char *sec
; /* section from file content */
88 char *arch
; /* architecture from file content */
89 char *title
; /* title from file content */
90 char *desc
; /* description from file content */
91 struct mpage
*next
; /* singly linked list */
92 struct mlink
*mlinks
; /* singly linked list */
94 enum form form
; /* format from file content */
98 char file
[PATH_MAX
]; /* filename rel. to manpath */
99 char *dsec
; /* section from directory */
100 char *arch
; /* architecture from directory */
101 char *name
; /* name from file name (not empty) */
102 char *fsec
; /* section from file name suffix */
103 struct mlink
*next
; /* singly linked list */
104 struct mpage
*mpage
; /* parent */
105 int gzip
; /* filename has a .gz suffix */
106 enum form dform
; /* format from directory */
107 enum form fform
; /* format from file name suffix */
110 typedef int (*mdoc_fp
)(struct mpage
*, const struct roff_meta
*,
111 const struct roff_node
*);
113 struct mdoc_handler
{
114 mdoc_fp fp
; /* optional handler */
115 uint64_t mask
; /* set unless handler returns 0 */
116 int taboo
; /* node flags that must not be set */
120 int mandocdb(int, char *[]);
122 static void dbadd(struct dba
*, struct mpage
*);
123 static void dbadd_mlink(const struct mlink
*);
124 static void dbprune(struct dba
*);
125 static void dbwrite(struct dba
*);
126 static void filescan(const char *);
127 #if HAVE_FTS_COMPARE_CONST
128 static int fts_compare(const FTSENT
*const *, const FTSENT
*const *);
130 static int fts_compare(const FTSENT
**, const FTSENT
**);
132 static void mlink_add(struct mlink
*, const struct stat
*);
133 static void mlink_check(struct mpage
*, struct mlink
*);
134 static void mlink_free(struct mlink
*);
135 static void mlinks_undupe(struct mpage
*);
136 static void mpages_free(void);
137 static void mpages_merge(struct dba
*, struct mparse
*);
138 static void parse_cat(struct mpage
*, int);
139 static void parse_man(struct mpage
*, const struct roff_meta
*,
140 const struct roff_node
*);
141 static void parse_mdoc(struct mpage
*, const struct roff_meta
*,
142 const struct roff_node
*);
143 static int parse_mdoc_head(struct mpage
*, const struct roff_meta
*,
144 const struct roff_node
*);
145 static int parse_mdoc_Fa(struct mpage
*, const struct roff_meta
*,
146 const struct roff_node
*);
147 static int parse_mdoc_Fd(struct mpage
*, const struct roff_meta
*,
148 const struct roff_node
*);
149 static void parse_mdoc_fname(struct mpage
*, const struct roff_node
*);
150 static int parse_mdoc_Fn(struct mpage
*, const struct roff_meta
*,
151 const struct roff_node
*);
152 static int parse_mdoc_Fo(struct mpage
*, const struct roff_meta
*,
153 const struct roff_node
*);
154 static int parse_mdoc_Nd(struct mpage
*, const struct roff_meta
*,
155 const struct roff_node
*);
156 static int parse_mdoc_Nm(struct mpage
*, const struct roff_meta
*,
157 const struct roff_node
*);
158 static int parse_mdoc_Sh(struct mpage
*, const struct roff_meta
*,
159 const struct roff_node
*);
160 static int parse_mdoc_Va(struct mpage
*, const struct roff_meta
*,
161 const struct roff_node
*);
162 static int parse_mdoc_Xr(struct mpage
*, const struct roff_meta
*,
163 const struct roff_node
*);
164 static void putkey(const struct mpage
*, char *, uint64_t);
165 static void putkeys(const struct mpage
*, char *, size_t, uint64_t);
166 static void putmdockey(const struct mpage
*,
167 const struct roff_node
*, uint64_t, int);
168 #ifdef READ_ALLOWED_PATH
169 static int read_allowed(const char *);
171 static int render_string(char **, size_t *);
172 static void say(const char *, const char *, ...)
173 __attribute__((__format__ (__printf__
, 2, 3)));
174 static int set_basedir(const char *, int);
175 static int treescan(void);
176 static size_t utf8(unsigned int, char [7]);
178 static int nodb
; /* no database changes */
179 static int mparse_options
; /* abort the parse early */
180 static int use_all
; /* use all found files */
181 static int debug
; /* print what we're doing */
182 static int warnings
; /* warn about crap */
183 static int write_utf8
; /* write UTF-8 output; else ASCII */
184 static int exitcode
; /* to be returned by main */
185 static enum op op
; /* operational mode */
186 static char basedir
[PATH_MAX
]; /* current base directory */
187 static size_t basedir_len
; /* strlen(basedir) */
188 static struct mpage
*mpage_head
; /* list of distinct manual pages */
189 static struct ohash mpages
; /* table of distinct manual pages */
190 static struct ohash mlinks
; /* table of directory entries */
191 static struct ohash names
; /* table of all names */
192 static struct ohash strings
; /* table of all strings */
193 static uint64_t name_mask
;
195 static const struct mdoc_handler mdoc_handlers
[MDOC_MAX
- MDOC_Dd
] = {
196 { NULL
, 0, NODE_NOPRT
}, /* Dd */
197 { NULL
, 0, NODE_NOPRT
}, /* Dt */
198 { NULL
, 0, NODE_NOPRT
}, /* Os */
199 { parse_mdoc_Sh
, TYPE_Sh
, 0 }, /* Sh */
200 { parse_mdoc_head
, TYPE_Ss
, 0 }, /* Ss */
201 { NULL
, 0, 0 }, /* Pp */
202 { NULL
, 0, 0 }, /* D1 */
203 { NULL
, 0, 0 }, /* Dl */
204 { NULL
, 0, 0 }, /* Bd */
205 { NULL
, 0, 0 }, /* Ed */
206 { NULL
, 0, 0 }, /* Bl */
207 { NULL
, 0, 0 }, /* El */
208 { NULL
, 0, 0 }, /* It */
209 { NULL
, 0, 0 }, /* Ad */
210 { NULL
, TYPE_An
, 0 }, /* An */
211 { NULL
, 0, 0 }, /* Ap */
212 { NULL
, TYPE_Ar
, 0 }, /* Ar */
213 { NULL
, TYPE_Cd
, 0 }, /* Cd */
214 { NULL
, TYPE_Cm
, 0 }, /* Cm */
215 { NULL
, TYPE_Dv
, 0 }, /* Dv */
216 { NULL
, TYPE_Er
, 0 }, /* Er */
217 { NULL
, TYPE_Ev
, 0 }, /* Ev */
218 { NULL
, 0, 0 }, /* Ex */
219 { parse_mdoc_Fa
, 0, 0 }, /* Fa */
220 { parse_mdoc_Fd
, 0, 0 }, /* Fd */
221 { NULL
, TYPE_Fl
, 0 }, /* Fl */
222 { parse_mdoc_Fn
, 0, 0 }, /* Fn */
223 { NULL
, TYPE_Ft
| TYPE_Vt
, 0 }, /* Ft */
224 { NULL
, TYPE_Ic
, 0 }, /* Ic */
225 { NULL
, TYPE_In
, 0 }, /* In */
226 { NULL
, TYPE_Li
, 0 }, /* Li */
227 { parse_mdoc_Nd
, 0, 0 }, /* Nd */
228 { parse_mdoc_Nm
, 0, 0 }, /* Nm */
229 { NULL
, 0, 0 }, /* Op */
230 { NULL
, 0, 0 }, /* Ot */
231 { NULL
, TYPE_Pa
, NODE_NOSRC
}, /* Pa */
232 { NULL
, 0, 0 }, /* Rv */
233 { NULL
, TYPE_St
, 0 }, /* St */
234 { parse_mdoc_Va
, TYPE_Va
, 0 }, /* Va */
235 { parse_mdoc_Va
, TYPE_Vt
, 0 }, /* Vt */
236 { parse_mdoc_Xr
, 0, 0 }, /* Xr */
237 { NULL
, 0, 0 }, /* %A */
238 { NULL
, 0, 0 }, /* %B */
239 { NULL
, 0, 0 }, /* %D */
240 { NULL
, 0, 0 }, /* %I */
241 { NULL
, 0, 0 }, /* %J */
242 { NULL
, 0, 0 }, /* %N */
243 { NULL
, 0, 0 }, /* %O */
244 { NULL
, 0, 0 }, /* %P */
245 { NULL
, 0, 0 }, /* %R */
246 { NULL
, 0, 0 }, /* %T */
247 { NULL
, 0, 0 }, /* %V */
248 { NULL
, 0, 0 }, /* Ac */
249 { NULL
, 0, 0 }, /* Ao */
250 { NULL
, 0, 0 }, /* Aq */
251 { NULL
, TYPE_At
, 0 }, /* At */
252 { NULL
, 0, 0 }, /* Bc */
253 { NULL
, 0, 0 }, /* Bf */
254 { NULL
, 0, 0 }, /* Bo */
255 { NULL
, 0, 0 }, /* Bq */
256 { NULL
, TYPE_Bsx
, NODE_NOSRC
}, /* Bsx */
257 { NULL
, TYPE_Bx
, NODE_NOSRC
}, /* Bx */
258 { NULL
, 0, 0 }, /* Db */
259 { NULL
, 0, 0 }, /* Dc */
260 { NULL
, 0, 0 }, /* Do */
261 { NULL
, 0, 0 }, /* Dq */
262 { NULL
, 0, 0 }, /* Ec */
263 { NULL
, 0, 0 }, /* Ef */
264 { NULL
, TYPE_Em
, 0 }, /* Em */
265 { NULL
, 0, 0 }, /* Eo */
266 { NULL
, TYPE_Fx
, NODE_NOSRC
}, /* Fx */
267 { NULL
, TYPE_Ms
, 0 }, /* Ms */
268 { NULL
, 0, 0 }, /* No */
269 { NULL
, 0, 0 }, /* Ns */
270 { NULL
, TYPE_Nx
, NODE_NOSRC
}, /* Nx */
271 { NULL
, TYPE_Ox
, NODE_NOSRC
}, /* Ox */
272 { NULL
, 0, 0 }, /* Pc */
273 { NULL
, 0, 0 }, /* Pf */
274 { NULL
, 0, 0 }, /* Po */
275 { NULL
, 0, 0 }, /* Pq */
276 { NULL
, 0, 0 }, /* Qc */
277 { NULL
, 0, 0 }, /* Ql */
278 { NULL
, 0, 0 }, /* Qo */
279 { NULL
, 0, 0 }, /* Qq */
280 { NULL
, 0, 0 }, /* Re */
281 { NULL
, 0, 0 }, /* Rs */
282 { NULL
, 0, 0 }, /* Sc */
283 { NULL
, 0, 0 }, /* So */
284 { NULL
, 0, 0 }, /* Sq */
285 { NULL
, 0, 0 }, /* Sm */
286 { NULL
, 0, 0 }, /* Sx */
287 { NULL
, TYPE_Sy
, 0 }, /* Sy */
288 { NULL
, TYPE_Tn
, 0 }, /* Tn */
289 { NULL
, 0, NODE_NOSRC
}, /* Ux */
290 { NULL
, 0, 0 }, /* Xc */
291 { NULL
, 0, 0 }, /* Xo */
292 { parse_mdoc_Fo
, 0, 0 }, /* Fo */
293 { NULL
, 0, 0 }, /* Fc */
294 { NULL
, 0, 0 }, /* Oo */
295 { NULL
, 0, 0 }, /* Oc */
296 { NULL
, 0, 0 }, /* Bk */
297 { NULL
, 0, 0 }, /* Ek */
298 { NULL
, 0, 0 }, /* Bt */
299 { NULL
, 0, 0 }, /* Hf */
300 { NULL
, 0, 0 }, /* Fr */
301 { NULL
, 0, 0 }, /* Ud */
302 { NULL
, TYPE_Lb
, NODE_NOSRC
}, /* Lb */
303 { NULL
, 0, 0 }, /* Lp */
304 { NULL
, TYPE_Lk
, 0 }, /* Lk */
305 { NULL
, TYPE_Mt
, NODE_NOSRC
}, /* Mt */
306 { NULL
, 0, 0 }, /* Brq */
307 { NULL
, 0, 0 }, /* Bro */
308 { NULL
, 0, 0 }, /* Brc */
309 { NULL
, 0, 0 }, /* %C */
310 { NULL
, 0, 0 }, /* Es */
311 { NULL
, 0, 0 }, /* En */
312 { NULL
, TYPE_Dx
, NODE_NOSRC
}, /* Dx */
313 { NULL
, 0, 0 }, /* %Q */
314 { NULL
, 0, 0 }, /* %U */
315 { NULL
, 0, 0 }, /* Ta */
320 mandocdb(int argc
, char *argv
[])
325 const char *path_arg
, *progname
;
330 if (pledge("stdio rpath wpath cpath", NULL
) == -1) {
332 return (int)MANDOCLEVEL_SYSERR
;
336 #if HAVE_SANDBOX_INIT
337 if (sandbox_init(kSBXProfileNoInternet
, SANDBOX_NAMED
, NULL
) == -1) {
338 warnx("sandbox_init");
339 return (int)MANDOCLEVEL_SYSERR
;
343 memset(&conf
, 0, sizeof(conf
));
346 * We accept a few different invocations.
347 * The CHECKOP macro makes sure that invocation styles don't
348 * clobber each other.
350 #define CHECKOP(_op, _ch) do \
351 if ((_op) != OP_DEFAULT) { \
352 warnx("-%c: Conflicting option", (_ch)); \
354 } while (/*CONSTCOND*/0)
356 mparse_options
= MPARSE_VALIDATE
;
360 while ((ch
= getopt(argc
, argv
, "aC:Dd:npQT:tu:v")) != -1)
385 mparse_options
|= MPARSE_QUICK
;
388 if (strcmp(optarg
, "utf8") != 0) {
389 warnx("-T%s: Unsupported output format",
397 dup2(STDOUT_FILENO
, STDERR_FILENO
);
407 /* Compatibility with espie@'s makewhatis. */
418 if (pledge("stdio rpath", NULL
) == -1) {
420 return (int)MANDOCLEVEL_SYSERR
;
425 if (op
== OP_CONFFILE
&& argc
> 0) {
426 warnx("-C: Too many arguments");
430 exitcode
= (int)MANDOCLEVEL_OK
;
432 mp
= mparse_alloc(mparse_options
, MANDOC_OS_OTHER
, NULL
);
433 mandoc_ohash_init(&mpages
, 6, offsetof(struct mpage
, inodev
));
434 mandoc_ohash_init(&mlinks
, 6, offsetof(struct mlink
, file
));
436 if (op
== OP_UPDATE
|| op
== OP_DELETE
|| op
== OP_TEST
) {
439 * Most of these deal with a specific directory.
440 * Jump into that directory first.
442 if (op
!= OP_TEST
&& set_basedir(path_arg
, 1) == 0)
445 dba
= nodb
? dba_new(128) : dba_read(MANDOC_DB
);
448 * The existing database is usable. Process
449 * all files specified on the command-line.
452 for (i
= 0; i
< argc
; i
++)
457 /* Database missing or corrupt. */
458 if (op
!= OP_UPDATE
|| errno
!= ENOENT
)
459 say(MANDOC_DB
, "%s: Automatically recreating"
460 " from scratch", strerror(errno
));
461 exitcode
= (int)MANDOCLEVEL_OK
;
468 mpages_merge(dba
, mp
);
474 * If we have arguments, use them as our manpaths.
475 * If we don't, use man.conf(5).
478 conf
.manpath
.paths
= mandoc_reallocarray(NULL
,
479 argc
, sizeof(char *));
480 conf
.manpath
.sz
= (size_t)argc
;
481 for (i
= 0; i
< argc
; i
++)
482 conf
.manpath
.paths
[i
] = mandoc_strdup(argv
[i
]);
484 manconf_parse(&conf
, path_arg
, NULL
, NULL
);
486 if (conf
.manpath
.sz
== 0) {
487 exitcode
= (int)MANDOCLEVEL_BADARG
;
488 say("", "Empty manpath");
492 * First scan the tree rooted at a base directory, then
493 * build a new database and finally move it into place.
494 * Ignore zero-length directories and strip trailing
497 for (j
= 0; j
< conf
.manpath
.sz
; j
++) {
498 sz
= strlen(conf
.manpath
.paths
[j
]);
499 if (sz
&& conf
.manpath
.paths
[j
][sz
- 1] == '/')
500 conf
.manpath
.paths
[j
][--sz
] = '\0';
505 mandoc_ohash_init(&mpages
, 6,
506 offsetof(struct mpage
, inodev
));
507 mandoc_ohash_init(&mlinks
, 6,
508 offsetof(struct mlink
, file
));
511 if (set_basedir(conf
.manpath
.paths
[j
], argc
> 0) == 0)
516 mpages_merge(dba
, mp
);
521 if (j
+ 1 < conf
.manpath
.sz
) {
523 ohash_delete(&mpages
);
524 ohash_delete(&mlinks
);
533 ohash_delete(&mpages
);
534 ohash_delete(&mlinks
);
540 progname
= getprogname();
541 fprintf(stderr
, "usage: %s [-aDnpQ] [-C file] [-Tutf8]\n"
542 " %s [-aDnpQ] [-Tutf8] dir ...\n"
543 " %s [-DnpQ] [-Tutf8] -d dir [file ...]\n"
544 " %s [-Dnp] -u dir [file ...]\n"
545 " %s [-Q] -t file ...\n",
546 progname
, progname
, progname
, progname
, progname
);
548 return (int)MANDOCLEVEL_BADARG
;
552 * To get a singly linked list in alpha order while inserting entries
553 * at the beginning, process directory entries in reverse alpha order.
556 #if HAVE_FTS_COMPARE_CONST
557 fts_compare(const FTSENT
*const *a
, const FTSENT
*const *b
)
559 fts_compare(const FTSENT
**a
, const FTSENT
**b
)
562 return -strcmp((*a
)->fts_name
, (*b
)->fts_name
);
566 * Scan a directory tree rooted at "basedir" for manpages.
567 * We use fts(), scanning directory parts along the way for clues to our
568 * section and architecture.
570 * If use_all has been specified, grok all files.
571 * If not, sanitise paths to the following:
573 * [./]man*[/<arch>]/<name>.<section>
575 * [./]cat<section>[/<arch>]/<name>.0
577 * TODO: accommodate for multi-language directories.
588 char *dsec
, *arch
, *fsec
, *cp
;
595 f
= fts_open((char * const *)argv
, FTS_PHYSICAL
| FTS_NOCHDIR
,
598 exitcode
= (int)MANDOCLEVEL_SYSERR
;
599 say("", "&fts_open");
606 while ((ff
= fts_read(f
)) != NULL
) {
607 path
= ff
->fts_path
+ 2;
608 switch (ff
->fts_info
) {
611 * Symbolic links require various sanity checks,
612 * then get handled just like regular files.
615 if (realpath(path
, buf
) == NULL
) {
617 say(path
, "&realpath");
620 if (strncmp(buf
, basedir
, basedir_len
) != 0
621 #ifdef READ_ALLOWED_PATH
622 && !read_allowed(buf
)
625 if (warnings
) say("",
626 "%s: outside base directory", buf
);
629 /* Use logical inode to avoid mpages dupe. */
630 if (stat(path
, ff
->fts_statp
) == -1) {
635 if ((ff
->fts_statp
->st_mode
& S_IFMT
) != S_IFREG
)
640 * If we're a regular file, add an mlink by using the
641 * stored directory data and handling the filename.
644 if ( ! strcmp(path
, MANDOC_DB
))
646 if ( ! use_all
&& ff
->fts_level
< 2) {
648 say(path
, "Extraneous file");
653 while (fsec
== NULL
) {
654 fsec
= strrchr(ff
->fts_name
, '.');
655 if (fsec
== NULL
|| strcmp(fsec
+1, "gz"))
665 "No filename suffix");
668 } else if ( ! strcmp(++fsec
, "html")) {
670 say(path
, "Skip html");
672 } else if ( ! strcmp(fsec
, "ps")) {
674 say(path
, "Skip ps");
676 } else if ( ! strcmp(fsec
, "pdf")) {
678 say(path
, "Skip pdf");
680 } else if ( ! use_all
&&
681 ((dform
== FORM_SRC
&&
682 strncmp(fsec
, dsec
, strlen(dsec
))) ||
683 (dform
== FORM_CAT
&& strcmp(fsec
, "0")))) {
685 say(path
, "Wrong filename suffix");
690 mlink
= mandoc_calloc(1, sizeof(struct mlink
));
691 if (strlcpy(mlink
->file
, path
,
692 sizeof(mlink
->file
)) >=
693 sizeof(mlink
->file
)) {
694 say(path
, "Filename too long");
698 mlink
->dform
= dform
;
701 mlink
->name
= ff
->fts_name
;
704 mlink_add(mlink
, ff
->fts_statp
);
713 say(path
, "Not a regular file");
717 switch (ff
->fts_level
) {
719 /* Ignore the root directory. */
723 * This might contain manX/ or catX/.
724 * Try to infer this from the name.
725 * If we're not in use_all, enforce it.
728 if (ff
->fts_info
== FTS_DP
) {
734 if ( ! strncmp(cp
, "man", 3)) {
737 } else if ( ! strncmp(cp
, "cat", 3)) {
745 if (dsec
!= NULL
|| use_all
)
749 say(path
, "Unknown directory part");
750 fts_set(f
, ff
, FTS_SKIP
);
754 * Possibly our architecture.
755 * If we're descending, keep tabs on it.
757 if (ff
->fts_info
!= FTS_DP
&& dsec
!= NULL
)
763 if (ff
->fts_info
== FTS_DP
|| use_all
)
766 say(path
, "Extraneous directory part");
767 fts_set(f
, ff
, FTS_SKIP
);
777 * Add a file to the mlinks table.
778 * Do not verify that it's a "valid" looking manpage (we'll do that
781 * Try to infer the manual section, architecture, and page name from the
782 * path, assuming it looks like
784 * [./]man*[/<arch>]/<name>.<section>
786 * [./]cat<section>[/<arch>]/<name>.0
788 * See treescan() for the fts(3) version of this.
791 filescan(const char *infile
)
795 char *linkfile
, *p
, *realdir
, *start
, *usefile
;
800 if (strncmp(infile
, "./", 2) == 0)
804 * We have to do lstat(2) before realpath(3) loses
805 * the information whether this is a symbolic link.
806 * We need to know that because for symbolic links,
807 * we want to use the orginal file name, while for
808 * regular files, we want to use the real path.
810 if (lstat(infile
, &st
) == -1) {
811 exitcode
= (int)MANDOCLEVEL_BADARG
;
812 say(infile
, "&lstat");
814 } else if (S_ISREG(st
.st_mode
) == 0 && S_ISLNK(st
.st_mode
) == 0) {
815 exitcode
= (int)MANDOCLEVEL_BADARG
;
816 say(infile
, "Not a regular file");
821 * We have to resolve the file name to the real path
822 * in any case for the base directory check.
824 if ((usefile
= realpath(infile
, NULL
)) == NULL
) {
825 exitcode
= (int)MANDOCLEVEL_BADARG
;
826 say(infile
, "&realpath");
832 else if (strncmp(usefile
, basedir
, basedir_len
) == 0)
833 start
= usefile
+ basedir_len
;
834 #ifdef READ_ALLOWED_PATH
835 else if (read_allowed(usefile
))
839 exitcode
= (int)MANDOCLEVEL_BADARG
;
840 say("", "%s: outside base directory", infile
);
846 * Now we are sure the file is inside our tree.
847 * If it is a symbolic link, ignore the real path
848 * and use the original name.
851 if (S_ISLNK(st
.st_mode
) == 0)
855 * Some implementations of realpath(3) may succeed
856 * even if the target of the link does not exist,
857 * so check again for extra safety.
859 if (stat(usefile
, &st
) == -1) {
860 exitcode
= (int)MANDOCLEVEL_BADARG
;
861 say(infile
, "&stat");
865 linkfile
= mandoc_strdup(infile
);
868 start
= usefile
= linkfile
;
871 if (strncmp(infile
, basedir
, basedir_len
) == 0) {
874 start
= usefile
+ basedir_len
;
879 * This symbolic link points into the basedir
880 * from the outside. Let's see whether any of
881 * the parent directories resolve to the basedir.
883 p
= strchr(linkfile
, '\0');
888 if ((realdir
= realpath(linkfile
, NULL
)) == NULL
) {
889 exitcode
= (int)MANDOCLEVEL_BADARG
;
890 say(infile
, "&realpath");
895 realdir_len
= strlen(realdir
) + 1;
898 } while (realdir_len
> basedir_len
);
901 * If one of the directories resolves to the basedir,
902 * use the rest of the original name.
903 * Otherwise, the best we can do
904 * is to use the filename pointed to.
906 if (realdir_len
== basedir_len
) {
912 start
= usefile
+ basedir_len
;
914 } while (/* CONSTCOND */ 0);
916 mlink
= mandoc_calloc(1, sizeof(struct mlink
));
917 mlink
->dform
= FORM_NONE
;
918 if (strlcpy(mlink
->file
, start
, sizeof(mlink
->file
)) >=
919 sizeof(mlink
->file
)) {
920 say(start
, "Filename too long");
927 * In test mode or when the original name is absolute
928 * but outside our tree, guess the base directory.
931 if (op
== OP_TEST
|| (start
== usefile
&& *start
== '/')) {
932 if (strncmp(usefile
, "man/", 4) == 0)
934 else if ((start
= strstr(usefile
, "/man/")) != NULL
)
941 * First try to guess our directory structure.
942 * If we find a separator, try to look for man* or cat*.
943 * If we find one of these and what's underneath is a directory,
944 * assume it's an architecture.
946 if ((p
= strchr(start
, '/')) != NULL
) {
948 if (strncmp(start
, "man", 3) == 0) {
949 mlink
->dform
= FORM_SRC
;
950 mlink
->dsec
= start
+ 3;
951 } else if (strncmp(start
, "cat", 3) == 0) {
952 mlink
->dform
= FORM_CAT
;
953 mlink
->dsec
= start
+ 3;
957 if (mlink
->dsec
!= NULL
&& (p
= strchr(start
, '/')) != NULL
) {
965 * Now check the file suffix.
966 * Suffix of `.0' indicates a catpage, `.1-9' is a manpage.
968 p
= strrchr(start
, '\0');
969 while (p
-- > start
&& *p
!= '/' && *p
!= '.')
978 * Now try to parse the name.
979 * Use the filename portion of the path.
982 if ((p
= strrchr(start
, '/')) != NULL
) {
986 mlink_add(mlink
, &st
);
991 mlink_add(struct mlink
*mlink
, const struct stat
*st
)
993 struct inodev inodev
;
997 assert(NULL
!= mlink
->file
);
999 mlink
->dsec
= mandoc_strdup(mlink
->dsec
? mlink
->dsec
: "");
1000 mlink
->arch
= mandoc_strdup(mlink
->arch
? mlink
->arch
: "");
1001 mlink
->name
= mandoc_strdup(mlink
->name
? mlink
->name
: "");
1002 mlink
->fsec
= mandoc_strdup(mlink
->fsec
? mlink
->fsec
: "");
1004 if ('0' == *mlink
->fsec
) {
1006 mlink
->fsec
= mandoc_strdup(mlink
->dsec
);
1007 mlink
->fform
= FORM_CAT
;
1008 } else if ('1' <= *mlink
->fsec
&& '9' >= *mlink
->fsec
)
1009 mlink
->fform
= FORM_SRC
;
1011 mlink
->fform
= FORM_NONE
;
1013 slot
= ohash_qlookup(&mlinks
, mlink
->file
);
1014 assert(NULL
== ohash_find(&mlinks
, slot
));
1015 ohash_insert(&mlinks
, slot
, mlink
);
1017 memset(&inodev
, 0, sizeof(inodev
)); /* Clear padding. */
1018 inodev
.st_ino
= st
->st_ino
;
1019 inodev
.st_dev
= st
->st_dev
;
1020 slot
= ohash_lookup_memory(&mpages
, (char *)&inodev
,
1021 sizeof(struct inodev
), inodev
.st_ino
);
1022 mpage
= ohash_find(&mpages
, slot
);
1023 if (NULL
== mpage
) {
1024 mpage
= mandoc_calloc(1, sizeof(struct mpage
));
1025 mpage
->inodev
.st_ino
= inodev
.st_ino
;
1026 mpage
->inodev
.st_dev
= inodev
.st_dev
;
1027 mpage
->form
= FORM_NONE
;
1028 mpage
->next
= mpage_head
;
1030 ohash_insert(&mpages
, slot
, mpage
);
1032 mlink
->next
= mpage
->mlinks
;
1033 mpage
->mlinks
= mlink
;
1034 mlink
->mpage
= mpage
;
1038 mlink_free(struct mlink
*mlink
)
1051 struct mpage
*mpage
;
1052 struct mlink
*mlink
;
1054 while ((mpage
= mpage_head
) != NULL
) {
1055 while ((mlink
= mpage
->mlinks
) != NULL
) {
1056 mpage
->mlinks
= mlink
->next
;
1059 mpage_head
= mpage
->next
;
1069 * For each mlink to the mpage, check whether the path looks like
1070 * it is formatted, and if it does, check whether a source manual
1071 * exists by the same name, ignoring the suffix.
1072 * If both conditions hold, drop the mlink.
1075 mlinks_undupe(struct mpage
*mpage
)
1078 struct mlink
**prev
;
1079 struct mlink
*mlink
;
1082 mpage
->form
= FORM_CAT
;
1083 prev
= &mpage
->mlinks
;
1084 while (NULL
!= (mlink
= *prev
)) {
1085 if (FORM_CAT
!= mlink
->dform
) {
1086 mpage
->form
= FORM_NONE
;
1089 (void)strlcpy(buf
, mlink
->file
, sizeof(buf
));
1090 bufp
= strstr(buf
, "cat");
1091 assert(NULL
!= bufp
);
1092 memcpy(bufp
, "man", 3);
1093 if (NULL
!= (bufp
= strrchr(buf
, '.')))
1095 (void)strlcat(buf
, mlink
->dsec
, sizeof(buf
));
1096 if (NULL
== ohash_find(&mlinks
,
1097 ohash_qlookup(&mlinks
, buf
)))
1100 say(mlink
->file
, "Man source exists: %s", buf
);
1103 *prev
= mlink
->next
;
1107 prev
= &(*prev
)->next
;
1112 mlink_check(struct mpage
*mpage
, struct mlink
*mlink
)
1118 * Check whether the manual section given in a file
1119 * agrees with the directory where the file is located.
1120 * Some manuals have suffixes like (3p) on their
1121 * section number either inside the file or in the
1122 * directory name, some are linked into more than one
1123 * section, like encrypt(1) = makekey(8).
1126 if (FORM_SRC
== mpage
->form
&&
1127 strcasecmp(mpage
->sec
, mlink
->dsec
))
1128 say(mlink
->file
, "Section \"%s\" manual in %s directory",
1129 mpage
->sec
, mlink
->dsec
);
1132 * Manual page directories exist for each kernel
1133 * architecture as returned by machine(1).
1134 * However, many manuals only depend on the
1135 * application architecture as returned by arch(1).
1136 * For example, some (2/ARM) manuals are shared
1137 * across the "armish" and "zaurus" kernel
1139 * A few manuals are even shared across completely
1140 * different architectures, for example fdformat(1)
1141 * on amd64, i386, and sparc64.
1144 if (strcasecmp(mpage
->arch
, mlink
->arch
))
1145 say(mlink
->file
, "Architecture \"%s\" manual in "
1146 "\"%s\" directory", mpage
->arch
, mlink
->arch
);
1150 * parse_cat() doesn't set NAME_TITLE yet.
1153 if (FORM_CAT
== mpage
->form
)
1157 * Check whether this mlink
1158 * appears as a name in the NAME section.
1161 slot
= ohash_qlookup(&names
, mlink
->name
);
1162 str
= ohash_find(&names
, slot
);
1163 assert(NULL
!= str
);
1164 if ( ! (NAME_TITLE
& str
->mask
))
1165 say(mlink
->file
, "Name missing in NAME section");
1169 * Run through the files in the global vector "mpages"
1170 * and add them to the database specified in "basedir".
1172 * This handles the parsing scheme itself, using the cues of directory
1173 * and filename to determine whether the file is parsable or not.
1176 mpages_merge(struct dba
*dba
, struct mparse
*mp
)
1178 struct mpage
*mpage
, *mpage_dest
;
1179 struct mlink
*mlink
, *mlink_dest
;
1180 struct roff_meta
*meta
;
1184 for (mpage
= mpage_head
; mpage
!= NULL
; mpage
= mpage
->next
) {
1185 mlinks_undupe(mpage
);
1186 if ((mlink
= mpage
->mlinks
) == NULL
)
1189 name_mask
= NAME_MASK
;
1190 mandoc_ohash_init(&names
, 4, offsetof(struct str
, key
));
1191 mandoc_ohash_init(&strings
, 6, offsetof(struct str
, key
));
1195 if ((fd
= mparse_open(mp
, mlink
->file
)) == -1) {
1196 say(mlink
->file
, "&open");
1201 * Interpret the file as mdoc(7) or man(7) source
1202 * code, unless it is known to be formatted.
1204 if (mlink
->dform
!= FORM_CAT
|| mlink
->fform
!= FORM_CAT
) {
1205 mparse_readfd(mp
, fd
, mlink
->file
);
1208 meta
= mparse_result(mp
);
1211 if (meta
!= NULL
&& meta
->sodest
!= NULL
) {
1212 mlink_dest
= ohash_find(&mlinks
,
1213 ohash_qlookup(&mlinks
, meta
->sodest
));
1214 if (mlink_dest
== NULL
) {
1215 mandoc_asprintf(&cp
, "%s.gz", meta
->sodest
);
1216 mlink_dest
= ohash_find(&mlinks
,
1217 ohash_qlookup(&mlinks
, cp
));
1220 if (mlink_dest
!= NULL
) {
1222 /* The .so target exists. */
1224 mpage_dest
= mlink_dest
->mpage
;
1226 mlink
->mpage
= mpage_dest
;
1229 * If the target was already
1230 * processed, add the links
1231 * to the database now.
1232 * Otherwise, this will
1233 * happen when we come
1237 if (mpage_dest
->dba
!= NULL
)
1240 if (mlink
->next
== NULL
)
1242 mlink
= mlink
->next
;
1245 /* Move all links to the target. */
1247 mlink
->next
= mlink_dest
->next
;
1248 mlink_dest
->next
= mpage
->mlinks
;
1249 mpage
->mlinks
= NULL
;
1252 meta
->macroset
= MACROSET_NONE
;
1254 if (meta
!= NULL
&& meta
->macroset
== MACROSET_MDOC
) {
1255 mpage
->form
= FORM_SRC
;
1256 mpage
->sec
= meta
->msec
;
1257 mpage
->sec
= mandoc_strdup(
1258 mpage
->sec
== NULL
? "" : mpage
->sec
);
1259 mpage
->arch
= meta
->arch
;
1260 mpage
->arch
= mandoc_strdup(
1261 mpage
->arch
== NULL
? "" : mpage
->arch
);
1262 mpage
->title
= mandoc_strdup(meta
->title
);
1263 } else if (meta
!= NULL
&& meta
->macroset
== MACROSET_MAN
) {
1264 if (*meta
->msec
!= '\0' || *meta
->title
!= '\0') {
1265 mpage
->form
= FORM_SRC
;
1266 mpage
->sec
= mandoc_strdup(meta
->msec
);
1267 mpage
->arch
= mandoc_strdup(mlink
->arch
);
1268 mpage
->title
= mandoc_strdup(meta
->title
);
1273 assert(mpage
->desc
== NULL
);
1274 if (meta
== NULL
|| meta
->sodest
!= NULL
) {
1275 mpage
->sec
= mandoc_strdup(mlink
->dsec
);
1276 mpage
->arch
= mandoc_strdup(mlink
->arch
);
1277 mpage
->title
= mandoc_strdup(mlink
->name
);
1279 mpage
->form
= FORM_CAT
;
1280 parse_cat(mpage
, fd
);
1282 mpage
->form
= FORM_SRC
;
1283 } else if (meta
->macroset
== MACROSET_MDOC
)
1284 parse_mdoc(mpage
, meta
, meta
->first
);
1286 parse_man(mpage
, meta
, meta
->first
);
1287 if (mpage
->desc
== NULL
) {
1288 mpage
->desc
= mandoc_strdup(mlink
->name
);
1290 say(mlink
->file
, "No one-line description, "
1291 "using filename \"%s\"", mlink
->name
);
1294 for (mlink
= mpage
->mlinks
;
1296 mlink
= mlink
->next
) {
1297 putkey(mpage
, mlink
->name
, NAME_FILE
);
1298 if (warnings
&& !use_all
)
1299 mlink_check(mpage
, mlink
);
1305 ohash_delete(&strings
);
1306 ohash_delete(&names
);
1311 parse_cat(struct mpage
*mpage
, int fd
)
1314 struct mlink
*mlink
;
1315 char *line
, *p
, *title
, *sec
;
1316 size_t linesz
, plen
, titlesz
;
1320 mlink
= mpage
->mlinks
;
1321 stream
= fd
== -1 ? fopen(mlink
->file
, "r") : fdopen(fd
, "r");
1322 if (stream
== NULL
) {
1326 say(mlink
->file
, "&fopen");
1333 /* Parse the section number from the header line. */
1335 while (getline(&line
, &linesz
, stream
) != -1) {
1338 if ((sec
= strchr(line
, '(')) == NULL
)
1340 if ((p
= strchr(++sec
, ')')) == NULL
)
1343 mpage
->sec
= mandoc_strndup(sec
, p
- sec
);
1344 if (warnings
&& *mlink
->dsec
!= '\0' &&
1345 strcasecmp(mpage
->sec
, mlink
->dsec
))
1347 "Section \"%s\" manual in %s directory",
1348 mpage
->sec
, mlink
->dsec
);
1352 /* Skip to first blank line. */
1354 while (line
== NULL
|| *line
!= '\n')
1355 if (getline(&line
, &linesz
, stream
) == -1)
1359 * Assume the first line that is not indented
1360 * is the first section header. Skip to it.
1363 while (getline(&line
, &linesz
, stream
) != -1)
1364 if (*line
!= '\n' && *line
!= ' ')
1368 * Read up until the next section into a buffer.
1369 * Strip the leading and trailing newline from each read line,
1370 * appending a trailing space.
1371 * Ignore empty (whitespace-only) lines.
1377 while ((len
= getline(&line
, &linesz
, stream
)) != -1) {
1381 while (isspace((unsigned char)line
[offs
]))
1383 if (line
[offs
] == '\0')
1385 title
= mandoc_realloc(title
, titlesz
+ len
- offs
);
1386 memcpy(title
+ titlesz
, line
+ offs
, len
- offs
);
1387 titlesz
+= len
- offs
;
1388 title
[titlesz
- 1] = ' ';
1393 * If no page content can be found, or the input line
1394 * is already the next section header, or there is no
1395 * trailing newline, reuse the page title as the page
1399 if (NULL
== title
|| '\0' == *title
) {
1401 say(mlink
->file
, "Cannot find NAME section");
1407 title
[titlesz
- 1] = '\0';
1410 * Skip to the first dash.
1411 * Use the remaining line as the description (no more than 70
1415 if (NULL
!= (p
= strstr(title
, "- "))) {
1416 for (p
+= 2; ' ' == *p
|| '\b' == *p
; p
++)
1417 /* Skip to next word. */ ;
1420 say(mlink
->file
, "No dash in title line, "
1421 "reusing \"%s\" as one-line description", title
);
1427 /* Strip backspace-encoding from line. */
1429 while (NULL
!= (line
= memchr(p
, '\b', plen
))) {
1432 memmove(line
, line
+ 1, plen
--);
1435 memmove(line
- 1, line
+ 1, plen
- len
);
1440 * Cut off excessive one-line descriptions.
1441 * Bad pages are not worth better heuristics.
1444 mpage
->desc
= mandoc_strndup(p
, 150);
1450 * Put a type/word pair into the word database for this particular file.
1453 putkey(const struct mpage
*mpage
, char *value
, uint64_t type
)
1455 putkeys(mpage
, value
, strlen(value
), type
);
1459 * Grok all nodes at or below a certain mdoc node into putkey().
1462 putmdockey(const struct mpage
*mpage
,
1463 const struct roff_node
*n
, uint64_t m
, int taboo
)
1466 for ( ; NULL
!= n
; n
= n
->next
) {
1467 if (n
->flags
& taboo
)
1469 if (NULL
!= n
->child
)
1470 putmdockey(mpage
, n
->child
, m
, taboo
);
1471 if (n
->type
== ROFFT_TEXT
)
1472 putkey(mpage
, n
->string
, m
);
1477 parse_man(struct mpage
*mpage
, const struct roff_meta
*meta
,
1478 const struct roff_node
*n
)
1480 const struct roff_node
*head
, *body
;
1481 char *start
, *title
;
1489 * We're only searching for one thing: the first text child in
1490 * the BODY of a NAME section. Since we don't keep track of
1491 * sections in -man, run some hoops to find out whether we're in
1492 * the correct section or not.
1495 if (n
->type
== ROFFT_BODY
&& n
->tok
== MAN_SH
) {
1497 if ((head
= body
->parent
->head
) != NULL
&&
1498 (head
= head
->child
) != NULL
&&
1499 head
->next
== NULL
&&
1500 head
->type
== ROFFT_TEXT
&&
1501 strcmp(head
->string
, "NAME") == 0 &&
1502 body
->child
!= NULL
) {
1505 * Suck the entire NAME section into memory.
1506 * Yes, we might run away.
1507 * But too many manuals have big, spread-out
1508 * NAME sections over many lines.
1512 deroff(&title
, body
);
1517 * Go through a special heuristic dance here.
1518 * Conventionally, one or more manual names are
1519 * comma-specified prior to a whitespace, then a
1520 * dash, then a description. Try to puzzle out
1521 * the name parts here.
1526 sz
= strcspn(start
, " ,");
1527 if ('\0' == start
[sz
])
1534 * Assume a stray trailing comma in the
1535 * name list if a name begins with a dash.
1538 if ('-' == start
[0] ||
1539 ('\\' == start
[0] && '-' == start
[1]))
1542 putkey(mpage
, start
, NAME_TITLE
);
1543 if ( ! (mpage
->name_head_done
||
1544 strcasecmp(start
, meta
->title
))) {
1545 putkey(mpage
, start
, NAME_HEAD
);
1546 mpage
->name_head_done
= 1;
1554 assert(',' == byte
);
1556 while (' ' == *start
)
1560 if (start
== title
) {
1561 putkey(mpage
, start
, NAME_TITLE
);
1562 if ( ! (mpage
->name_head_done
||
1563 strcasecmp(start
, meta
->title
))) {
1564 putkey(mpage
, start
, NAME_HEAD
);
1565 mpage
->name_head_done
= 1;
1571 while (isspace((unsigned char)*start
))
1574 if (0 == strncmp(start
, "-", 1))
1576 else if (0 == strncmp(start
, "\\-\\-", 4))
1578 else if (0 == strncmp(start
, "\\-", 2))
1580 else if (0 == strncmp(start
, "\\(en", 4))
1582 else if (0 == strncmp(start
, "\\(em", 4))
1585 while (' ' == *start
)
1589 * Cut off excessive one-line descriptions.
1590 * Bad pages are not worth better heuristics.
1593 mpage
->desc
= mandoc_strndup(start
, 150);
1599 for (n
= n
->child
; n
; n
= n
->next
) {
1600 if (NULL
!= mpage
->desc
)
1602 parse_man(mpage
, meta
, n
);
1607 parse_mdoc(struct mpage
*mpage
, const struct roff_meta
*meta
,
1608 const struct roff_node
*n
)
1610 const struct mdoc_handler
*handler
;
1612 for (n
= n
->child
; n
!= NULL
; n
= n
->next
) {
1613 if (n
->tok
== TOKEN_NONE
|| n
->tok
< ROFF_MAX
)
1615 assert(n
->tok
>= MDOC_Dd
&& n
->tok
< MDOC_MAX
);
1616 handler
= mdoc_handlers
+ (n
->tok
- MDOC_Dd
);
1617 if (n
->flags
& handler
->taboo
)
1626 if (handler
->fp
!= NULL
&&
1627 (*handler
->fp
)(mpage
, meta
, n
) == 0)
1630 putmdockey(mpage
, n
->child
,
1631 handler
->mask
, handler
->taboo
);
1636 if (NULL
!= n
->child
)
1637 parse_mdoc(mpage
, meta
, n
);
1642 parse_mdoc_Fa(struct mpage
*mpage
, const struct roff_meta
*meta
,
1643 const struct roff_node
*n
)
1648 if (n
->sec
== SEC_SYNOPSIS
)
1651 putmdockey(mpage
, n
->child
, mask
, 0);
1656 parse_mdoc_Fd(struct mpage
*mpage
, const struct roff_meta
*meta
,
1657 const struct roff_node
*n
)
1662 if (SEC_SYNOPSIS
!= n
->sec
||
1663 NULL
== (n
= n
->child
) ||
1664 n
->type
!= ROFFT_TEXT
)
1668 * Only consider those `Fd' macro fields that begin with an
1669 * "inclusion" token (versus, e.g., #define).
1672 if (strcmp("#include", n
->string
))
1675 if ((n
= n
->next
) == NULL
|| n
->type
!= ROFFT_TEXT
)
1679 * Strip away the enclosing angle brackets and make sure we're
1684 if ('<' == *start
|| '"' == *start
)
1687 if (0 == (sz
= strlen(start
)))
1690 end
= &start
[(int)sz
- 1];
1691 if ('>' == *end
|| '"' == *end
)
1695 putkeys(mpage
, start
, end
- start
+ 1, TYPE_In
);
1700 parse_mdoc_fname(struct mpage
*mpage
, const struct roff_node
*n
)
1705 if (n
->type
!= ROFFT_TEXT
)
1708 /* Skip function pointer punctuation. */
1711 while (*cp
== '(' || *cp
== '*')
1713 sz
= strcspn(cp
, "()");
1715 putkeys(mpage
, cp
, sz
, TYPE_Fn
);
1716 if (n
->sec
== SEC_SYNOPSIS
)
1717 putkeys(mpage
, cp
, sz
, NAME_SYN
);
1721 parse_mdoc_Fn(struct mpage
*mpage
, const struct roff_meta
*meta
,
1722 const struct roff_node
*n
)
1726 if (n
->child
== NULL
)
1729 parse_mdoc_fname(mpage
, n
->child
);
1732 if (n
!= NULL
&& n
->type
== ROFFT_TEXT
) {
1734 if (n
->sec
== SEC_SYNOPSIS
)
1736 putmdockey(mpage
, n
, mask
, 0);
1743 parse_mdoc_Fo(struct mpage
*mpage
, const struct roff_meta
*meta
,
1744 const struct roff_node
*n
)
1747 if (n
->type
!= ROFFT_HEAD
)
1750 if (n
->child
!= NULL
)
1751 parse_mdoc_fname(mpage
, n
->child
);
1757 parse_mdoc_Va(struct mpage
*mpage
, const struct roff_meta
*meta
,
1758 const struct roff_node
*n
)
1762 if (n
->type
!= ROFFT_ELEM
&& n
->type
!= ROFFT_BODY
)
1765 if (n
->child
!= NULL
&&
1766 n
->child
->next
== NULL
&&
1767 n
->child
->type
== ROFFT_TEXT
)
1773 putkey(mpage
, cp
, TYPE_Vt
| (n
->tok
== MDOC_Va
||
1774 n
->type
== ROFFT_BODY
? TYPE_Va
: 0));
1782 parse_mdoc_Xr(struct mpage
*mpage
, const struct roff_meta
*meta
,
1783 const struct roff_node
*n
)
1787 if (NULL
== (n
= n
->child
))
1790 if (NULL
== n
->next
) {
1791 putkey(mpage
, n
->string
, TYPE_Xr
);
1795 mandoc_asprintf(&cp
, "%s(%s)", n
->string
, n
->next
->string
);
1796 putkey(mpage
, cp
, TYPE_Xr
);
1802 parse_mdoc_Nd(struct mpage
*mpage
, const struct roff_meta
*meta
,
1803 const struct roff_node
*n
)
1806 if (n
->type
== ROFFT_BODY
)
1807 deroff(&mpage
->desc
, n
);
1812 parse_mdoc_Nm(struct mpage
*mpage
, const struct roff_meta
*meta
,
1813 const struct roff_node
*n
)
1816 if (SEC_NAME
== n
->sec
)
1817 putmdockey(mpage
, n
->child
, NAME_TITLE
, 0);
1818 else if (n
->sec
== SEC_SYNOPSIS
&& n
->type
== ROFFT_HEAD
) {
1819 if (n
->child
== NULL
)
1820 putkey(mpage
, meta
->name
, NAME_SYN
);
1822 putmdockey(mpage
, n
->child
, NAME_SYN
, 0);
1824 if ( ! (mpage
->name_head_done
||
1825 n
->child
== NULL
|| n
->child
->string
== NULL
||
1826 strcasecmp(n
->child
->string
, meta
->title
))) {
1827 putkey(mpage
, n
->child
->string
, NAME_HEAD
);
1828 mpage
->name_head_done
= 1;
1834 parse_mdoc_Sh(struct mpage
*mpage
, const struct roff_meta
*meta
,
1835 const struct roff_node
*n
)
1838 return n
->sec
== SEC_CUSTOM
&& n
->type
== ROFFT_HEAD
;
1842 parse_mdoc_head(struct mpage
*mpage
, const struct roff_meta
*meta
,
1843 const struct roff_node
*n
)
1846 return n
->type
== ROFFT_HEAD
;
1850 * Add a string to the hash table for the current manual.
1851 * Each string has a bitmask telling which macros it belongs to.
1852 * When we finish the manual, we'll dump the table.
1855 putkeys(const struct mpage
*mpage
, char *cp
, size_t sz
, uint64_t v
)
1866 mustfree
= render_string(&cp
, &sz
);
1872 name_mask
&= ~NAME_FIRST
;
1874 say(mpage
->mlinks
->file
,
1875 "Adding name %*s, bits=0x%llx", (int)sz
, cp
,
1876 (unsigned long long)v
);
1880 for (i
= 0; i
< KEY_MAX
; i
++)
1881 if ((uint64_t)1 << i
& v
)
1882 say(mpage
->mlinks
->file
,
1883 "Adding key %s=%*s",
1884 mansearch_keynames
[i
], (int)sz
, cp
);
1888 slot
= ohash_qlookupi(htab
, cp
, &end
);
1889 s
= ohash_find(htab
, slot
);
1891 if (NULL
!= s
&& mpage
== s
->mpage
) {
1894 } else if (NULL
== s
) {
1895 s
= mandoc_calloc(1, sizeof(struct str
) + sz
+ 1);
1896 memcpy(s
->key
, cp
, sz
);
1897 ohash_insert(htab
, slot
, s
);
1907 * Take a Unicode codepoint and produce its UTF-8 encoding.
1908 * This isn't the best way to do this, but it works.
1909 * The magic numbers are from the UTF-8 packaging.
1910 * They're not as scary as they seem: read the UTF-8 spec for details.
1913 utf8(unsigned int cp
, char out
[7])
1918 if (cp
<= 0x0000007F) {
1921 } else if (cp
<= 0x000007FF) {
1923 out
[0] = (cp
>> 6 & 31) | 192;
1924 out
[1] = (cp
& 63) | 128;
1925 } else if (cp
<= 0x0000FFFF) {
1927 out
[0] = (cp
>> 12 & 15) | 224;
1928 out
[1] = (cp
>> 6 & 63) | 128;
1929 out
[2] = (cp
& 63) | 128;
1930 } else if (cp
<= 0x001FFFFF) {
1932 out
[0] = (cp
>> 18 & 7) | 240;
1933 out
[1] = (cp
>> 12 & 63) | 128;
1934 out
[2] = (cp
>> 6 & 63) | 128;
1935 out
[3] = (cp
& 63) | 128;
1936 } else if (cp
<= 0x03FFFFFF) {
1938 out
[0] = (cp
>> 24 & 3) | 248;
1939 out
[1] = (cp
>> 18 & 63) | 128;
1940 out
[2] = (cp
>> 12 & 63) | 128;
1941 out
[3] = (cp
>> 6 & 63) | 128;
1942 out
[4] = (cp
& 63) | 128;
1943 } else if (cp
<= 0x7FFFFFFF) {
1945 out
[0] = (cp
>> 30 & 1) | 252;
1946 out
[1] = (cp
>> 24 & 63) | 128;
1947 out
[2] = (cp
>> 18 & 63) | 128;
1948 out
[3] = (cp
>> 12 & 63) | 128;
1949 out
[4] = (cp
>> 6 & 63) | 128;
1950 out
[5] = (cp
& 63) | 128;
1959 * If the string contains escape sequences,
1960 * replace it with an allocated rendering and return 1,
1961 * such that the caller can free it after use.
1962 * Otherwise, do nothing and return 0.
1965 render_string(char **public, size_t *psz
)
1967 const char *src
, *scp
, *addcp
, *seq
;
1969 size_t ssz
, dsz
, addsz
;
1970 char utfbuf
[7], res
[6];
1971 int seqlen
, unicode
;
1975 res
[2] = ASCII_NBRSP
;
1976 res
[3] = ASCII_HYPH
;
1977 res
[4] = ASCII_BREAK
;
1980 src
= scp
= *public;
1985 while (scp
< src
+ *psz
) {
1987 /* Leave normal characters unchanged. */
1989 if (strchr(res
, *scp
) == NULL
) {
1997 * Found something that requires replacing,
1998 * make sure we have a destination buffer.
2002 dst
= mandoc_malloc(ssz
+ 1);
2004 memcpy(dst
, src
, dsz
);
2007 /* Handle single-char special characters. */
2028 * Found an escape sequence.
2029 * Read past the slash, then parse it.
2030 * Ignore everything except characters.
2034 if (mandoc_escape(&scp
, &seq
, &seqlen
) != ESCAPE_SPECIAL
)
2038 * Render the special character
2039 * as either UTF-8 or ASCII.
2043 unicode
= mchars_spec2cp(seq
, seqlen
);
2046 addsz
= utf8(unicode
, utfbuf
);
2051 addcp
= mchars_spec2str(seq
, seqlen
, &addsz
);
2054 if (*addcp
== ASCII_NBRSP
) {
2060 /* Copy the rendered glyph into the stream. */
2063 dst
= mandoc_realloc(dst
, ssz
+ 1);
2064 memcpy(dst
+ dsz
, addcp
, addsz
);
2072 /* Trim trailing whitespace and NUL-terminate. */
2074 while (*psz
> 0 && (*public)[*psz
- 1] == ' ')
2077 (*public)[*psz
] = '\0';
2084 dbadd_mlink(const struct mlink
*mlink
)
2086 dba_page_alias(mlink
->mpage
->dba
, mlink
->name
, NAME_FILE
);
2087 dba_page_add(mlink
->mpage
->dba
, DBP_SECT
, mlink
->dsec
);
2088 dba_page_add(mlink
->mpage
->dba
, DBP_SECT
, mlink
->fsec
);
2089 dba_page_add(mlink
->mpage
->dba
, DBP_ARCH
, mlink
->arch
);
2090 dba_page_add(mlink
->mpage
->dba
, DBP_FILE
, mlink
->file
);
2094 * Flush the current page's terms (and their bits) into the database.
2095 * Also, handle escape sequences at the last possible moment.
2098 dbadd(struct dba
*dba
, struct mpage
*mpage
)
2100 struct mlink
*mlink
;
2108 mlink
= mpage
->mlinks
;
2111 for (key
= ohash_first(&names
, &slot
); NULL
!= key
;
2112 key
= ohash_next(&names
, &slot
))
2114 for (key
= ohash_first(&strings
, &slot
); NULL
!= key
;
2115 key
= ohash_next(&strings
, &slot
))
2119 while (NULL
!= mlink
) {
2120 fputs(mlink
->name
, stdout
);
2121 if (NULL
== mlink
->next
||
2122 strcmp(mlink
->dsec
, mlink
->next
->dsec
) ||
2123 strcmp(mlink
->fsec
, mlink
->next
->fsec
) ||
2124 strcmp(mlink
->arch
, mlink
->next
->arch
)) {
2126 if ('\0' == *mlink
->dsec
)
2127 fputs(mlink
->fsec
, stdout
);
2129 fputs(mlink
->dsec
, stdout
);
2130 if ('\0' != *mlink
->arch
)
2131 printf("/%s", mlink
->arch
);
2134 mlink
= mlink
->next
;
2136 fputs(", ", stdout
);
2138 printf(" - %s\n", mpage
->desc
);
2143 say(mlink
->file
, "Adding to database");
2147 mustfree
= render_string(&cp
, &i
);
2148 mpage
->dba
= dba_page_new(dba
->pages
,
2149 *mpage
->arch
== '\0' ? mlink
->arch
: mpage
->arch
,
2150 cp
, mlink
->file
, mpage
->form
);
2153 dba_page_add(mpage
->dba
, DBP_SECT
, mpage
->sec
);
2155 while (mlink
!= NULL
) {
2157 mlink
= mlink
->next
;
2160 for (key
= ohash_first(&names
, &slot
); NULL
!= key
;
2161 key
= ohash_next(&names
, &slot
)) {
2162 assert(key
->mpage
== mpage
);
2163 dba_page_alias(mpage
->dba
, key
->key
, key
->mask
);
2166 for (key
= ohash_first(&strings
, &slot
); NULL
!= key
;
2167 key
= ohash_next(&strings
, &slot
)) {
2168 assert(key
->mpage
== mpage
);
2170 for (mask
= TYPE_Xr
; mask
<= TYPE_Lb
; mask
*= 2) {
2171 if (key
->mask
& mask
)
2172 dba_macro_add(dba
->macros
, i
,
2173 key
->key
, mpage
->dba
);
2181 dbprune(struct dba
*dba
)
2183 struct dba_array
*page
, *files
;
2186 dba_array_FOREACH(dba
->pages
, page
) {
2187 files
= dba_array_get(page
, DBP_FILE
);
2188 dba_array_FOREACH(files
, file
) {
2191 if (ohash_find(&mlinks
, ohash_qlookup(&mlinks
,
2194 say(file
, "Deleting from database");
2195 dba_array_del(dba
->pages
);
2203 * Write the database from memory to disk.
2206 dbwrite(struct dba
*dba
)
2208 struct stat sb1
, sb2
;
2209 char tfn
[33], *cp1
, *cp2
;
2214 * Do not write empty databases, and delete existing ones
2215 * when makewhatis -u causes them to become empty.
2218 dba_array_start(dba
->pages
);
2219 if (dba_array_next(dba
->pages
) == NULL
) {
2220 if (unlink(MANDOC_DB
) == -1 && errno
!= ENOENT
)
2221 say(MANDOC_DB
, "&unlink");
2226 * Build the database in a temporary file,
2227 * then atomically move it into place.
2230 if (dba_write(MANDOC_DB
"~", dba
) != -1) {
2231 if (rename(MANDOC_DB
"~", MANDOC_DB
) == -1) {
2232 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2233 say(MANDOC_DB
, "&rename");
2234 unlink(MANDOC_DB
"~");
2240 * We lack write permission and cannot replace the database
2241 * file, but let's at least check whether the data changed.
2244 (void)strlcpy(tfn
, "/tmp/mandocdb.XXXXXXXX", sizeof(tfn
));
2245 if (mkdtemp(tfn
) == NULL
) {
2246 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2247 say("", "&%s", tfn
);
2250 cp1
= cp2
= MAP_FAILED
;
2252 (void)strlcat(tfn
, "/" MANDOC_DB
, sizeof(tfn
));
2253 if (dba_write(tfn
, dba
) == -1) {
2254 say(tfn
, "&dba_write");
2257 if ((fd1
= open(MANDOC_DB
, O_RDONLY
)) == -1) {
2258 say(MANDOC_DB
, "&open");
2261 if ((fd2
= open(tfn
, O_RDONLY
)) == -1) {
2265 if (fstat(fd1
, &sb1
) == -1) {
2266 say(MANDOC_DB
, "&fstat");
2269 if (fstat(fd2
, &sb2
) == -1) {
2273 if (sb1
.st_size
!= sb2
.st_size
)
2275 if ((cp1
= mmap(NULL
, sb1
.st_size
, PROT_READ
, MAP_PRIVATE
,
2276 fd1
, 0)) == MAP_FAILED
) {
2277 say(MANDOC_DB
, "&mmap");
2280 if ((cp2
= mmap(NULL
, sb2
.st_size
, PROT_READ
, MAP_PRIVATE
,
2281 fd2
, 0)) == MAP_FAILED
) {
2285 for (i
= 0; i
< sb1
.st_size
; i
++)
2286 if (cp1
[i
] != cp2
[i
])
2291 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2292 say(MANDOC_DB
, "Data changed, but cannot replace database");
2295 if (cp1
!= MAP_FAILED
)
2296 munmap(cp1
, sb1
.st_size
);
2297 if (cp2
!= MAP_FAILED
)
2298 munmap(cp2
, sb2
.st_size
);
2304 *strrchr(tfn
, '/') = '\0';
2309 set_basedir(const char *targetdir
, int report_baddir
)
2311 static char startdir
[PATH_MAX
];
2312 static int getcwd_status
; /* 1 = ok, 2 = failure */
2313 static int chdir_status
; /* 1 = changed directory */
2316 * Remember the original working directory, if possible.
2317 * This will be needed if the second or a later directory
2318 * on the command line is given as a relative path.
2319 * Do not error out if the current directory is not
2320 * searchable: Maybe it won't be needed after all.
2322 if (getcwd_status
== 0) {
2323 if (getcwd(startdir
, sizeof(startdir
)) == NULL
) {
2325 (void)strlcpy(startdir
, strerror(errno
),
2332 * We are leaving the old base directory.
2333 * Do not use it any longer, not even for messages.
2339 * If and only if the directory was changed earlier and
2340 * the next directory to process is given as a relative path,
2341 * first go back, or bail out if that is impossible.
2343 if (chdir_status
&& *targetdir
!= '/') {
2344 if (getcwd_status
== 2) {
2345 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2346 say("", "getcwd: %s", startdir
);
2349 if (chdir(startdir
) == -1) {
2350 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2351 say("", "&chdir %s", startdir
);
2357 * Always resolve basedir to the canonicalized absolute
2358 * pathname and append a trailing slash, such that
2359 * we can reliably check whether files are inside.
2361 if (realpath(targetdir
, basedir
) == NULL
) {
2362 if (report_baddir
|| errno
!= ENOENT
) {
2363 exitcode
= (int)MANDOCLEVEL_BADARG
;
2364 say("", "&%s: realpath", targetdir
);
2368 } else if (chdir(basedir
) == -1) {
2369 if (report_baddir
|| errno
!= ENOENT
) {
2370 exitcode
= (int)MANDOCLEVEL_BADARG
;
2377 basedir_len
= strlen(basedir
);
2378 if (basedir
[basedir_len
- 1] != '/') {
2379 if (basedir_len
>= PATH_MAX
- 1) {
2380 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2381 say("", "Filename too long");
2386 basedir
[basedir_len
++] = '/';
2387 basedir
[basedir_len
] = '\0';
2392 #ifdef READ_ALLOWED_PATH
2394 read_allowed(const char *candidate
)
2399 for (cp
= READ_ALLOWED_PATH
;; cp
+= len
) {
2404 len
= strcspn(cp
, ":");
2405 if (strncmp(candidate
, cp
, len
) == 0)
2412 say(const char *file
, const char *format
, ...)
2417 if (*basedir
!= '\0')
2418 fprintf(stderr
, "%s", basedir
);
2419 if (*basedir
!= '\0' && *file
!= '\0')
2422 fprintf(stderr
, "%s", file
);
2425 if (format
!= NULL
) {
2438 if (format
!= NULL
) {
2439 if (*basedir
!= '\0' || *file
!= '\0')
2440 fputs(": ", stderr
);
2441 va_start(ap
, format
);
2442 vfprintf(stderr
, format
, ap
);
2446 if (*basedir
!= '\0' || *file
!= '\0' || format
!= NULL
)
2447 fputs(": ", stderr
);
2450 fputc('\n', stderr
);