1 /*  This file is part of GNU bc.
2 
3     Copyright (C) 1991-1994, 1997, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
4 
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 3 of the License , or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program; see the file COPYING.  If not, see
17     <http://www.gnu.org/licenses>.
18 
19     You may contact the author by:
20        e-mail:  philnelson@acm.org
21       us-mail:  Philip A. Nelson
22                 Computer Science Department, 9062
23                 Western Washington University
24                 Bellingham, WA 98226-9062
25 
26 *************************************************************************/
27 
28 /* bcdefs.h:  The single file to include all constants and type definitions. */
29 
30 /* Include the configuration file. */
31 #include "config.h"
32 
33 /* Standard includes for all files. */
34 #include <stdio.h>
35 #include <sys/types.h>
36 #include <ctype.h>
37 #ifdef HAVE_STRING_H
38 #include <string.h>
39 #else
40 #include <strings.h>
41 #endif
42 #ifdef HAVE_LIMITS_H
43 #include <limits.h>
44 #endif
45 
46 #if defined(LIBEDIT)
47 #include <histedit.h>
48 #endif
49 
50 #if defined(READLINE)
51 #include <readline/readline.h>
52 #include <readline/history.h>
53 #endif
54 
55 /* Initialization magic ... */
56 #ifdef _GLOBAL_C
57 #define EXTERN
58 #define INIT(x) = x
59 #else
60 #define EXTERN extern
61 #define INIT(x)
62 #endif
63 
64 /* Include the other definitions. */
65 #include "const.h"
66 #include "number.h"
67 
68 /* These definitions define all the structures used in
69    code and data storage.  This includes the representation of
70    labels.   The "guiding" principle is to make structures that
71    take a minimum of space when unused but can be built to contain
72    the full structures.  */
73 
74 /* Labels are first.  Labels are generated sequentially in functions
75    and full code.  They just "point" to a single bye in the code.  The
76    "address" is the byte number.  The byte number is used to get an
77    actual character pointer. */
78 
79 typedef struct bc_label_group
80     {
81       unsigned long l_adrs [ BC_LABEL_GROUP ];
82       struct bc_label_group *l_next;
83     } bc_label_group;
84 
85 /* Argument list.  Recorded in the function so arguments can
86    be checked at call time. */
87 
88 typedef struct arg_list
89     {
90       int av_name;
91       int arg_is_var;		/* Extension ... variable parameters. */
92       struct arg_list *next;
93     } arg_list;
94 
95 /* Each function has its own code segments and labels.  There can be
96    no jumps between functions so labels are unique to a function. */
97 
98 typedef struct
99     {
100       char f_defined;   /* Is this function defined yet. */
101       char f_void;	/* Is this function a void function. */
102       char *f_body;
103       size_t f_body_size;  /* Size of body.  Power of 2. */
104       size_t f_code_size;
105       bc_label_group *f_label;
106       arg_list *f_params;
107       arg_list *f_autos;
108     } bc_function;
109 
110 /* Code addresses. */
111 typedef struct {
112       unsigned int pc_func;
113       unsigned int pc_addr;
114     } program_counter;
115 
116 
117 /* Variables are "pushable" (auto) and thus we need a stack mechanism.
118    This is built into the variable record. */
119 
120 typedef struct bc_var
121     {
122       bc_num v_value;
123       struct bc_var *v_next;
124     }  bc_var;
125 
126 
127 /* bc arrays can also be "auto" variables and thus need the same
128    kind of stacking mechanisms. */
129 
130 typedef struct bc_array_node
131     {
132       union
133 	{
134 	  bc_num n_num [NODE_SIZE];
135 	  struct bc_array_node *n_down [NODE_SIZE];
136 	} n_items;
137     } bc_array_node;
138 
139 typedef struct bc_array
140     {
141       bc_array_node *a_tree;
142       short a_depth;
143     } bc_array;
144 
145 typedef struct bc_var_array
146     {
147       bc_array *a_value;
148       char      a_param;
149       struct bc_var_array *a_next;
150     } bc_var_array;
151 
152 
153 /* For the stacks, execution and function, we need records to allow
154    for arbitrary size. */
155 
156 typedef struct estack_rec {
157 	bc_num s_num;
158 	struct estack_rec *s_next;
159 } estack_rec;
160 
161 typedef struct fstack_rec {
162 	int  s_val;
163 	struct fstack_rec *s_next;
164 } fstack_rec;
165 
166 
167 /* The following are for the name tree. */
168 
169 typedef struct id_rec {
170 	char  *id;      /* The program name. */
171 			/* A name == 0 => nothing assigned yet. */
172 	int   a_name;   /* The array variable name (number). */
173 	int   f_name;   /* The function name (number).  */
174 	int   v_name;   /* The variable name (number).  */
175         short balance;  /* For the balanced tree. */
176 	struct id_rec *left, *right; /* Tree pointers. */
177 } id_rec;
178 
179 
180 /* A list of files to process. */
181 
182 typedef struct file_node {
183 	char *name;
184 	struct file_node *next;
185 } file_node;
186 
187 /* Macro Definitions */
188 
189 #if defined(LIBEDIT)
190 #define HISTORY_SIZE(n) history(hist, &histev, H_SETSIZE, n)
191 #define UNLIMIT_HISTORY history(hist, &histev, H_SETSIZE, INT_MAX)
192 #endif
193 
194 #if defined(READLINE)
195 #define HISTORY_SIZE(n) stifle_history(n)
196 #define UNLIMIT_HISTORY unstifle_history()
197 #endif
198 
199 /* Now the global variable declarations. */
200 #include "global.h"
201