dc.c 5.1 KB

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