]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
lib: factor out some code in `_lldpctl_atom_set_str_config()` in a macro
authorVincent Bernat <vincent@bernat.im>
Tue, 4 Nov 2014 14:08:59 +0000 (15:08 +0100)
committerVincent Bernat <vincent@bernat.im>
Tue, 4 Nov 2014 14:09:17 +0000 (15:09 +0100)
src/lib/atom-private.c

index de6e7ee20a0a7e43b44fc258c74b0edf98230c0e..0447bab023227de523d725eff14356bbfb6add8b 100644 (file)
@@ -400,58 +400,38 @@ _lldpctl_atom_set_str_config(lldpctl_atom_t *atom, lldpctl_key_t key,
            (struct _lldpctl_atom_config_t *)atom;
        struct lldpd_config config;
        memcpy(&config, c->config, sizeof(struct lldpd_config));
-       char *iface_pattern = NULL, *mgmt_pattern = NULL;
-       char *description = NULL;
        char *canary = NULL;
        int rc, len;
 
        len = strlen(value) + 1;
 
+#define SET_STR(mbr)                                                   \
+       do {                                                            \
+           char *aval = NULL;                                          \
+           aval = _lldpctl_alloc_in_atom(atom, len);                   \
+           if (!aval)                                                  \
+                   return NULL;                                                \
+           memcpy(aval, value, len);                                   \
+           config.mbr = aval;                                          \
+           free(c->config->mbr);                                               \
+           c->config->mbr = strdup(aval);                              \
+       } while(0)
+
        switch (key) {
        case lldpctl_k_config_iface_pattern:
-               iface_pattern = _lldpctl_alloc_in_atom(atom, strlen(value) + 1);
-               if (!iface_pattern)
-                       return NULL;
-               memcpy(iface_pattern, value, len);
-               config.c_iface_pattern = iface_pattern;
-               free(c->config->c_iface_pattern);
-               c->config->c_iface_pattern = strdup(iface_pattern);
+               SET_STR(c_iface_pattern);
                break;
        case lldpctl_k_config_mgmt_pattern:
-               mgmt_pattern = _lldpctl_alloc_in_atom(atom, strlen(value) + 1);
-               if (!mgmt_pattern)
-                       return NULL;
-               memcpy(mgmt_pattern, value, len);
-               config.c_mgmt_pattern = mgmt_pattern;
-               free(c->config->c_mgmt_pattern);
-               c->config->c_mgmt_pattern = strdup(mgmt_pattern);
+               SET_STR(c_mgmt_pattern);
                break;
        case lldpctl_k_config_description:
-               description = _lldpctl_alloc_in_atom(atom, strlen(value) + 1);
-               if (!description)
-                       return NULL;
-               memcpy(description, value, len);
-               config.c_description = description;
-               free(c->config->c_description);
-               c->config->c_description = strdup(description);
+               SET_STR(c_description);
                break;
        case lldpctl_k_config_platform:
-               description = _lldpctl_alloc_in_atom(atom, strlen(value) + 1);
-               if (!description)
-                       return NULL;
-               memcpy(description, value, len);
-               config.c_platform = description;
-               free(c->config->c_platform);
-               c->config->c_description = strdup(description);
+               SET_STR(c_platform);
                break;
        case lldpctl_k_config_hostname:
-               description = _lldpctl_alloc_in_atom(atom, strlen(value) + 1);
-               if (!description)
-                       return NULL;
-               memcpy(description, value, len);
-               config.c_hostname = description;
-               free(c->config->c_hostname);
-               c->config->c_hostname = strdup(description);
+               SET_STR(c_hostname);
                break;
        default:
                SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST);
@@ -470,6 +450,8 @@ _lldpctl_atom_set_str_config(lldpctl_atom_t *atom, lldpctl_key_t key,
        free(canary);
        if (rc == 0) return atom;
 
+#undef SET_STR
+
        return NULL;
 }