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