Cut-n-paste strikes again
[oweals/busybox.git] / shell / ash.c
index 977ae464722b16aa845cff715f44ca0a1f243287..d9ea2b0f3731e6ce4bb21ec665579eb565ba422f 100644 (file)
@@ -174,7 +174,7 @@ static const char not_found_msg[] = "%s: not found";
  * We enclose jmp_buf in a structure so that we can declare pointers to
  * jump locations.  The global variable handler contains the location to
  * jump to when an exception occurs, and the global variable exception
- * contains a code identifying the exeception.  To implement nested
+ * contains a code identifying the exception.  To implement nested
  * exception handlers, the user should save the value of handler on entry
  * to an inner scope, set handler to point to a jmploc structure for the
  * inner scope, and restore handler on exit from the scope.
@@ -212,24 +212,24 @@ static volatile sig_atomic_t pendingsigs;
  * more fun than worrying about efficiency and portability. :-))
  */
 
-#define barrier() ({ __asm__ __volatile__ ("": : :"memory"); })
+#define xbarrier() ({ __asm__ __volatile__ ("": : :"memory"); })
 #define INTOFF \
        ({ \
                suppressint++; \
-               barrier(); \
+               xbarrier(); \
                0; \
        })
 #define SAVEINT(v) ((v) = suppressint)
 #define RESTOREINT(v) \
        ({ \
-               barrier(); \
+               xbarrier(); \
                if ((suppressint = (v)) == 0 && intpending) onint(); \
                0; \
        })
 #define EXSIGON() \
        ({ \
                exsig++; \
-               barrier(); \
+               xbarrier(); \
                if (pendingsigs) \
                        exraise(EXSIG); \
                0; \
@@ -263,13 +263,13 @@ static void forceinton(void)
 #else
 #define INTON \
        ({ \
-               barrier(); \
+               xbarrier(); \
                if (--suppressint == 0 && intpending) onint(); \
                0; \
        })
 #define FORCEINTON \
        ({ \
-               barrier(); \
+               xbarrier(); \
                suppressint = 0; \
                if (intpending) onint(); \
                0; \
@@ -624,7 +624,7 @@ static const char homestr[] = "HOME";
 #define __builtin_expect(x, expected_value) (x)
 #endif
 
-#define likely(x)       __builtin_expect((x),1)
+#define xlikely(x)       __builtin_expect((x),1)
 
 
 #define TEOF 0
@@ -1445,8 +1445,15 @@ static void changepath(const char *);
 static void defun(char *, union node *);
 static void unsetfunc(const char *);
 
+#ifdef CONFIG_ASH_MATH_SUPPORT_64
+typedef int64_t arith_t;
+#else
+typedef long arith_t;
+#endif
+
 #ifdef CONFIG_ASH_MATH_SUPPORT
-static long dash_arith(const char *);
+static arith_t dash_arith(const char *);
+static arith_t arith(const char *expr, int *perrcode);
 #endif
 
 #ifdef CONFIG_ASH_RANDOM_SUPPORT
@@ -2309,6 +2316,7 @@ cdcmd(int argc, char **argv)
                dest = bltinlookup(homestr);
        else if (dest[0] == '-' && dest[1] == '\0') {
                dest = bltinlookup("OLDPWD");
+               if ( !dest ) goto out;
                flags |= CD_PRINT;
                goto step7;
        }
@@ -2702,7 +2710,7 @@ static const struct builtincmd bltin = {
  */
 
 /*
- * The eval commmand.
+ * The eval command.
  */
 
 static int
@@ -4531,7 +4539,7 @@ static void ifsfree(void);
 static void expandmeta(struct strlist *, int);
 static int patmatch(char *, const char *);
 
-static int cvtnum(long);
+static int cvtnum(arith_t);
 static size_t esclen(const char *, const char *);
 static char *scanleft(char *, char *, char *, char *, int, int);
 static char *scanright(char *, char *, char *, char *, int, int);
@@ -5902,12 +5910,16 @@ casematch(union node *pattern, char *val)
  */
 
 static int
-cvtnum(long num)
+cvtnum(arith_t num)
 {
        int len;
 
        expdest = makestrspace(32, expdest);
+#ifdef CONFIG_ASH_MATH_SUPPORT_64
+       len = fmtstr(expdest, 32, "%lld", (long long) num);
+#else
        len = fmtstr(expdest, 32, "%ld", num);
+#endif
        STADJUST(len, expdest);
        return len;
 }
@@ -5942,33 +5954,6 @@ varunset(const char *end, const char *var, const char *umsg, int varflags)
 
 static void pushfile(void);
 
-/*
- * Read a line from the script.
- */
-
-static inline char *
-pfgets(char *line, int len)
-{
-       char *p = line;
-       int nleft = len;
-       int c;
-
-       while (--nleft > 0) {
-               c = pgetc2();
-               if (c == PEOF) {
-                       if (p == line)
-                               return NULL;
-                       break;
-               }
-               *p++ = c;
-               if (c == '\n')
-                       break;
-       }
-       *p = '\0';
-       return line;
-}
-
-
 /*
  * Read a character from the script, returning PEOF on end of file.
  * Nul characters in the input are silently discarded.
@@ -6013,6 +5998,33 @@ static inline int pgetc2(void)
 }
 #endif
 
+/*
+ * Read a line from the script.
+ */
+
+static inline char *
+pfgets(char *line, int len)
+{
+       char *p = line;
+       int nleft = len;
+       int c;
+
+       while (--nleft > 0) {
+               c = pgetc2();
+               if (c == PEOF) {
+                       if (p == line)
+                               return NULL;
+                       break;
+               }
+               *p++ = c;
+               if (c == '\n')
+                       break;
+       }
+       *p = '\0';
+       return line;
+}
+
+
 
 #ifdef CONFIG_FEATURE_COMMAND_EDITING
 static const char *cmdedit_prompt;
@@ -6039,7 +6051,9 @@ retry:
        if (!iflag || parsefile->fd)
                nr = safe_read(parsefile->fd, buf, BUFSIZ - 1);
        else {
+#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
                cmdedit_path_lookup = pathval();
+#endif
                nr = cmdedit_read_input((char *) cmdedit_prompt, buf);
                if(nr == 0) {
                        /* Ctrl+C presend */
@@ -6051,7 +6065,7 @@ retry:
                        }
                        goto retry;
                }
-               if(nr < 0) {
+               if(nr < 0 && errno == 0) {
                        /* Ctrl+D presend */
                        nr = 0;
                }
@@ -7073,7 +7087,7 @@ growjobtab(void)
                        jq--;
 #define joff(p) ((struct job *)((char *)(p) + l))
 #define jmove(p) (p) = (void *)((char *)(p) + offset)
-                       if (likely(joff(jp)->ps == &jq->ps0))
+                       if (xlikely(joff(jp)->ps == &jq->ps0))
                                jmove(joff(jp)->ps);
                        if (joff(jp)->prev_job)
                                jmove(joff(jp)->prev_job);
@@ -7228,7 +7242,7 @@ forkshell(struct job *jp, union node *n, int mode)
  * the interactive program catches interrupts, the user doesn't want
  * these interrupts to also abort the loop.  The approach we take here
  * is to have the shell ignore interrupt signals while waiting for a
- * forground process to terminate, and then send itself an interrupt
+ * foreground process to terminate, and then send itself an interrupt
  * signal if the child process was terminated by an interrupt signal.
  * Unfortunately, some programs want to do a bit of cleanup and then
  * exit on interrupt; unless these processes terminate themselves by
@@ -11802,7 +11816,7 @@ dotrap(void)
 
        savestatus = exitstatus;
        q = gotsig;
-       while (pendingsigs = 0, barrier(), (p = memchr(q, 1, NSIG - 1))) {
+       while (pendingsigs = 0, xbarrier(), (p = memchr(q, 1, NSIG - 1))) {
                *p = 0;
                p = trap[p - q + 1];
                if (!p)
@@ -11903,6 +11917,7 @@ exitshell(void)
                evalstring(p);
        }
        flushall();
+       setjobctl(0);
 #ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
        if (iflag && rootshell) {
                const char *hp = lookupvar("HISTFILE");
@@ -11932,7 +11947,7 @@ static int vpcmp(const void *, const void *);
 static struct var **findvar(struct var **, const char *);
 
 /*
- * Initialize the varable symbol tables and import the environment
+ * Initialize the variable symbol tables and import the environment
  */
 
 
@@ -12488,10 +12503,10 @@ static int timescmd(int ac, char **av)
 }
 
 #ifdef CONFIG_ASH_MATH_SUPPORT
-static long
+static arith_t
 dash_arith(const char *s)
 {
-       long result;
+       arith_t result;
        int errcode = 0;
 
        INTOFF;
@@ -12523,7 +12538,7 @@ static int
 letcmd(int argc, char **argv)
 {
        char **ap;
-       long i;
+       arith_t i;
 
        ap = argv + 1;
        if(!*ap)
@@ -12539,7 +12554,7 @@ letcmd(int argc, char **argv)
 /*      $NetBSD: miscbltin.c,v 1.31 2002/11/24 22:35:41 christos Exp $  */
 
 /*
- * Miscelaneous builtins.
+ * Miscellaneous builtins.
  */
 
 #undef rflag
@@ -12911,14 +12926,14 @@ ulimitcmd(int argc, char **argv)
 
 /* This is my infix parser/evaluator. It is optimized for size, intended
  * as a replacement for yacc-based parsers. However, it may well be faster
- * than a comparable parser writen in yacc. The supported operators are
+ * than a comparable parser written in yacc. The supported operators are
  * listed in #defines below. Parens, order of operations, and error handling
- * are supported. This code is threadsafe. The exact expression format should
+ * are supported. This code is thread safe. The exact expression format should
  * be that which POSIX specifies for shells. */
 
 /* The code uses a simple two-stack algorithm. See
  * http://www.onthenet.com.au/~grahamis/int2008/week02/lect02.html
- * for a detailed explaination of the infix-to-postfix algorithm on which
+ * for a detailed explanation of the infix-to-postfix algorithm on which
  * this is based (this code differs in that it applies operators immediately
  * to the stack instead of adding them to a queue to end up with an
  * expression). */
@@ -12948,7 +12963,7 @@ ulimitcmd(int argc, char **argv)
  *    parens and then checking that all binary ops and right parens are
  *    preceded by a valid expression (NUM_TOKEN).
  *
- * Note: It may be desireable to replace Aaron's test for whitespace with
+ * Note: It may be desirable to replace Aaron's test for whitespace with
  * ctype's isspace() if it is used by another busybox applet or if additional
  * whitespace chars should be considered.  Look below the "#include"s for a
  * precompiler test.
@@ -12974,7 +12989,7 @@ ulimitcmd(int argc, char **argv)
  * - realize comma separated - expr, expr
  * - realise ++expr --expr expr++ expr--
  * - realise expr ? expr : expr (but, second expr calculate always)
- * - allow hexdecimal and octal numbers
+ * - allow hexadecimal and octal numbers
  * - was restored loses XOR operator
  * - remove one goto label, added three ;-)
  * - protect $((num num)) as true zero expr (Manuel`s error)
@@ -12989,7 +13004,7 @@ ulimitcmd(int argc, char **argv)
 typedef unsigned char operator;
 
 /* An operator's token id is a bit of a bitfield. The lower 5 bits are the
- * precedence, and 3 high bits are an ID unique accross operators of that
+ * precedence, and 3 high bits are an ID unique across operators of that
  * precedence. The ID portion is so that multiple operators can have the
  * same precedence, ensuring that the leftmost one is evaluated first.
  * Consider * and /. */
@@ -13094,11 +13109,11 @@ static inline int is_right_associativity(operator prec)
 
 
 typedef struct ARITCH_VAR_NUM {
-       long val;
-       long contidional_second_val;
+       arith_t val;
+       arith_t contidional_second_val;
        char contidional_second_val_initialized;
        char *var;      /* if NULL then is regular number,
-                          else is varable name */
+                          else is variable name */
 } v_n_t;
 
 
@@ -13152,9 +13167,8 @@ static int arith_lookup_val(v_n_t *t)
 static inline int
 arith_apply(operator op, v_n_t *numstack, v_n_t **numstackptr)
 {
-       long numptr_val;
        v_n_t *numptr_m1;
-       long rez;
+       arith_t numptr_val, rez;
        int ret_arith_lookup_val;
 
        if (NUMPTR == numstack) goto err; /* There is no operator that can work
@@ -13257,7 +13271,7 @@ arith_apply(operator op, v_n_t *numstack, v_n_t **numstackptr)
                        if(numptr_val < 0)
                                return -3;      /* exponent less than 0 */
                        else {
-                               long c = 1;
+                               arith_t c = 1;
 
                                if(numptr_val)
                                        while(numptr_val--)
@@ -13280,7 +13294,7 @@ arith_apply(operator op, v_n_t *numstack, v_n_t **numstackptr)
                        goto err;
                }
                /* save to shell variable */
-               sprintf(buf, "%ld", rez);
+               snprintf(buf, sizeof(buf), "%lld", (long long) rez);
                setvar(numptr_m1->var, buf, 0);
                /* after saving, make previous value for v++ or v-- */
                if(op == TOK_POST_INC)
@@ -13343,7 +13357,7 @@ static const char op_tokens[] = {
 #define endexpression &op_tokens[sizeof(op_tokens)-7]
 
 
-extern long arith (const char *expr, int *perrcode)
+static arith_t arith (const char *expr, int *perrcode)
 {
     register char arithval; /* Current character under analysis */
     operator lasttok, op;
@@ -13356,7 +13370,7 @@ extern long arith (const char *expr, int *perrcode)
 
     /* Stack of integers */
     /* The proof that there can be no more than strlen(startbuf)/2+1 integers
-     * in any given correct or incorrect expression is left as an excersize to
+     * in any given correct or incorrect expression is left as an exercise to
      * the reader. */
     v_n_t *numstack = alloca(((datasizes)/2)*sizeof(v_n_t)),
            *numstackptr = numstack;
@@ -13406,7 +13420,7 @@ extern long arith (const char *expr, int *perrcode)
                        goto prologue;
                }
                if((p = endofname(expr)) != expr) {
-                       int var_name_size = (p-expr) + 1;  /* trailing zero */
+                       size_t var_name_size = (p-expr) + 1;  /* trailing zero */
 
                        numstackptr->var = alloca(var_name_size);
                        safe_strncpy(numstackptr->var, expr, var_name_size);
@@ -13418,7 +13432,7 @@ extern long arith (const char *expr, int *perrcode)
                        continue;
                } else if (is_digit(arithval)) {
                        numstackptr->var = NULL;
-                       numstackptr->val = strtol(expr, (char **) &expr, 0);
+                       numstackptr->val = strtoll(expr, (char **) &expr, 0);
                        goto num;
                }
                for(p = op_tokens; ; p++) {