Debian Codegraph @

terps/jacl/interpreter.c

raw symbols SCIP
1 /* interpreter.c --- Main functions for executing JACL code.
2 * (C) 2008 Stuart Allen, distribute and use
3 * according to GNU GPL, see file COPYING for details.
4 */
5
6 #include "jacl.h"
7 #include "language.h"
8 #include "types.h"
9 #include "prototypes.h"
10 #include "csv.h"
11 #include "errno.h"
12 #include "interpreter.h"
13 #include "parser.h"
14 #include "encapsulate.h"
15 #include <string.h>
16 #include <strings.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <strings.h>
20
21 #ifdef _WIN32
22 /* porter's note: i find it hilarious that someone found it easier to include these definitions
23 and the supplying a no-op fcntl function instead of ifdef'ing out the relevant segments below.
24 Windows is technically capable of implementing most of flock via LockFileEx and UnlockFileEx
25 but I'm too lazy to attempt that right now. -- AW 2023*/
26 struct flock {
27 short l_type;
28 short l_whence;
29 off_t l_start;
30 off_t l_len;
31 /* pid_t l_pid; */ /* don't actually need this at all. */
32 };
33
34 #define F_DUPFD 0
35 #define F_GETFD 1
36 #define F_SETFD 2
37 #define F_GETFL 3
38 #define F_SETFL 4
39 #define F_GETLK 5
40 #define F_SETLK 6
41 #define F_SETLKW 7
42
43 #define F_RDLCK 0
44 #define F_WRLCK 1
45 #define F_UNLCK 2
46
47 int fcntl(int __fd, int __cmd, ...) { return 0; }
48 #endif /* _WIN32 */
49
50 #ifndef strcasestr
51 char *strcasestr(const char *s, const char *find)
52 {
53 char c, sc;
54 size_t len;
55
56 if ((c = *find++) != 0) {
57 c = (char)tolower((unsigned char)c);
58 len = strlen(find);
59 do {
60 do {
61 if ((sc = *s++) == 0)
62 return (NULL);
63 } while ((char)tolower((unsigned char)sc) != c);
64 } while (strncasecmp(s, find, len) != 0);
65 s--;
66 }
67 return ((char *)s);
68 }
69 #endif
70
71 #ifdef __NDS__
72 extern void jflush(void);
73 #endif
74
75 #define MAX_TRY 10
76
77 static struct flock read_lck;
78 static int read_fd;
79 static struct flock write_lck;
80 static int write_fd;
81
82 static char *url_encode(const char *str);
83 static char to_hex(char code);
84
85 static const char * const location_attributes[] = {
86 "VISITED ", "DARK ", "ON_WATER ", "UNDER_WATER ", "WITHOUT_AIR ", "OUTDOORS ",
87 "MID_AIR ", "TIGHT_ROPE ", "POLLUTED ", "SOLVED ", "MID_WATER ", "DARKNESS ",
88 "MAPPED ", "KNOWN ",
89 NULL };
90
91 static const char * const object_attributes[] = {
92 "CLOSED ", "LOCKED ", "DEAD ", "IGNITABLE ", "WORN ", "CONCEALING ",
93 "LUMINOUS ", "WEARABLE ", "CLOSABLE ", "LOCKABLE ", "ANIMATE ", "LIQUID ",
94 "CONTAINER ", "SURFACE ", "PLURAL ", "FLAMMABLE ", "BURNING ", "LOCATION ",
95 "ON ", "DAMAGED ", "FEMALE ", "POSSESSIVE ", "OUT_OF_REACH ", "TOUCHED ",
96 "SCORED ", "SITTING ", "NPC ", "DONE ", "GAS ", "NO_TAB ",
97 "NOT_IMPORTANT ", NULL };
98
99 static const char * const object_elements[] = {
100 "parent", "capacity", "mass", "bearing", "velocity", "next", "previous",
101 "child", "index", "status", "timer", "counter", "points", "class", "x", "y",
102 NULL };
103
104 static const char * const location_elements[] = {
105 "north", "south", "east", "west", "northeast", "northwest", "southeast",
106 "southwest", "up", "down", "in", "out", "points", "class", "x", "y",
107 NULL };
108
109 struct csv_parser parser_csv;
110 static char in_name[1024];
111 static char out_name[1024];
112 static FILE *infile, *outfile, *tempfile;
113 static int first_column;
114
115 static int stack = 0;
116 static int proxy_stack = 0;
117
118 static int field_no = 0;
119
120 static struct stack_type backup[STACK_SIZE];
121 static struct proxy_type proxy_backup[STACK_SIZE];
122
123 static struct function_type *resolved_function = NULL;
124 static struct string_type *resolved_string = NULL;
125
126 static struct string_type *new_string = NULL;
127 struct string_type *current_cstring = NULL;
128 static struct string_type *previous_cstring = NULL;
129
130 static struct cinteger_type *new_cinteger = NULL;
131 struct cinteger_type *current_cinteger = NULL;
132 static struct cinteger_type *previous_cinteger = NULL;
133
134 static unsigned int bit_mask;
135 extern int encrypted;
136
137 extern char rpc_function_name[];
138 static char csv_buffer[2048];
139
140 int resolved_attribute;
141
142 /* THE ITERATION VARIABLE USED FOR LOOPS */
143 static int *loop_integer = NULL;
144 static int *select_integer = NULL;
145
146 static int criterion_value = 0;
147 static int criterion_type = 0;
148 static int criterion_negate = FALSE;
149 static int current_level;
150 static int execution_level;
151 static int *ask_integer;
152 static int new_x;
153 static int new_y;
154
155 int interrupted = FALSE;
156 static char string_buffer[2048];
157 static char argument_buffer[1024];
158 #ifdef GLK
159 static glsi32 top_of_loop = 0;
160 static glsi32 top_of_select = 0;
161 static glsi32 top_of_while = 0;
162 static glsi32 top_of_iterate = 0;
163 static glsi32 top_of_update = 0;
164 static glsi32 top_of_do_loop = 0;
165 #else
166 char option_buffer[2024];
167 int style_stack[100];
168 int style_index = 0;
169 long top_of_loop = 0;
170 long top_of_select = 0;
171 long top_of_while = 0;
172 long top_of_iterate = 0;
173 long top_of_update = 0;
174 long top_of_do_loop = 0;
175
176 #endif
177
178 #ifdef __NDS__
179 extern int bold_mode;
180 extern int pre_mode;
181 extern int reverse_mode;
182 extern int input_mode;
183 extern int subheader_mode;
184 extern int note_mode;
185 #endif
186
187 // VALUES FROM LOADER
188 extern int value_resolved;
189
190 char integer_buffer[16];
191 static char called_name[1024];
192 static char scope_criterion[24];
193 static const char *output;
194
195 static int exit_function(int return_code);
196 static int select_next(void);
197 static void new_position(double x1, double y1, double bearing, double velocity);
198 static void pop_proxy(void);
199 static void push_proxy(void);
200 static void pop_stack(void);
201 static int condition(void);
202 static int and_condition(void);
203 static int and_strcondition(void);
204 static int bearing(double x1, double y1, double x2, double y2);
205 static int distance(double x1, double y1, double x2, double y2);
206 static int strcondition(void);
207 static void set_arguments(const char *function_call);
208
209 void
210 terminate(int code)
211 {
212 // FREE ANY EXTRA RAM ALLOCATED BY THE CSV PARSER
213 csv_free(&parser_csv);
214
215 #ifdef GLK
216 int index;
217 event_t event;
218
219 // FLUSH THE GLK WINDOW SO THE ERROR GETS DISPLAYED IMMEDIATELY.
220 glk_select_poll(&event);
221
222 /* CLOSE THE SOUND CHANNELS */
223 for (index = 0; index < 8; index++) {
224 if (sound_channel[index] != NULL) {
225 glk_schannel_destroy(sound_channel[index]);
226 }
227 }
228
229 /* CLOSE THE STREAM */
230 if (game_stream != NULL) {
231 glk_stream_close(game_stream, NULL);
232 }
233
234 glk_exit();
235 #else
236 if (file != NULL) /* CLOSE THE GAME FILE */
237 fclose(file);
238
239 exit(code);
240 #endif
241 }
242
243 static void
244 build_proxy(void)
245 {
246 int index;
247
248 proxy_buffer[0] = 0;
249
250 /* LOOP THROUGH ALL THE PARAMETERS OF THE PROXY COMMAND
251 AND BUILD THE MOVE TO BE ISSUED ON THE PLAYER'S BEHALF */
252 for (index = 1; word[index] != NULL; index++) {
253 strcat(proxy_buffer, text_of_word(index));
254 }
255
256 for (index = 0; index < strlen(proxy_buffer); index++) {
257 if (proxy_buffer[index] == '~') {
258 proxy_buffer[index] = '\"';
259 }
260 }
261
262 //printf("--- proxy buffer = \"%s\"\n", proxy_buffer);
263 }
264
265 static void
266 cb1 (void *s, size_t i, void *not_used) {
267 struct string_type *resolved_cstring;
268
269 //sprintf (temp_buffer, "Trying to set field %d to equal %s^", field_no, (char *) s);
270 //write_text(temp_buffer);
271
272 sprintf (temp_buffer, "field[%d]", field_no);
273
274 if ((resolved_cstring = cstring_resolve(temp_buffer)) != NULL) {
275 //write_text("Resolved ");
276 //write_text(temp_buffer);
277 //write_text("^");
278 strncpy (resolved_cstring->value, s, i);
279 resolved_cstring->value[i] = 0;
280 //sprintf(temp_buffer, "Setting field %d to ~%s~^", field_no, (char *) s);
281 //write_text(temp_buffer);
282 // INCREMENT THE FIELD NUMBER SO THE NEXT ONE GETS STORED IN THE RIGHT CONSTANT
283 field_no++;
284 } else {
285 write_text("Can't resolve ");
286 write_text(temp_buffer);
287 write_text("^");
288 }
289
290 }
291
292 static void
293 cb2 (int c, void *not_used) {
294 // THE END OF THE RECORD HAS BEEN REACHED, EXPORT THE NUMBER OF FIELDS READ
295 struct cinteger_type *resolved_cinteger;
296
297 if ((resolved_cinteger = cinteger_resolve("field_count")) != NULL) {
298 resolved_cinteger->value = field_no;
299 }
300 }
301
302 int
303 execute(const char *funcname)
304 {
305 int index;
306 int counter;
307 int *container;
308
309 int object_1,
310 object_2;
311
312 /* THESE VARIABLE KEEP TRACK OF if AND endif COMMANDS TO DECIDE WHETHER
313 *THE CURRENT LINE OF CODE SHOULD BE EXECUTED OR NOT */
314 int current_level = 0;
315 int execution_level = 0;
316
317 /* THESE ARE USED AS FILE POINTER OFFSETS TO RETURN TO FIXED
318 * POINTS IN THE GAME FILE */
319 #ifdef GLK
320 int result;
321 glsi32 before_command = 0;
322 #else
323 long before_command = 0;
324 #endif
325
326 #ifdef GLK
327 strncpy (called_name, funcname, 1023);
328 #else
329 if (!strcmp(funcname, "+rpc")) {
330 strncpy (called_name, rpc_function_name, 80);
331 } else {
332 strncpy (called_name, funcname, 1023);
333 }
334
335 #endif
336 /* GET THE FUNCTION OBJECT BY THE FUNCTION NAME */
337 resolved_function = function_resolve(called_name);
338
339 if (resolved_function == NULL) {
340 //printf("--- failed to find %s\n", called_name);
341 return (FALSE);
342 }
343
344 #ifdef GLK
345 push_stack(glk_stream_get_position(game_stream));
346 #else
347 push_stack(ftell(file));
348 #endif
349
350 top_of_loop = 0;
351 top_of_select = 0;
352 top_of_while = 0;
353 top_of_iterate = 0;
354 top_of_update = 0;
355 top_of_do_loop = 0;
356
357 executing_function = resolved_function;
358 executing_function->call_count++;
359
360 // CREATE ALL THE PASSED ARGUMENTS AS JACL INTEGER CONSTANTS
361 set_arguments(called_name);
362
363 // SET function_name TO THE CORE NAME STORED IN THE FUNCTION OBJECT
364 // LEAVING called_name TO CONTAIN THE FULL ARGUMENT LIST
365 strncpy (function_name, executing_function->name, 80);
366 strncpy (cstring_resolve("function_name")->value, executing_function->name, 80);
367
368 //sprintf(temp_buffer, "--- starting to execute %s^", function_name);
369 //write_text(temp_buffer);
370
371 // JUMP TO THE POINT IN THE PROCESSED GAME FILE WHERE THIS FUNCTION STARTS
372 #ifdef GLK
373 glk_stream_set_position(game_stream, executing_function->position, seekmode_Start);
374 before_command = executing_function->position;
375 result = glk_get_bin_line_stream(game_stream, text_buffer, (glui32) 1024);
376 #else
377 fseek(file, executing_function->position, SEEK_SET);
378 before_command = executing_function->position;
379 fgets(text_buffer, 1024, file);
380 #endif
381
382 if (encrypted) jacl_decrypt(text_buffer);
383
384 while (text_buffer[0] != '}' && !interrupted) {
385 encapsulate();
386 if (word[0] == NULL);
387 else if (!strcmp(word[0], "endwhile")) {
388 current_level--;
389 if (current_level < execution_level) {
390 // THIS ENDWHILE COMMAND WAS BEING EXECUTED,
391 // NOT JUST COUNTED.
392 if (top_of_while == FALSE) {
393 sprintf(error_buffer, NO_WHILE, executing_function->name);
394 log_error(error_buffer, PLUS_STDOUT);
395 } else {
396 #ifdef GLK
397 glk_stream_set_position(game_stream, top_of_while, seekmode_Start);
398 #else
399 fseek(file, top_of_while, SEEK_SET);
400 #endif
401 execution_level = current_level;
402 }
403 }
404 } else if (!strcmp(word[0], "enditerate")) {
405 current_level--;
406 if (current_level < execution_level) {
407 // THIS ENDITERATE COMMAND WAS BEING EXECUTED,
408 // NOT JUST COUNTED.
409 if (top_of_iterate == FALSE) {
410 sprintf(error_buffer, NO_ITERATE, executing_function->name);
411 log_error(error_buffer, PLUS_STDOUT);
412 } else {
413 #ifdef GLK
414 glk_stream_set_position(game_stream, top_of_iterate, seekmode_Start);
415 #else
416 fseek(file, top_of_iterate, SEEK_SET);
417 #endif
418 execution_level = current_level;
419 }
420 }
421 } else if (!strcmp(word[0], "endupdate")) {
422 current_level--;
423 if (current_level < execution_level) {
424 // THIS ENDUPDATE COMMAND WAS BEING EXECUTED,
425 // NOT JUST COUNTED.
426 if (top_of_update == FALSE) {
427 sprintf(error_buffer, NO_UPDATE, executing_function->name);
428 log_error(error_buffer, PLUS_STDOUT);
429 } else {
430 #ifdef GLK
431 glk_stream_set_position(game_stream, top_of_update, seekmode_Start);
432 #else
433 fseek(file, top_of_update, SEEK_SET);
434 #endif
435 execution_level = current_level;
436 }
437 }
438 } else if (!strcmp(word[0], "print") && current_level != execution_level) {
439 // SKIP THIS BLOCK OF PLAIN TEXT UNTIL IT FINDS A
440 // LINE THAT STARTS WITH A '.'
441 #ifdef GLK
442 glk_get_bin_line_stream(game_stream, text_buffer, (glui32) 1024);
443 #else
444 fgets(text_buffer, 1024, file);
445 #endif
446
447 if (encrypted) jacl_decrypt(text_buffer);
448
449 while (text_buffer[0] != '.') {
450 // GET THE NEXT LINE
451 #ifdef GLK
452 glk_get_bin_line_stream(game_stream, text_buffer, (glui32) 1024);
453 #else
454 fgets(text_buffer, 1024, file);
455 #endif
456
457 if (encrypted) jacl_decrypt(text_buffer);
458
459 }
460 } else if (!strcmp(word[0], "endif")) {
461 current_level--;
462 if (current_level < execution_level) {
463 /* THIS SHOULD NEVER HAPPEN */
464 execution_level = current_level;
465 }
466 } else if (!strcmp(word[0], "endall")) {
467 current_level = 0;
468 execution_level = 0;
469 } else if (!strcmp(word[0], "else")) {
470 if (current_level == execution_level) {
471 execution_level--;
472 } else if (current_level == execution_level + 1) {
473 execution_level++;
474 }
475 } else if (current_level == execution_level) {
476 if (!strcmp(word[0], "look")) {
477 // THIS IS JUST HERE FOR BACKWARDS COMPATIBILITY
478 object[HERE]->attributes &= ~1L;
479 look_around();
480 } else if (!strcmp(word[0], "repeat")) {
481 #ifdef GLK
482 top_of_do_loop = glk_stream_get_position(game_stream);
483 #else
484 top_of_do_loop = ftell(file);
485 #endif
486 } else if (!strcmp(word[0], "until")) {
487 if (word[3] == NULL) {
488 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
489 noproprun();
490 return(exit_function (TRUE));
491 } else {
492 if (top_of_do_loop == FALSE) {
493 sprintf(error_buffer, NO_REPEAT, executing_function->name);
494 log_error(error_buffer, PLUS_STDOUT);
495 } else if (!condition()) {
496 #ifdef GLK
497 glk_stream_set_position(game_stream, top_of_do_loop, seekmode_Start);
498 #else
499 fseek(file, top_of_do_loop, SEEK_SET);
500 #endif
501 }
502 }
503 } else if (!strcmp(word[0], "untilall")) {
504 if (word[3] == NULL) {
505 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
506 noproprun();
507 return(exit_function (TRUE));
508 } else {
509 if (top_of_do_loop == FALSE) {
510 sprintf(error_buffer, NO_REPEAT, executing_function->name);
511 log_error(error_buffer, PLUS_STDOUT);
512 } else if (!and_condition()) {
513 #ifdef GLK
514 glk_stream_set_position(game_stream, top_of_do_loop, seekmode_Start);
515 #else
516 fseek(file, top_of_do_loop, SEEK_SET);
517 #endif
518
519 }
520 }
521 } else if (!strcmp(word[0], "iterate")) {
522 int i;
523
524 // A NEW iterate LOOP MEANS STARTING BACK AT THE FIRST FIELD
525 field_no = 0;
526
527 current_level++;
528 /* THIS LOOP COMES BACK TO THE START OF THE LINE CURRENTLY
529 EXECUTING, NOT THE LINE AFTER */
530
531 top_of_iterate = before_command;
532
533 // infile REMAINS OPEN DURING THE ITERATION, ONLY NEEDS
534 // OPENING THE FIRST TIME
535 if (infile == NULL) {
536 strcpy (temp_buffer, data_directory);
537 strcat (temp_buffer, prefix);
538 strcat (temp_buffer, "-");
539 strcat (temp_buffer, text_of_word(1));
540 strcat (temp_buffer, ".csv");
541
542 infile = fopen(temp_buffer, "rb");
543
544 if (word[2] != NULL && !strcmp(word[2], "skip_header")) {
545 fgets(csv_buffer, 2048, infile);
546 }
547 }
548
549 if (infile == NULL) {
550 sprintf(error_buffer, "Failed to open file %s: %s\n", temp_buffer, strerror(errno));
551 log_error(error_buffer, LOG_ONLY);
552 } else {
553 if (word[1] == NULL) {
554 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
555 noproprun();
556 return(exit_function (TRUE));
557 } else {
558 // IF THERE IS ANOTHER RECORD TO READ FROM THE CSV FILE THEN
559 // SET THE field[] CONSTANTS AND INCREMENT THE execution_level
560 fgets(csv_buffer, 2048, infile);
561 if (!feof(infile)) {
562 i = strlen(csv_buffer);
563 //sprintf (temp_buffer, "Read ~%s~ with %d bytes.^", csv_buffer, i);
564 //write_text(temp_buffer);
565 if (csv_parse(&parser_csv, csv_buffer, i, cb1, cb2, (void *) NULL) != i) {
566 sprintf(error_buffer, "Error parsing file: %s\n", csv_strerror(csv_error(&parser_csv)));
567 log_error(error_buffer, PLUS_STDOUT);
568 fclose(infile);
569 infile = NULL;
570 } else {
571 // A LINE HAS BEEN SUCCESSFULLY READ, EXECUTE THE CONTENTS OF THE LOOP
572 execution_level++;
573 }
574 } else {
575 fclose(infile);
576 infile = NULL;
577 }
578 }
579 }
580 } else if (!strcmp(word[0], "update")) {
581 int i;
582 int try = 0;
583
584 // SET UP THE RECORD LOCKING STRUCTURE, THE ADDRESS OF WHICH
585 // IS PASSED TO THE fcntl() SYSTEM CALL
586 write_lck.l_type = F_WRLCK; // SETTING A WRITE LOCK
587 write_lck.l_whence = 0; // OFFSET l_start FROM BEGINNING OF FILE
588 write_lck.l_start = 0LL;
589 write_lck.l_len = 0LL; // UNTIL THE END OF THE FILE ADDRESS SPACE
590
591 read_lck.l_type = F_RDLCK; // SETTING A READ LOCK
592 read_lck.l_whence = 0; // OFFSET l_start FROM BEGINNING OF FILE
593 read_lck.l_start = 0LL;
594 read_lck.l_len = 0LL; // UNTIL THE END OF THE FILE ADDRESS SPACE
595
596 // A NEW iterate LOOP MEANS STARTING BACK AT THE FIRST FIELD
597 field_no = 0;
598
599 current_level++;
600 // THIS LOOP COMES BACK TO THE START OF THE LINE CURRENTLY
601 // EXECUTING, NOT THE LINE AFTER
602
603 top_of_update = before_command;
604
605 // infile REMAINS OPEN DURING THE ITERATION, ONLY NEEDS
606 // OPENING THE FIRST TIME
607 if (infile == NULL) {
608 strcpy (in_name, data_directory);
609 strcat (in_name, prefix);
610 strcat (in_name, "-");
611 strcat (in_name, text_of_word(1));
612 strcat (in_name, ".csv");
613
614 infile = fopen(in_name, "rb");
615 }
616
617 if (outfile == NULL) {
618 // OPEN A TEMPORARY OUTPUT FILE TO WRITE THE MODIFICATIONS TO
619 strcpy (out_name, data_directory);
620 strcat (out_name, prefix);
621 strcat (out_name, "-");
622 strcat (out_name, text_of_word(1));
623 strcat (out_name, "-");
624 strcat (out_name, user_id);
625 strcat (out_name, ".csv");
626
627 outfile = fopen(out_name, "wb");
628 }
629
630 if (infile == NULL) {
631 sprintf(error_buffer, "Failed to open input CSV file ~%s~: %s\n", in_name, strerror(errno));
632 log_error(error_buffer, LOG_ONLY);
633 if (outfile != NULL) {
634 fclose(outfile);
635 outfile = NULL;
636 }
637 return(exit_function (TRUE));
638 } else {
639 if (outfile == NULL) {
640 sprintf(error_buffer, "Failed to open output CSV file ~%s~: %s\n", out_name, strerror(errno));
641 log_error(error_buffer, LOG_ONLY);
642 if (infile != NULL) {
643 fclose(infile);
644 infile = NULL;
645 }
646 return(exit_function (TRUE));
647 } else {
648 write_fd = fileno(outfile);
649 // ATTEMPT LOCKING OUTPUT FILE MAX_TRY TIMES BEFORE GIVING UP.
650 while (fcntl(write_fd, F_SETLK, &write_lck)<0) {
651 if (errno == EAGAIN || errno == EACCES) {
652 // THERE MIGHT BE OTHER ERROR CASES IN WHICH
653 // USERS MIGHT TRY AGAIN
654 if (++try < MAX_TRY) {
655 jacl_sleep(1000);
656 continue;
657 }
658 sprintf(error_buffer, "File busy unable to get lock on output file.\n");
659 log_error(error_buffer, PLUS_STDOUT);
660 return(exit_function (TRUE));
661 }
662 }
663
664 try = 0;
665
666 read_fd = fileno(infile);
667 // ATTEMPT LOCKING OUTPUT FILE MAX_TRY TIMES BEFORE GIVING UP.
668 while (fcntl(read_fd, F_SETLK, &read_lck)<0) {
669 if (errno == EAGAIN || errno == EACCES) {
670 // THERE MIGHT BE OTHER ERROR CASES IN WHICH
671 // USERS MIGHT TRY AGAIN
672 if (++try < MAX_TRY) {
673 jacl_sleep(1000);
674 continue;
675 }
676 sprintf(error_buffer, "File busy unable to get lock on input file.\n");
677 log_error(error_buffer, PLUS_STDOUT);
678 return(exit_function (TRUE));
679 }
680 }
681
682 if (word[1] == NULL) {
683 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
684 noproprun();
685 return(exit_function (TRUE));
686 } else {
687 // IF THERE IS ANOTHER RECORD TO READ FROM THE CSV FILE THEN
688 // SET THE field[] CONSTANTS AND INCREMENT THE execution_level
689 fgets(csv_buffer, 2048, infile);
690 if (!feof(infile)) {
691 i = strlen(csv_buffer);
692 if (csv_parse(&parser_csv, csv_buffer, i, cb1, cb2, (int *) &field_no) != i) {
693 sprintf(error_buffer, "Error parsing file: %s\n", csv_strerror(csv_error(&parser_csv)));
694 log_error(error_buffer, PLUS_STDOUT);
695 read_lck.l_type = F_UNLCK; // SETTING A READ LOCK
696 fcntl(read_fd, F_SETLK, &read_lck);
697 fclose(infile);
698 infile = NULL;
699 } else {
700 // A LINE HAS BEEN SUCCESSFULLY READ, EXECUTE THE CONTENTS OF THE LOOP
701 execution_level++;
702 }
703 } else {
704 write_lck.l_type = F_UNLCK; // REMOVE THE WRITE LOCK
705 fcntl(write_fd, F_SETLK, &write_lck);
706 fclose(outfile);
707
708 read_lck.l_type = F_UNLCK; // REMOVE THE READ LOCK
709 fcntl(read_fd, F_SETLK, &read_lck);
710 fclose(infile);
711
712 rename (out_name, in_name);
713
714 outfile = NULL;
715 infile = NULL;
716 }
717 }
718 }
719 }
720 } else if (!strcmp(word[0], "while")) {
721 current_level++;
722 /* THIS LOOP COMES BACK TO THE START OF THE LINE CURRENTLY
723 EXECUTING, NOT THE LINE AFTER */
724 top_of_while = before_command;
725 if (word[3] == NULL) {
726 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
727 noproprun();
728 return(exit_function (TRUE));
729 } else if (condition()) {
730 execution_level++;
731 }
732 } else if (!strcmp(word[0], "whileall")) {
733 current_level++;
734 /* THIS LOOP COMES BACK TO THE START OF THE LINE CURRENTLY
735 EXECUTING, NOT THE LINE AFTER */
736 top_of_while = before_command;
737 if (word[3] == NULL) {
738 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
739 noproprun();
740 return(exit_function (TRUE));
741 } else if (and_condition()) {
742 execution_level++;
743 }
744 } else if (!strcmp(word[0], "loop")) {
745 /* THE LOOP COMMAND LOOPS ONCE FOR EACH DEFINED
746 * OBJECT (FOREACH) */
747 #ifdef GLK
748 top_of_loop = glk_stream_get_position(game_stream);
749 #else
750 top_of_loop = ftell(file);
751 #endif
752 if (word[1] == NULL) {
753 // IF NONE IS SUPPLIED DEFAULT TO noun3
754 loop_integer = &noun[2];
755 } else {
756 // STORE THE CONTAINER TO PUT THE CURRENT OBJECT IN
757 loop_integer = container_resolve(word[1]);
758
759 // IF THE SUPPLIED CONTAINER CAN'T BE RESOLVED
760 // DEFAULT TO noun3
761 if (loop_integer == NULL)
762 loop_integer = &noun[2];
763 }
764
765 // SET THE VALUE OF THE LOOP INDEX TO POINT TO THE FIRST OBJECT
766 *loop_integer = 1;
767
768 } else if (!strcmp(word[0], "endloop")) {
769 if (top_of_loop == FALSE) {
770 sprintf(error_buffer, NO_LOOP, executing_function->name);
771 log_error(error_buffer, PLUS_STDOUT);
772 } else {
773 *loop_integer += 1;
774 if (*loop_integer > objects) {
775 top_of_loop = FALSE;
776 *loop_integer = 0;
777 } else {
778 #ifdef GLK
779 glk_stream_set_position(game_stream, top_of_loop, seekmode_Start);
780 #else
781 fseek(file, top_of_loop, SEEK_SET);
782 #endif
783 }
784 }
785 } else if (!strcmp(word[0], "select")) {
786 /* THE SELECT COMMAND LOOPS ONCE FOR EACH DEFINED
787 * OBJECT THAT MATCHES THE SUPPLIED CRITERION */
788 #ifdef GLK
789 top_of_select = glk_stream_get_position(game_stream);
790 #else
791 top_of_select = ftell(file);
792 #endif
793 if (word[1] == NULL) {
794 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
795 noproprun();
796 return(exit_function (TRUE));
797 } else if (word[2] == NULL) {
798 // IF NONE IS SUPPLIED DEFAULT TO noun3
799 select_integer = &noun[2];
800 } else {
801 // STORE THE CONTAINER TO PUT THE CURRENT OBJECT IN
802 select_integer = container_resolve(word[2]);
803
804 // IF THE SUPPLIED CONTAINER CAN'T BE RESOLVED
805 // DEFAULT TO noun3
806 if (select_integer == NULL) {
807 select_integer = &noun[2];
808 }
809 }
810
811 // SET THE VALUE OF THE SELECT INDEX TO ONE BEFORE THE
812 // FIRST OBJECT. THE NEXT FUNCTION AUTOMATICALLY INCREMENTS
813 // THE INDEX BY ONE AT THE START OF THE WHILE LOOP.
814 *select_integer = 0;
815
816 if (word[1][0] == '!') {
817 criterion_negate = TRUE;
818 strcpy(argument_buffer, &word[1][1]);
819 } else {
820 criterion_negate = FALSE;
821 strcpy(argument_buffer, word[1]);
822 }
823
824 // DETERMINE THE CRITERION FOR SELETION
825 if (!strcmp(argument_buffer, "*held")
826 || !strcmp(argument_buffer, "*here")
827 || !strcmp(argument_buffer, "*anywhere")
828 || !strcmp(argument_buffer, "*present")) {
829 criterion_type = CRI_SCOPE;
830 strncpy(scope_criterion, argument_buffer, 20);
831 } else if ((criterion_value = attribute_resolve(argument_buffer))) {
832 criterion_type = CRI_ATTRIBUTE;
833 } else if ((criterion_value = user_attribute_resolve(argument_buffer))) {
834 criterion_type = CRI_USER_ATTRIBUTE;
835 } else {
836 // USE VALUE OF AS A CATCH ALL IF IT IS NOT AN ATTRIBUTE OR SCOPE
837 criterion_value = value_of(argument_buffer, 0);
838
839 if (value_resolved) {
840 criterion_type = CRI_PARENT;
841 } else {
842 // CAN'T RESOLVE CRITERION
843 criterion_type = CRI_NONE;
844 }
845 }
846
847 if (criterion_type != CRI_NONE) {
848 if (select_next() == FALSE) {
849 *select_integer = 0;
850 top_of_select = 0;
851 }
852 } else {
853 *select_integer = 0;
854 }
855
856 if (*select_integer == 0) {
857 // THERE ARE NO MATCHING OBJECTS SO JUMP TO THE endselect
858 #ifdef GLK
859 glk_get_bin_line_stream(game_stream, text_buffer, (glui32) 1024);
860 #else
861 fgets(text_buffer, 1024, file);
862 #endif
863
864 if (encrypted) jacl_decrypt(text_buffer);
865
866 while (text_buffer[0] != '}') {
867 encapsulate();
868 if (word[0] != NULL && !strcmp(word[0], "endselect")) {
869 break;
870 }
871 #ifdef GLK
872 glk_get_bin_line_stream(game_stream, text_buffer, (glui32) 1024);
873 #else
874 fgets(text_buffer, 1024, file);
875 #endif
876 }
877 }
878 } else if (!strcmp(word[0], "endselect")) {
879 if (top_of_select == FALSE) {
880 sprintf(error_buffer, NO_LOOP, executing_function->name);
881 log_error(error_buffer, PLUS_STDOUT);
882 } else {
883 if (select_next()) {
884 #ifdef GLK
885 glk_stream_set_position(game_stream, top_of_select, seekmode_Start);
886 #else
887 fseek(file, top_of_select, SEEK_SET);
888 #endif
889 } else {
890 *select_integer = 0;
891 top_of_select = 0;
892 }
893 }
894 } else if (!strcmp(word[0], "break")) {
895 current_level++;
896 execution_level--;
897 #ifdef GLK
898 } else if (!strcmp(word[0], "cursor")) {
899 if (word[2] == NULL) {
900 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
901 noproprun();
902 return(exit_function (TRUE));
903 } else {
904 if (current_window == statuswin) {
905 glk_window_move_cursor(statuswin, value_of(word[1], TRUE), value_of(word[2], TRUE));
906 } else {
907 log_error(BAD_CURSOR, PLUS_STDOUT);
908 }
909 }
910 } else if (!strcmp(word[0], "stop")) {
911 int channel;
912
913 if (SOUND_SUPPORTED->value) {
914 /* SET THE CHANNEL TO STOP, IF SUPPLIED */
915 if (word[1] == NULL) {
916 channel = 0;
917 } else {
918 channel = value_of(word[1], TRUE);
919
920 /* SANITY CHECK THE CHANNEL SELECTED */
921 if (channel < 0 || channel > 7) {
922 channel = 0;
923 }
924 }
925 glk_schannel_stop(sound_channel[channel]);
926 }
927 } else if (!strcmp(word[0], "volume")) {
928 int channel, volume;
929
930 if (SOUND_SUPPORTED->value) {
931 /* SET THE CHANNEL TO STOP, IF SUPPLIED */
932 if (word[2] == NULL) {
933 channel = 0;
934 } else {
935 channel = value_of(word[2], TRUE);
936
937 /* SANITY CHECK THE CHANNEL SELECTED */
938 if (channel < 0 || channel > 7) {
939 channel = 0;
940 }
941 }
942
943 if (word[1] == NULL) {
944 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
945 noproprun();
946 return(exit_function (TRUE));
947 } else {
948 volume = value_of(word[1], TRUE);
949
950 /* SANITY CHECK THE CHANNEL SELECTED */
951 if (volume < 0) {
952 volume = 0;
953 }
954
955 if (volume > 100) {
956 volume = 100;
957 }
958
959 /* STORE A COPY OF THE CURRENT VOLUME FOR ACCESS
960 * FROM JACL CODE */
961 sprintf(temp_buffer, "volume[%d]", channel);
962 cinteger_resolve(temp_buffer)->value = volume;
963
964 /* NOW SCALE THE 0-100 VOLUME TO THE 0-65536 EXPECTED
965 * BY Glk */
966 volume = volume * 655;
967
968 /* SET THE VOLUME */
969 glk_schannel_set_volume(sound_channel[channel], (glui32) volume);
970 }
971 }
972 } else if (!strcmp(word[0], "timer")) {
973 if (TIMER_SUPPORTED->value && TIMER_ENABLED->value) {
974 if (word[1] == NULL) {
975 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
976 noproprun();
977 return(exit_function (TRUE));
978 } else {
979 index = value_of(word[1], TRUE);
980 /* DON'T ALLOW NEGATIVE VALUES, BUT NO UPPER LIMIT */
981 if (index < 0) index = 0;
982
983 /* SET THE GLK TIMER */
984 glk_request_timer_events((glui32) index);
985
986 /* EXPOSE THE CURRENT VALUE THROUGH A JACL CONSTANT
987 SO THAT GAME CODE CAN READ THE IT */
988 cinteger_resolve("event_timer")->value = index;
989 }
990 }
991 } else if (!strcmp(word[0], "sound")) {
992 int channel;
993 glui32 repeats;
994
995 if (SOUND_SUPPORTED->value && SOUND_ENABLED->value) {
996 /* SET THE CHANNEL TO USE, IF SUPPLIED */
997 if (word[2] == NULL) {
998 channel = 0;
999 } else {
1000 channel = value_of(word[2], TRUE);
1001
1002 /* SANITY CHECK THE CHANNEL SELECTED */
1003 if (channel < 0 || channel > 7) {
1004 channel = 0;
1005 }
1006 }
1007
1008 /* SET THE NUMBER OF REPEATS, IF SUPPLIED */
1009 if (word[3] == NULL) {
1010 repeats = 1;
1011 } else {
1012 repeats = value_of(word[3], TRUE);
1013 }
1014
1015 if (word[1] == NULL) {
1016 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
1017 noproprun();
1018 return(exit_function (TRUE));
1019 } else {
1020 if (glk_schannel_play_ext(sound_channel[channel], (glui32) value_of(word[1], TRUE), repeats, channel + 1) == 0) {
1021 /* THE CHANNEL NUMBER IS PASSED SO THAT THE SOUND
1022 * NOTIFICATION EVENT CAN USE THE INFORMATION
1023 * IT HAS 1 ADDED TO IT SO THAT IT IS A NON-ZERO
1024 * NUMBER AND THE EVENT IS ACTIVATED */
1025 sprintf(error_buffer, "Unable to play sound: %ld", value_of(word[1], FALSE));
1026 log_error(error_buffer, PLUS_STDERR);
1027 }
1028 }
1029 }
1030 } else if (!strcmp(word[0], "image")) {
1031 if (GRAPHICS_SUPPORTED->value && GRAPHICS_ENABLED->value) {
1032 if (word[1] == NULL) {
1033 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
1034 noproprun();
1035 return(exit_function (TRUE));
1036 } else {
1037 if (glk_image_draw(mainwin, (glui32) value_of(word[1], TRUE), imagealign_InlineDown, 0) == 0) {
1038 sprintf(error_buffer, "Unable to draw image: %ld", value_of(word[1], FALSE));
1039 log_error(error_buffer, PLUS_STDERR);
1040 }
1041 }
1042 }
1043 } else if (!strcmp(word[0], "askstring") || !strcmp(word[0], "getstring")) {
1044 if (word[1] == NULL) {
1045 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
1046 noproprun();
1047 return (exit_function(TRUE));
1048 } else {
1049 /* GET A POINTER TO THE STRING BEING MODIFIED */
1050 if ((resolved_string = string_resolve(word[1])) == NULL) {
1051 unkstrrun(word[1]);
1052 return (exit_function(TRUE));
1053 }
1054
1055 // PROMPT THE USER TO INPUT A STRING AND STORE IT IN THE
1056 // RESOLVED VARIABLE
1057 get_string(resolved_string->value);
1058 }
1059
1060 } else if (!strcmp(word[0], "asknumber") || !strcmp(word[0], "getnumber")) {
1061 int low, high;
1062
1063 int insist = FALSE;
1064
1065 /* THE ONLY DIFFERENCE WITH THE getnumber COMMAND IS THAT
1066 * IT INSISTS THE PLAYER GIVES A LEGAL RESPONSE */
1067 if (!strcmp(word[0], "getnumber")) {
1068 insist = TRUE;
1069 }
1070
1071 if (word[3] != NULL) {
1072 ask_integer = container_resolve(word[1]);
1073 if (ask_integer == NULL) {
1074 unkvarrun(word[1]);
1075 return(exit_function (TRUE));
1076 }
1077
1078 low = value_of(word[2], TRUE);
1079 high = value_of(word[3], TRUE);
1080
1081 if (high == -1 || low == -1) {
1082 return(exit_function (TRUE));
1083 }
1084
1085 *ask_integer = get_number(insist, low, high);
1086 } else {
1087 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
1088 noproprun();
1089 return(exit_function (TRUE));
1090 }
1091 } else if (!strcmp(word[0], "getyesorno")) {
1092 if (word[1] != NULL) {
1093 ask_integer = container_resolve(word[1]);
1094 if (ask_integer == NULL) {
1095 unkvarrun(word[1]);
1096 return(exit_function (TRUE));
1097 }
1098
1099 *ask_integer = get_yes_or_no();
1100 } else {
1101 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
1102 noproprun();
1103 return(exit_function (TRUE));
1104 }
1105 } else if (!strcmp(word[0], "clear")) {
1106 if (!walkthru_running) {
1107 glk_window_clear(current_window);
1108 }
1109 } else if (!strcmp(word[0], "terminate")) {
1110 terminate(0);
1111 } else if (!strcmp(word[0], "more")) {
1112 if (word[1] == NULL) {
1113 more ("[MORE]");
1114 } else {
1115 more (word[1]);
1116 }
1117 } else if (!strcmp(word[0], "style")) {
1118 /* THIS COMMAND IS USED TO OUTPUT ANSI CODES OR SET GLK
1119 * STREAM STYLES */
1120 if (word[1] == NULL) {
1121 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
1122 noproprun();
1123 return(exit_function (TRUE));
1124 } else {
1125 if (!strcmp(word[1], "bold")
1126 || !strcmp(word[1], "emphasised")) {
1127 glk_set_style(style_Emphasized);
1128 } else if (!strcmp(word[1], "note")) {
1129 glk_set_style(style_Note);
1130 } else if (!strcmp(word[1], "input")) {
1131 glk_set_style(style_Input);
1132 } else if (!strcmp(word[1], "header")) {
1133 glk_set_style(style_Header);
1134 } else if (!strcmp(word[1], "subheader")) {
1135 glk_set_style(style_Subheader);
1136 } else if (!strcmp(word[1], "reverse")
1137 || !strcmp(word[1], "inverse")) {
1138 if (current_window == mainwin) {
1139 glk_set_style(style_User2);
1140 } else {
1141 glk_set_style(style_User1);
1142 }
1143 } else if (!strcmp(word[1], "pre")
1144 || !strcmp(word[1], "preformatted")) {
1145 glk_set_style(style_Preformatted);
1146 } else if (!strcmp(word[1], "normal")) {
1147 glk_set_style(style_Normal);
1148 }
1149 }
1150 } else if (!strcmp(word[0], "flush")) {
1151 } else if (!strcmp(word[0], "hyperlink")) {
1152 /* OUTPUT LINK TEXT AS PLAIN TEXT UNDER Glk */
1153 if (word[2] == NULL) {
1154 noproprun();
1155 pop_stack();
1156 return (TRUE);
1157 } else {
1158 write_text(text_of_word(1));
1159 }
1160 #else
1161 #ifdef __NDS__
1162 } else if (!strcmp(word[0], "flush")) {
1163 jflush();
1164 } else if (!strcmp(word[0], "cursor")) {
1165 if (word[2] == NULL) {
1166 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
1167 noproprun();
1168 return(exit_function (TRUE));
1169 } else {
1170 printf("\x1b[%d;%dH", (int) value_of(word[1], TRUE), (int) value_of(word[2], TRUE));
1171 }
1172 } else if (!strcmp(word[0], "stop")) {
1173 } else if (!strcmp(word[0], "volume")) {
1174 } else if (!strcmp(word[0], "timer")) {
1175 } else if (!strcmp(word[0], "sound")) {
1176 } else if (!strcmp(word[0], "image")) {
1177 } else if (!strcmp(word[0], "askstring") || !strcmp(word[0], "getstring")) {
1178 if (word[1] == NULL) {
1179 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
1180 noproprun();
1181 return (exit_function(TRUE));
1182 } else {
1183 /* GET A POINTER TO THE STRING BEING MODIFIED */
1184 if ((resolved_string = string_resolve(word[1])) == NULL) {
1185 unkstrrun(word[1]);
1186 return (exit_function(TRUE));
1187 }
1188
1189 // PROMPT THE USER TO INPUT A STRING AND STORE IT IN THE
1190 // RESOLVED VARIABLE
1191 get_string(resolved_string->value);
1192 }
1193
1194 } else if (!strcmp(word[0], "asknumber") || !strcmp(word[0], "getnumber")) {
1195 int low, high;
1196
1197 int insist = FALSE;
1198
1199 /* THE ONLY DIFFERENCE WITH THE getnumber COMMAND IS THAT
1200 * IT INSISTS THE PLAYER GIVES A LEGAL RESPONSE */
1201 if (!strcmp(word[0], "getnumber")) {
1202 insist = TRUE;
1203 }
1204
1205 if (word[3] != NULL) {
1206 ask_integer = container_resolve(word[1]);
1207 if (ask_integer == NULL) {
1208 unkvarrun(word[1]);
1209 return(exit_function (TRUE));
1210 }
1211
1212 low = value_of(word[2], TRUE);
1213 high = value_of(word[3], TRUE);
1214
1215 if (high == -1 || low == -1) {
1216 return(exit_function (TRUE));
1217 }
1218
1219 *ask_integer = get_number(insist, low, high);
1220 } else {
1221 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
1222 noproprun();
1223 return(exit_function (TRUE));
1224 }
1225 } else if (!strcmp(word[0], "getyesorno")) {
1226 if (word[1] != NULL) {
1227 ask_integer = container_resolve(word[1]);
1228 if (ask_integer == NULL) {
1229 unkvarrun(word[1]);
1230 return(exit_function (TRUE));
1231 }
1232
1233 *ask_integer = get_yes_or_no();
1234 } else {
1235 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
1236 noproprun();
1237 return(exit_function (TRUE));
1238 }
1239 } else if (!strcmp(word[0], "clear")) {
1240 clrscrn();
1241 } else if (!strcmp(word[0], "terminate")) {
1242 terminate(0);
1243 } else if (!strcmp(word[0], "more")) {
1244 if (word[1] == NULL) {
1245 more ("[MORE]");
1246 } else {
1247 more (word[1]);
1248 }
1249 } else if (!strcmp(word[0], "style")) {
1250 /* THIS COMMAND IS USED TO OUTPUT ANSI CODES OR SET GLK
1251 * STREAM STYLES */
1252 if (word[1] == NULL) {
1253 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
1254 noproprun();
1255 return(exit_function (TRUE));
1256 } else {
1257 if (!strcmp(word[1], "bold")
1258 || !strcmp(word[1], "emphasised")) {
1259 printf("\x1b[37;1m"); // SET TO BRIGHT WHITE
1260 bold_mode = TRUE;
1261 } else if (!strcmp(word[1], "note")) {
1262 printf("\x1b[34;1m"); // SET TO BRIGHT BLUE
1263 note_mode = TRUE;
1264 } else if (!strcmp(word[1], "input")) {
1265 printf("\x1b[32;0m"); // SET TO DIM GREEN
1266 input_mode = TRUE;
1267 } else if (!strcmp(word[1], "header")) {
1268 printf("\x1b[37;0m"); // SET TO DIM WHITE
1269 } else if (!strcmp(word[1], "subheader")) {
1270 printf("\x1b[33;1m"); // SET TO BRIGHT YELLOW
1271 subheader_mode = TRUE;
1272 } else if (!strcmp(word[1], "reverse")
1273 || !strcmp(word[1], "inverse")) {
1274 printf("\x1b[7m"); // SET TO DIM WHITE
1275 reverse_mode = TRUE;
1276 } else if (!strcmp(word[1], "pre")
1277 || !strcmp(word[1], "preformatted")) {
1278 printf("\x1b[37;0m"); // SET TO DIM WHITE
1279 pre_mode = TRUE;
1280 } else if (!strcmp(word[1], "normal")) {
1281 printf("\x1b[37;0m"); // SET TO DIM WHITE
1282 bold_mode = FALSE;
1283 pre_mode = FALSE;
1284 reverse_mode = FALSE;
1285 input_mode = FALSE;
1286 subheader_mode = FALSE;
1287 note_mode = FALSE;
1288 }
1289 }
1290 } else if (!strcmp(word[0], "hyperlink")) {
1291 /* OUTPUT LINK TEXT AS PLAIN TEXT UNDER Glk */
1292 if (word[2] == NULL) {
1293 noproprun();
1294 pop_stack();
1295 return (TRUE);
1296 } else {
1297 write_text(text_of_word(1));
1298 }
1299 #else
1300 /* HERE STARTS THE CGIJACL-ONLY FUNCTIONS */
1301 } else if (!strcmp(word[0], "option")) {
1302 /* USED TO ADD AN OPTION TO AN HTML LIST */
1303 if (word[1] == NULL) {
1304 noproprun();
1305 pop_stack();
1306 return (TRUE);
1307 } else {
1308 index = value_of(word[1], 0);
1309 if (word[2] != NULL) {
1310 sprintf(option_buffer, "<option value=\"%d\">",
1311 index);
1312 } else {
1313 object_names(index, temp_buffer);
1314 sprintf(option_buffer, "<option value=\"%s\">", temp_buffer);
1315 }
1316
1317 write_text(option_buffer);
1318 list_output(index, TRUE);
1319 write_text(temp_buffer);
1320
1321 }
1322 } else if (!strcmp(word[0], "getenv")) {
1323 struct string_type *resolved_setstring = NULL;
1324
1325 if (word[2] == NULL) {
1326 noproprun();
1327 pop_stack();
1328 return (TRUE);
1329 } else {
1330 // GET A POINTER TO THE STRING BEING MODIFIED
1331 if ((resolved_setstring = string_resolve(word[1])) == NULL) {
1332 unkstrrun(word[1]);
1333 return (exit_function(TRUE));
1334 }
1335
1336 // COPY THE VARIABLE OF THE CGI VARIABLE INTO THE SPECIFIED STRING VARIABLE
1337 if (getenv(text_of_word(2)) != NULL) {
1338 strncpy (resolved_setstring->value, getenv(text_of_word(2)), 1023);
1339 } else {
1340 strncpy (resolved_setstring->value, "", 1023);
1341 }
1342 }
1343 } else if (!strcmp(word[0], "button")) {
1344 /* USED TO CREATE AN HTML BUTTON */
1345 if (word[1] == NULL) {
1346 noproprun();
1347 pop_stack();
1348 return (TRUE);
1349 } if (word[2] != NULL) {
1350 sprintf (option_buffer, "<input class=~button~ type=~image~ src=~%s~ name=~verb~ value=~", text_of_word(2));
1351 strcat (option_buffer, text_of_word(1));
1352 strcat (option_buffer, "~>");
1353 write_text(option_buffer);
1354 } else {
1355 sprintf (option_buffer, "<input class=~button~ type=~submit~ style=~width: 90px; margin: 5px;~ name=~verb~ value=~%s~>", text_of_word(1));
1356 write_text(option_buffer);
1357 }
1358 } else if (!strcmp(word[0], "hidden")) {
1359 sprintf (temp_buffer, "<INPUT TYPE=\"hidden\" NAME=\"user_id\" VALUE=\"%s\">", user_id);
1360 write_text(temp_buffer);
1361 } else if (!strcmp(word[0], "control")) {
1362 /* USED TO CREATE A HYPERLINK THAT IS AN IMAGE */
1363 if (word[2] == NULL) {
1364 noproprun();
1365 pop_stack();
1366 return (TRUE);
1367 } else {
1368 sprintf(option_buffer, "<a href=\"?command=%s&amp;user_id=%s\"><img border=0 SRC=\"", text_of_word(2), user_id);
1369 strcat(option_buffer, text_of_word(1));
1370 strcat(option_buffer, "\"></a>");
1371 write_text(option_buffer);
1372 }
1373 } else if (!strcmp(word[0], "hyperlink") || !strcmp(word[0], "hyperlinkNE")) {
1374 string_buffer[0] = 0;
1375
1376 /* USED TO CREATE A HYPERLINK WITH SESSION INFORMATION INCLUDED */
1377 if (word[2] == NULL) {
1378 noproprun();
1379 pop_stack();
1380 return (TRUE);
1381 } else {
1382 const char * encoded;
1383
1384 if (!strcmp(word[0], "hyperlink")) {
1385 encoded = url_encode(text_of_word(2));
1386 } else {
1387 encoded = text_of_word(2);
1388 }
1389
1390 if (word[3] == NULL) {
1391 if (REMOTE_USER_USED->value == TRUE) {
1392 sprintf (string_buffer, "<a href=\"?command=%s\">", encoded);
1393 } else {
1394 sprintf (string_buffer, "<a href=\"?command=%s&amp;user_id=%s\">", encoded, user_id);
1395 }
1396 strcat (string_buffer, text_of_word(1));
1397 strcat (string_buffer, "</a>");
1398 } else {
1399 sprintf (string_buffer, "<a class=\"%s\" href=\"?command=", text_of_word(3));
1400 strcat (string_buffer, encoded);
1401 if (REMOTE_USER_USED->value == FALSE) {
1402 sprintf (option_buffer, "&amp;user_id=%s\">%s</a>", user_id, text_of_word(1));
1403 strcat (string_buffer, option_buffer);
1404 } else {
1405 sprintf (option_buffer, "\">%s</a>", text_of_word(1));
1406 strcat (string_buffer, option_buffer);
1407 }
1408 }
1409
1410 if (!strcmp(word[0], "hyperlink")) {
1411 free (encoded);
1412 }
1413
1414 write_text(string_buffer);
1415 }
1416 } else if (!strcmp(word[0], "prompt")) {
1417 /* USED TO OUTPUT A HTML INPUT CONTROL THAT CONTAINS SESSION INFORMATION */
1418 if (word[1] != NULL) {
1419 sprintf(temp_buffer, "<input id=\"JACLCommandPrompt\" type=text name=~command~ onKeyPress=~%s~>\n", word[1]);
1420 write_text(temp_buffer);
1421 } else {
1422 sprintf(temp_buffer, "<input id=\"JACLCommandPrompt\" type=text name=~command~>\n");
1423 write_text(temp_buffer);
1424 }
1425 sprintf(temp_buffer, "<input type=hidden name=\"user_id\" value=\"%s\">", user_id);
1426 write_text(temp_buffer);
1427 } else if (!strcmp(word[0], "style")) {
1428 /* THIS COMMAND IS USED TO OUTPUT ANSI CODES OR SET GLK
1429 * STREAM STYLES */
1430 if (word[1] == NULL) {
1431 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
1432 noproprun();
1433 return(exit_function (TRUE));
1434 } else {
1435 if (!strcmp(word[1], "bold")
1436 || !strcmp(word[1], "emphasised")) {
1437 write_text("<b>");
1438 style_stack[style_index++] = BOLD;
1439 } else if (!strcmp(word[1], "note")) {
1440 write_text("<i>");
1441 style_stack[style_index++] = NOTE;
1442 } else if (!strcmp(word[1], "input")) {
1443 write_text("<i>");
1444 style_stack[style_index++] = INPUT;
1445 } else if (!strcmp(word[1], "header")) {
1446 write_text("<h1>");
1447 style_stack[style_index++] = HEADER;
1448 } else if (!strcmp(word[1], "subheader")) {
1449 write_text("<h2>");
1450 style_stack[style_index++] = SUBHEADER;
1451 } else if (!strcmp(word[1], "reverse")
1452 || !strcmp(word[1], "inverse")) {
1453 write_text("<b>");
1454 style_stack[style_index++] = REVERSE;
1455 } else if (!strcmp(word[1], "pre")
1456 || !strcmp(word[1], "preformatted")) {
1457 write_text("<pre>");
1458 style_stack[style_index++] = PRE;
1459 } else if (!strcmp(word[1], "normal")) {
1460 style_index--;
1461 for(; style_index > -1; style_index--) {
1462 switch(style_stack[style_index]) {
1463 case BOLD:
1464 write_text("</b>");
1465 break;
1466 case NOTE:
1467 write_text("</i>");
1468 break;
1469 case INPUT:
1470 write_text("</i>");
1471 break;
1472 case HEADER:
1473 write_text("</h1>");
1474 break;
1475 case SUBHEADER:
1476 write_text("</h2>");
1477 break;
1478 case REVERSE:
1479 write_text("</b>");
1480 break;
1481 case PRE:
1482 write_text("</pre>");
1483 break;
1484 }
1485 }
1486 style_index = 0;
1487 }
1488 }
1489 /* THESE FINAL COMMANDS HAVE NO EFFECT UNDER CGIJACL
1490 AND THERE IS NO HARM IN IGNORING THEM */
1491 } else if (!strcmp(word[0], "flush")) {
1492 } else if (!strcmp(word[0], "image")) {
1493 if (word[1] == NULL) {
1494 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
1495 noproprun();
1496 return (exit_function(TRUE));
1497 } else {
1498 if (word[2] == NULL) {
1499 sprintf(option_buffer, "<img src=~%s~>", text_of_word(1));
1500 } else {
1501 sprintf(option_buffer, "<img class=~%s~ src=~%s~>", text_of_word(2), text_of_word(1));
1502 }
1503
1504 write_text(option_buffer);
1505 }
1506 } else if (!strcmp(word[0], "sound")) {
1507 if (word[2] == NULL) {
1508 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
1509 noproprun();
1510 return (exit_function(TRUE));
1511 } else {
1512 write_text("<audio autoplay=~autoplay~>");
1513 if (word[3] == NULL) {
1514 sprintf(option_buffer, "<source src=~%s~ type=~%s~>", text_of_word(1), text_of_word(2));
1515 write_text(option_buffer);
1516 }
1517 write_text("</audio>");
1518 }
1519 } else if (!strcmp(word[0], "cursor")) {
1520 } else if (!strcmp(word[0], "timer")) {
1521 } else if (!strcmp(word[0], "volume")) {
1522 } else if (!strcmp(word[0], "askstring") || !strcmp(word[0], "getstring")) {
1523 } else if (!strcmp(word[0], "asknumber") || !strcmp(word[0], "getnumber")) {
1524 } else if (!strcmp(word[0], "getyesorno")) {
1525 } else if (!strcmp(word[0], "clear")) {
1526 } else if (!strcmp(word[0], "more")) {
1527 } else if (!strcmp(word[0], "terminate")) {
1528 #endif
1529 #endif
1530 } else if (!strcmp(word[0], "proxy")) {
1531 /* THE PROXY COMMAND ISSUES A MOVE ON THE PLAYER'S BEHALF
1532 * ALL STATE MUST BE SAVED SO THE CURRENT MOVE CAN CONTINUE
1533 * ONCE THE PROXIED MOVE IS COMPLETE */
1534 #ifdef GLK
1535 push_stack(glk_stream_get_position(game_stream));
1536 #else
1537 push_stack(ftell(file));
1538 #endif
1539 push_proxy();
1540
1541 build_proxy();
1542
1543 // TEXT BUFFER IS THE NORMAL ARRAY FOR HOLDING THE PLAYERS
1544 // MOVE FOR PROCESSING
1545 strncpy(text_buffer, proxy_buffer, 1024);
1546
1547 command_encapsulate();
1548
1549 jacl_truncate();
1550
1551 preparse();
1552
1553 pop_proxy();
1554
1555 pop_stack();
1556 } else if (!strcmp(word[0], "override")) {
1557 /* TELLS THE INTERPRETER TO LOOK FOR AN _override FUNCTION
1558 * TO EXECUTE IN PLACE OF ANY CODE THAT FOLLOWS THIS LINE.
1559 * THIS COMMAND IS USED EXCLUSIVELY IN GLOBAL FUNCTIONS
1560 * ASSOCIATED WITH GRAMMAR LINES */
1561 if (execute(override) == TRUE) {
1562 return(exit_function (TRUE));
1563 } else {
1564 if (execute(default_function) == TRUE) {
1565 return(exit_function (TRUE));
1566 }
1567 }
1568 } else if (!strcmp(word[0], "execute") || !strcmp(word[0], "call")) {
1569 /* CALLS ANOTHER JACL FUNCTION */
1570 if (word[1] == NULL) {
1571 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
1572 noproprun();
1573 return(exit_function (TRUE));
1574 } else {
1575 /* RESOLVE ALL THE TEXT AND STORE IT IN A TEMPORARY BUFFER*/
1576 string_buffer[0] = 0;
1577
1578 for (counter = 1; word[counter] != NULL && counter < MAX_WORDS; counter++) {
1579 strcat(string_buffer, arg_text_of_word(counter));
1580 }
1581
1582 if (function_resolve(string_buffer) == NULL && !strcmp(word[0], "execute")) {
1583 char * argstart;
1584
1585 /* REMOVE ANY PARAMETERS FROM FUNCTION NAME
1586 BEFORE DISPLAYING ERROR MESSAGE */
1587 argstart = strchr(string_buffer, '<');
1588 if (argstart != NULL)
1589 *argstart = 0;
1590
1591 sprintf(error_buffer, UNDEFINED_FUNCTION, executing_function->name, string_buffer);
1592 log_error(error_buffer, PLUS_STDOUT);
1593 } else {
1594 execute(string_buffer);
1595 }
1596 }
1597 } else if (!strcmp(word[0], "points")) {
1598 /* INCREASE THE PLAYER'S SCORE AND POTENTIALLY INFORM THEM OF THE INCREASE */
1599 if (word[1] != NULL) {
1600 SCORE->value += value_of(word[1], TRUE);
1601 if (NOTIFY->value) {
1602 #ifdef GLK
1603 glk_set_style(style_Note);
1604 #else
1605 #ifdef __NDS__
1606 printf("\x1b[34;1m"); // SET TO BRIGHT BLUE
1607 note_mode = TRUE;
1608 #else
1609 write_text("<b><i>");
1610 #endif
1611 #endif
1612 write_text(cstring_resolve("SCORE_UP")->value);
1613 sprintf(temp_buffer, "%ld", value_of(word[1], TRUE));
1614 write_text(temp_buffer);
1615 if (value_of(word[1], TRUE) == 1) {
1616 write_text(cstring_resolve("POINT")->value);
1617 } else {
1618 write_text(cstring_resolve("POINTS")->value);
1619 }
1620 #ifdef GLK
1621 glk_set_style(style_Normal);
1622 #else
1623 #ifdef __NDS__
1624 printf("\x1b[37;0m"); // SET TO DIM WHITE
1625 note_mode = FALSE;
1626 #else
1627 write_text("</i></b>");
1628 #endif
1629 #endif
1630 }
1631 } else {
1632 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
1633 noproprun();
1634 return(exit_function (TRUE));
1635 }
1636 } else if (!strcmp(word[0], "output")) {
1637 if (word[1] != NULL) {
1638 strcpy (temp_buffer, data_directory);
1639 strcat (temp_buffer, text_of_word(1));
1640 tempfile = fopen(temp_buffer, "r");
1641
1642 if (tempfile == NULL) {
1643 sprintf(error_buffer, "Failed to open file %s to output: %s\n", temp_buffer, strerror(errno));
1644 log_error(error_buffer, PLUS_STDOUT);
1645 } else {
1646 fgets(temp_buffer, 1024, tempfile);
1647
1648 while (!feof(tempfile)) {
1649 write_text(temp_buffer);
1650 fgets(temp_buffer, 1024, tempfile);
1651 }
1652
1653 fclose(tempfile);
1654 tempfile = NULL;
1655 }
1656 } else {
1657 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
1658 noproprun();
1659 return(exit_function (TRUE));
1660 }
1661 } else if (!strcmp(word[0], "print")) {
1662 int non_space = FALSE;
1663
1664 // DISPLAYS A BLOCK OF PLAIN TEXT UNTIL IT FINDS A
1665 // LINE THAT STARTS WITH A '.'
1666 #ifdef GLK
1667 glk_get_bin_line_stream(game_stream, text_buffer, (glui32) 1024);
1668 #else
1669 fgets(text_buffer, 1024, file);
1670 #endif
1671
1672 if (encrypted) jacl_decrypt(text_buffer);
1673
1674 while (text_buffer[0] != '.') {
1675 index = 0;
1676 non_space = FALSE;
1677
1678 /* REMOVE ANY NEWLINE CHARACTERS */
1679 while (text_buffer[index] != 0) {
1680 if (text_buffer[index] == '|' && non_space == FALSE) {
1681 /* THE BAR CHARACTER IS CHANGED TO A SPACE TO
1682 * ALLOW INDENTING OF NEW PARAGRAPHS ETC */
1683 text_buffer[index] = ' ';
1684 } else if (text_buffer[index] == '\r') {
1685 // GOT TO THE END OF THE LINE, STRIP THE \r AND BREAK
1686 text_buffer[index] = 0;
1687 break;
1688 } else if (text_buffer[index] == '\n') {
1689 // GOT TO THE END OF THE LINE, STRIP THE \n AND BREAK
1690 text_buffer[index] = 0;
1691 break;
1692 } else if (text_buffer[index] != ' ' && text_buffer[index] != '\t') {
1693 // THE FIRST CHARACTER THAT IS NOT A SPACE OR A TAB HAS BEEN REACHED
1694 non_space = TRUE;
1695 }
1696
1697 index++;
1698 }
1699
1700 if (text_buffer[0] != 0) {
1701 // CHECK IF THERE IS THE NEED TO ADD AN
1702 // IMPLICIT SPACE
1703 index = strlen(text_buffer);
1704
1705 if (text_buffer[index - 1] == '\\') {
1706 // A BACKSLASH IS USED TO INDICATE AN IMPLICIT
1707 // SPACE SHOULD NOT BE PRINTED
1708 text_buffer[index - 1] = 0;
1709 } else if (text_buffer[index - 1] != '^') {
1710 // ADD AN IMPLICIT SPACE IF THE PREVIOUS LINE
1711 // DIDN'T END WITH A CARRIAGE RETURN
1712 strcat(text_buffer, " ");
1713 }
1714
1715 // OUTPUT THE LINE READ AS PLAIN TEXT
1716 write_text(text_buffer);
1717 }
1718
1719 // GET THE NEXT LINE
1720 #ifdef GLK
1721 glk_get_bin_line_stream(game_stream, text_buffer, (glui32) 1024);
1722 #else
1723 fgets(text_buffer, 1024, file);
1724 #endif
1725
1726 if (encrypted) jacl_decrypt(text_buffer);
1727 }
1728 } else if (!strcmp(word[0], "mesg")) {
1729 for (counter = 1; word[counter] != NULL && counter < MAX_WORDS; counter++) {
1730 fprintf(stderr, "%s", text_of_word(counter));
1731 }
1732 } else if (!strcmp(word[0], "error")) {
1733 write_text("ERROR: In function ~");
1734 write_text(executing_function->name);
1735 write_text("~, ");
1736 for (counter = 1; word[counter] != NULL && counter < MAX_WORDS; counter++) {
1737 write_text(text_of_word(counter));
1738 }
1739 } else if (!strcmp(word[0], "debug") && DEBUG->value) {
1740 write_text("DEBUG: ");
1741 for (counter = 1; word[counter] != NULL && counter < MAX_WORDS; counter++) {
1742 write_text(text_of_word(counter));
1743 }
1744 } else if (!strcmp(word[0], "write")) {
1745 for (counter = 1; word[counter] != NULL && counter < MAX_WORDS; counter++) {
1746 output = text_of_word(counter);
1747 if (*output != 0) {
1748 // IF THE OUTPUT ISN'T AN EMPTY STRING, DISPLAY IT
1749 write_text(output);
1750 }
1751 }
1752 } else if (!strcmp(word[0], "length")) {
1753 if (word[2] == NULL) {
1754 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
1755 noproprun();
1756 return(exit_function (TRUE));
1757 } else {
1758 if ((container = container_resolve(word[1])) == NULL) {
1759 unkvarrun(word[1]);
1760 return(exit_function (TRUE));
1761 }
1762
1763 *container = strlen(text_of(word[2]));
1764 }
1765 } else if (!strcmp(word[0], "savegame")) {
1766 if (word[1] == NULL) {
1767 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
1768 noproprun();
1769 return (exit_function(TRUE));
1770 } else {
1771 if ((container = container_resolve(word[1])) == NULL) {
1772 unkvarrun(word[1]);
1773 return (exit_function(TRUE));
1774 } else {
1775 if (word[2] == NULL) {
1776 *container = save_interaction(NULL);
1777 } else {
1778 *container = save_interaction(arg_text_of_word(2));
1779 }
1780 }
1781 }
1782 } else if (!strcmp(word[0], "restoregame")) {
1783 if (word[1] == NULL) {
1784 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
1785 noproprun();
1786 return (exit_function(TRUE));
1787 } else {
1788 if ((container = container_resolve(word[1])) == NULL) {
1789 unkvarrun(word[1]);
1790 return (exit_function(TRUE));
1791 } else {
1792 if (word[2] == NULL) {
1793 *container = restore_interaction(NULL);
1794 } else {
1795 *container = restore_interaction(arg_text_of_word(2));
1796 }
1797 }
1798 }
1799 } else if (!strcmp(word[0], "restartgame")) {
1800 restart_game();
1801 execute ("+intro");
1802 eachturn();
1803 #ifdef GLK
1804 } else if (!strcmp(word[0], "undomove")) {
1805 undoing();
1806 } else if (!strcmp(word[0], "updatestatus")) {
1807 status_line();
1808 #else
1809 } else if (!strcmp(word[0], "undomove")) {
1810 } else if (!strcmp(word[0], "updatestatus")) {
1811 #endif
1812 } else if (!strcmp(word[0], "split")) {
1813
1814 // 0 1 2 3 4
1815 // split counter source delimiter destination
1816
1817 int *split_container;
1818 char split_buffer[1024] = "";
1819 char container_buffer[1024] = "";
1820 char delimiter[1024] = "";
1821 char *match = NULL;
1822 struct string_type *resolved_splitstring = NULL;
1823
1824 strcpy (split_buffer, text_of_word(2));
1825 strcpy (delimiter, text_of_word(3));
1826
1827 char *source = split_buffer;
1828
1829 if (word[4] == NULL) {
1830 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
1831 noproprun();
1832 return (exit_function(TRUE));
1833 } else {
1834 split_container = container_resolve(var_text_of_word(1));
1835
1836 if (split_container == NULL) {
1837 unkvarrun(var_text_of_word(1));
1838 return (exit_function(TRUE));
1839 } else {
1840 *split_container = 0;
1841 match = source; // THERE IS ALWAYS ONE MATCH, EVEN IF
1842 // NO DELIMETERS ARE FOUND
1843
1844 while ((match = strstr(source, delimiter))) {
1845 *match = 0;
1846 strcpy(container_buffer, var_text_of_word(4));
1847 strcat(container_buffer, "[");
1848 sprintf(integer_buffer, "%d", *split_container);
1849 strcat(container_buffer, integer_buffer);
1850 strcat(container_buffer, "]");
1851
1852 if ((resolved_splitstring = string_resolve(container_buffer)) == NULL) {
1853 unkstrrun(var_text_of_word(4));
1854 return (exit_function(TRUE));
1855 } else {
1856 strcpy(resolved_splitstring->value, source);
1857 source = match + strlen(delimiter);
1858 (*split_container)++;
1859 }
1860 }
1861 strcpy(container_buffer, var_text_of_word(4));
1862 strcat(container_buffer, "[");
1863 sprintf(integer_buffer, "%d", *split_container);
1864 strcat(container_buffer, integer_buffer);
1865 strcat(container_buffer, "]");
1866
1867 if ((resolved_splitstring = string_resolve(container_buffer)) == NULL) {
1868 unkstrrun(word[1]);
1869 return (exit_function(TRUE));
1870 } else {
1871 strcpy(resolved_splitstring->value, source);
1872 (*split_container)++;
1873 }
1874 }
1875 }
1876 } else if (!strcmp(word[0], "setstring") ||
1877 !strcmp(word[0], "addstring")) {
1878 char setstring_buffer[2048] = "";
1879 struct string_type *resolved_setstring = NULL;
1880
1881 if (word[2] == NULL) {
1882 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
1883 noproprun();
1884 return (exit_function(TRUE));
1885 } else {
1886 /* GET A POINTER TO THE STRING BEING MODIFIED */
1887 if ((resolved_setstring = string_resolve(var_text_of_word(1))) == NULL) {
1888 unkstrrun(word[1]);
1889 return (exit_function(TRUE));
1890 }
1891
1892 /* RESOLVE ALL THE TEXT AND STORE IT IN A TEMPORARY BUFFER*/
1893 for (counter = 2; word[counter] != NULL && counter < MAX_WORDS; counter++) {
1894 strcat(setstring_buffer, text_of_word(counter));
1895 }
1896
1897 /* setstring_buffer IS NOW FILLED, COPY THE UP TO 1023 BYTES OF
1898 * IT INTO THE STRING */
1899 if (!strcmp(word[0], "setstring")) {
1900 strncpy (resolved_setstring->value, setstring_buffer, 1023);
1901 } else {
1902 /* CALCULATE HOW MUCH SPACE IS LEFT IN THE STRING */
1903 counter = 1023 - strlen(resolved_setstring->value);
1904 /* THIS IS A addstring COMMAND, SO USE STRNCAT INSTEAD */
1905 strncat (resolved_setstring->value, setstring_buffer, counter);
1906 }
1907 }
1908 } else if (!strcmp(word[0], "padstring")) {
1909 char setstring_buffer[2048] = "";
1910 struct string_type *resolved_setstring = NULL;
1911 string_buffer[0] = 0;
1912
1913 if (word[3] == NULL) {
1914 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
1915 noproprun();
1916 return (exit_function(TRUE));
1917 } else {
1918 /* GET A POINTER TO THE STRING BEING MODIFIED */
1919 if ((resolved_setstring = string_resolve(word[1])) == NULL) {
1920 unkstrrun(word[1]);
1921 return (exit_function(TRUE));
1922 }
1923
1924 index = value_of(word[3], TRUE);
1925
1926 for (counter = 0; counter < index; counter++) {
1927 strcat(setstring_buffer, text_of_word(2));
1928 }
1929
1930 /* setstring_buffer IS NOW FILLED, COPY THE UP TO 1023 BYTES OF
1931 * IT INTO THE STRING */
1932 strncpy (resolved_setstring->value, setstring_buffer, 1023);
1933 }
1934 } else if (!strcmp(word[0], "return")) {
1935 /* RETURN FROM THIS FUNCTION, POSSIBLY RETURNING AN INTEGER VALUE */
1936 if (word[1] == NULL) {
1937 return (exit_function(TRUE));
1938 } else {
1939 index = value_of(word[1], TRUE);
1940 return (exit_function(index));
1941 }
1942 } else if (!strcmp(word[0], "position")) {
1943 /* MOVE AN OBJECT TO ITS NEW X,Y COORDINATES BASED ON ITS CURRENT VALUES
1944 * FOR x, y, bearing, velocity */
1945 if (word[1] == NULL) {
1946 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
1947 noproprun();
1948 return (exit_function(TRUE));
1949 } else {
1950 object_1 = value_of(word[1], TRUE);
1951
1952 if (object_1 < 1 || object_1 > objects) {
1953 badptrrun(word[1], object_1);
1954 return (exit_function(TRUE));
1955 } else {
1956 new_position((double) object[object_1]->X,
1957 (double) object[object_1]->Y,
1958 (double) object[object_1]->BEARING,
1959 (double) object[object_1]->VELOCITY);
1960
1961 object[object_1]->X = new_x;
1962 object[object_1]->Y = new_y;
1963 }
1964 }
1965 } else if (!strcmp(word[0], "bearing")) {
1966 /* CALCULATE THE BEARING BETWEEN TWO OBJECTS */
1967 if (word[3] == NULL) {
1968 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
1969 noproprun();
1970 return (exit_function(TRUE));
1971 } else {
1972 if ((container = container_resolve(word[1])) == NULL) {
1973 unkvarrun(word[1]);
1974 return (exit_function(TRUE));
1975 }
1976
1977 object_1 = value_of(word[2], TRUE);
1978
1979 if (object_1 < 1 || object_1 > objects) {
1980 badptrrun(word[2], object_1);
1981 return (exit_function(TRUE));
1982 } else {
1983 object_2 = value_of(word[3], TRUE);
1984
1985 if (object_2 < 1 || object_2 > objects) {
1986 badptrrun(word[3], object_2);
1987 return (exit_function(TRUE));
1988 } else {
1989 if (container != NULL
1990 && object_1 != FALSE
1991 && object_2 != FALSE) {
1992 *container = bearing((double) object[object_1]->X,
1993 (double) object[object_1]->Y,
1994 (double) object[object_2]->X,
1995 (double) object[object_2]->Y);
1996 }
1997 }
1998 }
1999 }
2000 } else if (!strcmp(word[0], "distance")) {
2001 /* CALCULATE THE DISTANCE BETWEEN TWO OBJECTS */
2002 if (word[3] == NULL) {
2003 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
2004 noproprun();
2005 return (exit_function(TRUE));
2006 } else {
2007 container = container_resolve(word[1]);
2008
2009 object_1 = value_of(word[2], TRUE);
2010
2011 if (object_1 < 1 || object_1 > objects) {
2012 badptrrun(word[2], object_1);
2013 return (exit_function(TRUE));
2014 } else {
2015 object_2 = value_of(word[3], TRUE);
2016
2017 if (object_2 < 1 || object_2 > objects) {
2018 badptrrun(word[3], object_2);
2019 return (exit_function(TRUE));
2020 } else {
2021 if (container != NULL
2022 && object_1 != FALSE
2023 && object_2 != FALSE) {
2024 *container = distance((double)
2025 object[object_1]->X,
2026 (double)
2027 object[object_1]->Y,
2028 (double)
2029 object[object_2]->X,
2030 (double)
2031 object[object_2]->Y);
2032 }
2033 }
2034 }
2035 }
2036 } else if (!strcmp(word[0], "travel")) {
2037 // THIS IS COMMAND IS HERE FOR BACKWARDS COMPATIBILITY
2038 // ALL TRAVEL RELATED CODE IS NOW IN VERBS.LIBRARY
2039 if (word[1] == NULL) {
2040 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
2041 noproprun();
2042 return (exit_function(TRUE));
2043 } else {
2044 index = value_of(word[1], TRUE);
2045
2046 // NOT A VALID DIRECTION
2047 if (index < 0 || index > 11) index = -1;
2048
2049 if (index != -1) {
2050 integer_resolve("destination")->value = object[HERE]->integer[index];
2051 if (execute("+movement")) break;
2052 if (execute("here.movement")) break;
2053
2054 if (integer_resolve("destination")->value == 0) {
2055 write_text("You can't go that way.^");\
2056 TIME->value = FALSE;
2057 } else {
2058 object[player]->PARENT = integer_resolve("destination")->value;
2059 object[HERE]->attributes &= ~1L;
2060 look_around();
2061 TIME->value = FALSE;
2062 }
2063 } else {
2064 unkdirrun(1);
2065 return (exit_function(TRUE));
2066 }
2067 }
2068 } else if (!strcmp(word[0], "dir_to") ||
2069 !strcmp(word[0], "npc_to")) {
2070 /* CALCULATE THE FIRST DIRECTION TO TRAVEL IN GET TO
2071 * A SPECIFIED LOCATION */
2072 if (word[3] == NULL) {
2073 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
2074 noproprun();
2075 return (exit_function(TRUE));
2076 } else {
2077 container = container_resolve(word[1]);
2078
2079 object_1 = value_of(word[2], TRUE);
2080
2081 if (object_1 < 1 || object_1 > objects) {
2082 badptrrun(word[2], object_1);
2083 return (exit_function(TRUE));
2084 } else {
2085 object_2 = value_of(word[3], TRUE);
2086
2087 if (object_2 < 1 || object_2 > objects) {
2088 badptrrun(word[3], object_2);
2089 return (exit_function(TRUE));
2090 } else {
2091 if (container != NULL
2092 && object_1 != FALSE
2093 && object_2 != FALSE) {
2094 if (!strcmp(word[0], "dir_to")) {
2095 *container = find_route(object_1, object_2, TRUE);
2096 } else {
2097 *container = find_route(object_1, object_2, FALSE);
2098 }
2099 }
2100 }
2101 }
2102 }
2103 } else if (!strcmp(word[0], "set")) {
2104 /* SET THE VALUE OF AN ELEMENT TO A SUPPLIED INTEGER */
2105 if (word[3] == NULL) {
2106 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
2107 noproprun();
2108 return (exit_function(TRUE));
2109 } else {
2110 container = container_resolve(var_text_of_word(1));
2111
2112 if (container == NULL) {
2113 unkvarrun(word[1]);
2114 return (exit_function(TRUE));
2115 } else {
2116 int mark = 2; // SET mark TO POINT TO THE FIRST OPERATOR
2117 while (word[mark + 1] != NULL) {
2118 counter = value_of(word[mark + 1], TRUE);
2119
2120 if (word[mark][0] == '+')
2121 *container += counter;
2122 else if (word[mark][0] == '-')
2123 *container -= counter;
2124 else if (word[mark][0] == '*')
2125 *container = *container * counter;
2126 else if (word[mark][0] == '%')
2127 *container = *container % counter;
2128 else if (word[mark][0] == '/') {
2129 if (counter == 0) {
2130 sprintf(error_buffer, DIVIDE_BY_ZERO,
2131 executing_function->name);
2132 log_error(error_buffer, PLUS_STDOUT);
2133 } else
2134 *container = *container / counter;
2135 } else if (!strcmp(word[mark], "locationof")) {
2136 *container = grand_of(counter, FALSE);
2137 } else if (!strcmp(word[mark], "grandof")) {
2138 *container = grand_of(counter, TRUE);
2139 } else if (word[mark][0] == '=') {
2140 *container = counter;
2141 } else {
2142 sprintf(error_buffer, ILLEGAL_OPERATOR,
2143 executing_function->name,
2144 word[2]);
2145 log_error(error_buffer, PLUS_STDOUT);
2146 }
2147
2148 mark += 2;
2149 }
2150 }
2151 }
2152 } else if (!strcmp(word[0], "ensure")) {
2153 /* USED TO GIVE OR TAKE AN ATTRIBUTE TO OR FROM AND OBJECT */
2154 if (word[3] == NULL) {
2155 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
2156 noproprun();
2157 return (exit_function(TRUE));
2158 } else {
2159 if ((bit_mask = attribute_resolve(arg_text_of(word[3])))) {
2160 index = value_of(word[1], TRUE);
2161 if (index < 1 || index > objects) {
2162 badptrrun(word[1], index);
2163 return (exit_function(TRUE));
2164 } else {
2165 if (!strcmp(word[2], "has")) {
2166 object[index]->attributes =
2167 object[index]->attributes | bit_mask;
2168 } else if (!strcmp(word[2], "hasnt")) {
2169 bit_mask = ~bit_mask;
2170 object[index]->attributes =
2171 object[index]->attributes & bit_mask;
2172 }
2173 }
2174 } else if ((bit_mask = user_attribute_resolve(arg_text_of(word[3])))) {
2175 index = value_of(word[1], TRUE);
2176 if (index < 1 || index > objects) {
2177 badptrrun(word[1], index);
2178 return (exit_function(TRUE));
2179 } else {
2180 if (!strcmp(word[2], "has")) {
2181 object[index]->user_attributes =
2182 object[index]->user_attributes | bit_mask;
2183 } else if (!strcmp(word[2], "hasnt")) {
2184 bit_mask = ~bit_mask;
2185 object[index]->user_attributes =
2186 object[index]->user_attributes & bit_mask;
2187 }
2188 }
2189 } else {
2190 unkattrun(3);
2191 return (exit_function(TRUE));
2192 }
2193 }
2194 } else if (!strcmp(word[0], "append")
2195 || !strcmp(word[0], "append_nt")
2196 || !strcmp(word[0], "append_fc")
2197 || !strcmp(word[0], "append_lc")) {
2198
2199 if (!strcmp(word[0], "append") || !strcmp(word[0], "append_fc")) {
2200 // INITIATE THE WRITING OF A NEW LINE
2201 first_column = TRUE;
2202
2203 if (outfile != NULL) {
2204 fclose(outfile);
2205 outfile = NULL;
2206 }
2207 }
2208
2209 if (word[2] == NULL) {
2210 // NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND
2211 noproprun();
2212 return (exit_function(TRUE));
2213 } else {
2214 if (outfile == NULL) {
2215 strcpy (temp_buffer, data_directory);
2216 strcat (temp_buffer, prefix);
2217 strcat (temp_buffer, "-");
2218 strcat (temp_buffer, text_of_word(1));
2219 strcat (temp_buffer, ".csv");
2220
2221 outfile = fopen(temp_buffer, "ab");
2222 }
2223
2224 if (outfile == NULL) {
2225 sprintf(error_buffer, "Failed to open file %s: %s\n", temp_buffer, strerror(errno));
2226 log_error(error_buffer, PLUS_STDOUT);
2227 } else {
2228 for (counter = 2; word[counter] != NULL && counter < MAX_WORDS; counter++) {
2229 output = text_of_word(counter);
2230 if (*output != 0) {
2231 if (first_column == FALSE) {
2232 fputc(',', outfile);
2233 }
2234 csv_fwrite(outfile, output, (size_t) strlen(output));
2235 first_column = FALSE;
2236 }
2237 }
2238
2239 if (!strcmp(word[0], "append") || !strcmp(word[0], "append_lc")) {
2240 // TERMINATE THE LINE
2241 fputc('\n', outfile);
2242
2243 // FLUSH AND CLOSE THE FILE
2244 fflush(outfile);
2245 fclose(outfile);
2246 outfile = NULL;
2247 }
2248
2249 }
2250
2251 }
2252 } else if (!strcmp(word[0], "insert")) {
2253 int first = TRUE;
2254
2255 if (word[1] == NULL) {
2256 // NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND
2257 noproprun();
2258 return (exit_function(TRUE));
2259 } else {
2260 if (outfile == NULL) {
2261 log_error("Insert statement not inside an 'update' loop.", PLUS_STDOUT);
2262 } else {
2263 for (counter = 1; word[counter] != NULL && counter < MAX_WORDS; counter++) {
2264 output = text_of_word(counter);
2265 if (*output != 0) {
2266 if (first == FALSE) {
2267 fputc(',',(FILE *)outfile);
2268 }
2269 csv_fwrite((FILE *)outfile, output, (size_t) strlen(output));
2270 first = FALSE;
2271 }
2272 }
2273
2274 // TERMINATE THE LINE
2275 fputc('\n', (FILE *)outfile);
2276 }
2277 }
2278 } else if (!strcmp(word[0], "endgame")) {
2279 terminate(0);
2280 } else if (!strcmp(word[0], "inspect")) {
2281 if (word[1] == NULL) {
2282 // NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND
2283 noproprun();
2284 return (exit_function(TRUE));
2285 } else {
2286 inspect(value_of(word[1], TRUE));
2287 }
2288 } else if (!strcmp(word[0], "move")) {
2289 /* THIS COMMAND IS USED TO MOVE AN OBJECT TO HAVE ANOTHER PARENT
2290 * INCLUDING MODIFYING ALL QUANTITY VALUES BASED ON THE OBJECTS MASS */
2291 if (word[3] == NULL) {
2292 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
2293 noproprun();
2294 return (exit_function(TRUE));
2295 }
2296
2297 index = value_of(word[1], TRUE);
2298 if (index < 1 || index > objects) {
2299 badptrrun(word[1], index);
2300 return (exit_function(TRUE));
2301 } else {
2302 object_2 = object[index]->PARENT;
2303 if (object_2 && !(object[object_2]->attributes & LOCATION)) {
2304 object[object_2]->QUANTITY += object[index]->MASS;
2305 }
2306 object_1 = value_of(word[3], TRUE);
2307 if (object_1 < 1 || object_1 > objects) {
2308 badptrrun(word[1], object_1);
2309 return (exit_function(TRUE));
2310 } else {
2311 object[index]->PARENT = object_1;
2312 if (!(object[object_1]->attributes & LOCATION))
2313 object[object_1]->QUANTITY -= object[index]->MASS;
2314 }
2315 }
2316 } else if (!strcmp(word[0], "ifstringall")) {
2317 /* CHECK IF A STRING EQUALS OR CONTAINS ANOTHER STRING */
2318 current_level++;
2319 if (word[3] == NULL) {
2320 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
2321 noproprun();
2322 return (exit_function(TRUE));
2323 } else if (and_strcondition()) {
2324 execution_level++;
2325 }
2326 } else if (!strcmp(word[0], "ifstring")) {
2327 /* CHECK IF A STRING EQUALS OR CONTAINS ANOTHER STRING */
2328 current_level++;
2329 if (word[3] == NULL) {
2330 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
2331 noproprun();
2332 return (exit_function(TRUE));
2333 } else if (strcondition()) {
2334 execution_level++;
2335 }
2336 } else if (!strcmp(word[0], "ifexecute")) {
2337 current_level++;
2338 if (word[1] == NULL) {
2339 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
2340 noproprun();
2341 return (exit_function(TRUE));
2342 } else {
2343 /* RESOLVE ALL THE TEXT AND STORE IT IN A TEMPORARY BUFFER*/
2344 string_buffer[0] = 0;
2345
2346 for (counter = 1; word[counter] != NULL && counter < MAX_WORDS; counter++) {
2347 strcat(string_buffer, arg_text_of_word(counter));
2348 }
2349
2350 if (execute(string_buffer)) {
2351 execution_level++;
2352 }
2353 }
2354 } else if (!strcmp(word[0], "if")) {
2355 current_level++;
2356 if (word[3] == NULL) {
2357 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
2358 noproprun();
2359 return (exit_function(TRUE));
2360 } else if (condition()) {
2361 execution_level++;
2362 }
2363 } else if (!strcmp(word[0], "ifall")) {
2364 current_level++;
2365 if (word[3] == NULL) {
2366 /* NOT ENOUGH PARAMETERS SUPPLIED FOR THIS COMMAND */
2367 noproprun();
2368 return (exit_function(TRUE));
2369 } else if (and_condition()) {
2370 execution_level++;
2371 }
2372 } else {
2373 sprintf(error_buffer, UNKNOWN_COMMAND,
2374 executing_function->name, word[0]);
2375 log_error(error_buffer, PLUS_STDOUT);
2376 }
2377 } else if (!strcmp(word[wp], "if")
2378 || !strcmp(word[wp], "ifall")
2379 || !strcmp(word[wp], "ifstring")
2380 || !strcmp(word[wp], "ifstringall")
2381 || !strcmp(word[wp], "ifexecute")
2382 || !strcmp(word[wp], "iterate")
2383 || !strcmp(word[wp], "update")
2384 || !strcmp(word[wp], "while")
2385 || !strcmp(word[wp], "whileall")) {
2386 current_level++;
2387 }
2388
2389 #ifdef GLK
2390 before_command = glk_stream_get_position(game_stream);
2391 glk_get_bin_line_stream(game_stream, text_buffer, (glui32) 1024);
2392 #else
2393 before_command = ftell(file);
2394 fgets(text_buffer, 1024, file);
2395 #endif
2396 if (encrypted) jacl_decrypt(text_buffer);
2397 };
2398
2399 return (exit_function(TRUE));
2400 }
2401
2402 int
2403 exit_function(int return_code)
2404 {
2405 if (infile != NULL) {
2406 read_lck.l_type = F_UNLCK; // SETTING A READ LOCK
2407 fcntl(read_fd, F_SETLK, &read_lck);
2408 fclose(infile);
2409 infile = NULL;
2410 }
2411
2412 if (outfile != NULL) {
2413 write_lck.l_type = F_UNLCK; // SETTING A WRITE LOCK
2414 fcntl(write_fd, F_SETLK, &write_lck);
2415 fclose(outfile);
2416 outfile = NULL;
2417 }
2418
2419 /* POP THE STACK REGARDLESS OF THE RETURN CODE */
2420 pop_stack();
2421
2422 return (return_code);
2423 }
2424
2425 char *
2426 object_names(int object_index, char *names_buffer)
2427 {
2428 /* THIS FUNCTION CREATES A LIST OF ALL AN OBJECT'S NAMES.
2429 THE escape ARGUMENT INDICATES WHETHER A + SIGN SHOULD BE
2430 USED IN PLACE OF A SPACE BETWEEN EACH OF THE NAMES */
2431 struct name_type *current_name = object[object_index]->first_name;
2432 names_buffer[0] = 0;
2433
2434 while (current_name != NULL) {
2435 strcat(names_buffer, " ");
2436 strcat(names_buffer, current_name->name);
2437 current_name = current_name->next_name;
2438 }
2439
2440 return names_buffer;
2441 }
2442
2443 int
2444 distance(double x1, double y1, double x2, double y2)
2445 {
2446 /* THIS FUNCTION CALCULATES THE DISTANCE BETWEEN TWO POINTS IN A
2447 TWO-DIMENSIONAL PLANE */
2448 double delta_x,
2449 delta_y;
2450 double distance,
2451 total;
2452
2453 /*
2454 * Object two in which quadrant compared to object one? 0 x = opp, y =
2455 * ajd + 0 degrees 1 x = adj, y = opp + 90 degrees 2 x = opp, y = ajd
2456 * + 180 degrees 3 x = adj, y = opp + 270 degrees
2457 */
2458
2459 /*
2460 * DETERMINE WHICH QUADRANT OBJECT TWO IS IN
2461 */
2462
2463 if (x2 > x1) {
2464 /*
2465 * OBJECT TWO IS IN 1 OR 2
2466 */
2467 delta_x = x2 - x1;
2468 if (y2 > y1) {
2469 delta_y = y2 - y1;
2470 } else {
2471 delta_y = y1 - y2;
2472 }
2473 } else {
2474 /*
2475 * OBJECT TWO IS IN 3 OR 4
2476 */
2477 delta_x = x1 - x2;
2478 if (y2 > y1) {
2479 delta_y = y2 - y1;
2480 } else {
2481 delta_y = y1 - y2;
2482 }
2483 }
2484
2485 delta_y = delta_y * delta_y;
2486 delta_x = delta_x * delta_x;
2487
2488 total = delta_y + delta_x;
2489
2490 distance = sqrt(total);
2491
2492 return ((int) distance);
2493 }
2494
2495 void
2496 new_position(double x1, double y1, double bearing, double velocity)
2497 {
2498 double delta_x,
2499 delta_y;
2500 double radians;
2501
2502 /*
2503 * Object two in which quadrant compared to object one? 0 x = opp, y =
2504 * ajd + 0 degrees 1 x = adj, y = opp + 90 degrees 2 x = opp, y = ajd
2505 * + 180 degrees 3 x = adj, y = opp + 270 degrees
2506 */
2507
2508 /*
2509 * sin finds opp, cos finds adj
2510 */
2511
2512 if (bearing < 91) {
2513 radians = bearing * 2.0 * M_PI / 360.;
2514 delta_x = velocity * sin(radians);
2515 delta_y = velocity * cos(radians);
2516 new_x = x1 + delta_x;
2517 new_y = y1 + delta_y;
2518 } else if (bearing < 181) {
2519 bearing -= 90;
2520 radians = bearing * 2.0 * M_PI / 360.;
2521 delta_y = velocity * sin(radians);
2522 delta_x = velocity * cos(radians);
2523 new_x = x1 + delta_x;
2524 new_y = y1 - delta_y;
2525 } else if (bearing < 271) {
2526 bearing -= 180;
2527 radians = bearing * 2.0 * M_PI / 360.;
2528 delta_x = velocity * sin(radians);
2529 delta_y = velocity * cos(radians);
2530 new_x = x1 - delta_x;
2531 new_y = y1 - delta_y;
2532 } else {
2533 bearing -= 270;
2534 radians = bearing * 2.0 * M_PI / 360.;
2535 delta_y = velocity * sin(radians);
2536 delta_x = velocity * cos(radians);
2537 new_x = x1 - delta_x;
2538 new_y = y1 + delta_y;
2539 }
2540 }
2541
2542 int
2543 bearing(double x1, double y1, double x2, double y2)
2544 {
2545 int quadrant;
2546 double delta_x,
2547 delta_y;
2548 double oppoadj;
2549 double bearing;
2550
2551 /*
2552 * Object two in which quadrant compared to object one? 0 x = opp, y =
2553 * ajd + 0 degrees 1 x = adj, y = opp + 90 degrees 2 x = opp, y = ajd
2554 * + 180 degrees 3 x = adj, y = opp + 270 degrees
2555 */
2556
2557 if (x2 > x1) {
2558 delta_x = x2 - x1;
2559 if (y2 > y1) {
2560 quadrant = 0;
2561 delta_y = y2 - y1;
2562 oppoadj = delta_x / delta_y;
2563 } else {
2564 quadrant = 1;
2565 delta_y = y1 - y2;
2566 oppoadj = delta_y / delta_x;
2567 }
2568 } else {
2569 delta_x = x1 - x2;
2570 if (y2 > y1) {
2571 quadrant = 3;
2572 delta_y = y2 - y1;
2573 oppoadj = delta_y / delta_x;
2574 } else {
2575 quadrant = 2;
2576 delta_y = y1 - y2;
2577 oppoadj = delta_x / delta_y;
2578 }
2579 }
2580
2581 bearing = atan(oppoadj);
2582 bearing = bearing / (2.0 * M_PI) * 360.;
2583 bearing = bearing + (90 * quadrant);
2584
2585 return ((int) bearing);
2586 }
2587
2588 void
2589 set_arguments(const char *function_call)
2590 {
2591 /* THIS FUNCTION CREATES AN ARRAY OF JACL INTEGER CONSTANTS TO
2592 REPRESENT THE ARGUMENTS PASSED TO A JACL FUNCTION */
2593 int index,
2594 counter,
2595 length;
2596 int position = 0; /* STORE THE INDEX OF THE WORD */
2597 /* SETTING new_word TO FALSE SKIPS THE FIRST */
2598 /* WORD WHICH IS THE FUNCTION NAME */
2599 int new_word = FALSE;
2600
2601 char *arg_ptr[MAX_WORDS];
2602 int arg_value[MAX_WORDS];
2603
2604 struct integer_type *resolved_integer;
2605 struct cinteger_type *resolved_cinteger;
2606
2607 /* SPLIT UP THE FUNCTION CALL STRING AND EXTRACT THE ARGUMENTS */
2608 length = strlen(function_call);
2609
2610 for (index = 0; index < length; index++) {
2611 if (function_call[index] == '<') {
2612 argument_buffer[index] = 0;
2613 new_word = TRUE;
2614 } else {
2615 // COPY THE CHARACTER FROM THE CALLED NAME INTO THE CURRENT
2616 // ARGUMENT BUFFER
2617 argument_buffer[index] = function_call[index];
2618 if (new_word) {
2619 // THIS IS THE FIRST CHARACTER OF A NEW ARGUMENT SO STORE
2620 // THE ADDRESS OF THIS CHARACTER IN THE ARGUMENT BUFFER
2621 arg_ptr[position] = &argument_buffer[index];
2622 new_word = FALSE;
2623 if (position < MAX_WORDS)
2624 position++;
2625 }
2626 }
2627 }
2628
2629 argument_buffer[index] = 0;
2630
2631 /* CLEAR THE NEXT ARGUMENT POINTER */
2632 arg_ptr[position] = NULL;
2633
2634 /* STORE THE INTEGER VALUE OF EACH ARGUMENT PASSED*/
2635 index = 0;
2636 while (arg_ptr[index] != NULL) {
2637 //arg_value[index] = value_of(arg_ptr[index], TRUE);
2638
2639 if ((resolved_integer = integer_resolve(arg_ptr[index])) != NULL) {
2640 arg_value[index] = resolved_integer->value;
2641 } else if ((resolved_cinteger = cinteger_resolve(arg_ptr[index])) != NULL) {
2642 arg_value[index] = resolved_cinteger->value;
2643 } else if (object_element_resolve(arg_ptr[index])) {
2644 arg_value[index] = oec;
2645 } else if ((counter = object_resolve(arg_ptr[index])) != -1) {
2646 if (counter < 1 || counter > objects) {
2647 badptrrun(arg_ptr[index], counter);
2648 pop_stack();
2649 return;
2650 } else {
2651 arg_value[index] = counter;
2652 }
2653 } else if (validate(arg_ptr[index])) {
2654 arg_value[index] = atoi(arg_ptr[index]);
2655 } else {
2656 arg_value[index] = -1;
2657 }
2658
2659 index++;
2660 }
2661
2662 /* THE CURRENT ARGUMENTS HAVE ALREADY BEEN PUSHED ONTO THE STACK
2663 * AND STORED IF PASSED AS AN ARGUMENT TO THIS FUNCTION SO IT IS
2664 * OKAY TO CLEAR THEM AND SET THE NEW VALUES */
2665 clear_cinteger("arg");
2666 clear_cstring("string_arg");
2667
2668 /* CREATE A CONSTANT FOR EACH ARGUMENT AFTER THE CORE FUNCTION NAME */
2669 index = 0;
2670 while (arg_ptr[index] != NULL) {
2671 if (index == 0) noun[3] = arg_value[index];
2672 add_cinteger ("arg", arg_value[index]);
2673 //printf("--- %s = %s\n", arg_ptr[index], arg_text_of(arg_ptr[index]));
2674 add_cstring ("string_arg", arg_text_of(arg_ptr[index]));
2675 index++;
2676 }
2677 }
2678
2679 void
2680 pop_stack()
2681 {
2682 int index, counter;
2683
2684 stack--;
2685
2686 clear_cinteger ("arg");
2687 clear_cstring ("string_arg");
2688
2689 /* RECREATE THE arg ARRAY FOR THIS STACK FRAME */
2690 for (index = 0; index < backup[stack].argcount; index++) {
2691 if (index == 0) noun[3] = backup[stack].arguments[0];
2692 add_cinteger ("arg", backup[stack].arguments[index]);
2693 }
2694
2695 /* RECREATE THE string_arg ARRAY FOR THIS STACK FRAME */
2696 for (index = 0; index < backup[stack].argcount; index++) {
2697 add_cstring ("string_arg", backup[stack].str_arguments[index]);
2698 }
2699
2700 /* RESTORE THE CONTENTS OF text_buffer */
2701 for (counter = 0; counter < 1024; counter++)
2702 text_buffer[counter] = backup[stack].text_buffer[counter];
2703
2704 /* RESTORE THE CONTENTS OF called_name */
2705 //for (counter = 0; counter < 256; counter++)
2706 //called_name[counter] = backup[stack].called_name[counter];
2707 strncpy(called_name, backup[stack].called_name, 1024);
2708
2709 /* RESTORE THE CONTENTS OF scope_criterion */
2710 //for (counter = 0; counter < 21; counter++)
2711 // scope_criterion[counter] = backup[stack].scope_criterion[counter];
2712 strncpy(scope_criterion, backup[stack].scope_criterion, 20);
2713
2714 /* RESTORE THE STORED FUNCTION NAMES THAT ARE USED WHEN AN
2715 * 'override' COMMAND IS ENCOUNTERED IN THE CURRENT FUNCTION */
2716 strncpy (override, backup[stack].override, 80);
2717 strncpy (default_function, backup[stack].default_function, 80);
2718
2719 /* RESTORE ALL THE WORD POINTERS */
2720 for (counter = 0; counter < MAX_WORDS; counter++) {
2721 word[counter] = backup[stack].word[counter];
2722 quoted[counter] = backup[stack].quoted[counter];
2723 }
2724
2725 executing_function = backup[stack].function;
2726
2727 if (executing_function != NULL) {
2728 strncpy (function_name, executing_function->name, 80);
2729 strncpy (cstring_resolve("function_name")->value, executing_function->name, 80);
2730 }
2731
2732 wp = backup[stack].wp;
2733 LOCAL->value = backup[stack].local;
2734 LOCAL_X->value = backup[stack].local_x;
2735 LOCAL_Y->value = backup[stack].local_y;
2736 LOCAL_A->value = backup[stack].local_a;
2737 top_of_loop = backup[stack].top_of_loop;
2738 outfile = backup[stack].outfile;
2739 infile = backup[stack].infile;
2740 top_of_select = backup[stack].top_of_select;
2741 top_of_while = backup[stack].top_of_while;
2742 top_of_iterate = backup[stack].top_of_iterate;
2743 top_of_update = backup[stack].top_of_update;
2744 top_of_do_loop = backup[stack].top_of_do_loop;
2745 criterion_value = backup[stack].criterion_value;
2746 criterion_type = backup[stack].criterion_type;
2747 criterion_negate = backup[stack].criterion_negate;
2748 current_level = backup[stack].current_level;
2749 execution_level = backup[stack].execution_level;
2750 loop_integer = backup[stack].loop_integer;
2751 select_integer = backup[stack].select_integer;
2752
2753 #ifdef GLK
2754 glk_stream_set_position(game_stream, backup[stack].address, seekmode_Start);
2755 #else
2756 fseek(file, backup[stack].address, SEEK_SET);
2757 #endif
2758
2759 }
2760
2761 #ifdef GLK
2762 void
2763 push_stack(glsi32 file_pointer)
2764 #else
2765 void
2766 push_stack(long file_pointer)
2767 #endif
2768 {
2769 /* COPY ALL THE CURRENT SYSTEM DATA ONTO THE STACK */
2770 int index;
2771 int counter = 0;
2772
2773 if (stack == STACK_SIZE) {
2774 log_error("Stack overflow.", PLUS_STDERR);
2775 terminate(45);
2776 } else {
2777 backup[stack].infile = infile;
2778 infile = NULL;
2779 backup[stack].outfile = outfile;
2780 outfile = NULL;
2781 backup[stack].function = executing_function;
2782 backup[stack].address = file_pointer;
2783 backup[stack].wp = wp;
2784 backup[stack].local = LOCAL->value;
2785 backup[stack].local_x = LOCAL_X->value;
2786 backup[stack].local_y = LOCAL_Y->value;
2787 backup[stack].local_a = LOCAL_A->value;
2788 backup[stack].top_of_loop = top_of_loop;
2789 backup[stack].top_of_select = top_of_select;
2790 backup[stack].top_of_while = top_of_while;
2791 backup[stack].top_of_iterate = top_of_iterate;
2792 backup[stack].top_of_update = top_of_update;
2793 backup[stack].top_of_do_loop = top_of_do_loop;
2794 backup[stack].criterion_value = criterion_value;
2795 backup[stack].criterion_type = criterion_type;
2796 backup[stack].criterion_negate = criterion_negate;
2797 backup[stack].current_level = current_level;
2798 backup[stack].execution_level = execution_level;
2799 backup[stack].loop_integer = loop_integer;
2800 backup[stack].select_integer = select_integer;
2801
2802 /* MAKE A COPY OF THE CURRENT CONTENTS OF text_buffer */
2803 for (counter = 0; counter < 1024; counter++)
2804 backup[stack].text_buffer[counter] = text_buffer[counter];
2805
2806 /* MAKE A COPY OF THE CURRENT CONTENTS OF called_name */
2807 strncpy(backup[stack].called_name, called_name, 1024);
2808
2809 // MAKE A COPY OF THE CURRENT CONTENTS OF scope_criterion
2810 strncpy(backup[stack].scope_criterion, scope_criterion, 20);
2811
2812 /* COPY THE STORED FUNCTION NAMES THAT ARE USED WHEN AN
2813 * 'override' COMMAND IS ENCOUNTERED IN THE CURRENT FUNCTION */
2814 strncpy (backup[stack].override, override, 80);
2815 strncpy (backup[stack].default_function, default_function, 80);
2816
2817 /* PUSH ALL THE WORD POINTERS ONTO THE STACK */
2818 for (counter = 0; counter < MAX_WORDS; counter++) {
2819 backup[stack].word[counter] = word[counter];
2820 backup[stack].quoted[counter] = quoted[counter];
2821 }
2822
2823 // PUSH ALL THE ARGUMENTS AS INTEGERS ONTO THE STACK
2824 index = 0;
2825 current_cinteger = cinteger_table;
2826
2827 if (current_cinteger != NULL) {
2828 do {
2829 if (!strcmp(current_cinteger->name, "arg")) {
2830 backup[stack].arguments[index++] = current_cinteger->value;
2831 }
2832 current_cinteger = current_cinteger->next_cinteger;
2833 }
2834 while (current_cinteger != NULL);
2835 }
2836
2837 // STORE THE NUMBER OF ARGUMENTS PASSED TO THIS FUNCTION
2838 // THIS IS THE SAME NUMBER FOR STRINGS AND INTEGERS
2839 backup[stack].argcount = index;
2840
2841 // PUSH ALL THE ARGUMENTS AS STRINGS STRING ONTO THE STACK
2842 index = 0;
2843 current_cstring = cstring_table;
2844
2845 if (current_cstring != NULL) {
2846 do {
2847 if (!strcmp(current_cstring->name, "string_arg")) {
2848 strncpy(backup[stack].str_arguments[index++], current_cstring->value, 1023);
2849 }
2850
2851 current_cstring = current_cstring->next_string;
2852 }
2853 while (current_cstring != NULL);
2854 }
2855 }
2856
2857 // PUSH ON TO THE NEXT STACK FRAME
2858 stack++;
2859 }
2860
2861 void
2862 pop_proxy()
2863 {
2864 int index, counter;
2865
2866 proxy_stack--;
2867
2868 clear_cinteger ("$integer");
2869 clear_cstring ("$string");
2870 clear_cstring ("$word");
2871
2872 /* RECREATE THE integer ARRAY FOR THIS STACK FRAME */
2873 for (index = 0; index < proxy_backup[proxy_stack].integercount; index++) {
2874 add_cinteger ("$integer", proxy_backup[proxy_stack].integer[index]);
2875 }
2876
2877 /* RECREATE THE text ARRAY FOR THIS STACK FRAME */
2878 for (index = 0; index < proxy_backup[proxy_stack].textcount; index++) {
2879 add_cstring ("$string", proxy_backup[proxy_stack].text[index]);
2880 }
2881
2882 /* RECREATE THE $word ARRAY FOR THIS STACK FRAME */
2883 for (index = 0; index < proxy_backup[proxy_stack].commandcount; index++) {
2884 add_cstring ("$word", proxy_backup[proxy_stack].command[index]);
2885 }
2886
2887 /* RESTORE ALL THE NOUN POINTERS */
2888 for (counter = 0; counter < 4; counter++)
2889 noun[counter] = proxy_backup[proxy_stack].object_pointers[counter];
2890
2891 /* PUSH ALL THE RESOLVED OBJECTS ONTO THE STACK */
2892 for (index = 0; index < 4; index++) {
2893 list_size[index] = proxy_backup[proxy_stack].list_size[index];
2894 max_size[index] = proxy_backup[proxy_stack].max_size[index];
2895 for (counter = 0; counter < max_size[index]; counter++) {
2896 object_list[index][counter] = proxy_backup[proxy_stack].object_list[index][counter];
2897 }
2898 }
2899
2900 start_of_this_command = proxy_backup[proxy_stack].start_of_this_command;
2901 start_of_last_command = proxy_backup[proxy_stack].start_of_last_command;
2902 after_from = proxy_backup[proxy_stack].after_from;
2903 last_exact = proxy_backup[proxy_stack].last_exact;
2904 }
2905
2906 void
2907 push_proxy()
2908 {
2909 /* COPY ALL THE CURRENT SYSTEM DATA ONTO THE STACK */
2910 int index;
2911 int counter = 0;
2912 int command = 0;
2913 int text = 0;
2914
2915 current_cinteger = cinteger_table;
2916 current_cstring = cstring_table;
2917
2918 if (proxy_stack == STACK_SIZE) {
2919 log_error("Stack overflow.", PLUS_STDERR);
2920 terminate(45);
2921 } else {
2922 proxy_backup[proxy_stack].start_of_this_command = start_of_this_command;
2923 proxy_backup[proxy_stack].start_of_last_command = start_of_last_command;
2924
2925 /* PUSH ALL THE OBJECT POINTERS ONTO THE STACK */
2926 for (counter = 0; counter < 4; counter++)
2927 proxy_backup[proxy_stack].object_pointers[counter] = noun[counter];
2928
2929 /* PUSH ALL THE RESOLVED OBJECTS ONTO THE STACK */
2930 for (index = 0; index < 4; index++) {
2931 for (counter = 0; counter < max_size[index]; counter++) {
2932 proxy_backup[proxy_stack].object_list[index][counter]
2933 = object_list[index][counter];
2934 }
2935 proxy_backup[proxy_stack].list_size[index] = list_size[index];
2936 proxy_backup[proxy_stack].max_size[index] = max_size[index];
2937 }
2938
2939 /* PUSH ALL THE CURRENT COMMAND INTEGERS ONTO THE STACK */
2940 counter = 0;
2941
2942 if (current_cinteger != NULL) {
2943 do {
2944 if (!strcmp(current_cinteger->name, "$integer")) {
2945 proxy_backup[proxy_stack].integer[counter++] = current_cinteger->value; }
2946 current_cinteger = current_cinteger->next_cinteger;
2947 }
2948 while (current_cinteger != NULL);
2949 }
2950
2951 proxy_backup[proxy_stack].integercount = counter;
2952
2953 // PUSH ALL THE TEXT STRING SUPPLIED BY THE CURRENT COMMAND ONTO THE STACK
2954 text = 0;
2955 command = 0;
2956
2957 if (current_cstring != NULL) {
2958 do {
2959 if (!strcmp(current_cstring->name, "$string")) {
2960 strncpy(proxy_backup[proxy_stack].text[text++], current_cstring->value, 254);
2961 proxy_backup[proxy_stack].text[counter++][255] = 0;
2962 } else if (!strcmp(current_cstring->name, "$word")) {
2963 strncpy(proxy_backup[proxy_stack].command[command++], current_cstring->value, 254);
2964 proxy_backup[proxy_stack].command[command++][255] = 0;
2965 }
2966
2967 current_cstring = current_cstring->next_string;
2968 }
2969 while (current_cstring != NULL);
2970 }
2971
2972 proxy_backup[proxy_stack].textcount = counter;
2973 proxy_backup[proxy_stack].commandcount = command;
2974 proxy_backup[proxy_stack].after_from = after_from;
2975 proxy_backup[proxy_stack].last_exact = last_exact;
2976 }
2977
2978 // PUSH ON TO THE NEXT STACK FRAME
2979 proxy_stack++;
2980 }
2981
2982 int
2983 condition()
2984 {
2985 /* COMPARE GROUPS OF TWO ELEMENTS. RETURN TRUE IF ANY ONE GROUP OF
2986 * ELEMENTS COMPARE 'TRUE' */
2987 int first;
2988
2989 first = 1;
2990
2991 while (word[first + 2] != NULL && ((first +2) < MAX_WORDS)) {
2992 if (logic_test(first))
2993 return (TRUE);
2994 else
2995 first = first + 3;
2996 }
2997 return (FALSE);
2998 }
2999
3000 int
3001 and_condition()
3002 {
3003 /* COMPARE GROUPS OF TWO ELEMENTS. RETURN FALSE IF ANY ONE GROUP OF
3004 * ELEMENTS COMPARE 'FALSE' */
3005 int first;
3006
3007 first = 1;
3008
3009 while (word[first + 2] != NULL && ((first +2) < MAX_WORDS)) {
3010 if (logic_test(first) == FALSE)
3011 return (FALSE);
3012 else
3013 first = first + 3;
3014 }
3015 return (TRUE);
3016 }
3017
3018 int
3019 logic_test(int first)
3020 {
3021 long index,
3022 compare;
3023
3024 resolved_attribute = FALSE;
3025
3026 index = value_of(word[first], TRUE);
3027 compare = value_of(word[first + 2], TRUE);
3028
3029 if (!strcmp(word[first + 1], "=") || !strcmp(word[first + 1], "==")) {
3030 if (index == compare)
3031 return (TRUE);
3032 else
3033 return (FALSE);
3034 } else if (!strcmp(word[first + 1], ">")) {
3035 if (index > compare)
3036 return (TRUE);
3037 else
3038 return (FALSE);
3039 } else if (!strcmp(word[first + 1], "<")) {
3040 if (index < compare)
3041 return (TRUE);
3042 else
3043 return (FALSE);
3044 } else if (!strcmp(word[first + 1], "is")) {
3045 if (index < 1 || index > objects) {
3046 unkobjrun(first);
3047 return (FALSE);
3048 } else
3049 return (scope(index, word[first + 2], FALSE));
3050 } else if (!strcmp(word[first + 1], "isnt")) {
3051 if (index < 1 || index > objects) {
3052 unkobjrun(first);
3053 return (FALSE);
3054 } else
3055 return (!scope(index, word[first + 2], FALSE));
3056 } else if (!strcmp(word[first + 1], "has"))
3057 if (index < 1 || index > objects) {
3058 unkobjrun(first);
3059 return (FALSE);
3060 } else {
3061 if (resolved_attribute == SYSTEM_ATTRIBUTE) {
3062 return (object[index]->attributes & compare);
3063 } else {
3064 return (object[index]->user_attributes & compare);
3065 }
3066 }
3067 else if (!strcmp(word[first + 1], "hasnt"))
3068 if (index < 1 || index > objects) {
3069 unkobjrun(first);
3070 return (FALSE);
3071 } else {
3072 if (resolved_attribute == SYSTEM_ATTRIBUTE) {
3073 return (!(object[index]->attributes & compare));
3074 } else {
3075 return (!(object[index]->user_attributes & compare));
3076 }
3077 }
3078 else if (!strcmp(word[first + 1], "!=")
3079 || !strcmp(word[first + 1], "<>")) {
3080 if (index != compare)
3081 return (TRUE);
3082 else
3083 return (FALSE);
3084 } else if (!strcmp(word[first + 1], ">=")
3085 || !strcmp(word[first + 1], "=>")) {
3086 if (index >= compare)
3087 return (TRUE);
3088 else
3089 return (FALSE);
3090 } else if (!strcmp(word[first + 1], "<=")
3091 || !strcmp(word[first + 1], "=<")) {
3092 if (index <= compare)
3093 return (TRUE);
3094 else
3095 return (FALSE);
3096 } else if (!strcmp(word[first + 1], "grandof")) {
3097 /* GRANDOF SAYS THAT AN OBJECT IS THE EVENTUAL PARENT OF ANOTHER OBJECT, NOT
3098 * NECESSARILY IMMEDIATE */
3099 if (index < 1 || index > objects) {
3100 unkobjrun(first);
3101 return (FALSE);
3102 } else {
3103 if (compare < 1 || compare > objects) {
3104 unkobjrun(first + 2);
3105 return (FALSE);
3106 } else {
3107 if (parent_of(index, compare, UNRESTRICT))
3108 return (TRUE);
3109 else
3110 return (FALSE);
3111 }
3112 }
3113 } else if (!strcmp(word[first + 1], "!grandof")) {
3114 if (index < 1 || index > objects) {
3115 unkobjrun(first);
3116 return (FALSE);
3117 } else {
3118 if (compare < 1 || compare > objects) {
3119 unkobjrun(first + 2);
3120 return (FALSE);
3121 } else {
3122 if (parent_of(index, compare, UNRESTRICT))
3123 return (FALSE);
3124 else
3125 return (TRUE);
3126 }
3127 }
3128 } else {
3129 sprintf(error_buffer,
3130 "ERROR: In function \"%s\", illegal operator \"%s\".^",
3131 executing_function->name, word[2]);
3132 write_text(error_buffer);
3133 return (FALSE);
3134 }
3135 }
3136
3137 int
3138 strcondition()
3139 {
3140 int first;
3141
3142 first = 1;
3143
3144 while (word[first + 2] != NULL && ((first +2) < MAX_WORDS)) {
3145 if (str_test(first))
3146 return (TRUE);
3147 else
3148 first = first + 3;
3149 }
3150 return (FALSE);
3151 }
3152
3153 int
3154 and_strcondition()
3155 {
3156 int first;
3157
3158 first = 1;
3159
3160 while (word[first + 2] != NULL && ((first +2) < MAX_WORDS)) {
3161 if (str_test(first) == FALSE)
3162 return (FALSE);
3163 else
3164 first = first + 3;
3165 }
3166 return (TRUE);
3167 }
3168
3169 int
3170 str_test(int first)
3171 {
3172 const char *index;
3173 const char *compare;
3174
3175 // GET THE TWO STRING VALUES TO COMPARE
3176
3177 index = arg_text_of_word(first);
3178 compare = arg_text_of_word(first + 2);
3179
3180 if (!strcmp(word[first + 1], "==") || !strcmp(word[first + 1], "=")) {
3181 if (!strcasecmp(index, compare)) {
3182 return (TRUE);
3183 } else {
3184 return (FALSE);
3185 }
3186 } else if (!strcmp(word[first + 1], "!contains")) {
3187 if (strcasestr(index, compare))
3188 return (FALSE);
3189 else
3190 return (TRUE);
3191 } else if (!strcmp(word[first + 1], "contains")) {
3192 if (strcasestr(index, compare))
3193 return (TRUE);
3194 else
3195 return (FALSE);
3196 } else if (!strcmp(word[first + 1], "!beginswith")) {
3197 if (strncmp(index, compare, strlen(compare)))
3198 return (TRUE);
3199 else
3200 return (FALSE);
3201 } else if (!strcmp(word[first + 1], "beginswith")) {
3202 if (!strncmp(index, compare, strlen(compare)))
3203 return (TRUE);
3204 else
3205 return (FALSE);
3206 } else if (!strcmp(word[first + 1], "<>") || !strcmp(word[first + 1], "!=")) {
3207 if (strcasecmp(index, compare))
3208 return (TRUE);
3209 else
3210 return (FALSE);
3211 } else if (!strcmp(word[first + 1], "==C") || !strcmp(word[first + 1], "=C")) {
3212 if (!strcmp(index, compare)) {
3213 return (TRUE);
3214 } else {
3215 return (FALSE);
3216 }
3217 } else if (!strcmp(word[first + 1], "!containsC")) {
3218 if (strstr(index, compare))
3219 return (FALSE);
3220 else
3221 return (TRUE);
3222 } else if (!strcmp(word[first + 1], "containsC")) {
3223 if (strstr(index, compare))
3224 return (TRUE);
3225 else
3226 return (FALSE);
3227 } else if (!strcmp(word[first + 1], "<>C") || !strcmp(word[first + 1], "!=C")) {
3228 if (strcmp(index, compare))
3229 return (TRUE);
3230 else
3231 return (FALSE);
3232 } else {
3233 sprintf(error_buffer,
3234 "ERROR: In function \"%s\", illegal operator \"%s\".^",
3235 executing_function->name, word[2]);
3236 write_text(error_buffer);
3237 return (FALSE);
3238 }
3239 }
3240
3241 void
3242 add_cinteger(const char *name, int value)
3243 {
3244 /* THIS FUNCTION ADDS A NEW JACL CONSTANT TO THE LIST */
3245
3246 if ((new_cinteger = (struct cinteger_type *)
3247 malloc(sizeof(struct cinteger_type))) == NULL)
3248 outofmem();
3249 else {
3250 if (cinteger_table == NULL) {
3251 cinteger_table = new_cinteger;
3252 } else {
3253 /* FIND LAST CONSTANT IN LIST */
3254 current_cinteger = cinteger_table;
3255 while (current_cinteger->next_cinteger != NULL) {
3256 current_cinteger = current_cinteger->next_cinteger;
3257 }
3258 current_cinteger->next_cinteger = new_cinteger;
3259 }
3260 strncpy(new_cinteger->name, name, 40);
3261 new_cinteger->name[40] = 0;
3262 new_cinteger->value = value;
3263 new_cinteger->next_cinteger = NULL;
3264 }
3265 }
3266
3267 void
3268 clear_cinteger(const char *name)
3269 {
3270 /* FREE CONSTANTS THAT HAVE SUPPLIED NAME*/
3271
3272 //printf("--- clear integer %s\n", name);
3273 if (cinteger_table != NULL) {
3274 current_cinteger = cinteger_table;
3275 previous_cinteger = cinteger_table;
3276 while (current_cinteger != NULL) {
3277 //sprintf(temp_buffer, "--- checking integer %s^", current_cinteger->name);
3278 //write_text(temp_buffer);
3279 if (!strcmp(current_cinteger->name, name)) {
3280 //sprintf(temp_buffer, "--- found integer %s^", name);
3281 //write_text(temp_buffer);
3282 /* FREE THIS CONSTANT */
3283 if (previous_cinteger == current_cinteger) {
3284 // THE INTEGER BEING CLEARED IS THE FIRST INTEGER IN THE LIST
3285 cinteger_table = current_cinteger->next_cinteger;
3286 previous_cinteger = current_cinteger->next_cinteger;
3287 free(current_cinteger);
3288 current_cinteger = previous_cinteger;
3289 } else {
3290 previous_cinteger->next_cinteger = current_cinteger->next_cinteger;
3291 free(current_cinteger);
3292 current_cinteger = previous_cinteger->next_cinteger;
3293 }
3294 } else {
3295 previous_cinteger = current_cinteger;
3296 current_cinteger = current_cinteger->next_cinteger;
3297 }
3298 }
3299 }
3300 //printf("--- leaving clear integer\n");
3301 }
3302
3303 void
3304 add_cstring(const char *name, const char *value)
3305 {
3306 /* ADD A STRING CONSTANT WITH THE SUPPLIED NAME AND VALUE */
3307
3308 if ((new_string = (struct string_type *)
3309 malloc(sizeof(struct string_type))) == NULL)
3310 outofmem();
3311 else {
3312 if (cstring_table == NULL) {
3313 cstring_table = new_string;
3314 } else {
3315 /* FIND LAST STRING IN LIST */
3316 current_cstring = cstring_table;
3317 while (current_cstring->next_string != NULL) {
3318 current_cstring = current_cstring->next_string;
3319 }
3320 current_cstring->next_string = new_string;
3321 }
3322 strncpy(new_string->name, name, 40);
3323 new_string->name[40] = 0;
3324 strncpy(new_string->value, value, 1023);
3325 new_string->value[1024] = 0;
3326 new_string->next_string = NULL;
3327 }
3328 }
3329
3330 void
3331 clear_cstring(const char *name)
3332 {
3333 /* FREE CONSTANTS THAT HAVE SUPPLIED NAME*/
3334 if (cstring_table != NULL) {
3335 current_cstring = cstring_table;
3336 previous_cstring = cstring_table;
3337 while (current_cstring != NULL) {
3338 if (!strcmp(current_cstring->name, name)) {
3339 /* FREE THIS STRING */
3340 if (previous_cstring == current_cstring) {
3341 cstring_table = current_cstring->next_string;
3342 previous_cstring = current_cstring->next_string;
3343 free(current_cstring);
3344 current_cstring = previous_cstring;
3345 } else {
3346 previous_cstring->next_string = current_cstring->next_string;
3347 free(current_cstring);
3348 current_cstring = previous_cstring->next_string;
3349 }
3350 } else {
3351 previous_cstring = current_cstring;
3352 current_cstring = current_cstring->next_string;
3353 }
3354 }
3355 }
3356 }
3357
3358 void
3359 inspect (int object_num)
3360 {
3361 // THIS FUNCTION DISPLAYS THE STATE OF A JACL OBJECT FOR DEBUGGING
3362
3363 int index, attribute_value;
3364
3365 struct attribute_type *pointer = attribute_table;
3366
3367 if (object_num < 1 || object_num > objects) {
3368 badptrrun(word[1], object_num);
3369 return;
3370 }
3371
3372 write_text("label: ");
3373 write_text(object[object_num]->label);
3374
3375 if (object[object_num]->attributes & LOCATION) {
3376 // OUTPUT ALL THE ATTRIBUTES WITH LOCATION ATTRIBUTE TEXT
3377 write_text("^has location attributes: ");
3378 index = 0;
3379 attribute_value = 1;
3380 while (location_attributes[index] != NULL) {
3381 if (object[object_num]->attributes & attribute_value) {
3382 write_text(location_attributes[index]);
3383 }
3384 index++;
3385 attribute_value *= 2;
3386 }
3387 } else {
3388 // OUTPUT ALL THE ATTRIBUTES WITH OBJECT ATTRIBUTE TEXT
3389 write_text("^has object attributes: ");
3390 index = 0;
3391 attribute_value = 1;
3392 while (object_attributes[index] != NULL) {
3393 if (object[object_num]->attributes & attribute_value) {
3394 write_text(object_attributes[index]);
3395 }
3396 index++;
3397 attribute_value *= 2;
3398 }
3399
3400 write_text("^has user attributes: ");
3401 attribute_value = 1;
3402 }
3403
3404 if (pointer != NULL) {
3405 // THERE ARE USER ATTRIBUTES, SO CHECK IF THIS OBJECT OR LOCATION
3406 // HAS ANY OF THEM
3407 do {
3408 if (object[object_num]->user_attributes & pointer->value) {
3409 write_text(pointer->name);
3410 write_text(" ");
3411 }
3412
3413 pointer = pointer->next_attribute;
3414 } while (pointer != NULL);
3415 }
3416
3417 write_text("^");
3418
3419 index = 0;
3420 if (object[object_num]->attributes & LOCATION) {
3421 while (location_elements[index] != NULL) {
3422 if (index < 12) {
3423 if (object[object_num]->integer[index] < 1 || object[object_num]->integer[index] > objects) {
3424 sprintf(temp_buffer, "%s: nowhere (%d)^", location_elements[index], object[object_num]->integer[index]);
3425 } else {
3426 sprintf(temp_buffer, "%s: %s (%d)^", location_elements[index], object[object[object_num]->integer[index]]->label, object[object_num]->integer[index]);
3427 }
3428 } else {
3429 sprintf(temp_buffer, "%s: %d^", location_elements[index], object[object_num]->integer[index]);
3430 }
3431 write_text(temp_buffer);
3432 index++;
3433 }
3434 } else {
3435 while (object_elements[index] != NULL) {
3436 if (index == 0) {
3437 sprintf(temp_buffer, "%s: %s (%d)^", object_elements[index], object[object[object_num]->integer[index]]->label, object[object_num]->integer[index]);
3438 } else {
3439 sprintf(temp_buffer, "%s: %d^", object_elements[index], object[object_num]->integer[index]);
3440 }
3441 write_text(temp_buffer);
3442 index++;
3443 }
3444 }
3445 }
3446
3447 int
3448 grand_of(int child, int objs_only)
3449 {
3450 /* THIS FUNCTION WILL CLIMB THE OBJECT TREE STARTING AT 'CHILD' UNTIL
3451 * A 'PARENT' IS REACHED */
3452
3453 /* objs_only ARGUMENT TELLS FUNCTION TO IGNORE OBJECT IF IT IS IN A
3454 * LOCATION */
3455
3456 int parent;
3457
3458 if (object[child]->PARENT != NOWHERE) {
3459 /* STORE THE CHILDS PARENT OBJECT */
3460 parent = object[child]->PARENT;
3461
3462 if (object[parent]->attributes & LOCATION) {
3463 if (objs_only) {
3464 /* THE CHILDS PARENT IS LOCATION AND SEARCH IS RESTRICTED TO
3465 * OBJECTS */
3466 return (child);
3467 } else {
3468 return (parent);
3469 }
3470 } else {
3471 /* KEEP LOOKING UP THE TREE UNTIL THE CHILD HAS NO
3472 * PARENT */
3473 return (grand_of(parent, objs_only));
3474 }
3475 } else {
3476 /* THE SPECIFIED OBJECT HAS NO PARENT */
3477 return (child);
3478 }
3479 }
3480
3481 int
3482 select_next()
3483 {
3484
3485 while (++*select_integer <= objects) {
3486 switch (criterion_type) {
3487 case CRI_ATTRIBUTE:
3488 if (object[*select_integer]->attributes & criterion_value) {
3489 if (!criterion_negate) {
3490 return TRUE;
3491 }
3492 } else {
3493 if (criterion_negate) {
3494 return TRUE;
3495 }
3496 }
3497 break;
3498 case CRI_USER_ATTRIBUTE:
3499 if (object[*select_integer]->user_attributes & criterion_value) {
3500 if (!criterion_negate) {
3501 return TRUE;
3502 }
3503 } else {
3504 if (criterion_negate) {
3505 return TRUE;
3506 }
3507 }
3508 break;
3509 case CRI_PARENT:
3510 if (object[*select_integer]->PARENT == criterion_value) {
3511 if (!criterion_negate) {
3512 return TRUE;
3513 }
3514 } else {
3515 if (criterion_negate) {
3516 return TRUE;
3517 }
3518 }
3519 break;
3520 case CRI_SCOPE:
3521 if (scope(*select_integer, scope_criterion, FALSE)) {
3522 if (!criterion_negate) {
3523 return TRUE;
3524 }
3525 } else {
3526 if (criterion_negate) {
3527 return TRUE;
3528 }
3529 }
3530 break;
3531 }
3532 }
3533
3534 return (FALSE);
3535 }
3536
3537 /* Converts an integer value to its hex character*/
3538 char to_hex(char code) {
3539 static char hex[] = "0123456789abcdef";
3540 return hex[code & 15];
3541 }
3542
3543 /* Returns a url-encoded version of str */
3544 /* IMPORTANT: be sure to free() the returned string after use */
3545 char *url_encode(const char *str) {
3546 const char *pstr = str;
3547 char *buf = malloc(strlen(str) * 3 + 1), *pbuf = buf;
3548 while (*pstr) {
3549 if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~')
3550 *pbuf++ = *pstr;
3551 else if (*pstr == ' ')
3552 *pbuf++ = '+';
3553 else {
3554 *pbuf++ = '%'; *pbuf++ = to_hex(*pstr >> 4); *pbuf++ = to_hex(*pstr & 15);
3555 }
3556 pstr++;
3557 }
3558 *pbuf = '\0';
3559 return buf;
3560 }