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