aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/jacl/jacl_main.cpp
blob: 00fa70621096999877ebf03ef7bb2f33797cfb3c (plain)
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
/* 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/jacl/jacl.h"
#include "glk/jacl/csv.h"
#include "glk/jacl/types.h"
#include "glk/jacl/language.h"
#include "glk/jacl/prototypes.h"
#include "glk/jacl/version.h"

namespace Glk {
namespace JACL {

int convert_to_utf32(unsigned char *text);

glui32              status_width, status_height;

schanid_t           sound_channel[8] = { NULL, NULL, NULL, NULL,
                                         NULL, NULL, NULL, NULL
                                       };

event_t             *cancelled_event;

extern struct csv_parser parser_csv;

extern char         text_buffer[];
extern const char   *word[];
extern short int    quoted[];
extern short int    punctuated[];
extern int          wp;

extern int          custom_error;
extern int          interrupted;

extern int          jpp_error;

extern int          it;
extern int          them[];
extern int          her;
extern int          him;

extern int          oops_word;

#ifdef WINGLK
struct  string_type *resolved_string;
#endif

char            include_directory[81] = "\0";
char            temp_directory[81] = "\0";
char            data_directory[81] = "\0";
char            special_prompt[81] = "\n: \0";
char            file_prompt[5] = ": \0";
char            bookmark[81] = "\0";
char            walkthru[81] = "\0";

char            function_name[81];

extern char default_function[84];
char            override_[81];

char            temp_buffer[1024];
char            error_buffer[1024];
unsigned char   chunk_buffer[4096];
#ifndef NOUNICODE
glui32          chunk_buffer_uni[4096];
#endif
char            proxy_buffer[1024];

char            oops_buffer[1024];
char            oopsed_current[1024];
char            last_command[1024];
const char      *blank_command = "blankjacl\0";
const char      *current_command = (const char *)NULL;
char            command_buffer[1024];
#ifndef NOUNICODE
glui32          command_buffer_uni[1024];
#endif
char            players_command[1024];

int             walkthru_running = FALSE;

int             start_of_last_command;
int             start_of_this_command;

int             objects, integers, functions, strings;

/* A STREAM FOR THE GAME FILE, WHEN IT'S OPEN. */
strid_t         game_stream = NULL;

/* THE STREAM FOR OPENING UP THE ARCHIVE CONTAINING GRAPHICS AND SOUND */
strid_t             blorb_stream;

/* A FILE REFERENCE FOR THE TRANSCRIPT FILE. */
static frefid_t script_fref = NULL;
/* A STREAM FOR THE TRANSCRIPT FILE, WHEN IT'S OPEN. */
static strid_t script_stream = NULL;

int             noun[4];
int             player = 0;

int             noun3_backup;
int             player_backup = 0;

int             variable_contents;
int             oec;
int            *object_element_address,
               *object_backup_address;

short int       spaced = TRUE;

int             delay = 0;

/* START OF GLK STUFF */

/* POINTERS TO THE GLK WINDOWS */
winid_t mainwin = NULL;
winid_t statuswin = NULL;
winid_t promptwin = NULL;
winid_t inputwin = NULL;
winid_t current_window = NULL;

/* POINTERS TO THE WINDOWS STREAMS */
strid_t mainstr = NULL;
strid_t statusstr = NULL;
strid_t promptstr = NULL;
strid_t inputstr = NULL;

/* END OF GLK STUFF */

char            user_id[] = "local";
char            prefix[81] = "\0";
char            blorb[81] = "\0";
char            game_path[256] = "\0";
char            game_file[256] = "\0";
char            processed_file[256] = "\0";

struct object_type *object[MAX_OBJECTS];
struct integer_type *integer_table = NULL;
struct cinteger_type *cinteger_table = NULL;
struct window_type *window_table = NULL;
struct attribute_type *attribute_table = NULL;
struct string_type *string_table = NULL;
struct string_type *cstring_table = NULL;
struct function_type *function_table = NULL;
struct function_type *executing_function = NULL;
struct command_type *completion_list = NULL;
struct word_type *grammar_table = NULL;
struct synonym_type *synonym_table = NULL;
struct filter_type *filter_table = NULL;

// Forward declarations
static void word_check();
static void version_info();


void glk_main() {
	int             index;

	override_[0] = 0;

	/* ALLOC AN EVENT TO STORE A CANCELLED EVENT IN */
	if ((cancelled_event = (event_t *) malloc(sizeof(event_t))) == NULL)
		outofmem();

	/* CREATE style_User1 FOR USE IN THE STATUS LINE */
	g_vm->glk_stylehint_set(wintype_TextGrid, style_User1, stylehint_ReverseColor, 1);
	g_vm->glk_stylehint_set(wintype_TextBuffer, style_User2, stylehint_ReverseColor, 1);

	/* OPEN THE MAIN WINDOW THE GLK WINDOWS */
	mainwin = g_vm->glk_window_open(0, 0, 0, wintype_TextBuffer, 1);

	if (!mainwin) {
		/* IT'S POSSIBLE THAT THE MAIN WINDOW FAILED TO OPEN. THERE's
		 * NOTHING WE CAN DO WITHOUT IT, SO EXIT. */
		return;
	} else {
		/* GET A REFERENCE TO mainwin's STREAM */
		mainstr = g_vm->glk_window_get_stream(mainwin);
	}

	/* SET THE CURRENT OUTPUT STREAM TO PRINT TO IT. */
	jacl_set_window(mainwin);

	/* OPEN A THIRD WINDOW: A TEXT GRID, BELOW THE MAIN WINDOW, ONE LINE
	 * HIGH. THIS IS THE WINDOW TO DISPLAY THE COMMAND PROMPT IN */
	//promptwin = g_vm->glk_window_open(mainwin, winmethod_Below | winmethod_Fixed,
	//    3, wintype_TextBuffer, 0);

	/* SET THIS TO DETERMINE THE SYTEM OF INPUT TO USE */
	//inputwin = promptwin;
	inputwin = mainwin;

	if (jpp_error) {
		/* THERE WAS AN ERROR DURING PREPROCESSING. NOW THAT THERE IS AN
		 * OPEN GLK WINDOW, OUTPUT THE ERROR MESSAGE AND EXIT */
		log_error(error_buffer, FALSE);
		terminate(200);
	}

	// INTIALISE THE CSV PARSER
	csv_init(&parser_csv, CSV_APPEND_NULL);

	/* NO PREPROCESSOR ERRORS, LOAD THE GAME FILE */
	read_gamefile();

	execute("+bootstrap");

	// OPEN A SECOND WINDOW: A TEXT GRID, ABOVE THE MAIN WINDOW, ONE LINE
	// HIGH. IT IS POSSIBLE THAT THIS WILL FAIL ALSO, BUT WE ACCEPT THAT.
	statuswin = g_vm->glk_window_open(mainwin, winmethod_Above | winmethod_Fixed,
	                                  0, wintype_TextGrid, 0);

	// GET A REFERENCE TO statuswin's STREAM
	if (statuswin != NULL) {
		statusstr = g_vm->glk_window_get_stream(statuswin);
	}

#ifdef WINGLK
	if ((resolved_string = cstring_resolve("game_title")) != NULL) {
		wing_vm->glk_window_set_title(resolved_string->value);
	} else {
		sprintf(temp_buffer, "JACL v%d.%d.%d ", J_VERSION, J_RELEASE, J_BUILD);
		wing_vm->glk_window_set_title(temp_buffer);
	}
#endif

	if (SOUND_SUPPORTED->value) {
		/* CREATE THE EIGHT SOUND CHANNELS */
		for (index = 0; index < 8; index++) {
			sound_channel[index] = g_vm->glk_schannel_create(0);
		}
	}

	jacl_set_window(mainwin);

	execute("+intro");

	if (object[2] == NULL) {
		log_error(CANT_RUN, PLUS_STDERR);
		terminate(43);
	}

	/* DUMMY RETRIEVE OF 'HERE' FOR TESTING OF GAME STATE */
	get_here();

	eachturn();

	/* TOP OF COMMAND LOOP */
	do {
		int gotline;
		event_t ev;

		custom_error = FALSE;

		jacl_set_window(mainwin);

		execute("+bottom");

		status_line();

		if (current_command != NULL) {
			strcpy(last_command, current_command);
		}

		if (inputwin == promptwin) {
			g_vm->glk_window_clear(promptwin);
			jacl_set_window(inputwin);
		}

		/* OUTPUT THE CUSTOM COMMAND PROMPT */
		write_text(string_resolve("command_prompt")->value);

#ifdef NOUNICODE
		g_vm->glk_request_line_event(inputwin, command_buffer, 255, 0);
#else
		g_vm->glk_request_line_event_uni(inputwin, command_buffer_uni, 255, 0);
#endif

		jacl_set_window(inputwin);

		gotline = FALSE;

		while (!gotline) {
			/* GRAB AN EVENT. */
			g_vm->glk_select(&ev);

			switch (ev.type) {

			case evtype_LineInput:
				if (ev.window == inputwin) {
					gotline = TRUE;
					jacl_set_window(mainwin);
					/* REALLY THE EVENT CAN *ONLY* BE FROM MAINWIN,
					 * BECAUSE WE NEVER REQUEST LINE INPUT FROM THE
					 * STATUS WINDOW. BUT WE DO A PARANOIA TEST,
					 * BECAUSE COMMANDBUF IS ONLY FILLED IF THE LINE
					 * EVENT COMES FROM THE MAINWIN REQUEST. IF THE
					 * LINE EVENT COMES FROM ANYWHERE ELSE, WE IGNORE
					 * IT. */
				}
				break;

			case evtype_SoundNotify:
				/* A SOUND HAS FINISHED PLAYING CALL +sound_finished
				 * WITH THE RESOUCE NUMBER AS THE FIRST ARGUMENT
				 * AND THE CHANNEL NUMBER AS THE SECOND ARGUMENT */
				sprintf(temp_buffer, "+sound_finished<%d<%d", (int) ev.val1, (int) ev.val2 - 1);
				execute(temp_buffer);
				break;

			case evtype_Timer:
				/* A TIMER EVENT IS TRIGGERED PERIODICALLY IF THE GAME
				 * REQUESTS THEM. THIS SIMPLY EXECUTES THE FUNCTION
				 * +timer WHICH IS LIKE +eachturn EXCEPT IT DOESN'T
				 * WAIT FOR THE PLAYER TO TYPE A COMMAND */

				jacl_set_window(mainwin);
				execute("+timer");
				break;

			case evtype_Arrange:
				/* WINDOWS HAVE CHANGED SIZE, SO WE HAVE TO REDRAW THE
				 * STATUS WINDOW. */
				status_line();
				break;

			default:
				break;
			}
		}

		// THE PLAYER'S INPUT WILL BE UTF-32. CONVERT IT TO UTF-8 AND NULL TERMINATE IT
#ifndef NOUNICODE
		convert_to_utf8(command_buffer_uni, ev.val1);
#endif

		current_command = command_buffer;

		/* SET ALL THE OUTPUT TO GO TO mainwin NOW THE COMMAND HAS BEEN READ */
		if (inputwin == promptwin) {
			jacl_set_window(mainwin);
			write_text(string_resolve("command_prompt")->value);
			g_vm->glk_set_style(style_Input);
			write_text(current_command);
			g_vm->glk_set_style(style_Normal);
			write_text("^");
		}

		execute("+top");

		index = 0;

		if (*current_command) {
			while (*(current_command + index) && index < 1024) {
				if (*(current_command + index) == '\r' || *(current_command + index) == '\n') {
					break;
				} else {
					text_buffer[index] = *(current_command + index);
					index++;
				}
			}
		}

		text_buffer[index] = 0;

		if (text_buffer[0] == 0) {
			/* NO COMMAND WAS SPECIFIED, FILL THE COMMAND IN AS 'blankjacl'
			 * FOR THE GAME TO PROCESS AS DESIRED */
			strcpy(text_buffer, "blankjacl");
			current_command = blank_command;
		}

		command_encapsulate();
		jacl_truncate();

		index = 0;

		/* SET THE INTEGER INTERRUPTED TO FALSE. IF THIS IS SET TO
		 * TRUE BY ANY COMMAND, FURTHER PROCESSING WILL STOP */
		INTERRUPTED->value = FALSE;

		interrupted = FALSE;

		if (word[0] != NULL) {
			if (strcmp(word[0], "undo")) {
				/* COMMAND DOES NOT EQUAL undo */
				save_game_state();
			}

			if (word[0][0] == '*') {
				if (script_stream) {
					write_text(cstring_resolve("COMMENT_RECORDED")->value);
				} else {
					write_text(cstring_resolve("COMMENT_IGNORED")->value);
				}
			} else {
				/* COMMAND IS NOT NULL, START PROCESSING IT */
				preparse();
			}
		} else {
			/* NO COMMAND WAS SPECIFIED, FILL THE COMMAND IN AS 'blankjacl'
			 * FOR THE GAME TO PROCESS AS DESIRED */
			strcpy(text_buffer, "blankjacl");
			command_encapsulate();
			preparse();
		}

	} while (TRUE);
}

void preparse() {
	int position;

	// THE INTERRUPTED VARIABLE IS USED TO STOP LATER ACTIONS IN A COMMAND
	// IF ANY ONE
	while (word[wp] != NULL && INTERRUPTED->value == FALSE) {
		//printf("--- preparse %s\n", word[wp]);
		// PROCESS THE CURRENT COMMAND
		// CREATE THE command STRINGS FROM THIS POINT ONWARDS SO THE VERB OF
		// THE CURRENT COMMAND IS ALWAYS command[0].

		clear_cstring("command");

		position = wp;

		while (word[position] != NULL && strcmp(word[position], cstring_resolve("THEN_WORD")->value)) {
			add_cstring("command", word[position]);
			position++;
		};

		// PROCESS THE COMMAND
		word_check();

		/* THE PREVIOUS COMMAND HAS FINISHED, LOOK FOR ANOTHER COMMAND */
		while (word[wp] != NULL) {
			if (word[wp] != NULL && !strcmp(word[wp], cstring_resolve("THEN_WORD")->value)) {
				wp++;
				break;
			}
			wp++;
		}
	}
}

void word_check() {
	int index;

	/* REMEMBER THE START OF THIS COMMAND TO SUPPORT 'oops' AND 'again' */
	start_of_this_command = wp;
	//printf("--- command starts at %d\n", start_of_this_command);

	/* START CHECKING THE PLAYER'S COMMAND FOR SYSTEM COMMANDS */
	if (!strcmp(word[wp], cstring_resolve("QUIT_WORD")->value) || !strcmp(word[wp], "q")) {
		if (execute("+quit_game") == FALSE) {
			TIME->value = FALSE;
			write_text(cstring_resolve("SURE_QUIT")->value);
			if (get_yes_or_no()) {
				newline();
				execute("+score");
				terminate(0);
			} else {
				write_text(cstring_resolve("RETURN_GAME")->value);
			}
		}
	} else if (!strcmp(word[wp], cstring_resolve("RESTART_WORD")->value)) {
		if (execute("+restart_game") == FALSE) {
			TIME->value = FALSE;
			write_text(cstring_resolve("SURE_RESTART")->value);
			if (get_yes_or_no()) {
				write_text(cstring_resolve("RESTARTING")->value);
				restart_game();
				g_vm->glk_window_clear(current_window);
				execute("+intro");
				eachturn();
			} else {
				write_text(cstring_resolve("RETURN_GAME")->value);
			}
		}
	} else if (!strcmp(word[wp], cstring_resolve("UNDO_WORD")->value)) {
		if (execute("+undo_move") == FALSE) {
			undoing();
		}
	} else if (!strcmp(word[wp], cstring_resolve("OOPS_WORD")->value) || !strcmp(word[wp], "o")) {
		//printf("--- oops word is %d\n", oops_word);
		if (word[++wp] != NULL) {
			if (oops_word == -1) {
				if (TOTAL_MOVES->value == 0) {
					write_text(cstring_resolve("NO_MOVES")->value);
					TIME->value = FALSE;
				} else {
					write_text(cstring_resolve("CANT_CORRECT")->value);
					TIME->value = FALSE;
				}
			} else {
				strcpy(oops_buffer, word[wp]);
				strcpy(text_buffer, last_command);
				command_encapsulate();
				//printf("--- trying to replace %s with %s\n", word[oops_word], oops_buffer);
				jacl_truncate();
				word[oops_word] = (char *)&oops_buffer;

				/* BUILD A PLAIN STRING REPRESENTING THE NEW COMMAND */
				oopsed_current[0] = 0;
				index = 0;

				while (word[index] != NULL) {
					if (oopsed_current[0] != 0) {
						strcat(oopsed_current, " ");
					}

					strcat(oopsed_current, word[index]);

					index++;
				}

				current_command = oopsed_current;
				//printf("--- current command is: %s\n", current_command);

				/* PROCESS THE FIXED COMMAND ONLY */
				wp = start_of_last_command;
				word_check();
			}
		} else {
			write_text(cstring_resolve("BAD_OOPS")->value);
			TIME->value = FALSE;
		}
	} else if (!strcmp(word[wp], cstring_resolve("AGAIN_WORD")->value) || !strcmp(word[wp], "g")) {
		if (TOTAL_MOVES->value == 0) {
			write_text(cstring_resolve("NO_MOVES")->value);
			TIME->value = FALSE;
		} else if (last_command[0] == 0) {
			write_text(cstring_resolve("NOT_CLEVER")->value);
			TIME->value = FALSE;
		} else {
			strcpy(text_buffer, last_command);
			current_command = last_command;
			command_encapsulate();
			jacl_truncate();
			//printf("--- command started at %d\n", start_of_last_command);
			wp = start_of_last_command;
			word_check();
		}
	} else if (!strcmp(word[wp], cstring_resolve("SCRIPT_WORD")->value) || !strcmp(word[wp], "transcript")) {
		scripting();
	} else if (!strcmp(word[wp], cstring_resolve("UNSCRIPT_WORD")->value)) {
		if (!script_stream) {
			write_text(cstring_resolve("SCRIPTING_ALREADY_OFF")->value);
		} else {
			/* Close the file. */
			g_vm->glk_put_string_stream(script_stream, "\nEND OF A TRANSCRIPT\n");
			g_vm->glk_stream_close(script_stream, NULL);
			write_text(cstring_resolve("SCRIPTING_OFF")->value);
			script_stream = NULL;
		}
	} else if (!strcmp(word[wp], cstring_resolve("WALKTHRU_WORD")->value)) {
		walking_thru();
	} else if (!strcmp(word[wp], cstring_resolve("INFO_WORD")->value) || !strcmp(word[wp], "version")) {
		version_info();
		write_text("you can redistribute it and/or modify it under the ");
		write_text("terms of the GNU General Public License as published by ");
		write_text("the Free Software Foundation; either version 2 of the ");
		write_text("License, or any later version.^^");
		write_text("This program is distributed in the hope that it will be ");
		write_text("useful, but WITHOUT ANY WARRANTY; without even the ");
		write_text("implied warranty of MERCHANTABILITY or FITNESS FOR A ");
		write_text("PARTICULAR PURPOSE. See the GNU General Public License ");
		write_text("for more details.^^");
		write_text("You should have received a copy of the GNU General ");
		write_text("Public License along with this program; if not, write ");
		write_text("to the Free Software Foundation, Inc., 675 Mass Ave, ");
		write_text("Cambridge, MA 02139, USA.^^");
		sprintf(temp_buffer, "OBJECTS DEFINED:   %d^", objects);
		write_text(temp_buffer);
		TIME->value = FALSE;
	} else {
		/* NO WORD HAS BEEN MARKED AS AN ERROR YET*/
		oops_word = -1;

		/* THIS IS NOT A SYSTEM COMMAND, CALL parser TO PROCESS THE COMMAND */
		parser();
	}

	start_of_last_command = start_of_this_command;
}

void version_info() {
	char            buffer[80];

	sprintf(buffer, "JACL Interpreter v%d.%d.%d ", J_VERSION, J_RELEASE,
	        J_BUILD);
	write_text(buffer);
	sprintf(buffer, "/ %d object.^", MAX_OBJECTS);
	write_text(buffer);
	write_text("Copyright (c) 1992-2010 Stuart Allen.^^");
}

void save_game_state() {
	/* THIS FUNCTION MAKES AN IN-MEMORY COPY OF THE GAME STATE AFTER EACH
	 * OF THE PLAYER'S COMMANDS SO THE 'undo' COMMAND CAN BE USED */
	int             index,
	                counter;

	struct integer_type *current_integer = integer_table;
	struct function_type *current_function = function_table;

	do {
		current_function->call_count_backup = current_function->call_count;
		current_function = current_function->next_function;
	} while (current_function != NULL);

	do {
		current_integer->value_backup = current_integer->value;
		current_integer = current_integer->next_integer;
	} while (current_integer != NULL);

	for (index = 1; index <= objects; index++) {
		if (object[index]->nosave)
			continue;

		for (counter = 0; counter < 16; counter++) {
			object[index]->integer_backup[counter] =
			    object[index]->integer[counter];
		}

		object[index]->attributes_backup = object[index]->attributes;
		object[index]->user_attributes_backup = object[index]->user_attributes;
	}

	player_backup = player;
	noun3_backup = noun[3];
}

int save_interaction(const char *filename) {
	frefid_t saveref;

	jacl_set_window(inputwin);

	if (inputwin == promptwin) {
		g_vm->glk_window_clear(promptwin);
		newline();
	}

	if (filename == NULL) {
		saveref = g_vm->glk_fileref_create_by_prompt(fileusage_SavedGame | fileusage_BinaryMode, filemode_Write, 0);
	} else {
		saveref = g_vm->glk_fileref_create_by_name(fileusage_SavedGame | fileusage_BinaryMode, filename, 0);

	}

	jacl_set_window(mainwin);

	if (!saveref) {
		write_text(cstring_resolve("CANT_SAVE")->value);
		return (FALSE);
	} else {
		if (save_game(saveref)) {
			return (TRUE);
		} else {
			write_text(cstring_resolve("CANT_SAVE")->value);
			return (FALSE);
		}
	}
}

void restore_game_state() {
	/* THIS FUNCTION IS CALLED AS A RESULT OF THE PLAYER USING THE 'undo'
	 * COMMAND */
	int             index,
	                counter;

	struct integer_type *current_integer = integer_table;
	struct function_type *current_function = function_table;

	do {
		current_function->call_count = current_function->call_count_backup;
		current_function = current_function->next_function;
	} while (current_function != NULL);


	do {
		current_integer->value = current_integer->value_backup;
		current_integer = current_integer->next_integer;
	} while (current_integer != NULL);

	for (index = 1; index <= objects; index++) {
		if (object[index]->nosave)
			continue;

		for (counter = 0; counter < 16; counter++)
			object[index]->integer[counter] =
			    object[index]->integer_backup[counter];

		object[index]->attributes = object[index]->attributes_backup;
		object[index]->user_attributes = object[index]->user_attributes_backup;
	}

	player = player_backup;
	noun[3] = noun3_backup;

	write_text(cstring_resolve("MOVE_UNDONE")->value);
	object[HERE]->attributes &= ~1;
	execute("+top");
	execute("+look_around");
	execute("+bottom");
	TIME->value = FALSE;
}

void write_text(const char *string_buffer) {
	int             index,
	                length;

	if (!strcmp(string_buffer, "tilde")) {
		g_vm->glk_put_string("~");
		return;
	} else if (!strcmp(string_buffer, "caret")) {
		g_vm->glk_put_string("^");
		return;
	}

	length = strlen(string_buffer);

	for (index = 0; index < length; index++) {
		if (*(string_buffer + index) == '^') {
			chunk_buffer[index] = '\n';
		} else if (*(string_buffer + index) == '~') {
			chunk_buffer[index] = '\"';
		} else {
			chunk_buffer[index] = *(string_buffer + index);
		}
	}

	chunk_buffer[index] = 0;

	/* PRINT THE CONTENTS OF string_buffer */
#ifdef NOUNICODE
	g_vm->glk_put_string(chunk_buffer);
#else
	chunk_buffer_uni[(glui32) convert_to_utf32(chunk_buffer)] = 0;
	g_vm->glk_put_string_uni(chunk_buffer_uni);
#endif
}

void jacl_sleep(unsigned int mseconds) {
	g_system->delayMillis(mseconds);
}

void status_line() {
	int cursor, index;
	winid_t pair_window;

	if (!statuswin) {
		return;
	} else {
		// THERE IS AN EXISTING STATUS WINDOW, MAKE SURE A NEW SIZE HASN'T BEEN
		// REQUESTED
		g_vm->glk_window_get_size(statuswin, &status_width, &status_height);
		if (status_height != (uint)integer_resolve("status_window")->value) {
			// HEIGHT HAS CHANGED, UPDATE THE WINDOW
			pair_window = g_vm->glk_window_get_parent(statuswin);
			g_vm->glk_window_set_arrangement(pair_window, winmethod_Above | winmethod_Fixed, integer_resolve("status_window")->value, statuswin);
			g_vm->glk_window_get_size(statuswin, &status_width, &status_height);
		}
	}

	if (status_height == 0) {
		// THE STATUS WINDOW CAN'T BE CLOSED, ONLY SET TO HAVE A HEIGHT OF ZERO
		return;
	}

	jacl_set_window(statuswin);
	g_vm->glk_window_clear(statuswin);

	if (execute("+update_status_window") == FALSE) {
		g_vm->glk_set_style(style_User1);

		/* DISPLAY THE INVERSE STATUS LINE AT THE TOP OF THE SCREEN */
		for (index = 0; index < (int)status_width; index++) {
			temp_buffer[index] = ' ';
		}
		temp_buffer[index] = 0;
		write_text(temp_buffer);

		/* PRINT THE LOCATION'S TITLE ON THE LEFT. */
		g_vm->glk_window_move_cursor(statuswin, 1, 0);
		write_text(sentence_output(HERE, TRUE));

		/* BUILD THE SCORE/ MOVES STRING */
		temp_buffer[0] = 0;
		sprintf(temp_buffer, "Score: %d  Moves: %d", SCORE->value, TOTAL_MOVES->value);

		cursor = status_width - strlen(temp_buffer);
		cursor--;
		g_vm->glk_window_move_cursor(statuswin, cursor, 0);
		write_text(temp_buffer);
	}

	jacl_set_window(mainwin);

}

void newline() {
	/* START A NEW LINE ON THE SCREEN */
	write_text("\n");
}

void more(const char *message) {
	jacl_set_window(inputwin);

	if (inputwin == promptwin) {
		g_vm->glk_window_clear(promptwin);
		newline();
	}

	g_vm->glk_set_style(style_Emphasized);
	write_text(message);
	g_vm->glk_set_style(style_Normal);

	(void)get_key();

	if (inputwin == mainwin) newline();
}

int get_key() {
	event_t ev;

	g_vm->glk_request_char_event(inputwin);

	while (!g_vm->shouldQuit()) {
		g_vm->glk_select(&ev);

		switch (ev.type) {
		case evtype_CharInput:
			if (ev.window == inputwin) {
				return (ev.val1);
			}
			break;

		default:
			break;
		}
	}

	return 0;
}

int get_number(int insist, int low, int high) {
	char *cx;
	char commandbuf[256];
	int response;
	int gotline;
	event_t ev;

	status_line();

	sprintf(temp_buffer, cstring_resolve("TYPE_NUMBER")->value, low, high);

	/* THIS LOOP IS IDENTICAL TO THE MAIN COMMAND LOOP IN g_vm->glk_main(). */

	while (1) {
		if (inputwin == promptwin) {
			g_vm->glk_window_clear(promptwin);
			jacl_set_window(inputwin);
		}

		write_text(temp_buffer);
		jacl_set_window(mainwin);

		g_vm->glk_request_line_event(inputwin, commandbuf, 255, 0);

		gotline = FALSE;
		while (!gotline && g_vm->shouldQuit()) {
			g_vm->glk_select(&ev);

			switch (ev.type) {
			case evtype_LineInput:
				if (ev.window == inputwin) {
					gotline = TRUE;
				}
				break;

			case evtype_Arrange:
				status_line();
				break;

			default:
				break;
			}
		}

		commandbuf[ev.val1] = '\0';
		for (cx = commandbuf; *cx == ' '; cx++) { };

		if (validate(cx)) {
			response = atoi(cx);
			if (response >= low && response <= high) {
				return (response);
			}
		}

		if (!insist) {
			return (-1);
		} else {
			write_text(cstring_resolve("INVALID_SELECTION")->value);
		}
	}
}

void get_string(char *string_buffer) {
	char *cx;
	char commandbuf[256];
	int gotline;
	event_t ev;

	status_line();

	/* THIS LOOP IS IDENTICAL TO THE MAIN COMMAND LOOP IN g_vm->glk_main(). */

	if (inputwin == promptwin) {
		g_vm->glk_window_clear(promptwin);
		jacl_set_window(inputwin);
	}

	jacl_set_window(mainwin);

	g_vm->glk_request_line_event(inputwin, commandbuf, 255, 0);

	gotline = FALSE;
	while (!gotline && !g_vm->shouldQuit()) {
		g_vm->glk_select(&ev);

		switch (ev.type) {
		case evtype_LineInput:
			if (ev.window == inputwin) {
				gotline = TRUE;
			}
			break;

		case evtype_Arrange:
			status_line();
			break;

		default:
			break;
		}
	}

	commandbuf[ev.val1] = '\0';
	for (cx = commandbuf; *cx == ' '; cx++) { };

	// COPY UP TO 255 BYTES OF THE ENTERED TEXT INTO THE SUPPLIED STRING
	strncpy(string_buffer, cx, 255);
}

int get_yes_or_no() {
	char *cx;
	char commandbuf[256];
	int gotline;
	event_t ev;

	status_line();

	/* THIS LOOP IS IDENTICAL TO THE MAIN COMMAND LOOP IN g_vm->glk_main(). */

	while (1) {
		if (inputwin == promptwin) {
			g_vm->glk_window_clear(promptwin);
			jacl_set_window(inputwin);
		}

		write_text(cstring_resolve("YES_OR_NO")->value);
		jacl_set_window(mainwin);

		g_vm->glk_request_line_event(inputwin, commandbuf, 255, 0);

		gotline = FALSE;
		while (!gotline && !g_vm->shouldQuit()) {

			g_vm->glk_select(&ev);

			switch (ev.type) {
			case evtype_LineInput:
				if (ev.window == inputwin) {
					gotline = TRUE;
				}
				break;

			case evtype_Arrange:
				status_line();
				break;

			default:
				break;
			}
		}

		commandbuf[ev.val1] = '\0';
		for (cx = commandbuf; *cx == ' '; cx++) { };

		// PUSH THE FIRST NON-SPACE CHARACTER TO LOWER FOR COMPARISON
		// WITH CONSTANT
		*cx = tolower(*cx);

		if (*cx == cstring_resolve("YES_WORD")->value[0]) {
			return TRUE;
		} else if (*cx == cstring_resolve("NO_WORD")->value[0]) {
			return FALSE;
		}

	}
}

char get_character(const char *message) {
	char *cx;
	char commandbuf[256];
	int gotline;
	event_t ev;

	status_line();

	/* THIS LOOP IS IDENTICAL TO THE MAIN COMMAND LOOP IN g_vm->glk_main(). */

	while (!g_vm->shouldQuit()) {
		if (inputwin == promptwin) {
			g_vm->glk_window_clear(promptwin);
			jacl_set_window(inputwin);
		}

		write_text(message);
		g_vm->glk_request_line_event(inputwin, commandbuf, 255, 0);
		jacl_set_window(mainwin);

		gotline = FALSE;
		while (!gotline && !g_vm->shouldQuit()) {

			g_vm->glk_select(&ev);

			switch (ev.type) {
			case evtype_LineInput:
				if (ev.window == inputwin) {
					gotline = TRUE;
				}
				break;

			case evtype_Arrange:
				status_line();
				break;

			default:
				break;
			}
		}

		commandbuf[ev.val1] = '\0';
		for (cx = commandbuf; *cx == ' '; cx++) { };

		return (*cx);
	}


	return '\0';
}

strid_t open_glk_file(uint usage, uint mode, char *filename) {

	frefid_t    file_reference;
	strid_t     stream_reference;

	file_reference = g_vm->glk_fileref_create_by_name(usage, filename, 0);

	if (file_reference) {
		stream_reference = g_vm->glk_stream_open_file(file_reference, (FileMode)mode, 0);

		if (stream_reference) {
			/* WE'RE DONE WITH THE FILE REFERENCE NOW THAT THE STREAM
			 * HAS BEEN SUCCESSFULLY OPENED */
			g_vm->glk_fileref_destroy(file_reference);

			return (stream_reference);
		}
	}

	return (strid_t) NULL;
}

void scripting() {
	if (script_stream) {
		write_text(cstring_resolve("SCRIPTING_ALREADY_ON")->value);
		return;
	}

	/* IF WE'VE TURNED ON SCRIPTING BEFORE, USE THE SAME FILE REFERENCE;
	 * OTHERWISE, PROMPT THE PLAYER FOR A FILE. */
	if (!script_fref) {
		script_fref = g_vm->glk_fileref_create_by_prompt(
		                  fileusage_Transcript | fileusage_TextMode,
		                  filemode_WriteAppend, 0);
		if (!script_fref) {
			write_text(cstring_resolve("CANT_WRITE_SCRIPT")->value);
			return;
		}
	}

	/* OPEN THE TRANSCRIPT FILE */
	script_stream = g_vm->glk_stream_open_file(script_fref, filemode_WriteAppend, 0);

	if (!script_stream) {
		write_text(cstring_resolve("CANT_WRITE_SCRIPT")->value);
		return;
	}
	write_text(cstring_resolve("SCRIPTING_ON")->value);
	g_vm->glk_window_set_echo_stream(mainwin, script_stream);
	g_vm->glk_put_string_stream(script_stream, "TRANSCRIPT OF: ");
	g_vm->glk_put_string_stream(script_stream, cstring_resolve("game_title")->value);
	g_vm->glk_put_string_stream(script_stream, "\n");
}

void undoing() {
	if (TOTAL_MOVES->value && strcmp(last_command, cstring_resolve("UNDO_WORD")->value)) {
		restore_game_state();
	} else {
		write_text(cstring_resolve("NO_UNDO")->value);
		TIME->value = FALSE;
	}
}

void walking_thru() {
	int result, index;

	int length;
	char script_line[81];

	/* A FILE REFERENCE FOR THE WALKTHRU FILE. */
	frefid_t walkthru_fref = NULL;

	/* A STREAM FOR THE WALKTHRU FILE, WHEN IT'S OPEN. */
	strid_t walkthru_stream = NULL;

	walkthru_fref = g_vm->glk_fileref_create_by_prompt(fileusage_Data | fileusage_TextMode, filemode_Read, 0);

	if (!walkthru_fref) {
		write_text(cstring_resolve("ERROR_READING_WALKTHRU")->value);
		return;
	}

	/* OPEN THE WALKTHRU FILE */
	walkthru_stream = g_vm->glk_stream_open_file(walkthru_fref, filemode_Read, 0);

	if (!walkthru_stream) {
		write_text(cstring_resolve("ERROR_READING_WALKTHRU")->value);
		return;
	}

	walkthru_running = TRUE;

	/* ISSUE ALL THE COMMANDS STORE IN THE WALKTHRU FILE */

	/* WE'RE DONE WITH THE FILE REFERENCE NOW THAT THE STREAM
	 * HAS BEEN SUCCESSFULLY OPENED */
	g_vm->glk_fileref_destroy(walkthru_fref);

	result = g_vm->glk_get_line_stream(walkthru_stream, text_buffer, (glui32) 80);

	/* SET TO LOWER CASE AND STRIP NEWLINES */
	length = strlen(text_buffer);
	for (index = 0; index < length; index++) {
		if (text_buffer[index] == '\r' ||
		        text_buffer[index] == '\n') {
			text_buffer[index] = 0;
			break;
		}
	}

	strcpy(script_line, text_buffer);

	while (result && INTERRUPTED->value == FALSE) {
		/* THERE COULD BE A LOT OF PROCESSING GOING ON HERE BEFORE GETTING
		 * TO THE NEXT EVENT LOOP SO CALL g_vm->glk_tick AFTER EACH LINE READ */
		g_vm->glk_tick();
		command_encapsulate();
		jacl_truncate();
		if (word[0] != NULL) {
			custom_error = FALSE;

			execute("+bottom");

			write_text(string_resolve("command_prompt")->value);
			g_vm->glk_set_style(style_Input);
			write_text(script_line);
			newline();
			g_vm->glk_set_style(style_Normal);

			execute("+top");

			preparse();
		}

		result = g_vm->glk_get_line_stream(walkthru_stream, text_buffer, (glui32) 80);

		/* SET TO LOWER CASE AND STRIP NEWLINES */
		length = strlen(text_buffer);
		for (index = 0; index < length; index++) {
			if (text_buffer[index] == '\r' ||
			        text_buffer[index] == '\n') {
				text_buffer[index] = 0;
				break;
			}
		}

		strcpy(script_line, text_buffer);
	}

	/* CLOSE THE STREAM */
	g_vm->glk_stream_close(walkthru_stream, NULL);

	/* FINISH UP */
	walkthru_running = FALSE;
}

int restore_interaction(const char *filename) {
	frefid_t saveref;

	jacl_set_window(inputwin);

	if (inputwin == promptwin) {
		g_vm->glk_window_clear(promptwin);
		newline();
	}

	if (filename == NULL) {
		saveref = g_vm->glk_fileref_create_by_prompt(fileusage_SavedGame | fileusage_BinaryMode, filemode_Read, 0);
	} else {
		saveref = g_vm->glk_fileref_create_by_name(fileusage_SavedGame | fileusage_BinaryMode, filename, 0);
	}

	jacl_set_window(mainwin);

	if (!saveref) {
		write_text(cstring_resolve("CANT_RESTORE")->value);
		return (FALSE);
	}

	if (restore_game(saveref, TRUE) == FALSE) {
		write_text(cstring_resolve("CANT_RESTORE")->value);
		return (FALSE);
	} else {
		return (TRUE);
	}
}

glui32 glk_get_bin_line_stream(strid_t file_stream, char *buffer, glui32 max_length) {
	int character = 0;

	register int index = 0;

	character = g_vm->glk_get_char_stream(file_stream);
	while (character != -1 && index < (int) max_length) {
		*(buffer + index) = (char) character;
		index++;
		if (character == (int) '\n' ||
		        character == (int) '\r') {
			break;
		}
		character = g_vm->glk_get_char_stream(file_stream);
	};

	*(buffer + index) = 0;

	return ((glui32) index);
}

void jacl_set_window(winid_t new_window) {
	current_window = new_window;
	g_vm->glk_set_window(new_window);
}

#ifdef READLINE
char **command_completion(const char *text, int start, int end) {
	/* READLINE TAB COMPLETION CODE */
	char **options;

	options = (const char **) NULL;

	if (start == 0)
		options = completion_matches(text, verb_generator);
	else
		options = completion_matches(text, object_generator);

	return (options);
}
#endif

const char *object_generator(const char *text, int state) {
	static int len;
	static struct command_type *now;
	struct command_type *to_send;
	struct name_type *current_name = (struct name_type *) NULL;

	/* IF THIS IS A NEW WORD TO COMPLETE, INITIALIZE NOW. THIS INCLUDES
	SAVING THE LENGTH OF TEXT FOR EFFICIENCY, AND INITIALIZING THE INDEX
	VARIABLE TO 0. */

	if (!state) {
		/* BUILD THE LIST */
		int index;
		completion_list = NULL;

		/* LOOP THROUGH ALL THE OBJECTS AND SEE IF THEY ARE IN
		   THE CURRENT LOCATION */
		for (index = 1; index <= objects; index++) {
			if (parent_of(HERE, index, UNRESTRICT) && !(object[index]->attributes & NO_TAB)) {
				/* LOOP THROUGH ALL THE OBJECTS NAMES AND
				   THEM TO THE COMPLETION LIST */
				current_name = object[index]->first_name;
				while (current_name) {
					add_word(current_name->name);
					current_name = current_name->next_name;
				}
			}
		}
		now = completion_list;
		len = strlen(text);
	}

	while (now != NULL) {
		if (!strncmp(text, now->word, len)) {
			to_send = now;
			now = now->next;
			return ((const char *)to_send->word);
		}
		now = now->next;
	}

	return (const char *)NULL;
}

const char *verb_generator(const char *text, int state) {
	static int len;
	static struct command_type *now;
	struct command_type *to_send;
	struct word_type *pointer;

	/* IF THIS IS A NEW WORD TO COMPLETE, INITIALIZE NOW. THIS INCLUDES
	SAVING THE LENGTH OF TEXT FOR EFFICIENCY, AND INITIALIZING THE INDEX
	VARIABLE TO 0. */

	if (!state) {
		/* BUILD THE LIST */
		completion_list = NULL;

		pointer = grammar_table;
		while (pointer != NULL) {
			add_word(pointer->word);
			pointer = pointer->next_sibling;
		}

		add_word("walkthru");

		now = completion_list;
		len = strlen(text);
	}

	while (now != NULL) {
		if (!strncmp(text, now->word, len)) {
			to_send = now;
			now = now->next;

			/* MALLOC A COPY AND RETURN A POINTER TO THE COPY */
			return ((const char *)to_send->word);
		}
		now = now->next;
	}

	return (const char *)NULL;
}

/* ADD A COPY OF STRING TO A LIST OF STRINGS IF IT IS NOT
   ALREADY IN THE LIST. THIS IS FOR THE USE OF READLINE */
void add_word(const char *newWord) {
	static struct command_type *current_word = NULL;
	struct command_type *previous_word = NULL;

	/* DON'T ADD WORDS SUCH AS *present TO THE LIST*/
	if (*newWord == '*')
		return;

	if (current_word != NULL)
		previous_word = current_word;

	current_word = (struct command_type *) malloc(sizeof(struct command_type));

	if (current_word != NULL) {
		if (completion_list == NULL) {
			completion_list = current_word;
		}

		strncpy(current_word->word, newWord, 40);
		current_word->word[40] = 0;
		current_word->next = NULL;

		if (previous_word != NULL) {
			previous_word->next = current_word;
		}
	}
}

void convert_to_utf8(glui32 *text, int len) {
	int i, k;

	i = 0;
	k = 0;

	/*convert UTF-32 to UTF-8 */
	while (i < len) {
		if (text[i] < 0x80) {
			command_buffer[k] = text[i];
			k++;
		} else if (text[i] < 0x800) {
			command_buffer[k  ] = (0xC0 | ((text[i] & 0x7C0) >> 6));
			command_buffer[k + 1] = (0x80 | (text[i] & 0x03F));
			k = k + 2;
		} else if (text[i] < 0x10000) {
			command_buffer[k  ] = (0xE0 | ((text[i] & 0xF000) >> 12));
			command_buffer[k + 1] = (0x80 | ((text[i] & 0x0FC0) >>  6));
			command_buffer[k + 2] = (0x80 | (text[i] & 0x003F));
			k = k + 3;
		} else if (text[i] < 0x200000) {
			command_buffer[k  ] = (0xF0 | ((text[i] & 0x1C0000) >> 18));
			command_buffer[k + 1] = (0x80 | ((text[i] & 0x03F000) >> 12));
			command_buffer[k + 2] = (0x80 | ((text[i] & 0x000FC0) >>  6));
			command_buffer[k + 3] = (0x80 | (text[i] & 0x00003F));
			k = k + 4;
		} else {
			command_buffer[k] = '?';
			k++;
		}
		i++;
	}

	/* null-terminated string */
	command_buffer[k] = '\0';
}

#ifndef NOUNICODE
int convert_to_utf32(unsigned char *text) {
	int text_len;
	int rlen;

	if (!text) {
		return 0;
	}

	text_len = strlen((const char *)text);

	if (!text_len) {
		return 0;
	}

	rlen = (int) parse_utf8(text, text_len, chunk_buffer_uni, text_len);

	return (rlen);
}

glui32 parse_utf8(unsigned char *buf, glui32 buflen, glui32 *out, glui32 outlen) {
	glui32 pos = 0;
	glui32 outpos = 0;
	glui32 res;
	glui32 val0, val1, val2, val3;

	while (outpos < outlen) {
		if (pos >= buflen)
			break;

		val0 = buf[pos++];

		if (val0 < 0x80) {
			res = val0;
			out[outpos++] = res;
			continue;
		}

		if ((val0 & 0xe0) == 0xc0) {
			if (pos + 1 > buflen) {
				warning("incomplete two-byte character");
				break;
			}
			val1 = buf[pos++];
			if ((val1 & 0xc0) != 0x80) {
				warning("malformed two-byte character");
				break;
			}
			res = (val0 & 0x1f) << 6;
			res |= (val1 & 0x3f);
			out[outpos++] = res;
			continue;
		}

		if ((val0 & 0xf0) == 0xe0) {
			if (pos + 2 > buflen) {
				warning("incomplete three-byte character");
				break;
			}
			val1 = buf[pos++];
			val2 = buf[pos++];
			if ((val1 & 0xc0) != 0x80) {
				warning("malformed three-byte character");
				break;
			}
			if ((val2 & 0xc0) != 0x80) {
				warning("malformed three-byte character");
				break;
			}
			res = (((val0 & 0xf) << 12)  & 0x0000f000);
			res |= (((val1 & 0x3f) << 6) & 0x00000fc0);
			res |= (((val2 & 0x3f))    & 0x0000003f);
			out[outpos++] = res;
			continue;
		}

		if ((val0 & 0xf0) == 0xf0) {
			if ((val0 & 0xf8) != 0xf0) {
				warning("malformed four-byte character");
				break;
			}
			if (pos + 3 > buflen) {
				warning("incomplete four-byte character");
				break;
			}
			val1 = buf[pos++];
			val2 = buf[pos++];
			val3 = buf[pos++];
			if ((val1 & 0xc0) != 0x80) {
				warning("malformed four-byte character");
				break;
			}
			if ((val2 & 0xc0) != 0x80) {
				warning("malformed four-byte character");
				break;
			}
			if ((val3 & 0xc0) != 0x80) {
				warning("malformed four-byte character");
				break;
			}
			res = (((val0 & 0x7) << 18)   & 0x1c0000);
			res |= (((val1 & 0x3f) << 12) & 0x03f000);
			res |= (((val2 & 0x3f) << 6)  & 0x000fc0);
			res |= (((val3 & 0x3f))     & 0x00003f);
			out[outpos++] = res;
			continue;
		}

		warning("malformed character");
	}

	return outpos;
}
#endif

} // End of namespace JACL
} // End of namespace Glk