cc76e340ab21d8fedc9f85a6ca251a4eef7e4fc5
[pspp-builds.git] / src / glob.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 "glob.h"
22 #include <assert.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25
26 #if TIME_WITH_SYS_TIME
27 #include <sys/time.h>
28 #include <time.h>
29 #else
30 #if HAVE_SYS_TIME_H
31 #include <sys/time.h>
32 #else
33 #include <time.h>
34 #endif
35 #endif
36
37 #if HAVE_LIBTERMCAP
38 #if HAVE_TERMCAP_H
39 #include <termcap.h>
40 #else /* !HAVE_TERMCAP_H */
41 int tgetent (char *, char *);
42 int tgetnum (char *);
43 #endif /* !HAVE_TERMCAP_H */
44 #endif /* !HAVE_LIBTERMCAP */
45
46 #if HAVE_LIBHISTORY
47 #if HAVE_READLINE_HISTORY_H
48 #include <readline/history.h>
49 #else /* no readline/history.h */
50 extern void using_history ();
51 extern int read_history ();
52 extern void stifle_history ();
53 #endif /* no readline/history.h */
54 #endif /* -lhistory */
55
56 #if HAVE_FPU_CONTROL_H
57 #include <fpu_control.h>
58 #elif __BORLANDC__
59 #include <float.h>
60 #include <math.h>
61 #endif
62
63 #if __DJGPP__
64 #include <conio.h>
65 #elif defined (__WIN32__) && defined (__BORLANDC__)
66 #undef gettext
67 #include <conio.h>
68 #define gettext(STRING)                         \
69         STRING
70 #endif
71
72 #if HAVE_LOCALE_H
73 #include <locale.h>
74 #endif
75
76 #if HAVE_FENV_H
77 #include <fenv.h>
78 #endif
79
80 #include "alloc.h"
81 #include "command.h"
82 #include "do-ifP.h"
83 #include "error.h"
84 #include "expr.h"
85 #include "file-handle.h"
86 #include "filename.h"
87 #include "getline.h"
88 #include "hash.h"
89 #include "julcal/julcal.h"
90 #include "lexer.h"
91 #include "magic.h"
92 #include "main.h"
93 #include "settings.h"
94 #include "str.h"
95 #include "var.h"
96 #include "version.h"
97 #include "vfm.h"
98
99 /* var.h */
100 struct dictionary *default_dict;
101 struct expression *process_if_expr;
102
103 struct ccase *temp_case;
104
105 struct trns_header **t_trns;
106 int n_trns;
107 int m_trns;
108 int f_trns;
109
110 int FILTER_before_TEMPORARY;
111
112 struct file_handle *default_handle;
113
114 void (*read_active_file) (void);
115 void (*cancel_input_pgm) (void);
116
117 struct ctl_stmt *ctl_stack;
118
119 /* log.h */
120 char *logfn;
121 FILE *logfile;
122 int logging;
123 \f
124 /* Functions. */
125
126 static void get_date (void);
127
128 #if HAVE_LIBTERMCAP
129 static char term_buffer[16384];
130 #endif
131
132 void
133 init_glob (int argc UNUSED, char **argv)
134 {
135   /* FIXME: Allow i18n of other locale items (besides LC_MESSAGES). */
136 #if ENABLE_NLS
137 #if HAVE_LC_MESSAGES
138   setlocale (LC_MESSAGES, "");
139 #endif
140   setlocale (LC_MONETARY, "");
141   bindtextdomain (PACKAGE, locale_dir);
142   textdomain (PACKAGE);
143 #endif /* ENABLE_NLS */
144
145   /* Workable defaults before we determine the real terminal size. */
146   set_viewwidth = 79;
147   set_viewlength = 24;
148
149   fn_init ();
150   getl_initialize ();
151
152   /* PORTME: If your system/OS has the nasty tendency to halt with a
153      SIGFPE whenever there's a floating-point overflow (or other
154      exception), be sure to mask off those bits in the FPU here.
155      PSPP wants a guarantee that, no matter what boneheaded
156      floating-point operation it performs, the process will not halt.  */
157 #if HAVE_FEHOLDEXCEPT
158   {
159     fenv_t foo;
160
161     feholdexcept (&foo);
162   }
163 #elif HAVE___SETFPUCW && defined(_FPU_IEEE)
164   __setfpucw (_FPU_IEEE);
165 #elif __BORLANDC__
166   _control87 (0xffff, 0x137f);
167 #endif
168
169   /* var.h */
170   default_dict = dict_create ();
171
172   last_vfm_invocation = time (NULL);
173
174   /* lexer.h */
175   ds_init (NULL, &tokstr, 64);
176
177   /* common.h */
178   {
179     char *cp;
180     
181     pgmname = argv[0];
182     for (;;)
183       {
184         cp = strchr (pgmname, DIR_SEPARATOR);
185         if (!cp)
186           break;
187         pgmname = &cp[1];
188       }
189     cur_proc = NULL;
190   }
191
192   /* settings.h */
193 #if !USE_INTERNAL_PAGER
194   {
195     char *pager;
196
197     pager = getenv ("STAT_PAGER");
198     if (!pager)
199       pager = getenv ("PAGER");
200     if (pager)
201       set_pager = xstrdup (pager);
202 #if DEFAULT_PAGER
203     else
204       set_pager = xstrdup (DEFAULT_PAGER);
205 #endif /* DEFAULT_PAGER */
206   }
207 #endif /* !USE_INTERNAL_PAGER */
208
209   set_blanks = SYSMIS;
210   set_scompression = 1;
211   set_format.type = FMT_F;
212   set_format.w = 8;
213   set_format.d = 2;
214   set_cpi = 6;
215   set_lpi = 10;
216   set_results_file = xstrdup ("pspp.prc");
217   set_dprompt = xstrdup (_("data> "));
218   
219   {
220     int i;
221     
222     for (i = 0; i < 5; i++)
223       {
224         struct set_cust_currency *cc = &set_cc[i];
225         strcpy (cc->buf, "-");
226         cc->neg_prefix = cc->buf;
227         cc->prefix = &cc->buf[1];
228         cc->suffix = &cc->buf[1];
229         cc->neg_suffix = &cc->buf[1];
230         cc->decimal = '.';
231         cc->grouping = ',';
232       }
233   }
234   
235   set_decimal = '.';
236   set_grouping = ',';
237   set_headers = 1;
238   set_journaling = 1;
239   set_journal = xstrdup ("pspp.jnl");
240   set_messages = 1;
241   set_mexpand = 1;
242   set_mprint = 1;
243   set_mxerrs = 50;
244   set_mxwarns = 100;
245   set_printback = 1;
246   set_undefined = 1;
247
248   set_cprompt = xstrdup ("    > ");
249   set_echo = 0;
250   set_endcmd = '.';
251   set_errorbreak = 0;
252   set_include = 1;
253   set_nullline = 1;
254   set_more = 1;
255   set_prompt = xstrdup ("PSPP> ");
256   set_seed = NOT_LONG;
257
258 #if __DJGPP__ || __BORLANDC__
259   {
260     struct text_info ti;
261
262     gettextinfo (&ti);
263     set_viewlength = max (ti.screenheight, 25);
264     set_viewwidth = max (ti.screenwidth, 79);
265   }
266 #elif HAVE_LIBTERMCAP
267   {
268     char *termtype;
269     int success;
270
271     /* This code stolen from termcap.info, though modified. */
272     termtype = getenv ("TERM");
273     if (!termtype)
274       msg (FE, _("Specify a terminal type with `setenv TERM <yourtype>'."));
275
276     success = tgetent (term_buffer, termtype);
277     if (success <= 0)
278       {
279         if (success < 0)
280           msg (IE, _("Could not access the termcap data base."));
281         else
282           msg (IE, _("Terminal type `%s' is not defined."), termtype);
283         msg (MM, _("Assuming screen of size 79x25."));
284         set_viewlength = 25;
285         set_viewwidth = 79;
286       }
287     else
288       {
289         set_viewlength = tgetnum ("li");
290         set_viewwidth = tgetnum ("co") - 1;
291       }
292   }
293 #else /* !HAVE_LIBTERMCAP */
294   set_viewlength = 25;
295   set_viewwidth = 79;
296 #endif /* !HAVE_LIBTERMCAP */
297
298   /* log.h */
299   logging = 1;
300   logfn = xstrdup ("pspp.log");
301   logfile = NULL;
302
303   /* file-handle.h */
304   fh_init_files ();
305   
306   get_date ();
307 }
308
309 static void
310 get_date (void)
311 {
312
313   time_t t;
314   struct tm *tmp;
315
316   if ((time_t) -1 == time (&t))
317     {
318       strcpy (curdate, "?? ??? 2???");
319       return;
320     }
321   tmp = localtime (&t);
322
323   strftime (curdate, 12, "%d %b %Y",tmp);
324 }
325
326 #if __BORLANDC__
327 int
328 _RTLENTRY _EXPFUNC _matherr (struct exception _FAR *__e)
329 {
330   return 1;
331 }
332
333 int
334 _RTLENTRY _EXPFUNC _matherrl (struct _exceptionl _FAR *__e)
335 {
336   return 1;
337 }
338 #endif