summaryrefslogtreecommitdiffstats
path: root/common/menutree.c
blob: c28284b47a0c66bab3a662a71ce8cedb5d0cc40e (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// SPDX-License-Identifier: GPL-2.0-only

#include <environment.h>
#include <libbb.h>
#include <common.h>
#include <command.h>
#include <glob.h>
#include <menu.h>
#include <fs.h>
#include <shell.h>
#include <libfile.h>

#include <linux/ctype.h>
#include <linux/stat.h>

struct menutree {
	char *action;
	struct menu_entry me;
};

static void menutree_action_subdir(struct menu *m, struct menu_entry *me)
{
	struct menutree *mt = container_of(me, struct menutree, me);

	menutree(mt->action, 0);
}

static void menutree_action(struct menu *m, struct menu_entry *me)
{
	struct menutree *mt = container_of(me, struct menutree, me);

	run_command(mt->action);
}

static void setenv_bool(const char *var, bool val)
{
	const char *str;

	if (val)
		str = "1";
	else
		str = "0";

	setenv(var, str);
}

static void menutree_box(struct menu *m, struct menu_entry *me)
{
	struct menutree *mt = container_of(me, struct menutree, me);

	setenv_bool(mt->action, me->box_state);
}

static void menutree_entry_free(struct menu_entry *me)
{
	struct menutree *mt = container_of(me, struct menutree, me);

	free(mt->action);
	free(mt->me.display);
	free(mt);
}

/*
 * menutree - show a menu constructed from a directory structure
 * @path: the path to the directory structure
 *
 * Each menu entry is described by a subdirectory. Each subdirectory
 * can contain the following files which further describe the entry:
 *
 * title - A file containing the title of the entry as shown in the menu
 * box - If present, the entry is a 'bool' entry. The file contains a variable
 *       name from which the current state of the bool is taken from and saved
 *       to.
 * action - if present this file contains a shell script which is executed when
 *          when the entry is selected.
 *
 * If neither 'box' or 'action' are present this entry is considered a submenu
 * containing more entries.
 */
int menutree(const char *path, int toplevel)
{
	int ret;
	struct menu *menu;
	struct stat s;
	char *box;
	struct menutree *mt;
	glob_t g;
	int i;
	char *globpath, *display;
	size_t size;

	menu = menu_alloc();

	globpath = basprintf("%s/*", path);
	ret = glob(globpath, 0, NULL, &g);
	free(globpath);
	if (ret == GLOB_NOMATCH) {
		ret = -EINVAL;
		goto out;
	}

	globpath = basprintf("%s/title", path);
	display = read_file(globpath, &size);
	free(globpath);
	if (!display) {
		eprintf("no title found in %s/title\n", path);
		ret = -EINVAL;
		goto out;
	}

	strim(display);
	menu_add_title(menu, shell_expand(display));
	free(display);

	for (i = 0; i < g.gl_pathc; i++) {
		ret = stat(g.gl_pathv[i], &s);
		if (ret)
			goto out;

		if (!S_ISDIR(s.st_mode))
			continue;

		mt = xzalloc(sizeof(*mt));

		display = read_file_line("%s/title", g.gl_pathv[i]);
		if (!display) {
			eprintf("no title found in %s/title\n", g.gl_pathv[i]);
			ret = -EINVAL;
			goto out;
		}

		mt->me.display = shell_expand(display);
		free(display);
		mt->me.free = menutree_entry_free;

		box = read_file_line("%s/box", g.gl_pathv[i]);
		if (box) {
			mt->me.type = MENU_ENTRY_BOX;
			mt->me.action = menutree_box;
			mt->action = box;
			getenv_bool(box, &mt->me.box_state);
			menu_add_entry(menu, &mt->me);
			continue;
		}

		mt->me.type = MENU_ENTRY_NORMAL;

		mt->action = basprintf("%s/action", g.gl_pathv[i]);

		ret = stat(mt->action, &s);
		if (ret) {
			mt->me.action = menutree_action_subdir;
			free(mt->action);
			mt->action = xstrdup(g.gl_pathv[i]);
		} else {
			mt->me.action = menutree_action;
		}

		menu_add_entry(menu, &mt->me);
	}

	if (!toplevel) {
		mt = xzalloc(sizeof(*mt));
		mt->me.display = xstrdup("back");
		mt->me.type = MENU_ENTRY_NORMAL;
		mt->me.non_re_ent = 1;
		mt->me.free = menutree_entry_free;
		menu_add_entry(menu, &mt->me);
	}

	menu_show(menu);

	ret = 0;
out:
	menu_free(menu);

	globfree(&g);

	return ret;
}