version 1.72, 2019/03/25 17:28:32 |
version 1.73, 2019/03/25 23:14:44 |
|
|
*/ |
*/ |
struct parse { |
struct parse { |
XML_Parser xml; |
XML_Parser xml; |
enum nodeid node; /* current (NODE_ROOT if pre-tree) */ |
|
const char *fname; /* filename */ |
const char *fname; /* filename */ |
int stop; /* should we stop now? */ |
int stop; /* should we stop now? */ |
#define PARSE_EQN 1 |
#define PARSE_EQN 1 |
Line 249 xml_char(void *arg, const XML_Char *p, int sz) |
|
Line 248 xml_char(void *arg, const XML_Char *p, int sz) |
|
struct pnode *dat; |
struct pnode *dat; |
int i; |
int i; |
|
|
/* Stopped or no tree yet. */ |
if (ps->stop) |
if (ps->stop || ps->node == NODE_ROOT) |
|
return; |
return; |
|
|
assert(ps->cur != NULL); |
|
|
|
/* |
/* |
* Are we in the midst of processing text? |
* Are we in the midst of processing text? |
* If we're not processing text right now, then create a text |
* If we're not processing text right now, then create a text |
Line 262 xml_char(void *arg, const XML_Char *p, int sz) |
|
Line 258 xml_char(void *arg, const XML_Char *p, int sz) |
|
* However, don't do so unless we have some non-whitespace to |
* However, don't do so unless we have some non-whitespace to |
* process: strip out all leading whitespace to be sure. |
* process: strip out all leading whitespace to be sure. |
*/ |
*/ |
if (ps->node != NODE_TEXT) { |
if (ps->cur->node != NODE_TEXT) { |
for (i = 0; i < sz; i++) |
for (i = 0; i < sz; i++) |
if ( ! isspace((unsigned char)p[i])) |
if ( ! isspace((unsigned char)p[i])) |
break; |
break; |
Line 276 xml_char(void *arg, const XML_Char *p, int sz) |
|
Line 272 xml_char(void *arg, const XML_Char *p, int sz) |
|
exit(1); |
exit(1); |
} |
} |
|
|
dat->node = ps->node = NODE_TEXT; |
dat->node = NODE_TEXT; |
dat->parent = ps->cur; |
dat->parent = ps->cur; |
TAILQ_INIT(&dat->childq); |
TAILQ_INIT(&dat->childq); |
TAILQ_INIT(&dat->attrq); |
TAILQ_INIT(&dat->attrq); |
Line 337 xml_elem_start(void *arg, const XML_Char *name, const |
|
Line 333 xml_elem_start(void *arg, const XML_Char *name, const |
|
return; |
return; |
|
|
/* Close out text node, if applicable... */ |
/* Close out text node, if applicable... */ |
if (ps->node == NODE_TEXT) { |
if (ps->cur != NULL && ps->cur->node == NODE_TEXT) { |
pnode_trim(ps->cur); |
pnode_trim(ps->cur); |
ps->cur = ps->cur->parent; |
ps->cur = ps->cur->parent; |
ps->node = ps->cur->node; |
|
} |
} |
|
|
for (node = nodes; node->name != NULL; node++) |
for (node = nodes; node->name != NULL; node++) |
Line 348 xml_elem_start(void *arg, const XML_Char *name, const |
|
Line 343 xml_elem_start(void *arg, const XML_Char *name, const |
|
break; |
break; |
|
|
if (node->name == NULL) { |
if (node->name == NULL) { |
if (ps->node == NODE_ROOT) |
|
return; |
|
fprintf(stderr, "%s:%zu:%zu: unknown node \"%s\"\n", |
fprintf(stderr, "%s:%zu:%zu: unknown node \"%s\"\n", |
ps->fname, XML_GetCurrentLineNumber(ps->xml), |
ps->fname, XML_GetCurrentLineNumber(ps->xml), |
XML_GetCurrentColumnNumber(ps->xml), name); |
XML_GetCurrentColumnNumber(ps->xml), name); |
ps->stop = 1; |
ps->stop = 1; |
return; |
return; |
} else if (ps->node == NODE_ROOT && ps->root != NULL) { |
|
fprintf(stderr, "%s:%zu:%zu: multiple refentries\n", |
|
ps->fname, XML_GetCurrentLineNumber(ps->xml), |
|
XML_GetCurrentColumnNumber(ps->xml)); |
|
ps->stop = 1; |
|
return; |
|
} |
} |
|
|
if (node->node == NODE_INLINEEQUATION) |
if (node->node == NODE_INLINEEQUATION) |
Line 371 xml_elem_start(void *arg, const XML_Char *name, const |
|
Line 358 xml_elem_start(void *arg, const XML_Char *name, const |
|
exit(1); |
exit(1); |
} |
} |
|
|
dat->node = ps->node = node->node; |
dat->node = node->node; |
dat->parent = ps->cur; |
dat->parent = ps->cur; |
TAILQ_INIT(&dat->childq); |
TAILQ_INIT(&dat->childq); |
TAILQ_INIT(&dat->attrq); |
TAILQ_INIT(&dat->attrq); |
Line 410 xml_elem_start(void *arg, const XML_Char *name, const |
|
Line 397 xml_elem_start(void *arg, const XML_Char *name, const |
|
pattr->rawval = strdup(att[1]); |
pattr->rawval = strdup(att[1]); |
TAILQ_INSERT_TAIL(&dat->attrq, pattr, child); |
TAILQ_INSERT_TAIL(&dat->attrq, pattr, child); |
} |
} |
|
|
} |
} |
|
|
/* |
/* |
* Roll up the parse tree. |
* Roll up the parse tree. |
* If we're at a text node, roll that one up first. |
* If we're at a text node, roll that one up first. |
* If we hit the root, then assign ourselves as the NODE_ROOT. |
|
*/ |
*/ |
static void |
static void |
xml_elem_end(void *arg, const XML_Char *name) |
xml_elem_end(void *arg, const XML_Char *name) |
{ |
{ |
struct parse *ps = arg; |
struct parse *ps = arg; |
|
|
|
if (ps->stop) |
|
return; |
|
|
/* FIXME: find a better way to ditch other namespaces. */ |
/* FIXME: find a better way to ditch other namespaces. */ |
if (ps->stop || ps->node == NODE_ROOT) |
if (strcmp(name, "xi:include") == 0) |
return; |
return; |
else if (strcmp(name, "xi:include") == 0) |
|
return; |
|
|
|
/* Close out text node, if applicable... */ |
/* Close out text node, if applicable... */ |
if (ps->node == NODE_TEXT) { |
if (ps->cur->node == NODE_TEXT) { |
pnode_trim(ps->cur); |
pnode_trim(ps->cur); |
ps->cur = ps->cur->parent; |
ps->cur = ps->cur->parent; |
ps->node = ps->cur->node; |
|
} |
} |
|
ps->cur = ps->cur->parent; |
if ((ps->cur = ps->cur->parent) == NULL) |
|
ps->node = NODE_ROOT; |
|
else |
|
ps->node = ps->cur->node; |
|
} |
} |
|
|
/* |
/* |
Line 500 pnode_getattr(struct pnode *pn, enum attrkey key) |
|
Line 481 pnode_getattr(struct pnode *pn, enum attrkey key) |
|
{ |
{ |
struct pattr *ap; |
struct pattr *ap; |
|
|
|
if (pn == NULL) |
|
return ATTRVAL__MAX; |
TAILQ_FOREACH(ap, &pn->attrq, child) |
TAILQ_FOREACH(ap, &pn->attrq, child) |
if (ap->key == key) |
if (ap->key == key) |
return ap->val; |
return ap->val; |
Line 515 pnode_getattr_raw(struct pnode *pn, enum attrkey key, |
|
Line 498 pnode_getattr_raw(struct pnode *pn, enum attrkey key, |
|
{ |
{ |
struct pattr *ap; |
struct pattr *ap; |
|
|
|
if (pn == NULL) |
|
return defval; |
TAILQ_FOREACH(ap, &pn->attrq, child) |
TAILQ_FOREACH(ap, &pn->attrq, child) |
if (ap->key == key) |
if (ap->key == key) |
return ap->val == ATTRVAL__MAX ? ap->rawval : |
return ap->val == ATTRVAL__MAX ? ap->rawval : |
Line 1604 readfile(XML_Parser xp, int fd, |
|
Line 1589 readfile(XML_Parser xp, int fd, |
|
int rc; |
int rc; |
ssize_t ssz; |
ssize_t ssz; |
|
|
memset(&p, 0, sizeof(struct parse)); |
memset(&p, 0, sizeof(p)); |
|
|
p.fname = fn; |
p.fname = fn; |
p.xml = xp; |
p.xml = xp; |
|
|