Whamcloud - gitweb
LU-12513 utils: add handling YAML_NO_TOKEN
[fs/lustre-release.git] / lnet / utils / lnetconfig / cyaml.c
index fbe0d4f..0dcae12 100644 (file)
@@ -18,7 +18,7 @@
  *
  * LGPL HEADER END
  *
- * Copyright (c) 2014, 2015, Intel Corporation.
+ * Copyright (c) 2014, 2017, Intel Corporation.
  *
  * Author:
  *   Amir Shehata <amir.shehata@intel.com>
@@ -107,7 +107,7 @@ struct cYAML_tree_node {
 typedef enum cYAML_handler_error (*yaml_token_handler)(yaml_token_t *token,
                                                struct cYAML_tree_node *);
 
-static enum cYAML_handler_error yaml_parse_error(yaml_token_t *token,
+static enum cYAML_handler_error yaml_no_token(yaml_token_t *token,
                                        struct cYAML_tree_node *tree);
 static enum cYAML_handler_error yaml_stream_start(yaml_token_t *token,
                                        struct cYAML_tree_node *tree);
@@ -136,7 +136,7 @@ static enum cYAML_handler_error yaml_entry_token(yaml_token_t *token,
 
 /* dispatch table */
 static yaml_token_handler dispatch_tbl[] = {
-       [YAML_NO_TOKEN] = yaml_parse_error,
+       [YAML_NO_TOKEN] = yaml_no_token,
        [YAML_STREAM_START_TOKEN] = yaml_stream_start,
        [YAML_STREAM_END_TOKEN] = yaml_stream_end,
        [YAML_VERSION_DIRECTIVE_TOKEN] = yaml_not_supported,
@@ -460,10 +460,11 @@ static int assign_type_value(struct cYAML *obj, const char *value)
  * else state = VALUE
  *
  */
-static enum cYAML_handler_error yaml_parse_error(yaml_token_t *token,
-                                                struct cYAML_tree_node *tree)
+
+static enum cYAML_handler_error yaml_no_token(yaml_token_t *token,
+                                             struct cYAML_tree_node *tree)
 {
-       return CYAML_ERROR_PARSE;
+       return CYAML_ERROR_NONE;
 }
 
 static enum cYAML_handler_error yaml_stream_start(yaml_token_t *token,
@@ -545,7 +546,8 @@ static enum cYAML_handler_error yaml_scalar(yaml_token_t *token,
                  strdup((const char *)token->data.scalar.value);
 
                tree->state = TREE_STATE_KEY_FILLED;
-       } else if (tree->state == TREE_STATE_VALUE) {
+       } else if (tree->state == TREE_STATE_VALUE ||
+                  tree->state == TREE_STATE_SEQ_START) {
                if (assign_type_value(tree->cur,
                                      (char *)token->data.scalar.value))
                        /* failed to assign a value */
@@ -590,7 +592,8 @@ static enum cYAML_handler_error yaml_entry_token(yaml_token_t *token,
        struct cYAML *obj;
 
        if (tree->state != TREE_STATE_SEQ_START &&
-           tree->state != TREE_STATE_BLK_STARTED)
+           tree->state != TREE_STATE_BLK_STARTED &&
+           tree->state != TREE_STATE_VALUE)
                return CYAML_ERROR_UNEXPECTED_STATE;
 
        if (tree->state == TREE_STATE_SEQ_START) {
@@ -678,9 +681,15 @@ static bool clean_usr_data(struct cYAML *node, void *usr_data, void **out)
 
 static bool free_node(struct cYAML *node, void *user_data, void **out)
 {
-       if (node)
-               free(node);
+       if (!node)
+               return true;
+
+       if (node->cy_type == CYAML_TYPE_STRING)
+               free(node->cy_valuestring);
+       if (node->cy_string)
+               free(node->cy_string);
 
+       free(node);
        return true;
 }
 
@@ -794,7 +803,7 @@ static inline void print_simple(FILE *f, struct cYAML *node,
        if (cpi->array_first_elem)
                fprintf(f, "%*s- ", INDENT * level, "");
 
-       fprintf(f, "%*s""%s: %d\n", (cpi->array_first_elem) ? 0 :
+       fprintf(f, "%*s""%s: %" PRId64 "\n", (cpi->array_first_elem) ? 0 :
                INDENT * level + ind, "", node->cy_string,
                node->cy_valueint);
 }
@@ -845,7 +854,7 @@ static void print_number(FILE *f, struct cYAML *node,
 
        if ((fabs(((double)node->cy_valueint) - d) <= DBL_EPSILON) &&
            (d <= INT_MAX) && (d >= INT_MIN))
-               fprintf(f, "%*s""%s: %d\n", (cpi->array_first_elem) ? 0 :
+               fprintf(f, "%*s""%s: %" PRId64 "\n", (cpi->array_first_elem) ? 0 :
                        INDENT * level + ind, "",
                        node->cy_string, node->cy_valueint);
        else {
@@ -1198,17 +1207,17 @@ struct cYAML *cYAML_build_tree(char *yaml_file,
                rc = dispatch_tbl[token.type](&token, &tree);
                if (rc != CYAML_ERROR_NONE) {
                        snprintf(err_str, sizeof(err_str),
-                               "Failed to handle token:%d "
+                               "Failed to handle token:%d %s"
                                "[state=%d, rc=%d]",
-                               token.type, tree.state, rc);
+                                token.type, token_type_string[token.type],
+                                tree.state, rc);
                        cYAML_build_error(-1, -1, "yaml", "builder",
                                          err_str,
                                          err_rc);
                }
                /* Are we finished? */
                done = (rc != CYAML_ERROR_NONE ||
-                       token.type == YAML_STREAM_END_TOKEN ||
-                       token.type == YAML_NO_TOKEN);
+                       token.type == YAML_STREAM_END_TOKEN);
 
                token_type = token.type;