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