2009-04-02 10:02:37 +00:00
|
|
|
/* math.h - interface to shell math "library" -- this allows shells to share
|
|
|
|
* the implementation of arithmetic $((...)) expansions.
|
|
|
|
*
|
|
|
|
* This aims to be a POSIX shell math library as documented here:
|
|
|
|
* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_04
|
|
|
|
*
|
|
|
|
* See math.c for internal documentation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* The math library has just one function:
|
|
|
|
*
|
2010-09-16 09:50:46 +00:00
|
|
|
* arith_t arith(arith_state_t *state, const char *expr);
|
2009-04-02 10:02:37 +00:00
|
|
|
*
|
2010-09-13 10:49:03 +00:00
|
|
|
* The expr argument is the math string to parse. All normal expansions must
|
|
|
|
* be done already. i.e. no dollar symbols should be present.
|
2009-04-02 10:02:37 +00:00
|
|
|
*
|
2010-09-13 10:49:03 +00:00
|
|
|
* The state argument is a pointer to a struct of hooks for your shell (see below),
|
2010-09-16 09:50:46 +00:00
|
|
|
* and an error message string (NULL if no error).
|
2009-04-02 10:02:37 +00:00
|
|
|
*
|
2010-09-13 10:49:03 +00:00
|
|
|
* The function returns the answer to the expression. So if you called it
|
|
|
|
* with the expression:
|
|
|
|
* "1 + 2 + 3"
|
|
|
|
* you would obviously get back 6.
|
2009-04-02 10:02:37 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* To add support to a shell, you need to implement three functions:
|
|
|
|
*
|
2010-09-13 10:49:03 +00:00
|
|
|
* lookupvar() - look up and return the value of a variable
|
2009-04-02 10:02:37 +00:00
|
|
|
*
|
2010-09-13 10:49:03 +00:00
|
|
|
* If the shell does:
|
|
|
|
* foo=123
|
|
|
|
* Then the code:
|
|
|
|
* const char *val = lookupvar("foo");
|
|
|
|
* will result in val pointing to "123"
|
2009-04-02 10:02:37 +00:00
|
|
|
*
|
2010-09-13 10:49:03 +00:00
|
|
|
* setvar() - set a variable to some value
|
2009-04-02 10:02:37 +00:00
|
|
|
*
|
2010-09-13 10:49:03 +00:00
|
|
|
* If the arithmetic expansion does something like:
|
|
|
|
* $(( i = 1))
|
|
|
|
* then the math code will make a call like so:
|
|
|
|
* setvar("i", "1", 0);
|
|
|
|
* The storage for the first two parameters are not allocated, so your
|
|
|
|
* shell implementation will most likely need to strdup() them to save.
|
2009-04-02 10:02:37 +00:00
|
|
|
*
|
2010-09-13 10:49:03 +00:00
|
|
|
* endofname() - return the end of a variable name from input
|
2009-04-02 10:02:37 +00:00
|
|
|
*
|
2010-09-13 10:49:03 +00:00
|
|
|
* The arithmetic code does not know about variable naming conventions.
|
|
|
|
* So when it is given an experession, it knows something is not numeric,
|
|
|
|
* but it is up to the shell to dictate what is a valid identifiers.
|
|
|
|
* So when it encounters something like:
|
|
|
|
* $(( some_var + 123 ))
|
|
|
|
* It will make a call like so:
|
|
|
|
* end = endofname("some_var + 123");
|
|
|
|
* So the shell needs to scan the input string and return a pointer to the
|
|
|
|
* first non-identifier string. In this case, it should return the input
|
|
|
|
* pointer with an offset pointing to the first space. The typical
|
|
|
|
* implementation will return the offset of first char that does not match
|
|
|
|
* the regex (in C locale): ^[a-zA-Z_][a-zA-Z_0-9]*
|
2009-04-02 10:02:37 +00:00
|
|
|
*/
|
|
|
|
|
2009-04-09 12:35:13 +00:00
|
|
|
#ifndef SHELL_MATH_H
|
|
|
|
#define SHELL_MATH_H 1
|
|
|
|
|
|
|
|
PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
|
2009-04-02 10:02:37 +00:00
|
|
|
|
|
|
|
#if ENABLE_SH_MATH_SUPPORT_64
|
2009-04-03 21:13:31 +00:00
|
|
|
typedef long long arith_t;
|
2010-09-16 09:50:46 +00:00
|
|
|
#define ARITH_FMT "%lld"
|
2009-06-05 14:24:29 +00:00
|
|
|
#define strto_arith_t strtoull
|
2009-04-02 10:02:37 +00:00
|
|
|
#else
|
|
|
|
typedef long arith_t;
|
2010-09-16 09:50:46 +00:00
|
|
|
#define ARITH_FMT "%ld"
|
2009-06-05 14:24:29 +00:00
|
|
|
#define strto_arith_t strtoul
|
2009-04-02 10:02:37 +00:00
|
|
|
#endif
|
|
|
|
|
2010-01-12 22:29:57 +00:00
|
|
|
typedef const char* FAST_FUNC (*arith_var_lookup_t)(const char *name);
|
|
|
|
typedef void FAST_FUNC (*arith_var_set_t)(const char *name, const char *val);
|
2010-09-07 10:19:33 +00:00
|
|
|
//typedef const char* FAST_FUNC (*arith_var_endofname_t)(const char *name);
|
2010-01-12 22:29:57 +00:00
|
|
|
|
2010-09-13 10:49:03 +00:00
|
|
|
typedef struct arith_state_t {
|
2010-09-15 11:33:02 +00:00
|
|
|
const char *errmsg;
|
2010-01-12 22:29:57 +00:00
|
|
|
arith_var_lookup_t lookupvar;
|
|
|
|
arith_var_set_t setvar;
|
2010-09-07 10:19:33 +00:00
|
|
|
// arith_var_endofname_t endofname;
|
2010-09-13 10:49:52 +00:00
|
|
|
void *list_of_recursed_names;
|
2010-09-13 10:49:03 +00:00
|
|
|
} arith_state_t;
|
2009-04-02 10:02:37 +00:00
|
|
|
|
2010-09-13 10:49:52 +00:00
|
|
|
arith_t FAST_FUNC arith(arith_state_t *state, const char *expr);
|
2009-04-02 10:02:37 +00:00
|
|
|
|
2009-04-09 12:35:13 +00:00
|
|
|
POP_SAVED_FUNCTION_VISIBILITY
|
|
|
|
|
2009-04-02 10:02:37 +00:00
|
|
|
#endif
|