blob: 5c5894ef638f7f09f97a4519de9296eb73e302c5 [file] [log] [blame]
Radek Krejcie9f13b12020-11-09 17:42:04 +01001/**
2 * @file common.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief libyang's yanglint tool - common functions for both interactive and non-interactive mode.
5 *
6 * Copyright (c) 2020 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#define _GNU_SOURCE
Radek Krejcif8dc59a2020-11-25 13:47:44 +010016#define _POSIX_C_SOURCE 200809L /* strdup, strndup */
Radek Krejcie9f13b12020-11-09 17:42:04 +010017
18#include "common.h"
19
20#include <assert.h>
21#include <errno.h>
22#include <getopt.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/stat.h>
27
28#include "compat.h"
29#include "libyang.h"
30
31int
32parse_schema_path(const char *path, char **dir, char **module)
33{
34 char *p;
35
36 assert(dir);
37 assert(module);
38
39 /* split the path to dirname and basename for further work */
40 *dir = strdup(path);
41 *module = strrchr(*dir, '/');
42 if (!(*module)) {
43 *module = *dir;
44 *dir = strdup("./");
45 } else {
46 *module[0] = '\0'; /* break the dir */
47 *module = strdup((*module) + 1);
48 }
49 /* get the pure module name without suffix or revision part of the filename */
50 if ((p = strchr(*module, '@'))) {
51 /* revision */
52 *p = '\0';
53 } else if ((p = strrchr(*module, '.'))) {
54 /* fileformat suffix */
55 *p = '\0';
56 }
57
58 return 0;
59}
60
61int
62get_input(const char *filepath, LYS_INFORMAT *format_schema, LYD_FORMAT *format_data, struct ly_in **in)
63{
64 struct stat st;
65
66 /* check that the filepath exists and is a regular file */
67 if (stat(filepath, &st) == -1) {
68 YLMSG_E("Unable to use input filepath (%s) - %s.\n", filepath, strerror(errno));
69 return -1;
70 }
71 if (!S_ISREG(st.st_mode)) {
72 YLMSG_E("Provided input file (%s) is not a regular file.\n", filepath);
73 return -1;
74 }
75
Michal Vaskod3b10542021-02-03 11:31:16 +010076 if ((format_schema && !*format_schema) || (format_data && !*format_data)) {
77 /* get the file format */
78 if (get_format(filepath, format_schema, format_data)) {
79 return -1;
80 }
Radek Krejcie9f13b12020-11-09 17:42:04 +010081 }
82
83 if (ly_in_new_filepath(filepath, 0, in)) {
84 YLMSG_E("Unable to process input file.\n");
85 return -1;
86 }
87
88 return 0;
89}
90
91void
92free_features(void *flist)
93{
94 struct schema_features *rec = (struct schema_features *)flist;
95
96 if (rec) {
Michal Vaskoa9a98612021-11-22 10:00:27 +010097 free(rec->mod_name);
Radek Krejcie9f13b12020-11-09 17:42:04 +010098 if (rec->features) {
99 for (uint32_t u = 0; rec->features[u]; ++u) {
100 free(rec->features[u]);
101 }
102 free(rec->features);
103 }
104 free(rec);
105 }
106}
107
108void
109get_features(struct ly_set *fset, const char *module, const char ***features)
110{
111 /* get features list for this module */
112 for (uint32_t u = 0; u < fset->count; ++u) {
113 struct schema_features *sf = (struct schema_features *)fset->objs[u];
Michal Vaskoa9a98612021-11-22 10:00:27 +0100114 if (!strcmp(module, sf->mod_name)) {
Radek Krejci8143bb42021-04-07 11:43:50 +0200115 /* matched module - explicitly set features */
Radek Krejcie9f13b12020-11-09 17:42:04 +0100116 *features = (const char **)sf->features;
117 return;
118 }
119 }
Radek Krejci8143bb42021-04-07 11:43:50 +0200120
Michal Vasko59740872021-11-22 10:01:34 +0100121 /* features not set so disable all */
122 *features = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100123}
124
125int
126parse_features(const char *fstring, struct ly_set *fset)
127{
128 struct schema_features *rec;
129 char *p;
130
131 rec = calloc(1, sizeof *rec);
132 if (!rec) {
133 YLMSG_E("Unable to allocate features information record (%s).\n", strerror(errno));
134 return -1;
135 }
136 if (ly_set_add(fset, rec, 1, NULL)) {
137 YLMSG_E("Unable to store features information (%s).\n", strerror(errno));
138 free(rec);
139 return -1;
140 }
141
142 /* fill the record */
143 p = strchr(fstring, ':');
144 if (!p) {
145 YLMSG_E("Invalid format of the features specification (%s)", fstring);
146 return -1;
147 }
Michal Vaskoa9a98612021-11-22 10:00:27 +0100148 rec->mod_name = strndup(fstring, p - fstring);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100149
150 /* start count on 2 to include terminating NULL byte */
151 for (int count = 2; p; ++count) {
152 size_t len = 0;
153 char *token = p + 1;
154 p = strchr(token, ',');
155 if (!p) {
156 /* the last item, if any */
157 len = strlen(token);
158 } else {
159 len = p - token;
160 }
161 if (len) {
162 char **fp = realloc(rec->features, count * sizeof *rec->features);
163 if (!fp) {
164 YLMSG_E("Unable to store features list information (%s).\n", strerror(errno));
165 return -1;
166 }
167 rec->features = fp;
168 rec->features[count - 1] = NULL; /* terminating NULL-byte */
169 fp = &rec->features[count - 2]; /* array item to set */
170 (*fp) = strndup(token, len);
171 }
172 }
173
174 return 0;
175}
176
177struct cmdline_file *
178fill_cmdline_file(struct ly_set *set, struct ly_in *in, const char *path, LYD_FORMAT format)
179{
180 struct cmdline_file *rec;
181
182 rec = malloc(sizeof *rec);
183 if (!rec) {
184 YLMSG_E("Allocating memory for data file information failed.\n");
185 return NULL;
186 }
187 if (set && ly_set_add(set, rec, 1, NULL)) {
188 free(rec);
Michal Vasko7edebb42021-01-25 14:18:46 +0100189 YLMSG_E("Storing data file information failed.\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100190 return NULL;
191 }
192 rec->in = in;
193 rec->path = path;
194 rec->format = format;
195
196 return rec;
197}
198
199void
200free_cmdline_file(void *cmdline_file)
201{
202 struct cmdline_file *rec = (struct cmdline_file *)cmdline_file;
203
204 if (rec) {
205 ly_in_free(rec->in, 1);
206 free(rec);
207 }
208}
209
210void
211free_cmdline(char *argv[])
212{
213 if (argv) {
214 free(argv[0]);
215 free(argv);
216 }
217}
218
219int
220parse_cmdline(const char *cmdline, int *argc_p, char **argv_p[])
221{
222 int count;
223 char **vector;
224 char *ptr;
225 char qmark = 0;
226
227 assert(cmdline);
228 assert(argc_p);
229 assert(argv_p);
230
231 /* init */
232 optind = 0; /* reinitialize getopt() */
233 count = 1;
234 vector = malloc((count + 1) * sizeof *vector);
235 vector[0] = strdup(cmdline);
236
237 /* command name */
238 strtok(vector[0], " ");
239
240 /* arguments */
241 while ((ptr = strtok(NULL, " "))) {
242 size_t len;
243 void *r;
244
245 len = strlen(ptr);
246
247 if (qmark) {
248 /* still in quotated text */
249 /* remove NULL termination of the previous token since it is not a token,
250 * but a part of the quotation string */
251 ptr[-1] = ' ';
252
253 if ((ptr[len - 1] == qmark) && (ptr[len - 2] != '\\')) {
254 /* end of quotation */
255 qmark = 0;
256 /* shorten the argument by the terminating quotation mark */
257 ptr[len - 1] = '\0';
258 }
259 continue;
260 }
261
262 /* another token in cmdline */
263 ++count;
264 r = realloc(vector, (count + 1) * sizeof *vector);
265 if (!r) {
266 YLMSG_E("Memory allocation failed (%s:%d, %s),", __FILE__, __LINE__, strerror(errno));
267 free(vector);
268 return -1;
269 }
270 vector = r;
271 vector[count - 1] = ptr;
272
273 if ((ptr[0] == '"') || (ptr[0] == '\'')) {
274 /* remember the quotation mark to identify end of quotation */
275 qmark = ptr[0];
276
277 /* move the remembered argument after the quotation mark */
278 ++vector[count - 1];
279
280 /* check if the quotation is terminated within this token */
281 if ((ptr[len - 1] == qmark) && (ptr[len - 2] != '\\')) {
282 /* end of quotation */
283 qmark = 0;
284 /* shorten the argument by the terminating quotation mark */
285 ptr[len - 1] = '\0';
286 }
287 }
288 }
289 vector[count] = NULL;
290
291 *argc_p = count;
292 *argv_p = vector;
293
294 return 0;
295}
296
297int
298get_format(const char *filename, LYS_INFORMAT *schema, LYD_FORMAT *data)
299{
300 char *ptr;
301 LYS_INFORMAT informat_s;
302 LYD_FORMAT informat_d;
303
304 /* get the file format */
305 if ((ptr = strrchr(filename, '.')) != NULL) {
306 ++ptr;
307 if (!strcmp(ptr, "yang")) {
308 informat_s = LYS_IN_YANG;
309 informat_d = 0;
310 } else if (!strcmp(ptr, "yin")) {
311 informat_s = LYS_IN_YIN;
312 informat_d = 0;
313 } else if (!strcmp(ptr, "xml")) {
314 informat_s = 0;
315 informat_d = LYD_XML;
316 } else if (!strcmp(ptr, "json")) {
317 informat_s = 0;
318 informat_d = LYD_JSON;
Michal Vaskod3b10542021-02-03 11:31:16 +0100319 } else if (!strcmp(ptr, "lyb")) {
320 informat_s = 0;
321 informat_d = LYD_LYB;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100322 } else {
323 YLMSG_E("Input file \"%s\" in an unknown format \"%s\".\n", filename, ptr);
324 return 0;
325 }
326 } else {
327 YLMSG_E("Input file \"%s\" without file extension - unknown format.\n", filename);
328 return 1;
329 }
330
331 if (informat_d) {
332 if (!data) {
333 YLMSG_E("Input file \"%s\" not expected to contain data instances (unexpected format).\n", filename);
334 return 2;
335 }
336 (*data) = informat_d;
337 } else if (informat_s) {
338 if (!schema) {
339 YLMSG_E("Input file \"%s\" not expected to contain schema definition (unexpected format).\n", filename);
340 return 3;
341 }
342 (*schema) = informat_s;
343 }
344
345 return 0;
346}
347
348int
349print_list(struct ly_out *out, struct ly_ctx *ctx, LYD_FORMAT outformat)
350{
351 struct lyd_node *ylib;
352 uint32_t idx = 0, has_modules = 0;
353 const struct lys_module *mod;
354
355 if (outformat != LYD_UNKNOWN) {
Michal Vasko794ab4b2021-03-31 09:42:19 +0200356 if (ly_ctx_get_yanglib_data(ctx, &ylib, "%u", ly_ctx_get_change_count(ctx))) {
Michal Vaskoc431a0a2021-01-25 14:31:58 +0100357 YLMSG_E("Getting context info (ietf-yang-library data) failed. If the YANG module is missing or not implemented, use an option to add it internally.\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100358 return 1;
359 }
360
361 lyd_print_all(out, ylib, outformat, 0);
362 lyd_free_all(ylib);
363 return 0;
364 }
365
366 /* iterate schemas in context and provide just the basic info */
367 ly_print(out, "List of the loaded models:\n");
368 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
369 has_modules++;
370
371 /* conformance print */
372 if (mod->implemented) {
373 ly_print(out, " I");
374 } else {
375 ly_print(out, " i");
376 }
377
378 /* module print */
379 ly_print(out, " %s", mod->name);
380 if (mod->revision) {
381 ly_print(out, "@%s", mod->revision);
382 }
383
384 /* submodules print */
385 if (mod->parsed && mod->parsed->includes) {
386 uint64_t u = 0;
387 ly_print(out, " (");
388 LY_ARRAY_FOR(mod->parsed->includes, u) {
389 ly_print(out, "%s%s", !u ? "" : ",", mod->parsed->includes[u].name);
390 if (mod->parsed->includes[u].rev[0]) {
391 ly_print(out, "@%s", mod->parsed->includes[u].rev);
392 }
393 }
394 ly_print(out, ")");
395 }
396
397 /* finish the line */
398 ly_print(out, "\n");
399 }
400
401 if (!has_modules) {
402 ly_print(out, "\t(none)\n");
403 }
404
405 ly_print_flush(out);
406 return 0;
407}
408
409int
Radek Krejcie9f13b12020-11-09 17:42:04 +0100410evaluate_xpath(const struct lyd_node *tree, const char *xpath)
411{
Radek Krejci89757c12020-11-26 12:07:47 +0100412 struct ly_set *set = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100413
414 if (lyd_find_xpath(tree, xpath, &set)) {
415 return -1;
416 }
417
418 /* print result */
419 printf("XPath \"%s\" evaluation result:\n", xpath);
420 if (!set->count) {
421 printf("\tEmpty\n");
422 } else {
423 for (uint32_t u = 0; u < set->count; ++u) {
424 struct lyd_node *node = (struct lyd_node *)set->objs[u];
425 printf(" %s \"%s\"", lys_nodetype2str(node->schema->nodetype), node->schema->name);
426 if (node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200427 printf(" (value: \"%s\")\n", lyd_get_value(node));
Radek Krejcie9f13b12020-11-09 17:42:04 +0100428 } else if (node->schema->nodetype == LYS_LIST) {
429 printf(" (");
430 for (struct lyd_node *key = ((struct lyd_node_inner *)node)->child; key && lysc_is_key(key->schema); key = key->next) {
431 printf("%s\"%s\": \"%s\";", (key != ((struct lyd_node_inner *)node)->child) ? " " : "",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200432 key->schema->name, lyd_get_value(key));
Radek Krejcie9f13b12020-11-09 17:42:04 +0100433 }
434 printf(")\n");
435 }
436 }
437 }
438
439 ly_set_free(set, NULL);
440 return 0;
441}
442
443LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100444process_data(struct ly_ctx *ctx, enum lyd_type data_type, uint8_t merge, LYD_FORMAT format, struct ly_out *out,
Radek Krejcie9f13b12020-11-09 17:42:04 +0100445 uint32_t options_parse, uint32_t options_validate, uint32_t options_print,
Radek Krejci6784a4e2020-12-09 14:23:05 +0100446 struct cmdline_file *operational_f, struct ly_set *inputs, struct ly_set *xpaths)
Radek Krejcie9f13b12020-11-09 17:42:04 +0100447{
Radek Krejci89757c12020-11-26 12:07:47 +0100448 LY_ERR ret = LY_SUCCESS;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100449 struct lyd_node *tree = NULL, *merged_tree = NULL;
Radek Krejci89757c12020-11-26 12:07:47 +0100450 struct lyd_node *operational = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100451
452 /* additional operational datastore */
453 if (operational_f && operational_f->in) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100454 ret = lyd_parse_data(ctx, NULL, operational_f->in, operational_f->format, LYD_PARSE_ONLY, 0, &operational);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100455 if (ret) {
456 YLMSG_E("Failed to parse operational datastore file \"%s\".\n", operational_f->path);
457 goto cleanup;
458 }
459 }
460
461 for (uint32_t u = 0; u < inputs->count; ++u) {
462 struct cmdline_file *input_f = (struct cmdline_file *)inputs->objs[u];
Radek Krejcie9f13b12020-11-09 17:42:04 +0100463 switch (data_type) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100464 case LYD_TYPE_DATA_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +0100465 ret = lyd_parse_data(ctx, NULL, input_f->in, input_f->format, options_parse, options_validate, &tree);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100466 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100467 case LYD_TYPE_RPC_YANG:
468 case LYD_TYPE_REPLY_YANG:
469 case LYD_TYPE_NOTIF_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +0100470 ret = lyd_parse_op(ctx, NULL, input_f->in, input_f->format, data_type, &tree, NULL);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100471 break;
Radek Krejci89757c12020-11-26 12:07:47 +0100472 default:
473 YLMSG_E("Internal error (%s:%d).\n", __FILE__, __LINE__);
474 goto cleanup;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100475 }
476
477 if (ret) {
478 YLMSG_E("Failed to parse input data file \"%s\".\n", input_f->path);
479 goto cleanup;
480 }
481
482 if (merge) {
483 /* merge the data so far parsed for later validation and print */
484 if (!merged_tree) {
485 merged_tree = tree;
486 } else {
487 ret = lyd_merge_siblings(&merged_tree, tree, LYD_MERGE_DESTRUCT);
488 if (ret) {
489 YLMSG_E("Merging %s with previous data failed.\n", input_f->path);
490 goto cleanup;
491 }
492 }
493 tree = NULL;
494 } else if (format) {
495 lyd_print_all(out, tree, format, options_print);
496 } else if (operational) {
497 /* additional validation of the RPC/Action/reply/Notification with the operational datastore */
498 ret = lyd_validate_op(tree, operational, data_type, NULL);
499 if (ret) {
500 YLMSG_E("Failed to validate input data file \"%s\" with operational datastore \"%s\".\n",
501 input_f->path, operational_f->path);
502 goto cleanup;
503 }
504 }
505 lyd_free_all(tree);
506 tree = NULL;
507 }
508
509 if (merge) {
510 /* validate the merged result */
511 ret = lyd_validate_all(&merged_tree, ctx, LYD_VALIDATE_PRESENT, NULL);
512 if (ret) {
513 YLMSG_E("Merged data are not valid.\n");
514 goto cleanup;
515 }
516
517 if (format) {
518 /* and print it */
519 lyd_print_all(out, merged_tree, format, options_print);
520 }
521
522 for (uint32_t u = 0; u < xpaths->count; ++u) {
523 if (evaluate_xpath(merged_tree, (const char *)xpaths->objs[u])) {
524 goto cleanup;
525 }
526 }
527 }
528
529cleanup:
530 /* cleanup */
531 lyd_free_all(merged_tree);
532 lyd_free_all(tree);
533
534 return ret;
535}