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