settings: Make viewwidth, viewlength "int"s instead of pointers.
[pspp] / 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
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 /* Program entry point. */
79 int
80 main (int argc, char **argv)
81 {
82   struct terminal_opts *terminal_opts;
83   struct argv_parser *parser;
84
85   set_program_name (argv[0]);
86
87   signal (SIGABRT, bug_handler);
88   signal (SIGSEGV, bug_handler);
89   signal (SIGFPE, bug_handler);
90   at_fatal_signal (clean_up);
91
92   i18n_init ();
93   fpu_init ();
94   gsl_set_error_handler_off ();
95
96   fh_init ();
97   the_source_stream = create_source_stream ();
98   prompt_init ();
99   readln_initialize ();
100   settings_init ();
101   terminal_check_size ();
102   random_init ();
103
104   the_dataset = create_dataset ();
105
106   parser = argv_parser_create ();
107   terminal_opts = terminal_opts_init (parser, the_source_stream);
108   source_init_register_argv_parser (parser, the_source_stream);
109   if (!argv_parser_run (parser, argc, argv))
110     exit (EXIT_FAILURE);
111   terminal_opts_done (terminal_opts, argc, argv);
112   argv_parser_destroy (parser);
113
114   msg_ui_init (the_source_stream);
115
116   the_lexer = lex_create (the_source_stream);
117
118   for (;;)
119     {
120       int result = cmd_parse (the_lexer, the_dataset);
121
122       if (result == CMD_EOF || result == CMD_FINISH)
123         break;
124       if (result == CMD_CASCADING_FAILURE &&
125           !getl_is_interactive (the_source_stream))
126         {
127           msg (SE, _("Stopping syntax file processing here to avoid "
128                      "a cascade of dependent command failures."));
129           getl_abort_noninteractive (the_source_stream);
130         }
131       else if (msg_ui_too_many_errors ())
132         getl_abort_noninteractive (the_source_stream);
133     }
134
135
136   clean_up ();
137   return msg_ui_any_errors ();
138 }
139 \f
140
141 static void
142 fpu_init (void)
143 {
144 #if HAVE_FEHOLDEXCEPT
145   fenv_t foo;
146   feholdexcept (&foo);
147 #elif HAVE___SETFPUCW && defined(_FPU_IEEE)
148   __setfpucw (_FPU_IEEE);
149 #elif HAVE_FPSETMASK
150   fpsetmask (0);
151 #endif
152 }
153
154 /* If a segfault happens, issue a message to that effect and halt */
155 void
156 bug_handler(int sig)
157 {
158 #if DEBUGGING
159   connect_debugger ();
160 #endif
161   switch (sig)
162     {
163     case SIGABRT:
164       request_bug_report_and_abort("Assertion Failure/Abort");
165     case SIGFPE:
166       request_bug_report_and_abort("Floating Point Exception");
167     case SIGSEGV:
168       request_bug_report_and_abort("Segmentation Violation");
169     default:
170       request_bug_report_and_abort("Unknown");
171     }
172 }
173
174 /* Clean up PSPP in preparation for termination.  */
175 static void
176 clean_up (void)
177 {
178   static bool terminating = false;
179   if (!terminating)
180     {
181       terminating = true;
182
183       destroy_dataset (the_dataset);
184
185       random_done ();
186       settings_done ();
187       fh_done ();
188       lex_destroy (the_lexer);
189       destroy_source_stream (the_source_stream);
190       prompt_done ();
191       readln_uninitialize ();
192       output_close ();
193       msg_ui_done ();
194       i18n_done ();
195     }
196 }