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_searchpath.c b/tools/lint/cmd_searchpath.c
index b3a83ad..15c98a1 100644
--- a/tools/lint/cmd_searchpath.c
+++ b/tools/lint/cmd_searchpath.c
@@ -2,9 +2,10 @@
* @file cmd_searchpath.c
* @author Michal Vasko <mvasko@cesnet.cz>
* @author Radek Krejci <rkrejci@cesnet.cz>
+ * @author Adam Piecek <piecek@cesnet.cz>
* @brief 'searchpath' 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_searchpath_help(void)
@@ -36,39 +38,49 @@
" to be loaded.\n");
}
-void
-cmd_searchpath(struct ly_ctx **ctx, const char *cmdline)
+int
+cmd_searchpath_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc)
{
- int argc = 0;
- char **argv = NULL;
+ int rc = 0, argc = 0;
int opt, opt_index;
struct option options[] = {
{"clear", no_argument, NULL, 'c'},
{"help", no_argument, NULL, 'h'},
{NULL, 0, NULL, 0}
};
- int8_t cleared = 0;
- if (parse_cmdline(cmdline, &argc, &argv)) {
- goto cleanup;
+ if ((rc = parse_cmdline(cmdline, &argc, &yo->argv))) {
+ return rc;
}
- while ((opt = getopt_long(argc, argv, commands[CMD_SEARCHPATH].optstring, options, &opt_index)) != -1) {
+ while ((opt = getopt_long(argc, yo->argv, commands[CMD_SEARCHPATH].optstring, options, &opt_index)) != -1) {
switch (opt) {
case 'c':
- ly_ctx_unset_searchdir(*ctx, NULL);
- cleared = 1;
+ yo->searchdir_unset = 1;
break;
case 'h':
cmd_searchpath_help();
- goto cleanup;
+ return 1;
default:
YLMSG_E("Unknown option.\n");
- goto cleanup;
+ return 1;
}
}
- if (!cleared && (argc == optind)) {
+ *posv = &yo->argv[optind];
+ *posc = argc - optind;
+
+ return 0;
+}
+
+int
+cmd_searchpath_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv)
+{
+ int rc = 0;
+
+ if (yo->searchdir_unset) {
+ ly_ctx_unset_searchdir(*ctx, NULL);
+ } else if (!yo->searchdir_unset && !posv) {
/* no argument - print the paths */
const char * const *dirs = ly_ctx_get_searchdirs(*ctx);
@@ -76,15 +88,9 @@
for (uint32_t i = 0; dirs[i]; ++i) {
printf(" %s\n", dirs[i]);
}
- goto cleanup;
+ } else {
+ rc = ly_ctx_set_searchdir(*ctx, posv);
}
- for (int i = 0; i < argc - optind; i++) {
- if (ly_ctx_set_searchdir(*ctx, argv[optind + i])) {
- goto cleanup;
- }
- }
-
-cleanup:
- free_cmdline(argv);
+ return rc;
}