-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
299 lines (279 loc) · 6.74 KB
/
main.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* Tiny BASIC for Curses
*
* Copyright (c) 2011, Tarvo Korrovits
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <curses.h>
#include "tinybc.h"
/* Make a large buffer */
#define BUFSIZE 1000000
#define ARRSIZE 100000
struct mttype {
char program[BUFSIZE];
long int numbers[ARRSIZE];
struct cttype *ct;
};
static void help(void)
{
printw("\nCommand line: tinybc [options] [filename]\n");
printw("Options: -number renumber, -i run interactively\n");
printw("CLEAR -- Delete program\n");
printw("LIST line1-line2 -- List program from line1 to line2\n");
printw("LOAD filename -- Load program from file\n");
printw("QUIT -- Exit\n");
printw("RUN -- Run program\n");
printw("SAVE [filename] -- Save program\n");
printw("HELP -- This help\n");
printw("Other statements do not run interactively\n\n");
}
static void insert(struct mttype *mt, char *buffer)
{
char *p, *p1;
long int n;
n = atoi(buffer);
if (!n) return;
p = mt->program;
if (strlen(mt->program) + strlen(buffer) >= BUFSIZE) return;
if (atoi(p) < n)
while (*p && n && atoi(p) < n) {
p += strcspn(p, "\n");
p += strspn(p, "\n");
}
p1 = buffer;
p1 += strcspn(p1, " ");
p1 += strspn(p1, " ");
if (atoi(p) == n) {
memmove(p, p + strcspn(p, "\n") + 1, strlen(p) -
strcspn(p, "\n")+ 1);
if (!(*p1))
if (*p == '\n')
memmove(p, p + strspn(p, "\n"),
strlen(p) - strspn(p, "\n") + 1);
}
if (!(*p1)) return;
sprintf(buffer, "%s\n", buffer);
memmove(p + strlen(buffer), p, strlen(p) + 1);
memmove(p, buffer, strlen(buffer));
}
static void intro(void)
{
printw("Tiny BASIC for Curses, version 0.8.6\n");
printw("Type HELP if you need help\n\n");
}
static void list(struct mttype *mt, long int m, long int n)
{
char buffer[FILENAME_MAX], *p;
long int exit, i, k;
p = mt->program;
while (*p && m && atoi(p) != m) {
p += strcspn(p, "\n");
p += strspn(p, "\n");
}
if (!(*p)) return;
i = 0;
exit = 0;
do {
if (strcspn(p, "\n") >= FILENAME_MAX - 1) break;
strncpy(buffer, p, strcspn(p, "\n"));
buffer[strcspn(p, "\n")] = 0;
printw("%s\n", buffer);
if (i++ == LINES - 3) {
i = 0;
printw("=== Press any key, ESC to break ===\n");
interactive(1);
while ((k = getch()) == -1) napms(10);
interactive(0);
if (k == 27) exit = 1;
}
p += strcspn(p, "\n");
if (*p) p++;
if (m && m == n) break;
} while (*p && (!m || atoi(p) <= n) && !exit);
}
static int load(struct mttype *mt, const char *fname)
{
char buffer[MAX_STRLEN];
long int pos;
FILE *f;
f = fopen(fname, "r");
if (!f) return 0;
for (pos = 0; fgets(buffer, MAX_STRLEN, f); ) {
if (pos + strlen(buffer) >= BUFSIZE) break;
if (buffer[0] == '\n') continue;
strcpy(mt->program + pos, buffer);
pos += strlen(buffer);
}
fclose(f);
return 1;
}
static void run(struct mttype *mt)
{
tinybc_start(mt->ct);
while (!tinybc_ended(mt->ct)) tinybc_statement(mt->ct);
}
int main(int argc, char **argv)
{
char buffer[MAX_STRLEN], fname[MAX_STRLEN], *p, *p1;
long int fileind, lastnum, exit, m, n;
struct mttype *mt;
FILE *f;
mt = (struct mttype *) calloc(sizeof(struct mttype), 1);
mt->ct = (struct cttype *) calloc(sizeof(struct cttype), 1);
exit = 0;
fileind = 1;
strcpy(fname, "");
if (argc == 3) fileind = 2;
if (argc > 1) {
strcpy(fname, argv[fileind]);
if (!load(mt, fname)) {
printf("tinybc: cannot open the file\n");
return 1;
}
}
if (argc != 3 || !isdigit(argv[1][1]))
tinybc_init(mt->ct, mt->program, mt->numbers);
if (argc == 1 || (argc == 3 && argv[1][1] == 'i')) {
intro();
while (!exit) {
printw("tinybc: ");
strcpy(buffer, "");
getnstr(buffer, MAX_STRLEN - 1);
p = buffer;
if (isdigit(buffer[0])) {
insert(mt, buffer);
continue;
}
switch (buffer[0]) {
case 'C':
strcpy(mt->program, "");
break;
case 'L':
p += strcspn(p, " ");
p += strspn(p, " ");
if (isdigit(*p) || !(*p)) {
m = atoi(p);
p += strcspn(p, "-");
p += strspn(p, "-");
n = atoi(p);
if (!n) n = m;
list(mt, m, n);
} else {
strcpy(fname, p);
if (!load(mt, fname)) {
printw("Cannot open file\n");
strcpy(fname, "");
}
}
break;
case 'Q':
tinybc_close(mt->ct);
return 0;
case 'R':
run(mt);
intro();
break;
case 'S':
p += strcspn(p, " ");
p += strspn(p, " ");
if (*p) {
f = fopen(p, "w");
fputs(mt->program, f);
fclose(f);
} else if (strcmp(fname, "")) {
f = fopen(fname, "w");
fputs(mt->program, f);
fclose(f);
}
break;
case 'H':
help();
break;
}
}
tinybc_close(mt->ct);
return 0;
}
if (argc == 2) {
run(mt);
tinybc_close(mt->ct);
return 0;
}
if (argc != 3 || !isdigit(argv[1][1])) {
tinybc_close(mt->ct);
return 1;
}
lastnum = atoi(argv[1] + 1);
for (p = mt->program; *p; p++) {
if (lastnum + 10 >= ARRSIZE || atoi(p) >= ARRSIZE) {
fclose(f);
return 1;
}
n = atoi(p);
mt->numbers[n] = lastnum;
lastnum += 10;
p += strcspn(p, "\n");
}
lastnum = atoi(argv[1] + 1);
f = fopen(argv[fileind], "w");
for (p = mt->program; *p; p++) {
if (*p != '\n') fprintf(f, "%d ", lastnum);
if (*p != '\n') p += strcspn(p, " \n");
p += strspn(p, " ");
lastnum += 10;
while (*p && *p != '\n')
if (!strncmp(p, "GOTO", strlen("GOTO"))) {
p1 = p;
p1 += strcspn(p1, " ");
p1 += strspn(p1, " ");
n = atoi(p1);
if (n >= ARRSIZE) return 1;
p1 += strcspn(p1, " \n");
p1 += strspn(p1, " ");
if (*p1 == '\n') {
fprintf(f, "GOTO %d",
mt->numbers[n]);
p += strcspn(p, "\n");
} else if (*p)
fputc(*p++, f);
} else if (!strncmp(p, "GOSUB", strlen("GOSUB"))) {
p1 = p;
p1 += strcspn(p1, " ");
p1 += strspn(p1, " ");
n = atoi(p1);
if (n >= ARRSIZE) return 1;
p1 += strcspn(p1, " \n");
p1 += strspn(p1, " ");
if (*p1 == '\n') {
fprintf(f, "GOSUB %d",
mt->numbers[n]);
p += strcspn(p, "\n");
} else if (*p)
fputc(*p++, f);
} else
fputc(*p++, f);
fprintf(f, "\n");
}
fprintf(f, "\n");
fclose(f);
return 0;
}