-
Notifications
You must be signed in to change notification settings - Fork 5
/
pezmain.c
274 lines (232 loc) · 5.08 KB
/
pezmain.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
pezmain.c provides a read-eval-print loop executable for Pez.
See doc/CREDITS for information about the authors.
This program is in the public domain.
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <gc/gc.h>
#include <unistd.h>
#include <signal.h>
#include "pezdef.h"
#define FALSE 0
#define TRUE 1
pez_instance *p;
/* Globals imported */
#ifndef HIGHC
/* CTRLC -- Catch a user console break signal. If your C library
does not provide this Unix-compatibile facility
(registered with the call on signal() in main()),
just turn this code off or, better still, replace it
with the equivalent on your system. */
static void ctrlc(int sig)
{
if(sig == SIGINT)
pez_break(p);
}
#endif /* HIGHC */
int print_usage(FILE * s, char *pname)
{
return fprintf(s,
"Usage: %s [options] [input file] [pez args]\n"
" Options:\n"
" -D Treat file as definitions\n"
" -Hn Heap length n\n"
" -Ifile Include named definition file\n"
" -Rn Return stack length n\n"
" -Sn Stack length n\n"
" -T Set TRACE mode\n"
" -U Print this message\n", pname);
}
static void init_pez_argv(int argc)
{
int size = sizeof(char *) *argc;
p->argv = GC_MALLOC(size);
if(!p->argv) {
fprintf(stderr, "Couldn't allocate enough memory to duplicate "
"argv (%d bytes).\n"
"Something's real bad wrong.\n", size);
exit(2);
}
memset(p->argv, 0, size);
}
void pezmain_load(char *fname, FILE *fp)
{
int stat;
if(fp == NULL) {
fprintf(stderr, "Unable to open include file %s\n", fname);
exit(1);
}
stat = pez_load(p, fp);
fclose(fp);
if(stat != PEZ_SNORM) {
printf("\nError %d in include file %s\n", stat, fname);
exit(1);
}
}
void include_file(char *fname)
{
FILE *fp;
fp = fopen(fname,
#ifdef FBmode
"rb"
#else
"r"
#endif
);
pezmain_load(fname, fp);
}
void load_rc()
{
FILE *fd;
int len;
char *path, *home;
home = getenv("HOME");
if(!home)
return;
len = strlen(home);
path = GC_malloc(len + 8);
if(!path) {
fprintf(stderr,
"Couldn't allocate %d bytes; this is troubling.\n",
len);
abort();
}
sprintf(path, "%s/.pezrc", home);
fd = fopen(path,
#ifdef FBmode
"rb"
#else
"r"
#endif
);
if(fd != NULL) {
pezmain_load(path, fd);
}
}
/* MAIN -- Main program. */
int main(int argc, char *argv[])
{
int i;
int fname = FALSE, defmode = FALSE;
int optdone = 0;
FILE *ifp;
char *include[20];
char *cp, opt;
int in = 0, status = PEZ_SNORM, interactive;
char **pez_argv_current;
p = pez_init(PEZ_A_EVERYTHING);
if(!p) {
fprintf(stderr, "Couldn't initialize Pez!\n");
exit(1);
}
init_pez_argv(argc);
pez_argv_current = p->argv;
ifp = stdin;
for(i = 1; i < argc; i++) {
cp = argv[i];
if(*cp == '-') {
opt = *(++cp);
if(islower(opt))
opt = toupper(opt);
// TODO: No more case-insensitive command-line opts,
// for one, and try to make these a little closer to
// what users would expect, like -h printing help rather
// than -u or -?.
// Aside from that, I suppose we should have a real
// parser here.
switch (opt) {
case 'D':
defmode = TRUE;
break;
case 'I':
include[in++] = cp + 1;
break;
case 'T':
p->trace = TRUE;
break;
case '-':
optdone = 1;
break;
case '?':
case 'U':
print_usage(stdout, argv[0]);
return 0;
default:
optdone = 1;
break;
}
} else {
break;
}
}
// Keeping i from before:
while(i < argc) {
cp = argv[i];
char fn[132];
if(fname) {
(*(pez_argv_current++)) = cp;
} else {
fname = TRUE;
strncpy(fn, cp, sizeof(fn));
ifp = fopen(fn, "r");
if(ifp == NULL) {
fprintf(stderr, "Unable to open file %s\n", fn);
return 1;
}
}
i++;
}
/* If any include files were named, load each in turn before
we execute the program. */
for(i = 0; i < in; i++) {
char fn[132];
strncpy(fn, include[i], sizeof(fn));
if(strchr(fn, '.') == NULL)
strcat(fn, ".pez");
include_file(fn);
}
/* Now that all the preliminaries are out of the way, fall into
the main PEZ execution loop. */
interactive = !fname && isatty(0);
if(interactive)
load_rc();
#ifndef HIGHC
signal(SIGINT, ctrlc);
#endif /* HIGHC */
while(status == PEZ_SNORM || interactive) {
char t[TOK_BUF_SZ];
if(interactive) {
if(p->comment)
printf("( ");
else if((p->heap != NULL && state) ||
pez_anticipating_token(p))
printf(":> ");
else {
// If it's safe to eval and we're running in
// interactive mode, we show the user the stack
// and (unless it's empty) the float stack.
if(p->fstk != p->fstack)
pez_eval(p, "10 nf.s");
pez_eval(p, "10 n.s");
printf("-> ");
}
fflush(stdout);
}
if(fgets(t, TOK_BUF_SZ, ifp) == NULL) {
if(fname && defmode) {
fname = defmode = FALSE;
interactive = TRUE;
ifp = stdin;
continue;
}
break;
}
status = pez_eval(p, t);
}
if(interactive)
printf("\n");
return 0;
}