source: MondoRescue/branches/2.2.9/mindi-busybox/shell/math.h@ 3320

Last change on this file since 3320 was 3320, checked in by Bruno Cornec, 9 years ago
  • Re-add (thanks git BTW) the 2.2.9 branch which had been destroyed in the move to 3.0
  • Property svn:eol-style set to native
File size: 3.3 KB
Line 
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
64PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
65
66#if ENABLE_SH_MATH_SUPPORT_64
67typedef long long arith_t;
68#define ARITH_FMT "%lld"
69#define strto_arith_t strtoull
70#else
71typedef long arith_t;
72#define ARITH_FMT "%ld"
73#define strto_arith_t strtoul
74#endif
75
76/* ash's and hush's endofname is the same, so... */
77# define is_name(c) ((c) == '_' || isalpha((unsigned char)(c)))
78# define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c)))
79const char* FAST_FUNC endofname(const char *name);
80
81typedef const char* FAST_FUNC (*arith_var_lookup_t)(const char *name);
82typedef void FAST_FUNC (*arith_var_set_t)(const char *name, const char *val);
83//typedef const char* FAST_FUNC (*arith_var_endofname_t)(const char *name);
84
85typedef struct arith_state_t {
86 const char *errmsg;
87 arith_var_lookup_t lookupvar;
88 arith_var_set_t setvar;
89// arith_var_endofname_t endofname;
90 void *list_of_recursed_names;
91} arith_state_t;
92
93arith_t FAST_FUNC arith(arith_state_t *state, const char *expr);
94
95POP_SAVED_FUNCTION_VISIBILITY
96
97#endif
Note: See TracBrowser for help on using the repository browser.