]>
git.cameronkatri.com Git - mandoc.git/blob - mandocdb.c
1 /* $Id: mandocdb.c,v 1.65 2013/06/06 19:00:28 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 struct str
*next
; /* next in owning parse sequence */
88 uint64_t mask
; /* bitmask in sequence */
89 char key
[]; /* the string itself */
98 struct id id
; /* used for hashing routine */
99 struct of
*next
; /* next in ofs */
100 enum form dform
; /* path-cued form */
101 enum form sform
; /* suffix-cued form */
102 char file
[PATH_MAX
]; /* filename rel. to manpath */
103 const char *desc
; /* parsed description */
104 const char *sec
; /* suffix-cued section (or empty) */
105 const char *dsec
; /* path-cued section (or empty) */
106 const char *arch
; /* path-cued arch. (or empty) */
107 const char *name
; /* name (from filename) (not empty) */
111 STMT_DELETE
= 0, /* delete manpage */
112 STMT_INSERT_DOC
, /* insert manpage */
113 STMT_INSERT_KEY
, /* insert parsed key */
117 typedef int (*mdoc_fp
)(struct of
*, const struct mdoc_node
*);
119 struct mdoc_handler
{
120 mdoc_fp fp
; /* optional handler */
121 uint64_t mask
; /* set unless handler returns 0 */
122 int flags
; /* for use by pmdoc_node */
123 #define MDOCF_CHILD 0x01 /* automatically index child nodes */
126 static void dbclose(int);
127 static void dbindex(struct mchars
*, int, const struct of
*);
128 static int dbopen(int);
129 static void dbprune(void);
130 static void fileadd(struct of
*);
131 static int filecheck(const char *);
132 static void filescan(const char *);
133 static void *hash_alloc(size_t, void *);
134 static void hash_free(void *, size_t, void *);
135 static void *hash_halloc(size_t, void *);
136 static void inoadd(const struct stat
*, struct of
*);
137 static int inocheck(const struct stat
*);
138 static void ofadd(int, const char *, const char *, const char *,
139 const char *, const char *, const struct stat
*);
140 static void offree(void);
141 static void ofmerge(struct mchars
*, struct mparse
*);
142 static void parse_catpage(struct of
*);
143 static void parse_man(struct of
*, const struct man_node
*);
144 static void parse_mdoc(struct of
*, const struct mdoc_node
*);
145 static int parse_mdoc_body(struct of
*, const struct mdoc_node
*);
146 static int parse_mdoc_head(struct of
*, const struct mdoc_node
*);
147 static int parse_mdoc_Fd(struct of
*, const struct mdoc_node
*);
148 static int parse_mdoc_Fn(struct of
*, const struct mdoc_node
*);
149 static int parse_mdoc_In(struct of
*, const struct mdoc_node
*);
150 static int parse_mdoc_Nd(struct of
*, const struct mdoc_node
*);
151 static int parse_mdoc_Nm(struct of
*, const struct mdoc_node
*);
152 static int parse_mdoc_Sh(struct of
*, const struct mdoc_node
*);
153 static int parse_mdoc_St(struct of
*, const struct mdoc_node
*);
154 static int parse_mdoc_Xr(struct of
*, const struct mdoc_node
*);
155 static int set_basedir(const char *);
156 static void putkey(const struct of
*,
157 const char *, uint64_t);
158 static void putkeys(const struct of
*,
159 const char *, size_t, uint64_t);
160 static void putmdockey(const struct of
*,
161 const struct mdoc_node
*, uint64_t);
162 static void say(const char *, const char *, ...);
163 static char *stradd(const char *);
164 static char *stradds(const char *, size_t);
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 struct str
*words
= NULL
; /* word list in current parse */
182 static sqlite3
*db
= NULL
; /* current database */
183 static sqlite3_stmt
*stmts
[STMT__MAX
]; /* current statements */
185 static const struct mdoc_handler mdocs
[MDOC_MAX
] = {
186 { NULL
, 0, 0 }, /* Ap */
187 { NULL
, 0, 0 }, /* Dd */
188 { NULL
, 0, 0 }, /* Dt */
189 { NULL
, 0, 0 }, /* Os */
190 { parse_mdoc_Sh
, TYPE_Sh
, MDOCF_CHILD
}, /* Sh */
191 { parse_mdoc_head
, TYPE_Ss
, MDOCF_CHILD
}, /* Ss */
192 { NULL
, 0, 0 }, /* Pp */
193 { NULL
, 0, 0 }, /* D1 */
194 { NULL
, 0, 0 }, /* Dl */
195 { NULL
, 0, 0 }, /* Bd */
196 { NULL
, 0, 0 }, /* Ed */
197 { NULL
, 0, 0 }, /* Bl */
198 { NULL
, 0, 0 }, /* El */
199 { NULL
, 0, 0 }, /* It */
200 { NULL
, 0, 0 }, /* Ad */
201 { NULL
, TYPE_An
, MDOCF_CHILD
}, /* An */
202 { NULL
, TYPE_Ar
, MDOCF_CHILD
}, /* Ar */
203 { NULL
, TYPE_Cd
, MDOCF_CHILD
}, /* Cd */
204 { NULL
, TYPE_Cm
, MDOCF_CHILD
}, /* Cm */
205 { NULL
, TYPE_Dv
, MDOCF_CHILD
}, /* Dv */
206 { NULL
, TYPE_Er
, MDOCF_CHILD
}, /* Er */
207 { NULL
, TYPE_Ev
, MDOCF_CHILD
}, /* Ev */
208 { NULL
, 0, 0 }, /* Ex */
209 { NULL
, TYPE_Fa
, MDOCF_CHILD
}, /* Fa */
210 { parse_mdoc_Fd
, TYPE_In
, 0 }, /* Fd */
211 { NULL
, TYPE_Fl
, MDOCF_CHILD
}, /* Fl */
212 { parse_mdoc_Fn
, 0, 0 }, /* Fn */
213 { NULL
, TYPE_Ft
, MDOCF_CHILD
}, /* Ft */
214 { NULL
, TYPE_Ic
, MDOCF_CHILD
}, /* Ic */
215 { parse_mdoc_In
, TYPE_In
, MDOCF_CHILD
}, /* In */
216 { NULL
, TYPE_Li
, MDOCF_CHILD
}, /* Li */
217 { parse_mdoc_Nd
, TYPE_Nd
, MDOCF_CHILD
}, /* Nd */
218 { parse_mdoc_Nm
, TYPE_Nm
, MDOCF_CHILD
}, /* Nm */
219 { NULL
, 0, 0 }, /* Op */
220 { NULL
, 0, 0 }, /* Ot */
221 { NULL
, TYPE_Pa
, MDOCF_CHILD
}, /* Pa */
222 { NULL
, 0, 0 }, /* Rv */
223 { parse_mdoc_St
, TYPE_St
, 0 }, /* St */
224 { NULL
, TYPE_Va
, MDOCF_CHILD
}, /* Va */
225 { parse_mdoc_body
, TYPE_Va
, MDOCF_CHILD
}, /* Vt */
226 { parse_mdoc_Xr
, TYPE_Xr
, 0 }, /* Xr */
227 { NULL
, 0, 0 }, /* %A */
228 { NULL
, 0, 0 }, /* %B */
229 { NULL
, 0, 0 }, /* %D */
230 { NULL
, 0, 0 }, /* %I */
231 { NULL
, 0, 0 }, /* %J */
232 { NULL
, 0, 0 }, /* %N */
233 { NULL
, 0, 0 }, /* %O */
234 { NULL
, 0, 0 }, /* %P */
235 { NULL
, 0, 0 }, /* %R */
236 { NULL
, 0, 0 }, /* %T */
237 { NULL
, 0, 0 }, /* %V */
238 { NULL
, 0, 0 }, /* Ac */
239 { NULL
, 0, 0 }, /* Ao */
240 { NULL
, 0, 0 }, /* Aq */
241 { NULL
, TYPE_At
, MDOCF_CHILD
}, /* At */
242 { NULL
, 0, 0 }, /* Bc */
243 { NULL
, 0, 0 }, /* Bf */
244 { NULL
, 0, 0 }, /* Bo */
245 { NULL
, 0, 0 }, /* Bq */
246 { NULL
, TYPE_Bsx
, MDOCF_CHILD
}, /* Bsx */
247 { NULL
, TYPE_Bx
, MDOCF_CHILD
}, /* Bx */
248 { NULL
, 0, 0 }, /* Db */
249 { NULL
, 0, 0 }, /* Dc */
250 { NULL
, 0, 0 }, /* Do */
251 { NULL
, 0, 0 }, /* Dq */
252 { NULL
, 0, 0 }, /* Ec */
253 { NULL
, 0, 0 }, /* Ef */
254 { NULL
, TYPE_Em
, MDOCF_CHILD
}, /* Em */
255 { NULL
, 0, 0 }, /* Eo */
256 { NULL
, TYPE_Fx
, MDOCF_CHILD
}, /* Fx */
257 { NULL
, TYPE_Ms
, MDOCF_CHILD
}, /* Ms */
258 { NULL
, 0, 0 }, /* No */
259 { NULL
, 0, 0 }, /* Ns */
260 { NULL
, TYPE_Nx
, MDOCF_CHILD
}, /* Nx */
261 { NULL
, TYPE_Ox
, MDOCF_CHILD
}, /* Ox */
262 { NULL
, 0, 0 }, /* Pc */
263 { NULL
, 0, 0 }, /* Pf */
264 { NULL
, 0, 0 }, /* Po */
265 { NULL
, 0, 0 }, /* Pq */
266 { NULL
, 0, 0 }, /* Qc */
267 { NULL
, 0, 0 }, /* Ql */
268 { NULL
, 0, 0 }, /* Qo */
269 { NULL
, 0, 0 }, /* Qq */
270 { NULL
, 0, 0 }, /* Re */
271 { NULL
, 0, 0 }, /* Rs */
272 { NULL
, 0, 0 }, /* Sc */
273 { NULL
, 0, 0 }, /* So */
274 { NULL
, 0, 0 }, /* Sq */
275 { NULL
, 0, 0 }, /* Sm */
276 { NULL
, 0, 0 }, /* Sx */
277 { NULL
, TYPE_Sy
, MDOCF_CHILD
}, /* Sy */
278 { NULL
, TYPE_Tn
, MDOCF_CHILD
}, /* Tn */
279 { NULL
, 0, 0 }, /* Ux */
280 { NULL
, 0, 0 }, /* Xc */
281 { NULL
, 0, 0 }, /* Xo */
282 { parse_mdoc_head
, TYPE_Fn
, 0 }, /* Fo */
283 { NULL
, 0, 0 }, /* Fc */
284 { NULL
, 0, 0 }, /* Oo */
285 { NULL
, 0, 0 }, /* Oc */
286 { NULL
, 0, 0 }, /* Bk */
287 { NULL
, 0, 0 }, /* Ek */
288 { NULL
, 0, 0 }, /* Bt */
289 { NULL
, 0, 0 }, /* Hf */
290 { NULL
, 0, 0 }, /* Fr */
291 { NULL
, 0, 0 }, /* Ud */
292 { NULL
, TYPE_Lb
, MDOCF_CHILD
}, /* Lb */
293 { NULL
, 0, 0 }, /* Lp */
294 { NULL
, TYPE_Lk
, MDOCF_CHILD
}, /* Lk */
295 { NULL
, TYPE_Mt
, MDOCF_CHILD
}, /* Mt */
296 { NULL
, 0, 0 }, /* Brq */
297 { NULL
, 0, 0 }, /* Bro */
298 { NULL
, 0, 0 }, /* Brc */
299 { NULL
, 0, 0 }, /* %C */
300 { NULL
, 0, 0 }, /* Es */
301 { NULL
, 0, 0 }, /* En */
302 { NULL
, TYPE_Dx
, MDOCF_CHILD
}, /* Dx */
303 { NULL
, 0, 0 }, /* %Q */
304 { NULL
, 0, 0 }, /* br */
305 { NULL
, 0, 0 }, /* sp */
306 { NULL
, 0, 0 }, /* %U */
307 { NULL
, 0, 0 }, /* Ta */
311 main(int argc
, char *argv
[])
316 const char *path_arg
;
319 struct manpaths dirs
;
321 struct ohash_info ino_info
, filename_info
, str_info
;
323 memset(stmts
, 0, STMT__MAX
* sizeof(sqlite3_stmt
*));
324 memset(&dirs
, 0, sizeof(struct manpaths
));
326 ino_info
.halloc
= filename_info
.halloc
=
327 str_info
.halloc
= hash_halloc
;
328 ino_info
.hfree
= filename_info
.hfree
=
329 str_info
.hfree
= hash_free
;
330 ino_info
.alloc
= filename_info
.alloc
=
331 str_info
.alloc
= hash_alloc
;
333 ino_info
.key_offset
= offsetof(struct of
, id
);
334 filename_info
.key_offset
= offsetof(struct of
, file
);
335 str_info
.key_offset
= offsetof(struct str
, key
);
337 progname
= strrchr(argv
[0], '/');
338 if (progname
== NULL
)
344 * We accept a few different invocations.
345 * The CHECKOP macro makes sure that invocation styles don't
346 * clobber each other.
348 #define CHECKOP(_op, _ch) do \
349 if (OP_DEFAULT != (_op)) { \
350 fprintf(stderr, "-%c: Conflicting option\n", (_ch)); \
352 } while (/*CONSTCOND*/0)
357 while (-1 != (ch
= getopt(argc
, argv
, "aC:d:ntu:vW")))
377 dup2(STDOUT_FILENO
, STDERR_FILENO
);
399 if (OP_CONFFILE
== op
&& argc
> 0) {
400 fprintf(stderr
, "-C: Too many arguments\n");
404 exitcode
= (int)MANDOCLEVEL_OK
;
405 mp
= mparse_alloc(MPARSE_AUTO
,
406 MANDOCLEVEL_FATAL
, NULL
, NULL
, NULL
);
409 ohash_init(&strings
, 6, &str_info
);
410 ohash_init(&inos
, 6, &ino_info
);
411 ohash_init(&filenames
, 6, &filename_info
);
413 if (OP_UPDATE
== op
|| OP_DELETE
== op
|| OP_TEST
== op
) {
415 * Force processing all files.
420 * All of these deal with a specific directory.
421 * Jump into that directory then collect files specified
422 * on the command-line.
424 if (0 == set_basedir(path_arg
))
426 for (i
= 0; i
< argc
; i
++)
437 * If we have arguments, use them as our manpaths.
438 * If we don't, grok from manpath(1) or however else
439 * manpath_parse() wants to do it.
442 dirs
.paths
= mandoc_calloc
443 (argc
, sizeof(char *));
444 dirs
.sz
= (size_t)argc
;
445 for (i
= 0; i
< argc
; i
++)
446 dirs
.paths
[i
] = mandoc_strdup(argv
[i
]);
448 manpath_parse(&dirs
, path_arg
, NULL
, NULL
);
451 * First scan the tree rooted at a base directory.
452 * Then whak its database (if one exists), parse, and
453 * build up the database.
454 * Ignore zero-length directories and strip trailing
457 for (j
= 0; j
< dirs
.sz
; j
++) {
458 sz
= strlen(dirs
.paths
[j
]);
459 if (sz
&& '/' == dirs
.paths
[j
][sz
- 1])
460 dirs
.paths
[j
][--sz
] = '\0';
463 if (0 == set_basedir(dirs
.paths
[j
]))
467 if (0 == set_basedir(dirs
.paths
[j
]))
473 * Since we're opening up a new database, we can
474 * turn off synchronous mode for much better
478 SQL_EXEC("PRAGMA synchronous = OFF");
485 ohash_init(&inos
, 6, &ino_info
);
486 ohash_delete(&filenames
);
487 ohash_init(&filenames
, 6, &filename_info
);
495 for (s
= ohash_first(&strings
, &slot
); NULL
!= s
;
496 s
= ohash_next(&strings
, &slot
)) {
497 if (s
->utf8
!= s
->key
)
501 ohash_delete(&strings
);
503 ohash_delete(&filenames
);
507 fprintf(stderr
, "usage: %s [-anvW] [-C file]\n"
508 " %s [-anvW] dir ...\n"
509 " %s [-nvW] -d dir [file ...]\n"
510 " %s [-nvW] -u dir [file ...]\n"
512 progname
, progname
, progname
,
515 return((int)MANDOCLEVEL_BADARG
);
519 * Scan a directory tree rooted at "basedir" for manpages.
520 * We use fts(), scanning directory parts along the way for clues to our
521 * section and architecture.
523 * If use_all has been specified, grok all files.
524 * If not, sanitise paths to the following:
526 * [./]man*[/<arch>]/<name>.<section>
528 * [./]cat<section>[/<arch>]/<name>.0
530 * TODO: accomodate for multi-language directories.
539 const char *dsec
, *arch
, *cp
, *name
, *path
;
543 argv
[1] = (char *)NULL
;
546 * Walk through all components under the directory, using the
547 * logical descent of files.
549 f
= fts_open((char * const *)argv
, FTS_LOGICAL
, NULL
);
551 exitcode
= (int)MANDOCLEVEL_SYSERR
;
559 while (NULL
!= (ff
= fts_read(f
))) {
560 path
= ff
->fts_path
+ 2;
562 * If we're a regular file, add an "of" by using the
563 * stored directory data and handling the filename.
564 * Disallow duplicate (hard-linked) files.
566 if (FTS_F
== ff
->fts_info
) {
567 if (0 == strcmp(path
, MANDOC_DB
))
569 if ( ! use_all
&& ff
->fts_level
< 2) {
571 say(path
, "Extraneous file");
573 } else if (inocheck(ff
->fts_statp
)) {
575 say(path
, "Duplicate file");
577 } else if (NULL
== (sec
=
578 strrchr(ff
->fts_name
, '.'))) {
582 "No filename suffix");
585 } else if (0 == strcmp(++sec
, "html")) {
587 say(path
, "Skip html");
589 } else if (0 == strcmp(sec
, "gz")) {
591 say(path
, "Skip gz");
593 } else if (0 == strcmp(sec
, "ps")) {
595 say(path
, "Skip ps");
597 } else if (0 == strcmp(sec
, "pdf")) {
599 say(path
, "Skip pdf");
601 } else if ( ! use_all
&&
602 ((FORM_SRC
== dform
&& strcmp(sec
, dsec
)) ||
603 (FORM_CAT
== dform
&& strcmp(sec
, "0")))) {
605 say(path
, "Wrong filename suffix");
611 name
= stradd(ff
->fts_name
);
613 name
, dsec
, sec
, arch
, ff
->fts_statp
);
615 } else if (FTS_D
!= ff
->fts_info
&&
616 FTS_DP
!= ff
->fts_info
) {
618 say(path
, "Not a regular file");
622 switch (ff
->fts_level
) {
624 /* Ignore the root directory. */
628 * This might contain manX/ or catX/.
629 * Try to infer this from the name.
630 * If we're not in use_all, enforce it.
635 if (FTS_DP
== ff
->fts_info
)
638 if (0 == strncmp(cp
, "man", 3)) {
640 dsec
= stradd(cp
+ 3);
641 } else if (0 == strncmp(cp
, "cat", 3)) {
643 dsec
= stradd(cp
+ 3);
646 if (NULL
!= dsec
|| use_all
)
650 say(path
, "Unknown directory part");
651 fts_set(f
, ff
, FTS_SKIP
);
655 * Possibly our architecture.
656 * If we're descending, keep tabs on it.
659 if (FTS_DP
!= ff
->fts_info
&& NULL
!= dsec
)
660 arch
= stradd(ff
->fts_name
);
663 if (FTS_DP
== ff
->fts_info
|| use_all
)
666 say(path
, "Extraneous directory part");
667 fts_set(f
, ff
, FTS_SKIP
);
677 * Add a file to the file vector.
678 * Do not verify that it's a "valid" looking manpage (we'll do that
681 * Try to infer the manual section, architecture, and page name from the
682 * path, assuming it looks like
684 * [./]man*[/<arch>]/<name>.<section>
686 * [./]cat<section>[/<arch>]/<name>.0
688 * Stuff this information directly into the "of" vector.
689 * See treescan() for the fts(3) version of this.
692 filescan(const char *file
)
695 const char *sec
, *arch
, *name
, *dsec
;
702 if (0 == strncmp(file
, "./", 2))
705 if (NULL
== realpath(file
, buf
)) {
706 exitcode
= (int)MANDOCLEVEL_BADARG
;
709 } else if (strstr(buf
, basedir
) != buf
) {
710 exitcode
= (int)MANDOCLEVEL_BADARG
;
711 say("", "%s: outside base directory", buf
);
713 } else if (-1 == stat(buf
, &st
)) {
714 exitcode
= (int)MANDOCLEVEL_BADARG
;
717 } else if ( ! (S_IFREG
& st
.st_mode
)) {
718 exitcode
= (int)MANDOCLEVEL_BADARG
;
719 say(file
, "Not a regular file");
721 } else if (inocheck(&st
)) {
723 say(file
, "Duplicate file");
726 start
= buf
+ strlen(basedir
);
727 sec
= arch
= name
= dsec
= NULL
;
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)) {
741 } else if (0 == strncmp(start
, "cat", 3)) {
747 if (NULL
!= 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
, '/'))) {
777 ofadd(dform
, file
, name
, dsec
, sec
, arch
, &st
);
784 filecheck(const char *name
)
787 return(NULL
!= ohash_find(&filenames
,
788 ohash_qlookup(&filenames
, name
)));
792 * Use the standard hashing mechanism (K&R) to see if the given filename
796 fileadd(struct of
*of
)
800 slot
= ohash_qlookup(&filenames
, of
->file
);
801 assert(NULL
== ohash_find(&filenames
, slot
));
802 ohash_insert(&filenames
, slot
, of
);
809 inocheck(const struct stat
*st
)
814 memset(&id
, 0, sizeof(id
));
815 id
.ino
= hash
= st
->st_ino
;
818 return(NULL
!= ohash_find(&inos
, ohash_lookup_memory(
819 &inos
, (char *)&id
, sizeof(id
), hash
)));
823 * The hashing function used here is quite simple: simply take the inode
824 * and use uint32_t of its bits.
825 * Then when we do the lookup, use both the inode and device identifier.
828 inoadd(const struct stat
*st
, struct of
*of
)
833 of
->id
.ino
= hash
= st
->st_ino
;
834 of
->id
.dev
= st
->st_dev
;
835 slot
= ohash_lookup_memory
836 (&inos
, (char *)&of
->id
, sizeof(of
->id
), hash
);
838 assert(NULL
== ohash_find(&inos
, slot
));
839 ohash_insert(&inos
, slot
, of
);
843 ofadd(int dform
, const char *file
, const char *name
, const char *dsec
,
844 const char *sec
, const char *arch
, const struct stat
*st
)
849 assert(NULL
!= file
);
861 if (NULL
!= sec
&& *sec
<= '9' && *sec
>= '1')
863 else if (NULL
!= sec
&& *sec
== '0') {
868 of
= mandoc_calloc(1, sizeof(struct of
));
869 strlcpy(of
->file
, file
, PATH_MAX
);
880 * Add to unique identifier hash.
881 * Then if it's a source manual and we're going to use source in
882 * favour of catpages, add it to that hash.
893 while (NULL
!= (of
= ofs
)) {
900 * Run through the files in the global vector "ofs" and add them to the
901 * database specified in "basedir".
903 * This handles the parsing scheme itself, using the cues of directory
904 * and filename to determine whether the file is parsable or not.
907 ofmerge(struct mchars
*mc
, struct mparse
*mp
)
915 const char *msec
, *march
, *mtitle
, *cp
;
917 enum mandoclevel lvl
;
919 for (of
= ofs
; NULL
!= of
; of
= of
->next
) {
921 * If we're a catpage (as defined by our path), then see
922 * if a manpage exists by the same name (ignoring the
924 * If it does, then we want to use it instead of our
927 if ( ! use_all
&& FORM_CAT
== of
->dform
) {
928 sz
= strlcpy(buf
, of
->file
, PATH_MAX
);
929 if (sz
>= PATH_MAX
) {
931 say(of
->file
, "Filename too long");
934 bufp
= strstr(buf
, "cat");
935 assert(NULL
!= bufp
);
936 memcpy(bufp
, "man", 3);
937 if (NULL
!= (bufp
= strrchr(buf
, '.')))
939 strlcat(buf
, of
->dsec
, PATH_MAX
);
940 if (filecheck(buf
)) {
943 "source exists: %s", buf
);
958 * Try interpreting the file as mdoc(7) or man(7)
959 * source code, unless it is already known to be
960 * formatted. Fall back to formatted mode.
962 if (FORM_SRC
== of
->dform
|| FORM_SRC
== of
->sform
) {
963 lvl
= mparse_readfd(mp
, -1, of
->file
);
964 if (lvl
< MANDOCLEVEL_FATAL
)
965 mparse_result(mp
, &mdoc
, &man
);
970 msec
= mdoc_meta(mdoc
)->msec
;
971 march
= mdoc_meta(mdoc
)->arch
;
972 mtitle
= mdoc_meta(mdoc
)->title
;
973 } else if (NULL
!= man
) {
975 msec
= man_meta(man
)->msec
;
977 mtitle
= man_meta(man
)->title
;
988 * Check whether the manual section given in a file
989 * agrees with the directory where the file is located.
990 * Some manuals have suffixes like (3p) on their
991 * section number either inside the file or in the
992 * directory name, some are linked into more than one
993 * section, like encrypt(1) = makekey(8). Do not skip
994 * manuals for such reasons.
996 if (warnings
&& !use_all
&& form
&&
997 strcasecmp(msec
, of
->dsec
))
998 say(of
->file
, "Section \"%s\" "
999 "manual in %s directory",
1003 * Manual page directories exist for each kernel
1004 * architecture as returned by machine(1).
1005 * However, many manuals only depend on the
1006 * application architecture as returned by arch(1).
1007 * For example, some (2/ARM) manuals are shared
1008 * across the "armish" and "zaurus" kernel
1010 * A few manuals are even shared across completely
1011 * different architectures, for example fdformat(1)
1012 * on amd64, i386, sparc, and sparc64.
1013 * Thus, warn about architecture mismatches,
1014 * but don't skip manuals for this reason.
1016 if (warnings
&& !use_all
&& strcasecmp(march
, of
->arch
))
1017 say(of
->file
, "Architecture \"%s\" "
1018 "manual in \"%s\" directory",
1021 putkey(of
, of
->name
, TYPE_Nm
);
1024 if (NULL
!= (cp
= mdoc_meta(mdoc
)->name
))
1025 putkey(of
, cp
, TYPE_Nm
);
1026 parse_mdoc(of
, mdoc_node(mdoc
));
1027 } else if (NULL
!= man
)
1028 parse_man(of
, man_node(man
));
1032 dbindex(mc
, form
, of
);
1037 parse_catpage(struct of
*of
)
1040 char *line
, *p
, *title
;
1041 size_t len
, plen
, titlesz
;
1043 if (NULL
== (stream
= fopen(of
->file
, "r"))) {
1045 say(of
->file
, NULL
);
1049 /* Skip to first blank line. */
1051 while (NULL
!= (line
= fgetln(stream
, &len
)))
1056 * Assume the first line that is not indented
1057 * is the first section header. Skip to it.
1060 while (NULL
!= (line
= fgetln(stream
, &len
)))
1061 if ('\n' != *line
&& ' ' != *line
)
1065 * Read up until the next section into a buffer.
1066 * Strip the leading and trailing newline from each read line,
1067 * appending a trailing space.
1068 * Ignore empty (whitespace-only) lines.
1074 while (NULL
!= (line
= fgetln(stream
, &len
))) {
1075 if (' ' != *line
|| '\n' != line
[len
- 1])
1077 while (len
> 0 && isspace((unsigned char)*line
)) {
1083 title
= mandoc_realloc(title
, titlesz
+ len
);
1084 memcpy(title
+ titlesz
, line
, len
);
1086 title
[titlesz
- 1] = ' ';
1090 * If no page content can be found, or the input line
1091 * is already the next section header, or there is no
1092 * trailing newline, reuse the page title as the page
1096 if (NULL
== title
|| '\0' == *title
) {
1098 say(of
->file
, "Cannot find NAME section");
1099 putkey(of
, of
->name
, TYPE_Nd
);
1105 title
= mandoc_realloc(title
, titlesz
+ 1);
1106 title
[titlesz
] = '\0';
1109 * Skip to the first dash.
1110 * Use the remaining line as the description (no more than 70
1114 if (NULL
!= (p
= strstr(title
, "- "))) {
1115 for (p
+= 2; ' ' == *p
|| '\b' == *p
; p
++)
1116 /* Skip to next word. */ ;
1119 say(of
->file
, "No dash in title line");
1125 /* Strip backspace-encoding from line. */
1127 while (NULL
!= (line
= memchr(p
, '\b', plen
))) {
1130 memmove(line
, line
+ 1, plen
--);
1133 memmove(line
- 1, line
+ 1, plen
- len
);
1137 of
->desc
= stradd(p
);
1138 putkey(of
, p
, TYPE_Nd
);
1144 * Put a type/word pair into the word database for this particular file.
1147 putkey(const struct of
*of
, const char *value
, uint64_t type
)
1150 assert(NULL
!= value
);
1151 putkeys(of
, value
, strlen(value
), type
);
1155 * Grok all nodes at or below a certain mdoc node into putkey().
1158 putmdockey(const struct of
*of
, const struct mdoc_node
*n
, uint64_t m
)
1161 for ( ; NULL
!= n
; n
= n
->next
) {
1162 if (NULL
!= n
->child
)
1163 putmdockey(of
, n
->child
, m
);
1164 if (MDOC_TEXT
== n
->type
)
1165 putkey(of
, n
->string
, m
);
1170 parse_man(struct of
*of
, const struct man_node
*n
)
1172 const struct man_node
*head
, *body
;
1173 char *start
, *sv
, *title
;
1181 * We're only searching for one thing: the first text child in
1182 * the BODY of a NAME section. Since we don't keep track of
1183 * sections in -man, run some hoops to find out whether we're in
1184 * the correct section or not.
1187 if (MAN_BODY
== n
->type
&& MAN_SH
== n
->tok
) {
1189 assert(body
->parent
);
1190 if (NULL
!= (head
= body
->parent
->head
) &&
1191 1 == head
->nchild
&&
1192 NULL
!= (head
= (head
->child
)) &&
1193 MAN_TEXT
== head
->type
&&
1194 0 == strcmp(head
->string
, "NAME") &&
1195 NULL
!= (body
= body
->child
) &&
1196 MAN_TEXT
== body
->type
) {
1202 * Suck the entire NAME section into memory.
1203 * Yes, we might run away.
1204 * But too many manuals have big, spread-out
1205 * NAME sections over many lines.
1208 for ( ; NULL
!= body
; body
= body
->next
) {
1209 if (MAN_TEXT
!= body
->type
)
1211 if (0 == (sz
= strlen(body
->string
)))
1213 title
= mandoc_realloc
1214 (title
, titlesz
+ sz
+ 1);
1215 memcpy(title
+ titlesz
, body
->string
, sz
);
1217 title
[titlesz
- 1] = ' ';
1222 title
= mandoc_realloc(title
, titlesz
+ 1);
1223 title
[titlesz
] = '\0';
1225 /* Skip leading space. */
1228 while (isspace((unsigned char)*sv
))
1231 if (0 == (sz
= strlen(sv
))) {
1236 /* Erase trailing space. */
1238 start
= &sv
[sz
- 1];
1239 while (start
> sv
&& isspace((unsigned char)*start
))
1250 * Go through a special heuristic dance here.
1251 * Conventionally, one or more manual names are
1252 * comma-specified prior to a whitespace, then a
1253 * dash, then a description. Try to puzzle out
1254 * the name parts here.
1258 sz
= strcspn(start
, " ,");
1259 if ('\0' == start
[sz
])
1265 putkey(of
, start
, TYPE_Nm
);
1272 assert(',' == byte
);
1274 while (' ' == *start
)
1279 putkey(of
, start
, TYPE_Nm
);
1284 while (isspace((unsigned char)*start
))
1287 if (0 == strncmp(start
, "-", 1))
1289 else if (0 == strncmp(start
, "\\-\\-", 4))
1291 else if (0 == strncmp(start
, "\\-", 2))
1293 else if (0 == strncmp(start
, "\\(en", 4))
1295 else if (0 == strncmp(start
, "\\(em", 4))
1298 while (' ' == *start
)
1301 assert(NULL
== of
->desc
);
1302 of
->desc
= stradd(start
);
1303 putkey(of
, start
, TYPE_Nd
);
1309 for (n
= n
->child
; n
; n
= n
->next
)
1314 parse_mdoc(struct of
*of
, const struct mdoc_node
*n
)
1318 for (n
= n
->child
; NULL
!= n
; n
= n
->next
) {
1329 if (NULL
!= mdocs
[n
->tok
].fp
)
1330 if (0 == (*mdocs
[n
->tok
].fp
)(of
, n
))
1333 if (MDOCF_CHILD
& mdocs
[n
->tok
].flags
)
1334 putmdockey(of
, n
->child
, mdocs
[n
->tok
].mask
);
1337 assert(MDOC_ROOT
!= n
->type
);
1340 if (NULL
!= n
->child
)
1346 parse_mdoc_Fd(struct of
*of
, const struct mdoc_node
*n
)
1348 const char *start
, *end
;
1351 if (SEC_SYNOPSIS
!= n
->sec
||
1352 NULL
== (n
= n
->child
) ||
1353 MDOC_TEXT
!= n
->type
)
1357 * Only consider those `Fd' macro fields that begin with an
1358 * "inclusion" token (versus, e.g., #define).
1361 if (strcmp("#include", n
->string
))
1364 if (NULL
== (n
= n
->next
) || MDOC_TEXT
!= n
->type
)
1368 * Strip away the enclosing angle brackets and make sure we're
1373 if ('<' == *start
|| '"' == *start
)
1376 if (0 == (sz
= strlen(start
)))
1379 end
= &start
[(int)sz
- 1];
1380 if ('>' == *end
|| '"' == *end
)
1384 putkeys(of
, start
, end
- start
+ 1, TYPE_In
);
1389 parse_mdoc_In(struct of
*of
, const struct mdoc_node
*n
)
1392 if (NULL
!= n
->child
&& MDOC_TEXT
== n
->child
->type
)
1395 putkey(of
, n
->child
->string
, TYPE_In
);
1400 parse_mdoc_Fn(struct of
*of
, const struct mdoc_node
*n
)
1404 if (NULL
== (n
= n
->child
) || MDOC_TEXT
!= n
->type
)
1408 * Parse: .Fn "struct type *name" "char *arg".
1409 * First strip away pointer symbol.
1410 * Then store the function name, then type.
1411 * Finally, store the arguments.
1414 if (NULL
== (cp
= strrchr(n
->string
, ' ')))
1420 putkey(of
, cp
, TYPE_Fn
);
1423 putkeys(of
, n
->string
, cp
- n
->string
, TYPE_Ft
);
1425 for (n
= n
->next
; NULL
!= n
; n
= n
->next
)
1426 if (MDOC_TEXT
== n
->type
)
1427 putkey(of
, n
->string
, TYPE_Fa
);
1433 parse_mdoc_St(struct of
*of
, const struct mdoc_node
*n
)
1436 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
1439 putkey(of
, n
->child
->string
, TYPE_St
);
1444 parse_mdoc_Xr(struct of
*of
, const struct mdoc_node
*n
)
1447 if (NULL
== (n
= n
->child
))
1450 putkey(of
, n
->string
, TYPE_Xr
);
1455 parse_mdoc_Nd(struct of
*of
, const struct mdoc_node
*n
)
1460 if (MDOC_BODY
!= n
->type
)
1464 * Special-case the `Nd' because we need to put the description
1465 * into the document table.
1469 for (n
= n
->child
; NULL
!= n
; n
= n
->next
) {
1470 if (MDOC_TEXT
== n
->type
) {
1471 sz
= strlen(n
->string
) + 1;
1472 if (NULL
!= (sv
= desc
))
1473 sz
+= strlen(desc
) + 1;
1474 desc
= mandoc_realloc(desc
, sz
);
1476 strlcat(desc
, " ", sz
);
1479 strlcat(desc
, n
->string
, sz
);
1481 if (NULL
!= n
->child
)
1482 parse_mdoc_Nd(of
, n
);
1485 of
->desc
= NULL
!= desc
? stradd(desc
) : NULL
;
1491 parse_mdoc_Nm(struct of
*of
, const struct mdoc_node
*n
)
1494 if (SEC_NAME
== n
->sec
)
1496 else if (SEC_SYNOPSIS
!= n
->sec
|| MDOC_HEAD
!= n
->type
)
1503 parse_mdoc_Sh(struct of
*of
, const struct mdoc_node
*n
)
1506 return(SEC_CUSTOM
== n
->sec
&& MDOC_HEAD
== n
->type
);
1510 parse_mdoc_head(struct of
*of
, const struct mdoc_node
*n
)
1513 return(MDOC_HEAD
== n
->type
);
1517 parse_mdoc_body(struct of
*of
, const struct mdoc_node
*n
)
1520 return(MDOC_BODY
== n
->type
);
1527 stradd(const char *cp
)
1530 return(stradds(cp
, strlen(cp
)));
1534 * This looks up or adds a string to the string table.
1535 * The string table is a table of all strings encountered during parse
1537 * In using it, we avoid having thousands of (e.g.) "cat1" string
1538 * allocations for the "of" table.
1539 * We also have a layer atop the string table for keeping track of words
1540 * in a parse sequence (see putkeys()).
1543 stradds(const char *cp
, size_t sz
)
1550 slot
= ohash_qlookupi(&strings
, cp
, &end
);
1551 if (NULL
!= (s
= ohash_find(&strings
, slot
)))
1554 s
= mandoc_calloc(sizeof(struct str
) + sz
+ 1, 1);
1555 memcpy(s
->key
, cp
, sz
);
1556 ohash_insert(&strings
, slot
, s
);
1561 * Add a word to the current parse sequence.
1562 * Within the hashtable of strings, we maintain a list of strings that
1563 * are currently indexed.
1564 * Each of these ("words") has a bitmask modified within the parse.
1565 * When we finish a parse, we'll dump the list, then remove the head
1566 * entry -- since the next parse will have a new "of", it can keep track
1567 * of its entries without conflict.
1570 putkeys(const struct of
*of
, const char *cp
, size_t sz
, uint64_t v
)
1580 slot
= ohash_qlookupi(&strings
, cp
, &end
);
1581 s
= ohash_find(&strings
, slot
);
1583 if (NULL
!= s
&& of
== s
->of
) {
1586 } else if (NULL
== s
) {
1587 s
= mandoc_calloc(sizeof(struct str
) + sz
+ 1, 1);
1588 memcpy(s
->key
, cp
, sz
);
1589 ohash_insert(&strings
, slot
, s
);
1599 * Take a Unicode codepoint and produce its UTF-8 encoding.
1600 * This isn't the best way to do this, but it works.
1601 * The magic numbers are from the UTF-8 packaging.
1602 * They're not as scary as they seem: read the UTF-8 spec for details.
1605 utf8(unsigned int cp
, char out
[7])
1610 if (cp
<= 0x0000007F) {
1613 } else if (cp
<= 0x000007FF) {
1615 out
[0] = (cp
>> 6 & 31) | 192;
1616 out
[1] = (cp
& 63) | 128;
1617 } else if (cp
<= 0x0000FFFF) {
1619 out
[0] = (cp
>> 12 & 15) | 224;
1620 out
[1] = (cp
>> 6 & 63) | 128;
1621 out
[2] = (cp
& 63) | 128;
1622 } else if (cp
<= 0x001FFFFF) {
1624 out
[0] = (cp
>> 18 & 7) | 240;
1625 out
[1] = (cp
>> 12 & 63) | 128;
1626 out
[2] = (cp
>> 6 & 63) | 128;
1627 out
[3] = (cp
& 63) | 128;
1628 } else if (cp
<= 0x03FFFFFF) {
1630 out
[0] = (cp
>> 24 & 3) | 248;
1631 out
[1] = (cp
>> 18 & 63) | 128;
1632 out
[2] = (cp
>> 12 & 63) | 128;
1633 out
[3] = (cp
>> 6 & 63) | 128;
1634 out
[4] = (cp
& 63) | 128;
1635 } else if (cp
<= 0x7FFFFFFF) {
1637 out
[0] = (cp
>> 30 & 1) | 252;
1638 out
[1] = (cp
>> 24 & 63) | 128;
1639 out
[2] = (cp
>> 18 & 63) | 128;
1640 out
[3] = (cp
>> 12 & 63) | 128;
1641 out
[4] = (cp
>> 6 & 63) | 128;
1642 out
[5] = (cp
& 63) | 128;
1651 * Store the UTF-8 version of a key, or alias the pointer if the key has
1652 * no UTF-8 transcription marks in it.
1655 utf8key(struct mchars
*mc
, struct str
*key
)
1657 size_t sz
, bsz
, pos
;
1658 char utfbuf
[7], res
[5];
1660 const char *seq
, *cpp
, *val
;
1662 enum mandoc_esc esc
;
1664 assert(NULL
== key
->utf8
);
1668 res
[2] = ASCII_NBRSP
;
1669 res
[3] = ASCII_HYPH
;
1676 * Pre-check: if we have no stop-characters, then set the
1677 * pointer as ourselvse and get out of here.
1679 if (strcspn(val
, res
) == bsz
) {
1680 key
->utf8
= key
->key
;
1684 /* Pre-allocate by the length of the input */
1686 buf
= mandoc_malloc(++bsz
);
1689 while ('\0' != *val
) {
1691 * Halt on the first escape sequence.
1692 * This also halts on the end of string, in which case
1693 * we just copy, fallthrough, and exit the loop.
1695 if ((sz
= strcspn(val
, res
)) > 0) {
1696 memcpy(&buf
[pos
], val
, sz
);
1701 if (ASCII_HYPH
== *val
) {
1705 } else if ('\t' == *val
|| ASCII_NBRSP
== *val
) {
1709 } else if ('\\' != *val
)
1712 /* Read past the slash. */
1718 * Parse the escape sequence and see if it's a
1719 * predefined character or special character.
1722 ((const char **)&val
, &seq
, &len
);
1723 if (ESCAPE_ERROR
== esc
)
1726 if (ESCAPE_SPECIAL
!= esc
)
1728 if (0 == (u
= mchars_spec2cp(mc
, seq
, len
)))
1732 * If we have a Unicode codepoint, try to convert that
1733 * to a UTF-8 byte string.
1736 if (0 == (sz
= utf8(u
, utfbuf
)))
1739 /* Copy the rendered glyph into the stream. */
1744 buf
= mandoc_realloc(buf
, bsz
);
1746 memcpy(&buf
[pos
], cpp
, sz
);
1755 * Flush the current page's terms (and their bits) into the database.
1756 * Wrap the entire set of additions in a transaction to make sqlite be a
1758 * Also, UTF-8-encode the description at the last possible moment.
1761 dbindex(struct mchars
*mc
, int form
, const struct of
*of
)
1769 say(of
->file
, "Adding to index");
1775 if (NULL
!= of
->desc
) {
1776 key
= ohash_find(&strings
,
1777 ohash_qlookup(&strings
, of
->desc
));
1778 assert(NULL
!= key
);
1779 if (NULL
== key
->utf8
)
1784 SQL_EXEC("BEGIN TRANSACTION");
1787 SQL_BIND_TEXT(stmts
[STMT_INSERT_DOC
], i
, of
->file
);
1788 SQL_BIND_TEXT(stmts
[STMT_INSERT_DOC
], i
, of
->sec
);
1789 SQL_BIND_TEXT(stmts
[STMT_INSERT_DOC
], i
, of
->arch
);
1790 SQL_BIND_TEXT(stmts
[STMT_INSERT_DOC
], i
, desc
);
1791 SQL_BIND_INT(stmts
[STMT_INSERT_DOC
], i
, form
);
1792 SQL_STEP(stmts
[STMT_INSERT_DOC
]);
1793 recno
= sqlite3_last_insert_rowid(db
);
1794 sqlite3_reset(stmts
[STMT_INSERT_DOC
]);
1796 for (key
= words
; NULL
!= key
; key
= key
->next
) {
1797 assert(key
->of
== of
);
1798 if (NULL
== key
->utf8
)
1801 SQL_BIND_INT64(stmts
[STMT_INSERT_KEY
], i
, key
->mask
);
1802 SQL_BIND_TEXT(stmts
[STMT_INSERT_KEY
], i
, key
->utf8
);
1803 SQL_BIND_INT64(stmts
[STMT_INSERT_KEY
], i
, recno
);
1804 SQL_STEP(stmts
[STMT_INSERT_KEY
]);
1805 sqlite3_reset(stmts
[STMT_INSERT_KEY
]);
1808 SQL_EXEC("END TRANSACTION");
1820 for (of
= ofs
; NULL
!= of
; of
= of
->next
) {
1822 SQL_BIND_TEXT(stmts
[STMT_DELETE
], i
, of
->file
);
1823 SQL_STEP(stmts
[STMT_DELETE
]);
1824 sqlite3_reset(stmts
[STMT_DELETE
]);
1826 say(of
->file
, "Deleted from index");
1831 * Close an existing database and its prepared statements.
1832 * If "real" is not set, rename the temporary file into the real one.
1842 for (i
= 0; i
< STMT__MAX
; i
++) {
1843 sqlite3_finalize(stmts
[i
]);
1853 if (-1 == rename(MANDOC_DB
"~", MANDOC_DB
)) {
1854 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1855 say(MANDOC_DB
, NULL
);
1860 * This is straightforward stuff.
1861 * Open a database connection to a "temporary" database, then open a set
1862 * of prepared statements we'll use over and over again.
1863 * If "real" is set, we use the existing database; if not, we truncate a
1865 * Must be matched by dbclose().
1870 const char *file
, *sql
;
1876 ofl
= SQLITE_OPEN_READWRITE
;
1878 file
= MANDOC_DB
"~";
1879 if (-1 == remove(file
) && ENOENT
!= errno
) {
1880 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1884 ofl
|= SQLITE_OPEN_EXCLUSIVE
;
1888 rc
= sqlite3_open_v2(file
, &db
, ofl
, NULL
);
1889 if (SQLITE_OK
== rc
)
1890 goto prepare_statements
;
1891 if (SQLITE_CANTOPEN
!= rc
) {
1892 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1900 if (SQLITE_OK
!= (rc
= sqlite3_open(file
, &db
))) {
1901 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1906 sql
= "CREATE TABLE \"docs\" (\n"
1907 " \"file\" TEXT NOT NULL,\n"
1908 " \"sec\" TEXT NOT NULL,\n"
1909 " \"arch\" TEXT NOT NULL,\n"
1910 " \"desc\" TEXT NOT NULL,\n"
1911 " \"form\" INTEGER NOT NULL,\n"
1912 " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
1915 "CREATE TABLE \"keys\" (\n"
1916 " \"bits\" INTEGER NOT NULL,\n"
1917 " \"key\" TEXT NOT NULL,\n"
1918 " \"docid\" INTEGER NOT NULL REFERENCES docs(id) "
1919 "ON DELETE CASCADE,\n"
1920 " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
1923 "CREATE INDEX \"key_index\" ON keys (key);\n";
1925 if (SQLITE_OK
!= sqlite3_exec(db
, sql
, NULL
, NULL
, NULL
)) {
1926 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1927 say(file
, "%s", sqlite3_errmsg(db
));
1932 SQL_EXEC("PRAGMA foreign_keys = ON");
1933 sql
= "DELETE FROM docs where file=?";
1934 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_DELETE
], NULL
);
1935 sql
= "INSERT INTO docs "
1936 "(file,sec,arch,desc,form) VALUES (?,?,?,?,?)";
1937 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_DOC
], NULL
);
1938 sql
= "INSERT INTO keys "
1939 "(bits,key,docid) VALUES (?,?,?)";
1940 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_KEY
], NULL
);
1945 hash_halloc(size_t sz
, void *arg
)
1948 return(mandoc_calloc(sz
, 1));
1952 hash_alloc(size_t sz
, void *arg
)
1955 return(mandoc_malloc(sz
));
1959 hash_free(void *p
, size_t sz
, void *arg
)
1966 set_basedir(const char *targetdir
)
1968 static char startdir
[PATH_MAX
];
1972 * Remember where we started by keeping a fd open to the origin
1973 * path component: throughout this utility, we chdir() a lot to
1974 * handle relative paths, and by doing this, we can return to
1975 * the starting point.
1977 if ('\0' == *startdir
) {
1978 if (NULL
== getcwd(startdir
, PATH_MAX
)) {
1979 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1980 if (NULL
!= targetdir
)
1984 if (-1 == (fd
= open(startdir
, O_RDONLY
, 0))) {
1985 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1986 say(startdir
, NULL
);
1989 if (NULL
== targetdir
)
1990 targetdir
= startdir
;
1994 if (-1 == fchdir(fd
)) {
1997 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1998 say(startdir
, NULL
);
2001 if (NULL
== targetdir
) {
2006 if (NULL
== realpath(targetdir
, basedir
)) {
2008 exitcode
= (int)MANDOCLEVEL_BADARG
;
2009 say(targetdir
, NULL
);
2011 } else if (-1 == chdir(basedir
)) {
2012 exitcode
= (int)MANDOCLEVEL_BADARG
;
2020 say(const char *file
, const char *format
, ...)
2024 if ('\0' != *basedir
)
2025 fprintf(stderr
, "%s", basedir
);
2026 if ('\0' != *basedir
&& '\0' != *file
)
2027 fputs("//", stderr
);
2029 fprintf(stderr
, "%s", file
);
2030 fputs(": ", stderr
);
2032 if (NULL
== format
) {
2037 va_start(ap
, format
);
2038 vfprintf(stderr
, format
, ap
);
2041 fputc('\n', stderr
);