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