blob: 32e1ffe350cbe00157e54a347e9d1463db8f66a5 [file] [log] [blame]
Mike Frysinger98c52642009-04-02 10:02:37 +00001/* 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 *
Denys Vlasenkobed7c812010-09-16 11:50:46 +020012 * arith_t arith(arith_state_t *state, const char *expr);
Mike Frysinger98c52642009-04-02 10:02:37 +000013 *
Denys Vlasenko06d44d72010-09-13 12:49:03 +020014 * 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.
Mike Frysinger98c52642009-04-02 10:02:37 +000016 *
Denys Vlasenko06d44d72010-09-13 12:49:03 +020017 * The state argument is a pointer to a struct of hooks for your shell (see below),
Denys Vlasenkobed7c812010-09-16 11:50:46 +020018 * and an error message string (NULL if no error).
Mike Frysinger98c52642009-04-02 10:02:37 +000019 *
Denys Vlasenko06d44d72010-09-13 12:49:03 +020020 * 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.
Mike Frysinger98c52642009-04-02 10:02:37 +000024 */
25
26/* To add support to a shell, you need to implement three functions:
27 *
Denys Vlasenko06d44d72010-09-13 12:49:03 +020028 * lookupvar() - look up and return the value of a variable
Mike Frysinger98c52642009-04-02 10:02:37 +000029 *
Denys Vlasenko06d44d72010-09-13 12:49:03 +020030 * 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"
Mike Frysinger98c52642009-04-02 10:02:37 +000035 *
Denys Vlasenko06d44d72010-09-13 12:49:03 +020036 * setvar() - set a variable to some value
Mike Frysinger98c52642009-04-02 10:02:37 +000037 *
Denys Vlasenko06d44d72010-09-13 12:49:03 +020038 * 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.
Mike Frysinger98c52642009-04-02 10:02:37 +000044 *
Denys Vlasenko06d44d72010-09-13 12:49:03 +020045 * endofname() - return the end of a variable name from input
Mike Frysinger98c52642009-04-02 10:02:37 +000046 *
Denys Vlasenko06d44d72010-09-13 12:49:03 +020047 * 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]*
Mike Frysinger98c52642009-04-02 10:02:37 +000059 */
60
Denis Vlasenkof81e8db2009-04-09 12:35:13 +000061#ifndef SHELL_MATH_H
62#define SHELL_MATH_H 1
63
64PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
Mike Frysinger98c52642009-04-02 10:02:37 +000065
Denys Vlasenko0b883582016-12-23 16:49:07 +010066#if ENABLE_FEATURE_SH_MATH_64
Denis Vlasenkocc8289d2009-04-03 21:13:31 +000067typedef long long arith_t;
Denys Vlasenkobed7c812010-09-16 11:50:46 +020068#define ARITH_FMT "%lld"
Denys Vlasenko71016ba2009-06-05 16:24:29 +020069#define strto_arith_t strtoull
Mike Frysinger98c52642009-04-02 10:02:37 +000070#else
71typedef long arith_t;
Denys Vlasenkobed7c812010-09-16 11:50:46 +020072#define ARITH_FMT "%ld"
Denys Vlasenko71016ba2009-06-05 16:24:29 +020073#define strto_arith_t strtoul
Mike Frysinger98c52642009-04-02 10:02:37 +000074#endif
75
Denys Vlasenko03dad222010-01-12 23:29:57 +010076typedef const char* FAST_FUNC (*arith_var_lookup_t)(const char *name);
77typedef void FAST_FUNC (*arith_var_set_t)(const char *name, const char *val);
Denys Vlasenko8b2f13d2010-09-07 12:19:33 +020078//typedef const char* FAST_FUNC (*arith_var_endofname_t)(const char *name);
Denys Vlasenko03dad222010-01-12 23:29:57 +010079
Denys Vlasenko06d44d72010-09-13 12:49:03 +020080typedef struct arith_state_t {
Denys Vlasenko063847d2010-09-15 13:33:02 +020081 const char *errmsg;
Denys Vlasenko03dad222010-01-12 23:29:57 +010082 arith_var_lookup_t lookupvar;
83 arith_var_set_t setvar;
Denys Vlasenko8b2f13d2010-09-07 12:19:33 +020084// arith_var_endofname_t endofname;
Denys Vlasenko0eac8ff2010-09-13 12:49:52 +020085 void *list_of_recursed_names;
Denys Vlasenko06d44d72010-09-13 12:49:03 +020086} arith_state_t;
Mike Frysinger98c52642009-04-02 10:02:37 +000087
Denys Vlasenko0eac8ff2010-09-13 12:49:52 +020088arith_t FAST_FUNC arith(arith_state_t *state, const char *expr);
Mike Frysinger98c52642009-04-02 10:02:37 +000089
Denis Vlasenkof81e8db2009-04-09 12:35:13 +000090POP_SAVED_FUNCTION_VISIBILITY
91
Mike Frysinger98c52642009-04-02 10:02:37 +000092#endif