version 1.42, 2009/03/14 12:35:02 |
version 1.49, 2009/03/19 11:49:00 |
|
|
/* $Id$ */ |
/* $Id$ */ |
/* |
/* |
* Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se> |
* Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.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 |
|
|
* PERFORMANCE OF THIS SOFTWARE. |
* PERFORMANCE OF THIS SOFTWARE. |
*/ |
*/ |
#include <sys/types.h> |
#include <sys/types.h> |
|
#include <sys/stat.h> |
|
|
#include <assert.h> |
#include <assert.h> |
#include <ctype.h> |
|
#include <err.h> |
#include <err.h> |
|
#include <fcntl.h> |
#include <getopt.h> |
#include <getopt.h> |
#include <stdio.h> |
#include <stdio.h> |
#include <stdlib.h> |
#include <stdlib.h> |
#include <string.h> |
#include <string.h> |
|
#include <unistd.h> |
|
|
#include "mmain.h" |
|
#include "term.h" |
#include "term.h" |
|
|
struct termseq { |
#define WARN_WALL 0x03 /* All-warnings mask. */ |
const char *enc; |
#define WARN_WCOMPAT (1 << 0) /* Compatibility warnings. */ |
int sym; |
#define WARN_WSYNTAX (1 << 1) /* Syntax warnings. */ |
|
#define WARN_WERR (1 << 2) /* Warnings->errors. */ |
|
|
|
enum termt { |
|
TERMT_ASCII, |
|
TERMT_LINT, |
|
TERMT_TREE |
}; |
}; |
|
|
static int option(void *, int, const char *); |
extern char *__progname; |
static void body(struct termp *, |
|
struct termpair *, |
__dead static void version(void); |
|
__dead static void usage(void); |
|
#if 0 |
|
__dead static void punt(struct cmdargs *, char **); |
|
#endif |
|
static int foptions(int *, char *); |
|
static int toptions(enum termt *, 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 void body(struct termp *, struct termpair *, |
const struct mdoc_meta *, |
const struct mdoc_meta *, |
const struct mdoc_node *); |
const struct mdoc_node *); |
static void header(struct termp *, |
static void header(struct termp *, |
const struct mdoc_meta *); |
const struct mdoc_meta *); |
static void footer(struct termp *, |
static void footer(struct termp *, |
const struct mdoc_meta *); |
const struct mdoc_meta *); |
|
static int file(char **, size_t *, char **, size_t *, |
static void pword(struct termp *, const char *, size_t); |
const char *, struct mdoc *); |
static void pescape(struct termp *, const char *, |
static int fdesc(char **, size_t *, char **, size_t *, |
size_t *, size_t); |
const char *, int, struct mdoc *); |
|
static void pword(struct termp *, const char *, int); |
|
static void pescape(struct termp *, |
|
const char *, int *, int); |
static void nescape(struct termp *, |
static void nescape(struct termp *, |
const char *, size_t); |
const char *, size_t); |
static void chara(struct termp *, char); |
static void chara(struct termp *, char); |
static void stringa(struct termp *, |
static void stringa(struct termp *, |
const char *, size_t); |
const char *, size_t); |
static void symbola(struct termp *, enum tsym); |
|
static void sanity(const struct mdoc_node *); |
static void sanity(const struct mdoc_node *); |
|
|
#ifdef __linux__ |
|
extern size_t strlcat(char *, const char *, size_t); |
|
extern size_t strlcpy(char *, const char *, size_t); |
|
#endif |
|
|
|
static struct termseq termenc1[] = { |
int |
{ "\\", TERMSYM_SLASH }, |
main(int argc, char *argv[]) |
{ "\'", TERMSYM_RSQUOTE }, |
{ |
{ "`", TERMSYM_LSQUOTE }, |
struct termp termp; |
{ "-", TERMSYM_HYPHEN }, |
int c, fflags, wflags; |
{ " ", TERMSYM_SPACE }, |
struct mdoc_cb cb; |
{ ".", TERMSYM_PERIOD }, |
struct mdoc *mdoc; |
{ "&", TERMSYM_BREAK }, |
char *buf, *line; |
{ "e", TERMSYM_SLASH }, |
size_t bufsz, linesz; |
{ "q", TERMSYM_DQUOTE }, |
enum termt termt; |
{ "|", TERMSYM_BREAK }, |
|
{ NULL, 0 } |
|
}; |
|
|
|
static struct termseq termenc2[] = { |
bzero(&termp, sizeof(struct termp)); |
{ "rC", TERMSYM_RBRACE }, |
bzero(&cb, sizeof(struct mdoc_cb)); |
{ "lC", TERMSYM_LBRACE }, |
|
{ "rB", TERMSYM_RBRACK }, |
|
{ "lB", TERMSYM_LBRACK }, |
|
{ "ra", TERMSYM_RANGLE }, |
|
{ "la", TERMSYM_LANGLE }, |
|
{ "Lq", TERMSYM_LDQUOTE }, |
|
{ "lq", TERMSYM_LDQUOTE }, |
|
{ "Rq", TERMSYM_RDQUOTE }, |
|
{ "rq", TERMSYM_RDQUOTE }, |
|
{ "oq", TERMSYM_LSQUOTE }, |
|
{ "aq", TERMSYM_RSQUOTE }, |
|
|
|
{ "<-", TERMSYM_LARROW }, |
termt = TERMT_ASCII; |
{ "->", TERMSYM_RARROW }, |
fflags = wflags = 0; |
{ "ua", TERMSYM_UARROW }, |
|
{ "da", TERMSYM_DARROW }, |
|
|
|
{ "bu", TERMSYM_BULLET }, |
/* LINTED */ |
{ "Ba", TERMSYM_BAR }, |
while (-1 != (c = getopt(argc, argv, "f:VW:T:"))) |
{ "ba", TERMSYM_BAR }, |
switch (c) { |
{ "co", TERMSYM_COPY }, |
case ('f'): |
{ "Am", TERMSYM_AMP }, |
if ( ! foptions(&fflags, optarg)) |
|
return(0); |
|
break; |
|
case ('T'): |
|
if ( ! toptions(&termt, optarg)) |
|
return(0); |
|
break; |
|
case ('W'): |
|
if ( ! woptions(&wflags, optarg)) |
|
return(0); |
|
break; |
|
case ('V'): |
|
version(); |
|
/* NOTREACHED */ |
|
default: |
|
usage(); |
|
/* NOTREACHED */ |
|
} |
|
|
{ "Le", TERMSYM_LE }, |
argc -= optind; |
{ "<=", TERMSYM_LE }, |
argv += optind; |
{ "Ge", TERMSYM_GE }, |
|
{ ">=", TERMSYM_GE }, |
|
{ "==", TERMSYM_EQ }, |
|
{ "Ne", TERMSYM_NEQ }, |
|
{ "!=", TERMSYM_NEQ }, |
|
{ "Pm", TERMSYM_PLUSMINUS }, |
|
{ "+-", TERMSYM_PLUSMINUS }, |
|
{ "If", TERMSYM_INF2 }, |
|
{ "if", TERMSYM_INF }, |
|
{ "Na", TERMSYM_NAN }, |
|
{ "na", TERMSYM_NAN }, |
|
{ "**", TERMSYM_ASTERISK }, |
|
{ "Gt", TERMSYM_GT }, |
|
{ "Lt", TERMSYM_LT }, |
|
|
|
{ "aa", TERMSYM_ACUTE }, |
termp.maxrmargin = 78; /* FIXME */ |
{ "ga", TERMSYM_GRAVE }, |
|
|
|
{ "en", TERMSYM_EN }, |
cb.mdoc_err = merr; |
{ "em", TERMSYM_EM }, |
cb.mdoc_warn = mwarn; |
|
|
{ "Pi", TERMSYM_PI }, |
/* Line and block buffers persist between parses. */ |
{ NULL, 0 } |
|
}; |
|
|
|
static struct termsym termsym_ascii[TERMSYM_MAX] = { |
buf = line = NULL; |
{ "]", 1 }, /* TERMSYM_RBRACK */ |
bufsz = linesz = 0; |
{ "[", 1 }, /* TERMSYM_LBRACK */ |
|
{ "<-", 2 }, /* TERMSYM_LARROW */ |
|
{ "->", 2 }, /* TERMSYM_RARROW */ |
|
{ "^", 1 }, /* TERMSYM_UARROW */ |
|
{ "v", 1 }, /* TERMSYM_DARROW */ |
|
{ "`", 1 }, /* TERMSYM_LSQUOTE */ |
|
{ "\'", 1 }, /* TERMSYM_RSQUOTE */ |
|
{ "\'", 1 }, /* TERMSYM_SQUOTE */ |
|
{ "``", 2 }, /* TERMSYM_LDQUOTE */ |
|
{ "\'\'", 2 }, /* TERMSYM_RDQUOTE */ |
|
{ "\"", 1 }, /* TERMSYM_DQUOTE */ |
|
{ "<", 1 }, /* TERMSYM_LT */ |
|
{ ">", 1 }, /* TERMSYM_GT */ |
|
{ "<=", 2 }, /* TERMSYM_LE */ |
|
{ ">=", 2 }, /* TERMSYM_GE */ |
|
{ "==", 2 }, /* TERMSYM_EQ */ |
|
{ "!=", 2 }, /* TERMSYM_NEQ */ |
|
{ "\'", 1 }, /* TERMSYM_ACUTE */ |
|
{ "`", 1 }, /* TERMSYM_GRAVE */ |
|
{ "pi", 2 }, /* TERMSYM_PI */ |
|
{ "+=", 2 }, /* TERMSYM_PLUSMINUS */ |
|
{ "oo", 2 }, /* TERMSYM_INF */ |
|
{ "infinity", 8 }, /* TERMSYM_INF2 */ |
|
{ "NaN", 3 }, /* TERMSYM_NAN */ |
|
{ "|", 1 }, /* TERMSYM_BAR */ |
|
{ "o", 1 }, /* TERMSYM_BULLET */ |
|
{ "&", 1 }, /* TERMSYM_AMP */ |
|
{ "--", 2 }, /* TERMSYM_EM */ |
|
{ "-", 1 }, /* TERMSYM_EN */ |
|
{ "(C)", 3 }, /* TERMSYM_COPY */ |
|
{ "*", 1 }, /* TERMSYM_ASTERISK */ |
|
{ "\\", 1 }, /* TERMSYM_SLASH */ |
|
{ "-", 1 }, /* TERMSYM_HYPHEN */ |
|
{ " ", 1 }, /* TERMSYM_SPACE */ |
|
{ ".", 1 }, /* TERMSYM_PERIOD */ |
|
{ "", 0 }, /* TERMSYM_BREAK */ |
|
{ "<", 1 }, /* TERMSYM_LANGLE */ |
|
{ ">", 1 }, /* TERMSYM_RANGLE */ |
|
{ "{", 1 }, /* TERMSYM_LBRACE */ |
|
{ "}", 1 }, /* TERMSYM_RBRACE */ |
|
}; |
|
|
|
|
/* Overall mdoc persists between parses. */ |
|
|
int |
mdoc = mdoc_alloc(&wflags, fflags, &cb); |
main(int argc, char *argv[]) |
|
{ |
|
struct mmain *p; |
|
int c; |
|
const struct mdoc *mdoc; |
|
struct termp termp; |
|
|
|
(void)memset(&termp, 0, sizeof(struct termp)); |
while (*argv) { |
|
if ( ! file(&line, &linesz, &buf, &bufsz, *argv, mdoc)) |
|
break; |
|
|
termp.maxrmargin = termp.rmargin = 78; /* XXX */ |
switch (termt) { |
termp.maxcols = 1024; |
case (TERMT_ASCII): |
termp.offset = termp.col = 0; |
if (NULL == termp.symtab) |
termp.flags = TERMP_NOSPACE; |
termp.symtab = ascii2htab(); |
termp.symtab = termsym_ascii; |
header(&termp, mdoc_meta(mdoc)); |
termp.enc = TERMENC_ANSI; |
body(&termp, NULL, mdoc_meta(mdoc), |
|
mdoc_node(mdoc)); |
|
footer(&termp, mdoc_meta(mdoc)); |
|
break; |
|
default: |
|
break; |
|
} |
|
|
p = mmain_alloc(); |
mdoc_reset(mdoc); |
c = mmain_getopt(p, argc, argv, "[-Ooption...]", |
argv++; |
"O:", &termp, option); |
} |
|
|
if (1 != c) |
if (buf) |
mmain_exit(p, -1 == c ? 1 : 0); |
free(buf); |
|
if (line) |
|
free(line); |
|
if (termp.buf) |
|
free(termp.buf); |
|
if (termp.symtab) |
|
asciifree(termp.symtab); |
|
|
if (NULL == (mdoc = mmain_mdoc(p))) |
mdoc_free(mdoc); |
mmain_exit(p, 1); |
|
|
|
if (NULL == (termp.buf = malloc(termp.maxcols))) |
return(0); |
err(1, "malloc"); |
} |
|
|
header(&termp, mdoc_meta(mdoc)); |
|
body(&termp, NULL, mdoc_meta(mdoc), mdoc_node(mdoc)); |
|
footer(&termp, mdoc_meta(mdoc)); |
|
|
|
free(termp.buf); |
__dead static void |
|
version(void) |
|
{ |
|
|
mmain_exit(p, 0); |
(void)printf("%s %s\n", __progname, VERSION); |
|
exit(0); |
/* NOTREACHED */ |
/* NOTREACHED */ |
} |
} |
|
|
|
|
int |
__dead static void |
option(void *ptr, int c, const char *arg) |
usage(void) |
{ |
{ |
struct termp *p; |
|
|
|
p = (struct termp *)ptr; |
(void)fprintf(stderr, "usage: %s\n", __progname); |
|
exit(1); |
|
/* NOTREACHED */ |
|
} |
|
|
if (0 == strcmp(arg, "nroff")) { |
|
p->enc = TERMENC_NROFF; |
static int |
return(1); |
file(char **ln, size_t *lnsz, char **buf, size_t *bufsz, |
} else if (0 == strcmp(arg, "ansi")) { |
const char *file, struct mdoc *mdoc) |
p->enc = TERMENC_ANSI; |
{ |
return(1); |
int fd, c; |
|
|
|
if (-1 == (fd = open(file, O_RDONLY, 0))) { |
|
warn("%s", file); |
|
return(0); |
} |
} |
|
|
warnx("unknown option: -O%s", arg); |
c = fdesc(ln, lnsz, buf, bufsz, file, fd, mdoc); |
return(0); |
|
|
if (-1 == close(fd)) |
|
warn("%s", file); |
|
|
|
return(c); |
} |
} |
|
|
|
|
|
static int |
|
fdesc(char **lnp, size_t *lnsz, char **bufp, size_t *bufsz, |
|
const char *f, int fd, struct mdoc *mdoc) |
|
{ |
|
size_t sz; |
|
ssize_t ssz; |
|
struct stat st; |
|
int j, i, pos, lnn; |
|
char *ln, *buf; |
|
|
|
buf = *bufp; |
|
ln = *lnp; |
|
|
|
/* |
|
* Two buffers: ln and buf. buf is the input buffer, optimised |
|
* for each file's block size. ln is a line buffer. Both |
|
* growable, hence passed in by ptr-ptr. |
|
*/ |
|
|
|
if (-1 == fstat(fd, &st)) { |
|
warnx("%s", f); |
|
sz = BUFSIZ; |
|
} else |
|
sz = (unsigned)BUFSIZ > st.st_blksize ? |
|
(size_t)BUFSIZ : st.st_blksize; |
|
|
|
if (sz > *bufsz) { |
|
if (NULL == (buf = realloc(buf, sz))) |
|
err(1, "realloc"); |
|
*bufp = buf; |
|
*bufsz = sz; |
|
} |
|
|
|
/* |
|
* Fill buf with file blocksize and parse newlines into ln. |
|
*/ |
|
|
|
for (lnn = 1, pos = 0; ; ) { |
|
if (-1 == (ssz = read(fd, buf, sz))) { |
|
warn("%s", f); |
|
return(0); |
|
} else if (0 == ssz) |
|
break; |
|
|
|
for (i = 0; i < (int)ssz; i++) { |
|
if (pos >= (int)*lnsz) { |
|
*lnsz += 256; /* Step-size. */ |
|
ln = realloc(ln, *lnsz); |
|
if (NULL == ln) |
|
err(1, "realloc"); |
|
*lnp = ln; |
|
} |
|
|
|
if ('\n' != buf[i]) { |
|
ln[pos++] = buf[i]; |
|
continue; |
|
} |
|
|
|
/* Check for CPP-escaped newline. */ |
|
|
|
if (pos > 0 && '\\' == ln[pos - 1]) { |
|
for (j = pos - 1; j >= 0; j--) |
|
if ('\\' != ln[j]) |
|
break; |
|
|
|
if ( ! ((pos - j) % 2)) { |
|
pos--; |
|
lnn++; |
|
continue; |
|
} |
|
} |
|
|
|
ln[pos] = 0; |
|
if ( ! mdoc_parseln(mdoc, lnn, ln)) |
|
return(0); |
|
lnn++; |
|
pos = 0; |
|
} |
|
} |
|
|
|
return(mdoc_endparse(mdoc)); |
|
} |
|
|
|
|
|
static int |
|
toptions(enum termt *tflags, char *arg) |
|
{ |
|
|
|
if (0 == strcmp(arg, "ascii")) |
|
*tflags = TERMT_ASCII; |
|
else if (0 == strcmp(arg, "lint")) |
|
*tflags = TERMT_LINT; |
|
else if (0 == strcmp(arg, "tree")) |
|
*tflags = TERMT_TREE; |
|
else { |
|
warnx("bad argument: -T%s", arg); |
|
return(0); |
|
} |
|
|
|
return(1); |
|
} |
|
|
|
|
/* |
/* |
|
* Parse out the options for [-fopt...] setting compiler options. These |
|
* can be comma-delimited or called again. |
|
*/ |
|
static int |
|
foptions(int *fflags, char *arg) |
|
{ |
|
char *v; |
|
char *toks[] = { "ign-scope", "ign-escape", |
|
"ign-macro", NULL }; |
|
|
|
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); |
|
} |
|
|
|
|
|
/* |
|
* Parse out the options for [-Werr...], which sets warning modes. |
|
* These can be comma-delimited or called again. XXX - should this be |
|
* using -w like troff? |
|
*/ |
|
static int |
|
woptions(int *wflags, char *arg) |
|
{ |
|
char *v; |
|
char *toks[] = { "all", "compat", |
|
"syntax", "error", NULL }; |
|
|
|
while (*arg) |
|
switch (getsubopt(&arg, toks, &v)) { |
|
case (0): |
|
*wflags |= WARN_WALL; |
|
break; |
|
case (1): |
|
*wflags |= WARN_WCOMPAT; |
|
break; |
|
case (2): |
|
*wflags |= WARN_WSYNTAX; |
|
break; |
|
case (3): |
|
*wflags |= WARN_WERR; |
|
break; |
|
default: |
|
warnx("bad argument: -W%s", arg); |
|
return(0); |
|
} |
|
|
|
return(1); |
|
} |
|
|
|
|
|
/* |
* Flush a line of text. A "line" is loosely defined as being something |
* Flush a line of text. A "line" is loosely defined as being something |
* that should be followed by a newline, regardless of whether it's |
* that should be followed by a newline, regardless of whether it's |
* broken apart by newlines getting there. A line can also be a |
* broken apart by newlines getting there. A line can also be a |
Line 252 option(void *ptr, int c, const char *arg) |
|
Line 398 option(void *ptr, int c, const char *arg) |
|
* columns. In short: don't print a newline and instead pad to the |
* columns. In short: don't print a newline and instead pad to the |
* right margin. Used in conjunction with TERMP_NOLPAD. |
* right margin. Used in conjunction with TERMP_NOLPAD. |
* |
* |
|
* - TERMP_NONOBREAK: don't newline when TERMP_NOBREAK is specified. |
|
* |
* In-line line breaking: |
* In-line line breaking: |
* |
* |
* If TERMP_NOBREAK is specified and the line overruns the right |
* If TERMP_NOBREAK is specified and the line overruns the right |
Line 267 option(void *ptr, int c, const char *arg) |
|
Line 415 option(void *ptr, int c, const char *arg) |
|
void |
void |
flushln(struct termp *p) |
flushln(struct termp *p) |
{ |
{ |
size_t i, j, vsz, vis, maxvis, mmax, bp; |
int i, j; |
|
size_t vsz, vis, maxvis, mmax, bp; |
|
|
/* |
/* |
* First, establish the maximum columns of "visible" content. |
* First, establish the maximum columns of "visible" content. |
Line 290 flushln(struct termp *p) |
|
Line 439 flushln(struct termp *p) |
|
|
|
if ( ! (p->flags & TERMP_NOLPAD)) |
if ( ! (p->flags & TERMP_NOLPAD)) |
/* LINTED */ |
/* LINTED */ |
for (j = 0; j < p->offset; j++) |
for (j = 0; j < (int)p->offset; j++) |
putchar(' '); |
putchar(' '); |
|
|
for (i = 0; i < p->col; i++) { |
for (i = 0; i < (int)p->col; i++) { |
/* |
/* |
* Count up visible word characters. Control sequences |
* Count up visible word characters. Control sequences |
* (starting with the CSI) aren't counted. A space |
* (starting with the CSI) aren't counted. A space |
Line 302 flushln(struct termp *p) |
|
Line 451 flushln(struct termp *p) |
|
*/ |
*/ |
|
|
/* LINTED */ |
/* LINTED */ |
for (j = i, vsz = 0; j < p->col; j++) { |
for (j = i, vsz = 0; j < (int)p->col; j++) { |
if (isspace((u_char)p->buf[j])) { |
if (' ' == p->buf[j]) |
break; |
break; |
} else if (27 == p->buf[j]) { |
else if (8 == p->buf[j]) |
assert(TERMENC_ANSI == p->enc); |
|
assert(j + 5 <= p->col); |
|
j += 4; |
|
} else if (8 == p->buf[j]) { |
|
assert(TERMENC_NROFF == p->enc); |
|
assert(j + 2 <= p->col); |
|
j += 1; |
j += 1; |
} else |
else |
vsz++; |
vsz++; |
} |
} |
|
|
Line 328 flushln(struct termp *p) |
|
Line 471 flushln(struct termp *p) |
|
if ( ! (TERMP_NOBREAK & p->flags)) { |
if ( ! (TERMP_NOBREAK & p->flags)) { |
if (vis && vis + vsz > bp) { |
if (vis && vis + vsz > bp) { |
putchar('\n'); |
putchar('\n'); |
for (j = 0; j < p->offset; j++) |
for (j = 0; j < (int)p->offset; j++) |
putchar(' '); |
putchar(' '); |
vis = 0; |
vis = 0; |
} else if (vis + vsz > bp) |
} else if (vis + vsz > bp) |
Line 339 flushln(struct termp *p) |
|
Line 482 flushln(struct termp *p) |
|
} else { |
} else { |
if (vis && vis + vsz > bp) { |
if (vis && vis + vsz > bp) { |
putchar('\n'); |
putchar('\n'); |
for (j = 0; j < p->rmargin; j++) |
for (j = 0; j < (int)p->rmargin; j++) |
putchar(' '); |
putchar(' '); |
vis = p->rmargin - p->offset; |
vis = p->rmargin - p->offset; |
} else if (vis + vsz > bp) |
} else if (vis + vsz > bp) |
Line 354 flushln(struct termp *p) |
|
Line 497 flushln(struct termp *p) |
|
* our breakpoint. |
* our breakpoint. |
*/ |
*/ |
|
|
for ( ; i < p->col; i++) { |
for ( ; i < (int)p->col; i++) { |
if (isspace((u_char)p->buf[i])) |
if (' ' == p->buf[i]) |
break; |
break; |
putchar(p->buf[i]); |
putchar(p->buf[i]); |
} |
} |
vis += vsz; |
vis += vsz; |
if (i < p->col && vis <= bp) { |
if (i < (int)p->col && vis <= bp) { |
putchar(' '); |
putchar(' '); |
vis++; |
vis++; |
} |
} |
Line 374 flushln(struct termp *p) |
|
Line 517 flushln(struct termp *p) |
|
if ((TERMP_NOBREAK & p->flags) && vis >= maxvis) { |
if ((TERMP_NOBREAK & p->flags) && vis >= maxvis) { |
if ( ! (TERMP_NONOBREAK & p->flags)) { |
if ( ! (TERMP_NONOBREAK & p->flags)) { |
putchar('\n'); |
putchar('\n'); |
for (i = 0; i < p->rmargin; i++) |
for (i = 0; i < (int)p->rmargin; i++) |
putchar(' '); |
putchar(' '); |
} |
} |
p->col = 0; |
p->col = 0; |
Line 440 vspace(struct termp *p) |
|
Line 583 vspace(struct termp *p) |
|
void |
void |
word(struct termp *p, const char *word) |
word(struct termp *p, const char *word) |
{ |
{ |
size_t i, j, len; |
int i, j, len; |
|
|
if (p->flags & TERMP_LITERAL) { |
if (p->flags & TERMP_LITERAL) { |
pword(p, word, strlen(word)); |
pword(p, word, (int)strlen(word)); |
return; |
return; |
} |
} |
|
|
if (0 == (len = strlen(word))) |
if (0 == (len = (int)strlen(word))) |
errx(1, "blank line not in literal context"); |
errx(1, "blank line not in literal context"); |
|
|
if (mdoc_isdelim(word)) { |
if (mdoc_isdelim(word)) { |
Line 458 word(struct termp *p, const char *word) |
|
Line 601 word(struct termp *p, const char *word) |
|
|
|
/* LINTED */ |
/* LINTED */ |
for (j = i = 0; i < len; i++) { |
for (j = i = 0; i < len; i++) { |
if ( ! isspace((u_char)word[i])) { |
if (' ' != word[i]) { |
j++; |
j++; |
continue; |
continue; |
} |
} |
|
|
/* Escaped spaces don't delimit... */ |
/* Escaped spaces don't delimit... */ |
if (i > 0 && isspace((u_char)word[i]) && |
if (i && ' ' == word[i] && '\\' == word[i - 1]) { |
'\\' == word[i - 1]) { |
|
j++; |
j++; |
continue; |
continue; |
} |
} |
Line 595 footer(struct termp *p, const struct mdoc_meta *meta) |
|
Line 737 footer(struct termp *p, const struct mdoc_meta *meta) |
|
static void |
static void |
header(struct termp *p, const struct mdoc_meta *meta) |
header(struct termp *p, const struct mdoc_meta *meta) |
{ |
{ |
char *buf, *title, *bufp; |
char *buf, *title; |
|
|
p->rmargin = p->maxrmargin; |
p->rmargin = p->maxrmargin; |
p->offset = 0; |
p->offset = 0; |
Line 630 header(struct termp *p, const struct mdoc_meta *meta) |
|
Line 772 header(struct termp *p, const struct mdoc_meta *meta) |
|
(void)snprintf(title, p->rmargin, "%s(%d)", |
(void)snprintf(title, p->rmargin, "%s(%d)", |
meta->title, meta->msec); |
meta->title, meta->msec); |
|
|
for (bufp = title; *bufp; bufp++) |
|
*bufp = toupper((u_char)*bufp); |
|
|
|
p->offset = 0; |
p->offset = 0; |
p->rmargin = (p->maxrmargin - strlen(buf)) / 2; |
p->rmargin = (p->maxrmargin - strlen(buf)) / 2; |
p->flags |= TERMP_NOBREAK | TERMP_NOSPACE; |
p->flags |= TERMP_NOBREAK | TERMP_NOSPACE; |
Line 672 header(struct termp *p, const struct mdoc_meta *meta) |
|
Line 811 header(struct termp *p, const struct mdoc_meta *meta) |
|
static void |
static void |
nescape(struct termp *p, const char *word, size_t len) |
nescape(struct termp *p, const char *word, size_t len) |
{ |
{ |
struct termseq *enc; |
const char *rhs; |
|
size_t sz; |
|
|
switch (len) { |
if (NULL == (rhs = a2ascii(p->symtab, word, len, &sz))) |
case (1): |
|
enc = termenc1; |
|
break; |
|
case (2): |
|
enc = termenc2; |
|
break; |
|
default: |
|
warnx("unsupported %zu-byte escape sequence", len); |
|
return; |
return; |
} |
stringa(p, rhs, sz); |
|
|
for ( ; enc->enc; enc++) |
|
if (0 == memcmp(enc->enc, word, len)) { |
|
symbola(p, enc->sym); |
|
return; |
|
} |
|
|
|
warnx("unsupported %zu-byte escape sequence", len); |
|
} |
} |
|
|
|
|
Line 702 nescape(struct termp *p, const char *word, size_t len) |
|
Line 826 nescape(struct termp *p, const char *word, size_t len) |
|
* the escape sequence (we assert upon badly-formed escape sequences). |
* the escape sequence (we assert upon badly-formed escape sequences). |
*/ |
*/ |
static void |
static void |
pescape(struct termp *p, const char *word, size_t *i, size_t len) |
pescape(struct termp *p, const char *word, int *i, int len) |
{ |
{ |
size_t j; |
int j; |
|
|
if (++(*i) >= len) { |
if (++(*i) >= len) |
warnx("ignoring bad escape sequence"); |
|
return; |
return; |
} |
|
|
|
if ('(' == word[*i]) { |
if ('(' == word[*i]) { |
(*i)++; |
(*i)++; |
if (*i + 1 >= len) { |
if (*i + 1 >= len) |
warnx("ignoring bad escape sequence"); |
|
return; |
return; |
} |
|
nescape(p, &word[*i], 2); |
nescape(p, &word[*i], 2); |
(*i)++; |
(*i)++; |
return; |
return; |
|
|
} else if ('*' == word[*i]) { |
} else if ('*' == word[*i]) { |
(*i)++; |
(*i)++; |
if (*i >= len) { |
if (*i >= len) |
warnx("ignoring bad escape sequence"); |
|
return; |
return; |
} |
|
switch (word[*i]) { |
switch (word[*i]) { |
case ('('): |
case ('('): |
(*i)++; |
(*i)++; |
if (*i + 1 >= len) { |
if (*i + 1 >= len) |
warnx("ignoring bad escape sequence"); |
|
return; |
return; |
} |
|
nescape(p, &word[*i], 2); |
nescape(p, &word[*i], 2); |
(*i)++; |
(*i)++; |
return; |
return; |
Line 753 pescape(struct termp *p, const char *word, size_t *i, |
|
Line 872 pescape(struct termp *p, const char *word, size_t *i, |
|
for (j = 0; word[*i] && ']' != word[*i]; (*i)++, j++) |
for (j = 0; word[*i] && ']' != word[*i]; (*i)++, j++) |
/* Loop... */ ; |
/* Loop... */ ; |
|
|
if (0 == word[*i]) { |
if (0 == word[*i]) |
warnx("ignoring bad escape sequence"); |
|
return; |
return; |
} |
|
nescape(p, &word[*i - j], j); |
nescape(p, &word[*i - j], (size_t)j); |
} |
} |
|
|
|
|
Line 767 pescape(struct termp *p, const char *word, size_t *i, |
|
Line 885 pescape(struct termp *p, const char *word, size_t *i, |
|
* handles word styling. |
* handles word styling. |
*/ |
*/ |
static void |
static void |
pword(struct termp *p, const char *word, size_t len) |
pword(struct termp *p, const char *word, int len) |
{ |
{ |
size_t i; |
int i; |
|
|
if ( ! (TERMP_NOSPACE & p->flags) && |
if ( ! (TERMP_NOSPACE & p->flags) && |
! (TERMP_LITERAL & p->flags)) |
! (TERMP_LITERAL & p->flags)) |
Line 783 pword(struct termp *p, const char *word, size_t len) |
|
Line 901 pword(struct termp *p, const char *word, size_t len) |
|
* before the word. |
* before the word. |
*/ |
*/ |
|
|
if (TERMENC_ANSI == p->enc && TERMP_STYLE & p->flags) { |
|
if (TERMP_BOLD & p->flags) { |
|
chara(p, 27); |
|
stringa(p, "[01m", 4); |
|
} |
|
if (TERMP_UNDER & p->flags) { |
|
chara(p, 27); |
|
stringa(p, "[04m", 4); |
|
} |
|
if (TERMP_RED & p->flags) { |
|
chara(p, 27); |
|
stringa(p, "[31m", 4); |
|
} |
|
if (TERMP_GREEN & p->flags) { |
|
chara(p, 27); |
|
stringa(p, "[32m", 4); |
|
} |
|
if (TERMP_YELLOW & p->flags) { |
|
chara(p, 27); |
|
stringa(p, "[33m", 4); |
|
} |
|
if (TERMP_BLUE & p->flags) { |
|
chara(p, 27); |
|
stringa(p, "[34m", 4); |
|
} |
|
if (TERMP_MAGENTA & p->flags) { |
|
chara(p, 27); |
|
stringa(p, "[35m", 4); |
|
} |
|
if (TERMP_CYAN & p->flags) { |
|
chara(p, 27); |
|
stringa(p, "[36m", 4); |
|
} |
|
} |
|
|
|
for (i = 0; i < len; i++) { |
for (i = 0; i < len; i++) { |
if ('\\' == word[i]) { |
if ('\\' == word[i]) { |
pescape(p, word, &i, len); |
pescape(p, word, &i, len); |
continue; |
continue; |
} |
} |
|
|
if (TERMENC_NROFF == p->enc && |
if (TERMP_STYLE & p->flags) { |
TERMP_STYLE & p->flags) { |
|
if (TERMP_BOLD & p->flags) { |
if (TERMP_BOLD & p->flags) { |
chara(p, word[i]); |
chara(p, word[i]); |
chara(p, 8); |
chara(p, 8); |
Line 838 pword(struct termp *p, const char *word, size_t len) |
|
Line 920 pword(struct termp *p, const char *word, size_t len) |
|
|
|
chara(p, word[i]); |
chara(p, word[i]); |
} |
} |
|
|
if (TERMENC_ANSI == p->enc && TERMP_STYLE & p->flags) { |
|
chara(p, 27); |
|
stringa(p, "[00m", 4); |
|
} |
|
} |
} |
|
|
|
|
/* |
/* |
* Add a symbol to the output line buffer. |
|
*/ |
|
static void |
|
symbola(struct termp *p, enum tsym sym) |
|
{ |
|
|
|
assert(p->symtab[sym].sym); |
|
stringa(p, p->symtab[sym].sym, p->symtab[sym].sz); |
|
} |
|
|
|
|
|
/* |
|
* Like chara() but for arbitrary-length buffers. Resize the buffer by |
* Like chara() but for arbitrary-length buffers. Resize the buffer by |
* a factor of two (if the buffer is less than that) or the buffer's |
* a factor of two (if the buffer is less than that) or the buffer's |
* size. |
* size. |
Line 871 stringa(struct termp *p, const char *c, size_t sz) |
|
Line 936 stringa(struct termp *p, const char *c, size_t sz) |
|
if (0 == sz) |
if (0 == sz) |
return; |
return; |
|
|
s = sz > p->maxcols * 2 ? sz : p->maxcols * 2; |
|
|
|
assert(c); |
assert(c); |
if (p->col + sz >= p->maxcols) { |
if (p->col + sz >= p->maxcols) { |
|
if (0 == p->maxcols) |
|
p->maxcols = 256; |
|
s = sz > p->maxcols * 2 ? sz : p->maxcols * 2; |
p->buf = realloc(p->buf, s); |
p->buf = realloc(p->buf, s); |
if (NULL == p->buf) |
if (NULL == p->buf) |
err(1, "realloc"); |
err(1, "realloc"); |
p->maxcols = s; |
p->maxcols = s; |
} |
} |
|
|
(void)memcpy(&p->buf[p->col], c, sz); |
(void)memcpy(&p->buf[(int)p->col], c, sz); |
p->col += sz; |
p->col += sz; |
} |
} |
|
|
Line 894 stringa(struct termp *p, const char *c, size_t sz) |
|
Line 960 stringa(struct termp *p, const char *c, size_t sz) |
|
static void |
static void |
chara(struct termp *p, char c) |
chara(struct termp *p, char c) |
{ |
{ |
|
size_t s; |
|
|
if (p->col + 1 >= p->maxcols) { |
if (p->col + 1 >= p->maxcols) { |
p->buf = realloc(p->buf, p->maxcols * 2); |
if (0 == p->maxcols) |
|
p->maxcols = 256; |
|
s = p->maxcols * 2; |
|
p->buf = realloc(p->buf, s); |
if (NULL == p->buf) |
if (NULL == p->buf) |
err(1, "malloc"); |
err(1, "realloc"); |
p->maxcols *= 2; |
p->maxcols = s; |
} |
} |
p->buf[(p->col)++] = c; |
p->buf[(int)(p->col)++] = c; |
} |
} |
|
|
|
|
Line 1013 sanity(const struct mdoc_node *n) |
|
Line 1083 sanity(const struct mdoc_node *n) |
|
break; |
break; |
} |
} |
} |
} |
|
|
|
|
|
static int |
|
merr(void *arg, int line, int col, const char *msg) |
|
{ |
|
|
|
warnx("error: %s (line %d, column %d)", msg, line, col); |
|
return(0); |
|
} |
|
|
|
|
|
static int |
|
mwarn(void *arg, int line, int col, |
|
enum mdoc_warn type, const char *msg) |
|
{ |
|
int flags; |
|
char *wtype; |
|
|
|
flags = *(int *)arg; |
|
wtype = NULL; |
|
|
|
switch (type) { |
|
case (WARN_COMPAT): |
|
wtype = "compat"; |
|
if (flags & WARN_WCOMPAT) |
|
break; |
|
return(1); |
|
case (WARN_SYNTAX): |
|
wtype = "syntax"; |
|
if (flags & WARN_WSYNTAX) |
|
break; |
|
return(1); |
|
} |
|
|
|
assert(wtype); |
|
warnx("%s warning: %s (line %d, column %d)", |
|
wtype, msg, line, col); |
|
|
|
if ( ! (flags & WARN_WERR)) |
|
return(1); |
|
|
|
warnx("%s: considering warnings as errors", |
|
__progname); |
|
return(0); |
|
} |
|
|
|
|