checkin of 0.3.0
[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
22 #include <assert.h>
23 #include <stdlib.h>
24
25 #if TIME_WITH_SYS_TIME
26 #include <sys/time.h>
27 #include <time.h>
28 #else
29 #if HAVE_SYS_TIME_H
30 #include <sys/time.h>
31 #else
32 #include <time.h>
33 #endif
34 #endif
35
36 #if HAVE_LIBTERMCAP
37 #if HAVE_TERMCAP_H
38 #include <termcap.h>
39 #else /* !HAVE_TERMCAP_H */
40 int tgetent (char *, char *);
41 int tgetnum (char *);
42 #endif /* !HAVE_TERMCAP_H */
43 #endif /* !HAVE_LIBTERMCAP */
44
45 #if HAVE_LIBHISTORY
46 #if HAVE_READLINE_HISTORY_H
47 #include <readline/history.h>
48 #else /* no readline/history.h */
49 extern void using_history ();
50 extern int read_history ();
51 extern void stifle_history ();
52 #endif /* no readline/history.h */
53 #endif /* -lhistory */
54
55 #if HAVE_FPU_CONTROL_H
56 #include <fpu_control.h>
57 #elif __BORLANDC__
58 #include <float.h>
59 #include <math.h>
60 #endif
61
62 #if __DJGPP__
63 #include <conio.h>
64 #elif __WIN32__ && __BORLANDC__
65 #undef gettext
66 #include <conio.h>
67 #define gettext(STRING)                         \
68         STRING
69 #endif
70
71 #if HAVE_LOCALE_H
72 #include <locale.h>
73 #endif
74
75 #if HAVE_FENV_H
76 #include <fenv.h>
77 #endif
78
79 #include "alloc.h"
80 #include "avl.h"
81 #include "command.h"
82 #include "do-ifP.h"
83 #include "error.h"
84 #include "expr.h"
85 #include "filename.h"
86 #include "getline.h"
87 #include "julcal/julcal.h"
88 #include "lexer.h"
89 #include "main.h"
90 #include "settings.h"
91 #include "str.h"
92 #include "var.h"
93 #include "version.h"
94 #include "vfm.h"
95
96 /* var.h */
97 struct dictionary default_dict;
98 struct expression *process_if_expr;
99
100 struct ccase *temp_case;
101
102 struct trns_header **t_trns;
103 int n_trns;
104 int m_trns;
105 int f_trns;
106
107 int FILTER_before_TEMPORARY;
108
109 struct file_handle *default_handle;
110
111 void (*read_active_file) (void);
112 void (*cancel_input_pgm) (void);
113
114 struct ctl_stmt *ctl_stack;
115
116 /* log.h */
117 char *logfn;
118 FILE *logfile;
119 int logging;
120 \f
121 /* Functions. */
122
123 static void get_date (void);
124
125 #if HAVE_LIBTERMCAP && !__CHECKER__
126 static char *term_buffer;
127 #endif
128
129 void
130 init_glob (int argc unused, char **argv)
131 {
132   /* FIXME: Allow i18n of other locale items (besides LC_MESSAGES). */
133 #if ENABLE_NLS
134 #if LC_MESSAGE
135   setlocale (LC_MESSAGES, "");
136 #endif
137   setlocale (LC_MONETARY, "");
138   bindtextdomain (PACKAGE, locale_dir);
139   textdomain (PACKAGE);
140 #endif /* ENABLE_NLS */
141
142   /* Workable defaults before we determine the real terminal size. */
143   set_viewwidth = 79;
144   set_viewlength = 24;
145
146   fn_init ();
147   getl_initialize ();
148
149   /* PORTME: If your system/OS has the nasty tendency to halt with a
150      SIGFPE whenever there's a floating-point overflow (or other
151      exception), be sure to mask off those bits in the FPU here.
152      PSPP wants a guarantee that, no matter what boneheaded
153      floating-point operation it performs, the process will not halt.  */
154 #if HAVE_FEHOLDEXCEPT
155   {
156     fenv_t foo;
157
158     feholdexcept (&foo);
159   }
160 #elif HAVE___SETFPUCW && defined(_FPU_IEEE)
161   __setfpucw (_FPU_IEEE);
162 #elif __BORLANDC__
163   _control87 (0xffff, 0x137f);
164 #endif
165
166 #if ENDIAN==UNKNOWN
167   {
168     /* Test for endianness borrowed from acspecific.m4, which was in
169      turn borrowed from Harbison&Steele. */
170     union
171       {
172         long l;
173         char c[sizeof (long)];
174       }
175     u;
176
177     u.l = 1;
178     if (u.c[sizeof u.l - 1] == 1)
179       endian = BIG;
180     else if (u.c[0] == 1)
181       endian = LITTLE;
182     else
183       msg (FE, _("Your machine does not appear to be either big- or little-"
184                  "endian.  At the moment, PSPP only supports machines of "
185                  "these standard endiannesses.  If you want to hack in "
186                  "others, contact the author."));
187   }
188 #endif
189
190   /* PORTME: Set the value for second_lowest_value, which is the
191      "second lowest" possible value for a double.  This is the value
192      for LOWEST on MISSING VALUES, etc. */
193 #ifndef SECOND_LOWEST_VALUE
194 #if FPREP == FPREP_IEEE754
195   {
196     union
197       {
198         unsigned char c[8];
199         double d;
200       }
201     second_lowest_little = {{0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xef, 0xff}},
202     second_lowest_big = {{0xff, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe}};
203
204     if (endian == LITTLE)
205       second_lowest_value = second_lowest_little.d;
206     else if (endian == BIG)
207       second_lowest_value = second_lowest_big.d;
208   }
209 #else /* FPREP != FPREP_IEEE754 */
210 #error Unknown floating-point representation.
211 #endif /* FPREP != FPREP_IEEE754 */
212 #endif /* !SECOND_LOWEST_VALUE */
213
214   /* var.h */
215   default_dict.var_by_name = avl_create (NULL, cmp_variable, NULL);
216
217   vec_init (&reinit_sysmis);
218   vec_init (&reinit_blanks);
219   vec_init (&init_zero);
220   vec_init (&init_blanks);
221
222   last_vfm_invocation = time (NULL);
223
224   /* lexer.h */
225   ds_init (NULL, &tokstr, 64);
226
227   /* common.h */
228   {
229     char *cp;
230     
231     pgmname = argv[0];
232     for (;;)
233       {
234         cp = strchr (pgmname, DIR_SEPARATOR);
235         if (!cp)
236           break;
237         pgmname = &cp[1];
238       }
239     cur_proc = NULL;
240   }
241
242   /* settings.h */
243 #if !USE_INTERNAL_PAGER
244   {
245     char *pager;
246
247     pager = getenv ("STAT_PAGER");
248     if (!pager)
249       pager = getenv ("PAGER");
250     if (pager)
251       set_pager = xstrdup (pager);
252 #if DEFAULT_PAGER
253     else
254       set_pager = xstrdup (DEFAULT_PAGER);
255 #endif /* DEFAULT_PAGER */
256   }
257 #endif /* !USE_INTERNAL_PAGER */
258
259   set_blanks = SYSMIS;
260   set_scompression = 1;
261   set_format.type = FMT_F;
262   set_format.w = 8;
263   set_format.d = 2;
264   set_cpi = 6;
265   set_lpi = 10;
266   set_results_file = xstrdup ("pspp.prc");
267   set_dprompt = xstrdup (_("data> "));
268   
269   {
270     int i;
271     
272     for (i = 0; i < 5; i++)
273       {
274         struct set_cust_currency *cc = &set_cc[i];
275         strcpy (cc->buf, "-");
276         cc->neg_prefix = cc->buf;
277         cc->prefix = &cc->buf[1];
278         cc->suffix = &cc->buf[1];
279         cc->neg_suffix = &cc->buf[1];
280         cc->decimal = '.';
281         cc->grouping = ',';
282       }
283   }
284   
285   set_decimal = '.';
286   set_grouping = ',';
287   set_headers = 1;
288   set_journaling = 1;
289   set_journal = xstrdup ("pspp.jnl");
290   set_messages = 1;
291   set_mexpand = 1;
292   set_mprint = 1;
293   set_mxerrs = 50;
294   set_mxwarns = 100;
295   set_printback = 1;
296   set_undefined = 1;
297
298   set_cprompt = xstrdup ("    > ");
299   set_echo = 0;
300   set_endcmd = '.';
301   set_errorbreak = 0;
302   set_include = 1;
303   set_nullline = 1;
304   set_more = 1;
305   set_prompt = xstrdup ("PSPP> ");
306   set_seed = 2000000;
307
308 #if __DJGPP__ || __BORLANDC__
309   {
310     struct text_info ti;
311
312     gettextinfo (&ti);
313     set_viewlength = max (ti.screenheight, 25);
314     set_viewwidth = max (ti.screenwidth, 79);
315   }
316 #elif HAVE_LIBTERMCAP
317   {
318     char *termtype;
319     int success;
320
321     /* This code stolen from termcap.info, though modified. */
322 #if !__CHECKER__
323     term_buffer = xmalloc (2048);
324 #endif
325
326     termtype = getenv ("TERM");
327     if (!termtype)
328       msg (FE, _("Specify a terminal type with `setenv TERM <yourtype>'."));
329
330 #if __CHECKER__
331     success = tgetent (NULL, termtype);
332 #else
333     success = tgetent (term_buffer, termtype);
334 #endif
335
336     if (success <= 0)
337       {
338         if (success < 0)
339           msg (IE, _("Could not access the termcap data base."));
340         else
341           msg (IE, _("Terminal type `%s' is not defined."), termtype);
342         msg (MM, _("Assuming screen of size 79x25."));
343         set_viewlength = 25;
344         set_viewwidth = 79;
345       }
346     else
347       {
348         set_viewlength = tgetnum ("li");
349         set_viewwidth = tgetnum ("co") - 1;
350       }
351   }
352 #else /* !HAVE_LIBTERMCAP */
353   set_viewlength = 25;
354   set_viewwidth = 79;
355 #endif /* !HAVE_LIBTERMCAP */
356
357   /* log.h */
358   logging = 1;
359   logfn = xstrdup ("pspp.log");
360   logfile = NULL;
361
362   /* file-handle.h */
363   {
364     extern void fh_init_files (void);
365     
366     fh_init_files ();
367   }
368   
369   get_date ();
370 }
371
372 static void
373 get_date ()
374 {
375   static const char *months[12] =
376     {
377       N_("Jan"), N_("Feb"), N_("Mar"), N_("Apr"), N_("May"), N_("Jun"),
378       N_("Jul"), N_("Aug"), N_("Sep"), N_("Oct"), N_("Nov"), N_("Dec"),
379     };
380
381   time_t t;
382   int mn, dy, yr;
383   struct tm *tmp;
384
385   if ((time_t) -1 == time (&t))
386     {
387       strcpy (curdate, "1 Jan 1970");
388       return;
389     }
390   tmp = localtime (&t);
391
392   mn = tmp->tm_mon;
393   if (mn < 0)
394     mn = 0;
395   if (mn > 11)
396     mn = 11;
397
398   dy = tmp->tm_mday;
399   if (dy < 0)
400     dy = 0;
401   if (dy > 99)
402     dy = 99;
403
404   yr = tmp->tm_year + 1900;
405   if (yr < 0)
406     yr = 0;
407   if (yr > 9999)
408     yr = 9999;
409
410   sprintf (curdate, "%2d %s %04d", dy, gettext (months[mn]), yr);
411 }
412
413 int
414 cmp_variable (const void *a, const void *b, void *foo unused)
415 {
416   return strcmp (((struct variable *) a)->name, ((struct variable *) b)->name);
417 }
418
419 #if __BORLANDC__
420 int
421 _RTLENTRY _EXPFUNC _matherr (struct exception _FAR *__e)
422 {
423   return 1;
424 }
425
426 int
427 _RTLENTRY _EXPFUNC _matherrl (struct _exceptionl _FAR *__e)
428 {
429   return 1;
430 }
431 #endif