1 /*****************************************************************************
2   FILE           : $Source: /projects/higgs1/SNNS/CVS/SNNS/tools/sources/batchman.c,v $
3   SHORTNAME      : batchman
4   SNNS VERSION   : 4.2
5 
6   PURPOSE        : SNNS batch interpreter main module
7                    initialization, command line argument handling, main()
8   NOTES          :
9 
10   AUTHOR         : Jens Wieland
11   DATE           :
12 
13   CHANGED BY     :
14   RCS VERSION    : $Revision: 1.8 $
15   LAST CHANGE    : $Date: 1998/03/03 16:13:18 $
16 
17     Copyright (c) 1990-1995  SNNS Group, IPVR, Univ. Stuttgart, FRG
18     Copyright (c) 1996-1998  SNNS Group, WSI, Univ. Tuebingen, FRG
19 
20 ******************************************************************************/
21 #include <config.h>
22 
23 #include <stdlib.h>
24 #include <stdio.h>
25 #ifdef HAVE_UNISTD_H
26 #include <unistd.h>                   /* for getopt() */
27 #endif
28 
29 #include "batchman.ph"
30 #include "symtab.h"                   /* for st_init() */
31 #include "ictab.h"                    /* for run() */
32 #include "scan1.h"                    /* scanner 1 declarations */
33 #include "glob_typ.h"         /* SNNS-Kernel: Global Datatypes and Constants */
34 #include "error.h"
35 
36 /*****************************************************************************
37   FUNCTION : print_help
38 
39   PURPOSE  : prints helpful information to stdout
40   RETURNS  :
41   NOTES    :
42 
43   UPDATE   :
44 ******************************************************************************/
print_help(void)45 void print_help(void)
46 {
47   printf("This is batchman, the SNNS Batch Interpreter.\n");
48   printf("Type Ctrl-D in interactive mode to start the interpreter.\n");
49   printf("Options:\n");
50   printf("-f <file> : read input from <file>\n");
51   printf("-l <file> : (logfile) write output to <file>\n");
52   printf("-p        : parse-only; just check the syntax, do not execute\n");
53   printf("-q        : quiet mode; neither warnings nor errors are printed\n");
54   printf("-s        : suppress warnings\n");
55   printf("-h        : print this help\n");
56   printf("\n");
57   exit(0);
58 }
59 
60 
61 /*****************************************************************************
62   FUNCTION : read_cmdline
63 
64   PURPOSE  : reads the command line
65   RETURNS  :
66   NOTES    :
67 
68   UPDATE   :
69 ******************************************************************************/
read_cmdline(int argc,char ** argv)70 void read_cmdline(int argc, char **argv)
71 {
72   int option;
73   extern char *optarg;
74   extern int opterr, optind;
75 
76   opterr = 0;      /* don't let getopt print errors */
77 
78   input_is_file = 0;
79   message_flag = 0;
80   warn_flag = 0;
81 
82   while ((option = getopt(argc, argv, "f:l:pqsh")) != -1)
83   switch(option)
84   {
85     case 'f': input_file_name = optarg; input_is_file = 1; break;
86     case 'l': log_file_name = optarg; break;
87     case 'p': parse_only_flag = 1; break;
88     case 'q': message_flag = 1; break;
89     case 's': warn_flag = 1; break;
90     case 'h': print_help(); break;
91     case '?': fprintf(stderr,"*** Batchman error: illegal option\n");
92               exit(1);
93   }
94 
95   if (optind != argc)
96   {
97     fprintf(stderr,
98 	    "*** Batchman error: unexpected command line argument(s)\n");
99     exit(1);
100   }
101 }
102 
103 
104 /*****************************************************************************
105   FUNCTION : open_files
106 
107   PURPOSE  : opens input and log files, if names are given on command line
108   RETURNS  :
109   NOTES    :
110 
111   UPDATE   :
112 ******************************************************************************/
open_files(void)113 void open_files(void)
114 {
115   /* open log file, if a name is given: */
116  if (log_file_name == NULL) log_file = stdout;
117  else
118  if (!(log_file = fopen(log_file_name, "w")))
119  {
120    fprintf(stderr,
121 	   "*** Batchman error: opening file %s failed\n", log_file_name);
122    exit(1);
123  }
124 
125  /* use stdin as input, if no filename is given: */
126  if (input_file_name == NULL)
127  {
128    printf("SNNS Batch Interpreter V1.0. Type batchman -h for help.\n");
129    printf("No input file specified, reading input from stdin.\n");
130    printf("batchman> ");
131  }
132  else
133  /* redirect scanner input to input file: */
134  if (!(yyzin = fopen(input_file_name, "r")))
135  {
136    fprintf(stderr,
137 	   "*** Batchman error: opening file %s failed\n", input_file_name);
138    exit(1);
139  }
140 }
141 
142 
143 /*****************************************************************************
144   FUNCTION : main
145 
146   PURPOSE  : reads the command line, starts parsing and runs
147              the interpreter loop
148   RETURNS  : exit value 0 on success, 1 or more on errors
149   NOTES    :
150 
151   UPDATE   :
152 ******************************************************************************/
main(int argc,char ** argv)153 int main(int argc, char **argv)
154 {
155   yyzdebug = 0;                /* set parser debug mode to off */
156 
157   read_cmdline(argc, argv);     /* have a look at the command line */
158   open_files();                 /* open some files */
159 
160   st_init();                    /* Initialize symbol table ... */
161   ictab_init();                 /* Initialize intermediate code table ... */
162 
163   signal_init();                /* Initialize the signal handler */
164 
165   yyzparse();                   /* do the parse ... */
166   if (!parse_only_flag) run();  /* and run the interpreter loop. */
167 
168   return 0;
169 }
170 
171