Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1 | /** |
Radek Krejci | efdd0ce | 2015-05-26 16:48:29 +0200 | [diff] [blame] | 2 | * @file printer/yang.c |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Radek Krejci | efdd0ce | 2015-05-26 16:48:29 +0200 | [diff] [blame] | 4 | * @brief YANG printer for libyang data model structure |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 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> |
| 25 | |
Radek Krejci | efdd0ce | 2015-05-26 16:48:29 +0200 | [diff] [blame] | 26 | #include "../common.h" |
| 27 | #include "../tree.h" |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 28 | |
| 29 | #define INDENT "" |
| 30 | #define LEVEL (level*2) |
| 31 | |
Michal Vasko | 5bbae10 | 2015-06-16 12:16:44 +0200 | [diff] [blame] | 32 | static void yang_print_mnode(FILE *f, int level, struct ly_mnode *mnode, int mask); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 33 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 34 | static void |
Michal Vasko | 5bbae10 | 2015-06-16 12:16:44 +0200 | [diff] [blame] | 35 | yang_print_text(FILE *f, int level, const char *name, const char *text) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 36 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 37 | const char *s, *t; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 38 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 39 | fprintf(f, "%*s%s\n", LEVEL, INDENT, name); |
| 40 | level++; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 41 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 42 | fprintf(f, "%*s\"", LEVEL, INDENT); |
| 43 | t = text; |
| 44 | while ((s = strchr(t, '\n'))) { |
Michal Vasko | 5bbae10 | 2015-06-16 12:16:44 +0200 | [diff] [blame] | 45 | fwrite(t, sizeof *t, (s - t) + 1, f); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 46 | t = s + 1; |
| 47 | fprintf(f, "%*s", LEVEL, INDENT); |
| 48 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 49 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 50 | fprintf(f, "%s\";\n\n", t); |
| 51 | level--; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 52 | |
| 53 | } |
| 54 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 55 | static void |
Radek Krejci | 6a11385 | 2015-07-03 16:04:20 +0200 | [diff] [blame] | 56 | yang_print_nacmext(FILE *f, int level, struct ly_mnode *mnode) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 57 | { |
Radek Krejci | 6764bb3 | 2015-07-03 15:16:04 +0200 | [diff] [blame] | 58 | int i, j; |
Radek Krejci | 86909d2 | 2015-07-03 16:11:25 +0200 | [diff] [blame] | 59 | const char *prefix = NULL; |
Radek Krejci | 6764bb3 | 2015-07-03 15:16:04 +0200 | [diff] [blame] | 60 | |
| 61 | if (mnode->nacm && (!mnode->parent || mnode->parent->nacm != mnode->nacm)) { |
| 62 | /* locate ietf-netconf-acm module in imports */ |
| 63 | if (!strcmp(mnode->module->name, "ietf-netconf-acm")) { |
| 64 | prefix = mnode->module->prefix; |
| 65 | } else { |
| 66 | /* search in imports */ |
| 67 | for (i = 0; i < mnode->module->imp_size; i++) { |
| 68 | if (!strcmp(mnode->module->imp[i].module->name, "ietf-netconf-acm")) { |
| 69 | prefix = mnode->module->imp[i].prefix; |
| 70 | break; |
| 71 | } |
| 72 | } |
| 73 | /* and in imports of includes */ |
| 74 | if (!prefix) { |
| 75 | for (j = 0; j < mnode->module->inc_size; j++) { |
| 76 | for (i = 0; i < mnode->module->inc[j].submodule->imp_size; i++) { |
| 77 | if (!strcmp(mnode->module->inc[j].submodule->imp[i].module->name, "ietf-netconf-acm")) { |
| 78 | prefix = mnode->module->inc[j].submodule->imp[i].prefix; |
| 79 | break; |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | if ((mnode->nacm & LY_NACM_DENYW) && (!mnode->parent || !(mnode->parent->nacm & LY_NACM_DENYW))) { |
| 87 | fprintf(f, "%*s%s:default-deny-write;\n", LEVEL, INDENT, prefix); |
| 88 | } |
| 89 | if ((mnode->nacm & LY_NACM_DENYA) && (!mnode->parent || !(mnode->parent->nacm & LY_NACM_DENYA))) { |
| 90 | fprintf(f, "%*s%s:default-deny-all;\n", LEVEL, INDENT, prefix); |
| 91 | } |
| 92 | } |
Radek Krejci | 6a11385 | 2015-07-03 16:04:20 +0200 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /* |
| 96 | * Covers: |
| 97 | * description, reference, status |
| 98 | */ |
| 99 | static void |
| 100 | yang_print_mnode_common(FILE *f, int level, struct ly_mnode *mnode) |
| 101 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 102 | if (mnode->flags & LY_NODE_STATUS_CURR) { |
| 103 | fprintf(f, "%*sstatus \"current\";\n", LEVEL, INDENT); |
| 104 | } else if (mnode->flags & LY_NODE_STATUS_DEPRC) { |
| 105 | fprintf(f, "%*sstatus \"deprecated\";\n", LEVEL, INDENT); |
| 106 | } else if (mnode->flags & LY_NODE_STATUS_OBSLT) { |
| 107 | fprintf(f, "%*sstatus \"obsolete\";\n", LEVEL, INDENT); |
| 108 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 109 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 110 | if (mnode->dsc) { |
| 111 | yang_print_text(f, level, "description", mnode->dsc); |
| 112 | } |
| 113 | if (mnode->ref) { |
| 114 | yang_print_text(f, level, "reference", mnode->ref); |
| 115 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /* |
| 119 | * Covers: |
Michal Vasko | 2b8faed | 2015-06-09 12:51:20 +0200 | [diff] [blame] | 120 | * config, mandatory |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 121 | * description, reference, status |
| 122 | */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 123 | static void |
Michal Vasko | 5bbae10 | 2015-06-16 12:16:44 +0200 | [diff] [blame] | 124 | yang_print_mnode_common2(FILE *f, int level, struct ly_mnode *mnode) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 125 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 126 | if (!mnode->parent || (mnode->parent->flags & LY_NODE_CONFIG_MASK) != (mnode->flags & LY_NODE_CONFIG_MASK)) { |
| 127 | /* print config only when it differs from the parent or in root */ |
| 128 | if (mnode->flags & LY_NODE_CONFIG_W) { |
| 129 | fprintf(f, "%*sconfig \"true\";\n", LEVEL, INDENT); |
| 130 | } else if (mnode->flags & LY_NODE_CONFIG_R) { |
| 131 | fprintf(f, "%*sconfig \"false\";\n", LEVEL, INDENT); |
| 132 | } |
| 133 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 134 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 135 | if (mnode->flags & LY_NODE_MAND_TRUE) { |
| 136 | fprintf(f, "%*smandatory \"true\";\n", LEVEL, INDENT); |
| 137 | } else if (mnode->flags & LY_NODE_MAND_FALSE) { |
| 138 | fprintf(f, "%*smandatory \"false\";\n", LEVEL, INDENT); |
| 139 | } |
Michal Vasko | 2b8faed | 2015-06-09 12:51:20 +0200 | [diff] [blame] | 140 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 141 | yang_print_mnode_common(f, level, mnode); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 142 | } |
| 143 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 144 | static void |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 145 | yang_print_restr(FILE *f, int level, struct ly_restr *restr) |
Radek Krejci | 41726f9 | 2015-06-19 13:11:05 +0200 | [diff] [blame] | 146 | { |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 147 | if (restr->dsc != NULL) { |
| 148 | yang_print_text(f, level, "description", restr->dsc); |
Radek Krejci | 41726f9 | 2015-06-19 13:11:05 +0200 | [diff] [blame] | 149 | } |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 150 | if (restr->ref != NULL) { |
| 151 | yang_print_text(f, level, "reference", restr->ref); |
Radek Krejci | 41726f9 | 2015-06-19 13:11:05 +0200 | [diff] [blame] | 152 | } |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 153 | if (restr->eapptag != NULL) { |
| 154 | fprintf(f, "%*serror-app-tag \"%s\";\n", LEVEL, INDENT, restr->eapptag); |
Radek Krejci | 41726f9 | 2015-06-19 13:11:05 +0200 | [diff] [blame] | 155 | } |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 156 | if (restr->emsg != NULL) { |
| 157 | yang_print_text(f, level, "error-message", restr->emsg); |
Radek Krejci | 41726f9 | 2015-06-19 13:11:05 +0200 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
| 161 | static void |
Michal Vasko | 5bbae10 | 2015-06-16 12:16:44 +0200 | [diff] [blame] | 162 | yang_print_type(FILE *f, int level, struct ly_module *module, struct ly_type *type) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 163 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 164 | int i; |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 165 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 166 | if (type->prefix) { |
| 167 | fprintf(f, "%*stype %s:%s {\n", LEVEL, INDENT, type->prefix, type->der->name); |
| 168 | } else { |
| 169 | fprintf(f, "%*stype %s {\n", LEVEL, INDENT, type->der->name); |
| 170 | } |
| 171 | level++; |
| 172 | switch (type->base) { |
| 173 | case LY_TYPE_BINARY: |
| 174 | if (type->info.binary.length != NULL) { |
Radek Krejci | 41726f9 | 2015-06-19 13:11:05 +0200 | [diff] [blame] | 175 | fprintf(f, "%*slength \"%s\" {\n", LEVEL, INDENT, type->info.binary.length->expr); |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 176 | yang_print_restr(f, level + 1, type->info.binary.length); |
Radek Krejci | 41726f9 | 2015-06-19 13:11:05 +0200 | [diff] [blame] | 177 | fprintf(f, "%*s}\n", LEVEL, INDENT); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 178 | } |
| 179 | break; |
| 180 | case LY_TYPE_BITS: |
| 181 | for (i = 0; i < type->info.bits.count; ++i) { |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 182 | fprintf(f, "%*sbit %s {\n", LEVEL, INDENT, type->info.bits.bit[i].name); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 183 | level++; |
| 184 | yang_print_mnode_common(f, level, (struct ly_mnode *)&type->info.bits.bit[i]); |
| 185 | fprintf(f, "%*sposition %u;\n", LEVEL, INDENT, type->info.bits.bit[i].pos); |
| 186 | level--; |
| 187 | fprintf(f, "%*s}\n", LEVEL, INDENT); |
| 188 | } |
| 189 | break; |
| 190 | case LY_TYPE_DEC64: |
| 191 | fprintf(f, "%*sfraction-digits %d;\n", LEVEL, INDENT, type->info.dec64.dig); |
| 192 | /* TODO range incomplete */ |
| 193 | break; |
| 194 | case LY_TYPE_ENUM: |
| 195 | for (i = 0; i < type->info.enums.count; i++) { |
| 196 | fprintf(f, "%*senum %s {\n", LEVEL, INDENT, type->info.enums.list[i].name); |
| 197 | level++; |
| 198 | yang_print_mnode_common(f, level, (struct ly_mnode *)&type->info.enums.list[i]); |
| 199 | fprintf(f, "%*svalue %d;\n", LEVEL, INDENT, type->info.enums.list[i].value); |
| 200 | level--; |
| 201 | fprintf(f, "%*s}\n", LEVEL, INDENT); |
| 202 | } |
| 203 | break; |
| 204 | case LY_TYPE_IDENT: |
| 205 | if (module == type->info.ident.ref->module) { |
| 206 | fprintf(f, "%*sbase %s;\n", LEVEL, INDENT, type->info.ident.ref->name); |
| 207 | } else { |
| 208 | fprintf(f, "%*sbase %s:%s;\n", LEVEL, INDENT, type->info.ident.ref->module->prefix, |
| 209 | type->info.ident.ref->name); |
| 210 | } |
| 211 | break; |
| 212 | case LY_TYPE_INST: |
Radek Krejci | af35142 | 2015-06-19 14:49:38 +0200 | [diff] [blame] | 213 | if (type->info.inst.req == 1) { |
| 214 | fprintf(f, "%*srequire-instance \"true\";\n", LEVEL, INDENT); |
| 215 | } else if (type->info.inst.req == -1) { |
| 216 | fprintf(f, "%*srequire-instance \"false\";\n", LEVEL, INDENT); |
| 217 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 218 | break; |
| 219 | case LY_TYPE_INT8: |
| 220 | case LY_TYPE_INT16: |
| 221 | case LY_TYPE_INT32: |
| 222 | case LY_TYPE_INT64: |
| 223 | case LY_TYPE_UINT8: |
| 224 | case LY_TYPE_UINT16: |
| 225 | case LY_TYPE_UINT32: |
| 226 | case LY_TYPE_UINT64: |
Radek Krejci | f286013 | 2015-06-20 12:37:20 +0200 | [diff] [blame] | 227 | if (type->info.num.range != NULL) { |
| 228 | fprintf(f, "%*srange \"%s\" {\n", LEVEL, INDENT, type->info.num.range->expr); |
| 229 | yang_print_restr(f, level + 1, type->info.num.range); |
| 230 | fprintf(f, "%*s}\n", LEVEL, INDENT); |
| 231 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 232 | break; |
| 233 | case LY_TYPE_LEAFREF: |
| 234 | fprintf(f, "%*spath \"%s\";\n", LEVEL, INDENT, type->info.lref.path); |
| 235 | break; |
| 236 | case LY_TYPE_STRING: |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 237 | if (type->info.str.length) { |
Radek Krejci | 061bd52 | 2015-06-19 13:45:16 +0200 | [diff] [blame] | 238 | fprintf(f, "%*slength \"%s\" {\n", LEVEL, INDENT, type->info.str.length->expr); |
| 239 | yang_print_restr(f, level + 1, type->info.str.length); |
| 240 | fprintf(f, "%*s}\n", LEVEL, INDENT); |
| 241 | } |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 242 | for (i = 0; i < type->info.str.pat_count; i++) { |
| 243 | fprintf(f, "%*spattern \"%s\" {\n", LEVEL, INDENT, type->info.str.patterns[i].expr); |
| 244 | yang_print_restr(f, level + 1, &type->info.str.patterns[i]); |
| 245 | fprintf(f, "%*s}\n", LEVEL, INDENT); |
| 246 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 247 | break; |
| 248 | case LY_TYPE_UNION: |
| 249 | for (i = 0; i < type->info.uni.count; ++i) { |
| 250 | yang_print_type(f, level, module, &type->info.uni.type[i]); |
| 251 | } |
| 252 | break; |
| 253 | default: |
| 254 | /* other types do not have substatements */ |
| 255 | break; |
| 256 | } |
| 257 | level--; |
| 258 | fprintf(f, "%*s}\n", LEVEL, INDENT); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 259 | } |
| 260 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 261 | static void |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 262 | yang_print_must(FILE *f, int level, struct ly_restr *must) |
Michal Vasko | 7f976ee | 2015-06-09 13:55:41 +0200 | [diff] [blame] | 263 | { |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 264 | fprintf(f, "%*smust \"%s\" {\n", LEVEL, INDENT, must->expr); |
Radek Krejci | 41726f9 | 2015-06-19 13:11:05 +0200 | [diff] [blame] | 265 | yang_print_restr(f, level + 1, must); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 266 | fprintf(f, "%*s}\n", LEVEL, INDENT); |
Michal Vasko | 7f976ee | 2015-06-09 13:55:41 +0200 | [diff] [blame] | 267 | } |
| 268 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 269 | static void |
Michal Vasko | 5bbae10 | 2015-06-16 12:16:44 +0200 | [diff] [blame] | 270 | yang_print_refine(FILE *f, int level, struct ly_refine *refine) |
Michal Vasko | 00b7cfe | 2015-06-09 13:56:38 +0200 | [diff] [blame] | 271 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 272 | int i; |
Michal Vasko | 00b7cfe | 2015-06-09 13:56:38 +0200 | [diff] [blame] | 273 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 274 | fprintf(f, "%*srefine \"%s\" {\n", LEVEL, INDENT, refine->target); |
| 275 | level++; |
Michal Vasko | 00b7cfe | 2015-06-09 13:56:38 +0200 | [diff] [blame] | 276 | |
Radek Krejci | 6a11385 | 2015-07-03 16:04:20 +0200 | [diff] [blame] | 277 | if (refine->flags & LY_NODE_CONFIG_W) { |
| 278 | fprintf(f, "%*sconfig \"true\";\n", LEVEL, INDENT); |
| 279 | } else if (refine->flags & LY_NODE_CONFIG_R) { |
| 280 | fprintf(f, "%*sconfig \"false\";\n", LEVEL, INDENT); |
| 281 | } |
| 282 | |
| 283 | if (refine->flags & LY_NODE_MAND_TRUE) { |
| 284 | fprintf(f, "%*smandatory \"true\";\n", LEVEL, INDENT); |
| 285 | } else if (refine->flags & LY_NODE_MAND_FALSE) { |
| 286 | fprintf(f, "%*smandatory \"false\";\n", LEVEL, INDENT); |
| 287 | } |
| 288 | |
| 289 | yang_print_mnode_common(f, level, (struct ly_mnode *)refine); |
Michal Vasko | 00b7cfe | 2015-06-09 13:56:38 +0200 | [diff] [blame] | 290 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 291 | for (i = 0; i < refine->must_size; ++i) { |
| 292 | yang_print_must(f, level, &refine->must[i]); |
| 293 | } |
Michal Vasko | 00b7cfe | 2015-06-09 13:56:38 +0200 | [diff] [blame] | 294 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 295 | if (refine->target_type & (LY_NODE_LEAF | LY_NODE_CHOICE)) { |
| 296 | if (refine->mod.dflt != NULL) { |
| 297 | fprintf(f, "%*sdefault \"%s\";\n", LEVEL, INDENT, refine->mod.dflt); |
| 298 | } |
| 299 | } else if (refine->target_type == LY_NODE_CONTAINER) { |
| 300 | if (refine->mod.presence != NULL) { |
| 301 | yang_print_text(f, level, "presence", refine->mod.presence); |
| 302 | } |
| 303 | } else if (refine->target_type & (LY_NODE_LIST | LY_NODE_LEAFLIST)) { |
| 304 | if (refine->mod.list.min > 0) { |
| 305 | fprintf(f, "%*smin-elements %u;\n", LEVEL, INDENT, refine->mod.list.min); |
| 306 | } |
| 307 | if (refine->mod.list.max > 0) { |
| 308 | fprintf(f, "%*smax-elements %u;\n", LEVEL, INDENT, refine->mod.list.max); |
| 309 | } |
| 310 | } |
Michal Vasko | 00b7cfe | 2015-06-09 13:56:38 +0200 | [diff] [blame] | 311 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 312 | level--; |
| 313 | fprintf(f, "%*s}\n", LEVEL, INDENT); |
Michal Vasko | 00b7cfe | 2015-06-09 13:56:38 +0200 | [diff] [blame] | 314 | } |
| 315 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 316 | static void |
Michal Vasko | 5bbae10 | 2015-06-16 12:16:44 +0200 | [diff] [blame] | 317 | yang_print_typedef(FILE *f, int level, struct ly_module *module, struct ly_tpdf *tpdf) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 318 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 319 | fprintf(f, "%*stypedef %s {\n", LEVEL, INDENT, tpdf->name); |
| 320 | level++; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 321 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 322 | yang_print_mnode_common(f, level, (struct ly_mnode *)tpdf); |
| 323 | yang_print_type(f, level, module, &tpdf->type); |
| 324 | if (tpdf->units != NULL) { |
| 325 | fprintf(f, "%*sunits \"%s\";\n", LEVEL, INDENT, tpdf->units); |
| 326 | } |
| 327 | if (tpdf->dflt != NULL) { |
| 328 | fprintf(f, "%*sdefault \"%s\";\n", LEVEL, INDENT, tpdf->dflt); |
| 329 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 330 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 331 | level--; |
| 332 | fprintf(f, "%*s}\n", LEVEL, INDENT); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 333 | } |
| 334 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 335 | static void |
Michal Vasko | 5bbae10 | 2015-06-16 12:16:44 +0200 | [diff] [blame] | 336 | yang_print_identity(FILE *f, int level, struct ly_ident *ident) |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 337 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 338 | fprintf(f, "%*sidentity %s {\n", LEVEL, INDENT, ident->name); |
| 339 | level++; |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 340 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 341 | yang_print_mnode_common(f, level, (struct ly_mnode *)ident); |
| 342 | if (ident->base) { |
| 343 | if (ident->base->module == ident->module) { |
| 344 | fprintf(f, "%*sbase %s;\n", LEVEL, INDENT, ident->base->name); |
| 345 | } else { |
| 346 | fprintf(f, "%*sbase %s:%s;\n", LEVEL, INDENT, ident->base->module->prefix, ident->base->name); |
| 347 | } |
| 348 | } |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 349 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 350 | level--; |
| 351 | fprintf(f, "%*s}\n", LEVEL, INDENT); |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 352 | |
| 353 | } |
| 354 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 355 | static void |
Michal Vasko | 5bbae10 | 2015-06-16 12:16:44 +0200 | [diff] [blame] | 356 | yang_print_container(FILE *f, int level, struct ly_mnode *mnode) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 357 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 358 | int i; |
| 359 | struct ly_mnode *sub; |
| 360 | struct ly_mnode_container *cont = (struct ly_mnode_container *)mnode; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 361 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 362 | fprintf(f, "%*scontainer %s {\n", LEVEL, INDENT, mnode->name); |
Michal Vasko | 7f976ee | 2015-06-09 13:55:41 +0200 | [diff] [blame] | 363 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 364 | level++; |
Radek Krejci | 6a11385 | 2015-07-03 16:04:20 +0200 | [diff] [blame] | 365 | |
| 366 | yang_print_nacmext(f, level, mnode); |
| 367 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 368 | if (cont->presence != NULL) { |
| 369 | yang_print_text(f, level, "presence", cont->presence); |
| 370 | } |
Michal Vasko | 7f976ee | 2015-06-09 13:55:41 +0200 | [diff] [blame] | 371 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 372 | for (i = 0; i < cont->must_size; i++) { |
| 373 | yang_print_must(f, level, &cont->must[i]); |
| 374 | } |
Michal Vasko | 7f976ee | 2015-06-09 13:55:41 +0200 | [diff] [blame] | 375 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 376 | yang_print_mnode_common2(f, level, mnode); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 377 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 378 | for (i = 0; i < cont->tpdf_size; i++) { |
| 379 | yang_print_typedef(f, level, mnode->module, &cont->tpdf[i]); |
| 380 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 381 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 382 | LY_TREE_FOR(mnode->child, sub) { |
| 383 | yang_print_mnode(f, level, sub, |
| 384 | LY_NODE_CHOICE | LY_NODE_CONTAINER | LY_NODE_LEAF | LY_NODE_LEAFLIST | LY_NODE_LIST | |
| 385 | LY_NODE_USES | LY_NODE_GROUPING | LY_NODE_ANYXML); |
| 386 | } |
Michal Vasko | c1329bc | 2015-06-09 13:58:18 +0200 | [diff] [blame] | 387 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 388 | level--; |
| 389 | fprintf(f, "%*s}\n", LEVEL, INDENT); |
Michal Vasko | c1329bc | 2015-06-09 13:58:18 +0200 | [diff] [blame] | 390 | } |
| 391 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 392 | static void |
Michal Vasko | 5bbae10 | 2015-06-16 12:16:44 +0200 | [diff] [blame] | 393 | yang_print_case(FILE *f, int level, struct ly_mnode *mnode) |
Michal Vasko | c1329bc | 2015-06-09 13:58:18 +0200 | [diff] [blame] | 394 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 395 | struct ly_mnode *sub; |
| 396 | struct ly_mnode_case *cas = (struct ly_mnode_case *)mnode; |
Michal Vasko | c1329bc | 2015-06-09 13:58:18 +0200 | [diff] [blame] | 397 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 398 | fprintf(f, "%*scase %s {\n", LEVEL, INDENT, cas->name); |
| 399 | level++; |
Radek Krejci | 6a11385 | 2015-07-03 16:04:20 +0200 | [diff] [blame] | 400 | yang_print_nacmext(f, level, mnode); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 401 | yang_print_mnode_common2(f, level, mnode); |
Michal Vasko | c1329bc | 2015-06-09 13:58:18 +0200 | [diff] [blame] | 402 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 403 | LY_TREE_FOR(mnode->child, sub) { |
| 404 | yang_print_mnode(f, level, sub, |
| 405 | LY_NODE_CHOICE | LY_NODE_CONTAINER | LY_NODE_LEAF | LY_NODE_LEAFLIST | LY_NODE_LIST | |
| 406 | LY_NODE_USES | LY_NODE_ANYXML); |
| 407 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 408 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 409 | level--; |
| 410 | fprintf(f, "%*s}\n", LEVEL, INDENT); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 411 | } |
| 412 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 413 | static void |
Michal Vasko | 5bbae10 | 2015-06-16 12:16:44 +0200 | [diff] [blame] | 414 | yang_print_choice(FILE *f, int level, struct ly_mnode *mnode) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 415 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 416 | struct ly_mnode *sub; |
| 417 | struct ly_mnode_choice *choice = (struct ly_mnode_choice *)mnode; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 418 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 419 | fprintf(f, "%*schoice %s {\n", LEVEL, INDENT, mnode->name); |
Michal Vasko | 1608366 | 2015-06-09 14:00:45 +0200 | [diff] [blame] | 420 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 421 | level++; |
Radek Krejci | 6a11385 | 2015-07-03 16:04:20 +0200 | [diff] [blame] | 422 | yang_print_nacmext(f, level, mnode); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 423 | if (choice->dflt != NULL) { |
| 424 | fprintf(f, "%*sdefault \"%s\";\n", LEVEL, INDENT, choice->dflt->name); |
| 425 | } |
Michal Vasko | 1608366 | 2015-06-09 14:00:45 +0200 | [diff] [blame] | 426 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 427 | yang_print_mnode_common2(f, level, mnode); |
| 428 | LY_TREE_FOR(mnode->child, sub) { |
| 429 | yang_print_mnode(f, level, sub, |
| 430 | LY_NODE_CONTAINER | LY_NODE_LEAF | LY_NODE_LEAFLIST | LY_NODE_LIST | LY_NODE_ANYXML | LY_NODE_CASE); |
| 431 | } |
| 432 | level--; |
| 433 | fprintf(f, "%*s}\n", LEVEL, INDENT); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 434 | } |
| 435 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 436 | static void |
Michal Vasko | 5bbae10 | 2015-06-16 12:16:44 +0200 | [diff] [blame] | 437 | yang_print_leaf(FILE *f, int level, struct ly_mnode *mnode) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 438 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 439 | int i; |
| 440 | struct ly_mnode_leaf *leaf = (struct ly_mnode_leaf *)mnode; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 441 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 442 | fprintf(f, "%*sleaf %s {\n", LEVEL, INDENT, mnode->name); |
Michal Vasko | 1608366 | 2015-06-09 14:00:45 +0200 | [diff] [blame] | 443 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 444 | level++; |
Radek Krejci | 6a11385 | 2015-07-03 16:04:20 +0200 | [diff] [blame] | 445 | yang_print_nacmext(f, level, mnode); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 446 | yang_print_mnode_common2(f, level, mnode); |
| 447 | for (i = 0; i < leaf->must_size; i++) { |
| 448 | yang_print_must(f, level, &leaf->must[i]); |
| 449 | } |
| 450 | yang_print_type(f, level, mnode->module, &leaf->type); |
| 451 | if (leaf->units != NULL) { |
| 452 | fprintf(f, "%*sunits \"%s\";\n", LEVEL, INDENT, leaf->units); |
| 453 | } |
| 454 | if (leaf->dflt != NULL) { |
| 455 | fprintf(f, "%*sdefault \"%s\";\n", LEVEL, INDENT, leaf->dflt); |
| 456 | } |
| 457 | level--; |
Michal Vasko | 1608366 | 2015-06-09 14:00:45 +0200 | [diff] [blame] | 458 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 459 | fprintf(f, "%*s}\n", LEVEL, INDENT); |
Michal Vasko | 1608366 | 2015-06-09 14:00:45 +0200 | [diff] [blame] | 460 | } |
| 461 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 462 | static void |
Michal Vasko | 5bbae10 | 2015-06-16 12:16:44 +0200 | [diff] [blame] | 463 | yang_print_anyxml(FILE *f, int level, struct ly_mnode *mnode) |
Michal Vasko | 1608366 | 2015-06-09 14:00:45 +0200 | [diff] [blame] | 464 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 465 | int i; |
| 466 | struct ly_mnode_anyxml *anyxml = (struct ly_mnode_anyxml *)mnode; |
Michal Vasko | 1608366 | 2015-06-09 14:00:45 +0200 | [diff] [blame] | 467 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 468 | fprintf(f, "%*sanyxml %s {\n", LEVEL, INDENT, anyxml->name); |
| 469 | level++; |
Radek Krejci | 6a11385 | 2015-07-03 16:04:20 +0200 | [diff] [blame] | 470 | yang_print_nacmext(f, level, mnode); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 471 | yang_print_mnode_common2(f, level, mnode); |
| 472 | for (i = 0; i < anyxml->must_size; i++) { |
| 473 | yang_print_must(f, level, &anyxml->must[i]); |
| 474 | } |
| 475 | level--; |
| 476 | fprintf(f, "%*s}\n", LEVEL, INDENT); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 477 | } |
| 478 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 479 | static void |
Michal Vasko | 5bbae10 | 2015-06-16 12:16:44 +0200 | [diff] [blame] | 480 | yang_print_leaflist(FILE *f, int level, struct ly_mnode *mnode) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 481 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 482 | int i; |
| 483 | struct ly_mnode_leaflist *llist = (struct ly_mnode_leaflist *)mnode; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 484 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 485 | fprintf(f, "%*sleaf-list %s {\n", LEVEL, INDENT, mnode->name); |
Michal Vasko | 1608366 | 2015-06-09 14:00:45 +0200 | [diff] [blame] | 486 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 487 | level++; |
Radek Krejci | 6a11385 | 2015-07-03 16:04:20 +0200 | [diff] [blame] | 488 | yang_print_nacmext(f, level, mnode); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 489 | yang_print_mnode_common2(f, level, mnode); |
| 490 | if (llist->flags & LY_NODE_USERORDERED) { |
| 491 | fprintf(f, "%*sordered-by user;\n", LEVEL, INDENT); |
| 492 | } |
| 493 | if (llist->min > 0) { |
| 494 | fprintf(f, "%*smin-elements %u;\n", LEVEL, INDENT, llist->min); |
| 495 | } |
| 496 | if (llist->max > 0) { |
| 497 | fprintf(f, "%*smax-elements %u;\n", LEVEL, INDENT, llist->max); |
| 498 | } |
| 499 | for (i = 0; i < llist->must_size; i++) { |
| 500 | yang_print_must(f, level, &llist->must[i]); |
| 501 | } |
| 502 | yang_print_type(f, level, mnode->module, &llist->type); |
| 503 | if (llist->units != NULL) { |
| 504 | fprintf(f, "%*sunits \"%s\";\n", LEVEL, INDENT, llist->units); |
| 505 | } |
| 506 | level--; |
Michal Vasko | 1608366 | 2015-06-09 14:00:45 +0200 | [diff] [blame] | 507 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 508 | fprintf(f, "%*s}\n", LEVEL, INDENT); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 509 | } |
| 510 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 511 | static void |
Michal Vasko | 5bbae10 | 2015-06-16 12:16:44 +0200 | [diff] [blame] | 512 | yang_print_list(FILE *f, int level, struct ly_mnode *mnode) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 513 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 514 | int i, j; |
| 515 | struct ly_mnode *sub; |
| 516 | struct ly_unique *uniq; |
| 517 | struct ly_mnode_list *list = (struct ly_mnode_list *)mnode; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 518 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 519 | fprintf(f, "%*slist %s {\n", LEVEL, INDENT, mnode->name); |
| 520 | level++; |
Radek Krejci | 6a11385 | 2015-07-03 16:04:20 +0200 | [diff] [blame] | 521 | yang_print_nacmext(f, level, mnode); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 522 | yang_print_mnode_common2(f, level, mnode); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 523 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 524 | if (list->keys_size) { |
| 525 | fprintf(f, "%*skey \"", LEVEL, INDENT); |
| 526 | for (i = 0; i < list->keys_size; i++) { |
| 527 | fprintf(f, "%s%s", list->keys[i]->name, i + 1 < list->keys_size ? " " : ""); |
| 528 | } |
| 529 | fprintf(f, "\";\n"); |
| 530 | } |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 531 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 532 | for (i = 0; i < list->unique_size; i++) { |
| 533 | uniq = &list->unique[i]; |
| 534 | fprintf(f, "%*sunique \"", LEVEL, INDENT); |
| 535 | for (j = 0; j < uniq->leafs_size; j++) { |
| 536 | fprintf(f, "%s%s", uniq->leafs[j]->name, j + 1 < uniq->leafs_size ? " " : ""); |
| 537 | } |
| 538 | fprintf(f, "\";\n"); |
| 539 | } |
Michal Vasko | dff818b | 2015-06-09 14:01:12 +0200 | [diff] [blame] | 540 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 541 | if (list->flags & LY_NODE_USERORDERED) { |
| 542 | fprintf(f, "%*sordered-by user;\n", LEVEL, INDENT); |
| 543 | } |
| 544 | if (list->min > 0) { |
| 545 | fprintf(f, "%*smin-elements %u;\n", LEVEL, INDENT, list->min); |
| 546 | } |
| 547 | if (list->max > 0) { |
| 548 | fprintf(f, "%*smax-elements %u;\n", LEVEL, INDENT, list->max); |
| 549 | } |
| 550 | for (i = 0; i < list->must_size; i++) { |
| 551 | yang_print_must(f, level, &list->must[i]); |
| 552 | } |
Michal Vasko | dff818b | 2015-06-09 14:01:12 +0200 | [diff] [blame] | 553 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 554 | for (i = 0; i < list->tpdf_size; i++) { |
| 555 | yang_print_typedef(f, level, list->module, &list->tpdf[i]); |
| 556 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 557 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 558 | LY_TREE_FOR(mnode->child, sub) { |
| 559 | yang_print_mnode(f, level, sub, |
| 560 | LY_NODE_CHOICE | LY_NODE_CONTAINER | LY_NODE_LEAF | LY_NODE_LEAFLIST | LY_NODE_LIST | |
| 561 | LY_NODE_USES | LY_NODE_GROUPING | LY_NODE_ANYXML); |
| 562 | } |
| 563 | level--; |
| 564 | fprintf(f, "%*s}\n", LEVEL, INDENT); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 565 | } |
| 566 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 567 | static void |
Michal Vasko | 5bbae10 | 2015-06-16 12:16:44 +0200 | [diff] [blame] | 568 | yang_print_grouping(FILE *f, int level, struct ly_mnode *mnode) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 569 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 570 | int i; |
| 571 | struct ly_mnode *node; |
| 572 | struct ly_mnode_grp *grp = (struct ly_mnode_grp *)mnode; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 573 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 574 | fprintf(f, "%*sgrouping %s {\n", LEVEL, INDENT, mnode->name); |
| 575 | level++; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 576 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 577 | yang_print_mnode_common(f, level, mnode); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 578 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 579 | for (i = 0; i < grp->tpdf_size; i++) { |
| 580 | yang_print_typedef(f, level, mnode->module, &grp->tpdf[i]); |
| 581 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 582 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 583 | LY_TREE_FOR(mnode->child, node) { |
| 584 | yang_print_mnode(f, level, node, |
| 585 | LY_NODE_CHOICE | LY_NODE_CONTAINER | LY_NODE_LEAF | LY_NODE_LEAFLIST | LY_NODE_LIST | |
| 586 | LY_NODE_USES | LY_NODE_GROUPING | LY_NODE_ANYXML); |
| 587 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 588 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 589 | level--; |
| 590 | fprintf(f, "%*s}\n", LEVEL, INDENT); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 591 | } |
| 592 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 593 | static void |
Michal Vasko | 5bbae10 | 2015-06-16 12:16:44 +0200 | [diff] [blame] | 594 | yang_print_uses(FILE *f, int level, struct ly_mnode *mnode) |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 595 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 596 | int i; |
| 597 | struct ly_mnode_uses *uses = (struct ly_mnode_uses *)mnode; |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 598 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 599 | fprintf(f, "%*suses %s {\n", LEVEL, INDENT, uses->name); |
| 600 | level++; |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 601 | |
Radek Krejci | 6a11385 | 2015-07-03 16:04:20 +0200 | [diff] [blame] | 602 | yang_print_nacmext(f, level, mnode); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 603 | yang_print_mnode_common(f, level, mnode); |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 604 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 605 | for (i = 0; i < uses->refine_size; i++) { |
| 606 | yang_print_refine(f, level, &uses->refine[i]); |
| 607 | } |
Michal Vasko | 00b7cfe | 2015-06-09 13:56:38 +0200 | [diff] [blame] | 608 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 609 | level--; |
| 610 | fprintf(f, "%*s}\n", LEVEL, INDENT); |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 611 | } |
| 612 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 613 | static void |
Michal Vasko | 5bbae10 | 2015-06-16 12:16:44 +0200 | [diff] [blame] | 614 | yang_print_input_output(FILE *f, int level, struct ly_mnode *mnode) |
| 615 | { |
Michal Vasko | f4d3d74 | 2015-06-16 11:51:09 +0200 | [diff] [blame] | 616 | int i; |
| 617 | struct ly_mnode *sub; |
Michal Vasko | fb69a84 | 2015-06-16 12:07:40 +0200 | [diff] [blame] | 618 | struct ly_mnode_input_output *inout = (struct ly_mnode_input_output *)mnode; |
Michal Vasko | f4d3d74 | 2015-06-16 11:51:09 +0200 | [diff] [blame] | 619 | |
| 620 | fprintf(f, "%*s%s {\n", LEVEL, INDENT, (inout->nodetype == LY_NODE_INPUT ? "input" : "output")); |
| 621 | |
| 622 | level++; |
| 623 | for (i = 0; i < inout->tpdf_size; i++) { |
| 624 | yang_print_typedef(f, level, mnode->module, &inout->tpdf[i]); |
| 625 | } |
| 626 | |
| 627 | LY_TREE_FOR(mnode->child, sub) { |
| 628 | yang_print_mnode(f, level, sub, |
| 629 | LY_NODE_CHOICE | LY_NODE_CONTAINER | LY_NODE_LEAF | LY_NODE_LEAFLIST | LY_NODE_LIST | |
| 630 | LY_NODE_USES | LY_NODE_GROUPING | LY_NODE_ANYXML); |
| 631 | } |
| 632 | |
| 633 | level--; |
| 634 | fprintf(f, "%*s}\n", LEVEL, INDENT); |
| 635 | } |
| 636 | |
| 637 | static void |
| 638 | yang_print_rpc(FILE *f, int level, struct ly_mnode *mnode) |
| 639 | { |
| 640 | int i; |
| 641 | struct ly_mnode *sub; |
| 642 | struct ly_mnode_rpc *rpc = (struct ly_mnode_rpc *)mnode; |
| 643 | |
| 644 | fprintf(f, "%*srpc %s {\n", LEVEL, INDENT, mnode->name); |
| 645 | |
| 646 | level++; |
| 647 | yang_print_mnode_common(f, level, mnode); |
| 648 | |
| 649 | for (i = 0; i < rpc->tpdf_size; i++) { |
| 650 | yang_print_typedef(f, level, mnode->module, &rpc->tpdf[i]); |
| 651 | } |
| 652 | |
| 653 | LY_TREE_FOR(mnode->child, sub) { |
| 654 | yang_print_mnode(f, level, sub, |
| 655 | LY_NODE_GROUPING | LY_NODE_INPUT | LY_NODE_OUTPUT); |
| 656 | } |
| 657 | |
| 658 | level--; |
| 659 | fprintf(f, "%*s}\n", LEVEL, INDENT); |
| 660 | } |
| 661 | |
| 662 | static void |
Michal Vasko | 7690bc1 | 2015-06-16 12:26:05 +0200 | [diff] [blame] | 663 | yang_print_notif(FILE *f, int level, struct ly_mnode *mnode) |
| 664 | { |
Michal Vasko | f4d3d74 | 2015-06-16 11:51:09 +0200 | [diff] [blame] | 665 | int i; |
| 666 | struct ly_mnode *sub; |
| 667 | struct ly_mnode_notif *notif = (struct ly_mnode_notif *)mnode; |
| 668 | |
| 669 | fprintf(f, "%*snotification %s {\n", LEVEL, INDENT, mnode->name); |
| 670 | |
| 671 | level++; |
| 672 | yang_print_mnode_common(f, level, mnode); |
| 673 | |
| 674 | for (i = 0; i < notif->tpdf_size; i++) { |
| 675 | yang_print_typedef(f, level, mnode->module, ¬if->tpdf[i]); |
| 676 | } |
| 677 | |
| 678 | LY_TREE_FOR(mnode->child, sub) { |
| 679 | yang_print_mnode(f, level, sub, |
| 680 | LY_NODE_CHOICE | LY_NODE_CONTAINER | LY_NODE_LEAF | LY_NODE_LEAFLIST | LY_NODE_LIST | |
| 681 | LY_NODE_USES | LY_NODE_GROUPING | LY_NODE_ANYXML); |
| 682 | } |
| 683 | |
| 684 | level--; |
| 685 | fprintf(f, "%*s}\n", LEVEL, INDENT); |
| 686 | } |
| 687 | |
| 688 | static void |
Michal Vasko | 5bbae10 | 2015-06-16 12:16:44 +0200 | [diff] [blame] | 689 | yang_print_mnode(FILE *f, int level, struct ly_mnode *mnode, int mask) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 690 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 691 | switch (mnode->nodetype & mask) { |
| 692 | case LY_NODE_CONTAINER: |
| 693 | yang_print_container(f, level, mnode); |
| 694 | break; |
| 695 | case LY_NODE_CHOICE: |
| 696 | yang_print_choice(f, level, mnode); |
| 697 | break; |
| 698 | case LY_NODE_LEAF: |
| 699 | yang_print_leaf(f, level, mnode); |
| 700 | break; |
| 701 | case LY_NODE_LEAFLIST: |
| 702 | yang_print_leaflist(f, level, mnode); |
| 703 | break; |
| 704 | case LY_NODE_LIST: |
| 705 | yang_print_list(f, level, mnode); |
| 706 | break; |
| 707 | case LY_NODE_USES: |
| 708 | yang_print_uses(f, level, mnode); |
| 709 | break; |
| 710 | case LY_NODE_GROUPING: |
| 711 | yang_print_grouping(f, level, mnode); |
| 712 | break; |
| 713 | case LY_NODE_ANYXML: |
| 714 | yang_print_anyxml(f, level, mnode); |
| 715 | break; |
| 716 | case LY_NODE_CASE: |
| 717 | yang_print_case(f, level, mnode); |
| 718 | break; |
Michal Vasko | f4d3d74 | 2015-06-16 11:51:09 +0200 | [diff] [blame] | 719 | case LY_NODE_INPUT: |
| 720 | case LY_NODE_OUTPUT: |
| 721 | yang_print_input_output(f, level, mnode); |
| 722 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 723 | default: |
| 724 | break; |
| 725 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 726 | } |
| 727 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 728 | int |
Michal Vasko | 5bbae10 | 2015-06-16 12:16:44 +0200 | [diff] [blame] | 729 | yang_print_model(FILE *f, struct ly_module *module) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 730 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 731 | unsigned int i; |
| 732 | int level = 0; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 733 | #define LEVEL (level*2) |
| 734 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 735 | struct ly_mnode *mnode; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 736 | |
Michal Vasko | 116172e | 2015-07-07 11:54:37 +0200 | [diff] [blame^] | 737 | if (module->type) { |
| 738 | fprintf(f, "submodule %s {\n", module->name); |
| 739 | level++; |
| 740 | fprintf(f, "%*sbelongs-to %s {\n", LEVEL, INDENT, ((struct ly_submodule *)module)->belongsto->name); |
| 741 | level++; |
| 742 | fprintf(f, "%*sprefix \"%s\";\n", LEVEL, INDENT, module->prefix); |
| 743 | level--; |
| 744 | fprintf(f, "%*s}\n", LEVEL, INDENT); |
| 745 | } else { |
| 746 | fprintf(f, "module %s {\n", module->name); |
| 747 | level++; |
| 748 | fprintf(f, "%*snamespace \"%s\";\n", LEVEL, INDENT, module->ns); |
| 749 | fprintf(f, "%*sprefix \"%s\";\n", LEVEL, INDENT, module->prefix); |
| 750 | } |
Radek Krejci | b0594bf | 2015-05-21 23:51:27 +0200 | [diff] [blame] | 751 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 752 | if (module->version) { |
| 753 | fprintf(f, "%*syang-version \"%s\";\n", LEVEL, INDENT, module->version == 1 ? "1.0" : "1.1"); |
| 754 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 755 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 756 | for (i = 0; i < module->imp_size; i++) { |
| 757 | fprintf(f, "%*simport \"%s\" {\n", LEVEL, INDENT, module->imp[i].module->name); |
| 758 | level++; |
| 759 | yang_print_text(f, level, "prefix", module->imp[i].prefix); |
| 760 | if (module->imp[i].rev[0]) { |
| 761 | yang_print_text(f, level, "revision-date", module->imp[i].rev); |
| 762 | } |
| 763 | level--; |
| 764 | fprintf(f, "%*s}\n", LEVEL, INDENT); |
| 765 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 766 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 767 | for (i = 0; i < module->inc_size; i++) { |
| 768 | if (module->inc[i].rev[0]) { |
| 769 | fprintf(f, "%*sinclude \"%s\" {\n", LEVEL, INDENT, module->inc[i].submodule->name); |
| 770 | yang_print_text(f, level + 1, "revision-date", module->imp[i].rev); |
| 771 | fprintf(f, "%*s}\n", LEVEL, INDENT); |
| 772 | } else { |
| 773 | fprintf(f, "%*sinclude \"%s\";\n", LEVEL, INDENT, module->inc[i].submodule->name); |
| 774 | } |
| 775 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 776 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 777 | if (module->org) { |
| 778 | yang_print_text(f, level, "organization", module->org); |
| 779 | } |
| 780 | if (module->contact) { |
| 781 | yang_print_text(f, level, "contact", module->contact); |
| 782 | } |
| 783 | if (module->dsc) { |
| 784 | yang_print_text(f, level, "description", module->dsc); |
| 785 | } |
| 786 | if (module->ref) { |
| 787 | yang_print_text(f, level, "reference", module->ref); |
| 788 | } |
| 789 | for (i = 0; i < module->rev_size; i++) { |
| 790 | if (module->rev[i].dsc || module->rev[i].ref) { |
| 791 | fprintf(f, "%*srevision \"%s\" {\n", LEVEL, INDENT, module->rev[i].date); |
| 792 | level++; |
| 793 | if (module->rev[i].dsc) { |
| 794 | yang_print_text(f, level, "description", module->rev[i].dsc); |
| 795 | } |
| 796 | if (module->rev[i].ref) { |
| 797 | yang_print_text(f, level, "reference", module->rev[i].ref); |
| 798 | } |
| 799 | level--; |
| 800 | fprintf(f, "%*s}\n", LEVEL, INDENT); |
| 801 | } else { |
| 802 | yang_print_text(f, level, "revision", module->rev[i].date); |
| 803 | } |
| 804 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 805 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 806 | for (i = 0; i < module->ident_size; i++) { |
| 807 | yang_print_identity(f, level, &module->ident[i]); |
| 808 | } |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 809 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 810 | for (i = 0; i < module->tpdf_size; i++) { |
| 811 | yang_print_typedef(f, level, module, &module->tpdf[i]); |
| 812 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 813 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 814 | LY_TREE_FOR(module->data, mnode) { |
| 815 | yang_print_mnode(f, level, mnode, |
| 816 | LY_NODE_CHOICE | LY_NODE_CONTAINER | LY_NODE_LEAF | LY_NODE_LEAFLIST | LY_NODE_LIST | |
| 817 | LY_NODE_USES | LY_NODE_GROUPING | LY_NODE_ANYXML); |
| 818 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 819 | |
Michal Vasko | f4d3d74 | 2015-06-16 11:51:09 +0200 | [diff] [blame] | 820 | LY_TREE_FOR(module->rpc, mnode) { |
| 821 | yang_print_rpc(f, level, mnode); |
| 822 | } |
| 823 | |
| 824 | LY_TREE_FOR(module->notif, mnode) { |
| 825 | yang_print_notif(f, level, mnode); |
| 826 | } |
| 827 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 828 | fprintf(f, "}\n"); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 829 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 830 | return EXIT_SUCCESS; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 831 | #undef LEVEL |
| 832 | } |