math.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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(const char *expr, int *perrcode, arith_eval_hooks_t *hooks);
  12. *
  13. * The first 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 second argument is a semi-detailed error description in case something
  17. * goes wrong in the parsing steps. Currently, those values are (for
  18. * compatibility, you should assume all negative values are errors):
  19. * 0 - no errors (yay!)
  20. * -1 - unspecified problem
  21. * -2 - divide by zero
  22. * -3 - exponent less than 0
  23. * -5 - expression recursion loop detected
  24. *
  25. * The third argument is a struct pointer of hooks for your shell (see below).
  26. *
  27. * The function returns the answer to the expression. So if you called it
  28. * with the expression:
  29. * "1 + 2 + 3"
  30. * You would obviously get back 6.
  31. */
  32. /* To add support to a shell, you need to implement three functions:
  33. *
  34. * lookupvar() - look up and return the value of a variable
  35. *
  36. * If the shell does:
  37. * foo=123
  38. * Then the code:
  39. * const char *val = lookupvar("foo");
  40. * Will result in val pointing to "123"
  41. *
  42. * setvar() - set a variable to some value
  43. *
  44. * If the arithmetic expansion does something like:
  45. * $(( i = 1))
  46. * Then the math code will make a call like so:
  47. * setvar("i", "1", 0);
  48. * The storage for the first two parameters are not allocated, so your
  49. * shell implementation will most likely need to strdup() them to save.
  50. *
  51. * endofname() - return the end of a variable name from input
  52. *
  53. * The arithmetic code does not know about variable naming conventions.
  54. * So when it is given an experession, it knows something is not numeric,
  55. * but it is up to the shell to dictate what is a valid identifiers.
  56. * So when it encounters something like:
  57. * $(( some_var + 123 ))
  58. * It will make a call like so:
  59. * end = endofname("some_var + 123");
  60. * So the shell needs to scan the input string and return a pointer to the
  61. * first non-identifier string. In this case, it should return the input
  62. * pointer with an offset pointing to the first space. The typical
  63. * implementation will return the offset of first char that does not match
  64. * the regex (in C locale): ^[a-zA-Z_][a-zA-Z_0-9]*
  65. */
  66. /* To make your life easier when dealing with optional 64bit math support,
  67. * rather than assume that the type is "signed long" and you can always
  68. * use "%ld" to scan/print the value, use the arith_t helper defines. See
  69. * below for the exact things that are available.
  70. */
  71. #ifndef SHELL_MATH_H
  72. #define SHELL_MATH_H 1
  73. PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
  74. #if ENABLE_SH_MATH_SUPPORT_64
  75. typedef long long arith_t;
  76. #define arith_t_fmt "%lld"
  77. #define strto_arith_t strtoull
  78. #else
  79. typedef long arith_t;
  80. #define arith_t_fmt "%ld"
  81. #define strto_arith_t strtoul
  82. #endif
  83. typedef const char* FAST_FUNC (*arith_var_lookup_t)(const char *name);
  84. typedef void FAST_FUNC (*arith_var_set_t)(const char *name, const char *val);
  85. typedef char* FAST_FUNC (*arith_var_endofname_t)(const char *name);
  86. typedef struct arith_eval_hooks {
  87. arith_var_lookup_t lookupvar;
  88. arith_var_set_t setvar;
  89. arith_var_endofname_t endofname;
  90. } arith_eval_hooks_t;
  91. arith_t arith(const char *expr, int *perrcode, arith_eval_hooks_t*);
  92. POP_SAVED_FUNCTION_VISIBILITY
  93. #endif