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