Rewrote the command line parser using argp.
[pspp] / src / ui / terminal / main.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2007 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <locale.h>
20 #include <signal.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #if HAVE_FPU_CONTROL_H
24 #include <fpu_control.h>
25 #endif
26 #if HAVE_FENV_H
27 #include <fenv.h>
28 #endif
29 #if HAVE_IEEEFP_H
30 #include <ieeefp.h>
31 #endif
32
33 #include <data/dictionary.h>
34 #include <data/file-handle-def.h>
35 #include <libpspp/getl.h>
36 #include <data/file-name.h>
37 #include <data/procedure.h>
38 #include <data/settings.h>
39 #include <data/variable.h>
40 #include <gsl/gsl_errno.h>
41 #include <language/command.h>
42 #include <language/lexer/lexer.h>
43 #include <language/prompt.h>
44 #include <libpspp/compiler.h>
45 #include <libpspp/message.h>
46 #include <libpspp/version.h>
47 #include <math/random.h>
48 #include <output/output.h>
49 #include <ui/debugger.h>
50 #include <ui/terminal/msg-ui.h>
51 #include <ui/terminal/read-line.h>
52 #include <ui/terminal/terminal.h>
53 #include <ui/terminal/terminal-opts.h>
54 #include <ui/command-line.h>
55 #include <ui/source-init-opts.h>
56
57 #include "fatal-signal.h"
58 #include "progname.h"
59
60 #include "gettext.h"
61 #define _(msgid) gettext (msgid)
62
63
64 static void i18n_init (void);
65 static void fpu_init (void);
66 static void clean_up (void);
67
68 /* If a segfault happens, issue a message to that effect and halt */
69 void bug_handler(int sig);
70
71 /* Handle quit/term/int signals */
72 void interrupt_handler(int sig);
73 static struct dataset * the_dataset = NULL;
74
75 static struct lexer *the_lexer;
76 static struct source_stream *the_source_stream ;
77
78 const char *argp_program_version = version;
79 const char *argp_program_bug_address = PACKAGE_BUGREPORT;
80
81 /* Program entry point. */
82 int
83 main (int argc, char **argv)
84 {
85   int *view_width_p, *view_length_p;
86   struct command_line_processor *clp;
87   set_program_name (argv[0]);
88
89   signal (SIGABRT, bug_handler);
90   signal (SIGSEGV, bug_handler);
91   signal (SIGFPE, bug_handler);
92   signal (SIGALRM, SIG_IGN);
93   at_fatal_signal (clean_up);
94
95   i18n_init ();
96   fpu_init ();
97   gsl_set_error_handler_off ();
98
99   outp_init ();
100   fn_init ();
101   fh_init ();
102   the_source_stream =
103     create_source_stream (
104                           fn_getenv_default ("STAT_INCLUDE_PATH", include_path)
105                           );
106   prompt_init ();
107   readln_initialize ();
108   terminal_init (&view_width_p, &view_length_p);
109   settings_init (view_width_p, view_length_p);
110   random_init ();
111
112   the_dataset = create_dataset ();
113
114
115
116   clp = command_line_processor_create (_("PSPP --- A program for statistical analysis"),
117                                        _("FILE1, FILE2 ... FILEn"), NULL);
118
119   command_line_processor_add_options (clp, &io_argp,
120                                       _("Options affecting input and output locations:"), the_source_stream);
121
122   command_line_processor_add_options (clp, &test_argp,
123                                       _("Diagnositic options:"), the_source_stream);
124
125   command_line_processor_add_options (clp, &post_init_argp,
126                                       _("Options affecting syntax and behavior:"), the_source_stream);
127
128   command_line_processor_parse (clp, argc, argv);
129
130   msg_ui_init (the_source_stream);
131
132   if (!settings_get_testing_mode ())
133     {
134       outp_read_devices ();
135     }
136   else
137     {
138       outp_configure_driver_line
139         (
140          ss_cstr ("raw-ascii:ascii:listing:width=9999 length=9999 "
141                   "output-file=\"pspp.list\" emphasis=none "
142                   "headers=off paginate=off squeeze=on "
143                   "top-margin=0 bottom-margin=0"));
144     }
145
146   the_lexer = lex_create (the_source_stream);
147
148   for (;;)
149     {
150       int result = cmd_parse (the_lexer, the_dataset);
151
152       if (result == CMD_EOF || result == CMD_FINISH)
153         break;
154       if (result == CMD_CASCADING_FAILURE &&
155           !getl_is_interactive (the_source_stream))
156         {
157           msg (SE, _("Stopping syntax file processing here to avoid "
158                      "a cascade of dependent command failures."));
159           getl_abort_noninteractive (the_source_stream);
160         }
161       else
162         check_msg_count (the_source_stream);
163     }
164
165
166   clean_up ();
167   return any_errors ();
168 }
169 \f
170 static void
171 i18n_init (void)
172 {
173 #if ENABLE_NLS
174 #if HAVE_LC_MESSAGES
175   setlocale (LC_MESSAGES, "");
176 #endif
177 #if HAVE_LC_PAPER
178   setlocale (LC_PAPER, "");
179 #endif
180   bindtextdomain (PACKAGE, locale_dir);
181   textdomain (PACKAGE);
182 #endif /* ENABLE_NLS */
183 }
184
185 static void
186 fpu_init (void)
187 {
188 #if HAVE_FEHOLDEXCEPT
189   fenv_t foo;
190   feholdexcept (&foo);
191 #elif HAVE___SETFPUCW && defined(_FPU_IEEE)
192   __setfpucw (_FPU_IEEE);
193 #elif HAVE_FPSETMASK
194   fpsetmask (0);
195 #endif
196 }
197
198 /* If a segfault happens, issue a message to that effect and halt */
199 void
200 bug_handler(int sig)
201 {
202 #if DEBUGGING
203   connect_debugger ();
204 #endif
205   switch (sig)
206     {
207     case SIGABRT:
208       request_bug_report_and_abort("Assertion Failure/Abort");
209     case SIGFPE:
210       request_bug_report_and_abort("Floating Point Exception");
211     case SIGSEGV:
212       request_bug_report_and_abort("Segmentation Violation");
213     default:
214       request_bug_report_and_abort("Unknown");
215     }
216 }
217
218 /* Clean up PSPP in preparation for termination.  */
219 static void
220 clean_up (void)
221 {
222   static bool terminating = false;
223   if (!terminating)
224     {
225       terminating = true;
226
227       destroy_dataset (the_dataset);
228
229       random_done ();
230       settings_done ();
231       fh_done ();
232       lex_destroy (the_lexer);
233       destroy_source_stream (the_source_stream);
234       prompt_done ();
235       readln_uninitialize ();
236       outp_done ();
237       msg_ui_done ();
238     }
239 }