xref: /netbsd/external/bsd/bc/dist/bcdefs.h (revision 0716a753)
1*0716a753Sphil /*	$NetBSD: bcdefs.h,v 1.1 2017/04/10 02:28:23 phil Exp $ */
2*0716a753Sphil 
3*0716a753Sphil /*
4*0716a753Sphil  * Copyright (C) 1991-1994, 1997, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
5*0716a753Sphil  * Copyright (C) 2016-2017 Philip A. Nelson.
6*0716a753Sphil  * All rights reserved.
7*0716a753Sphil  *
8*0716a753Sphil  * Redistribution and use in source and binary forms, with or without
9*0716a753Sphil  * modification, are permitted provided that the following conditions
10*0716a753Sphil  * are met:
11*0716a753Sphil  *
12*0716a753Sphil  * 1. Redistributions of source code must retain the above copyright
13*0716a753Sphil  *    notice, this list of conditions and the following disclaimer.
14*0716a753Sphil  * 2. Redistributions in binary form must reproduce the above copyright
15*0716a753Sphil  *    notice, this list of conditions and the following disclaimer in the
16*0716a753Sphil  *    documentation and/or other materials provided with the distribution.
17*0716a753Sphil  * 3. The names Philip A. Nelson and Free Software Foundation may not be
18*0716a753Sphil  *    used to endorse or promote products derived from this software
19*0716a753Sphil  *    without specific prior written permission.
20*0716a753Sphil  *
21*0716a753Sphil  * THIS SOFTWARE IS PROVIDED BY PHILIP A. NELSON ``AS IS'' AND ANY EXPRESS OR
22*0716a753Sphil  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23*0716a753Sphil  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24*0716a753Sphil  * IN NO EVENT SHALL PHILIP A. NELSON OR THE FREE SOFTWARE FOUNDATION BE
25*0716a753Sphil  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26*0716a753Sphil  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27*0716a753Sphil  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28*0716a753Sphil  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29*0716a753Sphil  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30*0716a753Sphil  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31*0716a753Sphil  * THE POSSIBILITY OF SUCH DAMAGE.
32*0716a753Sphil  */
33*0716a753Sphil 
34*0716a753Sphil /* bcdefs.h:  The single file to include all constants and type definitions. */
35*0716a753Sphil 
36*0716a753Sphil /* Include the configuration file. */
37*0716a753Sphil #include "config.h"
38*0716a753Sphil 
39*0716a753Sphil /* Standard includes for all files. */
40*0716a753Sphil #include <stdio.h>
41*0716a753Sphil #include <sys/types.h>
42*0716a753Sphil #include <ctype.h>
43*0716a753Sphil #ifdef HAVE_STRING_H
44*0716a753Sphil #include <string.h>
45*0716a753Sphil #else
46*0716a753Sphil #include <strings.h>
47*0716a753Sphil #endif
48*0716a753Sphil #ifdef HAVE_LIMITS_H
49*0716a753Sphil #include <limits.h>
50*0716a753Sphil #endif
51*0716a753Sphil 
52*0716a753Sphil #if defined(LIBEDIT)
53*0716a753Sphil #include <histedit.h>
54*0716a753Sphil #endif
55*0716a753Sphil 
56*0716a753Sphil #if defined(READLINE)
57*0716a753Sphil #include <readline/readline.h>
58*0716a753Sphil #include <readline/history.h>
59*0716a753Sphil #endif
60*0716a753Sphil 
61*0716a753Sphil /* Initialization magic ... */
62*0716a753Sphil #ifdef _GLOBAL_C
63*0716a753Sphil #define EXTERN
64*0716a753Sphil #define INIT(x) = x
65*0716a753Sphil #else
66*0716a753Sphil #define EXTERN extern
67*0716a753Sphil #define INIT(x)
68*0716a753Sphil #endif
69*0716a753Sphil 
70*0716a753Sphil /* Include the other definitions. */
71*0716a753Sphil #include "const.h"
72*0716a753Sphil #include "number.h"
73*0716a753Sphil 
74*0716a753Sphil /* These definitions define all the structures used in
75*0716a753Sphil    code and data storage.  This includes the representation of
76*0716a753Sphil    labels.   The "guiding" principle is to make structures that
77*0716a753Sphil    take a minimum of space when unused but can be built to contain
78*0716a753Sphil    the full structures.  */
79*0716a753Sphil 
80*0716a753Sphil /* Labels are first.  Labels are generated sequentially in functions
81*0716a753Sphil    and full code.  They just "point" to a single bye in the code.  The
82*0716a753Sphil    "address" is the byte number.  The byte number is used to get an
83*0716a753Sphil    actual character pointer. */
84*0716a753Sphil 
85*0716a753Sphil typedef struct bc_label_group
86*0716a753Sphil     {
87*0716a753Sphil       unsigned long l_adrs [ BC_LABEL_GROUP ];
88*0716a753Sphil       struct bc_label_group *l_next;
89*0716a753Sphil     } bc_label_group;
90*0716a753Sphil 
91*0716a753Sphil /* Argument list.  Recorded in the function so arguments can
92*0716a753Sphil    be checked at call time. */
93*0716a753Sphil 
94*0716a753Sphil typedef struct arg_list
95*0716a753Sphil     {
96*0716a753Sphil       int av_name;
97*0716a753Sphil       int arg_is_var;		/* Extension ... variable parameters. */
98*0716a753Sphil       struct arg_list *next;
99*0716a753Sphil     } arg_list;
100*0716a753Sphil 
101*0716a753Sphil /* Each function has its own code segments and labels.  There can be
102*0716a753Sphil    no jumps between functions so labels are unique to a function. */
103*0716a753Sphil 
104*0716a753Sphil typedef struct
105*0716a753Sphil     {
106*0716a753Sphil       char f_defined;   /* Is this function defined yet. */
107*0716a753Sphil       char f_void;	/* Is this function a void function. */
108*0716a753Sphil       char *f_body;
109*0716a753Sphil       size_t f_body_size;  /* Size of body.  Power of 2. */
110*0716a753Sphil       size_t f_code_size;
111*0716a753Sphil       bc_label_group *f_label;
112*0716a753Sphil       arg_list *f_params;
113*0716a753Sphil       arg_list *f_autos;
114*0716a753Sphil     } bc_function;
115*0716a753Sphil 
116*0716a753Sphil /* Code addresses. */
117*0716a753Sphil typedef struct {
118*0716a753Sphil       unsigned int pc_func;
119*0716a753Sphil       unsigned int pc_addr;
120*0716a753Sphil     } program_counter;
121*0716a753Sphil 
122*0716a753Sphil 
123*0716a753Sphil /* Variables are "pushable" (auto) and thus we need a stack mechanism.
124*0716a753Sphil    This is built into the variable record. */
125*0716a753Sphil 
126*0716a753Sphil typedef struct bc_var
127*0716a753Sphil     {
128*0716a753Sphil       bc_num v_value;
129*0716a753Sphil       struct bc_var *v_next;
130*0716a753Sphil     }  bc_var;
131*0716a753Sphil 
132*0716a753Sphil 
133*0716a753Sphil /* bc arrays can also be "auto" variables and thus need the same
134*0716a753Sphil    kind of stacking mechanisms. */
135*0716a753Sphil 
136*0716a753Sphil typedef struct bc_array_node
137*0716a753Sphil     {
138*0716a753Sphil       union
139*0716a753Sphil 	{
140*0716a753Sphil 	  bc_num n_num [NODE_SIZE];
141*0716a753Sphil 	  struct bc_array_node *n_down [NODE_SIZE];
142*0716a753Sphil 	} n_items;
143*0716a753Sphil     } bc_array_node;
144*0716a753Sphil 
145*0716a753Sphil typedef struct bc_array
146*0716a753Sphil     {
147*0716a753Sphil       bc_array_node *a_tree;
148*0716a753Sphil       short a_depth;
149*0716a753Sphil     } bc_array;
150*0716a753Sphil 
151*0716a753Sphil typedef struct bc_var_array
152*0716a753Sphil     {
153*0716a753Sphil       bc_array *a_value;
154*0716a753Sphil       char      a_param;
155*0716a753Sphil       struct bc_var_array *a_next;
156*0716a753Sphil     } bc_var_array;
157*0716a753Sphil 
158*0716a753Sphil 
159*0716a753Sphil /* For the stacks, execution and function, we need records to allow
160*0716a753Sphil    for arbitrary size. */
161*0716a753Sphil 
162*0716a753Sphil typedef struct estack_rec {
163*0716a753Sphil 	bc_num s_num;
164*0716a753Sphil 	struct estack_rec *s_next;
165*0716a753Sphil } estack_rec;
166*0716a753Sphil 
167*0716a753Sphil typedef struct fstack_rec {
168*0716a753Sphil 	int  s_val;
169*0716a753Sphil 	struct fstack_rec *s_next;
170*0716a753Sphil } fstack_rec;
171*0716a753Sphil 
172*0716a753Sphil 
173*0716a753Sphil /* The following are for the name tree. */
174*0716a753Sphil 
175*0716a753Sphil typedef struct id_rec {
176*0716a753Sphil 	char  *id;      /* The program name. */
177*0716a753Sphil 			/* A name == 0 => nothing assigned yet. */
178*0716a753Sphil 	int   a_name;   /* The array variable name (number). */
179*0716a753Sphil 	int   f_name;   /* The function name (number).  */
180*0716a753Sphil 	int   v_name;   /* The variable name (number).  */
181*0716a753Sphil         short balance;  /* For the balanced tree. */
182*0716a753Sphil 	struct id_rec *left, *right; /* Tree pointers. */
183*0716a753Sphil } id_rec;
184*0716a753Sphil 
185*0716a753Sphil 
186*0716a753Sphil /* A list of files to process. */
187*0716a753Sphil 
188*0716a753Sphil typedef struct file_node {
189*0716a753Sphil 	char *name;
190*0716a753Sphil 	struct file_node *next;
191*0716a753Sphil } file_node;
192*0716a753Sphil 
193*0716a753Sphil /* Macro Definitions */
194*0716a753Sphil 
195*0716a753Sphil #if defined(LIBEDIT)
196*0716a753Sphil #define HISTORY_SIZE(n) history(hist, &histev, H_SETSIZE, n)
197*0716a753Sphil #define UNLIMIT_HISTORY history(hist, &histev, H_SETSIZE, INT_MAX)
198*0716a753Sphil #endif
199*0716a753Sphil 
200*0716a753Sphil #if defined(READLINE)
201*0716a753Sphil #define HISTORY_SIZE(n) stifle_history(n)
202*0716a753Sphil #define UNLIMIT_HISTORY unstifle_history()
203*0716a753Sphil #endif
204*0716a753Sphil 
205*0716a753Sphil /* Now the global variable declarations. */
206*0716a753Sphil #include "global.h"
207