/* $NetBSD: storage.c,v 1.1 2017/04/10 02:28:23 phil Exp $ */
/*
* Copyright (C) 1991-1994, 1997, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
* Copyright (C) 2016-2017 Philip A. Nelson.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The names Philip A. Nelson and Free Software Foundation may not be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY PHILIP A. NELSON ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL PHILIP A. NELSON OR THE FREE SOFTWARE FOUNDATION BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
/* storage.c: Code and data storage manipulations. This includes labels. */
#include "bcdefs.h"
#include "proto.h"
/* Local prototypes */
static bc_array_node *copy_tree (bc_array_node *ary_node, int depth);
static bc_array *copy_array (bc_array *ary);
/* Initialize the storage at the beginning of the run. */
void
init_storage (void)
{
/* Functions: we start with none and ask for more. */
f_count = 0;
more_functions ();
f_names[0] = strdup("(main)");
/* get_array_num returns the address of the bc_num in the array
structure. If more structure is requried to get to the index,
this routine does the work to create that structure. VAR_INDEX
is a zero based index into the arrays storage array. INDEX is
the index into the bc array. */
bc_num *
get_array_num (int var_index, unsigned long idx)
{
bc_var_array *ary_ptr;
bc_array *a_var;
bc_array_node *temp;
int log;
unsigned int ix, ix1;
int sub [NODE_DEPTH];
if (var_name > 3)
{
/* It is a simple variable. */
var_ptr = get_var (var_name);
if (var_ptr != NULL)
{
bc_free_num(&var_ptr->v_value);
var_ptr->v_value = bc_copy_num (ex_stack->s_num);
}
}
else
{
/* It is a special variable... */
toobig = FALSE;
temp = 0;
if (bc_is_neg (ex_stack->s_num))
{
switch (var_name)
{
case 0:
rt_warn ("negative ibase, set to 2");
temp = 2;
break;
case 1:
rt_warn ("negative obase, set to 2");
temp = 2;
break;
case 2:
rt_warn ("negative scale, set to 0");
temp = 0;
break;
#if defined(READLINE) || defined(LIBEDIT)
case 3:
temp = -1;
break;
#endif
}
}
else
{
temp = bc_num2long (ex_stack->s_num);
if (!bc_is_zero (ex_stack->s_num) && temp == 0)
toobig = TRUE;
}
switch (var_name)
{
case 0:
if (temp < 2 && !toobig)
{
i_base = 2;
rt_warn ("ibase too small, set to 2");
}
else
if (temp > 16 || toobig)
{
if (std_only)
{
i_base = 16;
rt_warn ("ibase too large, set to 16");
}
else if (temp > 36 || toobig)
{
i_base = 36;
rt_warn ("ibase too large, set to 36");
}
else
{
if (temp >= 16 && warn_not_std)
rt_warn ("ibase larger than 16 is non-standard");
i_base = temp;
}
}
else
i_base = (int) temp;
break;
case 1:
if (temp < 2 && !toobig)
{
o_base = 2;
rt_warn ("obase too small, set to 2");
}
else
if (temp > BC_BASE_MAX || toobig)
{
o_base = BC_BASE_MAX;
rt_warn ("obase too large, set to %d", BC_BASE_MAX);
}
else
o_base = (int) temp;
break;
case 2:
/* WARNING: The following if statement may generate a compiler
warning if INT_MAX == LONG_MAX. This is NOT a problem. */
if (temp > BC_SCALE_MAX || toobig )
{
scale = BC_SCALE_MAX;
rt_warn ("scale too large, set to %d", BC_SCALE_MAX);
}
else
scale = (int) temp;
break;
#if defined(READLINE) || defined(LIBEDIT)
case 3:
if (toobig)
{
temp = -1;
rt_warn ("history too large, set to unlimited");
UNLIMIT_HISTORY;
}
else
{
n_history = temp;
if (temp < 0)
UNLIMIT_HISTORY;
else
HISTORY_SIZE(n_history);
}
#endif
}
}
}
/* Store the top of the execution stack into array VAR_NAME.
VAR_NAME is the name of an array, and the next to the top
of stack for the index into the array. */
void
store_array (int var_name)
{
bc_num *num_ptr;
long idx;
while (list != NULL)
{
ix = list->av_name;
if (ix > 0)
{
/* A simple variable. */
v_temp = variables[ix];
if (v_temp != NULL)
{
variables[ix] = v_temp->v_next;
bc_free_num (&v_temp->v_value);
free (v_temp);
}
}
else
{
/* An array variable. */
ix = -ix;
a_temp = arrays[ix];
if (a_temp != NULL)
{
arrays[ix] = a_temp->a_next;
if (!a_temp->a_param && a_temp->a_value != NULL)
{
free_a_tree (a_temp->a_value->a_tree,
a_temp->a_value->a_depth);
free (a_temp->a_value);
}
free (a_temp);
}
}
list = list->next;
}
}
/* COPY_NODE: Copies an array node for a call by value parameter. */
static bc_array_node *
copy_tree (bc_array_node *ary_node, int depth)
{
bc_array_node *res = bc_malloc (sizeof(bc_array_node));
int i;
if (depth > 1)
for (i=0; i<NODE_SIZE; i++)
if (ary_node->n_items.n_down[i] != NULL)
res->n_items.n_down[i] =
copy_tree (ary_node->n_items.n_down[i], depth - 1);
else
res->n_items.n_down[i] = NULL;
else
for (i=0; i<NODE_SIZE; i++)
if (ary_node->n_items.n_num[i] != NULL)
res->n_items.n_num[i] = bc_copy_num (ary_node->n_items.n_num[i]);
else
res->n_items.n_num[i] = NULL;
return res;
}
/* COPY_ARRAY: Copies an array for a call by value array parameter.
ARY is the pointer to the bc_array structure. */
/* A call is being made to FUNC. The call types are at PC. Process
the parameters by doing an auto on the parameter variable and then
store the value at the new variable or put a pointer the the array
variable. */
void
process_params (program_counter *progctr, int func)
{
char ch;
arg_list *params;
int ix, ix1;
bc_var *v_temp;
bc_var_array *a_src, *a_dest;
/* Get the parameter names from the function. */
params = functions[func].f_params;
while ((ch = byte(progctr)) != ':')
{
if (params != NULL)
{
if ((ch == '0') && params->av_name > 0)
{
/* A simple variable. */
ix = params->av_name;
v_temp = bc_malloc (sizeof(bc_var));
v_temp->v_next = variables[ix];
v_temp->v_value = ex_stack->s_num;
bc_init_num (&ex_stack->s_num);
variables[ix] = v_temp;
}
else
if ((ch == '1') && (params->av_name < 0))
{
/* The variables is an array variable. */
/* Compute source index and make sure some structure exists. */
ix = (int) bc_num2long (ex_stack->s_num);
(void) get_array_num (ix, 0);
/* Push a new array and Compute Destination index */
auto_var (params->av_name);
ix1 = -params->av_name;
/* Set up the correct pointers in the structure. */
if (ix == ix1)
a_src = arrays[ix]->a_next;
else
a_src = arrays[ix];
a_dest = arrays[ix1];
if (params->arg_is_var)
{
a_dest->a_param = TRUE;
a_dest->a_value = a_src->a_value;
}
else
{
a_dest->a_param = FALSE;
a_dest->a_value = copy_array (a_src->a_value);
}
}
else
{
if (params->av_name < 0)
rt_error ("Parameter type mismatch parameter %s.",
a_names[-params->av_name]);
else
rt_error ("Parameter type mismatch, parameter %s.",
v_names[params->av_name]);
params++;
}
pop ();
}
else
{
rt_error ("Parameter number mismatch");
return;
}
params = params->next;
}
if (params != NULL)
rt_error ("Parameter number mismatch");
}