diff options
author | Eric Andersen <andersen@codepoet.org> | 2001-03-17 00:20:10 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 2001-03-17 00:20:10 +0000 |
commit | 341744781a3090774fe9dc8ef788a16e6c793c7b (patch) | |
tree | 765409c7b9158086e46ff854d11b8caad2137fad | |
parent | 1a6f8cfadf9416a9a74a03468fd4c201e532184b (diff) | |
download | busybox-w32-341744781a3090774fe9dc8ef788a16e6c793c7b.tar.gz busybox-w32-341744781a3090774fe9dc8ef788a16e6c793c7b.tar.bz2 busybox-w32-341744781a3090774fe9dc8ef788a16e6c793c7b.zip |
Patch from Larry and Vladimir to clean up sh.c and fix
shell expansion to work in non POSIX locales.
-rw-r--r-- | lash.c | 30 | ||||
-rw-r--r-- | sh.c | 30 | ||||
-rw-r--r-- | shell/lash.c | 30 |
3 files changed, 36 insertions, 54 deletions
@@ -65,9 +65,9 @@ | |||
65 | #include <unistd.h> | 65 | #include <unistd.h> |
66 | #include <getopt.h> | 66 | #include <getopt.h> |
67 | 67 | ||
68 | #undef BB_FEATURE_SH_WORDEXP | 68 | //#define BB_FEATURE_SH_WORDEXP |
69 | 69 | ||
70 | #if BB_FEATURE_SH_WORDEXP | 70 | #ifdef BB_FEATURE_SH_WORDEXP |
71 | #include <wordexp.h> | 71 | #include <wordexp.h> |
72 | #define expand_t wordexp_t | 72 | #define expand_t wordexp_t |
73 | #undef BB_FEATURE_SH_BACKTICKS | 73 | #undef BB_FEATURE_SH_BACKTICKS |
@@ -881,9 +881,6 @@ static int get_command(FILE * source, char *command) | |||
881 | return 1; | 881 | return 1; |
882 | } | 882 | } |
883 | 883 | ||
884 | /* remove trailing newline */ | ||
885 | chomp(command); | ||
886 | |||
887 | return 0; | 884 | return 0; |
888 | } | 885 | } |
889 | 886 | ||
@@ -962,8 +959,8 @@ static int expand_arguments(char *command) | |||
962 | while( command && command[index]) { | 959 | while( command && command[index]) { |
963 | if (command[index] == '\\') { | 960 | if (command[index] == '\\') { |
964 | char *tmp = command+index+1; | 961 | char *tmp = command+index+1; |
965 | command[index+1] = process_escape_sequence( &tmp ); | 962 | command[index] = process_escape_sequence( &tmp ); |
966 | memmove(command+index, command+index+1, strlen(command+index)); | 963 | memmove(command+index + 1, tmp, strlen(tmp)+1); |
967 | } | 964 | } |
968 | index++; | 965 | index++; |
969 | } | 966 | } |
@@ -971,7 +968,7 @@ static int expand_arguments(char *command) | |||
971 | #ifdef BB_FEATURE_SH_ENVIRONMENT | 968 | #ifdef BB_FEATURE_SH_ENVIRONMENT |
972 | 969 | ||
973 | 970 | ||
974 | #if BB_FEATURE_SH_WORDEXP | 971 | #ifdef BB_FEATURE_SH_WORDEXP |
975 | /* This first part uses wordexp() which is a wonderful C lib | 972 | /* This first part uses wordexp() which is a wonderful C lib |
976 | * function which expands nearly everything. */ | 973 | * function which expands nearly everything. */ |
977 | retval = wordexp (command, &expand_result, WRDE_SHOWERR); | 974 | retval = wordexp (command, &expand_result, WRDE_SHOWERR); |
@@ -1116,25 +1113,24 @@ static int expand_arguments(char *command) | |||
1116 | } else { | 1113 | } else { |
1117 | /* Looks like an environment variable */ | 1114 | /* Looks like an environment variable */ |
1118 | char delim_hold; | 1115 | char delim_hold; |
1119 | int num_skip_chars=1; | 1116 | int num_skip_chars=0; |
1120 | int dstlen = strlen(dst); | 1117 | int dstlen = strlen(dst); |
1121 | /* Is this a ${foo} type variable? */ | 1118 | /* Is this a ${foo} type variable? */ |
1122 | if (dstlen >=2 && *(dst+1) == '{') { | 1119 | if (dstlen >=2 && *(dst+1) == '{') { |
1123 | src=strchr(dst+1, '}'); | 1120 | src=strchr(dst+1, '}'); |
1124 | num_skip_chars=2; | 1121 | num_skip_chars=1; |
1125 | } else { | 1122 | } else { |
1126 | src=strpbrk(dst+1, " \t~`!$^&*()=|\\[];\"'<>?./"); | 1123 | src=dst+1; |
1124 | while(isalnum(*src) || *src=='_') src++; | ||
1127 | } | 1125 | } |
1128 | if (src == NULL) { | 1126 | if (src == NULL) { |
1129 | src = dst+dstlen; | 1127 | src = dst+dstlen; |
1130 | } | 1128 | } |
1131 | delim_hold=*src; | 1129 | delim_hold=*src; |
1132 | *src='\0'; /* temporary */ | 1130 | *src='\0'; /* temporary */ |
1133 | var = getenv(dst + num_skip_chars); | 1131 | var = getenv(dst + 1 + num_skip_chars); |
1134 | *src=delim_hold; | 1132 | *src=delim_hold; |
1135 | if (num_skip_chars==2) { | 1133 | src += num_skip_chars; |
1136 | src++; | ||
1137 | } | ||
1138 | } | 1134 | } |
1139 | if (var == NULL) { | 1135 | if (var == NULL) { |
1140 | /* Seems we got an un-expandable variable. So delete it. */ | 1136 | /* Seems we got an un-expandable variable. So delete it. */ |
@@ -1149,8 +1145,7 @@ static int expand_arguments(char *command) | |||
1149 | } | 1145 | } |
1150 | /* Move stuff to the end of the string to accommodate | 1146 | /* Move stuff to the end of the string to accommodate |
1151 | * filling the created gap with the new stuff */ | 1147 | * filling the created gap with the new stuff */ |
1152 | memmove(dst+subst_len, src, trail_len); | 1148 | memmove(dst+subst_len, src, trail_len+1); |
1153 | *(dst+subst_len+trail_len)='\0'; | ||
1154 | /* Now copy in the new stuff */ | 1149 | /* Now copy in the new stuff */ |
1155 | memcpy(dst, var, subst_len); | 1150 | memcpy(dst, var, subst_len); |
1156 | src = dst+subst_len; | 1151 | src = dst+subst_len; |
@@ -1475,7 +1470,6 @@ static int parse_command(char **command_ptr, struct job *job, int *inbg) | |||
1475 | break; | 1470 | break; |
1476 | #else | 1471 | #else |
1477 | error_msg("character expected after \\"); | 1472 | error_msg("character expected after \\"); |
1478 | free(command); | ||
1479 | free_job(job); | 1473 | free_job(job); |
1480 | return 1; | 1474 | return 1; |
1481 | #endif | 1475 | #endif |
@@ -65,9 +65,9 @@ | |||
65 | #include <unistd.h> | 65 | #include <unistd.h> |
66 | #include <getopt.h> | 66 | #include <getopt.h> |
67 | 67 | ||
68 | #undef BB_FEATURE_SH_WORDEXP | 68 | //#define BB_FEATURE_SH_WORDEXP |
69 | 69 | ||
70 | #if BB_FEATURE_SH_WORDEXP | 70 | #ifdef BB_FEATURE_SH_WORDEXP |
71 | #include <wordexp.h> | 71 | #include <wordexp.h> |
72 | #define expand_t wordexp_t | 72 | #define expand_t wordexp_t |
73 | #undef BB_FEATURE_SH_BACKTICKS | 73 | #undef BB_FEATURE_SH_BACKTICKS |
@@ -881,9 +881,6 @@ static int get_command(FILE * source, char *command) | |||
881 | return 1; | 881 | return 1; |
882 | } | 882 | } |
883 | 883 | ||
884 | /* remove trailing newline */ | ||
885 | chomp(command); | ||
886 | |||
887 | return 0; | 884 | return 0; |
888 | } | 885 | } |
889 | 886 | ||
@@ -962,8 +959,8 @@ static int expand_arguments(char *command) | |||
962 | while( command && command[index]) { | 959 | while( command && command[index]) { |
963 | if (command[index] == '\\') { | 960 | if (command[index] == '\\') { |
964 | char *tmp = command+index+1; | 961 | char *tmp = command+index+1; |
965 | command[index+1] = process_escape_sequence( &tmp ); | 962 | command[index] = process_escape_sequence( &tmp ); |
966 | memmove(command+index, command+index+1, strlen(command+index)); | 963 | memmove(command+index + 1, tmp, strlen(tmp)+1); |
967 | } | 964 | } |
968 | index++; | 965 | index++; |
969 | } | 966 | } |
@@ -971,7 +968,7 @@ static int expand_arguments(char *command) | |||
971 | #ifdef BB_FEATURE_SH_ENVIRONMENT | 968 | #ifdef BB_FEATURE_SH_ENVIRONMENT |
972 | 969 | ||
973 | 970 | ||
974 | #if BB_FEATURE_SH_WORDEXP | 971 | #ifdef BB_FEATURE_SH_WORDEXP |
975 | /* This first part uses wordexp() which is a wonderful C lib | 972 | /* This first part uses wordexp() which is a wonderful C lib |
976 | * function which expands nearly everything. */ | 973 | * function which expands nearly everything. */ |
977 | retval = wordexp (command, &expand_result, WRDE_SHOWERR); | 974 | retval = wordexp (command, &expand_result, WRDE_SHOWERR); |
@@ -1116,25 +1113,24 @@ static int expand_arguments(char *command) | |||
1116 | } else { | 1113 | } else { |
1117 | /* Looks like an environment variable */ | 1114 | /* Looks like an environment variable */ |
1118 | char delim_hold; | 1115 | char delim_hold; |
1119 | int num_skip_chars=1; | 1116 | int num_skip_chars=0; |
1120 | int dstlen = strlen(dst); | 1117 | int dstlen = strlen(dst); |
1121 | /* Is this a ${foo} type variable? */ | 1118 | /* Is this a ${foo} type variable? */ |
1122 | if (dstlen >=2 && *(dst+1) == '{') { | 1119 | if (dstlen >=2 && *(dst+1) == '{') { |
1123 | src=strchr(dst+1, '}'); | 1120 | src=strchr(dst+1, '}'); |
1124 | num_skip_chars=2; | 1121 | num_skip_chars=1; |
1125 | } else { | 1122 | } else { |
1126 | src=strpbrk(dst+1, " \t~`!$^&*()=|\\[];\"'<>?./"); | 1123 | src=dst+1; |
1124 | while(isalnum(*src) || *src=='_') src++; | ||
1127 | } | 1125 | } |
1128 | if (src == NULL) { | 1126 | if (src == NULL) { |
1129 | src = dst+dstlen; | 1127 | src = dst+dstlen; |
1130 | } | 1128 | } |
1131 | delim_hold=*src; | 1129 | delim_hold=*src; |
1132 | *src='\0'; /* temporary */ | 1130 | *src='\0'; /* temporary */ |
1133 | var = getenv(dst + num_skip_chars); | 1131 | var = getenv(dst + 1 + num_skip_chars); |
1134 | *src=delim_hold; | 1132 | *src=delim_hold; |
1135 | if (num_skip_chars==2) { | 1133 | src += num_skip_chars; |
1136 | src++; | ||
1137 | } | ||
1138 | } | 1134 | } |
1139 | if (var == NULL) { | 1135 | if (var == NULL) { |
1140 | /* Seems we got an un-expandable variable. So delete it. */ | 1136 | /* Seems we got an un-expandable variable. So delete it. */ |
@@ -1149,8 +1145,7 @@ static int expand_arguments(char *command) | |||
1149 | } | 1145 | } |
1150 | /* Move stuff to the end of the string to accommodate | 1146 | /* Move stuff to the end of the string to accommodate |
1151 | * filling the created gap with the new stuff */ | 1147 | * filling the created gap with the new stuff */ |
1152 | memmove(dst+subst_len, src, trail_len); | 1148 | memmove(dst+subst_len, src, trail_len+1); |
1153 | *(dst+subst_len+trail_len)='\0'; | ||
1154 | /* Now copy in the new stuff */ | 1149 | /* Now copy in the new stuff */ |
1155 | memcpy(dst, var, subst_len); | 1150 | memcpy(dst, var, subst_len); |
1156 | src = dst+subst_len; | 1151 | src = dst+subst_len; |
@@ -1475,7 +1470,6 @@ static int parse_command(char **command_ptr, struct job *job, int *inbg) | |||
1475 | break; | 1470 | break; |
1476 | #else | 1471 | #else |
1477 | error_msg("character expected after \\"); | 1472 | error_msg("character expected after \\"); |
1478 | free(command); | ||
1479 | free_job(job); | 1473 | free_job(job); |
1480 | return 1; | 1474 | return 1; |
1481 | #endif | 1475 | #endif |
diff --git a/shell/lash.c b/shell/lash.c index 6fedfe8dd..8727e12ae 100644 --- a/shell/lash.c +++ b/shell/lash.c | |||
@@ -65,9 +65,9 @@ | |||
65 | #include <unistd.h> | 65 | #include <unistd.h> |
66 | #include <getopt.h> | 66 | #include <getopt.h> |
67 | 67 | ||
68 | #undef BB_FEATURE_SH_WORDEXP | 68 | //#define BB_FEATURE_SH_WORDEXP |
69 | 69 | ||
70 | #if BB_FEATURE_SH_WORDEXP | 70 | #ifdef BB_FEATURE_SH_WORDEXP |
71 | #include <wordexp.h> | 71 | #include <wordexp.h> |
72 | #define expand_t wordexp_t | 72 | #define expand_t wordexp_t |
73 | #undef BB_FEATURE_SH_BACKTICKS | 73 | #undef BB_FEATURE_SH_BACKTICKS |
@@ -881,9 +881,6 @@ static int get_command(FILE * source, char *command) | |||
881 | return 1; | 881 | return 1; |
882 | } | 882 | } |
883 | 883 | ||
884 | /* remove trailing newline */ | ||
885 | chomp(command); | ||
886 | |||
887 | return 0; | 884 | return 0; |
888 | } | 885 | } |
889 | 886 | ||
@@ -962,8 +959,8 @@ static int expand_arguments(char *command) | |||
962 | while( command && command[index]) { | 959 | while( command && command[index]) { |
963 | if (command[index] == '\\') { | 960 | if (command[index] == '\\') { |
964 | char *tmp = command+index+1; | 961 | char *tmp = command+index+1; |
965 | command[index+1] = process_escape_sequence( &tmp ); | 962 | command[index] = process_escape_sequence( &tmp ); |
966 | memmove(command+index, command+index+1, strlen(command+index)); | 963 | memmove(command+index + 1, tmp, strlen(tmp)+1); |
967 | } | 964 | } |
968 | index++; | 965 | index++; |
969 | } | 966 | } |
@@ -971,7 +968,7 @@ static int expand_arguments(char *command) | |||
971 | #ifdef BB_FEATURE_SH_ENVIRONMENT | 968 | #ifdef BB_FEATURE_SH_ENVIRONMENT |
972 | 969 | ||
973 | 970 | ||
974 | #if BB_FEATURE_SH_WORDEXP | 971 | #ifdef BB_FEATURE_SH_WORDEXP |
975 | /* This first part uses wordexp() which is a wonderful C lib | 972 | /* This first part uses wordexp() which is a wonderful C lib |
976 | * function which expands nearly everything. */ | 973 | * function which expands nearly everything. */ |
977 | retval = wordexp (command, &expand_result, WRDE_SHOWERR); | 974 | retval = wordexp (command, &expand_result, WRDE_SHOWERR); |
@@ -1116,25 +1113,24 @@ static int expand_arguments(char *command) | |||
1116 | } else { | 1113 | } else { |
1117 | /* Looks like an environment variable */ | 1114 | /* Looks like an environment variable */ |
1118 | char delim_hold; | 1115 | char delim_hold; |
1119 | int num_skip_chars=1; | 1116 | int num_skip_chars=0; |
1120 | int dstlen = strlen(dst); | 1117 | int dstlen = strlen(dst); |
1121 | /* Is this a ${foo} type variable? */ | 1118 | /* Is this a ${foo} type variable? */ |
1122 | if (dstlen >=2 && *(dst+1) == '{') { | 1119 | if (dstlen >=2 && *(dst+1) == '{') { |
1123 | src=strchr(dst+1, '}'); | 1120 | src=strchr(dst+1, '}'); |
1124 | num_skip_chars=2; | 1121 | num_skip_chars=1; |
1125 | } else { | 1122 | } else { |
1126 | src=strpbrk(dst+1, " \t~`!$^&*()=|\\[];\"'<>?./"); | 1123 | src=dst+1; |
1124 | while(isalnum(*src) || *src=='_') src++; | ||
1127 | } | 1125 | } |
1128 | if (src == NULL) { | 1126 | if (src == NULL) { |
1129 | src = dst+dstlen; | 1127 | src = dst+dstlen; |
1130 | } | 1128 | } |
1131 | delim_hold=*src; | 1129 | delim_hold=*src; |
1132 | *src='\0'; /* temporary */ | 1130 | *src='\0'; /* temporary */ |
1133 | var = getenv(dst + num_skip_chars); | 1131 | var = getenv(dst + 1 + num_skip_chars); |
1134 | *src=delim_hold; | 1132 | *src=delim_hold; |
1135 | if (num_skip_chars==2) { | 1133 | src += num_skip_chars; |
1136 | src++; | ||
1137 | } | ||
1138 | } | 1134 | } |
1139 | if (var == NULL) { | 1135 | if (var == NULL) { |
1140 | /* Seems we got an un-expandable variable. So delete it. */ | 1136 | /* Seems we got an un-expandable variable. So delete it. */ |
@@ -1149,8 +1145,7 @@ static int expand_arguments(char *command) | |||
1149 | } | 1145 | } |
1150 | /* Move stuff to the end of the string to accommodate | 1146 | /* Move stuff to the end of the string to accommodate |
1151 | * filling the created gap with the new stuff */ | 1147 | * filling the created gap with the new stuff */ |
1152 | memmove(dst+subst_len, src, trail_len); | 1148 | memmove(dst+subst_len, src, trail_len+1); |
1153 | *(dst+subst_len+trail_len)='\0'; | ||
1154 | /* Now copy in the new stuff */ | 1149 | /* Now copy in the new stuff */ |
1155 | memcpy(dst, var, subst_len); | 1150 | memcpy(dst, var, subst_len); |
1156 | src = dst+subst_len; | 1151 | src = dst+subst_len; |
@@ -1475,7 +1470,6 @@ static int parse_command(char **command_ptr, struct job *job, int *inbg) | |||
1475 | break; | 1470 | break; |
1476 | #else | 1471 | #else |
1477 | error_msg("character expected after \\"); | 1472 | error_msg("character expected after \\"); |
1478 | free(command); | ||
1479 | free_job(job); | 1473 | free_job(job); |
1480 | return 1; | 1474 | return 1; |
1481 | #endif | 1475 | #endif |