1 /* Id: mandocdb.c,v 1.101 2014/01/05 04:48:40 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.
41 #include "compat_ohash.h"
49 #include "mansearch.h"
51 #define SQL_EXEC(_v) \
52 if (SQLITE_OK != sqlite3_exec(db, (_v), NULL, NULL, NULL)) \
53 fprintf(stderr, "%s\n", sqlite3_errmsg(db))
54 #define SQL_BIND_TEXT(_s, _i, _v) \
55 if (SQLITE_OK != sqlite3_bind_text \
56 ((_s), (_i)++, (_v), -1, SQLITE_STATIC)) \
57 fprintf(stderr, "%s\n", sqlite3_errmsg(db))
58 #define SQL_BIND_INT(_s, _i, _v) \
59 if (SQLITE_OK != sqlite3_bind_int \
60 ((_s), (_i)++, (_v))) \
61 fprintf(stderr, "%s\n", sqlite3_errmsg(db))
62 #define SQL_BIND_INT64(_s, _i, _v) \
63 if (SQLITE_OK != sqlite3_bind_int64 \
64 ((_s), (_i)++, (_v))) \
65 fprintf(stderr, "%s\n", sqlite3_errmsg(db))
66 #define SQL_STEP(_s) \
67 if (SQLITE_DONE != sqlite3_step((_s))) \
68 fprintf(stderr, "%s\n", sqlite3_errmsg(db))
71 OP_DEFAULT
= 0, /* new dbs from dir list or default config */
72 OP_CONFFILE
, /* new databases from custom config file */
73 OP_UPDATE
, /* delete/add entries in existing database */
74 OP_DELETE
, /* delete entries from existing database */
75 OP_TEST
/* change no databases, report potential problems */
79 FORM_NONE
, /* format is unknown */
80 FORM_SRC
, /* format is -man or -mdoc */
81 FORM_CAT
/* format is cat */
85 char *rendered
; /* key in UTF-8 or ASCII form */
86 const struct mpage
*mpage
; /* if set, the owning parse */
87 uint64_t mask
; /* bitmask in sequence */
88 char key
[]; /* may contain escape sequences */
97 struct inodev inodev
; /* used for hashing routine */
98 enum form form
; /* format from file content */
99 char *sec
; /* section from file content */
100 char *arch
; /* architecture from file content */
101 char *title
; /* title from file content */
102 char *desc
; /* description from file content */
103 struct mlink
*mlinks
; /* singly linked list */
107 char file
[PATH_MAX
]; /* filename rel. to manpath */
108 enum form dform
; /* format from directory */
109 enum form fform
; /* format from file name suffix */
110 char *dsec
; /* section from directory */
111 char *arch
; /* architecture from directory */
112 char *name
; /* name from file name (not empty) */
113 char *fsec
; /* section from file name suffix */
114 struct mlink
*next
; /* singly linked list */
118 STMT_DELETE_PAGE
= 0, /* delete mpage */
119 STMT_INSERT_PAGE
, /* insert mpage */
120 STMT_INSERT_LINK
, /* insert mlink */
121 STMT_INSERT_KEY
, /* insert parsed key */
125 typedef int (*mdoc_fp
)(struct mpage
*, const struct mdoc_node
*);
127 struct mdoc_handler
{
128 mdoc_fp fp
; /* optional handler */
129 uint64_t mask
; /* set unless handler returns 0 */
132 static void dbclose(int);
133 static void dbindex(const struct mpage
*, struct mchars
*);
134 static int dbopen(int);
135 static void dbprune(void);
136 static void filescan(const char *);
137 static void *hash_alloc(size_t, void *);
138 static void hash_free(void *, size_t, void *);
139 static void *hash_halloc(size_t, void *);
140 static void mlink_add(struct mlink
*, const struct stat
*);
141 static int mlink_check(struct mpage
*, struct mlink
*);
142 static void mlink_free(struct mlink
*);
143 static void mlinks_undupe(struct mpage
*);
144 static void mpages_free(void);
145 static void mpages_merge(struct mchars
*, struct mparse
*);
146 static void parse_cat(struct mpage
*);
147 static void parse_man(struct mpage
*, const struct man_node
*);
148 static void parse_mdoc(struct mpage
*, const struct mdoc_node
*);
149 static int parse_mdoc_body(struct mpage
*, const struct mdoc_node
*);
150 static int parse_mdoc_head(struct mpage
*, const struct mdoc_node
*);
151 static int parse_mdoc_Fd(struct mpage
*, const struct mdoc_node
*);
152 static int parse_mdoc_Fn(struct mpage
*, const struct mdoc_node
*);
153 static int parse_mdoc_Nd(struct mpage
*, const struct mdoc_node
*);
154 static int parse_mdoc_Nm(struct mpage
*, const struct mdoc_node
*);
155 static int parse_mdoc_Sh(struct mpage
*, const struct mdoc_node
*);
156 static int parse_mdoc_Xr(struct mpage
*, const struct mdoc_node
*);
157 static void putkey(const struct mpage
*,
158 const char *, uint64_t);
159 static void putkeys(const struct mpage
*,
160 const char *, size_t, uint64_t);
161 static void putmdockey(const struct mpage
*,
162 const struct mdoc_node
*, uint64_t);
163 static void render_key(struct mchars
*, struct str
*);
164 static void say(const char *, const char *, ...);
165 static int set_basedir(const char *);
166 static int treescan(void);
167 static size_t utf8(unsigned int, char [7]);
169 static char *progname
;
170 static int use_all
; /* use all found files */
171 static int nodb
; /* no database changes */
172 static int verb
; /* print what we're doing */
173 static int warnings
; /* warn about crap */
174 static int write_utf8
; /* write UTF-8 output; else ASCII */
175 static int exitcode
; /* to be returned by main */
176 static enum op op
; /* operational mode */
177 static char basedir
[PATH_MAX
]; /* current base directory */
178 static struct ohash mpages
; /* table of distinct manual pages */
179 static struct ohash mlinks
; /* table of directory entries */
180 static struct ohash strings
; /* table of all strings */
181 static sqlite3
*db
= NULL
; /* current database */
182 static sqlite3_stmt
*stmts
[STMT__MAX
]; /* current statements */
184 static const struct mdoc_handler mdocs
[MDOC_MAX
] = {
185 { NULL
, 0 }, /* Ap */
186 { NULL
, 0 }, /* Dd */
187 { NULL
, 0 }, /* Dt */
188 { NULL
, 0 }, /* Os */
189 { parse_mdoc_Sh
, TYPE_Sh
}, /* Sh */
190 { parse_mdoc_head
, TYPE_Ss
}, /* Ss */
191 { NULL
, 0 }, /* Pp */
192 { NULL
, 0 }, /* D1 */
193 { NULL
, 0 }, /* Dl */
194 { NULL
, 0 }, /* Bd */
195 { NULL
, 0 }, /* Ed */
196 { NULL
, 0 }, /* Bl */
197 { NULL
, 0 }, /* El */
198 { NULL
, 0 }, /* It */
199 { NULL
, 0 }, /* Ad */
200 { NULL
, TYPE_An
}, /* An */
201 { NULL
, TYPE_Ar
}, /* Ar */
202 { NULL
, TYPE_Cd
}, /* Cd */
203 { NULL
, TYPE_Cm
}, /* Cm */
204 { NULL
, TYPE_Dv
}, /* Dv */
205 { NULL
, TYPE_Er
}, /* Er */
206 { NULL
, TYPE_Ev
}, /* Ev */
207 { NULL
, 0 }, /* Ex */
208 { NULL
, TYPE_Fa
}, /* Fa */
209 { parse_mdoc_Fd
, 0 }, /* Fd */
210 { NULL
, TYPE_Fl
}, /* Fl */
211 { parse_mdoc_Fn
, 0 }, /* Fn */
212 { NULL
, TYPE_Ft
}, /* Ft */
213 { NULL
, TYPE_Ic
}, /* Ic */
214 { NULL
, TYPE_In
}, /* In */
215 { NULL
, TYPE_Li
}, /* Li */
216 { parse_mdoc_Nd
, TYPE_Nd
}, /* Nd */
217 { parse_mdoc_Nm
, TYPE_Nm
}, /* Nm */
218 { NULL
, 0 }, /* Op */
219 { NULL
, 0 }, /* Ot */
220 { NULL
, TYPE_Pa
}, /* Pa */
221 { NULL
, 0 }, /* Rv */
222 { NULL
, TYPE_St
}, /* St */
223 { NULL
, TYPE_Va
}, /* Va */
224 { parse_mdoc_body
, TYPE_Va
}, /* Vt */
225 { parse_mdoc_Xr
, 0 }, /* Xr */
226 { NULL
, 0 }, /* %A */
227 { NULL
, 0 }, /* %B */
228 { NULL
, 0 }, /* %D */
229 { NULL
, 0 }, /* %I */
230 { NULL
, 0 }, /* %J */
231 { NULL
, 0 }, /* %N */
232 { NULL
, 0 }, /* %O */
233 { NULL
, 0 }, /* %P */
234 { NULL
, 0 }, /* %R */
235 { NULL
, 0 }, /* %T */
236 { NULL
, 0 }, /* %V */
237 { NULL
, 0 }, /* Ac */
238 { NULL
, 0 }, /* Ao */
239 { NULL
, 0 }, /* Aq */
240 { NULL
, TYPE_At
}, /* At */
241 { NULL
, 0 }, /* Bc */
242 { NULL
, 0 }, /* Bf */
243 { NULL
, 0 }, /* Bo */
244 { NULL
, 0 }, /* Bq */
245 { NULL
, TYPE_Bsx
}, /* Bsx */
246 { NULL
, TYPE_Bx
}, /* Bx */
247 { NULL
, 0 }, /* Db */
248 { NULL
, 0 }, /* Dc */
249 { NULL
, 0 }, /* Do */
250 { NULL
, 0 }, /* Dq */
251 { NULL
, 0 }, /* Ec */
252 { NULL
, 0 }, /* Ef */
253 { NULL
, TYPE_Em
}, /* Em */
254 { NULL
, 0 }, /* Eo */
255 { NULL
, TYPE_Fx
}, /* Fx */
256 { NULL
, TYPE_Ms
}, /* Ms */
257 { NULL
, 0 }, /* No */
258 { NULL
, 0 }, /* Ns */
259 { NULL
, TYPE_Nx
}, /* Nx */
260 { NULL
, TYPE_Ox
}, /* Ox */
261 { NULL
, 0 }, /* Pc */
262 { NULL
, 0 }, /* Pf */
263 { NULL
, 0 }, /* Po */
264 { NULL
, 0 }, /* Pq */
265 { NULL
, 0 }, /* Qc */
266 { NULL
, 0 }, /* Ql */
267 { NULL
, 0 }, /* Qo */
268 { NULL
, 0 }, /* Qq */
269 { NULL
, 0 }, /* Re */
270 { NULL
, 0 }, /* Rs */
271 { NULL
, 0 }, /* Sc */
272 { NULL
, 0 }, /* So */
273 { NULL
, 0 }, /* Sq */
274 { NULL
, 0 }, /* Sm */
275 { NULL
, 0 }, /* Sx */
276 { NULL
, TYPE_Sy
}, /* Sy */
277 { NULL
, TYPE_Tn
}, /* Tn */
278 { NULL
, 0 }, /* Ux */
279 { NULL
, 0 }, /* Xc */
280 { NULL
, 0 }, /* Xo */
281 { parse_mdoc_head
, 0 }, /* Fo */
282 { NULL
, 0 }, /* Fc */
283 { NULL
, 0 }, /* Oo */
284 { NULL
, 0 }, /* Oc */
285 { NULL
, 0 }, /* Bk */
286 { NULL
, 0 }, /* Ek */
287 { NULL
, 0 }, /* Bt */
288 { NULL
, 0 }, /* Hf */
289 { NULL
, 0 }, /* Fr */
290 { NULL
, 0 }, /* Ud */
291 { NULL
, TYPE_Lb
}, /* Lb */
292 { NULL
, 0 }, /* Lp */
293 { NULL
, TYPE_Lk
}, /* Lk */
294 { NULL
, TYPE_Mt
}, /* Mt */
295 { NULL
, 0 }, /* Brq */
296 { NULL
, 0 }, /* Bro */
297 { NULL
, 0 }, /* Brc */
298 { NULL
, 0 }, /* %C */
299 { NULL
, 0 }, /* Es */
300 { NULL
, 0 }, /* En */
301 { NULL
, TYPE_Dx
}, /* Dx */
302 { NULL
, 0 }, /* %Q */
303 { NULL
, 0 }, /* br */
304 { NULL
, 0 }, /* sp */
305 { NULL
, 0 }, /* %U */
306 { NULL
, 0 }, /* Ta */
310 main(int argc
, char *argv
[])
314 const char *path_arg
;
316 struct manpaths dirs
;
318 struct ohash_info mpages_info
, mlinks_info
;
320 memset(stmts
, 0, STMT__MAX
* sizeof(sqlite3_stmt
*));
321 memset(&dirs
, 0, sizeof(struct manpaths
));
323 mpages_info
.alloc
= mlinks_info
.alloc
= hash_alloc
;
324 mpages_info
.halloc
= mlinks_info
.halloc
= hash_halloc
;
325 mpages_info
.hfree
= mlinks_info
.hfree
= hash_free
;
327 mpages_info
.key_offset
= offsetof(struct mpage
, inodev
);
328 mlinks_info
.key_offset
= offsetof(struct mlink
, file
);
330 progname
= strrchr(argv
[0], '/');
331 if (progname
== NULL
)
337 * We accept a few different invocations.
338 * The CHECKOP macro makes sure that invocation styles don't
339 * clobber each other.
341 #define CHECKOP(_op, _ch) do \
342 if (OP_DEFAULT != (_op)) { \
343 fprintf(stderr, "-%c: Conflicting option\n", (_ch)); \
345 } while (/*CONSTCOND*/0)
350 while (-1 != (ch
= getopt(argc
, argv
, "aC:d:nT:tu:vW")))
369 if (strcmp(optarg
, "utf8")) {
370 fprintf(stderr
, "-T%s: Unsupported "
371 "output format\n", optarg
);
378 dup2(STDOUT_FILENO
, STDERR_FILENO
);
400 if (OP_CONFFILE
== op
&& argc
> 0) {
401 fprintf(stderr
, "-C: Too many arguments\n");
405 exitcode
= (int)MANDOCLEVEL_OK
;
406 mp
= mparse_alloc(MPARSE_AUTO
,
407 MANDOCLEVEL_FATAL
, NULL
, NULL
, NULL
);
410 ohash_init(&mpages
, 6, &mpages_info
);
411 ohash_init(&mlinks
, 6, &mlinks_info
);
413 if (OP_UPDATE
== op
|| OP_DELETE
== op
|| OP_TEST
== op
) {
415 * Force processing all files.
420 * All of these deal with a specific directory.
421 * Jump into that directory then collect files specified
422 * on the command-line.
424 if (0 == set_basedir(path_arg
))
426 for (i
= 0; i
< argc
; i
++)
433 mpages_merge(mc
, mp
);
437 * If we have arguments, use them as our manpaths.
438 * If we don't, grok from manpath(1) or however else
439 * manpath_parse() wants to do it.
442 dirs
.paths
= mandoc_calloc
443 (argc
, sizeof(char *));
444 dirs
.sz
= (size_t)argc
;
445 for (i
= 0; i
< argc
; i
++)
446 dirs
.paths
[i
] = mandoc_strdup(argv
[i
]);
448 manpath_parse(&dirs
, path_arg
, NULL
, NULL
);
451 * First scan the tree rooted at a base directory, then
452 * build a new database and finally move it into place.
453 * Ignore zero-length directories and strip trailing
456 for (j
= 0; j
< dirs
.sz
; j
++) {
457 sz
= strlen(dirs
.paths
[j
]);
458 if (sz
&& '/' == dirs
.paths
[j
][sz
- 1])
459 dirs
.paths
[j
][--sz
] = '\0';
464 ohash_init(&mpages
, 6, &mpages_info
);
465 ohash_init(&mlinks
, 6, &mlinks_info
);
468 if (0 == set_basedir(dirs
.paths
[j
]))
472 if (0 == set_basedir(dirs
.paths
[j
]))
477 mpages_merge(mc
, mp
);
480 if (j
+ 1 < dirs
.sz
) {
482 ohash_delete(&mpages
);
483 ohash_delete(&mlinks
);
493 ohash_delete(&mpages
);
494 ohash_delete(&mlinks
);
497 fprintf(stderr
, "usage: %s [-anvW] [-C file] [-Tutf8]\n"
498 " %s [-anvW] [-Tutf8] dir ...\n"
499 " %s [-nvW] [-Tutf8] -d dir [file ...]\n"
500 " %s [-nvW] -u dir [file ...]\n"
502 progname
, progname
, progname
,
505 return((int)MANDOCLEVEL_BADARG
);
509 * Scan a directory tree rooted at "basedir" for manpages.
510 * We use fts(), scanning directory parts along the way for clues to our
511 * section and architecture.
513 * If use_all has been specified, grok all files.
514 * If not, sanitise paths to the following:
516 * [./]man*[/<arch>]/<name>.<section>
518 * [./]cat<section>[/<arch>]/<name>.0
520 * TODO: accomodate for multi-language directories.
529 char *dsec
, *arch
, *fsec
, *cp
;
534 argv
[1] = (char *)NULL
;
537 * Walk through all components under the directory, using the
538 * logical descent of files.
540 f
= fts_open((char * const *)argv
, FTS_LOGICAL
, NULL
);
542 exitcode
= (int)MANDOCLEVEL_SYSERR
;
550 while (NULL
!= (ff
= fts_read(f
))) {
551 path
= ff
->fts_path
+ 2;
553 * If we're a regular file, add an mlink by using the
554 * stored directory data and handling the filename.
556 if (FTS_F
== ff
->fts_info
) {
557 if (0 == strcmp(path
, MANDOC_DB
))
559 if ( ! use_all
&& ff
->fts_level
< 2) {
561 say(path
, "Extraneous file");
563 } else if (NULL
== (fsec
=
564 strrchr(ff
->fts_name
, '.'))) {
568 "No filename suffix");
571 } else if (0 == strcmp(++fsec
, "html")) {
573 say(path
, "Skip html");
575 } else if (0 == strcmp(fsec
, "gz")) {
577 say(path
, "Skip gz");
579 } else if (0 == strcmp(fsec
, "ps")) {
581 say(path
, "Skip ps");
583 } else if (0 == strcmp(fsec
, "pdf")) {
585 say(path
, "Skip pdf");
587 } else if ( ! use_all
&&
588 ((FORM_SRC
== dform
&& strcmp(fsec
, dsec
)) ||
589 (FORM_CAT
== dform
&& strcmp(fsec
, "0")))) {
591 say(path
, "Wrong filename suffix");
596 mlink
= mandoc_calloc(1, sizeof(struct mlink
));
597 strlcpy(mlink
->file
, path
, sizeof(mlink
->file
));
598 mlink
->dform
= dform
;
601 mlink
->name
= ff
->fts_name
;
603 mlink_add(mlink
, ff
->fts_statp
);
605 } else if (FTS_D
!= ff
->fts_info
&&
606 FTS_DP
!= ff
->fts_info
) {
608 say(path
, "Not a regular file");
612 switch (ff
->fts_level
) {
614 /* Ignore the root directory. */
618 * This might contain manX/ or catX/.
619 * Try to infer this from the name.
620 * If we're not in use_all, enforce it.
623 if (FTS_DP
== ff
->fts_info
)
626 if (0 == strncmp(cp
, "man", 3)) {
629 } else if (0 == strncmp(cp
, "cat", 3)) {
637 if (NULL
!= dsec
|| use_all
)
641 say(path
, "Unknown directory part");
642 fts_set(f
, ff
, FTS_SKIP
);
646 * Possibly our architecture.
647 * If we're descending, keep tabs on it.
649 if (FTS_DP
!= ff
->fts_info
&& NULL
!= dsec
)
655 if (FTS_DP
== ff
->fts_info
|| use_all
)
658 say(path
, "Extraneous directory part");
659 fts_set(f
, ff
, FTS_SKIP
);
669 * Add a file to the mlinks table.
670 * Do not verify that it's a "valid" looking manpage (we'll do that
673 * Try to infer the manual section, architecture, and page name from the
674 * path, assuming it looks like
676 * [./]man*[/<arch>]/<name>.<section>
678 * [./]cat<section>[/<arch>]/<name>.0
680 * See treescan() for the fts(3) version of this.
683 filescan(const char *file
)
692 if (0 == strncmp(file
, "./", 2))
695 if (NULL
== realpath(file
, buf
)) {
696 exitcode
= (int)MANDOCLEVEL_BADARG
;
699 } else if (OP_TEST
!= op
&& strstr(buf
, basedir
) != buf
) {
700 exitcode
= (int)MANDOCLEVEL_BADARG
;
701 say("", "%s: outside base directory", buf
);
703 } else if (-1 == stat(buf
, &st
)) {
704 exitcode
= (int)MANDOCLEVEL_BADARG
;
707 } else if ( ! (S_IFREG
& st
.st_mode
)) {
708 exitcode
= (int)MANDOCLEVEL_BADARG
;
709 say(file
, "Not a regular file");
712 start
= buf
+ strlen(basedir
);
713 mlink
= mandoc_calloc(1, sizeof(struct mlink
));
714 strlcpy(mlink
->file
, start
, sizeof(mlink
->file
));
717 * First try to guess our directory structure.
718 * If we find a separator, try to look for man* or cat*.
719 * If we find one of these and what's underneath is a directory,
720 * assume it's an architecture.
722 if (NULL
!= (p
= strchr(start
, '/'))) {
724 if (0 == strncmp(start
, "man", 3)) {
725 mlink
->dform
= FORM_SRC
;
726 mlink
->dsec
= start
+ 3;
727 } else if (0 == strncmp(start
, "cat", 3)) {
728 mlink
->dform
= FORM_CAT
;
729 mlink
->dsec
= start
+ 3;
733 if (NULL
!= mlink
->dsec
&& NULL
!= (p
= strchr(start
, '/'))) {
741 * Now check the file suffix.
742 * Suffix of `.0' indicates a catpage, `.1-9' is a manpage.
744 p
= strrchr(start
, '\0');
745 while (p
-- > start
&& '/' != *p
&& '.' != *p
)
754 * Now try to parse the name.
755 * Use the filename portion of the path.
758 if (NULL
!= (p
= strrchr(start
, '/'))) {
762 mlink_add(mlink
, &st
);
766 mlink_add(struct mlink
*mlink
, const struct stat
*st
)
768 struct inodev inodev
;
772 assert(NULL
!= mlink
->file
);
774 mlink
->dsec
= mandoc_strdup(mlink
->dsec
? mlink
->dsec
: "");
775 mlink
->arch
= mandoc_strdup(mlink
->arch
? mlink
->arch
: "");
776 mlink
->name
= mandoc_strdup(mlink
->name
? mlink
->name
: "");
777 mlink
->fsec
= mandoc_strdup(mlink
->fsec
? mlink
->fsec
: "");
779 if ('0' == *mlink
->fsec
) {
781 mlink
->fsec
= mandoc_strdup(mlink
->dsec
);
782 mlink
->fform
= FORM_CAT
;
783 } else if ('1' <= *mlink
->fsec
&& '9' >= *mlink
->fsec
)
784 mlink
->fform
= FORM_SRC
;
786 mlink
->fform
= FORM_NONE
;
788 slot
= ohash_qlookup(&mlinks
, mlink
->file
);
789 assert(NULL
== ohash_find(&mlinks
, slot
));
790 ohash_insert(&mlinks
, slot
, mlink
);
792 inodev
.st_ino
= st
->st_ino
;
793 inodev
.st_dev
= st
->st_dev
;
794 slot
= ohash_lookup_memory(&mpages
, (char *)&inodev
,
795 sizeof(struct inodev
), inodev
.st_ino
);
796 mpage
= ohash_find(&mpages
, slot
);
798 mpage
= mandoc_calloc(1, sizeof(struct mpage
));
799 mpage
->inodev
.st_ino
= inodev
.st_ino
;
800 mpage
->inodev
.st_dev
= inodev
.st_dev
;
801 ohash_insert(&mpages
, slot
, mpage
);
803 mlink
->next
= mpage
->mlinks
;
804 mpage
->mlinks
= mlink
;
808 mlink_free(struct mlink
*mlink
)
825 mpage
= ohash_first(&mpages
, &slot
);
826 while (NULL
!= mpage
) {
827 while (NULL
!= (mlink
= mpage
->mlinks
)) {
828 mpage
->mlinks
= mlink
->next
;
836 mpage
= ohash_next(&mpages
, &slot
);
841 * For each mlink to the mpage, check whether the path looks like
842 * it is formatted, and if it does, check whether a source manual
843 * exists by the same name, ignoring the suffix.
844 * If both conditions hold, drop the mlink.
847 mlinks_undupe(struct mpage
*mpage
)
854 mpage
->form
= FORM_CAT
;
855 prev
= &mpage
->mlinks
;
856 while (NULL
!= (mlink
= *prev
)) {
857 if (FORM_CAT
!= mlink
->dform
) {
858 mpage
->form
= FORM_NONE
;
861 if (strlcpy(buf
, mlink
->file
, PATH_MAX
) >= PATH_MAX
) {
863 say(mlink
->file
, "Filename too long");
866 bufp
= strstr(buf
, "cat");
867 assert(NULL
!= bufp
);
868 memcpy(bufp
, "man", 3);
869 if (NULL
!= (bufp
= strrchr(buf
, '.')))
871 strlcat(buf
, mlink
->dsec
, PATH_MAX
);
872 if (NULL
== ohash_find(&mlinks
,
873 ohash_qlookup(&mlinks
, buf
)))
876 say(mlink
->file
, "Man source exists: %s", buf
);
883 prev
= &(*prev
)->next
;
888 mlink_check(struct mpage
*mpage
, struct mlink
*mlink
)
895 * Check whether the manual section given in a file
896 * agrees with the directory where the file is located.
897 * Some manuals have suffixes like (3p) on their
898 * section number either inside the file or in the
899 * directory name, some are linked into more than one
900 * section, like encrypt(1) = makekey(8).
903 if (FORM_SRC
== mpage
->form
&&
904 strcasecmp(mpage
->sec
, mlink
->dsec
)) {
906 say(mlink
->file
, "Section \"%s\" manual in %s directory",
907 mpage
->sec
, mlink
->dsec
);
911 * Manual page directories exist for each kernel
912 * architecture as returned by machine(1).
913 * However, many manuals only depend on the
914 * application architecture as returned by arch(1).
915 * For example, some (2/ARM) manuals are shared
916 * across the "armish" and "zaurus" kernel
918 * A few manuals are even shared across completely
919 * different architectures, for example fdformat(1)
920 * on amd64, i386, sparc, and sparc64.
923 if (strcasecmp(mpage
->arch
, mlink
->arch
)) {
925 say(mlink
->file
, "Architecture \"%s\" manual in "
926 "\"%s\" directory", mpage
->arch
, mlink
->arch
);
929 if (strcasecmp(mpage
->title
, mlink
->name
))
936 * Run through the files in the global vector "mpages"
937 * and add them to the database specified in "basedir".
939 * This handles the parsing scheme itself, using the cues of directory
940 * and filename to determine whether the file is parsable or not.
943 mpages_merge(struct mchars
*mc
, struct mparse
*mp
)
945 struct ohash_info str_info
;
953 enum mandoclevel lvl
;
955 str_info
.alloc
= hash_alloc
;
956 str_info
.halloc
= hash_halloc
;
957 str_info
.hfree
= hash_free
;
958 str_info
.key_offset
= offsetof(struct str
, key
);
960 mpage
= ohash_first(&mpages
, &pslot
);
961 while (NULL
!= mpage
) {
962 mlinks_undupe(mpage
);
963 if (NULL
== mpage
->mlinks
) {
964 mpage
= ohash_next(&mpages
, &pslot
);
968 ohash_init(&strings
, 6, &str_info
);
974 * Try interpreting the file as mdoc(7) or man(7)
975 * source code, unless it is already known to be
976 * formatted. Fall back to formatted mode.
978 if (FORM_CAT
!= mpage
->mlinks
->dform
||
979 FORM_CAT
!= mpage
->mlinks
->fform
) {
980 lvl
= mparse_readfd(mp
, -1, mpage
->mlinks
->file
);
981 if (lvl
< MANDOCLEVEL_FATAL
)
982 mparse_result(mp
, &mdoc
, &man
);
986 mpage
->form
= FORM_SRC
;
988 mandoc_strdup(mdoc_meta(mdoc
)->msec
);
989 mpage
->arch
= mdoc_meta(mdoc
)->arch
;
990 mpage
->arch
= mandoc_strdup(
991 NULL
== mpage
->arch
? "" : mpage
->arch
);
993 mandoc_strdup(mdoc_meta(mdoc
)->title
);
994 } else if (NULL
!= man
) {
995 mpage
->form
= FORM_SRC
;
997 mandoc_strdup(man_meta(man
)->msec
);
999 mandoc_strdup(mpage
->mlinks
->arch
);
1001 mandoc_strdup(man_meta(man
)->title
);
1003 mpage
->form
= FORM_CAT
;
1005 mandoc_strdup(mpage
->mlinks
->dsec
);
1007 mandoc_strdup(mpage
->mlinks
->arch
);
1009 mandoc_strdup(mpage
->mlinks
->name
);
1011 putkey(mpage
, mpage
->sec
, TYPE_sec
);
1012 putkey(mpage
, '\0' == *mpage
->arch
?
1013 "any" : mpage
->arch
, TYPE_arch
);
1015 for (mlink
= mpage
->mlinks
; mlink
; mlink
= mlink
->next
) {
1016 if ('\0' != *mlink
->dsec
)
1017 putkey(mpage
, mlink
->dsec
, TYPE_sec
);
1018 if ('\0' != *mlink
->fsec
)
1019 putkey(mpage
, mlink
->fsec
, TYPE_sec
);
1020 putkey(mpage
, '\0' == *mlink
->arch
?
1021 "any" : mlink
->arch
, TYPE_arch
);
1022 putkey(mpage
, mlink
->name
, TYPE_Nm
);
1025 if (warnings
&& !use_all
) {
1027 for (mlink
= mpage
->mlinks
; mlink
;
1028 mlink
= mlink
->next
)
1029 if (mlink_check(mpage
, mlink
))
1035 if (NULL
!= (cp
= mdoc_meta(mdoc
)->name
))
1036 putkey(mpage
, cp
, TYPE_Nm
);
1037 assert(NULL
== mpage
->desc
);
1038 parse_mdoc(mpage
, mdoc_node(mdoc
));
1039 putkey(mpage
, NULL
!= mpage
->desc
?
1040 mpage
->desc
: mpage
->mlinks
->name
, TYPE_Nd
);
1041 } else if (NULL
!= man
)
1042 parse_man(mpage
, man_node(man
));
1047 ohash_delete(&strings
);
1048 mpage
= ohash_next(&mpages
, &pslot
);
1053 parse_cat(struct mpage
*mpage
)
1056 char *line
, *p
, *title
;
1057 size_t len
, plen
, titlesz
;
1059 if (NULL
== (stream
= fopen(mpage
->mlinks
->file
, "r"))) {
1061 say(mpage
->mlinks
->file
, NULL
);
1065 /* Skip to first blank line. */
1067 while (NULL
!= (line
= fgetln(stream
, &len
)))
1072 * Assume the first line that is not indented
1073 * is the first section header. Skip to it.
1076 while (NULL
!= (line
= fgetln(stream
, &len
)))
1077 if ('\n' != *line
&& ' ' != *line
)
1081 * Read up until the next section into a buffer.
1082 * Strip the leading and trailing newline from each read line,
1083 * appending a trailing space.
1084 * Ignore empty (whitespace-only) lines.
1090 while (NULL
!= (line
= fgetln(stream
, &len
))) {
1091 if (' ' != *line
|| '\n' != line
[len
- 1])
1093 while (len
> 0 && isspace((unsigned char)*line
)) {
1099 title
= mandoc_realloc(title
, titlesz
+ len
);
1100 memcpy(title
+ titlesz
, line
, len
);
1102 title
[titlesz
- 1] = ' ';
1106 * If no page content can be found, or the input line
1107 * is already the next section header, or there is no
1108 * trailing newline, reuse the page title as the page
1112 if (NULL
== title
|| '\0' == *title
) {
1114 say(mpage
->mlinks
->file
,
1115 "Cannot find NAME section");
1116 assert(NULL
== mpage
->desc
);
1117 mpage
->desc
= mandoc_strdup(mpage
->mlinks
->name
);
1118 putkey(mpage
, mpage
->mlinks
->name
, TYPE_Nd
);
1124 title
= mandoc_realloc(title
, titlesz
+ 1);
1125 title
[titlesz
] = '\0';
1128 * Skip to the first dash.
1129 * Use the remaining line as the description (no more than 70
1133 if (NULL
!= (p
= strstr(title
, "- "))) {
1134 for (p
+= 2; ' ' == *p
|| '\b' == *p
; p
++)
1135 /* Skip to next word. */ ;
1138 say(mpage
->mlinks
->file
,
1139 "No dash in title line");
1145 /* Strip backspace-encoding from line. */
1147 while (NULL
!= (line
= memchr(p
, '\b', plen
))) {
1150 memmove(line
, line
+ 1, plen
--);
1153 memmove(line
- 1, line
+ 1, plen
- len
);
1157 assert(NULL
== mpage
->desc
);
1158 mpage
->desc
= mandoc_strdup(p
);
1159 putkey(mpage
, mpage
->desc
, TYPE_Nd
);
1165 * Put a type/word pair into the word database for this particular file.
1168 putkey(const struct mpage
*mpage
, const char *value
, uint64_t type
)
1171 assert(NULL
!= value
);
1172 putkeys(mpage
, value
, strlen(value
), type
);
1176 * Grok all nodes at or below a certain mdoc node into putkey().
1179 putmdockey(const struct mpage
*mpage
,
1180 const struct mdoc_node
*n
, uint64_t m
)
1183 for ( ; NULL
!= n
; n
= n
->next
) {
1184 if (NULL
!= n
->child
)
1185 putmdockey(mpage
, n
->child
, m
);
1186 if (MDOC_TEXT
== n
->type
)
1187 putkey(mpage
, n
->string
, m
);
1192 parse_man(struct mpage
*mpage
, const struct man_node
*n
)
1194 const struct man_node
*head
, *body
;
1195 char *start
, *sv
, *title
;
1203 * We're only searching for one thing: the first text child in
1204 * the BODY of a NAME section. Since we don't keep track of
1205 * sections in -man, run some hoops to find out whether we're in
1206 * the correct section or not.
1209 if (MAN_BODY
== n
->type
&& MAN_SH
== n
->tok
) {
1211 assert(body
->parent
);
1212 if (NULL
!= (head
= body
->parent
->head
) &&
1213 1 == head
->nchild
&&
1214 NULL
!= (head
= (head
->child
)) &&
1215 MAN_TEXT
== head
->type
&&
1216 0 == strcmp(head
->string
, "NAME") &&
1217 NULL
!= (body
= body
->child
) &&
1218 MAN_TEXT
== body
->type
) {
1224 * Suck the entire NAME section into memory.
1225 * Yes, we might run away.
1226 * But too many manuals have big, spread-out
1227 * NAME sections over many lines.
1230 for ( ; NULL
!= body
; body
= body
->next
) {
1231 if (MAN_TEXT
!= body
->type
)
1233 if (0 == (sz
= strlen(body
->string
)))
1235 title
= mandoc_realloc
1236 (title
, titlesz
+ sz
+ 1);
1237 memcpy(title
+ titlesz
, body
->string
, sz
);
1239 title
[titlesz
- 1] = ' ';
1244 title
= mandoc_realloc(title
, titlesz
+ 1);
1245 title
[titlesz
] = '\0';
1247 /* Skip leading space. */
1250 while (isspace((unsigned char)*sv
))
1253 if (0 == (sz
= strlen(sv
))) {
1258 /* Erase trailing space. */
1260 start
= &sv
[sz
- 1];
1261 while (start
> sv
&& isspace((unsigned char)*start
))
1272 * Go through a special heuristic dance here.
1273 * Conventionally, one or more manual names are
1274 * comma-specified prior to a whitespace, then a
1275 * dash, then a description. Try to puzzle out
1276 * the name parts here.
1280 sz
= strcspn(start
, " ,");
1281 if ('\0' == start
[sz
])
1287 putkey(mpage
, start
, TYPE_Nm
);
1294 assert(',' == byte
);
1296 while (' ' == *start
)
1301 putkey(mpage
, start
, TYPE_Nm
);
1306 while (isspace((unsigned char)*start
))
1309 if (0 == strncmp(start
, "-", 1))
1311 else if (0 == strncmp(start
, "\\-\\-", 4))
1313 else if (0 == strncmp(start
, "\\-", 2))
1315 else if (0 == strncmp(start
, "\\(en", 4))
1317 else if (0 == strncmp(start
, "\\(em", 4))
1320 while (' ' == *start
)
1323 assert(NULL
== mpage
->desc
);
1324 mpage
->desc
= mandoc_strdup(start
);
1325 putkey(mpage
, mpage
->desc
, TYPE_Nd
);
1331 for (n
= n
->child
; n
; n
= n
->next
) {
1332 if (NULL
!= mpage
->desc
)
1334 parse_man(mpage
, n
);
1339 parse_mdoc(struct mpage
*mpage
, const struct mdoc_node
*n
)
1343 for (n
= n
->child
; NULL
!= n
; n
= n
->next
) {
1354 if (NULL
!= mdocs
[n
->tok
].fp
)
1355 if (0 == (*mdocs
[n
->tok
].fp
)(mpage
, n
))
1357 if (mdocs
[n
->tok
].mask
)
1358 putmdockey(mpage
, n
->child
,
1359 mdocs
[n
->tok
].mask
);
1362 assert(MDOC_ROOT
!= n
->type
);
1365 if (NULL
!= n
->child
)
1366 parse_mdoc(mpage
, n
);
1371 parse_mdoc_Fd(struct mpage
*mpage
, const struct mdoc_node
*n
)
1373 const char *start
, *end
;
1376 if (SEC_SYNOPSIS
!= n
->sec
||
1377 NULL
== (n
= n
->child
) ||
1378 MDOC_TEXT
!= n
->type
)
1382 * Only consider those `Fd' macro fields that begin with an
1383 * "inclusion" token (versus, e.g., #define).
1386 if (strcmp("#include", n
->string
))
1389 if (NULL
== (n
= n
->next
) || MDOC_TEXT
!= n
->type
)
1393 * Strip away the enclosing angle brackets and make sure we're
1398 if ('<' == *start
|| '"' == *start
)
1401 if (0 == (sz
= strlen(start
)))
1404 end
= &start
[(int)sz
- 1];
1405 if ('>' == *end
|| '"' == *end
)
1409 putkeys(mpage
, start
, end
- start
+ 1, TYPE_In
);
1414 parse_mdoc_Fn(struct mpage
*mpage
, const struct mdoc_node
*n
)
1418 if (NULL
== (n
= n
->child
) || MDOC_TEXT
!= n
->type
)
1422 * Parse: .Fn "struct type *name" "char *arg".
1423 * First strip away pointer symbol.
1424 * Then store the function name, then type.
1425 * Finally, store the arguments.
1428 if (NULL
== (cp
= strrchr(n
->string
, ' ')))
1434 putkey(mpage
, cp
, TYPE_Fn
);
1437 putkeys(mpage
, n
->string
, cp
- n
->string
, TYPE_Ft
);
1439 for (n
= n
->next
; NULL
!= n
; n
= n
->next
)
1440 if (MDOC_TEXT
== n
->type
)
1441 putkey(mpage
, n
->string
, TYPE_Fa
);
1447 parse_mdoc_Xr(struct mpage
*mpage
, const struct mdoc_node
*n
)
1451 if (NULL
== (n
= n
->child
))
1454 if (NULL
== n
->next
) {
1455 putkey(mpage
, n
->string
, TYPE_Xr
);
1459 if (-1 == asprintf(&cp
, "%s(%s)", n
->string
, n
->next
->string
)) {
1461 exit((int)MANDOCLEVEL_SYSERR
);
1463 putkey(mpage
, cp
, TYPE_Xr
);
1469 parse_mdoc_Nd(struct mpage
*mpage
, const struct mdoc_node
*n
)
1473 if (MDOC_BODY
!= n
->type
)
1477 * Special-case the `Nd' because we need to put the description
1478 * into the document table.
1481 for (n
= n
->child
; NULL
!= n
; n
= n
->next
) {
1482 if (MDOC_TEXT
== n
->type
) {
1483 if (NULL
!= mpage
->desc
) {
1484 sz
= strlen(mpage
->desc
) +
1485 strlen(n
->string
) + 2;
1486 mpage
->desc
= mandoc_realloc(
1488 strlcat(mpage
->desc
, " ", sz
);
1489 strlcat(mpage
->desc
, n
->string
, sz
);
1491 mpage
->desc
= mandoc_strdup(n
->string
);
1493 if (NULL
!= n
->child
)
1494 parse_mdoc_Nd(mpage
, n
);
1500 parse_mdoc_Nm(struct mpage
*mpage
, const struct mdoc_node
*n
)
1503 return(SEC_NAME
== n
->sec
||
1504 (SEC_SYNOPSIS
== n
->sec
&& MDOC_HEAD
== n
->type
));
1508 parse_mdoc_Sh(struct mpage
*mpage
, const struct mdoc_node
*n
)
1511 return(SEC_CUSTOM
== n
->sec
&& MDOC_HEAD
== n
->type
);
1515 parse_mdoc_head(struct mpage
*mpage
, const struct mdoc_node
*n
)
1518 return(MDOC_HEAD
== n
->type
);
1522 parse_mdoc_body(struct mpage
*mpage
, const struct mdoc_node
*n
)
1525 return(MDOC_BODY
== n
->type
);
1529 * Add a string to the hash table for the current manual.
1530 * Each string has a bitmask telling which macros it belongs to.
1531 * When we finish the manual, we'll dump the table.
1534 putkeys(const struct mpage
*mpage
,
1535 const char *cp
, size_t sz
, uint64_t v
)
1545 slot
= ohash_qlookupi(&strings
, cp
, &end
);
1546 s
= ohash_find(&strings
, slot
);
1548 if (NULL
!= s
&& mpage
== s
->mpage
) {
1551 } else if (NULL
== s
) {
1552 s
= mandoc_calloc(sizeof(struct str
) + sz
+ 1, 1);
1553 memcpy(s
->key
, cp
, sz
);
1554 ohash_insert(&strings
, slot
, s
);
1561 * Take a Unicode codepoint and produce its UTF-8 encoding.
1562 * This isn't the best way to do this, but it works.
1563 * The magic numbers are from the UTF-8 packaging.
1564 * They're not as scary as they seem: read the UTF-8 spec for details.
1567 utf8(unsigned int cp
, char out
[7])
1572 if (cp
<= 0x0000007F) {
1575 } else if (cp
<= 0x000007FF) {
1577 out
[0] = (cp
>> 6 & 31) | 192;
1578 out
[1] = (cp
& 63) | 128;
1579 } else if (cp
<= 0x0000FFFF) {
1581 out
[0] = (cp
>> 12 & 15) | 224;
1582 out
[1] = (cp
>> 6 & 63) | 128;
1583 out
[2] = (cp
& 63) | 128;
1584 } else if (cp
<= 0x001FFFFF) {
1586 out
[0] = (cp
>> 18 & 7) | 240;
1587 out
[1] = (cp
>> 12 & 63) | 128;
1588 out
[2] = (cp
>> 6 & 63) | 128;
1589 out
[3] = (cp
& 63) | 128;
1590 } else if (cp
<= 0x03FFFFFF) {
1592 out
[0] = (cp
>> 24 & 3) | 248;
1593 out
[1] = (cp
>> 18 & 63) | 128;
1594 out
[2] = (cp
>> 12 & 63) | 128;
1595 out
[3] = (cp
>> 6 & 63) | 128;
1596 out
[4] = (cp
& 63) | 128;
1597 } else if (cp
<= 0x7FFFFFFF) {
1599 out
[0] = (cp
>> 30 & 1) | 252;
1600 out
[1] = (cp
>> 24 & 63) | 128;
1601 out
[2] = (cp
>> 18 & 63) | 128;
1602 out
[3] = (cp
>> 12 & 63) | 128;
1603 out
[4] = (cp
>> 6 & 63) | 128;
1604 out
[5] = (cp
& 63) | 128;
1613 * Store the rendered version of a key, or alias the pointer
1614 * if the key contains no escape sequences.
1617 render_key(struct mchars
*mc
, struct str
*key
)
1619 size_t sz
, bsz
, pos
;
1620 char utfbuf
[7], res
[5];
1622 const char *seq
, *cpp
, *val
;
1624 enum mandoc_esc esc
;
1626 assert(NULL
== key
->rendered
);
1630 res
[2] = ASCII_NBRSP
;
1631 res
[3] = ASCII_HYPH
;
1638 * Pre-check: if we have no stop-characters, then set the
1639 * pointer as ourselvse and get out of here.
1641 if (strcspn(val
, res
) == bsz
) {
1642 key
->rendered
= key
->key
;
1646 /* Pre-allocate by the length of the input */
1648 buf
= mandoc_malloc(++bsz
);
1651 while ('\0' != *val
) {
1653 * Halt on the first escape sequence.
1654 * This also halts on the end of string, in which case
1655 * we just copy, fallthrough, and exit the loop.
1657 if ((sz
= strcspn(val
, res
)) > 0) {
1658 memcpy(&buf
[pos
], val
, sz
);
1663 if (ASCII_HYPH
== *val
) {
1667 } else if ('\t' == *val
|| ASCII_NBRSP
== *val
) {
1671 } else if ('\\' != *val
)
1674 /* Read past the slash. */
1679 * Parse the escape sequence and see if it's a
1680 * predefined character or special character.
1684 ((const char **)&val
, &seq
, &len
);
1685 if (ESCAPE_ERROR
== esc
)
1687 if (ESCAPE_SPECIAL
!= esc
)
1691 * Render the special character
1692 * as either UTF-8 or ASCII.
1696 if (0 == (u
= mchars_spec2cp(mc
, seq
, len
)))
1699 if (0 == (sz
= utf8(u
, utfbuf
)))
1703 cpp
= mchars_spec2str(mc
, seq
, len
, &sz
);
1706 if (ASCII_NBRSP
== *cpp
) {
1712 /* Copy the rendered glyph into the stream. */
1715 buf
= mandoc_realloc(buf
, bsz
);
1716 memcpy(&buf
[pos
], cpp
, sz
);
1721 key
->rendered
= buf
;
1725 * Flush the current page's terms (and their bits) into the database.
1726 * Wrap the entire set of additions in a transaction to make sqlite be a
1728 * Also, handle escape sequences at the last possible moment.
1731 dbindex(const struct mpage
*mpage
, struct mchars
*mc
)
1733 struct mlink
*mlink
;
1741 say(mpage
->mlinks
->file
, "Adding to index");
1747 if (NULL
!= mpage
->desc
&& '\0' != *mpage
->desc
) {
1748 key
= ohash_find(&strings
,
1749 ohash_qlookup(&strings
, mpage
->desc
));
1750 assert(NULL
!= key
);
1751 if (NULL
== key
->rendered
)
1752 render_key(mc
, key
);
1753 desc
= key
->rendered
;
1756 SQL_EXEC("BEGIN TRANSACTION");
1759 SQL_BIND_TEXT(stmts
[STMT_INSERT_PAGE
], i
, desc
);
1760 SQL_BIND_INT(stmts
[STMT_INSERT_PAGE
], i
, FORM_SRC
== mpage
->form
);
1761 SQL_STEP(stmts
[STMT_INSERT_PAGE
]);
1762 recno
= sqlite3_last_insert_rowid(db
);
1763 sqlite3_reset(stmts
[STMT_INSERT_PAGE
]);
1765 for (mlink
= mpage
->mlinks
; mlink
; mlink
= mlink
->next
) {
1767 SQL_BIND_TEXT(stmts
[STMT_INSERT_LINK
], i
, mlink
->file
);
1768 SQL_BIND_TEXT(stmts
[STMT_INSERT_LINK
], i
, mlink
->dsec
);
1769 SQL_BIND_TEXT(stmts
[STMT_INSERT_LINK
], i
, mlink
->arch
);
1770 SQL_BIND_TEXT(stmts
[STMT_INSERT_LINK
], i
, mlink
->name
);
1771 SQL_BIND_INT64(stmts
[STMT_INSERT_LINK
], i
, recno
);
1772 SQL_STEP(stmts
[STMT_INSERT_LINK
]);
1773 sqlite3_reset(stmts
[STMT_INSERT_LINK
]);
1776 for (key
= ohash_first(&strings
, &slot
); NULL
!= key
;
1777 key
= ohash_next(&strings
, &slot
)) {
1778 assert(key
->mpage
== mpage
);
1779 if (NULL
== key
->rendered
)
1780 render_key(mc
, key
);
1782 SQL_BIND_INT64(stmts
[STMT_INSERT_KEY
], i
, key
->mask
);
1783 SQL_BIND_TEXT(stmts
[STMT_INSERT_KEY
], i
, key
->rendered
);
1784 SQL_BIND_INT64(stmts
[STMT_INSERT_KEY
], i
, recno
);
1785 SQL_STEP(stmts
[STMT_INSERT_KEY
]);
1786 sqlite3_reset(stmts
[STMT_INSERT_KEY
]);
1787 if (key
->rendered
!= key
->key
)
1788 free(key
->rendered
);
1792 SQL_EXEC("END TRANSACTION");
1798 struct mpage
*mpage
;
1799 struct mlink
*mlink
;
1806 mpage
= ohash_first(&mpages
, &slot
);
1807 while (NULL
!= mpage
) {
1808 mlink
= mpage
->mlinks
;
1810 SQL_BIND_TEXT(stmts
[STMT_DELETE_PAGE
], i
, mlink
->file
);
1811 SQL_STEP(stmts
[STMT_DELETE_PAGE
]);
1812 sqlite3_reset(stmts
[STMT_DELETE_PAGE
]);
1814 say(mlink
->file
, "Deleted from index");
1815 mpage
= ohash_next(&mpages
, &slot
);
1820 * Close an existing database and its prepared statements.
1821 * If "real" is not set, rename the temporary file into the real one.
1831 for (i
= 0; i
< STMT__MAX
; i
++) {
1832 sqlite3_finalize(stmts
[i
]);
1842 if (-1 == rename(MANDOC_DB
"~", MANDOC_DB
)) {
1843 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1844 say(MANDOC_DB
, NULL
);
1849 * This is straightforward stuff.
1850 * Open a database connection to a "temporary" database, then open a set
1851 * of prepared statements we'll use over and over again.
1852 * If "real" is set, we use the existing database; if not, we truncate a
1854 * Must be matched by dbclose().
1859 const char *file
, *sql
;
1865 ofl
= SQLITE_OPEN_READWRITE
;
1867 file
= MANDOC_DB
"~";
1868 if (-1 == remove(file
) && ENOENT
!= errno
) {
1869 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1873 ofl
|= SQLITE_OPEN_EXCLUSIVE
;
1877 rc
= sqlite3_open_v2(file
, &db
, ofl
, NULL
);
1878 if (SQLITE_OK
== rc
)
1879 goto prepare_statements
;
1880 if (SQLITE_CANTOPEN
!= rc
) {
1881 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1889 if (SQLITE_OK
!= (rc
= sqlite3_open(file
, &db
))) {
1890 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1895 sql
= "CREATE TABLE \"mpages\" (\n"
1896 " \"desc\" TEXT NOT NULL,\n"
1897 " \"form\" INTEGER NOT NULL,\n"
1898 " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
1901 "CREATE TABLE \"mlinks\" (\n"
1902 " \"file\" TEXT NOT NULL,\n"
1903 " \"sec\" TEXT NOT NULL,\n"
1904 " \"arch\" TEXT NOT NULL,\n"
1905 " \"name\" TEXT NOT NULL,\n"
1906 " \"pageid\" INTEGER NOT NULL REFERENCES mpages(id) "
1907 "ON DELETE CASCADE,\n"
1908 " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
1911 "CREATE TABLE \"keys\" (\n"
1912 " \"bits\" INTEGER NOT NULL,\n"
1913 " \"key\" TEXT NOT NULL,\n"
1914 " \"pageid\" INTEGER NOT NULL REFERENCES mpages(id) "
1915 "ON DELETE CASCADE,\n"
1916 " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
1919 "CREATE INDEX \"key_index\" ON keys (key);\n";
1921 if (SQLITE_OK
!= sqlite3_exec(db
, sql
, NULL
, NULL
, NULL
)) {
1922 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1923 say(file
, "%s", sqlite3_errmsg(db
));
1928 SQL_EXEC("PRAGMA foreign_keys = ON");
1929 sql
= "DELETE FROM mpages where file=?";
1930 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_DELETE_PAGE
], NULL
);
1931 sql
= "INSERT INTO mpages "
1932 "(desc,form) VALUES (?,?)";
1933 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_PAGE
], NULL
);
1934 sql
= "INSERT INTO mlinks "
1935 "(file,sec,arch,name,pageid) VALUES (?,?,?,?,?)";
1936 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_LINK
], NULL
);
1937 sql
= "INSERT INTO keys "
1938 "(bits,key,pageid) VALUES (?,?,?)";
1939 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_KEY
], NULL
);
1943 * When opening a new database, we can turn off
1944 * synchronous mode for much better performance.
1948 SQL_EXEC("PRAGMA synchronous = OFF");
1955 hash_halloc(size_t sz
, void *arg
)
1958 return(mandoc_calloc(sz
, 1));
1962 hash_alloc(size_t sz
, void *arg
)
1965 return(mandoc_malloc(sz
));
1969 hash_free(void *p
, size_t sz
, void *arg
)
1976 set_basedir(const char *targetdir
)
1978 static char startdir
[PATH_MAX
];
1982 * Remember where we started by keeping a fd open to the origin
1983 * path component: throughout this utility, we chdir() a lot to
1984 * handle relative paths, and by doing this, we can return to
1985 * the starting point.
1987 if ('\0' == *startdir
) {
1988 if (NULL
== getcwd(startdir
, PATH_MAX
)) {
1989 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1990 if (NULL
!= targetdir
)
1994 if (-1 == (fd
= open(startdir
, O_RDONLY
, 0))) {
1995 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1996 say(startdir
, NULL
);
1999 if (NULL
== targetdir
)
2000 targetdir
= startdir
;
2004 if (-1 == fchdir(fd
)) {
2007 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2008 say(startdir
, NULL
);
2011 if (NULL
== targetdir
) {
2016 if (NULL
== realpath(targetdir
, basedir
)) {
2018 exitcode
= (int)MANDOCLEVEL_BADARG
;
2019 say(targetdir
, NULL
);
2021 } else if (-1 == chdir(basedir
)) {
2022 exitcode
= (int)MANDOCLEVEL_BADARG
;
2030 say(const char *file
, const char *format
, ...)
2034 if ('\0' != *basedir
)
2035 fprintf(stderr
, "%s", basedir
);
2036 if ('\0' != *basedir
&& '\0' != *file
)
2037 fputs("//", stderr
);
2039 fprintf(stderr
, "%s", file
);
2040 fputs(": ", stderr
);
2042 if (NULL
== format
) {
2047 va_start(ap
, format
);
2048 vfprintf(stderr
, format
, ap
);
2051 fputc('\n', stderr
);