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
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
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
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;
}
void mapset(const unsigned char *set, int code)
{
const unsigned char *s;
for (s=set; *s; s++) map[*s] = code;
}
void update_ifs_map(void)
{
/* char *ifs and char map[256] are both globals. */
ifs = getenv("IFS");
if (ifs == NULL) ifs=" \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("\\$'\"`", 3); /* never flow through */
mapset("<>;&|(){}#", 1); /* flow through if quoted */
#else
mapset("\\$'\"", 3); /* never flow through */
mapset(";&|#", 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() */
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 = 0;
#endif
do {
ctx.type = flag;
initialize_context(&ctx);
update_ifs_map();
if (!(flag & FLAG_PARSE_SEMICOLON) || (flag & FLAG_REPARSING)) mapset(";$&|", 0);
inp->promptmode=1;
rcode = parse_stream(&temp, &ctx, inp, '\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)
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
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);
} while (rcode != -1 && !(flag & FLAG_EXIT_FROM_LOOP)); /* loop on syntax errors, return on EOF */
#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(char *s, int flag)
#endif /* __U_BOOT__ */
{
struct in_str input;
#ifdef __U_BOOT__
char *p = NULL;
int rcode;
if ( !s || !*s)
return 1;
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__
static void u_boot_hush_reloc(void)
{
DECLARE_GLOBAL_DATA_PTR;
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->next = 0;
top_vars->flg_export = 0;
top_vars->flg_read_only = 1;
u_boot_hush_reloc();
}
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
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)
3327
3328
3329
3330
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
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
{
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 **argv)
{
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
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
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
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
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
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)
{
int res_str_len = 0;
int len;
int done = 0;
char *p, *p1, *res_str = NULL;
while ((p = strchr(inp, SPECIAL_VAR_SYMBOL))) {
if (p != inp) {
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;
p = strchr(inp, SPECIAL_VAR_SYMBOL);
*p = '\0';
if ((p1 = lookup_param(inp))) {
len = res_str_len + strlen(p1);
res_str = xrealloc(res_str, (1 + len));
strcpy((res_str + res_str_len), p1);
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) {
if ((*p1 == ' ')) {
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 */
static char * make_string(char ** inp)
{
char *p;
char *str = NULL;
int n;
int len = 2;
for (n = 0; inp[n]; n++) {
p = insert_var_value(inp[n]);
str = xrealloc(str, (len + strlen(p)));
if (n) {
strcat(str, " ");
} else {
*str = '\0';
}
strcat(str, p);
len = strlen(str) + 3;
if (p != inp[n]) free(p);
}
len = strlen(str);
*(str + len) = '\n';
*(str + len + 1) = '\0';
return str;
}
#endif /* CFG_HUSH_PARSER */
/****************************************************************************/