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"
53 #include "debug-print.h"
55 /* file_handle extension structure. */
58 struct file_ext file; /* Associated file. */
60 char *line; /* Current line, not null-terminated. */
61 size_t len; /* Length of line. */
63 char *ptr; /* Pointer into line that is returned by
65 size_t size; /* Number of bytes allocated for line. */
66 int advance; /* Nonzero=dfm_get_record() reads a new
67 record; otherwise returns current record. */
70 /* These are defined at the end of this file. */
71 static struct fh_ext_class dfm_r_class;
72 static struct fh_ext_class dfm_w_class;
74 static void read_record (struct file_handle *h);
76 /* Internal (low level). */
78 /* Closes the file handle H which was opened by open_file_r() or
81 dfm_close (struct file_handle *h)
83 struct dfm_fhuser_ext *ext = h->ext;
85 /* Skip any remaining data on the inline file. */
87 while (ext->line != NULL)
90 msg (VM (2), _("%s: Closing data-file handle %s."),
91 fh_handle_filename (h), fh_handle_name (h));
92 assert (h->class == &dfm_r_class || h->class == &dfm_w_class);
95 fn_close_ext (&ext->file);
96 free (ext->file.filename);
97 ext->file.filename = NULL;
103 /* Initializes EXT properly as an inline data file. */
105 open_inline_file (struct dfm_fhuser_ext *ext)
107 /* We want to indicate that the file is open, that we are not at
108 eof, and that another line needs to be read in. */
109 ext->file.file = NULL;
110 ext->line = xmalloc (128);
112 strcpy (ext->line, _("<<Bug in dfm.c>>"));
114 ext->len = strlen (ext->line);
115 ext->ptr = ext->line;
120 /* Opens a file handle for reading as a data file. */
122 open_file_r (struct file_handle *h)
124 struct dfm_fhuser_ext ext;
126 h->where.line_number = 0;
127 ext.file.file = NULL;
134 msg (VM (1), _("%s: Opening data-file handle %s for reading."),
135 fh_handle_filename (h), fh_handle_name (h));
138 if (h == inline_file)
142 /* WTF can't this just be done with tokens?
143 Is this really a special case?
149 if (!getl_read_line ())
151 msg (SE, _("BEGIN DATA expected."));
155 /* Skip leading whitespace, separate out first word, so that
156 S points to a single word reduced to lowercase. */
157 s = ds_value (&getl_buf);
158 while (isspace ((unsigned char) *s))
160 for (cp = s; isalpha ((unsigned char) *cp); cp++)
161 *cp = tolower ((unsigned char) (*cp));
162 ds_truncate (&getl_buf, cp - s);
166 if (!lex_id_match_len ("begin", 5, s, strcspn (s, " \t\r\v\n")))
168 msg (SE, _("BEGIN DATA expected."));
170 lex_preprocess_line ();
173 getl_prompt = GETL_PRPT_DATA;
175 open_inline_file (&ext);
179 ext.file.filename = xstrdup (h->norm_fn);
180 ext.file.mode = "rb";
181 ext.file.file = NULL;
182 ext.file.sequence_no = NULL;
183 ext.file.param = NULL;
184 ext.file.postopen = NULL;
185 ext.file.preclose = NULL;
186 if (!fn_open_ext (&ext.file))
188 msg (ME, _("An error occurred while opening \"%s\" for reading "
189 "as a data file: %s."), h->fn, strerror (errno));
195 h->class = &dfm_r_class;
196 h->ext = xmalloc (sizeof (struct dfm_fhuser_ext));
197 memcpy (h->ext, &ext, sizeof (struct dfm_fhuser_ext));
202 /* Opens a file handle for writing as a data file. */
204 open_file_w (struct file_handle *h)
206 struct dfm_fhuser_ext ext;
208 ext.file.file = NULL;
215 h->where.line_number = 0;
217 msg (VM (1), _("%s: Opening data-file handle %s for writing."),
218 fh_handle_filename (h), fh_handle_name (h));
221 if (h == inline_file)
223 msg (ME, _("Cannot open the inline file for writing."));
228 ext.file.filename = xstrdup (h->norm_fn);
229 ext.file.mode = "wb";
230 ext.file.file = NULL;
231 ext.file.sequence_no = NULL;
232 ext.file.param = NULL;
233 ext.file.postopen = NULL;
234 ext.file.preclose = NULL;
236 if (!fn_open_ext (&ext.file))
238 msg (ME, _("An error occurred while opening \"%s\" for writing "
239 "as a data file: %s."), h->fn, strerror (errno));
244 h->class = &dfm_w_class;
245 h->ext = xmalloc (sizeof (struct dfm_fhuser_ext));
246 memcpy (h->ext, &ext, sizeof (struct dfm_fhuser_ext));
251 /* Ensures that the line buffer in file handle with extension EXT is
252 big enough to hold a line of length EXT->LEN characters not
253 including null terminator. */
254 #define force_line_buffer_expansion() \
257 if (ext->len + 1 > ext->size) \
259 ext->size = ext->len * 2; \
260 ext->line = xrealloc (ext->line, ext->size); \
265 /* Counts the number of tabs in string STRING of length LEN. */
267 count_tabs (char *s, size_t len)
273 char *cp = memchr (s, '\t', len);
282 /* Converts all the tabs in H->EXT->LINE to an equivalent number of
283 spaces, if necessary. */
285 tabs_to_spaces (struct file_handle *h)
287 struct dfm_fhuser_ext *ext = h->ext;
289 char *first_tab; /* Location of first tab (if any). */
290 char *second_tab; /* Location of second tab (if any). */
291 size_t orig_len; /* Line length at function entry. */
293 /* If there aren't any tabs then there's nothing to do. */
294 first_tab = memchr (ext->line, '\t', ext->len);
295 if (first_tab == NULL)
299 /* If there's just one tab then expand it inline. Otherwise do a
300 full string copy to another buffer. */
301 second_tab = memchr (first_tab + 1, '\t',
302 ext->len - (first_tab - ext->line + 1));
303 if (second_tab == NULL)
305 int n_spaces = 8 - (first_tab - ext->line) % 8;
307 ext->len += n_spaces - 1;
309 /* Expand the line if necessary, keeping the first_tab pointer
312 size_t ofs = first_tab - ext->line;
313 force_line_buffer_expansion ();
314 first_tab = ext->line + ofs;
317 memmove (first_tab + n_spaces, first_tab + 1,
318 orig_len - (first_tab - ext->line + 1));
319 memset (first_tab, ' ', n_spaces);
321 /* Make a local copy of original text. */
322 char *orig_line = local_alloc (ext->len + 1);
323 memcpy (orig_line, ext->line, ext->len);
325 /* Allocate memory assuming we need to add 8 spaces for every tab. */
326 ext->len += 2 + count_tabs (second_tab + 1,
327 ext->len - (second_tab - ext->line + 1));
329 /* Expand the line if necessary, keeping the first_tab pointer
332 size_t ofs = first_tab - ext->line;
333 force_line_buffer_expansion ();
334 first_tab = ext->line + ofs;
337 /* Walk through orig_line, expanding tabs into ext->line. */
339 char *src_p = orig_line + (first_tab - ext->line);
340 char *dest_p = first_tab;
342 for (; src_p < orig_line + orig_len; src_p++)
344 /* Most characters simply pass through untouched. */
351 /* Tabs are expanded into an equivalent number of
354 int n_spaces = 8 - (dest_p - ext->line) % 8;
356 memset (dest_p, ' ', n_spaces);
361 /* Supply null terminator and actual string length. */
363 ext->len = dest_p - ext->line;
366 local_free (orig_line);
370 /* Reads a record from H->EXT->FILE into H->EXT->LINE, setting
371 H->EXT->PTR to H->EXT->LINE, and setting H->EXT-LEN to the length
372 of the line. The line is not null-terminated. If an error occurs
373 or end-of-file is encountered, H->EXT->LINE is set to NULL. */
375 read_record (struct file_handle *h)
377 struct dfm_fhuser_ext *ext = h->ext;
379 if (h == inline_file)
381 if (!getl_read_line ())
383 msg (SE, _("Unexpected end-of-file while reading data in BEGIN "
384 "DATA. This probably indicates "
385 "a missing or misformatted END DATA command. "
386 "END DATA must appear by itself on a single line "
387 "with exactly one space between words."));
391 h->where.line_number++;
393 if (ds_length (&getl_buf) >= 8
394 && !strncasecmp (ds_value (&getl_buf), "end data", 8))
396 lex_set_prog (ds_value (&getl_buf) + ds_length (&getl_buf));
400 ext->len = ds_length (&getl_buf);
401 force_line_buffer_expansion ();
402 strcpy (ext->line, ds_value (&getl_buf));
406 if (h->recform == FH_RF_VARIABLE)
408 /* PORTME: here you should adapt the routine to your
409 system's concept of a "line" of text. */
410 int read_len = getline (&ext->line, &ext->size, ext->file.file);
414 if (ferror (ext->file.file))
416 msg (ME, _("Error reading file %s: %s."),
417 fh_handle_name (h), strerror (errno));
422 ext->len = (size_t) read_len;
424 else if (h->recform == FH_RF_FIXED)
428 if (ext->size < h->lrecl)
430 ext->size = h->lrecl;
431 ext->line = xmalloc (ext->size);
433 amt = fread (ext->line, 1, h->lrecl, ext->file.file);
436 if (ferror (ext->file.file))
437 msg (ME, _("Error reading file %s: %s."),
438 fh_handle_name (h), strerror (errno));
440 msg (ME, _("%s: Partial record at end of file."),
452 h->where.line_number++;
455 /* Strip trailing whitespace, I forget why. But there's a good
456 reason, I'm sure. I'm too scared to eliminate this code. */
457 if (h->recform == FH_RF_VARIABLE)
459 while (ext->len && isspace ((unsigned char) ext->line[ext->len - 1]))
462 /* Convert tabs to spaces. */
465 ext->ptr = ext->line;
470 /*hit eof or an error, clean up everything. */
474 ext->line = ext->ptr = NULL;
478 /* Public (high level). */
480 /* Returns the current record in the file corresponding to HANDLE.
481 Opens files and reads records, etc., as necessary. Sets *LEN to
482 the length of the line. The line returned is not null-terminated.
483 Returns NULL at end of file. Calls fail() on attempt to read past
486 dfm_get_record (struct file_handle *h, int *len)
488 if (h->class == NULL)
490 if (!open_file_r (h))
494 else if (h->class != &dfm_r_class)
496 msg (ME, _("Cannot read from file %s already opened for %s."),
497 fh_handle_name (h), gettext (h->class->name));
502 struct dfm_fhuser_ext *ext = h->ext;
510 msg (SE, _("Attempt to read beyond end-of-file on file %s."),
518 struct dfm_fhuser_ext *ext = h->ext;
524 *len = ext->len - (ext->ptr - ext->line);
532 /* Come here on reading beyond eof or reading from a file already
533 open for something else. */
539 /* Causes dfm_get_record() to read in the next record the next time it
540 is executed on file HANDLE. */
542 dfm_fwd_record (struct file_handle *h)
544 struct dfm_fhuser_ext *ext = h->ext;
546 assert (h->class == &dfm_r_class);
550 /* Cancels the effect of any previous dfm_fwd_record() executed on
551 file HANDLE. Sets the current line to begin in the 1-based column
552 COLUMN, as with dfm_set_record but based on a column number instead
553 of a character pointer. */
555 dfm_bkwd_record (struct file_handle *h, int column)
557 struct dfm_fhuser_ext *ext = h->ext;
559 assert (h->class == &dfm_r_class);
561 ext->ptr = ext->line + min ((int) ext->len + 1, column) - 1;
564 /* Sets the current line in HANDLE to NEW_LINE, which must point
565 somewhere in the line last returned by dfm_get_record(). Used by
566 DATA LIST FREE to strip the leading portion off the current line. */
568 dfm_set_record (struct file_handle *h, char *new_line)
570 struct dfm_fhuser_ext *ext = h->ext;
572 assert (h->class == &dfm_r_class);
576 /* Returns the 0-based current column to which the line pointer in
577 HANDLE is set. Unless dfm_set_record() or dfm_bkwd_record() have
578 been called, this is 0. */
580 dfm_get_cur_col (struct file_handle *h)
582 struct dfm_fhuser_ext *ext = h->ext;
584 assert (h->class == &dfm_r_class);
585 return ext->ptr - ext->line;
588 /* Writes record REC having length LEN to the file corresponding to
589 HANDLE. REC is not null-terminated. Returns nonzero on success,
592 dfm_put_record (struct file_handle *h, const char *rec, size_t len)
597 if (h->class == NULL)
599 if (!open_file_w (h))
602 else if (h->class != &dfm_w_class)
604 msg (ME, _("Cannot write to file %s already opened for %s."),
605 fh_handle_name (h), gettext (h->class->name));
610 if (h->recform == FH_RF_FIXED && len < h->lrecl)
615 ptr = local_alloc (amt);
616 memcpy (ptr, rec, len);
617 ch = h->mode == FH_MD_CHARACTER ? ' ' : 0;
618 memset (&ptr[len], ch, amt - len);
626 if (1 != fwrite (ptr, amt, 1, ((struct dfm_fhuser_ext *) h->ext)->file.file))
628 msg (ME, _("Error writing file %s: %s."), fh_handle_name (h),
640 /* Pushes the filename and line number on the fn/ln stack. */
642 dfm_push (struct file_handle *h)
644 if (h != inline_file)
645 err_push_file_locator (&h->where);
648 /* Pops the filename and line number from the fn/ln stack. */
650 dfm_pop (struct file_handle *h)
652 if (h != inline_file)
653 err_pop_file_locator (&h->where);
656 /* BEGIN DATA...END DATA procedure. */
658 /* Perform BEGIN DATA...END DATA as a procedure in itself. */
660 cmd_begin_data (void)
662 struct dfm_fhuser_ext *ext;
664 /* FIXME: figure out the *exact* conditions, not these really
665 lenient conditions. */
666 if (vfm_source == NULL
667 || vfm_source == &vfm_memory_stream
668 || vfm_source == &vfm_disk_stream
669 || vfm_source == &sort_stream)
671 msg (SE, _("This command is not valid here since the current "
672 "input program does not access the inline file."));
677 /* Initialize inline_file. */
678 msg (VM (1), _("inline file: Opening for reading."));
679 inline_file->class = &dfm_r_class;
680 inline_file->ext = xmalloc (sizeof (struct dfm_fhuser_ext));
681 open_inline_file (inline_file->ext);
683 /* We don't actually read from the inline file. The input procedure
684 is what reads from it. */
685 getl_prompt = GETL_PRPT_DATA;
686 procedure (NULL, NULL, NULL);
688 ext = inline_file->ext;
690 if (ext && ext->line)
692 msg (MW, _("Skipping remaining inline data."));
693 for (read_record (inline_file); ext->line; read_record (inline_file))
696 assert (inline_file->ext == NULL);
701 static struct fh_ext_class dfm_r_class =
704 N_("reading as a data file"),
708 static struct fh_ext_class dfm_w_class =
711 N_("writing as a data file"),