version 1.1, 2009/02/20 11:04:23 |
version 1.8, 2009/02/22 15:50:45 |
|
|
/* $Id$ */ |
/* $Id$ */ |
/* |
/* |
* Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se> |
* Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se> |
* |
* |
* 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 <assert.h> |
#include <assert.h> |
#include <curses.h> |
#include <ctype.h> |
#include <err.h> |
#include <err.h> |
#include <stdlib.h> |
#include <stdlib.h> |
#include <stdio.h> |
#include <stdio.h> |
#include <string.h> |
#include <string.h> |
#include <term.h> |
|
#include <unistd.h> |
#include <unistd.h> |
|
|
#include "mdoc.h" |
#ifdef __linux__ |
|
#include <time.h> |
|
#endif |
|
|
|
#include "term.h" |
|
|
static int termprint_r(size_t, size_t, |
enum termstyle { |
|
STYLE_CLEAR, |
|
STYLE_BOLD, |
|
STYLE_UNDERLINE |
|
}; |
|
|
|
static void termprint_r(struct termp *, |
|
const struct mdoc_meta *, |
const struct mdoc_node *); |
const struct mdoc_node *); |
static void termprint_head(size_t, |
static void termprint_header(struct termp *, |
const struct mdoc_meta *); |
const struct mdoc_meta *); |
static void termprint_tail(size_t, |
static void termprint_footer(struct termp *, |
const struct mdoc_meta *); |
const struct mdoc_meta *); |
|
|
static char *arch2a(enum mdoc_arch); |
static void pword(struct termp *, const char *, size_t); |
static char *vol2a(enum mdoc_vol); |
static void pescape(struct termp *, |
static char *msec2a(enum mdoc_msec); |
const char *, size_t *, size_t); |
|
static void chara(struct termp *, char); |
|
static void style(struct termp *, enum termstyle); |
|
|
static size_t ttitle2a(char *, enum mdoc_vol, enum mdoc_msec, |
#ifdef __linux__ |
enum mdoc_arch, size_t); |
extern size_t strlcat(char *, const char *, size_t); |
|
extern size_t strlcpy(char *, const char *, size_t); |
|
#endif |
|
|
|
void |
static char * |
flushln(struct termp *p) |
arch2a(enum mdoc_arch arch) |
|
{ |
{ |
|
size_t i, j, vsz, vis, maxvis; |
|
|
switch (arch) { |
/* |
case (ARCH_alpha): |
* First, establish the maximum columns of "visible" content. |
return("Alpha"); |
* This is usually the difference between the right-margin and |
case (ARCH_amd64): |
* an indentation, but can be, for tagged lists or columns, a |
return("AMD64"); |
* small set of values. |
case (ARCH_amiga): |
*/ |
return("Amiga"); |
|
case (ARCH_arc): |
assert(p->offset < p->rmargin); |
return("ARC"); |
maxvis = p->rmargin - p->offset; |
case (ARCH_arm): |
vis = 0; |
return("ARM"); |
|
case (ARCH_armish): |
/* |
return("ARMISH"); |
* If in the standard case (left-justified), then begin with our |
case (ARCH_aviion): |
* indentation, otherwise (columns, etc.) just start spitting |
return("AViion"); |
* out text. |
case (ARCH_hp300): |
*/ |
return("HP300"); |
|
case (ARCH_hppa): |
if ( ! (p->flags & TERMP_NOLPAD)) |
return("HPPA"); |
/* LINTED */ |
case (ARCH_hppa64): |
for (j = 0; j < p->offset; j++) |
return("HPPA64"); |
putchar(' '); |
case (ARCH_i386): |
|
return("i386"); |
/* |
case (ARCH_landisk): |
* If we're literal, print out verbatim. |
return("LANDISK"); |
*/ |
case (ARCH_luna88k): |
if (p->flags & TERMP_LITERAL) { |
return("Luna88k"); |
/* FIXME: count non-printing chars. */ |
case (ARCH_mac68k): |
for (i = 0; i < p->col; i++) |
return("Mac68k"); |
putchar(p->buf[i]); |
case (ARCH_macppc): |
putchar('\n'); |
return("MacPPC"); |
p->col = 0; |
case (ARCH_mvme68k): |
return; |
return("MVME68k"); |
|
case (ARCH_mvme88k): |
|
return("MVME88k"); |
|
case (ARCH_mvmeppc): |
|
return("MVMEPPC"); |
|
case (ARCH_pmax): |
|
return("PMAX"); |
|
case (ARCH_sgi): |
|
return("SGI"); |
|
case (ARCH_socppc): |
|
return("SOCPPC"); |
|
case (ARCH_sparc): |
|
return("SPARC"); |
|
case (ARCH_sparc64): |
|
return("SPARC64"); |
|
case (ARCH_sun3): |
|
return("Sun3"); |
|
case (ARCH_vax): |
|
return("VAX"); |
|
case (ARCH_zaurus): |
|
return("Zaurus"); |
|
default: |
|
break; |
|
} |
} |
|
|
return(NULL); |
for (i = 0; i < p->col; i++) { |
|
/* |
|
* Count up visible word characters. Control sequences |
|
* (starting with the CSI) aren't counted. |
|
*/ |
|
assert( ! isspace(p->buf[i])); |
|
|
|
/* LINTED */ |
|
for (j = i, vsz = 0; j < p->col; j++) { |
|
if (isspace(p->buf[j])) |
|
break; |
|
else if (27 == p->buf[j]) { |
|
assert(j + 4 <= p->col); |
|
j += 3; |
|
} else |
|
vsz++; |
|
} |
|
assert(vsz > 0); |
|
|
|
/* |
|
* If a word is too long and we're within a line, put it |
|
* on the next line. Puke if we're being asked to write |
|
* something that will exceed the right margin (i.e., |
|
* from a fresh line or when we're not allowed to break |
|
* the line with TERMP_NOBREAK). |
|
*/ |
|
|
|
if (vis && vis + vsz >= maxvis) { |
|
/* FIXME */ |
|
if (p->flags & TERMP_NOBREAK) |
|
errx(1, "word breaks right margin"); |
|
putchar('\n'); |
|
for (j = 0; j < p->offset; j++) |
|
putchar(' '); |
|
vis = 0; |
|
} else if (vis + vsz >= maxvis) { |
|
/* FIXME */ |
|
errx(1, "word breaks right margin"); |
|
} |
|
|
|
/* |
|
* Write out the word and a trailing space. Omit the |
|
* space if we're the last word in the line. |
|
*/ |
|
|
|
for ( ; i < p->col; i++) { |
|
if (isspace(p->buf[i])) |
|
break; |
|
putchar(p->buf[i]); |
|
} |
|
vis += vsz; |
|
if (i < p->col) { |
|
putchar(' '); |
|
vis++; |
|
} |
|
} |
|
|
|
/* |
|
* If we're not to right-marginalise it (newline), then instead |
|
* pad to the right margin and stay off. |
|
*/ |
|
|
|
if (p->flags & TERMP_NOBREAK) { |
|
for ( ; vis <= maxvis; vis++) |
|
putchar(' '); |
|
} else |
|
putchar('\n'); |
|
|
|
p->col = 0; |
} |
} |
|
|
|
|
static char * |
void |
vol2a(enum mdoc_vol vol) |
newln(struct termp *p) |
{ |
{ |
|
|
switch (vol) { |
/* |
case (VOL_AMD): |
* A newline only breaks an existing line; it won't assert |
return("OpenBSD Ancestral Manual Documents"); |
* vertical space. |
case (VOL_IND): |
*/ |
return("OpenBSD Manual Master Index"); |
p->flags |= TERMP_NOSPACE; |
case (VOL_KM): |
if (0 == p->col) |
return("OpenBSD Kernel Manual"); |
return; |
case (VOL_LOCAL): |
flushln(p); |
return("OpenBSD Local Manual"); |
} |
case (VOL_PRM): |
|
return("OpenBSD Programmer's Manual"); |
|
case (VOL_PS1): |
|
return("OpenBSD Programmer's Supplementary Documents"); |
|
case (VOL_SMM): |
|
return("OpenBSD System Manager's Manual"); |
|
case (VOL_URM): |
|
return("OpenBSD Reference Manual"); |
|
case (VOL_USD): |
|
return("OpenBSD User's Supplementary Documents"); |
|
default: |
|
break; |
|
} |
|
|
|
return(NULL); |
|
|
void |
|
vspace(struct termp *p) |
|
{ |
|
|
|
/* |
|
* Asserts a vertical space (a full, empty line-break between |
|
* lines). |
|
*/ |
|
newln(p); |
|
putchar('\n'); |
} |
} |
|
|
|
|
static char * |
static void |
msec2a(enum mdoc_msec msec) |
chara(struct termp *p, char c) |
{ |
{ |
|
|
switch (msec) { |
/* TODO: dynamically expand the buffer. */ |
case(MSEC_1): |
if (p->col + 1 >= p->maxcols) |
return("1"); |
errx(1, "line overrun"); |
case(MSEC_2): |
p->buf[(p->col)++] = c; |
return("2"); |
} |
case(MSEC_3): |
|
return("3"); |
|
case(MSEC_3f): |
static void |
return("3f"); |
style(struct termp *p, enum termstyle esc) |
case(MSEC_3p): |
{ |
return("3p"); |
|
case(MSEC_4): |
if (p->col + 4 >= p->maxcols) |
return("4"); |
errx(1, "line overrun"); |
case(MSEC_5): |
|
return("5"); |
p->buf[(p->col)++] = 27; |
case(MSEC_6): |
p->buf[(p->col)++] = '['; |
return("6"); |
switch (esc) { |
case(MSEC_7): |
case (STYLE_CLEAR): |
return("7"); |
p->buf[(p->col)++] = '0'; |
case(MSEC_8): |
|
return("8"); |
|
case(MSEC_9): |
|
return("9"); |
|
case(MSEC_X11): |
|
return("X11"); |
|
case(MSEC_X11R6): |
|
return("X11R6"); |
|
case(MSEC_local): |
|
return("local"); |
|
case(MSEC_n): |
|
return("n"); |
|
case(MSEC_unass): |
|
/* FALLTHROUGH */ |
|
case(MSEC_draft): |
|
return("draft"); |
|
case(MSEC_paper): |
|
return("paper"); |
|
default: |
|
break; |
break; |
|
case (STYLE_BOLD): |
|
p->buf[(p->col)++] = '1'; |
|
break; |
|
case (STYLE_UNDERLINE): |
|
p->buf[(p->col)++] = '4'; |
|
break; |
|
default: |
|
abort(); |
|
/* NOTREACHED */ |
} |
} |
return(NULL); |
p->buf[(p->col)++] = 'm'; |
} |
} |
|
|
|
|
static size_t |
static void |
ttitle2a(char *dst, enum mdoc_vol vol, enum mdoc_msec msec, |
pescape(struct termp *p, const char *word, size_t *i, size_t len) |
enum mdoc_arch arch, size_t sz) |
|
{ |
{ |
char *p; |
|
size_t ssz; |
|
|
|
if (NULL == (p = vol2a(vol))) |
(*i)++; |
switch (msec) { |
assert(*i < len); |
case (MSEC_1): |
|
|
if ('(' == word[*i]) { |
|
/* Two-character escapes. */ |
|
(*i)++; |
|
assert(*i + 1 < len); |
|
|
|
if ('r' == word[*i] && 'B' == word[*i + 1]) |
|
chara(p, ']'); |
|
else if ('l' == word[*i] && 'B' == word[*i + 1]) |
|
chara(p, '['); |
|
|
|
(*i)++; |
|
return; |
|
|
|
} else if ('[' != word[*i]) { |
|
/* One-character escapes. */ |
|
switch (word[*i]) { |
|
case ('\\'): |
/* FALLTHROUGH */ |
/* FALLTHROUGH */ |
case (MSEC_6): |
case ('\''): |
/* FALLTHROUGH */ |
/* FALLTHROUGH */ |
case (MSEC_7): |
case ('`'): |
p = vol2a(VOL_URM); |
|
break; |
|
case (MSEC_8): |
|
p = vol2a(VOL_SMM); |
|
break; |
|
case (MSEC_2): |
|
/* FALLTHROUGH */ |
/* FALLTHROUGH */ |
case (MSEC_3): |
case ('-'): |
/* FALLTHROUGH */ |
/* FALLTHROUGH */ |
case (MSEC_4): |
case ('.'): |
/* FALLTHROUGH */ |
chara(p, word[*i]); |
case (MSEC_5): |
|
p = vol2a(VOL_PRM); |
|
break; |
|
case (MSEC_9): |
|
p = vol2a(VOL_KM); |
|
break; |
|
default: |
default: |
/* FIXME: capitalise. */ |
|
if (NULL == (p = msec2a(msec))) |
|
p = msec2a(MSEC_local); |
|
break; |
break; |
} |
} |
assert(p); |
return; |
|
} |
|
/* n-character escapes. */ |
|
} |
|
|
if ((ssz = strlcpy(dst, p, sz)) >= sz) |
|
return(ssz); |
|
|
|
if ((p = arch2a(arch))) { |
static void |
if ((ssz = strlcat(dst, " (", sz)) >= sz) |
pword(struct termp *p, const char *word, size_t len) |
return(ssz); |
{ |
if ((ssz = strlcat(dst, p, sz)) >= sz) |
size_t i; |
return(ssz); |
|
if ((ssz = strlcat(dst, ")", sz)) >= sz) |
/*assert(len > 0);*/ /* Can be, if literal. */ |
return(ssz); |
|
|
if ( ! (p->flags & TERMP_NOSPACE) && |
|
! (p->flags & TERMP_LITERAL)) |
|
chara(p, ' '); |
|
|
|
p->flags &= ~TERMP_NOSPACE; |
|
|
|
if (p->flags & TERMP_BOLD) |
|
style(p, STYLE_BOLD); |
|
if (p->flags & TERMP_UNDERLINE) |
|
style(p, STYLE_UNDERLINE); |
|
|
|
for (i = 0; i < len; i++) { |
|
if ('\\' == word[i]) { |
|
pescape(p, word, &i, len); |
|
continue; |
|
} |
|
chara(p, word[i]); |
} |
} |
|
|
return(ssz); |
if (p->flags & TERMP_BOLD || |
|
p->flags & TERMP_UNDERLINE) |
|
style(p, STYLE_CLEAR); |
} |
} |
|
|
|
|
static int |
void |
termprint_r(size_t cols, size_t indent, const struct mdoc_node *node) |
word(struct termp *p, const char *word) |
{ |
{ |
|
size_t i, j, len; |
|
|
return(1); |
if (p->flags & TERMP_LITERAL) { |
|
pword(p, word, strlen(word)); |
|
return; |
|
} |
|
|
|
len = strlen(word); |
|
assert(len > 0); |
|
|
|
if (mdoc_isdelim(word)) |
|
p->flags |= TERMP_NOSPACE; |
|
|
|
/* LINTED */ |
|
for (j = i = 0; i < len; i++) { |
|
if ( ! isspace(word[i])) { |
|
j++; |
|
continue; |
|
} |
|
if (0 == j) |
|
continue; |
|
assert(i >= j); |
|
pword(p, &word[i - j], j); |
|
j = 0; |
|
} |
|
if (j > 0) { |
|
assert(i >= j); |
|
pword(p, &word[i - j], j); |
|
} |
} |
} |
|
|
|
|
static void |
static void |
termprint_tail(size_t cols, const struct mdoc_meta *meta) |
termprint_r(struct termp *p, const struct mdoc_meta *meta, |
|
const struct mdoc_node *node) |
{ |
{ |
|
int dochild; |
|
|
|
/* Pre-processing. */ |
|
|
|
dochild = 1; |
|
|
|
if (MDOC_TEXT != node->type) { |
|
if (termacts[node->tok].pre) |
|
if ( ! (*termacts[node->tok].pre)(p, meta, node)) |
|
dochild = 0; |
|
} else /* MDOC_TEXT == node->type */ |
|
word(p, node->data.text.string); |
|
|
|
/* Children. */ |
|
|
|
if (dochild && node->child) |
|
termprint_r(p, meta, node->child); |
|
|
|
/* Post-processing. */ |
|
|
|
if (MDOC_TEXT != node->type) |
|
if (termacts[node->tok].post) |
|
(*termacts[node->tok].post)(p, meta, node); |
|
|
|
/* Siblings. */ |
|
|
|
if (node->next) |
|
termprint_r(p, meta, node->next); |
|
} |
|
|
|
|
|
static void |
|
termprint_footer(struct termp *p, const struct mdoc_meta *meta) |
|
{ |
struct tm *tm; |
struct tm *tm; |
char *buf, *os; |
char *buf, *os; |
size_t sz, osz, ssz, i; |
size_t sz, osz, ssz, i; |
|
|
if (NULL == (buf = malloc(cols))) |
if (NULL == (buf = malloc(p->rmargin))) |
err(1, "malloc"); |
err(1, "malloc"); |
if (NULL == (os = malloc(cols))) |
if (NULL == (os = malloc(p->rmargin))) |
err(1, "malloc"); |
err(1, "malloc"); |
|
|
tm = localtime(&meta->date); |
tm = localtime(&meta->date); |
if (NULL == strftime(buf, cols, "%B %d, %Y", tm)) |
|
|
#ifdef __linux__ |
|
if (0 == strftime(buf, p->rmargin, "%B %d, %Y", tm)) |
|
#else |
|
if (NULL == strftime(buf, p->rmargin, "%B %d, %Y", tm)) |
|
#endif |
err(1, "strftime"); |
err(1, "strftime"); |
|
|
osz = strlcpy(os, meta->os, cols); |
osz = strlcpy(os, meta->os, p->rmargin); |
|
|
sz = strlen(buf); |
sz = strlen(buf); |
ssz = sz + osz + 1; |
ssz = sz + osz + 1; |
|
|
if (ssz > cols) { |
if (ssz > p->rmargin) { |
ssz -= cols; |
ssz -= p->rmargin; |
assert(ssz <= osz); |
assert(ssz <= osz); |
os[osz - ssz] = 0; |
os[osz - ssz] = 0; |
ssz = 1; |
ssz = 1; |
} else |
} else |
ssz = cols - ssz + 1; |
ssz = p->rmargin - ssz + 1; |
|
|
|
printf("\n"); |
printf("%s", os); |
printf("%s", os); |
for (i = 0; i < ssz; i++) |
for (i = 0; i < ssz; i++) |
printf(" "); |
printf(" "); |
|
|
printf("%s\n", buf); |
printf("%s\n", buf); |
|
fflush(stdout); |
|
|
free(buf); |
free(buf); |
free(os); |
free(os); |
Line 291 termprint_tail(size_t cols, const struct mdoc_meta *me |
|
Line 424 termprint_tail(size_t cols, const struct mdoc_meta *me |
|
|
|
|
|
static void |
static void |
termprint_head(size_t cols, const struct mdoc_meta *meta) |
termprint_header(struct termp *p, const struct mdoc_meta *meta) |
{ |
{ |
char *msec, *buf, *title; |
char *buf, *title; |
size_t ssz, tsz, ttsz, i; |
const char *pp, *msec; |
|
size_t ssz, tsz, ttsz, i;; |
|
|
if (NULL == (buf = malloc(cols))) |
if (NULL == (buf = malloc(p->rmargin))) |
err(1, "malloc"); |
err(1, "malloc"); |
if (NULL == (title = malloc(cols))) |
if (NULL == (title = malloc(p->rmargin))) |
err(1, "malloc"); |
err(1, "malloc"); |
|
|
/* Format the manual page header. */ |
if (NULL == (pp = mdoc_vol2a(meta->vol))) |
|
switch (meta->msec) { |
|
case (MSEC_1): |
|
/* FALLTHROUGH */ |
|
case (MSEC_6): |
|
/* FALLTHROUGH */ |
|
case (MSEC_7): |
|
pp = mdoc_vol2a(VOL_URM); |
|
break; |
|
case (MSEC_8): |
|
pp = mdoc_vol2a(VOL_SMM); |
|
break; |
|
case (MSEC_2): |
|
/* FALLTHROUGH */ |
|
case (MSEC_3): |
|
/* FALLTHROUGH */ |
|
case (MSEC_4): |
|
/* FALLTHROUGH */ |
|
case (MSEC_5): |
|
pp = mdoc_vol2a(VOL_PRM); |
|
break; |
|
case (MSEC_9): |
|
pp = mdoc_vol2a(VOL_KM); |
|
break; |
|
default: |
|
/* FIXME: capitalise. */ |
|
if (NULL == (pp = mdoc_msec2a(meta->msec))) |
|
pp = mdoc_msec2a(MSEC_local); |
|
break; |
|
} |
|
assert(pp); |
|
|
tsz = ttitle2a(buf, meta->vol, meta->msec, meta->arch, cols); |
tsz = strlcpy(buf, pp, p->rmargin); |
ttsz = strlcpy(title, meta->title, cols); |
assert(tsz < p->rmargin); |
|
|
if (NULL == (msec = msec2a(meta->msec))) |
if ((pp = mdoc_arch2a(meta->arch))) { |
|
tsz = strlcat(buf, " (", p->rmargin); |
|
assert(tsz < p->rmargin); |
|
tsz = strlcat(buf, pp, p->rmargin); |
|
assert(tsz < p->rmargin); |
|
tsz = strlcat(buf, ")", p->rmargin); |
|
assert(tsz < p->rmargin); |
|
} |
|
|
|
ttsz = strlcpy(title, meta->title, p->rmargin); |
|
|
|
if (NULL == (msec = mdoc_msec2a(meta->msec))) |
msec = ""; |
msec = ""; |
|
|
ssz = (2 * (ttsz + 2 + strlen(msec))) + tsz + 2; |
ssz = (2 * (ttsz + 2 + strlen(msec))) + tsz + 2; |
|
|
if (ssz > cols) { |
if (ssz > p->rmargin) { |
if ((ssz -= cols) % 2) |
if ((ssz -= p->rmargin) % 2) |
ssz++; |
ssz++; |
ssz /= 2; |
ssz /= 2; |
|
|
Line 320 termprint_head(size_t cols, const struct mdoc_meta *me |
|
Line 495 termprint_head(size_t cols, const struct mdoc_meta *me |
|
title[ttsz - ssz] = 0; |
title[ttsz - ssz] = 0; |
ssz = 1; |
ssz = 1; |
} else |
} else |
ssz = ((cols - ssz) / 2) + 1; |
ssz = ((p->rmargin - ssz) / 2) + 1; |
|
|
printf("%s(%s)", title, msec); |
printf("%s(%s)", title, msec); |
|
|
Line 332 termprint_head(size_t cols, const struct mdoc_meta *me |
|
Line 507 termprint_head(size_t cols, const struct mdoc_meta *me |
|
for (i = 0; i < ssz; i++) |
for (i = 0; i < ssz; i++) |
printf(" "); |
printf(" "); |
|
|
printf("%s(%s)\n\n", title, msec); |
printf("%s(%s)\n", title, msec); |
|
fflush(stdout); |
|
|
free(title); |
free(title); |
free(buf); |
free(buf); |
} |
} |
|
|
|
|
int |
void |
termprint(const struct mdoc_node *node, |
termprint(const struct mdoc_node *node, |
const struct mdoc_meta *meta) |
const struct mdoc_meta *meta) |
{ |
{ |
size_t cols; |
struct termp p; |
|
|
if (ERR == setupterm(NULL, STDOUT_FILENO, NULL)) |
p.maxrmargin = 80; /* XXX */ |
return(0); |
p.rmargin = p.maxrmargin; |
|
p.maxcols = 1024; |
|
p.offset = p.col = 0; |
|
p.flags = TERMP_NOSPACE; |
|
|
cols = columns < 60 ? 60 : (size_t)columns; |
if (NULL == (p.buf = malloc(p.maxcols))) |
|
err(1, "malloc"); |
|
|
termprint_head(cols, meta); |
termprint_header(&p, meta); |
if ( ! termprint_r(cols, 0, node)) |
termprint_r(&p, meta, node); |
return(0); |
termprint_footer(&p, meta); |
termprint_tail(cols, meta); |
|
return(1); |
free(p.buf); |
} |
} |
|
|
|
|