diff options
author | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2007-03-31 11:07:30 +0000 |
---|---|---|
committer | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2007-03-31 11:07:30 +0000 |
commit | 95a9fe21d2c31b06ce27e9139a45cc3c9f8fbee3 (patch) | |
tree | b32ccdfec5bdcb8aafe9512a3b34e8f61cacb81c | |
parent | a259d93e72097f8ce7bcc5bca76ff42d2515cefd (diff) | |
download | busybox-w32-95a9fe21d2c31b06ce27e9139a45cc3c9f8fbee3.tar.gz busybox-w32-95a9fe21d2c31b06ce27e9139a45cc3c9f8fbee3.tar.bz2 busybox-w32-95a9fe21d2c31b06ce27e9139a45cc3c9f8fbee3.zip |
lash: style cleanup
git-svn-id: svn://busybox.net/trunk/busybox@18289 69ca8d6d-28ef-0310-b511-8ec308f3f277
-rw-r--r-- | shell/lash.c | 165 |
1 files changed, 84 insertions, 81 deletions
diff --git a/shell/lash.c b/shell/lash.c index f938a74b9..99b6173bf 100644 --- a/shell/lash.c +++ b/shell/lash.c | |||
@@ -29,12 +29,14 @@ | |||
29 | /* Always enable for the moment... */ | 29 | /* Always enable for the moment... */ |
30 | #define CONFIG_LASH_PIPE_N_REDIRECTS | 30 | #define CONFIG_LASH_PIPE_N_REDIRECTS |
31 | #define CONFIG_LASH_JOB_CONTROL | 31 | #define CONFIG_LASH_JOB_CONTROL |
32 | #define ENABLE_LASH_PIPE_N_REDIRECTS 1 | ||
33 | #define ENABLE_LASH_JOB_CONTROL 1 | ||
32 | 34 | ||
33 | enum { MAX_READ = 128 }; /* size of input buffer for 'read' builtin */ | 35 | enum { MAX_READ = 128 }; /* size of input buffer for 'read' builtin */ |
34 | #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" | 36 | #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" |
35 | 37 | ||
36 | 38 | ||
37 | #ifdef CONFIG_LASH_PIPE_N_REDIRECTS | 39 | #if ENABLE_LASH_PIPE_N_REDIRECTS |
38 | enum redir_type { REDIRECT_INPUT, REDIRECT_OVERWRITE, | 40 | enum redir_type { REDIRECT_INPUT, REDIRECT_OVERWRITE, |
39 | REDIRECT_APPEND | 41 | REDIRECT_APPEND |
40 | }; | 42 | }; |
@@ -51,7 +53,7 @@ enum { | |||
51 | #define LASH_OPT_DONE (1) | 53 | #define LASH_OPT_DONE (1) |
52 | #define LASH_OPT_SAW_QUOTE (2) | 54 | #define LASH_OPT_SAW_QUOTE (2) |
53 | 55 | ||
54 | #ifdef CONFIG_LASH_PIPE_N_REDIRECTS | 56 | #if ENABLE_LASH_PIPE_N_REDIRECTS |
55 | struct redir_struct { | 57 | struct redir_struct { |
56 | enum redir_type type; /* type of redirection */ | 58 | enum redir_type type; /* type of redirection */ |
57 | int fd; /* file descriptor being redirected */ | 59 | int fd; /* file descriptor being redirected */ |
@@ -65,7 +67,7 @@ struct child_prog { | |||
65 | int num_redirects; /* elements in redirection array */ | 67 | int num_redirects; /* elements in redirection array */ |
66 | int is_stopped; /* is the program currently running? */ | 68 | int is_stopped; /* is the program currently running? */ |
67 | struct job *family; /* pointer back to the child's parent job */ | 69 | struct job *family; /* pointer back to the child's parent job */ |
68 | #ifdef CONFIG_LASH_PIPE_N_REDIRECTS | 70 | #if ENABLE_LASH_PIPE_N_REDIRECTS |
69 | struct redir_struct *redirects; /* I/O redirects */ | 71 | struct redir_struct *redirects; /* I/O redirects */ |
70 | #endif | 72 | #endif |
71 | }; | 73 | }; |
@@ -229,7 +231,8 @@ static int builtin_exec(struct child_prog *child) | |||
229 | if (child->argv[1] == NULL) | 231 | if (child->argv[1] == NULL) |
230 | return EXIT_SUCCESS; /* Really? */ | 232 | return EXIT_SUCCESS; /* Really? */ |
231 | child->argv++; | 233 | child->argv++; |
232 | while(close_me_list) close((long)llist_pop(&close_me_list)); | 234 | while (close_me_list) |
235 | close((long)llist_pop(&close_me_list)); | ||
233 | pseudo_exec(child); | 236 | pseudo_exec(child); |
234 | /* never returns */ | 237 | /* never returns */ |
235 | } | 238 | } |
@@ -247,7 +250,7 @@ static int builtin_exit(struct child_prog *child) | |||
247 | static int builtin_fg_bg(struct child_prog *child) | 250 | static int builtin_fg_bg(struct child_prog *child) |
248 | { | 251 | { |
249 | int i, jobnum; | 252 | int i, jobnum; |
250 | struct job *job=NULL; | 253 | struct job *job = NULL; |
251 | 254 | ||
252 | /* If they gave us no args, assume they want the last backgrounded task */ | 255 | /* If they gave us no args, assume they want the last backgrounded task */ |
253 | if (!child->argv[1]) { | 256 | if (!child->argv[1]) { |
@@ -289,7 +292,8 @@ static int builtin_fg_bg(struct child_prog *child) | |||
289 | 292 | ||
290 | job->stopped_progs = 0; | 293 | job->stopped_progs = 0; |
291 | 294 | ||
292 | if ( (i=kill(- job->pgrp, SIGCONT)) < 0) { | 295 | i = kill(- job->pgrp, SIGCONT); |
296 | if (i < 0) { | ||
293 | if (i == ESRCH) { | 297 | if (i == ESRCH) { |
294 | remove_job(&job_list, job); | 298 | remove_job(&job_list, job); |
295 | } else { | 299 | } else { |
@@ -365,16 +369,16 @@ static int builtin_export(struct child_prog *child) | |||
365 | res = putenv(v); | 369 | res = putenv(v); |
366 | if (res) | 370 | if (res) |
367 | bb_perror_msg("export"); | 371 | bb_perror_msg("export"); |
368 | #ifdef CONFIG_FEATURE_EDITING_FANCY_PROMPT | 372 | #if ENABLE_FEATURE_EDITING_FANCY_PROMPT |
369 | if (strncmp(v, "PS1=", 4)==0) | 373 | if (strncmp(v, "PS1=", 4) == 0) |
370 | PS1 = getenv("PS1"); | 374 | PS1 = getenv("PS1"); |
371 | #endif | 375 | #endif |
372 | 376 | ||
373 | #ifdef CONFIG_LOCALE_SUPPORT | 377 | #if ENABLE_LOCALE_SUPPORT |
374 | // TODO: why getenv? "" would be just as good... | 378 | // TODO: why getenv? "" would be just as good... |
375 | if(strncmp(v, "LC_ALL=", 7)==0) | 379 | if (strncmp(v, "LC_ALL=", 7) == 0) |
376 | setlocale(LC_ALL, getenv("LC_ALL")); | 380 | setlocale(LC_ALL, getenv("LC_ALL")); |
377 | if(strncmp(v, "LC_CTYPE=", 9)==0) | 381 | if (strncmp(v, "LC_CTYPE=", 9) == 0) |
378 | setlocale(LC_CTYPE, getenv("LC_CTYPE")); | 382 | setlocale(LC_CTYPE, getenv("LC_CTYPE")); |
379 | #endif | 383 | #endif |
380 | 384 | ||
@@ -445,7 +449,7 @@ static int builtin_unset(struct child_prog *child) | |||
445 | return EXIT_SUCCESS; | 449 | return EXIT_SUCCESS; |
446 | } | 450 | } |
447 | 451 | ||
448 | #ifdef CONFIG_LASH_JOB_CONTROL | 452 | #if ENABLE_LASH_JOB_CONTROL |
449 | /* free up all memory from a job */ | 453 | /* free up all memory from a job */ |
450 | static void free_job(struct job *cmd) | 454 | static void free_job(struct job *cmd) |
451 | { | 455 | { |
@@ -454,7 +458,7 @@ static void free_job(struct job *cmd) | |||
454 | 458 | ||
455 | for (i = 0; i < cmd->num_progs; i++) { | 459 | for (i = 0; i < cmd->num_progs; i++) { |
456 | free(cmd->progs[i].argv); | 460 | free(cmd->progs[i].argv); |
457 | #ifdef CONFIG_LASH_PIPE_N_REDIRECTS | 461 | #if ENABLE_LASH_PIPE_N_REDIRECTS |
458 | if (cmd->progs[i].redirects) | 462 | if (cmd->progs[i].redirects) |
459 | free(cmd->progs[i].redirects); | 463 | free(cmd->progs[i].redirects); |
460 | #endif | 464 | #endif |
@@ -509,7 +513,7 @@ static void checkjobs(struct jobset *j_list) | |||
509 | } | 513 | } |
510 | 514 | ||
511 | /* This happens on backticked commands */ | 515 | /* This happens on backticked commands */ |
512 | if(job==NULL) | 516 | if (job == NULL) |
513 | return; | 517 | return; |
514 | 518 | ||
515 | if (WIFEXITED(status) || WIFSIGNALED(status)) { | 519 | if (WIFEXITED(status) || WIFSIGNALED(status)) { |
@@ -519,7 +523,7 @@ static void checkjobs(struct jobset *j_list) | |||
519 | 523 | ||
520 | if (!job->running_progs) { | 524 | if (!job->running_progs) { |
521 | printf(JOB_STATUS_FORMAT, job->jobid, "Done", job->text); | 525 | printf(JOB_STATUS_FORMAT, job->jobid, "Done", job->text); |
522 | last_jobid=0; | 526 | last_jobid = 0; |
523 | remove_job(j_list, job); | 527 | remove_job(j_list, job); |
524 | } | 528 | } |
525 | } else { | 529 | } else { |
@@ -544,7 +548,7 @@ static void remove_job(struct jobset *j_list, struct job *job) | |||
544 | } | 548 | } |
545 | #endif | 549 | #endif |
546 | 550 | ||
547 | #ifdef CONFIG_LASH_PIPE_N_REDIRECTS | 551 | #if ENABLE_LASH_PIPE_N_REDIRECTS |
548 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, | 552 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
549 | * and stderr if they are redirected. */ | 553 | * and stderr if they are redirected. */ |
550 | static int setup_redirects(struct child_prog *prog, int squirrel[]) | 554 | static int setup_redirects(struct child_prog *prog, int squirrel[]) |
@@ -591,7 +595,7 @@ static int setup_redirects(struct child_prog *prog, int squirrel[]) | |||
591 | static void restore_redirects(int squirrel[]) | 595 | static void restore_redirects(int squirrel[]) |
592 | { | 596 | { |
593 | int i, fd; | 597 | int i, fd; |
594 | for (i=0; i<3; i++) { | 598 | for (i = 0; i < 3; i++) { |
595 | fd = squirrel[i]; | 599 | fd = squirrel[i]; |
596 | if (fd != -1) { | 600 | if (fd != -1) { |
597 | /* No error checking. I sure wouldn't know what | 601 | /* No error checking. I sure wouldn't know what |
@@ -617,7 +621,7 @@ static inline void cmdedit_set_initial_prompt(void) | |||
617 | PS1 = NULL; | 621 | PS1 = NULL; |
618 | #else | 622 | #else |
619 | PS1 = getenv("PS1"); | 623 | PS1 = getenv("PS1"); |
620 | if(PS1==0) | 624 | if (PS1 == 0) |
621 | PS1 = "\\w \\$ "; | 625 | PS1 = "\\w \\$ "; |
622 | #endif | 626 | #endif |
623 | } | 627 | } |
@@ -637,7 +641,7 @@ static inline const char* setup_prompt_string(void) | |||
637 | return PS2; | 641 | return PS2; |
638 | } | 642 | } |
639 | #else | 643 | #else |
640 | return (shell_context==0)? PS1 : PS2; | 644 | return (shell_context == 0)? PS1 : PS2; |
641 | #endif | 645 | #endif |
642 | } | 646 | } |
643 | 647 | ||
@@ -686,10 +690,10 @@ static int get_command(FILE * source, char *command) | |||
686 | return 0; | 690 | return 0; |
687 | } | 691 | } |
688 | 692 | ||
689 | static char * strsep_space( char *string, int * ix) | 693 | static char * strsep_space(char *string, int * ix) |
690 | { | 694 | { |
691 | /* Short circuit the trivial case */ | 695 | /* Short circuit the trivial case */ |
692 | if ( !string || ! string[*ix]) | 696 | if (!string || ! string[*ix]) |
693 | return NULL; | 697 | return NULL; |
694 | 698 | ||
695 | /* Find the end of the token. */ | 699 | /* Find the end of the token. */ |
@@ -713,7 +717,7 @@ static char * strsep_space( char *string, int * ix) | |||
713 | 717 | ||
714 | static int expand_arguments(char *command) | 718 | static int expand_arguments(char *command) |
715 | { | 719 | { |
716 | int total_length=0, length, i, retval, ix = 0; | 720 | int total_length = 0, length, i, retval, ix = 0; |
717 | expand_t expand_result; | 721 | expand_t expand_result; |
718 | char *tmpcmd, *cmd, *cmd_copy; | 722 | char *tmpcmd, *cmd, *cmd_copy; |
719 | char *src, *dst, *var; | 723 | char *src, *dst, *var; |
@@ -731,7 +735,7 @@ static int expand_arguments(char *command) | |||
731 | chomp(command); | 735 | chomp(command); |
732 | 736 | ||
733 | /* Fix up escape sequences to be the Real Thing(tm) */ | 737 | /* Fix up escape sequences to be the Real Thing(tm) */ |
734 | while( command && command[ix]) { | 738 | while (command && command[ix]) { |
735 | if (command[ix] == '\\') { | 739 | if (command[ix] == '\\') { |
736 | const char *tmp = command+ix+1; | 740 | const char *tmp = command+ix+1; |
737 | command[ix] = bb_process_escape_sequence( &tmp ); | 741 | command[ix] = bb_process_escape_sequence( &tmp ); |
@@ -750,7 +754,7 @@ static int expand_arguments(char *command) | |||
750 | cmd = cmd_copy = xstrdup(command); | 754 | cmd = cmd_copy = xstrdup(command); |
751 | *command = '\0'; | 755 | *command = '\0'; |
752 | for (ix = 0, tmpcmd = cmd; | 756 | for (ix = 0, tmpcmd = cmd; |
753 | (tmpcmd = strsep_space(cmd, &ix)) != NULL; cmd += ix, ix=0) { | 757 | (tmpcmd = strsep_space(cmd, &ix)) != NULL; cmd += ix, ix = 0) { |
754 | if (*tmpcmd == '\0') | 758 | if (*tmpcmd == '\0') |
755 | break; | 759 | break; |
756 | /* we need to trim() the result for glob! */ | 760 | /* we need to trim() the result for glob! */ |
@@ -771,16 +775,16 @@ static int expand_arguments(char *command) | |||
771 | } else { | 775 | } else { |
772 | /* Convert from char** (one word per string) to a simple char*, | 776 | /* Convert from char** (one word per string) to a simple char*, |
773 | * but don't overflow command which is BUFSIZ in length */ | 777 | * but don't overflow command which is BUFSIZ in length */ |
774 | for (i=0; i < expand_result.gl_pathc; i++) { | 778 | for (i = 0; i < expand_result.gl_pathc; i++) { |
775 | length=strlen(expand_result.gl_pathv[i]); | 779 | length = strlen(expand_result.gl_pathv[i]); |
776 | if (total_length+length+1 >= BUFSIZ) { | 780 | if (total_length+length+1 >= BUFSIZ) { |
777 | bb_error_msg(out_of_space); | 781 | bb_error_msg(out_of_space); |
778 | return FALSE; | 782 | return FALSE; |
779 | } | 783 | } |
780 | strcat(command+total_length, " "); | 784 | strcat(command+total_length, " "); |
781 | total_length+=1; | 785 | total_length += 1; |
782 | strcat(command+total_length, expand_result.gl_pathv[i]); | 786 | strcat(command+total_length, expand_result.gl_pathv[i]); |
783 | total_length+=length; | 787 | total_length += length; |
784 | } | 788 | } |
785 | globfree (&expand_result); | 789 | globfree (&expand_result); |
786 | } | 790 | } |
@@ -791,14 +795,14 @@ static int expand_arguments(char *command) | |||
791 | /* Now do the shell variable substitutions which | 795 | /* Now do the shell variable substitutions which |
792 | * wordexp can't do for us, namely $? and $! */ | 796 | * wordexp can't do for us, namely $? and $! */ |
793 | src = command; | 797 | src = command; |
794 | while((dst = strchr(src,'$')) != NULL){ | 798 | while ((dst = strchr(src,'$')) != NULL) { |
795 | var = NULL; | 799 | var = NULL; |
796 | switch (*(dst+1)) { | 800 | switch (*(dst+1)) { |
797 | case '?': | 801 | case '?': |
798 | var = itoa(last_return_code); | 802 | var = itoa(last_return_code); |
799 | break; | 803 | break; |
800 | case '!': | 804 | case '!': |
801 | if (last_bg_pid==-1) | 805 | if (last_bg_pid == -1) |
802 | *var = '\0'; | 806 | *var = '\0'; |
803 | else | 807 | else |
804 | var = itoa(last_bg_pid); | 808 | var = itoa(last_bg_pid); |
@@ -817,9 +821,9 @@ static int expand_arguments(char *command) | |||
817 | case '0':case '1':case '2':case '3':case '4': | 821 | case '0':case '1':case '2':case '3':case '4': |
818 | case '5':case '6':case '7':case '8':case '9': | 822 | case '5':case '6':case '7':case '8':case '9': |
819 | { | 823 | { |
820 | int ixx=*(dst+1)-48+1; | 824 | int ixx = *(dst+1)-48+1; |
821 | if (ixx >= argc) { | 825 | if (ixx >= argc) { |
822 | var='\0'; | 826 | var = '\0'; |
823 | } else { | 827 | } else { |
824 | var = argv[ixx]; | 828 | var = argv[ixx]; |
825 | } | 829 | } |
@@ -830,27 +834,27 @@ static int expand_arguments(char *command) | |||
830 | if (var) { | 834 | if (var) { |
831 | /* a single character construction was found, and | 835 | /* a single character construction was found, and |
832 | * already handled in the case statement */ | 836 | * already handled in the case statement */ |
833 | src=dst+2; | 837 | src = dst + 2; |
834 | } else { | 838 | } else { |
835 | /* Looks like an environment variable */ | 839 | /* Looks like an environment variable */ |
836 | char delim_hold; | 840 | char delim_hold; |
837 | int num_skip_chars=0; | 841 | int num_skip_chars = 0; |
838 | int dstlen = strlen(dst); | 842 | int dstlen = strlen(dst); |
839 | /* Is this a ${foo} type variable? */ | 843 | /* Is this a ${foo} type variable? */ |
840 | if (dstlen >=2 && *(dst+1) == '{') { | 844 | if (dstlen >= 2 && *(dst+1) == '{') { |
841 | src=strchr(dst+1, '}'); | 845 | src = strchr(dst+1, '}'); |
842 | num_skip_chars=1; | 846 | num_skip_chars = 1; |
843 | } else { | 847 | } else { |
844 | src=dst+1; | 848 | src = dst + 1; |
845 | while((isalnum)(*src) || *src=='_') src++; | 849 | while ((isalnum)(*src) || *src == '_') src++; |
846 | } | 850 | } |
847 | if (src == NULL) { | 851 | if (src == NULL) { |
848 | src = dst+dstlen; | 852 | src = dst+dstlen; |
849 | } | 853 | } |
850 | delim_hold=*src; | 854 | delim_hold = *src; |
851 | *src='\0'; /* temporary */ | 855 | *src = '\0'; /* temporary */ |
852 | var = getenv(dst + 1 + num_skip_chars); | 856 | var = getenv(dst + 1 + num_skip_chars); |
853 | *src=delim_hold; | 857 | *src = delim_hold; |
854 | src += num_skip_chars; | 858 | src += num_skip_chars; |
855 | } | 859 | } |
856 | if (var == NULL) { | 860 | if (var == NULL) { |
@@ -891,7 +895,7 @@ static int parse_command(char **command_ptr, struct job *job, int *inbg) | |||
891 | int argv_alloced; | 895 | int argv_alloced; |
892 | char quote = '\0'; | 896 | char quote = '\0'; |
893 | struct child_prog *prog; | 897 | struct child_prog *prog; |
894 | #ifdef CONFIG_LASH_PIPE_N_REDIRECTS | 898 | #if ENABLE_LASH_PIPE_N_REDIRECTS |
895 | int i; | 899 | int i; |
896 | char *chptr; | 900 | char *chptr; |
897 | #endif | 901 | #endif |
@@ -901,7 +905,7 @@ static int parse_command(char **command_ptr, struct job *job, int *inbg) | |||
901 | 905 | ||
902 | /* this handles empty lines or leading '#' characters */ | 906 | /* this handles empty lines or leading '#' characters */ |
903 | if (!**command_ptr || (**command_ptr == '#')) { | 907 | if (!**command_ptr || (**command_ptr == '#')) { |
904 | job->num_progs=0; | 908 | job->num_progs = 0; |
905 | return 0; | 909 | return 0; |
906 | } | 910 | } |
907 | 911 | ||
@@ -923,7 +927,7 @@ static int parse_command(char **command_ptr, struct job *job, int *inbg) | |||
923 | prog->num_redirects = 0; | 927 | prog->num_redirects = 0; |
924 | prog->is_stopped = 0; | 928 | prog->is_stopped = 0; |
925 | prog->family = job; | 929 | prog->family = job; |
926 | #ifdef CONFIG_LASH_PIPE_N_REDIRECTS | 930 | #if ENABLE_LASH_PIPE_N_REDIRECTS |
927 | prog->redirects = NULL; | 931 | prog->redirects = NULL; |
928 | #endif | 932 | #endif |
929 | 933 | ||
@@ -961,8 +965,7 @@ static int parse_command(char **command_ptr, struct job *job, int *inbg) | |||
961 | if ((argc_l + 1) == argv_alloced) { | 965 | if ((argc_l + 1) == argv_alloced) { |
962 | argv_alloced += 5; | 966 | argv_alloced += 5; |
963 | prog->argv = xrealloc(prog->argv, | 967 | prog->argv = xrealloc(prog->argv, |
964 | sizeof(*prog->argv) * | 968 | sizeof(*prog->argv) * argv_alloced); |
965 | argv_alloced); | ||
966 | } | 969 | } |
967 | prog->argv[argc_l] = buf; | 970 | prog->argv[argc_l] = buf; |
968 | flag ^= LASH_OPT_SAW_QUOTE; | 971 | flag ^= LASH_OPT_SAW_QUOTE; |
@@ -982,13 +985,12 @@ static int parse_command(char **command_ptr, struct job *job, int *inbg) | |||
982 | flag |= LASH_OPT_DONE; | 985 | flag |= LASH_OPT_DONE; |
983 | break; | 986 | break; |
984 | 987 | ||
985 | #ifdef CONFIG_LASH_PIPE_N_REDIRECTS | 988 | #if ENABLE_LASH_PIPE_N_REDIRECTS |
986 | case '>': /* redirects */ | 989 | case '>': /* redirects */ |
987 | case '<': | 990 | case '<': |
988 | i = prog->num_redirects++; | 991 | i = prog->num_redirects++; |
989 | prog->redirects = xrealloc(prog->redirects, | 992 | prog->redirects = xrealloc(prog->redirects, |
990 | sizeof(*prog->redirects) * | 993 | sizeof(*prog->redirects) * (i + 1)); |
991 | (i + 1)); | ||
992 | 994 | ||
993 | prog->redirects[i].fd = -1; | 995 | prog->redirects[i].fd = -1; |
994 | if (buf != prog->argv[argc_l]) { | 996 | if (buf != prog->argv[argc_l]) { |
@@ -1027,7 +1029,7 @@ static int parse_command(char **command_ptr, struct job *job, int *inbg) | |||
1027 | if (!*chptr) { | 1029 | if (!*chptr) { |
1028 | bb_error_msg("file name expected after %c", *(src-1)); | 1030 | bb_error_msg("file name expected after %c", *(src-1)); |
1029 | free_job(job); | 1031 | free_job(job); |
1030 | job->num_progs=0; | 1032 | job->num_progs = 0; |
1031 | return 1; | 1033 | return 1; |
1032 | } | 1034 | } |
1033 | 1035 | ||
@@ -1051,7 +1053,7 @@ static int parse_command(char **command_ptr, struct job *job, int *inbg) | |||
1051 | /* and start the next */ | 1053 | /* and start the next */ |
1052 | job->num_progs++; | 1054 | job->num_progs++; |
1053 | job->progs = xrealloc(job->progs, | 1055 | job->progs = xrealloc(job->progs, |
1054 | sizeof(*job->progs) * job->num_progs); | 1056 | sizeof(*job->progs) * job->num_progs); |
1055 | prog = job->progs + (job->num_progs - 1); | 1057 | prog = job->progs + (job->num_progs - 1); |
1056 | prog->num_redirects = 0; | 1058 | prog->num_redirects = 0; |
1057 | prog->redirects = NULL; | 1059 | prog->redirects = NULL; |
@@ -1070,7 +1072,7 @@ static int parse_command(char **command_ptr, struct job *job, int *inbg) | |||
1070 | empty_command_in_pipe: | 1072 | empty_command_in_pipe: |
1071 | bb_error_msg("empty command in pipe"); | 1073 | bb_error_msg("empty command in pipe"); |
1072 | free_job(job); | 1074 | free_job(job); |
1073 | job->num_progs=0; | 1075 | job->num_progs = 0; |
1074 | return 1; | 1076 | return 1; |
1075 | } | 1077 | } |
1076 | src--; /* we'll ++ it at the end of the loop */ | 1078 | src--; /* we'll ++ it at the end of the loop */ |
@@ -1078,7 +1080,7 @@ empty_command_in_pipe: | |||
1078 | break; | 1080 | break; |
1079 | #endif | 1081 | #endif |
1080 | 1082 | ||
1081 | #ifdef CONFIG_LASH_JOB_CONTROL | 1083 | #if ENABLE_LASH_JOB_CONTROL |
1082 | case '&': /* background */ | 1084 | case '&': /* background */ |
1083 | *inbg = 1; | 1085 | *inbg = 1; |
1084 | /* fallthrough */ | 1086 | /* fallthrough */ |
@@ -1146,8 +1148,8 @@ static int pseudo_exec(struct child_prog *child) | |||
1146 | /* Check if the command matches any of the forking builtins. */ | 1148 | /* Check if the command matches any of the forking builtins. */ |
1147 | for (x = bltins_forking; x->cmd; x++) { | 1149 | for (x = bltins_forking; x->cmd; x++) { |
1148 | if (strcmp(child->argv[0], x->cmd) == 0) { | 1150 | if (strcmp(child->argv[0], x->cmd) == 0) { |
1149 | applet_name=x->cmd; | 1151 | applet_name = x->cmd; |
1150 | _exit (x->function(child)); | 1152 | _exit(x->function(child)); |
1151 | } | 1153 | } |
1152 | } | 1154 | } |
1153 | 1155 | ||
@@ -1162,12 +1164,11 @@ static int pseudo_exec(struct child_prog *child) | |||
1162 | * /bin/foo invocation will fork and exec /bin/foo, even if | 1164 | * /bin/foo invocation will fork and exec /bin/foo, even if |
1163 | * /bin/foo is a symlink to busybox. | 1165 | * /bin/foo is a symlink to busybox. |
1164 | */ | 1166 | */ |
1165 | |||
1166 | if (ENABLE_FEATURE_SH_STANDALONE_SHELL) { | 1167 | if (ENABLE_FEATURE_SH_STANDALONE_SHELL) { |
1167 | char **argv_l = child->argv; | 1168 | char **argv_l = child->argv; |
1168 | int argc_l; | 1169 | int argc_l; |
1169 | 1170 | ||
1170 | for (argc_l=0; *argv_l; argv_l++, argc_l++); | 1171 | for (argc_l = 0; *argv_l; argv_l++, argc_l++); |
1171 | optind = 1; | 1172 | optind = 1; |
1172 | run_applet_by_name(child->argv[0], argc_l, child->argv); | 1173 | run_applet_by_name(child->argv[0], argc_l, child->argv); |
1173 | } | 1174 | } |
@@ -1183,7 +1184,7 @@ static int pseudo_exec(struct child_prog *child) | |||
1183 | static void insert_job(struct job *newjob, int inbg) | 1184 | static void insert_job(struct job *newjob, int inbg) |
1184 | { | 1185 | { |
1185 | struct job *thejob; | 1186 | struct job *thejob; |
1186 | struct jobset *j_list=newjob->job_list; | 1187 | struct jobset *j_list = newjob->job_list; |
1187 | 1188 | ||
1188 | /* find the ID for thejob to use */ | 1189 | /* find the ID for thejob to use */ |
1189 | newjob->jobid = 1; | 1190 | newjob->jobid = 1; |
@@ -1205,14 +1206,14 @@ static void insert_job(struct job *newjob, int inbg) | |||
1205 | thejob->running_progs = thejob->num_progs; | 1206 | thejob->running_progs = thejob->num_progs; |
1206 | thejob->stopped_progs = 0; | 1207 | thejob->stopped_progs = 0; |
1207 | 1208 | ||
1208 | #ifdef CONFIG_LASH_JOB_CONTROL | 1209 | #if ENABLE_LASH_JOB_CONTROL |
1209 | if (inbg) { | 1210 | if (inbg) { |
1210 | /* we don't wait for background thejobs to return -- append it | 1211 | /* we don't wait for background thejobs to return -- append it |
1211 | to the list of backgrounded thejobs and leave it alone */ | 1212 | to the list of backgrounded thejobs and leave it alone */ |
1212 | printf("[%d] %d\n", thejob->jobid, | 1213 | printf("[%d] %d\n", thejob->jobid, |
1213 | newjob->progs[newjob->num_progs - 1].pid); | 1214 | newjob->progs[newjob->num_progs - 1].pid); |
1214 | last_jobid = newjob->jobid; | 1215 | last_jobid = newjob->jobid; |
1215 | last_bg_pid=newjob->progs[newjob->num_progs - 1].pid; | 1216 | last_bg_pid = newjob->progs[newjob->num_progs - 1].pid; |
1216 | } else { | 1217 | } else { |
1217 | newjob->job_list->fg = thejob; | 1218 | newjob->job_list->fg = thejob; |
1218 | 1219 | ||
@@ -1237,10 +1238,11 @@ static int run_command(struct job *newjob, int inbg, int outpipe[2]) | |||
1237 | child = & (newjob->progs[i]); | 1238 | child = & (newjob->progs[i]); |
1238 | 1239 | ||
1239 | if ((i + 1) < newjob->num_progs) { | 1240 | if ((i + 1) < newjob->num_progs) { |
1240 | if (pipe(pipefds)<0) bb_perror_msg_and_die("pipe"); | 1241 | if (pipe(pipefds) < 0) |
1242 | bb_perror_msg_and_die("pipe"); | ||
1241 | nextout = pipefds[1]; | 1243 | nextout = pipefds[1]; |
1242 | } else { | 1244 | } else { |
1243 | if (outpipe[1]!=-1) { | 1245 | if (outpipe[1] != -1) { |
1244 | nextout = outpipe[1]; | 1246 | nextout = outpipe[1]; |
1245 | } else { | 1247 | } else { |
1246 | nextout = 1; | 1248 | nextout = 1; |
@@ -1288,9 +1290,10 @@ static int run_command(struct job *newjob, int inbg, int outpipe[2]) | |||
1288 | signal(SIGCHLD, SIG_DFL); | 1290 | signal(SIGCHLD, SIG_DFL); |
1289 | 1291 | ||
1290 | /* Close all open filehandles. */ | 1292 | /* Close all open filehandles. */ |
1291 | while(close_me_list) close((long)llist_pop(&close_me_list)); | 1293 | while (close_me_list) |
1294 | close((long)llist_pop(&close_me_list)); | ||
1292 | 1295 | ||
1293 | if (outpipe[1]!=-1) { | 1296 | if (outpipe[1] != -1) { |
1294 | close(outpipe[0]); | 1297 | close(outpipe[0]); |
1295 | } | 1298 | } |
1296 | if (nextin != 0) { | 1299 | if (nextin != 0) { |
@@ -1310,7 +1313,7 @@ static int run_command(struct job *newjob, int inbg, int outpipe[2]) | |||
1310 | 1313 | ||
1311 | pseudo_exec(child); | 1314 | pseudo_exec(child); |
1312 | } | 1315 | } |
1313 | if (outpipe[1]!=-1) { | 1316 | if (outpipe[1] != -1) { |
1314 | close(outpipe[1]); | 1317 | close(outpipe[1]); |
1315 | } | 1318 | } |
1316 | 1319 | ||
@@ -1342,7 +1345,7 @@ static int busy_loop(FILE * input) | |||
1342 | int i; | 1345 | int i; |
1343 | int inbg = 0; | 1346 | int inbg = 0; |
1344 | int status; | 1347 | int status; |
1345 | #ifdef CONFIG_LASH_JOB_CONTROL | 1348 | #if ENABLE_LASH_JOB_CONTROL |
1346 | pid_t parent_pgrp; | 1349 | pid_t parent_pgrp; |
1347 | /* save current owner of TTY so we can restore it on exit */ | 1350 | /* save current owner of TTY so we can restore it on exit */ |
1348 | parent_pgrp = tcgetpgrp(shell_terminal); | 1351 | parent_pgrp = tcgetpgrp(shell_terminal); |
@@ -1374,8 +1377,8 @@ static int busy_loop(FILE * input) | |||
1374 | 1377 | ||
1375 | if (!parse_command(&next_command, &newjob, &inbg) && | 1378 | if (!parse_command(&next_command, &newjob, &inbg) && |
1376 | newjob.num_progs) { | 1379 | newjob.num_progs) { |
1377 | int pipefds[2] = {-1,-1}; | 1380 | int pipefds[2] = { -1, -1 }; |
1378 | debug_printf( "job=%p fed to run_command by busy_loop()'\n", | 1381 | debug_printf("job=%p fed to run_command by busy_loop()'\n", |
1379 | &newjob); | 1382 | &newjob); |
1380 | run_command(&newjob, inbg, pipefds); | 1383 | run_command(&newjob, inbg, pipefds); |
1381 | } | 1384 | } |
@@ -1390,9 +1393,9 @@ static int busy_loop(FILE * input) | |||
1390 | while (!job_list.fg->progs[i].pid || | 1393 | while (!job_list.fg->progs[i].pid || |
1391 | job_list.fg->progs[i].is_stopped == 1) i++; | 1394 | job_list.fg->progs[i].is_stopped == 1) i++; |
1392 | 1395 | ||
1393 | if (waitpid(job_list.fg->progs[i].pid, &status, WUNTRACED)<0) { | 1396 | if (waitpid(job_list.fg->progs[i].pid, &status, WUNTRACED) < 0) { |
1394 | if (errno != ECHILD) { | 1397 | if (errno != ECHILD) { |
1395 | bb_perror_msg_and_die("waitpid(%d)",job_list.fg->progs[i].pid); | 1398 | bb_perror_msg_and_die("waitpid(%d)", job_list.fg->progs[i].pid); |
1396 | } | 1399 | } |
1397 | } | 1400 | } |
1398 | 1401 | ||
@@ -1401,7 +1404,7 @@ static int busy_loop(FILE * input) | |||
1401 | job_list.fg->running_progs--; | 1404 | job_list.fg->running_progs--; |
1402 | job_list.fg->progs[i].pid = 0; | 1405 | job_list.fg->progs[i].pid = 0; |
1403 | 1406 | ||
1404 | last_return_code=WEXITSTATUS(status); | 1407 | last_return_code = WEXITSTATUS(status); |
1405 | 1408 | ||
1406 | if (!job_list.fg->running_progs) { | 1409 | if (!job_list.fg->running_progs) { |
1407 | /* child exited */ | 1410 | /* child exited */ |
@@ -1409,7 +1412,7 @@ static int busy_loop(FILE * input) | |||
1409 | job_list.fg = NULL; | 1412 | job_list.fg = NULL; |
1410 | } | 1413 | } |
1411 | } | 1414 | } |
1412 | #ifdef CONFIG_LASH_JOB_CONTROL | 1415 | #if ENABLE_LASH_JOB_CONTROL |
1413 | else { | 1416 | else { |
1414 | /* the child was stopped */ | 1417 | /* the child was stopped */ |
1415 | job_list.fg->stopped_progs++; | 1418 | job_list.fg->stopped_progs++; |
@@ -1433,7 +1436,7 @@ static int busy_loop(FILE * input) | |||
1433 | } | 1436 | } |
1434 | free(command); | 1437 | free(command); |
1435 | 1438 | ||
1436 | #ifdef CONFIG_LASH_JOB_CONTROL | 1439 | #if ENABLE_LASH_JOB_CONTROL |
1437 | /* return controlling TTY back to parent process group before exiting */ | 1440 | /* return controlling TTY back to parent process group before exiting */ |
1438 | if (tcsetpgrp(shell_terminal, parent_pgrp) && errno != ENOTTY) | 1441 | if (tcsetpgrp(shell_terminal, parent_pgrp) && errno != ENOTTY) |
1439 | bb_perror_msg("tcsetpgrp"); | 1442 | bb_perror_msg("tcsetpgrp"); |
@@ -1446,10 +1449,10 @@ static int busy_loop(FILE * input) | |||
1446 | return 0; | 1449 | return 0; |
1447 | } | 1450 | } |
1448 | 1451 | ||
1449 | #ifdef CONFIG_FEATURE_CLEAN_UP | 1452 | #if ENABLE_FEATURE_CLEAN_UP |
1450 | static void free_memory(void) | 1453 | static void free_memory(void) |
1451 | { | 1454 | { |
1452 | if (cwd && cwd!=bb_msg_unknown) { | 1455 | if (cwd && cwd != bb_msg_unknown) { |
1453 | free((char*)cwd); | 1456 | free((char*)cwd); |
1454 | } | 1457 | } |
1455 | if (local_pending_command) | 1458 | if (local_pending_command) |
@@ -1463,7 +1466,7 @@ static void free_memory(void) | |||
1463 | void free_memory(void); | 1466 | void free_memory(void); |
1464 | #endif | 1467 | #endif |
1465 | 1468 | ||
1466 | #ifdef CONFIG_LASH_JOB_CONTROL | 1469 | #if ENABLE_LASH_JOB_CONTROL |
1467 | /* Make sure we have a controlling tty. If we get started under a job | 1470 | /* Make sure we have a controlling tty. If we get started under a job |
1468 | * aware app (like bash for example), make sure we are now in charge so | 1471 | * aware app (like bash for example), make sure we are now in charge so |
1469 | * we don't fight over who gets the foreground */ | 1472 | * we don't fight over who gets the foreground */ |
@@ -1519,7 +1522,7 @@ int lash_main(int argc_l, char **argv_l) | |||
1519 | close_me_list = NULL; | 1522 | close_me_list = NULL; |
1520 | job_list.head = NULL; | 1523 | job_list.head = NULL; |
1521 | job_list.fg = NULL; | 1524 | job_list.fg = NULL; |
1522 | last_return_code=1; | 1525 | last_return_code = 1; |
1523 | 1526 | ||
1524 | if (argv[0] && argv[0][0] == '-') { | 1527 | if (argv[0] && argv[0][0] == '-') { |
1525 | FILE *prof_input; | 1528 | FILE *prof_input; |
@@ -1548,9 +1551,9 @@ int lash_main(int argc_l, char **argv_l) | |||
1548 | * standard input is a terminal | 1551 | * standard input is a terminal |
1549 | * standard output is a terminal | 1552 | * standard output is a terminal |
1550 | * Refer to Posix.2, the description of the `sh' utility. */ | 1553 | * Refer to Posix.2, the description of the `sh' utility. */ |
1551 | if (argv[optind]==NULL && input==stdin && | 1554 | if (argv[optind] == NULL && input == stdin |
1552 | isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) | 1555 | && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) |
1553 | { | 1556 | ) { |
1554 | opt |= LASH_OPT_i; | 1557 | opt |= LASH_OPT_i; |
1555 | } | 1558 | } |
1556 | setup_job_control(); | 1559 | setup_job_control(); |