1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-gparser.c parse DBus description files
4 * Copyright (C) 2003, 2005 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "dbus-gparser.h"
24 #include "dbus-gidl.h"
28 #define _(x) gettext ((x))
31 #ifndef DOXYGEN_SHOULD_SKIP_THIS
33 #define ELEMENT_IS(name) (strcmp (element_name, (name)) == 0)
42 locate_attributes (const char *element_name,
43 const char **attribute_names,
44 const char **attribute_values,
46 const char *first_attribute_name,
47 const char **first_attribute_retloc,
55 LocateAttr attrs[MAX_ATTRS];
59 g_return_val_if_fail (first_attribute_name != NULL, FALSE);
60 g_return_val_if_fail (first_attribute_retloc != NULL, FALSE);
65 attrs[0].name = first_attribute_name;
66 attrs[0].retloc = first_attribute_retloc;
67 *first_attribute_retloc = NULL;
69 va_start (args, first_attribute_retloc);
71 name = va_arg (args, const char*);
72 retloc = va_arg (args, const char**);
76 g_return_val_if_fail (retloc != NULL, FALSE);
78 g_assert (n_attrs < MAX_ATTRS);
80 attrs[n_attrs].name = name;
81 attrs[n_attrs].retloc = retloc;
85 name = va_arg (args, const char*);
86 retloc = va_arg (args, const char**);
95 while (attribute_names[i])
104 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
106 retloc = attrs[j].retloc;
112 G_MARKUP_ERROR_PARSE,
113 _("Attribute \"%s\" repeated twice on the same <%s> element"),
114 attrs[j].name, element_name);
119 *retloc = attribute_values[i];
130 G_MARKUP_ERROR_PARSE,
131 _("Attribute \"%s\" is invalid on <%s> element in this context"),
132 attribute_names[i], element_name);
145 check_no_attributes (const char *element_name,
146 const char **attribute_names,
147 const char **attribute_values,
150 if (attribute_names[0] != NULL)
154 G_MARKUP_ERROR_PARSE,
155 _("Attribute \"%s\" is invalid on <%s> element in this context"),
156 attribute_names[0], element_name);
167 NodeInfo *result; /* Filled in when we pop the last node */
169 InterfaceInfo *interface;
172 PropertyInfo *property;
181 parser = g_new0 (Parser, 1);
183 parser->refcount = 1;
189 parser_ref (Parser *parser)
191 parser->refcount += 1;
197 parser_unref (Parser *parser)
199 parser->refcount -= 1;
200 if (parser->refcount == 0)
203 node_info_unref (parser->result);
210 parser_check_doctype (Parser *parser,
214 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
216 if (strcmp (doctype, "node") != 0)
220 G_MARKUP_ERROR_PARSE,
221 "D-BUS description file has the wrong document type %s, use node or interface",
230 parse_node (Parser *parser,
231 const char *element_name,
232 const char **attribute_names,
233 const char **attribute_values,
239 if (parser->interface ||
245 g_set_error (error, G_MARKUP_ERROR,
246 G_MARKUP_ERROR_PARSE,
247 _("Can't put <%s> element here"),
253 if (!locate_attributes (element_name, attribute_names,
254 attribute_values, error,
259 /* Only the root node can have no name */
260 if (parser->node_stack != NULL && name == NULL)
262 g_set_error (error, G_MARKUP_ERROR,
263 G_MARKUP_ERROR_PARSE,
264 _("\"%s\" attribute required on <%s> element "),
265 "name", element_name);
269 /* Root element name must be absolute */
270 if (parser->node_stack == NULL && name && *name != '/')
272 g_set_error (error, G_MARKUP_ERROR,
273 G_MARKUP_ERROR_PARSE,
274 _("\"%s\" attribute on <%s> element must be an absolute object path, \"%s\" not OK"),
275 "name", element_name, name);
279 /* Other element names must not be absolute */
280 if (parser->node_stack != NULL && name && *name == '/')
282 g_set_error (error, G_MARKUP_ERROR,
283 G_MARKUP_ERROR_PARSE,
284 _("\"%s\" attribute on <%s> element must not be an absolute object path, \"%s\" starts with /"),
285 "name", element_name, name);
289 node = node_info_new (name);
291 if (parser->node_stack != NULL)
293 node_info_add_node (parser->node_stack->data,
297 parser->node_stack = g_slist_prepend (parser->node_stack,
304 parse_interface (Parser *parser,
305 const char *element_name,
306 const char **attribute_names,
307 const char **attribute_values,
311 InterfaceInfo *iface;
314 if (parser->interface ||
319 (parser->node_stack == NULL))
321 g_set_error (error, G_MARKUP_ERROR,
322 G_MARKUP_ERROR_PARSE,
323 _("Can't put <%s> element here"),
329 if (!locate_attributes (element_name, attribute_names,
330 attribute_values, error,
337 g_set_error (error, G_MARKUP_ERROR,
338 G_MARKUP_ERROR_PARSE,
339 _("\"%s\" attribute required on <%s> element "),
340 "name", element_name);
344 top = parser->node_stack->data;
346 iface = interface_info_new (name);
347 node_info_add_interface (top, iface);
348 interface_info_unref (iface);
350 parser->interface = iface;
356 parse_method (Parser *parser,
357 const char *element_name,
358 const char **attribute_names,
359 const char **attribute_values,
366 if (parser->interface == NULL ||
367 parser->node_stack == NULL ||
373 g_set_error (error, G_MARKUP_ERROR,
374 G_MARKUP_ERROR_PARSE,
375 _("Can't put <%s> element here"),
381 if (!locate_attributes (element_name, attribute_names,
382 attribute_values, error,
389 g_set_error (error, G_MARKUP_ERROR,
390 G_MARKUP_ERROR_PARSE,
391 _("\"%s\" attribute required on <%s> element "),
392 "name", element_name);
396 top = parser->node_stack->data;
398 method = method_info_new (name);
399 interface_info_add_method (parser->interface, method);
400 method_info_unref (method);
402 parser->method = method;
408 parse_signal (Parser *parser,
409 const char *element_name,
410 const char **attribute_names,
411 const char **attribute_values,
418 if (parser->interface == NULL ||
419 parser->node_stack == NULL ||
425 g_set_error (error, G_MARKUP_ERROR,
426 G_MARKUP_ERROR_PARSE,
427 _("Can't put <%s> element here"),
433 if (!locate_attributes (element_name, attribute_names,
434 attribute_values, error,
441 g_set_error (error, G_MARKUP_ERROR,
442 G_MARKUP_ERROR_PARSE,
443 _("\"%s\" attribute required on <%s> element "),
444 "name", element_name);
448 top = parser->node_stack->data;
450 signal = signal_info_new (name);
451 interface_info_add_signal (parser->interface, signal);
452 signal_info_unref (signal);
454 parser->signal = signal;
460 basic_type_from_string (const char *str)
462 if (strcmp (str, "string") == 0)
463 return DBUS_TYPE_STRING;
464 else if (strcmp (str, "int16") == 0)
465 return DBUS_TYPE_INT16;
466 else if (strcmp (str, "uint16") == 0)
467 return DBUS_TYPE_UINT16;
468 else if (strcmp (str, "int32") == 0)
469 return DBUS_TYPE_INT32;
470 else if (strcmp (str, "uint32") == 0)
471 return DBUS_TYPE_UINT32;
472 else if (strcmp (str, "int64") == 0)
473 return DBUS_TYPE_INT64;
474 else if (strcmp (str, "uint64") == 0)
475 return DBUS_TYPE_UINT64;
476 else if (strcmp (str, "double") == 0)
477 return DBUS_TYPE_DOUBLE;
478 else if (strcmp (str, "byte") == 0)
479 return DBUS_TYPE_BYTE;
480 else if (strcmp (str, "boolean") == 0)
481 return DBUS_TYPE_BOOLEAN;
482 else if (strcmp (str, "byte") == 0)
483 return DBUS_TYPE_BYTE;
484 else if (strcmp (str, "object") == 0)
485 return DBUS_TYPE_OBJECT_PATH;
487 return DBUS_TYPE_INVALID;
490 /* FIXME we have to allow type signatures, not just basic types
493 type_from_string (const char *str,
494 const char *element_name,
499 t = basic_type_from_string (str);
501 if (t == DBUS_TYPE_INVALID)
503 g_set_error (error, G_MARKUP_ERROR,
504 G_MARKUP_ERROR_PARSE,
505 _("Type \"%s\" not understood on <%s> element "),
513 parse_property (Parser *parser,
514 const char *element_name,
515 const char **attribute_names,
516 const char **attribute_values,
522 PropertyInfo *property;
524 PropertyAccessFlags access_flags;
527 if (parser->interface == NULL ||
528 parser->node_stack == NULL ||
534 g_set_error (error, G_MARKUP_ERROR,
535 G_MARKUP_ERROR_PARSE,
536 _("Can't put <%s> element here"),
542 if (!locate_attributes (element_name, attribute_names,
543 attribute_values, error,
552 g_set_error (error, G_MARKUP_ERROR,
553 G_MARKUP_ERROR_PARSE,
554 _("\"%s\" attribute required on <%s> element "),
555 "name", element_name);
561 g_set_error (error, G_MARKUP_ERROR,
562 G_MARKUP_ERROR_PARSE,
563 _("\"%s\" attribute required on <%s> element "),
564 "access", element_name);
570 g_set_error (error, G_MARKUP_ERROR,
571 G_MARKUP_ERROR_PARSE,
572 _("\"%s\" attribute required on <%s> element "),
573 "type", element_name);
577 t = type_from_string (type, element_name, error);
578 if (t == DBUS_TYPE_INVALID)
582 if (strcmp (access, "readwrite") == 0)
583 access_flags = PROPERTY_READ | PROPERTY_WRITE;
584 else if (strcmp (access, "read") == 0)
585 access_flags = PROPERTY_READ;
586 else if (strcmp (access, "write") == 0)
587 access_flags = PROPERTY_WRITE;
590 g_set_error (error, G_MARKUP_ERROR,
591 G_MARKUP_ERROR_PARSE,
592 _("access=\"%s\" must have value readwrite, read, or write on %s\n"),
593 access, element_name);
597 top = parser->node_stack->data;
599 property = property_info_new (name, t, access_flags);
600 interface_info_add_property (parser->interface, property);
601 property_info_unref (property);
603 parser->property = property;
609 parse_arg (Parser *parser,
610 const char *element_name,
611 const char **attribute_names,
612 const char **attribute_values,
617 const char *direction;
622 if (!(parser->method || parser->signal) ||
623 parser->node_stack == NULL ||
627 g_set_error (error, G_MARKUP_ERROR,
628 G_MARKUP_ERROR_PARSE,
629 _("Can't put <%s> element here"),
635 if (!locate_attributes (element_name, attribute_names,
636 attribute_values, error,
639 "direction", &direction,
643 /* name can be null for args */
647 g_set_error (error, G_MARKUP_ERROR,
648 G_MARKUP_ERROR_PARSE,
649 _("\"%s\" attribute required on <%s> element "),
650 "type", element_name);
654 if (direction == NULL)
656 /* methods default to in, signal to out */
659 else if (parser->signal)
662 g_assert_not_reached ();
667 if (strcmp (direction, "in") == 0)
669 else if (strcmp (direction, "out") == 0)
672 if (dir == ARG_INVALID ||
673 (parser->signal && dir == ARG_IN))
676 g_set_error (error, G_MARKUP_ERROR,
677 G_MARKUP_ERROR_PARSE,
678 _("Signals must have direction=\"out\" (just omit the direction attribute)"));
680 g_set_error (error, G_MARKUP_ERROR,
681 G_MARKUP_ERROR_PARSE,
682 _("\"%s\" attribute on <%s> has value \"in\" or \"out\""),
683 "direction", element_name);
687 t = type_from_string (type, element_name, error);
688 if (t == DBUS_TYPE_INVALID)
691 arg = arg_info_new (name, dir, t);
693 method_info_add_arg (parser->method, arg);
694 else if (parser->signal)
695 signal_info_add_arg (parser->signal, arg);
697 g_assert_not_reached ();
699 arg_info_unref (arg);
707 parser_start_element (Parser *parser,
708 const char *element_name,
709 const char **attribute_names,
710 const char **attribute_values,
713 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
715 if (ELEMENT_IS ("node"))
717 if (!parse_node (parser, element_name, attribute_names,
718 attribute_values, error))
721 else if (ELEMENT_IS ("interface"))
723 if (!parse_interface (parser, element_name, attribute_names,
724 attribute_values, error))
727 else if (ELEMENT_IS ("method"))
729 if (!parse_method (parser, element_name, attribute_names,
730 attribute_values, error))
733 else if (ELEMENT_IS ("signal"))
735 if (!parse_signal (parser, element_name, attribute_names,
736 attribute_values, error))
739 else if (ELEMENT_IS ("property"))
741 if (!parse_property (parser, element_name, attribute_names,
742 attribute_values, error))
745 else if (ELEMENT_IS ("arg"))
747 if (!parse_arg (parser, element_name, attribute_names,
748 attribute_values, error))
753 g_set_error (error, G_MARKUP_ERROR,
754 G_MARKUP_ERROR_PARSE,
755 _("Element <%s> not recognized"),
763 parser_end_element (Parser *parser,
764 const char *element_name,
767 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
769 if (ELEMENT_IS ("interface"))
771 parser->interface = NULL;
773 else if (ELEMENT_IS ("method"))
775 parser->method = NULL;
777 else if (ELEMENT_IS ("signal"))
779 parser->signal = NULL;
781 else if (ELEMENT_IS ("property"))
783 parser->property = NULL;
785 else if (ELEMENT_IS ("arg"))
789 else if (ELEMENT_IS ("node"))
793 g_assert (parser->node_stack != NULL);
794 top = parser->node_stack->data;
796 parser->node_stack = g_slist_remove (parser->node_stack,
799 if (parser->node_stack == NULL)
800 parser->result = top; /* We are done, store the result */
803 g_assert_not_reached (); /* should have had an error on start_element */
809 parser_content (Parser *parser,
814 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
816 /* FIXME check that it's all whitespace */
822 parser_finished (Parser *parser,
825 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
831 parser_get_nodes (Parser *parser)
833 return parser->result;
836 #endif /* DOXYGEN_SHOULD_SKIP_THIS */