Change license from GPLv2+ to GPLv3+.
[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 (NULL, NULL);
111
112   if (parse_command_line (argc, argv, the_source_stream))
113     {
114       msg_ui_init (the_source_stream);
115       outp_read_devices ();
116       the_lexer = lex_create (the_source_stream);
117
118       for (;;)
119         {
120           int result = cmd_parse (the_lexer, the_dataset);
121
122           if (result == CMD_EOF || result == CMD_FINISH)
123             break;
124           if (result == CMD_CASCADING_FAILURE &&
125               !getl_is_interactive (the_source_stream))
126             {
127               msg (SE, _("Stopping syntax file processing here to avoid "
128                          "a cascade of dependent command failures."));
129               getl_abort_noninteractive (the_source_stream);
130             }
131           else
132             check_msg_count (the_source_stream);
133         }
134     }
135
136   terminate (!any_errors ());
137 }
138 \f
139 static void
140 i18n_init (void)
141 {
142 #if ENABLE_NLS
143 #if HAVE_LC_MESSAGES
144   setlocale (LC_MESSAGES, "");
145 #endif
146   setlocale (LC_MONETARY, "");
147   bindtextdomain (PACKAGE, locale_dir);
148   textdomain (PACKAGE);
149 #endif /* ENABLE_NLS */
150 }
151
152 static void
153 fpu_init (void)
154 {
155 #if HAVE_FEHOLDEXCEPT
156   fenv_t foo;
157   feholdexcept (&foo);
158 #elif HAVE___SETFPUCW && defined(_FPU_IEEE)
159   __setfpucw (_FPU_IEEE);
160 #elif HAVE_FPSETMASK
161   fpsetmask (0);
162 #endif
163 }
164
165 /* If a segfault happens, issue a message to that effect and halt */
166 void
167 bug_handler(int sig)
168 {
169 #if DEBUGGING
170   connect_debugger ();
171 #endif
172   switch (sig)
173     {
174     case SIGABRT:
175       request_bug_report_and_abort("Assertion Failure/Abort");
176     case SIGFPE:
177       request_bug_report_and_abort("Floating Point Exception");
178     case SIGSEGV:
179       request_bug_report_and_abort("Segmentation Violation");
180     default:
181       request_bug_report_and_abort("Unknown");
182     }
183 }
184
185 void
186 interrupt_handler(int sig UNUSED)
187 {
188   terminate (false);
189 }
190
191
192 /* Terminate PSPP.  SUCCESS should be true to exit successfully,
193    false to exit as a failure.  */
194 static void
195 terminate (bool success)
196 {
197   static bool terminating = false;
198   if (!terminating)
199     {
200       terminating = true;
201
202       destroy_dataset (the_dataset);
203
204       random_done ();
205       settings_done ();
206       fh_done ();
207       lex_destroy (the_lexer);
208       destroy_source_stream (the_source_stream);
209       prompt_done ();
210       readln_uninitialize ();
211
212       outp_done ();
213       msg_ui_done ();
214       fmt_done ();
215     }
216   exit (success ? EXIT_SUCCESS : EXIT_FAILURE);
217 }