a9db6febe4a9db25e72244b206e51bf88e3db724
[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, 2011 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/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/message-item.h"
51 #include "ui/debugger.h"
52 #include "ui/source-init-opts.h"
53 #include "ui/terminal/terminal-opts.h"
54 #include "ui/terminal/terminal-reader.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 static struct dataset *the_dataset;
65
66 static void add_syntax_reader (struct lexer *, const char *file_name,
67                                const char *encoding, enum lex_syntax_mode);
68 static void bug_handler(int sig);
69 static void fpu_init (void);
70 static void output_msg (const struct msg *, void *);
71
72 /* Program entry point. */
73 int
74 main (int argc, char **argv)
75 {
76   struct terminal_opts *terminal_opts;
77   struct argv_parser *parser;
78   enum lex_syntax_mode syntax_mode;
79   char *syntax_encoding;
80   bool process_statrc;
81   struct lexer *lexer;
82
83   set_program_name (argv[0]);
84
85   signal (SIGABRT, bug_handler);
86   signal (SIGSEGV, bug_handler);
87   signal (SIGFPE, bug_handler);
88
89   i18n_init ();
90   fpu_init ();
91   gsl_set_error_handler_off ();
92
93   fh_init ();
94   settings_init ();
95   terminal_check_size ();
96   random_init ();
97
98   lexer = lex_create ();
99   the_dataset = create_dataset ();
100
101   parser = argv_parser_create ();
102   terminal_opts = terminal_opts_init (parser, &syntax_mode, &process_statrc,
103                                       &syntax_encoding);
104   source_init_register_argv_parser (parser);
105   if (!argv_parser_run (parser, argc, argv))
106     exit (EXIT_FAILURE);
107   terminal_opts_done (terminal_opts, argc, argv);
108   argv_parser_destroy (parser);
109
110   msg_set_handler (output_msg, lexer);
111   dataset_set_default_syntax_encoding (the_dataset, syntax_encoding);
112
113   /* Add syntax files to source stream. */
114   if (process_statrc)
115     {
116       char *rc = include_path_search ("rc");
117       if (rc != NULL)
118         {
119           add_syntax_reader (lexer, rc, "Auto", LEX_SYNTAX_AUTO);
120           free (rc);
121         }
122     }
123   if (optind < argc)
124     {
125       int i;
126
127       for (i = optind; i < argc; i++)
128         add_syntax_reader (lexer, argv[i], syntax_encoding, syntax_mode);
129     }
130   else
131     add_syntax_reader (lexer, "-", syntax_encoding, syntax_mode);
132
133   /* Parse and execute syntax. */
134   lex_get (lexer);
135   for (;;)
136     {
137       int result = cmd_parse (lexer, the_dataset);
138
139       if (result == CMD_EOF || result == CMD_FINISH)
140         break;
141       else if (cmd_result_is_failure (result) && lex_token (lexer) != T_STOP)
142         {
143           if (lex_get_error_mode (lexer) == LEX_ERROR_STOP)
144             {
145               msg (MW, _("Error encountered while ERROR=STOP is effective."));
146               lex_discard_noninteractive (lexer);
147             }
148           else if (result == CMD_CASCADING_FAILURE
149                    && lex_get_error_mode (lexer) != LEX_ERROR_INTERACTIVE)
150             {
151               msg (SE, _("Stopping syntax file processing here to avoid "
152                          "a cascade of dependent command failures."));
153               lex_discard_noninteractive (lexer);
154             }
155         }
156
157       if (msg_ui_too_many_errors ())
158         lex_discard_noninteractive (lexer);
159     }
160
161
162   destroy_dataset (the_dataset);
163
164   random_done ();
165   settings_done ();
166   fh_done ();
167   lex_destroy (lexer);
168   output_close ();
169   i18n_done ();
170
171   return msg_ui_any_errors ();
172 }
173 \f
174 static void
175 fpu_init (void)
176 {
177 #if HAVE_FEHOLDEXCEPT
178   fenv_t foo;
179   feholdexcept (&foo);
180 #elif HAVE___SETFPUCW && defined(_FPU_IEEE)
181   __setfpucw (_FPU_IEEE);
182 #elif HAVE_FPSETMASK
183   fpsetmask (0);
184 #endif
185 }
186
187 /* If a segfault happens, issue a message to that effect and halt */
188 static void
189 bug_handler(int sig)
190 {
191   /* Reset SIG to its default handling so that if it happens again we won't
192      recurse. */
193   signal (sig, SIG_DFL);
194
195 #if DEBUGGING
196   connect_debugger ();
197 #endif
198   switch (sig)
199     {
200     case SIGABRT:
201       request_bug_report("Assertion Failure/Abort");
202       break;
203     case SIGFPE:
204       request_bug_report("Floating Point Exception");
205       break;
206     case SIGSEGV:
207       request_bug_report("Segmentation Violation");
208       break;
209     default:
210       request_bug_report("Unknown");
211       break;
212     }
213
214   /* Re-raise the signal so that we terminate with the correct status. */
215   raise (sig);
216 }
217
218 static void
219 output_msg (const struct msg *m_, void *lexer_)
220 {
221   struct lexer *lexer = lexer_;
222   struct msg m = *m_;
223
224   if (m.file_name == NULL)
225     {
226       m.file_name = CONST_CAST (char *, lex_get_file_name (lexer));
227       m.first_line = lex_get_first_line_number (lexer, 0);
228       m.last_line = lex_get_last_line_number (lexer, 0);
229     }
230
231   message_item_submit (message_item_create (&m));
232 }
233
234 static void
235 add_syntax_reader (struct lexer *lexer, const char *file_name,
236                    const char *encoding, enum lex_syntax_mode syntax_mode)
237 {
238   struct lex_reader *reader;
239
240   reader = (!strcmp (file_name, "-") && isatty (STDIN_FILENO)
241             ? terminal_reader_create ()
242             : lex_reader_for_file (file_name, encoding, syntax_mode,
243                                    LEX_ERROR_CONTINUE));
244
245   if (reader)
246     lex_append (lexer, reader);
247 }