Declare loop variable in the loop expression.
[collectd.git] / src / utils_cmd_putval.c
index ea3a16c..691f1f8 100644 (file)
@@ -69,8 +69,11 @@ static int set_option (value_list_t *vl, const char *key, const char *value)
  */
 
 cmd_status_t cmd_parse_putval (size_t argc, char **argv,
-               cmd_putval_t *ret_putval, cmd_error_handler_t *err)
+               cmd_putval_t *ret_putval, const cmd_options_t *opts,
+               cmd_error_handler_t *err)
 {
+       cmd_status_t result;
+
        char *identifier;
        char *hostname;
        char *plugin;
@@ -83,7 +86,13 @@ cmd_status_t cmd_parse_putval (size_t argc, char **argv,
 
        const data_set_t *ds;
        value_list_t vl = VALUE_LIST_INIT;
-       size_t i;
+
+       if ((ret_putval == NULL) || (opts == NULL))
+       {
+               errno = EINVAL;
+               cmd_error (CMD_ERROR, err, "Invalid arguments to cmd_parse_putval.");
+               return (CMD_ERROR);
+       }
 
        if (argc < 2)
        {
@@ -100,7 +109,8 @@ cmd_status_t cmd_parse_putval (size_t argc, char **argv,
 
        status = parse_identifier (identifier, &hostname,
                        &plugin, &plugin_instance,
-                       &type, &type_instance);
+                       &type, &type_instance,
+                       opts->identifier_default_host);
        if (status != 0)
        {
                DEBUG ("cmd_handle_putval: Cannot parse identifier `%s'.",
@@ -152,16 +162,18 @@ cmd_status_t cmd_parse_putval (size_t argc, char **argv,
                return (CMD_ERROR);
        }
 
-       ret_putval->identifier = identifier_copy;
-       if (ret_putval->identifier == NULL)
+       ret_putval->raw_identifier = identifier_copy;
+       if (ret_putval->raw_identifier == NULL)
        {
                cmd_error (CMD_ERROR, err, "malloc failed.");
                cmd_destroy_putval (ret_putval);
+               sfree (vl.values);
                return (CMD_ERROR);
        }
 
        /* All the remaining fields are part of the option list. */
-       for (i = 1; i < argc; ++i)
+       result = CMD_OK;
+       for (size_t i = 1; i < argc; ++i)
        {
                value_list_t *tmp;
 
@@ -180,8 +192,8 @@ cmd_status_t cmd_parse_putval (size_t argc, char **argv,
                {
                        /* parse_option failed, buffer has been modified.
                         * => we need to abort */
-                       cmd_destroy_putval (ret_putval);
-                       return (status);
+                       result = status;
+                       break;
                }
                /* else: cmd_parse_option did not find an option; treat this as a
                 * value list. */
@@ -190,8 +202,8 @@ cmd_status_t cmd_parse_putval (size_t argc, char **argv,
                if (status != 0)
                {
                        cmd_error (CMD_PARSE_ERROR, err, "Parsing the values string failed.");
-                       cmd_destroy_putval (ret_putval);
-                       return (CMD_PARSE_ERROR);
+                       result = CMD_PARSE_ERROR;
+                       break;
                }
 
                tmp = (value_list_t *) realloc (ret_putval->vl,
@@ -200,7 +212,8 @@ cmd_status_t cmd_parse_putval (size_t argc, char **argv,
                {
                        cmd_error (CMD_ERROR, err, "realloc failed.");
                        cmd_destroy_putval (ret_putval);
-                       return (CMD_ERROR);
+                       result = CMD_ERROR;
+                       break;
                }
 
                ret_putval->vl = tmp;
@@ -209,21 +222,27 @@ cmd_status_t cmd_parse_putval (size_t argc, char **argv,
        } /* while (*buffer != 0) */
        /* Done parsing the options. */
 
-       return (CMD_OK);
+       if (result != CMD_OK)
+       {
+               if (ret_putval->vl_num == 0)
+                       sfree (vl.values);
+               cmd_destroy_putval (ret_putval);
+       }
+
+       return (result);
 } /* cmd_status_t cmd_parse_putval */
 
 void cmd_destroy_putval (cmd_putval_t *putval)
 {
-       size_t i;
-
        if (putval == NULL)
                return;
 
-       sfree (putval->identifier);
+       sfree (putval->raw_identifier);
 
-       for (i = 0; i < putval->vl_num; ++i)
+       for (size_t i = 0; i < putval->vl_num; ++i)
        {
-               sfree (putval->vl[i].values);
+               if (i == 0) /* values is shared between all entries */
+                       sfree (putval->vl[i].values);
                meta_data_destroy (putval->vl[i].meta);
                putval->vl[i].meta = NULL;
        }
@@ -236,14 +255,13 @@ cmd_status_t cmd_handle_putval (FILE *fh, char *buffer)
 {
        cmd_error_handler_t err = { cmd_error_fh, fh };
        cmd_t cmd;
-       size_t i;
 
        int status;
 
        DEBUG ("utils_cmd_putval: cmd_handle_putval (fh = %p, buffer = %s);",
                        (void *) fh, buffer);
 
-       if ((status = cmd_parse (buffer, &cmd, &err)) != CMD_OK)
+       if ((status = cmd_parse (buffer, &cmd, NULL, &err)) != CMD_OK)
                return (status);
        if (cmd.type != CMD_PUTVAL)
        {
@@ -253,7 +271,7 @@ cmd_status_t cmd_handle_putval (FILE *fh, char *buffer)
                return (CMD_UNKNOWN_COMMAND);
        }
 
-       for (i = 0; i < cmd.cmd.putval.vl_num; ++i)
+       for (size_t i = 0; i < cmd.cmd.putval.vl_num; ++i)
                plugin_dispatch_values (&cmd.cmd.putval.vl[i]);
 
        if (fh != stdout)