[PATCH 03/17] config: use add_option for source and path too.

Peter Hutterer peter.hutterer at who-t.net
Sun Aug 7 23:20:59 PDT 2011


Change add_option to return the new InputOption on success, or NULL on
failure. This way we can at least check for errors in callers.

Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 config/config-backends.h |    2 +-
 config/config.c          |   13 ++++++++++---
 config/dbus.c            |   10 +---------
 config/hal.c             |   10 +---------
 config/udev.c            |    8 +-------
 5 files changed, 14 insertions(+), 29 deletions(-)

diff --git a/config/config-backends.h b/config/config-backends.h
index 0a2a22a..0d36d72 100644
--- a/config/config-backends.h
+++ b/config/config-backends.h
@@ -30,7 +30,7 @@
 
 void remove_devices(const char *backend, const char *config_info);
 BOOL device_is_duplicate(const char *config_info);
-void add_option(InputOption **options, const char *key, const char *value);
+InputOption* add_option(InputOption **options, const char *key, const char *value);
 
 #ifdef CONFIG_UDEV
 int config_udev_init(void);
diff --git a/config/config.c b/config/config.c
index d86f7c6..af8f4f9 100644
--- a/config/config.c
+++ b/config/config.c
@@ -122,18 +122,25 @@ device_is_duplicate(const char *config_info)
     return FALSE;
 }
 
-void
+/**
+ * Allocate a new option and append to the list.
+ *
+ * @return A pointer to the newly allocated InputOption struct.
+ */
+InputOption*
 add_option(InputOption **options, const char *key, const char *value)
 {
     if (!value || *value == '\0')
-        return;
+        return NULL;
 
     for (; *options; options = &(*options)->next)
         ;
     *options = calloc(sizeof(**options), 1);
     if (!*options) /* Yeesh. */
-        return;
+        return NULL;
     (*options)->key = strdup(key);
     (*options)->value = strdup(value);
     (*options)->next = NULL;
+
+    return *options;
 }
diff --git a/config/dbus.c b/config/dbus.c
index 34e3caa..ccef676 100644
--- a/config/dbus.c
+++ b/config/dbus.c
@@ -80,15 +80,7 @@ add_device(DBusMessage *message, DBusMessage *reply, DBusError *error)
         MALFORMED_MESSAGE();
     }
 
-    options = calloc(sizeof(*options), 1);
-    if (!options) {
-        ErrorF("[config/dbus] couldn't allocate option\n");
-        return BadAlloc;
-    }
-
-    options->key = strdup("_source");
-    options->value = strdup("client/dbus");
-    if (!options->key || !options->value) {
+    if (!add_option(&options, "_source", "client/dbus"))
         ErrorF("[config/dbus] couldn't allocate first key/value pair\n");
         ret = BadAlloc;
         goto unwind;
diff --git a/config/hal.c b/config/hal.c
index 297520a..6ab42a2 100644
--- a/config/hal.c
+++ b/config/hal.c
@@ -205,15 +205,7 @@ device_added(LibHalContext *hal_ctx, const char *udi)
         free(parent);
     }
 
-    options = calloc(sizeof(*options), 1);
-    if (!options){
-        LogMessage(X_ERROR, "config/hal: couldn't allocate space for input options!\n");
-        goto unwind;
-    }
-
-    options->key = strdup("_source");
-    options->value = strdup("server/hal");
-    if (!options->key || !options->value) {
+    if (!add_option(&options, "_source", "server/hal"))
         LogMessage(X_ERROR, "config/hal: couldn't allocate first key/value pair\n");
         goto unwind;
     }
diff --git a/config/udev.c b/config/udev.c
index b11c81d..1f431c1 100644
--- a/config/udev.c
+++ b/config/udev.c
@@ -93,13 +93,7 @@ device_added(struct udev_device *udev_device)
         return;
     }
 
-    options = calloc(sizeof(*options), 1);
-    if (!options)
-        return;
-
-    options->key = strdup("_source");
-    options->value = strdup("server/udev");
-    if (!options->key || !options->value)
+    if (!add_option(&options, "_source", "server/udev"))
         goto unwind;
 
     parent = udev_device_get_parent(udev_device);
-- 
1.7.6



More information about the xorg-devel mailing list