Newer
Older
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
#ifndef __U_BOOT__
if (fake_mode==0) {
#endif
rcode = run_list_real(pi);
#ifndef __U_BOOT__
}
#endif
/* free_pipe_list has the side effect of clearing memory
* In the long run that function can be merged with run_list_real,
* but doing that now would hobble the debugging effort. */
free_pipe_list(pi,0);
return rcode;
}
/* The API for glob is arguably broken. This routine pushes a non-matching
* string into the output structure, removing non-backslashed backslashes.
* If someone can prove me wrong, by performing this function within the
* original glob(3) api, feel free to rewrite this routine into oblivion.
* Return code (0 vs. GLOB_NOSPACE) matches glob(3).
* XXX broken if the last character is '\\', check that before calling.
*/
#ifndef __U_BOOT__
static int globhack(const char *src, int flags, glob_t *pglob)
{
int cnt=0, pathc;
const char *s;
char *dest;
for (cnt=1, s=src; s && *s; s++) {
if (*s == '\\') s++;
cnt++;
}
dest = malloc(cnt);
if (!dest) return GLOB_NOSPACE;
if (!(flags & GLOB_APPEND)) {
pglob->gl_pathv=NULL;
pglob->gl_pathc=0;
pglob->gl_offs=0;
pglob->gl_offs=0;
}
pathc = ++pglob->gl_pathc;
pglob->gl_pathv = realloc(pglob->gl_pathv, (pathc+1)*sizeof(*pglob->gl_pathv));
if (pglob->gl_pathv == NULL) return GLOB_NOSPACE;
pglob->gl_pathv[pathc-1]=dest;
pglob->gl_pathv[pathc]=NULL;
for (s=src; s && *s; s++, dest++) {
if (*s == '\\') s++;
*dest = *s;
}
*dest='\0';
return 0;
}
/* XXX broken if the last character is '\\', check that before calling */
static int glob_needed(const char *s)
{
for (; *s; s++) {
if (*s == '\\') s++;
if (strchr("*[?",*s)) return 1;
}
return 0;
}
#if 0
static void globprint(glob_t *pglob)
{
int i;
debug_printf("glob_t at %p:\n", pglob);
debug_printf(" gl_pathc=%d gl_pathv=%p gl_offs=%d gl_flags=%d\n",
pglob->gl_pathc, pglob->gl_pathv, pglob->gl_offs, pglob->gl_flags);
for (i=0; i<pglob->gl_pathc; i++)
debug_printf("pglob->gl_pathv[%d] = %p = %s\n", i,
pglob->gl_pathv[i], pglob->gl_pathv[i]);
}
#endif
static int xglob(o_string *dest, int flags, glob_t *pglob)
{
int gr;
/* we can code this better when the debug_printf's are gone */
if (dest->length == 0) {
if (dest->nonnull) {
/* bash man page calls this an "explicit" null */
gr = globhack(dest->data, flags, pglob);
debug_printf("globhack returned %d\n",gr);
} else {
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
gr = glob(dest->data, flags, NULL, pglob);
debug_printf("glob returned %d\n",gr);
if (gr == GLOB_NOMATCH) {
/* quote removal, or more accurately, backslash removal */
gr = globhack(dest->data, flags, pglob);
debug_printf("globhack returned %d\n",gr);
}
} else {
gr = globhack(dest->data, flags, pglob);
debug_printf("globhack returned %d\n",gr);
}
if (gr == GLOB_NOSPACE)
error_msg_and_die("out of memory during glob");
if (gr != 0) { /* GLOB_ABORTED ? */
error_msg("glob(3) error %d",gr);
}
/* globprint(glob_target); */
return gr;
}
#endif
#ifdef __U_BOOT__
static char *get_dollar_var(char ch);
#endif
char *get_local_var(const char *s)
{
struct variables *cur;
if (!s)
return NULL;
#ifdef __U_BOOT__
if (*s == '$')
return get_dollar_var(s[1]);
#endif
for (cur = top_vars; cur; cur=cur->next)
if(strcmp(cur->name, s)==0)
return cur->value;
return NULL;
}
/* This is used to set local shell variables
flg_export==0 if only local (not exporting) variable
flg_export==1 if "new" exporting environ
flg_export>1 if current startup environ (not call putenv()) */
int set_local_var(const char *s, int flg_export)
{
char *name, *value;
int result=0;
struct variables *cur;
#ifdef __U_BOOT__
/* might be possible! */
if (!isalpha(*s))
return -1;
#endif
name=strdup(s);
#ifdef __U_BOOT__
if (getenv(name) != NULL) {
printf ("ERROR: "
"There is a global environment variable with the same name.\n");
return -1;
}
#endif
/* Assume when we enter this function that we are already in
* NAME=VALUE format. So the first order of business is to
* split 's' on the '=' into 'name' and 'value' */
value = strchr(name, '=');
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
free(name);
return -1;
}
*value++ = 0;
for(cur = top_vars; cur; cur = cur->next) {
if(strcmp(cur->name, name)==0)
break;
}
if(cur) {
if(strcmp(cur->value, value)==0) {
if(flg_export>0 && cur->flg_export==0)
cur->flg_export=flg_export;
else
result++;
} else {
if(cur->flg_read_only) {
error_msg("%s: readonly variable", name);
result = -1;
} else {
if(flg_export>0 || cur->flg_export>1)
cur->flg_export=1;
free(cur->value);
cur->value = strdup(value);
}
}
} else {
cur = malloc(sizeof(struct variables));
if(!cur) {
result = -1;
} else {
cur->name = strdup(name);
free(cur);
result = -1;
} else {
struct variables *bottom = top_vars;
cur->value = strdup(value);
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
cur->flg_export = flg_export;
cur->flg_read_only = 0;
while(bottom->next) bottom=bottom->next;
bottom->next = cur;
}
}
}
#ifndef __U_BOOT__
if(result==0 && cur->flg_export==1) {
*(value-1) = '=';
result = putenv(name);
} else {
#endif
free(name);
#ifndef __U_BOOT__
if(result>0) /* equivalent to previous set */
result = 0;
}
#endif
return result;
}
void unset_local_var(const char *name)
{
struct variables *cur;
if (name) {
for (cur = top_vars; cur; cur=cur->next) {
if(strcmp(cur->name, name)==0)
break;
}
struct variables *next = top_vars;
if(cur->flg_read_only) {
error_msg("%s: readonly variable", name);
return;
} else {
free(cur->name);
free(cur->value);
while (next->next != cur)
next = next->next;
next->next = cur->next;
}
free(cur);
}
}
}
static int is_assignment(const char *s)
{
if (s == NULL)
return 0;
if (!isalpha(*s)) return 0;
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
++s;
while(isalnum(*s) || *s=='_') ++s;
return *s=='=';
}
#ifndef __U_BOOT__
/* the src parameter allows us to peek forward to a possible &n syntax
* for file descriptor duplication, e.g., "2>&1".
* Return code is 0 normally, 1 if a syntax error is detected in src.
* Resource errors (in xmalloc) cause the process to exit */
static int setup_redirect(struct p_context *ctx, int fd, redir_type style,
struct in_str *input)
{
struct child_prog *child=ctx->child;
struct redir_struct *redir = child->redirects;
struct redir_struct *last_redir=NULL;
/* Create a new redir_struct and drop it onto the end of the linked list */
while(redir) {
last_redir=redir;
redir=redir->next;
}
redir = xmalloc(sizeof(struct redir_struct));
redir->next=NULL;
redir->word.gl_pathv=NULL;
if (last_redir) {
last_redir->next=redir;
} else {
child->redirects=redir;
}
redir->type=style;
redir->fd= (fd==-1) ? redir_table[style].default_fd : fd ;
debug_printf("Redirect type %d%s\n", redir->fd, redir_table[style].descrip);
/* Check for a '2>&1' type redirect */
redir->dup = redirect_dup_num(input);
if (redir->dup == -2) return 1; /* syntax error */
if (redir->dup != -1) {
/* Erik had a check here that the file descriptor in question
* is legit; I postpone that to "run time"
* A "-" representation of "close me" shows up as a -3 here */
debug_printf("Duplicating redirect '%d>&%d'\n", redir->fd, redir->dup);
} else {
/* We do _not_ try to open the file that src points to,
* since we need to return and let src be expanded first.
* Set ctx->pending_redirect, so we know what to do at the
* end of the next parsed word.
*/
ctx->pending_redirect = redir;
}
return 0;
}
#endif
struct pipe *pi;
pi = xmalloc(sizeof(struct pipe));
pi->num_progs = 0;
pi->progs = NULL;
pi->next = NULL;
pi->followup = 0; /* invalid */
pi->r_mode = RES_NONE;
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
return pi;
}
static void initialize_context(struct p_context *ctx)
{
ctx->pipe=NULL;
#ifndef __U_BOOT__
ctx->pending_redirect=NULL;
#endif
ctx->child=NULL;
ctx->list_head=new_pipe();
ctx->pipe=ctx->list_head;
ctx->w=RES_NONE;
ctx->stack=NULL;
#ifdef __U_BOOT__
ctx->old_flag=0;
#endif
done_command(ctx); /* creates the memory for working child */
}
/* normal return is 0
* if a reserved word is found, and processed, return 1
* should handle if, then, elif, else, fi, for, while, until, do, done.
* case, function, and select are obnoxious, save those for later.
*/
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
struct reserved_combo {
char *literal;
int code;
long flag;
};
/* Mostly a list of accepted follow-up reserved words.
* FLAG_END means we are done with the sequence, and are ready
* to turn the compound list into a command.
* FLAG_START means the word must start a new compound list.
*/
static struct reserved_combo reserved_list[] = {
{ "if", RES_IF, FLAG_THEN | FLAG_START },
{ "then", RES_THEN, FLAG_ELIF | FLAG_ELSE | FLAG_FI },
{ "elif", RES_ELIF, FLAG_THEN },
{ "else", RES_ELSE, FLAG_FI },
{ "fi", RES_FI, FLAG_END },
{ "for", RES_FOR, FLAG_IN | FLAG_START },
{ "while", RES_WHILE, FLAG_DO | FLAG_START },
{ "until", RES_UNTIL, FLAG_DO | FLAG_START },
{ "in", RES_IN, FLAG_DO },
{ "do", RES_DO, FLAG_DONE },
{ "done", RES_DONE, FLAG_END }
};
#define NRES (sizeof(reserved_list)/sizeof(struct reserved_combo))
static int reserved_word(o_string *dest, struct p_context *ctx)
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
{
struct reserved_combo *r;
for (r=reserved_list;
r<reserved_list+NRES; r++) {
if (strcmp(dest->data, r->literal) == 0) {
debug_printf("found reserved word %s, code %d\n",r->literal,r->code);
if (r->flag & FLAG_START) {
struct p_context *new = xmalloc(sizeof(struct p_context));
debug_printf("push stack\n");
if (ctx->w == RES_IN || ctx->w == RES_FOR) {
syntax();
free(new);
ctx->w = RES_SNTX;
b_reset(dest);
return 1;
}
*new = *ctx; /* physical copy */
initialize_context(ctx);
ctx->stack=new;
} else if ( ctx->w == RES_NONE || ! (ctx->old_flag & (1<<r->code))) {
syntax();
ctx->w = RES_SNTX;
b_reset(dest);
return 1;
}
ctx->w=r->code;
ctx->old_flag = r->flag;
if (ctx->old_flag & FLAG_END) {
struct p_context *old;
debug_printf("pop stack\n");
done_pipe(ctx,PIPE_SEQ);
old = ctx->stack;
old->child->group = ctx->list_head;
#ifndef __U_BOOT__
old->child->subshell = 0;
#endif
*ctx = *old; /* physical copy */
free(old);
}
b_reset (dest);
return 1;
}
}
return 0;
}
/* normal return is 0.
* Syntax or xglob errors return 1. */
static int done_word(o_string *dest, struct p_context *ctx)
{
struct child_prog *child=ctx->child;
#ifndef __U_BOOT__
glob_t *glob_target;
int gr, flags = 0;
#else
char *str, *s;
int argc, cnt;
#endif
debug_printf("done_word: %s %p\n", dest->data, child);
if (dest->length == 0 && !dest->nonnull) {
debug_printf(" true null, ignored\n");
return 0;
}
#ifndef __U_BOOT__
if (ctx->pending_redirect) {
glob_target = &ctx->pending_redirect->word;
} else {
#endif
if (child->group) {
syntax();
return 1; /* syntax error, groups and arglists don't mix */
}
if (!child->argv && (ctx->type & FLAG_PARSE_SEMICOLON)) {
debug_printf("checking %s for reserved-ness\n",dest->data);
if (reserved_word(dest,ctx)) return ctx->w==RES_SNTX;
}
#ifndef __U_BOOT__
glob_target = &child->glob_result;
#else
for (cnt = 1, s = dest->data; s && *s; s++) {
if (*s == '\\') s++;
cnt++;
}
str = malloc(cnt);
if (!str) return 1;
if ( child->argv == NULL) {
child->argc=0;
}
argc = ++child->argc;
child->argv = realloc(child->argv, (argc+1)*sizeof(*child->argv));
if (child->argv == NULL) return 1;
child->argv_nonnull = realloc(child->argv_nonnull,
(argc+1)*sizeof(*child->argv_nonnull));
if (child->argv_nonnull == NULL)
return 1;
child->argv_nonnull[argc-1] = dest->nonnull;
child->argv_nonnull[argc] = 0;
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
for (s = dest->data; s && *s; s++,str++) {
if (*s == '\\') s++;
*str = *s;
}
*str = '\0';
#endif
#ifndef __U_BOOT__
}
gr = xglob(dest, flags, glob_target);
if (gr != 0) return 1;
#endif
b_reset(dest);
#ifndef __U_BOOT__
if (ctx->pending_redirect) {
ctx->pending_redirect=NULL;
if (glob_target->gl_pathc != 1) {
error_msg("ambiguous redirect");
return 1;
}
} else {
child->argv = glob_target->gl_pathv;
}
#endif
if (ctx->w == RES_FOR) {
done_word(dest,ctx);
done_pipe(ctx,PIPE_SEQ);
}
return 0;
}
/* The only possible error here is out of memory, in which case
* xmalloc exits. */
static int done_command(struct p_context *ctx)
{
/* The child is really already in the pipe structure, so
* advance the pipe counter and make a new, null child.
* Only real trickiness here is that the uncommitted
* child structure, to which ctx->child points, is not
* counted in pi->num_progs. */
struct pipe *pi=ctx->pipe;
struct child_prog *prog=ctx->child;
if (prog && prog->group == NULL
#else
) {
#endif
debug_printf("done_command: skipping null command\n");
return 0;
} else if (prog) {
pi->num_progs++;
debug_printf("done_command: num_progs incremented to %d\n",pi->num_progs);
} else {
debug_printf("done_command: initializing\n");
}
pi->progs = xrealloc(pi->progs, sizeof(*pi->progs) * (pi->num_progs+1));
prog = pi->progs + pi->num_progs;
#ifndef __U_BOOT__
prog->redirects = NULL;
#endif
prog->argv = NULL;
prog->argv_nonnull = NULL;
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
#ifndef __U_BOOT__
prog->is_stopped = 0;
#endif
prog->group = NULL;
#ifndef __U_BOOT__
prog->glob_result.gl_pathv = NULL;
prog->family = pi;
#endif
prog->sp = 0;
ctx->child = prog;
prog->type = ctx->type;
/* but ctx->pipe and ctx->list_head remain unchanged */
return 0;
}
static int done_pipe(struct p_context *ctx, pipe_style type)
{
struct pipe *new_p;
done_command(ctx); /* implicit closure of previous command */
debug_printf("done_pipe, type %d\n", type);
ctx->pipe->followup = type;
ctx->pipe->r_mode = ctx->w;
new_p=new_pipe();
ctx->pipe->next = new_p;
ctx->pipe = new_p;
ctx->child = NULL;
done_command(ctx); /* set up new pipe to accept commands */
return 0;
}
#ifndef __U_BOOT__
/* peek ahead in the in_str to find out if we have a "&n" construct,
* as in "2>&1", that represents duplicating a file descriptor.
* returns either -2 (syntax error), -1 (no &), or the number found.
*/
static int redirect_dup_num(struct in_str *input)
{
int ch, d=0, ok=0;
ch = b_peek(input);
if (ch != '&') return -1;
b_getch(input); /* get the & */
ch=b_peek(input);
if (ch == '-') {
b_getch(input);
return -3; /* "-" represents "close me" */
}
while (isdigit(ch)) {
d = d*10+(ch-'0');
ok=1;
b_getch(input);
ch = b_peek(input);
}
if (ok) return d;
error_msg("ambiguous redirect");
return -2;
}
/* If a redirect is immediately preceded by a number, that number is
* supposed to tell which file descriptor to redirect. This routine
* looks for such preceding numbers. In an ideal world this routine
* needs to handle all the following classes of redirects...
* echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo
* echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo
* echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo
* echo 49x>foo # redirects fd 1 to file "foo", "49x" passed to echo
* A -1 output from this program means no valid number was found, so the
* caller should use the appropriate default for this redirection.
*/
static int redirect_opt_num(o_string *o)
{
int num;
if (o->length==0) return -1;
for(num=0; num<o->length; num++) {
if (!isdigit(*(o->data+num))) {
return -1;
}
}
/* reuse num (and save an int) */
num=atoi(o->data);
b_reset(o);
return num;
}
FILE *generate_stream_from_list(struct pipe *head)
{
FILE *pf;
#if 1
int pid, channel[2];
if (pipe(channel)<0) perror_msg_and_die("pipe");
pid=fork();
if (pid<0) {
perror_msg_and_die("fork");
} else if (pid==0) {
close(channel[0]);
if (channel[1] != 1) {
dup2(channel[1],1);
close(channel[1]);
}
#if 0
#define SURROGATE "surrogate response"
write(1,SURROGATE,sizeof(SURROGATE));
_exit(run_list(head));
#else
_exit(run_list_real(head)); /* leaks memory */
#endif
}
debug_printf("forked child %d\n",pid);
close(channel[1]);
pf = fdopen(channel[0],"r");
debug_printf("pipe on FILE *%p\n",pf);
#else
free_pipe_list(head,0);
pf=popen("echo surrogate response","r");
debug_printf("started fake pipe on FILE *%p\n",pf);
#endif
return pf;
}
/* this version hacked for testing purposes */
/* return code is exit status of the process that is run. */
static int process_command_subs(o_string *dest, struct p_context *ctx, struct in_str *input, int subst_end)
{
int retcode;
o_string result=NULL_O_STRING;
struct p_context inner;
FILE *p;
struct in_str pipe_str;
initialize_context(&inner);
/* recursion to generate command */
retcode = parse_stream(&result, &inner, input, subst_end);
if (retcode != 0) return retcode; /* syntax error or EOF */
done_word(&result, &inner);
done_pipe(&inner, PIPE_SEQ);
b_free(&result);
p=generate_stream_from_list(inner.list_head);
if (p==NULL) return 1;
mark_open(fileno(p));
setup_file_in_str(&pipe_str, p);
/* now send results of command back into original context */
retcode = parse_stream(dest, ctx, &pipe_str, '\0');
/* XXX In case of a syntax error, should we try to kill the child?
* That would be tough to do right, so just read until EOF. */
if (retcode == 1) {
while (b_getch(&pipe_str)!=EOF) { /* discard */ };
}
debug_printf("done reading from pipe, pclose()ing\n");
/* This is the step that wait()s for the child. Should be pretty
* safe, since we just read an EOF from its stdout. We could try
* to better, by using wait(), and keeping track of background jobs
* at the same time. That would be a lot of work, and contrary
* to the KISS philosophy of this program. */
mark_closed(fileno(p));
retcode=pclose(p);
free_pipe_list(inner.list_head,0);
debug_printf("pclosed, retcode=%d\n",retcode);
/* XXX this process fails to trim a single trailing newline */
return retcode;
}
static int parse_group(o_string *dest, struct p_context *ctx,
struct in_str *input, int ch)
{
int rcode, endch=0;
struct p_context sub;
struct child_prog *child = ctx->child;
if (child->argv) {
syntax();
return 1; /* syntax error, groups and arglists don't mix */
}
initialize_context(&sub);
switch(ch) {
case '(': endch=')'; child->subshell=1; break;
case '{': endch='}'; break;
default: syntax(); /* really logic error */
}
rcode=parse_stream(dest,&sub,input,endch);
done_word(dest,&sub); /* finish off the final word in the subcontext */
done_pipe(&sub, PIPE_SEQ); /* and the final command there, too */
child->group = sub.list_head;
return rcode;
/* child remains "open", available for possible redirects */
}
#endif
/* basically useful version until someone wants to get fancier,
* see the bash man page under "Parameter Expansion" */
static char *lookup_param(char *src)
{
char *sep;
char *default_val = NULL;
int assign = 0;
int expand_empty = 0;
if (!src)
return NULL;
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
sep = strchr(src, ':');
if (sep) {
*sep = '\0';
if (*(sep + 1) == '-')
default_val = sep+2;
if (*(sep + 1) == '=') {
default_val = sep+2;
assign = 1;
}
if (*(sep + 1) == '+') {
default_val = sep+2;
expand_empty = 1;
}
}
p = getenv(src);
if (!p)
p = get_local_var(src);
if (!p || strlen(p) == 0) {
p = default_val;
if (assign) {
char *var = malloc(strlen(src)+strlen(default_val)+2);
if (var) {
sprintf(var, "%s=%s", src, default_val);
set_local_var(var, 0);
}
free(var);
}
} else if (expand_empty) {
p += strlen(p);
}
if (sep)
*sep = ':';
#ifdef __U_BOOT__
static char *get_dollar_var(char ch)
{
static char buf[40];
buf[0] = '\0';
switch (ch) {
case '?':
sprintf(buf, "%u", (unsigned int)last_return_code);
break;
default:
return NULL;
}
return buf;
}
#endif
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
/* return code: 0 for OK, 1 for syntax error */
static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *input)
{
#ifndef __U_BOOT__
int i, advance=0;
#else
int advance=0;
#endif
#ifndef __U_BOOT__
char sep[]=" ";
#endif
int ch = input->peek(input); /* first character after the $ */
debug_printf("handle_dollar: ch=%c\n",ch);
if (isalpha(ch)) {
b_addchr(dest, SPECIAL_VAR_SYMBOL);
ctx->child->sp++;
while(ch=b_peek(input),isalnum(ch) || ch=='_') {
b_getch(input);
b_addchr(dest,ch);
}
b_addchr(dest, SPECIAL_VAR_SYMBOL);
#ifndef __U_BOOT__
} else if (isdigit(ch)) {
i = ch-'0'; /* XXX is $0 special? */
if (i<global_argc) {
parse_string(dest, ctx, global_argv[i]); /* recursion */
}
advance = 1;
#endif
} else switch (ch) {
#ifndef __U_BOOT__
case '$':
b_adduint(dest,getpid());
advance = 1;
break;
case '!':
if (last_bg_pid > 0) b_adduint(dest, last_bg_pid);
advance = 1;
break;
#endif
case '?':
#else
ctx->child->sp++;
b_addchr(dest, SPECIAL_VAR_SYMBOL);
b_addchr(dest, '$');
b_addchr(dest, '?');
b_addchr(dest, SPECIAL_VAR_SYMBOL);
#endif
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
advance = 1;
break;
#ifndef __U_BOOT__
case '#':
b_adduint(dest,global_argc ? global_argc-1 : 0);
advance = 1;
break;
#endif
case '{':
b_addchr(dest, SPECIAL_VAR_SYMBOL);
ctx->child->sp++;
b_getch(input);
/* XXX maybe someone will try to escape the '}' */
while(ch=b_getch(input),ch!=EOF && ch!='}') {
b_addchr(dest,ch);
}
if (ch != '}') {
syntax();
return 1;
}
b_addchr(dest, SPECIAL_VAR_SYMBOL);
break;
#ifndef __U_BOOT__
case '(':
b_getch(input);
process_command_subs(dest, ctx, input, ')');
break;
case '*':
sep[0]=ifs[0];
for (i=1; i<global_argc; i++) {
parse_string(dest, ctx, global_argv[i]);
if (i+1 < global_argc) parse_string(dest, ctx, sep);
}
break;
case '@':
case '-':
case '_':
/* still unhandled, but should be eventually */
error_msg("unhandled syntax: $%c",ch);
return 1;
break;
#endif
default:
b_addqchr(dest,'$',dest->quote);
}
/* Eat the character if the flag was set. If the compiler
* is smart enough, we could substitute "b_getch(input);"
* for all the "advance = 1;" above, and also end up with
* a nice size-optimized program. Hah! That'll be the day.
*/
if (advance) b_getch(input);
return 0;
}
#ifndef __U_BOOT__
int parse_string(o_string *dest, struct p_context *ctx, const char *src)
{
struct in_str foo;
setup_string_in_str(&foo, src);
return parse_stream(dest, ctx, &foo, '\0');
}
#endif
/* return code is 0 for normal exit, 1 for syntax error */
static int parse_stream(o_string *dest, struct p_context *ctx,
struct in_str *input, int end_trigger)
{
unsigned int ch, m;
#ifndef __U_BOOT__
int redir_fd;
redir_type redir_style;
#endif
int next;
/* Only double-quote state is handled in the state variable dest->quote.
* 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. */
debug_printf("parse_stream, end_trigger=%d\n",end_trigger);
while ((ch=b_getch(input))!=EOF) {
m = map[ch];
#ifdef __U_BOOT__
if (input->__promptme == 0) return 1;
#endif
next = (ch == '\n') ? 0 : b_peek(input);
debug_printf("parse_stream: ch=%c (%d) m=%d quote=%d - %c\n",
ch >= ' ' ? ch : '.', ch, m,
dest->quote, ctx->stack == NULL ? '*' : '.');
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
if (m==0 || ((m==1 || m==2) && dest->quote)) {
b_addqchr(dest, ch, dest->quote);
} else {
if (m==2) { /* unquoted IFS */
if (done_word(dest, ctx)) {
return 1;
}
/* If we aren't performing a substitution, treat a newline as a
* command separator. */
if (end_trigger != '\0' && ch=='\n')
done_pipe(ctx,PIPE_SEQ);
}
if (ch == end_trigger && !dest->quote && ctx->w==RES_NONE) {
debug_printf("leaving parse_stream (triggered)\n");
return 0;
}
#if 0
if (ch=='\n') {
/* Yahoo! Time to run with it! */
done_pipe(ctx,PIPE_SEQ);
run_list(ctx->list_head);
initialize_context(ctx);
}
#endif
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);