Continue reforming procedure execution. In this phase, get rid of
[pspp] / 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 "procedure.h"
29 #include "read-line.h"
30
31 #include <data/dictionary.h>
32 #include <data/file-handle-def.h>
33 #include <data/file-name.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 #include <procedure.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 int execute_command (void);
67 static void terminate (bool success) NO_RETURN;
68
69 /* If a segfault happens, issue a message to that effect and halt */
70 void bug_handler(int sig);
71
72 /* Handle quit/term/int signals */
73 void interrupt_handler(int sig);
74
75
76 /* Program entry point. */
77 int
78 main (int argc, char **argv)
79 {
80   signal (SIGSEGV, bug_handler);
81   signal (SIGFPE, bug_handler);
82   signal (SIGINT, interrupt_handler);
83
84   set_program_name ("pspp");
85   i18n_init ();
86   fpu_init ();
87   gsl_set_error_handler_off ();
88
89   outp_init ();
90   msg_ui_init ();
91   fn_init ();
92   fh_init ();
93   getl_initialize ();
94   readln_initialize ();
95   settings_init ();
96   random_init ();
97   proc_init ();
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 (proc_has_source ()
139                     ? CMD_STATE_DATA : CMD_STATE_INITIAL);
140 }
141 \f
142 static void
143 i18n_init (void) 
144 {
145 #if ENABLE_NLS
146 #if HAVE_LC_MESSAGES
147   setlocale (LC_MESSAGES, "");
148 #endif
149   setlocale (LC_MONETARY, "");
150   bindtextdomain (PACKAGE, locale_dir);
151   textdomain (PACKAGE);
152 #endif /* ENABLE_NLS */
153 }
154
155 static void
156 fpu_init (void) 
157 {
158 #if HAVE_FEHOLDEXCEPT
159   fenv_t foo;
160   feholdexcept (&foo);
161 #elif HAVE___SETFPUCW && defined(_FPU_IEEE)
162   __setfpucw (_FPU_IEEE);
163 #endif
164 }
165
166 /* If a segfault happens, issue a message to that effect and halt */
167 void 
168 bug_handler(int sig)
169 {
170   switch (sig) 
171     {
172     case SIGFPE:
173       request_bug_report_and_abort("Floating Point Exception");
174       break;
175     case SIGSEGV:
176       request_bug_report_and_abort("Segmentation Violation");
177       break;
178     default:
179       request_bug_report_and_abort("");
180       break;
181     }
182 }
183
184 void 
185 interrupt_handler(int sig UNUSED)
186 {
187   terminate (false);
188 }
189
190
191 /* Terminate PSPP.  SUCCESS should be true to exit successfully,
192    false to exit as a failure.  */
193 static void
194 terminate (bool success)
195 {
196   static bool terminating = false;
197   if (!terminating) 
198     {
199       terminating = true;
200
201       proc_done ();
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 }