version 1.206, 2014/12/15 18:05:57 |
version 1.216, 2015/01/16 21:15:05 |
|
|
/* $Id$ */ |
/* $Id$ */ |
/* |
/* |
* Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv> |
* Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv> |
* Copyright (c) 2010, 2011, 2012, 2014 Ingo Schwarze <schwarze@openbsd.org> |
* Copyright (c) 2010-2012, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org> |
* Copyright (c) 2010 Joerg Sonnenberger <joerg@netbsd.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 |
|
|
#include "config.h" |
#include "config.h" |
|
|
#include <sys/types.h> |
#include <sys/types.h> |
|
#include <sys/param.h> /* MACHINE */ |
|
|
#include <assert.h> |
#include <assert.h> |
#include <ctype.h> |
#include <ctype.h> |
Line 82 struct curparse { |
|
Line 83 struct curparse { |
|
char outopts[BUFSIZ]; /* buf of output opts */ |
char outopts[BUFSIZ]; /* buf of output opts */ |
}; |
}; |
|
|
|
#if HAVE_SQLITE3 |
|
static int fs_lookup(const struct manpaths *, |
|
size_t ipath, const char *, |
|
const char *, const char *, |
|
struct manpage **, size_t *); |
|
static void fs_search(const struct mansearch *, |
|
const struct manpaths *, int, char**, |
|
struct manpage **, size_t *); |
|
#endif |
static int koptions(int *, char *); |
static int koptions(int *, char *); |
#if HAVE_SQLITE3 |
#if HAVE_SQLITE3 |
int mandocdb(int, char**); |
int mandocdb(int, char**); |
Line 114 main(int argc, char *argv[]) |
|
Line 124 main(int argc, char *argv[]) |
|
struct manpaths paths; |
struct manpaths paths; |
char *auxpaths; |
char *auxpaths; |
char *defos; |
char *defos; |
|
unsigned char *uc; |
#if HAVE_SQLITE3 |
#if HAVE_SQLITE3 |
struct manpage *res, *resp; |
struct manpage *res, *resp; |
char *conf_file, *defpaths; |
char *conf_file, *defpaths; |
Line 121 main(int argc, char *argv[]) |
|
Line 132 main(int argc, char *argv[]) |
|
int prio, best_prio, synopsis_only; |
int prio, best_prio, synopsis_only; |
char sec; |
char sec; |
#endif |
#endif |
enum mandoclevel rc; |
enum mandoclevel rc, rctmp; |
enum outmode outmode; |
enum outmode outmode; |
int fd; |
int fd; |
int show_usage; |
int show_usage; |
Line 166 main(int argc, char *argv[]) |
|
Line 177 main(int argc, char *argv[]) |
|
|
|
memset(&curp, 0, sizeof(struct curparse)); |
memset(&curp, 0, sizeof(struct curparse)); |
curp.outtype = OUTT_LOCALE; |
curp.outtype = OUTT_LOCALE; |
curp.wlevel = MANDOCLEVEL_FATAL; |
curp.wlevel = MANDOCLEVEL_BADARG; |
options = MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1; |
options = MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1; |
defos = NULL; |
defos = NULL; |
|
|
Line 205 main(int argc, char *argv[]) |
|
Line 216 main(int argc, char *argv[]) |
|
case 'I': |
case 'I': |
if (strncmp(optarg, "os=", 3)) { |
if (strncmp(optarg, "os=", 3)) { |
fprintf(stderr, |
fprintf(stderr, |
"%s: -I%s: Bad argument\n", |
"%s: -I %s: Bad argument\n", |
progname, optarg); |
progname, optarg); |
return((int)MANDOCLEVEL_BADARG); |
return((int)MANDOCLEVEL_BADARG); |
} |
} |
if (defos) { |
if (defos) { |
fprintf(stderr, |
fprintf(stderr, |
"%s: -I%s: Duplicate argument\n", |
"%s: -I %s: Duplicate argument\n", |
progname, optarg); |
progname, optarg); |
return((int)MANDOCLEVEL_BADARG); |
return((int)MANDOCLEVEL_BADARG); |
} |
} |
Line 309 main(int argc, char *argv[]) |
|
Line 320 main(int argc, char *argv[]) |
|
argv = help_argv; |
argv = help_argv; |
argc = 1; |
argc = 1; |
} |
} |
} else if (argv[0] != NULL && ( |
} else if (argc > 1 && |
(isdigit((unsigned char)argv[0][0]) && |
((uc = argv[0]) != NULL) && |
(argv[0][1] == '\0' || !strcmp(argv[0], "3p"))) || |
((isdigit(uc[0]) && (uc[1] == '\0' || |
(argv[0][0] == 'n' && argv[0][1] == '\0'))) { |
(isalpha(uc[1]) && uc[2] == '\0'))) || |
search.sec = argv[0]; |
(uc[0] == 'n' && uc[1] == '\0'))) { |
|
search.sec = uc; |
argv++; |
argv++; |
argc--; |
argc--; |
} |
} |
|
if (search.arch == NULL) |
|
search.arch = getenv("MACHINE"); |
|
if (search.arch == NULL) |
|
search.arch = MACHINE; |
} |
} |
|
|
rc = MANDOCLEVEL_OK; |
rc = MANDOCLEVEL_OK; |
Line 338 main(int argc, char *argv[]) |
|
Line 354 main(int argc, char *argv[]) |
|
mansearch_setup(1); |
mansearch_setup(1); |
if( ! mansearch(&search, &paths, argc, argv, &res, &sz)) |
if( ! mansearch(&search, &paths, argc, argv, &res, &sz)) |
usage(search.argmode); |
usage(search.argmode); |
resp = res; |
|
|
|
|
if (sz == 0 && search.argmode == ARG_NAME) |
|
fs_search(&search, &paths, argc, argv, &res, &sz); |
|
|
if (sz == 0) { |
if (sz == 0) { |
if (search.argmode == ARG_NAME) |
|
fprintf(stderr, "%s: No entry for %s " |
|
"in the manual.\n", progname, argv[0]); |
|
rc = MANDOCLEVEL_BADARG; |
rc = MANDOCLEVEL_BADARG; |
goto out; |
goto out; |
} |
} |
Line 362 main(int argc, char *argv[]) |
|
Line 377 main(int argc, char *argv[]) |
|
|
|
/* Iterate all matching manuals. */ |
/* Iterate all matching manuals. */ |
|
|
|
resp = res; |
for (i = 0; i < sz; i++) { |
for (i = 0; i < sz; i++) { |
if (outmode == OUTMODE_FLN) |
if (outmode == OUTMODE_FLN) |
puts(res[i].file); |
puts(res[i].file); |
Line 400 main(int argc, char *argv[]) |
|
Line 416 main(int argc, char *argv[]) |
|
|
|
/* mandoc(1) */ |
/* mandoc(1) */ |
|
|
if ( ! moptions(&options, auxpaths)) |
if (search.argmode == ARG_FILE && ! moptions(&options, auxpaths)) |
return((int)MANDOCLEVEL_BADARG); |
return((int)MANDOCLEVEL_BADARG); |
|
|
if (use_pager && isatty(STDOUT_FILENO)) |
|
spawn_pager(); |
|
|
|
curp.mchars = mchars_alloc(); |
curp.mchars = mchars_alloc(); |
curp.mp = mparse_alloc(options, curp.wlevel, mmsg, |
curp.mp = mparse_alloc(options, curp.wlevel, mmsg, |
curp.mchars, defos); |
curp.mchars, defos); |
Line 416 main(int argc, char *argv[]) |
|
Line 429 main(int argc, char *argv[]) |
|
if (OUTT_MAN == curp.outtype) |
if (OUTT_MAN == curp.outtype) |
mparse_keep(curp.mp); |
mparse_keep(curp.mp); |
|
|
if (argc == 0) |
if (argc == 0) { |
|
if (use_pager && isatty(STDOUT_FILENO)) |
|
spawn_pager(); |
parse(&curp, STDIN_FILENO, "<stdin>", &rc); |
parse(&curp, STDIN_FILENO, "<stdin>", &rc); |
|
} |
|
|
while (argc) { |
while (argc) { |
|
rctmp = mparse_open(curp.mp, &fd, |
#if HAVE_SQLITE3 |
#if HAVE_SQLITE3 |
if (resp != NULL) { |
resp != NULL ? resp->file : |
rc = mparse_open(curp.mp, &fd, resp->file); |
#endif |
if (fd == -1) |
*argv); |
/* nothing */; |
if (rc < rctmp) |
|
rc = rctmp; |
|
|
|
if (fd != -1) { |
|
if (use_pager && isatty(STDOUT_FILENO)) |
|
spawn_pager(); |
|
use_pager = 0; |
|
|
|
#if HAVE_SQLITE3 |
|
if (resp == NULL) |
|
#endif |
|
parse(&curp, fd, *argv, &rc); |
|
#if HAVE_SQLITE3 |
else if (resp->form & FORM_SRC) { |
else if (resp->form & FORM_SRC) { |
/* For .so only; ignore failure. */ |
/* For .so only; ignore failure. */ |
chdir(paths.paths[resp->ipath]); |
chdir(paths.paths[resp->ipath]); |
parse(&curp, fd, resp->file, &rc); |
parse(&curp, fd, resp->file, &rc); |
} else |
} else { |
rc = passthrough(resp->file, fd, |
rctmp = passthrough(resp->file, fd, |
synopsis_only); |
synopsis_only); |
resp++; |
if (rc < rctmp) |
} else |
rc = rctmp; |
|
} |
#endif |
#endif |
{ |
|
rc = mparse_open(curp.mp, &fd, *argv++); |
|
if (fd != -1) |
|
parse(&curp, fd, argv[-1], &rc); |
|
} |
|
|
|
if (mparse_wait(curp.mp) != MANDOCLEVEL_OK) |
rctmp = mparse_wait(curp.mp); |
rc = MANDOCLEVEL_SYSERR; |
if (rc < rctmp) |
|
rc = rctmp; |
|
|
|
if (argc > 1 && curp.outtype <= OUTT_UTF8) |
|
ascii_sepline(curp.outdata); |
|
} |
|
|
if (MANDOCLEVEL_OK != rc && curp.wstop) |
if (MANDOCLEVEL_OK != rc && curp.wstop) |
break; |
break; |
argc--; |
|
|
#if HAVE_SQLITE3 |
|
if (resp != NULL) |
|
resp++; |
|
else |
|
#endif |
|
argv++; |
|
if (--argc) |
|
mparse_reset(curp.mp); |
} |
} |
|
|
if (curp.outfree) |
if (curp.outfree) |
Line 487 usage(enum argmode argmode) |
|
Line 525 usage(enum argmode argmode) |
|
"\t [-Toutput] [-Wlevel] [file ...]\n", stderr); |
"\t [-Toutput] [-Wlevel] [file ...]\n", stderr); |
break; |
break; |
case ARG_NAME: |
case ARG_NAME: |
fputs("usage: man [-acfhklVw] [-C file] " |
fputs("usage: man [-acfhklVw] [-C file] [-I os=name] " |
"[-M path] [-m path] [-S arch] [-s section]\n" |
"[-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); |
"\t [section] name ...\n", stderr); |
break; |
break; |
case ARG_WORD: |
case ARG_WORD: |
Line 505 usage(enum argmode argmode) |
|
Line 545 usage(enum argmode argmode) |
|
exit((int)MANDOCLEVEL_BADARG); |
exit((int)MANDOCLEVEL_BADARG); |
} |
} |
|
|
|
#if HAVE_SQLITE3 |
|
static int |
|
fs_lookup(const struct manpaths *paths, size_t ipath, |
|
const char *sec, const char *arch, const char *name, |
|
struct manpage **res, size_t *ressz) |
|
{ |
|
struct manpage *page; |
|
char *file; |
|
int form; |
|
|
|
mandoc_asprintf(&file, "%s/man%s/%s.%s", |
|
paths->paths[ipath], sec, name, sec); |
|
if (access(file, R_OK) != -1) { |
|
form = FORM_SRC; |
|
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); |
|
|
|
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) { |
|
form = FORM_SRC; |
|
goto found; |
|
} |
|
free(file); |
|
} |
|
return(0); |
|
|
|
found: |
|
fprintf(stderr, "%s: outdated mandoc.db lacks %s(%s) entry,\n" |
|
" consider running # makewhatis %s\n", |
|
progname, name, sec, paths->paths[ipath]); |
|
|
|
*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 |
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]); |
|
|
|
size_t ipath, isec, lastsz; |
|
|
|
assert(cfg->argmode == ARG_NAME); |
|
|
|
*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--; |
|
} |
|
} |
|
#endif |
|
|
|
static void |
parse(struct curparse *curp, int fd, const char *file, |
parse(struct curparse *curp, int fd, const char *file, |
enum mandoclevel *level) |
enum mandoclevel *level) |
{ |
{ |
Line 520 parse(struct curparse *curp, int fd, const char *file, |
|
Line 652 parse(struct curparse *curp, int fd, const char *file, |
|
|
|
rc = mparse_readfd(curp->mp, fd, file); |
rc = mparse_readfd(curp->mp, fd, file); |
|
|
/* Stop immediately if the parse has failed. */ |
|
|
|
if (MANDOCLEVEL_FATAL <= rc) |
|
goto cleanup; |
|
|
|
/* |
/* |
* With -Wstop and warnings or errors of at least the requested |
* With -Wstop and warnings or errors of at least the requested |
* level, do not produce output. |
* level, do not produce output. |
Line 610 parse(struct curparse *curp, int fd, const char *file, |
|
Line 737 parse(struct curparse *curp, int fd, const char *file, |
|
if (mdoc && curp->outmdoc) |
if (mdoc && curp->outmdoc) |
(*curp->outmdoc)(curp->outdata, mdoc); |
(*curp->outmdoc)(curp->outdata, mdoc); |
|
|
cleanup: |
cleanup: |
|
|
mparse_reset(curp->mp); |
|
|
|
if (*level < rc) |
if (*level < rc) |
*level = rc; |
*level = rc; |
} |
} |
Line 632 passthrough(const char *file, int fd, int synopsis_onl |
|
Line 756 passthrough(const char *file, int fd, int synopsis_onl |
|
ssize_t nw; |
ssize_t nw; |
int print; |
int print; |
|
|
|
fflush(stdout); |
|
|
if ((stream = fdopen(fd, "r")) == NULL) { |
if ((stream = fdopen(fd, "r")) == NULL) { |
close(fd); |
close(fd); |
syscall = "fdopen"; |
syscall = "fdopen"; |
Line 697 koptions(int *options, char *arg) |
|
Line 823 koptions(int *options, char *arg) |
|
} else if ( ! strcmp(arg, "us-ascii")) { |
} else if ( ! strcmp(arg, "us-ascii")) { |
*options &= ~(MPARSE_UTF8 | MPARSE_LATIN1); |
*options &= ~(MPARSE_UTF8 | MPARSE_LATIN1); |
} else { |
} else { |
fprintf(stderr, "%s: -K%s: Bad argument\n", |
fprintf(stderr, "%s: -K %s: Bad argument\n", |
progname, arg); |
progname, arg); |
return(0); |
return(0); |
} |
} |
Line 717 moptions(int *options, char *arg) |
|
Line 843 moptions(int *options, char *arg) |
|
else if (0 == strcmp(arg, "an")) |
else if (0 == strcmp(arg, "an")) |
*options |= MPARSE_MAN; |
*options |= MPARSE_MAN; |
else { |
else { |
fprintf(stderr, "%s: -m%s: Bad argument\n", |
fprintf(stderr, "%s: -m %s: Bad argument\n", |
progname, arg); |
progname, arg); |
return(0); |
return(0); |
} |
} |
Line 751 toptions(struct curparse *curp, char *arg) |
|
Line 877 toptions(struct curparse *curp, char *arg) |
|
else if (0 == strcmp(arg, "pdf")) |
else if (0 == strcmp(arg, "pdf")) |
curp->outtype = OUTT_PDF; |
curp->outtype = OUTT_PDF; |
else { |
else { |
fprintf(stderr, "%s: -T%s: Bad argument\n", |
fprintf(stderr, "%s: -T %s: Bad argument\n", |
progname, arg); |
progname, arg); |
return(0); |
return(0); |
} |
} |
Line 787 woptions(struct curparse *curp, char *arg) |
|
Line 913 woptions(struct curparse *curp, char *arg) |
|
curp->wlevel = MANDOCLEVEL_ERROR; |
curp->wlevel = MANDOCLEVEL_ERROR; |
break; |
break; |
case 4: |
case 4: |
curp->wlevel = MANDOCLEVEL_FATAL; |
curp->wlevel = MANDOCLEVEL_BADARG; |
break; |
break; |
default: |
default: |
fprintf(stderr, "%s: -W%s: Bad argument\n", |
fprintf(stderr, "%s: -W %s: Bad argument\n", |
progname, o); |
progname, o); |
return(0); |
return(0); |
} |
} |