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