Annotation of docbook2mdoc/parse.c, Revision 1.59
1.59 ! schwarze 1: /* $Id: parse.c,v 1.58 2019/05/02 04:09:26 schwarze Exp $ */
1.1 schwarze 2: /*
3: * Copyright (c) 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4: * Copyright (c) 2019 Ingo Schwarze <schwarze@openbsd.org>
5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
1.53 schwarze 18: #include <sys/types.h>
19:
1.1 schwarze 20: #include <assert.h>
21: #include <ctype.h>
1.24 schwarze 22: #include <errno.h>
23: #include <fcntl.h>
24: #include <libgen.h>
1.6 schwarze 25: #include <stdarg.h>
1.1 schwarze 26: #include <stdio.h>
1.5 schwarze 27: #include <stdlib.h>
1.1 schwarze 28: #include <string.h>
29: #include <unistd.h>
30:
1.53 schwarze 31: #include "xmalloc.h"
1.1 schwarze 32: #include "node.h"
33: #include "parse.h"
34:
35: /*
36: * The implementation of the DocBook parser.
37: */
38:
1.14 schwarze 39: enum pstate {
40: PARSE_ELEM,
41: PARSE_TAG,
42: PARSE_ARG,
43: PARSE_SQ,
44: PARSE_DQ
45: };
46:
1.1 schwarze 47: /*
48: * Global parse state.
49: * Keep this as simple and small as possible.
50: */
51: struct parse {
52: const char *fname; /* Name of the input file. */
53: struct ptree *tree; /* Complete parse result. */
1.23 schwarze 54: struct pnode *doctype;
1.1 schwarze 55: struct pnode *cur; /* Current node in the tree. */
1.5 schwarze 56: enum nodeid ncur; /* Type of the current node. */
57: int line; /* Line number in the input file. */
58: int col; /* Column number in the input file. */
59: int nline; /* Line number of next token. */
60: int ncol; /* Column number of next token. */
1.4 schwarze 61: int del; /* Levels of nested nodes being deleted. */
1.45 schwarze 62: int nofill; /* Levels of open no-fill displays. */
1.23 schwarze 63: int flags;
64: #define PFLAG_WARN (1 << 0) /* Print warning messages. */
1.51 schwarze 65: #define PFLAG_LINE (1 << 1) /* New line before the next element. */
66: #define PFLAG_SPC (1 << 2) /* Whitespace before the next element. */
67: #define PFLAG_ATTR (1 << 3) /* The most recent attribute is valid. */
68: #define PFLAG_EEND (1 << 4) /* This element is self-closing. */
1.1 schwarze 69: };
70:
1.39 schwarze 71: struct alias {
1.1 schwarze 72: const char *name; /* DocBook element name. */
73: enum nodeid node; /* Node type to generate. */
74: };
75:
1.39 schwarze 76: static const struct alias aliases[] = {
1.3 schwarze 77: { "acronym", NODE_IGNORE },
1.43 schwarze 78: { "affiliation", NODE_IGNORE },
1.4 schwarze 79: { "anchor", NODE_DELETE },
1.42 schwarze 80: { "application", NODE_COMMAND },
1.22 schwarze 81: { "article", NODE_SECTION },
1.41 schwarze 82: { "articleinfo", NODE_BOOKINFO },
1.22 schwarze 83: { "book", NODE_SECTION },
1.1 schwarze 84: { "chapter", NODE_SECTION },
1.44 schwarze 85: { "caption", NODE_IGNORE },
1.13 schwarze 86: { "code", NODE_LITERAL },
1.36 schwarze 87: { "computeroutput", NODE_LITERAL },
1.23 schwarze 88: { "!doctype", NODE_DOCTYPE },
1.44 schwarze 89: { "figure", NODE_IGNORE },
1.7 schwarze 90: { "firstname", NODE_PERSONNAME },
1.21 schwarze 91: { "glossary", NODE_VARIABLELIST },
92: { "glossdef", NODE_IGNORE },
93: { "glossdiv", NODE_IGNORE },
94: { "glossentry", NODE_VARLISTENTRY },
95: { "glosslist", NODE_VARIABLELIST },
1.43 schwarze 96: { "holder", NODE_IGNORE },
1.44 schwarze 97: { "imageobject", NODE_IGNORE },
1.4 schwarze 98: { "indexterm", NODE_DELETE },
1.11 schwarze 99: { "informaltable", NODE_TABLE },
1.59 ! schwarze 100: { "jobtitle", NODE_IGNORE },
1.42 schwarze 101: { "keycap", NODE_KEYSYM },
102: { "keycode", NODE_IGNORE },
1.55 schwarze 103: { "keycombo", NODE_IGNORE },
1.44 schwarze 104: { "mediaobject", NODE_BLOCKQUOTE },
1.59 ! schwarze 105: { "orgdiv", NODE_IGNORE },
1.43 schwarze 106: { "orgname", NODE_IGNORE },
1.40 schwarze 107: { "othercredit", NODE_AUTHOR },
1.7 schwarze 108: { "othername", NODE_PERSONNAME },
1.1 schwarze 109: { "part", NODE_SECTION },
1.3 schwarze 110: { "phrase", NODE_IGNORE },
1.4 schwarze 111: { "primary", NODE_DELETE },
1.42 schwarze 112: { "property", NODE_PARAMETER },
1.52 schwarze 113: { "reference", NODE_SECTION },
1.1 schwarze 114: { "refsect1", NODE_SECTION },
115: { "refsect2", NODE_SECTION },
116: { "refsect3", NODE_SECTION },
117: { "refsection", NODE_SECTION },
1.43 schwarze 118: { "releaseinfo", NODE_IGNORE },
1.42 schwarze 119: { "returnvalue", NODE_IGNORE },
1.4 schwarze 120: { "secondary", NODE_DELETE },
1.1 schwarze 121: { "sect1", NODE_SECTION },
122: { "sect2", NODE_SECTION },
1.46 schwarze 123: { "sect3", NODE_SECTION },
124: { "sect4", NODE_SECTION },
1.36 schwarze 125: { "sgmltag", NODE_MARKUP },
1.15 schwarze 126: { "simpara", NODE_PARA },
1.13 schwarze 127: { "structfield", NODE_PARAMETER },
128: { "structname", NODE_TYPE },
1.7 schwarze 129: { "surname", NODE_PERSONNAME },
1.12 schwarze 130: { "symbol", NODE_CONSTANT },
1.48 schwarze 131: { "tag", NODE_MARKUP },
1.3 schwarze 132: { "trademark", NODE_IGNORE },
1.18 schwarze 133: { "ulink", NODE_LINK },
1.13 schwarze 134: { "userinput", NODE_LITERAL },
1.5 schwarze 135: { NULL, NODE_IGNORE }
1.1 schwarze 136: };
137:
1.9 schwarze 138: struct entity {
139: const char *name;
140: const char *roff;
141: };
142:
143: /*
144: * XML character entity references found in the wild.
145: * Those that don't have an exact mandoc_char(7) representation
146: * are approximated, and the desired codepoint is given as a comment.
147: * Encoding them as \\[u...] would leave -Tascii out in the cold.
148: */
149: static const struct entity entities[] = {
150: { "alpha", "\\(*a" },
151: { "amp", "&" },
152: { "apos", "'" },
153: { "auml", "\\(:a" },
154: { "beta", "\\(*b" },
155: { "circ", "^" }, /* U+02C6 */
156: { "copy", "\\(co" },
157: { "dagger", "\\(dg" },
158: { "Delta", "\\(*D" },
159: { "eacute", "\\('e" },
160: { "emsp", "\\ " }, /* U+2003 */
161: { "gt", ">" },
162: { "hairsp", "\\^" },
163: { "kappa", "\\(*k" },
164: { "larr", "\\(<-" },
165: { "ldquo", "\\(lq" },
166: { "le", "\\(<=" },
167: { "lowbar", "_" },
168: { "lsqb", "[" },
169: { "lt", "<" },
170: { "mdash", "\\(em" },
171: { "minus", "\\-" },
172: { "ndash", "\\(en" },
173: { "nbsp", "\\ " },
174: { "num", "#" },
175: { "oslash", "\\(/o" },
176: { "ouml", "\\(:o" },
177: { "percnt", "%" },
178: { "quot", "\\(dq" },
179: { "rarr", "\\(->" },
180: { "rArr", "\\(rA" },
181: { "rdquo", "\\(rq" },
182: { "reg", "\\(rg" },
183: { "rho", "\\(*r" },
184: { "rsqb", "]" },
185: { "sigma", "\\(*s" },
186: { "shy", "\\&" }, /* U+00AD */
187: { "tau", "\\(*t" },
188: { "tilde", "\\[u02DC]" },
189: { "times", "\\[tmu]" },
190: { "uuml", "\\(:u" },
191: { NULL, NULL }
192: };
193:
1.23 schwarze 194: static size_t parse_string(struct parse *, char *, size_t,
195: enum pstate *, int);
1.24 schwarze 196: static void parse_fd(struct parse *, int);
1.23 schwarze 197:
198:
1.6 schwarze 199: static void
200: error_msg(struct parse *p, const char *fmt, ...)
201: {
202: va_list ap;
203:
1.29 schwarze 204: fprintf(stderr, "%s:%d:%d: ERROR: ", p->fname, p->line, p->col);
1.6 schwarze 205: va_start(ap, fmt);
206: vfprintf(stderr, fmt, ap);
207: va_end(ap);
208: fputc('\n', stderr);
1.29 schwarze 209: p->tree->flags |= TREE_ERROR;
1.6 schwarze 210: }
211:
212: static void
213: warn_msg(struct parse *p, const char *fmt, ...)
214: {
215: va_list ap;
216:
1.23 schwarze 217: if ((p->flags & PFLAG_WARN) == 0)
1.6 schwarze 218: return;
219:
1.29 schwarze 220: fprintf(stderr, "%s:%d:%d: WARNING: ", p->fname, p->line, p->col);
1.6 schwarze 221: va_start(ap, fmt);
222: vfprintf(stderr, fmt, ap);
223: va_end(ap);
224: fputc('\n', stderr);
1.29 schwarze 225: p->tree->flags |= TREE_WARN;
1.6 schwarze 226: }
227:
1.1 schwarze 228: /*
229: * Process a string of characters.
230: * If a text node is already open, append to it.
231: * Otherwise, create a new one as a child of the current node.
232: */
233: static void
1.35 schwarze 234: xml_text(struct parse *p, const char *word, int sz)
1.1 schwarze 235: {
1.35 schwarze 236: struct pnode *n, *np;
1.32 schwarze 237: size_t oldsz, newsz;
1.35 schwarze 238: int i;
1.1 schwarze 239:
1.32 schwarze 240: assert(sz > 0);
1.30 schwarze 241: if (p->del > 0)
1.1 schwarze 242: return;
243:
1.32 schwarze 244: if ((n = p->cur) == NULL) {
1.35 schwarze 245: error_msg(p, "discarding text before document: %.*s",
246: sz, word);
1.5 schwarze 247: return;
248: }
249:
1.35 schwarze 250: /* Append to the current text node, if one is open. */
251:
252: if (n->node == NODE_TEXT) {
253: oldsz = strlen(n->b);
254: newsz = oldsz + sz;
255: if (oldsz && (p->flags & PFLAG_SPC))
256: newsz++;
1.53 schwarze 257: n->b = xrealloc(n->b, newsz + 1);
1.35 schwarze 258: if (oldsz && (p->flags & PFLAG_SPC))
259: n->b[oldsz++] = ' ';
260: memcpy(n->b + oldsz, word, sz);
261: n->b[newsz] = '\0';
1.51 schwarze 262: p->flags &= ~(PFLAG_LINE | PFLAG_SPC);
1.35 schwarze 263: return;
1.1 schwarze 264: }
265:
1.35 schwarze 266: if (p->tree->flags & TREE_CLOSED && n == p->tree->root)
1.30 schwarze 267: warn_msg(p, "text after end of document: %.*s", sz, word);
1.5 schwarze 268:
1.35 schwarze 269: /* Create a new text node. */
1.1 schwarze 270:
1.53 schwarze 271: n = pnode_alloc(p->cur);
1.35 schwarze 272: n->node = NODE_TEXT;
1.57 schwarze 273: if (p->flags & PFLAG_LINE && TAILQ_PREV(n, pnodeq, child) != NULL)
274: n->flags |= NFLAG_LINE;
275: if (p->flags & PFLAG_SPC)
276: n->flags |= NFLAG_SPC;
1.51 schwarze 277: p->flags &= ~(PFLAG_LINE | PFLAG_SPC);
1.35 schwarze 278:
279: /*
1.39 schwarze 280: * If this node follows an in-line macro without intervening
1.35 schwarze 281: * whitespace, keep the text in it as short as possible,
282: * and do not keep it open.
283: */
284:
1.51 schwarze 285: np = n->flags & NFLAG_SPC ? NULL : TAILQ_PREV(n, pnodeq, child);
1.39 schwarze 286: while (np != NULL) {
287: switch (pnode_class(np->node)) {
288: case CLASS_VOID:
289: case CLASS_TEXT:
290: case CLASS_BLOCK:
1.45 schwarze 291: case CLASS_NOFILL:
1.39 schwarze 292: np = NULL;
293: break;
294: case CLASS_TRANS:
295: np = TAILQ_LAST(&np->childq, pnodeq);
296: continue;
297: case CLASS_LINE:
298: case CLASS_ENCL:
299: break;
300: }
301: break;
302: }
303: if (np != NULL) {
1.35 schwarze 304: i = 0;
305: while (i < sz && !isspace((unsigned char)word[i]))
306: i++;
1.53 schwarze 307: n->b = xstrndup(word, i);
1.35 schwarze 308: if (i == sz)
309: return;
310: while (i < sz && isspace((unsigned char)word[i]))
311: i++;
312: if (i == sz) {
313: p->flags |= PFLAG_SPC;
314: return;
315: }
316:
317: /* Put any remaining text into a second node. */
318:
1.53 schwarze 319: n = pnode_alloc(p->cur);
1.35 schwarze 320: n->node = NODE_TEXT;
1.51 schwarze 321: n->flags |= NFLAG_SPC;
1.35 schwarze 322: word += i;
323: sz -= i;
324: }
1.53 schwarze 325: n->b = xstrndup(word, sz);
1.35 schwarze 326:
327: /* The new node remains open for later pnode_closetext(). */
328:
329: p->cur = n;
1.1 schwarze 330: }
331:
1.16 schwarze 332: /*
333: * Close out the text node and strip trailing whitespace, if one is open.
334: */
1.1 schwarze 335: static void
1.37 schwarze 336: pnode_closetext(struct parse *p, int check_last_word)
1.1 schwarze 337: {
1.16 schwarze 338: struct pnode *n;
1.37 schwarze 339: char *cp, *last_word;
1.16 schwarze 340:
341: if ((n = p->cur) == NULL || n->node != NODE_TEXT)
342: return;
343: p->cur = n->parent;
1.32 schwarze 344: for (cp = strchr(n->b, '\0');
345: cp > n->b && isspace((unsigned char)cp[-1]);
346: *--cp = '\0')
1.23 schwarze 347: p->flags |= PFLAG_SPC;
1.37 schwarze 348:
349: if (p->flags & PFLAG_SPC || !check_last_word)
350: return;
351:
352: /*
353: * Find the beginning of the last word
354: * and delete whitespace before it.
355: */
356:
357: while (cp > n->b && !isspace((unsigned char)cp[-1]))
358: cp--;
1.58 schwarze 359: last_word = cp;
360: while (cp > n->b && isspace((unsigned char)cp[-1]))
361: cp--;
1.37 schwarze 362: if (cp == n->b)
363: return;
1.58 schwarze 364: *cp = '\0';
1.37 schwarze 365:
366: /* Move the last word into its own node, for use with .Pf. */
367:
1.54 schwarze 368: n = pnode_alloc_text(p->cur, last_word);
1.51 schwarze 369: n->flags |= NFLAG_SPC;
1.1 schwarze 370: }
371:
1.9 schwarze 372: static void
373: xml_entity(struct parse *p, const char *name)
374: {
375: const struct entity *entity;
1.30 schwarze 376: struct pnode *n;
1.23 schwarze 377: const char *ccp;
378: char *cp;
1.49 schwarze 379: unsigned int codepoint;
1.23 schwarze 380: enum pstate pstate;
1.9 schwarze 381:
382: if (p->del > 0)
383: return;
384:
385: if (p->cur == NULL) {
386: error_msg(p, "discarding entity before document: &%s;", name);
387: return;
388: }
389:
1.37 schwarze 390: pnode_closetext(p, 0);
1.9 schwarze 391:
392: if (p->tree->flags & TREE_CLOSED && p->cur == p->tree->root)
393: warn_msg(p, "entity after end of document: &%s;", name);
394:
395: for (entity = entities; entity->name != NULL; entity++)
396: if (strcmp(name, entity->name) == 0)
397: break;
398:
399: if (entity->roff == NULL) {
1.23 schwarze 400: if (p->doctype != NULL) {
1.30 schwarze 401: TAILQ_FOREACH(n, &p->doctype->childq, child) {
402: if ((ccp = pnode_getattr_raw(n,
1.23 schwarze 403: ATTRKEY_NAME, NULL)) == NULL ||
1.25 schwarze 404: strcmp(ccp, name) != 0)
405: continue;
1.30 schwarze 406: if ((ccp = pnode_getattr_raw(n,
1.25 schwarze 407: ATTRKEY_SYSTEM, NULL)) != NULL) {
408: parse_file(p, -1, ccp);
1.51 schwarze 409: p->flags &= ~(PFLAG_LINE | PFLAG_SPC);
1.25 schwarze 410: return;
411: }
1.30 schwarze 412: if ((ccp = pnode_getattr_raw(n,
1.23 schwarze 413: ATTRKEY_DEFINITION, NULL)) == NULL)
414: continue;
1.53 schwarze 415: cp = xstrdup(ccp);
1.23 schwarze 416: pstate = PARSE_ELEM;
417: parse_string(p, cp, strlen(cp), &pstate, 0);
1.51 schwarze 418: p->flags &= ~(PFLAG_LINE | PFLAG_SPC);
1.23 schwarze 419: free(cp);
420: return;
421: }
422: }
1.49 schwarze 423: if (*name == '#') {
424: codepoint = strtonum(name + 1, 0, 0x10ffff, &ccp);
425: if (ccp == NULL) {
1.53 schwarze 426: n = pnode_alloc(p->cur);
427: xasprintf(&n->b, "\\[u%4.4X]", codepoint);
1.49 schwarze 428: goto done;
429: }
430: }
1.9 schwarze 431: error_msg(p, "unknown entity &%s;", name);
432: return;
433: }
434:
435: /* Create, append, and close out an entity node. */
1.53 schwarze 436: n = pnode_alloc(p->cur);
437: n->b = xstrdup(entity->roff);
1.49 schwarze 438: done:
1.30 schwarze 439: n->node = NODE_ESCAPE;
1.57 schwarze 440: if (p->flags & PFLAG_LINE && TAILQ_PREV(n, pnodeq, child) != NULL)
441: n->flags |= NFLAG_LINE;
442: if (p->flags & PFLAG_SPC)
443: n->flags |= NFLAG_SPC;
1.51 schwarze 444: p->flags &= ~(PFLAG_LINE | PFLAG_SPC);
1.9 schwarze 445: }
446:
1.1 schwarze 447: /*
1.39 schwarze 448: * Parse an element name.
449: */
450: static enum nodeid
451: xml_name2node(struct parse *p, const char *name)
452: {
453: const struct alias *alias;
454: enum nodeid node;
455:
456: if ((node = pnode_parse(name)) < NODE_UNKNOWN)
457: return node;
458:
459: for (alias = aliases; alias->name != NULL; alias++)
460: if (strcmp(alias->name, name) == 0)
461: return alias->node;
462:
463: return NODE_UNKNOWN;
464: }
465:
466: /*
1.1 schwarze 467: * Begin an element.
468: */
469: static void
1.30 schwarze 470: xml_elem_start(struct parse *p, const char *name)
1.1 schwarze 471: {
1.30 schwarze 472: struct pnode *n;
1.1 schwarze 473:
1.4 schwarze 474: /*
475: * An ancestor is excluded from the tree;
476: * keep track of the number of levels excluded.
477: */
1.30 schwarze 478: if (p->del > 0) {
1.23 schwarze 479: if (*name != '!' && *name != '?')
1.30 schwarze 480: p->del++;
1.4 schwarze 481: return;
482: }
483:
1.39 schwarze 484: switch (p->ncur = xml_name2node(p, name)) {
1.4 schwarze 485: case NODE_DELETE_WARN:
1.30 schwarze 486: warn_msg(p, "skipping element <%s>", name);
1.2 schwarze 487: /* FALLTHROUGH */
1.4 schwarze 488: case NODE_DELETE:
1.30 schwarze 489: p->del = 1;
1.4 schwarze 490: /* FALLTHROUGH */
1.2 schwarze 491: case NODE_IGNORE:
492: return;
1.39 schwarze 493: case NODE_UNKNOWN:
494: if (*name != '!' && *name != '?')
495: error_msg(p, "unknown element <%s>", name);
496: return;
1.2 schwarze 497: default:
498: break;
499: }
1.1 schwarze 500:
1.30 schwarze 501: if (p->tree->flags & TREE_CLOSED && p->cur->parent == NULL)
502: warn_msg(p, "element after end of document: <%s>", name);
1.5 schwarze 503:
1.39 schwarze 504: switch (pnode_class(p->ncur)) {
505: case CLASS_LINE:
506: case CLASS_ENCL:
507: pnode_closetext(p, 1);
508: break;
509: default:
510: pnode_closetext(p, 0);
511: break;
512: }
513:
1.53 schwarze 514: n = pnode_alloc(p->cur);
1.57 schwarze 515: if (p->flags & PFLAG_LINE && p->cur != NULL &&
516: TAILQ_PREV(n, pnodeq, child) != NULL)
517: n->flags |= NFLAG_LINE;
518: p->flags &= ~PFLAG_LINE;
1.17 schwarze 519:
520: /*
1.39 schwarze 521: * Some elements are self-closing.
1.17 schwarze 522: * Nodes that begin a new macro or request line or start by
523: * printing text always want whitespace before themselves.
524: */
525:
1.39 schwarze 526: switch (n->node = p->ncur) {
1.23 schwarze 527: case NODE_DOCTYPE:
528: case NODE_ENTITY:
529: case NODE_SBR:
1.48 schwarze 530: case NODE_VOID:
1.30 schwarze 531: p->flags |= PFLAG_EEND;
1.17 schwarze 532: break;
533: default:
1.39 schwarze 534: break;
535: }
536: switch (pnode_class(p->ncur)) {
537: case CLASS_LINE:
538: case CLASS_ENCL:
1.57 schwarze 539: if (p->flags & PFLAG_SPC)
540: n->flags |= NFLAG_SPC;
1.17 schwarze 541: break;
1.45 schwarze 542: case CLASS_NOFILL:
543: p->nofill++;
544: /* FALLTHROUGH */
1.39 schwarze 545: default:
1.51 schwarze 546: n->flags |= NFLAG_SPC;
1.39 schwarze 547: break;
1.17 schwarze 548: }
1.30 schwarze 549: p->cur = n;
550: if (n->node == NODE_DOCTYPE) {
551: if (p->doctype == NULL)
552: p->doctype = n;
1.23 schwarze 553: else
1.30 schwarze 554: error_msg(p, "duplicate doctype");
555: } else if (n->parent == NULL && p->tree->root == NULL)
556: p->tree->root = n;
1.5 schwarze 557: }
558:
559: static void
1.30 schwarze 560: xml_attrkey(struct parse *p, const char *name)
1.5 schwarze 561: {
1.30 schwarze 562: struct pattr *a;
1.23 schwarze 563: const char *value;
1.5 schwarze 564: enum attrkey key;
1.1 schwarze 565:
1.47 schwarze 566: if (p->del > 0 || p->ncur >= NODE_UNKNOWN || *name == '\0')
1.5 schwarze 567: return;
1.23 schwarze 568:
1.30 schwarze 569: if ((p->ncur == NODE_DOCTYPE || p->ncur == NODE_ENTITY) &&
570: TAILQ_FIRST(&p->cur->attrq) == NULL) {
1.23 schwarze 571: value = name;
572: name = "NAME";
573: } else
574: value = NULL;
575:
1.5 schwarze 576: if ((key = attrkey_parse(name)) == ATTRKEY__MAX) {
1.30 schwarze 577: p->flags &= ~PFLAG_ATTR;
1.5 schwarze 578: return;
579: }
1.53 schwarze 580: a = xcalloc(1, sizeof(*a));
1.30 schwarze 581: a->key = key;
582: a->val = ATTRVAL__MAX;
1.23 schwarze 583: if (value == NULL) {
1.30 schwarze 584: a->rawval = NULL;
585: p->flags |= PFLAG_ATTR;
1.23 schwarze 586: } else {
1.53 schwarze 587: a->rawval = xstrdup(value);
1.30 schwarze 588: p->flags &= ~PFLAG_ATTR;
589: }
590: TAILQ_INSERT_TAIL(&p->cur->attrq, a, child);
591: if (p->ncur == NODE_ENTITY && key == ATTRKEY_NAME)
592: xml_attrkey(p, "DEFINITION");
1.5 schwarze 593: }
594:
595: static void
1.30 schwarze 596: xml_attrval(struct parse *p, const char *name)
1.5 schwarze 597: {
1.30 schwarze 598: struct pattr *a;
1.5 schwarze 599:
1.47 schwarze 600: if (p->del > 0 || p->ncur >= NODE_UNKNOWN ||
1.30 schwarze 601: (p->flags & PFLAG_ATTR) == 0)
1.5 schwarze 602: return;
1.30 schwarze 603: if ((a = TAILQ_LAST(&p->cur->attrq, pattrq)) == NULL)
1.5 schwarze 604: return;
1.53 schwarze 605: if ((a->val = attrval_parse(name)) == ATTRVAL__MAX)
606: a->rawval = xstrdup(name);
1.30 schwarze 607: p->flags &= ~PFLAG_ATTR;
1.1 schwarze 608: }
609:
610: /*
611: * Roll up the parse tree.
612: * If we're at a text node, roll that one up first.
613: */
614: static void
1.31 schwarze 615: xml_elem_end(struct parse *p, const char *name)
1.1 schwarze 616: {
1.26 schwarze 617: struct pnode *n;
618: const char *cp;
1.5 schwarze 619: enum nodeid node;
1.1 schwarze 620:
1.4 schwarze 621: /*
622: * An ancestor is excluded from the tree;
623: * keep track of the number of levels excluded.
624: */
1.31 schwarze 625: if (p->del > 1) {
626: p->del--;
1.4 schwarze 627: return;
628: }
629:
1.31 schwarze 630: if (p->del == 0)
1.37 schwarze 631: pnode_closetext(p, 0);
1.2 schwarze 632:
1.50 schwarze 633: n = p->cur;
1.39 schwarze 634: node = name == NULL ? p->ncur : xml_name2node(p, name);
1.2 schwarze 635:
1.5 schwarze 636: switch (node) {
1.4 schwarze 637: case NODE_DELETE_WARN:
638: case NODE_DELETE:
1.31 schwarze 639: if (p->del > 0)
640: p->del--;
1.4 schwarze 641: break;
1.2 schwarze 642: case NODE_IGNORE:
1.39 schwarze 643: case NODE_UNKNOWN:
1.26 schwarze 644: break;
645: case NODE_INCLUDE:
1.50 schwarze 646: p->cur = n->parent;
1.26 schwarze 647: cp = pnode_getattr_raw(n, ATTRKEY_HREF, NULL);
648: if (cp == NULL)
1.31 schwarze 649: error_msg(p, "<xi:include> element "
1.26 schwarze 650: "without href attribute");
651: else
1.31 schwarze 652: parse_file(p, -1, cp);
1.26 schwarze 653: pnode_unlink(n);
1.51 schwarze 654: p->flags &= ~(PFLAG_LINE | PFLAG_SPC);
1.2 schwarze 655: break;
1.23 schwarze 656: case NODE_DOCTYPE:
1.32 schwarze 657: case NODE_SBR:
1.48 schwarze 658: case NODE_VOID:
1.31 schwarze 659: p->flags &= ~PFLAG_EEND;
1.23 schwarze 660: /* FALLTHROUGH */
1.2 schwarze 661: default:
1.50 schwarze 662: if (n == NULL || node != n->node) {
1.31 schwarze 663: warn_msg(p, "element not open: </%s>", name);
1.5 schwarze 664: break;
665: }
1.45 schwarze 666: if (pnode_class(node) == CLASS_NOFILL)
667: p->nofill--;
1.5 schwarze 668:
669: /*
670: * Refrain from actually closing the document element.
671: * If no more content follows, no harm is done, but if
672: * some content still follows, simply processing it is
673: * obviously better than discarding it or crashing.
674: */
675:
1.50 schwarze 676: if (n->parent != NULL || node == NODE_DOCTYPE) {
677: p->cur = n->parent;
1.31 schwarze 678: if (p->cur != NULL)
679: p->ncur = p->cur->node;
1.23 schwarze 680: } else
1.31 schwarze 681: p->tree->flags |= TREE_CLOSED;
1.51 schwarze 682: p->flags &= ~(PFLAG_LINE | PFLAG_SPC);
1.50 schwarze 683:
684: /* Include a file containing entity declarations. */
685:
686: if (node == NODE_ENTITY && strcmp("%",
687: pnode_getattr_raw(n, ATTRKEY_NAME, "")) == 0 &&
688: (cp = pnode_getattr_raw(n, ATTRKEY_SYSTEM, NULL)) != NULL)
689: parse_file(p, -1, cp);
690:
1.4 schwarze 691: break;
1.2 schwarze 692: }
1.31 schwarze 693: assert(p->del == 0);
1.1 schwarze 694: }
695:
696: struct parse *
697: parse_alloc(int warn)
698: {
699: struct parse *p;
700:
1.53 schwarze 701: p = xcalloc(1, sizeof(*p));
702: p->tree = xcalloc(1, sizeof(*p->tree));
1.23 schwarze 703: if (warn)
704: p->flags |= PFLAG_WARN;
705: else
706: p->flags &= ~PFLAG_WARN;
1.1 schwarze 707: return p;
708: }
709:
710: void
711: parse_free(struct parse *p)
712: {
713: if (p == NULL)
714: return;
715: if (p->tree != NULL) {
716: pnode_unlink(p->tree->root);
717: free(p->tree);
718: }
719: free(p);
720: }
721:
1.14 schwarze 722: static void
723: increment(struct parse *p, char *b, size_t *pend, int refill)
724: {
725: if (refill) {
726: if (b[*pend] == '\n') {
727: p->nline++;
728: p->ncol = 1;
729: } else
730: p->ncol++;
731: }
732: ++*pend;
733: }
734:
1.5 schwarze 735: /*
736: * Advance the pend pointer to the next character in the charset.
737: * If the charset starts with a space, it stands for any whitespace.
738: * Update the new input file position, used for messages.
739: * Do not overrun the buffer b of length rlen.
740: * When reaching the end, NUL-terminate the buffer and return 1;
741: * otherwise, return 0.
742: */
743: static int
744: advance(struct parse *p, char *b, size_t rlen, size_t *pend,
1.14 schwarze 745: const char *charset, int refill)
1.5 schwarze 746: {
747: int space;
748:
749: if (*charset == ' ') {
750: space = 1;
751: charset++;
752: } else
753: space = 0;
754:
1.14 schwarze 755: if (refill) {
756: p->nline = p->line;
757: p->ncol = p->col;
758: }
1.5 schwarze 759: while (*pend < rlen) {
760: if (space && isspace((unsigned char)b[*pend]))
761: break;
762: if (strchr(charset, b[*pend]) != NULL)
763: break;
1.14 schwarze 764: increment(p, b, pend, refill);
1.5 schwarze 765: }
766: if (*pend == rlen) {
767: b[rlen] = '\0';
1.14 schwarze 768: return refill;
1.5 schwarze 769: } else
770: return 0;
771: }
772:
1.14 schwarze 773: size_t
774: parse_string(struct parse *p, char *b, size_t rlen,
775: enum pstate *pstate, int refill)
776: {
777: char *cp;
1.45 schwarze 778: size_t pws; /* Parse offset including whitespace. */
1.14 schwarze 779: size_t poff; /* Parse offset in b[]. */
780: size_t pend; /* Offset of the end of the current word. */
781: int elem_end;
782:
1.45 schwarze 783: pend = pws = 0;
1.14 schwarze 784: for (;;) {
785:
786: /* Proceed to the next token, skipping whitespace. */
787:
788: if (refill) {
789: p->line = p->nline;
790: p->col = p->ncol;
791: }
792: if ((poff = pend) == rlen)
793: break;
794: if (isspace((unsigned char)b[pend])) {
1.23 schwarze 795: p->flags |= PFLAG_SPC;
1.51 schwarze 796: if (b[pend] == '\n') {
797: p->flags |= PFLAG_LINE;
1.45 schwarze 798: pws = pend + 1;
1.51 schwarze 799: }
1.14 schwarze 800: increment(p, b, &pend, refill);
801: continue;
802: }
803:
804: /*
805: * The following four cases (ARG, TAG, and starting an
806: * entity or a tag) all parse a word or quoted string.
807: * If that extends beyond the read buffer and the last
808: * read(2) still got data, they all break out of the
809: * token loop to request more data from the read loop.
810: *
811: * Also, three of them detect self-closing tags, those
812: * ending with "/>", setting the flag elem_end and
813: * calling xml_elem_end() at the very end, after
814: * handling the attribute value, attribute name, or
815: * tag name, respectively.
816: */
817:
818: /* Parse an attribute value. */
819:
820: if (*pstate >= PARSE_ARG) {
821: if (*pstate == PARSE_ARG &&
822: (b[pend] == '\'' || b[pend] == '"')) {
823: *pstate = b[pend] == '"' ?
824: PARSE_DQ : PARSE_SQ;
825: increment(p, b, &pend, refill);
826: continue;
827: }
828: if (advance(p, b, rlen, &pend,
829: *pstate == PARSE_DQ ? "\"" :
830: *pstate == PARSE_SQ ? "'" : " >", refill))
831: break;
832: *pstate = PARSE_TAG;
833: elem_end = 0;
834: if (b[pend] == '>') {
835: *pstate = PARSE_ELEM;
836: if (pend > 0 && b[pend - 1] == '/') {
837: b[pend - 1] = '\0';
838: elem_end = 1;
839: }
1.23 schwarze 840: if (p->flags & PFLAG_EEND)
841: elem_end = 1;
1.14 schwarze 842: }
843: b[pend] = '\0';
844: if (pend < rlen)
845: increment(p, b, &pend, refill);
846: xml_attrval(p, b + poff);
847: if (elem_end)
848: xml_elem_end(p, NULL);
849:
850: /* Look for an attribute name. */
851:
852: } else if (*pstate == PARSE_TAG) {
1.23 schwarze 853: switch (p->ncur) {
854: case NODE_DOCTYPE:
855: if (b[pend] == '[') {
856: *pstate = PARSE_ELEM;
857: increment(p, b, &pend, refill);
858: continue;
859: }
860: /* FALLTHROUGH */
861: case NODE_ENTITY:
862: if (b[pend] == '"' || b[pend] == '\'') {
863: *pstate = PARSE_ARG;
864: continue;
865: }
866: break;
867: default:
868: break;
869: }
1.14 schwarze 870: if (advance(p, b, rlen, &pend, " =>", refill))
871: break;
872: elem_end = 0;
873: switch (b[pend]) {
874: case '>':
875: *pstate = PARSE_ELEM;
876: if (pend > 0 && b[pend - 1] == '/') {
877: b[pend - 1] = '\0';
878: elem_end = 1;
879: }
1.23 schwarze 880: if (p->flags & PFLAG_EEND)
881: elem_end = 1;
1.14 schwarze 882: break;
883: case '=':
884: *pstate = PARSE_ARG;
885: break;
886: default:
887: break;
888: }
889: b[pend] = '\0';
890: if (pend < rlen)
891: increment(p, b, &pend, refill);
892: xml_attrkey(p, b + poff);
893: if (elem_end)
894: xml_elem_end(p, NULL);
895:
896: /* Begin an opening or closing tag. */
897:
898: } else if (b[poff] == '<') {
899: if (advance(p, b, rlen, &pend, " >", refill))
900: break;
901: if (pend > poff + 3 &&
902: strncmp(b + poff, "<!--", 4) == 0) {
903:
904: /* Skip a comment. */
905:
906: cp = strstr(b + pend - 2, "-->");
907: if (cp == NULL) {
908: if (refill)
909: break;
910: cp = b + rlen;
911: } else
912: cp += 3;
913: while (b + pend < cp)
914: increment(p, b, &pend, refill);
915: continue;
916: }
917: elem_end = 0;
918: if (b[pend] != '>')
919: *pstate = PARSE_TAG;
920: else if (pend > 0 && b[pend - 1] == '/') {
921: b[pend - 1] = '\0';
922: elem_end = 1;
923: }
924: b[pend] = '\0';
925: if (pend < rlen)
926: increment(p, b, &pend, refill);
927: if (b[++poff] == '/') {
928: elem_end = 1;
929: poff++;
1.23 schwarze 930: } else {
1.14 schwarze 931: xml_elem_start(p, b + poff);
1.23 schwarze 932: if (*pstate == PARSE_ELEM &&
933: p->flags & PFLAG_EEND)
934: elem_end = 1;
935: }
1.14 schwarze 936: if (elem_end)
937: xml_elem_end(p, b + poff);
938:
1.23 schwarze 939: /* Close a doctype. */
940:
941: } else if (p->ncur == NODE_DOCTYPE && b[poff] == ']') {
942: *pstate = PARSE_TAG;
943: increment(p, b, &pend, refill);
944:
1.14 schwarze 945: /* Process an entity. */
946:
947: } else if (b[poff] == '&') {
948: if (advance(p, b, rlen, &pend, ";", refill))
949: break;
950: b[pend] = '\0';
951: if (pend < rlen)
952: increment(p, b, &pend, refill);
953: xml_entity(p, b + poff + 1);
954:
955: /* Process text up to the next tag, entity, or EOL. */
956:
957: } else {
1.28 schwarze 958: advance(p, b, rlen, &pend,
1.33 schwarze 959: p->ncur == NODE_DOCTYPE ? "<&]\n" : "<&\n",
1.28 schwarze 960: refill);
1.45 schwarze 961: if (p->nofill)
962: poff = pws;
1.35 schwarze 963: xml_text(p, b + poff, pend - poff);
1.33 schwarze 964: if (b[pend] == '\n')
1.37 schwarze 965: pnode_closetext(p, 0);
1.14 schwarze 966: }
1.45 schwarze 967: pws = pend;
1.14 schwarze 968: }
969: return poff;
970: }
971:
1.24 schwarze 972:
973: /*
974: * The read loop.
975: * If the previous token was incomplete and asked for more input,
976: * we have to enter the read loop once more even on EOF.
977: * Once rsz is 0, incomplete tokens will no longer ask for more input
978: * but instead use whatever there is, and then exit the read loop.
979: * The minus one on the size limit for read(2) is needed such that
980: * advance() can set b[rlen] to NUL when needed.
981: */
982: static void
983: parse_fd(struct parse *p, int fd)
1.1 schwarze 984: {
985: char b[4096];
1.5 schwarze 986: ssize_t rsz; /* Return value from read(2). */
1.14 schwarze 987: size_t rlen; /* Number of bytes in b[]. */
1.5 schwarze 988: size_t poff; /* Parse offset in b[]. */
1.14 schwarze 989: enum pstate pstate;
1.1 schwarze 990:
1.24 schwarze 991: rlen = 0;
1.14 schwarze 992: pstate = PARSE_ELEM;
993: while ((rsz = read(fd, b + rlen, sizeof(b) - rlen - 1)) >= 0 &&
994: (rlen += rsz) > 0) {
995: poff = parse_string(p, b, rlen, &pstate, rsz > 0);
1.5 schwarze 996: /* Buffer exhausted; shift left and re-fill. */
997: assert(poff > 0);
998: rlen -= poff;
1.14 schwarze 999: memmove(b, b + poff, rlen);
1.5 schwarze 1000: }
1.24 schwarze 1001: if (rsz < 0)
1002: error_msg(p, "read: %s", strerror(errno));
1003: }
1004:
1005: /*
1006: * Open and parse a file.
1007: */
1008: struct ptree *
1009: parse_file(struct parse *p, int fd, const char *fname)
1010: {
1011: const char *save_fname;
1012: int save_line, save_col;
1013:
1014: /* Save and initialize reporting data. */
1015:
1016: save_fname = p->fname;
1017: save_line = p->nline;
1018: save_col = p->ncol;
1019: p->fname = fname;
1020: p->line = 0;
1021: p->col = 0;
1022:
1023: /* Open the file, unless it is already open. */
1024:
1025: if (fd == -1 && (fd = open(fname, O_RDONLY, 0)) == -1) {
1026: error_msg(p, "open: %s", strerror(errno));
1027: p->fname = save_fname;
1028: return p->tree;
1.5 schwarze 1029: }
1.24 schwarze 1030:
1031: /*
1032: * After opening the starting file, change to the directory it
1033: * is located in, in case it wants to include any further files,
1034: * which are typically given with relative paths in DocBook.
1035: * Do this on a best-effort basis; don't complain about failure.
1036: */
1037:
1038: if (save_fname == NULL && (fname = dirname(fname)) != NULL &&
1039: strcmp(fname, ".") != 0)
1040: (void)chdir(fname);
1041:
1042: /* Run the read loop. */
1043:
1044: p->nline = 1;
1045: p->ncol = 1;
1046: parse_fd(p, fd);
1047:
1048: /* On the top level, finalize the parse tree. */
1049:
1050: if (save_fname == NULL) {
1.37 schwarze 1051: pnode_closetext(p, 0);
1.24 schwarze 1052: if (p->tree->root == NULL)
1053: error_msg(p, "empty document");
1054: else if ((p->tree->flags & TREE_CLOSED) == 0)
1055: warn_msg(p, "document not closed");
1056: pnode_unlink(p->doctype);
1057: }
1058:
1059: /* Clean up. */
1060:
1061: if (fd != STDIN_FILENO)
1062: close(fd);
1063: p->fname = save_fname;
1064: p->nline = save_line;
1065: p->ncol = save_col;
1.1 schwarze 1066: return p->tree;
1067: }
CVSweb