version 1.3, 2009/02/23 07:09:13 |
version 1.11, 2009/02/25 13:30:53 |
|
|
/* $Id$ */ |
/* $Id$ */ |
/* |
/* |
* Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se> |
* Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se> |
* |
* |
|
|
#include <stdlib.h> |
#include <stdlib.h> |
#include <string.h> |
#include <string.h> |
|
|
|
#ifndef __OpenBSD__ |
|
#include <time.h> |
|
#endif |
|
|
#include "mmain.h" |
#include "mmain.h" |
#include "term.h" |
#include "term.h" |
|
|
|
#ifdef __NetBSD__ |
|
#define xisspace(x) isspace((int)(x)) |
|
#else |
|
#define xisspace(x) isspace((x)) |
|
#endif |
|
|
enum termstyle { |
enum termstyle { |
STYLE_CLEAR, |
STYLE_CLEAR, |
STYLE_BOLD, |
STYLE_BOLD, |
Line 45 static void pword(struct termp *, const char *, siz |
|
Line 55 static void pword(struct termp *, const char *, siz |
|
static void pescape(struct termp *, |
static void pescape(struct termp *, |
const char *, size_t *, size_t); |
const char *, size_t *, size_t); |
static void chara(struct termp *, char); |
static void chara(struct termp *, char); |
|
static void stringa(struct termp *, const char *); |
static void style(struct termp *, enum termstyle); |
static void style(struct termp *, enum termstyle); |
|
|
#ifdef __linux__ |
#ifdef __linux__ |
Line 60 main(int argc, char *argv[]) |
|
Line 71 main(int argc, char *argv[]) |
|
const struct mdoc *mdoc; |
const struct mdoc *mdoc; |
struct termp termp; |
struct termp termp; |
|
|
extern int optreset; |
|
extern int optind; |
|
|
|
p = mmain_alloc(); |
p = mmain_alloc(); |
|
|
if ( ! mmain_getopt(p, argc, argv, NULL, NULL, NULL, NULL)) |
if ( ! mmain_getopt(p, argc, argv, NULL, NULL, NULL, NULL)) |
Line 135 flushln(struct termp *p) |
|
Line 143 flushln(struct termp *p) |
|
* Count up visible word characters. Control sequences |
* Count up visible word characters. Control sequences |
* (starting with the CSI) aren't counted. |
* (starting with the CSI) aren't counted. |
*/ |
*/ |
assert( ! isspace(p->buf[i])); |
assert( ! xisspace(p->buf[i])); |
|
|
/* LINTED */ |
/* LINTED */ |
for (j = i, vsz = 0; j < p->col; j++) { |
for (j = i, vsz = 0; j < p->col; j++) { |
if (isspace(p->buf[j])) |
if (xisspace(p->buf[j])) |
break; |
break; |
else if (27 == p->buf[j]) { |
else if (27 == p->buf[j]) { |
assert(j + 4 <= p->col); |
assert(j + 4 <= p->col); |
Line 165 flushln(struct termp *p) |
|
Line 173 flushln(struct termp *p) |
|
for (j = 0; j < p->offset; j++) |
for (j = 0; j < p->offset; j++) |
putchar(' '); |
putchar(' '); |
vis = 0; |
vis = 0; |
} else if (vis + vsz >= maxvis) { |
} else if (vis + vsz >= maxvis) |
/* FIXME */ |
/* FIXME */ |
errx(1, "word breaks right margin"); |
errx(1, "word breaks right margin"); |
} |
|
|
|
/* |
/* |
* Write out the word and a trailing space. Omit the |
* Write out the word and a trailing space. Omit the |
Line 176 flushln(struct termp *p) |
|
Line 183 flushln(struct termp *p) |
|
*/ |
*/ |
|
|
for ( ; i < p->col; i++) { |
for ( ; i < p->col; i++) { |
if (isspace(p->buf[i])) |
if (xisspace(p->buf[i])) |
break; |
break; |
putchar(p->buf[i]); |
putchar(p->buf[i]); |
} |
} |
Line 193 flushln(struct termp *p) |
|
Line 200 flushln(struct termp *p) |
|
*/ |
*/ |
|
|
if (p->flags & TERMP_NOBREAK) { |
if (p->flags & TERMP_NOBREAK) { |
for ( ; vis <= maxvis; vis++) |
for ( ; vis < maxvis; vis++) |
putchar(' '); |
putchar(' '); |
} else |
} else |
putchar('\n'); |
putchar('\n'); |
Line 214 newln(struct termp *p) |
|
Line 221 newln(struct termp *p) |
|
if (0 == p->col) |
if (0 == p->col) |
return; |
return; |
flushln(p); |
flushln(p); |
|
p->flags &= ~TERMP_NOLPAD; |
} |
} |
|
|
|
|
Line 231 vspace(struct termp *p) |
|
Line 239 vspace(struct termp *p) |
|
|
|
|
|
static void |
static void |
|
stringa(struct termp *p, const char *s) |
|
{ |
|
|
|
/* XXX - speed up if not passing to chara. */ |
|
for ( ; *s; s++) |
|
chara(p, *s); |
|
} |
|
|
|
|
|
static void |
chara(struct termp *p, char c) |
chara(struct termp *p, char c) |
{ |
{ |
|
|
Line 284 pescape(struct termp *p, const char *word, size_t *i, |
|
Line 302 pescape(struct termp *p, const char *word, size_t *i, |
|
chara(p, ']'); |
chara(p, ']'); |
else if ('l' == word[*i] && 'B' == word[*i + 1]) |
else if ('l' == word[*i] && 'B' == word[*i + 1]) |
chara(p, '['); |
chara(p, '['); |
|
else if ('<' == word[*i] && '-' == word[*i + 1]) |
|
stringa(p, "<-"); |
|
else if ('-' == word[*i] && '>' == word[*i + 1]) |
|
stringa(p, "->"); |
|
else if ('l' == word[*i] && 'q' == word[*i + 1]) |
|
chara(p, '\"'); |
|
else if ('r' == word[*i] && 'q' == word[*i + 1]) |
|
chara(p, '\"'); |
|
|
(*i)++; |
(*i)++; |
return; |
return; |
Line 363 word(struct termp *p, const char *word) |
|
Line 389 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(word[i])) { |
if ( ! xisspace(word[i])) { |
j++; |
j++; |
continue; |
continue; |
} |
} |
Line 385 body(struct termp *p, const struct mdoc_meta *meta, |
|
Line 411 body(struct termp *p, const struct mdoc_meta *meta, |
|
const struct mdoc_node *node) |
const struct mdoc_node *node) |
{ |
{ |
int dochild; |
int dochild; |
|
struct termpair pair; |
|
|
/* Pre-processing. */ |
/* Pre-processing. */ |
|
|
dochild = 1; |
dochild = 1; |
|
pair.type = 0; |
|
pair.offset = pair.rmargin = 0; |
|
pair.flag = 0; |
|
|
if (MDOC_TEXT != node->type) { |
if (MDOC_TEXT != node->type) { |
if (termacts[node->tok].pre) |
if (termacts[node->tok].pre) |
if ( ! (*termacts[node->tok].pre)(p, meta, node)) |
if ( ! (*termacts[node->tok].pre)(p, &pair, meta, node)) |
dochild = 0; |
dochild = 0; |
} else /* MDOC_TEXT == node->type */ |
} else /* MDOC_TEXT == node->type */ |
word(p, node->data.text.string); |
word(p, node->data.text.string); |
|
|
/* Children. */ |
/* Children. */ |
|
|
|
if (TERMPAIR_FLAG & pair.type) |
|
p->flags |= pair.flag; |
|
|
if (dochild && node->child) |
if (dochild && node->child) |
body(p, meta, node->child); |
body(p, meta, node->child); |
|
|
|
if (TERMPAIR_FLAG & pair.type) |
|
p->flags &= ~pair.flag; |
|
|
/* Post-processing. */ |
/* Post-processing. */ |
|
|
if (MDOC_TEXT != node->type) |
if (MDOC_TEXT != node->type) |
if (termacts[node->tok].post) |
if (termacts[node->tok].post) |
(*termacts[node->tok].post)(p, meta, node); |
(*termacts[node->tok].post)(p, &pair, meta, node); |
|
|
/* Siblings. */ |
/* Siblings. */ |
|
|
Line 429 footer(struct termp *p, const struct mdoc_meta *meta) |
|
Line 465 footer(struct termp *p, const struct mdoc_meta *meta) |
|
|
|
tm = localtime(&meta->date); |
tm = localtime(&meta->date); |
|
|
#ifdef __linux__ |
#ifdef __OpenBSD__ |
if (0 == strftime(buf, p->rmargin, "%B %d, %Y", tm)) |
|
#else |
|
if (NULL == strftime(buf, p->rmargin, "%B %d, %Y", tm)) |
if (NULL == strftime(buf, p->rmargin, "%B %d, %Y", tm)) |
|
#else |
|
if (0 == strftime(buf, p->rmargin, "%B %d, %Y", tm)) |
#endif |
#endif |
err(1, "strftime"); |
err(1, "strftime"); |
|
|