diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-01-30 22:32:09 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-01-30 22:32:09 +0000 |
commit | bb81c5831aa81a9e0ef793681e21ea2352bc6f57 (patch) | |
tree | 37228bb318dc43248180739fe1ca73fbf447f6a4 | |
parent | c72c1ed93287d5a076ae7c74427959cea6274861 (diff) | |
download | busybox-w32-bb81c5831aa81a9e0ef793681e21ea2352bc6f57.tar.gz busybox-w32-bb81c5831aa81a9e0ef793681e21ea2352bc6f57.tar.bz2 busybox-w32-bb81c5831aa81a9e0ef793681e21ea2352bc6f57.zip |
hush: style fixes
-rw-r--r-- | shell/hush.c | 194 |
1 files changed, 105 insertions, 89 deletions
diff --git a/shell/hush.c b/shell/hush.c index 6d05c064b..7bc3b9a1e 100644 --- a/shell/hush.c +++ b/shell/hush.c | |||
@@ -583,7 +583,8 @@ static int builtin_fg_bg(struct child_prog *child) | |||
583 | for (i = 0; i < pi->num_progs; i++) | 583 | for (i = 0; i < pi->num_progs; i++) |
584 | pi->progs[i].is_stopped = 0; | 584 | pi->progs[i].is_stopped = 0; |
585 | 585 | ||
586 | if ( (i=kill(- pi->pgrp, SIGCONT)) < 0) { | 586 | i = kill(- pi->pgrp, SIGCONT); |
587 | if (i < 0) { | ||
587 | if (i == ESRCH) { | 588 | if (i == ESRCH) { |
588 | remove_bg_job(pi); | 589 | remove_bg_job(pi); |
589 | } else { | 590 | } else { |
@@ -831,7 +832,7 @@ static int b_adduint(o_string *o, unsigned i) | |||
831 | int r; | 832 | int r; |
832 | char *p = simple_itoa(i); | 833 | char *p = simple_itoa(i); |
833 | /* no escape checking necessary */ | 834 | /* no escape checking necessary */ |
834 | do r = b_addchr(o, *p++); while (r==0 && *p); | 835 | do r = b_addchr(o, *p++); while (r == 0 && *p); |
835 | return r; | 836 | return r; |
836 | } | 837 | } |
837 | 838 | ||
@@ -972,8 +973,8 @@ static void setup_string_in_str(struct in_str *i, const char *s) | |||
972 | { | 973 | { |
973 | i->peek = static_peek; | 974 | i->peek = static_peek; |
974 | i->get = static_get; | 975 | i->get = static_get; |
975 | i->__promptme=1; | 976 | i->__promptme = 1; |
976 | i->promptmode=1; | 977 | i->promptmode = 1; |
977 | i->p = s; | 978 | i->p = s; |
978 | } | 979 | } |
979 | 980 | ||
@@ -1114,7 +1115,7 @@ static void pseudo_exec(struct child_prog *child) | |||
1114 | #if ENABLE_FEATURE_SH_STANDALONE_SHELL | 1115 | #if ENABLE_FEATURE_SH_STANDALONE_SHELL |
1115 | { | 1116 | { |
1116 | int argc_l; | 1117 | int argc_l; |
1117 | char** argv_l=child->argv; | 1118 | char** argv_l = child->argv; |
1118 | char *name = child->argv[0]; | 1119 | char *name = child->argv[0]; |
1119 | 1120 | ||
1120 | /* Count argc for use in a second... */ | 1121 | /* Count argc for use in a second... */ |
@@ -1430,7 +1431,7 @@ static int run_pipe_real(struct pipe *pi) | |||
1430 | dup2(nextout, 1); | 1431 | dup2(nextout, 1); |
1431 | close(nextout); | 1432 | close(nextout); |
1432 | } | 1433 | } |
1433 | if (pipefds[0]!=-1) { | 1434 | if (pipefds[0] != -1) { |
1434 | close(pipefds[0]); /* opposite end of our output pipe */ | 1435 | close(pipefds[0]); /* opposite end of our output pipe */ |
1435 | } | 1436 | } |
1436 | 1437 | ||
@@ -1438,7 +1439,7 @@ static int run_pipe_real(struct pipe *pi) | |||
1438 | * and the pipe fd is available for dup'ing. */ | 1439 | * and the pipe fd is available for dup'ing. */ |
1439 | setup_redirects(child,NULL); | 1440 | setup_redirects(child,NULL); |
1440 | 1441 | ||
1441 | if (interactive && pi->followup!=PIPE_BG) { | 1442 | if (interactive && pi->followup != PIPE_BG) { |
1442 | /* If we (the child) win the race, put ourselves in the process | 1443 | /* If we (the child) win the race, put ourselves in the process |
1443 | * group whose leader is the first process in this pipe. */ | 1444 | * group whose leader is the first process in this pipe. */ |
1444 | if (pi->pgrp < 0) { | 1445 | if (pi->pgrp < 0) { |
@@ -1789,9 +1790,9 @@ static const char *get_local_var(const char *s) | |||
1789 | } | 1790 | } |
1790 | 1791 | ||
1791 | /* This is used to set local shell variables | 1792 | /* This is used to set local shell variables |
1792 | flg_export==0 if only local (not exporting) variable | 1793 | flg_export == 0 if only local (not exporting) variable |
1793 | flg_export==1 if "new" exporting environ | 1794 | flg_export == 1 if "new" exporting environ |
1794 | flg_export>1 if current startup environ (not call putenv()) */ | 1795 | flg_export > 1 if current startup environ (not call putenv()) */ |
1795 | static int set_local_var(const char *s, int flg_export) | 1796 | static int set_local_var(const char *s, int flg_export) |
1796 | { | 1797 | { |
1797 | char *name, *value; | 1798 | char *name, *value; |
@@ -1817,7 +1818,7 @@ static int set_local_var(const char *s, int flg_export) | |||
1817 | 1818 | ||
1818 | if (cur) { | 1819 | if (cur) { |
1819 | if (strcmp(cur->value, value) == 0) { | 1820 | if (strcmp(cur->value, value) == 0) { |
1820 | if (flg_export>0 && cur->flg_export == 0) | 1821 | if (flg_export > 0 && cur->flg_export == 0) |
1821 | cur->flg_export = flg_export; | 1822 | cur->flg_export = flg_export; |
1822 | else | 1823 | else |
1823 | result++; | 1824 | result++; |
@@ -1826,7 +1827,7 @@ static int set_local_var(const char *s, int flg_export) | |||
1826 | bb_error_msg("%s: readonly variable", name); | 1827 | bb_error_msg("%s: readonly variable", name); |
1827 | result = -1; | 1828 | result = -1; |
1828 | } else { | 1829 | } else { |
1829 | if (flg_export > 0 || cur->flg_export>1) | 1830 | if (flg_export > 0 || cur->flg_export > 1) |
1830 | cur->flg_export = 1; | 1831 | cur->flg_export = 1; |
1831 | free((char*)cur->value); | 1832 | free((char*)cur->value); |
1832 | 1833 | ||
@@ -2024,14 +2025,14 @@ static int reserved_word(o_string *dest, struct p_context *ctx) | |||
2024 | } | 2025 | } |
2025 | *new = *ctx; /* physical copy */ | 2026 | *new = *ctx; /* physical copy */ |
2026 | initialize_context(ctx); | 2027 | initialize_context(ctx); |
2027 | ctx->stack=new; | 2028 | ctx->stack = new; |
2028 | } else if ( ctx->w == RES_NONE || ! (ctx->old_flag & (1<<r->code))) { | 2029 | } else if (ctx->w == RES_NONE || !(ctx->old_flag & (1 << r->code))) { |
2029 | syntax(); | 2030 | syntax(); |
2030 | ctx->w = RES_SNTX; | 2031 | ctx->w = RES_SNTX; |
2031 | b_reset(dest); | 2032 | b_reset(dest); |
2032 | return 1; | 2033 | return 1; |
2033 | } | 2034 | } |
2034 | ctx->w=r->code; | 2035 | ctx->w = r->code; |
2035 | ctx->old_flag = r->flag; | 2036 | ctx->old_flag = r->flag; |
2036 | if (ctx->old_flag & FLAG_END) { | 2037 | if (ctx->old_flag & FLAG_END) { |
2037 | struct p_context *old; | 2038 | struct p_context *old; |
@@ -2054,7 +2055,7 @@ static int reserved_word(o_string *dest, struct p_context *ctx) | |||
2054 | * Syntax or xglob errors return 1. */ | 2055 | * Syntax or xglob errors return 1. */ |
2055 | static int done_word(o_string *dest, struct p_context *ctx) | 2056 | static int done_word(o_string *dest, struct p_context *ctx) |
2056 | { | 2057 | { |
2057 | struct child_prog *child=ctx->child; | 2058 | struct child_prog *child = ctx->child; |
2058 | glob_t *glob_target; | 2059 | glob_t *glob_target; |
2059 | int gr, flags = 0; | 2060 | int gr, flags = 0; |
2060 | 2061 | ||
@@ -2072,7 +2073,8 @@ static int done_word(o_string *dest, struct p_context *ctx) | |||
2072 | } | 2073 | } |
2073 | if (!child->argv && (ctx->type & FLAG_PARSE_SEMICOLON)) { | 2074 | if (!child->argv && (ctx->type & FLAG_PARSE_SEMICOLON)) { |
2074 | debug_printf("checking %s for reserved-ness\n",dest->data); | 2075 | debug_printf("checking %s for reserved-ness\n",dest->data); |
2075 | if (reserved_word(dest,ctx)) return ctx->w==RES_SNTX; | 2076 | if (reserved_word(dest,ctx)) |
2077 | return (ctx->w == RES_SNTX); | ||
2076 | } | 2078 | } |
2077 | glob_target = &child->glob_result; | 2079 | glob_target = &child->glob_result; |
2078 | if (child->argv) flags |= GLOB_APPEND; | 2080 | if (child->argv) flags |= GLOB_APPEND; |
@@ -2082,7 +2084,7 @@ static int done_word(o_string *dest, struct p_context *ctx) | |||
2082 | 2084 | ||
2083 | b_reset(dest); | 2085 | b_reset(dest); |
2084 | if (ctx->pending_redirect) { | 2086 | if (ctx->pending_redirect) { |
2085 | ctx->pending_redirect=NULL; | 2087 | ctx->pending_redirect = NULL; |
2086 | if (glob_target->gl_pathc != 1) { | 2088 | if (glob_target->gl_pathc != 1) { |
2087 | bb_error_msg("ambiguous redirect"); | 2089 | bb_error_msg("ambiguous redirect"); |
2088 | return 1; | 2090 | return 1; |
@@ -2106,8 +2108,8 @@ static int done_command(struct p_context *ctx) | |||
2106 | * Only real trickiness here is that the uncommitted | 2108 | * Only real trickiness here is that the uncommitted |
2107 | * child structure, to which ctx->child points, is not | 2109 | * child structure, to which ctx->child points, is not |
2108 | * counted in pi->num_progs. */ | 2110 | * counted in pi->num_progs. */ |
2109 | struct pipe *pi=ctx->pipe; | 2111 | struct pipe *pi = ctx->pipe; |
2110 | struct child_prog *prog=ctx->child; | 2112 | struct child_prog *prog = ctx->child; |
2111 | 2113 | ||
2112 | if (prog && prog->group == NULL | 2114 | if (prog && prog->group == NULL |
2113 | && prog->argv == NULL | 2115 | && prog->argv == NULL |
@@ -2144,7 +2146,7 @@ static int done_pipe(struct p_context *ctx, pipe_style type) | |||
2144 | debug_printf("done_pipe, type %d\n", type); | 2146 | debug_printf("done_pipe, type %d\n", type); |
2145 | ctx->pipe->followup = type; | 2147 | ctx->pipe->followup = type; |
2146 | ctx->pipe->r_mode = ctx->w; | 2148 | ctx->pipe->r_mode = ctx->w; |
2147 | new_p=new_pipe(); | 2149 | new_p = new_pipe(); |
2148 | ctx->pipe->next = new_p; | 2150 | ctx->pipe->next = new_p; |
2149 | ctx->pipe = new_p; | 2151 | ctx->pipe = new_p; |
2150 | ctx->child = NULL; | 2152 | ctx->child = NULL; |
@@ -2158,19 +2160,19 @@ static int done_pipe(struct p_context *ctx, pipe_style type) | |||
2158 | */ | 2160 | */ |
2159 | static int redirect_dup_num(struct in_str *input) | 2161 | static int redirect_dup_num(struct in_str *input) |
2160 | { | 2162 | { |
2161 | int ch, d=0, ok=0; | 2163 | int ch, d = 0, ok = 0; |
2162 | ch = b_peek(input); | 2164 | ch = b_peek(input); |
2163 | if (ch != '&') return -1; | 2165 | if (ch != '&') return -1; |
2164 | 2166 | ||
2165 | b_getch(input); /* get the & */ | 2167 | b_getch(input); /* get the & */ |
2166 | ch=b_peek(input); | 2168 | ch = b_peek(input); |
2167 | if (ch == '-') { | 2169 | if (ch == '-') { |
2168 | b_getch(input); | 2170 | b_getch(input); |
2169 | return -3; /* "-" represents "close me" */ | 2171 | return -3; /* "-" represents "close me" */ |
2170 | } | 2172 | } |
2171 | while (isdigit(ch)) { | 2173 | while (isdigit(ch)) { |
2172 | d = d*10+(ch-'0'); | 2174 | d = d*10+(ch-'0'); |
2173 | ok=1; | 2175 | ok = 1; |
2174 | b_getch(input); | 2176 | b_getch(input); |
2175 | ch = b_peek(input); | 2177 | ch = b_peek(input); |
2176 | } | 2178 | } |
@@ -2195,14 +2197,15 @@ static int redirect_opt_num(o_string *o) | |||
2195 | { | 2197 | { |
2196 | int num; | 2198 | int num; |
2197 | 2199 | ||
2198 | if (o->length==0) return -1; | 2200 | if (o->length == 0) |
2199 | for (num=0; num<o->length; num++) { | 2201 | return -1; |
2200 | if (!isdigit(*(o->data+num))) { | 2202 | for (num = 0; num < o->length; num++) { |
2203 | if (!isdigit(*(o->data + num))) { | ||
2201 | return -1; | 2204 | return -1; |
2202 | } | 2205 | } |
2203 | } | 2206 | } |
2204 | /* reuse num (and save an int) */ | 2207 | /* reuse num (and save an int) */ |
2205 | num=atoi(o->data); | 2208 | num = atoi(o->data); |
2206 | b_reset(o); | 2209 | b_reset(o); |
2207 | return num; | 2210 | return num; |
2208 | } | 2211 | } |
@@ -2211,15 +2214,15 @@ static FILE *generate_stream_from_list(struct pipe *head) | |||
2211 | { | 2214 | { |
2212 | FILE *pf; | 2215 | FILE *pf; |
2213 | int pid, channel[2]; | 2216 | int pid, channel[2]; |
2214 | if (pipe(channel)<0) bb_perror_msg_and_die("pipe"); | 2217 | if (pipe(channel) < 0) bb_perror_msg_and_die("pipe"); |
2215 | #if !defined(__UCLIBC__) || defined(__ARCH_HAS_MMU__) | 2218 | #if !defined(__UCLIBC__) || defined(__ARCH_HAS_MMU__) |
2216 | pid=fork(); | 2219 | pid = fork(); |
2217 | #else | 2220 | #else |
2218 | pid=vfork(); | 2221 | pid = vfork(); |
2219 | #endif | 2222 | #endif |
2220 | if (pid<0) { | 2223 | if (pid < 0) { |
2221 | bb_perror_msg_and_die("fork"); | 2224 | bb_perror_msg_and_die("fork"); |
2222 | } else if (pid==0) { | 2225 | } else if (pid == 0) { |
2223 | close(channel[0]); | 2226 | close(channel[0]); |
2224 | if (channel[1] != 1) { | 2227 | if (channel[1] != 1) { |
2225 | dup2(channel[1],1); | 2228 | dup2(channel[1],1); |
@@ -2239,7 +2242,7 @@ static FILE *generate_stream_from_list(struct pipe *head) | |||
2239 | static int process_command_subs(o_string *dest, struct p_context *ctx, struct in_str *input, int subst_end) | 2242 | static int process_command_subs(o_string *dest, struct p_context *ctx, struct in_str *input, int subst_end) |
2240 | { | 2243 | { |
2241 | int retcode; | 2244 | int retcode; |
2242 | o_string result=NULL_O_STRING; | 2245 | o_string result = NULL_O_STRING; |
2243 | struct p_context inner; | 2246 | struct p_context inner; |
2244 | FILE *p; | 2247 | FILE *p; |
2245 | struct in_str pipe_str; | 2248 | struct in_str pipe_str; |
@@ -2252,8 +2255,8 @@ static int process_command_subs(o_string *dest, struct p_context *ctx, struct in | |||
2252 | done_pipe(&inner, PIPE_SEQ); | 2255 | done_pipe(&inner, PIPE_SEQ); |
2253 | b_free(&result); | 2256 | b_free(&result); |
2254 | 2257 | ||
2255 | p=generate_stream_from_list(inner.list_head); | 2258 | p = generate_stream_from_list(inner.list_head); |
2256 | if (p==NULL) return 1; | 2259 | if (p == NULL) return 1; |
2257 | mark_open(fileno(p)); | 2260 | mark_open(fileno(p)); |
2258 | setup_file_in_str(&pipe_str, p); | 2261 | setup_file_in_str(&pipe_str, p); |
2259 | 2262 | ||
@@ -2262,7 +2265,8 @@ static int process_command_subs(o_string *dest, struct p_context *ctx, struct in | |||
2262 | /* XXX In case of a syntax error, should we try to kill the child? | 2265 | /* XXX In case of a syntax error, should we try to kill the child? |
2263 | * That would be tough to do right, so just read until EOF. */ | 2266 | * That would be tough to do right, so just read until EOF. */ |
2264 | if (retcode == 1) { | 2267 | if (retcode == 1) { |
2265 | while (b_getch(&pipe_str)!=EOF) { /* discard */ }; | 2268 | while (b_getch(&pipe_str) != EOF) |
2269 | /* discard */; | ||
2266 | } | 2270 | } |
2267 | 2271 | ||
2268 | debug_printf("done reading from pipe, pclose()ing\n"); | 2272 | debug_printf("done reading from pipe, pclose()ing\n"); |
@@ -2272,7 +2276,7 @@ static int process_command_subs(o_string *dest, struct p_context *ctx, struct in | |||
2272 | * at the same time. That would be a lot of work, and contrary | 2276 | * at the same time. That would be a lot of work, and contrary |
2273 | * to the KISS philosophy of this program. */ | 2277 | * to the KISS philosophy of this program. */ |
2274 | mark_closed(fileno(p)); | 2278 | mark_closed(fileno(p)); |
2275 | retcode=pclose(p); | 2279 | retcode = pclose(p); |
2276 | free_pipe_list(inner.list_head,0); | 2280 | free_pipe_list(inner.list_head,0); |
2277 | debug_printf("pclosed, retcode=%d\n",retcode); | 2281 | debug_printf("pclosed, retcode=%d\n",retcode); |
2278 | /* XXX this process fails to trim a single trailing newline */ | 2282 | /* XXX this process fails to trim a single trailing newline */ |
@@ -2282,7 +2286,7 @@ static int process_command_subs(o_string *dest, struct p_context *ctx, struct in | |||
2282 | static int parse_group(o_string *dest, struct p_context *ctx, | 2286 | static int parse_group(o_string *dest, struct p_context *ctx, |
2283 | struct in_str *input, int ch) | 2287 | struct in_str *input, int ch) |
2284 | { | 2288 | { |
2285 | int rcode, endch=0; | 2289 | int rcode, endch = 0; |
2286 | struct p_context sub; | 2290 | struct p_context sub; |
2287 | struct child_prog *child = ctx->child; | 2291 | struct child_prog *child = ctx->child; |
2288 | if (child->argv) { | 2292 | if (child->argv) { |
@@ -2291,11 +2295,17 @@ static int parse_group(o_string *dest, struct p_context *ctx, | |||
2291 | } | 2295 | } |
2292 | initialize_context(&sub); | 2296 | initialize_context(&sub); |
2293 | switch (ch) { | 2297 | switch (ch) { |
2294 | case '(': endch=')'; child->subshell=1; break; | 2298 | case '(': |
2295 | case '{': endch='}'; break; | 2299 | endch = ')'; |
2296 | default: syntax(); /* really logic error */ | 2300 | child->subshell = 1; |
2297 | } | 2301 | break; |
2298 | rcode=parse_stream(dest,&sub,input,endch); | 2302 | case '{': |
2303 | endch = '}'; | ||
2304 | break; | ||
2305 | default: | ||
2306 | syntax(); /* really logic error */ | ||
2307 | } | ||
2308 | rcode = parse_stream(dest,&sub,input,endch); | ||
2299 | done_word(dest,&sub); /* finish off the final word in the subcontext */ | 2309 | done_word(dest,&sub); /* finish off the final word in the subcontext */ |
2300 | done_pipe(&sub, PIPE_SEQ); /* and the final command there, too */ | 2310 | done_pipe(&sub, PIPE_SEQ); /* and the final command there, too */ |
2301 | child->group = sub.list_head; | 2311 | child->group = sub.list_head; |
@@ -2319,21 +2329,21 @@ static const char *lookup_param(const char *src) | |||
2319 | /* return code: 0 for OK, 1 for syntax error */ | 2329 | /* return code: 0 for OK, 1 for syntax error */ |
2320 | static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *input) | 2330 | static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *input) |
2321 | { | 2331 | { |
2322 | int i, advance=0; | 2332 | int i, advance = 0; |
2323 | char sep[] = " "; | 2333 | char sep[] = " "; |
2324 | int ch = input->peek(input); /* first character after the $ */ | 2334 | int ch = input->peek(input); /* first character after the $ */ |
2325 | debug_printf("handle_dollar: ch=%c\n",ch); | 2335 | debug_printf("handle_dollar: ch=%c\n",ch); |
2326 | if (isalpha(ch)) { | 2336 | if (isalpha(ch)) { |
2327 | b_addchr(dest, SPECIAL_VAR_SYMBOL); | 2337 | b_addchr(dest, SPECIAL_VAR_SYMBOL); |
2328 | ctx->child->sp++; | 2338 | ctx->child->sp++; |
2329 | while (ch=b_peek(input),isalnum(ch) || ch=='_') { | 2339 | while (ch = b_peek(input),isalnum(ch) || ch == '_') { |
2330 | b_getch(input); | 2340 | b_getch(input); |
2331 | b_addchr(dest,ch); | 2341 | b_addchr(dest,ch); |
2332 | } | 2342 | } |
2333 | b_addchr(dest, SPECIAL_VAR_SYMBOL); | 2343 | b_addchr(dest, SPECIAL_VAR_SYMBOL); |
2334 | } else if (isdigit(ch)) { | 2344 | } else if (isdigit(ch)) { |
2335 | i = ch-'0'; /* XXX is $0 special? */ | 2345 | i = ch-'0'; /* XXX is $0 special? */ |
2336 | if (i<global_argc) { | 2346 | if (i < global_argc) { |
2337 | parse_string(dest, ctx, global_argv[i]); /* recursion */ | 2347 | parse_string(dest, ctx, global_argv[i]); /* recursion */ |
2338 | } | 2348 | } |
2339 | advance = 1; | 2349 | advance = 1; |
@@ -2359,7 +2369,10 @@ static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *i | |||
2359 | ctx->child->sp++; | 2369 | ctx->child->sp++; |
2360 | b_getch(input); | 2370 | b_getch(input); |
2361 | /* XXX maybe someone will try to escape the '}' */ | 2371 | /* XXX maybe someone will try to escape the '}' */ |
2362 | while (ch=b_getch(input),ch!=EOF && ch!='}') { | 2372 | while (1) { |
2373 | ch = b_getch(input); | ||
2374 | if (ch == EOF || ch == '}') | ||
2375 | break; | ||
2363 | b_addchr(dest,ch); | 2376 | b_addchr(dest,ch); |
2364 | } | 2377 | } |
2365 | if (ch != '}') { | 2378 | if (ch != '}') { |
@@ -2373,10 +2386,11 @@ static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *i | |||
2373 | process_command_subs(dest, ctx, input, ')'); | 2386 | process_command_subs(dest, ctx, input, ')'); |
2374 | break; | 2387 | break; |
2375 | case '*': | 2388 | case '*': |
2376 | sep[0]=ifs[0]; | 2389 | sep[0] = ifs[0]; |
2377 | for (i=1; i<global_argc; i++) { | 2390 | for (i = 1; i < global_argc; i++) { |
2378 | parse_string(dest, ctx, global_argv[i]); | 2391 | parse_string(dest, ctx, global_argv[i]); |
2379 | if (i+1 < global_argc) parse_string(dest, ctx, sep); | 2392 | if (i+1 < global_argc) |
2393 | parse_string(dest, ctx, sep); | ||
2380 | } | 2394 | } |
2381 | break; | 2395 | break; |
2382 | case '@': | 2396 | case '@': |
@@ -2419,28 +2433,31 @@ int parse_stream(o_string *dest, struct p_context *ctx, | |||
2419 | * found. When recursing, quote state is passed in via dest->quote. */ | 2433 | * found. When recursing, quote state is passed in via dest->quote. */ |
2420 | 2434 | ||
2421 | debug_printf("parse_stream, end_trigger=%d\n",end_trigger); | 2435 | debug_printf("parse_stream, end_trigger=%d\n",end_trigger); |
2422 | while ((ch=b_getch(input))!=EOF) { | 2436 | while ((ch = b_getch(input)) != EOF) { |
2423 | m = map[ch]; | 2437 | m = map[ch]; |
2424 | next = (ch == '\n') ? 0 : b_peek(input); | 2438 | next = (ch == '\n') ? 0 : b_peek(input); |
2425 | debug_printf("parse_stream: ch=%c (%d) m=%d quote=%d\n", | 2439 | debug_printf("parse_stream: ch=%c (%d) m=%d quote=%d\n", |
2426 | ch,ch,m,dest->quote); | 2440 | ch, ch, m, dest->quote); |
2427 | if (m==0 || ((m==1 || m==2) && dest->quote)) { | 2441 | if (m == 0 || ((m == 1 || m == 2) && dest->quote)) { |
2428 | b_addqchr(dest, ch, dest->quote); | 2442 | b_addqchr(dest, ch, dest->quote); |
2429 | } else { | 2443 | continue; |
2430 | if (m==2) { /* unquoted IFS */ | 2444 | } |
2431 | if (done_word(dest, ctx)) { | 2445 | if (m == 2) { /* unquoted IFS */ |
2432 | return 1; | 2446 | if (done_word(dest, ctx)) { |
2433 | } | 2447 | return 1; |
2434 | /* If we aren't performing a substitution, treat a newline as a | ||
2435 | * command separator. */ | ||
2436 | if (end_trigger != '\0' && ch=='\n') | ||
2437 | done_pipe(ctx,PIPE_SEQ); | ||
2438 | } | ||
2439 | if (ch == end_trigger && !dest->quote && ctx->w==RES_NONE) { | ||
2440 | debug_printf("leaving parse_stream (triggered)\n"); | ||
2441 | return 0; | ||
2442 | } | 2448 | } |
2443 | if (m!=2) switch (ch) { | 2449 | /* If we aren't performing a substitution, treat a newline as a |
2450 | * command separator. */ | ||
2451 | if (end_trigger != '\0' && ch == '\n') | ||
2452 | done_pipe(ctx,PIPE_SEQ); | ||
2453 | } | ||
2454 | if (ch == end_trigger && !dest->quote && ctx->w == RES_NONE) { | ||
2455 | debug_printf("leaving parse_stream (triggered)\n"); | ||
2456 | return 0; | ||
2457 | } | ||
2458 | if (m == 2) | ||
2459 | continue; | ||
2460 | switch (ch) { | ||
2444 | case '#': | 2461 | case '#': |
2445 | if (dest->length == 0 && !dest->quote) { | 2462 | if (dest->length == 0 && !dest->quote) { |
2446 | while (1) { | 2463 | while (1) { |
@@ -2462,7 +2479,7 @@ int parse_stream(o_string *dest, struct p_context *ctx, | |||
2462 | b_addqchr(dest, b_getch(input), dest->quote); | 2479 | b_addqchr(dest, b_getch(input), dest->quote); |
2463 | break; | 2480 | break; |
2464 | case '$': | 2481 | case '$': |
2465 | if (handle_dollar(dest, ctx, input)!=0) return 1; | 2482 | if (handle_dollar(dest, ctx, input) != 0) return 1; |
2466 | break; | 2483 | break; |
2467 | case '\'': | 2484 | case '\'': |
2468 | dest->nonnull = 1; | 2485 | dest->nonnull = 1; |
@@ -2487,9 +2504,9 @@ int parse_stream(o_string *dest, struct p_context *ctx, | |||
2487 | case '>': | 2504 | case '>': |
2488 | redir_fd = redirect_opt_num(dest); | 2505 | redir_fd = redirect_opt_num(dest); |
2489 | done_word(dest, ctx); | 2506 | done_word(dest, ctx); |
2490 | redir_style=REDIRECT_OVERWRITE; | 2507 | redir_style = REDIRECT_OVERWRITE; |
2491 | if (next == '>') { | 2508 | if (next == '>') { |
2492 | redir_style=REDIRECT_APPEND; | 2509 | redir_style = REDIRECT_APPEND; |
2493 | b_getch(input); | 2510 | b_getch(input); |
2494 | } else if (next == '(') { | 2511 | } else if (next == '(') { |
2495 | syntax(); /* until we support >(list) Process Substitution */ | 2512 | syntax(); /* until we support >(list) Process Substitution */ |
@@ -2500,12 +2517,12 @@ int parse_stream(o_string *dest, struct p_context *ctx, | |||
2500 | case '<': | 2517 | case '<': |
2501 | redir_fd = redirect_opt_num(dest); | 2518 | redir_fd = redirect_opt_num(dest); |
2502 | done_word(dest, ctx); | 2519 | done_word(dest, ctx); |
2503 | redir_style=REDIRECT_INPUT; | 2520 | redir_style = REDIRECT_INPUT; |
2504 | if (next == '<') { | 2521 | if (next == '<') { |
2505 | redir_style=REDIRECT_HEREIS; | 2522 | redir_style = REDIRECT_HEREIS; |
2506 | b_getch(input); | 2523 | b_getch(input); |
2507 | } else if (next == '>') { | 2524 | } else if (next == '>') { |
2508 | redir_style=REDIRECT_IO; | 2525 | redir_style = REDIRECT_IO; |
2509 | b_getch(input); | 2526 | b_getch(input); |
2510 | } else if (next == '(') { | 2527 | } else if (next == '(') { |
2511 | syntax(); /* until we support <(list) Process Substitution */ | 2528 | syntax(); /* until we support <(list) Process Substitution */ |
@@ -2519,7 +2536,7 @@ int parse_stream(o_string *dest, struct p_context *ctx, | |||
2519 | break; | 2536 | break; |
2520 | case '&': | 2537 | case '&': |
2521 | done_word(dest, ctx); | 2538 | done_word(dest, ctx); |
2522 | if (next=='&') { | 2539 | if (next == '&') { |
2523 | b_getch(input); | 2540 | b_getch(input); |
2524 | done_pipe(ctx,PIPE_AND); | 2541 | done_pipe(ctx,PIPE_AND); |
2525 | } else { | 2542 | } else { |
@@ -2528,7 +2545,7 @@ int parse_stream(o_string *dest, struct p_context *ctx, | |||
2528 | break; | 2545 | break; |
2529 | case '|': | 2546 | case '|': |
2530 | done_word(dest, ctx); | 2547 | done_word(dest, ctx); |
2531 | if (next=='|') { | 2548 | if (next == '|') { |
2532 | b_getch(input); | 2549 | b_getch(input); |
2533 | done_pipe(ctx,PIPE_OR); | 2550 | done_pipe(ctx,PIPE_OR); |
2534 | } else { | 2551 | } else { |
@@ -2540,17 +2557,16 @@ int parse_stream(o_string *dest, struct p_context *ctx, | |||
2540 | break; | 2557 | break; |
2541 | case '(': | 2558 | case '(': |
2542 | case '{': | 2559 | case '{': |
2543 | if (parse_group(dest, ctx, input, ch)!=0) return 1; | 2560 | if (parse_group(dest, ctx, input, ch) != 0) |
2561 | return 1; | ||
2544 | break; | 2562 | break; |
2545 | case ')': | 2563 | case ')': |
2546 | case '}': | 2564 | case '}': |
2547 | syntax(); /* Proper use of this character caught by end_trigger */ | 2565 | syntax(); /* Proper use of this character caught by end_trigger */ |
2548 | return 1; | 2566 | return 1; |
2549 | break; | ||
2550 | default: | 2567 | default: |
2551 | syntax(); /* this is really an internal logic error */ | 2568 | syntax(); /* this is really an internal logic error */ |
2552 | return 1; | 2569 | return 1; |
2553 | } | ||
2554 | } | 2570 | } |
2555 | } | 2571 | } |
2556 | /* complain if quote? No, maybe we just finished a command substitution | 2572 | /* complain if quote? No, maybe we just finished a command substitution |
@@ -2595,14 +2611,14 @@ int parse_stream_outer(struct in_str *inp, int flag) | |||
2595 | { | 2611 | { |
2596 | 2612 | ||
2597 | struct p_context ctx; | 2613 | struct p_context ctx; |
2598 | o_string temp=NULL_O_STRING; | 2614 | o_string temp = NULL_O_STRING; |
2599 | int rcode; | 2615 | int rcode; |
2600 | do { | 2616 | do { |
2601 | ctx.type = flag; | 2617 | ctx.type = flag; |
2602 | initialize_context(&ctx); | 2618 | initialize_context(&ctx); |
2603 | update_ifs_map(); | 2619 | update_ifs_map(); |
2604 | if (!(flag & FLAG_PARSE_SEMICOLON) || (flag & FLAG_REPARSING)) mapset(";$&|", 0); | 2620 | if (!(flag & FLAG_PARSE_SEMICOLON) || (flag & FLAG_REPARSING)) mapset(";$&|", 0); |
2605 | inp->promptmode=1; | 2621 | inp->promptmode = 1; |
2606 | rcode = parse_stream(&temp, &ctx, inp, '\n'); | 2622 | rcode = parse_stream(&temp, &ctx, inp, '\n'); |
2607 | if (rcode != 1 && ctx.old_flag != 0) { | 2623 | if (rcode != 1 && ctx.old_flag != 0) { |
2608 | syntax(); | 2624 | syntax(); |
@@ -2710,8 +2726,7 @@ int hush_main(int argc, char **argv) | |||
2710 | set_local_var(*e, 2); /* without call putenv() */ | 2726 | set_local_var(*e, 2); /* without call putenv() */ |
2711 | } | 2727 | } |
2712 | 2728 | ||
2713 | last_return_code=EXIT_SUCCESS; | 2729 | last_return_code = EXIT_SUCCESS; |
2714 | |||
2715 | 2730 | ||
2716 | if (argv[0] && argv[0][0] == '-') { | 2731 | if (argv[0] && argv[0][0] == '-') { |
2717 | debug_printf("\nsourcing /etc/profile\n"); | 2732 | debug_printf("\nsourcing /etc/profile\n"); |
@@ -2722,7 +2737,7 @@ int hush_main(int argc, char **argv) | |||
2722 | fclose(input); | 2737 | fclose(input); |
2723 | } | 2738 | } |
2724 | } | 2739 | } |
2725 | input=stdin; | 2740 | input = stdin; |
2726 | 2741 | ||
2727 | while ((opt = getopt(argc, argv, "c:xif")) > 0) { | 2742 | while ((opt = getopt(argc, argv, "c:xif")) > 0) { |
2728 | switch (opt) { | 2743 | switch (opt) { |
@@ -2757,8 +2772,9 @@ int hush_main(int argc, char **argv) | |||
2757 | * standard input is a terminal | 2772 | * standard input is a terminal |
2758 | * standard output is a terminal | 2773 | * standard output is a terminal |
2759 | * Refer to Posix.2, the description of the `sh' utility. */ | 2774 | * Refer to Posix.2, the description of the `sh' utility. */ |
2760 | if (argv[optind]==NULL && input==stdin && | 2775 | if (argv[optind] == NULL && input == stdin |
2761 | isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { | 2776 | && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) |
2777 | ) { | ||
2762 | interactive++; | 2778 | interactive++; |
2763 | } | 2779 | } |
2764 | 2780 | ||
@@ -2773,8 +2789,8 @@ int hush_main(int argc, char **argv) | |||
2773 | setup_job_control(); | 2789 | setup_job_control(); |
2774 | } | 2790 | } |
2775 | 2791 | ||
2776 | if (argv[optind]==NULL) { | 2792 | if (argv[optind] == NULL) { |
2777 | opt=parse_file_outer(stdin); | 2793 | opt = parse_file_outer(stdin); |
2778 | goto final_return; | 2794 | goto final_return; |
2779 | } | 2795 | } |
2780 | 2796 | ||
@@ -2885,7 +2901,7 @@ static char **make_list_in(char **inp, char *name) | |||
2885 | } | 2901 | } |
2886 | 2902 | ||
2887 | /* Make new string for parser */ | 2903 | /* Make new string for parser */ |
2888 | static char * make_string(char ** inp) | 2904 | static char* make_string(char ** inp) |
2889 | { | 2905 | { |
2890 | char *p; | 2906 | char *p; |
2891 | char *str = NULL; | 2907 | char *str = NULL; |