Debian Codegraph @

terps/scott/parser.c

raw symbols SCIP
1 //
2 // parser.c
3 // part of ScottFree, an interpreter for adventures in Scott Adams format
4 //
5 // Created by Petter Sjölund on 2022-01-19.
6 //
7
8 #include <stdarg.h>
9 #include <stdlib.h>
10 #include <string.h>
11
12 #include "parser.h"
13 #include "scott.h"
14 #include "scottdefines.h"
15
16 #include "bsd.h"
17
18 #define MAX_WORDLENGTH 128
19 #define MAX_WORDS 128
20 #define MAX_BUFFER 128
21
22 extern struct Command *CurrentCommand;
23
24 glui32 **UnicodeWords = NULL;
25 char **CharWords = NULL;
26 static int WordsInInput = 0;
27
28 static int lastnoun = 0;
29
30 static glui32 *FirstErrorMessage = NULL;
31
32 const char *EnglishDirections[NUMBER_OF_DIRECTIONS] = {
33 NULL, "north", "south", "east", "west", "up", "down",
34 "n", "s", "e", "w", "u", "d", " "
35 };
36 const char *SpanishDirections[NUMBER_OF_DIRECTIONS] = {
37 NULL, "norte", "sur", "este", "oeste", "arriba", "abajo",
38 "n", "s", "e", "o", "u", "d", "w"
39 };
40 const char *GermanDirections[NUMBER_OF_DIRECTIONS] = {
41 NULL, "norden", "sueden", "osten", "westen", "oben", "unten",
42 "n", "s", "o", "w", "u", "d", " "
43 };
44
45 const char *Directions[NUMBER_OF_DIRECTIONS];
46
47 const char *ExtraCommands[NUMBER_OF_EXTRA_COMMANDS] = {
48 NULL,
49 "restart",
50 "#restart",
51 "save",
52 "#save",
53 "restore",
54 "load",
55 "#restore",
56 "transcript",
57 "#transcript",
58 "script",
59 "#script",
60 "oops",
61 "undo",
62 "bom",
63 "#undo",
64 "ram",
65 "ramload",
66 "ramrestore",
67 "qload",
68 "quickload",
69 "#qload",
70 "ramsave",
71 "qsave",
72 "quicksave",
73 "#qsave",
74 "except",
75 "but",
76 "#flicker",
77 "", "", "", "", ""
78 };
79
80 const char *GermanExtraCommands[NUMBER_OF_EXTRA_COMMANDS] = {
81 NULL,
82 "restart",
83 "#restart",
84 "save",
85 "#save",
86 "restore",
87 "load",
88 "#restore",
89 "transcript",
90 "#transcript",
91 "script",
92 "#script",
93 "oops",
94 "undo",
95 "bom",
96 "#undo",
97 "ram",
98 "ramload",
99 "ramrestore",
100 "qload",
101 "quickload",
102 "#qload",
103 "ramsave",
104 "qsave",
105 "quicksave",
106 "#qsave",
107 "ausser",
108 "bis",
109 "#flicker",
110 "laden",
111 "wiederherstellen",
112 "transkript",
113 "rueckgaengig",
114 "neustarten"
115 };
116
117 const char *SpanishExtraCommands[NUMBER_OF_EXTRA_COMMANDS] = {
118 NULL,
119 "restart",
120 "#restart",
121 "save",
122 "#save",
123 "restore",
124 "load",
125 "#restore",
126 "transcript",
127 "#transcript",
128 "script",
129 "#script",
130 "oops",
131 "undo",
132 "bom",
133 "#undo",
134 "ram",
135 "ramload",
136 "ramrestore",
137 "qload",
138 "quickload",
139 "#qload",
140 "ramsave",
141 "qsave",
142 "quicksave",
143 "#qsave",
144 "excepto",
145 "menos",
146 "#flicker",
147 "reanuda",
148 "cargar",
149 "transcripcion",
150 "deshacer",
151 "reinicia"
152 };
153
154 extra_command ExtraCommandsKey[NUMBER_OF_EXTRA_COMMANDS] = {
155 NO_COMMAND, RESTART, RESTART, SAVE, SAVE, RESTORE, RESTORE,
156 RESTORE, SCRIPT, SCRIPT, SCRIPT, SCRIPT, UNDO, UNDO, UNDO, UNDO,
157 RAM, RAMLOAD, RAMLOAD, RAMLOAD, RAMLOAD, RAMLOAD, RAMSAVE,
158 RAMSAVE, RAMSAVE, RAMSAVE, EXCEPT, EXCEPT, FLICKER,
159 RESTORE, RESTORE, SCRIPT, UNDO, RESTART
160 };
161
162 const char *EnglishExtraNouns[NUMBER_OF_EXTRA_NOUNS] = {
163 NULL,
164 "game",
165 "story",
166 "on",
167 "off",
168 "load",
169 "restore",
170 "save",
171 "move",
172 "command",
173 "turn",
174 "all",
175 "everything",
176 "it",
177 " ",
178 " ",
179 };
180
181 const char *GermanExtraNouns[NUMBER_OF_EXTRA_NOUNS] = {
182 NULL, "spiel", "story", "on", "off", "wiederherstellen",
183 "laden", "speichern", "move", "verschieben", "runde",
184 "alle", "alles", "es", "einschalten", "ausschalten"
185 };
186
187 const char *SpanishExtraNouns[NUMBER_OF_EXTRA_NOUNS] = {
188 NULL, "juego", "story", "on", "off", "cargar",
189 "reanuda", "conserva", "move", "command", "jugada",
190 "toda", "todo", "eso", "activar", "desactivar"
191 };
192
193 const char *ExtraNouns[NUMBER_OF_EXTRA_NOUNS];
194
195 const extra_command ExtraNounsKey[NUMBER_OF_EXTRA_NOUNS] = {
196 NO_COMMAND, GAME, GAME, ON, OFF, RAMLOAD,
197 RAMLOAD, RAMSAVE, COMMAND, COMMAND, COMMAND,
198 ALL, ALL, IT, ON, OFF
199 };
200
201 #define NUMBER_OF_ABBREVIATIONS 6
202
203 const char *Abbreviations[NUMBER_OF_ABBREVIATIONS] = { NULL, "i", "l",
204 "x", "z", "q" };
205
206 const char *AbbreviationsKey[NUMBER_OF_ABBREVIATIONS] = {
207 NULL, "inventory", "look", "examine", "wait", "quit"
208 };
209
210 const char *EnglishSkipList[NUMBER_OF_SKIPPABLE_WORDS] = {
211 NULL, "at", "to", "in", "into", "the",
212 "a", "an", "my", "quickly", "carefully", "quietly",
213 "slowly", "violently", "fast", "hard", "now", "room"
214 };
215
216 const char *GermanSkipList[NUMBER_OF_SKIPPABLE_WORDS] = {
217 NULL, "nach", "die", "der", "das", "im", "mein", "meine", "an",
218 "auf", "den", "lassen", "lass", "fallen", " ", " ", " ", " "
219 };
220
221 const char *SkipList[NUMBER_OF_SKIPPABLE_WORDS];
222
223 const char *EnglishDelimiterList[NUMBER_OF_DELIMITERS] = { NULL, ",", "and",
224 "then", " " };
225
226 const char *GermanDelimiterList[NUMBER_OF_DELIMITERS] = { NULL, ",", "und",
227 "dann", "and" };
228
229 const char *DelimiterList[NUMBER_OF_DELIMITERS];
230
231 static void FreeStrings(void)
232 {
233 if (FirstErrorMessage != NULL) {
234 free(FirstErrorMessage);
235 FirstErrorMessage = NULL;
236 }
237 if (WordsInInput == 0) {
238 if (UnicodeWords != NULL || CharWords != NULL) {
239 Fatal("ERROR! Wordcount 0 but word arrays not empty!\n");
240 }
241 return;
242 }
243 for (int i = 0; i < WordsInInput; i++) {
244 if (UnicodeWords[i] != NULL)
245 free(UnicodeWords[i]);
246 if (CharWords[i] != NULL)
247 free(CharWords[i]);
248 }
249 free(UnicodeWords);
250 UnicodeWords = NULL;
251 free(CharWords);
252 CharWords = NULL;
253 WordsInInput = 0;
254 }
255
256 static void CreateErrorMessage(const char *fchar, glui32 *second, const char *tchar)
257 {
258 if (FirstErrorMessage != NULL)
259 return;
260 glui32 *first = ToUnicode(fchar);
261 glui32 *third = ToUnicode(tchar);
262 glui32 buffer[MAX_BUFFER];
263 int i, j = 0, k = 0;
264 for (i = 0; first[i] != 0 && i < MAX_BUFFER; i++)
265 buffer[i] = first[i];
266 if (second != NULL) {
267 for (j = 0; second[j] != 0 && i + j < MAX_BUFFER; j++)
268 buffer[i + j] = second[j];
269 }
270 if (third != NULL) {
271 for (k = 0; third[k] != 0 && i + j + k < MAX_BUFFER; k++)
272 buffer[i + j + k] = third[k];
273 free(third);
274 }
275 int length = i + j + k;
276 FirstErrorMessage = MemAlloc((length + 1) * 4);
277 memcpy(FirstErrorMessage, buffer, length * 4);
278 FirstErrorMessage[length] = 0;
279 free(first);
280 }
281
282 glui32 *ToUnicode(const char *string)
283 {
284 if (string == NULL)
285 return NULL;
286 glui32 unicode[2048];
287 int i;
288 int dest = 0;
289 for (i = 0; string[i] != 0 && i < 2047; i++) {
290 char c = string[i];
291 if (c == 10 || c == 13) {
292 lastwasnewline = 1;
293 c = 10;
294 } else
295 lastwasnewline = 0;
296 glui32 unichar = (glui32)c;
297 if (Game && (CurrentGame == GREMLINS_GERMAN || CurrentGame == GREMLINS_GERMAN_C64)) {
298 const char d = string[i + 1];
299 if (c == 'u' && d == 'e') { // ü
300 if (!(i > 2 && string[i - 1] == 'e')) {
301 unichar = 0xfc;
302 i++;
303 }
304 } else if (c == 'o' && d == 'e') {
305 unichar = 0xf6; // ö
306 i++;
307 } else if (c == 'a' && d == 'e') {
308 unichar = 0xe4; // ä
309 i++;
310 } else if (c == 's' && d == 's') {
311 if (string[i + 2] != 'c' && string[i - 2] != 'W' && !(string[i - 1] == 'a' && string[i - 2] == 'l') && string[i + 2] != '-' && string[i - 2] != 'b') {
312 unichar = 0xdf; // ß
313 i++;
314 }
315 } else if (c == 'U' && d == 'E') {
316 unichar = 0xdc; // Ü
317 i++;
318 }
319 if (c == '\"') {
320 unichar = 0x2019; // ’
321 }
322 } else if (Game && (CurrentGame == GREMLINS_SPANISH_C64 || CurrentGame == GREMLINS_SPANISH)) {
323 switch (c) {
324 case '\x83':
325 unichar = 0xbf; // ¿
326 break;
327 case '\x80':
328 unichar = 0xa1; // ¡
329 break;
330 case '\x82':
331 unichar = 0xfc; // ü
332 break;
333 case '{':
334 unichar = 0xe1; // á
335 break;
336 case '}':
337 unichar = 0xed; // í
338 break;
339 case '|':
340 unichar = 0xf3; // ó
341 break;
342 case '~':
343 unichar = 0xf1; // ñ
344 break;
345 case '\x84':
346 unichar = 0xe9; // é
347 break;
348 case '\x85':
349 unichar = 0xfa; // ú
350 break;
351 }
352 } else if (Game && CurrentGame == TI994A) {
353 switch (c) {
354 case '@':
355 unicode[dest++] = 0xa9;
356 unichar = ' ';
357 break;
358 case '}':
359 unichar = 0xfc;
360 break;
361 case 12:
362 unichar = 0xf6;
363 break;
364 case '{':
365 unichar = 0xe4;
366 break;
367 }
368 }
369 unicode[dest++] = unichar;
370 }
371 unicode[dest] = 0;
372 glui32 *result = MemAlloc((dest + 1) * 4);
373 memcpy(result, unicode, (dest + 1) * 4);
374 return result;
375 }
376
377 static char *FromUnicode(glui32 *unicode_string, int origlength)
378 {
379 int sourcepos = 0;
380 int destpos = 0;
381
382 char dest[MAX_WORDLENGTH];
383 glui32 unichar = unicode_string[sourcepos];
384 while (unichar != 0 && destpos < MAX_WORDLENGTH && sourcepos < origlength) {
385 switch (unichar) {
386 case '.':
387 case ',':
388 case ';':
389 if (origlength == 1) {
390 dest[destpos++] = 'a';
391 dest[destpos++] = 'n';
392 dest[destpos++] = 'd';
393 } else {
394 dest[destpos] = (char)unichar;
395 }
396 break;
397 case 0xf6: // ö
398 dest[destpos++] = 'o';
399 dest[destpos] = 'e';
400 break;
401 case 0xe4: // ä
402 dest[destpos++] = 'a';
403 dest[destpos] = 'e';
404 break;
405 case 0xfc: // ü
406 dest[destpos] = 'u';
407 if (CurrentGame == GREMLINS_GERMAN || CurrentGame == GREMLINS_GERMAN_C64) {
408 destpos++;
409 dest[destpos] = 'e';
410 }
411 break;
412 case 0xdf: // ß
413 dest[destpos++] = 's';
414 dest[destpos] = 's';
415 break;
416 case 0xed: // í
417 dest[destpos] = 'i';
418 break;
419 case 0xe1: // á
420 dest[destpos] = 'a';
421 break;
422 case 0xf3: // ó
423 dest[destpos] = 'o';
424 break;
425 case 0xf1: // ñ
426 dest[destpos] = 'n';
427 break;
428 case 0xe9: // é
429 dest[destpos] = 'e';
430 break;
431 default:
432 dest[destpos] = (char)unichar;
433 break;
434 }
435 sourcepos++;
436 destpos++;
437 unichar = unicode_string[sourcepos];
438 }
439 if (destpos == 0)
440 return NULL;
441 char *result = MemAlloc(destpos + 1);
442 memcpy(result, dest, destpos);
443
444 result[destpos] = 0;
445 return result;
446 }
447
448 static int MatchYMCA(glui32 *string, int length, int index)
449 {
450 const char *ymca = "y.m.c.a.";
451 int i;
452 for (i = 0; i < 8; i++) {
453 if (i + index > length || string[index + i] != ymca[i])
454 return i;
455 }
456 return i;
457 }
458
459 /* Turns a unicode glui32 string into lower-case ASCII. */
460 /* Converts German and Spanish diacritical characters into non-diacritical
461 * equivalents */
462 /* (for the translated Gremlins variants.) Coalesces all runs of whitespace into
463 * a single standard space. */
464 /* Turns ending commas and periods into separate strings. */
465 void SplitIntoWords(glui32 *string, int length)
466 {
467 if (length < 1) {
468 return;
469 }
470
471 glk_buffer_to_lower_case_uni(string, 512, MIN(length, 512));
472 glk_buffer_canon_normalize_uni(string, 512, MIN(length, 512));
473
474 int startpos[MAX_WORDS];
475 int wordlength[MAX_WORDS];
476
477 int words_found = 0;
478 int word_index = 0;
479 int foundspace = 0;
480 int foundcomma = 0;
481 startpos[0] = 0;
482 wordlength[0] = 0;
483 int lastwasspace = 1;
484 for (int i = 0; string[i] != 0 && i < length && word_index < MAX_WORDS; i++) {
485 foundspace = 0;
486 switch (string[i]) {
487 case 'y': {
488 int ymca = MatchYMCA(string, length, i);
489 if (ymca > 3) {
490 /* Start a new word */
491 startpos[words_found] = i;
492 wordlength[words_found] = ymca;
493 words_found++;
494 wordlength[words_found] = 0;
495 i += ymca;
496 if (i < length)
497 foundspace = 1;
498 lastwasspace = 0;
499 }
500 } break;
501 /* Unicode space and tab variants */
502 case ' ':
503 case '\t':
504 case '!':
505 case '?':
506 case '\"':
507 case 0x83: // ¿
508 case 0x80: // ¡
509 case 0xa0: // non-breaking space
510 case 0x2000: // en quad
511 case 0x2001: // em quad
512 case 0x2003: // em
513 case 0x2004: // three-per-em
514 case 0x2005: // four-per-em
515 case 0x2006: // six-per-em
516 case 0x2007: // figure space
517 case 0x2009: // thin space
518 case 0x200A: // hair space
519 case 0x202f: // narrow no-break space
520 case 0x205f: // medium mathematical space
521 case 0x3000: // ideographic space
522 foundspace = 1;
523 break;
524 case '.':
525 case ',':
526 case ';':
527 foundcomma = 1;
528 break;
529 default:
530 break;
531 }
532 if (!foundspace) {
533 if (lastwasspace || foundcomma) {
534 /* Start a new word */
535 startpos[words_found] = i;
536 words_found++;
537 wordlength[words_found] = 0;
538 }
539 wordlength[words_found - 1]++;
540 lastwasspace = 0;
541 } else {
542 /* Check if the last character of previous word was a period or comma */
543 lastwasspace = 1;
544 foundcomma = 0;
545 }
546 }
547
548 if (words_found == 0) {
549 return;
550 }
551
552 wordlength[words_found]--; /* Don't count final newline character */
553
554 /* Now we've created two arrays, one for starting postions
555 and one for word length. Now we convert these into an array of strings */
556 glui32 **words = MemAlloc(words_found * sizeof(*words));
557 char **words8 = MemAlloc(words_found * sizeof(*words8));
558
559 for (int i = 0; i < words_found; i++) {
560 words[i] = (glui32 *)MemAlloc((wordlength[i] + 1) * 4);
561 memcpy(words[i], string + startpos[i], wordlength[i] * 4);
562 words[i][wordlength[i]] = 0;
563 words8[i] = FromUnicode(words[i], wordlength[i]);
564 }
565 UnicodeWords = words;
566 WordsInInput = words_found;
567 CharWords = words8;
568 }
569
570 void LineInput(void)
571 {
572 event_t ev;
573 glui32 unibuf[512];
574
575 do {
576 Display(Bottom, "\n%s", sys[WHAT_NOW]);
577 glk_request_line_event_uni(Bottom, unibuf, (glui32)511, 0);
578
579 while (1) {
580 glk_select(&ev);
581
582 if (ev.type == evtype_LineInput)
583 break;
584 else
585 Updates(ev);
586 }
587
588 unibuf[ev.val1] = 0;
589
590 lastwasnewline = 1;
591
592 SplitIntoWords(unibuf, ev.val1);
593
594 if (WordsInInput == 0 || CharWords == NULL)
595 Output(sys[HUH]);
596 else {
597 return;
598 }
599
600 } while (WordsInInput == 0 || CharWords == NULL);
601 return;
602 }
603
604 int WhichWord(const char *word, const char **list, int word_length,
605 int list_length)
606 {
607 int n = 1;
608 int ne = 1;
609 const char *tp;
610 while (ne < list_length) {
611 tp = list[ne];
612 if (*tp == '*')
613 tp++;
614 else
615 n = ne;
616 if (xstrncasecmp(word, tp, word_length) == 0)
617 return (n);
618 ne++;
619 }
620 return (0);
621 }
622
623 /* For the verb position in a command string sequence, we try the following
624 lists in this order: Verbs, Directions, Abbreviations, SkipList, Nouns,
625 ExtraCommands, Delimiters */
626 static int FindVerb(const char *string, const char ***list)
627 {
628 *list = Verbs;
629 int verb = WhichWord(string, *list, GameHeader.WordLength, GameHeader.NumWords + 1);
630 if (verb) {
631 return verb;
632 }
633 *list = Directions;
634
635 verb = WhichWord(string, *list, GameHeader.WordLength, NUMBER_OF_DIRECTIONS);
636 if (verb) {
637 if (verb == 13)
638 verb = 4;
639 if (verb > 6)
640 verb -= 6;
641 return verb;
642 }
643 *list = Abbreviations;
644 verb = WhichWord(string, *list, GameHeader.WordLength, NUMBER_OF_ABBREVIATIONS);
645 if (verb) {
646 verb = WhichWord(AbbreviationsKey[verb], Verbs, GameHeader.WordLength,
647 GameHeader.NumWords + 1);
648 if (verb) {
649 *list = Verbs;
650 return verb;
651 }
652 }
653
654 int stringlength = strlen(string);
655
656 *list = SkipList;
657 verb = WhichWord(string, *list, stringlength, NUMBER_OF_SKIPPABLE_WORDS);
658 if (verb) {
659 return 0;
660 }
661 *list = Nouns;
662 verb = WhichWord(string, *list, GameHeader.WordLength, GameHeader.NumWords + 1);
663 if (verb) {
664 return verb;
665 }
666
667 *list = ExtraCommands;
668 verb = WhichWord(string, *list, stringlength, NUMBER_OF_EXTRA_COMMANDS);
669 if (verb) {
670 verb = ExtraCommandsKey[verb];
671 return verb + GameHeader.NumWords;
672 }
673
674 *list = ExtraNouns;
675 verb = WhichWord(string, *list, stringlength, NUMBER_OF_EXTRA_NOUNS);
676 if (verb) {
677 verb = ExtraNounsKey[verb];
678 return verb + GameHeader.NumWords;
679 }
680
681 *list = DelimiterList;
682 verb = WhichWord(string, *list, stringlength, NUMBER_OF_DELIMITERS);
683 if (!verb)
684 *list = NULL;
685 return verb;
686 }
687
688 /* For the noun position in a command string sequence, we try the following
689 lists in this order:
690 Nouns, Directions, ExtraNouns, SkipList, Verbs, Delimiters */
691 static int FindNoun(const char *string, const char ***list)
692 {
693 *list = Nouns;
694 int noun = WhichWord(string, *list, GameHeader.WordLength, GameHeader.NumWords + 1);
695 if (noun) {
696 return noun;
697 }
698
699 *list = Directions;
700 noun = WhichWord(string, *list, GameHeader.WordLength, NUMBER_OF_DIRECTIONS);
701 if (noun) {
702 if (noun > 6)
703 noun -= 6;
704 *list = Nouns;
705 return noun;
706 }
707
708 int stringlength = strlen(string);
709
710 *list = ExtraNouns;
711
712 noun = WhichWord(string, *list, stringlength, NUMBER_OF_EXTRA_NOUNS);
713 if (noun) {
714 noun = ExtraNounsKey[noun];
715 return noun + GameHeader.NumWords;
716 }
717
718 *list = SkipList;
719 noun = WhichWord(string, *list, stringlength, NUMBER_OF_SKIPPABLE_WORDS);
720 if (noun) {
721 return 0;
722 }
723
724 *list = Verbs;
725 noun = WhichWord(string, *list, GameHeader.WordLength, GameHeader.NumWords + 1);
726 if (noun) {
727 return noun;
728 }
729
730 *list = DelimiterList;
731 noun = WhichWord(string, *list, stringlength, NUMBER_OF_DELIMITERS);
732
733 if (!noun)
734 *list = NULL;
735 return 0;
736 }
737
738 static struct Command *CommandFromStrings(int index, struct Command *previous);
739
740 static int FindExtaneousWords(int *index, int noun)
741 {
742 /* Looking for extraneous words that should invalidate the command */
743 int original_index = *index;
744 if (*index >= WordsInInput) {
745 return 0;
746 }
747 const char **list = NULL;
748 int verb = 0;
749 int stringlength = strlen(CharWords[*index]);
750
751 int secondnoun = WhichWord(CharWords[*index], Nouns, GameHeader.WordLength, GameHeader.NumWords + 1);
752 if (secondnoun) {
753 if (MapSynonym(secondnoun) == MapSynonym(noun)) {
754 *index = *index + 1;
755 return 0;
756 }
757 }
758
759 list = SkipList;
760 do {
761 verb = WhichWord(CharWords[*index], SkipList, stringlength,
762 NUMBER_OF_SKIPPABLE_WORDS);
763 if (verb)
764 *index = *index + 1;
765 } while (verb && *index < WordsInInput);
766
767 if (*index >= WordsInInput)
768 return 0;
769
770 FindVerb(CharWords[*index], &list);
771
772 if (list == DelimiterList) {
773 if (*index > original_index)
774 *index = *index - 1;
775 return 0;
776 }
777
778 if (list == NULL) {
779 if (*index >= WordsInInput)
780 *index = WordsInInput - 1;
781 // debug_print("FindExtaneousWords Error: I don't know what a \"%s\" is\n", CharWords[*index]);
782 CreateErrorMessage(sys[I_DONT_KNOW_WHAT_A], UnicodeWords[*index], sys[IS]);
783 } else {
784 // debug_print("FindExtaneousWords Error: I don't understand\n");
785 CreateErrorMessage(sys[I_DONT_UNDERSTAND], NULL, NULL);
786 }
787
788 return 1;
789 }
790
791 static struct Command *CreateCommandStruct(int verb, int noun, int verbindex,
792 int nounindex, struct Command *previous)
793 {
794 struct Command *command = MemAlloc(sizeof(struct Command));
795 command->verb = verb;
796 command->noun = noun;
797 command->allflag = 0;
798 command->item = 0;
799 command->previous = previous;
800 command->verbwordindex = verbindex;
801 if (noun && nounindex > 0) {
802 command->nounwordindex = nounindex - 1;
803 } else {
804 command->nounwordindex = 0;
805 }
806 command->next = CommandFromStrings(nounindex, command);
807 return command;
808 }
809
810 static struct Command *CommandFromStrings(int index, struct Command *previous)
811 {
812 if (index < 0 || index >= WordsInInput) {
813 return NULL;
814 }
815 const char **list = NULL;
816 int verb = 0;
817 int i = index;
818
819 do {
820 /* Checking if it is a verb */
821 verb = FindVerb(CharWords[i++], &list);
822 } while ((list == SkipList || list == DelimiterList) && i < WordsInInput);
823
824 int verbindex = i - 1;
825
826 if (list == Directions) {
827 /* It is a direction */
828 if (verb == 0 || FindExtaneousWords(&i, 0) != 0)
829 return NULL;
830 return CreateCommandStruct(GO, verb, 0, i, previous);
831 }
832
833 int found_noun_at_verb_position = 0;
834 int lastverb = 0;
835
836 if (list == Nouns || list == ExtraNouns) {
837 /* It is a noun */
838 /* If we find no verb, we try copying the verb from the previous command */
839 if (previous) {
840 lastverb = previous->verb;
841 }
842 /* Unless the game is German, where we allow the noun to come before the verb */
843 if (CurrentGame != GREMLINS_GERMAN && CurrentGame != GREMLINS_GERMAN_C64) {
844 if (!previous) {
845 CreateErrorMessage(sys[I_DONT_KNOW_HOW_TO], UnicodeWords[i - 1],
846 sys[SOMETHING]);
847 return NULL;
848 } else {
849 verbindex = previous->verbwordindex;
850 }
851 if (FindExtaneousWords(&i, verb) != 0)
852 return NULL;
853
854 return CreateCommandStruct(lastverb, verb, verbindex, i, previous);
855 } else {
856 found_noun_at_verb_position = 1;
857 }
858 }
859
860 if (list == NULL || list == SkipList) {
861 CreateErrorMessage(sys[I_DONT_KNOW_HOW_TO], UnicodeWords[i - 1],
862 sys[SOMETHING]);
863 return NULL;
864 }
865
866 if (i == WordsInInput) {
867 if (lastverb) {
868 return CreateCommandStruct(lastverb, verb, previous->verbwordindex, i,
869 previous);
870 } else if (found_noun_at_verb_position) {
871 CreateErrorMessage(sys[I_DONT_KNOW_HOW_TO], UnicodeWords[i - 1],
872 sys[SOMETHING]);
873 return NULL;
874 } else {
875 return CreateCommandStruct(verb, 0, i - 1, i, previous);
876 }
877 }
878
879 int noun = 0;
880
881 do {
882 /* Check if it is a noun */
883 noun = FindNoun(CharWords[i++], &list);
884 } while (list == SkipList && i < WordsInInput);
885
886 if (list == Nouns || list == ExtraNouns) {
887 /* It is a noun */
888
889 /* Check if it is an ALL followed by EXCEPT */
890 int except = 0;
891 if (list == ExtraNouns && i < WordsInInput && noun - GameHeader.NumWords == ALL) {
892 int stringlength = strlen(CharWords[i]);
893 except = WhichWord(CharWords[i], ExtraCommands, stringlength,
894 NUMBER_OF_EXTRA_COMMANDS);
895 }
896 if (ExtraCommandsKey[except] != EXCEPT && FindExtaneousWords(&i, noun) != 0)
897 return NULL;
898 if (found_noun_at_verb_position) {
899 int realverb = WhichWord(CharWords[i - 1], Verbs, GameHeader.WordLength,
900 GameHeader.NumWords);
901 if (realverb) {
902 noun = verb;
903 verb = realverb;
904 } else if (lastverb) {
905 noun = verb;
906 verb = lastverb;
907 }
908 }
909 return CreateCommandStruct(verb, noun, verbindex, i, previous);
910 }
911
912 if (list == DelimiterList) {
913 /* It is a delimiter */
914 return CreateCommandStruct(verb, 0, verbindex, i, previous);
915 }
916
917 if (list == Verbs && found_noun_at_verb_position) {
918 /* It is a verb */
919 /* Check if it is an ALL followed by EXCEPT */
920 int except = 0;
921 if (i < WordsInInput && verb - GameHeader.NumWords == ALL) {
922 int stringlength = strlen(CharWords[i]);
923 except = WhichWord(CharWords[i], ExtraCommands, stringlength,
924 NUMBER_OF_EXTRA_COMMANDS);
925 }
926 if (ExtraCommandsKey[except] != EXCEPT && FindExtaneousWords(&i, 0) != 0)
927 return NULL;
928 return CreateCommandStruct(noun, verb, i - 1, i, previous);
929 }
930
931 CreateErrorMessage(sys[I_DONT_KNOW_WHAT_A], UnicodeWords[i - 1], sys[IS]);
932 return NULL;
933 }
934
935 static int CreateAllCommands(struct Command *command)
936 {
937
938 if (GameHeader.NumItems > 2048)
939 Fatal("Bad number of items");
940 int exceptions[2048];
941 int exceptioncount = 0;
942
943 int location = CARRIED;
944 if (command->verb == TAKE)
945 location = MyLoc;
946
947 struct Command *next = command->next;
948 /* Check if the ALL command is followed by EXCEPT */
949 /* and if it is, build an array of items to be excepted */
950 while (next && next->verb == GameHeader.NumWords + EXCEPT) {
951 for (int i = 0; i <= GameHeader.NumItems; i++) {
952 if (Items[i].AutoGet && xstrncasecmp(Items[i].AutoGet, CharWords[next->nounwordindex], GameHeader.WordLength) == 0) {
953 exceptions[exceptioncount++] = i;
954 }
955 }
956 /* Remove the EXCEPT command from the linked list of commands */
957 next = next->next;
958 free(command->next);
959 command->next = next;
960 }
961
962 struct Command *c = command;
963 int found = 0;
964 for (int i = 0; i < GameHeader.NumItems; i++) {
965 if (Items[i].AutoGet != NULL && Items[i].AutoGet[0] != '*' && Items[i].Location == location) {
966 int exception = 0;
967 for (int j = 0; j < exceptioncount; j++) {
968 if (exceptions[j] == i) {
969 exception = 1;
970 break;
971 }
972 }
973 if (!exception) {
974 if (found) {
975 c->next = MemAlloc(sizeof(struct Command));
976 c->next->previous = c;
977 c = c->next;
978 }
979 found = 1;
980 c->verb = command->verb;
981 c->noun = WhichWord(Items[i].AutoGet, Nouns, GameHeader.WordLength,
982 GameHeader.NumWords);
983 c->item = i;
984 c->next = NULL;
985 c->nounwordindex = 0;
986 c->allflag = 1;
987 }
988 }
989 }
990 if (found == 0) {
991 if (command->verb == TAKE)
992 CreateErrorMessage(sys[NOTHING_HERE_TO_TAKE], NULL, NULL);
993 else
994 CreateErrorMessage(sys[YOU_HAVE_NOTHING], NULL, NULL);
995 return 0;
996 } else {
997 c->next = next;
998 c->allflag = 1 | LASTALL;
999 }
1000 return 1;
1001 }
1002
1003 void FreeCommands(void)
1004 {
1005 while (CurrentCommand && CurrentCommand->previous)
1006 CurrentCommand = CurrentCommand->previous;
1007 while (CurrentCommand) {
1008 struct Command *temp = CurrentCommand;
1009 CurrentCommand = CurrentCommand->next;
1010 free(temp);
1011 }
1012 CurrentCommand = NULL;
1013 FreeStrings();
1014 if (FirstErrorMessage)
1015 free(FirstErrorMessage);
1016 FirstErrorMessage = NULL;
1017 }
1018
1019 static void PrintPendingError(void)
1020 {
1021 if (FirstErrorMessage) {
1022 glk_put_string_stream_uni(glk_window_get_stream(Bottom), FirstErrorMessage);
1023 free(FirstErrorMessage);
1024 FirstErrorMessage = NULL;
1025 StopTime = 1;
1026 }
1027 }
1028
1029 int GetInput(int *vb, int *no)
1030 {
1031 if (CurrentCommand && CurrentCommand->next) {
1032 CurrentCommand = CurrentCommand->next;
1033 } else {
1034 PrintPendingError();
1035 if (CurrentCommand)
1036 FreeCommands();
1037 LineInput();
1038 CurrentCommand = CommandFromStrings(0, NULL);
1039 }
1040
1041 if (CurrentCommand == NULL) {
1042 PrintPendingError();
1043 return 1;
1044 }
1045
1046 /* Hack to make ALLE FALLEN LASSEN work in German Gremlins */
1047 /* The normal verb <-> noun switching mechanism gets confused */
1048 /* by the fact that the game lists FALLEN and LASSEN as both */
1049 /* verbs and nouns. */
1050
1051 if ((CurrentGame == GREMLINS_GERMAN || CurrentGame == GREMLINS_GERMAN_C64) && CurrentCommand->verb - GameHeader.NumWords == ALL && CurrentCommand->noun == 123) {
1052 CurrentCommand->verb = DROP;
1053 CurrentCommand->noun = ALL + GameHeader.NumWords;
1054 }
1055
1056 /* Hack to make RESTORE and RESTART work in Robin of Sherwood */
1057 /* instead of being understood as REST, a synonym of WAIT. */
1058 if (Game->type == SHERWOOD_VARIANT && CurrentCommand->verb == 56) {
1059 char *verbword = CharWords[CurrentCommand->verbwordindex];
1060 if (xstrncasecmp(verbword, "restore", 7) == 0)
1061 CurrentCommand->verb = GameHeader.NumWords + RESTORE;
1062 else if (xstrncasecmp(verbword, "restart", 7) == 0)
1063 CurrentCommand->verb = GameHeader.NumWords + RESTART;
1064 }
1065
1066 /* We use NumWords + verb for our extra commands */
1067 /* such as UNDO and TRANSCRIPT */
1068 if (CurrentCommand->verb > GameHeader.NumWords) {
1069 if (!PerformExtraCommand(0)) {
1070 CreateErrorMessage(sys[I_DONT_UNDERSTAND], NULL, NULL);
1071 }
1072 return 1;
1073 /* And NumWords + noun for our extra nouns */
1074 /* such as ALL */
1075 } else if (CurrentCommand->noun > GameHeader.NumWords) {
1076 CurrentCommand->noun -= GameHeader.NumWords;
1077 if (CurrentCommand->noun == ALL) {
1078 if (CurrentCommand->verb != TAKE && CurrentCommand->verb != DROP) {
1079 CreateErrorMessage(sys[CANT_USE_ALL], NULL, NULL);
1080 return 1;
1081 }
1082 if (!CreateAllCommands(CurrentCommand))
1083 return 1;
1084 } else if (CurrentCommand->noun == IT) {
1085 CurrentCommand->noun = lastnoun;
1086 }
1087 }
1088
1089 *vb = CurrentCommand->verb;
1090 *no = CurrentCommand->noun;
1091
1092 if (*no > 6) {
1093 lastnoun = *no;
1094 }
1095
1096 return 0;
1097 }
1098
1099 int RecheckForExtraCommand(void)
1100 {
1101 const char *VerbWord = CharWords[CurrentCommand->verbwordindex];
1102
1103 int ExtraVerb = WhichWord(VerbWord, ExtraCommands, GameHeader.WordLength,
1104 NUMBER_OF_EXTRA_COMMANDS);
1105 if (!ExtraVerb) {
1106 return 0;
1107 }
1108 int ExtraNoun = 0;
1109 if (CurrentCommand->noun) {
1110 const char *NounWord = CharWords[CurrentCommand->nounwordindex];
1111 ExtraNoun = WhichWord(NounWord, ExtraNouns, strlen(NounWord),
1112 NUMBER_OF_EXTRA_NOUNS);
1113 }
1114 CurrentCommand->verb = ExtraCommandsKey[ExtraVerb];
1115 if (ExtraNoun)
1116 CurrentCommand->noun = ExtraNounsKey[ExtraNoun];
1117
1118 return PerformExtraCommand(1);
1119 }