blob: 4f72ab8e11f97c45442b2044885a9632c7c0cb85 [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) {
97 free(rec->module);
98 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
Radek Krejci8143bb42021-04-07 11:43:50 +0200108
Radek Krejcie9f13b12020-11-09 17:42:04 +0100109void
110get_features(struct ly_set *fset, const char *module, const char ***features)
111{
Radek Krejci8143bb42021-04-07 11:43:50 +0200112 static const char *all_features[] = {"*", NULL};
113
Radek Krejcie9f13b12020-11-09 17:42:04 +0100114 /* get features list for this module */
115 for (uint32_t u = 0; u < fset->count; ++u) {
116 struct schema_features *sf = (struct schema_features *)fset->objs[u];
117 if (!strcmp(module, sf->module)) {
Radek Krejci8143bb42021-04-07 11:43:50 +0200118 /* matched module - explicitly set features */
Radek Krejcie9f13b12020-11-09 17:42:04 +0100119 *features = (const char **)sf->features;
120 return;
121 }
122 }
Radek Krejci8143bb42021-04-07 11:43:50 +0200123
124 /* features not set, enable all features by default */
125 *features = all_features;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100126}
127
128int
129parse_features(const char *fstring, struct ly_set *fset)
130{
131 struct schema_features *rec;
132 char *p;
133
134 rec = calloc(1, sizeof *rec);
135 if (!rec) {
136 YLMSG_E("Unable to allocate features information record (%s).\n", strerror(errno));
137 return -1;
138 }
139 if (ly_set_add(fset, rec, 1, NULL)) {
140 YLMSG_E("Unable to store features information (%s).\n", strerror(errno));
141 free(rec);
142 return -1;
143 }
144
145 /* fill the record */
146 p = strchr(fstring, ':');
147 if (!p) {
148 YLMSG_E("Invalid format of the features specification (%s)", fstring);
149 return -1;
150 }
151 rec->module = strndup(fstring, p - fstring);
152
153 /* start count on 2 to include terminating NULL byte */
154 for (int count = 2; p; ++count) {
155 size_t len = 0;
156 char *token = p + 1;
157 p = strchr(token, ',');
158 if (!p) {
159 /* the last item, if any */
160 len = strlen(token);
161 } else {
162 len = p - token;
163 }
164 if (len) {
165 char **fp = realloc(rec->features, count * sizeof *rec->features);
166 if (!fp) {
167 YLMSG_E("Unable to store features list information (%s).\n", strerror(errno));
168 return -1;
169 }
170 rec->features = fp;
171 rec->features[count - 1] = NULL; /* terminating NULL-byte */
172 fp = &rec->features[count - 2]; /* array item to set */
173 (*fp) = strndup(token, len);
174 }
175 }
176
177 return 0;
178}
179
180struct cmdline_file *
181fill_cmdline_file(struct ly_set *set, struct ly_in *in, const char *path, LYD_FORMAT format)
182{
183 struct cmdline_file *rec;
184
185 rec = malloc(sizeof *rec);
186 if (!rec) {
187 YLMSG_E("Allocating memory for data file information failed.\n");
188 return NULL;
189 }
190 if (set && ly_set_add(set, rec, 1, NULL)) {
191 free(rec);
Michal Vasko7edebb42021-01-25 14:18:46 +0100192 YLMSG_E("Storing data file information failed.\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100193 return NULL;
194 }
195 rec->in = in;
196 rec->path = path;
197 rec->format = format;
198
199 return rec;
200}
201
202void
203free_cmdline_file(void *cmdline_file)
204{
205 struct cmdline_file *rec = (struct cmdline_file *)cmdline_file;
206
207 if (rec) {
208 ly_in_free(rec->in, 1);
209 free(rec);
210 }
211}
212
213void
214free_cmdline(char *argv[])
215{
216 if (argv) {
217 free(argv[0]);
218 free(argv);
219 }
220}
221
222int
223parse_cmdline(const char *cmdline, int *argc_p, char **argv_p[])
224{
225 int count;
226 char **vector;
227 char *ptr;
228 char qmark = 0;
229
230 assert(cmdline);
231 assert(argc_p);
232 assert(argv_p);
233
234 /* init */
235 optind = 0; /* reinitialize getopt() */
236 count = 1;
237 vector = malloc((count + 1) * sizeof *vector);
238 vector[0] = strdup(cmdline);
239
240 /* command name */
241 strtok(vector[0], " ");
242
243 /* arguments */
244 while ((ptr = strtok(NULL, " "))) {
245 size_t len;
246 void *r;
247
248 len = strlen(ptr);
249
250 if (qmark) {
251 /* still in quotated text */
252 /* remove NULL termination of the previous token since it is not a token,
253 * but a part of the quotation string */
254 ptr[-1] = ' ';
255
256 if ((ptr[len - 1] == qmark) && (ptr[len - 2] != '\\')) {
257 /* end of quotation */
258 qmark = 0;
259 /* shorten the argument by the terminating quotation mark */
260 ptr[len - 1] = '\0';
261 }
262 continue;
263 }
264
265 /* another token in cmdline */
266 ++count;
267 r = realloc(vector, (count + 1) * sizeof *vector);
268 if (!r) {
269 YLMSG_E("Memory allocation failed (%s:%d, %s),", __FILE__, __LINE__, strerror(errno));
270 free(vector);
271 return -1;
272 }
273 vector = r;
274 vector[count - 1] = ptr;
275
276 if ((ptr[0] == '"') || (ptr[0] == '\'')) {
277 /* remember the quotation mark to identify end of quotation */
278 qmark = ptr[0];
279
280 /* move the remembered argument after the quotation mark */
281 ++vector[count - 1];
282
283 /* check if the quotation is terminated within this token */
284 if ((ptr[len - 1] == qmark) && (ptr[len - 2] != '\\')) {
285 /* end of quotation */
286 qmark = 0;
287 /* shorten the argument by the terminating quotation mark */
288 ptr[len - 1] = '\0';
289 }
290 }
291 }
292 vector[count] = NULL;
293
294 *argc_p = count;
295 *argv_p = vector;
296
297 return 0;
298}
299
300int
301get_format(const char *filename, LYS_INFORMAT *schema, LYD_FORMAT *data)
302{
303 char *ptr;
304 LYS_INFORMAT informat_s;
305 LYD_FORMAT informat_d;
306
307 /* get the file format */
308 if ((ptr = strrchr(filename, '.')) != NULL) {
309 ++ptr;
310 if (!strcmp(ptr, "yang")) {
311 informat_s = LYS_IN_YANG;
312 informat_d = 0;
313 } else if (!strcmp(ptr, "yin")) {
314 informat_s = LYS_IN_YIN;
315 informat_d = 0;
316 } else if (!strcmp(ptr, "xml")) {
317 informat_s = 0;
318 informat_d = LYD_XML;
319 } else if (!strcmp(ptr, "json")) {
320 informat_s = 0;
321 informat_d = LYD_JSON;
Michal Vaskod3b10542021-02-03 11:31:16 +0100322 } else if (!strcmp(ptr, "lyb")) {
323 informat_s = 0;
324 informat_d = LYD_LYB;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100325 } else {
326 YLMSG_E("Input file \"%s\" in an unknown format \"%s\".\n", filename, ptr);
327 return 0;
328 }
329 } else {
330 YLMSG_E("Input file \"%s\" without file extension - unknown format.\n", filename);
331 return 1;
332 }
333
334 if (informat_d) {
335 if (!data) {
336 YLMSG_E("Input file \"%s\" not expected to contain data instances (unexpected format).\n", filename);
337 return 2;
338 }
339 (*data) = informat_d;
340 } else if (informat_s) {
341 if (!schema) {
342 YLMSG_E("Input file \"%s\" not expected to contain schema definition (unexpected format).\n", filename);
343 return 3;
344 }
345 (*schema) = informat_s;
346 }
347
348 return 0;
349}
350
351int
352print_list(struct ly_out *out, struct ly_ctx *ctx, LYD_FORMAT outformat)
353{
354 struct lyd_node *ylib;
355 uint32_t idx = 0, has_modules = 0;
356 const struct lys_module *mod;
357
358 if (outformat != LYD_UNKNOWN) {
Michal Vasko794ab4b2021-03-31 09:42:19 +0200359 if (ly_ctx_get_yanglib_data(ctx, &ylib, "%u", ly_ctx_get_change_count(ctx))) {
Michal Vaskoc431a0a2021-01-25 14:31:58 +0100360 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 +0100361 return 1;
362 }
363
364 lyd_print_all(out, ylib, outformat, 0);
365 lyd_free_all(ylib);
366 return 0;
367 }
368
369 /* iterate schemas in context and provide just the basic info */
370 ly_print(out, "List of the loaded models:\n");
371 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
372 has_modules++;
373
374 /* conformance print */
375 if (mod->implemented) {
376 ly_print(out, " I");
377 } else {
378 ly_print(out, " i");
379 }
380
381 /* module print */
382 ly_print(out, " %s", mod->name);
383 if (mod->revision) {
384 ly_print(out, "@%s", mod->revision);
385 }
386
387 /* submodules print */
388 if (mod->parsed && mod->parsed->includes) {
389 uint64_t u = 0;
390 ly_print(out, " (");
391 LY_ARRAY_FOR(mod->parsed->includes, u) {
392 ly_print(out, "%s%s", !u ? "" : ",", mod->parsed->includes[u].name);
393 if (mod->parsed->includes[u].rev[0]) {
394 ly_print(out, "@%s", mod->parsed->includes[u].rev);
395 }
396 }
397 ly_print(out, ")");
398 }
399
400 /* finish the line */
401 ly_print(out, "\n");
402 }
403
404 if (!has_modules) {
405 ly_print(out, "\t(none)\n");
406 }
407
408 ly_print_flush(out);
409 return 0;
410}
411
412int
Radek Krejcie9f13b12020-11-09 17:42:04 +0100413evaluate_xpath(const struct lyd_node *tree, const char *xpath)
414{
Radek Krejci89757c12020-11-26 12:07:47 +0100415 struct ly_set *set = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100416
417 if (lyd_find_xpath(tree, xpath, &set)) {
418 return -1;
419 }
420
421 /* print result */
422 printf("XPath \"%s\" evaluation result:\n", xpath);
423 if (!set->count) {
424 printf("\tEmpty\n");
425 } else {
426 for (uint32_t u = 0; u < set->count; ++u) {
427 struct lyd_node *node = (struct lyd_node *)set->objs[u];
428 printf(" %s \"%s\"", lys_nodetype2str(node->schema->nodetype), node->schema->name);
429 if (node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
430 printf(" (value: \"%s\")\n", ((struct lyd_node_term *)node)->value.canonical);
431 } else if (node->schema->nodetype == LYS_LIST) {
432 printf(" (");
433 for (struct lyd_node *key = ((struct lyd_node_inner *)node)->child; key && lysc_is_key(key->schema); key = key->next) {
434 printf("%s\"%s\": \"%s\";", (key != ((struct lyd_node_inner *)node)->child) ? " " : "",
435 key->schema->name, ((struct lyd_node_term *)key)->value.canonical);
436 }
437 printf(")\n");
438 }
439 }
440 }
441
442 ly_set_free(set, NULL);
443 return 0;
444}
445
446LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100447process_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 +0100448 uint32_t options_parse, uint32_t options_validate, uint32_t options_print,
Radek Krejci6784a4e2020-12-09 14:23:05 +0100449 struct cmdline_file *operational_f, struct ly_set *inputs, struct ly_set *xpaths)
Radek Krejcie9f13b12020-11-09 17:42:04 +0100450{
Radek Krejci89757c12020-11-26 12:07:47 +0100451 LY_ERR ret = LY_SUCCESS;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100452 struct lyd_node *tree = NULL, *merged_tree = NULL;
Radek Krejci89757c12020-11-26 12:07:47 +0100453 struct lyd_node *operational = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100454
455 /* additional operational datastore */
456 if (operational_f && operational_f->in) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100457 ret = lyd_parse_data(ctx, NULL, operational_f->in, operational_f->format, LYD_PARSE_ONLY, 0, &operational);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100458 if (ret) {
459 YLMSG_E("Failed to parse operational datastore file \"%s\".\n", operational_f->path);
460 goto cleanup;
461 }
462 }
463
464 for (uint32_t u = 0; u < inputs->count; ++u) {
465 struct cmdline_file *input_f = (struct cmdline_file *)inputs->objs[u];
Radek Krejcie9f13b12020-11-09 17:42:04 +0100466 switch (data_type) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100467 case LYD_TYPE_DATA_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +0100468 ret = lyd_parse_data(ctx, NULL, input_f->in, input_f->format, options_parse, options_validate, &tree);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100469 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100470 case LYD_TYPE_RPC_YANG:
471 case LYD_TYPE_REPLY_YANG:
472 case LYD_TYPE_NOTIF_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +0100473 ret = lyd_parse_op(ctx, NULL, input_f->in, input_f->format, data_type, &tree, NULL);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100474 break;
Radek Krejci89757c12020-11-26 12:07:47 +0100475 default:
476 YLMSG_E("Internal error (%s:%d).\n", __FILE__, __LINE__);
477 goto cleanup;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100478 }
479
480 if (ret) {
481 YLMSG_E("Failed to parse input data file \"%s\".\n", input_f->path);
482 goto cleanup;
483 }
484
485 if (merge) {
486 /* merge the data so far parsed for later validation and print */
487 if (!merged_tree) {
488 merged_tree = tree;
489 } else {
490 ret = lyd_merge_siblings(&merged_tree, tree, LYD_MERGE_DESTRUCT);
491 if (ret) {
492 YLMSG_E("Merging %s with previous data failed.\n", input_f->path);
493 goto cleanup;
494 }
495 }
496 tree = NULL;
497 } else if (format) {
498 lyd_print_all(out, tree, format, options_print);
499 } else if (operational) {
500 /* additional validation of the RPC/Action/reply/Notification with the operational datastore */
501 ret = lyd_validate_op(tree, operational, data_type, NULL);
502 if (ret) {
503 YLMSG_E("Failed to validate input data file \"%s\" with operational datastore \"%s\".\n",
504 input_f->path, operational_f->path);
505 goto cleanup;
506 }
507 }
508 lyd_free_all(tree);
509 tree = NULL;
510 }
511
512 if (merge) {
513 /* validate the merged result */
514 ret = lyd_validate_all(&merged_tree, ctx, LYD_VALIDATE_PRESENT, NULL);
515 if (ret) {
516 YLMSG_E("Merged data are not valid.\n");
517 goto cleanup;
518 }
519
520 if (format) {
521 /* and print it */
522 lyd_print_all(out, merged_tree, format, options_print);
523 }
524
525 for (uint32_t u = 0; u < xpaths->count; ++u) {
526 if (evaluate_xpath(merged_tree, (const char *)xpaths->objs[u])) {
527 goto cleanup;
528 }
529 }
530 }
531
532cleanup:
533 /* cleanup */
534 lyd_free_all(merged_tree);
535 lyd_free_all(tree);
536
537 return ret;
538}