]>
git.cameronkatri.com Git - mandoc.git/blob - mandocdb.c
1 /* $Id: mandocdb.c,v 1.234 2016/10/22 10:09:27 schwarze Exp $ */
3 * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011-2016 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 */
116 int mandocdb(int, char *[]);
118 static void dbadd(struct dba
*, struct mpage
*);
119 static void dbadd_mlink(const struct mlink
*mlink
);
120 static void dbprune(struct dba
*);
121 static void dbwrite(struct dba
*);
122 static void filescan(const char *);
123 #if HAVE_FTS_COMPARE_CONST
124 static int fts_compare(const FTSENT
*const *, const FTSENT
*const *);
126 static int fts_compare(const FTSENT
**, const FTSENT
**);
128 static void mlink_add(struct mlink
*, const struct stat
*);
129 static void mlink_check(struct mpage
*, struct mlink
*);
130 static void mlink_free(struct mlink
*);
131 static void mlinks_undupe(struct mpage
*);
132 static void mpages_free(void);
133 static void mpages_merge(struct dba
*, struct mparse
*);
134 static void parse_cat(struct mpage
*, int);
135 static void parse_man(struct mpage
*, const struct roff_meta
*,
136 const struct roff_node
*);
137 static void parse_mdoc(struct mpage
*, const struct roff_meta
*,
138 const struct roff_node
*);
139 static int parse_mdoc_head(struct mpage
*, const struct roff_meta
*,
140 const struct roff_node
*);
141 static int parse_mdoc_Fd(struct mpage
*, const struct roff_meta
*,
142 const struct roff_node
*);
143 static void parse_mdoc_fname(struct mpage
*, const struct roff_node
*);
144 static int parse_mdoc_Fn(struct mpage
*, const struct roff_meta
*,
145 const struct roff_node
*);
146 static int parse_mdoc_Fo(struct mpage
*, const struct roff_meta
*,
147 const struct roff_node
*);
148 static int parse_mdoc_Nd(struct mpage
*, const struct roff_meta
*,
149 const struct roff_node
*);
150 static int parse_mdoc_Nm(struct mpage
*, const struct roff_meta
*,
151 const struct roff_node
*);
152 static int parse_mdoc_Sh(struct mpage
*, const struct roff_meta
*,
153 const struct roff_node
*);
154 static int parse_mdoc_Va(struct mpage
*, const struct roff_meta
*,
155 const struct roff_node
*);
156 static int parse_mdoc_Xr(struct mpage
*, const struct roff_meta
*,
157 const struct roff_node
*);
158 static void putkey(const struct mpage
*, char *, uint64_t);
159 static void putkeys(const struct mpage
*, char *, size_t, uint64_t);
160 static void putmdockey(const struct mpage
*,
161 const struct roff_node
*, uint64_t);
162 static int render_string(char **, size_t *);
163 static void say(const char *, const char *, ...)
164 __attribute__((__format__ (printf
, 2, 3)));
165 static int set_basedir(const char *, int);
166 static int treescan(void);
167 static size_t utf8(unsigned int, char [7]);
169 static int nodb
; /* no database changes */
170 static int mparse_options
; /* abort the parse early */
171 static int use_all
; /* use all found files */
172 static int debug
; /* print what we're doing */
173 static int warnings
; /* warn about crap */
174 static int write_utf8
; /* write UTF-8 output; else ASCII */
175 static int exitcode
; /* to be returned by main */
176 static enum op op
; /* operational mode */
177 static char basedir
[PATH_MAX
]; /* current base directory */
178 static struct mpage
*mpage_head
; /* list of distinct manual pages */
179 static struct ohash mpages
; /* table of distinct manual pages */
180 static struct ohash mlinks
; /* table of directory entries */
181 static struct ohash names
; /* table of all names */
182 static struct ohash strings
; /* table of all strings */
183 static uint64_t name_mask
;
185 static const struct mdoc_handler mdocs
[MDOC_MAX
] = {
186 { NULL
, 0 }, /* Ap */
187 { NULL
, 0 }, /* Dd */
188 { NULL
, 0 }, /* Dt */
189 { NULL
, 0 }, /* Os */
190 { parse_mdoc_Sh
, TYPE_Sh
}, /* Sh */
191 { parse_mdoc_head
, TYPE_Ss
}, /* Ss */
192 { NULL
, 0 }, /* Pp */
193 { NULL
, 0 }, /* D1 */
194 { NULL
, 0 }, /* Dl */
195 { NULL
, 0 }, /* Bd */
196 { NULL
, 0 }, /* Ed */
197 { NULL
, 0 }, /* Bl */
198 { NULL
, 0 }, /* El */
199 { NULL
, 0 }, /* It */
200 { NULL
, 0 }, /* Ad */
201 { NULL
, TYPE_An
}, /* An */
202 { NULL
, TYPE_Ar
}, /* Ar */
203 { NULL
, TYPE_Cd
}, /* Cd */
204 { NULL
, TYPE_Cm
}, /* Cm */
205 { NULL
, TYPE_Dv
}, /* Dv */
206 { NULL
, TYPE_Er
}, /* Er */
207 { NULL
, TYPE_Ev
}, /* Ev */
208 { NULL
, 0 }, /* Ex */
209 { NULL
, TYPE_Fa
}, /* Fa */
210 { parse_mdoc_Fd
, 0 }, /* Fd */
211 { NULL
, TYPE_Fl
}, /* Fl */
212 { parse_mdoc_Fn
, 0 }, /* Fn */
213 { NULL
, TYPE_Ft
}, /* Ft */
214 { NULL
, TYPE_Ic
}, /* Ic */
215 { NULL
, TYPE_In
}, /* In */
216 { NULL
, TYPE_Li
}, /* Li */
217 { parse_mdoc_Nd
, 0 }, /* Nd */
218 { parse_mdoc_Nm
, 0 }, /* Nm */
219 { NULL
, 0 }, /* Op */
220 { NULL
, 0 }, /* Ot */
221 { NULL
, TYPE_Pa
}, /* Pa */
222 { NULL
, 0 }, /* Rv */
223 { NULL
, TYPE_St
}, /* St */
224 { parse_mdoc_Va
, TYPE_Va
}, /* Va */
225 { parse_mdoc_Va
, TYPE_Vt
}, /* Vt */
226 { parse_mdoc_Xr
, 0 }, /* Xr */
227 { NULL
, 0 }, /* %A */
228 { NULL
, 0 }, /* %B */
229 { NULL
, 0 }, /* %D */
230 { NULL
, 0 }, /* %I */
231 { NULL
, 0 }, /* %J */
232 { NULL
, 0 }, /* %N */
233 { NULL
, 0 }, /* %O */
234 { NULL
, 0 }, /* %P */
235 { NULL
, 0 }, /* %R */
236 { NULL
, 0 }, /* %T */
237 { NULL
, 0 }, /* %V */
238 { NULL
, 0 }, /* Ac */
239 { NULL
, 0 }, /* Ao */
240 { NULL
, 0 }, /* Aq */
241 { NULL
, TYPE_At
}, /* At */
242 { NULL
, 0 }, /* Bc */
243 { NULL
, 0 }, /* Bf */
244 { NULL
, 0 }, /* Bo */
245 { NULL
, 0 }, /* Bq */
246 { NULL
, TYPE_Bsx
}, /* Bsx */
247 { NULL
, TYPE_Bx
}, /* Bx */
248 { NULL
, 0 }, /* Db */
249 { NULL
, 0 }, /* Dc */
250 { NULL
, 0 }, /* Do */
251 { NULL
, 0 }, /* Dq */
252 { NULL
, 0 }, /* Ec */
253 { NULL
, 0 }, /* Ef */
254 { NULL
, TYPE_Em
}, /* Em */
255 { NULL
, 0 }, /* Eo */
256 { NULL
, TYPE_Fx
}, /* Fx */
257 { NULL
, TYPE_Ms
}, /* Ms */
258 { NULL
, 0 }, /* No */
259 { NULL
, 0 }, /* Ns */
260 { NULL
, TYPE_Nx
}, /* Nx */
261 { NULL
, TYPE_Ox
}, /* Ox */
262 { NULL
, 0 }, /* Pc */
263 { NULL
, 0 }, /* Pf */
264 { NULL
, 0 }, /* Po */
265 { NULL
, 0 }, /* Pq */
266 { NULL
, 0 }, /* Qc */
267 { NULL
, 0 }, /* Ql */
268 { NULL
, 0 }, /* Qo */
269 { NULL
, 0 }, /* Qq */
270 { NULL
, 0 }, /* Re */
271 { NULL
, 0 }, /* Rs */
272 { NULL
, 0 }, /* Sc */
273 { NULL
, 0 }, /* So */
274 { NULL
, 0 }, /* Sq */
275 { NULL
, 0 }, /* Sm */
276 { NULL
, 0 }, /* Sx */
277 { NULL
, TYPE_Sy
}, /* Sy */
278 { NULL
, TYPE_Tn
}, /* Tn */
279 { NULL
, 0 }, /* Ux */
280 { NULL
, 0 }, /* Xc */
281 { NULL
, 0 }, /* Xo */
282 { parse_mdoc_Fo
, 0 }, /* Fo */
283 { NULL
, 0 }, /* Fc */
284 { NULL
, 0 }, /* Oo */
285 { NULL
, 0 }, /* Oc */
286 { NULL
, 0 }, /* Bk */
287 { NULL
, 0 }, /* Ek */
288 { NULL
, 0 }, /* Bt */
289 { NULL
, 0 }, /* Hf */
290 { NULL
, 0 }, /* Fr */
291 { NULL
, 0 }, /* Ud */
292 { NULL
, TYPE_Lb
}, /* Lb */
293 { NULL
, 0 }, /* Lp */
294 { NULL
, TYPE_Lk
}, /* Lk */
295 { NULL
, TYPE_Mt
}, /* Mt */
296 { NULL
, 0 }, /* Brq */
297 { NULL
, 0 }, /* Bro */
298 { NULL
, 0 }, /* Brc */
299 { NULL
, 0 }, /* %C */
300 { NULL
, 0 }, /* Es */
301 { NULL
, 0 }, /* En */
302 { NULL
, TYPE_Dx
}, /* Dx */
303 { NULL
, 0 }, /* %Q */
304 { NULL
, 0 }, /* br */
305 { NULL
, 0 }, /* sp */
306 { NULL
, 0 }, /* %U */
307 { NULL
, 0 }, /* Ta */
308 { NULL
, 0 }, /* ll */
313 mandocdb(int argc
, char *argv
[])
318 const char *path_arg
, *progname
;
323 if (pledge("stdio rpath wpath cpath fattr flock proc exec", NULL
) == -1) {
325 return (int)MANDOCLEVEL_SYSERR
;
329 #if HAVE_SANDBOX_INIT
330 if (sandbox_init(kSBXProfileNoInternet
, SANDBOX_NAMED
, NULL
) == -1) {
331 warnx("sandbox_init");
332 return (int)MANDOCLEVEL_SYSERR
;
336 memset(&conf
, 0, sizeof(conf
));
339 * We accept a few different invocations.
340 * The CHECKOP macro makes sure that invocation styles don't
341 * clobber each other.
343 #define CHECKOP(_op, _ch) do \
344 if (OP_DEFAULT != (_op)) { \
345 warnx("-%c: Conflicting option", (_ch)); \
347 } while (/*CONSTCOND*/0)
352 while (-1 != (ch
= getopt(argc
, argv
, "aC:Dd:npQT:tu:v")))
377 mparse_options
|= MPARSE_QUICK
;
380 if (strcmp(optarg
, "utf8")) {
381 warnx("-T%s: Unsupported output format",
389 dup2(STDOUT_FILENO
, STDERR_FILENO
);
399 /* Compatibility with espie@'s makewhatis. */
410 if (pledge("stdio rpath", NULL
) == -1) {
412 return (int)MANDOCLEVEL_SYSERR
;
417 if (OP_CONFFILE
== op
&& argc
> 0) {
418 warnx("-C: Too many arguments");
422 exitcode
= (int)MANDOCLEVEL_OK
;
424 mp
= mparse_alloc(mparse_options
, MANDOCLEVEL_BADARG
, NULL
, NULL
);
425 mandoc_ohash_init(&mpages
, 6, offsetof(struct mpage
, inodev
));
426 mandoc_ohash_init(&mlinks
, 6, offsetof(struct mlink
, file
));
428 if (OP_UPDATE
== op
|| OP_DELETE
== op
|| OP_TEST
== op
) {
431 * Most of these deal with a specific directory.
432 * Jump into that directory first.
434 if (OP_TEST
!= op
&& 0 == set_basedir(path_arg
, 1))
437 dba
= nodb
? dba_new(128) : dba_read(MANDOC_DB
);
440 * The existing database is usable. Process
441 * all files specified on the command-line.
445 if (pledge("stdio rpath wpath cpath fattr flock", NULL
) == -1) {
447 exitcode
= (int)MANDOCLEVEL_SYSERR
;
453 for (i
= 0; i
< argc
; i
++)
458 /* Database missing or corrupt. */
459 if (op
!= OP_UPDATE
|| errno
!= ENOENT
)
460 say(MANDOC_DB
, "%s: Automatically recreating"
461 " from scratch", strerror(errno
));
462 exitcode
= (int)MANDOCLEVEL_OK
;
469 mpages_merge(dba
, mp
);
475 * If we have arguments, use them as our manpaths.
476 * If we don't, use man.conf(5).
479 conf
.manpath
.paths
= mandoc_reallocarray(NULL
,
480 argc
, sizeof(char *));
481 conf
.manpath
.sz
= (size_t)argc
;
482 for (i
= 0; i
< argc
; i
++)
483 conf
.manpath
.paths
[i
] = mandoc_strdup(argv
[i
]);
485 manconf_parse(&conf
, path_arg
, NULL
, NULL
);
487 if (conf
.manpath
.sz
== 0) {
488 exitcode
= (int)MANDOCLEVEL_BADARG
;
489 say("", "Empty manpath");
493 * First scan the tree rooted at a base directory, then
494 * build a new database and finally move it into place.
495 * Ignore zero-length directories and strip trailing
498 for (j
= 0; j
< conf
.manpath
.sz
; j
++) {
499 sz
= strlen(conf
.manpath
.paths
[j
]);
500 if (sz
&& conf
.manpath
.paths
[j
][sz
- 1] == '/')
501 conf
.manpath
.paths
[j
][--sz
] = '\0';
506 mandoc_ohash_init(&mpages
, 6,
507 offsetof(struct mpage
, inodev
));
508 mandoc_ohash_init(&mlinks
, 6,
509 offsetof(struct mlink
, file
));
512 if ( ! set_basedir(conf
.manpath
.paths
[j
], argc
> 0))
517 mpages_merge(dba
, mp
);
522 if (j
+ 1 < conf
.manpath
.sz
) {
524 ohash_delete(&mpages
);
525 ohash_delete(&mlinks
);
534 ohash_delete(&mpages
);
535 ohash_delete(&mlinks
);
538 progname
= getprogname();
539 fprintf(stderr
, "usage: %s [-aDnpQ] [-C file] [-Tutf8]\n"
540 " %s [-aDnpQ] [-Tutf8] dir ...\n"
541 " %s [-DnpQ] [-Tutf8] -d dir [file ...]\n"
542 " %s [-Dnp] -u dir [file ...]\n"
543 " %s [-Q] -t file ...\n",
544 progname
, progname
, progname
, progname
, progname
);
546 return (int)MANDOCLEVEL_BADARG
;
550 * To get a singly linked list in alpha order while inserting entries
551 * at the beginning, process directory entries in reverse alpha order.
554 #if HAVE_FTS_COMPARE_CONST
555 fts_compare(const FTSENT
*const *a
, const FTSENT
*const *b
)
557 fts_compare(const FTSENT
**a
, const FTSENT
**b
)
560 return -strcmp((*a
)->fts_name
, (*b
)->fts_name
);
564 * Scan a directory tree rooted at "basedir" for manpages.
565 * We use fts(), scanning directory parts along the way for clues to our
566 * section and architecture.
568 * If use_all has been specified, grok all files.
569 * If not, sanitise paths to the following:
571 * [./]man*[/<arch>]/<name>.<section>
573 * [./]cat<section>[/<arch>]/<name>.0
575 * TODO: accommodate for multi-language directories.
586 char *dsec
, *arch
, *fsec
, *cp
;
591 argv
[1] = (char *)NULL
;
593 f
= fts_open((char * const *)argv
, FTS_PHYSICAL
| FTS_NOCHDIR
,
596 exitcode
= (int)MANDOCLEVEL_SYSERR
;
597 say("", "&fts_open");
604 while ((ff
= fts_read(f
)) != NULL
) {
605 path
= ff
->fts_path
+ 2;
606 switch (ff
->fts_info
) {
609 * Symbolic links require various sanity checks,
610 * then get handled just like regular files.
613 if (realpath(path
, buf
) == NULL
) {
615 say(path
, "&realpath");
618 if (strstr(buf
, basedir
) != buf
620 && strstr(buf
, HOMEBREWDIR
) != buf
623 if (warnings
) say("",
624 "%s: outside base directory", buf
);
627 /* Use logical inode to avoid mpages dupe. */
628 if (stat(path
, ff
->fts_statp
) == -1) {
636 * If we're a regular file, add an mlink by using the
637 * stored directory data and handling the filename.
640 if ( ! strcmp(path
, MANDOC_DB
))
642 if ( ! use_all
&& ff
->fts_level
< 2) {
644 say(path
, "Extraneous file");
649 while (fsec
== NULL
) {
650 fsec
= strrchr(ff
->fts_name
, '.');
651 if (fsec
== NULL
|| strcmp(fsec
+1, "gz"))
661 "No filename suffix");
664 } else if ( ! strcmp(++fsec
, "html")) {
666 say(path
, "Skip html");
668 } else if ( ! strcmp(fsec
, "ps")) {
670 say(path
, "Skip ps");
672 } else if ( ! strcmp(fsec
, "pdf")) {
674 say(path
, "Skip pdf");
676 } else if ( ! use_all
&&
677 ((dform
== FORM_SRC
&&
678 strncmp(fsec
, dsec
, strlen(dsec
))) ||
679 (dform
== FORM_CAT
&& strcmp(fsec
, "0")))) {
681 say(path
, "Wrong filename suffix");
686 mlink
= mandoc_calloc(1, sizeof(struct mlink
));
687 if (strlcpy(mlink
->file
, path
,
688 sizeof(mlink
->file
)) >=
689 sizeof(mlink
->file
)) {
690 say(path
, "Filename too long");
694 mlink
->dform
= dform
;
697 mlink
->name
= ff
->fts_name
;
700 mlink_add(mlink
, ff
->fts_statp
);
709 say(path
, "Not a regular file");
713 switch (ff
->fts_level
) {
715 /* Ignore the root directory. */
719 * This might contain manX/ or catX/.
720 * Try to infer this from the name.
721 * If we're not in use_all, enforce it.
724 if (ff
->fts_info
== FTS_DP
) {
730 if ( ! strncmp(cp
, "man", 3)) {
733 } else if ( ! strncmp(cp
, "cat", 3)) {
741 if (dsec
!= NULL
|| use_all
)
745 say(path
, "Unknown directory part");
746 fts_set(f
, ff
, FTS_SKIP
);
750 * Possibly our architecture.
751 * If we're descending, keep tabs on it.
753 if (ff
->fts_info
!= FTS_DP
&& dsec
!= NULL
)
759 if (ff
->fts_info
== FTS_DP
|| use_all
)
762 say(path
, "Extraneous directory part");
763 fts_set(f
, ff
, FTS_SKIP
);
773 * Add a file to the mlinks table.
774 * Do not verify that it's a "valid" looking manpage (we'll do that
777 * Try to infer the manual section, architecture, and page name from the
778 * path, assuming it looks like
780 * [./]man*[/<arch>]/<name>.<section>
782 * [./]cat<section>[/<arch>]/<name>.0
784 * See treescan() for the fts(3) version of this.
787 filescan(const char *file
)
796 if (0 == strncmp(file
, "./", 2))
800 * We have to do lstat(2) before realpath(3) loses
801 * the information whether this is a symbolic link.
802 * We need to know that because for symbolic links,
803 * we want to use the orginal file name, while for
804 * regular files, we want to use the real path.
806 if (-1 == lstat(file
, &st
)) {
807 exitcode
= (int)MANDOCLEVEL_BADARG
;
810 } else if (0 == ((S_IFREG
| S_IFLNK
) & st
.st_mode
)) {
811 exitcode
= (int)MANDOCLEVEL_BADARG
;
812 say(file
, "Not a regular file");
817 * We have to resolve the file name to the real path
818 * in any case for the base directory check.
820 if (NULL
== realpath(file
, buf
)) {
821 exitcode
= (int)MANDOCLEVEL_BADARG
;
822 say(file
, "&realpath");
828 else if (strstr(buf
, basedir
) == buf
)
829 start
= buf
+ strlen(basedir
);
831 else if (strstr(buf
, HOMEBREWDIR
) == buf
)
835 exitcode
= (int)MANDOCLEVEL_BADARG
;
836 say("", "%s: outside base directory", buf
);
841 * Now we are sure the file is inside our tree.
842 * If it is a symbolic link, ignore the real path
843 * and use the original name.
844 * This implies passing stuff like "cat1/../man1/foo.1"
845 * on the command line won't work. So don't do that.
846 * Note the stat(2) can still fail if the link target
849 if (S_IFLNK
& st
.st_mode
) {
850 if (-1 == stat(buf
, &st
)) {
851 exitcode
= (int)MANDOCLEVEL_BADARG
;
855 if (strlcpy(buf
, file
, sizeof(buf
)) >= sizeof(buf
)) {
856 say(file
, "Filename too long");
860 if (OP_TEST
!= op
&& strstr(buf
, basedir
) == buf
)
861 start
+= strlen(basedir
);
864 mlink
= mandoc_calloc(1, sizeof(struct mlink
));
865 mlink
->dform
= FORM_NONE
;
866 if (strlcpy(mlink
->file
, start
, sizeof(mlink
->file
)) >=
867 sizeof(mlink
->file
)) {
868 say(start
, "Filename too long");
874 * First try to guess our directory structure.
875 * If we find a separator, try to look for man* or cat*.
876 * If we find one of these and what's underneath is a directory,
877 * assume it's an architecture.
879 if (NULL
!= (p
= strchr(start
, '/'))) {
881 if (0 == strncmp(start
, "man", 3)) {
882 mlink
->dform
= FORM_SRC
;
883 mlink
->dsec
= start
+ 3;
884 } else if (0 == strncmp(start
, "cat", 3)) {
885 mlink
->dform
= FORM_CAT
;
886 mlink
->dsec
= start
+ 3;
890 if (NULL
!= mlink
->dsec
&& NULL
!= (p
= strchr(start
, '/'))) {
898 * Now check the file suffix.
899 * Suffix of `.0' indicates a catpage, `.1-9' is a manpage.
901 p
= strrchr(start
, '\0');
902 while (p
-- > start
&& '/' != *p
&& '.' != *p
)
911 * Now try to parse the name.
912 * Use the filename portion of the path.
915 if (NULL
!= (p
= strrchr(start
, '/'))) {
919 mlink_add(mlink
, &st
);
923 mlink_add(struct mlink
*mlink
, const struct stat
*st
)
925 struct inodev inodev
;
929 assert(NULL
!= mlink
->file
);
931 mlink
->dsec
= mandoc_strdup(mlink
->dsec
? mlink
->dsec
: "");
932 mlink
->arch
= mandoc_strdup(mlink
->arch
? mlink
->arch
: "");
933 mlink
->name
= mandoc_strdup(mlink
->name
? mlink
->name
: "");
934 mlink
->fsec
= mandoc_strdup(mlink
->fsec
? mlink
->fsec
: "");
936 if ('0' == *mlink
->fsec
) {
938 mlink
->fsec
= mandoc_strdup(mlink
->dsec
);
939 mlink
->fform
= FORM_CAT
;
940 } else if ('1' <= *mlink
->fsec
&& '9' >= *mlink
->fsec
)
941 mlink
->fform
= FORM_SRC
;
943 mlink
->fform
= FORM_NONE
;
945 slot
= ohash_qlookup(&mlinks
, mlink
->file
);
946 assert(NULL
== ohash_find(&mlinks
, slot
));
947 ohash_insert(&mlinks
, slot
, mlink
);
949 memset(&inodev
, 0, sizeof(inodev
)); /* Clear padding. */
950 inodev
.st_ino
= st
->st_ino
;
951 inodev
.st_dev
= st
->st_dev
;
952 slot
= ohash_lookup_memory(&mpages
, (char *)&inodev
,
953 sizeof(struct inodev
), inodev
.st_ino
);
954 mpage
= ohash_find(&mpages
, slot
);
956 mpage
= mandoc_calloc(1, sizeof(struct mpage
));
957 mpage
->inodev
.st_ino
= inodev
.st_ino
;
958 mpage
->inodev
.st_dev
= inodev
.st_dev
;
959 mpage
->form
= FORM_NONE
;
960 mpage
->next
= mpage_head
;
962 ohash_insert(&mpages
, slot
, mpage
);
964 mlink
->next
= mpage
->mlinks
;
965 mpage
->mlinks
= mlink
;
966 mlink
->mpage
= mpage
;
970 mlink_free(struct mlink
*mlink
)
986 while ((mpage
= mpage_head
) != NULL
) {
987 while ((mlink
= mpage
->mlinks
) != NULL
) {
988 mpage
->mlinks
= mlink
->next
;
991 mpage_head
= mpage
->next
;
1001 * For each mlink to the mpage, check whether the path looks like
1002 * it is formatted, and if it does, check whether a source manual
1003 * exists by the same name, ignoring the suffix.
1004 * If both conditions hold, drop the mlink.
1007 mlinks_undupe(struct mpage
*mpage
)
1010 struct mlink
**prev
;
1011 struct mlink
*mlink
;
1014 mpage
->form
= FORM_CAT
;
1015 prev
= &mpage
->mlinks
;
1016 while (NULL
!= (mlink
= *prev
)) {
1017 if (FORM_CAT
!= mlink
->dform
) {
1018 mpage
->form
= FORM_NONE
;
1021 (void)strlcpy(buf
, mlink
->file
, sizeof(buf
));
1022 bufp
= strstr(buf
, "cat");
1023 assert(NULL
!= bufp
);
1024 memcpy(bufp
, "man", 3);
1025 if (NULL
!= (bufp
= strrchr(buf
, '.')))
1027 (void)strlcat(buf
, mlink
->dsec
, sizeof(buf
));
1028 if (NULL
== ohash_find(&mlinks
,
1029 ohash_qlookup(&mlinks
, buf
)))
1032 say(mlink
->file
, "Man source exists: %s", buf
);
1035 *prev
= mlink
->next
;
1039 prev
= &(*prev
)->next
;
1044 mlink_check(struct mpage
*mpage
, struct mlink
*mlink
)
1050 * Check whether the manual section given in a file
1051 * agrees with the directory where the file is located.
1052 * Some manuals have suffixes like (3p) on their
1053 * section number either inside the file or in the
1054 * directory name, some are linked into more than one
1055 * section, like encrypt(1) = makekey(8).
1058 if (FORM_SRC
== mpage
->form
&&
1059 strcasecmp(mpage
->sec
, mlink
->dsec
))
1060 say(mlink
->file
, "Section \"%s\" manual in %s directory",
1061 mpage
->sec
, mlink
->dsec
);
1064 * Manual page directories exist for each kernel
1065 * architecture as returned by machine(1).
1066 * However, many manuals only depend on the
1067 * application architecture as returned by arch(1).
1068 * For example, some (2/ARM) manuals are shared
1069 * across the "armish" and "zaurus" kernel
1071 * A few manuals are even shared across completely
1072 * different architectures, for example fdformat(1)
1073 * on amd64, i386, and sparc64.
1076 if (strcasecmp(mpage
->arch
, mlink
->arch
))
1077 say(mlink
->file
, "Architecture \"%s\" manual in "
1078 "\"%s\" directory", mpage
->arch
, mlink
->arch
);
1082 * parse_cat() doesn't set NAME_TITLE yet.
1085 if (FORM_CAT
== mpage
->form
)
1089 * Check whether this mlink
1090 * appears as a name in the NAME section.
1093 slot
= ohash_qlookup(&names
, mlink
->name
);
1094 str
= ohash_find(&names
, slot
);
1095 assert(NULL
!= str
);
1096 if ( ! (NAME_TITLE
& str
->mask
))
1097 say(mlink
->file
, "Name missing in NAME section");
1101 * Run through the files in the global vector "mpages"
1102 * and add them to the database specified in "basedir".
1104 * This handles the parsing scheme itself, using the cues of directory
1105 * and filename to determine whether the file is parsable or not.
1108 mpages_merge(struct dba
*dba
, struct mparse
*mp
)
1110 struct mpage
*mpage
, *mpage_dest
;
1111 struct mlink
*mlink
, *mlink_dest
;
1112 struct roff_man
*man
;
1117 for (mpage
= mpage_head
; mpage
!= NULL
; mpage
= mpage
->next
) {
1118 mlinks_undupe(mpage
);
1119 if ((mlink
= mpage
->mlinks
) == NULL
)
1122 name_mask
= NAME_MASK
;
1123 mandoc_ohash_init(&names
, 4, offsetof(struct str
, key
));
1124 mandoc_ohash_init(&strings
, 6, offsetof(struct str
, key
));
1129 if ((fd
= mparse_open(mp
, mlink
->file
)) == -1) {
1130 say(mlink
->file
, "&open");
1135 * Interpret the file as mdoc(7) or man(7) source
1136 * code, unless it is known to be formatted.
1138 if (mlink
->dform
!= FORM_CAT
|| mlink
->fform
!= FORM_CAT
) {
1139 mparse_readfd(mp
, fd
, mlink
->file
);
1141 mparse_result(mp
, &man
, &sodest
);
1144 if (sodest
!= NULL
) {
1145 mlink_dest
= ohash_find(&mlinks
,
1146 ohash_qlookup(&mlinks
, sodest
));
1147 if (mlink_dest
== NULL
) {
1148 mandoc_asprintf(&cp
, "%s.gz", sodest
);
1149 mlink_dest
= ohash_find(&mlinks
,
1150 ohash_qlookup(&mlinks
, cp
));
1153 if (mlink_dest
!= NULL
) {
1155 /* The .so target exists. */
1157 mpage_dest
= mlink_dest
->mpage
;
1159 mlink
->mpage
= mpage_dest
;
1162 * If the target was already
1163 * processed, add the links
1164 * to the database now.
1165 * Otherwise, this will
1166 * happen when we come
1170 if (mpage_dest
->dba
!= NULL
)
1173 if (mlink
->next
== NULL
)
1175 mlink
= mlink
->next
;
1178 /* Move all links to the target. */
1180 mlink
->next
= mlink_dest
->next
;
1181 mlink_dest
->next
= mpage
->mlinks
;
1182 mpage
->mlinks
= NULL
;
1185 } else if (man
!= NULL
&& man
->macroset
== MACROSET_MDOC
) {
1187 mpage
->form
= FORM_SRC
;
1188 mpage
->sec
= man
->meta
.msec
;
1189 mpage
->sec
= mandoc_strdup(
1190 mpage
->sec
== NULL
? "" : mpage
->sec
);
1191 mpage
->arch
= man
->meta
.arch
;
1192 mpage
->arch
= mandoc_strdup(
1193 mpage
->arch
== NULL
? "" : mpage
->arch
);
1194 mpage
->title
= mandoc_strdup(man
->meta
.title
);
1195 } else if (man
!= NULL
&& man
->macroset
== MACROSET_MAN
) {
1197 mpage
->form
= FORM_SRC
;
1198 mpage
->sec
= mandoc_strdup(man
->meta
.msec
);
1199 mpage
->arch
= mandoc_strdup(mlink
->arch
);
1200 mpage
->title
= mandoc_strdup(man
->meta
.title
);
1202 mpage
->form
= FORM_CAT
;
1203 mpage
->sec
= mandoc_strdup(mlink
->dsec
);
1204 mpage
->arch
= mandoc_strdup(mlink
->arch
);
1205 mpage
->title
= mandoc_strdup(mlink
->name
);
1208 assert(mpage
->desc
== NULL
);
1209 if (man
!= NULL
&& man
->macroset
== MACROSET_MDOC
)
1210 parse_mdoc(mpage
, &man
->meta
, man
->first
);
1211 else if (man
!= NULL
)
1212 parse_man(mpage
, &man
->meta
, man
->first
);
1214 parse_cat(mpage
, fd
);
1215 if (mpage
->desc
== NULL
)
1216 mpage
->desc
= mandoc_strdup(mpage
->mlinks
->name
);
1218 if (warnings
&& !use_all
)
1219 for (mlink
= mpage
->mlinks
; mlink
;
1220 mlink
= mlink
->next
)
1221 mlink_check(mpage
, mlink
);
1224 mlink
= mpage
->mlinks
;
1227 ohash_delete(&strings
);
1228 ohash_delete(&names
);
1233 parse_cat(struct mpage
*mpage
, int fd
)
1236 char *line
, *p
, *title
;
1237 size_t linesz
, plen
, titlesz
;
1241 stream
= (-1 == fd
) ?
1242 fopen(mpage
->mlinks
->file
, "r") :
1244 if (NULL
== stream
) {
1248 say(mpage
->mlinks
->file
, "&fopen");
1255 /* Skip to first blank line. */
1257 while (getline(&line
, &linesz
, stream
) != -1)
1262 * Assume the first line that is not indented
1263 * is the first section header. Skip to it.
1266 while (getline(&line
, &linesz
, stream
) != -1)
1267 if (*line
!= '\n' && *line
!= ' ')
1271 * Read up until the next section into a buffer.
1272 * Strip the leading and trailing newline from each read line,
1273 * appending a trailing space.
1274 * Ignore empty (whitespace-only) lines.
1280 while ((len
= getline(&line
, &linesz
, stream
)) != -1) {
1284 while (isspace((unsigned char)line
[offs
]))
1286 if (line
[offs
] == '\0')
1288 title
= mandoc_realloc(title
, titlesz
+ len
- offs
);
1289 memcpy(title
+ titlesz
, line
+ offs
, len
- offs
);
1290 titlesz
+= len
- offs
;
1291 title
[titlesz
- 1] = ' ';
1296 * If no page content can be found, or the input line
1297 * is already the next section header, or there is no
1298 * trailing newline, reuse the page title as the page
1302 if (NULL
== title
|| '\0' == *title
) {
1304 say(mpage
->mlinks
->file
,
1305 "Cannot find NAME section");
1311 title
[titlesz
- 1] = '\0';
1314 * Skip to the first dash.
1315 * Use the remaining line as the description (no more than 70
1319 if (NULL
!= (p
= strstr(title
, "- "))) {
1320 for (p
+= 2; ' ' == *p
|| '\b' == *p
; p
++)
1321 /* Skip to next word. */ ;
1324 say(mpage
->mlinks
->file
,
1325 "No dash in title line");
1331 /* Strip backspace-encoding from line. */
1333 while (NULL
!= (line
= memchr(p
, '\b', plen
))) {
1336 memmove(line
, line
+ 1, plen
--);
1339 memmove(line
- 1, line
+ 1, plen
- len
);
1343 mpage
->desc
= mandoc_strdup(p
);
1349 * Put a type/word pair into the word database for this particular file.
1352 putkey(const struct mpage
*mpage
, char *value
, uint64_t type
)
1354 putkeys(mpage
, value
, strlen(value
), type
);
1358 * Grok all nodes at or below a certain mdoc node into putkey().
1361 putmdockey(const struct mpage
*mpage
,
1362 const struct roff_node
*n
, uint64_t m
)
1365 for ( ; NULL
!= n
; n
= n
->next
) {
1366 if (NULL
!= n
->child
)
1367 putmdockey(mpage
, n
->child
, m
);
1368 if (n
->type
== ROFFT_TEXT
)
1369 putkey(mpage
, n
->string
, m
);
1374 parse_man(struct mpage
*mpage
, const struct roff_meta
*meta
,
1375 const struct roff_node
*n
)
1377 const struct roff_node
*head
, *body
;
1378 char *start
, *title
;
1386 * We're only searching for one thing: the first text child in
1387 * the BODY of a NAME section. Since we don't keep track of
1388 * sections in -man, run some hoops to find out whether we're in
1389 * the correct section or not.
1392 if (n
->type
== ROFFT_BODY
&& n
->tok
== MAN_SH
) {
1394 if ((head
= body
->parent
->head
) != NULL
&&
1395 (head
= head
->child
) != NULL
&&
1396 head
->next
== NULL
&&
1397 head
->type
== ROFFT_TEXT
&&
1398 strcmp(head
->string
, "NAME") == 0 &&
1399 body
->child
!= NULL
) {
1402 * Suck the entire NAME section into memory.
1403 * Yes, we might run away.
1404 * But too many manuals have big, spread-out
1405 * NAME sections over many lines.
1409 deroff(&title
, body
);
1414 * Go through a special heuristic dance here.
1415 * Conventionally, one or more manual names are
1416 * comma-specified prior to a whitespace, then a
1417 * dash, then a description. Try to puzzle out
1418 * the name parts here.
1423 sz
= strcspn(start
, " ,");
1424 if ('\0' == start
[sz
])
1431 * Assume a stray trailing comma in the
1432 * name list if a name begins with a dash.
1435 if ('-' == start
[0] ||
1436 ('\\' == start
[0] && '-' == start
[1]))
1439 putkey(mpage
, start
, NAME_TITLE
);
1440 if ( ! (mpage
->name_head_done
||
1441 strcasecmp(start
, meta
->title
))) {
1442 putkey(mpage
, start
, NAME_HEAD
);
1443 mpage
->name_head_done
= 1;
1451 assert(',' == byte
);
1453 while (' ' == *start
)
1457 if (start
== title
) {
1458 putkey(mpage
, start
, NAME_TITLE
);
1459 if ( ! (mpage
->name_head_done
||
1460 strcasecmp(start
, meta
->title
))) {
1461 putkey(mpage
, start
, NAME_HEAD
);
1462 mpage
->name_head_done
= 1;
1468 while (isspace((unsigned char)*start
))
1471 if (0 == strncmp(start
, "-", 1))
1473 else if (0 == strncmp(start
, "\\-\\-", 4))
1475 else if (0 == strncmp(start
, "\\-", 2))
1477 else if (0 == strncmp(start
, "\\(en", 4))
1479 else if (0 == strncmp(start
, "\\(em", 4))
1482 while (' ' == *start
)
1485 mpage
->desc
= mandoc_strdup(start
);
1491 for (n
= n
->child
; n
; n
= n
->next
) {
1492 if (NULL
!= mpage
->desc
)
1494 parse_man(mpage
, meta
, n
);
1499 parse_mdoc(struct mpage
*mpage
, const struct roff_meta
*meta
,
1500 const struct roff_node
*n
)
1504 for (n
= n
->child
; NULL
!= n
; n
= n
->next
) {
1511 if (NULL
!= mdocs
[n
->tok
].fp
)
1512 if (0 == (*mdocs
[n
->tok
].fp
)(mpage
, meta
, n
))
1514 if (mdocs
[n
->tok
].mask
)
1515 putmdockey(mpage
, n
->child
,
1516 mdocs
[n
->tok
].mask
);
1519 assert(n
->type
!= ROFFT_ROOT
);
1522 if (NULL
!= n
->child
)
1523 parse_mdoc(mpage
, meta
, n
);
1528 parse_mdoc_Fd(struct mpage
*mpage
, const struct roff_meta
*meta
,
1529 const struct roff_node
*n
)
1534 if (SEC_SYNOPSIS
!= n
->sec
||
1535 NULL
== (n
= n
->child
) ||
1536 n
->type
!= ROFFT_TEXT
)
1540 * Only consider those `Fd' macro fields that begin with an
1541 * "inclusion" token (versus, e.g., #define).
1544 if (strcmp("#include", n
->string
))
1547 if ((n
= n
->next
) == NULL
|| n
->type
!= ROFFT_TEXT
)
1551 * Strip away the enclosing angle brackets and make sure we're
1556 if ('<' == *start
|| '"' == *start
)
1559 if (0 == (sz
= strlen(start
)))
1562 end
= &start
[(int)sz
- 1];
1563 if ('>' == *end
|| '"' == *end
)
1567 putkeys(mpage
, start
, end
- start
+ 1, TYPE_In
);
1572 parse_mdoc_fname(struct mpage
*mpage
, const struct roff_node
*n
)
1577 if (n
->type
!= ROFFT_TEXT
)
1580 /* Skip function pointer punctuation. */
1583 while (*cp
== '(' || *cp
== '*')
1585 sz
= strcspn(cp
, "()");
1587 putkeys(mpage
, cp
, sz
, TYPE_Fn
);
1588 if (n
->sec
== SEC_SYNOPSIS
)
1589 putkeys(mpage
, cp
, sz
, NAME_SYN
);
1593 parse_mdoc_Fn(struct mpage
*mpage
, const struct roff_meta
*meta
,
1594 const struct roff_node
*n
)
1597 if (n
->child
== NULL
)
1600 parse_mdoc_fname(mpage
, n
->child
);
1602 for (n
= n
->child
->next
; n
!= NULL
; n
= n
->next
)
1603 if (n
->type
== ROFFT_TEXT
)
1604 putkey(mpage
, n
->string
, TYPE_Fa
);
1610 parse_mdoc_Fo(struct mpage
*mpage
, const struct roff_meta
*meta
,
1611 const struct roff_node
*n
)
1614 if (n
->type
!= ROFFT_HEAD
)
1617 if (n
->child
!= NULL
)
1618 parse_mdoc_fname(mpage
, n
->child
);
1624 parse_mdoc_Va(struct mpage
*mpage
, const struct roff_meta
*meta
,
1625 const struct roff_node
*n
)
1629 if (n
->type
!= ROFFT_ELEM
&& n
->type
!= ROFFT_BODY
)
1632 if (n
->child
!= NULL
&&
1633 n
->child
->next
== NULL
&&
1634 n
->child
->type
== ROFFT_TEXT
)
1640 putkey(mpage
, cp
, TYPE_Vt
| (n
->tok
== MDOC_Va
||
1641 n
->type
== ROFFT_BODY
? TYPE_Va
: 0));
1649 parse_mdoc_Xr(struct mpage
*mpage
, const struct roff_meta
*meta
,
1650 const struct roff_node
*n
)
1654 if (NULL
== (n
= n
->child
))
1657 if (NULL
== n
->next
) {
1658 putkey(mpage
, n
->string
, TYPE_Xr
);
1662 mandoc_asprintf(&cp
, "%s(%s)", n
->string
, n
->next
->string
);
1663 putkey(mpage
, cp
, TYPE_Xr
);
1669 parse_mdoc_Nd(struct mpage
*mpage
, const struct roff_meta
*meta
,
1670 const struct roff_node
*n
)
1673 if (n
->type
== ROFFT_BODY
)
1674 deroff(&mpage
->desc
, n
);
1679 parse_mdoc_Nm(struct mpage
*mpage
, const struct roff_meta
*meta
,
1680 const struct roff_node
*n
)
1683 if (SEC_NAME
== n
->sec
)
1684 putmdockey(mpage
, n
->child
, NAME_TITLE
);
1685 else if (n
->sec
== SEC_SYNOPSIS
&& n
->type
== ROFFT_HEAD
) {
1686 if (n
->child
== NULL
)
1687 putkey(mpage
, meta
->name
, NAME_SYN
);
1689 putmdockey(mpage
, n
->child
, NAME_SYN
);
1691 if ( ! (mpage
->name_head_done
||
1692 n
->child
== NULL
|| n
->child
->string
== NULL
||
1693 strcasecmp(n
->child
->string
, meta
->title
))) {
1694 putkey(mpage
, n
->child
->string
, NAME_HEAD
);
1695 mpage
->name_head_done
= 1;
1701 parse_mdoc_Sh(struct mpage
*mpage
, const struct roff_meta
*meta
,
1702 const struct roff_node
*n
)
1705 return n
->sec
== SEC_CUSTOM
&& n
->type
== ROFFT_HEAD
;
1709 parse_mdoc_head(struct mpage
*mpage
, const struct roff_meta
*meta
,
1710 const struct roff_node
*n
)
1713 return n
->type
== ROFFT_HEAD
;
1717 * Add a string to the hash table for the current manual.
1718 * Each string has a bitmask telling which macros it belongs to.
1719 * When we finish the manual, we'll dump the table.
1722 putkeys(const struct mpage
*mpage
, char *cp
, size_t sz
, uint64_t v
)
1733 mustfree
= render_string(&cp
, &sz
);
1739 name_mask
&= ~NAME_FIRST
;
1741 say(mpage
->mlinks
->file
,
1742 "Adding name %*s, bits=0x%llx", (int)sz
, cp
,
1743 (unsigned long long)v
);
1747 for (i
= 0; i
< KEY_MAX
; i
++)
1748 if ((uint64_t)1 << i
& v
)
1749 say(mpage
->mlinks
->file
,
1750 "Adding key %s=%*s",
1751 mansearch_keynames
[i
], (int)sz
, cp
);
1755 slot
= ohash_qlookupi(htab
, cp
, &end
);
1756 s
= ohash_find(htab
, slot
);
1758 if (NULL
!= s
&& mpage
== s
->mpage
) {
1761 } else if (NULL
== s
) {
1762 s
= mandoc_calloc(1, sizeof(struct str
) + sz
+ 1);
1763 memcpy(s
->key
, cp
, sz
);
1764 ohash_insert(htab
, slot
, s
);
1774 * Take a Unicode codepoint and produce its UTF-8 encoding.
1775 * This isn't the best way to do this, but it works.
1776 * The magic numbers are from the UTF-8 packaging.
1777 * They're not as scary as they seem: read the UTF-8 spec for details.
1780 utf8(unsigned int cp
, char out
[7])
1785 if (cp
<= 0x0000007F) {
1788 } else if (cp
<= 0x000007FF) {
1790 out
[0] = (cp
>> 6 & 31) | 192;
1791 out
[1] = (cp
& 63) | 128;
1792 } else if (cp
<= 0x0000FFFF) {
1794 out
[0] = (cp
>> 12 & 15) | 224;
1795 out
[1] = (cp
>> 6 & 63) | 128;
1796 out
[2] = (cp
& 63) | 128;
1797 } else if (cp
<= 0x001FFFFF) {
1799 out
[0] = (cp
>> 18 & 7) | 240;
1800 out
[1] = (cp
>> 12 & 63) | 128;
1801 out
[2] = (cp
>> 6 & 63) | 128;
1802 out
[3] = (cp
& 63) | 128;
1803 } else if (cp
<= 0x03FFFFFF) {
1805 out
[0] = (cp
>> 24 & 3) | 248;
1806 out
[1] = (cp
>> 18 & 63) | 128;
1807 out
[2] = (cp
>> 12 & 63) | 128;
1808 out
[3] = (cp
>> 6 & 63) | 128;
1809 out
[4] = (cp
& 63) | 128;
1810 } else if (cp
<= 0x7FFFFFFF) {
1812 out
[0] = (cp
>> 30 & 1) | 252;
1813 out
[1] = (cp
>> 24 & 63) | 128;
1814 out
[2] = (cp
>> 18 & 63) | 128;
1815 out
[3] = (cp
>> 12 & 63) | 128;
1816 out
[4] = (cp
>> 6 & 63) | 128;
1817 out
[5] = (cp
& 63) | 128;
1826 * If the string contains escape sequences,
1827 * replace it with an allocated rendering and return 1,
1828 * such that the caller can free it after use.
1829 * Otherwise, do nothing and return 0.
1832 render_string(char **public, size_t *psz
)
1834 const char *src
, *scp
, *addcp
, *seq
;
1836 size_t ssz
, dsz
, addsz
;
1837 char utfbuf
[7], res
[6];
1838 int seqlen
, unicode
;
1842 res
[2] = ASCII_NBRSP
;
1843 res
[3] = ASCII_HYPH
;
1844 res
[4] = ASCII_BREAK
;
1847 src
= scp
= *public;
1852 while (scp
< src
+ *psz
) {
1854 /* Leave normal characters unchanged. */
1856 if (strchr(res
, *scp
) == NULL
) {
1864 * Found something that requires replacing,
1865 * make sure we have a destination buffer.
1869 dst
= mandoc_malloc(ssz
+ 1);
1871 memcpy(dst
, src
, dsz
);
1874 /* Handle single-char special characters. */
1895 * Found an escape sequence.
1896 * Read past the slash, then parse it.
1897 * Ignore everything except characters.
1901 if (mandoc_escape(&scp
, &seq
, &seqlen
) != ESCAPE_SPECIAL
)
1905 * Render the special character
1906 * as either UTF-8 or ASCII.
1910 unicode
= mchars_spec2cp(seq
, seqlen
);
1913 addsz
= utf8(unicode
, utfbuf
);
1918 addcp
= mchars_spec2str(seq
, seqlen
, &addsz
);
1921 if (*addcp
== ASCII_NBRSP
) {
1927 /* Copy the rendered glyph into the stream. */
1930 dst
= mandoc_realloc(dst
, ssz
+ 1);
1931 memcpy(dst
+ dsz
, addcp
, addsz
);
1939 /* Trim trailing whitespace and NUL-terminate. */
1941 while (*psz
> 0 && (*public)[*psz
- 1] == ' ')
1944 (*public)[*psz
] = '\0';
1951 dbadd_mlink(const struct mlink
*mlink
)
1953 dba_page_alias(mlink
->mpage
->dba
, mlink
->name
, NAME_FILE
);
1954 dba_page_add(mlink
->mpage
->dba
, DBP_SECT
, mlink
->dsec
);
1955 dba_page_add(mlink
->mpage
->dba
, DBP_SECT
, mlink
->fsec
);
1956 dba_page_add(mlink
->mpage
->dba
, DBP_ARCH
, mlink
->arch
);
1957 dba_page_add(mlink
->mpage
->dba
, DBP_FILE
, mlink
->file
);
1961 * Flush the current page's terms (and their bits) into the database.
1962 * Also, handle escape sequences at the last possible moment.
1965 dbadd(struct dba
*dba
, struct mpage
*mpage
)
1967 struct mlink
*mlink
;
1975 mlink
= mpage
->mlinks
;
1978 for (key
= ohash_first(&names
, &slot
); NULL
!= key
;
1979 key
= ohash_next(&names
, &slot
))
1981 for (key
= ohash_first(&strings
, &slot
); NULL
!= key
;
1982 key
= ohash_next(&strings
, &slot
))
1986 while (NULL
!= mlink
) {
1987 fputs(mlink
->name
, stdout
);
1988 if (NULL
== mlink
->next
||
1989 strcmp(mlink
->dsec
, mlink
->next
->dsec
) ||
1990 strcmp(mlink
->fsec
, mlink
->next
->fsec
) ||
1991 strcmp(mlink
->arch
, mlink
->next
->arch
)) {
1993 if ('\0' == *mlink
->dsec
)
1994 fputs(mlink
->fsec
, stdout
);
1996 fputs(mlink
->dsec
, stdout
);
1997 if ('\0' != *mlink
->arch
)
1998 printf("/%s", mlink
->arch
);
2001 mlink
= mlink
->next
;
2003 fputs(", ", stdout
);
2005 printf(" - %s\n", mpage
->desc
);
2010 say(mlink
->file
, "Adding to database");
2014 mustfree
= render_string(&cp
, &i
);
2015 mpage
->dba
= dba_page_new(dba
->pages
,
2016 *mpage
->arch
== '\0' ? mlink
->arch
: mpage
->arch
,
2017 cp
, mlink
->file
, mpage
->form
);
2020 dba_page_add(mpage
->dba
, DBP_SECT
, mpage
->sec
);
2022 while (mlink
!= NULL
) {
2024 mlink
= mlink
->next
;
2027 for (key
= ohash_first(&names
, &slot
); NULL
!= key
;
2028 key
= ohash_next(&names
, &slot
)) {
2029 assert(key
->mpage
== mpage
);
2030 dba_page_alias(mpage
->dba
, key
->key
, key
->mask
);
2033 for (key
= ohash_first(&strings
, &slot
); NULL
!= key
;
2034 key
= ohash_next(&strings
, &slot
)) {
2035 assert(key
->mpage
== mpage
);
2037 for (mask
= TYPE_Xr
; mask
<= TYPE_Lb
; mask
*= 2) {
2038 if (key
->mask
& mask
)
2039 dba_macro_add(dba
->macros
, i
,
2040 key
->key
, mpage
->dba
);
2048 dbprune(struct dba
*dba
)
2050 struct dba_array
*page
, *files
;
2053 dba_array_FOREACH(dba
->pages
, page
) {
2054 files
= dba_array_get(page
, DBP_FILE
);
2055 dba_array_FOREACH(files
, file
) {
2058 if (ohash_find(&mlinks
, ohash_qlookup(&mlinks
,
2061 say(file
, "Deleting from database");
2062 dba_array_del(dba
->pages
);
2070 * Write the database from memory to disk.
2073 dbwrite(struct dba
*dba
)
2079 if (dba_write(MANDOC_DB
"~", dba
) != -1) {
2080 if (rename(MANDOC_DB
"~", MANDOC_DB
) == -1) {
2081 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2082 say(MANDOC_DB
, "&rename");
2083 unlink(MANDOC_DB
"~");
2088 (void)strlcpy(tfn
, "/tmp/mandocdb.XXXXXXXX", sizeof(tfn
));
2089 if (mkdtemp(tfn
) == NULL
) {
2090 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2091 say("", "&%s", tfn
);
2095 (void)strlcat(tfn
, "/" MANDOC_DB
, sizeof(tfn
));
2096 if (dba_write(tfn
, dba
) == -1) {
2097 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2098 say(tfn
, "&dba_write");
2102 switch (child
= fork()) {
2104 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2105 say("", "&fork cmp");
2108 execlp("cmp", "cmp", "-s", tfn
, MANDOC_DB
, (char *)NULL
);
2109 say("", "&exec cmp");
2114 if (waitpid(child
, &status
, 0) == -1) {
2115 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2116 say("", "&wait cmp");
2117 } else if (WIFSIGNALED(status
)) {
2118 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2119 say("", "cmp died from signal %d", WTERMSIG(status
));
2120 } else if (WEXITSTATUS(status
)) {
2121 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2123 "Data changed, but cannot replace database");
2127 *strrchr(tfn
, '/') = '\0';
2128 switch (child
= fork()) {
2130 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2131 say("", "&fork rm");
2134 execlp("rm", "rm", "-rf", tfn
, (char *)NULL
);
2135 say("", "&exec rm");
2136 exit((int)MANDOCLEVEL_SYSERR
);
2140 if (waitpid(child
, &status
, 0) == -1) {
2141 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2142 say("", "&wait rm");
2143 } else if (WIFSIGNALED(status
) || WEXITSTATUS(status
)) {
2144 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2145 say("", "%s: Cannot remove temporary directory", tfn
);
2150 set_basedir(const char *targetdir
, int report_baddir
)
2152 static char startdir
[PATH_MAX
];
2153 static int getcwd_status
; /* 1 = ok, 2 = failure */
2154 static int chdir_status
; /* 1 = changed directory */
2158 * Remember the original working directory, if possible.
2159 * This will be needed if the second or a later directory
2160 * on the command line is given as a relative path.
2161 * Do not error out if the current directory is not
2162 * searchable: Maybe it won't be needed after all.
2164 if (0 == getcwd_status
) {
2165 if (NULL
== getcwd(startdir
, sizeof(startdir
))) {
2167 (void)strlcpy(startdir
, strerror(errno
),
2174 * We are leaving the old base directory.
2175 * Do not use it any longer, not even for messages.
2180 * If and only if the directory was changed earlier and
2181 * the next directory to process is given as a relative path,
2182 * first go back, or bail out if that is impossible.
2184 if (chdir_status
&& '/' != *targetdir
) {
2185 if (2 == getcwd_status
) {
2186 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2187 say("", "getcwd: %s", startdir
);
2190 if (-1 == chdir(startdir
)) {
2191 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2192 say("", "&chdir %s", startdir
);
2198 * Always resolve basedir to the canonicalized absolute
2199 * pathname and append a trailing slash, such that
2200 * we can reliably check whether files are inside.
2202 if (NULL
== realpath(targetdir
, basedir
)) {
2203 if (report_baddir
|| errno
!= ENOENT
) {
2204 exitcode
= (int)MANDOCLEVEL_BADARG
;
2205 say("", "&%s: realpath", targetdir
);
2208 } else if (-1 == chdir(basedir
)) {
2209 if (report_baddir
|| errno
!= ENOENT
) {
2210 exitcode
= (int)MANDOCLEVEL_BADARG
;
2216 cp
= strchr(basedir
, '\0');
2217 if ('/' != cp
[-1]) {
2218 if (cp
- basedir
>= PATH_MAX
- 1) {
2219 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2220 say("", "Filename too long");
2230 say(const char *file
, const char *format
, ...)
2235 if ('\0' != *basedir
)
2236 fprintf(stderr
, "%s", basedir
);
2237 if ('\0' != *basedir
&& '\0' != *file
)
2240 fprintf(stderr
, "%s", file
);
2243 if (NULL
!= format
) {
2256 if (NULL
!= format
) {
2257 if ('\0' != *basedir
|| '\0' != *file
)
2258 fputs(": ", stderr
);
2259 va_start(ap
, format
);
2260 vfprintf(stderr
, format
, ap
);
2264 if ('\0' != *basedir
|| '\0' != *file
|| NULL
!= format
)
2265 fputs(": ", stderr
);
2268 fputc('\n', stderr
);