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