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