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