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