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