]>
git.cameronkatri.com Git - mandoc.git/blob - mandocdb.c
1 /* $Id: mandocdb.c,v 1.114 2014/01/22 20:58:39 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 extern int mansearch_keymax
;
52 extern const char *const mansearch_keynames
[];
54 #define SQL_EXEC(_v) \
55 if (SQLITE_OK != sqlite3_exec(db, (_v), NULL, NULL, NULL)) \
56 fprintf(stderr, "%s\n", sqlite3_errmsg(db))
57 #define SQL_BIND_TEXT(_s, _i, _v) \
58 if (SQLITE_OK != sqlite3_bind_text \
59 ((_s), (_i)++, (_v), -1, SQLITE_STATIC)) \
60 fprintf(stderr, "%s\n", sqlite3_errmsg(db))
61 #define SQL_BIND_INT(_s, _i, _v) \
62 if (SQLITE_OK != sqlite3_bind_int \
63 ((_s), (_i)++, (_v))) \
64 fprintf(stderr, "%s\n", sqlite3_errmsg(db))
65 #define SQL_BIND_INT64(_s, _i, _v) \
66 if (SQLITE_OK != sqlite3_bind_int64 \
67 ((_s), (_i)++, (_v))) \
68 fprintf(stderr, "%s\n", sqlite3_errmsg(db))
69 #define SQL_STEP(_s) \
70 if (SQLITE_DONE != sqlite3_step((_s))) \
71 fprintf(stderr, "%s\n", sqlite3_errmsg(db))
74 OP_DEFAULT
= 0, /* new dbs from dir list or default config */
75 OP_CONFFILE
, /* new databases from custom config file */
76 OP_UPDATE
, /* delete/add entries in existing database */
77 OP_DELETE
, /* delete entries from existing database */
78 OP_TEST
/* change no databases, report potential problems */
82 FORM_NONE
, /* format is unknown */
83 FORM_SRC
, /* format is -man or -mdoc */
84 FORM_CAT
/* format is cat */
88 char *rendered
; /* key in UTF-8 or ASCII form */
89 const struct mpage
*mpage
; /* if set, the owning parse */
90 uint64_t mask
; /* bitmask in sequence */
91 char key
[]; /* may contain escape sequences */
100 struct inodev inodev
; /* used for hashing routine */
101 enum form form
; /* format from file content */
102 char *sec
; /* section from file content */
103 char *arch
; /* architecture from file content */
104 char *title
; /* title from file content */
105 char *desc
; /* description from file content */
106 struct mlink
*mlinks
; /* singly linked list */
110 char file
[PATH_MAX
]; /* filename rel. to manpath */
111 enum form dform
; /* format from directory */
112 enum form fform
; /* format from file name suffix */
113 char *dsec
; /* section from directory */
114 char *arch
; /* architecture from directory */
115 char *name
; /* name from file name (not empty) */
116 char *fsec
; /* section from file name suffix */
117 struct mlink
*next
; /* singly linked list */
121 STMT_DELETE_PAGE
= 0, /* delete mpage */
122 STMT_INSERT_PAGE
, /* insert mpage */
123 STMT_INSERT_LINK
, /* insert mlink */
124 STMT_INSERT_KEY
, /* insert parsed key */
128 typedef int (*mdoc_fp
)(struct mpage
*, const struct mdoc_node
*);
130 struct mdoc_handler
{
131 mdoc_fp fp
; /* optional handler */
132 uint64_t mask
; /* set unless handler returns 0 */
135 static void dbclose(int);
136 static void dbadd(const struct mpage
*, struct mchars
*);
137 static int dbopen(int);
138 static void dbprune(void);
139 static void filescan(const char *);
140 static void *hash_alloc(size_t, void *);
141 static void hash_free(void *, size_t, void *);
142 static void *hash_halloc(size_t, void *);
143 static void mlink_add(struct mlink
*, const struct stat
*);
144 static int mlink_check(struct mpage
*, struct mlink
*);
145 static void mlink_free(struct mlink
*);
146 static void mlinks_undupe(struct mpage
*);
147 static void mpages_free(void);
148 static void mpages_merge(struct mchars
*, struct mparse
*);
149 static void parse_cat(struct mpage
*);
150 static void parse_man(struct mpage
*, const struct man_node
*);
151 static void parse_mdoc(struct mpage
*, const struct mdoc_node
*);
152 static int parse_mdoc_body(struct mpage
*, const struct mdoc_node
*);
153 static int parse_mdoc_head(struct mpage
*, const struct mdoc_node
*);
154 static int parse_mdoc_Fd(struct mpage
*, const struct mdoc_node
*);
155 static int parse_mdoc_Fn(struct mpage
*, const struct mdoc_node
*);
156 static int parse_mdoc_Nd(struct mpage
*, const struct mdoc_node
*);
157 static int parse_mdoc_Nm(struct mpage
*, const struct mdoc_node
*);
158 static int parse_mdoc_Sh(struct mpage
*, const struct mdoc_node
*);
159 static int parse_mdoc_Xr(struct mpage
*, const struct mdoc_node
*);
160 static void putkey(const struct mpage
*, char *, uint64_t);
161 static void putkeys(const struct mpage
*,
162 const char *, size_t, uint64_t);
163 static void putmdockey(const struct mpage
*,
164 const struct mdoc_node
*, uint64_t);
165 static void render_key(struct mchars
*, struct str
*);
166 static void say(const char *, const char *, ...);
167 static int set_basedir(const char *);
168 static int treescan(void);
169 static size_t utf8(unsigned int, char [7]);
171 static char *progname
;
172 static int nodb
; /* no database changes */
173 static int quick
; /* abort the parse early */
174 static int use_all
; /* use all found files */
175 static int verb
; /* print what we're doing */
176 static int warnings
; /* warn about crap */
177 static int write_utf8
; /* write UTF-8 output; else ASCII */
178 static int exitcode
; /* to be returned by main */
179 static enum op op
; /* operational mode */
180 static char basedir
[PATH_MAX
]; /* current base directory */
181 static struct ohash mpages
; /* table of distinct manual pages */
182 static struct ohash mlinks
; /* table of directory entries */
183 static struct ohash strings
; /* table of all strings */
184 static sqlite3
*db
= NULL
; /* current database */
185 static sqlite3_stmt
*stmts
[STMT__MAX
]; /* current statements */
187 static const struct mdoc_handler mdocs
[MDOC_MAX
] = {
188 { NULL
, 0 }, /* Ap */
189 { NULL
, 0 }, /* Dd */
190 { NULL
, 0 }, /* Dt */
191 { NULL
, 0 }, /* Os */
192 { parse_mdoc_Sh
, TYPE_Sh
}, /* Sh */
193 { parse_mdoc_head
, TYPE_Ss
}, /* Ss */
194 { NULL
, 0 }, /* Pp */
195 { NULL
, 0 }, /* D1 */
196 { NULL
, 0 }, /* Dl */
197 { NULL
, 0 }, /* Bd */
198 { NULL
, 0 }, /* Ed */
199 { NULL
, 0 }, /* Bl */
200 { NULL
, 0 }, /* El */
201 { NULL
, 0 }, /* It */
202 { NULL
, 0 }, /* Ad */
203 { NULL
, TYPE_An
}, /* An */
204 { NULL
, TYPE_Ar
}, /* Ar */
205 { NULL
, TYPE_Cd
}, /* Cd */
206 { NULL
, TYPE_Cm
}, /* Cm */
207 { NULL
, TYPE_Dv
}, /* Dv */
208 { NULL
, TYPE_Er
}, /* Er */
209 { NULL
, TYPE_Ev
}, /* Ev */
210 { NULL
, 0 }, /* Ex */
211 { NULL
, TYPE_Fa
}, /* Fa */
212 { parse_mdoc_Fd
, 0 }, /* Fd */
213 { NULL
, TYPE_Fl
}, /* Fl */
214 { parse_mdoc_Fn
, 0 }, /* Fn */
215 { NULL
, TYPE_Ft
}, /* Ft */
216 { NULL
, TYPE_Ic
}, /* Ic */
217 { NULL
, TYPE_In
}, /* In */
218 { NULL
, TYPE_Li
}, /* Li */
219 { parse_mdoc_Nd
, TYPE_Nd
}, /* Nd */
220 { parse_mdoc_Nm
, TYPE_Nm
}, /* Nm */
221 { NULL
, 0 }, /* Op */
222 { NULL
, 0 }, /* Ot */
223 { NULL
, TYPE_Pa
}, /* Pa */
224 { NULL
, 0 }, /* Rv */
225 { NULL
, TYPE_St
}, /* St */
226 { NULL
, TYPE_Va
}, /* Va */
227 { parse_mdoc_body
, TYPE_Va
}, /* Vt */
228 { parse_mdoc_Xr
, 0 }, /* Xr */
229 { NULL
, 0 }, /* %A */
230 { NULL
, 0 }, /* %B */
231 { NULL
, 0 }, /* %D */
232 { NULL
, 0 }, /* %I */
233 { NULL
, 0 }, /* %J */
234 { NULL
, 0 }, /* %N */
235 { NULL
, 0 }, /* %O */
236 { NULL
, 0 }, /* %P */
237 { NULL
, 0 }, /* %R */
238 { NULL
, 0 }, /* %T */
239 { NULL
, 0 }, /* %V */
240 { NULL
, 0 }, /* Ac */
241 { NULL
, 0 }, /* Ao */
242 { NULL
, 0 }, /* Aq */
243 { NULL
, TYPE_At
}, /* At */
244 { NULL
, 0 }, /* Bc */
245 { NULL
, 0 }, /* Bf */
246 { NULL
, 0 }, /* Bo */
247 { NULL
, 0 }, /* Bq */
248 { NULL
, TYPE_Bsx
}, /* Bsx */
249 { NULL
, TYPE_Bx
}, /* Bx */
250 { NULL
, 0 }, /* Db */
251 { NULL
, 0 }, /* Dc */
252 { NULL
, 0 }, /* Do */
253 { NULL
, 0 }, /* Dq */
254 { NULL
, 0 }, /* Ec */
255 { NULL
, 0 }, /* Ef */
256 { NULL
, TYPE_Em
}, /* Em */
257 { NULL
, 0 }, /* Eo */
258 { NULL
, TYPE_Fx
}, /* Fx */
259 { NULL
, TYPE_Ms
}, /* Ms */
260 { NULL
, 0 }, /* No */
261 { NULL
, 0 }, /* Ns */
262 { NULL
, TYPE_Nx
}, /* Nx */
263 { NULL
, TYPE_Ox
}, /* Ox */
264 { NULL
, 0 }, /* Pc */
265 { NULL
, 0 }, /* Pf */
266 { NULL
, 0 }, /* Po */
267 { NULL
, 0 }, /* Pq */
268 { NULL
, 0 }, /* Qc */
269 { NULL
, 0 }, /* Ql */
270 { NULL
, 0 }, /* Qo */
271 { NULL
, 0 }, /* Qq */
272 { NULL
, 0 }, /* Re */
273 { NULL
, 0 }, /* Rs */
274 { NULL
, 0 }, /* Sc */
275 { NULL
, 0 }, /* So */
276 { NULL
, 0 }, /* Sq */
277 { NULL
, 0 }, /* Sm */
278 { NULL
, 0 }, /* Sx */
279 { NULL
, TYPE_Sy
}, /* Sy */
280 { NULL
, TYPE_Tn
}, /* Tn */
281 { NULL
, 0 }, /* Ux */
282 { NULL
, 0 }, /* Xc */
283 { NULL
, 0 }, /* Xo */
284 { parse_mdoc_head
, 0 }, /* Fo */
285 { NULL
, 0 }, /* Fc */
286 { NULL
, 0 }, /* Oo */
287 { NULL
, 0 }, /* Oc */
288 { NULL
, 0 }, /* Bk */
289 { NULL
, 0 }, /* Ek */
290 { NULL
, 0 }, /* Bt */
291 { NULL
, 0 }, /* Hf */
292 { NULL
, 0 }, /* Fr */
293 { NULL
, 0 }, /* Ud */
294 { NULL
, TYPE_Lb
}, /* Lb */
295 { NULL
, 0 }, /* Lp */
296 { NULL
, TYPE_Lk
}, /* Lk */
297 { NULL
, TYPE_Mt
}, /* Mt */
298 { NULL
, 0 }, /* Brq */
299 { NULL
, 0 }, /* Bro */
300 { NULL
, 0 }, /* Brc */
301 { NULL
, 0 }, /* %C */
302 { NULL
, 0 }, /* Es */
303 { NULL
, 0 }, /* En */
304 { NULL
, TYPE_Dx
}, /* Dx */
305 { NULL
, 0 }, /* %Q */
306 { NULL
, 0 }, /* br */
307 { NULL
, 0 }, /* sp */
308 { NULL
, 0 }, /* %U */
309 { NULL
, 0 }, /* Ta */
313 main(int argc
, char *argv
[])
317 const char *path_arg
;
319 struct manpaths dirs
;
321 struct ohash_info mpages_info
, mlinks_info
;
323 memset(stmts
, 0, STMT__MAX
* sizeof(sqlite3_stmt
*));
324 memset(&dirs
, 0, sizeof(struct manpaths
));
326 mpages_info
.alloc
= mlinks_info
.alloc
= hash_alloc
;
327 mpages_info
.halloc
= mlinks_info
.halloc
= hash_halloc
;
328 mpages_info
.hfree
= mlinks_info
.hfree
= hash_free
;
330 mpages_info
.key_offset
= offsetof(struct mpage
, inodev
);
331 mlinks_info
.key_offset
= offsetof(struct mlink
, file
);
333 progname
= strrchr(argv
[0], '/');
334 if (progname
== NULL
)
340 * We accept a few different invocations.
341 * The CHECKOP macro makes sure that invocation styles don't
342 * clobber each other.
344 #define CHECKOP(_op, _ch) do \
345 if (OP_DEFAULT != (_op)) { \
346 fprintf(stderr, "-%c: Conflicting option\n", (_ch)); \
348 } while (/*CONSTCOND*/0)
353 while (-1 != (ch
= getopt(argc
, argv
, "aC:d:nQT:tu:vW")))
375 if (strcmp(optarg
, "utf8")) {
376 fprintf(stderr
, "-T%s: Unsupported "
377 "output format\n", optarg
);
384 dup2(STDOUT_FILENO
, STDERR_FILENO
);
406 if (OP_CONFFILE
== op
&& argc
> 0) {
407 fprintf(stderr
, "-C: Too many arguments\n");
411 exitcode
= (int)MANDOCLEVEL_OK
;
412 mp
= mparse_alloc(MPARSE_AUTO
,
413 MANDOCLEVEL_FATAL
, NULL
, NULL
, quick
);
416 ohash_init(&mpages
, 6, &mpages_info
);
417 ohash_init(&mlinks
, 6, &mlinks_info
);
419 if (OP_UPDATE
== op
|| OP_DELETE
== op
|| OP_TEST
== op
) {
421 * Force processing all files.
426 * All of these deal with a specific directory.
427 * Jump into that directory then collect files specified
428 * on the command-line.
430 if (0 == set_basedir(path_arg
))
432 for (i
= 0; i
< argc
; i
++)
439 mpages_merge(mc
, mp
);
443 * If we have arguments, use them as our manpaths.
444 * If we don't, grok from manpath(1) or however else
445 * manpath_parse() wants to do it.
448 dirs
.paths
= mandoc_calloc
449 (argc
, sizeof(char *));
450 dirs
.sz
= (size_t)argc
;
451 for (i
= 0; i
< argc
; i
++)
452 dirs
.paths
[i
] = mandoc_strdup(argv
[i
]);
454 manpath_parse(&dirs
, path_arg
, NULL
, NULL
);
457 * First scan the tree rooted at a base directory, then
458 * build a new database and finally move it into place.
459 * Ignore zero-length directories and strip trailing
462 for (j
= 0; j
< dirs
.sz
; j
++) {
463 sz
= strlen(dirs
.paths
[j
]);
464 if (sz
&& '/' == dirs
.paths
[j
][sz
- 1])
465 dirs
.paths
[j
][--sz
] = '\0';
470 ohash_init(&mpages
, 6, &mpages_info
);
471 ohash_init(&mlinks
, 6, &mlinks_info
);
474 if (0 == set_basedir(dirs
.paths
[j
]))
478 if (0 == set_basedir(dirs
.paths
[j
]))
483 mpages_merge(mc
, mp
);
486 if (j
+ 1 < dirs
.sz
) {
488 ohash_delete(&mpages
);
489 ohash_delete(&mlinks
);
499 ohash_delete(&mpages
);
500 ohash_delete(&mlinks
);
503 fprintf(stderr
, "usage: %s [-anQvW] [-C file] [-Tutf8]\n"
504 " %s [-anQvW] [-Tutf8] dir ...\n"
505 " %s [-nQvW] [-Tutf8] -d dir [file ...]\n"
506 " %s [-nvW] -u dir [file ...]\n"
507 " %s [-Q] -t file ...\n",
508 progname
, progname
, progname
,
511 return((int)MANDOCLEVEL_BADARG
);
515 * Scan a directory tree rooted at "basedir" for manpages.
516 * We use fts(), scanning directory parts along the way for clues to our
517 * section and architecture.
519 * If use_all has been specified, grok all files.
520 * If not, sanitise paths to the following:
522 * [./]man*[/<arch>]/<name>.<section>
524 * [./]cat<section>[/<arch>]/<name>.0
526 * TODO: accomodate for multi-language directories.
535 char *dsec
, *arch
, *fsec
, *cp
;
540 argv
[1] = (char *)NULL
;
543 * Walk through all components under the directory, using the
544 * logical descent of files.
546 f
= fts_open((char * const *)argv
, FTS_LOGICAL
, NULL
);
548 exitcode
= (int)MANDOCLEVEL_SYSERR
;
556 while (NULL
!= (ff
= fts_read(f
))) {
557 path
= ff
->fts_path
+ 2;
559 * If we're a regular file, add an mlink by using the
560 * stored directory data and handling the filename.
562 if (FTS_F
== ff
->fts_info
) {
563 if (0 == strcmp(path
, MANDOC_DB
))
565 if ( ! use_all
&& ff
->fts_level
< 2) {
567 say(path
, "Extraneous file");
569 } else if (NULL
== (fsec
=
570 strrchr(ff
->fts_name
, '.'))) {
574 "No filename suffix");
577 } else if (0 == strcmp(++fsec
, "html")) {
579 say(path
, "Skip html");
581 } else if (0 == strcmp(fsec
, "gz")) {
583 say(path
, "Skip gz");
585 } else if (0 == strcmp(fsec
, "ps")) {
587 say(path
, "Skip ps");
589 } else if (0 == strcmp(fsec
, "pdf")) {
591 say(path
, "Skip pdf");
593 } else if ( ! use_all
&&
594 ((FORM_SRC
== dform
&& strcmp(fsec
, dsec
)) ||
595 (FORM_CAT
== dform
&& strcmp(fsec
, "0")))) {
597 say(path
, "Wrong filename suffix");
602 mlink
= mandoc_calloc(1, sizeof(struct mlink
));
603 strlcpy(mlink
->file
, path
, sizeof(mlink
->file
));
604 mlink
->dform
= dform
;
607 mlink
->name
= ff
->fts_name
;
609 mlink_add(mlink
, ff
->fts_statp
);
611 } else if (FTS_D
!= ff
->fts_info
&&
612 FTS_DP
!= ff
->fts_info
) {
614 say(path
, "Not a regular file");
618 switch (ff
->fts_level
) {
620 /* Ignore the root directory. */
624 * This might contain manX/ or catX/.
625 * Try to infer this from the name.
626 * If we're not in use_all, enforce it.
629 if (FTS_DP
== ff
->fts_info
)
632 if (0 == strncmp(cp
, "man", 3)) {
635 } else if (0 == strncmp(cp
, "cat", 3)) {
643 if (NULL
!= dsec
|| use_all
)
647 say(path
, "Unknown directory part");
648 fts_set(f
, ff
, FTS_SKIP
);
652 * Possibly our architecture.
653 * If we're descending, keep tabs on it.
655 if (FTS_DP
!= ff
->fts_info
&& NULL
!= dsec
)
661 if (FTS_DP
== ff
->fts_info
|| use_all
)
664 say(path
, "Extraneous directory part");
665 fts_set(f
, ff
, FTS_SKIP
);
675 * Add a file to the mlinks table.
676 * Do not verify that it's a "valid" looking manpage (we'll do that
679 * Try to infer the manual section, architecture, and page name from the
680 * path, assuming it looks like
682 * [./]man*[/<arch>]/<name>.<section>
684 * [./]cat<section>[/<arch>]/<name>.0
686 * See treescan() for the fts(3) version of this.
689 filescan(const char *file
)
698 if (0 == strncmp(file
, "./", 2))
701 if (NULL
== realpath(file
, buf
)) {
702 exitcode
= (int)MANDOCLEVEL_BADARG
;
707 if (strstr(buf
, basedir
) == buf
)
708 start
= buf
+ strlen(basedir
) + 1;
709 else if (OP_TEST
== op
)
712 exitcode
= (int)MANDOCLEVEL_BADARG
;
713 say("", "%s: outside base directory", buf
);
717 if (-1 == stat(buf
, &st
)) {
718 exitcode
= (int)MANDOCLEVEL_BADARG
;
721 } else if ( ! (S_IFREG
& st
.st_mode
)) {
722 exitcode
= (int)MANDOCLEVEL_BADARG
;
723 say(file
, "Not a regular file");
727 mlink
= mandoc_calloc(1, sizeof(struct mlink
));
728 strlcpy(mlink
->file
, start
, sizeof(mlink
->file
));
731 * First try to guess our directory structure.
732 * If we find a separator, try to look for man* or cat*.
733 * If we find one of these and what's underneath is a directory,
734 * assume it's an architecture.
736 if (NULL
!= (p
= strchr(start
, '/'))) {
738 if (0 == strncmp(start
, "man", 3)) {
739 mlink
->dform
= FORM_SRC
;
740 mlink
->dsec
= start
+ 3;
741 } else if (0 == strncmp(start
, "cat", 3)) {
742 mlink
->dform
= FORM_CAT
;
743 mlink
->dsec
= start
+ 3;
747 if (NULL
!= mlink
->dsec
&& NULL
!= (p
= strchr(start
, '/'))) {
755 * Now check the file suffix.
756 * Suffix of `.0' indicates a catpage, `.1-9' is a manpage.
758 p
= strrchr(start
, '\0');
759 while (p
-- > start
&& '/' != *p
&& '.' != *p
)
768 * Now try to parse the name.
769 * Use the filename portion of the path.
772 if (NULL
!= (p
= strrchr(start
, '/'))) {
776 mlink_add(mlink
, &st
);
780 mlink_add(struct mlink
*mlink
, const struct stat
*st
)
782 struct inodev inodev
;
786 assert(NULL
!= mlink
->file
);
788 mlink
->dsec
= mandoc_strdup(mlink
->dsec
? mlink
->dsec
: "");
789 mlink
->arch
= mandoc_strdup(mlink
->arch
? mlink
->arch
: "");
790 mlink
->name
= mandoc_strdup(mlink
->name
? mlink
->name
: "");
791 mlink
->fsec
= mandoc_strdup(mlink
->fsec
? mlink
->fsec
: "");
793 if ('0' == *mlink
->fsec
) {
795 mlink
->fsec
= mandoc_strdup(mlink
->dsec
);
796 mlink
->fform
= FORM_CAT
;
797 } else if ('1' <= *mlink
->fsec
&& '9' >= *mlink
->fsec
)
798 mlink
->fform
= FORM_SRC
;
800 mlink
->fform
= FORM_NONE
;
802 slot
= ohash_qlookup(&mlinks
, mlink
->file
);
803 assert(NULL
== ohash_find(&mlinks
, slot
));
804 ohash_insert(&mlinks
, slot
, mlink
);
806 inodev
.st_ino
= st
->st_ino
;
807 inodev
.st_dev
= st
->st_dev
;
808 slot
= ohash_lookup_memory(&mpages
, (char *)&inodev
,
809 sizeof(struct inodev
), inodev
.st_ino
);
810 mpage
= ohash_find(&mpages
, slot
);
812 mpage
= mandoc_calloc(1, sizeof(struct mpage
));
813 mpage
->inodev
.st_ino
= inodev
.st_ino
;
814 mpage
->inodev
.st_dev
= inodev
.st_dev
;
815 ohash_insert(&mpages
, slot
, mpage
);
817 mlink
->next
= mpage
->mlinks
;
818 mpage
->mlinks
= mlink
;
822 mlink_free(struct mlink
*mlink
)
839 mpage
= ohash_first(&mpages
, &slot
);
840 while (NULL
!= mpage
) {
841 while (NULL
!= (mlink
= mpage
->mlinks
)) {
842 mpage
->mlinks
= mlink
->next
;
850 mpage
= ohash_next(&mpages
, &slot
);
855 * For each mlink to the mpage, check whether the path looks like
856 * it is formatted, and if it does, check whether a source manual
857 * exists by the same name, ignoring the suffix.
858 * If both conditions hold, drop the mlink.
861 mlinks_undupe(struct mpage
*mpage
)
868 mpage
->form
= FORM_CAT
;
869 prev
= &mpage
->mlinks
;
870 while (NULL
!= (mlink
= *prev
)) {
871 if (FORM_CAT
!= mlink
->dform
) {
872 mpage
->form
= FORM_NONE
;
875 if (strlcpy(buf
, mlink
->file
, PATH_MAX
) >= PATH_MAX
) {
877 say(mlink
->file
, "Filename too long");
880 bufp
= strstr(buf
, "cat");
881 assert(NULL
!= bufp
);
882 memcpy(bufp
, "man", 3);
883 if (NULL
!= (bufp
= strrchr(buf
, '.')))
885 strlcat(buf
, mlink
->dsec
, PATH_MAX
);
886 if (NULL
== ohash_find(&mlinks
,
887 ohash_qlookup(&mlinks
, buf
)))
890 say(mlink
->file
, "Man source exists: %s", buf
);
897 prev
= &(*prev
)->next
;
902 mlink_check(struct mpage
*mpage
, struct mlink
*mlink
)
909 * Check whether the manual section given in a file
910 * agrees with the directory where the file is located.
911 * Some manuals have suffixes like (3p) on their
912 * section number either inside the file or in the
913 * directory name, some are linked into more than one
914 * section, like encrypt(1) = makekey(8).
917 if (FORM_SRC
== mpage
->form
&&
918 strcasecmp(mpage
->sec
, mlink
->dsec
)) {
920 say(mlink
->file
, "Section \"%s\" manual in %s directory",
921 mpage
->sec
, mlink
->dsec
);
925 * Manual page directories exist for each kernel
926 * architecture as returned by machine(1).
927 * However, many manuals only depend on the
928 * application architecture as returned by arch(1).
929 * For example, some (2/ARM) manuals are shared
930 * across the "armish" and "zaurus" kernel
932 * A few manuals are even shared across completely
933 * different architectures, for example fdformat(1)
934 * on amd64, i386, sparc, and sparc64.
937 if (strcasecmp(mpage
->arch
, mlink
->arch
)) {
939 say(mlink
->file
, "Architecture \"%s\" manual in "
940 "\"%s\" directory", mpage
->arch
, mlink
->arch
);
943 if (strcasecmp(mpage
->title
, mlink
->name
))
950 * Run through the files in the global vector "mpages"
951 * and add them to the database specified in "basedir".
953 * This handles the parsing scheme itself, using the cues of directory
954 * and filename to determine whether the file is parsable or not.
957 mpages_merge(struct mchars
*mc
, struct mparse
*mp
)
960 struct ohash_info str_info
;
968 enum mandoclevel lvl
;
970 str_info
.alloc
= hash_alloc
;
971 str_info
.halloc
= hash_halloc
;
972 str_info
.hfree
= hash_free
;
973 str_info
.key_offset
= offsetof(struct str
, key
);
976 SQL_EXEC("BEGIN TRANSACTION");
978 mpage
= ohash_first(&mpages
, &pslot
);
979 while (NULL
!= mpage
) {
980 mlinks_undupe(mpage
);
981 if (NULL
== mpage
->mlinks
) {
982 mpage
= ohash_next(&mpages
, &pslot
);
986 ohash_init(&strings
, 6, &str_info
);
992 * Try interpreting the file as mdoc(7) or man(7)
993 * source code, unless it is already known to be
994 * formatted. Fall back to formatted mode.
996 if (FORM_CAT
!= mpage
->mlinks
->dform
||
997 FORM_CAT
!= mpage
->mlinks
->fform
) {
998 lvl
= mparse_readfd(mp
, -1, mpage
->mlinks
->file
);
999 if (lvl
< MANDOCLEVEL_FATAL
)
1000 mparse_result(mp
, &mdoc
, &man
);
1004 mpage
->form
= FORM_SRC
;
1006 mandoc_strdup(mdoc_meta(mdoc
)->msec
);
1007 mpage
->arch
= mdoc_meta(mdoc
)->arch
;
1008 mpage
->arch
= mandoc_strdup(
1009 NULL
== mpage
->arch
? "" : mpage
->arch
);
1011 mandoc_strdup(mdoc_meta(mdoc
)->title
);
1012 } else if (NULL
!= man
) {
1013 mpage
->form
= FORM_SRC
;
1015 mandoc_strdup(man_meta(man
)->msec
);
1017 mandoc_strdup(mpage
->mlinks
->arch
);
1019 mandoc_strdup(man_meta(man
)->title
);
1021 mpage
->form
= FORM_CAT
;
1023 mandoc_strdup(mpage
->mlinks
->dsec
);
1025 mandoc_strdup(mpage
->mlinks
->arch
);
1027 mandoc_strdup(mpage
->mlinks
->name
);
1029 putkey(mpage
, mpage
->sec
, TYPE_sec
);
1030 putkey(mpage
, '\0' == *mpage
->arch
?
1031 any
: mpage
->arch
, TYPE_arch
);
1033 for (mlink
= mpage
->mlinks
; mlink
; mlink
= mlink
->next
) {
1034 if ('\0' != *mlink
->dsec
)
1035 putkey(mpage
, mlink
->dsec
, TYPE_sec
);
1036 if ('\0' != *mlink
->fsec
)
1037 putkey(mpage
, mlink
->fsec
, TYPE_sec
);
1038 putkey(mpage
, '\0' == *mlink
->arch
?
1039 any
: mlink
->arch
, TYPE_arch
);
1040 putkey(mpage
, mlink
->name
, TYPE_Nm
);
1043 if (warnings
&& !use_all
) {
1045 for (mlink
= mpage
->mlinks
; mlink
;
1046 mlink
= mlink
->next
)
1047 if (mlink_check(mpage
, mlink
))
1053 if (NULL
!= (cp
= mdoc_meta(mdoc
)->name
))
1054 putkey(mpage
, cp
, TYPE_Nm
);
1055 assert(NULL
== mpage
->desc
);
1056 parse_mdoc(mpage
, mdoc_node(mdoc
));
1057 putkey(mpage
, NULL
!= mpage
->desc
?
1058 mpage
->desc
: mpage
->mlinks
->name
, TYPE_Nd
);
1059 } else if (NULL
!= man
)
1060 parse_man(mpage
, man_node(man
));
1065 ohash_delete(&strings
);
1066 mpage
= ohash_next(&mpages
, &pslot
);
1070 SQL_EXEC("END TRANSACTION");
1074 parse_cat(struct mpage
*mpage
)
1077 char *line
, *p
, *title
;
1078 size_t len
, plen
, titlesz
;
1080 if (NULL
== (stream
= fopen(mpage
->mlinks
->file
, "r"))) {
1082 say(mpage
->mlinks
->file
, NULL
);
1086 /* Skip to first blank line. */
1088 while (NULL
!= (line
= fgetln(stream
, &len
)))
1093 * Assume the first line that is not indented
1094 * is the first section header. Skip to it.
1097 while (NULL
!= (line
= fgetln(stream
, &len
)))
1098 if ('\n' != *line
&& ' ' != *line
)
1102 * Read up until the next section into a buffer.
1103 * Strip the leading and trailing newline from each read line,
1104 * appending a trailing space.
1105 * Ignore empty (whitespace-only) lines.
1111 while (NULL
!= (line
= fgetln(stream
, &len
))) {
1112 if (' ' != *line
|| '\n' != line
[len
- 1])
1114 while (len
> 0 && isspace((unsigned char)*line
)) {
1120 title
= mandoc_realloc(title
, titlesz
+ len
);
1121 memcpy(title
+ titlesz
, line
, len
);
1123 title
[titlesz
- 1] = ' ';
1127 * If no page content can be found, or the input line
1128 * is already the next section header, or there is no
1129 * trailing newline, reuse the page title as the page
1133 if (NULL
== title
|| '\0' == *title
) {
1135 say(mpage
->mlinks
->file
,
1136 "Cannot find NAME section");
1137 assert(NULL
== mpage
->desc
);
1138 mpage
->desc
= mandoc_strdup(mpage
->mlinks
->name
);
1139 putkey(mpage
, mpage
->mlinks
->name
, TYPE_Nd
);
1145 title
= mandoc_realloc(title
, titlesz
+ 1);
1146 title
[titlesz
] = '\0';
1149 * Skip to the first dash.
1150 * Use the remaining line as the description (no more than 70
1154 if (NULL
!= (p
= strstr(title
, "- "))) {
1155 for (p
+= 2; ' ' == *p
|| '\b' == *p
; p
++)
1156 /* Skip to next word. */ ;
1159 say(mpage
->mlinks
->file
,
1160 "No dash in title line");
1166 /* Strip backspace-encoding from line. */
1168 while (NULL
!= (line
= memchr(p
, '\b', plen
))) {
1171 memmove(line
, line
+ 1, plen
--);
1174 memmove(line
- 1, line
+ 1, plen
- len
);
1178 assert(NULL
== mpage
->desc
);
1179 mpage
->desc
= mandoc_strdup(p
);
1180 putkey(mpage
, mpage
->desc
, TYPE_Nd
);
1186 * Put a type/word pair into the word database for this particular file.
1189 putkey(const struct mpage
*mpage
, char *value
, uint64_t type
)
1193 assert(NULL
!= value
);
1194 if (TYPE_arch
== type
)
1195 for (cp
= value
; *cp
; cp
++)
1196 if (isupper((unsigned char)*cp
))
1197 *cp
= _tolower((unsigned char)*cp
);
1198 putkeys(mpage
, value
, strlen(value
), type
);
1202 * Grok all nodes at or below a certain mdoc node into putkey().
1205 putmdockey(const struct mpage
*mpage
,
1206 const struct mdoc_node
*n
, uint64_t m
)
1209 for ( ; NULL
!= n
; n
= n
->next
) {
1210 if (NULL
!= n
->child
)
1211 putmdockey(mpage
, n
->child
, m
);
1212 if (MDOC_TEXT
== n
->type
)
1213 putkey(mpage
, n
->string
, m
);
1218 parse_man(struct mpage
*mpage
, const struct man_node
*n
)
1220 const struct man_node
*head
, *body
;
1221 char *start
, *sv
, *title
;
1229 * We're only searching for one thing: the first text child in
1230 * the BODY of a NAME section. Since we don't keep track of
1231 * sections in -man, run some hoops to find out whether we're in
1232 * the correct section or not.
1235 if (MAN_BODY
== n
->type
&& MAN_SH
== n
->tok
) {
1237 assert(body
->parent
);
1238 if (NULL
!= (head
= body
->parent
->head
) &&
1239 1 == head
->nchild
&&
1240 NULL
!= (head
= (head
->child
)) &&
1241 MAN_TEXT
== head
->type
&&
1242 0 == strcmp(head
->string
, "NAME") &&
1243 NULL
!= (body
= body
->child
) &&
1244 MAN_TEXT
== body
->type
) {
1250 * Suck the entire NAME section into memory.
1251 * Yes, we might run away.
1252 * But too many manuals have big, spread-out
1253 * NAME sections over many lines.
1256 for ( ; NULL
!= body
; body
= body
->next
) {
1257 if (MAN_TEXT
!= body
->type
)
1259 if (0 == (sz
= strlen(body
->string
)))
1261 title
= mandoc_realloc
1262 (title
, titlesz
+ sz
+ 1);
1263 memcpy(title
+ titlesz
, body
->string
, sz
);
1265 title
[titlesz
- 1] = ' ';
1270 title
= mandoc_realloc(title
, titlesz
+ 1);
1271 title
[titlesz
] = '\0';
1273 /* Skip leading space. */
1276 while (isspace((unsigned char)*sv
))
1279 if (0 == (sz
= strlen(sv
))) {
1284 /* Erase trailing space. */
1286 start
= &sv
[sz
- 1];
1287 while (start
> sv
&& isspace((unsigned char)*start
))
1298 * Go through a special heuristic dance here.
1299 * Conventionally, one or more manual names are
1300 * comma-specified prior to a whitespace, then a
1301 * dash, then a description. Try to puzzle out
1302 * the name parts here.
1306 sz
= strcspn(start
, " ,");
1307 if ('\0' == start
[sz
])
1314 * Assume a stray trailing comma in the
1315 * name list if a name begins with a dash.
1318 if ('-' == start
[0] ||
1319 ('\\' == start
[0] && '-' == start
[1]))
1322 putkey(mpage
, start
, TYPE_Nm
);
1329 assert(',' == byte
);
1331 while (' ' == *start
)
1336 putkey(mpage
, start
, TYPE_Nm
);
1341 while (isspace((unsigned char)*start
))
1344 if (0 == strncmp(start
, "-", 1))
1346 else if (0 == strncmp(start
, "\\-\\-", 4))
1348 else if (0 == strncmp(start
, "\\-", 2))
1350 else if (0 == strncmp(start
, "\\(en", 4))
1352 else if (0 == strncmp(start
, "\\(em", 4))
1355 while (' ' == *start
)
1358 assert(NULL
== mpage
->desc
);
1359 mpage
->desc
= mandoc_strdup(start
);
1360 putkey(mpage
, mpage
->desc
, TYPE_Nd
);
1366 for (n
= n
->child
; n
; n
= n
->next
) {
1367 if (NULL
!= mpage
->desc
)
1369 parse_man(mpage
, n
);
1374 parse_mdoc(struct mpage
*mpage
, const struct mdoc_node
*n
)
1378 for (n
= n
->child
; NULL
!= n
; n
= n
->next
) {
1389 if (NULL
!= mdocs
[n
->tok
].fp
)
1390 if (0 == (*mdocs
[n
->tok
].fp
)(mpage
, n
))
1392 if (mdocs
[n
->tok
].mask
)
1393 putmdockey(mpage
, n
->child
,
1394 mdocs
[n
->tok
].mask
);
1397 assert(MDOC_ROOT
!= n
->type
);
1400 if (NULL
!= n
->child
)
1401 parse_mdoc(mpage
, n
);
1406 parse_mdoc_Fd(struct mpage
*mpage
, const struct mdoc_node
*n
)
1408 const char *start
, *end
;
1411 if (SEC_SYNOPSIS
!= n
->sec
||
1412 NULL
== (n
= n
->child
) ||
1413 MDOC_TEXT
!= n
->type
)
1417 * Only consider those `Fd' macro fields that begin with an
1418 * "inclusion" token (versus, e.g., #define).
1421 if (strcmp("#include", n
->string
))
1424 if (NULL
== (n
= n
->next
) || MDOC_TEXT
!= n
->type
)
1428 * Strip away the enclosing angle brackets and make sure we're
1433 if ('<' == *start
|| '"' == *start
)
1436 if (0 == (sz
= strlen(start
)))
1439 end
= &start
[(int)sz
- 1];
1440 if ('>' == *end
|| '"' == *end
)
1444 putkeys(mpage
, start
, end
- start
+ 1, TYPE_In
);
1449 parse_mdoc_Fn(struct mpage
*mpage
, const struct mdoc_node
*n
)
1453 if (NULL
== (n
= n
->child
) || MDOC_TEXT
!= n
->type
)
1457 * Parse: .Fn "struct type *name" "char *arg".
1458 * First strip away pointer symbol.
1459 * Then store the function name, then type.
1460 * Finally, store the arguments.
1463 if (NULL
== (cp
= strrchr(n
->string
, ' ')))
1469 putkey(mpage
, cp
, TYPE_Fn
);
1472 putkeys(mpage
, n
->string
, cp
- n
->string
, TYPE_Ft
);
1474 for (n
= n
->next
; NULL
!= n
; n
= n
->next
)
1475 if (MDOC_TEXT
== n
->type
)
1476 putkey(mpage
, n
->string
, TYPE_Fa
);
1482 parse_mdoc_Xr(struct mpage
*mpage
, const struct mdoc_node
*n
)
1486 if (NULL
== (n
= n
->child
))
1489 if (NULL
== n
->next
) {
1490 putkey(mpage
, n
->string
, TYPE_Xr
);
1494 if (-1 == asprintf(&cp
, "%s(%s)", n
->string
, n
->next
->string
)) {
1496 exit((int)MANDOCLEVEL_SYSERR
);
1498 putkey(mpage
, cp
, TYPE_Xr
);
1504 parse_mdoc_Nd(struct mpage
*mpage
, const struct mdoc_node
*n
)
1508 if (MDOC_BODY
!= n
->type
)
1512 * Special-case the `Nd' because we need to put the description
1513 * into the document table.
1516 for (n
= n
->child
; NULL
!= n
; n
= n
->next
) {
1517 if (MDOC_TEXT
== n
->type
) {
1518 if (NULL
!= mpage
->desc
) {
1519 sz
= strlen(mpage
->desc
) +
1520 strlen(n
->string
) + 2;
1521 mpage
->desc
= mandoc_realloc(
1523 strlcat(mpage
->desc
, " ", sz
);
1524 strlcat(mpage
->desc
, n
->string
, sz
);
1526 mpage
->desc
= mandoc_strdup(n
->string
);
1528 if (NULL
!= n
->child
)
1529 parse_mdoc_Nd(mpage
, n
);
1535 parse_mdoc_Nm(struct mpage
*mpage
, const struct mdoc_node
*n
)
1538 return(SEC_NAME
== n
->sec
||
1539 (SEC_SYNOPSIS
== n
->sec
&& MDOC_HEAD
== n
->type
));
1543 parse_mdoc_Sh(struct mpage
*mpage
, const struct mdoc_node
*n
)
1546 return(SEC_CUSTOM
== n
->sec
&& MDOC_HEAD
== n
->type
);
1550 parse_mdoc_head(struct mpage
*mpage
, const struct mdoc_node
*n
)
1553 return(MDOC_HEAD
== n
->type
);
1557 parse_mdoc_body(struct mpage
*mpage
, const struct mdoc_node
*n
)
1560 return(MDOC_BODY
== n
->type
);
1564 * Add a string to the hash table for the current manual.
1565 * Each string has a bitmask telling which macros it belongs to.
1566 * When we finish the manual, we'll dump the table.
1569 putkeys(const struct mpage
*mpage
,
1570 const char *cp
, size_t sz
, uint64_t v
)
1582 for (i
= 0, mask
= 1;
1583 i
< mansearch_keymax
;
1587 say(mpage
->mlinks
->file
, "Adding key %s=%*s",
1588 mansearch_keynames
[i
], sz
, cp
);
1592 slot
= ohash_qlookupi(&strings
, cp
, &end
);
1593 s
= ohash_find(&strings
, slot
);
1595 if (NULL
!= s
&& mpage
== s
->mpage
) {
1598 } else if (NULL
== s
) {
1599 s
= mandoc_calloc(sizeof(struct str
) + sz
+ 1, 1);
1600 memcpy(s
->key
, cp
, sz
);
1601 ohash_insert(&strings
, slot
, s
);
1608 * Take a Unicode codepoint and produce its UTF-8 encoding.
1609 * This isn't the best way to do this, but it works.
1610 * The magic numbers are from the UTF-8 packaging.
1611 * They're not as scary as they seem: read the UTF-8 spec for details.
1614 utf8(unsigned int cp
, char out
[7])
1619 if (cp
<= 0x0000007F) {
1622 } else if (cp
<= 0x000007FF) {
1624 out
[0] = (cp
>> 6 & 31) | 192;
1625 out
[1] = (cp
& 63) | 128;
1626 } else if (cp
<= 0x0000FFFF) {
1628 out
[0] = (cp
>> 12 & 15) | 224;
1629 out
[1] = (cp
>> 6 & 63) | 128;
1630 out
[2] = (cp
& 63) | 128;
1631 } else if (cp
<= 0x001FFFFF) {
1633 out
[0] = (cp
>> 18 & 7) | 240;
1634 out
[1] = (cp
>> 12 & 63) | 128;
1635 out
[2] = (cp
>> 6 & 63) | 128;
1636 out
[3] = (cp
& 63) | 128;
1637 } else if (cp
<= 0x03FFFFFF) {
1639 out
[0] = (cp
>> 24 & 3) | 248;
1640 out
[1] = (cp
>> 18 & 63) | 128;
1641 out
[2] = (cp
>> 12 & 63) | 128;
1642 out
[3] = (cp
>> 6 & 63) | 128;
1643 out
[4] = (cp
& 63) | 128;
1644 } else if (cp
<= 0x7FFFFFFF) {
1646 out
[0] = (cp
>> 30 & 1) | 252;
1647 out
[1] = (cp
>> 24 & 63) | 128;
1648 out
[2] = (cp
>> 18 & 63) | 128;
1649 out
[3] = (cp
>> 12 & 63) | 128;
1650 out
[4] = (cp
>> 6 & 63) | 128;
1651 out
[5] = (cp
& 63) | 128;
1660 * Store the rendered version of a key, or alias the pointer
1661 * if the key contains no escape sequences.
1664 render_key(struct mchars
*mc
, struct str
*key
)
1666 size_t sz
, bsz
, pos
;
1667 char utfbuf
[7], res
[6];
1669 const char *seq
, *cpp
, *val
;
1671 enum mandoc_esc esc
;
1673 assert(NULL
== key
->rendered
);
1677 res
[2] = ASCII_NBRSP
;
1678 res
[3] = ASCII_HYPH
;
1679 res
[4] = ASCII_BREAK
;
1686 * Pre-check: if we have no stop-characters, then set the
1687 * pointer as ourselvse and get out of here.
1689 if (strcspn(val
, res
) == bsz
) {
1690 key
->rendered
= key
->key
;
1694 /* Pre-allocate by the length of the input */
1696 buf
= mandoc_malloc(++bsz
);
1699 while ('\0' != *val
) {
1701 * Halt on the first escape sequence.
1702 * This also halts on the end of string, in which case
1703 * we just copy, fallthrough, and exit the loop.
1705 if ((sz
= strcspn(val
, res
)) > 0) {
1706 memcpy(&buf
[pos
], val
, sz
);
1730 /* Read past the slash. */
1735 * Parse the escape sequence and see if it's a
1736 * predefined character or special character.
1740 ((const char **)&val
, &seq
, &len
);
1741 if (ESCAPE_ERROR
== esc
)
1743 if (ESCAPE_SPECIAL
!= esc
)
1747 * Render the special character
1748 * as either UTF-8 or ASCII.
1752 if (0 == (u
= mchars_spec2cp(mc
, seq
, len
)))
1755 if (0 == (sz
= utf8(u
, utfbuf
)))
1759 cpp
= mchars_spec2str(mc
, seq
, len
, &sz
);
1762 if (ASCII_NBRSP
== *cpp
) {
1768 /* Copy the rendered glyph into the stream. */
1771 buf
= mandoc_realloc(buf
, bsz
);
1772 memcpy(&buf
[pos
], cpp
, sz
);
1777 key
->rendered
= buf
;
1781 * Flush the current page's terms (and their bits) into the database.
1782 * Wrap the entire set of additions in a transaction to make sqlite be a
1784 * Also, handle escape sequences at the last possible moment.
1787 dbadd(const struct mpage
*mpage
, struct mchars
*mc
)
1789 struct mlink
*mlink
;
1796 say(mpage
->mlinks
->file
, "Adding to database");
1802 SQL_BIND_INT(stmts
[STMT_INSERT_PAGE
], i
, FORM_SRC
== mpage
->form
);
1803 SQL_STEP(stmts
[STMT_INSERT_PAGE
]);
1804 recno
= sqlite3_last_insert_rowid(db
);
1805 sqlite3_reset(stmts
[STMT_INSERT_PAGE
]);
1807 for (mlink
= mpage
->mlinks
; mlink
; mlink
= mlink
->next
) {
1809 SQL_BIND_TEXT(stmts
[STMT_INSERT_LINK
], i
, mlink
->dsec
);
1810 SQL_BIND_TEXT(stmts
[STMT_INSERT_LINK
], i
, mlink
->arch
);
1811 SQL_BIND_TEXT(stmts
[STMT_INSERT_LINK
], i
, mlink
->name
);
1812 SQL_BIND_INT64(stmts
[STMT_INSERT_LINK
], i
, recno
);
1813 SQL_STEP(stmts
[STMT_INSERT_LINK
]);
1814 sqlite3_reset(stmts
[STMT_INSERT_LINK
]);
1817 for (key
= ohash_first(&strings
, &slot
); NULL
!= key
;
1818 key
= ohash_next(&strings
, &slot
)) {
1819 assert(key
->mpage
== mpage
);
1820 if (NULL
== key
->rendered
)
1821 render_key(mc
, key
);
1823 SQL_BIND_INT64(stmts
[STMT_INSERT_KEY
], i
, key
->mask
);
1824 SQL_BIND_TEXT(stmts
[STMT_INSERT_KEY
], i
, key
->rendered
);
1825 SQL_BIND_INT64(stmts
[STMT_INSERT_KEY
], i
, recno
);
1826 SQL_STEP(stmts
[STMT_INSERT_KEY
]);
1827 sqlite3_reset(stmts
[STMT_INSERT_KEY
]);
1828 if (key
->rendered
!= key
->key
)
1829 free(key
->rendered
);
1837 struct mpage
*mpage
;
1838 struct mlink
*mlink
;
1843 SQL_EXEC("BEGIN TRANSACTION");
1845 for (mpage
= ohash_first(&mpages
, &slot
); NULL
!= mpage
;
1846 mpage
= ohash_next(&mpages
, &slot
)) {
1847 mlink
= mpage
->mlinks
;
1849 say(mlink
->file
, "Deleting from database");
1852 for ( ; NULL
!= mlink
; mlink
= mlink
->next
) {
1854 SQL_BIND_TEXT(stmts
[STMT_DELETE_PAGE
],
1856 SQL_BIND_TEXT(stmts
[STMT_DELETE_PAGE
],
1858 SQL_BIND_TEXT(stmts
[STMT_DELETE_PAGE
],
1860 SQL_STEP(stmts
[STMT_DELETE_PAGE
]);
1861 sqlite3_reset(stmts
[STMT_DELETE_PAGE
]);
1866 SQL_EXEC("END TRANSACTION");
1870 * Close an existing database and its prepared statements.
1871 * If "real" is not set, rename the temporary file into the real one.
1881 for (i
= 0; i
< STMT__MAX
; i
++) {
1882 sqlite3_finalize(stmts
[i
]);
1892 if (-1 == rename(MANDOC_DB
"~", MANDOC_DB
)) {
1893 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1894 say(MANDOC_DB
, NULL
);
1899 * This is straightforward stuff.
1900 * Open a database connection to a "temporary" database, then open a set
1901 * of prepared statements we'll use over and over again.
1902 * If "real" is set, we use the existing database; if not, we truncate a
1904 * Must be matched by dbclose().
1909 const char *file
, *sql
;
1915 ofl
= SQLITE_OPEN_READWRITE
;
1917 file
= MANDOC_DB
"~";
1918 if (-1 == remove(file
) && ENOENT
!= errno
) {
1919 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1923 ofl
|= SQLITE_OPEN_EXCLUSIVE
;
1927 rc
= sqlite3_open_v2(file
, &db
, ofl
, NULL
);
1928 if (SQLITE_OK
== rc
)
1929 goto prepare_statements
;
1930 if (SQLITE_CANTOPEN
!= rc
) {
1931 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1939 if (SQLITE_OK
!= (rc
= sqlite3_open(file
, &db
))) {
1940 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1945 sql
= "CREATE TABLE \"mpages\" (\n"
1946 " \"form\" INTEGER NOT NULL,\n"
1947 " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
1950 "CREATE TABLE \"mlinks\" (\n"
1951 " \"sec\" TEXT NOT NULL,\n"
1952 " \"arch\" TEXT NOT NULL,\n"
1953 " \"name\" TEXT NOT NULL,\n"
1954 " \"pageid\" INTEGER NOT NULL REFERENCES mpages(id) "
1955 "ON DELETE CASCADE\n"
1958 "CREATE TABLE \"keys\" (\n"
1959 " \"bits\" INTEGER NOT NULL,\n"
1960 " \"key\" TEXT NOT NULL,\n"
1961 " \"pageid\" INTEGER NOT NULL REFERENCES mpages(id) "
1962 "ON DELETE CASCADE\n"
1965 if (SQLITE_OK
!= sqlite3_exec(db
, sql
, NULL
, NULL
, NULL
)) {
1966 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1967 say(file
, "%s", sqlite3_errmsg(db
));
1972 SQL_EXEC("PRAGMA foreign_keys = ON");
1973 sql
= "DELETE FROM mpages WHERE id IN "
1974 "(SELECT pageid FROM mlinks WHERE "
1975 "sec=? AND arch=? AND name=?)";
1976 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_DELETE_PAGE
], NULL
);
1977 sql
= "INSERT INTO mpages "
1978 "(form) VALUES (?)";
1979 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_PAGE
], NULL
);
1980 sql
= "INSERT INTO mlinks "
1981 "(sec,arch,name,pageid) VALUES (?,?,?,?)";
1982 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_LINK
], NULL
);
1983 sql
= "INSERT INTO keys "
1984 "(bits,key,pageid) VALUES (?,?,?)";
1985 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_KEY
], NULL
);
1989 * When opening a new database, we can turn off
1990 * synchronous mode for much better performance.
1994 SQL_EXEC("PRAGMA synchronous = OFF");
2001 hash_halloc(size_t sz
, void *arg
)
2004 return(mandoc_calloc(sz
, 1));
2008 hash_alloc(size_t sz
, void *arg
)
2011 return(mandoc_malloc(sz
));
2015 hash_free(void *p
, size_t sz
, void *arg
)
2022 set_basedir(const char *targetdir
)
2024 static char startdir
[PATH_MAX
];
2028 * Remember where we started by keeping a fd open to the origin
2029 * path component: throughout this utility, we chdir() a lot to
2030 * handle relative paths, and by doing this, we can return to
2031 * the starting point.
2033 if ('\0' == *startdir
) {
2034 if (NULL
== getcwd(startdir
, PATH_MAX
)) {
2035 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2036 if (NULL
!= targetdir
)
2040 if (-1 == (fd
= open(startdir
, O_RDONLY
, 0))) {
2041 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2042 say(startdir
, NULL
);
2045 if (NULL
== targetdir
)
2046 targetdir
= startdir
;
2050 if (-1 == fchdir(fd
)) {
2053 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2054 say(startdir
, NULL
);
2057 if (NULL
== targetdir
) {
2062 if (NULL
== realpath(targetdir
, basedir
)) {
2064 exitcode
= (int)MANDOCLEVEL_BADARG
;
2065 say(targetdir
, NULL
);
2067 } else if (-1 == chdir(basedir
)) {
2068 exitcode
= (int)MANDOCLEVEL_BADARG
;
2076 say(const char *file
, const char *format
, ...)
2080 if ('\0' != *basedir
)
2081 fprintf(stderr
, "%s", basedir
);
2082 if ('\0' != *basedir
&& '\0' != *file
)
2083 fputs("//", stderr
);
2085 fprintf(stderr
, "%s", file
);
2086 fputs(": ", stderr
);
2088 if (NULL
== format
) {
2093 va_start(ap
, format
);
2094 vfprintf(stderr
, format
, ap
);
2097 fputc('\n', stderr
);