File: parse.c

package info (click to toggle)
libnih 1.0.3-4.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 10,544 kB
  • sloc: ansic: 188,634; sh: 11,217; makefile: 1,116; yacc: 291; xml: 239; sed: 16
file content (417 lines) | stat: -rw-r--r-- 11,113 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/* nih-dbus-tool
 *
 * parse.c - parse XML introspection data and tool-specific annotations
 *
 * Copyright © 2009 Scott James Remnant <scott@netsplit.com>.
 * Copyright © 2009 Canonical Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif /* HAVE_CONFIG_H */


#include <expat.h>

#include <errno.h>
#include <string.h>
#include <unistd.h>

#include <nih/macros.h>
#include <nih/alloc.h>
#include <nih/list.h>
#include <nih/error.h>
#include <nih/logging.h>

#include "node.h"
#include "interface.h"
#include "method.h"
#include "signal.h"
#include "property.h"
#include "argument.h"
#include "annotation.h"
#include "parse.h"
#include "errors.h"


/**
 * BUF_SIZE:
 *
 * Size of buffer we use when parsing.
 **/
#define BUF_SIZE 80


/**
 * parse_stack_push:
 * @parent: parent object of new stack entry,
 * @stack: stack to push onto,
 * @type: type of object to push,
 * @data: pointer to object data.
 *
 * Allocates a new Stack object with the @type and @data specified
 * and pushes it onto @stack.
 *
 * The entry can be removed from the stack by freeing it, though this will
 * not free the associated @data unless you arrange that by references.
 *
 * If @parent is not NULL, it should be a pointer to another object which
 * will be used as a parent for the returned entry.  When all parents
 * of the returned node are freed, the returned entry will also be
 * freed.
 *
 * Returns: new entry or NULL if the allocation failed.
 **/
ParseStack *
parse_stack_push (const void *   parent,
		  NihList *      stack,
		  ParseStackType type,
		  void *         data)
{
	ParseStack *entry;

	nih_assert (stack != NULL);

	entry = nih_new (parent, ParseStack);
	if (! entry)
		return NULL;

	nih_list_init (&entry->entry);

	nih_alloc_set_destructor (entry, nih_list_destroy);

	entry->type = type;
	entry->data = data;

	if ((entry->type != PARSE_IGNORED)
	    && (entry->type != PARSE_ANNOTATION)) {
		nih_assert (entry->data != NULL);
		nih_ref (entry->data, entry);
	} else {
		nih_assert (entry->data == NULL);
	}

	nih_list_add_after (stack, &entry->entry);

	return entry;
}

/**
 * parse_stack_top:
 * @stack: stack to return from.
 *
 * Returns: the top entry in @stack or NULL if the stack is empty.
 **/
ParseStack *
parse_stack_top (NihList *stack)
{
	nih_assert (stack != NULL);

	if (! NIH_LIST_EMPTY (stack)) {
		return (ParseStack *)stack->next;
	} else {
		return NULL;
	}
}


/**
 * parse_start_tag:
 * @xmlp: XML parser,
 * @tag: name of XML tag being parsed,
 * @attr: NULL-terminated array of attribute name and value pairs.
 *
 * This function is intended to be used as the start element handler for
 * the XML parser @xmlp and called by that parser.  It looks at the tag
 * name @tag and calls one of the other *_start_tag() functions to
 * handle the tag.
 *
 * Unknown tags result in a warning and are otherwise ignored, the stack
 * contains an ignore element and the content of those tags will also be
 * ignored with no warnings generated.
 *
 * Errors are raised and reported by stopping the parser, other element
 * handlers should check that the parsing status is not finished as they
 * may be called as part of the unwinding process.  XML_ParseBuffer will
 * return to indicate an error, the XML error code will be XML_ERROR_ABORTED
 * and the actual error can be retrieved with nih_error_get().
 **/
void
parse_start_tag (XML_Parser    xmlp,
		 const char *  tag,
		 char * const *attr)
{
	XML_ParsingStatus status;
	ParseContext *    context;
	ParseStack *      parent;
	int               ret = 0;

	nih_assert (xmlp != NULL);
	nih_assert (tag != NULL);
	nih_assert (attr != NULL);

	XML_GetParsingStatus (xmlp, &status);
	if (status.parsing == XML_FINISHED)
		return;

	nih_debug ("Parsed '%s' tag", tag);

	context = XML_GetUserData (xmlp);
	nih_assert (context != NULL);

	/* Ignore any tag inside an ignored tag */
	parent = parse_stack_top (&context->stack);
	if (parent && (parent->type == PARSE_IGNORED)) {
 		if (! parse_stack_push (NULL, &context->stack,
					PARSE_IGNORED, NULL)) {
			nih_error_raise_system ();
			ret = -1;
		}

		goto exit;
	}

	/* Otherwise call out to handle the tag */
	if (! strcmp (tag, "node")) {
		ret = node_start_tag (xmlp, tag, attr);
	} else if (! strcmp (tag, "interface")) {
		ret = interface_start_tag (xmlp, tag, attr);
	} else if (! strcmp (tag, "method")) {
		ret = method_start_tag (xmlp, tag, attr);
	} else if (! strcmp (tag, "signal")) {
		ret = signal_start_tag (xmlp, tag, attr);
	} else if (! strcmp (tag, "property")) {
		ret = property_start_tag (xmlp, tag, attr);
	} else if (! strcmp (tag, "arg")) {
		ret = argument_start_tag (xmlp, tag, attr);
	} else if (! strcmp (tag, "annotation")) {
		ret = annotation_start_tag (xmlp, tag, attr);
	} else {
		nih_warn ("%s:%zu:%zu: %s: %s", context->filename,
			  (size_t)XML_GetCurrentLineNumber (xmlp),
			  (size_t)XML_GetCurrentColumnNumber (xmlp),
			  _("Ignored unknown tag"),
			  tag);

 		if (! parse_stack_push (NULL, &context->stack,
					PARSE_IGNORED, NULL)) {
			nih_error_raise_system ();
			ret = -1;
		}
	}

exit:
	if (ret < 0)
		nih_assert (XML_StopParser (xmlp, FALSE) == XML_STATUS_OK);
}

/**
 * parse_end_tag:
 * @xmlp: XML parser,
 * @tag: name of XML tag being parsed.
 *
 * This function is intended to be used as the end element handler for
 * the XML parser @xmlp and called by that parser.  It looks at the tag
 * name @tag and calls one of the other *_end_tag() functions to
 * handle the tag.
 *
 * The end tags whose start was ignored are ignored without any warning.
 *
 * Errors are raised and reported by stopping the parser, other element
 * handlers should check that the parsing status is not finished as they
 * may be called as part of the unwinding process.  XML_ParseBuffer will
 * return to indicate an error, the XML error code will be XML_ERROR_ABORTED
 * and the actual error can be retrieved with nih_error_get().
 **/
void
parse_end_tag (XML_Parser  xmlp,
	       const char *tag)
{
	XML_ParsingStatus status;
	ParseContext *    context;
	ParseStack *      entry;
	int               ret = 0;

	nih_assert (xmlp != NULL);
	nih_assert (tag != NULL);

	XML_GetParsingStatus (xmlp, &status);
	if (status.parsing == XML_FINISHED)
		return;

	nih_debug ("Parsed '%s' end tag", tag);

	context = XML_GetUserData (xmlp);
	nih_assert (context != NULL);

	/* Ignore the end tag of any ignored tag */
	entry = parse_stack_top (&context->stack);
	nih_assert (entry != NULL);
	if (entry->type == PARSE_IGNORED) {
		nih_free (entry);
		goto exit;
	}

	/* Otherwise call out to handle the tag */
	if (! strcmp (tag, "node")) {
		ret = node_end_tag (xmlp, tag);
	} else if (! strcmp (tag, "interface")) {
		ret = interface_end_tag (xmlp, tag);
	} else if (! strcmp (tag, "method")) {
		ret = method_end_tag (xmlp, tag);
	} else if (! strcmp (tag, "signal")) {
		ret = signal_end_tag (xmlp, tag);
	} else if (! strcmp (tag, "property")) {
		ret = property_end_tag (xmlp, tag);
	} else if (! strcmp (tag, "arg")) {
		ret = argument_end_tag (xmlp, tag);
	} else if (! strcmp (tag, "annotation")) {
		ret = annotation_end_tag (xmlp, tag);
	} else {
		nih_assert_not_reached ();
	}

exit:
	if (ret < 0)
		nih_assert (XML_StopParser (xmlp, FALSE) == XML_STATUS_OK);
}


/**
 * parse_xml:
 * @parent: parent object of new node,
 * @fd: file descriptor to parse from,
 * @filename: filename for error reporting.
 *
 * Parse XML data from @fd according to the D-Bus Introspection
 * specification, returning the top-level Node which contains the
 * Interfaces defined by that object.
 *
 * Errors in parsing are output within this function, since it has the
 * line and column number available to it.  @filename is used when reporting
 * these errors.
 *
 * In general, the parser is fairly liberal and will ignore unexpected tags,
 * attributes and any character data.  However it is strict about restrictions
 * in the specification, for example it will not allow missing attributes or
 * unknown values in them.
 *
 * If @parent is not NULL, it should be a pointer to another object which
 * will be used as a parent for the returned node.  When all parents
 * of the returned node are freed, the returned node will also be
 * freed.
 *
 * Returns: newly allocated Node on success, NULL on error.
 **/
Node *
parse_xml (const void *parent,
	   int         fd,
	   const char *filename)
{
	ParseContext context;
	XML_Parser   xmlp;
	ssize_t      len;

	context.parent = parent;
	nih_list_init (&context.stack);
	context.filename = filename;
	context.node = NULL;

	xmlp = XML_ParserCreate ("UTF-8");
	if (! xmlp) {
		nih_error ("%s: %s", _("Unable to create XML Parser"),
			   strerror (ENOMEM));
		return NULL;
	}

	XML_SetUserData (xmlp, &context);
	XML_UseParserAsHandlerArg (xmlp);
	XML_SetElementHandler (xmlp,
			       (XML_StartElementHandler)parse_start_tag,
			       (XML_EndElementHandler)parse_end_tag);

	do {
		enum XML_Status status;
		enum XML_Error  error;
		void *          buf;

		buf = XML_GetBuffer (xmlp, BUF_SIZE);
		if (! buf) {
			error = XML_GetErrorCode (xmlp);
			nih_error ("%s: %s", _("Unable to allocate parsing buffer"),
				   XML_ErrorString (error));
			goto error;
		}

		len = read (fd, buf, BUF_SIZE);
		if (len < 0) {
			nih_error ("%s: %s: %s", context.filename,
				   _("Read error"), strerror (errno));
			goto error;
		}

		status = XML_ParseBuffer (xmlp, len, len == 0 ? TRUE : FALSE);
		if (status != XML_STATUS_OK) {
			error = XML_GetErrorCode (xmlp);

			if (error == XML_ERROR_ABORTED) {
				NihError *err;

				err = nih_error_get ();
				nih_error ("%s:%zu:%zu: %s", context.filename,
					   (size_t)XML_GetCurrentLineNumber (xmlp),
					   (size_t)XML_GetCurrentColumnNumber (xmlp),
					   err->message);
				nih_free (err);
			} else {
				nih_error ("%s:%zu:%zu: %s: %s",
					   context.filename,
					   (size_t)XML_GetCurrentLineNumber (xmlp),
					   (size_t)XML_GetCurrentColumnNumber (xmlp),
					   _("XML parse error"),
					   XML_ErrorString (error));
			}

			goto error;
		}
	} while (len > 0);

	nih_assert (NIH_LIST_EMPTY (&context.stack));

	if (! context.node) {
		nih_error ("%s: %s", context.filename,
			   _("No node present"));
		goto error;
	}

	XML_ParserFree (xmlp);

	return context.node;

error:
	NIH_LIST_FOREACH_SAFE (&context.stack, iter) {
		ParseStack *entry = (ParseStack *)iter;

		nih_free (entry);
	}

	if (context.node)
		nih_free (context.node);

	XML_ParserFree (xmlp);

	return NULL;
}