Completely rewrite src/data/format.[ch], to achieve better
[pspp-builds.git] / src / ui / terminal / main.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000, 2006 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/format.h>
34 #include <data/procedure.h>
35 #include <data/settings.h>
36 #include <data/variable.h>
37 #include <gsl/gsl_errno.h>
38 #include <language/command.h>
39 #include <language/lexer/lexer.h>
40 #include <language/line-buffer.h>
41 #include <libpspp/compiler.h>
42 #include <libpspp/message.h>
43 #include <libpspp/version.h>
44 #include <math/random.h>
45 #include <output/output.h>
46
47 #if HAVE_FPU_CONTROL_H
48 #include <fpu_control.h>
49 #endif
50
51 #if HAVE_LOCALE_H
52 #include <locale.h>
53 #endif
54
55 #if HAVE_FENV_H
56 #include <fenv.h>
57 #endif
58
59 #include "gettext.h"
60 #define _(msgid) gettext (msgid)
61
62 #include <stdlib.h>
63
64 static void i18n_init (void);
65 static void fpu_init (void);
66 static void terminate (bool success) NO_RETURN;
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
76
77 /* Program entry point. */
78 int
79 main (int argc, char **argv)
80 {
81   signal (SIGABRT, bug_handler);
82   signal (SIGSEGV, bug_handler);
83   signal (SIGFPE, bug_handler);
84   signal (SIGINT, interrupt_handler);
85
86   set_program_name ("pspp");
87   i18n_init ();
88   fpu_init ();
89   gsl_set_error_handler_off ();
90
91   fmt_init ();
92   outp_init ();
93   msg_ui_init ();
94   fn_init ();
95   fh_init ();
96   getl_initialize ();
97   readln_initialize ();
98   settings_init ();
99   random_init ();
100   the_dataset = create_dataset ();
101
102   if (parse_command_line (argc, argv)) 
103     {
104       outp_read_devices ();
105       lex_init (do_read_line);
106
107       for (;;)
108         {
109           int result = cmd_parse (the_dataset, 
110                                   proc_has_source (the_dataset)
111                                   ? CMD_STATE_DATA : CMD_STATE_INITIAL);
112           if (result == CMD_EOF || result == CMD_FINISH)
113             break;
114           if (result == CMD_CASCADING_FAILURE && !getl_is_interactive ())
115             {
116               msg (SE, _("Stopping syntax file processing here to avoid "
117                          "a cascade of dependent command failures."));
118               getl_abort_noninteractive (); 
119             }
120           else
121             check_msg_count ();
122         }
123     }
124   
125   terminate (!any_errors ());
126 }
127 \f
128 static void
129 i18n_init (void) 
130 {
131 #if ENABLE_NLS
132 #if HAVE_LC_MESSAGES
133   setlocale (LC_MESSAGES, "");
134 #endif
135   setlocale (LC_MONETARY, "");
136   bindtextdomain (PACKAGE, locale_dir);
137   textdomain (PACKAGE);
138 #endif /* ENABLE_NLS */
139 }
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 #endif
150 }
151
152 /* If a segfault happens, issue a message to that effect and halt */
153 void 
154 bug_handler(int sig)
155 {
156   switch (sig) 
157     {
158     case SIGABRT:
159       request_bug_report_and_abort("Assertion Failure/Abort");
160     case SIGFPE:
161       request_bug_report_and_abort("Floating Point Exception");
162     case SIGSEGV:
163       request_bug_report_and_abort("Segmentation Violation");
164     default:
165       request_bug_report_and_abort("Unknown");
166     }
167 }
168
169 void 
170 interrupt_handler(int sig UNUSED)
171 {
172   terminate (false);
173 }
174
175
176 /* Terminate PSPP.  SUCCESS should be true to exit successfully,
177    false to exit as a failure.  */
178 static void
179 terminate (bool success)
180 {
181   static bool terminating = false;
182   if (!terminating) 
183     {
184       terminating = true;
185
186       destroy_dataset (the_dataset);
187
188       random_done ();
189       settings_done ();
190       fh_done ();
191       lex_done ();
192       getl_uninitialize ();
193       readln_uninitialize ();
194
195       outp_done ();
196       msg_ui_done ();
197     }
198   exit (success ? EXIT_SUCCESS : EXIT_FAILURE);
199 }