Debian Codegraph @

terps/jacl/jacl.c

raw symbols SCIP
1 /* jacl.c --- A GLK port of the console JACL Interpreter
2 (C) 1992-2008 Stuart Allen
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 1, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include "csv.h"
20
21 #ifdef _WIN32
22 #ifndef GARGLK
23 #include <windows.h>
24 #include "glkterm/glk.h"
25 #include "WinGlk.h"
26 #endif
27 #endif
28
29 #include "jacl.h"
30 #include "types.h"
31 #include "language.h"
32 #include <string.h>
33 #include "prototypes.h"
34 #include "interpreter.h"
35 #include "parser.h"
36 #include "encapsulate.h"
37
38 #ifdef GARGLK
39 #include <glkstart.h>
40 #endif
41
42 #ifndef GARGLK
43 #include "glkterm/gi_blorb.h"
44 #include "glkterm/glk.h"
45 #endif
46
47 glui32 status_width, status_height;
48
49 schanid_t sound_channel[8] = { NULL, NULL, NULL, NULL,
50 NULL, NULL, NULL, NULL };
51
52 static event_t *cancelled_event;
53
54 extern int jpp_error;
55
56 #ifdef WINGLK
57 struct string_type *resolved_string;
58 #endif
59
60 char include_directory[81] = "\0";
61 char temp_directory[81] = "\0";
62 char data_directory[81] = "\0";
63 char special_prompt[81] = "\n: \0";
64 char file_prompt[5] = ": \0";
65 char bookmark[81] = "\0";
66 char walkthru[81] = "\0";
67
68 char function_name[81];
69
70 char override[81];
71
72 char temp_buffer[1024];
73 char error_buffer[1024];
74 static unsigned char chunk_buffer[4096];
75 #ifndef NOUNICODE
76 static glui32 chunk_buffer_uni[4096];
77 #endif
78 char proxy_buffer[1024];
79
80 static char oops_buffer[1024];
81 static char oopsed_current[1024];
82 char last_command[1024];
83 char *blank_command = "blankjacl\0";
84 static char *current_command = NULL;
85 static char command_buffer[1024];
86 #ifndef NOUNICODE
87 static glui32 command_buffer_uni[1024];
88 #endif
89 char players_command[1024];
90
91 int walkthru_running = FALSE;
92
93 int start_of_last_command;
94 int start_of_this_command;
95
96 int objects, integers, functions, strings;
97
98 /* A STREAM FOR THE GAME FILE, WHEN IT'S OPEN. */
99 strid_t game_stream = NULL;
100
101 /* THE STREAM FOR OPENING UP THE ARCHIVE CONTAINING GRAPHICS AND SOUND */
102 extern strid_t blorb_stream;
103
104 /* A FILE REFERENCE FOR THE TRANSCRIPT FILE. */
105 static frefid_t script_fref = NULL;
106 /* A STREAM FOR THE TRANSCRIPT FILE, WHEN IT'S OPEN. */
107 static strid_t script_stream = NULL;
108
109 int noun[4];
110 int player = 0;
111
112 static int noun3_backup;
113 static int player_backup = 0;
114
115 static int variable_contents;
116 int oec;
117 int *object_element_address,
118 *object_backup_address;
119
120 short int spaced = TRUE;
121
122 int delay = 0;
123
124 /* START OF GLK STUFF */
125
126 /* POINTERS TO THE GLK WINDOWS */
127 winid_t mainwin = NULL;
128 winid_t statuswin = NULL;
129 winid_t promptwin = NULL;
130 winid_t inputwin = NULL;
131 winid_t current_window = NULL;
132
133 /* POINTERS TO THE WINDOWS STREAMS */
134 strid_t mainstr = NULL;
135 strid_t statusstr = NULL;
136 strid_t promptstr = NULL;
137 strid_t inputstr = NULL;
138
139 /* END OF GLK STUFF */
140
141 char user_id[] = "local";
142 char prefix[81] = "\0";
143 char blorb[256] = "\0";
144 char game_path[256] = "\0";
145 char game_file[256] = "\0";
146 char processed_file[256] = "\0";
147
148 struct object_type *object[MAX_OBJECTS];
149 struct integer_type *integer_table = NULL;
150 struct cinteger_type *cinteger_table = NULL;
151 struct window_type *window_table = NULL;
152 struct attribute_type *attribute_table = NULL;
153 struct string_type *string_table = NULL;
154 struct string_type *cstring_table = NULL;
155 struct function_type *function_table = NULL;
156 struct function_type *executing_function = NULL;
157 struct command_type *completion_list = NULL;
158 struct word_type *grammar_table = NULL;
159 struct synonym_type *synonym_table = NULL;
160 struct filter_type *filter_table = NULL;
161
162 static void version_info(void);
163 static void jacl_set_window(winid_t new_window);
164 static void walking_thru(void);
165 static void scripting(void);
166 static void save_game_state(void);
167 static void restore_game_state(void);
168 static void word_check(void);
169 #ifdef READLINE
170 static char* object_generator(const char* text, int state);
171 static char* verb_generator(const char* text, int state);
172 static void add_word(const char * word);
173 #endif
174
175 static void jacl_strict_warning(const char *msg)
176 {
177 fprintf(stderr, "JACL error: %s\n", msg);
178 }
179
180 void
181 glk_main(void)
182 {
183 int index;
184
185 frefid_t blorb_file;
186
187 srand((int) time(NULL));
188
189 override[0] = 0;
190
191 /* ALLOC AN EVENT TO STORE A CANCELLED EVENT IN */
192 if ((cancelled_event = (event_t *) malloc(sizeof(event_t))) == NULL)
193 outofmem();
194
195 /* CREATE style_User1 FOR USE IN THE STATUS LINE */
196 glk_stylehint_set(wintype_TextGrid, style_User1, stylehint_ReverseColor, 1);
197 glk_stylehint_set(wintype_TextBuffer, style_User2, stylehint_ReverseColor, 1);
198
199 /* OPEN THE MAIN WINDOW THE GLK WINDOWS */
200 mainwin = glk_window_open(0, 0, 0, wintype_TextBuffer, 1);
201
202 if (!mainwin) {
203 /* IT'S POSSIBLE THAT THE MAIN WINDOW FAILED TO OPEN. THERE's
204 * NOTHING WE CAN DO WITHOUT IT, SO EXIT. */
205 return;
206 } else {
207 /* GET A REFERENCE TO mainwin's STREAM */
208 mainstr = glk_window_get_stream(mainwin);
209 }
210
211 /* SET THE CURRENT OUTPUT STREAM TO PRINT TO IT. */
212 jacl_set_window(mainwin);
213
214 /* OPEN A THIRD WINDOW: A TEXT GRID, BELOW THE MAIN WINDOW, ONE LINE
215 * HIGH. THIS IS THE WINDOW TO DISPLAY THE COMMAND PROMPT IN */
216 //promptwin = glk_window_open(mainwin, winmethod_Below | winmethod_Fixed,
217 // 3, wintype_TextBuffer, 0);
218
219 /* SET THIS TO DETERMINE THE SYTEM OF INPUT TO USE */
220 //inputwin = promptwin;
221 inputwin = mainwin;
222
223 if (jpp_error) {
224 /* THERE WAS AN ERROR DURING PREPROCESSING. NOW THAT THERE IS AN
225 * OPEN GLK WINDOW, OUTPUT THE ERROR MESSAGE AND EXIT */
226 log_error(error_buffer, FALSE);
227 terminate(200);
228 }
229
230 /* OPEN THE BLORB FILE IF ONE EXISTS */
231 #ifndef WINGLK
232 #ifdef GARGLK
233 // Per the Glk spec, Gargoyle appends ".glkdata" to files opened via the
234 // "normal" Glk routines (e.g. glk_fileref_create_by_name). JACL assumes
235 // that it can open arbitrary files, and for that,
236 // glkunix_stream_open_pathname is required.
237 blorb_stream = glkunix_stream_open_pathname(blorb, 0, 0);
238 #else
239 blorb_file = glk_fileref_create_by_name(fileusage_BinaryMode, blorb, 0);
240 #endif
241 #else
242 strcpy(temp_buffer, game_path);
243 strcat(temp_buffer, blorb);
244 strcpy(blorb, temp_buffer);
245 blorb_file = winglk_fileref_create_by_name(fileusage_BinaryMode, blorb, 0, 0);
246 #endif
247
248 // glkunix_stream_open_pathname does all this already.
249 #ifndef GARGLK
250 if (blorb_file != NULL && glk_fileref_does_file_exist(blorb_file)) {
251 blorb_stream = glk_stream_open_file(blorb_file, filemode_Read, 0);
252 #else
253 {
254 #endif
255
256 if (blorb_stream != NULL) {
257 /* IF THE FILE EXISTS, SET THE RESOURCE MAP */
258 giblorb_set_resource_map(blorb_stream);
259 }
260 }
261
262 // INTIALISE THE CSV PARSER
263 csv_init(&parser_csv, CSV_APPEND_NULL);
264
265 /* NO PREPROCESSOR ERRORS, LOAD THE GAME FILE */
266 read_gamefile();
267
268 execute ("+bootstrap");
269
270 // OPEN A SECOND WINDOW: A TEXT GRID, ABOVE THE MAIN WINDOW, ONE LINE
271 // HIGH. IT IS POSSIBLE THAT THIS WILL FAIL ALSO, BUT WE ACCEPT THAT.
272 statuswin = glk_window_open(mainwin, winmethod_Above | winmethod_Fixed,
273 0, wintype_TextGrid, 0);
274
275 // GET A REFERENCE TO statuswin's STREAM
276 if (statuswin != NULL) {
277 statusstr = glk_window_get_stream(statuswin);
278 }
279
280 #ifdef WINGLK
281 if ((resolved_string = cstring_resolve("game_title")) != NULL) {
282 winglk_window_set_title(resolved_string->value);
283 } else {
284 sprintf(temp_buffer, "JACL v%d.%d.%d ", J_VERSION, J_RELEASE, J_BUILD);
285 winglk_window_set_title(temp_buffer);
286 }
287 #endif
288
289 if (SOUND_SUPPORTED->value) {
290 /* CREATE THE EIGHT SOUND CHANNELS */
291 for (index = 0; index < 8; index++) {
292 sound_channel[index] = glk_schannel_create(0);
293 }
294 }
295
296 jacl_set_window(mainwin);
297
298 execute("+intro");
299
300 if (object[2] == NULL) {
301 log_error (CANT_RUN, PLUS_STDERR);
302 terminate(43);
303 }
304
305 /* DUMMY RETRIEVE OF 'HERE' FOR TESTING OF GAME STATE */
306 get_here();
307
308 eachturn();
309
310 /* TOP OF COMMAND LOOP */
311 do {
312 int gotline;
313 event_t ev;
314
315 custom_error = FALSE;
316
317 jacl_set_window(mainwin);
318
319 execute("+bottom");
320
321 status_line();
322
323 if (current_command != NULL) {
324 strcpy(last_command, current_command);
325 }
326
327 if (inputwin == promptwin) {
328 glk_window_clear(promptwin);
329 jacl_set_window(inputwin);
330 }
331
332 /* OUTPUT THE CUSTOM COMMAND PROMPT */
333 write_text(string_resolve("command_prompt")->value);
334
335 #ifdef NOUNICODE
336 glk_request_line_event(inputwin, command_buffer, 255, 0);
337 #else
338 glk_request_line_event_uni(inputwin, command_buffer_uni, 255, 0);
339 #endif
340
341 jacl_set_window(inputwin);
342
343 gotline = FALSE;
344
345 while (!gotline) {
346 /* GRAB AN EVENT. */
347 glk_select(&ev);
348
349 switch (ev.type) {
350
351 case evtype_LineInput:
352 if (ev.win == inputwin) {
353 gotline = TRUE;
354 jacl_set_window(mainwin);
355 /* REALLY THE EVENT CAN *ONLY* BE FROM MAINWIN,
356 * BECAUSE WE NEVER REQUEST LINE INPUT FROM THE
357 * STATUS WINDOW. BUT WE DO A PARANOIA TEST,
358 * BECAUSE COMMANDBUF IS ONLY FILLED IF THE LINE
359 * EVENT COMES FROM THE MAINWIN REQUEST. IF THE
360 * LINE EVENT COMES FROM ANYWHERE ELSE, WE IGNORE
361 * IT. */
362 }
363 break;
364
365 case evtype_SoundNotify:
366 /* A SOUND HAS FINISHED PLAYING CALL +sound_finished
367 * WITH THE RESOUCE NUMBER AS THE FIRST ARGUMENT
368 * AND THE CHANNEL NUMBER AS THE SECOND ARGUMENT */
369 sprintf(temp_buffer, "+sound_finished<%d<%d", (int) ev.val1, (int) ev.val2 - 1);
370 execute(temp_buffer);
371 break;
372
373 case evtype_Timer:
374 /* A TIMER EVENT IS TRIGGERED PERIODICALLY IF THE GAME
375 * REQUESTS THEM. THIS SIMPLY EXECUTES THE FUNCTION
376 * +timer WHICH IS LIKE +eachturn EXCEPT IT DOESN'T
377 * WAIT FOR THE PLAYER TO TYPE A COMMAND */
378
379 jacl_set_window(mainwin);
380 execute("+timer");
381 break;
382
383 case evtype_Arrange:
384 /* WINDOWS HAVE CHANGED SIZE, SO WE HAVE TO REDRAW THE
385 * STATUS WINDOW. */
386 status_line();
387 break;
388 }
389 }
390
391 // THE PLAYER'S INPUT WILL BE UTF-32. CONVERT IT TO UTF-8 AND NULL TERMINATE IT
392 #ifndef NOUNICODE
393 convert_to_utf8(command_buffer_uni, ev.val1);
394 #endif
395
396 current_command = command_buffer;
397
398 /* SET ALL THE OUTPUT TO GO TO mainwin NOW THE COMMAND HAS BEEN READ */
399 if (inputwin == promptwin) {
400 jacl_set_window(mainwin);
401 write_text(string_resolve("command_prompt")->value);
402 glk_set_style(style_Input);
403 write_text(current_command);
404 glk_set_style(style_Normal);
405 write_text("^");
406 }
407
408 execute("+top");
409
410 index = 0;
411
412 if (*current_command) {
413 while (*(current_command + index) && index < 1024) {
414 if (*(current_command + index) == '\r' || *(current_command + index) == '\n') {
415 break;
416 } else {
417 text_buffer[index] = *(current_command + index);
418 index++;
419 }
420 }
421 }
422
423 text_buffer[index] = 0;
424
425 if (text_buffer[0] == 0) {
426 /* NO COMMAND WAS SPECIFIED, FILL THE COMMAND IN AS 'blankjacl'
427 * FOR THE GAME TO PROCESS AS DESIRED */
428 strcpy(text_buffer, "blankjacl");
429 current_command = blank_command;
430 }
431
432 command_encapsulate();
433 jacl_truncate();
434
435 index = 0;
436
437 /* SET THE INTEGER INTERRUPTED TO FALSE. IF THIS IS SET TO
438 * TRUE BY ANY COMMAND, FURTHER PROCESSING WILL STOP */
439 INTERRUPTED->value = FALSE;
440
441 interrupted = FALSE;
442
443 if (word[0] != NULL) {
444 if (strcmp(word[0], "undo")) {
445 /* COMMAND DOES NOT EQUAL undo */
446 save_game_state();
447 }
448
449 if (word[0][0] == '*') {
450 if (script_stream) {
451 write_text(cstring_resolve("COMMENT_RECORDED")->value);
452 } else {
453 write_text(cstring_resolve("COMMENT_IGNORED")->value);
454 }
455 } else {
456 /* COMMAND IS NOT NULL, START PROCESSING IT */
457 preparse();
458 }
459 } else {
460 /* NO COMMAND WAS SPECIFIED, FILL THE COMMAND IN AS 'blankjacl'
461 * FOR THE GAME TO PROCESS AS DESIRED */
462 strcpy(text_buffer, "blankjacl");
463 command_encapsulate();
464 preparse();
465 }
466
467 } while (TRUE);
468 }
469
470 void
471 preparse()
472 {
473 int position;
474
475 // THE INTERRUPTED VARIABLE IS USED TO STOP LATER ACTIONS IN A COMMAND
476 // IF ANY ONE
477 while (word[wp] != NULL && INTERRUPTED->value == FALSE) {
478 //printf("--- preparse %s\n", word[wp]);
479 // PROCESS THE CURRENT COMMAND
480 // CREATE THE command STRINGS FROM THIS POINT ONWARDS SO THE VERB OF
481 // THE CURRENT COMMAND IS ALWAYS command[0].
482
483 clear_cstring("command");
484
485 position = wp;
486
487 while (word[position] != NULL && strcmp(word[position], cstring_resolve("THEN_WORD")->value)) {
488 add_cstring ("command", word[position]);
489 position++;
490 };
491
492 // PROCESS THE COMMAND
493 word_check();
494
495 /* THE PREVIOUS COMMAND HAS FINISHED, LOOK FOR ANOTHER COMMAND */
496 while (word[wp] != NULL) {
497 if (word[wp] != NULL && !strcmp(word[wp], cstring_resolve("THEN_WORD")->value)) {
498 wp++;
499 break;
500 }
501 wp++;
502 }
503 }
504 }
505
506 void
507 word_check()
508 {
509 int index;
510
511 /* REMEMBER THE START OF THIS COMMAND TO SUPPORT 'oops' AND 'again' */
512 start_of_this_command = wp;
513 //printf("--- command starts at %d\n", start_of_this_command);
514
515 /* START CHECKING THE PLAYER'S COMMAND FOR SYSTEM COMMANDS */
516 if (!strcmp(word[wp], cstring_resolve("QUIT_WORD")->value) || !strcmp(word[wp], "q")) {
517 if (execute("+quit_game") == FALSE) {
518 TIME->value = FALSE;
519 write_text(cstring_resolve("SURE_QUIT")->value);
520 if (get_yes_or_no()) {
521 newline();
522 execute("+score");
523 terminate(0);
524 } else {
525 write_text(cstring_resolve("RETURN_GAME")->value);
526 }
527 }
528 } else if (!strcmp(word[wp], cstring_resolve("RESTART_WORD")->value)) {
529 if (execute("+restart_game") == FALSE) {
530 TIME->value = FALSE;
531 write_text(cstring_resolve("SURE_RESTART")->value);
532 if (get_yes_or_no()) {
533 write_text(cstring_resolve("RESTARTING")->value);
534 restart_game();
535 glk_window_clear(current_window);
536 execute ("+intro");
537 eachturn();
538 } else {
539 write_text(cstring_resolve("RETURN_GAME")->value);
540 }
541 }
542 } else if (!strcmp(word[wp], cstring_resolve("UNDO_WORD")->value)) {
543 if (execute("+undo_move") == FALSE) {
544 undoing();
545 }
546 } else if (!strcmp(word[wp], cstring_resolve("OOPS_WORD")->value) || !strcmp(word[wp], "o")) {
547 //printf("--- oops word is %d\n", oops_word);
548 if (word[++wp] != NULL) {
549 if (oops_word == -1) {
550 if (TOTAL_MOVES->value == 0) {
551 write_text(cstring_resolve("NO_MOVES")->value);
552 TIME->value = FALSE;
553 } else {
554 write_text(cstring_resolve("CANT_CORRECT")->value);
555 TIME->value = FALSE;
556 }
557 } else {
558 strcpy(oops_buffer, word[wp]);
559 strcpy(text_buffer, last_command);
560 command_encapsulate();
561 //printf("--- trying to replace %s with %s\n", word[oops_word], oops_buffer);
562 jacl_truncate();
563 word[oops_word] = (char *) &oops_buffer;
564
565 /* BUILD A PLAIN STRING REPRESENTING THE NEW COMMAND */
566 oopsed_current[0] = 0;
567 index = 0;
568
569 while (word[index] != NULL) {
570 if (oopsed_current[0] != 0) {
571 strcat(oopsed_current, " ");
572 }
573
574 strcat(oopsed_current, word[index]);
575
576 index++;
577 }
578
579 current_command = oopsed_current;
580 //printf("--- current command is: %s\n", current_command);
581
582 /* PROCESS THE FIXED COMMAND ONLY */
583 wp = start_of_last_command;
584 word_check();
585 }
586 } else {
587 write_text(cstring_resolve("BAD_OOPS")->value);
588 TIME->value = FALSE;
589 }
590 } else if (!strcmp(word[wp], cstring_resolve("AGAIN_WORD")->value) || !strcmp(word[wp], "g")) {
591 if (TOTAL_MOVES->value == 0) {
592 write_text(cstring_resolve("NO_MOVES")->value);
593 TIME->value = FALSE;
594 } else if (last_command[0] == 0) {
595 write_text(cstring_resolve("NOT_CLEVER")->value);
596 TIME->value = FALSE;
597 } else {
598 strcpy(current_command, last_command);
599 strcpy(text_buffer, last_command);
600 command_encapsulate();
601 jacl_truncate();
602 wp = start_of_last_command;
603 word_check();
604 }
605 } else if (!strcmp(word[wp], cstring_resolve("SCRIPT_WORD")->value) || !strcmp(word[wp], "transcript")) {
606 scripting();
607 } else if (!strcmp(word[wp], cstring_resolve("UNSCRIPT_WORD")->value)) {
608 if (!script_stream) {
609 write_text(cstring_resolve("SCRIPTING_ALREADY_OFF")->value);
610 } else {
611 /* Close the file. */
612 glk_put_string_stream(script_stream, "\nEND OF A TRANSCRIPT\n");
613 glk_stream_close(script_stream, NULL);
614 write_text(cstring_resolve("SCRIPTING_OFF")->value);
615 script_stream = NULL;
616 }
617 } else if (!strcmp(word[wp], cstring_resolve("WALKTHRU_WORD")->value)) {
618 walking_thru();
619 } else if (!strcmp(word[wp], cstring_resolve("INFO_WORD")->value) || !strcmp(word[wp], "version")) {
620 version_info();
621 write_text("you can redistribute it and/or modify it under the ");
622 write_text("terms of the GNU General Public License as published by ");
623 write_text("the Free Software Foundation; either version 2 of the ");
624 write_text("License, or any later version.^^");
625 write_text("This program is distributed in the hope that it will be ");
626 write_text("useful, but WITHOUT ANY WARRANTY; without even the ");
627 write_text("implied warranty of MERCHANTABILITY or FITNESS FOR A ");
628 write_text("PARTICULAR PURPOSE. See the GNU General Public License ");
629 write_text("for more details.^^");
630 write_text("You should have received a copy of the GNU General ");
631 write_text("Public License along with this program; if not, write ");
632 write_text("to the Free Software Foundation, Inc., 675 Mass Ave, ");
633 write_text("Cambridge, MA 02139, USA.^^");
634 sprintf(temp_buffer, "OBJECTS DEFINED: %d^", objects);
635 write_text(temp_buffer);
636 TIME->value = FALSE;
637 } else {
638 /* NO WORD HAS BEEN MARKED AS AN ERROR YET*/
639 oops_word = -1;
640
641 /* THIS IS NOT A SYSTEM COMMAND, CALL parser TO PROCESS THE COMMAND */
642 parser();
643 }
644
645 start_of_last_command = start_of_this_command;
646 }
647
648 void
649 version_info()
650 {
651 char buffer[80];
652
653 sprintf(buffer, "JACL Interpreter v%d.%d.%d ", J_VERSION, J_RELEASE,
654 J_BUILD);
655 write_text(buffer);
656 sprintf(buffer, "/ %d object.^", MAX_OBJECTS);
657 write_text(buffer);
658 write_text("Copyright (c) 1992-2010 Stuart Allen.^^");
659 }
660
661 void
662 save_game_state()
663 {
664 /* THIS FUNCTION MAKES AN IN-MEMORY COPY OF THE GAME STATE AFTER EACH
665 * OF THE PLAYER'S COMMANDS SO THE 'undo' COMMAND CAN BE USED */
666 int index,
667 counter;
668
669 struct integer_type *current_integer = integer_table;
670 struct function_type *current_function = function_table;
671
672 do {
673 current_function->call_count_backup = current_function->call_count;
674 current_function = current_function->next_function;
675 }
676 while (current_function != NULL);
677
678 do {
679 current_integer->value_backup = current_integer->value;
680 current_integer = current_integer->next_integer;
681 }
682 while (current_integer != NULL);
683
684 for (index = 1; index <= objects; index++) {
685 if (object[index]->nosave)
686 continue;
687
688 for (counter = 0; counter < 16; counter++) {
689 object[index]->integer_backup[counter] =
690 object[index]->integer[counter];
691 }
692
693 object[index]->attributes_backup = object[index]->attributes;
694 object[index]->user_attributes_backup = object[index]->user_attributes;
695 }
696
697 player_backup = player;
698 noun3_backup = noun[3];
699 }
700
701 int
702 save_interaction(const char *filename)
703 {
704 frefid_t saveref;
705
706 jacl_set_window(inputwin);
707
708 if (inputwin == promptwin) {
709 glk_window_clear(promptwin);
710 newline();
711 }
712
713 if (filename == NULL) {
714 saveref = glk_fileref_create_by_prompt(fileusage_SavedGame | fileusage_BinaryMode, filemode_Write, 0);
715 } else {
716 saveref = glk_fileref_create_by_name(fileusage_SavedGame | fileusage_BinaryMode, (char*)filename, 0);
717
718 }
719
720 jacl_set_window(mainwin);
721
722 if (!saveref) {
723 write_text(cstring_resolve("CANT_SAVE")->value);
724 return (FALSE);
725 } else {
726 if (save_game(saveref)) {
727 return (TRUE);
728 } else {
729 write_text(cstring_resolve("CANT_SAVE")->value);
730 return (FALSE);
731 }
732 }
733 }
734
735 void
736 restore_game_state()
737 {
738 /* THIS FUNCTION IS CALLED AS A RESULT OF THE PLAYER USING THE 'undo'
739 * COMMAND */
740 int index,
741 counter;
742
743 struct integer_type *current_integer = integer_table;
744 struct function_type *current_function = function_table;
745
746 do {
747 current_function->call_count = current_function->call_count_backup;
748 current_function = current_function->next_function;
749 }
750 while (current_function != NULL);
751
752
753 do {
754 current_integer->value = current_integer->value_backup;
755 current_integer = current_integer->next_integer;
756 }
757 while (current_integer != NULL);
758
759 for (index = 1; index <= objects; index++) {
760 if (object[index]->nosave)
761 continue;
762
763 for (counter = 0; counter < 16; counter++)
764 object[index]->integer[counter] =
765 object[index]->integer_backup[counter];
766
767 object[index]->attributes = object[index]->attributes_backup;
768 object[index]->user_attributes = object[index]->user_attributes_backup;
769 }
770
771 player = player_backup;
772 noun[3] = noun3_backup;
773
774 write_text(cstring_resolve("MOVE_UNDONE")->value);
775 object[HERE]->attributes &= ~1;
776 execute("+top");
777 execute ("+look_around");
778 execute("+bottom");
779 TIME->value = FALSE;
780 }
781
782 void
783 write_text(const char *string_buffer)
784 {
785 int index,
786 length;
787
788 if (!strcmp(string_buffer, "tilde")) {
789 glk_put_string("~");
790 return;
791 } else if (!strcmp(string_buffer, "caret")) {
792 glk_put_string("^");
793 return;
794 }
795
796 length = strlen(string_buffer);
797
798 for (index = 0; index < length; index++) {
799 if (*(string_buffer + index) == '^') {
800 chunk_buffer[index] = '\n';
801 } else if (*(string_buffer + index) == '~') {
802 chunk_buffer[index] = '\"';
803 } else {
804 chunk_buffer[index] = *(string_buffer + index);
805 }
806 }
807
808 chunk_buffer[index] = 0;
809
810 /* PRINT THE CONTENTS OF string_buffer */
811 #ifdef NOUNICODE
812 glk_put_string(chunk_buffer);
813 #else
814 chunk_buffer_uni[(glui32) convert_to_utf32(chunk_buffer)] = 0;
815 glk_put_string_uni(chunk_buffer_uni);
816 #endif
817 }
818
819 void
820 jacl_sleep(unsigned int mseconds)
821 {
822 int multiplier = CLOCKS_PER_SEC / 1000;
823
824 /* WAIT FOR A GIVEN NUMBER OF MILLISECONDS */
825 clock_t goal = (mseconds * multiplier) + clock();
826 while (goal > clock());
827 }
828
829 void
830 status_line()
831 {
832 int cursor, index;
833 winid_t pair_window;
834
835 if (!statuswin) {
836 return;
837 } else {
838 // THERE IS AN EXISTING STATUS WINDOW, MAKE SURE A NEW SIZE HASN'T BEEN
839 // REQUESTED
840 glk_window_get_size(statuswin, &status_width, &status_height);
841 if (status_height != integer_resolve("status_window")->value) {
842 // HEIGHT HAS CHANGED, UPDATE THE WINDOW
843 pair_window = glk_window_get_parent(statuswin);
844 glk_window_set_arrangement(pair_window, winmethod_Above | winmethod_Fixed, integer_resolve("status_window")->value, statuswin);
845 glk_window_get_size(statuswin, &status_width, &status_height);
846 }
847 }
848
849 if (status_height == 0) {
850 // THE STATUS WINDOW CAN'T BE CLOSED, ONLY SET TO HAVE A HEIGHT OF ZERO
851 return;
852 }
853
854 jacl_set_window(statuswin);
855 glk_window_clear(statuswin);
856
857 if (execute("+update_status_window") == FALSE) {
858 glk_set_style(style_User1);
859
860 /* DISPLAY THE INVERSE STATUS LINE AT THE TOP OF THE SCREEN */
861 for (index = 0; index < status_width; index++) {
862 temp_buffer[index] = ' ';
863 }
864 temp_buffer[index] = 0;
865 write_text(temp_buffer);
866
867 /* PRINT THE LOCATION'S TITLE ON THE LEFT. */
868 glk_window_move_cursor(statuswin, 1, 0);
869 write_text(sentence_output(HERE, TRUE));
870
871 /* BUILD THE SCORE/ MOVES STRING */
872 temp_buffer[0] = 0;
873 sprintf (temp_buffer, "Score: %d Moves: %d", SCORE->value, TOTAL_MOVES->value);
874
875 cursor = status_width - strlen(temp_buffer);
876 cursor--;
877 glk_window_move_cursor(statuswin, cursor, 0);
878 write_text(temp_buffer);
879 }
880
881 jacl_set_window(mainwin);
882
883 }
884
885 void
886 newline()
887 {
888 /* START A NEW LINE ON THE SCREEN */
889 write_text("\n");
890 }
891
892 void
893 more(const char* message)
894 {
895 int character;
896
897 jacl_set_window(inputwin);
898
899 if (inputwin == promptwin) {
900 glk_window_clear(promptwin);
901 newline();
902 }
903
904 glk_set_style(style_Emphasized);
905 write_text(message);
906 glk_set_style(style_Normal);
907
908 character = get_key();
909
910 if (inputwin == mainwin) newline();
911 }
912
913 int
914 get_key()
915 {
916 event_t ev;
917
918 glk_request_char_event (inputwin);
919
920 while(1) {
921
922 glk_select(&ev);
923
924 switch (ev.type) {
925 case evtype_CharInput:
926 if (ev.win == inputwin) {
927 return (ev.val1);
928 }
929 break;
930 }
931 }
932
933 }
934
935 int
936 get_number(int insist, int low, int high)
937 {
938 char *cx;
939 char commandbuf[256];
940 int response;
941 int gotline;
942 event_t ev;
943
944 status_line();
945
946 sprintf(temp_buffer, cstring_resolve("TYPE_NUMBER")->value, low, high);
947
948 /* THIS LOOP IS IDENTICAL TO THE MAIN COMMAND LOOP IN glk_main(). */
949
950 while (1) {
951 if (inputwin == promptwin) {
952 glk_window_clear(promptwin);
953 jacl_set_window(inputwin);
954 }
955
956 write_text(temp_buffer);
957 jacl_set_window(mainwin);
958
959 glk_request_line_event(inputwin, commandbuf, 255, 0);
960
961 gotline = FALSE;
962 while (!gotline) {
963
964 glk_select(&ev);
965
966 switch (ev.type) {
967 case evtype_LineInput:
968 if (ev.win == inputwin) {
969 gotline = TRUE;
970 }
971 break;
972
973 case evtype_Arrange:
974 status_line();
975 break;
976 }
977 }
978
979 commandbuf[ev.val1] = '\0';
980 for (cx = commandbuf; *cx == ' '; cx++) { };
981
982 if (validate(cx)) {
983 response = atoi(cx);
984 if (response >= low && response <= high) {
985 return (response);
986 }
987 }
988
989 if (!insist) {
990 return (-1);
991 } else {
992 write_text(cstring_resolve("INVALID_SELECTION")->value);
993 }
994 }
995 }
996
997 void
998 get_string(char *string_buffer)
999 {
1000 char *cx;
1001 char commandbuf[256];
1002 int gotline;
1003 event_t ev;
1004
1005 status_line();
1006
1007 /* THIS LOOP IS IDENTICAL TO THE MAIN COMMAND LOOP IN glk_main(). */
1008
1009 if (inputwin == promptwin) {
1010 glk_window_clear(promptwin);
1011 jacl_set_window(inputwin);
1012 }
1013
1014 jacl_set_window(mainwin);
1015
1016 glk_request_line_event(inputwin, commandbuf, 255, 0);
1017
1018 gotline = FALSE;
1019 while (!gotline) {
1020
1021 glk_select(&ev);
1022
1023 switch (ev.type) {
1024 case evtype_LineInput:
1025 if (ev.win == inputwin) {
1026 gotline = TRUE;
1027 }
1028 break;
1029
1030 case evtype_Arrange:
1031 status_line();
1032 break;
1033 }
1034 }
1035
1036 commandbuf[ev.val1] = '\0';
1037 for (cx = commandbuf; *cx == ' '; cx++) { };
1038
1039 // COPY UP TO 255 BYTES OF THE ENTERED TEXT INTO THE SUPPLIED STRING
1040 strncpy (string_buffer, cx, 255);
1041 }
1042
1043 int
1044 get_yes_or_no(void)
1045 {
1046 char *cx;
1047 char commandbuf[256];
1048 int gotline;
1049 event_t ev;
1050
1051 status_line();
1052
1053 /* THIS LOOP IS IDENTICAL TO THE MAIN COMMAND LOOP IN glk_main(). */
1054
1055 while (1) {
1056 if (inputwin == promptwin) {
1057 glk_window_clear(promptwin);
1058 jacl_set_window(inputwin);
1059 }
1060
1061 write_text(cstring_resolve("YES_OR_NO")->value);
1062 jacl_set_window(mainwin);
1063
1064 glk_request_line_event(inputwin, commandbuf, 255, 0);
1065
1066 gotline = FALSE;
1067 while (!gotline) {
1068
1069 glk_select(&ev);
1070
1071 switch (ev.type) {
1072 case evtype_LineInput:
1073 if (ev.win == inputwin) {
1074 gotline = TRUE;
1075 }
1076 break;
1077
1078 case evtype_Arrange:
1079 status_line();
1080 break;
1081 }
1082 }
1083
1084 commandbuf[ev.val1] = '\0';
1085 for (cx = commandbuf; *cx == ' '; cx++) { };
1086
1087 // PUSH THE FIRST NON-SPACE CHARACTER TO LOWER FOR COMPARISON
1088 // WITH CONSTANT
1089 *cx = tolower(*cx);
1090
1091 if (*cx == cstring_resolve("YES_WORD")->value[0]) {
1092 return TRUE;
1093 } else if (*cx == cstring_resolve("NO_WORD")->value[0]) {
1094 return FALSE;
1095 }
1096
1097 }
1098 }
1099
1100 char
1101 get_character(const char *message)
1102 {
1103 char *cx;
1104 char commandbuf[256];
1105 int gotline;
1106 event_t ev;
1107
1108 status_line();
1109
1110 /* THIS LOOP IS IDENTICAL TO THE MAIN COMMAND LOOP IN glk_main(). */
1111
1112 while (1) {
1113 if (inputwin == promptwin) {
1114 glk_window_clear(promptwin);
1115 jacl_set_window(inputwin);
1116 }
1117
1118 write_text(message);
1119 glk_request_line_event(inputwin, commandbuf, 255, 0);
1120 jacl_set_window(mainwin);
1121
1122 gotline = FALSE;
1123 while (!gotline) {
1124
1125 glk_select(&ev);
1126
1127 switch (ev.type) {
1128 case evtype_LineInput:
1129 if (ev.win == inputwin) {
1130 gotline = TRUE;
1131 }
1132 break;
1133
1134 case evtype_Arrange:
1135 status_line();
1136 break;
1137 }
1138 }
1139
1140 commandbuf[ev.val1] = '\0';
1141 for (cx = commandbuf; *cx == ' '; cx++) { };
1142
1143 return (*cx);
1144 }
1145 }
1146
1147 strid_t
1148 open_glk_file (glui32 usage, glui32 mode, const char *filename)
1149 {
1150
1151 frefid_t file_reference;
1152 strid_t stream_reference;
1153
1154 file_reference = glk_fileref_create_by_name(usage, (char *)filename, 0);
1155
1156 if (file_reference) {
1157 stream_reference = glk_stream_open_file(file_reference, mode, 0);
1158
1159 if (stream_reference) {
1160 /* WE'RE DONE WITH THE FILE REFERENCE NOW THAT THE STREAM
1161 * HAS BEEN SUCCESSFULLY OPENED */
1162 glk_fileref_destroy (file_reference);
1163
1164 return (stream_reference);
1165 }
1166 }
1167
1168 return (strid_t) NULL;
1169 }
1170
1171 void
1172 scripting()
1173 {
1174 if (script_stream) {
1175 write_text(cstring_resolve("SCRIPTING_ALREADY_ON")->value);
1176 return;
1177 }
1178
1179 /* IF WE'VE TURNED ON SCRIPTING BEFORE, USE THE SAME FILE REFERENCE;
1180 * OTHERWISE, PROMPT THE PLAYER FOR A FILE. */
1181 if (!script_fref) {
1182 script_fref = glk_fileref_create_by_prompt(
1183 fileusage_Transcript | fileusage_TextMode,
1184 filemode_WriteAppend, 0);
1185 if (!script_fref) {
1186 write_text(cstring_resolve("CANT_WRITE_SCRIPT")->value);
1187 return;
1188 }
1189 }
1190
1191 /* OPEN THE TRANSCRIPT FILE */
1192 script_stream = glk_stream_open_file(script_fref, filemode_WriteAppend, 0);
1193
1194 if (!script_stream) {
1195 write_text(cstring_resolve("CANT_WRITE_SCRIPT")->value);
1196 return;
1197 }
1198 write_text(cstring_resolve("SCRIPTING_ON")->value);
1199 glk_window_set_echo_stream(mainwin, script_stream);
1200 glk_put_string_stream(script_stream, "TRANSCRIPT OF: ");
1201 glk_put_string_stream(script_stream, cstring_resolve("game_title")->value);
1202 glk_put_string_stream(script_stream, "\n");
1203 }
1204
1205 void
1206 undoing()
1207 {
1208 if (TOTAL_MOVES->value && strcmp(last_command, cstring_resolve("UNDO_WORD")->value)) {
1209 restore_game_state();
1210 } else {
1211 write_text(cstring_resolve("NO_UNDO")->value);
1212 TIME->value = FALSE;
1213 }
1214 }
1215
1216 void
1217 walking_thru()
1218 {
1219 int result, index;
1220
1221 int length;
1222 char script_line[81];
1223
1224 /* A FILE REFERENCE FOR THE WALKTHRU FILE. */
1225 frefid_t walkthru_fref = NULL;
1226
1227 /* A STREAM FOR THE WALKTHRU FILE, WHEN IT'S OPEN. */
1228 strid_t walkthru_stream = NULL;
1229
1230 walkthru_fref = glk_fileref_create_by_prompt(fileusage_Data | fileusage_TextMode, filemode_Read, 0);
1231
1232 if (!walkthru_fref) {
1233 write_text (cstring_resolve("ERROR_READING_WALKTHRU")->value);
1234 return;
1235 }
1236
1237 /* OPEN THE WALKTHRU FILE */
1238 walkthru_stream = glk_stream_open_file(walkthru_fref, filemode_Read, 0);
1239
1240 if (!walkthru_stream) {
1241 write_text(cstring_resolve("ERROR_READING_WALKTHRU")->value);
1242 return;
1243 }
1244
1245 walkthru_running = TRUE;
1246
1247 /* ISSUE ALL THE COMMANDS STORE IN THE WALKTHRU FILE */
1248
1249 /* WE'RE DONE WITH THE FILE REFERENCE NOW THAT THE STREAM
1250 * HAS BEEN SUCCESSFULLY OPENED */
1251 glk_fileref_destroy (walkthru_fref);
1252
1253 result = glk_get_line_stream(walkthru_stream, text_buffer, (glui32) 80);
1254
1255 /* SET TO LOWER CASE AND STRIP NEWLINES */
1256 length = strlen(text_buffer);
1257 for (index = 0; index < length; index++) {
1258 if (text_buffer[index] == '\r' ||
1259 text_buffer[index] == '\n') {
1260 text_buffer[index] = 0;
1261 break;
1262 }
1263 }
1264
1265 strcpy(script_line, text_buffer);
1266
1267 while (result && INTERRUPTED->value == FALSE) {
1268 /* THERE COULD BE A LOT OF PROCESSING GOING ON HERE BEFORE GETTING
1269 * TO THE NEXT EVENT LOOP SO CALL glk_tick AFTER EACH LINE READ */
1270 glk_tick();
1271 command_encapsulate();
1272 jacl_truncate();
1273 if (word[0] != NULL) {
1274 custom_error = FALSE;
1275
1276 execute("+bottom");
1277
1278 write_text(string_resolve("command_prompt")->value);
1279 glk_set_style(style_Input);
1280 write_text(script_line);
1281 newline();
1282 glk_set_style(style_Normal);
1283
1284 execute("+top");
1285
1286 preparse();
1287 }
1288
1289 result = glk_get_line_stream(walkthru_stream, text_buffer, (glui32) 80);
1290
1291 /* SET TO LOWER CASE AND STRIP NEWLINES */
1292 length = strlen(text_buffer);
1293 for (index = 0; index < length; index++) {
1294 if (text_buffer[index] == '\r' ||
1295 text_buffer[index] == '\n') {
1296 text_buffer[index] = 0;
1297 break;
1298 }
1299 }
1300
1301 strcpy(script_line, text_buffer);
1302 }
1303
1304 /* CLOSE THE STREAM */
1305 glk_stream_close(walkthru_stream, NULL);
1306
1307 /* FINISH UP */
1308 walkthru_running = FALSE;
1309 }
1310
1311 int
1312 restore_interaction(const char *filename)
1313 {
1314 frefid_t saveref;
1315
1316 jacl_set_window(inputwin);
1317
1318 if (inputwin == promptwin) {
1319 glk_window_clear(promptwin);
1320 newline();
1321 }
1322
1323 if (filename == NULL) {
1324 saveref = glk_fileref_create_by_prompt(fileusage_SavedGame | fileusage_BinaryMode, filemode_Read, 0);
1325 } else {
1326 saveref = glk_fileref_create_by_name(fileusage_SavedGame | fileusage_BinaryMode, (char*)filename, 0);
1327 }
1328
1329 jacl_set_window(mainwin);
1330
1331 if (!saveref) {
1332 write_text(cstring_resolve("CANT_RESTORE")->value);
1333 return (FALSE);
1334 }
1335
1336 if (restore_game(saveref, TRUE) == FALSE) {
1337 write_text(cstring_resolve("CANT_RESTORE")->value);
1338 return (FALSE);
1339 } else {
1340 return (TRUE);
1341 }
1342 }
1343
1344 glui32
1345 glk_get_bin_line_stream(strid_t file_stream, char *buffer, glui32 max_length)
1346 {
1347 glsi32 character = 0;
1348
1349 register int index = 0;
1350
1351 character = glk_get_char_stream(file_stream);
1352 while (character != -1 && index < (int) max_length) {
1353 *(buffer + index) = (char) character;
1354 index++;
1355 if (character == (glsi32) '\n' ||
1356 character == (glsi32) '\r') {
1357 break;
1358 }
1359 character = glk_get_char_stream(file_stream);
1360 };
1361
1362 *(buffer + index) = 0;
1363
1364 return ((glui32) index);
1365 }
1366
1367 void
1368 jacl_set_window(winid_t new_window)
1369 {
1370 current_window = new_window;
1371 glk_set_window(new_window);
1372 }
1373
1374 #ifdef READLINE
1375 char **
1376 command_completion(char* text, int start, int end)
1377 {
1378 /* READLINE TAB COMPLETION CODE */
1379 char **options;
1380
1381 options = (char **) NULL;
1382
1383 if (start == 0)
1384 options = completion_matches (text, verb_generator);
1385 else
1386 options = completion_matches (text, object_generator);
1387
1388 return (options);
1389 }
1390
1391 char *
1392 object_generator(char* text, int state)
1393 {
1394 static int len;
1395 static struct command_type* now;
1396 struct command_type* to_send;
1397 struct name_type *current_name = (struct name_type *) NULL;
1398
1399 /* IF THIS IS A NEW WORD TO COMPLETE, INITIALIZE NOW. THIS INCLUDES
1400 SAVING THE LENGTH OF TEXT FOR EFFICIENCY, AND INITIALIZING THE INDEX
1401 VARIABLE TO 0. */
1402
1403 if (!state) {
1404 /* BUILD THE LIST */
1405 int index;
1406 completion_list = NULL;
1407
1408 /* LOOP THROUGH ALL THE OBJECTS AND SEE IF THEY ARE IN
1409 THE CURRENT LOCATION */
1410 for (index = 1; index <= objects; index++) {
1411 if (parent_of (HERE, index, UNRESTRICT) && !(object[index]->attributes & NO_TAB)) {
1412 /* LOOP THROUGH ALL THE OBJECTS NAMES AND
1413 THEM TO THE COMPLETION LIST */
1414 current_name = object[index]->first_name;
1415 while (current_name) {
1416 add_word (current_name->name);
1417 current_name = current_name->next_name;
1418 }
1419 }
1420 }
1421 now = completion_list;
1422 len = strlen (text);
1423 }
1424
1425 while (now != NULL) {
1426 if (!strncmp(text, now->word, len)) {
1427 to_send = now;
1428 now = now->next;
1429 return ((char*) to_send->word);
1430 }
1431 now = now->next;
1432 }
1433
1434 return (char *) NULL;
1435 }
1436
1437 char *
1438 verb_generator(char* text, int state)
1439 {
1440 static int len;
1441 static struct command_type* now;
1442 struct command_type* to_send;
1443 struct word_type *pointer;
1444
1445 /* IF THIS IS A NEW WORD TO COMPLETE, INITIALIZE NOW. THIS INCLUDES
1446 SAVING THE LENGTH OF TEXT FOR EFFICIENCY, AND INITIALIZING THE INDEX
1447 VARIABLE TO 0. */
1448
1449 if (!state) {
1450 /* BUILD THE LIST */
1451 completion_list = NULL;
1452
1453 pointer = grammar_table;
1454 while(pointer != NULL) {
1455 add_word(pointer->word);
1456 pointer = pointer->next_sibling;
1457 }
1458
1459 add_word("walkthru");
1460
1461 now = completion_list;
1462 len = strlen (text);
1463 }
1464
1465 while (now != NULL) {
1466 if (!strncmp(text, now->word, len)) {
1467 to_send = now;
1468 now = now->next;
1469
1470 /* MALLOC A COPY AND RETURN A POINTER TO THE COPY */
1471 return ((char*) to_send->word);
1472 }
1473 now = now->next;
1474 }
1475
1476 return (char *) NULL;
1477 }
1478
1479 /* ADD A COPY OF STRING TO A LIST OF STRINGS IF IT IS NOT
1480 ALREADY IN THE LIST. THIS IS FOR THE USE OF READLINE */
1481 void
1482 add_word(const char * word)
1483 {
1484 static struct command_type *current_word = NULL;
1485 struct command_type *previous_word = NULL;
1486
1487 /* DON'T ADD WORDS SUCH AS *present TO THE LIST*/
1488 if (*word == '*')
1489 return;
1490
1491 if (current_word != NULL)
1492 previous_word = current_word;
1493
1494 current_word = (struct command_type *) malloc(sizeof(struct command_type));
1495
1496 if (current_word != NULL) {
1497 if (completion_list == NULL) {
1498 completion_list = current_word;
1499 }
1500
1501 strncpy(current_word->word, word, 40);
1502 current_word->word[40] = 0;
1503 current_word->next = NULL;
1504
1505 if (previous_word != NULL) {
1506 previous_word->next = current_word;
1507 }
1508 }
1509 }
1510 #endif
1511
1512 void
1513 convert_to_utf8(glui32 *text, int len) {
1514 int i, k;
1515
1516 i = 0;
1517 k = 0;
1518
1519 /*convert UTF-32 to UTF-8 */
1520 while (i < len) {
1521 if (text[i] < 0x80) {
1522 command_buffer[k] = text[i];
1523 k++;
1524 }
1525 else if (text[i] < 0x800) {
1526 command_buffer[k ] = (0xC0 | ((text[i] & 0x7C0) >> 6));
1527 command_buffer[k+1] = (0x80 | (text[i] & 0x03F) );
1528 k = k + 2;
1529 }
1530 else if (text[i] < 0x10000) {
1531 command_buffer[k ] = (0xE0 | ((text[i] & 0xF000) >> 12));
1532 command_buffer[k+1] = (0x80 | ((text[i] & 0x0FC0) >> 6));
1533 command_buffer[k+2] = (0x80 | (text[i] & 0x003F) );
1534 k = k + 3;
1535 }
1536 else if (text[i] < 0x200000) {
1537 command_buffer[k ] = (0xF0 | ((text[i] & 0x1C0000) >> 18));
1538 command_buffer[k+1] = (0x80 | ((text[i] & 0x03F000) >> 12));
1539 command_buffer[k+2] = (0x80 | ((text[i] & 0x000FC0) >> 6));
1540 command_buffer[k+3] = (0x80 | (text[i] & 0x00003F) );
1541 k = k + 4;
1542 }
1543 else {
1544 command_buffer[k] = '?';
1545 k++;
1546 }
1547 i++;
1548 }
1549
1550 /* null-terminated string */
1551 command_buffer[k] = '\0';
1552 }
1553
1554 #ifndef NOUNICODE
1555 int
1556 convert_to_utf32 (unsigned char *text)
1557 {
1558 int text_len;
1559 int rlen;
1560
1561 if (!text) {
1562 return 0;
1563 }
1564
1565 text_len = strlen((char *)text);
1566
1567 if (!text_len) {
1568 return 0;
1569 }
1570
1571 rlen = (int) parse_utf8(text, text_len, chunk_buffer_uni, text_len);
1572
1573 return (rlen);
1574 }
1575
1576 glui32
1577 parse_utf8(unsigned char *buf, glui32 buflen,
1578 glui32 *out, glui32 outlen)
1579 {
1580 glui32 pos = 0;
1581 glui32 outpos = 0;
1582 glui32 res;
1583 glui32 val0, val1, val2, val3;
1584
1585 while (outpos < outlen) {
1586 if (pos >= buflen)
1587 break;
1588
1589 val0 = buf[pos++];
1590
1591 if (val0 < 0x80) {
1592 res = val0;
1593 out[outpos++] = res;
1594 continue;
1595 }
1596
1597 if ((val0 & 0xe0) == 0xc0) {
1598 if (pos+1 > buflen) {
1599 jacl_strict_warning("incomplete two-byte character");
1600 break;
1601 }
1602 val1 = buf[pos++];
1603 if ((val1 & 0xc0) != 0x80) {
1604 jacl_strict_warning("malformed two-byte character");
1605 break;
1606 }
1607 res = (val0 & 0x1f) << 6;
1608 res |= (val1 & 0x3f);
1609 out[outpos++] = res;
1610 continue;
1611 }
1612
1613 if ((val0 & 0xf0) == 0xe0) {
1614 if (pos+2 > buflen) {
1615 jacl_strict_warning("incomplete three-byte character");
1616 break;
1617 }
1618 val1 = buf[pos++];
1619 val2 = buf[pos++];
1620 if ((val1 & 0xc0) != 0x80) {
1621 jacl_strict_warning("malformed three-byte character");
1622 break;
1623 }
1624 if ((val2 & 0xc0) != 0x80) {
1625 jacl_strict_warning("malformed three-byte character");
1626 break;
1627 }
1628 res = (((val0 & 0xf)<<12) & 0x0000f000);
1629 res |= (((val1 & 0x3f)<<6) & 0x00000fc0);
1630 res |= (((val2 & 0x3f)) & 0x0000003f);
1631 out[outpos++] = res;
1632 continue;
1633 }
1634
1635 if ((val0 & 0xf0) == 0xf0) {
1636 if ((val0 & 0xf8) != 0xf0) {
1637 jacl_strict_warning("malformed four-byte character");
1638 break;
1639 }
1640 if (pos+3 > buflen) {
1641 jacl_strict_warning("incomplete four-byte character");
1642 break;
1643 }
1644 val1 = buf[pos++];
1645 val2 = buf[pos++];
1646 val3 = buf[pos++];
1647 if ((val1 & 0xc0) != 0x80) {
1648 jacl_strict_warning("malformed four-byte character");
1649 break;
1650 }
1651 if ((val2 & 0xc0) != 0x80) {
1652 jacl_strict_warning("malformed four-byte character");
1653 break;
1654 }
1655 if ((val3 & 0xc0) != 0x80) {
1656 jacl_strict_warning("malformed four-byte character");
1657 break;
1658 }
1659 res = (((val0 & 0x7)<<18) & 0x1c0000);
1660 res |= (((val1 & 0x3f)<<12) & 0x03f000);
1661 res |= (((val2 & 0x3f)<<6) & 0x000fc0);
1662 res |= (((val3 & 0x3f)) & 0x00003f);
1663 out[outpos++] = res;
1664 continue;
1665 }
1666
1667 jacl_strict_warning("malformed character");
1668 }
1669
1670 return outpos;
1671 }
1672 #endif