aboutsummaryrefslogtreecommitdiff
path: root/shell/math.h
diff options
context:
space:
mode:
Diffstat (limited to 'shell/math.h')
-rw-r--r--shell/math.h96
1 files changed, 42 insertions, 54 deletions
diff --git a/shell/math.h b/shell/math.h
index 96088b4d2..2d305eb12 100644
--- a/shell/math.h
+++ b/shell/math.h
@@ -9,67 +9,53 @@
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 *state, 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 an error message string (NULL if no error).
19 * compatibility, you should assume all negative values are errors):
20 * 0 - no errors (yay!)
21 * -1 - unspecified problem
22 * -2 - divide by zero
23 * -3 - exponent less than 0
24 * -5 - expression recursion loop detected
25 * 19 *
26 * The third argument is a struct pointer of hooks for your shell (see below). 20 * The function returns the answer to the expression. So if you called it
27 * 21 * with the expression:
28 * The function returns the answer to the expression. So if you called it 22 * "1 + 2 + 3"
29 * with the expression: 23 * you would obviously get back 6.
30 * "1 + 2 + 3"
31 * You would obviously get back 6.
32 */ 24 */
33 25
34/* To add support to a shell, you need to implement three functions: 26/* To add support to a shell, you need to implement three functions:
35 * 27 *
36 * lookupvar() - look up and return the value of a variable 28 * lookupvar() - look up and return the value of a variable
37 * 29 *
38 * If the shell does: 30 * If the shell does:
39 * foo=123 31 * foo=123
40 * Then the code: 32 * Then the code:
41 * const char *val = lookupvar("foo"); 33 * const char *val = lookupvar("foo");
42 * Will result in val pointing to "123" 34 * will result in val pointing to "123"
43 * 35 *
44 * setvar() - set a variable to some value 36 * setvar() - set a variable to some value
45 * 37 *
46 * If the arithmetic expansion does something like: 38 * If the arithmetic expansion does something like:
47 * $(( i = 1)) 39 * $(( i = 1))
48 * Then the math code will make a call like so: 40 * then the math code will make a call like so:
49 * setvar("i", "1", 0); 41 * setvar("i", "1", 0);
50 * The storage for the first two parameters are not allocated, so your 42 * The storage for the first two parameters are not allocated, so your
51 * shell implementation will most likely need to strdup() them to save. 43 * shell implementation will most likely need to strdup() them to save.
52 * 44 *
53 * endofname() - return the end of a variable name from input 45 * endofname() - return the end of a variable name from input
54 * 46 *
55 * The arithmetic code does not know about variable naming conventions. 47 * The arithmetic code does not know about variable naming conventions.
56 * So when it is given an experession, it knows something is not numeric, 48 * 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. 49 * but it is up to the shell to dictate what is a valid identifiers.
58 * So when it encounters something like: 50 * So when it encounters something like:
59 * $(( some_var + 123 )) 51 * $(( some_var + 123 ))
60 * It will make a call like so: 52 * It will make a call like so:
61 * end = endofname("some_var + 123"); 53 * end = endofname("some_var + 123");
62 * So the shell needs to scan the input string and return a pointer to the 54 * 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 55 * first non-identifier string. In this case, it should return the input
64 * pointer with an offset pointing to the first space. The typical 56 * pointer with an offset pointing to the first space. The typical
65 * implementation will return the offset of first char that does not match 57 * 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]* 58 * the regex (in C locale): ^[a-zA-Z_][a-zA-Z_0-9]*
67 */
68
69/* To make your life easier when dealing with optional 64bit math support,
70 * rather than assume that the type is "signed long" and you can always
71 * use "%ld" to scan/print the value, use the arith_t helper defines. See
72 * below for the exact things that are available.
73 */ 59 */
74 60
75#ifndef SHELL_MATH_H 61#ifndef SHELL_MATH_H
@@ -79,11 +65,11 @@ PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
79 65
80#if ENABLE_SH_MATH_SUPPORT_64 66#if ENABLE_SH_MATH_SUPPORT_64
81typedef long long arith_t; 67typedef long long arith_t;
82#define arith_t_fmt "%lld" 68#define ARITH_FMT "%lld"
83#define strto_arith_t strtoull 69#define strto_arith_t strtoull
84#else 70#else
85typedef long arith_t; 71typedef long arith_t;
86#define arith_t_fmt "%ld" 72#define ARITH_FMT "%ld"
87#define strto_arith_t strtoul 73#define strto_arith_t strtoul
88#endif 74#endif
89 75
@@ -96,13 +82,15 @@ typedef const char* FAST_FUNC (*arith_var_lookup_t)(const char *name);
96typedef void FAST_FUNC (*arith_var_set_t)(const char *name, const char *val); 82typedef 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); 83//typedef const char* FAST_FUNC (*arith_var_endofname_t)(const char *name);
98 84
99typedef struct arith_eval_hooks { 85typedef struct arith_state_t {
86 const char *errmsg;
100 arith_var_lookup_t lookupvar; 87 arith_var_lookup_t lookupvar;
101 arith_var_set_t setvar; 88 arith_var_set_t setvar;
102// arith_var_endofname_t endofname; 89// arith_var_endofname_t endofname;
103} arith_eval_hooks_t; 90 void *list_of_recursed_names;
91} arith_state_t;
104 92
105arith_t arith(const char *expr, int *perrcode, arith_eval_hooks_t*); 93arith_t FAST_FUNC arith(arith_state_t *state, const char *expr);
106 94
107POP_SAVED_FUNCTION_VISIBILITY 95POP_SAVED_FUNCTION_VISIBILITY
108 96