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