version 1.185, 2013/10/14 01:43:14 |
version 1.192, 2014/02/14 22:27:41 |
|
|
/* $Id$ */ |
/* $Id$ */ |
/* |
/* |
* Copyright (c) 2010, 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv> |
* Copyright (c) 2010, 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv> |
* Copyright (c) 2010, 2011, 2012, 2013 Ingo Schwarze <schwarze@openbsd.org> |
* Copyright (c) 2010-2014 Ingo Schwarze <schwarze@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 above |
* purpose with or without fee is hereby granted, provided that the above |
|
|
ROFF_ds, |
ROFF_ds, |
ROFF_el, |
ROFF_el, |
ROFF_fam, |
ROFF_fam, |
|
ROFF_hw, |
ROFF_hy, |
ROFF_hy, |
ROFF_ie, |
ROFF_ie, |
ROFF_if, |
ROFF_if, |
Line 108 struct roffreg { |
|
Line 109 struct roffreg { |
|
struct roff { |
struct roff { |
enum mparset parsetype; /* requested parse type */ |
enum mparset parsetype; /* requested parse type */ |
struct mparse *parse; /* parse point */ |
struct mparse *parse; /* parse point */ |
|
int quick; /* skip standard macro deletion */ |
struct roffnode *last; /* leaf of stack */ |
struct roffnode *last; /* leaf of stack */ |
enum roffrule rstack[RSTACK_MAX]; /* stack of !`ie' rules */ |
enum roffrule rstack[RSTACK_MAX]; /* stack of !`ie' rules */ |
char control; /* control character */ |
char control; /* control character */ |
Line 190 static int roff_getnum(const char *, int *, int *); |
|
Line 192 static int roff_getnum(const char *, int *, int *); |
|
static int roff_getop(const char *, int *, char *); |
static int roff_getop(const char *, int *, char *); |
static int roff_getregn(const struct roff *, |
static int roff_getregn(const struct roff *, |
const char *, size_t); |
const char *, size_t); |
|
static int roff_getregro(const char *name); |
static const char *roff_getstrn(const struct roff *, |
static const char *roff_getstrn(const struct roff *, |
const char *, size_t); |
const char *, size_t); |
static enum rofferr roff_it(ROFF_ARGS); |
static enum rofferr roff_it(ROFF_ARGS); |
Line 237 static struct roffmac roffs[ROFF_MAX] = { |
|
Line 240 static struct roffmac roffs[ROFF_MAX] = { |
|
{ "ds", roff_ds, NULL, NULL, 0, NULL }, |
{ "ds", roff_ds, NULL, NULL, 0, NULL }, |
{ "el", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL }, |
{ "el", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL }, |
{ "fam", roff_line_ignore, NULL, NULL, 0, NULL }, |
{ "fam", roff_line_ignore, NULL, NULL, 0, NULL }, |
|
{ "hw", roff_line_ignore, NULL, NULL, 0, NULL }, |
{ "hy", roff_line_ignore, NULL, NULL, 0, NULL }, |
{ "hy", roff_line_ignore, NULL, NULL, 0, NULL }, |
{ "ie", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL }, |
{ "ie", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL }, |
{ "if", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL }, |
{ "if", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL }, |
Line 445 roff_free1(struct roff *r) |
|
Line 449 roff_free1(struct roff *r) |
|
void |
void |
roff_reset(struct roff *r) |
roff_reset(struct roff *r) |
{ |
{ |
int i; |
|
|
|
roff_free1(r); |
roff_free1(r); |
|
|
r->control = 0; |
r->control = 0; |
|
|
for (i = 0; i < PREDEFS_MAX; i++) |
|
roff_setstr(r, predefs[i].name, predefs[i].str, 0); |
|
} |
} |
|
|
|
|
Line 466 roff_free(struct roff *r) |
|
Line 465 roff_free(struct roff *r) |
|
|
|
|
|
struct roff * |
struct roff * |
roff_alloc(enum mparset type, struct mparse *parse) |
roff_alloc(enum mparset type, struct mparse *parse, int quick) |
{ |
{ |
struct roff *r; |
struct roff *r; |
int i; |
|
|
|
r = mandoc_calloc(1, sizeof(struct roff)); |
r = mandoc_calloc(1, sizeof(struct roff)); |
r->parsetype = type; |
r->parsetype = type; |
r->parse = parse; |
r->parse = parse; |
|
r->quick = quick; |
r->rstackpos = -1; |
r->rstackpos = -1; |
|
|
roffhash_init(); |
roffhash_init(); |
|
|
for (i = 0; i < PREDEFS_MAX; i++) |
|
roff_setstr(r, predefs[i].name, predefs[i].str, 0); |
|
|
|
return(r); |
return(r); |
} |
} |
|
|
Line 644 roff_parsetext(char **bufp, size_t *szp, int pos, int |
|
Line 640 roff_parsetext(char **bufp, size_t *szp, int pos, int |
|
if ('\\' == *p) { |
if ('\\' == *p) { |
/* Skip over escapes. */ |
/* Skip over escapes. */ |
p++; |
p++; |
esc = mandoc_escape |
esc = mandoc_escape((const char **)&p, NULL, NULL); |
((const char const **)&p, NULL, NULL); |
|
if (ESCAPE_ERROR == esc) |
if (ESCAPE_ERROR == esc) |
break; |
break; |
continue; |
continue; |
Line 1358 roff_ds(ROFF_ARGS) |
|
Line 1353 roff_ds(ROFF_ARGS) |
|
} |
} |
|
|
void |
void |
roff_setreg(struct roff *r, const char *name, int val) |
roff_setreg(struct roff *r, const char *name, int val, char sign) |
{ |
{ |
struct roffreg *reg; |
struct roffreg *reg; |
|
|
Line 1373 roff_setreg(struct roff *r, const char *name, int val) |
|
Line 1368 roff_setreg(struct roff *r, const char *name, int val) |
|
reg = mandoc_malloc(sizeof(struct roffreg)); |
reg = mandoc_malloc(sizeof(struct roffreg)); |
reg->key.p = mandoc_strdup(name); |
reg->key.p = mandoc_strdup(name); |
reg->key.sz = strlen(name); |
reg->key.sz = strlen(name); |
|
reg->val = 0; |
reg->next = r->regtab; |
reg->next = r->regtab; |
r->regtab = reg; |
r->regtab = reg; |
} |
} |
|
|
reg->val = val; |
if ('+' == sign) |
|
reg->val += val; |
|
else if ('-' == sign) |
|
reg->val -= val; |
|
else |
|
reg->val = val; |
} |
} |
|
|
|
/* |
|
* Handle some predefined read-only number registers. |
|
* For now, return -1 if the requested register is not predefined; |
|
* in case a predefined read-only register having the value -1 |
|
* were to turn up, another special value would have to be chosen. |
|
*/ |
|
static int |
|
roff_getregro(const char *name) |
|
{ |
|
|
|
switch (*name) { |
|
case ('A'): /* ASCII approximation mode is always off. */ |
|
return(0); |
|
case ('g'): /* Groff compatibility mode is always on. */ |
|
return(1); |
|
case ('H'): /* Fixed horizontal resolution. */ |
|
return (24); |
|
case ('j'): /* Always adjust left margin only. */ |
|
return(0); |
|
case ('T'): /* Some output device is always defined. */ |
|
return(1); |
|
case ('V'): /* Fixed vertical resolution. */ |
|
return (40); |
|
default: |
|
return (-1); |
|
} |
|
} |
|
|
int |
int |
roff_getreg(const struct roff *r, const char *name) |
roff_getreg(const struct roff *r, const char *name) |
{ |
{ |
struct roffreg *reg; |
struct roffreg *reg; |
|
int val; |
|
|
|
if ('.' == name[0] && '\0' != name[1] && '\0' == name[2]) { |
|
val = roff_getregro(name + 1); |
|
if (-1 != val) |
|
return (val); |
|
} |
|
|
for (reg = r->regtab; reg; reg = reg->next) |
for (reg = r->regtab; reg; reg = reg->next) |
if (0 == strcmp(name, reg->key.p)) |
if (0 == strcmp(name, reg->key.p)) |
return(reg->val); |
return(reg->val); |
|
|
roff_getregn(const struct roff *r, const char *name, size_t len) |
roff_getregn(const struct roff *r, const char *name, size_t len) |
{ |
{ |
struct roffreg *reg; |
struct roffreg *reg; |
|
int val; |
|
|
|
if ('.' == name[0] && 2 == len) { |
|
val = roff_getregro(name + 1); |
|
if (-1 != val) |
|
return (val); |
|
} |
|
|
for (reg = r->regtab; reg; reg = reg->next) |
for (reg = r->regtab; reg; reg = reg->next) |
if (len == reg->key.sz && |
if (len == reg->key.sz && |
0 == strncmp(name, reg->key.p, len)) |
0 == strncmp(name, reg->key.p, len)) |
Line 1424 roff_nr(ROFF_ARGS) |
|
Line 1467 roff_nr(ROFF_ARGS) |
|
{ |
{ |
const char *key; |
const char *key; |
char *val; |
char *val; |
|
size_t sz; |
int iv; |
int iv; |
|
char sign; |
|
|
val = *bufp + pos; |
val = *bufp + pos; |
key = roff_getname(r, &val, ln, pos); |
key = roff_getname(r, &val, ln, pos); |
|
|
iv = mandoc_strntoi(val, strlen(val), 10); |
sign = *val; |
|
if ('+' == sign || '-' == sign) |
|
val++; |
|
|
roff_setreg(r, key, iv); |
sz = strspn(val, "0123456789"); |
|
iv = sz ? mandoc_strntoi(val, sz, 10) : 0; |
|
|
|
roff_setreg(r, key, iv, sign); |
|
|
return(ROFF_IGN); |
return(ROFF_IGN); |
} |
} |
|
|
Line 1483 roff_Dd(ROFF_ARGS) |
|
Line 1533 roff_Dd(ROFF_ARGS) |
|
{ |
{ |
const char *const *cp; |
const char *const *cp; |
|
|
if (MPARSE_MDOC != r->parsetype) |
if (0 == r->quick && MPARSE_MDOC != r->parsetype) |
for (cp = __mdoc_reserved; *cp; cp++) |
for (cp = __mdoc_reserved; *cp; cp++) |
roff_setstr(r, *cp, NULL, 0); |
roff_setstr(r, *cp, NULL, 0); |
|
|
Line 1496 roff_TH(ROFF_ARGS) |
|
Line 1546 roff_TH(ROFF_ARGS) |
|
{ |
{ |
const char *const *cp; |
const char *const *cp; |
|
|
if (MPARSE_MDOC != r->parsetype) |
if (0 == r->quick && MPARSE_MDOC != r->parsetype) |
for (cp = __man_reserved; *cp; cp++) |
for (cp = __man_reserved; *cp; cp++) |
roff_setstr(r, *cp, NULL, 0); |
roff_setstr(r, *cp, NULL, 0); |
|
|
Line 1718 roff_userdef(ROFF_ARGS) |
|
Line 1768 roff_userdef(ROFF_ARGS) |
|
|
|
/* |
/* |
* Collect pointers to macro argument strings |
* Collect pointers to macro argument strings |
* and null-terminate them. |
* and NUL-terminate them. |
*/ |
*/ |
cp = *bufp + pos; |
cp = *bufp + pos; |
for (i = 0; i < 9; i++) |
for (i = 0; i < 9; i++) |
Line 1887 static const char * |
|
Line 1937 static const char * |
|
roff_getstrn(const struct roff *r, const char *name, size_t len) |
roff_getstrn(const struct roff *r, const char *name, size_t len) |
{ |
{ |
const struct roffkv *n; |
const struct roffkv *n; |
|
int i; |
|
|
for (n = r->strtab; n; n = n->next) |
for (n = r->strtab; n; n = n->next) |
if (0 == strncmp(name, n->key.p, len) && |
if (0 == strncmp(name, n->key.p, len) && |
'\0' == n->key.p[(int)len]) |
'\0' == n->key.p[(int)len]) |
return(n->val.p); |
return(n->val.p); |
|
|
|
for (i = 0; i < PREDEFS_MAX; i++) |
|
if (0 == strncmp(name, predefs[i].name, len) && |
|
'\0' == predefs[i].name[(int)len]) |
|
return(predefs[i].str); |
|
|
return(NULL); |
return(NULL); |
} |
} |