blob: 816c44e4ec55ebd622e0edb41a2171f16dd541a8 [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 Howard6365b242018-10-08 10:34:26 -060065typedef struct BcVm {
Gavin Howard773c86b2018-11-02 14:07:19 -060066
Gavin Howard6365b242018-10-08 10:34:26 -060067 BcParse prs;
68 BcProgram prog;
69
Gavin Howard6bd17002018-10-08 13:40:13 -060070 // ** Exclude start. **
Gavin Howarded2641c2018-10-31 11:10:15 -060071 uint32_t flags;
Gavin Howard6365b242018-10-08 10:34:26 -060072 BcVec files;
Gavin Howarded2641c2018-10-31 11:10:15 -060073
74 // ** Busybox exclude start. **
Gavin Howard6365b242018-10-08 10:34:26 -060075 BcVec exprs;
Gavin Howarded2641c2018-10-31 11:10:15 -060076 // ** Busybox exclude end. **
Gavin Howard6365b242018-10-08 10:34:26 -060077
Gavin Howard99efb102018-10-08 22:27:25 -060078 char *env_args;
Gavin Howardaf2cafd2018-10-05 12:36:27 -060079 // ** Exclude end. **
Gavin Howard773c86b2018-11-02 14:07:19 -060080
Gavin Howardd5551672018-09-22 19:52:42 -060081} BcVm;
82
83// ** Exclude start. **
84typedef struct BcGlobals {
Gavin Howard773c86b2018-11-02 14:07:19 -060085
Gavin Howard63738202018-09-26 15:34:20 -060086 unsigned long sig;
87 unsigned long sigc;
88 unsigned long signe;
Gavin Howardd5551672018-09-22 19:52:42 -060089
Gavin Howard9727b6f2018-11-14 15:24:43 -070090 long i;
Gavin Howardca893d72018-09-28 14:15:16 -060091 long ttyin;
Gavin Howard9727b6f2018-11-14 15:24:43 -070092 long s;
93 long w;
94 long x;
Gavin Howard118fc652018-10-03 16:45:29 -060095
Gavin Howarda84ad992018-12-03 19:11:06 -070096 // ** Busybox exclude start. **
Gavin Howardf4983b82018-09-27 10:44:21 -060097 const char *name;
Gavin Howard07f0d102018-12-04 14:28:03 -070098 const char *help;
Gavin Howarda84ad992018-12-03 19:11:06 -070099 // ** Busybox exclude end. **
Gavin Howard07f0d102018-12-04 14:28:03 -0700100
Gavin Howard02c856d2018-11-01 23:52:16 -0600101#if BC_ENABLE_SIGNALS
Gavin Howard118fc652018-10-03 16:45:29 -0600102 const char *sig_msg;
Gavin Howard02c856d2018-11-01 23:52:16 -0600103#endif // BC_ENABLE_SIGNALS
Gavin Howard773c86b2018-11-02 14:07:19 -0600104
Gavin Howard5ec75732018-12-03 12:11:28 -0700105 BcParseInit init;
106 BcParseExpr exp;
107 char sbgn;
108 char send;
109
Gavin Howardd5551672018-09-22 19:52:42 -0600110} BcGlobals;
111// ** Exclude end. **
112
Gavin Howard40a085f2018-12-03 12:08:59 -0700113#if BC_ENABLED
Gavin Howardc39fd492018-10-04 10:07:03 -0600114BcStatus bc_vm_posixError(BcStatus s, const char *file,
Gavin Howard6365b242018-10-08 10:34:26 -0600115 size_t line, const char *msg);
Gavin Howardf8f232b2018-10-11 17:29:47 -0600116#endif // BC_ENABLED
Gavin Howardd5551672018-09-22 19:52:42 -0600117
Gavin Howarda84ad992018-12-03 19:11:06 -0700118// ** Exclude start. **
119void bc_vm_info(const char* const help);
120BcStatus bc_vm_run(int argc, char *argv[], const char *env_len);
121// ** Exclude end. **
122
123// ** Busybox exclude start. **
124
Gavin Howard48af52e2018-10-30 14:47:38 -0600125void bc_vm_exit(BcStatus s);
Gavin Howarda84ad992018-12-03 19:11:06 -0700126void bc_vm_printf(const char *fmt, ...);
Gavin Howard48af52e2018-10-30 14:47:38 -0600127void bc_vm_puts(const char *str, FILE *restrict f);
128void bc_vm_putchar(int c);
129void bc_vm_fflush(FILE *restrict f);
130
Gavin Howardc9a9c472018-10-02 17:23:01 -0600131// ** Exclude start. **
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600132void* bc_vm_malloc(size_t n);
133void* bc_vm_realloc(void *ptr, size_t n);
134char* bc_vm_strdup(const char *str);
135
Gavin Howard0f31f5b2018-12-03 11:15:46 -0700136BcStatus bc_vm_error(BcStatus s, const char *file, size_t line);
137// ** Exclude end. **
138
Gavin Howard40a085f2018-12-03 12:08:59 -0700139#if BC_ENABLED
Gavin Howarda95501a2018-10-03 16:13:34 -0600140// ** Exclude start. **
Gavin Howard4ffe5a92018-09-26 20:58:31 -0600141extern const char bc_name[];
Gavin Howarda95501a2018-10-03 16:13:34 -0600142// ** Exclude end. **
143extern const char bc_sig_msg[];
144#endif // BC_ENABLED
Gavin Howard4ffe5a92018-09-26 20:58:31 -0600145
Gavin Howarda95501a2018-10-03 16:13:34 -0600146// ** Exclude start. **
Gavin Howard40a085f2018-12-03 12:08:59 -0700147#if DC_ENABLED
Gavin Howarda95501a2018-10-03 16:13:34 -0600148extern const char dc_name[];
149extern const char dc_sig_msg[];
150#endif // DC_ENABLED
151
152extern const char bc_copyright[];
Gavin Howardd5551672018-09-22 19:52:42 -0600153
154extern const char bc_lib[];
155extern const char *bc_lib_name;
156
157extern const char bc_err_fmt[];
Gavin Howarda218a392018-10-25 09:13:54 -0600158extern const char bc_warn_fmt[];
Gavin Howardd5551672018-09-22 19:52:42 -0600159extern const char bc_err_line[];
160extern const char *bc_errs[];
Gavin Howarda218a392018-10-25 09:13:54 -0600161extern const uint8_t bc_err_ids[];
162extern const char *bc_err_msgs[];
Gavin Howarda95501a2018-10-03 16:13:34 -0600163// ** Exclude end. **
Gavin Howardd5551672018-09-22 19:52:42 -0600164
Gavin Howardf89006a2018-10-29 13:01:51 -0600165// ** Busybox exclude end. **
166
Gavin Howarda95501a2018-10-03 16:13:34 -0600167extern BcGlobals bcg;
Gavin Howardd5551672018-09-22 19:52:42 -0600168
169#endif // BC_VM_H