Whamcloud - gitweb
LU-7734 lnet: configure local NI from DLC
[fs/lustre-release.git] / lnet / utils / cyaml / cyaml.c
index dccce6c..a713566 100644 (file)
@@ -18,7 +18,7 @@
  *
  * LGPL HEADER END
  *
- * Copyright (c) 2013, Intel Corporation.
+ * Copyright (c) 2014, 2015, Intel Corporation.
  *
  * Author:
  *   Amir Shehata <amir.shehata@intel.com>
@@ -44,7 +44,7 @@
 #include <float.h>
 #include <limits.h>
 #include <ctype.h>
-#include "libcfs/list.h"
+#include "libcfs/util/list.h"
 #include "cyaml.h"
 
 #define INDENT         4
@@ -160,6 +160,32 @@ static yaml_token_handler dispatch_tbl[] = {
        [YAML_SCALAR_TOKEN] = yaml_scalar,
 };
 
+/* dispatch table */
+static char *token_type_string[] = {
+       [YAML_NO_TOKEN] = "YAML_NO_TOKEN",
+       [YAML_STREAM_START_TOKEN] = "YAML_STREAM_START_TOKEN",
+       [YAML_STREAM_END_TOKEN] = "YAML_STREAM_END_TOKEN",
+       [YAML_VERSION_DIRECTIVE_TOKEN] = "YAML_VERSION_DIRECTIVE_TOKEN",
+       [YAML_TAG_DIRECTIVE_TOKEN] = "YAML_TAG_DIRECTIVE_TOKEN",
+       [YAML_DOCUMENT_START_TOKEN] = "YAML_DOCUMENT_START_TOKEN",
+       [YAML_DOCUMENT_END_TOKEN] = "YAML_DOCUMENT_END_TOKEN",
+       [YAML_BLOCK_SEQUENCE_START_TOKEN] = "YAML_BLOCK_SEQUENCE_START_TOKEN",
+       [YAML_BLOCK_MAPPING_START_TOKEN] = "YAML_BLOCK_MAPPING_START_TOKEN",
+       [YAML_BLOCK_END_TOKEN] = "YAML_BLOCK_END_TOKEN",
+       [YAML_FLOW_SEQUENCE_START_TOKEN] = "YAML_FLOW_SEQUENCE_START_TOKEN",
+       [YAML_FLOW_SEQUENCE_END_TOKEN] = "YAML_FLOW_SEQUENCE_END_TOKEN",
+       [YAML_FLOW_MAPPING_START_TOKEN] = "YAML_FLOW_MAPPING_START_TOKEN",
+       [YAML_FLOW_MAPPING_END_TOKEN] = "YAML_FLOW_MAPPING_END_TOKEN",
+       [YAML_BLOCK_ENTRY_TOKEN] = "YAML_BLOCK_ENTRY_TOKEN",
+       [YAML_FLOW_ENTRY_TOKEN] = "YAML_FLOW_ENTRY_TOKEN",
+       [YAML_KEY_TOKEN] = "YAML_KEY_TOKEN",
+       [YAML_VALUE_TOKEN] = "YAML_VALUE_TOKEN",
+       [YAML_ALIAS_TOKEN] = "YAML_ALIAS_TOKEN",
+       [YAML_ANCHOR_TOKEN] = "YAML_ANCHOR_TOKEN",
+       [YAML_TAG_TOKEN] = "YAML_TAG_TOKEN",
+       [YAML_SCALAR_TOKEN] = "YAML_SCALAR_TOKEN",
+};
+
 static void cYAML_ll_free(struct list_head *ll)
 {
        struct cYAML_ll *node, *tmp;
@@ -448,7 +474,8 @@ static enum cYAML_handler_error yaml_stream_start(yaml_token_t *token,
 static enum cYAML_handler_error yaml_stream_end(yaml_token_t *token,
                                                struct cYAML_tree_node *tree)
 {
-       if (tree->state != TREE_STATE_TREE_STARTED)
+       if (tree->state != TREE_STATE_TREE_STARTED &&
+           tree->state != TREE_STATE_COMPLETE)
                return CYAML_ERROR_UNEXPECTED_STATE;
 
        tree->state = TREE_STATE_INITED;
@@ -885,6 +912,9 @@ static void print_value(FILE *f, struct list_head *stack)
        struct cYAML_print_info *cpi = NULL;
        struct cYAML *node = cYAML_ll_pop(stack, &cpi);
 
+       if (node == NULL)
+               return;
+
        switch (node->cy_type) {
        case CYAML_TYPE_FALSE:
        case CYAML_TYPE_TRUE:
@@ -1072,7 +1102,7 @@ void cYAML_build_error(int rc, int seq_no, char *cmd,
                goto failed;
 
        if (seq_no >= 0 &&
-           cYAML_create_number(err, "seqno", seq_no) == NULL)
+           cYAML_create_number(err, "seq_no", seq_no) == NULL)
                goto failed;
 
        if (cYAML_create_number(err, "errno", rc) == NULL)
@@ -1084,14 +1114,17 @@ void cYAML_build_error(int rc, int seq_no, char *cmd,
        return;
 
 failed:
+       /* Only reason we get here is if we run out of memory */
        cYAML_free_tree(r);
        r = NULL;
+       fprintf(stderr, "error:\n\tfatal: out of memory\n");
 }
 
 struct cYAML *cYAML_build_tree(char *yaml_file,
                               const char *yaml_blk,
                               size_t yaml_blk_size,
-                              struct cYAML **err_rc)
+                              struct cYAML **err_rc,
+                              bool debug)
 {
        yaml_parser_t parser;
        yaml_token_t token;
@@ -1109,7 +1142,7 @@ struct cYAML *cYAML_build_tree(char *yaml_file,
        /* Create the Parser object. */
        yaml_parser_initialize(&parser);
 
-       /* file alwyas takes precedence */
+       /* file always takes precedence */
        if (yaml_file != NULL) {
                /* Set a file input. */
                input = fopen(yaml_file, "rb");
@@ -1139,6 +1172,11 @@ struct cYAML *cYAML_build_tree(char *yaml_file,
                */
                yaml_parser_scan(&parser, &token);
 
+               if (debug)
+                       fprintf(stderr, "token.type = %s: %s\n",
+                               token_type_string[token.type],
+                               (token.type == YAML_SCALAR_TOKEN) ?
+                               (char*)token.data.scalar.value : "");
                rc = dispatch_tbl[token.type](&token, &tree);
                if (rc != CYAML_ERROR_NONE) {
                        snprintf(err_str, sizeof(err_str),