source: MondoRescue/branches/3.3/mindi-busybox/shell/math.h@ 3865

Last change on this file since 3865 was 3621, checked in by Bruno Cornec, 10 years ago

New 3?3 banch for incorporation of latest busybox 1.25. Changing minor version to handle potential incompatibilities.

  • Property svn:eol-style set to native
File size: 3.0 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
76typedef 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);
78//typedef const char* FAST_FUNC (*arith_var_endofname_t)(const char *name);
79
80typedef struct arith_state_t {
81 const char *errmsg;
82 arith_var_lookup_t lookupvar;
83 arith_var_set_t setvar;
84// arith_var_endofname_t endofname;
85 void *list_of_recursed_names;
86} arith_state_t;
87
88arith_t FAST_FUNC arith(arith_state_t *state, const char *expr);
89
90POP_SAVED_FUNCTION_VISIBILITY
91
92#endif
Note: See TracBrowser for help on using the repository browser.