/
iomenu.c
427 lines (371 loc) · 8.03 KB
/
iomenu.c
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <signal.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
#include <assert.h>
#include "compat.h"
#include "term.h"
#include "utf8.h"
struct {
char input[LINE_MAX];
size_t cur;
char **lines_buf;
size_t lines_count;
char **match_buf;
size_t match_count;
} ctx;
int opt_comment;
/*
* Keep the line if it match every token (in no particular order,
* and allowed to be overlapping).
*/
static int
match_line(char *line, char **tokv)
{
if (opt_comment && line[0] == '#')
return 2;
for (; *tokv != NULL; tokv++)
if (strcasestr(line, *tokv) == NULL)
return 0;
return 1;
}
/*
* Free the structures, reset the terminal state and exit with an
* error message.
*/
static void
die(const char *msg)
{
int e = errno;
term_raw_off(2);
fprintf(stderr, "iomenu: ");
errno = e;
perror(msg);
exit(1);
}
void *
xrealloc(void *ptr, size_t sz)
{
ptr = realloc(ptr, sz);
if (ptr == NULL)
die("realloc");
return ptr;
}
void *
xmalloc(size_t sz)
{
void *ptr;
ptr = malloc(sz);
if (ptr == NULL)
die("malloc");
return ptr;
}
static void
do_move(int sign)
{
/* integer overflow will do what we need */
for (size_t i = ctx.cur + sign; i < ctx.match_count; i += sign) {
if (opt_comment == 0 || ctx.match_buf[i][0] != '#') {
ctx.cur = i;
break;
}
}
}
/*
* First split input into token, then match every token independently against
* every line. The matching lines fills matches. Matches are searched inside
* of `searchv' of size `searchc'
*/
static void
do_filter(char **search_buf, size_t search_count)
{
char **t, *tokv[(sizeof ctx.input + 1) * sizeof(char *)];
char *b, buf[sizeof ctx.input];
strlcpy(buf, ctx.input, sizeof buf);
for (b = buf, t = tokv; (*t = strsep(&b, " \t")) != NULL; t++)
continue;
*t = NULL;
ctx.cur = ctx.match_count = 0;
for (size_t n = 0; n < search_count; n++)
if (match_line(search_buf[n], tokv))
ctx.match_buf[ctx.match_count++] = search_buf[n];
if (opt_comment && ctx.match_buf[ctx.cur][0] == '#')
do_move(+1);
}
static void
do_move_page(signed int sign)
{
int rows = term.winsize.ws_row - 1;
size_t i = ctx.cur - ctx.cur % rows + rows * sign;
if (i >= ctx.match_count)
return;
ctx.cur = i - 1;
do_move(+1);
}
static void
do_move_header(signed int sign)
{
do_move(sign);
if (opt_comment == 0)
return;
for (ctx.cur += sign;; ctx.cur += sign) {
char *cur = ctx.match_buf[ctx.cur];
if (ctx.cur >= ctx.match_count) {
ctx.cur--;
break;
}
if (cur[0] == '#')
break;
}
do_move(+1);
}
static void
do_remove_word(void)
{
int len, i;
len = strlen(ctx.input) - 1;
for (i = len; i >= 0 && isspace(ctx.input[i]); i--)
ctx.input[i] = '\0';
len = strlen(ctx.input) - 1;
for (i = len; i >= 0 && !isspace(ctx.input[i]); i--)
ctx.input[i] = '\0';
do_filter(ctx.lines_buf, ctx.lines_count);
}
static void
do_add_char(char c)
{
int len;
len = strlen(ctx.input);
if (len + 1 == sizeof ctx.input)
return;
if (isprint(c)) {
ctx.input[len] = c;
ctx.input[len + 1] = '\0';
}
do_filter(ctx.match_buf, ctx.match_count);
}
static void
do_print_selection(void)
{
if (opt_comment) {
char **match = ctx.match_buf + ctx.cur;
while (--match >= ctx.match_buf) {
if ((*match)[0] == '#') {
fprintf(stdout, "%s", *match + 1);
break;
}
}
fprintf(stdout, "%c", '\t');
}
term_raw_off(2);
if (ctx.match_count == 0
|| (opt_comment && ctx.match_buf[ctx.cur][0] == '#'))
fprintf(stdout, "%s\n", ctx.input);
else
fprintf(stdout, "%s\n", ctx.match_buf[ctx.cur]);
term_raw_on(2);
}
/*
* Big case table, that calls itself back for with TERM_KEY_ALT (aka Esc), TERM_KEY_CSI
* (aka Esc + [). These last two have values above the range of ASCII.
*/
static int
key_action(void)
{
int key;
key = term_get_key(stderr);
switch (key) {
case TERM_KEY_CTRL('Z'):
term_raw_off(2);
kill(getpid(), SIGSTOP);
term_raw_on(2);
break;
case TERM_KEY_CTRL('C'):
case TERM_KEY_CTRL('D'):
return -1;
case TERM_KEY_CTRL('U'):
ctx.input[0] = '\0';
do_filter(ctx.lines_buf, ctx.lines_count);
break;
case TERM_KEY_CTRL('W'):
do_remove_word();
break;
case TERM_KEY_DELETE:
case TERM_KEY_BACKSPACE:
ctx.input[strlen(ctx.input) - 1] = '\0';
do_filter(ctx.lines_buf, ctx.lines_count);
break;
case TERM_KEY_ARROW_UP:
case TERM_KEY_CTRL('P'):
do_move(-1);
break;
case TERM_KEY_ALT('p'):
do_move_header(-1);
break;
case TERM_KEY_ARROW_DOWN:
case TERM_KEY_CTRL('N'):
do_move(+1);
break;
case TERM_KEY_ALT('n'):
do_move_header(+1);
break;
case TERM_KEY_PAGE_UP:
case TERM_KEY_ALT('v'):
do_move_page(-1);
break;
case TERM_KEY_PAGE_DOWN:
case TERM_KEY_CTRL('V'):
do_move_page(+1);
break;
case TERM_KEY_TAB:
if (ctx.match_count == 0)
break;
strlcpy(ctx.input, ctx.match_buf[ctx.cur], sizeof(ctx.input));
do_filter(ctx.match_buf, ctx.match_count);
break;
case TERM_KEY_ENTER:
case TERM_KEY_CTRL('M'):
do_print_selection();
return 0;
default:
do_add_char(key);
}
return 1;
}
static void
print_line(char *line, int highlight)
{
if (opt_comment && line[0] == '#') {
fprintf(stderr, "\n\x1b[1m\r%.*s\x1b[m",
term_at_width(line + 1, term.winsize.ws_col, 0), line + 1);
} else if (highlight) {
fprintf(stderr, "\n\x1b[47;30m\x1b[K\r%.*s\x1b[m",
term_at_width(line, term.winsize.ws_col, 0), line);
} else {
fprintf(stderr, "\n%.*s",
term_at_width(line, term.winsize.ws_col, 0), line);
}
}
static void
do_print_screen(void)
{
char **m;
int p, c, cols, rows;
size_t i;
cols = term.winsize.ws_col;
rows = term.winsize.ws_row - 1; /* -1 to keep one line for user input */
p = c = 0;
i = ctx.cur - ctx.cur % rows;
m = ctx.match_buf + i;
fprintf(stderr, "\x1b[2J");
while (p < rows && i < ctx.match_count) {
print_line(*m, i == ctx.cur);
p++, i++, m++;
}
fprintf(stderr, "\x1b[H%.*s",
term_at_width(ctx.input, cols, c), ctx.input);
fflush(stderr);
}
static void
sig_winch(int sig)
{
if (ioctl(STDERR_FILENO, TIOCGWINSZ, &term.winsize) == -1)
die("ioctl");
do_print_screen();
signal(sig, sig_winch);
}
static void
usage(char const *arg0)
{
fprintf(stderr, "usage: %s [-#] <lines\n", arg0);
exit(1);
}
static int
read_stdin(char **buf)
{
size_t len = 0;
assert(*buf == NULL);
for (int c; (c = fgetc(stdin)) != EOF;) {
if (c == '\0') {
fprintf(stderr, "iomenu: ignoring '\\0' byte in input\r\n");
continue;
}
*buf = xrealloc(*buf, sizeof *buf + len + 1);
(*buf)[len++] = c;
}
*buf = xrealloc(*buf, sizeof *buf + len + 1);
(*buf)[len] = '\0';
return 0;
}
/*
* Split a buffer into an array of lines, without allocating memory for every
* line, but using the input buffer and replacing '\n' by '\0'.
*/
static void
split_lines(char *s)
{
size_t sz;
ctx.lines_count = 0;
for (;;) {
sz = (ctx.lines_count + 1) * sizeof s;
ctx.lines_buf = xrealloc(ctx.lines_buf, sz);
ctx.lines_buf[ctx.lines_count++] = s;
s = strchr(s, '\n');
if (s == NULL)
break;
*s++ = '\0';
}
sz = ctx.lines_count * sizeof s;
ctx.match_buf = xmalloc(sz);
memcpy(ctx.match_buf, ctx.lines_buf, sz);
}
/*
* Read stdin in a buffer, filling a table of lines, then re-open stdin to
* /dev/tty for an interactive (raw) session to let the user filter and select
* one line by searching words within stdin. This was inspired from dmenu.
*/
int
main(int argc, char *argv[])
{
char *buf = NULL, *arg0;
arg0 = *argv;
for (int opt; (opt = getopt(argc, argv, "#v")) > 0;) {
switch (opt) {
case 'v':
fprintf(stdout, "%s\n", VERSION);
exit(0);
case '#':
opt_comment = 1;
break;
default:
usage(arg0);
}
}
argc -= optind;
argv += optind;
read_stdin(&buf);
split_lines(buf);
do_filter(ctx.lines_buf, ctx.lines_count);
if (!isatty(2))
die("file descriptor 2 (stderr)");
freopen("/dev/tty", "w+", stderr);
if (stderr == NULL)
die("re-opening standard error read/write");
term_raw_on(2);
sig_winch(SIGWINCH);
#ifdef __OpenBSD__
pledge("stdio tty", NULL);
#endif
while (key_action() > 0)
do_print_screen();
term_raw_off(2);
return 0;
}