Get rid of src/libpspp/debug-print.h and all its users. (There were
[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 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
63 /* If a segfault happens, issue a message to that effect and halt */
64 void bug_handler(int sig);
65
66 /* Handle quit/term/int signals */
67 void interrupt_handler(int sig);
68
69 void terminate (bool success);
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   abort ();
115 }
116
117 /* Parse and execute a command, returning its return code. */
118 static int
119 execute_command (void)
120 {
121   int result;
122   
123   /* Read the command's first token.  
124      The first token is part of the first line of the command. */
125   getl_set_prompt_style (GETL_PROMPT_FIRST);
126   lex_get ();
127   if (token == T_STOP)
128     return CMD_EOF;
129
130   /* Parse the command.
131      Any lines read after the first token must be continuation
132      lines. */
133   getl_set_prompt_style (GETL_PROMPT_LATER);
134   result = cmd_parse ();
135  
136   /* Unset the /ALGORITHM subcommand if it was used */
137   unset_cmd_algorithm ();
138
139   /* Clear any auxiliary data from the dictionary. */
140   dict_clear_aux (default_dict);
141
142   return result;
143 }
144
145 /* Print an error message corresponding to the command return code
146    CODE. */
147 static void
148 handle_error (int code)
149 {
150   if (code == CMD_CASCADING_FAILURE && !getl_is_interactive ()) 
151     {
152       msg (SW, _("This command not executed.  Stopping here "
153                  "to avoid cascading failures."));
154       getl_abort_noninteractive ();
155       return;
156     }
157
158   switch (code)
159     {
160     case CMD_FAILURE:
161     case CMD_CASCADING_FAILURE:
162       msg (SW,  _("This command not executed."));
163       break;
164
165     case CMD_PART_SUCCESS_MAYBE:
166       msg (SW, _("Skipping the rest of this command.  Part of "
167                  "this command may have been executed."));
168       break;
169                   
170     case CMD_PART_SUCCESS:
171       msg (SW, _("Skipping the rest of this command.  This "
172                  "command was fully executed up to this point."));
173       break;
174
175     case CMD_TRAILING_GARBAGE:
176       msg (SW, _("Trailing garbage was encountered following "
177                  "this command.  The command was fully executed "
178                  "to this point."));
179       break;
180
181     default:
182       abort ();
183     }
184
185   if (!getl_is_interactive ())
186     {
187       while (token != T_STOP && token != '.')
188         lex_get ();
189     }
190   else 
191     {
192       msg (SW, _("The rest of this command has been discarded."));
193       lex_discard_line (); 
194     }
195 }
196 \f
197 static void
198 i18n_init (void) 
199 {
200 #if ENABLE_NLS
201 #if HAVE_LC_MESSAGES
202   setlocale (LC_MESSAGES, "");
203 #endif
204   setlocale (LC_MONETARY, "");
205   bindtextdomain (PACKAGE, locale_dir);
206   textdomain (PACKAGE);
207 #endif /* ENABLE_NLS */
208 }
209
210 static void
211 fpu_init (void) 
212 {
213 #if HAVE_FEHOLDEXCEPT
214   fenv_t foo;
215   feholdexcept (&foo);
216 #elif HAVE___SETFPUCW && defined(_FPU_IEEE)
217   __setfpucw (_FPU_IEEE);
218 #endif
219 }
220
221 /* If a segfault happens, issue a message to that effect and halt */
222 void 
223 bug_handler(int sig)
224 {
225   switch (sig) 
226     {
227     case SIGFPE:
228       request_bug_report_and_abort("Floating Point Exception");
229       break;
230     case SIGSEGV:
231       request_bug_report_and_abort("Segmentation Violation");
232       break;
233     default:
234       request_bug_report_and_abort("");
235       break;
236     }
237 }
238
239 void 
240 interrupt_handler(int sig UNUSED)
241 {
242   terminate (false);
243 }
244
245
246 /* Terminate PSPP.  SUCCESS should be true to exit successfully,
247    false to exit as a failure.  */
248 void
249 terminate (bool success)
250 {
251   static bool terminating = false;
252   if (terminating)
253     return;
254   terminating = true;
255
256   err_done ();
257   outp_done ();
258
259   cancel_transformations ();
260   dict_destroy (default_dict);
261
262   random_done ();
263   settings_done ();
264   fh_done ();
265   lex_done ();
266   getl_uninitialize ();
267
268   exit (success ? EXIT_SUCCESS : EXIT_FAILURE);
269 }