Annotation of src/usr.bin/mandoc/mandocdb.c, Revision 1.80
1.80 ! schwarze 1: /* $Id: mandocdb.c,v 1.79 2014/03/23 12:44:18 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;
1.80 ! schwarze 546: say("", "&fts_open");
1.47 schwarze 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;
1.80 ! schwarze 700: say(file, "&realpath");
1.47 schwarze 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;
1.80 ! schwarze 716: say(file, "&stat");
1.47 schwarze 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)
1.80 ! schwarze 1119: say(mpage->mlinks->file, "&fopen");
1.47 schwarze 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;
1.78 schwarze 1258: char *start, *title;
1.47 schwarze 1259: char byte;
1.78 schwarze 1260: size_t sz;
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") &&
1.78 schwarze 1280: NULL != body->child) {
1.2 schwarze 1281:
1.47 schwarze 1282: /*
1283: * Suck the entire NAME section into memory.
1284: * Yes, we might run away.
1285: * But too many manuals have big, spread-out
1286: * NAME sections over many lines.
1287: */
1.2 schwarze 1288:
1.78 schwarze 1289: title = NULL;
1290: man_deroff(&title, body);
1.47 schwarze 1291: if (NULL == title)
1292: return;
1.16 schwarze 1293:
1.47 schwarze 1294: /*
1295: * Go through a special heuristic dance here.
1296: * Conventionally, one or more manual names are
1297: * comma-specified prior to a whitespace, then a
1298: * dash, then a description. Try to puzzle out
1299: * the name parts here.
1300: */
1.16 schwarze 1301:
1.78 schwarze 1302: start = title;
1.47 schwarze 1303: for ( ;; ) {
1304: sz = strcspn(start, " ,");
1305: if ('\0' == start[sz])
1306: break;
1.1 schwarze 1307:
1.47 schwarze 1308: byte = start[sz];
1309: start[sz] = '\0';
1.67 schwarze 1310:
1311: /*
1312: * Assume a stray trailing comma in the
1313: * name list if a name begins with a dash.
1314: */
1315:
1316: if ('-' == start[0] ||
1317: ('\\' == start[0] && '-' == start[1]))
1318: break;
1.1 schwarze 1319:
1.47 schwarze 1320: putkey(mpage, start, TYPE_Nm);
1.1 schwarze 1321:
1.47 schwarze 1322: if (' ' == byte) {
1323: start += sz + 1;
1324: break;
1325: }
1.1 schwarze 1326:
1.47 schwarze 1327: assert(',' == byte);
1328: start += sz + 1;
1329: while (' ' == *start)
1330: start++;
1331: }
1.1 schwarze 1332:
1.78 schwarze 1333: if (start == title) {
1.47 schwarze 1334: putkey(mpage, start, TYPE_Nm);
1335: free(title);
1336: return;
1337: }
1.1 schwarze 1338:
1.47 schwarze 1339: while (isspace((unsigned char)*start))
1340: start++;
1.1 schwarze 1341:
1.47 schwarze 1342: if (0 == strncmp(start, "-", 1))
1343: start += 1;
1344: else if (0 == strncmp(start, "\\-\\-", 4))
1345: start += 4;
1346: else if (0 == strncmp(start, "\\-", 2))
1347: start += 2;
1348: else if (0 == strncmp(start, "\\(en", 4))
1349: start += 4;
1350: else if (0 == strncmp(start, "\\(em", 4))
1351: start += 4;
1.1 schwarze 1352:
1.47 schwarze 1353: while (' ' == *start)
1354: start++;
1.1 schwarze 1355:
1.47 schwarze 1356: assert(NULL == mpage->desc);
1357: mpage->desc = mandoc_strdup(start);
1358: putkey(mpage, mpage->desc, TYPE_Nd);
1359: free(title);
1360: return;
1361: }
1362: }
1.1 schwarze 1363:
1.47 schwarze 1364: for (n = n->child; n; n = n->next) {
1365: if (NULL != mpage->desc)
1366: break;
1367: parse_man(mpage, n);
1.1 schwarze 1368: }
1369: }
1370:
1371: static void
1.47 schwarze 1372: parse_mdoc(struct mpage *mpage, const struct mdoc_node *n)
1.1 schwarze 1373: {
1374:
1.47 schwarze 1375: assert(NULL != n);
1376: for (n = n->child; NULL != n; n = n->next) {
1377: switch (n->type) {
1378: case (MDOC_ELEM):
1379: /* FALLTHROUGH */
1380: case (MDOC_BLOCK):
1381: /* FALLTHROUGH */
1382: case (MDOC_HEAD):
1383: /* FALLTHROUGH */
1384: case (MDOC_BODY):
1385: /* FALLTHROUGH */
1386: case (MDOC_TAIL):
1387: if (NULL != mdocs[n->tok].fp)
1388: if (0 == (*mdocs[n->tok].fp)(mpage, n))
1389: break;
1390: if (mdocs[n->tok].mask)
1391: putmdockey(mpage, n->child,
1392: mdocs[n->tok].mask);
1393: break;
1394: default:
1395: assert(MDOC_ROOT != n->type);
1396: continue;
1397: }
1398: if (NULL != n->child)
1399: parse_mdoc(mpage, n);
1.1 schwarze 1400: }
1401: }
1402:
1.19 schwarze 1403: static int
1.47 schwarze 1404: parse_mdoc_Fd(struct mpage *mpage, const struct mdoc_node *n)
1.1 schwarze 1405: {
1406: const char *start, *end;
1407: size_t sz;
1.19 schwarze 1408:
1.47 schwarze 1409: if (SEC_SYNOPSIS != n->sec ||
1410: NULL == (n = n->child) ||
1411: MDOC_TEXT != n->type)
1.19 schwarze 1412: return(0);
1.1 schwarze 1413:
1414: /*
1415: * Only consider those `Fd' macro fields that begin with an
1416: * "inclusion" token (versus, e.g., #define).
1417: */
1.47 schwarze 1418:
1.1 schwarze 1419: if (strcmp("#include", n->string))
1.19 schwarze 1420: return(0);
1.1 schwarze 1421:
1422: if (NULL == (n = n->next) || MDOC_TEXT != n->type)
1.19 schwarze 1423: return(0);
1.1 schwarze 1424:
1425: /*
1426: * Strip away the enclosing angle brackets and make sure we're
1427: * not zero-length.
1428: */
1429:
1430: start = n->string;
1431: if ('<' == *start || '"' == *start)
1432: start++;
1433:
1434: if (0 == (sz = strlen(start)))
1.19 schwarze 1435: return(0);
1.1 schwarze 1436:
1437: end = &start[(int)sz - 1];
1438: if ('>' == *end || '"' == *end)
1439: end--;
1440:
1.47 schwarze 1441: if (end > start)
1442: putkeys(mpage, start, end - start + 1, TYPE_In);
1.49 schwarze 1443: return(0);
1.1 schwarze 1444: }
1445:
1.19 schwarze 1446: static int
1.47 schwarze 1447: parse_mdoc_Fn(struct mpage *mpage, const struct mdoc_node *n)
1.1 schwarze 1448: {
1.69 schwarze 1449: char *cp;
1.1 schwarze 1450:
1.47 schwarze 1451: if (NULL == (n = n->child) || MDOC_TEXT != n->type)
1.19 schwarze 1452: return(0);
1453:
1.47 schwarze 1454: /*
1455: * Parse: .Fn "struct type *name" "char *arg".
1456: * First strip away pointer symbol.
1457: * Then store the function name, then type.
1458: * Finally, store the arguments.
1459: */
1.1 schwarze 1460:
1.47 schwarze 1461: if (NULL == (cp = strrchr(n->string, ' ')))
1462: cp = n->string;
1.1 schwarze 1463:
1464: while ('*' == *cp)
1465: cp++;
1466:
1.47 schwarze 1467: putkey(mpage, cp, TYPE_Fn);
1.19 schwarze 1468:
1.47 schwarze 1469: if (n->string < cp)
1470: putkeys(mpage, n->string, cp - n->string, TYPE_Ft);
1.19 schwarze 1471:
1.47 schwarze 1472: for (n = n->next; NULL != n; n = n->next)
1473: if (MDOC_TEXT == n->type)
1474: putkey(mpage, n->string, TYPE_Fa);
1.19 schwarze 1475:
1476: return(0);
1.1 schwarze 1477: }
1478:
1.19 schwarze 1479: static int
1.47 schwarze 1480: parse_mdoc_Xr(struct mpage *mpage, const struct mdoc_node *n)
1.1 schwarze 1481: {
1.47 schwarze 1482: char *cp;
1.1 schwarze 1483:
1484: if (NULL == (n = n->child))
1.19 schwarze 1485: return(0);
1.1 schwarze 1486:
1.47 schwarze 1487: if (NULL == n->next) {
1488: putkey(mpage, n->string, TYPE_Xr);
1489: return(0);
1490: }
1.1 schwarze 1491:
1.77 schwarze 1492: mandoc_asprintf(&cp, "%s(%s)", n->string, n->next->string);
1.47 schwarze 1493: putkey(mpage, cp, TYPE_Xr);
1494: free(cp);
1495: return(0);
1.1 schwarze 1496: }
1497:
1.19 schwarze 1498: static int
1.47 schwarze 1499: parse_mdoc_Nd(struct mpage *mpage, const struct mdoc_node *n)
1.1 schwarze 1500: {
1501:
1.79 schwarze 1502: if (MDOC_BODY == n->type)
1503: mdoc_deroff(&mpage->desc, n);
1504: return(0);
1.1 schwarze 1505: }
1506:
1.19 schwarze 1507: static int
1.47 schwarze 1508: parse_mdoc_Nm(struct mpage *mpage, const struct mdoc_node *n)
1.1 schwarze 1509: {
1510:
1.49 schwarze 1511: return(SEC_NAME == n->sec ||
1512: (SEC_SYNOPSIS == n->sec && MDOC_HEAD == n->type));
1.1 schwarze 1513: }
1514:
1.19 schwarze 1515: static int
1.47 schwarze 1516: parse_mdoc_Sh(struct mpage *mpage, const struct mdoc_node *n)
1.1 schwarze 1517: {
1518:
1.19 schwarze 1519: return(SEC_CUSTOM == n->sec && MDOC_HEAD == n->type);
1.1 schwarze 1520: }
1521:
1.47 schwarze 1522: static int
1523: parse_mdoc_head(struct mpage *mpage, const struct mdoc_node *n)
1.1 schwarze 1524: {
1525:
1.47 schwarze 1526: return(MDOC_HEAD == n->type);
1527: }
1.1 schwarze 1528:
1.47 schwarze 1529: static int
1530: parse_mdoc_body(struct mpage *mpage, const struct mdoc_node *n)
1531: {
1.1 schwarze 1532:
1.47 schwarze 1533: return(MDOC_BODY == n->type);
1.1 schwarze 1534: }
1535:
1.47 schwarze 1536: /*
1537: * Add a string to the hash table for the current manual.
1538: * Each string has a bitmask telling which macros it belongs to.
1539: * When we finish the manual, we'll dump the table.
1540: */
1.1 schwarze 1541: static void
1.47 schwarze 1542: putkeys(const struct mpage *mpage,
1543: const char *cp, size_t sz, uint64_t v)
1.1 schwarze 1544: {
1.47 schwarze 1545: struct str *s;
1.68 schwarze 1546: const char *end;
1547: uint64_t mask;
1.47 schwarze 1548: unsigned int slot;
1.68 schwarze 1549: int i;
1.1 schwarze 1550:
1.47 schwarze 1551: if (0 == sz)
1552: return;
1.68 schwarze 1553:
1554: if (verb > 1) {
1555: for (i = 0, mask = 1;
1556: i < mansearch_keymax;
1557: i++, mask <<= 1)
1558: if (mask & v)
1559: break;
1560: say(mpage->mlinks->file, "Adding key %s=%*s",
1561: mansearch_keynames[i], sz, cp);
1562: }
1.47 schwarze 1563:
1564: end = cp + sz;
1565: slot = ohash_qlookupi(&strings, cp, &end);
1566: s = ohash_find(&strings, slot);
1.1 schwarze 1567:
1.47 schwarze 1568: if (NULL != s && mpage == s->mpage) {
1569: s->mask |= v;
1.1 schwarze 1570: return;
1.47 schwarze 1571: } else if (NULL == s) {
1572: s = mandoc_calloc(sizeof(struct str) + sz + 1, 1);
1573: memcpy(s->key, cp, sz);
1574: ohash_insert(&strings, slot, s);
1575: }
1576: s->mpage = mpage;
1577: s->mask = v;
1.1 schwarze 1578: }
1579:
1580: /*
1.47 schwarze 1581: * Take a Unicode codepoint and produce its UTF-8 encoding.
1582: * This isn't the best way to do this, but it works.
1583: * The magic numbers are from the UTF-8 packaging.
1584: * They're not as scary as they seem: read the UTF-8 spec for details.
1.1 schwarze 1585: */
1.47 schwarze 1586: static size_t
1587: utf8(unsigned int cp, char out[7])
1.1 schwarze 1588: {
1.47 schwarze 1589: size_t rc;
1.1 schwarze 1590:
1.47 schwarze 1591: rc = 0;
1592: if (cp <= 0x0000007F) {
1593: rc = 1;
1594: out[0] = (char)cp;
1595: } else if (cp <= 0x000007FF) {
1596: rc = 2;
1597: out[0] = (cp >> 6 & 31) | 192;
1598: out[1] = (cp & 63) | 128;
1599: } else if (cp <= 0x0000FFFF) {
1600: rc = 3;
1601: out[0] = (cp >> 12 & 15) | 224;
1602: out[1] = (cp >> 6 & 63) | 128;
1603: out[2] = (cp & 63) | 128;
1604: } else if (cp <= 0x001FFFFF) {
1605: rc = 4;
1606: out[0] = (cp >> 18 & 7) | 240;
1607: out[1] = (cp >> 12 & 63) | 128;
1608: out[2] = (cp >> 6 & 63) | 128;
1609: out[3] = (cp & 63) | 128;
1610: } else if (cp <= 0x03FFFFFF) {
1611: rc = 5;
1612: out[0] = (cp >> 24 & 3) | 248;
1613: out[1] = (cp >> 18 & 63) | 128;
1614: out[2] = (cp >> 12 & 63) | 128;
1615: out[3] = (cp >> 6 & 63) | 128;
1616: out[4] = (cp & 63) | 128;
1617: } else if (cp <= 0x7FFFFFFF) {
1618: rc = 6;
1619: out[0] = (cp >> 30 & 1) | 252;
1620: out[1] = (cp >> 24 & 63) | 128;
1621: out[2] = (cp >> 18 & 63) | 128;
1622: out[3] = (cp >> 12 & 63) | 128;
1623: out[4] = (cp >> 6 & 63) | 128;
1624: out[5] = (cp & 63) | 128;
1625: } else
1626: return(0);
1.19 schwarze 1627:
1.47 schwarze 1628: out[rc] = '\0';
1629: return(rc);
1.1 schwarze 1630: }
1631:
1.47 schwarze 1632: /*
1.53 schwarze 1633: * Store the rendered version of a key, or alias the pointer
1634: * if the key contains no escape sequences.
1.47 schwarze 1635: */
1636: static void
1.53 schwarze 1637: render_key(struct mchars *mc, struct str *key)
1.1 schwarze 1638: {
1.47 schwarze 1639: size_t sz, bsz, pos;
1.71 schwarze 1640: char utfbuf[7], res[6];
1.47 schwarze 1641: char *buf;
1642: const char *seq, *cpp, *val;
1643: int len, u;
1644: enum mandoc_esc esc;
1645:
1.53 schwarze 1646: assert(NULL == key->rendered);
1.47 schwarze 1647:
1648: res[0] = '\\';
1649: res[1] = '\t';
1650: res[2] = ASCII_NBRSP;
1651: res[3] = ASCII_HYPH;
1.71 schwarze 1652: res[4] = ASCII_BREAK;
1653: res[5] = '\0';
1.1 schwarze 1654:
1.47 schwarze 1655: val = key->key;
1656: bsz = strlen(val);
1.1 schwarze 1657:
1658: /*
1.47 schwarze 1659: * Pre-check: if we have no stop-characters, then set the
1660: * pointer as ourselvse and get out of here.
1.1 schwarze 1661: */
1.47 schwarze 1662: if (strcspn(val, res) == bsz) {
1.53 schwarze 1663: key->rendered = key->key;
1.47 schwarze 1664: return;
1665: }
1.1 schwarze 1666:
1.47 schwarze 1667: /* Pre-allocate by the length of the input */
1.39 schwarze 1668:
1.47 schwarze 1669: buf = mandoc_malloc(++bsz);
1670: pos = 0;
1.39 schwarze 1671:
1.47 schwarze 1672: while ('\0' != *val) {
1673: /*
1674: * Halt on the first escape sequence.
1675: * This also halts on the end of string, in which case
1676: * we just copy, fallthrough, and exit the loop.
1677: */
1678: if ((sz = strcspn(val, res)) > 0) {
1679: memcpy(&buf[pos], val, sz);
1680: pos += sz;
1681: val += sz;
1682: }
1.39 schwarze 1683:
1.71 schwarze 1684: switch (*val) {
1685: case (ASCII_HYPH):
1.47 schwarze 1686: buf[pos++] = '-';
1687: val++;
1688: continue;
1.71 schwarze 1689: case ('\t'):
1690: /* FALLTHROUGH */
1691: case (ASCII_NBRSP):
1.47 schwarze 1692: buf[pos++] = ' ';
1693: val++;
1.71 schwarze 1694: /* FALLTHROUGH */
1695: case (ASCII_BREAK):
1.47 schwarze 1696: continue;
1.71 schwarze 1697: default:
1698: break;
1699: }
1700: if ('\\' != *val)
1.47 schwarze 1701: break;
1.39 schwarze 1702:
1.47 schwarze 1703: /* Read past the slash. */
1.39 schwarze 1704:
1.47 schwarze 1705: val++;
1.39 schwarze 1706:
1.47 schwarze 1707: /*
1708: * Parse the escape sequence and see if it's a
1709: * predefined character or special character.
1710: */
1.52 schwarze 1711:
1.47 schwarze 1712: esc = mandoc_escape
1713: ((const char **)&val, &seq, &len);
1714: if (ESCAPE_ERROR == esc)
1715: break;
1716: if (ESCAPE_SPECIAL != esc)
1717: continue;
1.39 schwarze 1718:
1.47 schwarze 1719: /*
1.52 schwarze 1720: * Render the special character
1721: * as either UTF-8 or ASCII.
1.47 schwarze 1722: */
1.52 schwarze 1723:
1724: if (write_utf8) {
1725: if (0 == (u = mchars_spec2cp(mc, seq, len)))
1726: continue;
1727: cpp = utfbuf;
1728: if (0 == (sz = utf8(u, utfbuf)))
1729: continue;
1730: sz = strlen(cpp);
1731: } else {
1732: cpp = mchars_spec2str(mc, seq, len, &sz);
1733: if (NULL == cpp)
1734: continue;
1735: if (ASCII_NBRSP == *cpp) {
1736: cpp = " ";
1737: sz = 1;
1738: }
1739: }
1.1 schwarze 1740:
1.47 schwarze 1741: /* Copy the rendered glyph into the stream. */
1.1 schwarze 1742:
1.47 schwarze 1743: bsz += sz;
1744: buf = mandoc_realloc(buf, bsz);
1745: memcpy(&buf[pos], cpp, sz);
1746: pos += sz;
1.1 schwarze 1747: }
1748:
1.47 schwarze 1749: buf[pos] = '\0';
1.53 schwarze 1750: key->rendered = buf;
1.1 schwarze 1751: }
1752:
1.75 schwarze 1753: static void
1754: dbadd_mlink(const struct mlink *mlink)
1755: {
1756: size_t i;
1757:
1758: i = 1;
1759: SQL_BIND_TEXT(stmts[STMT_INSERT_LINK], i, mlink->dsec);
1760: SQL_BIND_TEXT(stmts[STMT_INSERT_LINK], i, mlink->arch);
1761: SQL_BIND_TEXT(stmts[STMT_INSERT_LINK], i, mlink->name);
1762: SQL_BIND_INT64(stmts[STMT_INSERT_LINK], i, mlink->mpage->recno);
1763: SQL_STEP(stmts[STMT_INSERT_LINK]);
1764: sqlite3_reset(stmts[STMT_INSERT_LINK]);
1765: }
1766:
1.11 schwarze 1767: /*
1.47 schwarze 1768: * Flush the current page's terms (and their bits) into the database.
1769: * Wrap the entire set of additions in a transaction to make sqlite be a
1770: * little faster.
1.53 schwarze 1771: * Also, handle escape sequences at the last possible moment.
1.11 schwarze 1772: */
1773: static void
1.75 schwarze 1774: dbadd(struct mpage *mpage, struct mchars *mc)
1.11 schwarze 1775: {
1.47 schwarze 1776: struct mlink *mlink;
1777: struct str *key;
1778: size_t i;
1779: unsigned int slot;
1780:
1781: if (verb)
1.62 schwarze 1782: say(mpage->mlinks->file, "Adding to database");
1.11 schwarze 1783:
1.47 schwarze 1784: if (nodb)
1.11 schwarze 1785: return;
1.47 schwarze 1786:
1787: i = 1;
1788: SQL_BIND_INT(stmts[STMT_INSERT_PAGE], i, FORM_SRC == mpage->form);
1789: SQL_STEP(stmts[STMT_INSERT_PAGE]);
1.75 schwarze 1790: mpage->recno = sqlite3_last_insert_rowid(db);
1.47 schwarze 1791: sqlite3_reset(stmts[STMT_INSERT_PAGE]);
1792:
1.75 schwarze 1793: for (mlink = mpage->mlinks; mlink; mlink = mlink->next)
1794: dbadd_mlink(mlink);
1.47 schwarze 1795:
1796: for (key = ohash_first(&strings, &slot); NULL != key;
1797: key = ohash_next(&strings, &slot)) {
1798: assert(key->mpage == mpage);
1.53 schwarze 1799: if (NULL == key->rendered)
1800: render_key(mc, key);
1.47 schwarze 1801: i = 1;
1802: SQL_BIND_INT64(stmts[STMT_INSERT_KEY], i, key->mask);
1.53 schwarze 1803: SQL_BIND_TEXT(stmts[STMT_INSERT_KEY], i, key->rendered);
1.75 schwarze 1804: SQL_BIND_INT64(stmts[STMT_INSERT_KEY], i, mpage->recno);
1.47 schwarze 1805: SQL_STEP(stmts[STMT_INSERT_KEY]);
1806: sqlite3_reset(stmts[STMT_INSERT_KEY]);
1.53 schwarze 1807: if (key->rendered != key->key)
1808: free(key->rendered);
1.47 schwarze 1809: free(key);
1.33 schwarze 1810: }
1.47 schwarze 1811: }
1.41 deraadt 1812:
1.47 schwarze 1813: static void
1814: dbprune(void)
1815: {
1816: struct mpage *mpage;
1817: struct mlink *mlink;
1818: size_t i;
1819: unsigned int slot;
1.11 schwarze 1820:
1.63 schwarze 1821: if (0 == nodb)
1822: SQL_EXEC("BEGIN TRANSACTION");
1.47 schwarze 1823:
1.63 schwarze 1824: for (mpage = ohash_first(&mpages, &slot); NULL != mpage;
1825: mpage = ohash_next(&mpages, &slot)) {
1.47 schwarze 1826: mlink = mpage->mlinks;
1827: if (verb)
1.63 schwarze 1828: say(mlink->file, "Deleting from database");
1829: if (nodb)
1830: continue;
1831: for ( ; NULL != mlink; mlink = mlink->next) {
1832: i = 1;
1833: SQL_BIND_TEXT(stmts[STMT_DELETE_PAGE],
1834: i, mlink->dsec);
1835: SQL_BIND_TEXT(stmts[STMT_DELETE_PAGE],
1836: i, mlink->arch);
1837: SQL_BIND_TEXT(stmts[STMT_DELETE_PAGE],
1838: i, mlink->name);
1839: SQL_STEP(stmts[STMT_DELETE_PAGE]);
1840: sqlite3_reset(stmts[STMT_DELETE_PAGE]);
1841: }
1.11 schwarze 1842: }
1.63 schwarze 1843:
1844: if (0 == nodb)
1845: SQL_EXEC("END TRANSACTION");
1.47 schwarze 1846: }
1.22 schwarze 1847:
1.47 schwarze 1848: /*
1849: * Close an existing database and its prepared statements.
1850: * If "real" is not set, rename the temporary file into the real one.
1851: */
1852: static void
1853: dbclose(int real)
1854: {
1855: size_t i;
1.72 schwarze 1856: int status;
1857: pid_t child;
1.11 schwarze 1858:
1.47 schwarze 1859: if (nodb)
1860: return;
1.11 schwarze 1861:
1.47 schwarze 1862: for (i = 0; i < STMT__MAX; i++) {
1863: sqlite3_finalize(stmts[i]);
1864: stmts[i] = NULL;
1.28 schwarze 1865: }
1.22 schwarze 1866:
1.47 schwarze 1867: sqlite3_close(db);
1868: db = NULL;
1.11 schwarze 1869:
1.47 schwarze 1870: if (real)
1871: return;
1.22 schwarze 1872:
1.72 schwarze 1873: if ('\0' == *tempfilename) {
1874: if (-1 == rename(MANDOC_DB "~", MANDOC_DB)) {
1875: exitcode = (int)MANDOCLEVEL_SYSERR;
1.80 ! schwarze 1876: say(MANDOC_DB, "&rename");
1.72 schwarze 1877: }
1878: return;
1879: }
1880:
1881: switch (child = fork()) {
1882: case (-1):
1883: exitcode = (int)MANDOCLEVEL_SYSERR;
1.80 ! schwarze 1884: say("", "&fork cmp");
1.72 schwarze 1885: return;
1886: case (0):
1887: execlp("cmp", "cmp", "-s",
1888: tempfilename, MANDOC_DB, NULL);
1.80 ! schwarze 1889: say("", "&exec cmp");
1.72 schwarze 1890: exit(0);
1891: default:
1892: break;
1893: }
1894: if (-1 == waitpid(child, &status, 0)) {
1895: exitcode = (int)MANDOCLEVEL_SYSERR;
1.80 ! schwarze 1896: say("", "&wait cmp");
1.72 schwarze 1897: } else if (WIFSIGNALED(status)) {
1898: exitcode = (int)MANDOCLEVEL_SYSERR;
1.80 ! schwarze 1899: say("", "cmp died from signal %d", WTERMSIG(status));
1.72 schwarze 1900: } else if (WEXITSTATUS(status)) {
1.47 schwarze 1901: exitcode = (int)MANDOCLEVEL_SYSERR;
1.72 schwarze 1902: say(MANDOC_DB,
1903: "Data changed, but cannot replace database");
1904: }
1905:
1906: *strrchr(tempfilename, '/') = '\0';
1907: switch (child = fork()) {
1908: case (-1):
1909: exitcode = (int)MANDOCLEVEL_SYSERR;
1.80 ! schwarze 1910: say("", "&fork rm");
1.72 schwarze 1911: return;
1912: case (0):
1913: execlp("rm", "rm", "-rf", tempfilename, NULL);
1.80 ! schwarze 1914: say("", "&exec rm");
1.72 schwarze 1915: exit((int)MANDOCLEVEL_SYSERR);
1916: default:
1917: break;
1918: }
1919: if (-1 == waitpid(child, &status, 0)) {
1920: exitcode = (int)MANDOCLEVEL_SYSERR;
1.80 ! schwarze 1921: say("", "&wait rm");
1.72 schwarze 1922: } else if (WIFSIGNALED(status) || WEXITSTATUS(status)) {
1923: exitcode = (int)MANDOCLEVEL_SYSERR;
1.80 ! schwarze 1924: say("", "%s: Cannot remove temporary directory",
! 1925: tempfilename);
1.22 schwarze 1926: }
1.11 schwarze 1927: }
1928:
1.47 schwarze 1929: /*
1930: * This is straightforward stuff.
1931: * Open a database connection to a "temporary" database, then open a set
1932: * of prepared statements we'll use over and over again.
1933: * If "real" is set, we use the existing database; if not, we truncate a
1934: * temporary one.
1935: * Must be matched by dbclose().
1936: */
1937: static int
1938: dbopen(int real)
1.2 schwarze 1939: {
1.72 schwarze 1940: const char *sql;
1.47 schwarze 1941: int rc, ofl;
1.6 schwarze 1942:
1.47 schwarze 1943: if (nodb)
1944: return(1);
1.6 schwarze 1945:
1.72 schwarze 1946: *tempfilename = '\0';
1.47 schwarze 1947: ofl = SQLITE_OPEN_READWRITE;
1.72 schwarze 1948:
1949: if (real) {
1950: rc = sqlite3_open_v2(MANDOC_DB, &db, ofl, NULL);
1951: if (SQLITE_OK != rc) {
1.47 schwarze 1952: exitcode = (int)MANDOCLEVEL_SYSERR;
1.72 schwarze 1953: say(MANDOC_DB, "%s", sqlite3_errmsg(db));
1.47 schwarze 1954: return(0);
1.28 schwarze 1955: }
1.72 schwarze 1956: goto prepare_statements;
1957: }
1958:
1959: ofl |= SQLITE_OPEN_CREATE | SQLITE_OPEN_EXCLUSIVE;
1.6 schwarze 1960:
1.72 schwarze 1961: remove(MANDOC_DB "~");
1962: rc = sqlite3_open_v2(MANDOC_DB "~", &db, ofl, NULL);
1.47 schwarze 1963: if (SQLITE_OK == rc)
1.72 schwarze 1964: goto create_tables;
1.73 schwarze 1965: if (MPARSE_QUICK & mparse_options) {
1.47 schwarze 1966: exitcode = (int)MANDOCLEVEL_SYSERR;
1.72 schwarze 1967: say(MANDOC_DB "~", "%s", sqlite3_errmsg(db));
1.47 schwarze 1968: return(0);
1969: }
1.6 schwarze 1970:
1.72 schwarze 1971: if (strlcpy(tempfilename, "/tmp/mandocdb.XXXXXX",
1972: sizeof(tempfilename)) >= sizeof(tempfilename)) {
1973: exitcode = (int)MANDOCLEVEL_SYSERR;
1.80 ! schwarze 1974: say("", "/tmp/mandocdb.XXXXXX: Filename too long");
1.72 schwarze 1975: return(0);
1976: }
1977: if (NULL == mkdtemp(tempfilename)) {
1978: exitcode = (int)MANDOCLEVEL_SYSERR;
1.80 ! schwarze 1979: say("", "&%s", tempfilename);
1.72 schwarze 1980: return(0);
1981: }
1982: if (strlcat(tempfilename, "/" MANDOC_DB,
1983: sizeof(tempfilename)) >= sizeof(tempfilename)) {
1984: exitcode = (int)MANDOCLEVEL_SYSERR;
1.80 ! schwarze 1985: say("", "%s/" MANDOC_DB ": Filename too long",
! 1986: tempfilename);
1.72 schwarze 1987: return(0);
1988: }
1989: rc = sqlite3_open_v2(tempfilename, &db, ofl, NULL);
1990: if (SQLITE_OK != rc) {
1.47 schwarze 1991: exitcode = (int)MANDOCLEVEL_SYSERR;
1.80 ! schwarze 1992: say("", "%s: %s", tempfilename, sqlite3_errmsg(db));
1.47 schwarze 1993: return(0);
1.2 schwarze 1994: }
1995:
1.72 schwarze 1996: create_tables:
1.47 schwarze 1997: sql = "CREATE TABLE \"mpages\" (\n"
1998: " \"form\" INTEGER NOT NULL,\n"
1999: " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
2000: ");\n"
2001: "\n"
2002: "CREATE TABLE \"mlinks\" (\n"
2003: " \"sec\" TEXT NOT NULL,\n"
2004: " \"arch\" TEXT NOT NULL,\n"
2005: " \"name\" TEXT NOT NULL,\n"
2006: " \"pageid\" INTEGER NOT NULL REFERENCES mpages(id) "
1.66 schwarze 2007: "ON DELETE CASCADE\n"
1.47 schwarze 2008: ");\n"
2009: "\n"
2010: "CREATE TABLE \"keys\" (\n"
2011: " \"bits\" INTEGER NOT NULL,\n"
2012: " \"key\" TEXT NOT NULL,\n"
2013: " \"pageid\" INTEGER NOT NULL REFERENCES mpages(id) "
1.66 schwarze 2014: "ON DELETE CASCADE\n"
1.65 schwarze 2015: ");\n";
1.47 schwarze 2016:
2017: if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, NULL)) {
2018: exitcode = (int)MANDOCLEVEL_SYSERR;
1.72 schwarze 2019: say(MANDOC_DB, "%s", sqlite3_errmsg(db));
1.47 schwarze 2020: return(0);
1.2 schwarze 2021: }
2022:
1.47 schwarze 2023: prepare_statements:
2024: SQL_EXEC("PRAGMA foreign_keys = ON");
1.63 schwarze 2025: sql = "DELETE FROM mpages WHERE id IN "
2026: "(SELECT pageid FROM mlinks WHERE "
2027: "sec=? AND arch=? AND name=?)";
1.47 schwarze 2028: sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_DELETE_PAGE], NULL);
2029: sql = "INSERT INTO mpages "
1.60 schwarze 2030: "(form) VALUES (?)";
1.47 schwarze 2031: sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_PAGE], NULL);
2032: sql = "INSERT INTO mlinks "
1.61 schwarze 2033: "(sec,arch,name,pageid) VALUES (?,?,?,?)";
1.47 schwarze 2034: sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_LINK], NULL);
2035: sql = "INSERT INTO keys "
2036: "(bits,key,pageid) VALUES (?,?,?)";
2037: sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_KEY], NULL);
1.6 schwarze 2038:
1.47 schwarze 2039: /*
2040: * When opening a new database, we can turn off
2041: * synchronous mode for much better performance.
2042: */
1.6 schwarze 2043:
1.47 schwarze 2044: if (real)
2045: SQL_EXEC("PRAGMA synchronous = OFF");
1.11 schwarze 2046:
1.47 schwarze 2047: return(1);
2048: }
1.6 schwarze 2049:
1.47 schwarze 2050: static void *
2051: hash_halloc(size_t sz, void *arg)
2052: {
1.6 schwarze 2053:
1.47 schwarze 2054: return(mandoc_calloc(sz, 1));
2055: }
1.2 schwarze 2056:
1.47 schwarze 2057: static void *
2058: hash_alloc(size_t sz, void *arg)
2059: {
1.28 schwarze 2060:
1.47 schwarze 2061: return(mandoc_malloc(sz));
2062: }
1.6 schwarze 2063:
1.47 schwarze 2064: static void
2065: hash_free(void *p, size_t sz, void *arg)
2066: {
1.26 schwarze 2067:
1.47 schwarze 2068: free(p);
2069: }
1.6 schwarze 2070:
1.47 schwarze 2071: static int
2072: set_basedir(const char *targetdir)
2073: {
2074: static char startdir[PATH_MAX];
2075: static int fd;
1.6 schwarze 2076:
1.47 schwarze 2077: /*
2078: * Remember where we started by keeping a fd open to the origin
2079: * path component: throughout this utility, we chdir() a lot to
2080: * handle relative paths, and by doing this, we can return to
2081: * the starting point.
2082: */
2083: if ('\0' == *startdir) {
2084: if (NULL == getcwd(startdir, PATH_MAX)) {
2085: exitcode = (int)MANDOCLEVEL_SYSERR;
2086: if (NULL != targetdir)
1.80 ! schwarze 2087: say("", "&getcwd");
1.47 schwarze 2088: return(0);
2089: }
2090: if (-1 == (fd = open(startdir, O_RDONLY, 0))) {
2091: exitcode = (int)MANDOCLEVEL_SYSERR;
1.80 ! schwarze 2092: say("", "&open %s", startdir);
1.47 schwarze 2093: return(0);
1.11 schwarze 2094: }
1.47 schwarze 2095: if (NULL == targetdir)
2096: targetdir = startdir;
2097: } else {
2098: if (-1 == fd)
2099: return(0);
2100: if (-1 == fchdir(fd)) {
2101: close(fd);
2102: basedir[0] = '\0';
2103: exitcode = (int)MANDOCLEVEL_SYSERR;
1.80 ! schwarze 2104: say("", "&chdir %s", startdir);
1.47 schwarze 2105: return(0);
1.2 schwarze 2106: }
1.47 schwarze 2107: if (NULL == targetdir) {
2108: close(fd);
2109: return(1);
1.2 schwarze 2110: }
2111: }
1.47 schwarze 2112: if (NULL == realpath(targetdir, basedir)) {
2113: basedir[0] = '\0';
2114: exitcode = (int)MANDOCLEVEL_BADARG;
1.80 ! schwarze 2115: say("", "&%s: realpath", targetdir);
1.47 schwarze 2116: return(0);
2117: } else if (-1 == chdir(basedir)) {
2118: exitcode = (int)MANDOCLEVEL_BADARG;
1.80 ! schwarze 2119: say("", "&chdir");
1.47 schwarze 2120: return(0);
2121: }
2122: return(1);
1.2 schwarze 2123: }
2124:
2125: static void
1.47 schwarze 2126: say(const char *file, const char *format, ...)
1.2 schwarze 2127: {
1.47 schwarze 2128: va_list ap;
1.80 ! schwarze 2129: int use_errno;
1.2 schwarze 2130:
1.47 schwarze 2131: if ('\0' != *basedir)
2132: fprintf(stderr, "%s", basedir);
2133: if ('\0' != *basedir && '\0' != *file)
2134: fputs("//", stderr);
2135: if ('\0' != *file)
2136: fprintf(stderr, "%s", file);
1.31 schwarze 2137:
1.80 ! schwarze 2138: use_errno = 1;
! 2139: if (NULL != format) {
! 2140: switch (*format) {
! 2141: case ('&'):
! 2142: format++;
! 2143: break;
! 2144: case ('\0'):
! 2145: format = NULL;
! 2146: break;
! 2147: default:
! 2148: use_errno = 0;
! 2149: break;
! 2150: }
! 2151: }
! 2152: if (NULL != format) {
! 2153: if ('\0' != *basedir || '\0' != *file)
! 2154: fputs(": ", stderr);
! 2155: va_start(ap, format);
! 2156: vfprintf(stderr, format, ap);
! 2157: va_end(ap);
! 2158: }
! 2159: if (use_errno) {
! 2160: if ('\0' != *basedir || '\0' != *file || NULL != format)
! 2161: fputs(": ", stderr);
1.47 schwarze 2162: perror(NULL);
1.80 ! schwarze 2163: } else
! 2164: fputc('\n', stderr);
1.1 schwarze 2165: }