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