sh: convert /proc/cpu/aligmnent, /proc/cpu/kernel_alignment to seq_file
[safe/jmp/linux-2.6] / scripts / unifdef.c
index 5384b43..30d459f 100644 (file)
@@ -206,7 +206,7 @@ static void             done(void);
 static void             error(const char *);
 static int              findsym(const char *);
 static void             flushline(bool);
-static Linetype         getline(void);
+static Linetype         get_line(void);
 static Linetype         ifeval(const char **);
 static void             ignoreoff(void);
 static void             ignoreon(void);
@@ -450,7 +450,14 @@ ignoreon(void)
 static void
 keywordedit(const char *replacement)
 {
-       strlcpy(keyword, replacement, tline + sizeof(tline) - keyword);
+       size_t size = tline + sizeof(tline) - keyword;
+       char *dst = keyword;
+       const char *src = replacement;
+       if (size != 0) {
+               while ((--size != 0) && (*src != '\0'))
+                       *dst++ = *src++;
+               *dst = '\0';
+       }
        print();
 }
 static void
@@ -505,7 +512,7 @@ process(void)
 
        for (;;) {
                linenum++;
-               lineval = getline();
+               lineval = get_line();
                trans_table[ifstate[depth]][lineval]();
                debug("process %s -> %s depth %d",
                    linetype_name[lineval],
@@ -519,7 +526,7 @@ process(void)
  * help from skipcomment().
  */
 static Linetype
-getline(void)
+get_line(void)
 {
        const char *cp;
        int cursym;
@@ -671,8 +678,10 @@ eval_unary(const struct ops *ops, int *valp, const char **cpp)
        if (*cp == '!') {
                debug("eval%d !", ops - eval_ops);
                cp++;
-               if (eval_unary(ops, valp, &cp) == LT_IF)
+               if (eval_unary(ops, valp, &cp) == LT_IF) {
+                       *cpp = cp;
                        return (LT_IF);
+               }
                *valp = !*valp;
        } else if (*cp == '(') {
                cp++;
@@ -693,13 +702,16 @@ eval_unary(const struct ops *ops, int *valp, const char **cpp)
                        return (LT_IF);
                cp = skipcomment(cp);
                sym = findsym(cp);
-               if (sym < 0)
-                       return (LT_IF);
-               *valp = (value[sym] != NULL);
                cp = skipsym(cp);
                cp = skipcomment(cp);
                if (*cp++ != ')')
                        return (LT_IF);
+               if (sym >= 0)
+                       *valp = (value[sym] != NULL);
+               else {
+                       *cpp = cp;
+                       return (LT_IF);
+               }
                keepthis = false;
        } else if (!endsym(*cp)) {
                debug("eval%d symbol", ops - eval_ops);
@@ -734,11 +746,11 @@ eval_table(const struct ops *ops, int *valp, const char **cpp)
        const struct op *op;
        const char *cp;
        int val;
+       Linetype lhs, rhs;
 
        debug("eval%d", ops - eval_ops);
        cp = *cpp;
-       if (ops->inner(ops+1, valp, &cp) == LT_IF)
-               return (LT_IF);
+       lhs = ops->inner(ops+1, valp, &cp);
        for (;;) {
                cp = skipcomment(cp);
                for (op = ops->op; op->str != NULL; op++)
@@ -748,14 +760,32 @@ eval_table(const struct ops *ops, int *valp, const char **cpp)
                        break;
                cp += strlen(op->str);
                debug("eval%d %s", ops - eval_ops, op->str);
-               if (ops->inner(ops+1, &val, &cp) == LT_IF)
-                       return (LT_IF);
-               *valp = op->fn(*valp, val);
+               rhs = ops->inner(ops+1, &val, &cp);
+               if (op->fn == op_and && (lhs == LT_FALSE || rhs == LT_FALSE)) {
+                       debug("eval%d: and always false", ops - eval_ops);
+                       if (lhs == LT_IF)
+                               *valp = val;
+                       lhs = LT_FALSE;
+                       continue;
+               }
+               if (op->fn == op_or && (lhs == LT_TRUE || rhs == LT_TRUE)) {
+                       debug("eval%d: or always true", ops - eval_ops);
+                       if (lhs == LT_IF)
+                               *valp = val;
+                       lhs = LT_TRUE;
+                       continue;
+               }
+               if (rhs == LT_IF)
+                       lhs = LT_IF;
+               if (lhs != LT_IF)
+                       *valp = op->fn(*valp, val);
        }
 
        *cpp = cp;
        debug("eval%d = %d", ops - eval_ops, *valp);
-       return (*valp ? LT_TRUE : LT_FALSE);
+       if (lhs != LT_IF)
+               lhs = (*valp ? LT_TRUE : LT_FALSE);
+       return lhs;
 }
 
 /*
@@ -766,12 +796,15 @@ eval_table(const struct ops *ops, int *valp, const char **cpp)
 static Linetype
 ifeval(const char **cpp)
 {
+       const char *cp = *cpp;
        int ret;
        int val;
 
        debug("eval %s", *cpp);
        keepthis = killconsts ? false : true;
-       ret = eval_table(eval_ops, &val, cpp);
+       ret = eval_table(eval_ops, &val, &cp);
+       if (ret != LT_IF)
+               *cpp = cp;
        debug("eval = %d", val);
        return (keepthis ? LT_IF : ret);
 }