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