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