]>
git.cameronkatri.com Git - mandoc.git/blob - mandocdb.c
1 /* $Id: mandocdb.c,v 1.219 2016/07/15 18:03:45 schwarze Exp $ */
3 * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011-2016 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/types.h>
34 #include "compat_fts.h"
49 #include "mandoc_aux.h"
50 #include "mandoc_ohash.h"
56 #include "mansearch.h"
58 extern int mansearch_keymax
;
59 extern const char *const mansearch_keynames
[];
61 #define SQL_EXEC(_v) \
62 if (SQLITE_OK != sqlite3_exec(db, (_v), NULL, NULL, NULL)) \
63 say("", "%s: %s", (_v), sqlite3_errmsg(db))
64 #define SQL_BIND_TEXT(_s, _i, _v) \
65 if (SQLITE_OK != sqlite3_bind_text \
66 ((_s), (_i)++, (_v), -1, SQLITE_STATIC)) \
67 say(mlink->file, "%s", sqlite3_errmsg(db))
68 #define SQL_BIND_INT(_s, _i, _v) \
69 if (SQLITE_OK != sqlite3_bind_int \
70 ((_s), (_i)++, (_v))) \
71 say(mlink->file, "%s", sqlite3_errmsg(db))
72 #define SQL_BIND_INT64(_s, _i, _v) \
73 if (SQLITE_OK != sqlite3_bind_int64 \
74 ((_s), (_i)++, (_v))) \
75 say(mlink->file, "%s", sqlite3_errmsg(db))
76 #define SQL_STEP(_s) \
77 if (SQLITE_DONE != sqlite3_step((_s))) \
78 say(mlink->file, "%s", sqlite3_errmsg(db))
81 OP_DEFAULT
= 0, /* new dbs from dir list or default config */
82 OP_CONFFILE
, /* new databases from custom config file */
83 OP_UPDATE
, /* delete/add entries in existing database */
84 OP_DELETE
, /* delete entries from existing database */
85 OP_TEST
/* change no databases, report potential problems */
89 const struct mpage
*mpage
; /* if set, the owning parse */
90 uint64_t mask
; /* bitmask in sequence */
91 char key
[]; /* rendered text */
100 struct inodev inodev
; /* used for hashing routine */
101 int64_t pageid
; /* pageid in mpages SQL table */
102 char *sec
; /* section from file content */
103 char *arch
; /* architecture from file content */
104 char *title
; /* title from file content */
105 char *desc
; /* description from file content */
106 struct mlink
*mlinks
; /* singly linked list */
107 int form
; /* format from file content */
112 char file
[PATH_MAX
]; /* filename rel. to manpath */
113 char *dsec
; /* section from directory */
114 char *arch
; /* architecture from directory */
115 char *name
; /* name from file name (not empty) */
116 char *fsec
; /* section from file name suffix */
117 struct mlink
*next
; /* singly linked list */
118 struct mpage
*mpage
; /* parent */
119 int dform
; /* format from directory */
120 int fform
; /* format from file name suffix */
121 int gzip
; /* filename has a .gz suffix */
125 STMT_DELETE_PAGE
= 0, /* delete mpage */
126 STMT_INSERT_PAGE
, /* insert mpage */
127 STMT_INSERT_LINK
, /* insert mlink */
128 STMT_INSERT_NAME
, /* insert name */
129 STMT_SELECT_NAME
, /* retrieve existing name flags */
130 STMT_INSERT_KEY
, /* insert parsed key */
134 typedef int (*mdoc_fp
)(struct mpage
*, const struct roff_meta
*,
135 const struct roff_node
*);
137 struct mdoc_handler
{
138 mdoc_fp fp
; /* optional handler */
139 uint64_t mask
; /* set unless handler returns 0 */
143 int mandocdb(int, char *[]);
145 static void dbclose(int);
146 static void dbadd(struct mpage
*);
147 static void dbadd_mlink(const struct mlink
*mlink
);
148 static void dbadd_mlink_name(const struct mlink
*mlink
);
149 static int dbopen(int);
150 static void dbprune(void);
151 static void filescan(const char *);
152 static void mlink_add(struct mlink
*, const struct stat
*);
153 static void mlink_check(struct mpage
*, struct mlink
*);
154 static void mlink_free(struct mlink
*);
155 static void mlinks_undupe(struct mpage
*);
156 static void mpages_free(void);
157 static void mpages_merge(struct mparse
*);
158 static void names_check(void);
159 static void parse_cat(struct mpage
*, int);
160 static void parse_man(struct mpage
*, const struct roff_meta
*,
161 const struct roff_node
*);
162 static void parse_mdoc(struct mpage
*, const struct roff_meta
*,
163 const struct roff_node
*);
164 static int parse_mdoc_head(struct mpage
*, const struct roff_meta
*,
165 const struct roff_node
*);
166 static int parse_mdoc_Fd(struct mpage
*, const struct roff_meta
*,
167 const struct roff_node
*);
168 static void parse_mdoc_fname(struct mpage
*, const struct roff_node
*);
169 static int parse_mdoc_Fn(struct mpage
*, const struct roff_meta
*,
170 const struct roff_node
*);
171 static int parse_mdoc_Fo(struct mpage
*, const struct roff_meta
*,
172 const struct roff_node
*);
173 static int parse_mdoc_Nd(struct mpage
*, const struct roff_meta
*,
174 const struct roff_node
*);
175 static int parse_mdoc_Nm(struct mpage
*, const struct roff_meta
*,
176 const struct roff_node
*);
177 static int parse_mdoc_Sh(struct mpage
*, const struct roff_meta
*,
178 const struct roff_node
*);
179 static int parse_mdoc_Va(struct mpage
*, const struct roff_meta
*,
180 const struct roff_node
*);
181 static int parse_mdoc_Xr(struct mpage
*, const struct roff_meta
*,
182 const struct roff_node
*);
183 static void putkey(const struct mpage
*, char *, uint64_t);
184 static void putkeys(const struct mpage
*, char *, size_t, uint64_t);
185 static void putmdockey(const struct mpage
*,
186 const struct roff_node
*, uint64_t);
187 static int render_string(char **, size_t *);
188 static void say(const char *, const char *, ...);
189 static int set_basedir(const char *, int);
190 static int treescan(void);
191 static size_t utf8(unsigned int, char [7]);
193 static char tempfilename
[32];
194 static int nodb
; /* no database changes */
195 static int mparse_options
; /* abort the parse early */
196 static int use_all
; /* use all found files */
197 static int debug
; /* print what we're doing */
198 static int warnings
; /* warn about crap */
199 static int write_utf8
; /* write UTF-8 output; else ASCII */
200 static int exitcode
; /* to be returned by main */
201 static enum op op
; /* operational mode */
202 static char basedir
[PATH_MAX
]; /* current base directory */
203 static struct ohash mpages
; /* table of distinct manual pages */
204 static struct ohash mlinks
; /* table of directory entries */
205 static struct ohash names
; /* table of all names */
206 static struct ohash strings
; /* table of all strings */
207 static sqlite3
*db
= NULL
; /* current database */
208 static sqlite3_stmt
*stmts
[STMT__MAX
]; /* current statements */
209 static uint64_t name_mask
;
211 static const struct mdoc_handler mdocs
[MDOC_MAX
] = {
212 { NULL
, 0 }, /* Ap */
213 { NULL
, 0 }, /* Dd */
214 { NULL
, 0 }, /* Dt */
215 { NULL
, 0 }, /* Os */
216 { parse_mdoc_Sh
, TYPE_Sh
}, /* Sh */
217 { parse_mdoc_head
, TYPE_Ss
}, /* Ss */
218 { NULL
, 0 }, /* Pp */
219 { NULL
, 0 }, /* D1 */
220 { NULL
, 0 }, /* Dl */
221 { NULL
, 0 }, /* Bd */
222 { NULL
, 0 }, /* Ed */
223 { NULL
, 0 }, /* Bl */
224 { NULL
, 0 }, /* El */
225 { NULL
, 0 }, /* It */
226 { NULL
, 0 }, /* Ad */
227 { NULL
, TYPE_An
}, /* An */
228 { NULL
, TYPE_Ar
}, /* Ar */
229 { NULL
, TYPE_Cd
}, /* Cd */
230 { NULL
, TYPE_Cm
}, /* Cm */
231 { NULL
, TYPE_Dv
}, /* Dv */
232 { NULL
, TYPE_Er
}, /* Er */
233 { NULL
, TYPE_Ev
}, /* Ev */
234 { NULL
, 0 }, /* Ex */
235 { NULL
, TYPE_Fa
}, /* Fa */
236 { parse_mdoc_Fd
, 0 }, /* Fd */
237 { NULL
, TYPE_Fl
}, /* Fl */
238 { parse_mdoc_Fn
, 0 }, /* Fn */
239 { NULL
, TYPE_Ft
}, /* Ft */
240 { NULL
, TYPE_Ic
}, /* Ic */
241 { NULL
, TYPE_In
}, /* In */
242 { NULL
, TYPE_Li
}, /* Li */
243 { parse_mdoc_Nd
, 0 }, /* Nd */
244 { parse_mdoc_Nm
, 0 }, /* Nm */
245 { NULL
, 0 }, /* Op */
246 { NULL
, 0 }, /* Ot */
247 { NULL
, TYPE_Pa
}, /* Pa */
248 { NULL
, 0 }, /* Rv */
249 { NULL
, TYPE_St
}, /* St */
250 { parse_mdoc_Va
, TYPE_Va
}, /* Va */
251 { parse_mdoc_Va
, TYPE_Vt
}, /* Vt */
252 { parse_mdoc_Xr
, 0 }, /* Xr */
253 { NULL
, 0 }, /* %A */
254 { NULL
, 0 }, /* %B */
255 { NULL
, 0 }, /* %D */
256 { NULL
, 0 }, /* %I */
257 { NULL
, 0 }, /* %J */
258 { NULL
, 0 }, /* %N */
259 { NULL
, 0 }, /* %O */
260 { NULL
, 0 }, /* %P */
261 { NULL
, 0 }, /* %R */
262 { NULL
, 0 }, /* %T */
263 { NULL
, 0 }, /* %V */
264 { NULL
, 0 }, /* Ac */
265 { NULL
, 0 }, /* Ao */
266 { NULL
, 0 }, /* Aq */
267 { NULL
, TYPE_At
}, /* At */
268 { NULL
, 0 }, /* Bc */
269 { NULL
, 0 }, /* Bf */
270 { NULL
, 0 }, /* Bo */
271 { NULL
, 0 }, /* Bq */
272 { NULL
, TYPE_Bsx
}, /* Bsx */
273 { NULL
, TYPE_Bx
}, /* Bx */
274 { NULL
, 0 }, /* Db */
275 { NULL
, 0 }, /* Dc */
276 { NULL
, 0 }, /* Do */
277 { NULL
, 0 }, /* Dq */
278 { NULL
, 0 }, /* Ec */
279 { NULL
, 0 }, /* Ef */
280 { NULL
, TYPE_Em
}, /* Em */
281 { NULL
, 0 }, /* Eo */
282 { NULL
, TYPE_Fx
}, /* Fx */
283 { NULL
, TYPE_Ms
}, /* Ms */
284 { NULL
, 0 }, /* No */
285 { NULL
, 0 }, /* Ns */
286 { NULL
, TYPE_Nx
}, /* Nx */
287 { NULL
, TYPE_Ox
}, /* Ox */
288 { NULL
, 0 }, /* Pc */
289 { NULL
, 0 }, /* Pf */
290 { NULL
, 0 }, /* Po */
291 { NULL
, 0 }, /* Pq */
292 { NULL
, 0 }, /* Qc */
293 { NULL
, 0 }, /* Ql */
294 { NULL
, 0 }, /* Qo */
295 { NULL
, 0 }, /* Qq */
296 { NULL
, 0 }, /* Re */
297 { NULL
, 0 }, /* Rs */
298 { NULL
, 0 }, /* Sc */
299 { NULL
, 0 }, /* So */
300 { NULL
, 0 }, /* Sq */
301 { NULL
, 0 }, /* Sm */
302 { NULL
, 0 }, /* Sx */
303 { NULL
, TYPE_Sy
}, /* Sy */
304 { NULL
, TYPE_Tn
}, /* Tn */
305 { NULL
, 0 }, /* Ux */
306 { NULL
, 0 }, /* Xc */
307 { NULL
, 0 }, /* Xo */
308 { parse_mdoc_Fo
, 0 }, /* Fo */
309 { NULL
, 0 }, /* Fc */
310 { NULL
, 0 }, /* Oo */
311 { NULL
, 0 }, /* Oc */
312 { NULL
, 0 }, /* Bk */
313 { NULL
, 0 }, /* Ek */
314 { NULL
, 0 }, /* Bt */
315 { NULL
, 0 }, /* Hf */
316 { NULL
, 0 }, /* Fr */
317 { NULL
, 0 }, /* Ud */
318 { NULL
, TYPE_Lb
}, /* Lb */
319 { NULL
, 0 }, /* Lp */
320 { NULL
, TYPE_Lk
}, /* Lk */
321 { NULL
, TYPE_Mt
}, /* Mt */
322 { NULL
, 0 }, /* Brq */
323 { NULL
, 0 }, /* Bro */
324 { NULL
, 0 }, /* Brc */
325 { NULL
, 0 }, /* %C */
326 { NULL
, 0 }, /* Es */
327 { NULL
, 0 }, /* En */
328 { NULL
, TYPE_Dx
}, /* Dx */
329 { NULL
, 0 }, /* %Q */
330 { NULL
, 0 }, /* br */
331 { NULL
, 0 }, /* sp */
332 { NULL
, 0 }, /* %U */
333 { NULL
, 0 }, /* Ta */
334 { NULL
, 0 }, /* ll */
339 mandocdb(int argc
, char *argv
[])
343 const char *path_arg
, *progname
;
348 if (pledge("stdio rpath wpath cpath fattr flock proc exec", NULL
) == -1) {
350 return (int)MANDOCLEVEL_SYSERR
;
354 #if HAVE_SANDBOX_INIT
355 if (sandbox_init(kSBXProfileNoInternet
, SANDBOX_NAMED
, NULL
) == -1) {
356 warnx("sandbox_init");
357 return (int)MANDOCLEVEL_SYSERR
;
361 memset(&conf
, 0, sizeof(conf
));
362 memset(stmts
, 0, STMT__MAX
* sizeof(sqlite3_stmt
*));
365 * We accept a few different invocations.
366 * The CHECKOP macro makes sure that invocation styles don't
367 * clobber each other.
369 #define CHECKOP(_op, _ch) do \
370 if (OP_DEFAULT != (_op)) { \
371 warnx("-%c: Conflicting option", (_ch)); \
373 } while (/*CONSTCOND*/0)
378 while (-1 != (ch
= getopt(argc
, argv
, "aC:Dd:npQT:tu:v")))
403 mparse_options
|= MPARSE_QUICK
;
406 if (strcmp(optarg
, "utf8")) {
407 warnx("-T%s: Unsupported output format",
415 dup2(STDOUT_FILENO
, STDERR_FILENO
);
425 /* Compatibility with espie@'s makewhatis. */
436 if (pledge("stdio rpath", NULL
) == -1) {
438 return (int)MANDOCLEVEL_SYSERR
;
443 if (OP_CONFFILE
== op
&& argc
> 0) {
444 warnx("-C: Too many arguments");
448 exitcode
= (int)MANDOCLEVEL_OK
;
450 mp
= mparse_alloc(mparse_options
, MANDOCLEVEL_BADARG
, NULL
, NULL
);
451 mandoc_ohash_init(&mpages
, 6, offsetof(struct mpage
, inodev
));
452 mandoc_ohash_init(&mlinks
, 6, offsetof(struct mlink
, file
));
454 if (OP_UPDATE
== op
|| OP_DELETE
== op
|| OP_TEST
== op
) {
457 * Most of these deal with a specific directory.
458 * Jump into that directory first.
460 if (OP_TEST
!= op
&& 0 == set_basedir(path_arg
, 1))
465 * The existing database is usable. Process
466 * all files specified on the command-line.
470 if (pledge("stdio rpath wpath cpath fattr flock", NULL
) == -1) {
472 exitcode
= (int)MANDOCLEVEL_SYSERR
;
478 for (i
= 0; i
< argc
; i
++)
484 * Database missing or corrupt.
485 * Recreate from scratch.
487 exitcode
= (int)MANDOCLEVEL_OK
;
496 dbclose(OP_DEFAULT
== op
? 0 : 1);
499 * If we have arguments, use them as our manpaths.
500 * If we don't, grok from manpath(1) or however else
501 * manconf_parse() wants to do it.
504 conf
.manpath
.paths
= mandoc_reallocarray(NULL
,
505 argc
, sizeof(char *));
506 conf
.manpath
.sz
= (size_t)argc
;
507 for (i
= 0; i
< argc
; i
++)
508 conf
.manpath
.paths
[i
] = mandoc_strdup(argv
[i
]);
510 manconf_parse(&conf
, path_arg
, NULL
, NULL
);
512 if (conf
.manpath
.sz
== 0) {
513 exitcode
= (int)MANDOCLEVEL_BADARG
;
514 say("", "Empty manpath");
518 * First scan the tree rooted at a base directory, then
519 * build a new database and finally move it into place.
520 * Ignore zero-length directories and strip trailing
523 for (j
= 0; j
< conf
.manpath
.sz
; j
++) {
524 sz
= strlen(conf
.manpath
.paths
[j
]);
525 if (sz
&& conf
.manpath
.paths
[j
][sz
- 1] == '/')
526 conf
.manpath
.paths
[j
][--sz
] = '\0';
531 mandoc_ohash_init(&mpages
, 6,
532 offsetof(struct mpage
, inodev
));
533 mandoc_ohash_init(&mlinks
, 6,
534 offsetof(struct mlink
, file
));
537 if ( ! set_basedir(conf
.manpath
.paths
[j
], argc
> 0))
545 if (warnings
&& !nodb
&&
546 ! (MPARSE_QUICK
& mparse_options
))
550 if (j
+ 1 < conf
.manpath
.sz
) {
552 ohash_delete(&mpages
);
553 ohash_delete(&mlinks
);
562 ohash_delete(&mpages
);
563 ohash_delete(&mlinks
);
566 progname
= getprogname();
567 fprintf(stderr
, "usage: %s [-aDnpQ] [-C file] [-Tutf8]\n"
568 " %s [-aDnpQ] [-Tutf8] dir ...\n"
569 " %s [-DnpQ] [-Tutf8] -d dir [file ...]\n"
570 " %s [-Dnp] -u dir [file ...]\n"
571 " %s [-Q] -t file ...\n",
572 progname
, progname
, progname
, progname
, progname
);
574 return (int)MANDOCLEVEL_BADARG
;
578 * Scan a directory tree rooted at "basedir" for manpages.
579 * We use fts(), scanning directory parts along the way for clues to our
580 * section and architecture.
582 * If use_all has been specified, grok all files.
583 * If not, sanitise paths to the following:
585 * [./]man*[/<arch>]/<name>.<section>
587 * [./]cat<section>[/<arch>]/<name>.0
589 * TODO: accommodate for multi-language directories.
599 char *dsec
, *arch
, *fsec
, *cp
;
604 argv
[1] = (char *)NULL
;
606 f
= fts_open((char * const *)argv
,
607 FTS_PHYSICAL
| FTS_NOCHDIR
, NULL
);
609 exitcode
= (int)MANDOCLEVEL_SYSERR
;
610 say("", "&fts_open");
617 while ((ff
= fts_read(f
)) != NULL
) {
618 path
= ff
->fts_path
+ 2;
619 switch (ff
->fts_info
) {
622 * Symbolic links require various sanity checks,
623 * then get handled just like regular files.
626 if (realpath(path
, buf
) == NULL
) {
628 say(path
, "&realpath");
631 if (strstr(buf
, basedir
) != buf
633 && strstr(buf
, HOMEBREWDIR
) != buf
636 if (warnings
) say("",
637 "%s: outside base directory", buf
);
640 /* Use logical inode to avoid mpages dupe. */
641 if (stat(path
, ff
->fts_statp
) == -1) {
649 * If we're a regular file, add an mlink by using the
650 * stored directory data and handling the filename.
653 if ( ! strcmp(path
, MANDOC_DB
))
655 if ( ! use_all
&& ff
->fts_level
< 2) {
657 say(path
, "Extraneous file");
662 while (fsec
== NULL
) {
663 fsec
= strrchr(ff
->fts_name
, '.');
664 if (fsec
== NULL
|| strcmp(fsec
+1, "gz"))
674 "No filename suffix");
677 } else if ( ! strcmp(++fsec
, "html")) {
679 say(path
, "Skip html");
681 } else if ( ! strcmp(fsec
, "ps")) {
683 say(path
, "Skip ps");
685 } else if ( ! strcmp(fsec
, "pdf")) {
687 say(path
, "Skip pdf");
689 } else if ( ! use_all
&&
690 ((dform
== FORM_SRC
&&
691 strncmp(fsec
, dsec
, strlen(dsec
))) ||
692 (dform
== FORM_CAT
&& strcmp(fsec
, "0")))) {
694 say(path
, "Wrong filename suffix");
699 mlink
= mandoc_calloc(1, sizeof(struct mlink
));
700 if (strlcpy(mlink
->file
, path
,
701 sizeof(mlink
->file
)) >=
702 sizeof(mlink
->file
)) {
703 say(path
, "Filename too long");
707 mlink
->dform
= dform
;
710 mlink
->name
= ff
->fts_name
;
713 mlink_add(mlink
, ff
->fts_statp
);
722 say(path
, "Not a regular file");
726 switch (ff
->fts_level
) {
728 /* Ignore the root directory. */
732 * This might contain manX/ or catX/.
733 * Try to infer this from the name.
734 * If we're not in use_all, enforce it.
737 if (ff
->fts_info
== FTS_DP
) {
743 if ( ! strncmp(cp
, "man", 3)) {
746 } else if ( ! strncmp(cp
, "cat", 3)) {
754 if (dsec
!= NULL
|| use_all
)
758 say(path
, "Unknown directory part");
759 fts_set(f
, ff
, FTS_SKIP
);
763 * Possibly our architecture.
764 * If we're descending, keep tabs on it.
766 if (ff
->fts_info
!= FTS_DP
&& dsec
!= NULL
)
772 if (ff
->fts_info
== FTS_DP
|| use_all
)
775 say(path
, "Extraneous directory part");
776 fts_set(f
, ff
, FTS_SKIP
);
786 * Add a file to the mlinks table.
787 * Do not verify that it's a "valid" looking manpage (we'll do that
790 * Try to infer the manual section, architecture, and page name from the
791 * path, assuming it looks like
793 * [./]man*[/<arch>]/<name>.<section>
795 * [./]cat<section>[/<arch>]/<name>.0
797 * See treescan() for the fts(3) version of this.
800 filescan(const char *file
)
809 if (0 == strncmp(file
, "./", 2))
813 * We have to do lstat(2) before realpath(3) loses
814 * the information whether this is a symbolic link.
815 * We need to know that because for symbolic links,
816 * we want to use the orginal file name, while for
817 * regular files, we want to use the real path.
819 if (-1 == lstat(file
, &st
)) {
820 exitcode
= (int)MANDOCLEVEL_BADARG
;
823 } else if (0 == ((S_IFREG
| S_IFLNK
) & st
.st_mode
)) {
824 exitcode
= (int)MANDOCLEVEL_BADARG
;
825 say(file
, "Not a regular file");
830 * We have to resolve the file name to the real path
831 * in any case for the base directory check.
833 if (NULL
== realpath(file
, buf
)) {
834 exitcode
= (int)MANDOCLEVEL_BADARG
;
835 say(file
, "&realpath");
841 else if (strstr(buf
, basedir
) == buf
)
842 start
= buf
+ strlen(basedir
);
844 else if (strstr(buf
, HOMEBREWDIR
) == buf
)
848 exitcode
= (int)MANDOCLEVEL_BADARG
;
849 say("", "%s: outside base directory", buf
);
854 * Now we are sure the file is inside our tree.
855 * If it is a symbolic link, ignore the real path
856 * and use the original name.
857 * This implies passing stuff like "cat1/../man1/foo.1"
858 * on the command line won't work. So don't do that.
859 * Note the stat(2) can still fail if the link target
862 if (S_IFLNK
& st
.st_mode
) {
863 if (-1 == stat(buf
, &st
)) {
864 exitcode
= (int)MANDOCLEVEL_BADARG
;
868 if (strlcpy(buf
, file
, sizeof(buf
)) >= sizeof(buf
)) {
869 say(file
, "Filename too long");
873 if (OP_TEST
!= op
&& strstr(buf
, basedir
) == buf
)
874 start
+= strlen(basedir
);
877 mlink
= mandoc_calloc(1, sizeof(struct mlink
));
878 mlink
->dform
= FORM_NONE
;
879 if (strlcpy(mlink
->file
, start
, sizeof(mlink
->file
)) >=
880 sizeof(mlink
->file
)) {
881 say(start
, "Filename too long");
887 * First try to guess our directory structure.
888 * If we find a separator, try to look for man* or cat*.
889 * If we find one of these and what's underneath is a directory,
890 * assume it's an architecture.
892 if (NULL
!= (p
= strchr(start
, '/'))) {
894 if (0 == strncmp(start
, "man", 3)) {
895 mlink
->dform
= FORM_SRC
;
896 mlink
->dsec
= start
+ 3;
897 } else if (0 == strncmp(start
, "cat", 3)) {
898 mlink
->dform
= FORM_CAT
;
899 mlink
->dsec
= start
+ 3;
903 if (NULL
!= mlink
->dsec
&& NULL
!= (p
= strchr(start
, '/'))) {
911 * Now check the file suffix.
912 * Suffix of `.0' indicates a catpage, `.1-9' is a manpage.
914 p
= strrchr(start
, '\0');
915 while (p
-- > start
&& '/' != *p
&& '.' != *p
)
924 * Now try to parse the name.
925 * Use the filename portion of the path.
928 if (NULL
!= (p
= strrchr(start
, '/'))) {
932 mlink_add(mlink
, &st
);
936 mlink_add(struct mlink
*mlink
, const struct stat
*st
)
938 struct inodev inodev
;
942 assert(NULL
!= mlink
->file
);
944 mlink
->dsec
= mandoc_strdup(mlink
->dsec
? mlink
->dsec
: "");
945 mlink
->arch
= mandoc_strdup(mlink
->arch
? mlink
->arch
: "");
946 mlink
->name
= mandoc_strdup(mlink
->name
? mlink
->name
: "");
947 mlink
->fsec
= mandoc_strdup(mlink
->fsec
? mlink
->fsec
: "");
949 if ('0' == *mlink
->fsec
) {
951 mlink
->fsec
= mandoc_strdup(mlink
->dsec
);
952 mlink
->fform
= FORM_CAT
;
953 } else if ('1' <= *mlink
->fsec
&& '9' >= *mlink
->fsec
)
954 mlink
->fform
= FORM_SRC
;
956 mlink
->fform
= FORM_NONE
;
958 slot
= ohash_qlookup(&mlinks
, mlink
->file
);
959 assert(NULL
== ohash_find(&mlinks
, slot
));
960 ohash_insert(&mlinks
, slot
, mlink
);
962 memset(&inodev
, 0, sizeof(inodev
)); /* Clear padding. */
963 inodev
.st_ino
= st
->st_ino
;
964 inodev
.st_dev
= st
->st_dev
;
965 slot
= ohash_lookup_memory(&mpages
, (char *)&inodev
,
966 sizeof(struct inodev
), inodev
.st_ino
);
967 mpage
= ohash_find(&mpages
, slot
);
969 mpage
= mandoc_calloc(1, sizeof(struct mpage
));
970 mpage
->inodev
.st_ino
= inodev
.st_ino
;
971 mpage
->inodev
.st_dev
= inodev
.st_dev
;
972 ohash_insert(&mpages
, slot
, mpage
);
974 mlink
->next
= mpage
->mlinks
;
975 mpage
->mlinks
= mlink
;
976 mlink
->mpage
= mpage
;
980 mlink_free(struct mlink
*mlink
)
997 mpage
= ohash_first(&mpages
, &slot
);
998 while (NULL
!= mpage
) {
999 while (NULL
!= (mlink
= mpage
->mlinks
)) {
1000 mpage
->mlinks
= mlink
->next
;
1008 mpage
= ohash_next(&mpages
, &slot
);
1013 * For each mlink to the mpage, check whether the path looks like
1014 * it is formatted, and if it does, check whether a source manual
1015 * exists by the same name, ignoring the suffix.
1016 * If both conditions hold, drop the mlink.
1019 mlinks_undupe(struct mpage
*mpage
)
1022 struct mlink
**prev
;
1023 struct mlink
*mlink
;
1026 mpage
->form
= FORM_CAT
;
1027 prev
= &mpage
->mlinks
;
1028 while (NULL
!= (mlink
= *prev
)) {
1029 if (FORM_CAT
!= mlink
->dform
) {
1030 mpage
->form
= FORM_NONE
;
1033 (void)strlcpy(buf
, mlink
->file
, sizeof(buf
));
1034 bufp
= strstr(buf
, "cat");
1035 assert(NULL
!= bufp
);
1036 memcpy(bufp
, "man", 3);
1037 if (NULL
!= (bufp
= strrchr(buf
, '.')))
1039 (void)strlcat(buf
, mlink
->dsec
, sizeof(buf
));
1040 if (NULL
== ohash_find(&mlinks
,
1041 ohash_qlookup(&mlinks
, buf
)))
1044 say(mlink
->file
, "Man source exists: %s", buf
);
1047 *prev
= mlink
->next
;
1051 prev
= &(*prev
)->next
;
1056 mlink_check(struct mpage
*mpage
, struct mlink
*mlink
)
1062 * Check whether the manual section given in a file
1063 * agrees with the directory where the file is located.
1064 * Some manuals have suffixes like (3p) on their
1065 * section number either inside the file or in the
1066 * directory name, some are linked into more than one
1067 * section, like encrypt(1) = makekey(8).
1070 if (FORM_SRC
== mpage
->form
&&
1071 strcasecmp(mpage
->sec
, mlink
->dsec
))
1072 say(mlink
->file
, "Section \"%s\" manual in %s directory",
1073 mpage
->sec
, mlink
->dsec
);
1076 * Manual page directories exist for each kernel
1077 * architecture as returned by machine(1).
1078 * However, many manuals only depend on the
1079 * application architecture as returned by arch(1).
1080 * For example, some (2/ARM) manuals are shared
1081 * across the "armish" and "zaurus" kernel
1083 * A few manuals are even shared across completely
1084 * different architectures, for example fdformat(1)
1085 * on amd64, i386, sparc, and sparc64.
1088 if (strcasecmp(mpage
->arch
, mlink
->arch
))
1089 say(mlink
->file
, "Architecture \"%s\" manual in "
1090 "\"%s\" directory", mpage
->arch
, mlink
->arch
);
1094 * parse_cat() doesn't set NAME_TITLE yet.
1097 if (FORM_CAT
== mpage
->form
)
1101 * Check whether this mlink
1102 * appears as a name in the NAME section.
1105 slot
= ohash_qlookup(&names
, mlink
->name
);
1106 str
= ohash_find(&names
, slot
);
1107 assert(NULL
!= str
);
1108 if ( ! (NAME_TITLE
& str
->mask
))
1109 say(mlink
->file
, "Name missing in NAME section");
1113 * Run through the files in the global vector "mpages"
1114 * and add them to the database specified in "basedir".
1116 * This handles the parsing scheme itself, using the cues of directory
1117 * and filename to determine whether the file is parsable or not.
1120 mpages_merge(struct mparse
*mp
)
1123 struct mpage
*mpage
, *mpage_dest
;
1124 struct mlink
*mlink
, *mlink_dest
;
1125 struct roff_man
*man
;
1132 SQL_EXEC("BEGIN TRANSACTION");
1134 mpage
= ohash_first(&mpages
, &pslot
);
1135 while (mpage
!= NULL
) {
1136 mlinks_undupe(mpage
);
1137 if ((mlink
= mpage
->mlinks
) == NULL
) {
1138 mpage
= ohash_next(&mpages
, &pslot
);
1142 name_mask
= NAME_MASK
;
1143 mandoc_ohash_init(&names
, 4, offsetof(struct str
, key
));
1144 mandoc_ohash_init(&strings
, 6, offsetof(struct str
, key
));
1149 if ((fd
= mparse_open(mp
, mlink
->file
)) == -1) {
1150 say(mlink
->file
, "&open");
1155 * Interpret the file as mdoc(7) or man(7) source
1156 * code, unless it is known to be formatted.
1158 if (mlink
->dform
!= FORM_CAT
|| mlink
->fform
!= FORM_CAT
) {
1159 mparse_readfd(mp
, fd
, mlink
->file
);
1161 mparse_result(mp
, &man
, &sodest
);
1164 if (sodest
!= NULL
) {
1165 mlink_dest
= ohash_find(&mlinks
,
1166 ohash_qlookup(&mlinks
, sodest
));
1167 if (mlink_dest
== NULL
) {
1168 mandoc_asprintf(&cp
, "%s.gz", sodest
);
1169 mlink_dest
= ohash_find(&mlinks
,
1170 ohash_qlookup(&mlinks
, cp
));
1173 if (mlink_dest
!= NULL
) {
1175 /* The .so target exists. */
1177 mpage_dest
= mlink_dest
->mpage
;
1179 mlink
->mpage
= mpage_dest
;
1182 * If the target was already
1183 * processed, add the links
1184 * to the database now.
1185 * Otherwise, this will
1186 * happen when we come
1190 if (mpage_dest
->pageid
)
1191 dbadd_mlink_name(mlink
);
1193 if (mlink
->next
== NULL
)
1195 mlink
= mlink
->next
;
1198 /* Move all links to the target. */
1200 mlink
->next
= mlink_dest
->next
;
1201 mlink_dest
->next
= mpage
->mlinks
;
1202 mpage
->mlinks
= NULL
;
1205 } else if (man
!= NULL
&& man
->macroset
== MACROSET_MDOC
) {
1207 mpage
->form
= FORM_SRC
;
1208 mpage
->sec
= man
->meta
.msec
;
1209 mpage
->sec
= mandoc_strdup(
1210 mpage
->sec
== NULL
? "" : mpage
->sec
);
1211 mpage
->arch
= man
->meta
.arch
;
1212 mpage
->arch
= mandoc_strdup(
1213 mpage
->arch
== NULL
? "" : mpage
->arch
);
1214 mpage
->title
= mandoc_strdup(man
->meta
.title
);
1215 } else if (man
!= NULL
&& man
->macroset
== MACROSET_MAN
) {
1217 mpage
->form
= FORM_SRC
;
1218 mpage
->sec
= mandoc_strdup(man
->meta
.msec
);
1219 mpage
->arch
= mandoc_strdup(mlink
->arch
);
1220 mpage
->title
= mandoc_strdup(man
->meta
.title
);
1222 mpage
->form
= FORM_CAT
;
1223 mpage
->sec
= mandoc_strdup(mlink
->dsec
);
1224 mpage
->arch
= mandoc_strdup(mlink
->arch
);
1225 mpage
->title
= mandoc_strdup(mlink
->name
);
1227 putkey(mpage
, mpage
->sec
, TYPE_sec
);
1228 if (*mpage
->arch
!= '\0')
1229 putkey(mpage
, mpage
->arch
, TYPE_arch
);
1231 for ( ; mlink
!= NULL
; mlink
= mlink
->next
) {
1232 if ('\0' != *mlink
->dsec
)
1233 putkey(mpage
, mlink
->dsec
, TYPE_sec
);
1234 if ('\0' != *mlink
->fsec
)
1235 putkey(mpage
, mlink
->fsec
, TYPE_sec
);
1236 putkey(mpage
, '\0' == *mlink
->arch
?
1237 any
: mlink
->arch
, TYPE_arch
);
1238 putkey(mpage
, mlink
->name
, NAME_FILE
);
1241 assert(mpage
->desc
== NULL
);
1242 if (man
!= NULL
&& man
->macroset
== MACROSET_MDOC
)
1243 parse_mdoc(mpage
, &man
->meta
, man
->first
);
1244 else if (man
!= NULL
)
1245 parse_man(mpage
, &man
->meta
, man
->first
);
1247 parse_cat(mpage
, fd
);
1248 if (mpage
->desc
== NULL
)
1249 mpage
->desc
= mandoc_strdup(mpage
->mlinks
->name
);
1251 if (warnings
&& !use_all
)
1252 for (mlink
= mpage
->mlinks
; mlink
;
1253 mlink
= mlink
->next
)
1254 mlink_check(mpage
, mlink
);
1257 mlink
= mpage
->mlinks
;
1260 ohash_delete(&strings
);
1261 ohash_delete(&names
);
1262 mpage
= ohash_next(&mpages
, &pslot
);
1266 SQL_EXEC("END TRANSACTION");
1273 const char *name
, *sec
, *arch
, *key
;
1275 sqlite3_prepare_v2(db
,
1276 "SELECT name, sec, arch, key FROM ("
1277 "SELECT name AS key, pageid FROM names "
1278 "WHERE bits & ? AND NOT EXISTS ("
1279 "SELECT pageid FROM mlinks "
1280 "WHERE mlinks.pageid == names.pageid "
1281 "AND mlinks.name == names.name"
1284 "SELECT sec, arch, name, pageid FROM mlinks "
1286 ") USING (pageid);",
1289 if (sqlite3_bind_int64(stmt
, 1, NAME_TITLE
) != SQLITE_OK
)
1290 say("", "%s", sqlite3_errmsg(db
));
1292 while (sqlite3_step(stmt
) == SQLITE_ROW
) {
1293 name
= (const char *)sqlite3_column_text(stmt
, 0);
1294 sec
= (const char *)sqlite3_column_text(stmt
, 1);
1295 arch
= (const char *)sqlite3_column_text(stmt
, 2);
1296 key
= (const char *)sqlite3_column_text(stmt
, 3);
1297 say("", "%s(%s%s%s) lacks mlink \"%s\"", name
, sec
,
1298 '\0' == *arch
? "" : "/",
1299 '\0' == *arch
? "" : arch
, key
);
1301 sqlite3_finalize(stmt
);
1305 parse_cat(struct mpage
*mpage
, int fd
)
1308 char *line
, *p
, *title
;
1309 size_t linesz
, plen
, titlesz
;
1313 stream
= (-1 == fd
) ?
1314 fopen(mpage
->mlinks
->file
, "r") :
1316 if (NULL
== stream
) {
1320 say(mpage
->mlinks
->file
, "&fopen");
1327 /* Skip to first blank line. */
1329 while (getline(&line
, &linesz
, stream
) != -1)
1334 * Assume the first line that is not indented
1335 * is the first section header. Skip to it.
1338 while (getline(&line
, &linesz
, stream
) != -1)
1339 if (*line
!= '\n' && *line
!= ' ')
1343 * Read up until the next section into a buffer.
1344 * Strip the leading and trailing newline from each read line,
1345 * appending a trailing space.
1346 * Ignore empty (whitespace-only) lines.
1352 while ((len
= getline(&line
, &linesz
, stream
)) != -1) {
1356 while (isspace((unsigned char)line
[offs
]))
1358 if (line
[offs
] == '\0')
1360 title
= mandoc_realloc(title
, titlesz
+ len
- offs
);
1361 memcpy(title
+ titlesz
, line
+ offs
, len
- offs
);
1362 titlesz
+= len
- offs
;
1363 title
[titlesz
- 1] = ' ';
1368 * If no page content can be found, or the input line
1369 * is already the next section header, or there is no
1370 * trailing newline, reuse the page title as the page
1374 if (NULL
== title
|| '\0' == *title
) {
1376 say(mpage
->mlinks
->file
,
1377 "Cannot find NAME section");
1383 title
[titlesz
- 1] = '\0';
1386 * Skip to the first dash.
1387 * Use the remaining line as the description (no more than 70
1391 if (NULL
!= (p
= strstr(title
, "- "))) {
1392 for (p
+= 2; ' ' == *p
|| '\b' == *p
; p
++)
1393 /* Skip to next word. */ ;
1396 say(mpage
->mlinks
->file
,
1397 "No dash in title line");
1403 /* Strip backspace-encoding from line. */
1405 while (NULL
!= (line
= memchr(p
, '\b', plen
))) {
1408 memmove(line
, line
+ 1, plen
--);
1411 memmove(line
- 1, line
+ 1, plen
- len
);
1415 mpage
->desc
= mandoc_strdup(p
);
1421 * Put a type/word pair into the word database for this particular file.
1424 putkey(const struct mpage
*mpage
, char *value
, uint64_t type
)
1428 assert(NULL
!= value
);
1429 if (TYPE_arch
== type
)
1430 for (cp
= value
; *cp
; cp
++)
1431 if (isupper((unsigned char)*cp
))
1432 *cp
= _tolower((unsigned char)*cp
);
1433 putkeys(mpage
, value
, strlen(value
), type
);
1437 * Grok all nodes at or below a certain mdoc node into putkey().
1440 putmdockey(const struct mpage
*mpage
,
1441 const struct roff_node
*n
, uint64_t m
)
1444 for ( ; NULL
!= n
; n
= n
->next
) {
1445 if (NULL
!= n
->child
)
1446 putmdockey(mpage
, n
->child
, m
);
1447 if (n
->type
== ROFFT_TEXT
)
1448 putkey(mpage
, n
->string
, m
);
1453 parse_man(struct mpage
*mpage
, const struct roff_meta
*meta
,
1454 const struct roff_node
*n
)
1456 const struct roff_node
*head
, *body
;
1457 char *start
, *title
;
1465 * We're only searching for one thing: the first text child in
1466 * the BODY of a NAME section. Since we don't keep track of
1467 * sections in -man, run some hoops to find out whether we're in
1468 * the correct section or not.
1471 if (n
->type
== ROFFT_BODY
&& n
->tok
== MAN_SH
) {
1473 if ((head
= body
->parent
->head
) != NULL
&&
1474 (head
= head
->child
) != NULL
&&
1475 head
->next
== NULL
&&
1476 head
->type
== ROFFT_TEXT
&&
1477 strcmp(head
->string
, "NAME") == 0 &&
1478 body
->child
!= NULL
) {
1481 * Suck the entire NAME section into memory.
1482 * Yes, we might run away.
1483 * But too many manuals have big, spread-out
1484 * NAME sections over many lines.
1488 deroff(&title
, body
);
1493 * Go through a special heuristic dance here.
1494 * Conventionally, one or more manual names are
1495 * comma-specified prior to a whitespace, then a
1496 * dash, then a description. Try to puzzle out
1497 * the name parts here.
1502 sz
= strcspn(start
, " ,");
1503 if ('\0' == start
[sz
])
1510 * Assume a stray trailing comma in the
1511 * name list if a name begins with a dash.
1514 if ('-' == start
[0] ||
1515 ('\\' == start
[0] && '-' == start
[1]))
1518 putkey(mpage
, start
, NAME_TITLE
);
1519 if ( ! (mpage
->name_head_done
||
1520 strcasecmp(start
, meta
->title
))) {
1521 putkey(mpage
, start
, NAME_HEAD
);
1522 mpage
->name_head_done
= 1;
1530 assert(',' == byte
);
1532 while (' ' == *start
)
1536 if (start
== title
) {
1537 putkey(mpage
, start
, NAME_TITLE
);
1538 if ( ! (mpage
->name_head_done
||
1539 strcasecmp(start
, meta
->title
))) {
1540 putkey(mpage
, start
, NAME_HEAD
);
1541 mpage
->name_head_done
= 1;
1547 while (isspace((unsigned char)*start
))
1550 if (0 == strncmp(start
, "-", 1))
1552 else if (0 == strncmp(start
, "\\-\\-", 4))
1554 else if (0 == strncmp(start
, "\\-", 2))
1556 else if (0 == strncmp(start
, "\\(en", 4))
1558 else if (0 == strncmp(start
, "\\(em", 4))
1561 while (' ' == *start
)
1564 mpage
->desc
= mandoc_strdup(start
);
1570 for (n
= n
->child
; n
; n
= n
->next
) {
1571 if (NULL
!= mpage
->desc
)
1573 parse_man(mpage
, meta
, n
);
1578 parse_mdoc(struct mpage
*mpage
, const struct roff_meta
*meta
,
1579 const struct roff_node
*n
)
1583 for (n
= n
->child
; NULL
!= n
; n
= n
->next
) {
1590 if (NULL
!= mdocs
[n
->tok
].fp
)
1591 if (0 == (*mdocs
[n
->tok
].fp
)(mpage
, meta
, n
))
1593 if (mdocs
[n
->tok
].mask
)
1594 putmdockey(mpage
, n
->child
,
1595 mdocs
[n
->tok
].mask
);
1598 assert(n
->type
!= ROFFT_ROOT
);
1601 if (NULL
!= n
->child
)
1602 parse_mdoc(mpage
, meta
, n
);
1607 parse_mdoc_Fd(struct mpage
*mpage
, const struct roff_meta
*meta
,
1608 const struct roff_node
*n
)
1613 if (SEC_SYNOPSIS
!= n
->sec
||
1614 NULL
== (n
= n
->child
) ||
1615 n
->type
!= ROFFT_TEXT
)
1619 * Only consider those `Fd' macro fields that begin with an
1620 * "inclusion" token (versus, e.g., #define).
1623 if (strcmp("#include", n
->string
))
1626 if ((n
= n
->next
) == NULL
|| n
->type
!= ROFFT_TEXT
)
1630 * Strip away the enclosing angle brackets and make sure we're
1635 if ('<' == *start
|| '"' == *start
)
1638 if (0 == (sz
= strlen(start
)))
1641 end
= &start
[(int)sz
- 1];
1642 if ('>' == *end
|| '"' == *end
)
1646 putkeys(mpage
, start
, end
- start
+ 1, TYPE_In
);
1651 parse_mdoc_fname(struct mpage
*mpage
, const struct roff_node
*n
)
1656 if (n
->type
!= ROFFT_TEXT
)
1659 /* Skip function pointer punctuation. */
1662 while (*cp
== '(' || *cp
== '*')
1664 sz
= strcspn(cp
, "()");
1666 putkeys(mpage
, cp
, sz
, TYPE_Fn
);
1667 if (n
->sec
== SEC_SYNOPSIS
)
1668 putkeys(mpage
, cp
, sz
, NAME_SYN
);
1672 parse_mdoc_Fn(struct mpage
*mpage
, const struct roff_meta
*meta
,
1673 const struct roff_node
*n
)
1676 if (n
->child
== NULL
)
1679 parse_mdoc_fname(mpage
, n
->child
);
1681 for (n
= n
->child
->next
; n
!= NULL
; n
= n
->next
)
1682 if (n
->type
== ROFFT_TEXT
)
1683 putkey(mpage
, n
->string
, TYPE_Fa
);
1689 parse_mdoc_Fo(struct mpage
*mpage
, const struct roff_meta
*meta
,
1690 const struct roff_node
*n
)
1693 if (n
->type
!= ROFFT_HEAD
)
1696 if (n
->child
!= NULL
)
1697 parse_mdoc_fname(mpage
, n
->child
);
1703 parse_mdoc_Va(struct mpage
*mpage
, const struct roff_meta
*meta
,
1704 const struct roff_node
*n
)
1708 if (n
->type
!= ROFFT_ELEM
&& n
->type
!= ROFFT_BODY
)
1711 if (n
->child
!= NULL
&&
1712 n
->child
->next
== NULL
&&
1713 n
->child
->type
== ROFFT_TEXT
)
1719 putkey(mpage
, cp
, TYPE_Vt
| (n
->tok
== MDOC_Va
||
1720 n
->type
== ROFFT_BODY
? TYPE_Va
: 0));
1728 parse_mdoc_Xr(struct mpage
*mpage
, const struct roff_meta
*meta
,
1729 const struct roff_node
*n
)
1733 if (NULL
== (n
= n
->child
))
1736 if (NULL
== n
->next
) {
1737 putkey(mpage
, n
->string
, TYPE_Xr
);
1741 mandoc_asprintf(&cp
, "%s(%s)", n
->string
, n
->next
->string
);
1742 putkey(mpage
, cp
, TYPE_Xr
);
1748 parse_mdoc_Nd(struct mpage
*mpage
, const struct roff_meta
*meta
,
1749 const struct roff_node
*n
)
1752 if (n
->type
== ROFFT_BODY
)
1753 deroff(&mpage
->desc
, n
);
1758 parse_mdoc_Nm(struct mpage
*mpage
, const struct roff_meta
*meta
,
1759 const struct roff_node
*n
)
1762 if (SEC_NAME
== n
->sec
)
1763 putmdockey(mpage
, n
->child
, NAME_TITLE
);
1764 else if (n
->sec
== SEC_SYNOPSIS
&& n
->type
== ROFFT_HEAD
) {
1765 if (n
->child
== NULL
)
1766 putkey(mpage
, meta
->name
, NAME_SYN
);
1768 putmdockey(mpage
, n
->child
, NAME_SYN
);
1770 if ( ! (mpage
->name_head_done
||
1771 n
->child
== NULL
|| n
->child
->string
== NULL
||
1772 strcasecmp(n
->child
->string
, meta
->title
))) {
1773 putkey(mpage
, n
->child
->string
, ROFFT_HEAD
);
1774 mpage
->name_head_done
= 1;
1780 parse_mdoc_Sh(struct mpage
*mpage
, const struct roff_meta
*meta
,
1781 const struct roff_node
*n
)
1784 return n
->sec
== SEC_CUSTOM
&& n
->type
== ROFFT_HEAD
;
1788 parse_mdoc_head(struct mpage
*mpage
, const struct roff_meta
*meta
,
1789 const struct roff_node
*n
)
1792 return n
->type
== ROFFT_HEAD
;
1796 * Add a string to the hash table for the current manual.
1797 * Each string has a bitmask telling which macros it belongs to.
1798 * When we finish the manual, we'll dump the table.
1801 putkeys(const struct mpage
*mpage
, char *cp
, size_t sz
, uint64_t v
)
1812 mustfree
= render_string(&cp
, &sz
);
1818 name_mask
&= ~NAME_FIRST
;
1820 say(mpage
->mlinks
->file
,
1821 "Adding name %*s, bits=%d", sz
, cp
, v
);
1825 for (i
= 0; i
< mansearch_keymax
; i
++)
1826 if ((uint64_t)1 << i
& v
)
1827 say(mpage
->mlinks
->file
,
1828 "Adding key %s=%*s",
1829 mansearch_keynames
[i
], sz
, cp
);
1833 slot
= ohash_qlookupi(htab
, cp
, &end
);
1834 s
= ohash_find(htab
, slot
);
1836 if (NULL
!= s
&& mpage
== s
->mpage
) {
1839 } else if (NULL
== s
) {
1840 s
= mandoc_calloc(1, sizeof(struct str
) + sz
+ 1);
1841 memcpy(s
->key
, cp
, sz
);
1842 ohash_insert(htab
, slot
, s
);
1852 * Take a Unicode codepoint and produce its UTF-8 encoding.
1853 * This isn't the best way to do this, but it works.
1854 * The magic numbers are from the UTF-8 packaging.
1855 * They're not as scary as they seem: read the UTF-8 spec for details.
1858 utf8(unsigned int cp
, char out
[7])
1863 if (cp
<= 0x0000007F) {
1866 } else if (cp
<= 0x000007FF) {
1868 out
[0] = (cp
>> 6 & 31) | 192;
1869 out
[1] = (cp
& 63) | 128;
1870 } else if (cp
<= 0x0000FFFF) {
1872 out
[0] = (cp
>> 12 & 15) | 224;
1873 out
[1] = (cp
>> 6 & 63) | 128;
1874 out
[2] = (cp
& 63) | 128;
1875 } else if (cp
<= 0x001FFFFF) {
1877 out
[0] = (cp
>> 18 & 7) | 240;
1878 out
[1] = (cp
>> 12 & 63) | 128;
1879 out
[2] = (cp
>> 6 & 63) | 128;
1880 out
[3] = (cp
& 63) | 128;
1881 } else if (cp
<= 0x03FFFFFF) {
1883 out
[0] = (cp
>> 24 & 3) | 248;
1884 out
[1] = (cp
>> 18 & 63) | 128;
1885 out
[2] = (cp
>> 12 & 63) | 128;
1886 out
[3] = (cp
>> 6 & 63) | 128;
1887 out
[4] = (cp
& 63) | 128;
1888 } else if (cp
<= 0x7FFFFFFF) {
1890 out
[0] = (cp
>> 30 & 1) | 252;
1891 out
[1] = (cp
>> 24 & 63) | 128;
1892 out
[2] = (cp
>> 18 & 63) | 128;
1893 out
[3] = (cp
>> 12 & 63) | 128;
1894 out
[4] = (cp
>> 6 & 63) | 128;
1895 out
[5] = (cp
& 63) | 128;
1904 * If the string contains escape sequences,
1905 * replace it with an allocated rendering and return 1,
1906 * such that the caller can free it after use.
1907 * Otherwise, do nothing and return 0.
1910 render_string(char **public, size_t *psz
)
1912 const char *src
, *scp
, *addcp
, *seq
;
1914 size_t ssz
, dsz
, addsz
;
1915 char utfbuf
[7], res
[6];
1916 int seqlen
, unicode
;
1920 res
[2] = ASCII_NBRSP
;
1921 res
[3] = ASCII_HYPH
;
1922 res
[4] = ASCII_BREAK
;
1925 src
= scp
= *public;
1930 while (scp
< src
+ *psz
) {
1932 /* Leave normal characters unchanged. */
1934 if (strchr(res
, *scp
) == NULL
) {
1942 * Found something that requires replacing,
1943 * make sure we have a destination buffer.
1947 dst
= mandoc_malloc(ssz
+ 1);
1949 memcpy(dst
, src
, dsz
);
1952 /* Handle single-char special characters. */
1973 * Found an escape sequence.
1974 * Read past the slash, then parse it.
1975 * Ignore everything except characters.
1979 if (mandoc_escape(&scp
, &seq
, &seqlen
) != ESCAPE_SPECIAL
)
1983 * Render the special character
1984 * as either UTF-8 or ASCII.
1988 unicode
= mchars_spec2cp(seq
, seqlen
);
1991 addsz
= utf8(unicode
, utfbuf
);
1996 addcp
= mchars_spec2str(seq
, seqlen
, &addsz
);
1999 if (*addcp
== ASCII_NBRSP
) {
2005 /* Copy the rendered glyph into the stream. */
2008 dst
= mandoc_realloc(dst
, ssz
+ 1);
2009 memcpy(dst
+ dsz
, addcp
, addsz
);
2017 /* Trim trailing whitespace and NUL-terminate. */
2019 while (*psz
> 0 && (*public)[*psz
- 1] == ' ')
2022 (*public)[*psz
] = '\0';
2029 dbadd_mlink(const struct mlink
*mlink
)
2034 SQL_BIND_TEXT(stmts
[STMT_INSERT_LINK
], i
, mlink
->dsec
);
2035 SQL_BIND_TEXT(stmts
[STMT_INSERT_LINK
], i
, mlink
->arch
);
2036 SQL_BIND_TEXT(stmts
[STMT_INSERT_LINK
], i
, mlink
->name
);
2037 SQL_BIND_INT64(stmts
[STMT_INSERT_LINK
], i
, mlink
->mpage
->pageid
);
2038 SQL_STEP(stmts
[STMT_INSERT_LINK
]);
2039 sqlite3_reset(stmts
[STMT_INSERT_LINK
]);
2043 dbadd_mlink_name(const struct mlink
*mlink
)
2051 SQL_BIND_INT64(stmts
[STMT_SELECT_NAME
], i
, mlink
->mpage
->pageid
);
2052 bits
= NAME_FILE
& NAME_MASK
;
2053 if (sqlite3_step(stmts
[STMT_SELECT_NAME
]) == SQLITE_ROW
) {
2054 bits
|= sqlite3_column_int64(stmts
[STMT_SELECT_NAME
], 0);
2055 sqlite3_reset(stmts
[STMT_SELECT_NAME
]);
2059 SQL_BIND_INT64(stmts
[STMT_INSERT_NAME
], i
, bits
);
2060 SQL_BIND_TEXT(stmts
[STMT_INSERT_NAME
], i
, mlink
->name
);
2061 SQL_BIND_INT64(stmts
[STMT_INSERT_NAME
], i
, mlink
->mpage
->pageid
);
2062 SQL_STEP(stmts
[STMT_INSERT_NAME
]);
2063 sqlite3_reset(stmts
[STMT_INSERT_NAME
]);
2067 * Flush the current page's terms (and their bits) into the database.
2068 * Wrap the entire set of additions in a transaction to make sqlite be a
2070 * Also, handle escape sequences at the last possible moment.
2073 dbadd(struct mpage
*mpage
)
2075 struct mlink
*mlink
;
2082 mlink
= mpage
->mlinks
;
2085 for (key
= ohash_first(&names
, &slot
); NULL
!= key
;
2086 key
= ohash_next(&names
, &slot
))
2088 for (key
= ohash_first(&strings
, &slot
); NULL
!= key
;
2089 key
= ohash_next(&strings
, &slot
))
2093 while (NULL
!= mlink
) {
2094 fputs(mlink
->name
, stdout
);
2095 if (NULL
== mlink
->next
||
2096 strcmp(mlink
->dsec
, mlink
->next
->dsec
) ||
2097 strcmp(mlink
->fsec
, mlink
->next
->fsec
) ||
2098 strcmp(mlink
->arch
, mlink
->next
->arch
)) {
2100 if ('\0' == *mlink
->dsec
)
2101 fputs(mlink
->fsec
, stdout
);
2103 fputs(mlink
->dsec
, stdout
);
2104 if ('\0' != *mlink
->arch
)
2105 printf("/%s", mlink
->arch
);
2108 mlink
= mlink
->next
;
2110 fputs(", ", stdout
);
2112 printf(" - %s\n", mpage
->desc
);
2117 say(mlink
->file
, "Adding to database");
2121 mustfree
= render_string(&cp
, &i
);
2123 SQL_BIND_TEXT(stmts
[STMT_INSERT_PAGE
], i
, cp
);
2124 SQL_BIND_INT(stmts
[STMT_INSERT_PAGE
], i
, mpage
->form
);
2125 SQL_STEP(stmts
[STMT_INSERT_PAGE
]);
2126 mpage
->pageid
= sqlite3_last_insert_rowid(db
);
2127 sqlite3_reset(stmts
[STMT_INSERT_PAGE
]);
2131 while (NULL
!= mlink
) {
2133 mlink
= mlink
->next
;
2135 mlink
= mpage
->mlinks
;
2137 for (key
= ohash_first(&names
, &slot
); NULL
!= key
;
2138 key
= ohash_next(&names
, &slot
)) {
2139 assert(key
->mpage
== mpage
);
2141 SQL_BIND_INT64(stmts
[STMT_INSERT_NAME
], i
, key
->mask
);
2142 SQL_BIND_TEXT(stmts
[STMT_INSERT_NAME
], i
, key
->key
);
2143 SQL_BIND_INT64(stmts
[STMT_INSERT_NAME
], i
, mpage
->pageid
);
2144 SQL_STEP(stmts
[STMT_INSERT_NAME
]);
2145 sqlite3_reset(stmts
[STMT_INSERT_NAME
]);
2148 for (key
= ohash_first(&strings
, &slot
); NULL
!= key
;
2149 key
= ohash_next(&strings
, &slot
)) {
2150 assert(key
->mpage
== mpage
);
2152 SQL_BIND_INT64(stmts
[STMT_INSERT_KEY
], i
, key
->mask
);
2153 SQL_BIND_TEXT(stmts
[STMT_INSERT_KEY
], i
, key
->key
);
2154 SQL_BIND_INT64(stmts
[STMT_INSERT_KEY
], i
, mpage
->pageid
);
2155 SQL_STEP(stmts
[STMT_INSERT_KEY
]);
2156 sqlite3_reset(stmts
[STMT_INSERT_KEY
]);
2164 struct mpage
*mpage
;
2165 struct mlink
*mlink
;
2170 SQL_EXEC("BEGIN TRANSACTION");
2172 for (mpage
= ohash_first(&mpages
, &slot
); NULL
!= mpage
;
2173 mpage
= ohash_next(&mpages
, &slot
)) {
2174 mlink
= mpage
->mlinks
;
2176 say(mlink
->file
, "Deleting from database");
2179 for ( ; NULL
!= mlink
; mlink
= mlink
->next
) {
2181 SQL_BIND_TEXT(stmts
[STMT_DELETE_PAGE
],
2183 SQL_BIND_TEXT(stmts
[STMT_DELETE_PAGE
],
2185 SQL_BIND_TEXT(stmts
[STMT_DELETE_PAGE
],
2187 SQL_STEP(stmts
[STMT_DELETE_PAGE
]);
2188 sqlite3_reset(stmts
[STMT_DELETE_PAGE
]);
2193 SQL_EXEC("END TRANSACTION");
2197 * Close an existing database and its prepared statements.
2198 * If "real" is not set, rename the temporary file into the real one.
2210 for (i
= 0; i
< STMT__MAX
; i
++) {
2211 sqlite3_finalize(stmts
[i
]);
2221 if ('\0' == *tempfilename
) {
2222 if (-1 == rename(MANDOC_DB
"~", MANDOC_DB
)) {
2223 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2224 say(MANDOC_DB
, "&rename");
2229 switch (child
= fork()) {
2231 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2232 say("", "&fork cmp");
2235 execlp("cmp", "cmp", "-s",
2236 tempfilename
, MANDOC_DB
, (char *)NULL
);
2237 say("", "&exec cmp");
2242 if (-1 == waitpid(child
, &status
, 0)) {
2243 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2244 say("", "&wait cmp");
2245 } else if (WIFSIGNALED(status
)) {
2246 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2247 say("", "cmp died from signal %d", WTERMSIG(status
));
2248 } else if (WEXITSTATUS(status
)) {
2249 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2251 "Data changed, but cannot replace database");
2254 *strrchr(tempfilename
, '/') = '\0';
2255 switch (child
= fork()) {
2257 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2258 say("", "&fork rm");
2261 execlp("rm", "rm", "-rf", tempfilename
, (char *)NULL
);
2262 say("", "&exec rm");
2263 exit((int)MANDOCLEVEL_SYSERR
);
2267 if (-1 == waitpid(child
, &status
, 0)) {
2268 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2269 say("", "&wait rm");
2270 } else if (WIFSIGNALED(status
) || WEXITSTATUS(status
)) {
2271 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2272 say("", "%s: Cannot remove temporary directory",
2278 * This is straightforward stuff.
2279 * Open a database connection to a "temporary" database, then open a set
2280 * of prepared statements we'll use over and over again.
2281 * If "real" is set, we use the existing database; if not, we truncate a
2283 * Must be matched by dbclose().
2294 *tempfilename
= '\0';
2295 ofl
= SQLITE_OPEN_READWRITE
;
2298 rc
= sqlite3_open_v2(MANDOC_DB
, &db
, ofl
, NULL
);
2299 if (SQLITE_OK
!= rc
) {
2300 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2301 if (SQLITE_CANTOPEN
!= rc
)
2302 say(MANDOC_DB
, "%s", sqlite3_errstr(rc
));
2305 goto prepare_statements
;
2308 ofl
|= SQLITE_OPEN_CREATE
| SQLITE_OPEN_EXCLUSIVE
;
2310 remove(MANDOC_DB
"~");
2311 rc
= sqlite3_open_v2(MANDOC_DB
"~", &db
, ofl
, NULL
);
2312 if (SQLITE_OK
== rc
)
2314 if (MPARSE_QUICK
& mparse_options
) {
2315 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2316 say(MANDOC_DB
"~", "%s", sqlite3_errstr(rc
));
2320 (void)strlcpy(tempfilename
, "/tmp/mandocdb.XXXXXX",
2321 sizeof(tempfilename
));
2322 if (NULL
== mkdtemp(tempfilename
)) {
2323 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2324 say("", "&%s", tempfilename
);
2327 (void)strlcat(tempfilename
, "/" MANDOC_DB
,
2328 sizeof(tempfilename
));
2329 rc
= sqlite3_open_v2(tempfilename
, &db
, ofl
, NULL
);
2330 if (SQLITE_OK
!= rc
) {
2331 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2332 say("", "%s: %s", tempfilename
, sqlite3_errstr(rc
));
2337 sql
= "CREATE TABLE \"mpages\" (\n"
2338 " \"desc\" TEXT NOT NULL,\n"
2339 " \"form\" INTEGER NOT NULL,\n"
2340 " \"pageid\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
2343 "CREATE TABLE \"mlinks\" (\n"
2344 " \"sec\" TEXT NOT NULL,\n"
2345 " \"arch\" TEXT NOT NULL,\n"
2346 " \"name\" TEXT NOT NULL,\n"
2347 " \"pageid\" INTEGER NOT NULL REFERENCES mpages(pageid) "
2348 "ON DELETE CASCADE\n"
2350 "CREATE INDEX mlinks_pageid_idx ON mlinks (pageid);\n"
2352 "CREATE TABLE \"names\" (\n"
2353 " \"bits\" INTEGER NOT NULL,\n"
2354 " \"name\" TEXT NOT NULL,\n"
2355 " \"pageid\" INTEGER NOT NULL REFERENCES mpages(pageid) "
2356 "ON DELETE CASCADE,\n"
2357 " UNIQUE (\"name\", \"pageid\") ON CONFLICT REPLACE\n"
2360 "CREATE TABLE \"keys\" (\n"
2361 " \"bits\" INTEGER NOT NULL,\n"
2362 " \"key\" TEXT NOT NULL,\n"
2363 " \"pageid\" INTEGER NOT NULL REFERENCES mpages(pageid) "
2364 "ON DELETE CASCADE\n"
2366 "CREATE INDEX keys_pageid_idx ON keys (pageid);\n";
2368 if (SQLITE_OK
!= sqlite3_exec(db
, sql
, NULL
, NULL
, NULL
)) {
2369 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2370 say(MANDOC_DB
, "%s", sqlite3_errmsg(db
));
2376 if (SQLITE_OK
!= sqlite3_exec(db
,
2377 "PRAGMA foreign_keys = ON", NULL
, NULL
, NULL
)) {
2378 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2379 say(MANDOC_DB
, "PRAGMA foreign_keys: %s",
2380 sqlite3_errmsg(db
));
2385 sql
= "DELETE FROM mpages WHERE pageid IN "
2386 "(SELECT pageid FROM mlinks WHERE "
2387 "sec=? AND arch=? AND name=?)";
2388 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_DELETE_PAGE
], NULL
);
2389 sql
= "INSERT INTO mpages "
2390 "(desc,form) VALUES (?,?)";
2391 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_PAGE
], NULL
);
2392 sql
= "INSERT INTO mlinks "
2393 "(sec,arch,name,pageid) VALUES (?,?,?,?)";
2394 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_LINK
], NULL
);
2395 sql
= "SELECT bits FROM names where pageid = ?";
2396 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_SELECT_NAME
], NULL
);
2397 sql
= "INSERT INTO names "
2398 "(bits,name,pageid) VALUES (?,?,?)";
2399 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_NAME
], NULL
);
2400 sql
= "INSERT INTO keys "
2401 "(bits,key,pageid) VALUES (?,?,?)";
2402 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_KEY
], NULL
);
2406 * When opening a new database, we can turn off
2407 * synchronous mode for much better performance.
2410 if (real
&& SQLITE_OK
!= sqlite3_exec(db
,
2411 "PRAGMA synchronous = OFF", NULL
, NULL
, NULL
)) {
2412 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2413 say(MANDOC_DB
, "PRAGMA synchronous: %s",
2414 sqlite3_errmsg(db
));
2424 set_basedir(const char *targetdir
, int report_baddir
)
2426 static char startdir
[PATH_MAX
];
2427 static int getcwd_status
; /* 1 = ok, 2 = failure */
2428 static int chdir_status
; /* 1 = changed directory */
2432 * Remember the original working directory, if possible.
2433 * This will be needed if the second or a later directory
2434 * on the command line is given as a relative path.
2435 * Do not error out if the current directory is not
2436 * searchable: Maybe it won't be needed after all.
2438 if (0 == getcwd_status
) {
2439 if (NULL
== getcwd(startdir
, sizeof(startdir
))) {
2441 (void)strlcpy(startdir
, strerror(errno
),
2448 * We are leaving the old base directory.
2449 * Do not use it any longer, not even for messages.
2454 * If and only if the directory was changed earlier and
2455 * the next directory to process is given as a relative path,
2456 * first go back, or bail out if that is impossible.
2458 if (chdir_status
&& '/' != *targetdir
) {
2459 if (2 == getcwd_status
) {
2460 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2461 say("", "getcwd: %s", startdir
);
2464 if (-1 == chdir(startdir
)) {
2465 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2466 say("", "&chdir %s", startdir
);
2472 * Always resolve basedir to the canonicalized absolute
2473 * pathname and append a trailing slash, such that
2474 * we can reliably check whether files are inside.
2476 if (NULL
== realpath(targetdir
, basedir
)) {
2477 if (report_baddir
|| errno
!= ENOENT
) {
2478 exitcode
= (int)MANDOCLEVEL_BADARG
;
2479 say("", "&%s: realpath", targetdir
);
2482 } else if (-1 == chdir(basedir
)) {
2483 if (report_baddir
|| errno
!= ENOENT
) {
2484 exitcode
= (int)MANDOCLEVEL_BADARG
;
2490 cp
= strchr(basedir
, '\0');
2491 if ('/' != cp
[-1]) {
2492 if (cp
- basedir
>= PATH_MAX
- 1) {
2493 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2494 say("", "Filename too long");
2504 say(const char *file
, const char *format
, ...)
2509 if ('\0' != *basedir
)
2510 fprintf(stderr
, "%s", basedir
);
2511 if ('\0' != *basedir
&& '\0' != *file
)
2514 fprintf(stderr
, "%s", file
);
2517 if (NULL
!= format
) {
2530 if (NULL
!= format
) {
2531 if ('\0' != *basedir
|| '\0' != *file
)
2532 fputs(": ", stderr
);
2533 va_start(ap
, format
);
2534 vfprintf(stderr
, format
, ap
);
2538 if ('\0' != *basedir
|| '\0' != *file
|| NULL
!= format
)
2539 fputs(": ", stderr
);
2542 fputc('\n', stderr
);