Annotation of mandoc/main.c, Revision 1.221
1.221 ! schwarze 1: /* $Id: main.c,v 1.220 2015/02/10 08:05:30 schwarze Exp $ */
1.1 kristaps 2: /*
1.183 schwarze 3: * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.212 schwarze 4: * Copyright (c) 2010-2012, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
1.169 schwarze 5: * Copyright (c) 2010 Joerg Sonnenberger <joerg@netbsd.org>
1.1 kristaps 6: *
7: * Permission to use, copy, modify, and distribute this software for any
1.25 kristaps 8: * purpose with or without fee is hereby granted, provided that the above
9: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 10: *
1.25 kristaps 11: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
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.
1.1 kristaps 18: */
1.58 kristaps 19: #include "config.h"
1.178 schwarze 20:
21: #include <sys/types.h>
1.216 schwarze 22: #include <sys/param.h> /* MACHINE */
1.58 kristaps 23:
1.1 kristaps 24: #include <assert.h>
1.185 schwarze 25: #include <ctype.h>
1.183 schwarze 26: #include <errno.h>
1.186 schwarze 27: #include <fcntl.h>
1.1 kristaps 28: #include <stdio.h>
1.45 kristaps 29: #include <stdint.h>
1.1 kristaps 30: #include <stdlib.h>
31: #include <string.h>
32: #include <unistd.h>
33:
1.71 kristaps 34: #include "mandoc.h"
1.172 schwarze 35: #include "mandoc_aux.h"
1.90 kristaps 36: #include "main.h"
1.1 kristaps 37: #include "mdoc.h"
1.10 kristaps 38: #include "man.h"
1.180 schwarze 39: #include "manpath.h"
40: #include "mansearch.h"
1.101 joerg 41:
1.55 kristaps 42: #if !defined(__GNUC__) || (__GNUC__ < 2)
1.56 kristaps 43: # if !defined(lint)
44: # define __attribute__(x)
45: # endif
1.55 kristaps 46: #endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
1.16 kristaps 47:
1.181 schwarze 48: enum outmode {
49: OUTMODE_DEF = 0,
50: OUTMODE_FLN,
51: OUTMODE_LST,
52: OUTMODE_ALL,
53: OUTMODE_INT,
54: OUTMODE_ONE
55: };
56:
1.42 kristaps 57: typedef void (*out_mdoc)(void *, const struct mdoc *);
58: typedef void (*out_man)(void *, const struct man *);
1.22 kristaps 59: typedef void (*out_free)(void *);
60:
1.19 kristaps 61: enum outt {
1.154 kristaps 62: OUTT_ASCII = 0, /* -Tascii */
1.162 kristaps 63: OUTT_LOCALE, /* -Tlocale */
1.163 kristaps 64: OUTT_UTF8, /* -Tutf8 */
1.154 kristaps 65: OUTT_TREE, /* -Ttree */
1.164 schwarze 66: OUTT_MAN, /* -Tman */
1.154 kristaps 67: OUTT_HTML, /* -Thtml */
68: OUTT_LINT, /* -Tlint */
69: OUTT_PS, /* -Tps */
70: OUTT_PDF /* -Tpdf */
1.19 kristaps 71: };
72:
1.5 kristaps 73: struct curparse {
1.154 kristaps 74: struct mparse *mp;
1.195 schwarze 75: struct mchars *mchars; /* character table */
1.148 kristaps 76: enum mandoclevel wlevel; /* ignore messages below this */
77: int wstop; /* stop after a file with a warning */
1.173 schwarze 78: enum outt outtype; /* which output to use */
1.79 kristaps 79: out_mdoc outmdoc; /* mdoc output ptr */
1.173 schwarze 80: out_man outman; /* man output ptr */
1.79 kristaps 81: out_free outfree; /* free output ptr */
82: void *outdata; /* data for output */
83: char outopts[BUFSIZ]; /* buf of output opts */
84: };
85:
1.213 schwarze 86: static int fs_lookup(const struct manpaths *,
87: size_t ipath, const char *,
88: const char *, const char *,
89: struct manpage **, size_t *);
90: static void fs_search(const struct mansearch *,
91: const struct manpaths *, int, char**,
92: struct manpage **, size_t *);
1.194 schwarze 93: static int koptions(int *, char *);
1.203 schwarze 94: #if HAVE_SQLITE3
95: int mandocdb(int, char**);
96: #endif
1.170 schwarze 97: static int moptions(int *, char *);
1.155 kristaps 98: static void mmsg(enum mandocerr, enum mandoclevel,
99: const char *, int, int, const char *);
1.173 schwarze 100: static void parse(struct curparse *, int,
1.154 kristaps 101: const char *, enum mandoclevel *);
1.197 schwarze 102: static enum mandoclevel passthrough(const char *, int, int);
1.183 schwarze 103: static void spawn_pager(void);
1.66 kristaps 104: static int toptions(struct curparse *, char *);
1.180 schwarze 105: static void usage(enum argmode) __attribute__((noreturn));
1.103 schwarze 106: static int woptions(struct curparse *, char *);
1.1 kristaps 107:
1.182 schwarze 108: static const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
1.202 schwarze 109: static char help_arg[] = "help";
110: static char *help_argv[] = {help_arg, NULL};
1.54 kristaps 111: static const char *progname;
1.1 kristaps 112:
1.173 schwarze 113:
1.1 kristaps 114: int
115: main(int argc, char *argv[])
116: {
1.5 kristaps 117: struct curparse curp;
1.180 schwarze 118: struct mansearch search;
119: struct manpaths paths;
1.205 schwarze 120: char *auxpaths;
1.180 schwarze 121: char *defos;
1.207 schwarze 122: unsigned char *uc;
1.186 schwarze 123: struct manpage *res, *resp;
1.205 schwarze 124: char *conf_file, *defpaths;
1.182 schwarze 125: size_t isec, i, sz;
1.205 schwarze 126: int prio, best_prio, synopsis_only;
1.182 schwarze 127: char sec;
1.214 schwarze 128: enum mandoclevel rc, rctmp;
1.181 schwarze 129: enum outmode outmode;
1.192 schwarze 130: int fd;
1.180 schwarze 131: int show_usage;
1.183 schwarze 132: int use_pager;
1.170 schwarze 133: int options;
1.180 schwarze 134: int c;
1.1 kristaps 135:
1.219 schwarze 136: if (argc < 1)
137: progname = "mandoc";
138: else if ((progname = strrchr(argv[0], '/')) == NULL)
1.54 kristaps 139: progname = argv[0];
140: else
141: ++progname;
1.203 schwarze 142:
143: #if HAVE_SQLITE3
1.204 schwarze 144: if (strcmp(progname, BINM_MAKEWHATIS) == 0)
1.203 schwarze 145: return(mandocdb(argc, argv));
146: #endif
1.179 schwarze 147:
1.180 schwarze 148: /* Search options. */
149:
150: memset(&paths, 0, sizeof(struct manpaths));
1.205 schwarze 151: conf_file = defpaths = NULL;
152: auxpaths = NULL;
1.180 schwarze 153:
154: memset(&search, 0, sizeof(struct mansearch));
155: search.outkey = "Nd";
156:
1.204 schwarze 157: if (strcmp(progname, BINM_MAN) == 0)
1.180 schwarze 158: search.argmode = ARG_NAME;
1.204 schwarze 159: else if (strcmp(progname, BINM_APROPOS) == 0)
1.180 schwarze 160: search.argmode = ARG_EXPR;
1.204 schwarze 161: else if (strcmp(progname, BINM_WHATIS) == 0)
1.180 schwarze 162: search.argmode = ARG_WORD;
1.202 schwarze 163: else if (strncmp(progname, "help", 4) == 0)
164: search.argmode = ARG_NAME;
1.180 schwarze 165: else
166: search.argmode = ARG_FILE;
167:
168: /* Parser and formatter options. */
1.54 kristaps 169:
1.51 kristaps 170: memset(&curp, 0, sizeof(struct curparse));
1.201 schwarze 171: curp.outtype = OUTT_LOCALE;
1.215 schwarze 172: curp.wlevel = MANDOCLEVEL_BADARG;
1.194 schwarze 173: options = MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1;
1.166 schwarze 174: defos = NULL;
1.19 kristaps 175:
1.183 schwarze 176: use_pager = 1;
1.180 schwarze 177: show_usage = 0;
1.197 schwarze 178: synopsis_only = 0;
1.181 schwarze 179: outmode = OUTMODE_DEF;
1.183 schwarze 180:
1.194 schwarze 181: while (-1 != (c = getopt(argc, argv,
182: "aC:cfhI:iK:klM:m:O:S:s:T:VW:w"))) {
1.1 kristaps 183: switch (c) {
1.181 schwarze 184: case 'a':
185: outmode = OUTMODE_ALL;
186: break;
1.180 schwarze 187: case 'C':
188: conf_file = optarg;
189: break;
1.183 schwarze 190: case 'c':
191: use_pager = 0;
192: break;
1.180 schwarze 193: case 'f':
194: search.argmode = ARG_WORD;
195: break;
1.190 schwarze 196: case 'h':
197: (void)strlcat(curp.outopts, "synopsis,", BUFSIZ);
1.197 schwarze 198: synopsis_only = 1;
1.198 schwarze 199: use_pager = 0;
1.190 schwarze 200: outmode = OUTMODE_ALL;
201: break;
1.173 schwarze 202: case 'I':
1.166 schwarze 203: if (strncmp(optarg, "os=", 3)) {
1.173 schwarze 204: fprintf(stderr,
1.209 schwarze 205: "%s: -I %s: Bad argument\n",
1.176 schwarze 206: progname, optarg);
1.166 schwarze 207: return((int)MANDOCLEVEL_BADARG);
208: }
209: if (defos) {
1.173 schwarze 210: fprintf(stderr,
1.209 schwarze 211: "%s: -I %s: Duplicate argument\n",
1.176 schwarze 212: progname, optarg);
1.166 schwarze 213: return((int)MANDOCLEVEL_BADARG);
214: }
215: defos = mandoc_strdup(optarg + 3);
216: break;
1.181 schwarze 217: case 'i':
218: outmode = OUTMODE_INT;
219: break;
1.194 schwarze 220: case 'K':
221: if ( ! koptions(&options, optarg))
222: return((int)MANDOCLEVEL_BADARG);
223: break;
1.180 schwarze 224: case 'k':
225: search.argmode = ARG_EXPR;
226: break;
1.188 schwarze 227: case 'l':
228: search.argmode = ARG_FILE;
229: outmode = OUTMODE_ALL;
230: break;
1.180 schwarze 231: case 'M':
232: defpaths = optarg;
233: break;
1.173 schwarze 234: case 'm':
1.180 schwarze 235: auxpaths = optarg;
1.10 kristaps 236: break;
1.173 schwarze 237: case 'O':
1.180 schwarze 238: search.outkey = optarg;
1.49 kristaps 239: (void)strlcat(curp.outopts, optarg, BUFSIZ);
240: (void)strlcat(curp.outopts, ",", BUFSIZ);
1.44 kristaps 241: break;
1.180 schwarze 242: case 'S':
243: search.arch = optarg;
244: break;
245: case 's':
246: search.sec = optarg;
247: break;
1.173 schwarze 248: case 'T':
1.60 kristaps 249: if ( ! toptions(&curp, optarg))
1.105 kristaps 250: return((int)MANDOCLEVEL_BADARG);
1.1 kristaps 251: break;
1.173 schwarze 252: case 'W':
1.103 schwarze 253: if ( ! woptions(&curp, optarg))
1.105 kristaps 254: return((int)MANDOCLEVEL_BADARG);
1.1 kristaps 255: break;
1.181 schwarze 256: case 'w':
257: outmode = OUTMODE_FLN;
258: break;
1.1 kristaps 259: default:
1.180 schwarze 260: show_usage = 1;
261: break;
1.1 kristaps 262: }
1.180 schwarze 263: }
264:
265: if (show_usage)
266: usage(search.argmode);
267:
1.185 schwarze 268: /* Postprocess options. */
269:
1.181 schwarze 270: if (outmode == OUTMODE_DEF) {
271: switch (search.argmode) {
272: case ARG_FILE:
273: outmode = OUTMODE_ALL;
1.183 schwarze 274: use_pager = 0;
1.181 schwarze 275: break;
276: case ARG_NAME:
277: outmode = OUTMODE_ONE;
278: break;
279: default:
280: outmode = OUTMODE_LST;
281: break;
282: }
283: }
284:
1.185 schwarze 285: /* Parse arguments. */
286:
1.219 schwarze 287: if (argc > 0) {
288: argc -= optind;
289: argv += optind;
290: }
1.186 schwarze 291: resp = NULL;
1.185 schwarze 292:
1.202 schwarze 293: /*
294: * Quirks for help(1)
295: * and for a man(1) section argument without -s.
296: */
297:
298: if (search.argmode == ARG_NAME) {
299: if (*progname == 'h') {
300: if (argc == 0) {
301: argv = help_argv;
302: argc = 1;
303: }
1.211 schwarze 304: } else if (argc > 1 &&
305: ((uc = argv[0]) != NULL) &&
1.207 schwarze 306: ((isdigit(uc[0]) && (uc[1] == '\0' ||
307: (isalpha(uc[1]) && uc[2] == '\0'))) ||
308: (uc[0] == 'n' && uc[1] == '\0'))) {
309: search.sec = uc;
1.202 schwarze 310: argv++;
311: argc--;
312: }
1.216 schwarze 313: if (search.arch == NULL)
314: search.arch = getenv("MACHINE");
315: if (search.arch == NULL)
316: search.arch = MACHINE;
1.185 schwarze 317: }
1.182 schwarze 318:
319: rc = MANDOCLEVEL_OK;
1.180 schwarze 320:
321: /* man(1), whatis(1), apropos(1) */
322:
323: if (search.argmode != ARG_FILE) {
324: if (argc == 0)
325: usage(search.argmode);
1.199 schwarze 326:
327: if (search.argmode == ARG_NAME &&
328: outmode == OUTMODE_ONE)
329: search.firstmatch = 1;
1.182 schwarze 330:
331: /* Access the mandoc database. */
332:
1.180 schwarze 333: manpath_parse(&paths, conf_file, defpaths, auxpaths);
1.218 schwarze 334: #if HAVE_SQLITE3
1.180 schwarze 335: mansearch_setup(1);
336: if( ! mansearch(&search, &paths, argc, argv, &res, &sz))
337: usage(search.argmode);
1.218 schwarze 338: #else
339: if (search.argmode != ARG_NAME) {
340: fputs("mandoc: database support not compiled in\n",
341: stderr);
342: return((int)MANDOCLEVEL_BADARG);
343: }
344: sz = 0;
345: #endif
1.213 schwarze 346:
347: if (sz == 0 && search.argmode == ARG_NAME)
348: fs_search(&search, &paths, argc, argv, &res, &sz);
1.187 schwarze 349:
350: if (sz == 0) {
351: rc = MANDOCLEVEL_BADARG;
352: goto out;
353: }
1.182 schwarze 354:
355: /*
356: * For standard man(1) and -a output mode,
357: * prepare for copying filename pointers
358: * into the program parameter array.
359: */
360:
361: if (outmode == OUTMODE_ONE) {
362: argc = 1;
363: best_prio = 10;
1.186 schwarze 364: } else if (outmode == OUTMODE_ALL)
1.182 schwarze 365: argc = (int)sz;
366:
367: /* Iterate all matching manuals. */
368:
1.213 schwarze 369: resp = res;
1.181 schwarze 370: for (i = 0; i < sz; i++) {
371: if (outmode == OUTMODE_FLN)
372: puts(res[i].file);
1.182 schwarze 373: else if (outmode == OUTMODE_LST)
1.181 schwarze 374: printf("%s - %s\n", res[i].names,
375: res[i].output == NULL ? "" :
376: res[i].output);
1.186 schwarze 377: else if (outmode == OUTMODE_ONE) {
1.182 schwarze 378: /* Search for the best section. */
379: isec = strcspn(res[i].file, "123456789");
380: sec = res[i].file[isec];
381: if ('\0' == sec)
382: continue;
383: prio = sec_prios[sec - '1'];
384: if (prio >= best_prio)
385: continue;
386: best_prio = prio;
1.186 schwarze 387: resp = res + i;
1.182 schwarze 388: }
1.181 schwarze 389: }
1.182 schwarze 390:
391: /*
392: * For man(1), -a and -i output mode, fall through
393: * to the main mandoc(1) code iterating files
394: * and running the parsers on each of them.
395: */
396:
397: if (outmode == OUTMODE_FLN || outmode == OUTMODE_LST)
398: goto out;
1.180 schwarze 399: }
400:
401: /* mandoc(1) */
402:
1.209 schwarze 403: if (search.argmode == ARG_FILE && ! moptions(&options, auxpaths))
1.180 schwarze 404: return((int)MANDOCLEVEL_BADARG);
1.1 kristaps 405:
1.195 schwarze 406: curp.mchars = mchars_alloc();
407: curp.mp = mparse_alloc(options, curp.wlevel, mmsg,
408: curp.mchars, defos);
1.154 kristaps 409:
1.165 kristaps 410: /*
411: * Conditionally start up the lookaside buffer before parsing.
412: */
413: if (OUTT_MAN == curp.outtype)
414: mparse_keep(curp.mp);
415:
1.219 schwarze 416: if (argc < 1) {
1.212 schwarze 417: if (use_pager && isatty(STDOUT_FILENO))
418: spawn_pager();
1.154 kristaps 419: parse(&curp, STDIN_FILENO, "<stdin>", &rc);
1.212 schwarze 420: }
1.64 kristaps 421:
1.219 schwarze 422: while (argc > 0) {
1.214 schwarze 423: rctmp = mparse_open(curp.mp, &fd,
1.218 schwarze 424: resp != NULL ? resp->file : *argv);
1.214 schwarze 425: if (rc < rctmp)
426: rc = rctmp;
1.212 schwarze 427:
428: if (fd != -1) {
429: if (use_pager && isatty(STDOUT_FILENO))
430: spawn_pager();
431: use_pager = 0;
432:
433: if (resp == NULL)
434: parse(&curp, fd, *argv, &rc);
1.192 schwarze 435: else if (resp->form & FORM_SRC) {
1.189 schwarze 436: /* For .so only; ignore failure. */
437: chdir(paths.paths[resp->ipath]);
1.192 schwarze 438: parse(&curp, fd, resp->file, &rc);
1.214 schwarze 439: } else {
440: rctmp = passthrough(resp->file, fd,
1.197 schwarze 441: synopsis_only);
1.214 schwarze 442: if (rc < rctmp)
443: rc = rctmp;
444: }
1.212 schwarze 445:
1.214 schwarze 446: rctmp = mparse_wait(curp.mp);
447: if (rc < rctmp)
448: rc = rctmp;
1.212 schwarze 449:
450: if (argc > 1 && curp.outtype <= OUTT_UTF8)
451: ascii_sepline(curp.outdata);
1.192 schwarze 452: }
453:
1.154 kristaps 454: if (MANDOCLEVEL_OK != rc && curp.wstop)
1.64 kristaps 455: break;
1.210 schwarze 456:
1.212 schwarze 457: if (resp != NULL)
458: resp++;
459: else
460: argv++;
461: if (--argc)
462: mparse_reset(curp.mp);
1.1 kristaps 463: }
464:
1.22 kristaps 465: if (curp.outfree)
466: (*curp.outfree)(curp.outdata);
1.195 schwarze 467: mparse_free(curp.mp);
468: mchars_free(curp.mchars);
1.182 schwarze 469:
470: out:
471: if (search.argmode != ARG_FILE) {
1.189 schwarze 472: manpath_free(&paths);
1.218 schwarze 473: #if HAVE_SQLITE3
1.182 schwarze 474: mansearch_free(res, sz);
475: mansearch_setup(0);
1.218 schwarze 476: #endif
1.182 schwarze 477: }
478:
1.166 schwarze 479: free(defos);
1.1 kristaps 480:
1.154 kristaps 481: return((int)rc);
1.1 kristaps 482: }
483:
1.55 kristaps 484: static void
1.180 schwarze 485: usage(enum argmode argmode)
1.1 kristaps 486: {
487:
1.180 schwarze 488: switch (argmode) {
489: case ARG_FILE:
1.221 ! schwarze 490: fputs("usage: mandoc [-acfhkl] [-Ios=name] "
1.196 schwarze 491: "[-Kencoding] [-mformat] [-Ooption]\n"
492: "\t [-Toutput] [-Wlevel] [file ...]\n", stderr);
1.180 schwarze 493: break;
494: case ARG_NAME:
1.221 ! schwarze 495: fputs("usage: man [-acfhklw] [-C file] [-I os=name] "
1.208 schwarze 496: "[-K encoding] [-M path] [-m path]\n"
497: "\t [-O option=value] [-S subsection] [-s section] "
498: "[-T output] [-W level]\n"
1.180 schwarze 499: "\t [section] name ...\n", stderr);
500: break;
501: case ARG_WORD:
1.221 ! schwarze 502: fputs("usage: whatis [-acfhklw] [-C file] "
1.188 schwarze 503: "[-M path] [-m path] [-O outkey] [-S arch]\n"
504: "\t [-s section] name ...\n", stderr);
1.180 schwarze 505: break;
506: case ARG_EXPR:
1.221 ! schwarze 507: fputs("usage: apropos [-acfhklw] [-C file] "
1.188 schwarze 508: "[-M path] [-m path] [-O outkey] [-S arch]\n"
1.180 schwarze 509: "\t [-s section] expression ...\n", stderr);
510: break;
511: }
1.105 kristaps 512: exit((int)MANDOCLEVEL_BADARG);
1.68 joerg 513: }
1.213 schwarze 514:
515: static int
516: fs_lookup(const struct manpaths *paths, size_t ipath,
517: const char *sec, const char *arch, const char *name,
518: struct manpage **res, size_t *ressz)
519: {
520: struct manpage *page;
521: char *file;
522: int form;
523:
524: mandoc_asprintf(&file, "%s/man%s/%s.%s",
525: paths->paths[ipath], sec, name, sec);
526: if (access(file, R_OK) != -1) {
527: form = FORM_SRC;
528: goto found;
529: }
530: free(file);
531:
532: mandoc_asprintf(&file, "%s/cat%s/%s.0",
533: paths->paths[ipath], sec, name);
534: if (access(file, R_OK) != -1) {
535: form = FORM_CAT;
536: goto found;
537: }
538: free(file);
539:
540: if (arch != NULL) {
541: mandoc_asprintf(&file, "%s/man%s/%s/%s.%s",
542: paths->paths[ipath], sec, arch, name, sec);
543: if (access(file, R_OK) != -1) {
544: form = FORM_SRC;
545: goto found;
546: }
547: free(file);
548: }
549: return(0);
550:
551: found:
1.218 schwarze 552: #if HAVE_SQLITE3
1.213 schwarze 553: fprintf(stderr, "%s: outdated mandoc.db lacks %s(%s) entry,\n"
554: " consider running # makewhatis %s\n",
555: progname, name, sec, paths->paths[ipath]);
1.218 schwarze 556: #endif
1.220 schwarze 557:
1.213 schwarze 558: *res = mandoc_reallocarray(*res, ++*ressz, sizeof(struct manpage));
559: page = *res + (*ressz - 1);
560: page->file = file;
561: page->names = NULL;
562: page->output = NULL;
563: page->ipath = ipath;
564: page->bits = NAME_FILE & NAME_MASK;
565: page->sec = (*sec >= '1' && *sec <= '9') ? *sec - '1' + 1 : 10;
566: page->form = form;
567: return(1);
568: }
569:
570: static void
571: fs_search(const struct mansearch *cfg, const struct manpaths *paths,
572: int argc, char **argv, struct manpage **res, size_t *ressz)
573: {
574: const char *const sections[] =
575: {"1", "8", "6", "2", "3", "3p", "5", "7", "4", "9"};
576: const size_t nsec = sizeof(sections)/sizeof(sections[0]);
577:
578: size_t ipath, isec, lastsz;
579:
580: assert(cfg->argmode == ARG_NAME);
581:
582: *res = NULL;
583: *ressz = lastsz = 0;
584: while (argc) {
585: for (ipath = 0; ipath < paths->sz; ipath++) {
586: if (cfg->sec != NULL) {
587: if (fs_lookup(paths, ipath, cfg->sec,
588: cfg->arch, *argv, res, ressz) &&
589: cfg->firstmatch)
590: return;
591: } else for (isec = 0; isec < nsec; isec++)
592: if (fs_lookup(paths, ipath, sections[isec],
593: cfg->arch, *argv, res, ressz) &&
594: cfg->firstmatch)
595: return;
596: }
597: if (*ressz == lastsz)
598: fprintf(stderr,
599: "%s: No entry for %s in the manual.\n",
600: progname, *argv);
601: lastsz = *ressz;
602: argv++;
603: argc--;
604: }
605: }
1.68 joerg 606:
1.64 kristaps 607: static void
1.173 schwarze 608: parse(struct curparse *curp, int fd, const char *file,
609: enum mandoclevel *level)
1.1 kristaps 610: {
1.154 kristaps 611: enum mandoclevel rc;
612: struct mdoc *mdoc;
613: struct man *man;
1.112 kristaps 614:
1.154 kristaps 615: /* Begin by parsing the file itself. */
1.112 kristaps 616:
1.154 kristaps 617: assert(file);
618: assert(fd >= -1);
1.112 kristaps 619:
1.154 kristaps 620: rc = mparse_readfd(curp->mp, fd, file);
1.112 kristaps 621:
1.1 kristaps 622: /*
1.154 kristaps 623: * With -Wstop and warnings or errors of at least the requested
624: * level, do not produce output.
1.1 kristaps 625: */
626:
1.154 kristaps 627: if (MANDOCLEVEL_OK != rc && curp->wstop)
1.111 kristaps 628: goto cleanup;
629:
630: /* If unset, allocate output dev now (if applicable). */
631:
632: if ( ! (curp->outman && curp->outmdoc)) {
633: switch (curp->outtype) {
1.173 schwarze 634: case OUTT_HTML:
1.195 schwarze 635: curp->outdata = html_alloc(curp->mchars,
636: curp->outopts);
1.162 kristaps 637: curp->outfree = html_free;
638: break;
1.173 schwarze 639: case OUTT_UTF8:
1.195 schwarze 640: curp->outdata = utf8_alloc(curp->mchars,
641: curp->outopts);
1.163 kristaps 642: curp->outfree = ascii_free;
643: break;
1.173 schwarze 644: case OUTT_LOCALE:
1.195 schwarze 645: curp->outdata = locale_alloc(curp->mchars,
646: curp->outopts);
1.162 kristaps 647: curp->outfree = ascii_free;
1.111 kristaps 648: break;
1.173 schwarze 649: case OUTT_ASCII:
1.195 schwarze 650: curp->outdata = ascii_alloc(curp->mchars,
651: curp->outopts);
1.111 kristaps 652: curp->outfree = ascii_free;
653: break;
1.173 schwarze 654: case OUTT_PDF:
1.195 schwarze 655: curp->outdata = pdf_alloc(curp->mchars,
656: curp->outopts);
1.111 kristaps 657: curp->outfree = pspdf_free;
658: break;
1.173 schwarze 659: case OUTT_PS:
1.195 schwarze 660: curp->outdata = ps_alloc(curp->mchars,
661: curp->outopts);
1.111 kristaps 662: curp->outfree = pspdf_free;
663: break;
664: default:
665: break;
666: }
667:
668: switch (curp->outtype) {
1.173 schwarze 669: case OUTT_HTML:
1.111 kristaps 670: curp->outman = html_man;
671: curp->outmdoc = html_mdoc;
672: break;
1.173 schwarze 673: case OUTT_TREE:
1.111 kristaps 674: curp->outman = tree_man;
675: curp->outmdoc = tree_mdoc;
676: break;
1.173 schwarze 677: case OUTT_MAN:
1.164 schwarze 678: curp->outmdoc = man_mdoc;
1.165 kristaps 679: curp->outman = man_man;
1.164 schwarze 680: break;
1.173 schwarze 681: case OUTT_PDF:
1.111 kristaps 682: /* FALLTHROUGH */
1.173 schwarze 683: case OUTT_ASCII:
1.111 kristaps 684: /* FALLTHROUGH */
1.173 schwarze 685: case OUTT_UTF8:
1.163 kristaps 686: /* FALLTHROUGH */
1.173 schwarze 687: case OUTT_LOCALE:
1.162 kristaps 688: /* FALLTHROUGH */
1.173 schwarze 689: case OUTT_PS:
1.111 kristaps 690: curp->outman = terminal_man;
691: curp->outmdoc = terminal_mdoc;
692: break;
693: default:
694: break;
695: }
696: }
697:
1.171 schwarze 698: mparse_result(curp->mp, &mdoc, &man, NULL);
1.154 kristaps 699:
1.111 kristaps 700: /* Execute the out device, if it exists. */
701:
1.154 kristaps 702: if (man && curp->outman)
703: (*curp->outman)(curp->outdata, man);
704: if (mdoc && curp->outmdoc)
705: (*curp->outmdoc)(curp->outdata, mdoc);
1.111 kristaps 706:
1.212 schwarze 707: cleanup:
1.154 kristaps 708: if (*level < rc)
709: *level = rc;
1.186 schwarze 710: }
711:
712: static enum mandoclevel
1.197 schwarze 713: passthrough(const char *file, int fd, int synopsis_only)
1.186 schwarze 714: {
1.197 schwarze 715: const char synb[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
716: const char synr[] = "SYNOPSIS";
717:
718: FILE *stream;
1.186 schwarze 719: const char *syscall;
1.197 schwarze 720: char *line;
721: size_t len, off;
722: ssize_t nw;
723: int print;
1.210 schwarze 724:
725: fflush(stdout);
1.197 schwarze 726:
727: if ((stream = fdopen(fd, "r")) == NULL) {
728: close(fd);
729: syscall = "fdopen";
730: goto fail;
731: }
1.186 schwarze 732:
1.197 schwarze 733: print = 0;
734: while ((line = fgetln(stream, &len)) != NULL) {
735: if (synopsis_only) {
736: if (print) {
737: if ( ! isspace((unsigned char)*line))
738: goto done;
739: while (len &&
740: isspace((unsigned char)*line)) {
741: line++;
742: len--;
743: }
744: } else {
745: if ((len == sizeof(synb) &&
746: ! strncmp(line, synb, len - 1)) ||
747: (len == sizeof(synr) &&
748: ! strncmp(line, synr, len - 1)))
749: print = 1;
750: continue;
751: }
752: }
753: for (off = 0; off < len; off += nw)
754: if ((nw = write(STDOUT_FILENO, line + off,
755: len - off)) == -1 || nw == 0) {
756: fclose(stream);
1.186 schwarze 757: syscall = "write";
758: goto fail;
759: }
1.197 schwarze 760: }
1.186 schwarze 761:
1.197 schwarze 762: if (ferror(stream)) {
763: fclose(stream);
764: syscall = "fgetln";
765: goto fail;
766: }
1.193 schwarze 767:
1.197 schwarze 768: done:
769: fclose(stream);
770: return(MANDOCLEVEL_OK);
1.186 schwarze 771:
772: fail:
773: fprintf(stderr, "%s: %s: SYSERR: %s: %s",
774: progname, file, syscall, strerror(errno));
775: return(MANDOCLEVEL_SYSERR);
1.194 schwarze 776: }
777:
778: static int
779: koptions(int *options, char *arg)
780: {
781:
782: if ( ! strcmp(arg, "utf-8")) {
783: *options |= MPARSE_UTF8;
784: *options &= ~MPARSE_LATIN1;
785: } else if ( ! strcmp(arg, "iso-8859-1")) {
786: *options |= MPARSE_LATIN1;
787: *options &= ~MPARSE_UTF8;
788: } else if ( ! strcmp(arg, "us-ascii")) {
789: *options &= ~(MPARSE_UTF8 | MPARSE_LATIN1);
790: } else {
1.209 schwarze 791: fprintf(stderr, "%s: -K %s: Bad argument\n",
1.194 schwarze 792: progname, arg);
793: return(0);
794: }
795: return(1);
1.10 kristaps 796: }
797:
798: static int
1.170 schwarze 799: moptions(int *options, char *arg)
1.10 kristaps 800: {
801:
1.180 schwarze 802: if (arg == NULL)
803: /* nothing to do */;
804: else if (0 == strcmp(arg, "doc"))
1.170 schwarze 805: *options |= MPARSE_MDOC;
1.19 kristaps 806: else if (0 == strcmp(arg, "andoc"))
1.170 schwarze 807: /* nothing to do */;
1.17 kristaps 808: else if (0 == strcmp(arg, "an"))
1.170 schwarze 809: *options |= MPARSE_MAN;
1.10 kristaps 810: else {
1.209 schwarze 811: fprintf(stderr, "%s: -m %s: Bad argument\n",
1.176 schwarze 812: progname, arg);
1.10 kristaps 813: return(0);
814: }
815:
816: return(1);
1.1 kristaps 817: }
818:
819: static int
1.60 kristaps 820: toptions(struct curparse *curp, char *arg)
1.1 kristaps 821: {
822:
823: if (0 == strcmp(arg, "ascii"))
1.60 kristaps 824: curp->outtype = OUTT_ASCII;
825: else if (0 == strcmp(arg, "lint")) {
826: curp->outtype = OUTT_LINT;
1.103 schwarze 827: curp->wlevel = MANDOCLEVEL_WARNING;
1.154 kristaps 828: } else if (0 == strcmp(arg, "tree"))
1.60 kristaps 829: curp->outtype = OUTT_TREE;
1.164 schwarze 830: else if (0 == strcmp(arg, "man"))
831: curp->outtype = OUTT_MAN;
1.43 kristaps 832: else if (0 == strcmp(arg, "html"))
1.60 kristaps 833: curp->outtype = OUTT_HTML;
1.163 kristaps 834: else if (0 == strcmp(arg, "utf8"))
835: curp->outtype = OUTT_UTF8;
1.162 kristaps 836: else if (0 == strcmp(arg, "locale"))
837: curp->outtype = OUTT_LOCALE;
1.59 kristaps 838: else if (0 == strcmp(arg, "xhtml"))
1.195 schwarze 839: curp->outtype = OUTT_HTML;
1.85 kristaps 840: else if (0 == strcmp(arg, "ps"))
841: curp->outtype = OUTT_PS;
1.100 kristaps 842: else if (0 == strcmp(arg, "pdf"))
843: curp->outtype = OUTT_PDF;
1.1 kristaps 844: else {
1.209 schwarze 845: fprintf(stderr, "%s: -T %s: Bad argument\n",
1.176 schwarze 846: progname, arg);
1.1 kristaps 847: return(0);
848: }
849:
850: return(1);
851: }
852:
853: static int
1.103 schwarze 854: woptions(struct curparse *curp, char *arg)
1.1 kristaps 855: {
1.34 kristaps 856: char *v, *o;
1.217 schwarze 857: const char *toks[7];
1.1 kristaps 858:
1.103 schwarze 859: toks[0] = "stop";
860: toks[1] = "all";
861: toks[2] = "warning";
862: toks[3] = "error";
1.217 schwarze 863: toks[4] = "unsupp";
864: toks[5] = "fatal";
865: toks[6] = NULL;
1.1 kristaps 866:
1.34 kristaps 867: while (*arg) {
868: o = arg;
1.45 kristaps 869: switch (getsubopt(&arg, UNCONST(toks), &v)) {
1.173 schwarze 870: case 0:
1.103 schwarze 871: curp->wstop = 1;
1.1 kristaps 872: break;
1.173 schwarze 873: case 1:
1.103 schwarze 874: /* FALLTHROUGH */
1.173 schwarze 875: case 2:
1.103 schwarze 876: curp->wlevel = MANDOCLEVEL_WARNING;
1.1 kristaps 877: break;
1.173 schwarze 878: case 3:
1.103 schwarze 879: curp->wlevel = MANDOCLEVEL_ERROR;
1.24 kristaps 880: break;
1.173 schwarze 881: case 4:
1.217 schwarze 882: curp->wlevel = MANDOCLEVEL_UNSUPP;
883: break;
884: case 5:
1.215 schwarze 885: curp->wlevel = MANDOCLEVEL_BADARG;
1.50 kristaps 886: break;
1.1 kristaps 887: default:
1.209 schwarze 888: fprintf(stderr, "%s: -W %s: Bad argument\n",
1.176 schwarze 889: progname, o);
1.1 kristaps 890: return(0);
891: }
1.34 kristaps 892: }
1.1 kristaps 893:
894: return(1);
895: }
896:
1.153 kristaps 897: static void
1.173 schwarze 898: mmsg(enum mandocerr t, enum mandoclevel lvl,
1.155 kristaps 899: const char *file, int line, int col, const char *msg)
1.71 kristaps 900: {
1.177 schwarze 901: const char *mparse_msg;
1.103 schwarze 902:
1.175 schwarze 903: fprintf(stderr, "%s: %s:", progname, file);
904:
905: if (line)
906: fprintf(stderr, "%d:%d:", line, col + 1);
907:
1.177 schwarze 908: fprintf(stderr, " %s", mparse_strlevel(lvl));
909:
910: if (NULL != (mparse_msg = mparse_strerror(t)))
911: fprintf(stderr, ": %s", mparse_msg);
1.154 kristaps 912:
1.73 kristaps 913: if (msg)
914: fprintf(stderr, ": %s", msg);
1.154 kristaps 915:
1.73 kristaps 916: fputc('\n', stderr);
1.183 schwarze 917: }
918:
919: static void
920: spawn_pager(void)
921: {
1.184 schwarze 922: #define MAX_PAGER_ARGS 16
923: char *argv[MAX_PAGER_ARGS];
924: const char *pager;
925: char *cp;
926: int fildes[2];
927: int argc;
1.183 schwarze 928:
929: if (pipe(fildes) == -1) {
930: fprintf(stderr, "%s: pipe: %s\n",
931: progname, strerror(errno));
932: return;
933: }
934:
935: switch (fork()) {
936: case -1:
937: fprintf(stderr, "%s: fork: %s\n",
938: progname, strerror(errno));
939: exit((int)MANDOCLEVEL_SYSERR);
940: case 0:
941: close(fildes[0]);
942: if (dup2(fildes[1], STDOUT_FILENO) == -1) {
943: fprintf(stderr, "%s: dup output: %s\n",
944: progname, strerror(errno));
945: exit((int)MANDOCLEVEL_SYSERR);
946: }
947: return;
948: default:
1.184 schwarze 949: break;
950: }
951:
952: /* The original process becomes the pager. */
953:
954: close(fildes[1]);
955: if (dup2(fildes[0], STDIN_FILENO) == -1) {
956: fprintf(stderr, "%s: dup input: %s\n",
957: progname, strerror(errno));
1.183 schwarze 958: exit((int)MANDOCLEVEL_SYSERR);
959: }
1.184 schwarze 960:
961: pager = getenv("MANPAGER");
962: if (pager == NULL || *pager == '\0')
963: pager = getenv("PAGER");
964: if (pager == NULL || *pager == '\0')
965: pager = "/usr/bin/more -s";
966: cp = mandoc_strdup(pager);
967:
968: /*
969: * Parse the pager command into words.
970: * Intentionally do not do anything fancy here.
971: */
972:
973: argc = 0;
974: while (argc + 1 < MAX_PAGER_ARGS) {
975: argv[argc++] = cp;
976: cp = strchr(cp, ' ');
977: if (cp == NULL)
978: break;
979: *cp++ = '\0';
980: while (*cp == ' ')
981: cp++;
982: if (*cp == '\0')
983: break;
984: }
985: argv[argc] = NULL;
986:
987: /* Hand over to the pager. */
988:
989: execvp(argv[0], argv);
990: fprintf(stderr, "%s: exec: %s\n",
991: progname, strerror(errno));
992: exit((int)MANDOCLEVEL_SYSERR);
1.71 kristaps 993: }
CVSweb