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