]> git.cameronkatri.com Git - mandoc.git/blob - main.c
correctly handle scaling units after .PD
[mandoc.git] / main.c
1 /* $Id: main.c,v 1.209 2014/12/21 14:49:28 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 (((uc = argv[0]) != NULL) &&
314 ((isdigit(uc[0]) && (uc[1] == '\0' ||
315 (isalpha(uc[1]) && uc[2] == '\0'))) ||
316 (uc[0] == 'n' && uc[1] == '\0'))) {
317 search.sec = uc;
318 argv++;
319 argc--;
320 }
321 }
322
323 rc = MANDOCLEVEL_OK;
324
325 /* man(1), whatis(1), apropos(1) */
326
327 if (search.argmode != ARG_FILE) {
328 #if HAVE_SQLITE3
329 if (argc == 0)
330 usage(search.argmode);
331
332 if (search.argmode == ARG_NAME &&
333 outmode == OUTMODE_ONE)
334 search.firstmatch = 1;
335
336 /* Access the mandoc database. */
337
338 manpath_parse(&paths, conf_file, defpaths, auxpaths);
339 mansearch_setup(1);
340 if( ! mansearch(&search, &paths, argc, argv, &res, &sz))
341 usage(search.argmode);
342 resp = res;
343
344 if (sz == 0) {
345 if (search.argmode == ARG_NAME)
346 fprintf(stderr, "%s: No entry for %s "
347 "in the manual.\n", progname, argv[0]);
348 rc = MANDOCLEVEL_BADARG;
349 goto out;
350 }
351
352 /*
353 * For standard man(1) and -a output mode,
354 * prepare for copying filename pointers
355 * into the program parameter array.
356 */
357
358 if (outmode == OUTMODE_ONE) {
359 argc = 1;
360 best_prio = 10;
361 } else if (outmode == OUTMODE_ALL)
362 argc = (int)sz;
363
364 /* Iterate all matching manuals. */
365
366 for (i = 0; i < sz; i++) {
367 if (outmode == OUTMODE_FLN)
368 puts(res[i].file);
369 else if (outmode == OUTMODE_LST)
370 printf("%s - %s\n", res[i].names,
371 res[i].output == NULL ? "" :
372 res[i].output);
373 else if (outmode == OUTMODE_ONE) {
374 /* Search for the best section. */
375 isec = strcspn(res[i].file, "123456789");
376 sec = res[i].file[isec];
377 if ('\0' == sec)
378 continue;
379 prio = sec_prios[sec - '1'];
380 if (prio >= best_prio)
381 continue;
382 best_prio = prio;
383 resp = res + i;
384 }
385 }
386
387 /*
388 * For man(1), -a and -i output mode, fall through
389 * to the main mandoc(1) code iterating files
390 * and running the parsers on each of them.
391 */
392
393 if (outmode == OUTMODE_FLN || outmode == OUTMODE_LST)
394 goto out;
395 #else
396 fputs("mandoc: database support not compiled in\n",
397 stderr);
398 return((int)MANDOCLEVEL_BADARG);
399 #endif
400 }
401
402 /* mandoc(1) */
403
404 if (search.argmode == ARG_FILE && ! moptions(&options, auxpaths))
405 return((int)MANDOCLEVEL_BADARG);
406
407 if (use_pager && isatty(STDOUT_FILENO))
408 spawn_pager();
409
410 curp.mchars = mchars_alloc();
411 curp.mp = mparse_alloc(options, curp.wlevel, mmsg,
412 curp.mchars, defos);
413
414 /*
415 * Conditionally start up the lookaside buffer before parsing.
416 */
417 if (OUTT_MAN == curp.outtype)
418 mparse_keep(curp.mp);
419
420 if (argc == 0)
421 parse(&curp, STDIN_FILENO, "<stdin>", &rc);
422
423 while (argc) {
424 #if HAVE_SQLITE3
425 if (resp != NULL) {
426 rc = mparse_open(curp.mp, &fd, resp->file);
427 if (fd == -1)
428 /* nothing */;
429 else if (resp->form & FORM_SRC) {
430 /* For .so only; ignore failure. */
431 chdir(paths.paths[resp->ipath]);
432 parse(&curp, fd, resp->file, &rc);
433 } else
434 rc = passthrough(resp->file, fd,
435 synopsis_only);
436 resp++;
437 } else
438 #endif
439 {
440 rc = mparse_open(curp.mp, &fd, *argv++);
441 if (fd != -1)
442 parse(&curp, fd, argv[-1], &rc);
443 }
444
445 if (mparse_wait(curp.mp) != MANDOCLEVEL_OK)
446 rc = MANDOCLEVEL_SYSERR;
447
448 if (MANDOCLEVEL_OK != rc && curp.wstop)
449 break;
450 argc--;
451 }
452
453 if (curp.outfree)
454 (*curp.outfree)(curp.outdata);
455 mparse_free(curp.mp);
456 mchars_free(curp.mchars);
457
458 #if HAVE_SQLITE3
459 out:
460 if (search.argmode != ARG_FILE) {
461 manpath_free(&paths);
462 mansearch_free(res, sz);
463 mansearch_setup(0);
464 }
465 #endif
466
467 free(defos);
468
469 return((int)rc);
470 }
471
472 static void
473 version(void)
474 {
475
476 printf("mandoc %s\n", VERSION);
477 exit((int)MANDOCLEVEL_OK);
478 }
479
480 static void
481 usage(enum argmode argmode)
482 {
483
484 switch (argmode) {
485 case ARG_FILE:
486 fputs("usage: mandoc [-acfhklV] [-Ios=name] "
487 "[-Kencoding] [-mformat] [-Ooption]\n"
488 "\t [-Toutput] [-Wlevel] [file ...]\n", stderr);
489 break;
490 case ARG_NAME:
491 fputs("usage: man [-acfhklVw] [-C file] [-I os=name] "
492 "[-K encoding] [-M path] [-m path]\n"
493 "\t [-O option=value] [-S subsection] [-s section] "
494 "[-T output] [-W level]\n"
495 "\t [section] name ...\n", stderr);
496 break;
497 case ARG_WORD:
498 fputs("usage: whatis [-acfhklVw] [-C file] "
499 "[-M path] [-m path] [-O outkey] [-S arch]\n"
500 "\t [-s section] name ...\n", stderr);
501 break;
502 case ARG_EXPR:
503 fputs("usage: apropos [-acfhklVw] [-C file] "
504 "[-M path] [-m path] [-O outkey] [-S arch]\n"
505 "\t [-s section] expression ...\n", stderr);
506 break;
507 }
508 exit((int)MANDOCLEVEL_BADARG);
509 }
510
511 static void
512 parse(struct curparse *curp, int fd, const char *file,
513 enum mandoclevel *level)
514 {
515 enum mandoclevel rc;
516 struct mdoc *mdoc;
517 struct man *man;
518
519 /* Begin by parsing the file itself. */
520
521 assert(file);
522 assert(fd >= -1);
523
524 rc = mparse_readfd(curp->mp, fd, file);
525
526 /* Stop immediately if the parse has failed. */
527
528 if (MANDOCLEVEL_FATAL <= rc)
529 goto cleanup;
530
531 /*
532 * With -Wstop and warnings or errors of at least the requested
533 * level, do not produce output.
534 */
535
536 if (MANDOCLEVEL_OK != rc && curp->wstop)
537 goto cleanup;
538
539 /* If unset, allocate output dev now (if applicable). */
540
541 if ( ! (curp->outman && curp->outmdoc)) {
542 switch (curp->outtype) {
543 case OUTT_HTML:
544 curp->outdata = html_alloc(curp->mchars,
545 curp->outopts);
546 curp->outfree = html_free;
547 break;
548 case OUTT_UTF8:
549 curp->outdata = utf8_alloc(curp->mchars,
550 curp->outopts);
551 curp->outfree = ascii_free;
552 break;
553 case OUTT_LOCALE:
554 curp->outdata = locale_alloc(curp->mchars,
555 curp->outopts);
556 curp->outfree = ascii_free;
557 break;
558 case OUTT_ASCII:
559 curp->outdata = ascii_alloc(curp->mchars,
560 curp->outopts);
561 curp->outfree = ascii_free;
562 break;
563 case OUTT_PDF:
564 curp->outdata = pdf_alloc(curp->mchars,
565 curp->outopts);
566 curp->outfree = pspdf_free;
567 break;
568 case OUTT_PS:
569 curp->outdata = ps_alloc(curp->mchars,
570 curp->outopts);
571 curp->outfree = pspdf_free;
572 break;
573 default:
574 break;
575 }
576
577 switch (curp->outtype) {
578 case OUTT_HTML:
579 curp->outman = html_man;
580 curp->outmdoc = html_mdoc;
581 break;
582 case OUTT_TREE:
583 curp->outman = tree_man;
584 curp->outmdoc = tree_mdoc;
585 break;
586 case OUTT_MAN:
587 curp->outmdoc = man_mdoc;
588 curp->outman = man_man;
589 break;
590 case OUTT_PDF:
591 /* FALLTHROUGH */
592 case OUTT_ASCII:
593 /* FALLTHROUGH */
594 case OUTT_UTF8:
595 /* FALLTHROUGH */
596 case OUTT_LOCALE:
597 /* FALLTHROUGH */
598 case OUTT_PS:
599 curp->outman = terminal_man;
600 curp->outmdoc = terminal_mdoc;
601 break;
602 default:
603 break;
604 }
605 }
606
607 mparse_result(curp->mp, &mdoc, &man, NULL);
608
609 /* Execute the out device, if it exists. */
610
611 if (man && curp->outman)
612 (*curp->outman)(curp->outdata, man);
613 if (mdoc && curp->outmdoc)
614 (*curp->outmdoc)(curp->outdata, mdoc);
615
616 cleanup:
617
618 mparse_reset(curp->mp);
619
620 if (*level < rc)
621 *level = rc;
622 }
623
624 #if HAVE_SQLITE3
625 static enum mandoclevel
626 passthrough(const char *file, int fd, int synopsis_only)
627 {
628 const char synb[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
629 const char synr[] = "SYNOPSIS";
630
631 FILE *stream;
632 const char *syscall;
633 char *line;
634 size_t len, off;
635 ssize_t nw;
636 int print;
637
638 if ((stream = fdopen(fd, "r")) == NULL) {
639 close(fd);
640 syscall = "fdopen";
641 goto fail;
642 }
643
644 print = 0;
645 while ((line = fgetln(stream, &len)) != NULL) {
646 if (synopsis_only) {
647 if (print) {
648 if ( ! isspace((unsigned char)*line))
649 goto done;
650 while (len &&
651 isspace((unsigned char)*line)) {
652 line++;
653 len--;
654 }
655 } else {
656 if ((len == sizeof(synb) &&
657 ! strncmp(line, synb, len - 1)) ||
658 (len == sizeof(synr) &&
659 ! strncmp(line, synr, len - 1)))
660 print = 1;
661 continue;
662 }
663 }
664 for (off = 0; off < len; off += nw)
665 if ((nw = write(STDOUT_FILENO, line + off,
666 len - off)) == -1 || nw == 0) {
667 fclose(stream);
668 syscall = "write";
669 goto fail;
670 }
671 }
672
673 if (ferror(stream)) {
674 fclose(stream);
675 syscall = "fgetln";
676 goto fail;
677 }
678
679 done:
680 fclose(stream);
681 return(MANDOCLEVEL_OK);
682
683 fail:
684 fprintf(stderr, "%s: %s: SYSERR: %s: %s",
685 progname, file, syscall, strerror(errno));
686 return(MANDOCLEVEL_SYSERR);
687 }
688 #endif
689
690 static int
691 koptions(int *options, char *arg)
692 {
693
694 if ( ! strcmp(arg, "utf-8")) {
695 *options |= MPARSE_UTF8;
696 *options &= ~MPARSE_LATIN1;
697 } else if ( ! strcmp(arg, "iso-8859-1")) {
698 *options |= MPARSE_LATIN1;
699 *options &= ~MPARSE_UTF8;
700 } else if ( ! strcmp(arg, "us-ascii")) {
701 *options &= ~(MPARSE_UTF8 | MPARSE_LATIN1);
702 } else {
703 fprintf(stderr, "%s: -K %s: Bad argument\n",
704 progname, arg);
705 return(0);
706 }
707 return(1);
708 }
709
710 static int
711 moptions(int *options, char *arg)
712 {
713
714 if (arg == NULL)
715 /* nothing to do */;
716 else if (0 == strcmp(arg, "doc"))
717 *options |= MPARSE_MDOC;
718 else if (0 == strcmp(arg, "andoc"))
719 /* nothing to do */;
720 else if (0 == strcmp(arg, "an"))
721 *options |= MPARSE_MAN;
722 else {
723 fprintf(stderr, "%s: -m %s: Bad argument\n",
724 progname, arg);
725 return(0);
726 }
727
728 return(1);
729 }
730
731 static int
732 toptions(struct curparse *curp, char *arg)
733 {
734
735 if (0 == strcmp(arg, "ascii"))
736 curp->outtype = OUTT_ASCII;
737 else if (0 == strcmp(arg, "lint")) {
738 curp->outtype = OUTT_LINT;
739 curp->wlevel = MANDOCLEVEL_WARNING;
740 } else if (0 == strcmp(arg, "tree"))
741 curp->outtype = OUTT_TREE;
742 else if (0 == strcmp(arg, "man"))
743 curp->outtype = OUTT_MAN;
744 else if (0 == strcmp(arg, "html"))
745 curp->outtype = OUTT_HTML;
746 else if (0 == strcmp(arg, "utf8"))
747 curp->outtype = OUTT_UTF8;
748 else if (0 == strcmp(arg, "locale"))
749 curp->outtype = OUTT_LOCALE;
750 else if (0 == strcmp(arg, "xhtml"))
751 curp->outtype = OUTT_HTML;
752 else if (0 == strcmp(arg, "ps"))
753 curp->outtype = OUTT_PS;
754 else if (0 == strcmp(arg, "pdf"))
755 curp->outtype = OUTT_PDF;
756 else {
757 fprintf(stderr, "%s: -T %s: Bad argument\n",
758 progname, arg);
759 return(0);
760 }
761
762 return(1);
763 }
764
765 static int
766 woptions(struct curparse *curp, char *arg)
767 {
768 char *v, *o;
769 const char *toks[6];
770
771 toks[0] = "stop";
772 toks[1] = "all";
773 toks[2] = "warning";
774 toks[3] = "error";
775 toks[4] = "fatal";
776 toks[5] = NULL;
777
778 while (*arg) {
779 o = arg;
780 switch (getsubopt(&arg, UNCONST(toks), &v)) {
781 case 0:
782 curp->wstop = 1;
783 break;
784 case 1:
785 /* FALLTHROUGH */
786 case 2:
787 curp->wlevel = MANDOCLEVEL_WARNING;
788 break;
789 case 3:
790 curp->wlevel = MANDOCLEVEL_ERROR;
791 break;
792 case 4:
793 curp->wlevel = MANDOCLEVEL_FATAL;
794 break;
795 default:
796 fprintf(stderr, "%s: -W %s: Bad argument\n",
797 progname, o);
798 return(0);
799 }
800 }
801
802 return(1);
803 }
804
805 static void
806 mmsg(enum mandocerr t, enum mandoclevel lvl,
807 const char *file, int line, int col, const char *msg)
808 {
809 const char *mparse_msg;
810
811 fprintf(stderr, "%s: %s:", progname, file);
812
813 if (line)
814 fprintf(stderr, "%d:%d:", line, col + 1);
815
816 fprintf(stderr, " %s", mparse_strlevel(lvl));
817
818 if (NULL != (mparse_msg = mparse_strerror(t)))
819 fprintf(stderr, ": %s", mparse_msg);
820
821 if (msg)
822 fprintf(stderr, ": %s", msg);
823
824 fputc('\n', stderr);
825 }
826
827 static void
828 spawn_pager(void)
829 {
830 #define MAX_PAGER_ARGS 16
831 char *argv[MAX_PAGER_ARGS];
832 const char *pager;
833 char *cp;
834 int fildes[2];
835 int argc;
836
837 if (pipe(fildes) == -1) {
838 fprintf(stderr, "%s: pipe: %s\n",
839 progname, strerror(errno));
840 return;
841 }
842
843 switch (fork()) {
844 case -1:
845 fprintf(stderr, "%s: fork: %s\n",
846 progname, strerror(errno));
847 exit((int)MANDOCLEVEL_SYSERR);
848 case 0:
849 close(fildes[0]);
850 if (dup2(fildes[1], STDOUT_FILENO) == -1) {
851 fprintf(stderr, "%s: dup output: %s\n",
852 progname, strerror(errno));
853 exit((int)MANDOCLEVEL_SYSERR);
854 }
855 return;
856 default:
857 break;
858 }
859
860 /* The original process becomes the pager. */
861
862 close(fildes[1]);
863 if (dup2(fildes[0], STDIN_FILENO) == -1) {
864 fprintf(stderr, "%s: dup input: %s\n",
865 progname, strerror(errno));
866 exit((int)MANDOCLEVEL_SYSERR);
867 }
868
869 pager = getenv("MANPAGER");
870 if (pager == NULL || *pager == '\0')
871 pager = getenv("PAGER");
872 if (pager == NULL || *pager == '\0')
873 pager = "/usr/bin/more -s";
874 cp = mandoc_strdup(pager);
875
876 /*
877 * Parse the pager command into words.
878 * Intentionally do not do anything fancy here.
879 */
880
881 argc = 0;
882 while (argc + 1 < MAX_PAGER_ARGS) {
883 argv[argc++] = cp;
884 cp = strchr(cp, ' ');
885 if (cp == NULL)
886 break;
887 *cp++ = '\0';
888 while (*cp == ' ')
889 cp++;
890 if (*cp == '\0')
891 break;
892 }
893 argv[argc] = NULL;
894
895 /* Hand over to the pager. */
896
897 execvp(argv[0], argv);
898 fprintf(stderr, "%s: exec: %s\n",
899 progname, strerror(errno));
900 exit((int)MANDOCLEVEL_SYSERR);
901 }