Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file printer/tree.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief TREE printer for libyang data model structure |
| 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in |
| 15 | * the documentation and/or other materials provided with the |
| 16 | * distribution. |
| 17 | * 3. Neither the name of the Company nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this |
| 19 | * software without specific prior written permission. |
| 20 | */ |
| 21 | |
| 22 | #include <stdlib.h> |
| 23 | #include <stdio.h> |
| 24 | #include <string.h> |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 25 | #include <assert.h> |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 26 | |
| 27 | #include "../common.h" |
| 28 | #include "../tree.h" |
| 29 | |
Michal Vasko | e03bfbb | 2015-06-16 09:07:49 +0200 | [diff] [blame] | 30 | /* spec_config = 0 (no special config status), 1 (read-only - rpc output, notification), 2 (write-only - rpc input) */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 31 | static void tree_print_mnode_choice(FILE * f, int level, char *indent, unsigned int max_name_len, |
| 32 | struct ly_mnode *mnode, int mask, int spec_config); |
| 33 | static void tree_print_mnode(FILE * f, int level, char *indent, unsigned int max_name_len, struct ly_mnode *mnode, |
Michal Vasko | e03bfbb | 2015-06-16 09:07:49 +0200 | [diff] [blame] | 34 | int mask, int spec_config); |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 35 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 36 | static int |
| 37 | sibling_is_valid_child(const struct ly_mnode *mnode) |
Michal Vasko | 6db4fce | 2015-06-08 14:13:49 +0200 | [diff] [blame] | 38 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 39 | struct ly_mnode *cur; |
Michal Vasko | 7ea0a31 | 2015-06-08 10:36:48 +0200 | [diff] [blame] | 40 | |
Michal Vasko | 07898f9 | 2015-06-15 12:17:11 +0200 | [diff] [blame] | 41 | if (mnode == NULL) { |
| 42 | return 0; |
| 43 | } |
Michal Vasko | 7ea0a31 | 2015-06-08 10:36:48 +0200 | [diff] [blame] | 44 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 45 | /* has a following printed child */ |
| 46 | LY_TREE_FOR((struct ly_mnode *)mnode->next, cur) { |
| 47 | if (cur->nodetype & |
| 48 | (LY_NODE_CONTAINER | LY_NODE_LEAF | LY_NODE_LEAFLIST | LY_NODE_LIST | LY_NODE_ANYXML | LY_NODE_CHOICE | |
| 49 | LY_NODE_RPC | LY_NODE_INPUT | LY_NODE_OUTPUT | LY_NODE_NOTIF)) { |
| 50 | return 1; |
| 51 | } |
| 52 | } |
Michal Vasko | 7ea0a31 | 2015-06-08 10:36:48 +0200 | [diff] [blame] | 53 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 54 | /* if in uses, the following printed child can actually be in the parent node :-/ */ |
Michal Vasko | 07898f9 | 2015-06-15 12:17:11 +0200 | [diff] [blame] | 55 | if (mnode->parent && mnode->parent->nodetype == LY_NODE_USES) { |
| 56 | return sibling_is_valid_child(mnode->parent); |
| 57 | } |
| 58 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 59 | return 0; |
Michal Vasko | 7ea0a31 | 2015-06-08 10:36:48 +0200 | [diff] [blame] | 60 | } |
| 61 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 62 | static char * |
| 63 | create_indent(int level, const char *old_indent, const struct ly_mnode *mnode, int shorthand) |
Michal Vasko | 449afde | 2015-06-04 16:06:49 +0200 | [diff] [blame] | 64 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 65 | int next_is_case = 0, is_case = 0, has_next = 0, i, found; |
| 66 | char *new_indent = malloc((level * 4 + 1) * sizeof (char)); |
Michal Vasko | 1441046 | 2015-06-05 15:08:54 +0200 | [diff] [blame] | 67 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 68 | strcpy(new_indent, old_indent); |
Michal Vasko | 1441046 | 2015-06-05 15:08:54 +0200 | [diff] [blame] | 69 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 70 | /* this is the indent of a case (standard or shorthand) */ |
| 71 | if (mnode->nodetype == LY_NODE_CASE || shorthand) { |
| 72 | is_case = 1; |
| 73 | } |
Michal Vasko | 1441046 | 2015-06-05 15:08:54 +0200 | [diff] [blame] | 74 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 75 | /* this is the direct child of a case */ |
| 76 | if (!is_case && mnode->parent && (mnode->parent->nodetype & (LY_NODE_CASE | LY_NODE_CHOICE))) { |
| 77 | /* it is not the only child */ |
| 78 | if (mnode->next && mnode->next->parent && mnode->next->parent->nodetype == LY_NODE_CHOICE) { |
| 79 | next_is_case = 1; |
| 80 | } |
| 81 | } |
Michal Vasko | 1441046 | 2015-06-05 15:08:54 +0200 | [diff] [blame] | 82 | |
Michal Vasko | 07898f9 | 2015-06-15 12:17:11 +0200 | [diff] [blame] | 83 | /* next is a node that will actually be printed */ |
| 84 | has_next = sibling_is_valid_child(mnode); |
Michal Vasko | 1441046 | 2015-06-05 15:08:54 +0200 | [diff] [blame] | 85 | |
Michal Vasko | 07898f9 | 2015-06-15 12:17:11 +0200 | [diff] [blame] | 86 | /* there is no next, but we are in top-level of a submodule */ |
| 87 | if (!has_next && mnode->module->type == 1 && !mnode->parent) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 88 | struct ly_submodule *submod = (struct ly_submodule *)mnode->module; |
| 89 | struct ly_module *mod = submod->belongsto; |
Michal Vasko | 1441046 | 2015-06-05 15:08:54 +0200 | [diff] [blame] | 90 | |
Michal Vasko | 07898f9 | 2015-06-15 12:17:11 +0200 | [diff] [blame] | 91 | /* find this submodule, check all the next ones for valid printed nodes */ |
| 92 | found = 0; |
| 93 | for (i = 0; i < mod->inc_size; i++) { |
| 94 | /* we found ours, check all the following submodules and the module */ |
| 95 | if (found) { |
Michal Vasko | f289630 | 2015-06-16 09:30:02 +0200 | [diff] [blame] | 96 | if (mnode->nodetype == LY_NODE_RPC) { |
| 97 | has_next = sibling_is_valid_child(mod->inc[i].submodule->rpc); |
| 98 | } else if (mnode->nodetype == LY_NODE_NOTIF) { |
| 99 | has_next = sibling_is_valid_child(mod->inc[i].submodule->notif); |
| 100 | } else { |
| 101 | has_next = sibling_is_valid_child(mod->inc[i].submodule->data); |
Michal Vasko | 07898f9 | 2015-06-15 12:17:11 +0200 | [diff] [blame] | 102 | } |
| 103 | } |
Michal Vasko | 7ea0a31 | 2015-06-08 10:36:48 +0200 | [diff] [blame] | 104 | |
Michal Vasko | 07898f9 | 2015-06-15 12:17:11 +0200 | [diff] [blame] | 105 | if (!found && !strcmp(submod->name, mod->inc[i].submodule->name)) { |
| 106 | found = 1; |
| 107 | } |
| 108 | } |
Michal Vasko | 7ea0a31 | 2015-06-08 10:36:48 +0200 | [diff] [blame] | 109 | |
Michal Vasko | 07898f9 | 2015-06-15 12:17:11 +0200 | [diff] [blame] | 110 | /* there is nothing in submodules, check module */ |
| 111 | if (!has_next) { |
Michal Vasko | f289630 | 2015-06-16 09:30:02 +0200 | [diff] [blame] | 112 | if (mnode->nodetype == LY_NODE_RPC) { |
Michal Vasko | 0e893c9 | 2015-06-16 11:09:40 +0200 | [diff] [blame] | 113 | has_next = sibling_is_valid_child(mod->rpc); |
Michal Vasko | f289630 | 2015-06-16 09:30:02 +0200 | [diff] [blame] | 114 | } else if (mnode->nodetype == LY_NODE_NOTIF) { |
Michal Vasko | 0e893c9 | 2015-06-16 11:09:40 +0200 | [diff] [blame] | 115 | has_next = sibling_is_valid_child(mod->notif); |
Michal Vasko | f289630 | 2015-06-16 09:30:02 +0200 | [diff] [blame] | 116 | } else { |
Michal Vasko | 0e893c9 | 2015-06-16 11:09:40 +0200 | [diff] [blame] | 117 | has_next = sibling_is_valid_child(mod->data); |
Michal Vasko | 07898f9 | 2015-06-15 12:17:11 +0200 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 122 | if (has_next && !next_is_case) { |
| 123 | strcat(new_indent, "| "); |
| 124 | } else { |
| 125 | strcat(new_indent, " "); |
| 126 | } |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 127 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 128 | return new_indent; |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 129 | } |
| 130 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 131 | static unsigned int |
| 132 | get_max_name_len(struct ly_mnode *mnode) |
Michal Vasko | 8d479bd | 2015-06-05 10:50:03 +0200 | [diff] [blame] | 133 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 134 | struct ly_mnode *sub; |
| 135 | unsigned int max_name_len = 0, uses_max_name_len; |
Michal Vasko | 8d479bd | 2015-06-05 10:50:03 +0200 | [diff] [blame] | 136 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 137 | LY_TREE_FOR(mnode, sub) { |
| 138 | if (sub->nodetype == LY_NODE_USES) { |
| 139 | uses_max_name_len = get_max_name_len(sub->child); |
| 140 | if (uses_max_name_len > max_name_len) { |
| 141 | max_name_len = uses_max_name_len; |
| 142 | } |
| 143 | } else if (sub->nodetype & |
| 144 | (LY_NODE_CHOICE | LY_NODE_CONTAINER | LY_NODE_LEAF | LY_NODE_LEAFLIST | LY_NODE_LIST | LY_NODE_ANYXML)) { |
| 145 | if (strlen(sub->name) > max_name_len) { |
| 146 | max_name_len = strlen(sub->name); |
| 147 | } |
| 148 | } |
| 149 | } |
Michal Vasko | 8d479bd | 2015-06-05 10:50:03 +0200 | [diff] [blame] | 150 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 151 | return max_name_len; |
Michal Vasko | 8d479bd | 2015-06-05 10:50:03 +0200 | [diff] [blame] | 152 | } |
| 153 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 154 | static void |
| 155 | tree_print_type(FILE * f, struct ly_type *type) |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 156 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 157 | if (type->base == LY_TYPE_LEAFREF) { |
| 158 | fprintf(f, "-> %s", type->info.lref.path); |
| 159 | } else if (type->prefix) { |
| 160 | fprintf(f, "%s:%s", type->prefix, type->der->name); |
| 161 | } else { |
| 162 | fprintf(f, "%s", type->der->name); |
| 163 | } |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 164 | } |
| 165 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 166 | static void |
Michal Vasko | 28c6b57 | 2015-06-18 12:43:31 +0200 | [diff] [blame] | 167 | tree_print_features(FILE *f, const struct ly_feature **features, uint8_t features_size) |
| 168 | { |
| 169 | int i; |
| 170 | |
| 171 | if (!features_size) { |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | fprintf(f, " {"); |
| 176 | for (i = 0; i < features_size; i++) { |
| 177 | if (i > 0) { |
| 178 | fprintf(f, ","); |
| 179 | } |
| 180 | fprintf(f, "%s", features[i]->name); |
| 181 | } |
| 182 | fprintf(f, "}?"); |
| 183 | } |
| 184 | |
| 185 | static void |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 186 | tree_print_input_output(FILE * f, int level, char *indent, struct ly_mnode *mnode, int spec_config) |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 187 | { |
| 188 | unsigned int max_child_len; |
| 189 | char *new_indent; |
| 190 | struct ly_mnode *sub; |
| 191 | |
Michal Vasko | e03bfbb | 2015-06-16 09:07:49 +0200 | [diff] [blame] | 192 | assert(spec_config); |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 193 | |
Michal Vasko | e03bfbb | 2015-06-16 09:07:49 +0200 | [diff] [blame] | 194 | fprintf(f, "%s+--%s %s\n", indent, (spec_config == 1 ? "-w" : "ro"), (spec_config == 1 ? "input" : "output")); |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 195 | |
| 196 | level++; |
| 197 | new_indent = create_indent(level, indent, mnode, 0); |
| 198 | |
| 199 | max_child_len = get_max_name_len(mnode->child); |
| 200 | |
| 201 | LY_TREE_FOR(mnode->child, sub) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 202 | tree_print_mnode(f, level, new_indent, max_child_len, sub, |
| 203 | LY_NODE_CHOICE | LY_NODE_CONTAINER | LY_NODE_LEAF | LY_NODE_LEAFLIST | LY_NODE_LIST | LY_NODE_ANYXML | LY_NODE_USES, |
| 204 | spec_config); |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | free(new_indent); |
| 208 | } |
| 209 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 210 | static void |
| 211 | tree_print_container(FILE * f, int level, char *indent, struct ly_mnode *mnode, int spec_config) |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 212 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 213 | unsigned int max_child_len; |
| 214 | char *new_indent; |
| 215 | struct ly_mnode_container *cont = (struct ly_mnode_container *)mnode; |
| 216 | struct ly_mnode *sub; |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 217 | |
Michal Vasko | e03bfbb | 2015-06-16 09:07:49 +0200 | [diff] [blame] | 218 | assert(spec_config >= 0 && spec_config <= 2); |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 219 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 220 | fprintf(f, "%s%s--", indent, |
| 221 | (cont->flags & LY_NODE_STATUS_DEPRC ? "x" : (cont->flags & LY_NODE_STATUS_OBSLT ? "o" : "+"))); |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 222 | |
Michal Vasko | e03bfbb | 2015-06-16 09:07:49 +0200 | [diff] [blame] | 223 | if (spec_config == 0) { |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 224 | fprintf(f, "%s ", (cont->flags & LY_NODE_CONFIG_W ? "rw" : "ro")); |
Michal Vasko | e03bfbb | 2015-06-16 09:07:49 +0200 | [diff] [blame] | 225 | } else if (spec_config == 1) { |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 226 | fprintf(f, "-w "); |
Michal Vasko | e03bfbb | 2015-06-16 09:07:49 +0200 | [diff] [blame] | 227 | } else if (spec_config == 2) { |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 228 | fprintf(f, "ro "); |
| 229 | } |
| 230 | |
Michal Vasko | 28c6b57 | 2015-06-18 12:43:31 +0200 | [diff] [blame] | 231 | fprintf(f, "%s%s", cont->name, (cont->presence ? "!" : "")); |
| 232 | |
| 233 | tree_print_features(f, (const struct ly_feature **)cont->features, cont->features_size); |
| 234 | |
| 235 | fprintf(f, "\n"); |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 236 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 237 | level++; |
| 238 | new_indent = create_indent(level, indent, mnode, 0); |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 239 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 240 | max_child_len = get_max_name_len(mnode->child); |
Michal Vasko | 449afde | 2015-06-04 16:06:49 +0200 | [diff] [blame] | 241 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 242 | LY_TREE_FOR(cont->child, sub) { |
| 243 | tree_print_mnode(f, level, new_indent, max_child_len, sub, |
| 244 | LY_NODE_CHOICE | LY_NODE_CONTAINER | LY_NODE_LEAF | LY_NODE_LEAFLIST | LY_NODE_LIST | LY_NODE_ANYXML | LY_NODE_USES, |
| 245 | spec_config); |
| 246 | } |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 247 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 248 | free(new_indent); |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 249 | } |
| 250 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 251 | static void |
| 252 | tree_print_choice(FILE * f, int level, char *indent, struct ly_mnode *mnode, int spec_config) |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 253 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 254 | unsigned int max_child_len; |
| 255 | char *new_indent; |
| 256 | struct ly_mnode_choice *choice = (struct ly_mnode_choice *)mnode; |
| 257 | struct ly_mnode *sub; |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 258 | |
Michal Vasko | e03bfbb | 2015-06-16 09:07:49 +0200 | [diff] [blame] | 259 | assert(spec_config >= 0 && spec_config <= 2); |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 260 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 261 | fprintf(f, "%s%s--", indent, |
| 262 | (choice->flags & LY_NODE_STATUS_DEPRC ? "x" : (choice->flags & LY_NODE_STATUS_OBSLT ? "o" : "+"))); |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 263 | |
Michal Vasko | e03bfbb | 2015-06-16 09:07:49 +0200 | [diff] [blame] | 264 | if (spec_config == 0) { |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 265 | fprintf(f, "%s ", (choice->flags & LY_NODE_CONFIG_W ? "rw" : "ro")); |
Michal Vasko | e03bfbb | 2015-06-16 09:07:49 +0200 | [diff] [blame] | 266 | } else if (spec_config == 1) { |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 267 | fprintf(f, "-w "); |
Michal Vasko | e03bfbb | 2015-06-16 09:07:49 +0200 | [diff] [blame] | 268 | } else if (spec_config == 2) { |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 269 | fprintf(f, "ro "); |
| 270 | } |
| 271 | |
| 272 | fprintf(f, "(%s)%s", choice->name, (choice->flags & LY_NODE_MAND_TRUE ? "" : "?")); |
| 273 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 274 | if (choice->dflt != NULL) { |
| 275 | fprintf(f, " <%s>", choice->dflt->name); |
| 276 | } |
Michal Vasko | 28c6b57 | 2015-06-18 12:43:31 +0200 | [diff] [blame] | 277 | |
| 278 | tree_print_features(f, (const struct ly_feature **)choice->features, choice->features_size); |
| 279 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 280 | fprintf(f, "\n"); |
Michal Vasko | 449afde | 2015-06-04 16:06:49 +0200 | [diff] [blame] | 281 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 282 | level++; |
| 283 | new_indent = create_indent(level, indent, mnode, 0); |
Michal Vasko | 449afde | 2015-06-04 16:06:49 +0200 | [diff] [blame] | 284 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 285 | max_child_len = get_max_name_len(mnode->child); |
Michal Vasko | 449afde | 2015-06-04 16:06:49 +0200 | [diff] [blame] | 286 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 287 | LY_TREE_FOR(choice->child, sub) { |
| 288 | tree_print_mnode_choice(f, level, new_indent, max_child_len, sub, |
| 289 | LY_NODE_CASE | LY_NODE_CONTAINER | LY_NODE_LEAF | LY_NODE_LEAFLIST | LY_NODE_LIST | LY_NODE_ANYXML, |
| 290 | spec_config); |
| 291 | } |
Michal Vasko | 449afde | 2015-06-04 16:06:49 +0200 | [diff] [blame] | 292 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 293 | free(new_indent); |
Michal Vasko | 449afde | 2015-06-04 16:06:49 +0200 | [diff] [blame] | 294 | } |
| 295 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 296 | static void |
| 297 | tree_print_case(FILE * f, int level, char *indent, unsigned int max_name_len, struct ly_mnode *mnode, int shorthand, int spec_config) |
Michal Vasko | 449afde | 2015-06-04 16:06:49 +0200 | [diff] [blame] | 298 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 299 | char *new_indent; |
| 300 | struct ly_mnode_case *cas = (struct ly_mnode_case *)mnode; |
| 301 | struct ly_mnode *sub; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 302 | int i; |
| 303 | |
| 304 | /* stop if features are not enabled */ |
| 305 | for (i = 0; i < cas->features_size; i++) { |
| 306 | if (!(cas->features[i]->flags & LY_NODE_FENABLED)) { |
| 307 | return; |
| 308 | } |
| 309 | } |
| 310 | /* or if the data are from augment under a not enabled feature */ |
| 311 | if (cas->parent && cas->parent->nodetype == LY_NODE_AUGMENT) { |
| 312 | for (i = 0; i < cas->parent->features_size; i++) { |
| 313 | if (!(cas->parent->features[i]->flags & LY_NODE_FENABLED)) { |
| 314 | return; |
| 315 | } |
| 316 | } |
| 317 | } |
Michal Vasko | 449afde | 2015-06-04 16:06:49 +0200 | [diff] [blame] | 318 | |
Michal Vasko | 28c6b57 | 2015-06-18 12:43:31 +0200 | [diff] [blame] | 319 | fprintf(f, "%s%s--:(%s)", indent, |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 320 | (cas->flags & LY_NODE_STATUS_DEPRC ? "x" : (cas->flags & LY_NODE_STATUS_OBSLT ? "o" : "+")), mnode->name); |
Michal Vasko | 449afde | 2015-06-04 16:06:49 +0200 | [diff] [blame] | 321 | |
Michal Vasko | 28c6b57 | 2015-06-18 12:43:31 +0200 | [diff] [blame] | 322 | tree_print_features(f, (const struct ly_feature **)cas->features, cas->features_size); |
| 323 | |
| 324 | fprintf(f, "\n"); |
| 325 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 326 | level++; |
| 327 | new_indent = create_indent(level, indent, mnode, shorthand); |
Michal Vasko | 449afde | 2015-06-04 16:06:49 +0200 | [diff] [blame] | 328 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 329 | if (shorthand) { |
| 330 | tree_print_mnode(f, level, new_indent, max_name_len, mnode, |
| 331 | LY_NODE_CHOICE | LY_NODE_CONTAINER | LY_NODE_LEAF | LY_NODE_LEAFLIST | LY_NODE_LIST | LY_NODE_ANYXML | LY_NODE_USES, |
| 332 | spec_config); |
| 333 | } else { |
| 334 | LY_TREE_FOR(mnode->child, sub) { |
| 335 | tree_print_mnode(f, level, new_indent, max_name_len, sub, |
| 336 | LY_NODE_CHOICE | LY_NODE_CONTAINER | LY_NODE_LEAF | LY_NODE_LEAFLIST | LY_NODE_LIST | LY_NODE_ANYXML | LY_NODE_USES, |
| 337 | spec_config); |
| 338 | } |
| 339 | } |
Michal Vasko | 449afde | 2015-06-04 16:06:49 +0200 | [diff] [blame] | 340 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 341 | free(new_indent); |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 342 | } |
| 343 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 344 | static void |
| 345 | tree_print_anyxml(FILE * f, char *indent, unsigned int max_name_len, struct ly_mnode *mnode, int spec_config) |
Michal Vasko | 315f70b | 2015-06-05 10:48:59 +0200 | [diff] [blame] | 346 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 347 | struct ly_mnode_anyxml *anyxml = (struct ly_mnode_anyxml *)mnode; |
Michal Vasko | 315f70b | 2015-06-05 10:48:59 +0200 | [diff] [blame] | 348 | |
Michal Vasko | e03bfbb | 2015-06-16 09:07:49 +0200 | [diff] [blame] | 349 | assert(spec_config >= 0 && spec_config <= 2); |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 350 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 351 | fprintf(f, "%s%s--", indent, |
| 352 | (anyxml->flags & LY_NODE_STATUS_DEPRC ? "x" : (anyxml->flags & LY_NODE_STATUS_OBSLT ? "o" : "+"))); |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 353 | |
Michal Vasko | e03bfbb | 2015-06-16 09:07:49 +0200 | [diff] [blame] | 354 | if (spec_config == 0) { |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 355 | fprintf(f, "%s ", (anyxml->flags & LY_NODE_CONFIG_W ? "rw" : "ro")); |
Michal Vasko | e03bfbb | 2015-06-16 09:07:49 +0200 | [diff] [blame] | 356 | } else if (spec_config == 1) { |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 357 | fprintf(f, "-w "); |
Michal Vasko | e03bfbb | 2015-06-16 09:07:49 +0200 | [diff] [blame] | 358 | } else if (spec_config == 2) { |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 359 | fprintf(f, "ro "); |
| 360 | } |
| 361 | |
Michal Vasko | 28c6b57 | 2015-06-18 12:43:31 +0200 | [diff] [blame] | 362 | fprintf(f, "%s%s%*sanyxml", anyxml->name, (anyxml->flags & LY_NODE_MAND_TRUE ? " " : "?"), |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 363 | 3 + (int)(max_name_len - strlen(anyxml->name)), " "); |
Michal Vasko | 28c6b57 | 2015-06-18 12:43:31 +0200 | [diff] [blame] | 364 | |
| 365 | tree_print_features(f, (const struct ly_feature **)anyxml->features, anyxml->features_size); |
| 366 | |
| 367 | fprintf(f, "\n"); |
Michal Vasko | 315f70b | 2015-06-05 10:48:59 +0200 | [diff] [blame] | 368 | } |
| 369 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 370 | static void |
| 371 | tree_print_leaf(FILE * f, char *indent, unsigned int max_name_len, struct ly_mnode *mnode, int spec_config) |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 372 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 373 | struct ly_mnode_leaf *leaf = (struct ly_mnode_leaf *)mnode; |
| 374 | struct ly_mnode_list *list; |
| 375 | int i, is_key = 0; |
Michal Vasko | 449afde | 2015-06-04 16:06:49 +0200 | [diff] [blame] | 376 | |
Michal Vasko | e03bfbb | 2015-06-16 09:07:49 +0200 | [diff] [blame] | 377 | assert(spec_config >= 0 && spec_config <= 2); |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 378 | |
Radek Krejci | 87e840c | 2015-06-19 16:44:54 +0200 | [diff] [blame^] | 379 | if (leaf->parent && leaf->parent->nodetype == LY_NODE_LIST) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 380 | list = (struct ly_mnode_list *)leaf->parent; |
| 381 | for (i = 0; i < list->keys_size; i++) { |
| 382 | if (strcmp(list->keys[i]->name, leaf->name) == 0) { |
| 383 | is_key = 1; |
| 384 | break; |
| 385 | } |
| 386 | } |
| 387 | } |
Michal Vasko | 449afde | 2015-06-04 16:06:49 +0200 | [diff] [blame] | 388 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 389 | fprintf(f, "%s%s--", indent, |
| 390 | (leaf->flags & LY_NODE_STATUS_DEPRC ? "x" : (leaf->flags & LY_NODE_STATUS_OBSLT ? "o" : "+"))); |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 391 | |
Michal Vasko | e03bfbb | 2015-06-16 09:07:49 +0200 | [diff] [blame] | 392 | if (spec_config == 0) { |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 393 | fprintf(f, "%s ", (leaf->flags & LY_NODE_CONFIG_W ? "rw" : "ro")); |
Michal Vasko | e03bfbb | 2015-06-16 09:07:49 +0200 | [diff] [blame] | 394 | } else if (spec_config == 1) { |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 395 | fprintf(f, "-w "); |
Michal Vasko | e03bfbb | 2015-06-16 09:07:49 +0200 | [diff] [blame] | 396 | } else if (spec_config == 2) { |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 397 | fprintf(f, "ro "); |
| 398 | } |
| 399 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 400 | fprintf(f, "%s%s%*s", leaf->name, ((leaf->flags & LY_NODE_MAND_TRUE) || is_key ? " " : "?"), |
| 401 | 3 + (int)(max_name_len - strlen(leaf->name)), " "); |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 402 | |
| 403 | tree_print_type(f, &leaf->type); |
| 404 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 405 | if (leaf->dflt != NULL) { |
| 406 | fprintf(f, " <%s>", leaf->dflt); |
| 407 | } |
Michal Vasko | 28c6b57 | 2015-06-18 12:43:31 +0200 | [diff] [blame] | 408 | |
| 409 | tree_print_features(f, (const struct ly_feature **)leaf->features, leaf->features_size); |
| 410 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 411 | fprintf(f, "\n"); |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 412 | } |
| 413 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 414 | static void |
| 415 | tree_print_leaflist(FILE * f, char *indent, unsigned int max_name_len, struct ly_mnode *mnode, int spec_config) |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 416 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 417 | struct ly_mnode_leaflist *leaflist = (struct ly_mnode_leaflist *)mnode; |
Michal Vasko | 449afde | 2015-06-04 16:06:49 +0200 | [diff] [blame] | 418 | |
Michal Vasko | e03bfbb | 2015-06-16 09:07:49 +0200 | [diff] [blame] | 419 | assert(spec_config >= 0 && spec_config <= 2); |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 420 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 421 | fprintf(f, "%s%s--", indent, |
| 422 | (leaflist->flags & LY_NODE_STATUS_DEPRC ? "x" : (leaflist->flags & LY_NODE_STATUS_OBSLT ? "o" : "+"))); |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 423 | |
Michal Vasko | e03bfbb | 2015-06-16 09:07:49 +0200 | [diff] [blame] | 424 | if (spec_config == 0) { |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 425 | fprintf(f, "%s ", (leaflist->flags & LY_NODE_CONFIG_W ? "rw" : "ro")); |
Michal Vasko | e03bfbb | 2015-06-16 09:07:49 +0200 | [diff] [blame] | 426 | } else if (spec_config == 1) { |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 427 | fprintf(f, "-w "); |
Michal Vasko | e03bfbb | 2015-06-16 09:07:49 +0200 | [diff] [blame] | 428 | } else if (spec_config == 2) { |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 429 | fprintf(f, "ro "); |
| 430 | } |
| 431 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 432 | fprintf(f, "%s*%*s", leaflist->name, 3 + (int)(max_name_len - strlen(leaflist->name)), " "); |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 433 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 434 | tree_print_type(f, &leaflist->type); |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 435 | |
Michal Vasko | 28c6b57 | 2015-06-18 12:43:31 +0200 | [diff] [blame] | 436 | tree_print_features(f, (const struct ly_feature **)leaflist->features, leaflist->features_size); |
| 437 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 438 | fprintf(f, "\n"); |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 439 | } |
| 440 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 441 | static void |
| 442 | tree_print_list(FILE * f, int level, char *indent, struct ly_mnode *mnode, int spec_config) |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 443 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 444 | int i; |
| 445 | unsigned int max_child_len; |
| 446 | char *new_indent; |
| 447 | struct ly_mnode *sub; |
| 448 | struct ly_mnode_list *list = (struct ly_mnode_list *)mnode; |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 449 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 450 | fprintf(f, "%s%s--", indent, |
| 451 | (list->flags & LY_NODE_STATUS_DEPRC ? "x" : (list->flags & LY_NODE_STATUS_OBSLT ? "o" : "+"))); |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 452 | |
Michal Vasko | e03bfbb | 2015-06-16 09:07:49 +0200 | [diff] [blame] | 453 | if (spec_config == 0) { |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 454 | fprintf(f, "%s ", (list->flags & LY_NODE_CONFIG_W ? "rw" : "ro")); |
Michal Vasko | e03bfbb | 2015-06-16 09:07:49 +0200 | [diff] [blame] | 455 | } else if (spec_config == 1) { |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 456 | fprintf(f, "-w "); |
Michal Vasko | e03bfbb | 2015-06-16 09:07:49 +0200 | [diff] [blame] | 457 | } else if (spec_config == 2) { |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 458 | fprintf(f, "ro "); |
| 459 | } |
| 460 | |
| 461 | fprintf(f, "%s*", list->name); |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 462 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 463 | for (i = 0; i < list->keys_size; i++) { |
| 464 | if (i == 0) { |
| 465 | fprintf(f, " ["); |
| 466 | } |
| 467 | fprintf(f, "%s%s", list->keys[i]->name, i + 1 < list->keys_size ? "," : "]"); |
| 468 | } |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 469 | |
Michal Vasko | 28c6b57 | 2015-06-18 12:43:31 +0200 | [diff] [blame] | 470 | tree_print_features(f, (const struct ly_feature **)list->features, list->features_size); |
| 471 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 472 | fprintf(f, "\n"); |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 473 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 474 | level++; |
| 475 | new_indent = create_indent(level, indent, mnode, 0); |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 476 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 477 | max_child_len = get_max_name_len(mnode->child); |
Michal Vasko | 449afde | 2015-06-04 16:06:49 +0200 | [diff] [blame] | 478 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 479 | LY_TREE_FOR(mnode->child, sub) { |
| 480 | tree_print_mnode(f, level, new_indent, max_child_len, sub, |
| 481 | LY_NODE_CHOICE | LY_NODE_CONTAINER | LY_NODE_LEAF | LY_NODE_LEAFLIST | LY_NODE_LIST | LY_NODE_USES | LY_NODE_ANYXML, |
| 482 | spec_config); |
| 483 | } |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 484 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 485 | free(new_indent); |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 486 | } |
| 487 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 488 | static void |
| 489 | tree_print_uses(FILE * f, int level, char *indent, unsigned int max_name_len, struct ly_mnode *mnode, int spec_config) |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 490 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 491 | struct ly_mnode *node; |
| 492 | struct ly_mnode_uses *uses = (struct ly_mnode_uses *)mnode; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 493 | |
| 494 | /* FIXME there are no children here */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 495 | LY_TREE_FOR(uses->child, node) { |
| 496 | tree_print_mnode(f, level, indent, max_name_len, node, |
| 497 | LY_NODE_CHOICE | LY_NODE_CONTAINER | LY_NODE_LEAF | LY_NODE_LEAFLIST | LY_NODE_LIST | LY_NODE_USES | LY_NODE_ANYXML, |
| 498 | spec_config); |
| 499 | } |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 500 | } |
| 501 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 502 | static void |
| 503 | tree_print_rpc(FILE * f, int level, char *indent, struct ly_mnode *mnode) |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 504 | { |
| 505 | char *new_indent; |
| 506 | struct ly_mnode *node; |
| 507 | struct ly_mnode_rpc *rpc = (struct ly_mnode_rpc *)mnode; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 508 | int i; |
| 509 | |
| 510 | /* stop if features are not enabled */ |
| 511 | for (i = 0; i < rpc->features_size; i++) { |
| 512 | if (!(rpc->features[i]->flags & LY_NODE_FENABLED)) { |
| 513 | return; |
| 514 | } |
| 515 | } |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 516 | |
Michal Vasko | 28c6b57 | 2015-06-18 12:43:31 +0200 | [diff] [blame] | 517 | fprintf(f, "%s%s---x %s", indent, |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 518 | (rpc->flags & LY_NODE_STATUS_DEPRC ? "x" : (rpc->flags & LY_NODE_STATUS_OBSLT ? "o" : "+")), rpc->name); |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 519 | |
Michal Vasko | 28c6b57 | 2015-06-18 12:43:31 +0200 | [diff] [blame] | 520 | tree_print_features(f, (const struct ly_feature **)rpc->features, rpc->features_size); |
| 521 | |
| 522 | fprintf(f, "\n"); |
| 523 | |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 524 | level++; |
| 525 | new_indent = create_indent(level, indent, mnode, 0); |
| 526 | |
| 527 | LY_TREE_FOR(rpc->child, node) { |
| 528 | if (node->nodetype == LY_NODE_INPUT) { |
| 529 | tree_print_input_output(f, level, new_indent, node, 1); |
| 530 | } else if (node->nodetype == LY_NODE_OUTPUT) { |
| 531 | tree_print_input_output(f, level, new_indent, node, 2); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | free(new_indent); |
| 536 | } |
| 537 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 538 | static void |
| 539 | tree_print_notif(FILE * f, int level, char *indent, struct ly_mnode *mnode) |
Michal Vasko | e03bfbb | 2015-06-16 09:07:49 +0200 | [diff] [blame] | 540 | { |
| 541 | unsigned int max_child_len; |
| 542 | char *new_indent; |
| 543 | struct ly_mnode *node; |
| 544 | struct ly_mnode_notif *notif = (struct ly_mnode_notif *)mnode; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 545 | int i; |
| 546 | |
| 547 | /* stop if features are not enabled */ |
| 548 | for (i = 0; i < notif->features_size; i++) { |
| 549 | if (!(notif->features[i]->flags & LY_NODE_FENABLED)) { |
| 550 | return; |
| 551 | } |
| 552 | } |
Michal Vasko | e03bfbb | 2015-06-16 09:07:49 +0200 | [diff] [blame] | 553 | |
Michal Vasko | 28c6b57 | 2015-06-18 12:43:31 +0200 | [diff] [blame] | 554 | fprintf(f, "%s%s---n %s", indent, |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 555 | (notif->flags & LY_NODE_STATUS_DEPRC ? "x" : (notif->flags & LY_NODE_STATUS_OBSLT ? "o" : "+")), |
Michal Vasko | e03bfbb | 2015-06-16 09:07:49 +0200 | [diff] [blame] | 556 | notif->name); |
| 557 | |
Michal Vasko | 28c6b57 | 2015-06-18 12:43:31 +0200 | [diff] [blame] | 558 | tree_print_features(f, (const struct ly_feature **)notif->features, notif->features_size); |
| 559 | |
| 560 | fprintf(f, "\n"); |
| 561 | |
Michal Vasko | e03bfbb | 2015-06-16 09:07:49 +0200 | [diff] [blame] | 562 | level++; |
| 563 | new_indent = create_indent(level, indent, mnode, 0); |
| 564 | |
| 565 | max_child_len = get_max_name_len(mnode->child); |
| 566 | |
| 567 | LY_TREE_FOR(notif->child, node) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 568 | tree_print_mnode(f, level, new_indent, max_child_len, node, |
| 569 | LY_NODE_CHOICE | LY_NODE_CONTAINER | LY_NODE_LEAF | LY_NODE_LEAFLIST | LY_NODE_LIST | LY_NODE_ANYXML | LY_NODE_USES, |
| 570 | 2); |
Michal Vasko | e03bfbb | 2015-06-16 09:07:49 +0200 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | free(new_indent); |
| 574 | } |
| 575 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 576 | static void |
| 577 | tree_print_mnode_choice(FILE * f, int level, char *indent, unsigned int max_name_len, struct ly_mnode *mnode, int mask, |
| 578 | int spec_config) |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 579 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 580 | if (mnode->nodetype & mask) { |
| 581 | if (mnode->nodetype == LY_NODE_CASE) { |
| 582 | tree_print_case(f, level, indent, max_name_len, mnode, 0, spec_config); |
| 583 | } else { |
| 584 | tree_print_case(f, level, indent, max_name_len, mnode, 1, spec_config); |
| 585 | } |
| 586 | } |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 587 | } |
| 588 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 589 | static void |
| 590 | tree_print_mnode(FILE * f, int level, char *indent, unsigned int max_name_len, struct ly_mnode *mnode, int mask, |
| 591 | int spec_config) |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 592 | { |
Radek Krejci | 87e840c | 2015-06-19 16:44:54 +0200 | [diff] [blame^] | 593 | int i; |
| 594 | |
| 595 | /* stop if features are not enabled */ |
| 596 | for (i = 0; i < mnode->features_size; i++) { |
| 597 | if (!(mnode->features[i]->flags & LY_NODE_FENABLED)) { |
| 598 | return; |
| 599 | } |
| 600 | } |
| 601 | /* or if the data are from augment under a not enabled feature */ |
| 602 | if (mnode->parent && mnode->parent->nodetype == LY_NODE_AUGMENT) { |
| 603 | for (i = 0; i < mnode->parent->features_size; i++) { |
| 604 | if (!(mnode->parent->features[i]->flags & LY_NODE_FENABLED)) { |
| 605 | return; |
| 606 | } |
| 607 | } |
| 608 | } |
| 609 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 610 | switch (mnode->nodetype & mask) { |
| 611 | case LY_NODE_CONTAINER: |
| 612 | tree_print_container(f, level, indent, mnode, spec_config); |
| 613 | break; |
| 614 | case LY_NODE_CHOICE: |
| 615 | tree_print_choice(f, level, indent, mnode, spec_config); |
| 616 | break; |
| 617 | case LY_NODE_LEAF: |
| 618 | tree_print_leaf(f, indent, max_name_len, mnode, spec_config); |
| 619 | break; |
| 620 | case LY_NODE_LEAFLIST: |
| 621 | tree_print_leaflist(f, indent, max_name_len, mnode, spec_config); |
| 622 | break; |
| 623 | case LY_NODE_LIST: |
| 624 | tree_print_list(f, level, indent, mnode, spec_config); |
| 625 | break; |
| 626 | case LY_NODE_ANYXML: |
| 627 | tree_print_anyxml(f, indent, max_name_len, mnode, spec_config); |
| 628 | break; |
| 629 | case LY_NODE_USES: |
| 630 | tree_print_uses(f, level, indent, max_name_len, mnode, spec_config); |
| 631 | break; |
| 632 | default: |
| 633 | break; |
| 634 | } |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 635 | } |
| 636 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 637 | int |
| 638 | tree_print_model(FILE * f, struct ly_module *module) |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 639 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 640 | struct ly_mnode *mnode; |
| 641 | unsigned int max_child_len; |
| 642 | int level = 1, i, have_rpcs = 0, have_notifs = 0; |
| 643 | char *indent = malloc((level * 4 + 1) * sizeof (char)); |
| 644 | strcpy(indent, " "); |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 645 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 646 | fprintf(f, "module: %s\n", module->name); |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 647 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 648 | /* included submodules */ |
| 649 | for (i = 0; i < module->inc_size; i++) { |
| 650 | max_child_len = get_max_name_len(module->inc[i].submodule->data); |
Michal Vasko | 8d479bd | 2015-06-05 10:50:03 +0200 | [diff] [blame] | 651 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 652 | LY_TREE_FOR(module->inc[i].submodule->data, mnode) { |
| 653 | tree_print_mnode(f, level, indent, max_child_len, mnode, |
| 654 | LY_NODE_CHOICE | LY_NODE_CONTAINER | LY_NODE_LEAF | LY_NODE_LEAFLIST | LY_NODE_LIST | LY_NODE_ANYXML | LY_NODE_USES, |
| 655 | 0); |
| 656 | } |
| 657 | } |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 658 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 659 | /* module */ |
| 660 | max_child_len = get_max_name_len(module->data); |
| 661 | level++; |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 662 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 663 | LY_TREE_FOR(module->data, mnode) { |
| 664 | tree_print_mnode(f, level, indent, max_child_len, mnode, |
| 665 | LY_NODE_CHOICE | LY_NODE_CONTAINER | LY_NODE_LEAF | LY_NODE_LEAFLIST | LY_NODE_LIST | LY_NODE_ANYXML | LY_NODE_USES, |
| 666 | 0); |
| 667 | } |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 668 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 669 | /* rpc */ |
| 670 | if (module->rpc) { |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 671 | have_rpcs = 1; |
| 672 | } else { |
| 673 | for (i = 0; i < module->inc_size; i++) { |
| 674 | if (module->inc[i].submodule->rpc) { |
| 675 | have_rpcs = 1; |
| 676 | break; |
| 677 | } |
| 678 | } |
| 679 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 680 | if (have_rpcs) { |
Michal Vasko | b5c75d7 | 2015-06-15 12:16:52 +0200 | [diff] [blame] | 681 | fprintf(f, "rpcs:\n"); |
| 682 | for (i = 0; i < module->inc_size; i++) { |
| 683 | LY_TREE_FOR(module->inc[i].submodule->rpc, mnode) { |
| 684 | tree_print_rpc(f, level, indent, mnode); |
| 685 | } |
| 686 | } |
| 687 | LY_TREE_FOR(module->rpc, mnode) { |
| 688 | tree_print_rpc(f, level, indent, mnode); |
| 689 | } |
| 690 | } |
Michal Vasko | 449afde | 2015-06-04 16:06:49 +0200 | [diff] [blame] | 691 | |
Michal Vasko | e03bfbb | 2015-06-16 09:07:49 +0200 | [diff] [blame] | 692 | /* notification */ |
| 693 | if (module->notif) { |
| 694 | have_notifs = 1; |
| 695 | } else { |
| 696 | for (i = 0; i < module->inc_size; i++) { |
| 697 | if (module->inc[i].submodule->notif) { |
| 698 | have_notifs = 1; |
| 699 | break; |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | if (have_notifs) { |
| 704 | fprintf(f, "notifications:\n"); |
| 705 | for (i = 0; i < module->inc_size; i++) { |
| 706 | LY_TREE_FOR(module->inc[i].submodule->notif, mnode) { |
| 707 | tree_print_notif(f, level, indent, mnode); |
| 708 | } |
| 709 | } |
| 710 | LY_TREE_FOR(module->notif, mnode) { |
| 711 | tree_print_notif(f, level, indent, mnode); |
| 712 | } |
| 713 | } |
Michal Vasko | 449afde | 2015-06-04 16:06:49 +0200 | [diff] [blame] | 714 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 715 | free(indent); |
| 716 | return EXIT_SUCCESS; |
Michal Vasko | 5ed10f1 | 2015-06-04 10:04:57 +0200 | [diff] [blame] | 717 | } |