version 1.137, 2011/04/24 23:51:17 |
version 1.141, 2011/05/24 21:18:06 |
|
|
#endif |
#endif |
|
|
#include <assert.h> |
#include <assert.h> |
#include <errno.h> |
|
#include <ctype.h> |
#include <ctype.h> |
#include <limits.h> |
|
#include <stdlib.h> |
#include <stdlib.h> |
#include <string.h> |
#include <string.h> |
#include <stdio.h> |
|
|
|
#include "mandoc.h" |
#include "mandoc.h" |
#include "libroff.h" |
#include "libroff.h" |
#include "libmandoc.h" |
#include "libmandoc.h" |
|
|
|
/* Maximum number of nested if-else conditionals. */ |
#define RSTACK_MAX 128 |
#define RSTACK_MAX 128 |
|
|
enum rofft { |
enum rofft { |
|
|
ROFF_EQ, |
ROFF_EQ, |
ROFF_EN, |
ROFF_EN, |
ROFF_cblock, |
ROFF_cblock, |
ROFF_ccond, /* FIXME: remove this. */ |
ROFF_ccond, |
ROFF_USERDEF, |
ROFF_USERDEF, |
ROFF_MAX |
ROFF_MAX |
}; |
}; |
Line 127 struct roffmac { |
|
Line 125 struct roffmac { |
|
struct roffmac *next; |
struct roffmac *next; |
}; |
}; |
|
|
|
struct predef { |
|
const char *name; /* predefined input name */ |
|
const char *str; /* replacement symbol */ |
|
}; |
|
|
|
#define PREDEF(__name, __str) \ |
|
{ (__name), (__str) }, |
|
|
static enum rofferr roff_block(ROFF_ARGS); |
static enum rofferr roff_block(ROFF_ARGS); |
static enum rofferr roff_block_text(ROFF_ARGS); |
static enum rofferr roff_block_text(ROFF_ARGS); |
static enum rofferr roff_block_sub(ROFF_ARGS); |
static enum rofferr roff_block_sub(ROFF_ARGS); |
Line 198 static struct roffmac roffs[ROFF_MAX] = { |
|
Line 204 static struct roffmac roffs[ROFF_MAX] = { |
|
{ NULL, roff_userdef, NULL, NULL, 0, NULL }, |
{ NULL, roff_userdef, NULL, NULL, 0, NULL }, |
}; |
}; |
|
|
|
/* Array of injected predefined strings. */ |
|
#define PREDEFS_MAX 38 |
|
static const struct predef predefs[PREDEFS_MAX] = { |
|
#include "predefs.in" |
|
}; |
|
|
static void roff_free1(struct roff *); |
static void roff_free1(struct roff *); |
static enum rofft roff_hash_find(const char *, size_t); |
static enum rofft roff_hash_find(const char *, size_t); |
static void roff_hash_init(void); |
static void roff_hash_init(void); |
Line 206 static void roffnode_push(struct roff *, enum rofft, |
|
Line 218 static void roffnode_push(struct roff *, enum rofft, |
|
const char *, int, int); |
const char *, int, int); |
static void roffnode_pop(struct roff *); |
static void roffnode_pop(struct roff *); |
static enum rofft roff_parse(struct roff *, const char *, int *); |
static enum rofft roff_parse(struct roff *, const char *, int *); |
static int roff_parse_nat(const char *, unsigned int *); |
|
|
|
/* See roff_hash_find() */ |
/* See roff_hash_find() */ |
#define ROFF_HASH(p) (p[0] - ASCII_LO) |
#define ROFF_HASH(p) (p[0] - ASCII_LO) |
Line 232 roff_hash_init(void) |
|
Line 243 roff_hash_init(void) |
|
} |
} |
} |
} |
|
|
|
|
/* |
/* |
* Look up a roff token by its name. Returns ROFF_MAX if no macro by |
* Look up a roff token by its name. Returns ROFF_MAX if no macro by |
* the nil-terminated string name could be found. |
* the nil-terminated string name could be found. |
|
|
roff_alloc(struct regset *regs, struct mparse *parse) |
roff_alloc(struct regset *regs, struct mparse *parse) |
{ |
{ |
struct roff *r; |
struct roff *r; |
|
int i; |
|
|
r = mandoc_calloc(1, sizeof(struct roff)); |
r = mandoc_calloc(1, sizeof(struct roff)); |
r->regs = regs; |
r->regs = regs; |
Line 362 roff_alloc(struct regset *regs, struct mparse *parse) |
|
Line 373 roff_alloc(struct regset *regs, struct mparse *parse) |
|
r->rstackpos = -1; |
r->rstackpos = -1; |
|
|
roff_hash_init(); |
roff_hash_init(); |
|
|
|
for (i = 0; i < PREDEFS_MAX; i++) |
|
roff_setstr(r, predefs[i].name, predefs[i].str, 0); |
|
|
return(r); |
return(r); |
} |
} |
|
|
Line 593 roff_parse(struct roff *r, const char *buf, int *pos) |
|
Line 608 roff_parse(struct roff *r, const char *buf, int *pos) |
|
return(t); |
return(t); |
} |
} |
|
|
|
|
static int |
|
roff_parse_nat(const char *buf, unsigned int *res) |
|
{ |
|
char *ep; |
|
long lval; |
|
|
|
errno = 0; |
|
lval = strtol(buf, &ep, 10); |
|
if (buf[0] == '\0' || *ep != '\0') |
|
return(0); |
|
if ((errno == ERANGE && |
|
(lval == LONG_MAX || lval == LONG_MIN)) || |
|
(lval > INT_MAX || lval < 0)) |
|
return(0); |
|
|
|
*res = (unsigned int)lval; |
|
return(1); |
|
} |
|
|
|
|
|
/* ARGSUSED */ |
/* ARGSUSED */ |
static enum rofferr |
static enum rofferr |
roff_cblock(ROFF_ARGS) |
roff_cblock(ROFF_ARGS) |
Line 865 roff_cond_sub(ROFF_ARGS) |
|
Line 859 roff_cond_sub(ROFF_ARGS) |
|
{ |
{ |
enum rofft t; |
enum rofft t; |
enum roffrule rr; |
enum roffrule rr; |
|
char *ep; |
|
|
rr = r->last->rule; |
rr = r->last->rule; |
|
roffnode_cleanscope(r); |
|
|
/* |
/* |
* Clean out scope. If we've closed ourselves, then don't |
* If the macro is unknown, first check if it contains a closing |
* continue. |
* delimiter `\}'. If it does, close out our scope and return |
|
* the currently-scoped rule (ignore or continue). Else, drop |
|
* into the currently-scoped rule. |
*/ |
*/ |
|
|
roffnode_cleanscope(r); |
|
|
|
if (ROFF_MAX == (t = roff_parse(r, *bufp, &pos))) { |
if (ROFF_MAX == (t = roff_parse(r, *bufp, &pos))) { |
if ('\\' == (*bufp)[pos] && '}' == (*bufp)[pos + 1]) |
ep = &(*bufp)[pos]; |
return(roff_ccond |
for ( ; NULL != (ep = strchr(ep, '\\')); ep++) { |
(r, ROFF_ccond, bufp, szp, |
ep++; |
ln, pos, pos + 2, offs)); |
if ('}' != *ep) |
|
continue; |
|
*ep = '&'; |
|
roff_ccond(r, ROFF_ccond, bufp, szp, |
|
ln, pos, pos + 2, offs); |
|
break; |
|
} |
return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT); |
return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT); |
} |
} |
|
|
Line 888 roff_cond_sub(ROFF_ARGS) |
|
Line 890 roff_cond_sub(ROFF_ARGS) |
|
* if they're either structurally required (such as loops and |
* if they're either structurally required (such as loops and |
* conditionals) or a closing macro. |
* conditionals) or a closing macro. |
*/ |
*/ |
|
|
if (ROFFRULE_DENY == rr) |
if (ROFFRULE_DENY == rr) |
if ( ! (ROFFMAC_STRUCT & roffs[t].flags)) |
if ( ! (ROFFMAC_STRUCT & roffs[t].flags)) |
if (ROFF_ccond != t) |
if (ROFF_ccond != t) |
Line 898 roff_cond_sub(ROFF_ARGS) |
|
Line 901 roff_cond_sub(ROFF_ARGS) |
|
ln, ppos, pos, offs)); |
ln, ppos, pos, offs)); |
} |
} |
|
|
|
|
/* ARGSUSED */ |
/* ARGSUSED */ |
static enum rofferr |
static enum rofferr |
roff_cond_text(ROFF_ARGS) |
roff_cond_text(ROFF_ARGS) |
{ |
{ |
char *ep, *st; |
char *ep; |
enum roffrule rr; |
enum roffrule rr; |
|
|
rr = r->last->rule; |
rr = r->last->rule; |
|
roffnode_cleanscope(r); |
|
|
/* |
ep = &(*bufp)[pos]; |
* We display the value of the text if out current evaluation |
for ( ; NULL != (ep = strchr(ep, '\\')); ep++) { |
* scope permits us to do so. |
ep++; |
*/ |
if ('}' != *ep) |
|
continue; |
/* FIXME: use roff_ccond? */ |
*ep = '&'; |
|
roff_ccond(r, ROFF_ccond, bufp, szp, |
st = &(*bufp)[pos]; |
ln, pos, pos + 2, offs); |
if (NULL == (ep = strstr(st, "\\}"))) { |
|
roffnode_cleanscope(r); |
|
return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT); |
|
} |
} |
|
|
if (ep == st || (ep > st && '\\' != *(ep - 1))) |
|
roffnode_pop(r); |
|
|
|
roffnode_cleanscope(r); |
|
return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT); |
return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT); |
} |
} |
|
|
|
|
static enum roffrule |
static enum roffrule |
roff_evalcond(const char *v, int *pos) |
roff_evalcond(const char *v, int *pos) |
{ |
{ |
Line 1090 roff_nr(ROFF_ARGS) |
|
Line 1084 roff_nr(ROFF_ARGS) |
|
{ |
{ |
const char *key; |
const char *key; |
char *val; |
char *val; |
|
int iv; |
struct reg *rg; |
struct reg *rg; |
|
|
val = *bufp + pos; |
val = *bufp + pos; |
Line 1098 roff_nr(ROFF_ARGS) |
|
Line 1093 roff_nr(ROFF_ARGS) |
|
|
|
if (0 == strcmp(key, "nS")) { |
if (0 == strcmp(key, "nS")) { |
rg[(int)REG_nS].set = 1; |
rg[(int)REG_nS].set = 1; |
if ( ! roff_parse_nat(val, &rg[(int)REG_nS].v.u)) |
if ((iv = mandoc_strntou(val, strlen(val), 10)) >= 0) |
rg[(int)REG_nS].v.u = 0; |
rg[REG_nS].v.u = (unsigned)iv; |
|
else |
|
rg[(int)REG_nS].v.u = 0u; |
} |
} |
|
|
return(ROFF_IGN); |
return(ROFF_IGN); |