Debian Codegraph @

terps/taylor/parseinput.c

raw symbols SCIP
1 //
2 // parseinput.c
3 // Part of TaylorMade, an interpreter for Adventure Soft UK games
4 //
5 // Created by Petter Sjölund on 2022-06-04.
6 //
7
8 #include <ctype.h>
9 #include <stdlib.h>
10 #include <string.h>
11
12 #include "glk.h"
13
14 #include "extracommands.h"
15 #include "parseinput.h"
16 #include "taylor.h"
17 #include "utility.h"
18
19 // Input separated into word strings
20 char **InputWordStrings = NULL;
21 // The indices of the words in InputWordStrings, 0 if not found
22 uint8_t Word[5];
23 // The positions in InputWordStrings of the words in Word
24 int WordPositions[5];
25 // The number of word strings in InputWordStrings
26 int WordsInInput = 0;
27 // The index in InputWordStrings of the next command
28 int WordIndex = 0;
29
30 int FoundExtraCommand = 0;
31
32 void FreeInputWords(void)
33 {
34 WordIndex = 0;
35 if (InputWordStrings != NULL) {
36 for (int i = 0; i < WordsInInput && InputWordStrings[i] != NULL; i++) {
37 free(InputWordStrings[i]);
38 InputWordStrings[i] = NULL;
39 }
40 free(InputWordStrings);
41 InputWordStrings = NULL;
42 }
43 WordsInInput = 0;
44 }
45
46 void SplitIntoWords(const char *string, int length)
47 {
48 if (length < 1) {
49 return;
50 }
51
52 int startpos[MAX_WORDS];
53 int wordlength[MAX_WORDS];
54
55 int words_found = 0;
56 int word_index = 0;
57 int foundcomma = 0;
58 startpos[0] = 0;
59 wordlength[0] = 0;
60 int lastwasspace = 1;
61 for (int i = 0; string[i] != 0 && i < length && word_index < MAX_WORDS; i++) {
62 char c = string[i];
63 if (c == '.' || c == ',' || c == ';')
64 foundcomma = 1;
65 if (!isspace(c)) {
66 if (lastwasspace || foundcomma) {
67 /* Start a new word */
68 startpos[words_found] = i;
69 words_found++;
70 wordlength[words_found] = 0;
71 }
72 wordlength[words_found - 1]++;
73 lastwasspace = 0;
74 } else {
75 lastwasspace = 1;
76 foundcomma = 0;
77 }
78 }
79
80 if (words_found == 0) {
81 return;
82 }
83
84 wordlength[words_found]--; /* Don't count final newline character */
85
86 /* Now we've created two arrays, one for starting postions
87 and one for word length. Now we convert these into an array of strings */
88 char **words = MemAlloc(words_found * sizeof(*words));
89
90 debug_print("%d words found\n", words_found);
91 for (int i = 0; i < words_found; i++) {
92 words[i] = (char *)MemAlloc((wordlength[i] + 1));
93 memcpy(words[i], string + startpos[i], wordlength[i]);
94 words[i][wordlength[i]] = 0;
95 debug_print("Word %d: \"%s\"\n", i, words[i]);
96 }
97
98 WordsInInput = words_found;
99 InputWordStrings = words;
100 }
101
102 void LineInput(void)
103 {
104 event_t ev;
105 char buf[512];
106 FreeInputWords();
107 OutFlush();
108 FirstAfterInput = 0;
109 LastVerb = 0;
110 do {
111 LastChar = '\n';
112 if (Version == QUESTPROBE3_TYPE) {
113 if (MyLoc != 6) {
114 if (IsThing == 0)
115 SysMessage(8);
116 else
117 SysMessage(9);
118 }
119 OutCaps();
120 SysMessage(10);
121 } else
122 OutString("> ");
123 OutFlush();
124 glk_request_line_event(Bottom, buf, (glui32)511, 0);
125
126 while (1) {
127 glk_select(&ev);
128
129 if (ev.type == evtype_LineInput)
130 break;
131 else {
132 strid_t ParkedTranscript = Transcript;
133 Transcript = NULL;
134 Updates(ev);
135 Transcript = ParkedTranscript;
136 }
137 }
138
139 FirstAfterInput = 1;
140
141 int length = ev.val1;
142 buf[length] = 0;
143
144 SplitIntoWords(buf, length);
145
146 if (WordsInInput >= MAX_WORDS) {
147 Display(Bottom, "Too many words!\n");
148 FreeInputWords();
149 }
150
151 if (WordsInInput > 0 && InputWordStrings) {
152 return;
153 }
154
155 WordsInInput = 0;
156 Display(Bottom, "Huh?");
157 FirstAfterInput = 0;
158
159 } while (WordsInInput == 0 || InputWordStrings == NULL);
160 }
161
162 static const char *Abbreviations[] = { "I ", "L ", "X ", "Z ", "Q ", "Y ", NULL };
163 static const char *AbbreviationValue[] = { "INVE", "LOOK", "EXAM", "WAIT", "QUIT", "YES ", NULL };
164
165 int ParseWord(char *p)
166 {
167 char buf[5];
168 size_t len = strlen(p);
169 unsigned char *words = FileImage + VerbBase;
170 int i;
171
172 if (len >= 4) {
173 memcpy(buf, p, 4);
174 buf[4] = 0;
175 } else {
176 memcpy(buf, p, len);
177 memset(buf + len, ' ', 4 - len);
178 }
179 for (i = 0; i < 4; i++) {
180 if (buf[i] == 0)
181 break;
182 if (islower(buf[i]))
183 buf[i] = toupper(buf[i]);
184 }
185 while (*words != 126) {
186 if (memcmp(words, buf, 4) == 0)
187 return words[4];
188 words += 5;
189 }
190
191 words = FileImage + VerbBase;
192 for (i = 0; Abbreviations[i] != NULL; i++) {
193 if (memcmp(Abbreviations[i], buf, 4) == 0) {
194 while (*words != 126) {
195 if (memcmp(words, AbbreviationValue[i], 4) == 0)
196 return words[4];
197 words += 5;
198 }
199 }
200 }
201
202 if (ParseExtraCommand(p)) {
203 FoundExtraCommand = 1;
204 }
205 return 0;
206 }
207
208 static const char *Delimiters[] = { ",", ".", ";", "AND", "THEN", NULL };
209
210 static int IsDelimiterWord(char *word)
211 {
212 size_t len1 = strlen(word);
213 for (int i = 0; Delimiters[i] != NULL; i++) {
214 size_t len2 = strlen(Delimiters[i]);
215 if (len2 == len1) {
216 int found = 1;
217 for (int j = 0; j < len1; j++) {
218 if (toupper(word[j]) != Delimiters[i][j]) {
219 found = 0;
220 break;
221 }
222 }
223 if (found)
224 return 1;
225 }
226 }
227 return 0;
228 }
229
230 static int FindNextCommandDelimiter(void)
231 {
232 if (WordIndex >= WordsInInput - 1 || WordsInInput < 2)
233 return 0;
234 while (++WordIndex < WordsInInput) {
235 if (IsDelimiterWord(InputWordStrings[WordIndex])) {
236 WordIndex++;
237 while (WordIndex < WordsInInput && IsDelimiterWord(InputWordStrings[WordIndex]))
238 WordIndex++;
239 if (WordIndex >= WordsInInput)
240 return 0;
241 else
242 return 1;
243 }
244 }
245
246 return 0;
247 }
248
249 void Parser(void)
250 {
251 int i;
252
253 FoundExtraCommand = 0;
254
255 /* Is there input remaining to be analyzed? */
256 if (!FindNextCommandDelimiter()) {
257 LineInput();
258 if (WordsInInput == 0 || InputWordStrings == NULL)
259 return;
260 } else {
261 FirstAfterInput = 0;
262 OutChar(' ');
263 }
264
265 int wn = 0;
266
267 for (i = 0; i < 5 && WordIndex + i < WordsInInput; i++) {
268 Word[wn] = ParseWord(InputWordStrings[WordIndex + i]);
269 /* Hack for Blizzard Pass verbs */
270 if (CurrentGame == BLIZZARD_PASS && wn == 0) {
271 /* Change SKIN to VSKI */
272 if (Word[0] == 249)
273 Word[0] = 43;
274 /* Change POLI to VPOL */
275 else if (Word[0] == 175)
276 Word[0] = 44;
277 /* Change noun DRAW(bridge) to verb DRAW */
278 else if (Word[0] == 134)
279 Word[0] = 49;
280 }
281 if (Version == QUESTPROBE3_TYPE && wn == 1) {
282 /* Understand WALL and FIRE as the wall of fire */
283 if ((Word[1] == 54 || Word[1] == 44) && (ObjectLoc[10] == MyLoc || ObjectLoc[11] == MyLoc))
284 Word[1] = 77;
285 /* Understand MASTERS as Alicia Masters */
286 if (Word[1] == 106 && ObjectLoc[46] == MyLoc)
287 Word[1] = 121;
288 }
289 if (Word[wn]) {
290 debug_print("Word %d is %d\n", wn, Word[wn]);
291 WordPositions[wn] = WordIndex + i;
292 wn++;
293 } else {
294 if (wn > 0 && IsDelimiterWord(InputWordStrings[WordIndex + i]))
295 break;
296 debug_print("Word at position %d, %s, was not recognized\n", WordIndex + i, InputWordStrings[WordIndex + i]);
297 }
298 }
299 for (i = wn; i < 5; i++) {
300 Word[i] = 0;
301 WordPositions[i] = 0;
302 }
303 }