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