]>
git.cameronkatri.com Git - mandoc.git/blob - mandocdb.c
1 /* $Id: mandocdb.c,v 1.55 2012/06/09 14:11:16 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>
40 #include "compat_ohash.h"
48 #include "mansearch.h"
50 /* Post a warning to stderr. */
51 #define WARNING(_f, _b, _fmt, _args...) \
53 fprintf(stderr, "%s: ", (_b)); \
54 fprintf(stderr, (_fmt), ##_args); \
56 fprintf(stderr, ": %s", (_f)); \
57 fprintf(stderr, "\n"); \
58 } while (/* CONSTCOND */ 0)
59 /* Post a "verbose" message to stderr. */
60 #define DEBUG(_f, _b, _fmt, _args...) \
62 fprintf(stderr, "%s: ", (_b)); \
63 fprintf(stderr, (_fmt), ##_args); \
64 fprintf(stderr, ": %s\n", (_f)); \
65 } while (/* CONSTCOND */ 0)
67 #define SQL_EXEC(_v) \
68 if (SQLITE_OK != sqlite3_exec(db, (_v), NULL, NULL, NULL)) \
69 fprintf(stderr, "%s\n", sqlite3_errmsg(db))
70 #define SQL_BIND_TEXT(_s, _i, _v) \
71 if (SQLITE_OK != sqlite3_bind_text \
72 ((_s), (_i)++, (_v), -1, SQLITE_STATIC)) \
73 fprintf(stderr, "%s\n", sqlite3_errmsg(db))
74 #define SQL_BIND_INT(_s, _i, _v) \
75 if (SQLITE_OK != sqlite3_bind_int \
76 ((_s), (_i)++, (_v))) \
77 fprintf(stderr, "%s\n", sqlite3_errmsg(db))
78 #define SQL_BIND_INT64(_s, _i, _v) \
79 if (SQLITE_OK != sqlite3_bind_int64 \
80 ((_s), (_i)++, (_v))) \
81 fprintf(stderr, "%s\n", sqlite3_errmsg(db))
82 #define SQL_STEP(_s) \
83 if (SQLITE_DONE != sqlite3_step((_s))) \
84 fprintf(stderr, "%s\n", sqlite3_errmsg(db))
87 OP_DEFAULT
= 0, /* new dbs from dir list or default config */
88 OP_CONFFILE
, /* new databases from custom config file */
89 OP_UPDATE
, /* delete/add entries in existing database */
90 OP_DELETE
, /* delete entries from existing database */
91 OP_TEST
/* change no databases, report potential problems */
95 FORM_SRC
, /* format is -man or -mdoc */
96 FORM_CAT
, /* format is cat */
97 FORM_NONE
/* format is unknown */
101 char *utf8
; /* key in UTF-8 form */
102 const struct of
*of
; /* if set, the owning parse */
103 struct str
*next
; /* next in owning parse sequence */
104 uint64_t mask
; /* bitmask in sequence */
105 char key
[]; /* the string itself */
114 struct id id
; /* used for hashing routine */
115 struct of
*next
; /* next in ofs */
116 enum form dform
; /* path-cued form */
117 enum form sform
; /* suffix-cued form */
118 char file
[MAXPATHLEN
]; /* filename rel. to manpath */
119 const char *desc
; /* parsed description */
120 const char *sec
; /* suffix-cued section (or empty) */
121 const char *dsec
; /* path-cued section (or empty) */
122 const char *arch
; /* path-cued arch. (or empty) */
123 const char *name
; /* name (from filename) (not empty) */
127 STMT_DELETE
= 0, /* delete manpage */
128 STMT_INSERT_DOC
, /* insert manpage */
129 STMT_INSERT_KEY
, /* insert parsed key */
133 typedef int (*mdoc_fp
)(struct of
*, const struct mdoc_node
*);
135 struct mdoc_handler
{
136 mdoc_fp fp
; /* optional handler */
137 uint64_t mask
; /* set unless handler returns 0 */
138 int flags
; /* for use by pmdoc_node */
139 #define MDOCF_CHILD 0x01 /* automatically index child nodes */
142 static void dbclose(const char *, int);
143 static void dbindex(struct mchars
*, int,
144 const struct of
*, const char *);
145 static int dbopen(const char *, int);
146 static void dbprune(const char *);
147 static void fileadd(struct of
*);
148 static int filecheck(const char *);
149 static void filescan(const char *, const char *);
150 static struct str
*hashget(const char *, size_t);
151 static void *hash_alloc(size_t, void *);
152 static void hash_free(void *, size_t, void *);
153 static void *hash_halloc(size_t, void *);
154 static void inoadd(const struct stat
*, struct of
*);
155 static int inocheck(const struct stat
*);
156 static void ofadd(const char *, int, const char *,
157 const char *, const char *, const char *,
158 const char *, const struct stat
*);
159 static void offree(void);
160 static int ofmerge(struct mchars
*, struct mparse
*, const char *);
161 static void parse_catpage(struct of
*, const char *);
162 static int parse_man(struct of
*,
163 const struct man_node
*);
164 static void parse_mdoc(struct of
*, const struct mdoc_node
*);
165 static int parse_mdoc_body(struct of
*, const struct mdoc_node
*);
166 static int parse_mdoc_head(struct of
*, const struct mdoc_node
*);
167 static int parse_mdoc_Fd(struct of
*, const struct mdoc_node
*);
168 static int parse_mdoc_Fn(struct of
*, const struct mdoc_node
*);
169 static int parse_mdoc_In(struct of
*, const struct mdoc_node
*);
170 static int parse_mdoc_Nd(struct of
*, const struct mdoc_node
*);
171 static int parse_mdoc_Nm(struct of
*, const struct mdoc_node
*);
172 static int parse_mdoc_Sh(struct of
*, const struct mdoc_node
*);
173 static int parse_mdoc_St(struct of
*, const struct mdoc_node
*);
174 static int parse_mdoc_Xr(struct of
*, const struct mdoc_node
*);
175 static int path_reset(const char *, int, const char *);
176 static void putkey(const struct of
*,
177 const char *, uint64_t);
178 static void putkeys(const struct of
*,
179 const char *, int, uint64_t);
180 static void putmdockey(const struct of
*,
181 const struct mdoc_node
*, uint64_t);
182 static char *stradd(const char *);
183 static char *straddbuf(const char *, size_t);
184 static int treescan(const char *);
185 static size_t utf8(unsigned int, char [7]);
186 static void utf8key(struct mchars
*, struct str
*);
187 static void wordaddbuf(const struct of
*,
188 const char *, size_t, uint64_t);
190 static char *progname
;
191 static int use_all
; /* use all found files */
192 static int nodb
; /* no database changes */
193 static int verb
; /* print what we're doing */
194 static int warnings
; /* warn about crap */
195 static enum op op
; /* operational mode */
196 static struct ohash inos
; /* table of inodes/devices */
197 static struct ohash filenames
; /* table of filenames */
198 static struct ohash strings
; /* table of all strings */
199 static struct of
*ofs
= NULL
; /* vector of files to parse */
200 static struct str
*words
= NULL
; /* word list in current parse */
201 static sqlite3
*db
= NULL
; /* current database */
202 static sqlite3_stmt
*stmts
[STMT__MAX
]; /* current statements */
204 static const struct mdoc_handler mdocs
[MDOC_MAX
] = {
205 { NULL
, 0, 0 }, /* Ap */
206 { NULL
, 0, 0 }, /* Dd */
207 { NULL
, 0, 0 }, /* Dt */
208 { NULL
, 0, 0 }, /* Os */
209 { parse_mdoc_Sh
, TYPE_Sh
, MDOCF_CHILD
}, /* Sh */
210 { parse_mdoc_head
, TYPE_Ss
, MDOCF_CHILD
}, /* Ss */
211 { NULL
, 0, 0 }, /* Pp */
212 { NULL
, 0, 0 }, /* D1 */
213 { NULL
, 0, 0 }, /* Dl */
214 { NULL
, 0, 0 }, /* Bd */
215 { NULL
, 0, 0 }, /* Ed */
216 { NULL
, 0, 0 }, /* Bl */
217 { NULL
, 0, 0 }, /* El */
218 { NULL
, 0, 0 }, /* It */
219 { NULL
, 0, 0 }, /* Ad */
220 { NULL
, TYPE_An
, MDOCF_CHILD
}, /* An */
221 { NULL
, TYPE_Ar
, MDOCF_CHILD
}, /* Ar */
222 { NULL
, TYPE_Cd
, MDOCF_CHILD
}, /* Cd */
223 { NULL
, TYPE_Cm
, MDOCF_CHILD
}, /* Cm */
224 { NULL
, TYPE_Dv
, MDOCF_CHILD
}, /* Dv */
225 { NULL
, TYPE_Er
, MDOCF_CHILD
}, /* Er */
226 { NULL
, TYPE_Ev
, MDOCF_CHILD
}, /* Ev */
227 { NULL
, 0, 0 }, /* Ex */
228 { NULL
, TYPE_Fa
, MDOCF_CHILD
}, /* Fa */
229 { parse_mdoc_Fd
, TYPE_In
, 0 }, /* Fd */
230 { NULL
, TYPE_Fl
, MDOCF_CHILD
}, /* Fl */
231 { parse_mdoc_Fn
, 0, 0 }, /* Fn */
232 { NULL
, TYPE_Ft
, MDOCF_CHILD
}, /* Ft */
233 { NULL
, TYPE_Ic
, MDOCF_CHILD
}, /* Ic */
234 { parse_mdoc_In
, TYPE_In
, MDOCF_CHILD
}, /* In */
235 { NULL
, TYPE_Li
, MDOCF_CHILD
}, /* Li */
236 { parse_mdoc_Nd
, TYPE_Nd
, MDOCF_CHILD
}, /* Nd */
237 { parse_mdoc_Nm
, TYPE_Nm
, MDOCF_CHILD
}, /* Nm */
238 { NULL
, 0, 0 }, /* Op */
239 { NULL
, 0, 0 }, /* Ot */
240 { NULL
, TYPE_Pa
, MDOCF_CHILD
}, /* Pa */
241 { NULL
, 0, 0 }, /* Rv */
242 { parse_mdoc_St
, TYPE_St
, 0 }, /* St */
243 { NULL
, TYPE_Va
, MDOCF_CHILD
}, /* Va */
244 { parse_mdoc_body
, TYPE_Va
, MDOCF_CHILD
}, /* Vt */
245 { parse_mdoc_Xr
, TYPE_Xr
, 0 }, /* Xr */
246 { NULL
, 0, 0 }, /* %A */
247 { NULL
, 0, 0 }, /* %B */
248 { NULL
, 0, 0 }, /* %D */
249 { NULL
, 0, 0 }, /* %I */
250 { NULL
, 0, 0 }, /* %J */
251 { NULL
, 0, 0 }, /* %N */
252 { NULL
, 0, 0 }, /* %O */
253 { NULL
, 0, 0 }, /* %P */
254 { NULL
, 0, 0 }, /* %R */
255 { NULL
, 0, 0 }, /* %T */
256 { NULL
, 0, 0 }, /* %V */
257 { NULL
, 0, 0 }, /* Ac */
258 { NULL
, 0, 0 }, /* Ao */
259 { NULL
, 0, 0 }, /* Aq */
260 { NULL
, TYPE_At
, MDOCF_CHILD
}, /* At */
261 { NULL
, 0, 0 }, /* Bc */
262 { NULL
, 0, 0 }, /* Bf */
263 { NULL
, 0, 0 }, /* Bo */
264 { NULL
, 0, 0 }, /* Bq */
265 { NULL
, TYPE_Bsx
, MDOCF_CHILD
}, /* Bsx */
266 { NULL
, TYPE_Bx
, MDOCF_CHILD
}, /* Bx */
267 { NULL
, 0, 0 }, /* Db */
268 { NULL
, 0, 0 }, /* Dc */
269 { NULL
, 0, 0 }, /* Do */
270 { NULL
, 0, 0 }, /* Dq */
271 { NULL
, 0, 0 }, /* Ec */
272 { NULL
, 0, 0 }, /* Ef */
273 { NULL
, TYPE_Em
, MDOCF_CHILD
}, /* Em */
274 { NULL
, 0, 0 }, /* Eo */
275 { NULL
, TYPE_Fx
, MDOCF_CHILD
}, /* Fx */
276 { NULL
, TYPE_Ms
, MDOCF_CHILD
}, /* Ms */
277 { NULL
, 0, 0 }, /* No */
278 { NULL
, 0, 0 }, /* Ns */
279 { NULL
, TYPE_Nx
, MDOCF_CHILD
}, /* Nx */
280 { NULL
, TYPE_Ox
, MDOCF_CHILD
}, /* Ox */
281 { NULL
, 0, 0 }, /* Pc */
282 { NULL
, 0, 0 }, /* Pf */
283 { NULL
, 0, 0 }, /* Po */
284 { NULL
, 0, 0 }, /* Pq */
285 { NULL
, 0, 0 }, /* Qc */
286 { NULL
, 0, 0 }, /* Ql */
287 { NULL
, 0, 0 }, /* Qo */
288 { NULL
, 0, 0 }, /* Qq */
289 { NULL
, 0, 0 }, /* Re */
290 { NULL
, 0, 0 }, /* Rs */
291 { NULL
, 0, 0 }, /* Sc */
292 { NULL
, 0, 0 }, /* So */
293 { NULL
, 0, 0 }, /* Sq */
294 { NULL
, 0, 0 }, /* Sm */
295 { NULL
, 0, 0 }, /* Sx */
296 { NULL
, TYPE_Sy
, MDOCF_CHILD
}, /* Sy */
297 { NULL
, TYPE_Tn
, MDOCF_CHILD
}, /* Tn */
298 { NULL
, 0, 0 }, /* Ux */
299 { NULL
, 0, 0 }, /* Xc */
300 { NULL
, 0, 0 }, /* Xo */
301 { parse_mdoc_head
, TYPE_Fn
, 0 }, /* Fo */
302 { NULL
, 0, 0 }, /* Fc */
303 { NULL
, 0, 0 }, /* Oo */
304 { NULL
, 0, 0 }, /* Oc */
305 { NULL
, 0, 0 }, /* Bk */
306 { NULL
, 0, 0 }, /* Ek */
307 { NULL
, 0, 0 }, /* Bt */
308 { NULL
, 0, 0 }, /* Hf */
309 { NULL
, 0, 0 }, /* Fr */
310 { NULL
, 0, 0 }, /* Ud */
311 { NULL
, TYPE_Lb
, MDOCF_CHILD
}, /* Lb */
312 { NULL
, 0, 0 }, /* Lp */
313 { NULL
, TYPE_Lk
, MDOCF_CHILD
}, /* Lk */
314 { NULL
, TYPE_Mt
, MDOCF_CHILD
}, /* Mt */
315 { NULL
, 0, 0 }, /* Brq */
316 { NULL
, 0, 0 }, /* Bro */
317 { NULL
, 0, 0 }, /* Brc */
318 { NULL
, 0, 0 }, /* %C */
319 { NULL
, 0, 0 }, /* Es */
320 { NULL
, 0, 0 }, /* En */
321 { NULL
, TYPE_Dx
, MDOCF_CHILD
}, /* Dx */
322 { NULL
, 0, 0 }, /* %Q */
323 { NULL
, 0, 0 }, /* br */
324 { NULL
, 0, 0 }, /* sp */
325 { NULL
, 0, 0 }, /* %U */
326 { NULL
, 0, 0 }, /* Ta */
330 main(int argc
, char *argv
[])
332 char cwd
[MAXPATHLEN
];
339 struct manpaths dirs
;
341 struct ohash_info ino_info
, filename_info
, str_info
;
343 memset(stmts
, 0, STMT__MAX
* sizeof(sqlite3_stmt
*));
344 memset(&dirs
, 0, sizeof(struct manpaths
));
346 ino_info
.halloc
= filename_info
.halloc
=
347 str_info
.halloc
= hash_halloc
;
348 ino_info
.hfree
= filename_info
.hfree
=
349 str_info
.hfree
= hash_free
;
350 ino_info
.alloc
= filename_info
.alloc
=
351 str_info
.alloc
= hash_alloc
;
353 ino_info
.key_offset
= offsetof(struct of
, id
);
354 filename_info
.key_offset
= offsetof(struct of
, file
);
355 str_info
.key_offset
= offsetof(struct str
, key
);
357 progname
= strrchr(argv
[0], '/');
358 if (progname
== NULL
)
364 * Remember where we started by keeping a fd open to the origin
365 * path component: throughout this utility, we chdir() a lot to
366 * handle relative paths, and by doing this, we can return to
367 * the starting point.
369 if (NULL
== getcwd(cwd
, MAXPATHLEN
)) {
371 return(EXIT_FAILURE
);
372 } else if (-1 == (fd
= open(cwd
, O_RDONLY
, 0))) {
374 return(EXIT_FAILURE
);
378 * We accept a few different invocations.
379 * The CHECKOP macro makes sure that invocation styles don't
380 * clobber each other.
382 #define CHECKOP(_op, _ch) do \
383 if (OP_DEFAULT != (_op)) { \
384 fprintf(stderr, "-%c: Conflicting option\n", (_ch)); \
386 } while (/*CONSTCOND*/0)
391 while (-1 != (ch
= getopt(argc
, argv
, "aC:d:ntu:vW")))
411 dup2(STDOUT_FILENO
, STDERR_FILENO
);
433 if (OP_CONFFILE
== op
&& argc
> 0) {
434 fprintf(stderr
, "-C: Too many arguments\n");
439 mp
= mparse_alloc(MPARSE_AUTO
,
440 MANDOCLEVEL_FATAL
, NULL
, NULL
, NULL
);
443 ohash_init(&strings
, 6, &str_info
);
444 ohash_init(&inos
, 6, &ino_info
);
445 ohash_init(&filenames
, 6, &filename_info
);
447 if (OP_UPDATE
== op
|| OP_DELETE
== op
|| OP_TEST
== op
) {
449 * Force processing all files.
455 * All of these deal with a specific directory.
456 * Jump into that directory then collect files specified
457 * on the command-line.
459 if (0 == path_reset(cwd
, fd
, dir
))
461 for (i
= 0; i
< argc
; i
++)
462 filescan(argv
[i
], dir
);
463 if (0 == dbopen(dir
, 1))
468 rc
= ofmerge(mc
, mp
, dir
);
472 * If we have arguments, use them as our manpaths.
473 * If we don't, grok from manpath(1) or however else
474 * manpath_parse() wants to do it.
477 dirs
.paths
= mandoc_calloc
478 (argc
, sizeof(char *));
479 dirs
.sz
= (size_t)argc
;
480 for (i
= 0; i
< argc
; i
++)
481 dirs
.paths
[i
] = mandoc_strdup(argv
[i
]);
483 manpath_parse(&dirs
, dir
, NULL
, NULL
);
486 * First scan the tree rooted at a base directory.
487 * Then whak its database (if one exists), parse, and
488 * build up the database.
489 * Ignore zero-length directories and strip trailing
492 for (j
= 0; j
< dirs
.sz
; j
++) {
493 sz
= strlen(dirs
.paths
[j
]);
494 if (sz
&& '/' == dirs
.paths
[j
][sz
- 1])
495 dirs
.paths
[j
][--sz
] = '\0';
498 if (0 == path_reset(cwd
, fd
, dirs
.paths
[j
]))
500 if (0 == treescan(dirs
.paths
[j
]))
502 if (0 == path_reset(cwd
, fd
, dirs
.paths
[j
]))
504 if (0 == dbopen(dirs
.paths
[j
], 0))
508 * Since we're opening up a new database, we can
509 * turn off synchronous mode for much better
513 SQL_EXEC("PRAGMA synchronous = OFF");
516 if (0 == ofmerge(mc
, mp
, dirs
.paths
[j
]))
518 dbclose(dirs
.paths
[j
], 0);
521 ohash_init(&inos
, 6, &ino_info
);
522 ohash_delete(&filenames
);
523 ohash_init(&filenames
, 6, &filename_info
);
531 for (s
= ohash_first(&strings
, &index
);
532 NULL
!= s
; s
= ohash_next(&strings
, &index
)) {
533 if (s
->utf8
!= s
->key
)
537 ohash_delete(&strings
);
539 ohash_delete(&filenames
);
541 return(rc
? EXIT_SUCCESS
: EXIT_FAILURE
);
543 fprintf(stderr
, "usage: %s [-anvW] [-C file]\n"
544 " %s [-anvW] dir ...\n"
545 " %s [-nvW] -d dir [file ...]\n"
546 " %s [-nvW] -u dir [file ...]\n"
548 progname
, progname
, progname
,
551 return(EXIT_FAILURE
);
555 * Scan a directory tree rooted at "base" for manpages.
556 * We use fts(), scanning directory parts along the way for clues to our
557 * section and architecture.
559 * If use_all has been specified, grok all files.
560 * If not, sanitise paths to the following:
562 * [./]man*[/<arch>]/<name>.<section>
564 * [./]cat<section>[/<arch>]/<name>.0
566 * TODO: accomodate for multi-language directories.
569 treescan(const char *base
)
575 const char *dsec
, *arch
, *cp
, *name
, *path
;
579 argv
[1] = (char *)NULL
;
582 * Walk through all components under the directory, using the
583 * logical descent of files.
585 f
= fts_open((char * const *)argv
, FTS_LOGICAL
, NULL
);
594 while (NULL
!= (ff
= fts_read(f
))) {
595 path
= ff
->fts_path
+ 2;
597 * If we're a regular file, add an "of" by using the
598 * stored directory data and handling the filename.
599 * Disallow duplicate (hard-linked) files.
601 if (FTS_F
== ff
->fts_info
) {
602 if ( ! use_all
&& ff
->fts_level
< 2) {
603 WARNING(path
, base
, "Extraneous file");
605 } else if (inocheck(ff
->fts_statp
)) {
606 WARNING(path
, base
, "Duplicate file");
612 if (0 == strcmp(cp
, "mandocdb.db")) {
613 WARNING(path
, base
, "Skip database");
615 } else if (NULL
!= (cp
= strrchr(cp
, '.'))) {
616 if (0 == strcmp(cp
+ 1, "html")) {
617 WARNING(path
, base
, "Skip html");
619 } else if (0 == strcmp(cp
+ 1, "gz")) {
620 WARNING(path
, base
, "Skip gz");
622 } else if (0 == strcmp(cp
+ 1, "ps")) {
623 WARNING(path
, base
, "Skip ps");
625 } else if (0 == strcmp(cp
+ 1, "pdf")) {
626 WARNING(path
, base
, "Skip pdf");
631 if (NULL
!= (sec
= strrchr(ff
->fts_name
, '.'))) {
633 sec
= stradd(sec
+ 1);
635 name
= stradd(ff
->fts_name
);
636 ofadd(base
, dform
, path
,
637 name
, dsec
, sec
, arch
, ff
->fts_statp
);
639 } else if (FTS_D
!= ff
->fts_info
&&
640 FTS_DP
!= ff
->fts_info
)
643 switch (ff
->fts_level
) {
645 /* Ignore the root directory. */
649 * This might contain manX/ or catX/.
650 * Try to infer this from the name.
651 * If we're not in use_all, enforce it.
656 if (FTS_DP
== ff
->fts_info
)
659 if (0 == strncmp(cp
, "man", 3)) {
661 dsec
= stradd(cp
+ 3);
662 } else if (0 == strncmp(cp
, "cat", 3)) {
664 dsec
= stradd(cp
+ 3);
667 if (NULL
!= dsec
|| use_all
)
670 WARNING(path
, base
, "Unknown directory part");
671 fts_set(f
, ff
, FTS_SKIP
);
675 * Possibly our architecture.
676 * If we're descending, keep tabs on it.
679 if (FTS_DP
!= ff
->fts_info
&& NULL
!= dsec
)
680 arch
= stradd(ff
->fts_name
);
683 if (FTS_DP
== ff
->fts_info
|| use_all
)
685 WARNING(path
, base
, "Extraneous directory part");
686 fts_set(f
, ff
, FTS_SKIP
);
696 * Add a file to the file vector.
697 * Do not verify that it's a "valid" looking manpage (we'll do that
700 * Try to infer the manual section, architecture, and page name from the
701 * path, assuming it looks like
703 * [./]man*[/<arch>]/<name>.<section>
705 * [./]cat<section>[/<arch>]/<name>.0
707 * Stuff this information directly into the "of" vector.
708 * See treescan() for the fts(3) version of this.
711 filescan(const char *file
, const char *base
)
713 const char *sec
, *arch
, *name
, *dsec
;
714 char *p
, *start
, *buf
;
720 if (0 == strncmp(file
, "./", 2))
723 if (-1 == stat(file
, &st
)) {
724 WARNING(file
, base
, "%s", strerror(errno
));
726 } else if ( ! (S_IFREG
& st
.st_mode
)) {
727 WARNING(file
, base
, "Not a regular file");
729 } else if (inocheck(&st
)) {
730 WARNING(file
, base
, "Duplicate file");
734 buf
= mandoc_strdup(file
);
736 sec
= arch
= name
= dsec
= NULL
;
740 * First try to guess our directory structure.
741 * If we find a separator, try to look for man* or cat*.
742 * If we find one of these and what's underneath is a directory,
743 * assume it's an architecture.
745 if (NULL
!= (p
= strchr(start
, '/'))) {
747 if (0 == strncmp(start
, "man", 3)) {
750 } else if (0 == strncmp(start
, "cat", 3)) {
756 if (NULL
!= dsec
&& NULL
!= (p
= strchr(start
, '/'))) {
764 * Now check the file suffix.
765 * Suffix of `.0' indicates a catpage, `.1-9' is a manpage.
767 p
= strrchr(start
, '\0');
768 while (p
-- > start
&& '/' != *p
&& '.' != *p
)
777 * Now try to parse the name.
778 * Use the filename portion of the path.
781 if (NULL
!= (p
= strrchr(start
, '/'))) {
786 ofadd(base
, dform
, file
, name
, dsec
, sec
, arch
, &st
);
794 filecheck(const char *name
)
798 index
= ohash_qlookup(&filenames
, name
);
799 return(NULL
!= ohash_find(&filenames
, index
));
803 * Use the standard hashing mechanism (K&R) to see if the given filename
807 fileadd(struct of
*of
)
811 index
= ohash_qlookup(&filenames
, of
->file
);
812 assert(NULL
== ohash_find(&filenames
, index
));
813 ohash_insert(&filenames
, index
, of
);
820 inocheck(const struct stat
*st
)
826 memset(&id
, 0, sizeof(id
));
827 id
.ino
= hash
= st
->st_ino
;
829 index
= ohash_lookup_memory
830 (&inos
, (char *)&id
, sizeof(id
), hash
);
832 return(NULL
!= ohash_find(&inos
, index
));
836 * The hashing function used here is quite simple: simply take the inode
837 * and use uint32_t of its bits.
838 * Then when we do the lookup, use both the inode and device identifier.
841 inoadd(const struct stat
*st
, struct of
*of
)
846 of
->id
.ino
= hash
= st
->st_ino
;
847 of
->id
.dev
= st
->st_dev
;
848 index
= ohash_lookup_memory
849 (&inos
, (char *)&of
->id
, sizeof(of
->id
), hash
);
851 assert(NULL
== ohash_find(&inos
, index
));
852 ohash_insert(&inos
, index
, of
);
856 ofadd(const char *base
, int dform
, const char *file
,
857 const char *name
, const char *dsec
, const char *sec
,
858 const char *arch
, const struct stat
*st
)
863 assert(NULL
!= file
);
875 if (NULL
!= sec
&& *sec
<= '9' && *sec
>= '1')
877 else if (NULL
!= sec
&& *sec
== '0') {
882 of
= mandoc_calloc(1, sizeof(struct of
));
883 strlcpy(of
->file
, file
, MAXPATHLEN
);
894 * Add to unique identifier hash.
895 * Then if it's a source manual and we're going to use source in
896 * favour of catpages, add it to that hash.
907 while (NULL
!= (of
= ofs
)) {
914 * Run through the files in the global vector "ofs" and add them to the
915 * database specified in "base".
917 * This handles the parsing scheme itself, using the cues of directory
918 * and filename to determine whether the file is parsable or not.
921 ofmerge(struct mchars
*mc
, struct mparse
*mp
, const char *base
)
927 char buf
[MAXPATHLEN
];
929 const char *msec
, *march
, *mtitle
, *cp
;
931 enum mandoclevel lvl
;
933 for (of
= ofs
; NULL
!= of
; of
= of
->next
) {
935 * If we're a catpage (as defined by our path), then see
936 * if a manpage exists by the same name (ignoring the
938 * If it does, then we want to use it instead of our
941 if ( ! use_all
&& FORM_CAT
== of
->dform
) {
942 sz
= strlcpy(buf
, of
->file
, MAXPATHLEN
);
943 if (sz
>= MAXPATHLEN
) {
944 WARNING(of
->file
, base
,
945 "Filename too long");
948 bufp
= strstr(buf
, "cat");
949 assert(NULL
!= bufp
);
950 memcpy(bufp
, "man", 3);
951 if (NULL
!= (bufp
= strrchr(buf
, '.')))
953 strlcat(buf
, of
->dsec
, MAXPATHLEN
);
954 if (filecheck(buf
)) {
955 WARNING(of
->file
, base
, "Man "
956 "source exists: %s", buf
);
971 * Try interpreting the file as mdoc(7) or man(7)
972 * source code, unless it is already known to be
973 * formatted. Fall back to formatted mode.
975 if (FORM_SRC
== of
->dform
|| FORM_SRC
== of
->sform
) {
976 lvl
= mparse_readfd(mp
, -1, of
->file
);
977 if (lvl
< MANDOCLEVEL_FATAL
)
978 mparse_result(mp
, &mdoc
, &man
);
983 msec
= mdoc_meta(mdoc
)->msec
;
984 march
= mdoc_meta(mdoc
)->arch
;
985 mtitle
= mdoc_meta(mdoc
)->title
;
986 } else if (NULL
!= man
) {
988 msec
= man_meta(man
)->msec
;
990 mtitle
= man_meta(man
)->title
;
1001 * Check whether the manual section given in a file
1002 * agrees with the directory where the file is located.
1003 * Some manuals have suffixes like (3p) on their
1004 * section number either inside the file or in the
1005 * directory name, some are linked into more than one
1006 * section, like encrypt(1) = makekey(8). Do not skip
1007 * manuals for such reasons.
1009 if ( ! use_all
&& form
&& strcasecmp(msec
, of
->dsec
))
1010 WARNING(of
->file
, base
, "Section \"%s\" "
1011 "manual in %s directory",
1015 * Manual page directories exist for each kernel
1016 * architecture as returned by machine(1).
1017 * However, many manuals only depend on the
1018 * application architecture as returned by arch(1).
1019 * For example, some (2/ARM) manuals are shared
1020 * across the "armish" and "zaurus" kernel
1022 * A few manuals are even shared across completely
1023 * different architectures, for example fdformat(1)
1024 * on amd64, i386, sparc, and sparc64.
1025 * Thus, warn about architecture mismatches,
1026 * but don't skip manuals for this reason.
1028 if ( ! use_all
&& strcasecmp(march
, of
->arch
))
1029 WARNING(of
->file
, base
, "Architecture \"%s\" "
1030 "manual in \"%s\" directory",
1033 putkey(of
, of
->name
, TYPE_Nm
);
1036 if (NULL
!= (cp
= mdoc_meta(mdoc
)->name
))
1037 putkey(of
, cp
, TYPE_Nm
);
1038 parse_mdoc(of
, mdoc_node(mdoc
));
1039 } else if (NULL
!= man
)
1040 parse_man(of
, man_node(man
));
1042 parse_catpage(of
, base
);
1044 dbindex(mc
, form
, of
, base
);
1051 parse_catpage(struct of
*of
, const char *base
)
1054 char *line
, *p
, *title
;
1055 size_t len
, plen
, titlesz
;
1057 if (NULL
== (stream
= fopen(of
->file
, "r"))) {
1058 WARNING(of
->file
, base
, "%s", strerror(errno
));
1062 /* Skip to first blank line. */
1064 while (NULL
!= (line
= fgetln(stream
, &len
)))
1069 * Assume the first line that is not indented
1070 * is the first section header. Skip to it.
1073 while (NULL
!= (line
= fgetln(stream
, &len
)))
1074 if ('\n' != *line
&& ' ' != *line
)
1078 * Read up until the next section into a buffer.
1079 * Strip the leading and trailing newline from each read line,
1080 * appending a trailing space.
1081 * Ignore empty (whitespace-only) lines.
1087 while (NULL
!= (line
= fgetln(stream
, &len
))) {
1088 if (' ' != *line
|| '\n' != line
[len
- 1])
1090 while (len
> 0 && isspace((unsigned char)*line
)) {
1096 title
= mandoc_realloc(title
, titlesz
+ len
);
1097 memcpy(title
+ titlesz
, line
, len
);
1099 title
[titlesz
- 1] = ' ';
1103 * If no page content can be found, or the input line
1104 * is already the next section header, or there is no
1105 * trailing newline, reuse the page title as the page
1109 if (NULL
== title
|| '\0' == *title
) {
1110 WARNING(of
->file
, base
, "Cannot find NAME section");
1116 title
= mandoc_realloc(title
, titlesz
+ 1);
1117 title
[titlesz
] = '\0';
1120 * Skip to the first dash.
1121 * Use the remaining line as the description (no more than 70
1125 if (NULL
!= (p
= strstr(title
, "- "))) {
1126 for (p
+= 2; ' ' == *p
|| '\b' == *p
; p
++)
1127 /* Skip to next word. */ ;
1129 WARNING(of
->file
, base
, "No dash in title line");
1135 /* Strip backspace-encoding from line. */
1137 while (NULL
!= (line
= memchr(p
, '\b', plen
))) {
1140 memmove(line
, line
+ 1, plen
--);
1143 memmove(line
- 1, line
+ 1, plen
- len
);
1147 of
->desc
= stradd(p
);
1148 putkey(of
, p
, TYPE_Nd
);
1154 * Put a type/word pair into the word database for this particular file.
1157 putkey(const struct of
*of
, const char *value
, uint64_t type
)
1160 assert(NULL
!= value
);
1161 wordaddbuf(of
, value
, strlen(value
), type
);
1165 * Like putkey() but for unterminated strings.
1168 putkeys(const struct of
*of
, const char *value
, int sz
, uint64_t type
)
1171 wordaddbuf(of
, value
, sz
, type
);
1175 * Grok all nodes at or below a certain mdoc node into putkey().
1178 putmdockey(const struct of
*of
, const struct mdoc_node
*n
, uint64_t m
)
1181 for ( ; NULL
!= n
; n
= n
->next
) {
1182 if (NULL
!= n
->child
)
1183 putmdockey(of
, n
->child
, m
);
1184 if (MDOC_TEXT
== n
->type
)
1185 putkey(of
, n
->string
, m
);
1190 parse_man(struct of
*of
, const struct man_node
*n
)
1192 const struct man_node
*head
, *body
;
1193 char *start
, *sv
, *title
;
1201 * We're only searching for one thing: the first text child in
1202 * the BODY of a NAME section. Since we don't keep track of
1203 * sections in -man, run some hoops to find out whether we're in
1204 * the correct section or not.
1207 if (MAN_BODY
== n
->type
&& MAN_SH
== n
->tok
) {
1209 assert(body
->parent
);
1210 if (NULL
!= (head
= body
->parent
->head
) &&
1211 1 == head
->nchild
&&
1212 NULL
!= (head
= (head
->child
)) &&
1213 MAN_TEXT
== head
->type
&&
1214 0 == strcmp(head
->string
, "NAME") &&
1215 NULL
!= (body
= body
->child
) &&
1216 MAN_TEXT
== body
->type
) {
1222 * Suck the entire NAME section into memory.
1223 * Yes, we might run away.
1224 * But too many manuals have big, spread-out
1225 * NAME sections over many lines.
1228 for ( ; NULL
!= body
; body
= body
->next
) {
1229 if (MAN_TEXT
!= body
->type
)
1231 if (0 == (sz
= strlen(body
->string
)))
1233 title
= mandoc_realloc
1234 (title
, titlesz
+ sz
+ 1);
1235 memcpy(title
+ titlesz
, body
->string
, sz
);
1237 title
[titlesz
- 1] = ' ';
1242 title
= mandoc_realloc(title
, titlesz
+ 1);
1243 title
[titlesz
] = '\0';
1245 /* Skip leading space. */
1248 while (isspace((unsigned char)*sv
))
1251 if (0 == (sz
= strlen(sv
))) {
1256 /* Erase trailing space. */
1258 start
= &sv
[sz
- 1];
1259 while (start
> sv
&& isspace((unsigned char)*start
))
1270 * Go through a special heuristic dance here.
1271 * Conventionally, one or more manual names are
1272 * comma-specified prior to a whitespace, then a
1273 * dash, then a description. Try to puzzle out
1274 * the name parts here.
1278 sz
= strcspn(start
, " ,");
1279 if ('\0' == start
[sz
])
1285 putkey(of
, start
, TYPE_Nm
);
1292 assert(',' == byte
);
1294 while (' ' == *start
)
1299 putkey(of
, start
, TYPE_Nm
);
1304 while (isspace((unsigned char)*start
))
1307 if (0 == strncmp(start
, "-", 1))
1309 else if (0 == strncmp(start
, "\\-\\-", 4))
1311 else if (0 == strncmp(start
, "\\-", 2))
1313 else if (0 == strncmp(start
, "\\(en", 4))
1315 else if (0 == strncmp(start
, "\\(em", 4))
1318 while (' ' == *start
)
1321 assert(NULL
== of
->desc
);
1322 of
->desc
= stradd(start
);
1323 putkey(of
, start
, TYPE_Nd
);
1329 for (n
= n
->child
; n
; n
= n
->next
)
1330 if (parse_man(of
, n
))
1337 parse_mdoc(struct of
*of
, const struct mdoc_node
*n
)
1341 for (n
= n
->child
; NULL
!= n
; n
= n
->next
) {
1352 if (NULL
!= mdocs
[n
->tok
].fp
)
1353 if (0 == (*mdocs
[n
->tok
].fp
)(of
, n
))
1356 if (MDOCF_CHILD
& mdocs
[n
->tok
].flags
)
1357 putmdockey(of
, n
->child
, mdocs
[n
->tok
].mask
);
1360 assert(MDOC_ROOT
!= n
->type
);
1363 if (NULL
!= n
->child
)
1369 parse_mdoc_Fd(struct of
*of
, const struct mdoc_node
*n
)
1371 const char *start
, *end
;
1374 if (SEC_SYNOPSIS
!= n
->sec
||
1375 NULL
== (n
= n
->child
) ||
1376 MDOC_TEXT
!= n
->type
)
1380 * Only consider those `Fd' macro fields that begin with an
1381 * "inclusion" token (versus, e.g., #define).
1384 if (strcmp("#include", n
->string
))
1387 if (NULL
== (n
= n
->next
) || MDOC_TEXT
!= n
->type
)
1391 * Strip away the enclosing angle brackets and make sure we're
1396 if ('<' == *start
|| '"' == *start
)
1399 if (0 == (sz
= strlen(start
)))
1402 end
= &start
[(int)sz
- 1];
1403 if ('>' == *end
|| '"' == *end
)
1407 putkeys(of
, start
, end
- start
+ 1, TYPE_In
);
1412 parse_mdoc_In(struct of
*of
, const struct mdoc_node
*n
)
1415 if (NULL
!= n
->child
&& MDOC_TEXT
== n
->child
->type
)
1418 putkey(of
, n
->child
->string
, TYPE_In
);
1423 parse_mdoc_Fn(struct of
*of
, const struct mdoc_node
*n
)
1427 if (NULL
== (n
= n
->child
) || MDOC_TEXT
!= n
->type
)
1431 * Parse: .Fn "struct type *name" "char *arg".
1432 * First strip away pointer symbol.
1433 * Then store the function name, then type.
1434 * Finally, store the arguments.
1437 if (NULL
== (cp
= strrchr(n
->string
, ' ')))
1443 putkey(of
, cp
, TYPE_Fn
);
1446 putkeys(of
, n
->string
, cp
- n
->string
, TYPE_Ft
);
1448 for (n
= n
->next
; NULL
!= n
; n
= n
->next
)
1449 if (MDOC_TEXT
== n
->type
)
1450 putkey(of
, n
->string
, TYPE_Fa
);
1456 parse_mdoc_St(struct of
*of
, const struct mdoc_node
*n
)
1459 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
1462 putkey(of
, n
->child
->string
, TYPE_St
);
1467 parse_mdoc_Xr(struct of
*of
, const struct mdoc_node
*n
)
1470 if (NULL
== (n
= n
->child
))
1473 putkey(of
, n
->string
, TYPE_Xr
);
1478 parse_mdoc_Nd(struct of
*of
, const struct mdoc_node
*n
)
1483 if (MDOC_BODY
!= n
->type
)
1487 * Special-case the `Nd' because we need to put the description
1488 * into the document table.
1492 for (n
= n
->child
; NULL
!= n
; n
= n
->next
) {
1493 if (MDOC_TEXT
== n
->type
) {
1494 sz
= strlen(n
->string
) + 1;
1495 if (NULL
!= (sv
= desc
))
1496 sz
+= strlen(desc
) + 1;
1497 desc
= mandoc_realloc(desc
, sz
);
1499 strlcat(desc
, " ", sz
);
1502 strlcat(desc
, n
->string
, sz
);
1504 if (NULL
!= n
->child
)
1505 parse_mdoc_Nd(of
, n
);
1508 of
->desc
= NULL
!= desc
? stradd(desc
) : NULL
;
1514 parse_mdoc_Nm(struct of
*of
, const struct mdoc_node
*n
)
1517 if (SEC_NAME
== n
->sec
)
1519 else if (SEC_SYNOPSIS
!= n
->sec
|| MDOC_HEAD
!= n
->type
)
1526 parse_mdoc_Sh(struct of
*of
, const struct mdoc_node
*n
)
1529 return(SEC_CUSTOM
== n
->sec
&& MDOC_HEAD
== n
->type
);
1533 parse_mdoc_head(struct of
*of
, const struct mdoc_node
*n
)
1536 return(MDOC_HEAD
== n
->type
);
1540 parse_mdoc_body(struct of
*of
, const struct mdoc_node
*n
)
1543 return(MDOC_BODY
== n
->type
);
1550 stradd(const char *cp
)
1553 return(straddbuf(cp
, strlen(cp
)));
1557 * This looks up or adds a string to the string table.
1558 * The string table is a table of all strings encountered during parse
1560 * In using it, we avoid having thousands of (e.g.) "cat1" string
1561 * allocations for the "of" table.
1562 * We also have a layer atop the string table for keeping track of words
1563 * in a parse sequence (see wordaddbuf()).
1566 straddbuf(const char *cp
, size_t sz
)
1572 if (NULL
!= (s
= hashget(cp
, sz
)))
1575 s
= mandoc_calloc(sizeof(struct str
) + sz
+ 1, 1);
1576 memcpy(s
->key
, cp
, sz
);
1579 index
= ohash_qlookupi(&strings
, cp
, &end
);
1580 assert(NULL
== ohash_find(&strings
, index
));
1581 ohash_insert(&strings
, index
, s
);
1586 hashget(const char *cp
, size_t sz
)
1592 index
= ohash_qlookupi(&strings
, cp
, &end
);
1593 return(ohash_find(&strings
, index
));
1597 * Add a word to the current parse sequence.
1598 * Within the hashtable of strings, we maintain a list of strings that
1599 * are currently indexed.
1600 * Each of these ("words") has a bitmask modified within the parse.
1601 * When we finish a parse, we'll dump the list, then remove the head
1602 * entry -- since the next parse will have a new "of", it can keep track
1603 * of its entries without conflict.
1606 wordaddbuf(const struct of
*of
,
1607 const char *cp
, size_t sz
, uint64_t v
)
1616 s
= hashget(cp
, sz
);
1618 if (NULL
!= s
&& of
== s
->of
) {
1621 } else if (NULL
== s
) {
1622 s
= mandoc_calloc(sizeof(struct str
) + sz
+ 1, 1);
1623 memcpy(s
->key
, cp
, sz
);
1625 index
= ohash_qlookupi(&strings
, cp
, &end
);
1626 assert(NULL
== ohash_find(&strings
, index
));
1627 ohash_insert(&strings
, index
, s
);
1637 * Take a Unicode codepoint and produce its UTF-8 encoding.
1638 * This isn't the best way to do this, but it works.
1639 * The magic numbers are from the UTF-8 packaging.
1640 * They're not as scary as they seem: read the UTF-8 spec for details.
1643 utf8(unsigned int cp
, char out
[7])
1648 if (cp
<= 0x0000007F) {
1651 } else if (cp
<= 0x000007FF) {
1653 out
[0] = (cp
>> 6 & 31) | 192;
1654 out
[1] = (cp
& 63) | 128;
1655 } else if (cp
<= 0x0000FFFF) {
1657 out
[0] = (cp
>> 12 & 15) | 224;
1658 out
[1] = (cp
>> 6 & 63) | 128;
1659 out
[2] = (cp
& 63) | 128;
1660 } else if (cp
<= 0x001FFFFF) {
1662 out
[0] = (cp
>> 18 & 7) | 240;
1663 out
[1] = (cp
>> 12 & 63) | 128;
1664 out
[2] = (cp
>> 6 & 63) | 128;
1665 out
[3] = (cp
& 63) | 128;
1666 } else if (cp
<= 0x03FFFFFF) {
1668 out
[0] = (cp
>> 24 & 3) | 248;
1669 out
[1] = (cp
>> 18 & 63) | 128;
1670 out
[2] = (cp
>> 12 & 63) | 128;
1671 out
[3] = (cp
>> 6 & 63) | 128;
1672 out
[4] = (cp
& 63) | 128;
1673 } else if (cp
<= 0x7FFFFFFF) {
1675 out
[0] = (cp
>> 30 & 1) | 252;
1676 out
[1] = (cp
>> 24 & 63) | 128;
1677 out
[2] = (cp
>> 18 & 63) | 128;
1678 out
[3] = (cp
>> 12 & 63) | 128;
1679 out
[4] = (cp
>> 6 & 63) | 128;
1680 out
[5] = (cp
& 63) | 128;
1689 * Store the UTF-8 version of a key, or alias the pointer if the key has
1690 * no UTF-8 transcription marks in it.
1693 utf8key(struct mchars
*mc
, struct str
*key
)
1695 size_t sz
, bsz
, pos
;
1696 char utfbuf
[7], res
[5];
1698 const char *seq
, *cpp
, *val
;
1700 enum mandoc_esc esc
;
1702 assert(NULL
== key
->utf8
);
1706 res
[2] = ASCII_NBRSP
;
1707 res
[3] = ASCII_HYPH
;
1714 * Pre-check: if we have no stop-characters, then set the
1715 * pointer as ourselvse and get out of here.
1717 if (strcspn(val
, res
) == bsz
) {
1718 key
->utf8
= key
->key
;
1722 /* Pre-allocate by the length of the input */
1724 buf
= mandoc_malloc(++bsz
);
1727 while ('\0' != *val
) {
1729 * Halt on the first escape sequence.
1730 * This also halts on the end of string, in which case
1731 * we just copy, fallthrough, and exit the loop.
1733 if ((sz
= strcspn(val
, res
)) > 0) {
1734 memcpy(&buf
[pos
], val
, sz
);
1739 if (ASCII_HYPH
== *val
) {
1743 } else if ('\t' == *val
|| ASCII_NBRSP
== *val
) {
1747 } else if ('\\' != *val
)
1750 /* Read past the slash. */
1756 * Parse the escape sequence and see if it's a
1757 * predefined character or special character.
1760 ((const char **)&val
, &seq
, &len
);
1761 if (ESCAPE_ERROR
== esc
)
1764 if (ESCAPE_SPECIAL
!= esc
)
1766 if (0 == (u
= mchars_spec2cp(mc
, seq
, len
)))
1770 * If we have a Unicode codepoint, try to convert that
1771 * to a UTF-8 byte string.
1774 if (0 == (sz
= utf8(u
, utfbuf
)))
1777 /* Copy the rendered glyph into the stream. */
1782 buf
= mandoc_realloc(buf
, bsz
);
1784 memcpy(&buf
[pos
], cpp
, sz
);
1793 * Flush the current page's terms (and their bits) into the database.
1794 * Wrap the entire set of additions in a transaction to make sqlite be a
1796 * Also, UTF-8-encode the description at the last possible moment.
1799 dbindex(struct mchars
*mc
, int form
,
1800 const struct of
*of
, const char *base
)
1807 DEBUG(of
->file
, base
, "Adding to index");
1813 if (NULL
!= of
->desc
) {
1814 key
= hashget(of
->desc
, strlen(of
->desc
));
1815 assert(NULL
!= key
);
1816 if (NULL
== key
->utf8
)
1821 SQL_EXEC("BEGIN TRANSACTION");
1824 SQL_BIND_TEXT(stmts
[STMT_INSERT_DOC
], i
, of
->file
);
1825 SQL_BIND_TEXT(stmts
[STMT_INSERT_DOC
], i
, of
->sec
);
1826 SQL_BIND_TEXT(stmts
[STMT_INSERT_DOC
], i
, of
->arch
);
1827 SQL_BIND_TEXT(stmts
[STMT_INSERT_DOC
], i
, desc
);
1828 SQL_BIND_INT(stmts
[STMT_INSERT_DOC
], i
, form
);
1829 SQL_STEP(stmts
[STMT_INSERT_DOC
]);
1830 recno
= sqlite3_last_insert_rowid(db
);
1831 sqlite3_reset(stmts
[STMT_INSERT_DOC
]);
1833 for (key
= words
; NULL
!= key
; key
= key
->next
) {
1834 assert(key
->of
== of
);
1835 if (NULL
== key
->utf8
)
1838 SQL_BIND_INT64(stmts
[STMT_INSERT_KEY
], i
, key
->mask
);
1839 SQL_BIND_TEXT(stmts
[STMT_INSERT_KEY
], i
, key
->utf8
);
1840 SQL_BIND_INT64(stmts
[STMT_INSERT_KEY
], i
, recno
);
1841 SQL_STEP(stmts
[STMT_INSERT_KEY
]);
1842 sqlite3_reset(stmts
[STMT_INSERT_KEY
]);
1845 SQL_EXEC("END TRANSACTION");
1849 dbprune(const char *base
)
1857 for (of
= ofs
; NULL
!= of
; of
= of
->next
) {
1859 SQL_BIND_TEXT(stmts
[STMT_DELETE
], i
, of
->file
);
1860 SQL_STEP(stmts
[STMT_DELETE
]);
1861 sqlite3_reset(stmts
[STMT_DELETE
]);
1862 DEBUG(of
->file
, base
, "Deleted from index");
1867 * Close an existing database and its prepared statements.
1868 * If "real" is not set, rename the temporary file into the real one.
1871 dbclose(const char *base
, int real
)
1874 char file
[MAXPATHLEN
];
1879 for (i
= 0; i
< STMT__MAX
; i
++) {
1880 sqlite3_finalize(stmts
[i
]);
1890 strlcpy(file
, MANDOC_DB
, MAXPATHLEN
);
1891 strlcat(file
, "~", MAXPATHLEN
);
1892 if (-1 == rename(file
, MANDOC_DB
))
1897 * This is straightforward stuff.
1898 * Open a database connection to a "temporary" database, then open a set
1899 * of prepared statements we'll use over and over again.
1900 * If "real" is set, we use the existing database; if not, we truncate a
1902 * Must be matched by dbclose().
1905 dbopen(const char *base
, int real
)
1907 char file
[MAXPATHLEN
];
1915 sz
= strlcpy(file
, MANDOC_DB
, MAXPATHLEN
);
1917 sz
= strlcat(file
, "~", MAXPATHLEN
);
1919 if (sz
>= MAXPATHLEN
) {
1920 fprintf(stderr
, "%s: Path too long\n", file
);
1927 ofl
= SQLITE_OPEN_READWRITE
|
1928 (0 == real
? SQLITE_OPEN_EXCLUSIVE
: 0);
1930 rc
= sqlite3_open_v2(file
, &db
, ofl
, NULL
);
1931 if (SQLITE_OK
== rc
)
1933 if (SQLITE_CANTOPEN
!= rc
) {
1941 if (SQLITE_OK
!= (rc
= sqlite3_open(file
, &db
))) {
1946 sql
= "CREATE TABLE \"docs\" (\n"
1947 " \"file\" TEXT NOT NULL,\n"
1948 " \"sec\" TEXT NOT NULL,\n"
1949 " \"arch\" TEXT NOT NULL,\n"
1950 " \"desc\" TEXT NOT NULL,\n"
1951 " \"form\" INTEGER NOT NULL,\n"
1952 " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
1955 "CREATE TABLE \"keys\" (\n"
1956 " \"bits\" INTEGER NOT NULL,\n"
1957 " \"key\" TEXT NOT NULL,\n"
1958 " \"docid\" INTEGER NOT NULL REFERENCES docs(id) "
1959 "ON DELETE CASCADE,\n"
1960 " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
1963 "CREATE INDEX \"key_index\" ON keys (key);\n";
1965 if (SQLITE_OK
!= sqlite3_exec(db
, sql
, NULL
, NULL
, NULL
)) {
1966 perror(sqlite3_errmsg(db
));
1970 sql
= "DELETE FROM docs where file=?";
1971 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_DELETE
], NULL
);
1972 sql
= "INSERT INTO docs "
1973 "(file,sec,arch,desc,form) VALUES (?,?,?,?,?)";
1974 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_DOC
], NULL
);
1975 sql
= "INSERT INTO keys "
1976 "(bits,key,docid) VALUES (?,?,?)";
1977 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_KEY
], NULL
);
1982 hash_halloc(size_t sz
, void *arg
)
1985 return(mandoc_calloc(sz
, 1));
1989 hash_alloc(size_t sz
, void *arg
)
1992 return(mandoc_malloc(sz
));
1996 hash_free(void *p
, size_t sz
, void *arg
)
2003 path_reset(const char *cwd
, int fd
, const char *base
)
2006 if (-1 == fchdir(fd
)) {
2009 } else if (-1 == chdir(base
)) {