]>
git.cameronkatri.com Git - mandoc.git/blob - mandocdb.c
1 /* $Id: mandocdb.c,v 1.193 2015/04/18 17:53:21 schwarze Exp $ */
3 * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011-2015 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>
31 #include "compat_fts.h"
45 #include "compat_ohash.h"
49 #include "mandoc_aux.h"
55 #include "mansearch.h"
57 extern int mansearch_keymax
;
58 extern const char *const mansearch_keynames
[];
60 #define SQL_EXEC(_v) \
61 if (SQLITE_OK != sqlite3_exec(db, (_v), NULL, NULL, NULL)) \
62 say("", "%s: %s", (_v), sqlite3_errmsg(db))
63 #define SQL_BIND_TEXT(_s, _i, _v) \
64 if (SQLITE_OK != sqlite3_bind_text \
65 ((_s), (_i)++, (_v), -1, SQLITE_STATIC)) \
66 say(mlink->file, "%s", sqlite3_errmsg(db))
67 #define SQL_BIND_INT(_s, _i, _v) \
68 if (SQLITE_OK != sqlite3_bind_int \
69 ((_s), (_i)++, (_v))) \
70 say(mlink->file, "%s", sqlite3_errmsg(db))
71 #define SQL_BIND_INT64(_s, _i, _v) \
72 if (SQLITE_OK != sqlite3_bind_int64 \
73 ((_s), (_i)++, (_v))) \
74 say(mlink->file, "%s", sqlite3_errmsg(db))
75 #define SQL_STEP(_s) \
76 if (SQLITE_DONE != sqlite3_step((_s))) \
77 say(mlink->file, "%s", sqlite3_errmsg(db))
80 OP_DEFAULT
= 0, /* new dbs from dir list or default config */
81 OP_CONFFILE
, /* new databases from custom config file */
82 OP_UPDATE
, /* delete/add entries in existing database */
83 OP_DELETE
, /* delete entries from existing database */
84 OP_TEST
/* change no databases, report potential problems */
88 const struct mpage
*mpage
; /* if set, the owning parse */
89 uint64_t mask
; /* bitmask in sequence */
90 char key
[]; /* rendered text */
99 struct inodev inodev
; /* used for hashing routine */
100 int64_t pageid
; /* pageid in mpages SQL table */
101 char *sec
; /* section from file content */
102 char *arch
; /* architecture from file content */
103 char *title
; /* title from file content */
104 char *desc
; /* description from file content */
105 struct mlink
*mlinks
; /* singly linked list */
106 int form
; /* format from file content */
111 char file
[PATH_MAX
]; /* filename rel. to manpath */
112 char *dsec
; /* section from directory */
113 char *arch
; /* architecture from directory */
114 char *name
; /* name from file name (not empty) */
115 char *fsec
; /* section from file name suffix */
116 struct mlink
*next
; /* singly linked list */
117 struct mpage
*mpage
; /* parent */
118 int dform
; /* format from directory */
119 int fform
; /* format from file name suffix */
120 int gzip
; /* filename has a .gz suffix */
124 STMT_DELETE_PAGE
= 0, /* delete mpage */
125 STMT_INSERT_PAGE
, /* insert mpage */
126 STMT_INSERT_LINK
, /* insert mlink */
127 STMT_INSERT_NAME
, /* insert name */
128 STMT_SELECT_NAME
, /* retrieve existing name flags */
129 STMT_INSERT_KEY
, /* insert parsed key */
133 typedef int (*mdoc_fp
)(struct mpage
*, const struct roff_meta
*,
134 const struct roff_node
*);
136 struct mdoc_handler
{
137 mdoc_fp fp
; /* optional handler */
138 uint64_t mask
; /* set unless handler returns 0 */
141 static void dbclose(int);
142 static void dbadd(struct mpage
*);
143 static void dbadd_mlink(const struct mlink
*mlink
);
144 static void dbadd_mlink_name(const struct mlink
*mlink
);
145 static int dbopen(int);
146 static void dbprune(void);
147 static void filescan(const char *);
148 static void *hash_alloc(size_t, void *);
149 static void hash_free(void *, void *);
150 static void *hash_calloc(size_t, size_t, void *);
151 static void mlink_add(struct mlink
*, const struct stat
*);
152 static void mlink_check(struct mpage
*, struct mlink
*);
153 static void mlink_free(struct mlink
*);
154 static void mlinks_undupe(struct mpage
*);
155 static void mpages_free(void);
156 static void mpages_merge(struct mparse
*);
157 static void names_check(void);
158 static void parse_cat(struct mpage
*, int);
159 static void parse_man(struct mpage
*, const struct roff_meta
*,
160 const struct roff_node
*);
161 static void parse_mdoc(struct mpage
*, const struct roff_meta
*,
162 const struct roff_node
*);
163 static int parse_mdoc_body(struct mpage
*, const struct roff_meta
*,
164 const struct roff_node
*);
165 static int parse_mdoc_head(struct mpage
*, const struct roff_meta
*,
166 const struct roff_node
*);
167 static int parse_mdoc_Fd(struct mpage
*, const struct roff_meta
*,
168 const struct roff_node
*);
169 static void parse_mdoc_fname(struct mpage
*, const struct roff_node
*);
170 static int parse_mdoc_Fn(struct mpage
*, const struct roff_meta
*,
171 const struct roff_node
*);
172 static int parse_mdoc_Fo(struct mpage
*, const struct roff_meta
*,
173 const struct roff_node
*);
174 static int parse_mdoc_Nd(struct mpage
*, const struct roff_meta
*,
175 const struct roff_node
*);
176 static int parse_mdoc_Nm(struct mpage
*, const struct roff_meta
*,
177 const struct roff_node
*);
178 static int parse_mdoc_Sh(struct mpage
*, const struct roff_meta
*,
179 const struct roff_node
*);
180 static int parse_mdoc_Xr(struct mpage
*, const struct roff_meta
*,
181 const struct roff_node
*);
182 static void putkey(const struct mpage
*, char *, uint64_t);
183 static void putkeys(const struct mpage
*, char *, size_t, uint64_t);
184 static void putmdockey(const struct mpage
*,
185 const struct roff_node
*, uint64_t);
186 static int render_string(char **, size_t *);
187 static void say(const char *, const char *, ...);
188 static int set_basedir(const char *, int);
189 static int treescan(void);
190 static size_t utf8(unsigned int, char [7]);
192 static char tempfilename
[32];
193 static char *progname
;
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 mchars
*mchars
; /* table of named characters */
204 static struct ohash mpages
; /* table of distinct manual pages */
205 static struct ohash mlinks
; /* table of directory entries */
206 static struct ohash names
; /* table of all names */
207 static struct ohash strings
; /* table of all strings */
208 static sqlite3
*db
= NULL
; /* current database */
209 static sqlite3_stmt
*stmts
[STMT__MAX
]; /* current statements */
210 static uint64_t name_mask
;
212 static const struct mdoc_handler mdocs
[MDOC_MAX
] = {
213 { NULL
, 0 }, /* Ap */
214 { NULL
, 0 }, /* Dd */
215 { NULL
, 0 }, /* Dt */
216 { NULL
, 0 }, /* Os */
217 { parse_mdoc_Sh
, TYPE_Sh
}, /* Sh */
218 { parse_mdoc_head
, TYPE_Ss
}, /* Ss */
219 { NULL
, 0 }, /* Pp */
220 { NULL
, 0 }, /* D1 */
221 { NULL
, 0 }, /* Dl */
222 { NULL
, 0 }, /* Bd */
223 { NULL
, 0 }, /* Ed */
224 { NULL
, 0 }, /* Bl */
225 { NULL
, 0 }, /* El */
226 { NULL
, 0 }, /* It */
227 { NULL
, 0 }, /* Ad */
228 { NULL
, TYPE_An
}, /* An */
229 { NULL
, TYPE_Ar
}, /* Ar */
230 { NULL
, TYPE_Cd
}, /* Cd */
231 { NULL
, TYPE_Cm
}, /* Cm */
232 { NULL
, TYPE_Dv
}, /* Dv */
233 { NULL
, TYPE_Er
}, /* Er */
234 { NULL
, TYPE_Ev
}, /* Ev */
235 { NULL
, 0 }, /* Ex */
236 { NULL
, TYPE_Fa
}, /* Fa */
237 { parse_mdoc_Fd
, 0 }, /* Fd */
238 { NULL
, TYPE_Fl
}, /* Fl */
239 { parse_mdoc_Fn
, 0 }, /* Fn */
240 { NULL
, TYPE_Ft
}, /* Ft */
241 { NULL
, TYPE_Ic
}, /* Ic */
242 { NULL
, TYPE_In
}, /* In */
243 { NULL
, TYPE_Li
}, /* Li */
244 { parse_mdoc_Nd
, 0 }, /* Nd */
245 { parse_mdoc_Nm
, 0 }, /* Nm */
246 { NULL
, 0 }, /* Op */
247 { NULL
, 0 }, /* Ot */
248 { NULL
, TYPE_Pa
}, /* Pa */
249 { NULL
, 0 }, /* Rv */
250 { NULL
, TYPE_St
}, /* St */
251 { NULL
, TYPE_Va
}, /* Va */
252 { parse_mdoc_body
, TYPE_Va
}, /* Vt */
253 { parse_mdoc_Xr
, 0 }, /* Xr */
254 { NULL
, 0 }, /* %A */
255 { NULL
, 0 }, /* %B */
256 { NULL
, 0 }, /* %D */
257 { NULL
, 0 }, /* %I */
258 { NULL
, 0 }, /* %J */
259 { NULL
, 0 }, /* %N */
260 { NULL
, 0 }, /* %O */
261 { NULL
, 0 }, /* %P */
262 { NULL
, 0 }, /* %R */
263 { NULL
, 0 }, /* %T */
264 { NULL
, 0 }, /* %V */
265 { NULL
, 0 }, /* Ac */
266 { NULL
, 0 }, /* Ao */
267 { NULL
, 0 }, /* Aq */
268 { NULL
, TYPE_At
}, /* At */
269 { NULL
, 0 }, /* Bc */
270 { NULL
, 0 }, /* Bf */
271 { NULL
, 0 }, /* Bo */
272 { NULL
, 0 }, /* Bq */
273 { NULL
, TYPE_Bsx
}, /* Bsx */
274 { NULL
, TYPE_Bx
}, /* Bx */
275 { NULL
, 0 }, /* Db */
276 { NULL
, 0 }, /* Dc */
277 { NULL
, 0 }, /* Do */
278 { NULL
, 0 }, /* Dq */
279 { NULL
, 0 }, /* Ec */
280 { NULL
, 0 }, /* Ef */
281 { NULL
, TYPE_Em
}, /* Em */
282 { NULL
, 0 }, /* Eo */
283 { NULL
, TYPE_Fx
}, /* Fx */
284 { NULL
, TYPE_Ms
}, /* Ms */
285 { NULL
, 0 }, /* No */
286 { NULL
, 0 }, /* Ns */
287 { NULL
, TYPE_Nx
}, /* Nx */
288 { NULL
, TYPE_Ox
}, /* Ox */
289 { NULL
, 0 }, /* Pc */
290 { NULL
, 0 }, /* Pf */
291 { NULL
, 0 }, /* Po */
292 { NULL
, 0 }, /* Pq */
293 { NULL
, 0 }, /* Qc */
294 { NULL
, 0 }, /* Ql */
295 { NULL
, 0 }, /* Qo */
296 { NULL
, 0 }, /* Qq */
297 { NULL
, 0 }, /* Re */
298 { NULL
, 0 }, /* Rs */
299 { NULL
, 0 }, /* Sc */
300 { NULL
, 0 }, /* So */
301 { NULL
, 0 }, /* Sq */
302 { NULL
, 0 }, /* Sm */
303 { NULL
, 0 }, /* Sx */
304 { NULL
, TYPE_Sy
}, /* Sy */
305 { NULL
, TYPE_Tn
}, /* Tn */
306 { NULL
, 0 }, /* Ux */
307 { NULL
, 0 }, /* Xc */
308 { NULL
, 0 }, /* Xo */
309 { parse_mdoc_Fo
, 0 }, /* Fo */
310 { NULL
, 0 }, /* Fc */
311 { NULL
, 0 }, /* Oo */
312 { NULL
, 0 }, /* Oc */
313 { NULL
, 0 }, /* Bk */
314 { NULL
, 0 }, /* Ek */
315 { NULL
, 0 }, /* Bt */
316 { NULL
, 0 }, /* Hf */
317 { NULL
, 0 }, /* Fr */
318 { NULL
, 0 }, /* Ud */
319 { NULL
, TYPE_Lb
}, /* Lb */
320 { NULL
, 0 }, /* Lp */
321 { NULL
, TYPE_Lk
}, /* Lk */
322 { NULL
, TYPE_Mt
}, /* Mt */
323 { NULL
, 0 }, /* Brq */
324 { NULL
, 0 }, /* Bro */
325 { NULL
, 0 }, /* Brc */
326 { NULL
, 0 }, /* %C */
327 { NULL
, 0 }, /* Es */
328 { NULL
, 0 }, /* En */
329 { NULL
, TYPE_Dx
}, /* Dx */
330 { NULL
, 0 }, /* %Q */
331 { NULL
, 0 }, /* br */
332 { NULL
, 0 }, /* sp */
333 { NULL
, 0 }, /* %U */
334 { NULL
, 0 }, /* Ta */
335 { NULL
, 0 }, /* ll */
340 mandocdb(int argc
, char *argv
[])
343 struct ohash_info mpages_info
, mlinks_info
;
345 const char *path_arg
;
349 memset(&conf
, 0, sizeof(conf
));
350 memset(stmts
, 0, STMT__MAX
* sizeof(sqlite3_stmt
*));
352 mpages_info
.alloc
= mlinks_info
.alloc
= hash_alloc
;
353 mpages_info
.calloc
= mlinks_info
.calloc
= hash_calloc
;
354 mpages_info
.free
= mlinks_info
.free
= hash_free
;
355 mpages_info
.data
= mlinks_info
.data
= NULL
;
357 mpages_info
.key_offset
= offsetof(struct mpage
, inodev
);
358 mlinks_info
.key_offset
= offsetof(struct mlink
, file
);
360 progname
= strrchr(argv
[0], '/');
361 if (progname
== NULL
)
367 * We accept a few different invocations.
368 * The CHECKOP macro makes sure that invocation styles don't
369 * clobber each other.
371 #define CHECKOP(_op, _ch) do \
372 if (OP_DEFAULT != (_op)) { \
373 fprintf(stderr, "%s: -%c: Conflicting option\n", \
376 } while (/*CONSTCOND*/0)
381 while (-1 != (ch
= getopt(argc
, argv
, "aC:Dd:npQT:tu:v")))
406 mparse_options
|= MPARSE_QUICK
;
409 if (strcmp(optarg
, "utf8")) {
410 fprintf(stderr
, "%s: -T%s: "
411 "Unsupported output format\n",
419 dup2(STDOUT_FILENO
, STDERR_FILENO
);
429 /* Compatibility with espie@'s makewhatis. */
438 if (OP_CONFFILE
== op
&& argc
> 0) {
439 fprintf(stderr
, "%s: -C: Too many arguments\n",
444 exitcode
= (int)MANDOCLEVEL_OK
;
445 mchars
= mchars_alloc();
446 mp
= mparse_alloc(mparse_options
, MANDOCLEVEL_BADARG
, NULL
,
448 ohash_init(&mpages
, 6, &mpages_info
);
449 ohash_init(&mlinks
, 6, &mlinks_info
);
451 if (OP_UPDATE
== op
|| OP_DELETE
== op
|| OP_TEST
== op
) {
454 * Most of these deal with a specific directory.
455 * Jump into that directory first.
457 if (OP_TEST
!= op
&& 0 == set_basedir(path_arg
, 1))
462 * The existing database is usable. Process
463 * all files specified on the command-line.
466 for (i
= 0; i
< argc
; i
++)
472 * Database missing or corrupt.
473 * Recreate from scratch.
475 exitcode
= (int)MANDOCLEVEL_OK
;
484 dbclose(OP_DEFAULT
== op
? 0 : 1);
487 * If we have arguments, use them as our manpaths.
488 * If we don't, grok from manpath(1) or however else
489 * manconf_parse() wants to do it.
492 conf
.manpath
.paths
= mandoc_reallocarray(NULL
,
493 argc
, sizeof(char *));
494 conf
.manpath
.sz
= (size_t)argc
;
495 for (i
= 0; i
< argc
; i
++)
496 conf
.manpath
.paths
[i
] = mandoc_strdup(argv
[i
]);
498 manconf_parse(&conf
, path_arg
, NULL
, NULL
);
500 if (conf
.manpath
.sz
== 0) {
501 exitcode
= (int)MANDOCLEVEL_BADARG
;
502 say("", "Empty manpath");
506 * First scan the tree rooted at a base directory, then
507 * build a new database and finally move it into place.
508 * Ignore zero-length directories and strip trailing
511 for (j
= 0; j
< conf
.manpath
.sz
; j
++) {
512 sz
= strlen(conf
.manpath
.paths
[j
]);
513 if (sz
&& conf
.manpath
.paths
[j
][sz
- 1] == '/')
514 conf
.manpath
.paths
[j
][--sz
] = '\0';
519 ohash_init(&mpages
, 6, &mpages_info
);
520 ohash_init(&mlinks
, 6, &mlinks_info
);
523 if ( ! set_basedir(conf
.manpath
.paths
[j
], argc
> 0))
531 if (warnings
&& !nodb
&&
532 ! (MPARSE_QUICK
& mparse_options
))
536 if (j
+ 1 < conf
.manpath
.sz
) {
538 ohash_delete(&mpages
);
539 ohash_delete(&mlinks
);
548 ohash_delete(&mpages
);
549 ohash_delete(&mlinks
);
552 fprintf(stderr
, "usage: %s [-aDnpQ] [-C file] [-Tutf8]\n"
553 " %s [-aDnpQ] [-Tutf8] dir ...\n"
554 " %s [-DnpQ] [-Tutf8] -d dir [file ...]\n"
555 " %s [-Dnp] -u dir [file ...]\n"
556 " %s [-Q] -t file ...\n",
557 progname
, progname
, progname
,
560 return((int)MANDOCLEVEL_BADARG
);
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: accomodate for multi-language directories.
585 char *dsec
, *arch
, *fsec
, *cp
;
590 argv
[1] = (char *)NULL
;
592 f
= fts_open((char * const *)argv
,
593 FTS_PHYSICAL
| FTS_NOCHDIR
, NULL
);
595 exitcode
= (int)MANDOCLEVEL_SYSERR
;
596 say("", "&fts_open");
603 while (NULL
!= (ff
= fts_read(f
))) {
604 path
= ff
->fts_path
+ 2;
605 switch (ff
->fts_info
) {
608 * Symbolic links require various sanity checks,
609 * then get handled just like regular files.
612 if (NULL
== realpath(path
, buf
)) {
614 say(path
, "&realpath");
617 if (strstr(buf
, basedir
) != buf
619 && strstr(buf
, HOMEBREWDIR
) != buf
622 if (warnings
) say("",
623 "%s: outside base directory", buf
);
626 /* Use logical inode to avoid mpages dupe. */
627 if (-1 == stat(path
, ff
->fts_statp
)) {
635 * If we're a regular file, add an mlink by using the
636 * stored directory data and handling the filename.
639 if (0 == strcmp(path
, MANDOC_DB
))
641 if ( ! use_all
&& ff
->fts_level
< 2) {
643 say(path
, "Extraneous file");
648 while (NULL
== fsec
) {
649 fsec
= strrchr(ff
->fts_name
, '.');
650 if (NULL
== fsec
|| strcmp(fsec
+1, "gz"))
660 "No filename suffix");
663 } else if (0 == strcmp(++fsec
, "html")) {
665 say(path
, "Skip html");
667 } else if (0 == strcmp(fsec
, "ps")) {
669 say(path
, "Skip ps");
671 } else if (0 == strcmp(fsec
, "pdf")) {
673 say(path
, "Skip pdf");
675 } else if ( ! use_all
&&
676 ((FORM_SRC
== dform
&&
677 strncmp(fsec
, dsec
, strlen(dsec
))) ||
678 (FORM_CAT
== dform
&& strcmp(fsec
, "0")))) {
680 say(path
, "Wrong filename suffix");
685 mlink
= mandoc_calloc(1, sizeof(struct mlink
));
686 if (strlcpy(mlink
->file
, path
,
687 sizeof(mlink
->file
)) >=
688 sizeof(mlink
->file
)) {
689 say(path
, "Filename too long");
693 mlink
->dform
= dform
;
696 mlink
->name
= ff
->fts_name
;
699 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 (FTS_DP
== ff
->fts_info
)
727 if (0 == strncmp(cp
, "man", 3)) {
730 } else if (0 == strncmp(cp
, "cat", 3)) {
738 if (NULL
!= dsec
|| use_all
)
742 say(path
, "Unknown directory part");
743 fts_set(f
, ff
, FTS_SKIP
);
747 * Possibly our architecture.
748 * If we're descending, keep tabs on it.
750 if (FTS_DP
!= ff
->fts_info
&& NULL
!= dsec
)
756 if (FTS_DP
== ff
->fts_info
|| use_all
)
759 say(path
, "Extraneous directory part");
760 fts_set(f
, ff
, FTS_SKIP
);
770 * Add a file to the mlinks table.
771 * Do not verify that it's a "valid" looking manpage (we'll do that
774 * Try to infer the manual section, architecture, and page name from the
775 * path, assuming it looks like
777 * [./]man*[/<arch>]/<name>.<section>
779 * [./]cat<section>[/<arch>]/<name>.0
781 * See treescan() for the fts(3) version of this.
784 filescan(const char *file
)
793 if (0 == strncmp(file
, "./", 2))
797 * We have to do lstat(2) before realpath(3) loses
798 * the information whether this is a symbolic link.
799 * We need to know that because for symbolic links,
800 * we want to use the orginal file name, while for
801 * regular files, we want to use the real path.
803 if (-1 == lstat(file
, &st
)) {
804 exitcode
= (int)MANDOCLEVEL_BADARG
;
807 } else if (0 == ((S_IFREG
| S_IFLNK
) & st
.st_mode
)) {
808 exitcode
= (int)MANDOCLEVEL_BADARG
;
809 say(file
, "Not a regular file");
814 * We have to resolve the file name to the real path
815 * in any case for the base directory check.
817 if (NULL
== realpath(file
, buf
)) {
818 exitcode
= (int)MANDOCLEVEL_BADARG
;
819 say(file
, "&realpath");
825 else if (strstr(buf
, basedir
) == buf
)
826 start
= buf
+ strlen(basedir
);
828 else if (strstr(buf
, HOMEBREWDIR
) == buf
)
832 exitcode
= (int)MANDOCLEVEL_BADARG
;
833 say("", "%s: outside base directory", buf
);
838 * Now we are sure the file is inside our tree.
839 * If it is a symbolic link, ignore the real path
840 * and use the original name.
841 * This implies passing stuff like "cat1/../man1/foo.1"
842 * on the command line won't work. So don't do that.
843 * Note the stat(2) can still fail if the link target
846 if (S_IFLNK
& st
.st_mode
) {
847 if (-1 == stat(buf
, &st
)) {
848 exitcode
= (int)MANDOCLEVEL_BADARG
;
852 if (strlcpy(buf
, file
, sizeof(buf
)) >= sizeof(buf
)) {
853 say(file
, "Filename too long");
857 if (OP_TEST
!= op
&& strstr(buf
, basedir
) == buf
)
858 start
+= strlen(basedir
);
861 mlink
= mandoc_calloc(1, sizeof(struct mlink
));
862 mlink
->dform
= FORM_NONE
;
863 if (strlcpy(mlink
->file
, start
, sizeof(mlink
->file
)) >=
864 sizeof(mlink
->file
)) {
865 say(start
, "Filename too long");
871 * First try to guess our directory structure.
872 * If we find a separator, try to look for man* or cat*.
873 * If we find one of these and what's underneath is a directory,
874 * assume it's an architecture.
876 if (NULL
!= (p
= strchr(start
, '/'))) {
878 if (0 == strncmp(start
, "man", 3)) {
879 mlink
->dform
= FORM_SRC
;
880 mlink
->dsec
= start
+ 3;
881 } else if (0 == strncmp(start
, "cat", 3)) {
882 mlink
->dform
= FORM_CAT
;
883 mlink
->dsec
= start
+ 3;
887 if (NULL
!= mlink
->dsec
&& NULL
!= (p
= strchr(start
, '/'))) {
895 * Now check the file suffix.
896 * Suffix of `.0' indicates a catpage, `.1-9' is a manpage.
898 p
= strrchr(start
, '\0');
899 while (p
-- > start
&& '/' != *p
&& '.' != *p
)
908 * Now try to parse the name.
909 * Use the filename portion of the path.
912 if (NULL
!= (p
= strrchr(start
, '/'))) {
916 mlink_add(mlink
, &st
);
920 mlink_add(struct mlink
*mlink
, const struct stat
*st
)
922 struct inodev inodev
;
926 assert(NULL
!= mlink
->file
);
928 mlink
->dsec
= mandoc_strdup(mlink
->dsec
? mlink
->dsec
: "");
929 mlink
->arch
= mandoc_strdup(mlink
->arch
? mlink
->arch
: "");
930 mlink
->name
= mandoc_strdup(mlink
->name
? mlink
->name
: "");
931 mlink
->fsec
= mandoc_strdup(mlink
->fsec
? mlink
->fsec
: "");
933 if ('0' == *mlink
->fsec
) {
935 mlink
->fsec
= mandoc_strdup(mlink
->dsec
);
936 mlink
->fform
= FORM_CAT
;
937 } else if ('1' <= *mlink
->fsec
&& '9' >= *mlink
->fsec
)
938 mlink
->fform
= FORM_SRC
;
940 mlink
->fform
= FORM_NONE
;
942 slot
= ohash_qlookup(&mlinks
, mlink
->file
);
943 assert(NULL
== ohash_find(&mlinks
, slot
));
944 ohash_insert(&mlinks
, slot
, mlink
);
946 memset(&inodev
, 0, sizeof(inodev
)); /* Clear padding. */
947 inodev
.st_ino
= st
->st_ino
;
948 inodev
.st_dev
= st
->st_dev
;
949 slot
= ohash_lookup_memory(&mpages
, (char *)&inodev
,
950 sizeof(struct inodev
), inodev
.st_ino
);
951 mpage
= ohash_find(&mpages
, slot
);
953 mpage
= mandoc_calloc(1, sizeof(struct mpage
));
954 mpage
->inodev
.st_ino
= inodev
.st_ino
;
955 mpage
->inodev
.st_dev
= inodev
.st_dev
;
956 ohash_insert(&mpages
, slot
, mpage
);
958 mlink
->next
= mpage
->mlinks
;
959 mpage
->mlinks
= mlink
;
960 mlink
->mpage
= mpage
;
964 mlink_free(struct mlink
*mlink
)
981 mpage
= ohash_first(&mpages
, &slot
);
982 while (NULL
!= mpage
) {
983 while (NULL
!= (mlink
= mpage
->mlinks
)) {
984 mpage
->mlinks
= mlink
->next
;
992 mpage
= ohash_next(&mpages
, &slot
);
997 * For each mlink to the mpage, check whether the path looks like
998 * it is formatted, and if it does, check whether a source manual
999 * exists by the same name, ignoring the suffix.
1000 * If both conditions hold, drop the mlink.
1003 mlinks_undupe(struct mpage
*mpage
)
1006 struct mlink
**prev
;
1007 struct mlink
*mlink
;
1010 mpage
->form
= FORM_CAT
;
1011 prev
= &mpage
->mlinks
;
1012 while (NULL
!= (mlink
= *prev
)) {
1013 if (FORM_CAT
!= mlink
->dform
) {
1014 mpage
->form
= FORM_NONE
;
1017 (void)strlcpy(buf
, mlink
->file
, sizeof(buf
));
1018 bufp
= strstr(buf
, "cat");
1019 assert(NULL
!= bufp
);
1020 memcpy(bufp
, "man", 3);
1021 if (NULL
!= (bufp
= strrchr(buf
, '.')))
1023 (void)strlcat(buf
, mlink
->dsec
, sizeof(buf
));
1024 if (NULL
== ohash_find(&mlinks
,
1025 ohash_qlookup(&mlinks
, buf
)))
1028 say(mlink
->file
, "Man source exists: %s", buf
);
1031 *prev
= mlink
->next
;
1035 prev
= &(*prev
)->next
;
1040 mlink_check(struct mpage
*mpage
, struct mlink
*mlink
)
1046 * Check whether the manual section given in a file
1047 * agrees with the directory where the file is located.
1048 * Some manuals have suffixes like (3p) on their
1049 * section number either inside the file or in the
1050 * directory name, some are linked into more than one
1051 * section, like encrypt(1) = makekey(8).
1054 if (FORM_SRC
== mpage
->form
&&
1055 strcasecmp(mpage
->sec
, mlink
->dsec
))
1056 say(mlink
->file
, "Section \"%s\" manual in %s directory",
1057 mpage
->sec
, mlink
->dsec
);
1060 * Manual page directories exist for each kernel
1061 * architecture as returned by machine(1).
1062 * However, many manuals only depend on the
1063 * application architecture as returned by arch(1).
1064 * For example, some (2/ARM) manuals are shared
1065 * across the "armish" and "zaurus" kernel
1067 * A few manuals are even shared across completely
1068 * different architectures, for example fdformat(1)
1069 * on amd64, i386, sparc, and sparc64.
1072 if (strcasecmp(mpage
->arch
, mlink
->arch
))
1073 say(mlink
->file
, "Architecture \"%s\" manual in "
1074 "\"%s\" directory", mpage
->arch
, mlink
->arch
);
1078 * parse_cat() doesn't set NAME_TITLE yet.
1081 if (FORM_CAT
== mpage
->form
)
1085 * Check whether this mlink
1086 * appears as a name in the NAME section.
1089 slot
= ohash_qlookup(&names
, mlink
->name
);
1090 str
= ohash_find(&names
, slot
);
1091 assert(NULL
!= str
);
1092 if ( ! (NAME_TITLE
& str
->mask
))
1093 say(mlink
->file
, "Name missing in NAME section");
1097 * Run through the files in the global vector "mpages"
1098 * and add them to the database specified in "basedir".
1100 * This handles the parsing scheme itself, using the cues of directory
1101 * and filename to determine whether the file is parsable or not.
1104 mpages_merge(struct mparse
*mp
)
1107 struct ohash_info str_info
;
1108 struct mpage
*mpage
, *mpage_dest
;
1109 struct mlink
*mlink
, *mlink_dest
;
1110 struct roff_man
*man
;
1116 str_info
.alloc
= hash_alloc
;
1117 str_info
.calloc
= hash_calloc
;
1118 str_info
.free
= hash_free
;
1119 str_info
.data
= NULL
;
1120 str_info
.key_offset
= offsetof(struct str
, key
);
1123 SQL_EXEC("BEGIN TRANSACTION");
1125 mpage
= ohash_first(&mpages
, &pslot
);
1126 while (mpage
!= NULL
) {
1127 mlinks_undupe(mpage
);
1128 if ((mlink
= mpage
->mlinks
) == NULL
) {
1129 mpage
= ohash_next(&mpages
, &pslot
);
1133 name_mask
= NAME_MASK
;
1134 ohash_init(&names
, 4, &str_info
);
1135 ohash_init(&strings
, 6, &str_info
);
1140 mparse_open(mp
, &fd
, mlink
->file
);
1142 say(mlink
->file
, "&open");
1147 * Interpret the file as mdoc(7) or man(7) source
1148 * code, unless it is known to be formatted.
1150 if (mlink
->dform
!= FORM_CAT
|| mlink
->fform
!= FORM_CAT
) {
1151 mparse_readfd(mp
, fd
, mlink
->file
);
1152 mparse_result(mp
, &man
, &sodest
);
1155 if (sodest
!= NULL
) {
1156 mlink_dest
= ohash_find(&mlinks
,
1157 ohash_qlookup(&mlinks
, sodest
));
1158 if (mlink_dest
== NULL
) {
1159 mandoc_asprintf(&cp
, "%s.gz", sodest
);
1160 mlink_dest
= ohash_find(&mlinks
,
1161 ohash_qlookup(&mlinks
, cp
));
1164 if (mlink_dest
!= NULL
) {
1166 /* The .so target exists. */
1168 mpage_dest
= mlink_dest
->mpage
;
1170 mlink
->mpage
= mpage_dest
;
1173 * If the target was already
1174 * processed, add the links
1175 * to the database now.
1176 * Otherwise, this will
1177 * happen when we come
1181 if (mpage_dest
->pageid
)
1182 dbadd_mlink_name(mlink
);
1184 if (mlink
->next
== NULL
)
1186 mlink
= mlink
->next
;
1189 /* Move all links to the target. */
1191 mlink
->next
= mlink_dest
->next
;
1192 mlink_dest
->next
= mpage
->mlinks
;
1193 mpage
->mlinks
= NULL
;
1196 } else if (man
!= NULL
&& man
->macroset
== MACROSET_MDOC
) {
1197 mpage
->form
= FORM_SRC
;
1198 mpage
->sec
= man
->meta
.msec
;
1199 mpage
->sec
= mandoc_strdup(
1200 mpage
->sec
== NULL
? "" : mpage
->sec
);
1201 mpage
->arch
= man
->meta
.arch
;
1202 mpage
->arch
= mandoc_strdup(
1203 mpage
->arch
== NULL
? "" : mpage
->arch
);
1204 mpage
->title
= mandoc_strdup(man
->meta
.title
);
1205 } else if (man
!= NULL
&& man
->macroset
== MACROSET_MAN
) {
1206 mpage
->form
= FORM_SRC
;
1207 mpage
->sec
= mandoc_strdup(man
->meta
.msec
);
1208 mpage
->arch
= mandoc_strdup(mlink
->arch
);
1209 mpage
->title
= mandoc_strdup(man
->meta
.title
);
1211 mpage
->form
= FORM_CAT
;
1212 mpage
->sec
= mandoc_strdup(mlink
->dsec
);
1213 mpage
->arch
= mandoc_strdup(mlink
->arch
);
1214 mpage
->title
= mandoc_strdup(mlink
->name
);
1216 putkey(mpage
, mpage
->sec
, TYPE_sec
);
1217 if (*mpage
->arch
!= '\0')
1218 putkey(mpage
, mpage
->arch
, TYPE_arch
);
1220 for ( ; mlink
!= NULL
; mlink
= mlink
->next
) {
1221 if ('\0' != *mlink
->dsec
)
1222 putkey(mpage
, mlink
->dsec
, TYPE_sec
);
1223 if ('\0' != *mlink
->fsec
)
1224 putkey(mpage
, mlink
->fsec
, TYPE_sec
);
1225 putkey(mpage
, '\0' == *mlink
->arch
?
1226 any
: mlink
->arch
, TYPE_arch
);
1227 putkey(mpage
, mlink
->name
, NAME_FILE
);
1230 assert(mpage
->desc
== NULL
);
1231 if (man
!= NULL
&& man
->macroset
== MACROSET_MDOC
)
1232 parse_mdoc(mpage
, &man
->meta
, man
->first
);
1233 else if (man
!= NULL
)
1234 parse_man(mpage
, &man
->meta
, man
->first
);
1236 parse_cat(mpage
, fd
);
1237 if (mpage
->desc
== NULL
)
1238 mpage
->desc
= mandoc_strdup(mpage
->mlinks
->name
);
1240 if (warnings
&& !use_all
)
1241 for (mlink
= mpage
->mlinks
; mlink
;
1242 mlink
= mlink
->next
)
1243 mlink_check(mpage
, mlink
);
1246 mlink
= mpage
->mlinks
;
1249 if (mparse_wait(mp
) != MANDOCLEVEL_OK
) {
1250 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1251 say(mlink
->file
, "&wait gunzip");
1253 ohash_delete(&strings
);
1254 ohash_delete(&names
);
1255 mpage
= ohash_next(&mpages
, &pslot
);
1259 SQL_EXEC("END TRANSACTION");
1266 const char *name
, *sec
, *arch
, *key
;
1269 sqlite3_prepare_v2(db
,
1270 "SELECT name, sec, arch, key FROM ("
1271 "SELECT name AS key, pageid FROM names "
1272 "WHERE bits & ? AND NOT EXISTS ("
1273 "SELECT pageid FROM mlinks "
1274 "WHERE mlinks.pageid == names.pageid "
1275 "AND mlinks.name == names.name"
1278 "SELECT sec, arch, name, pageid FROM mlinks "
1280 ") USING (pageid);",
1283 if (SQLITE_OK
!= sqlite3_bind_int64(stmt
, 1, NAME_TITLE
))
1284 say("", "%s", sqlite3_errmsg(db
));
1286 while (SQLITE_ROW
== (irc
= sqlite3_step(stmt
))) {
1287 name
= (const char *)sqlite3_column_text(stmt
, 0);
1288 sec
= (const char *)sqlite3_column_text(stmt
, 1);
1289 arch
= (const char *)sqlite3_column_text(stmt
, 2);
1290 key
= (const char *)sqlite3_column_text(stmt
, 3);
1291 say("", "%s(%s%s%s) lacks mlink \"%s\"", name
, sec
,
1292 '\0' == *arch
? "" : "/",
1293 '\0' == *arch
? "" : arch
, key
);
1295 sqlite3_finalize(stmt
);
1299 parse_cat(struct mpage
*mpage
, int fd
)
1302 char *line
, *p
, *title
;
1303 size_t len
, plen
, titlesz
;
1305 stream
= (-1 == fd
) ?
1306 fopen(mpage
->mlinks
->file
, "r") :
1308 if (NULL
== stream
) {
1312 say(mpage
->mlinks
->file
, "&fopen");
1316 /* Skip to first blank line. */
1318 while (NULL
!= (line
= fgetln(stream
, &len
)))
1323 * Assume the first line that is not indented
1324 * is the first section header. Skip to it.
1327 while (NULL
!= (line
= fgetln(stream
, &len
)))
1328 if ('\n' != *line
&& ' ' != *line
)
1332 * Read up until the next section into a buffer.
1333 * Strip the leading and trailing newline from each read line,
1334 * appending a trailing space.
1335 * Ignore empty (whitespace-only) lines.
1341 while (NULL
!= (line
= fgetln(stream
, &len
))) {
1342 if (' ' != *line
|| '\n' != line
[len
- 1])
1344 while (len
> 0 && isspace((unsigned char)*line
)) {
1350 title
= mandoc_realloc(title
, titlesz
+ len
);
1351 memcpy(title
+ titlesz
, line
, len
);
1353 title
[titlesz
- 1] = ' ';
1357 * If no page content can be found, or the input line
1358 * is already the next section header, or there is no
1359 * trailing newline, reuse the page title as the page
1363 if (NULL
== title
|| '\0' == *title
) {
1365 say(mpage
->mlinks
->file
,
1366 "Cannot find NAME section");
1372 title
= mandoc_realloc(title
, titlesz
+ 1);
1373 title
[titlesz
] = '\0';
1376 * Skip to the first dash.
1377 * Use the remaining line as the description (no more than 70
1381 if (NULL
!= (p
= strstr(title
, "- "))) {
1382 for (p
+= 2; ' ' == *p
|| '\b' == *p
; p
++)
1383 /* Skip to next word. */ ;
1386 say(mpage
->mlinks
->file
,
1387 "No dash in title line");
1393 /* Strip backspace-encoding from line. */
1395 while (NULL
!= (line
= memchr(p
, '\b', plen
))) {
1398 memmove(line
, line
+ 1, plen
--);
1401 memmove(line
- 1, line
+ 1, plen
- len
);
1405 mpage
->desc
= mandoc_strdup(p
);
1411 * Put a type/word pair into the word database for this particular file.
1414 putkey(const struct mpage
*mpage
, char *value
, uint64_t type
)
1418 assert(NULL
!= value
);
1419 if (TYPE_arch
== type
)
1420 for (cp
= value
; *cp
; cp
++)
1421 if (isupper((unsigned char)*cp
))
1422 *cp
= _tolower((unsigned char)*cp
);
1423 putkeys(mpage
, value
, strlen(value
), type
);
1427 * Grok all nodes at or below a certain mdoc node into putkey().
1430 putmdockey(const struct mpage
*mpage
,
1431 const struct roff_node
*n
, uint64_t m
)
1434 for ( ; NULL
!= n
; n
= n
->next
) {
1435 if (NULL
!= n
->child
)
1436 putmdockey(mpage
, n
->child
, m
);
1437 if (n
->type
== ROFFT_TEXT
)
1438 putkey(mpage
, n
->string
, m
);
1443 parse_man(struct mpage
*mpage
, const struct roff_meta
*meta
,
1444 const struct roff_node
*n
)
1446 const struct roff_node
*head
, *body
;
1447 char *start
, *title
;
1455 * We're only searching for one thing: the first text child in
1456 * the BODY of a NAME section. Since we don't keep track of
1457 * sections in -man, run some hoops to find out whether we're in
1458 * the correct section or not.
1461 if (n
->type
== ROFFT_BODY
&& n
->tok
== MAN_SH
) {
1463 assert(body
->parent
);
1464 if (NULL
!= (head
= body
->parent
->head
) &&
1465 1 == head
->nchild
&&
1466 NULL
!= (head
= (head
->child
)) &&
1467 head
->type
== ROFFT_TEXT
&&
1468 0 == strcmp(head
->string
, "NAME") &&
1469 NULL
!= body
->child
) {
1472 * Suck the entire NAME section into memory.
1473 * Yes, we might run away.
1474 * But too many manuals have big, spread-out
1475 * NAME sections over many lines.
1479 man_deroff(&title
, body
);
1484 * Go through a special heuristic dance here.
1485 * Conventionally, one or more manual names are
1486 * comma-specified prior to a whitespace, then a
1487 * dash, then a description. Try to puzzle out
1488 * the name parts here.
1493 sz
= strcspn(start
, " ,");
1494 if ('\0' == start
[sz
])
1501 * Assume a stray trailing comma in the
1502 * name list if a name begins with a dash.
1505 if ('-' == start
[0] ||
1506 ('\\' == start
[0] && '-' == start
[1]))
1509 putkey(mpage
, start
, NAME_TITLE
);
1510 if ( ! (mpage
->name_head_done
||
1511 strcasecmp(start
, meta
->title
))) {
1512 putkey(mpage
, start
, NAME_HEAD
);
1513 mpage
->name_head_done
= 1;
1521 assert(',' == byte
);
1523 while (' ' == *start
)
1527 if (start
== title
) {
1528 putkey(mpage
, start
, NAME_TITLE
);
1529 if ( ! (mpage
->name_head_done
||
1530 strcasecmp(start
, meta
->title
))) {
1531 putkey(mpage
, start
, NAME_HEAD
);
1532 mpage
->name_head_done
= 1;
1538 while (isspace((unsigned char)*start
))
1541 if (0 == strncmp(start
, "-", 1))
1543 else if (0 == strncmp(start
, "\\-\\-", 4))
1545 else if (0 == strncmp(start
, "\\-", 2))
1547 else if (0 == strncmp(start
, "\\(en", 4))
1549 else if (0 == strncmp(start
, "\\(em", 4))
1552 while (' ' == *start
)
1555 mpage
->desc
= mandoc_strdup(start
);
1561 for (n
= n
->child
; n
; n
= n
->next
) {
1562 if (NULL
!= mpage
->desc
)
1564 parse_man(mpage
, meta
, n
);
1569 parse_mdoc(struct mpage
*mpage
, const struct roff_meta
*meta
,
1570 const struct roff_node
*n
)
1574 for (n
= n
->child
; NULL
!= n
; n
= n
->next
) {
1585 if (NULL
!= mdocs
[n
->tok
].fp
)
1586 if (0 == (*mdocs
[n
->tok
].fp
)(mpage
, meta
, n
))
1588 if (mdocs
[n
->tok
].mask
)
1589 putmdockey(mpage
, n
->child
,
1590 mdocs
[n
->tok
].mask
);
1593 assert(n
->type
!= ROFFT_ROOT
);
1596 if (NULL
!= n
->child
)
1597 parse_mdoc(mpage
, meta
, n
);
1602 parse_mdoc_Fd(struct mpage
*mpage
, const struct roff_meta
*meta
,
1603 const struct roff_node
*n
)
1608 if (SEC_SYNOPSIS
!= n
->sec
||
1609 NULL
== (n
= n
->child
) ||
1610 n
->type
!= ROFFT_TEXT
)
1614 * Only consider those `Fd' macro fields that begin with an
1615 * "inclusion" token (versus, e.g., #define).
1618 if (strcmp("#include", n
->string
))
1621 if ((n
= n
->next
) == NULL
|| n
->type
!= ROFFT_TEXT
)
1625 * Strip away the enclosing angle brackets and make sure we're
1630 if ('<' == *start
|| '"' == *start
)
1633 if (0 == (sz
= strlen(start
)))
1636 end
= &start
[(int)sz
- 1];
1637 if ('>' == *end
|| '"' == *end
)
1641 putkeys(mpage
, start
, end
- start
+ 1, TYPE_In
);
1646 parse_mdoc_fname(struct mpage
*mpage
, const struct roff_node
*n
)
1651 if (n
->type
!= ROFFT_TEXT
)
1654 /* Skip function pointer punctuation. */
1657 while (*cp
== '(' || *cp
== '*')
1659 sz
= strcspn(cp
, "()");
1661 putkeys(mpage
, cp
, sz
, TYPE_Fn
);
1662 if (n
->sec
== SEC_SYNOPSIS
)
1663 putkeys(mpage
, cp
, sz
, NAME_SYN
);
1667 parse_mdoc_Fn(struct mpage
*mpage
, const struct roff_meta
*meta
,
1668 const struct roff_node
*n
)
1671 if (n
->child
== NULL
)
1674 parse_mdoc_fname(mpage
, n
->child
);
1676 for (n
= n
->child
->next
; n
!= NULL
; n
= n
->next
)
1677 if (n
->type
== ROFFT_TEXT
)
1678 putkey(mpage
, n
->string
, TYPE_Fa
);
1684 parse_mdoc_Fo(struct mpage
*mpage
, const struct roff_meta
*meta
,
1685 const struct roff_node
*n
)
1688 if (n
->type
!= ROFFT_HEAD
)
1691 if (n
->child
!= NULL
)
1692 parse_mdoc_fname(mpage
, n
->child
);
1698 parse_mdoc_Xr(struct mpage
*mpage
, const struct roff_meta
*meta
,
1699 const struct roff_node
*n
)
1703 if (NULL
== (n
= n
->child
))
1706 if (NULL
== n
->next
) {
1707 putkey(mpage
, n
->string
, TYPE_Xr
);
1711 mandoc_asprintf(&cp
, "%s(%s)", n
->string
, n
->next
->string
);
1712 putkey(mpage
, cp
, TYPE_Xr
);
1718 parse_mdoc_Nd(struct mpage
*mpage
, const struct roff_meta
*meta
,
1719 const struct roff_node
*n
)
1722 if (n
->type
== ROFFT_BODY
)
1723 mdoc_deroff(&mpage
->desc
, n
);
1728 parse_mdoc_Nm(struct mpage
*mpage
, const struct roff_meta
*meta
,
1729 const struct roff_node
*n
)
1732 if (SEC_NAME
== n
->sec
)
1733 putmdockey(mpage
, n
->child
, NAME_TITLE
);
1734 else if (n
->sec
== SEC_SYNOPSIS
&& n
->type
== ROFFT_HEAD
) {
1735 if (n
->child
== NULL
)
1736 putkey(mpage
, meta
->name
, NAME_SYN
);
1738 putmdockey(mpage
, n
->child
, NAME_SYN
);
1740 if ( ! (mpage
->name_head_done
||
1741 n
->child
== NULL
|| n
->child
->string
== NULL
||
1742 strcasecmp(n
->child
->string
, meta
->title
))) {
1743 putkey(mpage
, n
->child
->string
, ROFFT_HEAD
);
1744 mpage
->name_head_done
= 1;
1750 parse_mdoc_Sh(struct mpage
*mpage
, const struct roff_meta
*meta
,
1751 const struct roff_node
*n
)
1754 return(n
->sec
== SEC_CUSTOM
&& n
->type
== ROFFT_HEAD
);
1758 parse_mdoc_head(struct mpage
*mpage
, const struct roff_meta
*meta
,
1759 const struct roff_node
*n
)
1762 return(n
->type
== ROFFT_HEAD
);
1766 parse_mdoc_body(struct mpage
*mpage
, const struct roff_meta
*meta
,
1767 const struct roff_node
*n
)
1770 return(n
->type
== ROFFT_BODY
);
1774 * Add a string to the hash table for the current manual.
1775 * Each string has a bitmask telling which macros it belongs to.
1776 * When we finish the manual, we'll dump the table.
1779 putkeys(const struct mpage
*mpage
, char *cp
, size_t sz
, uint64_t v
)
1790 mustfree
= render_string(&cp
, &sz
);
1796 name_mask
&= ~NAME_FIRST
;
1798 say(mpage
->mlinks
->file
,
1799 "Adding name %*s, bits=%d", sz
, cp
, v
);
1803 for (i
= 0; i
< mansearch_keymax
; i
++)
1804 if ((uint64_t)1 << i
& v
)
1805 say(mpage
->mlinks
->file
,
1806 "Adding key %s=%*s",
1807 mansearch_keynames
[i
], sz
, cp
);
1811 slot
= ohash_qlookupi(htab
, cp
, &end
);
1812 s
= ohash_find(htab
, slot
);
1814 if (NULL
!= s
&& mpage
== s
->mpage
) {
1817 } else if (NULL
== s
) {
1818 s
= mandoc_calloc(1, sizeof(struct str
) + sz
+ 1);
1819 memcpy(s
->key
, cp
, sz
);
1820 ohash_insert(htab
, slot
, s
);
1830 * Take a Unicode codepoint and produce its UTF-8 encoding.
1831 * This isn't the best way to do this, but it works.
1832 * The magic numbers are from the UTF-8 packaging.
1833 * They're not as scary as they seem: read the UTF-8 spec for details.
1836 utf8(unsigned int cp
, char out
[7])
1841 if (cp
<= 0x0000007F) {
1844 } else if (cp
<= 0x000007FF) {
1846 out
[0] = (cp
>> 6 & 31) | 192;
1847 out
[1] = (cp
& 63) | 128;
1848 } else if (cp
<= 0x0000FFFF) {
1850 out
[0] = (cp
>> 12 & 15) | 224;
1851 out
[1] = (cp
>> 6 & 63) | 128;
1852 out
[2] = (cp
& 63) | 128;
1853 } else if (cp
<= 0x001FFFFF) {
1855 out
[0] = (cp
>> 18 & 7) | 240;
1856 out
[1] = (cp
>> 12 & 63) | 128;
1857 out
[2] = (cp
>> 6 & 63) | 128;
1858 out
[3] = (cp
& 63) | 128;
1859 } else if (cp
<= 0x03FFFFFF) {
1861 out
[0] = (cp
>> 24 & 3) | 248;
1862 out
[1] = (cp
>> 18 & 63) | 128;
1863 out
[2] = (cp
>> 12 & 63) | 128;
1864 out
[3] = (cp
>> 6 & 63) | 128;
1865 out
[4] = (cp
& 63) | 128;
1866 } else if (cp
<= 0x7FFFFFFF) {
1868 out
[0] = (cp
>> 30 & 1) | 252;
1869 out
[1] = (cp
>> 24 & 63) | 128;
1870 out
[2] = (cp
>> 18 & 63) | 128;
1871 out
[3] = (cp
>> 12 & 63) | 128;
1872 out
[4] = (cp
>> 6 & 63) | 128;
1873 out
[5] = (cp
& 63) | 128;
1882 * If the string contains escape sequences,
1883 * replace it with an allocated rendering and return 1,
1884 * such that the caller can free it after use.
1885 * Otherwise, do nothing and return 0.
1888 render_string(char **public, size_t *psz
)
1890 const char *src
, *scp
, *addcp
, *seq
;
1892 size_t ssz
, dsz
, addsz
;
1893 char utfbuf
[7], res
[6];
1894 int seqlen
, unicode
;
1898 res
[2] = ASCII_NBRSP
;
1899 res
[3] = ASCII_HYPH
;
1900 res
[4] = ASCII_BREAK
;
1903 src
= scp
= *public;
1908 while (scp
< src
+ *psz
) {
1910 /* Leave normal characters unchanged. */
1912 if (strchr(res
, *scp
) == NULL
) {
1920 * Found something that requires replacing,
1921 * make sure we have a destination buffer.
1925 dst
= mandoc_malloc(ssz
+ 1);
1927 memcpy(dst
, src
, dsz
);
1930 /* Handle single-char special characters. */
1952 * Found an escape sequence.
1953 * Read past the slash, then parse it.
1954 * Ignore everything except characters.
1958 if (mandoc_escape(&scp
, &seq
, &seqlen
) != ESCAPE_SPECIAL
)
1962 * Render the special character
1963 * as either UTF-8 or ASCII.
1967 unicode
= mchars_spec2cp(mchars
, seq
, seqlen
);
1970 addsz
= utf8(unicode
, utfbuf
);
1975 addcp
= mchars_spec2str(mchars
, seq
, seqlen
, &addsz
);
1978 if (*addcp
== ASCII_NBRSP
) {
1984 /* Copy the rendered glyph into the stream. */
1987 dst
= mandoc_realloc(dst
, ssz
+ 1);
1988 memcpy(dst
+ dsz
, addcp
, addsz
);
1996 /* Trim trailing whitespace and NUL-terminate. */
1998 while (*psz
> 0 && (*public)[*psz
- 1] == ' ')
2001 (*public)[*psz
] = '\0';
2008 dbadd_mlink(const struct mlink
*mlink
)
2013 SQL_BIND_TEXT(stmts
[STMT_INSERT_LINK
], i
, mlink
->dsec
);
2014 SQL_BIND_TEXT(stmts
[STMT_INSERT_LINK
], i
, mlink
->arch
);
2015 SQL_BIND_TEXT(stmts
[STMT_INSERT_LINK
], i
, mlink
->name
);
2016 SQL_BIND_INT64(stmts
[STMT_INSERT_LINK
], i
, mlink
->mpage
->pageid
);
2017 SQL_STEP(stmts
[STMT_INSERT_LINK
]);
2018 sqlite3_reset(stmts
[STMT_INSERT_LINK
]);
2022 dbadd_mlink_name(const struct mlink
*mlink
)
2030 SQL_BIND_INT64(stmts
[STMT_SELECT_NAME
], i
, mlink
->mpage
->pageid
);
2031 bits
= NAME_FILE
& NAME_MASK
;
2032 if (sqlite3_step(stmts
[STMT_SELECT_NAME
]) == SQLITE_ROW
) {
2033 bits
|= sqlite3_column_int64(stmts
[STMT_SELECT_NAME
], 0);
2034 sqlite3_reset(stmts
[STMT_SELECT_NAME
]);
2038 SQL_BIND_INT64(stmts
[STMT_INSERT_NAME
], i
, bits
);
2039 SQL_BIND_TEXT(stmts
[STMT_INSERT_NAME
], i
, mlink
->name
);
2040 SQL_BIND_INT64(stmts
[STMT_INSERT_NAME
], i
, mlink
->mpage
->pageid
);
2041 SQL_STEP(stmts
[STMT_INSERT_NAME
]);
2042 sqlite3_reset(stmts
[STMT_INSERT_NAME
]);
2046 * Flush the current page's terms (and their bits) into the database.
2047 * Wrap the entire set of additions in a transaction to make sqlite be a
2049 * Also, handle escape sequences at the last possible moment.
2052 dbadd(struct mpage
*mpage
)
2054 struct mlink
*mlink
;
2061 mlink
= mpage
->mlinks
;
2064 for (key
= ohash_first(&names
, &slot
); NULL
!= key
;
2065 key
= ohash_next(&names
, &slot
))
2067 for (key
= ohash_first(&strings
, &slot
); NULL
!= key
;
2068 key
= ohash_next(&strings
, &slot
))
2072 while (NULL
!= mlink
) {
2073 fputs(mlink
->name
, stdout
);
2074 if (NULL
== mlink
->next
||
2075 strcmp(mlink
->dsec
, mlink
->next
->dsec
) ||
2076 strcmp(mlink
->fsec
, mlink
->next
->fsec
) ||
2077 strcmp(mlink
->arch
, mlink
->next
->arch
)) {
2079 if ('\0' == *mlink
->dsec
)
2080 fputs(mlink
->fsec
, stdout
);
2082 fputs(mlink
->dsec
, stdout
);
2083 if ('\0' != *mlink
->arch
)
2084 printf("/%s", mlink
->arch
);
2087 mlink
= mlink
->next
;
2089 fputs(", ", stdout
);
2091 printf(" - %s\n", mpage
->desc
);
2096 say(mlink
->file
, "Adding to database");
2100 mustfree
= render_string(&cp
, &i
);
2102 SQL_BIND_TEXT(stmts
[STMT_INSERT_PAGE
], i
, cp
);
2103 SQL_BIND_INT(stmts
[STMT_INSERT_PAGE
], i
, mpage
->form
);
2104 SQL_STEP(stmts
[STMT_INSERT_PAGE
]);
2105 mpage
->pageid
= sqlite3_last_insert_rowid(db
);
2106 sqlite3_reset(stmts
[STMT_INSERT_PAGE
]);
2110 while (NULL
!= mlink
) {
2112 mlink
= mlink
->next
;
2114 mlink
= mpage
->mlinks
;
2116 for (key
= ohash_first(&names
, &slot
); NULL
!= key
;
2117 key
= ohash_next(&names
, &slot
)) {
2118 assert(key
->mpage
== mpage
);
2120 SQL_BIND_INT64(stmts
[STMT_INSERT_NAME
], i
, key
->mask
);
2121 SQL_BIND_TEXT(stmts
[STMT_INSERT_NAME
], i
, key
->key
);
2122 SQL_BIND_INT64(stmts
[STMT_INSERT_NAME
], i
, mpage
->pageid
);
2123 SQL_STEP(stmts
[STMT_INSERT_NAME
]);
2124 sqlite3_reset(stmts
[STMT_INSERT_NAME
]);
2127 for (key
= ohash_first(&strings
, &slot
); NULL
!= key
;
2128 key
= ohash_next(&strings
, &slot
)) {
2129 assert(key
->mpage
== mpage
);
2131 SQL_BIND_INT64(stmts
[STMT_INSERT_KEY
], i
, key
->mask
);
2132 SQL_BIND_TEXT(stmts
[STMT_INSERT_KEY
], i
, key
->key
);
2133 SQL_BIND_INT64(stmts
[STMT_INSERT_KEY
], i
, mpage
->pageid
);
2134 SQL_STEP(stmts
[STMT_INSERT_KEY
]);
2135 sqlite3_reset(stmts
[STMT_INSERT_KEY
]);
2143 struct mpage
*mpage
;
2144 struct mlink
*mlink
;
2149 SQL_EXEC("BEGIN TRANSACTION");
2151 for (mpage
= ohash_first(&mpages
, &slot
); NULL
!= mpage
;
2152 mpage
= ohash_next(&mpages
, &slot
)) {
2153 mlink
= mpage
->mlinks
;
2155 say(mlink
->file
, "Deleting from database");
2158 for ( ; NULL
!= mlink
; mlink
= mlink
->next
) {
2160 SQL_BIND_TEXT(stmts
[STMT_DELETE_PAGE
],
2162 SQL_BIND_TEXT(stmts
[STMT_DELETE_PAGE
],
2164 SQL_BIND_TEXT(stmts
[STMT_DELETE_PAGE
],
2166 SQL_STEP(stmts
[STMT_DELETE_PAGE
]);
2167 sqlite3_reset(stmts
[STMT_DELETE_PAGE
]);
2172 SQL_EXEC("END TRANSACTION");
2176 * Close an existing database and its prepared statements.
2177 * If "real" is not set, rename the temporary file into the real one.
2189 for (i
= 0; i
< STMT__MAX
; i
++) {
2190 sqlite3_finalize(stmts
[i
]);
2200 if ('\0' == *tempfilename
) {
2201 if (-1 == rename(MANDOC_DB
"~", MANDOC_DB
)) {
2202 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2203 say(MANDOC_DB
, "&rename");
2208 switch (child
= fork()) {
2210 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2211 say("", "&fork cmp");
2214 execlp("cmp", "cmp", "-s",
2215 tempfilename
, MANDOC_DB
, NULL
);
2216 say("", "&exec cmp");
2221 if (-1 == waitpid(child
, &status
, 0)) {
2222 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2223 say("", "&wait cmp");
2224 } else if (WIFSIGNALED(status
)) {
2225 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2226 say("", "cmp died from signal %d", WTERMSIG(status
));
2227 } else if (WEXITSTATUS(status
)) {
2228 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2230 "Data changed, but cannot replace database");
2233 *strrchr(tempfilename
, '/') = '\0';
2234 switch (child
= fork()) {
2236 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2237 say("", "&fork rm");
2240 execlp("rm", "rm", "-rf", tempfilename
, NULL
);
2241 say("", "&exec rm");
2242 exit((int)MANDOCLEVEL_SYSERR
);
2246 if (-1 == waitpid(child
, &status
, 0)) {
2247 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2248 say("", "&wait rm");
2249 } else if (WIFSIGNALED(status
) || WEXITSTATUS(status
)) {
2250 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2251 say("", "%s: Cannot remove temporary directory",
2257 * This is straightforward stuff.
2258 * Open a database connection to a "temporary" database, then open a set
2259 * of prepared statements we'll use over and over again.
2260 * If "real" is set, we use the existing database; if not, we truncate a
2262 * Must be matched by dbclose().
2273 *tempfilename
= '\0';
2274 ofl
= SQLITE_OPEN_READWRITE
;
2277 rc
= sqlite3_open_v2(MANDOC_DB
, &db
, ofl
, NULL
);
2278 if (SQLITE_OK
!= rc
) {
2279 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2280 if (SQLITE_CANTOPEN
!= rc
)
2281 say(MANDOC_DB
, "%s", sqlite3_errstr(rc
));
2284 goto prepare_statements
;
2287 ofl
|= SQLITE_OPEN_CREATE
| SQLITE_OPEN_EXCLUSIVE
;
2289 remove(MANDOC_DB
"~");
2290 rc
= sqlite3_open_v2(MANDOC_DB
"~", &db
, ofl
, NULL
);
2291 if (SQLITE_OK
== rc
)
2293 if (MPARSE_QUICK
& mparse_options
) {
2294 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2295 say(MANDOC_DB
"~", "%s", sqlite3_errstr(rc
));
2299 (void)strlcpy(tempfilename
, "/tmp/mandocdb.XXXXXX",
2300 sizeof(tempfilename
));
2301 if (NULL
== mkdtemp(tempfilename
)) {
2302 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2303 say("", "&%s", tempfilename
);
2306 (void)strlcat(tempfilename
, "/" MANDOC_DB
,
2307 sizeof(tempfilename
));
2308 rc
= sqlite3_open_v2(tempfilename
, &db
, ofl
, NULL
);
2309 if (SQLITE_OK
!= rc
) {
2310 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2311 say("", "%s: %s", tempfilename
, sqlite3_errstr(rc
));
2316 sql
= "CREATE TABLE \"mpages\" (\n"
2317 " \"desc\" TEXT NOT NULL,\n"
2318 " \"form\" INTEGER NOT NULL,\n"
2319 " \"pageid\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
2322 "CREATE TABLE \"mlinks\" (\n"
2323 " \"sec\" TEXT NOT NULL,\n"
2324 " \"arch\" TEXT NOT NULL,\n"
2325 " \"name\" TEXT NOT NULL,\n"
2326 " \"pageid\" INTEGER NOT NULL REFERENCES mpages(pageid) "
2327 "ON DELETE CASCADE\n"
2329 "CREATE INDEX mlinks_pageid_idx ON mlinks (pageid);\n"
2331 "CREATE TABLE \"names\" (\n"
2332 " \"bits\" INTEGER NOT NULL,\n"
2333 " \"name\" TEXT NOT NULL,\n"
2334 " \"pageid\" INTEGER NOT NULL REFERENCES mpages(pageid) "
2335 "ON DELETE CASCADE,\n"
2336 " UNIQUE (\"name\", \"pageid\") ON CONFLICT REPLACE\n"
2339 "CREATE TABLE \"keys\" (\n"
2340 " \"bits\" INTEGER NOT NULL,\n"
2341 " \"key\" TEXT NOT NULL,\n"
2342 " \"pageid\" INTEGER NOT NULL REFERENCES mpages(pageid) "
2343 "ON DELETE CASCADE\n"
2345 "CREATE INDEX keys_pageid_idx ON keys (pageid);\n";
2347 if (SQLITE_OK
!= sqlite3_exec(db
, sql
, NULL
, NULL
, NULL
)) {
2348 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2349 say(MANDOC_DB
, "%s", sqlite3_errmsg(db
));
2355 if (SQLITE_OK
!= sqlite3_exec(db
,
2356 "PRAGMA foreign_keys = ON", NULL
, NULL
, NULL
)) {
2357 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2358 say(MANDOC_DB
, "PRAGMA foreign_keys: %s",
2359 sqlite3_errmsg(db
));
2364 sql
= "DELETE FROM mpages WHERE pageid IN "
2365 "(SELECT pageid FROM mlinks WHERE "
2366 "sec=? AND arch=? AND name=?)";
2367 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_DELETE_PAGE
], NULL
);
2368 sql
= "INSERT INTO mpages "
2369 "(desc,form) VALUES (?,?)";
2370 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_PAGE
], NULL
);
2371 sql
= "INSERT INTO mlinks "
2372 "(sec,arch,name,pageid) VALUES (?,?,?,?)";
2373 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_LINK
], NULL
);
2374 sql
= "SELECT bits FROM names where pageid = ?";
2375 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_SELECT_NAME
], NULL
);
2376 sql
= "INSERT INTO names "
2377 "(bits,name,pageid) VALUES (?,?,?)";
2378 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_NAME
], NULL
);
2379 sql
= "INSERT INTO keys "
2380 "(bits,key,pageid) VALUES (?,?,?)";
2381 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_KEY
], NULL
);
2385 * When opening a new database, we can turn off
2386 * synchronous mode for much better performance.
2389 if (real
&& SQLITE_OK
!= sqlite3_exec(db
,
2390 "PRAGMA synchronous = OFF", NULL
, NULL
, NULL
)) {
2391 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2392 say(MANDOC_DB
, "PRAGMA synchronous: %s",
2393 sqlite3_errmsg(db
));
2403 hash_calloc(size_t n
, size_t sz
, void *arg
)
2406 return(mandoc_calloc(n
, sz
));
2410 hash_alloc(size_t sz
, void *arg
)
2413 return(mandoc_malloc(sz
));
2417 hash_free(void *p
, void *arg
)
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
);