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