/* Copyright K. Mitchell 1995. This file has been generated by a program and is not intended to be read by a human. Run the file occam.w through nuweb. This produces a file called occam.tex. Typeset this file using LaTeX and then preview the resulting .dvi file, or just print it out. */ #include #include #include typedef int bool; #ifndef FALSE #define FALSE 0 #endif #ifndef TRUE #define TRUE 1 #endif #include #include #include #include #include "pool.h" #include "symbol_table.h" #include "lexer.h" #include "parser.h" #include "interpreter.h" #include "occam.h" int CHANNEL_BLOCK_SIZE = 1; /* for interpreter */ char *command_name = NULL; code program_code = NULL; int pool_flag = FALSE; int run_flag = TRUE; int print_flag = FALSE; int tracing_flag = FALSE; int debug_flag = FALSE; int waiting_time = 0; int max_slice = 10; int input_fd = 0; /* stdin as default */ int input_buffered = TRUE; /* buffered by default */ struct termios oldterm; void disable_buffering() { struct termios term; tcgetattr(0, &term); oldterm = term; term.c_lflag &= ~ICANON; term.c_cc[VMIN] = 0; term.c_cc[VTIME] = waiting_time; tcsetattr(0, TCSANOW, &term); input_buffered = FALSE; atexit(&reset_terminal); } void reset_terminal() { tcsetattr(0, TCSANOW, &oldterm); } void error(char *type, char *format, ...) { int i; va_list arg; va_start(arg,format); fprintf(stderr, "%s: %s", command_name, type); if (line_number) fprintf(stderr, " on line %d.\n", line_number); else fprintf(stderr, ".\n"); for (i = strlen(command_name)+2; i>0; i--) fprintf(stderr, " "); vfprintf(stderr, format, arg); va_end(arg); exit(EXIT_FAILURE); } void leave_fpe(int sig) { error("Runtime error", "Floating point exception.\n"); } void leave_segv(int sig) { error("Runtime error", "Segmentation violation.\n"); } int main( int argc, char *argv[] ) { int arg = 1; /* index into argv */ (void) signal(SIGFPE, leave_fpe); (void) signal(SIGSEGV, leave_segv); command_name = argv[0]; while (arg < argc) { char *s = argv[arg]; if (*s++ == '-') { { char c = *s++; while (c) { switch (c) { case 'p': pool_flag = TRUE; break; case 'r': run_flag = FALSE; break; case 'c': print_flag = TRUE; break; case 't': tracing_flag = TRUE; break; case 'd': debug_flag = TRUE; break; case 'w': waiting_time = atoi(argv[++arg]); break; case 's': max_slice = atoi(argv[++arg]); break; default: fprintf(stderr, "%s: Unexpected command line arguments.\n" "Usage is: %s [-prctd] [-w time] [-s num] sourcefile [inputfile]\n", command_name, command_name); exit(EXIT_FAILURE); } c = *s++; } } arg++; } else break; } { FILE *file; int num_files = argc - arg; if (num_files == 0 || num_files > 2) { fprintf(stderr, "%s: Unexpected command line arguments.\n" "Usage is: %s [-prctd] [-w time] [-s num] sourcefile [inputfile]\n", command_name, command_name); exit(EXIT_FAILURE); } file = fopen(argv[arg],"r"); if (file == NULL) error("Fatal error", "Cannot open file %s.\n",argv[arg]); initialise_lexer(file); if (num_files == 2) { input_fd = open(argv[arg+1],O_RDONLY); if (input_fd == -1) error("Fatal error", "Cannot open file %s.\n",argv[arg+1]); } else if (isatty(input_fd)) disable_buffering(); } initialise_symbol_table(); yyparse(); /* code placed in global program_code */ clear_symbol_table(); /* to reclaim storage */ line_number = 0; /* to disable spurious printing of line numbers in run-time errors */ if (print_flag) print_code(program_code); if (run_flag) interpret(build_ccode(io_code(), program_code, END)); return(EXIT_SUCCESS); }