Initial revision
[LeanCalc.git] / calc / calc / calc.h
1 /*
2  * calc - definitions for calculator program
3  *
4  * Copyright (C) 1999  David I. Bell
5  *
6  * Calc is open software; you can redistribute it and/or modify it under
7  * the terms of the version 2.1 of the GNU Lesser General Public License
8  * as published by the Free Software Foundation.
9  *
10  * Calc is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
13  * Public License for more details.
14  *
15  * A copy of version 2.1 of the GNU Lesser General Public License is
16  * distributed with calc under the filename COPYING-LGPL.  You should have
17  * received a copy with calc; if not, write to Free Software Foundation, Inc.
18  * 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * @(#) $Revision$
21  * @(#) $Id$
22  * @(#) $Source$
23  *
24  * Under source code control:   1990/02/15 01:48:31
25  * File existed as early as:    before 1990
26  *
27  * Share and enjoy!  :-)        http://www.isthe.com/chongo/tech/comp/calc/
28  */
29
30
31 #if !defined(__CALC_H__)
32 #define __CALC_H__
33
34 #include <setjmp.h>
35 #if defined(CALC_SRC)   /* if we are building from the calc source tree */
36 # include "calc/win32dll.h"
37 # include "calc/value.h"
38 # include "calc/have_const.h"
39 #else
40 # include <calc/win32dll.h>
41 # include <calc/value.h>
42 # include <calc/have_const.h>
43 #endif
44
45
46 /*
47  * Configuration definitions
48  */
49 #define CALCPATH        "CALCPATH"      /* environment variable for files */
50 #define CALCRC          "CALCRC"        /* environment variable for startup */
51 #define CALCBINDINGS    "CALCBINDINGS"  /* env variable for hist bindings */
52 #define HOME            "HOME"          /* environment variable for home dir */
53 #define PAGER           "PAGER"         /* environment variable for help */
54 #define SHELL           "SHELL"         /* environment variable for shell */
55 #define DEFAULTCALCBINDINGS "bindings"  /* default calc bindings file */
56 #define DEFAULTCALCHELP "help"          /* help file that -h prints */
57 #define DEFAULTSHELL    "sh"            /* default shell to use */
58 #define CALCEXT         ".cal"  /* extension for files read in */
59 #define MAX_CALCRC      1024    /* maximum length of $CALCRC */
60 #define HOMECHAR        '~'     /* char which indicates home directory */
61 #define DOTCHAR         '.'     /* char which indicates current directory */
62 #define PATHCHAR        '/'     /* char which separates path components */
63 #if defined(__MSDOS__) || defined(__WIN32)
64 #define LISTCHAR        ';'     /* char which separates paths in a list */
65 #else
66 #define LISTCHAR        ':'     /* char which separates paths in a list */
67 #endif
68 #define MAXCMD          16384   /* maximum length of command invocation */
69 #define MAXERROR        512     /* maximum length of error message string */
70
71 #define SYMBOLSIZE      256     /* maximum symbol name size */
72 #define MAXLABELS       100     /* maximum number of user labels in function */
73 #define MAXSTRING       1024    /* maximum size of string constant */
74 #define MAXSTACK        2048    /* maximum depth of evaluation stack */
75 #define MAXFILES        20      /* maximum number of opened files */
76 #define PROMPT1         "> "    /* default normal prompt*/
77 #define PROMPT2         ">> "   /* default prompt inside multi-line input */
78
79
80 #define TRACE_NORMAL    0x00    /* normal trace flags */
81 #define TRACE_OPCODES   0x01    /* trace every opcode */
82 #define TRACE_NODEBUG   0x02    /* suppress debugging opcodes */
83 #define TRACE_LINKS     0x04    /* display links for real and complex numbers */
84 #define TRACE_FNCODES   0x08    /* display code for newly defined function */
85 #define TRACE_MAX       0x0f    /* maximum value for trace flag */
86
87 #define ABORT_NONE      0       /* abort not needed yet */
88 #define ABORT_STATEMENT 1       /* abort on statement boundary */
89 #define ABORT_OPCODE    2       /* abort on any opcode boundary */
90 #define ABORT_MATH      3       /* abort on any math operation */
91 #define ABORT_NOW       4       /* abort right away */
92
93 #define ERRMAX 20               /* default errmax value */
94
95 /*
96  * File ids corresponding to standard in, out, error, and when not in use.
97  */
98 #define FILEID_STDIN    ((FILEID) 0)
99 #define FILEID_STDOUT   ((FILEID) 1)
100 #define FILEID_STDERR   ((FILEID) 2)
101 #define FILEID_NONE     ((FILEID) -1)
102
103 /*
104  * File I/O routines.
105  */
106 extern DLL FILEID openid(char *name, char *mode);
107 extern DLL FILEID indexid(long index);
108 extern DLL BOOL validid(FILEID id);
109 extern DLL BOOL errorid(FILEID id);
110 extern DLL BOOL eofid(FILEID id);
111 extern DLL int closeid(FILEID id);
112 extern DLL int getcharid(FILEID id);
113 extern DLL int idprintf(FILEID id, char *fmt, int count, VALUE **vals);
114 extern DLL int idfputc(FILEID id, int ch);
115 extern DLL int idfputs(FILEID id, char *str);
116 extern DLL int printid(FILEID id, int flags);
117 extern DLL int flushid(FILEID id);
118 extern DLL int readid(FILEID id, int flags, char **retptr);
119 extern DLL int getloc(FILEID id, ZVALUE *loc);
120 extern DLL int setloc(FILEID id, ZVALUE zpos);
121 extern DLL int getsize(FILEID id, ZVALUE *size);
122 extern DLL int get_device(FILEID id, ZVALUE *dev);
123 extern DLL int get_inode(FILEID id, ZVALUE *ino);
124 extern DLL FILEID reopenid(FILEID id, char *mode, char *name);
125 extern DLL int closeall(void);
126
127 #if !defined(_WIN32)
128 extern DLL int flushall(void);
129 #endif
130
131 extern DLL int idfputstr(FILEID id, char *str);
132 extern DLL int rewindid(FILEID id);
133 extern DLL void rewindall(void);
134 extern DLL ZVALUE zfilesize(FILEID id);
135 extern DLL void showfiles(void);
136 extern DLL int fscanfid(FILEID id, char *fmt, int count, VALUE **vals);
137 extern DLL int scanfstr(char *str, char *fmt, int count, VALUE **vals);
138 extern DLL int ftellid(FILEID id, ZVALUE *res);
139 extern DLL int fseekid(FILEID id, ZVALUE offset, int whence);
140 extern DLL int isattyid(FILEID id);
141 extern DLL int fsearch(FILEID id, char *str, ZVALUE start, ZVALUE end, ZVALUE *res);
142 extern DLL int frsearch(FILEID id, char *str, ZVALUE first, ZVALUE last, ZVALUE *res);
143 extern DLL void showconstants(void);
144 extern DLL void freeconstant(unsigned long);
145 extern DLL void freestringconstant(long);
146 extern DLL void trimconstants(void);
147
148 /*
149  * Input routines.
150  */
151 extern DLL int openstring(char *str, long num);
152 extern DLL int openterminal(void);
153 extern DLL int opensearchfile(char *name, char *pathlist, char *exten, int reopen_ok);
154 extern DLL char *nextline(void);
155 extern DLL int nextchar(void);
156 extern DLL void reread(void);
157 extern DLL void resetinput(void);
158 extern DLL void setprompt(char *);
159 extern DLL BOOL inputisterminal(void);
160 extern DLL int inputlevel(void);
161 extern DLL long calclevel(void);
162 extern DLL char *inputname(void);
163 extern DLL long linenumber(void);
164 extern DLL void runrcfiles(void);
165 extern DLL void closeinput(void);
166
167 /*
168  * Other routines.
169  */
170 extern DLL NUMBER *constvalue(unsigned long index);
171 extern DLL long addnumber(char *str);
172 extern DLL long addqconstant(NUMBER *q);
173 extern DLL void initstack(void);
174 extern DLL void getcommands(BOOL toplevel);
175 extern DLL void givehelp(char *type);
176 extern DLL void libcalc_call_me_first(void);
177 extern DLL void libcalc_call_me_last(void);
178 extern DLL BOOL calc_tty(int fd);
179 extern DLL BOOL orig_tty(int fd);
180 extern DLL void showerrors(void);
181 extern DLL char *calc_strdup(CONST char *);
182
183 /*
184  * Initialization
185  */
186 extern DLL void initialize(void);
187 extern DLL void reinitialize(void);
188 #if !defined (_WIN32)
189 extern DLL int isatty(int tty); /* TRUE if fd is a tty */
190 #endif
191 extern DLL char *version(void); /* return version string */
192 extern DLL int post_init;       /* TRUE => math_error setjmp is ready */
193
194 /*
195  * global flags and definitions
196  */
197 extern DLL int abortlevel;      /* current level of aborts */
198 extern DLL BOOL inputwait;      /* TRUE if in a terminal input wait */
199 extern DLL jmp_buf jmpbuf;      /* for errors */
200
201 extern DLL int p_flag;          /* TRUE => pipe mode */
202 extern DLL int q_flag;          /* TRUE => don't execute rc files */
203 extern DLL int u_flag;          /* TRUE => unbuffer stdin and stdout */
204 extern DLL int d_flag;          /* TRUE => disable heading, resource_debug */
205 extern DLL int c_flag;          /* TRUE => continue after error if permitted */
206 extern DLL int i_flag;          /* TRUE => try to go interactive after error */
207 extern DLL int s_flag;          /* TRUE => keep args as strings for argv() */
208 extern DLL int stoponerror;     /* >0 => stop, <0 => continue, ==0 => use -c */
209 extern DLL BOOL abort_now;      /* TRUE => try to go interactive */
210
211 extern DLL int argc_value;      /* count of argv[] strings for argv() builtin */
212 extern DLL char **argv_value;   /* argv[] strings for argv() builtin */
213
214 extern DLL char *pager;         /* $PAGER or default */
215 extern DLL int stdin_tty;       /* TRUE if stdin is a tty */
216 extern DLL int havecommands;    /* TRUE if have cmd args) */
217 extern DLL char *program;       /* our name */
218 extern DLL char *base_name;     /* basename of our name */
219 extern DLL char cmdbuf[];       /* command line expression */
220
221 extern DLL int abortlevel;      /* current level of aborts */
222 extern DLL BOOL inputwait;      /* TRUE if in a terminal input wait */
223 extern DLL VALUE *stack;        /* execution stack */
224 extern DLL int dumpnames;       /* TRUE => dump names rather than indices */
225
226 extern DLL char *calcpath;      /* $CALCPATH or default */
227 extern DLL char *calcrc;        /* $CALCRC or default */
228 extern DLL char *calcbindings;  /* $CALCBINDINGS or default */
229 extern DLL char *home;          /* $HOME or default */
230 extern DLL char *shell;         /* $SHELL or default */
231 extern DLL char *program;       /* our name (argv[0]) */
232
233 extern DLL int no_env;          /* TRUE (-e) => ignore env vars on startup */
234 extern DLL int errmax;  /* if >= 0, error when errcount exceeds errmax */
235 extern DLL int use_old_std;     /* TRUE (-O) => use classic configuration */
236
237 extern DLL int allow_read;      /* FALSE => dont open any files for reading */
238 extern DLL int allow_write;     /* FALSE => dont open any files for writing */
239 extern DLL int allow_exec;      /* FALSE => may not execute any commands */
240
241 /*
242  * calc startup and run state
243  */
244 typedef enum {
245     RUN_ZERO,                   /* unknown or unset start state */
246     RUN_BEGIN,                  /* calc execution starts */
247     RUN_RCFILES,                /* rc files being evaluated */
248     RUN_PRE_CMD_ARGS,           /* prepare to evaluate cmd args */
249     RUN_CMD_ARGS,               /* cmd args being evaluated */
250     RUN_PRE_TOP_LEVEL,          /* prepare to start top level activity */
251     RUN_TOP_LEVEL,              /* running at top level */
252     RUN_EXIT,                   /* normal exit from calc */
253     RUN_EXIT_WITH_ERROR         /* exit with error */
254 } run;
255 extern DLL run run_state;
256 extern DLL char *run_state_name(run state);
257
258 /*
259  * calc version information
260  */
261 #define CALC_TITLE "C-style arbitrary precision calculator"
262 extern int calc_major_ver;
263 extern int calc_minor_ver;
264 extern int calc_major_patch;
265 extern int calc_minor_patch;
266 extern char *Copyright;
267 extern DLL char *version(void);
268
269
270 #endif /* !__CALC_H__ */