xref: /freebsd/contrib/expat/examples/elements.c (revision dece5b05f577fc4c6b89af7ad48dfbdf7cc4945c)
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-2026 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 <inttypes.h> // PRIu64
45 #include <stdio.h>
46 #include <expat.h>
47 
48 #ifdef XML_UNICODE_WCHAR_T
49 #  define XML_FMT_STR "ls"
50 #else
51 #  define XML_FMT_STR "s"
52 #endif
53 
54 static void XMLCALL
55 startElement(void *userData, const XML_Char *name, const XML_Char **atts) {
56   int i;
57   int *const depthPtr = (int *)userData;
58   (void)atts;
59 
60   for (i = 0; i < *depthPtr; i++)
61     putchar('\t');
62   printf("%" XML_FMT_STR "\n", name);
63   *depthPtr += 1;
64 }
65 
66 static void XMLCALL
67 endElement(void *userData, const XML_Char *name) {
68   int *const depthPtr = (int *)userData;
69   (void)name;
70 
71   *depthPtr -= 1;
72 }
73 
74 int
75 main(void) {
76   XML_Parser parser = XML_ParserCreate(NULL);
77   int done;
78   int depth = 0;
79 
80   if (! parser) {
81     fprintf(stderr, "Couldn't allocate memory for parser\n");
82     return 1;
83   }
84 
85   XML_SetUserData(parser, &depth);
86   XML_SetElementHandler(parser, startElement, endElement);
87 
88   do {
89     void *const buf = XML_GetBuffer(parser, BUFSIZ);
90     if (! buf) {
91       fprintf(stderr, "Couldn't allocate memory for buffer\n");
92       XML_ParserFree(parser);
93       return 1;
94     }
95 
96     const size_t len = fread(buf, 1, BUFSIZ, stdin);
97 
98     if (ferror(stdin)) {
99       fprintf(stderr, "Read error\n");
100       XML_ParserFree(parser);
101       return 1;
102     }
103 
104     done = feof(stdin);
105 
106     if (XML_ParseBuffer(parser, (int)len, done) == XML_STATUS_ERROR) {
107       fprintf(stderr, "Parse error at line %" PRIu64 ":\n%" XML_FMT_STR "\n",
108               XML_GetCurrentLineNumber64(parser),
109               XML_ErrorString(XML_GetErrorCode(parser)));
110       XML_ParserFree(parser);
111       return 1;
112     }
113   } while (! done);
114 
115   XML_ParserFree(parser);
116   return 0;
117 }
118