]>
git.cameronkatri.com Git - mandoc.git/blob - mandocdb.c
1 /* $Id: mandocdb.c,v 1.121 2014/03/23 12:26:58 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.
42 #include "compat_ohash.h"
49 #include "mandoc_aux.h"
51 #include "mansearch.h"
53 extern int mansearch_keymax
;
54 extern const char *const mansearch_keynames
[];
56 #define SQL_EXEC(_v) \
57 if (SQLITE_OK != sqlite3_exec(db, (_v), NULL, NULL, NULL)) \
58 fprintf(stderr, "%s\n", sqlite3_errmsg(db))
59 #define SQL_BIND_TEXT(_s, _i, _v) \
60 if (SQLITE_OK != sqlite3_bind_text \
61 ((_s), (_i)++, (_v), -1, SQLITE_STATIC)) \
62 fprintf(stderr, "%s\n", sqlite3_errmsg(db))
63 #define SQL_BIND_INT(_s, _i, _v) \
64 if (SQLITE_OK != sqlite3_bind_int \
65 ((_s), (_i)++, (_v))) \
66 fprintf(stderr, "%s\n", sqlite3_errmsg(db))
67 #define SQL_BIND_INT64(_s, _i, _v) \
68 if (SQLITE_OK != sqlite3_bind_int64 \
69 ((_s), (_i)++, (_v))) \
70 fprintf(stderr, "%s\n", sqlite3_errmsg(db))
71 #define SQL_STEP(_s) \
72 if (SQLITE_DONE != sqlite3_step((_s))) \
73 fprintf(stderr, "%s\n", sqlite3_errmsg(db))
76 OP_DEFAULT
= 0, /* new dbs from dir list or default config */
77 OP_CONFFILE
, /* new databases from custom config file */
78 OP_UPDATE
, /* delete/add entries in existing database */
79 OP_DELETE
, /* delete entries from existing database */
80 OP_TEST
/* change no databases, report potential problems */
84 FORM_NONE
, /* format is unknown */
85 FORM_SRC
, /* format is -man or -mdoc */
86 FORM_CAT
/* format is cat */
90 char *rendered
; /* key in UTF-8 or ASCII form */
91 const struct mpage
*mpage
; /* if set, the owning parse */
92 uint64_t mask
; /* bitmask in sequence */
93 char key
[]; /* may contain escape sequences */
102 struct inodev inodev
; /* used for hashing routine */
103 int64_t recno
; /* id in mpages SQL table */
104 enum form form
; /* format from file content */
105 char *sec
; /* section from file content */
106 char *arch
; /* architecture from file content */
107 char *title
; /* title from file content */
108 char *desc
; /* description from file content */
109 struct mlink
*mlinks
; /* singly linked list */
113 char file
[PATH_MAX
]; /* filename rel. to manpath */
114 enum form dform
; /* format from directory */
115 enum form fform
; /* format from file name suffix */
116 char *dsec
; /* section from directory */
117 char *arch
; /* architecture from directory */
118 char *name
; /* name from file name (not empty) */
119 char *fsec
; /* section from file name suffix */
120 struct mlink
*next
; /* singly linked list */
121 struct mpage
*mpage
; /* parent */
125 STMT_DELETE_PAGE
= 0, /* delete mpage */
126 STMT_INSERT_PAGE
, /* insert mpage */
127 STMT_INSERT_LINK
, /* insert mlink */
128 STMT_INSERT_KEY
, /* insert parsed key */
132 typedef int (*mdoc_fp
)(struct mpage
*, const struct mdoc_node
*);
134 struct mdoc_handler
{
135 mdoc_fp fp
; /* optional handler */
136 uint64_t mask
; /* set unless handler returns 0 */
139 static void dbclose(int);
140 static void dbadd(struct mpage
*, struct mchars
*);
141 static void dbadd_mlink(const struct mlink
*mlink
);
142 static int dbopen(int);
143 static void dbprune(void);
144 static void filescan(const char *);
145 static void *hash_alloc(size_t, void *);
146 static void hash_free(void *, size_t, void *);
147 static void *hash_halloc(size_t, void *);
148 static void mlink_add(struct mlink
*, const struct stat
*);
149 static int mlink_check(struct mpage
*, struct mlink
*);
150 static void mlink_free(struct mlink
*);
151 static void mlinks_undupe(struct mpage
*);
152 static void mpages_free(void);
153 static void mpages_merge(struct mchars
*, struct mparse
*);
154 static void parse_cat(struct mpage
*);
155 static void parse_man(struct mpage
*, const struct man_node
*);
156 static void parse_mdoc(struct mpage
*, const struct mdoc_node
*);
157 static int parse_mdoc_body(struct mpage
*, const struct mdoc_node
*);
158 static int parse_mdoc_head(struct mpage
*, const struct mdoc_node
*);
159 static int parse_mdoc_Fd(struct mpage
*, const struct mdoc_node
*);
160 static int parse_mdoc_Fn(struct mpage
*, const struct mdoc_node
*);
161 static int parse_mdoc_Nd(struct mpage
*, const struct mdoc_node
*);
162 static int parse_mdoc_Nm(struct mpage
*, const struct mdoc_node
*);
163 static int parse_mdoc_Sh(struct mpage
*, const struct mdoc_node
*);
164 static int parse_mdoc_Xr(struct mpage
*, const struct mdoc_node
*);
165 static void putkey(const struct mpage
*, char *, uint64_t);
166 static void putkeys(const struct mpage
*,
167 const char *, size_t, uint64_t);
168 static void putmdockey(const struct mpage
*,
169 const struct mdoc_node
*, uint64_t);
170 static void render_key(struct mchars
*, struct str
*);
171 static void say(const char *, const char *, ...);
172 static int set_basedir(const char *);
173 static int treescan(void);
174 static size_t utf8(unsigned int, char [7]);
176 static char tempfilename
[32];
177 static char *progname
;
178 static int nodb
; /* no database changes */
179 static int mparse_options
; /* abort the parse early */
180 static int use_all
; /* use all found files */
181 static int verb
; /* print what we're doing */
182 static int warnings
; /* warn about crap */
183 static int write_utf8
; /* write UTF-8 output; else ASCII */
184 static int exitcode
; /* to be returned by main */
185 static enum op op
; /* operational mode */
186 static char basedir
[PATH_MAX
]; /* current base directory */
187 static struct ohash mpages
; /* table of distinct manual pages */
188 static struct ohash mlinks
; /* table of directory entries */
189 static struct ohash strings
; /* table of all strings */
190 static sqlite3
*db
= NULL
; /* current database */
191 static sqlite3_stmt
*stmts
[STMT__MAX
]; /* current statements */
193 static const struct mdoc_handler mdocs
[MDOC_MAX
] = {
194 { NULL
, 0 }, /* Ap */
195 { NULL
, 0 }, /* Dd */
196 { NULL
, 0 }, /* Dt */
197 { NULL
, 0 }, /* Os */
198 { parse_mdoc_Sh
, TYPE_Sh
}, /* Sh */
199 { parse_mdoc_head
, TYPE_Ss
}, /* Ss */
200 { NULL
, 0 }, /* Pp */
201 { NULL
, 0 }, /* D1 */
202 { NULL
, 0 }, /* Dl */
203 { NULL
, 0 }, /* Bd */
204 { NULL
, 0 }, /* Ed */
205 { NULL
, 0 }, /* Bl */
206 { NULL
, 0 }, /* El */
207 { NULL
, 0 }, /* It */
208 { NULL
, 0 }, /* Ad */
209 { NULL
, TYPE_An
}, /* An */
210 { NULL
, TYPE_Ar
}, /* Ar */
211 { NULL
, TYPE_Cd
}, /* Cd */
212 { NULL
, TYPE_Cm
}, /* Cm */
213 { NULL
, TYPE_Dv
}, /* Dv */
214 { NULL
, TYPE_Er
}, /* Er */
215 { NULL
, TYPE_Ev
}, /* Ev */
216 { NULL
, 0 }, /* Ex */
217 { NULL
, TYPE_Fa
}, /* Fa */
218 { parse_mdoc_Fd
, 0 }, /* Fd */
219 { NULL
, TYPE_Fl
}, /* Fl */
220 { parse_mdoc_Fn
, 0 }, /* Fn */
221 { NULL
, TYPE_Ft
}, /* Ft */
222 { NULL
, TYPE_Ic
}, /* Ic */
223 { NULL
, TYPE_In
}, /* In */
224 { NULL
, TYPE_Li
}, /* Li */
225 { parse_mdoc_Nd
, TYPE_Nd
}, /* Nd */
226 { parse_mdoc_Nm
, TYPE_Nm
}, /* Nm */
227 { NULL
, 0 }, /* Op */
228 { NULL
, 0 }, /* Ot */
229 { NULL
, TYPE_Pa
}, /* Pa */
230 { NULL
, 0 }, /* Rv */
231 { NULL
, TYPE_St
}, /* St */
232 { NULL
, TYPE_Va
}, /* Va */
233 { parse_mdoc_body
, TYPE_Va
}, /* Vt */
234 { parse_mdoc_Xr
, 0 }, /* Xr */
235 { NULL
, 0 }, /* %A */
236 { NULL
, 0 }, /* %B */
237 { NULL
, 0 }, /* %D */
238 { NULL
, 0 }, /* %I */
239 { NULL
, 0 }, /* %J */
240 { NULL
, 0 }, /* %N */
241 { NULL
, 0 }, /* %O */
242 { NULL
, 0 }, /* %P */
243 { NULL
, 0 }, /* %R */
244 { NULL
, 0 }, /* %T */
245 { NULL
, 0 }, /* %V */
246 { NULL
, 0 }, /* Ac */
247 { NULL
, 0 }, /* Ao */
248 { NULL
, 0 }, /* Aq */
249 { NULL
, TYPE_At
}, /* At */
250 { NULL
, 0 }, /* Bc */
251 { NULL
, 0 }, /* Bf */
252 { NULL
, 0 }, /* Bo */
253 { NULL
, 0 }, /* Bq */
254 { NULL
, TYPE_Bsx
}, /* Bsx */
255 { NULL
, TYPE_Bx
}, /* Bx */
256 { NULL
, 0 }, /* Db */
257 { NULL
, 0 }, /* Dc */
258 { NULL
, 0 }, /* Do */
259 { NULL
, 0 }, /* Dq */
260 { NULL
, 0 }, /* Ec */
261 { NULL
, 0 }, /* Ef */
262 { NULL
, TYPE_Em
}, /* Em */
263 { NULL
, 0 }, /* Eo */
264 { NULL
, TYPE_Fx
}, /* Fx */
265 { NULL
, TYPE_Ms
}, /* Ms */
266 { NULL
, 0 }, /* No */
267 { NULL
, 0 }, /* Ns */
268 { NULL
, TYPE_Nx
}, /* Nx */
269 { NULL
, TYPE_Ox
}, /* Ox */
270 { NULL
, 0 }, /* Pc */
271 { NULL
, 0 }, /* Pf */
272 { NULL
, 0 }, /* Po */
273 { NULL
, 0 }, /* Pq */
274 { NULL
, 0 }, /* Qc */
275 { NULL
, 0 }, /* Ql */
276 { NULL
, 0 }, /* Qo */
277 { NULL
, 0 }, /* Qq */
278 { NULL
, 0 }, /* Re */
279 { NULL
, 0 }, /* Rs */
280 { NULL
, 0 }, /* Sc */
281 { NULL
, 0 }, /* So */
282 { NULL
, 0 }, /* Sq */
283 { NULL
, 0 }, /* Sm */
284 { NULL
, 0 }, /* Sx */
285 { NULL
, TYPE_Sy
}, /* Sy */
286 { NULL
, TYPE_Tn
}, /* Tn */
287 { NULL
, 0 }, /* Ux */
288 { NULL
, 0 }, /* Xc */
289 { NULL
, 0 }, /* Xo */
290 { parse_mdoc_head
, 0 }, /* Fo */
291 { NULL
, 0 }, /* Fc */
292 { NULL
, 0 }, /* Oo */
293 { NULL
, 0 }, /* Oc */
294 { NULL
, 0 }, /* Bk */
295 { NULL
, 0 }, /* Ek */
296 { NULL
, 0 }, /* Bt */
297 { NULL
, 0 }, /* Hf */
298 { NULL
, 0 }, /* Fr */
299 { NULL
, 0 }, /* Ud */
300 { NULL
, TYPE_Lb
}, /* Lb */
301 { NULL
, 0 }, /* Lp */
302 { NULL
, TYPE_Lk
}, /* Lk */
303 { NULL
, TYPE_Mt
}, /* Mt */
304 { NULL
, 0 }, /* Brq */
305 { NULL
, 0 }, /* Bro */
306 { NULL
, 0 }, /* Brc */
307 { NULL
, 0 }, /* %C */
308 { NULL
, 0 }, /* Es */
309 { NULL
, 0 }, /* En */
310 { NULL
, TYPE_Dx
}, /* Dx */
311 { NULL
, 0 }, /* %Q */
312 { NULL
, 0 }, /* br */
313 { NULL
, 0 }, /* sp */
314 { NULL
, 0 }, /* %U */
315 { NULL
, 0 }, /* Ta */
319 main(int argc
, char *argv
[])
323 const char *path_arg
;
325 struct manpaths dirs
;
327 struct ohash_info mpages_info
, mlinks_info
;
329 memset(stmts
, 0, STMT__MAX
* sizeof(sqlite3_stmt
*));
330 memset(&dirs
, 0, sizeof(struct manpaths
));
332 mpages_info
.alloc
= mlinks_info
.alloc
= hash_alloc
;
333 mpages_info
.halloc
= mlinks_info
.halloc
= hash_halloc
;
334 mpages_info
.hfree
= mlinks_info
.hfree
= hash_free
;
336 mpages_info
.key_offset
= offsetof(struct mpage
, inodev
);
337 mlinks_info
.key_offset
= offsetof(struct mlink
, file
);
339 progname
= strrchr(argv
[0], '/');
340 if (progname
== NULL
)
346 * We accept a few different invocations.
347 * The CHECKOP macro makes sure that invocation styles don't
348 * clobber each other.
350 #define CHECKOP(_op, _ch) do \
351 if (OP_DEFAULT != (_op)) { \
352 fprintf(stderr, "-%c: Conflicting option\n", (_ch)); \
354 } while (/*CONSTCOND*/0)
359 while (-1 != (ch
= getopt(argc
, argv
, "aC:d:nQT:tu:vW")))
378 mparse_options
|= MPARSE_QUICK
;
381 if (strcmp(optarg
, "utf8")) {
382 fprintf(stderr
, "-T%s: Unsupported "
383 "output format\n", optarg
);
390 dup2(STDOUT_FILENO
, STDERR_FILENO
);
412 if (OP_CONFFILE
== op
&& argc
> 0) {
413 fprintf(stderr
, "-C: Too many arguments\n");
417 exitcode
= (int)MANDOCLEVEL_OK
;
418 mp
= mparse_alloc(mparse_options
, MANDOCLEVEL_FATAL
, NULL
, NULL
);
421 ohash_init(&mpages
, 6, &mpages_info
);
422 ohash_init(&mlinks
, 6, &mlinks_info
);
424 if (OP_UPDATE
== op
|| OP_DELETE
== op
|| OP_TEST
== op
) {
426 * Force processing all files.
431 * All of these deal with a specific directory.
432 * Jump into that directory then collect files specified
433 * on the command-line.
435 if (0 == set_basedir(path_arg
))
437 for (i
= 0; i
< argc
; i
++)
444 mpages_merge(mc
, mp
);
448 * If we have arguments, use them as our manpaths.
449 * If we don't, grok from manpath(1) or however else
450 * manpath_parse() wants to do it.
453 dirs
.paths
= mandoc_calloc
454 (argc
, sizeof(char *));
455 dirs
.sz
= (size_t)argc
;
456 for (i
= 0; i
< argc
; i
++)
457 dirs
.paths
[i
] = mandoc_strdup(argv
[i
]);
459 manpath_parse(&dirs
, path_arg
, NULL
, NULL
);
462 * First scan the tree rooted at a base directory, then
463 * build a new database and finally move it into place.
464 * Ignore zero-length directories and strip trailing
467 for (j
= 0; j
< dirs
.sz
; j
++) {
468 sz
= strlen(dirs
.paths
[j
]);
469 if (sz
&& '/' == dirs
.paths
[j
][sz
- 1])
470 dirs
.paths
[j
][--sz
] = '\0';
475 ohash_init(&mpages
, 6, &mpages_info
);
476 ohash_init(&mlinks
, 6, &mlinks_info
);
479 if (0 == set_basedir(dirs
.paths
[j
]))
483 if (0 == set_basedir(dirs
.paths
[j
]))
488 mpages_merge(mc
, mp
);
491 if (j
+ 1 < dirs
.sz
) {
493 ohash_delete(&mpages
);
494 ohash_delete(&mlinks
);
504 ohash_delete(&mpages
);
505 ohash_delete(&mlinks
);
508 fprintf(stderr
, "usage: %s [-anQvW] [-C file] [-Tutf8]\n"
509 " %s [-anQvW] [-Tutf8] dir ...\n"
510 " %s [-nQvW] [-Tutf8] -d dir [file ...]\n"
511 " %s [-nvW] -u dir [file ...]\n"
512 " %s [-Q] -t file ...\n",
513 progname
, progname
, progname
,
516 return((int)MANDOCLEVEL_BADARG
);
520 * Scan a directory tree rooted at "basedir" for manpages.
521 * We use fts(), scanning directory parts along the way for clues to our
522 * section and architecture.
524 * If use_all has been specified, grok all files.
525 * If not, sanitise paths to the following:
527 * [./]man*[/<arch>]/<name>.<section>
529 * [./]cat<section>[/<arch>]/<name>.0
531 * TODO: accomodate for multi-language directories.
540 char *dsec
, *arch
, *fsec
, *cp
;
545 argv
[1] = (char *)NULL
;
548 * Walk through all components under the directory, using the
549 * logical descent of files.
551 f
= fts_open((char * const *)argv
, FTS_LOGICAL
, NULL
);
553 exitcode
= (int)MANDOCLEVEL_SYSERR
;
561 while (NULL
!= (ff
= fts_read(f
))) {
562 path
= ff
->fts_path
+ 2;
564 * If we're a regular file, add an mlink by using the
565 * stored directory data and handling the filename.
567 if (FTS_F
== ff
->fts_info
) {
568 if (0 == strcmp(path
, MANDOC_DB
))
570 if ( ! use_all
&& ff
->fts_level
< 2) {
572 say(path
, "Extraneous file");
574 } else if (NULL
== (fsec
=
575 strrchr(ff
->fts_name
, '.'))) {
579 "No filename suffix");
582 } else if (0 == strcmp(++fsec
, "html")) {
584 say(path
, "Skip html");
586 } else if (0 == strcmp(fsec
, "gz")) {
588 say(path
, "Skip gz");
590 } else if (0 == strcmp(fsec
, "ps")) {
592 say(path
, "Skip ps");
594 } else if (0 == strcmp(fsec
, "pdf")) {
596 say(path
, "Skip pdf");
598 } else if ( ! use_all
&&
599 ((FORM_SRC
== dform
&& strcmp(fsec
, dsec
)) ||
600 (FORM_CAT
== dform
&& strcmp(fsec
, "0")))) {
602 say(path
, "Wrong filename suffix");
607 mlink
= mandoc_calloc(1, sizeof(struct mlink
));
608 strlcpy(mlink
->file
, path
, sizeof(mlink
->file
));
609 mlink
->dform
= dform
;
612 mlink
->name
= ff
->fts_name
;
614 mlink_add(mlink
, ff
->fts_statp
);
616 } else if (FTS_D
!= ff
->fts_info
&&
617 FTS_DP
!= ff
->fts_info
) {
619 say(path
, "Not a regular file");
623 switch (ff
->fts_level
) {
625 /* Ignore the root directory. */
629 * This might contain manX/ or catX/.
630 * Try to infer this from the name.
631 * If we're not in use_all, enforce it.
634 if (FTS_DP
== ff
->fts_info
)
637 if (0 == strncmp(cp
, "man", 3)) {
640 } else if (0 == strncmp(cp
, "cat", 3)) {
648 if (NULL
!= dsec
|| use_all
)
652 say(path
, "Unknown directory part");
653 fts_set(f
, ff
, FTS_SKIP
);
657 * Possibly our architecture.
658 * If we're descending, keep tabs on it.
660 if (FTS_DP
!= ff
->fts_info
&& NULL
!= dsec
)
666 if (FTS_DP
== ff
->fts_info
|| use_all
)
669 say(path
, "Extraneous directory part");
670 fts_set(f
, ff
, FTS_SKIP
);
680 * Add a file to the mlinks table.
681 * Do not verify that it's a "valid" looking manpage (we'll do that
684 * Try to infer the manual section, architecture, and page name from the
685 * path, assuming it looks like
687 * [./]man*[/<arch>]/<name>.<section>
689 * [./]cat<section>[/<arch>]/<name>.0
691 * See treescan() for the fts(3) version of this.
694 filescan(const char *file
)
703 if (0 == strncmp(file
, "./", 2))
706 if (NULL
== realpath(file
, buf
)) {
707 exitcode
= (int)MANDOCLEVEL_BADARG
;
712 if (strstr(buf
, basedir
) == buf
)
713 start
= buf
+ strlen(basedir
) + 1;
714 else if (OP_TEST
== op
)
717 exitcode
= (int)MANDOCLEVEL_BADARG
;
718 say("", "%s: outside base directory", buf
);
722 if (-1 == stat(buf
, &st
)) {
723 exitcode
= (int)MANDOCLEVEL_BADARG
;
726 } else if ( ! (S_IFREG
& st
.st_mode
)) {
727 exitcode
= (int)MANDOCLEVEL_BADARG
;
728 say(file
, "Not a regular file");
732 mlink
= mandoc_calloc(1, sizeof(struct mlink
));
733 strlcpy(mlink
->file
, start
, sizeof(mlink
->file
));
736 * First try to guess our directory structure.
737 * If we find a separator, try to look for man* or cat*.
738 * If we find one of these and what's underneath is a directory,
739 * assume it's an architecture.
741 if (NULL
!= (p
= strchr(start
, '/'))) {
743 if (0 == strncmp(start
, "man", 3)) {
744 mlink
->dform
= FORM_SRC
;
745 mlink
->dsec
= start
+ 3;
746 } else if (0 == strncmp(start
, "cat", 3)) {
747 mlink
->dform
= FORM_CAT
;
748 mlink
->dsec
= start
+ 3;
752 if (NULL
!= mlink
->dsec
&& NULL
!= (p
= strchr(start
, '/'))) {
760 * Now check the file suffix.
761 * Suffix of `.0' indicates a catpage, `.1-9' is a manpage.
763 p
= strrchr(start
, '\0');
764 while (p
-- > start
&& '/' != *p
&& '.' != *p
)
773 * Now try to parse the name.
774 * Use the filename portion of the path.
777 if (NULL
!= (p
= strrchr(start
, '/'))) {
781 mlink_add(mlink
, &st
);
785 mlink_add(struct mlink
*mlink
, const struct stat
*st
)
787 struct inodev inodev
;
791 assert(NULL
!= mlink
->file
);
793 mlink
->dsec
= mandoc_strdup(mlink
->dsec
? mlink
->dsec
: "");
794 mlink
->arch
= mandoc_strdup(mlink
->arch
? mlink
->arch
: "");
795 mlink
->name
= mandoc_strdup(mlink
->name
? mlink
->name
: "");
796 mlink
->fsec
= mandoc_strdup(mlink
->fsec
? mlink
->fsec
: "");
798 if ('0' == *mlink
->fsec
) {
800 mlink
->fsec
= mandoc_strdup(mlink
->dsec
);
801 mlink
->fform
= FORM_CAT
;
802 } else if ('1' <= *mlink
->fsec
&& '9' >= *mlink
->fsec
)
803 mlink
->fform
= FORM_SRC
;
805 mlink
->fform
= FORM_NONE
;
807 slot
= ohash_qlookup(&mlinks
, mlink
->file
);
808 assert(NULL
== ohash_find(&mlinks
, slot
));
809 ohash_insert(&mlinks
, slot
, mlink
);
811 inodev
.st_ino
= st
->st_ino
;
812 inodev
.st_dev
= st
->st_dev
;
813 slot
= ohash_lookup_memory(&mpages
, (char *)&inodev
,
814 sizeof(struct inodev
), inodev
.st_ino
);
815 mpage
= ohash_find(&mpages
, slot
);
817 mpage
= mandoc_calloc(1, sizeof(struct mpage
));
818 mpage
->inodev
.st_ino
= inodev
.st_ino
;
819 mpage
->inodev
.st_dev
= inodev
.st_dev
;
820 ohash_insert(&mpages
, slot
, mpage
);
822 mlink
->next
= mpage
->mlinks
;
823 mpage
->mlinks
= mlink
;
824 mlink
->mpage
= mpage
;
828 mlink_free(struct mlink
*mlink
)
845 mpage
= ohash_first(&mpages
, &slot
);
846 while (NULL
!= mpage
) {
847 while (NULL
!= (mlink
= mpage
->mlinks
)) {
848 mpage
->mlinks
= mlink
->next
;
856 mpage
= ohash_next(&mpages
, &slot
);
861 * For each mlink to the mpage, check whether the path looks like
862 * it is formatted, and if it does, check whether a source manual
863 * exists by the same name, ignoring the suffix.
864 * If both conditions hold, drop the mlink.
867 mlinks_undupe(struct mpage
*mpage
)
874 mpage
->form
= FORM_CAT
;
875 prev
= &mpage
->mlinks
;
876 while (NULL
!= (mlink
= *prev
)) {
877 if (FORM_CAT
!= mlink
->dform
) {
878 mpage
->form
= FORM_NONE
;
881 if (strlcpy(buf
, mlink
->file
, PATH_MAX
) >= PATH_MAX
) {
883 say(mlink
->file
, "Filename too long");
886 bufp
= strstr(buf
, "cat");
887 assert(NULL
!= bufp
);
888 memcpy(bufp
, "man", 3);
889 if (NULL
!= (bufp
= strrchr(buf
, '.')))
891 strlcat(buf
, mlink
->dsec
, PATH_MAX
);
892 if (NULL
== ohash_find(&mlinks
,
893 ohash_qlookup(&mlinks
, buf
)))
896 say(mlink
->file
, "Man source exists: %s", buf
);
903 prev
= &(*prev
)->next
;
908 mlink_check(struct mpage
*mpage
, struct mlink
*mlink
)
915 * Check whether the manual section given in a file
916 * agrees with the directory where the file is located.
917 * Some manuals have suffixes like (3p) on their
918 * section number either inside the file or in the
919 * directory name, some are linked into more than one
920 * section, like encrypt(1) = makekey(8).
923 if (FORM_SRC
== mpage
->form
&&
924 strcasecmp(mpage
->sec
, mlink
->dsec
)) {
926 say(mlink
->file
, "Section \"%s\" manual in %s directory",
927 mpage
->sec
, mlink
->dsec
);
931 * Manual page directories exist for each kernel
932 * architecture as returned by machine(1).
933 * However, many manuals only depend on the
934 * application architecture as returned by arch(1).
935 * For example, some (2/ARM) manuals are shared
936 * across the "armish" and "zaurus" kernel
938 * A few manuals are even shared across completely
939 * different architectures, for example fdformat(1)
940 * on amd64, i386, sparc, and sparc64.
943 if (strcasecmp(mpage
->arch
, mlink
->arch
)) {
945 say(mlink
->file
, "Architecture \"%s\" manual in "
946 "\"%s\" directory", mpage
->arch
, mlink
->arch
);
949 if (strcasecmp(mpage
->title
, mlink
->name
))
956 * Run through the files in the global vector "mpages"
957 * and add them to the database specified in "basedir".
959 * This handles the parsing scheme itself, using the cues of directory
960 * and filename to determine whether the file is parsable or not.
963 mpages_merge(struct mchars
*mc
, struct mparse
*mp
)
966 struct ohash_info str_info
;
967 struct mpage
*mpage
, *mpage_dest
;
968 struct mlink
*mlink
, *mlink_dest
;
975 enum mandoclevel lvl
;
977 str_info
.alloc
= hash_alloc
;
978 str_info
.halloc
= hash_halloc
;
979 str_info
.hfree
= hash_free
;
980 str_info
.key_offset
= offsetof(struct str
, key
);
983 SQL_EXEC("BEGIN TRANSACTION");
985 mpage
= ohash_first(&mpages
, &pslot
);
986 while (NULL
!= mpage
) {
987 mlinks_undupe(mpage
);
988 if (NULL
== mpage
->mlinks
) {
989 mpage
= ohash_next(&mpages
, &pslot
);
993 ohash_init(&strings
, 6, &str_info
);
999 * Try interpreting the file as mdoc(7) or man(7)
1000 * source code, unless it is already known to be
1001 * formatted. Fall back to formatted mode.
1003 if (FORM_CAT
!= mpage
->mlinks
->dform
||
1004 FORM_CAT
!= mpage
->mlinks
->fform
) {
1005 lvl
= mparse_readfd(mp
, -1, mpage
->mlinks
->file
);
1006 if (lvl
< MANDOCLEVEL_FATAL
)
1007 mparse_result(mp
, &mdoc
, &man
, &sodest
);
1010 if (NULL
!= sodest
) {
1011 mlink_dest
= ohash_find(&mlinks
,
1012 ohash_qlookup(&mlinks
, sodest
));
1013 if (NULL
!= mlink_dest
) {
1015 /* The .so target exists. */
1017 mpage_dest
= mlink_dest
->mpage
;
1018 mlink
= mpage
->mlinks
;
1020 mlink
->mpage
= mpage_dest
;
1023 * If the target was already
1024 * processed, add the links
1025 * to the database now.
1026 * Otherwise, this will
1027 * happen when we come
1031 if (mpage_dest
->recno
)
1034 if (NULL
== mlink
->next
)
1036 mlink
= mlink
->next
;
1039 /* Move all links to the target. */
1041 mlink
->next
= mlink_dest
->next
;
1042 mlink_dest
->next
= mpage
->mlinks
;
1043 mpage
->mlinks
= NULL
;
1045 ohash_delete(&strings
);
1046 mpage
= ohash_next(&mpages
, &pslot
);
1048 } else if (NULL
!= mdoc
) {
1049 mpage
->form
= FORM_SRC
;
1051 mandoc_strdup(mdoc_meta(mdoc
)->msec
);
1052 mpage
->arch
= mdoc_meta(mdoc
)->arch
;
1053 mpage
->arch
= mandoc_strdup(
1054 NULL
== mpage
->arch
? "" : mpage
->arch
);
1056 mandoc_strdup(mdoc_meta(mdoc
)->title
);
1057 } else if (NULL
!= man
) {
1058 mpage
->form
= FORM_SRC
;
1060 mandoc_strdup(man_meta(man
)->msec
);
1062 mandoc_strdup(mpage
->mlinks
->arch
);
1064 mandoc_strdup(man_meta(man
)->title
);
1066 mpage
->form
= FORM_CAT
;
1068 mandoc_strdup(mpage
->mlinks
->dsec
);
1070 mandoc_strdup(mpage
->mlinks
->arch
);
1072 mandoc_strdup(mpage
->mlinks
->name
);
1074 putkey(mpage
, mpage
->sec
, TYPE_sec
);
1075 putkey(mpage
, '\0' == *mpage
->arch
?
1076 any
: mpage
->arch
, TYPE_arch
);
1078 for (mlink
= mpage
->mlinks
; mlink
; mlink
= mlink
->next
) {
1079 if ('\0' != *mlink
->dsec
)
1080 putkey(mpage
, mlink
->dsec
, TYPE_sec
);
1081 if ('\0' != *mlink
->fsec
)
1082 putkey(mpage
, mlink
->fsec
, TYPE_sec
);
1083 putkey(mpage
, '\0' == *mlink
->arch
?
1084 any
: mlink
->arch
, TYPE_arch
);
1085 putkey(mpage
, mlink
->name
, TYPE_Nm
);
1088 if (warnings
&& !use_all
) {
1090 for (mlink
= mpage
->mlinks
; mlink
;
1091 mlink
= mlink
->next
)
1092 if (mlink_check(mpage
, mlink
))
1098 if (NULL
!= (cp
= mdoc_meta(mdoc
)->name
))
1099 putkey(mpage
, cp
, TYPE_Nm
);
1100 assert(NULL
== mpage
->desc
);
1101 parse_mdoc(mpage
, mdoc_node(mdoc
));
1102 putkey(mpage
, NULL
!= mpage
->desc
?
1103 mpage
->desc
: mpage
->mlinks
->name
, TYPE_Nd
);
1104 } else if (NULL
!= man
)
1105 parse_man(mpage
, man_node(man
));
1110 ohash_delete(&strings
);
1111 mpage
= ohash_next(&mpages
, &pslot
);
1115 SQL_EXEC("END TRANSACTION");
1119 parse_cat(struct mpage
*mpage
)
1122 char *line
, *p
, *title
;
1123 size_t len
, plen
, titlesz
;
1125 if (NULL
== (stream
= fopen(mpage
->mlinks
->file
, "r"))) {
1127 say(mpage
->mlinks
->file
, NULL
);
1131 /* Skip to first blank line. */
1133 while (NULL
!= (line
= fgetln(stream
, &len
)))
1138 * Assume the first line that is not indented
1139 * is the first section header. Skip to it.
1142 while (NULL
!= (line
= fgetln(stream
, &len
)))
1143 if ('\n' != *line
&& ' ' != *line
)
1147 * Read up until the next section into a buffer.
1148 * Strip the leading and trailing newline from each read line,
1149 * appending a trailing space.
1150 * Ignore empty (whitespace-only) lines.
1156 while (NULL
!= (line
= fgetln(stream
, &len
))) {
1157 if (' ' != *line
|| '\n' != line
[len
- 1])
1159 while (len
> 0 && isspace((unsigned char)*line
)) {
1165 title
= mandoc_realloc(title
, titlesz
+ len
);
1166 memcpy(title
+ titlesz
, line
, len
);
1168 title
[titlesz
- 1] = ' ';
1172 * If no page content can be found, or the input line
1173 * is already the next section header, or there is no
1174 * trailing newline, reuse the page title as the page
1178 if (NULL
== title
|| '\0' == *title
) {
1180 say(mpage
->mlinks
->file
,
1181 "Cannot find NAME section");
1182 assert(NULL
== mpage
->desc
);
1183 mpage
->desc
= mandoc_strdup(mpage
->mlinks
->name
);
1184 putkey(mpage
, mpage
->mlinks
->name
, TYPE_Nd
);
1190 title
= mandoc_realloc(title
, titlesz
+ 1);
1191 title
[titlesz
] = '\0';
1194 * Skip to the first dash.
1195 * Use the remaining line as the description (no more than 70
1199 if (NULL
!= (p
= strstr(title
, "- "))) {
1200 for (p
+= 2; ' ' == *p
|| '\b' == *p
; p
++)
1201 /* Skip to next word. */ ;
1204 say(mpage
->mlinks
->file
,
1205 "No dash in title line");
1211 /* Strip backspace-encoding from line. */
1213 while (NULL
!= (line
= memchr(p
, '\b', plen
))) {
1216 memmove(line
, line
+ 1, plen
--);
1219 memmove(line
- 1, line
+ 1, plen
- len
);
1223 assert(NULL
== mpage
->desc
);
1224 mpage
->desc
= mandoc_strdup(p
);
1225 putkey(mpage
, mpage
->desc
, TYPE_Nd
);
1231 * Put a type/word pair into the word database for this particular file.
1234 putkey(const struct mpage
*mpage
, char *value
, uint64_t type
)
1238 assert(NULL
!= value
);
1239 if (TYPE_arch
== type
)
1240 for (cp
= value
; *cp
; cp
++)
1241 if (isupper((unsigned char)*cp
))
1242 *cp
= _tolower((unsigned char)*cp
);
1243 putkeys(mpage
, value
, strlen(value
), type
);
1247 * Grok all nodes at or below a certain mdoc node into putkey().
1250 putmdockey(const struct mpage
*mpage
,
1251 const struct mdoc_node
*n
, uint64_t m
)
1254 for ( ; NULL
!= n
; n
= n
->next
) {
1255 if (NULL
!= n
->child
)
1256 putmdockey(mpage
, n
->child
, m
);
1257 if (MDOC_TEXT
== n
->type
)
1258 putkey(mpage
, n
->string
, m
);
1263 parse_man(struct mpage
*mpage
, const struct man_node
*n
)
1265 const struct man_node
*head
, *body
;
1266 char *start
, *title
;
1274 * We're only searching for one thing: the first text child in
1275 * the BODY of a NAME section. Since we don't keep track of
1276 * sections in -man, run some hoops to find out whether we're in
1277 * the correct section or not.
1280 if (MAN_BODY
== n
->type
&& MAN_SH
== n
->tok
) {
1282 assert(body
->parent
);
1283 if (NULL
!= (head
= body
->parent
->head
) &&
1284 1 == head
->nchild
&&
1285 NULL
!= (head
= (head
->child
)) &&
1286 MAN_TEXT
== head
->type
&&
1287 0 == strcmp(head
->string
, "NAME") &&
1288 NULL
!= body
->child
) {
1291 * Suck the entire NAME section into memory.
1292 * Yes, we might run away.
1293 * But too many manuals have big, spread-out
1294 * NAME sections over many lines.
1298 man_deroff(&title
, body
);
1303 * Go through a special heuristic dance here.
1304 * Conventionally, one or more manual names are
1305 * comma-specified prior to a whitespace, then a
1306 * dash, then a description. Try to puzzle out
1307 * the name parts here.
1312 sz
= strcspn(start
, " ,");
1313 if ('\0' == start
[sz
])
1320 * Assume a stray trailing comma in the
1321 * name list if a name begins with a dash.
1324 if ('-' == start
[0] ||
1325 ('\\' == start
[0] && '-' == start
[1]))
1328 putkey(mpage
, start
, TYPE_Nm
);
1335 assert(',' == byte
);
1337 while (' ' == *start
)
1341 if (start
== title
) {
1342 putkey(mpage
, start
, TYPE_Nm
);
1347 while (isspace((unsigned char)*start
))
1350 if (0 == strncmp(start
, "-", 1))
1352 else if (0 == strncmp(start
, "\\-\\-", 4))
1354 else if (0 == strncmp(start
, "\\-", 2))
1356 else if (0 == strncmp(start
, "\\(en", 4))
1358 else if (0 == strncmp(start
, "\\(em", 4))
1361 while (' ' == *start
)
1364 assert(NULL
== mpage
->desc
);
1365 mpage
->desc
= mandoc_strdup(start
);
1366 putkey(mpage
, mpage
->desc
, TYPE_Nd
);
1372 for (n
= n
->child
; n
; n
= n
->next
) {
1373 if (NULL
!= mpage
->desc
)
1375 parse_man(mpage
, n
);
1380 parse_mdoc(struct mpage
*mpage
, const struct mdoc_node
*n
)
1384 for (n
= n
->child
; NULL
!= n
; n
= n
->next
) {
1395 if (NULL
!= mdocs
[n
->tok
].fp
)
1396 if (0 == (*mdocs
[n
->tok
].fp
)(mpage
, n
))
1398 if (mdocs
[n
->tok
].mask
)
1399 putmdockey(mpage
, n
->child
,
1400 mdocs
[n
->tok
].mask
);
1403 assert(MDOC_ROOT
!= n
->type
);
1406 if (NULL
!= n
->child
)
1407 parse_mdoc(mpage
, n
);
1412 parse_mdoc_Fd(struct mpage
*mpage
, const struct mdoc_node
*n
)
1414 const char *start
, *end
;
1417 if (SEC_SYNOPSIS
!= n
->sec
||
1418 NULL
== (n
= n
->child
) ||
1419 MDOC_TEXT
!= n
->type
)
1423 * Only consider those `Fd' macro fields that begin with an
1424 * "inclusion" token (versus, e.g., #define).
1427 if (strcmp("#include", n
->string
))
1430 if (NULL
== (n
= n
->next
) || MDOC_TEXT
!= n
->type
)
1434 * Strip away the enclosing angle brackets and make sure we're
1439 if ('<' == *start
|| '"' == *start
)
1442 if (0 == (sz
= strlen(start
)))
1445 end
= &start
[(int)sz
- 1];
1446 if ('>' == *end
|| '"' == *end
)
1450 putkeys(mpage
, start
, end
- start
+ 1, TYPE_In
);
1455 parse_mdoc_Fn(struct mpage
*mpage
, const struct mdoc_node
*n
)
1459 if (NULL
== (n
= n
->child
) || MDOC_TEXT
!= n
->type
)
1463 * Parse: .Fn "struct type *name" "char *arg".
1464 * First strip away pointer symbol.
1465 * Then store the function name, then type.
1466 * Finally, store the arguments.
1469 if (NULL
== (cp
= strrchr(n
->string
, ' ')))
1475 putkey(mpage
, cp
, TYPE_Fn
);
1478 putkeys(mpage
, n
->string
, cp
- n
->string
, TYPE_Ft
);
1480 for (n
= n
->next
; NULL
!= n
; n
= n
->next
)
1481 if (MDOC_TEXT
== n
->type
)
1482 putkey(mpage
, n
->string
, TYPE_Fa
);
1488 parse_mdoc_Xr(struct mpage
*mpage
, const struct mdoc_node
*n
)
1492 if (NULL
== (n
= n
->child
))
1495 if (NULL
== n
->next
) {
1496 putkey(mpage
, n
->string
, TYPE_Xr
);
1500 mandoc_asprintf(&cp
, "%s(%s)", n
->string
, n
->next
->string
);
1501 putkey(mpage
, cp
, TYPE_Xr
);
1507 parse_mdoc_Nd(struct mpage
*mpage
, const struct mdoc_node
*n
)
1511 if (MDOC_BODY
!= n
->type
)
1515 * Special-case the `Nd' because we need to put the description
1516 * into the document table.
1519 for (n
= n
->child
; NULL
!= n
; n
= n
->next
) {
1520 if (MDOC_TEXT
== n
->type
) {
1521 if (NULL
!= mpage
->desc
) {
1522 sz
= strlen(mpage
->desc
) +
1523 strlen(n
->string
) + 2;
1524 mpage
->desc
= mandoc_realloc(
1526 strlcat(mpage
->desc
, " ", sz
);
1527 strlcat(mpage
->desc
, n
->string
, sz
);
1529 mpage
->desc
= mandoc_strdup(n
->string
);
1531 if (NULL
!= n
->child
)
1532 parse_mdoc_Nd(mpage
, n
);
1538 parse_mdoc_Nm(struct mpage
*mpage
, const struct mdoc_node
*n
)
1541 return(SEC_NAME
== n
->sec
||
1542 (SEC_SYNOPSIS
== n
->sec
&& MDOC_HEAD
== n
->type
));
1546 parse_mdoc_Sh(struct mpage
*mpage
, const struct mdoc_node
*n
)
1549 return(SEC_CUSTOM
== n
->sec
&& MDOC_HEAD
== n
->type
);
1553 parse_mdoc_head(struct mpage
*mpage
, const struct mdoc_node
*n
)
1556 return(MDOC_HEAD
== n
->type
);
1560 parse_mdoc_body(struct mpage
*mpage
, const struct mdoc_node
*n
)
1563 return(MDOC_BODY
== n
->type
);
1567 * Add a string to the hash table for the current manual.
1568 * Each string has a bitmask telling which macros it belongs to.
1569 * When we finish the manual, we'll dump the table.
1572 putkeys(const struct mpage
*mpage
,
1573 const char *cp
, size_t sz
, uint64_t v
)
1585 for (i
= 0, mask
= 1;
1586 i
< mansearch_keymax
;
1590 say(mpage
->mlinks
->file
, "Adding key %s=%*s",
1591 mansearch_keynames
[i
], sz
, cp
);
1595 slot
= ohash_qlookupi(&strings
, cp
, &end
);
1596 s
= ohash_find(&strings
, slot
);
1598 if (NULL
!= s
&& mpage
== s
->mpage
) {
1601 } else if (NULL
== s
) {
1602 s
= mandoc_calloc(sizeof(struct str
) + sz
+ 1, 1);
1603 memcpy(s
->key
, cp
, sz
);
1604 ohash_insert(&strings
, slot
, s
);
1611 * Take a Unicode codepoint and produce its UTF-8 encoding.
1612 * This isn't the best way to do this, but it works.
1613 * The magic numbers are from the UTF-8 packaging.
1614 * They're not as scary as they seem: read the UTF-8 spec for details.
1617 utf8(unsigned int cp
, char out
[7])
1622 if (cp
<= 0x0000007F) {
1625 } else if (cp
<= 0x000007FF) {
1627 out
[0] = (cp
>> 6 & 31) | 192;
1628 out
[1] = (cp
& 63) | 128;
1629 } else if (cp
<= 0x0000FFFF) {
1631 out
[0] = (cp
>> 12 & 15) | 224;
1632 out
[1] = (cp
>> 6 & 63) | 128;
1633 out
[2] = (cp
& 63) | 128;
1634 } else if (cp
<= 0x001FFFFF) {
1636 out
[0] = (cp
>> 18 & 7) | 240;
1637 out
[1] = (cp
>> 12 & 63) | 128;
1638 out
[2] = (cp
>> 6 & 63) | 128;
1639 out
[3] = (cp
& 63) | 128;
1640 } else if (cp
<= 0x03FFFFFF) {
1642 out
[0] = (cp
>> 24 & 3) | 248;
1643 out
[1] = (cp
>> 18 & 63) | 128;
1644 out
[2] = (cp
>> 12 & 63) | 128;
1645 out
[3] = (cp
>> 6 & 63) | 128;
1646 out
[4] = (cp
& 63) | 128;
1647 } else if (cp
<= 0x7FFFFFFF) {
1649 out
[0] = (cp
>> 30 & 1) | 252;
1650 out
[1] = (cp
>> 24 & 63) | 128;
1651 out
[2] = (cp
>> 18 & 63) | 128;
1652 out
[3] = (cp
>> 12 & 63) | 128;
1653 out
[4] = (cp
>> 6 & 63) | 128;
1654 out
[5] = (cp
& 63) | 128;
1663 * Store the rendered version of a key, or alias the pointer
1664 * if the key contains no escape sequences.
1667 render_key(struct mchars
*mc
, struct str
*key
)
1669 size_t sz
, bsz
, pos
;
1670 char utfbuf
[7], res
[6];
1672 const char *seq
, *cpp
, *val
;
1674 enum mandoc_esc esc
;
1676 assert(NULL
== key
->rendered
);
1680 res
[2] = ASCII_NBRSP
;
1681 res
[3] = ASCII_HYPH
;
1682 res
[4] = ASCII_BREAK
;
1689 * Pre-check: if we have no stop-characters, then set the
1690 * pointer as ourselvse and get out of here.
1692 if (strcspn(val
, res
) == bsz
) {
1693 key
->rendered
= key
->key
;
1697 /* Pre-allocate by the length of the input */
1699 buf
= mandoc_malloc(++bsz
);
1702 while ('\0' != *val
) {
1704 * Halt on the first escape sequence.
1705 * This also halts on the end of string, in which case
1706 * we just copy, fallthrough, and exit the loop.
1708 if ((sz
= strcspn(val
, res
)) > 0) {
1709 memcpy(&buf
[pos
], val
, sz
);
1733 /* Read past the slash. */
1738 * Parse the escape sequence and see if it's a
1739 * predefined character or special character.
1743 ((const char **)&val
, &seq
, &len
);
1744 if (ESCAPE_ERROR
== esc
)
1746 if (ESCAPE_SPECIAL
!= esc
)
1750 * Render the special character
1751 * as either UTF-8 or ASCII.
1755 if (0 == (u
= mchars_spec2cp(mc
, seq
, len
)))
1758 if (0 == (sz
= utf8(u
, utfbuf
)))
1762 cpp
= mchars_spec2str(mc
, seq
, len
, &sz
);
1765 if (ASCII_NBRSP
== *cpp
) {
1771 /* Copy the rendered glyph into the stream. */
1774 buf
= mandoc_realloc(buf
, bsz
);
1775 memcpy(&buf
[pos
], cpp
, sz
);
1780 key
->rendered
= buf
;
1784 dbadd_mlink(const struct mlink
*mlink
)
1789 SQL_BIND_TEXT(stmts
[STMT_INSERT_LINK
], i
, mlink
->dsec
);
1790 SQL_BIND_TEXT(stmts
[STMT_INSERT_LINK
], i
, mlink
->arch
);
1791 SQL_BIND_TEXT(stmts
[STMT_INSERT_LINK
], i
, mlink
->name
);
1792 SQL_BIND_INT64(stmts
[STMT_INSERT_LINK
], i
, mlink
->mpage
->recno
);
1793 SQL_STEP(stmts
[STMT_INSERT_LINK
]);
1794 sqlite3_reset(stmts
[STMT_INSERT_LINK
]);
1798 * Flush the current page's terms (and their bits) into the database.
1799 * Wrap the entire set of additions in a transaction to make sqlite be a
1801 * Also, handle escape sequences at the last possible moment.
1804 dbadd(struct mpage
*mpage
, struct mchars
*mc
)
1806 struct mlink
*mlink
;
1812 say(mpage
->mlinks
->file
, "Adding to database");
1818 SQL_BIND_INT(stmts
[STMT_INSERT_PAGE
], i
, FORM_SRC
== mpage
->form
);
1819 SQL_STEP(stmts
[STMT_INSERT_PAGE
]);
1820 mpage
->recno
= sqlite3_last_insert_rowid(db
);
1821 sqlite3_reset(stmts
[STMT_INSERT_PAGE
]);
1823 for (mlink
= mpage
->mlinks
; mlink
; mlink
= mlink
->next
)
1826 for (key
= ohash_first(&strings
, &slot
); NULL
!= key
;
1827 key
= ohash_next(&strings
, &slot
)) {
1828 assert(key
->mpage
== mpage
);
1829 if (NULL
== key
->rendered
)
1830 render_key(mc
, key
);
1832 SQL_BIND_INT64(stmts
[STMT_INSERT_KEY
], i
, key
->mask
);
1833 SQL_BIND_TEXT(stmts
[STMT_INSERT_KEY
], i
, key
->rendered
);
1834 SQL_BIND_INT64(stmts
[STMT_INSERT_KEY
], i
, mpage
->recno
);
1835 SQL_STEP(stmts
[STMT_INSERT_KEY
]);
1836 sqlite3_reset(stmts
[STMT_INSERT_KEY
]);
1837 if (key
->rendered
!= key
->key
)
1838 free(key
->rendered
);
1846 struct mpage
*mpage
;
1847 struct mlink
*mlink
;
1852 SQL_EXEC("BEGIN TRANSACTION");
1854 for (mpage
= ohash_first(&mpages
, &slot
); NULL
!= mpage
;
1855 mpage
= ohash_next(&mpages
, &slot
)) {
1856 mlink
= mpage
->mlinks
;
1858 say(mlink
->file
, "Deleting from database");
1861 for ( ; NULL
!= mlink
; mlink
= mlink
->next
) {
1863 SQL_BIND_TEXT(stmts
[STMT_DELETE_PAGE
],
1865 SQL_BIND_TEXT(stmts
[STMT_DELETE_PAGE
],
1867 SQL_BIND_TEXT(stmts
[STMT_DELETE_PAGE
],
1869 SQL_STEP(stmts
[STMT_DELETE_PAGE
]);
1870 sqlite3_reset(stmts
[STMT_DELETE_PAGE
]);
1875 SQL_EXEC("END TRANSACTION");
1879 * Close an existing database and its prepared statements.
1880 * If "real" is not set, rename the temporary file into the real one.
1892 for (i
= 0; i
< STMT__MAX
; i
++) {
1893 sqlite3_finalize(stmts
[i
]);
1903 if ('\0' == *tempfilename
) {
1904 if (-1 == rename(MANDOC_DB
"~", MANDOC_DB
)) {
1905 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1906 say(MANDOC_DB
, "%s", strerror(errno
));
1911 switch (child
= fork()) {
1913 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1914 say("fork cmp", "%s", strerror(errno
));
1917 execlp("cmp", "cmp", "-s",
1918 tempfilename
, MANDOC_DB
, NULL
);
1919 say("exec cmp", "%s", strerror(errno
));
1924 if (-1 == waitpid(child
, &status
, 0)) {
1925 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1926 say("wait cmp", "%s", strerror(errno
));
1927 } else if (WIFSIGNALED(status
)) {
1928 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1929 say("cmp", "Died from a signal");
1930 } else if (WEXITSTATUS(status
)) {
1931 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1933 "Data changed, but cannot replace database");
1936 *strrchr(tempfilename
, '/') = '\0';
1937 switch (child
= fork()) {
1939 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1940 say("fork rm", "%s", strerror(errno
));
1943 execlp("rm", "rm", "-rf", tempfilename
, NULL
);
1944 say("exec rm", "%s", strerror(errno
));
1945 exit((int)MANDOCLEVEL_SYSERR
);
1949 if (-1 == waitpid(child
, &status
, 0)) {
1950 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1951 say("wait rm", "%s", strerror(errno
));
1952 } else if (WIFSIGNALED(status
) || WEXITSTATUS(status
)) {
1953 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1955 "Cannot remove temporary directory");
1960 * This is straightforward stuff.
1961 * Open a database connection to a "temporary" database, then open a set
1962 * of prepared statements we'll use over and over again.
1963 * If "real" is set, we use the existing database; if not, we truncate a
1965 * Must be matched by dbclose().
1976 *tempfilename
= '\0';
1977 ofl
= SQLITE_OPEN_READWRITE
;
1980 rc
= sqlite3_open_v2(MANDOC_DB
, &db
, ofl
, NULL
);
1981 if (SQLITE_OK
!= rc
) {
1982 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1983 say(MANDOC_DB
, "%s", sqlite3_errmsg(db
));
1986 goto prepare_statements
;
1989 ofl
|= SQLITE_OPEN_CREATE
| SQLITE_OPEN_EXCLUSIVE
;
1991 remove(MANDOC_DB
"~");
1992 rc
= sqlite3_open_v2(MANDOC_DB
"~", &db
, ofl
, NULL
);
1993 if (SQLITE_OK
== rc
)
1995 if (MPARSE_QUICK
& mparse_options
) {
1996 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1997 say(MANDOC_DB
"~", "%s", sqlite3_errmsg(db
));
2001 if (strlcpy(tempfilename
, "/tmp/mandocdb.XXXXXX",
2002 sizeof(tempfilename
)) >= sizeof(tempfilename
)) {
2003 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2004 say("/tmp/mandocdb.XXXXXX", "Filename too long");
2007 if (NULL
== mkdtemp(tempfilename
)) {
2008 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2009 say(tempfilename
, "%s", strerror(errno
));
2012 if (strlcat(tempfilename
, "/" MANDOC_DB
,
2013 sizeof(tempfilename
)) >= sizeof(tempfilename
)) {
2014 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2015 say(tempfilename
, "Filename too long");
2018 rc
= sqlite3_open_v2(tempfilename
, &db
, ofl
, NULL
);
2019 if (SQLITE_OK
!= rc
) {
2020 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2021 say(tempfilename
, "%s", sqlite3_errmsg(db
));
2026 sql
= "CREATE TABLE \"mpages\" (\n"
2027 " \"form\" INTEGER NOT NULL,\n"
2028 " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
2031 "CREATE TABLE \"mlinks\" (\n"
2032 " \"sec\" TEXT NOT NULL,\n"
2033 " \"arch\" TEXT NOT NULL,\n"
2034 " \"name\" TEXT NOT NULL,\n"
2035 " \"pageid\" INTEGER NOT NULL REFERENCES mpages(id) "
2036 "ON DELETE CASCADE\n"
2039 "CREATE TABLE \"keys\" (\n"
2040 " \"bits\" INTEGER NOT NULL,\n"
2041 " \"key\" TEXT NOT NULL,\n"
2042 " \"pageid\" INTEGER NOT NULL REFERENCES mpages(id) "
2043 "ON DELETE CASCADE\n"
2046 if (SQLITE_OK
!= sqlite3_exec(db
, sql
, NULL
, NULL
, NULL
)) {
2047 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2048 say(MANDOC_DB
, "%s", sqlite3_errmsg(db
));
2053 SQL_EXEC("PRAGMA foreign_keys = ON");
2054 sql
= "DELETE FROM mpages WHERE id IN "
2055 "(SELECT pageid FROM mlinks WHERE "
2056 "sec=? AND arch=? AND name=?)";
2057 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_DELETE_PAGE
], NULL
);
2058 sql
= "INSERT INTO mpages "
2059 "(form) VALUES (?)";
2060 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_PAGE
], NULL
);
2061 sql
= "INSERT INTO mlinks "
2062 "(sec,arch,name,pageid) VALUES (?,?,?,?)";
2063 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_LINK
], NULL
);
2064 sql
= "INSERT INTO keys "
2065 "(bits,key,pageid) VALUES (?,?,?)";
2066 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_KEY
], NULL
);
2070 * When opening a new database, we can turn off
2071 * synchronous mode for much better performance.
2075 SQL_EXEC("PRAGMA synchronous = OFF");
2082 hash_halloc(size_t sz
, void *arg
)
2085 return(mandoc_calloc(sz
, 1));
2089 hash_alloc(size_t sz
, void *arg
)
2092 return(mandoc_malloc(sz
));
2096 hash_free(void *p
, size_t sz
, void *arg
)
2103 set_basedir(const char *targetdir
)
2105 static char startdir
[PATH_MAX
];
2109 * Remember where we started by keeping a fd open to the origin
2110 * path component: throughout this utility, we chdir() a lot to
2111 * handle relative paths, and by doing this, we can return to
2112 * the starting point.
2114 if ('\0' == *startdir
) {
2115 if (NULL
== getcwd(startdir
, PATH_MAX
)) {
2116 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2117 if (NULL
!= targetdir
)
2121 if (-1 == (fd
= open(startdir
, O_RDONLY
, 0))) {
2122 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2123 say(startdir
, NULL
);
2126 if (NULL
== targetdir
)
2127 targetdir
= startdir
;
2131 if (-1 == fchdir(fd
)) {
2134 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2135 say(startdir
, NULL
);
2138 if (NULL
== targetdir
) {
2143 if (NULL
== realpath(targetdir
, basedir
)) {
2145 exitcode
= (int)MANDOCLEVEL_BADARG
;
2146 say(targetdir
, NULL
);
2148 } else if (-1 == chdir(basedir
)) {
2149 exitcode
= (int)MANDOCLEVEL_BADARG
;
2157 say(const char *file
, const char *format
, ...)
2161 if ('\0' != *basedir
)
2162 fprintf(stderr
, "%s", basedir
);
2163 if ('\0' != *basedir
&& '\0' != *file
)
2164 fputs("//", stderr
);
2166 fprintf(stderr
, "%s", file
);
2167 fputs(": ", stderr
);
2169 if (NULL
== format
) {
2174 va_start(ap
, format
);
2175 vfprintf(stderr
, format
, ap
);
2178 fputc('\n', stderr
);