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