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