Motr  M0
console_yaml.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2011-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_OTHER
24 #include "lib/trace.h"
25 
26 #include "lib/errno.h"
27 #include "lib/memory.h"
28 #include "lib/string.h" /* m0_streq */
29 
30 #include "console/console_yaml.h"
31 
32 static void yaml_parser_error_detect(const yaml_parser_t * parser)
33 {
34  M0_PRE(parser != NULL);
35 
36  switch (parser->error) {
37  case YAML_MEMORY_ERROR:
38  fprintf(stderr, "Memory error: Not enough memory for parsing\n");
39  break;
40  case YAML_READER_ERROR:
41  if (parser->problem_value != -1)
42  fprintf(stderr, "Reader error: %s: #%X at %lu\n",
43  parser->problem, parser->problem_value,
44  parser->problem_offset);
45  else
46  fprintf(stderr, "Reader error: %s at %lu\n",
47  parser->problem, parser->problem_offset);
48  break;
49  case YAML_SCANNER_ERROR:
50  if (parser->context)
51  fprintf(stderr, "Scanner error: %s at line %lu, column %lu"
52  " %s at line %lu, column %lu\n",
53  parser->context, parser->context_mark.line+1,
54  parser->context_mark.column+1, parser->problem,
55  parser->problem_mark.line+1,
56  parser->problem_mark.column+1);
57  else
58  fprintf(stderr, "Scanner error: %s at line %lu, column %lu\n",
59  parser->problem, parser->problem_mark.line+1,
60  parser->problem_mark.column+1);
61  break;
62  case YAML_PARSER_ERROR:
63  if (parser->context)
64  fprintf(stderr, "Parser error: %s at line %lu, column %lu"
65  " %s at line %lu, column %lu\n",
66  parser->context, parser->context_mark.line+1,
67  parser->context_mark.column+1,parser->problem,
68  parser->problem_mark.line+1,
69  parser->problem_mark.column+1);
70  else
71  fprintf(stderr, "Parser error: %s at line %lu, column %lu\n",
72  parser->problem, parser->problem_mark.line+1,
73  parser->problem_mark.column+1);
74  break;
75  case YAML_COMPOSER_ERROR:
76  case YAML_WRITER_ERROR:
77  case YAML_EMITTER_ERROR:
78  case YAML_NO_ERROR:
79  break;
80  default:
81  M0_IMPOSSIBLE("Invalid error");
82  }
83 }
84 
92 M0_INTERNAL bool yaml_support;
93 
94 M0_INTERNAL int m0_cons_yaml_init(const char *file_path)
95 {
96  int rc;
97  yaml_node_t *root_node;
98 
99  M0_ENTRY("file_path=`%s'", file_path);
100  M0_PRE(file_path != NULL);
101 
102  yaml_info.cyi_file = fopen(file_path, "r");
103 
104  if (yaml_info.cyi_file == NULL) {
105  perror("Failed to open file ");
106  printf("%s, errno = %d\n", file_path, errno);
107  goto error;
108  }
109  /* Initialize parser */
110  rc = yaml_parser_initialize(&yaml_info.cyi_parser);
111  if (rc != 1) {
112  fprintf(stderr, "Failed to initialize parser!\n");
113  fclose(yaml_info.cyi_file);
114  goto error;
115  }
116 
117  /* Set input file */
118  yaml_parser_set_input_file(&yaml_info.cyi_parser, yaml_info.cyi_file);
119 
120  /* Load document */
121  rc = yaml_parser_load(&yaml_info.cyi_parser, &yaml_info.cyi_document);
122  if (rc != 1) {
123  yaml_parser_delete(&yaml_info.cyi_parser);
124  fclose(yaml_info.cyi_file);
125  fprintf(stderr, "yaml parser load failed!!\n");
126  goto error;
127  }
128 
129  root_node = yaml_document_get_root_node(&yaml_info.cyi_document);
130  if (root_node == NULL) {
131  yaml_document_delete(&yaml_info.cyi_document);
132  yaml_parser_delete(&yaml_info.cyi_parser);
133  fclose(yaml_info.cyi_file);
134  fprintf(stderr, "document get root node failed\n");
135  goto error;
136  }
137 
139  yaml_support = true;
140 
141  return M0_RC(0);
142 error:
144  return M0_RC(-EINVAL);
145 }
146 
147 M0_INTERNAL void m0_cons_yaml_fini(void)
148 {
149  yaml_support = false;
150  yaml_document_delete(&yaml_info.cyi_document);
151  yaml_parser_delete(&yaml_info.cyi_parser);
152  fclose(yaml_info.cyi_file);
153 }
154 
155 static yaml_node_t *search_node(const char *name)
156 {
157  yaml_document_t *doc = &yaml_info.cyi_document;
158  yaml_node_t *node = yaml_info.cyi_current;
159  unsigned char *data_value;
160 
161  M0_ENTRY("name=`%s'", name);
162  for ( ; node < doc->nodes.top; node++) {
163  if (node->type == YAML_SCALAR_NODE) {
164  data_value = node->data.scalar.value;
165  if (m0_streq((const char *)data_value, name)) {
166  node++;
168  M0_LEAVE("found");
169  return node;
170  }
171  }
172  }
173  M0_LEAVE("not found");
174  return NULL;
175 }
176 
177 M0_INTERNAL void *m0_cons_yaml_get_value(const char *name)
178 {
179  yaml_node_t *node = search_node(name);
180  return node == NULL ? NULL : node->data.scalar.value;
181 }
182 
183 M0_INTERNAL int m0_cons_yaml_set_value(const char *name, void *data)
184 {
185  return M0_ERR(-ENOTSUP);
186 }
187 
189 #undef M0_TRACE_SUBSYSTEM
190 
191 /*
192  * Local variables:
193  * c-indentation-style: "K&R"
194  * c-basic-offset: 8
195  * tab-width: 8
196  * fill-column: 80
197  * scroll-step: 1
198  * End:
199  */
#define M0_PRE(cond)
#define NULL
Definition: misc.h:38
yaml_document_t cyi_document
Definition: console_yaml.h:51
M0_LEAVE()
static struct net_test_cmd_node * node
Definition: commands.c:72
struct m0_bufvec data
Definition: di.c:40
static int error
Definition: mdstore.c:64
M0_INTERNAL int m0_cons_yaml_init(const char *file_path)
Inititalizes parser by opening given file. and also checks for error by getting root node...
Definition: console_yaml.c:94
static yaml_node_t * search_node(const char *name)
Definition: console_yaml.c:155
static struct m0_cons_yaml_info yaml_info
Definition: console_yaml.c:90
return M0_RC(rc)
#define M0_ENTRY(...)
Definition: trace.h:170
yaml_parser_t cyi_parser
Definition: console_yaml.h:47
return M0_ERR(-EOPNOTSUPP)
const char * name
Definition: trace.c:110
M0_INTERNAL void m0_cons_yaml_fini(void)
Deletes the parser and closes the YAML file.
Definition: console_yaml.c:147
#define m0_streq(a, b)
Definition: string.h:34
parser
Definition: queues.py:206
yaml_node_t * cyi_current
Definition: console_yaml.h:53
M0_INTERNAL int m0_cons_yaml_set_value(const char *name, void *data)
Search for specified string and get the respctive value form YAML file. (like "name : console") ...
Definition: console_yaml.c:183
Keeps info for YAML parser.
Definition: console_yaml.h:45
M0_INTERNAL void * m0_cons_yaml_get_value(const char *name)
Search for specified string and set the respctive value form YAML file. (like "name : console") ...
Definition: console_yaml.c:177
M0_INTERNAL bool yaml_support
Definition: console_yaml.c:92
int32_t rc
Definition: trigger_fop.h:47
static void yaml_parser_error_detect(const yaml_parser_t *parser)
Definition: console_yaml.c:32
#define M0_IMPOSSIBLE(fmt,...)