02be4f78c0e777f1e93df1719fc7fc0ed48f13f2
[pspp] / src / ui / terminal / main.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2007, 2009, 2010 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 #include <stdlib.h>
22 #if HAVE_FPU_CONTROL_H
23 #include <fpu_control.h>
24 #endif
25 #if HAVE_FENV_H
26 #include <fenv.h>
27 #endif
28 #if HAVE_IEEEFP_H
29 #include <ieeefp.h>
30 #endif
31
32 #include "data/dictionary.h"
33 #include "data/file-handle-def.h"
34 #include "data/file-name.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/prompt.h"
42 #include "libpspp/argv-parser.h"
43 #include "libpspp/compiler.h"
44 #include "libpspp/getl.h"
45 #include "libpspp/i18n.h"
46 #include "libpspp/message.h"
47 #include "libpspp/version.h"
48 #include "math/random.h"
49 #include "output/driver.h"
50 #include "ui/debugger.h"
51 #include "ui/source-init-opts.h"
52 #include "ui/terminal/msg-ui.h"
53 #include "ui/terminal/read-line.h"
54 #include "ui/terminal/terminal-opts.h"
55 #include "ui/terminal/terminal.h"
56
57 #include "gl/fatal-signal.h"
58 #include "gl/progname.h"
59 #include "gl/relocatable.h"
60
61 #include "gettext.h"
62 #define _(msgid) gettext (msgid)
63
64
65 static void fpu_init (void);
66 static void clean_up (void);
67
68 /* If a segfault happens, issue a message to that effect and halt */
69 void bug_handler(int sig);
70
71 /* Handle quit/term/int signals */
72 void interrupt_handler(int sig);
73 static struct dataset * the_dataset = NULL;
74
75 static struct lexer *the_lexer;
76 static struct source_stream *the_source_stream ;
77
78 /* Program entry point. */
79 int
80 main (int argc, char **argv)
81 {
82   int *view_width_p, *view_length_p;
83   struct terminal_opts *terminal_opts;
84   struct argv_parser *parser;
85
86   set_program_name (argv[0]);
87
88   signal (SIGABRT, bug_handler);
89   signal (SIGSEGV, bug_handler);
90   signal (SIGFPE, bug_handler);
91   at_fatal_signal (clean_up);
92
93   i18n_init ();
94   fpu_init ();
95   gsl_set_error_handler_off ();
96
97   fh_init ();
98   the_source_stream = create_source_stream ();
99   prompt_init ();
100   readln_initialize ();
101   terminal_init (&view_width_p, &view_length_p);
102   settings_init (view_width_p, view_length_p);
103   random_init ();
104
105   the_dataset = create_dataset ();
106
107   parser = argv_parser_create ();
108   terminal_opts = terminal_opts_init (parser, the_source_stream);
109   source_init_register_argv_parser (parser, the_source_stream);
110   if (!argv_parser_run (parser, argc, argv))
111     exit (EXIT_FAILURE);
112   terminal_opts_done (terminal_opts, argc, argv);
113   argv_parser_destroy (parser);
114
115   msg_ui_init (the_source_stream);
116
117   the_lexer = lex_create (the_source_stream);
118
119   for (;;)
120     {
121       int result = cmd_parse (the_lexer, the_dataset);
122
123       if (result == CMD_EOF || result == CMD_FINISH)
124         break;
125       if (result == CMD_CASCADING_FAILURE &&
126           !getl_is_interactive (the_source_stream))
127         {
128           msg (SE, _("Stopping syntax file processing here to avoid "
129                      "a cascade of dependent command failures."));
130           getl_abort_noninteractive (the_source_stream);
131         }
132       else if (msg_ui_too_many_errors ())
133         getl_abort_noninteractive (the_source_stream);
134     }
135
136
137   clean_up ();
138   return msg_ui_any_errors ();
139 }
140 \f
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 #elif HAVE_FPSETMASK
151   fpsetmask (0);
152 #endif
153 }
154
155 /* If a segfault happens, issue a message to that effect and halt */
156 void
157 bug_handler(int sig)
158 {
159 #if DEBUGGING
160   connect_debugger ();
161 #endif
162   switch (sig)
163     {
164     case SIGABRT:
165       request_bug_report_and_abort("Assertion Failure/Abort");
166     case SIGFPE:
167       request_bug_report_and_abort("Floating Point Exception");
168     case SIGSEGV:
169       request_bug_report_and_abort("Segmentation Violation");
170     default:
171       request_bug_report_and_abort("Unknown");
172     }
173 }
174
175 /* Clean up PSPP in preparation for termination.  */
176 static void
177 clean_up (void)
178 {
179   static bool terminating = false;
180   if (!terminating)
181     {
182       terminating = true;
183
184       destroy_dataset (the_dataset);
185
186       random_done ();
187       settings_done ();
188       fh_done ();
189       lex_destroy (the_lexer);
190       destroy_source_stream (the_source_stream);
191       prompt_done ();
192       readln_uninitialize ();
193       output_close ();
194       msg_ui_done ();
195       i18n_done ();
196     }
197 }