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