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