Make GtkItemEntry compatible with GTK+ 2.17.4 and later.
[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 <locale.h>
20 #include <signal.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #if HAVE_FPU_CONTROL_H
24 #include <fpu_control.h>
25 #endif
26 #if HAVE_FENV_H
27 #include <fenv.h>
28 #endif
29 #if HAVE_IEEEFP_H
30 #include <ieeefp.h>
31 #endif
32
33 #include <data/dictionary.h>
34 #include <data/file-handle-def.h>
35 #include <libpspp/getl.h>
36 #include <data/file-name.h>
37 #include <data/procedure.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/prompt.h>
44 #include <libpspp/compiler.h>
45 #include <libpspp/message.h>
46 #include <libpspp/version.h>
47 #include <math/random.h>
48 #include <output/output.h>
49 #include <ui/debugger.h>
50 #include <ui/terminal/command-line.h>
51 #include <ui/terminal/msg-ui.h>
52 #include <ui/terminal/read-line.h>
53 #include <ui/terminal/terminal.h>
54
55 #include "fatal-signal.h"
56 #include "progname.h"
57 #include "relocatable.h"
58
59 #include "gettext.h"
60 #define _(msgid) gettext (msgid)
61
62
63 static void i18n_init (void);
64 static void fpu_init (void);
65 static void clean_up (void);
66
67 /* If a segfault happens, issue a message to that effect and halt */
68 void bug_handler(int sig);
69
70 /* Handle quit/term/int signals */
71 void interrupt_handler(int sig);
72 static struct dataset * the_dataset = NULL;
73
74 static struct lexer *the_lexer;
75 static struct source_stream *the_source_stream ;
76
77 /* Program entry point. */
78 int
79 main (int argc, char **argv)
80 {
81   int *view_width_p, *view_length_p;
82
83   set_program_name (argv[0]);
84
85   signal (SIGABRT, bug_handler);
86   signal (SIGSEGV, bug_handler);
87   signal (SIGFPE, bug_handler);
88   at_fatal_signal (clean_up);
89
90   i18n_init ();
91   fpu_init ();
92   gsl_set_error_handler_off ();
93
94   outp_init ();
95   fn_init ();
96   fh_init ();
97   the_source_stream =
98     create_source_stream (
99                           fn_getenv_default ("STAT_INCLUDE_PATH", include_path)
100                           );
101   prompt_init ();
102   readln_initialize ();
103   terminal_init (&view_width_p, &view_length_p);
104   settings_init (view_width_p, view_length_p);
105   random_init ();
106
107   the_dataset = create_dataset ();
108
109   if (parse_command_line (argc, argv, the_source_stream))
110     {
111       msg_ui_init (the_source_stream);
112       if (!settings_get_testing_mode ())
113         outp_read_devices ();
114       else
115         outp_configure_driver_line (
116           ss_cstr ("raw-ascii:ascii:listing:width=9999 length=9999 "
117                    "output-file=\"pspp.list\" emphasis=none "
118                    "headers=off paginate=off squeeze=on "
119                    "top-margin=0 bottom-margin=0"));
120       the_lexer = lex_create (the_source_stream);
121
122       for (;;)
123         {
124           int result = cmd_parse (the_lexer, the_dataset);
125
126           if (result == CMD_EOF || result == CMD_FINISH)
127             break;
128           if (result == CMD_CASCADING_FAILURE &&
129               !getl_is_interactive (the_source_stream))
130             {
131               msg (SE, _("Stopping syntax file processing here to avoid "
132                          "a cascade of dependent command failures."));
133               getl_abort_noninteractive (the_source_stream);
134             }
135           else
136             check_msg_count (the_source_stream);
137         }
138     }
139
140   clean_up ();
141   return any_errors ();
142 }
143 \f
144 static void
145 i18n_init (void)
146 {
147 #if ENABLE_NLS
148 #if HAVE_LC_MESSAGES
149   setlocale (LC_MESSAGES, "");
150 #endif
151 #if HAVE_LC_PAPER
152   setlocale (LC_PAPER, "");
153 #endif
154   bindtextdomain (PACKAGE, relocate (locale_dir));
155   textdomain (PACKAGE);
156 #endif /* ENABLE_NLS */
157 }
158
159 static void
160 fpu_init (void)
161 {
162 #if HAVE_FEHOLDEXCEPT
163   fenv_t foo;
164   feholdexcept (&foo);
165 #elif HAVE___SETFPUCW && defined(_FPU_IEEE)
166   __setfpucw (_FPU_IEEE);
167 #elif HAVE_FPSETMASK
168   fpsetmask (0);
169 #endif
170 }
171
172 /* If a segfault happens, issue a message to that effect and halt */
173 void
174 bug_handler(int sig)
175 {
176 #if DEBUGGING
177   connect_debugger ();
178 #endif
179   switch (sig)
180     {
181     case SIGABRT:
182       request_bug_report_and_abort("Assertion Failure/Abort");
183     case SIGFPE:
184       request_bug_report_and_abort("Floating Point Exception");
185     case SIGSEGV:
186       request_bug_report_and_abort("Segmentation Violation");
187     default:
188       request_bug_report_and_abort("Unknown");
189     }
190 }
191
192 /* Clean up PSPP in preparation for termination.  */
193 static void
194 clean_up (void)
195 {
196   static bool terminating = false;
197   if (!terminating)
198     {
199       terminating = true;
200
201       destroy_dataset (the_dataset);
202
203       random_done ();
204       settings_done ();
205       fh_done ();
206       lex_destroy (the_lexer);
207       destroy_source_stream (the_source_stream);
208       prompt_done ();
209       readln_uninitialize ();
210       outp_done ();
211       msg_ui_done ();
212     }
213 }