diff options
| author | Eric Andersen <andersen@codepoet.org> | 2001-04-09 22:48:12 +0000 |
|---|---|---|
| committer | Eric Andersen <andersen@codepoet.org> | 2001-04-09 22:48:12 +0000 |
| commit | e5dfced23a904d08afa5dcee190c3c3d845d9f50 (patch) | |
| tree | ef367ee8a9096884fb40debdc9e10af8583f9d5f /shell/cmdedit.c | |
| parent | a75e2867435faa68ea03735fe09ad298fa3e4e72 (diff) | |
| download | busybox-w32-e5dfced23a904d08afa5dcee190c3c3d845d9f50.tar.gz busybox-w32-e5dfced23a904d08afa5dcee190c3c3d845d9f50.tar.bz2 busybox-w32-e5dfced23a904d08afa5dcee190c3c3d845d9f50.zip | |
Apply Vladimir's latest cleanup patch.
-Erik
Diffstat (limited to 'shell/cmdedit.c')
| -rw-r--r-- | shell/cmdedit.c | 249 |
1 files changed, 97 insertions, 152 deletions
diff --git a/shell/cmdedit.c b/shell/cmdedit.c index a3710812f..eef1a88c8 100644 --- a/shell/cmdedit.c +++ b/shell/cmdedit.c | |||
| @@ -31,8 +31,6 @@ | |||
| 31 | */ | 31 | */ |
| 32 | 32 | ||
| 33 | 33 | ||
| 34 | //#define TEST | ||
| 35 | |||
| 36 | #include <stdio.h> | 34 | #include <stdio.h> |
| 37 | #include <errno.h> | 35 | #include <errno.h> |
| 38 | #include <unistd.h> | 36 | #include <unistd.h> |
| @@ -43,10 +41,16 @@ | |||
| 43 | #include <signal.h> | 41 | #include <signal.h> |
| 44 | #include <limits.h> | 42 | #include <limits.h> |
| 45 | 43 | ||
| 46 | #ifndef TEST | ||
| 47 | |||
| 48 | #include "busybox.h" | 44 | #include "busybox.h" |
| 49 | 45 | ||
| 46 | #ifdef BB_LOCALE_SUPPORT | ||
| 47 | #define Isprint(c) isprint((c)) | ||
| 48 | #else | ||
| 49 | #define Isprint(c) ( (c) >= ' ' && (c) != ((unsigned char)'\233') ) | ||
| 50 | #endif | ||
| 51 | |||
| 52 | #ifndef TEST | ||
| 53 | |||
| 50 | #define D(x) | 54 | #define D(x) |
| 51 | 55 | ||
| 52 | #else | 56 | #else |
| @@ -59,13 +63,6 @@ | |||
| 59 | 63 | ||
| 60 | #define D(x) x | 64 | #define D(x) x |
| 61 | 65 | ||
| 62 | #ifndef TRUE | ||
| 63 | #define TRUE 1 | ||
| 64 | #endif | ||
| 65 | #ifndef FALSE | ||
| 66 | #define FALSE 0 | ||
| 67 | #endif | ||
| 68 | |||
| 69 | #endif /* TEST */ | 66 | #endif /* TEST */ |
| 70 | 67 | ||
| 71 | #ifdef BB_FEATURE_COMMAND_TAB_COMPLETION | 68 | #ifdef BB_FEATURE_COMMAND_TAB_COMPLETION |
| @@ -92,29 +89,6 @@ | |||
| 92 | #endif /* advanced FEATURES */ | 89 | #endif /* advanced FEATURES */ |
| 93 | 90 | ||
| 94 | 91 | ||
| 95 | #ifdef TEST | ||
| 96 | void *xrealloc(void *old, size_t size) | ||
| 97 | { | ||
| 98 | return realloc(old, size); | ||
| 99 | } | ||
| 100 | |||
| 101 | void *xmalloc(size_t size) | ||
| 102 | { | ||
| 103 | return malloc(size); | ||
| 104 | } | ||
| 105 | char *xstrdup(const char *s) | ||
| 106 | { | ||
| 107 | return strdup(s); | ||
| 108 | } | ||
| 109 | |||
| 110 | void *xcalloc(size_t size, size_t se) | ||
| 111 | { | ||
| 112 | return calloc(size, se); | ||
| 113 | } | ||
| 114 | |||
| 115 | #define error_msg(s, d) fprintf(stderr, s, d) | ||
| 116 | #endif /* TEST */ | ||
| 117 | |||
| 118 | 92 | ||
| 119 | struct history { | 93 | struct history { |
| 120 | char *s; | 94 | char *s; |
| @@ -238,7 +212,7 @@ static void win_changed(int nsig) | |||
| 238 | static void cmdedit_reset_term(void) | 212 | static void cmdedit_reset_term(void) |
| 239 | { | 213 | { |
| 240 | if ((handlers_sets & SET_RESET_TERM) != 0) { | 214 | if ((handlers_sets & SET_RESET_TERM) != 0) { |
| 241 | /* sparc and other have broken termios support: use old termio handling. */ | 215 | /* sparc and other have broken termios support: use old termio handling. */ |
| 242 | setTermSettings(fileno(stdin), (void *) &initial_settings); | 216 | setTermSettings(fileno(stdin), (void *) &initial_settings); |
| 243 | handlers_sets &= ~SET_RESET_TERM; | 217 | handlers_sets &= ~SET_RESET_TERM; |
| 244 | } | 218 | } |
| @@ -270,9 +244,9 @@ static void cmdedit_set_out_char(int next_char) | |||
| 270 | int c = (int)((unsigned char) command_ps[cursor]); | 244 | int c = (int)((unsigned char) command_ps[cursor]); |
| 271 | 245 | ||
| 272 | if (c == 0) | 246 | if (c == 0) |
| 273 | c = ' '; /* destroy end char? */ | 247 | c = ' '; /* destroy end char? */ |
| 274 | #ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT | 248 | #ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT |
| 275 | if (!isprint(c)) { /* Inverse put non-printable characters */ | 249 | if (!Isprint(c)) { /* Inverse put non-printable characters */ |
| 276 | if (c >= 128) | 250 | if (c >= 128) |
| 277 | c -= 128; | 251 | c -= 128; |
| 278 | if (c < ' ') | 252 | if (c < ' ') |
| @@ -328,7 +302,7 @@ static void input_backward(int num) | |||
| 328 | { | 302 | { |
| 329 | if (num > cursor) | 303 | if (num > cursor) |
| 330 | num = cursor; | 304 | num = cursor; |
| 331 | cursor -= num; /* new cursor (in command, not terminal) */ | 305 | cursor -= num; /* new cursor (in command, not terminal) */ |
| 332 | 306 | ||
| 333 | if (cmdedit_x >= num) { /* no to up line */ | 307 | if (cmdedit_x >= num) { /* no to up line */ |
| 334 | cmdedit_x -= num; | 308 | cmdedit_x -= num; |
| @@ -369,147 +343,116 @@ static void parse_prompt(const char *prmt_ptr) | |||
| 369 | put_prompt(); | 343 | put_prompt(); |
| 370 | } | 344 | } |
| 371 | #else | 345 | #else |
| 372 | static void add_to_prompt(char **prmt_mem_ptr, int *alm, | ||
| 373 | int *prmt_len, const char *addb) | ||
| 374 | { | ||
| 375 | *prmt_len += strlen(addb); | ||
| 376 | if (*alm < (*prmt_len) + 1) { | ||
| 377 | *alm = (*prmt_len) + 1; | ||
| 378 | *prmt_mem_ptr = xrealloc(*prmt_mem_ptr, *alm); | ||
| 379 | } | ||
| 380 | strcat(*prmt_mem_ptr, addb); | ||
| 381 | } | ||
| 382 | |||
| 383 | static void parse_prompt(const char *prmt_ptr) | 346 | static void parse_prompt(const char *prmt_ptr) |
| 384 | { | 347 | { |
| 385 | int alm = strlen(prmt_ptr) + 1; /* supposedly require memory */ | ||
| 386 | int prmt_len = 0; | 348 | int prmt_len = 0; |
| 387 | int sub_len = 0; | 349 | int sub_len = 0; |
| 388 | int flg_not_length = '['; | 350 | char flg_not_length = '['; |
| 389 | char *prmt_mem_ptr = xstrdup(prmt_ptr); | 351 | char *prmt_mem_ptr = xcalloc(1, 1); |
| 390 | char pwd_buf[PATH_MAX + 1]; | 352 | char *pwd_buf = xgetcwd(0); |
| 391 | char buf[16]; | 353 | char buf2[PATH_MAX + 1]; |
| 392 | int c; | 354 | char buf[2]; |
| 393 | 355 | char c; | |
| 394 | pwd_buf[0] = 0; | 356 | char *pbuf; |
| 395 | *prmt_mem_ptr = 0; | ||
| 396 | 357 | ||
| 397 | while (*prmt_ptr) { | 358 | while (*prmt_ptr) { |
| 359 | pbuf = buf; | ||
| 360 | pbuf[1] = 0; | ||
| 398 | c = *prmt_ptr++; | 361 | c = *prmt_ptr++; |
| 399 | if (c == '\\') { | 362 | if (c == '\\') { |
| 400 | c = *prmt_ptr; | 363 | const char *cp = prmt_ptr; |
| 401 | if (c == 0) | 364 | int l; |
| 365 | |||
| 366 | c = process_escape_sequence(&prmt_ptr); | ||
| 367 | if(prmt_ptr==cp) { | ||
| 368 | if (*cp == 0) | ||
| 402 | break; | 369 | break; |
| 403 | prmt_ptr++; | 370 | c = *prmt_ptr++; |
| 404 | switch (c) { | 371 | switch (c) { |
| 405 | #ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR | 372 | #ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR |
| 406 | case 'u': | 373 | case 'u': |
| 407 | add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, user_buf); | 374 | pbuf = user_buf; |
| 408 | continue; | 375 | break; |
| 409 | #endif | 376 | #endif |
| 410 | case 'h': | 377 | case 'h': |
| 411 | if (hostname_buf[0] == 0) { | 378 | pbuf = hostname_buf; |
| 412 | hostname_buf = xcalloc(256, 1); | 379 | if (*pbuf == 0) { |
| 413 | if (gethostname(hostname_buf, 255) < 0) { | 380 | pbuf = xcalloc(256, 1); |
| 414 | strcpy(hostname_buf, "?"); | 381 | if (gethostname(pbuf, 255) < 0) { |
| 382 | strcpy(pbuf, "?"); | ||
| 415 | } else { | 383 | } else { |
| 416 | char *s = strchr(hostname_buf, '.'); | 384 | char *s = strchr(pbuf, '.'); |
| 417 | 385 | ||
| 418 | if (s) | 386 | if (s) |
| 419 | *s = 0; | 387 | *s = 0; |
| 420 | } | 388 | } |
| 389 | hostname_buf = pbuf; | ||
| 421 | } | 390 | } |
| 422 | add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, | 391 | break; |
| 423 | hostname_buf); | 392 | case '$': |
| 424 | continue; | ||
| 425 | case '$': | ||
| 426 | c = my_euid == 0 ? '#' : '$'; | 393 | c = my_euid == 0 ? '#' : '$'; |
| 427 | break; | 394 | break; |
| 428 | #ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR | 395 | #ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR |
| 429 | case 'w': | 396 | case 'w': |
| 430 | if (pwd_buf[0] == 0) { | 397 | pbuf = pwd_buf; |
| 431 | int l; | 398 | l = strlen(home_pwd_buf); |
| 432 | 399 | if (home_pwd_buf[0] != 0 && | |
| 433 | getcwd(pwd_buf, PATH_MAX); | 400 | strncmp(home_pwd_buf, pbuf, l) == 0 && |
| 434 | l = strlen(home_pwd_buf); | 401 | (pbuf[l]=='/' || pbuf[l]=='\0') && |
| 435 | if (home_pwd_buf[0] != 0 && | 402 | strlen(pwd_buf+l)<PATH_MAX) { |
| 436 | strncmp(home_pwd_buf, pwd_buf, l) == 0) { | 403 | pbuf = buf2; |
| 437 | strcpy(pwd_buf + 1, pwd_buf + l); | 404 | *pbuf = '~'; |
| 438 | pwd_buf[0] = '~'; | 405 | strcpy(pbuf+1, pwd_buf+l); |
| 439 | } | 406 | } |
| 440 | } | 407 | break; |
| 441 | add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, pwd_buf); | ||
| 442 | continue; | ||
| 443 | #endif | 408 | #endif |
| 444 | case 'W': | 409 | case 'W': |
| 445 | if (pwd_buf[0] == 0) { | 410 | pbuf = pwd_buf; |
| 446 | char *z; | 411 | cp = strrchr(pbuf,'/'); |
| 447 | 412 | if ( (cp != NULL) && (cp != pbuf) ) | |
| 448 | getcwd(pwd_buf, PATH_MAX); | 413 | pbuf += (cp-pbuf)+1; |
| 449 | z = strrchr(pwd_buf,'/'); | 414 | break; |
| 450 | if ( (z != NULL) && (z != pwd_buf) ) { | 415 | case '!': |
| 451 | z++; | 416 | snprintf(pbuf = buf2, sizeof(buf2), "%d", num_ok_lines); |
| 452 | strcpy(pwd_buf,z); | 417 | break; |
| 453 | } | 418 | case 'e': case 'E': /* \e \E = \033 */ |
| 454 | } | ||
| 455 | add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, pwd_buf); | ||
| 456 | continue; | ||
| 457 | case '!': | ||
| 458 | snprintf(buf, sizeof(buf), "%d", num_ok_lines); | ||
| 459 | add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, buf); | ||
| 460 | continue; | ||
| 461 | case 'e': | ||
| 462 | case 'E': /* \e \E = \033 */ | ||
| 463 | c = '\033'; | 419 | c = '\033'; |
| 464 | break; | 420 | break; |
| 465 | case 'x': | 421 | case 'x': case 'X': |
| 466 | case 'X': | ||
| 467 | case '0': | ||
| 468 | case '1': | ||
| 469 | case '2': | ||
| 470 | case '3': | ||
| 471 | case '4': | ||
| 472 | case '5': | ||
| 473 | case '6': | ||
| 474 | case '7':{ | ||
| 475 | int l; | ||
| 476 | int ho = 0; | ||
| 477 | char *eho; | ||
| 478 | |||
| 479 | if (c == 'X') | ||
| 480 | c = 'x'; | ||
| 481 | |||
| 482 | for (l = 0; l < 3;) { | 422 | for (l = 0; l < 3;) { |
| 483 | 423 | int h; | |
| 484 | buf[l++] = *prmt_ptr; | 424 | buf2[l++] = *prmt_ptr; |
| 485 | buf[l] = 0; | 425 | buf2[l] = 0; |
| 486 | ho = strtol(buf, &eho, c == 'x' ? 16 : 8); | 426 | h = strtol(buf2, &pbuf, 16); |
| 487 | if (ho > UCHAR_MAX || (eho - buf) < l) { | 427 | if (h > UCHAR_MAX || (pbuf - buf2) < l) { |
| 488 | l--; | 428 | l--; |
| 489 | break; | 429 | break; |
| 490 | } | 430 | } |
| 491 | prmt_ptr++; | 431 | prmt_ptr++; |
| 492 | } | 432 | } |
| 493 | buf[l] = 0; | 433 | buf2[l] = 0; |
| 494 | ho = strtol(buf, 0, c == 'x' ? 16 : 8); | 434 | c = (char)strtol(buf2, 0, 16); |
| 495 | c = ho == 0 ? '?' : (char) ho; | 435 | if(c==0) |
| 436 | c = '?'; | ||
| 437 | pbuf = buf; | ||
| 496 | break; | 438 | break; |
| 497 | } | 439 | case '[': case ']': |
| 498 | case '[': | ||
| 499 | case ']': | ||
| 500 | if (c == flg_not_length) { | 440 | if (c == flg_not_length) { |
| 501 | flg_not_length = flg_not_length == '[' ? ']' : '['; | 441 | flg_not_length = flg_not_length == '[' ? ']' : '['; |
| 502 | continue; | 442 | continue; |
| 503 | } | 443 | } |
| 504 | break; | 444 | break; |
| 505 | } | 445 | } |
| 446 | } | ||
| 506 | } | 447 | } |
| 507 | buf[0] = c; | 448 | if(pbuf == buf) |
| 508 | buf[1] = 0; | 449 | *pbuf = c; |
| 509 | add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, buf); | 450 | prmt_len += strlen(pbuf); |
| 451 | prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf); | ||
| 510 | if (flg_not_length == ']') | 452 | if (flg_not_length == ']') |
| 511 | sub_len++; | 453 | sub_len++; |
| 512 | } | 454 | } |
| 455 | free(pwd_buf); | ||
| 513 | cmdedit_prompt = prmt_mem_ptr; | 456 | cmdedit_prompt = prmt_mem_ptr; |
| 514 | cmdedit_prmt_len = prmt_len - sub_len; | 457 | cmdedit_prmt_len = prmt_len - sub_len; |
| 515 | put_prompt(); | 458 | put_prompt(); |
| @@ -789,7 +732,7 @@ static char **exe_n_cwd_tab_completion(char *command, int *num_matches, | |||
| 789 | char **paths = path1; | 732 | char **paths = path1; |
| 790 | int npaths; | 733 | int npaths; |
| 791 | int i; | 734 | int i; |
| 792 | char found[BUFSIZ + 4 + PATH_MAX]; | 735 | char *found; |
| 793 | char *pfind = strrchr(command, '/'); | 736 | char *pfind = strrchr(command, '/'); |
| 794 | 737 | ||
| 795 | path1[0] = "."; | 738 | path1[0] = "."; |
| @@ -822,7 +765,6 @@ static char **exe_n_cwd_tab_completion(char *command, int *num_matches, | |||
| 822 | continue; | 765 | continue; |
| 823 | 766 | ||
| 824 | while ((next = readdir(dir)) != NULL) { | 767 | while ((next = readdir(dir)) != NULL) { |
| 825 | const char *str_merge = "%s/%s"; | ||
| 826 | char *str_found = next->d_name; | 768 | char *str_found = next->d_name; |
| 827 | 769 | ||
| 828 | /* matched ? */ | 770 | /* matched ? */ |
| @@ -835,25 +777,23 @@ static char **exe_n_cwd_tab_completion(char *command, int *num_matches, | |||
| 835 | else | 777 | else |
| 836 | continue; | 778 | continue; |
| 837 | } | 779 | } |
| 838 | if (paths[i][strlen(paths[i]) - 1] == '/') | 780 | found = concat_path_file(paths[i], str_found); |
| 839 | str_merge = "%s%s"; | ||
| 840 | sprintf(found, str_merge, paths[i], str_found); | ||
| 841 | /* hmm, remover in progress? */ | 781 | /* hmm, remover in progress? */ |
| 842 | if (stat(found, &st) < 0) | 782 | if (stat(found, &st) < 0) |
| 843 | continue; | 783 | goto cont; |
| 844 | /* find with dirs ? */ | 784 | /* find with dirs ? */ |
| 845 | if (paths[i] != dirbuf) | 785 | if (paths[i] != dirbuf) |
| 846 | strcpy(found, next->d_name); /* only name */ | 786 | strcpy(found, next->d_name); /* only name */ |
| 847 | if (S_ISDIR(st.st_mode)) { | 787 | if (S_ISDIR(st.st_mode)) { |
| 848 | /* name is directory */ | 788 | /* name is directory */ |
| 849 | /* algorithmic only "/" ? */ | 789 | str_found = found; |
| 850 | if (*str_found) | 790 | found = concat_path_file(found, ""); |
| 851 | strcat(found, "/"); | 791 | free(str_found); |
| 852 | str_found = add_quote_for_spec_chars(found); | 792 | str_found = add_quote_for_spec_chars(found); |
| 853 | } else { | 793 | } else { |
| 854 | /* not put found file if search only dirs for cd */ | 794 | /* not put found file if search only dirs for cd */ |
| 855 | if (type == FIND_DIR_ONLY) | 795 | if (type == FIND_DIR_ONLY) |
| 856 | continue; | 796 | goto cont; |
| 857 | str_found = add_quote_for_spec_chars(found); | 797 | str_found = add_quote_for_spec_chars(found); |
| 858 | if (type == FIND_FILE_ONLY || | 798 | if (type == FIND_FILE_ONLY || |
| 859 | (type == FIND_EXE_ONLY && is_execute(&st) == TRUE)) | 799 | (type == FIND_EXE_ONLY && is_execute(&st) == TRUE)) |
| @@ -863,6 +803,8 @@ static char **exe_n_cwd_tab_completion(char *command, int *num_matches, | |||
| 863 | matches = xrealloc(matches, (nm + 1) * sizeof(char *)); | 803 | matches = xrealloc(matches, (nm + 1) * sizeof(char *)); |
| 864 | 804 | ||
| 865 | matches[nm++] = str_found; | 805 | matches[nm++] = str_found; |
| 806 | cont: | ||
| 807 | free(found); | ||
| 866 | } | 808 | } |
| 867 | closedir(dir); | 809 | closedir(dir); |
| 868 | } | 810 | } |
| @@ -1440,7 +1382,7 @@ extern void cmdedit_read_input(char *prompt, char command[BUFSIZ]) | |||
| 1440 | } | 1382 | } |
| 1441 | } else | 1383 | } else |
| 1442 | #endif | 1384 | #endif |
| 1443 | if (!isprint(c)) /* Skip non-printable characters */ | 1385 | if (!Isprint(c)) /* Skip non-printable characters */ |
| 1444 | break; | 1386 | break; |
| 1445 | 1387 | ||
| 1446 | if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */ | 1388 | if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */ |
| @@ -1554,6 +1496,9 @@ extern void cmdedit_terminate(void) | |||
| 1554 | 1496 | ||
| 1555 | #ifdef TEST | 1497 | #ifdef TEST |
| 1556 | 1498 | ||
| 1499 | const char *applet_name = "debug stuff usage"; | ||
| 1500 | const char *memory_exhausted = "Memory exhausted"; | ||
| 1501 | |||
| 1557 | #ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT | 1502 | #ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT |
| 1558 | #include <locale.h> | 1503 | #include <locale.h> |
| 1559 | #endif | 1504 | #endif |
