Debian Codegraph @

rexx.c

raw
1 /*
2 * The Regina Rexx Interpreter
3 * Copyright (C) 1992-1994 Anders Christensen <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 /*
21 * Bug in LCC complier wchar.h that incorrectly says it defines stat struct
22 * but doesn't
23 */
24 #if defined(__LCC__)
25 # include <sys/stat.h>
26 #endif
27
28 #include "regina_c.h"
29
30 #if defined(WIN32) && defined(__IBMC__)
31 #include <windows.h>
32 #pragma warning(default: 4115 4201 4214)
33 #else
34 # ifdef RXLIB
35 # define APIENTRY
36 # if defined(__WATCOMC__) && defined(__NT__)
37 # undef APIENTRY
38 # include <windows.h>
39 # endif
40 # if defined(__MINGW32__) || defined(__LCC__)
41 # undef APIENTRY
42 # include <windows.h>
43 # endif
44 # if defined(WIN32) && defined(__BORLANDC__)
45 # undef APIENTRY
46 # include <windows.h>
47 # endif
48
49 # if defined(_MSC_VER)
50 # undef APIENTRY
51 # if _MSC_VER >= 1100
52 /* Stupid MSC can't compile own headers without warning at least in VC 5.0 */
53 # pragma warning(disable: 4115 4201 4214 4514)
54 # endif
55 # include <windows.h>
56 # if _MSC_VER >= 1100
57 # pragma warning(default: 4115 4201 4214)
58 # endif
59 # endif
60 # else /* not RXLIB */
61 # if defined(__WATCOMC__) && defined(__NT__)
62 # include <windows.h>
63 # endif
64 # if defined(__MINGW32__) || defined(__LCC__)
65 # include <windows.h>
66 # endif
67 # if defined(WIN32) && defined(__BORLANDC__)
68 # include <windows.h>
69 # endif
70
71 # if defined(_MSC_VER) && !defined(__WINS__)
72 # if _MSC_VER >= 1100
73 /* Stupid MSC can't compile own headers without warning at least in VC 5.0 */
74 # pragma warning(disable: 4115 4201 4214 4514)
75 # endif
76 # include <windows.h>
77 # if _MSC_VER >= 1100
78 # pragma warning(default: 4115 4201 4214)
79 # endif
80 # endif
81 # endif
82 #endif
83
84 #if defined(OS2) || defined(__EMX__)
85 # if defined(__WATCOMC__) && defined(RXLIB)
86 # undef APIENTRY
87 # elif defined(__INNOTEK_LIBC__)
88 # undef APIENTRY
89 # define APIENTRY _System
90 # endif
91 # define INCL_BASE
92 # include <os2.h>
93 # define DONT_TYPEDEF_PFN
94 #endif
95
96 #include "rexx.h"
97 #include <string.h>
98 #include <stdio.h>
99 #include <assert.h>
100
101 #ifndef HAVE_GETOPT_LONG
102 # include "mygetopt.h"
103 #endif
104
105 #ifdef VMS
106 # include <stat.h>
107 #elif defined(MAC)
108 # include "mac.h"
109 #else
110 # include <sys/stat.h>
111 #endif
112
113 #if defined(DJGPP) || defined(__EMX__) || defined(_MSC_VER) || (defined(__WATCOMC__) && !defined(__QNX__)) || defined(__EPOC32__)
114 # include <fcntl.h>
115 # if !defined(__WINS__) && !defined(__EPOC32__)
116 # include <io.h>
117 # endif
118 #endif
119
120 #ifdef HAVE_UNISTD_H
121 # include <unistd.h>
122 #endif
123
124 #ifdef __APPLE__
125 # include <mach-o/dyld.h>
126 #endif
127
128 /*
129 * Since development of Ultrix has ceased, and they never managed to
130 * fix a few things, we want to define a few things, just in order
131 * to kill a few warnings ...
132 */
133 #if defined(FIX_PROTOS) && defined(FIX_ALL_PROTOS) && defined(ultrix)
134 int fstat( int fd, struct stat *buf ) ;
135 int stat( char *path, struct stat *buf ) ;
136 #endif
137
138
139 /* Don't terminate the following lines by a semicolon */
140 GLOBAL_PROTECTION_VAR(regina_globals)
141
142
143 /*
144 * Note: must match the settings of NUM_FORM_* in flags.h
145 */
146 const char *numeric_forms[] = { "SCIENTIFIC", "ENGINEERING" } ;
147
148 /*
149 * Note: these must match the definitions of INVO_* in defs.h
150 */
151 const char *invo_strings[] = { "COMMAND", "FUNCTION", "SUBROUTINE" } ;
152
153 const char *argv0 = NULL;
154
155 /*
156 * The following global is to store the TSD of the currently running
157 * interpreter instance.
158 * Each API call checks this variable, and if not NULL, this is used
159 * as the TSD to use.
160 * This enables multiple threads to run within the one interpreter instance.
161 * Set by OPTIONS SINGLE_INTERPRETER
162 */
163 const tsd_t *__regina_global_TSD = NULL;
164
165 static void usage( char * );
166
167 #ifdef TRACEMEM
168 void marksubtree( nodeptr ptr )
169 {
170 unsigned i;
171
172 while ( ptr )
173 {
174 markmemory( ptr, TRC_TREENODE );
175
176 if ( ptr->name )
177 markmemory( ptr->name, TRC_TREENODE );
178
179 for ( i = 0; i < sizeof( ptr->p ) / sizeof( ptr->p[0] ); i++ )
180 marksubtree( ptr->p[i] );
181
182
183 if ( ( ptr->type == X_STRING ) || ( ptr->type == X_CON_SYMBOL ) )
184 {
185 if ( ptr->u.number )
186 {
187 markmemory( ptr->u.number, TRC_TREENODE );
188 markmemory( ptr->u.number->num, TRC_TREENODE );
189 }
190 }
191 else if ( ptr->type == X_CEXPRLIST )
192 {
193 if ( ptr->u.strng )
194 markmemory( ptr->u.strng, TRC_TREENODE );
195 }
196
197 ptr = ptr->next;
198 }
199 }
200 #endif /* TRACEMEM */
201
202
203 /*
204 * GetArgv0 tries to find the fully qualified filename of the current program.
205 * It uses some ugly and system specific tricks and it may return NULL if
206 * it can't find any useful value.
207 * The argument should be argv[0] of main() and it may be returned.
208 * This function must not be called from another as the sole one when starting
209 * up.
210 */
211 static const char *GetArgv0(const char *argv0)
212 {
213 #ifdef WIN32
214 char buf[512];
215
216 if (GetModuleFileName(NULL, buf, sizeof(buf)) != 0)
217 return(strdup(buf)); /* never freed up */
218 #elif defined(__QNX__) && defined(__WATCOMC__)
219 char buffer[PATH_MAX];
220 char *buf;
221 if ( (buf = _cmdname(buffer) ) != NULL )
222 return(strdup(buf)); /* never freed up */
223 #elif defined(OS2)
224 char buf[512];
225 PPIB ppib;
226
227 # ifdef __EMX__
228 if (_osmode == OS2_MODE)
229 {
230 # endif
231 if (DosGetInfoBlocks(NULL, &ppib) == 0)
232 if (DosQueryModuleName(ppib->pib_hmte, sizeof(buf), buf) == 0)
233 return(strdup(buf));
234 # ifdef __EMX__
235 }
236 # endif
237 #endif
238
239 #ifdef __APPLE__
240 {
241 /*
242 * will work on MacOS X
243 */
244 char buf[1024];
245 uint32_t result=sizeof(buf);
246 if ( _NSGetExecutablePath( buf, &result ) == 0 )
247 {
248 return strdup( buf );
249 }
250 }
251 #elif defined(HAVE_READLINK)
252 {
253 /*
254 * will work on Linux 2.1+
255 */
256 char buf[1024];
257 int result;
258 result = readlink("/proc/self/exe", buf, sizeof( buf ) );
259 if ( ( result > 0 ) && ( result < (int) sizeof( buf ) ) && ( buf[0] != '[' ) )
260 {
261 buf[result] = '\0';
262 return strdup( buf );
263 }
264 }
265 #endif
266 /* No specific code has found the right file name. Maybe, it's coded
267 * in argv0. Check it, if it is an absolute path. Be absolutely sure
268 * to detect it safely!
269 */
270 if (argv0 == NULL)
271 return(NULL);
272
273 if (argv0[0] == '/') /* unix systems and some others */
274 return(argv0);
275
276 if ((argv0[0] == '\\') && (argv0[1] == '\\')) /* MS and OS/2 UNC names */
277 return(argv0);
278
279 if (rx_isalpha(argv0[0]) && (argv0[1] == ':') && (argv0[2] == '\\'))
280 return(argv0); /* MS and OS/2 drive letter with path */
281
282 return(NULL); /* not a proven argv0 argument */
283 }
284
285
286 /*
287 * setup_system sets up some basics which are needed to use parts of the
288 * interpreter.
289 * This is a common routine of rexx's or regina's main() and every
290 * SAA function.
291 * isclient should be set to 1 if called from the SAA interface, 0 otherwise.
292 *
293 * Note that you have to set TSD->currlevel->script_exit as fast as possible.
294 * Otherwise you are at high risk that an error will stop the whole process,
295 * which is fatal if an application uses us via SAA API.
296 */
297 void setup_system( tsd_t *TSD, int isclient )
298 {
299 TSD->stddump = stderr;
300
301 TSD->systeminfo = creat_sysinfo( TSD, Str_creTSD( "SYSTEM" ) );
302
303 TSD->systeminfo->currlevel0 = TSD->currlevel = newlevel( TSD, NULL );
304 TSD->systeminfo->trace_override = 0;
305 TSD->isclient = isclient;
306 /*
307 * Under DJGPP setmode screws up Parse Pull and entering code
308 * interactively :-(
309 */
310 #if defined(__EMX__) || (defined(_MSC_VER) && !defined(__WINS__)) || (defined(__WATCOMC__) && !defined(__QNX__))
311 fflush( stdin );
312 fflush( stdout );
313 fflush( stderr );
314 setmode( fileno( stdin ), O_BINARY );
315 setmode( fileno( stdout ), O_BINARY );
316 setmode( fileno( stderr ), O_BINARY );
317 # ifdef _MSC_VER
318 /* set binary mode for pipes as well! */
319 _set_fmode(_O_BINARY);
320 # endif
321 #endif
322 }
323
324 static struct my_getopt_option long_options[] =
325 {
326 {"help", no_argument, 0, 'h' },
327 {"debug", optional_argument, 0, 'D' },
328 {"debug", optional_argument, 0, 'd' },
329 {"trace", optional_argument, 0, 't' },
330 {"pause", no_argument, 0, 'p' },
331 {"version", optional_argument, 0, 'v' },
332 {"yaccdbg", no_argument, 0, 'y' },
333 {"restricted", no_argument, 0, 'r' },
334 {"interactive", optional_argument, 0, 'i' },
335 {"args", no_argument, 0, 'a' },
336 {"compile", no_argument, 0, 'c' },
337 {"execute", no_argument, 0, 'e' },
338 {"locale", required_argument, 0, 'l' },
339 {"options", required_argument, 0, 'o' },
340 {0, 0, 0, 0 }
341 };
342 /*
343 * check_args examines the arguments of the program and assigns the values
344 * to the various structures.
345 *
346 * A process exit of 0 is required if 0 is returned. Otherwise the number
347 * of processed args including the zeroth is returned.
348 */
349 static int check_args( tsd_t *TSD, int argc, char **argv,
350 int *compile_to_tokens, int *execute_from_tokens,
351 int *locale_set )
352 {
353 int c;
354 while (1)
355 {
356 int option_index = 0;
357
358 c = my_getopt_long( argc, argv, "+hi::D::t::pv::yrd::acel:o:", long_options, &option_index );
359 if ( c == -1 )
360 break;
361
362 switch(c)
363 {
364 case 'i':
365 starttrace( TSD );
366 set_trace_char( TSD, '?' );
367 if ( optarg )
368 {
369 if ( strlen( optarg ) > 1 )
370 {
371 usage( argv[0] );
372 fprintf( stdout, "\nThe passed switch `-t' allows just one additional character, Regina exits.\n" );
373 exit( 1 );
374 }
375 set_trace_char( TSD, *optarg );
376 }
377 else
378 set_trace_char( TSD, 'A' );
379 /*
380 * Ensure PARSE SOURCE does not crash; Bug #571
381 */
382 TSD->systeminfo->input_file = Str_crestrTSD( "<stdin>" );
383 TSD->systeminfo->input_fp = NULL;
384 intertrace( TSD );
385 intertrace( TSD );
386 break;
387 /*
388 starttrace( TSD );
389 set_trace_char( TSD, '?' );
390 set_trace_char( TSD, 'A' );
391 intertrace( TSD );
392 intertrace( TSD );
393 break;
394 */
395 case 'p':
396 #if !defined(__WINS__) && !defined(__EPOC32__)
397 set_pause_at_exit();
398 #endif
399 break;
400
401 case 'v':
402 if ( optarg )
403 {
404 if ( strlen( optarg ) > 1 )
405 {
406 fprintf( stdout, "\nThe passed switch `-v' allows just one additional character, Regina exits.\n" );
407 exit( 1 );
408 }
409 switch( *optarg )
410 {
411 #if defined(HAVE_REGINA_ADDON_DIR)
412 case 'a':
413 fprintf( stdout, "%s\n", HAVE_REGINA_ADDON_DIR );
414 break;
415 #endif
416 case 'p':
417 /*
418 * Display any statically linked packages
419 */
420 #if defined( DYNAMIC_STATIC )
421 static_list_packages();
422 #else
423 fprintf( stdout, "\nRegina not built with static dynamic loading support.\n" );
424 #endif
425 break;
426 case 'v':
427 fprintf( stdout, "%s\n", REGINA_VERSION_MAJOR "." REGINA_VERSION_MINOR "." REGINA_VERSION_RELEASE REGINA_VERSION_SUPP );
428 break;
429 case 'b':
430 fprintf( stdout, "%d\n", REGINA_BITS );
431 break;
432 default:
433 fprintf( stdout, "\nUnsupported option to '-v' switch, Regina exits.\n" );
434 exit( 1 );
435 }
436 }
437 else
438 fprintf( stdout, "%s: %s (%d bit)\n", argv[0], PARSE_VERSION_STRING, REGINA_BITS );
439 return 0;
440
441 case 'y':
442 #ifndef NDEBUG
443 __reginadebug = 1; /* yacc-debugging */
444 #endif
445 break;
446
447 case 'r': /* safe-rexx */
448 TSD->restricted = 1;
449 break;
450
451 case 't':
452 if ( optarg )
453 {
454 if ( strlen( optarg ) > 1 )
455 {
456 usage( argv[0] );
457 fprintf( stdout, "\nThe passed switch `-t' allows just one additional character, Regina exits.\n" );
458 exit( 1 );
459 }
460 queue_trace_char( TSD, *optarg );
461 }
462 else
463 queue_trace_char( TSD, 'A' );
464 TSD->systeminfo->trace_override = 1;
465 break;
466
467 case 'd':
468 case 'D':
469 if ( optarg )
470 {
471 if ( *optarg == 'm' )
472 TSD->listleakedmemory = 1;
473 }
474 break;
475
476 case 'a': /* multiple args */
477 TSD->systeminfo->invoked = INVO_SUBROUTINE;
478 break;
479
480 case 'c': /* compile to tokenised file */
481 if ( *execute_from_tokens )
482 {
483 usage( argv[0] );
484 fprintf( stdout, "\nThe flags `-c' and `-e' are mutually exclusive, Regina exits.\n" );
485 exit( 1 );
486 }
487 *compile_to_tokens = 1;
488 break;
489
490 case 'e': /* execute from tokenised file */
491 if ( *compile_to_tokens )
492 {
493 usage( argv[0] );
494 fprintf( stdout, "\nThe flags `-c' and `-e' are mutually exclusive, Regina exits.\n" );
495 exit( 1 );
496 }
497 *execute_from_tokens = 1;
498 break;
499
500 case 'l': /* set locale information, accept empty string */
501 *locale_set = 1;
502 set_locale_info( optarg );
503 break;
504
505 case 'o': /* command-line OPTIONS */
506 {
507 streng *opts = Str_creTSD( optarg );
508 do_options( TSD, TSD->currlevel, opts, 0 );
509 break;
510 }
511
512 case 'h': /* usage */
513 case '?': /* usage */
514 usage( argv[0] );
515 return 0;
516
517 default:
518 usage( argv[0] );
519 fprintf( stdout, "\nThe passed switch `-%c' is unknown, Regina exits.\n", c );
520 exit( 1 );
521 }
522 }
523 return optind;
524 }
525
526 /*
527 * just_compile does a compile step without execution of the assigned input
528 * file (TSD->systeminfo's input_file) to the file named outputname.
529 *
530 * On exit everything has been done.
531 */
532 static void just_compile( tsd_t *TSD, char *outputname )
533 {
534 int len;
535 streng *SrcStr;
536 internal_parser_type ipt;
537 void *instore_buf;
538 unsigned long instore_length;
539 FILE *outfp;
540
541 /*
542 * Read the file
543 */
544 fseek( TSD->systeminfo->input_fp, 0, SEEK_END );
545 len = (int) ftell( TSD->systeminfo->input_fp );
546 rewind( TSD->systeminfo->input_fp );
547
548 SrcStr = Str_makeTSD( len );
549 if ( fread( Str_val( SrcStr ), len, 1, TSD->systeminfo->input_fp ) != 1 )
550 exiterror( ERR_PROG_UNREADABLE, 1, "Unable to read input file" );
551 SrcStr->len = len;
552
553 /*
554 * enter_macro() actually does the tokenising...
555 */
556 ipt = enter_macro( TSD, SrcStr, &instore_buf, &instore_length );
557 fclose( TSD->systeminfo->input_fp );
558
559 outfp = fopen( outputname, "wb" );
560 if ( outfp == NULL )
561 exiterror( ERR_PROG_UNREADABLE, 1, "Unable to open output file for "
562 "writing" );
563 if ( instore_buf == NULL )
564 exiterror( ERR_PROG_UNREADABLE, 1, "Error tokenising input file" );
565 if ( fwrite( instore_buf, instore_length, 1, outfp ) != 1 )
566 exiterror( ERR_PROG_UNREADABLE, 1, "Unable to write contents of output "
567 "file" );
568 fclose( outfp );
569 }
570
571 /*
572 * assign_args sets the current argument list to that one in argv. We count
573 * from the next_arg element to to excluding argc.
574 *
575 * The value is put in TSD->currlevel->args which has to be preeassigned to
576 * NULL.
577 */
578 static void assign_args( tsd_t *TSD, int argc, int next_arg, char **argv )
579 {
580 int i, len;
581 streng *string;
582 paramboxptr args, prev;
583
584 if ( next_arg >= argc )
585 return;
586
587 if ( TSD->systeminfo->invoked == INVO_SUBROUTINE )
588 {
589 prev = NULL;
590 for ( i = next_arg; i < argc; i++ )
591 {
592 args = (paramboxptr)MallocTSD( sizeof( parambox ) );
593 memset( args, 0, sizeof( parambox ) );
594
595 if ( i == next_arg )
596 TSD->currlevel->args = args;
597 else
598 prev->next = args;
599 args->value = Str_cre_TSD( TSD, argv[i] );
600 prev = args;
601 }
602
603 return;
604 }
605
606 for ( i = next_arg, len = 0; i < argc; i++ )
607 len += strlen( argv[i] ) + 1; /* delimiter or terminator */
608
609 TSD->currlevel->args = (paramboxptr)MallocTSD( sizeof( parambox ) );
610 if ( TSD->currlevel->args == NULL )
611 exiterror( ERR_STORAGE_EXHAUSTED, 0 );
612 args = TSD->currlevel->args;
613 memset( args, 0, sizeof(parambox) );
614 args->value = string = Str_makeTSD( len );
615
616 for ( i = next_arg; i < argc; i++ )
617 {
618 string = Str_catstrTSD( string, argv[i] );
619 string->value[string->len++] = ' ';
620 }
621 if ( string && string->len )
622 string->len--;
623 }
624
625 /*
626 * codeFromString translates string into a number and returns it. If this is
627 * not possible, EXIT_SUCCESS is returned.
628 */
629 static int codeFromString( tsd_t *TSD, streng *string )
630 {
631 int error, rcode;
632
633 if ( string )
634 {
635 /* fixes bug 657345 */
636 rcode = streng_to_int( TSD, string, &error );
637 if ( error )
638 rcode = EXIT_SUCCESS;
639 }
640 else
641 rcode = EXIT_SUCCESS;
642
643 return rcode;
644 }
645
646 /*
647 * execute_tokenized executes a tokenized script that has already been assigned
648 * as the input file.
649 *
650 * The arguments must have been assigned, too.
651 *
652 * The return value is the value that the main routine should return to the OS.
653 */
654 static int execute_tokenized( tsd_t *TSD )
655 {
656 void *TinnedTree;
657 unsigned long TinnedTreeLen;
658 streng *command;
659 streng *result;
660 streng *environment;
661 int err,RetCode;
662
663 /*
664 * Read the file into TinnedTree.
665 */
666 fseek( TSD->systeminfo->input_fp, 0, SEEK_END );
667 TinnedTreeLen = ftell( TSD->systeminfo->input_fp );
668 rewind( TSD->systeminfo->input_fp );
669 TinnedTree = MallocTSD( TinnedTreeLen );
670 if ( TinnedTree == NULL )
671 exiterror( ERR_STORAGE_EXHAUSTED, 0 );
672 if ( fread( TinnedTree, TinnedTreeLen, 1, TSD->systeminfo->input_fp ) != 1 )
673 exiterror( ERR_PROG_UNREADABLE, 1, "Unable to read input file" );
674 /*
675 * Don't close the file because the plain text file remains open as well.
676 * This inhibits the deletion or modification on most systems.
677 */
678
679 /*
680 * Check if the file being read is a valid tokenised file.
681 */
682 if ( !IsValidTin( (const external_parser_type *)TinnedTree, TinnedTreeLen ) )
683 exiterror( ERR_PROG_UNREADABLE,
684 1,
685 "The supplied file is not a valid Regina tokenised file" );
686
687 /*
688 * Set program file name and environment. The argument have been assigned
689 * already.
690 */
691 command = Str_dupTSD( TSD->systeminfo->input_file );
692
693 /*
694 * Changed after 3.3RC1: The environment is set to SYSTEM instead of
695 * the externally bound (ENVIR_PIPE) "DEFAULT".
696 */
697 environment = Str_creTSD( "SYSTEM" );
698
699 flush_trace_chars( TSD );
700
701 /*
702 * do_instore() actually does the execution...
703 */
704 result = do_instore( TSD, command, TSD->currlevel->args, environment,
705 &err,
706 0,
707 TinnedTree, TinnedTreeLen,
708 NULL, 0, /* source file contents */
709 NULL,
710 TSD->systeminfo->invoked );
711
712 FreeTSD( TinnedTree );
713 Free_stringTSD( command );
714 Free_stringTSD( environment );
715
716 if ( result )
717 {
718 RetCode = codeFromString( TSD, result );
719 Free_stringTSD( result );
720 }
721 else
722 RetCode = err;
723
724 return RetCode;
725 }
726
727 /*
728 * execute_file executes a plain text script that has already been assigned
729 * as the input file.
730 *
731 * The arguments must have been assigned, too.
732 *
733 * The return value is the value that the main routine should return to the OS.
734 */
735 static int execute_file( tsd_t *TSD )
736 {
737 FILE *fptr = TSD->systeminfo->input_fp;
738 internal_parser_type parsing;
739 streng *string;
740 int RetCode;
741
742 /*
743 * From here we are interpreting...
744 */
745 fetch_file( TSD, fptr ? fptr : stdin, &parsing );
746 if ( fptr )
747 fclose( fptr );
748 TSD->systeminfo->input_fp = NULL;
749
750 if ( parsing.result != 0 )
751 exiterror( ERR_YACC_SYNTAX, 1, parsing.tline );
752 else
753 TSD->systeminfo->tree = parsing;
754
755 #if !defined(MINIMAL) && !defined(VMS) && !defined(DOS) && !defined(_MSC_VER) && !defined(__IBMC__) && !defined(MAC)
756 if ( !fptr )
757 {
758 struct stat buffer;
759 int rc;
760
761 rc = fstat( fileno( stdin ), &buffer );
762 if ( ( rc == 0 ) && S_ISCHR( buffer.st_mode ) )
763 {
764 /*
765 * FIXME. MH and FGC.
766 * When does this happen. Add debugging code to determine this, because
767 * after 2 glasses of rocket fuel it seems silly to have this code!
768 * 13-5-2004.
769 */
770 printf( " \b\b" );
771 fflush( stdout );
772 rewind( stdin );
773 }
774 }
775 #endif
776
777 flush_trace_chars( TSD );
778
779 string = interpret( TSD, TSD->systeminfo->tree.root );
780 RetCode = codeFromString( TSD, string );
781 if ( string )
782 Free_stringTSD( string );
783
784 return RetCode;
785 }
786
787 #ifdef RXLIB
788 # if defined(__LCC__)
789 int __regina_faked_main(int argc,char *argv[])
790 # else
791 int APIENTRY __regina_faked_main(int argc,char *argv[])
792 # endif
793 # define CALL_MAIN __regina_faked_main
794 #else
795 int main(int argc,char *argv[])
796 # define CALL_MAIN main
797 #endif
798 {
799 tsd_t *TSD;
800 int processed;
801 int compile_to_tokens=0;
802 int execute_from_tokens=0;
803 int locale_set=0;
804 int stdinput, rcode;
805 jmp_buf jbuf;
806
807 #ifdef MAC
808 InitCursorCtl( nil );
809 #endif
810
811 if ( argv0 == NULL )
812 argv0 = GetArgv0( argv[0] );
813
814 TSD = GLOBAL_ENTRY_POINT();
815
816 setup_system( TSD, 0 );
817
818 if ( setjmp( jbuf ) )
819 {
820 /*
821 * We may either be jumped normally after an EXIT instruction or after
822 * an error. The first reason means normal continuation, the other
823 * means that we have to do an immediate stop.
824 */
825 if ( !TSD->instore_is_errorfree )
826 {
827 if ( TSD->systeminfo->result )
828 {
829 return atoi( TSD->systeminfo->result->value );
830 }
831 return -1;
832 }
833 else
834 {
835 if ( TSD->systeminfo->result )
836 rcode = codeFromString( TSD, TSD->systeminfo->result );
837 else
838 rcode = EXIT_SUCCESS;
839 }
840 }
841 else
842 {
843 TSD->systeminfo->script_exit = &jbuf;
844
845 processed = check_args( TSD, argc, argv, &compile_to_tokens,
846 &execute_from_tokens, &locale_set );
847
848 if ( processed == 0 )
849 return 0;
850
851 if ( !locale_set )
852 {
853 /*
854 * Check for a comma separated default locale in REGINA_LANG.
855 */
856 char *ptr = getenv( "REGINA_LANG" );
857 if ( ptr )
858 ptr = strchr( ptr, ',' );
859 if ( ptr )
860 set_locale_info( ptr + 1 );
861 }
862
863 if ( processed < argc )
864 {
865 stdinput = 0;
866 TSD->systeminfo->input_file = get_external_routine( TSD,
867 argv[processed], &TSD->systeminfo->input_fp );
868 if ( !TSD->systeminfo->input_file )
869 {
870 TSD->systeminfo->input_file = Str_crestrTSD( argv[processed] );
871 exiterror( ERR_PROG_UNREADABLE, 1, "Program was not found" );
872 }
873 processed++;
874 }
875 else
876 {
877 stdinput = 1;
878 TSD->systeminfo->input_file = Str_crestrTSD( "<stdin>" );
879 TSD->systeminfo->input_fp = NULL;
880 if ( compile_to_tokens )
881 exiterror( ERR_PROG_UNREADABLE, 1, "Too few arguments when "
882 "tokenising. Usage: -c inputfile outputfile" );
883 if ( execute_from_tokens )
884 exiterror( ERR_PROG_UNREADABLE, 1, "Cannot run tokenised code "
885 "from stdin." );
886 }
887 /* RFE #36 - set .FILE reserved name */
888 set_reserved_value( TSD, POOL0_FILE, Str_dupTSD( TSD->systeminfo->input_file ), 0, VFLAG_STR );
889
890 /*
891 * -c switch specified - tokenise the input file before mucking around
892 * with parameters etc.
893 */
894 if ( compile_to_tokens )
895 {
896 if ( processed >= argc )
897 exiterror( ERR_PROG_UNREADABLE, 1, "Too few arguments when "
898 "tokenising. Usage: -c inputfile outputfile" );
899 if ( processed + 1 < argc )
900 exiterror( ERR_PROG_UNREADABLE, 1, "Too many arguments when "
901 "tokenising. Usage: -c inputfile outputfile" );
902
903 just_compile( TSD, argv[processed] );
904 return 0;
905 }
906
907
908 assign_args( TSD, argc, processed, argv );
909 signal_setup( TSD );
910
911 /*
912 * -e switch specified - execute from tokenised code
913 */
914 if ( execute_from_tokens )
915 rcode = execute_tokenized( TSD );
916 else
917 rcode = execute_file( TSD );
918 }
919
920 #if defined(DEBUG) || defined(TRACEMEM)
921 /*
922 * Now do the cleanup. We don't need in real life, but for a proper cleanup
923 * and for debugging aid it is a good idea to track down the whole beast.
924 */
925 purge_stacks( TSD );
926 purge_filetable( TSD );
927 # if defined(FLISTS) && defined(NEW_FLISTS)
928 free_flists();
929 # endif
930
931 # ifdef DYNAMIC
932 /*
933 * Remove all external function package functions
934 * and libraries. Only valid for the DYNAMIC library.
935 */
936 purge_library( TSD );
937 # endif
938
939 # ifdef TRACEMEM
940 if ( TSD->listleakedmemory )
941 listleaked( TSD, MEMTRC_LEAKED );
942 # endif
943
944 TSD->systeminfo->script_exit = NULL; /* cannot be freed, it's on the stack*/
945 killsystem( TSD, TSD->systeminfo );
946 TSD->systeminfo = NULL;
947
948 /*
949 * Remove all memory allocated by the flists internal memory manager.
950 */
951 # ifdef FLISTS
952 purge_flists( TSD );
953 # endif
954
955 #endif /* DEBUG */
956
957 return rcode;
958 }
959
960 /* reexecute_main is possibly called by one of the fork_exec routines.
961 * This functions cleans up some stuff to reexecute without problems.
962 * The most useful thing to be done here is freeing all used memory.
963 * NOTE: usage is always the last thing you should try. Better use
964 * spawn or exec to let a fresh interpreter do the work.
965 */
966 int __regina_reexecute_main(int argc, char **argv)
967 {
968 tsd_t *TSD;
969
970 TSD = __regina_get_tsd(); /* hopefully not multithreading! */
971
972 if (TSD != NULL) /* yes! I don't know what happens on forking */
973 { /* and active multi-threading */
974
975 purge_stacks(TSD); /* see main above for comments */
976 purge_filetable(TSD);
977 #ifdef DYNAMIC
978 purge_library(TSD);
979 #endif
980 #if defined(FLISTS)
981 # if defined(NEW_FLISTS)
982 free_flists();
983 # endif
984 purge_flists(TSD);
985 #endif
986 }
987
988 return(CALL_MAIN(argc, argv));
989 }
990
991 #ifdef TRACEMEM
992 void mark_systeminfo( const tsd_t *TSD )
993 {
994 sysinfo sinfo=NULL ;
995 labelbox *lptr=NULL ;
996 lineboxptr llptr=NULL ;
997
998 for (sinfo=TSD->systeminfo; sinfo; sinfo=sinfo->previous)
999 {
1000 markmemory(sinfo, TRC_SYSINFO) ;
1001 markmemory(sinfo->input_file, TRC_SYSINFO) ;
1002 markmemory(sinfo->environment, TRC_SYSINFO) ;
1003 markmemory(sinfo->callstack, TRC_SYSINFO) ;
1004
1005 markvariables( TSD, sinfo->currlevel0 ) ;
1006 marksource( sinfo->tree.first_source_line ) ;
1007 /* FGC, FIXME: rewrite this: marksubtree( sinfo->tree.root ) ; */
1008
1009 for (lptr=sinfo->tree.first_label; lptr; lptr=lptr->next )
1010 {
1011 markmemory( lptr, TRC_SYSINFO ) ;
1012 }
1013
1014 for (llptr=sinfo->tree.first_source_line; llptr; llptr=llptr->next )
1015 {
1016 markmemory( llptr, TRC_SYSINFO ) ;
1017 markmemory( llptr->line, TRC_SYSINFO ) ;
1018 }
1019 }
1020
1021 markmemory( TSD->mem_tsd, TRC_SYSINFO );
1022 markmemory( TSD->var_tsd, TRC_SYSINFO );
1023 markmemory( TSD->stk_tsd, TRC_SYSINFO );
1024 markmemory( TSD->fil_tsd, TRC_SYSINFO );
1025 markmemory( TSD->itp_tsd, TRC_SYSINFO );
1026 markmemory( TSD->tra_tsd, TRC_SYSINFO );
1027 markmemory( TSD->err_tsd, TRC_SYSINFO );
1028 if (TSD->vms_tsd )
1029 markmemory( TSD->vms_tsd, TRC_SYSINFO );
1030 markmemory( TSD->bui_tsd, TRC_SYSINFO );
1031 if (TSD->vmf_tsd )
1032 markmemory( TSD->vmf_tsd, TRC_SYSINFO );
1033 markmemory( TSD->lib_tsd, TRC_SYSINFO );
1034 if (TSD->rex_tsd )
1035 markmemory( TSD->rex_tsd, TRC_SYSINFO );
1036 markmemory( TSD->shl_tsd, TRC_SYSINFO );
1037 markmemory( TSD->mat_tsd, TRC_SYSINFO );
1038 if (TSD->cli_tsd )
1039 markmemory( TSD->cli_tsd, TRC_SYSINFO );
1040 markmemory( TSD->arx_tsd, TRC_SYSINFO );
1041 markmemory( TSD->mt_tsd, TRC_SYSINFO );
1042 }
1043 #endif
1044
1045
1046 sysinfobox *creat_sysinfo( const tsd_t *TSD, streng *envir )
1047 {
1048 sysinfobox *sinfo;
1049
1050 sinfo = (sysinfobox *)MallocTSD( sizeof(sysinfobox) );
1051 sinfo->environment = envir;
1052 sinfo->tracing = DEFAULT_TRACING;
1053 sinfo->interactive = DEFAULT_INT_TRACING;
1054 sinfo->previous = NULL;
1055 sinfo->invoked = INVO_COMMAND;
1056 sinfo->input_file = NULL;
1057 sinfo->input_fp = NULL;
1058 sinfo->script_exit = NULL ;
1059 sinfo->hooks = 0;
1060 sinfo->callstack = (nodeptr *)MallocTSD( sizeof( nodeptr ) * 10 );
1061 sinfo->result = NULL;
1062 sinfo->cstackcnt = 0;
1063 sinfo->cstackmax = 10;
1064 sinfo->trace_override = 0;
1065 sinfo->ctrlcounter = 0;
1066 memset( &sinfo->tree, 0, sizeof( sinfo->tree ) );
1067
1068 return sinfo;
1069 }
1070 /*
1071 * The following two functions are used to set and retrieve the value of
1072 * the global TSD
1073 */
1074 void setGlobalTSD( const tsd_t *TSD)
1075 {
1076 __regina_global_TSD = TSD;
1077 }
1078
1079 const tsd_t *getGlobalTSD( void )
1080 {
1081 return __regina_global_TSD;
1082 }
1083
1084 #if !defined(RXLIB) && !defined(VMS)
1085
1086 static void NoAPI( void )
1087 {
1088 fprintf (stderr, "Warning: SAA API not compiled into interpreter\n" ) ;
1089 }
1090
1091 int hookup( tsd_t *TSD, int dummy )
1092 {
1093 /* This should never happen, if we don't have support for SAA API,
1094 * Then we should never get a system exit!
1095 */
1096 assert( 0 ) ;
1097 return 1 ; /* to keep compiler happy */
1098 }
1099 int hookup_input( tsd_t *TSD, int dummy1, streng **dummy2 )
1100 {
1101 /* This should never happen, if we don't have support for SAA API,
1102 * Then we should never get a system exit!
1103 */
1104 assert( 0 ) ;
1105 return 1 ; /* to keep compiler happy */
1106 }
1107 int hookup_input_output( tsd_t *TSD, int dummy1, const streng *dummy2, streng **dummy3 )
1108 {
1109 /* This should never happen, if we don't have support for SAA API,
1110 * Then we should never get a system exit!
1111 */
1112 assert( 0 ) ;
1113 return 1 ; /* to keep compiler happy */
1114 }
1115 int hookup_output( tsd_t *TSD, int dummy1, const streng *dummy2 )
1116 {
1117 /* This should never happen, if we don't have support for SAA API,
1118 * Then we should never get a system exit!
1119 */
1120 assert( 0 ) ;
1121 return 1 ; /* to keep compiler happy */
1122 }
1123 int hookup_output2( tsd_t *TSD, int dummy1, const streng *dummy2, const streng *dummy3 )
1124 {
1125 /* This should never happen, if we don't have support for SAA API,
1126 * Then we should never get a system exit!
1127 */
1128 assert( 0 ) ;
1129 return 1 ; /* to keep compiler happy */
1130 }
1131
1132 static void Exit( const tsd_t *TSD )
1133 {
1134 /*
1135 * cheat about the const, we go away anyway :-)
1136 */
1137 jump_interpreter_exit( ( tsd_t * ) TSD, 1 );
1138 }
1139
1140 streng *call_unknown_external( tsd_t *TSD, const streng *dummy1, cparamboxptr dummy2, char dummy3 )
1141 {
1142 NoAPI();
1143 Exit( TSD ) ;
1144 return NULL;
1145 }
1146
1147 streng *call_known_external( tsd_t *TSD, const struct entry_point *dummy1, cparamboxptr dummy2, char dummy3 )
1148 {
1149 NoAPI();
1150 Exit( TSD ) ;
1151 return NULL;
1152 }
1153
1154
1155 streng *SubCom( tsd_t *TSD, const streng *dummy1, const streng *dummy2, int *dummy3 )
1156 {
1157 NoAPI();
1158 Exit( TSD ) ;
1159 return NULL;
1160 }
1161
1162 int IfcHaveFunctionExit(const tsd_t *TSD)
1163 {
1164 return(0);
1165 }
1166
1167 #endif
1168
1169 static void usage( char *argv0 )
1170 {
1171 fprintf( stdout, "\n%s: %s (%d bit). All rights reserved.\n", argv0, PARSE_VERSION_STRING, REGINA_BITS );
1172 fprintf( stdout,"Regina is distributed under the terms of the GNU Library General Public License \n" );
1173 fprintf( stdout,"and comes with NO WARRANTY. See the file COPYING-LIB for details.\n" );
1174 fprintf( stdout,"\nTo run a Rexx program:\n" );
1175 fprintf( stdout,"%s [switches] [program] [arguments...]\n", argv0 );
1176 fprintf( stdout,"where switches are:\n\n" );
1177 fprintf( stdout," --help, -h show this message\n" );
1178 fprintf( stdout," --version[=option], -v[option] display various Regina details and exit\n" );
1179 fprintf( stdout," option one of:\n" );
1180 fprintf( stdout," no option - full Regina version\n" );
1181 fprintf( stdout," a - ADDONS directory\n" );
1182 fprintf( stdout," b - bits; 32 or 64\n" );
1183 fprintf( stdout," p - external packages statically linked\n" );
1184 fprintf( stdout," v - version only\n" );
1185 fprintf( stdout," --restricted, -r run Regina in \"safe\" mode\n" );
1186 fprintf( stdout," --trace[=char], -t[char] set TRACE to any valid TRACE character - default A\n" );
1187 fprintf( stdout," --interactive[=char], -i[char] set TRACE to any valid TRACE character and run interactively - default A\n" );
1188 fprintf( stdout," --args, -a pass command line to Rexx program as separate arguments\n");
1189 fprintf( stdout," --pause, -p pause after execution (Win32 only)\n");
1190 fprintf( stdout," --locale=locale, -llocale use the system's default charset or a supplied one\n");
1191 fprintf( stdout," --options=OPTIONS, -oOPTIONS specify OPTIONS in same format as OPTIONS instruction\n");
1192 fprintf( stdout," --compile, -c see \"To tokenise a Rexx program:\" below\n");
1193 fprintf( stdout," --execute, -e see \"To execute a tokenised file:\" below\n");
1194 fprintf( stdout,"\"program\" is the file containing Rexx code to execute\n");
1195 fprintf( stdout,"\"arguments\" are arguments passed to program\n");
1196 fprintf( stdout,"\nTo tokenise a Rexx program:\n" );
1197 fprintf( stdout,"%s -c program(input) tokenisedfile(output)\n", argv0 );
1198 fprintf( stdout,"\nTo execute a tokenised file:\n" );
1199 fprintf( stdout,"%s -e tokenisedfile [arguments...]\n", argv0 );
1200 fprintf( stdout,"\nIf you intend using external functions (using RxFuncAdd) you need to run the \"regina\" executable\n" );
1201 fflush( stdout );
1202 }