/src/barebox/commands/edit.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-only |
2 | | // SPDX-FileCopyrightText: © 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix |
3 | | |
4 | | #include <common.h> |
5 | | #include <command.h> |
6 | | #include <malloc.h> |
7 | | #include <fs.h> |
8 | | #include <linux/ctype.h> |
9 | | #include <fcntl.h> |
10 | | #include <libfile.h> |
11 | | #include <readkey.h> |
12 | | #include <errno.h> |
13 | | #include <xfuncs.h> |
14 | | #include <linux/stat.h> |
15 | | #include <console.h> |
16 | | #include <term.h> |
17 | | |
18 | 0 | #define TABSPACE 8 |
19 | | |
20 | | struct line { |
21 | | int length; |
22 | | struct line *next; |
23 | | struct line *prev; |
24 | | char *data; |
25 | | }; |
26 | | |
27 | | static struct line *buffer; |
28 | | |
29 | | static struct line *lastscrline; |
30 | | |
31 | | static int screenwidth = 80; |
32 | | static int screenheight = 25; |
33 | | |
34 | | static int cursx = 0; /* position on screen */ |
35 | | static int cursy = 0; |
36 | | |
37 | | static int textx = 0; /* position in text */ |
38 | | |
39 | | static struct line *curline; /* line where the cursor is */ |
40 | | |
41 | | static struct line *scrline; /* the first line on screen */ |
42 | | static int scrcol = 0; /* the first column on screen */ |
43 | | |
44 | | static char *screenline(char *line, int *pos) |
45 | 0 | { |
46 | 0 | int i, outpos = 0; |
47 | 0 | static char lbuf[1024]; |
48 | |
|
49 | 0 | if (!line) { |
50 | 0 | lbuf[outpos++] = '~'; |
51 | 0 | goto out; |
52 | 0 | } |
53 | | |
54 | 0 | for (i = 0; outpos < sizeof(lbuf) - 1; i++) { |
55 | 0 | if (i == textx && pos) |
56 | 0 | *pos = outpos; |
57 | 0 | if (!line[i]) |
58 | 0 | break; |
59 | 0 | if (line[i] == '\t') { |
60 | 0 | lbuf[outpos++] = ' '; |
61 | 0 | while (outpos < sizeof(lbuf) - 1 && outpos % TABSPACE) |
62 | 0 | lbuf[outpos++] = ' '; |
63 | 0 | continue; |
64 | 0 | } |
65 | 0 | lbuf[outpos++] = line[i]; |
66 | 0 | } |
67 | |
|
68 | 0 | out: |
69 | 0 | lbuf[outpos] = 0; |
70 | 0 | return lbuf; |
71 | 0 | } |
72 | | |
73 | | static int setpos(char *line, int position) |
74 | 0 | { |
75 | 0 | int i = 0; |
76 | 0 | int linepos = 0; |
77 | |
|
78 | 0 | while(line[linepos]) { |
79 | 0 | if (line[linepos] == '\t') |
80 | 0 | while ((i + 1) % TABSPACE) |
81 | 0 | i++; |
82 | 0 | if (i >= position) |
83 | 0 | return linepos; |
84 | 0 | linepos++; |
85 | 0 | i++; |
86 | 0 | } |
87 | | |
88 | 0 | return linepos; |
89 | 0 | } |
90 | | |
91 | | static void refresh_line(struct line *line, int ypos) |
92 | 0 | { |
93 | 0 | char *str = screenline(line->data, NULL) + scrcol; |
94 | 0 | term_setpos(0, ypos); |
95 | 0 | str[screenwidth] = 0; |
96 | 0 | printf("%s\x1b[K", str); |
97 | 0 | term_setpos(cursx, cursy); |
98 | 0 | } |
99 | | |
100 | | /* |
101 | | * Most sane terminal programs can do ansi screen scrolling. |
102 | | * Unfortunately one of the most popular programs cannot: |
103 | | * minicom. |
104 | | * Grmpf! |
105 | | */ |
106 | | static int smartscroll = 0; |
107 | | |
108 | | static void refresh(int full) |
109 | 0 | { |
110 | 0 | int i; |
111 | 0 | struct line *l = scrline; |
112 | |
|
113 | 0 | if (!full) { |
114 | 0 | if (smartscroll) { |
115 | 0 | if (scrline->next == lastscrline) { |
116 | 0 | printf("\x1b[1T"); |
117 | 0 | refresh_line(scrline, 0); |
118 | 0 | term_setpos(0, screenheight); |
119 | 0 | printf("%*s", screenwidth, ""); |
120 | 0 | return; |
121 | 0 | } |
122 | | |
123 | 0 | if (scrline->prev == lastscrline) { |
124 | 0 | printf("\x1b[1S"); |
125 | 0 | for (i = 0; i < screenheight - 1; i++) { |
126 | 0 | l = l->next; |
127 | 0 | if (!l) |
128 | 0 | return; |
129 | 0 | } |
130 | 0 | refresh_line(l, screenheight - 1); |
131 | 0 | return; |
132 | 0 | } |
133 | 0 | } else { |
134 | 0 | refresh(1); |
135 | 0 | return; |
136 | 0 | } |
137 | 0 | } |
138 | | |
139 | 0 | for (i = 0; i < screenheight; i++) { |
140 | 0 | refresh_line(l, i); |
141 | 0 | l = l->next; |
142 | 0 | if (!l) |
143 | 0 | break; |
144 | 0 | } |
145 | |
|
146 | 0 | i++; |
147 | 0 | while (i < screenheight) { |
148 | 0 | term_setpos(0, i++); |
149 | 0 | printf("~"); |
150 | 0 | } |
151 | 0 | } |
152 | | |
153 | | static void line_free(struct line *line) |
154 | 0 | { |
155 | 0 | free(line->data); |
156 | 0 | free(line); |
157 | 0 | } |
158 | | |
159 | | static struct line *line_realloc(int len, struct line *line) |
160 | 0 | { |
161 | 0 | int size = 32; |
162 | |
|
163 | 0 | if (!line) |
164 | 0 | line = xzalloc(sizeof(struct line)); |
165 | |
|
166 | 0 | while (size < len) |
167 | 0 | size <<= 1; |
168 | |
|
169 | 0 | line->data = xrealloc(line->data, size); |
170 | 0 | return line; |
171 | 0 | } |
172 | | |
173 | | static int edit_read_file(const char *path) |
174 | 0 | { |
175 | 0 | struct line *line; |
176 | 0 | struct line *lastline = NULL; |
177 | 0 | char *filebuffer; |
178 | 0 | char *linestr, *lineend; |
179 | |
|
180 | 0 | if (file_exists(path)) { |
181 | 0 | filebuffer = read_file(path, NULL); |
182 | 0 | if (!filebuffer) { |
183 | 0 | printf("could not read %s: %m\n", path); |
184 | 0 | return -1; |
185 | 0 | } |
186 | | |
187 | 0 | linestr = filebuffer; |
188 | 0 | while (1) { |
189 | 0 | if (!*linestr) |
190 | 0 | break; |
191 | | |
192 | 0 | lineend = strchr(linestr, '\n'); |
193 | |
|
194 | 0 | if (!lineend && !*linestr) |
195 | 0 | break; |
196 | | |
197 | 0 | if (lineend) |
198 | 0 | *lineend = 0; |
199 | |
|
200 | 0 | line = line_realloc(strlen(linestr) + 1, NULL); |
201 | 0 | if (!buffer) |
202 | 0 | buffer = line; |
203 | 0 | memcpy(line->data, linestr, strlen(linestr) + 1); |
204 | 0 | line->prev = lastline; |
205 | 0 | if (lastline) |
206 | 0 | lastline->next = line; |
207 | 0 | line->next = NULL; |
208 | 0 | lastline = line; |
209 | |
|
210 | 0 | if (!lineend) |
211 | 0 | break; |
212 | | |
213 | 0 | linestr = lineend + 1; |
214 | 0 | } |
215 | 0 | free(filebuffer); |
216 | 0 | } |
217 | | |
218 | 0 | if (!buffer) { |
219 | 0 | buffer = line_realloc(0, NULL); |
220 | 0 | buffer->data[0] = 0; |
221 | 0 | } |
222 | |
|
223 | 0 | return 0; |
224 | 0 | } |
225 | | |
226 | | static void free_buffer(void) |
227 | 0 | { |
228 | 0 | struct line *line, *tmp; |
229 | |
|
230 | 0 | line = buffer; |
231 | |
|
232 | 0 | while(line) { |
233 | 0 | tmp = line->next; |
234 | 0 | line_free(line); |
235 | 0 | line = tmp; |
236 | 0 | } |
237 | 0 | } |
238 | | |
239 | | static int save_file(const char *path) |
240 | 0 | { |
241 | 0 | struct line *line, *tmp; |
242 | 0 | int fd; |
243 | 0 | int ret = 0; |
244 | |
|
245 | 0 | fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0666); |
246 | 0 | if (fd < 0) { |
247 | 0 | printf("could not open file for writing: %m\n"); |
248 | 0 | return fd; |
249 | 0 | } |
250 | | |
251 | 0 | line = buffer; |
252 | |
|
253 | 0 | while(line) { |
254 | 0 | tmp = line->next; |
255 | 0 | ret = write_full(fd, line->data, strlen(line->data)); |
256 | 0 | if (ret < 0) |
257 | 0 | goto out; |
258 | 0 | ret = write_full(fd, "\n", 1); |
259 | 0 | if (ret < 0) |
260 | 0 | goto out; |
261 | 0 | line = tmp; |
262 | 0 | } |
263 | | |
264 | 0 | ret = 0; |
265 | |
|
266 | 0 | out: |
267 | 0 | close(fd); |
268 | 0 | return ret; |
269 | 0 | } |
270 | | |
271 | | static void insert_char(char c) |
272 | 0 | { |
273 | 0 | int pos = textx; |
274 | 0 | char *line; |
275 | 0 | int end = strlen(curline->data); |
276 | |
|
277 | 0 | line_realloc(strlen(curline->data) + 2, curline); |
278 | 0 | line = curline->data; |
279 | |
|
280 | 0 | while (end >= pos) { |
281 | 0 | line[end + 1] = line[end]; |
282 | 0 | end--; |
283 | 0 | } |
284 | 0 | line[pos] = c; |
285 | 0 | textx++; |
286 | 0 | refresh_line(curline, cursy); |
287 | 0 | } |
288 | | |
289 | | static void delete_char(int pos) |
290 | 0 | { |
291 | 0 | char *line = curline->data; |
292 | 0 | int end = strlen(line); |
293 | |
|
294 | 0 | while (pos < end) { |
295 | 0 | line[pos] = line[pos + 1]; |
296 | 0 | pos++; |
297 | 0 | } |
298 | 0 | refresh_line(curline, cursy); |
299 | 0 | } |
300 | | |
301 | | static void split_line(void) |
302 | 0 | { |
303 | 0 | int length = strlen(curline->data + textx); |
304 | 0 | struct line *newline = line_realloc(length + 1, NULL); |
305 | 0 | struct line *tmp; |
306 | |
|
307 | 0 | memcpy(newline->data, curline->data + textx, length + 1); |
308 | |
|
309 | 0 | curline->data[textx] = 0; |
310 | |
|
311 | 0 | tmp = curline->next; |
312 | 0 | curline->next = newline; |
313 | 0 | newline->prev = curline; |
314 | 0 | newline->next = tmp; |
315 | 0 | if (tmp) |
316 | 0 | tmp->prev = newline; |
317 | |
|
318 | 0 | textx = 0; |
319 | 0 | cursy++; |
320 | 0 | curline = curline->next; |
321 | 0 | refresh(1); |
322 | 0 | } |
323 | | |
324 | | static void merge_line(struct line *line) |
325 | 0 | { |
326 | 0 | struct line *tmp; |
327 | |
|
328 | 0 | line_realloc(strlen(line->data) + strlen(line->next->data) + 1, line); |
329 | |
|
330 | 0 | tmp = line->next; |
331 | |
|
332 | 0 | line->next = line->next->next; |
333 | 0 | if (line->next) |
334 | 0 | line->next->prev = line; |
335 | 0 | strcat(line->data, tmp->data); |
336 | |
|
337 | 0 | line_free(tmp); |
338 | |
|
339 | 0 | refresh(1); |
340 | 0 | } |
341 | | |
342 | | #define ESC "\033" |
343 | | |
344 | | static void statusbar(const char *str) |
345 | 0 | { |
346 | 0 | term_setpos(0, screenheight+1); |
347 | 0 | printf("%*c\r%s", screenwidth, ' ', str); |
348 | 0 | term_setpos(cursx, cursy); |
349 | 0 | } |
350 | | |
351 | | static int read_modal_key(bool is_modal) |
352 | 0 | { |
353 | 0 | static enum { MODE_INSERT, MODE_NORMAL } mode = MODE_NORMAL; |
354 | 0 | static int backlog[2] = { -1, -1 }; |
355 | 0 | int c; |
356 | |
|
357 | 0 | if (backlog[0] >= 0) { |
358 | | /* pop a character */ |
359 | 0 | c = backlog[0]; |
360 | 0 | backlog[0] = backlog[1]; |
361 | 0 | backlog[1] = -1; |
362 | 0 | } else { |
363 | 0 | c = read_key(); |
364 | 0 | } |
365 | |
|
366 | 0 | if (is_modal && mode == MODE_INSERT && (c == -1 || c == CTL_CH('c'))) { |
367 | 0 | mode = MODE_NORMAL; |
368 | 0 | statusbar(""); |
369 | 0 | return -EAGAIN; |
370 | 0 | } |
371 | | |
372 | 0 | if (!is_modal || mode == MODE_INSERT) |
373 | 0 | return c; |
374 | | |
375 | 0 | switch (c) { |
376 | 0 | case -1: /* invalid escape, e.g. two escapes in a row */ |
377 | 0 | break; |
378 | 0 | case 'i': |
379 | 0 | statusbar("-- INSERT --"); |
380 | 0 | mode = MODE_INSERT; |
381 | 0 | break; |
382 | 0 | case 'h': |
383 | 0 | return BB_KEY_LEFT; |
384 | 0 | case 'j': |
385 | 0 | return BB_KEY_DOWN; |
386 | 0 | case 'k': |
387 | 0 | return BB_KEY_UP; |
388 | 0 | case 'a': |
389 | 0 | statusbar("-- INSERT --"); |
390 | 0 | mode = MODE_INSERT; |
391 | 0 | fallthrough; |
392 | 0 | case 'l': |
393 | 0 | return BB_KEY_RIGHT; |
394 | 0 | case 'O': |
395 | 0 | backlog[0] = '\n'; |
396 | 0 | backlog[1] = BB_KEY_UP; |
397 | 0 | fallthrough; |
398 | 0 | case 'I': |
399 | 0 | statusbar("-- INSERT --"); |
400 | 0 | mode = MODE_INSERT; |
401 | 0 | fallthrough; |
402 | 0 | case '^': |
403 | 0 | case '0': |
404 | 0 | return BB_KEY_HOME; |
405 | 0 | case 'g': |
406 | 0 | c = read_key(); |
407 | 0 | if (c != 'g') |
408 | 0 | break; |
409 | 0 | backlog[0] = CTL_CH('u'); |
410 | 0 | backlog[1] = CTL_CH('u'); |
411 | 0 | fallthrough; |
412 | 0 | case CTL_CH('u'): |
413 | 0 | return BB_KEY_PAGEUP; |
414 | 0 | case 'G': |
415 | 0 | backlog[0] = CTL_CH('d'); |
416 | 0 | backlog[1] = CTL_CH('d'); |
417 | 0 | fallthrough; |
418 | 0 | case CTL_CH('d'): |
419 | 0 | return BB_KEY_PAGEDOWN; |
420 | 0 | case 'o': |
421 | 0 | backlog[0] = '\n'; |
422 | 0 | fallthrough; |
423 | 0 | case 'A': |
424 | 0 | statusbar("-- INSERT --"); |
425 | 0 | mode = MODE_INSERT; |
426 | 0 | fallthrough; |
427 | 0 | case '$': |
428 | 0 | return BB_KEY_END; |
429 | 0 | case CTL_CH('c'): |
430 | 0 | statusbar("Type ZQ to abandon all changes and exit vi." |
431 | 0 | "Type ZZ to exit while saving them."); |
432 | 0 | break; |
433 | 0 | case 'x': |
434 | 0 | return BB_KEY_DEL; |
435 | 0 | case 'X': |
436 | 0 | return '\b'; |
437 | 0 | case BB_KEY_PAGEUP: |
438 | 0 | case BB_KEY_PAGEDOWN: |
439 | 0 | case BB_KEY_HOME: |
440 | 0 | case BB_KEY_END: |
441 | 0 | case BB_KEY_UP: |
442 | 0 | case BB_KEY_DOWN: |
443 | 0 | case BB_KEY_RIGHT: |
444 | 0 | case BB_KEY_LEFT: |
445 | 0 | return c; |
446 | 0 | case ':': |
447 | 0 | statusbar("ERROR: command mode not supported"); |
448 | 0 | break; |
449 | 0 | case 'Z': |
450 | 0 | c = read_key(); |
451 | 0 | if (c == 'Z') |
452 | 0 | return CTL_CH('d'); |
453 | 0 | if (c == 'Q') |
454 | 0 | return CTL_CH('c'); |
455 | 0 | fallthrough; |
456 | 0 | default: |
457 | 0 | statusbar("ERROR: not implemented"); |
458 | 0 | break; |
459 | 0 | } |
460 | | |
461 | 0 | return -EAGAIN; |
462 | 0 | } |
463 | | |
464 | | static bool is_efi_console_active(void) |
465 | 0 | { |
466 | 0 | if (!IS_ENABLED(CONFIG_DRIVER_SERIAL_EFI_STDIO)) |
467 | 0 | return false; |
468 | | |
469 | 0 | for_each_console(cdev) { |
470 | 0 | if (!(cdev->f_active & CONSOLE_STDIN)) |
471 | 0 | continue; |
472 | 0 | if (!(cdev->f_active & CONSOLE_STDOUT)) |
473 | 0 | continue; |
474 | 0 | if (!strcmp(dev_name(cdev->dev), "efi-stdio")) |
475 | 0 | return true; |
476 | 0 | } |
477 | | |
478 | 0 | return false; |
479 | 0 | } |
480 | | |
481 | | static int do_edit(int argc, char *argv[]) |
482 | 0 | { |
483 | 0 | bool is_vi = false; |
484 | 0 | int lastscrcol; |
485 | 0 | int i; |
486 | 0 | int linepos; |
487 | 0 | int c; |
488 | 0 | int ret = COMMAND_SUCCESS; |
489 | |
|
490 | 0 | if (argc != 2) |
491 | 0 | return COMMAND_ERROR_USAGE; |
492 | | |
493 | 0 | buffer = NULL; |
494 | 0 | if(edit_read_file(argv[1])) |
495 | 0 | return 1; |
496 | | |
497 | 0 | screenwidth = 80; |
498 | 0 | screenheight = 25; |
499 | | |
500 | | /* check if we are not called as "edit" */ |
501 | 0 | if (*argv[0] != 'e') { |
502 | 0 | smartscroll = 1; |
503 | 0 | term_getsize(&screenwidth, &screenheight); |
504 | | |
505 | | /* check if we are called as "vi" */ |
506 | 0 | if (*argv[0] == 'v') |
507 | 0 | is_vi = true; |
508 | 0 | } |
509 | |
|
510 | 0 | if (is_efi_console_active()) { |
511 | | /* |
512 | | * The EFI simple text output protocol wraps to the next line and |
513 | | * scrolls down when we write to the right bottom screen position. |
514 | | * Reduce the number of rows by one to work around this. |
515 | | */ |
516 | 0 | screenheight--; |
517 | | |
518 | | /* |
519 | | * Our console driver for the EFI simple text output protocol does |
520 | | * not implement the "\e[1S" sequence we use for scrolling the |
521 | | * screen. |
522 | | */ |
523 | 0 | smartscroll = 0; |
524 | 0 | } |
525 | |
|
526 | 0 | cursx = 0; |
527 | 0 | cursy = 0; |
528 | 0 | textx = 0; |
529 | 0 | scrcol = 0; |
530 | 0 | curline = buffer; |
531 | 0 | scrline = curline; |
532 | 0 | lastscrline = scrline; |
533 | 0 | lastscrcol = 0; |
534 | |
|
535 | 0 | printf("\x1b[2J"); |
536 | |
|
537 | 0 | term_setpos(0, -1); |
538 | |
|
539 | 0 | if (is_vi) { |
540 | 0 | screenheight -= 2; |
541 | 0 | printf("\x1b[7m%*c\x1b[0m", screenwidth , ' '); |
542 | 0 | term_setpos(0, screenheight-1); |
543 | 0 | printf("\x1b[7m%*c\x1b[0m", screenwidth , ' '); |
544 | 0 | printf("\r\x1b[7m%-25s\x1b[0m", argv[1]); |
545 | 0 | } else { |
546 | 0 | printf("\x1b[7m %-25s <ctrl-d>: Save and quit <ctrl-c>: quit \x1b[0m", |
547 | 0 | argv[1]); |
548 | 0 | } |
549 | |
|
550 | 0 | if (smartscroll) |
551 | 0 | printf("\x1b[2;%dr", screenheight); |
552 | |
|
553 | 0 | term_setpos(0, 0); |
554 | |
|
555 | 0 | screenheight--; /* status line */ |
556 | |
|
557 | 0 | refresh(1); |
558 | |
|
559 | 0 | while (1) { |
560 | 0 | int curlen = strlen(curline->data); |
561 | |
|
562 | 0 | if (textx > curlen) |
563 | 0 | textx = curlen; |
564 | 0 | if (textx < 1) |
565 | 0 | textx = 0; |
566 | |
|
567 | 0 | screenline(curline->data, &linepos); |
568 | |
|
569 | 0 | if (linepos > scrcol + screenwidth) |
570 | 0 | scrcol = linepos - screenwidth; |
571 | |
|
572 | 0 | if (scrcol > linepos) |
573 | 0 | scrcol = linepos; |
574 | |
|
575 | 0 | cursx = linepos - scrcol; |
576 | |
|
577 | 0 | while (cursy >= screenheight) { |
578 | 0 | cursy--; |
579 | 0 | scrline = scrline->next; |
580 | 0 | } |
581 | |
|
582 | 0 | while (cursy < 0) { |
583 | 0 | cursy++; |
584 | 0 | scrline = scrline->prev; |
585 | 0 | } |
586 | |
|
587 | 0 | if (scrline != lastscrline || scrcol != lastscrcol) |
588 | 0 | refresh(0); |
589 | |
|
590 | 0 | lastscrcol = scrcol; |
591 | 0 | lastscrline = scrline; |
592 | 0 | term_setpos(cursx, cursy); |
593 | |
|
594 | 0 | again: |
595 | 0 | c = read_modal_key(is_vi); |
596 | 0 | if (c == -EAGAIN) |
597 | 0 | goto again; |
598 | | |
599 | 0 | switch (c) { |
600 | 0 | case BB_KEY_UP: |
601 | 0 | if (!curline->prev) |
602 | 0 | continue; |
603 | | |
604 | 0 | curline = curline->prev; |
605 | 0 | cursy--; |
606 | 0 | textx = setpos(curline->data, linepos); |
607 | 0 | break; |
608 | 0 | case BB_KEY_DOWN: |
609 | 0 | if (!curline->next) |
610 | 0 | continue; |
611 | | |
612 | 0 | curline = curline->next; |
613 | 0 | cursy++; |
614 | 0 | textx = setpos(curline->data, linepos); |
615 | 0 | break; |
616 | 0 | case BB_KEY_RIGHT: |
617 | 0 | textx++; |
618 | 0 | break; |
619 | 0 | case BB_KEY_LEFT: |
620 | 0 | textx--; |
621 | 0 | break; |
622 | 0 | case BB_KEY_HOME: |
623 | 0 | textx = 0; |
624 | 0 | break; |
625 | 0 | case BB_KEY_END: |
626 | 0 | textx = curlen; |
627 | 0 | break; |
628 | 0 | case BB_KEY_PAGEUP: |
629 | 0 | for (i = 0; i < screenheight - 1; i++) { |
630 | 0 | if (!curline->prev) |
631 | 0 | break; |
632 | 0 | cursy--; |
633 | 0 | curline = curline->prev; |
634 | 0 | } |
635 | 0 | textx = setpos(curline->data, linepos); |
636 | 0 | break; |
637 | 0 | case BB_KEY_PAGEDOWN: |
638 | 0 | for (i = 0; i < screenheight - 1; i++) { |
639 | 0 | if (!curline->next) |
640 | 0 | break; |
641 | 0 | cursy++; |
642 | 0 | curline = curline->next; |
643 | 0 | } |
644 | 0 | textx = setpos(curline->data, linepos); |
645 | 0 | break; |
646 | 0 | case BB_KEY_DEL: |
647 | 0 | if (textx == curlen) { |
648 | 0 | if (curline->next) |
649 | 0 | merge_line(curline); |
650 | 0 | } else |
651 | 0 | delete_char(textx); |
652 | 0 | break; |
653 | 0 | case '\r': |
654 | 0 | case '\n': |
655 | 0 | split_line(); |
656 | 0 | break; |
657 | 0 | case 127: |
658 | 0 | case '\b': |
659 | 0 | if (textx > 0) { |
660 | 0 | textx--; |
661 | 0 | delete_char(textx); |
662 | 0 | } else { |
663 | 0 | if (!curline->prev) |
664 | 0 | break; |
665 | 0 | curline = curline->prev; |
666 | 0 | cursy--; |
667 | 0 | textx = strlen(curline->data); |
668 | 0 | merge_line(curline); |
669 | 0 | } |
670 | 0 | break; |
671 | 0 | case CTL_CH('d'): |
672 | 0 | ret = save_file(argv[1]); |
673 | 0 | goto out; |
674 | 0 | case CTL_CH('c'): |
675 | 0 | goto out; |
676 | 0 | default: |
677 | 0 | if ((signed char)c != -1) |
678 | 0 | insert_char(c); |
679 | 0 | } |
680 | 0 | } |
681 | 0 | out: |
682 | 0 | free_buffer(); |
683 | 0 | printf("\x1b[2J\x1b[r"); |
684 | 0 | printf("\n"); |
685 | 0 | return ret; |
686 | 0 | } |
687 | | |
688 | | static const char * const edit_aliases[] = { "sedit", "vi", NULL}; |
689 | | |
690 | | BAREBOX_CMD_HELP_START(edit) |
691 | | BAREBOX_CMD_HELP_TEXT("Use cursor keys, Ctrl-C to exit and Ctrl-D to exit-with-save.") |
692 | | BAREBOX_CMD_HELP_END |
693 | | |
694 | | BAREBOX_CMD_START(edit) |
695 | | .cmd = do_edit, |
696 | | .aliases = edit_aliases, |
697 | | BAREBOX_CMD_DESC("a small full-screen editor") |
698 | | BAREBOX_CMD_OPTS("FILE") |
699 | | BAREBOX_CMD_GROUP(CMD_GRP_CONSOLE) |
700 | | BAREBOX_CMD_HELP(cmd_edit_help) |
701 | | BAREBOX_CMD_END |