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