Annotation of mandoc/mandocdb.c, Revision 1.234
1.234 ! schwarze 1: /* $Id: mandocdb.c,v 1.233 2016/10/20 16:31:00 schwarze Exp $ */
1.1 kristaps 2: /*
1.48 schwarze 3: * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.211 schwarze 4: * Copyright (c) 2011-2016 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.1 kristaps 113: };
1.219 schwarze 114:
115:
116: int mandocdb(int, char *[]);
1.1 kristaps 117:
1.221 schwarze 118: static void dbadd(struct dba *, struct mpage *);
1.118 schwarze 119: static void dbadd_mlink(const struct mlink *mlink);
1.221 schwarze 120: static void dbprune(struct dba *);
121: static void dbwrite(struct dba *);
1.59 schwarze 122: static void filescan(const char *);
1.232 schwarze 123: #if HAVE_FTS_COMPARE_CONST
124: static int fts_compare(const FTSENT *const *, const FTSENT *const *);
125: #else
1.231 schwarze 126: static int fts_compare(const FTSENT **, const FTSENT **);
1.232 schwarze 127: #endif
1.84 schwarze 128: static void mlink_add(struct mlink *, const struct stat *);
1.131 schwarze 129: static void mlink_check(struct mpage *, struct mlink *);
1.82 schwarze 130: static void mlink_free(struct mlink *);
1.89 schwarze 131: static void mlinks_undupe(struct mpage *);
1.78 schwarze 132: static void mpages_free(void);
1.221 schwarze 133: static void mpages_merge(struct dba *, struct mparse *);
1.124 schwarze 134: static void parse_cat(struct mpage *, int);
1.190 schwarze 135: static void parse_man(struct mpage *, const struct roff_meta *,
1.189 schwarze 136: const struct roff_node *);
1.190 schwarze 137: static void parse_mdoc(struct mpage *, const struct roff_meta *,
1.189 schwarze 138: const struct roff_node *);
1.190 schwarze 139: static int parse_mdoc_head(struct mpage *, const struct roff_meta *,
1.189 schwarze 140: const struct roff_node *);
1.190 schwarze 141: static int parse_mdoc_Fd(struct mpage *, const struct roff_meta *,
1.189 schwarze 142: const struct roff_node *);
143: static void parse_mdoc_fname(struct mpage *, const struct roff_node *);
1.190 schwarze 144: static int parse_mdoc_Fn(struct mpage *, const struct roff_meta *,
1.189 schwarze 145: const struct roff_node *);
1.190 schwarze 146: static int parse_mdoc_Fo(struct mpage *, const struct roff_meta *,
1.189 schwarze 147: const struct roff_node *);
1.190 schwarze 148: static int parse_mdoc_Nd(struct mpage *, const struct roff_meta *,
1.189 schwarze 149: const struct roff_node *);
1.190 schwarze 150: static int parse_mdoc_Nm(struct mpage *, const struct roff_meta *,
1.189 schwarze 151: const struct roff_node *);
1.190 schwarze 152: static int parse_mdoc_Sh(struct mpage *, const struct roff_meta *,
1.189 schwarze 153: const struct roff_node *);
1.211 schwarze 154: static int parse_mdoc_Va(struct mpage *, const struct roff_meta *,
155: const struct roff_node *);
1.190 schwarze 156: static int parse_mdoc_Xr(struct mpage *, const struct roff_meta *,
1.189 schwarze 157: const struct roff_node *);
1.112 schwarze 158: static void putkey(const struct mpage *, char *, uint64_t);
1.176 schwarze 159: static void putkeys(const struct mpage *, char *, size_t, uint64_t);
1.78 schwarze 160: static void putmdockey(const struct mpage *,
1.189 schwarze 161: const struct roff_node *, uint64_t);
1.176 schwarze 162: static int render_string(char **, size_t *);
1.220 schwarze 163: static void say(const char *, const char *, ...)
164: __attribute__((__format__ (printf, 2, 3)));
1.165 schwarze 165: static int set_basedir(const char *, int);
1.59 schwarze 166: static int treescan(void);
1.50 kristaps 167: static size_t utf8(unsigned int, char [7]);
168:
1.102 schwarze 169: static int nodb; /* no database changes */
1.116 schwarze 170: static int mparse_options; /* abort the parse early */
1.141 schwarze 171: static int use_all; /* use all found files */
172: static int debug; /* print what we're doing */
173: static int warnings; /* warn about crap */
1.95 schwarze 174: static int write_utf8; /* write UTF-8 output; else ASCII */
1.59 schwarze 175: static int exitcode; /* to be returned by main */
1.141 schwarze 176: static enum op op; /* operational mode */
1.59 schwarze 177: static char basedir[PATH_MAX]; /* current base directory */
1.231 schwarze 178: static struct mpage *mpage_head; /* list of distinct manual pages */
1.78 schwarze 179: static struct ohash mpages; /* table of distinct manual pages */
1.83 schwarze 180: static struct ohash mlinks; /* table of directory entries */
1.133 schwarze 181: static struct ohash names; /* table of all names */
1.50 kristaps 182: static struct ohash strings; /* table of all strings */
1.133 schwarze 183: static uint64_t name_mask;
1.25 schwarze 184:
185: static const struct mdoc_handler mdocs[MDOC_MAX] = {
1.68 schwarze 186: { NULL, 0 }, /* Ap */
187: { NULL, 0 }, /* Dd */
188: { NULL, 0 }, /* Dt */
189: { NULL, 0 }, /* Os */
190: { parse_mdoc_Sh, TYPE_Sh }, /* Sh */
191: { parse_mdoc_head, TYPE_Ss }, /* Ss */
192: { NULL, 0 }, /* Pp */
193: { NULL, 0 }, /* D1 */
194: { NULL, 0 }, /* Dl */
195: { NULL, 0 }, /* Bd */
196: { NULL, 0 }, /* Ed */
197: { NULL, 0 }, /* Bl */
198: { NULL, 0 }, /* El */
199: { NULL, 0 }, /* It */
200: { NULL, 0 }, /* Ad */
201: { NULL, TYPE_An }, /* An */
202: { NULL, TYPE_Ar }, /* Ar */
203: { NULL, TYPE_Cd }, /* Cd */
204: { NULL, TYPE_Cm }, /* Cm */
205: { NULL, TYPE_Dv }, /* Dv */
206: { NULL, TYPE_Er }, /* Er */
207: { NULL, TYPE_Ev }, /* Ev */
208: { NULL, 0 }, /* Ex */
209: { NULL, TYPE_Fa }, /* Fa */
210: { parse_mdoc_Fd, 0 }, /* Fd */
211: { NULL, TYPE_Fl }, /* Fl */
212: { parse_mdoc_Fn, 0 }, /* Fn */
213: { NULL, TYPE_Ft }, /* Ft */
214: { NULL, TYPE_Ic }, /* Ic */
1.92 schwarze 215: { NULL, TYPE_In }, /* In */
1.68 schwarze 216: { NULL, TYPE_Li }, /* Li */
1.132 schwarze 217: { parse_mdoc_Nd, 0 }, /* Nd */
1.129 schwarze 218: { parse_mdoc_Nm, 0 }, /* Nm */
1.68 schwarze 219: { NULL, 0 }, /* Op */
220: { NULL, 0 }, /* Ot */
221: { NULL, TYPE_Pa }, /* Pa */
222: { NULL, 0 }, /* Rv */
1.92 schwarze 223: { NULL, TYPE_St }, /* St */
1.211 schwarze 224: { parse_mdoc_Va, TYPE_Va }, /* Va */
225: { parse_mdoc_Va, TYPE_Vt }, /* Vt */
1.68 schwarze 226: { parse_mdoc_Xr, 0 }, /* Xr */
227: { NULL, 0 }, /* %A */
228: { NULL, 0 }, /* %B */
229: { NULL, 0 }, /* %D */
230: { NULL, 0 }, /* %I */
231: { NULL, 0 }, /* %J */
232: { NULL, 0 }, /* %N */
233: { NULL, 0 }, /* %O */
234: { NULL, 0 }, /* %P */
235: { NULL, 0 }, /* %R */
236: { NULL, 0 }, /* %T */
237: { NULL, 0 }, /* %V */
238: { NULL, 0 }, /* Ac */
239: { NULL, 0 }, /* Ao */
240: { NULL, 0 }, /* Aq */
241: { NULL, TYPE_At }, /* At */
242: { NULL, 0 }, /* Bc */
243: { NULL, 0 }, /* Bf */
244: { NULL, 0 }, /* Bo */
245: { NULL, 0 }, /* Bq */
246: { NULL, TYPE_Bsx }, /* Bsx */
247: { NULL, TYPE_Bx }, /* Bx */
248: { NULL, 0 }, /* Db */
249: { NULL, 0 }, /* Dc */
250: { NULL, 0 }, /* Do */
251: { NULL, 0 }, /* Dq */
252: { NULL, 0 }, /* Ec */
253: { NULL, 0 }, /* Ef */
254: { NULL, TYPE_Em }, /* Em */
255: { NULL, 0 }, /* Eo */
256: { NULL, TYPE_Fx }, /* Fx */
257: { NULL, TYPE_Ms }, /* Ms */
258: { NULL, 0 }, /* No */
259: { NULL, 0 }, /* Ns */
260: { NULL, TYPE_Nx }, /* Nx */
261: { NULL, TYPE_Ox }, /* Ox */
262: { NULL, 0 }, /* Pc */
263: { NULL, 0 }, /* Pf */
264: { NULL, 0 }, /* Po */
265: { NULL, 0 }, /* Pq */
266: { NULL, 0 }, /* Qc */
267: { NULL, 0 }, /* Ql */
268: { NULL, 0 }, /* Qo */
269: { NULL, 0 }, /* Qq */
270: { NULL, 0 }, /* Re */
271: { NULL, 0 }, /* Rs */
272: { NULL, 0 }, /* Sc */
273: { NULL, 0 }, /* So */
274: { NULL, 0 }, /* Sq */
275: { NULL, 0 }, /* Sm */
276: { NULL, 0 }, /* Sx */
277: { NULL, TYPE_Sy }, /* Sy */
278: { NULL, TYPE_Tn }, /* Tn */
279: { NULL, 0 }, /* Ux */
280: { NULL, 0 }, /* Xc */
281: { NULL, 0 }, /* Xo */
1.173 schwarze 282: { parse_mdoc_Fo, 0 }, /* Fo */
1.68 schwarze 283: { NULL, 0 }, /* Fc */
284: { NULL, 0 }, /* Oo */
285: { NULL, 0 }, /* Oc */
286: { NULL, 0 }, /* Bk */
287: { NULL, 0 }, /* Ek */
288: { NULL, 0 }, /* Bt */
289: { NULL, 0 }, /* Hf */
290: { NULL, 0 }, /* Fr */
291: { NULL, 0 }, /* Ud */
292: { NULL, TYPE_Lb }, /* Lb */
293: { NULL, 0 }, /* Lp */
294: { NULL, TYPE_Lk }, /* Lk */
295: { NULL, TYPE_Mt }, /* Mt */
296: { NULL, 0 }, /* Brq */
297: { NULL, 0 }, /* Bro */
298: { NULL, 0 }, /* Brc */
299: { NULL, 0 }, /* %C */
300: { NULL, 0 }, /* Es */
301: { NULL, 0 }, /* En */
302: { NULL, TYPE_Dx }, /* Dx */
303: { NULL, 0 }, /* %Q */
304: { NULL, 0 }, /* br */
305: { NULL, 0 }, /* sp */
306: { NULL, 0 }, /* %U */
307: { NULL, 0 }, /* Ta */
1.166 schwarze 308: { NULL, 0 }, /* ll */
1.1 kristaps 309: };
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] = ".";
591: argv[1] = (char *)NULL;
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;
871: }
1.17 schwarze 872:
1.50 kristaps 873: /*
874: * First try to guess our directory structure.
875: * If we find a separator, try to look for man* or cat*.
876: * If we find one of these and what's underneath is a directory,
877: * assume it's an architecture.
878: */
879: if (NULL != (p = strchr(start, '/'))) {
880: *p++ = '\0';
881: if (0 == strncmp(start, "man", 3)) {
1.84 schwarze 882: mlink->dform = FORM_SRC;
1.94 schwarze 883: mlink->dsec = start + 3;
1.50 kristaps 884: } else if (0 == strncmp(start, "cat", 3)) {
1.84 schwarze 885: mlink->dform = FORM_CAT;
1.94 schwarze 886: mlink->dsec = start + 3;
1.17 schwarze 887: }
1.5 kristaps 888:
1.50 kristaps 889: start = p;
1.84 schwarze 890: if (NULL != mlink->dsec && NULL != (p = strchr(start, '/'))) {
1.50 kristaps 891: *p++ = '\0';
1.94 schwarze 892: mlink->arch = start;
1.50 kristaps 893: start = p;
1.84 schwarze 894: }
1.50 kristaps 895: }
896:
897: /*
898: * Now check the file suffix.
899: * Suffix of `.0' indicates a catpage, `.1-9' is a manpage.
900: */
901: p = strrchr(start, '\0');
902: while (p-- > start && '/' != *p && '.' != *p)
903: /* Loop. */ ;
904:
905: if ('.' == *p) {
906: *p++ = '\0';
1.94 schwarze 907: mlink->fsec = p;
1.5 kristaps 908: }
909:
1.10 kristaps 910: /*
1.50 kristaps 911: * Now try to parse the name.
912: * Use the filename portion of the path.
1.10 kristaps 913: */
1.84 schwarze 914: mlink->name = start;
1.50 kristaps 915: if (NULL != (p = strrchr(start, '/'))) {
1.84 schwarze 916: mlink->name = p + 1;
1.50 kristaps 917: *p = '\0';
1.84 schwarze 918: }
919: mlink_add(mlink, &st);
1.50 kristaps 920: }
1.5 kristaps 921:
1.50 kristaps 922: static void
1.84 schwarze 923: mlink_add(struct mlink *mlink, const struct stat *st)
1.50 kristaps 924: {
1.83 schwarze 925: struct inodev inodev;
1.78 schwarze 926: struct mpage *mpage;
1.83 schwarze 927: unsigned int slot;
1.50 kristaps 928:
1.84 schwarze 929: assert(NULL != mlink->file);
1.50 kristaps 930:
1.94 schwarze 931: mlink->dsec = mandoc_strdup(mlink->dsec ? mlink->dsec : "");
932: mlink->arch = mandoc_strdup(mlink->arch ? mlink->arch : "");
933: mlink->name = mandoc_strdup(mlink->name ? mlink->name : "");
934: mlink->fsec = mandoc_strdup(mlink->fsec ? mlink->fsec : "");
1.84 schwarze 935:
936: if ('0' == *mlink->fsec) {
937: free(mlink->fsec);
938: mlink->fsec = mandoc_strdup(mlink->dsec);
939: mlink->fform = FORM_CAT;
940: } else if ('1' <= *mlink->fsec && '9' >= *mlink->fsec)
941: mlink->fform = FORM_SRC;
1.74 schwarze 942: else
1.84 schwarze 943: mlink->fform = FORM_NONE;
1.82 schwarze 944:
1.83 schwarze 945: slot = ohash_qlookup(&mlinks, mlink->file);
946: assert(NULL == ohash_find(&mlinks, slot));
947: ohash_insert(&mlinks, slot, mlink);
948:
1.186 schwarze 949: memset(&inodev, 0, sizeof(inodev)); /* Clear padding. */
1.83 schwarze 950: inodev.st_ino = st->st_ino;
951: inodev.st_dev = st->st_dev;
952: slot = ohash_lookup_memory(&mpages, (char *)&inodev,
953: sizeof(struct inodev), inodev.st_ino);
954: mpage = ohash_find(&mpages, slot);
955: if (NULL == mpage) {
956: mpage = mandoc_calloc(1, sizeof(struct mpage));
957: mpage->inodev.st_ino = inodev.st_ino;
958: mpage->inodev.st_dev = inodev.st_dev;
1.221 schwarze 959: mpage->form = FORM_NONE;
1.231 schwarze 960: mpage->next = mpage_head;
961: mpage_head = mpage;
1.83 schwarze 962: ohash_insert(&mpages, slot, mpage);
963: } else
1.85 schwarze 964: mlink->next = mpage->mlinks;
1.82 schwarze 965: mpage->mlinks = mlink;
1.118 schwarze 966: mlink->mpage = mpage;
1.82 schwarze 967: }
968:
969: static void
970: mlink_free(struct mlink *mlink)
971: {
972:
973: free(mlink->dsec);
974: free(mlink->arch);
975: free(mlink->name);
976: free(mlink->fsec);
977: free(mlink);
1.50 kristaps 978: }
1.3 kristaps 979:
1.50 kristaps 980: static void
1.78 schwarze 981: mpages_free(void)
1.50 kristaps 982: {
1.78 schwarze 983: struct mpage *mpage;
1.82 schwarze 984: struct mlink *mlink;
1.3 kristaps 985:
1.231 schwarze 986: while ((mpage = mpage_head) != NULL) {
987: while ((mlink = mpage->mlinks) != NULL) {
1.85 schwarze 988: mpage->mlinks = mlink->next;
1.82 schwarze 989: mlink_free(mlink);
990: }
1.231 schwarze 991: mpage_head = mpage->next;
1.78 schwarze 992: free(mpage->sec);
993: free(mpage->arch);
1.82 schwarze 994: free(mpage->title);
995: free(mpage->desc);
1.78 schwarze 996: free(mpage);
1.50 kristaps 997: }
998: }
1.38 schwarze 999:
1.50 kristaps 1000: /*
1.89 schwarze 1001: * For each mlink to the mpage, check whether the path looks like
1002: * it is formatted, and if it does, check whether a source manual
1003: * exists by the same name, ignoring the suffix.
1004: * If both conditions hold, drop the mlink.
1005: */
1006: static void
1007: mlinks_undupe(struct mpage *mpage)
1008: {
1009: char buf[PATH_MAX];
1010: struct mlink **prev;
1011: struct mlink *mlink;
1012: char *bufp;
1013:
1014: mpage->form = FORM_CAT;
1.90 schwarze 1015: prev = &mpage->mlinks;
1016: while (NULL != (mlink = *prev)) {
1.89 schwarze 1017: if (FORM_CAT != mlink->dform) {
1018: mpage->form = FORM_NONE;
1.90 schwarze 1019: goto nextlink;
1.89 schwarze 1020: }
1.143 schwarze 1021: (void)strlcpy(buf, mlink->file, sizeof(buf));
1.89 schwarze 1022: bufp = strstr(buf, "cat");
1023: assert(NULL != bufp);
1024: memcpy(bufp, "man", 3);
1025: if (NULL != (bufp = strrchr(buf, '.')))
1026: *++bufp = '\0';
1.143 schwarze 1027: (void)strlcat(buf, mlink->dsec, sizeof(buf));
1.89 schwarze 1028: if (NULL == ohash_find(&mlinks,
1.141 schwarze 1029: ohash_qlookup(&mlinks, buf)))
1.90 schwarze 1030: goto nextlink;
1.89 schwarze 1031: if (warnings)
1032: say(mlink->file, "Man source exists: %s", buf);
1033: if (use_all)
1.90 schwarze 1034: goto nextlink;
1.89 schwarze 1035: *prev = mlink->next;
1036: mlink_free(mlink);
1.90 schwarze 1037: continue;
1038: nextlink:
1039: prev = &(*prev)->next;
1.89 schwarze 1040: }
1041: }
1042:
1.131 schwarze 1043: static void
1.93 schwarze 1044: mlink_check(struct mpage *mpage, struct mlink *mlink)
1045: {
1.131 schwarze 1046: struct str *str;
1047: unsigned int slot;
1.93 schwarze 1048:
1049: /*
1050: * Check whether the manual section given in a file
1051: * agrees with the directory where the file is located.
1052: * Some manuals have suffixes like (3p) on their
1053: * section number either inside the file or in the
1054: * directory name, some are linked into more than one
1055: * section, like encrypt(1) = makekey(8).
1056: */
1057:
1058: if (FORM_SRC == mpage->form &&
1.131 schwarze 1059: strcasecmp(mpage->sec, mlink->dsec))
1.93 schwarze 1060: say(mlink->file, "Section \"%s\" manual in %s directory",
1061: mpage->sec, mlink->dsec);
1062:
1063: /*
1064: * Manual page directories exist for each kernel
1065: * architecture as returned by machine(1).
1066: * However, many manuals only depend on the
1067: * application architecture as returned by arch(1).
1068: * For example, some (2/ARM) manuals are shared
1069: * across the "armish" and "zaurus" kernel
1070: * architectures.
1071: * A few manuals are even shared across completely
1072: * different architectures, for example fdformat(1)
1.229 schwarze 1073: * on amd64, i386, and sparc64.
1.93 schwarze 1074: */
1075:
1.131 schwarze 1076: if (strcasecmp(mpage->arch, mlink->arch))
1.93 schwarze 1077: say(mlink->file, "Architecture \"%s\" manual in "
1078: "\"%s\" directory", mpage->arch, mlink->arch);
1079:
1.131 schwarze 1080: /*
1081: * XXX
1.133 schwarze 1082: * parse_cat() doesn't set NAME_TITLE yet.
1.131 schwarze 1083: */
1084:
1085: if (FORM_CAT == mpage->form)
1086: return;
1087:
1088: /*
1089: * Check whether this mlink
1090: * appears as a name in the NAME section.
1091: */
1.93 schwarze 1092:
1.133 schwarze 1093: slot = ohash_qlookup(&names, mlink->name);
1094: str = ohash_find(&names, slot);
1.131 schwarze 1095: assert(NULL != str);
1.133 schwarze 1096: if ( ! (NAME_TITLE & str->mask))
1.131 schwarze 1097: say(mlink->file, "Name missing in NAME section");
1.93 schwarze 1098: }
1099:
1.89 schwarze 1100: /*
1.80 schwarze 1101: * Run through the files in the global vector "mpages"
1102: * and add them to the database specified in "basedir".
1.50 kristaps 1103: *
1104: * This handles the parsing scheme itself, using the cues of directory
1105: * and filename to determine whether the file is parsable or not.
1106: */
1.59 schwarze 1107: static void
1.221 schwarze 1108: mpages_merge(struct dba *dba, struct mparse *mp)
1.50 kristaps 1109: {
1.231 schwarze 1110: struct mpage *mpage, *mpage_dest;
1.118 schwarze 1111: struct mlink *mlink, *mlink_dest;
1.191 schwarze 1112: struct roff_man *man;
1.118 schwarze 1113: char *sodest;
1.112 schwarze 1114: char *cp;
1.162 schwarze 1115: int fd;
1.69 schwarze 1116:
1.231 schwarze 1117: for (mpage = mpage_head; mpage != NULL; mpage = mpage->next) {
1.89 schwarze 1118: mlinks_undupe(mpage);
1.231 schwarze 1119: if ((mlink = mpage->mlinks) == NULL)
1120: continue;
1.50 kristaps 1121:
1.133 schwarze 1122: name_mask = NAME_MASK;
1.202 schwarze 1123: mandoc_ohash_init(&names, 4, offsetof(struct str, key));
1124: mandoc_ohash_init(&strings, 6, offsetof(struct str, key));
1.50 kristaps 1125: mparse_reset(mp);
1126: man = NULL;
1.124 schwarze 1127: sodest = NULL;
1128:
1.213 schwarze 1129: if ((fd = mparse_open(mp, mlink->file)) == -1) {
1.182 schwarze 1130: say(mlink->file, "&open");
1.162 schwarze 1131: goto nextpage;
1.124 schwarze 1132: }
1.14 schwarze 1133:
1134: /*
1.183 schwarze 1135: * Interpret the file as mdoc(7) or man(7) source
1136: * code, unless it is known to be formatted.
1.14 schwarze 1137: */
1.182 schwarze 1138: if (mlink->dform != FORM_CAT || mlink->fform != FORM_CAT) {
1.183 schwarze 1139: mparse_readfd(mp, fd, mlink->file);
1.212 schwarze 1140: close(fd);
1.192 schwarze 1141: mparse_result(mp, &man, &sodest);
1.76 schwarze 1142: }
1.14 schwarze 1143:
1.171 schwarze 1144: if (sodest != NULL) {
1.118 schwarze 1145: mlink_dest = ohash_find(&mlinks,
1146: ohash_qlookup(&mlinks, sodest));
1.171 schwarze 1147: if (mlink_dest == NULL) {
1148: mandoc_asprintf(&cp, "%s.gz", sodest);
1149: mlink_dest = ohash_find(&mlinks,
1150: ohash_qlookup(&mlinks, cp));
1151: free(cp);
1152: }
1153: if (mlink_dest != NULL) {
1.118 schwarze 1154:
1155: /* The .so target exists. */
1156:
1157: mpage_dest = mlink_dest->mpage;
1158: while (1) {
1159: mlink->mpage = mpage_dest;
1160:
1161: /*
1162: * If the target was already
1163: * processed, add the links
1164: * to the database now.
1165: * Otherwise, this will
1166: * happen when we come
1167: * to the target.
1168: */
1169:
1.221 schwarze 1170: if (mpage_dest->dba != NULL)
1171: dbadd_mlink(mlink);
1.118 schwarze 1172:
1.171 schwarze 1173: if (mlink->next == NULL)
1.118 schwarze 1174: break;
1175: mlink = mlink->next;
1176: }
1177:
1178: /* Move all links to the target. */
1179:
1180: mlink->next = mlink_dest->next;
1181: mlink_dest->next = mpage->mlinks;
1182: mpage->mlinks = NULL;
1183: }
1.124 schwarze 1184: goto nextpage;
1.192 schwarze 1185: } else if (man != NULL && man->macroset == MACROSET_MDOC) {
1.204 schwarze 1186: mdoc_validate(man);
1.82 schwarze 1187: mpage->form = FORM_SRC;
1.193 schwarze 1188: mpage->sec = man->meta.msec;
1.155 schwarze 1189: mpage->sec = mandoc_strdup(
1.171 schwarze 1190: mpage->sec == NULL ? "" : mpage->sec);
1.193 schwarze 1191: mpage->arch = man->meta.arch;
1.84 schwarze 1192: mpage->arch = mandoc_strdup(
1.171 schwarze 1193: mpage->arch == NULL ? "" : mpage->arch);
1.193 schwarze 1194: mpage->title = mandoc_strdup(man->meta.title);
1.192 schwarze 1195: } else if (man != NULL && man->macroset == MACROSET_MAN) {
1.205 schwarze 1196: man_validate(man);
1.82 schwarze 1197: mpage->form = FORM_SRC;
1.193 schwarze 1198: mpage->sec = mandoc_strdup(man->meta.msec);
1.182 schwarze 1199: mpage->arch = mandoc_strdup(mlink->arch);
1.193 schwarze 1200: mpage->title = mandoc_strdup(man->meta.title);
1.76 schwarze 1201: } else {
1.82 schwarze 1202: mpage->form = FORM_CAT;
1.182 schwarze 1203: mpage->sec = mandoc_strdup(mlink->dsec);
1204: mpage->arch = mandoc_strdup(mlink->arch);
1205: mpage->title = mandoc_strdup(mlink->name);
1.76 schwarze 1206: }
1.12 schwarze 1207:
1.174 schwarze 1208: assert(mpage->desc == NULL);
1.192 schwarze 1209: if (man != NULL && man->macroset == MACROSET_MDOC)
1.193 schwarze 1210: parse_mdoc(mpage, &man->meta, man->first);
1.174 schwarze 1211: else if (man != NULL)
1.193 schwarze 1212: parse_man(mpage, &man->meta, man->first);
1.50 kristaps 1213: else
1.162 schwarze 1214: parse_cat(mpage, fd);
1.174 schwarze 1215: if (mpage->desc == NULL)
1.135 schwarze 1216: mpage->desc = mandoc_strdup(mpage->mlinks->name);
1.131 schwarze 1217:
1218: if (warnings && !use_all)
1219: for (mlink = mpage->mlinks; mlink;
1220: mlink = mlink->next)
1221: mlink_check(mpage, mlink);
1.44 kristaps 1222:
1.221 schwarze 1223: dbadd(dba, mpage);
1.182 schwarze 1224: mlink = mpage->mlinks;
1.124 schwarze 1225:
1226: nextpage:
1.66 schwarze 1227: ohash_delete(&strings);
1.133 schwarze 1228: ohash_delete(&names);
1.50 kristaps 1229: }
1230: }
1.12 schwarze 1231:
1.50 kristaps 1232: static void
1.124 schwarze 1233: parse_cat(struct mpage *mpage, int fd)
1.50 kristaps 1234: {
1235: FILE *stream;
1236: char *line, *p, *title;
1.209 schwarze 1237: size_t linesz, plen, titlesz;
1238: ssize_t len;
1239: int offs;
1.12 schwarze 1240:
1.124 schwarze 1241: stream = (-1 == fd) ?
1242: fopen(mpage->mlinks->file, "r") :
1243: fdopen(fd, "r");
1244: if (NULL == stream) {
1.159 schwarze 1245: if (-1 != fd)
1246: close(fd);
1.56 schwarze 1247: if (warnings)
1.123 schwarze 1248: say(mpage->mlinks->file, "&fopen");
1.50 kristaps 1249: return;
1250: }
1.1 kristaps 1251:
1.209 schwarze 1252: line = NULL;
1253: linesz = 0;
1254:
1.50 kristaps 1255: /* Skip to first blank line. */
1.1 kristaps 1256:
1.209 schwarze 1257: while (getline(&line, &linesz, stream) != -1)
1258: if (*line == '\n')
1.50 kristaps 1259: break;
1.1 kristaps 1260:
1.50 kristaps 1261: /*
1262: * Assume the first line that is not indented
1263: * is the first section header. Skip to it.
1264: */
1.1 kristaps 1265:
1.209 schwarze 1266: while (getline(&line, &linesz, stream) != -1)
1267: if (*line != '\n' && *line != ' ')
1.50 kristaps 1268: break;
1.141 schwarze 1269:
1.50 kristaps 1270: /*
1271: * Read up until the next section into a buffer.
1272: * Strip the leading and trailing newline from each read line,
1273: * appending a trailing space.
1274: * Ignore empty (whitespace-only) lines.
1275: */
1.1 kristaps 1276:
1.50 kristaps 1277: titlesz = 0;
1278: title = NULL;
1.38 schwarze 1279:
1.209 schwarze 1280: while ((len = getline(&line, &linesz, stream)) != -1) {
1281: if (*line != ' ')
1.50 kristaps 1282: break;
1.209 schwarze 1283: offs = 0;
1284: while (isspace((unsigned char)line[offs]))
1285: offs++;
1286: if (line[offs] == '\0')
1.38 schwarze 1287: continue;
1.209 schwarze 1288: title = mandoc_realloc(title, titlesz + len - offs);
1289: memcpy(title + titlesz, line + offs, len - offs);
1290: titlesz += len - offs;
1.50 kristaps 1291: title[titlesz - 1] = ' ';
1292: }
1.209 schwarze 1293: free(line);
1.38 schwarze 1294:
1.50 kristaps 1295: /*
1296: * If no page content can be found, or the input line
1297: * is already the next section header, or there is no
1298: * trailing newline, reuse the page title as the page
1299: * description.
1300: */
1.44 kristaps 1301:
1.50 kristaps 1302: if (NULL == title || '\0' == *title) {
1.56 schwarze 1303: if (warnings)
1.82 schwarze 1304: say(mpage->mlinks->file,
1305: "Cannot find NAME section");
1.50 kristaps 1306: fclose(stream);
1307: free(title);
1308: return;
1309: }
1.1 kristaps 1310:
1.209 schwarze 1311: title[titlesz - 1] = '\0';
1.33 schwarze 1312:
1.50 kristaps 1313: /*
1314: * Skip to the first dash.
1315: * Use the remaining line as the description (no more than 70
1316: * bytes).
1317: */
1.33 schwarze 1318:
1.50 kristaps 1319: if (NULL != (p = strstr(title, "- "))) {
1320: for (p += 2; ' ' == *p || '\b' == *p; p++)
1321: /* Skip to next word. */ ;
1322: } else {
1.56 schwarze 1323: if (warnings)
1.82 schwarze 1324: say(mpage->mlinks->file,
1325: "No dash in title line");
1.50 kristaps 1326: p = title;
1327: }
1.38 schwarze 1328:
1.50 kristaps 1329: plen = strlen(p);
1.1 kristaps 1330:
1.50 kristaps 1331: /* Strip backspace-encoding from line. */
1.1 kristaps 1332:
1.50 kristaps 1333: while (NULL != (line = memchr(p, '\b', plen))) {
1334: len = line - p;
1335: if (0 == len) {
1336: memmove(line, line + 1, plen--);
1337: continue;
1.141 schwarze 1338: }
1.50 kristaps 1339: memmove(line - 1, line + 1, plen - len);
1340: plen -= 2;
1341: }
1.1 kristaps 1342:
1.78 schwarze 1343: mpage->desc = mandoc_strdup(p);
1.50 kristaps 1344: fclose(stream);
1345: free(title);
1346: }
1.1 kristaps 1347:
1.50 kristaps 1348: /*
1349: * Put a type/word pair into the word database for this particular file.
1350: */
1351: static void
1.112 schwarze 1352: putkey(const struct mpage *mpage, char *value, uint64_t type)
1.50 kristaps 1353: {
1.78 schwarze 1354: putkeys(mpage, value, strlen(value), type);
1.3 kristaps 1355: }
1356:
1357: /*
1.50 kristaps 1358: * Grok all nodes at or below a certain mdoc node into putkey().
1.3 kristaps 1359: */
1360: static void
1.78 schwarze 1361: putmdockey(const struct mpage *mpage,
1.189 schwarze 1362: const struct roff_node *n, uint64_t m)
1.3 kristaps 1363: {
1.18 kristaps 1364:
1.50 kristaps 1365: for ( ; NULL != n; n = n->next) {
1366: if (NULL != n->child)
1.78 schwarze 1367: putmdockey(mpage, n->child, m);
1.188 schwarze 1368: if (n->type == ROFFT_TEXT)
1.78 schwarze 1369: putkey(mpage, n->string, m);
1.50 kristaps 1370: }
1371: }
1.18 kristaps 1372:
1.61 schwarze 1373: static void
1.190 schwarze 1374: parse_man(struct mpage *mpage, const struct roff_meta *meta,
1.189 schwarze 1375: const struct roff_node *n)
1.50 kristaps 1376: {
1.189 schwarze 1377: const struct roff_node *head, *body;
1.121 schwarze 1378: char *start, *title;
1.50 kristaps 1379: char byte;
1.121 schwarze 1380: size_t sz;
1.18 kristaps 1381:
1.215 schwarze 1382: if (n == NULL)
1.61 schwarze 1383: return;
1.18 kristaps 1384:
1.50 kristaps 1385: /*
1386: * We're only searching for one thing: the first text child in
1387: * the BODY of a NAME section. Since we don't keep track of
1388: * sections in -man, run some hoops to find out whether we're in
1389: * the correct section or not.
1390: */
1.18 kristaps 1391:
1.188 schwarze 1392: if (n->type == ROFFT_BODY && n->tok == MAN_SH) {
1.50 kristaps 1393: body = n;
1.215 schwarze 1394: if ((head = body->parent->head) != NULL &&
1395: (head = head->child) != NULL &&
1396: head->next == NULL &&
1.188 schwarze 1397: head->type == ROFFT_TEXT &&
1.215 schwarze 1398: strcmp(head->string, "NAME") == 0 &&
1399: body->child != NULL) {
1.3 kristaps 1400:
1.50 kristaps 1401: /*
1402: * Suck the entire NAME section into memory.
1403: * Yes, we might run away.
1404: * But too many manuals have big, spread-out
1405: * NAME sections over many lines.
1406: */
1.3 kristaps 1407:
1.121 schwarze 1408: title = NULL;
1.194 schwarze 1409: deroff(&title, body);
1.50 kristaps 1410: if (NULL == title)
1.61 schwarze 1411: return;
1.18 kristaps 1412:
1.141 schwarze 1413: /*
1.50 kristaps 1414: * Go through a special heuristic dance here.
1415: * Conventionally, one or more manual names are
1416: * comma-specified prior to a whitespace, then a
1417: * dash, then a description. Try to puzzle out
1418: * the name parts here.
1419: */
1.18 kristaps 1420:
1.121 schwarze 1421: start = title;
1.50 kristaps 1422: for ( ;; ) {
1423: sz = strcspn(start, " ,");
1424: if ('\0' == start[sz])
1425: break;
1.1 kristaps 1426:
1.50 kristaps 1427: byte = start[sz];
1428: start[sz] = '\0';
1.110 schwarze 1429:
1430: /*
1431: * Assume a stray trailing comma in the
1432: * name list if a name begins with a dash.
1433: */
1434:
1435: if ('-' == start[0] ||
1436: ('\\' == start[0] && '-' == start[1]))
1437: break;
1.1 kristaps 1438:
1.133 schwarze 1439: putkey(mpage, start, NAME_TITLE);
1.174 schwarze 1440: if ( ! (mpage->name_head_done ||
1441: strcasecmp(start, meta->title))) {
1442: putkey(mpage, start, NAME_HEAD);
1443: mpage->name_head_done = 1;
1444: }
1.1 kristaps 1445:
1.50 kristaps 1446: if (' ' == byte) {
1447: start += sz + 1;
1448: break;
1449: }
1.1 kristaps 1450:
1.50 kristaps 1451: assert(',' == byte);
1452: start += sz + 1;
1453: while (' ' == *start)
1454: start++;
1455: }
1.1 kristaps 1456:
1.121 schwarze 1457: if (start == title) {
1.133 schwarze 1458: putkey(mpage, start, NAME_TITLE);
1.174 schwarze 1459: if ( ! (mpage->name_head_done ||
1460: strcasecmp(start, meta->title))) {
1461: putkey(mpage, start, NAME_HEAD);
1462: mpage->name_head_done = 1;
1463: }
1.50 kristaps 1464: free(title);
1.61 schwarze 1465: return;
1.50 kristaps 1466: }
1.1 kristaps 1467:
1.50 kristaps 1468: while (isspace((unsigned char)*start))
1469: start++;
1.1 kristaps 1470:
1.50 kristaps 1471: if (0 == strncmp(start, "-", 1))
1472: start += 1;
1473: else if (0 == strncmp(start, "\\-\\-", 4))
1474: start += 4;
1475: else if (0 == strncmp(start, "\\-", 2))
1476: start += 2;
1477: else if (0 == strncmp(start, "\\(en", 4))
1478: start += 4;
1479: else if (0 == strncmp(start, "\\(em", 4))
1480: start += 4;
1.1 kristaps 1481:
1.50 kristaps 1482: while (' ' == *start)
1483: start++;
1.1 kristaps 1484:
1.78 schwarze 1485: mpage->desc = mandoc_strdup(start);
1.50 kristaps 1486: free(title);
1.61 schwarze 1487: return;
1.50 kristaps 1488: }
1489: }
1.1 kristaps 1490:
1.77 schwarze 1491: for (n = n->child; n; n = n->next) {
1.78 schwarze 1492: if (NULL != mpage->desc)
1.77 schwarze 1493: break;
1.174 schwarze 1494: parse_man(mpage, meta, n);
1.77 schwarze 1495: }
1.1 kristaps 1496: }
1497:
1498: static void
1.190 schwarze 1499: parse_mdoc(struct mpage *mpage, const struct roff_meta *meta,
1.189 schwarze 1500: const struct roff_node *n)
1.1 kristaps 1501: {
1502:
1.50 kristaps 1503: assert(NULL != n);
1504: for (n = n->child; NULL != n; n = n->next) {
1505: switch (n->type) {
1.188 schwarze 1506: case ROFFT_ELEM:
1507: case ROFFT_BLOCK:
1508: case ROFFT_HEAD:
1509: case ROFFT_BODY:
1510: case ROFFT_TAIL:
1.50 kristaps 1511: if (NULL != mdocs[n->tok].fp)
1.172 schwarze 1512: if (0 == (*mdocs[n->tok].fp)(mpage, meta, n))
1.50 kristaps 1513: break;
1.68 schwarze 1514: if (mdocs[n->tok].mask)
1.78 schwarze 1515: putmdockey(mpage, n->child,
1516: mdocs[n->tok].mask);
1.50 kristaps 1517: break;
1518: default:
1.188 schwarze 1519: assert(n->type != ROFFT_ROOT);
1.50 kristaps 1520: continue;
1521: }
1522: if (NULL != n->child)
1.172 schwarze 1523: parse_mdoc(mpage, meta, n);
1.1 kristaps 1524: }
1525: }
1526:
1.25 schwarze 1527: static int
1.190 schwarze 1528: parse_mdoc_Fd(struct mpage *mpage, const struct roff_meta *meta,
1.189 schwarze 1529: const struct roff_node *n)
1.1 kristaps 1530: {
1.176 schwarze 1531: char *start, *end;
1.1 kristaps 1532: size_t sz;
1.25 schwarze 1533:
1.50 kristaps 1534: if (SEC_SYNOPSIS != n->sec ||
1.141 schwarze 1535: NULL == (n = n->child) ||
1.188 schwarze 1536: n->type != ROFFT_TEXT)
1.197 schwarze 1537: return 0;
1.1 kristaps 1538:
1539: /*
1540: * Only consider those `Fd' macro fields that begin with an
1541: * "inclusion" token (versus, e.g., #define).
1542: */
1.50 kristaps 1543:
1.1 kristaps 1544: if (strcmp("#include", n->string))
1.197 schwarze 1545: return 0;
1.1 kristaps 1546:
1.188 schwarze 1547: if ((n = n->next) == NULL || n->type != ROFFT_TEXT)
1.197 schwarze 1548: return 0;
1.1 kristaps 1549:
1550: /*
1551: * Strip away the enclosing angle brackets and make sure we're
1552: * not zero-length.
1553: */
1554:
1555: start = n->string;
1556: if ('<' == *start || '"' == *start)
1557: start++;
1558:
1559: if (0 == (sz = strlen(start)))
1.197 schwarze 1560: return 0;
1.1 kristaps 1561:
1562: end = &start[(int)sz - 1];
1563: if ('>' == *end || '"' == *end)
1564: end--;
1565:
1.50 kristaps 1566: if (end > start)
1.78 schwarze 1567: putkeys(mpage, start, end - start + 1, TYPE_In);
1.197 schwarze 1568: return 0;
1.1 kristaps 1569: }
1570:
1.178 schwarze 1571: static void
1.189 schwarze 1572: parse_mdoc_fname(struct mpage *mpage, const struct roff_node *n)
1.1 kristaps 1573: {
1.112 schwarze 1574: char *cp;
1.178 schwarze 1575: size_t sz;
1.1 kristaps 1576:
1.188 schwarze 1577: if (n->type != ROFFT_TEXT)
1.178 schwarze 1578: return;
1.25 schwarze 1579:
1.178 schwarze 1580: /* Skip function pointer punctuation. */
1.1 kristaps 1581:
1.178 schwarze 1582: cp = n->string;
1583: while (*cp == '(' || *cp == '*')
1.1 kristaps 1584: cp++;
1.178 schwarze 1585: sz = strcspn(cp, "()");
1.1 kristaps 1586:
1.178 schwarze 1587: putkeys(mpage, cp, sz, TYPE_Fn);
1.173 schwarze 1588: if (n->sec == SEC_SYNOPSIS)
1.178 schwarze 1589: putkeys(mpage, cp, sz, NAME_SYN);
1590: }
1.25 schwarze 1591:
1.178 schwarze 1592: static int
1.190 schwarze 1593: parse_mdoc_Fn(struct mpage *mpage, const struct roff_meta *meta,
1.189 schwarze 1594: const struct roff_node *n)
1.178 schwarze 1595: {
1596:
1597: if (n->child == NULL)
1.197 schwarze 1598: return 0;
1.178 schwarze 1599:
1600: parse_mdoc_fname(mpage, n->child);
1.25 schwarze 1601:
1.178 schwarze 1602: for (n = n->child->next; n != NULL; n = n->next)
1.188 schwarze 1603: if (n->type == ROFFT_TEXT)
1.78 schwarze 1604: putkey(mpage, n->string, TYPE_Fa);
1.25 schwarze 1605:
1.197 schwarze 1606: return 0;
1.173 schwarze 1607: }
1608:
1609: static int
1.190 schwarze 1610: parse_mdoc_Fo(struct mpage *mpage, const struct roff_meta *meta,
1.189 schwarze 1611: const struct roff_node *n)
1.173 schwarze 1612: {
1.177 schwarze 1613:
1.188 schwarze 1614: if (n->type != ROFFT_HEAD)
1.197 schwarze 1615: return 1;
1.173 schwarze 1616:
1.178 schwarze 1617: if (n->child != NULL)
1618: parse_mdoc_fname(mpage, n->child);
1619:
1.197 schwarze 1620: return 0;
1.1 kristaps 1621: }
1622:
1.25 schwarze 1623: static int
1.211 schwarze 1624: parse_mdoc_Va(struct mpage *mpage, const struct roff_meta *meta,
1625: const struct roff_node *n)
1626: {
1627: char *cp;
1628:
1629: if (n->type != ROFFT_ELEM && n->type != ROFFT_BODY)
1630: return 0;
1631:
1.215 schwarze 1632: if (n->child != NULL &&
1633: n->child->next == NULL &&
1634: n->child->type == ROFFT_TEXT)
1.211 schwarze 1635: return 1;
1636:
1637: cp = NULL;
1638: deroff(&cp, n);
1639: if (cp != NULL) {
1640: putkey(mpage, cp, TYPE_Vt | (n->tok == MDOC_Va ||
1641: n->type == ROFFT_BODY ? TYPE_Va : 0));
1642: free(cp);
1643: }
1644:
1645: return 0;
1646: }
1647:
1648: static int
1.190 schwarze 1649: parse_mdoc_Xr(struct mpage *mpage, const struct roff_meta *meta,
1.189 schwarze 1650: const struct roff_node *n)
1.1 kristaps 1651: {
1.67 schwarze 1652: char *cp;
1.1 kristaps 1653:
1654: if (NULL == (n = n->child))
1.197 schwarze 1655: return 0;
1.1 kristaps 1656:
1.67 schwarze 1657: if (NULL == n->next) {
1.78 schwarze 1658: putkey(mpage, n->string, TYPE_Xr);
1.197 schwarze 1659: return 0;
1.67 schwarze 1660: }
1661:
1.120 schwarze 1662: mandoc_asprintf(&cp, "%s(%s)", n->string, n->next->string);
1.78 schwarze 1663: putkey(mpage, cp, TYPE_Xr);
1.67 schwarze 1664: free(cp);
1.197 schwarze 1665: return 0;
1.1 kristaps 1666: }
1667:
1.25 schwarze 1668: static int
1.190 schwarze 1669: parse_mdoc_Nd(struct mpage *mpage, const struct roff_meta *meta,
1.189 schwarze 1670: const struct roff_node *n)
1.1 kristaps 1671: {
1672:
1.188 schwarze 1673: if (n->type == ROFFT_BODY)
1.194 schwarze 1674: deroff(&mpage->desc, n);
1.197 schwarze 1675: return 0;
1.1 kristaps 1676: }
1677:
1.25 schwarze 1678: static int
1.190 schwarze 1679: parse_mdoc_Nm(struct mpage *mpage, const struct roff_meta *meta,
1.189 schwarze 1680: const struct roff_node *n)
1.1 kristaps 1681: {
1682:
1.129 schwarze 1683: if (SEC_NAME == n->sec)
1.133 schwarze 1684: putmdockey(mpage, n->child, NAME_TITLE);
1.188 schwarze 1685: else if (n->sec == SEC_SYNOPSIS && n->type == ROFFT_HEAD) {
1.172 schwarze 1686: if (n->child == NULL)
1687: putkey(mpage, meta->name, NAME_SYN);
1688: else
1689: putmdockey(mpage, n->child, NAME_SYN);
1.174 schwarze 1690: }
1691: if ( ! (mpage->name_head_done ||
1692: n->child == NULL || n->child->string == NULL ||
1693: strcasecmp(n->child->string, meta->title))) {
1.226 schwarze 1694: putkey(mpage, n->child->string, NAME_HEAD);
1.174 schwarze 1695: mpage->name_head_done = 1;
1.172 schwarze 1696: }
1.197 schwarze 1697: return 0;
1.1 kristaps 1698: }
1699:
1.25 schwarze 1700: static int
1.190 schwarze 1701: parse_mdoc_Sh(struct mpage *mpage, const struct roff_meta *meta,
1.189 schwarze 1702: const struct roff_node *n)
1.1 kristaps 1703: {
1704:
1.197 schwarze 1705: return n->sec == SEC_CUSTOM && n->type == ROFFT_HEAD;
1.1 kristaps 1706: }
1707:
1.50 kristaps 1708: static int
1.190 schwarze 1709: parse_mdoc_head(struct mpage *mpage, const struct roff_meta *meta,
1.189 schwarze 1710: const struct roff_node *n)
1.1 kristaps 1711: {
1712:
1.197 schwarze 1713: return n->type == ROFFT_HEAD;
1.1 kristaps 1714: }
1715:
1.50 kristaps 1716: /*
1.66 schwarze 1717: * Add a string to the hash table for the current manual.
1718: * Each string has a bitmask telling which macros it belongs to.
1719: * When we finish the manual, we'll dump the table.
1.50 kristaps 1720: */
1721: static void
1.176 schwarze 1722: putkeys(const struct mpage *mpage, char *cp, size_t sz, uint64_t v)
1.50 kristaps 1723: {
1.133 schwarze 1724: struct ohash *htab;
1.50 kristaps 1725: struct str *s;
1.111 schwarze 1726: const char *end;
1.65 schwarze 1727: unsigned int slot;
1.176 schwarze 1728: int i, mustfree;
1.25 schwarze 1729:
1.50 kristaps 1730: if (0 == sz)
1731: return;
1.111 schwarze 1732:
1.176 schwarze 1733: mustfree = render_string(&cp, &sz);
1734:
1.133 schwarze 1735: if (TYPE_Nm & v) {
1736: htab = &names;
1737: v &= name_mask;
1.169 schwarze 1738: if (v & NAME_FIRST)
1739: name_mask &= ~NAME_FIRST;
1.133 schwarze 1740: if (debug > 1)
1741: say(mpage->mlinks->file,
1.234 ! schwarze 1742: "Adding name %*s, bits=0x%llx", (int)sz, cp,
! 1743: (unsigned long long)v);
1.133 schwarze 1744: } else {
1745: htab = &strings;
1746: if (debug > 1)
1.221 schwarze 1747: for (i = 0; i < KEY_MAX; i++)
1.163 schwarze 1748: if ((uint64_t)1 << i & v)
1.133 schwarze 1749: say(mpage->mlinks->file,
1750: "Adding key %s=%*s",
1.220 schwarze 1751: mansearch_keynames[i], (int)sz, cp);
1.111 schwarze 1752: }
1.25 schwarze 1753:
1.65 schwarze 1754: end = cp + sz;
1.133 schwarze 1755: slot = ohash_qlookupi(htab, cp, &end);
1756: s = ohash_find(htab, slot);
1.25 schwarze 1757:
1.78 schwarze 1758: if (NULL != s && mpage == s->mpage) {
1.50 kristaps 1759: s->mask |= v;
1760: return;
1761: } else if (NULL == s) {
1.144 schwarze 1762: s = mandoc_calloc(1, sizeof(struct str) + sz + 1);
1.50 kristaps 1763: memcpy(s->key, cp, sz);
1.133 schwarze 1764: ohash_insert(htab, slot, s);
1.1 kristaps 1765: }
1.78 schwarze 1766: s->mpage = mpage;
1.50 kristaps 1767: s->mask = v;
1.176 schwarze 1768:
1769: if (mustfree)
1770: free(cp);
1.1 kristaps 1771: }
1772:
1.50 kristaps 1773: /*
1774: * Take a Unicode codepoint and produce its UTF-8 encoding.
1775: * This isn't the best way to do this, but it works.
1776: * The magic numbers are from the UTF-8 packaging.
1777: * They're not as scary as they seem: read the UTF-8 spec for details.
1778: */
1779: static size_t
1780: utf8(unsigned int cp, char out[7])
1.1 kristaps 1781: {
1.50 kristaps 1782: size_t rc;
1.1 kristaps 1783:
1.50 kristaps 1784: rc = 0;
1785: if (cp <= 0x0000007F) {
1786: rc = 1;
1787: out[0] = (char)cp;
1788: } else if (cp <= 0x000007FF) {
1789: rc = 2;
1790: out[0] = (cp >> 6 & 31) | 192;
1791: out[1] = (cp & 63) | 128;
1792: } else if (cp <= 0x0000FFFF) {
1793: rc = 3;
1794: out[0] = (cp >> 12 & 15) | 224;
1795: out[1] = (cp >> 6 & 63) | 128;
1796: out[2] = (cp & 63) | 128;
1797: } else if (cp <= 0x001FFFFF) {
1798: rc = 4;
1799: out[0] = (cp >> 18 & 7) | 240;
1800: out[1] = (cp >> 12 & 63) | 128;
1801: out[2] = (cp >> 6 & 63) | 128;
1802: out[3] = (cp & 63) | 128;
1803: } else if (cp <= 0x03FFFFFF) {
1804: rc = 5;
1805: out[0] = (cp >> 24 & 3) | 248;
1806: out[1] = (cp >> 18 & 63) | 128;
1807: out[2] = (cp >> 12 & 63) | 128;
1808: out[3] = (cp >> 6 & 63) | 128;
1809: out[4] = (cp & 63) | 128;
1810: } else if (cp <= 0x7FFFFFFF) {
1811: rc = 6;
1812: out[0] = (cp >> 30 & 1) | 252;
1813: out[1] = (cp >> 24 & 63) | 128;
1814: out[2] = (cp >> 18 & 63) | 128;
1815: out[3] = (cp >> 12 & 63) | 128;
1816: out[4] = (cp >> 6 & 63) | 128;
1817: out[5] = (cp & 63) | 128;
1818: } else
1.197 schwarze 1819: return 0;
1.1 kristaps 1820:
1.50 kristaps 1821: out[rc] = '\0';
1.197 schwarze 1822: return rc;
1.50 kristaps 1823: }
1.1 kristaps 1824:
1.50 kristaps 1825: /*
1.176 schwarze 1826: * If the string contains escape sequences,
1827: * replace it with an allocated rendering and return 1,
1828: * such that the caller can free it after use.
1829: * Otherwise, do nothing and return 0.
1.50 kristaps 1830: */
1.176 schwarze 1831: static int
1832: render_string(char **public, size_t *psz)
1.50 kristaps 1833: {
1.176 schwarze 1834: const char *src, *scp, *addcp, *seq;
1835: char *dst;
1836: size_t ssz, dsz, addsz;
1.114 schwarze 1837: char utfbuf[7], res[6];
1.176 schwarze 1838: int seqlen, unicode;
1.50 kristaps 1839:
1840: res[0] = '\\';
1841: res[1] = '\t';
1842: res[2] = ASCII_NBRSP;
1843: res[3] = ASCII_HYPH;
1.114 schwarze 1844: res[4] = ASCII_BREAK;
1845: res[5] = '\0';
1.1 kristaps 1846:
1.176 schwarze 1847: src = scp = *public;
1848: ssz = *psz;
1849: dst = NULL;
1850: dsz = 0;
1851:
1852: while (scp < src + *psz) {
1853:
1854: /* Leave normal characters unchanged. */
1855:
1856: if (strchr(res, *scp) == NULL) {
1857: if (dst != NULL)
1858: dst[dsz++] = *scp;
1859: scp++;
1860: continue;
1861: }
1.46 kristaps 1862:
1.50 kristaps 1863: /*
1.176 schwarze 1864: * Found something that requires replacing,
1865: * make sure we have a destination buffer.
1.50 kristaps 1866: */
1.176 schwarze 1867:
1868: if (dst == NULL) {
1869: dst = mandoc_malloc(ssz + 1);
1870: dsz = scp - src;
1871: memcpy(dst, src, dsz);
1.50 kristaps 1872: }
1.46 kristaps 1873:
1.176 schwarze 1874: /* Handle single-char special characters. */
1875:
1876: switch (*scp) {
1877: case '\\':
1878: break;
1.141 schwarze 1879: case '\t':
1880: case ASCII_NBRSP:
1.176 schwarze 1881: dst[dsz++] = ' ';
1882: scp++;
1883: continue;
1884: case ASCII_HYPH:
1885: dst[dsz++] = '-';
1.114 schwarze 1886: /* FALLTHROUGH */
1.141 schwarze 1887: case ASCII_BREAK:
1.176 schwarze 1888: scp++;
1.50 kristaps 1889: continue;
1.114 schwarze 1890: default:
1.176 schwarze 1891: abort();
1.114 schwarze 1892: }
1.46 kristaps 1893:
1.50 kristaps 1894: /*
1.176 schwarze 1895: * Found an escape sequence.
1896: * Read past the slash, then parse it.
1897: * Ignore everything except characters.
1.50 kristaps 1898: */
1.95 schwarze 1899:
1.176 schwarze 1900: scp++;
1901: if (mandoc_escape(&scp, &seq, &seqlen) != ESCAPE_SPECIAL)
1.50 kristaps 1902: continue;
1.1 kristaps 1903:
1.50 kristaps 1904: /*
1.95 schwarze 1905: * Render the special character
1906: * as either UTF-8 or ASCII.
1.50 kristaps 1907: */
1.95 schwarze 1908:
1909: if (write_utf8) {
1.203 schwarze 1910: unicode = mchars_spec2cp(seq, seqlen);
1.176 schwarze 1911: if (unicode <= 0)
1.95 schwarze 1912: continue;
1.176 schwarze 1913: addsz = utf8(unicode, utfbuf);
1914: if (addsz == 0)
1.95 schwarze 1915: continue;
1.176 schwarze 1916: addcp = utfbuf;
1.95 schwarze 1917: } else {
1.203 schwarze 1918: addcp = mchars_spec2str(seq, seqlen, &addsz);
1.176 schwarze 1919: if (addcp == NULL)
1.95 schwarze 1920: continue;
1.176 schwarze 1921: if (*addcp == ASCII_NBRSP) {
1922: addcp = " ";
1923: addsz = 1;
1.95 schwarze 1924: }
1925: }
1.1 kristaps 1926:
1.50 kristaps 1927: /* Copy the rendered glyph into the stream. */
1.1 kristaps 1928:
1.176 schwarze 1929: ssz += addsz;
1930: dst = mandoc_realloc(dst, ssz + 1);
1931: memcpy(dst + dsz, addcp, addsz);
1932: dsz += addsz;
1933: }
1934: if (dst != NULL) {
1935: *public = dst;
1936: *psz = dsz;
1937: }
1938:
1939: /* Trim trailing whitespace and NUL-terminate. */
1940:
1941: while (*psz > 0 && (*public)[*psz - 1] == ' ')
1942: --*psz;
1943: if (dst != NULL) {
1944: (*public)[*psz] = '\0';
1.197 schwarze 1945: return 1;
1.176 schwarze 1946: } else
1.197 schwarze 1947: return 0;
1.1 kristaps 1948: }
1949:
1.118 schwarze 1950: static void
1951: dbadd_mlink(const struct mlink *mlink)
1952: {
1.221 schwarze 1953: dba_page_alias(mlink->mpage->dba, mlink->name, NAME_FILE);
1954: dba_page_add(mlink->mpage->dba, DBP_SECT, mlink->dsec);
1.223 schwarze 1955: dba_page_add(mlink->mpage->dba, DBP_SECT, mlink->fsec);
1.221 schwarze 1956: dba_page_add(mlink->mpage->dba, DBP_ARCH, mlink->arch);
1957: dba_page_add(mlink->mpage->dba, DBP_FILE, mlink->file);
1.118 schwarze 1958: }
1959:
1.14 schwarze 1960: /*
1.50 kristaps 1961: * Flush the current page's terms (and their bits) into the database.
1.96 schwarze 1962: * Also, handle escape sequences at the last possible moment.
1.14 schwarze 1963: */
1964: static void
1.221 schwarze 1965: dbadd(struct dba *dba, struct mpage *mpage)
1.14 schwarze 1966: {
1.87 schwarze 1967: struct mlink *mlink;
1.50 kristaps 1968: struct str *key;
1.176 schwarze 1969: char *cp;
1.221 schwarze 1970: uint64_t mask;
1.52 kristaps 1971: size_t i;
1.66 schwarze 1972: unsigned int slot;
1.176 schwarze 1973: int mustfree;
1.14 schwarze 1974:
1.128 schwarze 1975: mlink = mpage->mlinks;
1.43 kristaps 1976:
1.128 schwarze 1977: if (nodb) {
1.147 schwarze 1978: for (key = ohash_first(&names, &slot); NULL != key;
1.176 schwarze 1979: key = ohash_next(&names, &slot))
1.147 schwarze 1980: free(key);
1981: for (key = ohash_first(&strings, &slot); NULL != key;
1.176 schwarze 1982: key = ohash_next(&strings, &slot))
1.147 schwarze 1983: free(key);
1.145 schwarze 1984: if (0 == debug)
1985: return;
1.128 schwarze 1986: while (NULL != mlink) {
1987: fputs(mlink->name, stdout);
1988: if (NULL == mlink->next ||
1989: strcmp(mlink->dsec, mlink->next->dsec) ||
1990: strcmp(mlink->fsec, mlink->next->fsec) ||
1991: strcmp(mlink->arch, mlink->next->arch)) {
1992: putchar('(');
1993: if ('\0' == *mlink->dsec)
1994: fputs(mlink->fsec, stdout);
1995: else
1996: fputs(mlink->dsec, stdout);
1997: if ('\0' != *mlink->arch)
1998: printf("/%s", mlink->arch);
1999: putchar(')');
2000: }
2001: mlink = mlink->next;
2002: if (NULL != mlink)
2003: fputs(", ", stdout);
2004: }
1.132 schwarze 2005: printf(" - %s\n", mpage->desc);
1.14 schwarze 2006: return;
1.128 schwarze 2007: }
2008:
2009: if (debug)
2010: say(mlink->file, "Adding to database");
1.28 kristaps 2011:
1.176 schwarze 2012: cp = mpage->desc;
2013: i = strlen(cp);
2014: mustfree = render_string(&cp, &i);
1.227 schwarze 2015: mpage->dba = dba_page_new(dba->pages,
1.223 schwarze 2016: *mpage->arch == '\0' ? mlink->arch : mpage->arch,
2017: cp, mlink->file, mpage->form);
1.176 schwarze 2018: if (mustfree)
2019: free(cp);
1.227 schwarze 2020: dba_page_add(mpage->dba, DBP_SECT, mpage->sec);
1.81 schwarze 2021:
1.223 schwarze 2022: while (mlink != NULL) {
1.118 schwarze 2023: dbadd_mlink(mlink);
1.223 schwarze 2024: mlink = mlink->next;
2025: }
1.50 kristaps 2026:
1.133 schwarze 2027: for (key = ohash_first(&names, &slot); NULL != key;
2028: key = ohash_next(&names, &slot)) {
2029: assert(key->mpage == mpage);
1.221 schwarze 2030: dba_page_alias(mpage->dba, key->key, key->mask);
1.133 schwarze 2031: free(key);
2032: }
1.66 schwarze 2033: for (key = ohash_first(&strings, &slot); NULL != key;
2034: key = ohash_next(&strings, &slot)) {
1.78 schwarze 2035: assert(key->mpage == mpage);
1.221 schwarze 2036: i = 0;
2037: for (mask = TYPE_Xr; mask <= TYPE_Lb; mask *= 2) {
2038: if (key->mask & mask)
2039: dba_macro_add(dba->macros, i,
2040: key->key, mpage->dba);
2041: i++;
2042: }
1.66 schwarze 2043: free(key);
1.38 schwarze 2044: }
1.14 schwarze 2045: }
2046:
1.5 kristaps 2047: static void
1.221 schwarze 2048: dbprune(struct dba *dba)
1.5 kristaps 2049: {
1.221 schwarze 2050: struct dba_array *page, *files;
2051: char *file;
1.5 kristaps 2052:
1.221 schwarze 2053: dba_array_FOREACH(dba->pages, page) {
2054: files = dba_array_get(page, DBP_FILE);
2055: dba_array_FOREACH(files, file) {
2056: if (*file < ' ')
2057: file++;
2058: if (ohash_find(&mlinks, ohash_qlookup(&mlinks,
2059: file)) != NULL) {
2060: if (debug)
2061: say(file, "Deleting from database");
2062: dba_array_del(dba->pages);
2063: break;
2064: }
1.106 schwarze 2065: }
1.5 kristaps 2066: }
2067: }
2068:
1.4 kristaps 2069: /*
1.221 schwarze 2070: * Write the database from memory to disk.
1.4 kristaps 2071: */
1.35 kristaps 2072: static void
1.221 schwarze 2073: dbwrite(struct dba *dba)
1.4 kristaps 2074: {
1.221 schwarze 2075: char tfn[32];
1.115 schwarze 2076: int status;
2077: pid_t child;
1.4 kristaps 2078:
1.221 schwarze 2079: if (dba_write(MANDOC_DB "~", dba) != -1) {
2080: if (rename(MANDOC_DB "~", MANDOC_DB) == -1) {
2081: exitcode = (int)MANDOCLEVEL_SYSERR;
2082: say(MANDOC_DB, "&rename");
2083: unlink(MANDOC_DB "~");
2084: }
1.38 schwarze 2085: return;
1.4 kristaps 2086: }
2087:
1.221 schwarze 2088: (void)strlcpy(tfn, "/tmp/mandocdb.XXXXXXXX", sizeof(tfn));
2089: if (mkdtemp(tfn) == NULL) {
2090: exitcode = (int)MANDOCLEVEL_SYSERR;
2091: say("", "&%s", tfn);
1.50 kristaps 2092: return;
1.221 schwarze 2093: }
1.12 schwarze 2094:
1.221 schwarze 2095: (void)strlcat(tfn, "/" MANDOC_DB, sizeof(tfn));
2096: if (dba_write(tfn, dba) == -1) {
2097: exitcode = (int)MANDOCLEVEL_SYSERR;
2098: say(tfn, "&dba_write");
2099: goto out;
1.115 schwarze 2100: }
2101:
2102: switch (child = fork()) {
1.141 schwarze 2103: case -1:
1.115 schwarze 2104: exitcode = (int)MANDOCLEVEL_SYSERR;
1.123 schwarze 2105: say("", "&fork cmp");
1.115 schwarze 2106: return;
1.141 schwarze 2107: case 0:
1.221 schwarze 2108: execlp("cmp", "cmp", "-s", tfn, MANDOC_DB, (char *)NULL);
1.123 schwarze 2109: say("", "&exec cmp");
1.115 schwarze 2110: exit(0);
2111: default:
2112: break;
2113: }
1.221 schwarze 2114: if (waitpid(child, &status, 0) == -1) {
1.115 schwarze 2115: exitcode = (int)MANDOCLEVEL_SYSERR;
1.123 schwarze 2116: say("", "&wait cmp");
1.115 schwarze 2117: } else if (WIFSIGNALED(status)) {
2118: exitcode = (int)MANDOCLEVEL_SYSERR;
1.123 schwarze 2119: say("", "cmp died from signal %d", WTERMSIG(status));
1.115 schwarze 2120: } else if (WEXITSTATUS(status)) {
1.59 schwarze 2121: exitcode = (int)MANDOCLEVEL_SYSERR;
1.115 schwarze 2122: say(MANDOC_DB,
2123: "Data changed, but cannot replace database");
2124: }
2125:
1.221 schwarze 2126: out:
2127: *strrchr(tfn, '/') = '\0';
1.115 schwarze 2128: switch (child = fork()) {
1.141 schwarze 2129: case -1:
1.115 schwarze 2130: exitcode = (int)MANDOCLEVEL_SYSERR;
1.123 schwarze 2131: say("", "&fork rm");
1.115 schwarze 2132: return;
1.141 schwarze 2133: case 0:
1.221 schwarze 2134: execlp("rm", "rm", "-rf", tfn, (char *)NULL);
1.123 schwarze 2135: say("", "&exec rm");
1.115 schwarze 2136: exit((int)MANDOCLEVEL_SYSERR);
2137: default:
2138: break;
2139: }
1.221 schwarze 2140: if (waitpid(child, &status, 0) == -1) {
1.115 schwarze 2141: exitcode = (int)MANDOCLEVEL_SYSERR;
1.123 schwarze 2142: say("", "&wait rm");
1.115 schwarze 2143: } else if (WIFSIGNALED(status) || WEXITSTATUS(status)) {
2144: exitcode = (int)MANDOCLEVEL_SYSERR;
1.221 schwarze 2145: say("", "%s: Cannot remove temporary directory", tfn);
1.146 schwarze 2146: }
1.4 kristaps 2147: }
2148:
1.50 kristaps 2149: static int
1.165 schwarze 2150: set_basedir(const char *targetdir, int report_baddir)
1.4 kristaps 2151: {
1.59 schwarze 2152: static char startdir[PATH_MAX];
1.151 schwarze 2153: static int getcwd_status; /* 1 = ok, 2 = failure */
2154: static int chdir_status; /* 1 = changed directory */
1.150 schwarze 2155: char *cp;
1.4 kristaps 2156:
1.59 schwarze 2157: /*
1.151 schwarze 2158: * Remember the original working directory, if possible.
2159: * This will be needed if the second or a later directory
2160: * on the command line is given as a relative path.
2161: * Do not error out if the current directory is not
2162: * searchable: Maybe it won't be needed after all.
2163: */
2164: if (0 == getcwd_status) {
2165: if (NULL == getcwd(startdir, sizeof(startdir))) {
2166: getcwd_status = 2;
2167: (void)strlcpy(startdir, strerror(errno),
2168: sizeof(startdir));
2169: } else
2170: getcwd_status = 1;
2171: }
2172:
2173: /*
2174: * We are leaving the old base directory.
2175: * Do not use it any longer, not even for messages.
2176: */
2177: *basedir = '\0';
2178:
2179: /*
2180: * If and only if the directory was changed earlier and
2181: * the next directory to process is given as a relative path,
2182: * first go back, or bail out if that is impossible.
1.59 schwarze 2183: */
1.151 schwarze 2184: if (chdir_status && '/' != *targetdir) {
2185: if (2 == getcwd_status) {
1.59 schwarze 2186: exitcode = (int)MANDOCLEVEL_SYSERR;
1.151 schwarze 2187: say("", "getcwd: %s", startdir);
1.197 schwarze 2188: return 0;
1.59 schwarze 2189: }
1.151 schwarze 2190: if (-1 == chdir(startdir)) {
1.59 schwarze 2191: exitcode = (int)MANDOCLEVEL_SYSERR;
1.123 schwarze 2192: say("", "&chdir %s", startdir);
1.197 schwarze 2193: return 0;
1.59 schwarze 2194: }
2195: }
1.151 schwarze 2196:
2197: /*
2198: * Always resolve basedir to the canonicalized absolute
2199: * pathname and append a trailing slash, such that
2200: * we can reliably check whether files are inside.
2201: */
1.59 schwarze 2202: if (NULL == realpath(targetdir, basedir)) {
1.165 schwarze 2203: if (report_baddir || errno != ENOENT) {
2204: exitcode = (int)MANDOCLEVEL_BADARG;
2205: say("", "&%s: realpath", targetdir);
2206: }
1.197 schwarze 2207: return 0;
1.59 schwarze 2208: } else if (-1 == chdir(basedir)) {
1.165 schwarze 2209: if (report_baddir || errno != ENOENT) {
2210: exitcode = (int)MANDOCLEVEL_BADARG;
2211: say("", "&chdir");
2212: }
1.197 schwarze 2213: return 0;
1.150 schwarze 2214: }
1.151 schwarze 2215: chdir_status = 1;
1.150 schwarze 2216: cp = strchr(basedir, '\0');
2217: if ('/' != cp[-1]) {
2218: if (cp - basedir >= PATH_MAX - 1) {
2219: exitcode = (int)MANDOCLEVEL_SYSERR;
2220: say("", "Filename too long");
1.197 schwarze 2221: return 0;
1.150 schwarze 2222: }
2223: *cp++ = '/';
2224: *cp = '\0';
1.4 kristaps 2225: }
1.197 schwarze 2226: return 1;
1.56 schwarze 2227: }
2228:
2229: static void
1.59 schwarze 2230: say(const char *file, const char *format, ...)
1.56 schwarze 2231: {
2232: va_list ap;
1.123 schwarze 2233: int use_errno;
1.56 schwarze 2234:
1.59 schwarze 2235: if ('\0' != *basedir)
2236: fprintf(stderr, "%s", basedir);
2237: if ('\0' != *basedir && '\0' != *file)
1.151 schwarze 2238: fputc('/', stderr);
1.56 schwarze 2239: if ('\0' != *file)
1.59 schwarze 2240: fprintf(stderr, "%s", file);
2241:
1.123 schwarze 2242: use_errno = 1;
2243: if (NULL != format) {
2244: switch (*format) {
1.141 schwarze 2245: case '&':
1.123 schwarze 2246: format++;
2247: break;
1.141 schwarze 2248: case '\0':
1.123 schwarze 2249: format = NULL;
2250: break;
2251: default:
2252: use_errno = 0;
2253: break;
2254: }
2255: }
2256: if (NULL != format) {
2257: if ('\0' != *basedir || '\0' != *file)
2258: fputs(": ", stderr);
2259: va_start(ap, format);
2260: vfprintf(stderr, format, ap);
2261: va_end(ap);
2262: }
2263: if (use_errno) {
2264: if ('\0' != *basedir || '\0' != *file || NULL != format)
2265: fputs(": ", stderr);
1.59 schwarze 2266: perror(NULL);
1.123 schwarze 2267: } else
2268: fputc('\n', stderr);
1.1 kristaps 2269: }
CVSweb