Added a signal handler for SIGFPE
[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 <stdio.h>
22 #include <gsl/gsl_errno.h>
23 #include "main.h"
24 #include "cmdline.h"
25 #include "command.h"
26 #include "dictionary.h"
27 #include "error.h"
28 #include "getline.h"
29 #include "glob.h"
30 #include "lexer.h"
31 #include "output.h"
32 #include "settings.h"
33 #include "var.h"
34 #include <signal.h>
35
36 #include <stdlib.h>
37
38 #include "debug-print.h"
39
40 static void parse_script (void) NO_RETURN;
41 static void handle_error (int code);
42 static int execute_command (void);
43
44 /* argv[0] with stripped leading directories. */
45 char *pgmname;
46
47 /* Whether FINISH. has been executed. */
48 int finished;
49
50 /* The current date in the form DD MMM YYYY. */
51 char curdate[12];
52
53
54 /* If a segfault happens, issue a message to that effect and halt */
55 void bug_handler(int sig);
56
57 /* Whether we're dropping down to interactive mode immediately because
58    we hit end-of-file unexpectedly (or whatever). */
59 int start_interactive;
60
61
62 /* Program entry point. */
63 int
64 main (int argc, char **argv)
65 {
66   signal (SIGSEGV, bug_handler);
67   signal (SIGFPE, bug_handler);
68
69   gsl_set_error_handler_off();
70
71   /* Initialization. */
72   if (!outp_init ())
73     err_hcf (0);
74   init_glob (argc, argv);
75   parse_command_line (argc, argv);
76   if (!outp_read_devices ())
77     msg (FE, _("Error initializing output drivers."));
78
79   lex_init ();
80
81   /* Execution. */
82   parse_script ();
83
84   /* Should never be reached */
85   return (-1);
86 }
87
88 /* Parses the entire script. */
89 static void
90 parse_script (void)
91 {
92   while (!finished)
93     {
94       err_check_count ();
95       handle_error (execute_command ());
96     }
97
98   err_hcf (err_error_count==0);
99 }
100
101 /* Parse and execute a command, returning its return code. */
102 static int
103 execute_command (void)
104 {
105   int result;
106   
107   /* Read the command's first token.
108      We may hit end of file.
109      If so, give the line reader a chance to proceed to the next file.
110      End of file is not handled transparently since the user may want
111      the dictionary cleared between files. */
112   getl_prompt = GETL_PRPT_STANDARD;
113   for (;;)
114     {
115       lex_get ();
116       if (token != T_STOP)
117         break;
118
119       if (!getl_perform_delayed_reset ())
120         err_hcf (err_error_count==0);
121     }
122
123   /* Parse the command. */
124   getl_prompt = GETL_PRPT_CONTINUATION;
125   result =  cmd_parse ();
126  
127   /* Unset the /ALGORITHM subcommand if it was used */
128   unset_cmd_algorithm ();
129
130   /* Clear any auxiliary data from the dictionary. */
131   dict_clear_aux (default_dict);
132
133   return result;
134 }
135
136 /* Print an error message corresponding to the command return code
137    CODE. */
138 static void
139 handle_error (int code)
140 {
141   switch (code)
142     {
143     case CMD_SUCCESS:
144       return;
145           
146     case CMD_FAILURE:
147       msg (SW,  _("This command not executed."));
148       break;
149
150     case CMD_PART_SUCCESS_MAYBE:
151       msg (SW, _("Skipping the rest of this command.  Part of "
152                  "this command may have been executed."));
153       break;
154                   
155     case CMD_PART_SUCCESS:
156       msg (SW, _("Skipping the rest of this command.  This "
157                  "command was fully executed up to this point."));
158       break;
159
160     case CMD_TRAILING_GARBAGE:
161       msg (SW, _("Trailing garbage was encountered following "
162                  "this command.  The command was fully executed "
163                  "to this point."));
164       break;
165
166     default:
167       assert (0);
168     }
169
170   if (getl_reading_script)
171     {
172       err_break ();
173       while (token != T_STOP && token != '.')
174         lex_get ();
175     }
176   else 
177     {
178       msg (SW, _("The rest of this command has been discarded."));
179       lex_discard_line (); 
180     }
181 }
182
183
184
185 /* If a segfault happens, issue a message to that effect and halt */
186 void 
187 bug_handler(int sig UNUSED)
188 {
189   switch (sig) 
190     {
191     case SIGFPE:
192       request_bug_report_and_abort("Floating Point Exception");
193       break;
194     case SIGSEGV:
195       request_bug_report_and_abort("Segmentation Violation");
196       break;
197     default:
198       request_bug_report_and_abort("");
199       break;
200     }
201 }