]>
git.cameronkatri.com Git - mandoc.git/blob - mandocdb.c
1 /* $Id: mandocdb.c,v 1.133 2014/04/10 02:46:21 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.
42 #include "compat_ohash.h"
49 #include "mandoc_aux.h"
51 #include "mansearch.h"
53 extern int mansearch_keymax
;
54 extern const char *const mansearch_keynames
[];
56 #define SQL_EXEC(_v) \
57 if (SQLITE_OK != sqlite3_exec(db, (_v), NULL, NULL, NULL)) \
58 fprintf(stderr, "%s\n", sqlite3_errmsg(db))
59 #define SQL_BIND_TEXT(_s, _i, _v) \
60 if (SQLITE_OK != sqlite3_bind_text \
61 ((_s), (_i)++, (_v), -1, SQLITE_STATIC)) \
62 fprintf(stderr, "%s\n", sqlite3_errmsg(db))
63 #define SQL_BIND_INT(_s, _i, _v) \
64 if (SQLITE_OK != sqlite3_bind_int \
65 ((_s), (_i)++, (_v))) \
66 fprintf(stderr, "%s\n", sqlite3_errmsg(db))
67 #define SQL_BIND_INT64(_s, _i, _v) \
68 if (SQLITE_OK != sqlite3_bind_int64 \
69 ((_s), (_i)++, (_v))) \
70 fprintf(stderr, "%s\n", sqlite3_errmsg(db))
71 #define SQL_STEP(_s) \
72 if (SQLITE_DONE != sqlite3_step((_s))) \
73 fprintf(stderr, "%s\n", sqlite3_errmsg(db))
76 OP_DEFAULT
= 0, /* new dbs from dir list or default config */
77 OP_CONFFILE
, /* new databases from custom config file */
78 OP_UPDATE
, /* delete/add entries in existing database */
79 OP_DELETE
, /* delete entries from existing database */
80 OP_TEST
/* change no databases, report potential problems */
84 FORM_NONE
, /* format is unknown */
85 FORM_SRC
, /* format is -man or -mdoc */
86 FORM_CAT
/* format is cat */
90 char *rendered
; /* key in UTF-8 or ASCII form */
91 const struct mpage
*mpage
; /* if set, the owning parse */
92 uint64_t mask
; /* bitmask in sequence */
93 char key
[]; /* may contain escape sequences */
102 struct inodev inodev
; /* used for hashing routine */
103 int64_t recno
; /* id in mpages SQL table */
104 enum form form
; /* format from file content */
105 char *sec
; /* section from file content */
106 char *arch
; /* architecture from file content */
107 char *title
; /* title from file content */
108 char *desc
; /* description from file content */
109 struct mlink
*mlinks
; /* singly linked list */
113 char file
[PATH_MAX
]; /* filename rel. to manpath */
114 enum form dform
; /* format from directory */
115 enum form fform
; /* format from file name suffix */
116 char *dsec
; /* section from directory */
117 char *arch
; /* architecture from directory */
118 char *name
; /* name from file name (not empty) */
119 char *fsec
; /* section from file name suffix */
120 struct mlink
*next
; /* singly linked list */
121 struct mpage
*mpage
; /* parent */
122 int gzip
; /* filename has a .gz suffix */
126 STMT_DELETE_PAGE
= 0, /* delete mpage */
127 STMT_INSERT_PAGE
, /* insert mpage */
128 STMT_INSERT_LINK
, /* insert mlink */
129 STMT_INSERT_NAME
, /* insert name */
130 STMT_INSERT_KEY
, /* insert parsed key */
134 typedef int (*mdoc_fp
)(struct mpage
*, const struct mdoc_node
*);
136 struct mdoc_handler
{
137 mdoc_fp fp
; /* optional handler */
138 uint64_t mask
; /* set unless handler returns 0 */
141 static void dbclose(int);
142 static void dbadd(struct mpage
*, struct mchars
*);
143 static void dbadd_mlink(const struct mlink
*mlink
);
144 static int dbopen(int);
145 static void dbprune(void);
146 static void filescan(const char *);
147 static void *hash_alloc(size_t, void *);
148 static void hash_free(void *, size_t, void *);
149 static void *hash_halloc(size_t, void *);
150 static void mlink_add(struct mlink
*, const struct stat
*);
151 static void mlink_check(struct mpage
*, struct mlink
*);
152 static void mlink_free(struct mlink
*);
153 static void mlinks_undupe(struct mpage
*);
154 static void mpages_free(void);
155 static void mpages_merge(struct mchars
*, struct mparse
*);
156 static void names_check(void);
157 static void parse_cat(struct mpage
*, int);
158 static void parse_man(struct mpage
*, const struct man_node
*);
159 static void parse_mdoc(struct mpage
*, const struct mdoc_node
*);
160 static int parse_mdoc_body(struct mpage
*, const struct mdoc_node
*);
161 static int parse_mdoc_head(struct mpage
*, const struct mdoc_node
*);
162 static int parse_mdoc_Fd(struct mpage
*, const struct mdoc_node
*);
163 static int parse_mdoc_Fn(struct mpage
*, const struct mdoc_node
*);
164 static int parse_mdoc_Nd(struct mpage
*, const struct mdoc_node
*);
165 static int parse_mdoc_Nm(struct mpage
*, const struct mdoc_node
*);
166 static int parse_mdoc_Sh(struct mpage
*, const struct mdoc_node
*);
167 static int parse_mdoc_Xr(struct mpage
*, const struct mdoc_node
*);
168 static void putkey(const struct mpage
*, char *, uint64_t);
169 static void putkeys(const struct mpage
*,
170 const char *, size_t, uint64_t);
171 static void putmdockey(const struct mpage
*,
172 const struct mdoc_node
*, uint64_t);
173 static void render_key(struct mchars
*, struct str
*);
174 static void say(const char *, const char *, ...);
175 static int set_basedir(const char *);
176 static int treescan(void);
177 static size_t utf8(unsigned int, char [7]);
179 static char tempfilename
[32];
180 static char *progname
;
181 static int nodb
; /* no database changes */
182 static int mparse_options
; /* abort the parse early */
183 static int use_all
; /* use all found files */
184 static int debug
; /* print what we're doing */
185 static int warnings
; /* warn about crap */
186 static int write_utf8
; /* write UTF-8 output; else ASCII */
187 static int exitcode
; /* to be returned by main */
188 static enum op op
; /* operational mode */
189 static char basedir
[PATH_MAX
]; /* current base directory */
190 static struct ohash mpages
; /* table of distinct manual pages */
191 static struct ohash mlinks
; /* table of directory entries */
192 static struct ohash names
; /* table of all names */
193 static struct ohash strings
; /* table of all strings */
194 static sqlite3
*db
= NULL
; /* current database */
195 static sqlite3_stmt
*stmts
[STMT__MAX
]; /* current statements */
196 static uint64_t name_mask
;
198 static const struct mdoc_handler mdocs
[MDOC_MAX
] = {
199 { NULL
, 0 }, /* Ap */
200 { NULL
, 0 }, /* Dd */
201 { NULL
, 0 }, /* Dt */
202 { NULL
, 0 }, /* Os */
203 { parse_mdoc_Sh
, TYPE_Sh
}, /* Sh */
204 { parse_mdoc_head
, TYPE_Ss
}, /* Ss */
205 { NULL
, 0 }, /* Pp */
206 { NULL
, 0 }, /* D1 */
207 { NULL
, 0 }, /* Dl */
208 { NULL
, 0 }, /* Bd */
209 { NULL
, 0 }, /* Ed */
210 { NULL
, 0 }, /* Bl */
211 { NULL
, 0 }, /* El */
212 { NULL
, 0 }, /* It */
213 { NULL
, 0 }, /* Ad */
214 { NULL
, TYPE_An
}, /* An */
215 { NULL
, TYPE_Ar
}, /* Ar */
216 { NULL
, TYPE_Cd
}, /* Cd */
217 { NULL
, TYPE_Cm
}, /* Cm */
218 { NULL
, TYPE_Dv
}, /* Dv */
219 { NULL
, TYPE_Er
}, /* Er */
220 { NULL
, TYPE_Ev
}, /* Ev */
221 { NULL
, 0 }, /* Ex */
222 { NULL
, TYPE_Fa
}, /* Fa */
223 { parse_mdoc_Fd
, 0 }, /* Fd */
224 { NULL
, TYPE_Fl
}, /* Fl */
225 { parse_mdoc_Fn
, 0 }, /* Fn */
226 { NULL
, TYPE_Ft
}, /* Ft */
227 { NULL
, TYPE_Ic
}, /* Ic */
228 { NULL
, TYPE_In
}, /* In */
229 { NULL
, TYPE_Li
}, /* Li */
230 { parse_mdoc_Nd
, 0 }, /* Nd */
231 { parse_mdoc_Nm
, 0 }, /* Nm */
232 { NULL
, 0 }, /* Op */
233 { NULL
, 0 }, /* Ot */
234 { NULL
, TYPE_Pa
}, /* Pa */
235 { NULL
, 0 }, /* Rv */
236 { NULL
, TYPE_St
}, /* St */
237 { NULL
, TYPE_Va
}, /* Va */
238 { parse_mdoc_body
, TYPE_Va
}, /* Vt */
239 { parse_mdoc_Xr
, 0 }, /* Xr */
240 { NULL
, 0 }, /* %A */
241 { NULL
, 0 }, /* %B */
242 { NULL
, 0 }, /* %D */
243 { NULL
, 0 }, /* %I */
244 { NULL
, 0 }, /* %J */
245 { NULL
, 0 }, /* %N */
246 { NULL
, 0 }, /* %O */
247 { NULL
, 0 }, /* %P */
248 { NULL
, 0 }, /* %R */
249 { NULL
, 0 }, /* %T */
250 { NULL
, 0 }, /* %V */
251 { NULL
, 0 }, /* Ac */
252 { NULL
, 0 }, /* Ao */
253 { NULL
, 0 }, /* Aq */
254 { NULL
, TYPE_At
}, /* At */
255 { NULL
, 0 }, /* Bc */
256 { NULL
, 0 }, /* Bf */
257 { NULL
, 0 }, /* Bo */
258 { NULL
, 0 }, /* Bq */
259 { NULL
, TYPE_Bsx
}, /* Bsx */
260 { NULL
, TYPE_Bx
}, /* Bx */
261 { NULL
, 0 }, /* Db */
262 { NULL
, 0 }, /* Dc */
263 { NULL
, 0 }, /* Do */
264 { NULL
, 0 }, /* Dq */
265 { NULL
, 0 }, /* Ec */
266 { NULL
, 0 }, /* Ef */
267 { NULL
, TYPE_Em
}, /* Em */
268 { NULL
, 0 }, /* Eo */
269 { NULL
, TYPE_Fx
}, /* Fx */
270 { NULL
, TYPE_Ms
}, /* Ms */
271 { NULL
, 0 }, /* No */
272 { NULL
, 0 }, /* Ns */
273 { NULL
, TYPE_Nx
}, /* Nx */
274 { NULL
, TYPE_Ox
}, /* Ox */
275 { NULL
, 0 }, /* Pc */
276 { NULL
, 0 }, /* Pf */
277 { NULL
, 0 }, /* Po */
278 { NULL
, 0 }, /* Pq */
279 { NULL
, 0 }, /* Qc */
280 { NULL
, 0 }, /* Ql */
281 { NULL
, 0 }, /* Qo */
282 { NULL
, 0 }, /* Qq */
283 { NULL
, 0 }, /* Re */
284 { NULL
, 0 }, /* Rs */
285 { NULL
, 0 }, /* Sc */
286 { NULL
, 0 }, /* So */
287 { NULL
, 0 }, /* Sq */
288 { NULL
, 0 }, /* Sm */
289 { NULL
, 0 }, /* Sx */
290 { NULL
, TYPE_Sy
}, /* Sy */
291 { NULL
, TYPE_Tn
}, /* Tn */
292 { NULL
, 0 }, /* Ux */
293 { NULL
, 0 }, /* Xc */
294 { NULL
, 0 }, /* Xo */
295 { parse_mdoc_head
, 0 }, /* Fo */
296 { NULL
, 0 }, /* Fc */
297 { NULL
, 0 }, /* Oo */
298 { NULL
, 0 }, /* Oc */
299 { NULL
, 0 }, /* Bk */
300 { NULL
, 0 }, /* Ek */
301 { NULL
, 0 }, /* Bt */
302 { NULL
, 0 }, /* Hf */
303 { NULL
, 0 }, /* Fr */
304 { NULL
, 0 }, /* Ud */
305 { NULL
, TYPE_Lb
}, /* Lb */
306 { NULL
, 0 }, /* Lp */
307 { NULL
, TYPE_Lk
}, /* Lk */
308 { NULL
, TYPE_Mt
}, /* Mt */
309 { NULL
, 0 }, /* Brq */
310 { NULL
, 0 }, /* Bro */
311 { NULL
, 0 }, /* Brc */
312 { NULL
, 0 }, /* %C */
313 { NULL
, 0 }, /* Es */
314 { NULL
, 0 }, /* En */
315 { NULL
, TYPE_Dx
}, /* Dx */
316 { NULL
, 0 }, /* %Q */
317 { NULL
, 0 }, /* br */
318 { NULL
, 0 }, /* sp */
319 { NULL
, 0 }, /* %U */
320 { NULL
, 0 }, /* Ta */
324 main(int argc
, char *argv
[])
328 const char *path_arg
;
330 struct manpaths dirs
;
332 struct ohash_info mpages_info
, mlinks_info
;
334 memset(stmts
, 0, STMT__MAX
* sizeof(sqlite3_stmt
*));
335 memset(&dirs
, 0, sizeof(struct manpaths
));
337 mpages_info
.alloc
= mlinks_info
.alloc
= hash_alloc
;
338 mpages_info
.halloc
= mlinks_info
.halloc
= hash_halloc
;
339 mpages_info
.hfree
= mlinks_info
.hfree
= hash_free
;
341 mpages_info
.key_offset
= offsetof(struct mpage
, inodev
);
342 mlinks_info
.key_offset
= offsetof(struct mlink
, file
);
344 progname
= strrchr(argv
[0], '/');
345 if (progname
== NULL
)
351 * We accept a few different invocations.
352 * The CHECKOP macro makes sure that invocation styles don't
353 * clobber each other.
355 #define CHECKOP(_op, _ch) do \
356 if (OP_DEFAULT != (_op)) { \
357 fprintf(stderr, "-%c: Conflicting option\n", (_ch)); \
359 } while (/*CONSTCOND*/0)
364 while (-1 != (ch
= getopt(argc
, argv
, "aC:Dd:npQT:tu:v")))
389 mparse_options
|= MPARSE_QUICK
;
392 if (strcmp(optarg
, "utf8")) {
393 fprintf(stderr
, "-T%s: Unsupported "
394 "output format\n", optarg
);
401 dup2(STDOUT_FILENO
, STDERR_FILENO
);
411 /* Compatibility with espie@'s makewhatis. */
420 if (OP_CONFFILE
== op
&& argc
> 0) {
421 fprintf(stderr
, "-C: Too many arguments\n");
425 exitcode
= (int)MANDOCLEVEL_OK
;
426 mp
= mparse_alloc(mparse_options
, MANDOCLEVEL_FATAL
, NULL
, NULL
);
429 ohash_init(&mpages
, 6, &mpages_info
);
430 ohash_init(&mlinks
, 6, &mlinks_info
);
432 if (OP_UPDATE
== op
|| OP_DELETE
== op
|| OP_TEST
== op
) {
434 * Force processing all files.
439 * All of these deal with a specific directory.
440 * Jump into that directory then collect files specified
441 * on the command-line.
443 if (0 == set_basedir(path_arg
))
445 for (i
= 0; i
< argc
; i
++)
452 mpages_merge(mc
, mp
);
456 * If we have arguments, use them as our manpaths.
457 * If we don't, grok from manpath(1) or however else
458 * manpath_parse() wants to do it.
461 dirs
.paths
= mandoc_calloc
462 (argc
, sizeof(char *));
463 dirs
.sz
= (size_t)argc
;
464 for (i
= 0; i
< argc
; i
++)
465 dirs
.paths
[i
] = mandoc_strdup(argv
[i
]);
467 manpath_parse(&dirs
, path_arg
, NULL
, NULL
);
470 exitcode
= (int)MANDOCLEVEL_BADARG
;
471 say("", "Empty manpath");
475 * First scan the tree rooted at a base directory, then
476 * build a new database and finally move it into place.
477 * Ignore zero-length directories and strip trailing
480 for (j
= 0; j
< dirs
.sz
; j
++) {
481 sz
= strlen(dirs
.paths
[j
]);
482 if (sz
&& '/' == dirs
.paths
[j
][sz
- 1])
483 dirs
.paths
[j
][--sz
] = '\0';
488 ohash_init(&mpages
, 6, &mpages_info
);
489 ohash_init(&mlinks
, 6, &mlinks_info
);
492 if (0 == set_basedir(dirs
.paths
[j
]))
496 if (0 == set_basedir(dirs
.paths
[j
]))
501 mpages_merge(mc
, mp
);
503 ! (MPARSE_QUICK
& mparse_options
))
507 if (j
+ 1 < dirs
.sz
) {
509 ohash_delete(&mpages
);
510 ohash_delete(&mlinks
);
520 ohash_delete(&mpages
);
521 ohash_delete(&mlinks
);
524 fprintf(stderr
, "usage: %s [-aDnpQ] [-C file] [-Tutf8]\n"
525 " %s [-aDnpQ] [-Tutf8] dir ...\n"
526 " %s [-DnpQ] [-Tutf8] -d dir [file ...]\n"
527 " %s [-Dnp] -u dir [file ...]\n"
528 " %s [-Q] -t file ...\n",
529 progname
, progname
, progname
,
532 return((int)MANDOCLEVEL_BADARG
);
536 * Scan a directory tree rooted at "basedir" for manpages.
537 * We use fts(), scanning directory parts along the way for clues to our
538 * section and architecture.
540 * If use_all has been specified, grok all files.
541 * If not, sanitise paths to the following:
543 * [./]man*[/<arch>]/<name>.<section>
545 * [./]cat<section>[/<arch>]/<name>.0
547 * TODO: accomodate for multi-language directories.
556 char *dsec
, *arch
, *fsec
, *cp
;
561 argv
[1] = (char *)NULL
;
564 * Walk through all components under the directory, using the
565 * logical descent of files.
567 f
= fts_open((char * const *)argv
, FTS_LOGICAL
, NULL
);
569 exitcode
= (int)MANDOCLEVEL_SYSERR
;
570 say("", "&fts_open");
577 while (NULL
!= (ff
= fts_read(f
))) {
578 path
= ff
->fts_path
+ 2;
580 * If we're a regular file, add an mlink by using the
581 * stored directory data and handling the filename.
583 if (FTS_F
== ff
->fts_info
) {
584 if (0 == strcmp(path
, MANDOC_DB
))
586 if ( ! use_all
&& ff
->fts_level
< 2) {
588 say(path
, "Extraneous file");
593 while (NULL
== fsec
) {
594 fsec
= strrchr(ff
->fts_name
, '.');
595 if (NULL
== fsec
|| strcmp(fsec
+1, "gz"))
605 "No filename suffix");
608 } else if (0 == strcmp(++fsec
, "html")) {
610 say(path
, "Skip html");
612 } else if (0 == strcmp(fsec
, "ps")) {
614 say(path
, "Skip ps");
616 } else if (0 == strcmp(fsec
, "pdf")) {
618 say(path
, "Skip pdf");
620 } else if ( ! use_all
&&
621 ((FORM_SRC
== dform
&& strcmp(fsec
, dsec
)) ||
622 (FORM_CAT
== dform
&& strcmp(fsec
, "0")))) {
624 say(path
, "Wrong filename suffix");
629 mlink
= mandoc_calloc(1, sizeof(struct mlink
));
630 strlcpy(mlink
->file
, path
, sizeof(mlink
->file
));
631 mlink
->dform
= dform
;
634 mlink
->name
= ff
->fts_name
;
637 mlink_add(mlink
, ff
->fts_statp
);
639 } else if (FTS_D
!= ff
->fts_info
&&
640 FTS_DP
!= ff
->fts_info
) {
642 say(path
, "Not a regular file");
646 switch (ff
->fts_level
) {
648 /* Ignore the root directory. */
652 * This might contain manX/ or catX/.
653 * Try to infer this from the name.
654 * If we're not in use_all, enforce it.
657 if (FTS_DP
== ff
->fts_info
)
660 if (0 == strncmp(cp
, "man", 3)) {
663 } else if (0 == strncmp(cp
, "cat", 3)) {
671 if (NULL
!= dsec
|| use_all
)
675 say(path
, "Unknown directory part");
676 fts_set(f
, ff
, FTS_SKIP
);
680 * Possibly our architecture.
681 * If we're descending, keep tabs on it.
683 if (FTS_DP
!= ff
->fts_info
&& NULL
!= dsec
)
689 if (FTS_DP
== ff
->fts_info
|| use_all
)
692 say(path
, "Extraneous directory part");
693 fts_set(f
, ff
, FTS_SKIP
);
703 * Add a file to the mlinks table.
704 * Do not verify that it's a "valid" looking manpage (we'll do that
707 * Try to infer the manual section, architecture, and page name from the
708 * path, assuming it looks like
710 * [./]man*[/<arch>]/<name>.<section>
712 * [./]cat<section>[/<arch>]/<name>.0
714 * See treescan() for the fts(3) version of this.
717 filescan(const char *file
)
726 if (0 == strncmp(file
, "./", 2))
729 if (NULL
== realpath(file
, buf
)) {
730 exitcode
= (int)MANDOCLEVEL_BADARG
;
731 say(file
, "&realpath");
735 if (strstr(buf
, basedir
) == buf
)
736 start
= buf
+ strlen(basedir
) + 1;
737 else if (OP_TEST
== op
)
740 exitcode
= (int)MANDOCLEVEL_BADARG
;
741 say("", "%s: outside base directory", buf
);
745 if (-1 == stat(buf
, &st
)) {
746 exitcode
= (int)MANDOCLEVEL_BADARG
;
749 } else if ( ! (S_IFREG
& st
.st_mode
)) {
750 exitcode
= (int)MANDOCLEVEL_BADARG
;
751 say(file
, "Not a regular file");
755 mlink
= mandoc_calloc(1, sizeof(struct mlink
));
756 strlcpy(mlink
->file
, start
, sizeof(mlink
->file
));
759 * First try to guess our directory structure.
760 * If we find a separator, try to look for man* or cat*.
761 * If we find one of these and what's underneath is a directory,
762 * assume it's an architecture.
764 if (NULL
!= (p
= strchr(start
, '/'))) {
766 if (0 == strncmp(start
, "man", 3)) {
767 mlink
->dform
= FORM_SRC
;
768 mlink
->dsec
= start
+ 3;
769 } else if (0 == strncmp(start
, "cat", 3)) {
770 mlink
->dform
= FORM_CAT
;
771 mlink
->dsec
= start
+ 3;
775 if (NULL
!= mlink
->dsec
&& NULL
!= (p
= strchr(start
, '/'))) {
783 * Now check the file suffix.
784 * Suffix of `.0' indicates a catpage, `.1-9' is a manpage.
786 p
= strrchr(start
, '\0');
787 while (p
-- > start
&& '/' != *p
&& '.' != *p
)
796 * Now try to parse the name.
797 * Use the filename portion of the path.
800 if (NULL
!= (p
= strrchr(start
, '/'))) {
804 mlink_add(mlink
, &st
);
808 mlink_add(struct mlink
*mlink
, const struct stat
*st
)
810 struct inodev inodev
;
814 assert(NULL
!= mlink
->file
);
816 mlink
->dsec
= mandoc_strdup(mlink
->dsec
? mlink
->dsec
: "");
817 mlink
->arch
= mandoc_strdup(mlink
->arch
? mlink
->arch
: "");
818 mlink
->name
= mandoc_strdup(mlink
->name
? mlink
->name
: "");
819 mlink
->fsec
= mandoc_strdup(mlink
->fsec
? mlink
->fsec
: "");
821 if ('0' == *mlink
->fsec
) {
823 mlink
->fsec
= mandoc_strdup(mlink
->dsec
);
824 mlink
->fform
= FORM_CAT
;
825 } else if ('1' <= *mlink
->fsec
&& '9' >= *mlink
->fsec
)
826 mlink
->fform
= FORM_SRC
;
828 mlink
->fform
= FORM_NONE
;
830 slot
= ohash_qlookup(&mlinks
, mlink
->file
);
831 assert(NULL
== ohash_find(&mlinks
, slot
));
832 ohash_insert(&mlinks
, slot
, mlink
);
834 inodev
.st_ino
= st
->st_ino
;
835 inodev
.st_dev
= st
->st_dev
;
836 slot
= ohash_lookup_memory(&mpages
, (char *)&inodev
,
837 sizeof(struct inodev
), inodev
.st_ino
);
838 mpage
= ohash_find(&mpages
, slot
);
840 mpage
= mandoc_calloc(1, sizeof(struct mpage
));
841 mpage
->inodev
.st_ino
= inodev
.st_ino
;
842 mpage
->inodev
.st_dev
= inodev
.st_dev
;
843 ohash_insert(&mpages
, slot
, mpage
);
845 mlink
->next
= mpage
->mlinks
;
846 mpage
->mlinks
= mlink
;
847 mlink
->mpage
= mpage
;
851 mlink_free(struct mlink
*mlink
)
868 mpage
= ohash_first(&mpages
, &slot
);
869 while (NULL
!= mpage
) {
870 while (NULL
!= (mlink
= mpage
->mlinks
)) {
871 mpage
->mlinks
= mlink
->next
;
879 mpage
= ohash_next(&mpages
, &slot
);
884 * For each mlink to the mpage, check whether the path looks like
885 * it is formatted, and if it does, check whether a source manual
886 * exists by the same name, ignoring the suffix.
887 * If both conditions hold, drop the mlink.
890 mlinks_undupe(struct mpage
*mpage
)
897 mpage
->form
= FORM_CAT
;
898 prev
= &mpage
->mlinks
;
899 while (NULL
!= (mlink
= *prev
)) {
900 if (FORM_CAT
!= mlink
->dform
) {
901 mpage
->form
= FORM_NONE
;
904 if (strlcpy(buf
, mlink
->file
, PATH_MAX
) >= PATH_MAX
) {
906 say(mlink
->file
, "Filename too long");
909 bufp
= strstr(buf
, "cat");
910 assert(NULL
!= bufp
);
911 memcpy(bufp
, "man", 3);
912 if (NULL
!= (bufp
= strrchr(buf
, '.')))
914 strlcat(buf
, mlink
->dsec
, PATH_MAX
);
915 if (NULL
== ohash_find(&mlinks
,
916 ohash_qlookup(&mlinks
, buf
)))
919 say(mlink
->file
, "Man source exists: %s", buf
);
926 prev
= &(*prev
)->next
;
931 mlink_check(struct mpage
*mpage
, struct mlink
*mlink
)
937 * Check whether the manual section given in a file
938 * agrees with the directory where the file is located.
939 * Some manuals have suffixes like (3p) on their
940 * section number either inside the file or in the
941 * directory name, some are linked into more than one
942 * section, like encrypt(1) = makekey(8).
945 if (FORM_SRC
== mpage
->form
&&
946 strcasecmp(mpage
->sec
, mlink
->dsec
))
947 say(mlink
->file
, "Section \"%s\" manual in %s directory",
948 mpage
->sec
, mlink
->dsec
);
951 * Manual page directories exist for each kernel
952 * architecture as returned by machine(1).
953 * However, many manuals only depend on the
954 * application architecture as returned by arch(1).
955 * For example, some (2/ARM) manuals are shared
956 * across the "armish" and "zaurus" kernel
958 * A few manuals are even shared across completely
959 * different architectures, for example fdformat(1)
960 * on amd64, i386, sparc, and sparc64.
963 if (strcasecmp(mpage
->arch
, mlink
->arch
))
964 say(mlink
->file
, "Architecture \"%s\" manual in "
965 "\"%s\" directory", mpage
->arch
, mlink
->arch
);
969 * parse_cat() doesn't set NAME_TITLE yet.
972 if (FORM_CAT
== mpage
->form
)
976 * Check whether this mlink
977 * appears as a name in the NAME section.
980 slot
= ohash_qlookup(&names
, mlink
->name
);
981 str
= ohash_find(&names
, slot
);
983 if ( ! (NAME_TITLE
& str
->mask
))
984 say(mlink
->file
, "Name missing in NAME section");
988 * Run through the files in the global vector "mpages"
989 * and add them to the database specified in "basedir".
991 * This handles the parsing scheme itself, using the cues of directory
992 * and filename to determine whether the file is parsable or not.
995 mpages_merge(struct mchars
*mc
, struct mparse
*mp
)
998 struct ohash_info str_info
;
1000 struct mpage
*mpage
, *mpage_dest
;
1001 struct mlink
*mlink
, *mlink_dest
;
1009 enum mandoclevel lvl
;
1011 str_info
.alloc
= hash_alloc
;
1012 str_info
.halloc
= hash_halloc
;
1013 str_info
.hfree
= hash_free
;
1014 str_info
.key_offset
= offsetof(struct str
, key
);
1017 SQL_EXEC("BEGIN TRANSACTION");
1019 mpage
= ohash_first(&mpages
, &pslot
);
1020 while (NULL
!= mpage
) {
1021 mlinks_undupe(mpage
);
1022 if (NULL
== mpage
->mlinks
) {
1023 mpage
= ohash_next(&mpages
, &pslot
);
1027 name_mask
= NAME_MASK
;
1028 ohash_init(&names
, 4, &str_info
);
1029 ohash_init(&strings
, 6, &str_info
);
1038 if (mpage
->mlinks
->gzip
) {
1039 if (-1 == pipe(fd
)) {
1040 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1041 say(mpage
->mlinks
->file
, "&pipe gunzip");
1044 switch (child_pid
= fork()) {
1046 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1047 say(mpage
->mlinks
->file
, "&fork gunzip");
1054 if (-1 == dup2(fd
[1], STDOUT_FILENO
)) {
1055 say(mpage
->mlinks
->file
,
1059 execlp("gunzip", "gunzip", "-c",
1060 mpage
->mlinks
->file
, NULL
);
1061 say(mpage
->mlinks
->file
, "&exec gunzip");
1070 * Try interpreting the file as mdoc(7) or man(7)
1071 * source code, unless it is already known to be
1072 * formatted. Fall back to formatted mode.
1074 if (FORM_CAT
!= mpage
->mlinks
->dform
||
1075 FORM_CAT
!= mpage
->mlinks
->fform
) {
1076 lvl
= mparse_readfd(mp
, fd
[0], mpage
->mlinks
->file
);
1077 if (lvl
< MANDOCLEVEL_FATAL
)
1078 mparse_result(mp
, &mdoc
, &man
, &sodest
);
1081 if (NULL
!= sodest
) {
1082 mlink_dest
= ohash_find(&mlinks
,
1083 ohash_qlookup(&mlinks
, sodest
));
1084 if (NULL
!= mlink_dest
) {
1086 /* The .so target exists. */
1088 mpage_dest
= mlink_dest
->mpage
;
1089 mlink
= mpage
->mlinks
;
1091 mlink
->mpage
= mpage_dest
;
1094 * If the target was already
1095 * processed, add the links
1096 * to the database now.
1097 * Otherwise, this will
1098 * happen when we come
1102 if (mpage_dest
->recno
)
1105 if (NULL
== mlink
->next
)
1107 mlink
= mlink
->next
;
1110 /* Move all links to the target. */
1112 mlink
->next
= mlink_dest
->next
;
1113 mlink_dest
->next
= mpage
->mlinks
;
1114 mpage
->mlinks
= NULL
;
1117 } else if (NULL
!= mdoc
) {
1118 mpage
->form
= FORM_SRC
;
1120 mandoc_strdup(mdoc_meta(mdoc
)->msec
);
1121 mpage
->arch
= mdoc_meta(mdoc
)->arch
;
1122 mpage
->arch
= mandoc_strdup(
1123 NULL
== mpage
->arch
? "" : mpage
->arch
);
1125 mandoc_strdup(mdoc_meta(mdoc
)->title
);
1126 } else if (NULL
!= man
) {
1127 mpage
->form
= FORM_SRC
;
1129 mandoc_strdup(man_meta(man
)->msec
);
1131 mandoc_strdup(mpage
->mlinks
->arch
);
1133 mandoc_strdup(man_meta(man
)->title
);
1135 mpage
->form
= FORM_CAT
;
1137 mandoc_strdup(mpage
->mlinks
->dsec
);
1139 mandoc_strdup(mpage
->mlinks
->arch
);
1141 mandoc_strdup(mpage
->mlinks
->name
);
1143 putkey(mpage
, mpage
->sec
, TYPE_sec
);
1144 putkey(mpage
, '\0' == *mpage
->arch
?
1145 any
: mpage
->arch
, TYPE_arch
);
1147 for (mlink
= mpage
->mlinks
; mlink
; mlink
= mlink
->next
) {
1148 if ('\0' != *mlink
->dsec
)
1149 putkey(mpage
, mlink
->dsec
, TYPE_sec
);
1150 if ('\0' != *mlink
->fsec
)
1151 putkey(mpage
, mlink
->fsec
, TYPE_sec
);
1152 putkey(mpage
, '\0' == *mlink
->arch
?
1153 any
: mlink
->arch
, TYPE_arch
);
1154 putkey(mpage
, mlink
->name
, NAME_FILE
);
1158 if (NULL
!= (cp
= mdoc_meta(mdoc
)->name
))
1159 putkey(mpage
, cp
, NAME_HEAD
);
1160 assert(NULL
== mpage
->desc
);
1161 parse_mdoc(mpage
, mdoc_node(mdoc
));
1162 if (NULL
== mpage
->desc
)
1163 mpage
->desc
= mandoc_strdup(
1164 mpage
->mlinks
->name
);
1165 } else if (NULL
!= man
)
1166 parse_man(mpage
, man_node(man
));
1168 parse_cat(mpage
, fd
[0]);
1170 if (warnings
&& !use_all
)
1171 for (mlink
= mpage
->mlinks
; mlink
;
1172 mlink
= mlink
->next
)
1173 mlink_check(mpage
, mlink
);
1179 if (-1 == waitpid(child_pid
, &status
, 0)) {
1180 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1181 say(mpage
->mlinks
->file
, "&wait gunzip");
1182 } else if (WIFSIGNALED(status
)) {
1183 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1184 say(mpage
->mlinks
->file
,
1185 "gunzip died from signal %d",
1187 } else if (WEXITSTATUS(status
)) {
1188 exitcode
= (int)MANDOCLEVEL_SYSERR
;
1189 say(mpage
->mlinks
->file
,
1190 "gunzip failed with code %d",
1191 WEXITSTATUS(status
));
1194 ohash_delete(&strings
);
1195 ohash_delete(&names
);
1196 mpage
= ohash_next(&mpages
, &pslot
);
1200 SQL_EXEC("END TRANSACTION");
1207 const char *name
, *sec
, *arch
, *key
;
1211 sqlite3_prepare_v2(db
,
1212 "SELECT name, sec, arch, key FROM ("
1213 "SELECT name AS key, pageid FROM names "
1214 "WHERE bits & ? AND NOT EXISTS ("
1215 "SELECT pageid FROM mlinks "
1216 "WHERE mlinks.pageid == names.pageid "
1217 "AND mlinks.name == names.name"
1220 "SELECT * FROM mlinks GROUP BY pageid"
1221 ") USING (pageid);",
1225 SQL_BIND_INT64(stmt
, i
, NAME_TITLE
);
1227 while (SQLITE_ROW
== (irc
= sqlite3_step(stmt
))) {
1228 name
= sqlite3_column_text(stmt
, 0);
1229 sec
= sqlite3_column_text(stmt
, 1);
1230 arch
= sqlite3_column_text(stmt
, 2);
1231 key
= sqlite3_column_text(stmt
, 3);
1232 say("", "%s(%s%s%s) lacks mlink \"%s\"", name
, sec
,
1233 '\0' == *arch
? "" : "/",
1234 '\0' == *arch
? "" : arch
, key
);
1236 sqlite3_finalize(stmt
);
1240 parse_cat(struct mpage
*mpage
, int fd
)
1243 char *line
, *p
, *title
;
1244 size_t len
, plen
, titlesz
;
1246 stream
= (-1 == fd
) ?
1247 fopen(mpage
->mlinks
->file
, "r") :
1249 if (NULL
== stream
) {
1251 say(mpage
->mlinks
->file
, "&fopen");
1255 /* Skip to first blank line. */
1257 while (NULL
!= (line
= fgetln(stream
, &len
)))
1262 * Assume the first line that is not indented
1263 * is the first section header. Skip to it.
1266 while (NULL
!= (line
= fgetln(stream
, &len
)))
1267 if ('\n' != *line
&& ' ' != *line
)
1271 * Read up until the next section into a buffer.
1272 * Strip the leading and trailing newline from each read line,
1273 * appending a trailing space.
1274 * Ignore empty (whitespace-only) lines.
1280 while (NULL
!= (line
= fgetln(stream
, &len
))) {
1281 if (' ' != *line
|| '\n' != line
[len
- 1])
1283 while (len
> 0 && isspace((unsigned char)*line
)) {
1289 title
= mandoc_realloc(title
, titlesz
+ len
);
1290 memcpy(title
+ titlesz
, line
, len
);
1292 title
[titlesz
- 1] = ' ';
1296 * If no page content can be found, or the input line
1297 * is already the next section header, or there is no
1298 * trailing newline, reuse the page title as the page
1302 if (NULL
== title
|| '\0' == *title
) {
1304 say(mpage
->mlinks
->file
,
1305 "Cannot find NAME section");
1306 assert(NULL
== mpage
->desc
);
1307 mpage
->desc
= mandoc_strdup(mpage
->mlinks
->name
);
1313 title
= mandoc_realloc(title
, titlesz
+ 1);
1314 title
[titlesz
] = '\0';
1317 * Skip to the first dash.
1318 * Use the remaining line as the description (no more than 70
1322 if (NULL
!= (p
= strstr(title
, "- "))) {
1323 for (p
+= 2; ' ' == *p
|| '\b' == *p
; p
++)
1324 /* Skip to next word. */ ;
1327 say(mpage
->mlinks
->file
,
1328 "No dash in title line");
1334 /* Strip backspace-encoding from line. */
1336 while (NULL
!= (line
= memchr(p
, '\b', plen
))) {
1339 memmove(line
, line
+ 1, plen
--);
1342 memmove(line
- 1, line
+ 1, plen
- len
);
1346 assert(NULL
== mpage
->desc
);
1347 mpage
->desc
= mandoc_strdup(p
);
1353 * Put a type/word pair into the word database for this particular file.
1356 putkey(const struct mpage
*mpage
, char *value
, uint64_t type
)
1360 assert(NULL
!= value
);
1361 if (TYPE_arch
== type
)
1362 for (cp
= value
; *cp
; cp
++)
1363 if (isupper((unsigned char)*cp
))
1364 *cp
= _tolower((unsigned char)*cp
);
1365 putkeys(mpage
, value
, strlen(value
), type
);
1369 * Grok all nodes at or below a certain mdoc node into putkey().
1372 putmdockey(const struct mpage
*mpage
,
1373 const struct mdoc_node
*n
, uint64_t m
)
1376 for ( ; NULL
!= n
; n
= n
->next
) {
1377 if (NULL
!= n
->child
)
1378 putmdockey(mpage
, n
->child
, m
);
1379 if (MDOC_TEXT
== n
->type
)
1380 putkey(mpage
, n
->string
, m
);
1385 parse_man(struct mpage
*mpage
, const struct man_node
*n
)
1387 const struct man_node
*head
, *body
;
1388 char *start
, *title
;
1396 * We're only searching for one thing: the first text child in
1397 * the BODY of a NAME section. Since we don't keep track of
1398 * sections in -man, run some hoops to find out whether we're in
1399 * the correct section or not.
1402 if (MAN_BODY
== n
->type
&& MAN_SH
== n
->tok
) {
1404 assert(body
->parent
);
1405 if (NULL
!= (head
= body
->parent
->head
) &&
1406 1 == head
->nchild
&&
1407 NULL
!= (head
= (head
->child
)) &&
1408 MAN_TEXT
== head
->type
&&
1409 0 == strcmp(head
->string
, "NAME") &&
1410 NULL
!= body
->child
) {
1413 * Suck the entire NAME section into memory.
1414 * Yes, we might run away.
1415 * But too many manuals have big, spread-out
1416 * NAME sections over many lines.
1420 man_deroff(&title
, body
);
1425 * Go through a special heuristic dance here.
1426 * Conventionally, one or more manual names are
1427 * comma-specified prior to a whitespace, then a
1428 * dash, then a description. Try to puzzle out
1429 * the name parts here.
1434 sz
= strcspn(start
, " ,");
1435 if ('\0' == start
[sz
])
1442 * Assume a stray trailing comma in the
1443 * name list if a name begins with a dash.
1446 if ('-' == start
[0] ||
1447 ('\\' == start
[0] && '-' == start
[1]))
1450 putkey(mpage
, start
, NAME_TITLE
);
1457 assert(',' == byte
);
1459 while (' ' == *start
)
1463 if (start
== title
) {
1464 putkey(mpage
, start
, NAME_TITLE
);
1469 while (isspace((unsigned char)*start
))
1472 if (0 == strncmp(start
, "-", 1))
1474 else if (0 == strncmp(start
, "\\-\\-", 4))
1476 else if (0 == strncmp(start
, "\\-", 2))
1478 else if (0 == strncmp(start
, "\\(en", 4))
1480 else if (0 == strncmp(start
, "\\(em", 4))
1483 while (' ' == *start
)
1486 assert(NULL
== mpage
->desc
);
1487 mpage
->desc
= mandoc_strdup(start
);
1493 for (n
= n
->child
; n
; n
= n
->next
) {
1494 if (NULL
!= mpage
->desc
)
1496 parse_man(mpage
, n
);
1501 parse_mdoc(struct mpage
*mpage
, const struct mdoc_node
*n
)
1505 for (n
= n
->child
; NULL
!= n
; n
= n
->next
) {
1516 if (NULL
!= mdocs
[n
->tok
].fp
)
1517 if (0 == (*mdocs
[n
->tok
].fp
)(mpage
, n
))
1519 if (mdocs
[n
->tok
].mask
)
1520 putmdockey(mpage
, n
->child
,
1521 mdocs
[n
->tok
].mask
);
1524 assert(MDOC_ROOT
!= n
->type
);
1527 if (NULL
!= n
->child
)
1528 parse_mdoc(mpage
, n
);
1533 parse_mdoc_Fd(struct mpage
*mpage
, const struct mdoc_node
*n
)
1535 const char *start
, *end
;
1538 if (SEC_SYNOPSIS
!= n
->sec
||
1539 NULL
== (n
= n
->child
) ||
1540 MDOC_TEXT
!= n
->type
)
1544 * Only consider those `Fd' macro fields that begin with an
1545 * "inclusion" token (versus, e.g., #define).
1548 if (strcmp("#include", n
->string
))
1551 if (NULL
== (n
= n
->next
) || MDOC_TEXT
!= n
->type
)
1555 * Strip away the enclosing angle brackets and make sure we're
1560 if ('<' == *start
|| '"' == *start
)
1563 if (0 == (sz
= strlen(start
)))
1566 end
= &start
[(int)sz
- 1];
1567 if ('>' == *end
|| '"' == *end
)
1571 putkeys(mpage
, start
, end
- start
+ 1, TYPE_In
);
1576 parse_mdoc_Fn(struct mpage
*mpage
, const struct mdoc_node
*n
)
1580 if (NULL
== (n
= n
->child
) || MDOC_TEXT
!= n
->type
)
1584 * Parse: .Fn "struct type *name" "char *arg".
1585 * First strip away pointer symbol.
1586 * Then store the function name, then type.
1587 * Finally, store the arguments.
1590 if (NULL
== (cp
= strrchr(n
->string
, ' ')))
1596 putkey(mpage
, cp
, TYPE_Fn
);
1599 putkeys(mpage
, n
->string
, cp
- n
->string
, TYPE_Ft
);
1601 for (n
= n
->next
; NULL
!= n
; n
= n
->next
)
1602 if (MDOC_TEXT
== n
->type
)
1603 putkey(mpage
, n
->string
, TYPE_Fa
);
1609 parse_mdoc_Xr(struct mpage
*mpage
, const struct mdoc_node
*n
)
1613 if (NULL
== (n
= n
->child
))
1616 if (NULL
== n
->next
) {
1617 putkey(mpage
, n
->string
, TYPE_Xr
);
1621 mandoc_asprintf(&cp
, "%s(%s)", n
->string
, n
->next
->string
);
1622 putkey(mpage
, cp
, TYPE_Xr
);
1628 parse_mdoc_Nd(struct mpage
*mpage
, const struct mdoc_node
*n
)
1631 if (MDOC_BODY
== n
->type
)
1632 mdoc_deroff(&mpage
->desc
, n
);
1637 parse_mdoc_Nm(struct mpage
*mpage
, const struct mdoc_node
*n
)
1640 if (SEC_NAME
== n
->sec
)
1641 putmdockey(mpage
, n
->child
, NAME_TITLE
);
1642 else if (SEC_SYNOPSIS
== n
->sec
&& MDOC_HEAD
== n
->type
)
1643 putmdockey(mpage
, n
->child
, NAME_SYN
);
1648 parse_mdoc_Sh(struct mpage
*mpage
, const struct mdoc_node
*n
)
1651 return(SEC_CUSTOM
== n
->sec
&& MDOC_HEAD
== n
->type
);
1655 parse_mdoc_head(struct mpage
*mpage
, const struct mdoc_node
*n
)
1658 return(MDOC_HEAD
== n
->type
);
1662 parse_mdoc_body(struct mpage
*mpage
, const struct mdoc_node
*n
)
1665 return(MDOC_BODY
== n
->type
);
1669 * Add a string to the hash table for the current manual.
1670 * Each string has a bitmask telling which macros it belongs to.
1671 * When we finish the manual, we'll dump the table.
1674 putkeys(const struct mpage
*mpage
,
1675 const char *cp
, size_t sz
, uint64_t v
)
1689 name_mask
&= ~NAME_FIRST
;
1691 say(mpage
->mlinks
->file
,
1692 "Adding name %*s", sz
, cp
);
1696 for (i
= 0; i
< mansearch_keymax
; i
++)
1698 say(mpage
->mlinks
->file
,
1699 "Adding key %s=%*s",
1700 mansearch_keynames
[i
], sz
, cp
);
1704 slot
= ohash_qlookupi(htab
, cp
, &end
);
1705 s
= ohash_find(htab
, slot
);
1707 if (NULL
!= s
&& mpage
== s
->mpage
) {
1710 } else if (NULL
== s
) {
1711 s
= mandoc_calloc(sizeof(struct str
) + sz
+ 1, 1);
1712 memcpy(s
->key
, cp
, sz
);
1713 ohash_insert(htab
, slot
, s
);
1720 * Take a Unicode codepoint and produce its UTF-8 encoding.
1721 * This isn't the best way to do this, but it works.
1722 * The magic numbers are from the UTF-8 packaging.
1723 * They're not as scary as they seem: read the UTF-8 spec for details.
1726 utf8(unsigned int cp
, char out
[7])
1731 if (cp
<= 0x0000007F) {
1734 } else if (cp
<= 0x000007FF) {
1736 out
[0] = (cp
>> 6 & 31) | 192;
1737 out
[1] = (cp
& 63) | 128;
1738 } else if (cp
<= 0x0000FFFF) {
1740 out
[0] = (cp
>> 12 & 15) | 224;
1741 out
[1] = (cp
>> 6 & 63) | 128;
1742 out
[2] = (cp
& 63) | 128;
1743 } else if (cp
<= 0x001FFFFF) {
1745 out
[0] = (cp
>> 18 & 7) | 240;
1746 out
[1] = (cp
>> 12 & 63) | 128;
1747 out
[2] = (cp
>> 6 & 63) | 128;
1748 out
[3] = (cp
& 63) | 128;
1749 } else if (cp
<= 0x03FFFFFF) {
1751 out
[0] = (cp
>> 24 & 3) | 248;
1752 out
[1] = (cp
>> 18 & 63) | 128;
1753 out
[2] = (cp
>> 12 & 63) | 128;
1754 out
[3] = (cp
>> 6 & 63) | 128;
1755 out
[4] = (cp
& 63) | 128;
1756 } else if (cp
<= 0x7FFFFFFF) {
1758 out
[0] = (cp
>> 30 & 1) | 252;
1759 out
[1] = (cp
>> 24 & 63) | 128;
1760 out
[2] = (cp
>> 18 & 63) | 128;
1761 out
[3] = (cp
>> 12 & 63) | 128;
1762 out
[4] = (cp
>> 6 & 63) | 128;
1763 out
[5] = (cp
& 63) | 128;
1772 * Store the rendered version of a key, or alias the pointer
1773 * if the key contains no escape sequences.
1776 render_key(struct mchars
*mc
, struct str
*key
)
1778 size_t sz
, bsz
, pos
;
1779 char utfbuf
[7], res
[6];
1781 const char *seq
, *cpp
, *val
;
1783 enum mandoc_esc esc
;
1785 assert(NULL
== key
->rendered
);
1789 res
[2] = ASCII_NBRSP
;
1790 res
[3] = ASCII_HYPH
;
1791 res
[4] = ASCII_BREAK
;
1798 * Pre-check: if we have no stop-characters, then set the
1799 * pointer as ourselvse and get out of here.
1801 if (strcspn(val
, res
) == bsz
) {
1802 key
->rendered
= key
->key
;
1806 /* Pre-allocate by the length of the input */
1808 buf
= mandoc_malloc(++bsz
);
1811 while ('\0' != *val
) {
1813 * Halt on the first escape sequence.
1814 * This also halts on the end of string, in which case
1815 * we just copy, fallthrough, and exit the loop.
1817 if ((sz
= strcspn(val
, res
)) > 0) {
1818 memcpy(&buf
[pos
], val
, sz
);
1842 /* Read past the slash. */
1847 * Parse the escape sequence and see if it's a
1848 * predefined character or special character.
1852 ((const char **)&val
, &seq
, &len
);
1853 if (ESCAPE_ERROR
== esc
)
1855 if (ESCAPE_SPECIAL
!= esc
)
1859 * Render the special character
1860 * as either UTF-8 or ASCII.
1864 if (0 == (u
= mchars_spec2cp(mc
, seq
, len
)))
1867 if (0 == (sz
= utf8(u
, utfbuf
)))
1871 cpp
= mchars_spec2str(mc
, seq
, len
, &sz
);
1874 if (ASCII_NBRSP
== *cpp
) {
1880 /* Copy the rendered glyph into the stream. */
1883 buf
= mandoc_realloc(buf
, bsz
);
1884 memcpy(&buf
[pos
], cpp
, sz
);
1889 key
->rendered
= buf
;
1893 dbadd_mlink(const struct mlink
*mlink
)
1898 SQL_BIND_TEXT(stmts
[STMT_INSERT_LINK
], i
, mlink
->dsec
);
1899 SQL_BIND_TEXT(stmts
[STMT_INSERT_LINK
], i
, mlink
->arch
);
1900 SQL_BIND_TEXT(stmts
[STMT_INSERT_LINK
], i
, mlink
->name
);
1901 SQL_BIND_INT64(stmts
[STMT_INSERT_LINK
], i
, mlink
->mpage
->recno
);
1902 SQL_STEP(stmts
[STMT_INSERT_LINK
]);
1903 sqlite3_reset(stmts
[STMT_INSERT_LINK
]);
1907 * Flush the current page's terms (and their bits) into the database.
1908 * Wrap the entire set of additions in a transaction to make sqlite be a
1910 * Also, handle escape sequences at the last possible moment.
1913 dbadd(struct mpage
*mpage
, struct mchars
*mc
)
1915 struct mlink
*mlink
;
1920 mlink
= mpage
->mlinks
;
1923 while (NULL
!= mlink
) {
1924 fputs(mlink
->name
, stdout
);
1925 if (NULL
== mlink
->next
||
1926 strcmp(mlink
->dsec
, mlink
->next
->dsec
) ||
1927 strcmp(mlink
->fsec
, mlink
->next
->fsec
) ||
1928 strcmp(mlink
->arch
, mlink
->next
->arch
)) {
1930 if ('\0' == *mlink
->dsec
)
1931 fputs(mlink
->fsec
, stdout
);
1933 fputs(mlink
->dsec
, stdout
);
1934 if ('\0' != *mlink
->arch
)
1935 printf("/%s", mlink
->arch
);
1938 mlink
= mlink
->next
;
1940 fputs(", ", stdout
);
1942 printf(" - %s\n", mpage
->desc
);
1947 say(mlink
->file
, "Adding to database");
1950 SQL_BIND_TEXT(stmts
[STMT_INSERT_PAGE
], i
, mpage
->desc
);
1951 SQL_BIND_INT(stmts
[STMT_INSERT_PAGE
], i
, FORM_SRC
== mpage
->form
);
1952 SQL_STEP(stmts
[STMT_INSERT_PAGE
]);
1953 mpage
->recno
= sqlite3_last_insert_rowid(db
);
1954 sqlite3_reset(stmts
[STMT_INSERT_PAGE
]);
1956 while (NULL
!= mlink
) {
1958 mlink
= mlink
->next
;
1961 for (key
= ohash_first(&names
, &slot
); NULL
!= key
;
1962 key
= ohash_next(&names
, &slot
)) {
1963 assert(key
->mpage
== mpage
);
1964 if (NULL
== key
->rendered
)
1965 render_key(mc
, key
);
1967 SQL_BIND_INT64(stmts
[STMT_INSERT_NAME
], i
, key
->mask
);
1968 SQL_BIND_TEXT(stmts
[STMT_INSERT_NAME
], i
, key
->rendered
);
1969 SQL_BIND_INT64(stmts
[STMT_INSERT_NAME
], i
, mpage
->recno
);
1970 SQL_STEP(stmts
[STMT_INSERT_NAME
]);
1971 sqlite3_reset(stmts
[STMT_INSERT_NAME
]);
1972 if (key
->rendered
!= key
->key
)
1973 free(key
->rendered
);
1976 for (key
= ohash_first(&strings
, &slot
); NULL
!= key
;
1977 key
= ohash_next(&strings
, &slot
)) {
1978 assert(key
->mpage
== mpage
);
1979 if (NULL
== key
->rendered
)
1980 render_key(mc
, key
);
1982 SQL_BIND_INT64(stmts
[STMT_INSERT_KEY
], i
, key
->mask
);
1983 SQL_BIND_TEXT(stmts
[STMT_INSERT_KEY
], i
, key
->rendered
);
1984 SQL_BIND_INT64(stmts
[STMT_INSERT_KEY
], i
, mpage
->recno
);
1985 SQL_STEP(stmts
[STMT_INSERT_KEY
]);
1986 sqlite3_reset(stmts
[STMT_INSERT_KEY
]);
1987 if (key
->rendered
!= key
->key
)
1988 free(key
->rendered
);
1996 struct mpage
*mpage
;
1997 struct mlink
*mlink
;
2002 SQL_EXEC("BEGIN TRANSACTION");
2004 for (mpage
= ohash_first(&mpages
, &slot
); NULL
!= mpage
;
2005 mpage
= ohash_next(&mpages
, &slot
)) {
2006 mlink
= mpage
->mlinks
;
2008 say(mlink
->file
, "Deleting from database");
2011 for ( ; NULL
!= mlink
; mlink
= mlink
->next
) {
2013 SQL_BIND_TEXT(stmts
[STMT_DELETE_PAGE
],
2015 SQL_BIND_TEXT(stmts
[STMT_DELETE_PAGE
],
2017 SQL_BIND_TEXT(stmts
[STMT_DELETE_PAGE
],
2019 SQL_STEP(stmts
[STMT_DELETE_PAGE
]);
2020 sqlite3_reset(stmts
[STMT_DELETE_PAGE
]);
2025 SQL_EXEC("END TRANSACTION");
2029 * Close an existing database and its prepared statements.
2030 * If "real" is not set, rename the temporary file into the real one.
2042 for (i
= 0; i
< STMT__MAX
; i
++) {
2043 sqlite3_finalize(stmts
[i
]);
2053 if ('\0' == *tempfilename
) {
2054 if (-1 == rename(MANDOC_DB
"~", MANDOC_DB
)) {
2055 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2056 say(MANDOC_DB
, "&rename");
2061 switch (child
= fork()) {
2063 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2064 say("", "&fork cmp");
2067 execlp("cmp", "cmp", "-s",
2068 tempfilename
, MANDOC_DB
, NULL
);
2069 say("", "&exec cmp");
2074 if (-1 == waitpid(child
, &status
, 0)) {
2075 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2076 say("", "&wait cmp");
2077 } else if (WIFSIGNALED(status
)) {
2078 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2079 say("", "cmp died from signal %d", WTERMSIG(status
));
2080 } else if (WEXITSTATUS(status
)) {
2081 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2083 "Data changed, but cannot replace database");
2086 *strrchr(tempfilename
, '/') = '\0';
2087 switch (child
= fork()) {
2089 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2090 say("", "&fork rm");
2093 execlp("rm", "rm", "-rf", tempfilename
, NULL
);
2094 say("", "&exec rm");
2095 exit((int)MANDOCLEVEL_SYSERR
);
2099 if (-1 == waitpid(child
, &status
, 0)) {
2100 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2101 say("", "&wait rm");
2102 } else if (WIFSIGNALED(status
) || WEXITSTATUS(status
)) {
2103 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2104 say("", "%s: Cannot remove temporary directory",
2110 * This is straightforward stuff.
2111 * Open a database connection to a "temporary" database, then open a set
2112 * of prepared statements we'll use over and over again.
2113 * If "real" is set, we use the existing database; if not, we truncate a
2115 * Must be matched by dbclose().
2126 *tempfilename
= '\0';
2127 ofl
= SQLITE_OPEN_READWRITE
;
2130 rc
= sqlite3_open_v2(MANDOC_DB
, &db
, ofl
, NULL
);
2131 if (SQLITE_OK
!= rc
) {
2132 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2133 say(MANDOC_DB
, "%s", sqlite3_errmsg(db
));
2136 goto prepare_statements
;
2139 ofl
|= SQLITE_OPEN_CREATE
| SQLITE_OPEN_EXCLUSIVE
;
2141 remove(MANDOC_DB
"~");
2142 rc
= sqlite3_open_v2(MANDOC_DB
"~", &db
, ofl
, NULL
);
2143 if (SQLITE_OK
== rc
)
2145 if (MPARSE_QUICK
& mparse_options
) {
2146 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2147 say(MANDOC_DB
"~", "%s", sqlite3_errmsg(db
));
2151 if (strlcpy(tempfilename
, "/tmp/mandocdb.XXXXXX",
2152 sizeof(tempfilename
)) >= sizeof(tempfilename
)) {
2153 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2154 say("", "/tmp/mandocdb.XXXXXX: Filename too long");
2157 if (NULL
== mkdtemp(tempfilename
)) {
2158 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2159 say("", "&%s", tempfilename
);
2162 if (strlcat(tempfilename
, "/" MANDOC_DB
,
2163 sizeof(tempfilename
)) >= sizeof(tempfilename
)) {
2164 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2165 say("", "%s/" MANDOC_DB
": Filename too long",
2169 rc
= sqlite3_open_v2(tempfilename
, &db
, ofl
, NULL
);
2170 if (SQLITE_OK
!= rc
) {
2171 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2172 say("", "%s: %s", tempfilename
, sqlite3_errmsg(db
));
2177 sql
= "CREATE TABLE \"mpages\" (\n"
2178 " \"desc\" TEXT NOT NULL,\n"
2179 " \"form\" INTEGER NOT NULL,\n"
2180 " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
2183 "CREATE TABLE \"mlinks\" (\n"
2184 " \"sec\" TEXT NOT NULL,\n"
2185 " \"arch\" TEXT NOT NULL,\n"
2186 " \"name\" TEXT NOT NULL,\n"
2187 " \"pageid\" INTEGER NOT NULL REFERENCES mpages(id) "
2188 "ON DELETE CASCADE\n"
2191 "CREATE TABLE \"names\" (\n"
2192 " \"bits\" INTEGER NOT NULL,\n"
2193 " \"name\" TEXT NOT NULL,\n"
2194 " \"pageid\" INTEGER NOT NULL REFERENCES mpages(id) "
2195 "ON DELETE CASCADE\n"
2198 "CREATE TABLE \"keys\" (\n"
2199 " \"bits\" INTEGER NOT NULL,\n"
2200 " \"key\" TEXT NOT NULL,\n"
2201 " \"pageid\" INTEGER NOT NULL REFERENCES mpages(id) "
2202 "ON DELETE CASCADE\n"
2205 if (SQLITE_OK
!= sqlite3_exec(db
, sql
, NULL
, NULL
, NULL
)) {
2206 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2207 say(MANDOC_DB
, "%s", sqlite3_errmsg(db
));
2212 SQL_EXEC("PRAGMA foreign_keys = ON");
2213 sql
= "DELETE FROM mpages WHERE id IN "
2214 "(SELECT pageid FROM mlinks WHERE "
2215 "sec=? AND arch=? AND name=?)";
2216 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_DELETE_PAGE
], NULL
);
2217 sql
= "INSERT INTO mpages "
2218 "(desc,form) VALUES (?,?)";
2219 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_PAGE
], NULL
);
2220 sql
= "INSERT INTO mlinks "
2221 "(sec,arch,name,pageid) VALUES (?,?,?,?)";
2222 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_LINK
], NULL
);
2223 sql
= "INSERT INTO names "
2224 "(bits,name,pageid) VALUES (?,?,?)";
2225 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_NAME
], NULL
);
2226 sql
= "INSERT INTO keys "
2227 "(bits,key,pageid) VALUES (?,?,?)";
2228 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_KEY
], NULL
);
2232 * When opening a new database, we can turn off
2233 * synchronous mode for much better performance.
2237 SQL_EXEC("PRAGMA synchronous = OFF");
2244 hash_halloc(size_t sz
, void *arg
)
2247 return(mandoc_calloc(sz
, 1));
2251 hash_alloc(size_t sz
, void *arg
)
2254 return(mandoc_malloc(sz
));
2258 hash_free(void *p
, size_t sz
, void *arg
)
2265 set_basedir(const char *targetdir
)
2267 static char startdir
[PATH_MAX
];
2271 * Remember where we started by keeping a fd open to the origin
2272 * path component: throughout this utility, we chdir() a lot to
2273 * handle relative paths, and by doing this, we can return to
2274 * the starting point.
2276 if ('\0' == *startdir
) {
2277 if (NULL
== getcwd(startdir
, PATH_MAX
)) {
2278 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2279 if (NULL
!= targetdir
)
2283 if (-1 == (fd
= open(startdir
, O_RDONLY
, 0))) {
2284 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2285 say("", "&open %s", startdir
);
2288 if (NULL
== targetdir
)
2289 targetdir
= startdir
;
2293 if (-1 == fchdir(fd
)) {
2296 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2297 say("", "&chdir %s", startdir
);
2300 if (NULL
== targetdir
) {
2305 if (NULL
== realpath(targetdir
, basedir
)) {
2307 exitcode
= (int)MANDOCLEVEL_BADARG
;
2308 say("", "&%s: realpath", targetdir
);
2310 } else if (-1 == chdir(basedir
)) {
2311 exitcode
= (int)MANDOCLEVEL_BADARG
;
2319 say(const char *file
, const char *format
, ...)
2324 if ('\0' != *basedir
)
2325 fprintf(stderr
, "%s", basedir
);
2326 if ('\0' != *basedir
&& '\0' != *file
)
2327 fputs("//", stderr
);
2329 fprintf(stderr
, "%s", file
);
2332 if (NULL
!= format
) {
2345 if (NULL
!= format
) {
2346 if ('\0' != *basedir
|| '\0' != *file
)
2347 fputs(": ", stderr
);
2348 va_start(ap
, format
);
2349 vfprintf(stderr
, format
, ap
);
2353 if ('\0' != *basedir
|| '\0' != *file
|| NULL
!= format
)
2354 fputs(": ", stderr
);
2357 fputc('\n', stderr
);