]> git.cameronkatri.com Git - mandoc.git/blob - main.c
6f15e38fb1b83d7a4dca3be5d2558b1fed0e4bbc
[mandoc.git] / main.c
1 /* $Id: main.c,v 1.231 2015/04/02 21:36:49 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 mdoc *);
62 typedef void (*out_man)(void *, const struct 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 && search.argmode == ARG_NAME)
356 fs_search(&search, &conf.manpath,
357 argc, argv, &res, &sz);
358
359 if (sz == 0) {
360 rc = MANDOCLEVEL_BADARG;
361 goto out;
362 }
363
364 /*
365 * For standard man(1) and -a output mode,
366 * prepare for copying filename pointers
367 * into the program parameter array.
368 */
369
370 if (outmode == OUTMODE_ONE) {
371 argc = 1;
372 best_prio = 10;
373 } else if (outmode == OUTMODE_ALL)
374 argc = (int)sz;
375
376 /* Iterate all matching manuals. */
377
378 resp = res;
379 for (i = 0; i < sz; i++) {
380 if (outmode == OUTMODE_FLN)
381 puts(res[i].file);
382 else if (outmode == OUTMODE_LST)
383 printf("%s - %s\n", res[i].names,
384 res[i].output == NULL ? "" :
385 res[i].output);
386 else if (outmode == OUTMODE_ONE) {
387 /* Search for the best section. */
388 isec = strcspn(res[i].file, "123456789");
389 sec = res[i].file[isec];
390 if ('\0' == sec)
391 continue;
392 prio = sec_prios[sec - '1'];
393 if (prio >= best_prio)
394 continue;
395 best_prio = prio;
396 resp = res + i;
397 }
398 }
399
400 /*
401 * For man(1), -a and -i output mode, fall through
402 * to the main mandoc(1) code iterating files
403 * and running the parsers on each of them.
404 */
405
406 if (outmode == OUTMODE_FLN || outmode == OUTMODE_LST)
407 goto out;
408 }
409
410 /* mandoc(1) */
411
412 if (search.argmode == ARG_FILE && ! moptions(&options, auxpaths))
413 return((int)MANDOCLEVEL_BADARG);
414
415 curp.mchars = mchars_alloc();
416 curp.mp = mparse_alloc(options, curp.wlevel, mmsg,
417 curp.mchars, defos);
418
419 /*
420 * Conditionally start up the lookaside buffer before parsing.
421 */
422 if (OUTT_MAN == curp.outtype)
423 mparse_keep(curp.mp);
424
425 if (argc < 1) {
426 if (pager_pid == 1 && isatty(STDOUT_FILENO))
427 pager_pid = spawn_pager();
428 parse(&curp, STDIN_FILENO, "<stdin>");
429 }
430
431 while (argc > 0) {
432 rctmp = mparse_open(curp.mp, &fd,
433 resp != NULL ? resp->file : *argv);
434 if (rc < rctmp)
435 rc = rctmp;
436
437 if (fd != -1) {
438 if (pager_pid == 1 && isatty(STDOUT_FILENO))
439 pager_pid = spawn_pager();
440
441 if (resp == NULL)
442 parse(&curp, fd, *argv);
443 else if (resp->form & FORM_SRC) {
444 /* For .so only; ignore failure. */
445 chdir(conf.manpath.paths[resp->ipath]);
446 parse(&curp, fd, resp->file);
447 } else
448 passthrough(resp->file, fd,
449 conf.output.synopsisonly);
450
451 rctmp = mparse_wait(curp.mp);
452 if (rc < rctmp)
453 rc = rctmp;
454
455 if (argc > 1 && curp.outtype <= OUTT_UTF8)
456 ascii_sepline(curp.outdata);
457 }
458
459 if (MANDOCLEVEL_OK != rc && curp.wstop)
460 break;
461
462 if (resp != NULL)
463 resp++;
464 else
465 argv++;
466 if (--argc)
467 mparse_reset(curp.mp);
468 }
469
470 if (curp.outfree)
471 (*curp.outfree)(curp.outdata);
472 mparse_free(curp.mp);
473 mchars_free(curp.mchars);
474
475 out:
476 if (search.argmode != ARG_FILE) {
477 manconf_free(&conf);
478 #if HAVE_SQLITE3
479 mansearch_free(res, sz);
480 mansearch_setup(0);
481 #endif
482 }
483
484 free(defos);
485
486 /*
487 * If a pager is attached, flush the pipe leading to it
488 * and signal end of file such that the user can browse
489 * to the end. Then wait for the user to close the pager.
490 */
491
492 if (pager_pid != 0 && pager_pid != 1) {
493 fclose(stdout);
494 waitpid(pager_pid, NULL, 0);
495 }
496
497 return((int)rc);
498 }
499
500 static void
501 usage(enum argmode argmode)
502 {
503
504 switch (argmode) {
505 case ARG_FILE:
506 fputs("usage: mandoc [-acfhkl] [-I os=name] "
507 "[-K encoding] [-mformat] [-O option]\n"
508 "\t [-T output] [-W level] [file ...]\n", stderr);
509 break;
510 case ARG_NAME:
511 fputs("usage: man [-acfhklw] [-C file] [-I os=name] "
512 "[-K encoding] [-M path] [-m path]\n"
513 "\t [-O option=value] [-S subsection] [-s section] "
514 "[-T output] [-W level]\n"
515 "\t [section] name ...\n", stderr);
516 break;
517 case ARG_WORD:
518 fputs("usage: whatis [-acfhklw] [-C file] "
519 "[-M path] [-m path] [-O outkey] [-S arch]\n"
520 "\t [-s section] name ...\n", stderr);
521 break;
522 case ARG_EXPR:
523 fputs("usage: apropos [-acfhklw] [-C file] "
524 "[-M path] [-m path] [-O outkey] [-S arch]\n"
525 "\t [-s section] expression ...\n", stderr);
526 break;
527 }
528 exit((int)MANDOCLEVEL_BADARG);
529 }
530
531 static int
532 fs_lookup(const struct manpaths *paths, size_t ipath,
533 const char *sec, const char *arch, const char *name,
534 struct manpage **res, size_t *ressz)
535 {
536 glob_t globinfo;
537 struct manpage *page;
538 char *file;
539 int form, globres;
540
541 form = FORM_SRC;
542 mandoc_asprintf(&file, "%s/man%s/%s.%s",
543 paths->paths[ipath], sec, name, sec);
544 if (access(file, R_OK) != -1)
545 goto found;
546 free(file);
547
548 mandoc_asprintf(&file, "%s/cat%s/%s.0",
549 paths->paths[ipath], sec, name);
550 if (access(file, R_OK) != -1) {
551 form = FORM_CAT;
552 goto found;
553 }
554 free(file);
555
556 if (arch != NULL) {
557 mandoc_asprintf(&file, "%s/man%s/%s/%s.%s",
558 paths->paths[ipath], sec, arch, name, sec);
559 if (access(file, R_OK) != -1)
560 goto found;
561 free(file);
562 }
563
564 mandoc_asprintf(&file, "%s/man%s/%s.*",
565 paths->paths[ipath], sec, name);
566 globres = glob(file, 0, NULL, &globinfo);
567 if (globres != 0 && globres != GLOB_NOMATCH)
568 fprintf(stderr, "%s: %s: glob: %s\n",
569 progname, file, strerror(errno));
570 free(file);
571 if (globres == 0)
572 file = mandoc_strdup(*globinfo.gl_pathv);
573 globfree(&globinfo);
574 if (globres != 0)
575 return(0);
576
577 found:
578 #if HAVE_SQLITE3
579 fprintf(stderr, "%s: outdated mandoc.db lacks %s(%s) entry,\n"
580 " consider running # makewhatis %s\n",
581 progname, name, sec, paths->paths[ipath]);
582 #endif
583
584 *res = mandoc_reallocarray(*res, ++*ressz, sizeof(struct manpage));
585 page = *res + (*ressz - 1);
586 page->file = file;
587 page->names = NULL;
588 page->output = NULL;
589 page->ipath = ipath;
590 page->bits = NAME_FILE & NAME_MASK;
591 page->sec = (*sec >= '1' && *sec <= '9') ? *sec - '1' + 1 : 10;
592 page->form = form;
593 return(1);
594 }
595
596 static void
597 fs_search(const struct mansearch *cfg, const struct manpaths *paths,
598 int argc, char **argv, struct manpage **res, size_t *ressz)
599 {
600 const char *const sections[] =
601 {"1", "8", "6", "2", "3", "3p", "5", "7", "4", "9"};
602 const size_t nsec = sizeof(sections)/sizeof(sections[0]);
603
604 size_t ipath, isec, lastsz;
605
606 assert(cfg->argmode == ARG_NAME);
607
608 *res = NULL;
609 *ressz = lastsz = 0;
610 while (argc) {
611 for (ipath = 0; ipath < paths->sz; ipath++) {
612 if (cfg->sec != NULL) {
613 if (fs_lookup(paths, ipath, cfg->sec,
614 cfg->arch, *argv, res, ressz) &&
615 cfg->firstmatch)
616 return;
617 } else for (isec = 0; isec < nsec; isec++)
618 if (fs_lookup(paths, ipath, sections[isec],
619 cfg->arch, *argv, res, ressz) &&
620 cfg->firstmatch)
621 return;
622 }
623 if (*ressz == lastsz)
624 fprintf(stderr,
625 "%s: No entry for %s in the manual.\n",
626 progname, *argv);
627 lastsz = *ressz;
628 argv++;
629 argc--;
630 }
631 }
632
633 static void
634 parse(struct curparse *curp, int fd, const char *file)
635 {
636 enum mandoclevel rctmp;
637 struct mdoc *mdoc;
638 struct man *man;
639
640 /* Begin by parsing the file itself. */
641
642 assert(file);
643 assert(fd >= -1);
644
645 rctmp = mparse_readfd(curp->mp, fd, file);
646 if (rc < rctmp)
647 rc = rctmp;
648
649 /*
650 * With -Wstop and warnings or errors of at least the requested
651 * level, do not produce output.
652 */
653
654 if (rctmp != MANDOCLEVEL_OK && curp->wstop)
655 return;
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
735 static void
736 passthrough(const char *file, int fd, int synopsis_only)
737 {
738 const char synb[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
739 const char synr[] = "SYNOPSIS";
740
741 FILE *stream;
742 const char *syscall;
743 char *line;
744 size_t len, off;
745 ssize_t nw;
746 int print;
747
748 fflush(stdout);
749
750 if ((stream = fdopen(fd, "r")) == NULL) {
751 close(fd);
752 syscall = "fdopen";
753 goto fail;
754 }
755
756 print = 0;
757 while ((line = fgetln(stream, &len)) != NULL) {
758 if (synopsis_only) {
759 if (print) {
760 if ( ! isspace((unsigned char)*line))
761 goto done;
762 while (len &&
763 isspace((unsigned char)*line)) {
764 line++;
765 len--;
766 }
767 } else {
768 if ((len == sizeof(synb) &&
769 ! strncmp(line, synb, len - 1)) ||
770 (len == sizeof(synr) &&
771 ! strncmp(line, synr, len - 1)))
772 print = 1;
773 continue;
774 }
775 }
776 for (off = 0; off < len; off += nw)
777 if ((nw = write(STDOUT_FILENO, line + off,
778 len - off)) == -1 || nw == 0) {
779 fclose(stream);
780 syscall = "write";
781 goto fail;
782 }
783 }
784
785 if (ferror(stream)) {
786 fclose(stream);
787 syscall = "fgetln";
788 goto fail;
789 }
790
791 done:
792 fclose(stream);
793 return;
794
795 fail:
796 fprintf(stderr, "%s: %s: SYSERR: %s: %s",
797 progname, file, syscall, strerror(errno));
798 if (rc < MANDOCLEVEL_SYSERR)
799 rc = MANDOCLEVEL_SYSERR;
800 }
801
802 static int
803 koptions(int *options, char *arg)
804 {
805
806 if ( ! strcmp(arg, "utf-8")) {
807 *options |= MPARSE_UTF8;
808 *options &= ~MPARSE_LATIN1;
809 } else if ( ! strcmp(arg, "iso-8859-1")) {
810 *options |= MPARSE_LATIN1;
811 *options &= ~MPARSE_UTF8;
812 } else if ( ! strcmp(arg, "us-ascii")) {
813 *options &= ~(MPARSE_UTF8 | MPARSE_LATIN1);
814 } else {
815 fprintf(stderr, "%s: -K %s: Bad argument\n",
816 progname, arg);
817 return(0);
818 }
819 return(1);
820 }
821
822 static int
823 moptions(int *options, char *arg)
824 {
825
826 if (arg == NULL)
827 /* nothing to do */;
828 else if (0 == strcmp(arg, "doc"))
829 *options |= MPARSE_MDOC;
830 else if (0 == strcmp(arg, "andoc"))
831 /* nothing to do */;
832 else if (0 == strcmp(arg, "an"))
833 *options |= MPARSE_MAN;
834 else {
835 fprintf(stderr, "%s: -m %s: Bad argument\n",
836 progname, arg);
837 return(0);
838 }
839
840 return(1);
841 }
842
843 static int
844 toptions(struct curparse *curp, char *arg)
845 {
846
847 if (0 == strcmp(arg, "ascii"))
848 curp->outtype = OUTT_ASCII;
849 else if (0 == strcmp(arg, "lint")) {
850 curp->outtype = OUTT_LINT;
851 curp->wlevel = MANDOCLEVEL_WARNING;
852 } else if (0 == strcmp(arg, "tree"))
853 curp->outtype = OUTT_TREE;
854 else if (0 == strcmp(arg, "man"))
855 curp->outtype = OUTT_MAN;
856 else if (0 == strcmp(arg, "html"))
857 curp->outtype = OUTT_HTML;
858 else if (0 == strcmp(arg, "utf8"))
859 curp->outtype = OUTT_UTF8;
860 else if (0 == strcmp(arg, "locale"))
861 curp->outtype = OUTT_LOCALE;
862 else if (0 == strcmp(arg, "xhtml"))
863 curp->outtype = OUTT_HTML;
864 else if (0 == strcmp(arg, "ps"))
865 curp->outtype = OUTT_PS;
866 else if (0 == strcmp(arg, "pdf"))
867 curp->outtype = OUTT_PDF;
868 else {
869 fprintf(stderr, "%s: -T %s: Bad argument\n",
870 progname, arg);
871 return(0);
872 }
873
874 return(1);
875 }
876
877 static int
878 woptions(struct curparse *curp, char *arg)
879 {
880 char *v, *o;
881 const char *toks[7];
882
883 toks[0] = "stop";
884 toks[1] = "all";
885 toks[2] = "warning";
886 toks[3] = "error";
887 toks[4] = "unsupp";
888 toks[5] = "fatal";
889 toks[6] = NULL;
890
891 while (*arg) {
892 o = arg;
893 switch (getsubopt(&arg, UNCONST(toks), &v)) {
894 case 0:
895 curp->wstop = 1;
896 break;
897 case 1:
898 /* FALLTHROUGH */
899 case 2:
900 curp->wlevel = MANDOCLEVEL_WARNING;
901 break;
902 case 3:
903 curp->wlevel = MANDOCLEVEL_ERROR;
904 break;
905 case 4:
906 curp->wlevel = MANDOCLEVEL_UNSUPP;
907 break;
908 case 5:
909 curp->wlevel = MANDOCLEVEL_BADARG;
910 break;
911 default:
912 fprintf(stderr, "%s: -W %s: Bad argument\n",
913 progname, o);
914 return(0);
915 }
916 }
917
918 return(1);
919 }
920
921 static void
922 mmsg(enum mandocerr t, enum mandoclevel lvl,
923 const char *file, int line, int col, const char *msg)
924 {
925 const char *mparse_msg;
926
927 fprintf(stderr, "%s: %s:", progname, file);
928
929 if (line)
930 fprintf(stderr, "%d:%d:", line, col + 1);
931
932 fprintf(stderr, " %s", mparse_strlevel(lvl));
933
934 if (NULL != (mparse_msg = mparse_strerror(t)))
935 fprintf(stderr, ": %s", mparse_msg);
936
937 if (msg)
938 fprintf(stderr, ": %s", msg);
939
940 fputc('\n', stderr);
941 }
942
943 static void
944 handle_sigpipe(int signum)
945 {
946
947 exit((int)rc);
948 }
949
950 static pid_t
951 spawn_pager(void)
952 {
953 #define MAX_PAGER_ARGS 16
954 char *argv[MAX_PAGER_ARGS];
955 const char *pager;
956 char *cp;
957 int fildes[2];
958 int argc;
959 pid_t pager_pid;
960
961 if (pipe(fildes) == -1) {
962 fprintf(stderr, "%s: pipe: %s\n",
963 progname, strerror(errno));
964 return(0);
965 }
966
967 switch (pager_pid = fork()) {
968 case -1:
969 fprintf(stderr, "%s: fork: %s\n",
970 progname, strerror(errno));
971 exit((int)MANDOCLEVEL_SYSERR);
972 case 0:
973 break;
974 default:
975 close(fildes[0]);
976 if (dup2(fildes[1], STDOUT_FILENO) == -1) {
977 fprintf(stderr, "%s: dup output: %s\n",
978 progname, strerror(errno));
979 exit((int)MANDOCLEVEL_SYSERR);
980 }
981 close(fildes[1]);
982 signal(SIGPIPE, handle_sigpipe);
983 return(pager_pid);
984 }
985
986 /* The child process becomes the pager. */
987
988 close(fildes[1]);
989 if (dup2(fildes[0], STDIN_FILENO) == -1) {
990 fprintf(stderr, "%s: dup input: %s\n",
991 progname, strerror(errno));
992 exit((int)MANDOCLEVEL_SYSERR);
993 }
994 close(fildes[0]);
995
996 pager = getenv("MANPAGER");
997 if (pager == NULL || *pager == '\0')
998 pager = getenv("PAGER");
999 if (pager == NULL || *pager == '\0')
1000 pager = "/usr/bin/more -s";
1001 cp = mandoc_strdup(pager);
1002
1003 /*
1004 * Parse the pager command into words.
1005 * Intentionally do not do anything fancy here.
1006 */
1007
1008 argc = 0;
1009 while (argc + 1 < MAX_PAGER_ARGS) {
1010 argv[argc++] = cp;
1011 cp = strchr(cp, ' ');
1012 if (cp == NULL)
1013 break;
1014 *cp++ = '\0';
1015 while (*cp == ' ')
1016 cp++;
1017 if (*cp == '\0')
1018 break;
1019 }
1020 argv[argc] = NULL;
1021
1022 /* Hand over to the pager. */
1023
1024 execvp(argv[0], argv);
1025 fprintf(stderr, "%s: exec: %s\n",
1026 progname, strerror(errno));
1027 exit((int)MANDOCLEVEL_SYSERR);
1028 }