Adopt use of gnulib for portability.
[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 <stdio.h>
22 #include <gsl/gsl_errno.h>
23 #include "main.h"
24 #include "cmdline.h"
25 #include "command.h"
26 #include "dictionary.h"
27 #include "error.h"
28 #include "getl.h"
29 #include "glob.h"
30 #include "lexer.h"
31 #include "output.h"
32 #include "settings.h"
33 #include "var.h"
34 #include <signal.h>
35
36 #include "gettext.h"
37 #define _(msgid) gettext (msgid)
38
39 #include <stdlib.h>
40
41 #include "debug-print.h"
42
43 static void parse_script (void) NO_RETURN;
44 static void handle_error (int code);
45 static int execute_command (void);
46
47 /* argv[0] with stripped leading directories. */
48 char *pgmname;
49
50 /* Whether FINISH. has been executed. */
51 int finished;
52
53 /* The current date in the form DD MMM YYYY. */
54 char curdate[12];
55
56
57 /* If a segfault happens, issue a message to that effect and halt */
58 void bug_handler(int sig);
59
60 /* Handle quit/term/int signals */
61 void interrupt_handler(int sig);
62
63 /* Whether we're dropping down to interactive mode immediately because
64    we hit end-of-file unexpectedly (or whatever). */
65 int start_interactive;
66
67
68
69
70
71
72 /* Program entry point. */
73 int
74 main (int argc, char **argv)
75 {
76
77   signal (SIGSEGV, bug_handler);
78   signal (SIGFPE,  bug_handler);
79   signal (SIGINT,  interrupt_handler);
80
81   gsl_set_error_handler_off();
82
83   /* Initialization. */
84   if (!outp_init ())
85     err_hcf (0);
86   init_glob (argc, argv);
87   parse_command_line (argc, argv);
88   if (!outp_read_devices ())
89     msg (FE, _("Error initializing output drivers."));
90
91   lex_init ();
92
93   /* Execution. */
94   parse_script ();
95
96   /* Should never be reached */
97   return (-1);
98 }
99
100 /* Parses the entire script. */
101 static void
102 parse_script (void)
103 {
104   while (!finished)
105     {
106       err_check_count ();
107       handle_error (execute_command ());
108     }
109
110   err_hcf (err_error_count==0);
111 }
112
113 /* Parse and execute a command, returning its return code. */
114 static int
115 execute_command (void)
116 {
117   int result;
118   
119   /* Read the command's first token.
120      We may hit end of file.
121      If so, give the line reader a chance to proceed to the next file.
122      End of file is not handled transparently since the user may want
123      the dictionary cleared between files. */
124   getl_prompt = GETL_PRPT_STANDARD;
125   for (;;)
126     {
127       lex_get ();
128       if (token != T_STOP)
129         break;
130
131       if (!getl_perform_delayed_reset ())
132         err_hcf (err_error_count==0);
133     }
134
135   /* Parse the command. */
136   getl_prompt = GETL_PRPT_CONTINUATION;
137   result =  cmd_parse ();
138  
139   /* Unset the /ALGORITHM subcommand if it was used */
140   unset_cmd_algorithm ();
141
142   /* Clear any auxiliary data from the dictionary. */
143   dict_clear_aux (default_dict);
144
145   return result;
146 }
147
148 /* Print an error message corresponding to the command return code
149    CODE. */
150 static void
151 handle_error (int code)
152 {
153   switch (code)
154     {
155     case CMD_SUCCESS:
156       return;
157           
158     case CMD_FAILURE:
159       msg (SW,  _("This command not executed."));
160       break;
161
162     case CMD_PART_SUCCESS_MAYBE:
163       msg (SW, _("Skipping the rest of this command.  Part of "
164                  "this command may have been executed."));
165       break;
166                   
167     case CMD_PART_SUCCESS:
168       msg (SW, _("Skipping the rest of this command.  This "
169                  "command was fully executed up to this point."));
170       break;
171
172     case CMD_TRAILING_GARBAGE:
173       msg (SW, _("Trailing garbage was encountered following "
174                  "this command.  The command was fully executed "
175                  "to this point."));
176       break;
177
178     default:
179       assert (0);
180     }
181
182   if (getl_reading_script)
183     {
184       err_break ();
185       while (token != T_STOP && token != '.')
186         lex_get ();
187     }
188   else 
189     {
190       msg (SW, _("The rest of this command has been discarded."));
191       lex_discard_line (); 
192     }
193 }
194
195
196
197 /* If a segfault happens, issue a message to that effect and halt */
198 void 
199 bug_handler(int sig UNUSED)
200 {
201   switch (sig) 
202     {
203     case SIGFPE:
204       request_bug_report_and_abort("Floating Point Exception");
205       break;
206     case SIGSEGV:
207       request_bug_report_and_abort("Segmentation Violation");
208       break;
209     default:
210       request_bug_report_and_abort("");
211       break;
212     }
213 }
214
215
216 void 
217 interrupt_handler(int sig UNUSED)
218 {
219   err_hcf(0);
220 }