File: argument.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 (470 lines) | stat: -rw-r--r-- 12,665 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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/* nih-dbus-tool
 *
 * argument.c - argument parsing and handling
 *
 * 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 <string.h>

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

#include <nih-dbus/dbus_object.h>

#include "symbol.h"
#include "argument.h"
#include "parse.h"
#include "errors.h"


/**
 * argument_name_valid:
 * @name: Member name to verify.
 *
 * Verifies whether @name matches the specification for a D-Bus interface
 * member name, and thus is valid for a argument.
 *
 * Returns: TRUE if valid, FALSE if not.
 **/
int
argument_name_valid (const char *name)
{
	nih_assert (name != NULL);

	/* We can get away with just using strlen() here even through name
	 * is in UTF-8 because all the valid characters are ASCII.
	 */
	for (size_t i = 0; i < strlen (name); i++) {
		/* Names may contain digits, but not at the beginning. */
		if ((name[i] >= '0') && (name[i] <= '9')) {
			if (i == 0)
				return FALSE;

			continue;
		}

		/* Valid characters anywhere are [A-Za-z_] */
		if (   ((name[i] < 'A') || (name[i] > 'Z'))
		    && ((name[i] < 'a') || (name[i] > 'z'))
		    && (name[i] != '_'))
			return FALSE;
	}

	/* Name must be at least 1 character */
	if (strlen (name) < 1)
		return FALSE;

	return TRUE;
}


/**
 * argument_new:
 * @parent: parent object for new argument,
 * @name: D-Bus name of argument,
 * @type: D-Bus type argument,
 * @direction: argument direction.
 *
 * Allocates a new D-Bus object Argument data structure, with the D-Bus name
 * optionally set to @name and the D-Bus type signature set to @type.  The
 * returned structure is not placed into any list.
 *
 * If @parent is not NULL, it should be a pointer to another object which
 * will be used as a parent for the returned argument.  When all parents
 * of the returned argument are freed, the returned argument will also be
 * freed.
 *
 * Returns: the new argument or NULL if the allocation failed.
 **/
Argument *
argument_new (const void *  parent,
	      const char *  name,
	      const char *  type,
	      NihDBusArgDir direction)
{
	Argument *argument;

	nih_assert (type != NULL);

	argument = nih_new (parent, Argument);
	if (! argument)
		return NULL;

	nih_list_init (&argument->entry);

	nih_alloc_set_destructor (argument, nih_list_destroy);

	if (name) {
		argument->name = nih_strdup (argument, name);
		if (! argument->name) {
			nih_free (argument);
			return NULL;
		}
	} else {
		argument->name = NULL;
	}

	argument->symbol = NULL;

	argument->type = nih_strdup (argument, type);
	if (! argument->type) {
		nih_free (argument);
		return NULL;
	}

	argument->direction = direction;

	return argument;
}


/**
 * argument_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 called by parse_start_tag() for an "argument"
 * start tag, which may be a child of either the "method" or "signal" tags
 * defining an argument for the method or signal.
 *
 * If the argument does not appear within a method or signal tag a warning
 * is emitted and the tag will be ignored.
 *
 * Arguments must have a "type" attribute containing the D-Bus type
 * signature, they usually have a "name" attribute specifying the D-Bus name
 * but it's technically optional and they may also have a "direction"
 * attribute specifying whether the argument is input (default for methods)
 * or output (default for signals).
 *
 * Any unknown attributes result in a warning and will be ignored.
 *
 * An Argument object will be allocated and pushed onto the stack, this is
 * not added to the method or signal until the end tag is found.
 *
 * Returns: zero on success, negative value on raised error.
 **/
int
argument_start_tag (XML_Parser    xmlp,
		    const char *  tag,
		    char * const *attr)
{
	ParseContext *      context;
	ParseStack *        parent;
	nih_local Argument *argument = NULL;
	char * const *      key;
	char * const *      value;
	const char *        name = NULL;
	const char *        type = NULL;
	const char *        direction_str = NULL;
	NihDBusArgDir       direction;
	DBusError           error;

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

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

	/* Arguments should only appear inside methods or signals. */
	parent = parse_stack_top (&context->stack);
	if ((! parent) || ((parent->type != PARSE_METHOD)
			   && (parent->type != PARSE_SIGNAL)))
	{
		nih_warn ("%s:%zu:%zu: %s", context->filename,
			  (size_t)XML_GetCurrentLineNumber (xmlp),
			  (size_t)XML_GetCurrentColumnNumber (xmlp),
			  _("Ignored unexpected <arg> tag"));

		if (! parse_stack_push (NULL, &context->stack,
					PARSE_IGNORED, NULL))
			nih_return_system_error (-1);

		return 0;
	}

	/* Retrieve the name, type and direction from the attributes */
	for (key = attr; key && *key; key += 2) {
		value = key + 1;
		nih_assert (value && *value);

		if (! strcmp (*key, "name")) {
			name = *value;
		} else if (! strcmp (*key, "type")) {
			type = *value;
		} else if (! strcmp (*key, "direction")) {
			direction_str = *value;
		} else {
			nih_warn ("%s:%zu:%zu: %s: %s", context->filename,
				  (size_t)XML_GetCurrentLineNumber (xmlp),
				  (size_t)XML_GetCurrentColumnNumber (xmlp),
				  _("Ignored unknown <arg> attribute"),
				  *key);
		}
	}

	/* Check we have a type and that it's valid (name and direction
	 * are optional).  We also check the name is valid according to
	 * member rules, strictly speaking there is no such restriction,
	 * but we hereby invent one.
	 */
	if (name && (! argument_name_valid (name)))
		nih_return_error (-1, ARGUMENT_INVALID_NAME,
				  _(ARGUMENT_INVALID_NAME_STR));

	if (! type)
		nih_return_error (-1, ARGUMENT_MISSING_TYPE,
				  _(ARGUMENT_MISSING_TYPE_STR));

	dbus_error_init (&error);
	if (! dbus_signature_validate_single (type, &error)) {
		nih_error_raise_printf (ARGUMENT_INVALID_TYPE, "%s: %s",
					_(ARGUMENT_INVALID_TYPE_STR),
					error.message);
		dbus_error_free (&error);
		return -1;
	}

	switch (parent->type) {
	case PARSE_METHOD:
		if (! direction_str) {
			direction = NIH_DBUS_ARG_IN;
		} else if (! strcmp (direction_str, "in")) {
			direction = NIH_DBUS_ARG_IN;
		} else if (! strcmp (direction_str, "out")) {
			direction = NIH_DBUS_ARG_OUT;
		} else {
			nih_return_error (-1, ARGUMENT_ILLEGAL_METHOD_DIRECTION,
					  _(ARGUMENT_ILLEGAL_METHOD_DIRECTION_STR));
		}
		break;
	case PARSE_SIGNAL:
		if (! direction_str) {
			direction = NIH_DBUS_ARG_OUT;
		} else if (! strcmp (direction_str, "out")) {
			direction = NIH_DBUS_ARG_OUT;
		} else {
			nih_return_error (-1, ARGUMENT_ILLEGAL_SIGNAL_DIRECTION,
					  _(ARGUMENT_ILLEGAL_SIGNAL_DIRECTION_STR));
		}
		break;
	default:
		nih_assert_not_reached ();
	}

	/* Allocate an Argument object and push onto the stack */
	argument = argument_new (NULL, name, type, direction);
	if (! argument)
		nih_return_system_error (-1);

	if (! parse_stack_push (NULL, &context->stack,
				PARSE_ARGUMENT, argument)) {
		nih_error_raise_system ();
		return -1;
	}

	return 0;
}

/**
 * argument_end_tag:
 * @xmlp: XML parser,
 * @tag: name of XML tag being parsed.
 *
 * This function is called by parse_end_tag() for an "argument" end
 * tag, and matches a call to argument_start_tag() made at the same
 * parsing level.
 *
 * The argument is added to the list of arguments for the parent method
 * or signal.
 *
 * Returns: zero on success, negative value on raised error.
 **/
int
argument_end_tag (XML_Parser  xmlp,
		  const char *tag)
{
	ParseContext *context;
	ParseStack *  entry;
	ParseStack *  parent;
	Argument *    argument;
	Argument *    conflict;
	Method *      method;
	Signal *      signal;

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

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

	entry = parse_stack_top (&context->stack);
	nih_assert (entry != NULL);
	nih_assert (entry->type == PARSE_ARGUMENT);
	argument = entry->argument;

	/* Generate a symbol from the name if we have one */
	if (argument->name && (! argument->symbol)) {
		argument->symbol = symbol_from_name (argument, argument->name);
		if (! argument->symbol)
			nih_return_no_memory_error (-1);
	}

	nih_list_remove (&entry->entry);
	parent = parse_stack_top (&context->stack);
	nih_assert (parent != NULL);

	switch (parent->type) {
	case PARSE_METHOD:
		method = parent->method;

		/* Otherwise generate a symbol from the argument count */
		if (! argument->symbol) {
			size_t count = 0;

			NIH_LIST_FOREACH (&method->arguments, iter)
				count++;

			argument->symbol = nih_sprintf (argument, "arg%zu",
							++count);
			if (! argument->symbol) {
				nih_list_add_after (&context->stack,
						    &entry->entry);
				nih_return_no_memory_error (-1);
			}
		}

		/* Make sure there's not a conflict before adding the arg */
		conflict = method_lookup_argument (method, argument->symbol);
		if (conflict) {
			nih_error_raise_printf (ARGUMENT_DUPLICATE_SYMBOL,
						_(ARGUMENT_DUPLICATE_SYMBOL_STR),
						argument->symbol, conflict->name);
			return -1;
		}

		nih_debug ("Add %s argument to %s method",
			   argument->name ?: "(unknown)",
			   method->name);
		nih_list_add (&method->arguments, &argument->entry);
		nih_ref (argument, method);
		break;
	case PARSE_SIGNAL:
		signal = parent->signal;

		/* Otherwise generate a symbol from the argument count */
		if (! argument->symbol) {
			size_t count = 0;

			NIH_LIST_FOREACH (&signal->arguments, iter)
				count++;

			argument->symbol = nih_sprintf (argument, "arg%zu",
							++count);
			if (! argument->symbol) {
				nih_list_add_after (&context->stack,
						    &entry->entry);
				nih_return_no_memory_error (-1);
			}
		}

		/* Make sure there's not a conflict before adding the arg */
		conflict = signal_lookup_argument (signal, argument->symbol);
		if (conflict) {
			nih_error_raise_printf (ARGUMENT_DUPLICATE_SYMBOL,
						_(ARGUMENT_DUPLICATE_SYMBOL_STR),
						argument->symbol, conflict->name);
			return -1;
		}

		nih_debug ("Add %s argument to %s signal",
			   argument->name ?: "(unknown)",
			   signal->name);
		nih_list_add (&signal->arguments, &argument->entry);
		nih_ref (argument, signal);
		break;
	default:
		nih_assert_not_reached ();
	}

	nih_free (entry);

	return 0;
}


/**
 * argument_annotation:
 * @argument: argument object annotation applies to,
 * @name: annotation name,
 * @value: annotation value.
 *
 * Handles applying the annotation @name with value @value to the argument
 * @argument.  While the D-Bus Introspection specification does not permit
 * annotations for arguments, this is an nih-dbus-tool extension.  Arguments
 * may be annotated with an alternate symbol name specified.
 *
 * Unknown annotations or illegal values to the known annotations result
 * in an error being raised.
 *
 * Returns: zero on success, negative value on raised error.
 **/
int
argument_annotation (Argument *  argument,
		     const char *name,
		     const char *value)
{
	nih_assert (argument != NULL);
	nih_assert (name != NULL);
	nih_assert (value != NULL);

	if (! strcmp (name, "com.netsplit.Nih.Symbol")) {
		if (symbol_valid (value)) {
			if (argument->symbol)
				nih_unref (argument->symbol, argument);

			argument->symbol = nih_strdup (argument, value);
			if (! argument->symbol)
				nih_return_no_memory_error (-1);

			nih_debug ("Set %s argument symbol to %s",
				   argument->name ?: "(unknown)", argument->symbol);
		} else {
			nih_return_error (-1, ARGUMENT_INVALID_SYMBOL,
					  _(ARGUMENT_INVALID_SYMBOL_STR));
		}
	} else {
		nih_error_raise_printf (ARGUMENT_UNKNOWN_ANNOTATION,
					"%s: %s: %s",
					_(ARGUMENT_UNKNOWN_ANNOTATION_STR),
					argument->name ?: "(unnamed)",
					name);
		return -1;
	}

	return 0;
}