]>
git.cameronkatri.com Git - mandoc.git/blob - mandocdb.c
1 /* $Id: mandocdb.c,v 1.258 2018/02/23 18:25:57 schwarze Exp $ */
3 * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011-2017 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"
56 #include "mansearch.h"
57 #include "dba_array.h"
60 extern const char *const mansearch_keynames
[];
63 OP_DEFAULT
= 0, /* new dbs from dir list or default config */
64 OP_CONFFILE
, /* new databases from custom config file */
65 OP_UPDATE
, /* delete/add entries in existing database */
66 OP_DELETE
, /* delete entries from existing database */
67 OP_TEST
/* change no databases, report potential problems */
71 const struct mpage
*mpage
; /* if set, the owning parse */
72 uint64_t mask
; /* bitmask in sequence */
73 char key
[]; /* rendered text */
82 struct inodev inodev
; /* used for hashing routine */
83 struct dba_array
*dba
;
84 char *sec
; /* section from file content */
85 char *arch
; /* architecture from file content */
86 char *title
; /* title from file content */
87 char *desc
; /* description from file content */
88 struct mpage
*next
; /* singly linked list */
89 struct mlink
*mlinks
; /* singly linked list */
91 enum form form
; /* format from file content */
95 char file
[PATH_MAX
]; /* filename rel. to manpath */
96 char *dsec
; /* section from directory */
97 char *arch
; /* architecture from directory */
98 char *name
; /* name from file name (not empty) */
99 char *fsec
; /* section from file name suffix */
100 struct mlink
*next
; /* singly linked list */
101 struct mpage
*mpage
; /* parent */
102 int gzip
; /* filename has a .gz suffix */
103 enum form dform
; /* format from directory */
104 enum form fform
; /* format from file name suffix */
107 typedef int (*mdoc_fp
)(struct mpage
*, const struct roff_meta
*,
108 const struct roff_node
*);
110 struct mdoc_handler
{
111 mdoc_fp fp
; /* optional handler */
112 uint64_t mask
; /* set unless handler returns 0 */
113 int taboo
; /* node flags that must not be set */
117 int mandocdb(int, char *[]);
119 static void dbadd(struct dba
*, struct mpage
*);
120 static void dbadd_mlink(const struct mlink
*mlink
);
121 static void dbprune(struct dba
*);
122 static void dbwrite(struct dba
*);
123 static void filescan(const char *);
124 #if HAVE_FTS_COMPARE_CONST
125 static int fts_compare(const FTSENT
*const *, const FTSENT
*const *);
127 static int fts_compare(const FTSENT
**, const FTSENT
**);
129 static void mlink_add(struct mlink
*, const struct stat
*);
130 static void mlink_check(struct mpage
*, struct mlink
*);
131 static void mlink_free(struct mlink
*);
132 static void mlinks_undupe(struct mpage
*);
133 static void mpages_free(void);
134 static void mpages_merge(struct dba
*, struct mparse
*);
135 static void parse_cat(struct mpage
*, int);
136 static void parse_man(struct mpage
*, const struct roff_meta
*,
137 const struct roff_node
*);
138 static void parse_mdoc(struct mpage
*, const struct roff_meta
*,
139 const struct roff_node
*);
140 static int parse_mdoc_head(struct mpage
*, const struct roff_meta
*,
141 const struct roff_node
*);
142 static int parse_mdoc_Fa(struct mpage
*, const struct roff_meta
*,
143 const struct roff_node
*);
144 static int parse_mdoc_Fd(struct mpage
*, const struct roff_meta
*,
145 const struct roff_node
*);
146 static void parse_mdoc_fname(struct mpage
*, const struct roff_node
*);
147 static int parse_mdoc_Fn(struct mpage
*, const struct roff_meta
*,
148 const struct roff_node
*);
149 static int parse_mdoc_Fo(struct mpage
*, const struct roff_meta
*,
150 const struct roff_node
*);
151 static int parse_mdoc_Nd(struct mpage
*, const struct roff_meta
*,
152 const struct roff_node
*);
153 static int parse_mdoc_Nm(struct mpage
*, const struct roff_meta
*,
154 const struct roff_node
*);
155 static int parse_mdoc_Sh(struct mpage
*, const struct roff_meta
*,
156 const struct roff_node
*);
157 static int parse_mdoc_Va(struct mpage
*, const struct roff_meta
*,
158 const struct roff_node
*);
159 static int parse_mdoc_Xr(struct mpage
*, const struct roff_meta
*,
160 const struct roff_node
*);
161 static void putkey(const struct mpage
*, char *, uint64_t);
162 static void putkeys(const struct mpage
*, char *, size_t, uint64_t);
163 static void putmdockey(const struct mpage
*,
164 const struct roff_node
*, uint64_t, int);
165 static int render_string(char **, size_t *);
166 static void say(const char *, const char *, ...)
167 __attribute__((__format__ (__printf__
, 2, 3)));
168 static int set_basedir(const char *, int);
169 static int treescan(void);
170 static size_t utf8(unsigned int, char [7]);
172 static int nodb
; /* no database changes */
173 static int mparse_options
; /* abort the parse early */
174 static int use_all
; /* use all found files */
175 static int debug
; /* print what we're doing */
176 static int warnings
; /* warn about crap */
177 static int write_utf8
; /* write UTF-8 output; else ASCII */
178 static int exitcode
; /* to be returned by main */
179 static enum op op
; /* operational mode */
180 static char basedir
[PATH_MAX
]; /* current base directory */
181 static struct mpage
*mpage_head
; /* list of distinct manual pages */
182 static struct ohash mpages
; /* table of distinct manual pages */
183 static struct ohash mlinks
; /* table of directory entries */
184 static struct ohash names
; /* table of all names */
185 static struct ohash strings
; /* table of all strings */
186 static uint64_t name_mask
;
188 static const struct mdoc_handler __mdocs
[MDOC_MAX
- MDOC_Dd
] = {
189 { NULL
, 0, NODE_NOPRT
}, /* Dd */
190 { NULL
, 0, NODE_NOPRT
}, /* Dt */
191 { NULL
, 0, NODE_NOPRT
}, /* Os */
192 { parse_mdoc_Sh
, TYPE_Sh
, 0 }, /* Sh */
193 { parse_mdoc_head
, TYPE_Ss
, 0 }, /* Ss */
194 { NULL
, 0, 0 }, /* Pp */
195 { NULL
, 0, 0 }, /* D1 */
196 { NULL
, 0, 0 }, /* Dl */
197 { NULL
, 0, 0 }, /* Bd */
198 { NULL
, 0, 0 }, /* Ed */
199 { NULL
, 0, 0 }, /* Bl */
200 { NULL
, 0, 0 }, /* El */
201 { NULL
, 0, 0 }, /* It */
202 { NULL
, 0, 0 }, /* Ad */
203 { NULL
, TYPE_An
, 0 }, /* An */
204 { NULL
, 0, 0 }, /* Ap */
205 { NULL
, TYPE_Ar
, 0 }, /* Ar */
206 { NULL
, TYPE_Cd
, 0 }, /* Cd */
207 { NULL
, TYPE_Cm
, 0 }, /* Cm */
208 { NULL
, TYPE_Dv
, 0 }, /* Dv */
209 { NULL
, TYPE_Er
, 0 }, /* Er */
210 { NULL
, TYPE_Ev
, 0 }, /* Ev */
211 { NULL
, 0, 0 }, /* Ex */
212 { parse_mdoc_Fa
, 0, 0 }, /* Fa */
213 { parse_mdoc_Fd
, 0, 0 }, /* Fd */
214 { NULL
, TYPE_Fl
, 0 }, /* Fl */
215 { parse_mdoc_Fn
, 0, 0 }, /* Fn */
216 { NULL
, TYPE_Ft
| TYPE_Vt
, 0 }, /* Ft */
217 { NULL
, TYPE_Ic
, 0 }, /* Ic */
218 { NULL
, TYPE_In
, 0 }, /* In */
219 { NULL
, TYPE_Li
, 0 }, /* Li */
220 { parse_mdoc_Nd
, 0, 0 }, /* Nd */
221 { parse_mdoc_Nm
, 0, 0 }, /* Nm */
222 { NULL
, 0, 0 }, /* Op */
223 { NULL
, 0, 0 }, /* Ot */
224 { NULL
, TYPE_Pa
, NODE_NOSRC
}, /* Pa */
225 { NULL
, 0, 0 }, /* Rv */
226 { NULL
, TYPE_St
, 0 }, /* St */
227 { parse_mdoc_Va
, TYPE_Va
, 0 }, /* Va */
228 { parse_mdoc_Va
, TYPE_Vt
, 0 }, /* Vt */
229 { parse_mdoc_Xr
, 0, 0 }, /* Xr */
230 { NULL
, 0, 0 }, /* %A */
231 { NULL
, 0, 0 }, /* %B */
232 { NULL
, 0, 0 }, /* %D */
233 { NULL
, 0, 0 }, /* %I */
234 { NULL
, 0, 0 }, /* %J */
235 { NULL
, 0, 0 }, /* %N */
236 { NULL
, 0, 0 }, /* %O */
237 { NULL
, 0, 0 }, /* %P */
238 { NULL
, 0, 0 }, /* %R */
239 { NULL
, 0, 0 }, /* %T */
240 { NULL
, 0, 0 }, /* %V */
241 { NULL
, 0, 0 }, /* Ac */
242 { NULL
, 0, 0 }, /* Ao */
243 { NULL
, 0, 0 }, /* Aq */
244 { NULL
, TYPE_At
, 0 }, /* At */
245 { NULL
, 0, 0 }, /* Bc */
246 { NULL
, 0, 0 }, /* Bf */
247 { NULL
, 0, 0 }, /* Bo */
248 { NULL
, 0, 0 }, /* Bq */
249 { NULL
, TYPE_Bsx
, NODE_NOSRC
}, /* Bsx */
250 { NULL
, TYPE_Bx
, NODE_NOSRC
}, /* Bx */
251 { NULL
, 0, 0 }, /* Db */
252 { NULL
, 0, 0 }, /* Dc */
253 { NULL
, 0, 0 }, /* Do */
254 { NULL
, 0, 0 }, /* Dq */
255 { NULL
, 0, 0 }, /* Ec */
256 { NULL
, 0, 0 }, /* Ef */
257 { NULL
, TYPE_Em
, 0 }, /* Em */
258 { NULL
, 0, 0 }, /* Eo */
259 { NULL
, TYPE_Fx
, NODE_NOSRC
}, /* Fx */
260 { NULL
, TYPE_Ms
, 0 }, /* Ms */
261 { NULL
, 0, 0 }, /* No */
262 { NULL
, 0, 0 }, /* Ns */
263 { NULL
, TYPE_Nx
, NODE_NOSRC
}, /* Nx */
264 { NULL
, TYPE_Ox
, NODE_NOSRC
}, /* Ox */
265 { NULL
, 0, 0 }, /* Pc */
266 { NULL
, 0, 0 }, /* Pf */
267 { NULL
, 0, 0 }, /* Po */
268 { NULL
, 0, 0 }, /* Pq */
269 { NULL
, 0, 0 }, /* Qc */
270 { NULL
, 0, 0 }, /* Ql */
271 { NULL
, 0, 0 }, /* Qo */
272 { NULL
, 0, 0 }, /* Qq */
273 { NULL
, 0, 0 }, /* Re */
274 { NULL
, 0, 0 }, /* Rs */
275 { NULL
, 0, 0 }, /* Sc */
276 { NULL
, 0, 0 }, /* So */
277 { NULL
, 0, 0 }, /* Sq */
278 { NULL
, 0, 0 }, /* Sm */
279 { NULL
, 0, 0 }, /* Sx */
280 { NULL
, TYPE_Sy
, 0 }, /* Sy */
281 { NULL
, TYPE_Tn
, 0 }, /* Tn */
282 { NULL
, 0, NODE_NOSRC
}, /* Ux */
283 { NULL
, 0, 0 }, /* Xc */
284 { NULL
, 0, 0 }, /* Xo */
285 { parse_mdoc_Fo
, 0, 0 }, /* Fo */
286 { NULL
, 0, 0 }, /* Fc */
287 { NULL
, 0, 0 }, /* Oo */
288 { NULL
, 0, 0 }, /* Oc */
289 { NULL
, 0, 0 }, /* Bk */
290 { NULL
, 0, 0 }, /* Ek */
291 { NULL
, 0, 0 }, /* Bt */
292 { NULL
, 0, 0 }, /* Hf */
293 { NULL
, 0, 0 }, /* Fr */
294 { NULL
, 0, 0 }, /* Ud */
295 { NULL
, TYPE_Lb
, NODE_NOSRC
}, /* Lb */
296 { NULL
, 0, 0 }, /* Lp */
297 { NULL
, TYPE_Lk
, 0 }, /* Lk */
298 { NULL
, TYPE_Mt
, NODE_NOSRC
}, /* Mt */
299 { NULL
, 0, 0 }, /* Brq */
300 { NULL
, 0, 0 }, /* Bro */
301 { NULL
, 0, 0 }, /* Brc */
302 { NULL
, 0, 0 }, /* %C */
303 { NULL
, 0, 0 }, /* Es */
304 { NULL
, 0, 0 }, /* En */
305 { NULL
, TYPE_Dx
, NODE_NOSRC
}, /* Dx */
306 { NULL
, 0, 0 }, /* %Q */
307 { NULL
, 0, 0 }, /* %U */
308 { NULL
, 0, 0 }, /* Ta */
310 static const struct mdoc_handler
*const mdocs
= __mdocs
- MDOC_Dd
;
314 mandocdb(int argc
, char *argv
[])
319 const char *path_arg
, *progname
;
324 if (pledge("stdio rpath wpath cpath", NULL
) == -1) {
326 return (int)MANDOCLEVEL_SYSERR
;
330 #if HAVE_SANDBOX_INIT
331 if (sandbox_init(kSBXProfileNoInternet
, SANDBOX_NAMED
, NULL
) == -1) {
332 warnx("sandbox_init");
333 return (int)MANDOCLEVEL_SYSERR
;
337 memset(&conf
, 0, sizeof(conf
));
340 * We accept a few different invocations.
341 * The CHECKOP macro makes sure that invocation styles don't
342 * clobber each other.
344 #define CHECKOP(_op, _ch) do \
345 if (OP_DEFAULT != (_op)) { \
346 warnx("-%c: Conflicting option", (_ch)); \
348 } while (/*CONSTCOND*/0)
353 while (-1 != (ch
= getopt(argc
, argv
, "aC:Dd:npQT:tu:v")))
378 mparse_options
|= MPARSE_QUICK
;
381 if (strcmp(optarg
, "utf8")) {
382 warnx("-T%s: Unsupported output format",
390 dup2(STDOUT_FILENO
, STDERR_FILENO
);
400 /* Compatibility with espie@'s makewhatis. */
411 if (pledge("stdio rpath", NULL
) == -1) {
413 return (int)MANDOCLEVEL_SYSERR
;
418 if (OP_CONFFILE
== op
&& argc
> 0) {
419 warnx("-C: Too many arguments");
423 exitcode
= (int)MANDOCLEVEL_OK
;
425 mp
= mparse_alloc(mparse_options
, MANDOCERR_MAX
, NULL
,
426 MANDOC_OS_OTHER
, NULL
);
427 mandoc_ohash_init(&mpages
, 6, offsetof(struct mpage
, inodev
));
428 mandoc_ohash_init(&mlinks
, 6, offsetof(struct mlink
, file
));
430 if (OP_UPDATE
== op
|| OP_DELETE
== op
|| OP_TEST
== op
) {
433 * Most of these deal with a specific directory.
434 * Jump into that directory first.
436 if (OP_TEST
!= op
&& 0 == set_basedir(path_arg
, 1))
439 dba
= nodb
? dba_new(128) : dba_read(MANDOC_DB
);
442 * The existing database is usable. Process
443 * all files specified on the command-line.
446 for (i
= 0; i
< argc
; i
++)
451 /* Database missing or corrupt. */
452 if (op
!= OP_UPDATE
|| errno
!= ENOENT
)
453 say(MANDOC_DB
, "%s: Automatically recreating"
454 " from scratch", strerror(errno
));
455 exitcode
= (int)MANDOCLEVEL_OK
;
462 mpages_merge(dba
, mp
);
468 * If we have arguments, use them as our manpaths.
469 * If we don't, use man.conf(5).
472 conf
.manpath
.paths
= mandoc_reallocarray(NULL
,
473 argc
, sizeof(char *));
474 conf
.manpath
.sz
= (size_t)argc
;
475 for (i
= 0; i
< argc
; i
++)
476 conf
.manpath
.paths
[i
] = mandoc_strdup(argv
[i
]);
478 manconf_parse(&conf
, path_arg
, NULL
, NULL
);
480 if (conf
.manpath
.sz
== 0) {
481 exitcode
= (int)MANDOCLEVEL_BADARG
;
482 say("", "Empty manpath");
486 * First scan the tree rooted at a base directory, then
487 * build a new database and finally move it into place.
488 * Ignore zero-length directories and strip trailing
491 for (j
= 0; j
< conf
.manpath
.sz
; j
++) {
492 sz
= strlen(conf
.manpath
.paths
[j
]);
493 if (sz
&& conf
.manpath
.paths
[j
][sz
- 1] == '/')
494 conf
.manpath
.paths
[j
][--sz
] = '\0';
499 mandoc_ohash_init(&mpages
, 6,
500 offsetof(struct mpage
, inodev
));
501 mandoc_ohash_init(&mlinks
, 6,
502 offsetof(struct mlink
, file
));
505 if ( ! set_basedir(conf
.manpath
.paths
[j
], argc
> 0))
510 mpages_merge(dba
, mp
);
515 if (j
+ 1 < conf
.manpath
.sz
) {
517 ohash_delete(&mpages
);
518 ohash_delete(&mlinks
);
527 ohash_delete(&mpages
);
528 ohash_delete(&mlinks
);
531 progname
= getprogname();
532 fprintf(stderr
, "usage: %s [-aDnpQ] [-C file] [-Tutf8]\n"
533 " %s [-aDnpQ] [-Tutf8] dir ...\n"
534 " %s [-DnpQ] [-Tutf8] -d dir [file ...]\n"
535 " %s [-Dnp] -u dir [file ...]\n"
536 " %s [-Q] -t file ...\n",
537 progname
, progname
, progname
, progname
, progname
);
539 return (int)MANDOCLEVEL_BADARG
;
543 * To get a singly linked list in alpha order while inserting entries
544 * at the beginning, process directory entries in reverse alpha order.
547 #if HAVE_FTS_COMPARE_CONST
548 fts_compare(const FTSENT
*const *a
, const FTSENT
*const *b
)
550 fts_compare(const FTSENT
**a
, const FTSENT
**b
)
553 return -strcmp((*a
)->fts_name
, (*b
)->fts_name
);
557 * Scan a directory tree rooted at "basedir" for manpages.
558 * We use fts(), scanning directory parts along the way for clues to our
559 * section and architecture.
561 * If use_all has been specified, grok all files.
562 * If not, sanitise paths to the following:
564 * [./]man*[/<arch>]/<name>.<section>
566 * [./]cat<section>[/<arch>]/<name>.0
568 * TODO: accommodate for multi-language directories.
579 char *dsec
, *arch
, *fsec
, *cp
;
586 f
= fts_open((char * const *)argv
, FTS_PHYSICAL
| FTS_NOCHDIR
,
589 exitcode
= (int)MANDOCLEVEL_SYSERR
;
590 say("", "&fts_open");
597 while ((ff
= fts_read(f
)) != NULL
) {
598 path
= ff
->fts_path
+ 2;
599 switch (ff
->fts_info
) {
602 * Symbolic links require various sanity checks,
603 * then get handled just like regular files.
606 if (realpath(path
, buf
) == NULL
) {
608 say(path
, "&realpath");
611 if (strstr(buf
, basedir
) != buf
613 && strstr(buf
, HOMEBREWDIR
) != buf
616 if (warnings
) say("",
617 "%s: outside base directory", buf
);
620 /* Use logical inode to avoid mpages dupe. */
621 if (stat(path
, ff
->fts_statp
) == -1) {
629 * If we're a regular file, add an mlink by using the
630 * stored directory data and handling the filename.
633 if ( ! strcmp(path
, MANDOC_DB
))
635 if ( ! use_all
&& ff
->fts_level
< 2) {
637 say(path
, "Extraneous file");
642 while (fsec
== NULL
) {
643 fsec
= strrchr(ff
->fts_name
, '.');
644 if (fsec
== NULL
|| strcmp(fsec
+1, "gz"))
654 "No filename suffix");
657 } else if ( ! strcmp(++fsec
, "html")) {
659 say(path
, "Skip html");
661 } else if ( ! strcmp(fsec
, "ps")) {
663 say(path
, "Skip ps");
665 } else if ( ! strcmp(fsec
, "pdf")) {
667 say(path
, "Skip pdf");
669 } else if ( ! use_all
&&
670 ((dform
== FORM_SRC
&&
671 strncmp(fsec
, dsec
, strlen(dsec
))) ||
672 (dform
== FORM_CAT
&& strcmp(fsec
, "0")))) {
674 say(path
, "Wrong filename suffix");
679 mlink
= mandoc_calloc(1, sizeof(struct mlink
));
680 if (strlcpy(mlink
->file
, path
,
681 sizeof(mlink
->file
)) >=
682 sizeof(mlink
->file
)) {
683 say(path
, "Filename too long");
687 mlink
->dform
= dform
;
690 mlink
->name
= ff
->fts_name
;
693 mlink_add(mlink
, ff
->fts_statp
);
702 say(path
, "Not a regular file");
706 switch (ff
->fts_level
) {
708 /* Ignore the root directory. */
712 * This might contain manX/ or catX/.
713 * Try to infer this from the name.
714 * If we're not in use_all, enforce it.
717 if (ff
->fts_info
== FTS_DP
) {
723 if ( ! strncmp(cp
, "man", 3)) {
726 } else if ( ! strncmp(cp
, "cat", 3)) {
734 if (dsec
!= NULL
|| use_all
)
738 say(path
, "Unknown directory part");
739 fts_set(f
, ff
, FTS_SKIP
);
743 * Possibly our architecture.
744 * If we're descending, keep tabs on it.
746 if (ff
->fts_info
!= FTS_DP
&& dsec
!= NULL
)
752 if (ff
->fts_info
== FTS_DP
|| use_all
)
755 say(path
, "Extraneous directory part");
756 fts_set(f
, ff
, FTS_SKIP
);
766 * Add a file to the mlinks table.
767 * Do not verify that it's a "valid" looking manpage (we'll do that
770 * Try to infer the manual section, architecture, and page name from the
771 * path, assuming it looks like
773 * [./]man*[/<arch>]/<name>.<section>
775 * [./]cat<section>[/<arch>]/<name>.0
777 * See treescan() for the fts(3) version of this.
780 filescan(const char *file
)
789 if (0 == strncmp(file
, "./", 2))
793 * We have to do lstat(2) before realpath(3) loses
794 * the information whether this is a symbolic link.
795 * We need to know that because for symbolic links,
796 * we want to use the orginal file name, while for
797 * regular files, we want to use the real path.
799 if (-1 == lstat(file
, &st
)) {
800 exitcode
= (int)MANDOCLEVEL_BADARG
;
803 } else if (0 == ((S_IFREG
| S_IFLNK
) & st
.st_mode
)) {
804 exitcode
= (int)MANDOCLEVEL_BADARG
;
805 say(file
, "Not a regular file");
810 * We have to resolve the file name to the real path
811 * in any case for the base directory check.
813 if (NULL
== realpath(file
, buf
)) {
814 exitcode
= (int)MANDOCLEVEL_BADARG
;
815 say(file
, "&realpath");
821 else if (strstr(buf
, basedir
) == buf
)
822 start
= buf
+ strlen(basedir
);
824 else if (strstr(buf
, HOMEBREWDIR
) == buf
)
828 exitcode
= (int)MANDOCLEVEL_BADARG
;
829 say("", "%s: outside base directory", buf
);
834 * Now we are sure the file is inside our tree.
835 * If it is a symbolic link, ignore the real path
836 * and use the original name.
837 * This implies passing stuff like "cat1/../man1/foo.1"
838 * on the command line won't work. So don't do that.
839 * Note the stat(2) can still fail if the link target
842 if (S_IFLNK
& st
.st_mode
) {
843 if (-1 == stat(buf
, &st
)) {
844 exitcode
= (int)MANDOCLEVEL_BADARG
;
848 if (strlcpy(buf
, file
, sizeof(buf
)) >= sizeof(buf
)) {
849 say(file
, "Filename too long");
853 if (OP_TEST
!= op
&& strstr(buf
, basedir
) == buf
)
854 start
+= strlen(basedir
);
857 mlink
= mandoc_calloc(1, sizeof(struct mlink
));
858 mlink
->dform
= FORM_NONE
;
859 if (strlcpy(mlink
->file
, start
, sizeof(mlink
->file
)) >=
860 sizeof(mlink
->file
)) {
861 say(start
, "Filename too long");
867 * In test mode or when the original name is absolute
868 * but outside our tree, guess the base directory.
871 if (op
== OP_TEST
|| (start
== buf
&& *start
== '/')) {
872 if (strncmp(buf
, "man/", 4) == 0)
874 else if ((start
= strstr(buf
, "/man/")) != NULL
)
881 * First try to guess our directory structure.
882 * If we find a separator, try to look for man* or cat*.
883 * If we find one of these and what's underneath is a directory,
884 * assume it's an architecture.
886 if (NULL
!= (p
= strchr(start
, '/'))) {
888 if (0 == strncmp(start
, "man", 3)) {
889 mlink
->dform
= FORM_SRC
;
890 mlink
->dsec
= start
+ 3;
891 } else if (0 == strncmp(start
, "cat", 3)) {
892 mlink
->dform
= FORM_CAT
;
893 mlink
->dsec
= start
+ 3;
897 if (NULL
!= mlink
->dsec
&& NULL
!= (p
= strchr(start
, '/'))) {
905 * Now check the file suffix.
906 * Suffix of `.0' indicates a catpage, `.1-9' is a manpage.
908 p
= strrchr(start
, '\0');
909 while (p
-- > start
&& '/' != *p
&& '.' != *p
)
918 * Now try to parse the name.
919 * Use the filename portion of the path.
922 if (NULL
!= (p
= strrchr(start
, '/'))) {
926 mlink_add(mlink
, &st
);
930 mlink_add(struct mlink
*mlink
, const struct stat
*st
)
932 struct inodev inodev
;
936 assert(NULL
!= mlink
->file
);
938 mlink
->dsec
= mandoc_strdup(mlink
->dsec
? mlink
->dsec
: "");
939 mlink
->arch
= mandoc_strdup(mlink
->arch
? mlink
->arch
: "");
940 mlink
->name
= mandoc_strdup(mlink
->name
? mlink
->name
: "");
941 mlink
->fsec
= mandoc_strdup(mlink
->fsec
? mlink
->fsec
: "");
943 if ('0' == *mlink
->fsec
) {
945 mlink
->fsec
= mandoc_strdup(mlink
->dsec
);
946 mlink
->fform
= FORM_CAT
;
947 } else if ('1' <= *mlink
->fsec
&& '9' >= *mlink
->fsec
)
948 mlink
->fform
= FORM_SRC
;
950 mlink
->fform
= FORM_NONE
;
952 slot
= ohash_qlookup(&mlinks
, mlink
->file
);
953 assert(NULL
== ohash_find(&mlinks
, slot
));
954 ohash_insert(&mlinks
, slot
, mlink
);
956 memset(&inodev
, 0, sizeof(inodev
)); /* Clear padding. */
957 inodev
.st_ino
= st
->st_ino
;
958 inodev
.st_dev
= st
->st_dev
;
959 slot
= ohash_lookup_memory(&mpages
, (char *)&inodev
,
960 sizeof(struct inodev
), inodev
.st_ino
);
961 mpage
= ohash_find(&mpages
, slot
);
963 mpage
= mandoc_calloc(1, sizeof(struct mpage
));
964 mpage
->inodev
.st_ino
= inodev
.st_ino
;
965 mpage
->inodev
.st_dev
= inodev
.st_dev
;
966 mpage
->form
= FORM_NONE
;
967 mpage
->next
= mpage_head
;
969 ohash_insert(&mpages
, slot
, mpage
);
971 mlink
->next
= mpage
->mlinks
;
972 mpage
->mlinks
= mlink
;
973 mlink
->mpage
= mpage
;
977 mlink_free(struct mlink
*mlink
)
993 while ((mpage
= mpage_head
) != NULL
) {
994 while ((mlink
= mpage
->mlinks
) != NULL
) {
995 mpage
->mlinks
= mlink
->next
;
998 mpage_head
= mpage
->next
;
1008 * For each mlink to the mpage, check whether the path looks like
1009 * it is formatted, and if it does, check whether a source manual
1010 * exists by the same name, ignoring the suffix.
1011 * If both conditions hold, drop the mlink.
1014 mlinks_undupe(struct mpage
*mpage
)
1017 struct mlink
**prev
;
1018 struct mlink
*mlink
;
1021 mpage
->form
= FORM_CAT
;
1022 prev
= &mpage
->mlinks
;
1023 while (NULL
!= (mlink
= *prev
)) {
1024 if (FORM_CAT
!= mlink
->dform
) {
1025 mpage
->form
= FORM_NONE
;
1028 (void)strlcpy(buf
, mlink
->file
, sizeof(buf
));
1029 bufp
= strstr(buf
, "cat");
1030 assert(NULL
!= bufp
);
1031 memcpy(bufp
, "man", 3);
1032 if (NULL
!= (bufp
= strrchr(buf
, '.')))
1034 (void)strlcat(buf
, mlink
->dsec
, sizeof(buf
));
1035 if (NULL
== ohash_find(&mlinks
,
1036 ohash_qlookup(&mlinks
, buf
)))
1039 say(mlink
->file
, "Man source exists: %s", buf
);
1042 *prev
= mlink
->next
;
1046 prev
= &(*prev
)->next
;
1051 mlink_check(struct mpage
*mpage
, struct mlink
*mlink
)
1057 * Check whether the manual section given in a file
1058 * agrees with the directory where the file is located.
1059 * Some manuals have suffixes like (3p) on their
1060 * section number either inside the file or in the
1061 * directory name, some are linked into more than one
1062 * section, like encrypt(1) = makekey(8).
1065 if (FORM_SRC
== mpage
->form
&&
1066 strcasecmp(mpage
->sec
, mlink
->dsec
))
1067 say(mlink
->file
, "Section \"%s\" manual in %s directory",
1068 mpage
->sec
, mlink
->dsec
);
1071 * Manual page directories exist for each kernel
1072 * architecture as returned by machine(1).
1073 * However, many manuals only depend on the
1074 * application architecture as returned by arch(1).
1075 * For example, some (2/ARM) manuals are shared
1076 * across the "armish" and "zaurus" kernel
1078 * A few manuals are even shared across completely
1079 * different architectures, for example fdformat(1)
1080 * on amd64, i386, and sparc64.
1083 if (strcasecmp(mpage
->arch
, mlink
->arch
))
1084 say(mlink
->file
, "Architecture \"%s\" manual in "
1085 "\"%s\" directory", mpage
->arch
, mlink
->arch
);
1089 * parse_cat() doesn't set NAME_TITLE yet.
1092 if (FORM_CAT
== mpage
->form
)
1096 * Check whether this mlink
1097 * appears as a name in the NAME section.
1100 slot
= ohash_qlookup(&names
, mlink
->name
);
1101 str
= ohash_find(&names
, slot
);
1102 assert(NULL
!= str
);
1103 if ( ! (NAME_TITLE
& str
->mask
))
1104 say(mlink
->file
, "Name missing in NAME section");
1108 * Run through the files in the global vector "mpages"
1109 * and add them to the database specified in "basedir".
1111 * This handles the parsing scheme itself, using the cues of directory
1112 * and filename to determine whether the file is parsable or not.
1115 mpages_merge(struct dba
*dba
, struct mparse
*mp
)
1117 struct mpage
*mpage
, *mpage_dest
;
1118 struct mlink
*mlink
, *mlink_dest
;
1119 struct roff_man
*man
;
1124 for (mpage
= mpage_head
; mpage
!= NULL
; mpage
= mpage
->next
) {
1125 mlinks_undupe(mpage
);
1126 if ((mlink
= mpage
->mlinks
) == NULL
)
1129 name_mask
= NAME_MASK
;
1130 mandoc_ohash_init(&names
, 4, offsetof(struct str
, key
));
1131 mandoc_ohash_init(&strings
, 6, offsetof(struct str
, key
));
1136 if ((fd
= mparse_open(mp
, mlink
->file
)) == -1) {
1137 say(mlink
->file
, "&open");
1142 * Interpret the file as mdoc(7) or man(7) source
1143 * code, unless it is known to be formatted.
1145 if (mlink
->dform
!= FORM_CAT
|| mlink
->fform
!= FORM_CAT
) {
1146 mparse_readfd(mp
, fd
, mlink
->file
);
1149 mparse_result(mp
, &man
, &sodest
);
1152 if (sodest
!= NULL
) {
1153 mlink_dest
= ohash_find(&mlinks
,
1154 ohash_qlookup(&mlinks
, sodest
));
1155 if (mlink_dest
== NULL
) {
1156 mandoc_asprintf(&cp
, "%s.gz", sodest
);
1157 mlink_dest
= ohash_find(&mlinks
,
1158 ohash_qlookup(&mlinks
, cp
));
1161 if (mlink_dest
!= NULL
) {
1163 /* The .so target exists. */
1165 mpage_dest
= mlink_dest
->mpage
;
1167 mlink
->mpage
= mpage_dest
;
1170 * If the target was already
1171 * processed, add the links
1172 * to the database now.
1173 * Otherwise, this will
1174 * happen when we come
1178 if (mpage_dest
->dba
!= NULL
)
1181 if (mlink
->next
== NULL
)
1183 mlink
= mlink
->next
;
1186 /* Move all links to the target. */
1188 mlink
->next
= mlink_dest
->next
;
1189 mlink_dest
->next
= mpage
->mlinks
;
1190 mpage
->mlinks
= NULL
;
1193 } else if (man
!= NULL
&& man
->macroset
== MACROSET_MDOC
) {
1195 mpage
->form
= FORM_SRC
;
1196 mpage
->sec
= man
->meta
.msec
;
1197 mpage
->sec
= mandoc_strdup(
1198 mpage
->sec
== NULL
? "" : mpage
->sec
);
1199 mpage
->arch
= man
->meta
.arch
;
1200 mpage
->arch
= mandoc_strdup(
1201 mpage
->arch
== NULL
? "" : mpage
->arch
);
1202 mpage
->title
= mandoc_strdup(man
->meta
.title
);
1203 } else if (man
!= NULL
&& man
->macroset
== MACROSET_MAN
) {
1205 if (*man
->meta
.msec
!= '\0' ||
1206 *man
->meta
.title
!= '\0') {
1207 mpage
->form
= FORM_SRC
;
1208 mpage
->sec
= mandoc_strdup(man
->meta
.msec
);
1209 mpage
->arch
= mandoc_strdup(mlink
->arch
);
1210 mpage
->title
= mandoc_strdup(man
->meta
.title
);
1215 assert(mpage
->desc
== NULL
);
1217 mpage
->form
= FORM_CAT
;
1218 mpage
->sec
= mandoc_strdup(mlink
->dsec
);
1219 mpage
->arch
= mandoc_strdup(mlink
->arch
);
1220 mpage
->title
= mandoc_strdup(mlink
->name
);
1221 parse_cat(mpage
, fd
);
1222 } else if (man
->macroset
== MACROSET_MDOC
)
1223 parse_mdoc(mpage
, &man
->meta
, man
->first
);
1225 parse_man(mpage
, &man
->meta
, man
->first
);
1226 if (mpage
->desc
== NULL
) {
1227 mpage
->desc
= mandoc_strdup(mlink
->name
);
1229 say(mlink
->file
, "No one-line description, "
1230 "using filename \"%s\"", mlink
->name
);
1233 for (mlink
= mpage
->mlinks
;
1235 mlink
= mlink
->next
) {
1236 putkey(mpage
, mlink
->name
, NAME_FILE
);
1237 if (warnings
&& !use_all
)
1238 mlink_check(mpage
, mlink
);
1244 ohash_delete(&strings
);
1245 ohash_delete(&names
);
1250 parse_cat(struct mpage
*mpage
, int fd
)
1253 struct mlink
*mlink
;
1254 char *line
, *p
, *title
, *sec
;
1255 size_t linesz
, plen
, titlesz
;
1259 mlink
= mpage
->mlinks
;
1260 stream
= fd
== -1 ? fopen(mlink
->file
, "r") : fdopen(fd
, "r");
1261 if (stream
== NULL
) {
1265 say(mlink
->file
, "&fopen");
1272 /* Parse the section number from the header line. */
1274 while (getline(&line
, &linesz
, stream
) != -1) {
1277 if ((sec
= strchr(line
, '(')) == NULL
)
1279 if ((p
= strchr(++sec
, ')')) == NULL
)
1282 mpage
->sec
= mandoc_strndup(sec
, p
- sec
);
1283 if (warnings
&& *mlink
->dsec
!= '\0' &&
1284 strcasecmp(mpage
->sec
, mlink
->dsec
))
1286 "Section \"%s\" manual in %s directory",
1287 mpage
->sec
, mlink
->dsec
);
1291 /* Skip to first blank line. */
1293 while (line
== NULL
|| *line
!= '\n')
1294 if (getline(&line
, &linesz
, stream
) == -1)
1298 * Assume the first line that is not indented
1299 * is the first section header. Skip to it.
1302 while (getline(&line
, &linesz
, stream
) != -1)
1303 if (*line
!= '\n' && *line
!= ' ')
1307 * Read up until the next section into a buffer.
1308 * Strip the leading and trailing newline from each read line,
1309 * appending a trailing space.
1310 * Ignore empty (whitespace-only) lines.
1316 while ((len
= getline(&line
, &linesz
, stream
)) != -1) {
1320 while (isspace((unsigned char)line
[offs
]))
1322 if (line
[offs
] == '\0')
1324 title
= mandoc_realloc(title
, titlesz
+ len
- offs
);
1325 memcpy(title
+ titlesz
, line
+ offs
, len
- offs
);
1326 titlesz
+= len
- offs
;
1327 title
[titlesz
- 1] = ' ';
1332 * If no page content can be found, or the input line
1333 * is already the next section header, or there is no
1334 * trailing newline, reuse the page title as the page
1338 if (NULL
== title
|| '\0' == *title
) {
1340 say(mlink
->file
, "Cannot find NAME section");
1346 title
[titlesz
- 1] = '\0';
1349 * Skip to the first dash.
1350 * Use the remaining line as the description (no more than 70
1354 if (NULL
!= (p
= strstr(title
, "- "))) {
1355 for (p
+= 2; ' ' == *p
|| '\b' == *p
; p
++)
1356 /* Skip to next word. */ ;
1359 say(mlink
->file
, "No dash in title line, "
1360 "reusing \"%s\" as one-line description", title
);
1366 /* Strip backspace-encoding from line. */
1368 while (NULL
!= (line
= memchr(p
, '\b', plen
))) {
1371 memmove(line
, line
+ 1, plen
--);
1374 memmove(line
- 1, line
+ 1, plen
- len
);
1379 * Cut off excessive one-line descriptions.
1380 * Bad pages are not worth better heuristics.
1383 mpage
->desc
= mandoc_strndup(p
, 150);
1389 * Put a type/word pair into the word database for this particular file.
1392 putkey(const struct mpage
*mpage
, char *value
, uint64_t type
)
1394 putkeys(mpage
, value
, strlen(value
), type
);
1398 * Grok all nodes at or below a certain mdoc node into putkey().
1401 putmdockey(const struct mpage
*mpage
,
1402 const struct roff_node
*n
, uint64_t m
, int taboo
)
1405 for ( ; NULL
!= n
; n
= n
->next
) {
1406 if (n
->flags
& taboo
)
1408 if (NULL
!= n
->child
)
1409 putmdockey(mpage
, n
->child
, m
, taboo
);
1410 if (n
->type
== ROFFT_TEXT
)
1411 putkey(mpage
, n
->string
, m
);
1416 parse_man(struct mpage
*mpage
, const struct roff_meta
*meta
,
1417 const struct roff_node
*n
)
1419 const struct roff_node
*head
, *body
;
1420 char *start
, *title
;
1428 * We're only searching for one thing: the first text child in
1429 * the BODY of a NAME section. Since we don't keep track of
1430 * sections in -man, run some hoops to find out whether we're in
1431 * the correct section or not.
1434 if (n
->type
== ROFFT_BODY
&& n
->tok
== MAN_SH
) {
1436 if ((head
= body
->parent
->head
) != NULL
&&
1437 (head
= head
->child
) != NULL
&&
1438 head
->next
== NULL
&&
1439 head
->type
== ROFFT_TEXT
&&
1440 strcmp(head
->string
, "NAME") == 0 &&
1441 body
->child
!= NULL
) {
1444 * Suck the entire NAME section into memory.
1445 * Yes, we might run away.
1446 * But too many manuals have big, spread-out
1447 * NAME sections over many lines.
1451 deroff(&title
, body
);
1456 * Go through a special heuristic dance here.
1457 * Conventionally, one or more manual names are
1458 * comma-specified prior to a whitespace, then a
1459 * dash, then a description. Try to puzzle out
1460 * the name parts here.
1465 sz
= strcspn(start
, " ,");
1466 if ('\0' == start
[sz
])
1473 * Assume a stray trailing comma in the
1474 * name list if a name begins with a dash.
1477 if ('-' == start
[0] ||
1478 ('\\' == start
[0] && '-' == start
[1]))
1481 putkey(mpage
, start
, NAME_TITLE
);
1482 if ( ! (mpage
->name_head_done
||
1483 strcasecmp(start
, meta
->title
))) {
1484 putkey(mpage
, start
, NAME_HEAD
);
1485 mpage
->name_head_done
= 1;
1493 assert(',' == byte
);
1495 while (' ' == *start
)
1499 if (start
== title
) {
1500 putkey(mpage
, start
, NAME_TITLE
);
1501 if ( ! (mpage
->name_head_done
||
1502 strcasecmp(start
, meta
->title
))) {
1503 putkey(mpage
, start
, NAME_HEAD
);
1504 mpage
->name_head_done
= 1;
1510 while (isspace((unsigned char)*start
))
1513 if (0 == strncmp(start
, "-", 1))
1515 else if (0 == strncmp(start
, "\\-\\-", 4))
1517 else if (0 == strncmp(start
, "\\-", 2))
1519 else if (0 == strncmp(start
, "\\(en", 4))
1521 else if (0 == strncmp(start
, "\\(em", 4))
1524 while (' ' == *start
)
1528 * Cut off excessive one-line descriptions.
1529 * Bad pages are not worth better heuristics.
1532 mpage
->desc
= mandoc_strndup(start
, 150);
1538 for (n
= n
->child
; n
; n
= n
->next
) {
1539 if (NULL
!= mpage
->desc
)
1541 parse_man(mpage
, meta
, n
);
1546 parse_mdoc(struct mpage
*mpage
, const struct roff_meta
*meta
,
1547 const struct roff_node
*n
)
1550 for (n
= n
->child
; n
!= NULL
; n
= n
->next
) {
1551 if (n
->tok
== TOKEN_NONE
||
1552 n
->tok
< ROFF_MAX
||
1553 n
->flags
& mdocs
[n
->tok
].taboo
)
1555 assert(n
->tok
>= MDOC_Dd
&& n
->tok
< MDOC_MAX
);
1562 if (mdocs
[n
->tok
].fp
!= NULL
&&
1563 (*mdocs
[n
->tok
].fp
)(mpage
, meta
, n
) == 0)
1565 if (mdocs
[n
->tok
].mask
)
1566 putmdockey(mpage
, n
->child
,
1567 mdocs
[n
->tok
].mask
, mdocs
[n
->tok
].taboo
);
1572 if (NULL
!= n
->child
)
1573 parse_mdoc(mpage
, meta
, n
);
1578 parse_mdoc_Fa(struct mpage
*mpage
, const struct roff_meta
*meta
,
1579 const struct roff_node
*n
)
1584 if (n
->sec
== SEC_SYNOPSIS
)
1587 putmdockey(mpage
, n
->child
, mask
, 0);
1592 parse_mdoc_Fd(struct mpage
*mpage
, const struct roff_meta
*meta
,
1593 const struct roff_node
*n
)
1598 if (SEC_SYNOPSIS
!= n
->sec
||
1599 NULL
== (n
= n
->child
) ||
1600 n
->type
!= ROFFT_TEXT
)
1604 * Only consider those `Fd' macro fields that begin with an
1605 * "inclusion" token (versus, e.g., #define).
1608 if (strcmp("#include", n
->string
))
1611 if ((n
= n
->next
) == NULL
|| n
->type
!= ROFFT_TEXT
)
1615 * Strip away the enclosing angle brackets and make sure we're
1620 if ('<' == *start
|| '"' == *start
)
1623 if (0 == (sz
= strlen(start
)))
1626 end
= &start
[(int)sz
- 1];
1627 if ('>' == *end
|| '"' == *end
)
1631 putkeys(mpage
, start
, end
- start
+ 1, TYPE_In
);
1636 parse_mdoc_fname(struct mpage
*mpage
, const struct roff_node
*n
)
1641 if (n
->type
!= ROFFT_TEXT
)
1644 /* Skip function pointer punctuation. */
1647 while (*cp
== '(' || *cp
== '*')
1649 sz
= strcspn(cp
, "()");
1651 putkeys(mpage
, cp
, sz
, TYPE_Fn
);
1652 if (n
->sec
== SEC_SYNOPSIS
)
1653 putkeys(mpage
, cp
, sz
, NAME_SYN
);
1657 parse_mdoc_Fn(struct mpage
*mpage
, const struct roff_meta
*meta
,
1658 const struct roff_node
*n
)
1662 if (n
->child
== NULL
)
1665 parse_mdoc_fname(mpage
, n
->child
);
1668 if (n
!= NULL
&& n
->type
== ROFFT_TEXT
) {
1670 if (n
->sec
== SEC_SYNOPSIS
)
1672 putmdockey(mpage
, n
, mask
, 0);
1679 parse_mdoc_Fo(struct mpage
*mpage
, const struct roff_meta
*meta
,
1680 const struct roff_node
*n
)
1683 if (n
->type
!= ROFFT_HEAD
)
1686 if (n
->child
!= NULL
)
1687 parse_mdoc_fname(mpage
, n
->child
);
1693 parse_mdoc_Va(struct mpage
*mpage
, const struct roff_meta
*meta
,
1694 const struct roff_node
*n
)
1698 if (n
->type
!= ROFFT_ELEM
&& n
->type
!= ROFFT_BODY
)
1701 if (n
->child
!= NULL
&&
1702 n
->child
->next
== NULL
&&
1703 n
->child
->type
== ROFFT_TEXT
)
1709 putkey(mpage
, cp
, TYPE_Vt
| (n
->tok
== MDOC_Va
||
1710 n
->type
== ROFFT_BODY
? TYPE_Va
: 0));
1718 parse_mdoc_Xr(struct mpage
*mpage
, const struct roff_meta
*meta
,
1719 const struct roff_node
*n
)
1723 if (NULL
== (n
= n
->child
))
1726 if (NULL
== n
->next
) {
1727 putkey(mpage
, n
->string
, TYPE_Xr
);
1731 mandoc_asprintf(&cp
, "%s(%s)", n
->string
, n
->next
->string
);
1732 putkey(mpage
, cp
, TYPE_Xr
);
1738 parse_mdoc_Nd(struct mpage
*mpage
, const struct roff_meta
*meta
,
1739 const struct roff_node
*n
)
1742 if (n
->type
== ROFFT_BODY
)
1743 deroff(&mpage
->desc
, n
);
1748 parse_mdoc_Nm(struct mpage
*mpage
, const struct roff_meta
*meta
,
1749 const struct roff_node
*n
)
1752 if (SEC_NAME
== n
->sec
)
1753 putmdockey(mpage
, n
->child
, NAME_TITLE
, 0);
1754 else if (n
->sec
== SEC_SYNOPSIS
&& n
->type
== ROFFT_HEAD
) {
1755 if (n
->child
== NULL
)
1756 putkey(mpage
, meta
->name
, NAME_SYN
);
1758 putmdockey(mpage
, n
->child
, NAME_SYN
, 0);
1760 if ( ! (mpage
->name_head_done
||
1761 n
->child
== NULL
|| n
->child
->string
== NULL
||
1762 strcasecmp(n
->child
->string
, meta
->title
))) {
1763 putkey(mpage
, n
->child
->string
, NAME_HEAD
);
1764 mpage
->name_head_done
= 1;
1770 parse_mdoc_Sh(struct mpage
*mpage
, const struct roff_meta
*meta
,
1771 const struct roff_node
*n
)
1774 return n
->sec
== SEC_CUSTOM
&& n
->type
== ROFFT_HEAD
;
1778 parse_mdoc_head(struct mpage
*mpage
, const struct roff_meta
*meta
,
1779 const struct roff_node
*n
)
1782 return n
->type
== ROFFT_HEAD
;
1786 * Add a string to the hash table for the current manual.
1787 * Each string has a bitmask telling which macros it belongs to.
1788 * When we finish the manual, we'll dump the table.
1791 putkeys(const struct mpage
*mpage
, char *cp
, size_t sz
, uint64_t v
)
1802 mustfree
= render_string(&cp
, &sz
);
1808 name_mask
&= ~NAME_FIRST
;
1810 say(mpage
->mlinks
->file
,
1811 "Adding name %*s, bits=0x%llx", (int)sz
, cp
,
1812 (unsigned long long)v
);
1816 for (i
= 0; i
< KEY_MAX
; i
++)
1817 if ((uint64_t)1 << i
& v
)
1818 say(mpage
->mlinks
->file
,
1819 "Adding key %s=%*s",
1820 mansearch_keynames
[i
], (int)sz
, cp
);
1824 slot
= ohash_qlookupi(htab
, cp
, &end
);
1825 s
= ohash_find(htab
, slot
);
1827 if (NULL
!= s
&& mpage
== s
->mpage
) {
1830 } else if (NULL
== s
) {
1831 s
= mandoc_calloc(1, sizeof(struct str
) + sz
+ 1);
1832 memcpy(s
->key
, cp
, sz
);
1833 ohash_insert(htab
, slot
, s
);
1843 * Take a Unicode codepoint and produce its UTF-8 encoding.
1844 * This isn't the best way to do this, but it works.
1845 * The magic numbers are from the UTF-8 packaging.
1846 * They're not as scary as they seem: read the UTF-8 spec for details.
1849 utf8(unsigned int cp
, char out
[7])
1854 if (cp
<= 0x0000007F) {
1857 } else if (cp
<= 0x000007FF) {
1859 out
[0] = (cp
>> 6 & 31) | 192;
1860 out
[1] = (cp
& 63) | 128;
1861 } else if (cp
<= 0x0000FFFF) {
1863 out
[0] = (cp
>> 12 & 15) | 224;
1864 out
[1] = (cp
>> 6 & 63) | 128;
1865 out
[2] = (cp
& 63) | 128;
1866 } else if (cp
<= 0x001FFFFF) {
1868 out
[0] = (cp
>> 18 & 7) | 240;
1869 out
[1] = (cp
>> 12 & 63) | 128;
1870 out
[2] = (cp
>> 6 & 63) | 128;
1871 out
[3] = (cp
& 63) | 128;
1872 } else if (cp
<= 0x03FFFFFF) {
1874 out
[0] = (cp
>> 24 & 3) | 248;
1875 out
[1] = (cp
>> 18 & 63) | 128;
1876 out
[2] = (cp
>> 12 & 63) | 128;
1877 out
[3] = (cp
>> 6 & 63) | 128;
1878 out
[4] = (cp
& 63) | 128;
1879 } else if (cp
<= 0x7FFFFFFF) {
1881 out
[0] = (cp
>> 30 & 1) | 252;
1882 out
[1] = (cp
>> 24 & 63) | 128;
1883 out
[2] = (cp
>> 18 & 63) | 128;
1884 out
[3] = (cp
>> 12 & 63) | 128;
1885 out
[4] = (cp
>> 6 & 63) | 128;
1886 out
[5] = (cp
& 63) | 128;
1895 * If the string contains escape sequences,
1896 * replace it with an allocated rendering and return 1,
1897 * such that the caller can free it after use.
1898 * Otherwise, do nothing and return 0.
1901 render_string(char **public, size_t *psz
)
1903 const char *src
, *scp
, *addcp
, *seq
;
1905 size_t ssz
, dsz
, addsz
;
1906 char utfbuf
[7], res
[6];
1907 int seqlen
, unicode
;
1911 res
[2] = ASCII_NBRSP
;
1912 res
[3] = ASCII_HYPH
;
1913 res
[4] = ASCII_BREAK
;
1916 src
= scp
= *public;
1921 while (scp
< src
+ *psz
) {
1923 /* Leave normal characters unchanged. */
1925 if (strchr(res
, *scp
) == NULL
) {
1933 * Found something that requires replacing,
1934 * make sure we have a destination buffer.
1938 dst
= mandoc_malloc(ssz
+ 1);
1940 memcpy(dst
, src
, dsz
);
1943 /* Handle single-char special characters. */
1964 * Found an escape sequence.
1965 * Read past the slash, then parse it.
1966 * Ignore everything except characters.
1970 if (mandoc_escape(&scp
, &seq
, &seqlen
) != ESCAPE_SPECIAL
)
1974 * Render the special character
1975 * as either UTF-8 or ASCII.
1979 unicode
= mchars_spec2cp(seq
, seqlen
);
1982 addsz
= utf8(unicode
, utfbuf
);
1987 addcp
= mchars_spec2str(seq
, seqlen
, &addsz
);
1990 if (*addcp
== ASCII_NBRSP
) {
1996 /* Copy the rendered glyph into the stream. */
1999 dst
= mandoc_realloc(dst
, ssz
+ 1);
2000 memcpy(dst
+ dsz
, addcp
, addsz
);
2008 /* Trim trailing whitespace and NUL-terminate. */
2010 while (*psz
> 0 && (*public)[*psz
- 1] == ' ')
2013 (*public)[*psz
] = '\0';
2020 dbadd_mlink(const struct mlink
*mlink
)
2022 dba_page_alias(mlink
->mpage
->dba
, mlink
->name
, NAME_FILE
);
2023 dba_page_add(mlink
->mpage
->dba
, DBP_SECT
, mlink
->dsec
);
2024 dba_page_add(mlink
->mpage
->dba
, DBP_SECT
, mlink
->fsec
);
2025 dba_page_add(mlink
->mpage
->dba
, DBP_ARCH
, mlink
->arch
);
2026 dba_page_add(mlink
->mpage
->dba
, DBP_FILE
, mlink
->file
);
2030 * Flush the current page's terms (and their bits) into the database.
2031 * Also, handle escape sequences at the last possible moment.
2034 dbadd(struct dba
*dba
, struct mpage
*mpage
)
2036 struct mlink
*mlink
;
2044 mlink
= mpage
->mlinks
;
2047 for (key
= ohash_first(&names
, &slot
); NULL
!= key
;
2048 key
= ohash_next(&names
, &slot
))
2050 for (key
= ohash_first(&strings
, &slot
); NULL
!= key
;
2051 key
= ohash_next(&strings
, &slot
))
2055 while (NULL
!= mlink
) {
2056 fputs(mlink
->name
, stdout
);
2057 if (NULL
== mlink
->next
||
2058 strcmp(mlink
->dsec
, mlink
->next
->dsec
) ||
2059 strcmp(mlink
->fsec
, mlink
->next
->fsec
) ||
2060 strcmp(mlink
->arch
, mlink
->next
->arch
)) {
2062 if ('\0' == *mlink
->dsec
)
2063 fputs(mlink
->fsec
, stdout
);
2065 fputs(mlink
->dsec
, stdout
);
2066 if ('\0' != *mlink
->arch
)
2067 printf("/%s", mlink
->arch
);
2070 mlink
= mlink
->next
;
2072 fputs(", ", stdout
);
2074 printf(" - %s\n", mpage
->desc
);
2079 say(mlink
->file
, "Adding to database");
2083 mustfree
= render_string(&cp
, &i
);
2084 mpage
->dba
= dba_page_new(dba
->pages
,
2085 *mpage
->arch
== '\0' ? mlink
->arch
: mpage
->arch
,
2086 cp
, mlink
->file
, mpage
->form
);
2089 dba_page_add(mpage
->dba
, DBP_SECT
, mpage
->sec
);
2091 while (mlink
!= NULL
) {
2093 mlink
= mlink
->next
;
2096 for (key
= ohash_first(&names
, &slot
); NULL
!= key
;
2097 key
= ohash_next(&names
, &slot
)) {
2098 assert(key
->mpage
== mpage
);
2099 dba_page_alias(mpage
->dba
, key
->key
, key
->mask
);
2102 for (key
= ohash_first(&strings
, &slot
); NULL
!= key
;
2103 key
= ohash_next(&strings
, &slot
)) {
2104 assert(key
->mpage
== mpage
);
2106 for (mask
= TYPE_Xr
; mask
<= TYPE_Lb
; mask
*= 2) {
2107 if (key
->mask
& mask
)
2108 dba_macro_add(dba
->macros
, i
,
2109 key
->key
, mpage
->dba
);
2117 dbprune(struct dba
*dba
)
2119 struct dba_array
*page
, *files
;
2122 dba_array_FOREACH(dba
->pages
, page
) {
2123 files
= dba_array_get(page
, DBP_FILE
);
2124 dba_array_FOREACH(files
, file
) {
2127 if (ohash_find(&mlinks
, ohash_qlookup(&mlinks
,
2130 say(file
, "Deleting from database");
2131 dba_array_del(dba
->pages
);
2139 * Write the database from memory to disk.
2142 dbwrite(struct dba
*dba
)
2144 struct stat sb1
, sb2
;
2145 char tfn
[33], *cp1
, *cp2
;
2150 * Do not write empty databases, and delete existing ones
2151 * when makewhatis -u causes them to become empty.
2154 dba_array_start(dba
->pages
);
2155 if (dba_array_next(dba
->pages
) == NULL
) {
2156 if (unlink(MANDOC_DB
) == -1 && errno
!= ENOENT
)
2157 say(MANDOC_DB
, "&unlink");
2162 * Build the database in a temporary file,
2163 * then atomically move it into place.
2166 if (dba_write(MANDOC_DB
"~", dba
) != -1) {
2167 if (rename(MANDOC_DB
"~", MANDOC_DB
) == -1) {
2168 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2169 say(MANDOC_DB
, "&rename");
2170 unlink(MANDOC_DB
"~");
2176 * We lack write permission and cannot replace the database
2177 * file, but let's at least check whether the data changed.
2180 (void)strlcpy(tfn
, "/tmp/mandocdb.XXXXXXXX", sizeof(tfn
));
2181 if (mkdtemp(tfn
) == NULL
) {
2182 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2183 say("", "&%s", tfn
);
2186 cp1
= cp2
= MAP_FAILED
;
2188 (void)strlcat(tfn
, "/" MANDOC_DB
, sizeof(tfn
));
2189 if (dba_write(tfn
, dba
) == -1) {
2190 say(tfn
, "&dba_write");
2193 if ((fd1
= open(MANDOC_DB
, O_RDONLY
, 0)) == -1) {
2194 say(MANDOC_DB
, "&open");
2197 if ((fd2
= open(tfn
, O_RDONLY
, 0)) == -1) {
2201 if (fstat(fd1
, &sb1
) == -1) {
2202 say(MANDOC_DB
, "&fstat");
2205 if (fstat(fd2
, &sb2
) == -1) {
2209 if (sb1
.st_size
!= sb2
.st_size
)
2211 if ((cp1
= mmap(NULL
, sb1
.st_size
, PROT_READ
, MAP_PRIVATE
,
2212 fd1
, 0)) == MAP_FAILED
) {
2213 say(MANDOC_DB
, "&mmap");
2216 if ((cp2
= mmap(NULL
, sb2
.st_size
, PROT_READ
, MAP_PRIVATE
,
2217 fd2
, 0)) == MAP_FAILED
) {
2221 for (i
= 0; i
< sb1
.st_size
; i
++)
2222 if (cp1
[i
] != cp2
[i
])
2227 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2228 say(MANDOC_DB
, "Data changed, but cannot replace database");
2231 if (cp1
!= MAP_FAILED
)
2232 munmap(cp1
, sb1
.st_size
);
2233 if (cp2
!= MAP_FAILED
)
2234 munmap(cp2
, sb2
.st_size
);
2240 *strrchr(tfn
, '/') = '\0';
2245 set_basedir(const char *targetdir
, int report_baddir
)
2247 static char startdir
[PATH_MAX
];
2248 static int getcwd_status
; /* 1 = ok, 2 = failure */
2249 static int chdir_status
; /* 1 = changed directory */
2253 * Remember the original working directory, if possible.
2254 * This will be needed if the second or a later directory
2255 * on the command line is given as a relative path.
2256 * Do not error out if the current directory is not
2257 * searchable: Maybe it won't be needed after all.
2259 if (0 == getcwd_status
) {
2260 if (NULL
== getcwd(startdir
, sizeof(startdir
))) {
2262 (void)strlcpy(startdir
, strerror(errno
),
2269 * We are leaving the old base directory.
2270 * Do not use it any longer, not even for messages.
2275 * If and only if the directory was changed earlier and
2276 * the next directory to process is given as a relative path,
2277 * first go back, or bail out if that is impossible.
2279 if (chdir_status
&& '/' != *targetdir
) {
2280 if (2 == getcwd_status
) {
2281 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2282 say("", "getcwd: %s", startdir
);
2285 if (-1 == chdir(startdir
)) {
2286 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2287 say("", "&chdir %s", startdir
);
2293 * Always resolve basedir to the canonicalized absolute
2294 * pathname and append a trailing slash, such that
2295 * we can reliably check whether files are inside.
2297 if (NULL
== realpath(targetdir
, basedir
)) {
2298 if (report_baddir
|| errno
!= ENOENT
) {
2299 exitcode
= (int)MANDOCLEVEL_BADARG
;
2300 say("", "&%s: realpath", targetdir
);
2303 } else if (-1 == chdir(basedir
)) {
2304 if (report_baddir
|| errno
!= ENOENT
) {
2305 exitcode
= (int)MANDOCLEVEL_BADARG
;
2311 cp
= strchr(basedir
, '\0');
2312 if ('/' != cp
[-1]) {
2313 if (cp
- basedir
>= PATH_MAX
- 1) {
2314 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2315 say("", "Filename too long");
2325 say(const char *file
, const char *format
, ...)
2330 if ('\0' != *basedir
)
2331 fprintf(stderr
, "%s", basedir
);
2332 if ('\0' != *basedir
&& '\0' != *file
)
2335 fprintf(stderr
, "%s", file
);
2338 if (NULL
!= format
) {
2351 if (NULL
!= format
) {
2352 if ('\0' != *basedir
|| '\0' != *file
)
2353 fputs(": ", stderr
);
2354 va_start(ap
, format
);
2355 vfprintf(stderr
, format
, ap
);
2359 if ('\0' != *basedir
|| '\0' != *file
|| NULL
!= format
)
2360 fputs(": ", stderr
);
2363 fputc('\n', stderr
);