f43bd5dc11556c2ed05fade994654c7c8d989860
[pspp] / src / ui / terminal / main.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-2000, 2006-2007, 2009-2014 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/dataset.h"
34 #include "data/dictionary.h"
35 #include "data/file-handle-def.h"
36 #include "data/session.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/lexer/include-path.h"
43 #include "libpspp/argv-parser.h"
44 #include "libpspp/compiler.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 "output/output-item.h"
51 #include "ui/source-init-opts.h"
52 #include "ui/terminal/terminal-opts.h"
53 #include "ui/terminal/terminal-reader.h"
54
55 #include "gl/fatal-signal.h"
56 #include "gl/progname.h"
57 #include "gl/relocatable.h"
58
59 #include "gettext.h"
60 #define _(msgid) gettext (msgid)
61
62 static struct session *the_session;
63
64 static void add_syntax_reader (struct lexer *, const char *file_name,
65                                const char *encoding, enum segmenter_mode);
66 static void bug_handler(int sig);
67 static void fpu_init (void);
68 static void output_msg (const struct msg *, void *);
69
70 /* Program entry point. */
71 int
72 main (int argc, char **argv)
73 {
74   struct terminal_opts *terminal_opts;
75   struct argv_parser *parser;
76   enum segmenter_mode syntax_mode;
77   char *syntax_encoding;
78   bool process_statrc;
79   struct lexer *lexer;
80
81   set_program_name (argv[0]);
82
83   prepare_fatal_error_message ();
84   prepare_diagnostic_information ();
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   output_engine_push ();
95   fh_init ();
96   settings_init ();
97   random_init ();
98
99   lexer = lex_create ();
100   the_session = session_create (NULL);
101   dataset_create (the_session, "");
102
103   parser = argv_parser_create ();
104   terminal_opts = terminal_opts_init (parser, &syntax_mode, &process_statrc,
105                                       &syntax_encoding);
106   source_init_register_argv_parser (parser);
107   if (!argv_parser_run (parser, argc, argv))
108     exit (EXIT_FAILURE);
109   terminal_opts_done (terminal_opts, argc, argv);
110   argv_parser_destroy (parser);
111
112   msg_set_handler (output_msg, lexer);
113   session_set_default_syntax_encoding (the_session, syntax_encoding);
114
115   /* Add syntax files to source stream. */
116   if (process_statrc)
117     {
118       char *rc = include_path_search ("rc");
119       if (rc != NULL)
120         {
121           add_syntax_reader (lexer, rc, "Auto", SEG_MODE_AUTO);
122           free (rc);
123         }
124     }
125   if (optind < argc)
126     {
127       int i;
128
129       for (i = optind; i < argc; i++)
130         add_syntax_reader (lexer, argv[i], syntax_encoding, syntax_mode);
131     }
132   else
133     add_syntax_reader (lexer, "-", syntax_encoding, syntax_mode);
134
135   /* Parse and execute syntax. */
136   lex_get (lexer);
137   for (;;)
138     {
139       int result = cmd_parse (lexer, session_active_dataset (the_session));
140
141       if (result == CMD_EOF || result == CMD_FINISH)
142         break;
143       else if (cmd_result_is_failure (result) && lex_token (lexer) != T_STOP)
144         {
145           if (lex_get_error_mode (lexer) == LEX_ERROR_STOP)
146             {
147               msg (MW, _("Error encountered while ERROR=STOP is effective."));
148               lex_discard_noninteractive (lexer);
149             }
150           else if (result == CMD_CASCADING_FAILURE
151                    && lex_get_error_mode (lexer) != LEX_ERROR_TERMINAL)
152             {
153               msg (SE, _("Stopping syntax file processing here to avoid "
154                          "a cascade of dependent command failures."));
155               lex_discard_noninteractive (lexer);
156             }
157         }
158
159       if (msg_ui_too_many_errors ())
160         lex_discard_noninteractive (lexer);
161     }
162
163
164   output_engine_pop ();
165   session_destroy (the_session);
166
167   random_done ();
168   settings_done ();
169   fh_done ();
170   lex_destroy (lexer);
171   i18n_done ();
172
173   return msg_ui_any_errors ();
174 }
175 \f
176 static void
177 fpu_init (void)
178 {
179 #if HAVE_FEHOLDEXCEPT
180   fenv_t foo;
181   feholdexcept (&foo);
182 #elif HAVE___SETFPUCW && defined(_FPU_IEEE)
183   __setfpucw (_FPU_IEEE);
184 #elif HAVE_FPSETMASK
185   fpsetmask (0);
186 #endif
187 }
188
189 /* If a segfault happens, issue a message to that effect and halt */
190 static void
191 bug_handler(int sig)
192 {
193   /* Reset SIG to its default handling so that if it happens again we won't
194      recurse. */
195   signal (sig, SIG_DFL);
196
197   switch (sig)
198     {
199     case SIGABRT:
200       request_bug_report("Assertion Failure/Abort");
201       break;
202     case SIGFPE:
203       request_bug_report("Floating Point Exception");
204       break;
205     case SIGSEGV:
206       request_bug_report("Segmentation Violation");
207       break;
208     default:
209       request_bug_report("Unknown");
210       break;
211     }
212
213   /* Re-raise the signal so that we terminate with the correct status. */
214   raise (sig);
215 }
216
217 static void
218 output_msg (const struct msg *m_, void *lexer_)
219 {
220   struct lexer *lexer = lexer_;
221   struct msg m = {
222     .category = m_->category,
223     .severity = m_->severity,
224     .stack = m_->stack,
225     .n_stack = m_->n_stack,
226     .location = (m_->location ? m_->location
227                  : lexer ? lex_get_lines (lexer, 0, 0)
228                  : NULL),
229     .command_name = output_get_uppercase_command_name (),
230     .text = m_->text,
231   };
232
233   output_item_submit (message_item_create (&m));
234
235   free (m.command_name);
236   if (m.location != m_->location)
237     msg_location_destroy (m.location);
238 }
239
240 static void
241 add_syntax_reader (struct lexer *lexer, const char *file_name,
242                    const char *encoding, enum segmenter_mode syntax_mode)
243 {
244   struct lex_reader *reader;
245
246   reader = (!strcmp (file_name, "-") && isatty (STDIN_FILENO)
247             ? terminal_reader_create ()
248             : lex_reader_for_file (file_name, encoding, syntax_mode,
249                                    LEX_ERROR_CONTINUE));
250
251   if (reader)
252     lex_append (lexer, reader);
253 }