]>
git.cameronkatri.com Git - mandoc.git/blob - mandocdb.c
1 /* $Id: mandocdb.c,v 1.100 2014/01/05 04:13:52 schwarze Exp $ */
3 * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011, 2012, 2013, 2014 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
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_NONE
, /* format is unknown */
80 FORM_SRC
, /* format is -man or -mdoc */
81 FORM_CAT
/* format is cat */
85 char *rendered
; /* key in UTF-8 or ASCII form */
86 const struct mpage
*mpage
; /* if set, the owning parse */
87 uint64_t mask
; /* bitmask in sequence */
88 char key
[]; /* may contain escape sequences */
97 struct inodev inodev
; /* used for hashing routine */
98 enum form form
; /* format from file content */
99 char *sec
; /* section from file content */
100 char *arch
; /* architecture from file content */
101 char *title
; /* title from file content */
102 char *desc
; /* description from file content */
103 struct mlink
*mlinks
; /* singly linked list */
107 char file
[PATH_MAX
]; /* filename rel. to manpath */
108 enum form dform
; /* format from directory */
109 enum form fform
; /* format from file name suffix */
110 char *dsec
; /* section from directory */
111 char *arch
; /* architecture from directory */
112 char *name
; /* name from file name (not empty) */
113 char *fsec
; /* section from file name suffix */
114 struct mlink
*next
; /* singly linked list */
118 char *title
; /* name(sec/arch) given inside the file */
119 char *file
; /* file name in case of mismatch */
123 STMT_DELETE_PAGE
= 0, /* delete mpage */
124 STMT_INSERT_PAGE
, /* insert mpage */
125 STMT_INSERT_LINK
, /* insert mlink */
126 STMT_INSERT_KEY
, /* insert parsed key */
130 typedef int (*mdoc_fp
)(struct mpage
*, const struct mdoc_node
*);
132 struct mdoc_handler
{
133 mdoc_fp fp
; /* optional handler */
134 uint64_t mask
; /* set unless handler returns 0 */
137 static void dbclose(int);
138 static void dbindex(const struct mpage
*, struct mchars
*);
139 static int dbopen(int);
140 static void dbprune(void);
141 static void filescan(const char *);
142 static void *hash_alloc(size_t, void *);
143 static void hash_free(void *, size_t, void *);
144 static void *hash_halloc(size_t, void *);
145 static void mlink_add(struct mlink
*, const struct stat
*);
146 static int mlink_check(struct mpage
*, struct mlink
*);
147 static void mlink_free(struct mlink
*);
148 static void mlinks_undupe(struct mpage
*);
149 static void mpages_free(void);
150 static void mpages_merge(struct mchars
*, struct mparse
*, int);
151 static void parse_cat(struct mpage
*);
152 static void parse_man(struct mpage
*, const struct man_node
*);
153 static void parse_mdoc(struct mpage
*, const struct mdoc_node
*);
154 static int parse_mdoc_body(struct mpage
*, const struct mdoc_node
*);
155 static int parse_mdoc_head(struct mpage
*, const struct mdoc_node
*);
156 static int parse_mdoc_Fd(struct mpage
*, const struct mdoc_node
*);
157 static int parse_mdoc_Fn(struct mpage
*, const struct mdoc_node
*);
158 static int parse_mdoc_Nd(struct mpage
*, const struct mdoc_node
*);
159 static int parse_mdoc_Nm(struct mpage
*, const struct mdoc_node
*);
160 static int parse_mdoc_Sh(struct mpage
*, const struct mdoc_node
*);
161 static int parse_mdoc_Xr(struct mpage
*, const struct mdoc_node
*);
162 static void putkey(const struct mpage
*,
163 const char *, uint64_t);
164 static void putkeys(const struct mpage
*,
165 const char *, size_t, uint64_t);
166 static void putmdockey(const struct mpage
*,
167 const struct mdoc_node
*, uint64_t);
168 static void render_key(struct mchars
*, struct str
*);
169 static void say(const char *, const char *, ...);
170 static int set_basedir(const char *);
171 static int treescan(void);
172 static size_t utf8(unsigned int, char [7]);
174 static char *progname
;
175 static int use_all
; /* use all found files */
176 static int nodb
; /* no database changes */
177 static int verb
; /* print what we're doing */
178 static int warnings
; /* warn about crap */
179 static int write_utf8
; /* write UTF-8 output; else ASCII */
180 static int exitcode
; /* to be returned by main */
181 static enum op op
; /* operational mode */
182 static char basedir
[PATH_MAX
]; /* current base directory */
183 static struct ohash mpages
; /* table of distinct manual pages */
184 static struct ohash mlinks
; /* table of directory entries */
185 static struct ohash strings
; /* table of all strings */
186 static sqlite3
*db
= NULL
; /* current database */
187 static sqlite3_stmt
*stmts
[STMT__MAX
]; /* current statements */
189 static const struct mdoc_handler mdocs
[MDOC_MAX
] = {
190 { NULL
, 0 }, /* Ap */
191 { NULL
, 0 }, /* Dd */
192 { NULL
, 0 }, /* Dt */
193 { NULL
, 0 }, /* Os */
194 { parse_mdoc_Sh
, TYPE_Sh
}, /* Sh */
195 { parse_mdoc_head
, TYPE_Ss
}, /* Ss */
196 { NULL
, 0 }, /* Pp */
197 { NULL
, 0 }, /* D1 */
198 { NULL
, 0 }, /* Dl */
199 { NULL
, 0 }, /* Bd */
200 { NULL
, 0 }, /* Ed */
201 { NULL
, 0 }, /* Bl */
202 { NULL
, 0 }, /* El */
203 { NULL
, 0 }, /* It */
204 { NULL
, 0 }, /* Ad */
205 { NULL
, TYPE_An
}, /* An */
206 { NULL
, TYPE_Ar
}, /* Ar */
207 { NULL
, TYPE_Cd
}, /* Cd */
208 { NULL
, TYPE_Cm
}, /* Cm */
209 { NULL
, TYPE_Dv
}, /* Dv */
210 { NULL
, TYPE_Er
}, /* Er */
211 { NULL
, TYPE_Ev
}, /* Ev */
212 { NULL
, 0 }, /* Ex */
213 { NULL
, TYPE_Fa
}, /* Fa */
214 { parse_mdoc_Fd
, 0 }, /* Fd */
215 { NULL
, TYPE_Fl
}, /* Fl */
216 { parse_mdoc_Fn
, 0 }, /* Fn */
217 { NULL
, TYPE_Ft
}, /* Ft */
218 { NULL
, TYPE_Ic
}, /* Ic */
219 { NULL
, TYPE_In
}, /* In */
220 { NULL
, TYPE_Li
}, /* Li */
221 { parse_mdoc_Nd
, TYPE_Nd
}, /* Nd */
222 { parse_mdoc_Nm
, TYPE_Nm
}, /* Nm */
223 { NULL
, 0 }, /* Op */
224 { NULL
, 0 }, /* Ot */
225 { NULL
, TYPE_Pa
}, /* Pa */
226 { NULL
, 0 }, /* Rv */
227 { NULL
, TYPE_St
}, /* St */
228 { NULL
, TYPE_Va
}, /* Va */
229 { parse_mdoc_body
, TYPE_Va
}, /* Vt */
230 { parse_mdoc_Xr
, 0 }, /* Xr */
231 { NULL
, 0 }, /* %A */
232 { NULL
, 0 }, /* %B */
233 { NULL
, 0 }, /* %D */
234 { NULL
, 0 }, /* %I */
235 { NULL
, 0 }, /* %J */
236 { NULL
, 0 }, /* %N */
237 { NULL
, 0 }, /* %O */
238 { NULL
, 0 }, /* %P */
239 { NULL
, 0 }, /* %R */
240 { NULL
, 0 }, /* %T */
241 { NULL
, 0 }, /* %V */
242 { NULL
, 0 }, /* Ac */
243 { NULL
, 0 }, /* Ao */
244 { NULL
, 0 }, /* Aq */
245 { NULL
, TYPE_At
}, /* At */
246 { NULL
, 0 }, /* Bc */
247 { NULL
, 0 }, /* Bf */
248 { NULL
, 0 }, /* Bo */
249 { NULL
, 0 }, /* Bq */
250 { NULL
, TYPE_Bsx
}, /* Bsx */
251 { NULL
, TYPE_Bx
}, /* Bx */
252 { NULL
, 0 }, /* Db */
253 { NULL
, 0 }, /* Dc */
254 { NULL
, 0 }, /* Do */
255 { NULL
, 0 }, /* Dq */
256 { NULL
, 0 }, /* Ec */
257 { NULL
, 0 }, /* Ef */
258 { NULL
, TYPE_Em
}, /* Em */
259 { NULL
, 0 }, /* Eo */
260 { NULL
, TYPE_Fx
}, /* Fx */
261 { NULL
, TYPE_Ms
}, /* Ms */
262 { NULL
, 0 }, /* No */
263 { NULL
, 0 }, /* Ns */
264 { NULL
, TYPE_Nx
}, /* Nx */
265 { NULL
, TYPE_Ox
}, /* Ox */
266 { NULL
, 0 }, /* Pc */
267 { NULL
, 0 }, /* Pf */
268 { NULL
, 0 }, /* Po */
269 { NULL
, 0 }, /* Pq */
270 { NULL
, 0 }, /* Qc */
271 { NULL
, 0 }, /* Ql */
272 { NULL
, 0 }, /* Qo */
273 { NULL
, 0 }, /* Qq */
274 { NULL
, 0 }, /* Re */
275 { NULL
, 0 }, /* Rs */
276 { NULL
, 0 }, /* Sc */
277 { NULL
, 0 }, /* So */
278 { NULL
, 0 }, /* Sq */
279 { NULL
, 0 }, /* Sm */
280 { NULL
, 0 }, /* Sx */
281 { NULL
, TYPE_Sy
}, /* Sy */
282 { NULL
, TYPE_Tn
}, /* Tn */
283 { NULL
, 0 }, /* Ux */
284 { NULL
, 0 }, /* Xc */
285 { NULL
, 0 }, /* Xo */
286 { parse_mdoc_head
, 0 }, /* Fo */
287 { NULL
, 0 }, /* Fc */
288 { NULL
, 0 }, /* Oo */
289 { NULL
, 0 }, /* Oc */
290 { NULL
, 0 }, /* Bk */
291 { NULL
, 0 }, /* Ek */
292 { NULL
, 0 }, /* Bt */
293 { NULL
, 0 }, /* Hf */
294 { NULL
, 0 }, /* Fr */
295 { NULL
, 0 }, /* Ud */
296 { NULL
, TYPE_Lb
}, /* Lb */
297 { NULL
, 0 }, /* Lp */
298 { NULL
, TYPE_Lk
}, /* Lk */
299 { NULL
, TYPE_Mt
}, /* Mt */
300 { NULL
, 0 }, /* Brq */
301 { NULL
, 0 }, /* Bro */
302 { NULL
, 0 }, /* Brc */
303 { NULL
, 0 }, /* %C */
304 { NULL
, 0 }, /* Es */
305 { NULL
, 0 }, /* En */
306 { NULL
, TYPE_Dx
}, /* Dx */
307 { NULL
, 0 }, /* %Q */
308 { NULL
, 0 }, /* br */
309 { NULL
, 0 }, /* sp */
310 { NULL
, 0 }, /* %U */
311 { NULL
, 0 }, /* Ta */
315 main(int argc
, char *argv
[])
319 const char *path_arg
;
321 struct manpaths dirs
;
323 struct ohash_info mpages_info
, mlinks_info
;
325 memset(stmts
, 0, STMT__MAX
* sizeof(sqlite3_stmt
*));
326 memset(&dirs
, 0, sizeof(struct manpaths
));
328 mpages_info
.alloc
= mlinks_info
.alloc
= hash_alloc
;
329 mpages_info
.halloc
= mlinks_info
.halloc
= hash_halloc
;
330 mpages_info
.hfree
= mlinks_info
.hfree
= hash_free
;
332 mpages_info
.key_offset
= offsetof(struct mpage
, inodev
);
333 mlinks_info
.key_offset
= offsetof(struct mlink
, file
);
335 progname
= strrchr(argv
[0], '/');
336 if (progname
== NULL
)
342 * We accept a few different invocations.
343 * The CHECKOP macro makes sure that invocation styles don't
344 * clobber each other.
346 #define CHECKOP(_op, _ch) do \
347 if (OP_DEFAULT != (_op)) { \
348 fprintf(stderr, "-%c: Conflicting option\n", (_ch)); \
350 } while (/*CONSTCOND*/0)
355 while (-1 != (ch
= getopt(argc
, argv
, "aC:d:nT:tu:vW")))
374 if (strcmp(optarg
, "utf8")) {
375 fprintf(stderr
, "-T%s: Unsupported "
376 "output format\n", optarg
);
383 dup2(STDOUT_FILENO
, STDERR_FILENO
);
405 if (OP_CONFFILE
== op
&& argc
> 0) {
406 fprintf(stderr
, "-C: Too many arguments\n");
410 exitcode
= (int)MANDOCLEVEL_OK
;
411 mp
= mparse_alloc(MPARSE_AUTO
,
412 MANDOCLEVEL_FATAL
, NULL
, NULL
, NULL
);
415 ohash_init(&mpages
, 6, &mpages_info
);
416 ohash_init(&mlinks
, 6, &mlinks_info
);
418 if (OP_UPDATE
== op
|| OP_DELETE
== op
|| OP_TEST
== op
) {
420 * Force processing all files.
425 * All of these deal with a specific directory.
426 * Jump into that directory then collect files specified
427 * on the command-line.
429 if (0 == set_basedir(path_arg
))
431 for (i
= 0; i
< argc
; i
++)
438 mpages_merge(mc
, mp
, 0);
442 * If we have arguments, use them as our manpaths.
443 * If we don't, grok from manpath(1) or however else
444 * manpath_parse() wants to do it.
447 dirs
.paths
= mandoc_calloc
448 (argc
, sizeof(char *));
449 dirs
.sz
= (size_t)argc
;
450 for (i
= 0; i
< argc
; i
++)
451 dirs
.paths
[i
] = mandoc_strdup(argv
[i
]);
453 manpath_parse(&dirs
, path_arg
, NULL
, NULL
);
456 * First scan the tree rooted at a base directory, then
457 * build a new database and finally move it into place.
458 * Ignore zero-length directories and strip trailing
461 for (j
= 0; j
< dirs
.sz
; j
++) {
462 sz
= strlen(dirs
.paths
[j
]);
463 if (sz
&& '/' == dirs
.paths
[j
][sz
- 1])
464 dirs
.paths
[j
][--sz
] = '\0';
469 ohash_init(&mpages
, 6, &mpages_info
);
470 ohash_init(&mlinks
, 6, &mlinks_info
);
473 if (0 == set_basedir(dirs
.paths
[j
]))
477 if (0 == set_basedir(dirs
.paths
[j
]))
482 mpages_merge(mc
, mp
, warnings
&& !use_all
);
485 if (j
+ 1 < dirs
.sz
) {
487 ohash_delete(&mpages
);
488 ohash_delete(&mlinks
);
498 ohash_delete(&mpages
);
499 ohash_delete(&mlinks
);
502 fprintf(stderr
, "usage: %s [-anvW] [-C file] [-Tutf8]\n"
503 " %s [-anvW] [-Tutf8] dir ...\n"
504 " %s [-nvW] [-Tutf8] -d dir [file ...]\n"
505 " %s [-nvW] -u dir [file ...]\n"
507 progname
, progname
, progname
,
510 return((int)MANDOCLEVEL_BADARG
);
514 * Scan a directory tree rooted at "basedir" for manpages.
515 * We use fts(), scanning directory parts along the way for clues to our
516 * section and architecture.
518 * If use_all has been specified, grok all files.
519 * If not, sanitise paths to the following:
521 * [./]man*[/<arch>]/<name>.<section>
523 * [./]cat<section>[/<arch>]/<name>.0
525 * TODO: accomodate for multi-language directories.
534 char *dsec
, *arch
, *fsec
, *cp
;
539 argv
[1] = (char *)NULL
;
542 * Walk through all components under the directory, using the
543 * logical descent of files.
545 f
= fts_open((char * const *)argv
, FTS_LOGICAL
, NULL
);
547 exitcode
= (int)MANDOCLEVEL_SYSERR
;
555 while (NULL
!= (ff
= fts_read(f
))) {
556 path
= ff
->fts_path
+ 2;
558 * If we're a regular file, add an mlink by using the
559 * stored directory data and handling the filename.
561 if (FTS_F
== ff
->fts_info
) {
562 if (0 == strcmp(path
, MANDOC_DB
))
564 if ( ! use_all
&& ff
->fts_level
< 2) {
566 say(path
, "Extraneous file");
568 } else if (NULL
== (fsec
=
569 strrchr(ff
->fts_name
, '.'))) {
573 "No filename suffix");
576 } else if (0 == strcmp(++fsec
, "html")) {
578 say(path
, "Skip html");
580 } else if (0 == strcmp(fsec
, "gz")) {
582 say(path
, "Skip gz");
584 } else if (0 == strcmp(fsec
, "ps")) {
586 say(path
, "Skip ps");
588 } else if (0 == strcmp(fsec
, "pdf")) {
590 say(path
, "Skip pdf");
592 } else if ( ! use_all
&&
593 ((FORM_SRC
== dform
&& strcmp(fsec
, dsec
)) ||
594 (FORM_CAT
== dform
&& strcmp(fsec
, "0")))) {
596 say(path
, "Wrong filename suffix");
601 mlink
= mandoc_calloc(1, sizeof(struct mlink
));
602 strlcpy(mlink
->file
, path
, sizeof(mlink
->file
));
603 mlink
->dform
= dform
;
606 mlink
->name
= ff
->fts_name
;
608 mlink_add(mlink
, ff
->fts_statp
);
610 } else if (FTS_D
!= ff
->fts_info
&&
611 FTS_DP
!= ff
->fts_info
) {
613 say(path
, "Not a regular file");
617 switch (ff
->fts_level
) {
619 /* Ignore the root directory. */
623 * This might contain manX/ or catX/.
624 * Try to infer this from the name.
625 * If we're not in use_all, enforce it.
628 if (FTS_DP
== ff
->fts_info
)
631 if (0 == strncmp(cp
, "man", 3)) {
634 } else if (0 == strncmp(cp
, "cat", 3)) {
642 if (NULL
!= dsec
|| use_all
)
646 say(path
, "Unknown directory part");
647 fts_set(f
, ff
, FTS_SKIP
);
651 * Possibly our architecture.
652 * If we're descending, keep tabs on it.
654 if (FTS_DP
!= ff
->fts_info
&& NULL
!= dsec
)
660 if (FTS_DP
== ff
->fts_info
|| use_all
)
663 say(path
, "Extraneous directory part");
664 fts_set(f
, ff
, FTS_SKIP
);
674 * Add a file to the mlinks table.
675 * Do not verify that it's a "valid" looking manpage (we'll do that
678 * Try to infer the manual section, architecture, and page name from the
679 * path, assuming it looks like
681 * [./]man*[/<arch>]/<name>.<section>
683 * [./]cat<section>[/<arch>]/<name>.0
685 * See treescan() for the fts(3) version of this.
688 filescan(const char *file
)
697 if (0 == strncmp(file
, "./", 2))
700 if (NULL
== realpath(file
, buf
)) {
701 exitcode
= (int)MANDOCLEVEL_BADARG
;
704 } else if (OP_TEST
!= op
&& strstr(buf
, basedir
) != buf
) {
705 exitcode
= (int)MANDOCLEVEL_BADARG
;
706 say("", "%s: outside base directory", buf
);
708 } else if (-1 == stat(buf
, &st
)) {
709 exitcode
= (int)MANDOCLEVEL_BADARG
;
712 } else if ( ! (S_IFREG
& st
.st_mode
)) {
713 exitcode
= (int)MANDOCLEVEL_BADARG
;
714 say(file
, "Not a regular file");
717 start
= buf
+ strlen(basedir
);
718 mlink
= mandoc_calloc(1, sizeof(struct mlink
));
719 strlcpy(mlink
->file
, start
, sizeof(mlink
->file
));
722 * First try to guess our directory structure.
723 * If we find a separator, try to look for man* or cat*.
724 * If we find one of these and what's underneath is a directory,
725 * assume it's an architecture.
727 if (NULL
!= (p
= strchr(start
, '/'))) {
729 if (0 == strncmp(start
, "man", 3)) {
730 mlink
->dform
= FORM_SRC
;
731 mlink
->dsec
= start
+ 3;
732 } else if (0 == strncmp(start
, "cat", 3)) {
733 mlink
->dform
= FORM_CAT
;
734 mlink
->dsec
= start
+ 3;
738 if (NULL
!= mlink
->dsec
&& NULL
!= (p
= strchr(start
, '/'))) {
746 * Now check the file suffix.
747 * Suffix of `.0' indicates a catpage, `.1-9' is a manpage.
749 p
= strrchr(start
, '\0');
750 while (p
-- > start
&& '/' != *p
&& '.' != *p
)
759 * Now try to parse the name.
760 * Use the filename portion of the path.
763 if (NULL
!= (p
= strrchr(start
, '/'))) {
767 mlink_add(mlink
, &st
);
771 mlink_add(struct mlink
*mlink
, const struct stat
*st
)
773 struct inodev inodev
;
777 assert(NULL
!= mlink
->file
);
779 mlink
->dsec
= mandoc_strdup(mlink
->dsec
? mlink
->dsec
: "");
780 mlink
->arch
= mandoc_strdup(mlink
->arch
? mlink
->arch
: "");
781 mlink
->name
= mandoc_strdup(mlink
->name
? mlink
->name
: "");
782 mlink
->fsec
= mandoc_strdup(mlink
->fsec
? mlink
->fsec
: "");
784 if ('0' == *mlink
->fsec
) {
786 mlink
->fsec
= mandoc_strdup(mlink
->dsec
);
787 mlink
->fform
= FORM_CAT
;
788 } else if ('1' <= *mlink
->fsec
&& '9' >= *mlink
->fsec
)
789 mlink
->fform
= FORM_SRC
;
791 mlink
->fform
= FORM_NONE
;
793 slot
= ohash_qlookup(&mlinks
, mlink
->file
);
794 assert(NULL
== ohash_find(&mlinks
, slot
));
795 ohash_insert(&mlinks
, slot
, mlink
);
797 inodev
.st_ino
= st
->st_ino
;
798 inodev
.st_dev
= st
->st_dev
;
799 slot
= ohash_lookup_memory(&mpages
, (char *)&inodev
,
800 sizeof(struct inodev
), inodev
.st_ino
);
801 mpage
= ohash_find(&mpages
, slot
);
803 mpage
= mandoc_calloc(1, sizeof(struct mpage
));
804 mpage
->inodev
.st_ino
= inodev
.st_ino
;
805 mpage
->inodev
.st_dev
= inodev
.st_dev
;
806 ohash_insert(&mpages
, slot
, mpage
);
808 mlink
->next
= mpage
->mlinks
;
809 mpage
->mlinks
= mlink
;
813 mlink_free(struct mlink
*mlink
)
830 mpage
= ohash_first(&mpages
, &slot
);
831 while (NULL
!= mpage
) {
832 while (NULL
!= (mlink
= mpage
->mlinks
)) {
833 mpage
->mlinks
= mlink
->next
;
841 mpage
= ohash_next(&mpages
, &slot
);
846 * For each mlink to the mpage, check whether the path looks like
847 * it is formatted, and if it does, check whether a source manual
848 * exists by the same name, ignoring the suffix.
849 * If both conditions hold, drop the mlink.
852 mlinks_undupe(struct mpage
*mpage
)
859 mpage
->form
= FORM_CAT
;
860 prev
= &mpage
->mlinks
;
861 while (NULL
!= (mlink
= *prev
)) {
862 if (FORM_CAT
!= mlink
->dform
) {
863 mpage
->form
= FORM_NONE
;
866 if (strlcpy(buf
, mlink
->file
, PATH_MAX
) >= PATH_MAX
) {
868 say(mlink
->file
, "Filename too long");
871 bufp
= strstr(buf
, "cat");
872 assert(NULL
!= bufp
);
873 memcpy(bufp
, "man", 3);
874 if (NULL
!= (bufp
= strrchr(buf
, '.')))
876 strlcat(buf
, mlink
->dsec
, PATH_MAX
);
877 if (NULL
== ohash_find(&mlinks
,
878 ohash_qlookup(&mlinks
, buf
)))
881 say(mlink
->file
, "Man source exists: %s", buf
);
888 prev
= &(*prev
)->next
;
893 mlink_check(struct mpage
*mpage
, struct mlink
*mlink
)
900 * Check whether the manual section given in a file
901 * agrees with the directory where the file is located.
902 * Some manuals have suffixes like (3p) on their
903 * section number either inside the file or in the
904 * directory name, some are linked into more than one
905 * section, like encrypt(1) = makekey(8).
908 if (FORM_SRC
== mpage
->form
&&
909 strcasecmp(mpage
->sec
, mlink
->dsec
)) {
911 say(mlink
->file
, "Section \"%s\" manual in %s directory",
912 mpage
->sec
, mlink
->dsec
);
916 * Manual page directories exist for each kernel
917 * architecture as returned by machine(1).
918 * However, many manuals only depend on the
919 * application architecture as returned by arch(1).
920 * For example, some (2/ARM) manuals are shared
921 * across the "armish" and "zaurus" kernel
923 * A few manuals are even shared across completely
924 * different architectures, for example fdformat(1)
925 * on amd64, i386, sparc, and sparc64.
928 if (strcasecmp(mpage
->arch
, mlink
->arch
)) {
930 say(mlink
->file
, "Architecture \"%s\" manual in "
931 "\"%s\" directory", mpage
->arch
, mlink
->arch
);
934 if (strcasecmp(mpage
->title
, mlink
->name
))
941 * Run through the files in the global vector "mpages"
942 * and add them to the database specified in "basedir".
944 * This handles the parsing scheme itself, using the cues of directory
945 * and filename to determine whether the file is parsable or not.
948 mpages_merge(struct mchars
*mc
, struct mparse
*mp
, int check_reachable
)
950 struct ohash title_table
;
951 struct ohash_info title_info
, str_info
;
956 struct title
*title_entry
;
960 unsigned int pslot
, tslot
;
961 enum mandoclevel lvl
;
963 str_info
.alloc
= hash_alloc
;
964 str_info
.halloc
= hash_halloc
;
965 str_info
.hfree
= hash_free
;
966 str_info
.key_offset
= offsetof(struct str
, key
);
968 if (check_reachable
) {
969 title_info
.alloc
= hash_alloc
;
970 title_info
.halloc
= hash_halloc
;
971 title_info
.hfree
= hash_free
;
972 title_info
.key_offset
= offsetof(struct title
, title
);
973 ohash_init(&title_table
, 6, &title_info
);
976 mpage
= ohash_first(&mpages
, &pslot
);
977 while (NULL
!= mpage
) {
978 mlinks_undupe(mpage
);
979 if (NULL
== mpage
->mlinks
) {
980 mpage
= ohash_next(&mpages
, &pslot
);
984 ohash_init(&strings
, 6, &str_info
);
990 * Try interpreting the file as mdoc(7) or man(7)
991 * source code, unless it is already known to be
992 * formatted. Fall back to formatted mode.
994 if (FORM_CAT
!= mpage
->mlinks
->dform
||
995 FORM_CAT
!= mpage
->mlinks
->fform
) {
996 lvl
= mparse_readfd(mp
, -1, mpage
->mlinks
->file
);
997 if (lvl
< MANDOCLEVEL_FATAL
)
998 mparse_result(mp
, &mdoc
, &man
);
1002 mpage
->form
= FORM_SRC
;
1004 mandoc_strdup(mdoc_meta(mdoc
)->msec
);
1005 mpage
->arch
= mdoc_meta(mdoc
)->arch
;
1006 mpage
->arch
= mandoc_strdup(
1007 NULL
== mpage
->arch
? "" : mpage
->arch
);
1009 mandoc_strdup(mdoc_meta(mdoc
)->title
);
1010 } else if (NULL
!= man
) {
1011 mpage
->form
= FORM_SRC
;
1013 mandoc_strdup(man_meta(man
)->msec
);
1015 mandoc_strdup(mpage
->mlinks
->arch
);
1017 mandoc_strdup(man_meta(man
)->title
);
1019 mpage
->form
= FORM_CAT
;
1021 mandoc_strdup(mpage
->mlinks
->dsec
);
1023 mandoc_strdup(mpage
->mlinks
->arch
);
1025 mandoc_strdup(mpage
->mlinks
->name
);
1027 putkey(mpage
, mpage
->sec
, TYPE_sec
);
1028 putkey(mpage
, '\0' == *mpage
->arch
?
1029 "any" : mpage
->arch
, TYPE_arch
);
1031 for (mlink
= mpage
->mlinks
; mlink
; mlink
= mlink
->next
) {
1032 if ('\0' != *mlink
->dsec
)
1033 putkey(mpage
, mlink
->dsec
, TYPE_sec
);
1034 if ('\0' != *mlink
->fsec
)
1035 putkey(mpage
, mlink
->fsec
, TYPE_sec
);
1036 putkey(mpage
, '\0' == *mlink
->arch
?
1037 "any" : mlink
->arch
, TYPE_arch
);
1038 putkey(mpage
, mlink
->name
, TYPE_Nm
);
1041 if (warnings
&& !use_all
) {
1043 for (mlink
= mpage
->mlinks
; mlink
;
1044 mlink
= mlink
->next
)
1045 if (mlink_check(mpage
, mlink
))
1051 if (NULL
!= (cp
= mdoc_meta(mdoc
)->name
))
1052 putkey(mpage
, cp
, TYPE_Nm
);
1053 assert(NULL
== mpage
->desc
);
1054 parse_mdoc(mpage
, mdoc_node(mdoc
));
1055 putkey(mpage
, NULL
!= mpage
->desc
?
1056 mpage
->desc
: mpage
->mlinks
->name
, TYPE_Nd
);
1057 } else if (NULL
!= man
)
1058 parse_man(mpage
, man_node(man
));
1063 * Build a title string for the file. If it matches
1064 * the location of the file, remember the title as
1065 * found; else, remember it as missing.
1068 if (check_reachable
) {
1069 if (-1 == asprintf(&title_str
, "%s(%s%s%s)",
1070 mpage
->title
, mpage
->sec
,
1071 '\0' == *mpage
->arch
? "" : "/",
1074 exit((int)MANDOCLEVEL_SYSERR
);
1076 tslot
= ohash_qlookup(&title_table
, title_str
);
1077 title_entry
= ohash_find(&title_table
, tslot
);
1078 if (NULL
== title_entry
) {
1079 title_entry
= mandoc_malloc(
1080 sizeof(struct title
));
1081 title_entry
->title
= title_str
;
1082 title_entry
->file
= mandoc_strdup(
1083 match
? "" : mpage
->mlinks
->file
);
1084 ohash_insert(&title_table
, tslot
,
1088 *title_entry
->file
= '\0';
1094 ohash_delete(&strings
);
1095 mpage
= ohash_next(&mpages
, &pslot
);
1098 if (check_reachable
) {
1099 title_entry
= ohash_first(&title_table
, &tslot
);
1100 while (NULL
!= title_entry
) {
1101 if ('\0' != *title_entry
->file
)
1102 say(title_entry
->file
,
1103 "Probably unreachable, title is %s",
1104 title_entry
->title
);
1105 free(title_entry
->title
);
1106 free(title_entry
->file
);
1108 title_entry
= ohash_next(&title_table
, &tslot
);
1110 ohash_delete(&title_table
);
1115 parse_cat(struct mpage
*mpage
)
1118 char *line
, *p
, *title
;
1119 size_t len
, plen
, titlesz
;
1121 if (NULL
== (stream
= fopen(mpage
->mlinks
->file
, "r"))) {
1123 say(mpage
->mlinks
->file
, NULL
);
1127 /* Skip to first blank line. */
1129 while (NULL
!= (line
= fgetln(stream
, &len
)))
1134 * Assume the first line that is not indented
1135 * is the first section header. Skip to it.
1138 while (NULL
!= (line
= fgetln(stream
, &len
)))
1139 if ('\n' != *line
&& ' ' != *line
)
1143 * Read up until the next section into a buffer.
1144 * Strip the leading and trailing newline from each read line,
1145 * appending a trailing space.
1146 * Ignore empty (whitespace-only) lines.
1152 while (NULL
!= (line
= fgetln(stream
, &len
))) {
1153 if (' ' != *line
|| '\n' != line
[len
- 1])
1155 while (len
> 0 && isspace((unsigned char)*line
)) {
1161 title
= mandoc_realloc(title
, titlesz
+ len
);
1162 memcpy(title
+ titlesz
, line
, len
);
1164 title
[titlesz
- 1] = ' ';
1168 * If no page content can be found, or the input line
1169 * is already the next section header, or there is no
1170 * trailing newline, reuse the page title as the page
1174 if (NULL
== title
|| '\0' == *title
) {
1176 say(mpage
->mlinks
->file
,
1177 "Cannot find NAME section");
1178 assert(NULL
== mpage
->desc
);
1179 mpage
->desc
= mandoc_strdup(mpage
->mlinks
->name
);
1180 putkey(mpage
, mpage
->mlinks
->name
, TYPE_Nd
);
1186 title
= mandoc_realloc(title
, titlesz
+ 1);
1187 title
[titlesz
] = '\0';
1190 * Skip to the first dash.
1191 * Use the remaining line as the description (no more than 70
1195 if (NULL
!= (p
= strstr(title
, "- "))) {
1196 for (p
+= 2; ' ' == *p
|| '\b' == *p
; p
++)
1197 /* Skip to next word. */ ;
1200 say(mpage
->mlinks
->file
,
1201 "No dash in title line");
1207 /* Strip backspace-encoding from line. */
1209 while (NULL
!= (line
= memchr(p
, '\b', plen
))) {
1212 memmove(line
, line
+ 1, plen
--);
1215 memmove(line
- 1, line
+ 1, plen
- len
);
1219 assert(NULL
== mpage
->desc
);
1220 mpage
->desc
= mandoc_strdup(p
);
1221 putkey(mpage
, mpage
->desc
, TYPE_Nd
);
1227 * Put a type/word pair into the word database for this particular file.
1230 putkey(const struct mpage
*mpage
, const char *value
, uint64_t type
)
1233 assert(NULL
!= value
);
1234 putkeys(mpage
, value
, strlen(value
), type
);
1238 * Grok all nodes at or below a certain mdoc node into putkey().
1241 putmdockey(const struct mpage
*mpage
,
1242 const struct mdoc_node
*n
, uint64_t m
)
1245 for ( ; NULL
!= n
; n
= n
->next
) {
1246 if (NULL
!= n
->child
)
1247 putmdockey(mpage
, n
->child
, m
);
1248 if (MDOC_TEXT
== n
->type
)
1249 putkey(mpage
, n
->string
, m
);
1254 parse_man(struct mpage
*mpage
, const struct man_node
*n
)
1256 const struct man_node
*head
, *body
;
1257 char *start
, *sv
, *title
;
1265 * We're only searching for one thing: the first text child in
1266 * the BODY of a NAME section. Since we don't keep track of
1267 * sections in -man, run some hoops to find out whether we're in
1268 * the correct section or not.
1271 if (MAN_BODY
== n
->type
&& MAN_SH
== n
->tok
) {
1273 assert(body
->parent
);
1274 if (NULL
!= (head
= body
->parent
->head
) &&
1275 1 == head
->nchild
&&
1276 NULL
!= (head
= (head
->child
)) &&
1277 MAN_TEXT
== head
->type
&&
1278 0 == strcmp(head
->string
, "NAME") &&
1279 NULL
!= (body
= body
->child
) &&
1280 MAN_TEXT
== body
->type
) {
1286 * Suck the entire NAME section into memory.
1287 * Yes, we might run away.
1288 * But too many manuals have big, spread-out
1289 * NAME sections over many lines.
1292 for ( ; NULL
!= body
; body
= body
->next
) {
1293 if (MAN_TEXT
!= body
->type
)
1295 if (0 == (sz
= strlen(body
->string
)))
1297 title
= mandoc_realloc
1298 (title
, titlesz
+ sz
+ 1);
1299 memcpy(title
+ titlesz
, body
->string
, sz
);
1301 title
[titlesz
- 1] = ' ';
1306 title
= mandoc_realloc(title
, titlesz
+ 1);
1307 title
[titlesz
] = '\0';
1309 /* Skip leading space. */
1312 while (isspace((unsigned char)*sv
))
1315 if (0 == (sz
= strlen(sv
))) {
1320 /* Erase trailing space. */
1322 start
= &sv
[sz
- 1];
1323 while (start
> sv
&& isspace((unsigned char)*start
))
1334 * Go through a special heuristic dance here.
1335 * Conventionally, one or more manual names are
1336 * comma-specified prior to a whitespace, then a
1337 * dash, then a description. Try to puzzle out
1338 * the name parts here.
1342 sz
= strcspn(start
, " ,");
1343 if ('\0' == start
[sz
])
1349 putkey(mpage
, start
, TYPE_Nm
);
1356 assert(',' == byte
);
1358 while (' ' == *start
)
1363 putkey(mpage
, start
, TYPE_Nm
);
1368 while (isspace((unsigned char)*start
))
1371 if (0 == strncmp(start
, "-", 1))
1373 else if (0 == strncmp(start
, "\\-\\-", 4))
1375 else if (0 == strncmp(start
, "\\-", 2))
1377 else if (0 == strncmp(start
, "\\(en", 4))
1379 else if (0 == strncmp(start
, "\\(em", 4))
1382 while (' ' == *start
)
1385 assert(NULL
== mpage
->desc
);
1386 mpage
->desc
= mandoc_strdup(start
);
1387 putkey(mpage
, mpage
->desc
, TYPE_Nd
);
1393 for (n
= n
->child
; n
; n
= n
->next
) {
1394 if (NULL
!= mpage
->desc
)
1396 parse_man(mpage
, n
);
1401 parse_mdoc(struct mpage
*mpage
, const struct mdoc_node
*n
)
1405 for (n
= n
->child
; NULL
!= n
; n
= n
->next
) {
1416 if (NULL
!= mdocs
[n
->tok
].fp
)
1417 if (0 == (*mdocs
[n
->tok
].fp
)(mpage
, n
))
1419 if (mdocs
[n
->tok
].mask
)
1420 putmdockey(mpage
, n
->child
,
1421 mdocs
[n
->tok
].mask
);
1424 assert(MDOC_ROOT
!= n
->type
);
1427 if (NULL
!= n
->child
)
1428 parse_mdoc(mpage
, n
);
1433 parse_mdoc_Fd(struct mpage
*mpage
, const struct mdoc_node
*n
)
1435 const char *start
, *end
;
1438 if (SEC_SYNOPSIS
!= n
->sec
||
1439 NULL
== (n
= n
->child
) ||
1440 MDOC_TEXT
!= n
->type
)
1444 * Only consider those `Fd' macro fields that begin with an
1445 * "inclusion" token (versus, e.g., #define).
1448 if (strcmp("#include", n
->string
))
1451 if (NULL
== (n
= n
->next
) || MDOC_TEXT
!= n
->type
)
1455 * Strip away the enclosing angle brackets and make sure we're
1460 if ('<' == *start
|| '"' == *start
)
1463 if (0 == (sz
= strlen(start
)))
1466 end
= &start
[(int)sz
- 1];
1467 if ('>' == *end
|| '"' == *end
)
1471 putkeys(mpage
, start
, end
- start
+ 1, TYPE_In
);
1476 parse_mdoc_Fn(struct mpage
*mpage
, const struct mdoc_node
*n
)
1480 if (NULL
== (n
= n
->child
) || MDOC_TEXT
!= n
->type
)
1484 * Parse: .Fn "struct type *name" "char *arg".
1485 * First strip away pointer symbol.
1486 * Then store the function name, then type.
1487 * Finally, store the arguments.
1490 if (NULL
== (cp
= strrchr(n
->string
, ' ')))
1496 putkey(mpage
, cp
, TYPE_Fn
);
1499 putkeys(mpage
, n
->string
, cp
- n
->string
, TYPE_Ft
);
1501 for (n
= n
->next
; NULL
!= n
; n
= n
->next
)
1502 if (MDOC_TEXT
== n
->type
)
1503 putkey(mpage
, n
->string
, TYPE_Fa
);
1509 parse_mdoc_Xr(struct mpage
*mpage
, const struct mdoc_node
*n
)
1513 if (NULL
== (n
= n
->child
))
1516 if (NULL
== n
->next
) {
1517 putkey(mpage
, n
->string
, TYPE_Xr
);
1521 if (-1 == asprintf(&cp
, "%s(%s)", n
->string
, n
->next
->string
)) {
1523 exit((int)MANDOCLEVEL_SYSERR
);
1525 putkey(mpage
, cp
, TYPE_Xr
);
1531 parse_mdoc_Nd(struct mpage
*mpage
, const struct mdoc_node
*n
)
1535 if (MDOC_BODY
!= n
->type
)
1539 * Special-case the `Nd' because we need to put the description
1540 * into the document table.
1543 for (n
= n
->child
; NULL
!= n
; n
= n
->next
) {
1544 if (MDOC_TEXT
== n
->type
) {
1545 if (NULL
!= mpage
->desc
) {
1546 sz
= strlen(mpage
->desc
) +
1547 strlen(n
->string
) + 2;
1548 mpage
->desc
= mandoc_realloc(
1550 strlcat(mpage
->desc
, " ", sz
);
1551 strlcat(mpage
->desc
, n
->string
, sz
);
1553 mpage
->desc
= mandoc_strdup(n
->string
);
1555 if (NULL
!= n
->child
)
1556 parse_mdoc_Nd(mpage
, n
);
1562 parse_mdoc_Nm(struct mpage
*mpage
, const struct mdoc_node
*n
)
1565 return(SEC_NAME
== n
->sec
||
1566 (SEC_SYNOPSIS
== n
->sec
&& MDOC_HEAD
== n
->type
));
1570 parse_mdoc_Sh(struct mpage
*mpage
, const struct mdoc_node
*n
)
1573 return(SEC_CUSTOM
== n
->sec
&& MDOC_HEAD
== n
->type
);
1577 parse_mdoc_head(struct mpage
*mpage
, const struct mdoc_node
*n
)
1580 return(MDOC_HEAD
== n
->type
);
1584 parse_mdoc_body(struct mpage
*mpage
, const struct mdoc_node
*n
)
1587 return(MDOC_BODY
== n
->type
);
1591 * Add a string to the hash table for the current manual.
1592 * Each string has a bitmask telling which macros it belongs to.
1593 * When we finish the manual, we'll dump the table.
1596 putkeys(const struct mpage
*mpage
,
1597 const char *cp
, size_t sz
, uint64_t v
)
1607 slot
= ohash_qlookupi(&strings
, cp
, &end
);
1608 s
= ohash_find(&strings
, slot
);
1610 if (NULL
!= s
&& mpage
== s
->mpage
) {
1613 } else if (NULL
== s
) {
1614 s
= mandoc_calloc(sizeof(struct str
) + sz
+ 1, 1);
1615 memcpy(s
->key
, cp
, sz
);
1616 ohash_insert(&strings
, slot
, s
);
1623 * Take a Unicode codepoint and produce its UTF-8 encoding.
1624 * This isn't the best way to do this, but it works.
1625 * The magic numbers are from the UTF-8 packaging.
1626 * They're not as scary as they seem: read the UTF-8 spec for details.
1629 utf8(unsigned int cp
, char out
[7])
1634 if (cp
<= 0x0000007F) {
1637 } else if (cp
<= 0x000007FF) {
1639 out
[0] = (cp
>> 6 & 31) | 192;
1640 out
[1] = (cp
& 63) | 128;
1641 } else if (cp
<= 0x0000FFFF) {
1643 out
[0] = (cp
>> 12 & 15) | 224;
1644 out
[1] = (cp
>> 6 & 63) | 128;
1645 out
[2] = (cp
& 63) | 128;
1646 } else if (cp
<= 0x001FFFFF) {
1648 out
[0] = (cp
>> 18 & 7) | 240;
1649 out
[1] = (cp
>> 12 & 63) | 128;
1650 out
[2] = (cp
>> 6 & 63) | 128;
1651 out
[3] = (cp
& 63) | 128;
1652 } else if (cp
<= 0x03FFFFFF) {
1654 out
[0] = (cp
>> 24 & 3) | 248;
1655 out
[1] = (cp
>> 18 & 63) | 128;
1656 out
[2] = (cp
>> 12 & 63) | 128;
1657 out
[3] = (cp
>> 6 & 63) | 128;
1658 out
[4] = (cp
& 63) | 128;
1659 } else if (cp
<= 0x7FFFFFFF) {
1661 out
[0] = (cp
>> 30 & 1) | 252;
1662 out
[1] = (cp
>> 24 & 63) | 128;
1663 out
[2] = (cp
>> 18 & 63) | 128;
1664 out
[3] = (cp
>> 12 & 63) | 128;
1665 out
[4] = (cp
>> 6 & 63) | 128;
1666 out
[5] = (cp
& 63) | 128;
1675 * Store the rendered version of a key, or alias the pointer
1676 * if the key contains no escape sequences.
1679 render_key(struct mchars
*mc
, struct str
*key
)
1681 size_t sz
, bsz
, pos
;
1682 char utfbuf
[7], res
[5];
1684 const char *seq
, *cpp
, *val
;
1686 enum mandoc_esc esc
;
1688 assert(NULL
== key
->rendered
);
1692 res
[2] = ASCII_NBRSP
;
1693 res
[3] = ASCII_HYPH
;
1700 * Pre-check: if we have no stop-characters, then set the
1701 * pointer as ourselvse and get out of here.
1703 if (strcspn(val
, res
) == bsz
) {
1704 key
->rendered
= key
->key
;
1708 /* Pre-allocate by the length of the input */
1710 buf
= mandoc_malloc(++bsz
);
1713 while ('\0' != *val
) {
1715 * Halt on the first escape sequence.
1716 * This also halts on the end of string, in which case
1717 * we just copy, fallthrough, and exit the loop.
1719 if ((sz
= strcspn(val
, res
)) > 0) {
1720 memcpy(&buf
[pos
], val
, sz
);
1725 if (ASCII_HYPH
== *val
) {
1729 } else if ('\t' == *val
|| ASCII_NBRSP
== *val
) {
1733 } else if ('\\' != *val
)
1736 /* Read past the slash. */
1741 * Parse the escape sequence and see if it's a
1742 * predefined character or special character.
1746 ((const char **)&val
, &seq
, &len
);
1747 if (ESCAPE_ERROR
== esc
)
1749 if (ESCAPE_SPECIAL
!= esc
)
1753 * Render the special character
1754 * as either UTF-8 or ASCII.
1758 if (0 == (u
= mchars_spec2cp(mc
, seq
, len
)))
1761 if (0 == (sz
= utf8(u
, utfbuf
)))
1765 cpp
= mchars_spec2str(mc
, seq
, len
, &sz
);
1768 if (ASCII_NBRSP
== *cpp
) {
1774 /* Copy the rendered glyph into the stream. */
1777 buf
= mandoc_realloc(buf
, bsz
);
1778 memcpy(&buf
[pos
], cpp
, sz
);
1783 key
->rendered
= buf
;
1787 * Flush the current page's terms (and their bits) into the database.
1788 * Wrap the entire set of additions in a transaction to make sqlite be a
1790 * Also, handle escape sequences at the last possible moment.
1793 dbindex(const struct mpage
*mpage
, struct mchars
*mc
)
1795 struct mlink
*mlink
;
1803 say(mpage
->mlinks
->file
, "Adding to index");
1809 if (NULL
!= mpage
->desc
&& '\0' != *mpage
->desc
) {
1810 key
= ohash_find(&strings
,
1811 ohash_qlookup(&strings
, mpage
->desc
));
1812 assert(NULL
!= key
);
1813 if (NULL
== key
->rendered
)
1814 render_key(mc
, key
);
1815 desc
= key
->rendered
;
1818 SQL_EXEC("BEGIN TRANSACTION");
1821 SQL_BIND_TEXT(stmts
[STMT_INSERT_PAGE
], i
, desc
);
1822 SQL_BIND_INT(stmts
[STMT_INSERT_PAGE
], i
, FORM_SRC
== mpage
->form
);
1823 SQL_STEP(stmts
[STMT_INSERT_PAGE
]);
1824 recno
= sqlite3_last_insert_rowid(db
);
1825 sqlite3_reset(stmts
[STMT_INSERT_PAGE
]);
1827 for (mlink
= mpage
->mlinks
; mlink
; mlink
= mlink
->next
) {
1829 SQL_BIND_TEXT(stmts
[STMT_INSERT_LINK
], i
, mlink
->file
);
1830 SQL_BIND_TEXT(stmts
[STMT_INSERT_LINK
], i
, mlink
->dsec
);
1831 SQL_BIND_TEXT(stmts
[STMT_INSERT_LINK
], i
, mlink
->arch
);
1832 SQL_BIND_TEXT(stmts
[STMT_INSERT_LINK
], i
, mlink
->name
);
1833 SQL_BIND_INT64(stmts
[STMT_INSERT_LINK
], i
, recno
);
1834 SQL_STEP(stmts
[STMT_INSERT_LINK
]);
1835 sqlite3_reset(stmts
[STMT_INSERT_LINK
]);
1838 for (key
= ohash_first(&strings
, &slot
); NULL
!= key
;
1839 key
= ohash_next(&strings
, &slot
)) {
1840 assert(key
->mpage
== mpage
);
1841 if (NULL
== key
->rendered
)
1842 render_key(mc
, key
);
1844 SQL_BIND_INT64(stmts
[STMT_INSERT_KEY
], i
, key
->mask
);
1845 SQL_BIND_TEXT(stmts
[STMT_INSERT_KEY
], i
, key
->rendered
);
1846 SQL_BIND_INT64(stmts
[STMT_INSERT_KEY
], i
, recno
);
1847 SQL_STEP(stmts
[STMT_INSERT_KEY
]);
1848 sqlite3_reset(stmts
[STMT_INSERT_KEY
]);
1849 if (key
->rendered
!= key
->key
)
1850 free(key
->rendered
);
1854 SQL_EXEC("END TRANSACTION");
1860 struct mpage
*mpage
;
1861 struct mlink
*mlink
;
1868 mpage
= ohash_first(&mpages
, &slot
);
1869 while (NULL
!= mpage
) {
1870 mlink
= mpage
->mlinks
;
1872 SQL_BIND_TEXT(stmts
[STMT_DELETE_PAGE
], i
, mlink
->file
);
1873 SQL_STEP(stmts
[STMT_DELETE_PAGE
]);
1874 sqlite3_reset(stmts
[STMT_DELETE_PAGE
]);
1876 say(mlink
->file
, "Deleted from index");
1877 mpage
= ohash_next(&mpages
, &slot
);
1882 * Close an existing database and its prepared statements.
1883 * If "real" is not set, rename the temporary file into the real one.
1893 for (i
= 0; i
< STMT__MAX
; i
++) {
1894 sqlite3_finalize(stmts
[i
]);
1904 if (-1 == rename(MANDOC_DB
"~", MANDOC_DB
)) {
1905 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1906 say(MANDOC_DB
, NULL
);
1911 * This is straightforward stuff.
1912 * Open a database connection to a "temporary" database, then open a set
1913 * of prepared statements we'll use over and over again.
1914 * If "real" is set, we use the existing database; if not, we truncate a
1916 * Must be matched by dbclose().
1921 const char *file
, *sql
;
1927 ofl
= SQLITE_OPEN_READWRITE
;
1929 file
= MANDOC_DB
"~";
1930 if (-1 == remove(file
) && ENOENT
!= errno
) {
1931 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1935 ofl
|= SQLITE_OPEN_EXCLUSIVE
;
1939 rc
= sqlite3_open_v2(file
, &db
, ofl
, NULL
);
1940 if (SQLITE_OK
== rc
)
1941 goto prepare_statements
;
1942 if (SQLITE_CANTOPEN
!= rc
) {
1943 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1951 if (SQLITE_OK
!= (rc
= sqlite3_open(file
, &db
))) {
1952 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1957 sql
= "CREATE TABLE \"mpages\" (\n"
1958 " \"desc\" TEXT NOT NULL,\n"
1959 " \"form\" INTEGER NOT NULL,\n"
1960 " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
1963 "CREATE TABLE \"mlinks\" (\n"
1964 " \"file\" TEXT NOT NULL,\n"
1965 " \"sec\" TEXT NOT NULL,\n"
1966 " \"arch\" TEXT NOT NULL,\n"
1967 " \"name\" TEXT NOT NULL,\n"
1968 " \"pageid\" INTEGER NOT NULL REFERENCES mpages(id) "
1969 "ON DELETE CASCADE,\n"
1970 " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
1973 "CREATE TABLE \"keys\" (\n"
1974 " \"bits\" INTEGER NOT NULL,\n"
1975 " \"key\" TEXT NOT NULL,\n"
1976 " \"pageid\" INTEGER NOT NULL REFERENCES mpages(id) "
1977 "ON DELETE CASCADE,\n"
1978 " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
1981 "CREATE INDEX \"key_index\" ON keys (key);\n";
1983 if (SQLITE_OK
!= sqlite3_exec(db
, sql
, NULL
, NULL
, NULL
)) {
1984 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1985 say(file
, "%s", sqlite3_errmsg(db
));
1990 SQL_EXEC("PRAGMA foreign_keys = ON");
1991 sql
= "DELETE FROM mpages where file=?";
1992 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_DELETE_PAGE
], NULL
);
1993 sql
= "INSERT INTO mpages "
1994 "(desc,form) VALUES (?,?)";
1995 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_PAGE
], NULL
);
1996 sql
= "INSERT INTO mlinks "
1997 "(file,sec,arch,name,pageid) VALUES (?,?,?,?,?)";
1998 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_LINK
], NULL
);
1999 sql
= "INSERT INTO keys "
2000 "(bits,key,pageid) VALUES (?,?,?)";
2001 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_KEY
], NULL
);
2005 * When opening a new database, we can turn off
2006 * synchronous mode for much better performance.
2010 SQL_EXEC("PRAGMA synchronous = OFF");
2017 hash_halloc(size_t sz
, void *arg
)
2020 return(mandoc_calloc(sz
, 1));
2024 hash_alloc(size_t sz
, void *arg
)
2027 return(mandoc_malloc(sz
));
2031 hash_free(void *p
, size_t sz
, void *arg
)
2038 set_basedir(const char *targetdir
)
2040 static char startdir
[PATH_MAX
];
2044 * Remember where we started by keeping a fd open to the origin
2045 * path component: throughout this utility, we chdir() a lot to
2046 * handle relative paths, and by doing this, we can return to
2047 * the starting point.
2049 if ('\0' == *startdir
) {
2050 if (NULL
== getcwd(startdir
, PATH_MAX
)) {
2051 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2052 if (NULL
!= targetdir
)
2056 if (-1 == (fd
= open(startdir
, O_RDONLY
, 0))) {
2057 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2058 say(startdir
, NULL
);
2061 if (NULL
== targetdir
)
2062 targetdir
= startdir
;
2066 if (-1 == fchdir(fd
)) {
2069 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2070 say(startdir
, NULL
);
2073 if (NULL
== targetdir
) {
2078 if (NULL
== realpath(targetdir
, basedir
)) {
2080 exitcode
= (int)MANDOCLEVEL_BADARG
;
2081 say(targetdir
, NULL
);
2083 } else if (-1 == chdir(basedir
)) {
2084 exitcode
= (int)MANDOCLEVEL_BADARG
;
2092 say(const char *file
, const char *format
, ...)
2096 if ('\0' != *basedir
)
2097 fprintf(stderr
, "%s", basedir
);
2098 if ('\0' != *basedir
&& '\0' != *file
)
2099 fputs("//", stderr
);
2101 fprintf(stderr
, "%s", file
);
2102 fputs(": ", stderr
);
2104 if (NULL
== format
) {
2109 va_start(ap
, format
);
2110 vfprintf(stderr
, format
, ap
);
2113 fputc('\n', stderr
);