]> git.cameronkatri.com Git - mandoc.git/blob - main.c
This one needs config.h too, if only for __BEGIN_DECLS.
[mandoc.git] / main.c
1 /* $Id: main.c,v 1.238 2015/04/29 11:04:17 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 "main.h"
43 #include "manconf.h"
44 #include "mansearch.h"
45
46 #if !defined(__GNUC__) || (__GNUC__ < 2)
47 # if !defined(lint)
48 # define __attribute__(x)
49 # endif
50 #endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
51
52 enum outmode {
53 OUTMODE_DEF = 0,
54 OUTMODE_FLN,
55 OUTMODE_LST,
56 OUTMODE_ALL,
57 OUTMODE_INT,
58 OUTMODE_ONE
59 };
60
61 typedef void (*out_mdoc)(void *, const struct roff_man *);
62 typedef void (*out_man)(void *, const struct roff_man *);
63 typedef void (*out_free)(void *);
64
65 enum outt {
66 OUTT_ASCII = 0, /* -Tascii */
67 OUTT_LOCALE, /* -Tlocale */
68 OUTT_UTF8, /* -Tutf8 */
69 OUTT_TREE, /* -Ttree */
70 OUTT_MAN, /* -Tman */
71 OUTT_HTML, /* -Thtml */
72 OUTT_LINT, /* -Tlint */
73 OUTT_PS, /* -Tps */
74 OUTT_PDF /* -Tpdf */
75 };
76
77 struct curparse {
78 struct mparse *mp;
79 struct mchars *mchars; /* character table */
80 enum mandoclevel wlevel; /* ignore messages below this */
81 int wstop; /* stop after a file with a warning */
82 enum outt outtype; /* which output to use */
83 out_mdoc outmdoc; /* mdoc output ptr */
84 out_man outman; /* man output ptr */
85 out_free outfree; /* free output ptr */
86 void *outdata; /* data for output */
87 struct manoutput *outopts; /* output options */
88 };
89
90 static int fs_lookup(const struct manpaths *,
91 size_t ipath, const char *,
92 const char *, const char *,
93 struct manpage **, size_t *);
94 static void fs_search(const struct mansearch *,
95 const struct manpaths *, int, char**,
96 struct manpage **, size_t *);
97 static void handle_sigpipe(int);
98 static int koptions(int *, char *);
99 #if HAVE_SQLITE3
100 int mandocdb(int, char**);
101 #endif
102 static int moptions(int *, char *);
103 static void mmsg(enum mandocerr, enum mandoclevel,
104 const char *, int, int, const char *);
105 static void parse(struct curparse *, int, const char *);
106 static void passthrough(const char *, int, int);
107 static pid_t spawn_pager(void);
108 static int toptions(struct curparse *, char *);
109 static void usage(enum argmode) __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 static enum mandoclevel rc;
117
118
119 int
120 main(int argc, char *argv[])
121 {
122 struct manconf conf;
123 struct curparse curp;
124 struct mansearch search;
125 char *auxpaths;
126 char *defos;
127 unsigned char *uc;
128 struct manpage *res, *resp;
129 char *conf_file, *defpaths;
130 size_t isec, i, sz;
131 int prio, best_prio;
132 char sec;
133 enum mandoclevel rctmp;
134 enum outmode outmode;
135 int fd;
136 int show_usage;
137 int options;
138 int c;
139 pid_t pager_pid; /* 0: don't use; 1: not yet spawned. */
140
141 if (argc < 1)
142 progname = "mandoc";
143 else if ((progname = strrchr(argv[0], '/')) == 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(&conf, 0, sizeof(conf));
156 conf_file = defpaths = NULL;
157 auxpaths = NULL;
158
159 memset(&search, 0, sizeof(struct mansearch));
160 search.outkey = "Nd";
161
162 if (strcmp(progname, BINM_MAN) == 0)
163 search.argmode = ARG_NAME;
164 else if (strcmp(progname, BINM_APROPOS) == 0)
165 search.argmode = ARG_EXPR;
166 else if (strcmp(progname, BINM_WHATIS) == 0)
167 search.argmode = ARG_WORD;
168 else if (strncmp(progname, "help", 4) == 0)
169 search.argmode = ARG_NAME;
170 else
171 search.argmode = ARG_FILE;
172
173 /* Parser and formatter options. */
174
175 memset(&curp, 0, sizeof(struct curparse));
176 curp.outtype = OUTT_LOCALE;
177 curp.wlevel = MANDOCLEVEL_BADARG;
178 curp.outopts = &conf.output;
179 options = MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1;
180 defos = NULL;
181
182 pager_pid = 1;
183 show_usage = 0;
184 outmode = OUTMODE_DEF;
185
186 while (-1 != (c = getopt(argc, argv,
187 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w"))) {
188 switch (c) {
189 case 'a':
190 outmode = OUTMODE_ALL;
191 break;
192 case 'C':
193 conf_file = optarg;
194 break;
195 case 'c':
196 pager_pid = 0;
197 break;
198 case 'f':
199 search.argmode = ARG_WORD;
200 break;
201 case 'h':
202 conf.output.synopsisonly = 1;
203 pager_pid = 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 defpaths = optarg;
237 break;
238 case 'm':
239 auxpaths = optarg;
240 break;
241 case 'O':
242 search.outkey = optarg;
243 while (optarg != NULL)
244 manconf_output(&conf.output,
245 strsep(&optarg, ","));
246 break;
247 case 'S':
248 search.arch = optarg;
249 break;
250 case 's':
251 search.sec = optarg;
252 break;
253 case 'T':
254 if ( ! toptions(&curp, optarg))
255 return((int)MANDOCLEVEL_BADARG);
256 break;
257 case 'W':
258 if ( ! woptions(&curp, optarg))
259 return((int)MANDOCLEVEL_BADARG);
260 break;
261 case 'w':
262 outmode = OUTMODE_FLN;
263 break;
264 default:
265 show_usage = 1;
266 break;
267 }
268 }
269
270 if (show_usage)
271 usage(search.argmode);
272
273 /* Postprocess options. */
274
275 if (outmode == OUTMODE_DEF) {
276 switch (search.argmode) {
277 case ARG_FILE:
278 outmode = OUTMODE_ALL;
279 pager_pid = 0;
280 break;
281 case ARG_NAME:
282 outmode = OUTMODE_ONE;
283 break;
284 default:
285 outmode = OUTMODE_LST;
286 break;
287 }
288 }
289
290 /* Parse arguments. */
291
292 if (argc > 0) {
293 argc -= optind;
294 argv += optind;
295 }
296 resp = NULL;
297
298 /*
299 * Quirks for help(1)
300 * and for a man(1) section argument without -s.
301 */
302
303 if (search.argmode == ARG_NAME) {
304 if (*progname == 'h') {
305 if (argc == 0) {
306 argv = help_argv;
307 argc = 1;
308 }
309 } else if (argc > 1 &&
310 ((uc = (unsigned char *)argv[0]) != NULL) &&
311 ((isdigit(uc[0]) && (uc[1] == '\0' ||
312 (isalpha(uc[1]) && uc[2] == '\0'))) ||
313 (uc[0] == 'n' && uc[1] == '\0'))) {
314 search.sec = (char *)uc;
315 argv++;
316 argc--;
317 }
318 if (search.arch == NULL)
319 search.arch = getenv("MACHINE");
320 #ifdef MACHINE
321 if (search.arch == NULL)
322 search.arch = MACHINE;
323 #endif
324 }
325
326 rc = MANDOCLEVEL_OK;
327
328 /* man(1), whatis(1), apropos(1) */
329
330 if (search.argmode != ARG_FILE) {
331 if (argc == 0)
332 usage(search.argmode);
333
334 if (search.argmode == ARG_NAME &&
335 outmode == OUTMODE_ONE)
336 search.firstmatch = 1;
337
338 /* Access the mandoc database. */
339
340 manconf_parse(&conf, conf_file, defpaths, auxpaths);
341 #if HAVE_SQLITE3
342 mansearch_setup(1);
343 if ( ! mansearch(&search, &conf.manpath,
344 argc, argv, &res, &sz))
345 usage(search.argmode);
346 #else
347 if (search.argmode != ARG_NAME) {
348 fputs("mandoc: database support not compiled in\n",
349 stderr);
350 return((int)MANDOCLEVEL_BADARG);
351 }
352 sz = 0;
353 #endif
354
355 if (sz == 0) {
356 if (search.argmode == ARG_NAME)
357 fs_search(&search, &conf.manpath,
358 argc, argv, &res, &sz);
359 else
360 fprintf(stderr,
361 "%s: nothing appropriate\n",
362 progname);
363 }
364
365 if (sz == 0) {
366 rc = MANDOCLEVEL_BADARG;
367 goto out;
368 }
369
370 /*
371 * For standard man(1) and -a output mode,
372 * prepare for copying filename pointers
373 * into the program parameter array.
374 */
375
376 if (outmode == OUTMODE_ONE) {
377 argc = 1;
378 best_prio = 10;
379 } else if (outmode == OUTMODE_ALL)
380 argc = (int)sz;
381
382 /* Iterate all matching manuals. */
383
384 resp = res;
385 for (i = 0; i < sz; i++) {
386 if (outmode == OUTMODE_FLN)
387 puts(res[i].file);
388 else if (outmode == OUTMODE_LST)
389 printf("%s - %s\n", res[i].names,
390 res[i].output == NULL ? "" :
391 res[i].output);
392 else if (outmode == OUTMODE_ONE) {
393 /* Search for the best section. */
394 isec = strcspn(res[i].file, "123456789");
395 sec = res[i].file[isec];
396 if ('\0' == sec)
397 continue;
398 prio = sec_prios[sec - '1'];
399 if (prio >= best_prio)
400 continue;
401 best_prio = prio;
402 resp = res + i;
403 }
404 }
405
406 /*
407 * For man(1), -a and -i output mode, fall through
408 * to the main mandoc(1) code iterating files
409 * and running the parsers on each of them.
410 */
411
412 if (outmode == OUTMODE_FLN || outmode == OUTMODE_LST)
413 goto out;
414 }
415
416 /* mandoc(1) */
417
418 if (search.argmode == ARG_FILE && ! moptions(&options, auxpaths))
419 return((int)MANDOCLEVEL_BADARG);
420
421 curp.mchars = mchars_alloc();
422 curp.mp = mparse_alloc(options, curp.wlevel, mmsg,
423 curp.mchars, defos);
424
425 /*
426 * Conditionally start up the lookaside buffer before parsing.
427 */
428 if (OUTT_MAN == curp.outtype)
429 mparse_keep(curp.mp);
430
431 if (argc < 1) {
432 if (pager_pid == 1 && isatty(STDOUT_FILENO))
433 pager_pid = spawn_pager();
434 parse(&curp, STDIN_FILENO, "<stdin>");
435 }
436
437 while (argc > 0) {
438 rctmp = mparse_open(curp.mp, &fd,
439 resp != NULL ? resp->file : *argv);
440 if (rc < rctmp)
441 rc = rctmp;
442
443 if (fd != -1) {
444 if (pager_pid == 1 && isatty(STDOUT_FILENO))
445 pager_pid = spawn_pager();
446
447 if (resp == NULL)
448 parse(&curp, fd, *argv);
449 else if (resp->form & FORM_SRC) {
450 /* For .so only; ignore failure. */
451 chdir(conf.manpath.paths[resp->ipath]);
452 parse(&curp, fd, resp->file);
453 } else
454 passthrough(resp->file, fd,
455 conf.output.synopsisonly);
456
457 rctmp = mparse_wait(curp.mp);
458 if (rc < rctmp)
459 rc = rctmp;
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 waitpid(pager_pid, NULL, 0);
501 }
502
503 return((int)rc);
504 }
505
506 static void
507 usage(enum argmode argmode)
508 {
509
510 switch (argmode) {
511 case ARG_FILE:
512 fputs("usage: mandoc [-acfhkl] [-I os=name] "
513 "[-K encoding] [-mformat] [-O option]\n"
514 "\t [-T output] [-W level] [file ...]\n", stderr);
515 break;
516 case ARG_NAME:
517 fputs("usage: man [-acfhklw] [-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 [-acfhklw] [-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 [-acfhklw] [-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 static int
538 fs_lookup(const struct manpaths *paths, size_t ipath,
539 const char *sec, const char *arch, const char *name,
540 struct manpage **res, size_t *ressz)
541 {
542 glob_t globinfo;
543 struct manpage *page;
544 char *file;
545 int form, globres;
546
547 form = FORM_SRC;
548 mandoc_asprintf(&file, "%s/man%s/%s.%s",
549 paths->paths[ipath], sec, name, sec);
550 if (access(file, R_OK) != -1)
551 goto found;
552 free(file);
553
554 mandoc_asprintf(&file, "%s/cat%s/%s.0",
555 paths->paths[ipath], sec, name);
556 if (access(file, R_OK) != -1) {
557 form = FORM_CAT;
558 goto found;
559 }
560 free(file);
561
562 if (arch != NULL) {
563 mandoc_asprintf(&file, "%s/man%s/%s/%s.%s",
564 paths->paths[ipath], sec, arch, name, sec);
565 if (access(file, R_OK) != -1)
566 goto found;
567 free(file);
568 }
569
570 mandoc_asprintf(&file, "%s/man%s/%s.*",
571 paths->paths[ipath], sec, name);
572 globres = glob(file, 0, NULL, &globinfo);
573 if (globres != 0 && globres != GLOB_NOMATCH)
574 fprintf(stderr, "%s: %s: glob: %s\n",
575 progname, file, strerror(errno));
576 free(file);
577 if (globres == 0)
578 file = mandoc_strdup(*globinfo.gl_pathv);
579 globfree(&globinfo);
580 if (globres != 0)
581 return(0);
582
583 found:
584 #if HAVE_SQLITE3
585 fprintf(stderr, "%s: outdated mandoc.db lacks %s(%s) entry, run "
586 "makewhatis %s\n", progname, name, sec, paths->paths[ipath]);
587 #endif
588 *res = mandoc_reallocarray(*res, ++*ressz, sizeof(struct manpage));
589 page = *res + (*ressz - 1);
590 page->file = file;
591 page->names = NULL;
592 page->output = NULL;
593 page->ipath = ipath;
594 page->bits = NAME_FILE & NAME_MASK;
595 page->sec = (*sec >= '1' && *sec <= '9') ? *sec - '1' + 1 : 10;
596 page->form = form;
597 return(1);
598 }
599
600 static void
601 fs_search(const struct mansearch *cfg, const struct manpaths *paths,
602 int argc, char **argv, struct manpage **res, size_t *ressz)
603 {
604 const char *const sections[] =
605 {"1", "8", "6", "2", "3", "3p", "5", "7", "4", "9"};
606 const size_t nsec = sizeof(sections)/sizeof(sections[0]);
607
608 size_t ipath, isec, lastsz;
609
610 assert(cfg->argmode == ARG_NAME);
611
612 *res = NULL;
613 *ressz = lastsz = 0;
614 while (argc) {
615 for (ipath = 0; ipath < paths->sz; ipath++) {
616 if (cfg->sec != NULL) {
617 if (fs_lookup(paths, ipath, cfg->sec,
618 cfg->arch, *argv, res, ressz) &&
619 cfg->firstmatch)
620 return;
621 } else for (isec = 0; isec < nsec; isec++)
622 if (fs_lookup(paths, ipath, sections[isec],
623 cfg->arch, *argv, res, ressz) &&
624 cfg->firstmatch)
625 return;
626 }
627 if (*ressz == lastsz)
628 fprintf(stderr,
629 "%s: No entry for %s in the manual.\n",
630 progname, *argv);
631 lastsz = *ressz;
632 argv++;
633 argc--;
634 }
635 }
636
637 static void
638 parse(struct curparse *curp, int fd, const char *file)
639 {
640 enum mandoclevel rctmp;
641 struct roff_man *man;
642
643 /* Begin by parsing the file itself. */
644
645 assert(file);
646 assert(fd >= -1);
647
648 rctmp = mparse_readfd(curp->mp, fd, file);
649 if (rc < rctmp)
650 rc = rctmp;
651
652 /*
653 * With -Wstop and warnings or errors of at least the requested
654 * level, do not produce output.
655 */
656
657 if (rctmp != MANDOCLEVEL_OK && curp->wstop)
658 return;
659
660 /* If unset, allocate output dev now (if applicable). */
661
662 if ( ! (curp->outman && curp->outmdoc)) {
663 switch (curp->outtype) {
664 case OUTT_HTML:
665 curp->outdata = html_alloc(curp->mchars,
666 curp->outopts);
667 curp->outfree = html_free;
668 break;
669 case OUTT_UTF8:
670 curp->outdata = utf8_alloc(curp->mchars,
671 curp->outopts);
672 curp->outfree = ascii_free;
673 break;
674 case OUTT_LOCALE:
675 curp->outdata = locale_alloc(curp->mchars,
676 curp->outopts);
677 curp->outfree = ascii_free;
678 break;
679 case OUTT_ASCII:
680 curp->outdata = ascii_alloc(curp->mchars,
681 curp->outopts);
682 curp->outfree = ascii_free;
683 break;
684 case OUTT_PDF:
685 curp->outdata = pdf_alloc(curp->mchars,
686 curp->outopts);
687 curp->outfree = pspdf_free;
688 break;
689 case OUTT_PS:
690 curp->outdata = ps_alloc(curp->mchars,
691 curp->outopts);
692 curp->outfree = pspdf_free;
693 break;
694 default:
695 break;
696 }
697
698 switch (curp->outtype) {
699 case OUTT_HTML:
700 curp->outman = html_man;
701 curp->outmdoc = html_mdoc;
702 break;
703 case OUTT_TREE:
704 curp->outman = tree_man;
705 curp->outmdoc = tree_mdoc;
706 break;
707 case OUTT_MAN:
708 curp->outmdoc = man_mdoc;
709 curp->outman = man_man;
710 break;
711 case OUTT_PDF:
712 /* FALLTHROUGH */
713 case OUTT_ASCII:
714 /* FALLTHROUGH */
715 case OUTT_UTF8:
716 /* FALLTHROUGH */
717 case OUTT_LOCALE:
718 /* FALLTHROUGH */
719 case OUTT_PS:
720 curp->outman = terminal_man;
721 curp->outmdoc = terminal_mdoc;
722 break;
723 default:
724 break;
725 }
726 }
727
728 mparse_result(curp->mp, &man, NULL);
729
730 /* Execute the out device, if it exists. */
731
732 if (man == NULL)
733 return;
734 if (curp->outmdoc != NULL && man->macroset == MACROSET_MDOC)
735 (*curp->outmdoc)(curp->outdata, man);
736 if (curp->outman != NULL && man->macroset == MACROSET_MAN)
737 (*curp->outman)(curp->outdata, man);
738 }
739
740 static void
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;
799
800 fail:
801 fprintf(stderr, "%s: %s: SYSERR: %s: %s",
802 progname, file, syscall, strerror(errno));
803 if (rc < MANDOCLEVEL_SYSERR)
804 rc = MANDOCLEVEL_SYSERR;
805 }
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[7];
887
888 toks[0] = "stop";
889 toks[1] = "all";
890 toks[2] = "warning";
891 toks[3] = "error";
892 toks[4] = "unsupp";
893 toks[5] = "fatal";
894 toks[6] = NULL;
895
896 while (*arg) {
897 o = arg;
898 switch (getsubopt(&arg, UNCONST(toks), &v)) {
899 case 0:
900 curp->wstop = 1;
901 break;
902 case 1:
903 /* FALLTHROUGH */
904 case 2:
905 curp->wlevel = MANDOCLEVEL_WARNING;
906 break;
907 case 3:
908 curp->wlevel = MANDOCLEVEL_ERROR;
909 break;
910 case 4:
911 curp->wlevel = MANDOCLEVEL_UNSUPP;
912 break;
913 case 5:
914 curp->wlevel = MANDOCLEVEL_BADARG;
915 break;
916 default:
917 fprintf(stderr, "%s: -W %s: Bad argument\n",
918 progname, o);
919 return(0);
920 }
921 }
922
923 return(1);
924 }
925
926 static void
927 mmsg(enum mandocerr t, enum mandoclevel lvl,
928 const char *file, int line, int col, const char *msg)
929 {
930 const char *mparse_msg;
931
932 fprintf(stderr, "%s: %s:", progname, file);
933
934 if (line)
935 fprintf(stderr, "%d:%d:", line, col + 1);
936
937 fprintf(stderr, " %s", mparse_strlevel(lvl));
938
939 if (NULL != (mparse_msg = mparse_strerror(t)))
940 fprintf(stderr, ": %s", mparse_msg);
941
942 if (msg)
943 fprintf(stderr, ": %s", msg);
944
945 fputc('\n', stderr);
946 }
947
948 static void
949 handle_sigpipe(int signum)
950 {
951
952 exit((int)rc);
953 }
954
955 static pid_t
956 spawn_pager(void)
957 {
958 #define MAX_PAGER_ARGS 16
959 char *argv[MAX_PAGER_ARGS];
960 const char *pager;
961 char *cp;
962 int fildes[2];
963 int argc;
964 pid_t pager_pid;
965
966 if (pipe(fildes) == -1) {
967 fprintf(stderr, "%s: pipe: %s\n",
968 progname, strerror(errno));
969 return(0);
970 }
971
972 switch (pager_pid = fork()) {
973 case -1:
974 fprintf(stderr, "%s: fork: %s\n",
975 progname, strerror(errno));
976 exit((int)MANDOCLEVEL_SYSERR);
977 case 0:
978 break;
979 default:
980 close(fildes[0]);
981 if (dup2(fildes[1], STDOUT_FILENO) == -1) {
982 fprintf(stderr, "%s: dup output: %s\n",
983 progname, strerror(errno));
984 exit((int)MANDOCLEVEL_SYSERR);
985 }
986 close(fildes[1]);
987 signal(SIGPIPE, handle_sigpipe);
988 return(pager_pid);
989 }
990
991 /* The child process becomes the pager. */
992
993 close(fildes[1]);
994 if (dup2(fildes[0], STDIN_FILENO) == -1) {
995 fprintf(stderr, "%s: dup input: %s\n",
996 progname, strerror(errno));
997 exit((int)MANDOCLEVEL_SYSERR);
998 }
999 close(fildes[0]);
1000
1001 pager = getenv("MANPAGER");
1002 if (pager == NULL || *pager == '\0')
1003 pager = getenv("PAGER");
1004 if (pager == NULL || *pager == '\0')
1005 pager = "more -s";
1006 cp = mandoc_strdup(pager);
1007
1008 /*
1009 * Parse the pager command into words.
1010 * Intentionally do not do anything fancy here.
1011 */
1012
1013 argc = 0;
1014 while (argc + 1 < MAX_PAGER_ARGS) {
1015 argv[argc++] = cp;
1016 cp = strchr(cp, ' ');
1017 if (cp == NULL)
1018 break;
1019 *cp++ = '\0';
1020 while (*cp == ' ')
1021 cp++;
1022 if (*cp == '\0')
1023 break;
1024 }
1025 argv[argc] = NULL;
1026
1027 /* Hand over to the pager. */
1028
1029 execvp(argv[0], argv);
1030 fprintf(stderr, "%s: exec %s: %s\n",
1031 progname, argv[0], strerror(errno));
1032 exit((int)MANDOCLEVEL_SYSERR);
1033 }