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