math.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. /* The math library has just one function:
  10. *
  11. * arith_t arith(arith_state_t *state, const char *expr);
  12. *
  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.
  15. *
  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).
  18. *
  19. * The function returns the answer to the expression. So if you called it
  20. * with the expression:
  21. * "1 + 2 + 3"
  22. * you would obviously get back 6.
  23. */
  24. /* To add support to a shell, you need to implement three functions:
  25. *
  26. * lookupvar() - look up and return the value of a variable
  27. *
  28. * If the shell does:
  29. * foo=123
  30. * Then the code:
  31. * const char *val = lookupvar("foo");
  32. * will result in val pointing to "123"
  33. *
  34. * setvar() - set a variable to some value
  35. *
  36. * If the arithmetic expansion does something like:
  37. * $(( i = 1))
  38. * then the math code will make a call like so:
  39. * setvar("i", "1", 0);
  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.
  42. *
  43. * endofname() - return the end of a variable name from input
  44. *
  45. * The arithmetic code does not know about variable naming conventions.
  46. * So when it is given an experession, it knows something is not numeric,
  47. * but it is up to the shell to dictate what is a valid identifiers.
  48. * So when it encounters something like:
  49. * $(( some_var + 123 ))
  50. * It will make a call like so:
  51. * end = endofname("some_var + 123");
  52. * So the shell needs to scan the input string and return a pointer to the
  53. * first non-identifier string. In this case, it should return the input
  54. * pointer with an offset pointing to the first space. The typical
  55. * implementation will return the offset of first char that does not match
  56. * the regex (in C locale): ^[a-zA-Z_][a-zA-Z_0-9]*
  57. */
  58. #ifndef SHELL_MATH_H
  59. #define SHELL_MATH_H 1
  60. PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
  61. #if ENABLE_FEATURE_SH_MATH_64
  62. typedef long long arith_t;
  63. # define ARITH_FMT "%lld"
  64. #else
  65. typedef long arith_t;
  66. # define ARITH_FMT "%ld"
  67. #endif
  68. typedef const char* FAST_FUNC (*arith_var_lookup_t)(const char *name);
  69. typedef void FAST_FUNC (*arith_var_set_t)(const char *name, const char *val);
  70. //typedef const char* FAST_FUNC (*arith_var_endofname_t)(const char *name);
  71. typedef struct arith_state_t {
  72. const char *errmsg;
  73. arith_var_lookup_t lookupvar;
  74. arith_var_set_t setvar;
  75. // arith_var_endofname_t endofname;
  76. void *list_of_recursed_names;
  77. } arith_state_t;
  78. arith_t FAST_FUNC arith(arith_state_t *state, const char *expr);
  79. POP_SAVED_FUNCTION_VISIBILITY
  80. #endif