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