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