diff options
-rw-r--r-- | lash.c | 104 | ||||
-rw-r--r-- | sh.c | 104 | ||||
-rw-r--r-- | shell/lash.c | 104 |
3 files changed, 195 insertions, 117 deletions
@@ -45,9 +45,7 @@ | |||
45 | #include <sys/wait.h> | 45 | #include <sys/wait.h> |
46 | #include <unistd.h> | 46 | #include <unistd.h> |
47 | #include <getopt.h> | 47 | #include <getopt.h> |
48 | #ifdef BB_FEATURE_SH_COMMAND_EDITING | ||
49 | #include "cmdedit.h" | 48 | #include "cmdedit.h" |
50 | #endif | ||
51 | 49 | ||
52 | #define MAX_LINE 256 /* size of input buffer for `read' builtin */ | 50 | #define MAX_LINE 256 /* size of input buffer for `read' builtin */ |
53 | #define MAX_READ 128 /* size of input buffer for `read' builtin */ | 51 | #define MAX_READ 128 /* size of input buffer for `read' builtin */ |
@@ -55,6 +53,8 @@ | |||
55 | extern size_t NUM_APPLETS; | 53 | extern size_t NUM_APPLETS; |
56 | 54 | ||
57 | 55 | ||
56 | |||
57 | |||
58 | enum redirectionType { REDIRECT_INPUT, REDIRECT_OVERWRITE, | 58 | enum redirectionType { REDIRECT_INPUT, REDIRECT_OVERWRITE, |
59 | REDIRECT_APPEND | 59 | REDIRECT_APPEND |
60 | }; | 60 | }; |
@@ -167,7 +167,7 @@ static struct builtInCommand bltins_forking[] = { | |||
167 | {NULL, NULL, NULL} | 167 | {NULL, NULL, NULL} |
168 | }; | 168 | }; |
169 | 169 | ||
170 | static char *prompt = "# "; | 170 | static char prompt[3]; |
171 | static char *cwd; | 171 | static char *cwd; |
172 | static char *local_pending_command = NULL; | 172 | static char *local_pending_command = NULL; |
173 | static char *promptStr = NULL; | 173 | static char *promptStr = NULL; |
@@ -180,9 +180,20 @@ static int lastReturnCode=-1; | |||
180 | static int showXtrace=FALSE; | 180 | static int showXtrace=FALSE; |
181 | #endif | 181 | #endif |
182 | 182 | ||
183 | #ifdef DEBUG_SHELL | ||
184 | static inline void debug_printf(const char *format, ...) | ||
185 | { | ||
186 | va_list args; | ||
187 | va_start(args, format); | ||
188 | vfprintf(stderr, s, p); | ||
189 | va_end(args); | ||
190 | } | ||
191 | #else | ||
192 | static inline void debug_printf(const char *format, ...) { } | ||
193 | #endif | ||
183 | 194 | ||
184 | #ifdef BB_FEATURE_SH_COMMAND_EDITING | 195 | #ifdef BB_FEATURE_SH_COMMAND_EDITING |
185 | void win_changed(int junk) | 196 | static inline void win_changed(int junk) |
186 | { | 197 | { |
187 | struct winsize win = { 0, 0, 0, 0 }; | 198 | struct winsize win = { 0, 0, 0, 0 }; |
188 | ioctl(0, TIOCGWINSZ, &win); | 199 | ioctl(0, TIOCGWINSZ, &win); |
@@ -190,6 +201,8 @@ void win_changed(int junk) | |||
190 | cmdedit_setwidth( win.ws_col - 1); | 201 | cmdedit_setwidth( win.ws_col - 1); |
191 | } | 202 | } |
192 | } | 203 | } |
204 | #else | ||
205 | static inline void win_changed(int junk) {} | ||
193 | #endif | 206 | #endif |
194 | 207 | ||
195 | 208 | ||
@@ -402,22 +415,14 @@ static int builtin_if(struct job *cmd, struct jobSet *jobList) | |||
402 | local_pending_command = xmalloc(status+1); | 415 | local_pending_command = xmalloc(status+1); |
403 | strncpy(local_pending_command, charptr1, status); | 416 | strncpy(local_pending_command, charptr1, status); |
404 | local_pending_command[status]='\0'; | 417 | local_pending_command[status]='\0'; |
405 | #ifdef DEBUG_SHELL | 418 | debug_printf(stderr, "'if' now testing '%s'\n", local_pending_command); |
406 | fprintf(stderr, "'if' now testing '%s'\n", local_pending_command); | ||
407 | #endif | ||
408 | status = busy_loop(NULL); /* Frees local_pending_command */ | 419 | status = busy_loop(NULL); /* Frees local_pending_command */ |
409 | #ifdef DEBUG_SHELL | 420 | debug_printf(stderr, "if test returned "); |
410 | fprintf(stderr, "if test returned "); | ||
411 | #endif | ||
412 | if (status == 0) { | 421 | if (status == 0) { |
413 | #ifdef DEBUG_SHELL | 422 | debug_printf(stderr, "TRUE\n"); |
414 | fprintf(stderr, "TRUE\n"); | ||
415 | #endif | ||
416 | cmd->jobContext |= IF_TRUE_CONTEXT; | 423 | cmd->jobContext |= IF_TRUE_CONTEXT; |
417 | } else { | 424 | } else { |
418 | #ifdef DEBUG_SHELL | 425 | debug_printf(stderr, "FALSE\n"); |
419 | fprintf(stderr, "FALSE\n"); | ||
420 | #endif | ||
421 | cmd->jobContext |= IF_FALSE_CONTEXT; | 426 | cmd->jobContext |= IF_FALSE_CONTEXT; |
422 | } | 427 | } |
423 | 428 | ||
@@ -447,9 +452,7 @@ static int builtin_then(struct job *cmd, struct jobSet *junk) | |||
447 | local_pending_command = xmalloc(status+1); | 452 | local_pending_command = xmalloc(status+1); |
448 | strncpy(local_pending_command, charptr1, status); | 453 | strncpy(local_pending_command, charptr1, status); |
449 | local_pending_command[status]='\0'; | 454 | local_pending_command[status]='\0'; |
450 | #ifdef DEBUG_SHELL | 455 | debug_printf(stderr, "'then' now running '%s'\n", charptr1); |
451 | fprintf(stderr, "'then' now running '%s'\n", charptr1); | ||
452 | #endif | ||
453 | return( busy_loop(NULL)); | 456 | return( busy_loop(NULL)); |
454 | } | 457 | } |
455 | 458 | ||
@@ -476,9 +479,7 @@ static int builtin_else(struct job *cmd, struct jobSet *junk) | |||
476 | local_pending_command = xmalloc(status+1); | 479 | local_pending_command = xmalloc(status+1); |
477 | strncpy(local_pending_command, charptr1, status); | 480 | strncpy(local_pending_command, charptr1, status); |
478 | local_pending_command[status]='\0'; | 481 | local_pending_command[status]='\0'; |
479 | #ifdef DEBUG_SHELL | 482 | debug_printf(stderr, "'else' now running '%s'\n", charptr1); |
480 | fprintf(stderr, "'else' now running '%s'\n", charptr1); | ||
481 | #endif | ||
482 | return( busy_loop(NULL)); | 483 | return( busy_loop(NULL)); |
483 | } | 484 | } |
484 | 485 | ||
@@ -491,9 +492,7 @@ static int builtin_fi(struct job *cmd, struct jobSet *junk) | |||
491 | } | 492 | } |
492 | /* Clear out the if and then context bits */ | 493 | /* Clear out the if and then context bits */ |
493 | cmd->jobContext &= ~(IF_TRUE_CONTEXT|IF_FALSE_CONTEXT|THEN_EXP_CONTEXT|ELSE_EXP_CONTEXT); | 494 | cmd->jobContext &= ~(IF_TRUE_CONTEXT|IF_FALSE_CONTEXT|THEN_EXP_CONTEXT|ELSE_EXP_CONTEXT); |
494 | #ifdef DEBUG_SHELL | 495 | debug_printf(stderr, "Hit an fi -- jobContext=%d\n", cmd->jobContext); |
495 | fprintf(stderr, "Hit an fi -- jobContext=%d\n", cmd->jobContext); | ||
496 | #endif | ||
497 | return TRUE; | 496 | return TRUE; |
498 | } | 497 | } |
499 | #endif | 498 | #endif |
@@ -656,6 +655,8 @@ static int setupRedirections(struct childProgram *prog) | |||
656 | 655 | ||
657 | static int getCommand(FILE * source, char *command) | 656 | static int getCommand(FILE * source, char *command) |
658 | { | 657 | { |
658 | char *user,buf[255],*s; | ||
659 | |||
659 | if (source == NULL) { | 660 | if (source == NULL) { |
660 | if (local_pending_command) { | 661 | if (local_pending_command) { |
661 | /* a command specified (-c option): return it & mark it done */ | 662 | /* a command specified (-c option): return it & mark it done */ |
@@ -667,6 +668,17 @@ static int getCommand(FILE * source, char *command) | |||
667 | return 1; | 668 | return 1; |
668 | } | 669 | } |
669 | 670 | ||
671 | /* get User Name and setup prompt */ | ||
672 | strcpy(prompt,( geteuid() != 0 ) ? "$ ":"# "); | ||
673 | user=xcalloc(sizeof(int), 9); | ||
674 | my_getpwuid(user, geteuid()); | ||
675 | |||
676 | /* get HostName */ | ||
677 | gethostname(buf, 255); | ||
678 | s = strchr(buf, '.'); | ||
679 | if (s) | ||
680 | *s = 0; | ||
681 | |||
670 | if (source == stdin) { | 682 | if (source == stdin) { |
671 | #ifdef BB_FEATURE_SH_COMMAND_EDITING | 683 | #ifdef BB_FEATURE_SH_COMMAND_EDITING |
672 | int len; | 684 | int len; |
@@ -679,21 +691,33 @@ static int getCommand(FILE * source, char *command) | |||
679 | */ | 691 | */ |
680 | cmdedit_init(); | 692 | cmdedit_init(); |
681 | signal(SIGWINCH, win_changed); | 693 | signal(SIGWINCH, win_changed); |
682 | len=fprintf(stdout, "%s %s", cwd, prompt); | 694 | len=fprintf(stdout, "[%s@%s %s]%s", user, buf, |
695 | get_last_path_component(cwd), prompt); | ||
683 | fflush(stdout); | 696 | fflush(stdout); |
684 | promptStr=(char*)xmalloc(sizeof(char)*(len+1)); | 697 | promptStr=(char*)xmalloc(sizeof(char)*(len+1)); |
685 | sprintf(promptStr, "%s %s", cwd, prompt); | 698 | sprintf(promptStr, "[%s@%s %s]%s", user, buf, |
699 | get_last_path_component(cwd), prompt); | ||
686 | cmdedit_read_input(promptStr, command); | 700 | cmdedit_read_input(promptStr, command); |
687 | free( promptStr); | 701 | free( promptStr); |
688 | cmdedit_terminate(); | 702 | cmdedit_terminate(); |
689 | signal(SIGWINCH, SIG_DFL); | 703 | signal(SIGWINCH, SIG_DFL); |
690 | return 0; | 704 | return 0; |
691 | #else | 705 | #else |
692 | fprintf(stdout, "%s %s", cwd, prompt); | 706 | i=strlen(cwd); |
707 | i--; | ||
708 | if (i>1){ | ||
709 | while ((i>0) && (*(cwd+i)!='/') ) i--; | ||
710 | if (*(cwd+i)=='/') i++; | ||
711 | } | ||
712 | |||
713 | fprintf(stdout, "[%s@%s %s]%s",user, buf, (cwd+i), prompt); | ||
693 | fflush(stdout); | 714 | fflush(stdout); |
694 | #endif | 715 | #endif |
695 | } | 716 | } |
696 | 717 | ||
718 | /* don't leak memory */ | ||
719 | free(user); | ||
720 | |||
697 | if (!fgets(command, BUFSIZ - 2, source)) { | 721 | if (!fgets(command, BUFSIZ - 2, source)) { |
698 | if (source == stdin) | 722 | if (source == stdin) |
699 | printf("\n"); | 723 | printf("\n"); |
@@ -707,10 +731,9 @@ static int getCommand(FILE * source, char *command) | |||
707 | } | 731 | } |
708 | 732 | ||
709 | #ifdef BB_FEATURE_SH_ENVIRONMENT | 733 | #ifdef BB_FEATURE_SH_ENVIRONMENT |
710 | #define __MAX_INT_CHARS 7 | ||
711 | static char* itoa(register int i) | 734 | static char* itoa(register int i) |
712 | { | 735 | { |
713 | static char a[__MAX_INT_CHARS]; | 736 | static char a[7]; /* Max 7 ints */ |
714 | register char *b = a + sizeof(a) - 1; | 737 | register char *b = a + sizeof(a) - 1; |
715 | int sign = (i < 0); | 738 | int sign = (i < 0); |
716 | 739 | ||
@@ -1381,9 +1404,7 @@ static int busy_loop(FILE * input) | |||
1381 | #ifdef BB_FEATURE_SH_ENVIRONMENT | 1404 | #ifdef BB_FEATURE_SH_ENVIRONMENT |
1382 | lastReturnCode=WEXITSTATUS(status); | 1405 | lastReturnCode=WEXITSTATUS(status); |
1383 | #endif | 1406 | #endif |
1384 | #if 0 | 1407 | debug_printf("'%s' exited -- return code %d\n", jobList.fg->text, lastReturnCode); |
1385 | printf("'%s' exited -- return code %d\n", jobList.fg->text, lastReturnCode); | ||
1386 | #endif | ||
1387 | if (!jobList.fg->runningProgs) { | 1408 | if (!jobList.fg->runningProgs) { |
1388 | /* child exited */ | 1409 | /* child exited */ |
1389 | 1410 | ||
@@ -1449,9 +1470,17 @@ int shell_main(int argc_l, char **argv_l) | |||
1449 | argv = argv_l; | 1470 | argv = argv_l; |
1450 | 1471 | ||
1451 | 1472 | ||
1452 | //if (argv[0] && argv[0][0] == '-') { | 1473 | if (argv[0] && argv[0][0] == '-') { |
1453 | // builtin_source("/etc/profile"); | 1474 | FILE *input; |
1454 | //} | 1475 | input = fopen("/etc/profile", "r"); |
1476 | if (!input) { | ||
1477 | fprintf(stdout, "Couldn't open file '/etc/profile'\n"); | ||
1478 | } else { | ||
1479 | /* Now run the file */ | ||
1480 | busy_loop(input); | ||
1481 | fclose(input); | ||
1482 | } | ||
1483 | } | ||
1455 | 1484 | ||
1456 | while ((opt = getopt(argc_l, argv_l, "cx")) > 0) { | 1485 | while ((opt = getopt(argc_l, argv_l, "cx")) > 0) { |
1457 | switch (opt) { | 1486 | switch (opt) { |
@@ -1500,9 +1529,6 @@ int shell_main(int argc_l, char **argv_l) | |||
1500 | atexit(free_memory); | 1529 | atexit(free_memory); |
1501 | #endif | 1530 | #endif |
1502 | 1531 | ||
1503 | #ifdef BB_FEATURE_SH_COMMAND_EDITING | ||
1504 | win_changed(0); | 1532 | win_changed(0); |
1505 | #endif | ||
1506 | |||
1507 | return (busy_loop(input)); | 1533 | return (busy_loop(input)); |
1508 | } | 1534 | } |
@@ -45,9 +45,7 @@ | |||
45 | #include <sys/wait.h> | 45 | #include <sys/wait.h> |
46 | #include <unistd.h> | 46 | #include <unistd.h> |
47 | #include <getopt.h> | 47 | #include <getopt.h> |
48 | #ifdef BB_FEATURE_SH_COMMAND_EDITING | ||
49 | #include "cmdedit.h" | 48 | #include "cmdedit.h" |
50 | #endif | ||
51 | 49 | ||
52 | #define MAX_LINE 256 /* size of input buffer for `read' builtin */ | 50 | #define MAX_LINE 256 /* size of input buffer for `read' builtin */ |
53 | #define MAX_READ 128 /* size of input buffer for `read' builtin */ | 51 | #define MAX_READ 128 /* size of input buffer for `read' builtin */ |
@@ -55,6 +53,8 @@ | |||
55 | extern size_t NUM_APPLETS; | 53 | extern size_t NUM_APPLETS; |
56 | 54 | ||
57 | 55 | ||
56 | |||
57 | |||
58 | enum redirectionType { REDIRECT_INPUT, REDIRECT_OVERWRITE, | 58 | enum redirectionType { REDIRECT_INPUT, REDIRECT_OVERWRITE, |
59 | REDIRECT_APPEND | 59 | REDIRECT_APPEND |
60 | }; | 60 | }; |
@@ -167,7 +167,7 @@ static struct builtInCommand bltins_forking[] = { | |||
167 | {NULL, NULL, NULL} | 167 | {NULL, NULL, NULL} |
168 | }; | 168 | }; |
169 | 169 | ||
170 | static char *prompt = "# "; | 170 | static char prompt[3]; |
171 | static char *cwd; | 171 | static char *cwd; |
172 | static char *local_pending_command = NULL; | 172 | static char *local_pending_command = NULL; |
173 | static char *promptStr = NULL; | 173 | static char *promptStr = NULL; |
@@ -180,9 +180,20 @@ static int lastReturnCode=-1; | |||
180 | static int showXtrace=FALSE; | 180 | static int showXtrace=FALSE; |
181 | #endif | 181 | #endif |
182 | 182 | ||
183 | #ifdef DEBUG_SHELL | ||
184 | static inline void debug_printf(const char *format, ...) | ||
185 | { | ||
186 | va_list args; | ||
187 | va_start(args, format); | ||
188 | vfprintf(stderr, s, p); | ||
189 | va_end(args); | ||
190 | } | ||
191 | #else | ||
192 | static inline void debug_printf(const char *format, ...) { } | ||
193 | #endif | ||
183 | 194 | ||
184 | #ifdef BB_FEATURE_SH_COMMAND_EDITING | 195 | #ifdef BB_FEATURE_SH_COMMAND_EDITING |
185 | void win_changed(int junk) | 196 | static inline void win_changed(int junk) |
186 | { | 197 | { |
187 | struct winsize win = { 0, 0, 0, 0 }; | 198 | struct winsize win = { 0, 0, 0, 0 }; |
188 | ioctl(0, TIOCGWINSZ, &win); | 199 | ioctl(0, TIOCGWINSZ, &win); |
@@ -190,6 +201,8 @@ void win_changed(int junk) | |||
190 | cmdedit_setwidth( win.ws_col - 1); | 201 | cmdedit_setwidth( win.ws_col - 1); |
191 | } | 202 | } |
192 | } | 203 | } |
204 | #else | ||
205 | static inline void win_changed(int junk) {} | ||
193 | #endif | 206 | #endif |
194 | 207 | ||
195 | 208 | ||
@@ -402,22 +415,14 @@ static int builtin_if(struct job *cmd, struct jobSet *jobList) | |||
402 | local_pending_command = xmalloc(status+1); | 415 | local_pending_command = xmalloc(status+1); |
403 | strncpy(local_pending_command, charptr1, status); | 416 | strncpy(local_pending_command, charptr1, status); |
404 | local_pending_command[status]='\0'; | 417 | local_pending_command[status]='\0'; |
405 | #ifdef DEBUG_SHELL | 418 | debug_printf(stderr, "'if' now testing '%s'\n", local_pending_command); |
406 | fprintf(stderr, "'if' now testing '%s'\n", local_pending_command); | ||
407 | #endif | ||
408 | status = busy_loop(NULL); /* Frees local_pending_command */ | 419 | status = busy_loop(NULL); /* Frees local_pending_command */ |
409 | #ifdef DEBUG_SHELL | 420 | debug_printf(stderr, "if test returned "); |
410 | fprintf(stderr, "if test returned "); | ||
411 | #endif | ||
412 | if (status == 0) { | 421 | if (status == 0) { |
413 | #ifdef DEBUG_SHELL | 422 | debug_printf(stderr, "TRUE\n"); |
414 | fprintf(stderr, "TRUE\n"); | ||
415 | #endif | ||
416 | cmd->jobContext |= IF_TRUE_CONTEXT; | 423 | cmd->jobContext |= IF_TRUE_CONTEXT; |
417 | } else { | 424 | } else { |
418 | #ifdef DEBUG_SHELL | 425 | debug_printf(stderr, "FALSE\n"); |
419 | fprintf(stderr, "FALSE\n"); | ||
420 | #endif | ||
421 | cmd->jobContext |= IF_FALSE_CONTEXT; | 426 | cmd->jobContext |= IF_FALSE_CONTEXT; |
422 | } | 427 | } |
423 | 428 | ||
@@ -447,9 +452,7 @@ static int builtin_then(struct job *cmd, struct jobSet *junk) | |||
447 | local_pending_command = xmalloc(status+1); | 452 | local_pending_command = xmalloc(status+1); |
448 | strncpy(local_pending_command, charptr1, status); | 453 | strncpy(local_pending_command, charptr1, status); |
449 | local_pending_command[status]='\0'; | 454 | local_pending_command[status]='\0'; |
450 | #ifdef DEBUG_SHELL | 455 | debug_printf(stderr, "'then' now running '%s'\n", charptr1); |
451 | fprintf(stderr, "'then' now running '%s'\n", charptr1); | ||
452 | #endif | ||
453 | return( busy_loop(NULL)); | 456 | return( busy_loop(NULL)); |
454 | } | 457 | } |
455 | 458 | ||
@@ -476,9 +479,7 @@ static int builtin_else(struct job *cmd, struct jobSet *junk) | |||
476 | local_pending_command = xmalloc(status+1); | 479 | local_pending_command = xmalloc(status+1); |
477 | strncpy(local_pending_command, charptr1, status); | 480 | strncpy(local_pending_command, charptr1, status); |
478 | local_pending_command[status]='\0'; | 481 | local_pending_command[status]='\0'; |
479 | #ifdef DEBUG_SHELL | 482 | debug_printf(stderr, "'else' now running '%s'\n", charptr1); |
480 | fprintf(stderr, "'else' now running '%s'\n", charptr1); | ||
481 | #endif | ||
482 | return( busy_loop(NULL)); | 483 | return( busy_loop(NULL)); |
483 | } | 484 | } |
484 | 485 | ||
@@ -491,9 +492,7 @@ static int builtin_fi(struct job *cmd, struct jobSet *junk) | |||
491 | } | 492 | } |
492 | /* Clear out the if and then context bits */ | 493 | /* Clear out the if and then context bits */ |
493 | cmd->jobContext &= ~(IF_TRUE_CONTEXT|IF_FALSE_CONTEXT|THEN_EXP_CONTEXT|ELSE_EXP_CONTEXT); | 494 | cmd->jobContext &= ~(IF_TRUE_CONTEXT|IF_FALSE_CONTEXT|THEN_EXP_CONTEXT|ELSE_EXP_CONTEXT); |
494 | #ifdef DEBUG_SHELL | 495 | debug_printf(stderr, "Hit an fi -- jobContext=%d\n", cmd->jobContext); |
495 | fprintf(stderr, "Hit an fi -- jobContext=%d\n", cmd->jobContext); | ||
496 | #endif | ||
497 | return TRUE; | 496 | return TRUE; |
498 | } | 497 | } |
499 | #endif | 498 | #endif |
@@ -656,6 +655,8 @@ static int setupRedirections(struct childProgram *prog) | |||
656 | 655 | ||
657 | static int getCommand(FILE * source, char *command) | 656 | static int getCommand(FILE * source, char *command) |
658 | { | 657 | { |
658 | char *user,buf[255],*s; | ||
659 | |||
659 | if (source == NULL) { | 660 | if (source == NULL) { |
660 | if (local_pending_command) { | 661 | if (local_pending_command) { |
661 | /* a command specified (-c option): return it & mark it done */ | 662 | /* a command specified (-c option): return it & mark it done */ |
@@ -667,6 +668,17 @@ static int getCommand(FILE * source, char *command) | |||
667 | return 1; | 668 | return 1; |
668 | } | 669 | } |
669 | 670 | ||
671 | /* get User Name and setup prompt */ | ||
672 | strcpy(prompt,( geteuid() != 0 ) ? "$ ":"# "); | ||
673 | user=xcalloc(sizeof(int), 9); | ||
674 | my_getpwuid(user, geteuid()); | ||
675 | |||
676 | /* get HostName */ | ||
677 | gethostname(buf, 255); | ||
678 | s = strchr(buf, '.'); | ||
679 | if (s) | ||
680 | *s = 0; | ||
681 | |||
670 | if (source == stdin) { | 682 | if (source == stdin) { |
671 | #ifdef BB_FEATURE_SH_COMMAND_EDITING | 683 | #ifdef BB_FEATURE_SH_COMMAND_EDITING |
672 | int len; | 684 | int len; |
@@ -679,21 +691,33 @@ static int getCommand(FILE * source, char *command) | |||
679 | */ | 691 | */ |
680 | cmdedit_init(); | 692 | cmdedit_init(); |
681 | signal(SIGWINCH, win_changed); | 693 | signal(SIGWINCH, win_changed); |
682 | len=fprintf(stdout, "%s %s", cwd, prompt); | 694 | len=fprintf(stdout, "[%s@%s %s]%s", user, buf, |
695 | get_last_path_component(cwd), prompt); | ||
683 | fflush(stdout); | 696 | fflush(stdout); |
684 | promptStr=(char*)xmalloc(sizeof(char)*(len+1)); | 697 | promptStr=(char*)xmalloc(sizeof(char)*(len+1)); |
685 | sprintf(promptStr, "%s %s", cwd, prompt); | 698 | sprintf(promptStr, "[%s@%s %s]%s", user, buf, |
699 | get_last_path_component(cwd), prompt); | ||
686 | cmdedit_read_input(promptStr, command); | 700 | cmdedit_read_input(promptStr, command); |
687 | free( promptStr); | 701 | free( promptStr); |
688 | cmdedit_terminate(); | 702 | cmdedit_terminate(); |
689 | signal(SIGWINCH, SIG_DFL); | 703 | signal(SIGWINCH, SIG_DFL); |
690 | return 0; | 704 | return 0; |
691 | #else | 705 | #else |
692 | fprintf(stdout, "%s %s", cwd, prompt); | 706 | i=strlen(cwd); |
707 | i--; | ||
708 | if (i>1){ | ||
709 | while ((i>0) && (*(cwd+i)!='/') ) i--; | ||
710 | if (*(cwd+i)=='/') i++; | ||
711 | } | ||
712 | |||
713 | fprintf(stdout, "[%s@%s %s]%s",user, buf, (cwd+i), prompt); | ||
693 | fflush(stdout); | 714 | fflush(stdout); |
694 | #endif | 715 | #endif |
695 | } | 716 | } |
696 | 717 | ||
718 | /* don't leak memory */ | ||
719 | free(user); | ||
720 | |||
697 | if (!fgets(command, BUFSIZ - 2, source)) { | 721 | if (!fgets(command, BUFSIZ - 2, source)) { |
698 | if (source == stdin) | 722 | if (source == stdin) |
699 | printf("\n"); | 723 | printf("\n"); |
@@ -707,10 +731,9 @@ static int getCommand(FILE * source, char *command) | |||
707 | } | 731 | } |
708 | 732 | ||
709 | #ifdef BB_FEATURE_SH_ENVIRONMENT | 733 | #ifdef BB_FEATURE_SH_ENVIRONMENT |
710 | #define __MAX_INT_CHARS 7 | ||
711 | static char* itoa(register int i) | 734 | static char* itoa(register int i) |
712 | { | 735 | { |
713 | static char a[__MAX_INT_CHARS]; | 736 | static char a[7]; /* Max 7 ints */ |
714 | register char *b = a + sizeof(a) - 1; | 737 | register char *b = a + sizeof(a) - 1; |
715 | int sign = (i < 0); | 738 | int sign = (i < 0); |
716 | 739 | ||
@@ -1381,9 +1404,7 @@ static int busy_loop(FILE * input) | |||
1381 | #ifdef BB_FEATURE_SH_ENVIRONMENT | 1404 | #ifdef BB_FEATURE_SH_ENVIRONMENT |
1382 | lastReturnCode=WEXITSTATUS(status); | 1405 | lastReturnCode=WEXITSTATUS(status); |
1383 | #endif | 1406 | #endif |
1384 | #if 0 | 1407 | debug_printf("'%s' exited -- return code %d\n", jobList.fg->text, lastReturnCode); |
1385 | printf("'%s' exited -- return code %d\n", jobList.fg->text, lastReturnCode); | ||
1386 | #endif | ||
1387 | if (!jobList.fg->runningProgs) { | 1408 | if (!jobList.fg->runningProgs) { |
1388 | /* child exited */ | 1409 | /* child exited */ |
1389 | 1410 | ||
@@ -1449,9 +1470,17 @@ int shell_main(int argc_l, char **argv_l) | |||
1449 | argv = argv_l; | 1470 | argv = argv_l; |
1450 | 1471 | ||
1451 | 1472 | ||
1452 | //if (argv[0] && argv[0][0] == '-') { | 1473 | if (argv[0] && argv[0][0] == '-') { |
1453 | // builtin_source("/etc/profile"); | 1474 | FILE *input; |
1454 | //} | 1475 | input = fopen("/etc/profile", "r"); |
1476 | if (!input) { | ||
1477 | fprintf(stdout, "Couldn't open file '/etc/profile'\n"); | ||
1478 | } else { | ||
1479 | /* Now run the file */ | ||
1480 | busy_loop(input); | ||
1481 | fclose(input); | ||
1482 | } | ||
1483 | } | ||
1455 | 1484 | ||
1456 | while ((opt = getopt(argc_l, argv_l, "cx")) > 0) { | 1485 | while ((opt = getopt(argc_l, argv_l, "cx")) > 0) { |
1457 | switch (opt) { | 1486 | switch (opt) { |
@@ -1500,9 +1529,6 @@ int shell_main(int argc_l, char **argv_l) | |||
1500 | atexit(free_memory); | 1529 | atexit(free_memory); |
1501 | #endif | 1530 | #endif |
1502 | 1531 | ||
1503 | #ifdef BB_FEATURE_SH_COMMAND_EDITING | ||
1504 | win_changed(0); | 1532 | win_changed(0); |
1505 | #endif | ||
1506 | |||
1507 | return (busy_loop(input)); | 1533 | return (busy_loop(input)); |
1508 | } | 1534 | } |
diff --git a/shell/lash.c b/shell/lash.c index eba60b3b7..461f2c092 100644 --- a/shell/lash.c +++ b/shell/lash.c | |||
@@ -45,9 +45,7 @@ | |||
45 | #include <sys/wait.h> | 45 | #include <sys/wait.h> |
46 | #include <unistd.h> | 46 | #include <unistd.h> |
47 | #include <getopt.h> | 47 | #include <getopt.h> |
48 | #ifdef BB_FEATURE_SH_COMMAND_EDITING | ||
49 | #include "cmdedit.h" | 48 | #include "cmdedit.h" |
50 | #endif | ||
51 | 49 | ||
52 | #define MAX_LINE 256 /* size of input buffer for `read' builtin */ | 50 | #define MAX_LINE 256 /* size of input buffer for `read' builtin */ |
53 | #define MAX_READ 128 /* size of input buffer for `read' builtin */ | 51 | #define MAX_READ 128 /* size of input buffer for `read' builtin */ |
@@ -55,6 +53,8 @@ | |||
55 | extern size_t NUM_APPLETS; | 53 | extern size_t NUM_APPLETS; |
56 | 54 | ||
57 | 55 | ||
56 | |||
57 | |||
58 | enum redirectionType { REDIRECT_INPUT, REDIRECT_OVERWRITE, | 58 | enum redirectionType { REDIRECT_INPUT, REDIRECT_OVERWRITE, |
59 | REDIRECT_APPEND | 59 | REDIRECT_APPEND |
60 | }; | 60 | }; |
@@ -167,7 +167,7 @@ static struct builtInCommand bltins_forking[] = { | |||
167 | {NULL, NULL, NULL} | 167 | {NULL, NULL, NULL} |
168 | }; | 168 | }; |
169 | 169 | ||
170 | static char *prompt = "# "; | 170 | static char prompt[3]; |
171 | static char *cwd; | 171 | static char *cwd; |
172 | static char *local_pending_command = NULL; | 172 | static char *local_pending_command = NULL; |
173 | static char *promptStr = NULL; | 173 | static char *promptStr = NULL; |
@@ -180,9 +180,20 @@ static int lastReturnCode=-1; | |||
180 | static int showXtrace=FALSE; | 180 | static int showXtrace=FALSE; |
181 | #endif | 181 | #endif |
182 | 182 | ||
183 | #ifdef DEBUG_SHELL | ||
184 | static inline void debug_printf(const char *format, ...) | ||
185 | { | ||
186 | va_list args; | ||
187 | va_start(args, format); | ||
188 | vfprintf(stderr, s, p); | ||
189 | va_end(args); | ||
190 | } | ||
191 | #else | ||
192 | static inline void debug_printf(const char *format, ...) { } | ||
193 | #endif | ||
183 | 194 | ||
184 | #ifdef BB_FEATURE_SH_COMMAND_EDITING | 195 | #ifdef BB_FEATURE_SH_COMMAND_EDITING |
185 | void win_changed(int junk) | 196 | static inline void win_changed(int junk) |
186 | { | 197 | { |
187 | struct winsize win = { 0, 0, 0, 0 }; | 198 | struct winsize win = { 0, 0, 0, 0 }; |
188 | ioctl(0, TIOCGWINSZ, &win); | 199 | ioctl(0, TIOCGWINSZ, &win); |
@@ -190,6 +201,8 @@ void win_changed(int junk) | |||
190 | cmdedit_setwidth( win.ws_col - 1); | 201 | cmdedit_setwidth( win.ws_col - 1); |
191 | } | 202 | } |
192 | } | 203 | } |
204 | #else | ||
205 | static inline void win_changed(int junk) {} | ||
193 | #endif | 206 | #endif |
194 | 207 | ||
195 | 208 | ||
@@ -402,22 +415,14 @@ static int builtin_if(struct job *cmd, struct jobSet *jobList) | |||
402 | local_pending_command = xmalloc(status+1); | 415 | local_pending_command = xmalloc(status+1); |
403 | strncpy(local_pending_command, charptr1, status); | 416 | strncpy(local_pending_command, charptr1, status); |
404 | local_pending_command[status]='\0'; | 417 | local_pending_command[status]='\0'; |
405 | #ifdef DEBUG_SHELL | 418 | debug_printf(stderr, "'if' now testing '%s'\n", local_pending_command); |
406 | fprintf(stderr, "'if' now testing '%s'\n", local_pending_command); | ||
407 | #endif | ||
408 | status = busy_loop(NULL); /* Frees local_pending_command */ | 419 | status = busy_loop(NULL); /* Frees local_pending_command */ |
409 | #ifdef DEBUG_SHELL | 420 | debug_printf(stderr, "if test returned "); |
410 | fprintf(stderr, "if test returned "); | ||
411 | #endif | ||
412 | if (status == 0) { | 421 | if (status == 0) { |
413 | #ifdef DEBUG_SHELL | 422 | debug_printf(stderr, "TRUE\n"); |
414 | fprintf(stderr, "TRUE\n"); | ||
415 | #endif | ||
416 | cmd->jobContext |= IF_TRUE_CONTEXT; | 423 | cmd->jobContext |= IF_TRUE_CONTEXT; |
417 | } else { | 424 | } else { |
418 | #ifdef DEBUG_SHELL | 425 | debug_printf(stderr, "FALSE\n"); |
419 | fprintf(stderr, "FALSE\n"); | ||
420 | #endif | ||
421 | cmd->jobContext |= IF_FALSE_CONTEXT; | 426 | cmd->jobContext |= IF_FALSE_CONTEXT; |
422 | } | 427 | } |
423 | 428 | ||
@@ -447,9 +452,7 @@ static int builtin_then(struct job *cmd, struct jobSet *junk) | |||
447 | local_pending_command = xmalloc(status+1); | 452 | local_pending_command = xmalloc(status+1); |
448 | strncpy(local_pending_command, charptr1, status); | 453 | strncpy(local_pending_command, charptr1, status); |
449 | local_pending_command[status]='\0'; | 454 | local_pending_command[status]='\0'; |
450 | #ifdef DEBUG_SHELL | 455 | debug_printf(stderr, "'then' now running '%s'\n", charptr1); |
451 | fprintf(stderr, "'then' now running '%s'\n", charptr1); | ||
452 | #endif | ||
453 | return( busy_loop(NULL)); | 456 | return( busy_loop(NULL)); |
454 | } | 457 | } |
455 | 458 | ||
@@ -476,9 +479,7 @@ static int builtin_else(struct job *cmd, struct jobSet *junk) | |||
476 | local_pending_command = xmalloc(status+1); | 479 | local_pending_command = xmalloc(status+1); |
477 | strncpy(local_pending_command, charptr1, status); | 480 | strncpy(local_pending_command, charptr1, status); |
478 | local_pending_command[status]='\0'; | 481 | local_pending_command[status]='\0'; |
479 | #ifdef DEBUG_SHELL | 482 | debug_printf(stderr, "'else' now running '%s'\n", charptr1); |
480 | fprintf(stderr, "'else' now running '%s'\n", charptr1); | ||
481 | #endif | ||
482 | return( busy_loop(NULL)); | 483 | return( busy_loop(NULL)); |
483 | } | 484 | } |
484 | 485 | ||
@@ -491,9 +492,7 @@ static int builtin_fi(struct job *cmd, struct jobSet *junk) | |||
491 | } | 492 | } |
492 | /* Clear out the if and then context bits */ | 493 | /* Clear out the if and then context bits */ |
493 | cmd->jobContext &= ~(IF_TRUE_CONTEXT|IF_FALSE_CONTEXT|THEN_EXP_CONTEXT|ELSE_EXP_CONTEXT); | 494 | cmd->jobContext &= ~(IF_TRUE_CONTEXT|IF_FALSE_CONTEXT|THEN_EXP_CONTEXT|ELSE_EXP_CONTEXT); |
494 | #ifdef DEBUG_SHELL | 495 | debug_printf(stderr, "Hit an fi -- jobContext=%d\n", cmd->jobContext); |
495 | fprintf(stderr, "Hit an fi -- jobContext=%d\n", cmd->jobContext); | ||
496 | #endif | ||
497 | return TRUE; | 496 | return TRUE; |
498 | } | 497 | } |
499 | #endif | 498 | #endif |
@@ -656,6 +655,8 @@ static int setupRedirections(struct childProgram *prog) | |||
656 | 655 | ||
657 | static int getCommand(FILE * source, char *command) | 656 | static int getCommand(FILE * source, char *command) |
658 | { | 657 | { |
658 | char *user,buf[255],*s; | ||
659 | |||
659 | if (source == NULL) { | 660 | if (source == NULL) { |
660 | if (local_pending_command) { | 661 | if (local_pending_command) { |
661 | /* a command specified (-c option): return it & mark it done */ | 662 | /* a command specified (-c option): return it & mark it done */ |
@@ -667,6 +668,17 @@ static int getCommand(FILE * source, char *command) | |||
667 | return 1; | 668 | return 1; |
668 | } | 669 | } |
669 | 670 | ||
671 | /* get User Name and setup prompt */ | ||
672 | strcpy(prompt,( geteuid() != 0 ) ? "$ ":"# "); | ||
673 | user=xcalloc(sizeof(int), 9); | ||
674 | my_getpwuid(user, geteuid()); | ||
675 | |||
676 | /* get HostName */ | ||
677 | gethostname(buf, 255); | ||
678 | s = strchr(buf, '.'); | ||
679 | if (s) | ||
680 | *s = 0; | ||
681 | |||
670 | if (source == stdin) { | 682 | if (source == stdin) { |
671 | #ifdef BB_FEATURE_SH_COMMAND_EDITING | 683 | #ifdef BB_FEATURE_SH_COMMAND_EDITING |
672 | int len; | 684 | int len; |
@@ -679,21 +691,33 @@ static int getCommand(FILE * source, char *command) | |||
679 | */ | 691 | */ |
680 | cmdedit_init(); | 692 | cmdedit_init(); |
681 | signal(SIGWINCH, win_changed); | 693 | signal(SIGWINCH, win_changed); |
682 | len=fprintf(stdout, "%s %s", cwd, prompt); | 694 | len=fprintf(stdout, "[%s@%s %s]%s", user, buf, |
695 | get_last_path_component(cwd), prompt); | ||
683 | fflush(stdout); | 696 | fflush(stdout); |
684 | promptStr=(char*)xmalloc(sizeof(char)*(len+1)); | 697 | promptStr=(char*)xmalloc(sizeof(char)*(len+1)); |
685 | sprintf(promptStr, "%s %s", cwd, prompt); | 698 | sprintf(promptStr, "[%s@%s %s]%s", user, buf, |
699 | get_last_path_component(cwd), prompt); | ||
686 | cmdedit_read_input(promptStr, command); | 700 | cmdedit_read_input(promptStr, command); |
687 | free( promptStr); | 701 | free( promptStr); |
688 | cmdedit_terminate(); | 702 | cmdedit_terminate(); |
689 | signal(SIGWINCH, SIG_DFL); | 703 | signal(SIGWINCH, SIG_DFL); |
690 | return 0; | 704 | return 0; |
691 | #else | 705 | #else |
692 | fprintf(stdout, "%s %s", cwd, prompt); | 706 | i=strlen(cwd); |
707 | i--; | ||
708 | if (i>1){ | ||
709 | while ((i>0) && (*(cwd+i)!='/') ) i--; | ||
710 | if (*(cwd+i)=='/') i++; | ||
711 | } | ||
712 | |||
713 | fprintf(stdout, "[%s@%s %s]%s",user, buf, (cwd+i), prompt); | ||
693 | fflush(stdout); | 714 | fflush(stdout); |
694 | #endif | 715 | #endif |
695 | } | 716 | } |
696 | 717 | ||
718 | /* don't leak memory */ | ||
719 | free(user); | ||
720 | |||
697 | if (!fgets(command, BUFSIZ - 2, source)) { | 721 | if (!fgets(command, BUFSIZ - 2, source)) { |
698 | if (source == stdin) | 722 | if (source == stdin) |
699 | printf("\n"); | 723 | printf("\n"); |
@@ -707,10 +731,9 @@ static int getCommand(FILE * source, char *command) | |||
707 | } | 731 | } |
708 | 732 | ||
709 | #ifdef BB_FEATURE_SH_ENVIRONMENT | 733 | #ifdef BB_FEATURE_SH_ENVIRONMENT |
710 | #define __MAX_INT_CHARS 7 | ||
711 | static char* itoa(register int i) | 734 | static char* itoa(register int i) |
712 | { | 735 | { |
713 | static char a[__MAX_INT_CHARS]; | 736 | static char a[7]; /* Max 7 ints */ |
714 | register char *b = a + sizeof(a) - 1; | 737 | register char *b = a + sizeof(a) - 1; |
715 | int sign = (i < 0); | 738 | int sign = (i < 0); |
716 | 739 | ||
@@ -1381,9 +1404,7 @@ static int busy_loop(FILE * input) | |||
1381 | #ifdef BB_FEATURE_SH_ENVIRONMENT | 1404 | #ifdef BB_FEATURE_SH_ENVIRONMENT |
1382 | lastReturnCode=WEXITSTATUS(status); | 1405 | lastReturnCode=WEXITSTATUS(status); |
1383 | #endif | 1406 | #endif |
1384 | #if 0 | 1407 | debug_printf("'%s' exited -- return code %d\n", jobList.fg->text, lastReturnCode); |
1385 | printf("'%s' exited -- return code %d\n", jobList.fg->text, lastReturnCode); | ||
1386 | #endif | ||
1387 | if (!jobList.fg->runningProgs) { | 1408 | if (!jobList.fg->runningProgs) { |
1388 | /* child exited */ | 1409 | /* child exited */ |
1389 | 1410 | ||
@@ -1449,9 +1470,17 @@ int shell_main(int argc_l, char **argv_l) | |||
1449 | argv = argv_l; | 1470 | argv = argv_l; |
1450 | 1471 | ||
1451 | 1472 | ||
1452 | //if (argv[0] && argv[0][0] == '-') { | 1473 | if (argv[0] && argv[0][0] == '-') { |
1453 | // builtin_source("/etc/profile"); | 1474 | FILE *input; |
1454 | //} | 1475 | input = fopen("/etc/profile", "r"); |
1476 | if (!input) { | ||
1477 | fprintf(stdout, "Couldn't open file '/etc/profile'\n"); | ||
1478 | } else { | ||
1479 | /* Now run the file */ | ||
1480 | busy_loop(input); | ||
1481 | fclose(input); | ||
1482 | } | ||
1483 | } | ||
1455 | 1484 | ||
1456 | while ((opt = getopt(argc_l, argv_l, "cx")) > 0) { | 1485 | while ((opt = getopt(argc_l, argv_l, "cx")) > 0) { |
1457 | switch (opt) { | 1486 | switch (opt) { |
@@ -1500,9 +1529,6 @@ int shell_main(int argc_l, char **argv_l) | |||
1500 | atexit(free_memory); | 1529 | atexit(free_memory); |
1501 | #endif | 1530 | #endif |
1502 | 1531 | ||
1503 | #ifdef BB_FEATURE_SH_COMMAND_EDITING | ||
1504 | win_changed(0); | 1532 | win_changed(0); |
1505 | #endif | ||
1506 | |||
1507 | return (busy_loop(input)); | 1533 | return (busy_loop(input)); |
1508 | } | 1534 | } |