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