Newer
Older
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
if (m!=2) switch (ch) {
case '#':
if (dest->length == 0 && !dest->quote) {
while(ch=b_peek(input),ch!=EOF && ch!='\n') { b_getch(input); }
} else {
b_addqchr(dest, ch, dest->quote);
}
break;
case '\\':
if (next == EOF) {
syntax();
return 1;
}
b_addqchr(dest, '\\', dest->quote);
b_addqchr(dest, b_getch(input), dest->quote);
break;
case '$':
if (handle_dollar(dest, ctx, input)!=0) return 1;
break;
case '\'':
dest->nonnull = 1;
while(ch=b_getch(input),ch!=EOF && ch!='\'') {
#ifdef __U_BOOT__
if(input->__promptme == 0) return 1;
#endif
b_addchr(dest,ch);
}
if (ch==EOF) {
syntax();
return 1;
}
break;
case '"':
dest->nonnull = 1;
dest->quote = !dest->quote;
break;
#ifndef __U_BOOT__
case '`':
process_command_subs(dest, ctx, input, '`');
break;
case '>':
redir_fd = redirect_opt_num(dest);
done_word(dest, ctx);
redir_style=REDIRECT_OVERWRITE;
if (next == '>') {
redir_style=REDIRECT_APPEND;
b_getch(input);
} else if (next == '(') {
syntax(); /* until we support >(list) Process Substitution */
return 1;
}
setup_redirect(ctx, redir_fd, redir_style, input);
break;
case '<':
redir_fd = redirect_opt_num(dest);
done_word(dest, ctx);
redir_style=REDIRECT_INPUT;
if (next == '<') {
redir_style=REDIRECT_HEREIS;
b_getch(input);
} else if (next == '>') {
redir_style=REDIRECT_IO;
b_getch(input);
} else if (next == '(') {
syntax(); /* until we support <(list) Process Substitution */
return 1;
}
setup_redirect(ctx, redir_fd, redir_style, input);
break;
#endif
case ';':
done_word(dest, ctx);
done_pipe(ctx,PIPE_SEQ);
break;
case '&':
done_word(dest, ctx);
if (next=='&') {
b_getch(input);
done_pipe(ctx,PIPE_AND);
} else {
#ifndef __U_BOOT__
done_pipe(ctx,PIPE_BG);
#else
syntax_err();
return 1;
#endif
}
break;
case '|':
done_word(dest, ctx);
if (next=='|') {
b_getch(input);
done_pipe(ctx,PIPE_OR);
} else {
/* we could pick up a file descriptor choice here
* with redirect_opt_num(), but bash doesn't do it.
* "echo foo 2| cat" yields "foo 2". */
#ifndef __U_BOOT__
done_command(ctx);
#else
syntax_err();
return 1;
#endif
}
break;
#ifndef __U_BOOT__
case '(':
case '{':
if (parse_group(dest, ctx, input, ch)!=0) return 1;
break;
case ')':
case '}':
syntax(); /* Proper use of this character caught by end_trigger */
return 1;
break;
#endif
case SUBSTED_VAR_SYMBOL:
dest->nonnull = 1;
while (ch = b_getch(input), ch != EOF &&
ch != SUBSTED_VAR_SYMBOL) {
debug_printf("subst, pass=%d\n", ch);
if (input->__promptme == 0)
return 1;
b_addchr(dest, ch);
}
debug_printf("subst, term=%d\n", ch);
if (ch == EOF) {
syntax();
return 1;
}
break;
default:
syntax(); /* this is really an internal logic error */
return 1;
}
}
}
/* complain if quote? No, maybe we just finished a command substitution
* that was quoted. Example:
* $ echo "`cat foo` plus more"
* and we just got the EOF generated by the subshell that ran "cat foo"
* The only real complaint is if we got an EOF when end_trigger != '\0',
* that is, we were really supposed to get end_trigger, and never got
* one before the EOF. Can't use the standard "syntax error" return code,
* so that parse_stream_outer can distinguish the EOF and exit smoothly. */
debug_printf("leaving parse_stream (EOF)\n");
if (end_trigger != '\0') return -1;
return 0;
}
static void mapset(const unsigned char *set, int code)
{
const unsigned char *s;
for (s=set; *s; s++) map[*s] = code;
}
{
/* char *ifs and char map[256] are both globals. */
ifs = (uchar *)getenv("IFS");
if (ifs == NULL) ifs=(uchar *)" \t\n";
/* Precompute a list of 'flow through' behavior so it can be treated
* quickly up front. Computation is necessary because of IFS.
* Special case handling of IFS == " \t\n" is not implemented.
* The map[] array only really needs two bits each, and on most machines
* that would be faster because of the reduced L1 cache footprint.
*/
memset(map,0,sizeof(map)); /* most characters flow through always */
#ifndef __U_BOOT__
mapset((uchar *)"\\$'\"`", 3); /* never flow through */
mapset((uchar *)"<>;&|(){}#", 1); /* flow through if quoted */
{
uchar subst[2] = {SUBSTED_VAR_SYMBOL, 0};
mapset(subst, 3); /* never flow through */
}
mapset((uchar *)"\\$'\"", 3); /* never flow through */
mapset((uchar *)";&|#", 1); /* flow through if quoted */
#endif
mapset(ifs, 2); /* also flow through if quoted */
}
/* most recursion does not come through here, the exeception is
* from builtin_source() */
static int parse_stream_outer(struct in_str *inp, int flag)
{
struct p_context ctx;
o_string temp=NULL_O_STRING;
int rcode;
#ifdef __U_BOOT__
int code = 1;
#endif
do {
ctx.type = flag;
initialize_context(&ctx);
update_ifs_map();
if (!(flag & FLAG_PARSE_SEMICOLON) || (flag & FLAG_REPARSING)) mapset((uchar *)";$&|", 0);
rcode = parse_stream(&temp, &ctx, inp,
flag & FLAG_CONT_ON_NEWLINE ? -1 : '\n');
#ifdef __U_BOOT__
if (rcode == 1) flag_repeat = 0;
#endif
if (rcode != 1 && ctx.old_flag != 0) {
syntax();
#ifdef __U_BOOT__
flag_repeat = 0;
#endif
}
if (rcode != 1 && ctx.old_flag == 0) {
done_word(&temp, &ctx);
done_pipe(&ctx,PIPE_SEQ);
#ifndef __U_BOOT__
run_list(ctx.list_head);
#else
code = run_list(ctx.list_head);
if (code == -2) { /* exit */
b_free(&temp);
code = 0;
/* XXX hackish way to not allow exit from main loop */
if (inp->peek == file_peek) {
printf("exit not allowed from main input shell.\n");
continue;
}
break;
}
if (code == -1)
flag_repeat = 0;
#endif
} else {
if (ctx.old_flag != 0) {
free(ctx.stack);
b_reset(&temp);
}
#ifdef __U_BOOT__
if (inp->__promptme == 0) printf("<INTERRUPT>\n");
inp->__promptme = 1;
#endif
temp.nonnull = 0;
temp.quote = 0;
inp->p = NULL;
free_pipe_list(ctx.list_head,0);
}
b_free(&temp);
/* loop on syntax errors, return on EOF */
} while (rcode != -1 && !(flag & FLAG_EXIT_FROM_LOOP) &&
(inp->peek != static_peek || b_peek(inp)));
#ifndef __U_BOOT__
return 0;
#else
return (code != 0) ? 1 : 0;
#endif /* __U_BOOT__ */
}
#ifndef __U_BOOT__
static int parse_string_outer(const char *s, int flag)
#else
int parse_string_outer(const char *s, int flag)
#endif /* __U_BOOT__ */
{
struct in_str input;
#ifdef __U_BOOT__
char *p = NULL;
int rcode;
if (!*s)
return 0;
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
if (!(p = strchr(s, '\n')) || *++p) {
p = xmalloc(strlen(s) + 2);
strcpy(p, s);
strcat(p, "\n");
setup_string_in_str(&input, p);
rcode = parse_stream_outer(&input, flag);
free(p);
return rcode;
} else {
#endif
setup_string_in_str(&input, s);
return parse_stream_outer(&input, flag);
#ifdef __U_BOOT__
}
#endif
}
#ifndef __U_BOOT__
static int parse_file_outer(FILE *f)
#else
int parse_file_outer(void)
#endif
{
int rcode;
struct in_str input;
#ifndef __U_BOOT__
setup_file_in_str(&input, f);
#else
setup_file_in_str(&input);
#endif
rcode = parse_stream_outer(&input, FLAG_PARSE_SEMICOLON);
return rcode;
}
#ifdef __U_BOOT__
#ifdef CONFIG_NEEDS_MANUAL_RELOC
static void u_boot_hush_reloc(void)
{
unsigned long addr;
struct reserved_combo *r;
for (r=reserved_list; r<reserved_list+NRES; r++) {
addr = (ulong) (r->literal) + gd->reloc_off;
r->literal = (char *)addr;
}
}
if (top_vars == NULL) {
top_vars = malloc(sizeof(struct variables));
top_vars->name = "HUSH_VERSION";
top_vars->value = "0.01";
top_vars->flg_export = 0;
top_vars->flg_read_only = 1;
#ifdef CONFIG_NEEDS_MANUAL_RELOC
u_boot_hush_reloc();
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
return 0;
}
static void *xmalloc(size_t size)
{
void *p = NULL;
if (!(p = malloc(size))) {
printf("ERROR : memory not allocated\n");
for(;;);
}
return p;
}
static void *xrealloc(void *ptr, size_t size)
{
void *p = NULL;
if (!(p = realloc(ptr, size))) {
printf("ERROR : memory not allocated\n");
for(;;);
}
return p;
}
#endif /* __U_BOOT__ */
#ifndef __U_BOOT__
/* Make sure we have a controlling tty. If we get started under a job
* aware app (like bash for example), make sure we are now in charge so
* we don't fight over who gets the foreground */
static void setup_job_control(void)
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
{
static pid_t shell_pgrp;
/* Loop until we are in the foreground. */
while (tcgetpgrp (shell_terminal) != (shell_pgrp = getpgrp ()))
kill (- shell_pgrp, SIGTTIN);
/* Ignore interactive and job-control signals. */
signal(SIGINT, SIG_IGN);
signal(SIGQUIT, SIG_IGN);
signal(SIGTERM, SIG_IGN);
signal(SIGTSTP, SIG_IGN);
signal(SIGTTIN, SIG_IGN);
signal(SIGTTOU, SIG_IGN);
signal(SIGCHLD, SIG_IGN);
/* Put ourselves in our own process group. */
setsid();
shell_pgrp = getpid ();
setpgid (shell_pgrp, shell_pgrp);
/* Grab control of the terminal. */
tcsetpgrp(shell_terminal, shell_pgrp);
}
int hush_main(int argc, char * const *argv)
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
{
int opt;
FILE *input;
char **e = environ;
/* XXX what should these be while sourcing /etc/profile? */
global_argc = argc;
global_argv = argv;
/* (re?) initialize globals. Sometimes hush_main() ends up calling
* hush_main(), therefore we cannot rely on the BSS to zero out this
* stuff. Reset these to 0 every time. */
ifs = NULL;
/* map[] is taken care of with call to update_ifs_map() */
fake_mode = 0;
interactive = 0;
close_me_head = NULL;
last_bg_pid = 0;
job_list = NULL;
last_jobid = 0;
/* Initialize some more globals to non-zero values */
set_cwd();
#ifdef CONFIG_FEATURE_COMMAND_EDITING
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
cmdedit_set_initial_prompt();
#else
PS1 = NULL;
#endif
PS2 = "> ";
/* initialize our shell local variables with the values
* currently living in the environment */
if (e) {
for (; *e; e++)
set_local_var(*e, 2); /* without call putenv() */
}
last_return_code=EXIT_SUCCESS;
if (argv[0] && argv[0][0] == '-') {
debug_printf("\nsourcing /etc/profile\n");
if ((input = fopen("/etc/profile", "r")) != NULL) {
mark_open(fileno(input));
parse_file_outer(input);
mark_closed(fileno(input));
fclose(input);
}
}
input=stdin;
while ((opt = getopt(argc, argv, "c:xif")) > 0) {
switch (opt) {
case 'c':
{
global_argv = argv+optind;
global_argc = argc-optind;
opt = parse_string_outer(optarg, FLAG_PARSE_SEMICOLON);
goto final_return;
}
break;
case 'i':
interactive++;
break;
case 'f':
fake_mode++;
break;
default:
#ifndef BB_VER
fprintf(stderr, "Usage: sh [FILE]...\n"
" or: sh -c command [args]...\n\n");
exit(EXIT_FAILURE);
#else
show_usage();
#endif
}
}
/* A shell is interactive if the `-i' flag was given, or if all of
* the following conditions are met:
* no -c command
* no arguments remaining or the -s flag given
* standard input is a terminal
* standard output is a terminal
* Refer to Posix.2, the description of the `sh' utility. */
if (argv[optind]==NULL && input==stdin &&
isatty(fileno(stdin)) && isatty(fileno(stdout))) {
interactive++;
}
debug_printf("\ninteractive=%d\n", interactive);
if (interactive) {
/* Looks like they want an interactive shell */
printf( "\n\n" BB_BANNER " hush - the humble shell v0.01 (testing)\n");
printf( "Enter 'help' for a list of built-in commands.\n\n");
#endif
setup_job_control();
}
if (argv[optind]==NULL) {
opt=parse_file_outer(stdin);
goto final_return;
}
debug_printf("\nrunning script '%s'\n", argv[optind]);
global_argv = argv+optind;
global_argc = argc-optind;
input = xfopen(argv[optind], "r");
opt = parse_file_outer(input);
#ifdef CONFIG_FEATURE_CLEAN_UP
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
fclose(input);
if (cwd && cwd != unknown)
free((char*)cwd);
{
struct variables *cur, *tmp;
for(cur = top_vars; cur; cur = tmp) {
tmp = cur->next;
if (!cur->flg_read_only) {
free(cur->name);
free(cur->value);
free(cur);
}
}
}
#endif
final_return:
return(opt?opt:last_return_code);
}
#endif
static char *insert_var_value(char *inp)
{
return insert_var_value_sub(inp, 0);
}
static char *insert_var_value_sub(char *inp, int tag_subst)
{
int res_str_len = 0;
int len;
int done = 0;
char *p, *p1, *res_str = NULL;
while ((p = strchr(inp, SPECIAL_VAR_SYMBOL))) {
/* check the beginning of the string for normal characters */
/* copy any characters to the result string */
len = p - inp;
res_str = xrealloc(res_str, (res_str_len + len));
strncpy((res_str + res_str_len), inp, len);
res_str_len += len;
}
inp = ++p;
/* find the ending marker */
/* look up the value to substitute */
if (tag_subst)
len = res_str_len + strlen(p1) + 2;
else
len = res_str_len + strlen(p1);
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
if (tag_subst) {
/*
* copy the variable value to the result
* string
*/
strcpy((res_str + res_str_len + 1), p1);
/*
* mark the replaced text to be accepted as
* is
*/
res_str[res_str_len] = SUBSTED_VAR_SYMBOL;
res_str[res_str_len + 1 + strlen(p1)] =
SUBSTED_VAR_SYMBOL;
} else
/*
* copy the variable value to the result
* string
*/
strcpy((res_str + res_str_len), p1);
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
res_str_len = len;
}
*p = SPECIAL_VAR_SYMBOL;
inp = ++p;
done = 1;
}
if (done) {
res_str = xrealloc(res_str, (1 + res_str_len + strlen(inp)));
strcpy((res_str + res_str_len), inp);
while ((p = strchr(res_str, '\n'))) {
*p = ' ';
}
}
return (res_str == NULL) ? inp : res_str;
}
static char **make_list_in(char **inp, char *name)
{
int len, i;
int name_len = strlen(name);
int n = 0;
char **list;
char *p1, *p2, *p3;
/* create list of variable values */
list = xmalloc(sizeof(*list));
for (i = 0; inp[i]; i++) {
p3 = insert_var_value(inp[i]);
p1 = p3;
while (*p1) {
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
p1++;
continue;
}
if ((p2 = strchr(p1, ' '))) {
len = p2 - p1;
} else {
len = strlen(p1);
p2 = p1 + len;
}
/* we use n + 2 in realloc for list,because we add
* new element and then we will add NULL element */
list = xrealloc(list, sizeof(*list) * (n + 2));
list[n] = xmalloc(2 + name_len + len);
strcpy(list[n], name);
strcat(list[n], "=");
strncat(list[n], p1, len);
list[n++][name_len + len + 1] = '\0';
p1 = p2;
}
if (p3 != inp[i]) free(p3);
}
list[n] = NULL;
return list;
}
/*
* Make new string for parser
* inp - array of argument strings to flatten
* nonnull - indicates argument was quoted when originally parsed
*/
static char *make_string(char **inp, int *nonnull)
{
char *p;
char *str = NULL;
int n;
int len = 2;
char *noeval_str;
int noeval = 0;
noeval_str = get_local_var("HUSH_NO_EVAL");
if (noeval_str != NULL && *noeval_str != '0' && *noeval_str != '\0')
noeval = 1;
p = insert_var_value_sub(inp[n], noeval);
str = xrealloc(str, (len + strlen(p) + (2 * nonnull[n])));
if (n) {
strcat(str, " ");
} else {
*str = '\0';
}
if (nonnull[n])
strcat(str, "'");
if (nonnull[n])
strcat(str, "'");
len = strlen(str) + 3;
if (p != inp[n]) free(p);
}
len = strlen(str);
*(str + len) = '\n';
*(str + len + 1) = '\0';
return str;
}
static int do_showvar(cmd_tbl_t *cmdtp, int flag, int argc,
char * const argv[])
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
{
int i, k;
int rcode = 0;
struct variables *cur;
if (argc == 1) { /* Print all env variables */
for (cur = top_vars; cur; cur = cur->next) {
printf ("%s=%s\n", cur->name, cur->value);
if (ctrlc ()) {
puts ("\n ** Abort\n");
return 1;
}
}
return 0;
}
for (i = 1; i < argc; ++i) { /* print single env variables */
char *name = argv[i];
k = -1;
for (cur = top_vars; cur; cur = cur->next) {
if(strcmp (cur->name, name) == 0) {
k = 0;
printf ("%s=%s\n", cur->name, cur->value);
}
if (ctrlc ()) {
puts ("\n ** Abort\n");
return 1;
}
}
if (k < 0) {
printf ("## Error: \"%s\" not defined\n", name);
rcode ++;
}
}
return rcode;
}
U_BOOT_CMD(
showvar, CONFIG_SYS_MAXARGS, 1, do_showvar,
"\n - print values of all hushshell variables\n"
"showvar name ...\n"
" - print value of hushshell variable 'name'"
/****************************************************************************/