math.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #define strto_arith_t strtoull
  65. #else
  66. typedef long arith_t;
  67. #define ARITH_FMT "%ld"
  68. #define strto_arith_t strtoul
  69. #endif
  70. typedef const char* FAST_FUNC (*arith_var_lookup_t)(const char *name);
  71. typedef void FAST_FUNC (*arith_var_set_t)(const char *name, const char *val);
  72. //typedef const char* FAST_FUNC (*arith_var_endofname_t)(const char *name);
  73. typedef struct arith_state_t {
  74. const char *errmsg;
  75. arith_var_lookup_t lookupvar;
  76. arith_var_set_t setvar;
  77. // arith_var_endofname_t endofname;
  78. void *list_of_recursed_names;
  79. } arith_state_t;
  80. arith_t FAST_FUNC arith(arith_state_t *state, const char *expr);
  81. POP_SAVED_FUNCTION_VISIBILITY
  82. #endif