1be99732d7df6d002c6cbc8cfa705600729f7dc7
[pspp-builds.git] / 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 #include <unistd.h>
32
33 #include "data/dictionary.h"
34 #include "data/file-handle-def.h"
35 #include "data/file-name.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 "language/syntax-file.h"
44 #include "libpspp/argv-parser.h"
45 #include "libpspp/compiler.h"
46 #include "libpspp/getl.h"
47 #include "libpspp/i18n.h"
48 #include "libpspp/message.h"
49 #include "libpspp/version.h"
50 #include "math/random.h"
51 #include "output/driver.h"
52 #include "ui/debugger.h"
53 #include "ui/source-init-opts.h"
54 #include "ui/terminal/msg-ui.h"
55 #include "ui/terminal/read-line.h"
56 #include "ui/terminal/terminal-opts.h"
57 #include "ui/terminal/terminal.h"
58
59 #include "gl/fatal-signal.h"
60 #include "gl/progname.h"
61 #include "gl/relocatable.h"
62
63 #include "gettext.h"
64 #define _(msgid) gettext (msgid)
65
66 static struct dataset * the_dataset = NULL;
67
68 static struct lexer *the_lexer;
69 static struct source_stream *the_source_stream ;
70
71 static void add_syntax_file (struct source_stream *, enum syntax_mode,
72                              const char *file_name);
73 static void bug_handler(int sig);
74 static void fpu_init (void);
75 static void clean_up (void);
76
77 /* Program entry point. */
78 int
79 main (int argc, char **argv)
80 {
81   struct terminal_opts *terminal_opts;
82   struct argv_parser *parser;
83   enum syntax_mode syntax_mode;
84   bool process_statrc;
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   settings_init ();
102   terminal_check_size ();
103   random_init ();
104
105   the_dataset = create_dataset ();
106
107   parser = argv_parser_create ();
108   terminal_opts = terminal_opts_init (parser, &syntax_mode, &process_statrc);
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   /* Add syntax files to source stream. */
118   if (process_statrc)
119     {
120       char *rc = fn_search_path ("rc", getl_include_path (the_source_stream));
121       if (rc != NULL)
122         {
123           add_syntax_file (the_source_stream, GETL_BATCH, rc);
124           free (rc);
125         }
126     }
127   if (optind < argc)
128     {
129       int i;
130
131       for (i = optind; i < argc; i++)
132         add_syntax_file (the_source_stream, syntax_mode, argv[i]);
133     }
134   else
135     add_syntax_file (the_source_stream, syntax_mode, "-");
136
137   /* Parse and execute syntax. */
138   the_lexer = lex_create (the_source_stream);
139   for (;;)
140     {
141       int result = cmd_parse (the_lexer, the_dataset);
142
143       if (result == CMD_EOF || result == CMD_FINISH)
144         break;
145       if (result == CMD_CASCADING_FAILURE &&
146           !getl_is_interactive (the_source_stream))
147         {
148           msg (SE, _("Stopping syntax file processing here to avoid "
149                      "a cascade of dependent command failures."));
150           getl_abort_noninteractive (the_source_stream);
151         }
152       else if (msg_ui_too_many_errors ())
153         getl_abort_noninteractive (the_source_stream);
154     }
155
156
157   clean_up ();
158   return msg_ui_any_errors ();
159 }
160
161 static void
162 fpu_init (void)
163 {
164 #if HAVE_FEHOLDEXCEPT
165   fenv_t foo;
166   feholdexcept (&foo);
167 #elif HAVE___SETFPUCW && defined(_FPU_IEEE)
168   __setfpucw (_FPU_IEEE);
169 #elif HAVE_FPSETMASK
170   fpsetmask (0);
171 #endif
172 }
173
174 /* If a segfault happens, issue a message to that effect and halt */
175 static void
176 bug_handler(int sig)
177 {
178   /* Reset SIG to its default handling so that if it happens again we won't
179      recurse. */
180   signal (sig, SIG_DFL);
181
182 #if DEBUGGING
183   connect_debugger ();
184 #endif
185   switch (sig)
186     {
187     case SIGABRT:
188       request_bug_report("Assertion Failure/Abort");
189       break;
190     case SIGFPE:
191       request_bug_report("Floating Point Exception");
192       break;
193     case SIGSEGV:
194       request_bug_report("Segmentation Violation");
195       break;
196     default:
197       request_bug_report("Unknown");
198       break;
199     }
200
201   /* Re-raise the signal so that we terminate with the correct status. */
202   raise (sig);
203 }
204
205 /* Clean up PSPP in preparation for termination.  */
206 static void
207 clean_up (void)
208 {
209   static bool terminating = false;
210   if (!terminating)
211     {
212       terminating = true;
213
214       destroy_dataset (the_dataset);
215
216       random_done ();
217       settings_done ();
218       fh_done ();
219       lex_destroy (the_lexer);
220       destroy_source_stream (the_source_stream);
221       prompt_done ();
222       readln_uninitialize ();
223       output_close ();
224       msg_ui_done ();
225       i18n_done ();
226     }
227 }
228
229 static void
230 add_syntax_file (struct source_stream *ss, enum syntax_mode syntax_mode,
231                  const char *file_name)
232 {
233   struct getl_interface *source;
234
235   source = (!strcmp (file_name, "-") && isatty (STDIN_FILENO)
236            ? create_readln_source ()
237            : create_syntax_file_source (file_name));
238   getl_append_source (ss, source, syntax_mode, ERRMODE_CONTINUE);
239 }