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