Added a trap on signal 11 requesting a bug report.
[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., 59 Temple Place - Suite 330, Boston, MA
18    02111-1307, USA. */
19
20 #include <config.h>
21 #include "main.h"
22 #include <assert.h>
23 #include <stdio.h>
24 #include "cmdline.h"
25 #include "command.h"
26 #include "error.h"
27 #include "getline.h"
28 #include "glob.h"
29 #include "lexer.h"
30 #include "output.h"
31 #include "version.h"
32 #include <signal.h>
33
34 #include <stdlib.h>
35
36 #include "debug-print.h"
37
38 static void parse_script (void) NO_RETURN;
39 static void handle_error (int code);
40 static int execute_command (void);
41
42 /* argv[0] with stripped leading directories. */
43 char *pgmname;
44
45 /* Whether FINISH. has been executed. */
46 int finished;
47
48 /* The current date in the form DD MMM YYYY. */
49 char curdate[12];
50
51
52 /* If a segfault happens, issue a message to that effect and halt */
53 void bug_handler(int sig);
54
55 /* Whether we're dropping down to interactive mode immediately because
56    we hit end-of-file unexpectedly (or whatever). */
57 int start_interactive;
58
59 /* Program entry point. */
60 int
61 main (int argc, char **argv)
62 {
63   struct sigaction bug ;
64   bug.sa_handler = bug_handler;
65
66   sigaction(SIGSEGV, &bug,0);
67
68   /* Initialization. */
69   if (!outp_init ())
70     err_hcf (0);
71   init_glob (argc, argv);
72   parse_command_line (argc, argv);
73   if (!outp_read_devices ())
74     msg (FE, _("Error initializing output drivers."));
75
76   lex_init ();
77   cmd_init ();
78
79   /* Execution. */
80   parse_script ();
81
82   /* Should never be reached */
83   return (-1);
84 }
85
86 /* Parses the entire script. */
87 static void
88 parse_script (void)
89 {
90   while (!finished)
91     {
92       err_check_count ();
93       handle_error (execute_command ());
94     }
95
96   err_hcf (err_error_count==0);
97 }
98
99 /* Parse and execute a command, returning its return code. */
100 static int
101 execute_command (void)
102 {
103   /* Read the command's first token.
104      We may hit end of file.
105      If so, give the line reader a chance to proceed to the next file.
106      End of file is not handled transparently since the user may want
107      the dictionary cleared between files. */
108   getl_prompt = GETL_PRPT_STANDARD;
109   for (;;)
110     {
111       lex_get ();
112       if (token != T_STOP)
113         break;
114
115       if (!getl_perform_delayed_reset ())
116         err_hcf (err_error_count==0);
117     }
118
119   /* Parse the command. */
120   getl_prompt = GETL_PRPT_CONTINUATION;
121   return cmd_parse ();
122 }
123
124 /* Print an error message corresponding to the command return code
125    CODE. */
126 static void
127 handle_error (int code)
128 {
129   switch (code)
130     {
131     case CMD_SUCCESS:
132       return;
133           
134     case CMD_FAILURE:
135       msg (SW,  _("This command not executed."));
136       break;
137
138     case CMD_PART_SUCCESS_MAYBE:
139       msg (SW, _("Skipping the rest of this command.  Part of "
140                  "this command may have been executed."));
141       break;
142                   
143     case CMD_PART_SUCCESS:
144       msg (SW, _("Skipping the rest of this command.  This "
145                  "command was fully executed up to this point."));
146       break;
147
148     case CMD_TRAILING_GARBAGE:
149       msg (SW, _("Trailing garbage was encountered following "
150                  "this command.  The command was fully executed "
151                  "to this point."));
152       break;
153
154     default:
155       assert (0);
156     }
157
158   if (getl_reading_script)
159     {
160       err_break ();
161       while (token != T_STOP && token != '.')
162         lex_get ();
163     }
164   else
165     lex_discard_line ();
166 }
167
168
169
170 /* If a segfault happens, issue a message to that effect and halt */
171 void 
172 bug_handler(int sig UNUSED)
173 {
174   fprintf(stderr,
175           "******************************************************************\n"
176           "You have discovered a bug in PSPP.\n\n"
177           "  Please report this, by sending "
178           "an email to " PACKAGE_BUGREPORT ",\n"
179           "explaining what you were doing when this happened, and including\n"
180           "a sample of your input file which caused it.\n");
181
182   fprintf(stderr,
183           "Also, please copy the following lines into your bug report:\n\n"
184           "bare_version:        %s\n" 
185           "version:             %s\n"
186           "stat_version:        %s\n"
187           "host_system:         %s\n"
188           "build_system:        %s\n"
189           "default_config_path: %s\n"
190           "include_path:        %s\n"
191           "groff_font_path:     %s\n"
192           "locale_dir:          %s\n"
193           "******************************************************************\n",
194           bare_version,         
195           version,
196           stat_version,
197           host_system,        
198           build_system,
199           default_config_path,
200           include_path, 
201           groff_font_path,
202           locale_dir);     
203
204   exit(-1);
205 }
206