yanglint FEATURE --in-format for non-interact mode
diff --git a/tools/lint/common.c b/tools/lint/common.c
index 2f2905a..1ba4a1d 100644
--- a/tools/lint/common.c
+++ b/tools/lint/common.c
@@ -73,11 +73,8 @@
return -1;
}
- if ((format_schema && !*format_schema) || (format_data && !*format_data)) {
- /* get the file format */
- if (get_format(filepath, format_schema, format_data)) {
- return -1;
- }
+ if (get_format(filepath, format_schema, format_data)) {
+ return -1;
}
if (ly_in_new_filepath(filepath, 0, in)) {
@@ -448,52 +445,76 @@
return 0;
}
-int
-get_format(const char *filename, LYS_INFORMAT *schema, LYD_FORMAT *data)
+LYS_INFORMAT
+get_schema_format(const char *filename)
{
char *ptr;
- LYS_INFORMAT informat_s;
- LYD_FORMAT informat_d;
- /* get the file format */
if ((ptr = strrchr(filename, '.')) != NULL) {
++ptr;
if (!strcmp(ptr, "yang")) {
- informat_s = LYS_IN_YANG;
- informat_d = 0;
+ return LYS_IN_YANG;
} else if (!strcmp(ptr, "yin")) {
- informat_s = LYS_IN_YIN;
- informat_d = 0;
- } else if (!strcmp(ptr, "xml")) {
- informat_s = 0;
- informat_d = LYD_XML;
- } else if (!strcmp(ptr, "json")) {
- informat_s = 0;
- informat_d = LYD_JSON;
- } else if (!strcmp(ptr, "lyb")) {
- informat_s = 0;
- informat_d = LYD_LYB;
+ return LYS_IN_YIN;
} else {
- YLMSG_E("Input file \"%s\" in an unknown format \"%s\".\n", filename, ptr);
- return 0;
+ return LYS_IN_UNKNOWN;
}
} else {
- YLMSG_E("Input file \"%s\" without file extension - unknown format.\n", filename);
- return 1;
+ return LYS_IN_UNKNOWN;
+ }
+}
+
+LYD_FORMAT
+get_data_format(const char *filename)
+{
+ char *ptr;
+
+ if ((ptr = strrchr(filename, '.')) != NULL) {
+ ++ptr;
+ if (!strcmp(ptr, "xml")) {
+ return LYD_XML;
+ } else if (!strcmp(ptr, "json")) {
+ return LYD_JSON;
+ } else if (!strcmp(ptr, "lyb")) {
+ return LYD_LYB;
+ } else {
+ return LYD_UNKNOWN;
+ }
+ } else {
+ return LYD_UNKNOWN;
+ }
+}
+
+int
+get_format(const char *filepath, LYS_INFORMAT *schema_form, LYD_FORMAT *data_form)
+{
+ LYS_INFORMAT schema;
+ LYD_FORMAT data;
+
+ schema = !schema_form || !*schema_form ? LYS_IN_UNKNOWN : *schema_form;
+ data = !data_form || !*data_form ? LYD_UNKNOWN : *data_form;
+
+ if (!schema) {
+ schema = get_schema_format(filepath);
+ }
+ if (!data) {
+ data = get_data_format(filepath);
}
- if (informat_d) {
- if (!data) {
- YLMSG_E("Input file \"%s\" not expected to contain data instances (unexpected format).\n", filename);
- return 2;
- }
- (*data) = informat_d;
- } else if (informat_s) {
- if (!schema) {
- YLMSG_E("Input file \"%s\" not expected to contain schema definition (unexpected format).\n", filename);
- return 3;
- }
- (*schema) = informat_s;
+ if (!schema && !data) {
+ YLMSG_E("Input schema format for %s file not recognized.", filepath);
+ return -1;
+ } else if (!data && !schema) {
+ YLMSG_E("Input data format for %s file not recognized.", filepath);
+ return -1;
+ }
+ assert(schema || data);
+
+ if (schema_form) {
+ *schema_form = schema;
+ }
+ if (data_form) {
+ *data_form = data;
}
return 0;
diff --git a/tools/lint/common.h b/tools/lint/common.h
index 0a7c54f..6f117bc 100644
--- a/tools/lint/common.h
+++ b/tools/lint/common.h
@@ -204,16 +204,33 @@
void free_cmdline(char *argv[]);
/**
- * @brief Get expected format of the @p filename's content according to the @p filename's suffix.
+ * @brief Get schema format of the @p filename's content according to the @p filename's suffix.
+ *
* @param[in] filename Name of the file to examine.
- * @param[out] schema Pointer to a variable to store the expected input schema format. Do not provide the pointer in case a
- * schema format is not expected.
- * @param[out] data Pointer to a variable to store the expected input data format. Do not provide the pointer in case a data
- * format is not expected.
+ * @return Detected schema input format.
+ */
+LYS_INFORMAT get_schema_format(const char *filename);
+
+/**
+ * @brief Get data format of the @p filename's content according to the @p filename's suffix.
+ *
+ * @param[in] filename Name of the file to examine.
+ * @return Detected data input format.
+ */
+LYD_FORMAT get_data_format(const char *filename);
+
+/**
+ * @brief Get format of the @p filename's content according to the @p filename's suffix.
+ *
+ * Either the @p schema or @p data parameter is set.
+ *
+ * @param[in] filename Name of the file to examine.
+ * @param[out] schema_form Pointer to a variable to store the input schema format.
+ * @param[out] data_form Pointer to a variable to store the expected input data format.
* @return zero in case a format was successfully detected.
* @return nonzero in case it is not possible to get valid format from the @p filename.
*/
-int get_format(const char *filename, LYS_INFORMAT *schema, LYD_FORMAT *data);
+int get_format(const char *filepath, LYS_INFORMAT *schema_form, LYD_FORMAT *data_form);
/**
* @brief Print list of schemas in the context.
diff --git a/tools/lint/main_ni.c b/tools/lint/main_ni.c
index 22456b0..184ab57 100644
--- a/tools/lint/main_ni.c
+++ b/tools/lint/main_ni.c
@@ -94,6 +94,9 @@
/* value of --format in case of data format */
LYD_FORMAT data_out_format;
+ /* value of --in-format in case of data format */
+ LYD_FORMAT data_in_format;
+
/* input data files (struct cmdline_file *) */
struct ly_set data_inputs;
@@ -177,6 +180,11 @@
" yang, yin, tree, info and feature-param for schemas,\n"
" xml, json, and lyb for data.\n\n");
+ printf(" -I FORMAT, --in-format=FORMAT\n"
+ " Load the data in one of the following formats:\n"
+ " xml, json, lyb\n"
+ " If input format not specified, it is detected from the file extension.\n\n");
+
printf(" -p PATH, --path=PATH\n"
" Search path for schema (YANG/YIN) modules. The option can be\n"
" used multiple times. The current working directory and the\n"
@@ -440,7 +448,7 @@
for (int i = 0; i < argc - optind; i++) {
LYS_INFORMAT format_schema = LYS_IN_UNKNOWN;
- LYD_FORMAT format_data = LYD_UNKNOWN;
+ LYD_FORMAT format_data = c->data_in_format;
if (get_input(argv[optind + i], &format_schema, &format_data, &in)) {
goto error;
@@ -561,6 +569,7 @@
{"disable-searchdir", no_argument, NULL, 'D'},
{"features", required_argument, NULL, 'F'},
{"make-implemented", no_argument, NULL, 'i'},
+ {"in-format", required_argument, NULL, 'I'},
{"schema-node", required_argument, NULL, 'P'},
{"single-node", no_argument, NULL, 'q'},
{"submodule", required_argument, NULL, 's'},
@@ -594,9 +603,9 @@
opterr = 0;
#ifndef NDEBUG
- while ((opt = getopt_long(argc, argv, "hvVQf:p:DF:iP:qs:neE:t:d:lL:o:O:R:myY:Xx:G:", options, &opt_index)) != -1)
+ while ((opt = getopt_long(argc, argv, "hvVQf:I:p:DF:iP:qs:neE:t:d:lL:o:O:R:myY:Xx:G:", options, &opt_index)) != -1)
#else
- while ((opt = getopt_long(argc, argv, "hvVQf:p:DF:iP:qs:neE:t:d:lL:o:O:R:myY:Xx:", options, &opt_index)) != -1)
+ while ((opt = getopt_long(argc, argv, "hvVQf:I:p:DF:iP:qs:neE:t:d:lL:o:O:R:myY:Xx:", options, &opt_index)) != -1)
#endif
{
switch (opt) {
@@ -662,6 +671,20 @@
}
break;
+ case 'I': /* --in-format */
+ if (!strcasecmp(optarg, "xml")) {
+ c->data_in_format = LYD_XML;
+ } else if (!strcasecmp(optarg, "json")) {
+ c->data_in_format = LYD_JSON;
+ } else if (!strcasecmp(optarg, "lyb")) {
+ c->data_in_format = LYD_LYB;
+ } else {
+ YLMSG_E("Unknown input format %s\n", optarg);
+ help(1);
+ return -1;
+ }
+ break;
+
case 'p': { /* --path */
struct stat st;
diff --git a/tools/lint/tests/non-interactive/data_in_format.test b/tools/lint/tests/non-interactive/data_in_format.test
new file mode 100644
index 0000000..f1336dd
--- /dev/null
+++ b/tools/lint/tests/non-interactive/data_in_format.test
@@ -0,0 +1,18 @@
+source [expr {[info exists ::env(TESTS_DIR)] ? "$env(TESTS_DIR)/non-interactive/ly.tcl" : "ly.tcl"}]
+
+set mdir $::env(YANG_MODULES_DIR)
+set ddir $::env(TESTS_DIR)/data
+
+test data_in_format_xml {--in-format xml} {
+ ly_cmd "-I xml $mdir/modleaf.yang $ddir/modleaf.dxml"
+ ly_cmd_err "-I json $mdir/modleaf.yang $ddir/modleaf.dxml" "Failed to parse"
+ ly_cmd_err "-I lyb $mdir/modleaf.yang $ddir/modleaf.dxml" "Failed to parse"
+} {}
+
+test data_in_format_json {--in-format json} {
+ ly_cmd "-I json $mdir/modleaf.yang $ddir/modleaf.djson"
+ ly_cmd_err "-I xml $mdir/modleaf.yang $ddir/modleaf.djson" "Failed to parse"
+ ly_cmd_err "-I lyb $mdir/modleaf.yang $ddir/modleaf.djson" "Failed to parse"
+} {}
+
+cleanupTests