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