version 1.23, 2019/04/08 14:37:31 |
version 1.29, 2019/04/09 15:23:51 |
|
|
*/ |
*/ |
#include <assert.h> |
#include <assert.h> |
#include <ctype.h> |
#include <ctype.h> |
|
#include <errno.h> |
|
#include <fcntl.h> |
|
#include <libgen.h> |
#include <stdarg.h> |
#include <stdarg.h> |
#include <stdio.h> |
#include <stdio.h> |
#include <stdlib.h> |
#include <stdlib.h> |
Line 210 static const struct element elements[] = { |
|
Line 213 static const struct element elements[] = { |
|
{ "varname", NODE_VARNAME }, |
{ "varname", NODE_VARNAME }, |
{ "warning", NODE_WARNING }, |
{ "warning", NODE_WARNING }, |
{ "wordasword", NODE_WORDASWORD }, |
{ "wordasword", NODE_WORDASWORD }, |
{ "xi:include", NODE_DELETE_WARN }, |
{ "xi:include", NODE_INCLUDE }, |
{ "year", NODE_YEAR }, |
{ "year", NODE_YEAR }, |
{ NULL, NODE_IGNORE } |
{ NULL, NODE_IGNORE } |
}; |
}; |
Line 273 static const struct entity entities[] = { |
|
Line 276 static const struct entity entities[] = { |
|
|
|
static size_t parse_string(struct parse *, char *, size_t, |
static size_t parse_string(struct parse *, char *, size_t, |
enum pstate *, int); |
enum pstate *, int); |
|
static void parse_fd(struct parse *, int); |
|
|
|
|
static void |
static void |
|
fatal(struct parse *p) |
|
{ |
|
fprintf(stderr, "%s:%d:%d: FATAL: ", p->fname, p->line, p->col); |
|
perror(NULL); |
|
exit(6); |
|
} |
|
|
|
static void |
error_msg(struct parse *p, const char *fmt, ...) |
error_msg(struct parse *p, const char *fmt, ...) |
{ |
{ |
va_list ap; |
va_list ap; |
|
|
fprintf(stderr, "%s:%d:%d: ", p->fname, p->line, p->col); |
fprintf(stderr, "%s:%d:%d: ERROR: ", p->fname, p->line, p->col); |
va_start(ap, fmt); |
va_start(ap, fmt); |
vfprintf(stderr, fmt, ap); |
vfprintf(stderr, fmt, ap); |
va_end(ap); |
va_end(ap); |
fputc('\n', stderr); |
fputc('\n', stderr); |
p->tree->flags |= TREE_FAIL; |
p->tree->flags |= TREE_ERROR; |
} |
} |
|
|
static void |
static void |
Line 296 warn_msg(struct parse *p, const char *fmt, ...) |
|
Line 308 warn_msg(struct parse *p, const char *fmt, ...) |
|
if ((p->flags & PFLAG_WARN) == 0) |
if ((p->flags & PFLAG_WARN) == 0) |
return; |
return; |
|
|
fprintf(stderr, "%s:%d:%d: warning: ", p->fname, p->line, p->col); |
fprintf(stderr, "%s:%d:%d: WARNING: ", p->fname, p->line, p->col); |
va_start(ap, fmt); |
va_start(ap, fmt); |
vfprintf(stderr, fmt, ap); |
vfprintf(stderr, fmt, ap); |
va_end(ap); |
va_end(ap); |
fputc('\n', stderr); |
fputc('\n', stderr); |
|
p->tree->flags |= TREE_WARN; |
} |
} |
|
|
/* |
/* |
Line 323 xml_char(struct parse *ps, const char *p, int sz) |
|
Line 336 xml_char(struct parse *ps, const char *p, int sz) |
|
} |
} |
|
|
if (ps->cur->node != NODE_TEXT) { |
if (ps->cur->node != NODE_TEXT) { |
if ((dat = calloc(1, sizeof(*dat))) == NULL) { |
if ((dat = calloc(1, sizeof(*dat))) == NULL) |
perror(NULL); |
fatal(ps); |
exit(1); |
|
} |
|
dat->node = NODE_TEXT; |
dat->node = NODE_TEXT; |
dat->spc = (ps->flags & PFLAG_SPC) != 0; |
dat->spc = (ps->flags & PFLAG_SPC) != 0; |
dat->parent = ps->cur; |
dat->parent = ps->cur; |
Line 344 xml_char(struct parse *ps, const char *p, int sz) |
|
Line 355 xml_char(struct parse *ps, const char *p, int sz) |
|
|
|
assert(sz >= 0); |
assert(sz >= 0); |
newsz = ps->cur->bsz + (ps->cur->bsz && (ps->flags & PFLAG_SPC)) + sz; |
newsz = ps->cur->bsz + (ps->cur->bsz && (ps->flags & PFLAG_SPC)) + sz; |
ps->cur->b = realloc(ps->cur->b, newsz + 1); |
if ((ps->cur->b = realloc(ps->cur->b, newsz + 1)) == NULL) |
if (ps->cur->b == NULL) { |
fatal(ps); |
perror(NULL); |
|
exit(1); |
|
} |
|
if (ps->cur->bsz && (ps->flags & PFLAG_SPC)) |
if (ps->cur->bsz && (ps->flags & PFLAG_SPC)) |
ps->cur->b[ps->cur->bsz++] = ' '; |
ps->cur->b[ps->cur->bsz++] = ' '; |
memcpy(ps->cur->b + ps->cur->bsz, p, sz); |
memcpy(ps->cur->b + ps->cur->bsz, p, sz); |
Line 405 xml_entity(struct parse *p, const char *name) |
|
Line 413 xml_entity(struct parse *p, const char *name) |
|
TAILQ_FOREACH(dat, &p->doctype->childq, child) { |
TAILQ_FOREACH(dat, &p->doctype->childq, child) { |
if ((ccp = pnode_getattr_raw(dat, |
if ((ccp = pnode_getattr_raw(dat, |
ATTRKEY_NAME, NULL)) == NULL || |
ATTRKEY_NAME, NULL)) == NULL || |
strcmp(ccp, name) != 0 || |
strcmp(ccp, name) != 0) |
(ccp = pnode_getattr_raw(dat, |
|
ATTRKEY_DEFINITION, NULL)) == NULL) |
|
continue; |
continue; |
if ((cp = strdup(ccp)) == NULL) { |
if ((ccp = pnode_getattr_raw(dat, |
perror(NULL); |
ATTRKEY_SYSTEM, NULL)) != NULL) { |
exit(1); |
parse_file(p, -1, ccp); |
|
p->flags &= ~PFLAG_SPC; |
|
return; |
} |
} |
|
if ((ccp = pnode_getattr_raw(dat, |
|
ATTRKEY_DEFINITION, NULL)) == NULL) |
|
continue; |
|
if ((cp = strdup(ccp)) == NULL) |
|
fatal(p); |
pstate = PARSE_ELEM; |
pstate = PARSE_ELEM; |
parse_string(p, cp, strlen(cp), &pstate, 0); |
parse_string(p, cp, strlen(cp), &pstate, 0); |
p->flags &= ~PFLAG_SPC; |
p->flags &= ~PFLAG_SPC; |
Line 426 xml_entity(struct parse *p, const char *name) |
|
Line 439 xml_entity(struct parse *p, const char *name) |
|
|
|
/* Create, append, and close out an entity node. */ |
/* Create, append, and close out an entity node. */ |
if ((dat = calloc(1, sizeof(*dat))) == NULL || |
if ((dat = calloc(1, sizeof(*dat))) == NULL || |
(dat->b = dat->real = strdup(entity->roff)) == NULL) { |
(dat->b = dat->real = strdup(entity->roff)) == NULL) |
perror(NULL); |
fatal(p); |
exit(1); |
|
} |
|
dat->node = NODE_ESCAPE; |
dat->node = NODE_ESCAPE; |
dat->bsz = strlen(dat->b); |
dat->bsz = strlen(dat->b); |
dat->spc = (p->flags & PFLAG_SPC) != 0; |
dat->spc = (p->flags & PFLAG_SPC) != 0; |
Line 492 xml_elem_start(struct parse *ps, const char *name) |
|
Line 503 xml_elem_start(struct parse *ps, const char *name) |
|
if (ps->tree->flags & TREE_CLOSED && ps->cur->parent == NULL) |
if (ps->tree->flags & TREE_CLOSED && ps->cur->parent == NULL) |
warn_msg(ps, "element after end of document: <%s>", name); |
warn_msg(ps, "element after end of document: <%s>", name); |
|
|
if ((dat = calloc(1, sizeof(*dat))) == NULL) { |
if ((dat = calloc(1, sizeof(*dat))) == NULL) |
perror(NULL); |
fatal(ps); |
exit(1); |
|
} |
|
|
|
/* |
/* |
* Nodes that begin a new macro or request line or start by |
* Nodes that begin a new macro or request line or start by |
Line 584 xml_attrkey(struct parse *ps, const char *name) |
|
Line 593 xml_attrkey(struct parse *ps, const char *name) |
|
ps->flags &= ~PFLAG_ATTR; |
ps->flags &= ~PFLAG_ATTR; |
return; |
return; |
} |
} |
if ((attr = calloc(1, sizeof(*attr))) == NULL) { |
if ((attr = calloc(1, sizeof(*attr))) == NULL) |
perror(NULL); |
fatal(ps); |
exit(1); |
|
} |
|
attr->key = key; |
attr->key = key; |
attr->val = ATTRVAL__MAX; |
attr->val = ATTRVAL__MAX; |
if (value == NULL) { |
if (value == NULL) { |
attr->rawval = NULL; |
attr->rawval = NULL; |
ps->flags |= PFLAG_ATTR; |
ps->flags |= PFLAG_ATTR; |
} else { |
} else { |
if ((attr->rawval = strdup(value)) == NULL) { |
if ((attr->rawval = strdup(value)) == NULL) |
perror(NULL); |
fatal(ps); |
exit(1); |
|
} |
|
ps->flags &= ~PFLAG_ATTR; |
ps->flags &= ~PFLAG_ATTR; |
} |
} |
TAILQ_INSERT_TAIL(&ps->cur->attrq, attr, child); |
TAILQ_INSERT_TAIL(&ps->cur->attrq, attr, child); |
Line 616 xml_attrval(struct parse *ps, const char *name) |
|
Line 622 xml_attrval(struct parse *ps, const char *name) |
|
if ((attr = TAILQ_LAST(&ps->cur->attrq, pattrq)) == NULL) |
if ((attr = TAILQ_LAST(&ps->cur->attrq, pattrq)) == NULL) |
return; |
return; |
if ((attr->val = attrval_parse(name)) == ATTRVAL__MAX && |
if ((attr->val = attrval_parse(name)) == ATTRVAL__MAX && |
(attr->rawval = strdup(name)) == NULL) { |
(attr->rawval = strdup(name)) == NULL) |
perror(NULL); |
fatal(ps); |
exit(1); |
ps->flags &= ~PFLAG_ATTR; |
} |
|
} |
} |
|
|
/* |
/* |
|
|
xml_elem_end(struct parse *ps, const char *name) |
xml_elem_end(struct parse *ps, const char *name) |
{ |
{ |
const struct element *elem; |
const struct element *elem; |
|
struct pnode *n; |
|
const char *cp; |
enum nodeid node; |
enum nodeid node; |
|
|
/* |
/* |
Line 660 xml_elem_end(struct parse *ps, const char *name) |
|
Line 667 xml_elem_end(struct parse *ps, const char *name) |
|
break; |
break; |
case NODE_IGNORE: |
case NODE_IGNORE: |
break; |
break; |
|
case NODE_INCLUDE: |
|
n = ps->cur; |
|
ps->cur = ps->cur->parent; |
|
cp = pnode_getattr_raw(n, ATTRKEY_HREF, NULL); |
|
if (cp == NULL) |
|
error_msg(ps, "<xi:include> element " |
|
"without href attribute"); |
|
else |
|
parse_file(ps, -1, cp); |
|
pnode_unlink(n); |
|
ps->flags &= ~PFLAG_SPC; |
|
break; |
case NODE_DOCTYPE: |
case NODE_DOCTYPE: |
ps->flags &= ~PFLAG_EEND; |
ps->flags &= ~PFLAG_EEND; |
/* FALLTHROUGH */ |
/* FALLTHROUGH */ |
Line 950 parse_string(struct parse *p, char *b, size_t rlen, |
|
Line 969 parse_string(struct parse *p, char *b, size_t rlen, |
|
/* Process text up to the next tag, entity, or EOL. */ |
/* Process text up to the next tag, entity, or EOL. */ |
|
|
} else { |
} else { |
advance(p, b, rlen, &pend, "<&", refill); |
advance(p, b, rlen, &pend, |
|
p->ncur == NODE_DOCTYPE ? "<&]" : "<&", |
|
refill); |
xml_char(p, b + poff, pend - poff); |
xml_char(p, b + poff, pend - poff); |
} |
} |
} |
} |
return poff; |
return poff; |
} |
} |
|
|
struct ptree * |
|
parse_file(struct parse *p, int fd, const char *fname) |
/* |
|
* The read loop. |
|
* If the previous token was incomplete and asked for more input, |
|
* we have to enter the read loop once more even on EOF. |
|
* Once rsz is 0, incomplete tokens will no longer ask for more input |
|
* but instead use whatever there is, and then exit the read loop. |
|
* The minus one on the size limit for read(2) is needed such that |
|
* advance() can set b[rlen] to NUL when needed. |
|
*/ |
|
static void |
|
parse_fd(struct parse *p, int fd) |
{ |
{ |
char b[4096]; |
char b[4096]; |
ssize_t rsz; /* Return value from read(2). */ |
ssize_t rsz; /* Return value from read(2). */ |
Line 966 parse_file(struct parse *p, int fd, const char *fname) |
|
Line 997 parse_file(struct parse *p, int fd, const char *fname) |
|
size_t poff; /* Parse offset in b[]. */ |
size_t poff; /* Parse offset in b[]. */ |
enum pstate pstate; |
enum pstate pstate; |
|
|
p->fname = fname; |
|
p->nline = 1; |
|
p->ncol = 1; |
|
pstate = PARSE_ELEM; |
|
rlen = 0; |
rlen = 0; |
|
pstate = PARSE_ELEM; |
/* |
|
* Read loop. |
|
* |
|
* If the previous token was incomplete and asked for more |
|
* input, we have to enter the read loop once more even on EOF. |
|
* Once rsz is 0, incomplete tokens will no longer ask |
|
* for more input but instead use whatever there is, |
|
* and then exit the read loop. |
|
* The minus one on the size limit for read(2) is needed |
|
* such that advance() can set b[rlen] to NUL when needed. |
|
*/ |
|
|
|
while ((rsz = read(fd, b + rlen, sizeof(b) - rlen - 1)) >= 0 && |
while ((rsz = read(fd, b + rlen, sizeof(b) - rlen - 1)) >= 0 && |
(rlen += rsz) > 0) { |
(rlen += rsz) > 0) { |
poff = parse_string(p, b, rlen, &pstate, rsz > 0); |
poff = parse_string(p, b, rlen, &pstate, rsz > 0); |
Line 992 parse_file(struct parse *p, int fd, const char *fname) |
|
Line 1007 parse_file(struct parse *p, int fd, const char *fname) |
|
rlen -= poff; |
rlen -= poff; |
memmove(b, b + poff, rlen); |
memmove(b, b + poff, rlen); |
} |
} |
if (rsz < 0) { |
if (rsz < 0) |
perror(fname); |
error_msg(p, "read: %s", strerror(errno)); |
p->tree->flags |= TREE_FAIL; |
} |
|
|
|
/* |
|
* Open and parse a file. |
|
*/ |
|
struct ptree * |
|
parse_file(struct parse *p, int fd, const char *fname) |
|
{ |
|
const char *save_fname; |
|
int save_line, save_col; |
|
|
|
/* Save and initialize reporting data. */ |
|
|
|
save_fname = p->fname; |
|
save_line = p->nline; |
|
save_col = p->ncol; |
|
p->fname = fname; |
|
p->line = 0; |
|
p->col = 0; |
|
|
|
/* Open the file, unless it is already open. */ |
|
|
|
if (fd == -1 && (fd = open(fname, O_RDONLY, 0)) == -1) { |
|
error_msg(p, "open: %s", strerror(errno)); |
|
p->fname = save_fname; |
|
return p->tree; |
} |
} |
pnode_closetext(p); |
|
if ((p->tree->flags & TREE_CLOSED) == 0) |
/* |
warn_msg(p, "document not closed"); |
* After opening the starting file, change to the directory it |
pnode_unlink(p->doctype); |
* is located in, in case it wants to include any further files, |
|
* which are typically given with relative paths in DocBook. |
|
* Do this on a best-effort basis; don't complain about failure. |
|
*/ |
|
|
|
if (save_fname == NULL && (fname = dirname(fname)) != NULL && |
|
strcmp(fname, ".") != 0) |
|
(void)chdir(fname); |
|
|
|
/* Run the read loop. */ |
|
|
|
p->nline = 1; |
|
p->ncol = 1; |
|
parse_fd(p, fd); |
|
|
|
/* On the top level, finalize the parse tree. */ |
|
|
|
if (save_fname == NULL) { |
|
pnode_closetext(p); |
|
if (p->tree->root == NULL) |
|
error_msg(p, "empty document"); |
|
else if ((p->tree->flags & TREE_CLOSED) == 0) |
|
warn_msg(p, "document not closed"); |
|
pnode_unlink(p->doctype); |
|
} |
|
|
|
/* Clean up. */ |
|
|
|
if (fd != STDIN_FILENO) |
|
close(fd); |
|
p->fname = save_fname; |
|
p->nline = save_line; |
|
p->ncol = save_col; |
return p->tree; |
return p->tree; |
} |
} |