-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiosplit.c
571 lines (489 loc) · 12.2 KB
/
iosplit.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/*
* Copyright (c) 2021, Tommi Leino <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <curses.h>
#include <locale.h>
#include <ctype.h>
#include <err.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#ifdef HAVE_PTY_H
#include <pty.h>
#else
#include <util.h>
#endif
#ifndef INFTIM
#define INFTIM -1
#endif
#include <poll.h>
#include <string.h>
#include <termios.h>
struct view {
struct row *top;
size_t top_linenum;
};
enum {
OUTPUT_CURSOR,
INPUT_CURSOR
};
struct cursor {
struct row *row;
size_t col;
int type;
};
struct buffer {
struct cursor input;
struct cursor output;
struct row *first;
struct view view;
};
#define CMD_ROW (1 << 0)
struct row {
char *text;
int flags;
size_t text_alloc;
size_t text_len;
size_t prompt_len;
struct row *prev;
struct row *next;
struct buffer *buffer;
};
#define ROW_INITIAL_ALLOC (40)
void insert_text(struct cursor *, struct cursor *, char *, size_t, int);
void draw_buffer(WINDOW *, struct buffer *, int);
void init_buffer(struct buffer *);
struct row *add_row(struct buffer *, struct row *);
int is_trailing_cursor(struct cursor *, struct cursor *);
struct row *find_row_flags(struct row *, int);
void remove_row(struct row *);
void clear_to(struct row *, struct row *);
void set_cursor(struct cursor *, struct row *, size_t);
size_t nlines(struct row *, struct row *);
size_t
nlines(struct row *begin, struct row *end)
{
struct row *np;
size_t n;
n = 0;
for (np = begin; np != NULL && np != end; np = np->next)
n++;
return n;
}
int
is_trailing_cursor(struct cursor *candidate, struct cursor *reference)
{
if (candidate->row == reference->row &&
candidate->col >= reference->col)
return 1;
return 0;
}
struct row *
find_row_flags(struct row *np, int flags)
{
for (; np != NULL; np = np->next)
if (np->flags & flags)
return np;
return NULL;
}
void
remove_row(struct row *np)
{
if (np == np->buffer->first && np->buffer->first->next == NULL)
return;
if (np->prev)
np->prev->next = np->next;
if (np->next)
np->next->prev = np->prev;
if (np == np->buffer->first) {
if (np->buffer->view.top == np)
np->buffer->view.top = np->next;
np->buffer->first = np->next;
}
free(np);
}
void
clear_to(struct row *np, struct row *last)
{
struct row *next;
for (; np != NULL && np != last; np = next) {
next = np->next;
remove_row(np);
}
}
/*
* TODO: Simplify.
*
* Insert 'text' of 'len' bytes to 'cursor' while managing 'other'
* cursor, and scrolling the view if we're exceeding 'rows'.
*
* If we're inserting a new output row to a row which has input
* content, move the input content to a row of its own.
*/
void
insert_text(struct cursor *cursor, struct cursor *other, char *text,
size_t len, int rows)
{
struct row *row;
size_t i;
int other_cursor_ok;
assert(cursor != NULL);
assert(cursor->row != NULL);
for (i = 0; i < len; i++) {
if (*text == '\r') {
text++;
continue;
}
other_cursor_ok = 0;
if (*text == '\n') {
if (other->type == INPUT_CURSOR &&
is_trailing_cursor(other, cursor) &&
cursor->row == other->row) {
if (cursor->row->prompt_len <
cursor->row->text_len) {
other->row =
add_row(cursor->row->buffer,
cursor->row);
other->col = 0;
insert_text(other, cursor,
&other->row->prev->text[cursor->row->prompt_len],
cursor->row->text_len -
cursor->row->prompt_len, rows);
cursor->row->text_len =
cursor->row->prompt_len;
other_cursor_ok = 1;
}
}
if (cursor->type == OUTPUT_CURSOR)
cursor->row->prompt_len = 0;
row = add_row(cursor->row->buffer, cursor->row);
if (is_trailing_cursor(other, cursor) &&
!other_cursor_ok) {
cursor->row = other->row = row;
cursor->col = other->col = 0;
} else {
cursor->row = row;
cursor->col = 0;
}
text++;
/*
* Scroll so that input cursor will be visible.
*/
if (cursor->type == OUTPUT_CURSOR)
row = other->row;
while (nlines(row->buffer->view.top,
row) >= rows)
row->buffer->view.top =
row->buffer->view.top->next;
continue;
}
if (cursor->row->text_len == cursor->row->text_alloc) {
if (cursor->row->text_alloc == 0)
cursor->row->text_alloc = ROW_INITIAL_ALLOC;
else
cursor->row->text_alloc *= 2;
cursor->row->text = realloc(cursor->row->text,
cursor->row->text_alloc * sizeof(char));
if (cursor->row->text == NULL)
err(1, "realloc");
}
if (cursor->col > cursor->row->text_len)
cursor->col = cursor->row->text_len;
if (is_trailing_cursor(other, cursor) &&
other->type != OUTPUT_CURSOR)
other->col++;
cursor->row->text_len++;
memmove(&cursor->row->text[cursor->col+1],
&cursor->row->text[cursor->col],
cursor->row->text_len - cursor->col);
cursor->row->text[cursor->col++] = *text++;
if (cursor->type == OUTPUT_CURSOR)
cursor->row->prompt_len = cursor->col;
}
}
void
draw_buffer(WINDOW *win, struct buffer *buffer, int rows)
{
struct row *np;
size_t linenum, i, row;
assert(buffer != NULL);
linenum = buffer->view.top_linenum;
row = 0;
for (np = buffer->view.top; np != NULL && row < rows;
np = np->next, row++) {
wmove(win, row, 0);
#ifdef WANT_ROW_INFO
wattron(win, A_REVERSE);
printw(win, "%3d %3d/%3d %3d %c",
linenum+1, np->text_len,
np->text_alloc, np->prompt_len,
np->flags & CMD_ROW ? 'c' : '-');
wattroff(win, A_REVERSE);
waddch(win, ' ');
#endif
for (i = 0; i < np->text_len; i++) {
if (np->text[i] == '\r')
continue;
if (buffer->input.row == np && buffer->input.col == i)
wattron(win, COLOR_PAIR(1));
if (buffer->output.row == np && buffer->output.col == i)
wattron(win, COLOR_PAIR(2));
waddch(win, np->text[i]);
if (buffer->input.row == np && buffer->input.col == i)
wattroff(win, COLOR_PAIR(1));
if (buffer->output.row == np && buffer->output.col == i)
wattroff(win, COLOR_PAIR(2));
}
if (buffer->input.row == np && buffer->input.col == i)
wattron(win, COLOR_PAIR(1));
else if (buffer->output.row == np && buffer->output.col == i)
wattron(win, COLOR_PAIR(2));
waddch(win, ' ');
if (buffer->input.row == np && buffer->input.col == i)
wattroff(win, COLOR_PAIR(1));
else if (buffer->output.row == np && buffer->output.col == i)
wattroff(win, COLOR_PAIR(2));
linenum++;
wclrtoeol(win);
}
wclrtobot(win);
wrefresh(win);
}
void
set_cursor(struct cursor *cursor, struct row *row, size_t col)
{
cursor->col = col;
cursor->row = row;
}
void
init_buffer(struct buffer *buffer)
{
assert(buffer != NULL);
buffer->first = add_row(buffer, NULL);
set_cursor(&buffer->input, buffer->first, 0);
set_cursor(&buffer->output, buffer->first, 0);
buffer->input.type = INPUT_CURSOR;
buffer->output.type = OUTPUT_CURSOR;
buffer->view.top = buffer->first;
buffer->view.top_linenum = 0;
}
struct row *
add_row(struct buffer *buffer, struct row *after)
{
struct row *row;
assert(buffer != NULL);
row = calloc(1, sizeof(struct row));
if (row == NULL)
err(1, "calloc");
row->buffer = buffer;
if (after == NULL) {
row->next = buffer->first;
if (buffer->first != NULL)
buffer->first->prev = row;
buffer->first = row;
} else {
if (after->next) {
after->next->prev = row;
row->next = after->next;
}
after->next = row;
row->prev = after;
}
return row;
}
int
main(int argc, char *argv[])
{
WINDOW *win, *owin;
#ifdef WANT_BOXES
WINDOW *iwin, *swin;
int col = 0;
#endif
int ch;
char buf[4096 + 1], *p, ibuf[4096 + 1];
pid_t pid;
int fd;
int n;
struct pollfd pfd[2];
int nready;
static struct buffer buffer;
struct row *row;
int rows, cols;
init_buffer(&buffer);
p = buf;
*p = '\0';
setlocale(LC_ALL, "");
win = initscr();
start_color();
init_pair(1, COLOR_WHITE, COLOR_GREEN);
init_pair(2, COLOR_WHITE, COLOR_RED);
#ifdef WANT_BOXES
owin = newwin(getmaxy(win) - 2, getmaxx(win), 0, 0);
swin = newwin(1, getmaxx(win), getmaxy(win) - 2, 0);
iwin = newwin(1, getmaxx(win), getmaxy(win) - 1, 0);
idlok(iwin, true);
idlok(owin, true);
keypad(iwin, true);
keypad(win, true);
keypad(owin, true);
whline(swin, '-', getmaxx(win));
wrefresh(win);
wrefresh(owin);
wrefresh(iwin);
wrefresh(swin);
#else
owin = newwin(getmaxy(win), getmaxx(win), 0, 0);
idlok(owin, true);
keypad(owin, true);
keypad(win, true);
curs_set(0);
wrefresh(win);
wrefresh(owin);
#endif
getmaxyx(owin, rows, cols);
raw();
noecho();
setenv("TERM", "dumb", 1);
pid = forkpty(&fd, NULL, NULL, NULL);
if (pid < 0)
err(1, "forkpty");
if (pid == 0)
execl("/bin/sh", "/bin/sh", NULL);
#ifdef HAVE_PLEDGE
if (pledge("stdio tty", NULL) != 0)
err(1, "pledge");
#endif
pfd[0].fd = STDIN_FILENO;
pfd[0].events = POLLIN;
pfd[1].fd = fd;
pfd[1].events = POLLIN;
while (1) {
nready = poll(pfd, sizeof(pfd) / sizeof(pfd[0]), INFTIM);
if (nready == -1)
err(1, "poll");
if (pfd[0].revents & (POLLIN|POLLHUP)) {
ch = wgetch(win);
/*
* Write control commands immediately.
*/
if (iscntrl(ch) && ch != '\t' && ch != '\n' &&
isascii(ch)) {
write(fd, &ch, 1);
continue;
}
switch (ch) {
case '\n':
if (buffer.input.row->text) {
row = buffer.input.row;
write(fd,
&row->text[row->prompt_len],
row->text_len - row->prompt_len);
write(fd, "\n", 1);
free(row->text);
row->flags |= CMD_ROW;
row->text = NULL;
row->text_alloc = 0;
row->text_len = 0;
row->prompt_len = 0;
buffer.input.col = 0;
}
buffer.output.row = buffer.input.row;
buffer.output.col = buffer.input.col;
clear_to(row->next, find_row_flags(row->next,
CMD_ROW));
break;
case KEY_UP:
row = buffer.input.row;
if (row == buffer.view.top && row->prev)
buffer.view.top = row->prev;
row = buffer.input.row = row->prev;
if (row == NULL)
row = buffer.input.row = buffer.first;
if (buffer.input.col >= row->text_len)
buffer.input.col = row->text_len;
draw_buffer(owin, &buffer, rows);
break;
case KEY_DOWN:
row = buffer.input.row;
if (row->next != NULL)
row = buffer.input.row = row->next;
if (buffer.input.col >= row->text_len)
buffer.input.col = row->text_len;
if (nlines(row->buffer->view.top,
row) >= rows)
row->buffer->view.top =
row->buffer->view.top->next;
draw_buffer(owin, &buffer, rows);
break;
case KEY_LEFT:
if (buffer.input.col > 0)
buffer.input.col--;
draw_buffer(owin, &buffer, rows);
break;
case KEY_RIGHT:
row = buffer.input.row;
if (buffer.input.col < row->text_len)
buffer.input.col++;
draw_buffer(owin, &buffer, rows);
break;
case KEY_BACKSPACE:
row = buffer.input.row;
if (buffer.input.col > 0) {
row->text_len--;
if (buffer.input.col <= row->prompt_len)
row->prompt_len--;
if (is_trailing_cursor(&buffer.output,
&buffer.input))
buffer.output.col--;
buffer.input.col--;
if (row->text != NULL)
row->text[row->text_len] = '\0';
}
draw_buffer(owin, &buffer, rows);
break;
default:
insert_text(&buffer.input, &buffer.output,
(char *) &ch, 1, rows);
draw_buffer(owin, &buffer, rows);
break;
}
#ifdef WANT_BOXES
mvwprintw(swin, 0, 72-1, "%3d", col + 1);
wrefresh(swin);
wrefresh(iwin);
#endif
}
if (pfd[1].revents & (POLLIN|POLLHUP)) {
n = read(fd, ibuf, sizeof(ibuf));
if (n > 0) {
insert_text(&buffer.output, &buffer.input,
ibuf, n, rows);
} else {
if (n < 0) {
endwin();
err(1, "read");
}
break;
}
draw_buffer(owin, &buffer, rows);
#ifdef WANT_BOXES
wrefresh(iwin);
#endif
}
}
endwin();
return 0;
}