5e9a605bc4aaef361f8c28d2940c4d4770eb7ede
[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    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21
22 #include <signal.h>
23 #include <stdio.h>
24
25 #include <ui/debugger.h>
26 #include "command-line.h"
27 #include "msg-ui.h"
28 #include "progname.h"
29 #include "read-line.h"
30
31 #include <data/dictionary.h>
32 #include <data/file-handle-def.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/line-buffer.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 #include "gettext.h"
61 #define _(msgid) gettext (msgid)
62
63 #include <stdlib.h>
64
65 static void i18n_init (void);
66 static void fpu_init (void);
67 static void terminate (bool success) NO_RETURN;
68
69 /* If a segfault happens, issue a message to that effect and halt */
70 void bug_handler(int sig);
71
72 /* Handle quit/term/int signals */
73 void interrupt_handler(int sig);
74 static struct dataset * the_dataset = NULL;
75
76 static struct lexer *the_lexer;
77
78 /* Program entry point. */
79 int
80 main (int argc, char **argv)
81 {
82   signal (SIGABRT, bug_handler);
83   signal (SIGSEGV, bug_handler);
84   signal (SIGFPE, bug_handler);
85   signal (SIGINT, interrupt_handler);
86
87   set_program_name ("pspp");
88   i18n_init ();
89   fpu_init ();
90   gsl_set_error_handler_off ();
91
92   fmt_init ();
93   outp_init ();
94   fn_init ();
95   fh_init ();
96   getl_initialize ();
97   readln_initialize ();
98   settings_init ();
99   random_init ();
100   the_dataset = create_dataset ();
101
102   if (parse_command_line (argc, argv)) 
103     {
104       msg_ui_init ();
105       outp_read_devices ();
106       the_lexer = lex_create (do_read_line);
107
108       for (;;)
109         {
110           int result = cmd_parse (the_lexer, the_dataset, 
111                                   proc_has_source (the_dataset)
112                                  ? CMD_STATE_DATA : CMD_STATE_INITIAL);
113           if (result == CMD_EOF || result == CMD_FINISH)
114             break;
115           if (result == CMD_CASCADING_FAILURE && !getl_is_interactive ())
116             {
117               msg (SE, _("Stopping syntax file processing here to avoid "
118                          "a cascade of dependent command failures."));
119               getl_abort_noninteractive (); 
120             }
121           else
122             check_msg_count ();
123         }
124     }
125   
126   terminate (!any_errors ());
127 }
128 \f
129 static void
130 i18n_init (void) 
131 {
132 #if ENABLE_NLS
133 #if HAVE_LC_MESSAGES
134   setlocale (LC_MESSAGES, "");
135 #endif
136   setlocale (LC_MONETARY, "");
137   bindtextdomain (PACKAGE, locale_dir);
138   textdomain (PACKAGE);
139 #endif /* ENABLE_NLS */
140 }
141
142 static void
143 fpu_init (void) 
144 {
145 #if HAVE_FEHOLDEXCEPT
146   fenv_t foo;
147   feholdexcept (&foo);
148 #elif HAVE___SETFPUCW && defined(_FPU_IEEE)
149   __setfpucw (_FPU_IEEE);
150 #endif
151 }
152
153 /* If a segfault happens, issue a message to that effect and halt */
154 void 
155 bug_handler(int sig)
156 {
157 #if DEBUGGING
158   connect_debugger ();
159 #endif
160   switch (sig) 
161     {
162     case SIGABRT:
163       request_bug_report_and_abort("Assertion Failure/Abort");
164     case SIGFPE:
165       request_bug_report_and_abort("Floating Point Exception");
166     case SIGSEGV:
167       request_bug_report_and_abort("Segmentation Violation");
168     default:
169       request_bug_report_and_abort("Unknown");
170     }
171 }
172
173 void 
174 interrupt_handler(int sig UNUSED)
175 {
176   terminate (false);
177 }
178
179
180 /* Terminate PSPP.  SUCCESS should be true to exit successfully,
181    false to exit as a failure.  */
182 static void
183 terminate (bool success)
184 {
185   static bool terminating = false;
186   if (!terminating) 
187     {
188       terminating = true;
189
190       destroy_dataset (the_dataset);
191
192       random_done ();
193       settings_done ();
194       fh_done ();
195       lex_destroy (the_lexer);
196       getl_uninitialize ();
197       readln_uninitialize ();
198
199       outp_done ();
200       msg_ui_done ();
201       fmt_done ();
202     }
203   exit (success ? EXIT_SUCCESS : EXIT_FAILURE);
204 }