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