diff options
author | Denys Vlasenko <dvlasenk@redhat.com> | 2010-09-13 12:49:03 +0200 |
---|---|---|
committer | Denys Vlasenko <dvlasenk@redhat.com> | 2010-09-13 12:49:03 +0200 |
commit | 06d44d7dfb709bfe02e74d187cceb8591bbda3b4 (patch) | |
tree | 9a2a8e8381cecae29a5c02ed10995d0a0ad9d412 /shell/math.h | |
parent | bd14770b0c8594ce5b0ab9b0b1249b72fc781dd3 (diff) | |
download | busybox-w32-06d44d7dfb709bfe02e74d187cceb8591bbda3b4.tar.gz busybox-w32-06d44d7dfb709bfe02e74d187cceb8591bbda3b4.tar.bz2 busybox-w32-06d44d7dfb709bfe02e74d187cceb8591bbda3b4.zip |
shell/math.c: rename arith_eval_hooks to arith_state, put error code into it
function old new delta
expand_and_evaluate_arith 79 89 +10
arith 675 674 -1
arith_lookup_val 151 142 -9
ash_arith 135 122 -13
arith_apply 1304 1269 -35
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/4 up/down: 10/-58) Total: -48 bytes
Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
Diffstat (limited to 'shell/math.h')
-rw-r--r-- | shell/math.h | 91 |
1 files changed, 45 insertions, 46 deletions
diff --git a/shell/math.h b/shell/math.h index 96088b4d2..9f3da7f59 100644 --- a/shell/math.h +++ b/shell/math.h | |||
@@ -9,61 +9,59 @@ | |||
9 | 9 | ||
10 | /* The math library has just one function: | 10 | /* The math library has just one function: |
11 | * | 11 | * |
12 | * arith_t arith(const char *expr, int *perrcode, arith_eval_hooks_t *hooks); | 12 | * arith_t arith(arith_state_t *states, const char *expr); |
13 | * | 13 | * |
14 | * The first argument is the math string to parse. All normal expansions must | 14 | * The expr argument is the math string to parse. All normal expansions must |
15 | * be done already. i.e. no dollar symbols should be present. | 15 | * be done already. i.e. no dollar symbols should be present. |
16 | * | 16 | * |
17 | * The second argument is a semi-detailed error description in case something | 17 | * The state argument is a pointer to a struct of hooks for your shell (see below), |
18 | * goes wrong in the parsing steps. Currently, those values are (for | 18 | * and a semi-detailed error code. Currently, those values are (for |
19 | * compatibility, you should assume all negative values are errors): | 19 | * compatibility, you should assume all negative values are errors): |
20 | * 0 - no errors (yay!) | 20 | * 0 - no errors (yay!) |
21 | * -1 - unspecified problem | 21 | * -1 - unspecified problem |
22 | * -2 - divide by zero | 22 | * -2 - divide by zero |
23 | * -3 - exponent less than 0 | 23 | * -3 - exponent less than 0 |
24 | * -5 - expression recursion loop detected | 24 | * -5 - expression recursion loop detected |
25 | * | 25 | * |
26 | * The third argument is a struct pointer of hooks for your shell (see below). | 26 | * The function returns the answer to the expression. So if you called it |
27 | * | 27 | * with the expression: |
28 | * The function returns the answer to the expression. So if you called it | 28 | * "1 + 2 + 3" |
29 | * with the expression: | 29 | * you would obviously get back 6. |
30 | * "1 + 2 + 3" | ||
31 | * You would obviously get back 6. | ||
32 | */ | 30 | */ |
33 | 31 | ||
34 | /* To add support to a shell, you need to implement three functions: | 32 | /* To add support to a shell, you need to implement three functions: |
35 | * | 33 | * |
36 | * lookupvar() - look up and return the value of a variable | 34 | * lookupvar() - look up and return the value of a variable |
37 | * | 35 | * |
38 | * If the shell does: | 36 | * If the shell does: |
39 | * foo=123 | 37 | * foo=123 |
40 | * Then the code: | 38 | * Then the code: |
41 | * const char *val = lookupvar("foo"); | 39 | * const char *val = lookupvar("foo"); |
42 | * Will result in val pointing to "123" | 40 | * will result in val pointing to "123" |
43 | * | 41 | * |
44 | * setvar() - set a variable to some value | 42 | * setvar() - set a variable to some value |
45 | * | 43 | * |
46 | * If the arithmetic expansion does something like: | 44 | * If the arithmetic expansion does something like: |
47 | * $(( i = 1)) | 45 | * $(( i = 1)) |
48 | * Then the math code will make a call like so: | 46 | * then the math code will make a call like so: |
49 | * setvar("i", "1", 0); | 47 | * setvar("i", "1", 0); |
50 | * The storage for the first two parameters are not allocated, so your | 48 | * The storage for the first two parameters are not allocated, so your |
51 | * shell implementation will most likely need to strdup() them to save. | 49 | * shell implementation will most likely need to strdup() them to save. |
52 | * | 50 | * |
53 | * endofname() - return the end of a variable name from input | 51 | * endofname() - return the end of a variable name from input |
54 | * | 52 | * |
55 | * The arithmetic code does not know about variable naming conventions. | 53 | * The arithmetic code does not know about variable naming conventions. |
56 | * So when it is given an experession, it knows something is not numeric, | 54 | * So when it is given an experession, it knows something is not numeric, |
57 | * but it is up to the shell to dictate what is a valid identifiers. | 55 | * but it is up to the shell to dictate what is a valid identifiers. |
58 | * So when it encounters something like: | 56 | * So when it encounters something like: |
59 | * $(( some_var + 123 )) | 57 | * $(( some_var + 123 )) |
60 | * It will make a call like so: | 58 | * It will make a call like so: |
61 | * end = endofname("some_var + 123"); | 59 | * end = endofname("some_var + 123"); |
62 | * So the shell needs to scan the input string and return a pointer to the | 60 | * So the shell needs to scan the input string and return a pointer to the |
63 | * first non-identifier string. In this case, it should return the input | 61 | * first non-identifier string. In this case, it should return the input |
64 | * pointer with an offset pointing to the first space. The typical | 62 | * pointer with an offset pointing to the first space. The typical |
65 | * implementation will return the offset of first char that does not match | 63 | * implementation will return the offset of first char that does not match |
66 | * the regex (in C locale): ^[a-zA-Z_][a-zA-Z_0-9]* | 64 | * the regex (in C locale): ^[a-zA-Z_][a-zA-Z_0-9]* |
67 | */ | 65 | */ |
68 | 66 | ||
69 | /* To make your life easier when dealing with optional 64bit math support, | 67 | /* To make your life easier when dealing with optional 64bit math support, |
@@ -96,13 +94,14 @@ typedef const char* FAST_FUNC (*arith_var_lookup_t)(const char *name); | |||
96 | typedef void FAST_FUNC (*arith_var_set_t)(const char *name, const char *val); | 94 | typedef void FAST_FUNC (*arith_var_set_t)(const char *name, const char *val); |
97 | //typedef const char* FAST_FUNC (*arith_var_endofname_t)(const char *name); | 95 | //typedef const char* FAST_FUNC (*arith_var_endofname_t)(const char *name); |
98 | 96 | ||
99 | typedef struct arith_eval_hooks { | 97 | typedef struct arith_state_t { |
100 | arith_var_lookup_t lookupvar; | 98 | arith_var_lookup_t lookupvar; |
101 | arith_var_set_t setvar; | 99 | arith_var_set_t setvar; |
102 | // arith_var_endofname_t endofname; | 100 | // arith_var_endofname_t endofname; |
103 | } arith_eval_hooks_t; | 101 | int errcode; |
102 | } arith_state_t; | ||
104 | 103 | ||
105 | arith_t arith(const char *expr, int *perrcode, arith_eval_hooks_t*); | 104 | arith_t arith(arith_state_t *state, const char *expr); |
106 | 105 | ||
107 | POP_SAVED_FUNCTION_VISIBILITY | 106 | POP_SAVED_FUNCTION_VISIBILITY |
108 | 107 | ||