Debian Codegraph @

terps/tads/tads2/voc.c

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header: d:/cvsroot/tads/TADS2/VOC.C,v 1.4 1999/07/11 00:46:31 MJRoberts Exp $";
4 #endif
5
6 /*
7 * Copyright (c) 1991, 2002 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 voc.c - vocabulary functions
15 Function
16 Player command parser vocabulary word functions
17 Notes
18 None
19 Modified
20 04/11/99 CNebel - Fix signing errors.
21 11/07/91 MJRoberts - creation
22 */
23
24 #include <ctype.h>
25 #include <string.h>
26
27 #include "os.h"
28 #include "std.h"
29 #include "mch.h"
30 #include "mcm.h"
31 #include "voc.h"
32 #include "prp.h"
33 #include "obj.h"
34 #include "err.h"
35
36
37 /*
38 * Main vocabulary context. This can be saved globally if desired, so
39 * that routines that don't have any other access to it (such as
40 * Unix-style signal handlers) can reach it.
41 */
42 voccxdef *main_voc_ctx = 0;
43
44
45 #ifdef VOCW_IN_CACHE
46 vocwdef *vocwget(voccxdef *ctx, uint idx)
47 {
48 uint pg;
49
50 if (idx == VOCCXW_NONE)
51 return 0;
52
53 /* get the page we need */
54 pg = idx/VOCWPGSIZ;
55
56 /* if it's not locked, lock it */
57 if (pg != ctx->voccxwplck)
58 {
59 /* unlock the old page */
60 if (ctx->voccxwplck != MCMONINV)
61 mcmunlck(ctx->voccxmem, ctx->voccxwp[ctx->voccxwplck]);
62
63 /* lock the new page */
64 ctx->voccxwpgptr = (vocwdef *)mcmlck(ctx->voccxmem,
65 ctx->voccxwp[pg]);
66 ctx->voccxwplck = pg;
67 }
68
69 /* return the entry on that page */
70 return ctx->voccxwpgptr + (idx % VOCWPGSIZ);
71 }
72 #endif /*VOCW_IN_CACHE */
73
74 /* hash value is based on first 6 characters only to allow match-in-6 */
75 uint vochsh(uchar *t, int len)
76 {
77 uint ret = 0;
78
79 if (len > 6) len = 6;
80 for ( ; len ; --len, ++t)
81 ret = (ret + (uint)(vocisupper(*t) ? tolower(*t) : *t))
82 & (VOCHASHSIZ - 1);
83 return(ret);
84 }
85
86 /* copy vocabulary word, and convert to lower case */
87 static void voccpy(uchar *dst, uchar *src, int len)
88 {
89 for ( ; len ; --len, ++dst, ++src)
90 *dst = vocisupper(*src) ? tolower(*src) : *src;
91 }
92
93 /* allocate and set up a new vocwdef record, linking into a vocdef's list */
94 static void vocwset(voccxdef *ctx, vocdef *v, prpnum p, objnum objn,
95 int classflg)
96 {
97 vocwdef *vw;
98 uint inx;
99 vocwdef *vw2;
100
101 /*
102 * look through the vocdef list to see if there's an existing entry
103 * with the DELETED marker -- if so, simply undelete it
104 */
105 for (inx = v->vocwlst, vw = vocwget(ctx, inx) ; vw ;
106 inx = vw->vocwnxt, vw = vocwget(ctx, inx))
107 {
108 /* if this entry was deleted, and otherwise matches, undelete it */
109 if ((vw->vocwflg & VOCFDEL)
110 && vw->vocwobj == objn && vw->vocwtyp == p)
111 {
112 /*
113 * Remove the deleted flag. We will otherwise leave the
114 * flags unchanged, since the VOCFDEL flag applies only to
115 * statically allocated objects, and hence the original
116 * flags should take precedence over any run-time flags.
117 */
118 vw->vocwflg &= ~VOCFDEL;
119
120 /* we're done */
121 return;
122 }
123 }
124
125 /* make sure the word+object+type record isn't already defined */
126 for (inx = v->vocwlst, vw = vocwget(ctx, inx) ; vw ;
127 inx = vw->vocwnxt, vw = vocwget(ctx, inx))
128 {
129 if (vw->vocwobj == objn && vw->vocwtyp == p
130 && (vw->vocwflg & VOCFCLASS) == (classflg & VOCFCLASS))
131 {
132 /* it matches - don't add a redundant record */
133 return;
134 }
135 }
136
137 /* look in the free list for an available vocwdef */
138 if (ctx->voccxwfre != VOCCXW_NONE)
139 {
140 inx = ctx->voccxwfre;
141 vw = vocwget(ctx, inx); /* get the free vocwdef */
142 ctx->voccxwfre = vw->vocwnxt; /* unlink from free list */
143 }
144 else
145 {
146 /* allocate another page of vocwdef's if necssary */
147 if ((ctx->voccxwalocnt % VOCWPGSIZ) == 0)
148 {
149 int pg = ctx->voccxwalocnt / VOCWPGSIZ;
150
151 /* make sure we haven't exceeded the available page count */
152 if (pg >= VOCWPGMAX) errsig(ctx->voccxerr, ERR_VOCMNPG);
153
154 /* allocate on the new page */
155 #ifdef VOCW_IN_CACHE
156 mcmalo(ctx->voccxmem, (ushort)(VOCWPGSIZ * sizeof(vocwdef)),
157 &ctx->voccxwp[pg]);
158 mcmunlck(ctx->voccxmem, ctx->voccxwp[pg]);
159 #else
160 ctx->voccxwp[pg] =
161 (vocwdef *)mchalo(ctx->voccxerr,
162 (VOCWPGSIZ * sizeof(vocwdef)),
163 "vocwset");
164 #endif
165 }
166
167 /* get the next entry, and increment count of used entries */
168 inx = ctx->voccxwalocnt++;
169 vw = vocwget(ctx, inx);
170 }
171
172 /* link the new vocwdef into the vocdef's relation list */
173 vw->vocwnxt = v->vocwlst;
174 v->vocwlst = inx;
175
176 /* set up the new vocwdef */
177 vw->vocwtyp = (uchar)p;
178 vw->vocwobj = objn;
179 vw->vocwflg = classflg;
180
181 /*
182 * Scan the list and make sure we're not adding a redundant verb.
183 * Don't bother with the warning if this is a class.
184 */
185 if (p == PRP_VERB && (ctx->voccxflg & VOCCXFVWARN)
186 && (vw->vocwflg & VOCFCLASS) == 0)
187 {
188 for (vw2 = vocwget(ctx, v->vocwlst) ; vw2 ;
189 vw2 = vocwget(ctx, vw2->vocwnxt))
190 {
191 /*
192 * if this is a different object, and it's not a class, and
193 * it's defined as a verb, warn about it
194 */
195 if (vw2 != vw
196 && (vw2->vocwflg & VOCFCLASS) == 0
197 && vw2->vocwtyp == PRP_VERB)
198 {
199 if (v->vocln2 != 0)
200 errlog2(ctx->voccxerr, ERR_VOCREVB,
201 ERRTSTR,
202 errstr(ctx->voccxerr,
203 (char *)v->voctxt, v->voclen),
204 ERRTSTR,
205 errstr(ctx->voccxerr,
206 (char *)v->voctxt + v->voclen, v->vocln2));
207 else
208 errlog1(ctx->voccxerr, ERR_VOCREVB,
209 ERRTSTR,
210 errstr(ctx->voccxerr,
211 (char *)v->voctxt, v->voclen));
212 break;
213 }
214 }
215 }
216 }
217
218 /* set up a vocdef record, and link into hash table */
219 static void vocset(voccxdef *ctx, vocdef *v, prpnum p, objnum objn,
220 int classflg, uchar *wrdtxt, int len,
221 uchar *wrd2, int len2)
222 {
223 uint hshval = vochsh(wrdtxt, len);
224
225 v->vocnxt = ctx->voccxhsh[hshval];
226 ctx->voccxhsh[hshval] = v;
227
228 v->voclen = len;
229 v->vocln2 = len2;
230 voccpy(v->voctxt, wrdtxt, len);
231 if (wrd2) voccpy(v->voctxt + len, wrd2, len2);
232
233 /* allocate and initialize a vocwdef for the object */
234 vocwset(ctx, v, p, objn, classflg);
235 }
236
237 /* internal addword - already parsed into two words and have lengths */
238 void vocadd2(voccxdef *ctx, prpnum p, objnum objn, int classflg,
239 uchar *wrdtxt, int len, uchar *wrd2, int len2)
240 {
241 vocdef *v;
242 vocdef *prv;
243 uint need;
244 uint hshval;
245
246 /* if the word is null, ignore it entirely */
247 if (len == 0 && len2 == 0)
248 return;
249
250 /* look for a vocdef entry with the same word text */
251 hshval = vochsh(wrdtxt, len);
252 for (v = ctx->voccxhsh[hshval] ; v ; v = v->vocnxt)
253 {
254 /* if it matches on both words, use this entry */
255 if (v->voclen == len && !memcmp(wrdtxt, v->voctxt, (size_t)len)
256 && ((!wrd2 && v->vocln2 == 0)
257 || (v->vocln2 == len2 &&
258 !memcmp(wrd2, v->voctxt + len, (size_t)len2))))
259 {
260 vocwset(ctx, v, p, objn, classflg);
261 return;
262 }
263 }
264
265 /* look for a free vocdef entry of the same size */
266 for (prv = (vocdef *)0, v = ctx->voccxfre ; v ; prv = v, v = v->vocnxt)
267 if (v->voclen == len + len2) break;
268
269 if (v)
270 {
271 /* we found something - unlink from free list */
272 if (prv) prv->vocnxt = v->vocnxt;
273 else ctx->voccxfre = v->vocnxt;
274
275 /* reuse the entry */
276 v->vocwlst = VOCCXW_NONE;
277 vocset(ctx, v, p, objn, classflg, wrdtxt, len, wrd2, len2);
278 return;
279 }
280
281 /* didn't find an existing vocdef; allocate a new one */
282 need = sizeof(vocdef) + len + len2 - 1;
283 if (ctx->voccxrem < need)
284 {
285 /* not enough space in current page; allocate a new one */
286 ctx->voccxpool = mchalo(ctx->voccxerr, VOCPGSIZ, "vocadd2");
287 ctx->voccxrem = VOCPGSIZ;
288 }
289
290 /* use top of current pool, and update pool pointer and size */
291 v = (vocdef *)ctx->voccxpool;
292 need = osrndsz(need);
293 ctx->voccxpool += need;
294 if (ctx->voccxrem > need) ctx->voccxrem -= need;
295 else ctx->voccxrem = 0;
296
297 /* set up new vocdef */
298 v->vocwlst = VOCCXW_NONE;
299 vocset(ctx, v, p, objn, classflg, wrdtxt, len, wrd2, len2);
300 }
301
302 static void voc_parse_words(char **wrdtxt, int *len, char **wrd2, int *len2)
303 {
304 /* get length and pointer to actual text */
305 *len = osrp2(*wrdtxt) - 2;
306 *wrdtxt += 2;
307
308 /* see if there's a second word - look for a space */
309 for (*wrd2 = *wrdtxt, *len2 = *len ; *len2 && !vocisspace(**wrd2) ;
310 ++*wrd2, --*len2) ;
311 if (*len2)
312 {
313 *len -= *len2;
314 while (*len2 && vocisspace(**wrd2)) ++*wrd2, --*len2;
315 }
316 else
317 {
318 /* no space ==> no second word */
319 *wrd2 = (char *)0;
320 }
321 }
322
323 void vocadd(voccxdef *ctx, prpnum p, objnum objn, int classflg, char *wrdtxt)
324 {
325 int len;
326 char *wrd2;
327 int len2;
328
329 voc_parse_words(&wrdtxt, &len, &wrd2, &len2);
330 vocadd2(ctx, p, objn, classflg, (uchar *)wrdtxt, len, (uchar *)wrd2, len2);
331 }
332
333 /* make sure we have a page table entry for an object, allocating one if not */
334 void vocialo(voccxdef *ctx, objnum obj)
335 {
336 if (!ctx->voccxinh[obj >> 8])
337 {
338 ctx->voccxinh[obj >> 8] =
339 (vocidef **)mchalo(ctx->voccxerr,
340 (256 * sizeof(vocidef *)), "vocialo");
341 memset(ctx->voccxinh[obj >> 8], 0, (size_t)(256 * sizeof(vocidef *)));
342 }
343 }
344
345 /* add an inheritance/location record */
346 void vociadd(voccxdef *ctx, objnum obj, objnum loc,
347 int numsc, objnum *sc, int flags)
348 {
349 vocidef *v;
350 vocidef *min;
351 vocidef *prv;
352 vocidef *minprv;
353
354 /* make sure we have a page table entry for this object */
355 vocialo(ctx, obj);
356
357 /* look in free list for an entry that's big enough */
358 for (prv = (vocidef *)0, min = (vocidef *)0, v = ctx->voccxifr ; v ;
359 prv = v, v = v->vocinxt)
360 {
361 if (v->vocinsc == numsc)
362 {
363 min = v;
364 minprv = prv;
365 break;
366 }
367 else if (v->vocinsc > numsc)
368 {
369 if (!min || v->vocinsc < min->vocinsc)
370 {
371 min = v;
372 minprv = prv;
373 }
374 }
375 }
376
377 if (!min)
378 {
379 uint need;
380
381 /* nothing in free list; allocate a new entry */
382 need = osrndsz(sizeof(vocidef) + (numsc - 1)*sizeof(objnum));
383 if (ctx->voccxilst + need >= VOCISIZ)
384 {
385 /* nothing left on current page; allocate a new page */
386 ctx->voccxip[++(ctx->voccxiplst)] =
387 mchalo(ctx->voccxerr, VOCISIZ, "vociadd");
388 ctx->voccxilst = 0;
389 }
390
391 /* allocate space out of current page */
392 v = (vocidef *)(ctx->voccxip[ctx->voccxiplst] + ctx->voccxilst);
393 ctx->voccxilst += need;
394 }
395 else
396 {
397 /* unlink from chain and use */
398 v = min;
399 if (minprv)
400 minprv->vocinxt = v->vocinxt;
401 else
402 ctx->voccxifr = v->vocinxt;
403 }
404
405 /* set up the entry */
406 if (vocinh(ctx, obj) != (vocidef *)0) errsig(ctx->voccxerr, ERR_VOCINUS);
407 v->vociloc = loc;
408 v->vociilc = MCMONINV;
409 v->vociflg = (flags & ~VOCIFXLAT);
410 v->vocinsc = numsc;
411 if (numsc)
412 {
413 if (flags & VOCIFXLAT)
414 {
415 int i;
416
417 for (i = 0 ; i < numsc ; ++i)
418 v->vocisc[i] = osrp2(&sc[i]);
419 }
420 else
421 memcpy(v->vocisc, sc, (size_t)(numsc * sizeof(objnum)));
422 }
423 vocinh(ctx, obj) = v; /* set page table entry */
424 }
425
426 /* revert all objects to original state, using inheritance records */
427 void vocrevert(voccxdef *vctx)
428 {
429 vocidef ***vpg;
430 vocidef **v;
431 int i;
432 int j;
433 objnum obj;
434
435 /*
436 * Go through the inheritance records. Delete each dynamically
437 * allocated object, and revert each static object to its original
438 * load state.
439 */
440 for (vpg = vctx->voccxinh, i = 0 ; i < VOCINHMAX ; ++vpg, ++i)
441 {
442 if (!*vpg) continue;
443 for (v = *vpg, obj = (i << 8), j = 0 ; j < 256 ; ++v, ++obj, ++j)
444 {
445 if (*v)
446 {
447 /* if the object was dynamically allocated, delete it */
448 if ((*v)->vociflg & VOCIFNEW)
449 {
450 /* delete vocabulary and inheritance data for the object */
451 vocidel(vctx, obj);
452 vocdel(vctx, obj);
453
454 /* delete the object */
455 mcmfre(vctx->voccxmem, (mcmon)obj);
456 }
457 else
458 {
459 /* revert the object */
460 mcmrevert(vctx->voccxmem, (mcmon)obj);
461 }
462 }
463 }
464 }
465
466 /*
467 * Revert the vocabulary list: delete all newly added words, and
468 * undelete all original words marked as deleted.
469 */
470 vocdel1(vctx, MCMONINV, (char *)0, 0, TRUE, TRUE, FALSE);
471 }
472
473 /* initialize voc context */
474 void vocini(voccxdef *vocctx, errcxdef *errctx, mcmcxdef *memctx,
475 runcxdef *runctx, objucxdef *undoctx,
476 int fuses, int daemons, int notifiers)
477 {
478 CLRSTRUCT(*vocctx);
479 vocctx->voccxerr = errctx;
480 vocctx->voccxiplst = (uint)-1;
481 vocctx->voccxilst = VOCISIZ;
482 vocctx->voccxmem = memctx;
483 vocctx->voccxrun = runctx;
484 vocctx->voccxundo = undoctx;
485
486 vocctx->voccxme =
487 vocctx->voccxme_init =
488 vocctx->voccxvtk =
489 vocctx->voccxstr =
490 vocctx->voccxnum =
491 vocctx->voccxit =
492 vocctx->voccxhim =
493 vocctx->voccxprd =
494 vocctx->voccxpre =
495 vocctx->voccxpre2 =
496 vocctx->voccxppc =
497 vocctx->voccxlsv =
498 vocctx->voccxpreinit =
499 vocctx->voccxper =
500 vocctx->voccxprom =
501 vocctx->voccxpostprom =
502 vocctx->voccxpdis =
503 vocctx->voccxper2 =
504 vocctx->voccxperp =
505 vocctx->voccxpdef =
506 vocctx->voccxpdef2 =
507 vocctx->voccxpask =
508 vocctx->voccxpask2 =
509 vocctx->voccxpask3 =
510 vocctx->voccxinitrestore =
511 vocctx->voccxpuv =
512 vocctx->voccxpnp =
513 vocctx->voccxpostact =
514 vocctx->voccxendcmd =
515 vocctx->voccxher = MCMONINV;
516 vocctx->voccxthc = 0;
517 #ifdef VOCW_IN_CACHE
518 vocctx->voccxwplck = MCMONINV;
519 #endif
520
521 vocctx->voccxactor = MCMONINV;
522 vocctx->voccxverb = MCMONINV;
523 vocctx->voccxprep = MCMONINV;
524 vocctx->voccxdobj = 0;
525 vocctx->voccxiobj = 0;
526
527 vocctx->voccxunknown = 0;
528 vocctx->voccxlastunk = 0;
529
530 vocctx->voc_stk_ptr = 0;
531 vocctx->voc_stk_cur = 0;
532 vocctx->voc_stk_end = 0;
533
534 /* allocate fuses, daemons, notifiers */
535 vocinialo(vocctx, &vocctx->voccxfus, (vocctx->voccxfuc = fuses));
536 vocinialo(vocctx, &vocctx->voccxdmn, (vocctx->voccxdmc = daemons));
537 vocinialo(vocctx, &vocctx->voccxalm, (vocctx->voccxalc = notifiers));
538
539 /* no entries in vocwdef free list yet */
540 vocctx->voccxwfre = VOCCXW_NONE;
541 }
542
543 /* uninitialize the voc context */
544 void vocterm(voccxdef *ctx)
545 {
546 /* delete the fuses, daemons, and notifiers */
547 voctermfree(ctx->voccxfus);
548 voctermfree(ctx->voccxdmn);
549 voctermfree(ctx->voccxalm);
550
551 /* delete the private stack */
552 if (ctx->voc_stk_ptr != 0)
553 mchfre(ctx->voc_stk_ptr);
554 }
555
556 /* clean up the vocab context */
557 void voctermfree(vocddef *what)
558 {
559 if (what != 0)
560 mchfre(what);
561 }
562
563 /*
564 * Iterate through all words for a particular object, calling a
565 * function with each vocwdef found. If objn == MCMONINV, we'll call
566 * the callback for every word.
567 */
568 void voc_iterate(voccxdef *ctx, objnum objn,
569 void (*fn)(void *, vocdef *, vocwdef *), void *fnctx)
570 {
571 int i;
572 vocdef *v;
573 vocdef **vp;
574 vocwdef *vw;
575 uint idx;
576
577 /* go through each hash value looking for matching words */
578 for (i = VOCHASHSIZ, vp = ctx->voccxhsh ; i ; ++vp, --i)
579 {
580 /* go through all words in this hash chain */
581 for (v = *vp ; v ; v = v->vocnxt)
582 {
583 /* go through each object relation for this word */
584 for (idx = v->vocwlst, vw = vocwget(ctx, idx) ; vw ;
585 idx = vw->vocwnxt, vw = vocwget(ctx, idx))
586 {
587 /*
588 * if this word is for this object, call the callback
589 */
590 if (objn == MCMONINV || vw->vocwobj == objn)
591 (*fn)(fnctx, v, vw);
592 }
593 }
594 }
595 }
596
597 /* callback context for voc_count */
598 struct voc_count_ctx
599 {
600 int cnt;
601 int siz;
602 prpnum prp;
603 };
604
605 /* callback for voc_count */
606 static void voc_count_cb(void *ctx0, vocdef *voc, vocwdef *vocw)
607 {
608 struct voc_count_ctx *ctx = (struct voc_count_ctx *)ctx0;
609
610 VARUSED(vocw);
611
612 /*
613 * If it matches the property (or we want all properties), count
614 * it. Don't count deleted objects.
615 */
616 if ((ctx->prp == 0 || ctx->prp == vocw->vocwtyp)
617 && !(vocw->vocwflg & VOCFDEL))
618 {
619 /* count the word */
620 ctx->cnt++;
621
622 /* count the size */
623 ctx->siz += voc->voclen + voc->vocln2;
624 }
625 }
626
627 /*
628 * Get the number and size of words defined for an object. The size
629 * returns the total byte count from all the words involved. Do not
630 * include deleted words in the count.
631 */
632 void voc_count(voccxdef *ctx, objnum objn, prpnum prp, int *cnt, int *siz)
633 {
634 struct voc_count_ctx fnctx;
635
636 /* set up the context with zero initial counts */
637 fnctx.cnt = 0;
638 fnctx.siz = 0;
639 fnctx.prp = prp;
640
641 /* iterate over all words for the object with our callback */
642 voc_iterate(ctx, objn, voc_count_cb, &fnctx);
643
644 /* return the data */
645 if (cnt) *cnt = fnctx.cnt;
646 if (siz) *siz = fnctx.siz;
647 }
648
649
650 /*
651 * Delete a particular word associated with an object, or all words if
652 * the word pointer is null. If the word isn't marked as added at
653 * runtime (i.e., the VOCFNEW flag is not set for the word), the word is
654 * simply marked as deleted, rather than being actually deleted.
655 * However, if the 'really_delete' flag is set, the word is actually
656 * deleted. If the 'revert' flag is true, this routine deletes _every_
657 * dynamically created word, and undeletes all dynamically deleted words
658 * that were in the original vocabulary.
659 */
660 void vocdel1(voccxdef *ctx, objnum objn, char *wrd1, prpnum prp,
661 int really_delete, int revert, int keep_undo)
662 {
663 int i;
664 vocdef *v;
665 vocdef *prv;
666 vocdef *nxt;
667 vocdef **vp;
668 vocwdef *vw;
669 vocwdef *prvw;
670 vocwdef *nxtw;
671 uint nxtidx;
672 uint idx;
673 int deleted_vocdef;
674 char *wrd2;
675 int len1, len2;
676 int do_del;
677 char *orgwrd;
678
679 /* parse the word if provided */
680 orgwrd = wrd1;
681 if (wrd1)
682 voc_parse_words(&wrd1, &len1, &wrd2, &len2);
683
684 /* go through each hash value looking for matching words */
685 for (i = VOCHASHSIZ, vp = ctx->voccxhsh ; i ; ++vp, --i)
686 {
687 /* go through all words in this hash chain */
688 for (prv = (vocdef *)0, v = *vp ; v ; v = nxt)
689 {
690 /* remember next word in hash chain */
691 nxt = v->vocnxt;
692
693 /* if this word doesn't match, skip it */
694 if (wrd1)
695 {
696 /* compare the first word */
697 if (v->voclen != len1
698 || memicmp((char *)v->voctxt, wrd1, (size_t)len1))
699 {
700 prv = v;
701 continue;
702 }
703
704 /* if there's a second word, compare it as well */
705 if (wrd2 && (v->vocln2 != len2
706 || memicmp((char *)v->voctxt + len1,
707 wrd2, (size_t)len2)))
708 {
709 prv = v;
710 continue;
711 }
712 }
713
714 /* presume we're not going to delete this vocdef */
715 deleted_vocdef = FALSE;
716
717 /* go through all object relations for this word */
718 for (prvw = 0, idx = v->vocwlst, vw = vocwget(ctx, idx) ; vw ;
719 vw = nxtw, idx = nxtidx)
720 {
721 /* remember next word in relation list */
722 nxtidx = vw->vocwnxt;
723 nxtw = vocwget(ctx, nxtidx);
724
725 /*
726 * figure out whether to delete this word, based on the
727 * caller's specified operating mode
728 */
729 if (revert)
730 {
731 /* if reverting, delete all new words */
732 do_del = (vw->vocwflg & VOCFNEW);
733
734 /* also, remove the DELETED flag if present */
735 vw->vocwflg &= ~VOCFDEL;
736 }
737 else
738 {
739 /*
740 * delete the word if the object number matches,
741 * AND either we're not searching for a specific
742 * vocabulary word (in which case wrd1 will be null)
743 * or the word matches the vocabulary word we're
744 * seeking (in which case the part of speech must
745 * match)
746 */
747 do_del = (vw->vocwobj == objn
748 && (wrd1 == 0 || vw->vocwtyp == prp));
749
750 /*
751 * if we're not in really_delete mode, and the word
752 * matches and it wasn't dynamically added at
753 * run-time, simply mark it as deleted -- this will
754 * allow it to be undeleted if the game is reverted
755 * to RESTART conditions
756 */
757 if (do_del && !really_delete && !(vw->vocwflg & VOCFNEW))
758 {
759 /* geneate undo for the operation */
760 if (keep_undo && orgwrd)
761 vocdusave_delwrd(ctx, objn, prp,
762 vw->vocwflg, orgwrd);
763
764 /* just mark the word for deletion, but keep vw */
765 vw->vocwflg |= VOCFDEL;
766 do_del = FALSE;
767 }
768 }
769
770 /* now delete the structure if we decided we should */
771 if (do_del)
772 {
773 /* geneate undo for the operation */
774 if (keep_undo && orgwrd)
775 vocdusave_delwrd(ctx, objn, prp, vw->vocwflg, orgwrd);
776
777 /* unlink this vocwdef from the vocdef's list */
778 if (prvw)
779 prvw->vocwnxt = vw->vocwnxt;
780 else
781 v->vocwlst = vw->vocwnxt;
782
783 /* link the vocwdef into the vocwdef free list */
784 vw->vocwnxt = ctx->voccxwfre;
785 ctx->voccxwfre = idx;
786
787 /*
788 * if there's nothing left in the vocdef's list,
789 * delete the entire vocdef as well
790 */
791 if (v->vocwlst == VOCCXW_NONE)
792 {
793 if (prv) prv->vocnxt = v->vocnxt;
794 else *vp = v->vocnxt;
795
796 /* link into free chain */
797 v->vocnxt = ctx->voccxfre;
798 ctx->voccxfre = v;
799
800 /* note that it's been deleted */
801 deleted_vocdef = TRUE;
802 }
803 }
804 else
805 {
806 /* we're not deleting the word, so move prvw forward */
807 prvw = vw;
808 }
809 }
810
811 /* if we didn't delete this vocdef, move prv forward onto it */
812 if (!deleted_vocdef)
813 prv = v;
814 }
815 }
816 }
817
818 /* delete all vocabulary for an object */
819 void vocdel(voccxdef *ctx, objnum objn)
820 {
821 vocdel1(ctx, objn, (char *)0, (prpnum)0, TRUE, FALSE, FALSE);
822 }
823
824 /* delete object inheritance records for a particular object */
825 void vocidel(voccxdef *ctx, objnum obj)
826 {
827 vocidef *v;
828
829 /* get entry out of page table, and clear page table slot */
830 v = vocinh(ctx, obj);
831 vocinh(ctx, obj) = (vocidef *)0;
832
833 /* link into free list */
834 if (v)
835 {
836 v->vocinxt = ctx->voccxifr;
837 ctx->voccxifr = v;
838 }
839 }
840
841 /*
842 * Find template matching a verb+object+prep combination; return TRUE
843 * if a suitable template is found, FALSE otherwise.
844 */
845 int voctplfnd(voccxdef *ctx, objnum verb_in, objnum prep,
846 uchar *tplout, int *newstyle)
847 {
848 uchar *tplptr;
849 uchar *thistpl;
850 int found;
851 int tplcnt;
852 uint tplofs;
853 objnum verb;
854
855 /* look for a new-style template first */
856 tplofs = objgetap(ctx->voccxmem, verb_in, PRP_TPL2, &verb, FALSE);
857 if (tplofs)
858 {
859 /* flag the presence of the new-style template */
860 *newstyle = TRUE;
861 }
862 else
863 {
864 /* no new-style template - look for old version */
865 tplofs = objgetap(ctx->voccxmem, verb_in, PRP_TPL, &verb, FALSE);
866 *newstyle = FALSE;
867 }
868
869 /* inherit templates until we run out of them */
870 for (;;)
871 {
872 /* if we found something already, use it */
873 if (tplofs)
874 {
875 size_t siz;
876
877 /* figure the size of this template style */
878 siz = (*newstyle ? VOCTPL2SIZ : VOCTPLSIZ);
879
880 /* lock the verb object, and get the property value pointer */
881 tplptr = mcmlck(ctx->voccxmem, verb);
882 thistpl = prpvalp(tplptr + tplofs);
883
884 /* first byte is number of templates in array */
885 tplcnt = *thistpl++;
886
887 /* look for a template that matches the preposition object */
888 for (found = FALSE ; tplcnt ; thistpl += siz, --tplcnt)
889 {
890 if (voctplpr(thistpl) == prep)
891 {
892 found = TRUE;
893 break;
894 }
895 }
896
897 /* unlock the object and return the value if we found one */
898 mcmunlck(ctx->voccxmem, verb);
899 if (found)
900 {
901 memcpy(tplout, thistpl, siz);
902 return(TRUE);
903 }
904 }
905
906 /* try inheriting a template (new-style, then old-style) */
907 tplofs = objgetap(ctx->voccxmem, verb_in, PRP_TPL2, &verb, TRUE);
908 if (tplofs)
909 *newstyle = TRUE;
910 else
911 {
912 tplofs = objgetap(ctx->voccxmem, verb_in, PRP_TPL, &verb, TRUE);
913 *newstyle = FALSE;
914 }
915
916 /* return not-found if we couldn't inherit it */
917 if (!tplofs)
918 return FALSE;
919
920 /* use the newly found verb */
921 verb_in = verb;
922 }
923 }
924
925 /*
926 * Set the "Me" object
927 */
928 void voc_set_me(voccxdef *ctx, objnum new_me)
929 {
930 /* save undo for the change */
931 vocdusave_me(ctx, ctx->voccxme);
932
933 /* set the new "Me" object */
934 ctx->voccxme = new_me;
935 }
936