Notes on portability, and on when #include <linux/blah> is appropriate.
[oweals/busybox.git] / shell / ash.c
index ef1fe0af72cd43fd1c4ecd5b10fcdd77802a8d44..9eb395fd9b697d8bfc0f51dc3f7373b1cd259e47 100644 (file)
@@ -728,6 +728,9 @@ static const char *tokname(int tok)
 #define is_name(c)      ((c) == '_' || isalpha((unsigned char)(c)))
 #define is_in_name(c)   ((c) == '_' || isalnum((unsigned char)(c)))
 
+/* C99 say: "char" declaration may be signed or unsigned default */
+#define SC2INT(chr2may_be_negative_int) (int)((signed char)chr2may_be_negative_int)
+
 /*
  * is_special(c) evaluates to 1 for c in "!#$*-0123456789?@"; 0 otherwise
  * (assuming ascii char codes, as the original implementation did)
@@ -841,7 +844,7 @@ static int SIT(int c, int syntax)
        return S_I_T[indx][syntax];
 }
 
-#else                                                   /* USE_SIT_FUNCTION */
+#else   /* USE_SIT_FUNCTION */
 
 #define SIT(c, syntax) S_I_T[(int)syntax_index_table[((int)c)+SYNBASE]][syntax]
 
@@ -1142,7 +1145,7 @@ static const char syntax_index_table[258] = {
        /* 257   127      */ CWORD_CWORD_CWORD_CWORD,
 };
 
-#endif                                                  /* USE_SIT_FUNCTION */
+#endif  /* USE_SIT_FUNCTION */
 
 /*      alias.c      */
 
@@ -1399,8 +1402,10 @@ static void unsetfunc(const char *);
 
 #ifdef CONFIG_ASH_MATH_SUPPORT_64
 typedef int64_t arith_t;
+#define arith_t_type (long long)
 #else
 typedef long arith_t;
+#define arith_t_type (long)
 #endif
 
 #ifdef CONFIG_ASH_MATH_SUPPORT
@@ -5244,7 +5249,7 @@ memtodest(const char *p, size_t len, int syntax, int quotes) {
        q = makestrspace(len * 2, q);
 
        while (len--) {
-               int c = *p++;
+               int c = SC2INT(*p++);
                if (!c)
                        continue;
                if (quotes && (SIT(c, syntax) == CCTL || SIT(c, syntax) == CBACK))
@@ -5318,7 +5323,7 @@ numvar:
                        goto param;
                /* fall through */
        case '*':
-               sep = ifsset() ? ifsval()[0] : ' ';
+               sep = ifsset() ? SC2INT(ifsval()[0]) : ' ';
                if (quotes && (SIT(sep, syntax) == CCTL || SIT(sep, syntax) == CBACK))
                        sepq = 1;
 param:
@@ -5920,7 +5925,8 @@ static void pushfile(void);
  * Nul characters in the input are silently discarded.
  */
 
-#define pgetc_as_macro()   (--parsenleft >= 0? *parsenextc++ : preadbuffer())
+
+#define pgetc_as_macro()   (--parsenleft >= 0? SC2INT(*parsenextc++) : preadbuffer())
 
 #ifdef CONFIG_ASH_OPTIMIZE_FOR_SIZE
 #define pgetc_macro() pgetc()
@@ -6085,7 +6091,7 @@ preadbuffer(void)
 #endif
                popstring();
                if (--parsenleft >= 0)
-                       return (*parsenextc++);
+                       return SC2INT(*parsenextc++);
        }
        if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
                return PEOF;
@@ -6137,7 +6143,7 @@ again:
 
        *q = savec;
 
-       return *parsenextc++;
+       return SC2INT(*parsenextc++);
 }
 
 /*
@@ -10128,15 +10134,15 @@ readtoken1(int firstc, int syntax, char *eofmark, int striptabs)
        char *out;
        int len;
        char line[EOFMARKLEN + 1];
-       struct nodelist *bqlist;
-       int quotef;
-       int dblquote;
-       int varnest;    /* levels of variables expansion */
-       int arinest;    /* levels of arithmetic expansion */
-       int parenlevel; /* levels of parens in arithmetic */
-       int dqvarnest;  /* levels of variables expansion within double quotes */
-       int oldstyle;
-       int prevsyntax; /* syntax before arithmetic */
+       struct nodelist *bqlist = 0;
+       int quotef = 0;
+       int dblquote = 0;
+       int varnest = 0;    /* levels of variables expansion */
+       int arinest = 0;    /* levels of arithmetic expansion */
+       int parenlevel = 0; /* levels of parens in arithmetic */
+       int dqvarnest = 0;  /* levels of variables expansion within double quotes */
+       int oldstyle = 0;
+       int prevsyntax = 0; /* syntax before arithmetic */
 #if __GNUC__
        /* Avoid longjmp clobbering */
        (void) &out;
@@ -10195,13 +10201,11 @@ readtoken1(int firstc, int syntax, char *eofmark, int striptabs)
                                        if (doprompt)
                                                setprompt(2);
                                } else {
-                                       if (
-                                               dblquote &&
+                                       if (dblquote &&
                                                c != '\\' && c != '`' &&
                                                c != '$' && (
                                                        c != '"' ||
-                                                       eofmark != NULL
-                                               )
+                                                       eofmark != NULL)
                                        ) {
                                                USTPUTC(CTLESC, out);
                                                USTPUTC('\\', out);
@@ -10561,7 +10565,7 @@ parsebackq: {
        struct jmploc jmploc;
        struct jmploc *volatile savehandler;
        size_t savelen;
-       int saveprompt;
+       int saveprompt = 0;
 #ifdef __GNUC__
        (void) &saveprompt;
 #endif
@@ -11829,8 +11833,9 @@ setinteractive(int on)
 
                                if(!do_banner) {
                                        out1fmt(
-                       "\n\n" BB_BANNER " Built-in shell (ash)\n"
-                       "Enter 'help' for a list of built-in commands.\n\n");
+                       "\n\n%s Built-in shell (ash)\n"
+                       "Enter 'help' for a list of built-in commands.\n\n",
+                                       BB_BANNER);
                                        do_banner++;
                                }
                }
@@ -13378,9 +13383,9 @@ arith_apply(operator op, v_n_t *numstack, v_n_t **numstackptr)
                }
                /* save to shell variable */
 #ifdef CONFIG_ASH_MATH_SUPPORT_64
-               snprintf(buf, sizeof(buf), "%lld", rez);
+               snprintf(buf, sizeof(buf), "%lld", arith_t_type rez);
 #else
-               snprintf(buf, sizeof(buf), "%ld", rez);
+               snprintf(buf, sizeof(buf), "%ld", arith_t_type rez);
 #endif
                setvar(numptr_m1->var, buf, 0);
                /* after saving, make previous value for v++ or v-- */