16058e45d33913dda0224d721fc7e188409e93a9
[pspp-builds.git] / src / ui / terminal / 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., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21
22 #include "command-line.h"
23 #include "msg-ui.h"
24 #include "progname.h"
25 #include "read-line.h"
26
27 #include <data/dictionary.h>
28 #include <data/file-handle-def.h>
29 #include <data/file-name.h>
30 #include <data/settings.h>
31 #include <data/variable.h>
32 #include <gsl/gsl_errno.h>
33 #include <language/command.h>
34 #include <language/lexer/lexer.h>
35 #include <language/line-buffer.h>
36 #include <libpspp/compiler.h>
37 #include <libpspp/message.h>
38 #include <libpspp/version.h>
39 #include <math/random.h>
40 #include <output/output.h>
41 #include <signal.h>
42 #include <stdio.h>
43
44
45 #if HAVE_FPU_CONTROL_H
46 #include <fpu_control.h>
47 #endif
48
49 #if HAVE_LOCALE_H
50 #include <locale.h>
51 #endif
52
53 #if HAVE_FENV_H
54 #include <fenv.h>
55 #endif
56
57 #include "gettext.h"
58 #define _(msgid) gettext (msgid)
59
60 #include <stdlib.h>
61
62 static void i18n_init (void);
63 static void fpu_init (void);
64 static void handle_error (int code);
65 static int execute_command (void);
66 static void terminate (bool success) NO_RETURN;
67
68 /* If a segfault happens, issue a message to that effect and halt */
69 void bug_handler(int sig);
70
71 /* Handle quit/term/int signals */
72 void interrupt_handler(int sig);
73
74
75 /* Program entry point. */
76 int
77 main (int argc, char **argv)
78 {
79   signal (SIGSEGV, bug_handler);
80   signal (SIGFPE, bug_handler);
81   signal (SIGINT, interrupt_handler);
82
83   set_program_name ("pspp");
84   i18n_init ();
85   fpu_init ();
86   gsl_set_error_handler_off ();
87
88   outp_init ();
89   msg_ui_init ();
90   fn_init ();
91   fh_init ();
92   getl_initialize ();
93   readln_initialize ();
94   settings_init ();
95   random_init ();
96
97   default_dict = dict_create ();
98
99   if (parse_command_line (argc, argv)) 
100     {
101       outp_read_devices ();
102       lex_init ();
103
104       for (;;)
105         {
106           int retval;
107
108           check_msg_count ();
109
110           retval = execute_command ();
111           if (retval == CMD_EOF)
112             break;
113           if (retval != CMD_SUCCESS)
114             handle_error (retval);
115         }
116     }
117   
118   terminate (!any_errors ());
119 }
120
121 /* Parse and execute a command, returning its return code. */
122 static int
123 execute_command (void)
124 {
125   int result;
126   
127   /* Read the command's first token.  
128      The first token is part of the first line of the command. */
129   getl_set_prompt_style (GETL_PROMPT_FIRST);
130   lex_get ();
131   if (token == T_STOP)
132     return CMD_EOF;
133
134   /* Parse the command.
135      Any lines read after the first token must be continuation
136      lines. */
137   getl_set_prompt_style (GETL_PROMPT_LATER);
138   result = cmd_parse ();
139  
140   /* Unset the /ALGORITHM subcommand if it was used */
141   unset_cmd_algorithm ();
142
143   /* Clear any auxiliary data from the dictionary. */
144   dict_clear_aux (default_dict);
145
146   return result;
147 }
148
149 /* Print an error message corresponding to the command return code
150    CODE. */
151 static void
152 handle_error (int code)
153 {
154   if (code == CMD_CASCADING_FAILURE && !getl_is_interactive ()) 
155     {
156       msg (SW, _("This command not executed.  Stopping here "
157                  "to avoid cascading failures."));
158       getl_abort_noninteractive ();
159       return;
160     }
161
162   switch (code)
163     {
164     case CMD_FAILURE:
165     case CMD_CASCADING_FAILURE:
166       msg (SW,  _("This command not executed."));
167       break;
168
169     case CMD_PART_SUCCESS_MAYBE:
170       msg (SW, _("Skipping the rest of this command.  Part of "
171                  "this command may have been executed."));
172       break;
173                   
174     case CMD_PART_SUCCESS:
175       msg (SW, _("Skipping the rest of this command.  This "
176                  "command was fully executed up to this point."));
177       break;
178
179     case CMD_TRAILING_GARBAGE:
180       msg (SW, _("Trailing garbage was encountered following "
181                  "this command.  The command was fully executed "
182                  "to this point."));
183       break;
184
185     default:
186       abort ();
187     }
188
189   if (!getl_is_interactive ())
190     {
191       while (token != T_STOP && token != '.')
192         lex_get ();
193     }
194   else 
195     {
196       msg (SW, _("The rest of this command has been discarded."));
197       lex_discard_line (); 
198     }
199 }
200 \f
201 static void
202 i18n_init (void) 
203 {
204 #if ENABLE_NLS
205 #if HAVE_LC_MESSAGES
206   setlocale (LC_MESSAGES, "");
207 #endif
208   setlocale (LC_MONETARY, "");
209   bindtextdomain (PACKAGE, locale_dir);
210   textdomain (PACKAGE);
211 #endif /* ENABLE_NLS */
212 }
213
214 static void
215 fpu_init (void) 
216 {
217 #if HAVE_FEHOLDEXCEPT
218   fenv_t foo;
219   feholdexcept (&foo);
220 #elif HAVE___SETFPUCW && defined(_FPU_IEEE)
221   __setfpucw (_FPU_IEEE);
222 #endif
223 }
224
225 /* If a segfault happens, issue a message to that effect and halt */
226 void 
227 bug_handler(int sig)
228 {
229   switch (sig) 
230     {
231     case SIGFPE:
232       request_bug_report_and_abort("Floating Point Exception");
233       break;
234     case SIGSEGV:
235       request_bug_report_and_abort("Segmentation Violation");
236       break;
237     default:
238       request_bug_report_and_abort("");
239       break;
240     }
241 }
242
243 void 
244 interrupt_handler(int sig UNUSED)
245 {
246   terminate (false);
247 }
248
249
250 /* Terminate PSPP.  SUCCESS should be true to exit successfully,
251    false to exit as a failure.  */
252 static void
253 terminate (bool success)
254 {
255   static bool terminating = false;
256   if (!terminating) 
257     {
258       terminating = true;
259
260       cancel_transformations ();
261       dict_destroy (default_dict);
262
263       random_done ();
264       settings_done ();
265       fh_done ();
266       lex_done ();
267       getl_uninitialize ();
268       readln_uninitialize ();
269
270       outp_done ();
271       msg_ui_done ();
272     }
273   exit (success ? EXIT_SUCCESS : EXIT_FAILURE);
274 }