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