aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277>2006-12-21 22:27:10 +0000
committervda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277>2006-12-21 22:27:10 +0000
commitd8c6309724259aa293ac194650a154c4dbc154db (patch)
treed6b95ea26c3c4dd503a565eb01cadce47b090202
parent0840d2172285bf4c59e4d32289a0d3157fe18285 (diff)
downloadbusybox-w32-d8c6309724259aa293ac194650a154c4dbc154db.tar.gz
busybox-w32-d8c6309724259aa293ac194650a154c4dbc154db.tar.bz2
busybox-w32-d8c6309724259aa293ac194650a154c4dbc154db.zip
cmdedit: reformat code a bit, no algorithm changes
git-svn-id: svn://busybox.net/trunk/busybox@17037 69ca8d6d-28ef-0310-b511-8ec308f3f277
-rw-r--r--shell/cmdedit.c463
1 files changed, 230 insertions, 233 deletions
diff --git a/shell/cmdedit.c b/shell/cmdedit.c
index 8409bb267..68772fe05 100644
--- a/shell/cmdedit.c
+++ b/shell/cmdedit.c
@@ -78,7 +78,7 @@ static int cur_history;
78#endif 78#endif
79 79
80//#include <termios.h> 80//#include <termios.h>
81#define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp) 81#define setTermSettings(fd,argp) tcsetattr(fd, TCSANOW, argp)
82#define getTermSettings(fd,argp) tcgetattr(fd, argp); 82#define getTermSettings(fd,argp) tcgetattr(fd, argp);
83 83
84/* Current termio and the previous termio before starting sh */ 84/* Current termio and the previous termio before starting sh */
@@ -136,7 +136,7 @@ static void win_changed(int nsig)
136{ 136{
137 static sighandler_t previous_SIGWINCH_handler; /* for reset */ 137 static sighandler_t previous_SIGWINCH_handler; /* for reset */
138 138
139 /* emulate || signal call */ 139 /* emulate || signal call */
140 if (nsig == -SIGWINCH || nsig == SIGWINCH) { 140 if (nsig == -SIGWINCH || nsig == SIGWINCH) {
141 int width = 0; 141 int width = 0;
142 get_terminal_width_height(0, &width, NULL); 142 get_terminal_width_height(0, &width, NULL);
@@ -155,12 +155,12 @@ static void win_changed(int nsig)
155 155
156static void cmdedit_reset_term(void) 156static void cmdedit_reset_term(void)
157{ 157{
158 if ((handlers_sets & SET_RESET_TERM) != 0) { 158 if (handlers_sets & SET_RESET_TERM) {
159/* sparc and other have broken termios support: use old termio handling. */ 159/* sparc and other have broken termios support: use old termio handling. */
160 setTermSettings(STDIN_FILENO, (void *) &initial_settings); 160 setTermSettings(STDIN_FILENO, (void *) &initial_settings);
161 handlers_sets &= ~SET_RESET_TERM; 161 handlers_sets &= ~SET_RESET_TERM;
162 } 162 }
163 if ((handlers_sets & SET_WCHG_HANDLERS) != 0) { 163 if (handlers_sets & SET_WCHG_HANDLERS) {
164 /* reset SIGWINCH handler to previous (default) */ 164 /* reset SIGWINCH handler to previous (default) */
165 win_changed(0); 165 win_changed(0);
166 handlers_sets &= ~SET_WCHG_HANDLERS; 166 handlers_sets &= ~SET_WCHG_HANDLERS;
@@ -172,8 +172,7 @@ static void cmdedit_reset_term(void)
172/* special for recount position for scroll and remove terminal margin effect */ 172/* special for recount position for scroll and remove terminal margin effect */
173static void cmdedit_set_out_char(int next_char) 173static void cmdedit_set_out_char(int next_char)
174{ 174{
175 175 int c = (unsigned char)command_ps[cursor];
176 int c = (int)((unsigned char) command_ps[cursor]);
177 176
178 if (c == 0) 177 if (c == 0)
179 c = ' '; /* destroy end char? */ 178 c = ' '; /* destroy end char? */
@@ -188,7 +187,10 @@ static void cmdedit_set_out_char(int next_char)
188 printf("\033[7m%c\033[0m", c); 187 printf("\033[7m%c\033[0m", c);
189 } else 188 } else
190#endif 189#endif
191 if (initial_settings.c_lflag & ECHO) putchar(c); 190 {
191 if (initial_settings.c_lflag & ECHO)
192 putchar(c);
193 }
192 if (++cmdedit_x >= cmdedit_termw) { 194 if (++cmdedit_x >= cmdedit_termw) {
193 /* terminal is scrolled down */ 195 /* terminal is scrolled down */
194 cmdedit_y++; 196 cmdedit_y++;
@@ -221,7 +223,7 @@ static void goto_new_line(void)
221 223
222static void out1str(const char *s) 224static void out1str(const char *s)
223{ 225{
224 if ( s ) 226 if (s)
225 fputs(s, stdout); 227 fputs(s, stdout);
226} 228}
227 229
@@ -255,7 +257,7 @@ static void input_backward(int num)
255 count_y = 1 + num / cmdedit_termw; 257 count_y = 1 + num / cmdedit_termw;
256 printf("\033[%dA", count_y); 258 printf("\033[%dA", count_y);
257 cmdedit_y -= count_y; 259 cmdedit_y -= count_y;
258 /* require forward after uping */ 260 /* require forward after uping */
259 cmdedit_x = cmdedit_termw * count_y - num; 261 cmdedit_x = cmdedit_termw * count_y - num;
260 printf("\033[%dC", cmdedit_x); /* set term cursor */ 262 printf("\033[%dC", cmdedit_x); /* set term cursor */
261 } 263 }
@@ -281,20 +283,20 @@ static void parse_prompt(const char *prmt_ptr)
281{ 283{
282 int prmt_len = 0; 284 int prmt_len = 0;
283 size_t cur_prmt_len = 0; 285 size_t cur_prmt_len = 0;
284 char flg_not_length = '['; 286 char flg_not_length = '[';
285 char *prmt_mem_ptr = xzalloc(1); 287 char *prmt_mem_ptr = xzalloc(1);
286 char *pwd_buf = xgetcwd(0); 288 char *pwd_buf = xgetcwd(0);
287 char buf2[PATH_MAX + 1]; 289 char buf2[PATH_MAX + 1];
288 char buf[2]; 290 char buf[2];
289 char c; 291 char c;
290 char *pbuf; 292 char *pbuf;
291 293
292 if (!pwd_buf) { 294 if (!pwd_buf) {
293 pwd_buf=(char *)bb_msg_unknown; 295 pwd_buf = (char *)bb_msg_unknown;
294 } 296 }
295 297
296 while (*prmt_ptr) { 298 while (*prmt_ptr) {
297 pbuf = buf; 299 pbuf = buf;
298 pbuf[1] = 0; 300 pbuf[1] = 0;
299 c = *prmt_ptr++; 301 c = *prmt_ptr++;
300 if (c == '\\') { 302 if (c == '\\') {
@@ -302,85 +304,85 @@ static void parse_prompt(const char *prmt_ptr)
302 int l; 304 int l;
303 305
304 c = bb_process_escape_sequence(&prmt_ptr); 306 c = bb_process_escape_sequence(&prmt_ptr);
305 if (prmt_ptr==cp) { 307 if (prmt_ptr == cp) {
306 if (*cp == 0) 308 if (*cp == 0)
307 break; 309 break;
308 c = *prmt_ptr++; 310 c = *prmt_ptr++;
309 switch (c) { 311 switch (c) {
310#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR 312#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
311 case 'u': 313 case 'u':
312 pbuf = user_buf; 314 pbuf = user_buf;
313 break; 315 break;
314#endif 316#endif
315 case 'h': 317 case 'h':
316 pbuf = hostname_buf; 318 pbuf = hostname_buf;
317 if (pbuf == 0) { 319 if (pbuf == 0) {
318 pbuf = xzalloc(256); 320 pbuf = xzalloc(256);
319 if (gethostname(pbuf, 255) < 0) { 321 if (gethostname(pbuf, 255) < 0) {
320 strcpy(pbuf, "?"); 322 strcpy(pbuf, "?");
321 } else { 323 } else {
322 char *s = strchr(pbuf, '.'); 324 char *s = strchr(pbuf, '.');
323 325 if (s)
324 if (s) 326 *s = 0;
325 *s = 0; 327 }
328 hostname_buf = pbuf;
326 } 329 }
327 hostname_buf = pbuf; 330 break;
328 } 331 case '$':
329 break; 332 c = (my_euid == 0 ? '#' : '$');
330 case '$': 333 break;
331 c = my_euid == 0 ? '#' : '$';
332 break;
333#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR 334#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
334 case 'w': 335 case 'w':
335 pbuf = pwd_buf; 336 pbuf = pwd_buf;
336 l = strlen(home_pwd_buf); 337 l = strlen(home_pwd_buf);
337 if (home_pwd_buf[0] != 0 && 338 if (home_pwd_buf[0] != 0
338 strncmp(home_pwd_buf, pbuf, l) == 0 && 339 && strncmp(home_pwd_buf, pbuf, l) == 0
339 (pbuf[l]=='/' || pbuf[l]=='\0') && 340 && (pbuf[l]=='/' || pbuf[l]=='\0')
340 strlen(pwd_buf+l)<PATH_MAX) { 341 && strlen(pwd_buf+l)<PATH_MAX
341 pbuf = buf2; 342 ) {
342 *pbuf = '~'; 343 pbuf = buf2;
343 strcpy(pbuf+1, pwd_buf+l); 344 *pbuf = '~';
344 } 345 strcpy(pbuf+1, pwd_buf+l);
345 break; 346 }
347 break;
346#endif 348#endif
347 case 'W': 349 case 'W':
348 pbuf = pwd_buf; 350 pbuf = pwd_buf;
349 cp = strrchr(pbuf,'/'); 351 cp = strrchr(pbuf,'/');
350 if ( (cp != NULL) && (cp != pbuf) ) 352 if (cp != NULL && cp != pbuf)
351 pbuf += (cp-pbuf)+1; 353 pbuf += (cp-pbuf) + 1;
352 break; 354 break;
353 case '!': 355 case '!':
354 snprintf(pbuf = buf2, sizeof(buf2), "%d", num_ok_lines); 356 snprintf(pbuf = buf2, sizeof(buf2), "%d", num_ok_lines);
355 break; 357 break;
356 case 'e': case 'E': /* \e \E = \033 */ 358 case 'e': case 'E': /* \e \E = \033 */
357 c = '\033'; 359 c = '\033';
358 break; 360 break;
359 case 'x': case 'X': 361 case 'x': case 'X':
360 for (l = 0; l < 3;) { 362 for (l = 0; l < 3;) {
361 int h; 363 int h;
362 buf2[l++] = *prmt_ptr; 364 buf2[l++] = *prmt_ptr;
365 buf2[l] = 0;
366 h = strtol(buf2, &pbuf, 16);
367 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
368 l--;
369 break;
370 }
371 prmt_ptr++;
372 }
363 buf2[l] = 0; 373 buf2[l] = 0;
364 h = strtol(buf2, &pbuf, 16); 374 c = (char)strtol(buf2, 0, 16);
365 if (h > UCHAR_MAX || (pbuf - buf2) < l) { 375 if (c == 0)
366 l--; 376 c = '?';
367 break; 377 pbuf = buf;
378 break;
379 case '[': case ']':
380 if (c == flg_not_length) {
381 flg_not_length = flg_not_length == '[' ? ']' : '[';
382 continue;
368 } 383 }
369 prmt_ptr++; 384 break;
370 }
371 buf2[l] = 0;
372 c = (char)strtol(buf2, 0, 16);
373 if (c==0)
374 c = '?';
375 pbuf = buf;
376 break;
377 case '[': case ']':
378 if (c == flg_not_length) {
379 flg_not_length = flg_not_length == '[' ? ']' : '[';
380 continue;
381 } 385 }
382 break;
383 }
384 } 386 }
385 } 387 }
386 if (pbuf == buf) 388 if (pbuf == buf)
@@ -460,7 +462,7 @@ static void put(void)
460 strncpy(command_ps + cursor, delbuf, j); 462 strncpy(command_ps + cursor, delbuf, j);
461 len += j; 463 len += j;
462 input_end(); /* rewrite new line */ 464 input_end(); /* rewrite new line */
463 input_backward(cursor-ocursor-j+1); /* at end of new text */ 465 input_backward(cursor - ocursor - j + 1); /* at end of new text */
464} 466}
465#endif 467#endif
466 468
@@ -504,13 +506,13 @@ static void cmdedit_setwidth(int w, int redraw_flg)
504static void cmdedit_init(void) 506static void cmdedit_init(void)
505{ 507{
506 cmdedit_prmt_len = 0; 508 cmdedit_prmt_len = 0;
507 if ((handlers_sets & SET_WCHG_HANDLERS) == 0) { 509 if (!(handlers_sets & SET_WCHG_HANDLERS)) {
508 /* emulate usage handler to set handler and call yours work */ 510 /* emulate usage handler to set handler and call yours work */
509 win_changed(-SIGWINCH); 511 win_changed(-SIGWINCH);
510 handlers_sets |= SET_WCHG_HANDLERS; 512 handlers_sets |= SET_WCHG_HANDLERS;
511 } 513 }
512 514
513 if ((handlers_sets & SET_ATEXIT) == 0) { 515 if (!(handlers_sets & SET_ATEXIT)) {
514#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR 516#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
515 struct passwd *entry; 517 struct passwd *entry;
516 518
@@ -553,10 +555,13 @@ static void add_match(char *matched)
553/* 555/*
554static int is_execute(const struct stat *st) 556static int is_execute(const struct stat *st)
555{ 557{
556 if ((!my_euid && (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) || 558 if ((!my_euid && (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
557 (my_uid == st->st_uid && (st->st_mode & S_IXUSR)) || 559 || (my_uid == st->st_uid && (st->st_mode & S_IXUSR))
558 (my_gid == st->st_gid && (st->st_mode & S_IXGRP)) || 560 || (my_gid == st->st_gid && (st->st_mode & S_IXGRP))
559 (st->st_mode & S_IXOTH)) return TRUE; 561 || (st->st_mode & S_IXOTH)
562 ) {
563 return TRUE;
564 }
560 return FALSE; 565 return FALSE;
561} 566}
562*/ 567*/
@@ -629,26 +634,25 @@ static int path_parse(char ***p, int flags)
629{ 634{
630 int npth; 635 int npth;
631 const char *tmp; 636 const char *tmp;
632 const char *pth; 637 const char *pth = cmdedit_path_lookup;
633 638
634 /* if not setenv PATH variable, to search cur dir "." */ 639 /* if not setenv PATH variable, to search cur dir "." */
635 if (flags != FIND_EXE_ONLY || (pth = cmdedit_path_lookup) == 0 || 640 if (flags != FIND_EXE_ONLY)
636 /* PATH=<empty> or PATH=:<empty> */ 641 return 1;
637 *pth == 0 || (*pth == ':' && *(pth + 1) == 0)) { 642 /* PATH=<empty> or PATH=:<empty> */
643 if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
638 return 1; 644 return 1;
639 }
640 645
641 tmp = pth; 646 tmp = pth;
642 npth = 0; 647 npth = 0;
643 648
644 for (;;) { 649 while (1) {
645 npth++; /* count words is + 1 count ':' */ 650 npth++; /* count words is + 1 count ':' */
646 tmp = strchr(tmp, ':'); 651 tmp = strchr(tmp, ':');
647 if (tmp) { 652 if (!tmp)
648 if (*++tmp == 0)
649 break; /* :<empty> */
650 } else
651 break; 653 break;
654 if (*++tmp == 0)
655 break; /* :<empty> */
652 } 656 }
653 657
654 *p = xmalloc(npth * sizeof(char *)); 658 *p = xmalloc(npth * sizeof(char *));
@@ -657,14 +661,13 @@ static int path_parse(char ***p, int flags)
657 (*p)[0] = xstrdup(tmp); 661 (*p)[0] = xstrdup(tmp);
658 npth = 1; /* count words is + 1 count ':' */ 662 npth = 1; /* count words is + 1 count ':' */
659 663
660 for (;;) { 664 while (1) {
661 tmp = strchr(tmp, ':'); 665 tmp = strchr(tmp, ':');
662 if (tmp) { 666 if (!tmp)
663 (*p)[0][(tmp - pth)] = 0; /* ':' -> '\0' */
664 if (*++tmp == 0)
665 break; /* :<empty> */
666 } else
667 break; 667 break;
668 (*p)[0][(tmp - pth)] = 0; /* ':' -> '\0' */
669 if (*++tmp == 0)
670 break; /* :<empty> */
668 (*p)[npth++] = &(*p)[0][(tmp - pth)]; /* p[next]=p[0][&'\0'+1] */ 671 (*p)[npth++] = &(*p)[0][(tmp - pth)]; /* p[next]=p[0][&'\0'+1] */
669 } 672 }
670 673
@@ -728,28 +731,28 @@ static void exe_n_cwd_tab_completion(char *command, int type)
728 continue; 731 continue;
729 732
730 while ((next = readdir(dir)) != NULL) { 733 while ((next = readdir(dir)) != NULL) {
734 int len1;
731 char *str_found = next->d_name; 735 char *str_found = next->d_name;
732 736
733 /* matched ? */ 737 /* matched? */
734 if (strncmp(str_found, pfind, strlen(pfind))) 738 if (strncmp(str_found, pfind, strlen(pfind)))
735 continue; 739 continue;
736 /* not see .name without .match */ 740 /* not see .name without .match */
737 if (*str_found == '.' && *pfind == 0) { 741 if (*str_found == '.' && *pfind == 0) {
738 if (*paths[i] == '/' && paths[i][1] == 0 742 if (NOT_LONE_CHAR(paths[i], '/') || str_found[1])
739 && str_found[1] == 0) str_found = ""; /* only "/" */
740 else
741 continue; 743 continue;
744 str_found = ""; /* only "/" */
742 } 745 }
743 found = concat_path_file(paths[i], str_found); 746 found = concat_path_file(paths[i], str_found);
744 /* hmm, remover in progress? */ 747 /* hmm, remover in progress? */
745 if (stat(found, &st) < 0) 748 if (stat(found, &st) < 0)
746 goto cont; 749 goto cont;
747 /* find with dirs ? */ 750 /* find with dirs? */
748 if (paths[i] != dirbuf) 751 if (paths[i] != dirbuf)
749 strcpy(found, next->d_name); /* only name */ 752 strcpy(found, next->d_name); /* only name */
750 753
751 int len1 = strlen(found); 754 len1 = strlen(found);
752 found = xrealloc(found, len1+2); 755 found = xrealloc(found, len1 + 2);
753 found[len1] = '\0'; 756 found[len1] = '\0';
754 found[len1+1] = '\0'; 757 found[len1+1] = '\0';
755 758
@@ -766,7 +769,7 @@ static void exe_n_cwd_tab_completion(char *command, int type)
766 /* Add it to the list */ 769 /* Add it to the list */
767 add_match(found); 770 add_match(found);
768 continue; 771 continue;
769cont: 772 cont:
770 free(found); 773 free(found);
771 } 774 }
772 closedir(dir); 775 closedir(dir);
@@ -778,7 +781,7 @@ cont:
778} 781}
779 782
780 783
781#define QUOT (UCHAR_MAX+1) 784#define QUOT (UCHAR_MAX+1)
782 785
783#define collapse_pos(is, in) { \ 786#define collapse_pos(is, in) { \
784 memmove(int_buf+(is), int_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); \ 787 memmove(int_buf+(is), int_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); \
@@ -794,7 +797,7 @@ static int find_match(char *matchBuf, int *len_with_quotes)
794 797
795 /* set to integer dimension characters and own positions */ 798 /* set to integer dimension characters and own positions */
796 for (i = 0;; i++) { 799 for (i = 0;; i++) {
797 int_buf[i] = (int) ((unsigned char) matchBuf[i]); 800 int_buf[i] = (unsigned char)matchBuf[i];
798 if (int_buf[i] == 0) { 801 if (int_buf[i] == 0) {
799 pos_buf[i] = -1; /* indicator end line */ 802 pos_buf[i] = -1; /* indicator end line */
800 break; 803 break;
@@ -835,7 +838,7 @@ static int find_match(char *matchBuf, int *len_with_quotes)
835 int_buf[i] |= QUOT; 838 int_buf[i] |= QUOT;
836 } 839 }
837 840
838 /* skip commands with arguments if line have commands delimiters */ 841 /* skip commands with arguments if line has commands delimiters */
839 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */ 842 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
840 for (i = 0; int_buf[i]; i++) { 843 for (i = 0; int_buf[i]; i++) {
841 c = int_buf[i]; 844 c = int_buf[i];
@@ -906,16 +909,17 @@ static int find_match(char *matchBuf, int *len_with_quotes)
906 for (i = 0; int_buf[i]; i++) 909 for (i = 0; int_buf[i]; i++)
907 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') { 910 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
908 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY 911 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
909 && matchBuf[pos_buf[0]]=='c' 912 && matchBuf[pos_buf[0]]=='c'
910 && matchBuf[pos_buf[1]]=='d' ) 913 && matchBuf[pos_buf[1]]=='d'
914 ) {
911 command_mode = FIND_DIR_ONLY; 915 command_mode = FIND_DIR_ONLY;
912 else { 916 } else {
913 command_mode = FIND_FILE_ONLY; 917 command_mode = FIND_FILE_ONLY;
914 break; 918 break;
915 } 919 }
916 } 920 }
917 /* "strlen" */ 921 for (i = 0; int_buf[i]; i++)
918 for (i = 0; int_buf[i]; i++); 922 /* "strlen" */;
919 /* find last word */ 923 /* find last word */
920 for (--i; i >= 0; i--) { 924 for (--i; i >= 0; i--) {
921 c = int_buf[i]; 925 c = int_buf[i];
@@ -925,11 +929,12 @@ static int find_match(char *matchBuf, int *len_with_quotes)
925 } 929 }
926 } 930 }
927 /* skip first not quoted '\'' or '"' */ 931 /* skip first not quoted '\'' or '"' */
928 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++); 932 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++)
933 /*skip*/;
929 /* collapse quote or unquote // or /~ */ 934 /* collapse quote or unquote // or /~ */
930 while ((int_buf[i] & ~QUOT) == '/' && 935 while ((int_buf[i] & ~QUOT) == '/'
931 ((int_buf[i + 1] & ~QUOT) == '/' 936 && ((int_buf[i+1] & ~QUOT) == '/' || (int_buf[i+1] & ~QUOT) == '~')
932 || (int_buf[i + 1] & ~QUOT) == '~')) { 937 ) {
933 i++; 938 i++;
934 } 939 }
935 940
@@ -978,9 +983,9 @@ static void showfiles(void)
978 int n = row; 983 int n = row;
979 int nc; 984 int nc;
980 985
981 for(nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) { 986 for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
982 printf("%s%-*s", matches[n], 987 printf("%s%-*s", matches[n],
983 column_width - strlen(matches[n]), ""); 988 column_width - strlen(matches[n]), "");
984 } 989 }
985 printf("%s\n", matches[n]); 990 printf("%s\n", matches[n]);
986 } 991 }
@@ -1003,7 +1008,7 @@ static void input_tab(int *lastWasTab)
1003 } 1008 }
1004 return; 1009 return;
1005 } 1010 }
1006 if (! *lastWasTab) { 1011 if (!*lastWasTab) {
1007 char *tmp, *tmp1; 1012 char *tmp, *tmp1;
1008 int len_found; 1013 int len_found;
1009 char matchBuf[BUFSIZ]; 1014 char matchBuf[BUFSIZ];
@@ -1143,21 +1148,21 @@ static int get_next_history(void)
1143} 1148}
1144 1149
1145#if ENABLE_FEATURE_COMMAND_SAVEHISTORY 1150#if ENABLE_FEATURE_COMMAND_SAVEHISTORY
1146void load_history ( const char *fromfile ) 1151void load_history(const char *fromfile)
1147{ 1152{
1148 FILE *fp; 1153 FILE *fp;
1149 int hi; 1154 int hi;
1150 1155
1151 /* cleanup old */ 1156 /* cleanup old */
1152 1157
1153 for(hi = n_history; hi > 0; ) { 1158 for (hi = n_history; hi > 0;) {
1154 hi--; 1159 hi--;
1155 free ( history [hi] ); 1160 free(history[hi]);
1156 } 1161 }
1157 1162
1158 if (( fp = fopen ( fromfile, "r" ))) { 1163 fp = fopen(fromfile, "r");
1159 1164 if (fp) {
1160 for ( hi = 0; hi < MAX_HISTORY; ) { 1165 for (hi = 0; hi < MAX_HISTORY;) {
1161 char * hl = xmalloc_getline(fp); 1166 char * hl = xmalloc_getline(fp);
1162 int l; 1167 int l;
1163 1168
@@ -1170,24 +1175,24 @@ void load_history ( const char *fromfile )
1170 free(hl); 1175 free(hl);
1171 continue; 1176 continue;
1172 } 1177 }
1173 history [hi++] = hl; 1178 history[hi++] = hl;
1174 } 1179 }
1175 fclose ( fp ); 1180 fclose(fp);
1176 } 1181 }
1177 cur_history = n_history = hi; 1182 cur_history = n_history = hi;
1178} 1183}
1179 1184
1180void save_history ( const char *tofile ) 1185void save_history (const char *tofile)
1181{ 1186{
1182 FILE *fp = fopen ( tofile, "w" ); 1187 FILE *fp = fopen(tofile, "w");
1183 1188
1184 if ( fp ) { 1189 if (fp) {
1185 int i; 1190 int i;
1186 1191
1187 for ( i = 0; i < n_history; i++ ) { 1192 for (i = 0; i < n_history; i++) {
1188 fprintf(fp, "%s\n", history [i]); 1193 fprintf(fp, "%s\n", history[i]);
1189 } 1194 }
1190 fclose ( fp ); 1195 fclose(fp);
1191 } 1196 }
1192} 1197}
1193#endif 1198#endif
@@ -1239,13 +1244,11 @@ static void
1239vi_word_motion(char *command, int eat) 1244vi_word_motion(char *command, int eat)
1240{ 1245{
1241 if (isalnum(command[cursor]) || command[cursor] == '_') { 1246 if (isalnum(command[cursor]) || command[cursor] == '_') {
1242 while (cursor < len && 1247 while (cursor < len
1243 (isalnum(command[cursor+1]) || 1248 && (isalnum(command[cursor+1]) || command[cursor+1] == '_'))
1244 command[cursor+1] == '_'))
1245 input_forward(); 1249 input_forward();
1246 } else if (ispunct(command[cursor])) { 1250 } else if (ispunct(command[cursor])) {
1247 while (cursor < len && 1251 while (cursor < len && ispunct(command[cursor+1]))
1248 (ispunct(command[cursor+1])))
1249 input_forward(); 1252 input_forward();
1250 } 1253 }
1251 1254
@@ -1278,13 +1281,13 @@ vi_end_motion(char *command)
1278 if (cursor >= len-1) 1281 if (cursor >= len-1)
1279 return; 1282 return;
1280 if (isalnum(command[cursor]) || command[cursor] == '_') { 1283 if (isalnum(command[cursor]) || command[cursor] == '_') {
1281 while (cursor < len-1 && 1284 while (cursor < len-1
1282 (isalnum(command[cursor+1]) || 1285 && (isalnum(command[cursor+1]) || command[cursor+1] == '_')
1283 command[cursor+1] == '_')) 1286 ) {
1284 input_forward(); 1287 input_forward();
1288 }
1285 } else if (ispunct(command[cursor])) { 1289 } else if (ispunct(command[cursor])) {
1286 while (cursor < len-1 && 1290 while (cursor < len-1 && ispunct(command[cursor+1]))
1287 (ispunct(command[cursor+1])))
1288 input_forward(); 1291 input_forward();
1289 } 1292 }
1290} 1293}
@@ -1309,13 +1312,13 @@ vi_back_motion(char *command)
1309 if (cursor <= 0) 1312 if (cursor <= 0)
1310 return; 1313 return;
1311 if (isalnum(command[cursor]) || command[cursor] == '_') { 1314 if (isalnum(command[cursor]) || command[cursor] == '_') {
1312 while (cursor > 0 && 1315 while (cursor > 0
1313 (isalnum(command[cursor-1]) || 1316 && (isalnum(command[cursor-1]) || command[cursor-1] == '_')
1314 command[cursor-1] == '_')) 1317 ) {
1315 input_backward(1); 1318 input_backward(1);
1319 }
1316 } else if (ispunct(command[cursor])) { 1320 } else if (ispunct(command[cursor])) {
1317 while (cursor > 0 && 1321 while (cursor > 0 && ispunct(command[cursor-1]))
1318 (ispunct(command[cursor-1])))
1319 input_backward(1); 1322 input_backward(1);
1320 } 1323 }
1321} 1324}
@@ -1341,7 +1344,6 @@ vi_back_motion(char *command)
1341 1344
1342int cmdedit_read_input(char *prompt, char command[BUFSIZ]) 1345int cmdedit_read_input(char *prompt, char command[BUFSIZ])
1343{ 1346{
1344
1345 int break_out = 0; 1347 int break_out = 0;
1346 int lastWasTab = FALSE; 1348 int lastWasTab = FALSE;
1347 unsigned char c; 1349 unsigned char c;
@@ -1364,9 +1366,9 @@ int cmdedit_read_input(char *prompt, char command[BUFSIZ])
1364 new_settings.c_cc[VMIN] = 1; 1366 new_settings.c_cc[VMIN] = 1;
1365 new_settings.c_cc[VTIME] = 0; 1367 new_settings.c_cc[VTIME] = 0;
1366 /* Turn off CTRL-C, so we can trap it */ 1368 /* Turn off CTRL-C, so we can trap it */
1367# ifndef _POSIX_VDISABLE 1369# ifndef _POSIX_VDISABLE
1368# define _POSIX_VDISABLE '\0' 1370# define _POSIX_VDISABLE '\0'
1369# endif 1371# endif
1370 new_settings.c_cc[VINTR] = _POSIX_VDISABLE; 1372 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
1371 command[0] = 0; 1373 command[0] = 0;
1372 1374
@@ -1379,7 +1381,6 @@ int cmdedit_read_input(char *prompt, char command[BUFSIZ])
1379 parse_prompt(prompt); 1381 parse_prompt(prompt);
1380 1382
1381 while (1) { 1383 while (1) {
1382
1383 fflush(stdout); /* buffered out to fast */ 1384 fflush(stdout); /* buffered out to fast */
1384 1385
1385 if (safe_read(0, &c, 1) < 1) 1386 if (safe_read(0, &c, 1) < 1)
@@ -1393,8 +1394,7 @@ int cmdedit_read_input(char *prompt, char command[BUFSIZ])
1393 if (vi_cmdmode) 1394 if (vi_cmdmode)
1394 ic |= vbit; 1395 ic |= vbit;
1395#endif 1396#endif
1396 switch (ic) 1397 switch (ic) {
1397 {
1398 case '\n': 1398 case '\n':
1399 case '\r': 1399 case '\r':
1400 vi_case( case '\n'|vbit: ) 1400 vi_case( case '\n'|vbit: )
@@ -1433,8 +1433,8 @@ int cmdedit_read_input(char *prompt, char command[BUFSIZ])
1433 /* Control-d -- Delete one character, or exit 1433 /* Control-d -- Delete one character, or exit
1434 * if the len=0 and no chars to delete */ 1434 * if the len=0 and no chars to delete */
1435 if (len == 0) { 1435 if (len == 0) {
1436 errno = 0; 1436 errno = 0;
1437prepare_to_die: 1437 prepare_to_die:
1438#if !ENABLE_ASH 1438#if !ENABLE_ASH
1439 printf("exit"); 1439 printf("exit");
1440 goto_new_line(); 1440 goto_new_line();
@@ -1472,7 +1472,7 @@ prepare_to_die:
1472 break; 1472 break;
1473 case CNTRL('K'): 1473 case CNTRL('K'):
1474 /* Control-k -- clear to end of line */ 1474 /* Control-k -- clear to end of line */
1475 *(command + cursor) = 0; 1475 command[cursor] = 0;
1476 len = cursor; 1476 len = cursor;
1477 printf("\033[J"); 1477 printf("\033[J");
1478 break; 1478 break;
@@ -1480,7 +1480,7 @@ prepare_to_die:
1480 vi_case( case CNTRL('L')|vbit: ) 1480 vi_case( case CNTRL('L')|vbit: )
1481 /* Control-l -- clear screen */ 1481 /* Control-l -- clear screen */
1482 printf("\033[H"); 1482 printf("\033[H");
1483 redraw(0, len-cursor); 1483 redraw(0, len - cursor);
1484 break; 1484 break;
1485#if MAX_HISTORY > 0 1485#if MAX_HISTORY > 0
1486 case CNTRL('N'): 1486 case CNTRL('N'):
@@ -1570,65 +1570,64 @@ prepare_to_die:
1570 case 'c'|vbit: 1570 case 'c'|vbit:
1571 vi_cmdmode = 0; 1571 vi_cmdmode = 0;
1572 /* fall through */ 1572 /* fall through */
1573 case 'd'|vbit: 1573 case 'd'|vbit: {
1574 { 1574 int nc, sc;
1575 int nc, sc; 1575 sc = cursor;
1576 sc = cursor; 1576 prevc = ic;
1577 prevc = ic; 1577 if (safe_read(0, &c, 1) < 1)
1578 if (safe_read(0, &c, 1) < 1) 1578 goto prepare_to_die;
1579 goto prepare_to_die; 1579 if (c == (prevc & 0xff)) {
1580 if (c == (prevc & 0xff)) { 1580 /* "cc", "dd" */
1581 /* "cc", "dd" */ 1581 input_backward(cursor);
1582 input_backward(cursor); 1582 goto clear_to_eol;
1583 goto clear_to_eol; 1583 break;
1584 break; 1584 }
1585 } 1585 switch (c) {
1586 case 'w':
1587 case 'W':
1588 case 'e':
1589 case 'E':
1586 switch (c) { 1590 switch (c) {
1587 case 'w': 1591 case 'w': /* "dw", "cw" */
1588 case 'W': 1592 vi_word_motion(command, vi_cmdmode);
1589 case 'e':
1590 case 'E':
1591 switch (c) {
1592 case 'w': /* "dw", "cw" */
1593 vi_word_motion(command, vi_cmdmode);
1594 break;
1595 case 'W': /* 'dW', 'cW' */
1596 vi_Word_motion(command, vi_cmdmode);
1597 break;
1598 case 'e': /* 'de', 'ce' */
1599 vi_end_motion(command);
1600 input_forward();
1601 break;
1602 case 'E': /* 'dE', 'cE' */
1603 vi_End_motion(command);
1604 input_forward();
1605 break;
1606 }
1607 nc = cursor;
1608 input_backward(cursor - sc);
1609 while (nc-- > cursor)
1610 input_delete(1);
1611 break; 1593 break;
1612 case 'b': /* "db", "cb" */ 1594 case 'W': /* 'dW', 'cW' */
1613 case 'B': /* implemented as B */ 1595 vi_Word_motion(command, vi_cmdmode);
1614 if (c == 'b')
1615 vi_back_motion(command);
1616 else
1617 vi_Back_motion(command);
1618 while (sc-- > cursor)
1619 input_delete(1);
1620 break; 1596 break;
1621 case ' ': /* "d ", "c " */ 1597 case 'e': /* 'de', 'ce' */
1622 input_delete(1); 1598 vi_end_motion(command);
1599 input_forward();
1623 break; 1600 break;
1624 case '$': /* "d$", "c$" */ 1601 case 'E': /* 'dE', 'cE' */
1625 clear_to_eol: 1602 vi_End_motion(command);
1626 while (cursor < len) 1603 input_forward();
1627 input_delete(1);
1628 break; 1604 break;
1629 } 1605 }
1606 nc = cursor;
1607 input_backward(cursor - sc);
1608 while (nc-- > cursor)
1609 input_delete(1);
1610 break;
1611 case 'b': /* "db", "cb" */
1612 case 'B': /* implemented as B */
1613 if (c == 'b')
1614 vi_back_motion(command);
1615 else
1616 vi_Back_motion(command);
1617 while (sc-- > cursor)
1618 input_delete(1);
1619 break;
1620 case ' ': /* "d ", "c " */
1621 input_delete(1);
1622 break;
1623 case '$': /* "d$", "c$" */
1624 clear_to_eol:
1625 while (cursor < len)
1626 input_delete(1);
1627 break;
1630 } 1628 }
1631 break; 1629 break;
1630 }
1632 case 'p'|vbit: 1631 case 'p'|vbit:
1633 input_forward(); 1632 input_forward();
1634 /* fallthrough */ 1633 /* fallthrough */
@@ -1794,7 +1793,7 @@ rewrite_line:
1794#if MAX_HISTORY > 0 1793#if MAX_HISTORY > 0
1795 /* Handle command history log */ 1794 /* Handle command history log */
1796 /* cleanup may be saved current command line */ 1795 /* cleanup may be saved current command line */
1797 if (len> 0) { /* no put empty line */ 1796 if (len > 0) { /* no put empty line */
1798 int i = n_history; 1797 int i = n_history;
1799 1798
1800 free(history[MAX_HISTORY]); 1799 free(history[MAX_HISTORY]);
@@ -1802,7 +1801,7 @@ rewrite_line:
1802 /* After max history, remove the oldest command */ 1801 /* After max history, remove the oldest command */
1803 if (i >= MAX_HISTORY) { 1802 if (i >= MAX_HISTORY) {
1804 free(history[0]); 1803 free(history[0]);
1805 for(i = 0; i < (MAX_HISTORY-1); i++) 1804 for(i = 0; i < MAX_HISTORY-1; i++)
1806 history[i] = history[i+1]; 1805 history[i] = history[i+1];
1807 } 1806 }
1808 history[i++] = xstrdup(command); 1807 history[i++] = xstrdup(command);
@@ -1849,9 +1848,9 @@ int main(int argc, char **argv)
1849 char buff[BUFSIZ]; 1848 char buff[BUFSIZ];
1850 char *prompt = 1849 char *prompt =
1851#if ENABLE_FEATURE_SH_FANCY_PROMPT 1850#if ENABLE_FEATURE_SH_FANCY_PROMPT
1852 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:\ 1851 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
1853\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] \ 1852 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
1854\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]"; 1853 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
1855#else 1854#else
1856 "% "; 1855 "% ";
1857#endif 1856#endif
@@ -1862,12 +1861,10 @@ int main(int argc, char **argv)
1862 while (1) { 1861 while (1) {
1863 int l; 1862 int l;
1864 l = cmdedit_read_input(prompt, buff); 1863 l = cmdedit_read_input(prompt, buff);
1865 if (l > 0 && buff[l-1] == '\n') { 1864 if (l <= 0 || buff[l-1] != '\n')
1866 buff[l-1] = 0;
1867 printf("*** cmdedit_read_input() returned line =%s=\n", buff);
1868 } else {
1869 break; 1865 break;
1870 } 1866 buff[l-1] = 0;
1867 printf("*** cmdedit_read_input() returned line =%s=\n", buff);
1871 } 1868 }
1872 printf("*** cmdedit_read_input() detect ^D\n"); 1869 printf("*** cmdedit_read_input() detect ^D\n");
1873 return 0; 1870 return 0;