Allow output files to overwrite input files (bug #21280). Thanks to
[pspp-builds.git] / 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/format.h>
38 #include <data/procedure.h>
39 #include <data/settings.h>
40 #include <data/variable.h>
41 #include <gsl/gsl_errno.h>
42 #include <language/command.h>
43 #include <language/lexer/lexer.h>
44 #include <language/prompt.h>
45 #include <libpspp/compiler.h>
46 #include <libpspp/message.h>
47 #include <libpspp/version.h>
48 #include <math/random.h>
49 #include <output/output.h>
50 #include <ui/debugger.h>
51 #include <ui/terminal/command-line.h>
52 #include <ui/terminal/msg-ui.h>
53 #include <ui/terminal/read-line.h>
54 #include <ui/terminal/terminal.h>
55
56 #include "fatal-signal.h"
57 #include "progname.h"
58
59 #include "gettext.h"
60 #define _(msgid) gettext (msgid)
61
62
63 static void i18n_init (void);
64 static void fpu_init (void);
65 static void clean_up (void);
66
67 /* If a segfault happens, issue a message to that effect and halt */
68 void bug_handler(int sig);
69
70 /* Handle quit/term/int signals */
71 void interrupt_handler(int sig);
72 static struct dataset * the_dataset = NULL;
73
74 static struct lexer *the_lexer;
75 static struct source_stream *the_source_stream ;
76
77 /* Program entry point. */
78 int
79 main (int argc, char **argv)
80 {
81   int *view_width_p, *view_length_p;
82
83   set_program_name (argv[0]);
84
85   signal (SIGABRT, bug_handler);
86   signal (SIGSEGV, bug_handler);
87   signal (SIGFPE, bug_handler);
88   at_fatal_signal (clean_up);
89
90   i18n_init ();
91   fpu_init ();
92   gsl_set_error_handler_off ();
93
94   fmt_init ();
95   outp_init ();
96   fn_init ();
97   fh_init ();
98   the_source_stream =
99     create_source_stream (
100                           fn_getenv_default ("STAT_INCLUDE_PATH", include_path)
101                           );
102   prompt_init ();
103   readln_initialize ();
104   terminal_init (&view_width_p, &view_length_p);
105   settings_init (view_width_p, view_length_p);
106   random_init ();
107
108   the_dataset = create_dataset ();
109
110   if (parse_command_line (argc, argv, the_source_stream))
111     {
112       msg_ui_init (the_source_stream);
113       if (!get_testing_mode ())
114         outp_read_devices ();
115       else
116         outp_configure_driver_line (
117           ss_cstr ("raw-ascii:ascii:listing:width=9999 length=9999 "
118                    "output-file=\"pspp.list\" emphasis=none "
119                    "headers=off paginate=off squeeze=on "
120                    "top-margin=0 bottom-margin=0"));
121       the_lexer = lex_create (the_source_stream);
122
123       for (;;)
124         {
125           int result = cmd_parse (the_lexer, the_dataset);
126
127           if (result == CMD_EOF || result == CMD_FINISH)
128             break;
129           if (result == CMD_CASCADING_FAILURE &&
130               !getl_is_interactive (the_source_stream))
131             {
132               msg (SE, _("Stopping syntax file processing here to avoid "
133                          "a cascade of dependent command failures."));
134               getl_abort_noninteractive (the_source_stream);
135             }
136           else
137             check_msg_count (the_source_stream);
138         }
139     }
140
141   clean_up ();
142   return any_errors ();
143 }
144 \f
145 static void
146 i18n_init (void)
147 {
148 #if ENABLE_NLS
149 #if HAVE_LC_MESSAGES
150   setlocale (LC_MESSAGES, "");
151 #endif
152 #if HAVE_LC_PAPER
153   setlocale (LC_PAPER, "");
154 #endif
155   bindtextdomain (PACKAGE, locale_dir);
156   textdomain (PACKAGE);
157 #endif /* ENABLE_NLS */
158 }
159
160 static void
161 fpu_init (void)
162 {
163 #if HAVE_FEHOLDEXCEPT
164   fenv_t foo;
165   feholdexcept (&foo);
166 #elif HAVE___SETFPUCW && defined(_FPU_IEEE)
167   __setfpucw (_FPU_IEEE);
168 #elif HAVE_FPSETMASK
169   fpsetmask (0);
170 #endif
171 }
172
173 /* If a segfault happens, issue a message to that effect and halt */
174 void
175 bug_handler(int sig)
176 {
177 #if DEBUGGING
178   connect_debugger ();
179 #endif
180   switch (sig)
181     {
182     case SIGABRT:
183       request_bug_report_and_abort("Assertion Failure/Abort");
184     case SIGFPE:
185       request_bug_report_and_abort("Floating Point Exception");
186     case SIGSEGV:
187       request_bug_report_and_abort("Segmentation Violation");
188     default:
189       request_bug_report_and_abort("Unknown");
190     }
191 }
192
193 /* Clean up PSPP in preparation for termination.  */
194 static void
195 clean_up (void)
196 {
197   static bool terminating = false;
198   if (!terminating)
199     {
200       terminating = true;
201
202       destroy_dataset (the_dataset);
203
204       random_done ();
205       settings_done ();
206       fh_done ();
207       lex_destroy (the_lexer);
208       destroy_source_stream (the_source_stream);
209       prompt_done ();
210       readln_uninitialize ();
211       outp_done ();
212       msg_ui_done ();
213       fmt_done ();
214     }
215 }