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