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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <assert.h>
#include "common.h"
#include "../lib/3rdparty/tinydir.h"
static struct client client = {
.filter_mode = BM_FILTER_MODE_DMENU,
.title = "bemenu-run",
};
struct paths {
char *path;
char *paths;
};
static char*
c_strdup2(const char *str, size_t size)
{
char *cpy = calloc(1, size + 1);
return (cpy ? memcpy(cpy, str, size) : NULL);
}
static char*
c_strdup(const char *str)
{
return c_strdup2(str, strlen(str));
}
static char*
strip_slash(char *str)
{
size_t size = strlen(str);
if (size > 0)
for (char *s = str + size - 1; *s == '/'; --s)
*s = 0;
return str;
}
static const char*
get_paths(const char *env, const char *default_paths, struct paths *state)
{
if (state->path && !*state->path) {
free(state->paths);
return NULL;
}
if (!state->paths) {
const char *paths;
if (!(paths = getenv(env)) || !paths[0])
paths = default_paths;
state->path = state->paths = c_strdup(paths);
}
if (!state->path || !state->paths)
return NULL;
char *path;
do {
size_t f;
path = state->path;
if ((f = strcspn(state->path, ":")) > 0) {
state->path += f + (path[f] ? 1 : 0);
path[f] = 0;
} else {
state->path += 1;
}
if (!*path) {
free(state->paths);
return NULL;
}
} while (path[0] != '/');
return strip_slash(path);
}
static int
compare(const void *a, const void *b)
{
const struct bm_item *ia = *(struct bm_item**)a, *ib = *(struct bm_item**)b;
return strcmp(bm_item_get_text(ia), bm_item_get_text(ib));
}
static void
read_items_to_menu_from_dir(struct bm_menu *menu, const char *path)
{
assert(menu && path);
tinydir_dir dir;
if (tinydir_open(&dir, path) == -1)
return;
while (dir.has_next) {
tinydir_file file;
memset(&file, 0, sizeof(file));
tinydir_readfile(&dir, &file);
if (!file.is_dir && file.name) {
struct bm_item *item;
if (!(item = bm_item_new(file.name)))
break;
bm_menu_add_item(menu, item);
}
tinydir_next(&dir);
}
tinydir_close(&dir);
uint32_t count;
struct bm_item **items = bm_menu_get_items(menu, &count);
qsort(items, count, sizeof(struct bm_item*), compare);
bool unique = true;
for (uint32_t i = 0; i + 1 < count; i += unique) {
if (!(unique = strcmp(bm_item_get_text(items[i]), bm_item_get_text(items[i + 1])))) {
bm_item_free(items[i]);
bm_menu_remove_item_at(menu, i);
items = bm_menu_get_items(menu, &count);
}
}
}
static void
read_items_to_menu_from_path(struct bm_menu *menu)
{
assert(menu);
const char *path;
struct paths state;
memset(&state, 0, sizeof(state));
while ((path = get_paths("PATH", "/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/bin:/sbin", &state)))
read_items_to_menu_from_dir(menu, path);
}
#define WHITESPACE " \t\n\r"
static const char*
tokenize(const char *cstr, size_t *out_len, const char *separator, bool skip_whitespace, const char **state)
{
assert(out_len && separator && state);
const char *current = (state && *state ? *state : cstr);
if (!current || !*current || !cstr || !*cstr)
return NULL;
current += strspn(current, separator);
if (skip_whitespace)
current += strspn(current, WHITESPACE);
*out_len = strcspn(current, separator);
*state = current + *out_len;
if (skip_whitespace) {
const size_t ws = strcspn(current, WHITESPACE);
*out_len -= (ws < *out_len ? *out_len - ws : 0);
}
return current;
}
static const char*
tokenize_quoted(const char *cstr, size_t *out_len, const char *separator, const char *quotes, const char **state)
{
assert(out_len && separator && quotes && state);
const char *e, *current = tokenize(cstr, out_len, separator, true, state);
if (!current)
return NULL;
for (const char *q = quotes; *q; ++q) {
if (*current != *q)
continue;
bool escaped = false;
for (e = ++current; *e; ++e) {
if (escaped)
escaped = false;
else if (*e == '\\')
escaped = true;
else if (*e == *q)
break;
}
*out_len = e - current;
e = (!*e ? e : e + 1);
if (*e) {
size_t tmp;
const char *state2 = NULL;
*state = tokenize(e, &tmp, separator, true, &state2);
} else {
*state = e;
}
break;
}
return current;
}
static void
launch(const char *bin)
{
if (!bin)
return;
if (fork() == 0) {
setsid();
freopen("/dev/null", "w", stdout);
freopen("/dev/null", "w", stderr);
size_t count = 0;
{
size_t len;
const char *state = NULL;
while (tokenize_quoted(bin, &len, " ", "\"'", &state))
++count;
}
char **tokens;
if (!count || !(tokens = calloc(count + 1, sizeof(char*))))
_exit(EXIT_FAILURE);
{
size_t i = 0, len;
const char *t, *state = NULL;
while (i < count && (t = tokenize_quoted(bin, &len, " ", "\"'", &state))) {
if (!(tokens[i++] = c_strdup2(t, len)))
_exit(EXIT_FAILURE);
}
}
execvp(tokens[0], tokens);
_exit(EXIT_SUCCESS);
}
}
static void
item_cb(struct bm_item *item, const char *text)
{
(void)item; // may be null
launch(text);
}
int
main(int argc, char **argv)
{
struct sigaction action = {
.sa_handler = SIG_DFL,
.sa_flags = SA_NOCLDWAIT
};
// do not care about childs
sigaction(SIGCHLD, &action, NULL);
if (!bm_init())
return EXIT_FAILURE;
parse_args(&client, &argc, &argv);
struct bm_menu *menu;
if (!(menu = menu_with_options(&client)))
return EXIT_FAILURE;
read_items_to_menu_from_path(menu);
const enum bm_run_result status = run_menu(&client, menu, item_cb);
bm_menu_free(menu);
return (status == BM_RUN_RESULT_SELECTED ? EXIT_SUCCESS : EXIT_FAILURE);
}
/* vim: set ts=8 sw=4 tw=0 :*/
|