Annotation of src/usr.bin/mandoc/mandocdb.c, Revision 1.73
1.73 ! schwarze 1: /* $Id: mandocdb.c,v 1.72 2014/03/18 16:56:06 schwarze Exp $ */
1.1 schwarze 2: /*
1.47 schwarze 3: * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.52 schwarze 4: * Copyright (c) 2011, 2012, 2013, 2014 Ingo Schwarze <schwarze@openbsd.org>
1.1 schwarze 5: *
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.
9: *
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.
17: */
1.47 schwarze 18: #include <sys/stat.h>
1.72 schwarze 19: #include <sys/wait.h>
1.1 schwarze 20:
21: #include <assert.h>
1.33 schwarze 22: #include <ctype.h>
1.34 schwarze 23: #include <errno.h>
1.1 schwarze 24: #include <fcntl.h>
1.47 schwarze 25: #include <fts.h>
1.1 schwarze 26: #include <getopt.h>
1.44 schwarze 27: #include <limits.h>
1.47 schwarze 28: #include <stddef.h>
1.1 schwarze 29: #include <stdio.h>
30: #include <stdint.h>
31: #include <stdlib.h>
32: #include <string.h>
1.14 schwarze 33: #include <unistd.h>
1.1 schwarze 34:
1.47 schwarze 35: #include <ohash.h>
36: #include <sqlite3.h>
37:
38: #include "mdoc.h"
1.1 schwarze 39: #include "man.h"
40: #include "mandoc.h"
1.10 schwarze 41: #include "manpath.h"
1.47 schwarze 42: #include "mansearch.h"
1.1 schwarze 43:
1.68 schwarze 44: extern int mansearch_keymax;
45: extern const char *const mansearch_keynames[];
46:
1.47 schwarze 47: #define SQL_EXEC(_v) \
48: if (SQLITE_OK != sqlite3_exec(db, (_v), NULL, NULL, NULL)) \
49: fprintf(stderr, "%s\n", sqlite3_errmsg(db))
50: #define SQL_BIND_TEXT(_s, _i, _v) \
51: if (SQLITE_OK != sqlite3_bind_text \
52: ((_s), (_i)++, (_v), -1, SQLITE_STATIC)) \
53: fprintf(stderr, "%s\n", sqlite3_errmsg(db))
54: #define SQL_BIND_INT(_s, _i, _v) \
55: if (SQLITE_OK != sqlite3_bind_int \
56: ((_s), (_i)++, (_v))) \
57: fprintf(stderr, "%s\n", sqlite3_errmsg(db))
58: #define SQL_BIND_INT64(_s, _i, _v) \
59: if (SQLITE_OK != sqlite3_bind_int64 \
60: ((_s), (_i)++, (_v))) \
61: fprintf(stderr, "%s\n", sqlite3_errmsg(db))
62: #define SQL_STEP(_s) \
63: if (SQLITE_DONE != sqlite3_step((_s))) \
64: fprintf(stderr, "%s\n", sqlite3_errmsg(db))
1.1 schwarze 65:
1.47 schwarze 66: enum op {
67: OP_DEFAULT = 0, /* new dbs from dir list or default config */
68: OP_CONFFILE, /* new databases from custom config file */
69: OP_UPDATE, /* delete/add entries in existing database */
70: OP_DELETE, /* delete entries from existing database */
71: OP_TEST /* change no databases, report potential problems */
72: };
1.11 schwarze 73:
1.47 schwarze 74: enum form {
75: FORM_NONE, /* format is unknown */
76: FORM_SRC, /* format is -man or -mdoc */
77: FORM_CAT /* format is cat */
78: };
1.28 schwarze 79:
1.47 schwarze 80: struct str {
1.53 schwarze 81: char *rendered; /* key in UTF-8 or ASCII form */
1.47 schwarze 82: const struct mpage *mpage; /* if set, the owning parse */
83: uint64_t mask; /* bitmask in sequence */
1.53 schwarze 84: char key[]; /* may contain escape sequences */
1.28 schwarze 85: };
86:
1.47 schwarze 87: struct inodev {
88: ino_t st_ino;
89: dev_t st_dev;
90: };
1.28 schwarze 91:
1.47 schwarze 92: struct mpage {
93: struct inodev inodev; /* used for hashing routine */
94: enum form form; /* format from file content */
95: char *sec; /* section from file content */
96: char *arch; /* architecture from file content */
97: char *title; /* title from file content */
98: char *desc; /* description from file content */
99: struct mlink *mlinks; /* singly linked list */
1.28 schwarze 100: };
101:
1.47 schwarze 102: struct mlink {
103: char file[PATH_MAX]; /* filename rel. to manpath */
104: enum form dform; /* format from directory */
105: enum form fform; /* format from file name suffix */
106: char *dsec; /* section from directory */
107: char *arch; /* architecture from directory */
108: char *name; /* name from file name (not empty) */
109: char *fsec; /* section from file name suffix */
110: struct mlink *next; /* singly linked list */
1.2 schwarze 111: };
112:
1.47 schwarze 113: enum stmt {
114: STMT_DELETE_PAGE = 0, /* delete mpage */
115: STMT_INSERT_PAGE, /* insert mpage */
116: STMT_INSERT_LINK, /* insert mlink */
117: STMT_INSERT_KEY, /* insert parsed key */
118: STMT__MAX
1.1 schwarze 119: };
120:
1.47 schwarze 121: typedef int (*mdoc_fp)(struct mpage *, const struct mdoc_node *);
1.1 schwarze 122:
1.19 schwarze 123: struct mdoc_handler {
1.47 schwarze 124: mdoc_fp fp; /* optional handler */
125: uint64_t mask; /* set unless handler returns 0 */
1.19 schwarze 126: };
127:
1.47 schwarze 128: static void dbclose(int);
1.62 schwarze 129: static void dbadd(const struct mpage *, struct mchars *);
1.47 schwarze 130: static int dbopen(int);
131: static void dbprune(void);
132: static void filescan(const char *);
133: static void *hash_alloc(size_t, void *);
134: static void hash_free(void *, size_t, void *);
135: static void *hash_halloc(size_t, void *);
136: static void mlink_add(struct mlink *, const struct stat *);
1.50 schwarze 137: static int mlink_check(struct mpage *, struct mlink *);
1.47 schwarze 138: static void mlink_free(struct mlink *);
139: static void mlinks_undupe(struct mpage *);
140: static void mpages_free(void);
1.58 schwarze 141: static void mpages_merge(struct mchars *, struct mparse *);
1.47 schwarze 142: static void parse_cat(struct mpage *);
143: static void parse_man(struct mpage *, const struct man_node *);
144: static void parse_mdoc(struct mpage *, const struct mdoc_node *);
145: static int parse_mdoc_body(struct mpage *, const struct mdoc_node *);
146: static int parse_mdoc_head(struct mpage *, const struct mdoc_node *);
147: static int parse_mdoc_Fd(struct mpage *, const struct mdoc_node *);
148: static int parse_mdoc_Fn(struct mpage *, const struct mdoc_node *);
149: static int parse_mdoc_Nd(struct mpage *, const struct mdoc_node *);
150: static int parse_mdoc_Nm(struct mpage *, const struct mdoc_node *);
151: static int parse_mdoc_Sh(struct mpage *, const struct mdoc_node *);
152: static int parse_mdoc_Xr(struct mpage *, const struct mdoc_node *);
1.69 schwarze 153: static void putkey(const struct mpage *, char *, uint64_t);
1.47 schwarze 154: static void putkeys(const struct mpage *,
155: const char *, size_t, uint64_t);
156: static void putmdockey(const struct mpage *,
157: const struct mdoc_node *, uint64_t);
1.53 schwarze 158: static void render_key(struct mchars *, struct str *);
1.47 schwarze 159: static void say(const char *, const char *, ...);
160: static int set_basedir(const char *);
161: static int treescan(void);
162: static size_t utf8(unsigned int, char [7]);
163:
1.72 schwarze 164: static char tempfilename[32];
1.47 schwarze 165: static char *progname;
1.59 schwarze 166: static int nodb; /* no database changes */
1.73 ! schwarze 167: static int mparse_options; /* abort the parse early */
1.47 schwarze 168: static int use_all; /* use all found files */
169: static int verb; /* print what we're doing */
170: static int warnings; /* warn about crap */
1.52 schwarze 171: static int write_utf8; /* write UTF-8 output; else ASCII */
1.47 schwarze 172: static int exitcode; /* to be returned by main */
173: static enum op op; /* operational mode */
174: static char basedir[PATH_MAX]; /* current base directory */
175: static struct ohash mpages; /* table of distinct manual pages */
176: static struct ohash mlinks; /* table of directory entries */
177: static struct ohash strings; /* table of all strings */
178: static sqlite3 *db = NULL; /* current database */
179: static sqlite3_stmt *stmts[STMT__MAX]; /* current statements */
180:
1.19 schwarze 181: static const struct mdoc_handler mdocs[MDOC_MAX] = {
1.47 schwarze 182: { NULL, 0 }, /* Ap */
183: { NULL, 0 }, /* Dd */
184: { NULL, 0 }, /* Dt */
185: { NULL, 0 }, /* Os */
186: { parse_mdoc_Sh, TYPE_Sh }, /* Sh */
187: { parse_mdoc_head, TYPE_Ss }, /* Ss */
188: { NULL, 0 }, /* Pp */
189: { NULL, 0 }, /* D1 */
190: { NULL, 0 }, /* Dl */
191: { NULL, 0 }, /* Bd */
192: { NULL, 0 }, /* Ed */
193: { NULL, 0 }, /* Bl */
194: { NULL, 0 }, /* El */
195: { NULL, 0 }, /* It */
196: { NULL, 0 }, /* Ad */
197: { NULL, TYPE_An }, /* An */
198: { NULL, TYPE_Ar }, /* Ar */
199: { NULL, TYPE_Cd }, /* Cd */
200: { NULL, TYPE_Cm }, /* Cm */
201: { NULL, TYPE_Dv }, /* Dv */
202: { NULL, TYPE_Er }, /* Er */
203: { NULL, TYPE_Ev }, /* Ev */
204: { NULL, 0 }, /* Ex */
205: { NULL, TYPE_Fa }, /* Fa */
206: { parse_mdoc_Fd, 0 }, /* Fd */
207: { NULL, TYPE_Fl }, /* Fl */
208: { parse_mdoc_Fn, 0 }, /* Fn */
209: { NULL, TYPE_Ft }, /* Ft */
210: { NULL, TYPE_Ic }, /* Ic */
1.49 schwarze 211: { NULL, TYPE_In }, /* In */
1.47 schwarze 212: { NULL, TYPE_Li }, /* Li */
213: { parse_mdoc_Nd, TYPE_Nd }, /* Nd */
214: { parse_mdoc_Nm, TYPE_Nm }, /* Nm */
215: { NULL, 0 }, /* Op */
216: { NULL, 0 }, /* Ot */
217: { NULL, TYPE_Pa }, /* Pa */
218: { NULL, 0 }, /* Rv */
1.49 schwarze 219: { NULL, TYPE_St }, /* St */
1.47 schwarze 220: { NULL, TYPE_Va }, /* Va */
221: { parse_mdoc_body, TYPE_Va }, /* Vt */
222: { parse_mdoc_Xr, 0 }, /* Xr */
223: { NULL, 0 }, /* %A */
224: { NULL, 0 }, /* %B */
225: { NULL, 0 }, /* %D */
226: { NULL, 0 }, /* %I */
227: { NULL, 0 }, /* %J */
228: { NULL, 0 }, /* %N */
229: { NULL, 0 }, /* %O */
230: { NULL, 0 }, /* %P */
231: { NULL, 0 }, /* %R */
232: { NULL, 0 }, /* %T */
233: { NULL, 0 }, /* %V */
234: { NULL, 0 }, /* Ac */
235: { NULL, 0 }, /* Ao */
236: { NULL, 0 }, /* Aq */
237: { NULL, TYPE_At }, /* At */
238: { NULL, 0 }, /* Bc */
239: { NULL, 0 }, /* Bf */
240: { NULL, 0 }, /* Bo */
241: { NULL, 0 }, /* Bq */
242: { NULL, TYPE_Bsx }, /* Bsx */
243: { NULL, TYPE_Bx }, /* Bx */
244: { NULL, 0 }, /* Db */
245: { NULL, 0 }, /* Dc */
246: { NULL, 0 }, /* Do */
247: { NULL, 0 }, /* Dq */
248: { NULL, 0 }, /* Ec */
249: { NULL, 0 }, /* Ef */
250: { NULL, TYPE_Em }, /* Em */
251: { NULL, 0 }, /* Eo */
252: { NULL, TYPE_Fx }, /* Fx */
253: { NULL, TYPE_Ms }, /* Ms */
254: { NULL, 0 }, /* No */
255: { NULL, 0 }, /* Ns */
256: { NULL, TYPE_Nx }, /* Nx */
257: { NULL, TYPE_Ox }, /* Ox */
258: { NULL, 0 }, /* Pc */
259: { NULL, 0 }, /* Pf */
260: { NULL, 0 }, /* Po */
261: { NULL, 0 }, /* Pq */
262: { NULL, 0 }, /* Qc */
263: { NULL, 0 }, /* Ql */
264: { NULL, 0 }, /* Qo */
265: { NULL, 0 }, /* Qq */
266: { NULL, 0 }, /* Re */
267: { NULL, 0 }, /* Rs */
268: { NULL, 0 }, /* Sc */
269: { NULL, 0 }, /* So */
270: { NULL, 0 }, /* Sq */
271: { NULL, 0 }, /* Sm */
272: { NULL, 0 }, /* Sx */
273: { NULL, TYPE_Sy }, /* Sy */
274: { NULL, TYPE_Tn }, /* Tn */
275: { NULL, 0 }, /* Ux */
276: { NULL, 0 }, /* Xc */
277: { NULL, 0 }, /* Xo */
278: { parse_mdoc_head, 0 }, /* Fo */
279: { NULL, 0 }, /* Fc */
280: { NULL, 0 }, /* Oo */
281: { NULL, 0 }, /* Oc */
282: { NULL, 0 }, /* Bk */
283: { NULL, 0 }, /* Ek */
284: { NULL, 0 }, /* Bt */
285: { NULL, 0 }, /* Hf */
286: { NULL, 0 }, /* Fr */
287: { NULL, 0 }, /* Ud */
288: { NULL, TYPE_Lb }, /* Lb */
289: { NULL, 0 }, /* Lp */
290: { NULL, TYPE_Lk }, /* Lk */
291: { NULL, TYPE_Mt }, /* Mt */
292: { NULL, 0 }, /* Brq */
293: { NULL, 0 }, /* Bro */
294: { NULL, 0 }, /* Brc */
295: { NULL, 0 }, /* %C */
296: { NULL, 0 }, /* Es */
297: { NULL, 0 }, /* En */
298: { NULL, TYPE_Dx }, /* Dx */
299: { NULL, 0 }, /* %Q */
300: { NULL, 0 }, /* br */
301: { NULL, 0 }, /* sp */
302: { NULL, 0 }, /* %U */
303: { NULL, 0 }, /* Ta */
1.1 schwarze 304: };
305:
306: int
1.3 schwarze 307: mandocdb(int argc, char *argv[])
1.1 schwarze 308: {
1.47 schwarze 309: int ch, i;
310: size_t j, sz;
311: const char *path_arg;
312: struct mchars *mc;
313: struct manpaths dirs;
314: struct mparse *mp;
315: struct ohash_info mpages_info, mlinks_info;
316:
317: memset(stmts, 0, STMT__MAX * sizeof(sqlite3_stmt *));
318: memset(&dirs, 0, sizeof(struct manpaths));
319:
320: mpages_info.alloc = mlinks_info.alloc = hash_alloc;
321: mpages_info.halloc = mlinks_info.halloc = hash_halloc;
322: mpages_info.hfree = mlinks_info.hfree = hash_free;
323:
324: mpages_info.key_offset = offsetof(struct mpage, inodev);
325: mlinks_info.key_offset = offsetof(struct mlink, file);
1.1 schwarze 326:
327: progname = strrchr(argv[0], '/');
328: if (progname == NULL)
329: progname = argv[0];
330: else
331: ++progname;
332:
1.47 schwarze 333: /*
334: * We accept a few different invocations.
335: * The CHECKOP macro makes sure that invocation styles don't
336: * clobber each other.
337: */
338: #define CHECKOP(_op, _ch) do \
339: if (OP_DEFAULT != (_op)) { \
340: fprintf(stderr, "-%c: Conflicting option\n", (_ch)); \
341: goto usage; \
342: } while (/*CONSTCOND*/0)
1.10 schwarze 343:
1.47 schwarze 344: path_arg = NULL;
1.28 schwarze 345: op = OP_DEFAULT;
1.73 ! schwarze 346: mparse_options = MPARSE_SO;
1.1 schwarze 347:
1.59 schwarze 348: while (-1 != (ch = getopt(argc, argv, "aC:d:nQT:tu:vW")))
1.1 schwarze 349: switch (ch) {
1.6 schwarze 350: case ('a'):
351: use_all = 1;
352: break;
1.25 schwarze 353: case ('C'):
1.47 schwarze 354: CHECKOP(op, ch);
355: path_arg = optarg;
1.28 schwarze 356: op = OP_CONFFILE;
1.25 schwarze 357: break;
1.1 schwarze 358: case ('d'):
1.47 schwarze 359: CHECKOP(op, ch);
360: path_arg = optarg;
1.2 schwarze 361: op = OP_UPDATE;
1.1 schwarze 362: break;
1.47 schwarze 363: case ('n'):
364: nodb = 1;
365: break;
1.59 schwarze 366: case ('Q'):
1.73 ! schwarze 367: mparse_options |= MPARSE_QUICK;
1.59 schwarze 368: break;
1.52 schwarze 369: case ('T'):
370: if (strcmp(optarg, "utf8")) {
371: fprintf(stderr, "-T%s: Unsupported "
372: "output format\n", optarg);
373: goto usage;
374: }
375: write_utf8 = 1;
376: break;
1.28 schwarze 377: case ('t'):
1.47 schwarze 378: CHECKOP(op, ch);
1.28 schwarze 379: dup2(STDOUT_FILENO, STDERR_FILENO);
380: op = OP_TEST;
1.47 schwarze 381: nodb = warnings = 1;
1.28 schwarze 382: break;
1.2 schwarze 383: case ('u'):
1.47 schwarze 384: CHECKOP(op, ch);
385: path_arg = optarg;
1.1 schwarze 386: op = OP_DELETE;
387: break;
388: case ('v'):
389: verb++;
390: break;
1.28 schwarze 391: case ('W'):
392: warnings = 1;
393: break;
1.1 schwarze 394: default:
1.28 schwarze 395: goto usage;
1.1 schwarze 396: }
397:
398: argc -= optind;
399: argv += optind;
400:
1.28 schwarze 401: if (OP_CONFFILE == op && argc > 0) {
1.47 schwarze 402: fprintf(stderr, "-C: Too many arguments\n");
1.28 schwarze 403: goto usage;
404: }
405:
1.47 schwarze 406: exitcode = (int)MANDOCLEVEL_OK;
1.73 ! schwarze 407: mp = mparse_alloc(mparse_options, MANDOCLEVEL_FATAL, NULL, NULL);
1.47 schwarze 408: mc = mchars_alloc();
1.2 schwarze 409:
1.47 schwarze 410: ohash_init(&mpages, 6, &mpages_info);
411: ohash_init(&mlinks, 6, &mlinks_info);
1.2 schwarze 412:
1.47 schwarze 413: if (OP_UPDATE == op || OP_DELETE == op || OP_TEST == op) {
414: /*
415: * Force processing all files.
416: */
417: use_all = 1;
1.2 schwarze 418:
1.47 schwarze 419: /*
420: * All of these deal with a specific directory.
421: * Jump into that directory then collect files specified
422: * on the command-line.
423: */
424: if (0 == set_basedir(path_arg))
425: goto out;
426: for (i = 0; i < argc; i++)
427: filescan(argv[i]);
428: if (0 == dbopen(1))
429: goto out;
430: if (OP_TEST != op)
431: dbprune();
432: if (OP_DELETE != op)
1.58 schwarze 433: mpages_merge(mc, mp);
1.47 schwarze 434: dbclose(1);
435: } else {
436: /*
437: * If we have arguments, use them as our manpaths.
438: * If we don't, grok from manpath(1) or however else
439: * manpath_parse() wants to do it.
440: */
441: if (argc > 0) {
442: dirs.paths = mandoc_calloc
443: (argc, sizeof(char *));
444: dirs.sz = (size_t)argc;
445: for (i = 0; i < argc; i++)
446: dirs.paths[i] = mandoc_strdup(argv[i]);
447: } else
448: manpath_parse(&dirs, path_arg, NULL, NULL);
1.2 schwarze 449:
1.47 schwarze 450: /*
451: * First scan the tree rooted at a base directory, then
452: * build a new database and finally move it into place.
453: * Ignore zero-length directories and strip trailing
454: * slashes.
455: */
456: for (j = 0; j < dirs.sz; j++) {
457: sz = strlen(dirs.paths[j]);
458: if (sz && '/' == dirs.paths[j][sz - 1])
459: dirs.paths[j][--sz] = '\0';
460: if (0 == sz)
461: continue;
1.2 schwarze 462:
1.47 schwarze 463: if (j) {
464: ohash_init(&mpages, 6, &mpages_info);
465: ohash_init(&mlinks, 6, &mlinks_info);
466: }
1.1 schwarze 467:
1.47 schwarze 468: if (0 == set_basedir(dirs.paths[j]))
469: goto out;
470: if (0 == treescan())
471: goto out;
472: if (0 == set_basedir(dirs.paths[j]))
473: goto out;
474: if (0 == dbopen(0))
475: goto out;
1.41 deraadt 476:
1.58 schwarze 477: mpages_merge(mc, mp);
1.47 schwarze 478: dbclose(0);
1.28 schwarze 479:
1.47 schwarze 480: if (j + 1 < dirs.sz) {
481: mpages_free();
482: ohash_delete(&mpages);
483: ohash_delete(&mlinks);
484: }
1.2 schwarze 485: }
1.47 schwarze 486: }
487: out:
488: set_basedir(NULL);
489: manpath_free(&dirs);
490: mchars_free(mc);
491: mparse_free(mp);
492: mpages_free();
493: ohash_delete(&mpages);
494: ohash_delete(&mlinks);
495: return(exitcode);
496: usage:
1.59 schwarze 497: fprintf(stderr, "usage: %s [-anQvW] [-C file] [-Tutf8]\n"
498: " %s [-anQvW] [-Tutf8] dir ...\n"
499: " %s [-nQvW] [-Tutf8] -d dir [file ...]\n"
1.47 schwarze 500: " %s [-nvW] -u dir [file ...]\n"
1.59 schwarze 501: " %s [-Q] -t file ...\n",
1.47 schwarze 502: progname, progname, progname,
503: progname, progname);
1.1 schwarze 504:
1.47 schwarze 505: return((int)MANDOCLEVEL_BADARG);
506: }
1.1 schwarze 507:
1.47 schwarze 508: /*
509: * Scan a directory tree rooted at "basedir" for manpages.
510: * We use fts(), scanning directory parts along the way for clues to our
511: * section and architecture.
512: *
513: * If use_all has been specified, grok all files.
514: * If not, sanitise paths to the following:
515: *
516: * [./]man*[/<arch>]/<name>.<section>
517: * or
518: * [./]cat<section>[/<arch>]/<name>.0
519: *
520: * TODO: accomodate for multi-language directories.
521: */
522: static int
523: treescan(void)
524: {
525: FTS *f;
526: FTSENT *ff;
527: struct mlink *mlink;
528: int dform;
1.51 schwarze 529: char *dsec, *arch, *fsec, *cp;
530: const char *path;
1.47 schwarze 531: const char *argv[2];
1.1 schwarze 532:
1.47 schwarze 533: argv[0] = ".";
534: argv[1] = (char *)NULL;
1.1 schwarze 535:
1.47 schwarze 536: /*
537: * Walk through all components under the directory, using the
538: * logical descent of files.
539: */
540: f = fts_open((char * const *)argv, FTS_LOGICAL, NULL);
541: if (NULL == f) {
542: exitcode = (int)MANDOCLEVEL_SYSERR;
543: say("", NULL);
544: return(0);
545: }
1.2 schwarze 546:
1.47 schwarze 547: dsec = arch = NULL;
548: dform = FORM_NONE;
1.2 schwarze 549:
1.47 schwarze 550: while (NULL != (ff = fts_read(f))) {
551: path = ff->fts_path + 2;
1.15 schwarze 552: /*
1.47 schwarze 553: * If we're a regular file, add an mlink by using the
554: * stored directory data and handling the filename.
1.15 schwarze 555: */
1.47 schwarze 556: if (FTS_F == ff->fts_info) {
557: if (0 == strcmp(path, MANDOC_DB))
558: continue;
559: if ( ! use_all && ff->fts_level < 2) {
560: if (warnings)
561: say(path, "Extraneous file");
562: continue;
563: } else if (NULL == (fsec =
564: strrchr(ff->fts_name, '.'))) {
565: if ( ! use_all) {
566: if (warnings)
567: say(path,
568: "No filename suffix");
569: continue;
570: }
571: } else if (0 == strcmp(++fsec, "html")) {
572: if (warnings)
573: say(path, "Skip html");
574: continue;
575: } else if (0 == strcmp(fsec, "gz")) {
576: if (warnings)
577: say(path, "Skip gz");
578: continue;
579: } else if (0 == strcmp(fsec, "ps")) {
580: if (warnings)
581: say(path, "Skip ps");
582: continue;
583: } else if (0 == strcmp(fsec, "pdf")) {
584: if (warnings)
585: say(path, "Skip pdf");
586: continue;
587: } else if ( ! use_all &&
588: ((FORM_SRC == dform && strcmp(fsec, dsec)) ||
589: (FORM_CAT == dform && strcmp(fsec, "0")))) {
590: if (warnings)
591: say(path, "Wrong filename suffix");
592: continue;
593: } else
594: fsec[-1] = '\0';
1.51 schwarze 595:
1.47 schwarze 596: mlink = mandoc_calloc(1, sizeof(struct mlink));
597: strlcpy(mlink->file, path, sizeof(mlink->file));
598: mlink->dform = dform;
1.51 schwarze 599: mlink->dsec = dsec;
600: mlink->arch = arch;
601: mlink->name = ff->fts_name;
602: mlink->fsec = fsec;
1.47 schwarze 603: mlink_add(mlink, ff->fts_statp);
604: continue;
605: } else if (FTS_D != ff->fts_info &&
606: FTS_DP != ff->fts_info) {
607: if (warnings)
608: say(path, "Not a regular file");
609: continue;
610: }
611:
612: switch (ff->fts_level) {
613: case (0):
614: /* Ignore the root directory. */
615: break;
616: case (1):
617: /*
618: * This might contain manX/ or catX/.
619: * Try to infer this from the name.
620: * If we're not in use_all, enforce it.
621: */
622: cp = ff->fts_name;
623: if (FTS_DP == ff->fts_info)
624: break;
1.15 schwarze 625:
1.47 schwarze 626: if (0 == strncmp(cp, "man", 3)) {
627: dform = FORM_SRC;
628: dsec = cp + 3;
629: } else if (0 == strncmp(cp, "cat", 3)) {
630: dform = FORM_CAT;
631: dsec = cp + 3;
1.51 schwarze 632: } else {
633: dform = FORM_NONE;
634: dsec = NULL;
1.26 schwarze 635: }
1.47 schwarze 636:
637: if (NULL != dsec || use_all)
638: break;
639:
640: if (warnings)
641: say(path, "Unknown directory part");
642: fts_set(f, ff, FTS_SKIP);
643: break;
644: case (2):
645: /*
646: * Possibly our architecture.
647: * If we're descending, keep tabs on it.
648: */
649: if (FTS_DP != ff->fts_info && NULL != dsec)
650: arch = ff->fts_name;
1.51 schwarze 651: else
652: arch = NULL;
1.47 schwarze 653: break;
654: default:
655: if (FTS_DP == ff->fts_info || use_all)
656: break;
657: if (warnings)
658: say(path, "Extraneous directory part");
659: fts_set(f, ff, FTS_SKIP);
660: break;
1.14 schwarze 661: }
1.47 schwarze 662: }
663:
664: fts_close(f);
665: return(1);
666: }
1.1 schwarze 667:
1.47 schwarze 668: /*
669: * Add a file to the mlinks table.
670: * Do not verify that it's a "valid" looking manpage (we'll do that
671: * later).
672: *
673: * Try to infer the manual section, architecture, and page name from the
674: * path, assuming it looks like
675: *
676: * [./]man*[/<arch>]/<name>.<section>
677: * or
678: * [./]cat<section>[/<arch>]/<name>.0
679: *
680: * See treescan() for the fts(3) version of this.
681: */
682: static void
683: filescan(const char *file)
684: {
685: char buf[PATH_MAX];
686: struct stat st;
687: struct mlink *mlink;
688: char *p, *start;
689:
690: assert(use_all);
691:
692: if (0 == strncmp(file, "./", 2))
693: file += 2;
694:
695: if (NULL == realpath(file, buf)) {
696: exitcode = (int)MANDOCLEVEL_BADARG;
697: say(file, NULL);
698: return;
1.63 schwarze 699: }
700:
701: if (strstr(buf, basedir) == buf)
702: start = buf + strlen(basedir) + 1;
703: else if (OP_TEST == op)
704: start = buf;
705: else {
1.47 schwarze 706: exitcode = (int)MANDOCLEVEL_BADARG;
707: say("", "%s: outside base directory", buf);
708: return;
1.63 schwarze 709: }
710:
711: if (-1 == stat(buf, &st)) {
1.47 schwarze 712: exitcode = (int)MANDOCLEVEL_BADARG;
713: say(file, NULL);
714: return;
715: } else if ( ! (S_IFREG & st.st_mode)) {
716: exitcode = (int)MANDOCLEVEL_BADARG;
717: say(file, "Not a regular file");
718: return;
1.1 schwarze 719: }
1.63 schwarze 720:
1.47 schwarze 721: mlink = mandoc_calloc(1, sizeof(struct mlink));
722: strlcpy(mlink->file, start, sizeof(mlink->file));
1.1 schwarze 723:
1.10 schwarze 724: /*
1.47 schwarze 725: * First try to guess our directory structure.
726: * If we find a separator, try to look for man* or cat*.
727: * If we find one of these and what's underneath is a directory,
728: * assume it's an architecture.
1.10 schwarze 729: */
1.47 schwarze 730: if (NULL != (p = strchr(start, '/'))) {
731: *p++ = '\0';
732: if (0 == strncmp(start, "man", 3)) {
733: mlink->dform = FORM_SRC;
1.51 schwarze 734: mlink->dsec = start + 3;
1.47 schwarze 735: } else if (0 == strncmp(start, "cat", 3)) {
736: mlink->dform = FORM_CAT;
1.51 schwarze 737: mlink->dsec = start + 3;
1.47 schwarze 738: }
1.10 schwarze 739:
1.47 schwarze 740: start = p;
741: if (NULL != mlink->dsec && NULL != (p = strchr(start, '/'))) {
742: *p++ = '\0';
1.51 schwarze 743: mlink->arch = start;
1.47 schwarze 744: start = p;
1.41 deraadt 745: }
1.47 schwarze 746: }
1.7 schwarze 747:
1.47 schwarze 748: /*
749: * Now check the file suffix.
750: * Suffix of `.0' indicates a catpage, `.1-9' is a manpage.
751: */
752: p = strrchr(start, '\0');
753: while (p-- > start && '/' != *p && '.' != *p)
754: /* Loop. */ ;
755:
756: if ('.' == *p) {
757: *p++ = '\0';
1.51 schwarze 758: mlink->fsec = p;
1.47 schwarze 759: }
1.41 deraadt 760:
1.47 schwarze 761: /*
762: * Now try to parse the name.
763: * Use the filename portion of the path.
764: */
765: mlink->name = start;
766: if (NULL != (p = strrchr(start, '/'))) {
767: mlink->name = p + 1;
768: *p = '\0';
769: }
770: mlink_add(mlink, &st);
771: }
1.2 schwarze 772:
1.47 schwarze 773: static void
774: mlink_add(struct mlink *mlink, const struct stat *st)
775: {
776: struct inodev inodev;
777: struct mpage *mpage;
778: unsigned int slot;
779:
780: assert(NULL != mlink->file);
781:
1.51 schwarze 782: mlink->dsec = mandoc_strdup(mlink->dsec ? mlink->dsec : "");
783: mlink->arch = mandoc_strdup(mlink->arch ? mlink->arch : "");
784: mlink->name = mandoc_strdup(mlink->name ? mlink->name : "");
785: mlink->fsec = mandoc_strdup(mlink->fsec ? mlink->fsec : "");
1.47 schwarze 786:
787: if ('0' == *mlink->fsec) {
788: free(mlink->fsec);
789: mlink->fsec = mandoc_strdup(mlink->dsec);
790: mlink->fform = FORM_CAT;
791: } else if ('1' <= *mlink->fsec && '9' >= *mlink->fsec)
792: mlink->fform = FORM_SRC;
793: else
794: mlink->fform = FORM_NONE;
1.1 schwarze 795:
1.47 schwarze 796: slot = ohash_qlookup(&mlinks, mlink->file);
797: assert(NULL == ohash_find(&mlinks, slot));
798: ohash_insert(&mlinks, slot, mlink);
799:
800: inodev.st_ino = st->st_ino;
801: inodev.st_dev = st->st_dev;
802: slot = ohash_lookup_memory(&mpages, (char *)&inodev,
803: sizeof(struct inodev), inodev.st_ino);
804: mpage = ohash_find(&mpages, slot);
805: if (NULL == mpage) {
806: mpage = mandoc_calloc(1, sizeof(struct mpage));
807: mpage->inodev.st_ino = inodev.st_ino;
808: mpage->inodev.st_dev = inodev.st_dev;
809: ohash_insert(&mpages, slot, mpage);
810: } else
811: mlink->next = mpage->mlinks;
812: mpage->mlinks = mlink;
813: }
1.1 schwarze 814:
1.47 schwarze 815: static void
816: mlink_free(struct mlink *mlink)
817: {
1.1 schwarze 818:
1.47 schwarze 819: free(mlink->dsec);
820: free(mlink->arch);
821: free(mlink->name);
822: free(mlink->fsec);
823: free(mlink);
824: }
1.1 schwarze 825:
1.47 schwarze 826: static void
827: mpages_free(void)
828: {
829: struct mpage *mpage;
830: struct mlink *mlink;
831: unsigned int slot;
832:
833: mpage = ohash_first(&mpages, &slot);
834: while (NULL != mpage) {
835: while (NULL != (mlink = mpage->mlinks)) {
836: mpage->mlinks = mlink->next;
837: mlink_free(mlink);
838: }
839: free(mpage->sec);
840: free(mpage->arch);
841: free(mpage->title);
842: free(mpage->desc);
843: free(mpage);
844: mpage = ohash_next(&mpages, &slot);
845: }
846: }
1.1 schwarze 847:
1.47 schwarze 848: /*
849: * For each mlink to the mpage, check whether the path looks like
850: * it is formatted, and if it does, check whether a source manual
851: * exists by the same name, ignoring the suffix.
852: * If both conditions hold, drop the mlink.
853: */
854: static void
855: mlinks_undupe(struct mpage *mpage)
856: {
857: char buf[PATH_MAX];
858: struct mlink **prev;
859: struct mlink *mlink;
860: char *bufp;
861:
862: mpage->form = FORM_CAT;
863: prev = &mpage->mlinks;
864: while (NULL != (mlink = *prev)) {
865: if (FORM_CAT != mlink->dform) {
866: mpage->form = FORM_NONE;
867: goto nextlink;
868: }
869: if (strlcpy(buf, mlink->file, PATH_MAX) >= PATH_MAX) {
870: if (warnings)
871: say(mlink->file, "Filename too long");
872: goto nextlink;
1.26 schwarze 873: }
1.47 schwarze 874: bufp = strstr(buf, "cat");
875: assert(NULL != bufp);
876: memcpy(bufp, "man", 3);
877: if (NULL != (bufp = strrchr(buf, '.')))
878: *++bufp = '\0';
879: strlcat(buf, mlink->dsec, PATH_MAX);
880: if (NULL == ohash_find(&mlinks,
881: ohash_qlookup(&mlinks, buf)))
882: goto nextlink;
883: if (warnings)
884: say(mlink->file, "Man source exists: %s", buf);
885: if (use_all)
886: goto nextlink;
887: *prev = mlink->next;
888: mlink_free(mlink);
889: continue;
890: nextlink:
891: prev = &(*prev)->next;
1.1 schwarze 892: }
1.47 schwarze 893: }
1.1 schwarze 894:
1.50 schwarze 895: static int
896: mlink_check(struct mpage *mpage, struct mlink *mlink)
897: {
898: int match;
899:
900: match = 1;
901:
902: /*
903: * Check whether the manual section given in a file
904: * agrees with the directory where the file is located.
905: * Some manuals have suffixes like (3p) on their
906: * section number either inside the file or in the
907: * directory name, some are linked into more than one
908: * section, like encrypt(1) = makekey(8).
909: */
910:
911: if (FORM_SRC == mpage->form &&
912: strcasecmp(mpage->sec, mlink->dsec)) {
913: match = 0;
914: say(mlink->file, "Section \"%s\" manual in %s directory",
915: mpage->sec, mlink->dsec);
916: }
917:
918: /*
919: * Manual page directories exist for each kernel
920: * architecture as returned by machine(1).
921: * However, many manuals only depend on the
922: * application architecture as returned by arch(1).
923: * For example, some (2/ARM) manuals are shared
924: * across the "armish" and "zaurus" kernel
925: * architectures.
926: * A few manuals are even shared across completely
927: * different architectures, for example fdformat(1)
928: * on amd64, i386, sparc, and sparc64.
929: */
930:
931: if (strcasecmp(mpage->arch, mlink->arch)) {
932: match = 0;
933: say(mlink->file, "Architecture \"%s\" manual in "
934: "\"%s\" directory", mpage->arch, mlink->arch);
935: }
936:
937: if (strcasecmp(mpage->title, mlink->name))
938: match = 0;
939:
940: return(match);
941: }
942:
1.47 schwarze 943: /*
944: * Run through the files in the global vector "mpages"
945: * and add them to the database specified in "basedir".
946: *
947: * This handles the parsing scheme itself, using the cues of directory
948: * and filename to determine whether the file is parsable or not.
949: */
950: static void
1.58 schwarze 951: mpages_merge(struct mchars *mc, struct mparse *mp)
1.47 schwarze 952: {
1.70 schwarze 953: char any[] = "any";
1.58 schwarze 954: struct ohash_info str_info;
1.47 schwarze 955: struct mpage *mpage;
1.50 schwarze 956: struct mlink *mlink;
1.47 schwarze 957: struct mdoc *mdoc;
958: struct man *man;
1.69 schwarze 959: char *cp;
1.47 schwarze 960: int match;
1.58 schwarze 961: unsigned int pslot;
1.47 schwarze 962: enum mandoclevel lvl;
963:
964: str_info.alloc = hash_alloc;
965: str_info.halloc = hash_halloc;
966: str_info.hfree = hash_free;
967: str_info.key_offset = offsetof(struct str, key);
968:
1.64 schwarze 969: if (0 == nodb)
970: SQL_EXEC("BEGIN TRANSACTION");
971:
1.47 schwarze 972: mpage = ohash_first(&mpages, &pslot);
973: while (NULL != mpage) {
974: mlinks_undupe(mpage);
975: if (NULL == mpage->mlinks) {
976: mpage = ohash_next(&mpages, &pslot);
977: continue;
978: }
1.1 schwarze 979:
1.47 schwarze 980: ohash_init(&strings, 6, &str_info);
981: mparse_reset(mp);
982: mdoc = NULL;
983: man = NULL;
1.11 schwarze 984:
985: /*
1.24 schwarze 986: * Try interpreting the file as mdoc(7) or man(7)
987: * source code, unless it is already known to be
988: * formatted. Fall back to formatted mode.
1.11 schwarze 989: */
1.47 schwarze 990: if (FORM_CAT != mpage->mlinks->dform ||
991: FORM_CAT != mpage->mlinks->fform) {
992: lvl = mparse_readfd(mp, -1, mpage->mlinks->file);
993: if (lvl < MANDOCLEVEL_FATAL)
994: mparse_result(mp, &mdoc, &man);
995: }
1.11 schwarze 996:
997: if (NULL != mdoc) {
1.47 schwarze 998: mpage->form = FORM_SRC;
999: mpage->sec =
1000: mandoc_strdup(mdoc_meta(mdoc)->msec);
1001: mpage->arch = mdoc_meta(mdoc)->arch;
1002: mpage->arch = mandoc_strdup(
1003: NULL == mpage->arch ? "" : mpage->arch);
1004: mpage->title =
1005: mandoc_strdup(mdoc_meta(mdoc)->title);
1.11 schwarze 1006: } else if (NULL != man) {
1.47 schwarze 1007: mpage->form = FORM_SRC;
1008: mpage->sec =
1009: mandoc_strdup(man_meta(man)->msec);
1010: mpage->arch =
1011: mandoc_strdup(mpage->mlinks->arch);
1012: mpage->title =
1013: mandoc_strdup(man_meta(man)->title);
1.11 schwarze 1014: } else {
1.47 schwarze 1015: mpage->form = FORM_CAT;
1016: mpage->sec =
1017: mandoc_strdup(mpage->mlinks->dsec);
1018: mpage->arch =
1019: mandoc_strdup(mpage->mlinks->arch);
1020: mpage->title =
1021: mandoc_strdup(mpage->mlinks->name);
1.1 schwarze 1022: }
1.54 schwarze 1023: putkey(mpage, mpage->sec, TYPE_sec);
1.55 schwarze 1024: putkey(mpage, '\0' == *mpage->arch ?
1.70 schwarze 1025: any : mpage->arch, TYPE_arch);
1.1 schwarze 1026:
1.54 schwarze 1027: for (mlink = mpage->mlinks; mlink; mlink = mlink->next) {
1028: if ('\0' != *mlink->dsec)
1029: putkey(mpage, mlink->dsec, TYPE_sec);
1030: if ('\0' != *mlink->fsec)
1031: putkey(mpage, mlink->fsec, TYPE_sec);
1.55 schwarze 1032: putkey(mpage, '\0' == *mlink->arch ?
1.70 schwarze 1033: any : mlink->arch, TYPE_arch);
1.50 schwarze 1034: putkey(mpage, mlink->name, TYPE_Nm);
1.54 schwarze 1035: }
1.41 deraadt 1036:
1.50 schwarze 1037: if (warnings && !use_all) {
1.47 schwarze 1038: match = 0;
1.50 schwarze 1039: for (mlink = mpage->mlinks; mlink;
1040: mlink = mlink->next)
1041: if (mlink_check(mpage, mlink))
1042: match = 1;
1043: } else
1044: match = 1;
1.6 schwarze 1045:
1.47 schwarze 1046: if (NULL != mdoc) {
1047: if (NULL != (cp = mdoc_meta(mdoc)->name))
1048: putkey(mpage, cp, TYPE_Nm);
1049: assert(NULL == mpage->desc);
1050: parse_mdoc(mpage, mdoc_node(mdoc));
1051: putkey(mpage, NULL != mpage->desc ?
1052: mpage->desc : mpage->mlinks->name, TYPE_Nd);
1053: } else if (NULL != man)
1054: parse_man(mpage, man_node(man));
1055: else
1056: parse_cat(mpage);
1.6 schwarze 1057:
1.62 schwarze 1058: dbadd(mpage, mc);
1.47 schwarze 1059: ohash_delete(&strings);
1060: mpage = ohash_next(&mpages, &pslot);
1061: }
1.64 schwarze 1062:
1063: if (0 == nodb)
1064: SQL_EXEC("END TRANSACTION");
1.47 schwarze 1065: }
1.6 schwarze 1066:
1.47 schwarze 1067: static void
1068: parse_cat(struct mpage *mpage)
1069: {
1070: FILE *stream;
1071: char *line, *p, *title;
1072: size_t len, plen, titlesz;
1.1 schwarze 1073:
1.47 schwarze 1074: if (NULL == (stream = fopen(mpage->mlinks->file, "r"))) {
1075: if (warnings)
1076: say(mpage->mlinks->file, NULL);
1077: return;
1078: }
1.1 schwarze 1079:
1.47 schwarze 1080: /* Skip to first blank line. */
1.1 schwarze 1081:
1.47 schwarze 1082: while (NULL != (line = fgetln(stream, &len)))
1083: if ('\n' == *line)
1084: break;
1.1 schwarze 1085:
1.47 schwarze 1086: /*
1087: * Assume the first line that is not indented
1088: * is the first section header. Skip to it.
1089: */
1.1 schwarze 1090:
1.47 schwarze 1091: while (NULL != (line = fgetln(stream, &len)))
1092: if ('\n' != *line && ' ' != *line)
1093: break;
1094:
1095: /*
1096: * Read up until the next section into a buffer.
1097: * Strip the leading and trailing newline from each read line,
1098: * appending a trailing space.
1099: * Ignore empty (whitespace-only) lines.
1100: */
1.28 schwarze 1101:
1.47 schwarze 1102: titlesz = 0;
1103: title = NULL;
1.38 schwarze 1104:
1.47 schwarze 1105: while (NULL != (line = fgetln(stream, &len))) {
1106: if (' ' != *line || '\n' != line[len - 1])
1107: break;
1108: while (len > 0 && isspace((unsigned char)*line)) {
1109: line++;
1110: len--;
1111: }
1112: if (1 == len)
1113: continue;
1114: title = mandoc_realloc(title, titlesz + len);
1115: memcpy(title + titlesz, line, len);
1116: titlesz += len;
1117: title[titlesz - 1] = ' ';
1118: }
1.28 schwarze 1119:
1.47 schwarze 1120: /*
1121: * If no page content can be found, or the input line
1122: * is already the next section header, or there is no
1123: * trailing newline, reuse the page title as the page
1124: * description.
1125: */
1.1 schwarze 1126:
1.47 schwarze 1127: if (NULL == title || '\0' == *title) {
1128: if (warnings)
1129: say(mpage->mlinks->file,
1130: "Cannot find NAME section");
1131: assert(NULL == mpage->desc);
1132: mpage->desc = mandoc_strdup(mpage->mlinks->name);
1133: putkey(mpage, mpage->mlinks->name, TYPE_Nd);
1134: fclose(stream);
1135: free(title);
1136: return;
1137: }
1.24 schwarze 1138:
1.47 schwarze 1139: title = mandoc_realloc(title, titlesz + 1);
1140: title[titlesz] = '\0';
1.24 schwarze 1141:
1.47 schwarze 1142: /*
1143: * Skip to the first dash.
1144: * Use the remaining line as the description (no more than 70
1145: * bytes).
1146: */
1.28 schwarze 1147:
1.47 schwarze 1148: if (NULL != (p = strstr(title, "- "))) {
1149: for (p += 2; ' ' == *p || '\b' == *p; p++)
1150: /* Skip to next word. */ ;
1151: } else {
1152: if (warnings)
1153: say(mpage->mlinks->file,
1154: "No dash in title line");
1155: p = title;
1156: }
1.1 schwarze 1157:
1.47 schwarze 1158: plen = strlen(p);
1.1 schwarze 1159:
1.47 schwarze 1160: /* Strip backspace-encoding from line. */
1.1 schwarze 1161:
1.47 schwarze 1162: while (NULL != (line = memchr(p, '\b', plen))) {
1163: len = line - p;
1164: if (0 == len) {
1165: memmove(line, line + 1, plen--);
1166: continue;
1167: }
1168: memmove(line - 1, line + 1, plen - len);
1169: plen -= 2;
1170: }
1.1 schwarze 1171:
1.47 schwarze 1172: assert(NULL == mpage->desc);
1173: mpage->desc = mandoc_strdup(p);
1174: putkey(mpage, mpage->desc, TYPE_Nd);
1175: fclose(stream);
1176: free(title);
1177: }
1.16 schwarze 1178:
1.47 schwarze 1179: /*
1180: * Put a type/word pair into the word database for this particular file.
1181: */
1182: static void
1.69 schwarze 1183: putkey(const struct mpage *mpage, char *value, uint64_t type)
1.47 schwarze 1184: {
1.69 schwarze 1185: char *cp;
1.37 schwarze 1186:
1.47 schwarze 1187: assert(NULL != value);
1.69 schwarze 1188: if (TYPE_arch == type)
1189: for (cp = value; *cp; cp++)
1190: if (isupper((unsigned char)*cp))
1191: *cp = _tolower((unsigned char)*cp);
1.47 schwarze 1192: putkeys(mpage, value, strlen(value), type);
1.2 schwarze 1193: }
1194:
1195: /*
1.47 schwarze 1196: * Grok all nodes at or below a certain mdoc node into putkey().
1.2 schwarze 1197: */
1198: static void
1.47 schwarze 1199: putmdockey(const struct mpage *mpage,
1200: const struct mdoc_node *n, uint64_t m)
1.2 schwarze 1201: {
1.16 schwarze 1202:
1.47 schwarze 1203: for ( ; NULL != n; n = n->next) {
1204: if (NULL != n->child)
1205: putmdockey(mpage, n->child, m);
1206: if (MDOC_TEXT == n->type)
1207: putkey(mpage, n->string, m);
1208: }
1209: }
1.16 schwarze 1210:
1.47 schwarze 1211: static void
1212: parse_man(struct mpage *mpage, const struct man_node *n)
1213: {
1214: const struct man_node *head, *body;
1215: char *start, *sv, *title;
1216: char byte;
1217: size_t sz, titlesz;
1.16 schwarze 1218:
1.47 schwarze 1219: if (NULL == n)
1220: return;
1.16 schwarze 1221:
1.47 schwarze 1222: /*
1223: * We're only searching for one thing: the first text child in
1224: * the BODY of a NAME section. Since we don't keep track of
1225: * sections in -man, run some hoops to find out whether we're in
1226: * the correct section or not.
1227: */
1.16 schwarze 1228:
1.47 schwarze 1229: if (MAN_BODY == n->type && MAN_SH == n->tok) {
1230: body = n;
1231: assert(body->parent);
1232: if (NULL != (head = body->parent->head) &&
1233: 1 == head->nchild &&
1234: NULL != (head = (head->child)) &&
1235: MAN_TEXT == head->type &&
1236: 0 == strcmp(head->string, "NAME") &&
1237: NULL != (body = body->child) &&
1238: MAN_TEXT == body->type) {
1.2 schwarze 1239:
1.47 schwarze 1240: title = NULL;
1241: titlesz = 0;
1.2 schwarze 1242:
1.47 schwarze 1243: /*
1244: * Suck the entire NAME section into memory.
1245: * Yes, we might run away.
1246: * But too many manuals have big, spread-out
1247: * NAME sections over many lines.
1248: */
1.2 schwarze 1249:
1.47 schwarze 1250: for ( ; NULL != body; body = body->next) {
1251: if (MAN_TEXT != body->type)
1252: break;
1253: if (0 == (sz = strlen(body->string)))
1254: continue;
1255: title = mandoc_realloc
1256: (title, titlesz + sz + 1);
1257: memcpy(title + titlesz, body->string, sz);
1258: titlesz += sz + 1;
1259: title[titlesz - 1] = ' ';
1260: }
1261: if (NULL == title)
1262: return;
1.16 schwarze 1263:
1.47 schwarze 1264: title = mandoc_realloc(title, titlesz + 1);
1265: title[titlesz] = '\0';
1.16 schwarze 1266:
1.47 schwarze 1267: /* Skip leading space. */
1.16 schwarze 1268:
1.47 schwarze 1269: sv = title;
1270: while (isspace((unsigned char)*sv))
1271: sv++;
1.16 schwarze 1272:
1.47 schwarze 1273: if (0 == (sz = strlen(sv))) {
1274: free(title);
1275: return;
1276: }
1.1 schwarze 1277:
1.47 schwarze 1278: /* Erase trailing space. */
1.1 schwarze 1279:
1.47 schwarze 1280: start = &sv[sz - 1];
1281: while (start > sv && isspace((unsigned char)*start))
1282: *start-- = '\0';
1.1 schwarze 1283:
1.47 schwarze 1284: if (start == sv) {
1285: free(title);
1286: return;
1287: }
1.1 schwarze 1288:
1.47 schwarze 1289: start = sv;
1.16 schwarze 1290:
1.47 schwarze 1291: /*
1292: * Go through a special heuristic dance here.
1293: * Conventionally, one or more manual names are
1294: * comma-specified prior to a whitespace, then a
1295: * dash, then a description. Try to puzzle out
1296: * the name parts here.
1297: */
1.16 schwarze 1298:
1.47 schwarze 1299: for ( ;; ) {
1300: sz = strcspn(start, " ,");
1301: if ('\0' == start[sz])
1302: break;
1.1 schwarze 1303:
1.47 schwarze 1304: byte = start[sz];
1305: start[sz] = '\0';
1.67 schwarze 1306:
1307: /*
1308: * Assume a stray trailing comma in the
1309: * name list if a name begins with a dash.
1310: */
1311:
1312: if ('-' == start[0] ||
1313: ('\\' == start[0] && '-' == start[1]))
1314: break;
1.1 schwarze 1315:
1.47 schwarze 1316: putkey(mpage, start, TYPE_Nm);
1.1 schwarze 1317:
1.47 schwarze 1318: if (' ' == byte) {
1319: start += sz + 1;
1320: break;
1321: }
1.1 schwarze 1322:
1.47 schwarze 1323: assert(',' == byte);
1324: start += sz + 1;
1325: while (' ' == *start)
1326: start++;
1327: }
1.1 schwarze 1328:
1.47 schwarze 1329: if (sv == start) {
1330: putkey(mpage, start, TYPE_Nm);
1331: free(title);
1332: return;
1333: }
1.1 schwarze 1334:
1.47 schwarze 1335: while (isspace((unsigned char)*start))
1336: start++;
1.1 schwarze 1337:
1.47 schwarze 1338: if (0 == strncmp(start, "-", 1))
1339: start += 1;
1340: else if (0 == strncmp(start, "\\-\\-", 4))
1341: start += 4;
1342: else if (0 == strncmp(start, "\\-", 2))
1343: start += 2;
1344: else if (0 == strncmp(start, "\\(en", 4))
1345: start += 4;
1346: else if (0 == strncmp(start, "\\(em", 4))
1347: start += 4;
1.1 schwarze 1348:
1.47 schwarze 1349: while (' ' == *start)
1350: start++;
1.1 schwarze 1351:
1.47 schwarze 1352: assert(NULL == mpage->desc);
1353: mpage->desc = mandoc_strdup(start);
1354: putkey(mpage, mpage->desc, TYPE_Nd);
1355: free(title);
1356: return;
1357: }
1358: }
1.1 schwarze 1359:
1.47 schwarze 1360: for (n = n->child; n; n = n->next) {
1361: if (NULL != mpage->desc)
1362: break;
1363: parse_man(mpage, n);
1.1 schwarze 1364: }
1365: }
1366:
1367: static void
1.47 schwarze 1368: parse_mdoc(struct mpage *mpage, const struct mdoc_node *n)
1.1 schwarze 1369: {
1370:
1.47 schwarze 1371: assert(NULL != n);
1372: for (n = n->child; NULL != n; n = n->next) {
1373: switch (n->type) {
1374: case (MDOC_ELEM):
1375: /* FALLTHROUGH */
1376: case (MDOC_BLOCK):
1377: /* FALLTHROUGH */
1378: case (MDOC_HEAD):
1379: /* FALLTHROUGH */
1380: case (MDOC_BODY):
1381: /* FALLTHROUGH */
1382: case (MDOC_TAIL):
1383: if (NULL != mdocs[n->tok].fp)
1384: if (0 == (*mdocs[n->tok].fp)(mpage, n))
1385: break;
1386: if (mdocs[n->tok].mask)
1387: putmdockey(mpage, n->child,
1388: mdocs[n->tok].mask);
1389: break;
1390: default:
1391: assert(MDOC_ROOT != n->type);
1392: continue;
1393: }
1394: if (NULL != n->child)
1395: parse_mdoc(mpage, n);
1.1 schwarze 1396: }
1397: }
1398:
1.19 schwarze 1399: static int
1.47 schwarze 1400: parse_mdoc_Fd(struct mpage *mpage, const struct mdoc_node *n)
1.1 schwarze 1401: {
1402: const char *start, *end;
1403: size_t sz;
1.19 schwarze 1404:
1.47 schwarze 1405: if (SEC_SYNOPSIS != n->sec ||
1406: NULL == (n = n->child) ||
1407: MDOC_TEXT != n->type)
1.19 schwarze 1408: return(0);
1.1 schwarze 1409:
1410: /*
1411: * Only consider those `Fd' macro fields that begin with an
1412: * "inclusion" token (versus, e.g., #define).
1413: */
1.47 schwarze 1414:
1.1 schwarze 1415: if (strcmp("#include", n->string))
1.19 schwarze 1416: return(0);
1.1 schwarze 1417:
1418: if (NULL == (n = n->next) || MDOC_TEXT != n->type)
1.19 schwarze 1419: return(0);
1.1 schwarze 1420:
1421: /*
1422: * Strip away the enclosing angle brackets and make sure we're
1423: * not zero-length.
1424: */
1425:
1426: start = n->string;
1427: if ('<' == *start || '"' == *start)
1428: start++;
1429:
1430: if (0 == (sz = strlen(start)))
1.19 schwarze 1431: return(0);
1.1 schwarze 1432:
1433: end = &start[(int)sz - 1];
1434: if ('>' == *end || '"' == *end)
1435: end--;
1436:
1.47 schwarze 1437: if (end > start)
1438: putkeys(mpage, start, end - start + 1, TYPE_In);
1.49 schwarze 1439: return(0);
1.1 schwarze 1440: }
1441:
1.19 schwarze 1442: static int
1.47 schwarze 1443: parse_mdoc_Fn(struct mpage *mpage, const struct mdoc_node *n)
1.1 schwarze 1444: {
1.69 schwarze 1445: char *cp;
1.1 schwarze 1446:
1.47 schwarze 1447: if (NULL == (n = n->child) || MDOC_TEXT != n->type)
1.19 schwarze 1448: return(0);
1449:
1.47 schwarze 1450: /*
1451: * Parse: .Fn "struct type *name" "char *arg".
1452: * First strip away pointer symbol.
1453: * Then store the function name, then type.
1454: * Finally, store the arguments.
1455: */
1.1 schwarze 1456:
1.47 schwarze 1457: if (NULL == (cp = strrchr(n->string, ' ')))
1458: cp = n->string;
1.1 schwarze 1459:
1460: while ('*' == *cp)
1461: cp++;
1462:
1.47 schwarze 1463: putkey(mpage, cp, TYPE_Fn);
1.19 schwarze 1464:
1.47 schwarze 1465: if (n->string < cp)
1466: putkeys(mpage, n->string, cp - n->string, TYPE_Ft);
1.19 schwarze 1467:
1.47 schwarze 1468: for (n = n->next; NULL != n; n = n->next)
1469: if (MDOC_TEXT == n->type)
1470: putkey(mpage, n->string, TYPE_Fa);
1.19 schwarze 1471:
1472: return(0);
1.1 schwarze 1473: }
1474:
1.19 schwarze 1475: static int
1.47 schwarze 1476: parse_mdoc_Xr(struct mpage *mpage, const struct mdoc_node *n)
1.1 schwarze 1477: {
1.47 schwarze 1478: char *cp;
1.1 schwarze 1479:
1480: if (NULL == (n = n->child))
1.19 schwarze 1481: return(0);
1.1 schwarze 1482:
1.47 schwarze 1483: if (NULL == n->next) {
1484: putkey(mpage, n->string, TYPE_Xr);
1485: return(0);
1486: }
1.1 schwarze 1487:
1.47 schwarze 1488: if (-1 == asprintf(&cp, "%s(%s)", n->string, n->next->string)) {
1489: perror(NULL);
1490: exit((int)MANDOCLEVEL_SYSERR);
1491: }
1492: putkey(mpage, cp, TYPE_Xr);
1493: free(cp);
1494: return(0);
1.1 schwarze 1495: }
1496:
1.19 schwarze 1497: static int
1.47 schwarze 1498: parse_mdoc_Nd(struct mpage *mpage, const struct mdoc_node *n)
1.1 schwarze 1499: {
1.47 schwarze 1500: size_t sz;
1.1 schwarze 1501:
1502: if (MDOC_BODY != n->type)
1.19 schwarze 1503: return(0);
1.1 schwarze 1504:
1.47 schwarze 1505: /*
1506: * Special-case the `Nd' because we need to put the description
1507: * into the document table.
1508: */
1509:
1510: for (n = n->child; NULL != n; n = n->next) {
1511: if (MDOC_TEXT == n->type) {
1512: if (NULL != mpage->desc) {
1513: sz = strlen(mpage->desc) +
1514: strlen(n->string) + 2;
1515: mpage->desc = mandoc_realloc(
1516: mpage->desc, sz);
1517: strlcat(mpage->desc, " ", sz);
1518: strlcat(mpage->desc, n->string, sz);
1519: } else
1520: mpage->desc = mandoc_strdup(n->string);
1521: }
1522: if (NULL != n->child)
1523: parse_mdoc_Nd(mpage, n);
1524: }
1.19 schwarze 1525: return(1);
1.1 schwarze 1526: }
1527:
1.19 schwarze 1528: static int
1.47 schwarze 1529: parse_mdoc_Nm(struct mpage *mpage, const struct mdoc_node *n)
1.1 schwarze 1530: {
1531:
1.49 schwarze 1532: return(SEC_NAME == n->sec ||
1533: (SEC_SYNOPSIS == n->sec && MDOC_HEAD == n->type));
1.1 schwarze 1534: }
1535:
1.19 schwarze 1536: static int
1.47 schwarze 1537: parse_mdoc_Sh(struct mpage *mpage, const struct mdoc_node *n)
1.1 schwarze 1538: {
1539:
1.19 schwarze 1540: return(SEC_CUSTOM == n->sec && MDOC_HEAD == n->type);
1.1 schwarze 1541: }
1542:
1.47 schwarze 1543: static int
1544: parse_mdoc_head(struct mpage *mpage, const struct mdoc_node *n)
1.1 schwarze 1545: {
1546:
1.47 schwarze 1547: return(MDOC_HEAD == n->type);
1548: }
1.1 schwarze 1549:
1.47 schwarze 1550: static int
1551: parse_mdoc_body(struct mpage *mpage, const struct mdoc_node *n)
1552: {
1.1 schwarze 1553:
1.47 schwarze 1554: return(MDOC_BODY == n->type);
1.1 schwarze 1555: }
1556:
1.47 schwarze 1557: /*
1558: * Add a string to the hash table for the current manual.
1559: * Each string has a bitmask telling which macros it belongs to.
1560: * When we finish the manual, we'll dump the table.
1561: */
1.1 schwarze 1562: static void
1.47 schwarze 1563: putkeys(const struct mpage *mpage,
1564: const char *cp, size_t sz, uint64_t v)
1.1 schwarze 1565: {
1.47 schwarze 1566: struct str *s;
1.68 schwarze 1567: const char *end;
1568: uint64_t mask;
1.47 schwarze 1569: unsigned int slot;
1.68 schwarze 1570: int i;
1.1 schwarze 1571:
1.47 schwarze 1572: if (0 == sz)
1573: return;
1.68 schwarze 1574:
1575: if (verb > 1) {
1576: for (i = 0, mask = 1;
1577: i < mansearch_keymax;
1578: i++, mask <<= 1)
1579: if (mask & v)
1580: break;
1581: say(mpage->mlinks->file, "Adding key %s=%*s",
1582: mansearch_keynames[i], sz, cp);
1583: }
1.47 schwarze 1584:
1585: end = cp + sz;
1586: slot = ohash_qlookupi(&strings, cp, &end);
1587: s = ohash_find(&strings, slot);
1.1 schwarze 1588:
1.47 schwarze 1589: if (NULL != s && mpage == s->mpage) {
1590: s->mask |= v;
1.1 schwarze 1591: return;
1.47 schwarze 1592: } else if (NULL == s) {
1593: s = mandoc_calloc(sizeof(struct str) + sz + 1, 1);
1594: memcpy(s->key, cp, sz);
1595: ohash_insert(&strings, slot, s);
1596: }
1597: s->mpage = mpage;
1598: s->mask = v;
1.1 schwarze 1599: }
1600:
1601: /*
1.47 schwarze 1602: * Take a Unicode codepoint and produce its UTF-8 encoding.
1603: * This isn't the best way to do this, but it works.
1604: * The magic numbers are from the UTF-8 packaging.
1605: * They're not as scary as they seem: read the UTF-8 spec for details.
1.1 schwarze 1606: */
1.47 schwarze 1607: static size_t
1608: utf8(unsigned int cp, char out[7])
1.1 schwarze 1609: {
1.47 schwarze 1610: size_t rc;
1.1 schwarze 1611:
1.47 schwarze 1612: rc = 0;
1613: if (cp <= 0x0000007F) {
1614: rc = 1;
1615: out[0] = (char)cp;
1616: } else if (cp <= 0x000007FF) {
1617: rc = 2;
1618: out[0] = (cp >> 6 & 31) | 192;
1619: out[1] = (cp & 63) | 128;
1620: } else if (cp <= 0x0000FFFF) {
1621: rc = 3;
1622: out[0] = (cp >> 12 & 15) | 224;
1623: out[1] = (cp >> 6 & 63) | 128;
1624: out[2] = (cp & 63) | 128;
1625: } else if (cp <= 0x001FFFFF) {
1626: rc = 4;
1627: out[0] = (cp >> 18 & 7) | 240;
1628: out[1] = (cp >> 12 & 63) | 128;
1629: out[2] = (cp >> 6 & 63) | 128;
1630: out[3] = (cp & 63) | 128;
1631: } else if (cp <= 0x03FFFFFF) {
1632: rc = 5;
1633: out[0] = (cp >> 24 & 3) | 248;
1634: out[1] = (cp >> 18 & 63) | 128;
1635: out[2] = (cp >> 12 & 63) | 128;
1636: out[3] = (cp >> 6 & 63) | 128;
1637: out[4] = (cp & 63) | 128;
1638: } else if (cp <= 0x7FFFFFFF) {
1639: rc = 6;
1640: out[0] = (cp >> 30 & 1) | 252;
1641: out[1] = (cp >> 24 & 63) | 128;
1642: out[2] = (cp >> 18 & 63) | 128;
1643: out[3] = (cp >> 12 & 63) | 128;
1644: out[4] = (cp >> 6 & 63) | 128;
1645: out[5] = (cp & 63) | 128;
1646: } else
1647: return(0);
1.19 schwarze 1648:
1.47 schwarze 1649: out[rc] = '\0';
1650: return(rc);
1.1 schwarze 1651: }
1652:
1.47 schwarze 1653: /*
1.53 schwarze 1654: * Store the rendered version of a key, or alias the pointer
1655: * if the key contains no escape sequences.
1.47 schwarze 1656: */
1657: static void
1.53 schwarze 1658: render_key(struct mchars *mc, struct str *key)
1.1 schwarze 1659: {
1.47 schwarze 1660: size_t sz, bsz, pos;
1.71 schwarze 1661: char utfbuf[7], res[6];
1.47 schwarze 1662: char *buf;
1663: const char *seq, *cpp, *val;
1664: int len, u;
1665: enum mandoc_esc esc;
1666:
1.53 schwarze 1667: assert(NULL == key->rendered);
1.47 schwarze 1668:
1669: res[0] = '\\';
1670: res[1] = '\t';
1671: res[2] = ASCII_NBRSP;
1672: res[3] = ASCII_HYPH;
1.71 schwarze 1673: res[4] = ASCII_BREAK;
1674: res[5] = '\0';
1.1 schwarze 1675:
1.47 schwarze 1676: val = key->key;
1677: bsz = strlen(val);
1.1 schwarze 1678:
1679: /*
1.47 schwarze 1680: * Pre-check: if we have no stop-characters, then set the
1681: * pointer as ourselvse and get out of here.
1.1 schwarze 1682: */
1.47 schwarze 1683: if (strcspn(val, res) == bsz) {
1.53 schwarze 1684: key->rendered = key->key;
1.47 schwarze 1685: return;
1686: }
1.1 schwarze 1687:
1.47 schwarze 1688: /* Pre-allocate by the length of the input */
1.39 schwarze 1689:
1.47 schwarze 1690: buf = mandoc_malloc(++bsz);
1691: pos = 0;
1.39 schwarze 1692:
1.47 schwarze 1693: while ('\0' != *val) {
1694: /*
1695: * Halt on the first escape sequence.
1696: * This also halts on the end of string, in which case
1697: * we just copy, fallthrough, and exit the loop.
1698: */
1699: if ((sz = strcspn(val, res)) > 0) {
1700: memcpy(&buf[pos], val, sz);
1701: pos += sz;
1702: val += sz;
1703: }
1.39 schwarze 1704:
1.71 schwarze 1705: switch (*val) {
1706: case (ASCII_HYPH):
1.47 schwarze 1707: buf[pos++] = '-';
1708: val++;
1709: continue;
1.71 schwarze 1710: case ('\t'):
1711: /* FALLTHROUGH */
1712: case (ASCII_NBRSP):
1.47 schwarze 1713: buf[pos++] = ' ';
1714: val++;
1.71 schwarze 1715: /* FALLTHROUGH */
1716: case (ASCII_BREAK):
1.47 schwarze 1717: continue;
1.71 schwarze 1718: default:
1719: break;
1720: }
1721: if ('\\' != *val)
1.47 schwarze 1722: break;
1.39 schwarze 1723:
1.47 schwarze 1724: /* Read past the slash. */
1.39 schwarze 1725:
1.47 schwarze 1726: val++;
1.39 schwarze 1727:
1.47 schwarze 1728: /*
1729: * Parse the escape sequence and see if it's a
1730: * predefined character or special character.
1731: */
1.52 schwarze 1732:
1.47 schwarze 1733: esc = mandoc_escape
1734: ((const char **)&val, &seq, &len);
1735: if (ESCAPE_ERROR == esc)
1736: break;
1737: if (ESCAPE_SPECIAL != esc)
1738: continue;
1.39 schwarze 1739:
1.47 schwarze 1740: /*
1.52 schwarze 1741: * Render the special character
1742: * as either UTF-8 or ASCII.
1.47 schwarze 1743: */
1.52 schwarze 1744:
1745: if (write_utf8) {
1746: if (0 == (u = mchars_spec2cp(mc, seq, len)))
1747: continue;
1748: cpp = utfbuf;
1749: if (0 == (sz = utf8(u, utfbuf)))
1750: continue;
1751: sz = strlen(cpp);
1752: } else {
1753: cpp = mchars_spec2str(mc, seq, len, &sz);
1754: if (NULL == cpp)
1755: continue;
1756: if (ASCII_NBRSP == *cpp) {
1757: cpp = " ";
1758: sz = 1;
1759: }
1760: }
1.1 schwarze 1761:
1.47 schwarze 1762: /* Copy the rendered glyph into the stream. */
1.1 schwarze 1763:
1.47 schwarze 1764: bsz += sz;
1765: buf = mandoc_realloc(buf, bsz);
1766: memcpy(&buf[pos], cpp, sz);
1767: pos += sz;
1.1 schwarze 1768: }
1769:
1.47 schwarze 1770: buf[pos] = '\0';
1.53 schwarze 1771: key->rendered = buf;
1.1 schwarze 1772: }
1773:
1.11 schwarze 1774: /*
1.47 schwarze 1775: * Flush the current page's terms (and their bits) into the database.
1776: * Wrap the entire set of additions in a transaction to make sqlite be a
1777: * little faster.
1.53 schwarze 1778: * Also, handle escape sequences at the last possible moment.
1.11 schwarze 1779: */
1780: static void
1.62 schwarze 1781: dbadd(const struct mpage *mpage, struct mchars *mc)
1.11 schwarze 1782: {
1.47 schwarze 1783: struct mlink *mlink;
1784: struct str *key;
1785: int64_t recno;
1786: size_t i;
1787: unsigned int slot;
1788:
1789: if (verb)
1.62 schwarze 1790: say(mpage->mlinks->file, "Adding to database");
1.11 schwarze 1791:
1.47 schwarze 1792: if (nodb)
1.11 schwarze 1793: return;
1.47 schwarze 1794:
1795: i = 1;
1796: SQL_BIND_INT(stmts[STMT_INSERT_PAGE], i, FORM_SRC == mpage->form);
1797: SQL_STEP(stmts[STMT_INSERT_PAGE]);
1798: recno = sqlite3_last_insert_rowid(db);
1799: sqlite3_reset(stmts[STMT_INSERT_PAGE]);
1800:
1801: for (mlink = mpage->mlinks; mlink; mlink = mlink->next) {
1802: i = 1;
1803: SQL_BIND_TEXT(stmts[STMT_INSERT_LINK], i, mlink->dsec);
1804: SQL_BIND_TEXT(stmts[STMT_INSERT_LINK], i, mlink->arch);
1805: SQL_BIND_TEXT(stmts[STMT_INSERT_LINK], i, mlink->name);
1806: SQL_BIND_INT64(stmts[STMT_INSERT_LINK], i, recno);
1807: SQL_STEP(stmts[STMT_INSERT_LINK]);
1808: sqlite3_reset(stmts[STMT_INSERT_LINK]);
1809: }
1810:
1811: for (key = ohash_first(&strings, &slot); NULL != key;
1812: key = ohash_next(&strings, &slot)) {
1813: assert(key->mpage == mpage);
1.53 schwarze 1814: if (NULL == key->rendered)
1815: render_key(mc, key);
1.47 schwarze 1816: i = 1;
1817: SQL_BIND_INT64(stmts[STMT_INSERT_KEY], i, key->mask);
1.53 schwarze 1818: SQL_BIND_TEXT(stmts[STMT_INSERT_KEY], i, key->rendered);
1.47 schwarze 1819: SQL_BIND_INT64(stmts[STMT_INSERT_KEY], i, recno);
1820: SQL_STEP(stmts[STMT_INSERT_KEY]);
1821: sqlite3_reset(stmts[STMT_INSERT_KEY]);
1.53 schwarze 1822: if (key->rendered != key->key)
1823: free(key->rendered);
1.47 schwarze 1824: free(key);
1.33 schwarze 1825: }
1.47 schwarze 1826: }
1.41 deraadt 1827:
1.47 schwarze 1828: static void
1829: dbprune(void)
1830: {
1831: struct mpage *mpage;
1832: struct mlink *mlink;
1833: size_t i;
1834: unsigned int slot;
1.11 schwarze 1835:
1.63 schwarze 1836: if (0 == nodb)
1837: SQL_EXEC("BEGIN TRANSACTION");
1.47 schwarze 1838:
1.63 schwarze 1839: for (mpage = ohash_first(&mpages, &slot); NULL != mpage;
1840: mpage = ohash_next(&mpages, &slot)) {
1.47 schwarze 1841: mlink = mpage->mlinks;
1842: if (verb)
1.63 schwarze 1843: say(mlink->file, "Deleting from database");
1844: if (nodb)
1845: continue;
1846: for ( ; NULL != mlink; mlink = mlink->next) {
1847: i = 1;
1848: SQL_BIND_TEXT(stmts[STMT_DELETE_PAGE],
1849: i, mlink->dsec);
1850: SQL_BIND_TEXT(stmts[STMT_DELETE_PAGE],
1851: i, mlink->arch);
1852: SQL_BIND_TEXT(stmts[STMT_DELETE_PAGE],
1853: i, mlink->name);
1854: SQL_STEP(stmts[STMT_DELETE_PAGE]);
1855: sqlite3_reset(stmts[STMT_DELETE_PAGE]);
1856: }
1.11 schwarze 1857: }
1.63 schwarze 1858:
1859: if (0 == nodb)
1860: SQL_EXEC("END TRANSACTION");
1.47 schwarze 1861: }
1.22 schwarze 1862:
1.47 schwarze 1863: /*
1864: * Close an existing database and its prepared statements.
1865: * If "real" is not set, rename the temporary file into the real one.
1866: */
1867: static void
1868: dbclose(int real)
1869: {
1870: size_t i;
1.72 schwarze 1871: int status;
1872: pid_t child;
1.11 schwarze 1873:
1.47 schwarze 1874: if (nodb)
1875: return;
1.11 schwarze 1876:
1.47 schwarze 1877: for (i = 0; i < STMT__MAX; i++) {
1878: sqlite3_finalize(stmts[i]);
1879: stmts[i] = NULL;
1.28 schwarze 1880: }
1.22 schwarze 1881:
1.47 schwarze 1882: sqlite3_close(db);
1883: db = NULL;
1.11 schwarze 1884:
1.47 schwarze 1885: if (real)
1886: return;
1.22 schwarze 1887:
1.72 schwarze 1888: if ('\0' == *tempfilename) {
1889: if (-1 == rename(MANDOC_DB "~", MANDOC_DB)) {
1890: exitcode = (int)MANDOCLEVEL_SYSERR;
1891: say(MANDOC_DB, "%s", strerror(errno));
1892: }
1893: return;
1894: }
1895:
1896: switch (child = fork()) {
1897: case (-1):
1898: exitcode = (int)MANDOCLEVEL_SYSERR;
1899: say("fork cmp", "%s", strerror(errno));
1900: return;
1901: case (0):
1902: execlp("cmp", "cmp", "-s",
1903: tempfilename, MANDOC_DB, NULL);
1904: say("exec cmp", "%s", strerror(errno));
1905: exit(0);
1906: default:
1907: break;
1908: }
1909: if (-1 == waitpid(child, &status, 0)) {
1910: exitcode = (int)MANDOCLEVEL_SYSERR;
1911: say("wait cmp", "%s", strerror(errno));
1912: } else if (WIFSIGNALED(status)) {
1913: exitcode = (int)MANDOCLEVEL_SYSERR;
1914: say("cmp", "Died from a signal");
1915: } else if (WEXITSTATUS(status)) {
1.47 schwarze 1916: exitcode = (int)MANDOCLEVEL_SYSERR;
1.72 schwarze 1917: say(MANDOC_DB,
1918: "Data changed, but cannot replace database");
1919: }
1920:
1921: *strrchr(tempfilename, '/') = '\0';
1922: switch (child = fork()) {
1923: case (-1):
1924: exitcode = (int)MANDOCLEVEL_SYSERR;
1925: say("fork rm", "%s", strerror(errno));
1926: return;
1927: case (0):
1928: execlp("rm", "rm", "-rf", tempfilename, NULL);
1929: say("exec rm", "%s", strerror(errno));
1930: exit((int)MANDOCLEVEL_SYSERR);
1931: default:
1932: break;
1933: }
1934: if (-1 == waitpid(child, &status, 0)) {
1935: exitcode = (int)MANDOCLEVEL_SYSERR;
1936: say("wait rm", "%s", strerror(errno));
1937: } else if (WIFSIGNALED(status) || WEXITSTATUS(status)) {
1938: exitcode = (int)MANDOCLEVEL_SYSERR;
1939: say(tempfilename,
1940: "Cannot remove temporary directory");
1.22 schwarze 1941: }
1.11 schwarze 1942: }
1943:
1.47 schwarze 1944: /*
1945: * This is straightforward stuff.
1946: * Open a database connection to a "temporary" database, then open a set
1947: * of prepared statements we'll use over and over again.
1948: * If "real" is set, we use the existing database; if not, we truncate a
1949: * temporary one.
1950: * Must be matched by dbclose().
1951: */
1952: static int
1953: dbopen(int real)
1.2 schwarze 1954: {
1.72 schwarze 1955: const char *sql;
1.47 schwarze 1956: int rc, ofl;
1.6 schwarze 1957:
1.47 schwarze 1958: if (nodb)
1959: return(1);
1.6 schwarze 1960:
1.72 schwarze 1961: *tempfilename = '\0';
1.47 schwarze 1962: ofl = SQLITE_OPEN_READWRITE;
1.72 schwarze 1963:
1964: if (real) {
1965: rc = sqlite3_open_v2(MANDOC_DB, &db, ofl, NULL);
1966: if (SQLITE_OK != rc) {
1.47 schwarze 1967: exitcode = (int)MANDOCLEVEL_SYSERR;
1.72 schwarze 1968: say(MANDOC_DB, "%s", sqlite3_errmsg(db));
1.47 schwarze 1969: return(0);
1.28 schwarze 1970: }
1.72 schwarze 1971: goto prepare_statements;
1972: }
1973:
1974: ofl |= SQLITE_OPEN_CREATE | SQLITE_OPEN_EXCLUSIVE;
1.6 schwarze 1975:
1.72 schwarze 1976: remove(MANDOC_DB "~");
1977: rc = sqlite3_open_v2(MANDOC_DB "~", &db, ofl, NULL);
1.47 schwarze 1978: if (SQLITE_OK == rc)
1.72 schwarze 1979: goto create_tables;
1.73 ! schwarze 1980: if (MPARSE_QUICK & mparse_options) {
1.47 schwarze 1981: exitcode = (int)MANDOCLEVEL_SYSERR;
1.72 schwarze 1982: say(MANDOC_DB "~", "%s", sqlite3_errmsg(db));
1.47 schwarze 1983: return(0);
1984: }
1.6 schwarze 1985:
1.72 schwarze 1986: if (strlcpy(tempfilename, "/tmp/mandocdb.XXXXXX",
1987: sizeof(tempfilename)) >= sizeof(tempfilename)) {
1988: exitcode = (int)MANDOCLEVEL_SYSERR;
1989: say("/tmp/mandocdb.XXXXXX", "Filename too long");
1990: return(0);
1991: }
1992: if (NULL == mkdtemp(tempfilename)) {
1993: exitcode = (int)MANDOCLEVEL_SYSERR;
1994: say(tempfilename, "%s", strerror(errno));
1995: return(0);
1996: }
1997: if (strlcat(tempfilename, "/" MANDOC_DB,
1998: sizeof(tempfilename)) >= sizeof(tempfilename)) {
1999: exitcode = (int)MANDOCLEVEL_SYSERR;
2000: say(tempfilename, "Filename too long");
2001: return(0);
2002: }
2003: rc = sqlite3_open_v2(tempfilename, &db, ofl, NULL);
2004: if (SQLITE_OK != rc) {
1.47 schwarze 2005: exitcode = (int)MANDOCLEVEL_SYSERR;
1.72 schwarze 2006: say(tempfilename, "%s", sqlite3_errmsg(db));
1.47 schwarze 2007: return(0);
1.2 schwarze 2008: }
2009:
1.72 schwarze 2010: create_tables:
1.47 schwarze 2011: sql = "CREATE TABLE \"mpages\" (\n"
2012: " \"form\" INTEGER NOT NULL,\n"
2013: " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
2014: ");\n"
2015: "\n"
2016: "CREATE TABLE \"mlinks\" (\n"
2017: " \"sec\" TEXT NOT NULL,\n"
2018: " \"arch\" TEXT NOT NULL,\n"
2019: " \"name\" TEXT NOT NULL,\n"
2020: " \"pageid\" INTEGER NOT NULL REFERENCES mpages(id) "
1.66 schwarze 2021: "ON DELETE CASCADE\n"
1.47 schwarze 2022: ");\n"
2023: "\n"
2024: "CREATE TABLE \"keys\" (\n"
2025: " \"bits\" INTEGER NOT NULL,\n"
2026: " \"key\" TEXT NOT NULL,\n"
2027: " \"pageid\" INTEGER NOT NULL REFERENCES mpages(id) "
1.66 schwarze 2028: "ON DELETE CASCADE\n"
1.65 schwarze 2029: ");\n";
1.47 schwarze 2030:
2031: if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, NULL)) {
2032: exitcode = (int)MANDOCLEVEL_SYSERR;
1.72 schwarze 2033: say(MANDOC_DB, "%s", sqlite3_errmsg(db));
1.47 schwarze 2034: return(0);
1.2 schwarze 2035: }
2036:
1.47 schwarze 2037: prepare_statements:
2038: SQL_EXEC("PRAGMA foreign_keys = ON");
1.63 schwarze 2039: sql = "DELETE FROM mpages WHERE id IN "
2040: "(SELECT pageid FROM mlinks WHERE "
2041: "sec=? AND arch=? AND name=?)";
1.47 schwarze 2042: sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_DELETE_PAGE], NULL);
2043: sql = "INSERT INTO mpages "
1.60 schwarze 2044: "(form) VALUES (?)";
1.47 schwarze 2045: sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_PAGE], NULL);
2046: sql = "INSERT INTO mlinks "
1.61 schwarze 2047: "(sec,arch,name,pageid) VALUES (?,?,?,?)";
1.47 schwarze 2048: sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_LINK], NULL);
2049: sql = "INSERT INTO keys "
2050: "(bits,key,pageid) VALUES (?,?,?)";
2051: sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_KEY], NULL);
1.6 schwarze 2052:
1.47 schwarze 2053: /*
2054: * When opening a new database, we can turn off
2055: * synchronous mode for much better performance.
2056: */
1.6 schwarze 2057:
1.47 schwarze 2058: if (real)
2059: SQL_EXEC("PRAGMA synchronous = OFF");
1.11 schwarze 2060:
1.47 schwarze 2061: return(1);
2062: }
1.6 schwarze 2063:
1.47 schwarze 2064: static void *
2065: hash_halloc(size_t sz, void *arg)
2066: {
1.6 schwarze 2067:
1.47 schwarze 2068: return(mandoc_calloc(sz, 1));
2069: }
1.2 schwarze 2070:
1.47 schwarze 2071: static void *
2072: hash_alloc(size_t sz, void *arg)
2073: {
1.28 schwarze 2074:
1.47 schwarze 2075: return(mandoc_malloc(sz));
2076: }
1.6 schwarze 2077:
1.47 schwarze 2078: static void
2079: hash_free(void *p, size_t sz, void *arg)
2080: {
1.26 schwarze 2081:
1.47 schwarze 2082: free(p);
2083: }
1.6 schwarze 2084:
1.47 schwarze 2085: static int
2086: set_basedir(const char *targetdir)
2087: {
2088: static char startdir[PATH_MAX];
2089: static int fd;
1.6 schwarze 2090:
1.47 schwarze 2091: /*
2092: * Remember where we started by keeping a fd open to the origin
2093: * path component: throughout this utility, we chdir() a lot to
2094: * handle relative paths, and by doing this, we can return to
2095: * the starting point.
2096: */
2097: if ('\0' == *startdir) {
2098: if (NULL == getcwd(startdir, PATH_MAX)) {
2099: exitcode = (int)MANDOCLEVEL_SYSERR;
2100: if (NULL != targetdir)
2101: say(".", NULL);
2102: return(0);
2103: }
2104: if (-1 == (fd = open(startdir, O_RDONLY, 0))) {
2105: exitcode = (int)MANDOCLEVEL_SYSERR;
2106: say(startdir, NULL);
2107: return(0);
1.11 schwarze 2108: }
1.47 schwarze 2109: if (NULL == targetdir)
2110: targetdir = startdir;
2111: } else {
2112: if (-1 == fd)
2113: return(0);
2114: if (-1 == fchdir(fd)) {
2115: close(fd);
2116: basedir[0] = '\0';
2117: exitcode = (int)MANDOCLEVEL_SYSERR;
2118: say(startdir, NULL);
2119: return(0);
1.2 schwarze 2120: }
1.47 schwarze 2121: if (NULL == targetdir) {
2122: close(fd);
2123: return(1);
1.2 schwarze 2124: }
2125: }
1.47 schwarze 2126: if (NULL == realpath(targetdir, basedir)) {
2127: basedir[0] = '\0';
2128: exitcode = (int)MANDOCLEVEL_BADARG;
2129: say(targetdir, NULL);
2130: return(0);
2131: } else if (-1 == chdir(basedir)) {
2132: exitcode = (int)MANDOCLEVEL_BADARG;
2133: say("", NULL);
2134: return(0);
2135: }
2136: return(1);
1.2 schwarze 2137: }
2138:
2139: static void
1.47 schwarze 2140: say(const char *file, const char *format, ...)
1.2 schwarze 2141: {
1.47 schwarze 2142: va_list ap;
1.2 schwarze 2143:
1.47 schwarze 2144: if ('\0' != *basedir)
2145: fprintf(stderr, "%s", basedir);
2146: if ('\0' != *basedir && '\0' != *file)
2147: fputs("//", stderr);
2148: if ('\0' != *file)
2149: fprintf(stderr, "%s", file);
2150: fputs(": ", stderr);
1.31 schwarze 2151:
1.47 schwarze 2152: if (NULL == format) {
2153: perror(NULL);
2154: return;
1.2 schwarze 2155: }
1.47 schwarze 2156:
2157: va_start(ap, format);
2158: vfprintf(stderr, format, ap);
2159: va_end(ap);
2160:
2161: fputc('\n', stderr);
1.1 schwarze 2162: }