aboutsummaryrefslogtreecommitdiff
path: root/editors
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2017-05-29 14:20:10 +0100
committerRon Yorston <rmy@pobox.com>2017-05-29 14:34:28 +0100
commitda4f331955bed8afda670afcd58d524a04a0faa9 (patch)
treef6a3879aefdd714240f8c022375f687b512d2238 /editors
parent74163a535fd21f5fcca4c052d2e7c192d3e264fa (diff)
parent6683d1cbb44859f549f87f882545b84b9369585c (diff)
downloadbusybox-w32-da4f331955bed8afda670afcd58d524a04a0faa9.tar.gz
busybox-w32-da4f331955bed8afda670afcd58d524a04a0faa9.tar.bz2
busybox-w32-da4f331955bed8afda670afcd58d524a04a0faa9.zip
Merge branch 'busybox' into merge
Diffstat (limited to 'editors')
-rw-r--r--editors/diff.c37
-rw-r--r--editors/sed.c8
-rw-r--r--editors/vi.c68
3 files changed, 67 insertions, 46 deletions
diff --git a/editors/diff.c b/editors/diff.c
index f2eeb8257..975bc4603 100644
--- a/editors/diff.c
+++ b/editors/diff.c
@@ -295,17 +295,6 @@ static int search(const int *c, int k, int y, const struct cand *list)
295 } 295 }
296} 296}
297 297
298static unsigned isqrt(unsigned n)
299{
300 unsigned x = 1;
301 while (1) {
302 const unsigned y = x;
303 x = ((n / x) + x) >> 1;
304 if (x <= (y + 1) && x >= (y - 1))
305 return x;
306 }
307}
308
309static void stone(const int *a, int n, const int *b, int *J, int pref) 298static void stone(const int *a, int n, const int *b, int *J, int pref)
310{ 299{
311 const unsigned isq = isqrt(n); 300 const unsigned isq = isqrt(n);
@@ -730,9 +719,19 @@ static int diffreg(char *file[2])
730 fp[0] = stdin; 719 fp[0] = stdin;
731 fp[1] = stdin; 720 fp[1] = stdin;
732 for (i = 0; i < 2; i++) { 721 for (i = 0; i < 2; i++) {
733 int fd = open_or_warn_stdin(file[i]); 722 int fd = STDIN_FILENO;
734 if (fd == -1) 723 if (!LONE_DASH(file[i])) {
735 goto out; 724 if (!(option_mask32 & FLAG(N))) {
725 fd = open_or_warn(file[i], O_RDONLY);
726 if (fd == -1)
727 goto out;
728 } else {
729 /* -N: if some file does not exist compare it like empty */
730 fd = open(file[i], O_RDONLY);
731 if (fd == -1)
732 fd = xopen("/dev/null", O_RDONLY);
733 }
734 }
736 /* Our diff implementation is using seek. 735 /* Our diff implementation is using seek.
737 * When we meet non-seekable file, we must make a temp copy. 736 * When we meet non-seekable file, we must make a temp copy.
738 */ 737 */
@@ -1010,17 +1009,23 @@ int diff_main(int argc UNUSED_PARAM, char **argv)
1010 argv += optind; 1009 argv += optind;
1011 while (L_arg) 1010 while (L_arg)
1012 label[!!label[0]] = llist_pop(&L_arg); 1011 label[!!label[0]] = llist_pop(&L_arg);
1012
1013 /* Compat: "diff file name_which_doesnt_exist" exits with 2 */
1013 xfunc_error_retval = 2; 1014 xfunc_error_retval = 2;
1014 for (i = 0; i < 2; i++) { 1015 for (i = 0; i < 2; i++) {
1015 file[i] = argv[i]; 1016 file[i] = argv[i];
1016 /* Compat: "diff file name_which_doesnt_exist" exits with 2 */
1017 if (LONE_DASH(file[i])) { 1017 if (LONE_DASH(file[i])) {
1018 fstat(STDIN_FILENO, &stb[i]); 1018 fstat(STDIN_FILENO, &stb[i]);
1019 gotstdin++; 1019 gotstdin++;
1020 } else 1020 } else if (option_mask32 & FLAG(N)) {
1021 if (stat(file[i], &stb[i]))
1022 xstat("/dev/null", &stb[i]);
1023 } else {
1021 xstat(file[i], &stb[i]); 1024 xstat(file[i], &stb[i]);
1025 }
1022 } 1026 }
1023 xfunc_error_retval = 1; 1027 xfunc_error_retval = 1;
1028
1024 if (gotstdin && (S_ISDIR(stb[0].st_mode) || S_ISDIR(stb[1].st_mode))) 1029 if (gotstdin && (S_ISDIR(stb[0].st_mode) || S_ISDIR(stb[1].st_mode)))
1025 bb_error_msg_and_die("can't compare stdin to a directory"); 1030 bb_error_msg_and_die("can't compare stdin to a directory");
1026 1031
diff --git a/editors/sed.c b/editors/sed.c
index 86230ea42..b2c7ba829 100644
--- a/editors/sed.c
+++ b/editors/sed.c
@@ -337,7 +337,7 @@ static int get_address(const char *my_str, int *linenum, regex_t ** regex)
337 337
338 if (isdigit(*my_str)) { 338 if (isdigit(*my_str)) {
339 *linenum = strtol(my_str, (char**)&pos, 10); 339 *linenum = strtol(my_str, (char**)&pos, 10);
340 /* endstr shouldnt ever equal NULL */ 340 /* endstr shouldn't ever equal NULL */
341 } else if (*my_str == '$') { 341 } else if (*my_str == '$') {
342 *linenum = -1; 342 *linenum = -1;
343 pos++; 343 pos++;
@@ -444,7 +444,7 @@ static int parse_subst_cmd(sed_cmd_t *sed_cmd, const char *substr)
444 free(fname); 444 free(fname);
445 break; 445 break;
446 } 446 }
447 /* Ignore case (gnu exension) */ 447 /* Ignore case (gnu extension) */
448 case 'i': 448 case 'i':
449 case 'I': 449 case 'I':
450 cflags |= REG_ICASE; 450 cflags |= REG_ICASE;
@@ -587,7 +587,7 @@ static const char *parse_cmd_args(sed_cmd_t *sed_cmd, const char *cmdstr)
587 free(match); 587 free(match);
588 free(replace); 588 free(replace);
589 } 589 }
590 /* if it wasnt a single-letter command that takes no arguments 590 /* if it wasn't a single-letter command that takes no arguments
591 * then it must be an invalid command. 591 * then it must be an invalid command.
592 */ 592 */
593 else if (idx >= IDX_nul) { /* not d,D,g,G,h,H,l,n,N,p,P,q,x,=,{,} */ 593 else if (idx >= IDX_nul) { /* not d,D,g,G,h,H,l,n,N,p,P,q,x,=,{,} */
@@ -751,7 +751,7 @@ static void do_subst_w_backrefs(char *line, char *replace)
751 continue; 751 continue;
752 } 752 }
753 /* I _think_ it is impossible to get '\' to be 753 /* I _think_ it is impossible to get '\' to be
754 * the last char in replace string. Thus we dont check 754 * the last char in replace string. Thus we don't check
755 * for replace[i] == NUL. (counterexample anyone?) */ 755 * for replace[i] == NUL. (counterexample anyone?) */
756 /* if we find a backslash escaped character, print the character */ 756 /* if we find a backslash escaped character, print the character */
757 pipe_putc(replace[i]); 757 pipe_putc(replace[i]);
diff --git a/editors/vi.c b/editors/vi.c
index e5ca3adfa..900b41cb5 100644
--- a/editors/vi.c
+++ b/editors/vi.c
@@ -715,14 +715,6 @@ static int init_text_buffer(char *fn)
715{ 715{
716 int rc; 716 int rc;
717 717
718 flush_undo_data();
719 modified_count = 0;
720 last_modified_count = -1;
721#if ENABLE_FEATURE_VI_YANKMARK
722 /* init the marks */
723 memset(mark, 0, sizeof(mark));
724#endif
725
726 /* allocate/reallocate text buffer */ 718 /* allocate/reallocate text buffer */
727 free(text); 719 free(text);
728 text_size = 10240; 720 text_size = 10240;
@@ -737,6 +729,14 @@ static int init_text_buffer(char *fn)
737 // file doesnt exist. Start empty buf with dummy line 729 // file doesnt exist. Start empty buf with dummy line
738 char_insert(text, '\n', NO_UNDO); 730 char_insert(text, '\n', NO_UNDO);
739 } 731 }
732
733 flush_undo_data();
734 modified_count = 0;
735 last_modified_count = -1;
736#if ENABLE_FEATURE_VI_YANKMARK
737 /* init the marks */
738 memset(mark, 0, sizeof(mark));
739#endif
740 return rc; 740 return rc;
741} 741}
742 742
@@ -1034,7 +1034,9 @@ static void colon(char *buf)
1034 || strncmp(p, "wn", cnt) == 0 1034 || strncmp(p, "wn", cnt) == 0
1035 || (p[0] == 'x' && !p[1]) 1035 || (p[0] == 'x' && !p[1])
1036 ) { 1036 ) {
1037 cnt = file_write(current_filename, text, end - 1); 1037 if (modified_count != 0 || p[0] != 'x') {
1038 cnt = file_write(current_filename, text, end - 1);
1039 }
1038 if (cnt < 0) { 1040 if (cnt < 0) {
1039 if (cnt == -1) 1041 if (cnt == -1)
1040 status_line_bold("Write error: %s", strerror(errno)); 1042 status_line_bold("Write error: %s", strerror(errno));
@@ -1045,8 +1047,9 @@ static void colon(char *buf)
1045 current_filename, 1047 current_filename,
1046 count_lines(text, end - 1), cnt 1048 count_lines(text, end - 1), cnt
1047 ); 1049 );
1048 if (p[0] == 'x' || p[1] == 'q' || p[1] == 'n' 1050 if (p[0] == 'x'
1049 || p[0] == 'X' || p[1] == 'Q' || p[1] == 'N' 1051 || p[1] == 'q' || p[1] == 'n'
1052 || p[1] == 'Q' || p[1] == 'N'
1050 ) { 1053 ) {
1051 editing = 0; 1054 editing = 0;
1052 } 1055 }
@@ -1476,16 +1479,19 @@ static void colon(char *buf)
1476 goto ret; 1479 goto ret;
1477 } 1480 }
1478#endif 1481#endif
1479 // how many lines in text[]?
1480 li = count_lines(q, r);
1481 size = r - q + 1;
1482 //if (useforce) { 1482 //if (useforce) {
1483 // if "fn" is not write-able, chmod u+w 1483 // if "fn" is not write-able, chmod u+w
1484 // sprintf(syscmd, "chmod u+w %s", fn); 1484 // sprintf(syscmd, "chmod u+w %s", fn);
1485 // system(syscmd); 1485 // system(syscmd);
1486 // forced = TRUE; 1486 // forced = TRUE;
1487 //} 1487 //}
1488 l = file_write(fn, q, r); 1488 if (modified_count != 0 || cmd[0] != 'x') {
1489 size = r - q + 1;
1490 l = file_write(fn, q, r);
1491 } else {
1492 size = 0;
1493 l = 0;
1494 }
1489 //if (useforce && forced) { 1495 //if (useforce && forced) {
1490 // chmod u-w 1496 // chmod u-w
1491 // sprintf(syscmd, "chmod u-w %s", fn); 1497 // sprintf(syscmd, "chmod u-w %s", fn);
@@ -1496,17 +1502,20 @@ static void colon(char *buf)
1496 if (l == -1) 1502 if (l == -1)
1497 status_line_bold_errno(fn); 1503 status_line_bold_errno(fn);
1498 } else { 1504 } else {
1505 // how many lines written
1506 li = count_lines(q, q + l - 1);
1499 status_line("'%s' %dL, %dC", fn, li, l); 1507 status_line("'%s' %dL, %dC", fn, li, l);
1500 if (q == text && r == end - 1 && l == size) { 1508 if (l == size) {
1501 modified_count = 0; 1509 if (q == text && q + l == end) {
1502 last_modified_count = -1; 1510 modified_count = 0;
1503 } 1511 last_modified_count = -1;
1504 if ((cmd[0] == 'x' || cmd[1] == 'q' || cmd[1] == 'n' 1512 }
1505 || cmd[0] == 'X' || cmd[1] == 'Q' || cmd[1] == 'N' 1513 if (cmd[0] == 'x'
1506 ) 1514 || cmd[1] == 'q' || cmd[1] == 'n'
1507 && l == size 1515 || cmd[1] == 'Q' || cmd[1] == 'N'
1508 ) { 1516 ) {
1509 editing = 0; 1517 editing = 0;
1518 }
1510 } 1519 }
1511 } 1520 }
1512#if ENABLE_FEATURE_VI_YANKMARK 1521#if ENABLE_FEATURE_VI_YANKMARK
@@ -2816,8 +2825,15 @@ static int readit(void) // read (maybe cursor) key from stdin
2816 int c; 2825 int c;
2817 2826
2818 fflush_all(); 2827 fflush_all();
2819 c = read_key(STDIN_FILENO, readbuffer, /*timeout off:*/ -2); 2828
2829 // Wait for input. TIMEOUT = -1 makes read_key wait even
2830 // on nonblocking stdin.
2831 // Note: read_key sets errno to 0 on success.
2832 again:
2833 c = read_key(STDIN_FILENO, readbuffer, /*timeout:*/ -1);
2820 if (c == -1) { // EOF/error 2834 if (c == -1) { // EOF/error
2835 if (errno == EAGAIN) // paranoia
2836 goto again;
2821 go_bottom_and_clear_to_eol(); 2837 go_bottom_and_clear_to_eol();
2822 cookmode(); // terminal to "cooked" 2838 cookmode(); // terminal to "cooked"
2823 bb_error_msg_and_die("can't read user input"); 2839 bb_error_msg_and_die("can't read user input");