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