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