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