1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
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.
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.
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
20 /* AIX requires this to be the first thing in the file. */
23 #define alloca __builtin_alloca
31 #ifndef alloca /* predefined by HP cc +Olibcalls */
45 #include "file-handle.h"
54 /*#define DEBUGGING 1*/
55 #include "debug-print.h"
57 /* file_handle extension structure. */
60 struct file_ext file; /* Associated file. */
62 char *line; /* Current line, not null-terminated. */
63 size_t len; /* Length of line. */
65 char *ptr; /* Pointer into line that is returned by
67 size_t size; /* Number of bytes allocated for line. */
68 int advance; /* Nonzero=dfm_get_record() reads a new
69 record; otherwise returns current record. */
72 /* These are defined at the end of this file. */
73 static struct fh_ext_class dfm_r_class;
74 static struct fh_ext_class dfm_w_class;
76 static void read_record (struct file_handle *h);
78 /* Internal (low level). */
80 /* Closes the file handle H which was opened by open_file_r() or
83 dfm_close (struct file_handle *h)
85 struct dfm_fhuser_ext *ext = h->ext;
87 /* Skip any remaining data on the inline file. */
89 while (ext->line != NULL)
92 msg (VM (2), _("%s: Closing data-file handle %s."),
93 fh_handle_filename (h), fh_handle_name (h));
94 assert (h->class == &dfm_r_class || h->class == &dfm_w_class);
97 fn_close_ext (&ext->file);
98 free (ext->file.filename);
99 ext->file.filename = NULL;
105 /* Initializes EXT properly as an inline data file. */
107 open_inline_file (struct dfm_fhuser_ext *ext)
109 /* We want to indicate that the file is open, that we are not at
110 eof, and that another line needs to be read in. */
111 ext->file.file = NULL;
112 ext->line = xmalloc (128);
114 strcpy (ext->line, _("<<Bug in dfm.c>>"));
116 ext->len = strlen (ext->line);
117 ext->ptr = ext->line;
122 /* Opens a file handle for reading as a data file. */
124 open_file_r (struct file_handle *h)
126 struct dfm_fhuser_ext ext;
128 h->where.line_number = 0;
129 ext.file.file = NULL;
136 msg (VM (1), _("%s: Opening data-file handle %s for reading."),
137 fh_handle_filename (h), fh_handle_name (h));
140 if (h == inline_file)
144 /* WTF can't this just be done with tokens?
145 Is this really a special case?
151 if (!getl_read_line ())
153 msg (SE, _("BEGIN DATA expected."));
157 /* Skip leading whitespace, separate out first word, so that
158 S points to a single word reduced to lowercase. */
159 s = ds_value (&getl_buf);
160 while (isspace ((unsigned char) *s))
162 for (cp = s; isalpha ((unsigned char) *cp); cp++)
163 *cp = tolower ((unsigned char) (*cp));
164 ds_truncate (&getl_buf, cp - s);
168 if (!lex_id_match_len ("begin", 5, s, strcspn (s, " \t\r\v\n")))
170 msg (SE, _("BEGIN DATA expected."));
172 lex_preprocess_line ();
175 getl_prompt = GETL_PRPT_DATA;
177 open_inline_file (&ext);
181 ext.file.filename = xstrdup (h->norm_fn);
182 ext.file.mode = "rb";
183 ext.file.file = NULL;
184 ext.file.sequence_no = NULL;
185 ext.file.param = NULL;
186 ext.file.postopen = NULL;
187 ext.file.preclose = NULL;
188 if (!fn_open_ext (&ext.file))
190 msg (ME, _("An error occurred while opening \"%s\" for reading "
191 "as a data file: %s."), h->fn, strerror (errno));
197 h->class = &dfm_r_class;
198 h->ext = xmalloc (sizeof (struct dfm_fhuser_ext));
199 memcpy (h->ext, &ext, sizeof (struct dfm_fhuser_ext));
204 /* Opens a file handle for writing as a data file. */
206 open_file_w (struct file_handle *h)
208 struct dfm_fhuser_ext ext;
210 ext.file.file = NULL;
217 h->where.line_number = 0;
219 msg (VM (1), _("%s: Opening data-file handle %s for writing."),
220 fh_handle_filename (h), fh_handle_name (h));
223 if (h == inline_file)
225 msg (ME, _("Cannot open the inline file for writing."));
230 ext.file.filename = xstrdup (h->norm_fn);
231 ext.file.mode = "wb";
232 ext.file.file = NULL;
233 ext.file.sequence_no = NULL;
234 ext.file.param = NULL;
235 ext.file.postopen = NULL;
236 ext.file.preclose = NULL;
238 if (!fn_open_ext (&ext.file))
240 msg (ME, _("An error occurred while opening \"%s\" for writing "
241 "as a data file: %s."), h->fn, strerror (errno));
246 h->class = &dfm_w_class;
247 h->ext = xmalloc (sizeof (struct dfm_fhuser_ext));
248 memcpy (h->ext, &ext, sizeof (struct dfm_fhuser_ext));
253 /* Ensures that the line buffer in file handle with extension EXT is
254 big enough to hold a line of length EXT->LEN characters not
255 including null terminator. */
256 #define force_line_buffer_expansion() \
259 if (ext->len + 1 > ext->size) \
261 ext->size = ext->len * 2; \
262 ext->line = xrealloc (ext->line, ext->size); \
267 /* Counts the number of tabs in string STRING of length LEN. */
269 count_tabs (char *s, size_t len)
275 char *cp = memchr (s, '\t', len);
284 /* Converts all the tabs in H->EXT->LINE to an equivalent number of
285 spaces, if necessary. */
287 tabs_to_spaces (struct file_handle *h)
289 struct dfm_fhuser_ext *ext = h->ext;
291 char *first_tab; /* Location of first tab (if any). */
292 char *second_tab; /* Location of second tab (if any). */
293 size_t orig_len; /* Line length at function entry. */
295 /* If there aren't any tabs then there's nothing to do. */
296 first_tab = memchr (ext->line, '\t', ext->len);
297 if (first_tab == NULL)
301 /* If there's just one tab then expand it inline. Otherwise do a
302 full string copy to another buffer. */
303 second_tab = memchr (first_tab + 1, '\t',
304 ext->len - (first_tab - ext->line + 1));
305 if (second_tab == NULL)
307 int n_spaces = 8 - (first_tab - ext->line) % 8;
309 ext->len += n_spaces - 1;
311 /* Expand the line if necessary, keeping the first_tab pointer
314 size_t ofs = first_tab - ext->line;
315 force_line_buffer_expansion ();
316 first_tab = ext->line + ofs;
319 memmove (first_tab + n_spaces, first_tab + 1,
320 orig_len - (first_tab - ext->line + 1));
321 memset (first_tab, ' ', n_spaces);
323 /* Make a local copy of original text. */
324 char *orig_line = local_alloc (ext->len + 1);
325 memcpy (orig_line, ext->line, ext->len);
327 /* Allocate memory assuming we need to add 8 spaces for every tab. */
328 ext->len += 2 + count_tabs (second_tab + 1,
329 ext->len - (second_tab - ext->line + 1));
331 /* Expand the line if necessary, keeping the first_tab pointer
334 size_t ofs = first_tab - ext->line;
335 force_line_buffer_expansion ();
336 first_tab = ext->line + ofs;
339 /* Walk through orig_line, expanding tabs into ext->line. */
341 char *src_p = orig_line + (first_tab - ext->line);
342 char *dest_p = first_tab;
344 for (; src_p < orig_line + orig_len; src_p++)
346 /* Most characters simply pass through untouched. */
353 /* Tabs are expanded into an equivalent number of
356 int n_spaces = 8 - (dest_p - ext->line) % 8;
358 memset (dest_p, ' ', n_spaces);
363 /* Supply null terminator and actual string length. */
365 ext->len = dest_p - ext->line;
368 local_free (orig_line);
372 /* Reads a record from H->EXT->FILE into H->EXT->LINE, setting
373 H->EXT->PTR to H->EXT->LINE, and setting H->EXT-LEN to the length
374 of the line. The line is not null-terminated. If an error occurs
375 or end-of-file is encountered, H->EXT->LINE is set to NULL. */
377 read_record (struct file_handle *h)
379 struct dfm_fhuser_ext *ext = h->ext;
381 if (h == inline_file)
383 if (!getl_read_line ())
385 msg (SE, _("Unexpected end-of-file while reading data in BEGIN "
386 "DATA. This probably indicates "
387 "a missing or misformatted END DATA command. "
388 "END DATA must appear by itself on a single line "
389 "with exactly one space between words."));
393 h->where.line_number++;
395 if (ds_length (&getl_buf) >= 8
396 && !strncasecmp (ds_value (&getl_buf), "end data", 8))
398 lex_set_prog (ds_value (&getl_buf) + ds_length (&getl_buf));
402 ext->len = ds_length (&getl_buf);
403 force_line_buffer_expansion ();
404 strcpy (ext->line, ds_value (&getl_buf));
408 if (h->recform == FH_RF_VARIABLE)
410 /* PORTME: here you should adapt the routine to your
411 system's concept of a "line" of text. */
412 int read_len = getline (&ext->line, &ext->size, ext->file.file);
416 if (ferror (ext->file.file))
418 msg (ME, _("Error reading file %s: %s."),
419 fh_handle_name (h), strerror (errno));
424 ext->len = (size_t) read_len;
426 else if (h->recform == FH_RF_FIXED)
430 if (ext->size < h->lrecl)
432 ext->size = h->lrecl;
433 ext->line = xmalloc (ext->size);
435 amt = fread (ext->line, 1, h->lrecl, ext->file.file);
438 if (ferror (ext->file.file))
439 msg (ME, _("Error reading file %s: %s."),
440 fh_handle_name (h), strerror (errno));
442 msg (ME, _("%s: Partial record at end of file."),
454 h->where.line_number++;
457 /* Strip trailing whitespace, I forget why. But there's a good
458 reason, I'm sure. I'm too scared to eliminate this code. */
459 if (h->recform == FH_RF_VARIABLE)
461 while (ext->len && isspace ((unsigned char) ext->line[ext->len - 1]))
464 /* Convert tabs to spaces. */
467 ext->ptr = ext->line;
472 /*hit eof or an error, clean up everything. */
476 ext->line = ext->ptr = NULL;
480 /* Public (high level). */
482 /* Returns the current record in the file corresponding to HANDLE.
483 Opens files and reads records, etc., as necessary. Sets *LEN to
484 the length of the line. The line returned is not null-terminated.
485 Returns NULL at end of file. Calls fail() on attempt to read past
488 dfm_get_record (struct file_handle *h, int *len)
490 if (h->class == NULL)
492 if (!open_file_r (h))
496 else if (h->class != &dfm_r_class)
498 msg (ME, _("Cannot read from file %s already opened for %s."),
499 fh_handle_name (h), gettext (h->class->name));
504 struct dfm_fhuser_ext *ext = h->ext;
512 msg (SE, _("Attempt to read beyond end-of-file on file %s."),
520 struct dfm_fhuser_ext *ext = h->ext;
526 *len = ext->len - (ext->ptr - ext->line);
534 /* Come here on reading beyond eof or reading from a file already
535 open for something else. */
541 /* Causes dfm_get_record() to read in the next record the next time it
542 is executed on file HANDLE. */
544 dfm_fwd_record (struct file_handle *h)
546 struct dfm_fhuser_ext *ext = h->ext;
548 assert (h->class == &dfm_r_class);
552 /* Cancels the effect of any previous dfm_fwd_record() executed on
553 file HANDLE. Sets the current line to begin in the 1-based column
554 COLUMN, as with dfm_set_record but based on a column number instead
555 of a character pointer. */
557 dfm_bkwd_record (struct file_handle *h, int column)
559 struct dfm_fhuser_ext *ext = h->ext;
561 assert (h->class == &dfm_r_class);
563 ext->ptr = ext->line + min ((int) ext->len + 1, column) - 1;
566 /* Sets the current line in HANDLE to NEW_LINE, which must point
567 somewhere in the line last returned by dfm_get_record(). Used by
568 DATA LIST FREE to strip the leading portion off the current line. */
570 dfm_set_record (struct file_handle *h, char *new_line)
572 struct dfm_fhuser_ext *ext = h->ext;
574 assert (h->class == &dfm_r_class);
578 /* Returns the 0-based current column to which the line pointer in
579 HANDLE is set. Unless dfm_set_record() or dfm_bkwd_record() have
580 been called, this is 0. */
582 dfm_get_cur_col (struct file_handle *h)
584 struct dfm_fhuser_ext *ext = h->ext;
586 assert (h->class == &dfm_r_class);
587 return ext->ptr - ext->line;
590 /* Writes record REC having length LEN to the file corresponding to
591 HANDLE. REC is not null-terminated. Returns nonzero on success,
594 dfm_put_record (struct file_handle *h, const char *rec, size_t len)
599 if (h->class == NULL)
601 if (!open_file_w (h))
604 else if (h->class != &dfm_w_class)
606 msg (ME, _("Cannot write to file %s already opened for %s."),
607 fh_handle_name (h), gettext (h->class->name));
612 if (h->recform == FH_RF_FIXED && len < h->lrecl)
617 ptr = local_alloc (amt);
618 memcpy (ptr, rec, len);
619 ch = h->mode == FH_MD_CHARACTER ? ' ' : 0;
620 memset (&ptr[len], ch, amt - len);
628 if (1 != fwrite (ptr, amt, 1, ((struct dfm_fhuser_ext *) h->ext)->file.file))
630 msg (ME, _("Error writing file %s: %s."), fh_handle_name (h),
642 /* Pushes the filename and line number on the fn/ln stack. */
644 dfm_push (struct file_handle *h)
646 if (h != inline_file)
647 err_push_file_locator (&h->where);
650 /* Pops the filename and line number from the fn/ln stack. */
652 dfm_pop (struct file_handle *h)
654 if (h != inline_file)
655 err_pop_file_locator (&h->where);
658 /* BEGIN DATA...END DATA procedure. */
660 /* Perform BEGIN DATA...END DATA as a procedure in itself. */
662 cmd_begin_data (void)
664 struct dfm_fhuser_ext *ext;
666 /* FIXME: figure out the *exact* conditions, not these really
667 lenient conditions. */
668 if (vfm_source == NULL
669 || vfm_source == &vfm_memory_stream
670 || vfm_source == &vfm_disk_stream
671 || vfm_source == &sort_stream)
673 msg (SE, _("This command is not valid here since the current "
674 "input program does not access the inline file."));
679 /* Initialize inline_file. */
680 msg (VM (1), _("inline file: Opening for reading."));
681 inline_file->class = &dfm_r_class;
682 inline_file->ext = xmalloc (sizeof (struct dfm_fhuser_ext));
683 open_inline_file (inline_file->ext);
685 /* We don't actually read from the inline file. The input procedure
686 is what reads from it. */
687 getl_prompt = GETL_PRPT_DATA;
688 procedure (NULL, NULL, NULL);
690 ext = inline_file->ext;
692 if (ext && ext->line)
694 msg (MW, _("Skipping remaining inline data."));
695 for (read_record (inline_file); ext->line; read_record (inline_file))
698 assert (inline_file->ext == NULL);
703 static struct fh_ext_class dfm_r_class =
706 N_("reading as a data file"),
710 static struct fh_ext_class dfm_w_class =
713 N_("writing as a data file"),