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