mirror of
https://github.com/sheumann/hush.git
synced 2024-08-26 12:29:04 +00:00
hush: more style fixes.
This commit is contained in:
parent
f03dbeda90
commit
fbf6dea5a2
130
shell/hush.c
130
shell/hush.c
@ -104,7 +104,11 @@ typedef enum {
|
|||||||
|
|
||||||
/* The descrip member of this structure is only used to make debugging
|
/* The descrip member of this structure is only used to make debugging
|
||||||
* output pretty */
|
* output pretty */
|
||||||
static const struct {int mode; int default_fd; const char *descrip;} redir_table[] = {
|
static const struct {
|
||||||
|
int mode;
|
||||||
|
int default_fd;
|
||||||
|
const char *descrip;
|
||||||
|
} redir_table[] = {
|
||||||
{ 0, 0, "()" },
|
{ 0, 0, "()" },
|
||||||
{ O_RDONLY, 0, "<" },
|
{ O_RDONLY, 0, "<" },
|
||||||
{ O_CREAT|O_TRUNC|O_WRONLY, 1, ">" },
|
{ O_CREAT|O_TRUNC|O_WRONLY, 1, ">" },
|
||||||
@ -803,7 +807,7 @@ static void b_free(o_string *o)
|
|||||||
*/
|
*/
|
||||||
static int b_addqchr(o_string *o, int ch, int quote)
|
static int b_addqchr(o_string *o, int ch, int quote)
|
||||||
{
|
{
|
||||||
if (quote && strchr("*?[\\",ch)) {
|
if (quote && strchr("*?[\\", ch)) {
|
||||||
int rc;
|
int rc;
|
||||||
rc = b_addchr(o, '\\');
|
rc = b_addchr(o, '\\');
|
||||||
if (rc)
|
if (rc)
|
||||||
@ -1574,7 +1578,7 @@ static int run_list_real(struct pipe *pi)
|
|||||||
continue;
|
continue;
|
||||||
save_num_progs = pi->num_progs; /* save number of programs */
|
save_num_progs = pi->num_progs; /* save number of programs */
|
||||||
rcode = run_pipe_real(pi);
|
rcode = run_pipe_real(pi);
|
||||||
debug_printf("run_pipe_real returned %d\n",rcode);
|
debug_printf("run_pipe_real returned %d\n", rcode);
|
||||||
if (rcode != -1) {
|
if (rcode != -1) {
|
||||||
/* We only ran a builtin: rcode was set by the return value
|
/* We only ran a builtin: rcode was set by the return value
|
||||||
* of run_pipe_real(), and we don't need to wait for anything. */
|
* 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)
|
|||||||
} else {
|
} else {
|
||||||
rcode = checkjobs(pi);
|
rcode = checkjobs(pi);
|
||||||
}
|
}
|
||||||
debug_printf("checkjobs returned %d\n",rcode);
|
debug_printf("checkjobs returned %d\n", rcode);
|
||||||
}
|
}
|
||||||
last_return_code = rcode;
|
last_return_code = rcode;
|
||||||
pi->num_progs = save_num_progs; /* restore number of programs */
|
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)
|
|||||||
|
|
||||||
for (r = reserved_list; r < reserved_list+NRES; r++) {
|
for (r = reserved_list; r < reserved_list+NRES; r++) {
|
||||||
if (strcmp(dest->data, r->literal) == 0) {
|
if (strcmp(dest->data, r->literal) == 0) {
|
||||||
debug_printf("found reserved word %s, code %d\n",r->literal,r->code);
|
debug_printf("found reserved word %s, code %d\n", r->literal, r->code);
|
||||||
if (r->flag & FLAG_START) {
|
if (r->flag & FLAG_START) {
|
||||||
struct p_context *new = xmalloc(sizeof(struct p_context));
|
struct p_context *new = xmalloc(sizeof(struct p_context));
|
||||||
debug_printf("push stack\n");
|
debug_printf("push stack\n");
|
||||||
@ -2035,7 +2039,7 @@ static int reserved_word(o_string *dest, struct p_context *ctx)
|
|||||||
if (ctx->old_flag & FLAG_END) {
|
if (ctx->old_flag & FLAG_END) {
|
||||||
struct p_context *old;
|
struct p_context *old;
|
||||||
debug_printf("pop stack\n");
|
debug_printf("pop stack\n");
|
||||||
done_pipe(ctx,PIPE_SEQ);
|
done_pipe(ctx, PIPE_SEQ);
|
||||||
old = ctx->stack;
|
old = ctx->stack;
|
||||||
old->child->group = ctx->list_head;
|
old->child->group = ctx->list_head;
|
||||||
old->child->subshell = 0;
|
old->child->subshell = 0;
|
||||||
@ -2070,8 +2074,8 @@ static int done_word(o_string *dest, struct p_context *ctx)
|
|||||||
return 1; /* syntax error, groups and arglists don't mix */
|
return 1; /* syntax error, groups and arglists don't mix */
|
||||||
}
|
}
|
||||||
if (!child->argv && (ctx->type & FLAG_PARSE_SEMICOLON)) {
|
if (!child->argv && (ctx->type & FLAG_PARSE_SEMICOLON)) {
|
||||||
debug_printf("checking %s for reserved-ness\n",dest->data);
|
debug_printf("checking %s for reserved-ness\n", dest->data);
|
||||||
if (reserved_word(dest,ctx))
|
if (reserved_word(dest, ctx))
|
||||||
return (ctx->w == RES_SNTX);
|
return (ctx->w == RES_SNTX);
|
||||||
}
|
}
|
||||||
glob_target = &child->glob_result;
|
glob_target = &child->glob_result;
|
||||||
@ -2091,8 +2095,8 @@ static int done_word(o_string *dest, struct p_context *ctx)
|
|||||||
child->argv = glob_target->gl_pathv;
|
child->argv = glob_target->gl_pathv;
|
||||||
}
|
}
|
||||||
if (ctx->w == RES_FOR) {
|
if (ctx->w == RES_FOR) {
|
||||||
done_word(dest,ctx);
|
done_word(dest, ctx);
|
||||||
done_pipe(ctx,PIPE_SEQ);
|
done_pipe(ctx, PIPE_SEQ);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -2116,7 +2120,7 @@ static int done_command(struct p_context *ctx)
|
|||||||
return 0;
|
return 0;
|
||||||
} else if (prog) {
|
} else if (prog) {
|
||||||
pi->num_progs++;
|
pi->num_progs++;
|
||||||
debug_printf("done_command: num_progs incremented to %d\n",pi->num_progs);
|
debug_printf("done_command: num_progs incremented to %d\n", pi->num_progs);
|
||||||
} else {
|
} else {
|
||||||
debug_printf("done_command: initializing\n");
|
debug_printf("done_command: initializing\n");
|
||||||
}
|
}
|
||||||
@ -2228,10 +2232,10 @@ static FILE *generate_stream_from_list(struct pipe *head)
|
|||||||
}
|
}
|
||||||
_exit(run_list_real(head)); /* leaks memory */
|
_exit(run_list_real(head)); /* leaks memory */
|
||||||
}
|
}
|
||||||
debug_printf("forked child %d\n",pid);
|
debug_printf("forked child %d\n", pid);
|
||||||
close(channel[1]);
|
close(channel[1]);
|
||||||
pf = fdopen(channel[0],"r");
|
pf = fdopen(channel[0],"r");
|
||||||
debug_printf("pipe on FILE *%p\n",pf);
|
debug_printf("pipe on FILE *%p\n", pf);
|
||||||
return pf;
|
return pf;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2275,8 +2279,8 @@ static int process_command_subs(o_string *dest, struct p_context *ctx, struct in
|
|||||||
* to the KISS philosophy of this program. */
|
* to the KISS philosophy of this program. */
|
||||||
mark_closed(fileno(p));
|
mark_closed(fileno(p));
|
||||||
retcode = pclose(p);
|
retcode = pclose(p);
|
||||||
free_pipe_list(inner.list_head,0);
|
free_pipe_list(inner.list_head, 0);
|
||||||
debug_printf("pclosed, retcode=%d\n",retcode);
|
debug_printf("pclosed, retcode=%d\n", retcode);
|
||||||
/* XXX this process fails to trim a single trailing newline */
|
/* XXX this process fails to trim a single trailing newline */
|
||||||
return retcode;
|
return retcode;
|
||||||
}
|
}
|
||||||
@ -2303,8 +2307,8 @@ static int parse_group(o_string *dest, struct p_context *ctx,
|
|||||||
default:
|
default:
|
||||||
syntax(); /* really logic error */
|
syntax(); /* really logic error */
|
||||||
}
|
}
|
||||||
rcode = parse_stream(dest,&sub,input,endch);
|
rcode = parse_stream(dest, &sub, input, endch);
|
||||||
done_word(dest,&sub); /* finish off the final word in the subcontext */
|
done_word(dest, &sub); /* finish off the final word in the subcontext */
|
||||||
done_pipe(&sub, PIPE_SEQ); /* and the final command there, too */
|
done_pipe(&sub, PIPE_SEQ); /* and the final command there, too */
|
||||||
child->group = sub.list_head;
|
child->group = sub.list_head;
|
||||||
return rcode;
|
return rcode;
|
||||||
@ -2330,13 +2334,13 @@ static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *i
|
|||||||
int i, advance = 0;
|
int i, advance = 0;
|
||||||
char sep[] = " ";
|
char sep[] = " ";
|
||||||
int ch = input->peek(input); /* first character after the $ */
|
int ch = input->peek(input); /* first character after the $ */
|
||||||
debug_printf("handle_dollar: ch=%c\n",ch);
|
debug_printf("handle_dollar: ch=%c\n", ch);
|
||||||
if (isalpha(ch)) {
|
if (isalpha(ch)) {
|
||||||
b_addchr(dest, SPECIAL_VAR_SYMBOL);
|
b_addchr(dest, SPECIAL_VAR_SYMBOL);
|
||||||
ctx->child->sp++;
|
ctx->child->sp++;
|
||||||
while (ch = b_peek(input),isalnum(ch) || ch == '_') {
|
while (ch = b_peek(input), isalnum(ch) || ch == '_') {
|
||||||
b_getch(input);
|
b_getch(input);
|
||||||
b_addchr(dest,ch);
|
b_addchr(dest, ch);
|
||||||
}
|
}
|
||||||
b_addchr(dest, SPECIAL_VAR_SYMBOL);
|
b_addchr(dest, SPECIAL_VAR_SYMBOL);
|
||||||
} else if (isdigit(ch)) {
|
} else if (isdigit(ch)) {
|
||||||
@ -2347,7 +2351,7 @@ static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *i
|
|||||||
advance = 1;
|
advance = 1;
|
||||||
} else switch (ch) {
|
} else switch (ch) {
|
||||||
case '$':
|
case '$':
|
||||||
b_adduint(dest,getpid());
|
b_adduint(dest, getpid());
|
||||||
advance = 1;
|
advance = 1;
|
||||||
break;
|
break;
|
||||||
case '!':
|
case '!':
|
||||||
@ -2355,11 +2359,11 @@ static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *i
|
|||||||
advance = 1;
|
advance = 1;
|
||||||
break;
|
break;
|
||||||
case '?':
|
case '?':
|
||||||
b_adduint(dest,last_return_code);
|
b_adduint(dest, last_return_code);
|
||||||
advance = 1;
|
advance = 1;
|
||||||
break;
|
break;
|
||||||
case '#':
|
case '#':
|
||||||
b_adduint(dest,global_argc ? global_argc-1 : 0);
|
b_adduint(dest, global_argc ? global_argc-1 : 0);
|
||||||
advance = 1;
|
advance = 1;
|
||||||
break;
|
break;
|
||||||
case '{':
|
case '{':
|
||||||
@ -2371,7 +2375,7 @@ static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *i
|
|||||||
ch = b_getch(input);
|
ch = b_getch(input);
|
||||||
if (ch == EOF || ch == '}')
|
if (ch == EOF || ch == '}')
|
||||||
break;
|
break;
|
||||||
b_addchr(dest,ch);
|
b_addchr(dest, ch);
|
||||||
}
|
}
|
||||||
if (ch != '}') {
|
if (ch != '}') {
|
||||||
syntax();
|
syntax();
|
||||||
@ -2395,11 +2399,11 @@ static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *i
|
|||||||
case '-':
|
case '-':
|
||||||
case '_':
|
case '_':
|
||||||
/* still unhandled, but should be eventually */
|
/* still unhandled, but should be eventually */
|
||||||
bb_error_msg("unhandled syntax: $%c",ch);
|
bb_error_msg("unhandled syntax: $%c", ch);
|
||||||
return 1;
|
return 1;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
b_addqchr(dest,'$',dest->quote);
|
b_addqchr(dest,'$', dest->quote);
|
||||||
}
|
}
|
||||||
/* Eat the character if the flag was set. If the compiler
|
/* Eat the character if the flag was set. If the compiler
|
||||||
* is smart enough, we could substitute "b_getch(input);"
|
* is smart enough, we could substitute "b_getch(input);"
|
||||||
@ -2430,7 +2434,7 @@ int parse_stream(o_string *dest, struct p_context *ctx,
|
|||||||
* A single-quote triggers a bypass of the main loop until its mate is
|
* A single-quote triggers a bypass of the main loop until its mate is
|
||||||
* found. When recursing, quote state is passed in via dest->quote. */
|
* found. When recursing, quote state is passed in via dest->quote. */
|
||||||
|
|
||||||
debug_printf("parse_stream, end_trigger=%d\n",end_trigger);
|
debug_printf("parse_stream, end_trigger=%d\n", end_trigger);
|
||||||
while ((ch = b_getch(input)) != EOF) {
|
while ((ch = b_getch(input)) != EOF) {
|
||||||
m = map[ch];
|
m = map[ch];
|
||||||
next = (ch == '\n') ? 0 : b_peek(input);
|
next = (ch == '\n') ? 0 : b_peek(input);
|
||||||
@ -2447,7 +2451,7 @@ int parse_stream(o_string *dest, struct p_context *ctx,
|
|||||||
/* If we aren't performing a substitution, treat a newline as a
|
/* If we aren't performing a substitution, treat a newline as a
|
||||||
* command separator. */
|
* command separator. */
|
||||||
if (end_trigger != '\0' && ch == '\n')
|
if (end_trigger != '\0' && ch == '\n')
|
||||||
done_pipe(ctx,PIPE_SEQ);
|
done_pipe(ctx, PIPE_SEQ);
|
||||||
}
|
}
|
||||||
if (ch == end_trigger && !dest->quote && ctx->w == RES_NONE) {
|
if (ch == end_trigger && !dest->quote && ctx->w == RES_NONE) {
|
||||||
debug_printf("leaving parse_stream (triggered)\n");
|
debug_printf("leaving parse_stream (triggered)\n");
|
||||||
@ -2485,7 +2489,7 @@ int parse_stream(o_string *dest, struct p_context *ctx,
|
|||||||
ch = b_getch(input);
|
ch = b_getch(input);
|
||||||
if (ch == EOF || ch == '\'')
|
if (ch == EOF || ch == '\'')
|
||||||
break;
|
break;
|
||||||
b_addchr(dest,ch);
|
b_addchr(dest, ch);
|
||||||
}
|
}
|
||||||
if (ch == EOF) {
|
if (ch == EOF) {
|
||||||
syntax();
|
syntax();
|
||||||
@ -2530,22 +2534,22 @@ int parse_stream(o_string *dest, struct p_context *ctx,
|
|||||||
break;
|
break;
|
||||||
case ';':
|
case ';':
|
||||||
done_word(dest, ctx);
|
done_word(dest, ctx);
|
||||||
done_pipe(ctx,PIPE_SEQ);
|
done_pipe(ctx, PIPE_SEQ);
|
||||||
break;
|
break;
|
||||||
case '&':
|
case '&':
|
||||||
done_word(dest, ctx);
|
done_word(dest, ctx);
|
||||||
if (next == '&') {
|
if (next == '&') {
|
||||||
b_getch(input);
|
b_getch(input);
|
||||||
done_pipe(ctx,PIPE_AND);
|
done_pipe(ctx, PIPE_AND);
|
||||||
} else {
|
} else {
|
||||||
done_pipe(ctx,PIPE_BG);
|
done_pipe(ctx, PIPE_BG);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case '|':
|
case '|':
|
||||||
done_word(dest, ctx);
|
done_word(dest, ctx);
|
||||||
if (next == '|') {
|
if (next == '|') {
|
||||||
b_getch(input);
|
b_getch(input);
|
||||||
done_pipe(ctx,PIPE_OR);
|
done_pipe(ctx, PIPE_OR);
|
||||||
} else {
|
} else {
|
||||||
/* we could pick up a file descriptor choice here
|
/* we could pick up a file descriptor choice here
|
||||||
* with redirect_opt_num(), but bash doesn't do it.
|
* with redirect_opt_num(), but bash doesn't do it.
|
||||||
@ -2623,7 +2627,7 @@ int parse_stream_outer(struct in_str *inp, int flag)
|
|||||||
}
|
}
|
||||||
if (rcode != 1 && ctx.old_flag == 0) {
|
if (rcode != 1 && ctx.old_flag == 0) {
|
||||||
done_word(&temp, &ctx);
|
done_word(&temp, &ctx);
|
||||||
done_pipe(&ctx,PIPE_SEQ);
|
done_pipe(&ctx, PIPE_SEQ);
|
||||||
run_list(ctx.list_head);
|
run_list(ctx.list_head);
|
||||||
} else {
|
} else {
|
||||||
if (ctx.old_flag != 0) {
|
if (ctx.old_flag != 0) {
|
||||||
@ -2661,9 +2665,10 @@ static int parse_file_outer(FILE *f)
|
|||||||
* we don't fight over who gets the foreground */
|
* we don't fight over who gets the foreground */
|
||||||
static void setup_job_control(void)
|
static void setup_job_control(void)
|
||||||
{
|
{
|
||||||
static pid_t shell_pgrp;
|
/*static --why?? */ pid_t shell_pgrp;
|
||||||
|
|
||||||
/* Loop until we are in the foreground. */
|
/* Loop until we are in the foreground. */
|
||||||
while (tcgetpgrp (shell_terminal) != (shell_pgrp = getpgrp ()))
|
while (tcgetpgrp(shell_terminal) != (shell_pgrp = getpgrp()))
|
||||||
kill(- shell_pgrp, SIGTTIN);
|
kill(- shell_pgrp, SIGTTIN);
|
||||||
|
|
||||||
/* Ignore interactive and job-control signals. */
|
/* Ignore interactive and job-control signals. */
|
||||||
@ -2689,7 +2694,7 @@ int hush_main(int argc, char **argv)
|
|||||||
{
|
{
|
||||||
int opt;
|
int opt;
|
||||||
FILE *input;
|
FILE *input;
|
||||||
char **e = environ;
|
char **e;
|
||||||
|
|
||||||
#if ENABLE_FEATURE_EDITING
|
#if ENABLE_FEATURE_EDITING
|
||||||
line_input_state = new_line_input_t(FOR_SHELL);
|
line_input_state = new_line_input_t(FOR_SHELL);
|
||||||
@ -2720,10 +2725,10 @@ int hush_main(int argc, char **argv)
|
|||||||
|
|
||||||
/* initialize our shell local variables with the values
|
/* initialize our shell local variables with the values
|
||||||
* currently living in the environment */
|
* currently living in the environment */
|
||||||
if (e) {
|
e = environ;
|
||||||
for (; *e; e++)
|
if (e)
|
||||||
set_local_var(*e, 2); /* without call putenv() */
|
while (*e)
|
||||||
}
|
set_local_var(*e++, 2); /* without call putenv() */
|
||||||
|
|
||||||
last_return_code = EXIT_SUCCESS;
|
last_return_code = EXIT_SUCCESS;
|
||||||
|
|
||||||
@ -2741,37 +2746,34 @@ int hush_main(int argc, char **argv)
|
|||||||
|
|
||||||
while ((opt = getopt(argc, argv, "c:xif")) > 0) {
|
while ((opt = getopt(argc, argv, "c:xif")) > 0) {
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 'c':
|
case 'c':
|
||||||
{
|
global_argv = argv+optind;
|
||||||
global_argv = argv+optind;
|
global_argc = argc-optind;
|
||||||
global_argc = argc-optind;
|
opt = parse_string_outer(optarg, FLAG_PARSE_SEMICOLON);
|
||||||
opt = parse_string_outer(optarg, FLAG_PARSE_SEMICOLON);
|
goto final_return;
|
||||||
goto final_return;
|
case 'i':
|
||||||
}
|
interactive++;
|
||||||
break;
|
break;
|
||||||
case 'i':
|
case 'f':
|
||||||
interactive++;
|
fake_mode++;
|
||||||
break;
|
break;
|
||||||
case 'f':
|
default:
|
||||||
fake_mode++;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
#ifndef BB_VER
|
#ifndef BB_VER
|
||||||
fprintf(stderr, "Usage: sh [FILE]...\n"
|
fprintf(stderr, "Usage: sh [FILE]...\n"
|
||||||
" or: sh -c command [args]...\n\n");
|
" or: sh -c command [args]...\n\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
#else
|
#else
|
||||||
bb_show_usage();
|
bb_show_usage();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* A shell is interactive if the `-i' flag was given, or if all of
|
/* A shell is interactive if the '-i' flag was given, or if all of
|
||||||
* the following conditions are met:
|
* the following conditions are met:
|
||||||
* no -c command
|
* no -c command
|
||||||
* no arguments remaining or the -s flag given
|
* no arguments remaining or the -s flag given
|
||||||
* standard input is a terminal
|
* standard input is a terminal
|
||||||
* standard output is a terminal
|
* standard output is a terminal
|
||||||
* Refer to Posix.2, the description of the `sh' utility. */
|
* Refer to Posix.2, the description of the 'sh' utility. */
|
||||||
if (argv[optind] == NULL && input == stdin
|
if (argv[optind] == NULL && input == stdin
|
||||||
&& isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)
|
&& isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)
|
||||||
) {
|
) {
|
||||||
@ -2802,7 +2804,7 @@ int hush_main(int argc, char **argv)
|
|||||||
|
|
||||||
#if ENABLE_FEATURE_CLEAN_UP
|
#if ENABLE_FEATURE_CLEAN_UP
|
||||||
fclose(input);
|
fclose(input);
|
||||||
if (cwd && cwd != bb_msg_unknown)
|
if (cwd != bb_msg_unknown)
|
||||||
free((char*)cwd);
|
free((char*)cwd);
|
||||||
{
|
{
|
||||||
struct variables *cur, *tmp;
|
struct variables *cur, *tmp;
|
||||||
@ -2885,7 +2887,7 @@ static char **make_list_in(char **inp, char *name)
|
|||||||
len = strlen(p1);
|
len = strlen(p1);
|
||||||
p2 = p1 + len;
|
p2 = p1 + len;
|
||||||
}
|
}
|
||||||
/* we use n + 2 in realloc for list,because we add
|
/* we use n + 2 in realloc for list, because we add
|
||||||
* new element and then we will add NULL element */
|
* new element and then we will add NULL element */
|
||||||
list = xrealloc(list, sizeof(*list) * (n + 2));
|
list = xrealloc(list, sizeof(*list) * (n + 2));
|
||||||
list[n] = xmalloc(2 + name_len + len);
|
list[n] = xmalloc(2 + name_len + len);
|
||||||
|
Loading…
Reference in New Issue
Block a user