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