-
Notifications
You must be signed in to change notification settings - Fork 1
/
input.c
158 lines (138 loc) · 3.89 KB
/
input.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
/* input.c
*
* part of dbasic
* (C) k theis <theis.kurt@gmail.com> 2022
*
* BASIC INPUT statement
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "dbasic.h"
int run_input(char *line) {
extern float *NumVar[26];
extern char CharVars[][LINESIZE];
char getline[LINESIZE];
float eval(char *);
extern int NumSize[26];
extern FILE *fd[8];
extern int fdnumber;
int ISFILE=0; // flag for file operations
// debug
// printf("INPUT: %s\n",line);
while (isdigit(*line)) line++; // skip line number
if (isblank(*line)) while (isblank(*line)) line++; // skip spaces
while (isalpha(*line)) line++; // skip keyword
if (isblank(*line)) while (isblank(*line)) line++;
// test for file #
if (*line == '#') { // print to a file
ISFILE=1;
line++; // skip past #
if (!(isdigit(*line))) {
printf("Error - bad syntax in INPUT\n");
return -1;
}
fdnumber = atoi(line);
if (fdnumber < 1 || fdnumber > 7) { // 1-7 allowed
printf("Error - bad file number %d in INPUT\n",fdnumber);
return -1;
}
/* make sure it's already opened */
if (fd[fdnumber] == NULL) {
printf("Error - file number %d is not opened\n",fdnumber);
return -1;
}
line++; // skip file number
}
/* format: input a input #n a
* input a,b,c input #n a,b,c
* input "enter a",a input #n "enter a",a
* input a$ input #n a$
*/
while (1) { // loop and test until EOL
/* test for end of file - return error if reached */
if (ISFILE && feof(fd[fdnumber])) {
printf("Warning - Reached end of file\n");
return 0;
}
if (*line == '\0') break; // EOL
if (*line == ',' || *line == ';' || *line == ' ') { // ignore ,;[ ]
line++;
continue;
}
/* print all between " " */
if (*line == '"') {
line++; // skip initial "
while (1) {
if (*line == '\0' || *line == '\n') {
printf("Error - missing double quote\n");
return -1;
}
if (*line == '"') break;
if (!ISFILE) // don't show during file ops
printf("%c",*line++);
}
line++; // skip ending "
continue;
}
/* if a$-z$ get string input */
if (*line >= 'a' && *line <= 'z' && *(line+1) == '$') {
char strvar = *line; // get variable
memset(CharVars[strvar-'a'],0,sizeof(CharVars[strvar-'a']));
if (!ISFILE) printf("?");
if (!ISFILE) fgets(getline,LINESIZE,stdin);
if (ISFILE) {
fgets(getline,LINESIZE,fd[fdnumber]);
if (feof(fd[fdnumber])) {
printf("Warning - reached end of file\n");
return 0;
}
}
getline[strlen(getline)-1] = '\0'; // strip off \n
strcpy(CharVars[strvar-'a'],getline);
line += 2;
continue;
}
/* if a-z get numeric input */
if (*line >= 'a' && *line <= 'z' && *(line+1) != '$') {
int subscript = 0;
char charvar = *line;
line++;
if (*line == '(') { // using subscripts
line++;
char SUBNUM[LINESIZE]={'\0'};
int n=0;
while (1) {
if (*line == ')') break;
SUBNUM[n] = *line;
line++; n++;
}
subscript = eval(SUBNUM);
line++;
}
// error bounds checking
if (subscript < 0 || subscript > NumSize[charvar-'a']) {
printf("Error - bounds error in subscript: %c(%d)\n",charvar,subscript);
return -1;
}
if (!ISFILE) printf("?");
if (!ISFILE) fgets(getline,LINESIZE,stdin); // 20 chars max for digits
if (ISFILE) {
fgets(getline,LINESIZE,fd[fdnumber]);
if (feof(fd[fdnumber])) {
printf("Warning - reached end of file\n");
return 0;
}
}
NumVar[charvar-'a'][subscript] = eval(getline); // save value
line++;
continue;
}
/* unknown character */
printf("Error - unknown character %c \n",*line);
return -1;
}
return 0;
}