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