diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | mdocterm.1 | 41 | ||||
-rw-r--r-- | mdocterm.c | 150 | ||||
-rw-r--r-- | term.c | 20 | ||||
-rw-r--r-- | term.h | 37 |
5 files changed, 147 insertions, 103 deletions
@@ -1,6 +1,6 @@ .SUFFIXES: .html .sgml -VERSION = 1.4.15 +VERSION = 1.4.16 VDATE = 14 March 2009 BINDIR = $(PREFIX)/bin @@ -28,6 +28,7 @@ .Nm mdocmterm .Op Fl vV .Op Fl f Ns Ar option... +.Op Fl O Ns Ar option... .Op Fl W Ns Ar err... .Op Ar infile .\" SECTION @@ -50,6 +51,15 @@ Override default compiler behaviour. See .Sx Compiler Options for details. .\" ITEM +.It Fl O Ns Ar option... +Terminal-encoding options. May be set to +.Fl O Ns Ar ansi +for ANSI/VT100-encoded output (the default) or +.Fl O Ns Ar nroff +for nroff-encoded +.Qq backspace +output. +.\" ITEM .It Fl W Ns Ar err... Print warning messages. May be set to .Fl W Ns Ar all @@ -119,30 +129,11 @@ options may be grouped and delimited with a comma. Using .Fl f Ns Ar ign-scope,ign-escape , for example, will try to ignore scope and character-escape errors. .\" SUB-SECTION -.Ss Input Encoding -The -.Nm -utility expects its input to be 7-bit ASCII as defined in -.Xr ascii 7 . -The only non-graphing characters accepted are spaces, -.Sq \ , -and tabs, -.Sq \et . -Tabs are only accepted in literal block-displays and as column -delimiters. -.Pp -Only Unix-style newlines (\en) are accepted; if the newline is escaped, -the line is concatenated with the next. -.\" SUB-SECTION .Ss Character Escapes -This section documents the character-escapes accepted by -.Xr mdocterm 1 . -Note that the \\x, \\(xx and \\[n] forms are described here; the \\*(xx, -\\*[n] and \\*x forms described in -.Xr mdoc.samples 7 -are deprecated, but still rendered. All one- and two-character -sequences may be used in the n-character sequence \\[n]. -.Pp +The following table shows all +.Xr mdoc 7 +character escapes rendered by +.Nm . Note that the .Em Output column will render differently whether executed with @@ -266,7 +257,7 @@ Special symbols: the more general syntax. .\" SECTION .Sh EXAMPLES -To display this manual page: +To display this manual page on ANSI-capable terminal: .\" PARAGRAPH .Pp .D1 % mdocmterm \-Wall,error mdocmterm.1 @@ -274,7 +265,7 @@ To display this manual page: .Pp To pipe a manual page to the pager: .Pp -.D1 % mdocterm mdocterm.1 2>&1 | less \-R +.D1 % mdocterm -Onroff mdocterm.1 | less .\" SECTION .Sh SEE ALSO .Xr mdoctree 1 , @@ -29,11 +29,12 @@ #include "mmain.h" #include "term.h" -struct termenc { +struct termseq { const char *enc; int sym; }; +static int option(void *, int, const char *); static void body(struct termp *, struct termpair *, const struct mdoc_meta *, @@ -53,14 +54,13 @@ static void stringa(struct termp *, const char *, size_t); static void symbola(struct termp *, enum tsym); static void sanity(const struct mdoc_node *); -static void stylea(struct termp *, enum tstyle); #ifdef __linux__ extern size_t strlcat(char *, const char *, size_t); extern size_t strlcpy(char *, const char *, size_t); #endif -static struct termenc termenc1[] = { +static struct termseq termenc1[] = { { "\\", TERMSYM_SLASH }, { "\'", TERMSYM_RSQUOTE }, { "`", TERMSYM_LSQUOTE }, @@ -74,7 +74,7 @@ static struct termenc termenc1[] = { { NULL, 0 } }; -static struct termenc termenc2[] = { +static struct termseq termenc2[] = { { "rC", TERMSYM_RBRACE }, { "lC", TERMSYM_LBRACE }, { "rB", TERMSYM_RBRACK }, @@ -126,7 +126,7 @@ static struct termenc termenc2[] = { { NULL, 0 } }; -static struct termsym termsym_ansi[TERMSYM_MAX] = { +static struct termsym termsym_ascii[TERMSYM_MAX] = { { "]", 1 }, /* TERMSYM_RBRACK */ { "[", 1 }, /* TERMSYM_LBRACK */ { "<-", 2 }, /* TERMSYM_LARROW */ @@ -170,16 +170,6 @@ static struct termsym termsym_ansi[TERMSYM_MAX] = { { "}", 1 }, /* TERMSYM_RBRACE */ }; -static const char ansi_clear[] = { 27, '[', '0', 'm' }; -static const char ansi_bold[] = { 27, '[', '1', 'm' }; -static const char ansi_under[] = { 27, '[', '4', 'm' }; - -static struct termsym termstyle_ansi[] = { - { ansi_clear, 4 }, - { ansi_bold, 4 }, - { ansi_under, 4 } -}; - int main(int argc, char *argv[]) @@ -189,22 +179,25 @@ main(int argc, char *argv[]) const struct mdoc *mdoc; struct termp termp; + (void)memset(&termp, 0, sizeof(struct termp)); + + termp.maxrmargin = termp.rmargin = 78; /* XXX */ + termp.maxcols = 1024; + termp.offset = termp.col = 0; + termp.flags = TERMP_NOSPACE; + termp.symtab = termsym_ascii; + termp.enc = TERMENC_ANSI; + p = mmain_alloc(); + c = mmain_getopt(p, argc, argv, "[-Ooption...]", + "O:", &termp, option); - c = mmain_getopt(p, argc, argv, NULL, NULL, NULL, NULL); if (1 != c) mmain_exit(p, -1 == c ? 1 : 0); if (NULL == (mdoc = mmain_mdoc(p))) mmain_exit(p, 1); - termp.maxrmargin = termp.rmargin = 78; /* XXX */ - termp.maxcols = 1024; - termp.offset = termp.col = 0; - termp.flags = TERMP_NOSPACE; - termp.symtab = termsym_ansi; - termp.styletab = termstyle_ansi; - if (NULL == (termp.buf = malloc(termp.maxcols))) err(1, "malloc"); @@ -219,6 +212,26 @@ main(int argc, char *argv[]) } +int +option(void *ptr, int c, const char *arg) +{ + struct termp *p; + + p = (struct termp *)ptr; + + if (0 == strcmp(arg, "nroff")) { + p->enc = TERMENC_NROFF; + return(1); + } else if (0 == strcmp(arg, "ansi")) { + p->enc = TERMENC_ANSI; + return(1); + } + + warnx("unknown option: -O%s", arg); + return(0); +} + + /* * 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 @@ -288,15 +301,18 @@ flushln(struct termp *p) * space is printed according to regular spacing rules). */ - /* FIXME: make non-ANSI friendly. */ - /* LINTED */ for (j = i, vsz = 0; j < p->col; j++) { - if (isspace((u_char)p->buf[j])) + if (isspace((u_char)p->buf[j])) { break; - else if (27 == p->buf[j]) { - assert(j + 4 <= p->col); - j += 3; + } else if (27 == 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; } else vsz++; } @@ -656,7 +672,7 @@ header(struct termp *p, const struct mdoc_meta *meta) static void nescape(struct termp *p, const char *word, size_t len) { - struct termenc *enc; + struct termseq *enc; switch (len) { case (1): @@ -763,26 +779,70 @@ pword(struct termp *p, const char *word, size_t len) p->flags &= ~TERMP_NOSPACE; /* - * XXX - if literal and underlining, this will underline the - * spaces between literal words. + * If ANSI (word-length styling), then apply our style now, + * before the word. */ - if (p->flags & TERMP_BOLD) - stylea(p, TERMSTYLE_BOLD); - if (p->flags & TERMP_UNDERLINE) - stylea(p, TERMSTYLE_UNDER); + 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++) { if ('\\' == word[i]) { pescape(p, word, &i, len); continue; } + + if (TERMENC_NROFF == p->enc && + TERMP_STYLE & p->flags) { + if (TERMP_BOLD & p->flags) { + chara(p, word[i]); + chara(p, 8); + } + if (TERMP_UNDER & p->flags) { + chara(p, '_'); + chara(p, 8); + } + } + chara(p, word[i]); } - if (p->flags & TERMP_BOLD || - p->flags & TERMP_UNDERLINE) - stylea(p, TERMSTYLE_CLEAR); + if (TERMENC_ANSI == p->enc && TERMP_STYLE & p->flags) { + chara(p, 27); + stringa(p, "[00m", 4); + } } @@ -799,18 +859,6 @@ symbola(struct termp *p, enum tsym sym) /* - * Add a style to the output line buffer. - */ -static void -stylea(struct termp *p, enum tstyle style) -{ - - assert(p->styletab[style].sym); - stringa(p, p->styletab[style].sym, p->styletab[style].sz); -} - - -/* * 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 * size. @@ -67,28 +67,28 @@ /* TODO: abstract this into mdocterm.c. */ const int ttypes[TTYPE_NMAX] = { - TERMP_BOLD, /* TTYPE_PROG */ + TERMP_BOLD, /* TTYPE_PROG */ TERMP_BOLD, /* TTYPE_CMD_FLAG */ - TERMP_UNDERLINE, /* TTYPE_CMD_ARG */ + TERMP_UNDER, /* TTYPE_CMD_ARG */ TERMP_BOLD, /* TTYPE_SECTION */ TERMP_BOLD, /* TTYPE_FUNC_DECL */ - TERMP_UNDERLINE, /* TTYPE_VAR_DECL */ - TERMP_UNDERLINE, /* TTYPE_FUNC_TYPE */ + TERMP_UNDER, /* TTYPE_VAR_DECL */ + TERMP_UNDER, /* TTYPE_FUNC_TYPE */ TERMP_BOLD, /* TTYPE_FUNC_NAME */ - TERMP_UNDERLINE, /* TTYPE_FUNC_ARG */ - TERMP_UNDERLINE, /* TTYPE_LINK */ + TERMP_UNDER, /* TTYPE_FUNC_ARG */ + TERMP_UNDER, /* TTYPE_LINK */ TERMP_BOLD, /* TTYPE_SSECTION */ - TERMP_UNDERLINE, /* TTYPE_FILE */ - TERMP_UNDERLINE, /* TTYPE_EMPH */ + TERMP_UNDER, /* TTYPE_FILE */ + TERMP_UNDER, /* TTYPE_EMPH */ TERMP_BOLD, /* TTYPE_CONFIG */ TERMP_BOLD, /* TTYPE_CMD */ TERMP_BOLD, /* TTYPE_INCLUDE */ TERMP_BOLD, /* TTYPE_SYMB */ TERMP_BOLD, /* TTYPE_SYMBOL */ TERMP_BOLD, /* TTYPE_DIAG */ - TERMP_UNDERLINE, /* TTYPE_LINK_ANCHOR */ + TERMP_UNDER, /* TTYPE_LINK_ANCHOR */ TERMP_BOLD, /* TTYPE_LINK_TEXT */ - TERMP_UNDERLINE /* TTYPE_REF_TITLE */ + TERMP_UNDER /* TTYPE_REF_TITLE */ }; static int arg_hasattr(int, const struct mdoc_node *); @@ -70,12 +70,9 @@ enum tsym { TERMSYM_MAX = 41 }; - -enum tstyle { - TERMSTYLE_CLEAR = 0, - TERMSTYLE_BOLD = 1, - TERMSTYLE_UNDER = 2, - TERMSTYLE_MAX = 3 +enum termenc { + TERMENC_ANSI, + TERMENC_NROFF }; struct termsym { @@ -90,18 +87,26 @@ struct termp { size_t offset; size_t col; int flags; -#define TERMP_BOLD (1 << 0) /* Embolden words. */ -#define TERMP_UNDERLINE (1 << 1) /* Underline words. */ -#define TERMP_NOSPACE (1 << 2) /* No space before words. */ -#define TERMP_NOLPAD (1 << 3) /* No leftpad before flush. */ -#define TERMP_NOBREAK (1 << 4) /* No break after flush. */ -#define TERMP_LITERAL (1 << 5) /* Literal words. */ -#define TERMP_IGNDELIM (1 << 6) /* Delims like regulars. */ -#define TERMP_NONOSPACE (1 << 7) /* No space (no autounset). */ -#define TERMP_NONOBREAK (1 << 8) +#define TERMP_NOSPACE (1 << 0) /* No space before words. */ +#define TERMP_NOLPAD (1 << 1) /* No leftpad before flush. */ +#define TERMP_NOBREAK (1 << 2) /* No break after flush. */ +#define TERMP_LITERAL (1 << 3) /* Literal words. */ +#define TERMP_IGNDELIM (1 << 4) /* Delims like regulars. */ +#define TERMP_NONOSPACE (1 << 5) /* No space (no autounset). */ +#define TERMP_NONOBREAK (1 << 7) /* Don't newln NOBREAK. */ + +#define TERMP_STYLE 0xff00 /* Style mask. */ +#define TERMP_BOLD (1 << 8) /* Styles... */ +#define TERMP_UNDER (1 << 9) +#define TERMP_BLUE (1 << 10) +#define TERMP_RED (1 << 11) +#define TERMP_YELLOW (1 << 12) +#define TERMP_MAGENTA (1 << 13) +#define TERMP_CYAN (1 << 14) +#define TERMP_GREEN (1 << 15) char *buf; struct termsym *symtab; /* Special-symbol table. */ - struct termsym *styletab; /* Style table. */ + enum termenc enc; /* Encoding. */ }; struct termpair { |