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