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