blob: d6f32b03d98daa213a46d7b66d69603a6e2791e2 [file] [log] [blame]
Gavin Howardd5551672018-09-22 19:52:42 -06001/*
2 * *****************************************************************************
3 *
4 * Copyright 2018 Gavin D. Howard
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
16 *
17 * *****************************************************************************
18 *
19 * Definitions for bc's VM.
20 *
21 */
22
23#ifndef BC_VM_H
24#define BC_VM_H
25
Gavin Howard411f7322018-09-26 17:21:19 -060026#include <stddef.h>
Gavin Howardd5551672018-09-22 19:52:42 -060027#include <limits.h>
28
29#include <status.h>
30#include <parse.h>
31#include <program.h>
32
Gavin Howard40a085f2018-12-03 12:08:59 -070033#if !BC_ENABLED && !DC_ENABLED
Gavin Howarde6e84762018-10-03 11:46:34 -060034#error Must define BC_ENABLED, DC_ENABLED, or both
Gavin Howarda0ccea62018-09-26 21:02:22 -060035#endif
36
Gavin Howarda95501a2018-10-03 16:13:34 -060037// ** Exclude start. **
38#define VERSION_STR(V) #V
Gavin Howardbc2d23d2018-10-03 16:24:08 -060039#define VERSION_STR2(V) VERSION_STR(V)
40#define BC_VERSION VERSION_STR2(VERSION)
Gavin Howarda95501a2018-10-03 16:13:34 -060041// ** Exclude end. **
42
Gavin Howardf4983b82018-09-27 10:44:21 -060043#define BC_FLAG_X (1<<0)
44#define BC_FLAG_W (1<<1)
Gavin Howardb03e6682018-10-31 12:17:36 -060045#define BC_FLAG_V (1<<2)
46#define BC_FLAG_S (1<<3)
47#define BC_FLAG_Q (1<<4)
48#define BC_FLAG_L (1<<5)
49#define BC_FLAG_I (1<<6)
Gavin Howardd5551672018-09-22 19:52:42 -060050
51#define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
52#define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
53
54#define BC_MAX_OBASE ((unsigned long) 999)
55#define BC_MAX_DIM ((unsigned long) INT_MAX)
56#define BC_MAX_SCALE ((unsigned long) UINT_MAX)
57#define BC_MAX_STRING ((unsigned long) UINT_MAX - 1)
58#define BC_MAX_NAME BC_MAX_STRING
Gavin Howard911d7ef2018-10-17 09:48:41 -060059#define BC_MAX_NUM BC_MAX_STRING
Gavin Howardd5551672018-09-22 19:52:42 -060060#define BC_MAX_EXP ((unsigned long) LONG_MAX)
61#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1)
62
Gavin Howardf878d332018-12-03 12:58:38 -070063#define BC_IS_BC (BC_ENABLED && (!DC_ENABLED || bcg.name[0] == 'b'))
64
Gavin Howard112304b2018-12-04 14:30:01 -070065#if BC_ENABLE_SIGNALS
66#define BC_SIGINT (bcg.sig)
67#else // BC_ENABLE_SIGNALS
68#define BC_SIGINT (0)
69#endif // BC_ENABLE_SIGNALS
70
Gavin Howard6365b242018-10-08 10:34:26 -060071typedef struct BcVm {
Gavin Howard773c86b2018-11-02 14:07:19 -060072
Gavin Howard6365b242018-10-08 10:34:26 -060073 BcParse prs;
74 BcProgram prog;
75
Gavin Howard6bd17002018-10-08 13:40:13 -060076 // ** Exclude start. **
Gavin Howarded2641c2018-10-31 11:10:15 -060077 uint32_t flags;
Gavin Howard6365b242018-10-08 10:34:26 -060078 BcVec files;
Gavin Howarded2641c2018-10-31 11:10:15 -060079
80 // ** Busybox exclude start. **
Gavin Howard6365b242018-10-08 10:34:26 -060081 BcVec exprs;
Gavin Howarded2641c2018-10-31 11:10:15 -060082 // ** Busybox exclude end. **
Gavin Howard6365b242018-10-08 10:34:26 -060083
Gavin Howard99efb102018-10-08 22:27:25 -060084 char *env_args;
Gavin Howardaf2cafd2018-10-05 12:36:27 -060085 // ** Exclude end. **
Gavin Howard773c86b2018-11-02 14:07:19 -060086
Gavin Howardd5551672018-09-22 19:52:42 -060087} BcVm;
88
89// ** Exclude start. **
90typedef struct BcGlobals {
Gavin Howard773c86b2018-11-02 14:07:19 -060091
Gavin Howard9727b6f2018-11-14 15:24:43 -070092 long i;
Gavin Howardca893d72018-09-28 14:15:16 -060093 long ttyin;
Gavin Howard9727b6f2018-11-14 15:24:43 -070094 long s;
95 long w;
96 long x;
Gavin Howard118fc652018-10-03 16:45:29 -060097
Gavin Howarda84ad992018-12-03 19:11:06 -070098 // ** Busybox exclude start. **
Gavin Howardf4983b82018-09-27 10:44:21 -060099 const char *name;
Gavin Howard07f0d102018-12-04 14:28:03 -0700100 const char *help;
Gavin Howarda84ad992018-12-03 19:11:06 -0700101 // ** Busybox exclude end. **
Gavin Howard07f0d102018-12-04 14:28:03 -0700102
Gavin Howard02c856d2018-11-01 23:52:16 -0600103#if BC_ENABLE_SIGNALS
Gavin Howard112304b2018-12-04 14:30:01 -0700104 unsigned long sig;
Gavin Howard118fc652018-10-03 16:45:29 -0600105 const char *sig_msg;
Gavin Howard02c856d2018-11-01 23:52:16 -0600106#endif // BC_ENABLE_SIGNALS
Gavin Howard773c86b2018-11-02 14:07:19 -0600107
Gavin Howard5ec75732018-12-03 12:11:28 -0700108 BcParseInit init;
109 BcParseExpr exp;
110 char sbgn;
111 char send;
112
Gavin Howardd5551672018-09-22 19:52:42 -0600113} BcGlobals;
114// ** Exclude end. **
115
Gavin Howard40a085f2018-12-03 12:08:59 -0700116#if BC_ENABLED
Gavin Howardc39fd492018-10-04 10:07:03 -0600117BcStatus bc_vm_posixError(BcStatus s, const char *file,
Gavin Howard6365b242018-10-08 10:34:26 -0600118 size_t line, const char *msg);
Gavin Howardf8f232b2018-10-11 17:29:47 -0600119#endif // BC_ENABLED
Gavin Howardd5551672018-09-22 19:52:42 -0600120
Gavin Howarda84ad992018-12-03 19:11:06 -0700121// ** Exclude start. **
122void bc_vm_info(const char* const help);
123BcStatus bc_vm_run(int argc, char *argv[], const char *env_len);
124// ** Exclude end. **
125
126// ** Busybox exclude start. **
127
Gavin Howard48af52e2018-10-30 14:47:38 -0600128void bc_vm_exit(BcStatus s);
Gavin Howarda84ad992018-12-03 19:11:06 -0700129void bc_vm_printf(const char *fmt, ...);
Gavin Howard48af52e2018-10-30 14:47:38 -0600130void bc_vm_puts(const char *str, FILE *restrict f);
131void bc_vm_putchar(int c);
132void bc_vm_fflush(FILE *restrict f);
133
Gavin Howardc9a9c472018-10-02 17:23:01 -0600134// ** Exclude start. **
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600135void* bc_vm_malloc(size_t n);
136void* bc_vm_realloc(void *ptr, size_t n);
137char* bc_vm_strdup(const char *str);
138
Gavin Howard0f31f5b2018-12-03 11:15:46 -0700139BcStatus bc_vm_error(BcStatus s, const char *file, size_t line);
140// ** Exclude end. **
141
Gavin Howard40a085f2018-12-03 12:08:59 -0700142#if BC_ENABLED
Gavin Howarda95501a2018-10-03 16:13:34 -0600143// ** Exclude start. **
Gavin Howard4ffe5a92018-09-26 20:58:31 -0600144extern const char bc_name[];
Gavin Howarda95501a2018-10-03 16:13:34 -0600145// ** Exclude end. **
146extern const char bc_sig_msg[];
147#endif // BC_ENABLED
Gavin Howard4ffe5a92018-09-26 20:58:31 -0600148
Gavin Howarda95501a2018-10-03 16:13:34 -0600149// ** Exclude start. **
Gavin Howard40a085f2018-12-03 12:08:59 -0700150#if DC_ENABLED
Gavin Howarda95501a2018-10-03 16:13:34 -0600151extern const char dc_name[];
152extern const char dc_sig_msg[];
153#endif // DC_ENABLED
154
155extern const char bc_copyright[];
Gavin Howardd5551672018-09-22 19:52:42 -0600156
157extern const char bc_lib[];
158extern const char *bc_lib_name;
159
160extern const char bc_err_fmt[];
Gavin Howarda218a392018-10-25 09:13:54 -0600161extern const char bc_warn_fmt[];
Gavin Howardd5551672018-09-22 19:52:42 -0600162extern const char bc_err_line[];
163extern const char *bc_errs[];
Gavin Howarda218a392018-10-25 09:13:54 -0600164extern const uint8_t bc_err_ids[];
165extern const char *bc_err_msgs[];
Gavin Howarda95501a2018-10-03 16:13:34 -0600166// ** Exclude end. **
Gavin Howardd5551672018-09-22 19:52:42 -0600167
Gavin Howardf89006a2018-10-29 13:01:51 -0600168// ** Busybox exclude end. **
169
Gavin Howarda95501a2018-10-03 16:13:34 -0600170extern BcGlobals bcg;
Gavin Howardd5551672018-09-22 19:52:42 -0600171
172#endif // BC_VM_H