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