]>
git.cameronkatri.com Git - mandoc.git/blob - mandocdb.c
1 /* $Id: mandocdb.c,v 1.50 2012/06/08 10:43:01 kristaps Exp $ */
3 * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011, 2012 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.
22 #include <sys/param.h>
46 /* Post a warning to stderr. */
47 #define WARNING(_f, _b, _fmt, _args...) \
49 fprintf(stderr, "%s: ", (_b)); \
50 fprintf(stderr, (_fmt), ##_args); \
52 fprintf(stderr, ": %s", (_f)); \
53 fprintf(stderr, "\n"); \
54 } while (/* CONSTCOND */ 0)
55 /* Post a "verbose" message to stderr. */
56 #define DEBUG(_f, _b, _fmt, _args...) \
58 fprintf(stderr, "%s: ", (_b)); \
59 fprintf(stderr, (_fmt), ##_args); \
60 fprintf(stderr, ": %s\n", (_f)); \
61 } while (/* CONSTCOND */ 0)
64 OP_DEFAULT
= 0, /* new dbs from dir list or default config */
65 OP_CONFFILE
, /* new databases from custom config file */
66 OP_UPDATE
, /* delete/add entries in existing database */
67 OP_DELETE
, /* delete entries from existing database */
68 OP_TEST
/* change no databases, report potential problems */
72 FORM_SRC
, /* format is -man or -mdoc */
73 FORM_CAT
, /* format is cat */
74 FORM_NONE
/* format is unknown */
78 char *utf8
; /* key in UTF-8 form */
79 const struct of
*of
; /* if set, the owning parse */
80 struct str
*next
; /* next in owning parse sequence */
81 uint64_t mask
; /* bitmask in sequence */
82 char key
[1]; /* the string itself */
91 struct id id
; /* used for hashing routine */
92 struct of
*next
; /* next in ofs */
93 enum form dform
; /* path-cued form */
94 enum form sform
; /* suffix-cued form */
95 char file
[MAXPATHLEN
]; /* filename rel. to manpath */
96 const char *desc
; /* parsed description */
97 const char *sec
; /* suffix-cued section (or empty) */
98 const char *dsec
; /* path-cued section (or empty) */
99 const char *arch
; /* path-cued arch. (or empty) */
100 const char *name
; /* name (from filename) (not empty) */
104 STMT_DELETE
= 0, /* delete manpage */
105 STMT_INSERT_DOC
, /* insert manpage */
106 STMT_INSERT_KEY
, /* insert parsed key */
110 typedef int (*mdoc_fp
)(struct of
*, const struct mdoc_node
*);
112 struct mdoc_handler
{
113 mdoc_fp fp
; /* optional handler */
114 uint64_t mask
; /* set unless handler returns 0 */
115 int flags
; /* for use by pmdoc_node */
116 #define MDOCF_CHILD 0x01 /* automatically index child nodes */
119 static void dbclose(const char *, int);
120 static void dbindex(struct mchars
*, int,
121 const struct of
*, const char *);
122 static int dbopen(const char *, int);
123 static void dbprune(const char *);
124 static void fileadd(struct of
*);
125 static int filecheck(const char *);
126 static void filescan(const char *, const char *);
127 static struct str
*hashget(const char *, size_t);
128 static void *hash_alloc(size_t, void *);
129 static void hash_free(void *, size_t, void *);
130 static void *hash_halloc(size_t, void *);
131 static void inoadd(const struct stat
*, struct of
*);
132 static int inocheck(const struct stat
*);
133 static void ofadd(const char *, int, const char *,
134 const char *, const char *, const char *,
135 const char *, const struct stat
*);
136 static void offree(void);
137 static int ofmerge(struct mchars
*, struct mparse
*, const char *);
138 static void parse_catpage(struct of
*, const char *);
139 static int parse_man(struct of
*,
140 const struct man_node
*);
141 static void parse_mdoc(struct of
*, const struct mdoc_node
*);
142 static int parse_mdoc_body(struct of
*, const struct mdoc_node
*);
143 static int parse_mdoc_head(struct of
*, const struct mdoc_node
*);
144 static int parse_mdoc_Fd(struct of
*, const struct mdoc_node
*);
145 static int parse_mdoc_Fn(struct of
*, const struct mdoc_node
*);
146 static int parse_mdoc_In(struct of
*, const struct mdoc_node
*);
147 static int parse_mdoc_Nd(struct of
*, const struct mdoc_node
*);
148 static int parse_mdoc_Nm(struct of
*, const struct mdoc_node
*);
149 static int parse_mdoc_Sh(struct of
*, const struct mdoc_node
*);
150 static int parse_mdoc_St(struct of
*, const struct mdoc_node
*);
151 static int parse_mdoc_Xr(struct of
*, const struct mdoc_node
*);
152 static int path_reset(const char *, int, const char *);
153 static void putkey(const struct of
*,
154 const char *, uint64_t);
155 static void putkeys(const struct of
*,
156 const char *, int, uint64_t);
157 static void putmdockey(const struct of
*,
158 const struct mdoc_node
*, uint64_t);
159 static char *stradd(const char *);
160 static char *straddbuf(const char *, size_t);
161 static int treescan(const char *);
162 static size_t utf8(unsigned int, char [7]);
163 static void utf8key(struct mchars
*, struct str
*);
164 static void wordaddbuf(const struct of
*,
165 const char *, size_t, uint64_t);
167 static char *progname
;
168 static int use_all
; /* use all found files */
169 static int nodb
; /* no database changes */
170 static int verb
; /* print what we're doing */
171 static int warnings
; /* warn about crap */
172 static enum op op
; /* operational mode */
173 static struct ohash inos
; /* table of inodes/devices */
174 static struct ohash filenames
; /* table of filenames */
175 static struct ohash strings
; /* table of all strings */
176 static struct of
*ofs
= NULL
; /* vector of files to parse */
177 static struct str
*words
= NULL
; /* word list in current parse */
178 static sqlite3
*db
= NULL
; /* current database */
179 static sqlite3_stmt
*stmts
[STMT__MAX
]; /* current statements */
181 static const struct mdoc_handler mdocs
[MDOC_MAX
] = {
182 { NULL
, 0, 0 }, /* Ap */
183 { NULL
, 0, 0 }, /* Dd */
184 { NULL
, 0, 0 }, /* Dt */
185 { NULL
, 0, 0 }, /* Os */
186 { parse_mdoc_Sh
, TYPE_Sh
, MDOCF_CHILD
}, /* Sh */
187 { parse_mdoc_head
, TYPE_Ss
, MDOCF_CHILD
}, /* Ss */
188 { NULL
, 0, 0 }, /* Pp */
189 { NULL
, 0, 0 }, /* D1 */
190 { NULL
, 0, 0 }, /* Dl */
191 { NULL
, 0, 0 }, /* Bd */
192 { NULL
, 0, 0 }, /* Ed */
193 { NULL
, 0, 0 }, /* Bl */
194 { NULL
, 0, 0 }, /* El */
195 { NULL
, 0, 0 }, /* It */
196 { NULL
, 0, 0 }, /* Ad */
197 { NULL
, TYPE_An
, MDOCF_CHILD
}, /* An */
198 { NULL
, TYPE_Ar
, MDOCF_CHILD
}, /* Ar */
199 { NULL
, TYPE_Cd
, MDOCF_CHILD
}, /* Cd */
200 { NULL
, TYPE_Cm
, MDOCF_CHILD
}, /* Cm */
201 { NULL
, TYPE_Dv
, MDOCF_CHILD
}, /* Dv */
202 { NULL
, TYPE_Er
, MDOCF_CHILD
}, /* Er */
203 { NULL
, TYPE_Ev
, MDOCF_CHILD
}, /* Ev */
204 { NULL
, 0, 0 }, /* Ex */
205 { NULL
, TYPE_Fa
, MDOCF_CHILD
}, /* Fa */
206 { parse_mdoc_Fd
, TYPE_In
, 0 }, /* Fd */
207 { NULL
, TYPE_Fl
, MDOCF_CHILD
}, /* Fl */
208 { parse_mdoc_Fn
, 0, 0 }, /* Fn */
209 { NULL
, TYPE_Ft
, MDOCF_CHILD
}, /* Ft */
210 { NULL
, TYPE_Ic
, MDOCF_CHILD
}, /* Ic */
211 { parse_mdoc_In
, TYPE_In
, MDOCF_CHILD
}, /* In */
212 { NULL
, TYPE_Li
, MDOCF_CHILD
}, /* Li */
213 { parse_mdoc_Nd
, TYPE_Nd
, MDOCF_CHILD
}, /* Nd */
214 { parse_mdoc_Nm
, TYPE_Nm
, MDOCF_CHILD
}, /* Nm */
215 { NULL
, 0, 0 }, /* Op */
216 { NULL
, 0, 0 }, /* Ot */
217 { NULL
, TYPE_Pa
, MDOCF_CHILD
}, /* Pa */
218 { NULL
, 0, 0 }, /* Rv */
219 { parse_mdoc_St
, TYPE_St
, 0 }, /* St */
220 { NULL
, TYPE_Va
, MDOCF_CHILD
}, /* Va */
221 { parse_mdoc_body
, TYPE_Va
, MDOCF_CHILD
}, /* Vt */
222 { parse_mdoc_Xr
, TYPE_Xr
, 0 }, /* Xr */
223 { NULL
, 0, 0 }, /* %A */
224 { NULL
, 0, 0 }, /* %B */
225 { NULL
, 0, 0 }, /* %D */
226 { NULL
, 0, 0 }, /* %I */
227 { NULL
, 0, 0 }, /* %J */
228 { NULL
, 0, 0 }, /* %N */
229 { NULL
, 0, 0 }, /* %O */
230 { NULL
, 0, 0 }, /* %P */
231 { NULL
, 0, 0 }, /* %R */
232 { NULL
, 0, 0 }, /* %T */
233 { NULL
, 0, 0 }, /* %V */
234 { NULL
, 0, 0 }, /* Ac */
235 { NULL
, 0, 0 }, /* Ao */
236 { NULL
, 0, 0 }, /* Aq */
237 { NULL
, TYPE_At
, MDOCF_CHILD
}, /* At */
238 { NULL
, 0, 0 }, /* Bc */
239 { NULL
, 0, 0 }, /* Bf */
240 { NULL
, 0, 0 }, /* Bo */
241 { NULL
, 0, 0 }, /* Bq */
242 { NULL
, TYPE_Bsx
, MDOCF_CHILD
}, /* Bsx */
243 { NULL
, TYPE_Bx
, MDOCF_CHILD
}, /* Bx */
244 { NULL
, 0, 0 }, /* Db */
245 { NULL
, 0, 0 }, /* Dc */
246 { NULL
, 0, 0 }, /* Do */
247 { NULL
, 0, 0 }, /* Dq */
248 { NULL
, 0, 0 }, /* Ec */
249 { NULL
, 0, 0 }, /* Ef */
250 { NULL
, TYPE_Em
, MDOCF_CHILD
}, /* Em */
251 { NULL
, 0, 0 }, /* Eo */
252 { NULL
, TYPE_Fx
, MDOCF_CHILD
}, /* Fx */
253 { NULL
, TYPE_Ms
, MDOCF_CHILD
}, /* Ms */
254 { NULL
, 0, 0 }, /* No */
255 { NULL
, 0, 0 }, /* Ns */
256 { NULL
, TYPE_Nx
, MDOCF_CHILD
}, /* Nx */
257 { NULL
, TYPE_Ox
, MDOCF_CHILD
}, /* Ox */
258 { NULL
, 0, 0 }, /* Pc */
259 { NULL
, 0, 0 }, /* Pf */
260 { NULL
, 0, 0 }, /* Po */
261 { NULL
, 0, 0 }, /* Pq */
262 { NULL
, 0, 0 }, /* Qc */
263 { NULL
, 0, 0 }, /* Ql */
264 { NULL
, 0, 0 }, /* Qo */
265 { NULL
, 0, 0 }, /* Qq */
266 { NULL
, 0, 0 }, /* Re */
267 { NULL
, 0, 0 }, /* Rs */
268 { NULL
, 0, 0 }, /* Sc */
269 { NULL
, 0, 0 }, /* So */
270 { NULL
, 0, 0 }, /* Sq */
271 { NULL
, 0, 0 }, /* Sm */
272 { NULL
, 0, 0 }, /* Sx */
273 { NULL
, TYPE_Sy
, MDOCF_CHILD
}, /* Sy */
274 { NULL
, TYPE_Tn
, MDOCF_CHILD
}, /* Tn */
275 { NULL
, 0, 0 }, /* Ux */
276 { NULL
, 0, 0 }, /* Xc */
277 { NULL
, 0, 0 }, /* Xo */
278 { parse_mdoc_head
, TYPE_Fn
, 0 }, /* Fo */
279 { NULL
, 0, 0 }, /* Fc */
280 { NULL
, 0, 0 }, /* Oo */
281 { NULL
, 0, 0 }, /* Oc */
282 { NULL
, 0, 0 }, /* Bk */
283 { NULL
, 0, 0 }, /* Ek */
284 { NULL
, 0, 0 }, /* Bt */
285 { NULL
, 0, 0 }, /* Hf */
286 { NULL
, 0, 0 }, /* Fr */
287 { NULL
, 0, 0 }, /* Ud */
288 { NULL
, TYPE_Lb
, MDOCF_CHILD
}, /* Lb */
289 { NULL
, 0, 0 }, /* Lp */
290 { NULL
, TYPE_Lk
, MDOCF_CHILD
}, /* Lk */
291 { NULL
, TYPE_Mt
, MDOCF_CHILD
}, /* Mt */
292 { NULL
, 0, 0 }, /* Brq */
293 { NULL
, 0, 0 }, /* Bro */
294 { NULL
, 0, 0 }, /* Brc */
295 { NULL
, 0, 0 }, /* %C */
296 { NULL
, 0, 0 }, /* Es */
297 { NULL
, 0, 0 }, /* En */
298 { NULL
, TYPE_Dx
, MDOCF_CHILD
}, /* Dx */
299 { NULL
, 0, 0 }, /* %Q */
300 { NULL
, 0, 0 }, /* br */
301 { NULL
, 0, 0 }, /* sp */
302 { NULL
, 0, 0 }, /* %U */
303 { NULL
, 0, 0 }, /* Ta */
307 main(int argc
, char *argv
[])
309 char cwd
[MAXPATHLEN
];
315 struct manpaths dirs
;
317 struct ohash_info ino_info
, filename_info
, str_info
;
319 memset(stmts
, 0, STMT__MAX
* sizeof(sqlite3_stmt
*));
320 memset(&dirs
, 0, sizeof(struct manpaths
));
322 ino_info
.halloc
= filename_info
.halloc
=
323 str_info
.halloc
= hash_halloc
;
324 ino_info
.hfree
= filename_info
.hfree
=
325 str_info
.hfree
= hash_free
;
326 ino_info
.alloc
= filename_info
.alloc
=
327 str_info
.alloc
= hash_alloc
;
329 ino_info
.key_offset
= offsetof(struct of
, id
);
330 filename_info
.key_offset
= offsetof(struct of
, file
);
331 str_info
.key_offset
= offsetof(struct str
, key
);
333 progname
= strrchr(argv
[0], '/');
334 if (progname
== NULL
)
340 * Remember where we started by keeping a fd open to the origin
341 * path component: throughout this utility, we chdir() a lot to
342 * handle relative paths, and by doing this, we can return to
343 * the starting point.
345 if (NULL
== getcwd(cwd
, MAXPATHLEN
)) {
347 return(EXIT_FAILURE
);
348 } else if (-1 == (fd
= open(cwd
, O_RDONLY
, 0))) {
350 return(EXIT_FAILURE
);
354 * We accept a few different invocations.
355 * The CHECKOP macro makes sure that invocation styles don't
356 * clobber each other.
358 #define CHECKOP(_op, _ch) do \
359 if (OP_DEFAULT != (_op)) { \
360 fprintf(stderr, "-%c: Conflicting option\n", (_ch)); \
362 } while (/*CONSTCOND*/0)
367 while (-1 != (ch
= getopt(argc
, argv
, "aC:d:ntu:vW")))
387 dup2(STDOUT_FILENO
, STDERR_FILENO
);
409 if (OP_CONFFILE
== op
&& argc
> 0) {
410 fprintf(stderr
, "-C: Too many arguments\n");
415 mp
= mparse_alloc(MPARSE_AUTO
,
416 MANDOCLEVEL_FATAL
, NULL
, NULL
, NULL
);
419 ohash_init(&strings
, 6, &str_info
);
420 ohash_init(&inos
, 6, &ino_info
);
421 ohash_init(&filenames
, 6, &filename_info
);
423 if (OP_UPDATE
== op
|| OP_DELETE
== op
|| OP_TEST
== op
) {
425 * 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 == path_reset(cwd
, fd
, dir
))
437 for (i
= 0; i
< argc
; i
++)
438 filescan(argv
[i
], dir
);
439 if (0 == dbopen(dir
, 1))
444 rc
= ofmerge(mc
, mp
, dir
);
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
, dir
, NULL
, NULL
);
462 * First scan the tree rooted at a base directory.
463 * Then whak its database (if one exists), parse, and
464 * build up the database.
465 * Ignore zero-length directories and strip trailing
468 for (j
= 0; j
< dirs
.sz
; j
++) {
469 sz
= strlen(dirs
.paths
[j
]);
470 if (sz
&& '/' == dirs
.paths
[j
][sz
- 1])
471 dirs
.paths
[j
][--sz
] = '\0';
474 if (0 == path_reset(cwd
, fd
, dirs
.paths
[j
]))
476 if (0 == treescan(dirs
.paths
[j
]))
478 if (0 == path_reset(cwd
, fd
, dirs
.paths
[j
]))
480 if (0 == dbopen(dirs
.paths
[j
], 0))
482 if (0 == ofmerge(mc
, mp
, dirs
.paths
[j
]))
484 dbclose(dirs
.paths
[j
], 0);
487 ohash_init(&inos
, 6, &ino_info
);
488 ohash_delete(&filenames
);
489 ohash_init(&filenames
, 6, &filename_info
);
497 for (s
= ohash_first(&strings
, &ch
);
498 NULL
!= s
; s
= ohash_next(&strings
, &ch
)) {
499 if (s
->utf8
!= s
->key
)
503 ohash_delete(&strings
);
505 ohash_delete(&filenames
);
507 return(rc
? EXIT_SUCCESS
: EXIT_FAILURE
);
509 fprintf(stderr
, "usage: %s [-anvW] [-C file]\n"
510 " %s [-anvW] dir ...\n"
511 " %s [-nvW] -d dir [file ...]\n"
512 " %s [-nvW] -u dir [file ...]\n"
514 progname
, progname
, progname
,
517 return(EXIT_FAILURE
);
521 * Scan a directory tree rooted at "base" for manpages.
522 * We use fts(), scanning directory parts along the way for clues to our
523 * section and architecture.
525 * If use_all has been specified, grok all files.
526 * If not, sanitise paths to the following:
528 * [./]man*[/<arch>]/<name>.<section>
530 * [./]cat<section>[/<arch>]/<name>.0
532 * TODO: accomodate for multi-language directories.
535 treescan(const char *base
)
541 const char *dsec
, *arch
, *cp
, *name
, *path
;
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
);
560 while (NULL
!= (ff
= fts_read(f
))) {
561 path
= ff
->fts_path
+ 2;
563 * If we're a regular file, add an "of" by using the
564 * stored directory data and handling the filename.
565 * Disallow duplicate (hard-linked) files.
567 if (FTS_F
== ff
->fts_info
) {
568 if ( ! use_all
&& ff
->fts_level
< 2) {
569 WARNING(path
, base
, "Extraneous file");
571 } else if (inocheck(ff
->fts_statp
)) {
572 WARNING(path
, base
, "Duplicate file");
578 if (0 == strcmp(cp
, "mandocdb.db")) {
579 WARNING(path
, base
, "Skip database");
581 } else if (NULL
!= (cp
= strrchr(cp
, '.'))) {
582 if (0 == strcmp(cp
+ 1, "html")) {
583 WARNING(path
, base
, "Skip html");
585 } else if (0 == strcmp(cp
+ 1, "gz")) {
586 WARNING(path
, base
, "Skip gz");
588 } else if (0 == strcmp(cp
+ 1, "ps")) {
589 WARNING(path
, base
, "Skip ps");
591 } else if (0 == strcmp(cp
+ 1, "pdf")) {
592 WARNING(path
, base
, "Skip pdf");
597 if (NULL
!= (sec
= strrchr(ff
->fts_name
, '.'))) {
599 sec
= stradd(sec
+ 1);
601 name
= stradd(ff
->fts_name
);
602 ofadd(base
, dform
, path
,
603 name
, dsec
, sec
, arch
, ff
->fts_statp
);
605 } else if (FTS_D
!= ff
->fts_info
&&
606 FTS_DP
!= ff
->fts_info
)
609 switch (ff
->fts_level
) {
611 /* Ignore the root directory. */
615 * This might contain manX/ or catX/.
616 * Try to infer this from the name.
617 * If we're not in use_all, enforce it.
622 if (FTS_DP
== ff
->fts_info
)
625 if (0 == strncmp(cp
, "man", 3)) {
627 dsec
= stradd(cp
+ 3);
628 } else if (0 == strncmp(cp
, "cat", 3)) {
630 dsec
= stradd(cp
+ 3);
633 if (NULL
!= dsec
|| use_all
)
636 WARNING(path
, base
, "Unknown directory part");
637 fts_set(f
, ff
, FTS_SKIP
);
641 * Possibly our architecture.
642 * If we're descending, keep tabs on it.
645 if (FTS_DP
!= ff
->fts_info
&& NULL
!= dsec
)
646 arch
= stradd(ff
->fts_name
);
649 if (FTS_DP
== ff
->fts_info
|| use_all
)
651 WARNING(path
, base
, "Extraneous directory part");
652 fts_set(f
, ff
, FTS_SKIP
);
662 * Add a file to the file vector.
663 * Do not verify that it's a "valid" looking manpage (we'll do that
666 * Try to infer the manual section, architecture, and page name from the
667 * path, assuming it looks like
669 * [./]man*[/<arch>]/<name>.<section>
671 * [./]cat<section>[/<arch>]/<name>.0
673 * Stuff this information directly into the "of" vector.
674 * See treescan() for the fts(3) version of this.
677 filescan(const char *file
, const char *base
)
679 const char *sec
, *arch
, *name
, *dsec
;
680 char *p
, *start
, *buf
;
686 if (0 == strncmp(file
, "./", 2))
689 if (-1 == stat(file
, &st
)) {
690 WARNING(file
, base
, "%s", strerror(errno
));
692 } else if ( ! (S_IFREG
& st
.st_mode
)) {
693 WARNING(file
, base
, "Not a regular file");
695 } else if (inocheck(&st
)) {
696 WARNING(file
, base
, "Duplicate file");
700 buf
= mandoc_strdup(file
);
702 sec
= arch
= name
= dsec
= NULL
;
706 * First try to guess our directory structure.
707 * If we find a separator, try to look for man* or cat*.
708 * If we find one of these and what's underneath is a directory,
709 * assume it's an architecture.
711 if (NULL
!= (p
= strchr(start
, '/'))) {
713 if (0 == strncmp(start
, "man", 3)) {
716 } else if (0 == strncmp(start
, "cat", 3)) {
722 if (NULL
!= dsec
&& NULL
!= (p
= strchr(start
, '/'))) {
730 * Now check the file suffix.
731 * Suffix of `.0' indicates a catpage, `.1-9' is a manpage.
733 p
= strrchr(start
, '\0');
734 while (p
-- > start
&& '/' != *p
&& '.' != *p
)
743 * Now try to parse the name.
744 * Use the filename portion of the path.
747 if (NULL
!= (p
= strrchr(start
, '/'))) {
752 ofadd(base
, dform
, file
, name
, dsec
, sec
, arch
, &st
);
760 filecheck(const char *name
)
764 index
= ohash_qlookup(&filenames
, name
);
765 return(NULL
!= ohash_find(&filenames
, index
));
769 * Use the standard hashing mechanism (K&R) to see if the given filename
773 fileadd(struct of
*of
)
777 index
= ohash_qlookup(&filenames
, of
->file
);
778 assert(NULL
== ohash_find(&filenames
, index
));
779 ohash_insert(&filenames
, index
, of
);
786 inocheck(const struct stat
*st
)
792 memset(&id
, 0, sizeof(id
));
793 id
.ino
= hash
= st
->st_ino
;
795 index
= ohash_lookup_memory
796 (&inos
, (char *)&id
, sizeof(id
), hash
);
798 return(NULL
!= ohash_find(&inos
, index
));
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 index
= ohash_lookup_memory
815 (&inos
, (char *)&of
->id
, sizeof(of
->id
), hash
);
817 assert(NULL
== ohash_find(&inos
, index
));
818 ohash_insert(&inos
, index
, of
);
822 ofadd(const char *base
, int dform
, const char *file
,
823 const char *name
, const char *dsec
, const char *sec
,
824 const char *arch
, const struct stat
*st
)
829 assert(NULL
!= file
);
841 if (NULL
!= sec
&& *sec
<= '9' && *sec
>= '1')
843 else if (NULL
!= sec
&& *sec
== '0') {
848 of
= mandoc_calloc(1, sizeof(struct of
));
849 strlcpy(of
->file
, file
, MAXPATHLEN
);
860 * Add to unique identifier hash.
861 * Then if it's a source manual and we're going to use source in
862 * favour of catpages, add it to that hash.
873 while (NULL
!= (of
= ofs
)) {
880 * Run through the files in the global vector "ofs" and add them to the
881 * database specified in "base".
883 * This handles the parsing scheme itself, using the cues of directory
884 * and filename to determine whether the file is parsable or not.
887 ofmerge(struct mchars
*mc
, struct mparse
*mp
, const char *base
)
893 char buf
[MAXPATHLEN
];
895 const char *msec
, *march
, *mtitle
, *cp
;
897 enum mandoclevel lvl
;
899 for (of
= ofs
; NULL
!= of
; of
= of
->next
) {
901 * If we're a catpage (as defined by our path), then see
902 * if a manpage exists by the same name (ignoring the
904 * If it does, then we want to use it instead of our
907 if ( ! use_all
&& FORM_CAT
== of
->dform
) {
908 sz
= strlcpy(buf
, of
->file
, MAXPATHLEN
);
909 if (sz
>= MAXPATHLEN
) {
910 WARNING(of
->file
, base
,
911 "Filename too long");
914 bufp
= strstr(buf
, "cat");
915 assert(NULL
!= bufp
);
916 memcpy(bufp
, "man", 3);
917 if (NULL
!= (bufp
= strrchr(buf
, '.')))
919 strlcat(buf
, of
->dsec
, MAXPATHLEN
);
920 if (filecheck(buf
)) {
921 WARNING(of
->file
, base
, "Man "
922 "source exists: %s", buf
);
937 * Try interpreting the file as mdoc(7) or man(7)
938 * source code, unless it is already known to be
939 * formatted. Fall back to formatted mode.
941 if (FORM_SRC
== of
->dform
|| FORM_SRC
== of
->sform
) {
942 lvl
= mparse_readfd(mp
, -1, of
->file
);
943 if (lvl
< MANDOCLEVEL_FATAL
)
944 mparse_result(mp
, &mdoc
, &man
);
949 msec
= mdoc_meta(mdoc
)->msec
;
950 march
= mdoc_meta(mdoc
)->arch
;
951 mtitle
= mdoc_meta(mdoc
)->title
;
952 } else if (NULL
!= man
) {
954 msec
= man_meta(man
)->msec
;
956 mtitle
= man_meta(man
)->title
;
967 * Check whether the manual section given in a file
968 * agrees with the directory where the file is located.
969 * Some manuals have suffixes like (3p) on their
970 * section number either inside the file or in the
971 * directory name, some are linked into more than one
972 * section, like encrypt(1) = makekey(8). Do not skip
973 * manuals for such reasons.
975 if ( ! use_all
&& form
&& strcasecmp(msec
, of
->dsec
))
976 WARNING(of
->file
, base
, "Section \"%s\" "
977 "manual in %s directory",
981 * Manual page directories exist for each kernel
982 * architecture as returned by machine(1).
983 * However, many manuals only depend on the
984 * application architecture as returned by arch(1).
985 * For example, some (2/ARM) manuals are shared
986 * across the "armish" and "zaurus" kernel
988 * A few manuals are even shared across completely
989 * different architectures, for example fdformat(1)
990 * on amd64, i386, sparc, and sparc64.
991 * Thus, warn about architecture mismatches,
992 * but don't skip manuals for this reason.
994 if ( ! use_all
&& strcasecmp(march
, of
->arch
))
995 WARNING(of
->file
, base
, "Architecture \"%s\" "
996 "manual in \"%s\" directory",
999 putkey(of
, of
->name
, TYPE_Nm
);
1002 if (NULL
!= (cp
= mdoc_meta(mdoc
)->name
))
1003 putkey(of
, cp
, TYPE_Nm
);
1004 parse_mdoc(of
, mdoc_node(mdoc
));
1005 } else if (NULL
!= man
)
1006 parse_man(of
, man_node(man
));
1008 parse_catpage(of
, base
);
1010 dbindex(mc
, form
, of
, base
);
1017 parse_catpage(struct of
*of
, const char *base
)
1020 char *line
, *p
, *title
;
1021 size_t len
, plen
, titlesz
;
1023 if (NULL
== (stream
= fopen(of
->file
, "r"))) {
1024 WARNING(of
->file
, base
, "%s", strerror(errno
));
1028 /* Skip to first blank line. */
1030 while (NULL
!= (line
= fgetln(stream
, &len
)))
1035 * Assume the first line that is not indented
1036 * is the first section header. Skip to it.
1039 while (NULL
!= (line
= fgetln(stream
, &len
)))
1040 if ('\n' != *line
&& ' ' != *line
)
1044 * Read up until the next section into a buffer.
1045 * Strip the leading and trailing newline from each read line,
1046 * appending a trailing space.
1047 * Ignore empty (whitespace-only) lines.
1053 while (NULL
!= (line
= fgetln(stream
, &len
))) {
1054 if (' ' != *line
|| '\n' != line
[len
- 1])
1056 while (len
> 0 && isspace((unsigned char)*line
)) {
1062 title
= mandoc_realloc(title
, titlesz
+ len
);
1063 memcpy(title
+ titlesz
, line
, len
);
1065 title
[titlesz
- 1] = ' ';
1069 * If no page content can be found, or the input line
1070 * is already the next section header, or there is no
1071 * trailing newline, reuse the page title as the page
1075 if (NULL
== title
|| '\0' == *title
) {
1076 WARNING(of
->file
, base
, "Cannot find NAME section");
1082 title
= mandoc_realloc(title
, titlesz
+ 1);
1083 title
[titlesz
] = '\0';
1086 * Skip to the first dash.
1087 * Use the remaining line as the description (no more than 70
1091 if (NULL
!= (p
= strstr(title
, "- "))) {
1092 for (p
+= 2; ' ' == *p
|| '\b' == *p
; p
++)
1093 /* Skip to next word. */ ;
1095 WARNING(of
->file
, base
, "No dash in title line");
1101 /* Strip backspace-encoding from line. */
1103 while (NULL
!= (line
= memchr(p
, '\b', plen
))) {
1106 memmove(line
, line
+ 1, plen
--);
1109 memmove(line
- 1, line
+ 1, plen
- len
);
1113 of
->desc
= stradd(p
);
1114 putkey(of
, p
, TYPE_Nd
);
1120 * Put a type/word pair into the word database for this particular file.
1123 putkey(const struct of
*of
, const char *value
, uint64_t type
)
1126 assert(NULL
!= value
);
1127 wordaddbuf(of
, value
, strlen(value
), type
);
1131 * Like putkey() but for unterminated strings.
1134 putkeys(const struct of
*of
, const char *value
, int sz
, uint64_t type
)
1137 wordaddbuf(of
, value
, sz
, type
);
1141 * Grok all nodes at or below a certain mdoc node into putkey().
1144 putmdockey(const struct of
*of
, const struct mdoc_node
*n
, uint64_t m
)
1147 for ( ; NULL
!= n
; n
= n
->next
) {
1148 if (NULL
!= n
->child
)
1149 putmdockey(of
, n
->child
, m
);
1150 if (MDOC_TEXT
== n
->type
)
1151 putkey(of
, n
->string
, m
);
1156 parse_man(struct of
*of
, const struct man_node
*n
)
1158 const struct man_node
*head
, *body
;
1159 char *start
, *sv
, *title
;
1167 * We're only searching for one thing: the first text child in
1168 * the BODY of a NAME section. Since we don't keep track of
1169 * sections in -man, run some hoops to find out whether we're in
1170 * the correct section or not.
1173 if (MAN_BODY
== n
->type
&& MAN_SH
== n
->tok
) {
1175 assert(body
->parent
);
1176 if (NULL
!= (head
= body
->parent
->head
) &&
1177 1 == head
->nchild
&&
1178 NULL
!= (head
= (head
->child
)) &&
1179 MAN_TEXT
== head
->type
&&
1180 0 == strcmp(head
->string
, "NAME") &&
1181 NULL
!= (body
= body
->child
) &&
1182 MAN_TEXT
== body
->type
) {
1188 * Suck the entire NAME section into memory.
1189 * Yes, we might run away.
1190 * But too many manuals have big, spread-out
1191 * NAME sections over many lines.
1194 for ( ; NULL
!= body
; body
= body
->next
) {
1195 if (MAN_TEXT
!= body
->type
)
1197 if (0 == (sz
= strlen(body
->string
)))
1199 title
= mandoc_realloc
1200 (title
, titlesz
+ sz
+ 1);
1201 memcpy(title
+ titlesz
, body
->string
, sz
);
1203 title
[titlesz
- 1] = ' ';
1208 title
= mandoc_realloc(title
, titlesz
+ 1);
1209 title
[titlesz
] = '\0';
1211 /* Skip leading space. */
1214 while (isspace((unsigned char)*sv
))
1217 if (0 == (sz
= strlen(sv
))) {
1222 /* Erase trailing space. */
1224 start
= &sv
[sz
- 1];
1225 while (start
> sv
&& isspace((unsigned char)*start
))
1236 * Go through a special heuristic dance here.
1237 * Conventionally, one or more manual names are
1238 * comma-specified prior to a whitespace, then a
1239 * dash, then a description. Try to puzzle out
1240 * the name parts here.
1244 sz
= strcspn(start
, " ,");
1245 if ('\0' == start
[sz
])
1251 putkey(of
, start
, TYPE_Nm
);
1258 assert(',' == byte
);
1260 while (' ' == *start
)
1265 putkey(of
, start
, TYPE_Nm
);
1270 while (isspace((unsigned char)*start
))
1273 if (0 == strncmp(start
, "-", 1))
1275 else if (0 == strncmp(start
, "\\-\\-", 4))
1277 else if (0 == strncmp(start
, "\\-", 2))
1279 else if (0 == strncmp(start
, "\\(en", 4))
1281 else if (0 == strncmp(start
, "\\(em", 4))
1284 while (' ' == *start
)
1287 assert(NULL
== of
->desc
);
1288 of
->desc
= stradd(start
);
1289 putkey(of
, start
, TYPE_Nd
);
1295 for (n
= n
->child
; n
; n
= n
->next
)
1296 if (parse_man(of
, n
))
1303 parse_mdoc(struct of
*of
, const struct mdoc_node
*n
)
1307 for (n
= n
->child
; NULL
!= n
; n
= n
->next
) {
1318 if (NULL
!= mdocs
[n
->tok
].fp
)
1319 if (0 == (*mdocs
[n
->tok
].fp
)(of
, n
))
1322 if (MDOCF_CHILD
& mdocs
[n
->tok
].flags
)
1323 putmdockey(of
, n
->child
, mdocs
[n
->tok
].mask
);
1326 assert(MDOC_ROOT
!= n
->type
);
1329 if (NULL
!= n
->child
)
1335 parse_mdoc_Fd(struct of
*of
, const struct mdoc_node
*n
)
1337 const char *start
, *end
;
1340 if (SEC_SYNOPSIS
!= n
->sec
||
1341 NULL
== (n
= n
->child
) ||
1342 MDOC_TEXT
!= n
->type
)
1346 * Only consider those `Fd' macro fields that begin with an
1347 * "inclusion" token (versus, e.g., #define).
1350 if (strcmp("#include", n
->string
))
1353 if (NULL
== (n
= n
->next
) || MDOC_TEXT
!= n
->type
)
1357 * Strip away the enclosing angle brackets and make sure we're
1362 if ('<' == *start
|| '"' == *start
)
1365 if (0 == (sz
= strlen(start
)))
1368 end
= &start
[(int)sz
- 1];
1369 if ('>' == *end
|| '"' == *end
)
1373 putkeys(of
, start
, end
- start
+ 1, TYPE_In
);
1378 parse_mdoc_In(struct of
*of
, const struct mdoc_node
*n
)
1381 if (NULL
!= n
->child
&& MDOC_TEXT
== n
->child
->type
)
1384 putkey(of
, n
->child
->string
, TYPE_In
);
1389 parse_mdoc_Fn(struct of
*of
, const struct mdoc_node
*n
)
1393 if (NULL
== (n
= n
->child
) || MDOC_TEXT
!= n
->type
)
1397 * Parse: .Fn "struct type *name" "char *arg".
1398 * First strip away pointer symbol.
1399 * Then store the function name, then type.
1400 * Finally, store the arguments.
1403 if (NULL
== (cp
= strrchr(n
->string
, ' ')))
1409 putkey(of
, cp
, TYPE_Fn
);
1412 putkeys(of
, n
->string
, cp
- n
->string
, TYPE_Ft
);
1414 for (n
= n
->next
; NULL
!= n
; n
= n
->next
)
1415 if (MDOC_TEXT
== n
->type
)
1416 putkey(of
, n
->string
, TYPE_Fa
);
1422 parse_mdoc_St(struct of
*of
, const struct mdoc_node
*n
)
1425 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
1428 putkey(of
, n
->child
->string
, TYPE_St
);
1433 parse_mdoc_Xr(struct of
*of
, const struct mdoc_node
*n
)
1436 if (NULL
== (n
= n
->child
))
1439 putkey(of
, n
->string
, TYPE_Xr
);
1444 parse_mdoc_Nd(struct of
*of
, const struct mdoc_node
*n
)
1449 if (MDOC_BODY
!= n
->type
)
1453 * Special-case the `Nd' because we need to put the description
1454 * into the document table.
1458 for (n
= n
->child
; NULL
!= n
; n
= n
->next
) {
1459 if (MDOC_TEXT
== n
->type
) {
1460 sz
= strlen(n
->string
) + 1;
1461 if (NULL
!= (sv
= desc
))
1462 sz
+= strlen(desc
) + 1;
1463 desc
= mandoc_realloc(desc
, sz
);
1465 strlcat(desc
, " ", sz
);
1468 strlcat(desc
, n
->string
, sz
);
1470 if (NULL
!= n
->child
)
1471 parse_mdoc_Nd(of
, n
);
1474 of
->desc
= NULL
!= desc
? stradd(desc
) : NULL
;
1480 parse_mdoc_Nm(struct of
*of
, const struct mdoc_node
*n
)
1483 if (SEC_NAME
== n
->sec
)
1485 else if (SEC_SYNOPSIS
!= n
->sec
|| MDOC_HEAD
!= n
->type
)
1492 parse_mdoc_Sh(struct of
*of
, const struct mdoc_node
*n
)
1495 return(SEC_CUSTOM
== n
->sec
&& MDOC_HEAD
== n
->type
);
1499 parse_mdoc_head(struct of
*of
, const struct mdoc_node
*n
)
1502 return(MDOC_HEAD
== n
->type
);
1506 parse_mdoc_body(struct of
*of
, const struct mdoc_node
*n
)
1509 return(MDOC_BODY
== n
->type
);
1516 stradd(const char *cp
)
1519 return(straddbuf(cp
, strlen(cp
)));
1523 * This looks up or adds a string to the string table.
1524 * The string table is a table of all strings encountered during parse
1526 * In using it, we avoid having thousands of (e.g.) "cat1" string
1527 * allocations for the "of" table.
1528 * We also have a layer atop the string table for keeping track of words
1529 * in a parse sequence (see wordaddbuf()).
1532 straddbuf(const char *cp
, size_t sz
)
1538 if (NULL
!= (s
= hashget(cp
, sz
)))
1541 s
= mandoc_calloc(sizeof(struct str
) + sz
, 1);
1542 memcpy(s
->key
, cp
, sz
);
1545 index
= ohash_qlookupi(&strings
, cp
, &end
);
1546 assert(NULL
== ohash_find(&strings
, index
));
1547 ohash_insert(&strings
, index
, s
);
1552 hashget(const char *cp
, size_t sz
)
1558 index
= ohash_qlookupi(&strings
, cp
, &end
);
1559 return(ohash_find(&strings
, index
));
1563 * Add a word to the current parse sequence.
1564 * Within the hashtable of strings, we maintain a list of strings that
1565 * are currently indexed.
1566 * Each of these ("words") has a bitmask modified within the parse.
1567 * When we finish a parse, we'll dump the list, then remove the head
1568 * entry -- since the next parse will have a new "of", it can keep track
1569 * of its entries without conflict.
1572 wordaddbuf(const struct of
*of
,
1573 const char *cp
, size_t sz
, uint64_t v
)
1582 s
= hashget(cp
, sz
);
1584 if (NULL
!= s
&& of
== s
->of
) {
1587 } else if (NULL
== s
) {
1588 s
= mandoc_calloc(sizeof(struct str
) + sz
, 1);
1589 memcpy(s
->key
, cp
, sz
);
1591 index
= ohash_qlookupi(&strings
, cp
, &end
);
1592 assert(NULL
== ohash_find(&strings
, index
));
1593 ohash_insert(&strings
, index
, s
);
1603 * Take a Unicode codepoint and produce its UTF-8 encoding.
1604 * This isn't the best way to do this, but it works.
1605 * The magic numbers are from the UTF-8 packaging.
1606 * They're not as scary as they seem: read the UTF-8 spec for details.
1609 utf8(unsigned int cp
, char out
[7])
1614 if (cp
<= 0x0000007F) {
1617 } else if (cp
<= 0x000007FF) {
1619 out
[0] = (cp
>> 6 & 31) | 192;
1620 out
[1] = (cp
& 63) | 128;
1621 } else if (cp
<= 0x0000FFFF) {
1623 out
[0] = (cp
>> 12 & 15) | 224;
1624 out
[1] = (cp
>> 6 & 63) | 128;
1625 out
[2] = (cp
& 63) | 128;
1626 } else if (cp
<= 0x001FFFFF) {
1628 out
[0] = (cp
>> 18 & 7) | 240;
1629 out
[1] = (cp
>> 12 & 63) | 128;
1630 out
[2] = (cp
>> 6 & 63) | 128;
1631 out
[3] = (cp
& 63) | 128;
1632 } else if (cp
<= 0x03FFFFFF) {
1634 out
[0] = (cp
>> 24 & 3) | 248;
1635 out
[1] = (cp
>> 18 & 63) | 128;
1636 out
[2] = (cp
>> 12 & 63) | 128;
1637 out
[3] = (cp
>> 6 & 63) | 128;
1638 out
[4] = (cp
& 63) | 128;
1639 } else if (cp
<= 0x7FFFFFFF) {
1641 out
[0] = (cp
>> 30 & 1) | 252;
1642 out
[1] = (cp
>> 24 & 63) | 128;
1643 out
[2] = (cp
>> 18 & 63) | 128;
1644 out
[3] = (cp
>> 12 & 63) | 128;
1645 out
[4] = (cp
>> 6 & 63) | 128;
1646 out
[5] = (cp
& 63) | 128;
1655 * Store the UTF-8 version of a key, or alias the pointer if the key has
1656 * no UTF-8 transcription marks in it.
1659 utf8key(struct mchars
*mc
, struct str
*key
)
1661 size_t sz
, bsz
, pos
;
1662 char utfbuf
[7], res
[5];
1664 const char *seq
, *cpp
, *val
;
1666 enum mandoc_esc esc
;
1668 assert(NULL
== key
->utf8
);
1672 res
[2] = ASCII_NBRSP
;
1673 res
[3] = ASCII_HYPH
;
1680 * Pre-check: if we have no stop-characters, then set the
1681 * pointer as ourselvse and get out of here.
1683 if (strcspn(val
, res
) == bsz
) {
1684 key
->utf8
= key
->key
;
1688 /* Pre-allocate by the length of the input */
1690 buf
= mandoc_malloc(++bsz
);
1693 while ('\0' != *val
) {
1695 * Halt on the first escape sequence.
1696 * This also halts on the end of string, in which case
1697 * we just copy, fallthrough, and exit the loop.
1699 if ((sz
= strcspn(val
, res
)) > 0) {
1700 memcpy(&buf
[pos
], val
, sz
);
1705 if (ASCII_HYPH
== *val
) {
1709 } else if ('\t' == *val
|| ASCII_NBRSP
== *val
) {
1713 } else if ('\\' != *val
)
1716 /* Read past the slash. */
1722 * Parse the escape sequence and see if it's a
1723 * predefined character or special character.
1726 ((const char **)&val
, &seq
, &len
);
1727 if (ESCAPE_ERROR
== esc
)
1730 if (ESCAPE_SPECIAL
!= esc
)
1732 if (0 == (u
= mchars_spec2cp(mc
, seq
, len
)))
1736 * If we have a Unicode codepoint, try to convert that
1737 * to a UTF-8 byte string.
1740 if (0 == (sz
= utf8(u
, utfbuf
)))
1743 /* Copy the rendered glyph into the stream. */
1748 buf
= mandoc_realloc(buf
, bsz
);
1750 memcpy(&buf
[pos
], cpp
, sz
);
1759 * Flush the current page's terms (and their bits) into the database.
1760 * Wrap the entire set of additions in a transaction to make sqlite be a
1762 * Also, UTF-8-encode the description at the last possible moment.
1765 dbindex(struct mchars
*mc
, int form
,
1766 const struct of
*of
, const char *base
)
1772 DEBUG(of
->file
, base
, "Adding to index");
1778 if (NULL
!= of
->desc
) {
1779 key
= hashget(of
->desc
, strlen(of
->desc
));
1780 assert(NULL
!= key
);
1781 if (NULL
== key
->utf8
)
1786 sqlite3_exec(db
, "BEGIN TRANSACTION", NULL
, NULL
, NULL
);
1789 (stmts
[STMT_INSERT_DOC
], 1,
1790 of
->file
, -1, SQLITE_STATIC
);
1792 (stmts
[STMT_INSERT_DOC
], 2,
1793 of
->sec
, -1, SQLITE_STATIC
);
1795 (stmts
[STMT_INSERT_DOC
], 3,
1796 of
->arch
, -1, SQLITE_STATIC
);
1798 (stmts
[STMT_INSERT_DOC
], 4,
1799 desc
, -1, SQLITE_STATIC
);
1801 (stmts
[STMT_INSERT_DOC
], 5, form
);
1802 sqlite3_step(stmts
[STMT_INSERT_DOC
]);
1803 recno
= sqlite3_last_insert_rowid(db
);
1804 sqlite3_reset(stmts
[STMT_INSERT_DOC
]);
1806 for (key
= words
; NULL
!= key
; key
= key
->next
) {
1807 assert(key
->of
== of
);
1808 if (NULL
== key
->utf8
)
1811 (stmts
[STMT_INSERT_KEY
], 1, key
->mask
);
1813 (stmts
[STMT_INSERT_KEY
], 2,
1814 key
->utf8
, -1, SQLITE_STATIC
);
1816 (stmts
[STMT_INSERT_KEY
], 3, recno
);
1817 sqlite3_step(stmts
[STMT_INSERT_KEY
]);
1818 sqlite3_reset(stmts
[STMT_INSERT_KEY
]);
1821 sqlite3_exec(db
, "COMMIT TRANSACTION", NULL
, NULL
, NULL
);
1826 dbprune(const char *base
)
1833 for (of
= ofs
; NULL
!= of
; of
= of
->next
) {
1835 (stmts
[STMT_DELETE
], 1,
1836 of
->file
, -1, SQLITE_STATIC
);
1837 sqlite3_step(stmts
[STMT_DELETE
]);
1838 sqlite3_reset(stmts
[STMT_DELETE
]);
1839 DEBUG(of
->file
, base
, "Deleted from index");
1844 * Close an existing database and its prepared statements.
1845 * If "real" is not set, rename the temporary file into the real one.
1848 dbclose(const char *base
, int real
)
1851 char file
[MAXPATHLEN
];
1856 for (i
= 0; i
< STMT__MAX
; i
++) {
1857 sqlite3_finalize(stmts
[i
]);
1867 strlcpy(file
, MANDOC_DB
, MAXPATHLEN
);
1868 strlcat(file
, "~", MAXPATHLEN
);
1869 if (-1 == rename(file
, MANDOC_DB
))
1874 * This is straightforward stuff.
1875 * Open a database connection to a "temporary" database, then open a set
1876 * of prepared statements we'll use over and over again.
1877 * If "real" is set, we use the existing database; if not, we truncate a
1879 * Must be matched by dbclose().
1882 dbopen(const char *base
, int real
)
1884 char file
[MAXPATHLEN
];
1892 sz
= strlcpy(file
, MANDOC_DB
, MAXPATHLEN
);
1894 sz
= strlcat(file
, "~", MAXPATHLEN
);
1896 if (sz
>= MAXPATHLEN
) {
1897 fprintf(stderr
, "%s: Path too long\n", file
);
1904 ofl
= SQLITE_OPEN_PRIVATECACHE
| SQLITE_OPEN_READWRITE
|
1905 (0 == real
? SQLITE_OPEN_EXCLUSIVE
: 0);
1907 rc
= sqlite3_open_v2(file
, &db
, ofl
, NULL
);
1908 if (SQLITE_OK
== rc
)
1910 if (SQLITE_CANTOPEN
!= rc
) {
1918 if (SQLITE_OK
!= (rc
= sqlite3_open(file
, &db
))) {
1923 sql
= "CREATE TABLE \"docs\" (\n"
1924 " \"file\" TEXT NOT NULL,\n"
1925 " \"sec\" TEXT NOT NULL,\n"
1926 " \"arch\" TEXT NOT NULL,\n"
1927 " \"desc\" TEXT NOT NULL,\n"
1928 " \"form\" INTEGER NOT NULL,\n"
1929 " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
1932 "CREATE TABLE \"keys\" (\n"
1933 " \"bits\" INTEGER NOT NULL,\n"
1934 " \"key\" TEXT NOT NULL,\n"
1935 " \"docid\" INTEGER NOT NULL REFERENCES docs(id) "
1936 "ON DELETE CASCADE,\n"
1937 " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
1940 "CREATE INDEX \"key_index\" ON keys (key);\n";
1942 if (SQLITE_OK
!= sqlite3_exec(db
, sql
, NULL
, NULL
, NULL
)) {
1943 perror(sqlite3_errmsg(db
));
1947 sql
= "DELETE FROM docs where file=?";
1948 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_DELETE
], NULL
);
1949 sql
= "INSERT INTO docs "
1950 "(file,sec,arch,desc,form) VALUES (?,?,?,?,?)";
1951 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_DOC
], NULL
);
1952 sql
= "INSERT INTO keys "
1953 "(bits,key,docid) VALUES (?,?,?)";
1954 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_KEY
], NULL
);
1959 hash_halloc(size_t sz
, void *arg
)
1962 return(mandoc_calloc(sz
, 1));
1966 hash_alloc(size_t sz
, void *arg
)
1969 return(mandoc_malloc(sz
));
1973 hash_free(void *p
, size_t sz
, void *arg
)
1980 path_reset(const char *cwd
, int fd
, const char *base
)
1983 if (-1 == fchdir(fd
)) {
1986 } else if (-1 == chdir(base
)) {