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