version 1.13, 2009/03/23 21:20:24 |
version 1.241, 2015/07/19 00:07:42 |
|
|
/* $Id$ */ |
/* $Id$ */ |
/* |
/* |
* Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.org> |
* Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv> |
|
* Copyright (c) 2010-2012, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org> |
|
* Copyright (c) 2010 Joerg Sonnenberger <joerg@netbsd.org> |
* |
* |
* Permission to use, copy, modify, and distribute this software for any |
* Permission to use, copy, modify, and distribute this software for any |
* purpose with or without fee is hereby granted, provided that the |
* purpose with or without fee is hereby granted, provided that the above |
* above copyright notice and this permission notice appear in all |
* copyright notice and this permission notice appear in all copies. |
* copies. |
|
* |
* |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES |
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR |
* AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
* PERFORMANCE OF THIS SOFTWARE. |
|
*/ |
*/ |
#include <sys/stat.h> |
#include "config.h" |
|
|
|
#include <sys/types.h> |
|
#include <sys/param.h> /* MACHINE */ |
|
#include <sys/wait.h> |
|
|
#include <assert.h> |
#include <assert.h> |
#include <err.h> |
#include <ctype.h> |
|
#include <errno.h> |
#include <fcntl.h> |
#include <fcntl.h> |
|
#include <glob.h> |
|
#include <signal.h> |
#include <stdio.h> |
#include <stdio.h> |
|
#include <stdint.h> |
#include <stdlib.h> |
#include <stdlib.h> |
#include <string.h> |
#include <string.h> |
#include <unistd.h> |
#include <unistd.h> |
|
|
|
#include "mandoc_aux.h" |
|
#include "mandoc.h" |
|
#include "roff.h" |
#include "mdoc.h" |
#include "mdoc.h" |
#include "man.h" |
#include "man.h" |
|
#include "tag.h" |
|
#include "main.h" |
|
#include "manconf.h" |
|
#include "mansearch.h" |
|
|
#ifdef __linux__ |
#if !defined(__GNUC__) || (__GNUC__ < 2) |
extern int getsubopt(char **, char * const *, char **); |
# if !defined(lint) |
# ifndef __dead |
# define __attribute__(x) |
# define __dead __attribute__((__noreturn__)) |
|
# endif |
# endif |
#elif defined(__dead2) |
#endif /* !defined(__GNUC__) || (__GNUC__ < 2) */ |
# ifndef __dead |
|
# define __dead __dead2 |
|
# endif |
|
#endif |
|
|
|
struct buf { |
enum outmode { |
char *buf; |
OUTMODE_DEF = 0, |
size_t sz; |
OUTMODE_FLN, |
|
OUTMODE_LST, |
|
OUTMODE_ALL, |
|
OUTMODE_INT, |
|
OUTMODE_ONE |
}; |
}; |
|
|
struct curparse { |
typedef void (*out_mdoc)(void *, const struct roff_man *); |
const char *file; |
typedef void (*out_man)(void *, const struct roff_man *); |
int wflags; |
typedef void (*out_free)(void *); |
#define WARN_WALL 0x03 /* All-warnings mask. */ |
|
#define WARN_WCOMPAT (1 << 0) /* Compatibility warnings. */ |
|
#define WARN_WSYNTAX (1 << 1) /* Syntax warnings. */ |
|
#define WARN_WERR (1 << 2) /* Warnings->errors. */ |
|
}; |
|
|
|
enum intt { |
enum outt { |
INTT_MDOC = 0, |
OUTT_ASCII = 0, /* -Tascii */ |
INTT_MAN |
OUTT_LOCALE, /* -Tlocale */ |
|
OUTT_UTF8, /* -Tutf8 */ |
|
OUTT_TREE, /* -Ttree */ |
|
OUTT_MAN, /* -Tman */ |
|
OUTT_HTML, /* -Thtml */ |
|
OUTT_LINT, /* -Tlint */ |
|
OUTT_PS, /* -Tps */ |
|
OUTT_PDF /* -Tpdf */ |
}; |
}; |
|
|
enum outt { |
struct curparse { |
OUTT_ASCII = 0, |
struct mparse *mp; |
OUTT_LATIN1, |
struct mchars *mchars; /* character table */ |
OUTT_UTF8, |
enum mandoclevel wlevel; /* ignore messages below this */ |
OUTT_TREE, |
int wstop; /* stop after a file with a warning */ |
OUTT_LINT |
enum outt outtype; /* which output to use */ |
|
out_mdoc outmdoc; /* mdoc output ptr */ |
|
out_man outman; /* man output ptr */ |
|
out_free outfree; /* free output ptr */ |
|
void *outdata; /* data for output */ |
|
struct manoutput *outopts; /* output options */ |
}; |
}; |
|
|
typedef int (*out_run)(void *, const struct man *, |
static int fs_lookup(const struct manpaths *, |
const struct mdoc *); |
size_t ipath, const char *, |
typedef void (*out_free)(void *); |
const char *, const char *, |
|
struct manpage **, size_t *); |
|
static void fs_search(const struct mansearch *, |
|
const struct manpaths *, int, char**, |
|
struct manpage **, size_t *); |
|
static void handle_sigpipe(int); |
|
static int koptions(int *, char *); |
|
#if HAVE_SQLITE3 |
|
int mandocdb(int, char**); |
|
#endif |
|
static int moptions(int *, char *); |
|
static void mmsg(enum mandocerr, enum mandoclevel, |
|
const char *, int, int, const char *); |
|
static void parse(struct curparse *, int, const char *); |
|
static void passthrough(const char *, int, int); |
|
static pid_t spawn_pager(void); |
|
static int toptions(struct curparse *, char *); |
|
static void usage(enum argmode) __attribute__((noreturn)); |
|
static int woptions(struct curparse *, char *); |
|
|
extern char *__progname; |
static const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9}; |
|
static char help_arg[] = "help"; |
|
static char *help_argv[] = {help_arg, NULL}; |
|
static const char *progname; |
|
static enum mandoclevel rc; |
|
|
extern void *ascii_alloc(void); |
|
extern void *latin1_alloc(void); |
|
extern void *utf8_alloc(void); |
|
extern int terminal_run(void *, const struct man *, |
|
const struct mdoc *); |
|
extern int tree_run(void *, const struct man *, |
|
const struct mdoc *); |
|
extern void terminal_free(void *); |
|
|
|
static int foptions(int *, char *); |
|
static int toptions(enum outt *, char *); |
|
static int moptions(enum intt *, char *); |
|
static int woptions(int *, char *); |
|
static int merr(void *, int, int, const char *); |
|
static int mwarn(void *, int, int, |
|
enum mdoc_warn, const char *); |
|
static int file(struct buf *, struct buf *, |
|
const char *, |
|
struct man *, struct mdoc *); |
|
static int fdesc(struct buf *, struct buf *, |
|
const char *, int, |
|
struct man *, struct mdoc *); |
|
|
|
__dead static void version(void); |
|
__dead static void usage(void); |
|
|
|
|
|
int |
int |
main(int argc, char *argv[]) |
main(int argc, char *argv[]) |
{ |
{ |
int c, rc, fflags; |
struct manconf conf; |
struct mdoc_cb cb; |
|
struct man *man; |
|
struct mdoc *mdoc; |
|
void *outdata; |
|
enum outt outtype; |
|
enum intt inttype; |
|
struct buf ln, blk; |
|
out_run outrun; |
|
out_free outfree; |
|
struct curparse curp; |
struct curparse curp; |
|
struct mansearch search; |
|
char *auxpaths; |
|
char *defos; |
|
unsigned char *uc; |
|
struct manpage *res, *resp; |
|
char *conf_file, *defpaths; |
|
size_t isec, i, sz; |
|
int prio, best_prio; |
|
char sec; |
|
enum mandoclevel rctmp; |
|
enum outmode outmode; |
|
int fd; |
|
int show_usage; |
|
int options; |
|
int c; |
|
pid_t pager_pid; /* 0: don't use; 1: not yet spawned. */ |
|
|
fflags = 0; |
if (argc < 1) |
outtype = OUTT_ASCII; |
progname = "mandoc"; |
inttype = INTT_MDOC; |
else if ((progname = strrchr(argv[0], '/')) == NULL) |
|
progname = argv[0]; |
|
else |
|
++progname; |
|
|
bzero(&curp, sizeof(struct curparse)); |
#if HAVE_SQLITE3 |
|
if (strcmp(progname, BINM_MAKEWHATIS) == 0) |
|
return(mandocdb(argc, argv)); |
|
#endif |
|
|
/* LINTED */ |
/* Search options. */ |
while (-1 != (c = getopt(argc, argv, "f:m:VW:T:"))) |
|
|
memset(&conf, 0, sizeof(conf)); |
|
conf_file = defpaths = NULL; |
|
auxpaths = NULL; |
|
|
|
memset(&search, 0, sizeof(struct mansearch)); |
|
search.outkey = "Nd"; |
|
|
|
if (strcmp(progname, BINM_MAN) == 0) |
|
search.argmode = ARG_NAME; |
|
else if (strcmp(progname, BINM_APROPOS) == 0) |
|
search.argmode = ARG_EXPR; |
|
else if (strcmp(progname, BINM_WHATIS) == 0) |
|
search.argmode = ARG_WORD; |
|
else if (strncmp(progname, "help", 4) == 0) |
|
search.argmode = ARG_NAME; |
|
else |
|
search.argmode = ARG_FILE; |
|
|
|
/* Parser and formatter options. */ |
|
|
|
memset(&curp, 0, sizeof(struct curparse)); |
|
curp.outtype = OUTT_LOCALE; |
|
curp.wlevel = MANDOCLEVEL_BADARG; |
|
curp.outopts = &conf.output; |
|
options = MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1; |
|
defos = NULL; |
|
|
|
pager_pid = 1; |
|
show_usage = 0; |
|
outmode = OUTMODE_DEF; |
|
|
|
while (-1 != (c = getopt(argc, argv, |
|
"aC:cfhI:iK:klM:m:O:S:s:T:VW:w"))) { |
switch (c) { |
switch (c) { |
case ('f'): |
case 'a': |
if ( ! foptions(&fflags, optarg)) |
outmode = OUTMODE_ALL; |
return(0); |
|
break; |
break; |
case ('m'): |
case 'C': |
if ( ! moptions(&inttype, optarg)) |
conf_file = optarg; |
return(0); |
|
break; |
break; |
case ('T'): |
case 'c': |
if ( ! toptions(&outtype, optarg)) |
pager_pid = 0; |
return(0); |
|
break; |
break; |
case ('W'): |
case 'f': |
if ( ! woptions(&curp.wflags, optarg)) |
search.argmode = ARG_WORD; |
return(0); |
|
break; |
break; |
case ('V'): |
case 'h': |
version(); |
conf.output.synopsisonly = 1; |
/* NOTREACHED */ |
pager_pid = 0; |
|
outmode = OUTMODE_ALL; |
|
break; |
|
case 'I': |
|
if (strncmp(optarg, "os=", 3)) { |
|
fprintf(stderr, |
|
"%s: -I %s: Bad argument\n", |
|
progname, optarg); |
|
return((int)MANDOCLEVEL_BADARG); |
|
} |
|
if (defos) { |
|
fprintf(stderr, |
|
"%s: -I %s: Duplicate argument\n", |
|
progname, optarg); |
|
return((int)MANDOCLEVEL_BADARG); |
|
} |
|
defos = mandoc_strdup(optarg + 3); |
|
break; |
|
case 'i': |
|
outmode = OUTMODE_INT; |
|
break; |
|
case 'K': |
|
if ( ! koptions(&options, optarg)) |
|
return((int)MANDOCLEVEL_BADARG); |
|
break; |
|
case 'k': |
|
search.argmode = ARG_EXPR; |
|
break; |
|
case 'l': |
|
search.argmode = ARG_FILE; |
|
outmode = OUTMODE_ALL; |
|
break; |
|
case 'M': |
|
defpaths = optarg; |
|
break; |
|
case 'm': |
|
auxpaths = optarg; |
|
break; |
|
case 'O': |
|
search.outkey = optarg; |
|
while (optarg != NULL) |
|
manconf_output(&conf.output, |
|
strsep(&optarg, ",")); |
|
break; |
|
case 'S': |
|
search.arch = optarg; |
|
break; |
|
case 's': |
|
search.sec = optarg; |
|
break; |
|
case 'T': |
|
if ( ! toptions(&curp, optarg)) |
|
return((int)MANDOCLEVEL_BADARG); |
|
break; |
|
case 'W': |
|
if ( ! woptions(&curp, optarg)) |
|
return((int)MANDOCLEVEL_BADARG); |
|
break; |
|
case 'w': |
|
outmode = OUTMODE_FLN; |
|
break; |
default: |
default: |
usage(); |
show_usage = 1; |
/* NOTREACHED */ |
break; |
} |
} |
|
} |
|
|
argc -= optind; |
if (show_usage) |
argv += optind; |
usage(search.argmode); |
|
|
/* |
/* Postprocess options. */ |
* Allocate the appropriate front-end. Note that utf8, ascii |
|
* and latin1 all resolve to the terminal front-end with |
|
* different encodings (see terminal.c). Not all frontends have |
|
* cleanup or alloc routines. |
|
*/ |
|
|
|
switch (outtype) { |
if (outmode == OUTMODE_DEF) { |
case (OUTT_LATIN1): |
switch (search.argmode) { |
outdata = latin1_alloc(); |
case ARG_FILE: |
outrun = terminal_run; |
outmode = OUTMODE_ALL; |
outfree = terminal_free; |
pager_pid = 0; |
break; |
break; |
case (OUTT_UTF8): |
case ARG_NAME: |
outdata = utf8_alloc(); |
outmode = OUTMODE_ONE; |
outrun = terminal_run; |
break; |
outfree = terminal_free; |
default: |
break; |
outmode = OUTMODE_LST; |
case (OUTT_TREE): |
break; |
outdata = NULL; |
} |
outrun = tree_run; |
|
outfree = NULL; |
|
break; |
|
case (OUTT_LINT): |
|
outdata = NULL; |
|
outrun = NULL; |
|
outfree = NULL; |
|
break; |
|
default: |
|
outdata = ascii_alloc(); |
|
outrun = terminal_run; |
|
outfree = terminal_free; |
|
break; |
|
} |
} |
|
|
|
/* Parse arguments. */ |
|
|
|
if (argc > 0) { |
|
argc -= optind; |
|
argv += optind; |
|
} |
|
resp = NULL; |
|
|
/* |
/* |
* All callbacks route into here, where we print them onto the |
* Quirks for help(1) |
* screen. XXX - for now, no path for debugging messages. |
* and for a man(1) section argument without -s. |
*/ |
*/ |
|
|
cb.mdoc_msg = NULL; |
if (search.argmode == ARG_NAME) { |
cb.mdoc_err = merr; |
if (*progname == 'h') { |
cb.mdoc_warn = mwarn; |
if (argc == 0) { |
|
argv = help_argv; |
|
argc = 1; |
|
} |
|
} else if (argc > 1 && |
|
((uc = (unsigned char *)argv[0]) != NULL) && |
|
((isdigit(uc[0]) && (uc[1] == '\0' || |
|
(isalpha(uc[1]) && uc[2] == '\0'))) || |
|
(uc[0] == 'n' && uc[1] == '\0'))) { |
|
search.sec = (char *)uc; |
|
argv++; |
|
argc--; |
|
} |
|
if (search.arch == NULL) |
|
search.arch = getenv("MACHINE"); |
|
#ifdef MACHINE |
|
if (search.arch == NULL) |
|
search.arch = MACHINE; |
|
#endif |
|
} |
|
|
bzero(&ln, sizeof(struct buf)); |
rc = MANDOCLEVEL_OK; |
bzero(&blk, sizeof(struct buf)); |
|
|
|
man = NULL; |
/* man(1), whatis(1), apropos(1) */ |
mdoc = NULL; |
|
|
|
switch (inttype) { |
if (search.argmode != ARG_FILE) { |
case (INTT_MAN): |
if (argc == 0) |
man = man_alloc(); |
usage(search.argmode); |
break; |
|
default: |
if (search.argmode == ARG_NAME && |
mdoc = mdoc_alloc(&curp, fflags, &cb); |
outmode == OUTMODE_ONE) |
break; |
search.firstmatch = 1; |
|
|
|
/* Access the mandoc database. */ |
|
|
|
manconf_parse(&conf, conf_file, defpaths, auxpaths); |
|
#if HAVE_SQLITE3 |
|
mansearch_setup(1); |
|
if ( ! mansearch(&search, &conf.manpath, |
|
argc, argv, &res, &sz)) |
|
usage(search.argmode); |
|
#else |
|
if (search.argmode != ARG_NAME) { |
|
fputs("mandoc: database support not compiled in\n", |
|
stderr); |
|
return((int)MANDOCLEVEL_BADARG); |
|
} |
|
sz = 0; |
|
#endif |
|
|
|
if (sz == 0) { |
|
if (search.argmode == ARG_NAME) |
|
fs_search(&search, &conf.manpath, |
|
argc, argv, &res, &sz); |
|
else |
|
fprintf(stderr, |
|
"%s: nothing appropriate\n", |
|
progname); |
|
} |
|
|
|
if (sz == 0) { |
|
rc = MANDOCLEVEL_BADARG; |
|
goto out; |
|
} |
|
|
|
/* |
|
* For standard man(1) and -a output mode, |
|
* prepare for copying filename pointers |
|
* into the program parameter array. |
|
*/ |
|
|
|
if (outmode == OUTMODE_ONE) { |
|
argc = 1; |
|
best_prio = 10; |
|
} else if (outmode == OUTMODE_ALL) |
|
argc = (int)sz; |
|
|
|
/* Iterate all matching manuals. */ |
|
|
|
resp = res; |
|
for (i = 0; i < sz; i++) { |
|
if (outmode == OUTMODE_FLN) |
|
puts(res[i].file); |
|
else if (outmode == OUTMODE_LST) |
|
printf("%s - %s\n", res[i].names, |
|
res[i].output == NULL ? "" : |
|
res[i].output); |
|
else if (outmode == OUTMODE_ONE) { |
|
/* Search for the best section. */ |
|
isec = strcspn(res[i].file, "123456789"); |
|
sec = res[i].file[isec]; |
|
if ('\0' == sec) |
|
continue; |
|
prio = sec_prios[sec - '1']; |
|
if (prio >= best_prio) |
|
continue; |
|
best_prio = prio; |
|
resp = res + i; |
|
} |
|
} |
|
|
|
/* |
|
* For man(1), -a and -i output mode, fall through |
|
* to the main mandoc(1) code iterating files |
|
* and running the parsers on each of them. |
|
*/ |
|
|
|
if (outmode == OUTMODE_FLN || outmode == OUTMODE_LST) |
|
goto out; |
} |
} |
|
|
|
/* mandoc(1) */ |
|
|
|
if (search.argmode == ARG_FILE && ! moptions(&options, auxpaths)) |
|
return((int)MANDOCLEVEL_BADARG); |
|
|
|
if (pager_pid == 1 && isatty(STDOUT_FILENO) == 0) |
|
pager_pid = 0; |
|
|
|
curp.mchars = mchars_alloc(); |
|
curp.mp = mparse_alloc(options, curp.wlevel, mmsg, |
|
curp.mchars, defos); |
|
|
/* |
/* |
* Loop around available files. |
* Conditionally start up the lookaside buffer before parsing. |
*/ |
*/ |
|
if (OUTT_MAN == curp.outtype) |
|
mparse_keep(curp.mp); |
|
|
if (NULL == *argv) { |
if (argc < 1) { |
curp.file = "<stdin>"; |
if (pager_pid == 1) |
rc = 0; |
pager_pid = spawn_pager(); |
c = fdesc(&blk, &ln, "stdin", |
parse(&curp, STDIN_FILENO, "<stdin>"); |
STDIN_FILENO, man, mdoc); |
} |
|
|
if (c && NULL == outrun) |
while (argc > 0) { |
rc = 1; |
rctmp = mparse_open(curp.mp, &fd, |
else if (c && outrun && (*outrun)(outdata, man, mdoc)) |
resp != NULL ? resp->file : *argv); |
rc = 1; |
if (rc < rctmp) |
} else { |
rc = rctmp; |
while (*argv) { |
|
curp.file = *argv; |
|
c = file(&blk, &ln, *argv, man, mdoc); |
|
if ( ! c) |
|
break; |
|
if (outrun && ! (*outrun)(outdata, man, mdoc)) |
|
break; |
|
if (man) |
|
man_reset(man); |
|
if (mdoc) |
|
mdoc_reset(mdoc); |
|
|
|
argv++; |
if (fd != -1) { |
|
if (pager_pid == 1) |
|
pager_pid = spawn_pager(); |
|
|
|
if (resp == NULL) |
|
parse(&curp, fd, *argv); |
|
else if (resp->form & FORM_SRC) { |
|
/* For .so only; ignore failure. */ |
|
chdir(conf.manpath.paths[resp->ipath]); |
|
parse(&curp, fd, resp->file); |
|
} else |
|
passthrough(resp->file, fd, |
|
conf.output.synopsisonly); |
|
|
|
rctmp = mparse_wait(curp.mp); |
|
if (rc < rctmp) |
|
rc = rctmp; |
|
|
|
if (argc > 1 && curp.outtype <= OUTT_UTF8) |
|
ascii_sepline(curp.outdata); |
} |
} |
rc = NULL == *argv; |
|
|
if (MANDOCLEVEL_OK != rc && curp.wstop) |
|
break; |
|
|
|
if (resp != NULL) |
|
resp++; |
|
else |
|
argv++; |
|
if (--argc) |
|
mparse_reset(curp.mp); |
} |
} |
|
|
if (blk.buf) |
if (curp.outfree) |
free(blk.buf); |
(*curp.outfree)(curp.outdata); |
if (ln.buf) |
mparse_free(curp.mp); |
free(ln.buf); |
mchars_free(curp.mchars); |
if (outfree) |
|
(*outfree)(outdata); |
|
if (mdoc) |
|
mdoc_free(mdoc); |
|
if (man) |
|
man_free(man); |
|
|
|
return(rc ? EXIT_SUCCESS : EXIT_FAILURE); |
out: |
} |
if (search.argmode != ARG_FILE) { |
|
manconf_free(&conf); |
|
#if HAVE_SQLITE3 |
|
mansearch_free(res, sz); |
|
mansearch_setup(0); |
|
#endif |
|
} |
|
|
|
free(defos); |
|
|
__dead static void |
/* |
version(void) |
* If a pager is attached, flush the pipe leading to it |
{ |
* and signal end of file such that the user can browse |
|
* to the end. Then wait for the user to close the pager. |
|
*/ |
|
|
(void)printf("%s %s\n", __progname, VERSION); |
if (pager_pid != 0 && pager_pid != 1) { |
exit(0); |
fclose(stdout); |
/* NOTREACHED */ |
tag_write(); |
|
waitpid(pager_pid, NULL, 0); |
|
tag_unlink(); |
|
} |
|
|
|
return((int)rc); |
} |
} |
|
|
|
static void |
__dead static void |
usage(enum argmode argmode) |
usage(void) |
|
{ |
{ |
|
|
(void)fprintf(stderr, "usage: %s [-V] [-foption...] " |
switch (argmode) { |
"[-mformat] [-Toutput] [-Werr...]\n", |
case ARG_FILE: |
__progname); |
fputs("usage: mandoc [-acfhkl] [-I os=name] " |
exit(1); |
"[-K encoding] [-mformat] [-O option]\n" |
/* NOTREACHED */ |
"\t [-T output] [-W level] [file ...]\n", stderr); |
|
break; |
|
case ARG_NAME: |
|
fputs("usage: man [-acfhklw] [-C file] [-I os=name] " |
|
"[-K encoding] [-M path] [-m path]\n" |
|
"\t [-O option=value] [-S subsection] [-s section] " |
|
"[-T output] [-W level]\n" |
|
"\t [section] name ...\n", stderr); |
|
break; |
|
case ARG_WORD: |
|
fputs("usage: whatis [-acfhklw] [-C file] " |
|
"[-M path] [-m path] [-O outkey] [-S arch]\n" |
|
"\t [-s section] name ...\n", stderr); |
|
break; |
|
case ARG_EXPR: |
|
fputs("usage: apropos [-acfhklw] [-C file] " |
|
"[-M path] [-m path] [-O outkey] [-S arch]\n" |
|
"\t [-s section] expression ...\n", stderr); |
|
break; |
|
} |
|
exit((int)MANDOCLEVEL_BADARG); |
} |
} |
|
|
|
|
static int |
static int |
file(struct buf *blk, struct buf *ln, const char *file, |
fs_lookup(const struct manpaths *paths, size_t ipath, |
struct man *man, struct mdoc *mdoc) |
const char *sec, const char *arch, const char *name, |
|
struct manpage **res, size_t *ressz) |
{ |
{ |
int fd, c; |
glob_t globinfo; |
|
struct manpage *page; |
|
char *file; |
|
int form, globres; |
|
|
if (-1 == (fd = open(file, O_RDONLY, 0))) { |
form = FORM_SRC; |
warn("%s", file); |
mandoc_asprintf(&file, "%s/man%s/%s.%s", |
return(0); |
paths->paths[ipath], sec, name, sec); |
|
if (access(file, R_OK) != -1) |
|
goto found; |
|
free(file); |
|
|
|
mandoc_asprintf(&file, "%s/cat%s/%s.0", |
|
paths->paths[ipath], sec, name); |
|
if (access(file, R_OK) != -1) { |
|
form = FORM_CAT; |
|
goto found; |
} |
} |
|
free(file); |
|
|
c = fdesc(blk, ln, file, fd, man, mdoc); |
if (arch != NULL) { |
|
mandoc_asprintf(&file, "%s/man%s/%s/%s.%s", |
|
paths->paths[ipath], sec, arch, name, sec); |
|
if (access(file, R_OK) != -1) |
|
goto found; |
|
free(file); |
|
} |
|
|
if (-1 == close(fd)) |
mandoc_asprintf(&file, "%s/man%s/%s.[01-9]*", |
warn("%s", file); |
paths->paths[ipath], sec, name); |
|
globres = glob(file, 0, NULL, &globinfo); |
|
if (globres != 0 && globres != GLOB_NOMATCH) |
|
fprintf(stderr, "%s: %s: glob: %s\n", |
|
progname, file, strerror(errno)); |
|
free(file); |
|
if (globres == 0) |
|
file = mandoc_strdup(*globinfo.gl_pathv); |
|
globfree(&globinfo); |
|
if (globres != 0) |
|
return(0); |
|
|
return(c); |
found: |
|
#if HAVE_SQLITE3 |
|
fprintf(stderr, "%s: outdated mandoc.db lacks %s(%s) entry, run " |
|
"makewhatis %s\n", progname, name, sec, paths->paths[ipath]); |
|
#endif |
|
*res = mandoc_reallocarray(*res, ++*ressz, sizeof(struct manpage)); |
|
page = *res + (*ressz - 1); |
|
page->file = file; |
|
page->names = NULL; |
|
page->output = NULL; |
|
page->ipath = ipath; |
|
page->bits = NAME_FILE & NAME_MASK; |
|
page->sec = (*sec >= '1' && *sec <= '9') ? *sec - '1' + 1 : 10; |
|
page->form = form; |
|
return(1); |
} |
} |
|
|
|
static void |
|
fs_search(const struct mansearch *cfg, const struct manpaths *paths, |
|
int argc, char **argv, struct manpage **res, size_t *ressz) |
|
{ |
|
const char *const sections[] = |
|
{"1", "8", "6", "2", "3", "3p", "5", "7", "4", "9"}; |
|
const size_t nsec = sizeof(sections)/sizeof(sections[0]); |
|
|
static int |
size_t ipath, isec, lastsz; |
fdesc(struct buf *blk, struct buf *ln, |
|
const char *f, int fd, |
assert(cfg->argmode == ARG_NAME); |
struct man *man, struct mdoc *mdoc) |
|
|
*res = NULL; |
|
*ressz = lastsz = 0; |
|
while (argc) { |
|
for (ipath = 0; ipath < paths->sz; ipath++) { |
|
if (cfg->sec != NULL) { |
|
if (fs_lookup(paths, ipath, cfg->sec, |
|
cfg->arch, *argv, res, ressz) && |
|
cfg->firstmatch) |
|
return; |
|
} else for (isec = 0; isec < nsec; isec++) |
|
if (fs_lookup(paths, ipath, sections[isec], |
|
cfg->arch, *argv, res, ressz) && |
|
cfg->firstmatch) |
|
return; |
|
} |
|
if (*ressz == lastsz) |
|
fprintf(stderr, |
|
"%s: No entry for %s in the manual.\n", |
|
progname, *argv); |
|
lastsz = *ressz; |
|
argv++; |
|
argc--; |
|
} |
|
} |
|
|
|
static void |
|
parse(struct curparse *curp, int fd, const char *file) |
{ |
{ |
size_t sz; |
enum mandoclevel rctmp; |
ssize_t ssz; |
struct roff_man *man; |
struct stat st; |
|
int j, i, pos, lnn; |
|
|
|
assert( ! (man && mdoc)); |
/* Begin by parsing the file itself. */ |
|
|
|
assert(file); |
|
assert(fd >= -1); |
|
|
|
rctmp = mparse_readfd(curp->mp, fd, file); |
|
if (rc < rctmp) |
|
rc = rctmp; |
|
|
/* |
/* |
* Two buffers: ln and buf. buf is the input buffer, optimised |
* With -Wstop and warnings or errors of at least the requested |
* for each file's block size. ln is a line buffer. Both |
* level, do not produce output. |
* growable, hence passed in by ptr-ptr. |
|
*/ |
*/ |
|
|
sz = BUFSIZ; |
if (rctmp != MANDOCLEVEL_OK && curp->wstop) |
|
return; |
|
|
if (-1 == fstat(fd, &st)) |
/* If unset, allocate output dev now (if applicable). */ |
warnx("%s", f); |
|
else if ((size_t)st.st_blksize > sz) |
|
sz = st.st_blksize; |
|
|
|
if (sz > blk->sz) { |
if ( ! (curp->outman && curp->outmdoc)) { |
blk->buf = realloc(blk->buf, sz); |
switch (curp->outtype) { |
if (NULL == blk->buf) |
case OUTT_HTML: |
err(1, "realloc"); |
curp->outdata = html_alloc(curp->mchars, |
blk->sz = sz; |
curp->outopts); |
|
curp->outfree = html_free; |
|
break; |
|
case OUTT_UTF8: |
|
curp->outdata = utf8_alloc(curp->mchars, |
|
curp->outopts); |
|
curp->outfree = ascii_free; |
|
break; |
|
case OUTT_LOCALE: |
|
curp->outdata = locale_alloc(curp->mchars, |
|
curp->outopts); |
|
curp->outfree = ascii_free; |
|
break; |
|
case OUTT_ASCII: |
|
curp->outdata = ascii_alloc(curp->mchars, |
|
curp->outopts); |
|
curp->outfree = ascii_free; |
|
break; |
|
case OUTT_PDF: |
|
curp->outdata = pdf_alloc(curp->mchars, |
|
curp->outopts); |
|
curp->outfree = pspdf_free; |
|
break; |
|
case OUTT_PS: |
|
curp->outdata = ps_alloc(curp->mchars, |
|
curp->outopts); |
|
curp->outfree = pspdf_free; |
|
break; |
|
default: |
|
break; |
|
} |
|
|
|
switch (curp->outtype) { |
|
case OUTT_HTML: |
|
curp->outman = html_man; |
|
curp->outmdoc = html_mdoc; |
|
break; |
|
case OUTT_TREE: |
|
curp->outman = tree_man; |
|
curp->outmdoc = tree_mdoc; |
|
break; |
|
case OUTT_MAN: |
|
curp->outmdoc = man_mdoc; |
|
curp->outman = man_man; |
|
break; |
|
case OUTT_PDF: |
|
/* FALLTHROUGH */ |
|
case OUTT_ASCII: |
|
/* FALLTHROUGH */ |
|
case OUTT_UTF8: |
|
/* FALLTHROUGH */ |
|
case OUTT_LOCALE: |
|
/* FALLTHROUGH */ |
|
case OUTT_PS: |
|
curp->outman = terminal_man; |
|
curp->outmdoc = terminal_mdoc; |
|
break; |
|
default: |
|
break; |
|
} |
} |
} |
|
|
/* |
mparse_result(curp->mp, &man, NULL); |
* Fill buf with file blocksize and parse newlines into ln. |
|
*/ |
|
|
|
for (lnn = 1, pos = 0; ; ) { |
/* Execute the out device, if it exists. */ |
if (-1 == (ssz = read(fd, blk->buf, sz))) { |
|
warn("%s", f); |
|
return(0); |
|
} else if (0 == ssz) |
|
break; |
|
|
|
for (i = 0; i < (int)ssz; i++) { |
if (man == NULL) |
if (pos >= (int)ln->sz) { |
return; |
ln->sz += 256; /* Step-size. */ |
if (curp->outmdoc != NULL && man->macroset == MACROSET_MDOC) |
ln->buf = realloc(ln->buf, ln->sz); |
(*curp->outmdoc)(curp->outdata, man); |
if (NULL == ln->buf) |
if (curp->outman != NULL && man->macroset == MACROSET_MAN) |
err(1, "realloc"); |
(*curp->outman)(curp->outdata, man); |
} |
} |
|
|
if ('\n' != blk->buf[i]) { |
static void |
ln->buf[pos++] = blk->buf[i]; |
passthrough(const char *file, int fd, int synopsis_only) |
continue; |
{ |
} |
const char synb[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS"; |
|
const char synr[] = "SYNOPSIS"; |
|
|
/* Check for CPP-escaped newline. */ |
FILE *stream; |
|
const char *syscall; |
|
char *line; |
|
size_t len, off; |
|
ssize_t nw; |
|
int print; |
|
|
if (pos > 0 && '\\' == ln->buf[pos - 1]) { |
fflush(stdout); |
for (j = pos - 1; j >= 0; j--) |
|
if ('\\' != ln->buf[j]) |
|
break; |
|
|
|
if ( ! ((pos - j) % 2)) { |
if ((stream = fdopen(fd, "r")) == NULL) { |
pos--; |
close(fd); |
lnn++; |
syscall = "fdopen"; |
continue; |
goto fail; |
|
} |
|
|
|
print = 0; |
|
while ((line = fgetln(stream, &len)) != NULL) { |
|
if (synopsis_only) { |
|
if (print) { |
|
if ( ! isspace((unsigned char)*line)) |
|
goto done; |
|
while (len && |
|
isspace((unsigned char)*line)) { |
|
line++; |
|
len--; |
} |
} |
|
} else { |
|
if ((len == sizeof(synb) && |
|
! strncmp(line, synb, len - 1)) || |
|
(len == sizeof(synr) && |
|
! strncmp(line, synr, len - 1))) |
|
print = 1; |
|
continue; |
} |
} |
|
|
ln->buf[pos] = 0; |
|
if (mdoc && ! mdoc_parseln(mdoc, lnn, ln->buf)) |
|
return(0); |
|
if (man && ! man_parseln(man, lnn, ln->buf)) |
|
return(0); |
|
lnn++; |
|
pos = 0; |
|
} |
} |
|
for (off = 0; off < len; off += nw) |
|
if ((nw = write(STDOUT_FILENO, line + off, |
|
len - off)) == -1 || nw == 0) { |
|
fclose(stream); |
|
syscall = "write"; |
|
goto fail; |
|
} |
} |
} |
|
|
if (mdoc) |
if (ferror(stream)) { |
return(mdoc_endparse(mdoc)); |
fclose(stream); |
|
syscall = "fgetln"; |
|
goto fail; |
|
} |
|
|
return(man_endparse(man)); |
done: |
|
fclose(stream); |
|
return; |
|
|
|
fail: |
|
fprintf(stderr, "%s: %s: SYSERR: %s: %s", |
|
progname, file, syscall, strerror(errno)); |
|
if (rc < MANDOCLEVEL_SYSERR) |
|
rc = MANDOCLEVEL_SYSERR; |
} |
} |
|
|
|
|
static int |
static int |
moptions(enum intt *tflags, char *arg) |
koptions(int *options, char *arg) |
{ |
{ |
|
|
if (0 == strcmp(arg, "mdoc")) |
if ( ! strcmp(arg, "utf-8")) { |
*tflags = INTT_MDOC; |
*options |= MPARSE_UTF8; |
else if (0 == strcmp(arg, "man")) |
*options &= ~MPARSE_LATIN1; |
*tflags = INTT_MAN; |
} else if ( ! strcmp(arg, "iso-8859-1")) { |
else { |
*options |= MPARSE_LATIN1; |
warnx("bad argument: -m%s", arg); |
*options &= ~MPARSE_UTF8; |
|
} else if ( ! strcmp(arg, "us-ascii")) { |
|
*options &= ~(MPARSE_UTF8 | MPARSE_LATIN1); |
|
} else { |
|
fprintf(stderr, "%s: -K %s: Bad argument\n", |
|
progname, arg); |
return(0); |
return(0); |
} |
} |
|
|
return(1); |
return(1); |
} |
} |
|
|
|
|
static int |
static int |
toptions(enum outt *tflags, char *arg) |
moptions(int *options, char *arg) |
{ |
{ |
|
|
if (0 == strcmp(arg, "ascii")) |
if (arg == NULL) |
*tflags = OUTT_ASCII; |
/* nothing to do */; |
else if (0 == strcmp(arg, "latin1")) |
else if (0 == strcmp(arg, "doc")) |
*tflags = OUTT_LATIN1; |
*options |= MPARSE_MDOC; |
else if (0 == strcmp(arg, "utf8")) |
else if (0 == strcmp(arg, "andoc")) |
*tflags = OUTT_UTF8; |
/* nothing to do */; |
else if (0 == strcmp(arg, "lint")) |
else if (0 == strcmp(arg, "an")) |
*tflags = OUTT_LINT; |
*options |= MPARSE_MAN; |
else if (0 == strcmp(arg, "tree")) |
|
*tflags = OUTT_TREE; |
|
else { |
else { |
warnx("bad argument: -T%s", arg); |
fprintf(stderr, "%s: -m %s: Bad argument\n", |
|
progname, arg); |
return(0); |
return(0); |
} |
} |
|
|
return(1); |
return(1); |
} |
} |
|
|
|
|
/* |
|
* Parse out the options for [-fopt...] setting compiler options. These |
|
* can be comma-delimited or called again. |
|
*/ |
|
static int |
static int |
foptions(int *fflags, char *arg) |
toptions(struct curparse *curp, char *arg) |
{ |
{ |
char *v; |
|
char *toks[4]; |
|
|
|
toks[0] = "ign-scope"; |
if (0 == strcmp(arg, "ascii")) |
toks[1] = "ign-escape"; |
curp->outtype = OUTT_ASCII; |
toks[2] = "ign-macro"; |
else if (0 == strcmp(arg, "lint")) { |
toks[3] = NULL; |
curp->outtype = OUTT_LINT; |
|
curp->wlevel = MANDOCLEVEL_WARNING; |
|
} else if (0 == strcmp(arg, "tree")) |
|
curp->outtype = OUTT_TREE; |
|
else if (0 == strcmp(arg, "man")) |
|
curp->outtype = OUTT_MAN; |
|
else if (0 == strcmp(arg, "html")) |
|
curp->outtype = OUTT_HTML; |
|
else if (0 == strcmp(arg, "utf8")) |
|
curp->outtype = OUTT_UTF8; |
|
else if (0 == strcmp(arg, "locale")) |
|
curp->outtype = OUTT_LOCALE; |
|
else if (0 == strcmp(arg, "xhtml")) |
|
curp->outtype = OUTT_HTML; |
|
else if (0 == strcmp(arg, "ps")) |
|
curp->outtype = OUTT_PS; |
|
else if (0 == strcmp(arg, "pdf")) |
|
curp->outtype = OUTT_PDF; |
|
else { |
|
fprintf(stderr, "%s: -T %s: Bad argument\n", |
|
progname, arg); |
|
return(0); |
|
} |
|
|
while (*arg) |
|
switch (getsubopt(&arg, toks, &v)) { |
|
case (0): |
|
*fflags |= MDOC_IGN_SCOPE; |
|
break; |
|
case (1): |
|
*fflags |= MDOC_IGN_ESCAPE; |
|
break; |
|
case (2): |
|
*fflags |= MDOC_IGN_MACRO; |
|
break; |
|
default: |
|
warnx("bad argument: -f%s", arg); |
|
return(0); |
|
} |
|
|
|
return(1); |
return(1); |
} |
} |
|
|
|
|
/* |
|
* Parse out the options for [-Werr...], which sets warning modes. |
|
* These can be comma-delimited or called again. |
|
*/ |
|
static int |
static int |
woptions(int *wflags, char *arg) |
woptions(struct curparse *curp, char *arg) |
{ |
{ |
char *v; |
char *v, *o; |
char *toks[5]; |
const char *toks[7]; |
|
|
toks[0] = "all"; |
toks[0] = "stop"; |
toks[1] = "compat"; |
toks[1] = "all"; |
toks[2] = "syntax"; |
toks[2] = "warning"; |
toks[3] = "error"; |
toks[3] = "error"; |
toks[4] = NULL; |
toks[4] = "unsupp"; |
|
toks[5] = "fatal"; |
|
toks[6] = NULL; |
|
|
while (*arg) |
while (*arg) { |
switch (getsubopt(&arg, toks, &v)) { |
o = arg; |
case (0): |
switch (getsubopt(&arg, UNCONST(toks), &v)) { |
*wflags |= WARN_WALL; |
case 0: |
|
curp->wstop = 1; |
break; |
break; |
case (1): |
case 1: |
*wflags |= WARN_WCOMPAT; |
/* FALLTHROUGH */ |
|
case 2: |
|
curp->wlevel = MANDOCLEVEL_WARNING; |
break; |
break; |
case (2): |
case 3: |
*wflags |= WARN_WSYNTAX; |
curp->wlevel = MANDOCLEVEL_ERROR; |
break; |
break; |
case (3): |
case 4: |
*wflags |= WARN_WERR; |
curp->wlevel = MANDOCLEVEL_UNSUPP; |
break; |
break; |
|
case 5: |
|
curp->wlevel = MANDOCLEVEL_BADARG; |
|
break; |
default: |
default: |
warnx("bad argument: -W%s", arg); |
fprintf(stderr, "%s: -W %s: Bad argument\n", |
|
progname, o); |
return(0); |
return(0); |
} |
} |
|
} |
|
|
return(1); |
return(1); |
} |
} |
|
|
|
static void |
/* ARGSUSED */ |
mmsg(enum mandocerr t, enum mandoclevel lvl, |
static int |
const char *file, int line, int col, const char *msg) |
merr(void *arg, int line, int col, const char *msg) |
|
{ |
{ |
struct curparse *curp; |
const char *mparse_msg; |
|
|
curp = (struct curparse *)arg; |
fprintf(stderr, "%s: %s:", progname, file); |
|
|
warnx("%s:%d: error: %s (column %d)", |
if (line) |
curp->file, line, msg, col); |
fprintf(stderr, "%d:%d:", line, col + 1); |
return(0); |
|
|
fprintf(stderr, " %s", mparse_strlevel(lvl)); |
|
|
|
if (NULL != (mparse_msg = mparse_strerror(t))) |
|
fprintf(stderr, ": %s", mparse_msg); |
|
|
|
if (msg) |
|
fprintf(stderr, ": %s", msg); |
|
|
|
fputc('\n', stderr); |
} |
} |
|
|
|
static void |
|
handle_sigpipe(int signum) |
|
{ |
|
|
static int |
exit((int)rc); |
mwarn(void *arg, int line, int col, |
} |
enum mdoc_warn type, const char *msg) |
|
|
static pid_t |
|
spawn_pager(void) |
{ |
{ |
struct curparse *curp; |
#define MAX_PAGER_ARGS 16 |
char *wtype; |
char *argv[MAX_PAGER_ARGS]; |
|
const char *pager; |
|
char *cp; |
|
size_t cmdlen; |
|
int fildes[2]; |
|
int argc; |
|
pid_t pager_pid; |
|
|
curp = (struct curparse *)arg; |
pager = getenv("MANPAGER"); |
wtype = NULL; |
if (pager == NULL || *pager == '\0') |
|
pager = getenv("PAGER"); |
|
if (pager == NULL || *pager == '\0') |
|
pager = "more -s"; |
|
cp = mandoc_strdup(pager); |
|
|
switch (type) { |
/* |
case (WARN_COMPAT): |
* Parse the pager command into words. |
wtype = "compat"; |
* Intentionally do not do anything fancy here. |
if (curp->wflags & WARN_WCOMPAT) |
*/ |
|
|
|
argc = 0; |
|
while (argc + 4 < MAX_PAGER_ARGS) { |
|
argv[argc++] = cp; |
|
cp = strchr(cp, ' '); |
|
if (cp == NULL) |
break; |
break; |
return(1); |
*cp++ = '\0'; |
case (WARN_SYNTAX): |
while (*cp == ' ') |
wtype = "syntax"; |
cp++; |
if (curp->wflags & WARN_WSYNTAX) |
if (*cp == '\0') |
break; |
break; |
return(1); |
|
} |
} |
|
|
assert(wtype); |
/* Read all text right away and use the tag file. */ |
warnx("%s:%d: %s warning: %s (column %d)", |
|
curp->file, line, wtype, msg, col); |
|
|
|
if ( ! (curp->wflags & WARN_WERR)) |
if ((cmdlen = strlen(argv[0])) >= 4) { |
return(1); |
cp = argv[0] + cmdlen - 4; |
|
if (strcmp(cp, "less") == 0 || |
|
strcmp(cp, "more") == 0) { |
|
tag_init(); |
|
argv[argc++] = mandoc_strdup("+G1G"); |
|
argv[argc++] = mandoc_strdup("-T"); |
|
argv[argc++] = tag_filename(); |
|
} |
|
} |
|
argv[argc] = NULL; |
|
|
warnx("%s: considering warnings as errors", |
if (pipe(fildes) == -1) { |
__progname); |
fprintf(stderr, "%s: pipe: %s\n", |
return(0); |
progname, strerror(errno)); |
} |
return(0); |
|
} |
|
|
|
switch (pager_pid = fork()) { |
|
case -1: |
|
fprintf(stderr, "%s: fork: %s\n", |
|
progname, strerror(errno)); |
|
exit((int)MANDOCLEVEL_SYSERR); |
|
case 0: |
|
break; |
|
default: |
|
close(fildes[0]); |
|
if (dup2(fildes[1], STDOUT_FILENO) == -1) { |
|
fprintf(stderr, "%s: dup output: %s\n", |
|
progname, strerror(errno)); |
|
exit((int)MANDOCLEVEL_SYSERR); |
|
} |
|
close(fildes[1]); |
|
signal(SIGPIPE, handle_sigpipe); |
|
return(pager_pid); |
|
} |
|
|
|
/* The child process becomes the pager. */ |
|
|
|
close(fildes[1]); |
|
if (dup2(fildes[0], STDIN_FILENO) == -1) { |
|
fprintf(stderr, "%s: dup input: %s\n", |
|
progname, strerror(errno)); |
|
exit((int)MANDOCLEVEL_SYSERR); |
|
} |
|
close(fildes[0]); |
|
|
|
/* Hand over to the pager. */ |
|
|
|
execvp(argv[0], argv); |
|
fprintf(stderr, "%s: exec %s: %s\n", |
|
progname, argv[0], strerror(errno)); |
|
exit((int)MANDOCLEVEL_SYSERR); |
|
} |