2600a04883796c8870b63eb0ea3868dc7c465dba
[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 "command-line.h"
23 #include "msg-ui.h"
24 #include "progname.h"
25 #include "procedure.h"
26 #include "read-line.h"
27
28 #include <data/dictionary.h>
29 #include <data/file-handle-def.h>
30 #include <data/file-name.h>
31 #include <data/settings.h>
32 #include <data/variable.h>
33 #include <gsl/gsl_errno.h>
34 #include <language/command.h>
35 #include <language/lexer/lexer.h>
36 #include <language/line-buffer.h>
37 #include <libpspp/compiler.h>
38 #include <libpspp/message.h>
39 #include <libpspp/version.h>
40 #include <math/random.h>
41 #include <output/output.h>
42 #include <signal.h>
43 #include <stdio.h>
44
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 int execute_command (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
74
75 /* Program entry point. */
76 int
77 main (int argc, char **argv)
78 {
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
97   default_dict = dict_create ();
98
99   if (parse_command_line (argc, argv)) 
100     {
101       outp_read_devices ();
102       lex_init ();
103
104       for (;;)
105         {
106           int result = execute_command ();
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
123 /* Parses a command and returns the result. */
124 static int
125 execute_command (void)
126 {
127   /* Read the command's first token.  
128      The first token is part of the first line of the command. */
129   getl_set_prompt_style (GETL_PROMPT_FIRST);
130   lex_get ();
131   if (token == T_STOP)
132     return CMD_EOF;
133
134   /* Parse the command.
135      Any lines read after the first token must be continuation
136      lines. */
137   getl_set_prompt_style (GETL_PROMPT_LATER);
138   return cmd_parse (vfm_source != NULL ? CMD_STATE_DATA : CMD_STATE_INITIAL);
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   switch (sig) 
170     {
171     case SIGFPE:
172       request_bug_report_and_abort("Floating Point Exception");
173       break;
174     case SIGSEGV:
175       request_bug_report_and_abort("Segmentation Violation");
176       break;
177     default:
178       request_bug_report_and_abort("");
179       break;
180     }
181 }
182
183 void 
184 interrupt_handler(int sig UNUSED)
185 {
186   terminate (false);
187 }
188
189
190 /* Terminate PSPP.  SUCCESS should be true to exit successfully,
191    false to exit as a failure.  */
192 static void
193 terminate (bool success)
194 {
195   static bool terminating = false;
196   if (!terminating) 
197     {
198       terminating = true;
199
200       cancel_transformations ();
201       dict_destroy (default_dict);
202
203       random_done ();
204       settings_done ();
205       fh_done ();
206       lex_done ();
207       getl_uninitialize ();
208       readln_uninitialize ();
209
210       outp_done ();
211       msg_ui_done ();
212     }
213   exit (success ? EXIT_SUCCESS : EXIT_FAILURE);
214 }