/* * (C) Copyright 2003 * Tait Electronics Limited, Christchurch, New Zealand * * See file CREDITS for list of people who contributed to this * project. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA */ /* * This file provides an 'eval' function. It is very crude and can only set a variable, not pass * the result directly back to an inline calculation. * It supports binary operators +, -, *, /, &, | */ #include #include #include #if (CONFIG_COMMANDS & CFG_CMD_EVAL) #define PLUS 0 #define MINUS 1 #define TIMES 2 #define DIVIDE 3 #define LAND 4 #define LOR 5 #define LXOR 6 static struct op_tbl_s { char *op; /* operator string */ int opcode; /* internal representation of opcode */ }; typedef struct op_tbl_s op_tbl_t; static op_tbl_t op_table [] = { {"+", PLUS}, {"-", MINUS}, {"*", TIMES}, {"/", DIVIDE}, {"&", LAND}, {"|", LOR}, {"^", LXOR}, }; #define op_tbl_size (sizeof(op_table)/sizeof(op_table[0])) extern int cmd_get_data_size(char* arg, int default_size); static long evalexp(char *s, int w) { long l, *p; /* if the parameter starts with a * then assume is a pointer to the value we want */ if (s[0] == '*') { p = (long *)simple_strtoul(&s[1], NULL, 16); l = *p; } else { l = simple_strtoul(s, NULL, 16); } return (l & ((1 << (w * 8)) - 1)); } static int arith (char *s, char *t, int op, int w) { long l, r; l = evalexp (s, w); r = evalexp (t, w); switch (op) { case PLUS: return (l + r); case MINUS: return (l - r); case TIMES: return (l * r); case DIVIDE: return (l / r); case LAND: return (l & r); case LOR: return (l | r); case LXOR: return (l ^ r); } return (0); } /* command line interface to the shell test */ int do_eval ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] ) { int w, len, i; op_tbl_t *optp; char *op, buf[32]; long value; /* Validate arguments */ if ((argc != 5)){ printf("Usage:\n%s\n", cmdtp->usage); return 1; } /* Check for a data width specification. * Defaults to long (4) if no specification. * Uses -2 as 'width' for .s (string) so as not to upset existing code */ switch (w = cmd_get_data_size(argv[0], 4)) { case 1: case 2: case 4: op = argv[3]; len = strlen(op); for (optp = (op_tbl_t *)&op_table, i = 0; i < op_tbl_size; optp++, i++) { if ((strncmp (op, optp->op, len) == 0) && (len == strlen (optp->op))) { value = arith(argv[2], argv[4], optp->opcode, w); sprintf(buf, "%lx", value); setenv(argv[1], buf); return 0; } } printf("Unknown operator '%s'\n", op); break; case -1: puts("Invalid data width specifier\n"); break; } return 0; } U_BOOT_CMD( eval, 5, 0, do_eval, "eval - set var from arithmetic operation\n", "[.b, .w, .l] var [*]value1 [*]value2\n" ); #endif /* CONFIG_CMD_EVAL */ ======================================================================= This email, including any attachments, is only for the intended addressee. It is subject to copyright, is confidential and may be the subject of legal or other privilege, none of which is waived or lost by reason of this transmission. If the receiver is not the intended addressee, please accept our apologies, notify us by return, delete all copies and perform no other act on the email. Unfortunately, we cannot warrant that the email has not been altered or corrupted during transmission. =======================================================================