9375ffc62960317cb7825fcfa0189a82e1a3ccb5
[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       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   setlocale (LC_MONETARY, "");
154   bindtextdomain (PACKAGE, locale_dir);
155   textdomain (PACKAGE);
156 #endif /* ENABLE_NLS */
157 }
158
159 static void
160 fpu_init (void)
161 {
162 #if HAVE_FEHOLDEXCEPT
163   fenv_t foo;
164   feholdexcept (&foo);
165 #elif HAVE___SETFPUCW && defined(_FPU_IEEE)
166   __setfpucw (_FPU_IEEE);
167 #elif HAVE_FPSETMASK
168   fpsetmask (0);
169 #endif
170 }
171
172 /* If a segfault happens, issue a message to that effect and halt */
173 void
174 bug_handler(int sig)
175 {
176 #if DEBUGGING
177   connect_debugger ();
178 #endif
179   switch (sig)
180     {
181     case SIGABRT:
182       request_bug_report_and_abort("Assertion Failure/Abort");
183     case SIGFPE:
184       request_bug_report_and_abort("Floating Point Exception");
185     case SIGSEGV:
186       request_bug_report_and_abort("Segmentation Violation");
187     default:
188       request_bug_report_and_abort("Unknown");
189     }
190 }
191
192 void
193 interrupt_handler(int sig UNUSED)
194 {
195   terminate (false);
196 }
197
198
199 /* Terminate PSPP.  SUCCESS should be true to exit successfully,
200    false to exit as a failure.  */
201 static void
202 terminate (bool success)
203 {
204   static bool terminating = false;
205   if (!terminating)
206     {
207       terminating = true;
208
209       destroy_dataset (the_dataset);
210
211       random_done ();
212       settings_done ();
213       fh_done ();
214       lex_destroy (the_lexer);
215       destroy_source_stream (the_source_stream);
216       prompt_done ();
217       readln_uninitialize ();
218
219       outp_done ();
220       msg_ui_done ();
221       fmt_done ();
222     }
223   exit (success ? EXIT_SUCCESS : EXIT_FAILURE);
224 }