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