3
0

dc.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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_simple_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. //TODO: needs setlocale(LC_NUMERIC, "C")?
  200. number = strtod(argument, &end);
  201. if (end != argument) {
  202. argument = end;
  203. push(number);
  204. goto next;
  205. }
  206. /* We might have matched a digit, eventually advance the argument */
  207. argument = skip_whitespace(argument);
  208. if (*argument == '\0')
  209. return;
  210. o = operators;
  211. do {
  212. char *after_name = is_prefixed_with(argument, o->name);
  213. if (after_name) {
  214. argument = after_name;
  215. o->function();
  216. goto next;
  217. }
  218. o++;
  219. } while (o != operators + ARRAY_SIZE(operators));
  220. bb_error_msg_and_die("syntax error at '%s'", argument);
  221. }
  222. static void process_file(FILE *fp)
  223. {
  224. char *line;
  225. while ((line = xmalloc_fgetline(fp)) != NULL) {
  226. stack_machine(line);
  227. free(line);
  228. }
  229. }
  230. int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  231. int dc_main(int argc UNUSED_PARAM, char **argv)
  232. {
  233. bool script = 0;
  234. INIT_G();
  235. /* Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs */
  236. for (;;) {
  237. int n = getopt(argc, argv, "e:f:");
  238. if (n <= 0)
  239. break;
  240. switch (n) {
  241. case 'e':
  242. script = 1;
  243. stack_machine(optarg);
  244. break;
  245. case 'f':
  246. script = 1;
  247. process_file(xfopen_for_read(optarg));
  248. break;
  249. default:
  250. bb_show_usage();
  251. }
  252. }
  253. argv += optind;
  254. if (*argv) {
  255. do
  256. process_file(xfopen_for_read(*argv++));
  257. while (*argv);
  258. } else if (!script) {
  259. /* Take stuff from stdin if no args are given */
  260. process_file(stdin);
  261. }
  262. return EXIT_SUCCESS;
  263. }