aboutsummaryrefslogtreecommitdiff
path: root/shell
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-04-13 19:56:56 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-04-13 19:56:56 +0000
commitfbf6dea5a288362564c4ab4859a1018e1f2dcde4 (patch)
treeaf7dc73454086ec47239424024e1c2487fee2e5d /shell
parentf03dbeda90784fd9ac9ff4ed01f1257f6568d157 (diff)
downloadbusybox-w32-fbf6dea5a288362564c4ab4859a1018e1f2dcde4.tar.gz
busybox-w32-fbf6dea5a288362564c4ab4859a1018e1f2dcde4.tar.bz2
busybox-w32-fbf6dea5a288362564c4ab4859a1018e1f2dcde4.zip
hush: more style fixes.
Diffstat (limited to 'shell')
-rw-r--r--shell/hush.c130
1 files changed, 66 insertions, 64 deletions
diff --git a/shell/hush.c b/shell/hush.c
index 458664ff2..035919500 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -104,7 +104,11 @@ typedef enum {
104 104
105/* The descrip member of this structure is only used to make debugging 105/* The descrip member of this structure is only used to make debugging
106 * output pretty */ 106 * output pretty */
107static const struct {int mode; int default_fd; const char *descrip;} redir_table[] = { 107static const struct {
108 int mode;
109 int default_fd;
110 const char *descrip;
111} redir_table[] = {
108 { 0, 0, "()" }, 112 { 0, 0, "()" },
109 { O_RDONLY, 0, "<" }, 113 { O_RDONLY, 0, "<" },
110 { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, 114 { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" },
@@ -803,7 +807,7 @@ static void b_free(o_string *o)
803 */ 807 */
804static int b_addqchr(o_string *o, int ch, int quote) 808static int b_addqchr(o_string *o, int ch, int quote)
805{ 809{
806 if (quote && strchr("*?[\\",ch)) { 810 if (quote && strchr("*?[\\", ch)) {
807 int rc; 811 int rc;
808 rc = b_addchr(o, '\\'); 812 rc = b_addchr(o, '\\');
809 if (rc) 813 if (rc)
@@ -1574,7 +1578,7 @@ static int run_list_real(struct pipe *pi)
1574 continue; 1578 continue;
1575 save_num_progs = pi->num_progs; /* save number of programs */ 1579 save_num_progs = pi->num_progs; /* save number of programs */
1576 rcode = run_pipe_real(pi); 1580 rcode = run_pipe_real(pi);
1577 debug_printf("run_pipe_real returned %d\n",rcode); 1581 debug_printf("run_pipe_real returned %d\n", rcode);
1578 if (rcode != -1) { 1582 if (rcode != -1) {
1579 /* We only ran a builtin: rcode was set by the return value 1583 /* We only ran a builtin: rcode was set by the return value
1580 * of run_pipe_real(), and we don't need to wait for anything. */ 1584 * of run_pipe_real(), and we don't need to wait for anything. */
@@ -1596,7 +1600,7 @@ static int run_list_real(struct pipe *pi)
1596 } else { 1600 } else {
1597 rcode = checkjobs(pi); 1601 rcode = checkjobs(pi);
1598 } 1602 }
1599 debug_printf("checkjobs returned %d\n",rcode); 1603 debug_printf("checkjobs returned %d\n", rcode);
1600 } 1604 }
1601 last_return_code = rcode; 1605 last_return_code = rcode;
1602 pi->num_progs = save_num_progs; /* restore number of programs */ 1606 pi->num_progs = save_num_progs; /* restore number of programs */
@@ -2010,7 +2014,7 @@ static int reserved_word(o_string *dest, struct p_context *ctx)
2010 2014
2011 for (r = reserved_list; r < reserved_list+NRES; r++) { 2015 for (r = reserved_list; r < reserved_list+NRES; r++) {
2012 if (strcmp(dest->data, r->literal) == 0) { 2016 if (strcmp(dest->data, r->literal) == 0) {
2013 debug_printf("found reserved word %s, code %d\n",r->literal,r->code); 2017 debug_printf("found reserved word %s, code %d\n", r->literal, r->code);
2014 if (r->flag & FLAG_START) { 2018 if (r->flag & FLAG_START) {
2015 struct p_context *new = xmalloc(sizeof(struct p_context)); 2019 struct p_context *new = xmalloc(sizeof(struct p_context));
2016 debug_printf("push stack\n"); 2020 debug_printf("push stack\n");
@@ -2035,7 +2039,7 @@ static int reserved_word(o_string *dest, struct p_context *ctx)
2035 if (ctx->old_flag & FLAG_END) { 2039 if (ctx->old_flag & FLAG_END) {
2036 struct p_context *old; 2040 struct p_context *old;
2037 debug_printf("pop stack\n"); 2041 debug_printf("pop stack\n");
2038 done_pipe(ctx,PIPE_SEQ); 2042 done_pipe(ctx, PIPE_SEQ);
2039 old = ctx->stack; 2043 old = ctx->stack;
2040 old->child->group = ctx->list_head; 2044 old->child->group = ctx->list_head;
2041 old->child->subshell = 0; 2045 old->child->subshell = 0;
@@ -2070,8 +2074,8 @@ static int done_word(o_string *dest, struct p_context *ctx)
2070 return 1; /* syntax error, groups and arglists don't mix */ 2074 return 1; /* syntax error, groups and arglists don't mix */
2071 } 2075 }
2072 if (!child->argv && (ctx->type & FLAG_PARSE_SEMICOLON)) { 2076 if (!child->argv && (ctx->type & FLAG_PARSE_SEMICOLON)) {
2073 debug_printf("checking %s for reserved-ness\n",dest->data); 2077 debug_printf("checking %s for reserved-ness\n", dest->data);
2074 if (reserved_word(dest,ctx)) 2078 if (reserved_word(dest, ctx))
2075 return (ctx->w == RES_SNTX); 2079 return (ctx->w == RES_SNTX);
2076 } 2080 }
2077 glob_target = &child->glob_result; 2081 glob_target = &child->glob_result;
@@ -2091,8 +2095,8 @@ static int done_word(o_string *dest, struct p_context *ctx)
2091 child->argv = glob_target->gl_pathv; 2095 child->argv = glob_target->gl_pathv;
2092 } 2096 }
2093 if (ctx->w == RES_FOR) { 2097 if (ctx->w == RES_FOR) {
2094 done_word(dest,ctx); 2098 done_word(dest, ctx);
2095 done_pipe(ctx,PIPE_SEQ); 2099 done_pipe(ctx, PIPE_SEQ);
2096 } 2100 }
2097 return 0; 2101 return 0;
2098} 2102}
@@ -2116,7 +2120,7 @@ static int done_command(struct p_context *ctx)
2116 return 0; 2120 return 0;
2117 } else if (prog) { 2121 } else if (prog) {
2118 pi->num_progs++; 2122 pi->num_progs++;
2119 debug_printf("done_command: num_progs incremented to %d\n",pi->num_progs); 2123 debug_printf("done_command: num_progs incremented to %d\n", pi->num_progs);
2120 } else { 2124 } else {
2121 debug_printf("done_command: initializing\n"); 2125 debug_printf("done_command: initializing\n");
2122 } 2126 }
@@ -2228,10 +2232,10 @@ static FILE *generate_stream_from_list(struct pipe *head)
2228 } 2232 }
2229 _exit(run_list_real(head)); /* leaks memory */ 2233 _exit(run_list_real(head)); /* leaks memory */
2230 } 2234 }
2231 debug_printf("forked child %d\n",pid); 2235 debug_printf("forked child %d\n", pid);
2232 close(channel[1]); 2236 close(channel[1]);
2233 pf = fdopen(channel[0],"r"); 2237 pf = fdopen(channel[0],"r");
2234 debug_printf("pipe on FILE *%p\n",pf); 2238 debug_printf("pipe on FILE *%p\n", pf);
2235 return pf; 2239 return pf;
2236} 2240}
2237 2241
@@ -2275,8 +2279,8 @@ static int process_command_subs(o_string *dest, struct p_context *ctx, struct in
2275 * to the KISS philosophy of this program. */ 2279 * to the KISS philosophy of this program. */
2276 mark_closed(fileno(p)); 2280 mark_closed(fileno(p));
2277 retcode = pclose(p); 2281 retcode = pclose(p);
2278 free_pipe_list(inner.list_head,0); 2282 free_pipe_list(inner.list_head, 0);
2279 debug_printf("pclosed, retcode=%d\n",retcode); 2283 debug_printf("pclosed, retcode=%d\n", retcode);
2280 /* XXX this process fails to trim a single trailing newline */ 2284 /* XXX this process fails to trim a single trailing newline */
2281 return retcode; 2285 return retcode;
2282} 2286}
@@ -2303,8 +2307,8 @@ static int parse_group(o_string *dest, struct p_context *ctx,
2303 default: 2307 default:
2304 syntax(); /* really logic error */ 2308 syntax(); /* really logic error */
2305 } 2309 }
2306 rcode = parse_stream(dest,&sub,input,endch); 2310 rcode = parse_stream(dest, &sub, input, endch);
2307 done_word(dest,&sub); /* finish off the final word in the subcontext */ 2311 done_word(dest, &sub); /* finish off the final word in the subcontext */
2308 done_pipe(&sub, PIPE_SEQ); /* and the final command there, too */ 2312 done_pipe(&sub, PIPE_SEQ); /* and the final command there, too */
2309 child->group = sub.list_head; 2313 child->group = sub.list_head;
2310 return rcode; 2314 return rcode;
@@ -2330,13 +2334,13 @@ static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *i
2330 int i, advance = 0; 2334 int i, advance = 0;
2331 char sep[] = " "; 2335 char sep[] = " ";
2332 int ch = input->peek(input); /* first character after the $ */ 2336 int ch = input->peek(input); /* first character after the $ */
2333 debug_printf("handle_dollar: ch=%c\n",ch); 2337 debug_printf("handle_dollar: ch=%c\n", ch);
2334 if (isalpha(ch)) { 2338 if (isalpha(ch)) {
2335 b_addchr(dest, SPECIAL_VAR_SYMBOL); 2339 b_addchr(dest, SPECIAL_VAR_SYMBOL);
2336 ctx->child->sp++; 2340 ctx->child->sp++;
2337 while (ch = b_peek(input),isalnum(ch) || ch == '_') { 2341 while (ch = b_peek(input), isalnum(ch) || ch == '_') {
2338 b_getch(input); 2342 b_getch(input);
2339 b_addchr(dest,ch); 2343 b_addchr(dest, ch);
2340 } 2344 }
2341 b_addchr(dest, SPECIAL_VAR_SYMBOL); 2345 b_addchr(dest, SPECIAL_VAR_SYMBOL);
2342 } else if (isdigit(ch)) { 2346 } else if (isdigit(ch)) {
@@ -2347,7 +2351,7 @@ static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *i
2347 advance = 1; 2351 advance = 1;
2348 } else switch (ch) { 2352 } else switch (ch) {
2349 case '$': 2353 case '$':
2350 b_adduint(dest,getpid()); 2354 b_adduint(dest, getpid());
2351 advance = 1; 2355 advance = 1;
2352 break; 2356 break;
2353 case '!': 2357 case '!':
@@ -2355,11 +2359,11 @@ static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *i
2355 advance = 1; 2359 advance = 1;
2356 break; 2360 break;
2357 case '?': 2361 case '?':
2358 b_adduint(dest,last_return_code); 2362 b_adduint(dest, last_return_code);
2359 advance = 1; 2363 advance = 1;
2360 break; 2364 break;
2361 case '#': 2365 case '#':
2362 b_adduint(dest,global_argc ? global_argc-1 : 0); 2366 b_adduint(dest, global_argc ? global_argc-1 : 0);
2363 advance = 1; 2367 advance = 1;
2364 break; 2368 break;
2365 case '{': 2369 case '{':
@@ -2371,7 +2375,7 @@ static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *i
2371 ch = b_getch(input); 2375 ch = b_getch(input);
2372 if (ch == EOF || ch == '}') 2376 if (ch == EOF || ch == '}')
2373 break; 2377 break;
2374 b_addchr(dest,ch); 2378 b_addchr(dest, ch);
2375 } 2379 }
2376 if (ch != '}') { 2380 if (ch != '}') {
2377 syntax(); 2381 syntax();
@@ -2395,11 +2399,11 @@ static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *i
2395 case '-': 2399 case '-':
2396 case '_': 2400 case '_':
2397 /* still unhandled, but should be eventually */ 2401 /* still unhandled, but should be eventually */
2398 bb_error_msg("unhandled syntax: $%c",ch); 2402 bb_error_msg("unhandled syntax: $%c", ch);
2399 return 1; 2403 return 1;
2400 break; 2404 break;
2401 default: 2405 default:
2402 b_addqchr(dest,'$',dest->quote); 2406 b_addqchr(dest,'$', dest->quote);
2403 } 2407 }
2404 /* Eat the character if the flag was set. If the compiler 2408 /* Eat the character if the flag was set. If the compiler
2405 * is smart enough, we could substitute "b_getch(input);" 2409 * is smart enough, we could substitute "b_getch(input);"
@@ -2430,7 +2434,7 @@ int parse_stream(o_string *dest, struct p_context *ctx,
2430 * A single-quote triggers a bypass of the main loop until its mate is 2434 * A single-quote triggers a bypass of the main loop until its mate is
2431 * found. When recursing, quote state is passed in via dest->quote. */ 2435 * found. When recursing, quote state is passed in via dest->quote. */
2432 2436
2433 debug_printf("parse_stream, end_trigger=%d\n",end_trigger); 2437 debug_printf("parse_stream, end_trigger=%d\n", end_trigger);
2434 while ((ch = b_getch(input)) != EOF) { 2438 while ((ch = b_getch(input)) != EOF) {
2435 m = map[ch]; 2439 m = map[ch];
2436 next = (ch == '\n') ? 0 : b_peek(input); 2440 next = (ch == '\n') ? 0 : b_peek(input);
@@ -2447,7 +2451,7 @@ int parse_stream(o_string *dest, struct p_context *ctx,
2447 /* If we aren't performing a substitution, treat a newline as a 2451 /* If we aren't performing a substitution, treat a newline as a
2448 * command separator. */ 2452 * command separator. */
2449 if (end_trigger != '\0' && ch == '\n') 2453 if (end_trigger != '\0' && ch == '\n')
2450 done_pipe(ctx,PIPE_SEQ); 2454 done_pipe(ctx, PIPE_SEQ);
2451 } 2455 }
2452 if (ch == end_trigger && !dest->quote && ctx->w == RES_NONE) { 2456 if (ch == end_trigger && !dest->quote && ctx->w == RES_NONE) {
2453 debug_printf("leaving parse_stream (triggered)\n"); 2457 debug_printf("leaving parse_stream (triggered)\n");
@@ -2485,7 +2489,7 @@ int parse_stream(o_string *dest, struct p_context *ctx,
2485 ch = b_getch(input); 2489 ch = b_getch(input);
2486 if (ch == EOF || ch == '\'') 2490 if (ch == EOF || ch == '\'')
2487 break; 2491 break;
2488 b_addchr(dest,ch); 2492 b_addchr(dest, ch);
2489 } 2493 }
2490 if (ch == EOF) { 2494 if (ch == EOF) {
2491 syntax(); 2495 syntax();
@@ -2530,22 +2534,22 @@ int parse_stream(o_string *dest, struct p_context *ctx,
2530 break; 2534 break;
2531 case ';': 2535 case ';':
2532 done_word(dest, ctx); 2536 done_word(dest, ctx);
2533 done_pipe(ctx,PIPE_SEQ); 2537 done_pipe(ctx, PIPE_SEQ);
2534 break; 2538 break;
2535 case '&': 2539 case '&':
2536 done_word(dest, ctx); 2540 done_word(dest, ctx);
2537 if (next == '&') { 2541 if (next == '&') {
2538 b_getch(input); 2542 b_getch(input);
2539 done_pipe(ctx,PIPE_AND); 2543 done_pipe(ctx, PIPE_AND);
2540 } else { 2544 } else {
2541 done_pipe(ctx,PIPE_BG); 2545 done_pipe(ctx, PIPE_BG);
2542 } 2546 }
2543 break; 2547 break;
2544 case '|': 2548 case '|':
2545 done_word(dest, ctx); 2549 done_word(dest, ctx);
2546 if (next == '|') { 2550 if (next == '|') {
2547 b_getch(input); 2551 b_getch(input);
2548 done_pipe(ctx,PIPE_OR); 2552 done_pipe(ctx, PIPE_OR);
2549 } else { 2553 } else {
2550 /* we could pick up a file descriptor choice here 2554 /* we could pick up a file descriptor choice here
2551 * with redirect_opt_num(), but bash doesn't do it. 2555 * with redirect_opt_num(), but bash doesn't do it.
@@ -2623,7 +2627,7 @@ int parse_stream_outer(struct in_str *inp, int flag)
2623 } 2627 }
2624 if (rcode != 1 && ctx.old_flag == 0) { 2628 if (rcode != 1 && ctx.old_flag == 0) {
2625 done_word(&temp, &ctx); 2629 done_word(&temp, &ctx);
2626 done_pipe(&ctx,PIPE_SEQ); 2630 done_pipe(&ctx, PIPE_SEQ);
2627 run_list(ctx.list_head); 2631 run_list(ctx.list_head);
2628 } else { 2632 } else {
2629 if (ctx.old_flag != 0) { 2633 if (ctx.old_flag != 0) {
@@ -2661,9 +2665,10 @@ static int parse_file_outer(FILE *f)
2661 * we don't fight over who gets the foreground */ 2665 * we don't fight over who gets the foreground */
2662static void setup_job_control(void) 2666static void setup_job_control(void)
2663{ 2667{
2664 static pid_t shell_pgrp; 2668 /*static --why?? */ pid_t shell_pgrp;
2669
2665 /* Loop until we are in the foreground. */ 2670 /* Loop until we are in the foreground. */
2666 while (tcgetpgrp (shell_terminal) != (shell_pgrp = getpgrp ())) 2671 while (tcgetpgrp(shell_terminal) != (shell_pgrp = getpgrp()))
2667 kill(- shell_pgrp, SIGTTIN); 2672 kill(- shell_pgrp, SIGTTIN);
2668 2673
2669 /* Ignore interactive and job-control signals. */ 2674 /* Ignore interactive and job-control signals. */
@@ -2689,7 +2694,7 @@ int hush_main(int argc, char **argv)
2689{ 2694{
2690 int opt; 2695 int opt;
2691 FILE *input; 2696 FILE *input;
2692 char **e = environ; 2697 char **e;
2693 2698
2694#if ENABLE_FEATURE_EDITING 2699#if ENABLE_FEATURE_EDITING
2695 line_input_state = new_line_input_t(FOR_SHELL); 2700 line_input_state = new_line_input_t(FOR_SHELL);
@@ -2720,10 +2725,10 @@ int hush_main(int argc, char **argv)
2720 2725
2721 /* initialize our shell local variables with the values 2726 /* initialize our shell local variables with the values
2722 * currently living in the environment */ 2727 * currently living in the environment */
2723 if (e) { 2728 e = environ;
2724 for (; *e; e++) 2729 if (e)
2725 set_local_var(*e, 2); /* without call putenv() */ 2730 while (*e)
2726 } 2731 set_local_var(*e++, 2); /* without call putenv() */
2727 2732
2728 last_return_code = EXIT_SUCCESS; 2733 last_return_code = EXIT_SUCCESS;
2729 2734
@@ -2741,37 +2746,34 @@ int hush_main(int argc, char **argv)
2741 2746
2742 while ((opt = getopt(argc, argv, "c:xif")) > 0) { 2747 while ((opt = getopt(argc, argv, "c:xif")) > 0) {
2743 switch (opt) { 2748 switch (opt) {
2744 case 'c': 2749 case 'c':
2745 { 2750 global_argv = argv+optind;
2746 global_argv = argv+optind; 2751 global_argc = argc-optind;
2747 global_argc = argc-optind; 2752 opt = parse_string_outer(optarg, FLAG_PARSE_SEMICOLON);
2748 opt = parse_string_outer(optarg, FLAG_PARSE_SEMICOLON); 2753 goto final_return;
2749 goto final_return; 2754 case 'i':
2750 } 2755 interactive++;
2751 break; 2756 break;
2752 case 'i': 2757 case 'f':
2753 interactive++; 2758 fake_mode++;
2754 break; 2759 break;
2755 case 'f': 2760 default:
2756 fake_mode++;
2757 break;
2758 default:
2759#ifndef BB_VER 2761#ifndef BB_VER
2760 fprintf(stderr, "Usage: sh [FILE]...\n" 2762 fprintf(stderr, "Usage: sh [FILE]...\n"
2761 " or: sh -c command [args]...\n\n"); 2763 " or: sh -c command [args]...\n\n");
2762 exit(EXIT_FAILURE); 2764 exit(EXIT_FAILURE);
2763#else 2765#else
2764 bb_show_usage(); 2766 bb_show_usage();
2765#endif 2767#endif
2766 } 2768 }
2767 } 2769 }
2768 /* A shell is interactive if the `-i' flag was given, or if all of 2770 /* A shell is interactive if the '-i' flag was given, or if all of
2769 * the following conditions are met: 2771 * the following conditions are met:
2770 * no -c command 2772 * no -c command
2771 * no arguments remaining or the -s flag given 2773 * no arguments remaining or the -s flag given
2772 * standard input is a terminal 2774 * standard input is a terminal
2773 * standard output is a terminal 2775 * standard output is a terminal
2774 * Refer to Posix.2, the description of the `sh' utility. */ 2776 * Refer to Posix.2, the description of the 'sh' utility. */
2775 if (argv[optind] == NULL && input == stdin 2777 if (argv[optind] == NULL && input == stdin
2776 && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) 2778 && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)
2777 ) { 2779 ) {
@@ -2802,7 +2804,7 @@ int hush_main(int argc, char **argv)
2802 2804
2803#if ENABLE_FEATURE_CLEAN_UP 2805#if ENABLE_FEATURE_CLEAN_UP
2804 fclose(input); 2806 fclose(input);
2805 if (cwd && cwd != bb_msg_unknown) 2807 if (cwd != bb_msg_unknown)
2806 free((char*)cwd); 2808 free((char*)cwd);
2807 { 2809 {
2808 struct variables *cur, *tmp; 2810 struct variables *cur, *tmp;
@@ -2885,7 +2887,7 @@ static char **make_list_in(char **inp, char *name)
2885 len = strlen(p1); 2887 len = strlen(p1);
2886 p2 = p1 + len; 2888 p2 = p1 + len;
2887 } 2889 }
2888 /* we use n + 2 in realloc for list,because we add 2890 /* we use n + 2 in realloc for list, because we add
2889 * new element and then we will add NULL element */ 2891 * new element and then we will add NULL element */
2890 list = xrealloc(list, sizeof(*list) * (n + 2)); 2892 list = xrealloc(list, sizeof(*list) * (n + 2));
2891 list[n] = xmalloc(2 + name_len + len); 2893 list[n] = xmalloc(2 + name_len + len);