3b7687b2cede0bb7367d99dc3cc2d1153ac0d7bf
[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 <stdio.h>
22 #include <gsl/gsl_errno.h>
23 #include "main.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 <signal.h>
32
33 #include <stdlib.h>
34
35 #include "debug-print.h"
36
37 static void parse_script (void) NO_RETURN;
38 static void handle_error (int code);
39 static int execute_command (void);
40
41 /* argv[0] with stripped leading directories. */
42 char *pgmname;
43
44 /* Whether FINISH. has been executed. */
45 int finished;
46
47 /* The current date in the form DD MMM YYYY. */
48 char curdate[12];
49
50
51 /* If a segfault happens, issue a message to that effect and halt */
52 void bug_handler(int sig);
53
54 /* Whether we're dropping down to interactive mode immediately because
55    we hit end-of-file unexpectedly (or whatever). */
56 int start_interactive;
57
58 /* Initialise error handling on the gsl library */
59 static void 
60 err_handler_gsl (const char *reason, const char *file,
61         int line, int gsl_errno UNUSED)
62 {
63   msg(FE, _("gsl error at %s:%d; reason: \"%s\""), file,line,reason);
64 }
65
66 /* Program entry point. */
67 int
68 main (int argc, char **argv)
69 {
70   signal (SIGSEGV, bug_handler);
71
72   gsl_set_error_handler(err_handler_gsl);
73
74   /* Initialization. */
75   if (!outp_init ())
76     err_hcf (0);
77   init_glob (argc, argv);
78   parse_command_line (argc, argv);
79   if (!outp_read_devices ())
80     msg (FE, _("Error initializing output drivers."));
81
82   lex_init ();
83
84   /* Execution. */
85   parse_script ();
86
87   /* Should never be reached */
88   return (-1);
89 }
90
91 /* Parses the entire script. */
92 static void
93 parse_script (void)
94 {
95   while (!finished)
96     {
97       err_check_count ();
98       handle_error (execute_command ());
99     }
100
101   err_hcf (err_error_count==0);
102 }
103
104 /* Parse and execute a command, returning its return code. */
105 static int
106 execute_command (void)
107 {
108   /* Read the command's first token.
109      We may hit end of file.
110      If so, give the line reader a chance to proceed to the next file.
111      End of file is not handled transparently since the user may want
112      the dictionary cleared between files. */
113   getl_prompt = GETL_PRPT_STANDARD;
114   for (;;)
115     {
116       lex_get ();
117       if (token != T_STOP)
118         break;
119
120       if (!getl_perform_delayed_reset ())
121         err_hcf (err_error_count==0);
122     }
123
124   /* Parse the command. */
125   getl_prompt = GETL_PRPT_CONTINUATION;
126   return cmd_parse ();
127 }
128
129 /* Print an error message corresponding to the command return code
130    CODE. */
131 static void
132 handle_error (int code)
133 {
134   switch (code)
135     {
136     case CMD_SUCCESS:
137       return;
138           
139     case CMD_FAILURE:
140       msg (SW,  _("This command not executed."));
141       break;
142
143     case CMD_PART_SUCCESS_MAYBE:
144       msg (SW, _("Skipping the rest of this command.  Part of "
145                  "this command may have been executed."));
146       break;
147                   
148     case CMD_PART_SUCCESS:
149       msg (SW, _("Skipping the rest of this command.  This "
150                  "command was fully executed up to this point."));
151       break;
152
153     case CMD_TRAILING_GARBAGE:
154       msg (SW, _("Trailing garbage was encountered following "
155                  "this command.  The command was fully executed "
156                  "to this point."));
157       break;
158
159     default:
160       assert (0);
161     }
162
163   if (getl_reading_script)
164     {
165       err_break ();
166       while (token != T_STOP && token != '.')
167         lex_get ();
168     }
169   else
170     lex_discard_line ();
171 }
172
173
174
175 /* If a segfault happens, issue a message to that effect and halt */
176 void 
177 bug_handler(int sig UNUSED)
178 {
179   request_bug_report_and_abort("Segmentation Violation");
180 }