4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of the
9 * License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 * Copyright (c) 2013, Intel Corporation.
24 * Amir Shehata <amir.shehata@intel.com>
28 * The cYAML tree is constructed as an n-tree.
32 * cmd 2 -> attr1 -> attr2
35 * attr2.1 -> attr2.1.1 -> attr2.1.2
47 #include "libcfs/list.h"
55 * This structure contains print information
56 * required when printing the node
58 struct cYAML_print_info {
66 * Linked list of different trees representing YAML
70 struct list_head list;
72 struct cYAML_print_info *print_info;
75 static void print_value(FILE *f, struct list_head *stack);
77 enum cYAML_handler_error {
79 CYAML_ERROR_UNEXPECTED_STATE = -1,
80 CYAML_ERROR_NOT_SUPPORTED = -2,
81 CYAML_ERROR_OUT_OF_MEM = -3,
82 CYAML_ERROR_BAD_VALUE = -4,
83 CYAML_ERROR_PARSE = -5,
86 enum cYAML_tree_state {
87 TREE_STATE_COMPLETE = 0,
89 TREE_STATE_TREE_STARTED,
90 TREE_STATE_BLK_STARTED,
92 TREE_STATE_KEY_FILLED,
97 struct cYAML_tree_node {
99 /* cur is the current node we're operating on */
101 enum cYAML_tree_state state;
102 int from_blk_map_start;
103 /* represents the tree depth */
107 typedef enum cYAML_handler_error (*yaml_token_handler)(yaml_token_t *token,
108 struct cYAML_tree_node *);
110 static enum cYAML_handler_error yaml_parse_error(yaml_token_t *token,
111 struct cYAML_tree_node *tree);
112 static enum cYAML_handler_error yaml_stream_start(yaml_token_t *token,
113 struct cYAML_tree_node *tree);
114 static enum cYAML_handler_error yaml_stream_end(yaml_token_t *token,
115 struct cYAML_tree_node *tree);
116 static enum cYAML_handler_error yaml_not_supported(yaml_token_t *token,
117 struct cYAML_tree_node *tree);
118 static enum cYAML_handler_error yaml_document_start(yaml_token_t *token,
119 struct cYAML_tree_node *tree);
120 static enum cYAML_handler_error yaml_document_end(yaml_token_t *token,
121 struct cYAML_tree_node *tree);
122 static enum cYAML_handler_error yaml_blk_seq_start(yaml_token_t *token,
123 struct cYAML_tree_node *tree);
124 static enum cYAML_handler_error yaml_blk_mapping_start(yaml_token_t *token,
125 struct cYAML_tree_node *tree);
126 static enum cYAML_handler_error yaml_block_end(yaml_token_t *token,
127 struct cYAML_tree_node *tree);
128 static enum cYAML_handler_error yaml_key(yaml_token_t *token,
129 struct cYAML_tree_node *tree);
130 static enum cYAML_handler_error yaml_value(yaml_token_t *token,
131 struct cYAML_tree_node *tree);
132 static enum cYAML_handler_error yaml_scalar(yaml_token_t *token,
133 struct cYAML_tree_node *tree);
134 static enum cYAML_handler_error yaml_entry_token(yaml_token_t *token,
135 struct cYAML_tree_node *tree);
138 static yaml_token_handler dispatch_tbl[] = {
139 [YAML_NO_TOKEN] = yaml_parse_error,
140 [YAML_STREAM_START_TOKEN] = yaml_stream_start,
141 [YAML_STREAM_END_TOKEN] = yaml_stream_end,
142 [YAML_VERSION_DIRECTIVE_TOKEN] = yaml_not_supported,
143 [YAML_TAG_DIRECTIVE_TOKEN] = yaml_not_supported,
144 [YAML_DOCUMENT_START_TOKEN] = yaml_document_start,
145 [YAML_DOCUMENT_END_TOKEN] = yaml_document_end,
146 [YAML_BLOCK_SEQUENCE_START_TOKEN] = yaml_blk_seq_start,
147 [YAML_BLOCK_MAPPING_START_TOKEN] = yaml_blk_mapping_start,
148 [YAML_BLOCK_END_TOKEN] = yaml_block_end,
149 [YAML_FLOW_SEQUENCE_START_TOKEN] = yaml_not_supported,
150 [YAML_FLOW_SEQUENCE_END_TOKEN] = yaml_not_supported,
151 [YAML_FLOW_MAPPING_START_TOKEN] = yaml_not_supported,
152 [YAML_FLOW_MAPPING_END_TOKEN] = yaml_not_supported,
153 [YAML_BLOCK_ENTRY_TOKEN] = yaml_entry_token,
154 [YAML_FLOW_ENTRY_TOKEN] = yaml_not_supported,
155 [YAML_KEY_TOKEN] = yaml_key,
156 [YAML_VALUE_TOKEN] = yaml_value,
157 [YAML_ALIAS_TOKEN] = yaml_not_supported,
158 [YAML_ANCHOR_TOKEN] = yaml_not_supported,
159 [YAML_TAG_TOKEN] = yaml_not_supported,
160 [YAML_SCALAR_TOKEN] = yaml_scalar,
163 static void cYAML_ll_free(struct list_head *ll)
165 struct cYAML_ll *node, *tmp;
167 list_for_each_entry_safe(node, tmp, ll, list) {
168 free(node->print_info);
173 static int cYAML_ll_push(struct cYAML *obj,
174 const struct cYAML_print_info *print_info,
175 struct list_head *list)
177 struct cYAML_ll *node = calloc(1, sizeof(*node));
181 INIT_LIST_HEAD(&node->list);
184 node->print_info = calloc(1, sizeof(*print_info));
185 if (node->print_info == NULL) {
189 *node->print_info = *print_info;
193 list_add(&node->list, list);
198 static struct cYAML *cYAML_ll_pop(struct list_head *list,
199 struct cYAML_print_info **print_info)
201 struct cYAML_ll *pop;
202 struct cYAML *obj = NULL;
204 if (!list_empty(list)) {
205 pop = list_entry(list->next, struct cYAML_ll, list);
208 if (print_info != NULL)
209 *print_info = pop->print_info;
210 list_del(&pop->list);
212 if (print_info == NULL)
213 free(pop->print_info);
220 static int cYAML_ll_count(struct list_head *ll)
223 struct list_head *node;
225 list_for_each(node, ll)
231 static int cYAML_tree_init(struct cYAML_tree_node *tree)
233 struct cYAML *obj = NULL, *cur = NULL;
238 obj = calloc(1, sizeof(*obj));
243 /* append the node */
245 while (cur->cy_next != NULL)
252 obj->cy_type = CYAML_TYPE_OBJECT;
255 tree->state = TREE_STATE_COMPLETE;
257 /* free it and start anew */
258 if (!list_empty(&tree->ll))
259 cYAML_ll_free(&tree->ll);
264 static struct cYAML *create_child(struct cYAML *parent)
271 obj = calloc(1, sizeof(*obj));
275 /* set the type to OBJECT and let the value change that */
276 obj->cy_type = CYAML_TYPE_OBJECT;
278 parent->cy_child = obj;
283 static struct cYAML *create_sibling(struct cYAML *sibling)
290 obj = calloc(1, sizeof(*obj));
294 /* set the type to OBJECT and let the value change that */
295 obj->cy_type = CYAML_TYPE_OBJECT;
297 sibling->cy_next = obj;
298 obj->cy_prev = sibling;
303 /* Parse the input text to generate a number,
304 * and populate the result into item. */
305 static bool parse_number(struct cYAML *item, const char *input)
307 double n = 0, sign = 1, scale = 0;
308 int subscale = 0, signsubscale = 1;
309 const char *num = input;
319 if (*num >= '1' && *num <= '9') {
321 n = (n * 10.0) + (*num++ - '0');
322 } while (*num >= '0' && *num <= '9');
325 if (*num == '.' && num[1] >= '0' && num[1] <= '9') {
328 n = (n * 10.0) + (*num++ - '0');
330 } while (*num >= '0' && *num <= '9');
333 if (*num == 'e' || *num == 'E') {
337 } else if (*num == '-') {
341 while (*num >= '0' && *num <= '9')
342 subscale = (subscale * 10) + (*num++ - '0');
345 /* check to see if the entire string is consumed. If not then
346 * that means this is a string with a number in it */
347 if (num != (input + strlen(input)))
350 /* number = +/- number.fraction * 10^+/- exponent */
351 n = sign * n * pow(10.0, (scale + subscale * signsubscale));
353 item->cy_valuedouble = n;
354 item->cy_valueint = (int)n;
355 item->cy_type = CYAML_TYPE_NUMBER;
360 static int assign_type_value(struct cYAML *obj, const char *value)
365 if (strcmp(value, "null") == 0)
366 obj->cy_type = CYAML_TYPE_NULL;
367 else if (strcmp(value, "false") == 0) {
368 obj->cy_type = CYAML_TYPE_FALSE;
369 obj->cy_valueint = 0;
370 } else if (strcmp(value, "true") == 0) {
371 obj->cy_type = CYAML_TYPE_TRUE;
372 obj->cy_valueint = 1;
373 } else if (*value == '-' || (*value >= '0' && *value <= '9')) {
374 if (parse_number(obj, value) == 0) {
375 obj->cy_valuestring = strdup(value);
376 obj->cy_type = CYAML_TYPE_STRING;
379 obj->cy_valuestring = strdup(value);
380 obj->cy_type = CYAML_TYPE_STRING;
388 * Builds the YAML tree rpresentation as the tokens are passed in
390 * if token == STREAM_START && tree_state != COMPLETE
391 * something wrong. fail.
392 * else tree_state = INITIED
393 * if token == DOCUMENT_START && tree_state != COMPLETE || INITED
394 * something wrong, fail.
395 * else tree_state = TREE_STARTED
396 * if token == DOCUMENT_END
397 * tree_state = INITED if no STREAM START, else tree_state = COMPLETE
398 * erase everything on ll
399 * if token == STREAM_END && tree_state != INITED
400 * something wrong fail.
401 * else tree_state = COMPLETED
402 * if token == YAML_KEY_TOKEN && state != TREE_STARTED
403 * something wrong, fail.
404 * if token == YAML_SCALAR_TOKEN && state != KEY || VALUE
406 * else if tree_state == KEY
407 * create a new sibling under the current head of the ll (if ll is
408 * empty insert the new node there and it becomes the root.)
409 * add the scalar value in the "string"
410 * tree_state = KEY_FILLED
411 * else if tree_state == VALUE
412 * try and figure out whether this is a double, int or string and store
414 * state = TREE_STARTED
415 * else if token == YAML_BLOCK_MAPPING_START_TOKEN && tree_state != VALUE
417 * else push the current node on the ll && state = TREE_STARTED
418 * if token == YAML_BLOCK_END_TOKEN && state != TREE_STARTED
420 * else pop the current token off the ll and make it the cur
421 * if token == YAML_VALUE_TOKEN && state != KEY_FILLED
426 static enum cYAML_handler_error yaml_parse_error(yaml_token_t *token,
427 struct cYAML_tree_node *tree)
429 return CYAML_ERROR_PARSE;
432 static enum cYAML_handler_error yaml_stream_start(yaml_token_t *token,
433 struct cYAML_tree_node *tree)
435 enum cYAML_handler_error rc;
437 /* with each new stream initialize a new tree */
438 rc = cYAML_tree_init(tree);
440 if (rc != CYAML_ERROR_NONE)
443 tree->state = TREE_STATE_INITED;
445 return CYAML_ERROR_NONE;
448 static enum cYAML_handler_error yaml_stream_end(yaml_token_t *token,
449 struct cYAML_tree_node *tree)
451 if (tree->state != TREE_STATE_TREE_STARTED)
452 return CYAML_ERROR_UNEXPECTED_STATE;
454 tree->state = TREE_STATE_INITED;
456 return CYAML_ERROR_NONE;
459 static enum cYAML_handler_error
460 yaml_document_start(yaml_token_t *token, struct cYAML_tree_node *tree)
462 if (tree->state != TREE_STATE_INITED)
463 return CYAML_ERROR_UNEXPECTED_STATE;
465 /* go to started state since we're expecting more tokens to come */
466 tree->state = TREE_STATE_TREE_STARTED;
468 return CYAML_ERROR_NONE;
471 static enum cYAML_handler_error yaml_document_end(yaml_token_t *token,
472 struct cYAML_tree_node *tree)
474 if (tree->state != TREE_STATE_COMPLETE)
475 return CYAML_ERROR_UNEXPECTED_STATE;
477 tree->state = TREE_STATE_TREE_STARTED;
479 return CYAML_ERROR_NONE;
482 static enum cYAML_handler_error yaml_key(yaml_token_t *token,
483 struct cYAML_tree_node *tree)
485 if (tree->state != TREE_STATE_BLK_STARTED &&
486 tree->state != TREE_STATE_VALUE)
487 return CYAML_ERROR_UNEXPECTED_STATE;
489 if (tree->from_blk_map_start == 0 ||
490 tree->state == TREE_STATE_VALUE)
491 tree->cur = create_sibling(tree->cur);
493 tree->from_blk_map_start = 0;
495 tree->state = TREE_STATE_KEY;
497 return CYAML_ERROR_NONE;
500 static enum cYAML_handler_error yaml_scalar(yaml_token_t *token,
501 struct cYAML_tree_node *tree)
503 if (tree->state == TREE_STATE_KEY) {
504 /* assign the scalar value to the key that was created */
505 tree->cur->cy_string =
506 strdup((const char *)token->data.scalar.value);
508 tree->state = TREE_STATE_KEY_FILLED;
509 } else if (tree->state == TREE_STATE_VALUE) {
510 if (assign_type_value(tree->cur,
511 (char *)token->data.scalar.value))
512 /* failed to assign a value */
513 return CYAML_ERROR_BAD_VALUE;
514 tree->state = TREE_STATE_BLK_STARTED;
516 return CYAML_ERROR_UNEXPECTED_STATE;
519 return CYAML_ERROR_NONE;
522 static enum cYAML_handler_error yaml_value(yaml_token_t *token,
523 struct cYAML_tree_node *tree)
525 if (tree->state != TREE_STATE_KEY_FILLED)
526 return CYAML_ERROR_UNEXPECTED_STATE;
528 tree->state = TREE_STATE_VALUE;
530 return CYAML_ERROR_NONE;
533 static enum cYAML_handler_error yaml_blk_seq_start(yaml_token_t *token,
534 struct cYAML_tree_node *tree)
536 if (tree->state != TREE_STATE_VALUE)
537 return CYAML_ERROR_UNEXPECTED_STATE;
539 /* Since a sequenc start event determines that this is the start
540 * of an array, then that means the current node we're at is an
541 * array and we need to flag it as such */
542 tree->cur->cy_type = CYAML_TYPE_ARRAY;
543 tree->state = TREE_STATE_SEQ_START;
545 return CYAML_ERROR_NONE;
548 static enum cYAML_handler_error yaml_entry_token(yaml_token_t *token,
549 struct cYAML_tree_node *tree)
553 if (tree->state != TREE_STATE_SEQ_START &&
554 tree->state != TREE_STATE_BLK_STARTED)
555 return CYAML_ERROR_UNEXPECTED_STATE;
557 if (tree->state == TREE_STATE_SEQ_START) {
558 obj = create_child(tree->cur);
560 if (cYAML_ll_push(tree->cur, NULL, &tree->ll))
561 return CYAML_ERROR_OUT_OF_MEM;
565 tree->cur = create_sibling(tree->cur);
566 tree->state = TREE_STATE_SEQ_START;
569 return CYAML_ERROR_NONE;
572 static enum cYAML_handler_error
573 yaml_blk_mapping_start(yaml_token_t *token,
574 struct cYAML_tree_node *tree)
578 if (tree->state != TREE_STATE_VALUE &&
579 tree->state != TREE_STATE_INITED &&
580 tree->state != TREE_STATE_SEQ_START &&
581 tree->state != TREE_STATE_TREE_STARTED)
582 return CYAML_ERROR_UNEXPECTED_STATE;
584 /* block_mapping_start means we're entering another block
585 * indentation, so we need to go one level deeper
586 * create a child of cur */
587 obj = create_child(tree->cur);
589 /* push cur on the stack */
590 if (cYAML_ll_push(tree->cur, NULL, &tree->ll))
591 return CYAML_ERROR_OUT_OF_MEM;
593 /* adding the new child to cur */
596 tree->state = TREE_STATE_BLK_STARTED;
598 tree->from_blk_map_start = 1;
600 return CYAML_ERROR_NONE;
603 static enum cYAML_handler_error yaml_block_end(yaml_token_t *token,
604 struct cYAML_tree_node *tree)
606 if (tree->state != TREE_STATE_BLK_STARTED &&
607 tree->state != TREE_STATE_VALUE)
608 return CYAML_ERROR_UNEXPECTED_STATE;
610 tree->cur = cYAML_ll_pop(&tree->ll, NULL);
612 /* if you have popped all the way to the top level, then move to
613 * the complete state. */
614 if (cYAML_ll_count(&tree->ll) == 0)
615 tree->state = TREE_STATE_COMPLETE;
616 else if (tree->state == TREE_STATE_VALUE)
617 tree->state = TREE_STATE_BLK_STARTED;
619 return CYAML_ERROR_NONE;
622 static enum cYAML_handler_error yaml_not_supported(yaml_token_t *token,
623 struct cYAML_tree_node *tree)
625 return CYAML_ERROR_NOT_SUPPORTED;
628 static bool clean_usr_data(struct cYAML *node, void *usr_data, void **out)
630 cYAML_user_data_free_cb free_cb = usr_data;
632 if (free_cb && node && node->cy_user_data) {
633 free_cb(node->cy_user_data);
634 node->cy_user_data = NULL;
640 static bool free_node(struct cYAML *node, void *user_data, void **out)
648 static bool find_obj_iter(struct cYAML *node, void *usr_data, void **out)
650 char *name = usr_data;
652 if (node != NULL && node->cy_string != NULL &&
653 strcmp(node->cy_string, name) == 0) {
661 struct cYAML *cYAML_get_object_item(struct cYAML *parent, const char *name)
665 if (parent == NULL || parent->cy_child == NULL || name == NULL)
668 node = parent->cy_child;
670 while (node != NULL &&
671 strcmp(node->cy_string, name) != 0) {
672 node = node->cy_next;
678 struct cYAML *cYAML_get_next_seq_item(struct cYAML *seq, struct cYAML **itm)
680 if (*itm != NULL && (*itm)->cy_next != NULL) {
681 *itm = (*itm)->cy_next;
685 if (*itm == NULL && seq->cy_type == CYAML_TYPE_ARRAY) {
686 *itm = seq->cy_child;
693 bool cYAML_is_sequence(struct cYAML *node)
695 return (node != NULL ? node->cy_type == CYAML_TYPE_ARRAY : 0);
698 void cYAML_tree_recursive_walk(struct cYAML *node, cYAML_walk_cb cb,
707 if (!cb(node, usr_data, out))
712 cYAML_tree_recursive_walk(node->cy_child, cb,
713 cb_first, usr_data, out);
716 cYAML_tree_recursive_walk(node->cy_next, cb,
717 cb_first, usr_data, out);
720 if (!cb(node, usr_data, out))
725 struct cYAML *cYAML_find_object(struct cYAML *root, const char *name)
727 struct cYAML *found = NULL;
729 cYAML_tree_recursive_walk(root, find_obj_iter, true,
730 (void *)name, (void **)&found);
735 void cYAML_clean_usr_data(struct cYAML *node, cYAML_user_data_free_cb free_cb)
737 cYAML_tree_recursive_walk(node, clean_usr_data, false, free_cb, NULL);
740 void cYAML_free_tree(struct cYAML *node)
742 cYAML_tree_recursive_walk(node, free_node, false, NULL, NULL);
745 static inline void print_simple(FILE *f, struct cYAML *node,
746 struct cYAML_print_info *cpi)
748 int level = cpi->level;
749 int ind = cpi->extra_ind;
751 if (cpi->array_first_elem)
752 fprintf(f, "%*s- ", INDENT * level, "");
754 fprintf(f, "%*s""%s: %d\n", (cpi->array_first_elem) ? 0 :
755 INDENT * level + ind, "", node->cy_string,
759 static void print_string(FILE *f, struct cYAML *node,
760 struct cYAML_print_info *cpi)
763 int level = cpi->level;
764 int ind = cpi->extra_ind;
766 if (cpi->array_first_elem)
767 fprintf(f, "%*s- ", INDENT * level, "");
769 new_line = strchr(node->cy_valuestring, '\n');
770 if (new_line == NULL)
771 fprintf(f, "%*s""%s: %s\n", (cpi->array_first_elem) ?
772 0 : INDENT * level + ind, "",
773 node->cy_string, node->cy_valuestring);
776 fprintf(f, "%*s""%s: ", (cpi->array_first_elem) ?
777 0 : INDENT * level + ind, "",
779 char *l = node->cy_valuestring;
782 fprintf(f, "%*s""%s\n", indent, "", l);
783 indent = INDENT * level + ind +
784 strlen(node->cy_string) + 2;
787 new_line = strchr(l, '\n');
789 fprintf(f, "%*s""%s\n", indent, "", l);
793 static void print_number(FILE *f, struct cYAML *node,
794 struct cYAML_print_info *cpi)
796 double d = node->cy_valuedouble;
797 int level = cpi->level;
798 int ind = cpi->extra_ind;
800 if (cpi->array_first_elem)
801 fprintf(f, "%*s- ", INDENT * level, "");
803 if ((fabs(((double)node->cy_valueint) - d) <= DBL_EPSILON) &&
804 (d <= INT_MAX) && (d >= INT_MIN))
805 fprintf(f, "%*s""%s: %d\n", (cpi->array_first_elem) ? 0 :
806 INDENT * level + ind, "",
807 node->cy_string, node->cy_valueint);
809 if ((fabs(floor(d) - d) <= DBL_EPSILON) &&
811 fprintf(f, "%*s""%s: %.0f\n",
812 (cpi->array_first_elem) ? 0 :
813 INDENT * level + ind, "",
815 else if ((fabs(d) < 1.0e-6) || (fabs(d) > 1.0e9))
816 fprintf(f, "%*s""%s: %e\n",
817 (cpi->array_first_elem) ? 0 :
818 INDENT * level + ind, "",
821 fprintf(f, "%*s""%s: %f\n",
822 (cpi->array_first_elem) ? 0 :
823 INDENT * level + ind, "",
828 static void print_object(FILE *f, struct cYAML *node,
829 struct list_head *stack,
830 struct cYAML_print_info *cpi)
832 struct cYAML_print_info print_info;
833 struct cYAML *child = node->cy_child;
835 if (node->cy_string != NULL)
836 fprintf(f, "%*s""%s%s:\n", (cpi->array_first_elem) ?
837 INDENT * cpi->level :
838 INDENT * cpi->level + cpi->extra_ind,
839 "", (cpi->array_first_elem) ? "- " : "",
842 print_info.level = (node->cy_string != NULL) ? cpi->level + 1 :
844 print_info.array_first_elem = (node->cy_string == NULL) ?
845 cpi->array_first_elem : 0;
846 print_info.extra_ind = (cpi->array_first_elem) ? EXTRA_IND :
850 if (cYAML_ll_push(child, &print_info, stack) != 0)
852 print_value(f, stack);
853 print_info.array_first_elem = 0;
854 child = child->cy_next;
858 static void print_array(FILE *f, struct cYAML *node,
859 struct list_head *stack,
860 struct cYAML_print_info *cpi)
862 struct cYAML_print_info print_info;
863 struct cYAML *child = node->cy_child;
865 if (node->cy_string != NULL) {
866 fprintf(f, "%*s""%s:\n", INDENT * cpi->level + cpi->extra_ind,
867 "", node->cy_string);
870 print_info.level = (node->cy_string != NULL) ? cpi->level + 1 :
872 print_info.array_first_elem = 1;
873 print_info.extra_ind = EXTRA_IND;
876 if (cYAML_ll_push(child, &print_info, stack) != 0)
878 print_value(f, stack);
879 child = child->cy_next;
883 static void print_value(FILE *f, struct list_head *stack)
885 struct cYAML_print_info *cpi = NULL;
886 struct cYAML *node = cYAML_ll_pop(stack, &cpi);
888 switch (node->cy_type) {
889 case CYAML_TYPE_FALSE:
890 case CYAML_TYPE_TRUE:
891 case CYAML_TYPE_NULL:
892 print_simple(f, node, cpi);
894 case CYAML_TYPE_STRING:
895 print_string(f, node, cpi);
897 case CYAML_TYPE_NUMBER:
898 print_number(f, node, cpi);
900 case CYAML_TYPE_ARRAY:
901 print_array(f, node, stack, cpi);
903 case CYAML_TYPE_OBJECT:
904 print_object(f, node, stack, cpi);
914 void cYAML_print_tree(struct cYAML *node)
916 struct cYAML_print_info print_info;
917 struct list_head list;
919 INIT_LIST_HEAD(&list);
924 memset(&print_info, 0, sizeof(struct cYAML_print_info));
926 if (cYAML_ll_push(node, &print_info, &list) == 0)
927 print_value(stdout, &list);
930 void cYAML_print_tree2file(FILE *f, struct cYAML *node)
932 struct cYAML_print_info print_info;
933 struct list_head list;
935 INIT_LIST_HEAD(&list);
940 memset(&print_info, 0, sizeof(struct cYAML_print_info));
942 if (cYAML_ll_push(node, &print_info, &list) == 0)
943 print_value(f, &list);
946 static struct cYAML *insert_item(struct cYAML *parent, char *key,
947 enum cYAML_object_type type)
949 struct cYAML *node = calloc(1, sizeof(*node));
955 node->cy_string = strdup(key);
957 node->cy_type = type;
959 cYAML_insert_child(parent, node);
964 struct cYAML *cYAML_create_seq(struct cYAML *parent, char *key)
966 return insert_item(parent, key, CYAML_TYPE_ARRAY);
969 struct cYAML *cYAML_create_seq_item(struct cYAML *seq)
971 return insert_item(seq, NULL, CYAML_TYPE_OBJECT);
974 struct cYAML *cYAML_create_object(struct cYAML *parent, char *key)
976 return insert_item(parent, key, CYAML_TYPE_OBJECT);
979 struct cYAML *cYAML_create_string(struct cYAML *parent, char *key, char *value)
981 struct cYAML *node = calloc(1, sizeof(*node));
985 node->cy_string = strdup(key);
986 node->cy_valuestring = strdup(value);
987 node->cy_type = CYAML_TYPE_STRING;
989 cYAML_insert_child(parent, node);
994 struct cYAML *cYAML_create_number(struct cYAML *parent, char *key, double value)
996 struct cYAML *node = calloc(1, sizeof(*node));
1000 node->cy_string = strdup(key);
1001 node->cy_valuedouble = value;
1002 node->cy_valueint = (int)value;
1003 node->cy_type = CYAML_TYPE_NUMBER;
1005 cYAML_insert_child(parent, node);
1010 void cYAML_insert_child(struct cYAML *parent, struct cYAML *node)
1014 if (parent && node) {
1015 if (parent->cy_child == NULL) {
1016 parent->cy_child = node;
1020 cur = parent->cy_child;
1022 while (cur->cy_next)
1025 cur->cy_next = node;
1026 node->cy_prev = cur;
1030 void cYAML_insert_sibling(struct cYAML *root, struct cYAML *sibling)
1032 struct cYAML *last = NULL;
1033 if (root == NULL || sibling == NULL)
1037 while (last->cy_next != NULL)
1038 last = last->cy_next;
1040 last->cy_next = sibling;
1043 void cYAML_build_error(int rc, int seq_no, char *cmd,
1044 char *entity, char *err_str,
1045 struct cYAML **root)
1047 struct cYAML *r = NULL, *err, *s, *itm, *cmd_obj;
1051 /* add to the tail of the root that's passed in */
1052 if ((*root) == NULL) {
1053 *root = cYAML_create_object(NULL, NULL);
1054 if ((*root) == NULL)
1060 /* look for the command */
1061 cmd_obj = cYAML_get_object_item(r, (const char *)cmd);
1062 if (cmd_obj != NULL && cmd_obj->cy_type == CYAML_TYPE_ARRAY)
1063 itm = cYAML_create_seq_item(cmd_obj);
1064 else if (cmd_obj == NULL) {
1065 s = cYAML_create_seq(r, cmd);
1066 itm = cYAML_create_seq_item(s);
1067 } else if (cmd_obj != NULL && cmd_obj->cy_type != CYAML_TYPE_ARRAY)
1070 err = cYAML_create_object(itm, entity);
1075 cYAML_create_number(err, "seq_no", seq_no) == NULL)
1078 if (cYAML_create_number(err, "errno", rc) == NULL)
1081 if (cYAML_create_string(err, "descr", err_str) == NULL)
1091 struct cYAML *cYAML_build_tree(char *yaml_file,
1092 const char *yaml_blk,
1093 size_t yaml_blk_size,
1094 struct cYAML **err_rc)
1096 yaml_parser_t parser;
1098 struct cYAML_tree_node tree;
1099 enum cYAML_handler_error rc;
1100 yaml_token_type_t token_type;
1105 memset(&tree, 0, sizeof(struct cYAML_tree_node));
1107 INIT_LIST_HEAD(&tree.ll);
1109 /* Create the Parser object. */
1110 yaml_parser_initialize(&parser);
1112 /* file alwyas takes precedence */
1113 if (yaml_file != NULL) {
1114 /* Set a file input. */
1115 input = fopen(yaml_file, "rb");
1116 if (input == NULL) {
1117 snprintf(err_str, sizeof(err_str),
1118 "Failed to open file: %s", yaml_file);
1119 cYAML_build_error(-1, -1, "yaml", "builder",
1125 yaml_parser_set_input_file(&parser, input);
1126 } else if (yaml_blk != NULL) {
1127 yaml_parser_set_input_string(&parser,
1128 (const unsigned char *) yaml_blk,
1131 /* assume that we're getting our input froms stdin */
1132 yaml_parser_set_input_file(&parser, stdin);
1134 /* Read the event sequence. */
1137 * Go through the parser and build a cYAML representation
1138 * of the passed in YAML text
1140 yaml_parser_scan(&parser, &token);
1142 rc = dispatch_tbl[token.type](&token, &tree);
1143 if (rc != CYAML_ERROR_NONE) {
1144 snprintf(err_str, sizeof(err_str),
1145 "Failed to handle token:%d "
1146 "[state=%d, rc=%d]",
1147 token.type, tree.state, rc);
1148 cYAML_build_error(-1, -1, "yaml", "builder",
1152 /* Are we finished? */
1153 done = (rc != CYAML_ERROR_NONE ||
1154 token.type == YAML_STREAM_END_TOKEN ||
1155 token.type == YAML_NO_TOKEN);
1157 token_type = token.type;
1159 yaml_token_delete(&token);
1162 /* Destroy the Parser object. */
1163 yaml_parser_delete(&parser);
1168 if (token_type == YAML_STREAM_END_TOKEN &&
1169 rc == CYAML_ERROR_NONE)
1172 cYAML_free_tree(tree.root);