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