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