1 /* vi: set sw=4 ts=4: */
3 * ash shell port for busybox
5 * Copyright (c) 1989, 1991, 1993, 1994
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * This version of ash is adapted from the source in Debian's ash 0.3.8-5
28 * Modified by Erik Andersen <andersee@debian.org> and
29 * Vladimir Oleynik <dzo@simtreas.ru> to be used in busybox
32 * Original copyright notice is retained at the end of this file.
36 /* Enable this to compile in extra debugging noise. When debugging is
37 * on, debugging info will be written to $HOME/trace and a quit signal
38 * will generate a core dump. */
41 /* These are here to work with glibc -- Don't change these... */
63 #include <sys/cdefs.h>
64 #include <sys/ioctl.h>
65 #include <sys/param.h>
66 #include <sys/resource.h>
68 #include <sys/times.h>
69 #include <sys/types.h>
75 #if !defined(FNMATCH_BROKEN)
78 #if !defined(GLOB_BROKEN)
82 #ifdef CONFIG_ASH_JOB_CONTROL
88 #if defined(__uClinux__)
89 #error "Do not even bother, ash will not run on uClinux"
93 * This file was generated by the mksyntax program.
97 #define CWORD 0 /* character is nothing special */
98 #define CNL 1 /* newline character */
99 #define CBACK 2 /* a backslash character */
100 #define CSQUOTE 3 /* single quote */
101 #define CDQUOTE 4 /* double quote */
102 #define CENDQUOTE 5 /* a terminating quote */
103 #define CBQUOTE 6 /* backwards single quote */
104 #define CVAR 7 /* a dollar sign */
105 #define CENDVAR 8 /* a '}' character */
106 #define CLP 9 /* a left paren in arithmetic */
107 #define CRP 10 /* a right paren in arithmetic */
108 #define CENDFILE 11 /* end of file */
109 #define CCTL 12 /* like CWORD, except it must be escaped */
110 #define CSPCL 13 /* these terminate a word */
111 #define CIGN 14 /* character should be ignored */
131 #define TENDBQUOTE 13
151 /* control characters in argument strings */
152 #define CTLESC '\201'
153 #define CTLVAR '\202'
154 #define CTLENDVAR '\203'
155 #define CTLBACKQ '\204'
156 #define CTLQUOTE 01 /* ored with CTLBACKQ code if in quotes */
157 /* CTLBACKQ | CTLQUOTE == '\205' */
158 #define CTLARI '\206'
159 #define CTLENDARI '\207'
160 #define CTLQUOTEMARK '\210'
163 #define is_digit(c) ((c)>='0' && (c)<='9')
164 #define is_name(c) (((c) < CTLESC || (c) > CTLENDARI) && ((c) == '_' || isalpha((unsigned char) (c))))
165 #define is_in_name(c) (((c) < CTLESC || (c) > CTLENDARI) && ((c) == '_' || isalnum((unsigned char) (c))))
168 * is_special(c) evaluates to 1 for c in "!#$*-0123456789?@"; 0 otherwise
169 * (assuming ascii char codes, as the original implementation did)
171 #define is_special(c) \
172 ( (((unsigned int)c) - 33 < 32) \
173 && ((0xc1ff920dUL >> (((unsigned int)c) - 33)) & 1))
175 #define digit_val(c) ((c) - '0')
178 #define S_DFL 1 /* default signal handling (SIG_DFL) */
179 #define S_CATCH 2 /* signal is caught */
180 #define S_IGN 3 /* signal is ignored (SIG_IGN) */
181 #define S_HARD_IGN 4 /* signal is ignored permenantly */
182 #define S_RESET 5 /* temporary - to reset a hard ignored sig */
185 /* variable substitution byte (follows CTLVAR) */
186 #define VSTYPE 0x0f /* type of variable substitution */
187 #define VSNUL 0x10 /* colon--treat the empty string as unset */
188 #define VSQUOTE 0x80 /* inside double quotes--suppress splitting */
190 /* values of VSTYPE field */
191 #define VSNORMAL 0x1 /* normal variable: $var or ${var} */
192 #define VSMINUS 0x2 /* ${var-text} */
193 #define VSPLUS 0x3 /* ${var+text} */
194 #define VSQUESTION 0x4 /* ${var?message} */
195 #define VSASSIGN 0x5 /* ${var=text} */
196 #define VSTRIMLEFT 0x6 /* ${var#pattern} */
197 #define VSTRIMLEFTMAX 0x7 /* ${var##pattern} */
198 #define VSTRIMRIGHT 0x8 /* ${var%pattern} */
199 #define VSTRIMRIGHTMAX 0x9 /* ${var%%pattern} */
200 #define VSLENGTH 0xa /* ${#var} */
202 /* flags passed to redirect */
203 #define REDIR_PUSH 01 /* save previous values of file descriptors */
204 #define REDIR_BACKQ 02 /* save the command output to pipe */
207 * BSD setjmp saves the signal mask, which violates ANSI C and takes time,
208 * so we use _setjmp instead.
212 #define setjmp(jmploc) _setjmp(jmploc)
213 #define longjmp(jmploc, val) _longjmp(jmploc, val)
217 * Most machines require the value returned from malloc to be aligned
218 * in some way. The following macro will get this right on many machines.
227 #define ALIGN(nbytes) (((nbytes) + sizeof(union align) - 1) & ~(sizeof(union align) - 1))
230 #ifdef CONFIG_LOCALE_SUPPORT
232 static void change_lc_all(const char *value);
233 static void change_lc_ctype(const char *value);
237 * These macros allow the user to suspend the handling of interrupt signals
238 * over a period of time. This is similar to SIGHOLD to or sigblock, but
239 * much more efficient and portable. (But hacking the kernel is so much
240 * more fun than worrying about efficiency and portability. :-))
243 static void onint(void);
244 static volatile int suppressint;
245 static volatile int intpending;
247 #define INTOFF suppressint++
248 #ifndef CONFIG_ASH_OPTIMIZE_FOR_SIZE
249 #define INTON { if (--suppressint == 0 && intpending) onint(); }
250 #define FORCEINTON {suppressint = 0; if (intpending) onint();}
252 static void __inton(void);
253 static void forceinton(void);
255 #define INTON __inton()
256 #define FORCEINTON forceinton()
259 #define CLEAR_PENDING_INT intpending = 0
260 #define int_pending() intpending
263 typedef void *pointer;
266 #define NULL (void *)0
269 static pointer stalloc(int);
270 static void stunalloc(pointer);
271 static void ungrabstackstr(char *, char *);
272 static char *growstackstr(void);
273 static char *makestrspace(size_t newlen);
276 * Parse trees for commands are allocated in lifo order, so we use a stack
277 * to make this more efficient, and also to avoid all sorts of exception
278 * handling code to handle interrupts in the middle of a parse.
280 * The size 504 was chosen because the Ultrix malloc handles that size
284 #define MINSIZE 504 /* minimum size of a block */
288 struct stack_block *prev;
292 static struct stack_block stackbase;
293 static struct stack_block *stackp = &stackbase;
294 static struct stackmark *markp;
295 static char *stacknxt = stackbase.space;
296 static int stacknleft = MINSIZE;
299 #define equal(s1, s2) (strcmp(s1, s2) == 0)
301 #define stackblock() stacknxt
302 #define stackblocksize() stacknleft
303 #define STARTSTACKSTR(p) p = stackblock(), sstrnleft = stackblocksize()
305 #define STPUTC(c, p) (--sstrnleft >= 0? (*p++ = (c)) : (p = growstackstr(), *p++ = (c)))
306 #define CHECKSTRSPACE(n, p) { if (sstrnleft < n) p = makestrspace(n); }
307 #define STACKSTRNUL(p) (sstrnleft == 0? (p = growstackstr(), *p = '\0') : (*p = '\0'))
310 #define USTPUTC(c, p) (--sstrnleft, *p++ = (c))
311 #define STUNPUTC(p) (++sstrnleft, --p)
312 #define STTOPC(p) p[-1]
313 #define STADJUST(amount, p) (p += (amount), sstrnleft -= (amount))
314 #define grabstackstr(p) stalloc(stackblocksize() - sstrnleft)
318 #define TRACE(param) trace param
319 typedef union node unode;
320 static void trace(const char *, ...);
321 static void trargs(char **);
322 static void showtree(unode *);
323 static void trputc(int);
324 static void trputs(const char *);
325 static void opentrace(void);
360 #define EXP_FULL 0x1 /* perform word splitting & file globbing */
361 #define EXP_TILDE 0x2 /* do normal tilde expansion */
362 #define EXP_VARTILDE 0x4 /* expand tildes in an assignment */
363 #define EXP_REDIR 0x8 /* file glob for a redirection (1 match only) */
364 #define EXP_CASE 0x10 /* keeps quotes around for CASE pattern */
365 #define EXP_RECORD 0x20 /* need to record arguments for ifs breakup */
370 static char optet_vals[NOPTS];
372 static const char *const optlist[NOPTS] = {
391 #define optent_name(optent) (optent+1)
392 #define optent_letter(optent) optent[0]
393 #define optent_val(optent) optet_vals[optent]
395 #define eflag optent_val(0)
396 #define fflag optent_val(1)
397 #define Iflag optent_val(2)
398 #define iflag optent_val(3)
399 #define mflag optent_val(4)
400 #define nflag optent_val(5)
401 #define sflag optent_val(6)
402 #define xflag optent_val(7)
403 #define vflag optent_val(8)
404 #define Vflag optent_val(9)
405 #define Eflag optent_val(10)
406 #define Cflag optent_val(11)
407 #define aflag optent_val(12)
408 #define bflag optent_val(13)
409 #define uflag optent_val(14)
410 #define qflag optent_val(15)
413 /* Mode argument to forkshell. Don't change FORK_FG or FORK_BG. */
431 union node *redirect;
438 struct nodelist *cmdlist;
445 union node *redirect;
453 union node *elsepart;
484 struct nodelist *backquote;
522 struct nbinary nbinary;
525 struct nredir nredir;
529 struct nclist nclist;
539 struct nodelist *next;
543 struct backcmd { /* result of evalbackcmd */
544 int fd; /* file descriptor to read from */
545 char *buf; /* buffer */
546 int nleft; /* number of chars in buffer */
547 struct job *jp; /* job structure for command */
555 const struct builtincmd *cmd;
560 struct strlist *next;
566 struct strlist *list;
567 struct strlist **lastp;
571 struct strpush *prev; /* preceding string on stack */
574 #ifdef CONFIG_ASH_ALIAS
575 struct alias *ap; /* if push was associated with an alias */
577 char *string; /* remember the string since it may change */
581 struct parsefile *prev; /* preceding file on stack */
582 int linno; /* current line */
583 int fd; /* file descriptor (or -1 if string) */
584 int nleft; /* number of chars left in this line */
585 int lleft; /* number of chars left in this buffer */
586 char *nextc; /* next char in buffer */
587 char *buf; /* input buffer */
588 struct strpush *strpush; /* for pushing strings at this level */
589 struct strpush basestrpush; /* so pushing one is fast */
593 struct stack_block *stackp;
596 struct stackmark *marknext;
600 int nparam; /* # of positional parameters (without $0) */
601 unsigned char malloc; /* if parameter list dynamically allocated */
602 char **p; /* parameter list */
603 int optind; /* next parameter to be processed by getopts */
604 int optoff; /* used by getopts */
608 * When commands are first encountered, they are entered in a hash table.
609 * This ensures that a full path search will not have to be done for them
610 * on each invocation.
612 * We should investigate converting to a linear search, even though that
613 * would make the command name "hash" a misnomer.
615 #define CMDTABLESIZE 31 /* should be prime */
616 #define ARB 1 /* actual size determined at run time */
621 struct tblentry *next; /* next entry in hash chain */
622 union param param; /* definition of builtin function */
623 short cmdtype; /* index identifying command */
624 char rehash; /* if set, cd done since entry created */
625 char cmdname[ARB]; /* name of command */
629 static struct tblentry *cmdtable[CMDTABLESIZE];
630 static int builtinloc = -1; /* index in path of %builtin, or -1 */
631 static int exerrno = 0; /* Last exec error */
634 static void tryexec(char *, char **, char **);
635 static void printentry(struct tblentry *, int);
636 static void clearcmdentry(int);
637 static struct tblentry *cmdlookup(const char *, int);
638 static void delete_cmd_entry(void);
639 static int path_change(const char *, int *);
642 static void flushall(void);
643 static void out2fmt(const char *, ...)
644 __attribute__ ((__format__(__printf__, 1, 2)));
645 static int xwrite(int, const char *, int);
647 static inline void outstr(const char *p, FILE * file)
651 static void out1str(const char *p)
655 static void out2str(const char *p)
660 #ifndef CONFIG_ASH_OPTIMIZE_FOR_SIZE
661 #define out2c(c) putc((c), stderr)
663 static void out2c(int c)
670 #ifdef CONFIG_ASH_OPTIMIZE_FOR_SIZE
671 #define USE_SIT_FUNCTION
674 /* number syntax index */
675 #define BASESYNTAX 0 /* not in quotes */
676 #define DQSYNTAX 1 /* in double quotes */
677 #define SQSYNTAX 2 /* in single quotes */
678 #define ARISYNTAX 3 /* in arithmetic */
680 static const char S_I_T[][4] = {
681 {CSPCL, CIGN, CIGN, CIGN}, /* 0, PEOA */
682 {CSPCL, CWORD, CWORD, CWORD}, /* 1, ' ' */
683 {CNL, CNL, CNL, CNL}, /* 2, \n */
684 {CWORD, CCTL, CCTL, CWORD}, /* 3, !*-/:=?[]~ */
685 {CDQUOTE, CENDQUOTE, CWORD, CDQUOTE}, /* 4, '"' */
686 {CVAR, CVAR, CWORD, CVAR}, /* 5, $ */
687 {CSQUOTE, CWORD, CENDQUOTE, CSQUOTE}, /* 6, "'" */
688 {CSPCL, CWORD, CWORD, CLP}, /* 7, ( */
689 {CSPCL, CWORD, CWORD, CRP}, /* 8, ) */
690 {CBACK, CBACK, CCTL, CBACK}, /* 9, \ */
691 {CBQUOTE, CBQUOTE, CWORD, CBQUOTE}, /* 10, ` */
692 {CENDVAR, CENDVAR, CWORD, CENDVAR}, /* 11, } */
693 #ifndef USE_SIT_FUNCTION
694 {CENDFILE, CENDFILE, CENDFILE, CENDFILE}, /* 12, PEOF */
695 {CWORD, CWORD, CWORD, CWORD}, /* 13, 0-9A-Za-z */
696 {CCTL, CCTL, CCTL, CCTL} /* 14, CTLESC ... */
700 #ifdef USE_SIT_FUNCTION
702 #define U_C(c) ((unsigned char)(c))
704 static int SIT(int c, int syntax)
706 static const char spec_symbls[] = "\t\n !\"$&'()*-/:;<=>?[\\]`|}~";
707 static const char syntax_index_table[] = {
708 1, 2, 1, 3, 4, 5, 1, 6, /* "\t\n !\"$&'" */
709 7, 8, 3, 3, 3, 3, 1, 1, /* "()*-/:;<" */
710 3, 1, 3, 3, 9, 3, 10, 1, /* "=>?[\\]`|" */
716 if (c == PEOF) /* 2^8+2 */
718 if (c == PEOA) /* 2^8+1 */
720 else if (U_C(c) >= U_C(CTLESC) && U_C(c) <= U_C(CTLQUOTEMARK))
723 s = strchr(spec_symbls, c);
726 indx = syntax_index_table[(s - spec_symbls)];
728 return S_I_T[indx][syntax];
731 #else /* USE_SIT_FUNCTION */
733 #define SIT(c, syntax) S_I_T[(int)syntax_index_table[((int)c)+SYNBASE]][syntax]
735 #define CSPCL_CIGN_CIGN_CIGN 0
736 #define CSPCL_CWORD_CWORD_CWORD 1
737 #define CNL_CNL_CNL_CNL 2
738 #define CWORD_CCTL_CCTL_CWORD 3
739 #define CDQUOTE_CENDQUOTE_CWORD_CDQUOTE 4
740 #define CVAR_CVAR_CWORD_CVAR 5
741 #define CSQUOTE_CWORD_CENDQUOTE_CSQUOTE 6
742 #define CSPCL_CWORD_CWORD_CLP 7
743 #define CSPCL_CWORD_CWORD_CRP 8
744 #define CBACK_CBACK_CCTL_CBACK 9
745 #define CBQUOTE_CBQUOTE_CWORD_CBQUOTE 10
746 #define CENDVAR_CENDVAR_CWORD_CENDVAR 11
747 #define CENDFILE_CENDFILE_CENDFILE_CENDFILE 12
748 #define CWORD_CWORD_CWORD_CWORD 13
749 #define CCTL_CCTL_CCTL_CCTL 14
751 static const char syntax_index_table[258] = {
752 /* BASESYNTAX_DQSYNTAX_SQSYNTAX_ARISYNTAX */
753 /* 0 -130 PEOF */ CENDFILE_CENDFILE_CENDFILE_CENDFILE,
754 /* 1 -129 PEOA */ CSPCL_CIGN_CIGN_CIGN,
755 /* 2 -128 0xff */ CWORD_CWORD_CWORD_CWORD,
756 /* 3 -127 */ CCTL_CCTL_CCTL_CCTL,
758 /* 4 -126 */ CCTL_CCTL_CCTL_CCTL,
759 /* 5 -125 */ CCTL_CCTL_CCTL_CCTL,
760 /* 6 -124 */ CCTL_CCTL_CCTL_CCTL,
761 /* 7 -123 */ CCTL_CCTL_CCTL_CCTL,
762 /* 8 -122 */ CCTL_CCTL_CCTL_CCTL,
763 /* 9 -121 */ CCTL_CCTL_CCTL_CCTL,
764 /* 10 -120 */ CCTL_CCTL_CCTL_CCTL,
766 /* 11 -119 */ CWORD_CWORD_CWORD_CWORD,
767 /* 12 -118 */ CWORD_CWORD_CWORD_CWORD,
768 /* 13 -117 */ CWORD_CWORD_CWORD_CWORD,
769 /* 14 -116 */ CWORD_CWORD_CWORD_CWORD,
770 /* 15 -115 */ CWORD_CWORD_CWORD_CWORD,
771 /* 16 -114 */ CWORD_CWORD_CWORD_CWORD,
772 /* 17 -113 */ CWORD_CWORD_CWORD_CWORD,
773 /* 18 -112 */ CWORD_CWORD_CWORD_CWORD,
774 /* 19 -111 */ CWORD_CWORD_CWORD_CWORD,
775 /* 20 -110 */ CWORD_CWORD_CWORD_CWORD,
776 /* 21 -109 */ CWORD_CWORD_CWORD_CWORD,
777 /* 22 -108 */ CWORD_CWORD_CWORD_CWORD,
778 /* 23 -107 */ CWORD_CWORD_CWORD_CWORD,
779 /* 24 -106 */ CWORD_CWORD_CWORD_CWORD,
780 /* 25 -105 */ CWORD_CWORD_CWORD_CWORD,
781 /* 26 -104 */ CWORD_CWORD_CWORD_CWORD,
782 /* 27 -103 */ CWORD_CWORD_CWORD_CWORD,
783 /* 28 -102 */ CWORD_CWORD_CWORD_CWORD,
784 /* 29 -101 */ CWORD_CWORD_CWORD_CWORD,
785 /* 30 -100 */ CWORD_CWORD_CWORD_CWORD,
786 /* 31 -99 */ CWORD_CWORD_CWORD_CWORD,
787 /* 32 -98 */ CWORD_CWORD_CWORD_CWORD,
788 /* 33 -97 */ CWORD_CWORD_CWORD_CWORD,
789 /* 34 -96 */ CWORD_CWORD_CWORD_CWORD,
790 /* 35 -95 */ CWORD_CWORD_CWORD_CWORD,
791 /* 36 -94 */ CWORD_CWORD_CWORD_CWORD,
792 /* 37 -93 */ CWORD_CWORD_CWORD_CWORD,
793 /* 38 -92 */ CWORD_CWORD_CWORD_CWORD,
794 /* 39 -91 */ CWORD_CWORD_CWORD_CWORD,
795 /* 40 -90 */ CWORD_CWORD_CWORD_CWORD,
796 /* 41 -89 */ CWORD_CWORD_CWORD_CWORD,
797 /* 42 -88 */ CWORD_CWORD_CWORD_CWORD,
798 /* 43 -87 */ CWORD_CWORD_CWORD_CWORD,
799 /* 44 -86 */ CWORD_CWORD_CWORD_CWORD,
800 /* 45 -85 */ CWORD_CWORD_CWORD_CWORD,
801 /* 46 -84 */ CWORD_CWORD_CWORD_CWORD,
802 /* 47 -83 */ CWORD_CWORD_CWORD_CWORD,
803 /* 48 -82 */ CWORD_CWORD_CWORD_CWORD,
804 /* 49 -81 */ CWORD_CWORD_CWORD_CWORD,
805 /* 50 -80 */ CWORD_CWORD_CWORD_CWORD,
806 /* 51 -79 */ CWORD_CWORD_CWORD_CWORD,
807 /* 52 -78 */ CWORD_CWORD_CWORD_CWORD,
808 /* 53 -77 */ CWORD_CWORD_CWORD_CWORD,
809 /* 54 -76 */ CWORD_CWORD_CWORD_CWORD,
810 /* 55 -75 */ CWORD_CWORD_CWORD_CWORD,
811 /* 56 -74 */ CWORD_CWORD_CWORD_CWORD,
812 /* 57 -73 */ CWORD_CWORD_CWORD_CWORD,
813 /* 58 -72 */ CWORD_CWORD_CWORD_CWORD,
814 /* 59 -71 */ CWORD_CWORD_CWORD_CWORD,
815 /* 60 -70 */ CWORD_CWORD_CWORD_CWORD,
816 /* 61 -69 */ CWORD_CWORD_CWORD_CWORD,
817 /* 62 -68 */ CWORD_CWORD_CWORD_CWORD,
818 /* 63 -67 */ CWORD_CWORD_CWORD_CWORD,
819 /* 64 -66 */ CWORD_CWORD_CWORD_CWORD,
820 /* 65 -65 */ CWORD_CWORD_CWORD_CWORD,
821 /* 66 -64 */ CWORD_CWORD_CWORD_CWORD,
822 /* 67 -63 */ CWORD_CWORD_CWORD_CWORD,
823 /* 68 -62 */ CWORD_CWORD_CWORD_CWORD,
824 /* 69 -61 */ CWORD_CWORD_CWORD_CWORD,
825 /* 70 -60 */ CWORD_CWORD_CWORD_CWORD,
826 /* 71 -59 */ CWORD_CWORD_CWORD_CWORD,
827 /* 72 -58 */ CWORD_CWORD_CWORD_CWORD,
828 /* 73 -57 */ CWORD_CWORD_CWORD_CWORD,
829 /* 74 -56 */ CWORD_CWORD_CWORD_CWORD,
830 /* 75 -55 */ CWORD_CWORD_CWORD_CWORD,
831 /* 76 -54 */ CWORD_CWORD_CWORD_CWORD,
832 /* 77 -53 */ CWORD_CWORD_CWORD_CWORD,
833 /* 78 -52 */ CWORD_CWORD_CWORD_CWORD,
834 /* 79 -51 */ CWORD_CWORD_CWORD_CWORD,
835 /* 80 -50 */ CWORD_CWORD_CWORD_CWORD,
836 /* 81 -49 */ CWORD_CWORD_CWORD_CWORD,
837 /* 82 -48 */ CWORD_CWORD_CWORD_CWORD,
838 /* 83 -47 */ CWORD_CWORD_CWORD_CWORD,
839 /* 84 -46 */ CWORD_CWORD_CWORD_CWORD,
840 /* 85 -45 */ CWORD_CWORD_CWORD_CWORD,
841 /* 86 -44 */ CWORD_CWORD_CWORD_CWORD,
842 /* 87 -43 */ CWORD_CWORD_CWORD_CWORD,
843 /* 88 -42 */ CWORD_CWORD_CWORD_CWORD,
844 /* 89 -41 */ CWORD_CWORD_CWORD_CWORD,
845 /* 90 -40 */ CWORD_CWORD_CWORD_CWORD,
846 /* 91 -39 */ CWORD_CWORD_CWORD_CWORD,
847 /* 92 -38 */ CWORD_CWORD_CWORD_CWORD,
848 /* 93 -37 */ CWORD_CWORD_CWORD_CWORD,
849 /* 94 -36 */ CWORD_CWORD_CWORD_CWORD,
850 /* 95 -35 */ CWORD_CWORD_CWORD_CWORD,
851 /* 96 -34 */ CWORD_CWORD_CWORD_CWORD,
852 /* 97 -33 */ CWORD_CWORD_CWORD_CWORD,
853 /* 98 -32 */ CWORD_CWORD_CWORD_CWORD,
854 /* 99 -31 */ CWORD_CWORD_CWORD_CWORD,
855 /* 100 -30 */ CWORD_CWORD_CWORD_CWORD,
856 /* 101 -29 */ CWORD_CWORD_CWORD_CWORD,
857 /* 102 -28 */ CWORD_CWORD_CWORD_CWORD,
858 /* 103 -27 */ CWORD_CWORD_CWORD_CWORD,
859 /* 104 -26 */ CWORD_CWORD_CWORD_CWORD,
860 /* 105 -25 */ CWORD_CWORD_CWORD_CWORD,
861 /* 106 -24 */ CWORD_CWORD_CWORD_CWORD,
862 /* 107 -23 */ CWORD_CWORD_CWORD_CWORD,
863 /* 108 -22 */ CWORD_CWORD_CWORD_CWORD,
864 /* 109 -21 */ CWORD_CWORD_CWORD_CWORD,
865 /* 110 -20 */ CWORD_CWORD_CWORD_CWORD,
866 /* 111 -19 */ CWORD_CWORD_CWORD_CWORD,
867 /* 112 -18 */ CWORD_CWORD_CWORD_CWORD,
868 /* 113 -17 */ CWORD_CWORD_CWORD_CWORD,
869 /* 114 -16 */ CWORD_CWORD_CWORD_CWORD,
870 /* 115 -15 */ CWORD_CWORD_CWORD_CWORD,
871 /* 116 -14 */ CWORD_CWORD_CWORD_CWORD,
872 /* 117 -13 */ CWORD_CWORD_CWORD_CWORD,
873 /* 118 -12 */ CWORD_CWORD_CWORD_CWORD,
874 /* 119 -11 */ CWORD_CWORD_CWORD_CWORD,
875 /* 120 -10 */ CWORD_CWORD_CWORD_CWORD,
876 /* 121 -9 */ CWORD_CWORD_CWORD_CWORD,
877 /* 122 -8 */ CWORD_CWORD_CWORD_CWORD,
878 /* 123 -7 */ CWORD_CWORD_CWORD_CWORD,
879 /* 124 -6 */ CWORD_CWORD_CWORD_CWORD,
880 /* 125 -5 */ CWORD_CWORD_CWORD_CWORD,
881 /* 126 -4 */ CWORD_CWORD_CWORD_CWORD,
882 /* 127 -3 */ CWORD_CWORD_CWORD_CWORD,
883 /* 128 -2 */ CWORD_CWORD_CWORD_CWORD,
884 /* 129 -1 */ CWORD_CWORD_CWORD_CWORD,
885 /* 130 0 */ CWORD_CWORD_CWORD_CWORD,
886 /* 131 1 */ CWORD_CWORD_CWORD_CWORD,
887 /* 132 2 */ CWORD_CWORD_CWORD_CWORD,
888 /* 133 3 */ CWORD_CWORD_CWORD_CWORD,
889 /* 134 4 */ CWORD_CWORD_CWORD_CWORD,
890 /* 135 5 */ CWORD_CWORD_CWORD_CWORD,
891 /* 136 6 */ CWORD_CWORD_CWORD_CWORD,
892 /* 137 7 */ CWORD_CWORD_CWORD_CWORD,
893 /* 138 8 */ CWORD_CWORD_CWORD_CWORD,
894 /* 139 9 "\t" */ CSPCL_CWORD_CWORD_CWORD,
895 /* 140 10 "\n" */ CNL_CNL_CNL_CNL,
896 /* 141 11 */ CWORD_CWORD_CWORD_CWORD,
897 /* 142 12 */ CWORD_CWORD_CWORD_CWORD,
898 /* 143 13 */ CWORD_CWORD_CWORD_CWORD,
899 /* 144 14 */ CWORD_CWORD_CWORD_CWORD,
900 /* 145 15 */ CWORD_CWORD_CWORD_CWORD,
901 /* 146 16 */ CWORD_CWORD_CWORD_CWORD,
902 /* 147 17 */ CWORD_CWORD_CWORD_CWORD,
903 /* 148 18 */ CWORD_CWORD_CWORD_CWORD,
904 /* 149 19 */ CWORD_CWORD_CWORD_CWORD,
905 /* 150 20 */ CWORD_CWORD_CWORD_CWORD,
906 /* 151 21 */ CWORD_CWORD_CWORD_CWORD,
907 /* 152 22 */ CWORD_CWORD_CWORD_CWORD,
908 /* 153 23 */ CWORD_CWORD_CWORD_CWORD,
909 /* 154 24 */ CWORD_CWORD_CWORD_CWORD,
910 /* 155 25 */ CWORD_CWORD_CWORD_CWORD,
911 /* 156 26 */ CWORD_CWORD_CWORD_CWORD,
912 /* 157 27 */ CWORD_CWORD_CWORD_CWORD,
913 /* 158 28 */ CWORD_CWORD_CWORD_CWORD,
914 /* 159 29 */ CWORD_CWORD_CWORD_CWORD,
915 /* 160 30 */ CWORD_CWORD_CWORD_CWORD,
916 /* 161 31 */ CWORD_CWORD_CWORD_CWORD,
917 /* 162 32 " " */ CSPCL_CWORD_CWORD_CWORD,
918 /* 163 33 "!" */ CWORD_CCTL_CCTL_CWORD,
919 /* 164 34 """ */ CDQUOTE_CENDQUOTE_CWORD_CDQUOTE,
920 /* 165 35 "#" */ CWORD_CWORD_CWORD_CWORD,
921 /* 166 36 "$" */ CVAR_CVAR_CWORD_CVAR,
922 /* 167 37 "%" */ CWORD_CWORD_CWORD_CWORD,
923 /* 168 38 "&" */ CSPCL_CWORD_CWORD_CWORD,
924 /* 169 39 "'" */ CSQUOTE_CWORD_CENDQUOTE_CSQUOTE,
925 /* 170 40 "(" */ CSPCL_CWORD_CWORD_CLP,
926 /* 171 41 ")" */ CSPCL_CWORD_CWORD_CRP,
927 /* 172 42 "*" */ CWORD_CCTL_CCTL_CWORD,
928 /* 173 43 "+" */ CWORD_CWORD_CWORD_CWORD,
929 /* 174 44 "," */ CWORD_CWORD_CWORD_CWORD,
930 /* 175 45 "-" */ CWORD_CCTL_CCTL_CWORD,
931 /* 176 46 "." */ CWORD_CWORD_CWORD_CWORD,
932 /* 177 47 "/" */ CWORD_CCTL_CCTL_CWORD,
933 /* 178 48 "0" */ CWORD_CWORD_CWORD_CWORD,
934 /* 179 49 "1" */ CWORD_CWORD_CWORD_CWORD,
935 /* 180 50 "2" */ CWORD_CWORD_CWORD_CWORD,
936 /* 181 51 "3" */ CWORD_CWORD_CWORD_CWORD,
937 /* 182 52 "4" */ CWORD_CWORD_CWORD_CWORD,
938 /* 183 53 "5" */ CWORD_CWORD_CWORD_CWORD,
939 /* 184 54 "6" */ CWORD_CWORD_CWORD_CWORD,
940 /* 185 55 "7" */ CWORD_CWORD_CWORD_CWORD,
941 /* 186 56 "8" */ CWORD_CWORD_CWORD_CWORD,
942 /* 187 57 "9" */ CWORD_CWORD_CWORD_CWORD,
943 /* 188 58 ":" */ CWORD_CCTL_CCTL_CWORD,
944 /* 189 59 ";" */ CSPCL_CWORD_CWORD_CWORD,
945 /* 190 60 "<" */ CSPCL_CWORD_CWORD_CWORD,
946 /* 191 61 "=" */ CWORD_CCTL_CCTL_CWORD,
947 /* 192 62 ">" */ CSPCL_CWORD_CWORD_CWORD,
948 /* 193 63 "?" */ CWORD_CCTL_CCTL_CWORD,
949 /* 194 64 "@" */ CWORD_CWORD_CWORD_CWORD,
950 /* 195 65 "A" */ CWORD_CWORD_CWORD_CWORD,
951 /* 196 66 "B" */ CWORD_CWORD_CWORD_CWORD,
952 /* 197 67 "C" */ CWORD_CWORD_CWORD_CWORD,
953 /* 198 68 "D" */ CWORD_CWORD_CWORD_CWORD,
954 /* 199 69 "E" */ CWORD_CWORD_CWORD_CWORD,
955 /* 200 70 "F" */ CWORD_CWORD_CWORD_CWORD,
956 /* 201 71 "G" */ CWORD_CWORD_CWORD_CWORD,
957 /* 202 72 "H" */ CWORD_CWORD_CWORD_CWORD,
958 /* 203 73 "I" */ CWORD_CWORD_CWORD_CWORD,
959 /* 204 74 "J" */ CWORD_CWORD_CWORD_CWORD,
960 /* 205 75 "K" */ CWORD_CWORD_CWORD_CWORD,
961 /* 206 76 "L" */ CWORD_CWORD_CWORD_CWORD,
962 /* 207 77 "M" */ CWORD_CWORD_CWORD_CWORD,
963 /* 208 78 "N" */ CWORD_CWORD_CWORD_CWORD,
964 /* 209 79 "O" */ CWORD_CWORD_CWORD_CWORD,
965 /* 210 80 "P" */ CWORD_CWORD_CWORD_CWORD,
966 /* 211 81 "Q" */ CWORD_CWORD_CWORD_CWORD,
967 /* 212 82 "R" */ CWORD_CWORD_CWORD_CWORD,
968 /* 213 83 "S" */ CWORD_CWORD_CWORD_CWORD,
969 /* 214 84 "T" */ CWORD_CWORD_CWORD_CWORD,
970 /* 215 85 "U" */ CWORD_CWORD_CWORD_CWORD,
971 /* 216 86 "V" */ CWORD_CWORD_CWORD_CWORD,
972 /* 217 87 "W" */ CWORD_CWORD_CWORD_CWORD,
973 /* 218 88 "X" */ CWORD_CWORD_CWORD_CWORD,
974 /* 219 89 "Y" */ CWORD_CWORD_CWORD_CWORD,
975 /* 220 90 "Z" */ CWORD_CWORD_CWORD_CWORD,
976 /* 221 91 "[" */ CWORD_CCTL_CCTL_CWORD,
977 /* 222 92 "\" */ CBACK_CBACK_CCTL_CBACK,
978 /* 223 93 "]" */ CWORD_CCTL_CCTL_CWORD,
979 /* 224 94 "^" */ CWORD_CWORD_CWORD_CWORD,
980 /* 225 95 "_" */ CWORD_CWORD_CWORD_CWORD,
981 /* 226 96 "`" */ CBQUOTE_CBQUOTE_CWORD_CBQUOTE,
982 /* 227 97 "a" */ CWORD_CWORD_CWORD_CWORD,
983 /* 228 98 "b" */ CWORD_CWORD_CWORD_CWORD,
984 /* 229 99 "c" */ CWORD_CWORD_CWORD_CWORD,
985 /* 230 100 "d" */ CWORD_CWORD_CWORD_CWORD,
986 /* 231 101 "e" */ CWORD_CWORD_CWORD_CWORD,
987 /* 232 102 "f" */ CWORD_CWORD_CWORD_CWORD,
988 /* 233 103 "g" */ CWORD_CWORD_CWORD_CWORD,
989 /* 234 104 "h" */ CWORD_CWORD_CWORD_CWORD,
990 /* 235 105 "i" */ CWORD_CWORD_CWORD_CWORD,
991 /* 236 106 "j" */ CWORD_CWORD_CWORD_CWORD,
992 /* 237 107 "k" */ CWORD_CWORD_CWORD_CWORD,
993 /* 238 108 "l" */ CWORD_CWORD_CWORD_CWORD,
994 /* 239 109 "m" */ CWORD_CWORD_CWORD_CWORD,
995 /* 240 110 "n" */ CWORD_CWORD_CWORD_CWORD,
996 /* 241 111 "o" */ CWORD_CWORD_CWORD_CWORD,
997 /* 242 112 "p" */ CWORD_CWORD_CWORD_CWORD,
998 /* 243 113 "q" */ CWORD_CWORD_CWORD_CWORD,
999 /* 244 114 "r" */ CWORD_CWORD_CWORD_CWORD,
1000 /* 245 115 "s" */ CWORD_CWORD_CWORD_CWORD,
1001 /* 246 116 "t" */ CWORD_CWORD_CWORD_CWORD,
1002 /* 247 117 "u" */ CWORD_CWORD_CWORD_CWORD,
1003 /* 248 118 "v" */ CWORD_CWORD_CWORD_CWORD,
1004 /* 249 119 "w" */ CWORD_CWORD_CWORD_CWORD,
1005 /* 250 120 "x" */ CWORD_CWORD_CWORD_CWORD,
1006 /* 251 121 "y" */ CWORD_CWORD_CWORD_CWORD,
1007 /* 252 122 "z" */ CWORD_CWORD_CWORD_CWORD,
1008 /* 253 123 "{" */ CWORD_CWORD_CWORD_CWORD,
1009 /* 254 124 "|" */ CSPCL_CWORD_CWORD_CWORD,
1010 /* 255 125 "}" */ CENDVAR_CENDVAR_CWORD_CENDVAR,
1011 /* 256 126 "~" */ CWORD_CCTL_CCTL_CWORD,
1012 /* 257 127 */ CWORD_CWORD_CWORD_CWORD,
1015 #endif /* USE_SIT_FUNCTION */
1018 /* first char is indicating which tokens mark the end of a list */
1019 static const char *const tokname_array[] = {
1034 #define KWDOFFSET 14
1035 /* the following are keywords */
1054 static const char *tokname(int tok)
1056 static char buf[16];
1060 sprintf(buf + (tok >= TSEMI), "%s%c",
1061 tokname_array[tok] + 1, (tok >= TSEMI ? '"' : 0));
1065 static int plinno = 1; /* input line number */
1067 static int parselleft; /* copy of parsefile->lleft */
1069 static struct parsefile basepf; /* top level input file */
1070 static char basebuf[BUFSIZ]; /* buffer for top level input file */
1071 static struct parsefile *parsefile = &basepf; /* current input file */
1074 * NEOF is returned by parsecmd when it encounters an end of file. It
1075 * must be distinct from NULL, so we use the address of a variable that
1076 * happens to be handy.
1079 static int tokpushback; /* last token pushed back */
1081 #define NEOF ((union node *)&tokpushback)
1082 static int checkkwd; /* 1 == check for kwds, 2 == also eat newlines */
1085 static void error(const char *, ...) __attribute__ ((__noreturn__));
1086 static void exerror(int, const char *, ...) __attribute__ ((__noreturn__));
1087 static void shellexec(char **, char **, const char *, int)
1088 __attribute__ ((noreturn));
1089 static void exitshell(int) __attribute__ ((noreturn));
1091 static int goodname(const char *);
1092 static void ignoresig(int);
1093 static void onsig(int);
1094 static void dotrap(void);
1095 static int decode_signal(const char *, int);
1097 static void setparam(char **);
1098 static void freeparam(volatile struct shparam *);
1100 /* reasons for skipping commands (see comment on breakcmd routine) */
1106 /* values of cmdtype */
1107 #define CMDUNKNOWN -1 /* no entry in table for command */
1108 #define CMDNORMAL 0 /* command is an executable program */
1109 #define CMDBUILTIN 1 /* command is a shell builtin */
1110 #define CMDFUNCTION 2 /* command is a shell function */
1112 #define DO_ERR 1 /* find_command prints errors */
1113 #define DO_ABS 2 /* find_command checks absolute paths */
1114 #define DO_NOFUN 4 /* find_command ignores functions */
1115 #define DO_BRUTE 8 /* find_command ignores hash table */
1122 #define VEXPORT 0x01 /* variable is exported */
1123 #define VREADONLY 0x02 /* variable cannot be modified */
1124 #define VSTRFIXED 0x04 /* variable struct is staticly allocated */
1125 #define VTEXTFIXED 0x08 /* text is staticly allocated */
1126 #define VSTACK 0x10 /* text is allocated on the stack */
1127 #define VUNSET 0x20 /* the variable is not set */
1128 #define VNOFUNC 0x40 /* don't call the callback function */
1132 struct var *next; /* next entry in hash list */
1133 int flags; /* flags are defined above */
1134 char *text; /* name=value */
1135 void (*func) (const char *);
1136 /* function to be called when */
1137 /* the variable gets set/unset */
1141 struct localvar *next; /* next local variable in list */
1142 struct var *vp; /* the variable that was made local */
1143 int flags; /* saved flags */
1144 char *text; /* saved text */
1148 #if defined(__GLIBC__) && __GLIBC__ >= 2 && !defined(FNMATCH_BROKEN)
1149 #define rmescapes(p) _rmescapes((p), 0)
1150 static char *_rmescapes(char *, int);
1152 static void rmescapes(char *);
1155 static int casematch(union node *, const char *);
1156 static void clearredir(void);
1157 static void popstring(void);
1158 static void readcmdfile(const char *);
1160 static int number(const char *);
1161 static int is_number(const char *, int *num);
1162 static char *single_quote(const char *);
1163 static int nextopt(const char *);
1165 static void redirect(union node *, int);
1166 static void popredir(void);
1167 static int dup_as_newfd(int, int);
1169 static void changepath(const char *newval);
1170 static void getoptsreset(const char *value);
1173 static int parsenleft; /* copy of parsefile->nleft */
1174 static char *parsenextc; /* copy of parsefile->nextc */
1175 static int rootpid; /* pid of main shell */
1176 static int rootshell; /* true if we aren't a child of the main shell */
1178 static const char spcstr[] = " ";
1179 static const char snlfmt[] = "%s\n";
1181 static int sstrnleft;
1182 static int herefd = -1;
1184 static struct localvar *localvars;
1186 static struct var vifs;
1187 static struct var vmail;
1188 static struct var vmpath;
1189 static struct var vpath;
1190 static struct var vps1;
1191 static struct var vps2;
1192 static struct var voptind;
1194 #ifdef CONFIG_LOCALE_SUPPORT
1195 static struct var vlc_all;
1196 static struct var vlc_ctype;
1199 #ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
1200 static struct var vhistfile;
1207 void (*func) (const char *);
1210 static const char defpathvar[] =
1211 "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin";
1212 #define defpath (defpathvar + 5)
1215 static const char defifsvar[] = "IFS= \t\n";
1217 #define defifs (defifsvar + 4)
1219 static const char defifs[] = " \t\n";
1222 static const struct varinit varinit[] = {
1224 {&vifs, VSTRFIXED | VTEXTFIXED, defifsvar,
1226 {&vifs, VSTRFIXED | VTEXTFIXED | VUNSET, "IFS=",
1229 {&vmail, VSTRFIXED | VTEXTFIXED | VUNSET, "MAIL=",
1231 {&vmpath, VSTRFIXED | VTEXTFIXED | VUNSET, "MAILPATH=",
1233 {&vpath, VSTRFIXED | VTEXTFIXED, defpathvar,
1235 #if defined(CONFIG_FEATURE_COMMAND_EDITING) && defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
1236 {&vps1, VSTRFIXED | VTEXTFIXED, "PS1=\\w \\$ ",
1238 #endif /* else vps1 depends on uid */
1239 {&vps2, VSTRFIXED | VTEXTFIXED, "PS2=> ",
1241 {&voptind, VSTRFIXED | VTEXTFIXED, "OPTIND=1",
1243 #ifdef CONFIG_LOCALE_SUPPORT
1244 {&vlc_all, VSTRFIXED | VTEXTFIXED | VUNSET, "LC_ALL=",
1246 {&vlc_ctype, VSTRFIXED | VTEXTFIXED | VUNSET, "LC_CTYPE=",
1249 #ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
1250 {&vhistfile, VSTRFIXED | VTEXTFIXED | VUNSET, "HISTFILE=",
1259 static struct var *vartab[VTABSIZE];
1262 * The following macros access the values of the above variables.
1263 * They have to skip over the name. They return the null string
1264 * for unset variables.
1267 #define ifsval() (vifs.text + 4)
1268 #define ifsset() ((vifs.flags & VUNSET) == 0)
1269 #define mailval() (vmail.text + 5)
1270 #define mpathval() (vmpath.text + 9)
1271 #define pathval() (vpath.text + 5)
1272 #define ps1val() (vps1.text + 4)
1273 #define ps2val() (vps2.text + 4)
1274 #define optindval() (voptind.text + 7)
1276 #define mpathset() ((vmpath.flags & VUNSET) == 0)
1278 static void initvar(void);
1279 static void setvar(const char *, const char *, int);
1280 static void setvareq(char *, int);
1281 static void listsetvar(struct strlist *);
1282 static const char *lookupvar(const char *);
1283 static const char *bltinlookup(const char *);
1284 static char **environment(void);
1285 static int showvarscmd(int, char **);
1286 static void mklocal(char *);
1287 static void poplocalvars(void);
1288 static int unsetvar(const char *);
1289 static int varequal(const char *, const char *);
1292 static char *arg0; /* value of $0 */
1293 static struct shparam shellparam; /* current positional parameters */
1294 static char **argptr; /* argument list for builtin commands */
1295 static char *optionarg; /* set by nextopt (like getopt) */
1296 static char *optptr; /* used by nextopt */
1297 static char *minusc; /* argument to -c option */
1300 #ifdef CONFIG_ASH_ALIAS
1302 #define ALIASINUSE 1
1314 static struct alias *atab[ATABSIZE];
1316 static void setalias(char *, char *);
1317 static struct alias **hashalias(const char *);
1318 static struct alias *freealias(struct alias *);
1319 static struct alias **__lookupalias(const char *);
1321 static void setalias(char *name, char *val)
1323 struct alias *ap, **app;
1325 app = __lookupalias(name);
1329 if (!(ap->flag & ALIASINUSE)) {
1332 ap->val = xstrdup(val);
1333 ap->flag &= ~ALIASDEAD;
1336 ap = xmalloc(sizeof(struct alias));
1337 ap->name = xstrdup(name);
1338 ap->val = xstrdup(val);
1346 static int unalias(char *name)
1350 app = __lookupalias(name);
1354 *app = freealias(*app);
1362 static void rmaliases(void)
1364 struct alias *ap, **app;
1368 for (i = 0; i < ATABSIZE; i++) {
1370 for (ap = *app; ap; ap = *app) {
1371 *app = freealias(*app);
1380 static void printalias(const struct alias *ap)
1384 p = single_quote(ap->val);
1385 printf("alias %s=%s\n", ap->name, p);
1391 * TODO - sort output
1393 static int aliascmd(int argc, char **argv)
1402 for (i = 0; i < ATABSIZE; i++)
1403 for (ap = atab[i]; ap; ap = ap->next) {
1408 while ((n = *++argv) != NULL) {
1409 if ((v = strchr(n + 1, '=')) == NULL) { /* n+1: funny ksh stuff */
1410 if ((ap = *__lookupalias(n)) == NULL) {
1411 out2fmt("%s: %s not found\n", "alias", n);
1424 static int unaliascmd(int argc, char **argv)
1428 while ((i = nextopt("a")) != '\0') {
1434 for (i = 0; *argptr; argptr++) {
1435 if (unalias(*argptr)) {
1436 out2fmt("%s: %s not found\n", "unalias", *argptr);
1444 static struct alias **hashalias(const char *p)
1446 unsigned int hashval;
1451 return &atab[hashval % ATABSIZE];
1454 static struct alias *freealias(struct alias *ap)
1458 if (ap->flag & ALIASINUSE) {
1459 ap->flag |= ALIASDEAD;
1471 static struct alias **__lookupalias(const char *name)
1473 struct alias **app = hashalias(name);
1475 for (; *app; app = &(*app)->next) {
1476 if (equal(name, (*app)->name)) {
1485 #ifdef CONFIG_ASH_MATH_SUPPORT
1486 /* The generated file arith.c has been replaced with a custom hand
1487 * written implementation written by Aaron Lehmann <aaronl@vitelus.com>.
1488 * This is now part of libbb, so that it can be used by all the shells
1490 static void expari(int);
1493 static char *trap[NSIG]; /* trap handler commands */
1494 static char sigmode[NSIG - 1]; /* current value of signal */
1495 static char gotsig[NSIG - 1]; /* indicates specified signal received */
1496 static int pendingsigs; /* indicates some signal received */
1499 * This file was generated by the mkbuiltins program.
1502 #ifdef CONFIG_ASH_JOB_CONTROL
1503 static int bgcmd(int, char **);
1504 static int fgcmd(int, char **);
1505 static int killcmd(int, char **);
1507 static int bltincmd(int, char **);
1508 static int cdcmd(int, char **);
1509 static int breakcmd(int, char **);
1511 #ifdef CONFIG_ASH_CMDCMD
1512 static int commandcmd(int, char **);
1514 static int dotcmd(int, char **);
1515 static int evalcmd(int, char **);
1516 static int execcmd(int, char **);
1517 static int exitcmd(int, char **);
1518 static int exportcmd(int, char **);
1519 static int histcmd(int, char **);
1520 static int hashcmd(int, char **);
1521 static int helpcmd(int, char **);
1522 static int jobscmd(int, char **);
1523 static int localcmd(int, char **);
1524 static int pwdcmd(int, char **);
1525 static int readcmd(int, char **);
1526 static int returncmd(int, char **);
1527 static int setcmd(int, char **);
1528 static int setvarcmd(int, char **);
1529 static int shiftcmd(int, char **);
1530 static int trapcmd(int, char **);
1531 static int umaskcmd(int, char **);
1533 #ifdef CONFIG_ASH_ALIAS
1534 static int aliascmd(int, char **);
1535 static int unaliascmd(int, char **);
1537 static int unsetcmd(int, char **);
1538 static int waitcmd(int, char **);
1539 static int ulimitcmd(int, char **);
1540 static int timescmd(int, char **);
1542 #ifdef CONFIG_ASH_MATH_SUPPORT
1543 static int letcmd(int, char **);
1545 static int typecmd(int, char **);
1547 #ifdef CONFIG_ASH_GETOPTS
1548 static int getoptscmd(int, char **);
1552 static int true_main(int, char **);
1554 #ifndef CONFIG_FALSE
1555 static int false_main(int, char **);
1558 static void setpwd(const char *, int);
1561 #define BUILTIN_NOSPEC "0"
1562 #define BUILTIN_SPECIAL "1"
1563 #define BUILTIN_REGULAR "2"
1564 #define BUILTIN_ASSIGN "4"
1565 #define BUILTIN_SPEC_ASSG "5"
1566 #define BUILTIN_REG_ASSG "6"
1568 #define IS_BUILTIN_SPECIAL(builtincmd) ((builtincmd)->name[0] & 1)
1569 #define IS_BUILTIN_REGULAR(builtincmd) ((builtincmd)->name[0] & 2)
1570 #define IS_BUILTIN_ASSIGN(builtincmd) ((builtincmd)->name[0] & 4)
1574 int (*const builtinfunc) (int, char **);
1579 /* It is CRUCIAL that this listing be kept in ascii order, otherwise
1580 * the binary search in find_builtin() will stop working. If you value
1581 * your kneecaps, you'll be sure to *make sure* that any changes made
1582 * to this array result in the listing remaining in ascii order. You
1585 static const struct builtincmd builtincmds[] = {
1586 {BUILTIN_SPECIAL ".", dotcmd}, /* first, see declare DOTCMD */
1587 {BUILTIN_SPECIAL ":", true_main},
1588 #ifdef CONFIG_ASH_ALIAS
1589 {BUILTIN_REG_ASSG "alias", aliascmd},
1591 #ifdef CONFIG_ASH_JOB_CONTROL
1592 {BUILTIN_REGULAR "bg", bgcmd},
1594 {BUILTIN_SPECIAL "break", breakcmd},
1595 {BUILTIN_SPECIAL "builtin", bltincmd},
1596 {BUILTIN_REGULAR "cd", cdcmd},
1597 {BUILTIN_NOSPEC "chdir", cdcmd},
1598 #ifdef CONFIG_ASH_CMDCMD
1599 {BUILTIN_REGULAR "command", commandcmd},
1601 {BUILTIN_SPECIAL "continue", breakcmd},
1602 {BUILTIN_SPECIAL "eval", evalcmd},
1603 {BUILTIN_SPECIAL "exec", execcmd},
1604 {BUILTIN_SPECIAL "exit", exitcmd},
1605 {BUILTIN_SPEC_ASSG "export", exportcmd},
1606 {BUILTIN_REGULAR "false", false_main},
1607 {BUILTIN_REGULAR "fc", histcmd},
1608 #ifdef CONFIG_ASH_JOB_CONTROL
1609 {BUILTIN_REGULAR "fg", fgcmd},
1611 #ifdef CONFIG_ASH_GETOPTS
1612 {BUILTIN_REGULAR "getopts", getoptscmd},
1614 {BUILTIN_NOSPEC "hash", hashcmd},
1615 {BUILTIN_NOSPEC "help", helpcmd},
1616 {BUILTIN_REGULAR "jobs", jobscmd},
1617 #ifdef CONFIG_ASH_JOB_CONTROL
1618 {BUILTIN_REGULAR "kill", killcmd},
1620 #ifdef CONFIG_ASH_MATH_SUPPORT
1621 {BUILTIN_REGULAR "let", letcmd},
1623 {BUILTIN_ASSIGN "local", localcmd},
1624 {BUILTIN_NOSPEC "pwd", pwdcmd},
1625 {BUILTIN_REGULAR "read", readcmd},
1626 {BUILTIN_SPEC_ASSG "readonly", exportcmd},
1627 {BUILTIN_SPECIAL "return", returncmd},
1628 {BUILTIN_SPECIAL "set", setcmd},
1629 {BUILTIN_NOSPEC "setvar", setvarcmd},
1630 {BUILTIN_SPECIAL "shift", shiftcmd},
1631 {BUILTIN_SPECIAL "times", timescmd},
1632 {BUILTIN_SPECIAL "trap", trapcmd},
1633 {BUILTIN_REGULAR "true", true_main},
1634 {BUILTIN_NOSPEC "type", typecmd},
1635 {BUILTIN_NOSPEC "ulimit", ulimitcmd},
1636 {BUILTIN_REGULAR "umask", umaskcmd},
1637 #ifdef CONFIG_ASH_ALIAS
1638 {BUILTIN_REGULAR "unalias", unaliascmd},
1640 {BUILTIN_SPECIAL "unset", unsetcmd},
1641 {BUILTIN_REGULAR "wait", waitcmd},
1644 #define NUMBUILTINS (sizeof (builtincmds) / sizeof (struct builtincmd) )
1646 #define DOTCMD &builtincmds[0]
1647 static struct builtincmd *BLTINCMD;
1648 static struct builtincmd *EXECCMD;
1649 static struct builtincmd *EVALCMD;
1652 #define JOBSTOPPED 1 /* all procs are stopped */
1653 #define JOBDONE 2 /* all procs are completed */
1656 * A job structure contains information about a job. A job is either a
1657 * single process or a set of processes contained in a pipeline. In the
1658 * latter case, pidlist will be non-NULL, and will point to a -1 terminated
1663 pid_t pid; /* process id */
1664 int status; /* status flags (defined above) */
1665 char *cmd; /* text of command being run */
1669 static int job_warning; /* user was warned about stopped jobs */
1671 #ifdef CONFIG_ASH_JOB_CONTROL
1672 static void setjobctl(int enable);
1674 #define setjobctl(on) /* do nothing */
1679 struct procstat ps0; /* status of process */
1680 struct procstat *ps; /* status or processes when more than one */
1681 short nprocs; /* number of processes */
1682 short pgrp; /* process group of this job */
1683 char state; /* true if job is finished */
1684 char used; /* true if this entry is in used */
1685 char changed; /* true if status has changed */
1686 #ifdef CONFIG_ASH_JOB_CONTROL
1687 char jobctl; /* job running under job control */
1691 static struct job *jobtab; /* array of jobs */
1692 static int njobs; /* size of array */
1693 static int backgndpid = -1; /* pid of last background process */
1695 #ifdef CONFIG_ASH_JOB_CONTROL
1696 static int initialpgrp; /* pgrp of shell on invocation */
1697 static int curjob; /* current job */
1701 static struct job *makejob(const union node *, int);
1702 static int forkshell(struct job *, const union node *, int);
1703 static int waitforjob(struct job *);
1705 static int docd(char *, int);
1706 static void getpwd(void);
1708 static char *padvance(const char **, const char *);
1710 static char nullstr[1]; /* zero length string */
1711 static char *curdir = nullstr; /* current working directory */
1713 static int cdcmd(int argc, char **argv)
1722 if ((dest = *argptr) == NULL && (dest = bltinlookup("HOME")) == NULL)
1723 error("HOME not set");
1726 if (dest[0] == '-' && dest[1] == '\0') {
1727 dest = bltinlookup("OLDPWD");
1728 if (!dest || !*dest) {
1737 if (*dest == '/' || (path = bltinlookup("CDPATH")) == NULL)
1739 while ((p = padvance(&path, dest)) != NULL) {
1740 if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) {
1745 if (p[0] == '.' && p[1] == '/' && p[2] != '\0')
1747 print = strcmp(p, dest);
1749 if (docd(p, print) >= 0)
1754 error("can't cd to %s", dest);
1760 * Update curdir (the name of the current directory) in response to a
1761 * cd command. We also call hashcd to let the routines in exec.c know
1762 * that the current directory has changed.
1765 static void hashcd(void);
1767 static inline void updatepwd(const char *dir)
1769 hashcd(); /* update command hash table */
1772 * If our argument is NULL, we don't know the current directory
1774 if (dir == NULL || curdir == nullstr) {
1782 * Actually do the chdir. In an interactive shell, print the
1783 * directory name if "print" is nonzero.
1786 static int docd(char *dest, int print)
1788 TRACE(("docd(\"%s\", %d) called\n", dest, print));
1790 if (chdir(dest) < 0) {
1802 static int pwdcmd(int argc, char **argv)
1808 /* Ask system the current directory */
1809 static void getpwd(void)
1811 curdir = xgetcwd(0);
1816 static void setpwd(const char *val, int setold)
1821 setvar("OLDPWD", curdir, VEXPORT);
1824 if (curdir != nullstr) {
1825 if (val != NULL && *val != '/')
1826 val = cated = concat_path_file(curdir, val);
1832 curdir = simplify_path(val);
1835 setvar("PWD", curdir, VEXPORT);
1839 * Errors and exceptions.
1843 * Code to handle exceptions in C.
1847 * We enclose jmp_buf in a structure so that we can declare pointers to
1848 * jump locations. The global variable handler contains the location to
1849 * jump to when an exception occurs, and the global variable exception
1850 * contains a code identifying the exeception. To implement nested
1851 * exception handlers, the user should save the value of handler on entry
1852 * to an inner scope, set handler to point to a jmploc structure for the
1853 * inner scope, and restore handler on exit from the scope.
1861 #define EXINT 0 /* SIGINT received */
1862 #define EXERROR 1 /* a generic error */
1863 #define EXSHELLPROC 2 /* execute a shell procedure */
1864 #define EXEXEC 3 /* command execution failed */
1865 #define EXREDIR 4 /* redirection error */
1867 static struct jmploc *handler;
1868 static int exception;
1870 static void exverror(int, const char *, va_list)
1871 __attribute__ ((__noreturn__));
1874 * Called to raise an exception. Since C doesn't include exceptions, we
1875 * just do a longjmp to the exception handler. The type of exception is
1876 * stored in the global variable "exception".
1879 static void exraise(int) __attribute__ ((__noreturn__));
1881 static void exraise(int e)
1884 if (handler == NULL)
1889 longjmp(handler->loc, 1);
1894 * Called from trap.c when a SIGINT is received. (If the user specifies
1895 * that SIGINT is to be trapped or ignored using the trap builtin, then
1896 * this routine is not called.) Suppressint is nonzero when interrupts
1897 * are held using the INTOFF macro. The call to _exit is necessary because
1898 * there is a short period after a fork before the signal handlers are
1899 * set to the appropriate value for the child. (The test for iflag is
1900 * just defensive programming.)
1903 static void onint(void)
1912 sigemptyset(&mysigset);
1913 sigprocmask(SIG_SETMASK, &mysigset, NULL);
1914 if (!(rootshell && iflag)) {
1915 signal(SIGINT, SIG_DFL);
1923 static char *commandname; /* currently executing command */
1926 * Exverror is called to raise the error exception. If the first argument
1927 * is not NULL then error prints an error message using printf style
1928 * formatting. It then raises the error exception.
1930 static void exverror(int cond, const char *msg, va_list ap)
1937 TRACE(("exverror(%d, \"%s\") pid=%d\n", cond, msg, getpid()));
1939 TRACE(("exverror(%d, NULL) pid=%d\n", cond, getpid()));
1943 out2fmt("%s: ", commandname);
1944 vfprintf(stderr, msg, ap);
1952 static void error(const char *msg, ...)
1957 exverror(EXERROR, msg, ap);
1963 static void exerror(int cond, const char *msg, ...)
1968 exverror(cond, msg, ap);
1976 * Table of error messages.
1980 short errcode; /* error number */
1981 short action; /* operation which encountered the error */
1985 * Types of operations (passed to the errmsg routine).
1988 #define E_OPEN 01 /* opening a file */
1989 #define E_CREAT 02 /* creating a file */
1990 #define E_EXEC 04 /* executing a program */
1992 #define ALL (E_OPEN|E_CREAT|E_EXEC)
1994 static const struct errname errormsg[] = {
2048 #define ERRNAME_SIZE (sizeof(errormsg)/sizeof(struct errname))
2051 * Return a string describing an error. The returned string may be a
2052 * pointer to a static buffer that will be overwritten on the next call.
2053 * Action describes the operation that got the error.
2056 static const char *errmsg(int e, int action)
2058 struct errname const *ep;
2059 static char buf[12];
2061 for (ep = errormsg; ep < errormsg + ERRNAME_SIZE; ep++) {
2062 if (ep->errcode == e && (ep->action & action) != 0)
2066 snprintf(buf, sizeof buf, "error %d", e);
2071 #ifdef CONFIG_ASH_OPTIMIZE_FOR_SIZE
2072 static void __inton()
2074 if (--suppressint == 0 && intpending) {
2078 static void forceinton(void)
2086 /* flags in argument to evaltree */
2087 #define EV_EXIT 01 /* exit after evaluating tree */
2088 #define EV_TESTED 02 /* exit status is checked; ignore -e flag */
2089 #define EV_BACKCMD 04 /* command executing within back quotes */
2091 static int evalskip; /* set if we are skipping commands */
2092 static int skipcount; /* number of levels to skip */
2093 static int loopnest; /* current loop nesting level */
2094 static int funcnest; /* depth of function calls */
2097 static struct strlist *cmdenviron; /* environment for builtin command */
2098 static int exitstatus; /* exit status of last command */
2099 static int oexitstatus; /* saved exit status */
2101 static void evalsubshell(const union node *, int);
2102 static void expredir(union node *);
2103 static void prehash(union node *);
2104 static void eprintlist(struct strlist *);
2106 static union node *parsecmd(int);
2109 * Called to reset things after an exception.
2113 * The eval commmand.
2115 static void evalstring(char *, int);
2117 static int evalcmd(int argc, char **argv)
2126 STARTSTACKSTR(concat);
2130 STPUTC(*p++, concat);
2131 if ((p = *ap++) == NULL)
2133 STPUTC(' ', concat);
2135 STPUTC('\0', concat);
2136 p = grabstackstr(concat);
2138 evalstring(p, EV_TESTED);
2144 * Execute a command or commands contained in a string.
2147 static void evaltree(union node *, int);
2148 static void setinputstring(char *);
2149 static void popfile(void);
2150 static void setstackmark(struct stackmark *mark);
2151 static void popstackmark(struct stackmark *mark);
2154 static void evalstring(char *s, int flag)
2157 struct stackmark smark;
2159 setstackmark(&smark);
2161 while ((n = parsecmd(0)) != NEOF) {
2163 popstackmark(&smark);
2166 popstackmark(&smark);
2169 static struct builtincmd *find_builtin(const char *);
2170 static void expandarg(union node *, struct arglist *, int);
2171 static void calcsize(const union node *);
2172 static union node *copynode(const union node *);
2175 * Make a copy of a parse tree.
2178 static int funcblocksize; /* size of structures in function */
2179 static int funcstringsize; /* size of strings in node */
2180 static pointer funcblock; /* block to allocate function from */
2181 static char *funcstring; /* block to allocate strings from */
2184 static inline union node *copyfunc(union node *n)
2191 funcblock = xmalloc(funcblocksize + funcstringsize);
2192 funcstring = (char *) funcblock + funcblocksize;
2197 * Add a new command entry, replacing any existing command entry for
2201 static inline void addcmdentry(char *name, struct cmdentry *entry)
2203 struct tblentry *cmdp;
2206 cmdp = cmdlookup(name, 1);
2207 if (cmdp->cmdtype == CMDFUNCTION) {
2208 free(cmdp->param.func);
2210 cmdp->cmdtype = entry->cmdtype;
2211 cmdp->param = entry->u;
2215 static inline void evalloop(const union node *n, int flags)
2223 evaltree(n->nbinary.ch1, EV_TESTED);
2225 skipping:if (evalskip == SKIPCONT && --skipcount <= 0) {
2229 if (evalskip == SKIPBREAK && --skipcount <= 0)
2233 if (n->type == NWHILE) {
2234 if (exitstatus != 0)
2237 if (exitstatus == 0)
2240 evaltree(n->nbinary.ch2, flags);
2241 status = exitstatus;
2246 exitstatus = status;
2249 static void evalfor(const union node *n, int flags)
2251 struct arglist arglist;
2254 struct stackmark smark;
2256 setstackmark(&smark);
2257 arglist.lastp = &arglist.list;
2258 for (argp = n->nfor.args; argp; argp = argp->narg.next) {
2259 oexitstatus = exitstatus;
2260 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE | EXP_RECORD);
2264 *arglist.lastp = NULL;
2269 for (sp = arglist.list; sp; sp = sp->next) {
2270 setvar(n->nfor.var, sp->text, 0);
2271 evaltree(n->nfor.body, flags);
2273 if (evalskip == SKIPCONT && --skipcount <= 0) {
2277 if (evalskip == SKIPBREAK && --skipcount <= 0)
2284 popstackmark(&smark);
2287 static inline void evalcase(const union node *n, int flags)
2291 struct arglist arglist;
2292 struct stackmark smark;
2294 setstackmark(&smark);
2295 arglist.lastp = &arglist.list;
2296 oexitstatus = exitstatus;
2297 expandarg(n->ncase.expr, &arglist, EXP_TILDE);
2298 for (cp = n->ncase.cases; cp && evalskip == 0; cp = cp->nclist.next) {
2299 for (patp = cp->nclist.pattern; patp; patp = patp->narg.next) {
2300 if (casematch(patp, arglist.list->text)) {
2301 if (evalskip == 0) {
2302 evaltree(cp->nclist.body, flags);
2309 popstackmark(&smark);
2313 * Evaluate a pipeline. All the processes in the pipeline are children
2314 * of the process creating the pipeline. (This differs from some versions
2315 * of the shell, which make the last process in a pipeline the parent
2319 static inline void evalpipe(union node *n, int flags)
2322 struct nodelist *lp;
2327 TRACE(("evalpipe(0x%lx) called\n", (long) n));
2329 for (lp = n->npipe.cmdlist; lp; lp = lp->next)
2333 jp = makejob(n, pipelen);
2335 for (lp = n->npipe.cmdlist; lp; lp = lp->next) {
2339 if (pipe(pip) < 0) {
2341 error("Pipe call failed");
2344 if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {
2357 evaltree(lp->n, flags);
2364 if (n->npipe.backgnd == 0) {
2365 exitstatus = waitforjob(jp);
2366 TRACE(("evalpipe: job done exit status %d\n", exitstatus));
2371 static void find_command(const char *, struct cmdentry *, int, const char *);
2373 static int isassignment(const char *word)
2375 if (!is_name(*word)) {
2380 } while (is_in_name(*word));
2381 return *word == '=';
2385 static void evalcommand(union node *cmd, int flags)
2387 struct stackmark smark;
2389 struct arglist arglist;
2390 struct arglist varlist;
2396 struct cmdentry cmdentry;
2398 char *volatile savecmdname;
2399 volatile struct shparam saveparam;
2400 struct localvar *volatile savelocalvars;
2405 struct jmploc *volatile savehandler;
2406 struct jmploc jmploc;
2409 /* Avoid longjmp clobbering */
2417 /* First expand the arguments. */
2418 TRACE(("evalcommand(0x%lx, %d) called\n", (long) cmd, flags));
2419 setstackmark(&smark);
2420 arglist.lastp = &arglist.list;
2421 varlist.lastp = &varlist.list;
2423 oexitstatus = exitstatus;
2426 for (argp = cmd->ncmd.assign; argp; argp = argp->narg.next) {
2427 expandarg(argp, &varlist, EXP_VARTILDE);
2429 for (argp = cmd->ncmd.args; argp && !arglist.list; argp = argp->narg.next) {
2430 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
2433 struct builtincmd *bcmd;
2436 bcmd = find_builtin(arglist.list->text);
2437 pseudovarflag = bcmd && IS_BUILTIN_ASSIGN(bcmd);
2438 for (; argp; argp = argp->narg.next) {
2439 if (pseudovarflag && isassignment(argp->narg.text)) {
2440 expandarg(argp, &arglist, EXP_VARTILDE);
2443 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
2446 *arglist.lastp = NULL;
2447 *varlist.lastp = NULL;
2448 expredir(cmd->ncmd.redirect);
2450 for (sp = arglist.list; sp; sp = sp->next)
2452 argv = stalloc(sizeof(char *) * (argc + 1));
2454 for (sp = arglist.list; sp; sp = sp->next) {
2455 TRACE(("evalcommand arg: %s\n", sp->text));
2460 if (iflag && funcnest == 0 && argc > 0)
2464 /* Print the command if xflag is set. */
2467 eprintlist(varlist.list);
2468 eprintlist(arglist.list);
2472 /* Now locate the command. */
2474 cmdentry.cmdtype = CMDBUILTIN;
2475 cmdentry.u.cmd = BLTINCMD;
2478 const char *oldpath;
2479 int findflag = DO_ERR;
2483 * Modify the command lookup path, if a PATH= assignment
2486 for (sp = varlist.list; sp; sp = sp->next)
2487 if (varequal(sp->text, defpathvar)) {
2488 path = sp->text + 5;
2489 findflag |= DO_BRUTE;
2492 oldfindflag = findflag;
2495 find_command(argv[0], &cmdentry, findflag, path);
2496 if (cmdentry.cmdtype == CMDUNKNOWN) { /* command not found */
2500 /* implement bltin and command here */
2501 if (cmdentry.cmdtype != CMDBUILTIN) {
2504 if (spclbltin < 0) {
2505 spclbltin = !!(IS_BUILTIN_SPECIAL(cmdentry.u.cmd)) * 2;
2507 if (cmdentry.u.cmd == BLTINCMD) {
2509 struct builtincmd *bcmd;
2514 if (!(bcmd = find_builtin(*argv))) {
2515 out2fmt("%s: not found\n", *argv);
2519 cmdentry.u.cmd = bcmd;
2520 if (bcmd != BLTINCMD)
2524 if (cmdentry.u.cmd == find_builtin("command")) {
2529 if (*argv[0] == '-') {
2530 if (!equal(argv[0], "-p")) {
2540 findflag |= DO_BRUTE;
2543 findflag = oldfindflag;
2545 findflag |= DO_NOFUN;
2553 /* Fork off a child process if necessary. */
2554 if (cmd->ncmd.backgnd
2555 || (cmdentry.cmdtype == CMDNORMAL && (!(flags & EV_EXIT) || trap[0]))
2558 jp = makejob(cmd, 1);
2559 mode = cmd->ncmd.backgnd;
2560 if (forkshell(jp, cmd, mode) != 0)
2561 goto parent; /* at end of routine */
2568 /* This is the child process if a fork occurred. */
2569 /* Execute the command. */
2570 if (cmdentry.cmdtype == CMDFUNCTION) {
2572 trputs("Shell function: ");
2575 exitstatus = oexitstatus;
2576 saveparam = shellparam;
2577 shellparam.malloc = 0;
2578 shellparam.nparam = argc - 1;
2579 shellparam.p = argv + 1;
2581 savelocalvars = localvars;
2584 if (setjmp(jmploc.loc)) {
2585 if (exception == EXREDIR) {
2589 saveparam.optind = shellparam.optind;
2590 saveparam.optoff = shellparam.optoff;
2591 freeparam(&shellparam);
2592 shellparam = saveparam;
2594 localvars = savelocalvars;
2595 handler = savehandler;
2596 longjmp(handler->loc, 1);
2598 savehandler = handler;
2600 redirect(cmd->ncmd.redirect, REDIR_PUSH);
2601 listsetvar(varlist.list);
2603 evaltree(cmdentry.u.func, flags & EV_TESTED);
2608 localvars = savelocalvars;
2609 saveparam.optind = shellparam.optind;
2610 saveparam.optoff = shellparam.optoff;
2611 freeparam(&shellparam);
2612 shellparam = saveparam;
2613 handler = savehandler;
2616 if (evalskip == SKIPFUNC) {
2620 } else if (cmdentry.cmdtype == CMDBUILTIN) {
2624 trputs("builtin command: ");
2627 redir = (cmdentry.u.cmd == EXECCMD) ? 0 : REDIR_PUSH;
2628 savecmdname = commandname;
2630 listsetvar(varlist.list);
2632 cmdenviron = varlist.list;
2635 if (setjmp(jmploc.loc)) {
2637 exitstatus = (e == EXINT) ? SIGINT + 128 : 2;
2640 savehandler = handler;
2642 redirect(cmd->ncmd.redirect, redir);
2643 commandname = argv[0];
2645 optptr = NULL; /* initialize nextopt */
2646 exitstatus = (*cmdentry.u.cmd->builtinfunc) (argc, argv);
2650 commandname = savecmdname;
2651 handler = savehandler;
2653 if (e == EXINT || spclbltin & 2) {
2659 if (cmdentry.u.cmd != EXECCMD)
2663 trputs("normal command: ");
2666 redirect(cmd->ncmd.redirect, 0);
2668 for (sp = varlist.list; sp; sp = sp->next)
2669 setvareq(sp->text, VEXPORT | VSTACK);
2670 envp = environment();
2671 shellexec(argv, envp, path, cmdentry.u.index);
2673 if (flags & EV_EXIT)
2674 exitshell(exitstatus);
2677 parent: /* parent process gets here (if we forked) */
2678 if (mode == 0) { /* argument to fork */
2679 exitstatus = waitforjob(jp);
2685 setvar("_", lastarg, 0);
2686 popstackmark(&smark);
2690 * Evaluate a parse tree. The value is left in the global variable
2693 static void evaltree(union node *n, int flags)
2698 TRACE(("evaltree(NULL) called\n"));
2701 TRACE(("evaltree(0x%lx: %d) called\n", (long) n, n->type));
2704 evaltree(n->nbinary.ch1, flags & EV_TESTED);
2707 evaltree(n->nbinary.ch2, flags);
2710 evaltree(n->nbinary.ch1, EV_TESTED);
2711 if (evalskip || exitstatus != 0)
2713 evaltree(n->nbinary.ch2, flags);
2716 evaltree(n->nbinary.ch1, EV_TESTED);
2717 if (evalskip || exitstatus == 0)
2719 evaltree(n->nbinary.ch2, flags);
2722 expredir(n->nredir.redirect);
2723 redirect(n->nredir.redirect, REDIR_PUSH);
2724 evaltree(n->nredir.n, flags);
2728 evalsubshell(n, flags);
2731 evalsubshell(n, flags);
2734 evaltree(n->nif.test, EV_TESTED);
2737 if (exitstatus == 0)
2738 evaltree(n->nif.ifpart, flags);
2739 else if (n->nif.elsepart)
2740 evaltree(n->nif.elsepart, flags);
2756 struct builtincmd *bcmd;
2757 struct cmdentry entry;
2759 if ((bcmd = find_builtin(n->narg.text)) && IS_BUILTIN_SPECIAL(bcmd)
2761 out2fmt("%s is a special built-in\n", n->narg.text);
2765 entry.cmdtype = CMDFUNCTION;
2766 entry.u.func = copyfunc(n->narg.next);
2767 addcmdentry(n->narg.text, &entry);
2772 evaltree(n->nnot.com, EV_TESTED);
2773 exitstatus = !exitstatus;
2781 evalcommand(n, flags);
2786 printf("Node type = %d\n", n->type);
2793 if (flags & EV_EXIT ||
2794 (checkexit && eflag && exitstatus && !(flags & EV_TESTED))
2796 exitshell(exitstatus);
2800 * Kick off a subshell to evaluate a tree.
2803 static void evalsubshell(const union node *n, int flags)
2806 int backgnd = (n->type == NBACKGND);
2808 expredir(n->nredir.redirect);
2809 if (!backgnd && flags & EV_EXIT && !trap[0])
2813 if (forkshell(jp, n, backgnd) == 0) {
2817 flags &= ~EV_TESTED;
2819 redirect(n->nredir.redirect, 0);
2820 evaltree(n->nredir.n, flags); /* never returns */
2823 exitstatus = waitforjob(jp);
2829 * Compute the names of the files in a redirection list.
2832 static void fixredir(union node *n, const char *text, int err);
2834 static void expredir(union node *n)
2838 for (redir = n; redir; redir = redir->nfile.next) {
2841 fn.lastp = &fn.list;
2842 oexitstatus = exitstatus;
2843 switch (redir->type) {
2849 expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
2850 redir->nfile.expfname = fn.list->text;
2854 if (redir->ndup.vname) {
2855 expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE);
2856 fixredir(redir, fn.list->text, 1);
2865 * Execute a command inside back quotes. If it's a builtin command, we
2866 * want to save its output in a block obtained from malloc. Otherwise
2867 * we fork off a subprocess and get the output of the command via a pipe.
2868 * Should be called with interrupts off.
2871 static void evalbackcmd(union node *n, struct backcmd *result)
2875 struct stackmark smark; /* unnecessary */
2877 setstackmark(&smark);
2888 error("Pipe call failed");
2890 if (forkshell(jp, n, FORK_NOJOB) == 0) {
2895 dup_as_newfd(pip[1], 1);
2899 evaltree(n, EV_EXIT);
2902 result->fd = pip[0];
2905 popstackmark(&smark);
2906 TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
2907 result->fd, result->buf, result->nleft, result->jp));
2912 * Execute a simple command.
2916 * Search for a command. This is called before we fork so that the
2917 * location of the command will be available in the parent as well as
2918 * the child. The check for "goodname" is an overly conservative
2919 * check that the name will not be subject to expansion.
2922 static void prehash(union node *n)
2924 struct cmdentry entry;
2926 if (n->type == NCMD && n->ncmd.args)
2927 if (goodname(n->ncmd.args->narg.text))
2928 find_command(n->ncmd.args->narg.text, &entry, 0, pathval());
2933 * Builtin commands. Builtin commands whose functions are closely
2934 * tied to evaluation are implemented here.
2938 * No command given, or a bltin command with no arguments. Set the
2939 * specified variables.
2942 int bltincmd(int argc, char **argv)
2945 * Preserve exitstatus of a previous possible redirection
2953 * Handle break and continue commands. Break, continue, and return are
2954 * all handled by setting the evalskip flag. The evaluation routines
2955 * above all check this flag, and if it is set they start skipping
2956 * commands rather than executing them. The variable skipcount is
2957 * the number of loops to break/continue, or the number of function
2958 * levels to return. (The latter is always 1.) It should probably
2959 * be an error to break out of more loops than exist, but it isn't
2960 * in the standard shell so we don't make it one here.
2963 static int breakcmd(int argc, char **argv)
2965 int n = argc > 1 ? number(argv[1]) : 1;
2968 error("Illegal number: %s", argv[1]);
2972 evalskip = (**argv == 'c') ? SKIPCONT : SKIPBREAK;
2980 * The return command.
2983 static int returncmd(int argc, char **argv)
2985 int ret = argc > 1 ? number(argv[1]) : oexitstatus;
2988 evalskip = SKIPFUNC;
2992 /* Do what ksh does; skip the rest of the file */
2993 evalskip = SKIPFILE;
3000 #ifndef CONFIG_FALSE
3001 static int false_main(int argc, char **argv)
3008 static int true_main(int argc, char **argv)
3015 * Controls whether the shell is interactive or not.
3018 static void setsignal(int signo);
3020 #ifdef CONFIG_ASH_MAIL
3021 static void chkmail(int silent);
3024 static void setinteractive(int on)
3026 static int is_interactive;
3027 static int do_banner = 0;
3029 if (on == is_interactive)
3034 #ifdef CONFIG_ASH_MAIL
3037 is_interactive = on;
3038 if (do_banner == 0 && is_interactive) {
3039 /* Looks like they want an interactive shell */
3040 #ifndef CONFIG_FEATURE_SH_EXTRA_QUIET
3041 printf("\n\n" BB_BANNER " Built-in shell (ash)\n");
3042 printf("Enter 'help' for a list of built-in commands.\n\n");
3048 static void optschanged(void)
3050 setinteractive(iflag);
3055 static int execcmd(int argc, char **argv)
3060 iflag = 0; /* exit on error */
3063 for (sp = cmdenviron; sp; sp = sp->next)
3064 setvareq(sp->text, VEXPORT | VSTACK);
3065 shellexec(argv + 1, environment(), pathval(), 0);
3070 static void eprintlist(struct strlist *sp)
3072 for (; sp; sp = sp->next) {
3073 out2fmt(" %s", sp->text);
3078 * Exec a program. Never returns. If you change this routine, you may
3079 * have to change the find_command routine as well.
3082 static const char *pathopt; /* set by padvance */
3084 static void shellexec(char **argv, char **envp, const char *path, int idx)
3089 if (strchr(argv[0], '/') != NULL
3090 #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
3091 || find_applet_by_name(argv[0])
3095 tryexec(argv[0], argv, envp);
3099 while ((cmdname = padvance(&path, argv[0])) != NULL) {
3100 if (--idx < 0 && pathopt == NULL) {
3101 tryexec(cmdname, argv, envp);
3102 if (errno != ENOENT && errno != ENOTDIR)
3109 /* Map to POSIX errors */
3121 exerror(EXEXEC, "%s: %s", argv[0], errmsg(e, E_EXEC));
3126 * Clear traps on a fork.
3128 static void clear_traps(void)
3132 for (tp = trap; tp < &trap[NSIG]; tp++) {
3133 if (*tp && **tp) { /* trap not NULL or SIG_IGN */
3138 setsignal(tp - trap);
3145 static int preadbuffer(void);
3146 static void pushfile(void);
3149 * Read a character from the script, returning PEOF on end of file.
3150 * Nul characters in the input are silently discarded.
3153 #ifndef CONFIG_ASH_OPTIMIZE_FOR_SIZE
3154 #define pgetc_macro() (--parsenleft >= 0? *parsenextc++ : preadbuffer())
3155 static int pgetc(void)
3157 return pgetc_macro();
3160 static int pgetc_macro(void)
3162 return --parsenleft >= 0 ? *parsenextc++ : preadbuffer();
3165 static inline int pgetc(void)
3167 return pgetc_macro();
3173 * Undo the last call to pgetc. Only one character may be pushed back.
3174 * PEOF may be pushed back.
3177 static void pungetc(void)
3184 static void popfile(void)
3186 struct parsefile *pf = parsefile;
3194 parsefile = pf->prev;
3196 parsenleft = parsefile->nleft;
3197 parselleft = parsefile->lleft;
3198 parsenextc = parsefile->nextc;
3199 plinno = parsefile->linno;
3205 * Return to top level.
3208 static void popallfiles(void)
3210 while (parsefile != &basepf)
3215 * Close the file(s) that the shell is reading commands from. Called
3216 * after a fork is done.
3219 static void closescript(void)
3222 if (parsefile->fd > 0) {
3223 close(parsefile->fd);
3230 * Like setinputfile, but takes an open file descriptor. Call this with
3234 static void setinputfd(int fd, int push)
3236 (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
3242 while (parsefile->strpush)
3246 if (parsefile->buf == NULL)
3247 parsefile->buf = xmalloc(BUFSIZ);
3248 parselleft = parsenleft = 0;
3254 * Set the input to take input from a file. If push is set, push the
3255 * old input onto the stack first.
3258 static void setinputfile(const char *fname, int push)
3264 if ((fd = open(fname, O_RDONLY)) < 0)
3265 error("Can't open %s", fname);
3267 myfileno2 = dup_as_newfd(fd, 10);
3270 error("Out of file descriptors");
3273 setinputfd(fd, push);
3278 static void tryexec(char *cmd, char **argv, char **envp)
3282 #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
3286 #ifdef CONFIG_FEATURE_SH_APPLETS_ALWAYS_WIN
3287 name = get_last_path_component(name);
3288 if(find_applet_by_name(name) != NULL)
3291 if(strchr(name, '/') == NULL && find_applet_by_name(name) != NULL) {
3300 if(strcmp(name, "busybox")) {
3301 for (ap = argv; *ap; ap++);
3302 ap = new = xmalloc((ap - argv + 2) * sizeof(char *));
3303 *ap++ = cmd = "/bin/busybox";
3304 while ((*ap++ = *argv++));
3308 cmd = "/bin/busybox";
3313 execve(cmd, argv, envp);
3316 } else if (errno == ENOEXEC) {
3320 for (ap = argv; *ap; ap++);
3321 ap = new = xmalloc((ap - argv + 2) * sizeof(char *));
3322 *ap++ = cmd = "/bin/sh";
3323 while ((*ap++ = *argv++));
3329 static char *commandtext(const union node *);
3332 * Do a path search. The variable path (passed by reference) should be
3333 * set to the start of the path before the first call; padvance will update
3334 * this value as it proceeds. Successive calls to padvance will return
3335 * the possible path expansions in sequence. If an option (indicated by
3336 * a percent sign) appears in the path entry then the global variable
3337 * pathopt will be set to point to it; otherwise pathopt will be set to
3341 static const char *pathopt;
3343 static void growstackblock(void);
3346 static char *padvance(const char **path, const char *name)
3356 for (p = start; *p && *p != ':' && *p != '%'; p++);
3357 len = p - start + strlen(name) + 2; /* "2" is for '/' and '\0' */
3358 while (stackblocksize() < len)
3362 memcpy(q, start, p - start);
3370 while (*p && *p != ':')
3377 return stalloc(len);
3381 * Wrapper around strcmp for qsort/bsearch/...
3383 static int pstrcmp(const void *a, const void *b)
3385 return strcmp((const char *) a, (*(const char *const *) b) + 1);
3389 * Find a keyword is in a sorted array.
3392 static const char *const *findkwd(const char *s)
3394 return bsearch(s, tokname_array + KWDOFFSET,
3395 (sizeof(tokname_array) / sizeof(const char *)) - KWDOFFSET,
3396 sizeof(const char *), pstrcmp);
3400 /*** Command hashing code ***/
3402 static int hashcmd(int argc, char **argv)
3404 struct tblentry **pp;
3405 struct tblentry *cmdp;
3408 struct cmdentry entry;
3411 #ifdef CONFIG_ASH_ALIAS
3412 const struct alias *ap;
3416 while ((c = nextopt("rvV")) != '\0') {
3420 } else if (c == 'v' || c == 'V') {
3424 if (*argptr == NULL) {
3425 for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) {
3426 for (cmdp = *pp; cmdp; cmdp = cmdp->next) {
3427 if (cmdp->cmdtype != CMDBUILTIN) {
3428 printentry(cmdp, verbose);
3435 while ((name = *argptr++) != NULL) {
3436 if ((cmdp = cmdlookup(name, 0)) != NULL
3437 && (cmdp->cmdtype == CMDNORMAL
3438 || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0)))
3440 #ifdef CONFIG_ASH_ALIAS
3441 /* Then look at the aliases */
3442 if ((ap = *__lookupalias(name)) != NULL) {
3444 printf("%s is an alias for %s\n", name, ap->val);
3450 /* First look at the keywords */
3451 if (findkwd(name) != 0) {
3453 printf("%s is a shell keyword\n", name);
3459 find_command(name, &entry, DO_ERR, pathval());
3460 if (entry.cmdtype == CMDUNKNOWN)
3463 cmdp = cmdlookup(name, 0);
3465 printentry(cmdp, verbose == 'v');
3472 static void printentry(struct tblentry *cmdp, int verbose)
3478 printf("%s%s", cmdp->cmdname, (verbose ? " is " : ""));
3479 if (cmdp->cmdtype == CMDNORMAL) {
3480 idx = cmdp->param.index;
3483 name = padvance(&path, cmdp->cmdname);
3485 } while (--idx >= 0);
3488 } else if (cmdp->cmdtype == CMDBUILTIN) {
3490 out1str("a shell builtin");
3491 } else if (cmdp->cmdtype == CMDFUNCTION) {
3494 out1str("a function\n");
3495 name = commandtext(cmdp->param.func);
3496 printf("%s() {\n %s\n}", cmdp->cmdname, name);
3502 error("internal error: cmdtype %d", cmdp->cmdtype);
3505 puts(cmdp->rehash ? "*" : nullstr);
3510 /*** List the available builtins ***/
3513 static int helpcmd(int argc, char **argv)
3517 printf("\nBuilt-in commands:\n-------------------\n");
3518 for (col = 0, i = 0; i < NUMBUILTINS; i++) {
3519 col += printf("%c%s", ((col == 0) ? '\t' : ' '),
3520 builtincmds[i].name + 1);
3526 #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
3528 extern const struct BB_applet applets[];
3529 extern const size_t NUM_APPLETS;
3531 for (i = 0; i < NUM_APPLETS; i++) {
3533 col += printf("%c%s", ((col == 0) ? '\t' : ' '), applets[i].name);
3542 return EXIT_SUCCESS;
3546 * Resolve a command name. If you change this routine, you may have to
3547 * change the shellexec routine as well.
3550 static int prefix(const char *, const char *);
3553 find_command(const char *name, struct cmdentry *entry, int act,
3556 struct tblentry *cmdp;
3566 struct builtincmd *bcmd;
3568 /* If name contains a slash, don't use the hash table */
3569 if (strchr(name, '/') != NULL) {
3571 while (stat(name, &statb) < 0) {
3572 if (errno != ENOENT && errno != ENOTDIR)
3574 entry->cmdtype = CMDUNKNOWN;
3575 entry->u.index = -1;
3578 entry->cmdtype = CMDNORMAL;
3579 entry->u.index = -1;
3582 entry->cmdtype = CMDNORMAL;
3587 #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
3588 if (find_applet_by_name(name)) {
3589 entry->cmdtype = CMDNORMAL;
3590 entry->u.index = -1;
3596 if (act & DO_BRUTE) {
3597 firstchange = path_change(path, &bltin);
3603 /* If name is in the table, and not invalidated by cd, we're done */
3604 if ((cmdp = cmdlookup(name, 0)) != NULL && cmdp->rehash == 0) {
3605 if (cmdp->cmdtype == CMDFUNCTION) {
3606 if (act & DO_NOFUN) {
3611 } else if (act & DO_BRUTE) {
3612 if ((cmdp->cmdtype == CMDNORMAL &&
3613 cmdp->param.index >= firstchange) ||
3614 (cmdp->cmdtype == CMDBUILTIN &&
3615 ((builtinloc < 0 && bltin >= 0) ?
3616 bltin : builtinloc) >= firstchange)) {
3617 /* need to recompute the entry */
3626 bcmd = find_builtin(name);
3627 regular = bcmd && IS_BUILTIN_REGULAR(bcmd);
3630 if (cmdp && (cmdp->cmdtype == CMDBUILTIN)) {
3633 } else if (act & DO_BRUTE) {
3634 if (firstchange == 0) {
3639 /* If %builtin not in path, check for builtin next */
3640 if (regular || (bltin < 0 && bcmd)) {
3643 entry->cmdtype = CMDBUILTIN;
3644 entry->u.cmd = bcmd;
3648 cmdp = cmdlookup(name, 1);
3649 cmdp->cmdtype = CMDBUILTIN;
3650 cmdp->param.cmd = bcmd;
3655 /* We have to search path. */
3656 prev = -1; /* where to start */
3657 if (cmdp && cmdp->rehash) { /* doing a rehash */
3658 if (cmdp->cmdtype == CMDBUILTIN)
3661 prev = cmdp->param.index;
3667 while ((fullname = padvance(&path, name)) != NULL) {
3668 stunalloc(fullname);
3670 if (idx >= firstchange) {
3674 if (prefix("builtin", pathopt)) {
3675 if ((bcmd = find_builtin(name))) {
3679 } else if (!(act & DO_NOFUN) && prefix("func", pathopt)) {
3682 continue; /* ignore unimplemented options */
3685 /* if rehash, don't redo absolute path names */
3686 if (fullname[0] == '/' && idx <= prev && idx < firstchange) {
3689 TRACE(("searchexec \"%s\": no change\n", name));
3692 while (stat(fullname, &statb) < 0) {
3693 if (errno != ENOENT && errno != ENOTDIR)
3697 e = EACCES; /* if we fail, this will be the error */
3698 if (!S_ISREG(statb.st_mode))
3700 if (pathopt) { /* this is a %func directory */
3701 stalloc(strlen(fullname) + 1);
3702 readcmdfile(fullname);
3703 if ((cmdp = cmdlookup(name, 0)) == NULL
3704 || cmdp->cmdtype != CMDFUNCTION)
3705 error("%s not defined in %s", name, fullname);
3706 stunalloc(fullname);
3709 TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname));
3710 /* If we aren't called with DO_BRUTE and cmdp is set, it must
3711 be a function and we're being called with DO_NOFUN */
3713 entry->cmdtype = CMDNORMAL;
3714 entry->u.index = idx;
3718 cmdp = cmdlookup(name, 1);
3719 cmdp->cmdtype = CMDNORMAL;
3720 cmdp->param.index = idx;
3725 /* We failed. If there was an entry for this command, delete it */
3726 if (cmdp && updatetbl)
3729 out2fmt("%s: %s\n", name, errmsg(e, E_EXEC));
3730 entry->cmdtype = CMDUNKNOWN;
3735 entry->cmdtype = cmdp->cmdtype;
3736 entry->u = cmdp->param;
3742 * Search the table of builtin commands.
3745 static struct builtincmd *find_builtin(const char *name)
3747 struct builtincmd *bp;
3749 bp = bsearch(name, builtincmds, NUMBUILTINS, sizeof(struct builtincmd),
3756 * Called when a cd is done. Marks all commands so the next time they
3757 * are executed they will be rehashed.
3760 static void hashcd(void)
3762 struct tblentry **pp;
3763 struct tblentry *cmdp;
3765 for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) {
3766 for (cmdp = *pp; cmdp; cmdp = cmdp->next) {
3767 if (cmdp->cmdtype == CMDNORMAL
3768 || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0))
3777 * Called before PATH is changed. The argument is the new value of PATH;
3778 * pathval() still returns the old value at this point. Called with
3782 static void changepath(const char *newval)
3787 firstchange = path_change(newval, &bltin);
3788 if (builtinloc < 0 && bltin >= 0)
3789 builtinloc = bltin; /* zap builtins */
3790 clearcmdentry(firstchange);
3792 /* Ensure that getenv("PATH") stays current */
3793 setenv("PATH", newval, 1);
3798 * Clear out command entries. The argument specifies the first entry in
3799 * PATH which has changed.
3802 static void clearcmdentry(int firstchange)
3804 struct tblentry **tblp;
3805 struct tblentry **pp;
3806 struct tblentry *cmdp;
3809 for (tblp = cmdtable; tblp < &cmdtable[CMDTABLESIZE]; tblp++) {
3811 while ((cmdp = *pp) != NULL) {
3812 if ((cmdp->cmdtype == CMDNORMAL &&
3813 cmdp->param.index >= firstchange)
3814 || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= firstchange)) {
3827 * Locate a command in the command hash table. If "add" is nonzero,
3828 * add the command to the table if it is not already present. The
3829 * variable "lastcmdentry" is set to point to the address of the link
3830 * pointing to the entry, so that delete_cmd_entry can delete the
3834 static struct tblentry **lastcmdentry;
3836 static struct tblentry *cmdlookup(const char *name, int add)
3840 struct tblentry *cmdp;
3841 struct tblentry **pp;
3848 pp = &cmdtable[hashval % CMDTABLESIZE];
3849 for (cmdp = *pp; cmdp; cmdp = cmdp->next) {
3850 if (equal(cmdp->cmdname, name))
3854 if (add && cmdp == NULL) {
3856 cmdp = *pp = xmalloc(sizeof(struct tblentry) - ARB
3857 + strlen(name) + 1);
3859 cmdp->cmdtype = CMDUNKNOWN;
3861 strcpy(cmdp->cmdname, name);
3869 * Delete the command entry returned on the last lookup.
3872 static void delete_cmd_entry(void)
3874 struct tblentry *cmdp;
3877 cmdp = *lastcmdentry;
3878 *lastcmdentry = cmdp->next;
3887 static const unsigned char nodesize[26] = {
3888 ALIGN(sizeof(struct nbinary)),
3889 ALIGN(sizeof(struct ncmd)),
3890 ALIGN(sizeof(struct npipe)),
3891 ALIGN(sizeof(struct nredir)),
3892 ALIGN(sizeof(struct nredir)),
3893 ALIGN(sizeof(struct nredir)),
3894 ALIGN(sizeof(struct nbinary)),
3895 ALIGN(sizeof(struct nbinary)),
3896 ALIGN(sizeof(struct nif)),
3897 ALIGN(sizeof(struct nbinary)),
3898 ALIGN(sizeof(struct nbinary)),
3899 ALIGN(sizeof(struct nfor)),
3900 ALIGN(sizeof(struct ncase)),
3901 ALIGN(sizeof(struct nclist)),
3902 ALIGN(sizeof(struct narg)),
3903 ALIGN(sizeof(struct narg)),
3904 ALIGN(sizeof(struct nfile)),
3905 ALIGN(sizeof(struct nfile)),
3906 ALIGN(sizeof(struct nfile)),
3907 ALIGN(sizeof(struct nfile)),
3908 ALIGN(sizeof(struct nfile)),
3909 ALIGN(sizeof(struct ndup)),
3910 ALIGN(sizeof(struct ndup)),
3911 ALIGN(sizeof(struct nhere)),
3912 ALIGN(sizeof(struct nhere)),
3913 ALIGN(sizeof(struct nnot)),
3919 * Delete a function if it exists.
3922 static void unsetfunc(char *name)
3924 struct tblentry *cmdp;
3926 if ((cmdp = cmdlookup(name, 0)) != NULL && cmdp->cmdtype == CMDFUNCTION) {
3927 free(cmdp->param.func);
3934 * Locate and print what a word is...
3937 static int typecmd(int argc, char **argv)
3945 for (i = 1; i < argc; i++) {
3946 argv_a[0] = argv[i];
3949 err |= hashcmd(2, argv);
3954 #ifdef CONFIG_ASH_CMDCMD
3955 static int commandcmd(int argc, char **argv)
3958 int default_path = 0;
3959 int verify_only = 0;
3960 int verbose_verify_only = 0;
3962 while ((c = nextopt("pvV")) != '\0')
3971 verbose_verify_only = 1;
3975 if (default_path + verify_only + verbose_verify_only > 1 || !*argptr) {
3976 out2str("command [-p] command [arg ...]\n"
3977 "command {-v|-V} command\n");
3981 if (verify_only || verbose_verify_only) {
3985 argv_a[0] = *argptr;
3987 optptr = verbose_verify_only ? "v" : "V"; /* reverse special */
3988 return hashcmd(argc, argv);
3995 static int path_change(const char *newval, int *bltin)
3997 const char *old, *new;
4003 firstchange = 9999; /* assume no change */
4009 if ((*old == '\0' && *new == ':')
4010 || (*old == ':' && *new == '\0'))
4012 old = new; /* ignore subsequent differences */
4016 if (*new == '%' && *bltin < 0 && prefix("builtin", new + 1))
4023 if (builtinloc >= 0 && *bltin < 0)
4029 * Routines to expand arguments to commands. We have to deal with
4030 * backquotes, shell variables, and file metacharacters.
4035 #define RMESCAPE_ALLOC 0x1 /* Allocate a new string */
4036 #define RMESCAPE_GLOB 0x2 /* Add backslashes for glob */
4039 * Structure specifying which parts of the string should be searched
4040 * for IFS characters.
4044 struct ifsregion *next; /* next region in list */
4045 int begoff; /* offset of start of region */
4046 int endoff; /* offset of end of region */
4047 int nulonly; /* search for nul bytes only */
4051 static char *expdest; /* output of current string */
4052 static struct nodelist *argbackq; /* list of back quote expressions */
4053 static struct ifsregion ifsfirst; /* first struct in list of ifs regions */
4054 static struct ifsregion *ifslastp; /* last struct in list */
4055 static struct arglist exparg; /* holds expanded arg list */
4057 static void argstr(char *, int);
4058 static char *exptilde(char *, int);
4059 static void expbackq(union node *, int, int);
4060 static int subevalvar(char *, char *, int, int, int, int, int);
4061 static int varisset(char *, int);
4062 static void strtodest(const char *, int, int);
4063 static void varvalue(char *, int, int);
4064 static void recordregion(int, int, int);
4065 static void removerecordregions(int);
4066 static void ifsbreakup(char *, struct arglist *);
4067 static void ifsfree(void);
4068 static void expandmeta(struct strlist *, int);
4070 #if defined(__GLIBC__) && __GLIBC__ >= 2 && !defined(FNMATCH_BROKEN)
4071 #define preglob(p) _rmescapes((p), RMESCAPE_ALLOC | RMESCAPE_GLOB)
4072 #if !defined(GLOB_BROKEN)
4073 static void addglob(const glob_t *);
4076 #if !(defined(__GLIBC__) && __GLIBC__ >= 2 && !defined(FNMATCH_BROKEN) && !defined(GLOB_BROKEN))
4077 static void expmeta(char *, char *);
4079 #if !(defined(__GLIBC__) && __GLIBC__ >= 2 && !defined(FNMATCH_BROKEN) && !defined(GLOB_BROKEN))
4080 static struct strlist *expsort(struct strlist *);
4081 static struct strlist *msort(struct strlist *, int);
4083 static int patmatch(char *, char *, int);
4085 #if defined(__GLIBC__) && __GLIBC__ >= 2 && !defined(FNMATCH_BROKEN)
4086 static int patmatch2(char *, char *, int);
4088 static int pmatch(char *, char *, int);
4090 #define patmatch2 patmatch
4092 static char *cvtnum(int, char *);
4095 * Expand shell variables and backquotes inside a here document.
4098 /* arg: the document, fd: where to write the expanded version */
4099 static inline void expandhere(union node *arg, int fd)
4102 expandarg(arg, (struct arglist *) NULL, 0);
4103 xwrite(fd, stackblock(), expdest - stackblock());
4108 * Perform variable substitution and command substitution on an argument,
4109 * placing the resulting list of arguments in arglist. If EXP_FULL is true,
4110 * perform splitting and file name expansion. When arglist is NULL, perform
4111 * here document expansion.
4114 static void expandarg(union node *arg, struct arglist *arglist, int flag)
4119 argbackq = arg->narg.backquote;
4120 STARTSTACKSTR(expdest);
4121 ifsfirst.next = NULL;
4123 argstr(arg->narg.text, flag);
4124 if (arglist == NULL) {
4125 return; /* here document expanded */
4127 STPUTC('\0', expdest);
4128 p = grabstackstr(expdest);
4129 exparg.lastp = &exparg.list;
4133 if (flag & EXP_FULL) {
4134 ifsbreakup(p, &exparg);
4135 *exparg.lastp = NULL;
4136 exparg.lastp = &exparg.list;
4137 expandmeta(exparg.list, flag);
4139 if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */
4141 sp = (struct strlist *) stalloc(sizeof(struct strlist));
4144 exparg.lastp = &sp->next;
4147 *exparg.lastp = NULL;
4149 *arglist->lastp = exparg.list;
4150 arglist->lastp = exparg.lastp;
4156 * Expand a variable, and return a pointer to the next character in the
4160 static inline char *evalvar(char *p, int flag)
4173 int quotes = flag & (EXP_FULL | EXP_CASE);
4176 subtype = varflags & VSTYPE;
4181 p = strchr(p, '=') + 1;
4182 again: /* jump here after setting a variable with ${var=text} */
4184 set = varisset(var, varflags & VSNUL);
4187 val = lookupvar(var);
4188 if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) {
4195 startloc = expdest - stackblock();
4196 if (set && subtype != VSPLUS) {
4197 /* insert the value of the variable */
4199 varvalue(var, varflags & VSQUOTE, flag);
4200 if (subtype == VSLENGTH) {
4201 varlen = expdest - stackblock() - startloc;
4202 STADJUST(-varlen, expdest);
4205 if (subtype == VSLENGTH) {
4206 varlen = strlen(val);
4209 varflags & VSQUOTE ? DQSYNTAX : BASESYNTAX, quotes);
4214 if (subtype == VSPLUS)
4217 easy = ((varflags & VSQUOTE) == 0 ||
4218 (*var == '@' && shellparam.nparam != 1));
4223 expdest = cvtnum(varlen, expdest);
4230 recordregion(startloc, expdest - stackblock(), varflags & VSQUOTE);
4246 case VSTRIMRIGHTMAX:
4250 * Terminate the string and start recording the pattern
4253 STPUTC('\0', expdest);
4254 patloc = expdest - stackblock();
4255 if (subevalvar(p, NULL, patloc, subtype,
4256 startloc, varflags, quotes) == 0) {
4257 int amount = (expdest - stackblock() - patloc) + 1;
4259 STADJUST(-amount, expdest);
4261 /* Remove any recorded regions beyond start of variable */
4262 removerecordregions(startloc);
4268 if (subevalvar(p, var, 0, subtype, startloc, varflags, quotes)) {
4271 * Remove any recorded regions beyond
4274 removerecordregions(startloc);
4289 if (subtype != VSNORMAL) { /* skip to end of alternative */
4293 if ((c = *p++) == CTLESC)
4295 else if (c == CTLBACKQ || c == (CTLBACKQ | CTLQUOTE)) {
4297 argbackq = argbackq->next;
4298 } else if (c == CTLVAR) {
4299 if ((*p++ & VSTYPE) != VSNORMAL)
4301 } else if (c == CTLENDVAR) {
4312 * Perform variable and command substitution. If EXP_FULL is set, output CTLESC
4313 * characters to allow for further processing. Otherwise treat
4314 * $@ like $* since no splitting will be performed.
4317 static void argstr(char *p, int flag)
4320 int quotes = flag & (EXP_FULL | EXP_CASE); /* do CTLESC */
4323 if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE)))
4324 p = exptilde(p, flag);
4328 case CTLENDVAR: /* ??? */
4331 /* "$@" syntax adherence hack */
4332 if (p[0] == CTLVAR && p[2] == '@' && p[3] == '=')
4334 if ((flag & EXP_FULL) != 0)
4344 p = evalvar(p, flag);
4347 case CTLBACKQ | CTLQUOTE:
4348 expbackq(argbackq->n, c & CTLQUOTE, flag);
4349 argbackq = argbackq->next;
4351 #ifdef CONFIG_ASH_MATH_SUPPORT
4359 * sort of a hack - expand tildes in variable
4360 * assignments (after the first '=' and after ':'s).
4363 if (flag & EXP_VARTILDE && *p == '~') {
4370 p = exptilde(p, flag);
4379 static char *exptilde(char *p, int flag)
4381 char c, *startp = p;
4384 int quotes = flag & (EXP_FULL | EXP_CASE);
4386 while ((c = *p) != '\0') {
4393 if (flag & EXP_VARTILDE)
4403 if (*(startp + 1) == '\0') {
4404 if ((home = lookupvar("HOME")) == NULL)
4407 if ((pw = getpwnam(startp + 1)) == NULL)
4414 strtodest(home, SQSYNTAX, quotes);
4422 static void removerecordregions(int endoff)
4424 if (ifslastp == NULL)
4427 if (ifsfirst.endoff > endoff) {
4428 while (ifsfirst.next != NULL) {
4429 struct ifsregion *ifsp;
4432 ifsp = ifsfirst.next->next;
4433 free(ifsfirst.next);
4434 ifsfirst.next = ifsp;
4437 if (ifsfirst.begoff > endoff)
4440 ifslastp = &ifsfirst;
4441 ifsfirst.endoff = endoff;
4446 ifslastp = &ifsfirst;
4447 while (ifslastp->next && ifslastp->next->begoff < endoff)
4448 ifslastp = ifslastp->next;
4449 while (ifslastp->next != NULL) {
4450 struct ifsregion *ifsp;
4453 ifsp = ifslastp->next->next;
4454 free(ifslastp->next);
4455 ifslastp->next = ifsp;
4458 if (ifslastp->endoff > endoff)
4459 ifslastp->endoff = endoff;
4463 #ifdef CONFIG_ASH_MATH_SUPPORT
4465 * Expand arithmetic expression. Backup to start of expression,
4466 * evaluate, place result in (backed up) result, adjust string position.
4468 static void expari(int flag)
4474 int quotes = flag & (EXP_FULL | EXP_CASE);
4480 * This routine is slightly over-complicated for
4481 * efficiency. First we make sure there is
4482 * enough space for the result, which may be bigger
4483 * than the expression if we add exponentation. Next we
4484 * scan backwards looking for the start of arithmetic. If the
4485 * next previous character is a CTLESC character, then we
4486 * have to rescan starting from the beginning since CTLESC
4487 * characters have to be processed left to right.
4489 CHECKSTRSPACE(10, expdest);
4490 USTPUTC('\0', expdest);
4491 start = stackblock();
4493 while (*p != CTLARI && p >= start)
4496 error("missing CTLARI (shouldn't happen)");
4497 if (p > start && *(p - 1) == CTLESC)
4498 for (p = start; *p != CTLARI; p++)
4507 removerecordregions(begoff);
4510 result = arith(p + 2, &errcode);
4513 error("divide by zero");
4515 error("syntax error: \"%s\"\n", p + 2);
4517 snprintf(p, 12, "%d", result);
4521 recordregion(begoff, p - 1 - start, 0);
4522 result = expdest - p + 1;
4523 STADJUST(-result, expdest);
4528 * Expand stuff in backwards quotes.
4531 static void expbackq(union node *cmd, int quoted, int flag)
4533 volatile struct backcmd in;
4537 char *dest = expdest;
4538 volatile struct ifsregion saveifs;
4539 struct ifsregion *volatile savelastp;
4540 struct nodelist *volatile saveargbackq;
4542 int startloc = dest - stackblock();
4543 int syntax = quoted ? DQSYNTAX : BASESYNTAX;
4544 volatile int saveherefd;
4545 int quotes = flag & (EXP_FULL | EXP_CASE);
4546 struct jmploc jmploc;
4547 struct jmploc *volatile savehandler;
4551 /* Avoid longjmp clobbering */
4562 savelastp = ifslastp;
4563 saveargbackq = argbackq;
4564 saveherefd = herefd;
4566 if ((ex = setjmp(jmploc.loc))) {
4569 savehandler = handler;
4572 p = grabstackstr(dest);
4573 evalbackcmd(cmd, (struct backcmd *) &in);
4574 ungrabstackstr(p, dest);
4578 ifslastp = savelastp;
4579 argbackq = saveargbackq;
4580 herefd = saveherefd;
4588 if (--in.nleft < 0) {
4591 i = safe_read(in.fd, buf, sizeof buf);
4592 TRACE(("expbackq: read returns %d\n", i));
4599 if (lastc != '\0') {
4600 if (quotes && SIT(lastc, syntax) == CCTL)
4601 STPUTC(CTLESC, dest);
4602 STPUTC(lastc, dest);
4606 /* Eat all trailing newlines */
4607 for (; dest > stackblock() && dest[-1] == '\n';)
4615 exitstatus = waitforjob(in.jp);
4616 handler = savehandler;
4618 longjmp(handler->loc, 1);
4621 recordregion(startloc, dest - stackblock(), 0);
4622 TRACE(("evalbackq: size=%d: \"%.*s\"\n",
4623 (dest - stackblock()) - startloc,
4624 (dest - stackblock()) - startloc, stackblock() + startloc));
4630 subevalvar(char *p, char *str, int strloc, int subtype, int startloc,
4631 int varflags, int quotes)
4637 int saveherefd = herefd;
4638 struct nodelist *saveargbackq = argbackq;
4642 argstr(p, subtype != VSASSIGN && subtype != VSQUESTION ? EXP_CASE : 0);
4643 STACKSTRNUL(expdest);
4644 herefd = saveherefd;
4645 argbackq = saveargbackq;
4646 startp = stackblock() + startloc;
4648 str = stackblock() + strloc;
4652 setvar(str, startp, 0);
4653 amount = startp - expdest;
4654 STADJUST(amount, expdest);
4661 if (*p != CTLENDVAR) {
4662 out2fmt(snlfmt, startp);
4663 error((char *) NULL);
4665 error("%.*s: parameter %snot set", p - str - 1,
4666 str, (varflags & VSNUL) ? "null or " : nullstr);
4670 for (loc = startp; loc < str; loc++) {
4673 if (patmatch2(str, startp, quotes))
4676 if (quotes && *loc == CTLESC)
4682 for (loc = str - 1; loc >= startp;) {
4685 if (patmatch2(str, startp, quotes))
4689 if (quotes && loc > startp && *(loc - 1) == CTLESC) {
4690 for (q = startp; q < loc; q++)
4700 for (loc = str - 1; loc >= startp;) {
4701 if (patmatch2(str, loc, quotes))
4704 if (quotes && loc > startp && *(loc - 1) == CTLESC) {
4705 for (q = startp; q < loc; q++)
4714 case VSTRIMRIGHTMAX:
4715 for (loc = startp; loc < str - 1; loc++) {
4716 if (patmatch2(str, loc, quotes))
4718 if (quotes && *loc == CTLESC)
4731 amount = ((str - 1) - (loc - startp)) - expdest;
4732 STADJUST(amount, expdest);
4733 while (loc != str - 1)
4738 amount = loc - expdest;
4739 STADJUST(amount, expdest);
4740 STPUTC('\0', expdest);
4741 STADJUST(-1, expdest);
4747 * Test whether a specialized variable is set.
4750 static int varisset(char *name, int nulok)
4753 return backgndpid != -1;
4754 else if (*name == '@' || *name == '*') {
4755 if (*shellparam.p == NULL)
4761 for (av = shellparam.p; *av; av++)
4766 } else if (is_digit(*name)) {
4768 int num = atoi(name);
4770 if (num > shellparam.nparam)
4776 ap = shellparam.p[num - 1];
4778 if (nulok && (ap == NULL || *ap == '\0'))
4785 * Put a string on the stack.
4788 static void strtodest(const char *p, int syntax, int quotes)
4791 if (quotes && SIT(*p, syntax) == CCTL)
4792 STPUTC(CTLESC, expdest);
4793 STPUTC(*p++, expdest);
4798 * Add the value of a specialized variable to the stack string.
4801 static void varvalue(char *name, int quoted, int flags)
4810 int allow_split = flags & EXP_FULL;
4811 int quotes = flags & (EXP_FULL | EXP_CASE);
4813 syntax = quoted ? DQSYNTAX : BASESYNTAX;
4822 num = shellparam.nparam;
4827 expdest = cvtnum(num, expdest);
4830 for (i = 0; i < NOPTS; i++) {
4832 STPUTC(optent_letter(optlist[i]), expdest);
4836 if (allow_split && quoted) {
4837 sep = 1 << CHAR_BIT;
4842 sep = ifsset()? ifsval()[0] : ' ';
4844 sepq = SIT(sep, syntax) == CCTL;
4847 for (ap = shellparam.p; (p = *ap++) != NULL;) {
4848 strtodest(p, syntax, quotes);
4851 STPUTC(CTLESC, expdest);
4852 STPUTC(sep, expdest);
4857 strtodest(arg0, syntax, quotes);
4861 if (num > 0 && num <= shellparam.nparam) {
4862 strtodest(shellparam.p[num - 1], syntax, quotes);
4870 * Record the fact that we have to scan this region of the
4871 * string for IFS characters.
4874 static void recordregion(int start, int end, int nulonly)
4876 struct ifsregion *ifsp;
4878 if (ifslastp == NULL) {
4882 ifsp = (struct ifsregion *) xmalloc(sizeof(struct ifsregion));
4884 ifslastp->next = ifsp;
4888 ifslastp->begoff = start;
4889 ifslastp->endoff = end;
4890 ifslastp->nulonly = nulonly;
4896 * Break the argument string into pieces based upon IFS and add the
4897 * strings to the argument list. The regions of the string to be
4898 * searched for IFS characters have been stored by recordregion.
4900 static void ifsbreakup(char *string, struct arglist *arglist)
4902 struct ifsregion *ifsp;
4907 const char *ifs, *realifs;
4915 realifs = ifsset()? ifsval() : defifs;
4916 if (ifslastp != NULL) {
4919 p = string + ifsp->begoff;
4920 nulonly = ifsp->nulonly;
4921 ifs = nulonly ? nullstr : realifs;
4923 while (p < string + ifsp->endoff) {
4927 if (strchr(ifs, *p)) {
4929 ifsspc = (strchr(defifs, *p) != NULL);
4930 /* Ignore IFS whitespace at start */
4931 if (q == start && ifsspc) {
4937 sp = (struct strlist *) stalloc(sizeof *sp);
4939 *arglist->lastp = sp;
4940 arglist->lastp = &sp->next;
4944 if (p >= string + ifsp->endoff) {
4950 if (strchr(ifs, *p) == NULL) {
4953 } else if (strchr(defifs, *p) == NULL) {
4969 } while ((ifsp = ifsp->next) != NULL);
4970 if (!(*start || (!ifsspc && start > string && nulonly))) {
4975 sp = (struct strlist *) stalloc(sizeof *sp);
4977 *arglist->lastp = sp;
4978 arglist->lastp = &sp->next;
4981 static void ifsfree(void)
4983 while (ifsfirst.next != NULL) {
4984 struct ifsregion *ifsp;
4987 ifsp = ifsfirst.next->next;
4988 free(ifsfirst.next);
4989 ifsfirst.next = ifsp;
4993 ifsfirst.next = NULL;
4997 * Add a file name to the list.
5000 static void addfname(const char *name)
5003 size_t len = strlen(name) + 1;
5005 sp = (struct strlist *) stalloc(sizeof *sp);
5006 sp->text = memcpy(stalloc(len), name, len);
5008 exparg.lastp = &sp->next;
5012 * Expand shell metacharacters. At this point, the only control characters
5013 * should be escapes. The results are stored in the list exparg.
5016 #if defined(__GLIBC__) && __GLIBC__ >= 2 && !defined(FNMATCH_BROKEN) && !defined(GLOB_BROKEN)
5017 static void expandmeta(struct strlist *str, int flag)
5022 /* TODO - EXP_REDIR */
5027 p = preglob(str->text);
5029 switch (glob(p, 0, 0, &pglob)) {
5031 if (pglob.gl_pathv[1] == 0 && !strcmp(p, pglob.gl_pathv[0]))
5042 *exparg.lastp = str;
5043 rmescapes(str->text);
5044 exparg.lastp = &str->next;
5046 default: /* GLOB_NOSPACE */
5047 error("Out of space");
5055 * Add the result of glob(3) to the list.
5058 static void addglob(const glob_t * pglob)
5060 char **p = pglob->gl_pathv;
5068 #else /* defined(__GLIBC__) && !defined(FNMATCH_BROKEN) && !defined(GLOB_BROKEN) */
5069 static char *expdir;
5072 static void expandmeta(struct strlist *str, int flag)
5075 struct strlist **savelastp;
5079 /* TODO - EXP_REDIR */
5085 for (;;) { /* fast check for meta chars */
5086 if ((c = *p++) == '\0')
5088 if (c == '*' || c == '?' || c == '[' || c == '!')
5091 savelastp = exparg.lastp;
5093 if (expdir == NULL) {
5094 int i = strlen(str->text);
5096 expdir = xmalloc(i < 2048 ? 2048 : i); /* XXX */
5099 expmeta(expdir, str->text);
5103 if (exparg.lastp == savelastp) {
5108 *exparg.lastp = str;
5109 rmescapes(str->text);
5110 exparg.lastp = &str->next;
5112 *exparg.lastp = NULL;
5113 *savelastp = sp = expsort(*savelastp);
5114 while (sp->next != NULL)
5116 exparg.lastp = &sp->next;
5124 * Do metacharacter (i.e. *, ?, [...]) expansion.
5127 static void expmeta(char *enddir, char *name)
5143 for (p = name;; p++) {
5144 if (*p == '*' || *p == '?')
5146 else if (*p == '[') {
5151 while (*q == CTLQUOTEMARK)
5155 if (*q == '/' || *q == '\0')
5162 } else if (*p == '!' && p[1] == '!' && (p == name || p[-1] == '/')) {
5164 } else if (*p == '\0')
5166 else if (*p == CTLQUOTEMARK)
5168 else if (*p == CTLESC)
5176 if (metaflag == 0) { /* we've reached the end of the file name */
5177 if (enddir != expdir)
5179 for (p = name;; p++) {
5180 if (*p == CTLQUOTEMARK)
5188 if (metaflag == 0 || lstat(expdir, &statb) >= 0)
5193 if (start != name) {
5196 while (*p == CTLQUOTEMARK)
5203 if (enddir == expdir) {
5205 } else if (enddir == expdir + 1 && *expdir == '/') {
5211 if ((dirp = opendir(cp)) == NULL)
5213 if (enddir != expdir)
5215 if (*endname == 0) {
5223 while (*p == CTLQUOTEMARK)
5229 while (!int_pending() && (dp = readdir(dirp)) != NULL) {
5230 if (dp->d_name[0] == '.' && !matchdot)
5232 if (patmatch(start, dp->d_name, 0)) {
5234 strcpy(enddir, dp->d_name);
5237 for (p = enddir, cp = dp->d_name; (*p++ = *cp++) != '\0';)
5240 expmeta(p, endname);
5248 #endif /* defined(__GLIBC__) && !defined(FNMATCH_BROKEN) && !defined(GLOB_BROKEN) */
5252 #if !(defined(__GLIBC__) && __GLIBC__ >= 2 && !defined(FNMATCH_BROKEN) && !defined(GLOB_BROKEN))
5254 * Sort the results of file name expansion. It calculates the number of
5255 * strings to sort and then calls msort (short for merge sort) to do the
5259 static struct strlist *expsort(struct strlist *str)
5265 for (sp = str; sp; sp = sp->next)
5267 return msort(str, len);
5271 static struct strlist *msort(struct strlist *list, int len)
5273 struct strlist *p, *q = NULL;
5274 struct strlist **lpp;
5282 for (n = half; --n >= 0;) {
5286 q->next = NULL; /* terminate first half of list */
5287 q = msort(list, half); /* sort first half of list */
5288 p = msort(p, len - half); /* sort second half */
5291 if (strcmp(p->text, q->text) < 0) {
5294 if ((p = *lpp) == NULL) {
5301 if ((q = *lpp) == NULL) {
5314 * Returns true if the pattern matches the string.
5317 #if defined(__GLIBC__) && __GLIBC__ >= 2 && !defined(FNMATCH_BROKEN)
5318 /* squoted: string might have quote chars */
5319 static int patmatch(char *pattern, char *string, int squoted)
5324 p = preglob(pattern);
5325 q = squoted ? _rmescapes(string, RMESCAPE_ALLOC) : string;
5327 return !fnmatch(p, q, 0);
5331 static int patmatch2(char *pattern, char *string, int squoted)
5337 p = grabstackstr(expdest);
5338 res = patmatch(pattern, string, squoted);
5339 ungrabstackstr(p, expdest);
5343 static int patmatch(char *pattern, char *string, int squoted)
5345 return pmatch(pattern, string, squoted);
5349 static int pmatch(char *pattern, char *string, int squoted)
5361 if (squoted && *q == CTLESC)
5369 if (squoted && *q == CTLESC)
5376 while (c == CTLQUOTEMARK || c == '*')
5378 if (c != CTLESC && c != CTLQUOTEMARK &&
5379 c != '?' && c != '*' && c != '[') {
5381 if (squoted && *q == CTLESC && q[1] == c)
5385 if (squoted && *q == CTLESC)
5391 if (pmatch(p, q, squoted))
5393 if (squoted && *q == CTLESC)
5395 } while (*q++ != '\0');
5406 while (*endp == CTLQUOTEMARK)
5409 goto dft; /* no matching ] */
5410 if (*endp == CTLESC)
5422 if (squoted && chr == CTLESC)
5428 if (c == CTLQUOTEMARK)
5432 if (*p == '-' && p[1] != ']') {
5434 while (*p == CTLQUOTEMARK)
5438 if (chr >= c && chr <= *p)
5445 } while ((c = *p++) != ']');
5446 if (found == invert)
5451 if (squoted && *q == CTLESC)
5468 * Remove any CTLESC characters from a string.
5471 #if defined(__GLIBC__) && __GLIBC__ >= 2 && !defined(FNMATCH_BROKEN)
5472 static char *_rmescapes(char *str, int flag)
5475 static const char qchars[] = { CTLESC, CTLQUOTEMARK, 0 };
5477 p = strpbrk(str, qchars);
5483 if (flag & RMESCAPE_ALLOC) {
5484 size_t len = p - str;
5486 q = r = stalloc(strlen(p) + len + 1);
5488 memcpy(q, str, len);
5493 if (*p == CTLQUOTEMARK) {
5499 if (flag & RMESCAPE_GLOB && *p != '/') {
5509 static void rmescapes(char *str)
5514 while (*p != CTLESC && *p != CTLQUOTEMARK) {
5520 if (*p == CTLQUOTEMARK) {
5535 * See if a pattern matches in a case statement.
5538 static int casematch(union node *pattern, const char *val)
5540 struct stackmark smark;
5544 setstackmark(&smark);
5545 argbackq = pattern->narg.backquote;
5546 STARTSTACKSTR(expdest);
5548 argstr(pattern->narg.text, EXP_TILDE | EXP_CASE);
5549 STPUTC('\0', expdest);
5550 p = grabstackstr(expdest);
5551 result = patmatch(p, (char *) val, 0);
5552 popstackmark(&smark);
5560 static char *cvtnum(int num, char *buf)
5564 CHECKSTRSPACE(32, buf);
5565 len = sprintf(buf, "%d", num);
5571 * Editline and history functions (and glue).
5573 static int histcmd(int argc, char **argv)
5575 error("not compiled with history support");
5581 struct redirtab *next;
5582 short renamed[10]; /* Current ash support only 0-9 descriptors */
5583 /* char on arm (and others) can't be negative */
5586 static struct redirtab *redirlist;
5588 extern char **environ;
5593 * Initialization code.
5596 static void init(void)
5607 basepf.nextc = basepf.buf = basebuf;
5616 for (envp = environ; *envp; envp++) {
5617 if (strchr(*envp, '=')) {
5618 setvareq(*envp, VEXPORT | VTEXTFIXED);
5622 snprintf(ppid, sizeof(ppid), "%d", (int) getppid());
5623 setvar("PPID", ppid, 0);
5630 * This routine is called when an error or an interrupt occurs in an
5631 * interactive shell and control is returned to the main command loop.
5634 /* 1 == check for aliases, 2 == also check for assignments */
5635 static int checkalias; /* also used in no alias mode for check assignments */
5637 static void reset(void)
5649 parselleft = parsenleft = 0; /* clear input buffer */
5653 /* from parser.c: */
5671 * This file implements the input routines used by the parser.
5674 #ifdef CONFIG_FEATURE_COMMAND_EDITING
5675 static const char *cmdedit_prompt;
5676 static inline void putprompt(const char *s)
5681 static inline void putprompt(const char *s)
5687 #define EOF_NLEFT -99 /* value of parsenleft when EOF pushed back */
5692 * Same as pgetc(), but ignores PEOA.
5695 #ifdef CONFIG_ASH_ALIAS
5696 static int pgetc2(void)
5702 } while (c == PEOA);
5706 static inline int pgetc2(void)
5708 return pgetc_macro();
5713 * Read a line from the script.
5716 static inline char *pfgets(char *line, int len)
5722 while (--nleft > 0) {
5737 static inline int preadfd(void)
5740 char *buf = parsefile->buf;
5745 #ifdef CONFIG_FEATURE_COMMAND_EDITING
5747 if (!iflag || parsefile->fd)
5748 nr = safe_read(parsefile->fd, buf, BUFSIZ - 1);
5750 nr = cmdedit_read_input((char *) cmdedit_prompt, buf);
5754 nr = safe_read(parsefile->fd, buf, BUFSIZ - 1);
5758 if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
5759 int flags = fcntl(0, F_GETFL, 0);
5761 if (flags >= 0 && flags & O_NONBLOCK) {
5762 flags &= ~O_NONBLOCK;
5763 if (fcntl(0, F_SETFL, flags) >= 0) {
5764 out2str("sh: turning off NDELAY mode\n");
5773 static void popstring(void)
5775 struct strpush *sp = parsefile->strpush;
5778 #ifdef CONFIG_ASH_ALIAS
5780 if (parsenextc[-1] == ' ' || parsenextc[-1] == '\t') {
5785 if (sp->string != sp->ap->val) {
5789 sp->ap->flag &= ~ALIASINUSE;
5790 if (sp->ap->flag & ALIASDEAD) {
5791 unalias(sp->ap->name);
5795 parsenextc = sp->prevstring;
5796 parsenleft = sp->prevnleft;
5797 /*dprintf("*** calling popstring: restoring to '%s'\n", parsenextc);*/
5798 parsefile->strpush = sp->prev;
5799 if (sp != &(parsefile->basestrpush))
5806 * Refill the input buffer and return the next input character:
5808 * 1) If a string was pushed back on the input, pop it;
5809 * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading
5810 * from a string so we can't refill the buffer, return EOF.
5811 * 3) If the is more stuff in this buffer, use it else call read to fill it.
5812 * 4) Process input up to the next newline, deleting nul characters.
5815 static int preadbuffer(void)
5821 while (parsefile->strpush) {
5822 #ifdef CONFIG_ASH_ALIAS
5823 if (parsenleft == -1 && parsefile->strpush->ap &&
5824 parsenextc[-1] != ' ' && parsenextc[-1] != '\t') {
5829 if (--parsenleft >= 0)
5830 return (*parsenextc++);
5832 if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
5837 if (parselleft <= 0) {
5838 if ((parselleft = preadfd()) <= 0) {
5839 parselleft = parsenleft = EOF_NLEFT;
5846 /* delete nul characters */
5847 for (more = 1; more;) {
5855 parsenleft = q - parsenextc;
5856 more = 0; /* Stop processing here */
5862 if (--parselleft <= 0 && more) {
5863 parsenleft = q - parsenextc - 1;
5874 out2str(parsenextc);
5879 return *parsenextc++;
5884 * Push a string back onto the input at this current parsefile level.
5885 * We handle aliases this way.
5887 static void pushstring(char *s, int len, void *ap)
5892 /*dprintf("*** calling pushstring: %s, %d\n", s, len);*/
5893 if (parsefile->strpush) {
5894 sp = xmalloc(sizeof(struct strpush));
5895 sp->prev = parsefile->strpush;
5896 parsefile->strpush = sp;
5898 sp = parsefile->strpush = &(parsefile->basestrpush);
5899 sp->prevstring = parsenextc;
5900 sp->prevnleft = parsenleft;
5901 #ifdef CONFIG_ASH_ALIAS
5902 sp->ap = (struct alias *) ap;
5904 ((struct alias *) ap)->flag |= ALIASINUSE;
5915 * Like setinputfile, but takes input from a string.
5918 static void setinputstring(char *string)
5922 parsenextc = string;
5923 parsenleft = strlen(string);
5924 parsefile->buf = NULL;
5932 * To handle the "." command, a stack of input files is used. Pushfile
5933 * adds a new entry to the stack and popfile restores the previous level.
5936 static void pushfile(void)
5938 struct parsefile *pf;
5940 parsefile->nleft = parsenleft;
5941 parsefile->lleft = parselleft;
5942 parsefile->nextc = parsenextc;
5943 parsefile->linno = plinno;
5944 pf = (struct parsefile *) xmalloc(sizeof(struct parsefile));
5945 pf->prev = parsefile;
5948 pf->basestrpush.prev = NULL;
5952 #ifdef CONFIG_ASH_JOB_CONTROL
5953 static void restartjob(struct job *);
5955 static void freejob(struct job *);
5956 static struct job *getjob(const char *);
5957 static int dowait(int, struct job *);
5961 * We keep track of whether or not fd0 has been redirected. This is for
5962 * background commands, where we want to redirect fd0 to /dev/null only
5963 * if it hasn't already been redirected.
5965 static int fd0_redirected = 0;
5967 /* Return true if fd 0 has already been redirected at least once. */
5968 static inline int fd0_redirected_p(void)
5970 return fd0_redirected != 0;
5973 static void dupredirect(const union node *, int, int fd1dup);
5975 #ifdef CONFIG_ASH_JOB_CONTROL
5977 * Turn job control on and off.
5979 * Note: This code assumes that the third arg to ioctl is a character
5980 * pointer, which is true on Berkeley systems but not System V. Since
5981 * System V doesn't have job control yet, this isn't a problem now.
5986 static void setjobctl(int enable)
5988 if (enable == jobctl || rootshell == 0)
5991 do { /* while we are in the background */
5992 initialpgrp = tcgetpgrp(2);
5993 if (initialpgrp < 0) {
5994 out2str("sh: can't access tty; job control turned off\n");
5998 if (initialpgrp == getpgrp())
6005 setpgid(0, rootpid);
6006 tcsetpgrp(2, rootpid);
6007 } else { /* turning job control off */
6008 setpgid(0, initialpgrp);
6009 tcsetpgrp(2, initialpgrp);
6019 #ifdef CONFIG_ASH_JOB_CONTROL
6020 static int killcmd(int argc, char **argv)
6031 ("Usage: kill [-s sigspec | -signum | -sigspec] [pid | job]... or\n"
6032 "kill -l [exitstatus]");
6035 if (*argv[1] == '-') {
6036 signo = decode_signal(argv[1] + 1, 1);
6040 while ((c = nextopt("ls:")) != '\0')
6046 signo = decode_signal(optionarg, 1);
6048 error("invalid signal number or name: %s", optionarg);
6053 error("nextopt returned character code 0%o", c);
6060 if (!list && signo < 0)
6063 if ((signo < 0 || !*argptr) ^ list) {
6072 for (i = 1; i < NSIG; i++) {
6073 name = u_signal_names(0, &i, 1);
6079 name = u_signal_names(*argptr, &signo, -1);
6083 error("invalid signal number or exit status: %s", *argptr);
6088 if (**argptr == '%') {
6089 jp = getjob(*argptr);
6090 if (jp->jobctl == 0)
6091 error("job %s not created under job control", *argptr);
6092 pid = -jp->ps[0].pid;
6094 pid = atoi(*argptr);
6095 if (kill(pid, signo) != 0)
6096 error("%s: %m", *argptr);
6097 } while (*++argptr);
6102 static int fgcmd(int argc, char **argv)
6108 jp = getjob(argv[1]);
6109 if (jp->jobctl == 0)
6110 error("job not created under job control");
6111 pgrp = jp->ps[0].pid;
6112 ioctl(2, TIOCSPGRP, (char *) &pgrp);
6114 status = waitforjob(jp);
6119 static int bgcmd(int argc, char **argv)
6124 jp = getjob(*++argv);
6125 if (jp->jobctl == 0)
6126 error("job not created under job control");
6128 } while (--argc > 1);
6133 static void restartjob(struct job *jp)
6135 struct procstat *ps;
6138 if (jp->state == JOBDONE)
6141 killpg(jp->ps[0].pid, SIGCONT);
6142 for (ps = jp->ps, i = jp->nprocs; --i >= 0; ps++) {
6143 if (WIFSTOPPED(ps->status)) {
6152 static void showjobs(int change);
6155 static int jobscmd(int argc, char **argv)
6163 * Print a list of jobs. If "change" is nonzero, only print jobs whose
6164 * statuses have changed since the last call to showjobs.
6166 * If the shell is interrupted in the process of creating a job, the
6167 * result may be a job structure containing zero processes. Such structures
6168 * will be freed here.
6171 static void showjobs(int change)
6177 struct procstat *ps;
6181 TRACE(("showjobs(%d) called\n", change));
6182 while (dowait(0, (struct job *) NULL) > 0);
6183 for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
6186 if (jp->nprocs == 0) {
6190 if (change && !jp->changed)
6192 procno = jp->nprocs;
6193 for (ps = jp->ps;; ps++) { /* for each process */
6195 snprintf(s, 64, "[%d] %ld ", jobno, (long) ps->pid);
6197 snprintf(s, 64, " %ld ", (long) ps->pid);
6201 if (ps->status == -1) {
6202 /* don't print anything */
6203 } else if (WIFEXITED(ps->status)) {
6204 snprintf(s, 64, "Exit %d", WEXITSTATUS(ps->status));
6206 #ifdef CONFIG_ASH_JOB_CONTROL
6207 if (WIFSTOPPED(ps->status))
6208 i = WSTOPSIG(ps->status);
6209 else /* WIFSIGNALED(ps->status) */
6211 i = WTERMSIG(ps->status);
6212 if ((i & 0x7F) < NSIG && sys_siglist[i & 0x7F])
6213 strcpy(s, sys_siglist[i & 0x7F]);
6215 snprintf(s, 64, "Signal %d", i & 0x7F);
6216 if (WCOREDUMP(ps->status))
6217 strcat(s, " (core dumped)");
6221 printf("%*c%s\n", 30 - col >= 0 ? 30 - col : 0, ' ', ps->cmd);
6226 if (jp->state == JOBDONE) {
6234 * Mark a job structure as unused.
6237 static void freejob(struct job *jp)
6239 const struct procstat *ps;
6243 for (i = jp->nprocs, ps = jp->ps; --i >= 0; ps++) {
6244 if (ps->cmd != nullstr)
6247 if (jp->ps != &jp->ps0)
6250 #ifdef CONFIG_ASH_JOB_CONTROL
6251 if (curjob == jp - jobtab + 1)
6259 static int waitcmd(int argc, char **argv)
6267 job = getjob(*++argv);
6271 for (;;) { /* loop until process terminated or stopped */
6274 status = job->ps[job->nprocs - 1].status;
6280 if (WIFEXITED(status))
6281 retval = WEXITSTATUS(status);
6282 #ifdef CONFIG_ASH_JOB_CONTROL
6283 else if (WIFSTOPPED(status))
6284 retval = WSTOPSIG(status) + 128;
6287 /* XXX: limits number of signals */
6288 retval = WTERMSIG(status) + 128;
6293 for (jp = jobtab;; jp++) {
6294 if (jp >= jobtab + njobs) { /* no running procs */
6297 if (jp->used && jp->state == 0)
6301 if (dowait(2, 0) < 0 && errno == EINTR) {
6310 * Convert a job name to a job structure.
6313 static struct job *getjob(const char *name)
6321 #ifdef CONFIG_ASH_JOB_CONTROL
6323 if ((jobno = curjob) == 0 || jobtab[jobno - 1].used == 0)
6324 error("No current job");
6325 return &jobtab[jobno - 1];
6327 error("No current job");
6329 } else if (name[0] == '%') {
6330 if (is_digit(name[1])) {
6331 jobno = number(name + 1);
6332 if (jobno > 0 && jobno <= njobs && jobtab[jobno - 1].used != 0)
6333 return &jobtab[jobno - 1];
6334 #ifdef CONFIG_ASH_JOB_CONTROL
6335 } else if (name[1] == '%' && name[2] == '\0') {
6339 struct job *found = NULL;
6341 for (jp = jobtab, i = njobs; --i >= 0; jp++) {
6342 if (jp->used && jp->nprocs > 0
6343 && prefix(name + 1, jp->ps[0].cmd)) {
6345 error("%s: ambiguous", name);
6352 } else if (is_number(name, &pid)) {
6353 for (jp = jobtab, i = njobs; --i >= 0; jp++) {
6354 if (jp->used && jp->nprocs > 0
6355 && jp->ps[jp->nprocs - 1].pid == pid)
6359 error("No such job: %s", name);
6366 * Return a new job structure,
6369 static struct job *makejob(const union node *node, int nprocs)
6374 for (i = njobs, jp = jobtab;; jp++) {
6378 jobtab = xmalloc(4 * sizeof jobtab[0]);
6380 jp = xmalloc((njobs + 4) * sizeof jobtab[0]);
6381 memcpy(jp, jobtab, njobs * sizeof jp[0]);
6382 /* Relocate `ps' pointers */
6383 for (i = 0; i < njobs; i++)
6384 if (jp[i].ps == &jobtab[i].ps0)
6385 jp[i].ps = &jp[i].ps0;
6389 jp = jobtab + njobs;
6390 for (i = 4; --i >= 0; jobtab[njobs++].used = 0);
6402 #ifdef CONFIG_ASH_JOB_CONTROL
6403 jp->jobctl = jobctl;
6406 jp->ps = xmalloc(nprocs * sizeof(struct procstat));
6411 TRACE(("makejob(0x%lx, %d) returns %%%d\n", (long) node, nprocs,
6418 * Fork of a subshell. If we are doing job control, give the subshell its
6419 * own process group. Jp is a job structure that the job is to be added to.
6420 * N is the command that will be evaluated by the child. Both jp and n may
6421 * be NULL. The mode parameter can be one of the following:
6422 * FORK_FG - Fork off a foreground process.
6423 * FORK_BG - Fork off a background process.
6424 * FORK_NOJOB - Like FORK_FG, but don't give the process its own
6425 * process group even if job control is on.
6427 * When job control is turned off, background processes have their standard
6428 * input redirected to /dev/null (except for the second and later processes
6434 static int forkshell(struct job *jp, const union node *n, int mode)
6438 #ifdef CONFIG_ASH_JOB_CONTROL
6441 const char *devnull = _PATH_DEVNULL;
6442 const char *nullerr = "Can't open %s";
6444 TRACE(("forkshell(%%%d, 0x%lx, %d) called\n", jp - jobtab, (long) n,
6449 TRACE(("Fork failed, errno=%d\n", errno));
6451 error("Cannot fork");
6458 TRACE(("Child shell %d\n", getpid()));
6459 wasroot = rootshell;
6464 #ifdef CONFIG_ASH_JOB_CONTROL
6465 jobctl = 0; /* do job control only in root shell */
6466 if (wasroot && mode != FORK_NOJOB && mflag) {
6467 if (jp == NULL || jp->nprocs == 0)
6470 pgrp = jp->ps[0].pid;
6472 if (mode == FORK_FG) {
6473 /*** this causes superfluous TIOCSPGRPS ***/
6474 if (tcsetpgrp(2, pgrp) < 0)
6475 error("tcsetpgrp failed, errno=%d", errno);
6479 } else if (mode == FORK_BG) {
6481 if (mode == FORK_BG) {
6485 if ((jp == NULL || jp->nprocs == 0) && !fd0_redirected_p()) {
6487 if (open(devnull, O_RDONLY) != 0)
6488 error(nullerr, devnull);
6491 for (i = njobs, p = jobtab; --i >= 0; p++)
6494 if (wasroot && iflag) {
6501 #ifdef CONFIG_ASH_JOB_CONTROL
6502 if (rootshell && mode != FORK_NOJOB && mflag) {
6503 if (jp == NULL || jp->nprocs == 0)
6506 pgrp = jp->ps[0].pid;
6510 if (mode == FORK_BG)
6511 backgndpid = pid; /* set $! */
6513 struct procstat *ps = &jp->ps[jp->nprocs++];
6518 if (iflag && rootshell && n)
6519 ps->cmd = commandtext(n);
6522 TRACE(("In parent shell: child = %d\n", pid));
6529 * Wait for job to finish.
6531 * Under job control we have the problem that while a child process is
6532 * running interrupts generated by the user are sent to the child but not
6533 * to the shell. This means that an infinite loop started by an inter-
6534 * active user may be hard to kill. With job control turned off, an
6535 * interactive user may place an interactive program inside a loop. If
6536 * the interactive program catches interrupts, the user doesn't want
6537 * these interrupts to also abort the loop. The approach we take here
6538 * is to have the shell ignore interrupt signals while waiting for a
6539 * forground process to terminate, and then send itself an interrupt
6540 * signal if the child process was terminated by an interrupt signal.
6541 * Unfortunately, some programs want to do a bit of cleanup and then
6542 * exit on interrupt; unless these processes terminate themselves by
6543 * sending a signal to themselves (instead of calling exit) they will
6544 * confuse this approach.
6547 static int waitforjob(struct job *jp)
6549 #ifdef CONFIG_ASH_JOB_CONTROL
6550 int mypgrp = getpgrp();
6556 TRACE(("waitforjob(%%%d) called\n", jp - jobtab + 1));
6557 while (jp->state == 0) {
6560 #ifdef CONFIG_ASH_JOB_CONTROL
6562 if (tcsetpgrp(2, mypgrp) < 0)
6563 error("tcsetpgrp failed, errno=%d\n", errno);
6565 if (jp->state == JOBSTOPPED)
6566 curjob = jp - jobtab + 1;
6568 status = jp->ps[jp->nprocs - 1].status;
6569 /* convert to 8 bits */
6570 if (WIFEXITED(status))
6571 st = WEXITSTATUS(status);
6572 #ifdef CONFIG_ASH_JOB_CONTROL
6573 else if (WIFSTOPPED(status))
6574 st = WSTOPSIG(status) + 128;
6577 st = WTERMSIG(status) + 128;
6578 #ifdef CONFIG_ASH_JOB_CONTROL
6581 * This is truly gross.
6582 * If we're doing job control, then we did a TIOCSPGRP which
6583 * caused us (the shell) to no longer be in the controlling
6584 * session -- so we wouldn't have seen any ^C/SIGINT. So, we
6585 * intuit from the subprocess exit status whether a SIGINT
6586 * occured, and if so interrupt ourselves. Yuck. - mycroft
6588 if (WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
6591 if (jp->state == JOBDONE)
6601 * Wait for a process to terminate.
6605 * Do a wait system call. If job control is compiled in, we accept
6606 * stopped processes. If block is zero, we return a value of zero
6607 * rather than blocking.
6609 * System V doesn't have a non-blocking wait system call. It does
6610 * have a SIGCLD signal that is sent to a process when one of it's
6611 * children dies. The obvious way to use SIGCLD would be to install
6612 * a handler for SIGCLD which simply bumped a counter when a SIGCLD
6613 * was received, and have waitproc bump another counter when it got
6614 * the status of a process. Waitproc would then know that a wait
6615 * system call would not block if the two counters were different.
6616 * This approach doesn't work because if a process has children that
6617 * have not been waited for, System V will send it a SIGCLD when it
6618 * installs a signal handler for SIGCLD. What this means is that when
6619 * a child exits, the shell will be sent SIGCLD signals continuously
6620 * until is runs out of stack space, unless it does a wait call before
6621 * restoring the signal handler. The code below takes advantage of
6622 * this (mis)feature by installing a signal handler for SIGCLD and
6623 * then checking to see whether it was called. If there are any
6624 * children to be waited for, it will be.
6628 static inline int waitproc(int block, int *status)
6633 #ifdef CONFIG_ASH_JOB_CONTROL
6639 return wait3(status, flags, (struct rusage *) NULL);
6642 static int dowait(int block, struct job *job)
6646 struct procstat *sp;
6648 struct job *thisjob;
6654 TRACE(("dowait(%d) called\n", block));
6656 pid = waitproc(block, &status);
6657 TRACE(("wait returns %d, status=%d\n", pid, status));
6658 } while (!(block & 2) && pid == -1 && errno == EINTR);
6663 for (jp = jobtab; jp < jobtab + njobs; jp++) {
6667 for (sp = jp->ps; sp < jp->ps + jp->nprocs; sp++) {
6670 if (sp->pid == pid) {
6671 TRACE(("Changing status of proc %d from 0x%x to 0x%x\n",
6672 pid, sp->status, status));
6673 sp->status = status;
6676 if (sp->status == -1)
6678 else if (WIFSTOPPED(sp->status))
6681 if (stopped) { /* stopped or done */
6682 int state = done ? JOBDONE : JOBSTOPPED;
6684 if (jp->state != state) {
6685 TRACE(("Job %d: changing state from %d to %d\n",
6686 jp - jobtab + 1, jp->state, state));
6688 #ifdef CONFIG_ASH_JOB_CONTROL
6689 if (done && curjob == jp - jobtab + 1)
6690 curjob = 0; /* no current job */
6697 if (!rootshell || !iflag || (job && thisjob == job)) {
6698 core = WCOREDUMP(status);
6699 #ifdef CONFIG_ASH_JOB_CONTROL
6700 if (WIFSTOPPED(status))
6701 sig = WSTOPSIG(status);
6704 if (WIFEXITED(status))
6707 sig = WTERMSIG(status);
6709 if (sig != 0 && sig != SIGINT && sig != SIGPIPE) {
6711 out2fmt("%d: ", pid);
6712 #ifdef CONFIG_ASH_JOB_CONTROL
6713 if (sig == SIGTSTP && rootshell && iflag)
6714 out2fmt("%%%ld ", (long) (job - jobtab + 1));
6716 if (sig < NSIG && sys_siglist[sig])
6717 out2str(sys_siglist[sig]);
6719 out2fmt("Signal %d", sig);
6721 out2str(" - core dumped");
6724 TRACE(("Not printing status: status=%d, sig=%d\n", status, sig));
6727 TRACE(("Not printing status, rootshell=%d, job=0x%x\n", rootshell,
6730 thisjob->changed = 1;
6739 * return 1 if there are stopped jobs, otherwise 0
6741 static int stoppedjobs(void)
6748 for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
6751 if (jp->state == JOBSTOPPED) {
6752 out2str("You have stopped jobs.\n");
6762 * Return a string identifying a command (to be printed by the
6766 static char *cmdnextc;
6767 static int cmdnleft;
6769 #define MAXCMDTEXT 200
6771 static void cmdputs(const char *s)
6782 while ((c = *p++) != '\0') {
6785 else if (c == CTLVAR) {
6790 } else if (c == '=' && subtype != 0) {
6791 *q++ = "}-+?="[(subtype & VSTYPE) - VSNORMAL];
6793 } else if (c == CTLENDVAR) {
6795 } else if (c == CTLBACKQ || c == CTLBACKQ + CTLQUOTE)
6796 cmdnleft++; /* ignore it */
6799 if (--cmdnleft <= 0) {
6809 #define CMDTXT_TABLE
6812 * To collect a lot of redundant code in cmdtxt() case statements, we
6813 * implement a mini language here. Each type of node struct has an
6814 * associated instruction sequence that operates on its members via
6815 * their offsets. The instruction are pack in unsigned chars with
6816 * format IIDDDDDE where the bits are
6817 * I : part of the instruction opcode, which are
6818 * 00 : member is a pointer to another node -- process it recursively
6819 * 40 : member is a pointer to a char string -- output it
6820 * 80 : output the string whose index is stored in the data field
6821 * CC : flag signaling that this case needs external processing
6822 * D : data - either the (shifted) index of a fixed string to output or
6823 * the actual offset of the member to operate on in the struct
6824 * (since we assume bit 0 is set, the offset is not shifted)
6825 * E : flag signaling end of instruction sequence
6827 * WARNING: In order to handle larger offsets for 64bit archs, this code
6828 * assumes that no offset can be an odd number and stores the
6829 * end-of-instructions flag in bit 0.
6832 #define CMDTXT_NOMORE 0x01 /* NOTE: no offset should be odd */
6833 #define CMDTXT_CHARPTR 0x40
6834 #define CMDTXT_STRING 0x80
6835 #define CMDTXT_SPECIAL 0xC0
6836 #define CMDTXT_OFFSETMASK 0x3E
6838 static const char *const cmdtxt_strings[] = {
6839 /* 0 1 2 3 4 5 6 7 */
6840 "; ", "(", ")", " && ", " || ", "if ", "; then ", "...",
6841 /* 8 9 10 11 12 13 */
6842 "while ", "; do ", "; done", "until ", "for ", " in ...",
6844 "case ", "???", "() ...", "<<..."
6847 static const char *const redir_strings[] = {
6848 ">", "<", "<>", ">>", ">|", ">&", "<&"
6851 static const unsigned char cmdtxt_ops[] = {
6852 #define CMDTXT_NSEMI 0
6853 offsetof(union node, nbinary.ch1),
6855 offsetof(union node, nbinary.ch2) | CMDTXT_NOMORE,
6856 #define CMDTXT_NCMD (CMDTXT_NSEMI + 3)
6857 #define CMDTXT_NPIPE (CMDTXT_NCMD)
6858 #define CMDTXT_NCASE (CMDTXT_NCMD)
6859 #define CMDTXT_NTO (CMDTXT_NCMD)
6860 #define CMDTXT_NFROM (CMDTXT_NCMD)
6861 #define CMDTXT_NFROMTO (CMDTXT_NCMD)
6862 #define CMDTXT_NAPPEND (CMDTXT_NCMD)
6863 #define CMDTXT_NTOOV (CMDTXT_NCMD)
6864 #define CMDTXT_NTOFD (CMDTXT_NCMD)
6865 #define CMDTXT_NFROMFD (CMDTXT_NCMD)
6867 #define CMDTXT_NREDIR (CMDTXT_NPIPE + 1)
6868 #define CMDTXT_NBACKGND (CMDTXT_NREDIR)
6869 offsetof(union node, nredir.n) | CMDTXT_NOMORE,
6870 #define CMDTXT_NSUBSHELL (CMDTXT_NBACKGND + 1)
6871 (1 * 2) | CMDTXT_STRING,
6872 offsetof(union node, nredir.n),
6873 (2 * 2) | CMDTXT_STRING | CMDTXT_NOMORE,
6874 #define CMDTXT_NAND (CMDTXT_NSUBSHELL + 3)
6875 offsetof(union node, nbinary.ch1),
6876 (3 * 2) | CMDTXT_STRING,
6877 offsetof(union node, nbinary.ch2) | CMDTXT_NOMORE,
6878 #define CMDTXT_NOR (CMDTXT_NAND + 3)
6879 offsetof(union node, nbinary.ch1),
6880 (4 * 2) | CMDTXT_STRING,
6881 offsetof(union node, nbinary.ch2) | CMDTXT_NOMORE,
6882 #define CMDTXT_NIF (CMDTXT_NOR + 3)
6883 (5 * 2) | CMDTXT_STRING,
6884 offsetof(union node, nif.test),
6885 (6 * 2) | CMDTXT_STRING,
6886 offsetof(union node, nif.ifpart),
6887 (7 * 2) | CMDTXT_STRING | CMDTXT_NOMORE,
6888 #define CMDTXT_NWHILE (CMDTXT_NIF + 5)
6889 (8 * 2) | CMDTXT_STRING,
6890 offsetof(union node, nbinary.ch1),
6891 (9 * 2) | CMDTXT_STRING,
6892 offsetof(union node, nbinary.ch2),
6893 (10 * 2) | CMDTXT_STRING | CMDTXT_NOMORE,
6894 #define CMDTXT_NUNTIL (CMDTXT_NWHILE + 5)
6895 (11 * 2) | CMDTXT_STRING,
6896 offsetof(union node, nbinary.ch1),
6897 (9 * 2) | CMDTXT_STRING,
6898 offsetof(union node, nbinary.ch2),
6899 (10 * 2) | CMDTXT_STRING | CMDTXT_NOMORE,
6900 #define CMDTXT_NFOR (CMDTXT_NUNTIL + 5)
6901 (12 * 2) | CMDTXT_STRING,
6902 offsetof(union node, nfor.var) | CMDTXT_CHARPTR,
6903 (13 * 2) | CMDTXT_STRING | CMDTXT_NOMORE,
6904 #define CMDTXT_NCLIST (CMDTXT_NFOR + 3) /* TODO: IS THIS CORRECT??? */
6905 #define CMDTXT_NNOT (CMDTXT_NCLIST) /* TODO: IS THIS CORRECT??? */
6906 (15 * 2) | CMDTXT_STRING | CMDTXT_NOMORE,
6907 #define CMDTXT_NDEFUN (CMDTXT_NCLIST + 1)
6908 offsetof(union node, narg.text) | CMDTXT_CHARPTR,
6909 (16 * 2) | CMDTXT_STRING | CMDTXT_NOMORE,
6910 #define CMDTXT_NARG (CMDTXT_NDEFUN + 2)
6911 offsetof(union node, narg.text) | CMDTXT_CHARPTR | CMDTXT_NOMORE,
6912 #define CMDTXT_NHERE (CMDTXT_NARG + 1)
6913 #define CMDTXT_NXHERE (CMDTXT_NHERE)
6914 (17 * 2) | CMDTXT_STRING | CMDTXT_NOMORE,
6917 #if CMDTXT_NXHERE != 36
6918 #error CMDTXT_NXHERE
6921 static const unsigned char cmdtxt_ops_index[26] = {
6950 static void cmdtxt(const union node *n)
6957 p = cmdtxt_ops + (int) cmdtxt_ops_index[n->type];
6958 if ((*p & CMDTXT_SPECIAL) != CMDTXT_SPECIAL) { /* normal case */
6960 if (*p & CMDTXT_STRING) { /* output fixed string */
6961 cmdputs(cmdtxt_strings
6962 [((int) (*p & CMDTXT_OFFSETMASK) >> 1)]);
6964 const char *pf = ((const char *) n)
6965 + ((int) (*p & CMDTXT_OFFSETMASK));
6967 if (*p & CMDTXT_CHARPTR) { /* output dynamic string */
6968 cmdputs(*((const char **) pf));
6969 } else { /* output field */
6970 cmdtxt(*((const union node **) pf));
6973 } while (!(*p++ & CMDTXT_NOMORE));
6974 } else if (n->type == NCMD) {
6977 for (np = n->ncmd.args; np; np = np->narg.next) {
6982 for (np = n->ncmd.redirect; np; np = np->nfile.next) {
6986 } else if (n->type == NPIPE) {
6987 struct nodelist *lp;
6989 for (lp = n->npipe.cmdlist; lp; lp = lp->next) {
6994 } else if (n->type == NCASE) {
6995 cmdputs(cmdtxt_strings[14]);
6996 cmdputs(n->ncase.expr->narg.text);
6997 cmdputs(cmdtxt_strings[13]);
6999 #if (NTO != 16) || (NFROM != 17) || (NFROMTO != 18) || (NAPPEND != 19) || (NTOOV != 20) || (NTOFD != 21) || (NFROMFD != 22)
7000 #error Assumption violated regarding range and ordering of NTO ... NFROMFD!
7005 assert((n->type >= NTO) && (n->type <= NFROMFD));
7008 p = redir_strings[n->type - NTO];
7009 if (n->nfile.fd != ('>' == *p)) {
7010 s[0] = n->nfile.fd + '0';
7015 if (n->type >= NTOFD) {
7016 s[0] = n->ndup.dupfd + '0';
7020 cmdtxt(n->nfile.fname);
7024 #else /* CMDTXT_TABLE */
7025 static void cmdtxt(const union node *n)
7028 struct nodelist *lp;
7037 cmdtxt(n->nbinary.ch1);
7039 cmdtxt(n->nbinary.ch2);
7042 cmdtxt(n->nbinary.ch1);
7044 cmdtxt(n->nbinary.ch2);
7047 cmdtxt(n->nbinary.ch1);
7049 cmdtxt(n->nbinary.ch2);
7052 for (lp = n->npipe.cmdlist; lp; lp = lp->next) {
7060 cmdtxt(n->nredir.n);
7065 cmdtxt(n->nredir.n);
7069 cmdtxt(n->nif.test);
7071 cmdtxt(n->nif.ifpart);
7080 cmdtxt(n->nbinary.ch1);
7082 cmdtxt(n->nbinary.ch2);
7087 cmdputs(n->nfor.var);
7092 cmdputs(n->ncase.expr->narg.text);
7096 cmdputs(n->narg.text);
7100 for (np = n->ncmd.args; np; np = np->narg.next) {
7105 for (np = n->ncmd.redirect; np; np = np->nfile.next) {
7111 cmdputs(n->narg.text);
7142 if (n->nfile.fd != i) {
7143 s[0] = n->nfile.fd + '0';
7148 if (n->type == NTOFD || n->type == NFROMFD) {
7149 s[0] = n->ndup.dupfd + '0';
7153 cmdtxt(n->nfile.fname);
7165 #endif /* CMDTXT_TABLE */
7167 static char *commandtext(const union node *n)
7171 cmdnextc = name = xmalloc(MAXCMDTEXT);
7172 cmdnleft = MAXCMDTEXT - 4;
7179 #ifdef CONFIG_ASH_MAIL
7182 * Routines to check for mail.
7186 #define MAXMBOXES 10
7189 static int nmboxes; /* number of mailboxes */
7190 static time_t mailtime[MAXMBOXES]; /* times of mailboxes */
7195 * Print appropriate message(s) if mail has arrived. If the argument is
7196 * nozero, then the value of MAIL has changed, so we just update the
7200 static void chkmail(int silent)
7206 struct stackmark smark;
7213 setstackmark(&smark);
7214 mpath = mpathset()? mpathval() : mailval();
7215 for (i = 0; i < nmboxes; i++) {
7216 p = padvance(&mpath, nullstr);
7221 for (q = p; *q; q++);
7226 q[-1] = '\0'; /* delete trailing '/' */
7227 if (stat(p, &statb) < 0)
7229 if (statb.st_size > mailtime[i] && !silent) {
7230 out2fmt(snlfmt, pathopt ? pathopt : "you have mail");
7232 mailtime[i] = statb.st_size;
7235 popstackmark(&smark);
7238 #endif /* CONFIG_ASH_MAIL */
7243 static short profile_buf[16384];
7247 static int isloginsh = 0;
7249 static void read_profile(const char *);
7250 static void cmdloop(int);
7251 static void options(int);
7252 static void setoption(int, int);
7253 static void procargs(int, char **);
7257 * Main routine. We initialize things, parse the arguments, execute
7258 * profiles if we're a login shell, and then call cmdloop to execute
7259 * commands. The setjmp call sets up the location to jump to when an
7260 * exception occurs. When an exception occurs the variable "state"
7261 * is used to figure out how far we had gotten.
7264 int ash_main(int argc, char **argv)
7266 struct jmploc jmploc;
7267 struct stackmark smark;
7271 BLTINCMD = find_builtin("builtin");
7272 EXECCMD = find_builtin("exec");
7273 EVALCMD = find_builtin("eval");
7275 #ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
7281 monitor(4, etext, profile_buf, sizeof profile_buf, 50);
7283 #if defined(linux) || defined(__GNU__)
7284 signal(SIGCHLD, SIG_DFL);
7287 if (setjmp(jmploc.loc)) {
7290 * When a shell procedure is executed, we raise the
7291 * exception EXSHELLPROC to clean up before executing
7292 * the shell procedure.
7294 if (exception == EXSHELLPROC) {
7300 if (exception == EXEXEC) {
7301 exitstatus = exerrno;
7302 } else if (exception == EXERROR) {
7305 if (state == 0 || iflag == 0 || !rootshell)
7306 exitshell(exitstatus);
7309 if (exception == EXINT) {
7312 popstackmark(&smark);
7313 FORCEINTON; /* enable interrupts */
7316 else if (state == 2)
7318 else if (state == 3)
7326 trputs("Shell args: ");
7332 setstackmark(&smark);
7333 procargs(argc, argv);
7334 #ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
7336 const char *hp = lookupvar("HISTFILE");
7339 hp = lookupvar("HOME");
7341 char *defhp = concat_path_file(hp, ".ash_history");
7342 setvar("HISTFILE", defhp, 0);
7348 if (argv[0] && argv[0][0] == '-')
7352 read_profile("/etc/profile");
7355 read_profile(".profile");
7360 if (getuid() == geteuid() && getgid() == getegid()) {
7362 if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
7364 read_profile(shinit);
7371 if (sflag == 0 || minusc) {
7372 static const char sigs[] = {
7373 SIGINT, SIGQUIT, SIGHUP,
7380 #define SIGSSIZE ((sizeof(sigs)/sizeof(sigs[0])))
7383 for (i = 0; i < SIGSSIZE; i++)
7388 evalstring(minusc, 0);
7390 if (sflag || minusc == NULL) {
7391 state4: /* XXX ??? - why isn't this before the "if" statement */
7392 #ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
7394 const char *hp = lookupvar("HISTFILE");
7397 load_history ( hp );
7405 exitshell(exitstatus);
7411 * Read and execute commands. "Top" is nonzero for the top level command
7412 * loop; it turns on prompting if the shell is interactive.
7415 static void cmdloop(int top)
7418 struct stackmark smark;
7422 TRACE(("cmdloop(%d) called\n", top));
7423 setstackmark(&smark);
7431 #ifdef CONFIG_ASH_MAIL
7436 n = parsecmd(inter);
7437 /* showtree(n); DEBUG */
7439 if (!top || numeof >= 50)
7441 if (!stoppedjobs()) {
7444 out2str("\nUse \"exit\" to leave shell.\n");
7447 } else if (n != NULL && nflag == 0) {
7448 job_warning = (job_warning == 2) ? 1 : 0;
7452 popstackmark(&smark);
7453 setstackmark(&smark);
7454 if (evalskip == SKIPFILE) {
7459 popstackmark(&smark);
7465 * Read /etc/profile or .profile. Return on error.
7468 static void read_profile(const char *name)
7475 if ((fd = open(name, O_RDONLY)) >= 0)
7480 /* -q turns off -x and -v just when executing init files */
7481 /* Note: Might do a little redundant work, but reduces code size. */
7496 * Read a file containing shell functions.
7499 static void readcmdfile(const char *name)
7504 if ((fd = open(name, O_RDONLY)) >= 0)
7507 error("Can't open %s", name);
7516 * Take commands from a file. To be compatable we should do a path
7517 * search for the file, which is necessary to find sub-commands.
7520 static inline char *find_dot_file(char *mybasename)
7523 const char *path = pathval();
7526 /* don't try this for absolute or relative paths */
7527 if (strchr(mybasename, '/'))
7530 while ((fullname = padvance(&path, mybasename)) != NULL) {
7531 if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) {
7533 * Don't bother freeing here, since it will
7534 * be freed by the caller.
7538 stunalloc(fullname);
7541 /* not found in the PATH */
7542 error("%s: not found", mybasename);
7546 static int dotcmd(int argc, char **argv)
7549 volatile struct shparam saveparam;
7553 for (sp = cmdenviron; sp; sp = sp->next)
7554 setvareq(xstrdup(sp->text), VSTRFIXED | VTEXTFIXED);
7556 if (argc >= 2) { /* That's what SVR2 does */
7558 struct stackmark smark;
7560 setstackmark(&smark);
7561 fullname = find_dot_file(argv[1]);
7564 saveparam = shellparam;
7565 shellparam.malloc = 0;
7566 shellparam.nparam = argc - 2;
7567 shellparam.p = argv + 2;
7570 setinputfile(fullname, 1);
7571 commandname = fullname;
7576 freeparam(&shellparam);
7577 shellparam = saveparam;
7580 popstackmark(&smark);
7586 static int exitcmd(int argc, char **argv)
7592 exitstatus = number(argv[1]);
7594 exitstatus = oexitstatus;
7595 exitshell(exitstatus);
7599 static pointer stalloc(int nbytes)
7603 nbytes = ALIGN(nbytes);
7604 if (nbytes > stacknleft) {
7606 struct stack_block *sp;
7609 if (blocksize < MINSIZE)
7610 blocksize = MINSIZE;
7612 sp = xmalloc(sizeof(struct stack_block) - MINSIZE + blocksize);
7614 stacknxt = sp->space;
7615 stacknleft = blocksize;
7621 stacknleft -= nbytes;
7626 static void stunalloc(pointer p)
7629 if (p == NULL) { /*DEBUG */
7630 write(2, "stunalloc\n", 10);
7634 if (!(stacknxt >= (char *) p && (char *) p >= stackp->space)) {
7637 stacknleft += stacknxt - (char *) p;
7642 static void setstackmark(struct stackmark *mark)
7644 mark->stackp = stackp;
7645 mark->stacknxt = stacknxt;
7646 mark->stacknleft = stacknleft;
7647 mark->marknext = markp;
7652 static void popstackmark(struct stackmark *mark)
7654 struct stack_block *sp;
7657 markp = mark->marknext;
7658 while (stackp != mark->stackp) {
7663 stacknxt = mark->stacknxt;
7664 stacknleft = mark->stacknleft;
7670 * When the parser reads in a string, it wants to stick the string on the
7671 * stack and only adjust the stack pointer when it knows how big the
7672 * string is. Stackblock (defined in stack.h) returns a pointer to a block
7673 * of space on top of the stack and stackblocklen returns the length of
7674 * this block. Growstackblock will grow this space by at least one byte,
7675 * possibly moving it (like realloc). Grabstackblock actually allocates the
7676 * part of the block that has been used.
7679 static void growstackblock(void)
7682 int newlen = ALIGN(stacknleft * 2 + 100);
7683 char *oldspace = stacknxt;
7684 int oldlen = stacknleft;
7685 struct stack_block *sp;
7686 struct stack_block *oldstackp;
7688 if (stacknxt == stackp->space && stackp != &stackbase) {
7693 sp = xrealloc((pointer) sp,
7694 sizeof(struct stack_block) - MINSIZE + newlen);
7697 stacknxt = sp->space;
7698 stacknleft = newlen;
7700 /* Stack marks pointing to the start of the old block
7701 * must be relocated to point to the new block
7703 struct stackmark *xmark;
7706 while (xmark != NULL && xmark->stackp == oldstackp) {
7707 xmark->stackp = stackp;
7708 xmark->stacknxt = stacknxt;
7709 xmark->stacknleft = stacknleft;
7710 xmark = xmark->marknext;
7715 p = stalloc(newlen);
7716 memcpy(p, oldspace, oldlen);
7717 stacknxt = p; /* free the space */
7718 stacknleft += newlen; /* we just allocated */
7724 static inline void grabstackblock(int len)
7734 * The following routines are somewhat easier to use that the above.
7735 * The user declares a variable of type STACKSTR, which may be declared
7736 * to be a register. The macro STARTSTACKSTR initializes things. Then
7737 * the user uses the macro STPUTC to add characters to the string. In
7738 * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is
7739 * grown as necessary. When the user is done, she can just leave the
7740 * string there and refer to it using stackblock(). Or she can allocate
7741 * the space for it using grabstackstr(). If it is necessary to allow
7742 * someone else to use the stack temporarily and then continue to grow
7743 * the string, the user should use grabstack to allocate the space, and
7744 * then call ungrabstr(p) to return to the previous mode of operation.
7746 * USTPUTC is like STPUTC except that it doesn't check for overflow.
7747 * CHECKSTACKSPACE can be called before USTPUTC to ensure that there
7748 * is space for at least one character.
7752 static char *growstackstr(void)
7754 int len = stackblocksize();
7756 if (herefd >= 0 && len >= 1024) {
7757 xwrite(herefd, stackblock(), len);
7758 sstrnleft = len - 1;
7759 return stackblock();
7762 sstrnleft = stackblocksize() - len - 1;
7763 return stackblock() + len;
7768 * Called from CHECKSTRSPACE.
7771 static char *makestrspace(size_t newlen)
7773 int len = stackblocksize() - sstrnleft;
7777 sstrnleft = stackblocksize() - len;
7778 } while (sstrnleft < newlen);
7779 return stackblock() + len;
7784 static void ungrabstackstr(char *s, char *p)
7786 stacknleft += stacknxt - s;
7788 sstrnleft = stacknleft - (p - s);
7792 * Miscelaneous builtins.
7798 #if !defined(__GLIBC__) || __GLIBC__ == 2 && __GLIBC_MINOR__ < 1
7799 typedef long rlim_t;
7805 * The read builtin. The -e option causes backslashes to escape the
7806 * following character.
7808 * This uses unbuffered input, which may be avoidable in some cases.
7811 static int readcmd(int argc, char **argv)
7826 while ((i = nextopt("p:r")) != '\0') {
7832 if (prompt && isatty(0)) {
7833 out2str(prompt); /* read without cmdedit */
7836 if (*(ap = argptr) == NULL)
7838 if ((ifs = bltinlookup("IFS")) == NULL)
7845 if (read(0, &c, 1) != 1) {
7857 if (!rflag && c == '\\') {
7863 if (startword && *ifs == ' ' && strchr(ifs, c)) {
7867 if (backslash && c == '\\') {
7868 if (read(0, &c, 1) != 1) {
7873 } else if (ap[1] != NULL && strchr(ifs, c) != NULL) {
7875 setvar(*ap, stackblock(), 0);
7884 /* Remove trailing blanks */
7885 while (stackblock() <= --p && strchr(ifs, *p) != NULL)
7887 setvar(*ap, stackblock(), 0);
7888 while (*++ap != NULL)
7889 setvar(*ap, nullstr, 0);
7895 static int umaskcmd(int argc, char **argv)
7897 static const char permuser[3] = "ugo";
7898 static const char permmode[3] = "rwx";
7899 static const short int permmask[] = {
7900 S_IRUSR, S_IWUSR, S_IXUSR,
7901 S_IRGRP, S_IWGRP, S_IXGRP,
7902 S_IROTH, S_IWOTH, S_IXOTH
7908 int symbolic_mode = 0;
7910 while (nextopt("S") != '\0') {
7919 if ((ap = *argptr) == NULL) {
7920 if (symbolic_mode) {
7924 for (i = 0; i < 3; i++) {
7929 for (j = 0; j < 3; j++) {
7930 if ((mask & permmask[3 * i + j]) == 0) {
7939 printf("%.4o\n", mask);
7942 if (is_digit((unsigned char) *ap)) {
7945 if (*ap >= '8' || *ap < '0')
7946 error("Illegal number: %s", argv[1]);
7947 mask = (mask << 3) + (*ap - '0');
7948 } while (*++ap != '\0');
7951 mask = ~mask & 0777;
7952 if (!parse_mode(ap, &mask)) {
7953 error("Illegal mode: %s", ap);
7955 umask(~mask & 0777);
7964 * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
7965 * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
7966 * ash by J.T. Conklin.
7974 short factor; /* multiply by to get rlim_{cur,max} values */
7977 static const struct limits limits[] = {
7979 {"time(seconds)", RLIMIT_CPU, 1},
7982 {"file(blocks)", RLIMIT_FSIZE, 512},
7985 {"data(kbytes)", RLIMIT_DATA, 1024},
7988 {"stack(kbytes)", RLIMIT_STACK, 1024},
7991 {"coredump(blocks)", RLIMIT_CORE, 512},
7994 {"memory(kbytes)", RLIMIT_RSS, 1024},
7996 #ifdef RLIMIT_MEMLOCK
7997 {"locked memory(kbytes)", RLIMIT_MEMLOCK, 1024},
8000 {"process(processes)", RLIMIT_NPROC, 1},
8002 #ifdef RLIMIT_NOFILE
8003 {"nofiles(descriptors)", RLIMIT_NOFILE, 1},
8006 {"vmemory(kbytes)", RLIMIT_VMEM, 1024},
8009 {"swap(kbytes)", RLIMIT_SWAP, 1024},
8014 static int ulimitcmd(int argc, char **argv)
8016 static const char unlimited_string[] = "unlimited";
8019 enum { SOFT = 0x1, HARD = 0x2 } how = SOFT | HARD;
8020 const struct limits *l;
8023 struct rlimit limit;
8027 while ((optc = nextopt("HSa"
8046 #ifdef RLIMIT_MEMLOCK
8052 #ifdef RLIMIT_NOFILE
8064 } else if (optc == 'S') {
8066 } else if (optc == 'a') {
8073 for (l = limits; l->name; l++) {
8074 if (l->name[0] == what)
8076 if (l->name[1] == 'w' && what == 'w')
8080 set = *argptr ? 1 : 0;
8084 if (all || argptr[1])
8085 error("too many arguments");
8086 if (strcmp(p, unlimited_string) == 0)
8087 val = RLIM_INFINITY;
8091 while ((c = *p++) >= '0' && c <= '9') {
8092 val = (val * 10) + (long) (c - '0');
8093 if (val < (rlim_t) 0)
8097 error("bad number");
8103 for (l = limits; l->name; l++) {
8104 printf("%-20s ", l->name);
8105 getrlimit(l->cmd, &limit);
8108 val = limit.rlim_cur;
8109 else if (how & HARD)
8110 val = limit.rlim_max;
8112 if (val == RLIM_INFINITY)
8113 puts(unlimited_string);
8116 printf("%lld\n", (long long) val);
8129 getrlimit(l->cmd, &limit);
8131 limit.rlim_max = val;
8133 limit.rlim_cur = val;
8134 if (setrlimit(l->cmd, &limit) < 0)
8135 error("error setting limit (%m)");
8140 * prefix -- see if pfx is a prefix of string.
8143 static int prefix(char const *pfx, char const *string)
8146 if (*pfx++ != *string++)
8153 * Return true if s is a string of digits, and save munber in intptr
8157 static int is_number(const char *p, int *intptr)
8165 ret += digit_val(*p);
8167 } while (*p != '\0');
8174 * Convert a string of digits to an integer, printing an error message on
8178 static int number(const char *s)
8182 if (!is_number(s, &i))
8183 error("Illegal number: %s", s);
8188 * Produce a possibly single quoted string suitable as input to the shell.
8189 * The return string is allocated on the stack.
8192 static char *single_quote(const char *s)
8200 size_t len1, len1p, len2, len2p;
8202 len1 = strcspn(s, "'");
8203 len2 = strspn(s + len1, "'");
8205 len1p = len1 ? len1 + 2 : len1;
8206 len2p = len2 + ((len2 < 2) ? len2 : 2);
8208 CHECKSTRSPACE(len1p + len2p + 1, p);
8213 memcpy(p + 1, s, len1);
8221 memcpy(q + 1, s, len2);
8224 } else if (len2 == 1) {
8230 STADJUST(len1p + len2p, p);
8235 return grabstackstr(p);
8239 * Routine for dealing with parsed shell commands.
8243 static void sizenodelist(const struct nodelist *);
8244 static struct nodelist *copynodelist(const struct nodelist *);
8245 static char *nodesavestr(const char *);
8247 #define CALCSIZE_TABLE
8248 #define COPYNODE_TABLE
8249 #if defined(CALCSIZE_TABLE) || defined(COPYNODE_TABLE)
8251 * To collect a lot of redundant code in case statements for copynode()
8252 * and calcsize(), we implement a mini language here. Each type of node
8253 * struct has an associated instruction sequence that operates on its
8254 * members via their offsets. The instruction are pack in unsigned chars
8255 * with format IIDDDDDE where the bits are
8256 * I : part of the instruction opcode, which are
8257 * 00 : member is a pointer to another node
8258 * 40 : member is an integer
8259 * 80 : member is a pointer to a nodelist
8260 * CC : member is a pointer to a char string
8261 * D : data - the actual offset of the member to operate on in the struct
8262 * (since we assume bit 0 is set, it is not shifted)
8263 * E : flag signaling end of instruction sequence
8265 * WARNING: In order to handle larger offsets for 64bit archs, this code
8266 * assumes that no offset can be an odd number and stores the
8267 * end-of-instructions flag in bit 0.
8270 #define NODE_INTEGER 0x40
8271 #define NODE_NODELIST 0x80
8272 #define NODE_CHARPTR 0xC0
8273 #define NODE_NOMORE 0x01 /* Note: no offset should be odd (aligned) */
8274 #define NODE_MBRMASK 0xC0
8275 #define NODE_OFFSETMASK 0x3E
8277 static const unsigned char copynode_ops[35] = {
8278 #define COPYNODE_OPS0 0
8279 offsetof(union node, nbinary.ch2),
8280 offsetof(union node, nbinary.ch1) | NODE_NOMORE,
8281 #define COPYNODE_OPS1 (COPYNODE_OPS0 + 2)
8282 offsetof(union node, ncmd.redirect),
8283 offsetof(union node, ncmd.args),
8284 offsetof(union node, ncmd.assign),
8285 offsetof(union node, ncmd.backgnd) | NODE_INTEGER | NODE_NOMORE,
8286 #define COPYNODE_OPS2 (COPYNODE_OPS1 + 4)
8287 offsetof(union node, npipe.cmdlist) | NODE_NODELIST,
8288 offsetof(union node, npipe.backgnd) | NODE_INTEGER | NODE_NOMORE,
8289 #define COPYNODE_OPS3 (COPYNODE_OPS2 + 2)
8290 offsetof(union node, nredir.redirect),
8291 offsetof(union node, nredir.n) | NODE_NOMORE,
8292 #define COPYNODE_OPS4 (COPYNODE_OPS3 + 2)
8293 offsetof(union node, nif.elsepart),
8294 offsetof(union node, nif.ifpart),
8295 offsetof(union node, nif.test) | NODE_NOMORE,
8296 #define COPYNODE_OPS5 (COPYNODE_OPS4 + 3)
8297 offsetof(union node, nfor.var) | NODE_CHARPTR,
8298 offsetof(union node, nfor.body),
8299 offsetof(union node, nfor.args) | NODE_NOMORE,
8300 #define COPYNODE_OPS6 (COPYNODE_OPS5 + 3)
8301 offsetof(union node, ncase.cases),
8302 offsetof(union node, ncase.expr) | NODE_NOMORE,
8303 #define COPYNODE_OPS7 (COPYNODE_OPS6 + 2)
8304 offsetof(union node, nclist.body),
8305 offsetof(union node, nclist.pattern),
8306 offsetof(union node, nclist.next) | NODE_NOMORE,
8307 #define COPYNODE_OPS8 (COPYNODE_OPS7 + 3)
8308 offsetof(union node, narg.backquote) | NODE_NODELIST,
8309 offsetof(union node, narg.text) | NODE_CHARPTR,
8310 offsetof(union node, narg.next) | NODE_NOMORE,
8311 #define COPYNODE_OPS9 (COPYNODE_OPS8 + 3)
8312 offsetof(union node, nfile.fname),
8313 offsetof(union node, nfile.fd) | NODE_INTEGER,
8314 offsetof(union node, nfile.next) | NODE_NOMORE,
8315 #define COPYNODE_OPS10 (COPYNODE_OPS9 + 3)
8316 offsetof(union node, ndup.vname),
8317 offsetof(union node, ndup.dupfd) | NODE_INTEGER,
8318 offsetof(union node, ndup.fd) | NODE_INTEGER,
8319 offsetof(union node, ndup.next) | NODE_NOMORE,
8320 #define COPYNODE_OPS11 (COPYNODE_OPS10 + 4)
8321 offsetof(union node, nhere.doc),
8322 offsetof(union node, nhere.fd) | NODE_INTEGER,
8323 offsetof(union node, nhere.next) | NODE_NOMORE,
8324 #define COPYNODE_OPS12 (COPYNODE_OPS11 + 3)
8325 offsetof(union node, nnot.com) | NODE_NOMORE,
8328 #if COPYNODE_OPS12 != 34
8329 #error COPYNODE_OPS12 is incorrect
8332 static const unsigned char copynode_ops_index[26] = {
8333 COPYNODE_OPS0, /* NSEMI */
8334 COPYNODE_OPS1, /* NCMD */
8335 COPYNODE_OPS2, /* NPIPE */
8336 COPYNODE_OPS3, /* NREDIR */
8337 COPYNODE_OPS3, /* NBACKGND */
8338 COPYNODE_OPS3, /* NSUBSHELL */
8339 COPYNODE_OPS0, /* NAND */
8340 COPYNODE_OPS0, /* NOR */
8341 COPYNODE_OPS4, /* NIF */
8342 COPYNODE_OPS0, /* NWHILE */
8343 COPYNODE_OPS0, /* NUNTIL */
8344 COPYNODE_OPS5, /* NFOR */
8345 COPYNODE_OPS6, /* NCASE */
8346 COPYNODE_OPS7, /* NCLIST */
8347 COPYNODE_OPS8, /* NDEFUN */
8348 COPYNODE_OPS8, /* NARG */
8349 COPYNODE_OPS9, /* NTO */
8350 COPYNODE_OPS9, /* NFROM */
8351 COPYNODE_OPS9, /* NFROMTO */
8352 COPYNODE_OPS9, /* NAPPEND */
8353 COPYNODE_OPS9, /* NTOOV */
8354 COPYNODE_OPS10, /* NTOFD */
8355 COPYNODE_OPS10, /* NFROMFD */
8356 COPYNODE_OPS11, /* NHERE */
8357 COPYNODE_OPS11, /* NXHERE */
8358 COPYNODE_OPS12, /* NNOT */
8361 #if NODE_CHARPTR != NODE_MBRMASK
8362 #error NODE_CHARPTR != NODE_MBRMASK!!!
8364 #endif /* defined(CALCSIZE_TABLE) || defined(COPYNODE_TABLE) */
8366 #ifdef COPYNODE_TABLE
8367 static union node *copynode(const union node *n)
8370 const unsigned char *p;
8376 new->type = n->type;
8377 funcblock = (char *) funcblock + (int) nodesize[n->type];
8378 p = copynode_ops + (int) copynode_ops_index[n->type];
8380 char *nn = ((char *) new) + ((int) (*p & NODE_OFFSETMASK));
8381 const char *no = ((const char *) n) + ((int) (*p & NODE_OFFSETMASK));
8383 if (!(*p & NODE_MBRMASK)) { /* standard node */
8384 *((union node **) nn) = copynode(*((const union node **) no));
8385 } else if ((*p & NODE_MBRMASK) == NODE_CHARPTR) { /* string */
8386 *((const char **) nn) = nodesavestr(*((const char **) no));
8387 } else if (*p & NODE_NODELIST) { /* nodelist */
8388 *((struct nodelist **) nn)
8389 = copynodelist(*((const struct nodelist **) no));
8390 } else { /* integer */
8391 *((int *) nn) = *((int *) no);
8393 } while (!(*p++ & NODE_NOMORE));
8396 #else /* COPYNODE_TABLE */
8397 static union node *copynode(const union node *n)
8404 funcblock = (char *) funcblock + nodesize[n->type];
8411 new->nbinary.ch2 = copynode(n->nbinary.ch2);
8412 new->nbinary.ch1 = copynode(n->nbinary.ch1);
8415 new->ncmd.redirect = copynode(n->ncmd.redirect);
8416 new->ncmd.args = copynode(n->ncmd.args);
8417 new->ncmd.assign = copynode(n->ncmd.assign);
8418 new->ncmd.backgnd = n->ncmd.backgnd;
8421 new->npipe.cmdlist = copynodelist(n->npipe.cmdlist);
8422 new->npipe.backgnd = n->npipe.backgnd;
8427 new->nredir.redirect = copynode(n->nredir.redirect);
8428 new->nredir.n = copynode(n->nredir.n);
8431 new->nif.elsepart = copynode(n->nif.elsepart);
8432 new->nif.ifpart = copynode(n->nif.ifpart);
8433 new->nif.test = copynode(n->nif.test);
8436 new->nfor.var = nodesavestr(n->nfor.var);
8437 new->nfor.body = copynode(n->nfor.body);
8438 new->nfor.args = copynode(n->nfor.args);
8441 new->ncase.cases = copynode(n->ncase.cases);
8442 new->ncase.expr = copynode(n->ncase.expr);
8445 new->nclist.body = copynode(n->nclist.body);
8446 new->nclist.pattern = copynode(n->nclist.pattern);
8447 new->nclist.next = copynode(n->nclist.next);
8451 new->narg.backquote = copynodelist(n->narg.backquote);
8452 new->narg.text = nodesavestr(n->narg.text);
8453 new->narg.next = copynode(n->narg.next);
8460 new->nfile.fname = copynode(n->nfile.fname);
8461 new->nfile.fd = n->nfile.fd;
8462 new->nfile.next = copynode(n->nfile.next);
8466 new->ndup.vname = copynode(n->ndup.vname);
8467 new->ndup.dupfd = n->ndup.dupfd;
8468 new->ndup.fd = n->ndup.fd;
8469 new->ndup.next = copynode(n->ndup.next);
8473 new->nhere.doc = copynode(n->nhere.doc);
8474 new->nhere.fd = n->nhere.fd;
8475 new->nhere.next = copynode(n->nhere.next);
8478 new->nnot.com = copynode(n->nnot.com);
8481 new->type = n->type;
8484 #endif /* COPYNODE_TABLE */
8486 #ifdef CALCSIZE_TABLE
8487 static void calcsize(const union node *n)
8489 const unsigned char *p;
8493 funcblocksize += (int) nodesize[n->type];
8495 p = copynode_ops + (int) copynode_ops_index[n->type];
8497 const char *no = ((const char *) n) + ((int) (*p & NODE_OFFSETMASK));
8499 if (!(*p & NODE_MBRMASK)) { /* standard node */
8500 calcsize(*((const union node **) no));
8501 } else if ((*p & NODE_MBRMASK) == NODE_CHARPTR) { /* string */
8502 funcstringsize += strlen(*((const char **) no)) + 1;
8503 } else if (*p & NODE_NODELIST) { /* nodelist */
8504 sizenodelist(*((const struct nodelist **) no));
8505 } /* else integer -- ignore */
8506 } while (!(*p++ & NODE_NOMORE));
8508 #else /* CALCSIZE_TABLE */
8509 static void calcsize(const union node *n)
8513 funcblocksize += nodesize[n->type];
8520 calcsize(n->nbinary.ch2);
8521 calcsize(n->nbinary.ch1);
8524 calcsize(n->ncmd.redirect);
8525 calcsize(n->ncmd.args);
8526 calcsize(n->ncmd.assign);
8529 sizenodelist(n->npipe.cmdlist);
8534 calcsize(n->nredir.redirect);
8535 calcsize(n->nredir.n);
8538 calcsize(n->nif.elsepart);
8539 calcsize(n->nif.ifpart);
8540 calcsize(n->nif.test);
8543 funcstringsize += strlen(n->nfor.var) + 1;
8544 calcsize(n->nfor.body);
8545 calcsize(n->nfor.args);
8548 calcsize(n->ncase.cases);
8549 calcsize(n->ncase.expr);
8552 calcsize(n->nclist.body);
8553 calcsize(n->nclist.pattern);
8554 calcsize(n->nclist.next);
8558 sizenodelist(n->narg.backquote);
8559 funcstringsize += strlen(n->narg.text) + 1;
8560 calcsize(n->narg.next);
8567 calcsize(n->nfile.fname);
8568 calcsize(n->nfile.next);
8572 calcsize(n->ndup.vname);
8573 calcsize(n->ndup.next);
8577 calcsize(n->nhere.doc);
8578 calcsize(n->nhere.next);
8581 calcsize(n->nnot.com);
8585 #endif /* CALCSIZE_TABLE */
8587 static void sizenodelist(const struct nodelist *lp)
8590 funcblocksize += ALIGN(sizeof(struct nodelist));
8597 static struct nodelist *copynodelist(const struct nodelist *lp)
8599 struct nodelist *start;
8600 struct nodelist **lpp;
8605 funcblock = (char *) funcblock + ALIGN(sizeof(struct nodelist));
8606 (*lpp)->n = copynode(lp->n);
8608 lpp = &(*lpp)->next;
8615 static char *nodesavestr(const char *s)
8618 char *q = funcstring;
8619 char *rtn = funcstring;
8621 while ((*q++ = *p++) != '\0')
8627 #ifdef CONFIG_ASH_GETOPTS
8628 static int getopts(char *, char *, char **, int *, int *);
8632 * Process the shell command line arguments.
8635 static void procargs(int argc, char **argv)
8642 for (i = 0; i < NOPTS; i++)
8645 if (*argptr == NULL && minusc == NULL)
8647 if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1))
8651 for (i = 0; i < NOPTS; i++)
8652 if (optent_val(i) == 2)
8655 if (sflag == 0 && minusc == NULL) {
8656 commandname = argv[0];
8658 setinputfile(arg0, 0);
8661 /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */
8662 if (argptr && minusc && *argptr)
8665 shellparam.p = argptr;
8666 shellparam.optind = 1;
8667 shellparam.optoff = -1;
8668 /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
8670 shellparam.nparam++;
8679 * Process shell options. The global variable argptr contains a pointer
8680 * to the argument list; we advance it past the options.
8683 static inline void minus_o(const char *name, int val)
8688 out1str("Current option settings\n");
8689 for (i = 0; i < NOPTS; i++)
8690 printf("%-16s%s\n", optent_name(optlist[i]),
8691 optent_val(i) ? "on" : "off");
8693 for (i = 0; i < NOPTS; i++)
8694 if (equal(name, optent_name(optlist[i]))) {
8695 setoption(optent_letter(optlist[i]), val);
8698 error("Illegal option -o %s", name);
8703 static void options(int cmdline)
8711 while ((p = *argptr) != NULL) {
8713 if ((c = *p++) == '-') {
8715 if (p[0] == '\0' || (p[0] == '-' && p[1] == '\0')) {
8717 /* "-" means turn off -x and -v */
8720 /* "--" means reset params */
8721 else if (*argptr == NULL)
8724 break; /* "-" or "--" terminates options */
8726 } else if (c == '+') {
8732 while ((c = *p++) != '\0') {
8733 if (c == 'c' && cmdline) {
8736 #ifdef NOHACK /* removing this code allows sh -ce 'foo' for compat */
8740 if (q == NULL || minusc != NULL)
8741 error("Bad -c option");
8746 } else if (c == 'o') {
8747 minus_o(*argptr, val);
8750 } else if (cmdline && (c == '-')) { // long options
8751 if (strcmp(p, "login") == 0)
8762 static void setoption(int flag, int val)
8766 for (i = 0; i < NOPTS; i++)
8767 if (optent_letter(optlist[i]) == flag) {
8768 optent_val(i) = val;
8770 /* #%$ hack for ksh semantics */
8773 else if (flag == 'E')
8778 error("Illegal option -%c", flag);
8785 * Set the shell parameters.
8788 static void setparam(char **argv)
8794 for (nparam = 0; argv[nparam]; nparam++);
8795 ap = newparam = xmalloc((nparam + 1) * sizeof *ap);
8797 *ap++ = xstrdup(*argv++);
8800 freeparam(&shellparam);
8801 shellparam.malloc = 1;
8802 shellparam.nparam = nparam;
8803 shellparam.p = newparam;
8804 shellparam.optind = 1;
8805 shellparam.optoff = -1;
8810 * Free the list of positional parameters.
8813 static void freeparam(volatile struct shparam *param)
8817 if (param->malloc) {
8818 for (ap = param->p; *ap; ap++)
8827 * The shift builtin command.
8830 static int shiftcmd(int argc, char **argv)
8837 n = number(argv[1]);
8838 if (n > shellparam.nparam)
8839 error("can't shift that many");
8841 shellparam.nparam -= n;
8842 for (ap1 = shellparam.p; --n >= 0; ap1++) {
8843 if (shellparam.malloc)
8847 while ((*ap2++ = *ap1++) != NULL);
8848 shellparam.optind = 1;
8849 shellparam.optoff = -1;
8857 * The set command builtin.
8860 static int setcmd(int argc, char **argv)
8863 return showvarscmd(argc, argv);
8867 if (*argptr != NULL) {
8875 static void getoptsreset(const char *value)
8877 shellparam.optind = number(value);
8878 shellparam.optoff = -1;
8881 #ifdef CONFIG_LOCALE_SUPPORT
8882 static void change_lc_all(const char *value)
8884 if (value != 0 && *value != 0)
8885 setlocale(LC_ALL, value);
8888 static void change_lc_ctype(const char *value)
8890 if (value != 0 && *value != 0)
8891 setlocale(LC_CTYPE, value);
8896 #ifdef CONFIG_ASH_GETOPTS
8898 * The getopts builtin. Shellparam.optnext points to the next argument
8899 * to be processed. Shellparam.optptr points to the next character to
8900 * be processed in the current argument. If shellparam.optnext is NULL,
8901 * then it's the first time getopts has been called.
8904 static int getoptscmd(int argc, char **argv)
8909 error("Usage: getopts optstring var [arg]");
8910 else if (argc == 3) {
8911 optbase = shellparam.p;
8912 if (shellparam.optind > shellparam.nparam + 1) {
8913 shellparam.optind = 1;
8914 shellparam.optoff = -1;
8918 if (shellparam.optind > argc - 2) {
8919 shellparam.optind = 1;
8920 shellparam.optoff = -1;
8924 return getopts(argv[1], argv[2], optbase, &shellparam.optind,
8925 &shellparam.optoff);
8929 * Safe version of setvar, returns 1 on success 0 on failure.
8932 static int setvarsafe(const char *name, const char *val, int flags)
8934 struct jmploc jmploc;
8935 struct jmploc *volatile savehandler = handler;
8942 if (setjmp(jmploc.loc))
8946 setvar(name, val, flags);
8948 handler = savehandler;
8953 getopts(char *optstr, char *optvar, char **optfirst, int *myoptind,
8961 char **optnext = optfirst + *myoptind - 1;
8963 if (*myoptind <= 1 || *optoff < 0 || !(*(optnext - 1)) ||
8964 strlen(*(optnext - 1)) < *optoff)
8967 p = *(optnext - 1) + *optoff;
8968 if (p == NULL || *p == '\0') {
8969 /* Current word is done, advance */
8970 if (optnext == NULL)
8973 if (p == NULL || *p != '-' || *++p == '\0') {
8975 *myoptind = optnext - optfirst + 1;
8981 if (p[0] == '-' && p[1] == '\0') /* check for "--" */
8986 for (q = optstr; *q != c;) {
8988 if (optstr[0] == ':') {
8991 err |= setvarsafe("OPTARG", s, 0);
8993 out2fmt("Illegal option -%c\n", c);
8994 (void) unsetvar("OPTARG");
9004 if (*p == '\0' && (p = *optnext) == NULL) {
9005 if (optstr[0] == ':') {
9008 err |= setvarsafe("OPTARG", s, 0);
9011 out2fmt("No arg for -%c option\n", c);
9012 (void) unsetvar("OPTARG");
9020 setvarsafe("OPTARG", p, 0);
9023 setvarsafe("OPTARG", "", 0);
9024 *myoptind = optnext - optfirst + 1;
9031 *optoff = p ? p - *(optnext - 1) : -1;
9032 snprintf(s, sizeof(s), "%d", *myoptind);
9033 err |= setvarsafe("OPTIND", s, VNOFUNC);
9036 err |= setvarsafe(optvar, s, 0);
9047 * XXX - should get rid of. have all builtins use getopt(3). the
9048 * library getopt must have the BSD extension static variable "optreset"
9049 * otherwise it can't be used within the shell safely.
9051 * Standard option processing (a la getopt) for builtin routines. The
9052 * only argument that is passed to nextopt is the option string; the
9053 * other arguments are unnecessary. It return the character, or '\0' on
9057 static int nextopt(const char *optstring)
9063 if ((p = optptr) == NULL || *p == '\0') {
9065 if (p == NULL || *p != '-' || *++p == '\0')
9068 if (p[0] == '-' && p[1] == '\0') /* check for "--" */
9072 for (q = optstring; *q != c;) {
9074 error("Illegal option -%c", c);
9079 if (*p == '\0' && (p = *argptr++) == NULL)
9080 error("No arg for -%c option", c);
9088 static void flushall()
9096 static void out2fmt(const char *fmt, ...)
9101 vfprintf(stderr, fmt, ap);
9106 * Version of write which resumes after a signal is caught.
9109 static int xwrite(int fd, const char *buf, int nbytes)
9118 i = write(fd, buf, n);
9124 } else if (i == 0) {
9127 } else if (errno != EINTR) {
9135 * Shell command parser.
9138 #define EOFMARKLEN 79
9143 struct heredoc *next; /* next here document in list */
9144 union node *here; /* redirection node */
9145 char *eofmark; /* string indicating end of input */
9146 int striptabs; /* if set, strip leading tabs */
9149 static struct heredoc *heredoclist; /* list of here documents to read */
9150 static int parsebackquote; /* nonzero if we are inside backquotes */
9151 static int doprompt; /* if set, prompt the user */
9152 static int needprompt; /* true if interactive and at start of line */
9153 static int lasttoken; /* last token read */
9155 static char *wordtext; /* text of last word returned by readtoken */
9157 static struct nodelist *backquotelist;
9158 static union node *redirnode;
9159 static struct heredoc *heredoc;
9160 static int quoteflag; /* set if (part of) last token was quoted */
9161 static int startlinno; /* line # where last token started */
9164 static union node *list(int);
9165 static union node *andor(void);
9166 static union node *pipeline(void);
9167 static union node *command(void);
9168 static union node *simplecmd(union node **rpp, union node *redir);
9169 static void parsefname(void);
9170 static void parseheredoc(void);
9171 static char peektoken(void);
9172 static int readtoken(void);
9173 static int xxreadtoken(void);
9174 static int readtoken1(int, int, const char *, int);
9175 static int noexpand(char *);
9176 static void synexpect(int) __attribute__ ((noreturn));
9177 static void synerror(const char *) __attribute__ ((noreturn));
9178 static void setprompt(int);
9182 * Read and parse a command. Returns NEOF on end of file. (NULL is a
9183 * valid parse tree indicating a blank line.)
9186 static union node *parsecmd(int interact)
9191 doprompt = interact;
9207 static union node *list(int nlflag)
9209 union node *n1, *n2, *n3;
9213 if (nlflag == 0 && peektoken())
9219 if (tok == TBACKGND) {
9220 if (n2->type == NCMD || n2->type == NPIPE) {
9221 n2->ncmd.backgnd = 1;
9222 } else if (n2->type == NREDIR) {
9223 n2->type = NBACKGND;
9225 n3 = (union node *) stalloc(sizeof(struct nredir));
9226 n3->type = NBACKGND;
9228 n3->nredir.redirect = NULL;
9235 n3 = (union node *) stalloc(sizeof(struct nbinary));
9237 n3->nbinary.ch1 = n1;
9238 n3->nbinary.ch2 = n2;
9262 pungetc(); /* push back EOF on input */
9275 static union node *andor()
9277 union node *n1, *n2, *n3;
9283 if ((t = readtoken()) == TAND) {
9285 } else if (t == TOR) {
9293 n3 = (union node *) stalloc(sizeof(struct nbinary));
9295 n3->nbinary.ch1 = n1;
9296 n3->nbinary.ch2 = n2;
9303 static union node *pipeline()
9305 union node *n1, *n2, *pipenode;
9306 struct nodelist *lp, *prev;
9310 TRACE(("pipeline: entered\n"));
9311 if (readtoken() == TNOT) {
9317 if (readtoken() == TPIPE) {
9318 pipenode = (union node *) stalloc(sizeof(struct npipe));
9319 pipenode->type = NPIPE;
9320 pipenode->npipe.backgnd = 0;
9321 lp = (struct nodelist *) stalloc(sizeof(struct nodelist));
9322 pipenode->npipe.cmdlist = lp;
9326 lp = (struct nodelist *) stalloc(sizeof(struct nodelist));
9330 } while (readtoken() == TPIPE);
9336 n2 = (union node *) stalloc(sizeof(struct nnot));
9346 static union node *command(void)
9348 union node *n1, *n2;
9349 union node *ap, **app;
9350 union node *cp, **cpp;
9351 union node *redir, **rpp;
9358 /* Check for redirection which may precede command */
9359 while (readtoken() == TREDIR) {
9360 *rpp = n2 = redirnode;
9361 rpp = &n2->nfile.next;
9366 switch (readtoken()) {
9368 n1 = (union node *) stalloc(sizeof(struct nif));
9370 n1->nif.test = list(0);
9371 if (readtoken() != TTHEN)
9373 n1->nif.ifpart = list(0);
9375 while (readtoken() == TELIF) {
9376 n2->nif.elsepart = (union node *) stalloc(sizeof(struct nif));
9377 n2 = n2->nif.elsepart;
9379 n2->nif.test = list(0);
9380 if (readtoken() != TTHEN)
9382 n2->nif.ifpart = list(0);
9384 if (lasttoken == TELSE)
9385 n2->nif.elsepart = list(0);
9387 n2->nif.elsepart = NULL;
9390 if (readtoken() != TFI)
9397 n1 = (union node *) stalloc(sizeof(struct nbinary));
9398 n1->type = (lasttoken == TWHILE) ? NWHILE : NUNTIL;
9399 n1->nbinary.ch1 = list(0);
9400 if ((got = readtoken()) != TDO) {
9401 TRACE(("expecting DO got %s %s\n", tokname(got),
9402 got == TWORD ? wordtext : ""));
9405 n1->nbinary.ch2 = list(0);
9406 if (readtoken() != TDONE)
9412 if (readtoken() != TWORD || quoteflag || !goodname(wordtext))
9413 synerror("Bad for loop variable");
9414 n1 = (union node *) stalloc(sizeof(struct nfor));
9416 n1->nfor.var = wordtext;
9418 if (readtoken() == TIN) {
9420 while (readtoken() == TWORD) {
9421 n2 = (union node *) stalloc(sizeof(struct narg));
9423 n2->narg.text = wordtext;
9424 n2->narg.backquote = backquotelist;
9426 app = &n2->narg.next;
9430 if (lasttoken != TNL && lasttoken != TSEMI)
9433 static char argvars[5] = { CTLVAR, VSNORMAL | VSQUOTE,
9436 n2 = (union node *) stalloc(sizeof(struct narg));
9438 n2->narg.text = argvars;
9439 n2->narg.backquote = NULL;
9440 n2->narg.next = NULL;
9443 * Newline or semicolon here is optional (but note
9444 * that the original Bourne shell only allowed NL).
9446 if (lasttoken != TNL && lasttoken != TSEMI)
9450 if (readtoken() != TDO)
9452 n1->nfor.body = list(0);
9453 if (readtoken() != TDONE)
9458 n1 = (union node *) stalloc(sizeof(struct ncase));
9460 if (readtoken() != TWORD)
9462 n1->ncase.expr = n2 = (union node *) stalloc(sizeof(struct narg));
9464 n2->narg.text = wordtext;
9465 n2->narg.backquote = backquotelist;
9466 n2->narg.next = NULL;
9469 } while (readtoken() == TNL);
9470 if (lasttoken != TIN)
9471 synerror("expecting \"in\"");
9472 cpp = &n1->ncase.cases;
9473 checkkwd = 2, readtoken();
9475 if (lasttoken == TLP)
9477 *cpp = cp = (union node *) stalloc(sizeof(struct nclist));
9479 app = &cp->nclist.pattern;
9481 *app = ap = (union node *) stalloc(sizeof(struct narg));
9483 ap->narg.text = wordtext;
9484 ap->narg.backquote = backquotelist;
9485 if (checkkwd = 2, readtoken() != TPIPE)
9487 app = &ap->narg.next;
9490 ap->narg.next = NULL;
9491 if (lasttoken != TRP)
9493 cp->nclist.body = list(0);
9496 if ((t = readtoken()) != TESAC) {
9498 synexpect(TENDCASE);
9500 checkkwd = 2, readtoken();
9502 cpp = &cp->nclist.next;
9503 } while (lasttoken != TESAC);
9508 n1 = (union node *) stalloc(sizeof(struct nredir));
9509 n1->type = NSUBSHELL;
9510 n1->nredir.n = list(0);
9511 n1->nredir.redirect = NULL;
9512 if (readtoken() != TRP)
9518 if (readtoken() != TEND)
9522 /* Handle an empty command like other simple commands. */
9531 * An empty command before a ; doesn't make much sense, and
9532 * should certainly be disallowed in the case of `if ;'.
9538 n1 = simplecmd(rpp, redir);
9545 /* Now check for redirection which may follow command */
9546 while (readtoken() == TREDIR) {
9547 *rpp = n2 = redirnode;
9548 rpp = &n2->nfile.next;
9554 if (n1->type != NSUBSHELL) {
9555 n2 = (union node *) stalloc(sizeof(struct nredir));
9560 n1->nredir.redirect = redir;
9567 static union node *simplecmd(union node **rpp, union node *redir)
9569 union node *args, **app;
9570 union node *n = NULL;
9571 union node *vars, **vpp;
9572 union node **orig_rpp;
9579 /* If we don't have any redirections already, then we must reset
9580 rpp to be the address of the local redir variable. */
9583 /* We save the incoming value, because we need this for shell
9584 functions. There can not be a redirect or an argument between
9585 the function name and the open parenthesis. */
9590 switch (readtoken()) {
9593 n = (union node *) stalloc(sizeof(struct narg));
9595 n->narg.text = wordtext;
9596 n->narg.backquote = backquotelist;
9597 if (lasttoken == TWORD) {
9599 app = &n->narg.next;
9602 vpp = &n->narg.next;
9606 *rpp = n = redirnode;
9607 rpp = &n->nfile.next;
9608 parsefname(); /* read name of redirection file */
9611 if (args && app == &args->narg.next && !vars && rpp == orig_rpp) {
9612 /* We have a function */
9613 if (readtoken() != TRP)
9617 n->narg.next = command();
9630 n = (union node *) stalloc(sizeof(struct ncmd));
9632 n->ncmd.backgnd = 0;
9633 n->ncmd.args = args;
9634 n->ncmd.assign = vars;
9635 n->ncmd.redirect = redir;
9639 static union node *makename(void)
9643 n = (union node *) stalloc(sizeof(struct narg));
9645 n->narg.next = NULL;
9646 n->narg.text = wordtext;
9647 n->narg.backquote = backquotelist;
9651 static void fixredir(union node *n, const char *text, int err)
9653 TRACE(("Fix redir %s %d\n", text, err));
9655 n->ndup.vname = NULL;
9657 if (is_digit(text[0]) && text[1] == '\0')
9658 n->ndup.dupfd = digit_val(text[0]);
9659 else if (text[0] == '-' && text[1] == '\0')
9664 synerror("Bad fd number");
9666 n->ndup.vname = makename();
9671 static void parsefname(void)
9673 union node *n = redirnode;
9675 if (readtoken() != TWORD)
9677 if (n->type == NHERE) {
9678 struct heredoc *here = heredoc;
9684 TRACE(("Here document %d\n", n->type));
9685 if (here->striptabs) {
9686 while (*wordtext == '\t')
9689 if (!noexpand(wordtext) || (i = strlen(wordtext)) == 0
9691 synerror("Illegal eof marker for << redirection");
9692 rmescapes(wordtext);
9693 here->eofmark = wordtext;
9695 if (heredoclist == NULL)
9698 for (p = heredoclist; p->next; p = p->next);
9701 } else if (n->type == NTOFD || n->type == NFROMFD) {
9702 fixredir(n, wordtext, 0);
9704 n->nfile.fname = makename();
9710 * Input any here documents.
9713 static void parseheredoc()
9715 struct heredoc *here;
9718 while (heredoclist) {
9720 heredoclist = here->next;
9725 readtoken1(pgetc(), here->here->type == NHERE ? SQSYNTAX : DQSYNTAX,
9726 here->eofmark, here->striptabs);
9727 n = (union node *) stalloc(sizeof(struct narg));
9728 n->narg.type = NARG;
9729 n->narg.next = NULL;
9730 n->narg.text = wordtext;
9731 n->narg.backquote = backquotelist;
9732 here->here->nhere.doc = n;
9736 static char peektoken()
9742 return tokname_array[t][0];
9745 static int readtoken()
9749 int savecheckalias = checkalias;
9751 #ifdef CONFIG_ASH_ALIAS
9752 int savecheckkwd = checkkwd;
9757 int alreadyseen = tokpushback;
9760 #ifdef CONFIG_ASH_ALIAS
9766 checkalias = savecheckalias;
9772 if (checkkwd == 2) {
9781 * check for keywords
9783 if (t == TWORD && !quoteflag) {
9784 const char *const *pp;
9786 if ((pp = findkwd(wordtext))) {
9787 lasttoken = t = pp - tokname_array;
9788 TRACE(("keyword %s recognized\n", tokname(t)));
9799 } else if (checkalias == 2 && isassignment(wordtext)) {
9800 lasttoken = t = TASSIGN;
9801 } else if (checkalias) {
9802 #ifdef CONFIG_ASH_ALIAS
9803 if (!quoteflag && (ap = *__lookupalias(wordtext)) != NULL
9804 && !(ap->flag & ALIASINUSE)) {
9806 pushstring(ap->val, strlen(ap->val), ap);
9808 checkkwd = savecheckkwd;
9817 TRACE(("token %s %s\n", tokname(t), t == TWORD
9818 || t == TASSIGN ? wordtext : ""));
9820 TRACE(("reread token %s %s\n", tokname(t), t == TWORD
9821 || t == TASSIGN ? wordtext : ""));
9828 * Read the next input token.
9829 * If the token is a word, we set backquotelist to the list of cmds in
9830 * backquotes. We set quoteflag to true if any part of the word was
9832 * If the token is TREDIR, then we set redirnode to a structure containing
9834 * In all cases, the variable startlinno is set to the number of the line
9835 * on which the token starts.
9837 * [Change comment: here documents and internal procedures]
9838 * [Readtoken shouldn't have any arguments. Perhaps we should make the
9839 * word parsing code into a separate routine. In this case, readtoken
9840 * doesn't need to have any internal procedures, but parseword does.
9841 * We could also make parseoperator in essence the main routine, and
9842 * have parseword (readtoken1?) handle both words and redirection.]
9845 #define NEW_xxreadtoken
9846 #ifdef NEW_xxreadtoken
9848 static const char xxreadtoken_chars[] = "\n()&|;"; /* singles must be first! */
9849 static const char xxreadtoken_tokens[] = {
9850 TNL, TLP, TRP, /* only single occurrence allowed */
9851 TBACKGND, TPIPE, TSEMI, /* if single occurrence */
9852 TEOF, /* corresponds to trailing nul */
9853 TAND, TOR, TENDCASE, /* if double occurrence */
9856 #define xxreadtoken_doubles \
9857 (sizeof(xxreadtoken_tokens) - sizeof(xxreadtoken_chars))
9858 #define xxreadtoken_singles \
9859 (sizeof(xxreadtoken_chars) - xxreadtoken_doubles - 1)
9861 static int xxreadtoken()
9873 startlinno = plinno;
9874 for (;;) { /* until token or start of word found */
9877 if ((c != ' ') && (c != '\t')
9878 #ifdef CONFIG_ASH_ALIAS
9883 while ((c = pgetc()) != '\n' && c != PEOF);
9885 } else if (c == '\\') {
9886 if (pgetc() != '\n') {
9890 startlinno = ++plinno;
9891 setprompt(doprompt ? 2 : 0);
9894 = xxreadtoken_chars + sizeof(xxreadtoken_chars) - 1;
9899 needprompt = doprompt;
9902 p = strchr(xxreadtoken_chars, c);
9905 return readtoken1(c, BASESYNTAX, (char *) NULL, 0);
9908 if (p - xxreadtoken_chars >= xxreadtoken_singles) {
9909 if (pgetc() == *p) { /* double occurrence? */
9910 p += xxreadtoken_doubles + 1;
9917 return lasttoken = xxreadtoken_tokens[p - xxreadtoken_chars];
9925 #define RETURN(token) return lasttoken = token
9927 static int xxreadtoken()
9939 startlinno = plinno;
9940 for (;;) { /* until token or start of word found */
9945 #ifdef CONFIG_ASH_ALIAS
9950 while ((c = pgetc()) != '\n' && c != PEOF);
9954 if (pgetc() == '\n') {
9955 startlinno = ++plinno;
9966 needprompt = doprompt;
9994 return readtoken1(c, BASESYNTAX, (char *) NULL, 0);
10000 * If eofmark is NULL, read a word or a redirection symbol. If eofmark
10001 * is not NULL, read a here document. In the latter case, eofmark is the
10002 * word which marks the end of the document and striptabs is true if
10003 * leading tabs should be stripped from the document. The argument firstc
10004 * is the first character of the input token or document.
10006 * Because C does not have internal subroutines, I have simulated them
10007 * using goto's to implement the subroutine linkage. The following macros
10008 * will run code that appears at the end of readtoken1.
10011 #define CHECKEND() {goto checkend; checkend_return:;}
10012 #define PARSEREDIR() {goto parseredir; parseredir_return:;}
10013 #define PARSESUB() {goto parsesub; parsesub_return:;}
10014 #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
10015 #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
10016 #define PARSEARITH() {goto parsearith; parsearith_return:;}
10019 readtoken1(int firstc, int syntax, const char *eofmark, int striptabs)
10024 char line[EOFMARKLEN + 1];
10025 struct nodelist *bqlist;
10028 int varnest; /* levels of variables expansion */
10029 int arinest; /* levels of arithmetic expansion */
10030 int parenlevel; /* levels of parens in arithmetic */
10031 int dqvarnest; /* levels of variables expansion within double quotes */
10033 int prevsyntax; /* syntax before arithmetic */
10036 /* Avoid longjmp clobbering */
10042 (void) &parenlevel;
10045 (void) &prevsyntax;
10049 startlinno = plinno;
10051 if (syntax == DQSYNTAX)
10060 STARTSTACKSTR(out);
10061 loop:{ /* for each line, until end of word */
10062 CHECKEND(); /* set c to PEOF if at end of here document */
10063 for (;;) { /* until end of line or end of word */
10064 CHECKSTRSPACE(3, out); /* permit 3 calls to USTPUTC */
10065 switch (SIT(c, syntax)) {
10066 case CNL: /* '\n' */
10067 if (syntax == BASESYNTAX)
10068 goto endword; /* exit outer loop */
10076 goto loop; /* continue outer loop */
10081 if ((eofmark == NULL || dblquote) && dqvarnest == 0)
10082 USTPUTC(CTLESC, out);
10085 case CBACK: /* backslash */
10088 USTPUTC('\\', out);
10090 } else if (c == '\n') {
10096 if (dblquote && c != '\\' && c != '`' && c != '$'
10097 && (c != '"' || eofmark != NULL))
10098 USTPUTC('\\', out);
10099 if (SIT(c, SQSYNTAX) == CCTL)
10100 USTPUTC(CTLESC, out);
10101 else if (eofmark == NULL)
10102 USTPUTC(CTLQUOTEMARK, out);
10108 if (eofmark == NULL)
10109 USTPUTC(CTLQUOTEMARK, out);
10113 if (eofmark == NULL)
10114 USTPUTC(CTLQUOTEMARK, out);
10119 if (eofmark != NULL && arinest == 0 && varnest == 0) {
10123 syntax = ARISYNTAX;
10125 } else if (eofmark == NULL && dqvarnest == 0) {
10126 syntax = BASESYNTAX;
10132 case CVAR: /* '$' */
10133 PARSESUB(); /* parse substitution */
10135 case CENDVAR: /* '}' */
10138 if (dqvarnest > 0) {
10141 USTPUTC(CTLENDVAR, out);
10146 #ifdef CONFIG_ASH_MATH_SUPPORT
10147 case CLP: /* '(' in arithmetic */
10151 case CRP: /* ')' in arithmetic */
10152 if (parenlevel > 0) {
10156 if (pgetc() == ')') {
10157 if (--arinest == 0) {
10158 USTPUTC(CTLENDARI, out);
10159 syntax = prevsyntax;
10160 if (syntax == DQSYNTAX)
10168 * unbalanced parens
10169 * (don't 2nd guess - no error)
10177 case CBQUOTE: /* '`' */
10181 goto endword; /* exit outer loop */
10186 goto endword; /* exit outer loop */
10187 #ifdef CONFIG_ASH_ALIAS
10197 if (syntax == ARISYNTAX)
10198 synerror("Missing '))'");
10199 if (syntax != BASESYNTAX && !parsebackquote && eofmark == NULL)
10200 synerror("Unterminated quoted string");
10201 if (varnest != 0) {
10202 startlinno = plinno;
10203 synerror("Missing '}'");
10205 USTPUTC('\0', out);
10206 len = out - stackblock();
10207 out = stackblock();
10208 if (eofmark == NULL) {
10209 if ((c == '>' || c == '<')
10210 && quotef == 0 && len <= 2 && (*out == '\0' || is_digit(*out))) {
10212 return lasttoken = TREDIR;
10217 quoteflag = quotef;
10218 backquotelist = bqlist;
10219 grabstackblock(len);
10221 return lasttoken = TWORD;
10222 /* end of readtoken routine */
10227 * Check to see whether we are at the end of the here document. When this
10228 * is called, c is set to the first character of the next input line. If
10229 * we are at the end of the here document, this routine sets the c to PEOF.
10234 #ifdef CONFIG_ASH_ALIAS
10240 while (c == '\t') {
10244 if (c == *eofmark) {
10245 if (pfgets(line, sizeof line) != NULL) {
10249 for (q = eofmark + 1; *q && *p == *q; p++, q++);
10250 if (*p == '\n' && *q == '\0') {
10253 needprompt = doprompt;
10255 pushstring(line, strlen(line), NULL);
10260 goto checkend_return;
10265 * Parse a redirection operator. The variable "out" points to a string
10266 * specifying the fd to be redirected. The variable "c" contains the
10267 * first character of the redirection operator.
10274 np = (union node *) stalloc(sizeof(struct nfile));
10279 np->type = NAPPEND;
10288 } else { /* c == '<' */
10290 switch (c = pgetc()) {
10292 if (sizeof(struct nfile) != sizeof(struct nhere)) {
10293 np = (union node *) stalloc(sizeof(struct nhere));
10297 heredoc = (struct heredoc *) stalloc(sizeof(struct heredoc));
10298 heredoc->here = np;
10299 if ((c = pgetc()) == '-') {
10300 heredoc->striptabs = 1;
10302 heredoc->striptabs = 0;
10308 np->type = NFROMFD;
10312 np->type = NFROMTO;
10322 np->nfile.fd = digit_val(fd);
10324 goto parseredir_return;
10329 * Parse a substitution. At this point, we have read the dollar sign
10330 * and nothing else.
10338 static const char types[] = "}-+?=";
10342 (c != '(' && c != '{' && !is_name(c) && !is_special(c))
10346 } else if (c == '(') { /* $(command) or $((arith)) */
10347 if (pgetc() == '(') {
10354 USTPUTC(CTLVAR, out);
10355 typeloc = out - stackblock();
10356 USTPUTC(VSNORMAL, out);
10357 subtype = VSNORMAL;
10361 if ((c = pgetc()) == '}')
10364 subtype = VSLENGTH;
10368 if (c > PEOA && is_name(c)) {
10372 } while (c > PEOA && is_in_name(c));
10373 } else if (is_digit(c)) {
10377 } while (is_digit(c));
10378 } else if (is_special(c)) {
10382 badsub:synerror("Bad substitution");
10386 if (subtype == 0) {
10391 /*FALLTHROUGH*/ default:
10392 p = strchr(types, c);
10395 subtype = p - types + VSNORMAL;
10402 subtype = c == '#' ? VSTRIMLEFT : VSTRIMRIGHT;
10414 if (dblquote || arinest)
10416 *(stackblock() + typeloc) = subtype | flags;
10417 if (subtype != VSNORMAL) {
10424 goto parsesub_return;
10429 * Called to parse command substitutions. Newstyle is set if the command
10430 * is enclosed inside $(...); nlpp is a pointer to the head of the linked
10431 * list of commands (passed by reference), and savelen is the number of
10432 * characters on the top of the stack which must be preserved.
10436 struct nodelist **nlpp;
10439 char *volatile str;
10440 struct jmploc jmploc;
10441 struct jmploc *volatile savehandler;
10446 (void) &saveprompt;
10449 savepbq = parsebackquote;
10450 if (setjmp(jmploc.loc)) {
10452 parsebackquote = 0;
10453 handler = savehandler;
10454 longjmp(handler->loc, 1);
10458 savelen = out - stackblock();
10460 str = xmalloc(savelen);
10461 memcpy(str, stackblock(), savelen);
10463 savehandler = handler;
10467 /* We must read until the closing backquote, giving special
10468 treatment to some slashes, and then push the string and
10469 reread it as input, interpreting it normally. */
10476 STARTSTACKSTR(pout);
10482 switch (pc = pgetc()) {
10487 if ((pc = pgetc()) == '\n') {
10494 * If eating a newline, avoid putting
10495 * the newline into the new character
10496 * stream (via the STPUTC after the
10501 if (pc != '\\' && pc != '`' && pc != '$'
10502 && (!dblquote || pc != '"'))
10503 STPUTC('\\', pout);
10510 #ifdef CONFIG_ASH_ALIAS
10513 startlinno = plinno;
10514 synerror("EOF in backquote substitution");
10518 needprompt = doprompt;
10527 STPUTC('\0', pout);
10528 psavelen = pout - stackblock();
10529 if (psavelen > 0) {
10530 pstr = grabstackstr(pout);
10531 setinputstring(pstr);
10536 nlpp = &(*nlpp)->next;
10537 *nlpp = (struct nodelist *) stalloc(sizeof(struct nodelist));
10538 (*nlpp)->next = NULL;
10539 parsebackquote = oldstyle;
10542 saveprompt = doprompt;
10549 doprompt = saveprompt;
10551 if (readtoken() != TRP)
10558 * Start reading from old file again, ignoring any pushed back
10559 * tokens left from the backquote parsing
10564 while (stackblocksize() <= savelen)
10566 STARTSTACKSTR(out);
10568 memcpy(out, str, savelen);
10569 STADJUST(savelen, out);
10575 parsebackquote = savepbq;
10576 handler = savehandler;
10577 if (arinest || dblquote)
10578 USTPUTC(CTLBACKQ | CTLQUOTE, out);
10580 USTPUTC(CTLBACKQ, out);
10582 goto parsebackq_oldreturn;
10584 goto parsebackq_newreturn;
10588 * Parse an arithmetic expansion (indicate start of one and set state)
10592 if (++arinest == 1) {
10593 prevsyntax = syntax;
10594 syntax = ARISYNTAX;
10595 USTPUTC(CTLARI, out);
10602 * we collapse embedded arithmetic expansion to
10603 * parenthesis, which should be equivalent
10607 goto parsearith_return;
10610 } /* end of readtoken */
10614 * Returns true if the text contains nothing to expand (no dollar signs
10618 static int noexpand(char *text)
10624 while ((c = *p++) != '\0') {
10625 if (c == CTLQUOTEMARK)
10629 else if (SIT(c, BASESYNTAX) == CCTL)
10637 * Return true if the argument is a legal variable name (a letter or
10638 * underscore followed by zero or more letters, underscores, and digits).
10641 static int goodname(const char *name)
10649 if (!is_in_name(*p))
10657 * Called when an unexpected token is read during the parse. The argument
10658 * is the token that is expected, or -1 if more than one type of token can
10659 * occur at this point.
10662 static void synexpect(int token)
10667 l = sprintf(msg, "%s unexpected", tokname(lasttoken));
10669 sprintf(msg + l, " (expecting %s)", tokname(token));
10675 static void synerror(const char *msg)
10678 out2fmt("%s: %d: ", commandname, startlinno);
10679 out2fmt("Syntax error: %s\n", msg);
10680 error((char *) NULL);
10686 * called by editline -- any expansions to the prompt
10687 * should be added here.
10689 static void setprompt(int whichprompt)
10693 switch (whichprompt) {
10708 * Code for dealing with input/output redirection.
10711 #define EMPTY -2 /* marks an unused slot in redirtab */
10713 # define PIPESIZE 4096 /* amount of buffering in a pipe */
10715 # define PIPESIZE PIPE_BUF
10720 * Open a file in noclobber mode.
10721 * The code was copied from bash.
10723 static inline int noclobberopen(const char *fname)
10726 struct stat finfo, finfo2;
10729 * If the file exists and is a regular file, return an error
10732 r = stat(fname, &finfo);
10733 if (r == 0 && S_ISREG(finfo.st_mode)) {
10739 * If the file was not present (r != 0), make sure we open it
10740 * exclusively so that if it is created before we open it, our open
10741 * will fail. Make sure that we do not truncate an existing file.
10742 * Note that we don't turn on O_EXCL unless the stat failed -- if the
10743 * file was not a regular file, we leave O_EXCL off.
10746 return open(fname, O_WRONLY | O_CREAT | O_EXCL, 0666);
10747 fd = open(fname, O_WRONLY | O_CREAT, 0666);
10749 /* If the open failed, return the file descriptor right away. */
10754 * OK, the open succeeded, but the file may have been changed from a
10755 * non-regular file to a regular file between the stat and the open.
10756 * We are assuming that the O_EXCL open handles the case where FILENAME
10757 * did not exist and is symlinked to an existing file between the stat
10762 * If we can open it and fstat the file descriptor, and neither check
10763 * revealed that it was a regular file, and the file has not been
10764 * replaced, return the file descriptor.
10766 if (fstat(fd, &finfo2) == 0 && !S_ISREG(finfo2.st_mode) &&
10767 finfo.st_dev == finfo2.st_dev && finfo.st_ino == finfo2.st_ino)
10770 /* The file has been replaced. badness. */
10777 * Handle here documents. Normally we fork off a process to write the
10778 * data to a pipe. If the document is short, we can stuff the data in
10779 * the pipe without forking.
10782 static inline int openhere(const union node *redir)
10788 error("Pipe call failed");
10789 if (redir->type == NHERE) {
10790 len = strlen(redir->nhere.doc->narg.text);
10791 if (len <= PIPESIZE) {
10792 xwrite(pip[1], redir->nhere.doc->narg.text, len);
10796 if (forkshell((struct job *) NULL, (union node *) NULL, FORK_NOJOB) == 0) {
10798 signal(SIGINT, SIG_IGN);
10799 signal(SIGQUIT, SIG_IGN);
10800 signal(SIGHUP, SIG_IGN);
10802 signal(SIGTSTP, SIG_IGN);
10804 signal(SIGPIPE, SIG_DFL);
10805 if (redir->type == NHERE)
10806 xwrite(pip[1], redir->nhere.doc->narg.text, len);
10808 expandhere(redir->nhere.doc, pip[1]);
10817 static inline int openredirect(const union node *redir)
10822 switch (redir->nfile.type) {
10824 fname = redir->nfile.expfname;
10825 if ((f = open(fname, O_RDONLY)) < 0)
10829 fname = redir->nfile.expfname;
10830 if ((f = open(fname, O_RDWR | O_CREAT | O_TRUNC, 0666)) < 0)
10834 /* Take care of noclobber mode. */
10836 fname = redir->nfile.expfname;
10837 if ((f = noclobberopen(fname)) < 0)
10842 fname = redir->nfile.expfname;
10844 if ((f = open(fname, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0)
10847 if ((f = creat(fname, 0666)) < 0)
10852 fname = redir->nfile.expfname;
10854 if ((f = open(fname, O_WRONLY | O_CREAT | O_APPEND, 0666)) < 0)
10857 if ((f = open(fname, O_WRONLY)) < 0 && (f = creat(fname, 0666)) < 0)
10859 lseek(f, (off_t) 0, 2);
10866 /* Fall through to eliminate warning. */
10873 f = openhere(redir);
10879 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
10881 error("cannot open %s: %s", fname, errmsg(errno, E_OPEN));
10886 * Process a list of redirection commands. If the REDIR_PUSH flag is set,
10887 * old file descriptors are stashed away so that the redirection can be
10888 * undone by calling popredir. If the REDIR_BACKQ flag is set, then the
10889 * standard output, and the standard error if it becomes a duplicate of
10893 static void redirect(union node *redir, int flags)
10896 struct redirtab *sv = NULL;
10901 int fd1dup = flags & REDIR_BACKQ;; /* stdout `cmd` redir to pipe */
10903 TRACE(("redirect(%s) called\n",
10904 flags & REDIR_PUSH ? "REDIR_PUSH" : "NO_REDIR_PUSH"));
10905 if (flags & REDIR_PUSH) {
10906 sv = xmalloc(sizeof(struct redirtab));
10907 for (i = 0; i < 10; i++)
10908 sv->renamed[i] = EMPTY;
10909 sv->next = redirlist;
10912 for (n = redir; n; n = n->nfile.next) {
10915 if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) &&
10916 n->ndup.dupfd == fd)
10917 continue; /* redirect from/to same file descriptor */
10920 newfd = openredirect(n);
10921 if ((flags & REDIR_PUSH) && sv->renamed[fd] == EMPTY) {
10925 } else if ((i = fcntl(fd, F_DUPFD, 10)) == -1) {
10929 dupredirect(n, newfd, fd1dup);
10939 error("%d: %m", fd);
10945 if (flags & REDIR_PUSH) {
10946 sv->renamed[fd] = i;
10949 } else if (fd != newfd) {
10955 dupredirect(n, newfd, fd1dup);
10961 static void dupredirect(const union node *redir, int f, int fd1dup)
10963 int fd = redir->nfile.fd;
10967 if (redir->nfile.type == NTOFD || redir->nfile.type == NFROMFD) {
10968 if (redir->ndup.dupfd >= 0) { /* if not ">&-" */
10969 if (redir->ndup.dupfd != 1 || fd1dup != 1)
10970 dup_as_newfd(redir->ndup.dupfd, fd);
10976 dup_as_newfd(f, fd);
10985 * Undo the effects of the last redirection.
10988 static void popredir(void)
10990 struct redirtab *rp = redirlist;
10994 for (i = 0; i < 10; i++) {
10995 if (rp->renamed[i] != EMPTY) {
10999 if (rp->renamed[i] >= 0) {
11000 dup_as_newfd(rp->renamed[i], i);
11001 close(rp->renamed[i]);
11005 redirlist = rp->next;
11011 * Discard all saved file descriptors.
11014 static void clearredir(void)
11016 struct redirtab *rp;
11019 for (rp = redirlist; rp; rp = rp->next) {
11020 for (i = 0; i < 10; i++) {
11021 if (rp->renamed[i] >= 0) {
11022 close(rp->renamed[i]);
11024 rp->renamed[i] = EMPTY;
11031 * Copy a file descriptor to be >= to. Returns -1
11032 * if the source file descriptor is closed, EMPTY if there are no unused
11033 * file descriptors left.
11036 static int dup_as_newfd(int from, int to)
11040 newfd = fcntl(from, F_DUPFD, to);
11042 if (errno == EMFILE)
11045 error("%d: %m", from);
11055 static void shtree(union node *, int, char *, FILE *);
11056 static void shcmd(union node *, FILE *);
11057 static void sharg(union node *, FILE *);
11058 static void indent(int, char *, FILE *);
11059 static void trstring(char *);
11063 static void showtree(node * n)
11065 trputs("showtree called\n");
11066 shtree(n, 1, NULL, stdout);
11070 static void shtree(union node *n, int ind, char *pfx, FILE * fp)
11072 struct nodelist *lp;
11078 indent(ind, pfx, fp);
11089 shtree(n->nbinary.ch1, ind, NULL, fp);
11092 shtree(n->nbinary.ch2, ind, NULL, fp);
11100 for (lp = n->npipe.cmdlist; lp; lp = lp->next) {
11105 if (n->npipe.backgnd)
11111 fprintf(fp, "<node type %d>", n->type);
11119 static void shcmd(union node *cmd, FILE * fp)
11127 for (np = cmd->ncmd.args; np; np = np->narg.next) {
11133 for (np = cmd->ncmd.redirect; np; np = np->nfile.next) {
11139 if ((np->nfile.type <= NFROMFD) && (np->nfile.type >= NTO)) {
11140 s = redir_strings[np->nfile.type - NTO];
11146 switch (np->nfile.type) {
11181 if (np->nfile.fd != dftfd)
11182 fprintf(fp, "%d", np->nfile.fd);
11184 if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) {
11185 fprintf(fp, "%d", np->ndup.dupfd);
11187 sharg(np->nfile.fname, fp);
11194 static void sharg(union node *arg, FILE * fp)
11197 struct nodelist *bqlist;
11200 if (arg->type != NARG) {
11201 printf("<node type %d>\n", arg->type);
11205 bqlist = arg->narg.backquote;
11206 for (p = arg->narg.text; *p; p++) {
11215 if (subtype == VSLENGTH)
11221 if (subtype & VSNUL)
11224 switch (subtype & VSTYPE) {
11243 case VSTRIMLEFTMAX:
11250 case VSTRIMRIGHTMAX:
11257 printf("<subtype %d>", subtype);
11264 case CTLBACKQ | CTLQUOTE:
11267 shtree(bqlist->n, -1, NULL, fp);
11278 static void indent(int amount, char *pfx, FILE * fp)
11282 for (i = 0; i < amount; i++) {
11283 if (pfx && i == amount - 1)
11292 static int debug = 1;
11294 static int debug = 0;
11298 static void trputc(int c)
11300 if (tracefile == NULL)
11302 putc(c, tracefile);
11307 static void trace(const char *fmt, ...)
11312 if (tracefile != NULL) {
11313 (void) vfprintf(tracefile, fmt, va);
11314 if (strchr(fmt, '\n'))
11315 (void) fflush(tracefile);
11321 static void trputs(const char *s)
11323 if (tracefile == NULL)
11325 fputs(s, tracefile);
11326 if (strchr(s, '\n'))
11331 static void trstring(char *s)
11336 if (tracefile == NULL)
11338 putc('"', tracefile);
11339 for (p = s; *p; p++) {
11362 case CTLVAR + CTLQUOTE:
11368 case CTLBACKQ + CTLQUOTE:
11371 backslash:putc('\\', tracefile);
11372 putc(c, tracefile);
11375 if (*p >= ' ' && *p <= '~')
11376 putc(*p, tracefile);
11378 putc('\\', tracefile);
11379 putc(*p >> 6 & 03, tracefile);
11380 putc(*p >> 3 & 07, tracefile);
11381 putc(*p & 07, tracefile);
11386 putc('"', tracefile);
11390 static void trargs(char **ap)
11392 if (tracefile == NULL)
11397 putc(' ', tracefile);
11399 putc('\n', tracefile);
11405 static void opentrace()
11415 #ifdef not_this_way
11419 if ((p = getenv("HOME")) == NULL) {
11420 if (geteuid() == 0)
11426 strcat(s, "/trace");
11429 strcpy(s, "./trace");
11430 #endif /* not_this_way */
11431 if ((tracefile = wfopen(s, "a")) == NULL)
11434 if ((flags = fcntl(fileno(tracefile), F_GETFL, 0)) >= 0)
11435 fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND);
11437 fputs("\nTracing started.\n", tracefile);
11444 * The trap builtin.
11447 static int trapcmd(int argc, char **argv)
11454 for (signo = 0; signo < NSIG; signo++) {
11455 if (trap[signo] != NULL) {
11459 p = single_quote(trap[signo]);
11460 sn = sys_siglist[signo];
11462 sn = u_signal_names(0, &signo, 0);
11465 printf("trap -- %s %s\n", p, sn);
11477 if ((signo = decode_signal(*ap, 0)) < 0)
11478 error("%s: bad trap", *ap);
11481 if (action[0] == '-' && action[1] == '\0')
11484 action = xstrdup(action);
11487 trap[signo] = action;
11498 * Set the signal handler for the specified signal. The routine figures
11499 * out what it should be set to.
11502 static void setsignal(int signo)
11506 struct sigaction act;
11508 if ((t = trap[signo]) == NULL)
11510 else if (*t != '\0')
11514 if (rootshell && action == S_DFL) {
11517 if (iflag || minusc || sflag == 0)
11533 #ifdef CONFIG_ASH_JOB_CONTROL
11543 t = &sigmode[signo - 1];
11546 * current setting unknown
11548 if (sigaction(signo, 0, &act) == -1) {
11550 * Pretend it worked; maybe we should give a warning
11551 * here, but other shells don't. We don't alter
11552 * sigmode, so that we retry every time.
11556 if (act.sa_handler == SIG_IGN) {
11557 if (mflag && (signo == SIGTSTP ||
11558 signo == SIGTTIN || signo == SIGTTOU)) {
11559 *t = S_IGN; /* don't hard ignore these */
11563 *t = S_RESET; /* force to be set */
11566 if (*t == S_HARD_IGN || *t == action)
11568 act.sa_handler = ((action == S_CATCH) ? onsig
11569 : ((action == S_IGN) ? SIG_IGN : SIG_DFL));
11572 sigemptyset(&act.sa_mask);
11573 sigaction(signo, &act, 0);
11580 static void ignoresig(int signo)
11582 if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) {
11583 signal(signo, SIG_IGN);
11585 sigmode[signo - 1] = S_HARD_IGN;
11593 static void onsig(int signo)
11595 if (signo == SIGINT && trap[SIGINT] == NULL) {
11599 gotsig[signo - 1] = 1;
11605 * Called to execute a trap. Perhaps we should avoid entering new trap
11606 * handlers while we are executing a trap handler.
11609 static void dotrap(void)
11615 for (i = 1;; i++) {
11622 savestatus = exitstatus;
11623 evalstring(trap[i], 0);
11624 exitstatus = savestatus;
11631 * Called to exit the shell.
11634 static void exitshell(int status)
11636 struct jmploc loc1, loc2;
11639 TRACE(("exitshell(%d) pid=%d\n", status, getpid()));
11640 if (setjmp(loc1.loc)) {
11643 if (setjmp(loc2.loc)) {
11647 if ((p = trap[0]) != NULL && *p != '\0') {
11652 handler = &loc2; /* probably unnecessary */
11654 #ifdef CONFIG_ASH_JOB_CONTROL
11657 #ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
11658 if (iflag && rootshell) {
11659 const char *hp = lookupvar("HISTFILE");
11662 save_history ( hp );
11670 static int decode_signal(const char *string, int minsig)
11673 const char *name = u_signal_names(string, &signo, minsig);
11675 return name ? signo : -1;
11678 static struct var **hashvar(const char *);
11679 static void showvars(const char *, int, int);
11680 static struct var **findvar(struct var **, const char *);
11683 * Initialize the varable symbol tables and import the environment
11687 * This routine initializes the builtin variables. It is called when the
11688 * shell is initialized and again when a shell procedure is spawned.
11691 static void initvar()
11693 const struct varinit *ip;
11697 for (ip = varinit; (vp = ip->var) != NULL; ip++) {
11698 if ((vp->flags & VEXPORT) == 0) {
11699 vpp = hashvar(ip->text);
11702 vp->text = xstrdup(ip->text);
11703 vp->flags = ip->flags;
11704 vp->func = ip->func;
11707 #if !defined(CONFIG_FEATURE_COMMAND_EDITING) || !defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
11709 * PS1 depends on uid
11711 if ((vps1.flags & VEXPORT) == 0) {
11712 vpp = hashvar("PS1=$ ");
11715 vps1.text = xstrdup(geteuid()? "PS1=$ " : "PS1=# ");
11716 vps1.flags = VSTRFIXED | VTEXTFIXED;
11722 * Set the value of a variable. The flags argument is ored with the
11723 * flags of the variable. If val is NULL, the variable is unset.
11726 static void setvar(const char *name, const char *val, int flags)
11741 if (!is_in_name(*p)) {
11742 if (*p == '\0' || *p == '=')
11748 namelen = p - name;
11750 error("%.*s: bad variable name", namelen, name);
11751 len = namelen + 2; /* 2 is space for '=' and '\0' */
11755 len += vallen = strlen(val);
11758 nameeq = xmalloc(len);
11759 memcpy(nameeq, name, namelen);
11760 nameeq[namelen] = '=';
11762 memcpy(nameeq + namelen + 1, val, vallen + 1);
11764 nameeq[namelen + 1] = '\0';
11766 setvareq(nameeq, flags);
11773 * Same as setvar except that the variable and value are passed in
11774 * the first argument as name=value. Since the first argument will
11775 * be actually stored in the table, it should not be a string that
11779 static void setvareq(char *s, int flags)
11781 struct var *vp, **vpp;
11784 flags |= (VEXPORT & (((unsigned) (1 - aflag)) - 1));
11785 if ((vp = *findvar(vpp, s))) {
11786 if (vp->flags & VREADONLY) {
11787 size_t len = strchr(s, '=') - s;
11789 error("%.*s: is read only", len, s);
11793 if (vp->func && (flags & VNOFUNC) == 0)
11794 (*vp->func) (strchr(s, '=') + 1);
11796 if ((vp->flags & (VTEXTFIXED | VSTACK)) == 0)
11799 vp->flags &= ~(VTEXTFIXED | VSTACK | VUNSET);
11800 vp->flags |= flags;
11803 #ifdef CONFIG_ASH_MAIL
11805 * We could roll this to a function, to handle it as
11806 * a regular variable function callback, but why bother?
11808 if (iflag && (vp == &vmpath || (vp == &vmail && !mpathset())))
11815 vp = xmalloc(sizeof(*vp));
11826 * Process a linked list of variable assignments.
11829 static void listsetvar(struct strlist *mylist)
11831 struct strlist *lp;
11834 for (lp = mylist; lp; lp = lp->next) {
11835 setvareq(xstrdup(lp->text), 0);
11843 * Find the value of a variable. Returns NULL if not set.
11846 static const char *lookupvar(const char *name)
11850 if ((v = *findvar(hashvar(name), name)) && !(v->flags & VUNSET)) {
11851 return strchr(v->text, '=') + 1;
11859 * Search the environment of a builtin command.
11862 static const char *bltinlookup(const char *name)
11864 const struct strlist *sp;
11866 for (sp = cmdenviron; sp; sp = sp->next) {
11867 if (varequal(sp->text, name))
11868 return strchr(sp->text, '=') + 1;
11870 return lookupvar(name);
11876 * Generate a list of exported variables. This routine is used to construct
11877 * the third argument to execve when executing a program.
11880 static char **environment()
11889 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
11890 for (vp = *vpp; vp; vp = vp->next)
11891 if (vp->flags & VEXPORT)
11894 ep = env = stalloc((nenv + 1) * sizeof *env);
11895 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
11896 for (vp = *vpp; vp; vp = vp->next)
11897 if (vp->flags & VEXPORT)
11906 * Command to list all variables which are set. Currently this command
11907 * is invoked from the set command when the set command is called without
11911 static int showvarscmd(int argc, char **argv)
11913 showvars(nullstr, VUNSET, VUNSET);
11920 * The export and readonly commands.
11923 static int exportcmd(int argc, char **argv)
11928 int flag = argv[0][0] == 'r' ? VREADONLY : VEXPORT;
11931 listsetvar(cmdenviron);
11932 pflag = (nextopt("p") == 'p');
11933 if (argc > 1 && !pflag) {
11934 while ((name = *argptr++) != NULL) {
11935 if ((p = strchr(name, '=')) != NULL) {
11938 if ((vp = *findvar(hashvar(name), name))) {
11943 setvar(name, p, flag);
11947 showvars(argv[0], flag, 0);
11954 * The "local" command.
11957 /* funcnest nonzero if we are currently evaluating a function */
11959 static int localcmd(int argc, char **argv)
11964 error("Not in a function");
11965 while ((name = *argptr++) != NULL) {
11973 * Make a variable a local variable. When a variable is made local, it's
11974 * value and flags are saved in a localvar structure. The saved values
11975 * will be restored when the shell function returns. We handle the name
11976 * "-" as a special case.
11979 static void mklocal(char *name)
11981 struct localvar *lvp;
11986 lvp = xmalloc(sizeof(struct localvar));
11987 if (name[0] == '-' && name[1] == '\0') {
11990 p = xmalloc(sizeof optet_vals);
11991 lvp->text = memcpy(p, optet_vals, sizeof optet_vals);
11994 vpp = hashvar(name);
11995 vp = *findvar(vpp, name);
11997 if (strchr(name, '='))
11998 setvareq(xstrdup(name), VSTRFIXED);
12000 setvar(name, NULL, VSTRFIXED);
12001 vp = *vpp; /* the new variable */
12003 lvp->flags = VUNSET;
12005 lvp->text = vp->text;
12006 lvp->flags = vp->flags;
12007 vp->flags |= VSTRFIXED | VTEXTFIXED;
12008 if (strchr(name, '='))
12009 setvareq(xstrdup(name), 0);
12013 lvp->next = localvars;
12020 * Called after a function returns.
12023 static void poplocalvars()
12025 struct localvar *lvp;
12028 while ((lvp = localvars) != NULL) {
12029 localvars = lvp->next;
12031 if (vp == NULL) { /* $- saved */
12032 memcpy(optet_vals, lvp->text, sizeof optet_vals);
12034 } else if ((lvp->flags & (VUNSET | VSTRFIXED)) == VUNSET) {
12035 (void) unsetvar(vp->text);
12037 if ((vp->flags & VTEXTFIXED) == 0)
12039 vp->flags = lvp->flags;
12040 vp->text = lvp->text;
12047 static int setvarcmd(int argc, char **argv)
12050 return unsetcmd(argc, argv);
12051 else if (argc == 3)
12052 setvar(argv[1], argv[2], 0);
12054 error("List assignment not implemented");
12060 * The unset builtin command. We unset the function before we unset the
12061 * variable to allow a function to be unset when there is a readonly variable
12062 * with the same name.
12065 static int unsetcmd(int argc, char **argv)
12073 while ((i = nextopt("vf")) != '\0') {
12079 if (flg_func == 0 && flg_var == 0)
12082 for (ap = argptr; *ap; ap++) {
12086 ret |= unsetvar(*ap);
12093 * Unset the specified variable.
12096 static int unsetvar(const char *s)
12101 vpp = findvar(hashvar(s), s);
12104 if (vp->flags & VREADONLY)
12107 if (*(strchr(vp->text, '=') + 1) != '\0')
12108 setvar(s, nullstr, 0);
12109 vp->flags &= ~VEXPORT;
12110 vp->flags |= VUNSET;
12111 if ((vp->flags & VSTRFIXED) == 0) {
12112 if ((vp->flags & VTEXTFIXED) == 0)
12127 * Find the appropriate entry in the hash table from the name.
12130 static struct var **hashvar(const char *p)
12132 unsigned int hashval;
12134 hashval = ((unsigned char) *p) << 4;
12135 while (*p && *p != '=')
12136 hashval += (unsigned char) *p++;
12137 return &vartab[hashval % VTABSIZE];
12143 * Returns true if the two strings specify the same varable. The first
12144 * variable name is terminated by '='; the second may be terminated by
12145 * either '=' or '\0'.
12148 static int varequal(const char *p, const char *q)
12150 while (*p == *q++) {
12154 if (*p == '=' && *(q - 1) == '\0')
12159 static void showvars(const char *myprefix, int mask, int xor)
12163 const char *sep = myprefix == nullstr ? myprefix : spcstr;
12165 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
12166 for (vp = *vpp; vp; vp = vp->next) {
12167 if ((vp->flags & mask) ^ xor) {
12171 p = strchr(vp->text, '=') + 1;
12172 len = p - vp->text;
12173 p = single_quote(p);
12175 printf("%s%s%.*s%s\n", myprefix, sep, len, vp->text, p);
12182 static struct var **findvar(struct var **vpp, const char *name)
12184 for (; *vpp; vpp = &(*vpp)->next) {
12185 if (varequal((*vpp)->text, name)) {
12193 * Copyright (c) 1999 Herbert Xu <herbert@debian.org>
12194 * This file contains code for the times builtin.
12196 static int timescmd(int argc, char **argv)
12199 long int clk_tck = sysconf(_SC_CLK_TCK);
12202 printf("%dm%fs %dm%fs\n%dm%fs %dm%fs\n",
12203 (int) (buf.tms_utime / clk_tck / 60),
12204 ((double) buf.tms_utime) / clk_tck,
12205 (int) (buf.tms_stime / clk_tck / 60),
12206 ((double) buf.tms_stime) / clk_tck,
12207 (int) (buf.tms_cutime / clk_tck / 60),
12208 ((double) buf.tms_cutime) / clk_tck,
12209 (int) (buf.tms_cstime / clk_tck / 60),
12210 ((double) buf.tms_cstime) / clk_tck);
12214 #ifdef CONFIG_ASH_MATH_SUPPORT
12215 /* The let builtin. */
12216 int letcmd(int argc, char **argv)
12222 char *tmp, *expression, p[13];
12224 expression = strchr(argv[1], '=');
12226 /* Cannot use 'error()' here, or the return code
12227 * will be incorrect */
12228 out2fmt("sh: let: syntax error: \"%s\"\n", argv[1]);
12231 *expression = '\0';
12232 tmp = ++expression;
12233 result = arith(tmp, &errcode);
12235 /* Cannot use 'error()' here, or the return code
12236 * will be incorrect */
12237 out2fmt("sh: let: ");
12239 out2fmt("divide by zero");
12241 out2fmt("syntax error: \"%s=%s\"\n", argv[1], expression);
12244 snprintf(p, 12, "%ld", result);
12245 setvar(argv[1], xstrdup(p), 0);
12246 } else if (argc >= 3)
12247 synerror("invalid operand");
12255 * Copyright (c) 1989, 1991, 1993, 1994
12256 * The Regents of the University of California. All rights reserved.
12258 * This code is derived from software contributed to Berkeley by
12259 * Kenneth Almquist.
12261 * Redistribution and use in source and binary forms, with or without
12262 * modification, are permitted provided that the following conditions
12264 * 1. Redistributions of source code must retain the above copyright
12265 * notice, this list of conditions and the following disclaimer.
12266 * 2. Redistributions in binary form must reproduce the above copyright
12267 * notice, this list of conditions and the following disclaimer in the
12268 * documentation and/or other materials provided with the distribution.
12270 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
12271 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
12273 * 4. Neither the name of the University nor the names of its contributors
12274 * may be used to endorse or promote products derived from this software
12275 * without specific prior written permission.
12277 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
12278 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
12279 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
12280 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
12281 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
12282 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
12283 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
12284 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
12285 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
12286 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF