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