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