Improve portability to NetBSD and Alpha.
[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/fastfile-factory.h>
31 #include <data/dictionary.h>
32 #include <data/file-handle-def.h>
33 #include <libpspp/getl.h>
34 #include <data/file-name.h>
35 #include <data/format.h>
36 #include <data/procedure.h>
37 #include <data/settings.h>
38 #include <data/variable.h>
39 #include <gsl/gsl_errno.h>
40 #include <language/command.h>
41 #include <language/lexer/lexer.h>
42 #include <language/prompt.h>
43 #include <libpspp/compiler.h>
44 #include <libpspp/message.h>
45 #include <libpspp/version.h>
46 #include <math/random.h>
47 #include <output/output.h>
48
49 #if HAVE_FPU_CONTROL_H
50 #include <fpu_control.h>
51 #endif
52
53 #if HAVE_LOCALE_H
54 #include <locale.h>
55 #endif
56
57 #if HAVE_FENV_H
58 #include <fenv.h>
59 #endif
60
61 #if HAVE_IEEEFP_H
62 #include <ieeefp.h>
63 #endif
64
65 #include "gettext.h"
66 #define _(msgid) gettext (msgid)
67
68 #include <stdlib.h>
69
70 static void i18n_init (void);
71 static void fpu_init (void);
72 static void terminate (bool success) NO_RETURN;
73
74 /* If a segfault happens, issue a message to that effect and halt */
75 void bug_handler(int sig);
76
77 /* Handle quit/term/int signals */
78 void interrupt_handler(int sig);
79 static struct dataset * the_dataset = NULL;
80
81 static struct lexer *the_lexer;
82 static struct source_stream *the_source_stream ;
83
84
85 /* Program entry point. */
86 int
87 main (int argc, char **argv)
88 {
89   struct casefile_factory *factory;
90   signal (SIGABRT, bug_handler);
91   signal (SIGSEGV, bug_handler);
92   signal (SIGFPE, bug_handler);
93   signal (SIGINT, interrupt_handler);
94
95   set_program_name ("pspp");
96   i18n_init ();
97   fpu_init ();
98   gsl_set_error_handler_off ();
99
100   fmt_init ();
101   outp_init ();
102   fn_init ();
103   fh_init ();
104   the_source_stream =
105     create_source_stream (
106                           fn_getenv_default ("STAT_INCLUDE_PATH", include_path)
107                           );
108   prompt_init ();
109   readln_initialize ();
110   settings_init ();
111   random_init ();
112
113   factory = fastfile_factory_create ();
114
115   the_dataset = create_dataset (factory, NULL, NULL);
116
117   if (parse_command_line (argc, argv, the_source_stream))
118     {
119       msg_ui_init (the_source_stream);
120       outp_read_devices ();
121       the_lexer = lex_create (the_source_stream);
122
123       for (;;)
124         {
125           int result = cmd_parse (the_lexer, the_dataset);
126
127           if (result == CMD_EOF || result == CMD_FINISH)
128             break;
129           if (result == CMD_CASCADING_FAILURE &&
130               !getl_is_interactive (the_source_stream))
131             {
132               msg (SE, _("Stopping syntax file processing here to avoid "
133                          "a cascade of dependent command failures."));
134               getl_abort_noninteractive (the_source_stream); 
135             }
136           else
137             check_msg_count (the_source_stream);
138         }
139     }
140
141   terminate (!any_errors ());
142 }
143 \f
144 static void
145 i18n_init (void) 
146 {
147 #if ENABLE_NLS
148 #if HAVE_LC_MESSAGES
149   setlocale (LC_MESSAGES, "");
150 #endif
151   setlocale (LC_MONETARY, "");
152   bindtextdomain (PACKAGE, locale_dir);
153   textdomain (PACKAGE);
154 #endif /* ENABLE_NLS */
155 }
156
157 static void
158 fpu_init (void) 
159 {
160 #if HAVE_FEHOLDEXCEPT
161   fenv_t foo;
162   feholdexcept (&foo);
163 #elif HAVE___SETFPUCW && defined(_FPU_IEEE)
164   __setfpucw (_FPU_IEEE);
165 #elif HAVE_FPSETMASK
166   fpsetmask (0);
167 #endif
168 }
169
170 /* If a segfault happens, issue a message to that effect and halt */
171 void 
172 bug_handler(int sig)
173 {
174 #if DEBUGGING
175   connect_debugger ();
176 #endif
177   switch (sig) 
178     {
179     case SIGABRT:
180       request_bug_report_and_abort("Assertion Failure/Abort");
181     case SIGFPE:
182       request_bug_report_and_abort("Floating Point Exception");
183     case SIGSEGV:
184       request_bug_report_and_abort("Segmentation Violation");
185     default:
186       request_bug_report_and_abort("Unknown");
187     }
188 }
189
190 void 
191 interrupt_handler(int sig UNUSED)
192 {
193   terminate (false);
194 }
195
196
197 /* Terminate PSPP.  SUCCESS should be true to exit successfully,
198    false to exit as a failure.  */
199 static void
200 terminate (bool success)
201 {
202   static bool terminating = false;
203   if (!terminating) 
204     {
205       terminating = true;
206
207       destroy_dataset (the_dataset);
208
209       random_done ();
210       settings_done ();
211       fh_done ();
212       lex_destroy (the_lexer);
213       destroy_source_stream (the_source_stream);
214       prompt_done ();
215       readln_uninitialize ();
216
217       outp_done ();
218       msg_ui_done ();
219       fmt_done ();
220     }
221   exit (success ? EXIT_SUCCESS : EXIT_FAILURE);
222 }