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