Skip to content

Commit d7375b4

Browse files
committed
metaparse: Add shell test parser
Signed-off-by: Cyril Hrubis <chrubis@suse.cz> Reviewed-by: Petr Vorel <pvorel@suse.cz> Reviewed-by: Ricardo B. Marlière <ricardo@marliere.net> Tested-by: Ricardo B. Marlière <ricardo@marliere.net>
1 parent 18d5024 commit d7375b4

File tree

4 files changed

+149
-2
lines changed

4 files changed

+149
-2
lines changed

metadata/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
metaparse
2+
metaparse-sh
23
ltp.json

metadata/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ include $(top_srcdir)/include/mk/env_pre.mk
77
include $(top_srcdir)/include/mk/functions.mk
88

99
MAKE_TARGETS := ltp.json
10-
HOST_MAKE_TARGETS := metaparse
10+
HOST_MAKE_TARGETS := metaparse metaparse-sh
1111
INSTALL_DIR = metadata
1212

1313
.PHONY: ltp.json
1414

15-
ltp.json: metaparse
15+
ltp.json: metaparse metaparse-sh
1616
$(abs_srcdir)/parse.sh > ltp.json
1717

1818
test:

metadata/metaparse-sh.c

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* Copyright (c) 2025 Cyril Hrubis <chrubis@suse.cz>
4+
*/
5+
6+
#include <stdio.h>
7+
#include <string.h>
8+
#include <errno.h>
9+
#include <libgen.h>
10+
11+
#include "data_storage.h"
12+
13+
static int started;
14+
15+
static void json_start(char *path)
16+
{
17+
if (started)
18+
return;
19+
20+
started = 1;
21+
22+
printf(" \"%s\": {\n", basename(path));
23+
}
24+
25+
static void json_finish(const char *path)
26+
{
27+
if (!started)
28+
return;
29+
30+
printf(" \"fname\": \"%s\"\n", path);
31+
printf(" }");
32+
}
33+
34+
enum state {
35+
NONE,
36+
START,
37+
DOC_FIRST,
38+
DOC,
39+
ENV_START,
40+
ENV_FIRST,
41+
ENV
42+
};
43+
44+
static void parse_shell(char *path)
45+
{
46+
char line[4096];
47+
FILE *f = fopen(path, "r");
48+
enum state state = NONE;
49+
50+
if (!f) {
51+
fprintf(stderr, "Failed to open '%s': %s\n",
52+
path, strerror(errno));
53+
exit(1);
54+
}
55+
56+
while (fgets(line, sizeof(line), f)) {
57+
/* Strip newline */
58+
line[strlen(line)-1] = 0;
59+
60+
switch (state) {
61+
case NONE:
62+
if (!strcmp(line, "# ---"))
63+
state = START;
64+
break;
65+
case START:
66+
if (!strcmp(line, "# doc")) {
67+
json_start(path);
68+
state = DOC_FIRST;
69+
printf(" \"doc\": [\n");
70+
} else if (!strcmp(line, "# env")) {
71+
json_start(path);
72+
state = ENV_START;
73+
} else {
74+
state = NONE;
75+
}
76+
break;
77+
case DOC:
78+
case DOC_FIRST:
79+
if (!strcmp(line, "# ---")) {
80+
state = NONE;
81+
printf("\n ],\n");
82+
continue;
83+
}
84+
85+
if (state == DOC_FIRST)
86+
state = DOC;
87+
else
88+
printf(",\n");
89+
90+
data_fprintf_esc(stdout, 4, line+2);
91+
break;
92+
case ENV_START:
93+
if (!strcmp(line, "# {")) {
94+
state = ENV_FIRST;
95+
} else {
96+
fprintf(stderr,
97+
"%s: Invalid line in JSON block '%s'",
98+
path, line);
99+
}
100+
break;
101+
case ENV:
102+
case ENV_FIRST:
103+
if (!strcmp(line, "# }")) {
104+
state = NONE;
105+
printf(",\n");
106+
continue;
107+
}
108+
109+
if (state == ENV_FIRST)
110+
state = ENV;
111+
else
112+
printf("\n");
113+
114+
line[0] = ' ';
115+
line[1] = ' ';
116+
117+
printf("%s", line);
118+
break;
119+
}
120+
}
121+
122+
json_finish(path);
123+
}
124+
125+
int main(int argc, char *argv[])
126+
{
127+
int i;
128+
129+
for (i = 1; i < argc; i++)
130+
parse_shell(argv[i]);
131+
132+
return 0;
133+
}

metadata/parse.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ EOF
4242
fi
4343
done
4444

45+
for test in `find testcases/ -not -path "testcases/lib/*" -name '*.sh'|sort`; do
46+
a=$($top_builddir/metadata/metaparse-sh "$test")
47+
if [ -n "$a" ]; then
48+
if [ -z "$first" ]; then
49+
echo ','
50+
fi
51+
first=
52+
cat <<EOF
53+
$a
54+
EOF
55+
fi
56+
done
57+
4558
echo
4659
echo ' }'
4760
echo '}'

0 commit comments

Comments
 (0)