xref: /freebsd/contrib/expat/examples/elements.c (revision 207d96dabfec14d7b3699747abb539ab3c1118ab)
1 /* This is simple demonstration of how to use expat. This program
2    reads an XML document from standard input and writes a line with
3    the name of each element to standard output indenting child
4    elements by one tab stop more than their parent element.
5    It must be used with Expat compiled for UTF-8 output.
6                             __  __            _
7                          ___\ \/ /_ __   __ _| |_
8                         / _ \\  /| '_ \ / _` | __|
9                        |  __//  \| |_) | (_| | |_
10                         \___/_/\_\ .__/ \__,_|\__|
11                                  |_| XML parser
12 
13    Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
14    Copyright (c) 2001-2003 Fred L. Drake, Jr. <fdrake@users.sourceforge.net>
15    Copyright (c) 2004-2006 Karl Waclawek <karl@waclawek.net>
16    Copyright (c) 2005-2007 Steven Solie <steven@solie.ca>
17    Copyright (c) 2016-2022 Sebastian Pipping <sebastian@pipping.org>
18    Copyright (c) 2017      Rhodri James <rhodri@wildebeest.org.uk>
19    Copyright (c) 2019      Zhongyuan Zhou <zhouzhongyuan@huawei.com>
20    Licensed under the MIT license:
21 
22    Permission is  hereby granted,  free of charge,  to any  person obtaining
23    a  copy  of  this  software   and  associated  documentation  files  (the
24    "Software"),  to  deal in  the  Software  without restriction,  including
25    without  limitation the  rights  to use,  copy,  modify, merge,  publish,
26    distribute, sublicense, and/or sell copies of the Software, and to permit
27    persons  to whom  the Software  is  furnished to  do so,  subject to  the
28    following conditions:
29 
30    The above copyright  notice and this permission notice  shall be included
31    in all copies or substantial portions of the Software.
32 
33    THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
34    EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
35    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
36    NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
37    DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
38    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
39    USE OR OTHER DEALINGS IN THE SOFTWARE.
40 
41    SPDX-License-Identifier: MIT
42 */
43 
44 #include <stdio.h>
45 #include <expat.h>
46 
47 #ifdef XML_LARGE_SIZE
48 #  define XML_FMT_INT_MOD "ll"
49 #else
50 #  define XML_FMT_INT_MOD "l"
51 #endif
52 
53 #ifdef XML_UNICODE_WCHAR_T
54 #  define XML_FMT_STR "ls"
55 #else
56 #  define XML_FMT_STR "s"
57 #endif
58 
59 static void XMLCALL
startElement(void * userData,const XML_Char * name,const XML_Char ** atts)60 startElement(void *userData, const XML_Char *name, const XML_Char **atts) {
61   int i;
62   int *const depthPtr = (int *)userData;
63   (void)atts;
64 
65   for (i = 0; i < *depthPtr; i++)
66     putchar('\t');
67   printf("%" XML_FMT_STR "\n", name);
68   *depthPtr += 1;
69 }
70 
71 static void XMLCALL
endElement(void * userData,const XML_Char * name)72 endElement(void *userData, const XML_Char *name) {
73   int *const depthPtr = (int *)userData;
74   (void)name;
75 
76   *depthPtr -= 1;
77 }
78 
79 int
main(void)80 main(void) {
81   XML_Parser parser = XML_ParserCreate(NULL);
82   int done;
83   int depth = 0;
84 
85   if (! parser) {
86     fprintf(stderr, "Couldn't allocate memory for parser\n");
87     return 1;
88   }
89 
90   XML_SetUserData(parser, &depth);
91   XML_SetElementHandler(parser, startElement, endElement);
92 
93   do {
94     void *const buf = XML_GetBuffer(parser, BUFSIZ);
95     if (! buf) {
96       fprintf(stderr, "Couldn't allocate memory for buffer\n");
97       XML_ParserFree(parser);
98       return 1;
99     }
100 
101     const size_t len = fread(buf, 1, BUFSIZ, stdin);
102 
103     if (ferror(stdin)) {
104       fprintf(stderr, "Read error\n");
105       XML_ParserFree(parser);
106       return 1;
107     }
108 
109     done = feof(stdin);
110 
111     if (XML_ParseBuffer(parser, (int)len, done) == XML_STATUS_ERROR) {
112       fprintf(stderr,
113               "Parse error at line %" XML_FMT_INT_MOD "u:\n%" XML_FMT_STR "\n",
114               XML_GetCurrentLineNumber(parser),
115               XML_ErrorString(XML_GetErrorCode(parser)));
116       XML_ParserFree(parser);
117       return 1;
118     }
119   } while (! done);
120 
121   XML_ParserFree(parser);
122   return 0;
123 }
124