main.c revision 371584c2eae4cf827fd406ba26c14f021adaaa70
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov/* $Id: main.c,v 1.269 2016/07/12 05:18:38 kristaps Exp $ */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov * Copyright (c) 2010-2012, 2014-2016 Ingo Schwarze <schwarze@openbsd.org>
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Copyright (c) 2010 Joerg Sonnenberger <joerg@netbsd.org>
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Permission to use, copy, modify, and distribute this software for any
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * purpose with or without fee is hereby granted, provided that the above
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * copyright notice and this permission notice appear in all copies.
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore# if !defined(lint)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore#endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore enum mandoclevel wlevel; /* ignore messages below this */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore int wstop; /* stop after a file with a warning */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov const char *, const char *,
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov const struct manpaths *, int, char**,
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovstatic int koptions(int *, char *);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovint mandocdb(int, char**);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovstatic int moptions(int *, char *);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorestatic void mmsg(enum mandocerr, enum mandoclevel,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore const char *, int, int, const char *);
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankovstatic void parse(struct curparse *, int, const char *);
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankovstatic void passthrough(const char *, int, int);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorestatic int toptions(struct curparse *, char *);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovstatic void usage(enum argmode) __attribute__((noreturn));
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorestatic int woptions(struct curparse *, char *);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovstatic const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov unsigned char *uc;
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov const char *sec;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov else if ((progname = strrchr(argv[0], '/')) == NULL)
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov if (pledge("stdio rpath tmppath tty proc exec flock", NULL) == -1)
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov if (sandbox_init(kSBXProfileNoInternet, SANDBOX_NAMED, NULL) == -1)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov /* Search options. */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov /* Parser and formatter options. */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov options = MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov "aC:cfhI:iK:klM:m:O:S:s:T:VW:w"))) {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov /* Postprocess options. */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov /* Parse arguments. */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Quirks for help(1)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * and for a man(1) section argument without -s.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov /* man(1), whatis(1), apropos(1) */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov /* Access the mandoc database. */
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov manconf_parse(&conf, conf_file, defpaths, auxpaths);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov fputs("mandoc: database support not compiled in\n",
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * For standard man(1) and -a output mode,
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * prepare for copying filename pointers
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * into the program parameter array.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov /* Iterate all matching manuals. */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov for (i = 0; i < sz; i++) {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov /* Search for the best section. */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * For man(1), -a and -i output mode, fall through
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * to the main mandoc(1) code iterating files
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * and running the parsers on each of them.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (outmode == OUTMODE_FLN || outmode == OUTMODE_LST)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov /* mandoc(1) */
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov if (pledge("stdio rpath tmppath tty proc exec", NULL) == -1)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (search.argmode == ARG_FILE && ! moptions(&options, auxpaths))
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov curp.mp = mparse_alloc(options, curp.wlevel, mmsg, defos);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Conditionally start up the lookaside buffer before parsing.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov while (argc > 0) {
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov fd = mparse_open(curp.mp, resp != NULL ? resp->file : *argv);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov /* For .so only; ignore failure. */
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov * When using a pager, finish writing both temporary files,
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov * fork it, wait for the user to close it, and clean up.
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov /* Stop here until moved to the foreground. */
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov /* Once in the foreground, activate the pager. */
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov /* Wait for the pager to stop or exit. */
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov return (int)rc;
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov "[-K encoding] [-mformat] [-O option]\n"
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov "\t [-T output] [-W level] [file ...]\n", stderr);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov fputs("usage: man [-acfhklw] [-C file] [-I os=name] "
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov "[-K encoding] [-M path] [-m path]\n"
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov "\t [-O option=value] [-S subsection] [-s section] "
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov "[-T output] [-W level]\n"
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov "[-M path] [-m path] [-O outkey] [-S arch]\n"
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov "[-M path] [-m path] [-O outkey] [-S arch]\n"
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovfs_lookup(const struct manpaths *paths, size_t ipath,
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov const char *sec, const char *arch, const char *name,
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov warnx("outdated mandoc.db lacks %s(%s) entry, run %s %s",
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov name, sec, BINM_MAKEWHATIS, paths->paths[ipath]);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov *res = mandoc_reallocarray(*res, ++*ressz, sizeof(struct manpage));
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov page->sec = (*sec >= '1' && *sec <= '9') ? *sec - '1' + 1 : 10;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovfs_search(const struct mansearch *cfg, const struct manpaths *paths,
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov int argc, char **argv, struct manpage **res, size_t *ressz)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov const char *const sections[] =
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov {"1", "8", "6", "2", "3", "5", "7", "4", "9", "3p"};
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov const size_t nsec = sizeof(sections)/sizeof(sections[0]);
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankovparse(struct curparse *curp, int fd, const char *file)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Begin by parsing the file itself. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * With -Wstop and warnings or errors of at least the requested
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * level, do not produce output.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* If unset, allocate output dev now (if applicable). */
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov /* Execute the out device, if it exists. */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovpassthrough(const char *file, int fd, int synopsis_only)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov const char synb[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov /* nothing to do */;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov /* nothing to do */;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore switch (getsubopt(&arg, UNCONST(toks), &v)) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore const char *file, int line, int col, const char *msg)
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov const struct timespec timeout = { 0, 100000000 }; /* 0.1s */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov const char *pager;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Parse the pager command into words.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Intentionally do not do anything fancy here.
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov /* For less(1), use the tag file. */
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov if (pledge("stdio rpath tmppath tty proc", NULL) == -1)
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov /* The child process becomes the pager. */
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov /* Do not start the pager before controlling the terminal. */
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov err((int)MANDOCLEVEL_SYSERR, "exec %s", argv[0]);