]>
git.cameronkatri.com Git - mandoc.git/blob - mandocdb.c
1 /* $Id: mandocdb.c,v 1.158 2014/08/16 19:00:01 schwarze Exp $ */
3 * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011, 2012, 2013, 2014 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 AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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.
21 #include <sys/types.h>
31 #include "compat_fts.h"
45 #include "compat_ohash.h"
52 #include "mandoc_aux.h"
54 #include "mansearch.h"
56 extern int mansearch_keymax
;
57 extern const char *const mansearch_keynames
[];
59 #define SQL_EXEC(_v) \
60 if (SQLITE_OK != sqlite3_exec(db, (_v), NULL, NULL, NULL)) \
61 say("", "%s: %s", (_v), sqlite3_errmsg(db))
62 #define SQL_BIND_TEXT(_s, _i, _v) \
63 if (SQLITE_OK != sqlite3_bind_text \
64 ((_s), (_i)++, (_v), -1, SQLITE_STATIC)) \
65 say(mlink->file, "%s", sqlite3_errmsg(db))
66 #define SQL_BIND_INT(_s, _i, _v) \
67 if (SQLITE_OK != sqlite3_bind_int \
68 ((_s), (_i)++, (_v))) \
69 say(mlink->file, "%s", sqlite3_errmsg(db))
70 #define SQL_BIND_INT64(_s, _i, _v) \
71 if (SQLITE_OK != sqlite3_bind_int64 \
72 ((_s), (_i)++, (_v))) \
73 say(mlink->file, "%s", sqlite3_errmsg(db))
74 #define SQL_STEP(_s) \
75 if (SQLITE_DONE != sqlite3_step((_s))) \
76 say(mlink->file, "%s", sqlite3_errmsg(db))
79 OP_DEFAULT
= 0, /* new dbs from dir list or default config */
80 OP_CONFFILE
, /* new databases from custom config file */
81 OP_UPDATE
, /* delete/add entries in existing database */
82 OP_DELETE
, /* delete entries from existing database */
83 OP_TEST
/* change no databases, report potential problems */
87 FORM_NONE
, /* format is unknown */
88 FORM_SRC
, /* format is -man or -mdoc */
89 FORM_CAT
/* format is cat */
93 char *rendered
; /* key in UTF-8 or ASCII form */
94 const struct mpage
*mpage
; /* if set, the owning parse */
95 uint64_t mask
; /* bitmask in sequence */
96 char key
[]; /* may contain escape sequences */
105 struct inodev inodev
; /* used for hashing routine */
106 int64_t pageid
; /* pageid in mpages SQL table */
107 enum form form
; /* format from file content */
108 char *sec
; /* section from file content */
109 char *arch
; /* architecture from file content */
110 char *title
; /* title from file content */
111 char *desc
; /* description from file content */
112 struct mlink
*mlinks
; /* singly linked list */
116 char file
[PATH_MAX
]; /* filename rel. to manpath */
117 enum form dform
; /* format from directory */
118 enum form fform
; /* format from file name suffix */
119 char *dsec
; /* section from directory */
120 char *arch
; /* architecture from directory */
121 char *name
; /* name from file name (not empty) */
122 char *fsec
; /* section from file name suffix */
123 struct mlink
*next
; /* singly linked list */
124 struct mpage
*mpage
; /* parent */
125 int gzip
; /* filename has a .gz suffix */
129 STMT_DELETE_PAGE
= 0, /* delete mpage */
130 STMT_INSERT_PAGE
, /* insert mpage */
131 STMT_INSERT_LINK
, /* insert mlink */
132 STMT_INSERT_NAME
, /* insert name */
133 STMT_INSERT_KEY
, /* insert parsed key */
137 typedef int (*mdoc_fp
)(struct mpage
*, const struct mdoc_node
*);
139 struct mdoc_handler
{
140 mdoc_fp fp
; /* optional handler */
141 uint64_t mask
; /* set unless handler returns 0 */
144 static void dbclose(int);
145 static void dbadd(struct mpage
*, struct mchars
*);
146 static void dbadd_mlink(const struct mlink
*mlink
);
147 static int dbopen(int);
148 static void dbprune(void);
149 static void filescan(const char *);
150 static void *hash_alloc(size_t, void *);
151 static void hash_free(void *, void *);
152 static void *hash_calloc(size_t, size_t, void *);
153 static void mlink_add(struct mlink
*, const struct stat
*);
154 static void mlink_check(struct mpage
*, struct mlink
*);
155 static void mlink_free(struct mlink
*);
156 static void mlinks_undupe(struct mpage
*);
157 static void mpages_free(void);
158 static void mpages_merge(struct mchars
*, struct mparse
*);
159 static void names_check(void);
160 static void parse_cat(struct mpage
*, int);
161 static void parse_man(struct mpage
*, const struct man_node
*);
162 static void parse_mdoc(struct mpage
*, const struct mdoc_node
*);
163 static int parse_mdoc_body(struct mpage
*, const struct mdoc_node
*);
164 static int parse_mdoc_head(struct mpage
*, const struct mdoc_node
*);
165 static int parse_mdoc_Fd(struct mpage
*, const struct mdoc_node
*);
166 static int parse_mdoc_Fn(struct mpage
*, const struct mdoc_node
*);
167 static int parse_mdoc_Nd(struct mpage
*, const struct mdoc_node
*);
168 static int parse_mdoc_Nm(struct mpage
*, const struct mdoc_node
*);
169 static int parse_mdoc_Sh(struct mpage
*, const struct mdoc_node
*);
170 static int parse_mdoc_Xr(struct mpage
*, const struct mdoc_node
*);
171 static void putkey(const struct mpage
*, char *, uint64_t);
172 static void putkeys(const struct mpage
*,
173 const char *, size_t, uint64_t);
174 static void putmdockey(const struct mpage
*,
175 const struct mdoc_node
*, uint64_t);
176 static void render_key(struct mchars
*, struct str
*);
177 static void say(const char *, const char *, ...);
178 static int set_basedir(const char *);
179 static int treescan(void);
180 static size_t utf8(unsigned int, char [7]);
182 static char tempfilename
[32];
183 static char *progname
;
184 static int nodb
; /* no database changes */
185 static int mparse_options
; /* abort the parse early */
186 static int use_all
; /* use all found files */
187 static int debug
; /* print what we're doing */
188 static int warnings
; /* warn about crap */
189 static int write_utf8
; /* write UTF-8 output; else ASCII */
190 static int exitcode
; /* to be returned by main */
191 static enum op op
; /* operational mode */
192 static char basedir
[PATH_MAX
]; /* current base directory */
193 static struct ohash mpages
; /* table of distinct manual pages */
194 static struct ohash mlinks
; /* table of directory entries */
195 static struct ohash names
; /* table of all names */
196 static struct ohash strings
; /* table of all strings */
197 static sqlite3
*db
= NULL
; /* current database */
198 static sqlite3_stmt
*stmts
[STMT__MAX
]; /* current statements */
199 static uint64_t name_mask
;
201 static const struct mdoc_handler mdocs
[MDOC_MAX
] = {
202 { NULL
, 0 }, /* Ap */
203 { NULL
, 0 }, /* Dd */
204 { NULL
, 0 }, /* Dt */
205 { NULL
, 0 }, /* Os */
206 { parse_mdoc_Sh
, TYPE_Sh
}, /* Sh */
207 { parse_mdoc_head
, TYPE_Ss
}, /* Ss */
208 { NULL
, 0 }, /* Pp */
209 { NULL
, 0 }, /* D1 */
210 { NULL
, 0 }, /* Dl */
211 { NULL
, 0 }, /* Bd */
212 { NULL
, 0 }, /* Ed */
213 { NULL
, 0 }, /* Bl */
214 { NULL
, 0 }, /* El */
215 { NULL
, 0 }, /* It */
216 { NULL
, 0 }, /* Ad */
217 { NULL
, TYPE_An
}, /* An */
218 { NULL
, TYPE_Ar
}, /* Ar */
219 { NULL
, TYPE_Cd
}, /* Cd */
220 { NULL
, TYPE_Cm
}, /* Cm */
221 { NULL
, TYPE_Dv
}, /* Dv */
222 { NULL
, TYPE_Er
}, /* Er */
223 { NULL
, TYPE_Ev
}, /* Ev */
224 { NULL
, 0 }, /* Ex */
225 { NULL
, TYPE_Fa
}, /* Fa */
226 { parse_mdoc_Fd
, 0 }, /* Fd */
227 { NULL
, TYPE_Fl
}, /* Fl */
228 { parse_mdoc_Fn
, 0 }, /* Fn */
229 { NULL
, TYPE_Ft
}, /* Ft */
230 { NULL
, TYPE_Ic
}, /* Ic */
231 { NULL
, TYPE_In
}, /* In */
232 { NULL
, TYPE_Li
}, /* Li */
233 { parse_mdoc_Nd
, 0 }, /* Nd */
234 { parse_mdoc_Nm
, 0 }, /* Nm */
235 { NULL
, 0 }, /* Op */
236 { NULL
, 0 }, /* Ot */
237 { NULL
, TYPE_Pa
}, /* Pa */
238 { NULL
, 0 }, /* Rv */
239 { NULL
, TYPE_St
}, /* St */
240 { NULL
, TYPE_Va
}, /* Va */
241 { parse_mdoc_body
, TYPE_Va
}, /* Vt */
242 { parse_mdoc_Xr
, 0 }, /* Xr */
243 { NULL
, 0 }, /* %A */
244 { NULL
, 0 }, /* %B */
245 { NULL
, 0 }, /* %D */
246 { NULL
, 0 }, /* %I */
247 { NULL
, 0 }, /* %J */
248 { NULL
, 0 }, /* %N */
249 { NULL
, 0 }, /* %O */
250 { NULL
, 0 }, /* %P */
251 { NULL
, 0 }, /* %R */
252 { NULL
, 0 }, /* %T */
253 { NULL
, 0 }, /* %V */
254 { NULL
, 0 }, /* Ac */
255 { NULL
, 0 }, /* Ao */
256 { NULL
, 0 }, /* Aq */
257 { NULL
, TYPE_At
}, /* At */
258 { NULL
, 0 }, /* Bc */
259 { NULL
, 0 }, /* Bf */
260 { NULL
, 0 }, /* Bo */
261 { NULL
, 0 }, /* Bq */
262 { NULL
, TYPE_Bsx
}, /* Bsx */
263 { NULL
, TYPE_Bx
}, /* Bx */
264 { NULL
, 0 }, /* Db */
265 { NULL
, 0 }, /* Dc */
266 { NULL
, 0 }, /* Do */
267 { NULL
, 0 }, /* Dq */
268 { NULL
, 0 }, /* Ec */
269 { NULL
, 0 }, /* Ef */
270 { NULL
, TYPE_Em
}, /* Em */
271 { NULL
, 0 }, /* Eo */
272 { NULL
, TYPE_Fx
}, /* Fx */
273 { NULL
, TYPE_Ms
}, /* Ms */
274 { NULL
, 0 }, /* No */
275 { NULL
, 0 }, /* Ns */
276 { NULL
, TYPE_Nx
}, /* Nx */
277 { NULL
, TYPE_Ox
}, /* Ox */
278 { NULL
, 0 }, /* Pc */
279 { NULL
, 0 }, /* Pf */
280 { NULL
, 0 }, /* Po */
281 { NULL
, 0 }, /* Pq */
282 { NULL
, 0 }, /* Qc */
283 { NULL
, 0 }, /* Ql */
284 { NULL
, 0 }, /* Qo */
285 { NULL
, 0 }, /* Qq */
286 { NULL
, 0 }, /* Re */
287 { NULL
, 0 }, /* Rs */
288 { NULL
, 0 }, /* Sc */
289 { NULL
, 0 }, /* So */
290 { NULL
, 0 }, /* Sq */
291 { NULL
, 0 }, /* Sm */
292 { NULL
, 0 }, /* Sx */
293 { NULL
, TYPE_Sy
}, /* Sy */
294 { NULL
, TYPE_Tn
}, /* Tn */
295 { NULL
, 0 }, /* Ux */
296 { NULL
, 0 }, /* Xc */
297 { NULL
, 0 }, /* Xo */
298 { parse_mdoc_head
, 0 }, /* Fo */
299 { NULL
, 0 }, /* Fc */
300 { NULL
, 0 }, /* Oo */
301 { NULL
, 0 }, /* Oc */
302 { NULL
, 0 }, /* Bk */
303 { NULL
, 0 }, /* Ek */
304 { NULL
, 0 }, /* Bt */
305 { NULL
, 0 }, /* Hf */
306 { NULL
, 0 }, /* Fr */
307 { NULL
, 0 }, /* Ud */
308 { NULL
, TYPE_Lb
}, /* Lb */
309 { NULL
, 0 }, /* Lp */
310 { NULL
, TYPE_Lk
}, /* Lk */
311 { NULL
, TYPE_Mt
}, /* Mt */
312 { NULL
, 0 }, /* Brq */
313 { NULL
, 0 }, /* Bro */
314 { NULL
, 0 }, /* Brc */
315 { NULL
, 0 }, /* %C */
316 { NULL
, 0 }, /* Es */
317 { NULL
, 0 }, /* En */
318 { NULL
, TYPE_Dx
}, /* Dx */
319 { NULL
, 0 }, /* %Q */
320 { NULL
, 0 }, /* br */
321 { NULL
, 0 }, /* sp */
322 { NULL
, 0 }, /* %U */
323 { NULL
, 0 }, /* Ta */
328 main(int argc
, char *argv
[])
332 const char *path_arg
;
334 struct manpaths dirs
;
336 struct ohash_info mpages_info
, mlinks_info
;
338 memset(stmts
, 0, STMT__MAX
* sizeof(sqlite3_stmt
*));
339 memset(&dirs
, 0, sizeof(struct manpaths
));
341 mpages_info
.alloc
= mlinks_info
.alloc
= hash_alloc
;
342 mpages_info
.calloc
= mlinks_info
.calloc
= hash_calloc
;
343 mpages_info
.free
= mlinks_info
.free
= hash_free
;
345 mpages_info
.key_offset
= offsetof(struct mpage
, inodev
);
346 mlinks_info
.key_offset
= offsetof(struct mlink
, file
);
348 progname
= strrchr(argv
[0], '/');
349 if (progname
== NULL
)
355 * We accept a few different invocations.
356 * The CHECKOP macro makes sure that invocation styles don't
357 * clobber each other.
359 #define CHECKOP(_op, _ch) do \
360 if (OP_DEFAULT != (_op)) { \
361 fprintf(stderr, "%s: -%c: Conflicting option\n", \
364 } while (/*CONSTCOND*/0)
369 while (-1 != (ch
= getopt(argc
, argv
, "aC:Dd:npQT:tu:v")))
394 mparse_options
|= MPARSE_QUICK
;
397 if (strcmp(optarg
, "utf8")) {
398 fprintf(stderr
, "%s: -T%s: "
399 "Unsupported output format\n",
407 dup2(STDOUT_FILENO
, STDERR_FILENO
);
417 /* Compatibility with espie@'s makewhatis. */
426 if (OP_CONFFILE
== op
&& argc
> 0) {
427 fprintf(stderr
, "%s: -C: Too many arguments\n",
432 exitcode
= (int)MANDOCLEVEL_OK
;
433 mp
= mparse_alloc(mparse_options
, MANDOCLEVEL_FATAL
, NULL
, NULL
);
436 ohash_init(&mpages
, 6, &mpages_info
);
437 ohash_init(&mlinks
, 6, &mlinks_info
);
439 if (OP_UPDATE
== op
|| OP_DELETE
== op
|| OP_TEST
== op
) {
442 * Most of these deal with a specific directory.
443 * Jump into that directory first.
445 if (OP_TEST
!= op
&& 0 == set_basedir(path_arg
))
450 * The existing database is usable. Process
451 * all files specified on the command-line.
454 for (i
= 0; i
< argc
; i
++)
460 * Database missing or corrupt.
461 * Recreate from scratch.
463 exitcode
= (int)MANDOCLEVEL_OK
;
471 mpages_merge(mc
, mp
);
472 dbclose(OP_DEFAULT
== op
? 0 : 1);
475 * If we have arguments, use them as our manpaths.
476 * If we don't, grok from manpath(1) or however else
477 * manpath_parse() wants to do it.
480 dirs
.paths
= mandoc_reallocarray(NULL
,
481 argc
, sizeof(char *));
482 dirs
.sz
= (size_t)argc
;
483 for (i
= 0; i
< argc
; i
++)
484 dirs
.paths
[i
] = mandoc_strdup(argv
[i
]);
486 manpath_parse(&dirs
, path_arg
, NULL
, NULL
);
489 exitcode
= (int)MANDOCLEVEL_BADARG
;
490 say("", "Empty manpath");
494 * First scan the tree rooted at a base directory, then
495 * build a new database and finally move it into place.
496 * Ignore zero-length directories and strip trailing
499 for (j
= 0; j
< dirs
.sz
; j
++) {
500 sz
= strlen(dirs
.paths
[j
]);
501 if (sz
&& '/' == dirs
.paths
[j
][sz
- 1])
502 dirs
.paths
[j
][--sz
] = '\0';
507 ohash_init(&mpages
, 6, &mpages_info
);
508 ohash_init(&mlinks
, 6, &mlinks_info
);
511 if (0 == set_basedir(dirs
.paths
[j
]))
518 mpages_merge(mc
, mp
);
519 if (warnings
&& !nodb
&&
520 ! (MPARSE_QUICK
& mparse_options
))
524 if (j
+ 1 < dirs
.sz
) {
526 ohash_delete(&mpages
);
527 ohash_delete(&mlinks
);
536 ohash_delete(&mpages
);
537 ohash_delete(&mlinks
);
540 fprintf(stderr
, "usage: %s [-aDnpQ] [-C file] [-Tutf8]\n"
541 " %s [-aDnpQ] [-Tutf8] dir ...\n"
542 " %s [-DnpQ] [-Tutf8] -d dir [file ...]\n"
543 " %s [-Dnp] -u dir [file ...]\n"
544 " %s [-Q] -t file ...\n",
545 progname
, progname
, progname
,
548 return((int)MANDOCLEVEL_BADARG
);
552 * Scan a directory tree rooted at "basedir" for manpages.
553 * We use fts(), scanning directory parts along the way for clues to our
554 * section and architecture.
556 * If use_all has been specified, grok all files.
557 * If not, sanitise paths to the following:
559 * [./]man*[/<arch>]/<name>.<section>
561 * [./]cat<section>[/<arch>]/<name>.0
563 * TODO: accomodate for multi-language directories.
573 char *dsec
, *arch
, *fsec
, *cp
;
578 argv
[1] = (char *)NULL
;
580 f
= fts_open((char * const *)argv
,
581 FTS_PHYSICAL
| FTS_NOCHDIR
, NULL
);
583 exitcode
= (int)MANDOCLEVEL_SYSERR
;
584 say("", "&fts_open");
591 while (NULL
!= (ff
= fts_read(f
))) {
592 path
= ff
->fts_path
+ 2;
593 switch (ff
->fts_info
) {
596 * Symbolic links require various sanity checks,
597 * then get handled just like regular files.
600 if (NULL
== realpath(path
, buf
)) {
602 say(path
, "&realpath");
605 if (strstr(buf
, basedir
) != buf
) {
606 if (warnings
) say("",
607 "%s: outside base directory", buf
);
610 /* Use logical inode to avoid mpages dupe. */
611 if (-1 == stat(path
, ff
->fts_statp
)) {
619 * If we're a regular file, add an mlink by using the
620 * stored directory data and handling the filename.
623 if (0 == strcmp(path
, MANDOC_DB
))
625 if ( ! use_all
&& ff
->fts_level
< 2) {
627 say(path
, "Extraneous file");
632 while (NULL
== fsec
) {
633 fsec
= strrchr(ff
->fts_name
, '.');
634 if (NULL
== fsec
|| strcmp(fsec
+1, "gz"))
644 "No filename suffix");
647 } else if (0 == strcmp(++fsec
, "html")) {
649 say(path
, "Skip html");
651 } else if (0 == strcmp(fsec
, "ps")) {
653 say(path
, "Skip ps");
655 } else if (0 == strcmp(fsec
, "pdf")) {
657 say(path
, "Skip pdf");
659 } else if ( ! use_all
&&
660 ((FORM_SRC
== dform
&& strcmp(fsec
, dsec
)) ||
661 (FORM_CAT
== dform
&& strcmp(fsec
, "0")))) {
663 say(path
, "Wrong filename suffix");
668 mlink
= mandoc_calloc(1, sizeof(struct mlink
));
669 if (strlcpy(mlink
->file
, path
,
670 sizeof(mlink
->file
)) >=
671 sizeof(mlink
->file
)) {
672 say(path
, "Filename too long");
676 mlink
->dform
= dform
;
679 mlink
->name
= ff
->fts_name
;
682 mlink_add(mlink
, ff
->fts_statp
);
692 say(path
, "Not a regular file");
696 switch (ff
->fts_level
) {
698 /* Ignore the root directory. */
702 * This might contain manX/ or catX/.
703 * Try to infer this from the name.
704 * If we're not in use_all, enforce it.
707 if (FTS_DP
== ff
->fts_info
)
710 if (0 == strncmp(cp
, "man", 3)) {
713 } else if (0 == strncmp(cp
, "cat", 3)) {
721 if (NULL
!= dsec
|| use_all
)
725 say(path
, "Unknown directory part");
726 fts_set(f
, ff
, FTS_SKIP
);
730 * Possibly our architecture.
731 * If we're descending, keep tabs on it.
733 if (FTS_DP
!= ff
->fts_info
&& NULL
!= dsec
)
739 if (FTS_DP
== ff
->fts_info
|| use_all
)
742 say(path
, "Extraneous directory part");
743 fts_set(f
, ff
, FTS_SKIP
);
753 * Add a file to the mlinks table.
754 * Do not verify that it's a "valid" looking manpage (we'll do that
757 * Try to infer the manual section, architecture, and page name from the
758 * path, assuming it looks like
760 * [./]man*[/<arch>]/<name>.<section>
762 * [./]cat<section>[/<arch>]/<name>.0
764 * See treescan() for the fts(3) version of this.
767 filescan(const char *file
)
776 if (0 == strncmp(file
, "./", 2))
780 * We have to do lstat(2) before realpath(3) loses
781 * the information whether this is a symbolic link.
782 * We need to know that because for symbolic links,
783 * we want to use the orginal file name, while for
784 * regular files, we want to use the real path.
786 if (-1 == lstat(file
, &st
)) {
787 exitcode
= (int)MANDOCLEVEL_BADARG
;
790 } else if (0 == ((S_IFREG
| S_IFLNK
) & st
.st_mode
)) {
791 exitcode
= (int)MANDOCLEVEL_BADARG
;
792 say(file
, "Not a regular file");
797 * We have to resolve the file name to the real path
798 * in any case for the base directory check.
800 if (NULL
== realpath(file
, buf
)) {
801 exitcode
= (int)MANDOCLEVEL_BADARG
;
802 say(file
, "&realpath");
808 else if (strstr(buf
, basedir
) == buf
)
809 start
= buf
+ strlen(basedir
);
811 exitcode
= (int)MANDOCLEVEL_BADARG
;
812 say("", "%s: outside base directory", buf
);
817 * Now we are sure the file is inside our tree.
818 * If it is a symbolic link, ignore the real path
819 * and use the original name.
820 * This implies passing stuff like "cat1/../man1/foo.1"
821 * on the command line won't work. So don't do that.
822 * Note the stat(2) can still fail if the link target
825 if (S_IFLNK
& st
.st_mode
) {
826 if (-1 == stat(buf
, &st
)) {
827 exitcode
= (int)MANDOCLEVEL_BADARG
;
831 if (strlcpy(buf
, file
, sizeof(buf
)) >= sizeof(buf
)) {
832 say(file
, "Filename too long");
836 if (OP_TEST
!= op
&& strstr(buf
, basedir
) == buf
)
837 start
+= strlen(basedir
);
840 mlink
= mandoc_calloc(1, sizeof(struct mlink
));
841 if (strlcpy(mlink
->file
, start
, sizeof(mlink
->file
)) >=
842 sizeof(mlink
->file
)) {
843 say(start
, "Filename too long");
848 * First try to guess our directory structure.
849 * If we find a separator, try to look for man* or cat*.
850 * If we find one of these and what's underneath is a directory,
851 * assume it's an architecture.
853 if (NULL
!= (p
= strchr(start
, '/'))) {
855 if (0 == strncmp(start
, "man", 3)) {
856 mlink
->dform
= FORM_SRC
;
857 mlink
->dsec
= start
+ 3;
858 } else if (0 == strncmp(start
, "cat", 3)) {
859 mlink
->dform
= FORM_CAT
;
860 mlink
->dsec
= start
+ 3;
864 if (NULL
!= mlink
->dsec
&& NULL
!= (p
= strchr(start
, '/'))) {
872 * Now check the file suffix.
873 * Suffix of `.0' indicates a catpage, `.1-9' is a manpage.
875 p
= strrchr(start
, '\0');
876 while (p
-- > start
&& '/' != *p
&& '.' != *p
)
885 * Now try to parse the name.
886 * Use the filename portion of the path.
889 if (NULL
!= (p
= strrchr(start
, '/'))) {
893 mlink_add(mlink
, &st
);
897 mlink_add(struct mlink
*mlink
, const struct stat
*st
)
899 struct inodev inodev
;
903 assert(NULL
!= mlink
->file
);
905 mlink
->dsec
= mandoc_strdup(mlink
->dsec
? mlink
->dsec
: "");
906 mlink
->arch
= mandoc_strdup(mlink
->arch
? mlink
->arch
: "");
907 mlink
->name
= mandoc_strdup(mlink
->name
? mlink
->name
: "");
908 mlink
->fsec
= mandoc_strdup(mlink
->fsec
? mlink
->fsec
: "");
910 if ('0' == *mlink
->fsec
) {
912 mlink
->fsec
= mandoc_strdup(mlink
->dsec
);
913 mlink
->fform
= FORM_CAT
;
914 } else if ('1' <= *mlink
->fsec
&& '9' >= *mlink
->fsec
)
915 mlink
->fform
= FORM_SRC
;
917 mlink
->fform
= FORM_NONE
;
919 slot
= ohash_qlookup(&mlinks
, mlink
->file
);
920 assert(NULL
== ohash_find(&mlinks
, slot
));
921 ohash_insert(&mlinks
, slot
, mlink
);
923 inodev
.st_ino
= st
->st_ino
;
924 inodev
.st_dev
= st
->st_dev
;
925 slot
= ohash_lookup_memory(&mpages
, (char *)&inodev
,
926 sizeof(struct inodev
), inodev
.st_ino
);
927 mpage
= ohash_find(&mpages
, slot
);
929 mpage
= mandoc_calloc(1, sizeof(struct mpage
));
930 mpage
->inodev
.st_ino
= inodev
.st_ino
;
931 mpage
->inodev
.st_dev
= inodev
.st_dev
;
932 ohash_insert(&mpages
, slot
, mpage
);
934 mlink
->next
= mpage
->mlinks
;
935 mpage
->mlinks
= mlink
;
936 mlink
->mpage
= mpage
;
940 mlink_free(struct mlink
*mlink
)
957 mpage
= ohash_first(&mpages
, &slot
);
958 while (NULL
!= mpage
) {
959 while (NULL
!= (mlink
= mpage
->mlinks
)) {
960 mpage
->mlinks
= mlink
->next
;
968 mpage
= ohash_next(&mpages
, &slot
);
973 * For each mlink to the mpage, check whether the path looks like
974 * it is formatted, and if it does, check whether a source manual
975 * exists by the same name, ignoring the suffix.
976 * If both conditions hold, drop the mlink.
979 mlinks_undupe(struct mpage
*mpage
)
986 mpage
->form
= FORM_CAT
;
987 prev
= &mpage
->mlinks
;
988 while (NULL
!= (mlink
= *prev
)) {
989 if (FORM_CAT
!= mlink
->dform
) {
990 mpage
->form
= FORM_NONE
;
993 (void)strlcpy(buf
, mlink
->file
, sizeof(buf
));
994 bufp
= strstr(buf
, "cat");
995 assert(NULL
!= bufp
);
996 memcpy(bufp
, "man", 3);
997 if (NULL
!= (bufp
= strrchr(buf
, '.')))
999 (void)strlcat(buf
, mlink
->dsec
, sizeof(buf
));
1000 if (NULL
== ohash_find(&mlinks
,
1001 ohash_qlookup(&mlinks
, buf
)))
1004 say(mlink
->file
, "Man source exists: %s", buf
);
1007 *prev
= mlink
->next
;
1011 prev
= &(*prev
)->next
;
1016 mlink_check(struct mpage
*mpage
, struct mlink
*mlink
)
1022 * Check whether the manual section given in a file
1023 * agrees with the directory where the file is located.
1024 * Some manuals have suffixes like (3p) on their
1025 * section number either inside the file or in the
1026 * directory name, some are linked into more than one
1027 * section, like encrypt(1) = makekey(8).
1030 if (FORM_SRC
== mpage
->form
&&
1031 strcasecmp(mpage
->sec
, mlink
->dsec
))
1032 say(mlink
->file
, "Section \"%s\" manual in %s directory",
1033 mpage
->sec
, mlink
->dsec
);
1036 * Manual page directories exist for each kernel
1037 * architecture as returned by machine(1).
1038 * However, many manuals only depend on the
1039 * application architecture as returned by arch(1).
1040 * For example, some (2/ARM) manuals are shared
1041 * across the "armish" and "zaurus" kernel
1043 * A few manuals are even shared across completely
1044 * different architectures, for example fdformat(1)
1045 * on amd64, i386, sparc, and sparc64.
1048 if (strcasecmp(mpage
->arch
, mlink
->arch
))
1049 say(mlink
->file
, "Architecture \"%s\" manual in "
1050 "\"%s\" directory", mpage
->arch
, mlink
->arch
);
1054 * parse_cat() doesn't set NAME_TITLE yet.
1057 if (FORM_CAT
== mpage
->form
)
1061 * Check whether this mlink
1062 * appears as a name in the NAME section.
1065 slot
= ohash_qlookup(&names
, mlink
->name
);
1066 str
= ohash_find(&names
, slot
);
1067 assert(NULL
!= str
);
1068 if ( ! (NAME_TITLE
& str
->mask
))
1069 say(mlink
->file
, "Name missing in NAME section");
1073 * Run through the files in the global vector "mpages"
1074 * and add them to the database specified in "basedir".
1076 * This handles the parsing scheme itself, using the cues of directory
1077 * and filename to determine whether the file is parsable or not.
1080 mpages_merge(struct mchars
*mc
, struct mparse
*mp
)
1083 struct ohash_info str_info
;
1085 struct mpage
*mpage
, *mpage_dest
;
1086 struct mlink
*mlink
, *mlink_dest
;
1094 enum mandoclevel lvl
;
1096 str_info
.alloc
= hash_alloc
;
1097 str_info
.calloc
= hash_calloc
;
1098 str_info
.free
= hash_free
;
1099 str_info
.key_offset
= offsetof(struct str
, key
);
1102 SQL_EXEC("BEGIN TRANSACTION");
1104 mpage
= ohash_first(&mpages
, &pslot
);
1105 while (NULL
!= mpage
) {
1106 mlinks_undupe(mpage
);
1107 if (NULL
== mpage
->mlinks
) {
1108 mpage
= ohash_next(&mpages
, &pslot
);
1112 name_mask
= NAME_MASK
;
1113 ohash_init(&names
, 4, &str_info
);
1114 ohash_init(&strings
, 6, &str_info
);
1123 if (mpage
->mlinks
->gzip
) {
1124 if (-1 == pipe(fd
)) {
1125 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1126 say(mpage
->mlinks
->file
, "&pipe gunzip");
1129 switch (child_pid
= fork()) {
1131 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1132 say(mpage
->mlinks
->file
, "&fork gunzip");
1139 if (-1 == dup2(fd
[1], STDOUT_FILENO
)) {
1140 say(mpage
->mlinks
->file
,
1144 execlp("gunzip", "gunzip", "-c",
1145 mpage
->mlinks
->file
, NULL
);
1146 say(mpage
->mlinks
->file
, "&exec gunzip");
1155 * Try interpreting the file as mdoc(7) or man(7)
1156 * source code, unless it is already known to be
1157 * formatted. Fall back to formatted mode.
1159 if (FORM_CAT
!= mpage
->mlinks
->dform
||
1160 FORM_CAT
!= mpage
->mlinks
->fform
) {
1161 lvl
= mparse_readfd(mp
, fd
[0], mpage
->mlinks
->file
);
1162 if (lvl
< MANDOCLEVEL_FATAL
)
1163 mparse_result(mp
, &mdoc
, &man
, &sodest
);
1166 if (NULL
!= sodest
) {
1167 mlink_dest
= ohash_find(&mlinks
,
1168 ohash_qlookup(&mlinks
, sodest
));
1169 if (NULL
!= mlink_dest
) {
1171 /* The .so target exists. */
1173 mpage_dest
= mlink_dest
->mpage
;
1174 mlink
= mpage
->mlinks
;
1176 mlink
->mpage
= mpage_dest
;
1179 * If the target was already
1180 * processed, add the links
1181 * to the database now.
1182 * Otherwise, this will
1183 * happen when we come
1187 if (mpage_dest
->pageid
)
1190 if (NULL
== mlink
->next
)
1192 mlink
= mlink
->next
;
1195 /* Move all links to the target. */
1197 mlink
->next
= mlink_dest
->next
;
1198 mlink_dest
->next
= mpage
->mlinks
;
1199 mpage
->mlinks
= NULL
;
1202 } else if (NULL
!= mdoc
) {
1203 mpage
->form
= FORM_SRC
;
1204 mpage
->sec
= mdoc_meta(mdoc
)->msec
;
1205 mpage
->sec
= mandoc_strdup(
1206 NULL
== mpage
->sec
? "" : mpage
->sec
);
1207 mpage
->arch
= mdoc_meta(mdoc
)->arch
;
1208 mpage
->arch
= mandoc_strdup(
1209 NULL
== mpage
->arch
? "" : mpage
->arch
);
1211 mandoc_strdup(mdoc_meta(mdoc
)->title
);
1212 } else if (NULL
!= man
) {
1213 mpage
->form
= FORM_SRC
;
1215 mandoc_strdup(man_meta(man
)->msec
);
1217 mandoc_strdup(mpage
->mlinks
->arch
);
1219 mandoc_strdup(man_meta(man
)->title
);
1221 mpage
->form
= FORM_CAT
;
1223 mandoc_strdup(mpage
->mlinks
->dsec
);
1225 mandoc_strdup(mpage
->mlinks
->arch
);
1227 mandoc_strdup(mpage
->mlinks
->name
);
1229 putkey(mpage
, mpage
->sec
, TYPE_sec
);
1230 putkey(mpage
, '\0' == *mpage
->arch
?
1231 any
: mpage
->arch
, TYPE_arch
);
1233 for (mlink
= mpage
->mlinks
; mlink
; mlink
= mlink
->next
) {
1234 if ('\0' != *mlink
->dsec
)
1235 putkey(mpage
, mlink
->dsec
, TYPE_sec
);
1236 if ('\0' != *mlink
->fsec
)
1237 putkey(mpage
, mlink
->fsec
, TYPE_sec
);
1238 putkey(mpage
, '\0' == *mlink
->arch
?
1239 any
: mlink
->arch
, TYPE_arch
);
1240 putkey(mpage
, mlink
->name
, NAME_FILE
);
1243 assert(NULL
== mpage
->desc
);
1245 if (NULL
!= (cp
= mdoc_meta(mdoc
)->name
))
1246 putkey(mpage
, cp
, NAME_HEAD
);
1247 parse_mdoc(mpage
, mdoc_node(mdoc
));
1248 } else if (NULL
!= man
)
1249 parse_man(mpage
, man_node(man
));
1251 parse_cat(mpage
, fd
[0]);
1252 if (NULL
== mpage
->desc
)
1253 mpage
->desc
= mandoc_strdup(mpage
->mlinks
->name
);
1255 if (warnings
&& !use_all
)
1256 for (mlink
= mpage
->mlinks
; mlink
;
1257 mlink
= mlink
->next
)
1258 mlink_check(mpage
, mlink
);
1264 if (-1 == waitpid(child_pid
, &status
, 0)) {
1265 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1266 say(mpage
->mlinks
->file
, "&wait gunzip");
1267 } else if (WIFSIGNALED(status
)) {
1268 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1269 say(mpage
->mlinks
->file
,
1270 "gunzip died from signal %d",
1272 } else if (WEXITSTATUS(status
)) {
1273 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1274 say(mpage
->mlinks
->file
,
1275 "gunzip failed with code %d",
1276 WEXITSTATUS(status
));
1279 ohash_delete(&strings
);
1280 ohash_delete(&names
);
1281 mpage
= ohash_next(&mpages
, &pslot
);
1285 SQL_EXEC("END TRANSACTION");
1292 const char *name
, *sec
, *arch
, *key
;
1295 sqlite3_prepare_v2(db
,
1296 "SELECT name, sec, arch, key FROM ("
1297 "SELECT name AS key, pageid FROM names "
1298 "WHERE bits & ? AND NOT EXISTS ("
1299 "SELECT pageid FROM mlinks "
1300 "WHERE mlinks.pageid == names.pageid "
1301 "AND mlinks.name == names.name"
1304 "SELECT sec, arch, name, pageid FROM mlinks "
1306 ") USING (pageid);",
1309 if (SQLITE_OK
!= sqlite3_bind_int64(stmt
, 1, NAME_TITLE
))
1310 say("", "%s", sqlite3_errmsg(db
));
1312 while (SQLITE_ROW
== (irc
= sqlite3_step(stmt
))) {
1313 name
= (const char *)sqlite3_column_text(stmt
, 0);
1314 sec
= (const char *)sqlite3_column_text(stmt
, 1);
1315 arch
= (const char *)sqlite3_column_text(stmt
, 2);
1316 key
= (const char *)sqlite3_column_text(stmt
, 3);
1317 say("", "%s(%s%s%s) lacks mlink \"%s\"", name
, sec
,
1318 '\0' == *arch
? "" : "/",
1319 '\0' == *arch
? "" : arch
, key
);
1321 sqlite3_finalize(stmt
);
1325 parse_cat(struct mpage
*mpage
, int fd
)
1328 char *line
, *p
, *title
;
1329 size_t len
, plen
, titlesz
;
1331 stream
= (-1 == fd
) ?
1332 fopen(mpage
->mlinks
->file
, "r") :
1334 if (NULL
== stream
) {
1336 say(mpage
->mlinks
->file
, "&fopen");
1340 /* Skip to first blank line. */
1342 while (NULL
!= (line
= fgetln(stream
, &len
)))
1347 * Assume the first line that is not indented
1348 * is the first section header. Skip to it.
1351 while (NULL
!= (line
= fgetln(stream
, &len
)))
1352 if ('\n' != *line
&& ' ' != *line
)
1356 * Read up until the next section into a buffer.
1357 * Strip the leading and trailing newline from each read line,
1358 * appending a trailing space.
1359 * Ignore empty (whitespace-only) lines.
1365 while (NULL
!= (line
= fgetln(stream
, &len
))) {
1366 if (' ' != *line
|| '\n' != line
[len
- 1])
1368 while (len
> 0 && isspace((unsigned char)*line
)) {
1374 title
= mandoc_realloc(title
, titlesz
+ len
);
1375 memcpy(title
+ titlesz
, line
, len
);
1377 title
[titlesz
- 1] = ' ';
1381 * If no page content can be found, or the input line
1382 * is already the next section header, or there is no
1383 * trailing newline, reuse the page title as the page
1387 if (NULL
== title
|| '\0' == *title
) {
1389 say(mpage
->mlinks
->file
,
1390 "Cannot find NAME section");
1396 title
= mandoc_realloc(title
, titlesz
+ 1);
1397 title
[titlesz
] = '\0';
1400 * Skip to the first dash.
1401 * Use the remaining line as the description (no more than 70
1405 if (NULL
!= (p
= strstr(title
, "- "))) {
1406 for (p
+= 2; ' ' == *p
|| '\b' == *p
; p
++)
1407 /* Skip to next word. */ ;
1410 say(mpage
->mlinks
->file
,
1411 "No dash in title line");
1417 /* Strip backspace-encoding from line. */
1419 while (NULL
!= (line
= memchr(p
, '\b', plen
))) {
1422 memmove(line
, line
+ 1, plen
--);
1425 memmove(line
- 1, line
+ 1, plen
- len
);
1429 mpage
->desc
= mandoc_strdup(p
);
1435 * Put a type/word pair into the word database for this particular file.
1438 putkey(const struct mpage
*mpage
, char *value
, uint64_t type
)
1442 assert(NULL
!= value
);
1443 if (TYPE_arch
== type
)
1444 for (cp
= value
; *cp
; cp
++)
1445 if (isupper((unsigned char)*cp
))
1446 *cp
= _tolower((unsigned char)*cp
);
1447 putkeys(mpage
, value
, strlen(value
), type
);
1451 * Grok all nodes at or below a certain mdoc node into putkey().
1454 putmdockey(const struct mpage
*mpage
,
1455 const struct mdoc_node
*n
, uint64_t m
)
1458 for ( ; NULL
!= n
; n
= n
->next
) {
1459 if (NULL
!= n
->child
)
1460 putmdockey(mpage
, n
->child
, m
);
1461 if (MDOC_TEXT
== n
->type
)
1462 putkey(mpage
, n
->string
, m
);
1467 parse_man(struct mpage
*mpage
, const struct man_node
*n
)
1469 const struct man_node
*head
, *body
;
1470 char *start
, *title
;
1478 * We're only searching for one thing: the first text child in
1479 * the BODY of a NAME section. Since we don't keep track of
1480 * sections in -man, run some hoops to find out whether we're in
1481 * the correct section or not.
1484 if (MAN_BODY
== n
->type
&& MAN_SH
== n
->tok
) {
1486 assert(body
->parent
);
1487 if (NULL
!= (head
= body
->parent
->head
) &&
1488 1 == head
->nchild
&&
1489 NULL
!= (head
= (head
->child
)) &&
1490 MAN_TEXT
== head
->type
&&
1491 0 == strcmp(head
->string
, "NAME") &&
1492 NULL
!= body
->child
) {
1495 * Suck the entire NAME section into memory.
1496 * Yes, we might run away.
1497 * But too many manuals have big, spread-out
1498 * NAME sections over many lines.
1502 man_deroff(&title
, body
);
1507 * Go through a special heuristic dance here.
1508 * Conventionally, one or more manual names are
1509 * comma-specified prior to a whitespace, then a
1510 * dash, then a description. Try to puzzle out
1511 * the name parts here.
1516 sz
= strcspn(start
, " ,");
1517 if ('\0' == start
[sz
])
1524 * Assume a stray trailing comma in the
1525 * name list if a name begins with a dash.
1528 if ('-' == start
[0] ||
1529 ('\\' == start
[0] && '-' == start
[1]))
1532 putkey(mpage
, start
, NAME_TITLE
);
1539 assert(',' == byte
);
1541 while (' ' == *start
)
1545 if (start
== title
) {
1546 putkey(mpage
, start
, NAME_TITLE
);
1551 while (isspace((unsigned char)*start
))
1554 if (0 == strncmp(start
, "-", 1))
1556 else if (0 == strncmp(start
, "\\-\\-", 4))
1558 else if (0 == strncmp(start
, "\\-", 2))
1560 else if (0 == strncmp(start
, "\\(en", 4))
1562 else if (0 == strncmp(start
, "\\(em", 4))
1565 while (' ' == *start
)
1568 mpage
->desc
= mandoc_strdup(start
);
1574 for (n
= n
->child
; n
; n
= n
->next
) {
1575 if (NULL
!= mpage
->desc
)
1577 parse_man(mpage
, n
);
1582 parse_mdoc(struct mpage
*mpage
, const struct mdoc_node
*n
)
1586 for (n
= n
->child
; NULL
!= n
; n
= n
->next
) {
1597 if (NULL
!= mdocs
[n
->tok
].fp
)
1598 if (0 == (*mdocs
[n
->tok
].fp
)(mpage
, n
))
1600 if (mdocs
[n
->tok
].mask
)
1601 putmdockey(mpage
, n
->child
,
1602 mdocs
[n
->tok
].mask
);
1605 assert(MDOC_ROOT
!= n
->type
);
1608 if (NULL
!= n
->child
)
1609 parse_mdoc(mpage
, n
);
1614 parse_mdoc_Fd(struct mpage
*mpage
, const struct mdoc_node
*n
)
1616 const char *start
, *end
;
1619 if (SEC_SYNOPSIS
!= n
->sec
||
1620 NULL
== (n
= n
->child
) ||
1621 MDOC_TEXT
!= n
->type
)
1625 * Only consider those `Fd' macro fields that begin with an
1626 * "inclusion" token (versus, e.g., #define).
1629 if (strcmp("#include", n
->string
))
1632 if (NULL
== (n
= n
->next
) || MDOC_TEXT
!= n
->type
)
1636 * Strip away the enclosing angle brackets and make sure we're
1641 if ('<' == *start
|| '"' == *start
)
1644 if (0 == (sz
= strlen(start
)))
1647 end
= &start
[(int)sz
- 1];
1648 if ('>' == *end
|| '"' == *end
)
1652 putkeys(mpage
, start
, end
- start
+ 1, TYPE_In
);
1657 parse_mdoc_Fn(struct mpage
*mpage
, const struct mdoc_node
*n
)
1661 if (NULL
== (n
= n
->child
) || MDOC_TEXT
!= n
->type
)
1665 * Parse: .Fn "struct type *name" "char *arg".
1666 * First strip away pointer symbol.
1667 * Then store the function name, then type.
1668 * Finally, store the arguments.
1671 if (NULL
== (cp
= strrchr(n
->string
, ' ')))
1677 putkey(mpage
, cp
, TYPE_Fn
);
1680 putkeys(mpage
, n
->string
, cp
- n
->string
, TYPE_Ft
);
1682 for (n
= n
->next
; NULL
!= n
; n
= n
->next
)
1683 if (MDOC_TEXT
== n
->type
)
1684 putkey(mpage
, n
->string
, TYPE_Fa
);
1690 parse_mdoc_Xr(struct mpage
*mpage
, const struct mdoc_node
*n
)
1694 if (NULL
== (n
= n
->child
))
1697 if (NULL
== n
->next
) {
1698 putkey(mpage
, n
->string
, TYPE_Xr
);
1702 mandoc_asprintf(&cp
, "%s(%s)", n
->string
, n
->next
->string
);
1703 putkey(mpage
, cp
, TYPE_Xr
);
1709 parse_mdoc_Nd(struct mpage
*mpage
, const struct mdoc_node
*n
)
1712 if (MDOC_BODY
== n
->type
)
1713 mdoc_deroff(&mpage
->desc
, n
);
1718 parse_mdoc_Nm(struct mpage
*mpage
, const struct mdoc_node
*n
)
1721 if (SEC_NAME
== n
->sec
)
1722 putmdockey(mpage
, n
->child
, NAME_TITLE
);
1723 else if (SEC_SYNOPSIS
== n
->sec
&& MDOC_HEAD
== n
->type
)
1724 putmdockey(mpage
, n
->child
, NAME_SYN
);
1729 parse_mdoc_Sh(struct mpage
*mpage
, const struct mdoc_node
*n
)
1732 return(SEC_CUSTOM
== n
->sec
&& MDOC_HEAD
== n
->type
);
1736 parse_mdoc_head(struct mpage
*mpage
, const struct mdoc_node
*n
)
1739 return(MDOC_HEAD
== n
->type
);
1743 parse_mdoc_body(struct mpage
*mpage
, const struct mdoc_node
*n
)
1746 return(MDOC_BODY
== n
->type
);
1750 * Add a string to the hash table for the current manual.
1751 * Each string has a bitmask telling which macros it belongs to.
1752 * When we finish the manual, we'll dump the table.
1755 putkeys(const struct mpage
*mpage
,
1756 const char *cp
, size_t sz
, uint64_t v
)
1770 name_mask
&= ~NAME_FIRST
;
1772 say(mpage
->mlinks
->file
,
1773 "Adding name %*s", sz
, cp
);
1777 for (i
= 0; i
< mansearch_keymax
; i
++)
1779 say(mpage
->mlinks
->file
,
1780 "Adding key %s=%*s",
1781 mansearch_keynames
[i
], sz
, cp
);
1785 slot
= ohash_qlookupi(htab
, cp
, &end
);
1786 s
= ohash_find(htab
, slot
);
1788 if (NULL
!= s
&& mpage
== s
->mpage
) {
1791 } else if (NULL
== s
) {
1792 s
= mandoc_calloc(1, sizeof(struct str
) + sz
+ 1);
1793 memcpy(s
->key
, cp
, sz
);
1794 ohash_insert(htab
, slot
, s
);
1801 * Take a Unicode codepoint and produce its UTF-8 encoding.
1802 * This isn't the best way to do this, but it works.
1803 * The magic numbers are from the UTF-8 packaging.
1804 * They're not as scary as they seem: read the UTF-8 spec for details.
1807 utf8(unsigned int cp
, char out
[7])
1812 if (cp
<= 0x0000007F) {
1815 } else if (cp
<= 0x000007FF) {
1817 out
[0] = (cp
>> 6 & 31) | 192;
1818 out
[1] = (cp
& 63) | 128;
1819 } else if (cp
<= 0x0000FFFF) {
1821 out
[0] = (cp
>> 12 & 15) | 224;
1822 out
[1] = (cp
>> 6 & 63) | 128;
1823 out
[2] = (cp
& 63) | 128;
1824 } else if (cp
<= 0x001FFFFF) {
1826 out
[0] = (cp
>> 18 & 7) | 240;
1827 out
[1] = (cp
>> 12 & 63) | 128;
1828 out
[2] = (cp
>> 6 & 63) | 128;
1829 out
[3] = (cp
& 63) | 128;
1830 } else if (cp
<= 0x03FFFFFF) {
1832 out
[0] = (cp
>> 24 & 3) | 248;
1833 out
[1] = (cp
>> 18 & 63) | 128;
1834 out
[2] = (cp
>> 12 & 63) | 128;
1835 out
[3] = (cp
>> 6 & 63) | 128;
1836 out
[4] = (cp
& 63) | 128;
1837 } else if (cp
<= 0x7FFFFFFF) {
1839 out
[0] = (cp
>> 30 & 1) | 252;
1840 out
[1] = (cp
>> 24 & 63) | 128;
1841 out
[2] = (cp
>> 18 & 63) | 128;
1842 out
[3] = (cp
>> 12 & 63) | 128;
1843 out
[4] = (cp
>> 6 & 63) | 128;
1844 out
[5] = (cp
& 63) | 128;
1853 * Store the rendered version of a key, or alias the pointer
1854 * if the key contains no escape sequences.
1857 render_key(struct mchars
*mc
, struct str
*key
)
1859 size_t sz
, bsz
, pos
;
1860 char utfbuf
[7], res
[6];
1862 const char *seq
, *cpp
, *val
;
1864 enum mandoc_esc esc
;
1866 assert(NULL
== key
->rendered
);
1870 res
[2] = ASCII_NBRSP
;
1871 res
[3] = ASCII_HYPH
;
1872 res
[4] = ASCII_BREAK
;
1879 * Pre-check: if we have no stop-characters, then set the
1880 * pointer as ourselvse and get out of here.
1882 if (strcspn(val
, res
) == bsz
) {
1883 key
->rendered
= key
->key
;
1887 /* Pre-allocate by the length of the input */
1889 buf
= mandoc_malloc(++bsz
);
1892 while ('\0' != *val
) {
1894 * Halt on the first escape sequence.
1895 * This also halts on the end of string, in which case
1896 * we just copy, fallthrough, and exit the loop.
1898 if ((sz
= strcspn(val
, res
)) > 0) {
1899 memcpy(&buf
[pos
], val
, sz
);
1923 /* Read past the slash. */
1928 * Parse the escape sequence and see if it's a
1929 * predefined character or special character.
1932 esc
= mandoc_escape((const char **)&val
,
1934 if (ESCAPE_ERROR
== esc
)
1936 if (ESCAPE_SPECIAL
!= esc
)
1940 * Render the special character
1941 * as either UTF-8 or ASCII.
1945 if (0 == (u
= mchars_spec2cp(mc
, seq
, len
)))
1948 if (0 == (sz
= utf8(u
, utfbuf
)))
1952 cpp
= mchars_spec2str(mc
, seq
, len
, &sz
);
1955 if (ASCII_NBRSP
== *cpp
) {
1961 /* Copy the rendered glyph into the stream. */
1964 buf
= mandoc_realloc(buf
, bsz
);
1965 memcpy(&buf
[pos
], cpp
, sz
);
1970 key
->rendered
= buf
;
1974 dbadd_mlink(const struct mlink
*mlink
)
1979 SQL_BIND_TEXT(stmts
[STMT_INSERT_LINK
], i
, mlink
->dsec
);
1980 SQL_BIND_TEXT(stmts
[STMT_INSERT_LINK
], i
, mlink
->arch
);
1981 SQL_BIND_TEXT(stmts
[STMT_INSERT_LINK
], i
, mlink
->name
);
1982 SQL_BIND_INT64(stmts
[STMT_INSERT_LINK
], i
, mlink
->mpage
->pageid
);
1983 SQL_STEP(stmts
[STMT_INSERT_LINK
]);
1984 sqlite3_reset(stmts
[STMT_INSERT_LINK
]);
1988 * Flush the current page's terms (and their bits) into the database.
1989 * Wrap the entire set of additions in a transaction to make sqlite be a
1991 * Also, handle escape sequences at the last possible moment.
1994 dbadd(struct mpage
*mpage
, struct mchars
*mc
)
1996 struct mlink
*mlink
;
2001 mlink
= mpage
->mlinks
;
2004 for (key
= ohash_first(&names
, &slot
); NULL
!= key
;
2005 key
= ohash_next(&names
, &slot
)) {
2006 if (key
->rendered
!= key
->key
)
2007 free(key
->rendered
);
2010 for (key
= ohash_first(&strings
, &slot
); NULL
!= key
;
2011 key
= ohash_next(&strings
, &slot
)) {
2012 if (key
->rendered
!= key
->key
)
2013 free(key
->rendered
);
2018 while (NULL
!= mlink
) {
2019 fputs(mlink
->name
, stdout
);
2020 if (NULL
== mlink
->next
||
2021 strcmp(mlink
->dsec
, mlink
->next
->dsec
) ||
2022 strcmp(mlink
->fsec
, mlink
->next
->fsec
) ||
2023 strcmp(mlink
->arch
, mlink
->next
->arch
)) {
2025 if ('\0' == *mlink
->dsec
)
2026 fputs(mlink
->fsec
, stdout
);
2028 fputs(mlink
->dsec
, stdout
);
2029 if ('\0' != *mlink
->arch
)
2030 printf("/%s", mlink
->arch
);
2033 mlink
= mlink
->next
;
2035 fputs(", ", stdout
);
2037 printf(" - %s\n", mpage
->desc
);
2042 say(mlink
->file
, "Adding to database");
2044 i
= strlen(mpage
->desc
) + 1;
2045 key
= mandoc_calloc(1, sizeof(struct str
) + i
);
2046 memcpy(key
->key
, mpage
->desc
, i
);
2047 render_key(mc
, key
);
2050 SQL_BIND_TEXT(stmts
[STMT_INSERT_PAGE
], i
, key
->rendered
);
2051 SQL_BIND_INT(stmts
[STMT_INSERT_PAGE
], i
, FORM_SRC
== mpage
->form
);
2052 SQL_STEP(stmts
[STMT_INSERT_PAGE
]);
2053 mpage
->pageid
= sqlite3_last_insert_rowid(db
);
2054 sqlite3_reset(stmts
[STMT_INSERT_PAGE
]);
2056 if (key
->rendered
!= key
->key
)
2057 free(key
->rendered
);
2060 while (NULL
!= mlink
) {
2062 mlink
= mlink
->next
;
2064 mlink
= mpage
->mlinks
;
2066 for (key
= ohash_first(&names
, &slot
); NULL
!= key
;
2067 key
= ohash_next(&names
, &slot
)) {
2068 assert(key
->mpage
== mpage
);
2069 if (NULL
== key
->rendered
)
2070 render_key(mc
, key
);
2072 SQL_BIND_INT64(stmts
[STMT_INSERT_NAME
], i
, key
->mask
);
2073 SQL_BIND_TEXT(stmts
[STMT_INSERT_NAME
], i
, key
->rendered
);
2074 SQL_BIND_INT64(stmts
[STMT_INSERT_NAME
], i
, mpage
->pageid
);
2075 SQL_STEP(stmts
[STMT_INSERT_NAME
]);
2076 sqlite3_reset(stmts
[STMT_INSERT_NAME
]);
2077 if (key
->rendered
!= key
->key
)
2078 free(key
->rendered
);
2081 for (key
= ohash_first(&strings
, &slot
); NULL
!= key
;
2082 key
= ohash_next(&strings
, &slot
)) {
2083 assert(key
->mpage
== mpage
);
2084 if (NULL
== key
->rendered
)
2085 render_key(mc
, key
);
2087 SQL_BIND_INT64(stmts
[STMT_INSERT_KEY
], i
, key
->mask
);
2088 SQL_BIND_TEXT(stmts
[STMT_INSERT_KEY
], i
, key
->rendered
);
2089 SQL_BIND_INT64(stmts
[STMT_INSERT_KEY
], i
, mpage
->pageid
);
2090 SQL_STEP(stmts
[STMT_INSERT_KEY
]);
2091 sqlite3_reset(stmts
[STMT_INSERT_KEY
]);
2092 if (key
->rendered
!= key
->key
)
2093 free(key
->rendered
);
2101 struct mpage
*mpage
;
2102 struct mlink
*mlink
;
2107 SQL_EXEC("BEGIN TRANSACTION");
2109 for (mpage
= ohash_first(&mpages
, &slot
); NULL
!= mpage
;
2110 mpage
= ohash_next(&mpages
, &slot
)) {
2111 mlink
= mpage
->mlinks
;
2113 say(mlink
->file
, "Deleting from database");
2116 for ( ; NULL
!= mlink
; mlink
= mlink
->next
) {
2118 SQL_BIND_TEXT(stmts
[STMT_DELETE_PAGE
],
2120 SQL_BIND_TEXT(stmts
[STMT_DELETE_PAGE
],
2122 SQL_BIND_TEXT(stmts
[STMT_DELETE_PAGE
],
2124 SQL_STEP(stmts
[STMT_DELETE_PAGE
]);
2125 sqlite3_reset(stmts
[STMT_DELETE_PAGE
]);
2130 SQL_EXEC("END TRANSACTION");
2134 * Close an existing database and its prepared statements.
2135 * If "real" is not set, rename the temporary file into the real one.
2147 for (i
= 0; i
< STMT__MAX
; i
++) {
2148 sqlite3_finalize(stmts
[i
]);
2158 if ('\0' == *tempfilename
) {
2159 if (-1 == rename(MANDOC_DB
"~", MANDOC_DB
)) {
2160 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2161 say(MANDOC_DB
, "&rename");
2166 switch (child
= fork()) {
2168 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2169 say("", "&fork cmp");
2172 execlp("cmp", "cmp", "-s",
2173 tempfilename
, MANDOC_DB
, NULL
);
2174 say("", "&exec cmp");
2179 if (-1 == waitpid(child
, &status
, 0)) {
2180 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2181 say("", "&wait cmp");
2182 } else if (WIFSIGNALED(status
)) {
2183 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2184 say("", "cmp died from signal %d", WTERMSIG(status
));
2185 } else if (WEXITSTATUS(status
)) {
2186 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2188 "Data changed, but cannot replace database");
2191 *strrchr(tempfilename
, '/') = '\0';
2192 switch (child
= fork()) {
2194 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2195 say("", "&fork rm");
2198 execlp("rm", "rm", "-rf", tempfilename
, NULL
);
2199 say("", "&exec rm");
2200 exit((int)MANDOCLEVEL_SYSERR
);
2204 if (-1 == waitpid(child
, &status
, 0)) {
2205 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2206 say("", "&wait rm");
2207 } else if (WIFSIGNALED(status
) || WEXITSTATUS(status
)) {
2208 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2209 say("", "%s: Cannot remove temporary directory",
2215 * This is straightforward stuff.
2216 * Open a database connection to a "temporary" database, then open a set
2217 * of prepared statements we'll use over and over again.
2218 * If "real" is set, we use the existing database; if not, we truncate a
2220 * Must be matched by dbclose().
2231 *tempfilename
= '\0';
2232 ofl
= SQLITE_OPEN_READWRITE
;
2235 rc
= sqlite3_open_v2(MANDOC_DB
, &db
, ofl
, NULL
);
2236 if (SQLITE_OK
!= rc
) {
2237 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2238 if (SQLITE_CANTOPEN
!= rc
)
2239 say(MANDOC_DB
, "%s", sqlite3_errstr(rc
));
2242 goto prepare_statements
;
2245 ofl
|= SQLITE_OPEN_CREATE
| SQLITE_OPEN_EXCLUSIVE
;
2247 remove(MANDOC_DB
"~");
2248 rc
= sqlite3_open_v2(MANDOC_DB
"~", &db
, ofl
, NULL
);
2249 if (SQLITE_OK
== rc
)
2251 if (MPARSE_QUICK
& mparse_options
) {
2252 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2253 say(MANDOC_DB
"~", "%s", sqlite3_errstr(rc
));
2257 (void)strlcpy(tempfilename
, "/tmp/mandocdb.XXXXXX",
2258 sizeof(tempfilename
));
2259 if (NULL
== mkdtemp(tempfilename
)) {
2260 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2261 say("", "&%s", tempfilename
);
2264 (void)strlcat(tempfilename
, "/" MANDOC_DB
,
2265 sizeof(tempfilename
));
2266 rc
= sqlite3_open_v2(tempfilename
, &db
, ofl
, NULL
);
2267 if (SQLITE_OK
!= rc
) {
2268 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2269 say("", "%s: %s", tempfilename
, sqlite3_errstr(rc
));
2274 sql
= "CREATE TABLE \"mpages\" (\n"
2275 " \"desc\" TEXT NOT NULL,\n"
2276 " \"form\" INTEGER NOT NULL,\n"
2277 " \"pageid\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
2280 "CREATE TABLE \"mlinks\" (\n"
2281 " \"sec\" TEXT NOT NULL,\n"
2282 " \"arch\" TEXT NOT NULL,\n"
2283 " \"name\" TEXT NOT NULL,\n"
2284 " \"pageid\" INTEGER NOT NULL REFERENCES mpages(pageid) "
2285 "ON DELETE CASCADE\n"
2287 "CREATE INDEX mlinks_pageid_idx ON mlinks (pageid);\n"
2289 "CREATE TABLE \"names\" (\n"
2290 " \"bits\" INTEGER NOT NULL,\n"
2291 " \"name\" TEXT NOT NULL,\n"
2292 " \"pageid\" INTEGER NOT NULL REFERENCES mpages(pageid) "
2293 "ON DELETE CASCADE\n"
2296 "CREATE TABLE \"keys\" (\n"
2297 " \"bits\" INTEGER NOT NULL,\n"
2298 " \"key\" TEXT NOT NULL,\n"
2299 " \"pageid\" INTEGER NOT NULL REFERENCES mpages(pageid) "
2300 "ON DELETE CASCADE\n"
2302 "CREATE INDEX keys_pageid_idx ON keys (pageid);\n";
2304 if (SQLITE_OK
!= sqlite3_exec(db
, sql
, NULL
, NULL
, NULL
)) {
2305 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2306 say(MANDOC_DB
, "%s", sqlite3_errmsg(db
));
2312 if (SQLITE_OK
!= sqlite3_exec(db
,
2313 "PRAGMA foreign_keys = ON", NULL
, NULL
, NULL
)) {
2314 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2315 say(MANDOC_DB
, "PRAGMA foreign_keys: %s",
2316 sqlite3_errmsg(db
));
2321 sql
= "DELETE FROM mpages WHERE pageid IN "
2322 "(SELECT pageid FROM mlinks WHERE "
2323 "sec=? AND arch=? AND name=?)";
2324 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_DELETE_PAGE
], NULL
);
2325 sql
= "INSERT INTO mpages "
2326 "(desc,form) VALUES (?,?)";
2327 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_PAGE
], NULL
);
2328 sql
= "INSERT INTO mlinks "
2329 "(sec,arch,name,pageid) VALUES (?,?,?,?)";
2330 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_LINK
], NULL
);
2331 sql
= "INSERT INTO names "
2332 "(bits,name,pageid) VALUES (?,?,?)";
2333 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_NAME
], NULL
);
2334 sql
= "INSERT INTO keys "
2335 "(bits,key,pageid) VALUES (?,?,?)";
2336 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_KEY
], NULL
);
2340 * When opening a new database, we can turn off
2341 * synchronous mode for much better performance.
2344 if (real
&& SQLITE_OK
!= sqlite3_exec(db
,
2345 "PRAGMA synchronous = OFF", NULL
, NULL
, NULL
)) {
2346 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2347 say(MANDOC_DB
, "PRAGMA synchronous: %s",
2348 sqlite3_errmsg(db
));
2358 hash_calloc(size_t n
, size_t sz
, void *arg
)
2361 return(mandoc_calloc(n
, sz
));
2365 hash_alloc(size_t sz
, void *arg
)
2368 return(mandoc_malloc(sz
));
2372 hash_free(void *p
, void *arg
)
2379 set_basedir(const char *targetdir
)
2381 static char startdir
[PATH_MAX
];
2382 static int getcwd_status
; /* 1 = ok, 2 = failure */
2383 static int chdir_status
; /* 1 = changed directory */
2387 * Remember the original working directory, if possible.
2388 * This will be needed if the second or a later directory
2389 * on the command line is given as a relative path.
2390 * Do not error out if the current directory is not
2391 * searchable: Maybe it won't be needed after all.
2393 if (0 == getcwd_status
) {
2394 if (NULL
== getcwd(startdir
, sizeof(startdir
))) {
2396 (void)strlcpy(startdir
, strerror(errno
),
2403 * We are leaving the old base directory.
2404 * Do not use it any longer, not even for messages.
2409 * If and only if the directory was changed earlier and
2410 * the next directory to process is given as a relative path,
2411 * first go back, or bail out if that is impossible.
2413 if (chdir_status
&& '/' != *targetdir
) {
2414 if (2 == getcwd_status
) {
2415 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2416 say("", "getcwd: %s", startdir
);
2419 if (-1 == chdir(startdir
)) {
2420 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2421 say("", "&chdir %s", startdir
);
2427 * Always resolve basedir to the canonicalized absolute
2428 * pathname and append a trailing slash, such that
2429 * we can reliably check whether files are inside.
2431 if (NULL
== realpath(targetdir
, basedir
)) {
2432 exitcode
= (int)MANDOCLEVEL_BADARG
;
2433 say("", "&%s: realpath", targetdir
);
2435 } else if (-1 == chdir(basedir
)) {
2436 exitcode
= (int)MANDOCLEVEL_BADARG
;
2441 cp
= strchr(basedir
, '\0');
2442 if ('/' != cp
[-1]) {
2443 if (cp
- basedir
>= PATH_MAX
- 1) {
2444 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2445 say("", "Filename too long");
2455 say(const char *file
, const char *format
, ...)
2460 if ('\0' != *basedir
)
2461 fprintf(stderr
, "%s", basedir
);
2462 if ('\0' != *basedir
&& '\0' != *file
)
2465 fprintf(stderr
, "%s", file
);
2468 if (NULL
!= format
) {
2481 if (NULL
!= format
) {
2482 if ('\0' != *basedir
|| '\0' != *file
)
2483 fputs(": ", stderr
);
2484 va_start(ap
, format
);
2485 vfprintf(stderr
, format
, ap
);
2489 if ('\0' != *basedir
|| '\0' != *file
|| NULL
!= format
)
2490 fputs(": ", stderr
);
2493 fputc('\n', stderr
);