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