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