6a7decd9d95d77192488ba0ba66ea8a2b0266154
[pspp-builds.git] / src / ui / terminal / main.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21
22 #include <signal.h>
23 #include <stdio.h>
24
25 #include "command-line.h"
26 #include "msg-ui.h"
27 #include "progname.h"
28 #include "read-line.h"
29
30 #include <data/dictionary.h>
31 #include <data/file-handle-def.h>
32 #include <data/file-name.h>
33 #include <data/procedure.h>
34 #include <data/settings.h>
35 #include <data/variable.h>
36 #include <gsl/gsl_errno.h>
37 #include <language/command.h>
38 #include <language/lexer/lexer.h>
39 #include <language/line-buffer.h>
40 #include <libpspp/compiler.h>
41 #include <libpspp/message.h>
42 #include <libpspp/version.h>
43 #include <math/random.h>
44 #include <output/output.h>
45
46 #if HAVE_FPU_CONTROL_H
47 #include <fpu_control.h>
48 #endif
49
50 #if HAVE_LOCALE_H
51 #include <locale.h>
52 #endif
53
54 #if HAVE_FENV_H
55 #include <fenv.h>
56 #endif
57
58 #include "gettext.h"
59 #define _(msgid) gettext (msgid)
60
61 #include <stdlib.h>
62
63 static void i18n_init (void);
64 static void fpu_init (void);
65 static void terminate (bool success) NO_RETURN;
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
75
76 /* Program entry point. */
77 int
78 main (int argc, char **argv)
79 {
80   signal (SIGABRT, bug_handler);
81   signal (SIGSEGV, bug_handler);
82   signal (SIGFPE, bug_handler);
83   signal (SIGINT, interrupt_handler);
84
85   set_program_name ("pspp");
86   i18n_init ();
87   fpu_init ();
88   gsl_set_error_handler_off ();
89
90   outp_init ();
91   msg_ui_init ();
92   fn_init ();
93   fh_init ();
94   getl_initialize ();
95   readln_initialize ();
96   settings_init ();
97   random_init ();
98   the_dataset = create_dataset ();
99
100   if (parse_command_line (argc, argv)) 
101     {
102       outp_read_devices ();
103       lex_init (do_read_line);
104
105       for (;;)
106         {
107           int result = cmd_parse (the_dataset, 
108                                   proc_has_source (the_dataset)
109                                   ? CMD_STATE_DATA : CMD_STATE_INITIAL);
110           if (result == CMD_EOF || result == CMD_FINISH)
111             break;
112           if (result == CMD_CASCADING_FAILURE && !getl_is_interactive ())
113             {
114               msg (SE, _("Stopping syntax file processing here to avoid "
115                          "a cascade of dependent command failures."));
116               getl_abort_noninteractive (); 
117             }
118           else
119             check_msg_count ();
120         }
121     }
122   
123   terminate (!any_errors ());
124 }
125 \f
126 static void
127 i18n_init (void) 
128 {
129 #if ENABLE_NLS
130 #if HAVE_LC_MESSAGES
131   setlocale (LC_MESSAGES, "");
132 #endif
133   setlocale (LC_MONETARY, "");
134   bindtextdomain (PACKAGE, locale_dir);
135   textdomain (PACKAGE);
136 #endif /* ENABLE_NLS */
137 }
138
139 static void
140 fpu_init (void) 
141 {
142 #if HAVE_FEHOLDEXCEPT
143   fenv_t foo;
144   feholdexcept (&foo);
145 #elif HAVE___SETFPUCW && defined(_FPU_IEEE)
146   __setfpucw (_FPU_IEEE);
147 #endif
148 }
149
150 /* If a segfault happens, issue a message to that effect and halt */
151 void 
152 bug_handler(int sig)
153 {
154   switch (sig) 
155     {
156     case SIGABRT:
157       request_bug_report_and_abort("Assertion Failure/Abort");
158     case SIGFPE:
159       request_bug_report_and_abort("Floating Point Exception");
160     case SIGSEGV:
161       request_bug_report_and_abort("Segmentation Violation");
162     default:
163       request_bug_report_and_abort("Unknown");
164     }
165 }
166
167 void 
168 interrupt_handler(int sig UNUSED)
169 {
170   terminate (false);
171 }
172
173
174 /* Terminate PSPP.  SUCCESS should be true to exit successfully,
175    false to exit as a failure.  */
176 static void
177 terminate (bool success)
178 {
179   static bool terminating = false;
180   if (!terminating) 
181     {
182       terminating = true;
183
184       destroy_dataset (the_dataset);
185
186       random_done ();
187       settings_done ();
188       fh_done ();
189       lex_done ();
190       getl_uninitialize ();
191       readln_uninitialize ();
192
193       outp_done ();
194       msg_ui_done ();
195     }
196   exit (success ? EXIT_SUCCESS : EXIT_FAILURE);
197 }