[LTP] [PATCH 1/2] metaparse: Add shell test parser
Cyril Hrubis
chrubis@suse.cz
Wed Feb 12 14:16:17 CET 2025
Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
---
metadata/.gitignore | 1 +
metadata/Makefile | 4 +-
metadata/metaparse-sh.c | 127 ++++++++++++++++++++++++++++++++++++++++
metadata/parse.sh | 13 ++++
4 files changed, 143 insertions(+), 2 deletions(-)
create mode 100644 metadata/metaparse-sh.c
diff --git a/metadata/.gitignore b/metadata/.gitignore
index 07d2fd6ff..bb6399e5c 100644
--- a/metadata/.gitignore
+++ b/metadata/.gitignore
@@ -1,2 +1,3 @@
metaparse
+metaparse-sh
ltp.json
diff --git a/metadata/Makefile b/metadata/Makefile
index 522af4270..641657318 100644
--- a/metadata/Makefile
+++ b/metadata/Makefile
@@ -7,12 +7,12 @@ include $(top_srcdir)/include/mk/env_pre.mk
include $(top_srcdir)/include/mk/functions.mk
MAKE_TARGETS := ltp.json
-HOST_MAKE_TARGETS := metaparse
+HOST_MAKE_TARGETS := metaparse metaparse-sh
INSTALL_DIR = metadata
.PHONY: ltp.json
-ltp.json: metaparse
+ltp.json: metaparse metaparse-sh
$(abs_srcdir)/parse.sh > ltp.json
ifeq ($(WITH_METADATA),yes)
mkdir -p $(abs_top_builddir)/docparse
diff --git a/metadata/metaparse-sh.c b/metadata/metaparse-sh.c
new file mode 100644
index 000000000..9eb38f583
--- /dev/null
+++ b/metadata/metaparse-sh.c
@@ -0,0 +1,127 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2025 Cyril Hrubis <chrubis@suse.cz>
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <libgen.h>
+
+#include "data_storage.h"
+
+static int started;
+
+static void json_start(char *path)
+{
+ if (started)
+ return;
+
+ started = 1;
+
+ printf(" \"%s\": {\n", basename(path));
+}
+
+static void json_finish(const char *path)
+{
+ if (!started)
+ return;
+
+ printf(" \"fname\": \"%s\"\n", path);
+ printf(" }");
+}
+
+enum state {
+ NONE,
+ START,
+ DOC_FIRST,
+ DOC,
+ ENV_START,
+ ENV_FIRST,
+ ENV
+};
+
+static void parse_shell(char *path)
+{
+ char line[4096];
+ FILE *f = fopen(path, "r");
+ enum state state = NONE;
+
+ while (fgets(line, sizeof(line), f)) {
+ /* Strip newline */
+ line[strlen(line)-1] = 0;
+
+ switch (state) {
+ case NONE:
+ if (!strcmp(line, "# ---"))
+ state = START;
+ break;
+ case START:
+ if (!strcmp(line, "# doc")) {
+ json_start(path);
+ state = DOC_FIRST;
+ printf(" \"doc\": [\n");
+ } else if (!strcmp(line, "# env")) {
+ json_start(path);
+ state = ENV_START;
+ } else {
+ state = NONE;
+ }
+ break;
+ case DOC:
+ case DOC_FIRST:
+ if (!strcmp(line, "# ---")) {
+ state = NONE;
+ printf("\n ],\n");
+ continue;
+ }
+
+ if (state == DOC_FIRST)
+ state = DOC;
+ else
+ printf(",\n");
+
+ data_fprintf_esc(stdout, 4, line+2);
+ break;
+ case ENV_START:
+ if (!strcmp(line, "# {")) {
+ state = ENV_FIRST;
+ } else {
+ fprintf(stderr,
+ "%s: Invalid line in JSON block '%s'",
+ path, line);
+ }
+ break;
+ case ENV:
+ case ENV_FIRST:
+ if (!strcmp(line, "# }")) {
+ state = NONE;
+ printf(",\n");
+ continue;
+ }
+
+
+ if (state == ENV_FIRST)
+ state = ENV;
+ else
+ printf("\n");
+
+ line[0] = ' ';
+ line[1] = ' ';
+
+ printf("%s", line);
+ break;
+ }
+ }
+
+ json_finish(path);
+}
+
+int main(int argc, char *argv[])
+{
+ int i;
+
+ for (i = 1; i < argc; i++)
+ parse_shell(argv[i]);
+
+ return 0;
+}
diff --git a/metadata/parse.sh b/metadata/parse.sh
index 7db2e2415..45776e4d0 100755
--- a/metadata/parse.sh
+++ b/metadata/parse.sh
@@ -42,6 +42,19 @@ EOF
fi
done
+for test in `find testcases/ -not -path "testcases/lib/*" -name '*.sh'|sort`; do
+ a=$($top_builddir/metadata/metaparse-sh "$test")
+ if [ -n "$a" ]; then
+ if [ -z "$first" ]; then
+ echo ','
+ fi
+ first=
+ cat <<EOF
+$a
+EOF
+ fi
+done
+
echo
echo ' }'
echo '}'
--
2.45.3
More information about the ltp
mailing list