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