Added new files resulting from directory restructuring.
[pspp-builds.git] / src / 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 #include "main.h"
22 #include <gsl/gsl_errno.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include "cmdline.h"
26 #include "command.h"
27 #include "dictionary.h"
28 #include "error.h"
29 #include "file-handle-def.h"
30 #include "filename.h"
31 #include "getl.h"
32 #include "glob.h"
33 #include "lexer.h"
34 #include "output.h"
35 #include "progname.h"
36 #include "random.h"
37 #include "readln.h"
38 #include "settings.h"
39 #include "var.h"
40 #include "version.h"
41
42 #if HAVE_FPU_CONTROL_H
43 #include <fpu_control.h>
44 #endif
45
46 #if HAVE_LOCALE_H
47 #include <locale.h>
48 #endif
49
50 #if HAVE_FENV_H
51 #include <fenv.h>
52 #endif
53
54 #include "gettext.h"
55 #define _(msgid) gettext (msgid)
56
57 #include <stdlib.h>
58
59 #include "debug-print.h"
60
61 static void i18n_init (void);
62 static void fpu_init (void);
63 static void handle_error (int code);
64 static int execute_command (void);
65
66 /* Whether FINISH. has been executed. */
67 int finished;
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 /* Whether we're dropping down to interactive mode immediately because
76    we hit end-of-file unexpectedly (or whatever). */
77 int start_interactive;
78
79 /* Program entry point. */
80 int
81 main (int argc, char **argv)
82 {
83   signal (SIGSEGV, bug_handler);
84   signal (SIGFPE, bug_handler);
85   signal (SIGINT, interrupt_handler);
86
87   set_program_name ("pspp");
88   i18n_init ();
89   fpu_init ();
90   gsl_set_error_handler_off ();
91
92   outp_init ();
93   fn_init ();
94   fh_init ();
95   getl_initialize ();
96   readln_initialize ();
97   settings_init ();
98   random_init ();
99
100   default_dict = dict_create ();
101
102   parse_command_line (argc, argv);
103   outp_read_devices ();
104
105   lex_init ();
106
107   while (!finished)
108     {
109       err_check_count ();
110       handle_error (execute_command ());
111     }
112
113   terminate (err_error_count == 0);
114   abort ();
115 }
116
117 /* Terminate PSPP.  SUCCESS should be true to exit successfully,
118    false to exit as a failure.  */
119 void
120 terminate (bool success)
121 {
122   static bool terminating = false;
123   if (terminating)
124     return;
125   terminating = true;
126
127   err_done ();
128   outp_done ();
129
130   cancel_transformations ();
131   dict_destroy (default_dict);
132
133   random_done ();
134   settings_done ();
135   fh_done ();
136   lex_done ();
137   getl_uninitialize ();
138
139   exit (success ? EXIT_SUCCESS : EXIT_FAILURE);
140 }
141
142 /* Parse and execute a command, returning its return code. */
143 static int
144 execute_command (void)
145 {
146   int result;
147   
148   /* Read the command's first token.
149      We may hit end of file.
150      If so, give the line reader a chance to proceed to the next file.
151      End of file is not handled transparently since the user may want
152      the dictionary cleared between files. */
153   getl_prompt = GETL_PRPT_STANDARD;
154   for (;;)
155     {
156       lex_get ();
157       if (token != T_STOP)
158         break;
159
160       /* Sets the options flag of the current script to 0, thus allowing it
161          to be read in.  Returns nonzero if this action was taken, zero
162          otherwise. */
163       if (getl_head && getl_head->separate)
164         {
165           getl_head->separate = 0;
166           discard_variables ();
167           lex_reset_eof ();
168         }
169       else
170         terminate (err_error_count == 0);
171     }
172
173   /* Parse the command. */
174   getl_prompt = GETL_PRPT_CONTINUATION;
175   result =  cmd_parse ();
176  
177   /* Unset the /ALGORITHM subcommand if it was used */
178   unset_cmd_algorithm ();
179
180   /* Clear any auxiliary data from the dictionary. */
181   dict_clear_aux (default_dict);
182
183   return result;
184 }
185
186 /* Print an error message corresponding to the command return code
187    CODE. */
188 static void
189 handle_error (int code)
190 {
191   switch (code)
192     {
193     case CMD_SUCCESS:
194       return;
195           
196     case CMD_FAILURE:
197       msg (SW,  _("This command not executed."));
198       break;
199
200     case CMD_PART_SUCCESS_MAYBE:
201       msg (SW, _("Skipping the rest of this command.  Part of "
202                  "this command may have been executed."));
203       break;
204                   
205     case CMD_PART_SUCCESS:
206       msg (SW, _("Skipping the rest of this command.  This "
207                  "command was fully executed up to this point."));
208       break;
209
210     case CMD_TRAILING_GARBAGE:
211       msg (SW, _("Trailing garbage was encountered following "
212                  "this command.  The command was fully executed "
213                  "to this point."));
214       break;
215
216     default:
217       assert (0);
218     }
219
220   if (getl_reading_script())
221     {
222       err_break ();
223       while (token != T_STOP && token != '.')
224         lex_get ();
225     }
226   else 
227     {
228       msg (SW, _("The rest of this command has been discarded."));
229       lex_discard_line (); 
230     }
231 }
232 \f
233 static void
234 i18n_init (void) 
235 {
236 #if ENABLE_NLS
237 #if HAVE_LC_MESSAGES
238   setlocale (LC_MESSAGES, "");
239 #endif
240   setlocale (LC_MONETARY, "");
241   bindtextdomain (PACKAGE, locale_dir);
242   textdomain (PACKAGE);
243 #endif /* ENABLE_NLS */
244 }
245
246 static void
247 fpu_init (void) 
248 {
249 #if HAVE_FEHOLDEXCEPT
250   fenv_t foo;
251   feholdexcept (&foo);
252 #elif HAVE___SETFPUCW && defined(_FPU_IEEE)
253   __setfpucw (_FPU_IEEE);
254 #endif
255 }
256
257 /* If a segfault happens, issue a message to that effect and halt */
258 void 
259 bug_handler(int sig)
260 {
261   switch (sig) 
262     {
263     case SIGFPE:
264       request_bug_report_and_abort("Floating Point Exception");
265       break;
266     case SIGSEGV:
267       request_bug_report_and_abort("Segmentation Violation");
268       break;
269     default:
270       request_bug_report_and_abort("");
271       break;
272     }
273 }
274
275
276 void 
277 interrupt_handler(int sig UNUSED)
278 {
279   terminate (false);
280 }