3
0

dc.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  4. */
  5. #include "libbb.h"
  6. #include <math.h>
  7. //usage:#define dc_trivial_usage
  8. //usage: "EXPRESSION..."
  9. //usage:
  10. //usage:#define dc_full_usage "\n\n"
  11. //usage: "Tiny RPN calculator. Operations:\n"
  12. //usage: "+, add, -, sub, *, mul, /, div, %, mod, "IF_FEATURE_DC_LIBM("**, exp, ")"and, or, not, xor,\n"
  13. //usage: "p - print top of the stack (without popping),\n"
  14. //usage: "f - print entire stack,\n"
  15. //usage: "o - pop the value and set output radix (must be 10, 16, 8 or 2).\n"
  16. //usage: "Examples: 'dc 2 2 add p' -> 4, 'dc 8 8 mul 2 2 + / p' -> 16"
  17. //usage:
  18. //usage:#define dc_example_usage
  19. //usage: "$ dc 2 2 + p\n"
  20. //usage: "4\n"
  21. //usage: "$ dc 8 8 \\* 2 2 + / p\n"
  22. //usage: "16\n"
  23. //usage: "$ dc 0 1 and p\n"
  24. //usage: "0\n"
  25. //usage: "$ dc 0 1 or p\n"
  26. //usage: "1\n"
  27. //usage: "$ echo 72 9 div 8 mul p | dc\n"
  28. //usage: "64\n"
  29. #if 0
  30. typedef unsigned data_t;
  31. #define DATA_FMT ""
  32. #elif 0
  33. typedef unsigned long data_t;
  34. #define DATA_FMT "l"
  35. #else
  36. typedef unsigned long long data_t;
  37. #define DATA_FMT "ll"
  38. #endif
  39. struct globals {
  40. unsigned pointer;
  41. unsigned base;
  42. double stack[1];
  43. } FIX_ALIASING;
  44. enum { STACK_SIZE = (COMMON_BUFSIZE - offsetof(struct globals, stack)) / sizeof(double) };
  45. #define G (*(struct globals*)&bb_common_bufsiz1)
  46. #define pointer (G.pointer )
  47. #define base (G.base )
  48. #define stack (G.stack )
  49. #define INIT_G() do { \
  50. base = 10; \
  51. } while (0)
  52. static void push(double a)
  53. {
  54. if (pointer >= STACK_SIZE)
  55. bb_error_msg_and_die("stack overflow");
  56. stack[pointer++] = a;
  57. }
  58. static double pop(void)
  59. {
  60. if (pointer == 0)
  61. bb_error_msg_and_die("stack underflow");
  62. return stack[--pointer];
  63. }
  64. static void add(void)
  65. {
  66. push(pop() + pop());
  67. }
  68. static void sub(void)
  69. {
  70. double subtrahend = pop();
  71. push(pop() - subtrahend);
  72. }
  73. static void mul(void)
  74. {
  75. push(pop() * pop());
  76. }
  77. #if ENABLE_FEATURE_DC_LIBM
  78. static void power(void)
  79. {
  80. double topower = pop();
  81. push(pow(pop(), topower));
  82. }
  83. #endif
  84. static void divide(void)
  85. {
  86. double divisor = pop();
  87. push(pop() / divisor);
  88. }
  89. static void mod(void)
  90. {
  91. data_t d = pop();
  92. push((data_t) pop() % d);
  93. }
  94. static void and(void)
  95. {
  96. push((data_t) pop() & (data_t) pop());
  97. }
  98. static void or(void)
  99. {
  100. push((data_t) pop() | (data_t) pop());
  101. }
  102. static void eor(void)
  103. {
  104. push((data_t) pop() ^ (data_t) pop());
  105. }
  106. static void not(void)
  107. {
  108. push(~(data_t) pop());
  109. }
  110. static void set_output_base(void)
  111. {
  112. static const char bases[] ALIGN1 = { 2, 8, 10, 16, 0 };
  113. unsigned b = (unsigned)pop();
  114. base = *strchrnul(bases, b);
  115. if (base == 0) {
  116. bb_error_msg("error, base %u is not supported", b);
  117. base = 10;
  118. }
  119. }
  120. static void print_base(double print)
  121. {
  122. data_t x, i;
  123. x = (data_t) print;
  124. if (base == 10) {
  125. if (x == print) /* exactly representable as unsigned integer */
  126. printf("%"DATA_FMT"u\n", x);
  127. else
  128. printf("%g\n", print);
  129. return;
  130. }
  131. switch (base) {
  132. case 16:
  133. printf("%"DATA_FMT"x\n", x);
  134. break;
  135. case 8:
  136. printf("%"DATA_FMT"o\n", x);
  137. break;
  138. default: /* base 2 */
  139. i = MAXINT(data_t) - (MAXINT(data_t) >> 1);
  140. /* i is 100000...00000 */
  141. do {
  142. if (x & i)
  143. break;
  144. i >>= 1;
  145. } while (i > 1);
  146. do {
  147. bb_putchar('1' - !(x & i));
  148. i >>= 1;
  149. } while (i);
  150. bb_putchar('\n');
  151. }
  152. }
  153. static void print_stack_no_pop(void)
  154. {
  155. unsigned i = pointer;
  156. while (i)
  157. print_base(stack[--i]);
  158. }
  159. static void print_no_pop(void)
  160. {
  161. print_base(stack[pointer-1]);
  162. }
  163. struct op {
  164. const char name[4];
  165. void (*function) (void);
  166. };
  167. static const struct op operators[] = {
  168. {"+", add},
  169. {"add", add},
  170. {"-", sub},
  171. {"sub", sub},
  172. {"*", mul},
  173. {"mul", mul},
  174. {"/", divide},
  175. {"div", divide},
  176. #if ENABLE_FEATURE_DC_LIBM
  177. {"**", power},
  178. {"exp", power},
  179. {"pow", power},
  180. #endif
  181. {"%", mod},
  182. {"mod", mod},
  183. {"and", and},
  184. {"or", or},
  185. {"not", not},
  186. {"eor", eor},
  187. {"xor", eor},
  188. {"p", print_no_pop},
  189. {"f", print_stack_no_pop},
  190. {"o", set_output_base},
  191. };
  192. static void stack_machine(const char *argument)
  193. {
  194. char *end;
  195. double d;
  196. const struct op *o;
  197. d = strtod(argument, &end);
  198. if (end != argument && *end == '\0') {
  199. push(d);
  200. return;
  201. }
  202. o = operators;
  203. do {
  204. if (strcmp(o->name, argument) == 0) {
  205. o->function();
  206. return;
  207. }
  208. o++;
  209. } while (o != operators + ARRAY_SIZE(operators));
  210. bb_error_msg_and_die("syntax error at '%s'", argument);
  211. }
  212. int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  213. int dc_main(int argc UNUSED_PARAM, char **argv)
  214. {
  215. INIT_G();
  216. argv++;
  217. if (!argv[0]) {
  218. /* take stuff from stdin if no args are given */
  219. char *line;
  220. char *cursor;
  221. char *token;
  222. while ((line = xmalloc_fgetline(stdin)) != NULL) {
  223. cursor = line;
  224. while (1) {
  225. token = skip_whitespace(cursor);
  226. if (*token == '\0')
  227. break;
  228. cursor = skip_non_whitespace(token);
  229. if (*cursor != '\0')
  230. *cursor++ = '\0';
  231. stack_machine(token);
  232. }
  233. free(line);
  234. }
  235. } else {
  236. // why? it breaks "dc -2 2 + p"
  237. //if (argv[0][0] == '-')
  238. // bb_show_usage();
  239. do {
  240. stack_machine(*argv);
  241. } while (*++argv);
  242. }
  243. return EXIT_SUCCESS;
  244. }