Changed DFM from open-at-first-access to explicit-open. Before,
[pspp-builds.git] / src / main.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18    02111-1307, USA. */
19
20 #include <config.h>
21 #include "main.h"
22 #include <assert.h>
23 #include <stdio.h>
24 #include "cmdline.h"
25 #include "command.h"
26 #include "error.h"
27 #include "getline.h"
28 #include "glob.h"
29 #include "lexer.h"
30 #include "output.h"
31 #include "version.h"
32 #include <signal.h>
33
34 #include <stdlib.h>
35
36 #include "debug-print.h"
37
38 static void parse_script (void) NO_RETURN;
39 static void handle_error (int code);
40 static int execute_command (void);
41
42 /* argv[0] with stripped leading directories. */
43 char *pgmname;
44
45 /* Whether FINISH. has been executed. */
46 int finished;
47
48 /* The current date in the form DD MMM YYYY. */
49 char curdate[12];
50
51
52 /* If a segfault happens, issue a message to that effect and halt */
53 void bug_handler(int sig);
54
55 /* Whether we're dropping down to interactive mode immediately because
56    we hit end-of-file unexpectedly (or whatever). */
57 int start_interactive;
58
59 /* Program entry point. */
60 int
61 main (int argc, char **argv)
62 {
63   signal (SIGSEGV, bug_handler);
64
65   /* Initialization. */
66   if (!outp_init ())
67     err_hcf (0);
68   init_glob (argc, argv);
69   parse_command_line (argc, argv);
70   if (!outp_read_devices ())
71     msg (FE, _("Error initializing output drivers."));
72
73   lex_init ();
74
75   /* Execution. */
76   parse_script ();
77
78   /* Should never be reached */
79   return (-1);
80 }
81
82 /* Parses the entire script. */
83 static void
84 parse_script (void)
85 {
86   while (!finished)
87     {
88       err_check_count ();
89       handle_error (execute_command ());
90     }
91
92   err_hcf (err_error_count==0);
93 }
94
95 /* Parse and execute a command, returning its return code. */
96 static int
97 execute_command (void)
98 {
99   /* Read the command's first token.
100      We may hit end of file.
101      If so, give the line reader a chance to proceed to the next file.
102      End of file is not handled transparently since the user may want
103      the dictionary cleared between files. */
104   getl_prompt = GETL_PRPT_STANDARD;
105   for (;;)
106     {
107       lex_get ();
108       if (token != T_STOP)
109         break;
110
111       if (!getl_perform_delayed_reset ())
112         err_hcf (err_error_count==0);
113     }
114
115   /* Parse the command. */
116   getl_prompt = GETL_PRPT_CONTINUATION;
117   return cmd_parse ();
118 }
119
120 /* Print an error message corresponding to the command return code
121    CODE. */
122 static void
123 handle_error (int code)
124 {
125   switch (code)
126     {
127     case CMD_SUCCESS:
128       return;
129           
130     case CMD_FAILURE:
131       msg (SW,  _("This command not executed."));
132       break;
133
134     case CMD_PART_SUCCESS_MAYBE:
135       msg (SW, _("Skipping the rest of this command.  Part of "
136                  "this command may have been executed."));
137       break;
138                   
139     case CMD_PART_SUCCESS:
140       msg (SW, _("Skipping the rest of this command.  This "
141                  "command was fully executed up to this point."));
142       break;
143
144     case CMD_TRAILING_GARBAGE:
145       msg (SW, _("Trailing garbage was encountered following "
146                  "this command.  The command was fully executed "
147                  "to this point."));
148       break;
149
150     default:
151       assert (0);
152     }
153
154   if (getl_reading_script)
155     {
156       err_break ();
157       while (token != T_STOP && token != '.')
158         lex_get ();
159     }
160   else
161     lex_discard_line ();
162 }
163
164
165
166 /* If a segfault happens, issue a message to that effect and halt */
167 void 
168 bug_handler(int sig UNUSED)
169 {
170   fprintf(stderr,
171           "******************************************************************\n"
172           "You have discovered a bug in PSPP.\n\n"
173           "  Please report this, by sending "
174           "an email to " PACKAGE_BUGREPORT ",\n"
175           "explaining what you were doing when this happened, and including\n"
176           "a sample of your input file which caused it.\n");
177
178   fprintf(stderr,
179           "Also, please copy the following lines into your bug report:\n\n"
180           "bare_version:        %s\n" 
181           "version:             %s\n"
182           "stat_version:        %s\n"
183           "host_system:         %s\n"
184           "build_system:        %s\n"
185           "default_config_path: %s\n"
186           "include_path:        %s\n"
187           "groff_font_path:     %s\n"
188           "locale_dir:          %s\n"
189           "******************************************************************\n",
190           bare_version,         
191           version,
192           stat_version,
193           host_system,        
194           build_system,
195           default_config_path,
196           include_path, 
197           groff_font_path,
198           locale_dir);     
199
200   exit(-1);
201 }
202