-/* $Id: main.c,v 1.194 2014/10/25 01:03:52 schwarze Exp $ */
+/* $Id: main.c,v 1.199 2014/11/11 19:04:55 schwarze Exp $ */
/*
* Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2010, 2011, 2012, 2014 Ingo Schwarze <schwarze@openbsd.org>
OUTT_TREE, /* -Ttree */
OUTT_MAN, /* -Tman */
OUTT_HTML, /* -Thtml */
- OUTT_XHTML, /* -Txhtml */
OUTT_LINT, /* -Tlint */
OUTT_PS, /* -Tps */
OUTT_PDF /* -Tpdf */
struct curparse {
struct mparse *mp;
+ struct mchars *mchars; /* character table */
enum mandoclevel wlevel; /* ignore messages below this */
int wstop; /* stop after a file with a warning */
enum outt outtype; /* which output to use */
const char *, int, int, const char *);
static void parse(struct curparse *, int,
const char *, enum mandoclevel *);
-static enum mandoclevel passthrough(const char *, int);
+static enum mandoclevel passthrough(const char *, int, int);
static void spawn_pager(void);
static int toptions(struct curparse *, char *);
static void usage(enum argmode) __attribute__((noreturn));
int fd;
int show_usage;
int use_pager;
+ int synopsis_only;
int options;
int c;
use_pager = 1;
show_usage = 0;
+ synopsis_only = 0;
outmode = OUTMODE_DEF;
while (-1 != (c = getopt(argc, argv,
break;
case 'h':
(void)strlcat(curp.outopts, "synopsis,", BUFSIZ);
+ synopsis_only = 1;
+ use_pager = 0;
outmode = OUTMODE_ALL;
break;
case 'I':
if (argc == 0)
usage(search.argmode);
+ if (search.argmode == ARG_NAME &&
+ outmode == OUTMODE_ONE)
+ search.firstmatch = 1;
+
/* Access the mandoc database. */
manpath_parse(&paths, conf_file, defpaths, auxpaths);
if (use_pager && isatty(STDOUT_FILENO))
spawn_pager();
- curp.mp = mparse_alloc(options, curp.wlevel, mmsg, defos);
+ curp.mchars = mchars_alloc();
+ curp.mp = mparse_alloc(options, curp.wlevel, mmsg,
+ curp.mchars, defos);
/*
* Conditionally start up the lookaside buffer before parsing.
chdir(paths.paths[resp->ipath]);
parse(&curp, fd, resp->file, &rc);
} else
- rc = passthrough(resp->file, fd);
+ rc = passthrough(resp->file, fd,
+ synopsis_only);
resp++;
} else
#endif
if (curp.outfree)
(*curp.outfree)(curp.outdata);
- if (curp.mp)
- mparse_free(curp.mp);
+ mparse_free(curp.mp);
+ mchars_free(curp.mchars);
#if HAVE_SQLITE3
out:
switch (argmode) {
case ARG_FILE:
fputs("usage: mandoc [-acfhklV] [-Ios=name] "
- "[-mformat] [-Ooption] [-Toutput] [-Wlevel]\n"
- "\t [file ...]\n", stderr);
+ "[-Kencoding] [-mformat] [-Ooption]\n"
+ "\t [-Toutput] [-Wlevel] [file ...]\n", stderr);
break;
case ARG_NAME:
fputs("usage: man [-acfhklVw] [-C file] "
if ( ! (curp->outman && curp->outmdoc)) {
switch (curp->outtype) {
- case OUTT_XHTML:
- curp->outdata = xhtml_alloc(curp->outopts);
- curp->outfree = html_free;
- break;
case OUTT_HTML:
- curp->outdata = html_alloc(curp->outopts);
+ curp->outdata = html_alloc(curp->mchars,
+ curp->outopts);
curp->outfree = html_free;
break;
case OUTT_UTF8:
- curp->outdata = utf8_alloc(curp->outopts);
+ curp->outdata = utf8_alloc(curp->mchars,
+ curp->outopts);
curp->outfree = ascii_free;
break;
case OUTT_LOCALE:
- curp->outdata = locale_alloc(curp->outopts);
+ curp->outdata = locale_alloc(curp->mchars,
+ curp->outopts);
curp->outfree = ascii_free;
break;
case OUTT_ASCII:
- curp->outdata = ascii_alloc(curp->outopts);
+ curp->outdata = ascii_alloc(curp->mchars,
+ curp->outopts);
curp->outfree = ascii_free;
break;
case OUTT_PDF:
- curp->outdata = pdf_alloc(curp->outopts);
+ curp->outdata = pdf_alloc(curp->mchars,
+ curp->outopts);
curp->outfree = pspdf_free;
break;
case OUTT_PS:
- curp->outdata = ps_alloc(curp->outopts);
+ curp->outdata = ps_alloc(curp->mchars,
+ curp->outopts);
curp->outfree = pspdf_free;
break;
default:
switch (curp->outtype) {
case OUTT_HTML:
- /* FALLTHROUGH */
- case OUTT_XHTML:
curp->outman = html_man;
curp->outmdoc = html_mdoc;
break;
}
static enum mandoclevel
-passthrough(const char *file, int fd)
+passthrough(const char *file, int fd, int synopsis_only)
{
- char buf[BUFSIZ];
+ const char synb[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
+ const char synr[] = "SYNOPSIS";
+
+ FILE *stream;
const char *syscall;
- ssize_t nr, nw, off;
+ char *line;
+ size_t len, off;
+ ssize_t nw;
+ int print;
+
+ if ((stream = fdopen(fd, "r")) == NULL) {
+ close(fd);
+ syscall = "fdopen";
+ goto fail;
+ }
- while ((nr = read(fd, buf, BUFSIZ)) != -1 && nr != 0)
- for (off = 0; off < nr; off += nw)
- if ((nw = write(STDOUT_FILENO, buf + off,
- (size_t)(nr - off))) == -1 || nw == 0) {
- close(fd);
+ 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;
+ }
+ }
+ 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;
}
+ }
- close(fd);
+ if (ferror(stream)) {
+ fclose(stream);
+ syscall = "fgetln";
+ goto fail;
+ }
- if (nr == 0)
- return(MANDOCLEVEL_OK);
+done:
+ fclose(stream);
+ return(MANDOCLEVEL_OK);
- syscall = "read";
fail:
fprintf(stderr, "%s: %s: SYSERR: %s: %s",
progname, file, syscall, strerror(errno));
else if (0 == strcmp(arg, "locale"))
curp->outtype = OUTT_LOCALE;
else if (0 == strcmp(arg, "xhtml"))
- curp->outtype = OUTT_XHTML;
+ curp->outtype = OUTT_HTML;
else if (0 == strcmp(arg, "ps"))
curp->outtype = OUTT_PS;
else if (0 == strcmp(arg, "pdf"))