dc.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  4. */
  5. /* config/applet/usage bits are in bc.c */
  6. //#include "libbb.h"
  7. //#include "common_bufsiz.h"
  8. #include <math.h>
  9. #if 0
  10. typedef unsigned data_t;
  11. #define DATA_FMT ""
  12. #elif 0
  13. typedef unsigned long data_t;
  14. #define DATA_FMT "l"
  15. #else
  16. typedef unsigned long long data_t;
  17. #define DATA_FMT "ll"
  18. #endif
  19. struct globals {
  20. unsigned pointer;
  21. unsigned base;
  22. double stack[1];
  23. } FIX_ALIASING;
  24. enum { STACK_SIZE = (COMMON_BUFSIZE - offsetof(struct globals, stack)) / sizeof(double) };
  25. #define G (*(struct globals*)bb_common_bufsiz1)
  26. #define pointer (G.pointer )
  27. #define base (G.base )
  28. #define stack (G.stack )
  29. #define INIT_G() do { \
  30. setup_common_bufsiz(); \
  31. base = 10; \
  32. } while (0)
  33. static unsigned check_under(void)
  34. {
  35. unsigned p = pointer;
  36. if (p == 0)
  37. bb_simple_error_msg_and_die("stack underflow");
  38. return p - 1;
  39. }
  40. static void push(double a)
  41. {
  42. if (pointer >= STACK_SIZE)
  43. bb_simple_error_msg_and_die("stack overflow");
  44. stack[pointer++] = a;
  45. }
  46. static double pop(void)
  47. {
  48. unsigned p = check_under();
  49. pointer = p;
  50. return stack[p];
  51. }
  52. static void add(void)
  53. {
  54. push(pop() + pop());
  55. }
  56. static void sub(void)
  57. {
  58. double subtrahend = pop();
  59. push(pop() - subtrahend);
  60. }
  61. static void mul(void)
  62. {
  63. push(pop() * pop());
  64. }
  65. #if ENABLE_FEATURE_DC_LIBM
  66. static void power(void)
  67. {
  68. double topower = pop();
  69. push(pow(pop(), topower));
  70. }
  71. #endif
  72. static void divide(void)
  73. {
  74. double divisor = pop();
  75. push(pop() / divisor);
  76. }
  77. static void mod(void)
  78. {
  79. data_t d = pop();
  80. /* compat with dc (GNU bc 1.07.1) 1.4.1:
  81. * $ dc -e '4 0 % p'
  82. * dc: remainder by zero
  83. * 0
  84. */
  85. if (d == 0) {
  86. bb_error_msg("remainder by zero");
  87. pop();
  88. push(0);
  89. return;
  90. }
  91. /* ^^^^ without this, we simply get SIGFPE and die */
  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[check_under()]);
  162. }
  163. struct op {
  164. const char name[4];
  165. void (*function) (void);
  166. };
  167. static const struct op operators[] ALIGN_PTR = {
  168. #if ENABLE_FEATURE_DC_LIBM
  169. {"^", power},
  170. // {"exp", power},
  171. // {"pow", power},
  172. #endif
  173. {"%", mod},
  174. // {"mod", mod},
  175. // logic ops are not standard, remove?
  176. {"and", and},
  177. {"or", or},
  178. {"not", not},
  179. {"xor", eor},
  180. {"+", add},
  181. // {"add", add},
  182. {"-", sub},
  183. // {"sub", sub},
  184. {"*", mul},
  185. // {"mul", mul},
  186. {"/", divide},
  187. // {"div", divide},
  188. {"p", print_no_pop},
  189. {"f", print_stack_no_pop},
  190. {"o", set_output_base},
  191. };
  192. /* Feed the stack machine */
  193. static void stack_machine(const char *argument)
  194. {
  195. char *end;
  196. double number;
  197. const struct op *o;
  198. next:
  199. number = strtod(argument, &end);
  200. if (end != argument) {
  201. argument = end;
  202. push(number);
  203. goto next;
  204. }
  205. /* We might have matched a digit, eventually advance the argument */
  206. argument = skip_whitespace(argument);
  207. if (*argument == '\0')
  208. return;
  209. o = operators;
  210. do {
  211. char *after_name = is_prefixed_with(argument, o->name);
  212. if (after_name) {
  213. argument = after_name;
  214. o->function();
  215. goto next;
  216. }
  217. o++;
  218. } while (o != operators + ARRAY_SIZE(operators));
  219. bb_error_msg_and_die("syntax error at '%s'", argument);
  220. }
  221. static void process_file(FILE *fp)
  222. {
  223. char *line;
  224. while ((line = xmalloc_fgetline(fp)) != NULL) {
  225. stack_machine(line);
  226. free(line);
  227. }
  228. }
  229. int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  230. int dc_main(int argc UNUSED_PARAM, char **argv)
  231. {
  232. bool script = 0;
  233. INIT_G();
  234. /* Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs */
  235. for (;;) {
  236. int n = getopt(argc, argv, "e:f:");
  237. if (n <= 0)
  238. break;
  239. switch (n) {
  240. case 'e':
  241. script = 1;
  242. stack_machine(optarg);
  243. break;
  244. case 'f':
  245. script = 1;
  246. process_file(xfopen_for_read(optarg));
  247. break;
  248. default:
  249. bb_show_usage();
  250. }
  251. }
  252. argv += optind;
  253. if (*argv) {
  254. do
  255. process_file(xfopen_for_read(*argv++));
  256. while (*argv);
  257. } else if (!script) {
  258. /* Take stuff from stdin if no args are given */
  259. process_file(stdin);
  260. }
  261. return EXIT_SUCCESS;
  262. }