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