math.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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_SH_MATH_SUPPORT_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. /* ash's and hush's endofname is the same, so... */
  71. # define is_name(c) ((c) == '_' || isalpha((unsigned char)(c)))
  72. # define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c)))
  73. const char* FAST_FUNC endofname(const char *name);
  74. typedef const char* FAST_FUNC (*arith_var_lookup_t)(const char *name);
  75. typedef void FAST_FUNC (*arith_var_set_t)(const char *name, const char *val);
  76. //typedef const char* FAST_FUNC (*arith_var_endofname_t)(const char *name);
  77. typedef struct arith_state_t {
  78. const char *errmsg;
  79. arith_var_lookup_t lookupvar;
  80. arith_var_set_t setvar;
  81. // arith_var_endofname_t endofname;
  82. void *list_of_recursed_names;
  83. } arith_state_t;
  84. arith_t FAST_FUNC arith(arith_state_t *state, const char *expr);
  85. POP_SAVED_FUNCTION_VISIBILITY
  86. #endif