a1f201697193e04282e5726fc3b4493050fecbb1
[pspp-builds.git] / src / ui / terminal / main.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2007, 2009 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
32
33 #include <libpspp/i18n.h>
34 #include <data/dictionary.h>
35 #include <data/file-handle-def.h>
36 #include <libpspp/getl.h>
37 #include <data/file-name.h>
38 #include <data/procedure.h>
39 #include <data/settings.h>
40 #include <data/variable.h>
41 #include <gsl/gsl_errno.h>
42 #include <language/command.h>
43 #include <language/lexer/lexer.h>
44 #include <language/prompt.h>
45 #include <libpspp/compiler.h>
46 #include <libpspp/message.h>
47 #include <libpspp/version.h>
48 #include <math/random.h>
49 #include <output/output.h>
50 #include <ui/debugger.h>
51 #include <ui/terminal/msg-ui.h>
52 #include <ui/terminal/read-line.h>
53 #include <ui/terminal/terminal.h>
54 #include <ui/terminal/terminal-opts.h>
55 #include <ui/command-line.h>
56 #include <ui/source-init-opts.h>
57
58 #include "fatal-signal.h"
59 #include "progname.h"
60
61 #include "gettext.h"
62 #define _(msgid) gettext (msgid)
63
64
65 static void fpu_init (void);
66 static void clean_up (void);
67
68 /* If a segfault happens, issue a message to that effect and halt */
69 void bug_handler(int sig);
70
71 /* Handle quit/term/int signals */
72 void interrupt_handler(int sig);
73 static struct dataset * the_dataset = NULL;
74
75 static struct lexer *the_lexer;
76 static struct source_stream *the_source_stream ;
77
78 const char *argp_program_version = version;
79 const char *argp_program_bug_address = PACKAGE_BUGREPORT;
80
81 /* Program entry point. */
82 int
83 main (int argc, char **argv)
84 {
85   int *view_width_p, *view_length_p;
86   struct command_line_processor *clp;
87   set_program_name (argv[0]);
88
89   signal (SIGABRT, bug_handler);
90   signal (SIGSEGV, bug_handler);
91   signal (SIGFPE, bug_handler);
92   at_fatal_signal (clean_up);
93
94   i18n_init ();
95   fpu_init ();
96   gsl_set_error_handler_off ();
97
98   outp_init ();
99   fn_init ();
100   fh_init ();
101   the_source_stream =
102     create_source_stream (
103                           fn_getenv_default ("STAT_INCLUDE_PATH", include_path)
104                           );
105   prompt_init ();
106   readln_initialize ();
107   terminal_init (&view_width_p, &view_length_p);
108   settings_init (view_width_p, view_length_p);
109   random_init ();
110
111   the_dataset = create_dataset ();
112
113
114
115   clp = command_line_processor_create (_("PSPP --- A program for statistical analysis"),
116                                        _("FILE1, FILE2 ... FILEn"), NULL);
117
118   command_line_processor_add_options (clp, &io_argp,
119                                       _("Options affecting input and output locations:"), the_source_stream);
120
121   command_line_processor_add_options (clp, &test_argp,
122                                       _("Diagnositic options:"), the_source_stream);
123
124   command_line_processor_add_options (clp, &post_init_argp,
125                                       _("Options affecting syntax and behavior:"), the_source_stream);
126
127   command_line_processor_parse (clp, argc, argv);
128
129   msg_ui_init (the_source_stream);
130
131   if (!settings_get_testing_mode ())
132     {
133       outp_read_devices ();
134     }
135   else
136     {
137       outp_configure_driver_line
138         (
139          ss_cstr ("raw-ascii:ascii:listing:width=9999 length=9999 "
140                   "output-file=\"pspp.list\" emphasis=none "
141                   "headers=off paginate=off squeeze=on "
142                   "top-margin=0 bottom-margin=0"));
143     }
144
145   the_lexer = lex_create (the_source_stream);
146
147   for (;;)
148     {
149       int result = cmd_parse (the_lexer, the_dataset);
150
151       if (result == CMD_EOF || result == CMD_FINISH)
152         break;
153       if (result == CMD_CASCADING_FAILURE &&
154           !getl_is_interactive (the_source_stream))
155         {
156           msg (SE, _("Stopping syntax file processing here to avoid "
157                      "a cascade of dependent command failures."));
158           getl_abort_noninteractive (the_source_stream);
159         }
160       else
161         check_msg_count (the_source_stream);
162     }
163
164
165   clean_up ();
166   return any_errors ();
167 }
168 \f
169 static void
170 fpu_init (void)
171 {
172 #if HAVE_FEHOLDEXCEPT
173   fenv_t foo;
174   feholdexcept (&foo);
175 #elif HAVE___SETFPUCW && defined(_FPU_IEEE)
176   __setfpucw (_FPU_IEEE);
177 #elif HAVE_FPSETMASK
178   fpsetmask (0);
179 #endif
180 }
181
182 /* If a segfault happens, issue a message to that effect and halt */
183 void
184 bug_handler(int sig)
185 {
186 #if DEBUGGING
187   connect_debugger ();
188 #endif
189   switch (sig)
190     {
191     case SIGABRT:
192       request_bug_report_and_abort("Assertion Failure/Abort");
193     case SIGFPE:
194       request_bug_report_and_abort("Floating Point Exception");
195     case SIGSEGV:
196       request_bug_report_and_abort("Segmentation Violation");
197     default:
198       request_bug_report_and_abort("Unknown");
199     }
200 }
201
202 /* Clean up PSPP in preparation for termination.  */
203 static void
204 clean_up (void)
205 {
206   static bool terminating = false;
207   if (!terminating)
208     {
209       terminating = true;
210
211       destroy_dataset (the_dataset);
212
213       random_done ();
214       settings_done ();
215       fh_done ();
216       lex_destroy (the_lexer);
217       destroy_source_stream (the_source_stream);
218       prompt_done ();
219       readln_uninitialize ();
220       outp_done ();
221       msg_ui_done ();
222       i18n_done ();
223     }
224 }