1 /* $Id: main.c,v 1.225 2015/03/10 13:50:03 schwarze Exp $ */
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>
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.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/types.h>
22 #include <sys/param.h> /* MACHINE */
37 #include "mandoc_aux.h"
42 #include "mansearch.h"
44 #if !defined(__GNUC__) || (__GNUC__ < 2)
46 # define __attribute__(x)
48 #endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
59 typedef void (*out_mdoc)(void *, const struct mdoc *);
60 typedef void (*out_man)(void *, const struct man *);
61 typedef void (*out_free)(void *);
64 OUTT_ASCII = 0, /* -Tascii */
65 OUTT_LOCALE, /* -Tlocale */
66 OUTT_UTF8, /* -Tutf8 */
67 OUTT_TREE, /* -Ttree */
69 OUTT_HTML, /* -Thtml */
70 OUTT_LINT, /* -Tlint */
77 struct mchars *mchars; /* character table */
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 out_mdoc outmdoc; /* mdoc output ptr */
82 out_man outman; /* man output ptr */
83 out_free outfree; /* free output ptr */
84 void *outdata; /* data for output */
85 char outopts[BUFSIZ]; /* buf of output opts */
88 static int fs_lookup(const struct manpaths *,
89 size_t ipath, const char *,
90 const char *, const char *,
91 struct manpage **, size_t *);
92 static void fs_search(const struct mansearch *,
93 const struct manpaths *, int, char**,
94 struct manpage **, size_t *);
95 static int koptions(int *, char *);
97 int mandocdb(int, char**);
99 static int moptions(int *, char *);
100 static void mmsg(enum mandocerr, enum mandoclevel,
101 const char *, int, int, const char *);
102 static void parse(struct curparse *, int,
103 const char *, enum mandoclevel *);
104 static enum mandoclevel passthrough(const char *, int, int);
105 static pid_t spawn_pager(void);
106 static int toptions(struct curparse *, char *);
107 static void usage(enum argmode) __attribute__((noreturn));
108 static int woptions(struct curparse *, char *);
110 static const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
111 static char help_arg[] = "help";
112 static char *help_argv[] = {help_arg, NULL};
113 static const char *progname;
117 main(int argc, char *argv[])
119 struct curparse curp;
120 struct mansearch search;
121 struct manpaths paths;
125 struct manpage *res, *resp;
126 char *conf_file, *defpaths;
128 int prio, best_prio, synopsis_only;
130 enum mandoclevel rc, rctmp;
131 enum outmode outmode;
136 pid_t pager_pid; /* 0: don't use; 1: not yet spawned. */
140 else if ((progname = strrchr(argv[0], '/')) == NULL)
146 if (strcmp(progname, BINM_MAKEWHATIS) == 0)
147 return(mandocdb(argc, argv));
150 /* Search options. */
152 memset(&paths, 0, sizeof(struct manpaths));
153 conf_file = defpaths = NULL;
156 memset(&search, 0, sizeof(struct mansearch));
157 search.outkey = "Nd";
159 if (strcmp(progname, BINM_MAN) == 0)
160 search.argmode = ARG_NAME;
161 else if (strcmp(progname, BINM_APROPOS) == 0)
162 search.argmode = ARG_EXPR;
163 else if (strcmp(progname, BINM_WHATIS) == 0)
164 search.argmode = ARG_WORD;
165 else if (strncmp(progname, "help", 4) == 0)
166 search.argmode = ARG_NAME;
168 search.argmode = ARG_FILE;
170 /* Parser and formatter options. */
172 memset(&curp, 0, sizeof(struct curparse));
173 curp.outtype = OUTT_LOCALE;
174 curp.wlevel = MANDOCLEVEL_BADARG;
175 options = MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1;
181 outmode = OUTMODE_DEF;
183 while (-1 != (c = getopt(argc, argv,
184 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w"))) {
187 outmode = OUTMODE_ALL;
196 search.argmode = ARG_WORD;
199 (void)strlcat(curp.outopts, "synopsis,", BUFSIZ);
202 outmode = OUTMODE_ALL;
205 if (strncmp(optarg, "os=", 3)) {
207 "%s: -I %s: Bad argument\n",
209 return((int)MANDOCLEVEL_BADARG);
213 "%s: -I %s: Duplicate argument\n",
215 return((int)MANDOCLEVEL_BADARG);
217 defos = mandoc_strdup(optarg + 3);
220 outmode = OUTMODE_INT;
223 if ( ! koptions(&options, optarg))
224 return((int)MANDOCLEVEL_BADARG);
227 search.argmode = ARG_EXPR;
230 search.argmode = ARG_FILE;
231 outmode = OUTMODE_ALL;
240 search.outkey = optarg;
241 (void)strlcat(curp.outopts, optarg, BUFSIZ);
242 (void)strlcat(curp.outopts, ",", BUFSIZ);
245 search.arch = optarg;
251 if ( ! toptions(&curp, optarg))
252 return((int)MANDOCLEVEL_BADARG);
255 if ( ! woptions(&curp, optarg))
256 return((int)MANDOCLEVEL_BADARG);
259 outmode = OUTMODE_FLN;
268 usage(search.argmode);
270 /* Postprocess options. */
272 if (outmode == OUTMODE_DEF) {
273 switch (search.argmode) {
275 outmode = OUTMODE_ALL;
279 outmode = OUTMODE_ONE;
282 outmode = OUTMODE_LST;
287 /* Parse arguments. */
297 * and for a man(1) section argument without -s.
300 if (search.argmode == ARG_NAME) {
301 if (*progname == 'h') {
306 } else if (argc > 1 &&
307 ((uc = (unsigned char *)argv[0]) != NULL) &&
308 ((isdigit(uc[0]) && (uc[1] == '\0' ||
309 (isalpha(uc[1]) && uc[2] == '\0'))) ||
310 (uc[0] == 'n' && uc[1] == '\0'))) {
311 search.sec = (char *)uc;
315 if (search.arch == NULL)
316 search.arch = getenv("MACHINE");
318 if (search.arch == NULL)
319 search.arch = MACHINE;
325 /* man(1), whatis(1), apropos(1) */
327 if (search.argmode != ARG_FILE) {
329 usage(search.argmode);
331 if (search.argmode == ARG_NAME &&
332 outmode == OUTMODE_ONE)
333 search.firstmatch = 1;
335 /* Access the mandoc database. */
337 manpath_parse(&paths, conf_file, defpaths, auxpaths);
340 if( ! mansearch(&search, &paths, argc, argv, &res, &sz))
341 usage(search.argmode);
343 if (search.argmode != ARG_NAME) {
344 fputs("mandoc: database support not compiled in\n",
346 return((int)MANDOCLEVEL_BADARG);
351 if (sz == 0 && search.argmode == ARG_NAME)
352 fs_search(&search, &paths, argc, argv, &res, &sz);
355 rc = MANDOCLEVEL_BADARG;
360 * For standard man(1) and -a output mode,
361 * prepare for copying filename pointers
362 * into the program parameter array.
365 if (outmode == OUTMODE_ONE) {
368 } else if (outmode == OUTMODE_ALL)
371 /* Iterate all matching manuals. */
374 for (i = 0; i < sz; i++) {
375 if (outmode == OUTMODE_FLN)
377 else if (outmode == OUTMODE_LST)
378 printf("%s - %s\n", res[i].names,
379 res[i].output == NULL ? "" :
381 else if (outmode == OUTMODE_ONE) {
382 /* Search for the best section. */
383 isec = strcspn(res[i].file, "123456789");
384 sec = res[i].file[isec];
387 prio = sec_prios[sec - '1'];
388 if (prio >= best_prio)
396 * For man(1), -a and -i output mode, fall through
397 * to the main mandoc(1) code iterating files
398 * and running the parsers on each of them.
401 if (outmode == OUTMODE_FLN || outmode == OUTMODE_LST)
407 if (search.argmode == ARG_FILE && ! moptions(&options, auxpaths))
408 return((int)MANDOCLEVEL_BADARG);
410 curp.mchars = mchars_alloc();
411 curp.mp = mparse_alloc(options, curp.wlevel, mmsg,
415 * Conditionally start up the lookaside buffer before parsing.
417 if (OUTT_MAN == curp.outtype)
418 mparse_keep(curp.mp);
421 if (pager_pid == 1 && isatty(STDOUT_FILENO))
422 pager_pid = spawn_pager();
423 parse(&curp, STDIN_FILENO, "<stdin>", &rc);
427 rctmp = mparse_open(curp.mp, &fd,
428 resp != NULL ? resp->file : *argv);
433 if (pager_pid == 1 && isatty(STDOUT_FILENO))
434 pager_pid = spawn_pager();
437 parse(&curp, fd, *argv, &rc);
438 else if (resp->form & FORM_SRC) {
439 /* For .so only; ignore failure. */
440 chdir(paths.paths[resp->ipath]);
441 parse(&curp, fd, resp->file, &rc);
443 rctmp = passthrough(resp->file, fd,
449 rctmp = mparse_wait(curp.mp);
453 if (argc > 1 && curp.outtype <= OUTT_UTF8)
454 ascii_sepline(curp.outdata);
457 if (MANDOCLEVEL_OK != rc && curp.wstop)
465 mparse_reset(curp.mp);
469 (*curp.outfree)(curp.outdata);
470 mparse_free(curp.mp);
471 mchars_free(curp.mchars);
474 if (search.argmode != ARG_FILE) {
475 manpath_free(&paths);
477 mansearch_free(res, sz);
485 * If a pager is attached, flush the pipe leading to it
486 * and signal end of file such that the user can browse
487 * to the end. Then wait for the user to close the pager.
490 if (pager_pid != 0 && pager_pid != 1) {
492 waitpid(pager_pid, NULL, 0);
499 usage(enum argmode argmode)
504 fputs("usage: mandoc [-acfhkl] [-Ios=name] "
505 "[-Kencoding] [-mformat] [-Ooption]\n"
506 "\t [-Toutput] [-Wlevel] [file ...]\n", stderr);
509 fputs("usage: man [-acfhklw] [-C file] [-I os=name] "
510 "[-K encoding] [-M path] [-m path]\n"
511 "\t [-O option=value] [-S subsection] [-s section] "
512 "[-T output] [-W level]\n"
513 "\t [section] name ...\n", stderr);
516 fputs("usage: whatis [-acfhklw] [-C file] "
517 "[-M path] [-m path] [-O outkey] [-S arch]\n"
518 "\t [-s section] name ...\n", stderr);
521 fputs("usage: apropos [-acfhklw] [-C file] "
522 "[-M path] [-m path] [-O outkey] [-S arch]\n"
523 "\t [-s section] expression ...\n", stderr);
526 exit((int)MANDOCLEVEL_BADARG);
530 fs_lookup(const struct manpaths *paths, size_t ipath,
531 const char *sec, const char *arch, const char *name,
532 struct manpage **res, size_t *ressz)
535 struct manpage *page;
540 mandoc_asprintf(&file, "%s/man%s/%s.%s",
541 paths->paths[ipath], sec, name, sec);
542 if (access(file, R_OK) != -1)
546 mandoc_asprintf(&file, "%s/cat%s/%s.0",
547 paths->paths[ipath], sec, name);
548 if (access(file, R_OK) != -1) {
555 mandoc_asprintf(&file, "%s/man%s/%s/%s.%s",
556 paths->paths[ipath], sec, arch, name, sec);
557 if (access(file, R_OK) != -1)
562 mandoc_asprintf(&file, "%s/man%s/%s.*",
563 paths->paths[ipath], sec, name);
564 globres = glob(file, 0, NULL, &globinfo);
565 if (globres != 0 && globres != GLOB_NOMATCH)
566 fprintf(stderr, "%s: %s: glob: %s\n",
567 progname, file, strerror(errno));
570 file = mandoc_strdup(*globinfo.gl_pathv);
577 fprintf(stderr, "%s: outdated mandoc.db lacks %s(%s) entry,\n"
578 " consider running # makewhatis %s\n",
579 progname, name, sec, paths->paths[ipath]);
582 *res = mandoc_reallocarray(*res, ++*ressz, sizeof(struct manpage));
583 page = *res + (*ressz - 1);
588 page->bits = NAME_FILE & NAME_MASK;
589 page->sec = (*sec >= '1' && *sec <= '9') ? *sec - '1' + 1 : 10;
595 fs_search(const struct mansearch *cfg, const struct manpaths *paths,
596 int argc, char **argv, struct manpage **res, size_t *ressz)
598 const char *const sections[] =
599 {"1", "8", "6", "2", "3", "3p", "5", "7", "4", "9"};
600 const size_t nsec = sizeof(sections)/sizeof(sections[0]);
602 size_t ipath, isec, lastsz;
604 assert(cfg->argmode == ARG_NAME);
609 for (ipath = 0; ipath < paths->sz; ipath++) {
610 if (cfg->sec != NULL) {
611 if (fs_lookup(paths, ipath, cfg->sec,
612 cfg->arch, *argv, res, ressz) &&
615 } else for (isec = 0; isec < nsec; isec++)
616 if (fs_lookup(paths, ipath, sections[isec],
617 cfg->arch, *argv, res, ressz) &&
621 if (*ressz == lastsz)
623 "%s: No entry for %s in the manual.\n",
632 parse(struct curparse *curp, int fd, const char *file,
633 enum mandoclevel *level)
639 /* Begin by parsing the file itself. */
644 rc = mparse_readfd(curp->mp, fd, file);
647 * With -Wstop and warnings or errors of at least the requested
648 * level, do not produce output.
651 if (MANDOCLEVEL_OK != rc && curp->wstop)
654 /* If unset, allocate output dev now (if applicable). */
656 if ( ! (curp->outman && curp->outmdoc)) {
657 switch (curp->outtype) {
659 curp->outdata = html_alloc(curp->mchars,
661 curp->outfree = html_free;
664 curp->outdata = utf8_alloc(curp->mchars,
666 curp->outfree = ascii_free;
669 curp->outdata = locale_alloc(curp->mchars,
671 curp->outfree = ascii_free;
674 curp->outdata = ascii_alloc(curp->mchars,
676 curp->outfree = ascii_free;
679 curp->outdata = pdf_alloc(curp->mchars,
681 curp->outfree = pspdf_free;
684 curp->outdata = ps_alloc(curp->mchars,
686 curp->outfree = pspdf_free;
692 switch (curp->outtype) {
694 curp->outman = html_man;
695 curp->outmdoc = html_mdoc;
698 curp->outman = tree_man;
699 curp->outmdoc = tree_mdoc;
702 curp->outmdoc = man_mdoc;
703 curp->outman = man_man;
714 curp->outman = terminal_man;
715 curp->outmdoc = terminal_mdoc;
722 mparse_result(curp->mp, &mdoc, &man, NULL);
724 /* Execute the out device, if it exists. */
726 if (man && curp->outman)
727 (*curp->outman)(curp->outdata, man);
728 if (mdoc && curp->outmdoc)
729 (*curp->outmdoc)(curp->outdata, mdoc);
736 static enum mandoclevel
737 passthrough(const char *file, int fd, int synopsis_only)
739 const char synb[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
740 const char synr[] = "SYNOPSIS";
751 if ((stream = fdopen(fd, "r")) == NULL) {
758 while ((line = fgetln(stream, &len)) != NULL) {
761 if ( ! isspace((unsigned char)*line))
764 isspace((unsigned char)*line)) {
769 if ((len == sizeof(synb) &&
770 ! strncmp(line, synb, len - 1)) ||
771 (len == sizeof(synr) &&
772 ! strncmp(line, synr, len - 1)))
777 for (off = 0; off < len; off += nw)
778 if ((nw = write(STDOUT_FILENO, line + off,
779 len - off)) == -1 || nw == 0) {
786 if (ferror(stream)) {
794 return(MANDOCLEVEL_OK);
797 fprintf(stderr, "%s: %s: SYSERR: %s: %s",
798 progname, file, syscall, strerror(errno));
799 return(MANDOCLEVEL_SYSERR);
803 koptions(int *options, char *arg)
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);
815 fprintf(stderr, "%s: -K %s: Bad argument\n",
823 moptions(int *options, char *arg)
828 else if (0 == strcmp(arg, "doc"))
829 *options |= MPARSE_MDOC;
830 else if (0 == strcmp(arg, "andoc"))
832 else if (0 == strcmp(arg, "an"))
833 *options |= MPARSE_MAN;
835 fprintf(stderr, "%s: -m %s: Bad argument\n",
844 toptions(struct curparse *curp, char *arg)
847 if (0 == strcmp(arg, "ascii"))
848 curp->outtype = OUTT_ASCII;
849 else if (0 == strcmp(arg, "lint")) {
850 curp->outtype = OUTT_LINT;
851 curp->wlevel = MANDOCLEVEL_WARNING;
852 } else if (0 == strcmp(arg, "tree"))
853 curp->outtype = OUTT_TREE;
854 else if (0 == strcmp(arg, "man"))
855 curp->outtype = OUTT_MAN;
856 else if (0 == strcmp(arg, "html"))
857 curp->outtype = OUTT_HTML;
858 else if (0 == strcmp(arg, "utf8"))
859 curp->outtype = OUTT_UTF8;
860 else if (0 == strcmp(arg, "locale"))
861 curp->outtype = OUTT_LOCALE;
862 else if (0 == strcmp(arg, "xhtml"))
863 curp->outtype = OUTT_HTML;
864 else if (0 == strcmp(arg, "ps"))
865 curp->outtype = OUTT_PS;
866 else if (0 == strcmp(arg, "pdf"))
867 curp->outtype = OUTT_PDF;
869 fprintf(stderr, "%s: -T %s: Bad argument\n",
878 woptions(struct curparse *curp, char *arg)
893 switch (getsubopt(&arg, UNCONST(toks), &v)) {
900 curp->wlevel = MANDOCLEVEL_WARNING;
903 curp->wlevel = MANDOCLEVEL_ERROR;
906 curp->wlevel = MANDOCLEVEL_UNSUPP;
909 curp->wlevel = MANDOCLEVEL_BADARG;
912 fprintf(stderr, "%s: -W %s: Bad argument\n",
922 mmsg(enum mandocerr t, enum mandoclevel lvl,
923 const char *file, int line, int col, const char *msg)
925 const char *mparse_msg;
927 fprintf(stderr, "%s: %s:", progname, file);
930 fprintf(stderr, "%d:%d:", line, col + 1);
932 fprintf(stderr, " %s", mparse_strlevel(lvl));
934 if (NULL != (mparse_msg = mparse_strerror(t)))
935 fprintf(stderr, ": %s", mparse_msg);
938 fprintf(stderr, ": %s", msg);
946 #define MAX_PAGER_ARGS 16
947 char *argv[MAX_PAGER_ARGS];
954 if (pipe(fildes) == -1) {
955 fprintf(stderr, "%s: pipe: %s\n",
956 progname, strerror(errno));
960 switch (pager_pid = fork()) {
962 fprintf(stderr, "%s: fork: %s\n",
963 progname, strerror(errno));
964 exit((int)MANDOCLEVEL_SYSERR);
969 if (dup2(fildes[1], STDOUT_FILENO) == -1) {
970 fprintf(stderr, "%s: dup output: %s\n",
971 progname, strerror(errno));
972 exit((int)MANDOCLEVEL_SYSERR);
978 /* The child process becomes the pager. */
981 if (dup2(fildes[0], STDIN_FILENO) == -1) {
982 fprintf(stderr, "%s: dup input: %s\n",
983 progname, strerror(errno));
984 exit((int)MANDOCLEVEL_SYSERR);
988 pager = getenv("MANPAGER");
989 if (pager == NULL || *pager == '\0')
990 pager = getenv("PAGER");
991 if (pager == NULL || *pager == '\0')
992 pager = "/usr/bin/more -s";
993 cp = mandoc_strdup(pager);
996 * Parse the pager command into words.
997 * Intentionally do not do anything fancy here.
1001 while (argc + 1 < MAX_PAGER_ARGS) {
1003 cp = strchr(cp, ' ');
1014 /* Hand over to the pager. */
1016 execvp(argv[0], argv);
1017 fprintf(stderr, "%s: exec: %s\n",
1018 progname, strerror(errno));
1019 exit((int)MANDOCLEVEL_SYSERR);