X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=miscutils%2Fdc.c;h=b922a7184d7d2fe2934662fca61b7ad50dd5cef6;hb=d72e804e6db1bd6eb2417961004b4fe33aba9384;hp=f94d6fa6b301c17119cfe0bcabfc6c227c24e880;hpb=8dff01d06a7ebd7330e3a1dd1ba47b3c74ee7dfb;p=oweals%2Fbusybox.git diff --git a/miscutils/dc.c b/miscutils/dc.c index f94d6fa6b..b922a7184 100644 --- a/miscutils/dc.c +++ b/miscutils/dc.c @@ -2,9 +2,24 @@ /* * Licensed under GPLv2 or later, see file LICENSE in this source tree. */ - -#include "libbb.h" -#include +//config:config DC +//config: bool "dc (4.2 kb)" +//config: default y +//config: help +//config: Dc is a reverse-polish desk calculator which supports unlimited +//config: precision arithmetic. +//config: +//config:config FEATURE_DC_LIBM +//config: bool "Enable power and exp functions (requires libm)" +//config: default y +//config: depends on DC +//config: help +//config: Enable power and exp functions. +//config: NOTE: This will require libm to be present for linking. + +//applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP)) + +//kbuild:lib-$(CONFIG_DC) += dc.o //usage:#define dc_trivial_usage //usage: "EXPRESSION..." @@ -29,6 +44,10 @@ //usage: "$ echo 72 9 div 8 mul p | dc\n" //usage: "64\n" +#include "libbb.h" +#include "common_bufsiz.h" +#include + #if 0 typedef unsigned data_t; #define DATA_FMT "" @@ -47,15 +66,22 @@ struct globals { double stack[1]; } FIX_ALIASING; enum { STACK_SIZE = (COMMON_BUFSIZE - offsetof(struct globals, stack)) / sizeof(double) }; -#define G (*(struct globals*)&bb_common_bufsiz1) +#define G (*(struct globals*)bb_common_bufsiz1) #define pointer (G.pointer ) #define base (G.base ) #define stack (G.stack ) #define INIT_G() do { \ + setup_common_bufsiz(); \ base = 10; \ } while (0) +static void check_under(void) +{ + if (pointer == 0) + bb_error_msg_and_die("stack underflow"); +} + static void push(double a) { if (pointer >= STACK_SIZE) @@ -65,8 +91,7 @@ static void push(double a) static double pop(void) { - if (pointer == 0) - bb_error_msg_and_die("stack underflow"); + check_under(); return stack[--pointer]; } @@ -187,6 +212,7 @@ static void print_stack_no_pop(void) static void print_no_pop(void) { + check_under(); print_base(stack[pointer-1]); }