Continue reforming procedure execution. In this phase, move
[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 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   proc_init ();
97
98   if (parse_command_line (argc, argv)) 
99     {
100       outp_read_devices ();
101       lex_init ();
102
103       for (;;)
104         {
105           int result = execute_command ();
106           if (result == CMD_EOF || result == CMD_FINISH)
107             break;
108           if (result == CMD_CASCADING_FAILURE && !getl_is_interactive ())
109             {
110               msg (SE, _("Stopping syntax file processing here to avoid "
111                          "a cascade of dependent command failures."));
112               getl_abort_noninteractive (); 
113             }
114           else
115             check_msg_count ();
116         }
117     }
118   
119   terminate (!any_errors ());
120 }
121
122 /* Parses a command and returns the result. */
123 static int
124 execute_command (void)
125 {
126   /* Read the command's first token.  
127      The first token is part of the first line of the command. */
128   getl_set_prompt_style (GETL_PROMPT_FIRST);
129   lex_get ();
130   if (token == T_STOP)
131     return CMD_EOF;
132
133   /* Parse the command.
134      Any lines read after the first token must be continuation
135      lines. */
136   getl_set_prompt_style (GETL_PROMPT_LATER);
137   return cmd_parse (proc_has_source ()
138                     ? 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       proc_done ();
201
202       random_done ();
203       settings_done ();
204       fh_done ();
205       lex_done ();
206       getl_uninitialize ();
207       readln_uninitialize ();
208
209       outp_done ();
210       msg_ui_done ();
211     }
212   exit (success ? EXIT_SUCCESS : EXIT_FAILURE);
213 }