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