summaryrefslogtreecommitdiffstats
path: root/commands/tutorial.c
blob: 444177764311f5a1725cfc1b8f4b93918ae51362 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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("navigate the 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