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