version 1.51, 2019/04/24 18:38:02 |
version 1.60, 2019/05/07 23:41:48 |
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
*/ |
*/ |
|
#include <sys/types.h> |
|
|
#include <assert.h> |
#include <assert.h> |
#include <ctype.h> |
#include <ctype.h> |
#include <errno.h> |
#include <errno.h> |
|
|
#include <string.h> |
#include <string.h> |
#include <unistd.h> |
#include <unistd.h> |
|
|
|
#include "xmalloc.h" |
#include "node.h" |
#include "node.h" |
#include "parse.h" |
#include "parse.h" |
|
|
Line 94 static const struct alias aliases[] = { |
|
Line 97 static const struct alias aliases[] = { |
|
{ "imageobject", NODE_IGNORE }, |
{ "imageobject", NODE_IGNORE }, |
{ "indexterm", NODE_DELETE }, |
{ "indexterm", NODE_DELETE }, |
{ "informaltable", NODE_TABLE }, |
{ "informaltable", NODE_TABLE }, |
|
{ "jobtitle", NODE_IGNORE }, |
{ "keycap", NODE_KEYSYM }, |
{ "keycap", NODE_KEYSYM }, |
{ "keycode", NODE_IGNORE }, |
{ "keycode", NODE_IGNORE }, |
|
{ "keycombo", NODE_IGNORE }, |
{ "mediaobject", NODE_BLOCKQUOTE }, |
{ "mediaobject", NODE_BLOCKQUOTE }, |
|
{ "orgdiv", NODE_IGNORE }, |
{ "orgname", NODE_IGNORE }, |
{ "orgname", NODE_IGNORE }, |
{ "othercredit", NODE_AUTHOR }, |
{ "othercredit", NODE_AUTHOR }, |
{ "othername", NODE_PERSONNAME }, |
{ "othername", NODE_PERSONNAME }, |
Line 104 static const struct alias aliases[] = { |
|
Line 110 static const struct alias aliases[] = { |
|
{ "phrase", NODE_IGNORE }, |
{ "phrase", NODE_IGNORE }, |
{ "primary", NODE_DELETE }, |
{ "primary", NODE_DELETE }, |
{ "property", NODE_PARAMETER }, |
{ "property", NODE_PARAMETER }, |
|
{ "reference", NODE_SECTION }, |
{ "refsect1", NODE_SECTION }, |
{ "refsect1", NODE_SECTION }, |
{ "refsect2", NODE_SECTION }, |
{ "refsect2", NODE_SECTION }, |
{ "refsect3", NODE_SECTION }, |
{ "refsect3", NODE_SECTION }, |
Line 125 static const struct alias aliases[] = { |
|
Line 132 static const struct alias aliases[] = { |
|
{ "trademark", NODE_IGNORE }, |
{ "trademark", NODE_IGNORE }, |
{ "ulink", NODE_LINK }, |
{ "ulink", NODE_LINK }, |
{ "userinput", NODE_LITERAL }, |
{ "userinput", NODE_LITERAL }, |
{ "year", NODE_IGNORE }, |
|
{ NULL, NODE_IGNORE } |
{ NULL, NODE_IGNORE } |
}; |
}; |
|
|
Line 191 static void parse_fd(struct parse *, int); |
|
Line 197 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; |
Line 256 xml_text(struct parse *p, const char *word, int sz) |
|
Line 254 xml_text(struct parse *p, const char *word, int sz) |
|
newsz = oldsz + sz; |
newsz = oldsz + sz; |
if (oldsz && (p->flags & PFLAG_SPC)) |
if (oldsz && (p->flags & PFLAG_SPC)) |
newsz++; |
newsz++; |
if ((n->b = realloc(n->b, newsz + 1)) == NULL) |
n->b = xrealloc(n->b, newsz + 1); |
fatal(p); |
|
if (oldsz && (p->flags & PFLAG_SPC)) |
if (oldsz && (p->flags & PFLAG_SPC)) |
n->b[oldsz++] = ' '; |
n->b[oldsz++] = ' '; |
memcpy(n->b + oldsz, word, sz); |
memcpy(n->b + oldsz, word, sz); |
Line 271 xml_text(struct parse *p, const char *word, int sz) |
|
Line 268 xml_text(struct parse *p, const char *word, int sz) |
|
|
|
/* Create a new text node. */ |
/* Create a new text node. */ |
|
|
if ((n = pnode_alloc(p->cur)) == NULL) |
n = pnode_alloc(p->cur); |
fatal(p); |
|
n->node = NODE_TEXT; |
n->node = NODE_TEXT; |
n->flags = ((p->flags & PFLAG_LINE) ? NFLAG_LINE : 0) | |
if (p->flags & PFLAG_LINE && TAILQ_PREV(n, pnodeq, child) != NULL) |
((p->flags & PFLAG_SPC) ? NFLAG_SPC : 0); |
n->flags |= NFLAG_LINE; |
|
if (p->flags & PFLAG_SPC) |
|
n->flags |= NFLAG_SPC; |
p->flags &= ~(PFLAG_LINE | PFLAG_SPC); |
p->flags &= ~(PFLAG_LINE | PFLAG_SPC); |
|
|
/* |
/* |
Line 306 xml_text(struct parse *p, const char *word, int sz) |
|
Line 304 xml_text(struct parse *p, const char *word, int sz) |
|
i = 0; |
i = 0; |
while (i < sz && !isspace((unsigned char)word[i])) |
while (i < sz && !isspace((unsigned char)word[i])) |
i++; |
i++; |
if ((n->b = strndup(word, i)) == NULL) |
n->b = xstrndup(word, i); |
fatal(p); |
|
if (i == sz) |
if (i == sz) |
return; |
return; |
while (i < sz && isspace((unsigned char)word[i])) |
while (i < sz && isspace((unsigned char)word[i])) |
Line 319 xml_text(struct parse *p, const char *word, int sz) |
|
Line 316 xml_text(struct parse *p, const char *word, int sz) |
|
|
|
/* Put any remaining text into a second node. */ |
/* Put any remaining text into a second node. */ |
|
|
if ((n = pnode_alloc(p->cur)) == NULL) |
n = pnode_alloc(p->cur); |
fatal(p); |
|
n->node = NODE_TEXT; |
n->node = NODE_TEXT; |
n->flags |= NFLAG_SPC; |
n->flags |= NFLAG_SPC; |
word += i; |
word += i; |
sz -= i; |
sz -= i; |
} |
} |
if ((n->b = strndup(word, sz)) == NULL) |
n->b = xstrndup(word, sz); |
fatal(p); |
|
|
|
/* The new node remains open for later pnode_closetext(). */ |
/* The new node remains open for later pnode_closetext(). */ |
|
|
Line 361 pnode_closetext(struct parse *p, int check_last_word) |
|
Line 356 pnode_closetext(struct parse *p, int check_last_word) |
|
|
|
while (cp > n->b && !isspace((unsigned char)cp[-1])) |
while (cp > n->b && !isspace((unsigned char)cp[-1])) |
cp--; |
cp--; |
|
last_word = cp; |
|
while (cp > n->b && isspace((unsigned char)cp[-1])) |
|
cp--; |
if (cp == n->b) |
if (cp == n->b) |
return; |
return; |
|
*cp = '\0'; |
|
|
last_word = cp; |
|
while (cp > n->b && isspace((unsigned char)cp[-1])) |
|
*--cp = '\0'; |
|
|
|
/* Move the last word into its own node, for use with .Pf. */ |
/* Move the last word into its own node, for use with .Pf. */ |
|
|
if ((n = pnode_alloc(p->cur)) == NULL) |
n = pnode_alloc_text(p->cur, last_word); |
fatal(p); |
|
n->node = NODE_TEXT; |
|
n->flags |= NFLAG_SPC; |
n->flags |= NFLAG_SPC; |
if ((n->b = strdup(last_word)) == NULL) |
|
fatal(p); |
|
} |
} |
|
|
static void |
static void |
Line 385 xml_entity(struct parse *p, const char *name) |
|
Line 376 xml_entity(struct parse *p, const char *name) |
|
struct pnode *n; |
struct pnode *n; |
const char *ccp; |
const char *ccp; |
char *cp; |
char *cp; |
unsigned int codepoint; |
long codepoint; |
enum pstate pstate; |
enum pstate pstate; |
|
|
if (p->del > 0) |
if (p->del > 0) |
Line 421 xml_entity(struct parse *p, const char *name) |
|
Line 412 xml_entity(struct parse *p, const char *name) |
|
if ((ccp = pnode_getattr_raw(n, |
if ((ccp = pnode_getattr_raw(n, |
ATTRKEY_DEFINITION, NULL)) == NULL) |
ATTRKEY_DEFINITION, NULL)) == NULL) |
continue; |
continue; |
if ((cp = strdup(ccp)) == NULL) |
cp = xstrdup(ccp); |
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_LINE | PFLAG_SPC); |
p->flags &= ~(PFLAG_LINE | PFLAG_SPC); |
Line 431 xml_entity(struct parse *p, const char *name) |
|
Line 421 xml_entity(struct parse *p, const char *name) |
|
} |
} |
} |
} |
if (*name == '#') { |
if (*name == '#') { |
codepoint = strtonum(name + 1, 0, 0x10ffff, &ccp); |
errno = 0; |
if (ccp == NULL) { |
if (name[1] == 'x') { |
if ((n = pnode_alloc(p->cur)) == NULL || |
ccp = name + 2; |
asprintf(&n->b, "\\[u%4.4X]", |
codepoint = strtol(ccp, &cp, 16); |
codepoint) < 0) |
} else { |
fatal(p); |
ccp = name + 1; |
|
codepoint = strtol(ccp, &cp, 10); |
|
} |
|
if (*ccp != '\0' && *cp == '\0' && errno != ERANGE && |
|
codepoint >= 0 && codepoint <= 0x10ffff) { |
|
n = pnode_alloc(p->cur); |
|
xasprintf(&n->b, "\\[u%4.4X]", codepoint); |
goto done; |
goto done; |
} |
} |
} |
} |
Line 445 xml_entity(struct parse *p, const char *name) |
|
Line 441 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 ((n = pnode_alloc(p->cur)) == NULL || |
n = pnode_alloc(p->cur); |
(n->b = strdup(entity->roff)) == NULL) |
n->b = xstrdup(entity->roff); |
fatal(p); |
|
done: |
done: |
n->node = NODE_ESCAPE; |
n->node = NODE_ESCAPE; |
n->flags = ((p->flags & PFLAG_LINE) ? NFLAG_LINE : 0) | |
if (p->flags & PFLAG_LINE && TAILQ_PREV(n, pnodeq, child) != NULL) |
((p->flags & PFLAG_SPC) ? NFLAG_SPC : 0); |
n->flags |= NFLAG_LINE; |
|
if (p->flags & PFLAG_SPC) |
|
n->flags |= NFLAG_SPC; |
p->flags &= ~(PFLAG_LINE | PFLAG_SPC); |
p->flags &= ~(PFLAG_LINE | PFLAG_SPC); |
} |
} |
|
|
Line 522 xml_elem_start(struct parse *p, const char *name) |
|
Line 519 xml_elem_start(struct parse *p, const char *name) |
|
break; |
break; |
} |
} |
|
|
if ((n = pnode_alloc(p->cur)) == NULL) |
n = pnode_alloc(p->cur); |
fatal(p); |
if (p->flags & PFLAG_LINE && p->cur != NULL && |
|
TAILQ_PREV(n, pnodeq, child) != NULL) |
|
n->flags |= NFLAG_LINE; |
|
p->flags &= ~PFLAG_LINE; |
|
|
/* |
/* |
* Some elements are self-closing. |
* Some elements are self-closing. |
Line 544 xml_elem_start(struct parse *p, const char *name) |
|
Line 544 xml_elem_start(struct parse *p, const char *name) |
|
switch (pnode_class(p->ncur)) { |
switch (pnode_class(p->ncur)) { |
case CLASS_LINE: |
case CLASS_LINE: |
case CLASS_ENCL: |
case CLASS_ENCL: |
n->flags = ((p->flags & PFLAG_LINE) ? NFLAG_LINE : 0) | |
if (p->flags & PFLAG_SPC) |
((p->flags & PFLAG_SPC) ? NFLAG_SPC : 0); |
n->flags |= NFLAG_SPC; |
break; |
break; |
case CLASS_NOFILL: |
case CLASS_NOFILL: |
p->nofill++; |
p->nofill++; |
Line 585 xml_attrkey(struct parse *p, const char *name) |
|
Line 585 xml_attrkey(struct parse *p, const char *name) |
|
p->flags &= ~PFLAG_ATTR; |
p->flags &= ~PFLAG_ATTR; |
return; |
return; |
} |
} |
if ((a = calloc(1, sizeof(*a))) == NULL) |
a = xcalloc(1, sizeof(*a)); |
fatal(p); |
|
|
|
a->key = key; |
a->key = key; |
a->val = ATTRVAL__MAX; |
a->val = ATTRVAL__MAX; |
if (value == NULL) { |
if (value == NULL) { |
a->rawval = NULL; |
a->rawval = NULL; |
p->flags |= PFLAG_ATTR; |
p->flags |= PFLAG_ATTR; |
} else { |
} else { |
if ((a->rawval = strdup(value)) == NULL) |
a->rawval = xstrdup(value); |
fatal(p); |
|
p->flags &= ~PFLAG_ATTR; |
p->flags &= ~PFLAG_ATTR; |
} |
} |
TAILQ_INSERT_TAIL(&p->cur->attrq, a, child); |
TAILQ_INSERT_TAIL(&p->cur->attrq, a, child); |
Line 613 xml_attrval(struct parse *p, const char *name) |
|
Line 610 xml_attrval(struct parse *p, const char *name) |
|
return; |
return; |
if ((a = TAILQ_LAST(&p->cur->attrq, pattrq)) == NULL) |
if ((a = TAILQ_LAST(&p->cur->attrq, pattrq)) == NULL) |
return; |
return; |
if ((a->val = attrval_parse(name)) == ATTRVAL__MAX && |
if ((a->val = attrval_parse(name)) == ATTRVAL__MAX) |
(a->rawval = strdup(name)) == NULL) |
a->rawval = xstrdup(name); |
fatal(p); |
|
p->flags &= ~PFLAG_ATTR; |
p->flags &= ~PFLAG_ATTR; |
} |
} |
|
|
Line 710 parse_alloc(int warn) |
|
Line 706 parse_alloc(int warn) |
|
{ |
{ |
struct parse *p; |
struct parse *p; |
|
|
if ((p = calloc(1, sizeof(*p))) == NULL) |
p = xcalloc(1, sizeof(*p)); |
return NULL; |
p->tree = xcalloc(1, sizeof(*p->tree)); |
|
|
if ((p->tree = calloc(1, sizeof(*p->tree))) == NULL) { |
|
free(p); |
|
return NULL; |
|
} |
|
if (warn) |
if (warn) |
p->flags |= PFLAG_WARN; |
p->flags |= PFLAG_WARN; |
else |
else |