mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 2/2] commands: add new tutorial command
Date: Sat,  8 Jan 2022 18:15:55 +0100	[thread overview]
Message-ID: <20220108171555.588426-2-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20220108171555.588426-1-a.fatoum@pengutronix.de>

We have a web demo at http://barebox.org/jsbarebox and it would
be nice to have a tutorial there. Add a new tutorial command that
cycles through a number of tips.

Tutorial will follow later.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 commands/Kconfig    |   7 ++
 commands/Makefile   |   1 +
 commands/tutorial.c | 160 ++++++++++++++++++++++++++++++++++++++++++++
 common/complete.c   |  21 ++++--
 include/complete.h  |   1 +
 5 files changed, 184 insertions(+), 6 deletions(-)
 create mode 100644 commands/tutorial.c

diff --git a/commands/Kconfig b/commands/Kconfig
index 9abd97271952..87583db31f90 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -23,6 +23,13 @@ menu "Commands"
 
 menu "Information"
 
+config CMD_TUTORIAL
+	bool "barebox tutorial (next command)"
+	default SANDBOX
+	help
+	  Help navigate a barebox tutorial available
+	  in /env/data/tutorial/
+
 config CMD_AT91CLK
 	bool "at91clk"
 	default y
diff --git a/commands/Makefile b/commands/Makefile
index 875826743ffe..db78d0b877f6 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -135,5 +135,6 @@ obj-$(CONFIG_CMD_IP_ROUTE_GET)  += ip-route-get.o
 obj-$(CONFIG_CMD_BTHREAD)	+= bthread.o
 obj-$(CONFIG_CMD_UBSAN)		+= ubsan.o
 obj-$(CONFIG_CMD_SELFTEST)	+= selftest.o
+obj-$(CONFIG_CMD_TUTORIAL)	+= tutorial.o
 
 UBSAN_SANITIZE_ubsan.o := y
diff --git a/commands/tutorial.c b/commands/tutorial.c
new file mode 100644
index 000000000000..81e2f93716cd
--- /dev/null
+++ b/commands/tutorial.c
@@ -0,0 +1,160 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+// SPDX-FileCopyrightText: (c) 2021 Ahmad Fatoum
+
+#include <common.h>
+#include <command.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <complete.h>
+#include <sys/stat.h>
+#include <glob.h>
+#include <getopt.h>
+#include <magicvar.h>
+#include <globalvar.h>
+
+static int next_step;
+BAREBOX_MAGICVAR(global.tutorial.step, "Next tutorial step.");
+
+#define BUFSIZE 1024
+
+static glob_t steps;
+
+static int register_tutorial_vars(void)
+{
+	char *oldcwd;
+	int ret = 0;
+
+	oldcwd = pushd("/env/data/tutorial");
+	if (!oldcwd)
+		return 0;
+
+	ret = glob("*", 0, NULL, &steps);
+	if (ret)
+		goto out;
+
+	ret = globalvar_add_simple_enum("tutorial.step", &next_step,
+					(const char **)steps.gl_pathv, steps.gl_pathc);
+
+out:
+	popd(oldcwd);
+	return ret;
+
+}
+postenvironment_initcall(register_tutorial_vars);
+
+static int print_tutorial_step(const char *step)
+{
+	bool highlight = false;
+	int fd, ret, i;
+	char *buf;
+
+	fd = open(step, O_RDONLY);
+	if (fd < 0) {
+		printf("could not open /env/data/tutorial/%s\n", step);
+		return -errno;
+	}
+
+	buf = malloc(BUFSIZE);
+	if (!buf) {
+		ret = -ENOMEM;
+		goto out;
+	}
+
+	printf("%s\n", step);
+
+	while((ret = read(fd, buf, BUFSIZE)) > 0) {
+		for(i = 0; i < ret; i++) {
+			if (buf[i] != '`') {
+				putchar(buf[i]);
+				continue;
+			}
+
+			puts(highlight ? "\e[0m" : "\e[1;31m");
+			highlight = !highlight;
+		}
+	}
+
+	free(buf);
+out:
+	close(fd);
+
+	return ret;
+}
+
+static int do_tutorial_next(int argc, char *argv[])
+{
+	int opt, i;
+	char *step = NULL;
+	char *oldcwd;
+	ssize_t ret = 0;
+	bool is_prev = *argv[0] == 'p';
+
+	while ((opt = getopt(argc, argv, "rh")) > 0) {
+		switch (opt) {
+		case 'r':
+			if (steps.gl_pathc) {
+				globalvar_remove("tutorial.step");
+				next_step = 0;
+				globfree(&steps);
+				steps.gl_pathc = 0;
+			}
+			ret = register_tutorial_vars();
+			if (ret == 0 && steps.gl_pathc == 0)
+				ret = -ENOENT;
+			if (ret)
+				printf("could not load /env/data/tutorial/\n");
+			return ret;
+		case 'h':
+		default:
+			return COMMAND_ERROR_USAGE;
+		}
+	}
+
+	if (optind > argc + 1)
+		return COMMAND_ERROR_USAGE;
+
+	oldcwd = pushd("/env/data/tutorial");
+	if (!oldcwd)
+		return ret;
+
+	if (is_prev)
+		next_step = next_step > 0 ? next_step - 1 : 0;
+
+	if (optind == argc) {
+		step = steps.gl_pathv[next_step];
+		ret = print_tutorial_step(step);
+		if (ret == 0 && !is_prev)
+			next_step = (next_step + 1) % steps.gl_pathc;
+	} else {
+		for (i = optind; i < argc; i++) {
+			step = strdup(argv[i]);
+			ret = print_tutorial_step(step);
+			if (ret)
+				break;
+		}
+	}
+
+	popd(oldcwd);
+
+	return ret;
+}
+
+BAREBOX_CMD_HELP_START(next)
+BAREBOX_CMD_HELP_TEXT("Help navigate the barebox tutorial")
+BAREBOX_CMD_HELP_TEXT("Without a parameter, the next or previous tutorial step is printed")
+BAREBOX_CMD_HELP_TEXT("Options:")
+BAREBOX_CMD_HELP_OPT("-h\t", "show usage text")
+BAREBOX_CMD_HELP_OPT("-r\t", "rewind and reload tutorial")
+BAREBOX_CMD_HELP_END
+
+static __maybe_unused const char *const prev_alias[] = { "prev", NULL};
+
+BAREBOX_CMD_START(next)
+	.cmd		= do_tutorial_next,
+	.aliases	= prev_alias,
+	BAREBOX_CMD_DESC("print next tip for barebox tutorial")
+	BAREBOX_CMD_OPTS("[-hr] [STEP]")
+	BAREBOX_CMD_HELP(cmd_next_help)
+	BAREBOX_CMD_GROUP(CMD_GRP_INFO)
+	BAREBOX_CMD_COMPLETE(tutorial_complete)
+BAREBOX_CMD_END
diff --git a/common/complete.c b/common/complete.c
index e504b7560668..17c0ddb04a02 100644
--- a/common/complete.c
+++ b/common/complete.c
@@ -14,17 +14,18 @@
 #include <command.h>
 #include <environment.h>
 
-static int file_complete(struct string_list *sl, char *instr, int exec)
+static int file_complete(struct string_list *sl, char *instr,
+			 const char *dirn, int exec)
 {
 	char *path = strdup(instr);
 	struct stat s;
 	DIR *dir;
 	struct dirent *d;
 	char tmp[PATH_MAX];
-	char *base, *dirn;
+	char *base;
 
 	base = basename(instr);
-	dirn = dirname(path);
+	dirn = dirn ?: dirname(path);
 
 	dir = opendir(dirn);
 	if (!dir)
@@ -250,7 +251,15 @@ EXPORT_SYMBOL(devicetree_complete);
 int devicetree_file_complete(struct string_list *sl, char *instr)
 {
 	devicetree_complete(sl, instr);
-	file_complete(sl, instr, 0);
+	file_complete(sl, instr, NULL, 0);
+
+	return 0;
+}
+EXPORT_SYMBOL(devicetree_file_complete);
+
+int tutorial_complete(struct string_list *sl, char *instr)
+{
+	file_complete(sl, instr, "/env/data/tutorial", 0);
 
 	return 0;
 }
@@ -392,11 +401,11 @@ int complete(char *instr, char **outstr)
 	if (!instr) {
 		instr = t;
 		if (t && (t[0] == '/' || !strncmp(t, "./", 2))) {
-			file_complete(&sl, t, 1);
+			file_complete(&sl, t, NULL, 1);
 			instr = t;
 		} else if ((t = strrchr(t, ' '))) {
 			t++;
-			file_complete(&sl, t, 0);
+			file_complete(&sl, t, NULL, 0);
 			instr = t;
 		} else {
 			command_complete(&sl, instr);
diff --git a/include/complete.h b/include/complete.h
index 75a92fc86aa0..b0e675b5599f 100644
--- a/include/complete.h
+++ b/include/complete.h
@@ -23,5 +23,6 @@ int devicetree_nodepath_complete(struct string_list *sl, char *instr);
 int devicetree_complete(struct string_list *sl, char *instr);
 int devicetree_file_complete(struct string_list *sl, char *instr);
 int env_param_noeval_complete(struct string_list *sl, char *instr);
+int tutorial_complete(struct string_list *sl, char *instr);
 
 #endif /* __COMPLETE_ */
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


  reply	other threads:[~2022-01-08 17:18 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-08 17:15 [PATCH 1/2] fs: implement pushd/popd chdir wrappers Ahmad Fatoum
2022-01-08 17:15 ` Ahmad Fatoum [this message]
2022-01-12 10:29 ` Sascha Hauer

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=20220108171555.588426-2-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox