From: "Ricardo B. Marlière" <ricardo@marliere.net>
To: "Cyril Hrubis" <chrubis@suse.cz>, <ltp@lists.linux.it>
Cc: ltp <ltp-bounces+ricardo=marliere.net@lists.linux.it>
Subject: Re: [LTP] [PATCH 1/2] metaparse: Add shell test parser
Date: Thu, 13 Feb 2025 09:48:44 -0300 [thread overview]
Message-ID: <D7RBW3C1993Z.2F0MWK8SORBO4@marliere.net> (raw)
In-Reply-To: <20250212131618.6810-2-chrubis@suse.cz>
On Wed Feb 12, 2025 at 10:16 AM -03, Cyril Hrubis wrote:
> 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;
> + }
> +
> +
nit: double blank line here
> + 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 '}'
--
Mailing list info: https://lists.linux.it/listinfo/ltp
next prev parent reply other threads:[~2025-02-13 12:49 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-12 13:16 [LTP] [PATCH 0/2] Add shell test metadata parser Cyril Hrubis
2025-02-12 13:16 ` [LTP] [PATCH 1/2] metaparse: Add shell test parser Cyril Hrubis
2025-02-12 14:03 ` Petr Vorel
2025-02-13 12:48 ` Ricardo B. Marlière [this message]
2025-02-12 13:16 ` [LTP] [PATCH 2/2] metaparse: data_storage: Fix warning Cyril Hrubis
2025-02-12 14:04 ` Petr Vorel
2025-02-13 12:41 ` [LTP] [PATCH 0/2] Add shell test metadata parser Ricardo B. Marlière
2025-02-13 14:06 ` Cyril Hrubis
2025-02-25 13:14 ` Cyril Hrubis
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=D7RBW3C1993Z.2F0MWK8SORBO4@marliere.net \
--to=ricardo@marliere.net \
--cc=chrubis@suse.cz \
--cc=ltp-bounces+ricardo=marliere.net@lists.linux.it \
--cc=ltp@lists.linux.it \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.