]>
git.cameronkatri.com Git - mandoc.git/blob - mandocdb.c
1 /* $Id: mandocdb.c,v 1.76 2013/10/27 17:01:18 schwarze Exp $ */
3 * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011, 2012, 2013 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_SRC
, /* format is -man or -mdoc */
80 FORM_CAT
, /* format is cat */
81 FORM_NONE
/* format is unknown */
85 char *utf8
; /* key in UTF-8 form */
86 const struct of
*of
; /* if set, the owning parse */
87 uint64_t mask
; /* bitmask in sequence */
88 char key
[]; /* the string itself */
97 struct id id
; /* used for hashing routine */
98 struct of
*next
; /* next in ofs */
99 enum form dform
; /* path-cued form */
100 enum form sform
; /* suffix-cued form */
101 char file
[PATH_MAX
]; /* filename rel. to manpath */
102 char *desc
; /* parsed description */
103 char *name
; /* name (from filename) (not empty) */
104 char *sec
; /* suffix-cued section (or empty) */
105 char *dsec
; /* path-cued section (or empty) */
106 char *arch
; /* path-cued arch. (or empty) */
110 char *title
; /* name(sec/arch) given inside the file */
111 char *file
; /* file name in case of mismatch */
115 STMT_DELETE
= 0, /* delete manpage */
116 STMT_INSERT_DOC
, /* insert manpage */
117 STMT_INSERT_KEY
, /* insert parsed key */
121 typedef int (*mdoc_fp
)(struct of
*, const struct mdoc_node
*);
123 struct mdoc_handler
{
124 mdoc_fp fp
; /* optional handler */
125 uint64_t mask
; /* set unless handler returns 0 */
128 static void dbclose(int);
129 static void dbindex(struct mchars
*, int, const struct of
*);
130 static int dbopen(int);
131 static void dbprune(void);
132 static void fileadd(struct of
*);
133 static int filecheck(const char *);
134 static void filescan(const char *);
135 static void *hash_alloc(size_t, void *);
136 static void hash_free(void *, size_t, void *);
137 static void *hash_halloc(size_t, void *);
138 static void inoadd(const struct stat
*, struct of
*);
139 static int inocheck(const struct stat
*);
140 static void ofadd(int, const char *, const char *, const char *,
141 const char *, const char *, const struct stat
*);
142 static void offree(void);
143 static void ofmerge(struct mchars
*, struct mparse
*, int);
144 static void parse_catpage(struct of
*);
145 static void parse_man(struct of
*, const struct man_node
*);
146 static void parse_mdoc(struct of
*, const struct mdoc_node
*);
147 static int parse_mdoc_body(struct of
*, const struct mdoc_node
*);
148 static int parse_mdoc_head(struct of
*, const struct mdoc_node
*);
149 static int parse_mdoc_Fd(struct of
*, const struct mdoc_node
*);
150 static int parse_mdoc_Fn(struct of
*, const struct mdoc_node
*);
151 static int parse_mdoc_In(struct of
*, const struct mdoc_node
*);
152 static int parse_mdoc_Nd(struct of
*, const struct mdoc_node
*);
153 static int parse_mdoc_Nm(struct of
*, const struct mdoc_node
*);
154 static int parse_mdoc_Sh(struct of
*, const struct mdoc_node
*);
155 static int parse_mdoc_St(struct of
*, const struct mdoc_node
*);
156 static int parse_mdoc_Xr(struct of
*, const struct mdoc_node
*);
157 static int set_basedir(const char *);
158 static void putkey(const struct of
*,
159 const char *, uint64_t);
160 static void putkeys(const struct of
*,
161 const char *, size_t, uint64_t);
162 static void putmdockey(const struct of
*,
163 const struct mdoc_node
*, uint64_t);
164 static void say(const char *, const char *, ...);
165 static int treescan(void);
166 static size_t utf8(unsigned int, char [7]);
167 static void utf8key(struct mchars
*, struct str
*);
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 exitcode
; /* to be returned by main */
175 static enum op op
; /* operational mode */
176 static char basedir
[PATH_MAX
]; /* current base directory */
177 static struct ohash inos
; /* table of inodes/devices */
178 static struct ohash filenames
; /* table of filenames */
179 static struct ohash strings
; /* table of all strings */
180 static struct of
*ofs
= NULL
; /* vector of files to parse */
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 { parse_mdoc_In
, 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 { parse_mdoc_St
, 0 }, /* 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 ino_info
, filename_info
;
320 memset(stmts
, 0, STMT__MAX
* sizeof(sqlite3_stmt
*));
321 memset(&dirs
, 0, sizeof(struct manpaths
));
323 ino_info
.alloc
= filename_info
.alloc
= hash_alloc
;
324 ino_info
.halloc
= filename_info
.halloc
= hash_halloc
;
325 ino_info
.hfree
= filename_info
.hfree
= hash_free
;
327 ino_info
.key_offset
= offsetof(struct of
, id
);
328 filename_info
.key_offset
= offsetof(struct of
, 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:ntu:vW")))
370 dup2(STDOUT_FILENO
, STDERR_FILENO
);
392 if (OP_CONFFILE
== op
&& argc
> 0) {
393 fprintf(stderr
, "-C: Too many arguments\n");
397 exitcode
= (int)MANDOCLEVEL_OK
;
398 mp
= mparse_alloc(MPARSE_AUTO
,
399 MANDOCLEVEL_FATAL
, NULL
, NULL
, NULL
);
402 ohash_init(&inos
, 6, &ino_info
);
403 ohash_init(&filenames
, 6, &filename_info
);
405 if (OP_UPDATE
== op
|| OP_DELETE
== op
|| OP_TEST
== op
) {
407 * Force processing all files.
412 * All of these deal with a specific directory.
413 * Jump into that directory then collect files specified
414 * on the command-line.
416 if (0 == set_basedir(path_arg
))
418 for (i
= 0; i
< argc
; i
++)
429 * If we have arguments, use them as our manpaths.
430 * If we don't, grok from manpath(1) or however else
431 * manpath_parse() wants to do it.
434 dirs
.paths
= mandoc_calloc
435 (argc
, sizeof(char *));
436 dirs
.sz
= (size_t)argc
;
437 for (i
= 0; i
< argc
; i
++)
438 dirs
.paths
[i
] = mandoc_strdup(argv
[i
]);
440 manpath_parse(&dirs
, path_arg
, NULL
, NULL
);
443 * First scan the tree rooted at a base directory, then
444 * build a new database and finally move it into place.
445 * Ignore zero-length directories and strip trailing
448 for (j
= 0; j
< dirs
.sz
; j
++) {
449 sz
= strlen(dirs
.paths
[j
]);
450 if (sz
&& '/' == dirs
.paths
[j
][sz
- 1])
451 dirs
.paths
[j
][--sz
] = '\0';
456 ohash_init(&inos
, 6, &ino_info
);
457 ohash_init(&filenames
, 6, &filename_info
);
460 if (0 == set_basedir(dirs
.paths
[j
]))
464 if (0 == set_basedir(dirs
.paths
[j
]))
469 ofmerge(mc
, mp
, warnings
&& !use_all
);
472 if (j
+ 1 < dirs
.sz
) {
474 ohash_delete(&filenames
);
485 ohash_delete(&filenames
);
489 fprintf(stderr
, "usage: %s [-anvW] [-C file]\n"
490 " %s [-anvW] dir ...\n"
491 " %s [-nvW] -d dir [file ...]\n"
492 " %s [-nvW] -u dir [file ...]\n"
494 progname
, progname
, progname
,
497 return((int)MANDOCLEVEL_BADARG
);
501 * Scan a directory tree rooted at "basedir" for manpages.
502 * We use fts(), scanning directory parts along the way for clues to our
503 * section and architecture.
505 * If use_all has been specified, grok all files.
506 * If not, sanitise paths to the following:
508 * [./]man*[/<arch>]/<name>.<section>
510 * [./]cat<section>[/<arch>]/<name>.0
512 * TODO: accomodate for multi-language directories.
521 const char *dsec
, *arch
, *cp
, *path
;
525 argv
[1] = (char *)NULL
;
528 * Walk through all components under the directory, using the
529 * logical descent of files.
531 f
= fts_open((char * const *)argv
, FTS_LOGICAL
, NULL
);
533 exitcode
= (int)MANDOCLEVEL_SYSERR
;
541 while (NULL
!= (ff
= fts_read(f
))) {
542 path
= ff
->fts_path
+ 2;
544 * If we're a regular file, add an "of" by using the
545 * stored directory data and handling the filename.
546 * Disallow duplicate (hard-linked) files.
548 if (FTS_F
== ff
->fts_info
) {
549 if (0 == strcmp(path
, MANDOC_DB
))
551 if ( ! use_all
&& ff
->fts_level
< 2) {
553 say(path
, "Extraneous file");
555 } else if (inocheck(ff
->fts_statp
)) {
557 say(path
, "Duplicate file");
559 } else if (NULL
== (sec
=
560 strrchr(ff
->fts_name
, '.'))) {
564 "No filename suffix");
567 } else if (0 == strcmp(++sec
, "html")) {
569 say(path
, "Skip html");
571 } else if (0 == strcmp(sec
, "gz")) {
573 say(path
, "Skip gz");
575 } else if (0 == strcmp(sec
, "ps")) {
577 say(path
, "Skip ps");
579 } else if (0 == strcmp(sec
, "pdf")) {
581 say(path
, "Skip pdf");
583 } else if ( ! use_all
&&
584 ((FORM_SRC
== dform
&& strcmp(sec
, dsec
)) ||
585 (FORM_CAT
== dform
&& strcmp(sec
, "0")))) {
587 say(path
, "Wrong filename suffix");
591 ofadd(dform
, path
, ff
->fts_name
, dsec
, sec
,
592 arch
, ff
->fts_statp
);
594 } else if (FTS_D
!= ff
->fts_info
&&
595 FTS_DP
!= ff
->fts_info
) {
597 say(path
, "Not a regular file");
601 switch (ff
->fts_level
) {
603 /* Ignore the root directory. */
607 * This might contain manX/ or catX/.
608 * Try to infer this from the name.
609 * If we're not in use_all, enforce it.
614 if (FTS_DP
== ff
->fts_info
)
617 if (0 == strncmp(cp
, "man", 3)) {
620 } else if (0 == strncmp(cp
, "cat", 3)) {
625 if (NULL
!= dsec
|| use_all
)
629 say(path
, "Unknown directory part");
630 fts_set(f
, ff
, FTS_SKIP
);
634 * Possibly our architecture.
635 * If we're descending, keep tabs on it.
638 if (FTS_DP
!= ff
->fts_info
&& NULL
!= dsec
)
642 if (FTS_DP
== ff
->fts_info
|| use_all
)
645 say(path
, "Extraneous directory part");
646 fts_set(f
, ff
, FTS_SKIP
);
656 * Add a file to the file vector.
657 * Do not verify that it's a "valid" looking manpage (we'll do that
660 * Try to infer the manual section, architecture, and page name from the
661 * path, assuming it looks like
663 * [./]man*[/<arch>]/<name>.<section>
665 * [./]cat<section>[/<arch>]/<name>.0
667 * Stuff this information directly into the "of" vector.
668 * See treescan() for the fts(3) version of this.
671 filescan(const char *file
)
674 const char *sec
, *arch
, *name
, *dsec
;
681 if (0 == strncmp(file
, "./", 2))
684 if (NULL
== realpath(file
, buf
)) {
685 exitcode
= (int)MANDOCLEVEL_BADARG
;
688 } else if (OP_TEST
!= op
&& strstr(buf
, basedir
) != buf
) {
689 exitcode
= (int)MANDOCLEVEL_BADARG
;
690 say("", "%s: outside base directory", buf
);
692 } else if (-1 == stat(buf
, &st
)) {
693 exitcode
= (int)MANDOCLEVEL_BADARG
;
696 } else if ( ! (S_IFREG
& st
.st_mode
)) {
697 exitcode
= (int)MANDOCLEVEL_BADARG
;
698 say(file
, "Not a regular file");
700 } else if (inocheck(&st
)) {
702 say(file
, "Duplicate file");
705 start
= buf
+ strlen(basedir
);
706 sec
= arch
= name
= dsec
= NULL
;
710 * First try to guess our directory structure.
711 * If we find a separator, try to look for man* or cat*.
712 * If we find one of these and what's underneath is a directory,
713 * assume it's an architecture.
715 if (NULL
!= (p
= strchr(start
, '/'))) {
717 if (0 == strncmp(start
, "man", 3)) {
720 } else if (0 == strncmp(start
, "cat", 3)) {
726 if (NULL
!= dsec
&& NULL
!= (p
= strchr(start
, '/'))) {
734 * Now check the file suffix.
735 * Suffix of `.0' indicates a catpage, `.1-9' is a manpage.
737 p
= strrchr(start
, '\0');
738 while (p
-- > start
&& '/' != *p
&& '.' != *p
)
747 * Now try to parse the name.
748 * Use the filename portion of the path.
751 if (NULL
!= (p
= strrchr(start
, '/'))) {
756 ofadd(dform
, file
, name
, dsec
, sec
, arch
, &st
);
763 filecheck(const char *name
)
766 return(NULL
!= ohash_find(&filenames
,
767 ohash_qlookup(&filenames
, name
)));
771 * Use the standard hashing mechanism (K&R) to see if the given filename
775 fileadd(struct of
*of
)
779 slot
= ohash_qlookup(&filenames
, of
->file
);
780 assert(NULL
== ohash_find(&filenames
, slot
));
781 ohash_insert(&filenames
, slot
, of
);
788 inocheck(const struct stat
*st
)
793 memset(&id
, 0, sizeof(id
));
794 id
.ino
= hash
= st
->st_ino
;
797 return(NULL
!= ohash_find(&inos
, ohash_lookup_memory(
798 &inos
, (char *)&id
, sizeof(id
), hash
)));
802 * The hashing function used here is quite simple: simply take the inode
803 * and use uint32_t of its bits.
804 * Then when we do the lookup, use both the inode and device identifier.
807 inoadd(const struct stat
*st
, struct of
*of
)
812 of
->id
.ino
= hash
= st
->st_ino
;
813 of
->id
.dev
= st
->st_dev
;
814 slot
= ohash_lookup_memory
815 (&inos
, (char *)&of
->id
, sizeof(of
->id
), hash
);
817 assert(NULL
== ohash_find(&inos
, slot
));
818 ohash_insert(&inos
, slot
, of
);
822 ofadd(int dform
, const char *file
, const char *name
, const char *dsec
,
823 const char *sec
, const char *arch
, const struct stat
*st
)
828 assert(NULL
!= file
);
842 } else if ('1' <= *sec
&& '9' >= *sec
)
847 of
= mandoc_calloc(1, sizeof(struct of
));
848 strlcpy(of
->file
, file
, PATH_MAX
);
849 of
->name
= mandoc_strdup(name
);
850 of
->sec
= mandoc_strdup(sec
);
851 of
->dsec
= mandoc_strdup(dsec
);
852 of
->arch
= mandoc_strdup(arch
);
859 * Add to unique identifier hash.
860 * Then if it's a source manual and we're going to use source in
861 * favour of catpages, add it to that hash.
872 while (NULL
!= (of
= ofs
)) {
883 * Run through the files in the global vector "ofs" and add them to the
884 * database specified in "basedir".
886 * This handles the parsing scheme itself, using the cues of directory
887 * and filename to determine whether the file is parsable or not.
890 ofmerge(struct mchars
*mc
, struct mparse
*mp
, int check_reachable
)
892 struct ohash title_table
;
893 struct ohash_info title_info
, str_info
;
898 struct title
*title_entry
;
899 char *bufp
, *title_str
;
900 const char *msec
, *march
, *mtitle
, *cp
;
905 enum mandoclevel lvl
;
907 str_info
.alloc
= hash_alloc
;
908 str_info
.halloc
= hash_halloc
;
909 str_info
.hfree
= hash_free
;
910 str_info
.key_offset
= offsetof(struct str
, key
);
912 if (check_reachable
) {
913 title_info
.alloc
= hash_alloc
;
914 title_info
.halloc
= hash_halloc
;
915 title_info
.hfree
= hash_free
;
916 title_info
.key_offset
= offsetof(struct title
, title
);
917 ohash_init(&title_table
, 6, &title_info
);
920 for (of
= ofs
; NULL
!= of
; of
= of
->next
) {
922 * If we're a catpage (as defined by our path), then see
923 * if a manpage exists by the same name (ignoring the
925 * If it does, then we want to use it instead of our
928 if ( ! use_all
&& FORM_CAT
== of
->dform
) {
929 sz
= strlcpy(buf
, of
->file
, PATH_MAX
);
930 if (sz
>= PATH_MAX
) {
932 say(of
->file
, "Filename too long");
935 bufp
= strstr(buf
, "cat");
936 assert(NULL
!= bufp
);
937 memcpy(bufp
, "man", 3);
938 if (NULL
!= (bufp
= strrchr(buf
, '.')))
940 strlcat(buf
, of
->dsec
, PATH_MAX
);
941 if (filecheck(buf
)) {
944 "source exists: %s", buf
);
949 ohash_init(&strings
, 6, &str_info
);
956 * Try interpreting the file as mdoc(7) or man(7)
957 * source code, unless it is already known to be
958 * formatted. Fall back to formatted mode.
960 if (FORM_CAT
!= of
->dform
|| FORM_CAT
!= of
->sform
) {
961 lvl
= mparse_readfd(mp
, -1, of
->file
);
962 if (lvl
< MANDOCLEVEL_FATAL
)
963 mparse_result(mp
, &mdoc
, &man
);
968 msec
= mdoc_meta(mdoc
)->msec
;
969 march
= mdoc_meta(mdoc
)->arch
;
970 mtitle
= mdoc_meta(mdoc
)->title
;
971 } else if (NULL
!= man
) {
973 msec
= man_meta(man
)->msec
;
975 mtitle
= man_meta(man
)->title
;
991 * Check whether the manual section given in a file
992 * agrees with the directory where the file is located.
993 * Some manuals have suffixes like (3p) on their
994 * section number either inside the file or in the
995 * directory name, some are linked into more than one
996 * section, like encrypt(1) = makekey(8). Do not skip
997 * manuals for such reasons.
999 if (warnings
&& !use_all
&& form
&&
1000 strcasecmp(msec
, of
->dsec
)) {
1002 say(of
->file
, "Section \"%s\" "
1003 "manual in %s directory",
1008 * Manual page directories exist for each kernel
1009 * architecture as returned by machine(1).
1010 * However, many manuals only depend on the
1011 * application architecture as returned by arch(1).
1012 * For example, some (2/ARM) manuals are shared
1013 * across the "armish" and "zaurus" kernel
1015 * A few manuals are even shared across completely
1016 * different architectures, for example fdformat(1)
1017 * on amd64, i386, sparc, and sparc64.
1018 * Thus, warn about architecture mismatches,
1019 * but don't skip manuals for this reason.
1021 if (warnings
&& !use_all
&& strcasecmp(march
, of
->arch
)) {
1023 say(of
->file
, "Architecture \"%s\" "
1024 "manual in \"%s\" directory",
1027 if (warnings
&& !use_all
&& strcasecmp(mtitle
, of
->name
))
1030 putkey(of
, of
->name
, TYPE_Nm
);
1033 if (NULL
!= (cp
= mdoc_meta(mdoc
)->name
))
1034 putkey(of
, cp
, TYPE_Nm
);
1035 assert(NULL
== of
->desc
);
1036 parse_mdoc(of
, mdoc_node(mdoc
));
1037 putkey(of
, NULL
!= of
->desc
?
1038 of
->desc
: of
->name
, TYPE_Nd
);
1039 } else if (NULL
!= man
)
1040 parse_man(of
, man_node(man
));
1045 * Build a title string for the file. If it matches
1046 * the location of the file, remember the title as
1047 * found; else, remember it as missing.
1050 if (check_reachable
) {
1051 if (-1 == asprintf(&title_str
, "%s(%s%s%s)", mtitle
,
1052 msec
, '\0' == *march
? "" : "/", march
)) {
1054 exit((int)MANDOCLEVEL_SYSERR
);
1056 slot
= ohash_qlookup(&title_table
, title_str
);
1057 title_entry
= ohash_find(&title_table
, slot
);
1058 if (NULL
== title_entry
) {
1059 title_entry
= mandoc_malloc(
1060 sizeof(struct title
));
1061 title_entry
->title
= title_str
;
1062 title_entry
->file
= mandoc_strdup(
1063 match
? "" : of
->file
);
1064 ohash_insert(&title_table
, slot
,
1068 *title_entry
->file
= '\0';
1073 dbindex(mc
, form
, of
);
1074 ohash_delete(&strings
);
1077 if (check_reachable
) {
1078 title_entry
= ohash_first(&title_table
, &slot
);
1079 while (NULL
!= title_entry
) {
1080 if ('\0' != *title_entry
->file
)
1081 say(title_entry
->file
,
1082 "Probably unreachable, title is %s",
1083 title_entry
->title
);
1084 free(title_entry
->title
);
1085 free(title_entry
->file
);
1087 title_entry
= ohash_next(&title_table
, &slot
);
1089 ohash_delete(&title_table
);
1094 parse_catpage(struct of
*of
)
1097 char *line
, *p
, *title
;
1098 size_t len
, plen
, titlesz
;
1100 if (NULL
== (stream
= fopen(of
->file
, "r"))) {
1102 say(of
->file
, NULL
);
1106 /* Skip to first blank line. */
1108 while (NULL
!= (line
= fgetln(stream
, &len
)))
1113 * Assume the first line that is not indented
1114 * is the first section header. Skip to it.
1117 while (NULL
!= (line
= fgetln(stream
, &len
)))
1118 if ('\n' != *line
&& ' ' != *line
)
1122 * Read up until the next section into a buffer.
1123 * Strip the leading and trailing newline from each read line,
1124 * appending a trailing space.
1125 * Ignore empty (whitespace-only) lines.
1131 while (NULL
!= (line
= fgetln(stream
, &len
))) {
1132 if (' ' != *line
|| '\n' != line
[len
- 1])
1134 while (len
> 0 && isspace((unsigned char)*line
)) {
1140 title
= mandoc_realloc(title
, titlesz
+ len
);
1141 memcpy(title
+ titlesz
, line
, len
);
1143 title
[titlesz
- 1] = ' ';
1147 * If no page content can be found, or the input line
1148 * is already the next section header, or there is no
1149 * trailing newline, reuse the page title as the page
1153 if (NULL
== title
|| '\0' == *title
) {
1155 say(of
->file
, "Cannot find NAME section");
1156 assert(NULL
== of
->desc
);
1157 of
->desc
= mandoc_strdup(of
->name
);
1158 putkey(of
, of
->name
, TYPE_Nd
);
1164 title
= mandoc_realloc(title
, titlesz
+ 1);
1165 title
[titlesz
] = '\0';
1168 * Skip to the first dash.
1169 * Use the remaining line as the description (no more than 70
1173 if (NULL
!= (p
= strstr(title
, "- "))) {
1174 for (p
+= 2; ' ' == *p
|| '\b' == *p
; p
++)
1175 /* Skip to next word. */ ;
1178 say(of
->file
, "No dash in title line");
1184 /* Strip backspace-encoding from line. */
1186 while (NULL
!= (line
= memchr(p
, '\b', plen
))) {
1189 memmove(line
, line
+ 1, plen
--);
1192 memmove(line
- 1, line
+ 1, plen
- len
);
1196 assert(NULL
== of
->desc
);
1197 of
->desc
= mandoc_strdup(p
);
1198 putkey(of
, of
->desc
, TYPE_Nd
);
1204 * Put a type/word pair into the word database for this particular file.
1207 putkey(const struct of
*of
, const char *value
, uint64_t type
)
1210 assert(NULL
!= value
);
1211 putkeys(of
, value
, strlen(value
), type
);
1215 * Grok all nodes at or below a certain mdoc node into putkey().
1218 putmdockey(const struct of
*of
, const struct mdoc_node
*n
, uint64_t m
)
1221 for ( ; NULL
!= n
; n
= n
->next
) {
1222 if (NULL
!= n
->child
)
1223 putmdockey(of
, n
->child
, m
);
1224 if (MDOC_TEXT
== n
->type
)
1225 putkey(of
, n
->string
, m
);
1230 parse_man(struct of
*of
, const struct man_node
*n
)
1232 const struct man_node
*head
, *body
;
1233 char *start
, *sv
, *title
;
1241 * We're only searching for one thing: the first text child in
1242 * the BODY of a NAME section. Since we don't keep track of
1243 * sections in -man, run some hoops to find out whether we're in
1244 * the correct section or not.
1247 if (MAN_BODY
== n
->type
&& MAN_SH
== n
->tok
) {
1249 assert(body
->parent
);
1250 if (NULL
!= (head
= body
->parent
->head
) &&
1251 1 == head
->nchild
&&
1252 NULL
!= (head
= (head
->child
)) &&
1253 MAN_TEXT
== head
->type
&&
1254 0 == strcmp(head
->string
, "NAME") &&
1255 NULL
!= (body
= body
->child
) &&
1256 MAN_TEXT
== body
->type
) {
1262 * Suck the entire NAME section into memory.
1263 * Yes, we might run away.
1264 * But too many manuals have big, spread-out
1265 * NAME sections over many lines.
1268 for ( ; NULL
!= body
; body
= body
->next
) {
1269 if (MAN_TEXT
!= body
->type
)
1271 if (0 == (sz
= strlen(body
->string
)))
1273 title
= mandoc_realloc
1274 (title
, titlesz
+ sz
+ 1);
1275 memcpy(title
+ titlesz
, body
->string
, sz
);
1277 title
[titlesz
- 1] = ' ';
1282 title
= mandoc_realloc(title
, titlesz
+ 1);
1283 title
[titlesz
] = '\0';
1285 /* Skip leading space. */
1288 while (isspace((unsigned char)*sv
))
1291 if (0 == (sz
= strlen(sv
))) {
1296 /* Erase trailing space. */
1298 start
= &sv
[sz
- 1];
1299 while (start
> sv
&& isspace((unsigned char)*start
))
1310 * Go through a special heuristic dance here.
1311 * Conventionally, one or more manual names are
1312 * comma-specified prior to a whitespace, then a
1313 * dash, then a description. Try to puzzle out
1314 * the name parts here.
1318 sz
= strcspn(start
, " ,");
1319 if ('\0' == start
[sz
])
1325 putkey(of
, start
, TYPE_Nm
);
1332 assert(',' == byte
);
1334 while (' ' == *start
)
1339 putkey(of
, start
, TYPE_Nm
);
1344 while (isspace((unsigned char)*start
))
1347 if (0 == strncmp(start
, "-", 1))
1349 else if (0 == strncmp(start
, "\\-\\-", 4))
1351 else if (0 == strncmp(start
, "\\-", 2))
1353 else if (0 == strncmp(start
, "\\(en", 4))
1355 else if (0 == strncmp(start
, "\\(em", 4))
1358 while (' ' == *start
)
1361 assert(NULL
== of
->desc
);
1362 of
->desc
= mandoc_strdup(start
);
1363 putkey(of
, of
->desc
, TYPE_Nd
);
1369 for (n
= n
->child
; n
; n
= n
->next
)
1374 parse_mdoc(struct of
*of
, 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
)(of
, n
))
1392 if (mdocs
[n
->tok
].mask
)
1393 putmdockey(of
, n
->child
, mdocs
[n
->tok
].mask
);
1396 assert(MDOC_ROOT
!= n
->type
);
1399 if (NULL
!= n
->child
)
1405 parse_mdoc_Fd(struct of
*of
, const struct mdoc_node
*n
)
1407 const char *start
, *end
;
1410 if (SEC_SYNOPSIS
!= n
->sec
||
1411 NULL
== (n
= n
->child
) ||
1412 MDOC_TEXT
!= n
->type
)
1416 * Only consider those `Fd' macro fields that begin with an
1417 * "inclusion" token (versus, e.g., #define).
1420 if (strcmp("#include", n
->string
))
1423 if (NULL
== (n
= n
->next
) || MDOC_TEXT
!= n
->type
)
1427 * Strip away the enclosing angle brackets and make sure we're
1432 if ('<' == *start
|| '"' == *start
)
1435 if (0 == (sz
= strlen(start
)))
1438 end
= &start
[(int)sz
- 1];
1439 if ('>' == *end
|| '"' == *end
)
1443 putkeys(of
, start
, end
- start
+ 1, TYPE_In
);
1448 parse_mdoc_In(struct of
*of
, const struct mdoc_node
*n
)
1451 if (NULL
!= n
->child
&& MDOC_TEXT
== n
->child
->type
)
1454 putkey(of
, n
->child
->string
, TYPE_In
);
1459 parse_mdoc_Fn(struct of
*of
, const struct mdoc_node
*n
)
1463 if (NULL
== (n
= n
->child
) || MDOC_TEXT
!= n
->type
)
1467 * Parse: .Fn "struct type *name" "char *arg".
1468 * First strip away pointer symbol.
1469 * Then store the function name, then type.
1470 * Finally, store the arguments.
1473 if (NULL
== (cp
= strrchr(n
->string
, ' ')))
1479 putkey(of
, cp
, TYPE_Fn
);
1482 putkeys(of
, n
->string
, cp
- n
->string
, TYPE_Ft
);
1484 for (n
= n
->next
; NULL
!= n
; n
= n
->next
)
1485 if (MDOC_TEXT
== n
->type
)
1486 putkey(of
, n
->string
, TYPE_Fa
);
1492 parse_mdoc_St(struct of
*of
, const struct mdoc_node
*n
)
1495 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
1498 putkey(of
, n
->child
->string
, TYPE_St
);
1503 parse_mdoc_Xr(struct of
*of
, const struct mdoc_node
*n
)
1507 if (NULL
== (n
= n
->child
))
1510 if (NULL
== n
->next
) {
1511 putkey(of
, n
->string
, TYPE_Xr
);
1515 if (-1 == asprintf(&cp
, "%s(%s)", n
->string
, n
->next
->string
)) {
1517 exit((int)MANDOCLEVEL_SYSERR
);
1519 putkey(of
, cp
, TYPE_Xr
);
1525 parse_mdoc_Nd(struct of
*of
, const struct mdoc_node
*n
)
1529 if (MDOC_BODY
!= n
->type
)
1533 * Special-case the `Nd' because we need to put the description
1534 * into the document table.
1537 for (n
= n
->child
; NULL
!= n
; n
= n
->next
) {
1538 if (MDOC_TEXT
== n
->type
) {
1539 if (NULL
!= of
->desc
) {
1540 sz
= strlen(of
->desc
) +
1541 strlen(n
->string
) + 2;
1542 of
->desc
= mandoc_realloc(of
->desc
, sz
);
1543 strlcat(of
->desc
, " ", sz
);
1544 strlcat(of
->desc
, n
->string
, sz
);
1546 of
->desc
= mandoc_strdup(n
->string
);
1548 if (NULL
!= n
->child
)
1549 parse_mdoc_Nd(of
, n
);
1555 parse_mdoc_Nm(struct of
*of
, const struct mdoc_node
*n
)
1558 if (SEC_NAME
== n
->sec
)
1560 else if (SEC_SYNOPSIS
!= n
->sec
|| MDOC_HEAD
!= n
->type
)
1567 parse_mdoc_Sh(struct of
*of
, const struct mdoc_node
*n
)
1570 return(SEC_CUSTOM
== n
->sec
&& MDOC_HEAD
== n
->type
);
1574 parse_mdoc_head(struct of
*of
, const struct mdoc_node
*n
)
1577 return(MDOC_HEAD
== n
->type
);
1581 parse_mdoc_body(struct of
*of
, const struct mdoc_node
*n
)
1584 return(MDOC_BODY
== n
->type
);
1588 * Add a string to the hash table for the current manual.
1589 * Each string has a bitmask telling which macros it belongs to.
1590 * When we finish the manual, we'll dump the table.
1593 putkeys(const struct of
*of
, const char *cp
, size_t sz
, uint64_t v
)
1603 slot
= ohash_qlookupi(&strings
, cp
, &end
);
1604 s
= ohash_find(&strings
, slot
);
1606 if (NULL
!= s
&& of
== s
->of
) {
1609 } else if (NULL
== s
) {
1610 s
= mandoc_calloc(sizeof(struct str
) + sz
+ 1, 1);
1611 memcpy(s
->key
, cp
, sz
);
1612 ohash_insert(&strings
, slot
, s
);
1619 * Take a Unicode codepoint and produce its UTF-8 encoding.
1620 * This isn't the best way to do this, but it works.
1621 * The magic numbers are from the UTF-8 packaging.
1622 * They're not as scary as they seem: read the UTF-8 spec for details.
1625 utf8(unsigned int cp
, char out
[7])
1630 if (cp
<= 0x0000007F) {
1633 } else if (cp
<= 0x000007FF) {
1635 out
[0] = (cp
>> 6 & 31) | 192;
1636 out
[1] = (cp
& 63) | 128;
1637 } else if (cp
<= 0x0000FFFF) {
1639 out
[0] = (cp
>> 12 & 15) | 224;
1640 out
[1] = (cp
>> 6 & 63) | 128;
1641 out
[2] = (cp
& 63) | 128;
1642 } else if (cp
<= 0x001FFFFF) {
1644 out
[0] = (cp
>> 18 & 7) | 240;
1645 out
[1] = (cp
>> 12 & 63) | 128;
1646 out
[2] = (cp
>> 6 & 63) | 128;
1647 out
[3] = (cp
& 63) | 128;
1648 } else if (cp
<= 0x03FFFFFF) {
1650 out
[0] = (cp
>> 24 & 3) | 248;
1651 out
[1] = (cp
>> 18 & 63) | 128;
1652 out
[2] = (cp
>> 12 & 63) | 128;
1653 out
[3] = (cp
>> 6 & 63) | 128;
1654 out
[4] = (cp
& 63) | 128;
1655 } else if (cp
<= 0x7FFFFFFF) {
1657 out
[0] = (cp
>> 30 & 1) | 252;
1658 out
[1] = (cp
>> 24 & 63) | 128;
1659 out
[2] = (cp
>> 18 & 63) | 128;
1660 out
[3] = (cp
>> 12 & 63) | 128;
1661 out
[4] = (cp
>> 6 & 63) | 128;
1662 out
[5] = (cp
& 63) | 128;
1671 * Store the UTF-8 version of a key, or alias the pointer if the key has
1672 * no UTF-8 transcription marks in it.
1675 utf8key(struct mchars
*mc
, struct str
*key
)
1677 size_t sz
, bsz
, pos
;
1678 char utfbuf
[7], res
[5];
1680 const char *seq
, *cpp
, *val
;
1682 enum mandoc_esc esc
;
1684 assert(NULL
== key
->utf8
);
1688 res
[2] = ASCII_NBRSP
;
1689 res
[3] = ASCII_HYPH
;
1696 * Pre-check: if we have no stop-characters, then set the
1697 * pointer as ourselvse and get out of here.
1699 if (strcspn(val
, res
) == bsz
) {
1700 key
->utf8
= key
->key
;
1704 /* Pre-allocate by the length of the input */
1706 buf
= mandoc_malloc(++bsz
);
1709 while ('\0' != *val
) {
1711 * Halt on the first escape sequence.
1712 * This also halts on the end of string, in which case
1713 * we just copy, fallthrough, and exit the loop.
1715 if ((sz
= strcspn(val
, res
)) > 0) {
1716 memcpy(&buf
[pos
], val
, sz
);
1721 if (ASCII_HYPH
== *val
) {
1725 } else if ('\t' == *val
|| ASCII_NBRSP
== *val
) {
1729 } else if ('\\' != *val
)
1732 /* Read past the slash. */
1738 * Parse the escape sequence and see if it's a
1739 * predefined character or special character.
1742 ((const char **)&val
, &seq
, &len
);
1743 if (ESCAPE_ERROR
== esc
)
1746 if (ESCAPE_SPECIAL
!= esc
)
1748 if (0 == (u
= mchars_spec2cp(mc
, seq
, len
)))
1752 * If we have a Unicode codepoint, try to convert that
1753 * to a UTF-8 byte string.
1756 if (0 == (sz
= utf8(u
, utfbuf
)))
1759 /* Copy the rendered glyph into the stream. */
1764 buf
= mandoc_realloc(buf
, bsz
);
1766 memcpy(&buf
[pos
], cpp
, sz
);
1775 * Flush the current page's terms (and their bits) into the database.
1776 * Wrap the entire set of additions in a transaction to make sqlite be a
1778 * Also, UTF-8-encode the description at the last possible moment.
1781 dbindex(struct mchars
*mc
, int form
, const struct of
*of
)
1790 say(of
->file
, "Adding to index");
1796 if (NULL
!= of
->desc
&& '\0' != *of
->desc
) {
1797 key
= ohash_find(&strings
,
1798 ohash_qlookup(&strings
, of
->desc
));
1799 assert(NULL
!= key
);
1800 if (NULL
== key
->utf8
)
1805 SQL_EXEC("BEGIN TRANSACTION");
1808 SQL_BIND_TEXT(stmts
[STMT_INSERT_DOC
], i
, of
->file
);
1809 SQL_BIND_TEXT(stmts
[STMT_INSERT_DOC
], i
, of
->sec
);
1810 SQL_BIND_TEXT(stmts
[STMT_INSERT_DOC
], i
, of
->arch
);
1811 SQL_BIND_TEXT(stmts
[STMT_INSERT_DOC
], i
, desc
);
1812 SQL_BIND_INT(stmts
[STMT_INSERT_DOC
], i
, form
);
1813 SQL_STEP(stmts
[STMT_INSERT_DOC
]);
1814 recno
= sqlite3_last_insert_rowid(db
);
1815 sqlite3_reset(stmts
[STMT_INSERT_DOC
]);
1817 for (key
= ohash_first(&strings
, &slot
); NULL
!= key
;
1818 key
= ohash_next(&strings
, &slot
)) {
1819 assert(key
->of
== of
);
1820 if (NULL
== key
->utf8
)
1823 SQL_BIND_INT64(stmts
[STMT_INSERT_KEY
], i
, key
->mask
);
1824 SQL_BIND_TEXT(stmts
[STMT_INSERT_KEY
], i
, key
->utf8
);
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
->utf8
!= key
->key
)
1833 SQL_EXEC("END TRANSACTION");
1845 for (of
= ofs
; NULL
!= of
; of
= of
->next
) {
1847 SQL_BIND_TEXT(stmts
[STMT_DELETE
], i
, of
->file
);
1848 SQL_STEP(stmts
[STMT_DELETE
]);
1849 sqlite3_reset(stmts
[STMT_DELETE
]);
1851 say(of
->file
, "Deleted from index");
1856 * Close an existing database and its prepared statements.
1857 * If "real" is not set, rename the temporary file into the real one.
1867 for (i
= 0; i
< STMT__MAX
; i
++) {
1868 sqlite3_finalize(stmts
[i
]);
1878 if (-1 == rename(MANDOC_DB
"~", MANDOC_DB
)) {
1879 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1880 say(MANDOC_DB
, NULL
);
1885 * This is straightforward stuff.
1886 * Open a database connection to a "temporary" database, then open a set
1887 * of prepared statements we'll use over and over again.
1888 * If "real" is set, we use the existing database; if not, we truncate a
1890 * Must be matched by dbclose().
1895 const char *file
, *sql
;
1901 ofl
= SQLITE_OPEN_READWRITE
;
1903 file
= MANDOC_DB
"~";
1904 if (-1 == remove(file
) && ENOENT
!= errno
) {
1905 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1909 ofl
|= SQLITE_OPEN_EXCLUSIVE
;
1913 rc
= sqlite3_open_v2(file
, &db
, ofl
, NULL
);
1914 if (SQLITE_OK
== rc
)
1915 goto prepare_statements
;
1916 if (SQLITE_CANTOPEN
!= rc
) {
1917 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1925 if (SQLITE_OK
!= (rc
= sqlite3_open(file
, &db
))) {
1926 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1931 sql
= "CREATE TABLE \"docs\" (\n"
1932 " \"file\" TEXT NOT NULL,\n"
1933 " \"sec\" TEXT NOT NULL,\n"
1934 " \"arch\" TEXT NOT NULL,\n"
1935 " \"desc\" TEXT NOT NULL,\n"
1936 " \"form\" INTEGER NOT NULL,\n"
1937 " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
1940 "CREATE TABLE \"keys\" (\n"
1941 " \"bits\" INTEGER NOT NULL,\n"
1942 " \"key\" TEXT NOT NULL,\n"
1943 " \"docid\" INTEGER NOT NULL REFERENCES docs(id) "
1944 "ON DELETE CASCADE,\n"
1945 " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
1948 "CREATE INDEX \"key_index\" ON keys (key);\n";
1950 if (SQLITE_OK
!= sqlite3_exec(db
, sql
, NULL
, NULL
, NULL
)) {
1951 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1952 say(file
, "%s", sqlite3_errmsg(db
));
1957 SQL_EXEC("PRAGMA foreign_keys = ON");
1958 sql
= "DELETE FROM docs where file=?";
1959 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_DELETE
], NULL
);
1960 sql
= "INSERT INTO docs "
1961 "(file,sec,arch,desc,form) VALUES (?,?,?,?,?)";
1962 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_DOC
], NULL
);
1963 sql
= "INSERT INTO keys "
1964 "(bits,key,docid) VALUES (?,?,?)";
1965 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_KEY
], NULL
);
1969 * When opening a new database, we can turn off
1970 * synchronous mode for much better performance.
1974 SQL_EXEC("PRAGMA synchronous = OFF");
1981 hash_halloc(size_t sz
, void *arg
)
1984 return(mandoc_calloc(sz
, 1));
1988 hash_alloc(size_t sz
, void *arg
)
1991 return(mandoc_malloc(sz
));
1995 hash_free(void *p
, size_t sz
, void *arg
)
2002 set_basedir(const char *targetdir
)
2004 static char startdir
[PATH_MAX
];
2008 * Remember where we started by keeping a fd open to the origin
2009 * path component: throughout this utility, we chdir() a lot to
2010 * handle relative paths, and by doing this, we can return to
2011 * the starting point.
2013 if ('\0' == *startdir
) {
2014 if (NULL
== getcwd(startdir
, PATH_MAX
)) {
2015 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2016 if (NULL
!= targetdir
)
2020 if (-1 == (fd
= open(startdir
, O_RDONLY
, 0))) {
2021 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2022 say(startdir
, NULL
);
2025 if (NULL
== targetdir
)
2026 targetdir
= startdir
;
2030 if (-1 == fchdir(fd
)) {
2033 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2034 say(startdir
, NULL
);
2037 if (NULL
== targetdir
) {
2042 if (NULL
== realpath(targetdir
, basedir
)) {
2044 exitcode
= (int)MANDOCLEVEL_BADARG
;
2045 say(targetdir
, NULL
);
2047 } else if (-1 == chdir(basedir
)) {
2048 exitcode
= (int)MANDOCLEVEL_BADARG
;
2056 say(const char *file
, const char *format
, ...)
2060 if ('\0' != *basedir
)
2061 fprintf(stderr
, "%s", basedir
);
2062 if ('\0' != *basedir
&& '\0' != *file
)
2063 fputs("//", stderr
);
2065 fprintf(stderr
, "%s", file
);
2066 fputs(": ", stderr
);
2068 if (NULL
== format
) {
2073 va_start(ap
, format
);
2074 vfprintf(stderr
, format
, ap
);
2077 fputc('\n', stderr
);