hal: Branch 'master'

David Zeuthen david at kemper.freedesktop.org
Mon Oct 9 17:23:12 PDT 2006


 hald/device.c      |  158 ++++++++++++++++++++++++++++++++++++++++-------------
 hald/device.h      |   20 ++++++
 hald/device_info.c |   22 +++----
 hald/hald.c        |   33 +++++------
 hald/hald_dbus.c   |  135 ++++++++++++++++++++++-----------------------
 hald/util.c        |   20 ++++--
 6 files changed, 247 insertions(+), 141 deletions(-)

New commits:
diff-tree 5742b5dbee411fb2df32053791dfc6ec0383bc94 (from 1f777cb8d4a4300c7762eeb6150789ba0dec9e44)
Author: David Zeuthen <davidz at redhat.com>
Date:   Mon Oct 9 20:23:07 2006 -0400

    use an iterator to iterate over strlist instead of using GSList

diff --git a/hald/device.c b/hald/device.c
index b10dc84..97005e1 100644
--- a/hald/device.c
+++ b/hald/device.c
@@ -262,6 +262,128 @@ hal_device_merge_with_rewrite  (HalDevic
 
 }
 
+static HalProperty *
+hal_device_property_find (HalDevice *device, const char *key)
+{
+	GSList *iter;
+
+	g_return_val_if_fail (device != NULL, NULL);
+	g_return_val_if_fail (key != NULL, NULL);
+
+	for (iter = device->private->properties; iter != NULL; iter = iter->next) {
+		HalProperty *p = iter->data;
+
+		if (strcmp (hal_property_get_key (p), key) == 0) {
+			return p;
+		}
+	}
+
+	return NULL;
+}
+
+static GSList *
+hal_device_property_get_strlist (HalDevice    *device, 
+				 const char   *key)
+{
+	HalProperty *prop;
+
+	g_return_val_if_fail (device != NULL, NULL);
+	g_return_val_if_fail (key != NULL, NULL);
+
+	prop = hal_device_property_find (device, key);
+
+	if (prop != NULL)
+		return hal_property_get_strlist (prop);
+	else
+		return NULL;
+}
+
+guint
+hal_device_property_get_strlist_length (HalDevice    *device,
+					const char   *key)
+{
+	GSList *i;
+
+	i = hal_device_property_get_strlist (device, key);
+	if (i != NULL)
+		return g_slist_length (i);
+	else
+		return 0;
+}
+
+void
+hal_device_property_strlist_iter_init (HalDevice    *device,
+				       const char   *key,
+				       HalDeviceStrListIter *iter)
+{
+	HalProperty *prop;
+
+	g_return_if_fail (device != NULL);
+	g_return_if_fail (key != NULL);
+
+	prop = hal_device_property_find (device, key);
+
+	if (prop != NULL)
+		iter->i = hal_property_get_strlist (prop);
+	else
+		iter->i = NULL;
+}
+
+const char *
+hal_device_property_strlist_iter_get_value (HalDeviceStrListIter *iter)
+{
+	g_return_val_if_fail (iter != NULL, NULL);
+	g_return_val_if_fail (iter->i != NULL, NULL);
+	return iter->i->data;
+}
+
+void
+hal_device_property_strlist_iter_next (HalDeviceStrListIter *iter)
+{
+	g_return_if_fail (iter != NULL);
+	g_return_if_fail (iter->i != NULL);
+	iter->i = g_slist_next (iter->i);
+}
+
+gboolean
+hal_device_property_strlist_iter_is_valid (HalDeviceStrListIter *iter)
+{
+	g_return_val_if_fail (iter != NULL, FALSE);
+	g_return_val_if_fail (iter->i != NULL, FALSE);
+	return TRUE;
+}
+
+char **
+hal_device_property_dup_strlist_as_strv (HalDevice    *device,
+					 const char   *key)
+{
+	guint j;
+	guint len;
+	gchar **strv;
+	GSList *i;
+
+	strv = NULL;
+
+	i = hal_device_property_get_strlist (device, key);
+	if (i == NULL)
+		goto out;
+
+	len = g_slist_length (i);
+	if (len == 0)
+		goto out;
+
+	strv = g_new (char *, len + 1);
+
+	for (j = 0; i != NULL; i = g_slist_next (i), j++) {
+		strv[j] = g_strdup ((const gchar *) i->data);
+	}
+	strv[j] = NULL;
+
+out:
+	return strv;	
+}
+
+
 void
 hal_device_merge (HalDevice *target, HalDevice *source)
 {
@@ -452,25 +574,6 @@ hal_device_num_properties (HalDevice *de
 	return g_slist_length (device->private->properties);
 }
 
-static HalProperty *
-hal_device_property_find (HalDevice *device, const char *key)
-{
-	GSList *iter;
-
-	g_return_val_if_fail (device != NULL, NULL);
-	g_return_val_if_fail (key != NULL, NULL);
-
-	for (iter = device->private->properties; iter != NULL; iter = iter->next) {
-		HalProperty *p = iter->data;
-
-		if (strcmp (hal_property_get_key (p), key) == 0) {
-			return p;
-		}
-	}
-
-	return NULL;
-}
-
 gboolean
 hal_device_has_property (HalDevice *device, const char *key)
 {
@@ -952,23 +1055,6 @@ hal_device_print (HalDevice *device)
         fprintf (stderr, "\n");
 }
 
-GSList *
-hal_device_property_get_strlist (HalDevice    *device, 
-				 const char   *key)
-{
-	HalProperty *prop;
-
-	g_return_val_if_fail (device != NULL, NULL);
-	g_return_val_if_fail (key != NULL, NULL);
-
-	prop = hal_device_property_find (device, key);
-
-	if (prop != NULL)
-		return hal_property_get_strlist (prop);
-	else
-		return NULL;
-}
-
 const char *
 hal_device_property_get_strlist_elem (HalDevice    *device,
 				      const char   *key,
diff --git a/hald/device.h b/hald/device.h
index b30109c..4ba52f3 100644
--- a/hald/device.h
+++ b/hald/device.h
@@ -74,6 +74,13 @@ struct _HalDeviceClass {
 #define HAL_PROPERTY_TYPE_STRLIST     ((int) (DBUS_TYPE_STRING<<8)+('l'))
 
 
+/* private; do not access; might change in the future */
+typedef struct _HalDeviceStrListIter      HalDeviceStrListIter;
+struct _HalDeviceStrListIter {
+	GSList *i;
+};
+
+
 /* Return value of FALSE means that the foreach should be short-circuited */
 typedef gboolean (*HalDevicePropertyForeachFn) (HalDevice *device,
 						const char *key,
@@ -132,11 +139,20 @@ dbus_bool_t   hal_device_property_get_bo
 					      const char   *key);
 double        hal_device_property_get_double (HalDevice    *device,
 					      const char   *key);
-GSList       *hal_device_property_get_strlist (HalDevice    *device,
-					       const char   *key);
 const char   *hal_device_property_get_strlist_elem (HalDevice    *device,
 						    const char   *key,
 						    guint index);
+guint         hal_device_property_get_strlist_length (HalDevice    *device,
+						      const char   *key);
+char        **hal_device_property_dup_strlist_as_strv (HalDevice    *device,
+						       const char   *key);
+
+void          hal_device_property_strlist_iter_init (HalDevice    *device,
+						     const char   *key,
+						     HalDeviceStrListIter *iter);
+const char   *hal_device_property_strlist_iter_get_value (HalDeviceStrListIter *iter);
+void          hal_device_property_strlist_iter_next (HalDeviceStrListIter *iter);
+gboolean      hal_device_property_strlist_iter_is_valid (HalDeviceStrListIter *iter);
 
 
 
diff --git a/hald/device_info.c b/hald/device_info.c
index 6f2a153..1ccaa45 100644
--- a/hald/device_info.c
+++ b/hald/device_info.c
@@ -579,12 +579,12 @@ handle_match (ParsingContext * pc, const
 			}
 		} else if (hal_device_property_get_type (d, prop_to_check) == HAL_PROPERTY_TYPE_STRLIST && 
 			   needle != NULL) {
-			GSList *i;
-			GSList *value;
+			guint i;
+			guint num_elems;
 
-			value = hal_device_property_get_strlist (d, prop_to_check);
-			for (i = value; i != NULL; i = g_slist_next (i)) {
-				const char *str = i->data;
+			num_elems = hal_device_property_get_strlist_length (d, prop_to_check);
+			for (i = 0; i < num_elems; i++) {
+				const char *str = hal_device_property_get_strlist_elem (d, prop_to_check, i);
 				if (strcmp (str, needle) == 0) {
 					contains = TRUE;
 					break;
@@ -617,12 +617,12 @@ handle_match (ParsingContext * pc, const
 			}
 		} else if (hal_device_property_get_type (d, prop_to_check) == HAL_PROPERTY_TYPE_STRLIST && 
 			   needle != NULL) {
-			GSList *i;
-			GSList *value;
-
-			value = hal_device_property_get_strlist (d, prop_to_check);
-			for (i = value; i != NULL; i = g_slist_next (i)) {
-				const char *str = i->data;
+			guint i;
+			guint num_elems;
+			
+			num_elems = hal_device_property_get_strlist_length (d, prop_to_check);
+			for (i = 0; i < num_elems; i++) {
+				const char *str = hal_device_property_get_strlist_elem (d, prop_to_check, i);
 				if (g_ascii_strcasecmp (str, needle) == 0) {
 					contains_ncase = TRUE;
 					break;
diff --git a/hald/hald.c b/hald/hald.c
index 48b35f4..71e73bd 100644
--- a/hald/hald.c
+++ b/hald/hald.c
@@ -101,26 +101,25 @@ gdl_store_changed (HalDeviceStore *store
 		   gboolean is_added, gpointer user_data)
 {
 	if (is_added) {
-		GSList *addons;
+		HalDeviceStrListIter iter;
 
 		HAL_INFO (("Added device to GDL; udi=%s", hal_device_get_udi(device)));
 
-		if ((addons = hal_device_property_get_strlist (device, "info.addons")) != NULL) {
-			GSList *i;
-
-			for (i = addons; i != NULL; i = g_slist_next (i)) {
-				const gchar *command_line;
-				gchar *extra_env[2] = {"HALD_ACTION=addon", NULL};
-
-				command_line = (const gchar *) i->data;
-				if (hald_runner_start(device, command_line, extra_env, addon_terminated, NULL, NULL)) {
-					HAL_INFO (("Started addon %s for udi %s", 
-						   command_line, hal_device_get_udi(device)));
-					hal_device_inc_num_addons (device);
-				} else {
-					HAL_ERROR (("Cannot start addon %s for udi %s", 
-						    command_line, hal_device_get_udi(device)));
-				}
+		for (hal_device_property_strlist_iter_init (device, "info.addons", &iter);
+		     hal_device_property_strlist_iter_is_valid (&iter);
+		     hal_device_property_strlist_iter_next (&iter)) {
+			const gchar *command_line;
+			gchar *extra_env[2] = {"HALD_ACTION=addon", NULL};
+			
+			command_line = hal_device_property_strlist_iter_get_value (&iter);
+
+			if (hald_runner_start(device, command_line, extra_env, addon_terminated, NULL, NULL)) {
+				HAL_INFO (("Started addon %s for udi %s", 
+					   command_line, hal_device_get_udi(device)));
+				hal_device_inc_num_addons (device);
+			} else {
+				HAL_ERROR (("Cannot start addon %s for udi %s", 
+					    command_line, hal_device_get_udi(device)));
 			}
 		}
 	} else {
diff --git a/hald/hald_dbus.c b/hald/hald_dbus.c
index f864e63..8f30e2f 100644
--- a/hald/hald_dbus.c
+++ b/hald/hald_dbus.c
@@ -798,7 +798,7 @@ foreach_property_append (HalDevice *devi
 	case HAL_PROPERTY_TYPE_STRLIST:
 	{
 		DBusMessageIter iter_var, iter_array;
-		GSList *iter;
+		HalDeviceStrListIter iter;
 
 		dbus_message_iter_open_container (&iter_dict_entry,
 						  DBUS_TYPE_VARIANT,
@@ -811,10 +811,11 @@ foreach_property_append (HalDevice *devi
 						  DBUS_TYPE_STRING_AS_STRING,
 						  &iter_array);
 
-		for (iter = hal_device_property_get_strlist (device, key); iter != NULL; iter = iter->next) {
-				     
+		for (hal_device_property_strlist_iter_init (device, key, &iter);
+		     hal_device_property_strlist_iter_is_valid (&iter);
+		     hal_device_property_strlist_iter_next (&iter)) {
 			const char *v;
-			v = (const char *) iter->data;
+			v = hal_device_property_strlist_iter_get_value (&iter);
 
 			dbus_message_iter_append_basic (&iter_array, 
 							DBUS_TYPE_STRING,
@@ -1196,7 +1197,7 @@ device_get_property (DBusConnection * co
 	}
 	case HAL_PROPERTY_TYPE_STRLIST:
 	{
-		GSList *l;
+		HalDeviceStrListIter striter;
 		DBusMessageIter iter_array;
 
 		dbus_message_iter_open_container (&iter, 
@@ -1204,8 +1205,12 @@ device_get_property (DBusConnection * co
 						  DBUS_TYPE_STRING_AS_STRING,
 						  &iter_array);
 
-		for (l = hal_device_property_get_strlist (d, key); l != NULL; l = g_slist_next (l)) {
-			dbus_message_iter_append_basic (&iter_array, DBUS_TYPE_STRING, &(l->data));
+		for (hal_device_property_strlist_iter_init (d, key, &striter);
+		     hal_device_property_strlist_iter_is_valid (&striter);
+		     hal_device_property_strlist_iter_next (&striter)) {
+			const char *v;
+			v = hal_device_property_strlist_iter_get_value (&striter);
+			dbus_message_iter_append_basic (&iter_array, DBUS_TYPE_STRING, v);
 		}
 
 		dbus_message_iter_close_container (&iter, &iter_array);
@@ -1742,7 +1747,7 @@ device_query_capability (DBusConnection 
 {
 	dbus_bool_t rc;
 	const char *udi;
-	GSList *caps;
+	HalDeviceStrListIter striter;
 	char *capability;
 	HalDevice *d;
 	DBusMessage *reply;
@@ -1775,15 +1780,12 @@ device_query_capability (DBusConnection 
 		DIE (("No memory"));
 
 	rc = FALSE;
-	caps = hal_device_property_get_strlist (d, "info.capabilities");
-	if (caps != NULL) {
-		GSList *iter;
-
-		for (iter = caps; iter != NULL; iter=g_slist_next(iter)) {
-			if (strcmp (iter->data, capability) == 0) {
-				rc = TRUE;
-				break;
-			}
+	for (hal_device_property_strlist_iter_init (d, "info.capabilities", &striter);
+	     hal_device_property_strlist_iter_is_valid (&striter);
+	     hal_device_property_strlist_iter_next (&striter)) {
+		if (strcmp (hal_device_property_strlist_iter_get_value (&striter), capability) == 0) {
+			rc = TRUE;
+			break;
 		}
 	}
 
@@ -3464,49 +3466,45 @@ do_introspect (DBusConnection  *connecti
 				       "    </method>\n"
 
 				       "  </interface>\n");
+			HalDeviceStrListIter if_iter;
 
-			GSList *interfaces;
-			GSList *i;
+			for (hal_device_property_strlist_iter_init (d, "info.interfaces", &if_iter);
+			     hal_device_property_strlist_iter_is_valid (&if_iter);
+			     hal_device_property_strlist_iter_next (&if_iter)) {
+				const char *ifname;
+				char *method_name_prop;
+				char *method_sign_prop;
+				char *method_argn_prop;
+				GSList *i;
+				HalDeviceStrListIter name_iter;
+				HalDeviceStrListIter sign_iter;
+				HalDeviceStrListIter argn_iter;
 
-			interfaces = hal_device_property_get_strlist (d, "info.interfaces");
-			for (i = interfaces; i != NULL; i = g_slist_next (i)) {
-				const char *ifname = (const char *) i->data;
-				char *method_names_prop;
-				char *method_signatures_prop;
-				char *method_argnames_prop;
-				GSList *method_names;
-				GSList *method_signatures;
-				GSList *method_argnames;
-				GSList *j;
-				GSList *k;
-				GSList *l;
+				ifname = hal_device_property_strlist_iter_get_value (&if_iter);
 
 				g_string_append_printf (xml, "  <interface name=\"%s\">\n", ifname);
 
-				method_names_prop = g_strdup_printf ("%s.method_names", ifname);
-				method_signatures_prop = g_strdup_printf ("%s.method_signatures", ifname);
-				method_argnames_prop = g_strdup_printf ("%s.method_argnames", ifname);
-
-				method_names = hal_device_property_get_strlist (d, method_names_prop);
-				method_signatures = hal_device_property_get_strlist (d, method_signatures_prop);
-				method_argnames = hal_device_property_get_strlist (d, method_argnames_prop);
+				method_name_prop = g_strdup_printf ("%s.method_names", ifname);
+				method_sign_prop = g_strdup_printf ("%s.method_signatures", ifname);
+				method_argn_prop = g_strdup_printf ("%s.method_argnames", ifname);
 
 				/* consult local list */
-				if (method_names == NULL) {
-					GSList *i;
-
-					for (i = helper_interface_handlers; i != NULL; i = g_slist_next (i)) {
-						HelperInterfaceHandler *hih = i->data;
-						if (strcmp (hih->udi, path) == 0) {
-							xml = g_string_append (xml, hih->introspection_xml);
-						}
+				for (i = helper_interface_handlers; i != NULL; i = g_slist_next (i)) {
+					HelperInterfaceHandler *hih = i->data;
+					if (strcmp (hih->udi, path) == 0) {
+						xml = g_string_append (xml, hih->introspection_xml);
 					}
-					
 				}
-
-				for (j = method_names, k = method_signatures, l = method_argnames;
-				     j != NULL && k != NULL && l != NULL;
-				     j = g_slist_next (j), k = g_slist_next (k), l = g_slist_next (l)) {
+				
+				for (hal_device_property_strlist_iter_init (d, method_name_prop, &name_iter),
+					     hal_device_property_strlist_iter_init (d, method_sign_prop, &sign_iter),
+					     hal_device_property_strlist_iter_init (d, method_argn_prop, &argn_iter);
+				     hal_device_property_strlist_iter_is_valid (&name_iter) &&
+					     hal_device_property_strlist_iter_is_valid (&sign_iter) &&
+					     hal_device_property_strlist_iter_is_valid (&argn_iter);
+				     hal_device_property_strlist_iter_next (&name_iter),
+					     hal_device_property_strlist_iter_next (&sign_iter),
+					     hal_device_property_strlist_iter_next (&argn_iter)) {
 					const char *name;
 					const char *sig;
 					const char *argnames;
@@ -3514,9 +3512,9 @@ do_introspect (DBusConnection  *connecti
 					unsigned int n;
 					unsigned int m;
 
-					name = j->data;
-					sig = k->data;
-					argnames = l->data;
+					name     = hal_device_property_strlist_iter_get_value (&name_iter);
+					sig      = hal_device_property_strlist_iter_get_value (&sign_iter);
+					argnames = hal_device_property_strlist_iter_get_value (&argn_iter);
 
 					args = g_strsplit (argnames, " ", 0);
 
@@ -3558,9 +3556,9 @@ do_introspect (DBusConnection  *connecti
 
 				xml = g_string_append (xml, "  </interface>\n");
 
-				g_free (method_names_prop);
-				g_free (method_signatures_prop);
-				g_free (method_argnames_prop);
+				g_free (method_name_prop);
+				g_free (method_sign_prop);
+				g_free (method_argn_prop);
 			}		
 
 	}
@@ -3848,23 +3846,26 @@ hald_dbus_filter_handle_methods (DBusCon
 		} 
 
 		if (d != NULL && interface != NULL && method != NULL && signature != NULL) {
-			GSList *interfaces;
-			GSList *i;
-
-			interfaces = hal_device_property_get_strlist (d, "info.interfaces");
-			for (i = interfaces; i != NULL; i = g_slist_next (i)) {
-				const char *ifname = (const char *) i->data;
+			HalDeviceStrListIter if_iter;
+			
+			for (hal_device_property_strlist_iter_init (d, "info.interfaces", &if_iter);
+			     hal_device_property_strlist_iter_is_valid (&if_iter);
+			     hal_device_property_strlist_iter_next (&if_iter)) {
+				const char *ifname = hal_device_property_strlist_iter_get_value (&if_iter);
 
 				if (strcmp (ifname, interface) == 0) {
 					guint num;
-					GSList *method_names;
+					HalDeviceStrListIter name_iter;
 					char *s;
 
 					s = g_strdup_printf ("%s.method_names", interface);
-					method_names = hal_device_property_get_strlist (d, s);
+					hal_device_property_strlist_iter_init (d, s, &name_iter);
 					g_free (s);
-					for (i = method_names, num = 0; i != NULL; i = g_slist_next (i), num++) {
-						const char *methodname = (const char *) i->data;
+					for (num = 0; 
+					     hal_device_property_strlist_iter_is_valid (&name_iter);
+					     hal_device_property_strlist_iter_next (&name_iter), num++) {
+						const char *methodname;
+						methodname = hal_device_property_strlist_iter_get_value (&name_iter);
 						if (strcmp (methodname, method) == 0) {
 							const char *execpath;
 							const char *sig;
diff --git a/hald/util.c b/hald/util.c
index 8cac0f1..405cc23 100644
--- a/hald/util.c
+++ b/hald/util.c
@@ -841,7 +841,7 @@ callout_do_next (Callout *c)
 
 static void
 hal_callout_device (HalDevice *d, HalCalloutsDone callback, gpointer userdata1, gpointer userdata2, 
-		    GSList *programs, gchar **extra_env)
+		    char **programs, gchar **extra_env)
 {
 	Callout *c;
 
@@ -850,7 +850,7 @@ hal_callout_device (HalDevice *d, HalCal
 	c->callback = callback;
 	c->userdata1 = userdata1;
 	c->userdata2 = userdata2;
-	c->programs = hal_util_dup_strv_from_g_slist (programs);
+	c->programs = programs;
 	c->extra_env = g_strdupv (extra_env);
 	c->next_program = 0;
 
@@ -860,14 +860,16 @@ hal_callout_device (HalDevice *d, HalCal
 void
 hal_util_callout_device_add (HalDevice *d, HalCalloutsDone callback, gpointer userdata1, gpointer userdata2)
 {
-	GSList *programs;
+	char **programs;
 	gchar *extra_env[2] = {"HALD_ACTION=add", NULL};
 
-	if ((programs = hal_device_property_get_strlist (d, "info.callouts.add")) == NULL) {
+	programs = hal_device_property_dup_strlist_as_strv (d, "info.callouts.add");
+	if (programs == NULL) {
 		callback (d, userdata1, userdata2);
 		goto out;
 	}	
 
+
 	HAL_INFO (("Add callouts for udi=%s", hal_device_get_udi (d)));
 
 	hal_callout_device (d, callback, userdata1, userdata2, programs, extra_env);
@@ -878,10 +880,11 @@ out:
 void
 hal_util_callout_device_remove (HalDevice *d, HalCalloutsDone callback, gpointer userdata1, gpointer userdata2)
 {
-	GSList *programs;
+	char **programs;
 	gchar *extra_env[2] = {"HALD_ACTION=remove", NULL};
 
-	if ((programs = hal_device_property_get_strlist (d, "info.callouts.remove")) == NULL) {
+	programs = hal_device_property_dup_strlist_as_strv (d, "info.callouts.remove");
+	if (programs == NULL) {
 		callback (d, userdata1, userdata2);
 		goto out;
 	}	
@@ -896,10 +899,11 @@ out:
 void
 hal_util_callout_device_preprobe (HalDevice *d, HalCalloutsDone callback, gpointer userdata1, gpointer userdata2)
 {
-	GSList *programs;
+	char **programs;
 	gchar *extra_env[2] = {"HALD_ACTION=preprobe", NULL};
 
-	if ((programs = hal_device_property_get_strlist (d, "info.callouts.preprobe")) == NULL) {
+	programs = hal_device_property_dup_strlist_as_strv (d, "info.callouts.preprobe");
+	if (programs == NULL) {
 		callback (d, userdata1, userdata2);
 		goto out;
 	}	


More information about the hal-commit mailing list