pspp: Make style slightly more like the rest of the program.
[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
32 #include "data/dictionary.h"
33 #include "data/file-handle-def.h"
34 #include "data/file-name.h"
35 #include "data/procedure.h"
36 #include "data/settings.h"
37 #include "data/variable.h"
38 #include "gsl/gsl_errno.h"
39 #include "language/command.h"
40 #include "language/lexer/lexer.h"
41 #include "language/prompt.h"
42 #include "libpspp/argv-parser.h"
43 #include "libpspp/compiler.h"
44 #include "libpspp/getl.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 "ui/debugger.h"
51 #include "ui/source-init-opts.h"
52 #include "ui/terminal/msg-ui.h"
53 #include "ui/terminal/read-line.h"
54 #include "ui/terminal/terminal-opts.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 = NULL;
65
66 static struct lexer *the_lexer;
67 static struct source_stream *the_source_stream ;
68
69 static void bug_handler(int sig);
70 static void fpu_init (void);
71 static void clean_up (void);
72
73 /* Program entry point. */
74 int
75 main (int argc, char **argv)
76 {
77   struct terminal_opts *terminal_opts;
78   struct argv_parser *parser;
79
80   set_program_name (argv[0]);
81
82   signal (SIGABRT, bug_handler);
83   signal (SIGSEGV, bug_handler);
84   signal (SIGFPE, bug_handler);
85   at_fatal_signal (clean_up);
86
87   i18n_init ();
88   fpu_init ();
89   gsl_set_error_handler_off ();
90
91   fh_init ();
92   the_source_stream = create_source_stream ();
93   prompt_init ();
94   readln_initialize ();
95   settings_init ();
96   terminal_check_size ();
97   random_init ();
98
99   the_dataset = create_dataset ();
100
101   parser = argv_parser_create ();
102   terminal_opts = terminal_opts_init (parser, the_source_stream);
103   source_init_register_argv_parser (parser, the_source_stream);
104   if (!argv_parser_run (parser, argc, argv))
105     exit (EXIT_FAILURE);
106   terminal_opts_done (terminal_opts, argc, argv);
107   argv_parser_destroy (parser);
108
109   msg_ui_init (the_source_stream);
110
111   the_lexer = lex_create (the_source_stream);
112
113   for (;;)
114     {
115       int result = cmd_parse (the_lexer, the_dataset);
116
117       if (result == CMD_EOF || result == CMD_FINISH)
118         break;
119       if (result == CMD_CASCADING_FAILURE &&
120           !getl_is_interactive (the_source_stream))
121         {
122           msg (SE, _("Stopping syntax file processing here to avoid "
123                      "a cascade of dependent command failures."));
124           getl_abort_noninteractive (the_source_stream);
125         }
126       else if (msg_ui_too_many_errors ())
127         getl_abort_noninteractive (the_source_stream);
128     }
129
130
131   clean_up ();
132   return msg_ui_any_errors ();
133 }
134
135 static void
136 fpu_init (void)
137 {
138 #if HAVE_FEHOLDEXCEPT
139   fenv_t foo;
140   feholdexcept (&foo);
141 #elif HAVE___SETFPUCW && defined(_FPU_IEEE)
142   __setfpucw (_FPU_IEEE);
143 #elif HAVE_FPSETMASK
144   fpsetmask (0);
145 #endif
146 }
147
148 /* If a segfault happens, issue a message to that effect and halt */
149 static void
150 bug_handler(int sig)
151 {
152   /* Reset SIG to its default handling so that if it happens again we won't
153      recurse. */
154   signal (sig, SIG_DFL);
155
156 #if DEBUGGING
157   connect_debugger ();
158 #endif
159   switch (sig)
160     {
161     case SIGABRT:
162       request_bug_report("Assertion Failure/Abort");
163       break;
164     case SIGFPE:
165       request_bug_report("Floating Point Exception");
166       break;
167     case SIGSEGV:
168       request_bug_report("Segmentation Violation");
169       break;
170     default:
171       request_bug_report("Unknown");
172       break;
173     }
174
175   /* Re-raise the signal so that we terminate with the correct status. */
176   raise (sig);
177 }
178
179 /* Clean up PSPP in preparation for termination.  */
180 static void
181 clean_up (void)
182 {
183   static bool terminating = false;
184   if (!terminating)
185     {
186       terminating = true;
187
188       destroy_dataset (the_dataset);
189
190       random_done ();
191       settings_done ();
192       fh_done ();
193       lex_destroy (the_lexer);
194       destroy_source_stream (the_source_stream);
195       prompt_done ();
196       readln_uninitialize ();
197       output_close ();
198       msg_ui_done ();
199       i18n_done ();
200     }
201 }