Bug #21128. Reviewed by John Darrington.
[pspp-builds.git] / src / ui / terminal / main.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <signal.h>
20 #include <stdio.h>
21
22 #include <ui/debugger.h>
23 #include "command-line.h"
24 #include "msg-ui.h"
25 #include "progname.h"
26 #include "read-line.h"
27
28 #include <data/dictionary.h>
29 #include <data/file-handle-def.h>
30 #include <libpspp/getl.h>
31 #include <data/file-name.h>
32 #include <data/format.h>
33 #include <data/procedure.h>
34 #include <data/settings.h>
35 #include <data/variable.h>
36 #include <gsl/gsl_errno.h>
37 #include <language/command.h>
38 #include <language/lexer/lexer.h>
39 #include <language/prompt.h>
40 #include <libpspp/compiler.h>
41 #include <libpspp/message.h>
42 #include <libpspp/version.h>
43 #include <math/random.h>
44 #include <output/output.h>
45
46 #if HAVE_FPU_CONTROL_H
47 #include <fpu_control.h>
48 #endif
49
50 #if HAVE_LOCALE_H
51 #include <locale.h>
52 #endif
53
54 #if HAVE_FENV_H
55 #include <fenv.h>
56 #endif
57
58 #if HAVE_IEEEFP_H
59 #include <ieeefp.h>
60 #endif
61
62 #include "gettext.h"
63 #define _(msgid) gettext (msgid)
64
65 #include <stdlib.h>
66
67 static void i18n_init (void);
68 static void fpu_init (void);
69 static void terminate (bool success) NO_RETURN;
70
71 /* If a segfault happens, issue a message to that effect and halt */
72 void bug_handler(int sig);
73
74 /* Handle quit/term/int signals */
75 void interrupt_handler(int sig);
76 static struct dataset * the_dataset = NULL;
77
78 static struct lexer *the_lexer;
79 static struct source_stream *the_source_stream ;
80
81
82 /* Program entry point. */
83 int
84 main (int argc, char **argv)
85 {
86   signal (SIGABRT, bug_handler);
87   signal (SIGSEGV, bug_handler);
88   signal (SIGFPE, bug_handler);
89   signal (SIGINT, interrupt_handler);
90
91   set_program_name (argv[0]);
92
93   i18n_init ();
94   fpu_init ();
95   gsl_set_error_handler_off ();
96
97   fmt_init ();
98   outp_init ();
99   fn_init ();
100   fh_init ();
101   the_source_stream =
102     create_source_stream (
103                           fn_getenv_default ("STAT_INCLUDE_PATH", include_path)
104                           );
105   prompt_init ();
106   readln_initialize ();
107   settings_init ();
108   random_init ();
109
110   the_dataset = create_dataset ();
111
112   if (parse_command_line (argc, argv, the_source_stream))
113     {
114       msg_ui_init (the_source_stream);
115       if (!get_testing_mode ())
116         outp_read_devices ();
117       else
118         outp_configure_driver_line (
119           ss_cstr ("raw-ascii:ascii:listing:width=9999 length=9999 "
120                    "output-file=\"pspp.list\" emphasis=none "
121                    "headers=off paginate=off squeeze=on "
122                    "top-margin=0 bottom-margin=0"));
123       the_lexer = lex_create (the_source_stream);
124
125       for (;;)
126         {
127           int result = cmd_parse (the_lexer, the_dataset);
128
129           if (result == CMD_EOF || result == CMD_FINISH)
130             break;
131           if (result == CMD_CASCADING_FAILURE &&
132               !getl_is_interactive (the_source_stream))
133             {
134               msg (SE, _("Stopping syntax file processing here to avoid "
135                          "a cascade of dependent command failures."));
136               getl_abort_noninteractive (the_source_stream);
137             }
138           else
139             check_msg_count (the_source_stream);
140         }
141     }
142
143   terminate (!any_errors ());
144 }
145 \f
146 static void
147 i18n_init (void)
148 {
149 #if ENABLE_NLS
150 #if HAVE_LC_MESSAGES
151   setlocale (LC_MESSAGES, "");
152 #endif
153 #if HAVE_LC_PAPER
154   setlocale (LC_PAPER, "");
155 #endif
156   bindtextdomain (PACKAGE, locale_dir);
157   textdomain (PACKAGE);
158 #endif /* ENABLE_NLS */
159 }
160
161 static void
162 fpu_init (void)
163 {
164 #if HAVE_FEHOLDEXCEPT
165   fenv_t foo;
166   feholdexcept (&foo);
167 #elif HAVE___SETFPUCW && defined(_FPU_IEEE)
168   __setfpucw (_FPU_IEEE);
169 #elif HAVE_FPSETMASK
170   fpsetmask (0);
171 #endif
172 }
173
174 /* If a segfault happens, issue a message to that effect and halt */
175 void
176 bug_handler(int sig)
177 {
178 #if DEBUGGING
179   connect_debugger ();
180 #endif
181   switch (sig)
182     {
183     case SIGABRT:
184       request_bug_report_and_abort("Assertion Failure/Abort");
185     case SIGFPE:
186       request_bug_report_and_abort("Floating Point Exception");
187     case SIGSEGV:
188       request_bug_report_and_abort("Segmentation Violation");
189     default:
190       request_bug_report_and_abort("Unknown");
191     }
192 }
193
194 void
195 interrupt_handler(int sig UNUSED)
196 {
197   terminate (false);
198 }
199
200
201 /* Terminate PSPP.  SUCCESS should be true to exit successfully,
202    false to exit as a failure.  */
203 static void
204 terminate (bool success)
205 {
206   static bool terminating = false;
207   if (!terminating)
208     {
209       terminating = true;
210
211       destroy_dataset (the_dataset);
212
213       random_done ();
214       settings_done ();
215       fh_done ();
216       lex_destroy (the_lexer);
217       destroy_source_stream (the_source_stream);
218       prompt_done ();
219       readln_uninitialize ();
220
221       outp_done ();
222       msg_ui_done ();
223       fmt_done ();
224     }
225   exit (success ? EXIT_SUCCESS : EXIT_FAILURE);
226 }