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