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