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