yanglint REFACTOR new yl_opt and cmd callbacks
The yanglint options were stored in non-interactive mode in the
structure named 'context'. This structure was renamed to yl_opt
and is now also used in the interactive mode, which has also been
expanded with new callbacks. These callbacks are the basis for
further refactoring commits, which aim to reduce duplicate code
between interactive and non-interactive modes to a minimum.
diff --git a/tools/lint/cmd_clear.c b/tools/lint/cmd_clear.c
index 403dc26..233cc92 100644
--- a/tools/lint/cmd_clear.c
+++ b/tools/lint/cmd_clear.c
@@ -2,9 +2,10 @@
* @file cmd_clear.c
* @author Michal Vasko <mvasko@cesnet.cz>
* @author Radek Krejci <rkrejci@cesnet.cz>
+ * @author Adam Piecek <piecek@cesnet.cz>
* @brief 'clear' command of the libyang's yanglint tool.
*
- * Copyright (c) 2015-2020 CESNET, z.s.p.o.
+ * Copyright (c) 2015-2023 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
@@ -24,6 +25,7 @@
#include "libyang.h"
#include "common.h"
+#include "yl_opt.h"
void
cmd_clear_help(void)
@@ -45,6 +47,74 @@
" are used, but then deleted.\n");
}
+int
+cmd_clear_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc)
+{
+ int rc = 0, argc = 0;
+ int opt, opt_index;
+ struct option options[] = {
+ {"make-implemented", no_argument, NULL, 'i'},
+ {"yang-library", no_argument, NULL, 'y'},
+ {"yang-library-file", no_argument, NULL, 'Y'},
+ {"help", no_argument, NULL, 'h'},
+ {NULL, 0, NULL, 0}
+ };
+
+ yo->ctx_options = LY_CTX_NO_YANGLIBRARY;
+
+ if ((rc = parse_cmdline(cmdline, &argc, &yo->argv))) {
+ return rc;
+ }
+
+ while ((opt = getopt_long(argc, yo->argv, commands[CMD_CLEAR].optstring, options, &opt_index)) != -1) {
+ switch (opt) {
+ case 'i':
+ if (yo->ctx_options & LY_CTX_REF_IMPLEMENTED) {
+ yo->ctx_options &= ~LY_CTX_REF_IMPLEMENTED;
+ yo->ctx_options |= LY_CTX_ALL_IMPLEMENTED;
+ } else {
+ yo->ctx_options |= LY_CTX_REF_IMPLEMENTED;
+ }
+ break;
+ case 'y':
+ yo->ctx_options &= ~LY_CTX_NO_YANGLIBRARY;
+ break;
+ case 'Y':
+ yo->ctx_options &= ~LY_CTX_NO_YANGLIBRARY;
+ yo->yang_lib_file = optarg;
+ if (!yo->yang_lib_file) {
+ YLMSG_E("Memory allocation error.\n");
+ return 1;
+ }
+ break;
+ case 'h':
+ cmd_clear_help();
+ return 1;
+ default:
+ YLMSG_E("Unknown option.\n");
+ return 1;
+ }
+ }
+
+ *posv = &yo->argv[optind];
+ *posc = argc - optind;
+
+ return rc;
+}
+
+int
+cmd_clear_dep(struct yl_opt *yo, int posc)
+{
+ (void) yo;
+
+ if (posc) {
+ YLMSG_E("No positional arguments are allowed.\n");
+ return 1;
+ }
+
+ return 0;
+}
+
/**
* @brief Convert searchpaths into single string.
*
@@ -70,68 +140,25 @@
return rc;
}
-void
-cmd_clear(struct ly_ctx **ctx, const char *cmdline)
+int
+cmd_clear_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv)
{
- int argc = 0;
- char **argv = NULL;
- int opt, opt_index;
- struct option options[] = {
- {"make-implemented", no_argument, NULL, 'i'},
- {"yang-library", no_argument, NULL, 'y'},
- {"yang-library-file", no_argument, NULL, 'Y'},
- {"help", no_argument, NULL, 'h'},
- {NULL, 0, NULL, 0}
- };
- uint16_t options_ctx = LY_CTX_NO_YANGLIBRARY;
+ (void) posv;
struct ly_ctx *ctx_new = NULL;
- char *ylibfile = NULL;
- char *searchpaths = NULL;
- if (parse_cmdline(cmdline, &argc, &argv)) {
- goto cleanup;
- }
-
- while ((opt = getopt_long(argc, argv, commands[CMD_CLEAR].optstring, options, &opt_index)) != -1) {
- switch (opt) {
- case 'i':
- if (options_ctx & LY_CTX_REF_IMPLEMENTED) {
- options_ctx &= ~LY_CTX_REF_IMPLEMENTED;
- options_ctx |= LY_CTX_ALL_IMPLEMENTED;
- } else {
- options_ctx |= LY_CTX_REF_IMPLEMENTED;
- }
- break;
- case 'y':
- options_ctx &= ~LY_CTX_NO_YANGLIBRARY;
- break;
- case 'Y':
- options_ctx &= ~LY_CTX_NO_YANGLIBRARY;
- ylibfile = optarg;
- break;
- case 'h':
- cmd_clear_help();
- goto cleanup;
- default:
- YLMSG_E("Unknown option.\n");
- goto cleanup;
- }
- }
-
- if (ylibfile) {
- /* Create context according to the provided yang-library data in a file but use already defined searchpaths. */
- if (searchpaths_to_str(*ctx, &searchpaths)) {
+ if (yo->yang_lib_file) {
+ if (searchpaths_to_str(*ctx, &yo->searchpaths)) {
YLMSG_E("Storing searchpaths failed.\n");
- goto cleanup;
+ return 1;
}
- if (ly_ctx_new_ylpath(searchpaths, ylibfile, LYD_UNKNOWN, options_ctx, &ctx_new)) {
+ if (ly_ctx_new_ylpath(yo->searchpaths, yo->yang_lib_file, LYD_UNKNOWN, yo->ctx_options, &ctx_new)) {
YLMSG_E("Unable to create libyang context with yang-library data.\n");
- goto cleanup;
+ return 1;
}
} else {
- if (ly_ctx_new(NULL, options_ctx, &ctx_new)) {
+ if (ly_ctx_new(NULL, yo->ctx_options, &ctx_new)) {
YLMSG_W("Failed to create context.\n");
- goto cleanup;
+ return 1;
}
}
@@ -141,7 +168,5 @@
ly_ctx_destroy(*ctx);
*ctx = ctx_new;
-cleanup:
- free_cmdline(argv);
- free(searchpaths);
+ return 0;
}