blob: d3304f49e45a670c6498c46a81d2fd217b8fd7bb [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkfe8c2802002-11-03 00:38:21 +00002/*
3 * sh.c -- a prototype Bourne shell grammar parser
4 * Intended to follow the original Thompson and Ritchie
5 * "small and simple is beautiful" philosophy, which
6 * incidentally is a good match to today's BusyBox.
7 *
8 * Copyright (C) 2000,2001 Larry Doolittle <larry@doolittle.boa.org>
9 *
10 * Credits:
11 * The parser routines proper are all original material, first
12 * written Dec 2000 and Jan 2001 by Larry Doolittle.
13 * The execution engine, the builtins, and much of the underlying
14 * support has been adapted from busybox-0.49pre's lash,
15 * which is Copyright (C) 2000 by Lineo, Inc., and
16 * written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>.
17 * That, in turn, is based in part on ladsh.c, by Michael K. Johnson and
18 * Erik W. Troan, which they placed in the public domain. I don't know
19 * how much of the Johnson/Troan code has survived the repeated rewrites.
20 * Other credits:
wdenkfe8c2802002-11-03 00:38:21 +000021 * b_addchr() derived from similar w_addchar function in glibc-2.2
22 * setup_redirect(), redirect_opt_num(), and big chunks of main()
23 * and many builtins derived from contributions by Erik Andersen
24 * miscellaneous bugfixes from Matt Kraai
25 *
26 * There are two big (and related) architecture differences between
27 * this parser and the lash parser. One is that this version is
28 * actually designed from the ground up to understand nearly all
29 * of the Bourne grammar. The second, consequential change is that
30 * the parser and input reader have been turned inside out. Now,
31 * the parser is in control, and asks for input as needed. The old
32 * way had the input reader in control, and it asked for parsing to
33 * take place as needed. The new way makes it much easier to properly
34 * handle the recursion implicit in the various substitutions, especially
35 * across continuation lines.
36 *
37 * Bash grammar not implemented: (how many of these were in original sh?)
38 * $@ (those sure look like weird quoting rules)
39 * $_
40 * ! negation operator for pipes
41 * &> and >& redirection of stdout+stderr
42 * Brace Expansion
43 * Tilde Expansion
44 * fancy forms of Parameter Expansion
45 * aliases
46 * Arithmetic Expansion
47 * <(list) and >(list) Process Substitution
48 * reserved words: case, esac, select, function
49 * Here Documents ( << word )
50 * Functions
51 * Major bugs:
52 * job handling woefully incomplete and buggy
53 * reserved word execution woefully incomplete and buggy
54 * to-do:
55 * port selected bugfixes from post-0.49 busybox lash - done?
56 * finish implementing reserved words: for, while, until, do, done
57 * change { and } from special chars to reserved words
58 * builtins: break, continue, eval, return, set, trap, ulimit
59 * test magic exec
60 * handle children going into background
61 * clean up recognition of null pipes
62 * check setting of global_argc and global_argv
63 * control-C handling, probably with longjmp
64 * follow IFS rules more precisely, including update semantics
65 * figure out what to do with backslash-newline
66 * explain why we use signal instead of sigaction
67 * propagate syntax errors, die on resource errors?
68 * continuation lines, both explicit and implicit - done?
69 * memory leak finding and plugging - done?
70 * more testing, especially quoting rules and redirection
71 * document how quoting rules not precisely followed for variable assignments
72 * maybe change map[] to use 2-bit entries
73 * (eventually) remove all the printf's
wdenkfe8c2802002-11-03 00:38:21 +000074 */
Wolfgang Denk1a459662013-07-08 09:37:19 +020075
wdenkfe8c2802002-11-03 00:38:21 +000076#define __U_BOOT__
77#ifdef __U_BOOT__
Heinrich Schuchardt68c09122019-10-26 23:45:08 +020078#include <common.h> /* readline */
Simon Glass9fb625c2019-08-01 09:46:51 -060079#include <env.h>
wdenkfe8c2802002-11-03 00:38:21 +000080#include <malloc.h> /* malloc, free, realloc*/
81#include <linux/ctype.h> /* isalpha, isdigit */
Simon Glass24b852a2015-11-08 23:47:45 -070082#include <console.h>
Simon Glass0098e172014-04-10 20:01:30 -060083#include <bootretry.h>
Simon Glass18d66532014-04-10 20:01:25 -060084#include <cli.h>
Simon Glasseca86fa2014-04-10 20:01:24 -060085#include <cli_hush.h>
wdenkfe8c2802002-11-03 00:38:21 +000086#include <command.h> /* find_cmd */
Simon Glass401d1c42020-10-30 21:38:53 -060087#include <asm/global_data.h>
wdenkfe8c2802002-11-03 00:38:21 +000088#endif
wdenkfe8c2802002-11-03 00:38:21 +000089#ifndef __U_BOOT__
90#include <ctype.h> /* isalpha, isdigit */
91#include <unistd.h> /* getpid */
92#include <stdlib.h> /* getenv, atoi */
93#include <string.h> /* strchr */
94#include <stdio.h> /* popen etc. */
95#include <glob.h> /* glob, of course */
96#include <stdarg.h> /* va_list */
97#include <errno.h>
98#include <fcntl.h>
99#include <getopt.h> /* should be pretty obvious */
100
101#include <sys/stat.h> /* ulimit */
102#include <sys/types.h>
103#include <sys/wait.h>
104#include <signal.h>
105
106/* #include <dmalloc.h> */
wdenkfe8c2802002-11-03 00:38:21 +0000107
wdenkd0fb80c2003-01-11 09:48:40 +0000108#if 1
wdenkfe8c2802002-11-03 00:38:21 +0000109#include "busybox.h"
110#include "cmdedit.h"
111#else
112#define applet_name "hush"
113#include "standalone.h"
114#define hush_main main
wdenkd0fb80c2003-01-11 09:48:40 +0000115#undef CONFIG_FEATURE_SH_FANCY_PROMPT
116#define BB_BANNER
wdenkfe8c2802002-11-03 00:38:21 +0000117#endif
118#endif
119#define SPECIAL_VAR_SYMBOL 03
Joe Hershbergera005f192012-08-17 10:26:30 +0000120#define SUBSTED_VAR_SYMBOL 04
wdenkfe8c2802002-11-03 00:38:21 +0000121#ifndef __U_BOOT__
122#define FLAG_EXIT_FROM_LOOP 1
123#define FLAG_PARSE_SEMICOLON (1 << 1) /* symbol ';' is special for parser */
124#define FLAG_REPARSING (1 << 2) /* >= 2nd pass */
125
126#endif
127
xia.jin291268e2024-06-14 08:27:14 +0000128#ifdef CONFIG_ARMV8_MULTIENTRY
129#include <spinlock.h>
130#include <asm/arch-meson/smp.h>
131static spin_lock_t cmd_lock = {.lock = UNLOCK };
132static int lock_holder = -1;
133static int lock_depth;
134#endif
135
wdenkfe8c2802002-11-03 00:38:21 +0000136#ifdef __U_BOOT__
Wolfgang Denkd87080b2006-03-31 18:32:53 +0200137DECLARE_GLOBAL_DATA_PTR;
138
wdenkfe8c2802002-11-03 00:38:21 +0000139#define EXIT_SUCCESS 0
140#define EOF -1
141#define syntax() syntax_err()
142#define xstrdup strdup
143#define error_msg printf
144#else
145typedef enum {
146 REDIRECT_INPUT = 1,
147 REDIRECT_OVERWRITE = 2,
148 REDIRECT_APPEND = 3,
149 REDIRECT_HEREIS = 4,
150 REDIRECT_IO = 5
151} redir_type;
152
153/* The descrip member of this structure is only used to make debugging
154 * output pretty */
155struct {int mode; int default_fd; char *descrip;} redir_table[] = {
156 { 0, 0, "()" },
157 { O_RDONLY, 0, "<" },
158 { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" },
159 { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" },
160 { O_RDONLY, -1, "<<" },
161 { O_RDWR, 1, "<>" }
162};
163#endif
164
165typedef enum {
166 PIPE_SEQ = 1,
167 PIPE_AND = 2,
168 PIPE_OR = 3,
169 PIPE_BG = 4,
170} pipe_style;
171
172/* might eventually control execution */
173typedef enum {
174 RES_NONE = 0,
175 RES_IF = 1,
176 RES_THEN = 2,
177 RES_ELIF = 3,
178 RES_ELSE = 4,
179 RES_FI = 5,
180 RES_FOR = 6,
181 RES_WHILE = 7,
182 RES_UNTIL = 8,
183 RES_DO = 9,
184 RES_DONE = 10,
185 RES_XXXX = 11,
186 RES_IN = 12,
187 RES_SNTX = 13
188} reserved_style;
189#define FLAG_END (1<<RES_NONE)
190#define FLAG_IF (1<<RES_IF)
191#define FLAG_THEN (1<<RES_THEN)
192#define FLAG_ELIF (1<<RES_ELIF)
193#define FLAG_ELSE (1<<RES_ELSE)
194#define FLAG_FI (1<<RES_FI)
195#define FLAG_FOR (1<<RES_FOR)
196#define FLAG_WHILE (1<<RES_WHILE)
197#define FLAG_UNTIL (1<<RES_UNTIL)
198#define FLAG_DO (1<<RES_DO)
199#define FLAG_DONE (1<<RES_DONE)
200#define FLAG_IN (1<<RES_IN)
201#define FLAG_START (1<<RES_XXXX)
202
203/* This holds pointers to the various results of parsing */
204struct p_context {
205 struct child_prog *child;
206 struct pipe *list_head;
207 struct pipe *pipe;
208#ifndef __U_BOOT__
209 struct redir_struct *pending_redirect;
210#endif
211 reserved_style w;
212 int old_flag; /* for figuring out valid reserved words */
213 struct p_context *stack;
214 int type; /* define type of parser : ";$" common or special symbol */
215 /* How about quoting status? */
216};
217
218#ifndef __U_BOOT__
219struct redir_struct {
220 redir_type type; /* type of redirection */
221 int fd; /* file descriptor being redirected */
222 int dup; /* -1, or file descriptor being duplicated */
223 struct redir_struct *next; /* pointer to the next redirect in the list */
224 glob_t word; /* *word.gl_pathv is the filename */
225};
226#endif
227
228struct child_prog {
229#ifndef __U_BOOT__
230 pid_t pid; /* 0 if exited */
231#endif
232 char **argv; /* program name and arguments */
Stephen Warrenfe9ca3d2014-03-01 22:16:10 -0700233 /* was quoted when parsed; copy of struct o_string.nonnull field */
Simon Glasseca86fa2014-04-10 20:01:24 -0600234 int *argv_nonnull;
wdenkfe8c2802002-11-03 00:38:21 +0000235#ifdef __U_BOOT__
236 int argc; /* number of program arguments */
237#endif
238 struct pipe *group; /* if non-NULL, first in group or subshell */
239#ifndef __U_BOOT__
240 int subshell; /* flag, non-zero if group must be forked */
241 struct redir_struct *redirects; /* I/O redirections */
242 glob_t glob_result; /* result of parameter globbing */
243 int is_stopped; /* is the program currently running? */
244 struct pipe *family; /* pointer back to the child's parent pipe */
245#endif
246 int sp; /* number of SPECIAL_VAR_SYMBOL */
247 int type;
248};
249
250struct pipe {
251#ifndef __U_BOOT__
252 int jobid; /* job number */
253#endif
254 int num_progs; /* total number of programs in job */
255#ifndef __U_BOOT__
256 int running_progs; /* number of programs running */
257 char *text; /* name of job */
258 char *cmdbuf; /* buffer various argv's point into */
259 pid_t pgrp; /* process group ID for the job */
260#endif
261 struct child_prog *progs; /* array of commands in pipe */
262 struct pipe *next; /* to track background commands */
263#ifndef __U_BOOT__
264 int stopped_progs; /* number of programs alive, but stopped */
265 int job_context; /* bitmask defining current context */
266#endif
267 pipe_style followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */
268 reserved_style r_mode; /* supports if, for, while, until */
269};
270
271#ifndef __U_BOOT__
272struct close_me {
273 int fd;
274 struct close_me *next;
275};
276#endif
277
278struct variables {
279 char *name;
280 char *value;
281 int flg_export;
282 int flg_read_only;
283 struct variables *next;
284};
285
286/* globals, connect us to the outside world
287 * the first three support $?, $#, and $1 */
288#ifndef __U_BOOT__
289char **global_argv;
290unsigned int global_argc;
291#endif
Kim Phillips199adb62012-10-29 13:34:32 +0000292static unsigned int last_return_code;
wdenkfe8c2802002-11-03 00:38:21 +0000293#ifndef __U_BOOT__
294extern char **environ; /* This is in <unistd.h>, but protected with __USE_GNU */
295#endif
296
297/* "globals" within this file */
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200298static uchar *ifs;
wdenkfe8c2802002-11-03 00:38:21 +0000299static char map[256];
300#ifndef __U_BOOT__
301static int fake_mode;
302static int interactive;
303static struct close_me *close_me_head;
304static const char *cwd;
305static struct pipe *job_list;
306static unsigned int last_bg_pid;
307static unsigned int last_jobid;
308static unsigned int shell_terminal;
309static char *PS1;
310static char *PS2;
311struct variables shell_ver = { "HUSH_VERSION", "0.01", 1, 1, 0 };
312struct variables *top_vars = &shell_ver;
313#else
314static int flag_repeat = 0;
315static int do_repeat = 0;
wdenk2d5b5612003-10-14 19:43:55 +0000316static struct variables *top_vars = NULL ;
wdenkfe8c2802002-11-03 00:38:21 +0000317#endif /*__U_BOOT__ */
318
319#define B_CHUNK (100)
320#define B_NOSPAC 1
321
322typedef struct {
323 char *data;
324 int length;
325 int maxlen;
326 int quote;
327 int nonnull;
328} o_string;
329#define NULL_O_STRING {NULL,0,0,0,0}
330/* used for initialization:
331 o_string foo = NULL_O_STRING; */
332
333/* I can almost use ordinary FILE *. Is open_memstream() universally
334 * available? Where is it documented? */
335struct in_str {
336 const char *p;
337#ifndef __U_BOOT__
338 char peek_buf[2];
339#endif
340 int __promptme;
341 int promptmode;
342#ifndef __U_BOOT__
343 FILE *file;
344#endif
345 int (*get) (struct in_str *);
346 int (*peek) (struct in_str *);
347};
348#define b_getch(input) ((input)->get(input))
349#define b_peek(input) ((input)->peek(input))
350
351#ifndef __U_BOOT__
352#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
353
354struct built_in_command {
355 char *cmd; /* name */
356 char *descr; /* description */
357 int (*function) (struct child_prog *); /* function ptr */
358};
359#endif
360
Stefan Roese4cacf7c2008-08-19 14:57:55 +0200361/* define DEBUG_SHELL for debugging output (obviously ;-)) */
362#if 0
363#define DEBUG_SHELL
364#endif
365
wdenkfe8c2802002-11-03 00:38:21 +0000366/* This should be in utility.c */
367#ifdef DEBUG_SHELL
368#ifndef __U_BOOT__
369static void debug_printf(const char *format, ...)
370{
371 va_list args;
372 va_start(args, format);
373 vfprintf(stderr, format, args);
374 va_end(args);
375}
376#else
Stefan Roese4cacf7c2008-08-19 14:57:55 +0200377#define debug_printf(fmt,args...) printf (fmt ,##args)
wdenkfe8c2802002-11-03 00:38:21 +0000378#endif
379#else
380static inline void debug_printf(const char *format, ...) { }
381#endif
382#define final_printf debug_printf
383
384#ifdef __U_BOOT__
385static void syntax_err(void) {
386 printf("syntax error\n");
387}
388#else
389static void __syntax(char *file, int line) {
390 error_msg("syntax error %s:%d", file, line);
391}
392#define syntax() __syntax(__FILE__, __LINE__)
393#endif
394
395#ifdef __U_BOOT__
396static void *xmalloc(size_t size);
397static void *xrealloc(void *ptr, size_t size);
398#else
399/* Index of subroutines: */
400/* function prototypes for builtins */
401static int builtin_cd(struct child_prog *child);
402static int builtin_env(struct child_prog *child);
403static int builtin_eval(struct child_prog *child);
404static int builtin_exec(struct child_prog *child);
405static int builtin_exit(struct child_prog *child);
406static int builtin_export(struct child_prog *child);
407static int builtin_fg_bg(struct child_prog *child);
408static int builtin_help(struct child_prog *child);
409static int builtin_jobs(struct child_prog *child);
410static int builtin_pwd(struct child_prog *child);
411static int builtin_read(struct child_prog *child);
412static int builtin_set(struct child_prog *child);
413static int builtin_shift(struct child_prog *child);
414static int builtin_source(struct child_prog *child);
415static int builtin_umask(struct child_prog *child);
416static int builtin_unset(struct child_prog *child);
417static int builtin_not_written(struct child_prog *child);
418#endif
419/* o_string manipulation: */
420static int b_check_space(o_string *o, int len);
421static int b_addchr(o_string *o, int ch);
422static void b_reset(o_string *o);
423static int b_addqchr(o_string *o, int ch, int quote);
wdenkc26e4542004-04-18 10:13:26 +0000424#ifndef __U_BOOT__
wdenkfe8c2802002-11-03 00:38:21 +0000425static int b_adduint(o_string *o, unsigned int i);
wdenkc26e4542004-04-18 10:13:26 +0000426#endif
wdenkfe8c2802002-11-03 00:38:21 +0000427/* in_str manipulations: */
428static int static_get(struct in_str *i);
429static int static_peek(struct in_str *i);
430static int file_get(struct in_str *i);
431static int file_peek(struct in_str *i);
432#ifndef __U_BOOT__
433static void setup_file_in_str(struct in_str *i, FILE *f);
434#else
435static void setup_file_in_str(struct in_str *i);
436#endif
437static void setup_string_in_str(struct in_str *i, const char *s);
438#ifndef __U_BOOT__
439/* close_me manipulations: */
440static void mark_open(int fd);
441static void mark_closed(int fd);
wdenkd0fb80c2003-01-11 09:48:40 +0000442static void close_all(void);
wdenkfe8c2802002-11-03 00:38:21 +0000443#endif
444/* "run" the final data structures: */
445static char *indenter(int i);
446static int free_pipe_list(struct pipe *head, int indent);
447static int free_pipe(struct pipe *pi, int indent);
448/* really run the final data structures: */
449#ifndef __U_BOOT__
450static int setup_redirects(struct child_prog *prog, int squirrel[]);
451#endif
452static int run_list_real(struct pipe *pi);
453#ifndef __U_BOOT__
454static void pseudo_exec(struct child_prog *child) __attribute__ ((noreturn));
455#endif
456static int run_pipe_real(struct pipe *pi);
457/* extended glob support: */
458#ifndef __U_BOOT__
459static int globhack(const char *src, int flags, glob_t *pglob);
460static int glob_needed(const char *s);
461static int xglob(o_string *dest, int flags, glob_t *pglob);
462#endif
463/* variable assignment: */
464static int is_assignment(const char *s);
465/* data structure manipulation: */
466#ifndef __U_BOOT__
467static int setup_redirect(struct p_context *ctx, int fd, redir_type style, struct in_str *input);
468#endif
469static void initialize_context(struct p_context *ctx);
470static int done_word(o_string *dest, struct p_context *ctx);
471static int done_command(struct p_context *ctx);
472static int done_pipe(struct p_context *ctx, pipe_style type);
473/* primary string parsing: */
474#ifndef __U_BOOT__
475static int redirect_dup_num(struct in_str *input);
476static int redirect_opt_num(o_string *o);
477static int process_command_subs(o_string *dest, struct p_context *ctx, struct in_str *input, int subst_end);
478static int parse_group(o_string *dest, struct p_context *ctx, struct in_str *input, int ch);
479#endif
480static char *lookup_param(char *src);
Stephen Warrenfe9ca3d2014-03-01 22:16:10 -0700481static char *make_string(char **inp, int *nonnull);
wdenkfe8c2802002-11-03 00:38:21 +0000482static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *input);
483#ifndef __U_BOOT__
484static int parse_string(o_string *dest, struct p_context *ctx, const char *src);
485#endif
486static int parse_stream(o_string *dest, struct p_context *ctx, struct in_str *input0, int end_trigger);
487/* setup: */
488static int parse_stream_outer(struct in_str *inp, int flag);
489#ifndef __U_BOOT__
490static int parse_string_outer(const char *s, int flag);
491static int parse_file_outer(FILE *f);
492#endif
493#ifndef __U_BOOT__
494/* job management: */
495static int checkjobs(struct pipe* fg_pipe);
496static void insert_bg_job(struct pipe *pi);
497static void remove_bg_job(struct pipe *pi);
498#endif
499/* local variable support */
500static char **make_list_in(char **inp, char *name);
501static char *insert_var_value(char *inp);
Joe Hershbergera005f192012-08-17 10:26:30 +0000502static char *insert_var_value_sub(char *inp, int tag_subst);
wdenkfe8c2802002-11-03 00:38:21 +0000503
504#ifndef __U_BOOT__
505/* Table of built-in functions. They can be forked or not, depending on
506 * context: within pipes, they fork. As simple commands, they do not.
507 * When used in non-forking context, they can change global variables
508 * in the parent shell process. If forked, of course they can not.
509 * For example, 'unset foo | whatever' will parse and run, but foo will
510 * still be set at the end. */
511static struct built_in_command bltins[] = {
512 {"bg", "Resume a job in the background", builtin_fg_bg},
513 {"break", "Exit for, while or until loop", builtin_not_written},
514 {"cd", "Change working directory", builtin_cd},
515 {"continue", "Continue for, while or until loop", builtin_not_written},
516 {"env", "Print all environment variables", builtin_env},
517 {"eval", "Construct and run shell command", builtin_eval},
518 {"exec", "Exec command, replacing this shell with the exec'd process",
519 builtin_exec},
520 {"exit", "Exit from shell()", builtin_exit},
521 {"export", "Set environment variable", builtin_export},
522 {"fg", "Bring job into the foreground", builtin_fg_bg},
523 {"jobs", "Lists the active jobs", builtin_jobs},
524 {"pwd", "Print current directory", builtin_pwd},
525 {"read", "Input environment variable", builtin_read},
526 {"return", "Return from a function", builtin_not_written},
527 {"set", "Set/unset shell local variables", builtin_set},
528 {"shift", "Shift positional parameters", builtin_shift},
529 {"trap", "Trap signals", builtin_not_written},
530 {"ulimit","Controls resource limits", builtin_not_written},
531 {"umask","Sets file creation mask", builtin_umask},
532 {"unset", "Unset environment variable", builtin_unset},
533 {".", "Source-in and run commands in a file", builtin_source},
534 {"help", "List shell built-in commands", builtin_help},
535 {NULL, NULL, NULL}
536};
537
538static const char *set_cwd(void)
539{
540 if(cwd==unknown)
541 cwd = NULL; /* xgetcwd(arg) called free(arg) */
542 cwd = xgetcwd((char *)cwd);
543 if (!cwd)
544 cwd = unknown;
545 return cwd;
546}
547
548/* built-in 'eval' handler */
549static int builtin_eval(struct child_prog *child)
550{
551 char *str = NULL;
552 int rcode = EXIT_SUCCESS;
553
554 if (child->argv[1]) {
555 str = make_string(child->argv + 1);
556 parse_string_outer(str, FLAG_EXIT_FROM_LOOP |
557 FLAG_PARSE_SEMICOLON);
558 free(str);
559 rcode = last_return_code;
560 }
561 return rcode;
562}
563
564/* built-in 'cd <path>' handler */
565static int builtin_cd(struct child_prog *child)
566{
567 char *newdir;
568 if (child->argv[1] == NULL)
Simon Glass00caae62017-08-03 12:22:12 -0600569 newdir = env_get("HOME");
wdenkfe8c2802002-11-03 00:38:21 +0000570 else
571 newdir = child->argv[1];
572 if (chdir(newdir)) {
573 printf("cd: %s: %s\n", newdir, strerror(errno));
574 return EXIT_FAILURE;
575 }
576 set_cwd();
577 return EXIT_SUCCESS;
578}
579
580/* built-in 'env' handler */
581static int builtin_env(struct child_prog *dummy)
582{
583 char **e = environ;
584 if (e == NULL) return EXIT_FAILURE;
585 for (; *e; e++) {
586 puts(*e);
587 }
588 return EXIT_SUCCESS;
589}
590
591/* built-in 'exec' handler */
592static int builtin_exec(struct child_prog *child)
593{
594 if (child->argv[1] == NULL)
595 return EXIT_SUCCESS; /* Really? */
596 child->argv++;
597 pseudo_exec(child);
598 /* never returns */
599}
600
601/* built-in 'exit' handler */
602static int builtin_exit(struct child_prog *child)
603{
604 if (child->argv[1] == NULL)
605 exit(last_return_code);
606 exit (atoi(child->argv[1]));
607}
608
609/* built-in 'export VAR=value' handler */
610static int builtin_export(struct child_prog *child)
611{
612 int res = 0;
613 char *name = child->argv[1];
614
615 if (name == NULL) {
616 return (builtin_env(child));
617 }
618
619 name = strdup(name);
620
621 if(name) {
622 char *value = strchr(name, '=');
623
624 if (!value) {
625 char *tmp;
626 /* They are exporting something without an =VALUE */
627
628 value = get_local_var(name);
629 if (value) {
630 size_t ln = strlen(name);
631
632 tmp = realloc(name, ln+strlen(value)+2);
633 if(tmp==NULL)
634 res = -1;
635 else {
636 sprintf(tmp+ln, "=%s", value);
637 name = tmp;
638 }
639 } else {
640 /* bash does not return an error when trying to export
641 * an undefined variable. Do likewise. */
642 res = 1;
643 }
644 }
645 }
646 if (res<0)
647 perror_msg("export");
648 else if(res==0)
649 res = set_local_var(name, 1);
650 else
651 res = 0;
652 free(name);
653 return res;
654}
655
656/* built-in 'fg' and 'bg' handler */
657static int builtin_fg_bg(struct child_prog *child)
658{
659 int i, jobnum;
660 struct pipe *pi=NULL;
661
662 if (!interactive)
663 return EXIT_FAILURE;
664 /* If they gave us no args, assume they want the last backgrounded task */
665 if (!child->argv[1]) {
666 for (pi = job_list; pi; pi = pi->next) {
667 if (pi->jobid == last_jobid) {
668 break;
669 }
670 }
671 if (!pi) {
672 error_msg("%s: no current job", child->argv[0]);
673 return EXIT_FAILURE;
674 }
675 } else {
676 if (sscanf(child->argv[1], "%%%d", &jobnum) != 1) {
677 error_msg("%s: bad argument '%s'", child->argv[0], child->argv[1]);
678 return EXIT_FAILURE;
679 }
680 for (pi = job_list; pi; pi = pi->next) {
681 if (pi->jobid == jobnum) {
682 break;
683 }
684 }
685 if (!pi) {
686 error_msg("%s: %d: no such job", child->argv[0], jobnum);
687 return EXIT_FAILURE;
688 }
689 }
690
691 if (*child->argv[0] == 'f') {
692 /* Put the job into the foreground. */
693 tcsetpgrp(shell_terminal, pi->pgrp);
694 }
695
696 /* Restart the processes in the job */
697 for (i = 0; i < pi->num_progs; i++)
698 pi->progs[i].is_stopped = 0;
699
700 if ( (i=kill(- pi->pgrp, SIGCONT)) < 0) {
701 if (i == ESRCH) {
702 remove_bg_job(pi);
703 } else {
704 perror_msg("kill (SIGCONT)");
705 }
706 }
707
708 pi->stopped_progs = 0;
709 return EXIT_SUCCESS;
710}
711
712/* built-in 'help' handler */
713static int builtin_help(struct child_prog *dummy)
714{
715 struct built_in_command *x;
716
717 printf("\nBuilt-in commands:\n");
718 printf("-------------------\n");
719 for (x = bltins; x->cmd; x++) {
720 if (x->descr==NULL)
721 continue;
722 printf("%s\t%s\n", x->cmd, x->descr);
723 }
724 printf("\n\n");
725 return EXIT_SUCCESS;
726}
727
728/* built-in 'jobs' handler */
729static int builtin_jobs(struct child_prog *child)
730{
731 struct pipe *job;
732 char *status_string;
733
734 for (job = job_list; job; job = job->next) {
735 if (job->running_progs == job->stopped_progs)
736 status_string = "Stopped";
737 else
738 status_string = "Running";
739
740 printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->text);
741 }
742 return EXIT_SUCCESS;
743}
744
745
746/* built-in 'pwd' handler */
747static int builtin_pwd(struct child_prog *dummy)
748{
749 puts(set_cwd());
750 return EXIT_SUCCESS;
751}
752
753/* built-in 'read VAR' handler */
754static int builtin_read(struct child_prog *child)
755{
756 int res;
757
758 if (child->argv[1]) {
759 char string[BUFSIZ];
760 char *var = 0;
761
762 string[0] = 0; /* In case stdin has only EOF */
763 /* read string */
764 fgets(string, sizeof(string), stdin);
765 chomp(string);
766 var = malloc(strlen(child->argv[1])+strlen(string)+2);
767 if(var) {
768 sprintf(var, "%s=%s", child->argv[1], string);
769 res = set_local_var(var, 0);
770 } else
771 res = -1;
772 if (res)
773 fprintf(stderr, "read: %m\n");
774 free(var); /* So not move up to avoid breaking errno */
775 return res;
776 } else {
777 do res=getchar(); while(res!='\n' && res!=EOF);
778 return 0;
779 }
780}
781
782/* built-in 'set VAR=value' handler */
783static int builtin_set(struct child_prog *child)
784{
785 char *temp = child->argv[1];
786 struct variables *e;
787
788 if (temp == NULL)
789 for(e = top_vars; e; e=e->next)
790 printf("%s=%s\n", e->name, e->value);
791 else
792 set_local_var(temp, 0);
793
794 return EXIT_SUCCESS;
795}
796
797
798/* Built-in 'shift' handler */
799static int builtin_shift(struct child_prog *child)
800{
801 int n=1;
802 if (child->argv[1]) {
803 n=atoi(child->argv[1]);
804 }
805 if (n>=0 && n<global_argc) {
806 /* XXX This probably breaks $0 */
807 global_argc -= n;
808 global_argv += n;
809 return EXIT_SUCCESS;
810 } else {
811 return EXIT_FAILURE;
812 }
813}
814
815/* Built-in '.' handler (read-in and execute commands from file) */
816static int builtin_source(struct child_prog *child)
817{
818 FILE *input;
819 int status;
820
821 if (child->argv[1] == NULL)
822 return EXIT_FAILURE;
823
824 /* XXX search through $PATH is missing */
825 input = fopen(child->argv[1], "r");
826 if (!input) {
827 error_msg("Couldn't open file '%s'", child->argv[1]);
828 return EXIT_FAILURE;
829 }
830
831 /* Now run the file */
832 /* XXX argv and argc are broken; need to save old global_argv
833 * (pointer only is OK!) on this stack frame,
834 * set global_argv=child->argv+1, recurse, and restore. */
835 mark_open(fileno(input));
836 status = parse_file_outer(input);
837 mark_closed(fileno(input));
838 fclose(input);
839 return (status);
840}
841
842static int builtin_umask(struct child_prog *child)
843{
844 mode_t new_umask;
845 const char *arg = child->argv[1];
846 char *end;
847 if (arg) {
848 new_umask=strtoul(arg, &end, 8);
849 if (*end!='\0' || end == arg) {
850 return EXIT_FAILURE;
851 }
852 } else {
853 printf("%.3o\n", (unsigned int) (new_umask=umask(0)));
854 }
855 umask(new_umask);
856 return EXIT_SUCCESS;
857}
858
859/* built-in 'unset VAR' handler */
860static int builtin_unset(struct child_prog *child)
861{
862 /* bash returned already true */
863 unset_local_var(child->argv[1]);
864 return EXIT_SUCCESS;
865}
866
867static int builtin_not_written(struct child_prog *child)
868{
869 printf("builtin_%s not written\n",child->argv[0]);
870 return EXIT_FAILURE;
871}
872#endif
873
xia.jin83aee562024-08-07 02:35:05 +0000874#ifdef CONFIG_ARMV8_MULTIENTRY
875void release_cmd_locker(int cpu)
876{
877 if (lock_holder == cpu)
878 spin_unlock(&cmd_lock);
879}
880#endif
881
wdenkfe8c2802002-11-03 00:38:21 +0000882static int b_check_space(o_string *o, int len)
883{
884 /* It would be easy to drop a more restrictive policy
885 * in here, such as setting a maximum string length */
886 if (o->length + len > o->maxlen) {
887 char *old_data = o->data;
888 /* assert (data == NULL || o->maxlen != 0); */
889 o->maxlen += max(2*len, B_CHUNK);
890 o->data = realloc(o->data, 1 + o->maxlen);
891 if (o->data == NULL) {
892 free(old_data);
893 }
894 }
895 return o->data == NULL;
896}
897
898static int b_addchr(o_string *o, int ch)
899{
900 debug_printf("b_addchr: %c %d %p\n", ch, o->length, o);
901 if (b_check_space(o, 1)) return B_NOSPAC;
902 o->data[o->length] = ch;
903 o->length++;
904 o->data[o->length] = '\0';
905 return 0;
906}
907
908static void b_reset(o_string *o)
909{
910 o->length = 0;
911 o->nonnull = 0;
912 if (o->data != NULL) *o->data = '\0';
913}
914
915static void b_free(o_string *o)
916{
917 b_reset(o);
wdenkd0fb80c2003-01-11 09:48:40 +0000918 free(o->data);
wdenkfe8c2802002-11-03 00:38:21 +0000919 o->data = NULL;
920 o->maxlen = 0;
921}
922
923/* My analysis of quoting semantics tells me that state information
924 * is associated with a destination, not a source.
925 */
926static int b_addqchr(o_string *o, int ch, int quote)
927{
928 if (quote && strchr("*?[\\",ch)) {
929 int rc;
930 rc = b_addchr(o, '\\');
931 if (rc) return rc;
932 }
933 return b_addchr(o, ch);
934}
935
wdenkc26e4542004-04-18 10:13:26 +0000936#ifndef __U_BOOT__
wdenkfe8c2802002-11-03 00:38:21 +0000937static int b_adduint(o_string *o, unsigned int i)
938{
939 int r;
940 char *p = simple_itoa(i);
941 /* no escape checking necessary */
942 do r=b_addchr(o, *p++); while (r==0 && *p);
943 return r;
944}
wdenkc26e4542004-04-18 10:13:26 +0000945#endif
wdenkfe8c2802002-11-03 00:38:21 +0000946
947static int static_get(struct in_str *i)
948{
Wolfgang Denkd0ff51b2008-07-14 15:19:07 +0200949 int ch = *i->p++;
wdenkfe8c2802002-11-03 00:38:21 +0000950 if (ch=='\0') return EOF;
951 return ch;
952}
953
954static int static_peek(struct in_str *i)
955{
956 return *i->p;
957}
958
959#ifndef __U_BOOT__
960static inline void cmdedit_set_initial_prompt(void)
961{
wdenkd0fb80c2003-01-11 09:48:40 +0000962#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
wdenkfe8c2802002-11-03 00:38:21 +0000963 PS1 = NULL;
964#else
Simon Glass00caae62017-08-03 12:22:12 -0600965 PS1 = env_get("PS1");
wdenkfe8c2802002-11-03 00:38:21 +0000966 if(PS1==0)
967 PS1 = "\\w \\$ ";
968#endif
969}
970
971static inline void setup_prompt_string(int promptmode, char **prompt_str)
972{
973 debug_printf("setup_prompt_string %d ",promptmode);
wdenkd0fb80c2003-01-11 09:48:40 +0000974#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
wdenkfe8c2802002-11-03 00:38:21 +0000975 /* Set up the prompt */
976 if (promptmode == 1) {
wdenkd0fb80c2003-01-11 09:48:40 +0000977 free(PS1);
wdenkfe8c2802002-11-03 00:38:21 +0000978 PS1=xmalloc(strlen(cwd)+4);
979 sprintf(PS1, "%s %s", cwd, ( geteuid() != 0 ) ? "$ ":"# ");
980 *prompt_str = PS1;
981 } else {
982 *prompt_str = PS2;
983 }
984#else
985 *prompt_str = (promptmode==1)? PS1 : PS2;
986#endif
987 debug_printf("result %s\n",*prompt_str);
988}
989#endif
990
Marek Vasut13d30462016-01-27 04:47:54 +0100991#ifdef __U_BOOT__
992static int uboot_cli_readline(struct in_str *i)
993{
994 char *prompt;
Marek Vasutf3b267b2016-01-27 04:47:55 +0100995 char __maybe_unused *ps_prompt = NULL;
Marek Vasut13d30462016-01-27 04:47:54 +0100996
997 if (i->promptmode == 1)
998 prompt = CONFIG_SYS_PROMPT;
999 else
1000 prompt = CONFIG_SYS_PROMPT_HUSH_PS2;
1001
Marek Vasutf3b267b2016-01-27 04:47:55 +01001002#ifdef CONFIG_CMDLINE_PS_SUPPORT
1003 if (i->promptmode == 1)
Simon Glass00caae62017-08-03 12:22:12 -06001004 ps_prompt = env_get("PS1");
Marek Vasutf3b267b2016-01-27 04:47:55 +01001005 else
Simon Glass00caae62017-08-03 12:22:12 -06001006 ps_prompt = env_get("PS2");
Marek Vasutf3b267b2016-01-27 04:47:55 +01001007 if (ps_prompt)
1008 prompt = ps_prompt;
1009#endif
1010
Marek Vasut13d30462016-01-27 04:47:54 +01001011 return cli_readline(prompt);
1012}
1013#endif
1014
wdenkfe8c2802002-11-03 00:38:21 +00001015static void get_user_input(struct in_str *i)
1016{
1017#ifndef __U_BOOT__
1018 char *prompt_str;
1019 static char the_command[BUFSIZ];
1020
1021 setup_prompt_string(i->promptmode, &prompt_str);
wdenkd0fb80c2003-01-11 09:48:40 +00001022#ifdef CONFIG_FEATURE_COMMAND_EDITING
wdenkfe8c2802002-11-03 00:38:21 +00001023 /*
1024 ** enable command line editing only while a command line
1025 ** is actually being read; otherwise, we'll end up bequeathing
1026 ** atexit() handlers and other unwanted stuff to our
1027 ** child processes (rob@sysgo.de)
1028 */
1029 cmdedit_read_input(prompt_str, the_command);
1030#else
1031 fputs(prompt_str, stdout);
1032 fflush(stdout);
1033 the_command[0]=fgetc(i->file);
1034 the_command[1]='\0';
1035#endif
1036 fflush(stdout);
1037 i->p = the_command;
1038#else
wdenkfe8c2802002-11-03 00:38:21 +00001039 int n;
Kristian Otnes5c50a922014-04-25 15:35:43 +02001040 static char the_command[CONFIG_SYS_CBSIZE + 1];
wdenkfe8c2802002-11-03 00:38:21 +00001041
Simon Glassb26440f2014-04-10 20:01:31 -06001042 bootretry_reset_cmd_timeout();
wdenkfe8c2802002-11-03 00:38:21 +00001043 i->__promptme = 1;
Marek Vasut13d30462016-01-27 04:47:54 +01001044 n = uboot_cli_readline(i);
1045
Wolfgang Denk396387a2005-08-12 23:34:51 +02001046#ifdef CONFIG_BOOT_RETRY_TIME
1047 if (n == -2) {
1048 puts("\nTimeout waiting for command\n");
1049# ifdef CONFIG_RESET_TO_RETRY
1050 do_reset(NULL, 0, 0, NULL);
1051# else
1052# error "This currently only works with CONFIG_RESET_TO_RETRY enabled"
1053# endif
1054 }
1055#endif
wdenkfe8c2802002-11-03 00:38:21 +00001056 if (n == -1 ) {
1057 flag_repeat = 0;
1058 i->__promptme = 0;
1059 }
1060 n = strlen(console_buffer);
1061 console_buffer[n] = '\n';
1062 console_buffer[n+1]= '\0';
1063 if (had_ctrlc()) flag_repeat = 0;
1064 clear_ctrlc();
1065 do_repeat = 0;
1066 if (i->promptmode == 1) {
1067 if (console_buffer[0] == '\n'&& flag_repeat == 0) {
1068 strcpy(the_command,console_buffer);
1069 }
1070 else {
1071 if (console_buffer[0] != '\n') {
1072 strcpy(the_command,console_buffer);
1073 flag_repeat = 1;
1074 }
1075 else {
1076 do_repeat = 1;
1077 }
1078 }
1079 i->p = the_command;
1080 }
1081 else {
wdenk8bde7f72003-06-27 21:31:46 +00001082 if (console_buffer[0] != '\n') {
1083 if (strlen(the_command) + strlen(console_buffer)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001084 < CONFIG_SYS_CBSIZE) {
wdenk8bde7f72003-06-27 21:31:46 +00001085 n = strlen(the_command);
1086 the_command[n-1] = ' ';
1087 strcpy(&the_command[n],console_buffer);
wdenkfe8c2802002-11-03 00:38:21 +00001088 }
1089 else {
1090 the_command[0] = '\n';
1091 the_command[1] = '\0';
1092 flag_repeat = 0;
1093 }
1094 }
1095 if (i->__promptme == 0) {
1096 the_command[0] = '\n';
1097 the_command[1] = '\0';
1098 }
1099 i->p = console_buffer;
1100 }
1101#endif
1102}
1103
1104/* This is the magic location that prints prompts
1105 * and gets data back from the user */
1106static int file_get(struct in_str *i)
1107{
1108 int ch;
1109
1110 ch = 0;
1111 /* If there is data waiting, eat it up */
1112 if (i->p && *i->p) {
Wolfgang Denkd0ff51b2008-07-14 15:19:07 +02001113 ch = *i->p++;
wdenkfe8c2802002-11-03 00:38:21 +00001114 } else {
1115 /* need to double check i->file because we might be doing something
1116 * more complicated by now, like sourcing or substituting. */
1117#ifndef __U_BOOT__
1118 if (i->__promptme && interactive && i->file == stdin) {
1119 while(! i->p || (interactive && strlen(i->p)==0) ) {
1120#else
1121 while(! i->p || strlen(i->p)==0 ) {
1122#endif
1123 get_user_input(i);
1124 }
1125 i->promptmode=2;
1126#ifndef __U_BOOT__
1127 i->__promptme = 0;
1128#endif
1129 if (i->p && *i->p) {
Wolfgang Denkd0ff51b2008-07-14 15:19:07 +02001130 ch = *i->p++;
wdenkfe8c2802002-11-03 00:38:21 +00001131 }
1132#ifndef __U_BOOT__
1133 } else {
1134 ch = fgetc(i->file);
1135 }
1136
1137#endif
1138 debug_printf("b_getch: got a %d\n", ch);
1139 }
1140#ifndef __U_BOOT__
1141 if (ch == '\n') i->__promptme=1;
1142#endif
1143 return ch;
1144}
1145
1146/* All the callers guarantee this routine will never be
1147 * used right after a newline, so prompting is not needed.
1148 */
1149static int file_peek(struct in_str *i)
1150{
1151#ifndef __U_BOOT__
1152 if (i->p && *i->p) {
1153#endif
1154 return *i->p;
1155#ifndef __U_BOOT__
1156 } else {
1157 i->peek_buf[0] = fgetc(i->file);
1158 i->peek_buf[1] = '\0';
1159 i->p = i->peek_buf;
1160 debug_printf("b_peek: got a %d\n", *i->p);
1161 return *i->p;
1162 }
1163#endif
1164}
1165
1166#ifndef __U_BOOT__
1167static void setup_file_in_str(struct in_str *i, FILE *f)
1168#else
1169static void setup_file_in_str(struct in_str *i)
1170#endif
1171{
1172 i->peek = file_peek;
1173 i->get = file_get;
1174 i->__promptme=1;
1175 i->promptmode=1;
1176#ifndef __U_BOOT__
1177 i->file = f;
1178#endif
1179 i->p = NULL;
1180}
1181
1182static void setup_string_in_str(struct in_str *i, const char *s)
1183{
1184 i->peek = static_peek;
1185 i->get = static_get;
1186 i->__promptme=1;
1187 i->promptmode=1;
1188 i->p = s;
1189}
1190
1191#ifndef __U_BOOT__
1192static void mark_open(int fd)
1193{
1194 struct close_me *new = xmalloc(sizeof(struct close_me));
1195 new->fd = fd;
1196 new->next = close_me_head;
1197 close_me_head = new;
1198}
1199
1200static void mark_closed(int fd)
1201{
1202 struct close_me *tmp;
1203 if (close_me_head == NULL || close_me_head->fd != fd)
1204 error_msg_and_die("corrupt close_me");
1205 tmp = close_me_head;
1206 close_me_head = close_me_head->next;
1207 free(tmp);
1208}
1209
wdenkd0fb80c2003-01-11 09:48:40 +00001210static void close_all(void)
wdenkfe8c2802002-11-03 00:38:21 +00001211{
1212 struct close_me *c;
1213 for (c=close_me_head; c; c=c->next) {
1214 close(c->fd);
1215 }
1216 close_me_head = NULL;
1217}
1218
1219/* squirrel != NULL means we squirrel away copies of stdin, stdout,
1220 * and stderr if they are redirected. */
1221static int setup_redirects(struct child_prog *prog, int squirrel[])
1222{
1223 int openfd, mode;
1224 struct redir_struct *redir;
1225
1226 for (redir=prog->redirects; redir; redir=redir->next) {
1227 if (redir->dup == -1 && redir->word.gl_pathv == NULL) {
1228 /* something went wrong in the parse. Pretend it didn't happen */
1229 continue;
1230 }
1231 if (redir->dup == -1) {
1232 mode=redir_table[redir->type].mode;
1233 openfd = open(redir->word.gl_pathv[0], mode, 0666);
1234 if (openfd < 0) {
1235 /* this could get lost if stderr has been redirected, but
1236 bash and ash both lose it as well (though zsh doesn't!) */
1237 perror_msg("error opening %s", redir->word.gl_pathv[0]);
1238 return 1;
1239 }
1240 } else {
1241 openfd = redir->dup;
1242 }
1243
1244 if (openfd != redir->fd) {
1245 if (squirrel && redir->fd < 3) {
1246 squirrel[redir->fd] = dup(redir->fd);
1247 }
1248 if (openfd == -3) {
1249 close(openfd);
1250 } else {
1251 dup2(openfd, redir->fd);
1252 if (redir->dup == -1)
1253 close (openfd);
1254 }
1255 }
1256 }
1257 return 0;
1258}
1259
1260static void restore_redirects(int squirrel[])
1261{
1262 int i, fd;
1263 for (i=0; i<3; i++) {
1264 fd = squirrel[i];
1265 if (fd != -1) {
1266 /* No error checking. I sure wouldn't know what
1267 * to do with an error if I found one! */
1268 dup2(fd, i);
1269 close(fd);
1270 }
1271 }
1272}
1273
1274/* never returns */
1275/* XXX no exit() here. If you don't exec, use _exit instead.
1276 * The at_exit handlers apparently confuse the calling process,
1277 * in particular stdin handling. Not sure why? */
1278static void pseudo_exec(struct child_prog *child)
1279{
1280 int i, rcode;
1281 char *p;
1282 struct built_in_command *x;
1283 if (child->argv) {
1284 for (i=0; is_assignment(child->argv[i]); i++) {
1285 debug_printf("pid %d environment modification: %s\n",getpid(),child->argv[i]);
1286 p = insert_var_value(child->argv[i]);
1287 putenv(strdup(p));
1288 if (p != child->argv[i]) free(p);
1289 }
1290 child->argv+=i; /* XXX this hack isn't so horrible, since we are about
wdenk8bde7f72003-06-27 21:31:46 +00001291 to exit, and therefore don't need to keep data
1292 structures consistent for free() use. */
wdenkfe8c2802002-11-03 00:38:21 +00001293 /* If a variable is assigned in a forest, and nobody listens,
1294 * was it ever really set?
1295 */
1296 if (child->argv[0] == NULL) {
1297 _exit(EXIT_SUCCESS);
1298 }
1299
1300 /*
1301 * Check if the command matches any of the builtins.
1302 * Depending on context, this might be redundant. But it's
1303 * easier to waste a few CPU cycles than it is to figure out
1304 * if this is one of those cases.
1305 */
1306 for (x = bltins; x->cmd; x++) {
1307 if (strcmp(child->argv[0], x->cmd) == 0 ) {
1308 debug_printf("builtin exec %s\n", child->argv[0]);
1309 rcode = x->function(child);
1310 fflush(stdout);
1311 _exit(rcode);
1312 }
1313 }
1314
1315 /* Check if the command matches any busybox internal commands
1316 * ("applets") here.
1317 * FIXME: This feature is not 100% safe, since
1318 * BusyBox is not fully reentrant, so we have no guarantee the things
1319 * from the .bss are still zeroed, or that things from .data are still
1320 * at their defaults. We could exec ourself from /proc/self/exe, but I
1321 * really dislike relying on /proc for things. We could exec ourself
1322 * from global_argv[0], but if we are in a chroot, we may not be able
1323 * to find ourself... */
wdenkd0fb80c2003-01-11 09:48:40 +00001324#ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
wdenkfe8c2802002-11-03 00:38:21 +00001325 {
1326 int argc_l;
1327 char** argv_l=child->argv;
1328 char *name = child->argv[0];
1329
wdenkd0fb80c2003-01-11 09:48:40 +00001330#ifdef CONFIG_FEATURE_SH_APPLETS_ALWAYS_WIN
wdenkfe8c2802002-11-03 00:38:21 +00001331 /* Following discussions from November 2000 on the busybox mailing
1332 * list, the default configuration, (without
1333 * get_last_path_component()) lets the user force use of an
1334 * external command by specifying the full (with slashes) filename.
wdenkd0fb80c2003-01-11 09:48:40 +00001335 * If you enable CONFIG_FEATURE_SH_APPLETS_ALWAYS_WIN then applets
wdenkfe8c2802002-11-03 00:38:21 +00001336 * _aways_ override external commands, so if you want to run
1337 * /bin/cat, it will use BusyBox cat even if /bin/cat exists on the
1338 * filesystem and is _not_ busybox. Some systems may want this,
1339 * most do not. */
1340 name = get_last_path_component(name);
1341#endif
1342 /* Count argc for use in a second... */
1343 for(argc_l=0;*argv_l!=NULL; argv_l++, argc_l++);
1344 optind = 1;
1345 debug_printf("running applet %s\n", name);
1346 run_applet_by_name(name, argc_l, child->argv);
1347 }
1348#endif
1349 debug_printf("exec of %s\n",child->argv[0]);
1350 execvp(child->argv[0],child->argv);
1351 perror_msg("couldn't exec: %s",child->argv[0]);
1352 _exit(1);
1353 } else if (child->group) {
1354 debug_printf("runtime nesting to group\n");
1355 interactive=0; /* crucial!!!! */
1356 rcode = run_list_real(child->group);
1357 /* OK to leak memory by not calling free_pipe_list,
1358 * since this process is about to exit */
1359 _exit(rcode);
1360 } else {
1361 /* Can happen. See what bash does with ">foo" by itself. */
1362 debug_printf("trying to pseudo_exec null command\n");
1363 _exit(EXIT_SUCCESS);
1364 }
1365}
1366
1367static void insert_bg_job(struct pipe *pi)
1368{
1369 struct pipe *thejob;
1370
1371 /* Linear search for the ID of the job to use */
1372 pi->jobid = 1;
1373 for (thejob = job_list; thejob; thejob = thejob->next)
1374 if (thejob->jobid >= pi->jobid)
1375 pi->jobid = thejob->jobid + 1;
1376
1377 /* add thejob to the list of running jobs */
1378 if (!job_list) {
1379 thejob = job_list = xmalloc(sizeof(*thejob));
1380 } else {
1381 for (thejob = job_list; thejob->next; thejob = thejob->next) /* nothing */;
1382 thejob->next = xmalloc(sizeof(*thejob));
1383 thejob = thejob->next;
1384 }
1385
1386 /* physically copy the struct job */
1387 memcpy(thejob, pi, sizeof(struct pipe));
1388 thejob->next = NULL;
1389 thejob->running_progs = thejob->num_progs;
1390 thejob->stopped_progs = 0;
1391 thejob->text = xmalloc(BUFSIZ); /* cmdedit buffer size */
1392
1393 /*if (pi->progs[0] && pi->progs[0].argv && pi->progs[0].argv[0]) */
1394 {
1395 char *bar=thejob->text;
1396 char **foo=pi->progs[0].argv;
1397 while(foo && *foo) {
1398 bar += sprintf(bar, "%s ", *foo++);
1399 }
1400 }
1401
1402 /* we don't wait for background thejobs to return -- append it
1403 to the list of backgrounded thejobs and leave it alone */
1404 printf("[%d] %d\n", thejob->jobid, thejob->progs[0].pid);
1405 last_bg_pid = thejob->progs[0].pid;
1406 last_jobid = thejob->jobid;
1407}
1408
1409/* remove a backgrounded job */
1410static void remove_bg_job(struct pipe *pi)
1411{
1412 struct pipe *prev_pipe;
1413
1414 if (pi == job_list) {
1415 job_list = pi->next;
1416 } else {
1417 prev_pipe = job_list;
1418 while (prev_pipe->next != pi)
1419 prev_pipe = prev_pipe->next;
1420 prev_pipe->next = pi->next;
1421 }
1422 if (job_list)
1423 last_jobid = job_list->jobid;
1424 else
1425 last_jobid = 0;
1426
1427 pi->stopped_progs = 0;
1428 free_pipe(pi, 0);
1429 free(pi);
1430}
1431
1432/* Checks to see if any processes have exited -- if they
1433 have, figure out why and see if a job has completed */
1434static int checkjobs(struct pipe* fg_pipe)
1435{
1436 int attributes;
1437 int status;
1438 int prognum = 0;
1439 struct pipe *pi;
1440 pid_t childpid;
1441
1442 attributes = WUNTRACED;
1443 if (fg_pipe==NULL) {
1444 attributes |= WNOHANG;
1445 }
1446
1447 while ((childpid = waitpid(-1, &status, attributes)) > 0) {
1448 if (fg_pipe) {
1449 int i, rcode = 0;
1450 for (i=0; i < fg_pipe->num_progs; i++) {
1451 if (fg_pipe->progs[i].pid == childpid) {
1452 if (i==fg_pipe->num_progs-1)
1453 rcode=WEXITSTATUS(status);
1454 (fg_pipe->num_progs)--;
1455 return(rcode);
1456 }
1457 }
1458 }
1459
1460 for (pi = job_list; pi; pi = pi->next) {
1461 prognum = 0;
1462 while (prognum < pi->num_progs && pi->progs[prognum].pid != childpid) {
1463 prognum++;
1464 }
1465 if (prognum < pi->num_progs)
1466 break;
1467 }
1468
1469 if(pi==NULL) {
1470 debug_printf("checkjobs: pid %d was not in our list!\n", childpid);
1471 continue;
1472 }
1473
1474 if (WIFEXITED(status) || WIFSIGNALED(status)) {
1475 /* child exited */
1476 pi->running_progs--;
1477 pi->progs[prognum].pid = 0;
1478
1479 if (!pi->running_progs) {
1480 printf(JOB_STATUS_FORMAT, pi->jobid, "Done", pi->text);
1481 remove_bg_job(pi);
1482 }
1483 } else {
1484 /* child stopped */
1485 pi->stopped_progs++;
1486 pi->progs[prognum].is_stopped = 1;
1487
1488#if 0
1489 /* Printing this stuff is a pain, since it tends to
1490 * overwrite the prompt an inconveinient moments. So
1491 * don't do that. */
1492 if (pi->stopped_progs == pi->num_progs) {
1493 printf("\n"JOB_STATUS_FORMAT, pi->jobid, "Stopped", pi->text);
1494 }
1495#endif
1496 }
1497 }
1498
1499 if (childpid == -1 && errno != ECHILD)
1500 perror_msg("waitpid");
1501
1502 /* move the shell to the foreground */
1503 /*if (interactive && tcsetpgrp(shell_terminal, getpgid(0))) */
1504 /* perror_msg("tcsetpgrp-2"); */
1505 return -1;
1506}
1507
1508/* Figure out our controlling tty, checking in order stderr,
1509 * stdin, and stdout. If check_pgrp is set, also check that
1510 * we belong to the foreground process group associated with
1511 * that tty. The value of shell_terminal is needed in order to call
1512 * tcsetpgrp(shell_terminal, ...); */
1513void controlling_tty(int check_pgrp)
1514{
1515 pid_t curpgrp;
1516
1517 if ((curpgrp = tcgetpgrp(shell_terminal = 2)) < 0
1518 && (curpgrp = tcgetpgrp(shell_terminal = 0)) < 0
1519 && (curpgrp = tcgetpgrp(shell_terminal = 1)) < 0)
1520 goto shell_terminal_error;
1521
1522 if (check_pgrp && curpgrp != getpgid(0))
1523 goto shell_terminal_error;
1524
1525 return;
1526
1527shell_terminal_error:
1528 shell_terminal = -1;
1529 return;
1530}
1531#endif
1532
1533/* run_pipe_real() starts all the jobs, but doesn't wait for anything
1534 * to finish. See checkjobs().
1535 *
1536 * return code is normally -1, when the caller has to wait for children
1537 * to finish to determine the exit status of the pipe. If the pipe
1538 * is a simple builtin command, however, the action is done by the
1539 * time run_pipe_real returns, and the exit code is provided as the
1540 * return value.
1541 *
1542 * The input of the pipe is always stdin, the output is always
1543 * stdout. The outpipe[] mechanism in BusyBox-0.48 lash is bogus,
1544 * because it tries to avoid running the command substitution in
1545 * subshell, when that is in fact necessary. The subshell process
1546 * now has its stdout directed to the input of the appropriate pipe,
1547 * so this routine is noticeably simpler.
1548 */
1549static int run_pipe_real(struct pipe *pi)
1550{
1551 int i;
1552#ifndef __U_BOOT__
1553 int nextin, nextout;
1554 int pipefds[2]; /* pipefds[0] is for reading */
1555 struct child_prog *child;
1556 struct built_in_command *x;
1557 char *p;
wdenkd0fb80c2003-01-11 09:48:40 +00001558# if __GNUC__
1559 /* Avoid longjmp clobbering */
1560 (void) &i;
1561 (void) &nextin;
1562 (void) &nextout;
1563 (void) &child;
1564# endif
wdenkfe8c2802002-11-03 00:38:21 +00001565#else
1566 int nextin;
1567 int flag = do_repeat ? CMD_FLAG_REPEAT : 0;
1568 struct child_prog *child;
wdenkfe8c2802002-11-03 00:38:21 +00001569 char *p;
wdenkd0fb80c2003-01-11 09:48:40 +00001570# if __GNUC__
1571 /* Avoid longjmp clobbering */
1572 (void) &i;
1573 (void) &nextin;
1574 (void) &child;
1575# endif
1576#endif /* __U_BOOT__ */
wdenkfe8c2802002-11-03 00:38:21 +00001577
1578 nextin = 0;
1579#ifndef __U_BOOT__
1580 pi->pgrp = -1;
1581#endif
1582
1583 /* Check if this is a simple builtin (not part of a pipe).
1584 * Builtins within pipes have to fork anyway, and are handled in
1585 * pseudo_exec. "echo foo | read bar" doesn't work on bash, either.
1586 */
1587 if (pi->num_progs == 1) child = & (pi->progs[0]);
1588#ifndef __U_BOOT__
1589 if (pi->num_progs == 1 && child->group && child->subshell == 0) {
1590 int squirrel[] = {-1, -1, -1};
1591 int rcode;
1592 debug_printf("non-subshell grouping\n");
1593 setup_redirects(child, squirrel);
1594 /* XXX could we merge code with following builtin case,
1595 * by creating a pseudo builtin that calls run_list_real? */
1596 rcode = run_list_real(child->group);
1597 restore_redirects(squirrel);
1598#else
1599 if (pi->num_progs == 1 && child->group) {
1600 int rcode;
1601 debug_printf("non-subshell grouping\n");
1602 rcode = run_list_real(child->group);
1603#endif
1604 return rcode;
1605 } else if (pi->num_progs == 1 && pi->progs[0].argv != NULL) {
1606 for (i=0; is_assignment(child->argv[i]); i++) { /* nothing */ }
1607 if (i!=0 && child->argv[i]==NULL) {
1608 /* assignments, but no command: set the local environment */
1609 for (i=0; child->argv[i]!=NULL; i++) {
1610
1611 /* Ok, this case is tricky. We have to decide if this is a
1612 * local variable, or an already exported variable. If it is
1613 * already exported, we have to export the new value. If it is
1614 * not exported, we need only set this as a local variable.
1615 * This junk is all to decide whether or not to export this
1616 * variable. */
1617 int export_me=0;
1618 char *name, *value;
1619 name = xstrdup(child->argv[i]);
1620 debug_printf("Local environment set: %s\n", name);
1621 value = strchr(name, '=');
1622 if (value)
1623 *value=0;
1624#ifndef __U_BOOT__
1625 if ( get_local_var(name)) {
1626 export_me=1;
1627 }
1628#endif
1629 free(name);
1630 p = insert_var_value(child->argv[i]);
1631 set_local_var(p, export_me);
1632 if (p != child->argv[i]) free(p);
1633 }
1634 return EXIT_SUCCESS; /* don't worry about errors in set_local_var() yet */
1635 }
1636 for (i = 0; is_assignment(child->argv[i]); i++) {
1637 p = insert_var_value(child->argv[i]);
1638#ifndef __U_BOOT__
1639 putenv(strdup(p));
1640#else
1641 set_local_var(p, 0);
1642#endif
1643 if (p != child->argv[i]) {
1644 child->sp--;
1645 free(p);
1646 }
1647 }
1648 if (child->sp) {
1649 char * str = NULL;
1650
Stephen Warrenfe9ca3d2014-03-01 22:16:10 -07001651 str = make_string(child->argv + i,
1652 child->argv_nonnull + i);
wdenkfe8c2802002-11-03 00:38:21 +00001653 parse_string_outer(str, FLAG_EXIT_FROM_LOOP | FLAG_REPARSING);
1654 free(str);
1655 return last_return_code;
1656 }
1657#ifndef __U_BOOT__
1658 for (x = bltins; x->cmd; x++) {
1659 if (strcmp(child->argv[i], x->cmd) == 0 ) {
1660 int squirrel[] = {-1, -1, -1};
1661 int rcode;
1662 if (x->function == builtin_exec && child->argv[i+1]==NULL) {
1663 debug_printf("magic exec\n");
1664 setup_redirects(child,NULL);
1665 return EXIT_SUCCESS;
1666 }
1667 debug_printf("builtin inline %s\n", child->argv[0]);
1668 /* XXX setup_redirects acts on file descriptors, not FILEs.
1669 * This is perfect for work that comes after exec().
1670 * Is it really safe for inline use? Experimentally,
1671 * things seem to work with glibc. */
1672 setup_redirects(child, squirrel);
Simon Glass7344f912011-12-06 19:47:53 +00001673
1674 child->argv += i; /* XXX horrible hack */
1675 rcode = x->function(child);
1676 /* XXX restore hack so free() can work right */
1677 child->argv -= i;
1678 restore_redirects(squirrel);
1679 }
1680 return rcode;
1681 }
wdenkfe8c2802002-11-03 00:38:21 +00001682#else
Simon Glass9d12d5d2012-02-14 19:59:25 +00001683 /* check ";", because ,example , argv consist from
1684 * "help;flinfo" must not execute
1685 */
1686 if (strchr(child->argv[i], ';')) {
1687 printf("Unknown command '%s' - try 'help' or use "
1688 "'run' command\n", child->argv[i]);
1689 return -1;
wdenkfe8c2802002-11-03 00:38:21 +00001690 }
Simon Glass9d12d5d2012-02-14 19:59:25 +00001691 /* Process the command */
Sean Anderson9539f712021-02-28 16:29:51 -05001692 return cmd_process(flag, child->argc - i, child->argv + i,
Richard Genoud34765e82012-12-03 06:28:28 +00001693 &flag_repeat, NULL);
Simon Glass9d12d5d2012-02-14 19:59:25 +00001694#endif
wdenkfe8c2802002-11-03 00:38:21 +00001695 }
Simon Glass9d12d5d2012-02-14 19:59:25 +00001696#ifndef __U_BOOT__
wdenkfe8c2802002-11-03 00:38:21 +00001697
1698 for (i = 0; i < pi->num_progs; i++) {
1699 child = & (pi->progs[i]);
1700
1701 /* pipes are inserted between pairs of commands */
1702 if ((i + 1) < pi->num_progs) {
1703 if (pipe(pipefds)<0) perror_msg_and_die("pipe");
1704 nextout = pipefds[1];
1705 } else {
1706 nextout=1;
1707 pipefds[0] = -1;
1708 }
1709
1710 /* XXX test for failed fork()? */
1711 if (!(child->pid = fork())) {
1712 /* Set the handling for job control signals back to the default. */
1713 signal(SIGINT, SIG_DFL);
1714 signal(SIGQUIT, SIG_DFL);
1715 signal(SIGTERM, SIG_DFL);
1716 signal(SIGTSTP, SIG_DFL);
1717 signal(SIGTTIN, SIG_DFL);
1718 signal(SIGTTOU, SIG_DFL);
1719 signal(SIGCHLD, SIG_DFL);
1720
1721 close_all();
1722
1723 if (nextin != 0) {
1724 dup2(nextin, 0);
1725 close(nextin);
1726 }
1727 if (nextout != 1) {
1728 dup2(nextout, 1);
1729 close(nextout);
1730 }
1731 if (pipefds[0]!=-1) {
1732 close(pipefds[0]); /* opposite end of our output pipe */
1733 }
1734
1735 /* Like bash, explicit redirects override pipes,
1736 * and the pipe fd is available for dup'ing. */
1737 setup_redirects(child,NULL);
1738
1739 if (interactive && pi->followup!=PIPE_BG) {
1740 /* If we (the child) win the race, put ourselves in the process
1741 * group whose leader is the first process in this pipe. */
1742 if (pi->pgrp < 0) {
1743 pi->pgrp = getpid();
1744 }
1745 if (setpgid(0, pi->pgrp) == 0) {
1746 tcsetpgrp(2, pi->pgrp);
1747 }
1748 }
1749
1750 pseudo_exec(child);
1751 }
1752
1753
1754 /* put our child in the process group whose leader is the
1755 first process in this pipe */
1756 if (pi->pgrp < 0) {
1757 pi->pgrp = child->pid;
1758 }
1759 /* Don't check for errors. The child may be dead already,
1760 * in which case setpgid returns error code EACCES. */
1761 setpgid(child->pid, pi->pgrp);
1762
1763 if (nextin != 0)
1764 close(nextin);
1765 if (nextout != 1)
1766 close(nextout);
1767
1768 /* If there isn't another process, nextin is garbage
1769 but it doesn't matter */
1770 nextin = pipefds[0];
1771 }
1772#endif
1773 return -1;
1774}
1775
1776static int run_list_real(struct pipe *pi)
1777{
1778 char *save_name = NULL;
1779 char **list = NULL;
1780 char **save_list = NULL;
1781 struct pipe *rpipe;
1782 int flag_rep = 0;
1783#ifndef __U_BOOT__
1784 int save_num_progs;
1785#endif
1786 int rcode=0, flag_skip=1;
1787 int flag_restore = 0;
1788 int if_code=0, next_if_code=0; /* need double-buffer to handle elif */
1789 reserved_style rmode, skip_more_in_this_rmode=RES_XXXX;
1790 /* check syntax for "for" */
1791 for (rpipe = pi; rpipe; rpipe = rpipe->next) {
1792 if ((rpipe->r_mode == RES_IN ||
1793 rpipe->r_mode == RES_FOR) &&
1794 (rpipe->next == NULL)) {
1795 syntax();
1796#ifdef __U_BOOT__
1797 flag_repeat = 0;
1798#endif
1799 return 1;
1800 }
1801 if ((rpipe->r_mode == RES_IN &&
1802 (rpipe->next->r_mode == RES_IN &&
1803 rpipe->next->progs->argv != NULL))||
1804 (rpipe->r_mode == RES_FOR &&
1805 rpipe->next->r_mode != RES_IN)) {
1806 syntax();
1807#ifdef __U_BOOT__
1808 flag_repeat = 0;
1809#endif
1810 return 1;
1811 }
1812 }
1813 for (; pi; pi = (flag_restore != 0) ? rpipe : pi->next) {
1814 if (pi->r_mode == RES_WHILE || pi->r_mode == RES_UNTIL ||
1815 pi->r_mode == RES_FOR) {
1816#ifdef __U_BOOT__
1817 /* check Ctrl-C */
1818 ctrlc();
1819 if ((had_ctrlc())) {
1820 return 1;
1821 }
1822#endif
1823 flag_restore = 0;
1824 if (!rpipe) {
1825 flag_rep = 0;
1826 rpipe = pi;
1827 }
1828 }
1829 rmode = pi->r_mode;
1830 debug_printf("rmode=%d if_code=%d next_if_code=%d skip_more=%d\n", rmode, if_code, next_if_code, skip_more_in_this_rmode);
1831 if (rmode == skip_more_in_this_rmode && flag_skip) {
1832 if (pi->followup == PIPE_SEQ) flag_skip=0;
1833 continue;
1834 }
1835 flag_skip = 1;
1836 skip_more_in_this_rmode = RES_XXXX;
1837 if (rmode == RES_THEN || rmode == RES_ELSE) if_code = next_if_code;
1838 if (rmode == RES_THEN && if_code) continue;
1839 if (rmode == RES_ELSE && !if_code) continue;
wdenk56b86bf2004-04-12 14:31:43 +00001840 if (rmode == RES_ELIF && !if_code) break;
wdenkfe8c2802002-11-03 00:38:21 +00001841 if (rmode == RES_FOR && pi->num_progs) {
1842 if (!list) {
1843 /* if no variable values after "in" we skip "for" */
1844 if (!pi->next->progs->argv) continue;
1845 /* create list of variable values */
1846 list = make_list_in(pi->next->progs->argv,
1847 pi->progs->argv[0]);
1848 save_list = list;
1849 save_name = pi->progs->argv[0];
1850 pi->progs->argv[0] = NULL;
1851 flag_rep = 1;
1852 }
1853 if (!(*list)) {
1854 free(pi->progs->argv[0]);
1855 free(save_list);
1856 list = NULL;
1857 flag_rep = 0;
1858 pi->progs->argv[0] = save_name;
1859#ifndef __U_BOOT__
1860 pi->progs->glob_result.gl_pathv[0] =
1861 pi->progs->argv[0];
1862#endif
1863 continue;
1864 } else {
1865 /* insert new value from list for variable */
Heinrich Schuchardtf4070e62020-04-28 21:56:10 +02001866 free(pi->progs->argv[0]);
wdenkfe8c2802002-11-03 00:38:21 +00001867 pi->progs->argv[0] = *list++;
1868#ifndef __U_BOOT__
1869 pi->progs->glob_result.gl_pathv[0] =
1870 pi->progs->argv[0];
1871#endif
1872 }
1873 }
1874 if (rmode == RES_IN) continue;
1875 if (rmode == RES_DO) {
1876 if (!flag_rep) continue;
1877 }
Jeroen Hofstee930e4252014-06-11 00:28:47 +02001878 if (rmode == RES_DONE) {
wdenkfe8c2802002-11-03 00:38:21 +00001879 if (flag_rep) {
1880 flag_restore = 1;
1881 } else {
1882 rpipe = NULL;
1883 }
1884 }
1885 if (pi->num_progs == 0) continue;
1886#ifndef __U_BOOT__
1887 save_num_progs = pi->num_progs; /* save number of programs */
1888#endif
1889 rcode = run_pipe_real(pi);
1890 debug_printf("run_pipe_real returned %d\n",rcode);
1891#ifndef __U_BOOT__
1892 if (rcode!=-1) {
1893 /* We only ran a builtin: rcode was set by the return value
1894 * of run_pipe_real(), and we don't need to wait for anything. */
1895 } else if (pi->followup==PIPE_BG) {
1896 /* XXX check bash's behavior with nontrivial pipes */
1897 /* XXX compute jobid */
1898 /* XXX what does bash do with attempts to background builtins? */
1899 insert_bg_job(pi);
1900 rcode = EXIT_SUCCESS;
1901 } else {
1902 if (interactive) {
1903 /* move the new process group into the foreground */
1904 if (tcsetpgrp(shell_terminal, pi->pgrp) && errno != ENOTTY)
1905 perror_msg("tcsetpgrp-3");
1906 rcode = checkjobs(pi);
1907 /* move the shell to the foreground */
1908 if (tcsetpgrp(shell_terminal, getpgid(0)) && errno != ENOTTY)
1909 perror_msg("tcsetpgrp-4");
1910 } else {
1911 rcode = checkjobs(pi);
1912 }
1913 debug_printf("checkjobs returned %d\n",rcode);
1914 }
1915 last_return_code=rcode;
1916#else
wdenkc26e4542004-04-18 10:13:26 +00001917 if (rcode < -1) {
1918 last_return_code = -rcode - 2;
1919 return -2; /* exit */
1920 }
wdenkfe8c2802002-11-03 00:38:21 +00001921 last_return_code=(rcode == 0) ? 0 : 1;
1922#endif
1923#ifndef __U_BOOT__
1924 pi->num_progs = save_num_progs; /* restore number of programs */
1925#endif
1926 if ( rmode == RES_IF || rmode == RES_ELIF )
1927 next_if_code=rcode; /* can be overwritten a number of times */
1928 if (rmode == RES_WHILE)
1929 flag_rep = !last_return_code;
1930 if (rmode == RES_UNTIL)
1931 flag_rep = last_return_code;
1932 if ( (rcode==EXIT_SUCCESS && pi->followup==PIPE_OR) ||
1933 (rcode!=EXIT_SUCCESS && pi->followup==PIPE_AND) )
1934 skip_more_in_this_rmode=rmode;
1935#ifndef __U_BOOT__
1936 checkjobs(NULL);
1937#endif
1938 }
1939 return rcode;
1940}
1941
1942/* broken, of course, but OK for testing */
1943static char *indenter(int i)
1944{
1945 static char blanks[]=" ";
1946 return &blanks[sizeof(blanks)-i-1];
1947}
1948
1949/* return code is the exit status of the pipe */
1950static int free_pipe(struct pipe *pi, int indent)
1951{
1952 char **p;
1953 struct child_prog *child;
1954#ifndef __U_BOOT__
1955 struct redir_struct *r, *rnext;
1956#endif
1957 int a, i, ret_code=0;
1958 char *ind = indenter(indent);
1959
1960#ifndef __U_BOOT__
1961 if (pi->stopped_progs > 0)
1962 return ret_code;
1963 final_printf("%s run pipe: (pid %d)\n",ind,getpid());
1964#endif
1965 for (i=0; i<pi->num_progs; i++) {
1966 child = &pi->progs[i];
1967 final_printf("%s command %d:\n",ind,i);
1968 if (child->argv) {
1969 for (a=0,p=child->argv; *p; a++,p++) {
1970 final_printf("%s argv[%d] = %s\n",ind,a,*p);
1971 }
1972#ifndef __U_BOOT__
1973 globfree(&child->glob_result);
1974#else
Peter Tyser197324d2009-08-05 16:18:44 -05001975 for (a = 0; a < child->argc; a++) {
wdenk8bde7f72003-06-27 21:31:46 +00001976 free(child->argv[a]);
1977 }
Stephen Warrenfe9ca3d2014-03-01 22:16:10 -07001978 free(child->argv);
1979 free(child->argv_nonnull);
wdenk8bde7f72003-06-27 21:31:46 +00001980 child->argc = 0;
wdenkfe8c2802002-11-03 00:38:21 +00001981#endif
1982 child->argv=NULL;
1983 } else if (child->group) {
1984#ifndef __U_BOOT__
1985 final_printf("%s begin group (subshell:%d)\n",ind, child->subshell);
1986#endif
1987 ret_code = free_pipe_list(child->group,indent+3);
1988 final_printf("%s end group\n",ind);
1989 } else {
1990 final_printf("%s (nil)\n",ind);
1991 }
1992#ifndef __U_BOOT__
1993 for (r=child->redirects; r; r=rnext) {
1994 final_printf("%s redirect %d%s", ind, r->fd, redir_table[r->type].descrip);
1995 if (r->dup == -1) {
1996 /* guard against the case >$FOO, where foo is unset or blank */
1997 if (r->word.gl_pathv) {
1998 final_printf(" %s\n", *r->word.gl_pathv);
1999 globfree(&r->word);
2000 }
2001 } else {
2002 final_printf("&%d\n", r->dup);
2003 }
2004 rnext=r->next;
2005 free(r);
2006 }
2007 child->redirects=NULL;
2008#endif
2009 }
2010 free(pi->progs); /* children are an array, they get freed all at once */
2011 pi->progs=NULL;
2012 return ret_code;
2013}
2014
2015static int free_pipe_list(struct pipe *head, int indent)
2016{
2017 int rcode=0; /* if list has no members */
2018 struct pipe *pi, *next;
2019 char *ind = indenter(indent);
2020 for (pi=head; pi; pi=next) {
2021 final_printf("%s pipe reserved mode %d\n", ind, pi->r_mode);
2022 rcode = free_pipe(pi, indent);
2023 final_printf("%s pipe followup code %d\n", ind, pi->followup);
2024 next=pi->next;
2025 pi->next=NULL;
2026 free(pi);
2027 }
2028 return rcode;
2029}
2030
2031/* Select which version we will use */
2032static int run_list(struct pipe *pi)
2033{
2034 int rcode=0;
2035#ifndef __U_BOOT__
2036 if (fake_mode==0) {
2037#endif
2038 rcode = run_list_real(pi);
2039#ifndef __U_BOOT__
2040 }
2041#endif
2042 /* free_pipe_list has the side effect of clearing memory
2043 * In the long run that function can be merged with run_list_real,
2044 * but doing that now would hobble the debugging effort. */
2045 free_pipe_list(pi,0);
2046 return rcode;
2047}
2048
2049/* The API for glob is arguably broken. This routine pushes a non-matching
2050 * string into the output structure, removing non-backslashed backslashes.
2051 * If someone can prove me wrong, by performing this function within the
2052 * original glob(3) api, feel free to rewrite this routine into oblivion.
2053 * Return code (0 vs. GLOB_NOSPACE) matches glob(3).
2054 * XXX broken if the last character is '\\', check that before calling.
2055 */
2056#ifndef __U_BOOT__
2057static int globhack(const char *src, int flags, glob_t *pglob)
2058{
2059 int cnt=0, pathc;
2060 const char *s;
2061 char *dest;
2062 for (cnt=1, s=src; s && *s; s++) {
2063 if (*s == '\\') s++;
2064 cnt++;
2065 }
2066 dest = malloc(cnt);
2067 if (!dest) return GLOB_NOSPACE;
2068 if (!(flags & GLOB_APPEND)) {
2069 pglob->gl_pathv=NULL;
2070 pglob->gl_pathc=0;
2071 pglob->gl_offs=0;
2072 pglob->gl_offs=0;
2073 }
2074 pathc = ++pglob->gl_pathc;
2075 pglob->gl_pathv = realloc(pglob->gl_pathv, (pathc+1)*sizeof(*pglob->gl_pathv));
2076 if (pglob->gl_pathv == NULL) return GLOB_NOSPACE;
2077 pglob->gl_pathv[pathc-1]=dest;
2078 pglob->gl_pathv[pathc]=NULL;
2079 for (s=src; s && *s; s++, dest++) {
2080 if (*s == '\\') s++;
2081 *dest = *s;
2082 }
2083 *dest='\0';
2084 return 0;
2085}
2086
2087/* XXX broken if the last character is '\\', check that before calling */
2088static int glob_needed(const char *s)
2089{
2090 for (; *s; s++) {
2091 if (*s == '\\') s++;
2092 if (strchr("*[?",*s)) return 1;
2093 }
2094 return 0;
2095}
2096
2097#if 0
2098static void globprint(glob_t *pglob)
2099{
2100 int i;
2101 debug_printf("glob_t at %p:\n", pglob);
2102 debug_printf(" gl_pathc=%d gl_pathv=%p gl_offs=%d gl_flags=%d\n",
2103 pglob->gl_pathc, pglob->gl_pathv, pglob->gl_offs, pglob->gl_flags);
2104 for (i=0; i<pglob->gl_pathc; i++)
2105 debug_printf("pglob->gl_pathv[%d] = %p = %s\n", i,
2106 pglob->gl_pathv[i], pglob->gl_pathv[i]);
2107}
2108#endif
2109
2110static int xglob(o_string *dest, int flags, glob_t *pglob)
2111{
2112 int gr;
2113
wdenk8bde7f72003-06-27 21:31:46 +00002114 /* short-circuit for null word */
wdenkfe8c2802002-11-03 00:38:21 +00002115 /* we can code this better when the debug_printf's are gone */
wdenk8bde7f72003-06-27 21:31:46 +00002116 if (dest->length == 0) {
2117 if (dest->nonnull) {
2118 /* bash man page calls this an "explicit" null */
2119 gr = globhack(dest->data, flags, pglob);
2120 debug_printf("globhack returned %d\n",gr);
2121 } else {
wdenkfe8c2802002-11-03 00:38:21 +00002122 return 0;
2123 }
wdenk8bde7f72003-06-27 21:31:46 +00002124 } else if (glob_needed(dest->data)) {
wdenkfe8c2802002-11-03 00:38:21 +00002125 gr = glob(dest->data, flags, NULL, pglob);
2126 debug_printf("glob returned %d\n",gr);
2127 if (gr == GLOB_NOMATCH) {
2128 /* quote removal, or more accurately, backslash removal */
2129 gr = globhack(dest->data, flags, pglob);
2130 debug_printf("globhack returned %d\n",gr);
2131 }
2132 } else {
2133 gr = globhack(dest->data, flags, pglob);
2134 debug_printf("globhack returned %d\n",gr);
2135 }
2136 if (gr == GLOB_NOSPACE)
2137 error_msg_and_die("out of memory during glob");
2138 if (gr != 0) { /* GLOB_ABORTED ? */
2139 error_msg("glob(3) error %d",gr);
2140 }
2141 /* globprint(glob_target); */
2142 return gr;
2143}
2144#endif
2145
wdenkc26e4542004-04-18 10:13:26 +00002146#ifdef __U_BOOT__
2147static char *get_dollar_var(char ch);
2148#endif
2149
wdenkfe8c2802002-11-03 00:38:21 +00002150/* This is used to get/check local shell variables */
Holger Brunckeae3b062011-04-08 02:47:42 +00002151char *get_local_var(const char *s)
wdenkfe8c2802002-11-03 00:38:21 +00002152{
2153 struct variables *cur;
2154
2155 if (!s)
2156 return NULL;
wdenkc26e4542004-04-18 10:13:26 +00002157
2158#ifdef __U_BOOT__
2159 if (*s == '$')
2160 return get_dollar_var(s[1]);
2161#endif
2162
wdenkfe8c2802002-11-03 00:38:21 +00002163 for (cur = top_vars; cur; cur=cur->next)
2164 if(strcmp(cur->name, s)==0)
2165 return cur->value;
2166 return NULL;
2167}
2168
2169/* This is used to set local shell variables
2170 flg_export==0 if only local (not exporting) variable
2171 flg_export==1 if "new" exporting environ
2172 flg_export>1 if current startup environ (not call putenv()) */
Heiko Schocher81473f62008-10-15 09:40:28 +02002173int set_local_var(const char *s, int flg_export)
wdenkfe8c2802002-11-03 00:38:21 +00002174{
2175 char *name, *value;
2176 int result=0;
2177 struct variables *cur;
2178
wdenkc26e4542004-04-18 10:13:26 +00002179#ifdef __U_BOOT__
2180 /* might be possible! */
2181 if (!isalpha(*s))
2182 return -1;
2183#endif
2184
wdenkfe8c2802002-11-03 00:38:21 +00002185 name=strdup(s);
2186
wdenkfe8c2802002-11-03 00:38:21 +00002187 /* Assume when we enter this function that we are already in
2188 * NAME=VALUE format. So the first order of business is to
2189 * split 's' on the '=' into 'name' and 'value' */
2190 value = strchr(name, '=');
Peng Fanaa722522015-11-24 16:54:21 +08002191 if (value == NULL || *(value + 1) == 0) {
wdenkfe8c2802002-11-03 00:38:21 +00002192 free(name);
2193 return -1;
2194 }
2195 *value++ = 0;
2196
2197 for(cur = top_vars; cur; cur = cur->next) {
2198 if(strcmp(cur->name, name)==0)
2199 break;
2200 }
2201
2202 if(cur) {
2203 if(strcmp(cur->value, value)==0) {
2204 if(flg_export>0 && cur->flg_export==0)
2205 cur->flg_export=flg_export;
2206 else
2207 result++;
2208 } else {
2209 if(cur->flg_read_only) {
2210 error_msg("%s: readonly variable", name);
2211 result = -1;
2212 } else {
2213 if(flg_export>0 || cur->flg_export>1)
2214 cur->flg_export=1;
2215 free(cur->value);
2216
2217 cur->value = strdup(value);
2218 }
2219 }
2220 } else {
2221 cur = malloc(sizeof(struct variables));
2222 if(!cur) {
2223 result = -1;
2224 } else {
2225 cur->name = strdup(name);
Kim Phillips199adb62012-10-29 13:34:32 +00002226 if (cur->name == NULL) {
wdenkfe8c2802002-11-03 00:38:21 +00002227 free(cur);
2228 result = -1;
2229 } else {
2230 struct variables *bottom = top_vars;
2231 cur->value = strdup(value);
Kim Phillips199adb62012-10-29 13:34:32 +00002232 cur->next = NULL;
wdenkfe8c2802002-11-03 00:38:21 +00002233 cur->flg_export = flg_export;
2234 cur->flg_read_only = 0;
2235 while(bottom->next) bottom=bottom->next;
2236 bottom->next = cur;
2237 }
2238 }
2239 }
2240
2241#ifndef __U_BOOT__
2242 if(result==0 && cur->flg_export==1) {
2243 *(value-1) = '=';
2244 result = putenv(name);
2245 } else {
2246#endif
2247 free(name);
2248#ifndef __U_BOOT__
2249 if(result>0) /* equivalent to previous set */
2250 result = 0;
2251 }
2252#endif
2253 return result;
2254}
2255
Heiko Schocher81473f62008-10-15 09:40:28 +02002256void unset_local_var(const char *name)
wdenkfe8c2802002-11-03 00:38:21 +00002257{
2258 struct variables *cur;
2259
2260 if (name) {
2261 for (cur = top_vars; cur; cur=cur->next) {
2262 if(strcmp(cur->name, name)==0)
2263 break;
2264 }
Kim Phillips199adb62012-10-29 13:34:32 +00002265 if (cur != NULL) {
wdenkfe8c2802002-11-03 00:38:21 +00002266 struct variables *next = top_vars;
2267 if(cur->flg_read_only) {
2268 error_msg("%s: readonly variable", name);
2269 return;
2270 } else {
Heiko Schocher81473f62008-10-15 09:40:28 +02002271#ifndef __U_BOOT__
wdenkfe8c2802002-11-03 00:38:21 +00002272 if(cur->flg_export)
Simon Glass382bee52017-08-03 12:22:09 -06002273 unenv_set(cur->name);
Heiko Schocher81473f62008-10-15 09:40:28 +02002274#endif
wdenkfe8c2802002-11-03 00:38:21 +00002275 free(cur->name);
2276 free(cur->value);
2277 while (next->next != cur)
2278 next = next->next;
2279 next->next = cur->next;
2280 }
2281 free(cur);
2282 }
2283 }
2284}
wdenkfe8c2802002-11-03 00:38:21 +00002285
2286static int is_assignment(const char *s)
2287{
wdenkc26e4542004-04-18 10:13:26 +00002288 if (s == NULL)
2289 return 0;
2290
2291 if (!isalpha(*s)) return 0;
wdenkfe8c2802002-11-03 00:38:21 +00002292 ++s;
2293 while(isalnum(*s) || *s=='_') ++s;
2294 return *s=='=';
2295}
2296
2297#ifndef __U_BOOT__
2298/* the src parameter allows us to peek forward to a possible &n syntax
2299 * for file descriptor duplication, e.g., "2>&1".
2300 * Return code is 0 normally, 1 if a syntax error is detected in src.
2301 * Resource errors (in xmalloc) cause the process to exit */
2302static int setup_redirect(struct p_context *ctx, int fd, redir_type style,
2303 struct in_str *input)
2304{
2305 struct child_prog *child=ctx->child;
2306 struct redir_struct *redir = child->redirects;
2307 struct redir_struct *last_redir=NULL;
2308
2309 /* Create a new redir_struct and drop it onto the end of the linked list */
2310 while(redir) {
2311 last_redir=redir;
2312 redir=redir->next;
2313 }
2314 redir = xmalloc(sizeof(struct redir_struct));
2315 redir->next=NULL;
2316 redir->word.gl_pathv=NULL;
2317 if (last_redir) {
2318 last_redir->next=redir;
2319 } else {
2320 child->redirects=redir;
2321 }
2322
2323 redir->type=style;
2324 redir->fd= (fd==-1) ? redir_table[style].default_fd : fd ;
2325
2326 debug_printf("Redirect type %d%s\n", redir->fd, redir_table[style].descrip);
2327
2328 /* Check for a '2>&1' type redirect */
2329 redir->dup = redirect_dup_num(input);
2330 if (redir->dup == -2) return 1; /* syntax error */
2331 if (redir->dup != -1) {
2332 /* Erik had a check here that the file descriptor in question
2333 * is legit; I postpone that to "run time"
2334 * A "-" representation of "close me" shows up as a -3 here */
2335 debug_printf("Duplicating redirect '%d>&%d'\n", redir->fd, redir->dup);
2336 } else {
2337 /* We do _not_ try to open the file that src points to,
2338 * since we need to return and let src be expanded first.
2339 * Set ctx->pending_redirect, so we know what to do at the
2340 * end of the next parsed word.
2341 */
2342 ctx->pending_redirect = redir;
2343 }
2344 return 0;
2345}
2346#endif
2347
Kim Phillips199adb62012-10-29 13:34:32 +00002348static struct pipe *new_pipe(void)
2349{
wdenkfe8c2802002-11-03 00:38:21 +00002350 struct pipe *pi;
2351 pi = xmalloc(sizeof(struct pipe));
2352 pi->num_progs = 0;
2353 pi->progs = NULL;
2354 pi->next = NULL;
2355 pi->followup = 0; /* invalid */
Wolfgang Denke98f68b2005-09-28 01:49:47 +02002356 pi->r_mode = RES_NONE;
wdenkfe8c2802002-11-03 00:38:21 +00002357 return pi;
2358}
2359
2360static void initialize_context(struct p_context *ctx)
2361{
2362 ctx->pipe=NULL;
2363#ifndef __U_BOOT__
2364 ctx->pending_redirect=NULL;
2365#endif
2366 ctx->child=NULL;
2367 ctx->list_head=new_pipe();
2368 ctx->pipe=ctx->list_head;
2369 ctx->w=RES_NONE;
2370 ctx->stack=NULL;
2371#ifdef __U_BOOT__
2372 ctx->old_flag=0;
2373#endif
2374 done_command(ctx); /* creates the memory for working child */
2375}
2376
2377/* normal return is 0
2378 * if a reserved word is found, and processed, return 1
2379 * should handle if, then, elif, else, fi, for, while, until, do, done.
2380 * case, function, and select are obnoxious, save those for later.
2381 */
wdenk3e386912003-04-05 00:53:31 +00002382struct reserved_combo {
2383 char *literal;
2384 int code;
2385 long flag;
2386};
2387/* Mostly a list of accepted follow-up reserved words.
2388 * FLAG_END means we are done with the sequence, and are ready
2389 * to turn the compound list into a command.
2390 * FLAG_START means the word must start a new compound list.
2391 */
2392static struct reserved_combo reserved_list[] = {
2393 { "if", RES_IF, FLAG_THEN | FLAG_START },
2394 { "then", RES_THEN, FLAG_ELIF | FLAG_ELSE | FLAG_FI },
2395 { "elif", RES_ELIF, FLAG_THEN },
2396 { "else", RES_ELSE, FLAG_FI },
2397 { "fi", RES_FI, FLAG_END },
2398 { "for", RES_FOR, FLAG_IN | FLAG_START },
2399 { "while", RES_WHILE, FLAG_DO | FLAG_START },
2400 { "until", RES_UNTIL, FLAG_DO | FLAG_START },
2401 { "in", RES_IN, FLAG_DO },
2402 { "do", RES_DO, FLAG_DONE },
2403 { "done", RES_DONE, FLAG_END }
2404};
2405#define NRES (sizeof(reserved_list)/sizeof(struct reserved_combo))
2406
Kim Phillips199adb62012-10-29 13:34:32 +00002407static int reserved_word(o_string *dest, struct p_context *ctx)
wdenkfe8c2802002-11-03 00:38:21 +00002408{
wdenkfe8c2802002-11-03 00:38:21 +00002409 struct reserved_combo *r;
2410 for (r=reserved_list;
wdenkfe8c2802002-11-03 00:38:21 +00002411 r<reserved_list+NRES; r++) {
2412 if (strcmp(dest->data, r->literal) == 0) {
2413 debug_printf("found reserved word %s, code %d\n",r->literal,r->code);
2414 if (r->flag & FLAG_START) {
2415 struct p_context *new = xmalloc(sizeof(struct p_context));
2416 debug_printf("push stack\n");
2417 if (ctx->w == RES_IN || ctx->w == RES_FOR) {
2418 syntax();
2419 free(new);
2420 ctx->w = RES_SNTX;
2421 b_reset(dest);
2422 return 1;
2423 }
2424 *new = *ctx; /* physical copy */
2425 initialize_context(ctx);
2426 ctx->stack=new;
2427 } else if ( ctx->w == RES_NONE || ! (ctx->old_flag & (1<<r->code))) {
2428 syntax();
2429 ctx->w = RES_SNTX;
2430 b_reset(dest);
2431 return 1;
2432 }
2433 ctx->w=r->code;
2434 ctx->old_flag = r->flag;
2435 if (ctx->old_flag & FLAG_END) {
2436 struct p_context *old;
2437 debug_printf("pop stack\n");
2438 done_pipe(ctx,PIPE_SEQ);
2439 old = ctx->stack;
2440 old->child->group = ctx->list_head;
2441#ifndef __U_BOOT__
2442 old->child->subshell = 0;
2443#endif
2444 *ctx = *old; /* physical copy */
2445 free(old);
2446 }
2447 b_reset (dest);
2448 return 1;
2449 }
2450 }
2451 return 0;
2452}
2453
2454/* normal return is 0.
2455 * Syntax or xglob errors return 1. */
2456static int done_word(o_string *dest, struct p_context *ctx)
2457{
2458 struct child_prog *child=ctx->child;
2459#ifndef __U_BOOT__
2460 glob_t *glob_target;
2461 int gr, flags = 0;
2462#else
2463 char *str, *s;
2464 int argc, cnt;
2465#endif
2466
2467 debug_printf("done_word: %s %p\n", dest->data, child);
2468 if (dest->length == 0 && !dest->nonnull) {
2469 debug_printf(" true null, ignored\n");
2470 return 0;
2471 }
2472#ifndef __U_BOOT__
2473 if (ctx->pending_redirect) {
2474 glob_target = &ctx->pending_redirect->word;
2475 } else {
2476#endif
2477 if (child->group) {
2478 syntax();
2479 return 1; /* syntax error, groups and arglists don't mix */
2480 }
2481 if (!child->argv && (ctx->type & FLAG_PARSE_SEMICOLON)) {
2482 debug_printf("checking %s for reserved-ness\n",dest->data);
2483 if (reserved_word(dest,ctx)) return ctx->w==RES_SNTX;
2484 }
2485#ifndef __U_BOOT__
2486 glob_target = &child->glob_result;
wdenk8bde7f72003-06-27 21:31:46 +00002487 if (child->argv) flags |= GLOB_APPEND;
wdenkfe8c2802002-11-03 00:38:21 +00002488#else
2489 for (cnt = 1, s = dest->data; s && *s; s++) {
2490 if (*s == '\\') s++;
2491 cnt++;
2492 }
2493 str = malloc(cnt);
2494 if (!str) return 1;
2495 if ( child->argv == NULL) {
2496 child->argc=0;
2497 }
2498 argc = ++child->argc;
2499 child->argv = realloc(child->argv, (argc+1)*sizeof(*child->argv));
Peng Fanc6bb23c2015-11-27 10:12:02 +08002500 if (child->argv == NULL) {
2501 free(str);
2502 return 1;
2503 }
Stephen Warrenfe9ca3d2014-03-01 22:16:10 -07002504 child->argv_nonnull = realloc(child->argv_nonnull,
2505 (argc+1)*sizeof(*child->argv_nonnull));
Peng Fanc6bb23c2015-11-27 10:12:02 +08002506 if (child->argv_nonnull == NULL) {
2507 free(str);
Stephen Warrenfe9ca3d2014-03-01 22:16:10 -07002508 return 1;
Peng Fanc6bb23c2015-11-27 10:12:02 +08002509 }
wdenkfe8c2802002-11-03 00:38:21 +00002510 child->argv[argc-1]=str;
Stephen Warrenfe9ca3d2014-03-01 22:16:10 -07002511 child->argv_nonnull[argc-1] = dest->nonnull;
wdenkfe8c2802002-11-03 00:38:21 +00002512 child->argv[argc]=NULL;
Stephen Warrenfe9ca3d2014-03-01 22:16:10 -07002513 child->argv_nonnull[argc] = 0;
wdenkfe8c2802002-11-03 00:38:21 +00002514 for (s = dest->data; s && *s; s++,str++) {
2515 if (*s == '\\') s++;
2516 *str = *s;
2517 }
2518 *str = '\0';
2519#endif
2520#ifndef __U_BOOT__
2521 }
2522 gr = xglob(dest, flags, glob_target);
2523 if (gr != 0) return 1;
2524#endif
2525
2526 b_reset(dest);
2527#ifndef __U_BOOT__
2528 if (ctx->pending_redirect) {
2529 ctx->pending_redirect=NULL;
2530 if (glob_target->gl_pathc != 1) {
2531 error_msg("ambiguous redirect");
2532 return 1;
2533 }
2534 } else {
2535 child->argv = glob_target->gl_pathv;
2536 }
2537#endif
2538 if (ctx->w == RES_FOR) {
2539 done_word(dest,ctx);
2540 done_pipe(ctx,PIPE_SEQ);
2541 }
2542 return 0;
2543}
2544
2545/* The only possible error here is out of memory, in which case
2546 * xmalloc exits. */
2547static int done_command(struct p_context *ctx)
2548{
2549 /* The child is really already in the pipe structure, so
2550 * advance the pipe counter and make a new, null child.
2551 * Only real trickiness here is that the uncommitted
2552 * child structure, to which ctx->child points, is not
2553 * counted in pi->num_progs. */
2554 struct pipe *pi=ctx->pipe;
2555 struct child_prog *prog=ctx->child;
2556
2557 if (prog && prog->group == NULL
wdenk8bde7f72003-06-27 21:31:46 +00002558 && prog->argv == NULL
wdenkfe8c2802002-11-03 00:38:21 +00002559#ifndef __U_BOOT__
wdenk8bde7f72003-06-27 21:31:46 +00002560 && prog->redirects == NULL) {
wdenkfe8c2802002-11-03 00:38:21 +00002561#else
2562 ) {
2563#endif
2564 debug_printf("done_command: skipping null command\n");
2565 return 0;
2566 } else if (prog) {
2567 pi->num_progs++;
2568 debug_printf("done_command: num_progs incremented to %d\n",pi->num_progs);
2569 } else {
2570 debug_printf("done_command: initializing\n");
2571 }
2572 pi->progs = xrealloc(pi->progs, sizeof(*pi->progs) * (pi->num_progs+1));
2573
2574 prog = pi->progs + pi->num_progs;
2575#ifndef __U_BOOT__
2576 prog->redirects = NULL;
2577#endif
2578 prog->argv = NULL;
Stephen Warrenfe9ca3d2014-03-01 22:16:10 -07002579 prog->argv_nonnull = NULL;
wdenkfe8c2802002-11-03 00:38:21 +00002580#ifndef __U_BOOT__
2581 prog->is_stopped = 0;
2582#endif
2583 prog->group = NULL;
2584#ifndef __U_BOOT__
2585 prog->glob_result.gl_pathv = NULL;
2586 prog->family = pi;
2587#endif
2588 prog->sp = 0;
2589 ctx->child = prog;
2590 prog->type = ctx->type;
2591
2592 /* but ctx->pipe and ctx->list_head remain unchanged */
2593 return 0;
2594}
2595
2596static int done_pipe(struct p_context *ctx, pipe_style type)
2597{
2598 struct pipe *new_p;
2599 done_command(ctx); /* implicit closure of previous command */
2600 debug_printf("done_pipe, type %d\n", type);
2601 ctx->pipe->followup = type;
2602 ctx->pipe->r_mode = ctx->w;
2603 new_p=new_pipe();
2604 ctx->pipe->next = new_p;
2605 ctx->pipe = new_p;
2606 ctx->child = NULL;
2607 done_command(ctx); /* set up new pipe to accept commands */
2608 return 0;
2609}
2610
2611#ifndef __U_BOOT__
2612/* peek ahead in the in_str to find out if we have a "&n" construct,
2613 * as in "2>&1", that represents duplicating a file descriptor.
2614 * returns either -2 (syntax error), -1 (no &), or the number found.
2615 */
2616static int redirect_dup_num(struct in_str *input)
2617{
2618 int ch, d=0, ok=0;
2619 ch = b_peek(input);
2620 if (ch != '&') return -1;
2621
2622 b_getch(input); /* get the & */
2623 ch=b_peek(input);
2624 if (ch == '-') {
2625 b_getch(input);
2626 return -3; /* "-" represents "close me" */
2627 }
2628 while (isdigit(ch)) {
2629 d = d*10+(ch-'0');
2630 ok=1;
2631 b_getch(input);
2632 ch = b_peek(input);
2633 }
2634 if (ok) return d;
2635
2636 error_msg("ambiguous redirect");
2637 return -2;
2638}
2639
2640/* If a redirect is immediately preceded by a number, that number is
2641 * supposed to tell which file descriptor to redirect. This routine
2642 * looks for such preceding numbers. In an ideal world this routine
2643 * needs to handle all the following classes of redirects...
2644 * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo
2645 * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo
2646 * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo
2647 * echo 49x>foo # redirects fd 1 to file "foo", "49x" passed to echo
2648 * A -1 output from this program means no valid number was found, so the
2649 * caller should use the appropriate default for this redirection.
2650 */
2651static int redirect_opt_num(o_string *o)
2652{
2653 int num;
2654
2655 if (o->length==0) return -1;
2656 for(num=0; num<o->length; num++) {
2657 if (!isdigit(*(o->data+num))) {
2658 return -1;
2659 }
2660 }
2661 /* reuse num (and save an int) */
2662 num=atoi(o->data);
2663 b_reset(o);
2664 return num;
2665}
2666
2667FILE *generate_stream_from_list(struct pipe *head)
2668{
2669 FILE *pf;
2670#if 1
2671 int pid, channel[2];
2672 if (pipe(channel)<0) perror_msg_and_die("pipe");
2673 pid=fork();
2674 if (pid<0) {
2675 perror_msg_and_die("fork");
2676 } else if (pid==0) {
2677 close(channel[0]);
2678 if (channel[1] != 1) {
2679 dup2(channel[1],1);
2680 close(channel[1]);
2681 }
2682#if 0
2683#define SURROGATE "surrogate response"
2684 write(1,SURROGATE,sizeof(SURROGATE));
2685 _exit(run_list(head));
2686#else
2687 _exit(run_list_real(head)); /* leaks memory */
2688#endif
2689 }
2690 debug_printf("forked child %d\n",pid);
2691 close(channel[1]);
2692 pf = fdopen(channel[0],"r");
2693 debug_printf("pipe on FILE *%p\n",pf);
2694#else
2695 free_pipe_list(head,0);
2696 pf=popen("echo surrogate response","r");
2697 debug_printf("started fake pipe on FILE *%p\n",pf);
2698#endif
2699 return pf;
2700}
2701
2702/* this version hacked for testing purposes */
2703/* return code is exit status of the process that is run. */
2704static int process_command_subs(o_string *dest, struct p_context *ctx, struct in_str *input, int subst_end)
2705{
2706 int retcode;
2707 o_string result=NULL_O_STRING;
2708 struct p_context inner;
2709 FILE *p;
2710 struct in_str pipe_str;
2711 initialize_context(&inner);
2712
2713 /* recursion to generate command */
2714 retcode = parse_stream(&result, &inner, input, subst_end);
2715 if (retcode != 0) return retcode; /* syntax error or EOF */
2716 done_word(&result, &inner);
2717 done_pipe(&inner, PIPE_SEQ);
2718 b_free(&result);
2719
2720 p=generate_stream_from_list(inner.list_head);
2721 if (p==NULL) return 1;
2722 mark_open(fileno(p));
2723 setup_file_in_str(&pipe_str, p);
2724
2725 /* now send results of command back into original context */
2726 retcode = parse_stream(dest, ctx, &pipe_str, '\0');
2727 /* XXX In case of a syntax error, should we try to kill the child?
2728 * That would be tough to do right, so just read until EOF. */
2729 if (retcode == 1) {
2730 while (b_getch(&pipe_str)!=EOF) { /* discard */ };
2731 }
2732
2733 debug_printf("done reading from pipe, pclose()ing\n");
2734 /* This is the step that wait()s for the child. Should be pretty
2735 * safe, since we just read an EOF from its stdout. We could try
2736 * to better, by using wait(), and keeping track of background jobs
2737 * at the same time. That would be a lot of work, and contrary
2738 * to the KISS philosophy of this program. */
2739 mark_closed(fileno(p));
2740 retcode=pclose(p);
2741 free_pipe_list(inner.list_head,0);
2742 debug_printf("pclosed, retcode=%d\n",retcode);
2743 /* XXX this process fails to trim a single trailing newline */
2744 return retcode;
2745}
2746
2747static int parse_group(o_string *dest, struct p_context *ctx,
2748 struct in_str *input, int ch)
2749{
2750 int rcode, endch=0;
2751 struct p_context sub;
2752 struct child_prog *child = ctx->child;
2753 if (child->argv) {
2754 syntax();
2755 return 1; /* syntax error, groups and arglists don't mix */
2756 }
2757 initialize_context(&sub);
2758 switch(ch) {
2759 case '(': endch=')'; child->subshell=1; break;
2760 case '{': endch='}'; break;
2761 default: syntax(); /* really logic error */
2762 }
2763 rcode=parse_stream(dest,&sub,input,endch);
2764 done_word(dest,&sub); /* finish off the final word in the subcontext */
2765 done_pipe(&sub, PIPE_SEQ); /* and the final command there, too */
2766 child->group = sub.list_head;
2767 return rcode;
2768 /* child remains "open", available for possible redirects */
2769}
2770#endif
2771
2772/* basically useful version until someone wants to get fancier,
2773 * see the bash man page under "Parameter Expansion" */
2774static char *lookup_param(char *src)
2775{
wdenkc26e4542004-04-18 10:13:26 +00002776 char *p;
Joe Hershberger641b0d32012-08-17 10:26:29 +00002777 char *sep;
2778 char *default_val = NULL;
2779 int assign = 0;
2780 int expand_empty = 0;
wdenkc26e4542004-04-18 10:13:26 +00002781
2782 if (!src)
2783 return NULL;
2784
Joe Hershberger641b0d32012-08-17 10:26:29 +00002785 sep = strchr(src, ':');
2786
2787 if (sep) {
2788 *sep = '\0';
2789 if (*(sep + 1) == '-')
2790 default_val = sep+2;
2791 if (*(sep + 1) == '=') {
2792 default_val = sep+2;
2793 assign = 1;
2794 }
2795 if (*(sep + 1) == '+') {
2796 default_val = sep+2;
2797 expand_empty = 1;
2798 }
2799 }
2800
Simon Glass00caae62017-08-03 12:22:12 -06002801 p = env_get(src);
Joe Hershberger641b0d32012-08-17 10:26:29 +00002802 if (!p)
2803 p = get_local_var(src);
2804
2805 if (!p || strlen(p) == 0) {
2806 p = default_val;
2807 if (assign) {
2808 char *var = malloc(strlen(src)+strlen(default_val)+2);
2809 if (var) {
2810 sprintf(var, "%s=%s", src, default_val);
2811 set_local_var(var, 0);
2812 }
2813 free(var);
2814 }
2815 } else if (expand_empty) {
2816 p += strlen(p);
2817 }
2818
2819 if (sep)
2820 *sep = ':';
wdenkc26e4542004-04-18 10:13:26 +00002821
wdenkfe8c2802002-11-03 00:38:21 +00002822 return p;
2823}
2824
wdenkc26e4542004-04-18 10:13:26 +00002825#ifdef __U_BOOT__
2826static char *get_dollar_var(char ch)
2827{
2828 static char buf[40];
2829
2830 buf[0] = '\0';
2831 switch (ch) {
2832 case '?':
2833 sprintf(buf, "%u", (unsigned int)last_return_code);
2834 break;
2835 default:
2836 return NULL;
2837 }
2838 return buf;
2839}
2840#endif
2841
wdenkfe8c2802002-11-03 00:38:21 +00002842/* return code: 0 for OK, 1 for syntax error */
2843static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *input)
2844{
2845#ifndef __U_BOOT__
2846 int i, advance=0;
2847#else
2848 int advance=0;
2849#endif
2850#ifndef __U_BOOT__
2851 char sep[]=" ";
2852#endif
2853 int ch = input->peek(input); /* first character after the $ */
2854 debug_printf("handle_dollar: ch=%c\n",ch);
2855 if (isalpha(ch)) {
2856 b_addchr(dest, SPECIAL_VAR_SYMBOL);
2857 ctx->child->sp++;
2858 while(ch=b_peek(input),isalnum(ch) || ch=='_') {
2859 b_getch(input);
2860 b_addchr(dest,ch);
2861 }
2862 b_addchr(dest, SPECIAL_VAR_SYMBOL);
2863#ifndef __U_BOOT__
2864 } else if (isdigit(ch)) {
2865 i = ch-'0'; /* XXX is $0 special? */
2866 if (i<global_argc) {
2867 parse_string(dest, ctx, global_argv[i]); /* recursion */
2868 }
2869 advance = 1;
2870#endif
2871 } else switch (ch) {
2872#ifndef __U_BOOT__
2873 case '$':
2874 b_adduint(dest,getpid());
2875 advance = 1;
2876 break;
2877 case '!':
2878 if (last_bg_pid > 0) b_adduint(dest, last_bg_pid);
2879 advance = 1;
2880 break;
2881#endif
2882 case '?':
wdenkc26e4542004-04-18 10:13:26 +00002883#ifndef __U_BOOT__
wdenkfe8c2802002-11-03 00:38:21 +00002884 b_adduint(dest,last_return_code);
wdenkc26e4542004-04-18 10:13:26 +00002885#else
2886 ctx->child->sp++;
2887 b_addchr(dest, SPECIAL_VAR_SYMBOL);
2888 b_addchr(dest, '$');
2889 b_addchr(dest, '?');
2890 b_addchr(dest, SPECIAL_VAR_SYMBOL);
2891#endif
wdenkfe8c2802002-11-03 00:38:21 +00002892 advance = 1;
2893 break;
2894#ifndef __U_BOOT__
2895 case '#':
2896 b_adduint(dest,global_argc ? global_argc-1 : 0);
2897 advance = 1;
2898 break;
2899#endif
2900 case '{':
2901 b_addchr(dest, SPECIAL_VAR_SYMBOL);
2902 ctx->child->sp++;
2903 b_getch(input);
2904 /* XXX maybe someone will try to escape the '}' */
2905 while(ch=b_getch(input),ch!=EOF && ch!='}') {
2906 b_addchr(dest,ch);
2907 }
2908 if (ch != '}') {
2909 syntax();
2910 return 1;
2911 }
2912 b_addchr(dest, SPECIAL_VAR_SYMBOL);
2913 break;
2914#ifndef __U_BOOT__
2915 case '(':
2916 b_getch(input);
2917 process_command_subs(dest, ctx, input, ')');
2918 break;
2919 case '*':
2920 sep[0]=ifs[0];
2921 for (i=1; i<global_argc; i++) {
2922 parse_string(dest, ctx, global_argv[i]);
2923 if (i+1 < global_argc) parse_string(dest, ctx, sep);
2924 }
2925 break;
2926 case '@':
2927 case '-':
2928 case '_':
2929 /* still unhandled, but should be eventually */
2930 error_msg("unhandled syntax: $%c",ch);
2931 return 1;
2932 break;
2933#endif
2934 default:
2935 b_addqchr(dest,'$',dest->quote);
2936 }
2937 /* Eat the character if the flag was set. If the compiler
2938 * is smart enough, we could substitute "b_getch(input);"
2939 * for all the "advance = 1;" above, and also end up with
2940 * a nice size-optimized program. Hah! That'll be the day.
2941 */
2942 if (advance) b_getch(input);
2943 return 0;
2944}
2945
2946#ifndef __U_BOOT__
2947int parse_string(o_string *dest, struct p_context *ctx, const char *src)
2948{
2949 struct in_str foo;
2950 setup_string_in_str(&foo, src);
2951 return parse_stream(dest, ctx, &foo, '\0');
2952}
2953#endif
2954
2955/* return code is 0 for normal exit, 1 for syntax error */
Kim Phillips199adb62012-10-29 13:34:32 +00002956static int parse_stream(o_string *dest, struct p_context *ctx,
2957 struct in_str *input, int end_trigger)
wdenkfe8c2802002-11-03 00:38:21 +00002958{
2959 unsigned int ch, m;
2960#ifndef __U_BOOT__
2961 int redir_fd;
2962 redir_type redir_style;
2963#endif
2964 int next;
2965
2966 /* Only double-quote state is handled in the state variable dest->quote.
2967 * A single-quote triggers a bypass of the main loop until its mate is
2968 * found. When recursing, quote state is passed in via dest->quote. */
2969
2970 debug_printf("parse_stream, end_trigger=%d\n",end_trigger);
2971 while ((ch=b_getch(input))!=EOF) {
2972 m = map[ch];
2973#ifdef __U_BOOT__
2974 if (input->__promptme == 0) return 1;
2975#endif
2976 next = (ch == '\n') ? 0 : b_peek(input);
wdenkc26e4542004-04-18 10:13:26 +00002977
2978 debug_printf("parse_stream: ch=%c (%d) m=%d quote=%d - %c\n",
2979 ch >= ' ' ? ch : '.', ch, m,
2980 dest->quote, ctx->stack == NULL ? '*' : '.');
2981
wdenkfe8c2802002-11-03 00:38:21 +00002982 if (m==0 || ((m==1 || m==2) && dest->quote)) {
2983 b_addqchr(dest, ch, dest->quote);
2984 } else {
2985 if (m==2) { /* unquoted IFS */
2986 if (done_word(dest, ctx)) {
2987 return 1;
2988 }
2989 /* If we aren't performing a substitution, treat a newline as a
2990 * command separator. */
2991 if (end_trigger != '\0' && ch=='\n')
2992 done_pipe(ctx,PIPE_SEQ);
2993 }
2994 if (ch == end_trigger && !dest->quote && ctx->w==RES_NONE) {
2995 debug_printf("leaving parse_stream (triggered)\n");
2996 return 0;
2997 }
2998#if 0
2999 if (ch=='\n') {
3000 /* Yahoo! Time to run with it! */
3001 done_pipe(ctx,PIPE_SEQ);
3002 run_list(ctx->list_head);
3003 initialize_context(ctx);
3004 }
3005#endif
3006 if (m!=2) switch (ch) {
3007 case '#':
3008 if (dest->length == 0 && !dest->quote) {
3009 while(ch=b_peek(input),ch!=EOF && ch!='\n') { b_getch(input); }
3010 } else {
3011 b_addqchr(dest, ch, dest->quote);
3012 }
3013 break;
3014 case '\\':
3015 if (next == EOF) {
3016 syntax();
3017 return 1;
3018 }
3019 b_addqchr(dest, '\\', dest->quote);
3020 b_addqchr(dest, b_getch(input), dest->quote);
3021 break;
3022 case '$':
3023 if (handle_dollar(dest, ctx, input)!=0) return 1;
3024 break;
3025 case '\'':
3026 dest->nonnull = 1;
3027 while(ch=b_getch(input),ch!=EOF && ch!='\'') {
3028#ifdef __U_BOOT__
3029 if(input->__promptme == 0) return 1;
3030#endif
3031 b_addchr(dest,ch);
3032 }
3033 if (ch==EOF) {
3034 syntax();
3035 return 1;
3036 }
3037 break;
3038 case '"':
3039 dest->nonnull = 1;
3040 dest->quote = !dest->quote;
3041 break;
3042#ifndef __U_BOOT__
3043 case '`':
3044 process_command_subs(dest, ctx, input, '`');
3045 break;
3046 case '>':
3047 redir_fd = redirect_opt_num(dest);
3048 done_word(dest, ctx);
3049 redir_style=REDIRECT_OVERWRITE;
3050 if (next == '>') {
3051 redir_style=REDIRECT_APPEND;
3052 b_getch(input);
3053 } else if (next == '(') {
3054 syntax(); /* until we support >(list) Process Substitution */
3055 return 1;
3056 }
3057 setup_redirect(ctx, redir_fd, redir_style, input);
3058 break;
3059 case '<':
3060 redir_fd = redirect_opt_num(dest);
3061 done_word(dest, ctx);
3062 redir_style=REDIRECT_INPUT;
3063 if (next == '<') {
3064 redir_style=REDIRECT_HEREIS;
3065 b_getch(input);
3066 } else if (next == '>') {
3067 redir_style=REDIRECT_IO;
3068 b_getch(input);
3069 } else if (next == '(') {
3070 syntax(); /* until we support <(list) Process Substitution */
3071 return 1;
3072 }
3073 setup_redirect(ctx, redir_fd, redir_style, input);
3074 break;
3075#endif
3076 case ';':
3077 done_word(dest, ctx);
3078 done_pipe(ctx,PIPE_SEQ);
3079 break;
3080 case '&':
3081 done_word(dest, ctx);
3082 if (next=='&') {
3083 b_getch(input);
3084 done_pipe(ctx,PIPE_AND);
3085 } else {
3086#ifndef __U_BOOT__
3087 done_pipe(ctx,PIPE_BG);
3088#else
3089 syntax_err();
3090 return 1;
3091#endif
3092 }
3093 break;
3094 case '|':
3095 done_word(dest, ctx);
3096 if (next=='|') {
3097 b_getch(input);
3098 done_pipe(ctx,PIPE_OR);
3099 } else {
3100 /* we could pick up a file descriptor choice here
3101 * with redirect_opt_num(), but bash doesn't do it.
3102 * "echo foo 2| cat" yields "foo 2". */
3103#ifndef __U_BOOT__
3104 done_command(ctx);
3105#else
3106 syntax_err();
3107 return 1;
3108#endif
3109 }
3110 break;
3111#ifndef __U_BOOT__
3112 case '(':
3113 case '{':
3114 if (parse_group(dest, ctx, input, ch)!=0) return 1;
3115 break;
3116 case ')':
3117 case '}':
3118 syntax(); /* Proper use of this character caught by end_trigger */
3119 return 1;
3120 break;
3121#endif
Joe Hershbergera005f192012-08-17 10:26:30 +00003122 case SUBSTED_VAR_SYMBOL:
3123 dest->nonnull = 1;
3124 while (ch = b_getch(input), ch != EOF &&
3125 ch != SUBSTED_VAR_SYMBOL) {
3126 debug_printf("subst, pass=%d\n", ch);
3127 if (input->__promptme == 0)
3128 return 1;
3129 b_addchr(dest, ch);
3130 }
3131 debug_printf("subst, term=%d\n", ch);
3132 if (ch == EOF) {
3133 syntax();
3134 return 1;
3135 }
3136 break;
wdenkfe8c2802002-11-03 00:38:21 +00003137 default:
3138 syntax(); /* this is really an internal logic error */
3139 return 1;
3140 }
3141 }
3142 }
3143 /* complain if quote? No, maybe we just finished a command substitution
3144 * that was quoted. Example:
3145 * $ echo "`cat foo` plus more"
3146 * and we just got the EOF generated by the subshell that ran "cat foo"
3147 * The only real complaint is if we got an EOF when end_trigger != '\0',
3148 * that is, we were really supposed to get end_trigger, and never got
3149 * one before the EOF. Can't use the standard "syntax error" return code,
3150 * so that parse_stream_outer can distinguish the EOF and exit smoothly. */
3151 debug_printf("leaving parse_stream (EOF)\n");
3152 if (end_trigger != '\0') return -1;
3153 return 0;
3154}
3155
Kim Phillips199adb62012-10-29 13:34:32 +00003156static void mapset(const unsigned char *set, int code)
wdenkfe8c2802002-11-03 00:38:21 +00003157{
3158 const unsigned char *s;
3159 for (s=set; *s; s++) map[*s] = code;
3160}
3161
Kim Phillips199adb62012-10-29 13:34:32 +00003162static void update_ifs_map(void)
wdenkfe8c2802002-11-03 00:38:21 +00003163{
3164 /* char *ifs and char map[256] are both globals. */
Simon Glass00caae62017-08-03 12:22:12 -06003165 ifs = (uchar *)env_get("IFS");
Wolfgang Denk77ddac92005-10-13 16:45:02 +02003166 if (ifs == NULL) ifs=(uchar *)" \t\n";
wdenkfe8c2802002-11-03 00:38:21 +00003167 /* Precompute a list of 'flow through' behavior so it can be treated
3168 * quickly up front. Computation is necessary because of IFS.
3169 * Special case handling of IFS == " \t\n" is not implemented.
3170 * The map[] array only really needs two bits each, and on most machines
3171 * that would be faster because of the reduced L1 cache footprint.
3172 */
3173 memset(map,0,sizeof(map)); /* most characters flow through always */
3174#ifndef __U_BOOT__
Wolfgang Denk77ddac92005-10-13 16:45:02 +02003175 mapset((uchar *)"\\$'\"`", 3); /* never flow through */
3176 mapset((uchar *)"<>;&|(){}#", 1); /* flow through if quoted */
wdenkfe8c2802002-11-03 00:38:21 +00003177#else
Joe Hershbergera005f192012-08-17 10:26:30 +00003178 {
3179 uchar subst[2] = {SUBSTED_VAR_SYMBOL, 0};
3180 mapset(subst, 3); /* never flow through */
3181 }
Wolfgang Denk77ddac92005-10-13 16:45:02 +02003182 mapset((uchar *)"\\$'\"", 3); /* never flow through */
3183 mapset((uchar *)";&|#", 1); /* flow through if quoted */
wdenkfe8c2802002-11-03 00:38:21 +00003184#endif
3185 mapset(ifs, 2); /* also flow through if quoted */
3186}
3187
3188/* most recursion does not come through here, the exeception is
3189 * from builtin_source() */
Kim Phillips199adb62012-10-29 13:34:32 +00003190static int parse_stream_outer(struct in_str *inp, int flag)
wdenkfe8c2802002-11-03 00:38:21 +00003191{
3192
3193 struct p_context ctx;
3194 o_string temp=NULL_O_STRING;
3195 int rcode;
3196#ifdef __U_BOOT__
Rabin Vincent2302b3a2014-10-29 23:21:41 +01003197 int code = 1;
wdenkfe8c2802002-11-03 00:38:21 +00003198#endif
3199 do {
3200 ctx.type = flag;
3201 initialize_context(&ctx);
3202 update_ifs_map();
Wolfgang Denk77ddac92005-10-13 16:45:02 +02003203 if (!(flag & FLAG_PARSE_SEMICOLON) || (flag & FLAG_REPARSING)) mapset((uchar *)";$&|", 0);
wdenkfe8c2802002-11-03 00:38:21 +00003204 inp->promptmode=1;
Simon Glass87b63982014-10-07 13:59:43 -06003205 rcode = parse_stream(&temp, &ctx, inp,
3206 flag & FLAG_CONT_ON_NEWLINE ? -1 : '\n');
wdenkfe8c2802002-11-03 00:38:21 +00003207#ifdef __U_BOOT__
3208 if (rcode == 1) flag_repeat = 0;
3209#endif
3210 if (rcode != 1 && ctx.old_flag != 0) {
3211 syntax();
3212#ifdef __U_BOOT__
3213 flag_repeat = 0;
3214#endif
3215 }
3216 if (rcode != 1 && ctx.old_flag == 0) {
3217 done_word(&temp, &ctx);
3218 done_pipe(&ctx,PIPE_SEQ);
3219#ifndef __U_BOOT__
3220 run_list(ctx.list_head);
3221#else
wdenkc26e4542004-04-18 10:13:26 +00003222 code = run_list(ctx.list_head);
3223 if (code == -2) { /* exit */
3224 b_free(&temp);
3225 code = 0;
3226 /* XXX hackish way to not allow exit from main loop */
3227 if (inp->peek == file_peek) {
3228 printf("exit not allowed from main input shell.\n");
3229 continue;
3230 }
3231 break;
3232 }
3233 if (code == -1)
wdenkfe8c2802002-11-03 00:38:21 +00003234 flag_repeat = 0;
3235#endif
3236 } else {
3237 if (ctx.old_flag != 0) {
3238 free(ctx.stack);
3239 b_reset(&temp);
3240 }
3241#ifdef __U_BOOT__
3242 if (inp->__promptme == 0) printf("<INTERRUPT>\n");
3243 inp->__promptme = 1;
3244#endif
3245 temp.nonnull = 0;
3246 temp.quote = 0;
3247 inp->p = NULL;
3248 free_pipe_list(ctx.list_head,0);
3249 }
3250 b_free(&temp);
Simon Glass587e1d42014-05-30 14:41:50 -06003251 /* loop on syntax errors, return on EOF */
Rabin Vincentf3a05c82014-11-21 23:05:22 +01003252 } while (rcode != -1 && !(flag & FLAG_EXIT_FROM_LOOP) &&
Simon Glass587e1d42014-05-30 14:41:50 -06003253 (inp->peek != static_peek || b_peek(inp)));
wdenkfe8c2802002-11-03 00:38:21 +00003254#ifndef __U_BOOT__
3255 return 0;
3256#else
3257 return (code != 0) ? 1 : 0;
3258#endif /* __U_BOOT__ */
3259}
3260
3261#ifndef __U_BOOT__
3262static int parse_string_outer(const char *s, int flag)
3263#else
Jason Hobbsc8a20792011-08-31 05:37:24 +00003264int parse_string_outer(const char *s, int flag)
wdenkfe8c2802002-11-03 00:38:21 +00003265#endif /* __U_BOOT__ */
3266{
3267 struct in_str input;
3268#ifdef __U_BOOT__
3269 char *p = NULL;
3270 int rcode;
Rabin Vincent484408f2014-10-29 23:21:39 +01003271 if (!s)
wdenkfe8c2802002-11-03 00:38:21 +00003272 return 1;
Rabin Vincent484408f2014-10-29 23:21:39 +01003273 if (!*s)
3274 return 0;
xia.jin291268e2024-06-14 08:27:14 +00003275#ifdef CONFIG_ARMV8_MULTIENTRY
xia.jin83aee562024-08-07 02:35:05 +00003276 if (lock_holder != get_core_id() && (gd->flags & GD_FLG_SMP)) {
xia.jin291268e2024-06-14 08:27:14 +00003277 spin_lock(&cmd_lock);
3278 lock_holder = get_core_id();
3279 }
3280 lock_depth++;
3281#endif
wdenkfe8c2802002-11-03 00:38:21 +00003282 if (!(p = strchr(s, '\n')) || *++p) {
3283 p = xmalloc(strlen(s) + 2);
3284 strcpy(p, s);
3285 strcat(p, "\n");
3286 setup_string_in_str(&input, p);
3287 rcode = parse_stream_outer(&input, flag);
3288 free(p);
xia.jin291268e2024-06-14 08:27:14 +00003289 #ifndef CONFIG_ARMV8_MULTIENTRY
wdenkfe8c2802002-11-03 00:38:21 +00003290 return rcode;
xia.jin291268e2024-06-14 08:27:14 +00003291 #endif
wdenkfe8c2802002-11-03 00:38:21 +00003292 } else {
3293#endif
3294 setup_string_in_str(&input, s);
xia.jin291268e2024-06-14 08:27:14 +00003295#ifdef CONFIG_ARMV8_MULTIENTRY
3296 rcode = parse_stream_outer(&input, flag);
3297#else
wdenkfe8c2802002-11-03 00:38:21 +00003298 return parse_stream_outer(&input, flag);
xia.jin291268e2024-06-14 08:27:14 +00003299#endif
wdenkfe8c2802002-11-03 00:38:21 +00003300#ifdef __U_BOOT__
3301 }
3302#endif
xia.jin291268e2024-06-14 08:27:14 +00003303#ifdef CONFIG_ARMV8_MULTIENTRY
3304 lock_depth--;
3305 if (!lock_depth) {
3306 lock_holder = -1;
3307 spin_unlock(&cmd_lock);
3308 }
3309 return rcode;
3310#endif
wdenkfe8c2802002-11-03 00:38:21 +00003311}
3312
3313#ifndef __U_BOOT__
3314static int parse_file_outer(FILE *f)
3315#else
3316int parse_file_outer(void)
3317#endif
3318{
3319 int rcode;
3320 struct in_str input;
3321#ifndef __U_BOOT__
3322 setup_file_in_str(&input, f);
3323#else
3324 setup_file_in_str(&input);
3325#endif
3326 rcode = parse_stream_outer(&input, FLAG_PARSE_SEMICOLON);
3327 return rcode;
3328}
3329
3330#ifdef __U_BOOT__
Wolfgang Denk2e5167c2010-10-28 20:00:11 +02003331#ifdef CONFIG_NEEDS_MANUAL_RELOC
wdenk3e386912003-04-05 00:53:31 +00003332static void u_boot_hush_reloc(void)
3333{
wdenk3e386912003-04-05 00:53:31 +00003334 unsigned long addr;
3335 struct reserved_combo *r;
3336
3337 for (r=reserved_list; r<reserved_list+NRES; r++) {
3338 addr = (ulong) (r->literal) + gd->reloc_off;
3339 r->literal = (char *)addr;
3340 }
3341}
Peter Tyser521af042009-09-21 11:20:36 -05003342#endif
wdenk3e386912003-04-05 00:53:31 +00003343
wdenkfe8c2802002-11-03 00:38:21 +00003344int u_boot_hush_start(void)
3345{
wdenk2d5b5612003-10-14 19:43:55 +00003346 if (top_vars == NULL) {
3347 top_vars = malloc(sizeof(struct variables));
3348 top_vars->name = "HUSH_VERSION";
3349 top_vars->value = "0.01";
Kim Phillips199adb62012-10-29 13:34:32 +00003350 top_vars->next = NULL;
wdenk2d5b5612003-10-14 19:43:55 +00003351 top_vars->flg_export = 0;
3352 top_vars->flg_read_only = 1;
Wolfgang Denk2e5167c2010-10-28 20:00:11 +02003353#ifdef CONFIG_NEEDS_MANUAL_RELOC
wdenk2d5b5612003-10-14 19:43:55 +00003354 u_boot_hush_reloc();
Peter Tyser521af042009-09-21 11:20:36 -05003355#endif
wdenk2d5b5612003-10-14 19:43:55 +00003356 }
wdenkfe8c2802002-11-03 00:38:21 +00003357 return 0;
3358}
3359
3360static void *xmalloc(size_t size)
3361{
3362 void *p = NULL;
3363
3364 if (!(p = malloc(size))) {
peng.wang@smartm.com6c353b32021-05-04 01:45:59 -07003365 printf("ERROR : xmalloc failed\n");
wdenkfe8c2802002-11-03 00:38:21 +00003366 for(;;);
3367 }
3368 return p;
3369}
3370
3371static void *xrealloc(void *ptr, size_t size)
3372{
3373 void *p = NULL;
3374
3375 if (!(p = realloc(ptr, size))) {
peng.wang@smartm.com6c353b32021-05-04 01:45:59 -07003376 printf("ERROR : xrealloc failed\n");
wdenkfe8c2802002-11-03 00:38:21 +00003377 for(;;);
3378 }
3379 return p;
3380}
3381#endif /* __U_BOOT__ */
3382
3383#ifndef __U_BOOT__
3384/* Make sure we have a controlling tty. If we get started under a job
3385 * aware app (like bash for example), make sure we are now in charge so
3386 * we don't fight over who gets the foreground */
wdenkd0fb80c2003-01-11 09:48:40 +00003387static void setup_job_control(void)
wdenkfe8c2802002-11-03 00:38:21 +00003388{
3389 static pid_t shell_pgrp;
3390 /* Loop until we are in the foreground. */
3391 while (tcgetpgrp (shell_terminal) != (shell_pgrp = getpgrp ()))
3392 kill (- shell_pgrp, SIGTTIN);
3393
3394 /* Ignore interactive and job-control signals. */
3395 signal(SIGINT, SIG_IGN);
3396 signal(SIGQUIT, SIG_IGN);
3397 signal(SIGTERM, SIG_IGN);
3398 signal(SIGTSTP, SIG_IGN);
3399 signal(SIGTTIN, SIG_IGN);
3400 signal(SIGTTOU, SIG_IGN);
3401 signal(SIGCHLD, SIG_IGN);
3402
3403 /* Put ourselves in our own process group. */
3404 setsid();
3405 shell_pgrp = getpid ();
3406 setpgid (shell_pgrp, shell_pgrp);
3407
3408 /* Grab control of the terminal. */
3409 tcsetpgrp(shell_terminal, shell_pgrp);
3410}
3411
Wolfgang Denk54841ab2010-06-28 22:00:46 +02003412int hush_main(int argc, char * const *argv)
wdenkfe8c2802002-11-03 00:38:21 +00003413{
3414 int opt;
3415 FILE *input;
3416 char **e = environ;
3417
3418 /* XXX what should these be while sourcing /etc/profile? */
3419 global_argc = argc;
3420 global_argv = argv;
3421
3422 /* (re?) initialize globals. Sometimes hush_main() ends up calling
3423 * hush_main(), therefore we cannot rely on the BSS to zero out this
3424 * stuff. Reset these to 0 every time. */
3425 ifs = NULL;
3426 /* map[] is taken care of with call to update_ifs_map() */
3427 fake_mode = 0;
3428 interactive = 0;
3429 close_me_head = NULL;
3430 last_bg_pid = 0;
3431 job_list = NULL;
3432 last_jobid = 0;
3433
3434 /* Initialize some more globals to non-zero values */
3435 set_cwd();
wdenkd0fb80c2003-01-11 09:48:40 +00003436#ifdef CONFIG_FEATURE_COMMAND_EDITING
wdenkfe8c2802002-11-03 00:38:21 +00003437 cmdedit_set_initial_prompt();
3438#else
3439 PS1 = NULL;
3440#endif
3441 PS2 = "> ";
3442
3443 /* initialize our shell local variables with the values
3444 * currently living in the environment */
3445 if (e) {
3446 for (; *e; e++)
3447 set_local_var(*e, 2); /* without call putenv() */
3448 }
3449
3450 last_return_code=EXIT_SUCCESS;
3451
3452
3453 if (argv[0] && argv[0][0] == '-') {
3454 debug_printf("\nsourcing /etc/profile\n");
3455 if ((input = fopen("/etc/profile", "r")) != NULL) {
3456 mark_open(fileno(input));
3457 parse_file_outer(input);
3458 mark_closed(fileno(input));
3459 fclose(input);
3460 }
3461 }
3462 input=stdin;
3463
3464 while ((opt = getopt(argc, argv, "c:xif")) > 0) {
3465 switch (opt) {
3466 case 'c':
3467 {
3468 global_argv = argv+optind;
3469 global_argc = argc-optind;
3470 opt = parse_string_outer(optarg, FLAG_PARSE_SEMICOLON);
3471 goto final_return;
3472 }
3473 break;
3474 case 'i':
3475 interactive++;
3476 break;
3477 case 'f':
3478 fake_mode++;
3479 break;
3480 default:
3481#ifndef BB_VER
3482 fprintf(stderr, "Usage: sh [FILE]...\n"
3483 " or: sh -c command [args]...\n\n");
3484 exit(EXIT_FAILURE);
3485#else
3486 show_usage();
3487#endif
3488 }
3489 }
3490 /* A shell is interactive if the `-i' flag was given, or if all of
3491 * the following conditions are met:
3492 * no -c command
3493 * no arguments remaining or the -s flag given
3494 * standard input is a terminal
3495 * standard output is a terminal
3496 * Refer to Posix.2, the description of the `sh' utility. */
3497 if (argv[optind]==NULL && input==stdin &&
3498 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
3499 interactive++;
3500 }
3501
3502 debug_printf("\ninteractive=%d\n", interactive);
3503 if (interactive) {
3504 /* Looks like they want an interactive shell */
wdenk8bde7f72003-06-27 21:31:46 +00003505#ifndef CONFIG_FEATURE_SH_EXTRA_QUIET
wdenkd0fb80c2003-01-11 09:48:40 +00003506 printf( "\n\n" BB_BANNER " hush - the humble shell v0.01 (testing)\n");
3507 printf( "Enter 'help' for a list of built-in commands.\n\n");
3508#endif
wdenkfe8c2802002-11-03 00:38:21 +00003509 setup_job_control();
3510 }
3511
3512 if (argv[optind]==NULL) {
3513 opt=parse_file_outer(stdin);
3514 goto final_return;
3515 }
3516
3517 debug_printf("\nrunning script '%s'\n", argv[optind]);
3518 global_argv = argv+optind;
3519 global_argc = argc-optind;
3520 input = xfopen(argv[optind], "r");
3521 opt = parse_file_outer(input);
3522
wdenkd0fb80c2003-01-11 09:48:40 +00003523#ifdef CONFIG_FEATURE_CLEAN_UP
wdenkfe8c2802002-11-03 00:38:21 +00003524 fclose(input);
3525 if (cwd && cwd != unknown)
3526 free((char*)cwd);
3527 {
3528 struct variables *cur, *tmp;
3529 for(cur = top_vars; cur; cur = tmp) {
3530 tmp = cur->next;
3531 if (!cur->flg_read_only) {
3532 free(cur->name);
3533 free(cur->value);
3534 free(cur);
3535 }
3536 }
3537 }
3538#endif
3539
3540final_return:
3541 return(opt?opt:last_return_code);
3542}
3543#endif
3544
3545static char *insert_var_value(char *inp)
3546{
Joe Hershbergera005f192012-08-17 10:26:30 +00003547 return insert_var_value_sub(inp, 0);
3548}
3549
3550static char *insert_var_value_sub(char *inp, int tag_subst)
3551{
wdenkfe8c2802002-11-03 00:38:21 +00003552 int res_str_len = 0;
3553 int len;
3554 int done = 0;
3555 char *p, *p1, *res_str = NULL;
3556
3557 while ((p = strchr(inp, SPECIAL_VAR_SYMBOL))) {
Nishanth Menon8405b8d2015-12-04 13:02:11 -06003558 /* check the beginning of the string for normal characters */
wdenkfe8c2802002-11-03 00:38:21 +00003559 if (p != inp) {
Nishanth Menon8405b8d2015-12-04 13:02:11 -06003560 /* copy any characters to the result string */
wdenkfe8c2802002-11-03 00:38:21 +00003561 len = p - inp;
3562 res_str = xrealloc(res_str, (res_str_len + len));
3563 strncpy((res_str + res_str_len), inp, len);
3564 res_str_len += len;
3565 }
3566 inp = ++p;
Joe Hershbergera005f192012-08-17 10:26:30 +00003567 /* find the ending marker */
wdenkfe8c2802002-11-03 00:38:21 +00003568 p = strchr(inp, SPECIAL_VAR_SYMBOL);
3569 *p = '\0';
Joe Hershbergera005f192012-08-17 10:26:30 +00003570 /* look up the value to substitute */
wdenkfe8c2802002-11-03 00:38:21 +00003571 if ((p1 = lookup_param(inp))) {
Joe Hershbergera005f192012-08-17 10:26:30 +00003572 if (tag_subst)
3573 len = res_str_len + strlen(p1) + 2;
3574 else
3575 len = res_str_len + strlen(p1);
wdenkfe8c2802002-11-03 00:38:21 +00003576 res_str = xrealloc(res_str, (1 + len));
Joe Hershbergera005f192012-08-17 10:26:30 +00003577 if (tag_subst) {
3578 /*
3579 * copy the variable value to the result
3580 * string
3581 */
3582 strcpy((res_str + res_str_len + 1), p1);
3583
3584 /*
3585 * mark the replaced text to be accepted as
3586 * is
3587 */
3588 res_str[res_str_len] = SUBSTED_VAR_SYMBOL;
3589 res_str[res_str_len + 1 + strlen(p1)] =
3590 SUBSTED_VAR_SYMBOL;
3591 } else
3592 /*
3593 * copy the variable value to the result
3594 * string
3595 */
3596 strcpy((res_str + res_str_len), p1);
3597
wdenkfe8c2802002-11-03 00:38:21 +00003598 res_str_len = len;
3599 }
3600 *p = SPECIAL_VAR_SYMBOL;
3601 inp = ++p;
3602 done = 1;
3603 }
3604 if (done) {
3605 res_str = xrealloc(res_str, (1 + res_str_len + strlen(inp)));
3606 strcpy((res_str + res_str_len), inp);
3607 while ((p = strchr(res_str, '\n'))) {
3608 *p = ' ';
3609 }
3610 }
3611 return (res_str == NULL) ? inp : res_str;
3612}
3613
3614static char **make_list_in(char **inp, char *name)
3615{
3616 int len, i;
3617 int name_len = strlen(name);
3618 int n = 0;
3619 char **list;
3620 char *p1, *p2, *p3;
3621
3622 /* create list of variable values */
3623 list = xmalloc(sizeof(*list));
3624 for (i = 0; inp[i]; i++) {
3625 p3 = insert_var_value(inp[i]);
3626 p1 = p3;
3627 while (*p1) {
Jeroen Hofstee930e4252014-06-11 00:28:47 +02003628 if (*p1 == ' ') {
wdenkfe8c2802002-11-03 00:38:21 +00003629 p1++;
3630 continue;
3631 }
3632 if ((p2 = strchr(p1, ' '))) {
3633 len = p2 - p1;
3634 } else {
3635 len = strlen(p1);
3636 p2 = p1 + len;
3637 }
3638 /* we use n + 2 in realloc for list,because we add
3639 * new element and then we will add NULL element */
3640 list = xrealloc(list, sizeof(*list) * (n + 2));
3641 list[n] = xmalloc(2 + name_len + len);
3642 strcpy(list[n], name);
3643 strcat(list[n], "=");
3644 strncat(list[n], p1, len);
3645 list[n++][name_len + len + 1] = '\0';
3646 p1 = p2;
3647 }
3648 if (p3 != inp[i]) free(p3);
3649 }
3650 list[n] = NULL;
3651 return list;
3652}
3653
Stephen Warrenfe9ca3d2014-03-01 22:16:10 -07003654/*
3655 * Make new string for parser
3656 * inp - array of argument strings to flatten
3657 * nonnull - indicates argument was quoted when originally parsed
3658 */
3659static char *make_string(char **inp, int *nonnull)
wdenkfe8c2802002-11-03 00:38:21 +00003660{
3661 char *p;
3662 char *str = NULL;
3663 int n;
3664 int len = 2;
Joe Hershbergera005f192012-08-17 10:26:30 +00003665 char *noeval_str;
3666 int noeval = 0;
wdenkfe8c2802002-11-03 00:38:21 +00003667
Joe Hershbergera005f192012-08-17 10:26:30 +00003668 noeval_str = get_local_var("HUSH_NO_EVAL");
3669 if (noeval_str != NULL && *noeval_str != '0' && *noeval_str != '\0')
3670 noeval = 1;
wdenkfe8c2802002-11-03 00:38:21 +00003671 for (n = 0; inp[n]; n++) {
Joe Hershbergera005f192012-08-17 10:26:30 +00003672 p = insert_var_value_sub(inp[n], noeval);
Stephen Warrenfe9ca3d2014-03-01 22:16:10 -07003673 str = xrealloc(str, (len + strlen(p) + (2 * nonnull[n])));
wdenkfe8c2802002-11-03 00:38:21 +00003674 if (n) {
3675 strcat(str, " ");
3676 } else {
3677 *str = '\0';
3678 }
Stephen Warrenfe9ca3d2014-03-01 22:16:10 -07003679 if (nonnull[n])
3680 strcat(str, "'");
wdenkfe8c2802002-11-03 00:38:21 +00003681 strcat(str, p);
Stephen Warrenfe9ca3d2014-03-01 22:16:10 -07003682 if (nonnull[n])
3683 strcat(str, "'");
wdenkfe8c2802002-11-03 00:38:21 +00003684 len = strlen(str) + 3;
3685 if (p != inp[n]) free(p);
3686 }
3687 len = strlen(str);
3688 *(str + len) = '\n';
3689 *(str + len + 1) = '\0';
3690 return str;
3691}
3692
Heiko Schocher81473f62008-10-15 09:40:28 +02003693#ifdef __U_BOOT__
Simon Glass09140112020-05-10 11:40:03 -06003694static int do_showvar(struct cmd_tbl *cmdtp, int flag, int argc,
3695 char *const argv[])
Heiko Schocher81473f62008-10-15 09:40:28 +02003696{
3697 int i, k;
3698 int rcode = 0;
3699 struct variables *cur;
3700
3701 if (argc == 1) { /* Print all env variables */
3702 for (cur = top_vars; cur; cur = cur->next) {
3703 printf ("%s=%s\n", cur->name, cur->value);
3704 if (ctrlc ()) {
3705 puts ("\n ** Abort\n");
3706 return 1;
3707 }
3708 }
3709 return 0;
3710 }
3711 for (i = 1; i < argc; ++i) { /* print single env variables */
3712 char *name = argv[i];
3713
3714 k = -1;
3715 for (cur = top_vars; cur; cur = cur->next) {
3716 if(strcmp (cur->name, name) == 0) {
3717 k = 0;
3718 printf ("%s=%s\n", cur->name, cur->value);
3719 }
3720 if (ctrlc ()) {
3721 puts ("\n ** Abort\n");
3722 return 1;
3723 }
3724 }
3725 if (k < 0) {
3726 printf ("## Error: \"%s\" not defined\n", name);
3727 rcode ++;
3728 }
3729 }
3730 return rcode;
3731}
3732
3733U_BOOT_CMD(
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02003734 showvar, CONFIG_SYS_MAXARGS, 1, do_showvar,
Peter Tyser2fb26042009-01-27 18:03:12 -06003735 "print local hushshell variables",
Heiko Schocher81473f62008-10-15 09:40:28 +02003736 "\n - print values of all hushshell variables\n"
3737 "showvar name ...\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02003738 " - print value of hushshell variable 'name'"
Heiko Schocher81473f62008-10-15 09:40:28 +02003739);
3740
3741#endif
wdenkfe8c2802002-11-03 00:38:21 +00003742/****************************************************************************/