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