Encapsulated the static data of procedure.[ch] into a single object, to be
[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
73
74 /* Program entry point. */
75 int
76 main (int argc, char **argv)
77 {
78   signal (SIGABRT, bug_handler);
79   signal (SIGSEGV, bug_handler);
80   signal (SIGFPE, bug_handler);
81   signal (SIGINT, interrupt_handler);
82
83   set_program_name ("pspp");
84   i18n_init ();
85   fpu_init ();
86   gsl_set_error_handler_off ();
87
88   outp_init ();
89   msg_ui_init ();
90   fn_init ();
91   fh_init ();
92   getl_initialize ();
93   readln_initialize ();
94   settings_init ();
95   random_init ();
96   current_dataset = create_dataset ();
97
98   if (parse_command_line (argc, argv)) 
99     {
100       outp_read_devices ();
101       lex_init ();
102
103       for (;;)
104         {
105           int result = cmd_parse (proc_has_source (current_dataset)
106                                   ? CMD_STATE_DATA : CMD_STATE_INITIAL);
107           if (result == CMD_EOF || result == CMD_FINISH)
108             break;
109           if (result == CMD_CASCADING_FAILURE && !getl_is_interactive ())
110             {
111               msg (SE, _("Stopping syntax file processing here to avoid "
112                          "a cascade of dependent command failures."));
113               getl_abort_noninteractive (); 
114             }
115           else
116             check_msg_count ();
117         }
118     }
119   
120   terminate (!any_errors ());
121 }
122 \f
123 static void
124 i18n_init (void) 
125 {
126 #if ENABLE_NLS
127 #if HAVE_LC_MESSAGES
128   setlocale (LC_MESSAGES, "");
129 #endif
130   setlocale (LC_MONETARY, "");
131   bindtextdomain (PACKAGE, locale_dir);
132   textdomain (PACKAGE);
133 #endif /* ENABLE_NLS */
134 }
135
136 static void
137 fpu_init (void) 
138 {
139 #if HAVE_FEHOLDEXCEPT
140   fenv_t foo;
141   feholdexcept (&foo);
142 #elif HAVE___SETFPUCW && defined(_FPU_IEEE)
143   __setfpucw (_FPU_IEEE);
144 #endif
145 }
146
147 /* If a segfault happens, issue a message to that effect and halt */
148 void 
149 bug_handler(int sig)
150 {
151   switch (sig) 
152     {
153     case SIGABRT:
154       request_bug_report_and_abort("Assertion Failure/Abort");
155     case SIGFPE:
156       request_bug_report_and_abort("Floating Point Exception");
157     case SIGSEGV:
158       request_bug_report_and_abort("Segmentation Violation");
159     default:
160       request_bug_report_and_abort("Unknown");
161     }
162 }
163
164 void 
165 interrupt_handler(int sig UNUSED)
166 {
167   terminate (false);
168 }
169
170
171 /* Terminate PSPP.  SUCCESS should be true to exit successfully,
172    false to exit as a failure.  */
173 static void
174 terminate (bool success)
175 {
176   static bool terminating = false;
177   if (!terminating) 
178     {
179       terminating = true;
180
181       destroy_dataset (current_dataset);
182
183       random_done ();
184       settings_done ();
185       fh_done ();
186       lex_done ();
187       getl_uninitialize ();
188       readln_uninitialize ();
189
190       outp_done ();
191       msg_ui_done ();
192     }
193   exit (success ? EXIT_SUCCESS : EXIT_FAILURE);
194 }