Annotation of mandoc/mandocdb.c, Revision 1.185
1.185 ! schwarze 1: /* $Id: mandocdb.c,v 1.184 2015/01/21 22:41:49 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.183 schwarze 445: mp = mparse_alloc(mparse_options, MANDOCLEVEL_BADARG, 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: }
1.184 schwarze 616: if (strstr(buf, basedir) != buf
617: #ifdef HOMEBREWDIR
618: && strstr(buf, HOMEBREWDIR) != buf
619: #endif
620: ) {
1.139 schwarze 621: if (warnings) say("",
622: "%s: outside base directory", buf);
623: continue;
624: }
625: /* Use logical inode to avoid mpages dupe. */
626: if (-1 == stat(path, ff->fts_statp)) {
627: if (warnings)
628: say(path, "&stat");
629: continue;
630: }
631: /* FALLTHROUGH */
632:
1.50 kristaps 633: /*
1.83 schwarze 634: * If we're a regular file, add an mlink by using the
1.50 kristaps 635: * stored directory data and handling the filename.
636: */
1.141 schwarze 637: case FTS_F:
1.60 schwarze 638: if (0 == strcmp(path, MANDOC_DB))
639: continue;
1.50 kristaps 640: if ( ! use_all && ff->fts_level < 2) {
1.56 schwarze 641: if (warnings)
1.59 schwarze 642: say(path, "Extraneous file");
1.50 kristaps 643: continue;
1.124 schwarze 644: }
645: gzip = 0;
646: fsec = NULL;
647: while (NULL == fsec) {
648: fsec = strrchr(ff->fts_name, '.');
649: if (NULL == fsec || strcmp(fsec+1, "gz"))
650: break;
651: gzip = 1;
652: *fsec = '\0';
653: fsec = NULL;
654: }
655: if (NULL == fsec) {
1.60 schwarze 656: if ( ! use_all) {
1.56 schwarze 657: if (warnings)
1.60 schwarze 658: say(path,
659: "No filename suffix");
1.50 kristaps 660: continue;
661: }
1.84 schwarze 662: } else if (0 == strcmp(++fsec, "html")) {
1.60 schwarze 663: if (warnings)
664: say(path, "Skip html");
665: continue;
1.84 schwarze 666: } else if (0 == strcmp(fsec, "ps")) {
1.60 schwarze 667: if (warnings)
668: say(path, "Skip ps");
669: continue;
1.84 schwarze 670: } else if (0 == strcmp(fsec, "pdf")) {
1.60 schwarze 671: if (warnings)
672: say(path, "Skip pdf");
673: continue;
674: } else if ( ! use_all &&
1.185 ! schwarze 675: ((FORM_SRC == dform &&
! 676: strncmp(fsec, dsec, strlen(dsec))) ||
1.84 schwarze 677: (FORM_CAT == dform && strcmp(fsec, "0")))) {
1.60 schwarze 678: if (warnings)
679: say(path, "Wrong filename suffix");
680: continue;
1.66 schwarze 681: } else
1.84 schwarze 682: fsec[-1] = '\0';
1.94 schwarze 683:
1.84 schwarze 684: mlink = mandoc_calloc(1, sizeof(struct mlink));
1.143 schwarze 685: if (strlcpy(mlink->file, path,
686: sizeof(mlink->file)) >=
687: sizeof(mlink->file)) {
688: say(path, "Filename too long");
689: free(mlink);
690: continue;
691: }
1.84 schwarze 692: mlink->dform = dform;
1.94 schwarze 693: mlink->dsec = dsec;
694: mlink->arch = arch;
695: mlink->name = ff->fts_name;
696: mlink->fsec = fsec;
1.124 schwarze 697: mlink->gzip = gzip;
1.84 schwarze 698: mlink_add(mlink, ff->fts_statp);
1.50 kristaps 699: continue;
1.139 schwarze 700:
1.141 schwarze 701: case FTS_D:
1.139 schwarze 702: /* FALLTHROUGH */
1.141 schwarze 703: case FTS_DP:
1.139 schwarze 704: break;
705:
706: default:
1.60 schwarze 707: if (warnings)
708: say(path, "Not a regular file");
1.50 kristaps 709: continue;
1.60 schwarze 710: }
1.1 kristaps 711:
1.50 kristaps 712: switch (ff->fts_level) {
1.141 schwarze 713: case 0:
1.50 kristaps 714: /* Ignore the root directory. */
715: break;
1.141 schwarze 716: case 1:
1.50 kristaps 717: /*
718: * This might contain manX/ or catX/.
719: * Try to infer this from the name.
720: * If we're not in use_all, enforce it.
721: */
722: cp = ff->fts_name;
723: if (FTS_DP == ff->fts_info)
724: break;
1.1 kristaps 725:
1.50 kristaps 726: if (0 == strncmp(cp, "man", 3)) {
727: dform = FORM_SRC;
1.66 schwarze 728: dsec = cp + 3;
1.50 kristaps 729: } else if (0 == strncmp(cp, "cat", 3)) {
730: dform = FORM_CAT;
1.66 schwarze 731: dsec = cp + 3;
1.94 schwarze 732: } else {
733: dform = FORM_NONE;
734: dsec = NULL;
1.50 kristaps 735: }
1.1 kristaps 736:
1.141 schwarze 737: if (NULL != dsec || use_all)
1.50 kristaps 738: break;
1.1 kristaps 739:
1.56 schwarze 740: if (warnings)
1.59 schwarze 741: say(path, "Unknown directory part");
1.50 kristaps 742: fts_set(f, ff, FTS_SKIP);
743: break;
1.141 schwarze 744: case 2:
1.50 kristaps 745: /*
746: * Possibly our architecture.
747: * If we're descending, keep tabs on it.
748: */
749: if (FTS_DP != ff->fts_info && NULL != dsec)
1.66 schwarze 750: arch = ff->fts_name;
1.94 schwarze 751: else
752: arch = NULL;
1.50 kristaps 753: break;
754: default:
755: if (FTS_DP == ff->fts_info || use_all)
756: break;
1.56 schwarze 757: if (warnings)
1.59 schwarze 758: say(path, "Extraneous directory part");
1.50 kristaps 759: fts_set(f, ff, FTS_SKIP);
760: break;
1.5 kristaps 761: }
1.50 kristaps 762: }
763:
764: fts_close(f);
765: return(1);
766: }
1.5 kristaps 767:
1.50 kristaps 768: /*
1.89 schwarze 769: * Add a file to the mlinks table.
1.50 kristaps 770: * Do not verify that it's a "valid" looking manpage (we'll do that
771: * later).
772: *
773: * Try to infer the manual section, architecture, and page name from the
774: * path, assuming it looks like
775: *
1.141 schwarze 776: * [./]man*[/<arch>]/<name>.<section>
1.50 kristaps 777: * or
778: * [./]cat<section>[/<arch>]/<name>.0
779: *
780: * See treescan() for the fts(3) version of this.
781: */
782: static void
1.59 schwarze 783: filescan(const char *file)
1.50 kristaps 784: {
1.59 schwarze 785: char buf[PATH_MAX];
1.84 schwarze 786: struct stat st;
787: struct mlink *mlink;
1.59 schwarze 788: char *p, *start;
1.5 kristaps 789:
1.50 kristaps 790: assert(use_all);
1.5 kristaps 791:
1.50 kristaps 792: if (0 == strncmp(file, "./", 2))
793: file += 2;
1.5 kristaps 794:
1.139 schwarze 795: /*
796: * We have to do lstat(2) before realpath(3) loses
797: * the information whether this is a symbolic link.
798: * We need to know that because for symbolic links,
799: * we want to use the orginal file name, while for
800: * regular files, we want to use the real path.
801: */
802: if (-1 == lstat(file, &st)) {
803: exitcode = (int)MANDOCLEVEL_BADARG;
804: say(file, "&lstat");
805: return;
806: } else if (0 == ((S_IFREG | S_IFLNK) & st.st_mode)) {
807: exitcode = (int)MANDOCLEVEL_BADARG;
808: say(file, "Not a regular file");
809: return;
810: }
811:
812: /*
813: * We have to resolve the file name to the real path
814: * in any case for the base directory check.
815: */
1.59 schwarze 816: if (NULL == realpath(file, buf)) {
817: exitcode = (int)MANDOCLEVEL_BADARG;
1.123 schwarze 818: say(file, "&realpath");
1.59 schwarze 819: return;
1.106 schwarze 820: }
821:
1.150 schwarze 822: if (OP_TEST == op)
1.106 schwarze 823: start = buf;
1.150 schwarze 824: else if (strstr(buf, basedir) == buf)
825: start = buf + strlen(basedir);
1.184 schwarze 826: #ifdef HOMEBREWDIR
827: else if (strstr(buf, HOMEBREWDIR) == buf)
828: start = buf;
829: #endif
1.106 schwarze 830: else {
1.59 schwarze 831: exitcode = (int)MANDOCLEVEL_BADARG;
832: say("", "%s: outside base directory", buf);
833: return;
1.106 schwarze 834: }
835:
1.139 schwarze 836: /*
837: * Now we are sure the file is inside our tree.
838: * If it is a symbolic link, ignore the real path
839: * and use the original name.
840: * This implies passing stuff like "cat1/../man1/foo.1"
841: * on the command line won't work. So don't do that.
842: * Note the stat(2) can still fail if the link target
843: * doesn't exist.
844: */
845: if (S_IFLNK & st.st_mode) {
846: if (-1 == stat(buf, &st)) {
847: exitcode = (int)MANDOCLEVEL_BADARG;
848: say(file, "&stat");
849: return;
850: }
1.143 schwarze 851: if (strlcpy(buf, file, sizeof(buf)) >= sizeof(buf)) {
852: say(file, "Filename too long");
853: return;
854: }
1.150 schwarze 855: start = buf;
856: if (OP_TEST != op && strstr(buf, basedir) == buf)
857: start += strlen(basedir);
1.50 kristaps 858: }
1.106 schwarze 859:
1.84 schwarze 860: mlink = mandoc_calloc(1, sizeof(struct mlink));
1.161 schwarze 861: mlink->dform = FORM_NONE;
1.143 schwarze 862: if (strlcpy(mlink->file, start, sizeof(mlink->file)) >=
863: sizeof(mlink->file)) {
864: say(start, "Filename too long");
1.180 schwarze 865: free(mlink);
1.143 schwarze 866: return;
867: }
1.17 schwarze 868:
1.50 kristaps 869: /*
870: * First try to guess our directory structure.
871: * If we find a separator, try to look for man* or cat*.
872: * If we find one of these and what's underneath is a directory,
873: * assume it's an architecture.
874: */
875: if (NULL != (p = strchr(start, '/'))) {
876: *p++ = '\0';
877: if (0 == strncmp(start, "man", 3)) {
1.84 schwarze 878: mlink->dform = FORM_SRC;
1.94 schwarze 879: mlink->dsec = start + 3;
1.50 kristaps 880: } else if (0 == strncmp(start, "cat", 3)) {
1.84 schwarze 881: mlink->dform = FORM_CAT;
1.94 schwarze 882: mlink->dsec = start + 3;
1.17 schwarze 883: }
1.5 kristaps 884:
1.50 kristaps 885: start = p;
1.84 schwarze 886: if (NULL != mlink->dsec && NULL != (p = strchr(start, '/'))) {
1.50 kristaps 887: *p++ = '\0';
1.94 schwarze 888: mlink->arch = start;
1.50 kristaps 889: start = p;
1.84 schwarze 890: }
1.50 kristaps 891: }
892:
893: /*
894: * Now check the file suffix.
895: * Suffix of `.0' indicates a catpage, `.1-9' is a manpage.
896: */
897: p = strrchr(start, '\0');
898: while (p-- > start && '/' != *p && '.' != *p)
899: /* Loop. */ ;
900:
901: if ('.' == *p) {
902: *p++ = '\0';
1.94 schwarze 903: mlink->fsec = p;
1.5 kristaps 904: }
905:
1.10 kristaps 906: /*
1.50 kristaps 907: * Now try to parse the name.
908: * Use the filename portion of the path.
1.10 kristaps 909: */
1.84 schwarze 910: mlink->name = start;
1.50 kristaps 911: if (NULL != (p = strrchr(start, '/'))) {
1.84 schwarze 912: mlink->name = p + 1;
1.50 kristaps 913: *p = '\0';
1.84 schwarze 914: }
915: mlink_add(mlink, &st);
1.50 kristaps 916: }
1.5 kristaps 917:
1.50 kristaps 918: static void
1.84 schwarze 919: mlink_add(struct mlink *mlink, const struct stat *st)
1.50 kristaps 920: {
1.83 schwarze 921: struct inodev inodev;
1.78 schwarze 922: struct mpage *mpage;
1.83 schwarze 923: unsigned int slot;
1.50 kristaps 924:
1.84 schwarze 925: assert(NULL != mlink->file);
1.50 kristaps 926:
1.94 schwarze 927: mlink->dsec = mandoc_strdup(mlink->dsec ? mlink->dsec : "");
928: mlink->arch = mandoc_strdup(mlink->arch ? mlink->arch : "");
929: mlink->name = mandoc_strdup(mlink->name ? mlink->name : "");
930: mlink->fsec = mandoc_strdup(mlink->fsec ? mlink->fsec : "");
1.84 schwarze 931:
932: if ('0' == *mlink->fsec) {
933: free(mlink->fsec);
934: mlink->fsec = mandoc_strdup(mlink->dsec);
935: mlink->fform = FORM_CAT;
936: } else if ('1' <= *mlink->fsec && '9' >= *mlink->fsec)
937: mlink->fform = FORM_SRC;
1.74 schwarze 938: else
1.84 schwarze 939: mlink->fform = FORM_NONE;
1.82 schwarze 940:
1.83 schwarze 941: slot = ohash_qlookup(&mlinks, mlink->file);
942: assert(NULL == ohash_find(&mlinks, slot));
943: ohash_insert(&mlinks, slot, mlink);
944:
945: inodev.st_ino = st->st_ino;
946: inodev.st_dev = st->st_dev;
947: slot = ohash_lookup_memory(&mpages, (char *)&inodev,
948: sizeof(struct inodev), inodev.st_ino);
949: mpage = ohash_find(&mpages, slot);
950: if (NULL == mpage) {
951: mpage = mandoc_calloc(1, sizeof(struct mpage));
952: mpage->inodev.st_ino = inodev.st_ino;
953: mpage->inodev.st_dev = inodev.st_dev;
954: ohash_insert(&mpages, slot, mpage);
955: } else
1.85 schwarze 956: mlink->next = mpage->mlinks;
1.82 schwarze 957: mpage->mlinks = mlink;
1.118 schwarze 958: mlink->mpage = mpage;
1.82 schwarze 959: }
960:
961: static void
962: mlink_free(struct mlink *mlink)
963: {
964:
965: free(mlink->dsec);
966: free(mlink->arch);
967: free(mlink->name);
968: free(mlink->fsec);
969: free(mlink);
1.50 kristaps 970: }
1.3 kristaps 971:
1.50 kristaps 972: static void
1.78 schwarze 973: mpages_free(void)
1.50 kristaps 974: {
1.78 schwarze 975: struct mpage *mpage;
1.82 schwarze 976: struct mlink *mlink;
1.80 schwarze 977: unsigned int slot;
1.3 kristaps 978:
1.80 schwarze 979: mpage = ohash_first(&mpages, &slot);
980: while (NULL != mpage) {
1.82 schwarze 981: while (NULL != (mlink = mpage->mlinks)) {
1.85 schwarze 982: mpage->mlinks = mlink->next;
1.82 schwarze 983: mlink_free(mlink);
984: }
1.78 schwarze 985: free(mpage->sec);
986: free(mpage->arch);
1.82 schwarze 987: free(mpage->title);
988: free(mpage->desc);
1.78 schwarze 989: free(mpage);
1.80 schwarze 990: mpage = ohash_next(&mpages, &slot);
1.50 kristaps 991: }
992: }
1.38 schwarze 993:
1.50 kristaps 994: /*
1.89 schwarze 995: * For each mlink to the mpage, check whether the path looks like
996: * it is formatted, and if it does, check whether a source manual
997: * exists by the same name, ignoring the suffix.
998: * If both conditions hold, drop the mlink.
999: */
1000: static void
1001: mlinks_undupe(struct mpage *mpage)
1002: {
1003: char buf[PATH_MAX];
1004: struct mlink **prev;
1005: struct mlink *mlink;
1006: char *bufp;
1007:
1008: mpage->form = FORM_CAT;
1.90 schwarze 1009: prev = &mpage->mlinks;
1010: while (NULL != (mlink = *prev)) {
1.89 schwarze 1011: if (FORM_CAT != mlink->dform) {
1012: mpage->form = FORM_NONE;
1.90 schwarze 1013: goto nextlink;
1.89 schwarze 1014: }
1.143 schwarze 1015: (void)strlcpy(buf, mlink->file, sizeof(buf));
1.89 schwarze 1016: bufp = strstr(buf, "cat");
1017: assert(NULL != bufp);
1018: memcpy(bufp, "man", 3);
1019: if (NULL != (bufp = strrchr(buf, '.')))
1020: *++bufp = '\0';
1.143 schwarze 1021: (void)strlcat(buf, mlink->dsec, sizeof(buf));
1.89 schwarze 1022: if (NULL == ohash_find(&mlinks,
1.141 schwarze 1023: ohash_qlookup(&mlinks, buf)))
1.90 schwarze 1024: goto nextlink;
1.89 schwarze 1025: if (warnings)
1026: say(mlink->file, "Man source exists: %s", buf);
1027: if (use_all)
1.90 schwarze 1028: goto nextlink;
1.89 schwarze 1029: *prev = mlink->next;
1030: mlink_free(mlink);
1.90 schwarze 1031: continue;
1032: nextlink:
1033: prev = &(*prev)->next;
1.89 schwarze 1034: }
1035: }
1036:
1.131 schwarze 1037: static void
1.93 schwarze 1038: mlink_check(struct mpage *mpage, struct mlink *mlink)
1039: {
1.131 schwarze 1040: struct str *str;
1041: unsigned int slot;
1.93 schwarze 1042:
1043: /*
1044: * Check whether the manual section given in a file
1045: * agrees with the directory where the file is located.
1046: * Some manuals have suffixes like (3p) on their
1047: * section number either inside the file or in the
1048: * directory name, some are linked into more than one
1049: * section, like encrypt(1) = makekey(8).
1050: */
1051:
1052: if (FORM_SRC == mpage->form &&
1.131 schwarze 1053: strcasecmp(mpage->sec, mlink->dsec))
1.93 schwarze 1054: say(mlink->file, "Section \"%s\" manual in %s directory",
1055: mpage->sec, mlink->dsec);
1056:
1057: /*
1058: * Manual page directories exist for each kernel
1059: * architecture as returned by machine(1).
1060: * However, many manuals only depend on the
1061: * application architecture as returned by arch(1).
1062: * For example, some (2/ARM) manuals are shared
1063: * across the "armish" and "zaurus" kernel
1064: * architectures.
1065: * A few manuals are even shared across completely
1066: * different architectures, for example fdformat(1)
1067: * on amd64, i386, sparc, and sparc64.
1068: */
1069:
1.131 schwarze 1070: if (strcasecmp(mpage->arch, mlink->arch))
1.93 schwarze 1071: say(mlink->file, "Architecture \"%s\" manual in "
1072: "\"%s\" directory", mpage->arch, mlink->arch);
1073:
1.131 schwarze 1074: /*
1075: * XXX
1.133 schwarze 1076: * parse_cat() doesn't set NAME_TITLE yet.
1.131 schwarze 1077: */
1078:
1079: if (FORM_CAT == mpage->form)
1080: return;
1081:
1082: /*
1083: * Check whether this mlink
1084: * appears as a name in the NAME section.
1085: */
1.93 schwarze 1086:
1.133 schwarze 1087: slot = ohash_qlookup(&names, mlink->name);
1088: str = ohash_find(&names, slot);
1.131 schwarze 1089: assert(NULL != str);
1.133 schwarze 1090: if ( ! (NAME_TITLE & str->mask))
1.131 schwarze 1091: say(mlink->file, "Name missing in NAME section");
1.93 schwarze 1092: }
1093:
1.89 schwarze 1094: /*
1.80 schwarze 1095: * Run through the files in the global vector "mpages"
1096: * and add them to the database specified in "basedir".
1.50 kristaps 1097: *
1098: * This handles the parsing scheme itself, using the cues of directory
1099: * and filename to determine whether the file is parsable or not.
1100: */
1.59 schwarze 1101: static void
1.176 schwarze 1102: mpages_merge(struct mparse *mp)
1.50 kristaps 1103: {
1.113 schwarze 1104: char any[] = "any";
1.101 schwarze 1105: struct ohash_info str_info;
1.118 schwarze 1106: struct mpage *mpage, *mpage_dest;
1107: struct mlink *mlink, *mlink_dest;
1.69 schwarze 1108: struct mdoc *mdoc;
1109: struct man *man;
1.118 schwarze 1110: char *sodest;
1.112 schwarze 1111: char *cp;
1.162 schwarze 1112: int fd;
1.101 schwarze 1113: unsigned int pslot;
1.69 schwarze 1114:
1.71 schwarze 1115: str_info.alloc = hash_alloc;
1.152 schwarze 1116: str_info.calloc = hash_calloc;
1117: str_info.free = hash_free;
1.181 schwarze 1118: str_info.data = NULL;
1.71 schwarze 1119: str_info.key_offset = offsetof(struct str, key);
1120:
1.171 schwarze 1121: if ( ! nodb)
1.107 schwarze 1122: SQL_EXEC("BEGIN TRANSACTION");
1123:
1.80 schwarze 1124: mpage = ohash_first(&mpages, &pslot);
1.171 schwarze 1125: while (mpage != NULL) {
1.89 schwarze 1126: mlinks_undupe(mpage);
1.182 schwarze 1127: if ((mlink = mpage->mlinks) == NULL) {
1.89 schwarze 1128: mpage = ohash_next(&mpages, &pslot);
1129: continue;
1.50 kristaps 1130: }
1131:
1.133 schwarze 1132: name_mask = NAME_MASK;
1133: ohash_init(&names, 4, &str_info);
1.71 schwarze 1134: ohash_init(&strings, 6, &str_info);
1.50 kristaps 1135: mparse_reset(mp);
1136: mdoc = NULL;
1137: man = NULL;
1.124 schwarze 1138: sodest = NULL;
1139:
1.182 schwarze 1140: mparse_open(mp, &fd, mlink->file);
1.162 schwarze 1141: if (fd == -1) {
1.182 schwarze 1142: say(mlink->file, "&open");
1.162 schwarze 1143: goto nextpage;
1.124 schwarze 1144: }
1.14 schwarze 1145:
1146: /*
1.183 schwarze 1147: * Interpret the file as mdoc(7) or man(7) source
1148: * code, unless it is known to be formatted.
1.14 schwarze 1149: */
1.182 schwarze 1150: if (mlink->dform != FORM_CAT || mlink->fform != FORM_CAT) {
1.183 schwarze 1151: mparse_readfd(mp, fd, mlink->file);
1152: mparse_result(mp, &mdoc, &man, &sodest);
1.76 schwarze 1153: }
1.14 schwarze 1154:
1.171 schwarze 1155: if (sodest != NULL) {
1.118 schwarze 1156: mlink_dest = ohash_find(&mlinks,
1157: ohash_qlookup(&mlinks, sodest));
1.171 schwarze 1158: if (mlink_dest == NULL) {
1159: mandoc_asprintf(&cp, "%s.gz", sodest);
1160: mlink_dest = ohash_find(&mlinks,
1161: ohash_qlookup(&mlinks, cp));
1162: free(cp);
1163: }
1164: if (mlink_dest != NULL) {
1.118 schwarze 1165:
1166: /* The .so target exists. */
1167:
1168: mpage_dest = mlink_dest->mpage;
1169: while (1) {
1170: mlink->mpage = mpage_dest;
1171:
1172: /*
1173: * If the target was already
1174: * processed, add the links
1175: * to the database now.
1176: * Otherwise, this will
1177: * happen when we come
1178: * to the target.
1179: */
1180:
1.137 schwarze 1181: if (mpage_dest->pageid)
1.169 schwarze 1182: dbadd_mlink_name(mlink);
1.118 schwarze 1183:
1.171 schwarze 1184: if (mlink->next == NULL)
1.118 schwarze 1185: break;
1186: mlink = mlink->next;
1187: }
1188:
1189: /* Move all links to the target. */
1190:
1191: mlink->next = mlink_dest->next;
1192: mlink_dest->next = mpage->mlinks;
1193: mpage->mlinks = NULL;
1194: }
1.124 schwarze 1195: goto nextpage;
1.171 schwarze 1196: } else if (mdoc != NULL) {
1.82 schwarze 1197: mpage->form = FORM_SRC;
1.155 schwarze 1198: mpage->sec = mdoc_meta(mdoc)->msec;
1199: mpage->sec = mandoc_strdup(
1.171 schwarze 1200: mpage->sec == NULL ? "" : mpage->sec);
1.82 schwarze 1201: mpage->arch = mdoc_meta(mdoc)->arch;
1.84 schwarze 1202: mpage->arch = mandoc_strdup(
1.171 schwarze 1203: mpage->arch == NULL ? "" : mpage->arch);
1.82 schwarze 1204: mpage->title =
1205: mandoc_strdup(mdoc_meta(mdoc)->title);
1.171 schwarze 1206: } else if (man != NULL) {
1.82 schwarze 1207: mpage->form = FORM_SRC;
1.182 schwarze 1208: mpage->sec = mandoc_strdup(man_meta(man)->msec);
1209: mpage->arch = mandoc_strdup(mlink->arch);
1210: mpage->title = mandoc_strdup(man_meta(man)->title);
1.76 schwarze 1211: } else {
1.82 schwarze 1212: mpage->form = FORM_CAT;
1.182 schwarze 1213: mpage->sec = mandoc_strdup(mlink->dsec);
1214: mpage->arch = mandoc_strdup(mlink->arch);
1215: mpage->title = mandoc_strdup(mlink->name);
1.76 schwarze 1216: }
1.97 schwarze 1217: putkey(mpage, mpage->sec, TYPE_sec);
1.164 schwarze 1218: if (*mpage->arch != '\0')
1219: putkey(mpage, mpage->arch, TYPE_arch);
1.50 kristaps 1220:
1.182 schwarze 1221: for ( ; mlink != NULL; mlink = mlink->next) {
1.97 schwarze 1222: if ('\0' != *mlink->dsec)
1223: putkey(mpage, mlink->dsec, TYPE_sec);
1224: if ('\0' != *mlink->fsec)
1225: putkey(mpage, mlink->fsec, TYPE_sec);
1.98 schwarze 1226: putkey(mpage, '\0' == *mlink->arch ?
1.113 schwarze 1227: any : mlink->arch, TYPE_arch);
1.133 schwarze 1228: putkey(mpage, mlink->name, NAME_FILE);
1.97 schwarze 1229: }
1.12 schwarze 1230:
1.174 schwarze 1231: assert(mpage->desc == NULL);
1232: if (mdoc != NULL)
1.172 schwarze 1233: parse_mdoc(mpage, mdoc_meta(mdoc), mdoc_node(mdoc));
1.174 schwarze 1234: else if (man != NULL)
1235: parse_man(mpage, man_meta(man), man_node(man));
1.50 kristaps 1236: else
1.162 schwarze 1237: parse_cat(mpage, fd);
1.174 schwarze 1238: if (mpage->desc == NULL)
1.135 schwarze 1239: mpage->desc = mandoc_strdup(mpage->mlinks->name);
1.131 schwarze 1240:
1241: if (warnings && !use_all)
1242: for (mlink = mpage->mlinks; mlink;
1243: mlink = mlink->next)
1244: mlink_check(mpage, mlink);
1.44 kristaps 1245:
1.176 schwarze 1246: dbadd(mpage);
1.182 schwarze 1247: mlink = mpage->mlinks;
1.124 schwarze 1248:
1249: nextpage:
1.170 schwarze 1250: if (mparse_wait(mp) != MANDOCLEVEL_OK) {
1.162 schwarze 1251: exitcode = (int)MANDOCLEVEL_SYSERR;
1.182 schwarze 1252: say(mlink->file, "&wait gunzip");
1.124 schwarze 1253: }
1.66 schwarze 1254: ohash_delete(&strings);
1.133 schwarze 1255: ohash_delete(&names);
1.80 schwarze 1256: mpage = ohash_next(&mpages, &pslot);
1.50 kristaps 1257: }
1.107 schwarze 1258:
1259: if (0 == nodb)
1260: SQL_EXEC("END TRANSACTION");
1.130 schwarze 1261: }
1262:
1263: static void
1264: names_check(void)
1265: {
1266: sqlite3_stmt *stmt;
1267: const char *name, *sec, *arch, *key;
1268: int irc;
1269:
1270: sqlite3_prepare_v2(db,
1271: "SELECT name, sec, arch, key FROM ("
1.133 schwarze 1272: "SELECT name AS key, pageid FROM names "
1.130 schwarze 1273: "WHERE bits & ? AND NOT EXISTS ("
1274: "SELECT pageid FROM mlinks "
1.133 schwarze 1275: "WHERE mlinks.pageid == names.pageid "
1276: "AND mlinks.name == names.name"
1.130 schwarze 1277: ")"
1278: ") JOIN ("
1.142 schwarze 1279: "SELECT sec, arch, name, pageid FROM mlinks "
1280: "GROUP BY pageid"
1.130 schwarze 1281: ") USING (pageid);",
1282: -1, &stmt, NULL);
1283:
1.134 schwarze 1284: if (SQLITE_OK != sqlite3_bind_int64(stmt, 1, NAME_TITLE))
1285: say("", "%s", sqlite3_errmsg(db));
1.130 schwarze 1286:
1287: while (SQLITE_ROW == (irc = sqlite3_step(stmt))) {
1.154 schwarze 1288: name = (const char *)sqlite3_column_text(stmt, 0);
1289: sec = (const char *)sqlite3_column_text(stmt, 1);
1290: arch = (const char *)sqlite3_column_text(stmt, 2);
1291: key = (const char *)sqlite3_column_text(stmt, 3);
1.130 schwarze 1292: say("", "%s(%s%s%s) lacks mlink \"%s\"", name, sec,
1293: '\0' == *arch ? "" : "/",
1294: '\0' == *arch ? "" : arch, key);
1295: }
1296: sqlite3_finalize(stmt);
1.50 kristaps 1297: }
1.12 schwarze 1298:
1.50 kristaps 1299: static void
1.124 schwarze 1300: parse_cat(struct mpage *mpage, int fd)
1.50 kristaps 1301: {
1302: FILE *stream;
1303: char *line, *p, *title;
1304: size_t len, plen, titlesz;
1.12 schwarze 1305:
1.124 schwarze 1306: stream = (-1 == fd) ?
1307: fopen(mpage->mlinks->file, "r") :
1308: fdopen(fd, "r");
1309: if (NULL == stream) {
1.159 schwarze 1310: if (-1 != fd)
1311: close(fd);
1.56 schwarze 1312: if (warnings)
1.123 schwarze 1313: say(mpage->mlinks->file, "&fopen");
1.50 kristaps 1314: return;
1315: }
1.1 kristaps 1316:
1.50 kristaps 1317: /* Skip to first blank line. */
1.1 kristaps 1318:
1.50 kristaps 1319: while (NULL != (line = fgetln(stream, &len)))
1320: if ('\n' == *line)
1321: break;
1.1 kristaps 1322:
1.50 kristaps 1323: /*
1324: * Assume the first line that is not indented
1325: * is the first section header. Skip to it.
1326: */
1.1 kristaps 1327:
1.50 kristaps 1328: while (NULL != (line = fgetln(stream, &len)))
1329: if ('\n' != *line && ' ' != *line)
1330: break;
1.141 schwarze 1331:
1.50 kristaps 1332: /*
1333: * Read up until the next section into a buffer.
1334: * Strip the leading and trailing newline from each read line,
1335: * appending a trailing space.
1336: * Ignore empty (whitespace-only) lines.
1337: */
1.1 kristaps 1338:
1.50 kristaps 1339: titlesz = 0;
1340: title = NULL;
1.38 schwarze 1341:
1.50 kristaps 1342: while (NULL != (line = fgetln(stream, &len))) {
1343: if (' ' != *line || '\n' != line[len - 1])
1344: break;
1345: while (len > 0 && isspace((unsigned char)*line)) {
1346: line++;
1347: len--;
1348: }
1349: if (1 == len)
1.38 schwarze 1350: continue;
1.50 kristaps 1351: title = mandoc_realloc(title, titlesz + len);
1352: memcpy(title + titlesz, line, len);
1353: titlesz += len;
1354: title[titlesz - 1] = ' ';
1355: }
1.38 schwarze 1356:
1.50 kristaps 1357: /*
1358: * If no page content can be found, or the input line
1359: * is already the next section header, or there is no
1360: * trailing newline, reuse the page title as the page
1361: * description.
1362: */
1.44 kristaps 1363:
1.50 kristaps 1364: if (NULL == title || '\0' == *title) {
1.56 schwarze 1365: if (warnings)
1.82 schwarze 1366: say(mpage->mlinks->file,
1367: "Cannot find NAME section");
1.50 kristaps 1368: fclose(stream);
1369: free(title);
1370: return;
1371: }
1.1 kristaps 1372:
1.50 kristaps 1373: title = mandoc_realloc(title, titlesz + 1);
1374: title[titlesz] = '\0';
1.33 schwarze 1375:
1.50 kristaps 1376: /*
1377: * Skip to the first dash.
1378: * Use the remaining line as the description (no more than 70
1379: * bytes).
1380: */
1.33 schwarze 1381:
1.50 kristaps 1382: if (NULL != (p = strstr(title, "- "))) {
1383: for (p += 2; ' ' == *p || '\b' == *p; p++)
1384: /* Skip to next word. */ ;
1385: } else {
1.56 schwarze 1386: if (warnings)
1.82 schwarze 1387: say(mpage->mlinks->file,
1388: "No dash in title line");
1.50 kristaps 1389: p = title;
1390: }
1.38 schwarze 1391:
1.50 kristaps 1392: plen = strlen(p);
1.1 kristaps 1393:
1.50 kristaps 1394: /* Strip backspace-encoding from line. */
1.1 kristaps 1395:
1.50 kristaps 1396: while (NULL != (line = memchr(p, '\b', plen))) {
1397: len = line - p;
1398: if (0 == len) {
1399: memmove(line, line + 1, plen--);
1400: continue;
1.141 schwarze 1401: }
1.50 kristaps 1402: memmove(line - 1, line + 1, plen - len);
1403: plen -= 2;
1404: }
1.1 kristaps 1405:
1.78 schwarze 1406: mpage->desc = mandoc_strdup(p);
1.50 kristaps 1407: fclose(stream);
1408: free(title);
1409: }
1.1 kristaps 1410:
1.50 kristaps 1411: /*
1412: * Put a type/word pair into the word database for this particular file.
1413: */
1414: static void
1.112 schwarze 1415: putkey(const struct mpage *mpage, char *value, uint64_t type)
1.50 kristaps 1416: {
1.112 schwarze 1417: char *cp;
1.18 kristaps 1418:
1.50 kristaps 1419: assert(NULL != value);
1.112 schwarze 1420: if (TYPE_arch == type)
1421: for (cp = value; *cp; cp++)
1422: if (isupper((unsigned char)*cp))
1423: *cp = _tolower((unsigned char)*cp);
1.78 schwarze 1424: putkeys(mpage, value, strlen(value), type);
1.3 kristaps 1425: }
1426:
1427: /*
1.50 kristaps 1428: * Grok all nodes at or below a certain mdoc node into putkey().
1.3 kristaps 1429: */
1430: static void
1.78 schwarze 1431: putmdockey(const struct mpage *mpage,
1432: const struct mdoc_node *n, uint64_t m)
1.3 kristaps 1433: {
1.18 kristaps 1434:
1.50 kristaps 1435: for ( ; NULL != n; n = n->next) {
1436: if (NULL != n->child)
1.78 schwarze 1437: putmdockey(mpage, n->child, m);
1.50 kristaps 1438: if (MDOC_TEXT == n->type)
1.78 schwarze 1439: putkey(mpage, n->string, m);
1.50 kristaps 1440: }
1441: }
1.18 kristaps 1442:
1.61 schwarze 1443: static void
1.174 schwarze 1444: parse_man(struct mpage *mpage, const struct man_meta *meta,
1445: const struct man_node *n)
1.50 kristaps 1446: {
1447: const struct man_node *head, *body;
1.121 schwarze 1448: char *start, *title;
1.50 kristaps 1449: char byte;
1.121 schwarze 1450: size_t sz;
1.18 kristaps 1451:
1.50 kristaps 1452: if (NULL == n)
1.61 schwarze 1453: return;
1.18 kristaps 1454:
1.50 kristaps 1455: /*
1456: * We're only searching for one thing: the first text child in
1457: * the BODY of a NAME section. Since we don't keep track of
1458: * sections in -man, run some hoops to find out whether we're in
1459: * the correct section or not.
1460: */
1.18 kristaps 1461:
1.50 kristaps 1462: if (MAN_BODY == n->type && MAN_SH == n->tok) {
1463: body = n;
1464: assert(body->parent);
1465: if (NULL != (head = body->parent->head) &&
1.141 schwarze 1466: 1 == head->nchild &&
1467: NULL != (head = (head->child)) &&
1468: MAN_TEXT == head->type &&
1469: 0 == strcmp(head->string, "NAME") &&
1470: NULL != body->child) {
1.3 kristaps 1471:
1.50 kristaps 1472: /*
1473: * Suck the entire NAME section into memory.
1474: * Yes, we might run away.
1475: * But too many manuals have big, spread-out
1476: * NAME sections over many lines.
1477: */
1.3 kristaps 1478:
1.121 schwarze 1479: title = NULL;
1480: man_deroff(&title, body);
1.50 kristaps 1481: if (NULL == title)
1.61 schwarze 1482: return;
1.18 kristaps 1483:
1.141 schwarze 1484: /*
1.50 kristaps 1485: * Go through a special heuristic dance here.
1486: * Conventionally, one or more manual names are
1487: * comma-specified prior to a whitespace, then a
1488: * dash, then a description. Try to puzzle out
1489: * the name parts here.
1490: */
1.18 kristaps 1491:
1.121 schwarze 1492: start = title;
1.50 kristaps 1493: for ( ;; ) {
1494: sz = strcspn(start, " ,");
1495: if ('\0' == start[sz])
1496: break;
1.1 kristaps 1497:
1.50 kristaps 1498: byte = start[sz];
1499: start[sz] = '\0';
1.110 schwarze 1500:
1501: /*
1502: * Assume a stray trailing comma in the
1503: * name list if a name begins with a dash.
1504: */
1505:
1506: if ('-' == start[0] ||
1507: ('\\' == start[0] && '-' == start[1]))
1508: break;
1.1 kristaps 1509:
1.133 schwarze 1510: putkey(mpage, start, NAME_TITLE);
1.174 schwarze 1511: if ( ! (mpage->name_head_done ||
1512: strcasecmp(start, meta->title))) {
1513: putkey(mpage, start, NAME_HEAD);
1514: mpage->name_head_done = 1;
1515: }
1.1 kristaps 1516:
1.50 kristaps 1517: if (' ' == byte) {
1518: start += sz + 1;
1519: break;
1520: }
1.1 kristaps 1521:
1.50 kristaps 1522: assert(',' == byte);
1523: start += sz + 1;
1524: while (' ' == *start)
1525: start++;
1526: }
1.1 kristaps 1527:
1.121 schwarze 1528: if (start == title) {
1.133 schwarze 1529: putkey(mpage, start, NAME_TITLE);
1.174 schwarze 1530: if ( ! (mpage->name_head_done ||
1531: strcasecmp(start, meta->title))) {
1532: putkey(mpage, start, NAME_HEAD);
1533: mpage->name_head_done = 1;
1534: }
1.50 kristaps 1535: free(title);
1.61 schwarze 1536: return;
1.50 kristaps 1537: }
1.1 kristaps 1538:
1.50 kristaps 1539: while (isspace((unsigned char)*start))
1540: start++;
1.1 kristaps 1541:
1.50 kristaps 1542: if (0 == strncmp(start, "-", 1))
1543: start += 1;
1544: else if (0 == strncmp(start, "\\-\\-", 4))
1545: start += 4;
1546: else if (0 == strncmp(start, "\\-", 2))
1547: start += 2;
1548: else if (0 == strncmp(start, "\\(en", 4))
1549: start += 4;
1550: else if (0 == strncmp(start, "\\(em", 4))
1551: start += 4;
1.1 kristaps 1552:
1.50 kristaps 1553: while (' ' == *start)
1554: start++;
1.1 kristaps 1555:
1.78 schwarze 1556: mpage->desc = mandoc_strdup(start);
1.50 kristaps 1557: free(title);
1.61 schwarze 1558: return;
1.50 kristaps 1559: }
1560: }
1.1 kristaps 1561:
1.77 schwarze 1562: for (n = n->child; n; n = n->next) {
1.78 schwarze 1563: if (NULL != mpage->desc)
1.77 schwarze 1564: break;
1.174 schwarze 1565: parse_man(mpage, meta, n);
1.77 schwarze 1566: }
1.1 kristaps 1567: }
1568:
1569: static void
1.172 schwarze 1570: parse_mdoc(struct mpage *mpage, const struct mdoc_meta *meta,
1571: const struct mdoc_node *n)
1.1 kristaps 1572: {
1573:
1.50 kristaps 1574: assert(NULL != n);
1575: for (n = n->child; NULL != n; n = n->next) {
1576: switch (n->type) {
1.141 schwarze 1577: case MDOC_ELEM:
1.50 kristaps 1578: /* FALLTHROUGH */
1.141 schwarze 1579: case MDOC_BLOCK:
1.50 kristaps 1580: /* FALLTHROUGH */
1.141 schwarze 1581: case MDOC_HEAD:
1.50 kristaps 1582: /* FALLTHROUGH */
1.141 schwarze 1583: case MDOC_BODY:
1.50 kristaps 1584: /* FALLTHROUGH */
1.141 schwarze 1585: case MDOC_TAIL:
1.50 kristaps 1586: if (NULL != mdocs[n->tok].fp)
1.172 schwarze 1587: if (0 == (*mdocs[n->tok].fp)(mpage, meta, n))
1.50 kristaps 1588: break;
1.68 schwarze 1589: if (mdocs[n->tok].mask)
1.78 schwarze 1590: putmdockey(mpage, n->child,
1591: mdocs[n->tok].mask);
1.50 kristaps 1592: break;
1593: default:
1594: assert(MDOC_ROOT != n->type);
1595: continue;
1596: }
1597: if (NULL != n->child)
1.172 schwarze 1598: parse_mdoc(mpage, meta, n);
1.1 kristaps 1599: }
1600: }
1601:
1.25 schwarze 1602: static int
1.172 schwarze 1603: parse_mdoc_Fd(struct mpage *mpage, const struct mdoc_meta *meta,
1604: const struct mdoc_node *n)
1.1 kristaps 1605: {
1.176 schwarze 1606: char *start, *end;
1.1 kristaps 1607: size_t sz;
1.25 schwarze 1608:
1.50 kristaps 1609: if (SEC_SYNOPSIS != n->sec ||
1.141 schwarze 1610: NULL == (n = n->child) ||
1611: MDOC_TEXT != n->type)
1.25 schwarze 1612: return(0);
1.1 kristaps 1613:
1614: /*
1615: * Only consider those `Fd' macro fields that begin with an
1616: * "inclusion" token (versus, e.g., #define).
1617: */
1.50 kristaps 1618:
1.1 kristaps 1619: if (strcmp("#include", n->string))
1.25 schwarze 1620: return(0);
1.1 kristaps 1621:
1622: if (NULL == (n = n->next) || MDOC_TEXT != n->type)
1.25 schwarze 1623: return(0);
1.1 kristaps 1624:
1625: /*
1626: * Strip away the enclosing angle brackets and make sure we're
1627: * not zero-length.
1628: */
1629:
1630: start = n->string;
1631: if ('<' == *start || '"' == *start)
1632: start++;
1633:
1634: if (0 == (sz = strlen(start)))
1.25 schwarze 1635: return(0);
1.1 kristaps 1636:
1637: end = &start[(int)sz - 1];
1638: if ('>' == *end || '"' == *end)
1639: end--;
1640:
1.50 kristaps 1641: if (end > start)
1.78 schwarze 1642: putkeys(mpage, start, end - start + 1, TYPE_In);
1.92 schwarze 1643: return(0);
1.1 kristaps 1644: }
1645:
1.178 schwarze 1646: static void
1647: parse_mdoc_fname(struct mpage *mpage, const struct mdoc_node *n)
1.1 kristaps 1648: {
1.112 schwarze 1649: char *cp;
1.178 schwarze 1650: size_t sz;
1.1 kristaps 1651:
1.178 schwarze 1652: if (n->type != MDOC_TEXT)
1653: return;
1.25 schwarze 1654:
1.178 schwarze 1655: /* Skip function pointer punctuation. */
1.1 kristaps 1656:
1.178 schwarze 1657: cp = n->string;
1658: while (*cp == '(' || *cp == '*')
1.1 kristaps 1659: cp++;
1.178 schwarze 1660: sz = strcspn(cp, "()");
1.1 kristaps 1661:
1.178 schwarze 1662: putkeys(mpage, cp, sz, TYPE_Fn);
1.173 schwarze 1663: if (n->sec == SEC_SYNOPSIS)
1.178 schwarze 1664: putkeys(mpage, cp, sz, NAME_SYN);
1665: }
1.25 schwarze 1666:
1.178 schwarze 1667: static int
1668: parse_mdoc_Fn(struct mpage *mpage, const struct mdoc_meta *meta,
1669: const struct mdoc_node *n)
1670: {
1671:
1672: if (n->child == NULL)
1673: return(0);
1674:
1675: parse_mdoc_fname(mpage, n->child);
1.25 schwarze 1676:
1.178 schwarze 1677: for (n = n->child->next; n != NULL; n = n->next)
1678: if (n->type == MDOC_TEXT)
1.78 schwarze 1679: putkey(mpage, n->string, TYPE_Fa);
1.25 schwarze 1680:
1.173 schwarze 1681: return(0);
1682: }
1683:
1684: static int
1685: parse_mdoc_Fo(struct mpage *mpage, const struct mdoc_meta *meta,
1686: const struct mdoc_node *n)
1687: {
1.177 schwarze 1688:
1689: if (n->type != MDOC_HEAD)
1690: return(1);
1.173 schwarze 1691:
1.178 schwarze 1692: if (n->child != NULL)
1693: parse_mdoc_fname(mpage, n->child);
1694:
1.25 schwarze 1695: return(0);
1.1 kristaps 1696: }
1697:
1.25 schwarze 1698: static int
1.172 schwarze 1699: parse_mdoc_Xr(struct mpage *mpage, const struct mdoc_meta *meta,
1700: const struct mdoc_node *n)
1.1 kristaps 1701: {
1.67 schwarze 1702: char *cp;
1.1 kristaps 1703:
1704: if (NULL == (n = n->child))
1.25 schwarze 1705: return(0);
1.1 kristaps 1706:
1.67 schwarze 1707: if (NULL == n->next) {
1.78 schwarze 1708: putkey(mpage, n->string, TYPE_Xr);
1.67 schwarze 1709: return(0);
1710: }
1711:
1.120 schwarze 1712: mandoc_asprintf(&cp, "%s(%s)", n->string, n->next->string);
1.78 schwarze 1713: putkey(mpage, cp, TYPE_Xr);
1.67 schwarze 1714: free(cp);
1715: return(0);
1.1 kristaps 1716: }
1717:
1.25 schwarze 1718: static int
1.172 schwarze 1719: parse_mdoc_Nd(struct mpage *mpage, const struct mdoc_meta *meta,
1720: const struct mdoc_node *n)
1.1 kristaps 1721: {
1722:
1.122 schwarze 1723: if (MDOC_BODY == n->type)
1724: mdoc_deroff(&mpage->desc, n);
1725: return(0);
1.1 kristaps 1726: }
1727:
1.25 schwarze 1728: static int
1.172 schwarze 1729: parse_mdoc_Nm(struct mpage *mpage, const struct mdoc_meta *meta,
1730: const struct mdoc_node *n)
1.1 kristaps 1731: {
1732:
1.129 schwarze 1733: if (SEC_NAME == n->sec)
1.133 schwarze 1734: putmdockey(mpage, n->child, NAME_TITLE);
1.172 schwarze 1735: else if (SEC_SYNOPSIS == n->sec && MDOC_HEAD == n->type) {
1736: if (n->child == NULL)
1737: putkey(mpage, meta->name, NAME_SYN);
1738: else
1739: putmdockey(mpage, n->child, NAME_SYN);
1.174 schwarze 1740: }
1741: if ( ! (mpage->name_head_done ||
1742: n->child == NULL || n->child->string == NULL ||
1743: strcasecmp(n->child->string, meta->title))) {
1744: putkey(mpage, n->child->string, NAME_HEAD);
1745: mpage->name_head_done = 1;
1.172 schwarze 1746: }
1.129 schwarze 1747: return(0);
1.1 kristaps 1748: }
1749:
1.25 schwarze 1750: static int
1.172 schwarze 1751: parse_mdoc_Sh(struct mpage *mpage, const struct mdoc_meta *meta,
1752: const struct mdoc_node *n)
1.1 kristaps 1753: {
1754:
1.25 schwarze 1755: return(SEC_CUSTOM == n->sec && MDOC_HEAD == n->type);
1.1 kristaps 1756: }
1757:
1.50 kristaps 1758: static int
1.172 schwarze 1759: parse_mdoc_head(struct mpage *mpage, const struct mdoc_meta *meta,
1760: const struct mdoc_node *n)
1.1 kristaps 1761: {
1762:
1.50 kristaps 1763: return(MDOC_HEAD == n->type);
1764: }
1.1 kristaps 1765:
1.50 kristaps 1766: static int
1.172 schwarze 1767: parse_mdoc_body(struct mpage *mpage, const struct mdoc_meta *meta,
1768: const struct mdoc_node *n)
1.50 kristaps 1769: {
1.1 kristaps 1770:
1.50 kristaps 1771: return(MDOC_BODY == n->type);
1.1 kristaps 1772: }
1773:
1.50 kristaps 1774: /*
1.66 schwarze 1775: * Add a string to the hash table for the current manual.
1776: * Each string has a bitmask telling which macros it belongs to.
1777: * When we finish the manual, we'll dump the table.
1.50 kristaps 1778: */
1779: static void
1.176 schwarze 1780: putkeys(const struct mpage *mpage, char *cp, size_t sz, uint64_t v)
1.50 kristaps 1781: {
1.133 schwarze 1782: struct ohash *htab;
1.50 kristaps 1783: struct str *s;
1.111 schwarze 1784: const char *end;
1.65 schwarze 1785: unsigned int slot;
1.176 schwarze 1786: int i, mustfree;
1.25 schwarze 1787:
1.50 kristaps 1788: if (0 == sz)
1789: return;
1.111 schwarze 1790:
1.176 schwarze 1791: mustfree = render_string(&cp, &sz);
1792:
1.133 schwarze 1793: if (TYPE_Nm & v) {
1794: htab = &names;
1795: v &= name_mask;
1.169 schwarze 1796: if (v & NAME_FIRST)
1797: name_mask &= ~NAME_FIRST;
1.133 schwarze 1798: if (debug > 1)
1799: say(mpage->mlinks->file,
1.175 schwarze 1800: "Adding name %*s, bits=%d", sz, cp, v);
1.133 schwarze 1801: } else {
1802: htab = &strings;
1803: if (debug > 1)
1804: for (i = 0; i < mansearch_keymax; i++)
1.163 schwarze 1805: if ((uint64_t)1 << i & v)
1.133 schwarze 1806: say(mpage->mlinks->file,
1807: "Adding key %s=%*s",
1808: mansearch_keynames[i], sz, cp);
1.111 schwarze 1809: }
1.25 schwarze 1810:
1.65 schwarze 1811: end = cp + sz;
1.133 schwarze 1812: slot = ohash_qlookupi(htab, cp, &end);
1813: s = ohash_find(htab, slot);
1.25 schwarze 1814:
1.78 schwarze 1815: if (NULL != s && mpage == s->mpage) {
1.50 kristaps 1816: s->mask |= v;
1817: return;
1818: } else if (NULL == s) {
1.144 schwarze 1819: s = mandoc_calloc(1, sizeof(struct str) + sz + 1);
1.50 kristaps 1820: memcpy(s->key, cp, sz);
1.133 schwarze 1821: ohash_insert(htab, slot, s);
1.1 kristaps 1822: }
1.78 schwarze 1823: s->mpage = mpage;
1.50 kristaps 1824: s->mask = v;
1.176 schwarze 1825:
1826: if (mustfree)
1827: free(cp);
1.1 kristaps 1828: }
1829:
1.50 kristaps 1830: /*
1831: * Take a Unicode codepoint and produce its UTF-8 encoding.
1832: * This isn't the best way to do this, but it works.
1833: * The magic numbers are from the UTF-8 packaging.
1834: * They're not as scary as they seem: read the UTF-8 spec for details.
1835: */
1836: static size_t
1837: utf8(unsigned int cp, char out[7])
1.1 kristaps 1838: {
1.50 kristaps 1839: size_t rc;
1.1 kristaps 1840:
1.50 kristaps 1841: rc = 0;
1842: if (cp <= 0x0000007F) {
1843: rc = 1;
1844: out[0] = (char)cp;
1845: } else if (cp <= 0x000007FF) {
1846: rc = 2;
1847: out[0] = (cp >> 6 & 31) | 192;
1848: out[1] = (cp & 63) | 128;
1849: } else if (cp <= 0x0000FFFF) {
1850: rc = 3;
1851: out[0] = (cp >> 12 & 15) | 224;
1852: out[1] = (cp >> 6 & 63) | 128;
1853: out[2] = (cp & 63) | 128;
1854: } else if (cp <= 0x001FFFFF) {
1855: rc = 4;
1856: out[0] = (cp >> 18 & 7) | 240;
1857: out[1] = (cp >> 12 & 63) | 128;
1858: out[2] = (cp >> 6 & 63) | 128;
1859: out[3] = (cp & 63) | 128;
1860: } else if (cp <= 0x03FFFFFF) {
1861: rc = 5;
1862: out[0] = (cp >> 24 & 3) | 248;
1863: out[1] = (cp >> 18 & 63) | 128;
1864: out[2] = (cp >> 12 & 63) | 128;
1865: out[3] = (cp >> 6 & 63) | 128;
1866: out[4] = (cp & 63) | 128;
1867: } else if (cp <= 0x7FFFFFFF) {
1868: rc = 6;
1869: out[0] = (cp >> 30 & 1) | 252;
1870: out[1] = (cp >> 24 & 63) | 128;
1871: out[2] = (cp >> 18 & 63) | 128;
1872: out[3] = (cp >> 12 & 63) | 128;
1873: out[4] = (cp >> 6 & 63) | 128;
1874: out[5] = (cp & 63) | 128;
1875: } else
1.1 kristaps 1876: return(0);
1877:
1.50 kristaps 1878: out[rc] = '\0';
1879: return(rc);
1880: }
1.1 kristaps 1881:
1.50 kristaps 1882: /*
1.176 schwarze 1883: * If the string contains escape sequences,
1884: * replace it with an allocated rendering and return 1,
1885: * such that the caller can free it after use.
1886: * Otherwise, do nothing and return 0.
1.50 kristaps 1887: */
1.176 schwarze 1888: static int
1889: render_string(char **public, size_t *psz)
1.50 kristaps 1890: {
1.176 schwarze 1891: const char *src, *scp, *addcp, *seq;
1892: char *dst;
1893: size_t ssz, dsz, addsz;
1.114 schwarze 1894: char utfbuf[7], res[6];
1.176 schwarze 1895: int seqlen, unicode;
1.50 kristaps 1896:
1897: res[0] = '\\';
1898: res[1] = '\t';
1899: res[2] = ASCII_NBRSP;
1900: res[3] = ASCII_HYPH;
1.114 schwarze 1901: res[4] = ASCII_BREAK;
1902: res[5] = '\0';
1.1 kristaps 1903:
1.176 schwarze 1904: src = scp = *public;
1905: ssz = *psz;
1906: dst = NULL;
1907: dsz = 0;
1908:
1909: while (scp < src + *psz) {
1910:
1911: /* Leave normal characters unchanged. */
1912:
1913: if (strchr(res, *scp) == NULL) {
1914: if (dst != NULL)
1915: dst[dsz++] = *scp;
1916: scp++;
1917: continue;
1918: }
1.46 kristaps 1919:
1.50 kristaps 1920: /*
1.176 schwarze 1921: * Found something that requires replacing,
1922: * make sure we have a destination buffer.
1.50 kristaps 1923: */
1.176 schwarze 1924:
1925: if (dst == NULL) {
1926: dst = mandoc_malloc(ssz + 1);
1927: dsz = scp - src;
1928: memcpy(dst, src, dsz);
1.50 kristaps 1929: }
1.46 kristaps 1930:
1.176 schwarze 1931: /* Handle single-char special characters. */
1932:
1933: switch (*scp) {
1934: case '\\':
1935: break;
1.141 schwarze 1936: case '\t':
1.114 schwarze 1937: /* FALLTHROUGH */
1.141 schwarze 1938: case ASCII_NBRSP:
1.176 schwarze 1939: dst[dsz++] = ' ';
1940: scp++;
1941: continue;
1942: case ASCII_HYPH:
1943: dst[dsz++] = '-';
1.114 schwarze 1944: /* FALLTHROUGH */
1.141 schwarze 1945: case ASCII_BREAK:
1.176 schwarze 1946: scp++;
1.50 kristaps 1947: continue;
1.114 schwarze 1948: default:
1.176 schwarze 1949: abort();
1.114 schwarze 1950: }
1.46 kristaps 1951:
1.50 kristaps 1952: /*
1.176 schwarze 1953: * Found an escape sequence.
1954: * Read past the slash, then parse it.
1955: * Ignore everything except characters.
1.50 kristaps 1956: */
1.95 schwarze 1957:
1.176 schwarze 1958: scp++;
1959: if (mandoc_escape(&scp, &seq, &seqlen) != ESCAPE_SPECIAL)
1.50 kristaps 1960: continue;
1.1 kristaps 1961:
1.50 kristaps 1962: /*
1.95 schwarze 1963: * Render the special character
1964: * as either UTF-8 or ASCII.
1.50 kristaps 1965: */
1.95 schwarze 1966:
1967: if (write_utf8) {
1.176 schwarze 1968: unicode = mchars_spec2cp(mchars, seq, seqlen);
1969: if (unicode <= 0)
1.95 schwarze 1970: continue;
1.176 schwarze 1971: addsz = utf8(unicode, utfbuf);
1972: if (addsz == 0)
1.95 schwarze 1973: continue;
1.176 schwarze 1974: addcp = utfbuf;
1.95 schwarze 1975: } else {
1.176 schwarze 1976: addcp = mchars_spec2str(mchars, seq, seqlen, &addsz);
1977: if (addcp == NULL)
1.95 schwarze 1978: continue;
1.176 schwarze 1979: if (*addcp == ASCII_NBRSP) {
1980: addcp = " ";
1981: addsz = 1;
1.95 schwarze 1982: }
1983: }
1.1 kristaps 1984:
1.50 kristaps 1985: /* Copy the rendered glyph into the stream. */
1.1 kristaps 1986:
1.176 schwarze 1987: ssz += addsz;
1988: dst = mandoc_realloc(dst, ssz + 1);
1989: memcpy(dst + dsz, addcp, addsz);
1990: dsz += addsz;
1991: }
1992: if (dst != NULL) {
1993: *public = dst;
1994: *psz = dsz;
1995: }
1996:
1997: /* Trim trailing whitespace and NUL-terminate. */
1998:
1999: while (*psz > 0 && (*public)[*psz - 1] == ' ')
2000: --*psz;
2001: if (dst != NULL) {
2002: (*public)[*psz] = '\0';
2003: return(1);
2004: } else
2005: return(0);
1.1 kristaps 2006: }
2007:
1.118 schwarze 2008: static void
2009: dbadd_mlink(const struct mlink *mlink)
2010: {
2011: size_t i;
2012:
2013: i = 1;
2014: SQL_BIND_TEXT(stmts[STMT_INSERT_LINK], i, mlink->dsec);
2015: SQL_BIND_TEXT(stmts[STMT_INSERT_LINK], i, mlink->arch);
2016: SQL_BIND_TEXT(stmts[STMT_INSERT_LINK], i, mlink->name);
1.137 schwarze 2017: SQL_BIND_INT64(stmts[STMT_INSERT_LINK], i, mlink->mpage->pageid);
1.118 schwarze 2018: SQL_STEP(stmts[STMT_INSERT_LINK]);
2019: sqlite3_reset(stmts[STMT_INSERT_LINK]);
1.169 schwarze 2020: }
2021:
2022: static void
2023: dbadd_mlink_name(const struct mlink *mlink)
2024: {
1.175 schwarze 2025: uint64_t bits;
1.169 schwarze 2026: size_t i;
2027:
2028: dbadd_mlink(mlink);
1.160 schwarze 2029:
2030: i = 1;
1.175 schwarze 2031: SQL_BIND_INT64(stmts[STMT_SELECT_NAME], i, mlink->mpage->pageid);
2032: bits = NAME_FILE & NAME_MASK;
2033: if (sqlite3_step(stmts[STMT_SELECT_NAME]) == SQLITE_ROW) {
2034: bits |= sqlite3_column_int64(stmts[STMT_SELECT_NAME], 0);
2035: sqlite3_reset(stmts[STMT_SELECT_NAME]);
2036: }
2037:
2038: i = 1;
2039: SQL_BIND_INT64(stmts[STMT_INSERT_NAME], i, bits);
1.160 schwarze 2040: SQL_BIND_TEXT(stmts[STMT_INSERT_NAME], i, mlink->name);
2041: SQL_BIND_INT64(stmts[STMT_INSERT_NAME], i, mlink->mpage->pageid);
2042: SQL_STEP(stmts[STMT_INSERT_NAME]);
2043: sqlite3_reset(stmts[STMT_INSERT_NAME]);
1.118 schwarze 2044: }
2045:
1.14 schwarze 2046: /*
1.50 kristaps 2047: * Flush the current page's terms (and their bits) into the database.
2048: * Wrap the entire set of additions in a transaction to make sqlite be a
2049: * little faster.
1.96 schwarze 2050: * Also, handle escape sequences at the last possible moment.
1.14 schwarze 2051: */
2052: static void
1.176 schwarze 2053: dbadd(struct mpage *mpage)
1.14 schwarze 2054: {
1.87 schwarze 2055: struct mlink *mlink;
1.50 kristaps 2056: struct str *key;
1.176 schwarze 2057: char *cp;
1.52 kristaps 2058: size_t i;
1.66 schwarze 2059: unsigned int slot;
1.176 schwarze 2060: int mustfree;
1.14 schwarze 2061:
1.128 schwarze 2062: mlink = mpage->mlinks;
1.43 kristaps 2063:
1.128 schwarze 2064: if (nodb) {
1.147 schwarze 2065: for (key = ohash_first(&names, &slot); NULL != key;
1.176 schwarze 2066: key = ohash_next(&names, &slot))
1.147 schwarze 2067: free(key);
2068: for (key = ohash_first(&strings, &slot); NULL != key;
1.176 schwarze 2069: key = ohash_next(&strings, &slot))
1.147 schwarze 2070: free(key);
1.145 schwarze 2071: if (0 == debug)
2072: return;
1.128 schwarze 2073: while (NULL != mlink) {
2074: fputs(mlink->name, stdout);
2075: if (NULL == mlink->next ||
2076: strcmp(mlink->dsec, mlink->next->dsec) ||
2077: strcmp(mlink->fsec, mlink->next->fsec) ||
2078: strcmp(mlink->arch, mlink->next->arch)) {
2079: putchar('(');
2080: if ('\0' == *mlink->dsec)
2081: fputs(mlink->fsec, stdout);
2082: else
2083: fputs(mlink->dsec, stdout);
2084: if ('\0' != *mlink->arch)
2085: printf("/%s", mlink->arch);
2086: putchar(')');
2087: }
2088: mlink = mlink->next;
2089: if (NULL != mlink)
2090: fputs(", ", stdout);
2091: }
1.132 schwarze 2092: printf(" - %s\n", mpage->desc);
1.14 schwarze 2093: return;
1.128 schwarze 2094: }
2095:
2096: if (debug)
2097: say(mlink->file, "Adding to database");
1.28 kristaps 2098:
1.176 schwarze 2099: cp = mpage->desc;
2100: i = strlen(cp);
2101: mustfree = render_string(&cp, &i);
1.52 kristaps 2102: i = 1;
1.176 schwarze 2103: SQL_BIND_TEXT(stmts[STMT_INSERT_PAGE], i, cp);
1.161 schwarze 2104: SQL_BIND_INT(stmts[STMT_INSERT_PAGE], i, mpage->form);
1.81 schwarze 2105: SQL_STEP(stmts[STMT_INSERT_PAGE]);
1.137 schwarze 2106: mpage->pageid = sqlite3_last_insert_rowid(db);
1.81 schwarze 2107: sqlite3_reset(stmts[STMT_INSERT_PAGE]);
1.176 schwarze 2108: if (mustfree)
2109: free(cp);
1.81 schwarze 2110:
1.128 schwarze 2111: while (NULL != mlink) {
1.118 schwarze 2112: dbadd_mlink(mlink);
1.128 schwarze 2113: mlink = mlink->next;
2114: }
1.134 schwarze 2115: mlink = mpage->mlinks;
1.50 kristaps 2116:
1.133 schwarze 2117: for (key = ohash_first(&names, &slot); NULL != key;
2118: key = ohash_next(&names, &slot)) {
2119: assert(key->mpage == mpage);
2120: i = 1;
2121: SQL_BIND_INT64(stmts[STMT_INSERT_NAME], i, key->mask);
1.176 schwarze 2122: SQL_BIND_TEXT(stmts[STMT_INSERT_NAME], i, key->key);
1.137 schwarze 2123: SQL_BIND_INT64(stmts[STMT_INSERT_NAME], i, mpage->pageid);
1.133 schwarze 2124: SQL_STEP(stmts[STMT_INSERT_NAME]);
2125: sqlite3_reset(stmts[STMT_INSERT_NAME]);
2126: free(key);
2127: }
1.66 schwarze 2128: for (key = ohash_first(&strings, &slot); NULL != key;
2129: key = ohash_next(&strings, &slot)) {
1.78 schwarze 2130: assert(key->mpage == mpage);
1.52 kristaps 2131: i = 1;
2132: SQL_BIND_INT64(stmts[STMT_INSERT_KEY], i, key->mask);
1.176 schwarze 2133: SQL_BIND_TEXT(stmts[STMT_INSERT_KEY], i, key->key);
1.137 schwarze 2134: SQL_BIND_INT64(stmts[STMT_INSERT_KEY], i, mpage->pageid);
1.52 kristaps 2135: SQL_STEP(stmts[STMT_INSERT_KEY]);
1.50 kristaps 2136: sqlite3_reset(stmts[STMT_INSERT_KEY]);
1.66 schwarze 2137: free(key);
1.38 schwarze 2138: }
1.14 schwarze 2139: }
2140:
1.5 kristaps 2141: static void
1.59 schwarze 2142: dbprune(void)
1.5 kristaps 2143: {
1.78 schwarze 2144: struct mpage *mpage;
1.82 schwarze 2145: struct mlink *mlink;
1.52 kristaps 2146: size_t i;
1.80 schwarze 2147: unsigned int slot;
1.5 kristaps 2148:
1.106 schwarze 2149: if (0 == nodb)
2150: SQL_EXEC("BEGIN TRANSACTION");
1.12 schwarze 2151:
1.106 schwarze 2152: for (mpage = ohash_first(&mpages, &slot); NULL != mpage;
2153: mpage = ohash_next(&mpages, &slot)) {
1.82 schwarze 2154: mlink = mpage->mlinks;
1.125 schwarze 2155: if (debug)
1.106 schwarze 2156: say(mlink->file, "Deleting from database");
2157: if (nodb)
2158: continue;
2159: for ( ; NULL != mlink; mlink = mlink->next) {
2160: i = 1;
2161: SQL_BIND_TEXT(stmts[STMT_DELETE_PAGE],
2162: i, mlink->dsec);
2163: SQL_BIND_TEXT(stmts[STMT_DELETE_PAGE],
2164: i, mlink->arch);
2165: SQL_BIND_TEXT(stmts[STMT_DELETE_PAGE],
2166: i, mlink->name);
2167: SQL_STEP(stmts[STMT_DELETE_PAGE]);
2168: sqlite3_reset(stmts[STMT_DELETE_PAGE]);
2169: }
1.5 kristaps 2170: }
1.106 schwarze 2171:
2172: if (0 == nodb)
2173: SQL_EXEC("END TRANSACTION");
1.5 kristaps 2174: }
2175:
1.4 kristaps 2176: /*
1.50 kristaps 2177: * Close an existing database and its prepared statements.
2178: * If "real" is not set, rename the temporary file into the real one.
1.4 kristaps 2179: */
1.35 kristaps 2180: static void
1.59 schwarze 2181: dbclose(int real)
1.4 kristaps 2182: {
1.50 kristaps 2183: size_t i;
1.115 schwarze 2184: int status;
2185: pid_t child;
1.4 kristaps 2186:
1.50 kristaps 2187: if (nodb)
1.38 schwarze 2188: return;
1.50 kristaps 2189:
2190: for (i = 0; i < STMT__MAX; i++) {
2191: sqlite3_finalize(stmts[i]);
2192: stmts[i] = NULL;
1.4 kristaps 2193: }
2194:
1.50 kristaps 2195: sqlite3_close(db);
2196: db = NULL;
1.12 schwarze 2197:
1.50 kristaps 2198: if (real)
2199: return;
1.12 schwarze 2200:
1.115 schwarze 2201: if ('\0' == *tempfilename) {
2202: if (-1 == rename(MANDOC_DB "~", MANDOC_DB)) {
2203: exitcode = (int)MANDOCLEVEL_SYSERR;
1.123 schwarze 2204: say(MANDOC_DB, "&rename");
1.115 schwarze 2205: }
2206: return;
2207: }
2208:
2209: switch (child = fork()) {
1.141 schwarze 2210: case -1:
1.115 schwarze 2211: exitcode = (int)MANDOCLEVEL_SYSERR;
1.123 schwarze 2212: say("", "&fork cmp");
1.115 schwarze 2213: return;
1.141 schwarze 2214: case 0:
1.115 schwarze 2215: execlp("cmp", "cmp", "-s",
2216: tempfilename, MANDOC_DB, NULL);
1.123 schwarze 2217: say("", "&exec cmp");
1.115 schwarze 2218: exit(0);
2219: default:
2220: break;
2221: }
2222: if (-1 == waitpid(child, &status, 0)) {
2223: exitcode = (int)MANDOCLEVEL_SYSERR;
1.123 schwarze 2224: say("", "&wait cmp");
1.115 schwarze 2225: } else if (WIFSIGNALED(status)) {
2226: exitcode = (int)MANDOCLEVEL_SYSERR;
1.123 schwarze 2227: say("", "cmp died from signal %d", WTERMSIG(status));
1.115 schwarze 2228: } else if (WEXITSTATUS(status)) {
1.59 schwarze 2229: exitcode = (int)MANDOCLEVEL_SYSERR;
1.115 schwarze 2230: say(MANDOC_DB,
2231: "Data changed, but cannot replace database");
2232: }
2233:
2234: *strrchr(tempfilename, '/') = '\0';
2235: switch (child = fork()) {
1.141 schwarze 2236: case -1:
1.115 schwarze 2237: exitcode = (int)MANDOCLEVEL_SYSERR;
1.123 schwarze 2238: say("", "&fork rm");
1.115 schwarze 2239: return;
1.141 schwarze 2240: case 0:
1.115 schwarze 2241: execlp("rm", "rm", "-rf", tempfilename, NULL);
1.123 schwarze 2242: say("", "&exec rm");
1.115 schwarze 2243: exit((int)MANDOCLEVEL_SYSERR);
2244: default:
2245: break;
2246: }
2247: if (-1 == waitpid(child, &status, 0)) {
2248: exitcode = (int)MANDOCLEVEL_SYSERR;
1.123 schwarze 2249: say("", "&wait rm");
1.115 schwarze 2250: } else if (WIFSIGNALED(status) || WEXITSTATUS(status)) {
2251: exitcode = (int)MANDOCLEVEL_SYSERR;
1.123 schwarze 2252: say("", "%s: Cannot remove temporary directory",
2253: tempfilename);
1.59 schwarze 2254: }
1.50 kristaps 2255: }
1.14 schwarze 2256:
1.50 kristaps 2257: /*
2258: * This is straightforward stuff.
2259: * Open a database connection to a "temporary" database, then open a set
2260: * of prepared statements we'll use over and over again.
2261: * If "real" is set, we use the existing database; if not, we truncate a
2262: * temporary one.
2263: * Must be matched by dbclose().
2264: */
2265: static int
1.59 schwarze 2266: dbopen(int real)
1.50 kristaps 2267: {
1.115 schwarze 2268: const char *sql;
1.50 kristaps 2269: int rc, ofl;
1.12 schwarze 2270:
1.141 schwarze 2271: if (nodb)
1.50 kristaps 2272: return(1);
1.12 schwarze 2273:
1.115 schwarze 2274: *tempfilename = '\0';
1.63 schwarze 2275: ofl = SQLITE_OPEN_READWRITE;
1.115 schwarze 2276:
2277: if (real) {
2278: rc = sqlite3_open_v2(MANDOC_DB, &db, ofl, NULL);
2279: if (SQLITE_OK != rc) {
1.63 schwarze 2280: exitcode = (int)MANDOCLEVEL_SYSERR;
1.149 schwarze 2281: if (SQLITE_CANTOPEN != rc)
2282: say(MANDOC_DB, "%s", sqlite3_errstr(rc));
1.63 schwarze 2283: return(0);
2284: }
1.115 schwarze 2285: goto prepare_statements;
2286: }
2287:
2288: ofl |= SQLITE_OPEN_CREATE | SQLITE_OPEN_EXCLUSIVE;
1.45 kristaps 2289:
1.115 schwarze 2290: remove(MANDOC_DB "~");
2291: rc = sqlite3_open_v2(MANDOC_DB "~", &db, ofl, NULL);
1.141 schwarze 2292: if (SQLITE_OK == rc)
1.115 schwarze 2293: goto create_tables;
1.116 schwarze 2294: if (MPARSE_QUICK & mparse_options) {
1.59 schwarze 2295: exitcode = (int)MANDOCLEVEL_SYSERR;
1.146 schwarze 2296: say(MANDOC_DB "~", "%s", sqlite3_errstr(rc));
1.50 kristaps 2297: return(0);
2298: }
1.12 schwarze 2299:
1.143 schwarze 2300: (void)strlcpy(tempfilename, "/tmp/mandocdb.XXXXXX",
2301: sizeof(tempfilename));
1.115 schwarze 2302: if (NULL == mkdtemp(tempfilename)) {
2303: exitcode = (int)MANDOCLEVEL_SYSERR;
1.123 schwarze 2304: say("", "&%s", tempfilename);
1.115 schwarze 2305: return(0);
2306: }
1.143 schwarze 2307: (void)strlcat(tempfilename, "/" MANDOC_DB,
2308: sizeof(tempfilename));
1.115 schwarze 2309: rc = sqlite3_open_v2(tempfilename, &db, ofl, NULL);
2310: if (SQLITE_OK != rc) {
1.59 schwarze 2311: exitcode = (int)MANDOCLEVEL_SYSERR;
1.146 schwarze 2312: say("", "%s: %s", tempfilename, sqlite3_errstr(rc));
1.50 kristaps 2313: return(0);
2314: }
1.12 schwarze 2315:
1.115 schwarze 2316: create_tables:
1.81 schwarze 2317: sql = "CREATE TABLE \"mpages\" (\n"
1.132 schwarze 2318: " \"desc\" TEXT NOT NULL,\n"
1.50 kristaps 2319: " \"form\" INTEGER NOT NULL,\n"
1.137 schwarze 2320: " \"pageid\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
1.50 kristaps 2321: ");\n"
2322: "\n"
1.81 schwarze 2323: "CREATE TABLE \"mlinks\" (\n"
2324: " \"sec\" TEXT NOT NULL,\n"
2325: " \"arch\" TEXT NOT NULL,\n"
2326: " \"name\" TEXT NOT NULL,\n"
1.137 schwarze 2327: " \"pageid\" INTEGER NOT NULL REFERENCES mpages(pageid) "
1.109 schwarze 2328: "ON DELETE CASCADE\n"
1.81 schwarze 2329: ");\n"
1.136 schwarze 2330: "CREATE INDEX mlinks_pageid_idx ON mlinks (pageid);\n"
1.81 schwarze 2331: "\n"
1.133 schwarze 2332: "CREATE TABLE \"names\" (\n"
2333: " \"bits\" INTEGER NOT NULL,\n"
2334: " \"name\" TEXT NOT NULL,\n"
1.137 schwarze 2335: " \"pageid\" INTEGER NOT NULL REFERENCES mpages(pageid) "
1.175 schwarze 2336: "ON DELETE CASCADE,\n"
2337: " UNIQUE (\"name\", \"pageid\") ON CONFLICT REPLACE\n"
1.133 schwarze 2338: ");\n"
2339: "\n"
1.50 kristaps 2340: "CREATE TABLE \"keys\" (\n"
2341: " \"bits\" INTEGER NOT NULL,\n"
2342: " \"key\" TEXT NOT NULL,\n"
1.137 schwarze 2343: " \"pageid\" INTEGER NOT NULL REFERENCES mpages(pageid) "
1.109 schwarze 2344: "ON DELETE CASCADE\n"
1.136 schwarze 2345: ");\n"
2346: "CREATE INDEX keys_pageid_idx ON keys (pageid);\n";
1.14 schwarze 2347:
1.50 kristaps 2348: if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, NULL)) {
1.59 schwarze 2349: exitcode = (int)MANDOCLEVEL_SYSERR;
1.115 schwarze 2350: say(MANDOC_DB, "%s", sqlite3_errmsg(db));
1.146 schwarze 2351: sqlite3_close(db);
1.50 kristaps 2352: return(0);
2353: }
1.4 kristaps 2354:
1.57 schwarze 2355: prepare_statements:
1.146 schwarze 2356: if (SQLITE_OK != sqlite3_exec(db,
2357: "PRAGMA foreign_keys = ON", NULL, NULL, NULL)) {
2358: exitcode = (int)MANDOCLEVEL_SYSERR;
2359: say(MANDOC_DB, "PRAGMA foreign_keys: %s",
2360: sqlite3_errmsg(db));
2361: sqlite3_close(db);
2362: return(0);
2363: }
2364:
1.137 schwarze 2365: sql = "DELETE FROM mpages WHERE pageid IN "
1.106 schwarze 2366: "(SELECT pageid FROM mlinks WHERE "
2367: "sec=? AND arch=? AND name=?)";
1.81 schwarze 2368: sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_DELETE_PAGE], NULL);
2369: sql = "INSERT INTO mpages "
1.132 schwarze 2370: "(desc,form) VALUES (?,?)";
1.81 schwarze 2371: sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_PAGE], NULL);
2372: sql = "INSERT INTO mlinks "
1.104 schwarze 2373: "(sec,arch,name,pageid) VALUES (?,?,?,?)";
1.81 schwarze 2374: sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_LINK], NULL);
1.175 schwarze 2375: sql = "SELECT bits FROM names where pageid = ?";
2376: sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_SELECT_NAME], NULL);
1.133 schwarze 2377: sql = "INSERT INTO names "
2378: "(bits,name,pageid) VALUES (?,?,?)";
2379: sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_NAME], NULL);
1.50 kristaps 2380: sql = "INSERT INTO keys "
1.81 schwarze 2381: "(bits,key,pageid) VALUES (?,?,?)";
1.50 kristaps 2382: sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_KEY], NULL);
1.70 schwarze 2383:
2384: #ifndef __APPLE__
2385: /*
2386: * When opening a new database, we can turn off
2387: * synchronous mode for much better performance.
2388: */
2389:
1.146 schwarze 2390: if (real && SQLITE_OK != sqlite3_exec(db,
2391: "PRAGMA synchronous = OFF", NULL, NULL, NULL)) {
2392: exitcode = (int)MANDOCLEVEL_SYSERR;
2393: say(MANDOC_DB, "PRAGMA synchronous: %s",
1.162 schwarze 2394: sqlite3_errmsg(db));
1.146 schwarze 2395: sqlite3_close(db);
2396: return(0);
2397: }
1.70 schwarze 2398: #endif
2399:
1.50 kristaps 2400: return(1);
2401: }
1.5 kristaps 2402:
1.50 kristaps 2403: static void *
1.152 schwarze 2404: hash_calloc(size_t n, size_t sz, void *arg)
1.50 kristaps 2405: {
1.12 schwarze 2406:
1.152 schwarze 2407: return(mandoc_calloc(n, sz));
1.50 kristaps 2408: }
1.12 schwarze 2409:
1.50 kristaps 2410: static void *
2411: hash_alloc(size_t sz, void *arg)
2412: {
1.5 kristaps 2413:
1.50 kristaps 2414: return(mandoc_malloc(sz));
2415: }
1.41 kristaps 2416:
1.50 kristaps 2417: static void
1.152 schwarze 2418: hash_free(void *p, void *arg)
1.50 kristaps 2419: {
1.4 kristaps 2420:
1.50 kristaps 2421: free(p);
1.4 kristaps 2422: }
2423:
1.50 kristaps 2424: static int
1.165 schwarze 2425: set_basedir(const char *targetdir, int report_baddir)
1.4 kristaps 2426: {
1.59 schwarze 2427: static char startdir[PATH_MAX];
1.151 schwarze 2428: static int getcwd_status; /* 1 = ok, 2 = failure */
2429: static int chdir_status; /* 1 = changed directory */
1.150 schwarze 2430: char *cp;
1.4 kristaps 2431:
1.59 schwarze 2432: /*
1.151 schwarze 2433: * Remember the original working directory, if possible.
2434: * This will be needed if the second or a later directory
2435: * on the command line is given as a relative path.
2436: * Do not error out if the current directory is not
2437: * searchable: Maybe it won't be needed after all.
2438: */
2439: if (0 == getcwd_status) {
2440: if (NULL == getcwd(startdir, sizeof(startdir))) {
2441: getcwd_status = 2;
2442: (void)strlcpy(startdir, strerror(errno),
2443: sizeof(startdir));
2444: } else
2445: getcwd_status = 1;
2446: }
2447:
2448: /*
2449: * We are leaving the old base directory.
2450: * Do not use it any longer, not even for messages.
2451: */
2452: *basedir = '\0';
2453:
2454: /*
2455: * If and only if the directory was changed earlier and
2456: * the next directory to process is given as a relative path,
2457: * first go back, or bail out if that is impossible.
1.59 schwarze 2458: */
1.151 schwarze 2459: if (chdir_status && '/' != *targetdir) {
2460: if (2 == getcwd_status) {
1.59 schwarze 2461: exitcode = (int)MANDOCLEVEL_SYSERR;
1.151 schwarze 2462: say("", "getcwd: %s", startdir);
1.59 schwarze 2463: return(0);
2464: }
1.151 schwarze 2465: if (-1 == chdir(startdir)) {
1.59 schwarze 2466: exitcode = (int)MANDOCLEVEL_SYSERR;
1.123 schwarze 2467: say("", "&chdir %s", startdir);
1.59 schwarze 2468: return(0);
2469: }
2470: }
1.151 schwarze 2471:
2472: /*
2473: * Always resolve basedir to the canonicalized absolute
2474: * pathname and append a trailing slash, such that
2475: * we can reliably check whether files are inside.
2476: */
1.59 schwarze 2477: if (NULL == realpath(targetdir, basedir)) {
1.165 schwarze 2478: if (report_baddir || errno != ENOENT) {
2479: exitcode = (int)MANDOCLEVEL_BADARG;
2480: say("", "&%s: realpath", targetdir);
2481: }
1.50 kristaps 2482: return(0);
1.59 schwarze 2483: } else if (-1 == chdir(basedir)) {
1.165 schwarze 2484: if (report_baddir || errno != ENOENT) {
2485: exitcode = (int)MANDOCLEVEL_BADARG;
2486: say("", "&chdir");
2487: }
1.50 kristaps 2488: return(0);
1.150 schwarze 2489: }
1.151 schwarze 2490: chdir_status = 1;
1.150 schwarze 2491: cp = strchr(basedir, '\0');
2492: if ('/' != cp[-1]) {
2493: if (cp - basedir >= PATH_MAX - 1) {
2494: exitcode = (int)MANDOCLEVEL_SYSERR;
2495: say("", "Filename too long");
2496: return(0);
2497: }
2498: *cp++ = '/';
2499: *cp = '\0';
1.4 kristaps 2500: }
1.50 kristaps 2501: return(1);
1.56 schwarze 2502: }
2503:
2504: static void
1.59 schwarze 2505: say(const char *file, const char *format, ...)
1.56 schwarze 2506: {
2507: va_list ap;
1.123 schwarze 2508: int use_errno;
1.56 schwarze 2509:
1.59 schwarze 2510: if ('\0' != *basedir)
2511: fprintf(stderr, "%s", basedir);
2512: if ('\0' != *basedir && '\0' != *file)
1.151 schwarze 2513: fputc('/', stderr);
1.56 schwarze 2514: if ('\0' != *file)
1.59 schwarze 2515: fprintf(stderr, "%s", file);
2516:
1.123 schwarze 2517: use_errno = 1;
2518: if (NULL != format) {
2519: switch (*format) {
1.141 schwarze 2520: case '&':
1.123 schwarze 2521: format++;
2522: break;
1.141 schwarze 2523: case '\0':
1.123 schwarze 2524: format = NULL;
2525: break;
2526: default:
2527: use_errno = 0;
2528: break;
2529: }
2530: }
2531: if (NULL != format) {
2532: if ('\0' != *basedir || '\0' != *file)
2533: fputs(": ", stderr);
2534: va_start(ap, format);
2535: vfprintf(stderr, format, ap);
2536: va_end(ap);
2537: }
2538: if (use_errno) {
2539: if ('\0' != *basedir || '\0' != *file || NULL != format)
2540: fputs(": ", stderr);
1.59 schwarze 2541: perror(NULL);
1.123 schwarze 2542: } else
2543: fputc('\n', stderr);
1.1 kristaps 2544: }
CVSweb