version 1.1, 2008/12/02 18:26:57 |
version 1.7, 2008/12/05 19:45:15 |
|
|
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
* PERFORMANCE OF THIS SOFTWARE. |
* PERFORMANCE OF THIS SOFTWARE. |
*/ |
*/ |
|
#include <assert.h> |
#include <stdlib.h> |
#include <stdlib.h> |
#include <string.h> |
#include <string.h> |
|
|
#include "libmdocml.h" |
#include "libmdocml.h" |
#include "private.h" |
#include "private.h" |
|
#include "ml.h" |
|
|
|
#ifdef __linux__ |
|
extern size_t strlcat(char *, const char *, size_t); |
|
extern size_t strlcpy(char *, const char *, size_t); |
|
#endif |
|
|
#define MAXINDENT 8 |
|
|
|
static ssize_t ml_puts(struct md_mbuf *, const char *); |
int |
static ssize_t ml_putchar(struct md_mbuf *, char); |
ml_putstring(struct md_mbuf *p, const char *buf, size_t *pos) |
static ssize_t ml_putstring(struct md_mbuf *, const char *); |
|
|
|
|
|
static ssize_t |
|
ml_puts(struct md_mbuf *p, const char *buf) |
|
{ |
{ |
|
|
return(ml_nputs(p, buf, strlen(buf))); |
return(ml_nputstring(p, buf, strlen(buf), pos)); |
} |
} |
|
|
|
|
static ssize_t |
int |
ml_putchar(struct md_mbuf *p, char buf) |
ml_nputstring(struct md_mbuf *p, |
|
const char *buf, size_t sz, size_t *pos) |
{ |
{ |
|
int i, v; |
|
const char *seq; |
|
size_t ssz; |
|
|
return(ml_nputs(p, &buf, 1)); |
for (i = 0; i < (int)sz; i++) { |
} |
switch (buf[i]) { |
|
|
|
/* Escaped value. */ |
|
case ('\\'): |
|
if (-1 == (v = rofftok_scan(buf, &i))) |
|
return(0); |
|
|
static ssize_t |
switch (v) { |
ml_putstring(struct md_mbuf *p, const char *buf) |
case (ROFFTok_Sp_A): |
{ |
seq = "\\a"; |
|
ssz = 2; |
return(ml_nputstring(p, buf, strlen(buf))); |
break; |
} |
case (ROFFTok_Sp_B): |
|
seq = "\\b"; |
|
ssz = 2; |
|
break; |
|
case (ROFFTok_Sp_F): |
|
seq = "\\f"; |
|
ssz = 2; |
|
break; |
|
case (ROFFTok_Sp_N): |
|
seq = "\\n"; |
|
ssz = 2; |
|
break; |
|
case (ROFFTok_Sp_R): |
|
seq = "\\r"; |
|
ssz = 2; |
|
break; |
|
case (ROFFTok_Sp_T): |
|
seq = "\\t"; |
|
ssz = 2; |
|
break; |
|
case (ROFFTok_Sp_V): |
|
seq = "\\v"; |
|
ssz = 2; |
|
break; |
|
case (ROFFTok_Sp_0): |
|
seq = "\\0"; |
|
ssz = 2; |
|
break; |
|
case (ROFFTok_Space): |
|
seq = " "; |
|
ssz = 6; |
|
break; |
|
case (ROFFTok_Hyphen): |
|
seq = "‐"; |
|
ssz = 7; |
|
break; |
|
case (ROFFTok_Em): |
|
seq = "—"; |
|
ssz = 7; |
|
break; |
|
case (ROFFTok_En): |
|
seq = "–"; |
|
ssz = 7; |
|
break; |
|
case (ROFFTok_Ge): |
|
seq = "≥"; |
|
ssz = 7; |
|
break; |
|
case (ROFFTok_Le): |
|
seq = "≤"; |
|
ssz = 7; |
|
break; |
|
case (ROFFTok_Rquote): |
|
seq = "”"; |
|
ssz = 7; |
|
break; |
|
case (ROFFTok_Lquote): |
|
seq = "“"; |
|
ssz = 7; |
|
break; |
|
case (ROFFTok_Uparrow): |
|
seq = "↑"; |
|
ssz = 7; |
|
break; |
|
case (ROFFTok_Acute): |
|
seq = "´"; |
|
ssz = 6; |
|
break; |
|
case (ROFFTok_Grave): |
|
seq = "`"; |
|
ssz = 5; |
|
break; |
|
case (ROFFTok_Pi): |
|
seq = "π"; |
|
ssz = 6; |
|
break; |
|
case (ROFFTok_Ne): |
|
seq = "≠"; |
|
ssz = 7; |
|
break; |
|
case (ROFFTok_Lt): |
|
seq = "<"; |
|
ssz = 4; |
|
break; |
|
case (ROFFTok_Gt): |
|
seq = ">"; |
|
ssz = 4; |
|
break; |
|
case (ROFFTok_Plusmin): |
|
seq = "±"; |
|
ssz = 6; |
|
break; |
|
case (ROFFTok_Infty): |
|
seq = "∞"; |
|
ssz = 7; |
|
break; |
|
case (ROFFTok_Bar): |
|
seq = "|"; |
|
ssz = 6; |
|
break; |
|
case (ROFFTok_Nan): |
|
seq = "Nan"; |
|
ssz = 3; |
|
break; |
|
case (ROFFTok_Quote): |
|
seq = """; |
|
ssz = 6; |
|
break; |
|
case (ROFFTok_Slash): |
|
seq = "\\"; |
|
ssz = 1; |
|
break; |
|
case (ROFFTok_Null): |
|
seq = ""; |
|
ssz = 0; |
|
break; |
|
default: |
|
return(0); |
|
} |
|
break; |
|
|
|
/* Ampersand ml-escape. */ |
|
case ('&'): |
|
seq = "&"; |
|
ssz = 5; |
|
break; |
|
|
ssize_t |
/* Quotation ml-escape. */ |
ml_begintag(struct md_mbuf *p, const char *name, |
case ('"'): |
int *argc, char **argv) |
seq = """; |
{ |
ssz = 6; |
int i; |
break; |
ssize_t res, sz; |
|
|
|
res = 0; |
/* Lt ml-escape. */ |
|
case ('<'): |
|
seq = "<"; |
|
ssz = 4; |
|
break; |
|
|
if (-1 == (sz = ml_nputs(p, "<", 1))) |
/* Gt ml-escape. */ |
return(-1); |
case ('>'): |
res += sz; |
seq = ">"; |
|
ssz = 4; |
|
break; |
|
|
if (-1 == (sz = ml_puts(p, name))) |
default: |
return(-1); |
seq = &buf[i]; |
res += sz; |
ssz = 1; |
|
break; |
|
} |
|
|
for (i = 0; ROFF_ARGMAX != argc[i]; i++) { |
if (ssz > 0 && ! ml_nputs(p, seq, ssz, pos)) |
if (-1 == (sz = ml_nputs(p, " ", 1))) |
|
return(-1); |
return(-1); |
res += sz; |
|
|
|
if (-1 == (sz = ml_puts(p, tokargnames[argc[i]]))) |
|
return(-1); |
|
res += sz; |
|
|
|
if (-1 == (sz = ml_nputs(p, "=\"", 2))) |
|
return(-1); |
|
res += sz; |
|
|
|
if (-1 == (sz = ml_putstring(p, argv[i] ? |
|
argv[i] : "true"))) |
|
return(-1); |
|
res += sz; |
|
|
|
if (-1 == (sz = ml_nputs(p, "\"", 1))) |
|
return(-1); |
|
res += sz; |
|
} |
} |
|
return(1); |
if (-1 == (sz = ml_nputs(p, ">", 1))) |
|
return(-1); |
|
|
|
return(res + sz); |
|
} |
} |
|
|
|
|
ssize_t |
int |
ml_endtag(struct md_mbuf *p, const char *tag) |
ml_nputs(struct md_mbuf *p, const char *buf, size_t sz, size_t *pos) |
{ |
{ |
ssize_t res, sz; |
|
|
|
res = 0; |
if (0 == sz) |
|
return(1); |
|
|
if (-1 == (sz = ml_nputs(p, "</", 2))) |
if ( ! md_buf_puts(p, buf, sz)) |
return(-1); |
return(0); |
res += sz; |
|
|
|
if (-1 == (sz = ml_puts(p, tag))) |
if (pos) |
return(-1); |
*pos += sz; |
res += sz; |
return(1); |
|
|
if (-1 == (sz = ml_nputs(p, ">", 1))) |
|
return(-1); |
|
|
|
return(res + sz); |
|
} |
} |
|
|
|
|
ssize_t |
int |
ml_nputstring(struct md_mbuf *p, const char *buf, size_t bufsz) |
ml_puts(struct md_mbuf *p, const char *buf, size_t *pos) |
{ |
{ |
int i; |
size_t sz; |
ssize_t res, sz; |
|
|
|
res = 0; |
if (0 == (sz = strlen(buf))) |
|
return(1); |
|
|
for (i = 0; i < (int)bufsz; i++) { |
if ( ! md_buf_puts(p, buf, sz)) |
switch (buf[i]) { |
return(0); |
case ('&'): |
|
if (-1 == (sz = ml_nputs(p, "&", 5))) |
|
return(-1); |
|
break; |
|
case ('"'): |
|
if (-1 == (sz = ml_nputs(p, """, 6))) |
|
return(-1); |
|
break; |
|
case ('<'): |
|
if (-1 == (sz = ml_nputs(p, "<", 4))) |
|
return(-1); |
|
break; |
|
case ('>'): |
|
if (-1 == (sz = ml_nputs(p, ">", 4))) |
|
return(-1); |
|
break; |
|
default: |
|
if (-1 == (sz = ml_putchar(p, buf[i]))) |
|
return(-1); |
|
break; |
|
} |
|
res += sz; |
|
} |
|
return(res); |
|
} |
|
|
|
|
if (pos) |
ssize_t |
*pos += sz; |
ml_nputs(struct md_mbuf *p, const char *buf, size_t sz) |
return(1); |
{ |
|
|
|
return(0 == md_buf_puts(p, buf, sz) ? -1 : (ssize_t)sz); |
|
} |
} |
|
|
|
|
ssize_t |
int |
ml_indent(struct md_mbuf *p, int indent) |
ml_putchars(struct md_mbuf *p, char buf, size_t count, size_t *pos) |
{ |
{ |
size_t i; |
size_t i; |
ssize_t res, sz; |
|
|
|
res = sz 0; |
for (i = 0; i < count; i++) |
|
if ( ! ml_nputs(p, &buf, 1, pos)) |
|
return(0); |
|
|
/* LINTED */ |
return(1); |
for (i = 0; i < MIN(indent, MAXINDENT); i++, res += sz) |
|
if (-1 == (sz = ml_nputs(p, " ", 4))) |
|
return(-1); |
|
return(res); |
|
} |
} |