1 /* math.h - interface to shell math "library" -- this allows shells to share
2 * the implementation of arithmetic $((...)) expansions.
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
7 * See math.c for internal documentation.
9 /* The math library has just one function:
11 * arith_t arith(arith_state_t *state, const char *expr);
13 * The expr argument is the math string to parse. All normal expansions must
14 * be done already. i.e. no dollar symbols should be present.
16 * The state argument is a pointer to a struct of hooks for your shell (see below),
17 * and an error message string (NULL if no error).
19 * The function returns the answer to the expression. So if you called it
20 * with the expression:
22 * you would obviously get back 6.
24 /* To add support to a shell, you need to implement three functions:
26 * lookupvar() - look up and return the value of a variable
31 * const char *val = lookupvar("foo");
32 * will result in val pointing to "123"
34 * setvar() - set a variable to some value
36 * If the arithmetic expansion does something like:
38 * then the math code will make a call like so:
40 * The storage for the first two parameters are not allocated, so your
41 * shell implementation will most likely need to strdup() them to save.
44 #define SHELL_MATH_H 1
46 PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
48 #if ENABLE_FEATURE_SH_MATH_64
49 typedef long long arith_t
;
50 # define ARITH_FMT "%lld"
53 # define ARITH_FMT "%ld"
56 typedef const char* FAST_FUNC (*arith_var_lookup_t
)(const char *name
);
57 typedef void FAST_FUNC (*arith_var_set_t
)(const char *name
, const char *val
);
59 typedef struct arith_state_t
{
60 unsigned evaluation_disabled
;
62 void *list_of_recursed_names
;
63 arith_var_lookup_t lookupvar
;
64 arith_var_set_t setvar
;
67 arith_t FAST_FUNC
arith(arith_state_t
*state
, const char *expr
);
69 POP_SAVED_FUNCTION_VISIBILITY