1 /* math.h - interface to shell math "library" -- this allows shells to share 2 * the implementation of arithmetic $((...)) expansions. 3 * 4 * This aims to be a POSIX shell math library as documented here: 5 * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_04 6 * 7 * See math.c for internal documentation. 8 */ 9 10 /* The math library has just one function: 11 * 12 * arith_t arith(arith_state_t *state, const char *expr); 13 * 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. 16 * 17 * The state argument is a pointer to a struct of hooks for your shell (see below), 18 * and an error message string (NULL if no error). 19 * 20 * The function returns the answer to the expression. So if you called it 21 * with the expression: 22 * "1 + 2 + 3" 23 * you would obviously get back 6. 24 */ 25 26 /* To add support to a shell, you need to implement three functions: 27 * 28 * lookupvar() - look up and return the value of a variable 29 * 30 * If the shell does: 31 * foo=123 32 * Then the code: 33 * const char *val = lookupvar("foo"); 34 * will result in val pointing to "123" 35 * 36 * setvar() - set a variable to some value 37 * 38 * If the arithmetic expansion does something like: 39 * $(( i = 1)) 40 * then the math code will make a call like so: 41 * setvar("i", "1", 0); 42 * The storage for the first two parameters are not allocated, so your 43 * shell implementation will most likely need to strdup() them to save. 44 * 45 * endofname() - return the end of a variable name from input 46 * 47 * The arithmetic code does not know about variable naming conventions. 48 * So when it is given an experession, it knows something is not numeric, 49 * but it is up to the shell to dictate what is a valid identifiers. 50 * So when it encounters something like: 51 * $(( some_var + 123 )) 52 * It will make a call like so: 53 * end = endofname("some_var + 123"); 54 * So the shell needs to scan the input string and return a pointer to the 55 * first non-identifier string. In this case, it should return the input 56 * pointer with an offset pointing to the first space. The typical 57 * implementation will return the offset of first char that does not match 58 * the regex (in C locale): ^[a-zA-Z_][a-zA-Z_0-9]* 59 */ 60 61 #ifndef SHELL_MATH_H 62 #define SHELL_MATH_H 1 63 64 PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN 65 66 #if ENABLE_FEATURE_SH_MATH_64 67 typedef long long arith_t; 68 # define ARITH_FMT "%lld" 69 #else 70 typedef long arith_t; 71 # define ARITH_FMT "%ld" 72 #endif 73 74 typedef const char* FAST_FUNC (*arith_var_lookup_t)(const char *name); 75 typedef void FAST_FUNC (*arith_var_set_t)(const char *name, const char *val); 76 //typedef const char* FAST_FUNC (*arith_var_endofname_t)(const char *name); 77 78 typedef struct arith_state_t { 79 const char *errmsg; 80 arith_var_lookup_t lookupvar; 81 arith_var_set_t setvar; 82 // arith_var_endofname_t endofname; 83 void *list_of_recursed_names; 84 } arith_state_t; 85 86 arith_t FAST_FUNC arith(arith_state_t *state, const char *expr); 87 88 POP_SAVED_FUNCTION_VISIBILITY 89 90 #endif 91