version 1.66, 2017/06/21 20:50:50 |
version 1.69, 2017/06/23 21:04:57 |
|
|
/* $OpenBSD$ */ |
/* $Id$ |
/* |
/* |
* Copyright (c) 2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv> |
* Copyright (c) 2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv> |
* Copyright (c) 2014, 2015, 2017 Ingo Schwarze <schwarze@openbsd.org> |
* Copyright (c) 2014, 2015, 2017 Ingo Schwarze <schwarze@openbsd.org> |
|
|
EQN_TOK_ABOVE, |
EQN_TOK_ABOVE, |
EQN_TOK__MAX, |
EQN_TOK__MAX, |
EQN_TOK_FUNC, |
EQN_TOK_FUNC, |
|
EQN_TOK_QUOTED, |
|
EQN_TOK_SYM, |
EQN_TOK_EOF |
EQN_TOK_EOF |
}; |
}; |
|
|
Line 517 eqn_tok_parse(struct eqn_node *ep, char **p) |
|
Line 519 eqn_tok_parse(struct eqn_node *ep, char **p) |
|
if (quoted) { |
if (quoted) { |
if (p != NULL) |
if (p != NULL) |
*p = mandoc_strndup(start, sz); |
*p = mandoc_strndup(start, sz); |
return EQN_TOK__MAX; |
return EQN_TOK_QUOTED; |
} |
} |
|
|
for (i = 0; i < EQN_TOK__MAX; i++) |
for (i = 0; i < EQN_TOK__MAX; i++) |
Line 528 eqn_tok_parse(struct eqn_node *ep, char **p) |
|
Line 530 eqn_tok_parse(struct eqn_node *ep, char **p) |
|
if (STRNEQ(start, sz, |
if (STRNEQ(start, sz, |
eqnsyms[i].str, strlen(eqnsyms[i].str))) { |
eqnsyms[i].str, strlen(eqnsyms[i].str))) { |
mandoc_asprintf(p, "\\[%s]", eqnsyms[i].sym); |
mandoc_asprintf(p, "\\[%s]", eqnsyms[i].sym); |
return EQN_TOK__MAX; |
return EQN_TOK_SYM; |
} |
} |
} |
} |
|
|
Line 571 eqn_box_alloc(struct eqn_node *ep, struct eqn_box *par |
|
Line 573 eqn_box_alloc(struct eqn_node *ep, struct eqn_box *par |
|
bp->parent = parent; |
bp->parent = parent; |
bp->parent->args++; |
bp->parent->args++; |
bp->expectargs = UINT_MAX; |
bp->expectargs = UINT_MAX; |
|
bp->font = bp->parent->font; |
bp->size = ep->gsize; |
bp->size = ep->gsize; |
|
|
if (NULL != parent->first) { |
if (NULL != parent->first) { |
Line 719 static enum rofferr |
|
Line 722 static enum rofferr |
|
eqn_parse(struct eqn_node *ep, struct eqn_box *parent) |
eqn_parse(struct eqn_node *ep, struct eqn_box *parent) |
{ |
{ |
char sym[64]; |
char sym[64]; |
struct eqn_box *cur, *fontp, *nbox; |
struct eqn_box *cur, *nbox; |
const char *cp, *cpn, *start; |
const char *cp, *cpn, *start; |
char *p; |
char *p; |
size_t sz; |
size_t sz; |
enum eqn_tok tok, subtok; |
enum eqn_tok tok, subtok; |
enum eqn_post pos; |
enum eqn_post pos; |
|
enum { CCL_LET, CCL_DIG, CCL_PUN } ccl, ccln; |
int size; |
int size; |
|
|
assert(parent != NULL); |
assert(parent != NULL); |
|
|
parent = eqn_box_makebinary(ep, EQNPOS_NONE, parent); |
parent = eqn_box_makebinary(ep, EQNPOS_NONE, parent); |
parent->type = EQN_LISTONE; |
parent->type = EQN_LISTONE; |
parent->expectargs = 1; |
parent->expectargs = 1; |
|
parent->font = EQNFONT_ROMAN; |
switch (tok) { |
switch (tok) { |
case EQN_TOK_DOTDOT: |
case EQN_TOK_DOTDOT: |
strlcpy(sym, "\\[ad]", sizeof(sym)); |
strlcpy(sym, "\\[ad]", sizeof(sym)); |
|
|
* TODO: make sure we're not in an open subexpression. |
* TODO: make sure we're not in an open subexpression. |
*/ |
*/ |
return ROFF_EQN; |
return ROFF_EQN; |
case EQN_TOK_FUNC: |
|
case EQN_TOK__MAX: |
case EQN_TOK__MAX: |
|
case EQN_TOK_FUNC: |
|
case EQN_TOK_QUOTED: |
|
case EQN_TOK_SYM: |
assert(p != NULL); |
assert(p != NULL); |
/* |
/* |
* If we already have something in the stack and we're |
* If we already have something in the stack and we're |
|
|
*/ |
*/ |
while (parent->args == parent->expectargs) |
while (parent->args == parent->expectargs) |
parent = parent->parent; |
parent = parent->parent; |
/* |
|
* Wrap well-known function names in a roman box, |
|
* unless they already are in roman context. |
|
*/ |
|
for (fontp = parent; fontp != NULL; fontp = fontp->parent) |
|
if (fontp->font != EQNFONT_NONE) |
|
break; |
|
if (tok == EQN_TOK_FUNC && |
|
(fontp == NULL || fontp->font != EQNFONT_ROMAN)) { |
|
parent = fontp = eqn_box_alloc(ep, parent); |
|
parent->type = EQN_LISTONE; |
|
parent->font = EQNFONT_ROMAN; |
|
parent->expectargs = 1; |
|
} |
|
cur = eqn_box_alloc(ep, parent); |
cur = eqn_box_alloc(ep, parent); |
cur->type = EQN_TEXT; |
cur->type = EQN_TEXT; |
cur->text = p; |
cur->text = p; |
/* |
switch (tok) { |
* If not inside any explicit font context, |
case EQN_TOK_FUNC: |
* give every letter its own box. |
cur->font = EQNFONT_ROMAN; |
*/ |
break; |
if (fontp == NULL && *p != '\0') { |
case EQN_TOK_QUOTED: |
cp = p; |
if (cur->font == EQNFONT_NONE) |
|
cur->font = EQNFONT_ITALIC; |
|
break; |
|
case EQN_TOK_SYM: |
|
break; |
|
default: |
|
if (cur->font != EQNFONT_NONE || *p == '\0') |
|
break; |
|
cpn = p - 1; |
|
ccln = CCL_LET; |
for (;;) { |
for (;;) { |
cpn = cp + 1; |
/* Advance to next character. */ |
|
cp = cpn++; |
|
ccl = ccln; |
|
ccln = isalpha((unsigned char)*cpn) ? CCL_LET : |
|
isdigit((unsigned char)*cpn) || |
|
(*cpn == '.' && (ccl == CCL_DIG || |
|
isdigit((unsigned char)cpn[1]))) ? |
|
CCL_DIG : CCL_PUN; |
|
/* No boundary before first character. */ |
|
if (cp < p) |
|
continue; |
|
cur->font = ccl == CCL_LET ? |
|
EQNFONT_ITALIC : EQNFONT_ROMAN; |
if (*cp == '\\') |
if (*cp == '\\') |
mandoc_escape(&cpn, NULL, NULL); |
mandoc_escape(&cpn, NULL, NULL); |
|
/* No boundary after last character. */ |
if (*cpn == '\0') |
if (*cpn == '\0') |
break; |
break; |
if (isalpha((unsigned char)*cp) == 0 && |
if (ccln == ccl) |
isalpha((unsigned char)*cpn) == 0) { |
|
cp = cpn; |
|
continue; |
continue; |
|
/* Boundary found, split the text. */ |
|
if (parent->args == parent->expectargs) { |
|
/* Remove the text from the tree. */ |
|
if (cur->prev == NULL) |
|
parent->first = cur->next; |
|
else |
|
cur->prev->next = NULL; |
|
parent->last = cur->prev; |
|
parent->args--; |
|
/* Set up a list instead. */ |
|
nbox = eqn_box_alloc(ep, parent); |
|
nbox->type = EQN_LIST; |
|
/* Insert the word into the list. */ |
|
nbox->first = nbox->last = cur; |
|
cur->parent = nbox; |
|
cur->prev = NULL; |
|
parent = nbox; |
} |
} |
|
/* Append a new text box. */ |
nbox = eqn_box_alloc(ep, parent); |
nbox = eqn_box_alloc(ep, parent); |
nbox->type = EQN_TEXT; |
nbox->type = EQN_TEXT; |
nbox->text = mandoc_strdup(cpn); |
nbox->text = mandoc_strdup(cpn); |
|
/* Truncate the old box. */ |
p = mandoc_strndup(cur->text, |
p = mandoc_strndup(cur->text, |
cpn - cur->text); |
cpn - cur->text); |
free(cur->text); |
free(cur->text); |
cur->text = p; |
cur->text = p; |
|
/* Setup to process the new box. */ |
cur = nbox; |
cur = nbox; |
cp = nbox->text; |
p = nbox->text; |
|
cpn = p - 1; |
|
ccln = CCL_LET; |
} |
} |
|
break; |
} |
} |
/* |
/* |
* Post-process list status. |
* Post-process list status. |