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