Annotation of docbook2mdoc/parse.c, Revision 1.33
1.33 ! schwarze 1: /* $Id: parse.c,v 1.32 2019/04/11 04:23:22 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.30 schwarze 325: xml_char(struct parse *p, const char *word, int sz)
1.1 schwarze 326: {
1.30 schwarze 327: struct pnode *n;
1.32 schwarze 328: size_t oldsz, newsz;
1.1 schwarze 329:
1.32 schwarze 330: assert(sz > 0);
1.30 schwarze 331: if (p->del > 0)
1.1 schwarze 332: return;
333:
1.32 schwarze 334: if ((n = p->cur) == NULL) {
1.30 schwarze 335: error_msg(p, "discarding text before document: %.*s", sz, word);
1.5 schwarze 336: return;
337: }
338:
1.32 schwarze 339: if (n->node != NODE_TEXT) {
1.30 schwarze 340: if ((n = calloc(1, sizeof(*n))) == NULL)
341: fatal(p);
342: n->node = NODE_TEXT;
343: n->spc = (p->flags & PFLAG_SPC) != 0;
344: n->parent = p->cur;
345: TAILQ_INIT(&n->childq);
346: TAILQ_INIT(&n->attrq);
347: TAILQ_INSERT_TAIL(&p->cur->childq, n, child);
348: p->cur = n;
1.1 schwarze 349: }
350:
1.32 schwarze 351: if (p->tree->flags & TREE_CLOSED && n->parent == p->tree->root)
1.30 schwarze 352: warn_msg(p, "text after end of document: %.*s", sz, word);
1.5 schwarze 353:
1.1 schwarze 354: /* Append to the current text node. */
355:
1.32 schwarze 356: oldsz = n->b == NULL ? 0 : strlen(n->b);
357: newsz = oldsz + sz;
358: if (oldsz && (p->flags & PFLAG_SPC))
359: newsz++;
360: if ((n->b = realloc(n->b, newsz + 1)) == NULL)
1.30 schwarze 361: fatal(p);
1.32 schwarze 362: if (oldsz && (p->flags & PFLAG_SPC))
363: n->b[oldsz++] = ' ';
364: memcpy(n->b + oldsz, word, sz);
365: n->b[newsz] = '\0';
1.30 schwarze 366: p->flags &= ~PFLAG_SPC;
1.1 schwarze 367: }
368:
1.16 schwarze 369: /*
370: * Close out the text node and strip trailing whitespace, if one is open.
371: */
1.1 schwarze 372: static void
1.16 schwarze 373: pnode_closetext(struct parse *p)
1.1 schwarze 374: {
1.16 schwarze 375: struct pnode *n;
1.32 schwarze 376: char *cp;
1.16 schwarze 377:
378: if ((n = p->cur) == NULL || n->node != NODE_TEXT)
379: return;
380: p->cur = n->parent;
1.32 schwarze 381: for (cp = strchr(n->b, '\0');
382: cp > n->b && isspace((unsigned char)cp[-1]);
383: *--cp = '\0')
1.23 schwarze 384: p->flags |= PFLAG_SPC;
1.1 schwarze 385: }
386:
1.9 schwarze 387: static void
388: xml_entity(struct parse *p, const char *name)
389: {
390: const struct entity *entity;
1.30 schwarze 391: struct pnode *n;
1.23 schwarze 392: const char *ccp;
393: char *cp;
394: enum pstate pstate;
1.9 schwarze 395:
396: if (p->del > 0)
397: return;
398:
399: if (p->cur == NULL) {
400: error_msg(p, "discarding entity before document: &%s;", name);
401: return;
402: }
403:
1.16 schwarze 404: pnode_closetext(p);
1.9 schwarze 405:
406: if (p->tree->flags & TREE_CLOSED && p->cur == p->tree->root)
407: warn_msg(p, "entity after end of document: &%s;", name);
408:
409: for (entity = entities; entity->name != NULL; entity++)
410: if (strcmp(name, entity->name) == 0)
411: break;
412:
413: if (entity->roff == NULL) {
1.23 schwarze 414: if (p->doctype != NULL) {
1.30 schwarze 415: TAILQ_FOREACH(n, &p->doctype->childq, child) {
416: if ((ccp = pnode_getattr_raw(n,
1.23 schwarze 417: ATTRKEY_NAME, NULL)) == NULL ||
1.25 schwarze 418: strcmp(ccp, name) != 0)
419: continue;
1.30 schwarze 420: if ((ccp = pnode_getattr_raw(n,
1.25 schwarze 421: ATTRKEY_SYSTEM, NULL)) != NULL) {
422: parse_file(p, -1, ccp);
423: p->flags &= ~PFLAG_SPC;
424: return;
425: }
1.30 schwarze 426: if ((ccp = pnode_getattr_raw(n,
1.23 schwarze 427: ATTRKEY_DEFINITION, NULL)) == NULL)
428: continue;
1.29 schwarze 429: if ((cp = strdup(ccp)) == NULL)
430: fatal(p);
1.23 schwarze 431: pstate = PARSE_ELEM;
432: parse_string(p, cp, strlen(cp), &pstate, 0);
433: p->flags &= ~PFLAG_SPC;
434: free(cp);
435: return;
436: }
437: }
1.9 schwarze 438: error_msg(p, "unknown entity &%s;", name);
439: return;
440: }
441:
442: /* Create, append, and close out an entity node. */
1.30 schwarze 443: if ((n = calloc(1, sizeof(*n))) == NULL ||
1.32 schwarze 444: (n->b = strdup(entity->roff)) == NULL)
1.29 schwarze 445: fatal(p);
1.30 schwarze 446: n->node = NODE_ESCAPE;
447: n->spc = (p->flags & PFLAG_SPC) != 0;
448: n->parent = p->cur;
449: TAILQ_INIT(&n->childq);
450: TAILQ_INIT(&n->attrq);
451: TAILQ_INSERT_TAIL(&p->cur->childq, n, child);
1.23 schwarze 452: p->flags &= ~PFLAG_SPC;
1.9 schwarze 453: }
454:
1.1 schwarze 455: /*
456: * Begin an element.
457: */
458: static void
1.30 schwarze 459: xml_elem_start(struct parse *p, const char *name)
1.1 schwarze 460: {
1.5 schwarze 461: const struct element *elem;
1.30 schwarze 462: struct pnode *n;
1.1 schwarze 463:
1.4 schwarze 464: /*
465: * An ancestor is excluded from the tree;
466: * keep track of the number of levels excluded.
467: */
1.30 schwarze 468: if (p->del > 0) {
1.23 schwarze 469: if (*name != '!' && *name != '?')
1.30 schwarze 470: p->del++;
1.4 schwarze 471: return;
472: }
473:
1.30 schwarze 474: pnode_closetext(p);
1.1 schwarze 475:
476: for (elem = elements; elem->name != NULL; elem++)
477: if (strcmp(elem->name, name) == 0)
478: break;
479:
1.23 schwarze 480: if (elem->name == NULL) {
481: if (*name == '!' || *name == '?')
482: return;
1.30 schwarze 483: error_msg(p, "unknown element <%s>", name);
1.23 schwarze 484: }
1.6 schwarze 485:
1.30 schwarze 486: p->ncur = elem->node;
1.1 schwarze 487:
1.30 schwarze 488: switch (p->ncur) {
1.4 schwarze 489: case NODE_DELETE_WARN:
1.30 schwarze 490: warn_msg(p, "skipping element <%s>", name);
1.2 schwarze 491: /* FALLTHROUGH */
1.4 schwarze 492: case NODE_DELETE:
1.30 schwarze 493: p->del = 1;
1.4 schwarze 494: /* FALLTHROUGH */
1.2 schwarze 495: case NODE_IGNORE:
496: return;
497: case NODE_INLINEEQUATION:
1.30 schwarze 498: p->tree->flags |= TREE_EQN;
1.2 schwarze 499: break;
500: default:
501: break;
502: }
1.1 schwarze 503:
1.30 schwarze 504: if (p->tree->flags & TREE_CLOSED && p->cur->parent == NULL)
505: warn_msg(p, "element after end of document: <%s>", name);
1.5 schwarze 506:
1.30 schwarze 507: if ((n = calloc(1, sizeof(*n))) == NULL)
508: fatal(p);
1.17 schwarze 509:
510: /*
511: * Nodes that begin a new macro or request line or start by
512: * printing text always want whitespace before themselves.
513: */
514:
1.30 schwarze 515: switch (n->node = elem->node) {
1.23 schwarze 516: case NODE_DOCTYPE:
517: case NODE_ENTITY:
518: case NODE_SBR:
1.30 schwarze 519: p->flags |= PFLAG_EEND;
1.23 schwarze 520: /* FALLTHROUGH */
1.22 schwarze 521: case NODE_APPENDIX:
1.17 schwarze 522: case NODE_AUTHORGROUP:
1.20 schwarze 523: case NODE_BLOCKQUOTE:
1.17 schwarze 524: case NODE_BOOKINFO:
525: case NODE_CAUTION:
526: case NODE_EDITOR:
527: case NODE_ENTRY:
528: case NODE_FUNCDEF:
529: case NODE_FUNCPROTOTYPE:
530: case NODE_INFORMALEQUATION:
531: case NODE_INLINEEQUATION:
532: case NODE_ITEMIZEDLIST:
533: case NODE_LEGALNOTICE:
534: case NODE_LITERALLAYOUT:
535: case NODE_NOTE:
536: case NODE_ORDEREDLIST:
537: case NODE_PARA:
538: case NODE_PREFACE:
539: case NODE_PROGRAMLISTING:
540: case NODE_REFMETA:
541: case NODE_REFNAMEDIV:
542: case NODE_REFSYNOPSISDIV:
543: case NODE_ROW:
544: case NODE_SCREEN:
545: case NODE_SECTION:
546: case NODE_SYNOPSIS:
547: case NODE_TGROUP:
548: case NODE_TIP:
549: case NODE_TITLE:
550: case NODE_VARIABLELIST:
551: case NODE_VARLISTENTRY:
552: case NODE_WARNING:
1.30 schwarze 553: n->spc = 1;
1.17 schwarze 554: break;
555: default:
1.30 schwarze 556: n->spc = (p->flags & PFLAG_SPC) != 0;
1.17 schwarze 557: break;
558: }
1.30 schwarze 559: n->parent = p->cur;
560: TAILQ_INIT(&n->childq);
561: TAILQ_INIT(&n->attrq);
562:
563: if (p->cur != NULL)
564: TAILQ_INSERT_TAIL(&p->cur->childq, n, child);
565:
566: p->cur = n;
567: if (n->node == NODE_DOCTYPE) {
568: if (p->doctype == NULL)
569: p->doctype = n;
1.23 schwarze 570: else
1.30 schwarze 571: error_msg(p, "duplicate doctype");
572: } else if (n->parent == NULL && p->tree->root == NULL)
573: p->tree->root = n;
1.5 schwarze 574: }
575:
576: static void
1.30 schwarze 577: xml_attrkey(struct parse *p, const char *name)
1.5 schwarze 578: {
1.30 schwarze 579: struct pattr *a;
1.23 schwarze 580: const char *value;
1.5 schwarze 581: enum attrkey key;
1.1 schwarze 582:
1.30 schwarze 583: if (p->del > 0 || p->ncur == NODE_IGNORE || *name == '\0')
1.5 schwarze 584: return;
1.23 schwarze 585:
1.30 schwarze 586: if ((p->ncur == NODE_DOCTYPE || p->ncur == NODE_ENTITY) &&
587: TAILQ_FIRST(&p->cur->attrq) == NULL) {
1.23 schwarze 588: value = name;
589: name = "NAME";
590: } else
591: value = NULL;
592:
1.5 schwarze 593: if ((key = attrkey_parse(name)) == ATTRKEY__MAX) {
1.30 schwarze 594: p->flags &= ~PFLAG_ATTR;
1.5 schwarze 595: return;
596: }
1.30 schwarze 597: if ((a = calloc(1, sizeof(*a))) == NULL)
598: fatal(p);
1.29 schwarze 599:
1.30 schwarze 600: a->key = key;
601: a->val = ATTRVAL__MAX;
1.23 schwarze 602: if (value == NULL) {
1.30 schwarze 603: a->rawval = NULL;
604: p->flags |= PFLAG_ATTR;
1.23 schwarze 605: } else {
1.30 schwarze 606: if ((a->rawval = strdup(value)) == NULL)
607: fatal(p);
608: p->flags &= ~PFLAG_ATTR;
609: }
610: TAILQ_INSERT_TAIL(&p->cur->attrq, a, child);
611: if (p->ncur == NODE_ENTITY && key == ATTRKEY_NAME)
612: xml_attrkey(p, "DEFINITION");
1.5 schwarze 613: }
614:
615: static void
1.30 schwarze 616: xml_attrval(struct parse *p, const char *name)
1.5 schwarze 617: {
1.30 schwarze 618: struct pattr *a;
1.5 schwarze 619:
1.30 schwarze 620: if (p->del > 0 || p->ncur == NODE_IGNORE ||
621: (p->flags & PFLAG_ATTR) == 0)
1.5 schwarze 622: return;
1.30 schwarze 623: if ((a = TAILQ_LAST(&p->cur->attrq, pattrq)) == NULL)
1.5 schwarze 624: return;
1.30 schwarze 625: if ((a->val = attrval_parse(name)) == ATTRVAL__MAX &&
626: (a->rawval = strdup(name)) == NULL)
627: fatal(p);
628: p->flags &= ~PFLAG_ATTR;
1.1 schwarze 629: }
630:
631: /*
632: * Roll up the parse tree.
633: * If we're at a text node, roll that one up first.
634: */
635: static void
1.31 schwarze 636: xml_elem_end(struct parse *p, const char *name)
1.1 schwarze 637: {
1.5 schwarze 638: const struct element *elem;
1.26 schwarze 639: struct pnode *n;
640: const char *cp;
1.5 schwarze 641: enum nodeid node;
1.1 schwarze 642:
1.4 schwarze 643: /*
644: * An ancestor is excluded from the tree;
645: * keep track of the number of levels excluded.
646: */
1.31 schwarze 647: if (p->del > 1) {
648: p->del--;
1.4 schwarze 649: return;
650: }
651:
1.31 schwarze 652: if (p->del == 0)
653: pnode_closetext(p);
1.2 schwarze 654:
1.5 schwarze 655: if (name != NULL) {
656: for (elem = elements; elem->name != NULL; elem++)
657: if (strcmp(elem->name, name) == 0)
658: break;
659: node = elem->node;
660: } else
1.31 schwarze 661: node = p->ncur;
1.2 schwarze 662:
1.5 schwarze 663: switch (node) {
1.4 schwarze 664: case NODE_DELETE_WARN:
665: case NODE_DELETE:
1.31 schwarze 666: if (p->del > 0)
667: p->del--;
1.4 schwarze 668: break;
1.2 schwarze 669: case NODE_IGNORE:
1.26 schwarze 670: break;
671: case NODE_INCLUDE:
1.31 schwarze 672: n = p->cur;
673: p->cur = p->cur->parent;
1.26 schwarze 674: cp = pnode_getattr_raw(n, ATTRKEY_HREF, NULL);
675: if (cp == NULL)
1.31 schwarze 676: error_msg(p, "<xi:include> element "
1.26 schwarze 677: "without href attribute");
678: else
1.31 schwarze 679: parse_file(p, -1, cp);
1.26 schwarze 680: pnode_unlink(n);
1.31 schwarze 681: p->flags &= ~PFLAG_SPC;
1.2 schwarze 682: break;
1.23 schwarze 683: case NODE_DOCTYPE:
1.32 schwarze 684: case NODE_SBR:
1.31 schwarze 685: p->flags &= ~PFLAG_EEND;
1.23 schwarze 686: /* FALLTHROUGH */
1.2 schwarze 687: default:
1.31 schwarze 688: if (p->cur == NULL || node != p->cur->node) {
689: warn_msg(p, "element not open: </%s>", name);
1.5 schwarze 690: break;
691: }
692:
693: /*
694: * Refrain from actually closing the document element.
695: * If no more content follows, no harm is done, but if
696: * some content still follows, simply processing it is
697: * obviously better than discarding it or crashing.
698: */
699:
1.31 schwarze 700: if (p->cur->parent != NULL || node == NODE_DOCTYPE) {
701: p->cur = p->cur->parent;
702: if (p->cur != NULL)
703: p->ncur = p->cur->node;
1.23 schwarze 704: } else
1.31 schwarze 705: p->tree->flags |= TREE_CLOSED;
706: p->flags &= ~PFLAG_SPC;
1.4 schwarze 707: break;
1.2 schwarze 708: }
1.31 schwarze 709: assert(p->del == 0);
1.1 schwarze 710: }
711:
712: struct parse *
713: parse_alloc(int warn)
714: {
715: struct parse *p;
716:
717: if ((p = calloc(1, sizeof(*p))) == NULL)
718: return NULL;
719:
720: if ((p->tree = calloc(1, sizeof(*p->tree))) == NULL) {
721: free(p);
722: return NULL;
723: }
1.23 schwarze 724: if (warn)
725: p->flags |= PFLAG_WARN;
726: else
727: p->flags &= ~PFLAG_WARN;
1.1 schwarze 728: return p;
729: }
730:
731: void
732: parse_free(struct parse *p)
733: {
734: if (p == NULL)
735: return;
736: if (p->tree != NULL) {
737: pnode_unlink(p->tree->root);
738: free(p->tree);
739: }
740: free(p);
741: }
742:
1.14 schwarze 743: static void
744: increment(struct parse *p, char *b, size_t *pend, int refill)
745: {
746: if (refill) {
747: if (b[*pend] == '\n') {
748: p->nline++;
749: p->ncol = 1;
750: } else
751: p->ncol++;
752: }
753: ++*pend;
754: }
755:
1.5 schwarze 756: /*
757: * Advance the pend pointer to the next character in the charset.
758: * If the charset starts with a space, it stands for any whitespace.
759: * Update the new input file position, used for messages.
760: * Do not overrun the buffer b of length rlen.
761: * When reaching the end, NUL-terminate the buffer and return 1;
762: * otherwise, return 0.
763: */
764: static int
765: advance(struct parse *p, char *b, size_t rlen, size_t *pend,
1.14 schwarze 766: const char *charset, int refill)
1.5 schwarze 767: {
768: int space;
769:
770: if (*charset == ' ') {
771: space = 1;
772: charset++;
773: } else
774: space = 0;
775:
1.14 schwarze 776: if (refill) {
777: p->nline = p->line;
778: p->ncol = p->col;
779: }
1.5 schwarze 780: while (*pend < rlen) {
781: if (space && isspace((unsigned char)b[*pend]))
782: break;
783: if (strchr(charset, b[*pend]) != NULL)
784: break;
1.14 schwarze 785: increment(p, b, pend, refill);
1.5 schwarze 786: }
787: if (*pend == rlen) {
788: b[rlen] = '\0';
1.14 schwarze 789: return refill;
1.5 schwarze 790: } else
791: return 0;
792: }
793:
1.14 schwarze 794: size_t
795: parse_string(struct parse *p, char *b, size_t rlen,
796: enum pstate *pstate, int refill)
797: {
798: char *cp;
799: size_t poff; /* Parse offset in b[]. */
800: size_t pend; /* Offset of the end of the current word. */
801: int elem_end;
802:
803: pend = 0;
804: for (;;) {
805:
806: /* Proceed to the next token, skipping whitespace. */
807:
808: if (refill) {
809: p->line = p->nline;
810: p->col = p->ncol;
811: }
812: if ((poff = pend) == rlen)
813: break;
814: if (isspace((unsigned char)b[pend])) {
1.23 schwarze 815: p->flags |= PFLAG_SPC;
1.14 schwarze 816: increment(p, b, &pend, refill);
817: continue;
818: }
819:
820: /*
821: * The following four cases (ARG, TAG, and starting an
822: * entity or a tag) all parse a word or quoted string.
823: * If that extends beyond the read buffer and the last
824: * read(2) still got data, they all break out of the
825: * token loop to request more data from the read loop.
826: *
827: * Also, three of them detect self-closing tags, those
828: * ending with "/>", setting the flag elem_end and
829: * calling xml_elem_end() at the very end, after
830: * handling the attribute value, attribute name, or
831: * tag name, respectively.
832: */
833:
834: /* Parse an attribute value. */
835:
836: if (*pstate >= PARSE_ARG) {
837: if (*pstate == PARSE_ARG &&
838: (b[pend] == '\'' || b[pend] == '"')) {
839: *pstate = b[pend] == '"' ?
840: PARSE_DQ : PARSE_SQ;
841: increment(p, b, &pend, refill);
842: continue;
843: }
844: if (advance(p, b, rlen, &pend,
845: *pstate == PARSE_DQ ? "\"" :
846: *pstate == PARSE_SQ ? "'" : " >", refill))
847: break;
848: *pstate = PARSE_TAG;
849: elem_end = 0;
850: if (b[pend] == '>') {
851: *pstate = PARSE_ELEM;
852: if (pend > 0 && b[pend - 1] == '/') {
853: b[pend - 1] = '\0';
854: elem_end = 1;
855: }
1.23 schwarze 856: if (p->flags & PFLAG_EEND)
857: elem_end = 1;
1.14 schwarze 858: }
859: b[pend] = '\0';
860: if (pend < rlen)
861: increment(p, b, &pend, refill);
862: xml_attrval(p, b + poff);
863: if (elem_end)
864: xml_elem_end(p, NULL);
865:
866: /* Look for an attribute name. */
867:
868: } else if (*pstate == PARSE_TAG) {
1.23 schwarze 869: switch (p->ncur) {
870: case NODE_DOCTYPE:
871: if (b[pend] == '[') {
872: *pstate = PARSE_ELEM;
873: increment(p, b, &pend, refill);
874: continue;
875: }
876: /* FALLTHROUGH */
877: case NODE_ENTITY:
878: if (b[pend] == '"' || b[pend] == '\'') {
879: *pstate = PARSE_ARG;
880: continue;
881: }
882: break;
883: default:
884: break;
885: }
1.14 schwarze 886: if (advance(p, b, rlen, &pend, " =>", refill))
887: break;
888: elem_end = 0;
889: switch (b[pend]) {
890: case '>':
891: *pstate = PARSE_ELEM;
892: if (pend > 0 && b[pend - 1] == '/') {
893: b[pend - 1] = '\0';
894: elem_end = 1;
895: }
1.23 schwarze 896: if (p->flags & PFLAG_EEND)
897: elem_end = 1;
1.14 schwarze 898: break;
899: case '=':
900: *pstate = PARSE_ARG;
901: break;
902: default:
903: break;
904: }
905: b[pend] = '\0';
906: if (pend < rlen)
907: increment(p, b, &pend, refill);
908: xml_attrkey(p, b + poff);
909: if (elem_end)
910: xml_elem_end(p, NULL);
911:
912: /* Begin an opening or closing tag. */
913:
914: } else if (b[poff] == '<') {
915: if (advance(p, b, rlen, &pend, " >", refill))
916: break;
917: if (pend > poff + 3 &&
918: strncmp(b + poff, "<!--", 4) == 0) {
919:
920: /* Skip a comment. */
921:
922: cp = strstr(b + pend - 2, "-->");
923: if (cp == NULL) {
924: if (refill)
925: break;
926: cp = b + rlen;
927: } else
928: cp += 3;
929: while (b + pend < cp)
930: increment(p, b, &pend, refill);
931: continue;
932: }
933: elem_end = 0;
934: if (b[pend] != '>')
935: *pstate = PARSE_TAG;
936: else if (pend > 0 && b[pend - 1] == '/') {
937: b[pend - 1] = '\0';
938: elem_end = 1;
939: }
940: b[pend] = '\0';
941: if (pend < rlen)
942: increment(p, b, &pend, refill);
943: if (b[++poff] == '/') {
944: elem_end = 1;
945: poff++;
1.23 schwarze 946: } else {
1.14 schwarze 947: xml_elem_start(p, b + poff);
1.23 schwarze 948: if (*pstate == PARSE_ELEM &&
949: p->flags & PFLAG_EEND)
950: elem_end = 1;
951: }
1.14 schwarze 952: if (elem_end)
953: xml_elem_end(p, b + poff);
954:
1.23 schwarze 955: /* Close a doctype. */
956:
957: } else if (p->ncur == NODE_DOCTYPE && b[poff] == ']') {
958: *pstate = PARSE_TAG;
959: increment(p, b, &pend, refill);
960:
1.14 schwarze 961: /* Process an entity. */
962:
963: } else if (b[poff] == '&') {
964: if (advance(p, b, rlen, &pend, ";", refill))
965: break;
966: b[pend] = '\0';
967: if (pend < rlen)
968: increment(p, b, &pend, refill);
969: xml_entity(p, b + poff + 1);
970:
971: /* Process text up to the next tag, entity, or EOL. */
972:
973: } else {
1.28 schwarze 974: advance(p, b, rlen, &pend,
1.33 ! schwarze 975: p->ncur == NODE_DOCTYPE ? "<&]\n" : "<&\n",
1.28 schwarze 976: refill);
1.14 schwarze 977: xml_char(p, b + poff, pend - poff);
1.33 ! schwarze 978: if (b[pend] == '\n')
! 979: pnode_closetext(p);
1.14 schwarze 980: }
981: }
982: return poff;
983: }
984:
1.24 schwarze 985:
986: /*
987: * The read loop.
988: * If the previous token was incomplete and asked for more input,
989: * we have to enter the read loop once more even on EOF.
990: * Once rsz is 0, incomplete tokens will no longer ask for more input
991: * but instead use whatever there is, and then exit the read loop.
992: * The minus one on the size limit for read(2) is needed such that
993: * advance() can set b[rlen] to NUL when needed.
994: */
995: static void
996: parse_fd(struct parse *p, int fd)
1.1 schwarze 997: {
998: char b[4096];
1.5 schwarze 999: ssize_t rsz; /* Return value from read(2). */
1.14 schwarze 1000: size_t rlen; /* Number of bytes in b[]. */
1.5 schwarze 1001: size_t poff; /* Parse offset in b[]. */
1.14 schwarze 1002: enum pstate pstate;
1.1 schwarze 1003:
1.24 schwarze 1004: rlen = 0;
1.14 schwarze 1005: pstate = PARSE_ELEM;
1006: while ((rsz = read(fd, b + rlen, sizeof(b) - rlen - 1)) >= 0 &&
1007: (rlen += rsz) > 0) {
1008: poff = parse_string(p, b, rlen, &pstate, rsz > 0);
1.5 schwarze 1009: /* Buffer exhausted; shift left and re-fill. */
1010: assert(poff > 0);
1011: rlen -= poff;
1.14 schwarze 1012: memmove(b, b + poff, rlen);
1.5 schwarze 1013: }
1.24 schwarze 1014: if (rsz < 0)
1015: error_msg(p, "read: %s", strerror(errno));
1016: }
1017:
1018: /*
1019: * Open and parse a file.
1020: */
1021: struct ptree *
1022: parse_file(struct parse *p, int fd, const char *fname)
1023: {
1024: const char *save_fname;
1025: int save_line, save_col;
1026:
1027: /* Save and initialize reporting data. */
1028:
1029: save_fname = p->fname;
1030: save_line = p->nline;
1031: save_col = p->ncol;
1032: p->fname = fname;
1033: p->line = 0;
1034: p->col = 0;
1035:
1036: /* Open the file, unless it is already open. */
1037:
1038: if (fd == -1 && (fd = open(fname, O_RDONLY, 0)) == -1) {
1039: error_msg(p, "open: %s", strerror(errno));
1040: p->fname = save_fname;
1041: return p->tree;
1.5 schwarze 1042: }
1.24 schwarze 1043:
1044: /*
1045: * After opening the starting file, change to the directory it
1046: * is located in, in case it wants to include any further files,
1047: * which are typically given with relative paths in DocBook.
1048: * Do this on a best-effort basis; don't complain about failure.
1049: */
1050:
1051: if (save_fname == NULL && (fname = dirname(fname)) != NULL &&
1052: strcmp(fname, ".") != 0)
1053: (void)chdir(fname);
1054:
1055: /* Run the read loop. */
1056:
1057: p->nline = 1;
1058: p->ncol = 1;
1059: parse_fd(p, fd);
1060:
1061: /* On the top level, finalize the parse tree. */
1062:
1063: if (save_fname == NULL) {
1064: pnode_closetext(p);
1065: if (p->tree->root == NULL)
1066: error_msg(p, "empty document");
1067: else if ((p->tree->flags & TREE_CLOSED) == 0)
1068: warn_msg(p, "document not closed");
1069: pnode_unlink(p->doctype);
1070: }
1071:
1072: /* Clean up. */
1073:
1074: if (fd != STDIN_FILENO)
1075: close(fd);
1076: p->fname = save_fname;
1077: p->nline = save_line;
1078: p->ncol = save_col;
1.1 schwarze 1079: return p->tree;
1080: }
CVSweb