Debian Codegraph @

terps/tads/tads2/vocab.c

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header: d:/cvsroot/tads/TADS2/vocab.c,v 1.5 1999/07/11 00:46:35 MJRoberts Exp $";
4 #endif
5
6 /*
7 * Copyright (c) 1987-1992 by Michael J. Roberts. All Rights Reserved.
8 *
9 * Please see the accompanying license file, LICENSE.TXT, for information
10 * on using and copying this software.
11 */
12 /*
13 Name
14 vocab - TADS run-time player command parser
15 Function
16 Player command parser
17 Notes
18 This version of the parser is for TADS 2.0.
19 Returns
20 0 for success, 1 for failure
21 Modified
22 04/11/99 CNebel - Fix C++ errors.
23 03/11/92 MJRoberts - TADS 2.0
24 11/20/91 MJRoberts - fix isVisible operation
25 11/02/91 MJRoberts - fix strObj.value problem
26 08/13/91 MJRoberts - add him/her support
27 08/12/91 MJRoberts - make strObj.value an RSTRING (preprsfn arg, too)
28 08/08/91 MJRoberts - add preprsfn (preparse function)
29 04/12/91 MJRoberts - check abortcmd: if true, skip rest of cmd line
30 03/10/91 MJRoberts - moved getstring() to getstr.c for modularity,
31 pick up John's qa-scripter mods
32 06/28/89 MJRoberts - call rtreset() before pardonfn invocations
33 11/04/88 MJRoberts - fix "it" and "them"
34 10/30/88 MJRoberts - new "version 6" game/parser interface
35 12/27/87 MJRoberts - created
36 */
37
38 #include <stdio.h>
39 #include <ctype.h>
40 #include <string.h>
41 #include <stdlib.h>
42 #include <stdarg.h>
43
44 #include "os.h"
45 #include "err.h"
46 #include "voc.h"
47 #include "tio.h"
48 #include "mcm.h"
49 #include "obj.h"
50 #include "prp.h"
51 #include "run.h"
52 #include "lst.h"
53
54
55 static char *type_names[] =
56 {
57 "article", "adj", "noun", "prep", "verb", "special", "plural",
58 "unknown"
59 };
60
61 /* array of flag values for words by part of speech */
62 static int voctype[] =
63 { 0, 0, VOCT_VERB, VOCT_NOUN, VOCT_ADJ, VOCT_PREP, VOCT_ARTICLE };
64
65 /* ------------------------------------------------------------------------ */
66 /*
67 * Allocate and push a list, given the number of bytes needed for the
68 * elements of the list.
69 */
70 uchar *voc_push_list_siz(voccxdef *ctx, uint lstsiz)
71 {
72 runcxdef *rcx = ctx->voccxrun;
73 runsdef val;
74 uchar *lstp;
75
76 /* add in the size needed for the list's length prefix */
77 lstsiz += 2;
78
79 /* allocate space in the heap */
80 runhres(rcx, lstsiz, 0);
81
82 /* set up a stack value to push */
83 val.runstyp = DAT_LIST;
84 val.runsv.runsvstr = lstp = ctx->voccxrun->runcxhp;
85
86 /* set up the list's length prefix */
87 oswp2(lstp, lstsiz);
88 lstp += 2;
89
90 /* commit the space in the heap */
91 rcx->runcxhp += lstsiz;
92
93 /* push the list value (repush, since we can use the original copy) */
94 runrepush(rcx, &val);
95
96 /* return the list element pointer */
97 return lstp;
98 }
99
100 /*
101 * Allocate and push a list. Returns a pointer to the space for the
102 * list's first element in the heap.
103 */
104 static uchar *voc_push_list(voccxdef *ctx, int ele_count, int ele_size)
105 {
106 uint lstsiz;
107
108 /*
109 * Figure the list size - we need space for the given number of
110 * elements of the given size; in addition, each element requires
111 * one byte of overhead for its type prefix.
112 */
113 lstsiz = (uint)(ele_count * (1 + ele_size));
114
115 /* allocate and return the list */
116 return voc_push_list_siz(ctx, lstsiz);
117 }
118
119 /*
120 * Push a list of numbers
121 */
122 static void voc_push_numlist(voccxdef *ctx, uint numlist[], int cnt)
123 {
124 int i;
125 uchar *lstp;
126
127 /* allocate space for the list of numbers */
128 lstp = voc_push_list(ctx, cnt, 4);
129
130 /* enter the list elements */
131 for (i = 0 ; i < cnt ; ++i)
132 {
133 /* add the type prefix */
134 *lstp++ = DAT_NUMBER;
135
136 /* add the value */
137 oswp4(lstp, numlist[i]);
138 lstp += 4;
139 }
140 }
141
142 /*
143 * Push a list of object ID's obtained from a vocoldef array
144 */
145 void voc_push_vocoldef_list(voccxdef *ctx, vocoldef *objlist, int cnt)
146 {
147 int i;
148 uchar *lstp;
149 uint lstsiz;
150
151 /*
152 * count the size - we need 3 bytes per object (1 for type plus 2
153 * for the value), and 1 byte per nil
154 */
155 for (lstsiz = 0, i = 0 ; i < cnt ; ++i)
156 lstsiz += (objlist[i].vocolobj == MCMONINV ? 1 : 3);
157
158 /* allocate space for the list */
159 lstp = voc_push_list_siz(ctx, lstsiz);
160
161 /* enter the list elements */
162 for (i = 0 ; i < cnt ; ++i)
163 {
164 if (objlist[i].vocolobj == MCMONINV)
165 {
166 /* store the nil */
167 *lstp++ = DAT_NIL;
168 }
169 else
170 {
171 /* add the type prefix */
172 *lstp++ = DAT_OBJECT;
173
174 /* add the value */
175 oswp2(lstp, objlist[i].vocolobj);
176 lstp += 2;
177 }
178 }
179 }
180
181 /*
182 * Push a list of object ID's
183 */
184 void voc_push_objlist(voccxdef *ctx, objnum objlist[], int cnt)
185 {
186 int i;
187 uchar *lstp;
188 uint lstsiz;
189
190 /*
191 * count the size - we need 3 bytes per object (1 for type plus 2
192 * for the value), and 1 byte per nil
193 */
194 for (lstsiz = 0, i = 0 ; i < cnt ; ++i)
195 lstsiz += (objlist[i] == MCMONINV ? 1 : 3);
196
197 /* allocate space for the list */
198 lstp = voc_push_list_siz(ctx, lstsiz);
199
200 /* enter the list elements */
201 for (i = 0 ; i < cnt ; ++i)
202 {
203 if (objlist[i] == MCMONINV)
204 {
205 /* store the nil */
206 *lstp++ = DAT_NIL;
207 }
208 else
209 {
210 /* add the type prefix */
211 *lstp++ = DAT_OBJECT;
212
213 /* add the value */
214 oswp2(lstp, objlist[i]);
215 lstp += 2;
216 }
217 }
218 }
219
220 /*
221 * Push a list of strings, where the strings are stored in memory, one
222 * after the other, each string separated from the next with a null
223 * byte. The list is bounded by firstwrd and lastwrd, inclusive of
224 * both.
225 */
226 static void voc_push_strlist(voccxdef *ctx, char *firstwrd, char *lastwrd)
227 {
228 size_t curlen;
229 char *p;
230 uint lstsiz;
231 uchar *lstp;
232
233 /*
234 * Determine how much space we need for the word list. For each
235 * entry, we need one byte for the type prefix, two bytes for the
236 * length prefix, and the bytes of the string itself.
237 */
238 for (lstsiz = 0, p = firstwrd ; p != 0 && p <= lastwrd ; p += curlen + 1)
239 {
240 curlen = strlen(p);
241 lstsiz += curlen + (1+2);
242 }
243
244 /* allocate space for the word list */
245 lstp = voc_push_list_siz(ctx, lstsiz);
246
247 /* enter the list elements */
248 for (p = firstwrd ; p != 0 && p <= lastwrd ; p += curlen + 1)
249 {
250 /* add the type prefix */
251 *lstp++ = DAT_SSTRING;
252
253 /* add the length prefix for this string */
254 curlen = strlen(p);
255 oswp2(lstp, curlen + 2);
256 lstp += 2;
257
258 /* add this string */
259 memcpy(lstp, p, curlen);
260 lstp += curlen;
261 }
262 }
263
264 /*
265 * Push a list of strings, taking the strings from an array.
266 */
267 static void voc_push_strlist_arr(voccxdef *ctx, char *wordlist[], int cnt)
268 {
269 int i;
270 char **p;
271 uint lstsiz;
272 uchar *lstp;
273
274 /*
275 * Add up the lengths of the strings in the array. For each
276 * element, we need space for the string's bytes, plus two bytes for
277 * the length prefix, plus one byte for the type prefix.
278 */
279 for (lstsiz = 0, p = wordlist, i = 0 ; i < cnt ; ++i, ++p)
280 lstsiz += strlen(*p) + 3;
281
282 /* allocate space for the list */
283 lstp = voc_push_list_siz(ctx, lstsiz);
284
285 /* enter the list elements */
286 for (p = wordlist, i = 0 ; i < cnt ; ++i, ++p)
287 {
288 size_t curlen;
289
290 /* add the type prefix */
291 *lstp++ = DAT_SSTRING;
292
293 /* add the length prefix for this string */
294 curlen = strlen(*p);
295 oswp2(lstp, curlen + 2);
296 lstp += 2;
297
298 /* add this string */
299 memcpy(lstp, *p, curlen);
300 lstp += curlen;
301 }
302 }
303
304 /*
305 * Push a list of strings, taking the strings from an array that was
306 * prepared by the parser tokenizer. This is almost the same as pushing
307 * a regular string array, with the difference that we must recognize
308 * the special format that the tokenizer uses to store string tokens.
309 */
310 static void voc_push_toklist(voccxdef *ctx, char *wordlist[], int cnt)
311 {
312 int i;
313 char **p;
314 uint lstsiz;
315 uchar *lstp;
316 size_t cur_len;
317
318 /*
319 * Add up the lengths of the strings in the array. For each
320 * element, we need space for the string's bytes, plus two bytes for
321 * the length prefix, plus one byte for the type prefix.
322 */
323 for (lstsiz = 0, p = wordlist, i = 0 ; i < cnt ; ++i, ++p)
324 {
325 /*
326 * get the length of the current token - check what kind of
327 * token we have, since we must sense the length of different
328 * token types in different ways
329 */
330 if (**p == '"')
331 {
332 /*
333 * It's a string token - the string follows with a two-byte
334 * length prefix; add two bytes for the open and close quote
335 * characters that we'll add to the output string. Note
336 * that we must deduct two bytes from the prefix length,
337 * because the prefix includes the size of the prefix
338 * itself, which we're not copying and will account for
339 * separately in the result string.
340 */
341 cur_len = osrp2(*p + 1) - 2 + 2;
342 }
343 else
344 {
345 /* for anything else, it's just a null-terminated string */
346 cur_len = strlen(*p);
347 }
348
349 /* add the current length to the total so far */
350 lstsiz += cur_len + 3;
351 }
352
353 /* allocate space for the list */
354 lstp = voc_push_list_siz(ctx, lstsiz);
355
356 /* enter the list elements */
357 for (p = wordlist, i = 0 ; i < cnt ; ++i, ++p)
358 {
359 char *cur_ptr;
360 size_t copy_len;
361
362 /* add the type prefix */
363 *lstp++ = DAT_SSTRING;
364
365 /* get the information for the string based on the type */
366 if (**p == '"')
367 {
368 /*
369 * it's a string - use the length prefix (deducting two
370 * bytes for the prefix itself, which we're not copying)
371 */
372 copy_len = osrp2(*p + 1) - 2;
373
374 /* add space in the result for the open and close quotes */
375 cur_len = copy_len + 2;
376
377 /* the string itself follows the length prefix and '"' flag */
378 cur_ptr = *p + 3;
379 }
380 else
381 {
382 /* for anything else, it's just a null-terminated string */
383 cur_len = copy_len = strlen(*p);
384 cur_ptr = *p;
385 }
386
387 /* write the length prefix for this string */
388 oswp2(lstp, cur_len + 2);
389 lstp += 2;
390
391 /* add the open quote if this is a quoted string */
392 if (**p == '"')
393 *lstp++ = '"';
394
395 /* add this string */
396 memcpy(lstp, cur_ptr, copy_len);
397 lstp += copy_len;
398
399 /* add the close quote if it's a quoted string */
400 if (**p == '"')
401 *lstp++ = '"';
402 }
403 }
404
405 /* ------------------------------------------------------------------------ */
406 /*
407 * Read a command from the keyboard, doing all necessary output flushing
408 * and prompting.
409 */
410 int vocread(voccxdef *ctx, objnum actor, objnum verb,
411 char *buf, int bufl, int type)
412 {
413 char *prompt;
414 int ret;
415
416 /* presume we'll return success */
417 ret = VOCREAD_OK;
418
419 /* make sure output capturing is off */
420 tiocapture(ctx->voccxtio, (mcmcxdef *)0, FALSE);
421 tioclrcapture(ctx->voccxtio);
422
423 /*
424 * Clear out the command buffer. This is important for the
425 * timeout-based command reader, since it will take what's in the
426 * buffer as the initial contents of the command line; this lets us
427 * remember any partial line that the player entered before a
428 * timeout interrupted their typing and redisplay the original
429 * partial line on the next command line. Initially, there's no
430 * partial line, so clear it out.
431 */
432 buf[0] = '\0';
433
434 /* show the game-defined prompt, if appropriate */
435 if (ctx->voccxprom != MCMONINV)
436 {
437 runpnum(ctx->voccxrun, (long)type);
438 runfn(ctx->voccxrun, ctx->voccxprom, 1);
439 tioflushn(ctx->voccxtio, 0);
440 prompt = "";
441 }
442 else
443 {
444 /* there's no game-defined prompt - use our default */
445 tioblank(tio);
446 prompt = ">";
447 }
448
449 /* get a line of input */
450 if (tiogets(ctx->voccxtio, prompt, buf, bufl))
451 errsig(ctx->voccxerr, ERR_RUNQUIT);
452
453 /* abort immediately if we see the special panic command */
454 if (!strcmp(buf, "$$ABEND"))
455 {
456 /* make sure any script file is closed */
457 qasclose();
458
459 /* use the OS-level termination */
460 os_term(OSEXFAIL);
461
462 /* if that returned, signal a quit */
463 errsig(ctx->voccxerr, ERR_RUNQUIT);
464 }
465
466 /* call the post-prompt function if defined */
467 if (ctx->voccxpostprom != MCMONINV)
468 {
469 runpnum(ctx->voccxrun, (long)type);
470 runfn(ctx->voccxrun, ctx->voccxpostprom, 1);
471 }
472
473 /*
474 * If this isn't a type "0" input, and preparseExt() is defined, call
475 * it. Don't call preparseExt() for type "0" inputs, since these will
476 * be handled via the conventional preparse().
477 */
478 if (ctx->voccxpre2 != MCMONINV && type != 0)
479 {
480 uchar *s;
481 size_t len;
482
483 /* push the arguments - actor, verb, str, type */
484 runpnum(ctx->voccxrun, (long)type);
485 runpstr(ctx->voccxrun, buf, (int)strlen(buf), 0);
486 runpobj(ctx->voccxrun, verb);
487 runpobj(ctx->voccxrun, actor);
488
489 /* call preparseExt() */
490 runfn(ctx->voccxrun, ctx->voccxpre2, 4);
491
492 /* check the result */
493 switch(runtostyp(ctx->voccxrun))
494 {
495 case DAT_SSTRING:
496 /*
497 * They returned a string. Replace the input buffer we read
498 * with the new string. Pop the new string and scan its length
499 * prefix.
500 */
501 s = runpopstr(ctx->voccxrun);
502 len = osrp2(s) - 2;
503 s += 2;
504
505 /*
506 * limit the size we copy to our buffer length (leaving space
507 * for null termination)
508 */
509 if (len > (size_t)bufl - 1)
510 len = bufl - 1;
511
512 /* copy the new command string into our buffer */
513 memcpy(buf, s, len);
514
515 /* null-terminate the result */
516 buf[len] = '\0';
517
518 /* proceed as normal with the new string */
519 break;
520
521 case DAT_TRUE:
522 /*
523 * they simply want to keep the current string as it is -
524 * proceed as normal
525 */
526 break;
527
528 case DAT_NIL:
529 /*
530 * They want to skip the special interpretation of the input
531 * and proceed directly to treating the input as a brand new
532 * command. The caller will have to take care of the details;
533 * we need only indicate this to the caller through our "redo"
534 * result code.
535 */
536 ret = VOCREAD_REDO;
537 break;
538 }
539 }
540
541 /* return our result */
542 return ret;
543 }
544
545 /*
546 * Compare a pair of words, truncated to six characters or the
547 * length of the first word, whichever is longer. (The first word is
548 * the user's entry, the second is the reference word in the dictionary.)
549 * Returns TRUE if the words match, FALSE otherwise.
550 */
551 static int voceq(uchar *s1, uint l1, uchar *s2, uint l2)
552 {
553 uint i;
554
555 if (l1 == 0 && l2 == 0) return(TRUE); /* both NULL - a match */
556 if (l1 == 0 || l2 == 0) return(FALSE); /* one NULL only - not a match */
557 if (l1 >= 6 && l2 >= l1) l2 = l1;
558 if (l1 != l2) return(FALSE); /* ==> not equal */
559 for (i = 0 ; i < l1 ; i++)
560 if (*s1++ != *s2++) return(FALSE);
561 return(TRUE); /* strings match */
562 }
563
564 /* find the next word in a search */
565 vocwdef *vocfnw(voccxdef *voccx, vocseadef *search_ctx)
566 {
567 vocdef *v, *vf;
568 vocwdef *vw, *vwf;
569 vocdef *c = search_ctx->v;
570 int first;
571
572 /* continue with current word's vocwdef list if anything is left */
573 first = TRUE;
574 vw = vocwget(voccx, search_ctx->vw->vocwnxt);
575
576 /* keep going until we run out of hash chain entries or find a match */
577 for (v = c, vf = 0 ; v != 0 && vf == 0 ; v = v->vocnxt, first = FALSE)
578 {
579 /* if this word matches, look at the objects in its list */
580 if (first
581 || (voceq(search_ctx->wrd1, search_ctx->len1,
582 v->voctxt, v->voclen)
583 && voceq(search_ctx->wrd2, search_ctx->len2,
584 v->voctxt + v->voclen, v->vocln2)))
585 {
586 /*
587 * on the first time through, vw has already been set up
588 * with the next vocwdef in the current list; on subsequent
589 * times through the loop, start at the head of the current
590 * word's list
591 */
592 if (!first)
593 vw = vocwget(voccx, v->vocwlst);
594
595 /* search the list from vw forward */
596 for ( ; vw ; vw = vocwget(voccx, vw->vocwnxt))
597 {
598 if (search_ctx->vw->vocwtyp == vw->vocwtyp
599 && !(vw->vocwflg & VOCFCLASS)
600 && !(vw->vocwflg & VOCFDEL))
601 {
602 /*
603 * remember the first vocdef that we found, and
604 * remember this, the first matching vocwdef, then
605 * stop scanning
606 */
607 vf = v;
608 vwf = vw;
609 break;
610 }
611 }
612 }
613 }
614
615 /* return the first vocwdef in this word's list */
616 search_ctx->v = vf;
617 search_ctx->vw = (vf ? vwf : 0);
618 return(search_ctx->vw);
619 }
620
621 /* find the first vocdef matching a set of words */
622 vocwdef *vocffw(voccxdef *ctx, char *wrd, int len, char *wrd2, int len2,
623 int p, vocseadef *search_ctx)
624 {
625 uint hshval;
626 vocdef *v, *vf;
627 vocwdef *vw, *vwf;
628
629 /* get the word's hash value */
630 hshval = vochsh((uchar *)wrd, len);
631
632 /* scan the hash list until we run out of entries, or find a match */
633 for (v = ctx->voccxhsh[hshval], vf = 0 ; v != 0 && vf == 0 ;
634 v = v->vocnxt)
635 {
636 /* if this word matches, look at the objects in its list */
637 if (voceq((uchar *)wrd, len, v->voctxt, v->voclen)
638 && voceq((uchar *)wrd2, len2, v->voctxt + v->voclen, v->vocln2))
639 {
640 /* look for a suitable object in the vocwdef list */
641 for (vw = vocwget(ctx, v->vocwlst) ; vw ;
642 vw = vocwget(ctx, vw->vocwnxt))
643 {
644 if (vw->vocwtyp == p && !(vw->vocwflg & VOCFCLASS)
645 && !(vw->vocwflg & VOCFDEL))
646 {
647 /*
648 * remember the first vocdef that we found, and
649 * remember this, the first matching vocwdef; then
650 * stop scanning, since we have a match
651 */
652 vf = v;
653 vwf = vw;
654 break;
655 }
656 }
657 }
658 }
659
660 /* set up the caller-provided search structure for next time */
661 vw = (vf != 0 ? vwf : 0);
662 if (search_ctx)
663 {
664 /* save the search position */
665 search_ctx->v = vf;
666 search_ctx->vw = vw;
667
668 /* save the search criteria */
669 search_ctx->wrd1 = (uchar *)wrd;
670 search_ctx->len1 = len;
671 search_ctx->wrd2 = (uchar *)wrd2;
672 search_ctx->len2 = len2;
673 }
674
675 /* return the match */
676 return vw;
677 }
678
679 /* ------------------------------------------------------------------------ */
680 /*
681 * vocerr_va information structure. This is initialized in the call to
682 * vocerr_va_prep(), and must then be passed to vocerr_va().
683 */
684 struct vocerr_va_info
685 {
686 /* parseError/parseErrorParam result */
687 char user_msg[400];
688
689 /* the sprintf-style format string to display */
690 char *fmt;
691
692 /*
693 * Pointer to the output buffer to use to format the string 'fmt' with
694 * its arguments, using vsprintf. The prep function will set this up
695 * to point to user_msg[].
696 */
697 char *outp;
698
699 /* size of the output buffer, in bytes */
700 size_t outsiz;
701 };
702
703 /*
704 * General parser error formatter - preparation. This must be called to
705 * initialize the context before the message can be displayed with
706 * vocerr_va().
707 */
708 static void vocerr_va_prep(voccxdef *ctx, struct vocerr_va_info *info,
709 int err, char *f, va_list argptr)
710 {
711 /*
712 * presume that we'll use the given format string, instead of one
713 * provided by the program
714 */
715 info->fmt = f;
716
717 /* use the output buffer from the info structure */
718 info->outp = info->user_msg;
719 info->outsiz = sizeof(info->user_msg);
720
721 /*
722 * if the user has a parseError or parseErrorParam function, see if it
723 * provides a msg
724 */
725 if (ctx->voccxper != MCMONINV || ctx->voccxperp != MCMONINV)
726 {
727 runcxdef *rcx = ctx->voccxrun;
728 dattyp typ;
729 size_t len;
730 int argc;
731
732 /* start off with the two arguments that are always present */
733 argc = 2;
734
735 /*
736 * if we're calling parseErrorParam, and we have additional
737 * arguments, push them as well
738 */
739 if (ctx->voccxperp != MCMONINV)
740 {
741 enum typ_t
742 {
743 ARGBUF_STR, ARGBUF_INT, ARGBUF_CHAR
744 };
745 struct argbuf_t
746 {
747 enum typ_t typ;
748 union
749 {
750 char *strval;
751 int intval;
752 char charval;
753 } val;
754 };
755 struct argbuf_t args[5];
756 struct argbuf_t *argp;
757 char *p;
758
759 /*
760 * Retrieve the arguments by examining the format string. We
761 * must buffer up the arguments before pushing them, because
762 * we need to push them in reverse order (last to first); so,
763 * we must scan all arguments before we push the first one.
764 */
765 for (p = f, argp = args ; *p != '\0' ; ++p)
766 {
767 /* check if this is a parameter */
768 if (*p == '%')
769 {
770 /* find out what type it is */
771 switch(*++p)
772 {
773 case 's':
774 /* string - save the char pointer */
775 argp->val.strval = va_arg(argptr, char *);
776 argp->typ = ARGBUF_STR;
777
778 /* consume an argument slot */
779 ++argp;
780 break;
781
782 case 'd':
783 /* integer - save the integer */
784 argp->val.intval = va_arg(argptr, int);
785 argp->typ = ARGBUF_INT;
786
787 /* consume an argument slot */
788 ++argp;
789 break;
790
791 case 'c':
792 /* character */
793 argp->val.charval = (char)va_arg(argptr, int);
794 argp->typ = ARGBUF_CHAR;
795
796 /* consume an argument slot */
797 ++argp;
798 break;
799
800 default:
801 /*
802 * ignore other types (there shouldn't be any
803 * other types anyway)
804 */
805 break;
806 }
807 }
808 }
809
810 /*
811 * Push the arguments - keep looping until we get back to the
812 * first argument slot
813 */
814 while (argp != args)
815 {
816 /* move to the next argument, working backwards */
817 --argp;
818
819 /* push this argument */
820 switch(argp->typ)
821 {
822 case ARGBUF_STR:
823 /* push the string value */
824 runpstr(rcx, argp->val.strval,
825 (int)strlen(argp->val.strval), 0);
826 break;
827
828 case ARGBUF_INT:
829 /* push the number value */
830 runpnum(rcx, argp->val.intval);
831 break;
832
833 case ARGBUF_CHAR:
834 /* push the character as a one-character string */
835 runpstr(rcx, &argp->val.charval, 1, 0);
836 break;
837 }
838
839 /* count the argument */
840 ++argc;
841 }
842 }
843
844 /* push standard arguments: error code and default message */
845 runpstr(rcx, f, (int)strlen(f), 0); /* 2nd arg: default msg */
846 runpnum(rcx, (long)err); /* 1st arg: error number */
847
848 /* invoke parseErrorParam if it's defined, otherwise parseError */
849 runfn(rcx, (objnum)(ctx->voccxperp == MCMONINV
850 ? ctx->voccxper : ctx->voccxperp), argc);
851
852 /* see what the function returned */
853 typ = runtostyp(rcx);
854 if (typ == DAT_SSTRING)
855 {
856 char *p;
857
858 /*
859 * they returned a string - use it as the error message
860 * instead of the default message
861 */
862 p = (char *)runpopstr(rcx);
863 len = osrp2(p) - 2;
864 p += 2;
865 if (len > sizeof(info->user_msg) - 1)
866 len = sizeof(info->user_msg) - 1;
867 memcpy(info->user_msg, p, len);
868 info->user_msg[len] = '\0';
869
870 /* use the returned string as the message to display */
871 info->fmt = info->user_msg;
872
873 /* use the remainder of the buffer for the final formatting */
874 info->outp = info->user_msg + len + 1;
875 info->outsiz = sizeof(info->user_msg) - len - 1;
876 }
877 else
878 {
879 /* ignore other return values */
880 rundisc(rcx);
881 }
882 }
883
884 }
885
886 /*
887 * General parser error formatter.
888 *
889 * Before calling this routine, callers MUST invoke vocerr_va_prep() to
890 * prepare the information structure. Because both this routine and the
891 * prep routine need to look at the varargs list ('argptr'), the caller
892 * must call va_start/va_end around the prep call, and then AGAIN on this
893 * call. va_start/va_end must be used twice to ensure that the argptr is
894 * property re-initialized for the call to this routine.
895 */
896 static void vocerr_va(voccxdef *ctx, struct vocerr_va_info *info,
897 int err, char *f, va_list argptr)
898 {
899 char *buf;
900
901 /* turn on output */
902 (void)tioshow(ctx->voccxtio);
903
904 /* build the string to display */
905 if (os_vasprintf(&buf, info->fmt, argptr) >= 0)
906 {
907 /* display it */
908 tioputs(ctx->voccxtio, buf);
909
910 /* free the buffer */
911 osfree(buf);
912 }
913 }
914
915 /* ------------------------------------------------------------------------ */
916 /*
917 * display a parser informational message
918 */
919 void vocerr_info(voccxdef *ctx, int err, char *f, ...)
920 {
921 va_list argptr;
922 struct vocerr_va_info info;
923
924 /* prepare to format the message */
925 va_start(argptr, f);
926 vocerr_va_prep(ctx, &info, err, f, argptr);
927 va_end(argptr);
928
929 /* call the general vocerr formatter */
930 va_start(argptr, f);
931 vocerr_va(ctx, &info, err, f, argptr);
932 va_end(argptr);
933 }
934
935 /*
936 * display a parser error
937 */
938 void vocerr(voccxdef *ctx, int err, char *f, ...)
939 {
940 va_list argptr;
941 struct vocerr_va_info info;
942
943 /*
944 * If the unknown word flag is set, suppress this error, because
945 * we're going to be trying the whole parsing from the beginning
946 * again anyway.
947 */
948 if (ctx->voccxunknown > 0)
949 return;
950
951 /* prepare to format the message */
952 va_start(argptr, f);
953 vocerr_va_prep(ctx, &info, err, f, argptr);
954 va_end(argptr);
955
956 /* call the general vocerr formatter */
957 va_start(argptr, f);
958 vocerr_va(ctx, &info, err, f, argptr);
959 va_end(argptr);
960 }
961
962 /*
963 * Handle an unknown verb or sentence structure. We'll call this when
964 * we encounter a sentence where we don't know the verb word, or we
965 * don't know the combination of verb and verb preposition, or we don't
966 * recognize the sentence structure (for example, an indirect object is
967 * present, but we don't have a template defined using an indirect
968 * object for the verb).
969 *
970 * This function calls the game-defined function parseUnknownVerb, if it
971 * exists. If the function doesn't exist, we'll simply display the
972 * given error message, using the normal parseError mechanism. The
973 * function should use "abort" or "exit" if it wants to cancel further
974 * processing of the command.
975 *
976 * We'll return true if the function exists, in which case normal
977 * processing should continue with any remaining command on the command
978 * line. We'll return false if the function doesn't exist, in which
979 * case the remainder of the command should be aborted.
980 *
981 * 'wrdcnt' is the number of words in the cmd[] array. If wrdcnt is
982 * zero, we'll automatically count the array entries, with the end of
983 * the array indicated by a null pointer entry.
984 *
985 * 'next_start' is a variable that we may fill in with the index of the
986 * next word in the command to be parsed. If the user function
987 * indicates the number of words it consumes, we'll use 'next_start' to
988 * communicate this back to the caller, so that the caller can resume
989 * parsing after the part parsed by the function.
990 */
991 int try_unknown_verb(voccxdef *ctx, objnum actor,
992 char **cmd, int *typelist, int wrdcnt, int *next_start,
993 int do_fuses, int vocerr_err, char *vocerr_msg, ...)
994 {
995 int show_msg;
996 va_list argptr;
997
998 /* presume we won't show the message */
999 show_msg = FALSE;
1000
1001 /* determine the word count if the caller passed in zero */
1002 if (wrdcnt == 0)
1003 {
1004 /* count the words before the terminating null entry */
1005 for ( ; cmd[wrdcnt] != 0 ; ++wrdcnt) ;
1006 }
1007
1008 /* if parseUnknownVerb exists, call it */
1009 if (ctx->voccxpuv != MCMONINV)
1010 {
1011 int err;
1012 int i;
1013 int do_fuses;
1014
1015 /* no error has occurred yet */
1016 err = 0;
1017
1018 /* presume we will run the fuses */
1019 do_fuses = TRUE;
1020
1021 /* push the error number argument */
1022 runpnum(ctx->voccxrun, (long)vocerr_err);
1023
1024 /* push a list of the word types */
1025 voc_push_numlist(ctx, (uint *)typelist, wrdcnt);
1026
1027 /* push a list of the words */
1028 voc_push_toklist(ctx, cmd, wrdcnt);
1029
1030 /* use "Me" as the default actor */
1031 if (actor == MCMONINV)
1032 actor = ctx->voccxme;
1033
1034 /* push the actor argument */
1035 runpobj(ctx->voccxrun, actor);
1036
1037 /* catch any errors that occur while calling the function */
1038 ERRBEGIN(ctx->voccxerr)
1039 {
1040 /* invoke the function */
1041 runfn(ctx->voccxrun, ctx->voccxpuv, 4);
1042
1043 /* get the return value */
1044 switch(runtostyp(ctx->voccxrun))
1045 {
1046 case DAT_TRUE:
1047 /* the command was handled */
1048 rundisc(ctx->voccxrun);
1049
1050 /* consume the entire command */
1051 *next_start = wrdcnt;
1052
1053 /*
1054 * since the command has now been handled, forget about
1055 * any unknown words
1056 */
1057 ctx->voccxunknown = 0;
1058 break;
1059
1060 case DAT_NUMBER:
1061 /*
1062 * The command was handled, and the function indicated
1063 * the number of words it wants to skip. Communicate
1064 * this information back to the caller in *next_start.
1065 * Since the routine returns the 1-based index of the
1066 * next entry, we must subtract one to get the number of
1067 * words actually consumed.
1068 */
1069 *next_start = runpopnum(ctx->voccxrun);
1070 if (*next_start > 0)
1071 --(*next_start);
1072
1073 /* make sure the value is in range */
1074 if (*next_start < 0)
1075 *next_start = 0;
1076 else if (*next_start > wrdcnt)
1077 *next_start = wrdcnt;
1078
1079 /*
1080 * forget about any unknown words in the list up to the
1081 * next word
1082 */
1083 for (i = 0 ; i < *next_start ; ++i)
1084 {
1085 /* if this word was unknown, forget about that now */
1086 if ((typelist[i] & VOCT_UNKNOWN) != 0
1087 && ctx->voccxunknown > 0)
1088 --(ctx->voccxunknown);
1089 }
1090 break;
1091
1092 default:
1093 /* treat anything else like nil */
1094
1095 case DAT_NIL:
1096 /* nil - command not handled; show the message */
1097 rundisc(ctx->voccxrun);
1098 show_msg = TRUE;
1099 break;
1100 }
1101 }
1102 ERRCATCH(ctx->voccxerr, err)
1103 {
1104 /* check the error */
1105 switch(err)
1106 {
1107 case ERR_RUNEXIT:
1108 case ERR_RUNEXITOBJ:
1109 /*
1110 * Exit or exitobj was executed - skip to the fuses.
1111 * Forget about any unknown words, since we've finished
1112 * processing this command and we don't want to allow
1113 * "oops" processing.
1114 */
1115 ctx->voccxunknown = 0;
1116 break;
1117
1118 case ERR_RUNABRT:
1119 /*
1120 * abort was executed - skip to the end of the command,
1121 * but do not execute the fuses
1122 */
1123 do_fuses = FALSE;
1124
1125 /*
1126 * Since we're aborting the command, ignore any
1127 * remaining unknown words - we're skipping out of the
1128 * command entirely, so we don't care that there were
1129 * unknown words in the command.
1130 */
1131 ctx->voccxunknown = 0;
1132 break;
1133
1134 default:
1135 /* re-throw any other errors */
1136 errrse(ctx->voccxerr);
1137 }
1138 }
1139 ERREND(ctx->voccxerr);
1140
1141 /* if we're not showing the message, process fuses and daemons */
1142 if (!show_msg)
1143 {
1144 /* execute fuses and daemons */
1145 if (exe_fuses_and_daemons(ctx, err, do_fuses,
1146 actor, MCMONINV, 0, 0,
1147 MCMONINV, MCMONINV) != 0)
1148 {
1149 /*
1150 * aborted from fuses and daemons - return false to tell
1151 * the caller not to execute anything left on the
1152 * command line
1153 */
1154 return FALSE;
1155 }
1156
1157 /* indicate that the game code successfully handled the command */
1158 return TRUE;
1159 }
1160 }
1161
1162 /*
1163 * If we made it here, it means we're showing the default message.
1164 * If we have unknown words, suppress the message so that we show
1165 * the unknown word error instead after returning.
1166 */
1167 if (ctx->voccxunknown == 0)
1168 {
1169 struct vocerr_va_info info;
1170
1171 /* prepare to format the message */
1172 va_start(argptr, vocerr_msg);
1173 vocerr_va_prep(ctx, &info, vocerr_err, vocerr_msg, argptr);
1174 va_end(argptr);
1175
1176 /* format the mesage */
1177 va_start(argptr, vocerr_msg);
1178 vocerr_va(ctx, &info, vocerr_err, vocerr_msg, argptr);
1179 va_end(argptr);
1180 }
1181
1182 /* indicate that the remainder of the command should be aborted */
1183 return FALSE;
1184 }
1185
1186
1187 /* determine if a tokenized word is a special internal word flag */
1188 /* int vocisspec(char *wrd); */
1189 #define vocisspec(wrd) \
1190 (vocisupper(*wrd) || (!vocisalpha(*wrd) && *wrd != '\'' && *wrd != '-'))
1191
1192 static vocspdef vocsptab[] =
1193 {
1194 { "of", VOCW_OF },
1195 { "and", VOCW_AND },
1196 { "then", VOCW_THEN },
1197 { "all", VOCW_ALL },
1198 { "everyt", VOCW_ALL },
1199 { "both", VOCW_BOTH },
1200 { "but", VOCW_BUT },
1201 { "except", VOCW_BUT },
1202 { "one", VOCW_ONE },
1203 { "ones", VOCW_ONES },
1204 { "it", VOCW_IT },
1205 { "them", VOCW_THEM },
1206 { "him", VOCW_HIM },
1207 { "her", VOCW_HER },
1208 { "any", VOCW_ANY },
1209 { "either", VOCW_ANY },
1210 { 0, 0 }
1211 };
1212
1213 /* test a word to see if it's a particular special word */
1214 static int voc_check_special(voccxdef *ctx, char *wrd, int checktyp)
1215 {
1216 /* search the user or built-in special table, as appropriate */
1217 if (ctx->voccxspp)
1218 {
1219 char *p;
1220 char *endp;
1221 char typ;
1222 int len;
1223 int wrdlen = strlen((char *)wrd);
1224
1225 for (p = ctx->voccxspp, endp = p + ctx->voccxspl ;
1226 p < endp ; )
1227 {
1228 typ = *p++;
1229 len = *p++;
1230
1231 /* if this word matches in type and text, we have a match */
1232 if (typ == checktyp
1233 && len == wrdlen && !memcmp(p, wrd, (size_t)len))
1234 return TRUE;
1235
1236 /* no match - keep going */
1237 p += len;
1238 }
1239 }
1240 else
1241 {
1242 vocspdef *x;
1243
1244 for (x = vocsptab ; x->vocspin ; ++x)
1245 {
1246 /* if it matches in type and text, we have a match */
1247 if (x->vocspout == checktyp
1248 && !strncmp((char *)wrd, x->vocspin, (size_t)6))
1249 return TRUE;
1250 }
1251 }
1252
1253 /* didn't find a match for the text and type */
1254 return FALSE;
1255 }
1256
1257
1258 /* tokenize a command line - returns number of words in command */
1259 int voctok(voccxdef *ctx, char *cmd, char *outbuf, char **wrd,
1260 int lower, int cvt_ones, int show_errors)
1261 {
1262 int i;
1263 vocspdef *x;
1264 int l;
1265 char *p;
1266 char *w;
1267 uint len;
1268
1269 for (i = 0 ;; )
1270 {
1271 while (vocisspace(*cmd)) cmd++;
1272 if (!*cmd)
1273 {
1274 wrd[i] = outbuf;
1275 *outbuf = '\0';
1276 return(i);
1277 }
1278
1279 wrd[i++] = outbuf;
1280 if (vocisalpha(*cmd) || *cmd == '-')
1281 {
1282 while(vocisalpha(*cmd) || vocisdigit(*cmd) ||
1283 *cmd=='\'' || *cmd=='-')
1284 {
1285 *outbuf++ = (vocisupper(*cmd) && lower) ? tolower(*cmd) : *cmd;
1286 ++cmd;
1287 }
1288
1289 /*
1290 * Check for a special case: abbreviations that end in a
1291 * period. For example, "Mr. Patrick J. Wayne." We wish
1292 * to absorb the period after "Mr" and the one after "J"
1293 * into the respective words; we detect this condition by
1294 * actually trying to find a word in the dictionary that
1295 * has the period.
1296 */
1297 w = wrd[i-1];
1298 len = outbuf - w;
1299 if (*cmd == '.')
1300 {
1301 *outbuf++ = *cmd++; /* add the period to the word */
1302 *outbuf = '\0'; /* null-terminate it */
1303 ++len;
1304 if (!vocffw(ctx, (char *)w, len, 0, 0, PRP_NOUN,
1305 (vocseadef *)0)
1306 && !vocffw(ctx, (char *)w, len, 0, 0, PRP_ADJ,
1307 (vocseadef *)0))
1308 {
1309 /* no word with period in dictionary - remove period */
1310 --outbuf;
1311 --cmd;
1312 --len;
1313 }
1314 }
1315
1316 /* null-terminate the buffer */
1317 *outbuf = '\0';
1318
1319 /* find compound words and glue them together */
1320 for (p = ctx->voccxcpp, l = ctx->voccxcpl ; l ; )
1321 {
1322 uint l1 = osrp2(p);
1323 char *p2 = p + l1; /* get second word */
1324 uint l2 = osrp2(p2);
1325 char *p3 = p2 + l2; /* get compound word */
1326 uint l3 = osrp2(p3);
1327
1328 if (i > 1 && len == (l2 - 2)
1329 && !memcmp(w, p2 + 2, (size_t)len)
1330 && strlen((char *)wrd[i-2]) == (l1 - 2)
1331 && !memcmp(wrd[i-2], p + 2, (size_t)(l1 - 2)))
1332 {
1333 memcpy(wrd[i-2], p3 + 2, (size_t)(l3 - 2));
1334 *(wrd[i-2] + l3 - 2) = '\0';
1335 --i;
1336 break;
1337 }
1338
1339 /* move on to the next word */
1340 l -= l1 + l2 + l3;
1341 p = p3 + l3;
1342 }
1343
1344 /*
1345 * Find any special keywords, and set to appropriate flag
1346 * char. Note that we no longer convert "of" in this
1347 * fashion; "of" is now handled separately in order to
1348 * facilitate its use as an ordinary preposition.
1349 */
1350 if (ctx->voccxspp)
1351 {
1352 char *p;
1353 char *endp;
1354 char typ;
1355 int len;
1356 int wrdlen = strlen((char *)wrd[i-1]);
1357
1358 for (p = ctx->voccxspp, endp = p + ctx->voccxspl ;
1359 p < endp ; )
1360 {
1361 typ = *p++;
1362 len = *p++;
1363 if (len == wrdlen && !memcmp(p, wrd[i-1], (size_t)len)
1364 && (cvt_ones || (typ != VOCW_ONE && typ != VOCW_ONES))
1365 && typ != VOCW_OF)
1366 {
1367 *wrd[i-1] = typ;
1368 *(wrd[i-1] + 1) = '\0';
1369 break;
1370 }
1371 p += len;
1372 }
1373 }
1374 else
1375 {
1376 for (x = vocsptab ; x->vocspin ; ++x)
1377 {
1378 if (!strncmp((char *)wrd[i-1], (char *)x->vocspin,
1379 (size_t)6)
1380 && (cvt_ones ||
1381 (x->vocspout != VOCW_ONE
1382 && x->vocspout != VOCW_ONES))
1383 && x->vocspout != VOCW_OF)
1384 {
1385 *wrd[i-1] = x->vocspout;
1386 *(wrd[i-1] + 1) = '\0';
1387 break;
1388 }
1389 }
1390 }
1391
1392 /* make sure the output pointer is fixed up to the right spot */
1393 outbuf = wrd[i-1];
1394 outbuf += strlen((char *)outbuf);
1395 }
1396 else if (vocisdigit( *cmd ))
1397 {
1398 while(vocisdigit(*cmd) || vocisalpha(*cmd)
1399 || *cmd == '\'' || *cmd == '-')
1400 *outbuf++ = *cmd++;
1401 }
1402 else switch( *cmd )
1403 {
1404 case '.':
1405 case '!':
1406 case '?':
1407 case ';':
1408 *outbuf++ = VOCW_THEN;
1409 ++cmd;
1410 break;
1411
1412 case ',':
1413 case ':':
1414 *outbuf++ = VOCW_AND;
1415 ++cmd;
1416 break;
1417
1418 case '"':
1419 case '\'':
1420 {
1421 char *lenptr;
1422 char quote = *cmd++;
1423
1424 /*
1425 * remember that this is a quoted string (it doesn't
1426 * matter whether they're actually using single or
1427 * double quotes - in either case, we use '"' as the
1428 * flag to indicate that it's a quote string)
1429 */
1430 *outbuf++ = '"';
1431
1432 /* make room for the length prefix */
1433 lenptr = outbuf;
1434 outbuf += 2;
1435
1436 /* copy up to the matching close quote */
1437 while (*cmd && *cmd != quote)
1438 {
1439 char c;
1440
1441 /* get this character */
1442 c = *cmd++;
1443
1444 /* escape the character if necessary */
1445 switch(c)
1446 {
1447 case '\\':
1448 *outbuf++ = '\\';
1449 break;
1450 }
1451
1452 /* copy this character */
1453 *outbuf++ = c;
1454 }
1455
1456 oswp2(lenptr, ((int)(outbuf - lenptr)));
1457 if (*cmd == quote) cmd++;
1458 break;
1459 }
1460
1461 default:
1462 /* display an error if appropriate */
1463 if (show_errors)
1464 {
1465 int hmode = tio_is_html_mode();
1466
1467 /*
1468 * if we're in HTML mode, switch out momentarily, so that
1469 * we show the character literally, even if it's a
1470 * markup-significant character (such as '<' or '&')
1471 */
1472 if (hmode)
1473 tioputs(ctx->voccxtio, "\\H-");
1474
1475 /* show the message */
1476 vocerr(ctx, VOCERR(1),
1477 "I don't understand the punctuation \"%c\".", *cmd);
1478
1479 /* restore HTML mode if appropriate */
1480 if (hmode)
1481 tioputs(ctx->voccxtio, "\\H+");
1482 }
1483
1484 /* return failure */
1485 return -1;
1486 }
1487
1488 /* null-terminate the result */
1489 *outbuf++ = '\0';
1490 }
1491 }
1492
1493
1494 /* ------------------------------------------------------------------------ */
1495 /*
1496 * Look up a word's type. If 'of_is_spec' is true, we'll treat OF as
1497 * being of type special if it's not otherwise defined.
1498 */
1499 static int voc_lookup_type(voccxdef *ctx, char *p, int len, int of_is_spec)
1500 {
1501 int t;
1502
1503 /* check for a special word */
1504 if (vocisspec(p))
1505 {
1506 /* it's a special word - this is its type */
1507 t = VOCT_SPEC;
1508 }
1509 else
1510 {
1511 vocwdef *vw;
1512 vocdef *v;
1513
1514 /*
1515 * Now check the various entries of this word to get the word
1516 * type flag bits. The Noun and Adjective flags can be set for
1517 * any word which matches this word in the first six letters (or
1518 * more if more were provided by the player), but the Plural
1519 * flag can only be set if the plural word matches exactly.
1520 * Note that this pass only matches the first word in two-word
1521 * verbs; the second word is considered later during the
1522 * semantic analysis.
1523 */
1524 for (t = 0, v = ctx->voccxhsh[vochsh((uchar *)p, len)] ; v != 0 ;
1525 v = v->vocnxt)
1526 {
1527 /* if this hash chain entry matches, add it to our types */
1528 if (voceq((uchar *)p, len, v->voctxt, v->voclen))
1529 {
1530 /* we have a match - look through relation list for word */
1531 for (vw = vocwget(ctx, v->vocwlst) ; vw != 0 ;
1532 vw = vocwget(ctx, vw->vocwnxt))
1533 {
1534 /* skip this word if it's been deleted */
1535 if (vw->vocwflg & VOCFDEL)
1536 continue;
1537
1538 /* we need a special check for plurals */
1539 if (vw->vocwtyp == PRP_PLURAL)
1540 {
1541 /* plurals must be exact (non-truncated) match */
1542 if (len == v->voclen)
1543 {
1544 /* plurals also count as nouns */
1545 t |= (VOCT_NOUN | VOCT_PLURAL);
1546 }
1547 }
1548 else
1549 {
1550 /* add this type bit to our type value */
1551 t |= voctype[vw->vocwtyp];
1552 }
1553 }
1554 }
1555 }
1556 }
1557
1558 /* check for "of" if the caller wants us to */
1559 if (of_is_spec && t == 0 && voc_check_special(ctx, p, VOCW_OF))
1560 t = VOCT_SPEC;
1561
1562 /* return the type */
1563 return t;
1564 }
1565
1566
1567 /* ------------------------------------------------------------------------ */
1568 /*
1569 * Display an unknown word error, and read a new command, allowing the
1570 * user to respond with the special OOPS command to correct the unknown
1571 * word. Returns a pointer to the start of the replacement text if the
1572 * player entered a correction via OOPS, or a null pointer if the player
1573 * simply entered a new command.
1574 */
1575 static char *voc_read_oops(voccxdef *ctx, char *oopsbuf, size_t oopsbuflen,
1576 const char *unknown_word)
1577 {
1578 char *p;
1579
1580 /* display the error */
1581 vocerr(ctx, VOCERR(2), "I don't know the word \"%s\".", unknown_word);
1582
1583 /* read a new command */
1584 if (vocread(ctx, MCMONINV, MCMONINV,
1585 oopsbuf, (int)oopsbuflen, 1) == VOCREAD_REDO)
1586 {
1587 /*
1588 * we've already decided it's not an OOPS input - return null to
1589 * indicate to the caller that we have a new command
1590 */
1591 return 0;
1592 }
1593
1594 /* lower-case the string */
1595 for (p = oopsbuf ; *p != '\0' ; ++p)
1596 *p = (vocisupper(*p) ? tolower(*p) : *p);
1597
1598 /* skip leading spaces */
1599 for (p = oopsbuf ; vocisspace(*p) ; ++p) ;
1600
1601 /*
1602 * See if they are saying "oops". Allow "oops" or simply "o",
1603 * followed by either a space or a comma.
1604 */
1605 if ((strlen(p) > 5 && memcmp(p, "oops ", 5) == 0)
1606 || (strlen(p) > 5 && memcmp(p, "oops,", 5) == 0))
1607 {
1608 /* we found "OOPS" - move to the next character */
1609 p += 5;
1610 }
1611 else if ((strlen(p) > 2 && memcmp(p, "o ", 2) == 0)
1612 || (strlen(p) > 2 && memcmp(p, "o,", 2) == 0))
1613 {
1614 /* we found "O" - move to the next character */
1615 p += 2;
1616 }
1617 else
1618 {
1619 /*
1620 * we didn't find any form of "OOPS" response - return null to
1621 * indicate to the caller that the player entered a new command
1622 */
1623 return 0;
1624 }
1625
1626 /* skip spaces before the replacement text */
1627 for ( ; vocisspace(*p) ; ++p) ;
1628
1629 /* return a pointer to the start of the replacement text */
1630 return p;
1631 }
1632
1633 /* ------------------------------------------------------------------------ */
1634 /*
1635 * figure out what parts of speech are associated with each
1636 * word in a tokenized command list
1637 */
1638 int vocgtyp(voccxdef *ctx, char *cmd[], int types[], char *orgbuf)
1639 {
1640 int cur;
1641 int t;
1642 char *p;
1643 int len;
1644 int unknown_count = 0;
1645
1646 startover:
1647 if (ctx->voccxflg & VOCCXFDBG)
1648 tioputs(ctx->vocxtio, ". Checking words:\\n");
1649
1650 for (cur = 0 ; cmd[cur] ; ++cur)
1651 {
1652 /* get the word */
1653 p = cmd[cur];
1654 len = strlen(p);
1655
1656 /* look it up */
1657 t = voc_lookup_type(ctx, p, len, FALSE);
1658
1659 /* see if the word was found */
1660 if (t == 0 && !voc_check_special(ctx, p, VOCW_OF))
1661 {
1662 /*
1663 * We didn't find the word. For now, set its type to
1664 * "unknown".
1665 */
1666 t = VOCT_UNKNOWN;
1667
1668 /*
1669 * If the unknown word count is already non-zero, it means
1670 * that we've tried to let the game resolve this word using
1671 * the parseUnknownDobj/Iobj mechanism, but it wasn't able
1672 * to do so, thus we've come back here to use the normal
1673 * "oops" processing instead.
1674 *
1675 * Don't generate a message until we get to the first
1676 * unknown word from the original list that we weren't able
1677 * to resolve. We may have been able to handle one or more
1678 * of the original list of unknown words (through
1679 * parseNounPhrase or other means), so we don't want to
1680 * generate a message for any words we ended up handling.
1681 * The number we resolved is the last full unknown count
1682 * minus the remaining unknown count.
1683 */
1684 if (ctx->voccxunknown != 0
1685 && unknown_count >= ctx->voccxlastunk - ctx->voccxunknown)
1686 {
1687 char oopsbuf[VOCBUFSIZ];
1688 char *p1;
1689
1690 /*
1691 * we can try using the parseUnknownDobj/Iobj again
1692 * after this, so clear the unknown word count for now
1693 */
1694 ctx->voccxunknown = 0;
1695
1696 /* display an error, and ask for a new command */
1697 p1 = voc_read_oops(ctx, oopsbuf, sizeof(oopsbuf), p);
1698
1699 /* if they responded with replacement text, apply it */
1700 if (p1 != 0)
1701 {
1702 char redobuf[200];
1703 char *q;
1704 int i;
1705 int wc;
1706 char **w;
1707 char *outp;
1708
1709 /*
1710 * copy words from the original string, replacing
1711 * the unknown word with what follows the "oops" in
1712 * the new command
1713 */
1714 for (outp = redobuf, i = 0, w = cmd ; *w != 0 ; ++i, ++w)
1715 {
1716
1717 /* see what we have */
1718 if (i == cur)
1719 {
1720 /*
1721 * We've reached the word to be replaced.
1722 * Ignore the original token, and replace it
1723 * with the word or words from the OOPS
1724 * command
1725 */
1726 for (q = p1, len = 0 ;
1727 *q != '\0' && *q != '.' && *q != ','
1728 && *q != '?' && *q != '!' ; ++q, ++len) ;
1729 memcpy(outp, p1, (size_t)len);
1730 outp += len;
1731 }
1732 else if (**w == '"')
1733 {
1734 char *strp;
1735 char *p2;
1736 char qu;
1737 int rem;
1738
1739 /*
1740 * It's a string - add a quote mark, then
1741 * copy the string as indicated by the
1742 * length prefix, then add another quote
1743 * mark. Get the length by reading the
1744 * length prefix following the quote mark,
1745 * and get a pointer to the text of the
1746 * string, which immediately follows the
1747 * length prefix.
1748 */
1749 len = osrp2(*w + 1) - 2;
1750 strp = *w + 3;
1751
1752 /*
1753 * We need to figure out what kind of quote
1754 * mark to use. If the string contains any
1755 * embedded double quotes, use single quotes
1756 * to delimit the string; otherwise, use
1757 * double quotes. Presume we'll use double
1758 * quotes as the delimiter, then scan the
1759 * string for embedded double quotes.
1760 */
1761 for (qu = '"', p2 = strp, rem = len ; rem != 0 ;
1762 --rem, ++p2)
1763 {
1764 /*
1765 * if this is an embedded double quote,
1766 * use single quotes to delimite the
1767 * string
1768 */
1769 if (*p2 == '"')
1770 {
1771 /* use single quotes as delimiters */
1772 qu = '\'';
1773
1774 /* no need to look any further */
1775 break;
1776 }
1777 }
1778
1779 /* add the open quote */
1780 *outp++ = qu;
1781
1782 /* copy the string */
1783 memcpy(outp, strp, len);
1784 outp += len;
1785
1786 /* add the close quote */
1787 *outp++ = qu;
1788 }
1789 else
1790 {
1791 /*
1792 * it's an ordinary token - copy the
1793 * null-terminated string for the token from
1794 * the original command list
1795 */
1796 len = strlen(*w);
1797 memcpy(outp, *w, (size_t)len);
1798 outp += len;
1799 }
1800
1801 /* add a space between words */
1802 *outp++ = ' ';
1803 }
1804
1805 /* terminate the new string */
1806 *outp = '\0';
1807
1808 /* try tokenizing the string */
1809 *(cmd[0]) = '\0';
1810 if ((wc = voctok(ctx, redobuf, cmd[0],
1811 cmd, FALSE, FALSE, TRUE)) <= 0)
1812 return 1;
1813 cmd[wc] = 0;
1814
1815 /* start over with the typing */
1816 goto startover;
1817 }
1818 else
1819 {
1820 /*
1821 * They didn't start the command with "oops", so
1822 * this must be a brand new command. Replace the
1823 * original command with the new command.
1824 */
1825 strcpy(orgbuf, oopsbuf);
1826
1827 /*
1828 * forget we had an unknown word so that we're sure
1829 * to start over with a new command
1830 */
1831 ctx->voccxunknown = 0;
1832
1833 /*
1834 * set the "redo" flag to start over with the new
1835 * command
1836 */
1837 ctx->voccxredo = 1;
1838
1839 /*
1840 * return an error to indicate the current command
1841 * has been aborted
1842 */
1843 return 1;
1844 }
1845 }
1846 else
1847 {
1848 /*
1849 * We've now encountered an unknown word, and we're
1850 * going to defer resolution. Remember this; we'll
1851 * count the unknown word in the context when we return
1852 * (do so only locally for now, since we may encounter
1853 * more unknown words before we return, in which case we
1854 * want to know that this is still the first pass).
1855 */
1856 ++unknown_count;
1857 }
1858 }
1859
1860 /* display if in debug mode */
1861 if (ctx->voccxflg & VOCCXFDBG)
1862 {
1863 char buf[128];
1864 size_t i;
1865 char *p;
1866 int cnt;
1867
1868 (void)tioshow(ctx->voccxtio);
1869 sprintf(buf, "... %s (", cmd[cur]);
1870 p = buf + strlen(buf);
1871 cnt = 0;
1872 for (i = 0 ; i < sizeof(type_names)/sizeof(type_names[0]) ; ++i)
1873 {
1874 if (t & (1 << i))
1875 {
1876 if (cnt) *p++ = ',';
1877 strcpy(p, type_names[i]);
1878 p += strlen(p);
1879 ++cnt;
1880 }
1881 }
1882 *p++ = ')';
1883 *p++ = '\\';
1884 *p++ = 'n';
1885 *p = '\0';
1886 tioputs(ctx->voccxtio, buf);
1887 }
1888
1889 types[cur] = t; /* record type of this word */
1890 }
1891
1892 /* if we found any unknown words, note this in our context */
1893 ctx->voccxunknown = unknown_count;
1894 ctx->voccxlastunk = unknown_count;
1895
1896 /* successful acquisition of types */
1897 return 0;
1898 }
1899
1900 /*
1901 * intersect - takes two lists and puts the intersection of them into
1902 * the first list.
1903 */
1904 static int vocisect(objnum *list1, objnum *list2)
1905 {
1906 int i, j, k;
1907
1908 for (i = k = 0 ; list1[i] != MCMONINV ; ++i)
1909 {
1910 for (j = 0 ; list2[j] != MCMONINV ; ++j)
1911 {
1912 if (list1[i] == list2[j])
1913 {
1914 list1[k++] = list1[i];
1915 break;
1916 }
1917 }
1918 }
1919 list1[k] = MCMONINV;
1920 return(k);
1921 }
1922
1923 /*
1924 * Intersect lists, including parallel flags lists. The flags from the
1925 * two lists for any matching object are OR'd together.
1926 */
1927 static int vocisect_flags(objnum *list1, uint *flags1,
1928 objnum *list2, uint *flags2)
1929 {
1930 int i, j, k;
1931
1932 for (i = k = 0 ; list1[i] != MCMONINV ; ++i)
1933 {
1934 for (j = 0 ; list2[j] != MCMONINV ; ++j)
1935 {
1936 if (list1[i] == list2[j])
1937 {
1938 list1[k] = list1[i];
1939 flags1[k] = flags1[i] | flags2[j];
1940 ++k;
1941 break;
1942 }
1943 }
1944 }
1945 list1[k] = MCMONINV;
1946 return(k);
1947 }
1948
1949 /*
1950 * get obj list: build a list of the objects that are associated with a
1951 * given word of player input.
1952 */
1953 static int vocgol(voccxdef *ctx, objnum *list, uint *flags, char **wrdlst,
1954 int *typlst, int first, int cur, int last, int ofword)
1955 {
1956 char *wrd;
1957 int typ;
1958 vocwdef *v;
1959 int cnt;
1960 int len;
1961 vocseadef search_ctx;
1962 int try_plural;
1963 int try_noun_before_num;
1964 int try_endadj;
1965 int trying_endadj;
1966 int wrdtyp;
1967
1968 /* get the current word and its type */
1969 wrd = wrdlst[cur];
1970 typ = typlst[cur];
1971
1972 /* get the length of the word */
1973 len = strlen(wrd);
1974
1975 /*
1976 * Get word type: figure out the correct part of speech, given by
1977 * context, for a given word. If it could count as only a
1978 * noun/plural or only an adjective, we use that. If it could count
1979 * as either a noun/plural or an adjective, we will treat it as a
1980 * noun/plural if it is the last word in the name or the last word
1981 * before "of", otherwise as an adjective.
1982 *
1983 * If the word is unknown, treat it as a noun or adjective - treat
1984 * it as part of the current noun phrase. One unknown word renders
1985 * the whole noun phrase unknown.
1986 */
1987 try_plural = (typ & VOCT_PLURAL);
1988
1989 /* presume we won't retry this word as an adjective */
1990 try_endadj = FALSE;
1991
1992 /* presume we won't retry this as a noun before a number */
1993 try_noun_before_num = FALSE;
1994
1995 /* we're not yet trying with adjective-at-end */
1996 trying_endadj = FALSE;
1997
1998 /* check to see what parts of speech are defined for this word */
1999 if ((typ & (VOCT_NOUN | VOCT_PLURAL)) && (typ & VOCT_ADJ))
2000 {
2001 /*
2002 * This can be either an adjective or a plural/noun. If this is
2003 * the last word in the noun phrase, treat it as a noun/plural if
2004 * possible. Otherwise, treat it as an adjective.
2005 */
2006 if (cur + 1 == last || cur == ofword - 1)
2007 {
2008 /*
2009 * This is the last word in the entire phrase, or the last word
2010 * before an 'of' (which makes it the last word of its
2011 * subphrase). Treat it as a noun if possible, otherwise as a
2012 * plural
2013 */
2014 wrdtyp = ((typ & VOCT_NOUN) ? PRP_NOUN : PRP_PLURAL);
2015
2016 /*
2017 * If this can be an adjective, too, make a note to come back
2018 * and try it again as an adjective. We prefer not to end a
2019 * noun phrase with an adjective, but we allow it, since it's
2020 * often convenient to abbreviate a noun phrase to just the
2021 * adjectives (as in TAKE RED, where there's only one object
2022 * nearby to which RED applies).
2023 */
2024 if ((typ & VOCT_ADJ) != 0)
2025 try_endadj = TRUE;
2026 }
2027 else if ((cur + 2 == last || cur == ofword - 2)
2028 && vocisdigit(wrdlst[cur+1][0]))
2029 {
2030 /*
2031 * This is the second-to-last word, and the last word is
2032 * numeric. In this case, try this word as BOTH a noun and an
2033 * adjective. Try it as an adjective first, but make a note to
2034 * go back and try it again as a noun.
2035 */
2036 wrdtyp = PRP_ADJ;
2037 try_noun_before_num = TRUE;
2038 }
2039 else
2040 {
2041 /*
2042 * This isn't the last word, so it can only be an adjective.
2043 * Look at it only as an adjective.
2044 */
2045 wrdtyp = PRP_ADJ;
2046 }
2047 }
2048 else if (typ & VOCT_NOUN)
2049 wrdtyp = PRP_NOUN;
2050 else if (typ & VOCT_UNKNOWN)
2051 wrdtyp = PRP_UNKNOWN;
2052 else
2053 {
2054 /* it's just an adjective */
2055 wrdtyp = PRP_ADJ;
2056
2057 /*
2058 * if this is the last word in the phrase, flag it as an ending
2059 * adjective
2060 */
2061 if (cur + 1 == last || cur == ofword - 1)
2062 trying_endadj = TRUE;
2063 }
2064
2065 /* display debugger information if appropriate */
2066 if (ctx->voccxflg & VOCCXFDBG)
2067 {
2068 char buf[128];
2069
2070 sprintf(buf, "... %s (treating as %s%s)\\n", wrd,
2071 (wrdtyp == PRP_ADJ ? "adjective" :
2072 wrdtyp == PRP_NOUN ? "noun" :
2073 wrdtyp == PRP_INVALID ? "unknown" : "plural"),
2074 (wrdtyp == PRP_NOUN && try_plural ? " + plural" : ""));
2075 tioputs(ctx->vocxtio, buf);
2076 }
2077
2078 /* if this is an unknown word, it doesn't have any objects */
2079 if (wrdtyp == PRP_UNKNOWN)
2080 {
2081 list[0] = MCMONINV;
2082 return 0;
2083 }
2084
2085 /* we have nothing in the list yet */
2086 cnt = 0;
2087
2088 add_words:
2089 for (v = vocffw(ctx, wrd, len, (char *)0, 0, wrdtyp, &search_ctx)
2090 ; v != 0 ; v = vocfnw(ctx, &search_ctx))
2091 {
2092 int i;
2093
2094 /* add the matching object to the output list */
2095 list[cnt] = v->vocwobj;
2096
2097 /* clear the flags */
2098 flags[cnt] = 0;
2099
2100 /* set the PLURAL flag if this is the plural vocabulary usage */
2101 if (wrdtyp == PRP_PLURAL)
2102 flags[cnt] |= VOCS_PLURAL;
2103
2104 /* set the ADJECTIVE AT END flag if appropriate */
2105 if (wrdtyp == PRP_ADJ && trying_endadj)
2106 flags[cnt] |= VOCS_ENDADJ;
2107
2108 /*
2109 * if this is not an exact match for the word, but is merely a
2110 * long-enough leading substring, flag it as truncated
2111 */
2112 if (len < search_ctx.v->voclen)
2113 flags[cnt] |= VOCS_TRUNC;
2114
2115 /* count the additional word in the list */
2116 ++cnt;
2117
2118 /*
2119 * if this object is already in the list with the same flags,
2120 * don't add it again
2121 */
2122 for (i = 0 ; i < cnt - 1 ; ++i)
2123 {
2124 /* check for an identical entry */
2125 if (list[i] == list[cnt-1] && flags[i] == flags[cnt-1])
2126 {
2127 /* take it back out of the list */
2128 --cnt;
2129
2130 /* no need to continue looking for the duplicate */
2131 break;
2132 }
2133 }
2134
2135 /* make sure we haven't overflowed the list */
2136 if (cnt >= VOCMAXAMBIG)
2137 {
2138 vocerr(ctx, VOCERR(3),
2139 "The word \"%s\" refers to too many objects.", wrd);
2140 list[0] = MCMONINV;
2141 return -1;
2142 }
2143 }
2144
2145 /*
2146 * if we want to go back and try the word again as a noun before a
2147 * number (as in "button 5"), do so now
2148 */
2149 if (try_noun_before_num && wrdtyp == PRP_ADJ)
2150 {
2151 /* change the word type to noun */
2152 wrdtyp = PRP_NOUN;
2153
2154 /* don't try this again */
2155 try_noun_before_num = FALSE;
2156
2157 /* add the words for the noun usage */
2158 goto add_words;
2159 }
2160
2161 /*
2162 * if we're interpreting the word as a noun, and the word can be a
2163 * plural, add in the plural interpretation as well
2164 */
2165 if (try_plural && wrdtyp != PRP_PLURAL)
2166 {
2167 /* change the word type to plural */
2168 wrdtyp = PRP_PLURAL;
2169
2170 /* don't try plurals again */
2171 try_plural = FALSE;
2172
2173 /* add the words for the plural usage */
2174 goto add_words;
2175 }
2176
2177 /*
2178 * if this was the last word in the phrase, and it could have been
2179 * an adjective, try it again as an adjective
2180 */
2181 if (try_endadj && wrdtyp != PRP_ADJ)
2182 {
2183 /* change the word type to adjective */
2184 wrdtyp = PRP_ADJ;
2185
2186 /* note that we're retrying as an adjective */
2187 trying_endadj = TRUE;
2188
2189 /* don't try this again */
2190 try_endadj = FALSE;
2191
2192 /* add the words for the adjective usage */
2193 goto add_words;
2194 }
2195
2196 /*
2197 * If we're interpreting the word as an adjective, and it's
2198 * numeric, include objects with "#" in their adjective list --
2199 * these objects allow arbitrary numbers as adjectives. Don't do
2200 * this if there's only the one word.
2201 */
2202 if (vocisdigit(wrd[0]) && wrdtyp == PRP_ADJ && first + 1 != last)
2203 {
2204 wrd = "#";
2205 len = 1;
2206 goto add_words;
2207 }
2208
2209 list[cnt] = MCMONINV;
2210 return cnt;
2211 }
2212
2213 /*
2214 * Add the user-defined word for "of" to a buffer. If no such word is
2215 * defined by the user (with the specialWords construct), add "of".
2216 */
2217 static void vocaddof(voccxdef *ctx, char *buf)
2218 {
2219 if (ctx->voccxspp)
2220 {
2221 size_t len = ctx->voccxspp[1];
2222 size_t oldlen = strlen(buf);
2223 memcpy(buf + oldlen, ctx->voccxspp + 2, len);
2224 buf[len + oldlen] = '\0';
2225 }
2226 else
2227 strcat(buf, "of");
2228 }
2229
2230 /* ------------------------------------------------------------------------ */
2231 /*
2232 * Call the parseNounPhrase user function, if defined, to attempt to
2233 * parse a noun phrase.
2234 *
2235 * Returns VOC_PNP_ERROR if the hook function indicates that an error
2236 * occurred; PNP_DEFAULT if the hook function told us to use the default
2237 * list; or PNP_SUCCESS to indicate that the hook function provided a
2238 * list to use.
2239 */
2240 static int voc_pnp_hook(voccxdef *ctx, char *cmd[], int typelist[],
2241 int cur, int *next, int complain,
2242 vocoldef *out_nounlist, int *out_nouncount,
2243 int chkact, int *no_match)
2244 {
2245 runcxdef *rcx = ctx->voccxrun;
2246 runsdef val;
2247 int wordcnt;
2248 char **cmdp;
2249 int outcnt;
2250 vocoldef *outp;
2251 int i;
2252
2253 /* if parseNounPhrase isn't defined, use the default handling */
2254 if (ctx->voccxpnp == MCMONINV)
2255 return VOC_PNP_DEFAULT;
2256
2257 /* push the actor-check flag */
2258 val.runstyp = (chkact ? DAT_TRUE : DAT_NIL);
2259 runpush(rcx, val.runstyp, &val);
2260
2261 /* push the complain flag */
2262 val.runstyp = (complain ? DAT_TRUE : DAT_NIL);
2263 runpush(rcx, val.runstyp, &val);
2264
2265 /* push the current index (adjusted to 1-based user convention) */
2266 runpnum(rcx, cur + 1);
2267
2268 /* count the entries in the command list */
2269 for (wordcnt = 0, cmdp = cmd ; *cmdp != 0 && **cmdp != '\0' ;
2270 ++wordcnt, ++cmdp) ;
2271
2272 /* push the type list */
2273 voc_push_numlist(ctx, (uint *)typelist, wordcnt);
2274
2275 /* push the command word list */
2276 voc_push_strlist_arr(ctx, cmd, wordcnt);
2277
2278 /* call the method */
2279 runfn(rcx, ctx->voccxpnp, 5);
2280
2281 /* check the return value */
2282 if (runtostyp(rcx) == DAT_NUMBER)
2283 {
2284 /* return the status code directly from the hook function */
2285 return (int)runpopnum(rcx);
2286 }
2287 else if (runtostyp(rcx) == DAT_LIST)
2288 {
2289 uchar *lstp;
2290 uint lstsiz;
2291
2292 /* pop the list */
2293 lstp = runpoplst(rcx);
2294
2295 /* read and skip the size prefix */
2296 lstsiz = osrp2(lstp);
2297 lstsiz -= 2;
2298 lstp += 2;
2299
2300 /* the first element should be the next index */
2301 if (lstsiz > 1 && *lstp == DAT_NUMBER)
2302 {
2303 /* set the 'next' pointer, adjusting to 0-based indexing */
2304 *next = osrp4(lstp+1) - 1;
2305
2306 /*
2307 * If 'next' is out of range, force it into range. We can't
2308 * go backwards (so 'next' must always be at least 'cur'),
2309 * and we can't go past the null element at the end of the
2310 * list.
2311 */
2312 if (*next < cur)
2313 *next = cur;
2314 else if (*next > wordcnt)
2315 *next = wordcnt;
2316
2317 /* skip the list entry */
2318 lstadv(&lstp, &lstsiz);
2319 }
2320 else
2321 {
2322 /* ignore the list and use the default parsing */
2323 return VOC_PNP_DEFAULT;
2324 }
2325
2326 /* read the list entries and store them in the output array */
2327 for (outcnt = 0, outp = out_nounlist ; lstsiz > 0 ; )
2328 {
2329 /* make sure we have room for another entry */
2330 if (outcnt >= VOCMAXAMBIG - 1)
2331 break;
2332
2333 /* get the next list entry, and store it in the output array */
2334 if (*lstp == DAT_NIL)
2335 {
2336 /* set the list entry */
2337 outp->vocolobj = MCMONINV;
2338
2339 /* skip the entry */
2340 lstadv(&lstp, &lstsiz);
2341 }
2342 else if (*lstp == DAT_OBJECT)
2343 {
2344 /* set the list entry */
2345 outp->vocolobj = osrp2(lstp+1);
2346
2347 /* skip the list entry */
2348 lstadv(&lstp, &lstsiz);
2349 }
2350 else
2351 {
2352 /* ignore other types in the list */
2353 lstadv(&lstp, &lstsiz);
2354 continue;
2355 }
2356
2357 /* check for a flag entry */
2358 if (lstsiz > 0 && *lstp == DAT_NUMBER)
2359 {
2360 /* set the flags */
2361 outp->vocolflg = (int)osrp4(lstp+1);
2362
2363 /* skip the number */
2364 lstadv(&lstp, &lstsiz);
2365 }
2366 else
2367 {
2368 /* no flags were specified - use the default */
2369 outp->vocolflg = 0;
2370 }
2371
2372 /* set the word list boundaries */
2373 outp->vocolfst = cmd[cur];
2374 outp->vocollst = cmd[*next - 1];
2375
2376 /* count the entry */
2377 ++outp;
2378 ++outcnt;
2379 }
2380
2381 /* terminate the list */
2382 outp->vocolobj = MCMONINV;
2383 outp->vocolflg = 0;
2384
2385 /* set the output count */
2386 *out_nouncount = outcnt;
2387
2388 /*
2389 * set "no_match" appropriately -- set "no_match" true if we're
2390 * returning an empty list and we parsed one or more words
2391 */
2392 if (no_match != 0)
2393 *no_match = (outcnt == 0 && *next > cur);
2394
2395 /*
2396 * Adjust the unknown word count in the context. If the routine
2397 * parsed any unknown words, decrement the unknown word count in
2398 * the context by the number of unknown words parsed, since
2399 * these have now been dealt with. If the return list contains
2400 * any objects flagged as having unknown words, add the count of
2401 * such objects back into the context, since we must still
2402 * resolve these at disambiguation time.
2403 */
2404 for (i = cur ; i < *next ; ++i)
2405 {
2406 /* if this parsed word was unknown, remove it from the count */
2407 if ((typelist[i] & VOCT_UNKNOWN) != 0)
2408 --(ctx->voccxunknown);
2409 }
2410 for (i = 0, outp = out_nounlist ; i < outcnt ; ++i)
2411 {
2412 /* if this object has the unknown flag, count it */
2413 if ((outp->vocolflg & VOCS_UNKNOWN) != 0)
2414 ++(ctx->voccxunknown);
2415 }
2416
2417 /* indicate that the hook provided a list */
2418 return VOC_PNP_SUCCESS;
2419 }
2420 else
2421 {
2422 /*
2423 * ignore any other return value - consider others equivalent to
2424 * DEFAULT
2425 */
2426 rundisc(rcx);
2427 return VOC_PNP_DEFAULT;
2428 }
2429 }
2430
2431 /* ------------------------------------------------------------------------ */
2432 /*
2433 * Build an object name from the words in a command
2434 */
2435 void voc_make_obj_name(voccxdef *ctx, char *namebuf, char *cmd[],
2436 int firstwrd, int lastwrd)
2437 {
2438 int i;
2439
2440 /* run through the range of words, and add them to the buffer */
2441 for (i = firstwrd, namebuf[0] = '\0' ; i < lastwrd ; ++i)
2442 {
2443 if (voc_check_special(ctx, cmd[i], VOCW_OF))
2444 vocaddof(ctx, namebuf);
2445 else
2446 strcat(namebuf, cmd[i]);
2447
2448 if (cmd[i][strlen(cmd[i])-1] == '.' && i + 1 < lastwrd)
2449 strcat(namebuf, "\\");
2450
2451 if (i + 1 < lastwrd)
2452 strcat(namebuf, " ");
2453 }
2454 }
2455
2456 /*
2457 * Make an object name from a list entry
2458 */
2459 void voc_make_obj_name_from_list(voccxdef *ctx, char *namebuf,
2460 char *cmd[], char *firstwrd, char *lastwrd)
2461 {
2462 int i, i1, i2;
2463
2464 /* find the cmd indices */
2465 for (i = i1 = i2 = 0 ; cmd[i] != 0 && *cmd[i] != 0 ; ++i)
2466 {
2467 if (cmd[i] == firstwrd)
2468 i1 = i;
2469 if (cmd[i] == lastwrd)
2470 i2 = i + 1;
2471 }
2472
2473 /* build the name */
2474 voc_make_obj_name(ctx, namebuf, cmd, i1, i2);
2475 }
2476
2477
2478 /* ------------------------------------------------------------------------ */
2479 /*
2480 * get 1 obj - attempts to figure out the limits of a single noun
2481 * phrase. Aside from dealing with special words here ("all", "it",
2482 * "them", string objects, numeric objects), we will accept a basic noun
2483 * phrase of the form [article][adjective*][noun]["of" [noun-phrase]].
2484 * (Note that this is not actually recursive; only one "of" can occur in
2485 * a noun phrase.) If successful, we will construct a list of all
2486 * objects that have all the adjectives and nouns in the noun phrase.
2487 * Note that plurals are treated basically like nouns, except that we
2488 * will flag them so that the disambiguator knows to include all objects
2489 * that work with the plural.
2490 *
2491 * Note that we also allow the special constructs "all [of]
2492 * <noun-phrase>" and "both [of] <noun-phrase>"; these are treated
2493 * identically to normal plurals.
2494 *
2495 * If no_match is not null, we'll set it to true if we found valid
2496 * syntax but no matching objects, false otherwise.
2497 */
2498 static int vocg1o(voccxdef *ctx, char *cmd[], int typelist[],
2499 int cur, int *next, int complain, vocoldef *nounlist,
2500 int chkact, int *no_match)
2501 {
2502 int l1;
2503 int firstwrd;
2504 int i;
2505 int ofword = -1;
2506 int hypothetical_last = -1;
2507 int trim_flags = 0;
2508 int outcnt = 0;
2509 objnum *list1;
2510 uint *flags1;
2511 objnum *list2;
2512 uint *flags2;
2513 char *namebuf;
2514 int has_any = FALSE;
2515 uchar *save_sp;
2516 int found_plural;
2517 int unknown_count;
2518 int trying_count = FALSE;
2519 int retry_with_count;
2520
2521 voc_enter(ctx, &save_sp);
2522 VOC_MAX_ARRAY(ctx, objnum, list1);
2523 VOC_MAX_ARRAY(ctx, uint, flags1);
2524 VOC_MAX_ARRAY(ctx, objnum, list2);
2525 VOC_MAX_ARRAY(ctx, uint, flags2);
2526 VOC_STK_ARRAY(ctx, char, namebuf, VOCBUFSIZ);
2527
2528 /* presume we'll find a match */
2529 if (no_match != 0)
2530 *no_match = FALSE;
2531
2532 /* start at the first word */
2533 *next = cur;
2534
2535 /* if we're at the end of the command, return no objects in list */
2536 if (cur == -1 || !cmd[cur]) { VOC_RETVAL(ctx, save_sp, 0); }
2537
2538 /* show trace message if in debug mode */
2539 if (ctx->voccxflg & VOCCXFDBG)
2540 tioputs(ctx->vocxtio,
2541 chkact ? ". Checking for actor\\n"
2542 : ". Reading noun phrase\\n");
2543
2544 /* try the user parseNounPhrase hook */
2545 switch(voc_pnp_hook(ctx, cmd, typelist, cur, next, complain,
2546 nounlist, &outcnt, chkact, no_match))
2547 {
2548 case VOC_PNP_DEFAULT:
2549 /* continue on to the default processing */
2550 break;
2551
2552 case VOC_PNP_ERROR:
2553 default:
2554 /* return an error */
2555 VOC_RETVAL(ctx, save_sp, -1);
2556
2557 case VOC_PNP_SUCCESS:
2558 /* use their list */
2559 VOC_RETVAL(ctx, save_sp, outcnt);
2560 }
2561
2562 /* check for a quoted string */
2563 if (*cmd[cur] == '"')
2564 {
2565 /* can't use a quoted string as an actor */
2566 if (chkact) { VOC_RETVAL(ctx, save_sp, 0); }
2567
2568 if (ctx->voccxflg & VOCCXFDBG)
2569 tioputs(ctx->vocxtio, "... found quoted string\\n");
2570
2571 nounlist[outcnt].vocolobj = MCMONINV;
2572 nounlist[outcnt].vocolflg = VOCS_STR;
2573 nounlist[outcnt].vocolfst = nounlist[outcnt].vocollst = cmd[cur];
2574 *next = ++cur;
2575 ++outcnt;
2576 VOC_RETVAL(ctx, save_sp, outcnt);
2577 }
2578
2579 /* check for ALL/ANY/BOTH/EITHER [OF] <plural> contruction */
2580 if ((vocspec(cmd[cur], VOCW_ALL)
2581 || vocspec(cmd[cur], VOCW_BOTH)
2582 || vocspec(cmd[cur], VOCW_ANY)) &&
2583 cmd[cur+1] != (char *)0)
2584 {
2585 int nxt;
2586 int n = cur+1;
2587 int has_of;
2588
2589 /* can't use ALL as an actor */
2590 if (chkact) { VOC_RETVAL(ctx, save_sp, 0); }
2591
2592 /* remember whether we have "any" or "either" */
2593 has_any = vocspec(cmd[cur], VOCW_ANY);
2594
2595 /* check for optional 'of' */
2596 if (voc_check_special(ctx, cmd[n], VOCW_OF))
2597 {
2598 if (ctx->voccxflg & VOCCXFDBG)
2599 tioputs(ctx->vocxtio, "... found ALL/ANY/BOTH/EITHER OF\\n");
2600
2601 has_of = TRUE;
2602 n++;
2603 if (!cmd[n])
2604 {
2605 char *p;
2606 int ver;
2607
2608 if (vocspec(cmd[cur], VOCW_ALL))
2609 {
2610 ver = VOCERR(4);
2611 p = "I think you left something out after \"all of\".";
2612 }
2613 else if (vocspec(cmd[cur], VOCW_ANY))
2614 {
2615 ver = VOCERR(29);
2616 p = "I think you left something out after \"any of\".";
2617 }
2618 else
2619 {
2620 ver = VOCERR(5);
2621 p = "There's something missing after \"both of\".";
2622 }
2623 vocerr(ctx, ver, p);
2624 VOC_RETVAL(ctx, save_sp, -1);
2625 }
2626 }
2627 else
2628 has_of = FALSE;
2629
2630 nxt = n;
2631 if (typelist[n] & VOCT_ARTICLE) ++n; /* skip leading article */
2632 for ( ;; )
2633 {
2634 if (!cmd[n])
2635 break;
2636
2637 if (voc_check_special(ctx, cmd[n], VOCW_OF))
2638 {
2639 ++n;
2640 if (!cmd[n])
2641 {
2642 vocerr(ctx, VOCERR(6), "I expected a noun after \"of\".");
2643 VOC_RETVAL(ctx, save_sp, -1);
2644 }
2645 if (*cmd[n] & VOCT_ARTICLE) ++n;
2646 }
2647 else if (typelist[n] & (VOCT_ADJ | VOCT_NOUN))
2648 ++n;
2649 else
2650 break;
2651 }
2652
2653 /*
2654 * Accept the ALL if the last word is a plural. Accept the ANY
2655 * if either we don't have an OF (ANY NOUN is okay even without
2656 * a plural), or if we have OF and a plural. (More simply put,
2657 * accept the ALL or ANY if the last word is a plural, or if we
2658 * have ANY but not OF).
2659 */
2660 if (n > cur && ((typelist[n-1] & VOCT_PLURAL)
2661 || (has_any && !has_of)))
2662 {
2663 if (ctx->voccxflg & VOCCXFDBG)
2664 tioputs(ctx->vocxtio,
2665 "... found ALL/ANY/BOTH/EITHER + noun phrase\\n");
2666
2667 cur = nxt;
2668 }
2669 }
2670
2671 if (vocspec(cmd[cur], VOCW_ALL) && !has_any)
2672 {
2673 /* can't use ALL as an actor */
2674 if (chkact)
2675 {
2676 VOC_RETVAL(ctx, save_sp, 0);
2677 }
2678
2679 if (ctx->voccxflg & VOCCXFDBG)
2680 tioputs(ctx->vocxtio, "... found ALL\\n");
2681
2682 nounlist[outcnt].vocolobj = MCMONINV;
2683 nounlist[outcnt].vocolflg = VOCS_ALL;
2684 nounlist[outcnt].vocolfst = nounlist[outcnt].vocollst = cmd[cur];
2685 ++outcnt;
2686 ++cur;
2687
2688 if (cmd[cur] && vocspec(cmd[cur], VOCW_BUT))
2689 {
2690 int cnt;
2691 int i;
2692 vocoldef *xlist;
2693 uchar *inner_save_sp;
2694
2695 if (ctx->voccxflg & VOCCXFDBG)
2696 tioputs(ctx->vocxtio, "... found ALL EXCEPT\\n");
2697
2698 voc_enter(ctx, &inner_save_sp);
2699 VOC_MAX_ARRAY(ctx, vocoldef, xlist);
2700
2701 cur++;
2702 cnt = vocgobj(ctx, cmd, typelist, cur, next, complain, xlist, 1,
2703 chkact, 0);
2704 if (cnt < 0)
2705 {
2706 /*
2707 * An error occurred - return it. Note that, since
2708 * we're returning from the entire function, we're
2709 * popping the save_sp frame, NOT the inner_save_sp
2710 * frame -- the inner frame is nested within the save_sp
2711 * frame, and we want to pop the entire way out since
2712 * we're exiting the entire function.
2713 */
2714 VOC_RETVAL(ctx, save_sp, cnt);
2715 }
2716 cur = *next;
2717 for (i = 0 ; i < cnt ; ++i)
2718 {
2719 OSCPYSTRUCT(nounlist[outcnt], xlist[i]);
2720 nounlist[outcnt].vocolflg |= VOCS_EXCEPT;
2721 ++outcnt;
2722 }
2723
2724 voc_leave(ctx, inner_save_sp);
2725 }
2726 *next = cur;
2727 nounlist[outcnt].vocolobj = MCMONINV;
2728 nounlist[outcnt].vocolflg = 0;
2729 VOC_RETVAL(ctx, save_sp, outcnt);
2730 }
2731
2732 switch(*cmd[cur])
2733 {
2734 case VOCW_IT:
2735 nounlist[outcnt].vocolflg = VOCS_IT;
2736 goto do_special;
2737 case VOCW_THEM:
2738 nounlist[outcnt].vocolflg = VOCS_THEM;
2739 goto do_special;
2740 case VOCW_HIM:
2741 nounlist[outcnt].vocolflg = VOCS_HIM;
2742 goto do_special;
2743 case VOCW_HER:
2744 nounlist[outcnt].vocolflg = VOCS_HER;
2745 /* FALLTHRU */
2746 do_special:
2747 if (ctx->voccxflg & VOCCXFDBG)
2748 tioputs(ctx->vocxtio, "... found pronoun\\n");
2749
2750 *next = cur + 1;
2751 nounlist[outcnt].vocolobj = MCMONINV;
2752 nounlist[outcnt].vocolfst = nounlist[outcnt].vocollst = cmd[cur];
2753 ++outcnt;
2754 VOC_RETVAL(ctx, save_sp, outcnt);
2755 default:
2756 break;
2757 }
2758
2759 if (((typelist[cur]
2760 & (VOCT_ARTICLE | VOCT_ADJ | VOCT_NOUN | VOCT_UNKNOWN)) == 0)
2761 && !vocisdigit(*cmd[cur]))
2762 {
2763 VOC_RETVAL(ctx, save_sp, 0);
2764 }
2765
2766 if (typelist[cur] & VOCT_ARTICLE)
2767 {
2768 ++cur;
2769 if (cmd[cur] == (char *)0
2770 || ((typelist[cur] & (VOCT_ADJ | VOCT_NOUN | VOCT_UNKNOWN)) == 0
2771 && !vocisdigit(*cmd[cur])))
2772 {
2773 vocerr(ctx, VOCERR(7), "An article must be followed by a noun.");
2774 *next = cur;
2775 VOC_RETVAL(ctx, save_sp, -1);
2776 }
2777 }
2778
2779 /* start at the current word */
2780 firstwrd = cur;
2781
2782 /* scan words for inclusion in this noun phrase */
2783 for (found_plural = FALSE, unknown_count = 0, l1 = 0 ; ; )
2784 {
2785 if (cmd[cur] == (char *)0)
2786 break;
2787
2788 if (typelist[cur] & VOCT_ADJ)
2789 ++cur;
2790 else if (typelist[cur] & VOCT_UNKNOWN)
2791 {
2792 /*
2793 * Remember that we found an unknown word, but include it in
2794 * the noun phrase - this will render the entire noun phrase
2795 * unknown, but we'll resolve that later.
2796 */
2797 ++unknown_count;
2798 ++cur;
2799 }
2800 else if (typelist[cur] & VOCT_NOUN)
2801 {
2802 ++cur;
2803 if (cmd[cur] == (char *)0) break;
2804 if (vocisdigit(*cmd[cur])) ++cur;
2805 if (cmd[cur] == (char *)0) break;
2806 if (!voc_check_special(ctx, cmd[cur], VOCW_OF)) break;
2807 }
2808 else if (vocisdigit(*cmd[cur]))
2809 ++cur;
2810 else if (voc_check_special(ctx, cmd[cur], VOCW_OF))
2811 {
2812 ++cur;
2813 if (ofword != -1)
2814 {
2815 /* there's already one 'of' - we must be done */
2816 --cur;
2817 break;
2818 }
2819 ofword = cur-1;
2820 if (typelist[cur] & VOCT_ARTICLE) /* allow article after "of" */
2821 ++cur;
2822 }
2823 else
2824 break;
2825
2826 /* note whether we found anything that might be a plural */
2827 if (cmd[cur] != 0 && (typelist[cur] & VOCT_PLURAL) != 0)
2828 found_plural = TRUE;
2829 }
2830
2831 try_again:
2832 /*
2833 * build a printable string consisting of the words in the noun
2834 * phrase, for displaying error messages
2835 */
2836 voc_make_obj_name(ctx, namebuf, cmd, firstwrd, cur);
2837
2838 /* remember the next word after this noun phrase */
2839 *next = cur;
2840
2841 /*
2842 * If we have any unknown words, we won't be able to match any
2843 * objects for the noun phrase. Return with one entry in the list,
2844 * but use an invalid object and mark the object as containing
2845 * unknown words.
2846 */
2847 if (unknown_count > 0)
2848 {
2849 /*
2850 * Add one unknown object for each unknown word. This lets us
2851 * communicate the number of unknown words that we found to the
2852 * disambiguator, which will later attempt to resolve the
2853 * reference. Each object we add is the same; they're here only
2854 * for the word count.
2855 */
2856 for ( ; unknown_count > 0 ; --unknown_count)
2857 {
2858 nounlist[outcnt].vocolobj = MCMONINV;
2859 nounlist[outcnt].vocolflg = VOCS_UNKNOWN;
2860 nounlist[outcnt].vocolfst = cmd[firstwrd];
2861 nounlist[outcnt].vocollst = cmd[cur-1];
2862 ++outcnt;
2863 }
2864 nounlist[outcnt].vocolobj = MCMONINV;
2865 nounlist[outcnt].vocolflg = 0;
2866 VOC_RETVAL(ctx, save_sp, outcnt);
2867 }
2868
2869 /* get the list of objects that match the first word */
2870 l1 = vocgol(ctx, list1, flags1, cmd, typelist,
2871 firstwrd, firstwrd, cur, ofword);
2872
2873 /*
2874 * Allow retrying with a count plus a plural if the first word is a
2875 * number, and we have something plural in the list. Only treat "1"
2876 * this way if more words follow in the noun phrase.
2877 */
2878 retry_with_count = ((vocisdigit(*cmd[firstwrd]) && found_plural)
2879 || (vocisdigit(*cmd[firstwrd])
2880 && cur != firstwrd+1
2881 && atoi(cmd[firstwrd]) == 1));
2882
2883 /* see if we found anything on the first word */
2884 if (l1 <= 0)
2885 {
2886 if (chkact) { VOC_RETVAL(ctx, save_sp, 0); }
2887
2888 if (vocisdigit(*cmd[firstwrd]))
2889 {
2890 if (retry_with_count)
2891 {
2892 /* interpret it as a count plus a plural */
2893 trying_count = TRUE;
2894
2895 /* don't try this again */
2896 retry_with_count = FALSE;
2897 }
2898 else
2899 {
2900 /* not a plural - take the number as the entire noun phrase */
2901 nounlist[outcnt].vocolobj = MCMONINV;
2902 nounlist[outcnt].vocolflg = VOCS_NUM;
2903 nounlist[outcnt].vocolfst = nounlist[outcnt].vocollst =
2904 cmd[firstwrd];
2905 *next = firstwrd + 1;
2906 ++outcnt;
2907 nounlist[outcnt].vocolobj = MCMONINV;
2908 nounlist[outcnt].vocolflg = 0;
2909 VOC_RETVAL(ctx, save_sp, outcnt);
2910 }
2911 }
2912 else
2913 {
2914 /*
2915 * display a message if we didn't already (if vocgol
2916 * returned less than zero, it already displayed its own
2917 * error message)
2918 */
2919 if (l1 == 0)
2920 vocerr(ctx, VOCERR(9), "I don't see any %s here.", namebuf);
2921
2922 /* return failure */
2923 VOC_RETVAL(ctx, save_sp, -1);
2924 }
2925 }
2926
2927 retry_exclude_first:
2928 for (i = firstwrd + 1 ; i < cur ; ++i)
2929 {
2930 int l2;
2931
2932 if (voc_check_special(ctx, cmd[i], VOCW_OF)
2933 || (typelist[i] & VOCT_ARTICLE))
2934 continue;
2935
2936 /* get the list for this new object */
2937 l2 = vocgol(ctx, list2, flags2, cmd, typelist,
2938 firstwrd, i, cur, ofword);
2939
2940 /* if that failed and displayed an error, return failure */
2941 if (l2 < 0)
2942 {
2943 /* return failure */
2944 VOC_RETVAL(ctx, save_sp, -1);
2945 }
2946
2947 /*
2948 * Intersect the last list with the new list. If the previous
2949 * list didn't have anything in it, it must mean that the word
2950 * list started with a number, in which case we're trying to
2951 * interpret this as a count plus a plural. So, don't intersect
2952 * the list if there was nothing in the first list.
2953 */
2954 if (l1 == 0)
2955 {
2956 /* just copy the new list */
2957 l1 = l2;
2958 memcpy(list1, list2, (size_t)((l1+1) * sizeof(list1[0])));
2959 memcpy(flags1, flags2, (size_t)(l1 * sizeof(flags1[0])));
2960 }
2961 else
2962 {
2963 /* intersect the two lists */
2964 l1 = vocisect_flags(list1, flags1, list2, flags2);
2965 }
2966
2967 /*
2968 * If there's nothing in the list, it means that there's no
2969 * object that defines all of these words.
2970 */
2971 if (l1 == 0)
2972 {
2973 if (ctx->voccxflg & VOCCXFDBG)
2974 tioputs(ctx->vocxtio,
2975 "... can't find any objects matching these words\\n");
2976 /*
2977 * If there's an "of", remove the "of" and everything that
2978 * follows, and go back and reprocess the part up to the
2979 * "of" -- treat it as a sentence that has two objects, with
2980 * "of" as the preposition introducing the indirect object.
2981 */
2982 if (ofword != -1)
2983 {
2984 if (ctx->voccxflg & VOCCXFDBG)
2985 tioputs(ctx->vocxtio,
2986 "... dropping the part after OF and retrying\\n");
2987
2988 /*
2989 * drop the part from 'of' on - scan only from firstwrd
2990 * to the word before 'of'
2991 */
2992 hypothetical_last = cur;
2993 trim_flags |= VOCS_TRIMPREP;
2994 cur = ofword;
2995
2996 /* forget that we have an 'of' phrase at all */
2997 ofword = -1;
2998
2999 /* retry with the new, shortened phrase */
3000 goto try_again;
3001 }
3002
3003 /*
3004 * Try again with the count + plural interpretation, if
3005 * possible
3006 */
3007 if (retry_with_count)
3008 {
3009 if (ctx->voccxflg & VOCCXFDBG)
3010 tioputs(ctx->vocxtio,
3011 "... treating the number as a count and retrying\\n");
3012
3013 /* we've exhausted our retries */
3014 retry_with_count = FALSE;
3015 trying_count = TRUE;
3016
3017 /* go try it */
3018 goto retry_exclude_first;
3019 }
3020
3021 /*
3022 * If one of the words will work as a preposition, and we
3023 * took it as an adjective, go back and try the word again
3024 * as a preposition.
3025 */
3026 for (i = cur - 1; i > firstwrd ; --i)
3027 {
3028 if (typelist[i] & VOCT_PREP)
3029 {
3030 if (ctx->voccxflg & VOCCXFDBG)
3031 tioputs(ctx->vocxtio,
3032 "... changing word to prep and retrying\\n");
3033
3034 hypothetical_last = cur;
3035 trim_flags |= VOCS_TRIMPREP;
3036 cur = i;
3037 goto try_again;
3038 }
3039 }
3040
3041 /* if just checking actor, don't display an error */
3042 if (chkact) { VOC_RETVAL(ctx, save_sp, 0); }
3043
3044 /*
3045 * tell the player about it unless supressing complaints,
3046 * and return an error
3047 */
3048 if (complain)
3049 vocerr(ctx, VOCERR(9), "I don't see any %s here.", namebuf);
3050 if (no_match != 0)
3051 *no_match = TRUE;
3052 VOC_RETVAL(ctx, save_sp, 0);
3053 }
3054 }
3055
3056 /*
3057 * We have one or more objects, so make a note of how we found
3058 * them.
3059 */
3060 if (ctx->voccxflg & VOCCXFDBG)
3061 tioputs(ctx->vocxtio, "... found objects matching vocabulary:\\n");
3062
3063 /* store the list of objects that match all of our words */
3064 for (i = 0 ; i < l1 ; ++i)
3065 {
3066 if (ctx->voccxflg & VOCCXFDBG)
3067 {
3068 tioputs(ctx->voccxtio, "..... ");
3069 runppr(ctx->voccxrun, list1[i], PRP_SDESC, 0);
3070 tioflushn(ctx->voccxtio, 1);
3071 }
3072
3073 nounlist[outcnt].vocolfst = cmd[firstwrd];
3074 nounlist[outcnt].vocollst = cmd[cur-1];
3075 nounlist[outcnt].vocolflg =
3076 flags1[i] | (trying_count ? VOCS_COUNT : 0) | trim_flags;
3077 if (trim_flags)
3078 nounlist[outcnt].vocolhlst = cmd[hypothetical_last - 1];
3079 if (has_any)
3080 nounlist[outcnt].vocolflg |= VOCS_ANY;
3081 nounlist[outcnt++].vocolobj = list1[i];
3082 if (outcnt > VOCMAXAMBIG)
3083 {
3084 vocerr(ctx, VOCERR(10),
3085 "You're referring to too many objects with \"%s\".",
3086 namebuf);
3087 VOC_RETVAL(ctx, save_sp, -2);
3088 }
3089 }
3090
3091 /*
3092 * If we have a one-word noun phrase, and the word is a number, add
3093 * the number object into the list of objects we're considering,
3094 * even though we found an object that matches. We'll probably
3095 * easily disambiguate this later, but we need to keep open the
3096 * possibility that they're just referring to a number rather than a
3097 * numbered adjective for now.
3098 */
3099 if (firstwrd + 1 == cur && vocisdigit(*cmd[firstwrd]))
3100 {
3101 /* add just the number as an object */
3102 nounlist[outcnt].vocolobj = ctx->voccxnum;
3103 nounlist[outcnt].vocolflg = VOCS_NUM;
3104 nounlist[outcnt].vocolfst = nounlist[outcnt].vocollst =
3105 cmd[firstwrd];
3106 ++outcnt;
3107 }
3108
3109 /* terminate the list */
3110 nounlist[outcnt].vocolobj = MCMONINV;
3111 nounlist[outcnt].vocolflg = 0;
3112
3113 /* return the number of objects in our match list */
3114 VOC_RETVAL(ctx, save_sp, outcnt);
3115 }
3116
3117 /*
3118 * get obj - gets one or more noun lists (a flag, "multi", says whether
3119 * we should allow multiple lists). We use vocg1o() to read noun lists
3120 * one at a time, and keep going (if "multi" is true) as long as there
3121 * are more "and <noun-phrase>" clauses.
3122 *
3123 * If no_match is not null, we'll set it to true if the syntax was okay
3124 * but we didn't find any match for the list of words, false otherwise.
3125 */
3126 int vocgobj(voccxdef *ctx, char *cmd[], int typelist[],
3127 int cur, int *next, int complain, vocoldef *nounlist,
3128 int multi, int chkact, int *no_match)
3129 {
3130 int cnt;
3131 int outcnt = 0;
3132 int i;
3133 int again = FALSE;
3134 int lastcur;
3135 vocoldef *tmplist;
3136 uchar *save_sp;
3137
3138 voc_enter(ctx, &save_sp);
3139 VOC_MAX_ARRAY(ctx, vocoldef, tmplist);
3140
3141 for ( ;; )
3142 {
3143 /* try getting a single object */
3144 cnt = vocg1o(ctx, cmd, typelist, cur, next, complain,
3145 tmplist, chkact, no_match);
3146
3147 /* if we encountered a syntax error, return failure */
3148 if (cnt < 0)
3149 {
3150 VOC_RETVAL(ctx, save_sp, cnt);
3151 }
3152
3153 /* if we got any objects, store them in our output list */
3154 if (cnt > 0)
3155 {
3156 for (i = 0 ; i < cnt ; ++i)
3157 {
3158 OSCPYSTRUCT(nounlist[outcnt], tmplist[i]);
3159 if (++outcnt > VOCMAXAMBIG)
3160 {
3161 vocerr(ctx, VOCERR(11),
3162 "You're referring to too many objects.");
3163 VOC_RETVAL(ctx, save_sp, -1);
3164 }
3165 }
3166 }
3167
3168 /* if we didn't find any objects, stop looking */
3169 if (cnt == 0)
3170 {
3171 if (again)
3172 *next = lastcur;
3173 break;
3174 }
3175
3176 /*
3177 * if the caller only wanted a single object (or is getting an
3178 * actor, in which case they implicitly want only a single
3179 * object), stop looking for additional noun phrases
3180 */
3181 if (!multi || chkact)
3182 break;
3183
3184 /* skip past the previous noun phrase */
3185 cur = *next;
3186
3187 /*
3188 * if we're looking at a noun phrase separator ("and" or a
3189 * comma), get the next noun phrase; otherwise, we're done
3190 */
3191 if (cur != -1 && cmd[cur] != 0 && vocspec(cmd[cur], VOCW_AND))
3192 {
3193 lastcur = cur;
3194 while (cmd[cur] && vocspec(cmd[cur], VOCW_AND)) ++cur;
3195 again = TRUE;
3196 if (complain) complain = 2;
3197 }
3198 else
3199 {
3200 /* end of line, or not at a separator - we're done */
3201 break;
3202 }
3203 }
3204
3205 /* terminate the list and return the number of objects we found */
3206 nounlist[outcnt].vocolobj = MCMONINV;
3207 nounlist[outcnt].vocolflg = 0;
3208 VOC_RETVAL(ctx, save_sp, outcnt);
3209 }
3210
3211
3212 /* ------------------------------------------------------------------------ */
3213 /*
3214 * TADS program code interface - tokenize a string. Returns a list of
3215 * strings, with each string giving a token in the command.
3216 */
3217 void voc_parse_tok(voccxdef *ctx)
3218 {
3219 uchar *save_sp;
3220 runcxdef *rcx = ctx->voccxrun;
3221 char **cmd;
3222 char *inbuf;
3223 char *outbuf;
3224 uchar *p;
3225 uint len;
3226 int cnt;
3227
3228 /* enter our stack frame */
3229 voc_enter(ctx, &save_sp);
3230
3231 /* get the string argument */
3232 p = runpopstr(rcx);
3233
3234 /* get and skip the length prefix */
3235 len = osrp2(p) - 2;
3236 p += 2;
3237
3238 /*
3239 * Allocate space for the original string, and space for the token
3240 * pointers and the tokenized string buffer. We could potentially
3241 * have one token per character in the original string, and we could
3242 * potentially need one extra null terminator for each character in
3243 * the original string; allocate accordingly.
3244 */
3245 VOC_STK_ARRAY(ctx, char, inbuf, len + 1);
3246 VOC_STK_ARRAY(ctx, char, outbuf, len*2);
3247 VOC_STK_ARRAY(ctx, char *, cmd, len*2);
3248
3249 /* copy the string into our buffer, and null-terminate it */
3250 memcpy(inbuf, p, len);
3251 inbuf[len] = '\0';
3252
3253 /* tokenize the string */
3254 cnt = voctok(ctx, inbuf, outbuf, cmd, TRUE, FALSE, FALSE);
3255
3256 /* check the result */
3257 if (cnt < 0)
3258 {
3259 /* negative count - it's an error, so return nil */
3260 runpnil(rcx);
3261 }
3262 else
3263 {
3264 /* push the return list */
3265 voc_push_toklist(ctx, cmd, cnt);
3266 }
3267
3268 /* leave our stack frame */
3269 voc_leave(ctx, save_sp);
3270 }
3271
3272 /* ------------------------------------------------------------------------ */
3273 /*
3274 * TADS program code interface - get the list of types for a list words.
3275 * The words are simply strings of the type returned from the tokenizer.
3276 * The return value is a list of types, with each entry in the return
3277 * list giving the types of the corresponding entry in the word list.
3278 */
3279 void voc_parse_types(voccxdef *ctx)
3280 {
3281 runcxdef *rcx = ctx->voccxrun;
3282 uchar *wrdp;
3283 uint wrdsiz;
3284 uchar *typp;
3285 uchar *lstp;
3286 uint lstsiz;
3287 int wrdcnt;
3288
3289 /* get the list of words from the stack */
3290 wrdp = runpoplst(rcx);
3291
3292 /* read and skip the size prefix */
3293 wrdsiz = osrp2(wrdp) - 2;
3294 wrdp += 2;
3295
3296 /* scan the list and count the number of string entries */
3297 for (wrdcnt = 0, lstp = wrdp, lstsiz = wrdsiz ; lstsiz != 0 ;
3298 lstadv(&lstp, &lstsiz))
3299 {
3300 /* if this is a string, count it */
3301 if (*lstp == DAT_SSTRING)
3302 ++wrdcnt;
3303 }
3304
3305 /* allocate the return list - one number entry per word */
3306 typp = voc_push_list(ctx, wrdcnt, 4);
3307
3308 /* look up each word and set the corresponding element in the type list */
3309 for (lstp = wrdp, lstsiz = wrdsiz ; lstsiz != 0 ; lstadv(&lstp, &lstsiz))
3310 {
3311 /* if this is a string, look it up in the dictionary */
3312 if (*lstp == DAT_SSTRING)
3313 {
3314 char buf[256];
3315 int curtyp;
3316 uint len;
3317
3318 /* make sure it fits in our buffer */
3319 len = osrp2(lstp+1) - 2;
3320 if (len < sizeof(buf))
3321 {
3322 /* extract the word into our buffer */
3323 memcpy(buf, lstp+3, len);
3324
3325 /* null-terminate it */
3326 buf[len] = '\0';
3327
3328 /* get the type */
3329 curtyp = voc_lookup_type(ctx, buf, len, TRUE);
3330
3331 /* if they didn't find a type at all, set it to UNKNOWN */
3332 if (curtyp == 0)
3333 curtyp = VOCT_UNKNOWN;
3334 }
3335 else
3336 {
3337 /* the string is too big - just mark it as unknown */
3338 curtyp = VOCT_UNKNOWN;
3339 }
3340
3341 /* add this type to the return list */
3342 *typp++ = DAT_NUMBER;
3343 oswp4s(typp, curtyp);
3344 typp += 4;
3345 }
3346 }
3347 }
3348
3349
3350 /* ------------------------------------------------------------------------ */
3351 /*
3352 * Parse a noun phrase from TADS program code. Takes a list of words
3353 * and a list of types from the stack, uses the standard noun phrase
3354 * parser, and returns a list of matching objects. The object list is
3355 * not disambiguated, but merely reflects all matching objects. The
3356 * entire standard parsing algorithm applies, including parseNounPhrase
3357 * invocation if appropriate.
3358 */
3359 void voc_parse_np(voccxdef *ctx)
3360 {
3361 runcxdef *rcx = ctx->voccxrun;
3362 vocoldef *objlist;
3363 uchar *save_sp;
3364 uchar *wordp;
3365 uint wordsiz;
3366 uchar *typp;
3367 uint typsiz;
3368 int cnt;
3369 char **wordarr;
3370 int wordcnt;
3371 char *wordchararr;
3372 uint wordcharsiz;
3373 int *typarr;
3374 int complain;
3375 int chkact;
3376 int multi;
3377 int no_match;
3378 int next;
3379 int cur;
3380 uchar *lstp;
3381 uint lstsiz;
3382 char *p;
3383 int i;
3384 int old_unknown, old_lastunk;
3385
3386 /* allocate stack arrays */
3387 voc_enter(ctx, &save_sp);
3388 VOC_MAX_ARRAY(ctx, vocoldef, objlist);
3389
3390 /*
3391 * Save the original context unknown values, since we don't want to
3392 * affect the context information in this game-initiated call, then
3393 * clear the unknown word count for the duration of the call.
3394 */
3395 old_unknown = ctx->voccxunknown;
3396 old_lastunk = ctx->voccxlastunk;
3397 ctx->voccxunknown = ctx->voccxlastunk = 0;
3398
3399 /* get the list of words, and read its length prefix */
3400 wordp = runpoplst(rcx);
3401 wordsiz = osrp2(wordp) - 2;
3402 wordp += 2;
3403
3404 /* get the list of types, and read its length prefix */
3405 typp = runpoplst(rcx);
3406 typsiz = osrp2(typp) - 2;
3407 typp += 2;
3408
3409 /* get the starting index (adjusting for zero-based indexing) */
3410 cur = runpopnum(rcx) - 1;
3411 next = cur;
3412
3413 /* get the flag arguments */
3414 complain = runpoplog(rcx);
3415 multi = runpoplog(rcx);
3416 chkact = runpoplog(rcx);
3417
3418 /* count the words in the word list */
3419 for (wordcnt = 0, lstp = wordp, wordcharsiz = 0, lstsiz = wordsiz ;
3420 lstsiz != 0 ; lstadv(&lstp, &lstsiz))
3421 {
3422 /* count the word */
3423 ++wordcnt;
3424
3425 /*
3426 * count the space needed for the word - count the bytes of the
3427 * string plus a null terminator
3428 */
3429 if (*lstp == DAT_SSTRING)
3430 wordcharsiz += osrp2(lstp+1) + 1;
3431 }
3432
3433 /* allocate space for the word arrays */
3434 VOC_STK_ARRAY(ctx, char, wordchararr, wordcharsiz);
3435 VOC_STK_ARRAY(ctx, char *, wordarr, wordcnt+1);
3436 VOC_STK_ARRAY(ctx, int, typarr, wordcnt+1);
3437
3438 /* build the word array */
3439 for (i = 0, p = wordchararr, lstp = wordp, lstsiz = wordsiz ;
3440 lstsiz != 0 ; lstadv(&lstp, &lstsiz))
3441 {
3442 /* add the word to the word array */
3443 if (*lstp == DAT_SSTRING)
3444 {
3445 uint len;
3446
3447 /* add this entry to the word array */
3448 wordarr[i] = p;
3449
3450 /* copy the word to the character array and null-terminate it */
3451 len = osrp2(lstp + 1) - 2;
3452 memcpy(p, lstp + 3, len);
3453 p[len] = '\0';
3454
3455 /* move the write pointer past this entry */
3456 p += len + 1;
3457
3458 /* bump the index */
3459 ++i;
3460 }
3461 }
3462
3463 /* store an empty last entry */
3464 wordarr[i] = 0;
3465
3466 /* build the type array */
3467 for (i = 0, lstp = typp, lstsiz = typsiz ;
3468 lstsiz != 0 && i < wordcnt ; lstadv(&lstp, &lstsiz))
3469 {
3470 /* add this entry to the type array */
3471 if (*lstp == DAT_NUMBER)
3472 {
3473 /* set the entry */
3474 typarr[i] = (int)osrp4(lstp + 1);
3475
3476 /* bump the index */
3477 ++i;
3478 }
3479 }
3480
3481 /* parse the noun phrase */
3482 cnt = vocgobj(ctx, wordarr, typarr, cur, &next, complain, objlist,
3483 multi, chkact, &no_match);
3484
3485 /* restore context unknown values */
3486 ctx->voccxunknown = old_unknown;
3487 ctx->voccxlastunk = old_lastunk;
3488
3489 /* check the return value */
3490 if (cnt < 0)
3491 {
3492 /* syntax error; return nil to indicate an error */
3493 runpnil(rcx);
3494 }
3495 else if (cnt == 0)
3496 {
3497 /*
3498 * No objects found. Return a list consisting only of the next
3499 * index. If the next index is equal to the starting index,
3500 * this will tell the caller that no noun phrase is
3501 * syntactically present; otherwise, it will tell the caller
3502 * that a noun phrase is present but there are no matching
3503 * objects.
3504 *
3505 * Note that we must increment the returned element index to
3506 * conform with the 1-based index values that the game function
3507 * uses.
3508 */
3509 ++next;
3510 voc_push_numlist(ctx, (uint *)&next, 1);
3511 }
3512 else
3513 {
3514 /*
3515 * We found one or more objects. Return a list whose first
3516 * element is the index of the next word to be parsed, and whose
3517 * remaining elements are sublists. Each sublist contains a
3518 * match for one noun phrase; for each AND adding another noun
3519 * phrase, there's another sublist. Each sublist contains the
3520 * index of the first word of its noun phrase, the index of the
3521 * last word of its noun phrase, and then the objects. For each
3522 * object, there is a pair of entries: the object itself, and
3523 * the flags for the object.
3524 *
3525 * First, figure out how much space we need by scanning the
3526 * return list.
3527 */
3528 for (lstsiz = 0, i = 0 ; i < cnt ; )
3529 {
3530 int j;
3531
3532 /*
3533 * count the entries in this sublist by looking for the next
3534 * entry whose starting word is different
3535 */
3536 for (j = i ;
3537 j < cnt && objlist[j].vocolfst == objlist[i].vocolfst ;
3538 ++j)
3539 {
3540 /*
3541 * for this entry, we need space for the object (1 + 2
3542 * for an object, or just 1 for nil) and flags (1 + 4)
3543 */
3544 if (objlist[j].vocolobj == MCMONINV)
3545 lstsiz += 1;
3546 else
3547 lstsiz += 3;
3548 lstsiz += 5;
3549 }
3550
3551 /*
3552 * For this sublist, we need space for the first index (type
3553 * prefix + number = 1 + 4 = 5) and the last index (5).
3554 * We've already counted space for the objects in the list.
3555 * Finally, we need space for the list type and length
3556 * prefixes (1 + 2) for the sublist itself.
3557 */
3558 lstsiz += (5 + 5) + 3;
3559
3560 /* skip to the next element */
3561 i = j;
3562 }
3563
3564 /*
3565 * finally, we need space for the first element of the list,
3566 * which is the index of the next word to be parsed (1+4)
3567 */
3568 lstsiz += 5;
3569
3570 /* allocate space for the list */
3571 lstp = voc_push_list_siz(ctx, lstsiz);
3572
3573 /*
3574 * store the first element - the index of the next word to parse
3575 * (adjusting to 1-based indexing)
3576 */
3577 *lstp++ = DAT_NUMBER;
3578 oswp4s(lstp, next + 1);
3579 lstp += 4;
3580
3581 /* build the list */
3582 for (i = 0 ; i < cnt ; )
3583 {
3584 uchar *sublstp;
3585 int j;
3586 int firstidx;
3587 int lastidx;
3588
3589 /* store the list type prefix */
3590 *lstp++ = DAT_LIST;
3591
3592 /*
3593 * remember where the length prefix is, then skip it - we'll
3594 * come back and fill it in when we're done with the sublist
3595 */
3596 sublstp = lstp;
3597 lstp += 2;
3598
3599 /* search the array to find the indices of the boundary words */
3600 for (j = 0 ; j < wordcnt ; ++j)
3601 {
3602 /* if this is the first word, remember the index */
3603 if (wordarr[j] == objlist[i].vocolfst)
3604 firstidx = j;
3605
3606 /* if this is the last word, remember the index */
3607 if (wordarr[j] == objlist[i].vocollst)
3608 {
3609 /* remember the index */
3610 lastidx = j;
3611
3612 /*
3613 * we can stop looking now, since the words are
3614 * always in order (so the first index will never be
3615 * after the last index)
3616 */
3617 break;
3618 }
3619 }
3620
3621 /* add the first and last index, adjusting to 1-based indexing */
3622 *lstp++ = DAT_NUMBER;
3623 oswp4s(lstp, firstidx + 1);
3624 lstp += 4;
3625 *lstp++ = DAT_NUMBER;
3626 oswp4s(lstp, lastidx + 1);
3627 lstp += 4;
3628
3629 /* add the objects */
3630 for (j = i ;
3631 j < cnt && objlist[j].vocolfst == objlist[i].vocolfst ;
3632 ++j)
3633 {
3634 /* add this object */
3635 if (objlist[j].vocolobj == MCMONINV)
3636 {
3637 /* just store a nil */
3638 *lstp++ = DAT_NIL;
3639 }
3640 else
3641 {
3642 /* store the object */
3643 *lstp++ = DAT_OBJECT;
3644 oswp2(lstp, objlist[j].vocolobj);
3645 lstp += 2;
3646 }
3647
3648 /* add the flags */
3649 *lstp++ = DAT_NUMBER;
3650 oswp4s(lstp, objlist[i].vocolflg);
3651 lstp += 4;
3652 }
3653
3654 /* fix up the sublist's length prefix */
3655 oswp2(sublstp, lstp - sublstp);
3656
3657 /* move on to the next sublist */
3658 i = j;
3659 }
3660 }
3661
3662 /* exit the stack frame */
3663 voc_leave(ctx, save_sp);
3664 }
3665
3666
3667 /* ------------------------------------------------------------------------ */
3668 /*
3669 * TADS program code interface - given a list of words and a list of
3670 * their types, produce a list of objects that match all of the words.
3671 */
3672 void voc_parse_dict_lookup(voccxdef *ctx)
3673 {
3674 uchar *save_sp;
3675 runcxdef *rcx = ctx->voccxrun;
3676 uchar *wrdp;
3677 uint wrdsiz;
3678 uchar *typp;
3679 uint typsiz;
3680 objnum *list1;
3681 objnum *list2;
3682 int cnt1;
3683 int cnt2;
3684
3685 /* enter our stack frame and allocate stack arrays */
3686 voc_enter(ctx, &save_sp);
3687 VOC_MAX_ARRAY(ctx, objnum, list1);
3688 VOC_MAX_ARRAY(ctx, objnum, list2);
3689
3690 /* get the word list, and read and skip its size prefix */
3691 wrdp = runpoplst(rcx);
3692 wrdsiz = osrp2(wrdp) - 2;
3693 wrdp += 2;
3694
3695 /* get the type list, and read and skip its size prefix */
3696 typp = runpoplst(rcx);
3697 typsiz = osrp2(typp) - 2;
3698 typp += 2;
3699
3700 /* nothing in the main list yet */
3701 cnt1 = 0;
3702
3703 /* go through the word list */
3704 while (wrdsiz > 0)
3705 {
3706 int curtyp;
3707 int type_prop;
3708 char *curword;
3709 uint curwordlen;
3710 char *curword2;
3711 uint curwordlen2;
3712 vocwdef *v;
3713 char *p;
3714 uint len;
3715 vocseadef search_ctx;
3716
3717 /* if this entry is a string, consider it */
3718 if (*wrdp == DAT_SSTRING)
3719 {
3720 /* get the current word's text string */
3721 curword = (char *)(wrdp + 3);
3722 curwordlen = osrp2(wrdp+1) - 2;
3723
3724 /* check for an embedded space */
3725 for (p = curword, len = curwordlen ; len != 0 && !t_isspace(*p) ;
3726 ++p, --len) ;
3727 if (len != 0)
3728 {
3729 /* get the second word */
3730 curword2 = p + 1;
3731 curwordlen2 = len - 1;
3732
3733 /* truncate the first word accordingly */
3734 curwordlen -= len;
3735 }
3736 else
3737 {
3738 /* no embedded space -> no second word */
3739 curword2 = 0;
3740 curwordlen2 = 0;
3741 }
3742
3743 /* presume we won't find a valid type property */
3744 type_prop = PRP_INVALID;
3745
3746 /*
3747 * get this type entry, if there's another entry in the
3748 * list, and it's of the appropriate type
3749 */
3750 if (typsiz > 0 && *typp == DAT_NUMBER)
3751 {
3752 /*
3753 * Figure out what type property we'll be using. We'll
3754 * consider only one meaning for each word, and we'll
3755 * arbitrarily pick one if the type code has more than
3756 * one type, because we expect the caller to provide
3757 * exactly one type per word.
3758 */
3759 int i;
3760 struct typemap_t
3761 {
3762 int flag;
3763 int prop;
3764 };
3765 static struct typemap_t typemap[] =
3766 {
3767 { VOCT_ARTICLE, PRP_ARTICLE },
3768 { VOCT_ADJ, PRP_ADJ },
3769 { VOCT_NOUN, PRP_NOUN },
3770 { VOCT_PREP, PRP_PREP },
3771 { VOCT_VERB, PRP_VERB },
3772 { VOCT_PLURAL, PRP_PLURAL }
3773 };
3774 struct typemap_t *mapp;
3775
3776 /* get the type */
3777 curtyp = (int)osrp4(typp+1);
3778
3779 /* search for a type */
3780 for (mapp = typemap, i = sizeof(typemap)/sizeof(typemap[0]) ;
3781 i != 0 ; ++mapp, --i)
3782 {
3783 /* if this flag is set, use this type property */
3784 if ((curtyp & mapp->flag) != 0)
3785 {
3786 /* use this one */
3787 type_prop = mapp->prop;
3788 break;
3789 }
3790 }
3791 }
3792
3793 /* nothing in the new list yet */
3794 cnt2 = 0;
3795
3796 /* scan for matching words */
3797 for (v = vocffw(ctx, curword, curwordlen, curword2, curwordlen2,
3798 type_prop, &search_ctx) ;
3799 v != 0 ;
3800 v = vocfnw(ctx, &search_ctx))
3801 {
3802 int i;
3803
3804 /* make sure we have room in our list */
3805 if (cnt2 >= VOCMAXAMBIG - 1)
3806 break;
3807
3808 /* make sure that this entry isn't already in our list */
3809 for (i = 0 ; i < cnt2 ; ++i)
3810 {
3811 /* if this entry matches, stop looking */
3812 if (list2[i] == v->vocwobj)
3813 break;
3814 }
3815
3816 /* if it's not already in the list, add it now */
3817 if (i == cnt2)
3818 {
3819 /* add it to our list */
3820 list2[cnt2++] = v->vocwobj;
3821 }
3822 }
3823
3824 /* terminate the list */
3825 list2[cnt2] = MCMONINV;
3826
3827 /*
3828 * if there's nothing in the first list, simply copy this
3829 * into the first list; otherwise, intersect the two lists
3830 */
3831 if (cnt1 == 0)
3832 {
3833 /* this is the first list -> copy it into the main list */
3834 memcpy(list1, list2, (cnt2+1)*sizeof(list2[0]));
3835 cnt1 = cnt2;
3836 }
3837 else
3838 {
3839 /* intersect the two lists */
3840 cnt1 = vocisect(list1, list2);
3841 }
3842
3843 /*
3844 * if there's nothing in the result list now, there's no
3845 * need to look any further, because further intersection
3846 * will yield nothing
3847 */
3848 if (cnt1 == 0)
3849 break;
3850 }
3851
3852 /* advance the word list */
3853 lstadv(&wrdp, &wrdsiz);
3854
3855 /* if there's anything left in the type list, advance it as well */
3856 if (typsiz > 0)
3857 lstadv(&typp, &typsiz);
3858 }
3859
3860 /* push the result list */
3861 voc_push_objlist(ctx, list1, cnt1);
3862
3863 /* exit our stack frame */
3864 voc_leave(ctx, save_sp);
3865 }
3866
3867 /* ------------------------------------------------------------------------ */
3868 /*
3869 * TADS program code interface - disambiguate a noun list. We take the
3870 * kind of complex object list returned by voc_parse_np(), and produce a
3871 * fully-resolved list of objects.
3872 */
3873 void voc_parse_disambig(voccxdef *ctx)
3874 {
3875 voccxdef ctx_copy;
3876 uchar *save_sp;
3877 runcxdef *rcx = ctx->voccxrun;
3878 vocoldef *inlist;
3879 vocoldef *outlist;
3880 int objcnt;
3881 char **cmd;
3882 char *cmdbuf;
3883 char *oopsbuf;
3884 objnum actor;
3885 objnum verb;
3886 objnum prep;
3887 objnum otherobj;
3888 prpnum defprop;
3889 prpnum accprop;
3890 prpnum verprop;
3891 uchar *tokp;
3892 uint toksiz;
3893 uchar *objp;
3894 uint objsiz;
3895 uchar *lstp;
3896 uint lstsiz;
3897 int tokcnt;
3898 char *p;
3899 int i;
3900 int silent;
3901 int err;
3902 int unknown_count;
3903
3904 /* allocate our stack arrays */
3905 voc_enter(ctx, &save_sp);
3906 VOC_MAX_ARRAY(ctx, vocoldef, outlist);
3907 VOC_STK_ARRAY(ctx, char, cmdbuf, VOCBUFSIZ);
3908 VOC_STK_ARRAY(ctx, char, oopsbuf, VOCBUFSIZ);
3909
3910 /* get the actor, verb, prep, and otherobj arguments */
3911 actor = runpopobj(rcx);
3912 verb = runpopobj(rcx);
3913 prep = runpopobjnil(rcx);
3914 otherobj = runpopobjnil(rcx);
3915
3916 /*
3917 * get the usage parameter, and use it to select the appropriate
3918 * defprop and accprop
3919 */
3920 switch(runpopnum(rcx))
3921 {
3922 case VOC_PRO_RESOLVE_DOBJ:
3923 default:
3924 defprop = PRP_DODEFAULT;
3925 accprop = PRP_VALIDDO;
3926 break;
3927
3928 case VOC_PRO_RESOLVE_IOBJ:
3929 defprop = PRP_IODEFAULT;
3930 accprop = PRP_VALIDIO;
3931 break;
3932
3933 case VOC_PRO_RESOLVE_ACTOR:
3934 defprop = PRP_DODEFAULT;
3935 accprop = PRP_VALIDACTOR;
3936 break;
3937 }
3938
3939 /* get the verprop argument */
3940 verprop = runpopprp(rcx);
3941
3942 /* pop the token list, and read and skip the length prefix */
3943 tokp = runpoplst(rcx);
3944 toksiz = osrp2(tokp) - 2;
3945 tokp += 2;
3946
3947 /* pop the object list, and read and skip the length prefix */
3948 objp = runpoplst(rcx);
3949 objsiz = osrp2(objp) - 2;
3950 objp += 2;
3951
3952 /* pop the "silent" flag */
3953 silent = runpoplog(rcx);
3954
3955 /* count the tokens */
3956 for (lstp = tokp, lstsiz = toksiz, tokcnt = 0 ;
3957 lstsiz != 0 ; lstadv(&lstp, &lstsiz))
3958 {
3959 /* if this is a string, count it */
3960 if (*lstp == DAT_SSTRING)
3961 ++tokcnt;
3962 }
3963
3964 /* allocate stack space for the command pointers and buffer */
3965 VOC_STK_ARRAY(ctx, char *, cmd, tokcnt + 1);
3966
3967 /* extract the tokens into our pointer buffer */
3968 for (lstp = tokp, lstsiz = toksiz, i = 0, p = cmdbuf ;
3969 lstsiz != 0 ; lstadv(&lstp, &lstsiz))
3970 {
3971 /* if this is a string, count and size it */
3972 if (*lstp == DAT_SSTRING)
3973 {
3974 uint len;
3975
3976 /* store a pointer to the word in the command buffer */
3977 cmd[i++] = p;
3978
3979 /* copy the token into the command buffer and null-terminate it */
3980 len = osrp2(lstp + 1) - 2;
3981 memcpy(p, lstp + 3, len);
3982 p[len] = '\0';
3983
3984 /* move the buffer pointer past this word */
3985 p += len + 1;
3986 }
3987 }
3988
3989 /* store a null at the end of the command pointer list */
3990 cmd[i] = 0;
3991
3992 /*
3993 * The object list is provided in the same format as the list
3994 * returned by voc_parse_np(), but the leading index number is
3995 * optional. We don't need the leading index for anything, so if
3996 * it's there, simply skip it so that we can start with the first
3997 * sublist.
3998 */
3999 if (objsiz > 0 && *objp == DAT_NUMBER)
4000 lstadv(&objp, &objsiz);
4001
4002 /*
4003 * Count the objects in the object list, so that we can figure out
4004 * how much we need to allocate for the input object list.
4005 */
4006 for (lstp = objp, lstsiz = objsiz, objcnt = 0 ; lstsiz != 0 ;
4007 lstadv(&lstp, &lstsiz))
4008 {
4009 /* if this is a sublist, parse it */
4010 if (*lstp == DAT_LIST)
4011 {
4012 uchar *subp;
4013 uint subsiz;
4014
4015 /* get the sublist pointer, and read and skip the size prefix */
4016 subp = lstp + 1;
4017 subsiz = osrp2(subp) - 2;
4018 subp += 2;
4019
4020 /* scan the sublist */
4021 while (subsiz > 0)
4022 {
4023 /* if this is an object, count it */
4024 if (*subp == DAT_OBJECT || *subp == DAT_NIL)
4025 ++objcnt;
4026
4027 /* skip this element */
4028 lstadv(&subp, &subsiz);
4029 }
4030 }
4031 }
4032
4033 /* allocate space for the input list */
4034 VOC_STK_ARRAY(ctx, vocoldef, inlist, objcnt + 1);
4035
4036 /* we don't have any unknown words yet */
4037 unknown_count = 0;
4038
4039 /* parse the list, filling in the input array */
4040 for (lstp = objp, lstsiz = objsiz, i = 0 ; lstsiz != 0 ;
4041 lstadv(&lstp, &lstsiz))
4042 {
4043 /* if this is a sublist, parse it */
4044 if (*lstp == DAT_LIST)
4045 {
4046 uchar *subp;
4047 uint subsiz;
4048 int firstwrd, lastwrd;
4049
4050 /* get the sublist pointer, and read and skip the size prefix */
4051 subp = lstp + 1;
4052 subsiz = osrp2(subp) - 2;
4053 subp += 2;
4054
4055 /* in case we don't find token indices, clear them */
4056 firstwrd = 0;
4057 lastwrd = 0;
4058
4059 /*
4060 * the first two elements of the list are the token indices
4061 * of the first and last words of this object's noun phrase
4062 */
4063 if (subsiz > 0 && *subp == DAT_NUMBER)
4064 {
4065 /* read the first index, adjusting to zero-based indexing */
4066 firstwrd = (int)osrp4(subp+1) - 1;
4067
4068 /* make sure it's in range */
4069 if (firstwrd < 0)
4070 firstwrd = 0;
4071 else if (firstwrd > tokcnt)
4072 firstwrd = tokcnt;
4073
4074 /* skip it */
4075 lstadv(&subp, &subsiz);
4076 }
4077 if (subsiz > 0 && *subp == DAT_NUMBER)
4078 {
4079 /* read the last index, adjusting to zero-based indexing */
4080 lastwrd = (int)osrp4(subp+1) - 1;
4081
4082 /* make sure it's in range */
4083 if (lastwrd < firstwrd)
4084 lastwrd = firstwrd;
4085 else if (lastwrd > tokcnt)
4086 lastwrd = tokcnt;
4087
4088 /* skip it */
4089 lstadv(&subp, &subsiz);
4090 }
4091
4092 /* scan the sublist */
4093 while (subsiz > 0)
4094 {
4095 /* if this is an object, store it */
4096 if (*subp == DAT_OBJECT || *subp == DAT_NIL)
4097 {
4098 /* store the object */
4099 if (*subp == DAT_OBJECT)
4100 inlist[i].vocolobj = osrp2(subp+1);
4101 else
4102 inlist[i].vocolobj = MCMONINV;
4103
4104 /* set the first and last word pointers */
4105 inlist[i].vocolfst = cmd[firstwrd];
4106 inlist[i].vocollst = cmd[lastwrd];
4107
4108 /* skip the object */
4109 lstadv(&subp, &subsiz);
4110
4111 /* check for flags */
4112 if (subsiz > 0 && *subp == DAT_NUMBER)
4113 {
4114 /* get the flags value */
4115 inlist[i].vocolflg = (int)osrp4(subp+1);
4116
4117 /* skip the number in the list */
4118 lstadv(&subp, &subsiz);
4119 }
4120 else
4121 {
4122 /* clear the flags */
4123 inlist[i].vocolflg = 0;
4124 }
4125
4126 /* if an unknown word was involved, note it */
4127 if ((inlist[i].vocolflg & VOCS_UNKNOWN) != 0)
4128 ++unknown_count;
4129
4130 /* consume the element */
4131 ++i;
4132 }
4133 else
4134 {
4135 /* skip this element */
4136 lstadv(&subp, &subsiz);
4137 }
4138 }
4139 }
4140 }
4141
4142 /* terminate the list */
4143 inlist[i].vocolobj = MCMONINV;
4144 inlist[i].vocolflg = 0;
4145
4146 /*
4147 * make a copy of our context, so the disambiguation can't make any
4148 * global changes
4149 */
4150 memcpy(&ctx_copy, ctx, sizeof(ctx_copy));
4151
4152 /*
4153 * Count the unknown words and set the count in the context. This
4154 * will allow us to determine after we call the resolver whether the
4155 * resolution process cleared up the unknown words (via
4156 * parseUnknownDobj/Iobj).
4157 */
4158 ctx_copy.voccxunknown = ctx_copy.voccxlastunk = unknown_count;
4159
4160 /* disambiguate the noun list */
4161 err = vocdisambig(&ctx_copy, outlist, inlist,
4162 defprop, accprop, verprop, cmd,
4163 otherobj, actor, verb, prep, cmdbuf, silent);
4164
4165 /*
4166 * If the error was VOCERR(2) - unknown word - check the input list
4167 * to see if it contained any unknown words. If it does, and we're
4168 * not in "silent" mode, flag the error and then give the user a
4169 * chance to use "oops" to correct the problem. If we're in silent
4170 * mode, don't display an error and don't allow interactive
4171 * correction via "oops."
4172 *
4173 * It is possible that the unknown word is not in the input list,
4174 * but in the user's response to an interactive disambiguation
4175 * query. This is why we must check to see if the unknown word is
4176 * in the original input list or not.
4177 */
4178 if (err == VOCERR(2) && ctx_copy.voccxunknown != 0 && !silent)
4179 {
4180 char *unk;
4181 int unk_idx;
4182 char *rpl_text;
4183
4184 /*
4185 * forget we have unknown words, since we're going to handle
4186 * them now
4187 */
4188 ctx_copy.voccxunknown = 0;
4189
4190 /*
4191 * find the unknown word - look up each word until we find one
4192 * that's not in the dictionary
4193 */
4194 for (i = 0, unk = 0 ; cmd[i] != 0 ; ++i)
4195 {
4196 int t;
4197
4198 /*
4199 * get this word's type - if the word has no type, it's an
4200 * unknown word
4201 */
4202 t = voc_lookup_type(ctx, cmd[i], strlen(cmd[i]), TRUE);
4203 if (t == 0)
4204 {
4205 /* this is it - note it and stop searching */
4206 unk_idx = i;
4207 unk = cmd[i];
4208 break;
4209 }
4210 }
4211
4212 /*
4213 * if we didn't find any unknown words, assume the first word
4214 * was unknown
4215 */
4216 if (unk == 0)
4217 {
4218 unk_idx = 0;
4219 unk = cmd[0];
4220 }
4221
4222 /* display an error, and read a new command */
4223 rpl_text = voc_read_oops(&ctx_copy, oopsbuf, VOCBUFSIZ, unk);
4224
4225 /*
4226 * if they didn't respond with "oops," treat the response as a
4227 * brand new command to replace the current command
4228 */
4229 if (rpl_text == 0)
4230 {
4231 /*
4232 * it's a replacement command - set the redo flag to
4233 * indicate that we should process the replacement command
4234 */
4235 ctx_copy.voccxredo = TRUE;
4236
4237 /* copy the response into the command buffer */
4238 strcpy(cmdbuf, oopsbuf);
4239 }
4240 else
4241 {
4242 /* indicate the correction via the result code */
4243 err = VOCERR(45);
4244
4245 /*
4246 * Build the new command string. The new command string
4247 * consists of all of the tokens up to the unknown token,
4248 * then the replacement text, then all of the remaining
4249 * tokens.
4250 */
4251 for (p = cmdbuf, i = 0 ; cmd[i] != 0 ; ++i)
4252 {
4253 size_t needed;
4254
4255 /* figure the size needs for this token */
4256 if (i == unk_idx)
4257 {
4258 /* we need to insert the replacement text */
4259 needed = strlen(rpl_text);
4260 }
4261 else
4262 {
4263 /* we need to insert this token string */
4264 needed = strlen(cmd[i]);
4265 }
4266
4267 /*
4268 * if more tokens follow, we need a space after the
4269 * replacement text to separate it from what follows
4270 */
4271 if (cmd[i+1] != 0 && needed != 0)
4272 needed += 1;
4273
4274 /* leave room for null termination */
4275 needed += 1;
4276
4277 /* if we don't have room for this token, stop now */
4278 if (needed > (size_t)(VOCBUFSIZ - (p - cmdbuf)))
4279 break;
4280
4281 /*
4282 * if we've reached the unknown token, insert the
4283 * replacement text; otherwise, insert this token
4284 */
4285 if (i == unk_idx)
4286 {
4287 /* insert the replacement text */
4288 strcpy(p, rpl_text);
4289 }
4290 else if (*cmd[i] == '"')
4291 {
4292 char *p1;
4293 char qu;
4294
4295 /*
4296 * Scan the quoted string for embedded double quotes
4297 * - if it has any, use single quotes as the
4298 * delimiter; otherwise, use double quotes as the
4299 * delimiter. Note that we ignore the first and
4300 * last characters in the string, since these are
4301 * always the delimiting double quotes in the
4302 * original token text.
4303 */
4304 for (qu = '"', p1 = cmd[i] + 1 ;
4305 *p1 != '\0' && *(p1 + 1) != '\0' ; ++p1)
4306 {
4307 /* check for an embedded double quote */
4308 if (*p1 == '"')
4309 {
4310 /* switch to single quotes as delimiters */
4311 qu = '\'';
4312
4313 /* no need to look any further */
4314 break;
4315 }
4316 }
4317
4318 /* add the open quote */
4319 *p++ = qu;
4320
4321 /*
4322 * add the text, leaving out the first and last
4323 * characters (which are the original quotes)
4324 */
4325 if (strlen(cmd[i]) > 2)
4326 {
4327 memcpy(p, cmd[i] + 1, strlen(cmd[i]) - 2);
4328 p += strlen(cmd[i]) - 2;
4329 }
4330
4331 /* add the closing quote */
4332 *p++ = qu;
4333
4334 /* null-terminate here so we don't skip any further */
4335 *p = '\0';
4336 }
4337 else
4338 {
4339 /* copy this word */
4340 strcpy(p, cmd[i]);
4341 }
4342
4343 /* move past this token */
4344 p += strlen(p);
4345
4346 /* add a space if another token follows */
4347 if (cmd[i+1] != 0)
4348 *p++ = ' ';
4349 }
4350
4351 /* null-terminate the replacement text */
4352 *p = '\0';
4353 }
4354 }
4355
4356 /*
4357 * Count the objects. An object list is returned only on success or
4358 * VOCERR(44), which indicates that the list is still ambiguous.
4359 */
4360 if (err == 0 || err == VOCERR(44))
4361 {
4362 /* count the objects in the list */
4363 for (i = 0 ; outlist[i].vocolobj != MCMONINV ; ++i) ;
4364 objcnt = i;
4365 }
4366 else
4367 {
4368 /* there's nothing in the list */
4369 objcnt = 0;
4370 }
4371
4372 /* figure out how much space we need for the objects */
4373 lstsiz = (1+2) * objcnt;
4374
4375 /* add space for the first element, which contains the status code */
4376 lstsiz += (1 + 4);
4377
4378 /* if there's a new command string, we'll store it, so make room */
4379 if (ctx_copy.voccxredo || err == VOCERR(45))
4380 {
4381 /*
4382 * add space for the type prefix (1), length prefix (2), and the
4383 * string bytes (with no null terminator, of course)
4384 */
4385 lstsiz += (1 + 2 + strlen(cmdbuf));
4386
4387 /*
4388 * if we're retrying due to the redo flag, always return the
4389 * RETRY error code, regardless of what caused us to retry the
4390 * command
4391 */
4392 if (ctx_copy.voccxredo)
4393 err = VOCERR(43);
4394 }
4395
4396 /* push a list with space for the objects */
4397 lstp = voc_push_list_siz(ctx, lstsiz);
4398
4399 /* store the status code in the first element */
4400 *lstp++ = DAT_NUMBER;
4401 oswp4s(lstp, err);
4402 lstp += 4;
4403
4404 /* store the remainder of the list */
4405 if (err == 0 || err == VOCERR(44))
4406 {
4407 /* fill in the list with the objects */
4408 for (i = 0 ; i < objcnt ; ++i)
4409 {
4410 /* set this element */
4411 *lstp++ = DAT_OBJECT;
4412 oswp2(lstp, outlist[i].vocolobj);
4413 lstp += 2;
4414 }
4415 }
4416 else if (ctx_copy.voccxredo || err == VOCERR(45))
4417 {
4418 uint len;
4419
4420 /* there's a new command - return it as the second element */
4421 *lstp++ = DAT_SSTRING;
4422
4423 /* store the length */
4424 len = strlen(cmdbuf);
4425 oswp2(lstp, len + 2);
4426 lstp += 2;
4427
4428 /* store the string */
4429 memcpy(lstp, cmdbuf, len);
4430 }
4431
4432 /* leave the stack frame */
4433 voc_leave(ctx, save_sp);
4434 }
4435
4436
4437 /* ------------------------------------------------------------------------ */
4438 /*
4439 * TADS program code interface - replace the current command line with a
4440 * new string, aborting the current command.
4441 */
4442 void voc_parse_replace_cmd(voccxdef *ctx)
4443 {
4444 runcxdef *rcx = ctx->voccxrun;
4445 uchar *p;
4446 uint len;
4447
4448 /* get the string */
4449 p = runpopstr(rcx);
4450
4451 /* read and skip the length prefix */
4452 len = osrp2(p) - 2;
4453 p += 2;
4454
4455 /* make sure it fits in the redo buffer - truncate it if necessary */
4456 if (len + 1 > VOCBUFSIZ)
4457 len = VOCBUFSIZ - 1;
4458
4459 /* copy the string and null-terminate it */
4460 memcpy(ctx->voccxredobuf, p, len);
4461 ctx->voccxredobuf[len] = '\0';
4462
4463 /* set the "redo" flag so that we execute what's in the buffer */
4464 ctx->voccxredo = TRUE;
4465
4466 /* abort the current command so that we start anew with the replacement */
4467 errsig(ctx->voccxerr, ERR_RUNABRT);
4468 }
4469
4470
4471 /* ------------------------------------------------------------------------ */
4472 /*
4473 * This routine gets an actor, which is just a single object reference at
4474 * the beginning of a sentence. We return 0 if we fail to find an actor;
4475 * since this can be either a harmless or troublesome condition, we must
4476 * return additional information. The method used to return back ERROR/OK
4477 * is to set *next != cur if there is an error, *next == cur if not. So,
4478 * getting back (objdef*)0 means that you should check *next. If the return
4479 * value is nonzero, then that object is the actor.
4480 */
4481 static objnum vocgetactor(voccxdef *ctx, char *cmd[], int typelist[],
4482 int cur, int *next, char *cmdbuf)
4483 {
4484 int l;
4485 vocoldef *nounlist;
4486 vocoldef *actlist;
4487 int cnt;
4488 uchar *save_sp;
4489 prpnum valprop, verprop;
4490
4491 voc_enter(ctx, &save_sp);
4492 VOC_MAX_ARRAY(ctx, vocoldef, nounlist);
4493 VOC_MAX_ARRAY(ctx, vocoldef, actlist);
4494
4495 *next = cur; /* assume no error will occur */
4496 cnt = vocchknoun(ctx, cmd, typelist, cur, next, nounlist, TRUE);
4497 if (cnt > 0 && *next != -1 && cmd[*next] && vocspec(cmd[*next], VOCW_AND))
4498 {
4499 int have_unknown;
4500
4501 /* make a note as to whether the list contains an unknown word */
4502 have_unknown = ((nounlist[0].vocolflg & VOCS_UNKNOWN) != 0);
4503
4504 /*
4505 * If validActor is defined for any of the actors, use it;
4506 * otherwise, for compatibility with past versions, use the
4507 * takeVerb disambiguation mechanism. If we have a pronoun, we
4508 * can't decide yet how to do this, so presume that we'll use
4509 * the new mechanism and switch later if necessary.
4510 *
4511 * If we have don't have a valid object (which will be the case
4512 * for a pronoun), we can't decide until we get into the
4513 * disambiguation process, so presume we'll use validActor for
4514 * now.
4515 */
4516 verprop = PRP_VERACTOR;
4517 if (nounlist[0].vocolobj == MCMONINV
4518 || objgetap(ctx->voccxmem, nounlist[0].vocolobj, PRP_VALIDACTOR,
4519 (objnum *)0, FALSE))
4520 valprop = PRP_VALIDACTOR;
4521 else
4522 valprop = PRP_VALIDDO;
4523
4524 /* disambiguate it using the selected properties */
4525 if (vocdisambig(ctx, actlist, nounlist, PRP_DODEFAULT, valprop,
4526 verprop, cmd, MCMONINV, ctx->voccxme,
4527 ctx->voccxvtk, MCMONINV, cmdbuf, FALSE))
4528 {
4529 /*
4530 * if we have an unknown word in the list, assume for the
4531 * moment that this isn't an actor phrase after all, but a
4532 * verb
4533 */
4534 if (have_unknown)
4535 {
4536 /* indicate that we didn't find a noun phrase syntactically */
4537 *next = cur;
4538 }
4539
4540 /* return no actor */
4541 VOC_RETVAL(ctx, save_sp, MCMONINV);
4542 }
4543
4544 if ((l = voclistlen(actlist)) > 1)
4545 {
4546 vocerr(ctx, VOCERR(12),
4547 "You can only speak to one person at a time.");
4548 *next = cur + 1; /* error flag - return invalid but next!=cur */
4549 VOC_RETVAL(ctx, save_sp, MCMONINV);
4550 }
4551 else if (l == 0)
4552 return(MCMONINV);
4553
4554 if (cmd[*next] && vocspec(cmd[*next], VOCW_AND))
4555 {
4556 ++(*next);
4557 VOC_RETVAL(ctx, save_sp, actlist[0].vocolobj);
4558 }
4559 }
4560 if (cnt < 0)
4561 {
4562 /* error - make *next != cur */
4563 *next = cur + 1;
4564 }
4565 else
4566 *next = cur; /* no error condition, but nothing found */
4567
4568 VOC_RETVAL(ctx, save_sp, MCMONINV); /* so return invalid and *next == cur */
4569 }
4570
4571 /* figure out how many objects are in an object list */
4572 int voclistlen(vocoldef *lst)
4573 {
4574 int i;
4575
4576 for (i = 0 ; lst->vocolobj != MCMONINV || lst->vocolflg != 0 ;
4577 ++lst, ++i) ;
4578 return(i);
4579 }
4580
4581 /*
4582 * check access - evaluates cmdVerb.verprop(actor, obj, seqno), and
4583 * returns whatever it returns. The seqno parameter is used for special
4584 * cases, such as "ask", when the validation routine wishes to return
4585 * "true" on the first object and "nil" on all subsequent objects which
4586 * correspond to a particular noun phrase. We expect to be called with
4587 * seqno==0 on the first object, non-zero on others; we will pass
4588 * seqno==1 on the first object to the validation property, higher on
4589 * subsequent objects, to maintain consistency with the TADS language
4590 * convention of indexing from 1 up (as seen by the user in indexing
4591 * functions). Note that if we're checking an actor, we'll just call
4592 * obj.validActor() for the object itself (not the verb).
4593 */
4594 int vocchkaccess(voccxdef *ctx, objnum obj, prpnum verprop,
4595 int seqno, objnum cmdActor, objnum cmdVerb)
4596 {
4597 /*
4598 * special case: the special "string" and "number" objects are
4599 * always accessible
4600 */
4601 if (obj == ctx->voccxstr || obj == ctx->voccxnum)
4602 return TRUE;
4603
4604 /*
4605 * If the access method is validActor, make sure the object in fact
4606 * has a validActor method defined; if it doesn't, we must be
4607 * running a game from before validActor's invention, so use the old
4608 * ValidXo mechanism instead.
4609 */
4610 if (verprop == PRP_VALIDACTOR)
4611 {
4612 /* checking an actor - check to see if ValidActor is defined */
4613 if (objgetap(ctx->voccxmem, obj, PRP_VALIDACTOR, (objnum *)0, FALSE))
4614 {
4615 /* ValidActor is present - call ValidActor in the object itself */
4616 runppr(ctx->voccxrun, obj, verprop, 0);
4617
4618 /* return the result */
4619 return runpoplog(ctx->voccxrun);
4620 }
4621 else
4622 {
4623 /* there's no ValidActor - call ValidXo in the "take" verb */
4624 cmdVerb = ctx->voccxvtk;
4625 verprop = PRP_VALIDDO;
4626 }
4627 }
4628
4629 /* call ValidXo in the verb */
4630 runpnum(ctx->voccxrun, (long)(seqno + 1));
4631 runpobj(ctx->voccxrun, obj);
4632 runpobj(ctx->voccxrun,
4633 (objnum)(cmdActor == MCMONINV ? ctx->voccxme : cmdActor));
4634 runppr(ctx->voccxrun, cmdVerb, verprop, 3);
4635
4636 /* return the result */
4637 return runpoplog(ctx->voccxrun);
4638 }
4639
4640 /* ask game if object is visible to the actor */
4641 int vocchkvis(voccxdef *ctx, objnum obj, objnum cmdActor)
4642 {
4643 runpobj(ctx->voccxrun,
4644 (objnum)(cmdActor == MCMONINV ? ctx->voccxme : cmdActor));
4645 runppr(ctx->voccxrun, obj, (prpnum)PRP_ISVIS, 1);
4646 return(runpoplog(ctx->voccxrun));
4647 }
4648
4649 /* set {numObj | strObj}.value, as appropriate */
4650 void vocsetobj(voccxdef *ctx, objnum obj, dattyp typ, void *val,
4651 vocoldef *inobj, vocoldef *outobj)
4652 {
4653 *outobj = *inobj;
4654 outobj->vocolobj = obj;
4655 objsetp(ctx->voccxmem, obj, PRP_VALUE, typ, val, ctx->voccxundo);
4656 }
4657
4658 /* set up a vocoldef */
4659 static void vocout(vocoldef *outobj, objnum obj, int flg,
4660 char *fst, char *lst)
4661 {
4662 outobj->vocolobj = obj;
4663 outobj->vocolflg = flg;
4664 outobj->vocolfst = fst;
4665 outobj->vocollst = lst;
4666 }
4667
4668 /*
4669 * Generate an appropriate error message saying that the objects in the
4670 * command are visible, but can't be used with the command for some
4671 * reason. Use the cantReach method of the verb (the new way), or if
4672 * there is no cantReach in the verb, of each object in the list.
4673 */
4674 void vocnoreach(voccxdef *ctx, objnum *list1, int cnt,
4675 objnum actor, objnum verb, objnum prep, prpnum defprop,
4676 int show_multi_prefix,
4677 int multi_flags, int multi_base_index, int multi_total_count)
4678 {
4679 /* see if the verb has a cantReach method - use it if so */
4680 if (objgetap(ctx->voccxmem, verb, PRP_NOREACH, (objnum *)0, FALSE))
4681 {
4682 /* push arguments: (actor, dolist, iolist, prep) */
4683 runpobj(ctx->voccxrun, prep);
4684 if (defprop == PRP_DODEFAULT)
4685 {
4686 runpnil(ctx->voccxrun);
4687 voc_push_objlist(ctx, list1, cnt);
4688 }
4689 else
4690 {
4691 voc_push_objlist(ctx, list1, cnt);
4692 runpnil(ctx->voccxrun);
4693 }
4694 runpobj(ctx->voccxrun, actor);
4695
4696 /* invoke the method in the verb */
4697 runppr(ctx->voccxrun, verb, PRP_NOREACH, 4);
4698 }
4699 else
4700 {
4701 int i;
4702
4703 /* use the old way - call obj.cantReach() for each object */
4704 for (i = 0 ; i < cnt ; ++i)
4705 {
4706 /*
4707 * display this object's name if there's more than one, so
4708 * that the player can tell to which object this message
4709 * applies
4710 */
4711 voc_multi_prefix(ctx, list1[i], show_multi_prefix, multi_flags,
4712 multi_base_index + i, multi_total_count);
4713
4714 /* call cantReach on the object */
4715 runpobj(ctx->voccxrun,
4716 (objnum)(actor == MCMONINV ? ctx->voccxme : actor));
4717 runppr(ctx->voccxrun, list1[i], (prpnum)PRP_NOREACH, 1);
4718 tioflush(ctx->voccxtio);
4719 }
4720 }
4721 }
4722
4723 /*
4724 * Get the specialWords string for a given special word entry. Returns
4725 * the first string if multiple strings are defined for the entry.
4726 */
4727 static void voc_get_spec_str(voccxdef *ctx, char vocw_id,
4728 char *buf, size_t buflen,
4729 const char *default_name)
4730 {
4731 int found;
4732
4733 /* presume we won't find it */
4734 found = FALSE;
4735
4736 /* if there's a special word list, search it for this entry */
4737 if (ctx->voccxspp != 0)
4738 {
4739 char *p;
4740 char *endp;
4741 size_t len;
4742
4743 /* find appropriate user-defined word in specialWords list */
4744 for (p = ctx->voccxspp, endp = p + ctx->voccxspl ; p < endp ; )
4745 {
4746 /* if this one matches, get its first word */
4747 if (*p++ == vocw_id)
4748 {
4749 /* note that we found it */
4750 found = TRUE;
4751
4752 /*
4753 * get the length, and limit it to the buffer size,
4754 * leaving room for null termination
4755 */
4756 len = *p++;
4757 if (len + 1 > buflen)
4758 len = buflen - 1;
4759
4760 /* copy it and null-terminate the string */
4761 memcpy(buf, p, len);
4762 buf[len] = '\0';
4763
4764 /* we found it - no need to look any further */
4765 break;
4766 }
4767
4768 /*
4769 * move on to the next one - skip the length prefix plus the
4770 * length
4771 */
4772 p += *p + 1;
4773 }
4774 }
4775
4776 /* if we didn't find it, use the default */
4777 if (!found)
4778 {
4779 strncpy(buf, default_name, (size_t)buflen);
4780 buf[buflen - 1] = '\0';
4781 }
4782 }
4783
4784 /* set it/him/her */
4785 static int vocsetit(voccxdef *ctx, objnum obj, int accprop,
4786 objnum actor, objnum verb, objnum prep,
4787 vocoldef *outobj, char *default_name, char vocw_id,
4788 prpnum defprop, int silent)
4789 {
4790 if (obj == MCMONINV || !vocchkaccess(ctx, obj, (prpnum)accprop,
4791 0, actor, verb))
4792 {
4793 char nambuf[40];
4794
4795 /* get the display name for this specialWords entry */
4796 voc_get_spec_str(ctx, vocw_id, nambuf, sizeof(nambuf), default_name);
4797
4798 /* show the error if appropriate */
4799 if (!silent)
4800 {
4801 /* use 'noreach' if possible, otherwise use a default message */
4802 if (obj == MCMONINV)
4803 vocerr(ctx, VOCERR(13),
4804 "I don't know what you're referring to with '%s'.",
4805 nambuf);
4806 else
4807 vocnoreach(ctx, &obj, 1, actor, verb, prep,
4808 defprop, FALSE, 0, 0, 1);
4809 }
4810
4811 /* indicate that the antecedent is inaccessible */
4812 return VOCERR(13);
4813 }
4814
4815 /* set the object */
4816 vocout(outobj, obj, 0, default_name, default_name);
4817 return 0;
4818 }
4819
4820 /*
4821 * Get a new numbered object, given a number. This is used for objects
4822 * that define '#' as one of their adjectives; we call the object,
4823 * asking it to create an object with a particular number. The object
4824 * can return nil, in which case we'll reject the command.
4825 */
4826 static objnum voc_new_num_obj(voccxdef *ctx, objnum objn,
4827 objnum actor, objnum verb,
4828 long num, int plural)
4829 {
4830 /* push the number - if we need a plural object, use nil instead */
4831 if (plural)
4832 runpnil(ctx->voccxrun);
4833 else
4834 runpnum(ctx->voccxrun, num);
4835
4836 /* push the other arguments and call the method */
4837 runpobj(ctx->voccxrun, verb);
4838 runpobj(ctx->voccxrun, actor);
4839 runppr(ctx->voccxrun, objn, PRP_NEWNUMOBJ, 3);
4840
4841 /* if it was rejected, return an invalid object, else return the object */
4842 if (runtostyp(ctx->voccxrun) == DAT_NIL)
4843 {
4844 rundisc(ctx->voccxrun);
4845 return MCMONINV;
4846 }
4847 else
4848 return runpopobj(ctx->voccxrun);
4849 }
4850
4851 /* check if an object defines the special adjective '#' */
4852 static int has_gen_num_adj(voccxdef *ctx, objnum objn)
4853 {
4854 vocwdef *v;
4855 vocseadef search_ctx;
4856
4857 /* scan the list of objects defined '#' as an adjective */
4858 for (v = vocffw(ctx, "#", 1, (char *)0, 0, PRP_ADJ, &search_ctx) ;
4859 v ; v = vocfnw(ctx, &search_ctx))
4860 {
4861 /* if this is the object, return positive indication */
4862 if (v->vocwobj == objn)
4863 return TRUE;
4864 }
4865
4866 /* didn't find it */
4867 return FALSE;
4868 }
4869
4870
4871 /* ------------------------------------------------------------------------ */
4872 /*
4873 * Call the deepverb's disambigDobj or disambigIobj method to perform
4874 * game-controlled disambiguation.
4875 */
4876 static int voc_disambig_hook(voccxdef *ctx, objnum verb, objnum actor,
4877 objnum prep, objnum otherobj,
4878 prpnum accprop, prpnum verprop,
4879 objnum *objlist, uint *flags, int *objcount,
4880 char *firstwrd, char *lastwrd,
4881 int num_wanted, int is_ambig, char *resp,
4882 int silent)
4883 {
4884 runcxdef *rcx = ctx->voccxrun;
4885 prpnum call_prop;
4886 runsdef val;
4887 uchar *lstp;
4888 uint lstsiz;
4889 int ret;
4890 int i;
4891
4892 /* check for actor disambiguation */
4893 if (verprop == PRP_VERACTOR)
4894 {
4895 /* do nothing on actor disambiguation */
4896 return VOC_DISAMBIG_CONT;
4897 }
4898
4899 /* figure out whether this is a dobj method or an iobj method */
4900 call_prop = (accprop == PRP_VALIDDO ? PRP_DISAMBIGDO : PRP_DISAMBIGIO);
4901
4902 /* if the method isn't defined, we can skip this entirely */
4903 if (objgetap(ctx->voccxmem, verb, call_prop, (objnum *)0, FALSE) == 0)
4904 return VOC_DISAMBIG_CONT;
4905
4906 /* push the "silent" flag */
4907 val.runstyp = (silent ? DAT_TRUE : DAT_NIL);
4908 runpush(rcx, val.runstyp, &val);
4909
4910 /* push the "is_ambiguous" flag */
4911 val.runstyp = (is_ambig ? DAT_TRUE : DAT_NIL);
4912 runpush(rcx, val.runstyp, &val);
4913
4914 /* push the "numWanted" count */
4915 runpnum(rcx, num_wanted);
4916
4917 /* push the flag list */
4918 voc_push_numlist(ctx, flags, *objcount);
4919
4920 /* push the object list */
4921 voc_push_objlist(ctx, objlist, *objcount);
4922
4923 /* push the word list */
4924 voc_push_strlist(ctx, firstwrd, lastwrd);
4925
4926 /* push the verification property */
4927 val.runstyp = DAT_PROPNUM;
4928 val.runsv.runsvprp = verprop;
4929 runpush(rcx, DAT_PROPNUM, &val);
4930
4931 /* push the other object */
4932 runpobj(rcx, otherobj);
4933
4934 /* push the preposition and the actor objects */
4935 runpobj(rcx, prep);
4936 runpobj(rcx, actor);
4937
4938 /* call the method */
4939 runppr(rcx, verb, call_prop, 10);
4940
4941 /* check the return value */
4942 switch(runtostyp(rcx))
4943 {
4944 case DAT_LIST:
4945 /* get the list */
4946 lstp = runpoplst(rcx);
4947
4948 /* read the list size prefix */
4949 lstsiz = osrp2(lstp) - 2;
4950 lstp += 2;
4951
4952 /* check for the status code */
4953 if (lstsiz > 0 && *lstp == DAT_NUMBER)
4954 {
4955 /* get the status code */
4956 ret = osrp4s(lstp+1);
4957
4958 /* skip the element */
4959 lstadv(&lstp, &lstsiz);
4960 }
4961 else
4962 {
4963 /* there's no status code - assume CONTINUE */
4964 ret = VOC_DISAMBIG_CONT;
4965 }
4966
4967 /* check for a PARSE_RESP return */
4968 if (ret == VOC_DISAMBIG_PARSE_RESP)
4969 {
4970 /* the second element is the string */
4971 if (*lstp == DAT_SSTRING)
4972 {
4973 uint len;
4974
4975 /* get the length, and limit it to our buffer size */
4976 len = osrp2(lstp+1) - 2;
4977 if (len > VOCBUFSIZ - 1)
4978 len = VOCBUFSIZ - 1;
4979
4980 /* copy the string into the caller's buffer */
4981 memcpy(resp, lstp+3, len);
4982 resp[len] = '\0';
4983 }
4984 else
4985 {
4986 /* there's no string - ignore it */
4987 ret = VOC_DISAMBIG_CONT;
4988 }
4989 }
4990 else
4991 {
4992 /* store the object list in the caller's list */
4993 for (i = 0 ; lstsiz > 0 && i < VOCMAXAMBIG-1 ; ++i)
4994 {
4995 /* get this object */
4996 if (*lstp == DAT_OBJECT)
4997 objlist[i] = osrp2(lstp+1);
4998 else
4999 objlist[i] = MCMONINV;
5000
5001 /* skip the list entry */
5002 lstadv(&lstp, &lstsiz);
5003
5004 /* check for flags */
5005 if (lstsiz > 0 && *lstp == DAT_NUMBER)
5006 {
5007 /* store the flags */
5008 flags[i] = (int)osrp4(lstp+1);
5009
5010 /* skip the flags elements */
5011 lstadv(&lstp, &lstsiz);
5012 }
5013 else
5014 {
5015 /* no flags - use zero by default */
5016 flags[i] = 0;
5017 }
5018 }
5019
5020 /* store a terminator at the end of the list */
5021 objlist[i] = MCMONINV;
5022 flags[i] = 0;
5023
5024 /* store the output count for the caller */
5025 *objcount = i;
5026 }
5027
5028 /* return the result */
5029 return ret;
5030
5031 case DAT_NUMBER:
5032 /* get the status code */
5033 ret = runpopnum(rcx);
5034
5035 /* ignore raw PARSE_RESP codes, since they need to return a string */
5036 if (ret == VOC_DISAMBIG_PARSE_RESP)
5037 ret = VOC_DISAMBIG_CONT;
5038
5039 /* return the status */
5040 return ret;
5041
5042 default:
5043 /* treat anything else as CONTINUE */
5044 rundisc(rcx);
5045 return VOC_DISAMBIG_CONT;
5046 }
5047 }
5048
5049
5050 /* ------------------------------------------------------------------------ */
5051 /*
5052 * Prune a list of matches by keeping only the matches without the given
5053 * flag value, if we have a mix of entries with and without the flag.
5054 * This is a service routine for voc_prune_matches.
5055 *
5056 * The flag indicates a lower quality of matching, so this routine can
5057 * be used to reduce ambiguity by keeping only the best-quality matches
5058 * when matches of mixed quality are present.
5059 */
5060 static int voc_remove_objs_with_flag(voccxdef *ctx,
5061 objnum *list, uint *flags, int cnt,
5062 int flag_to_remove)
5063 {
5064 int i;
5065 int flag_cnt;
5066 int special_cnt;
5067
5068 /* first, count the number of objects with the flag */
5069 for (i = 0, flag_cnt = special_cnt = 0 ; i < cnt ; ++i)
5070 {
5071 /* if this object exhibits the flag, count it */
5072 if ((flags[i] & flag_to_remove) != 0)
5073 ++flag_cnt;
5074
5075 /* if it's numObj or strObj, count it separately */
5076 if (list[i] == ctx->voccxnum || list[i] == ctx->voccxstr)
5077 ++special_cnt;
5078 }
5079
5080 /*
5081 * If all of the objects didn't have the flag, omit the ones that
5082 * did, so that we reduce the ambiguity to those without the flag.
5083 * Don't include the special objects (numObj and strObj) in the
5084 * count, since they will never have any of these flags set.
5085 */
5086 if (flag_cnt != 0 && flag_cnt < cnt - special_cnt)
5087 {
5088 int dst;
5089
5090 /*
5091 * Remove the flagged objects. Note that we can make this
5092 * adjustment to the arrays in place, because they can only
5093 * shrink - there's no need to make an extra temporary copy.
5094 */
5095 for (i = 0, dst = 0 ; i < cnt ; ++i)
5096 {
5097 /*
5098 * If this one doesn't have the flag, keep it. Always keep
5099 * the special objects (numObj and strObj).
5100 */
5101 if ((flags[i] & flag_to_remove) == 0
5102 || list[i] == ctx->voccxnum
5103 || list[i] == ctx->voccxstr)
5104 {
5105 /* copy this one to the output location */
5106 list[dst] = list[i];
5107 flags[dst] = flags[i];
5108
5109 /* count the new element of the output */
5110 ++dst;
5111 }
5112 }
5113
5114 /* set the updated count */
5115 cnt = dst;
5116 list[cnt] = MCMONINV;
5117 }
5118
5119 /* return the new count */
5120 return cnt;
5121 }
5122
5123 /*
5124 * Prune a list of matches by keeping only the best matches when matches
5125 * of different qualities are present.
5126 *
5127 * If we have a mix of objects matching noun phrases that end in
5128 * adjectives and phrases ending in nouns with the same words, remove
5129 * those elements that end in adjectives, keeping only the better
5130 * matches that end in nouns.
5131 *
5132 * If we have a mix of objects where the words match exactly, and others
5133 * where the words are only leading substrings of longer dictionary
5134 * words, keep only the exact matches.
5135 *
5136 * Returns the number of elements in the result list.
5137 */
5138 static int voc_prune_matches(voccxdef *ctx,
5139 objnum *list, uint *flags, int cnt)
5140 {
5141 /* remove matches that end with an adjective */
5142 cnt = voc_remove_objs_with_flag(ctx, list, flags, cnt, VOCS_ENDADJ);
5143
5144 /* remove matches that use truncated words */
5145 cnt = voc_remove_objs_with_flag(ctx, list, flags, cnt, VOCS_TRUNC);
5146
5147 /* return the new list size */
5148 return cnt;
5149 }
5150
5151 /* ------------------------------------------------------------------------ */
5152 /*
5153 * Count indistinguishable items.
5154 *
5155 * If 'keep_all' is true, we'll keep all of the items, whether or not
5156 * some are indistinguishable from one another. If 'keep_all' is false,
5157 * we'll keep only one item from each set of indistinguishable items.
5158 */
5159 static int voc_count_diff(voccxdef *ctx, objnum *list, uint *flags, int *cnt,
5160 int keep_all)
5161 {
5162 int i;
5163 int diff_cnt;
5164
5165 /*
5166 * Presume all items will be distinguishable from one another. As
5167 * we scan the list for indistinguishable items, we'll decrement
5168 * this count each time we find an item that can't be distinguished
5169 * from another item.
5170 */
5171 diff_cnt = *cnt;
5172
5173 /*
5174 * Look for indistinguishable items.
5175 *
5176 * An object is distinguishable if it doesn't have the special
5177 * property marking it as one of a group of equivalent objects
5178 * (PRP_ISEQUIV), or if it has the property but there is no object
5179 * following it in the list which has the same immediate superclass.
5180 *
5181 * Note that we want to keep the duplicates if we're looking for
5182 * plurals, because the player is explicitly referring to all
5183 * matching objects.
5184 */
5185 for (i = 0 ; i < *cnt ; ++i)
5186 {
5187 /*
5188 * check to see if this object might have indistinguishable
5189 * duplicates - it must be marked with isEquiv for this to be
5190 * possible
5191 */
5192 runppr(ctx->voccxrun, list[i], PRP_ISEQUIV, 0);
5193 if (runpoplog(ctx->voccxrun))
5194 {
5195 int j;
5196 int dst;
5197 objnum sc;
5198
5199 /* get the superclass, if possible */
5200 sc = objget1sc(ctx->voccxmem, list[i]);
5201 if (sc == MCMONINV)
5202 continue;
5203
5204 /*
5205 * run through the remainder of the list, and remove any
5206 * duplicates of this item
5207 */
5208 for (j = i + 1, dst = i + 1 ; j < *cnt ; ++j)
5209 {
5210 /*
5211 * see if it matches our object - if not, keep it in the
5212 * list by copying it to our destination position
5213 */
5214 if (objget1sc(ctx->voccxmem, list[j]) != sc)
5215 {
5216 /* it's distinguishable - keep it */
5217 list[dst] = list[j];
5218 flags[dst++] = flags[j];
5219 }
5220 else
5221 {
5222 /*
5223 * This item is indistinguishable from the list[i].
5224 * First, reduce the count of different items.
5225 */
5226 --diff_cnt;
5227
5228 /*
5229 * Keep this object only if we're keeping all
5230 * redundant indistinguishable items.
5231 */
5232 if (keep_all)
5233 {
5234 /* keep all items -> keep this item */
5235 list[dst] = list[j];
5236 flags[dst++] = flags[j];
5237 }
5238 }
5239 }
5240
5241 /* adjust the count to reflect the updated list */
5242 *cnt = dst;
5243
5244 /* add a terminator */
5245 list[dst] = MCMONINV;
5246 flags[dst] = 0;
5247 }
5248 }
5249
5250 /* return the number of distinguishable items */
5251 return diff_cnt;
5252 }
5253
5254 /* ------------------------------------------------------------------------ */
5255 /*
5256 * vocdisambig - determines which nouns in a noun list apply. When this
5257 * is called, we must know the verb that we are processing, so we delay
5258 * disambiguation until quite late in the parsing of a sentence, opting
5259 * to keep all relevant information around until such time as we can
5260 * meaningfully disambiguate.
5261 *
5262 * This routine resolves any "all [except...]", "it", and "them"
5263 * references. We determine if all of the objects listed are accessible
5264 * (via verb.validDo, verb.validIo). We finally try to determine which
5265 * nouns apply when there are ambiguous nouns by using do.verDo<Verb>
5266 * and io.verIo<Verb>.
5267 */
5268 int vocdisambig(voccxdef *ctx, vocoldef *outlist, vocoldef *inlist,
5269 prpnum defprop, prpnum accprop, prpnum verprop,
5270 char *cmd[], objnum otherobj, objnum cmdActor,
5271 objnum cmdVerb, objnum cmdPrep, char *cmdbuf,
5272 int silent)
5273 {
5274 int inpos;
5275 int outpos;
5276 int listlen = voclistlen(inlist);
5277 int noreach = FALSE;
5278 prpnum listprop;
5279 uchar *save_sp;
5280 int old_unknown, old_lastunk;
5281 int err;
5282 int still_ambig;
5283
5284 voc_enter(ctx, &save_sp);
5285
5286 ERRBEGIN(ctx->voccxerr)
5287
5288 /* presume we will not leave any ambiguity in the result */
5289 still_ambig = FALSE;
5290
5291 /* loop through all of the objects in the input list */
5292 for (inpos = outpos = 0 ; inpos < listlen ; ++inpos)
5293 {
5294 /*
5295 * reset the stack to our entrypoint value, since our stack
5296 * variables are all temporary for a single iteration
5297 */
5298 voc_leave(ctx, save_sp);
5299 voc_enter(ctx, &save_sp);
5300
5301 if (inlist[inpos].vocolflg == VOCS_STR)
5302 {
5303 vocsetobj(ctx, ctx->voccxstr, DAT_SSTRING,
5304 inlist[inpos].vocolfst + 1,
5305 &inlist[inpos], &outlist[outpos]);
5306 ++outpos;
5307 }
5308 else if (inlist[inpos].vocolflg == VOCS_NUM)
5309 {
5310 long v1;
5311 char vbuf[4];
5312
5313 v1 = atol(inlist[inpos].vocolfst);
5314 oswp4s(vbuf, v1);
5315 vocsetobj(ctx, ctx->voccxnum, DAT_NUMBER, vbuf,
5316 &inlist[inpos], &outlist[outpos]);
5317 ++outpos;
5318 }
5319 else if (inlist[inpos].vocolflg == VOCS_IT ||
5320 (inlist[inpos].vocolflg == VOCS_THEM && ctx->voccxthc == 0))
5321 {
5322 err = vocsetit(ctx, ctx->voccxit, accprop, cmdActor,
5323 cmdVerb, cmdPrep, &outlist[outpos],
5324 inlist[inpos].vocolflg == VOCS_IT ? "it" : "them",
5325 (char)(inlist[inpos].vocolflg == VOCS_IT
5326 ? VOCW_IT : VOCW_THEM), defprop, silent);
5327 if (err != 0)
5328 goto done;
5329 ++outpos;
5330 }
5331 else if (inlist[inpos].vocolflg == VOCS_HIM)
5332 {
5333 err = vocsetit(ctx, ctx->voccxhim, accprop, cmdActor, cmdVerb,
5334 cmdPrep, &outlist[outpos], "him", VOCW_HIM,
5335 defprop, silent);
5336 if (err != 0)
5337 goto done;
5338 ++outpos;
5339 }
5340 else if (inlist[inpos].vocolflg == VOCS_HER)
5341 {
5342 err = vocsetit(ctx, ctx->voccxher, accprop, cmdActor, cmdVerb,
5343 cmdPrep, &outlist[outpos], "her", VOCW_HER,
5344 defprop, silent);
5345 if (err != 0)
5346 goto done;
5347 ++outpos;
5348 }
5349 else if (inlist[inpos].vocolflg == VOCS_THEM)
5350 {
5351 int i;
5352 int thempos = outpos;
5353 static char them_name[] = "them";
5354
5355 for (i = 0 ; i < ctx->voccxthc ; ++i)
5356 {
5357 if (outpos >= VOCMAXAMBIG)
5358 {
5359 if (!silent)
5360 vocerr(ctx, VOCERR(11),
5361 "You're referring to too many objects.");
5362 err = VOCERR(11);
5363 goto done;
5364 }
5365
5366 /* add object only if it's still accessible */
5367 if (vocchkaccess(ctx, ctx->voccxthm[i], accprop, 0,
5368 cmdActor, cmdVerb))
5369 {
5370 /* it's still accessible - add it to the list */
5371 vocout(&outlist[outpos++], ctx->voccxthm[i], VOCS_THEM,
5372 them_name, them_name);
5373 }
5374 else
5375 {
5376 /* it's not accessible - complain about it */
5377 vocnoreach(ctx, &ctx->voccxthm[i], 1,
5378 cmdActor, cmdVerb, cmdPrep,
5379 defprop, TRUE, VOCS_THEM, i, ctx->voccxthc);
5380 tioflush(ctx->voccxtio);
5381 }
5382 }
5383
5384 /* make sure we found at least one acceptable object */
5385 if (outpos == thempos)
5386 {
5387 if (!silent)
5388 vocerr(ctx, VOCERR(14),
5389 "I don't know what you're referring to.");
5390 err = VOCERR(14);
5391 goto done;
5392 }
5393 }
5394 else if (inlist[inpos].vocolflg == VOCS_ALL)
5395 {
5396 uchar *l;
5397 int exccnt = 0;
5398 int allpos = outpos;
5399 int k;
5400 uint len;
5401 static char all_name[] = "all";
5402 vocoldef *exclist;
5403 vocoldef *exclist2;
5404
5405 VOC_MAX_ARRAY(ctx, vocoldef, exclist);
5406 VOC_MAX_ARRAY(ctx, vocoldef, exclist2);
5407
5408 if (defprop != PRP_IODEFAULT)
5409 runpobj(ctx->voccxrun, otherobj);
5410 runpobj(ctx->voccxrun, cmdPrep);
5411 runpobj(ctx->voccxrun, cmdActor);
5412 runppr(ctx->voccxrun, cmdVerb, defprop,
5413 defprop == PRP_DODEFAULT ? 3 : 2);
5414
5415 if (runtostyp(ctx->voccxrun) == DAT_LIST)
5416 {
5417 l = runpoplst(ctx->voccxrun);
5418 len = osrp2(l) - 2;
5419 l += 2;
5420
5421 while (len)
5422 {
5423 /* add list element to output if it's an object */
5424 if (*l == DAT_OBJECT)
5425 vocout(&outlist[outpos++], (objnum)osrp2(l+1), 0,
5426 all_name, all_name);
5427
5428 /* move on to next list element */
5429 lstadv(&l, &len);
5430 }
5431
5432 vocout(&outlist[outpos], MCMONINV, 0, (char *)0, (char *)0);
5433 }
5434 else
5435 rundisc(ctx->voccxrun); /* discard non-list value */
5436
5437 /* if we didn't get anything, complain about it and quit */
5438 if (outpos <= allpos)
5439 {
5440 if (!silent)
5441 vocerr(ctx, VOCERR(15),
5442 "I don't see what you're referring to.");
5443 err = VOCERR(15);
5444 goto done;
5445 }
5446
5447 /* remove any items in "except" list */
5448 while (inlist[inpos + 1].vocolflg & VOCS_EXCEPT)
5449 {
5450 OSCPYSTRUCT(exclist[exccnt], inlist[++inpos]);
5451 exclist[exccnt++].vocolflg &= ~VOCS_EXCEPT;
5452 }
5453 exclist[exccnt].vocolobj = MCMONINV;
5454 exclist[exccnt].vocolflg = 0;
5455
5456 /* disambiguate "except" list */
5457 if (exccnt)
5458 {
5459 err = vocdisambig(ctx, exclist2, exclist, defprop, accprop,
5460 verprop, cmd, otherobj, cmdActor,
5461 cmdVerb, cmdPrep, cmdbuf, silent);
5462 if (err != 0)
5463 goto done;
5464
5465 exccnt = voclistlen(exclist2);
5466 for (k = 0 ; k < exccnt ; ++k)
5467 {
5468 int i;
5469 for (i = allpos ; i < outpos ; ++i)
5470 {
5471 if (outlist[i].vocolobj == exclist2[k].vocolobj)
5472 {
5473 int j;
5474 for (j = i ; j < outpos ; ++j)
5475 outlist[j].vocolobj = outlist[j+1].vocolobj;
5476 --i;
5477 --outpos;
5478 if (outpos <= allpos)
5479 {
5480 if (!silent)
5481 vocerr(ctx, VOCERR(15),
5482 "I don't see what you're referring to.");
5483 err = VOCERR(15);
5484 goto done;
5485 }
5486 }
5487 }
5488 }
5489 }
5490 }
5491 else /* we have a (possibly ambiguous) noun */
5492 {
5493 int lpos = inpos;
5494 int i = 0;
5495 int cnt;
5496 char *p;
5497 int cnt2, cnt3;
5498 int trying_again;
5499 int user_count = 0;
5500 objnum *cantreach_list;
5501 int unknown_count;
5502 int use_all_objs;
5503 objnum *list1;
5504 uint *flags1;
5505 objnum *list2;
5506 uint *flags2;
5507 objnum *list3;
5508 uint *flags3;
5509 char *usrobj;
5510 uchar *lstbuf;
5511 char *newobj;
5512 char *disnewbuf;
5513 char *disbuffer;
5514 char **diswordlist;
5515 int *distypelist;
5516 vocoldef *disnounlist;
5517 int dst;
5518
5519 VOC_MAX_ARRAY(ctx, objnum, list1);
5520 VOC_MAX_ARRAY(ctx, objnum, list2);
5521 VOC_MAX_ARRAY(ctx, objnum, list3);
5522 VOC_MAX_ARRAY(ctx, uint, flags1);
5523 VOC_MAX_ARRAY(ctx, uint, flags2);
5524 VOC_MAX_ARRAY(ctx, uint, flags3);
5525 VOC_MAX_ARRAY(ctx, vocoldef, disnounlist);
5526 VOC_STK_ARRAY(ctx, char, disnewbuf, VOCBUFSIZ);
5527 VOC_STK_ARRAY(ctx, char, disbuffer, 2*VOCBUFSIZ);
5528 VOC_STK_ARRAY(ctx, char *, diswordlist, VOCBUFSIZ);
5529 VOC_STK_ARRAY(ctx, int, distypelist, VOCBUFSIZ);
5530 VOC_STK_ARRAY(ctx, char, usrobj, VOCBUFSIZ);
5531 VOC_STK_ARRAY(ctx, char, newobj, VOCBUFSIZ);
5532 VOC_STK_ARRAY(ctx, uchar, lstbuf, 2 + VOCMAXAMBIG*3);
5533
5534 /* presume we won't resolve any unknown words */
5535 unknown_count = 0;
5536
5537 /*
5538 * Presume that we won't use all the objects that match
5539 * these words, since we normally want to try to find a
5540 * single, unambiguous match for a given singular noun
5541 * phrase. Under certain circumstances, we'll want to keep
5542 * all of the words that match the noun phrase, in which
5543 * case we'll set this flag accordingly.
5544 */
5545 use_all_objs = FALSE;
5546
5547 /*
5548 * go through the objects matching the current noun phrase
5549 * and add them into our list
5550 */
5551 while (inlist[lpos].vocolfst == inlist[inpos].vocolfst
5552 && lpos < listlen)
5553 {
5554 /* add this object to the list of nouns */
5555 list1[i] = inlist[lpos].vocolobj;
5556
5557 /*
5558 * note whether this object matched a plural, whether it
5559 * matched adjective-at-end usage, and whether it
5560 * matched a truncated dictionary word
5561 */
5562 flags1[i] = inlist[lpos].vocolflg
5563 & (VOCS_PLURAL | VOCS_ANY | VOCS_COUNT
5564 | VOCS_ENDADJ | VOCS_TRUNC);
5565
5566 /* if this is a valid object, count it */
5567 if (list1[i] != MCMONINV)
5568 ++i;
5569
5570 /* if there's a user count, note it */
5571 if ((inlist[lpos].vocolflg & VOCS_COUNT) != 0)
5572 user_count = atoi(inlist[lpos].vocolfst);
5573
5574 /* if an unknown word was involved, note it */
5575 if ((inlist[lpos].vocolflg & VOCS_UNKNOWN) != 0)
5576 ++unknown_count;
5577
5578 /* move on to the next entry */
5579 ++lpos;
5580 }
5581
5582 /* terminate the list */
5583 list1[i] = MCMONINV;
5584 cnt = i;
5585
5586 /*
5587 * If this noun phrase contained an unknown word, check to
5588 * see if the verb defines the parseUnknownXobj() method.
5589 * If so, call the method and check the result.
5590 */
5591 if (unknown_count > 0)
5592 {
5593 prpnum prp;
5594
5595 /*
5596 * figure out which method to call - use
5597 * parseUnknownDobj if we're disambiguating the direct
5598 * object, parseUnknownIobj for the indirect object
5599 */
5600 prp = (defprop == PRP_DODEFAULT
5601 ? PRP_PARSEUNKNOWNDOBJ : PRP_PARSEUNKNOWNIOBJ);
5602
5603 /* check if the verb defines this method */
5604 if (objgetap(ctx->voccxmem, cmdVerb, prp, (objnum *)0, FALSE))
5605 {
5606 uchar *lstp;
5607 uint lstlen;
5608
5609 /* trace the event for debugging */
5610 if (ctx->voccxflg & VOCCXFDBG)
5611 tioputs(ctx->voccxtio,
5612 "... unknown word: calling "
5613 "parseUnknownXobj\\n");
5614
5615 /* push the list of words in the noun phrase */
5616 voc_push_strlist(ctx, inlist[inpos].vocolfst,
5617 inlist[inpos].vocollst);
5618
5619 /* push the other arguments */
5620 runpobj(ctx->voccxrun, otherobj);
5621 runpobj(ctx->voccxrun, cmdPrep);
5622 runpobj(ctx->voccxrun, cmdActor);
5623
5624 /* call the method */
5625 runppr(ctx->voccxrun, cmdVerb, prp, 4);
5626
5627 /* see what they returned */
5628 switch(runtostyp(ctx->voccxrun))
5629 {
5630 case DAT_OBJECT:
5631 /*
5632 * use the object they returned as the match for
5633 * the noun phrase
5634 */
5635 list1[cnt++] = runpopobj(ctx->voccxrun);
5636
5637 /* terminate the new list */
5638 list1[cnt] = MCMONINV;
5639 break;
5640
5641 case DAT_LIST:
5642 /*
5643 * use the list of objects they returned as the
5644 * match for the noun phrase
5645 */
5646 lstp = runpoplst(ctx->voccxrun);
5647
5648 /* get the length of the list */
5649 lstlen = osrp2(lstp) - 2;
5650 lstp += 2;
5651
5652 /* run through the list's elements */
5653 while (lstlen != 0)
5654 {
5655 /* if this is an object, add it */
5656 if (*lstp == DAT_OBJECT
5657 && i < VOCMAXAMBIG)
5658 list1[cnt++] = osrp2(lstp+1);
5659
5660 /* move on to the next element */
5661 lstadv(&lstp, &lstlen);
5662 }
5663
5664 /*
5665 * Note that we want to use all of these objects
5666 * without disambiguation, since the game code
5667 * has explicitly said that this is the list
5668 * that matches the given noun phrase.
5669 */
5670 use_all_objs = TRUE;
5671
5672 /* terminate the new list */
5673 list1[cnt] = MCMONINV;
5674 break;
5675
5676 case DAT_TRUE:
5677 /*
5678 * A 'true' return value indicates that the
5679 * parseUnknownXobj routine has fully handled
5680 * the command. They don't want anything more
5681 * to be done with these words. Simply remove
5682 * the unknown words and continue with any other
5683 * words in the list.
5684 */
5685 rundisc(ctx->voccxrun);
5686
5687 /* we're done with this input phrase */
5688 continue;
5689
5690 default:
5691 /*
5692 * For anything else, use the default mechanism.
5693 * Simply return an error; since the "unknown
5694 * word" flag is set, we'll reparse the
5695 * sentence, this time rejecting unknown words
5696 * from the outset.
5697 *
5698 * Return error 2, since that's the generic "I
5699 * don't know the word..." error code.
5700 */
5701 rundisc(ctx->voccxrun);
5702 err = VOCERR(2);
5703 goto done;
5704 }
5705
5706 /*
5707 * If we made it this far, it means that they've
5708 * resolved the object for us, so we can consider
5709 * the previously unknown words to be known now.
5710 */
5711 ctx->voccxunknown -= unknown_count;
5712 }
5713 else
5714 {
5715 /* trace the event for debugging */
5716 if (ctx->voccxflg & VOCCXFDBG)
5717 tioputs(ctx->voccxtio,
5718 "... unknown word: no parseUnknownXobj - "
5719 "restarting parsing\\n");
5720
5721 /*
5722 * The verb doesn't define this method, so we should
5723 * use the traditional method; simply return
5724 * failure, and we'll reparse the sentence to reject
5725 * the unknown word in the usual fashion. Return
5726 * error 2, since that's the generic "I don't know
5727 * the word..." error code.
5728 */
5729 err = VOCERR(2);
5730 goto done;
5731 }
5732 }
5733
5734 /*
5735 * Use a new method to cut down on the time it will take to
5736 * iterate through the verprop's on all of those words.
5737 * We'll call the verb's validXoList method - it should
5738 * return a list containing all of the valid objects for the
5739 * verb (it's sort of a Fourier transform of validDo).
5740 * We'll intersect that list with the list we're about to
5741 * disambiguate, which should provide a list of objects that
5742 * are already qualified, in that validDo should return true
5743 * for every one of them.
5744 *
5745 * The calling sequence is:
5746 * verb.validXoList(actor, prep, otherobj)
5747 *
5748 * For reverse compatibility, if the return value is nil,
5749 * we use the old algorithm and consider all objects
5750 * that match the vocabulary. The return value must be
5751 * a list to be considered.
5752 *
5753 * If disambiguating the actor, skip this phase, since
5754 * we don't have a verb yet.
5755 */
5756 if (accprop != PRP_VALIDACTOR && cnt != 0)
5757 {
5758 if (defprop == PRP_DODEFAULT)
5759 listprop = PRP_VALDOLIST;
5760 else
5761 listprop = PRP_VALIOLIST;
5762
5763 /* push the arguments: the actor, prep, and other object */
5764 runpobj(ctx->voccxrun, otherobj);
5765 runpobj(ctx->voccxrun, cmdPrep);
5766 runpobj(ctx->voccxrun, cmdActor);
5767 runppr(ctx->voccxrun, cmdVerb, listprop, 3);
5768 if (runtostyp(ctx->voccxrun) == DAT_LIST)
5769 {
5770 uchar *l;
5771 uint len;
5772 int kept_numobj;
5773
5774 /* presume we won't keep numObj */
5775 kept_numobj = FALSE;
5776
5777 /* read the list length prefix, and skip it */
5778 l = runpoplst(ctx->voccxrun);
5779 len = osrp2(l) - 2;
5780 l += 2;
5781
5782 /*
5783 * For each element of the return value, see if
5784 * it's in list1. If so, copy the object into
5785 * list2, unless it's already in list2.
5786 */
5787 for (cnt2 = 0 ; len != 0 ; )
5788 {
5789 if (*l == DAT_OBJECT)
5790 {
5791 objnum o = osrp2(l+1);
5792
5793 for (i = 0 ; i < cnt ; ++i)
5794 {
5795 if (list1[i] == o)
5796 {
5797 int j;
5798
5799 /* check to see if o is already in list2 */
5800 for (j = 0 ; j < cnt2 ; ++j)
5801 if (list2[j] == o) break;
5802
5803 /* if o is not in list2 yet, add it */
5804 if (j == cnt2)
5805 {
5806 /* add it */
5807 list2[cnt2] = o;
5808 flags2[cnt2] = flags1[i];
5809 ++cnt2;
5810
5811 /*
5812 * if it's numObj, note that
5813 * we've already included it in
5814 * the output list, so that we
5815 * don't add it again later
5816 */
5817 if (o == ctx->voccxnum)
5818 kept_numobj = TRUE;
5819 }
5820 break;
5821 }
5822 }
5823 }
5824
5825 /* move on to next element */
5826 lstadv(&l, &len);
5827 }
5828
5829 /*
5830 * If the original list included numObj, keep it in
5831 * the accessible list for now - we consider numObj
5832 * to be always accessible. The noun phrase matcher
5833 * will include numObj whenever the player enters a
5834 * single number as a noun phrase, even when the
5835 * number matches an object. Note that we can skip
5836 * this special step if we already kept numObj in
5837 * the valid list.
5838 */
5839 if (!kept_numobj)
5840 {
5841 /* search the original list for numObj */
5842 for (i = 0 ; i < cnt ; ++i)
5843 {
5844 /* if this original entry is numObj, keep it */
5845 if (list1[i] == ctx->voccxnum)
5846 {
5847 /* keep it in the accessible list */
5848 list2[cnt2++] = ctx->voccxnum;
5849
5850 /* no need to look any further */
5851 break;
5852 }
5853 }
5854 }
5855
5856 /* copy list2 into list1 */
5857 memcpy(list1, list2, (size_t)(cnt2 * sizeof(list1[0])));
5858 memcpy(flags1, flags2, (size_t)cnt2 * sizeof(flags1[0]));
5859 cnt = cnt2;
5860 list1[cnt] = MCMONINV;
5861 }
5862 else
5863 rundisc(ctx->voccxrun);
5864 }
5865
5866 /*
5867 * Determine accessibility and visibility. First, limit
5868 * list1 to those objects that are visible OR accessible,
5869 * and limit list3 to those objects that are visible.
5870 */
5871 for (cnt = cnt3 = i = 0 ; list1[i] != MCMONINV ; ++i)
5872 {
5873 int is_vis;
5874 int is_acc;
5875
5876 /* determine if the object is visible */
5877 is_vis = vocchkvis(ctx, list1[i], cmdActor);
5878
5879 /* determine if it's accessible */
5880 is_acc = vocchkaccess(ctx, list1[i], accprop, i,
5881 cmdActor, cmdVerb);
5882
5883 /* keep items that are visible OR accessible in list1 */
5884 if (is_acc || is_vis)
5885 {
5886 list1[cnt] = list1[i];
5887 flags1[cnt] = flags1[i];
5888 ++cnt;
5889 }
5890
5891 /*
5892 * put items that are visible (regardless of whether or
5893 * not they're accessible) in list3
5894 */
5895 if (is_vis)
5896 {
5897 list3[cnt3] = list1[i];
5898 flags3[cnt3] = flags1[i];
5899 ++cnt3;
5900 }
5901 }
5902
5903 /*
5904 * If some of our accessible objects matched with an
5905 * adjective at the end of the noun phrase, and others
5906 * didn't (i.e., the others matched with a noun or plural at
5907 * the end of the noun phrase), eliminate the ones that
5908 * matched with an adjective at the end. Ending a noun
5909 * phrase with an adjective is really a kind of short-hand;
5910 * if we have matches for both the full name version (with a
5911 * noun at the end) and a short-hand version, we want to
5912 * discard the short-hand version so that we don't treat it
5913 * as ambiguous with the long-name version. Likewise, if we
5914 * have some exact matches and some truncations, keep only
5915 * the exact matches.
5916 */
5917 cnt = voc_prune_matches(ctx, list1, flags1, cnt);
5918 cnt3 = voc_prune_matches(ctx, list3, flags3, cnt3);
5919
5920 /*
5921 * Now, reduce list1 to objects that are accessible. The
5922 * reason for this multi-step process is to ensure that we
5923 * prune the list with respect to every object in scope
5924 * (visible or accessible for the verb), so that we get the
5925 * most sensible pruning behavior. This is more sensible
5926 * than pruning by accessibility only, because sometimes we
5927 * may have objects that are visible but are not accessible;
5928 * as far as the player is concerned, the visible objects
5929 * are part of the current location, so the player should be
5930 * able to refer to them regardless of whether they're
5931 * accessible.
5932 */
5933 for (dst = 0, i = 0 ; i < cnt ; ++i)
5934 {
5935 /* check this object for accessibility */
5936 if (vocchkaccess(ctx, list1[i], accprop, i,
5937 cmdActor, cmdVerb))
5938 {
5939 /* keep it in the final list */
5940 list1[dst] = list1[i];
5941 flags1[dst] = flags1[i];
5942
5943 /* count the new list entry */
5944 ++dst;
5945 }
5946 }
5947
5948 /* terminate list1 */
5949 cnt = dst;
5950 list1[dst] = MCMONINV;
5951
5952 /*
5953 * Go through the list of accessible objects, and perform
5954 * the sensible-object (verXoVerb) check on each. Copy each
5955 * sensible object to list2.
5956 */
5957 for (i = 0, cnt2 = 0 ; i < cnt ; ++i)
5958 {
5959 /* run it by the appropriate sensible-object check */
5960 if (accprop == PRP_VALIDACTOR)
5961 {
5962 /* run it through preferredActor */
5963 runppr(ctx->voccxrun, list1[i], PRP_PREFACTOR, 0);
5964 if (runpoplog(ctx->voccxrun))
5965 {
5966 list2[cnt2] = list1[i];
5967 flags2[cnt2] = flags1[i];
5968 ++cnt2;
5969 }
5970 }
5971 else
5972 {
5973 /* run it through verXoVerb */
5974 tiohide(ctx->voccxtio);
5975 if (otherobj != MCMONINV)
5976 runpobj(ctx->voccxrun, otherobj);
5977 runpobj(ctx->voccxrun, cmdActor);
5978 runppr(ctx->voccxrun, list1[i], verprop,
5979 (otherobj != MCMONINV ? 2 : 1));
5980
5981 /*
5982 * If that didn't result in a message, this object
5983 * passed the tougher test of ver?oX, so include it
5984 * in list2.
5985 */
5986 if (!tioshow(ctx->voccxtio))
5987 {
5988 list2[cnt2] = list1[i];
5989 flags2[cnt2] = flags1[i];
5990 ++cnt2;
5991 }
5992 }
5993 }
5994
5995 /*
5996 * Construct a string consisting of the words the user typed
5997 * to reference this object, in case we need to complain.
5998 */
5999 usrobj[0] = '\0';
6000 if (inlist[inpos].vocolfst != 0 && inlist[inpos].vocollst != 0)
6001 {
6002 for (p = inlist[inpos].vocolfst ; p <= inlist[inpos].vocollst
6003 ; p += strlen(p) + 1)
6004 {
6005 /* add a space if we have a prior word */
6006 if (usrobj[0] != '\0')
6007 {
6008 /* quote the space if the last word ended with '.' */
6009 if (p[strlen(p)-1] == '.')
6010 strcat(usrobj, "\\");
6011
6012 /* add the space */
6013 strcat(usrobj, " ");
6014 }
6015
6016 /* add the current word, or "of" if it's "of" */
6017 if (voc_check_special(ctx, p, VOCW_OF))
6018 vocaddof(ctx, usrobj);
6019 else
6020 strcat(usrobj, p);
6021 }
6022 }
6023
6024 /*
6025 * If there's nothing in the YES list, and we have just a
6026 * single number as our word, act as though they are talking
6027 * about the number itself, rather than one of the objects
6028 * that happened to use the number -- none of those objects
6029 * make any sense, it seems, so fall back on the number.
6030 *
6031 * Note that we may also have only numObj in the YES list,
6032 * because the noun phrase parser normally adds numObj when
6033 * the player types a noun phrase consisting only of a
6034 * number. Do the same thing in this case -- just return
6035 * the number object.
6036 */
6037 if ((cnt2 == 0
6038 || (cnt2 == 1 && list2[0] == ctx->voccxnum))
6039 && inlist[inpos].vocolfst != 0
6040 && inlist[inpos].vocolfst == inlist[inpos].vocollst
6041 && vocisdigit(*inlist[inpos].vocolfst))
6042 {
6043 long v1;
6044 char vbuf[4];
6045
6046 v1 = atol(inlist[inpos].vocolfst);
6047 oswp4s(vbuf, v1);
6048 vocsetobj(ctx, ctx->voccxnum, DAT_NUMBER, vbuf,
6049 &inlist[inpos], &outlist[outpos]);
6050 outlist[outpos].vocolflg = VOCS_NUM;
6051 ++outpos;
6052
6053 /* skip all objects that matched the number */
6054 for ( ; inlist[inpos+1].vocolobj != MCMONINV
6055 && inlist[inpos+1].vocolfst == inlist[inpos].vocolfst
6056 ; ++inpos) ;
6057 continue;
6058 }
6059
6060 /*
6061 * Check if we found anything in either the YES (list2) or
6062 * MAYBE (list1) lists. If there's nothing in either list,
6063 * complain and return.
6064 */
6065 if (cnt2 == 0 && cnt == 0)
6066 {
6067 /*
6068 * We have nothing sensible, and nothing even
6069 * accessible. If there's anything merely visible,
6070 * complain about those items.
6071 */
6072 if (cnt3 != 0)
6073 {
6074 /* there are visible items - complain about them */
6075 cnt = cnt3;
6076 cantreach_list = list3;
6077 noreach = TRUE;
6078
6079 /* give the cantReach message, even for multiple objects */
6080 goto noreach1;
6081 }
6082 else
6083 {
6084 /*
6085 * explain that there's nothing visible or
6086 * accessible matching the noun phrase, and abort
6087 * the command with an error
6088 */
6089 if (!silent)
6090 vocerr(ctx, VOCERR(9),
6091 "I don't see any %s here.", usrobj);
6092 err = VOCERR(9);
6093 goto done;
6094 }
6095 }
6096
6097 /*
6098 * If anything passed the stronger test (objects passing are
6099 * in list2), use this as our proposed resolution for the
6100 * noun phrase. If nothing passed the stronger test (i.e.,
6101 * list2 is empty), simply keep the list of accessible
6102 * objects in list1.
6103 */
6104 if (cnt2 != 0)
6105 {
6106 /*
6107 * we have items passing the stronger test -- copy the
6108 * stronger list (list2) to list1
6109 */
6110 cnt = cnt2;
6111 memcpy(list1, list2, (size_t)(cnt2 * sizeof(list1[0])));
6112 memcpy(flags1, flags2, (size_t)(cnt2 * sizeof(flags1[0])));
6113 }
6114
6115 /*
6116 * Check for redundant objects in the list. If the same
6117 * object appears multiple times in the list, remove the
6118 * extra occurrences. Sometimes, a game can inadvertantly
6119 * define the same vocabulary word several times for the
6120 * same object, because of the parser's leniency with
6121 * matching leading substrings of 6 characters or longer.
6122 * To avoid unnecessary "which x do you mean..." errors,
6123 * simply discard any duplicates in the list.
6124 */
6125 for (dst = 0, i = 0 ; i < cnt ; ++i)
6126 {
6127 int dup;
6128 int j;
6129
6130 /* presume we won't find a duplicate of this object */
6131 dup = FALSE;
6132
6133 /*
6134 * look for duplicates of this object in the remainder
6135 * of the list
6136 */
6137 for (j = i + 1 ; j < cnt ; ++j)
6138 {
6139 /* check for a duplicate */
6140 if (list1[i] == list1[j])
6141 {
6142 /* note that this object has a duplicate */
6143 dup = TRUE;
6144
6145 /* we don't need to look any further */
6146 break;
6147 }
6148 }
6149
6150 /*
6151 * if this object has no duplicate, retain it in the
6152 * output list
6153 */
6154 if (!dup)
6155 {
6156 /* copy the element to the output */
6157 list1[dst] = list1[i];
6158 flags1[dst] = flags1[i];
6159
6160 /* count the output */
6161 ++dst;
6162 }
6163 }
6164
6165 /* update the count to the new list's size */
6166 cnt = dst;
6167 list1[cnt] = MCMONINV;
6168
6169 /*
6170 * If we have more than one object in the list, and numObj
6171 * is still in the list, remove numObj - we don't want to
6172 * consider numObj to be considered ambiguous with another
6173 * object when the other object passes access and validation
6174 * tests.
6175 */
6176 if (cnt > 1)
6177 {
6178 /* scan the list for numObj */
6179 for (i = 0, dst = 0 ; i < cnt ; ++i)
6180 {
6181 /* if this isn't numObj, keep this element */
6182 if (list1[i] != ctx->voccxnum)
6183 list1[dst++] = list1[i];
6184 }
6185
6186 /* update the final count */
6187 cnt = dst;
6188 list1[cnt] = MCMONINV;
6189 }
6190
6191 /*
6192 * Check for a generic numeric adjective ('#' in the
6193 * adjective list for the object) in each object. If we
6194 * find it, we need to make sure there's a number in the
6195 * name of the object.
6196 */
6197 for (i = 0 ; i < cnt ; ++i)
6198 {
6199 if (has_gen_num_adj(ctx, list1[i]))
6200 {
6201 /*
6202 * If they specified a count, create the specified
6203 * number of objects. Otherwise, if the object is
6204 * plural, they mean to use all of the objects, so a
6205 * numeric adjective isn't required -- set the
6206 * numeric adjective property in the object to nil
6207 * to so indicate. Otherwise, look for the number,
6208 * and set the numeric adjective property
6209 * accordingly.
6210 */
6211 if ((flags1[i] & (VOCS_ANY | VOCS_COUNT)) != 0)
6212 {
6213 int n = (user_count ? user_count : 1);
6214 int j;
6215 objnum objn = list1[i];
6216
6217 /*
6218 * They specified a count, so we want to create
6219 * n-1 copies of the numbered object. Make room
6220 * for the n-1 new copies of this object by
6221 * shifting any elements that follow up n-1
6222 * slots.
6223 */
6224 if (i + 1 != cnt && n > 1)
6225 {
6226 memmove(&list1[i + n - 1], &list1[i],
6227 (cnt - i) * sizeof(list1[i]));
6228 memmove(&flags1[i + n - 1], &flags1[i],
6229 (cnt - i) * sizeof(flags1[i]));
6230 }
6231
6232 /* create n copies of this object */
6233 for (j = 0 ; j < n ; ++j)
6234 {
6235 long l;
6236
6237 /*
6238 * Generate a number for the new object,
6239 * asking the object to tell us what value
6240 * to use for an "any".
6241 */
6242 runpnum(ctx->voccxrun, (long)(j + 1));
6243 runppr(ctx->voccxrun, objn, PRP_ANYVALUE, 1);
6244 l = runpopnum(ctx->voccxrun);
6245
6246 /* try creating the new object */
6247 list1[i+j] =
6248 voc_new_num_obj(ctx, objn,
6249 cmdActor, cmdVerb,
6250 l, FALSE);
6251 if (list1[i+j] == MCMONINV)
6252 {
6253 err = VOCERR(40);
6254 goto done;
6255 }
6256 }
6257 }
6258 else if ((flags1[i] & VOCS_PLURAL) != 0)
6259 {
6260 /*
6261 * get the plural object by asking for the
6262 * numbered object with a nil number parameter
6263 */
6264 list1[i] =
6265 voc_new_num_obj(ctx, list1[i], cmdActor, cmdVerb,
6266 (long)0, TRUE);
6267 if (list1[i] == MCMONINV)
6268 {
6269 err = VOCERR(40);
6270 goto done;
6271 }
6272 }
6273 else
6274 {
6275 char *p;
6276 int found;
6277
6278 /*
6279 * No plural, no "any" - we just want to create
6280 * one numbered object, using the number that
6281 * the player must have specified. Make sure
6282 * the player did, in fact, specify a number.
6283 */
6284 for (found = FALSE, p = inlist[inpos].vocolfst ;
6285 p != 0 && p <= inlist[inpos].vocollst ;
6286 p += strlen(p) + 1)
6287 {
6288 /* did we find it? */
6289 if (vocisdigit(*p))
6290 {
6291 long l;
6292
6293 /* get the number */
6294 l = atol(p);
6295
6296 /* create the object with this number */
6297 list1[i] = voc_new_num_obj(ctx, list1[i],
6298 cmdActor, cmdVerb,
6299 l, FALSE);
6300 if (list1[i] == MCMONINV)
6301 {
6302 err = VOCERR(40);
6303 goto done;
6304 }
6305
6306 /* the command looks to be valid */
6307 found = TRUE;
6308 break;
6309 }
6310 }
6311
6312 /* if we didn't find it, stop now */
6313 if (!found)
6314 {
6315 if (!silent)
6316 vocerr(ctx, VOCERR(160),
6317 "You'll have to be more specific about which %s you mean.",
6318 usrobj);
6319 err = VOCERR(160);
6320 goto done;
6321 }
6322 }
6323 }
6324 }
6325
6326 /*
6327 * We still have an ambiguous word - ask the user which of
6328 * the possible objects they meant to use
6329 */
6330 trying_again = FALSE;
6331 for (;;)
6332 {
6333 int wrdcnt;
6334 int next;
6335 uchar *p;
6336 int cleared_noun;
6337 int diff_cnt;
6338 int stat;
6339 int num_wanted;
6340 int is_ambig;
6341 int all_plural;
6342
6343 /*
6344 * check for usage - determine if we have singular
6345 * definite, singular indefinite, counted, or plural
6346 * usage
6347 */
6348 if ((flags1[0] & (VOCS_PLURAL | VOCS_ANY | VOCS_COUNT)) != 0)
6349 {
6350 int i;
6351
6352 /*
6353 * loop through the objects to AND together the
6354 * flags from all of the objects; we only care about
6355 * the plural flags (PLURAL, ANY, and COUNT), so
6356 * start out with only those, then AND off any that
6357 * aren't in all of the objects
6358 */
6359 for (all_plural = VOCS_PLURAL | VOCS_ANY | VOCS_COUNT,
6360 i = 0 ; i < cnt ; ++i)
6361 {
6362 /* AND out this object's flags */
6363 all_plural &= flags1[i];
6364
6365 /*
6366 * if we've ANDed down to zero, there's no need
6367 * to look any further
6368 */
6369 if (!all_plural)
6370 break;
6371 }
6372 }
6373 else
6374 {
6375 /*
6376 * it looks like we want just a single object -
6377 * clear the various plural flags
6378 */
6379 all_plural = 0;
6380 }
6381
6382 /*
6383 * Count the distinguishable items.
6384 *
6385 * If we're looking for a single object, don't keep
6386 * duplicate indistinguishable items (i.e., keep only
6387 * one item from each set of mutually indistinguishable
6388 * items), since we could equally well use any single
6389 * one of those items. If we're looking for multiple
6390 * objects, keep all of the items, since the user is
6391 * referring to all of them.
6392 */
6393 diff_cnt = voc_count_diff(ctx, list1, flags1, &cnt,
6394 all_plural != 0 || use_all_objs);
6395
6396 /*
6397 * Determine how many objects we'd like to find. If we
6398 * have a count specified, we'd like to find the given
6399 * number of objects. If we have "ANY" specified, we
6400 * just want to pick one object arbitrarily. If we have
6401 * all plurals, we can keep all of the objects. If the
6402 * 'use_all_objs' flag is true, it means that we can use
6403 * everything in the list.
6404 */
6405 if (use_all_objs)
6406 {
6407 /* we want to use all of the objects */
6408 num_wanted = cnt;
6409 is_ambig = FALSE;
6410 }
6411 else if ((all_plural & VOCS_COUNT) != 0)
6412 {
6413 /*
6414 * we have a count - we want exactly the given
6415 * number of objects, but we can pick an arbitrary
6416 * subset, so it's not ambiguous even if we have too
6417 * many at the moment
6418 */
6419 num_wanted = user_count;
6420 is_ambig = FALSE;
6421 }
6422 else if ((all_plural & VOCS_ANY) != 0)
6423 {
6424 /*
6425 * they specified "any", so we want exactly one, but
6426 * we can pick one arbitrarily, so there's no
6427 * ambiguity
6428 */
6429 num_wanted = 1;
6430 is_ambig = FALSE;
6431 }
6432 else if (all_plural != 0)
6433 {
6434 /*
6435 * we have a simple plural, so we can use all of the
6436 * provided objects without ambiguity
6437 */
6438 num_wanted = cnt;
6439 is_ambig = FALSE;
6440 }
6441 else
6442 {
6443 /*
6444 * it's a singular, definite usage, so we want
6445 * exactly one item; if we have more than one in our
6446 * list, it's ambiguous
6447 */
6448 num_wanted = 1;
6449 is_ambig = (cnt != 1);
6450 }
6451
6452 /* call the disambiguation hook */
6453 stat = voc_disambig_hook(ctx, cmdVerb, cmdActor, cmdPrep,
6454 otherobj, accprop, verprop,
6455 list1, flags1, &cnt,
6456 inlist[inpos].vocolfst,
6457 inlist[inpos].vocollst,
6458 num_wanted, is_ambig, disnewbuf,
6459 silent);
6460
6461 /* check the status */
6462 if (stat == VOC_DISAMBIG_DONE)
6463 {
6464 /* that's it - copy the result */
6465 for (i = 0 ; i < cnt ; ++i)
6466 vocout(&outlist[outpos++], list1[i], flags1[i],
6467 inlist[inpos].vocolfst,
6468 inlist[inpos].vocollst);
6469
6470 /* we're done */
6471 break;
6472 }
6473 else if (stat == VOC_DISAMBIG_CONT)
6474 {
6475 /*
6476 * Continue with the new list (which is the same as
6477 * the old list, if it wasn't actually updated by
6478 * the hook routine) - proceed with remaining
6479 * processing, but using the new list.
6480 *
6481 * Because the list has been updated, we must once
6482 * again count the number of distinguishable items,
6483 * since that may have changed.
6484 */
6485 diff_cnt = voc_count_diff(ctx, list1, flags1, &cnt, TRUE);
6486 }
6487 else if (stat == VOC_DISAMBIG_PARSE_RESP
6488 || stat == VOC_DISAMBIG_PROMPTED)
6489 {
6490 /*
6491 * The status indicates one of the following:
6492 *
6493 * - the hook prompted for more information and read
6494 * a response from the player, but decided not to
6495 * parse it; we will continue with the current list,
6496 * and parse the player's response as provided by
6497 * the hook.
6498 *
6499 * - the hook prompted for more information, but
6500 * left the reading to us. We'll proceed with the
6501 * current list and read a response as normal, but
6502 * without displaying another prompt.
6503 *
6504 * In any case, just continue processing; we'll take
6505 * appropriate action on the prompting and reading
6506 * when we reach those steps.
6507 */
6508 }
6509 else
6510 {
6511 /* anything else is an error */
6512 err = VOCERR(41);
6513 goto done;
6514 }
6515
6516 /*
6517 * If we found only one word, or a plural/ANY, we are
6518 * finished. If we found a count, use that count if
6519 * possible.
6520 */
6521 if (cnt == 1 || all_plural || use_all_objs)
6522 {
6523 int flags;
6524
6525 /* keep only one of the objects if ANY was used */
6526 if ((all_plural & VOCS_COUNT) != 0)
6527 {
6528 if (user_count > cnt)
6529 {
6530 if (!silent)
6531 vocerr(ctx, VOCERR(30),
6532 "I only see %d of those.", cnt);
6533 err = VOCERR(30);
6534 goto done;
6535 }
6536 cnt = user_count;
6537 flags = VOCS_ALL;
6538 }
6539 else if ((all_plural & VOCS_ANY) != 0)
6540 {
6541 cnt = 1;
6542 flags = VOCS_ALL;
6543 }
6544 else
6545 flags = 0;
6546
6547 /* put the list */
6548 for (i = 0 ; i < cnt ; ++i)
6549 vocout(&outlist[outpos++], list1[i], flags,
6550 inlist[inpos].vocolfst,
6551 inlist[inpos].vocollst);
6552
6553 /* we're done */
6554 break;
6555 }
6556
6557 /* make sure output capturing is off */
6558 tiocapture(ctx->voccxtio, (mcmcxdef *)0, FALSE);
6559 tioclrcapture(ctx->voccxtio);
6560
6561 /*
6562 * if we're in "silent" mode, we can't ask the player
6563 * for help, so return an error
6564 */
6565 if (silent)
6566 {
6567 /*
6568 * We can't disambiguate the list. Fill in the
6569 * return list with what's left, which is still
6570 * ambiguous, and note that we need to return an
6571 * error code indicating that the list remains
6572 * ambiguous.
6573 */
6574 for (i = 0 ; i < cnt && outpos < VOCMAXAMBIG ; ++i)
6575 vocout(&outlist[outpos++], list1[i], 0,
6576 inlist[inpos].vocolfst,
6577 inlist[inpos].vocollst);
6578
6579 /* note that we have ambiguity remaining */
6580 still_ambig = TRUE;
6581
6582 /* we're done with this sublist */
6583 break;
6584 }
6585
6586 /*
6587 * We need to prompt for more information interactively.
6588 * Figure out how we're going to display the prompt.
6589 *
6590 * - If the disambigXobj hook status (stat) indicates
6591 * that the hook already displayed a prompt of its own,
6592 * we don't need to add anything here.
6593 *
6594 * - Otherwise, if there's a parseDisambig function
6595 * defined in the game, call it to display the prompt.
6596 *
6597 * - Otherwise, display our default prompt.
6598 */
6599 if (stat == VOC_DISAMBIG_PARSE_RESP
6600 || stat == VOC_DISAMBIG_PROMPTED)
6601 {
6602 /*
6603 * the disambigXobj hook already asked for a
6604 * response, so don't display any prompt of our own
6605 */
6606 }
6607 else if (ctx->voccxpdis != MCMONINV)
6608 {
6609 uint l;
6610
6611 /*
6612 * There's a parseDisambig function defined in the
6613 * game - call it to display the prompt, passing the
6614 * list of possible objects and the player's
6615 * original noun phrase text as parameters.
6616 */
6617 for (i = 0, p = lstbuf+2 ; i < cnt ; ++i, p += 2)
6618 {
6619 *p++ = DAT_OBJECT;
6620 oswp2(p, list1[i]);
6621 }
6622 l = p - lstbuf;
6623 oswp2(lstbuf, l);
6624 runpbuf(ctx->voccxrun, DAT_LIST, lstbuf);
6625 runpstr(ctx->voccxrun, usrobj, (int)strlen(usrobj), 1);
6626 runfn(ctx->voccxrun, ctx->voccxpdis, 2);
6627 }
6628 else
6629 {
6630 /* display "again" message, if necessary */
6631 if (trying_again)
6632 vocerr_info(ctx, VOCERR(100), "Let's try it again: ");
6633
6634 /* ask the user about it */
6635 vocerr_info(ctx, VOCERR(101),
6636 "Which %s do you mean, ", usrobj);
6637 for (i = 0 ; i < cnt ; )
6638 {
6639 int eqcnt;
6640 int j;
6641 objnum sc;
6642
6643 /*
6644 * See if we have multiple instances of an
6645 * identical object. All such instances should
6646 * be grouped together (this was done above), so
6647 * we can just count the number of consecutive
6648 * equivalent objects.
6649 */
6650 eqcnt = 1;
6651 runppr(ctx->voccxrun, list1[i], PRP_ISEQUIV, 0);
6652 if (runpoplog(ctx->voccxrun))
6653 {
6654 /* get the superclass, if possible */
6655 sc = objget1sc(ctx->voccxmem, list1[i]);
6656 if (sc != MCMONINV)
6657 {
6658 /* count equivalent objects that follow */
6659 for (j = i + 1 ; j < cnt ; ++j)
6660 {
6661 if (objget1sc(ctx->voccxmem, list1[j])
6662 == sc)
6663 ++eqcnt;
6664 else
6665 break;
6666 }
6667 }
6668 }
6669
6670 /*
6671 * Display this object's name. If we have only
6672 * one such object, display its thedesc,
6673 * otherwise display its adesc.
6674 */
6675 runppr(ctx->voccxrun, list1[i],
6676 (prpnum)(eqcnt == 1 ?
6677 PRP_THEDESC : PRP_ADESC), 0);
6678
6679 /* display the separator as appropriate */
6680 if (i + 1 < diff_cnt)
6681 vocerr_info(ctx, VOCERR(102), ", ");
6682 if (i + 2 == diff_cnt)
6683 vocerr_info(ctx, VOCERR(103), "or ");
6684
6685 /* skip all equivalent items */
6686 i += eqcnt;
6687 }
6688 vocerr_info(ctx, VOCERR(104), "?");
6689 }
6690
6691 /*
6692 * Read the response. If the disambigXobj hook already
6693 * read the response, we don't need to read anything
6694 * more.
6695 */
6696 if (stat != VOC_DISAMBIG_PARSE_RESP
6697 && vocread(ctx, cmdActor, cmdVerb, disnewbuf,
6698 (int)VOCBUFSIZ, 2) == VOCREAD_REDO)
6699 {
6700 /* they want to treat the input as a new command */
6701 strcpy(cmdbuf, disnewbuf);
6702 ctx->voccxunknown = 0;
6703 ctx->voccxredo = TRUE;
6704 err = VOCERR(43);
6705 goto done;
6706 }
6707
6708 /*
6709 * parse the response
6710 */
6711
6712 /* tokenize the list */
6713 wrdcnt = voctok(ctx, disnewbuf, disbuffer, diswordlist,
6714 TRUE, TRUE, TRUE);
6715 if (wrdcnt == 0)
6716 {
6717 /* empty response - run pardon() function and abort */
6718 runfn(ctx->voccxrun, ctx->voccxprd, 0);
6719 err = VOCERR(42);
6720 goto done;
6721 }
6722 if (wrdcnt < 0)
6723 {
6724 /* return the generic punctuation error */
6725 err = VOCERR(1);
6726 goto done;
6727 }
6728
6729 /*
6730 * Before we tokenize the sentence, remember the current
6731 * unknown word count, then momentarily set the count to
6732 * zero. This will cause the tokenizer to absorb any
6733 * unknown words; if there are any unknown words, the
6734 * tokenizer will parse them and set the unknown count.
6735 * If we find any unknown words in the input, we'll
6736 * simply treat the input as an entirely new command.
6737 */
6738 old_unknown = ctx->voccxunknown;
6739 old_lastunk = ctx->voccxlastunk;
6740 ctx->voccxunknown = 0;
6741
6742 /* clear our internal type list */
6743 memset(distypelist, 0, VOCBUFSIZ * sizeof(distypelist[0]));
6744
6745 /* tokenize the sentence */
6746 diswordlist[wrdcnt] = 0;
6747 if (vocgtyp(ctx, diswordlist, distypelist, cmdbuf)
6748 || ctx->voccxunknown != 0)
6749 {
6750 /*
6751 * there's an unknown word or other problem - retry
6752 * the input as an entirely new command
6753 */
6754 strcpy(cmdbuf, disnewbuf);
6755 ctx->voccxunknown = 0;
6756 ctx->voccxredo = TRUE;
6757 err = VOCERR(2);
6758 goto done;
6759 }
6760
6761 /* restore the original unknown word count */
6762 ctx->voccxunknown = old_unknown;
6763 ctx->voccxlastunk = old_lastunk;
6764
6765 /*
6766 * Find the last word that can be an adj and/or a noun.
6767 * If it can be either (i.e., both bits are set), clear
6768 * the noun bit and make it just an adjective. This is
6769 * because we're asking for an adjective for clarification,
6770 * and we most likely want it to be an adjective in this
6771 * context; if the noun bit is set, too, the object lister
6772 * will think it must be a noun, being the last word.
6773 */
6774 for (i = 0 ; i < wrdcnt ; ++i)
6775 {
6776 if (!(distypelist[i] &
6777 (VOCT_ADJ | VOCT_NOUN | VOCT_ARTICLE)))
6778 break;
6779 }
6780
6781 if (i && (distypelist[i-1] & VOCT_ADJ)
6782 && (distypelist[i-1] & VOCT_NOUN))
6783 {
6784 /*
6785 * Note that we're clearing the noun flag. If
6786 * we're unsuccessful in finding the object with the
6787 * noun flag cleared, we'll put the noun flag back
6788 * in and give it another try (by adding VOCT_NOUN
6789 * back into distypelist[cleared_noun], and coming
6790 * back to the label below).
6791 */
6792 cleared_noun = i-1;
6793 distypelist[i-1] &= ~VOCT_NOUN;
6794 }
6795 else
6796 cleared_noun = -1;
6797
6798 try_current_flags:
6799 /* start with the first word */
6800 if (vocspec(diswordlist[0], VOCW_ALL)
6801 || vocspec(diswordlist[0], VOCW_BOTH))
6802 {
6803 char *nam;
6804 static char all_name[] = "all";
6805 static char both_name[] = "both";
6806
6807 if (vocspec(diswordlist[0], VOCW_ALL))
6808 nam = all_name;
6809 else
6810 nam = both_name;
6811
6812 for (i = 0 ; i < cnt ; ++i)
6813 vocout(&outlist[outpos++], list1[i], 0, nam, nam);
6814 if (noreach)
6815 {
6816 cantreach_list = list1;
6817 goto noreach1;
6818 }
6819 break;
6820 }
6821 else if (vocspec(diswordlist[0], VOCW_ANY))
6822 {
6823 static char *anynm = "any";
6824
6825 /* choose the first object arbitrarily */
6826 vocout(&outlist[outpos++], list1[i], VOCS_ALL,
6827 anynm, anynm);
6828 break;
6829 }
6830 else
6831 {
6832 /* check for a word matching the phrase */
6833 cnt2 = vocchknoun(ctx, diswordlist, distypelist,
6834 0, &next, disnounlist, FALSE);
6835 if (cnt2 > 0)
6836 {
6837 /*
6838 * if that didn't consume the entire phrase, or
6839 * at least up to "one" or "ones" or a period,
6840 * disallow it, since they must be entering
6841 * something more complicated
6842 */
6843 if (diswordlist[next] != 0
6844 && !vocspec(diswordlist[next], VOCW_ONE)
6845 && !vocspec(diswordlist[next], VOCW_ONES)
6846 && !vocspec(diswordlist[next], VOCW_THEN))
6847 {
6848 cnt2 = 0;
6849 }
6850 }
6851 else if (cnt2 < 0)
6852 {
6853 /*
6854 * There was a syntax error in the phrase.
6855 * vocchknoun() will have displayed a message in
6856 * this case, so we're done parsing this command.
6857 */
6858 err = VOCERR(45);
6859 goto done;
6860 }
6861
6862 /* proceed only if we got a valid phrase */
6863 if (cnt2 > 0)
6864 {
6865 int cnt3;
6866 int newcnt;
6867
6868 /* build the list of matches for the new phrase */
6869 for (i = 0, newcnt = 0 ; i < cnt2 ; ++i)
6870 {
6871 int j;
6872 int found;
6873
6874 /*
6875 * make sure this object isn't already in
6876 * our list - we want each object only once
6877 */
6878 for (j = 0, found = FALSE ; j < newcnt ; ++j)
6879 {
6880 /* if this is in the list, note it */
6881 if (list2[j] == disnounlist[i].vocolobj)
6882 {
6883 found = TRUE;
6884 break;
6885 }
6886 }
6887
6888 /*
6889 * add it to our list only if it wasn't
6890 * already there
6891 */
6892 if (!found)
6893 {
6894 /* copy the object ID */
6895 list2[newcnt] = disnounlist[i].vocolobj;
6896
6897 /* copy the flags that we care about */
6898 flags2[newcnt] = disnounlist[i].vocolflg
6899 & (VOCS_PLURAL | VOCS_ANY
6900 | VOCS_COUNT);
6901
6902 /* count the entry */
6903 ++newcnt;
6904 }
6905 }
6906
6907 /* terminate the list */
6908 list2[newcnt] = MCMONINV;
6909
6910 /* intersect the new list with the old list */
6911 newcnt = vocisect(list2, list1);
6912
6913 /* count the noun phrases in the new list */
6914 for (i = cnt3 = 0 ; i < cnt2 ; ++i)
6915 {
6916 /* we have one more noun phrase */
6917 ++cnt3;
6918
6919 /* if we have a noun phrase, skip matching objs */
6920 if (disnounlist[i].vocolfst != 0)
6921 {
6922 int j;
6923
6924 /* skip objects matching this noun phrase */
6925 for (j = i + 1 ; disnounlist[i].vocolfst ==
6926 disnounlist[j].vocolfst ; ++j) ;
6927 i = j - 1;
6928 }
6929 }
6930
6931 /*
6932 * If the count of items in the intersection of
6933 * the original list and the typed-in list is no
6934 * bigger than the number of items specified in
6935 * the typed-in list, we've successfully
6936 * disambiguated the object, because the user's
6937 * new list matches only one object for each set
6938 * of words the user typed.
6939 */
6940 if (newcnt
6941 && (newcnt <= cnt3
6942 || (diswordlist[next]
6943 && vocspec(diswordlist[next],
6944 VOCW_ONES))))
6945 {
6946 static char one_name[] = "ones";
6947
6948 for (i = 0 ; i < cnt ; ++i)
6949 vocout(&outlist[outpos++], list2[i], 0,
6950 one_name, one_name);
6951
6952 if (noreach)
6953 {
6954 cnt = newcnt;
6955 cantreach_list = list2;
6956 noreach1:
6957 if (accprop == PRP_VALIDACTOR)
6958 {
6959 /* for actors, show a special message */
6960 vocerr(ctx, VOCERR(31),
6961 "You can't talk to that.");
6962 }
6963 else
6964 {
6965 /* use the normal no-reach message */
6966 vocnoreach(ctx, cantreach_list, cnt,
6967 cmdActor, cmdVerb, cmdPrep,
6968 defprop, cnt > 1, 0, 0, cnt);
6969 }
6970 err = VOCERR(31);
6971 goto done;
6972 }
6973 break;
6974 }
6975 else if (newcnt == 0)
6976 {
6977 /*
6978 * If we cleared the noun, maybe we actually
6979 * need to treat the word as a noun, so add
6980 * the noun flag back in and give it another
6981 * go. If we didn't clear the noun, there's
6982 * nothing left to try, so explain that we
6983 * don't see any such object and give up.
6984 */
6985 if (cleared_noun != -1)
6986 {
6987 distypelist[cleared_noun] |= VOCT_NOUN;
6988 cleared_noun = -1;
6989 goto try_current_flags;
6990 }
6991
6992 /* find the first object with a noun phrase */
6993 for (i = 0 ; i < cnt2 ; ++i)
6994 {
6995 /* if we have a noun phrase, stop scanning */
6996 if (disnounlist[i].vocolfst != 0)
6997 break;
6998 }
6999
7000 /*
7001 * if we found a noun phrase, build a string
7002 * out of the words used; otherwise, just
7003 * use "such"
7004 */
7005 if (i != cnt2)
7006 {
7007 char *p;
7008 char *last;
7009
7010 /* clear the word buffer */
7011 newobj[0] = '\0';
7012
7013 /* build a string out of the words */
7014 p = disnounlist[i].vocolfst;
7015 last = disnounlist[i].vocollst;
7016 for ( ; p <= last ; p += strlen(p) + 1)
7017 {
7018 /*
7019 * If this is a special word, we
7020 * probably can't construct a
7021 * sensible sentence - special words
7022 * are special parts of speech that
7023 * will look weird if inserted into
7024 * our constructed noun phrase. In
7025 * these cases, turn the entire
7026 * thing into "I don't see any
7027 * *such* object" rather than trying
7028 * to make do with pronouns or other
7029 * special words.
7030 */
7031 if (vocisspec(p))
7032 {
7033 /*
7034 * replace the entire adjective
7035 * phrase with "such"
7036 */
7037 strcpy(newobj, "such");
7038
7039 /*
7040 * stop here - don't add any
7041 * more, since "such" is the
7042 * whole thing
7043 */
7044 break;
7045 }
7046
7047 /* add a space if we have a prior word */
7048 if (newobj[0] != '\0')
7049 strcat(newobj, " ");
7050
7051 /* add this word */
7052 strcat(newobj, p);
7053 }
7054 }
7055 else
7056 {
7057 /* no noun phrase found */
7058 strcpy(newobj, "such");
7059 }
7060
7061 /* didn't find anything - complain and give up */
7062 vocerr(ctx, VOCERR(16),
7063 "You don't see any %s %s here.",
7064 newobj, usrobj);
7065 err = VOCERR(16);
7066 goto done;
7067 }
7068
7069 /*
7070 * If we get here, it means that we have still
7071 * more than one object per noun phrase typed in
7072 * the latest sentence. Limit the list to the
7073 * intersection (by copying list2 to list1), and
7074 * try again.
7075 */
7076 memcpy(list1, list2,
7077 (size_t)((newcnt + 1) * sizeof(list1[0])));
7078 cnt = newcnt;
7079 trying_again = TRUE;
7080 }
7081 else
7082 {
7083 /*
7084 * We didn't find a noun phrase, so it's probably a
7085 * new command. However, check first to see if we
7086 * were making a trial run with the noun flag
7087 * cleared: if so, go back and make another pass
7088 * with the noun flag added back in to see if that
7089 * works any better.
7090 */
7091 if (cleared_noun != -1)
7092 {
7093 distypelist[cleared_noun] |= VOCT_NOUN;
7094 cleared_noun = -1;
7095 goto try_current_flags;
7096 }
7097
7098 /* retry as an entire new command */
7099 strcpy(cmdbuf, disnewbuf);
7100 ctx->voccxunknown = 0;
7101 ctx->voccxredo = TRUE;
7102 err = VOCERR(43);
7103 goto done;
7104 }
7105 }
7106 }
7107 inpos = lpos - 1;
7108 }
7109 }
7110
7111 /* terminate the output list */
7112 vocout(&outlist[outpos], MCMONINV, 0, (char *)0, (char *)0);
7113
7114 /*
7115 * If we still have ambiguous objects, so indicate. This can only
7116 * happen when we operate in "silent" mode, because only then can we
7117 * give up without fully resolving a list.
7118 */
7119 if (still_ambig)
7120 err = VOCERR(44);
7121
7122 /* no error */
7123 err = 0;
7124
7125 done:
7126 ERRCLEAN(ctx->voccxerr)
7127 {
7128 /*
7129 * reset the stack before we return, in case the caller handles
7130 * the error without aborting the command
7131 */
7132 voc_leave(ctx, save_sp);
7133 }
7134 ERRENDCLN(ctx->voccxerr);
7135
7136 /* return success */
7137 VOC_RETVAL(ctx, save_sp, err);
7138 }
7139
7140 /* vocready - see if at end of command, execute & return TRUE if so */
7141 static int vocready(voccxdef *ctx, char *cmd[], int *typelist, int cur,
7142 objnum cmdActor, objnum cmdPrep, char *vverb, char *vprep,
7143 vocoldef *dolist, vocoldef *iolist, int *errp,
7144 char *cmdbuf, int first_word, uchar **preparse_list,
7145 int *next_start)
7146 {
7147 if (cur != -1
7148 && (cmd[cur] == (char *)0
7149 || vocspec(cmd[cur], VOCW_AND) || vocspec(cmd[cur], VOCW_THEN)))
7150 {
7151 if (ctx->voccxflg & VOCCXFDBG)
7152 {
7153 char buf[128];
7154
7155 sprintf(buf, ". executing verb: %s %s\\n",
7156 vverb, vprep ? vprep : "");
7157 tioputs(ctx->vocxtio, buf);
7158 }
7159
7160 *errp = execmd(ctx, cmdActor, cmdPrep, vverb, vprep, dolist, iolist,
7161 &cmd[first_word], &typelist[first_word],cmdbuf,
7162 cur - first_word, preparse_list, next_start);
7163 return(TRUE);
7164 }
7165 return(FALSE);
7166 }
7167
7168 /* execute a single command */
7169 static int voc1cmd(voccxdef *ctx, char *cmd[], char *cmdbuf,
7170 objnum *cmdActorp, int first)
7171 {
7172 int cur;
7173 int next;
7174 objnum o;
7175 vocwdef *v;
7176 char *vverb;
7177 int vvlen;
7178 char *vprep;
7179 int cnt;
7180 int err;
7181 vocoldef *dolist;
7182 vocoldef *iolist;
7183 int *typelist;
7184 objnum cmdActor = *cmdActorp;
7185 objnum cmdPrep;
7186 int swapObj; /* TRUE -> swap dobj and iobj */
7187 int again;
7188 int first_word;
7189 uchar *preparse_list;
7190 int next_start;
7191 struct
7192 {
7193 int active;
7194 int cur;
7195 char **cmd;
7196 char *cmdbuf;
7197 } preparseCmd_stat;
7198 char **newcmd;
7199 char *origcmdbuf;
7200 char *newcmdbuf;
7201 uchar *save_sp;
7202 int no_match;
7203 int retval;
7204
7205 voc_enter(ctx, &save_sp);
7206 VOC_MAX_ARRAY(ctx, vocoldef, dolist);
7207 VOC_MAX_ARRAY(ctx, vocoldef, iolist);
7208 VOC_STK_ARRAY(ctx, int, typelist, VOCBUFSIZ);
7209 VOC_STK_ARRAY(ctx, char *, newcmd, VOCBUFSIZ);
7210 VOC_STK_ARRAY(ctx, char, newcmdbuf, VOCBUFSIZ);
7211
7212 /* save the original command buf in case we need to redo the command */
7213 origcmdbuf = cmdbuf;
7214
7215 /* clear out the type list */
7216 memset(typelist, 0, VOCBUFSIZ*sizeof(typelist[0]));
7217
7218 /* get the types of the words in the command */
7219 if (vocgtyp(ctx, cmd, typelist, cmdbuf))
7220 {
7221 retval = 1;
7222 goto done;
7223 }
7224
7225 /* start off at the first word */
7226 cur = next = first_word = 0;
7227
7228 /*
7229 * Presume we will be in control of the next word - when execmd() or
7230 * another routine we call decides where the command ends, it will
7231 * fill in a new value here. When this value is non-zero, it will
7232 * tell us where the next sentence start is relative to the previous
7233 * sentence start.
7234 */
7235 next_start = 0;
7236
7237 /* we don't have a preparseCmd result yet */
7238 preparseCmd_stat.active = FALSE;
7239
7240 /* keep going until we run out of work to do */
7241 for (again = FALSE, err = 0 ; ; again = TRUE)
7242 {
7243 /*
7244 * if preparseCmd sent us back a list, parse that list as a new
7245 * command
7246 */
7247 if (err == ERR_PREPRSCMDREDO)
7248 {
7249 uchar *src;
7250 size_t len;
7251 size_t curlen;
7252 char *dst;
7253 int cnt;
7254
7255 /* don't allow a preparseCmd to loop */
7256 if (preparseCmd_stat.active)
7257 {
7258 vocerr(ctx, VOCERR(34),
7259 "Internal game error: preparseCmd loop");
7260 retval = 1;
7261 goto done;
7262 }
7263
7264 /* save our status prior to processing the preparseCmd list */
7265 preparseCmd_stat.active = TRUE;
7266 preparseCmd_stat.cur = cur;
7267 preparseCmd_stat.cmd = cmd;
7268 preparseCmd_stat.cmdbuf = cmdbuf;
7269
7270 /* set up with the new command */
7271 cmd = newcmd;
7272 cmdbuf = newcmdbuf;
7273 cur = 0;
7274
7275 /* break up the list into the new command buffer */
7276 src = preparse_list;
7277 len = osrp2(src) - 2;
7278 for (src += 2, dst = cmdbuf, cnt = 0 ; len ; )
7279 {
7280 /* make sure the next element is a string */
7281 if (*src != DAT_SSTRING)
7282 {
7283 vocerr(ctx, VOCERR(32),
7284 "Internal game error: preparseCmd returned an invalid list");
7285 retval = 1;
7286 goto done;
7287 }
7288
7289 /* get the string */
7290 ++src;
7291 curlen = osrp2(src) - 2;
7292 src += 2;
7293
7294 /* make sure it will fit in the buffer */
7295 if (dst + curlen + 1 >= cmdbuf + VOCBUFSIZ)
7296 {
7297 vocerr(ctx, VOCERR(33),
7298 "Internal game error: preparseCmd command too long");
7299 retval = 1;
7300 goto done;
7301 }
7302
7303 /* store the word */
7304 cmd[cnt++] = dst;
7305 memcpy(dst, src, curlen);
7306 dst[curlen] = '\0';
7307
7308 /* move on to the next word */
7309 len -= 3 + curlen;
7310 src += curlen;
7311 dst += curlen + 1;
7312 }
7313
7314 /* enter a null last word */
7315 cmd[cnt] = 0;
7316
7317 /* generate the type list for the new list */
7318 if (vocgtyp(ctx, cmd, typelist, cmdbuf))
7319 {
7320 /* return an error */
7321 retval = 1;
7322 goto done;
7323 }
7324
7325 /*
7326 * this is not a new command - it's just further processing
7327 * of the current command
7328 */
7329 again = FALSE;
7330
7331 /* clear the error */
7332 err = 0;
7333 }
7334
7335 /* initialize locals */
7336 cmdPrep = MCMONINV; /* assume no preposition */
7337 swapObj = FALSE; /* assume no object swapping */
7338 dolist[0].vocolobj = iolist[0].vocolobj = MCMONINV;
7339 dolist[0].vocolflg = iolist[0].vocolflg = 0;
7340
7341 /* check error return from vocready (which returns from execmd) */
7342 if (err)
7343 {
7344 /* return the error */
7345 retval = err;
7346 goto done;
7347 }
7348
7349 skip_leading_stuff:
7350 /*
7351 * If someone updated the sentence start point, jump there. The
7352 * sentence start is relative to the previous sentence start.
7353 */
7354 if (next_start != 0)
7355 cur = first_word + next_start;
7356
7357 /* clear next_start, so we can tell if someone updates it */
7358 next_start = 0;
7359
7360 /* skip any leading THEN's and AND's */
7361 while (cmd[cur] && (vocspec(cmd[cur], VOCW_THEN)
7362 || vocspec(cmd[cur], VOCW_AND)))
7363 ++cur;
7364
7365 /* see if there's anything left to parse */
7366 if (cmd[cur] == 0)
7367 {
7368 /*
7369 * if we've been off doing preparseCmd work, return to the
7370 * original command list
7371 */
7372 if (preparseCmd_stat.active)
7373 {
7374 /* restore the original status */
7375 cur = preparseCmd_stat.cur;
7376 cmd = preparseCmd_stat.cmd;
7377 cmdbuf = preparseCmd_stat.cmdbuf;
7378 preparseCmd_stat.active = FALSE;
7379
7380 /* get the type list for the original list again */
7381 if (vocgtyp(ctx, cmd, typelist, cmdbuf))
7382 {
7383 /* return the error */
7384 retval = 1;
7385 goto done;
7386 }
7387
7388 /* try again */
7389 goto skip_leading_stuff;
7390 }
7391 else
7392 {
7393 /* nothing to pop - we must be done */
7394 retval = 0;
7395 goto done;
7396 }
7397 }
7398
7399 /*
7400 * display a blank line if this is not the first command on this
7401 * command line, so that we visually separate the results of the
7402 * new command from the results of the previous command
7403 */
7404 if (again)
7405 outformat("\\b"); /* tioblank(ctx->voccxtio); */
7406
7407 {
7408 /* look for an explicit actor in the command */
7409 if ((o = vocgetactor(ctx, cmd, typelist, cur, &next, cmdbuf))
7410 != MCMONINV)
7411 {
7412 /* skip the actor noun phrase in the input */
7413 cur = next;
7414
7415 /* remember the actor internally */
7416 cmdActor = *cmdActorp = o;
7417
7418 /* set the actor in the context */
7419 ctx->voccxactor = o;
7420 }
7421
7422 /* if the actor parsing failed, return an error */
7423 if (cur != next)
7424 {
7425 /* error getting actor */
7426 retval = 1;
7427 goto done;
7428 }
7429 }
7430
7431 /* remember where the sentence starts */
7432 first_word = cur;
7433
7434 /* make sure we have a verb */
7435 if ((cmd[cur] == (char *)0) || !(typelist[cur] & VOCT_VERB))
7436 {
7437 /* unknown verb - handle it with parseUnknownVerb if possible */
7438 if (!try_unknown_verb(ctx, cmdActor, &cmd[cur], &typelist[cur],
7439 0, &next_start, TRUE, VOCERR(17),
7440 "There's no verb in that sentence!"))
7441 {
7442 /* error - abort the command */
7443 retval = 1;
7444 goto done;
7445 }
7446 else
7447 {
7448 /* go back for more */
7449 continue;
7450 }
7451 }
7452 vverb = cmd[cur++]; /* this is the verb */
7453 vvlen = strlen(vverb); /* remember length of verb */
7454 vprep = 0; /* assume no verb-preposition */
7455
7456 /* execute if the command is just a verb */
7457 if (vocready(ctx, cmd, typelist, cur, cmdActor, cmdPrep,
7458 vverb, vprep, dolist, iolist, &err, cmdbuf,
7459 first_word, &preparse_list, &next_start))
7460 continue;
7461
7462 /*
7463 * If the next word is a preposition, and it makes sense to be
7464 * aggregated with this verb, use it as such.
7465 */
7466 if (typelist[cur] & VOCT_PREP)
7467 {
7468 if (vocffw(ctx, vverb, vvlen, cmd[cur], (int)strlen(cmd[cur]),
7469 PRP_VERB, (vocseadef *)0))
7470 {
7471 vprep = cmd[cur++];
7472 if (vocready(ctx, cmd, typelist, cur, cmdActor, cmdPrep,
7473 vverb, vprep, dolist, iolist, &err, cmdbuf,
7474 first_word, &preparse_list, &next_start))
7475 continue;
7476 }
7477 else
7478 {
7479 /*
7480 * If we have a preposition which can NOT be aggregated
7481 * with the verb, take command of this form: "verb prep
7482 * iobj dobj". Note that we do *not* do this if the
7483 * word is also a noun, or it's an adjective and a noun
7484 * (possibly separated by one or more adjectives)
7485 * follows.
7486 */
7487 if ((v = vocffw(ctx, cmd[cur], (int)strlen(cmd[cur]),
7488 (char *)0, 0, PRP_PREP, (vocseadef *)0)) != 0)
7489 {
7490 int swap_ok;
7491
7492 /* if it can be an adjective, check further */
7493 if (typelist[cur] & VOCT_NOUN)
7494 {
7495 /* don't allow the swap */
7496 swap_ok = FALSE;
7497 }
7498 else if (typelist[cur] & VOCT_ADJ)
7499 {
7500 int i;
7501
7502 /* look for a noun, possibly preceded by adj's */
7503 for (i = cur + 1 ;
7504 cmd[i] && (typelist[i] & VOCT_ADJ)
7505 && !(typelist[i] & VOCT_NOUN) ; ++i) ;
7506 swap_ok = (!cmd[i] || !(typelist[i] & VOCT_NOUN));
7507 }
7508 else
7509 {
7510 /* we can definitely allow this swap */
7511 swap_ok = TRUE;
7512 }
7513
7514 if (swap_ok)
7515 {
7516 cmdPrep = v->vocwobj;
7517 swapObj = TRUE;
7518 ++cur;
7519 }
7520 }
7521 }
7522 }
7523
7524 retry_swap:
7525 /* get the direct object if there is one */
7526 if ((cnt = vocchknoun2(ctx, cmd, typelist, cur, &next, dolist,
7527 FALSE, &no_match)) > 0)
7528 {
7529 /* we found a noun phrase matching one or more objects */
7530 cur = next;
7531 }
7532 else if (no_match)
7533 {
7534 /*
7535 * we found a valid noun phrase, but we didn't find any
7536 * objects that matched the words -- get the noun again,
7537 * this time showing the error
7538 */
7539 vocgetnoun(ctx, cmd, typelist, cur, &next, dolist);
7540
7541 /* return the error */
7542 retval = 1;
7543 goto done;
7544 }
7545 else if (cnt < 0)
7546 {
7547 /* invalid syntax - return failure */
7548 retval = 1;
7549 goto done;
7550 }
7551 else
7552 {
7553 /*
7554 * If we thought we were going to get a two-object
7555 * sentence, and we got a zero-object sentence, and it looks
7556 * like the word we took as a preposition is also an
7557 * adjective or noun, go back and treat it as such.
7558 */
7559 if (swapObj &&
7560 ((typelist[cur-1] & VOCT_NOUN)
7561 || (typelist[cur-1] & VOCT_ADJ)))
7562 {
7563 --cur;
7564 swapObj = FALSE;
7565 cmdPrep = MCMONINV;
7566 goto retry_swap;
7567 }
7568
7569 bad_sentence:
7570 /* find the last word */
7571 while (cmd[cur]) ++cur;
7572
7573 /* try running the sentence through preparseCmd */
7574 err = try_preparse_cmd(ctx, &cmd[first_word], cur - first_word,
7575 &preparse_list);
7576 switch(err)
7577 {
7578 case 0:
7579 /* preparseCmd didn't do anything - the sentence fails */
7580 if (!try_unknown_verb(ctx, cmdActor, &cmd[first_word],
7581 &typelist[first_word], 0, &next_start,
7582 TRUE, VOCERR(18),
7583 "I don't understand that sentence."))
7584 {
7585 /* error - abort the command */
7586 retval = 1;
7587 goto done;
7588 }
7589 else
7590 {
7591 /* success - go back for more */
7592 continue;
7593 }
7594
7595 case ERR_PREPRSCMDCAN:
7596 /* they cancelled - we're done with the sentence */
7597 retval = 0;
7598 goto done;
7599
7600 case ERR_PREPRSCMDREDO:
7601 /* reparse with the new sentence */
7602 continue;
7603 }
7604 }
7605
7606 /* see if we want to execute the command now */
7607 if (vocready(ctx, cmd, typelist, cur, cmdActor, cmdPrep,
7608 vverb, vprep,
7609 swapObj ? iolist : dolist,
7610 swapObj ? dolist : iolist,
7611 &err, cmdbuf, first_word, &preparse_list,
7612 &next_start))
7613 continue;
7614
7615 /*
7616 * Check for an indirect object, which may or may not be preceded
7617 * by a preposition. (Note that the lack of a preposition implies
7618 * that the object we already found is the indirect object, and the
7619 * next object is the direct object. It also implies a preposition
7620 * of "to.")
7621 */
7622 if (cmdPrep == MCMONINV && (typelist[cur] & VOCT_PREP))
7623 {
7624 char *p1 = cmd[cur++];
7625
7626 /* if this is the end of the sentence, add the prep to the verb */
7627 if (cmd[cur] == (char *)0
7628 || vocspec(cmd[cur], VOCW_AND)
7629 || vocspec(cmd[cur], VOCW_THEN))
7630 {
7631 if (vocffw(ctx, vverb, vvlen, p1, (int)strlen(p1), PRP_VERB,
7632 (vocseadef *)0)
7633 && !vprep)
7634 vprep = p1;
7635 else
7636 {
7637 /* call parseUnknownVerb */
7638 if (!try_unknown_verb(ctx, cmdActor, &cmd[first_word],
7639 &typelist[first_word], 0,
7640 &next_start, TRUE, VOCERR(19),
7641 "There are words after your command I couldn't use."))
7642 {
7643 /* error - abandon the command */
7644 retval = 1;
7645 goto done;
7646 }
7647 else
7648 {
7649 /* go back for more */
7650 continue;
7651 }
7652 }
7653
7654 if ((err = execmd(ctx, cmdActor, cmdPrep, vverb, vprep,
7655 dolist, iolist,
7656 &cmd[first_word], &typelist[first_word],
7657 cmdbuf, cur - first_word,
7658 &preparse_list, &next_start)) != 0)
7659 {
7660 retval = 1;
7661 goto done;
7662 }
7663 continue;
7664 }
7665
7666 /*
7667 * If we have no verb preposition already, and we have
7668 * another prep-capable word following this prep-capable
7669 * word, and this preposition aggregates with the verb, take
7670 * it as a sentence of the form "pry box open with crowbar"
7671 * (where the current word is "with"). We also must have at
7672 * least one more word after that, since there will have to
7673 * be an indirect object.
7674 */
7675 if (cmd[cur] && (typelist[cur] & VOCT_PREP) && cmd[cur+1]
7676 && vprep == 0
7677 && vocffw(ctx, vverb, vvlen, p1, (int)strlen(p1), PRP_VERB,
7678 (vocseadef *)0))
7679 {
7680 /*
7681 * check to make sure that the next word, which we're
7682 * about to take for a prep (the "with" in "pry box open
7683 * with crowbar") is actually not part of an object name
7684 * - if it is, use it as the object name rather than as
7685 * the prep
7686 */
7687 if (vocgobj(ctx, cmd, typelist, cur, &next,
7688 FALSE, iolist, FALSE, FALSE, 0) <= 0)
7689 {
7690 /* aggregate the first preposition into the verb */
7691 vprep = p1;
7692
7693 /* use the current word as the object-introducing prep */
7694 p1 = cmd[cur++];
7695 }
7696 }
7697
7698 /* try for an indirect object */
7699 cnt = vocgobj(ctx, cmd, typelist, cur, &next, TRUE, iolist,
7700 TRUE, FALSE, &no_match);
7701 if (cnt > 0)
7702 {
7703 cur = next;
7704 v = vocffw(ctx, p1, (int)strlen(p1), (char *)0, 0, PRP_PREP,
7705 (vocseadef *)0);
7706 if (v == (vocwdef *)0)
7707 {
7708 /* let parseUnknownVerb handle it */
7709 if (!try_unknown_verb(ctx, cmdActor, &cmd[first_word],
7710 &typelist[first_word], 0,
7711 &next_start, TRUE, VOCERR(20),
7712 "I don't know how to use the word \"%s\" like that.", p1))
7713 {
7714 /* error - abort the command */
7715 retval = 1;
7716 goto done;
7717 }
7718 else
7719 {
7720 /* go on to the next command */
7721 continue;
7722 }
7723 }
7724 cmdPrep = v->vocwobj;
7725
7726 if (vocready(ctx, cmd, typelist, cur, cmdActor, cmdPrep,
7727 vverb, vprep, dolist, iolist, &err, cmdbuf,
7728 first_word, &preparse_list, &next_start))
7729 continue;
7730 else if ((typelist[cur] & VOCT_PREP) &&
7731 vocffw(ctx, vverb, vvlen, cmd[cur],
7732 (int)strlen(cmd[cur]), PRP_VERB,
7733 (vocseadef *)0) && !vprep)
7734 {
7735 vprep = cmd[cur++];
7736 if (vocready(ctx, cmd, typelist, cur, cmdActor,
7737 cmdPrep, vverb, vprep, dolist, iolist,
7738 &err, cmdbuf, first_word, &preparse_list,
7739 &next_start))
7740 continue;
7741 else
7742 {
7743 /* let parseUnknownVerb handle it */
7744 if (!try_unknown_verb(ctx, cmdActor, &cmd[first_word],
7745 &typelist[first_word], 0,
7746 &next_start, TRUE, VOCERR(19),
7747 "There are words after your command I couldn't use."))
7748 {
7749 /* error - abandon the command */
7750 retval = 1;
7751 goto done;
7752 }
7753 else
7754 {
7755 /* go on to the next command */
7756 continue;
7757 }
7758 }
7759 }
7760 else
7761 {
7762 /*
7763 * If the latter object phrase is flagged with the
7764 * "trimmed preposition" flag, meaning that we could
7765 * have used the preposition in the noun phrase but
7766 * assumed instead it was part of the verb, reverse
7767 * this assumption now: add the preposition back to the
7768 * noun phrase and explain that there's no such thing
7769 * present.
7770 *
7771 * Otherwise, we simply have an unknown verb phrasing,
7772 * so let parseUnknownVerb handle it.
7773 */
7774 vocoldef *np1 =
7775 (dolist[0].vocolflg & VOCS_TRIMPREP) != 0
7776 ? &dolist[0]
7777 : (iolist[0].vocolflg & VOCS_TRIMPREP) != 0
7778 ? &iolist[0]
7779 : 0;
7780 if (np1 != 0)
7781 {
7782 char namebuf[VOCBUFSIZ];
7783
7784 /* show the name, adding the prep back in */
7785 voc_make_obj_name_from_list(
7786 ctx, namebuf, cmd, np1->vocolfst, np1->vocolhlst);
7787 vocerr(ctx, VOCERR(9), "I don't see any %s here.",
7788 namebuf);
7789
7790 /* error - abort */
7791 retval = 1;
7792 goto done;
7793 }
7794 else if (!try_unknown_verb(
7795 ctx, cmdActor,
7796 &cmd[first_word], &typelist[first_word],
7797 0, &next_start, TRUE, VOCERR(19),
7798 "There are words after your command that I couldn't use."))
7799 {
7800 /* error - abort */
7801 retval = 1;
7802 goto done;
7803 }
7804 else
7805 {
7806 /* continue with the next command */
7807 continue;
7808 }
7809 }
7810 }
7811 else if (cnt < 0)
7812 {
7813 /*
7814 * the noun phrase syntax was invalid - we've already
7815 * displayed an error about it, so simply return failure
7816 */
7817 retval = 1;
7818 goto done;
7819 }
7820 else if (no_match)
7821 {
7822 /*
7823 * we found a valid noun phrase, but we didn't find any
7824 * matching objects - we've already generated an error,
7825 * so simply return failure
7826 */
7827 retval = 1;
7828 goto done;
7829 }
7830 else
7831 {
7832 goto bad_sentence;
7833 }
7834 }
7835 else if ((cnt = vocchknoun(ctx, cmd, typelist, cur,
7836 &next, iolist, FALSE)) > 0)
7837 {
7838 /* look for prep at end of command */
7839 cur = next;
7840 if (cmd[cur])
7841 {
7842 if ((typelist[cur] & VOCT_PREP) &&
7843 vocffw(ctx, vverb, vvlen, cmd[cur],
7844 (int)strlen(cmd[cur]), PRP_VERB,
7845 (vocseadef *)0) && !vprep)
7846 {
7847 vprep = cmd[cur++];
7848 }
7849 }
7850
7851 /* the command should definitely be done now */
7852 if (cmd[cur] != 0)
7853 {
7854 /* let parseUnknownVerb handle it */
7855 if (!try_unknown_verb(ctx, cmdActor, &cmd[first_word],
7856 &typelist[first_word], 0,
7857 &next_start, TRUE, VOCERR(21),
7858 "There appear to be extra words after your command."))
7859 {
7860 /* error - stop the command */
7861 retval = 1;
7862 goto done;
7863 }
7864 else
7865 {
7866 /* go on to the next command */
7867 continue;
7868 }
7869 }
7870
7871 /*
7872 * If we don't have a preposition yet, we need to find the
7873 * verb's default. If the verb object has a nilPrep
7874 * property defined, use that prep object; otherwise, look
7875 * up the word "to" and use that.
7876 */
7877 if (cmdPrep == MCMONINV &&
7878 (v = vocffw(ctx, vverb, vvlen,
7879 vprep, (vprep ? (int)strlen(vprep) : 0),
7880 PRP_VERB, (vocseadef *)0)) != 0)
7881 {
7882 runppr(ctx->voccxrun, v->vocwobj, PRP_NILPREP, 0);
7883 if (runtostyp(ctx->voccxrun) == DAT_OBJECT)
7884 cmdPrep = runpopobj(ctx->voccxrun);
7885 else
7886 rundisc(ctx->voccxrun);
7887 }
7888
7889 /* if we didn't find anything with nilPrep, find "to" */
7890 if (cmdPrep == MCMONINV)
7891 {
7892 v = vocffw(ctx, "to", 2, (char *)0, 0, PRP_PREP,
7893 (vocseadef *)0);
7894 if (v) cmdPrep = v->vocwobj;
7895 }
7896
7897 /* execute the command */
7898 err = execmd(ctx, cmdActor, cmdPrep, vverb, vprep,
7899 iolist, dolist,
7900 &cmd[first_word], &typelist[first_word], cmdbuf,
7901 cur - first_word, &preparse_list, &next_start);
7902 continue;
7903 }
7904 else if (cnt < 0)
7905 {
7906 retval = 1;
7907 goto done;
7908 }
7909 else
7910 {
7911 goto bad_sentence;
7912 }
7913 }
7914
7915 done:
7916 /* copy back the command if we need to redo */
7917 if (ctx->voccxredo && cmdbuf != origcmdbuf)
7918 strcpy(origcmdbuf, cmdbuf);
7919
7920 /* return the status */
7921 VOC_RETVAL(ctx, save_sp, retval);
7922 }
7923
7924 /* execute a player command */
7925 int voccmd(voccxdef *ctx, char *cmd, uint cmdlen)
7926 {
7927 int wrdcnt;
7928 int cur;
7929 int next;
7930 char *buffer;
7931 char **wordlist;
7932 objnum cmdActor;
7933 int first;
7934
7935 /*
7936 * Make sure the stack is set up, resetting the stack on entry. Note
7937 * that this makes this routine non-reentrant - recursively calling
7938 * this routine will wipe out the enclosing caller's stack.
7939 */
7940 voc_stk_ini(ctx, (uint)VOC_STACK_SIZE);
7941
7942 /* allocate our stack arrays */
7943 VOC_STK_ARRAY(ctx, char, buffer, 2*VOCBUFSIZ);
7944 VOC_STK_ARRAY(ctx, char *, wordlist, VOCBUFSIZ);
7945
7946 /* until further notice, the actor for formatStrings is Me */
7947 tiosetactor(ctx->voccxtio, ctx->voccxme);
7948
7949 /* clear the 'ignore' flag */
7950 ctx->voccxflg &= ~VOCCXFCLEAR;
7951
7952 /* send to game function 'preparse' */
7953 if (ctx->voccxpre != MCMONINV)
7954 {
7955 int typ;
7956 uchar *s;
7957 size_t len;
7958 int err;
7959
7960 /* push the argument string */
7961 runpstr(ctx->voccxrun, cmd, (int)strlen(cmd), 0);
7962
7963 /* presume no error will occur */
7964 err = 0;
7965
7966 /* catch errors that occur during preparse() */
7967 ERRBEGIN(ctx->voccxerr)
7968 {
7969 /* call preparse() */
7970 runfn(ctx->voccxrun, ctx->voccxpre, 1);
7971 }
7972 ERRCATCH(ctx->voccxerr, err)
7973 {
7974 /* see what we have */
7975 switch(err)
7976 {
7977 case ERR_RUNEXIT:
7978 case ERR_RUNEXITOBJ:
7979 case ERR_RUNABRT:
7980 /*
7981 * if we encountered abort, exit, or exitobj, treat it
7982 * the same as a nil result code - simply cancel the
7983 * entire current command
7984 */
7985 break;
7986
7987 default:
7988 /* re-throw any other error */
7989 errrse(ctx->voccxerr);
7990 }
7991 }
7992 ERREND(ctx->voccxerr);
7993
7994 /* if an error occurred, abort the command */
7995 if (err != 0)
7996 return 0;
7997
7998 /* check the return value's type */
7999 typ = runtostyp(ctx->voccxrun);
8000 if (typ == DAT_SSTRING)
8001 {
8002 /*
8003 * It's a string - replace the command with the new string.
8004 * Pop the new string and scan its length prefix.
8005 */
8006 s = runpopstr(ctx->voccxrun);
8007 len = osrp2(s) - 2;
8008 s += 2;
8009
8010 /*
8011 * limit the size of the command to our buffer length,
8012 * leaving space for null termination
8013 */
8014 if (len > cmdlen - 1)
8015 len = cmdlen - 1;
8016
8017 /* copy the new command string into our command buffer */
8018 memcpy(cmd, s, len);
8019
8020 /* null-terminate the command buffer */
8021 cmd[len] = '\0';
8022 }
8023 else
8024 {
8025 /* the value isn't important for any other type */
8026 rundisc(ctx->voccxrun);
8027
8028 /* if they returned nil, we're done processing the command */
8029 if (typ == DAT_NIL)
8030 return 0;
8031 }
8032 }
8033
8034 /* break up into individual words */
8035 if ((wrdcnt = voctok(ctx, cmd, buffer, wordlist, TRUE, FALSE, TRUE)) > 0)
8036 {
8037 /* skip any leading "and" and "then" separators */
8038 for (cur = 0 ; cur < wrdcnt ; ++cur)
8039 {
8040 /* if this isn't "and" or "then", we're done scanning */
8041 if (!vocspec(wordlist[cur], VOCW_THEN)
8042 && !vocspec(wordlist[cur], VOCW_AND))
8043 break;
8044 }
8045 }
8046
8047 /*
8048 * if we found no words, or we found only useless leading "and" and
8049 * "then" separators, run the "pardon" function to tell the player
8050 * that we didn't find any command on the line
8051 */
8052 if (wrdcnt == 0 || (wrdcnt > 0 && cur >= wrdcnt))
8053 {
8054 runfn(ctx->voccxrun, ctx->voccxprd, 0);
8055 return 0;
8056 }
8057
8058 /*
8059 * if we got an error tokenizing the word list, return - we've
8060 * already displayed an error message, so there's nothing more for
8061 * us to do here
8062 */
8063 if (wrdcnt < 0)
8064 return 0;
8065
8066 /* process the commands on the line */
8067 for (first = TRUE, cmdActor = ctx->voccxactor = MCMONINV ; cur < wrdcnt ;
8068 ++cur, first = FALSE)
8069 {
8070 /* presume we won't find an unknown word */
8071 ctx->voccxunknown = 0;
8072
8073 /* find the THEN that ends the command, if there is one */
8074 for (next = cur ; cur < wrdcnt && !vocspec(wordlist[cur], VOCW_THEN)
8075 ; ++cur) ;
8076 wordlist[cur] = (char *)0;
8077
8078 /* process until we run out of work to do */
8079 for (;;)
8080 {
8081 /* try processing the command */
8082 if (voc1cmd(ctx, &wordlist[next], cmd, &cmdActor, first))
8083 {
8084 /*
8085 * If the unknown word flag is set, try the command
8086 * again from the beginning. This flag means that we
8087 * tried using parseUnknownDobj/Iobj to resolve the
8088 * unknown word, but that failed and we need to try
8089 * again with the normal "oops" processing.
8090 */
8091 if (ctx->voccxunknown > 0)
8092 continue;
8093
8094 /* return an error */
8095 return 1;
8096 }
8097
8098 /* success - we're done with the command */
8099 break;
8100 }
8101
8102 /* if the rest of the command is to be ignored, we're done */
8103 if (ctx->voccxflg & VOCCXFCLEAR)
8104 return 0;
8105
8106 /* scan past any separating AND's and THEN's */
8107 while (cur + 1 < wrdcnt
8108 && (vocspec(wordlist[cur+1], VOCW_THEN)
8109 || vocspec(wordlist[cur+1], VOCW_AND)))
8110 ++cur;
8111
8112 /*
8113 * if another command follows, add a blank line to separate the
8114 * results from the previous command and those from the next
8115 * command
8116 */
8117 if (cur + 1 < wrdcnt)
8118 outformat("\\b");
8119 }
8120
8121 /* done */
8122 return 0;
8123 }
8124
8125
8126 /*
8127 * Off-stack stack management
8128 */
8129
8130 /* allocate/reset the stack */
8131 void voc_stk_ini(voccxdef *ctx, uint siz)
8132 {
8133 /* allocate it if it's not already allocated */
8134 if (ctx->voc_stk_ptr == 0)
8135 {
8136 ctx->voc_stk_ptr = mchalo(ctx->voccxerr, siz, "voc_stk_ini");
8137 ctx->voc_stk_end = ctx->voc_stk_ptr + siz;
8138 }
8139
8140 /* reset the stack */
8141 ctx->voc_stk_cur = ctx->voc_stk_ptr;
8142 }
8143
8144 /* allocate space from the off-stack stack */
8145 void *voc_stk_alo(voccxdef *ctx, uint siz)
8146 {
8147 void *ret;
8148
8149 /* round size up to allocation units */
8150 siz = osrndsz(siz);
8151
8152 /* if there's not space, signal an error */
8153 if (ctx->voc_stk_cur + siz > ctx->voc_stk_end)
8154 errsig(ctx->voccxerr, ERR_VOCSTK);
8155
8156 /* save the return pointer */
8157 ret = ctx->voc_stk_cur;
8158
8159 /* consume the space */
8160 ctx->voc_stk_cur += siz;
8161
8162 /*#define SHOW_HI*/
8163 #ifdef SHOW_HI
8164 {
8165 static uint maxsiz;
8166 if (ctx->voc_stk_cur - ctx->voc_stk_ptr > maxsiz)
8167 {
8168 char buf[20];
8169
8170 maxsiz = ctx->voc_stk_cur - ctx->voc_stk_ptr;
8171 sprintf(buf, "%u\n", maxsiz);
8172 os_printz(buf);
8173 }
8174 }
8175 #endif
8176
8177
8178 /* return the space */
8179 return ret;
8180 }
8181