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