* [PATCH 1/2] fs: implement pushd/popd chdir wrappers
@ 2022-01-08 17:15 Ahmad Fatoum
2022-01-08 17:15 ` [PATCH 2/2] commands: add new tutorial command Ahmad Fatoum
2022-01-12 10:29 ` [PATCH 1/2] fs: implement pushd/popd chdir wrappers Sascha Hauer
0 siblings, 2 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2022-01-08 17:15 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
We don't have dirfd's, so running operations relative to the current
working directory always involves some allocation/freeing boilerplate.
Add pushd/popd helpers to move the boilerplate out of the callsites.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
fs/fs.c | 30 ++++++++++++++++++++++++++++++
include/unistd.h | 2 ++
2 files changed, 32 insertions(+)
diff --git a/fs/fs.c b/fs/fs.c
index 7da3a050c1cb..60fdb29078d6 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -2846,6 +2846,36 @@ out:
}
EXPORT_SYMBOL(chdir);
+char *pushd(const char *dir)
+{
+ char *oldcwd;
+ int ret;
+
+ oldcwd = strdup(getcwd());
+ if (!oldcwd)
+ return NULL;
+
+ ret = chdir(dir);
+ if (ret) {
+ free(oldcwd);
+ return NULL;
+ }
+
+ return oldcwd;
+}
+
+int popd(char *oldcwd)
+{
+ int ret;
+
+ if (!oldcwd)
+ return 0;
+
+ ret = chdir(oldcwd);
+ free(oldcwd);
+ return ret;
+}
+
static char *get_linux_mmcblkdev(struct fs_device_d *fsdev)
{
struct cdev *cdevm, *cdev;
diff --git a/include/unistd.h b/include/unistd.h
index 06ce3558099f..f7fe737d002b 100644
--- a/include/unistd.h
+++ b/include/unistd.h
@@ -20,6 +20,8 @@ int rmdir (const char *pathname);
int symlink(const char *pathname, const char *newpath);
int readlink(const char *path, char *buf, size_t bufsiz);
int chdir(const char *pathname);
+char *pushd(const char *dir);
+int popd(char *dir);
const char *getcwd(void);
int ftruncate(int fd, loff_t length);
--
2.30.2
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH 2/2] commands: add new tutorial command
2022-01-08 17:15 [PATCH 1/2] fs: implement pushd/popd chdir wrappers Ahmad Fatoum
@ 2022-01-08 17:15 ` Ahmad Fatoum
2022-01-12 10:29 ` [PATCH 1/2] fs: implement pushd/popd chdir wrappers Sascha Hauer
1 sibling, 0 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2022-01-08 17:15 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH 1/2] fs: implement pushd/popd chdir wrappers
2022-01-08 17:15 [PATCH 1/2] fs: implement pushd/popd chdir wrappers Ahmad Fatoum
2022-01-08 17:15 ` [PATCH 2/2] commands: add new tutorial command Ahmad Fatoum
@ 2022-01-12 10:29 ` Sascha Hauer
1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2022-01-12 10:29 UTC (permalink / raw)
To: Ahmad Fatoum; +Cc: barebox
On Sat, Jan 08, 2022 at 06:15:54PM +0100, Ahmad Fatoum wrote:
> We don't have dirfd's, so running operations relative to the current
> working directory always involves some allocation/freeing boilerplate.
>
> Add pushd/popd helpers to move the boilerplate out of the callsites.
>
> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> ---
> fs/fs.c | 30 ++++++++++++++++++++++++++++++
> include/unistd.h | 2 ++
> 2 files changed, 32 insertions(+)
Applied, thanks
Sascha
--
Pengutronix e.K. | |
Steuerwalder Str. 21 | http://www.pengutronix.de/ |
31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-01-12 10:31 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-08 17:15 [PATCH 1/2] fs: implement pushd/popd chdir wrappers Ahmad Fatoum
2022-01-08 17:15 ` [PATCH 2/2] commands: add new tutorial command Ahmad Fatoum
2022-01-12 10:29 ` [PATCH 1/2] fs: implement pushd/popd chdir wrappers Sascha Hauer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox