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