1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2009 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: David Zeuthen <davidz@redhat.com>
28 #include "gdbusintrospection.h"
33 * SECTION:gdbusintrospection
34 * @title: Introspection XML
35 * @short_description: Parse and Generate Introspection XML
38 * Various data structures and convenience routines to parse and
39 * generate D-Bus introspection XML.
42 /* ---------------------------------------------------------------------------------------------------- */
44 /* See also https://bugzilla.gnome.org/show_bug.cgi?id=449565 ... */
45 #define _MY_DEFINE_BOXED_TYPE(TypeName, type_name) \
47 type_name##_get_type (void) \
49 static volatile gsize type_volatile = 0; \
50 if (g_once_init_enter (&type_volatile)) \
52 GType type = g_boxed_type_register_static (g_intern_static_string (#TypeName), \
53 (GBoxedCopyFunc) type_name##_ref, \
54 (GBoxedFreeFunc) type_name##_unref); \
55 g_once_init_leave (&type_volatile, type); \
57 return (GType) type_volatile; \
60 _MY_DEFINE_BOXED_TYPE (GDBusNodeInfo, g_dbus_node_info);
61 _MY_DEFINE_BOXED_TYPE (GDBusInterfaceInfo, g_dbus_interface_info);
62 _MY_DEFINE_BOXED_TYPE (GDBusMethodInfo, g_dbus_method_info);
63 _MY_DEFINE_BOXED_TYPE (GDBusPropertyInfo, g_dbus_property_info);
64 _MY_DEFINE_BOXED_TYPE (GDBusArgInfo, g_dbus_arg_info);
65 _MY_DEFINE_BOXED_TYPE (GDBusAnnotationInfo, g_dbus_annotation_info);
67 /* ---------------------------------------------------------------------------------------------------- */
71 /* stuff we are currently collecting */
76 GPtrArray *properties;
77 GPtrArray *interfaces;
79 GPtrArray *annotations;
81 /* A list of GPtrArray's containing annotations */
82 GSList *annotations_stack;
84 /* A list of GPtrArray's containing interfaces */
85 GSList *interfaces_stack;
87 /* A list of GPtrArray's containing nodes */
90 /* Whether the direction was "in" for last parsed arg */
91 gboolean last_arg_was_in;
93 /* Number of args currently being collected; used for assigning
94 * names to args without a "name" attribute
100 /* ---------------------------------------------------------------------------------------------------- */
103 * g_dbus_node_info_ref:
104 * @info: A #GDBusNodeInfo
106 * If @info is statically allocated does nothing. Otherwise increases
107 * the reference count.
109 * Returns: The same @info.
114 g_dbus_node_info_ref (GDBusNodeInfo *info)
116 if (info->ref_count == -1)
118 g_atomic_int_inc (&info->ref_count);
123 * g_dbus_interface_info_ref:
124 * @info: A #GDBusInterfaceInfo
126 * If @info is statically allocated does nothing. Otherwise increases
127 * the reference count.
129 * Returns: The same @info.
134 g_dbus_interface_info_ref (GDBusInterfaceInfo *info)
136 if (info->ref_count == -1)
138 g_atomic_int_inc (&info->ref_count);
143 * g_dbus_method_info_ref:
144 * @info: A #GDBusMethodInfo
146 * If @info is statically allocated does nothing. Otherwise increases
147 * the reference count.
149 * Returns: The same @info.
154 g_dbus_method_info_ref (GDBusMethodInfo *info)
156 if (info->ref_count == -1)
158 g_atomic_int_inc (&info->ref_count);
163 * g_dbus_signal_info_ref:
164 * @info: A #GDBusSignalInfo
166 * If @info is statically allocated does nothing. Otherwise increases
167 * the reference count.
169 * Returns: The same @info.
174 g_dbus_signal_info_ref (GDBusSignalInfo *info)
176 if (info->ref_count == -1)
178 g_atomic_int_inc (&info->ref_count);
183 * g_dbus_property_info_ref:
184 * @info: A #GDBusPropertyInfo
186 * If @info is statically allocated does nothing. Otherwise increases
187 * the reference count.
189 * Returns: The same @info.
194 g_dbus_property_info_ref (GDBusPropertyInfo *info)
196 if (info->ref_count == -1)
198 g_atomic_int_inc (&info->ref_count);
203 * g_dbus_arg_info_ref:
204 * @info: A #GDBusArgInfo
206 * If @info is statically allocated does nothing. Otherwise increases
207 * the reference count.
209 * Returns: The same @info.
214 g_dbus_arg_info_ref (GDBusArgInfo *info)
216 if (info->ref_count == -1)
218 g_atomic_int_inc (&info->ref_count);
223 * g_dbus_node_info_ref:
224 * @info: A #GDBusNodeInfo
226 * If @info is statically allocated does nothing. Otherwise increases
227 * the reference count.
229 * Returns: The same @info.
233 GDBusAnnotationInfo *
234 g_dbus_annotation_info_ref (GDBusAnnotationInfo *info)
236 if (info->ref_count == -1)
238 g_atomic_int_inc (&info->ref_count);
242 /* ---------------------------------------------------------------------------------------------------- */
245 free_null_terminated_array (gpointer array, GDestroyNotify unref_func)
251 for (n = 0; p[n] != NULL; n++)
257 * g_dbus_annotation_info_unref:
258 * @info: A #GDBusAnnotationInfo.
260 * If @info is statically allocated, does nothing. Otherwise decreases
261 * the reference count of @info. When its reference count drops to 0,
262 * the memory used is freed.
267 g_dbus_annotation_info_unref (GDBusAnnotationInfo *info)
269 if (info->ref_count == -1)
271 if (g_atomic_int_dec_and_test (&info->ref_count))
274 g_free (info->value);
275 free_null_terminated_array (info->annotations, (GDestroyNotify) g_dbus_annotation_info_unref);
281 * g_dbus_arg_info_unref:
282 * @info: A #GDBusArgInfo.
284 * If @info is statically allocated, does nothing. Otherwise decreases
285 * the reference count of @info. When its reference count drops to 0,
286 * the memory used is freed.
291 g_dbus_arg_info_unref (GDBusArgInfo *info)
293 if (info->ref_count == -1)
295 if (g_atomic_int_dec_and_test (&info->ref_count))
298 g_free (info->signature);
299 free_null_terminated_array (info->annotations, (GDestroyNotify) g_dbus_annotation_info_unref);
305 * g_dbus_method_info_unref:
306 * @info: A #GDBusMethodInfo.
308 * If @info is statically allocated, does nothing. Otherwise decreases
309 * the reference count of @info. When its reference count drops to 0,
310 * the memory used is freed.
315 g_dbus_method_info_unref (GDBusMethodInfo *info)
317 if (info->ref_count == -1)
319 if (g_atomic_int_dec_and_test (&info->ref_count))
322 free_null_terminated_array (info->in_args, (GDestroyNotify) g_dbus_arg_info_unref);
323 free_null_terminated_array (info->out_args, (GDestroyNotify) g_dbus_arg_info_unref);
324 free_null_terminated_array (info->annotations, (GDestroyNotify) g_dbus_annotation_info_unref);
330 * g_dbus_signal_info_unref:
331 * @info: A #GDBusSignalInfo.
333 * If @info is statically allocated, does nothing. Otherwise decreases
334 * the reference count of @info. When its reference count drops to 0,
335 * the memory used is freed.
340 g_dbus_signal_info_unref (GDBusSignalInfo *info)
342 if (info->ref_count == -1)
344 if (g_atomic_int_dec_and_test (&info->ref_count))
347 free_null_terminated_array (info->args, (GDestroyNotify) g_dbus_arg_info_unref);
348 free_null_terminated_array (info->annotations, (GDestroyNotify) g_dbus_annotation_info_unref);
354 * g_dbus_property_info_unref:
355 * @info: A #GDBusPropertyInfo.
357 * If @info is statically allocated, does nothing. Otherwise decreases
358 * the reference count of @info. When its reference count drops to 0,
359 * the memory used is freed.
364 g_dbus_property_info_unref (GDBusPropertyInfo *info)
366 if (info->ref_count == -1)
368 if (g_atomic_int_dec_and_test (&info->ref_count))
371 g_free (info->signature);
372 free_null_terminated_array (info->annotations, (GDestroyNotify) g_dbus_annotation_info_unref);
378 * g_dbus_interface_info_unref:
379 * @info: A #GDBusInterfaceInfo.
381 * If @info is statically allocated, does nothing. Otherwise decreases
382 * the reference count of @info. When its reference count drops to 0,
383 * the memory used is freed.
388 g_dbus_interface_info_unref (GDBusInterfaceInfo *info)
390 if (info->ref_count == -1)
392 if (g_atomic_int_dec_and_test (&info->ref_count))
395 free_null_terminated_array (info->methods, (GDestroyNotify) g_dbus_method_info_unref);
396 free_null_terminated_array (info->signals, (GDestroyNotify) g_dbus_signal_info_unref);
397 free_null_terminated_array (info->properties, (GDestroyNotify) g_dbus_property_info_unref);
398 free_null_terminated_array (info->annotations, (GDestroyNotify) g_dbus_annotation_info_unref);
404 * g_dbus_node_info_unref:
405 * @info: A #GDBusNodeInfo.
407 * If @info is statically allocated, does nothing. Otherwise decreases
408 * the reference count of @info. When its reference count drops to 0,
409 * the memory used is freed.
414 g_dbus_node_info_unref (GDBusNodeInfo *info)
416 if (info->ref_count == -1)
418 if (g_atomic_int_dec_and_test (&info->ref_count))
421 free_null_terminated_array (info->interfaces, (GDestroyNotify) g_dbus_interface_info_unref);
422 free_null_terminated_array (info->nodes, (GDestroyNotify) g_dbus_node_info_unref);
423 free_null_terminated_array (info->annotations, (GDestroyNotify) g_dbus_annotation_info_unref);
428 /* ---------------------------------------------------------------------------------------------------- */
431 g_dbus_annotation_info_set (ParseData *data,
432 GDBusAnnotationInfo *info,
435 GDBusAnnotationInfo **embedded_annotations)
440 info->key = g_strdup (key);
443 info->value = g_strdup (value);
445 if (embedded_annotations != NULL)
446 info->annotations = embedded_annotations;
450 g_dbus_arg_info_set (ParseData *data,
453 const gchar *signature,
454 GDBusAnnotationInfo **annotations)
458 /* name may be NULL - TODO: compute name? */
460 info->name = g_strdup (name);
462 if (signature != NULL)
463 info->signature = g_strdup (signature);
465 if (annotations != NULL)
466 info->annotations = annotations;
470 g_dbus_method_info_set (ParseData *data,
471 GDBusMethodInfo *info,
474 GDBusArgInfo **in_args,
476 GDBusArgInfo **out_args,
477 GDBusAnnotationInfo **annotations)
482 info->name = g_strdup (name);
484 if (in_num_args != 0)
486 //info->in_num_args = in_num_args;
487 info->in_args = in_args;
490 if (out_num_args != 0)
492 //info->out_num_args = out_num_args;
493 info->out_args = out_args;
496 if (annotations != NULL)
497 info->annotations = annotations;
501 g_dbus_signal_info_set (ParseData *data,
502 GDBusSignalInfo *info,
506 GDBusAnnotationInfo **annotations)
511 info->name = g_strdup (name);
515 //info->num_args = num_args;
519 if (annotations != NULL)
521 info->annotations = annotations;
526 g_dbus_property_info_set (ParseData *data,
527 GDBusPropertyInfo *info,
529 const gchar *signature,
530 GDBusPropertyInfoFlags flags,
531 GDBusAnnotationInfo **annotations)
536 info->name = g_strdup (name);
538 if (flags != G_DBUS_PROPERTY_INFO_FLAGS_NONE)
541 if (signature != NULL)
543 info->signature = g_strdup (signature);
546 if (annotations != NULL)
548 info->annotations = annotations;
553 g_dbus_interface_info_set (ParseData *data,
554 GDBusInterfaceInfo *info,
557 GDBusMethodInfo **methods,
559 GDBusSignalInfo **signals,
560 guint num_properties,
561 GDBusPropertyInfo **properties,
562 GDBusAnnotationInfo **annotations)
568 info->name = g_strdup (name);
571 if (num_methods != 0)
573 //info->num_methods = num_methods;
574 info->methods = methods;
577 if (num_signals != 0)
579 //info->num_signals = num_signals;
580 info->signals = signals;
583 if (num_properties != 0)
585 //info->num_properties = num_properties;
586 info->properties = properties;
589 if (annotations != NULL)
591 info->annotations = annotations;
596 g_dbus_node_info_set (ParseData *data,
599 guint num_interfaces,
600 GDBusInterfaceInfo **interfaces,
602 GDBusNodeInfo **nodes,
603 GDBusAnnotationInfo **annotations)
609 info->path = g_strdup (path);
610 /* TODO: relative / absolute path snafu */
613 if (num_interfaces != 0)
615 //info->num_interfaces = num_interfaces;
616 info->interfaces = interfaces;
621 //info->num_nodes = num_nodes;
625 if (annotations != NULL)
627 info->annotations = annotations;
632 /* ---------------------------------------------------------------------------------------------------- */
635 g_dbus_annotation_info_generate_xml (const GDBusAnnotationInfo *info,
637 GString *string_builder)
641 g_string_append_printf (string_builder, "%*s<annotation name=\"%s\" value=\"%s\"",
646 if (info->annotations == NULL)
648 g_string_append (string_builder, "/>\n");
652 g_string_append (string_builder, ">\n");
654 for (n = 0; info->annotations != NULL && info->annotations[n] != NULL; n++)
655 g_dbus_annotation_info_generate_xml (info->annotations[n],
659 g_string_append_printf (string_builder, "%*s</annotation>\n",
666 g_dbus_arg_info_generate_xml (const GDBusArgInfo *info,
668 const gchar *extra_attributes,
669 GString *string_builder)
673 g_string_append_printf (string_builder, "%*s<arg type=\"%s\"",
677 if (info->name != NULL)
678 g_string_append_printf (string_builder, " name=\"%s\"", info->name);
680 if (extra_attributes != NULL)
681 g_string_append_printf (string_builder, " %s", extra_attributes);
683 if (info->annotations == NULL)
685 g_string_append (string_builder, "/>\n");
689 g_string_append (string_builder, ">\n");
691 for (n = 0; info->annotations != NULL && info->annotations[n] != NULL; n++)
692 g_dbus_annotation_info_generate_xml (info->annotations[n],
696 g_string_append_printf (string_builder, "%*s</arg>\n", indent, "");
702 g_dbus_method_info_generate_xml (const GDBusMethodInfo *info,
704 GString *string_builder)
708 g_string_append_printf (string_builder, "%*s<method name=\"%s\"",
712 if (info->annotations == NULL && info->in_args == NULL && info->out_args == NULL)
714 g_string_append (string_builder, "/>\n");
718 g_string_append (string_builder, ">\n");
720 for (n = 0; info->annotations != NULL && info->annotations[n] != NULL; n++)
721 g_dbus_annotation_info_generate_xml (info->annotations[n],
725 for (n = 0; info->in_args != NULL && info->in_args[n] != NULL; n++)
726 g_dbus_arg_info_generate_xml (info->in_args[n],
731 for (n = 0; info->out_args != NULL && info->out_args[n] != NULL; n++)
732 g_dbus_arg_info_generate_xml (info->out_args[n],
737 g_string_append_printf (string_builder, "%*s</method>\n", indent, "");
742 g_dbus_signal_info_generate_xml (const GDBusSignalInfo *info,
744 GString *string_builder)
748 g_string_append_printf (string_builder, "%*s<signal name=\"%s\"",
752 if (info->annotations == NULL && info->args == NULL)
754 g_string_append (string_builder, "/>\n");
758 g_string_append (string_builder, ">\n");
760 for (n = 0; info->annotations != NULL && info->annotations[n] != NULL; n++)
761 g_dbus_annotation_info_generate_xml (info->annotations[n],
765 for (n = 0; info->args != NULL && info->args[n] != NULL; n++)
766 g_dbus_arg_info_generate_xml (info->args[n],
771 g_string_append_printf (string_builder, "%*s</signal>\n", indent, "");
776 g_dbus_property_info_generate_xml (const GDBusPropertyInfo *info,
778 GString *string_builder)
781 const gchar *access_string;
783 if ((info->flags & G_DBUS_PROPERTY_INFO_FLAGS_READABLE) &&
784 (info->flags & G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE))
786 access_string = "readwrite";
788 else if (info->flags & G_DBUS_PROPERTY_INFO_FLAGS_READABLE)
790 access_string = "read";
792 else if (info->flags & G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE)
794 access_string = "write";
798 g_assert_not_reached ();
801 g_string_append_printf (string_builder, "%*s<property type=\"%s\" name=\"%s\" access=\"%s\"",
807 if (info->annotations == NULL)
809 g_string_append (string_builder, "/>\n");
813 g_string_append (string_builder, ">\n");
815 for (n = 0; info->annotations != NULL && info->annotations[n] != NULL; n++)
816 g_dbus_annotation_info_generate_xml (info->annotations[n],
820 g_string_append_printf (string_builder, "%*s</property>\n", indent, "");
826 * g_dbus_interface_info_generate_xml:
827 * @info: A #GDBusNodeInfo
828 * @indent: Indentation level.
829 * @string_builder: A #GString to to append XML data to.
831 * Appends an XML representation of @info (and its children) to @string_builder.
833 * This function is typically used for generating introspection XML
834 * documents at run-time for handling the
835 * <literal>org.freedesktop.DBus.Introspectable.Introspect</literal>
841 g_dbus_interface_info_generate_xml (const GDBusInterfaceInfo *info,
843 GString *string_builder)
847 g_string_append_printf (string_builder, "%*s<interface name=\"%s\">\n",
851 for (n = 0; info->annotations != NULL && info->annotations[n] != NULL; n++)
852 g_dbus_annotation_info_generate_xml (info->annotations[n],
856 for (n = 0; info->methods != NULL && info->methods[n] != NULL; n++)
857 g_dbus_method_info_generate_xml (info->methods[n],
861 for (n = 0; info->signals != NULL && info->signals[n] != NULL; n++)
862 g_dbus_signal_info_generate_xml (info->signals[n],
866 for (n = 0; info->properties != NULL && info->properties[n] != NULL; n++)
867 g_dbus_property_info_generate_xml (info->properties[n],
871 g_string_append_printf (string_builder, "%*s</interface>\n", indent, "");
875 * g_dbus_node_info_generate_xml:
876 * @node_info: A #GDBusNodeInfo.
877 * @indent: Indentation level.
878 * @string_builder: A #GString to to append XML data to.
880 * Appends an XML representation of @node_info (and its children) to @string_builder.
882 * This function is typically used for generating introspection XML documents at run-time for
883 * handling the <literal>org.freedesktop.DBus.Introspectable.Introspect</literal> method.
888 g_dbus_node_info_generate_xml (const GDBusNodeInfo *node_info,
890 GString *string_builder)
894 g_string_append_printf (string_builder, "%*s<node", indent, "");
895 if (node_info->path != NULL)
896 g_string_append_printf (string_builder, " name=\"%s\"", node_info->path);
898 if (node_info->interfaces == NULL && node_info->nodes == NULL && node_info->annotations == NULL)
900 g_string_append (string_builder, "/>\n");
904 g_string_append (string_builder, ">\n");
906 for (n = 0; node_info->annotations != NULL && node_info->annotations[n] != NULL; n++)
907 g_dbus_annotation_info_generate_xml (node_info->annotations[n],
911 for (n = 0; node_info->interfaces != NULL && node_info->interfaces[n] != NULL; n++)
912 g_dbus_interface_info_generate_xml (node_info->interfaces[n],
916 for (n = 0; node_info->nodes != NULL && node_info->nodes[n] != NULL; n++)
917 g_dbus_node_info_generate_xml (node_info->nodes[n],
921 g_string_append_printf (string_builder, "%*s</node>\n", indent, "");
925 /* ---------------------------------------------------------------------------------------------------- */
927 static GDBusAnnotationInfo **
928 parse_data_steal_annotations (ParseData *data, guint *out_num_elements)
930 GDBusAnnotationInfo **ret;
931 if (out_num_elements != NULL)
932 *out_num_elements = data->annotations->len;
933 if (data->annotations == NULL)
937 g_ptr_array_add (data->annotations, NULL);
938 ret = (GDBusAnnotationInfo **) g_ptr_array_free (data->annotations, FALSE);
940 data->annotations = g_ptr_array_new ();
944 static GDBusArgInfo **
945 parse_data_steal_args (ParseData *data, guint *out_num_elements)
948 if (out_num_elements != NULL)
949 *out_num_elements = data->args->len;
950 if (data->args == NULL)
954 g_ptr_array_add (data->args, NULL);
955 ret = (GDBusArgInfo **) g_ptr_array_free (data->args, FALSE);
957 data->args = g_ptr_array_new ();
961 static GDBusArgInfo **
962 parse_data_steal_out_args (ParseData *data, guint *out_num_elements)
965 if (out_num_elements != NULL)
966 *out_num_elements = data->out_args->len;
967 if (data->out_args == NULL)
971 g_ptr_array_add (data->out_args, NULL);
972 ret = (GDBusArgInfo **) g_ptr_array_free (data->out_args, FALSE);
974 data->out_args = g_ptr_array_new ();
978 static GDBusMethodInfo **
979 parse_data_steal_methods (ParseData *data, guint *out_num_elements)
981 GDBusMethodInfo **ret;
982 if (out_num_elements != NULL)
983 *out_num_elements = data->methods->len;
984 if (data->methods == NULL)
988 g_ptr_array_add (data->methods, NULL);
989 ret = (GDBusMethodInfo **) g_ptr_array_free (data->methods, FALSE);
991 data->methods = g_ptr_array_new ();
995 static GDBusSignalInfo **
996 parse_data_steal_signals (ParseData *data, guint *out_num_elements)
998 GDBusSignalInfo **ret;
999 if (out_num_elements != NULL)
1000 *out_num_elements = data->signals->len;
1001 if (data->signals == NULL)
1005 g_ptr_array_add (data->signals, NULL);
1006 ret = (GDBusSignalInfo **) g_ptr_array_free (data->signals, FALSE);
1008 data->signals = g_ptr_array_new ();
1012 static GDBusPropertyInfo **
1013 parse_data_steal_properties (ParseData *data, guint *out_num_elements)
1015 GDBusPropertyInfo **ret;
1016 if (out_num_elements != NULL)
1017 *out_num_elements = data->properties->len;
1018 if (data->properties == NULL)
1022 g_ptr_array_add (data->properties, NULL);
1023 ret = (GDBusPropertyInfo **) g_ptr_array_free (data->properties, FALSE);
1025 data->properties = g_ptr_array_new ();
1029 static GDBusInterfaceInfo **
1030 parse_data_steal_interfaces (ParseData *data, guint *out_num_elements)
1032 GDBusInterfaceInfo **ret;
1033 if (out_num_elements != NULL)
1034 *out_num_elements = data->interfaces->len;
1035 if (data->interfaces == NULL)
1039 g_ptr_array_add (data->interfaces, NULL);
1040 ret = (GDBusInterfaceInfo **) g_ptr_array_free (data->interfaces, FALSE);
1042 data->interfaces = g_ptr_array_new ();
1046 static GDBusNodeInfo **
1047 parse_data_steal_nodes (ParseData *data, guint *out_num_elements)
1049 GDBusNodeInfo **ret;
1050 if (out_num_elements != NULL)
1051 *out_num_elements = data->nodes->len;
1052 if (data->nodes == NULL)
1056 g_ptr_array_add (data->nodes, NULL);
1057 ret = (GDBusNodeInfo **) g_ptr_array_free (data->nodes, FALSE);
1059 data->nodes = g_ptr_array_new ();
1063 /* ---------------------------------------------------------------------------------------------------- */
1066 parse_data_free_annotations (ParseData *data)
1068 if (data->annotations == NULL)
1070 g_ptr_array_foreach (data->annotations, (GFunc) g_dbus_annotation_info_unref, NULL);
1071 g_ptr_array_free (data->annotations, TRUE);
1072 data->annotations = NULL;
1076 parse_data_free_args (ParseData *data)
1078 if (data->args == NULL)
1080 g_ptr_array_foreach (data->args, (GFunc) g_dbus_arg_info_unref, NULL);
1081 g_ptr_array_free (data->args, TRUE);
1086 parse_data_free_out_args (ParseData *data)
1088 if (data->out_args == NULL)
1090 g_ptr_array_foreach (data->out_args, (GFunc) g_dbus_arg_info_unref, NULL);
1091 g_ptr_array_free (data->out_args, TRUE);
1092 data->out_args = NULL;
1096 parse_data_free_methods (ParseData *data)
1098 if (data->methods == NULL)
1100 g_ptr_array_foreach (data->methods, (GFunc) g_dbus_method_info_unref, NULL);
1101 g_ptr_array_free (data->methods, TRUE);
1102 data->methods = NULL;
1106 parse_data_free_signals (ParseData *data)
1108 if (data->signals == NULL)
1110 g_ptr_array_foreach (data->signals, (GFunc) g_dbus_signal_info_unref, NULL);
1111 g_ptr_array_free (data->signals, TRUE);
1112 data->signals = NULL;
1116 parse_data_free_properties (ParseData *data)
1118 if (data->properties == NULL)
1120 g_ptr_array_foreach (data->properties, (GFunc) g_dbus_property_info_unref, NULL);
1121 g_ptr_array_free (data->properties, TRUE);
1122 data->properties = NULL;
1126 parse_data_free_interfaces (ParseData *data)
1128 if (data->interfaces == NULL)
1130 g_ptr_array_foreach (data->interfaces, (GFunc) g_dbus_interface_info_unref, NULL);
1131 g_ptr_array_free (data->interfaces, TRUE);
1132 data->interfaces = NULL;
1136 parse_data_free_nodes (ParseData *data)
1138 if (data->nodes == NULL)
1140 g_ptr_array_foreach (data->nodes, (GFunc) g_dbus_node_info_unref, NULL);
1141 g_ptr_array_free (data->nodes, TRUE);
1145 /* ---------------------------------------------------------------------------------------------------- */
1147 static GDBusAnnotationInfo *
1148 parse_data_get_annotation (ParseData *data, gboolean create_new)
1151 g_ptr_array_add (data->annotations, g_new0 (GDBusAnnotationInfo, 1));
1152 return data->annotations->pdata[data->annotations->len - 1];
1155 static GDBusArgInfo *
1156 parse_data_get_arg (ParseData *data, gboolean create_new)
1159 g_ptr_array_add (data->args, g_new0 (GDBusArgInfo, 1));
1160 return data->args->pdata[data->args->len - 1];
1163 static GDBusArgInfo *
1164 parse_data_get_out_arg (ParseData *data, gboolean create_new)
1167 g_ptr_array_add (data->out_args, g_new0 (GDBusArgInfo, 1));
1168 return data->out_args->pdata[data->out_args->len - 1];
1171 static GDBusMethodInfo *
1172 parse_data_get_method (ParseData *data, gboolean create_new)
1175 g_ptr_array_add (data->methods, g_new0 (GDBusMethodInfo, 1));
1176 return data->methods->pdata[data->methods->len - 1];
1179 static GDBusSignalInfo *
1180 parse_data_get_signal (ParseData *data, gboolean create_new)
1183 g_ptr_array_add (data->signals, g_new0 (GDBusSignalInfo, 1));
1184 return data->signals->pdata[data->signals->len - 1];
1187 static GDBusPropertyInfo *
1188 parse_data_get_property (ParseData *data, gboolean create_new)
1191 g_ptr_array_add (data->properties, g_new0 (GDBusPropertyInfo, 1));
1192 return data->properties->pdata[data->properties->len - 1];
1195 static GDBusInterfaceInfo *
1196 parse_data_get_interface (ParseData *data, gboolean create_new)
1199 g_ptr_array_add (data->interfaces, g_new0 (GDBusInterfaceInfo, 1));
1200 return data->interfaces->pdata[data->interfaces->len - 1];
1203 static GDBusNodeInfo *
1204 parse_data_get_node (ParseData *data, gboolean create_new)
1207 g_ptr_array_add (data->nodes, g_new0 (GDBusNodeInfo, 1));
1208 return data->nodes->pdata[data->nodes->len - 1];
1211 /* ---------------------------------------------------------------------------------------------------- */
1214 parse_data_new (void)
1218 data = g_new0 (ParseData, 1);
1220 /* initialize arrays */
1221 parse_data_steal_annotations (data, NULL);
1222 parse_data_steal_args (data, NULL);
1223 parse_data_steal_out_args (data, NULL);
1224 parse_data_steal_methods (data, NULL);
1225 parse_data_steal_signals (data, NULL);
1226 parse_data_steal_properties (data, NULL);
1227 parse_data_steal_interfaces (data, NULL);
1228 parse_data_steal_nodes (data, NULL);
1234 parse_data_free (ParseData *data)
1238 /* free stack of annotation arrays */
1239 for (l = data->annotations_stack; l != NULL; l = l->next)
1241 GPtrArray *annotations = l->data;
1242 g_ptr_array_foreach (annotations, (GFunc) g_dbus_annotation_info_unref, NULL);
1243 g_ptr_array_free (annotations, TRUE);
1245 g_slist_free (data->annotations_stack);
1247 /* free stack of interface arrays */
1248 for (l = data->interfaces_stack; l != NULL; l = l->next)
1250 GPtrArray *interfaces = l->data;
1251 g_ptr_array_foreach (interfaces, (GFunc) g_dbus_interface_info_unref, NULL);
1252 g_ptr_array_free (interfaces, TRUE);
1254 g_slist_free (data->interfaces_stack);
1256 /* free stack of node arrays */
1257 for (l = data->nodes_stack; l != NULL; l = l->next)
1259 GPtrArray *nodes = l->data;
1260 g_ptr_array_foreach (nodes, (GFunc) g_dbus_node_info_unref, NULL);
1261 g_ptr_array_free (nodes, TRUE);
1263 g_slist_free (data->nodes_stack);
1265 /* free arrays (data->annotations, data->interfaces and data->nodes have been freed above) */
1266 parse_data_free_args (data);
1267 parse_data_free_out_args (data);
1268 parse_data_free_methods (data);
1269 parse_data_free_signals (data);
1270 parse_data_free_properties (data);
1275 /* ---------------------------------------------------------------------------------------------------- */
1278 parser_start_element (GMarkupParseContext *context,
1279 const gchar *element_name,
1280 const gchar **attribute_names,
1281 const gchar **attribute_values,
1285 ParseData *data = user_data;
1289 const gchar *access;
1290 const gchar *direction;
1299 stack = (GSList *) g_markup_parse_context_get_element_stack (context);
1301 /* ---------------------------------------------------------------------------------------------------- */
1302 if (strcmp (element_name, "node") == 0)
1304 if (!(g_slist_length (stack) >= 1 || strcmp (stack->next->data, "node") != 0))
1306 g_set_error_literal (error,
1308 G_MARKUP_ERROR_INVALID_CONTENT,
1309 "<node> elements can only be top-level or embedded in other <node> elements");
1313 if (!g_markup_collect_attributes (element_name,
1317 G_MARKUP_COLLECT_STRING | G_MARKUP_COLLECT_OPTIONAL, "name", &name,
1318 /* some hand-written introspection XML documents use this */
1319 G_MARKUP_COLLECT_STRING | G_MARKUP_COLLECT_OPTIONAL, "xmlns:doc", NULL,
1320 G_MARKUP_COLLECT_INVALID))
1323 g_dbus_node_info_set (data,
1324 parse_data_get_node (data, TRUE),
1330 /* push the currently retrieved interfaces and nodes on the stack and prepare new arrays */
1331 data->interfaces_stack = g_slist_prepend (data->interfaces_stack, data->interfaces);
1332 data->interfaces = NULL;
1333 parse_data_steal_interfaces (data, NULL);
1335 data->nodes_stack = g_slist_prepend (data->nodes_stack, data->nodes);
1337 parse_data_steal_nodes (data, NULL);
1340 /* ---------------------------------------------------------------------------------------------------- */
1341 else if (strcmp (element_name, "interface") == 0)
1343 if (g_slist_length (stack) < 2 || strcmp (stack->next->data, "node") != 0)
1345 g_set_error_literal (error,
1347 G_MARKUP_ERROR_INVALID_CONTENT,
1348 "<interface> elements can only be embedded in <node> elements");
1352 if (!g_markup_collect_attributes (element_name,
1356 G_MARKUP_COLLECT_STRING, "name", &name,
1357 G_MARKUP_COLLECT_INVALID))
1360 g_dbus_interface_info_set (data,
1361 parse_data_get_interface (data, TRUE),
1369 /* ---------------------------------------------------------------------------------------------------- */
1370 else if (strcmp (element_name, "method") == 0)
1372 if (g_slist_length (stack) < 2 || strcmp (stack->next->data, "interface") != 0)
1374 g_set_error_literal (error,
1376 G_MARKUP_ERROR_INVALID_CONTENT,
1377 "<method> elements can only be embedded in <interface> elements");
1381 if (!g_markup_collect_attributes (element_name,
1385 G_MARKUP_COLLECT_STRING, "name", &name,
1386 G_MARKUP_COLLECT_INVALID))
1389 g_dbus_method_info_set (data,
1390 parse_data_get_method (data, TRUE),
1399 /* ---------------------------------------------------------------------------------------------------- */
1400 else if (strcmp (element_name, "signal") == 0)
1402 if (g_slist_length (stack) < 2 || strcmp (stack->next->data, "interface") != 0)
1404 g_set_error_literal (error,
1406 G_MARKUP_ERROR_INVALID_CONTENT,
1407 "<signal> elements can only be embedded in <interface> elements");
1411 if (!g_markup_collect_attributes (element_name,
1415 G_MARKUP_COLLECT_STRING, "name", &name,
1416 G_MARKUP_COLLECT_INVALID))
1419 g_dbus_signal_info_set (data,
1420 parse_data_get_signal (data, TRUE),
1428 /* ---------------------------------------------------------------------------------------------------- */
1429 else if (strcmp (element_name, "property") == 0)
1431 GDBusPropertyInfoFlags flags;
1433 if (g_slist_length (stack) < 2 || strcmp (stack->next->data, "interface") != 0)
1435 g_set_error_literal (error,
1437 G_MARKUP_ERROR_INVALID_CONTENT,
1438 "<property> elements can only be embedded in <interface> elements");
1442 if (!g_markup_collect_attributes (element_name,
1446 G_MARKUP_COLLECT_STRING, "name", &name,
1447 G_MARKUP_COLLECT_STRING, "type", &type,
1448 G_MARKUP_COLLECT_STRING, "access", &access,
1449 G_MARKUP_COLLECT_INVALID))
1452 if (strcmp (access, "read") == 0)
1453 flags = G_DBUS_PROPERTY_INFO_FLAGS_READABLE;
1454 else if (strcmp (access, "write") == 0)
1455 flags = G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE;
1456 else if (strcmp (access, "readwrite") == 0)
1457 flags = G_DBUS_PROPERTY_INFO_FLAGS_READABLE | G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE;
1462 G_MARKUP_ERROR_INVALID_CONTENT,
1463 "Unknown value '%s' of access attribute for element <property>",
1468 g_dbus_property_info_set (data,
1469 parse_data_get_property (data, TRUE),
1476 /* ---------------------------------------------------------------------------------------------------- */
1477 else if (strcmp (element_name, "arg") == 0)
1482 if (g_slist_length (stack) < 2 ||
1483 (strcmp (stack->next->data, "method") != 0 &&
1484 strcmp (stack->next->data, "signal") != 0))
1486 g_set_error_literal (error,
1488 G_MARKUP_ERROR_INVALID_CONTENT,
1489 "<arg> elements can only be embedded in <method> or <signal> elements");
1493 if (!g_markup_collect_attributes (element_name,
1497 G_MARKUP_COLLECT_STRING | G_MARKUP_COLLECT_OPTIONAL, "name", &name,
1498 G_MARKUP_COLLECT_STRING | G_MARKUP_COLLECT_OPTIONAL, "direction", &direction,
1499 G_MARKUP_COLLECT_STRING, "type", &type,
1500 G_MARKUP_COLLECT_INVALID))
1504 if (direction != NULL)
1506 if (strcmp (direction, "in") == 0)
1508 else if (strcmp (direction, "out") == 0)
1514 G_MARKUP_ERROR_INVALID_CONTENT,
1515 "Unknown value '%s' of direction attribute",
1521 if (is_in && strcmp (stack->next->data, "signal") == 0)
1523 g_set_error_literal (error,
1525 G_MARKUP_ERROR_INVALID_CONTENT,
1526 "Only direction 'out' is allowed for <arg> elements embedded in <signal>");
1531 name_to_use = g_strdup_printf ("arg_%d", data->num_args);
1533 name_to_use = g_strdup (name);
1538 g_dbus_arg_info_set (data,
1539 parse_data_get_arg (data, TRUE),
1543 data->last_arg_was_in = TRUE;
1547 g_dbus_arg_info_set (data,
1548 parse_data_get_out_arg (data, TRUE),
1552 data->last_arg_was_in = FALSE;
1556 g_free (name_to_use);
1558 /* ---------------------------------------------------------------------------------------------------- */
1559 else if (strcmp (element_name, "annotation") == 0)
1561 if (g_slist_length (stack) < 2 ||
1562 (strcmp (stack->next->data, "node") != 0 &&
1563 strcmp (stack->next->data, "interface") != 0 &&
1564 strcmp (stack->next->data, "signal") != 0 &&
1565 strcmp (stack->next->data, "method") != 0 &&
1566 strcmp (stack->next->data, "property") != 0 &&
1567 strcmp (stack->next->data, "arg") != 0 &&
1568 strcmp (stack->next->data, "annotation") != 0))
1570 g_set_error_literal (error,
1572 G_MARKUP_ERROR_INVALID_CONTENT,
1573 "<annotation> elements can only be embedded in <node>, <interface>, <signal>, <method>, <property>, <arg> or <annotation> elements");
1577 if (!g_markup_collect_attributes (element_name,
1581 G_MARKUP_COLLECT_STRING, "name", &name,
1582 G_MARKUP_COLLECT_STRING, "value", &value,
1583 G_MARKUP_COLLECT_INVALID))
1586 g_dbus_annotation_info_set (data,
1587 parse_data_get_annotation (data, TRUE),
1592 /* ---------------------------------------------------------------------------------------------------- */
1595 /* don't bail on unknown elements; just ignore them */
1597 /* ---------------------------------------------------------------------------------------------------- */
1599 /* push the currently retrieved annotations on the stack and prepare a new one */
1600 data->annotations_stack = g_slist_prepend (data->annotations_stack, data->annotations);
1601 data->annotations = NULL;
1602 parse_data_steal_annotations (data, NULL);
1608 /* ---------------------------------------------------------------------------------------------------- */
1610 static GDBusAnnotationInfo **
1611 steal_annotations (ParseData *data)
1613 return parse_data_steal_annotations (data, NULL);
1618 parser_end_element (GMarkupParseContext *context,
1619 const gchar *element_name,
1623 ParseData *data = user_data;
1624 gboolean have_popped_annotations;
1626 have_popped_annotations = FALSE;
1628 if (strcmp (element_name, "node") == 0)
1631 guint num_interfaces;
1632 GDBusNodeInfo **nodes;
1633 GDBusInterfaceInfo **interfaces;
1635 nodes = parse_data_steal_nodes (data, &num_nodes);
1636 interfaces = parse_data_steal_interfaces (data, &num_interfaces);
1638 /* destroy the nodes, interfaces for scope we're exiting and and pop the nodes, interfaces from the
1639 * scope we're reentering
1641 parse_data_free_interfaces (data);
1642 data->interfaces = (GPtrArray *) data->interfaces_stack->data;
1643 data->interfaces_stack = g_slist_remove (data->interfaces_stack, data->interfaces_stack->data);
1645 parse_data_free_nodes (data);
1646 data->nodes = (GPtrArray *) data->nodes_stack->data;
1647 data->nodes_stack = g_slist_remove (data->nodes_stack, data->nodes_stack->data);
1649 g_dbus_node_info_set (data,
1650 parse_data_get_node (data, FALSE),
1656 steal_annotations (data));
1659 else if (strcmp (element_name, "interface") == 0)
1663 guint num_properties;
1664 GDBusMethodInfo **methods;
1665 GDBusSignalInfo **signals;
1666 GDBusPropertyInfo **properties;
1668 methods = parse_data_steal_methods (data, &num_methods);
1669 signals = parse_data_steal_signals (data, &num_signals);
1670 properties = parse_data_steal_properties (data, &num_properties);
1672 g_dbus_interface_info_set (data,
1673 parse_data_get_interface (data, FALSE),
1681 steal_annotations (data));
1684 else if (strcmp (element_name, "method") == 0)
1688 GDBusArgInfo **in_args;
1689 GDBusArgInfo **out_args;
1691 in_args = parse_data_steal_args (data, &in_num_args);
1692 out_args = parse_data_steal_out_args (data, &out_num_args);
1694 g_dbus_method_info_set (data,
1695 parse_data_get_method (data, FALSE),
1701 steal_annotations (data));
1703 else if (strcmp (element_name, "signal") == 0)
1706 GDBusArgInfo **args;
1708 args = parse_data_steal_out_args (data, &num_args);
1710 g_dbus_signal_info_set (data,
1711 parse_data_get_signal (data, FALSE),
1715 steal_annotations (data));
1717 else if (strcmp (element_name, "property") == 0)
1719 g_dbus_property_info_set (data,
1720 parse_data_get_property (data, FALSE),
1723 G_DBUS_PROPERTY_INFO_FLAGS_NONE,
1724 steal_annotations (data));
1726 else if (strcmp (element_name, "arg") == 0)
1728 g_dbus_arg_info_set (data,
1729 data->last_arg_was_in ? parse_data_get_arg (data, FALSE) : parse_data_get_out_arg (data, FALSE),
1732 steal_annotations (data));
1734 else if (strcmp (element_name, "annotation") == 0)
1736 GDBusAnnotationInfo **embedded_annotations;
1738 embedded_annotations = steal_annotations (data);
1740 /* destroy the annotations for scope we're exiting and and pop the annotations from the scope we're reentering */
1741 parse_data_free_annotations (data);
1742 data->annotations = (GPtrArray *) data->annotations_stack->data;
1743 data->annotations_stack = g_slist_remove (data->annotations_stack, data->annotations_stack->data);
1745 have_popped_annotations = TRUE;
1747 g_dbus_annotation_info_set (data,
1748 parse_data_get_annotation (data, FALSE),
1751 embedded_annotations);
1755 /* don't bail on unknown elements; just ignore them */
1758 if (!have_popped_annotations)
1760 /* destroy the annotations for scope we're exiting and and pop the annotations from the scope we're reentering */
1761 parse_data_free_annotations (data);
1762 data->annotations = (GPtrArray *) data->annotations_stack->data;
1763 data->annotations_stack = g_slist_remove (data->annotations_stack, data->annotations_stack->data);
1767 /* ---------------------------------------------------------------------------------------------------- */
1770 parser_error (GMarkupParseContext *context,
1777 g_markup_parse_context_get_position (context, &line_number, &char_number);
1779 g_prefix_error (&error, "%d:%d: ",
1784 /* ---------------------------------------------------------------------------------------------------- */
1787 * g_dbus_node_info_new_for_xml:
1788 * @xml_data: Valid D-Bus introspection XML.
1789 * @error: Return location for error.
1791 * Parses @xml_data and returns a #GDBusNodeInfo representing the data.
1793 * Returns: A #GDBusNodeInfo structure or %NULL if @error is set. Free
1794 * with g_dbus_node_info_unref().
1799 g_dbus_node_info_new_for_xml (const gchar *xml_data,
1803 GMarkupParseContext *context;
1804 GMarkupParser *parser;
1812 parser = g_new0 (GMarkupParser, 1);
1813 parser->start_element = parser_start_element;
1814 parser->end_element = parser_end_element;
1815 parser->error = parser_error;
1817 data = parse_data_new ();
1818 context = g_markup_parse_context_new (parser,
1821 (GDestroyNotify) parse_data_free);
1823 if (!g_markup_parse_context_parse (context,
1829 GDBusNodeInfo **ughret;
1830 ughret = parse_data_steal_nodes (data, &num_nodes);
1838 G_MARKUP_ERROR_INVALID_CONTENT,
1839 "Expected a single node in introspection XML, found %d",
1843 for (n = 0; n < num_nodes; n++)
1845 for (n = 0; n < num_nodes; n++)
1846 g_dbus_node_info_unref (&(ret[n]));
1858 if (context != NULL)
1859 g_markup_parse_context_free (context);
1864 /* ---------------------------------------------------------------------------------------------------- */
1867 * g_dbus_annotation_info_lookup:
1868 * @annotations: A %NULL-terminated array of annotations or %NULL.
1869 * @name: The name of the annotation to look up.
1871 * Looks up the value of an annotation.
1873 * This cost of this function is O(n) in number of annotations.
1875 * Returns: The value or %NULL if not found. Do not free, it is owned by @annotations.
1880 g_dbus_annotation_info_lookup (const GDBusAnnotationInfo **annotations,
1887 for (n = 0; annotations != NULL && annotations[n]->key != NULL; n++)
1889 if (g_strcmp0 (annotations[n]->key, name) == 0)
1891 ret = annotations[n]->value;
1900 /* ---------------------------------------------------------------------------------------------------- */
1903 * g_dbus_interface_info_lookup_method:
1904 * @info: A #GDBusInterfaceInfo.
1905 * @name: A D-Bus method name (typically in CamelCase)
1907 * Looks up information about a method.
1909 * This cost of this function is O(n) in number of methods.
1911 * Returns: A #GDBusMethodInfo or %NULL if not found. Do not free, it is owned by @info.
1915 const GDBusMethodInfo *
1916 g_dbus_interface_info_lookup_method (const GDBusInterfaceInfo *info,
1920 const GDBusMethodInfo *result;
1922 for (n = 0; info->methods != NULL && info->methods[n] != NULL; n++)
1924 const GDBusMethodInfo *i = info->methods[n];
1926 if (g_strcmp0 (i->name, name) == 0)
1939 /* ---------------------------------------------------------------------------------------------------- */
1942 * g_dbus_interface_info_lookup_signal:
1943 * @info: A #GDBusInterfaceInfo.
1944 * @name: A D-Bus signal name (typically in CamelCase)
1946 * Looks up information about a signal.
1948 * This cost of this function is O(n) in number of signals.
1950 * Returns: A #GDBusSignalInfo or %NULL if not found. Do not free, it is owned by @info.
1954 const GDBusSignalInfo *
1955 g_dbus_interface_info_lookup_signal (const GDBusInterfaceInfo *info,
1959 const GDBusSignalInfo *result;
1961 for (n = 0; info->signals != NULL && info->signals[n] != NULL; n++)
1963 const GDBusSignalInfo *i = info->signals[n];
1965 if (g_strcmp0 (i->name, name) == 0)
1978 /* ---------------------------------------------------------------------------------------------------- */
1981 * g_dbus_interface_info_lookup_property:
1982 * @info: A #GDBusInterfaceInfo.
1983 * @name: A D-Bus property name (typically in CamelCase).
1985 * Looks up information about a property.
1987 * This cost of this function is O(n) in number of properties.
1989 * Returns: A #GDBusPropertyInfo or %NULL if not found. Do not free, it is owned by @info.
1993 const GDBusPropertyInfo *
1994 g_dbus_interface_info_lookup_property (const GDBusInterfaceInfo *info,
1998 const GDBusPropertyInfo *result;
2000 for (n = 0; info->properties != NULL && info->properties[n] != NULL; n++)
2002 const GDBusPropertyInfo *i = info->properties[n];
2004 if (g_strcmp0 (i->name, name) == 0)
2017 /* ---------------------------------------------------------------------------------------------------- */
2020 * g_dbus_node_info_lookup_interface:
2021 * @node_info: A #GDBusNodeInfo.
2022 * @name: A D-Bus interface name.
2024 * Looks up information about an interface.
2026 * This cost of this function is O(n) in number of interfaces.
2028 * Returns: A #GDBusInterfaceInfo or %NULL if not found. Do not free, it is owned by @node_info.
2032 const GDBusInterfaceInfo *
2033 g_dbus_node_info_lookup_interface (const GDBusNodeInfo *node_info,
2037 const GDBusInterfaceInfo *result;
2039 for (n = 0; node_info->interfaces != NULL && node_info->interfaces[n] != NULL; n++)
2041 const GDBusInterfaceInfo *i = node_info->interfaces[n];
2043 if (g_strcmp0 (i->name, name) == 0)