Skip to content

Commit

Permalink
Fix #55 on GitHub
Browse files Browse the repository at this point in the history
The --mathlib option was making `bc` ignore the --scale option. This
attempts to fix that.

Signed-off-by: Gavin Howard <[email protected]>
  • Loading branch information
YzenaLLC committed Aug 10, 2022
1 parent cae90a7 commit 6291208
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 32 deletions.
1 change: 0 additions & 1 deletion gen/lib.bc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
*
*/

scale=2*A
define e(x){
auto b,s,n,r,d,i,p,f,v
b=ibase
Expand Down
10 changes: 8 additions & 2 deletions include/args.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,16 @@
* @param argv The array of arguments.
* @param exit_exprs True if bc/dc should exit when there are expressions,
* false otherwise.
* @param scale The current scale.
* @param scale A pointer to return the scale that the arguments set, if
* any.
* @param ibase A pointer to return the ibase that the arguments set, if
* any.
* @param obase A pointer to return the obase that the arguments set, if
* any.
*/
void
bc_args(int argc, char* argv[], bool exit_exprs, BcBigDig scale);
bc_args(int argc, char* argv[], bool exit_exprs, BcBigDig* scale,
BcBigDig* ibase, BcBigDig* obase);

#if BC_ENABLED

Expand Down
30 changes: 5 additions & 25 deletions src/args.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,13 @@ bc_args_redefine(const char* keyword)
#endif // BC_ENABLED

void
bc_args(int argc, char* argv[], bool exit_exprs, BcBigDig scale)
bc_args(int argc, char* argv[], bool exit_exprs, BcBigDig* scale,
BcBigDig* ibase, BcBigDig* obase)
{
int c;
size_t i;
bool do_exit = false, version = false;
BcOpt opts;
BcBigDig newscale = scale, ibase = BC_BASE, obase = BC_BASE;
#if BC_ENABLE_EXTRA_MATH
char* seed = NULL;
#endif // BC_ENABLE_EXTRA_MATH
Expand Down Expand Up @@ -232,7 +232,7 @@ bc_args(int argc, char* argv[], bool exit_exprs, BcBigDig scale)

case 'I':
{
ibase = bc_args_builtin(opts.optarg);
*ibase = bc_args_builtin(opts.optarg);
break;
}

Expand All @@ -250,7 +250,7 @@ bc_args(int argc, char* argv[], bool exit_exprs, BcBigDig scale)

case 'O':
{
obase = bc_args_builtin(opts.optarg);
*obase = bc_args_builtin(opts.optarg);
break;
}

Expand All @@ -268,7 +268,7 @@ bc_args(int argc, char* argv[], bool exit_exprs, BcBigDig scale)

case 'S':
{
newscale = bc_args_builtin(opts.optarg);
*scale = bc_args_builtin(opts.optarg);
break;
}

Expand Down Expand Up @@ -402,24 +402,4 @@ bc_args(int argc, char* argv[], bool exit_exprs, BcBigDig scale)
bc_num_free(&n);
}
#endif // BC_ENABLE_EXTRA_MATH

BC_SIG_UNLOCK;

if (newscale != scale)
{
bc_program_assignBuiltin(&vm->prog, true, false, newscale);
}

if (obase != BC_BASE)
{
bc_program_assignBuiltin(&vm->prog, false, true, obase);
}

// This is last to avoid it affecting the value of the others.
if (ibase != BC_BASE)
{
bc_program_assignBuiltin(&vm->prog, false, false, ibase);
}

BC_SIG_LOCK;
}
52 changes: 48 additions & 4 deletions src/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,16 @@ bc_vm_setenvFlag(const char* const var, int def, uint16_t flag)
/**
* Parses the arguments in {B,D]C_ENV_ARGS.
* @param env_args_name The environment variable to use.
* @param scale A pointer to return the scale that the arguments set,
* if any.
* @param ibase A pointer to return the ibase that the arguments set,
* if any.
* @param obase A pointer to return the obase that the arguments set,
* if any.
*/
static void
bc_vm_envArgs(const char* const env_args_name)
bc_vm_envArgs(const char* const env_args_name, size_t* scale, size_t* ibase,
size_t* obase)
{
char *env_args = bc_vm_getenv(env_args_name), *buf, *start;
char instr = '\0';
Expand Down Expand Up @@ -568,7 +575,7 @@ bc_vm_envArgs(const char* const env_args_name)

// Parse the arguments.
bc_args((int) vm->env_args.len - 1, bc_vec_item(&vm->env_args, 0), false,
BC_PROG_SCALE(&vm->prog));
scale, ibase, obase);
}

/**
Expand Down Expand Up @@ -1510,6 +1517,12 @@ bc_vm_boot(int argc, char* argv[])
const char* const env_clamp = BC_VM_DIGIT_CLAMP_STR;
int env_exit_def = BC_VM_EXPR_EXIT_DEF;
int env_clamp_def = BC_VM_DIGIT_CLAMP_DEF;
size_t scale = SIZE_MAX;
size_t env_scale = SIZE_MAX;
size_t ibase = SIZE_MAX;
size_t env_ibase = SIZE_MAX;
size_t obase = SIZE_MAX;
size_t env_obase = SIZE_MAX;

// We need to know which of stdin, stdout, and stderr are tty's.
ttyin = isatty(STDIN_FILENO);
Expand Down Expand Up @@ -1617,8 +1630,39 @@ bc_vm_boot(int argc, char* argv[])
}

// Process environment and command-line arguments.
bc_vm_envArgs(env_args);
bc_args(argc, argv, true, BC_PROG_SCALE(&vm->prog));
bc_vm_envArgs(env_args, &env_scale, &env_ibase, &env_obase);
bc_args(argc, argv, true, &scale, &ibase, &obase);

// This section is here because we don't want the math library to stomp on
// the user's given value for scale. And we don't want ibase affecting how
// the scale is interpreted. Also, it's sectioned off just for this comment.
{
BC_SIG_UNLOCK;

scale = scale == SIZE_MAX ? env_scale : scale;
// Assign the library value only if it is used and no value was set.
scale = scale == SIZE_MAX && BC_L ? 20 : scale;
obase = obase == SIZE_MAX ? env_obase : obase;
ibase = ibase == SIZE_MAX ? env_ibase : ibase;

if (scale != SIZE_MAX)
{
bc_program_assignBuiltin(&vm->prog, true, false, scale);
}

if (obase != SIZE_MAX)
{
bc_program_assignBuiltin(&vm->prog, false, true, obase);
}

// This is last to avoid it affecting the value of the others.
if (ibase != SIZE_MAX)
{
bc_program_assignBuiltin(&vm->prog, false, false, ibase);
}

BC_SIG_LOCK;
}

// If we are in interactive mode...
if (BC_I)
Expand Down

0 comments on commit 6291208

Please sign in to comment.