1 /* $Id: mandocdb.c,v 1.266 2020/01/26 21:25:41 schwarze Exp $ */
3 * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011-2020 Ingo Schwarze <schwarze@openbsd.org>
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.
21 #include <sys/types.h>
35 #include "compat_fts.h"
49 #include "mandoc_aux.h"
50 #include "mandoc_ohash.h"
55 #include "mandoc_parse.h"
57 #include "mansearch.h"
58 #include "dba_array.h"
61 extern const char *const mansearch_keynames
[];
64 OP_DEFAULT
= 0, /* new dbs from dir list or default config */
65 OP_CONFFILE
, /* new databases from custom config file */
66 OP_UPDATE
, /* delete/add entries in existing database */
67 OP_DELETE
, /* delete entries from existing database */
68 OP_TEST
/* change no databases, report potential problems */
72 const struct mpage
*mpage
; /* if set, the owning parse */
73 uint64_t mask
; /* bitmask in sequence */
74 char key
[]; /* rendered text */
83 struct inodev inodev
; /* used for hashing routine */
84 struct dba_array
*dba
;
85 char *sec
; /* section from file content */
86 char *arch
; /* architecture from file content */
87 char *title
; /* title from file content */
88 char *desc
; /* description from file content */
89 struct mpage
*next
; /* singly linked list */
90 struct mlink
*mlinks
; /* singly linked list */
92 enum form form
; /* format from file content */
96 char file
[PATH_MAX
]; /* filename rel. to manpath */
97 char *dsec
; /* section from directory */
98 char *arch
; /* architecture from directory */
99 char *name
; /* name from file name (not empty) */
100 char *fsec
; /* section from file name suffix */
101 struct mlink
*next
; /* singly linked list */
102 struct mpage
*mpage
; /* parent */
103 int gzip
; /* filename has a .gz suffix */
104 enum form dform
; /* format from directory */
105 enum form fform
; /* format from file name suffix */
108 typedef int (*mdoc_fp
)(struct mpage
*, const struct roff_meta
*,
109 const struct roff_node
*);
111 struct mdoc_handler
{
112 mdoc_fp fp
; /* optional handler */
113 uint64_t mask
; /* set unless handler returns 0 */
114 int taboo
; /* node flags that must not be set */
118 int mandocdb(int, char *[]);
120 static void dbadd(struct dba
*, struct mpage
*);
121 static void dbadd_mlink(const struct mlink
*mlink
);
122 static void dbprune(struct dba
*);
123 static void dbwrite(struct dba
*);
124 static void filescan(const char *);
125 #if HAVE_FTS_COMPARE_CONST
126 static int fts_compare(const FTSENT
*const *, const FTSENT
*const *);
128 static int fts_compare(const FTSENT
**, const FTSENT
**);
130 static void mlink_add(struct mlink
*, const struct stat
*);
131 static void mlink_check(struct mpage
*, struct mlink
*);
132 static void mlink_free(struct mlink
*);
133 static void mlinks_undupe(struct mpage
*);
134 static void mpages_free(void);
135 static void mpages_merge(struct dba
*, struct mparse
*);
136 static void parse_cat(struct mpage
*, int);
137 static void parse_man(struct mpage
*, const struct roff_meta
*,
138 const struct roff_node
*);
139 static void parse_mdoc(struct mpage
*, const struct roff_meta
*,
140 const struct roff_node
*);
141 static int parse_mdoc_head(struct mpage
*, const struct roff_meta
*,
142 const struct roff_node
*);
143 static int parse_mdoc_Fa(struct mpage
*, const struct roff_meta
*,
144 const struct roff_node
*);
145 static int parse_mdoc_Fd(struct mpage
*, const struct roff_meta
*,
146 const struct roff_node
*);
147 static void parse_mdoc_fname(struct mpage
*, const struct roff_node
*);
148 static int parse_mdoc_Fn(struct mpage
*, const struct roff_meta
*,
149 const struct roff_node
*);
150 static int parse_mdoc_Fo(struct mpage
*, const struct roff_meta
*,
151 const struct roff_node
*);
152 static int parse_mdoc_Nd(struct mpage
*, const struct roff_meta
*,
153 const struct roff_node
*);
154 static int parse_mdoc_Nm(struct mpage
*, const struct roff_meta
*,
155 const struct roff_node
*);
156 static int parse_mdoc_Sh(struct mpage
*, const struct roff_meta
*,
157 const struct roff_node
*);
158 static int parse_mdoc_Va(struct mpage
*, const struct roff_meta
*,
159 const struct roff_node
*);
160 static int parse_mdoc_Xr(struct mpage
*, const struct roff_meta
*,
161 const struct roff_node
*);
162 static void putkey(const struct mpage
*, char *, uint64_t);
163 static void putkeys(const struct mpage
*, char *, size_t, uint64_t);
164 static void putmdockey(const struct mpage
*,
165 const struct roff_node
*, uint64_t, int);
166 static int render_string(char **, size_t *);
167 static void say(const char *, const char *, ...)
168 __attribute__((__format__ (__printf__
, 2, 3)));
169 static int set_basedir(const char *, int);
170 static int treescan(void);
171 static size_t utf8(unsigned int, char [7]);
173 static int nodb
; /* no database changes */
174 static int mparse_options
; /* abort the parse early */
175 static int use_all
; /* use all found files */
176 static int debug
; /* print what we're doing */
177 static int warnings
; /* warn about crap */
178 static int write_utf8
; /* write UTF-8 output; else ASCII */
179 static int exitcode
; /* to be returned by main */
180 static enum op op
; /* operational mode */
181 static char basedir
[PATH_MAX
]; /* current base directory */
182 static size_t basedir_len
; /* strlen(basedir) */
183 static struct mpage
*mpage_head
; /* list of distinct manual pages */
184 static struct ohash mpages
; /* table of distinct manual pages */
185 static struct ohash mlinks
; /* table of directory entries */
186 static struct ohash names
; /* table of all names */
187 static struct ohash strings
; /* table of all strings */
188 static uint64_t name_mask
;
190 static const struct mdoc_handler mdoc_handlers
[MDOC_MAX
- MDOC_Dd
] = {
191 { NULL
, 0, NODE_NOPRT
}, /* Dd */
192 { NULL
, 0, NODE_NOPRT
}, /* Dt */
193 { NULL
, 0, NODE_NOPRT
}, /* Os */
194 { parse_mdoc_Sh
, TYPE_Sh
, 0 }, /* Sh */
195 { parse_mdoc_head
, TYPE_Ss
, 0 }, /* Ss */
196 { NULL
, 0, 0 }, /* Pp */
197 { NULL
, 0, 0 }, /* D1 */
198 { NULL
, 0, 0 }, /* Dl */
199 { NULL
, 0, 0 }, /* Bd */
200 { NULL
, 0, 0 }, /* Ed */
201 { NULL
, 0, 0 }, /* Bl */
202 { NULL
, 0, 0 }, /* El */
203 { NULL
, 0, 0 }, /* It */
204 { NULL
, 0, 0 }, /* Ad */
205 { NULL
, TYPE_An
, 0 }, /* An */
206 { NULL
, 0, 0 }, /* Ap */
207 { NULL
, TYPE_Ar
, 0 }, /* Ar */
208 { NULL
, TYPE_Cd
, 0 }, /* Cd */
209 { NULL
, TYPE_Cm
, 0 }, /* Cm */
210 { NULL
, TYPE_Dv
, 0 }, /* Dv */
211 { NULL
, TYPE_Er
, 0 }, /* Er */
212 { NULL
, TYPE_Ev
, 0 }, /* Ev */
213 { NULL
, 0, 0 }, /* Ex */
214 { parse_mdoc_Fa
, 0, 0 }, /* Fa */
215 { parse_mdoc_Fd
, 0, 0 }, /* Fd */
216 { NULL
, TYPE_Fl
, 0 }, /* Fl */
217 { parse_mdoc_Fn
, 0, 0 }, /* Fn */
218 { NULL
, TYPE_Ft
| TYPE_Vt
, 0 }, /* Ft */
219 { NULL
, TYPE_Ic
, 0 }, /* Ic */
220 { NULL
, TYPE_In
, 0 }, /* In */
221 { NULL
, TYPE_Li
, 0 }, /* Li */
222 { parse_mdoc_Nd
, 0, 0 }, /* Nd */
223 { parse_mdoc_Nm
, 0, 0 }, /* Nm */
224 { NULL
, 0, 0 }, /* Op */
225 { NULL
, 0, 0 }, /* Ot */
226 { NULL
, TYPE_Pa
, NODE_NOSRC
}, /* Pa */
227 { NULL
, 0, 0 }, /* Rv */
228 { NULL
, TYPE_St
, 0 }, /* St */
229 { parse_mdoc_Va
, TYPE_Va
, 0 }, /* Va */
230 { parse_mdoc_Va
, TYPE_Vt
, 0 }, /* Vt */
231 { parse_mdoc_Xr
, 0, 0 }, /* Xr */
232 { NULL
, 0, 0 }, /* %A */
233 { NULL
, 0, 0 }, /* %B */
234 { NULL
, 0, 0 }, /* %D */
235 { NULL
, 0, 0 }, /* %I */
236 { NULL
, 0, 0 }, /* %J */
237 { NULL
, 0, 0 }, /* %N */
238 { NULL
, 0, 0 }, /* %O */
239 { NULL
, 0, 0 }, /* %P */
240 { NULL
, 0, 0 }, /* %R */
241 { NULL
, 0, 0 }, /* %T */
242 { NULL
, 0, 0 }, /* %V */
243 { NULL
, 0, 0 }, /* Ac */
244 { NULL
, 0, 0 }, /* Ao */
245 { NULL
, 0, 0 }, /* Aq */
246 { NULL
, TYPE_At
, 0 }, /* At */
247 { NULL
, 0, 0 }, /* Bc */
248 { NULL
, 0, 0 }, /* Bf */
249 { NULL
, 0, 0 }, /* Bo */
250 { NULL
, 0, 0 }, /* Bq */
251 { NULL
, TYPE_Bsx
, NODE_NOSRC
}, /* Bsx */
252 { NULL
, TYPE_Bx
, NODE_NOSRC
}, /* Bx */
253 { NULL
, 0, 0 }, /* Db */
254 { NULL
, 0, 0 }, /* Dc */
255 { NULL
, 0, 0 }, /* Do */
256 { NULL
, 0, 0 }, /* Dq */
257 { NULL
, 0, 0 }, /* Ec */
258 { NULL
, 0, 0 }, /* Ef */
259 { NULL
, TYPE_Em
, 0 }, /* Em */
260 { NULL
, 0, 0 }, /* Eo */
261 { NULL
, TYPE_Fx
, NODE_NOSRC
}, /* Fx */
262 { NULL
, TYPE_Ms
, 0 }, /* Ms */
263 { NULL
, 0, 0 }, /* No */
264 { NULL
, 0, 0 }, /* Ns */
265 { NULL
, TYPE_Nx
, NODE_NOSRC
}, /* Nx */
266 { NULL
, TYPE_Ox
, NODE_NOSRC
}, /* Ox */
267 { NULL
, 0, 0 }, /* Pc */
268 { NULL
, 0, 0 }, /* Pf */
269 { NULL
, 0, 0 }, /* Po */
270 { NULL
, 0, 0 }, /* Pq */
271 { NULL
, 0, 0 }, /* Qc */
272 { NULL
, 0, 0 }, /* Ql */
273 { NULL
, 0, 0 }, /* Qo */
274 { NULL
, 0, 0 }, /* Qq */
275 { NULL
, 0, 0 }, /* Re */
276 { NULL
, 0, 0 }, /* Rs */
277 { NULL
, 0, 0 }, /* Sc */
278 { NULL
, 0, 0 }, /* So */
279 { NULL
, 0, 0 }, /* Sq */
280 { NULL
, 0, 0 }, /* Sm */
281 { NULL
, 0, 0 }, /* Sx */
282 { NULL
, TYPE_Sy
, 0 }, /* Sy */
283 { NULL
, TYPE_Tn
, 0 }, /* Tn */
284 { NULL
, 0, NODE_NOSRC
}, /* Ux */
285 { NULL
, 0, 0 }, /* Xc */
286 { NULL
, 0, 0 }, /* Xo */
287 { parse_mdoc_Fo
, 0, 0 }, /* Fo */
288 { NULL
, 0, 0 }, /* Fc */
289 { NULL
, 0, 0 }, /* Oo */
290 { NULL
, 0, 0 }, /* Oc */
291 { NULL
, 0, 0 }, /* Bk */
292 { NULL
, 0, 0 }, /* Ek */
293 { NULL
, 0, 0 }, /* Bt */
294 { NULL
, 0, 0 }, /* Hf */
295 { NULL
, 0, 0 }, /* Fr */
296 { NULL
, 0, 0 }, /* Ud */
297 { NULL
, TYPE_Lb
, NODE_NOSRC
}, /* Lb */
298 { NULL
, 0, 0 }, /* Lp */
299 { NULL
, TYPE_Lk
, 0 }, /* Lk */
300 { NULL
, TYPE_Mt
, NODE_NOSRC
}, /* Mt */
301 { NULL
, 0, 0 }, /* Brq */
302 { NULL
, 0, 0 }, /* Bro */
303 { NULL
, 0, 0 }, /* Brc */
304 { NULL
, 0, 0 }, /* %C */
305 { NULL
, 0, 0 }, /* Es */
306 { NULL
, 0, 0 }, /* En */
307 { NULL
, TYPE_Dx
, NODE_NOSRC
}, /* Dx */
308 { NULL
, 0, 0 }, /* %Q */
309 { NULL
, 0, 0 }, /* %U */
310 { NULL
, 0, 0 }, /* Ta */
315 mandocdb(int argc
, char *argv
[])
320 const char *path_arg
, *progname
;
325 if (pledge("stdio rpath wpath cpath", NULL
) == -1) {
327 return (int)MANDOCLEVEL_SYSERR
;
331 #if HAVE_SANDBOX_INIT
332 if (sandbox_init(kSBXProfileNoInternet
, SANDBOX_NAMED
, NULL
) == -1) {
333 warnx("sandbox_init");
334 return (int)MANDOCLEVEL_SYSERR
;
338 memset(&conf
, 0, sizeof(conf
));
341 * We accept a few different invocations.
342 * The CHECKOP macro makes sure that invocation styles don't
343 * clobber each other.
345 #define CHECKOP(_op, _ch) do \
346 if ((_op) != OP_DEFAULT) { \
347 warnx("-%c: Conflicting option", (_ch)); \
349 } while (/*CONSTCOND*/0)
351 mparse_options
= MPARSE_VALIDATE
;
355 while ((ch
= getopt(argc
, argv
, "aC:Dd:npQT:tu:v")) != -1)
380 mparse_options
|= MPARSE_QUICK
;
383 if (strcmp(optarg
, "utf8") != 0) {
384 warnx("-T%s: Unsupported output format",
392 dup2(STDOUT_FILENO
, STDERR_FILENO
);
402 /* Compatibility with espie@'s makewhatis. */
413 if (pledge("stdio rpath", NULL
) == -1) {
415 return (int)MANDOCLEVEL_SYSERR
;
420 if (op
== OP_CONFFILE
&& argc
> 0) {
421 warnx("-C: Too many arguments");
425 exitcode
= (int)MANDOCLEVEL_OK
;
427 mp
= mparse_alloc(mparse_options
, MANDOC_OS_OTHER
, NULL
);
428 mandoc_ohash_init(&mpages
, 6, offsetof(struct mpage
, inodev
));
429 mandoc_ohash_init(&mlinks
, 6, offsetof(struct mlink
, file
));
431 if (op
== OP_UPDATE
|| op
== OP_DELETE
|| op
== OP_TEST
) {
434 * Most of these deal with a specific directory.
435 * Jump into that directory first.
437 if (op
!= OP_TEST
&& set_basedir(path_arg
, 1) == 0)
440 dba
= nodb
? dba_new(128) : dba_read(MANDOC_DB
);
443 * The existing database is usable. Process
444 * all files specified on the command-line.
447 for (i
= 0; i
< argc
; i
++)
452 /* Database missing or corrupt. */
453 if (op
!= OP_UPDATE
|| errno
!= ENOENT
)
454 say(MANDOC_DB
, "%s: Automatically recreating"
455 " from scratch", strerror(errno
));
456 exitcode
= (int)MANDOCLEVEL_OK
;
463 mpages_merge(dba
, mp
);
469 * If we have arguments, use them as our manpaths.
470 * If we don't, use man.conf(5).
473 conf
.manpath
.paths
= mandoc_reallocarray(NULL
,
474 argc
, sizeof(char *));
475 conf
.manpath
.sz
= (size_t)argc
;
476 for (i
= 0; i
< argc
; i
++)
477 conf
.manpath
.paths
[i
] = mandoc_strdup(argv
[i
]);
479 manconf_parse(&conf
, path_arg
, NULL
, NULL
);
481 if (conf
.manpath
.sz
== 0) {
482 exitcode
= (int)MANDOCLEVEL_BADARG
;
483 say("", "Empty manpath");
487 * First scan the tree rooted at a base directory, then
488 * build a new database and finally move it into place.
489 * Ignore zero-length directories and strip trailing
492 for (j
= 0; j
< conf
.manpath
.sz
; j
++) {
493 sz
= strlen(conf
.manpath
.paths
[j
]);
494 if (sz
&& conf
.manpath
.paths
[j
][sz
- 1] == '/')
495 conf
.manpath
.paths
[j
][--sz
] = '\0';
500 mandoc_ohash_init(&mpages
, 6,
501 offsetof(struct mpage
, inodev
));
502 mandoc_ohash_init(&mlinks
, 6,
503 offsetof(struct mlink
, file
));
506 if (set_basedir(conf
.manpath
.paths
[j
], argc
> 0) == 0)
511 mpages_merge(dba
, mp
);
516 if (j
+ 1 < conf
.manpath
.sz
) {
518 ohash_delete(&mpages
);
519 ohash_delete(&mlinks
);
528 ohash_delete(&mpages
);
529 ohash_delete(&mlinks
);
532 progname
= getprogname();
533 fprintf(stderr
, "usage: %s [-aDnpQ] [-C file] [-Tutf8]\n"
534 " %s [-aDnpQ] [-Tutf8] dir ...\n"
535 " %s [-DnpQ] [-Tutf8] -d dir [file ...]\n"
536 " %s [-Dnp] -u dir [file ...]\n"
537 " %s [-Q] -t file ...\n",
538 progname
, progname
, progname
, progname
, progname
);
540 return (int)MANDOCLEVEL_BADARG
;
544 * To get a singly linked list in alpha order while inserting entries
545 * at the beginning, process directory entries in reverse alpha order.
548 #if HAVE_FTS_COMPARE_CONST
549 fts_compare(const FTSENT
*const *a
, const FTSENT
*const *b
)
551 fts_compare(const FTSENT
**a
, const FTSENT
**b
)
554 return -strcmp((*a
)->fts_name
, (*b
)->fts_name
);
558 * Scan a directory tree rooted at "basedir" for manpages.
559 * We use fts(), scanning directory parts along the way for clues to our
560 * section and architecture.
562 * If use_all has been specified, grok all files.
563 * If not, sanitise paths to the following:
565 * [./]man*[/<arch>]/<name>.<section>
567 * [./]cat<section>[/<arch>]/<name>.0
569 * TODO: accommodate for multi-language directories.
580 char *dsec
, *arch
, *fsec
, *cp
;
587 f
= fts_open((char * const *)argv
, FTS_PHYSICAL
| FTS_NOCHDIR
,
590 exitcode
= (int)MANDOCLEVEL_SYSERR
;
591 say("", "&fts_open");
598 while ((ff
= fts_read(f
)) != NULL
) {
599 path
= ff
->fts_path
+ 2;
600 switch (ff
->fts_info
) {
603 * Symbolic links require various sanity checks,
604 * then get handled just like regular files.
607 if (realpath(path
, buf
) == NULL
) {
609 say(path
, "&realpath");
612 if (strncmp(buf
, basedir
, basedir_len
) != 0
614 && strncmp(buf
, HOMEBREWDIR
, strlen(HOMEBREWDIR
))
617 if (warnings
) say("",
618 "%s: outside base directory", buf
);
621 /* Use logical inode to avoid mpages dupe. */
622 if (stat(path
, ff
->fts_statp
) == -1) {
630 * If we're a regular file, add an mlink by using the
631 * stored directory data and handling the filename.
634 if ( ! strcmp(path
, MANDOC_DB
))
636 if ( ! use_all
&& ff
->fts_level
< 2) {
638 say(path
, "Extraneous file");
643 while (fsec
== NULL
) {
644 fsec
= strrchr(ff
->fts_name
, '.');
645 if (fsec
== NULL
|| strcmp(fsec
+1, "gz"))
655 "No filename suffix");
658 } else if ( ! strcmp(++fsec
, "html")) {
660 say(path
, "Skip html");
662 } else if ( ! strcmp(fsec
, "ps")) {
664 say(path
, "Skip ps");
666 } else if ( ! strcmp(fsec
, "pdf")) {
668 say(path
, "Skip pdf");
670 } else if ( ! use_all
&&
671 ((dform
== FORM_SRC
&&
672 strncmp(fsec
, dsec
, strlen(dsec
))) ||
673 (dform
== FORM_CAT
&& strcmp(fsec
, "0")))) {
675 say(path
, "Wrong filename suffix");
680 mlink
= mandoc_calloc(1, sizeof(struct mlink
));
681 if (strlcpy(mlink
->file
, path
,
682 sizeof(mlink
->file
)) >=
683 sizeof(mlink
->file
)) {
684 say(path
, "Filename too long");
688 mlink
->dform
= dform
;
691 mlink
->name
= ff
->fts_name
;
694 mlink_add(mlink
, ff
->fts_statp
);
703 say(path
, "Not a regular file");
707 switch (ff
->fts_level
) {
709 /* Ignore the root directory. */
713 * This might contain manX/ or catX/.
714 * Try to infer this from the name.
715 * If we're not in use_all, enforce it.
718 if (ff
->fts_info
== FTS_DP
) {
724 if ( ! strncmp(cp
, "man", 3)) {
727 } else if ( ! strncmp(cp
, "cat", 3)) {
735 if (dsec
!= NULL
|| use_all
)
739 say(path
, "Unknown directory part");
740 fts_set(f
, ff
, FTS_SKIP
);
744 * Possibly our architecture.
745 * If we're descending, keep tabs on it.
747 if (ff
->fts_info
!= FTS_DP
&& dsec
!= NULL
)
753 if (ff
->fts_info
== FTS_DP
|| use_all
)
756 say(path
, "Extraneous directory part");
757 fts_set(f
, ff
, FTS_SKIP
);
767 * Add a file to the mlinks table.
768 * Do not verify that it's a "valid" looking manpage (we'll do that
771 * Try to infer the manual section, architecture, and page name from the
772 * path, assuming it looks like
774 * [./]man*[/<arch>]/<name>.<section>
776 * [./]cat<section>[/<arch>]/<name>.0
778 * See treescan() for the fts(3) version of this.
781 filescan(const char *infile
)
785 char *linkfile
, *p
, *realdir
, *start
, *usefile
;
790 if (strncmp(infile
, "./", 2) == 0)
794 * We have to do lstat(2) before realpath(3) loses
795 * the information whether this is a symbolic link.
796 * We need to know that because for symbolic links,
797 * we want to use the orginal file name, while for
798 * regular files, we want to use the real path.
800 if (lstat(infile
, &st
) == -1) {
801 exitcode
= (int)MANDOCLEVEL_BADARG
;
802 say(infile
, "&lstat");
804 } else if (S_ISREG(st
.st_mode
) == 0 && S_ISLNK(st
.st_mode
) == 0) {
805 exitcode
= (int)MANDOCLEVEL_BADARG
;
806 say(infile
, "Not a regular file");
811 * We have to resolve the file name to the real path
812 * in any case for the base directory check.
814 if ((usefile
= realpath(infile
, NULL
)) == NULL
) {
815 exitcode
= (int)MANDOCLEVEL_BADARG
;
816 say(infile
, "&realpath");
822 else if (strncmp(usefile
, basedir
, basedir_len
) == 0)
823 start
= usefile
+ basedir_len
;
825 else if (strncmp(usefile
, HOMEBREWDIR
, strlen(HOMEBREWDIR
)) == 0)
829 exitcode
= (int)MANDOCLEVEL_BADARG
;
830 say("", "%s: outside base directory", infile
);
836 * Now we are sure the file is inside our tree.
837 * If it is a symbolic link, ignore the real path
838 * and use the original name.
841 if (S_ISLNK(st
.st_mode
) == 0)
845 * Some implementations of realpath(3) may succeed
846 * even if the target of the link does not exist,
847 * so check again for extra safety.
849 if (stat(usefile
, &st
) == -1) {
850 exitcode
= (int)MANDOCLEVEL_BADARG
;
851 say(infile
, "&stat");
855 linkfile
= mandoc_strdup(infile
);
858 start
= usefile
= linkfile
;
861 if (strncmp(infile
, basedir
, basedir_len
) == 0) {
864 start
= usefile
+ basedir_len
;
869 * This symbolic link points into the basedir
870 * from the outside. Let's see whether any of
871 * the parent directories resolve to the basedir.
873 p
= strchr(linkfile
, '\0');
878 if ((realdir
= realpath(linkfile
, NULL
)) == NULL
) {
879 exitcode
= (int)MANDOCLEVEL_BADARG
;
880 say(infile
, "&realpath");
885 realdir_len
= strlen(realdir
) + 1;
888 } while (realdir_len
> basedir_len
);
891 * If one of the directories resolves to the basedir,
892 * use the rest of the original name.
893 * Otherwise, the best we can do
894 * is to use the filename pointed to.
896 if (realdir_len
== basedir_len
) {
902 start
= usefile
+ basedir_len
;
904 } while (/* CONSTCOND */ 0);
906 mlink
= mandoc_calloc(1, sizeof(struct mlink
));
907 mlink
->dform
= FORM_NONE
;
908 if (strlcpy(mlink
->file
, start
, sizeof(mlink
->file
)) >=
909 sizeof(mlink
->file
)) {
910 say(start
, "Filename too long");
917 * In test mode or when the original name is absolute
918 * but outside our tree, guess the base directory.
921 if (op
== OP_TEST
|| (start
== usefile
&& *start
== '/')) {
922 if (strncmp(usefile
, "man/", 4) == 0)
924 else if ((start
= strstr(usefile
, "/man/")) != NULL
)
931 * First try to guess our directory structure.
932 * If we find a separator, try to look for man* or cat*.
933 * If we find one of these and what's underneath is a directory,
934 * assume it's an architecture.
936 if ((p
= strchr(start
, '/')) != NULL
) {
938 if (strncmp(start
, "man", 3) == 0) {
939 mlink
->dform
= FORM_SRC
;
940 mlink
->dsec
= start
+ 3;
941 } else if (strncmp(start
, "cat", 3) == 0) {
942 mlink
->dform
= FORM_CAT
;
943 mlink
->dsec
= start
+ 3;
947 if (mlink
->dsec
!= NULL
&& (p
= strchr(start
, '/')) != NULL
) {
955 * Now check the file suffix.
956 * Suffix of `.0' indicates a catpage, `.1-9' is a manpage.
958 p
= strrchr(start
, '\0');
959 while (p
-- > start
&& *p
!= '/' && *p
!= '.')
968 * Now try to parse the name.
969 * Use the filename portion of the path.
972 if ((p
= strrchr(start
, '/')) != NULL
) {
976 mlink_add(mlink
, &st
);
981 mlink_add(struct mlink
*mlink
, const struct stat
*st
)
983 struct inodev inodev
;
987 assert(NULL
!= mlink
->file
);
989 mlink
->dsec
= mandoc_strdup(mlink
->dsec
? mlink
->dsec
: "");
990 mlink
->arch
= mandoc_strdup(mlink
->arch
? mlink
->arch
: "");
991 mlink
->name
= mandoc_strdup(mlink
->name
? mlink
->name
: "");
992 mlink
->fsec
= mandoc_strdup(mlink
->fsec
? mlink
->fsec
: "");
994 if ('0' == *mlink
->fsec
) {
996 mlink
->fsec
= mandoc_strdup(mlink
->dsec
);
997 mlink
->fform
= FORM_CAT
;
998 } else if ('1' <= *mlink
->fsec
&& '9' >= *mlink
->fsec
)
999 mlink
->fform
= FORM_SRC
;
1001 mlink
->fform
= FORM_NONE
;
1003 slot
= ohash_qlookup(&mlinks
, mlink
->file
);
1004 assert(NULL
== ohash_find(&mlinks
, slot
));
1005 ohash_insert(&mlinks
, slot
, mlink
);
1007 memset(&inodev
, 0, sizeof(inodev
)); /* Clear padding. */
1008 inodev
.st_ino
= st
->st_ino
;
1009 inodev
.st_dev
= st
->st_dev
;
1010 slot
= ohash_lookup_memory(&mpages
, (char *)&inodev
,
1011 sizeof(struct inodev
), inodev
.st_ino
);
1012 mpage
= ohash_find(&mpages
, slot
);
1013 if (NULL
== mpage
) {
1014 mpage
= mandoc_calloc(1, sizeof(struct mpage
));
1015 mpage
->inodev
.st_ino
= inodev
.st_ino
;
1016 mpage
->inodev
.st_dev
= inodev
.st_dev
;
1017 mpage
->form
= FORM_NONE
;
1018 mpage
->next
= mpage_head
;
1020 ohash_insert(&mpages
, slot
, mpage
);
1022 mlink
->next
= mpage
->mlinks
;
1023 mpage
->mlinks
= mlink
;
1024 mlink
->mpage
= mpage
;
1028 mlink_free(struct mlink
*mlink
)
1041 struct mpage
*mpage
;
1042 struct mlink
*mlink
;
1044 while ((mpage
= mpage_head
) != NULL
) {
1045 while ((mlink
= mpage
->mlinks
) != NULL
) {
1046 mpage
->mlinks
= mlink
->next
;
1049 mpage_head
= mpage
->next
;
1059 * For each mlink to the mpage, check whether the path looks like
1060 * it is formatted, and if it does, check whether a source manual
1061 * exists by the same name, ignoring the suffix.
1062 * If both conditions hold, drop the mlink.
1065 mlinks_undupe(struct mpage
*mpage
)
1068 struct mlink
**prev
;
1069 struct mlink
*mlink
;
1072 mpage
->form
= FORM_CAT
;
1073 prev
= &mpage
->mlinks
;
1074 while (NULL
!= (mlink
= *prev
)) {
1075 if (FORM_CAT
!= mlink
->dform
) {
1076 mpage
->form
= FORM_NONE
;
1079 (void)strlcpy(buf
, mlink
->file
, sizeof(buf
));
1080 bufp
= strstr(buf
, "cat");
1081 assert(NULL
!= bufp
);
1082 memcpy(bufp
, "man", 3);
1083 if (NULL
!= (bufp
= strrchr(buf
, '.')))
1085 (void)strlcat(buf
, mlink
->dsec
, sizeof(buf
));
1086 if (NULL
== ohash_find(&mlinks
,
1087 ohash_qlookup(&mlinks
, buf
)))
1090 say(mlink
->file
, "Man source exists: %s", buf
);
1093 *prev
= mlink
->next
;
1097 prev
= &(*prev
)->next
;
1102 mlink_check(struct mpage
*mpage
, struct mlink
*mlink
)
1108 * Check whether the manual section given in a file
1109 * agrees with the directory where the file is located.
1110 * Some manuals have suffixes like (3p) on their
1111 * section number either inside the file or in the
1112 * directory name, some are linked into more than one
1113 * section, like encrypt(1) = makekey(8).
1116 if (FORM_SRC
== mpage
->form
&&
1117 strcasecmp(mpage
->sec
, mlink
->dsec
))
1118 say(mlink
->file
, "Section \"%s\" manual in %s directory",
1119 mpage
->sec
, mlink
->dsec
);
1122 * Manual page directories exist for each kernel
1123 * architecture as returned by machine(1).
1124 * However, many manuals only depend on the
1125 * application architecture as returned by arch(1).
1126 * For example, some (2/ARM) manuals are shared
1127 * across the "armish" and "zaurus" kernel
1129 * A few manuals are even shared across completely
1130 * different architectures, for example fdformat(1)
1131 * on amd64, i386, and sparc64.
1134 if (strcasecmp(mpage
->arch
, mlink
->arch
))
1135 say(mlink
->file
, "Architecture \"%s\" manual in "
1136 "\"%s\" directory", mpage
->arch
, mlink
->arch
);
1140 * parse_cat() doesn't set NAME_TITLE yet.
1143 if (FORM_CAT
== mpage
->form
)
1147 * Check whether this mlink
1148 * appears as a name in the NAME section.
1151 slot
= ohash_qlookup(&names
, mlink
->name
);
1152 str
= ohash_find(&names
, slot
);
1153 assert(NULL
!= str
);
1154 if ( ! (NAME_TITLE
& str
->mask
))
1155 say(mlink
->file
, "Name missing in NAME section");
1159 * Run through the files in the global vector "mpages"
1160 * and add them to the database specified in "basedir".
1162 * This handles the parsing scheme itself, using the cues of directory
1163 * and filename to determine whether the file is parsable or not.
1166 mpages_merge(struct dba
*dba
, struct mparse
*mp
)
1168 struct mpage
*mpage
, *mpage_dest
;
1169 struct mlink
*mlink
, *mlink_dest
;
1170 struct roff_meta
*meta
;
1174 for (mpage
= mpage_head
; mpage
!= NULL
; mpage
= mpage
->next
) {
1175 mlinks_undupe(mpage
);
1176 if ((mlink
= mpage
->mlinks
) == NULL
)
1179 name_mask
= NAME_MASK
;
1180 mandoc_ohash_init(&names
, 4, offsetof(struct str
, key
));
1181 mandoc_ohash_init(&strings
, 6, offsetof(struct str
, key
));
1185 if ((fd
= mparse_open(mp
, mlink
->file
)) == -1) {
1186 say(mlink
->file
, "&open");
1191 * Interpret the file as mdoc(7) or man(7) source
1192 * code, unless it is known to be formatted.
1194 if (mlink
->dform
!= FORM_CAT
|| mlink
->fform
!= FORM_CAT
) {
1195 mparse_readfd(mp
, fd
, mlink
->file
);
1198 meta
= mparse_result(mp
);
1201 if (meta
!= NULL
&& meta
->sodest
!= NULL
) {
1202 mlink_dest
= ohash_find(&mlinks
,
1203 ohash_qlookup(&mlinks
, meta
->sodest
));
1204 if (mlink_dest
== NULL
) {
1205 mandoc_asprintf(&cp
, "%s.gz", meta
->sodest
);
1206 mlink_dest
= ohash_find(&mlinks
,
1207 ohash_qlookup(&mlinks
, cp
));
1210 if (mlink_dest
!= NULL
) {
1212 /* The .so target exists. */
1214 mpage_dest
= mlink_dest
->mpage
;
1216 mlink
->mpage
= mpage_dest
;
1219 * If the target was already
1220 * processed, add the links
1221 * to the database now.
1222 * Otherwise, this will
1223 * happen when we come
1227 if (mpage_dest
->dba
!= NULL
)
1230 if (mlink
->next
== NULL
)
1232 mlink
= mlink
->next
;
1235 /* Move all links to the target. */
1237 mlink
->next
= mlink_dest
->next
;
1238 mlink_dest
->next
= mpage
->mlinks
;
1239 mpage
->mlinks
= NULL
;
1242 meta
->macroset
= MACROSET_NONE
;
1244 if (meta
!= NULL
&& meta
->macroset
== MACROSET_MDOC
) {
1245 mpage
->form
= FORM_SRC
;
1246 mpage
->sec
= meta
->msec
;
1247 mpage
->sec
= mandoc_strdup(
1248 mpage
->sec
== NULL
? "" : mpage
->sec
);
1249 mpage
->arch
= meta
->arch
;
1250 mpage
->arch
= mandoc_strdup(
1251 mpage
->arch
== NULL
? "" : mpage
->arch
);
1252 mpage
->title
= mandoc_strdup(meta
->title
);
1253 } else if (meta
!= NULL
&& meta
->macroset
== MACROSET_MAN
) {
1254 if (*meta
->msec
!= '\0' || *meta
->title
!= '\0') {
1255 mpage
->form
= FORM_SRC
;
1256 mpage
->sec
= mandoc_strdup(meta
->msec
);
1257 mpage
->arch
= mandoc_strdup(mlink
->arch
);
1258 mpage
->title
= mandoc_strdup(meta
->title
);
1263 assert(mpage
->desc
== NULL
);
1264 if (meta
== NULL
|| meta
->sodest
!= NULL
) {
1265 mpage
->sec
= mandoc_strdup(mlink
->dsec
);
1266 mpage
->arch
= mandoc_strdup(mlink
->arch
);
1267 mpage
->title
= mandoc_strdup(mlink
->name
);
1269 mpage
->form
= FORM_CAT
;
1270 parse_cat(mpage
, fd
);
1272 mpage
->form
= FORM_SRC
;
1273 } else if (meta
->macroset
== MACROSET_MDOC
)
1274 parse_mdoc(mpage
, meta
, meta
->first
);
1276 parse_man(mpage
, meta
, meta
->first
);
1277 if (mpage
->desc
== NULL
) {
1278 mpage
->desc
= mandoc_strdup(mlink
->name
);
1280 say(mlink
->file
, "No one-line description, "
1281 "using filename \"%s\"", mlink
->name
);
1284 for (mlink
= mpage
->mlinks
;
1286 mlink
= mlink
->next
) {
1287 putkey(mpage
, mlink
->name
, NAME_FILE
);
1288 if (warnings
&& !use_all
)
1289 mlink_check(mpage
, mlink
);
1295 ohash_delete(&strings
);
1296 ohash_delete(&names
);
1301 parse_cat(struct mpage
*mpage
, int fd
)
1304 struct mlink
*mlink
;
1305 char *line
, *p
, *title
, *sec
;
1306 size_t linesz
, plen
, titlesz
;
1310 mlink
= mpage
->mlinks
;
1311 stream
= fd
== -1 ? fopen(mlink
->file
, "r") : fdopen(fd
, "r");
1312 if (stream
== NULL
) {
1316 say(mlink
->file
, "&fopen");
1323 /* Parse the section number from the header line. */
1325 while (getline(&line
, &linesz
, stream
) != -1) {
1328 if ((sec
= strchr(line
, '(')) == NULL
)
1330 if ((p
= strchr(++sec
, ')')) == NULL
)
1333 mpage
->sec
= mandoc_strndup(sec
, p
- sec
);
1334 if (warnings
&& *mlink
->dsec
!= '\0' &&
1335 strcasecmp(mpage
->sec
, mlink
->dsec
))
1337 "Section \"%s\" manual in %s directory",
1338 mpage
->sec
, mlink
->dsec
);
1342 /* Skip to first blank line. */
1344 while (line
== NULL
|| *line
!= '\n')
1345 if (getline(&line
, &linesz
, stream
) == -1)
1349 * Assume the first line that is not indented
1350 * is the first section header. Skip to it.
1353 while (getline(&line
, &linesz
, stream
) != -1)
1354 if (*line
!= '\n' && *line
!= ' ')
1358 * Read up until the next section into a buffer.
1359 * Strip the leading and trailing newline from each read line,
1360 * appending a trailing space.
1361 * Ignore empty (whitespace-only) lines.
1367 while ((len
= getline(&line
, &linesz
, stream
)) != -1) {
1371 while (isspace((unsigned char)line
[offs
]))
1373 if (line
[offs
] == '\0')
1375 title
= mandoc_realloc(title
, titlesz
+ len
- offs
);
1376 memcpy(title
+ titlesz
, line
+ offs
, len
- offs
);
1377 titlesz
+= len
- offs
;
1378 title
[titlesz
- 1] = ' ';
1383 * If no page content can be found, or the input line
1384 * is already the next section header, or there is no
1385 * trailing newline, reuse the page title as the page
1389 if (NULL
== title
|| '\0' == *title
) {
1391 say(mlink
->file
, "Cannot find NAME section");
1397 title
[titlesz
- 1] = '\0';
1400 * Skip to the first dash.
1401 * Use the remaining line as the description (no more than 70
1405 if (NULL
!= (p
= strstr(title
, "- "))) {
1406 for (p
+= 2; ' ' == *p
|| '\b' == *p
; p
++)
1407 /* Skip to next word. */ ;
1410 say(mlink
->file
, "No dash in title line, "
1411 "reusing \"%s\" as one-line description", title
);
1417 /* Strip backspace-encoding from line. */
1419 while (NULL
!= (line
= memchr(p
, '\b', plen
))) {
1422 memmove(line
, line
+ 1, plen
--);
1425 memmove(line
- 1, line
+ 1, plen
- len
);
1430 * Cut off excessive one-line descriptions.
1431 * Bad pages are not worth better heuristics.
1434 mpage
->desc
= mandoc_strndup(p
, 150);
1440 * Put a type/word pair into the word database for this particular file.
1443 putkey(const struct mpage
*mpage
, char *value
, uint64_t type
)
1445 putkeys(mpage
, value
, strlen(value
), type
);
1449 * Grok all nodes at or below a certain mdoc node into putkey().
1452 putmdockey(const struct mpage
*mpage
,
1453 const struct roff_node
*n
, uint64_t m
, int taboo
)
1456 for ( ; NULL
!= n
; n
= n
->next
) {
1457 if (n
->flags
& taboo
)
1459 if (NULL
!= n
->child
)
1460 putmdockey(mpage
, n
->child
, m
, taboo
);
1461 if (n
->type
== ROFFT_TEXT
)
1462 putkey(mpage
, n
->string
, m
);
1467 parse_man(struct mpage
*mpage
, const struct roff_meta
*meta
,
1468 const struct roff_node
*n
)
1470 const struct roff_node
*head
, *body
;
1471 char *start
, *title
;
1479 * We're only searching for one thing: the first text child in
1480 * the BODY of a NAME section. Since we don't keep track of
1481 * sections in -man, run some hoops to find out whether we're in
1482 * the correct section or not.
1485 if (n
->type
== ROFFT_BODY
&& n
->tok
== MAN_SH
) {
1487 if ((head
= body
->parent
->head
) != NULL
&&
1488 (head
= head
->child
) != NULL
&&
1489 head
->next
== NULL
&&
1490 head
->type
== ROFFT_TEXT
&&
1491 strcmp(head
->string
, "NAME") == 0 &&
1492 body
->child
!= NULL
) {
1495 * Suck the entire NAME section into memory.
1496 * Yes, we might run away.
1497 * But too many manuals have big, spread-out
1498 * NAME sections over many lines.
1502 deroff(&title
, body
);
1507 * Go through a special heuristic dance here.
1508 * Conventionally, one or more manual names are
1509 * comma-specified prior to a whitespace, then a
1510 * dash, then a description. Try to puzzle out
1511 * the name parts here.
1516 sz
= strcspn(start
, " ,");
1517 if ('\0' == start
[sz
])
1524 * Assume a stray trailing comma in the
1525 * name list if a name begins with a dash.
1528 if ('-' == start
[0] ||
1529 ('\\' == start
[0] && '-' == start
[1]))
1532 putkey(mpage
, start
, NAME_TITLE
);
1533 if ( ! (mpage
->name_head_done
||
1534 strcasecmp(start
, meta
->title
))) {
1535 putkey(mpage
, start
, NAME_HEAD
);
1536 mpage
->name_head_done
= 1;
1544 assert(',' == byte
);
1546 while (' ' == *start
)
1550 if (start
== title
) {
1551 putkey(mpage
, start
, NAME_TITLE
);
1552 if ( ! (mpage
->name_head_done
||
1553 strcasecmp(start
, meta
->title
))) {
1554 putkey(mpage
, start
, NAME_HEAD
);
1555 mpage
->name_head_done
= 1;
1561 while (isspace((unsigned char)*start
))
1564 if (0 == strncmp(start
, "-", 1))
1566 else if (0 == strncmp(start
, "\\-\\-", 4))
1568 else if (0 == strncmp(start
, "\\-", 2))
1570 else if (0 == strncmp(start
, "\\(en", 4))
1572 else if (0 == strncmp(start
, "\\(em", 4))
1575 while (' ' == *start
)
1579 * Cut off excessive one-line descriptions.
1580 * Bad pages are not worth better heuristics.
1583 mpage
->desc
= mandoc_strndup(start
, 150);
1589 for (n
= n
->child
; n
; n
= n
->next
) {
1590 if (NULL
!= mpage
->desc
)
1592 parse_man(mpage
, meta
, n
);
1597 parse_mdoc(struct mpage
*mpage
, const struct roff_meta
*meta
,
1598 const struct roff_node
*n
)
1600 const struct mdoc_handler
*handler
;
1602 for (n
= n
->child
; n
!= NULL
; n
= n
->next
) {
1603 if (n
->tok
== TOKEN_NONE
|| n
->tok
< ROFF_MAX
)
1605 assert(n
->tok
>= MDOC_Dd
&& n
->tok
< MDOC_MAX
);
1606 handler
= mdoc_handlers
+ (n
->tok
- MDOC_Dd
);
1607 if (n
->flags
& handler
->taboo
)
1616 if (handler
->fp
!= NULL
&&
1617 (*handler
->fp
)(mpage
, meta
, n
) == 0)
1620 putmdockey(mpage
, n
->child
,
1621 handler
->mask
, handler
->taboo
);
1626 if (NULL
!= n
->child
)
1627 parse_mdoc(mpage
, meta
, n
);
1632 parse_mdoc_Fa(struct mpage
*mpage
, const struct roff_meta
*meta
,
1633 const struct roff_node
*n
)
1638 if (n
->sec
== SEC_SYNOPSIS
)
1641 putmdockey(mpage
, n
->child
, mask
, 0);
1646 parse_mdoc_Fd(struct mpage
*mpage
, const struct roff_meta
*meta
,
1647 const struct roff_node
*n
)
1652 if (SEC_SYNOPSIS
!= n
->sec
||
1653 NULL
== (n
= n
->child
) ||
1654 n
->type
!= ROFFT_TEXT
)
1658 * Only consider those `Fd' macro fields that begin with an
1659 * "inclusion" token (versus, e.g., #define).
1662 if (strcmp("#include", n
->string
))
1665 if ((n
= n
->next
) == NULL
|| n
->type
!= ROFFT_TEXT
)
1669 * Strip away the enclosing angle brackets and make sure we're
1674 if ('<' == *start
|| '"' == *start
)
1677 if (0 == (sz
= strlen(start
)))
1680 end
= &start
[(int)sz
- 1];
1681 if ('>' == *end
|| '"' == *end
)
1685 putkeys(mpage
, start
, end
- start
+ 1, TYPE_In
);
1690 parse_mdoc_fname(struct mpage
*mpage
, const struct roff_node
*n
)
1695 if (n
->type
!= ROFFT_TEXT
)
1698 /* Skip function pointer punctuation. */
1701 while (*cp
== '(' || *cp
== '*')
1703 sz
= strcspn(cp
, "()");
1705 putkeys(mpage
, cp
, sz
, TYPE_Fn
);
1706 if (n
->sec
== SEC_SYNOPSIS
)
1707 putkeys(mpage
, cp
, sz
, NAME_SYN
);
1711 parse_mdoc_Fn(struct mpage
*mpage
, const struct roff_meta
*meta
,
1712 const struct roff_node
*n
)
1716 if (n
->child
== NULL
)
1719 parse_mdoc_fname(mpage
, n
->child
);
1722 if (n
!= NULL
&& n
->type
== ROFFT_TEXT
) {
1724 if (n
->sec
== SEC_SYNOPSIS
)
1726 putmdockey(mpage
, n
, mask
, 0);
1733 parse_mdoc_Fo(struct mpage
*mpage
, const struct roff_meta
*meta
,
1734 const struct roff_node
*n
)
1737 if (n
->type
!= ROFFT_HEAD
)
1740 if (n
->child
!= NULL
)
1741 parse_mdoc_fname(mpage
, n
->child
);
1747 parse_mdoc_Va(struct mpage
*mpage
, const struct roff_meta
*meta
,
1748 const struct roff_node
*n
)
1752 if (n
->type
!= ROFFT_ELEM
&& n
->type
!= ROFFT_BODY
)
1755 if (n
->child
!= NULL
&&
1756 n
->child
->next
== NULL
&&
1757 n
->child
->type
== ROFFT_TEXT
)
1763 putkey(mpage
, cp
, TYPE_Vt
| (n
->tok
== MDOC_Va
||
1764 n
->type
== ROFFT_BODY
? TYPE_Va
: 0));
1772 parse_mdoc_Xr(struct mpage
*mpage
, const struct roff_meta
*meta
,
1773 const struct roff_node
*n
)
1777 if (NULL
== (n
= n
->child
))
1780 if (NULL
== n
->next
) {
1781 putkey(mpage
, n
->string
, TYPE_Xr
);
1785 mandoc_asprintf(&cp
, "%s(%s)", n
->string
, n
->next
->string
);
1786 putkey(mpage
, cp
, TYPE_Xr
);
1792 parse_mdoc_Nd(struct mpage
*mpage
, const struct roff_meta
*meta
,
1793 const struct roff_node
*n
)
1796 if (n
->type
== ROFFT_BODY
)
1797 deroff(&mpage
->desc
, n
);
1802 parse_mdoc_Nm(struct mpage
*mpage
, const struct roff_meta
*meta
,
1803 const struct roff_node
*n
)
1806 if (SEC_NAME
== n
->sec
)
1807 putmdockey(mpage
, n
->child
, NAME_TITLE
, 0);
1808 else if (n
->sec
== SEC_SYNOPSIS
&& n
->type
== ROFFT_HEAD
) {
1809 if (n
->child
== NULL
)
1810 putkey(mpage
, meta
->name
, NAME_SYN
);
1812 putmdockey(mpage
, n
->child
, NAME_SYN
, 0);
1814 if ( ! (mpage
->name_head_done
||
1815 n
->child
== NULL
|| n
->child
->string
== NULL
||
1816 strcasecmp(n
->child
->string
, meta
->title
))) {
1817 putkey(mpage
, n
->child
->string
, NAME_HEAD
);
1818 mpage
->name_head_done
= 1;
1824 parse_mdoc_Sh(struct mpage
*mpage
, const struct roff_meta
*meta
,
1825 const struct roff_node
*n
)
1828 return n
->sec
== SEC_CUSTOM
&& n
->type
== ROFFT_HEAD
;
1832 parse_mdoc_head(struct mpage
*mpage
, const struct roff_meta
*meta
,
1833 const struct roff_node
*n
)
1836 return n
->type
== ROFFT_HEAD
;
1840 * Add a string to the hash table for the current manual.
1841 * Each string has a bitmask telling which macros it belongs to.
1842 * When we finish the manual, we'll dump the table.
1845 putkeys(const struct mpage
*mpage
, char *cp
, size_t sz
, uint64_t v
)
1856 mustfree
= render_string(&cp
, &sz
);
1862 name_mask
&= ~NAME_FIRST
;
1864 say(mpage
->mlinks
->file
,
1865 "Adding name %*s, bits=0x%llx", (int)sz
, cp
,
1866 (unsigned long long)v
);
1870 for (i
= 0; i
< KEY_MAX
; i
++)
1871 if ((uint64_t)1 << i
& v
)
1872 say(mpage
->mlinks
->file
,
1873 "Adding key %s=%*s",
1874 mansearch_keynames
[i
], (int)sz
, cp
);
1878 slot
= ohash_qlookupi(htab
, cp
, &end
);
1879 s
= ohash_find(htab
, slot
);
1881 if (NULL
!= s
&& mpage
== s
->mpage
) {
1884 } else if (NULL
== s
) {
1885 s
= mandoc_calloc(1, sizeof(struct str
) + sz
+ 1);
1886 memcpy(s
->key
, cp
, sz
);
1887 ohash_insert(htab
, slot
, s
);
1897 * Take a Unicode codepoint and produce its UTF-8 encoding.
1898 * This isn't the best way to do this, but it works.
1899 * The magic numbers are from the UTF-8 packaging.
1900 * They're not as scary as they seem: read the UTF-8 spec for details.
1903 utf8(unsigned int cp
, char out
[7])
1908 if (cp
<= 0x0000007F) {
1911 } else if (cp
<= 0x000007FF) {
1913 out
[0] = (cp
>> 6 & 31) | 192;
1914 out
[1] = (cp
& 63) | 128;
1915 } else if (cp
<= 0x0000FFFF) {
1917 out
[0] = (cp
>> 12 & 15) | 224;
1918 out
[1] = (cp
>> 6 & 63) | 128;
1919 out
[2] = (cp
& 63) | 128;
1920 } else if (cp
<= 0x001FFFFF) {
1922 out
[0] = (cp
>> 18 & 7) | 240;
1923 out
[1] = (cp
>> 12 & 63) | 128;
1924 out
[2] = (cp
>> 6 & 63) | 128;
1925 out
[3] = (cp
& 63) | 128;
1926 } else if (cp
<= 0x03FFFFFF) {
1928 out
[0] = (cp
>> 24 & 3) | 248;
1929 out
[1] = (cp
>> 18 & 63) | 128;
1930 out
[2] = (cp
>> 12 & 63) | 128;
1931 out
[3] = (cp
>> 6 & 63) | 128;
1932 out
[4] = (cp
& 63) | 128;
1933 } else if (cp
<= 0x7FFFFFFF) {
1935 out
[0] = (cp
>> 30 & 1) | 252;
1936 out
[1] = (cp
>> 24 & 63) | 128;
1937 out
[2] = (cp
>> 18 & 63) | 128;
1938 out
[3] = (cp
>> 12 & 63) | 128;
1939 out
[4] = (cp
>> 6 & 63) | 128;
1940 out
[5] = (cp
& 63) | 128;
1949 * If the string contains escape sequences,
1950 * replace it with an allocated rendering and return 1,
1951 * such that the caller can free it after use.
1952 * Otherwise, do nothing and return 0.
1955 render_string(char **public, size_t *psz
)
1957 const char *src
, *scp
, *addcp
, *seq
;
1959 size_t ssz
, dsz
, addsz
;
1960 char utfbuf
[7], res
[6];
1961 int seqlen
, unicode
;
1965 res
[2] = ASCII_NBRSP
;
1966 res
[3] = ASCII_HYPH
;
1967 res
[4] = ASCII_BREAK
;
1970 src
= scp
= *public;
1975 while (scp
< src
+ *psz
) {
1977 /* Leave normal characters unchanged. */
1979 if (strchr(res
, *scp
) == NULL
) {
1987 * Found something that requires replacing,
1988 * make sure we have a destination buffer.
1992 dst
= mandoc_malloc(ssz
+ 1);
1994 memcpy(dst
, src
, dsz
);
1997 /* Handle single-char special characters. */
2018 * Found an escape sequence.
2019 * Read past the slash, then parse it.
2020 * Ignore everything except characters.
2024 if (mandoc_escape(&scp
, &seq
, &seqlen
) != ESCAPE_SPECIAL
)
2028 * Render the special character
2029 * as either UTF-8 or ASCII.
2033 unicode
= mchars_spec2cp(seq
, seqlen
);
2036 addsz
= utf8(unicode
, utfbuf
);
2041 addcp
= mchars_spec2str(seq
, seqlen
, &addsz
);
2044 if (*addcp
== ASCII_NBRSP
) {
2050 /* Copy the rendered glyph into the stream. */
2053 dst
= mandoc_realloc(dst
, ssz
+ 1);
2054 memcpy(dst
+ dsz
, addcp
, addsz
);
2062 /* Trim trailing whitespace and NUL-terminate. */
2064 while (*psz
> 0 && (*public)[*psz
- 1] == ' ')
2067 (*public)[*psz
] = '\0';
2074 dbadd_mlink(const struct mlink
*mlink
)
2076 dba_page_alias(mlink
->mpage
->dba
, mlink
->name
, NAME_FILE
);
2077 dba_page_add(mlink
->mpage
->dba
, DBP_SECT
, mlink
->dsec
);
2078 dba_page_add(mlink
->mpage
->dba
, DBP_SECT
, mlink
->fsec
);
2079 dba_page_add(mlink
->mpage
->dba
, DBP_ARCH
, mlink
->arch
);
2080 dba_page_add(mlink
->mpage
->dba
, DBP_FILE
, mlink
->file
);
2084 * Flush the current page's terms (and their bits) into the database.
2085 * Also, handle escape sequences at the last possible moment.
2088 dbadd(struct dba
*dba
, struct mpage
*mpage
)
2090 struct mlink
*mlink
;
2098 mlink
= mpage
->mlinks
;
2101 for (key
= ohash_first(&names
, &slot
); NULL
!= key
;
2102 key
= ohash_next(&names
, &slot
))
2104 for (key
= ohash_first(&strings
, &slot
); NULL
!= key
;
2105 key
= ohash_next(&strings
, &slot
))
2109 while (NULL
!= mlink
) {
2110 fputs(mlink
->name
, stdout
);
2111 if (NULL
== mlink
->next
||
2112 strcmp(mlink
->dsec
, mlink
->next
->dsec
) ||
2113 strcmp(mlink
->fsec
, mlink
->next
->fsec
) ||
2114 strcmp(mlink
->arch
, mlink
->next
->arch
)) {
2116 if ('\0' == *mlink
->dsec
)
2117 fputs(mlink
->fsec
, stdout
);
2119 fputs(mlink
->dsec
, stdout
);
2120 if ('\0' != *mlink
->arch
)
2121 printf("/%s", mlink
->arch
);
2124 mlink
= mlink
->next
;
2126 fputs(", ", stdout
);
2128 printf(" - %s\n", mpage
->desc
);
2133 say(mlink
->file
, "Adding to database");
2137 mustfree
= render_string(&cp
, &i
);
2138 mpage
->dba
= dba_page_new(dba
->pages
,
2139 *mpage
->arch
== '\0' ? mlink
->arch
: mpage
->arch
,
2140 cp
, mlink
->file
, mpage
->form
);
2143 dba_page_add(mpage
->dba
, DBP_SECT
, mpage
->sec
);
2145 while (mlink
!= NULL
) {
2147 mlink
= mlink
->next
;
2150 for (key
= ohash_first(&names
, &slot
); NULL
!= key
;
2151 key
= ohash_next(&names
, &slot
)) {
2152 assert(key
->mpage
== mpage
);
2153 dba_page_alias(mpage
->dba
, key
->key
, key
->mask
);
2156 for (key
= ohash_first(&strings
, &slot
); NULL
!= key
;
2157 key
= ohash_next(&strings
, &slot
)) {
2158 assert(key
->mpage
== mpage
);
2160 for (mask
= TYPE_Xr
; mask
<= TYPE_Lb
; mask
*= 2) {
2161 if (key
->mask
& mask
)
2162 dba_macro_add(dba
->macros
, i
,
2163 key
->key
, mpage
->dba
);
2171 dbprune(struct dba
*dba
)
2173 struct dba_array
*page
, *files
;
2176 dba_array_FOREACH(dba
->pages
, page
) {
2177 files
= dba_array_get(page
, DBP_FILE
);
2178 dba_array_FOREACH(files
, file
) {
2181 if (ohash_find(&mlinks
, ohash_qlookup(&mlinks
,
2184 say(file
, "Deleting from database");
2185 dba_array_del(dba
->pages
);
2193 * Write the database from memory to disk.
2196 dbwrite(struct dba
*dba
)
2198 struct stat sb1
, sb2
;
2199 char tfn
[33], *cp1
, *cp2
;
2204 * Do not write empty databases, and delete existing ones
2205 * when makewhatis -u causes them to become empty.
2208 dba_array_start(dba
->pages
);
2209 if (dba_array_next(dba
->pages
) == NULL
) {
2210 if (unlink(MANDOC_DB
) == -1 && errno
!= ENOENT
)
2211 say(MANDOC_DB
, "&unlink");
2216 * Build the database in a temporary file,
2217 * then atomically move it into place.
2220 if (dba_write(MANDOC_DB
"~", dba
) != -1) {
2221 if (rename(MANDOC_DB
"~", MANDOC_DB
) == -1) {
2222 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2223 say(MANDOC_DB
, "&rename");
2224 unlink(MANDOC_DB
"~");
2230 * We lack write permission and cannot replace the database
2231 * file, but let's at least check whether the data changed.
2234 (void)strlcpy(tfn
, "/tmp/mandocdb.XXXXXXXX", sizeof(tfn
));
2235 if (mkdtemp(tfn
) == NULL
) {
2236 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2237 say("", "&%s", tfn
);
2240 cp1
= cp2
= MAP_FAILED
;
2242 (void)strlcat(tfn
, "/" MANDOC_DB
, sizeof(tfn
));
2243 if (dba_write(tfn
, dba
) == -1) {
2244 say(tfn
, "&dba_write");
2247 if ((fd1
= open(MANDOC_DB
, O_RDONLY
, 0)) == -1) {
2248 say(MANDOC_DB
, "&open");
2251 if ((fd2
= open(tfn
, O_RDONLY
, 0)) == -1) {
2255 if (fstat(fd1
, &sb1
) == -1) {
2256 say(MANDOC_DB
, "&fstat");
2259 if (fstat(fd2
, &sb2
) == -1) {
2263 if (sb1
.st_size
!= sb2
.st_size
)
2265 if ((cp1
= mmap(NULL
, sb1
.st_size
, PROT_READ
, MAP_PRIVATE
,
2266 fd1
, 0)) == MAP_FAILED
) {
2267 say(MANDOC_DB
, "&mmap");
2270 if ((cp2
= mmap(NULL
, sb2
.st_size
, PROT_READ
, MAP_PRIVATE
,
2271 fd2
, 0)) == MAP_FAILED
) {
2275 for (i
= 0; i
< sb1
.st_size
; i
++)
2276 if (cp1
[i
] != cp2
[i
])
2281 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2282 say(MANDOC_DB
, "Data changed, but cannot replace database");
2285 if (cp1
!= MAP_FAILED
)
2286 munmap(cp1
, sb1
.st_size
);
2287 if (cp2
!= MAP_FAILED
)
2288 munmap(cp2
, sb2
.st_size
);
2294 *strrchr(tfn
, '/') = '\0';
2299 set_basedir(const char *targetdir
, int report_baddir
)
2301 static char startdir
[PATH_MAX
];
2302 static int getcwd_status
; /* 1 = ok, 2 = failure */
2303 static int chdir_status
; /* 1 = changed directory */
2306 * Remember the original working directory, if possible.
2307 * This will be needed if the second or a later directory
2308 * on the command line is given as a relative path.
2309 * Do not error out if the current directory is not
2310 * searchable: Maybe it won't be needed after all.
2312 if (getcwd_status
== 0) {
2313 if (getcwd(startdir
, sizeof(startdir
)) == NULL
) {
2315 (void)strlcpy(startdir
, strerror(errno
),
2322 * We are leaving the old base directory.
2323 * Do not use it any longer, not even for messages.
2329 * If and only if the directory was changed earlier and
2330 * the next directory to process is given as a relative path,
2331 * first go back, or bail out if that is impossible.
2333 if (chdir_status
&& *targetdir
!= '/') {
2334 if (getcwd_status
== 2) {
2335 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2336 say("", "getcwd: %s", startdir
);
2339 if (chdir(startdir
) == -1) {
2340 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2341 say("", "&chdir %s", startdir
);
2347 * Always resolve basedir to the canonicalized absolute
2348 * pathname and append a trailing slash, such that
2349 * we can reliably check whether files are inside.
2351 if (realpath(targetdir
, basedir
) == NULL
) {
2352 if (report_baddir
|| errno
!= ENOENT
) {
2353 exitcode
= (int)MANDOCLEVEL_BADARG
;
2354 say("", "&%s: realpath", targetdir
);
2358 } else if (chdir(basedir
) == -1) {
2359 if (report_baddir
|| errno
!= ENOENT
) {
2360 exitcode
= (int)MANDOCLEVEL_BADARG
;
2367 basedir_len
= strlen(basedir
);
2368 if (basedir
[basedir_len
- 1] != '/') {
2369 if (basedir_len
>= PATH_MAX
- 1) {
2370 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2371 say("", "Filename too long");
2376 basedir
[basedir_len
++] = '/';
2377 basedir
[basedir_len
] = '\0';
2383 say(const char *file
, const char *format
, ...)
2388 if (*basedir
!= '\0')
2389 fprintf(stderr
, "%s", basedir
);
2390 if (*basedir
!= '\0' && *file
!= '\0')
2393 fprintf(stderr
, "%s", file
);
2396 if (format
!= NULL
) {
2409 if (format
!= NULL
) {
2410 if (*basedir
!= '\0' || *file
!= '\0')
2411 fputs(": ", stderr
);
2412 va_start(ap
, format
);
2413 vfprintf(stderr
, format
, ap
);
2417 if (*basedir
!= '\0' || *file
!= '\0' || format
!= NULL
)
2418 fputs(": ", stderr
);
2421 fputc('\n', stderr
);