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 /* FIXME: seems like a lot of code duplication with data-list.c. */
28 #include "dfm-write.h"
31 #include "file-handle.h"
38 /* Describes what to do when an output field is encountered. */
41 PRT_ERROR, /* Invalid value. */
42 PRT_NEWLINE, /* Newline. */
43 PRT_CONST, /* Constant string. */
44 PRT_VAR, /* Variable. */
45 PRT_SPACE /* A single space. */
48 /* Describes how to output one field. */
51 struct prt_out_spec *next;
52 int type; /* PRT_* constant. */
53 int fc; /* 0-based first column. */
56 char *c; /* PRT_CONST: Associated string. */
59 struct variable *v; /* PRT_VAR: Associated variable. */
60 struct fmt_spec f; /* PRT_VAR: Output spec. */
67 /* Enums for use with print_trns's `options' field. */
70 PRT_CMD_MASK = 1, /* Command type mask. */
71 PRT_PRINT = 0, /* PRINT transformation identifier. */
72 PRT_WRITE = 1, /* WRITE transformation identifier. */
73 PRT_EJECT = 002, /* Can be combined with CMD_PRINT only. */
74 PRT_BINARY = 004 /* File is binary, omit newlines. */
77 /* PRINT, PRINT EJECT, WRITE private data structure. */
81 struct dfm_writer *writer; /* Output file, NULL=listing file. */
82 int options; /* PRT_* bitmapped field. */
83 struct prt_out_spec *spec; /* Output specifications. */
84 int max_width; /* Maximum line width including null. */
85 char *line; /* Buffer for sticking lines in. */
88 /* PRT_PRINT or PRT_WRITE. */
91 /* Holds information on parsing the data file. */
92 static struct print_trns prt;
94 /* Last prt_out_spec in the chain. Used for building the linked-list. */
95 static struct prt_out_spec *next;
97 /* Number of records. */
100 static int internal_cmd_print (int flags);
101 static trns_proc_func print_trns_proc;
102 static trns_free_func print_trns_free;
103 static int parse_specs (void);
104 static void dump_table (const struct file_handle *);
105 static void append_var_spec (struct prt_out_spec *);
106 static void alloc_line (void);
110 /* Parses PRINT command. */
114 return internal_cmd_print (PRT_PRINT);
117 /* Parses PRINT EJECT command. */
119 cmd_print_eject (void)
121 return internal_cmd_print (PRT_PRINT | PRT_EJECT);
124 /* Parses WRITE command. */
128 return internal_cmd_print (PRT_WRITE);
131 /* Parses the output commands. F is PRT_PRINT, PRT_WRITE, or
132 PRT_PRINT|PRT_EJECT. */
134 internal_cmd_print (int f)
136 int table = 0; /* Print table? */
137 struct print_trns *trns; /* malloc()'d transformation. */
138 struct file_handle *fh = NULL;
140 /* Fill in prt to facilitate error-handling. */
141 prt.h.proc = print_trns_proc;
142 prt.h.free = print_trns_free;
150 which_cmd = f & PRT_CMD_MASK;
152 /* Parse the command options. */
153 while (!lex_match ('/'))
155 if (lex_match_id ("OUTFILE"))
163 else if (lex_match_id ("RECORDS"))
167 if (!lex_force_int ())
169 nrec = lex_integer ();
173 else if (lex_match_id ("TABLE"))
175 else if (lex_match_id ("NOTABLE"))
179 lex_error (_("expecting a valid subcommand"));
184 /* Parse variables and strings. */
190 prt.writer = dfm_open_writer (fh);
191 if (prt.writer == NULL)
194 if (handle_get_mode (fh) == MODE_BINARY)
195 prt.options |= PRT_BINARY;
198 /* Output the variable table if requested. */
202 /* Count the maximum line width. Allocate linebuffer if
206 /* Put the transformation in the queue. */
207 trns = xmalloc (sizeof *trns);
208 memcpy (trns, &prt, sizeof *trns);
209 add_transformation ((struct trns_header *) trns);
214 print_trns_free ((struct trns_header *) & prt);
218 /* Appends the field output specification SPEC to the list maintained
221 append_var_spec (struct prt_out_spec *spec)
224 prt.spec = next = xmalloc (sizeof *spec);
226 next = next->next = xmalloc (sizeof *spec);
228 memcpy (next, spec, sizeof *spec);
232 /* Field parsing. Mostly stolen from data-list.c. */
234 /* Used for chaining together fortran-like format specifiers. */
237 struct fmt_list *next;
240 struct fmt_list *down;
243 /* Used as "local" variables among the fixed-format parsing funcs. If
244 it were guaranteed that PSPP were going to be compiled by gcc,
245 I'd make all these functions a single set of nested functions. */
248 struct variable **v; /* variable list */
249 int nv; /* number of variables in list */
250 int cv; /* number of variables from list used up so far
251 by the FORTRAN-like format specifiers */
253 int recno; /* current 1-based record number */
254 int sc; /* 1-based starting column for next variable */
256 struct prt_out_spec spec; /* next format spec to append to list */
257 int fc, lc; /* first, last 1-based column number of current
260 int level; /* recursion level for FORTRAN-like format
265 static int fixed_parse_compatible (void);
266 static struct fmt_list *fixed_parse_fortran (void);
268 static int parse_string_argument (void);
269 static int parse_variable_argument (void);
271 /* Parses all the variable and string specifications on a single
272 PRINT, PRINT EJECT, or WRITE command into the prt structure.
277 /* Return code from called function. */
285 while (lex_match ('/'))
287 int prev_recno = fx.recno;
292 if (!lex_force_int ())
294 if (lex_integer () < fx.recno)
296 msg (SE, _("The record number specified, %ld, is "
297 "before the previous record, %d. Data "
298 "fields must be listed in order of "
299 "increasing record number."),
300 lex_integer (), fx.recno - 1);
303 fx.recno = lex_integer ();
307 fx.spec.type = PRT_NEWLINE;
308 while (prev_recno++ < fx.recno)
309 append_var_spec (&fx.spec);
314 if (token == T_STRING)
315 code = parse_string_argument ();
317 code = parse_variable_argument ();
321 fx.spec.type = PRT_NEWLINE;
322 append_var_spec (&fx.spec);
326 else if (fx.recno > nrec)
328 msg (SE, _("Variables are specified on records that "
329 "should not exist according to RECORDS subcommand."));
335 lex_error (_("expecting end of command"));
342 /* Parses a string argument to the PRINT commands. Returns success. */
344 parse_string_argument (void)
346 fx.spec.type = PRT_CONST;
347 fx.spec.fc = fx.sc - 1;
348 fx.spec.u.c = xstrdup (ds_c_str (&tokstr));
351 /* Parse the included column range. */
354 /* Width of column range in characters. */
357 /* Width of constant string in characters. */
360 /* 1-based index of last column in range. */
363 if (!lex_integer_p () || lex_integer () <= 0)
365 msg (SE, _("%g is not a valid column location."), tokval);
368 fx.spec.fc = lex_integer () - 1;
371 lex_negative_to_dash ();
374 if (!lex_integer_p ())
376 msg (SE, _("Column location expected following `%d-'."),
380 if (lex_integer () <= 0)
382 msg (SE, _("%g is not a valid column location."), tokval);
385 if (lex_integer () < fx.spec.fc + 1)
387 msg (SE, _("%d-%ld is not a valid column range. The second "
388 "column must be greater than or equal to the first."),
389 fx.spec.fc + 1, lex_integer ());
392 lc = lex_integer () - 1;
397 /* If only a starting location is specified then the field is
398 the width of the provided string. */
399 lc = fx.spec.fc + strlen (fx.spec.u.c) - 1;
401 /* Apply the range. */
402 c_len = lc - fx.spec.fc + 1;
403 s_len = strlen (fx.spec.u.c);
405 fx.spec.u.c[c_len] = 0;
406 else if (s_len < c_len)
408 fx.spec.u.c = xrealloc (fx.spec.u.c, c_len + 1);
409 memset (&fx.spec.u.c[s_len], ' ', c_len - s_len);
410 fx.spec.u.c[c_len] = 0;
416 /* If nothing is provided then the field is the width of the
418 fx.sc += strlen (fx.spec.u.c);
420 append_var_spec (&fx.spec);
428 /* Parses a variable argument to the PRINT commands by passing it off
429 to fixed_parse_compatible() or fixed_parse_fortran() as appropriate.
432 parse_variable_argument (void)
434 if (!parse_variables (default_dict, &fx.v, &fx.nv, PV_DUPLICATE))
439 if (!fixed_parse_compatible ())
442 else if (token == '(')
446 if (!fixed_parse_fortran ())
451 /* User wants dictionary format specifiers. */
455 for (i = 0; i < fx.nv; i++)
458 fx.spec.type = PRT_VAR;
459 fx.spec.fc = fx.sc - 1;
460 fx.spec.u.v.v = fx.v[i];
461 fx.spec.u.v.f = fx.v[i]->print;
462 append_var_spec (&fx.spec);
463 fx.sc += fx.v[i]->print.w;
466 fx.spec.type = PRT_SPACE;
467 fx.spec.fc = fx.sc - 1;
468 append_var_spec (&fx.spec);
481 /* Parses a column specification for parse_specs(). */
483 fixed_parse_compatible (void)
489 type = fx.v[0]->type;
490 for (i = 1; i < fx.nv; i++)
491 if (type != fx.v[i]->type)
493 msg (SE, _("%s is not of the same type as %s. To specify "
494 "variables of different types in the same variable "
495 "list, use a FORTRAN-like format specifier."),
496 fx.v[i]->name, fx.v[0]->name);
500 if (!lex_force_int ())
502 fx.fc = lex_integer () - 1;
505 msg (SE, _("Column positions for fields must be positive."));
510 lex_negative_to_dash ();
513 if (!lex_force_int ())
515 fx.lc = lex_integer () - 1;
518 msg (SE, _("Column positions for fields must be positive."));
521 else if (fx.lc < fx.fc)
523 msg (SE, _("The ending column for a field must not "
524 "be less than the starting column."));
532 fx.spec.u.v.f.w = fx.lc - fx.fc + 1;
535 struct fmt_desc *fdp;
541 fx.spec.u.v.f.type = parse_format_specifier_name (&cp, 0);
542 if (fx.spec.u.v.f.type == -1)
546 msg (SE, _("A format specifier on this line "
547 "has extra characters on the end."));
554 fx.spec.u.v.f.type = FMT_F;
558 if (!lex_force_int ())
560 if (lex_integer () < 1)
562 msg (SE, _("The value for number of decimal places "
563 "must be at least 1."));
566 fx.spec.u.v.f.d = lex_integer ();
572 fdp = &formats[fx.spec.u.v.f.type];
573 if (fdp->n_args < 2 && fx.spec.u.v.f.d)
575 msg (SE, _("Input format %s doesn't accept decimal places."),
579 if (fx.spec.u.v.f.d > 16)
580 fx.spec.u.v.f.d = 16;
582 if (!lex_force_match (')'))
587 fx.spec.u.v.f.type = FMT_F;
593 if ((fx.lc - fx.fc + 1) % fx.nv)
595 msg (SE, _("The %d columns %d-%d can't be evenly divided into %d "
596 "fields."), fx.lc - fx.fc + 1, fx.fc + 1, fx.lc + 1, fx.nv);
600 dividend = (fx.lc - fx.fc + 1) / fx.nv;
601 fx.spec.u.v.f.w = dividend;
602 if (!check_output_specifier (&fx.spec.u.v.f))
604 if ((type == ALPHA) ^ (formats[fx.spec.u.v.f.type].cat & FCAT_STRING))
606 msg (SE, _("%s variables cannot be displayed with format %s."),
607 type == ALPHA ? _("String") : _("Numeric"),
608 fmt_to_string (&fx.spec.u.v.f));
612 /* Check that, for string variables, the user didn't specify a width
613 longer than an actual string width. */
616 /* Minimum width of all the string variables specified. */
617 int min_len = fx.v[0]->width;
619 for (i = 1; i < fx.nv; i++)
620 min_len = min (min_len, fx.v[i]->width);
621 if (!check_string_specifier (&fx.spec.u.v.f, min_len))
625 fx.spec.type = PRT_VAR;
626 for (i = 0; i < fx.nv; i++)
628 fx.spec.fc = fx.fc + dividend * i;
629 fx.spec.u.v.v = fx.v[i];
630 append_var_spec (&fx.spec);
635 /* Destroy a format list and, optionally, all its sublists. */
637 destroy_fmt_list (struct fmt_list * f, int recurse)
639 struct fmt_list *next;
644 if (recurse && f->f.type == FMT_DESCEND)
645 destroy_fmt_list (f->down, 1);
650 /* Recursively puts the format list F (which represents a set of
651 FORTRAN-like format specifications, like 4(F10,2X)) into the
654 dump_fmt_list (struct fmt_list * f)
658 for (; f; f = f->next)
659 if (f->f.type == FMT_X)
661 else if (f->f.type == FMT_T)
663 else if (f->f.type == FMT_NEWREC)
665 fx.recno += f->count;
667 fx.spec.type = PRT_NEWLINE;
668 for (i = 0; i < f->count; i++)
669 append_var_spec (&fx.spec);
672 for (i = 0; i < f->count; i++)
673 if (f->f.type == FMT_DESCEND)
675 if (!dump_fmt_list (f->down))
684 msg (SE, _("The number of format "
685 "specifications exceeds the number of variable "
691 if ((v->type == ALPHA) ^ (formats[f->f.type].cat & FCAT_STRING))
693 msg (SE, _("Display format %s may not be used with a "
694 "%s variable."), fmt_to_string (&f->f),
695 v->type == ALPHA ? _("string") : _("numeric"));
698 if (!check_string_specifier (&f->f, v->width))
701 fx.spec.type = PRT_VAR;
703 fx.spec.u.v.f = f->f;
704 fx.spec.fc = fx.sc - 1;
705 append_var_spec (&fx.spec);
712 /* Recursively parses a list of FORTRAN-like format specifiers. Calls
713 itself to parse nested levels of parentheses. Returns to its
714 original caller NULL, to indicate error, non-NULL, but nothing
715 useful, to indicate success (it returns a free()'d block). */
716 static struct fmt_list *
717 fixed_parse_fortran (void)
719 struct fmt_list *head = NULL;
720 struct fmt_list *fl = NULL;
722 lex_get (); /* skip opening parenthesis */
726 fl = fl->next = xmalloc (sizeof *fl);
728 head = fl = xmalloc (sizeof *fl);
732 if (!lex_integer_p ())
734 fl->count = lex_integer ();
742 fl->f.type = FMT_DESCEND;
744 fl->down = fixed_parse_fortran ();
749 else if (lex_match ('/'))
750 fl->f.type = FMT_NEWREC;
751 else if (!parse_format_specifier (&fl->f, 1)
752 || !check_output_specifier (&fl->f))
764 dump_fmt_list (head);
765 destroy_fmt_list (head, 1);
768 msg (SE, _("There aren't enough format specifications "
769 "to match the number of variable names given."));
776 destroy_fmt_list (head, 0);
781 /* Prints the table produced by the TABLE subcommand to the listing
784 dump_table (const struct file_handle *fh)
786 struct prt_out_spec *spec;
791 for (nspec = 0, spec = prt.spec; spec; spec = spec->next)
792 if (spec->type == PRT_CONST || spec->type == PRT_VAR)
794 t = tab_create (4, nspec + 1, 0);
795 tab_columns (t, TAB_COL_DOWN, 1);
796 tab_box (t, TAL_1, TAL_1, TAL_0, TAL_1, 0, 0, 3, nspec);
797 tab_hline (t, TAL_2, 0, 3, 1);
798 tab_headers (t, 0, 0, 1, 0);
799 tab_text (t, 0, 0, TAB_CENTER | TAT_TITLE, _("Variable"));
800 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Record"));
801 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Columns"));
802 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Format"));
803 tab_dim (t, tab_natural_dimensions);
804 for (nspec = recno = 0, spec = prt.spec; spec; spec = spec->next)
812 int len = strlen (spec->u.c);
814 tab_text (t, 0, nspec, TAB_LEFT | TAT_FIX | TAT_PRINTF,
815 "\"%s\"", spec->u.c);
816 tab_text (t, 1, nspec, TAT_PRINTF, "%d", recno + 1);
817 tab_text (t, 2, nspec, TAT_PRINTF, "%3d-%3d",
818 spec->fc + 1, spec->fc + len);
819 tab_text (t, 3, nspec, TAB_LEFT | TAT_FIX | TAT_PRINTF,
826 tab_text (t, 0, nspec, TAB_LEFT, spec->u.v.v->name);
827 tab_text (t, 1, nspec, TAT_PRINTF, "%d", recno + 1);
828 tab_text (t, 2, nspec, TAT_PRINTF, "%3d-%3d",
829 spec->fc + 1, spec->fc + spec->u.v.f.w);
830 tab_text (t, 3, nspec, TAB_LEFT | TAT_FIX,
831 fmt_to_string (&spec->u.v.f));
841 tab_title (t, 1, _("Writing %d record(s) to file %s."),
842 recno, handle_get_filename (fh));
844 tab_title (t, 1, _("Writing %d record(s) to the listing file."), recno);
848 /* PORTME: The number of characters in a line terminator. */
850 #define LINE_END_WIDTH 2 /* \r\n */
852 #define LINE_END_WIDTH 1 /* \n */
855 /* Calculates the maximum possible line width and allocates a buffer
856 big enough to contain it */
860 /* Cumulative maximum line width (excluding null terminator) so far. */
863 /* Width required by current this prt_out_spec. */
864 int pot_w; /* Potential w. */
867 struct prt_out_spec *i;
869 for (i = prt.spec; i; i = i->next)
877 pot_w = i->fc + strlen (i->u.c);
880 pot_w = i->fc + i->u.v.f.w;
893 prt.max_width = w + LINE_END_WIDTH + 1;
894 prt.line = xmalloc (prt.max_width);
897 /* Transformation. */
899 /* Performs the transformation inside print_trns T on case C. */
901 print_trns_proc (struct trns_header * trns, struct ccase * c,
904 /* Transformation. */
905 struct print_trns *t = (struct print_trns *) trns;
908 struct prt_out_spec *i;
913 /* Length of the line in buf. */
915 memset (buf, ' ', t->max_width);
917 if (t->options & PRT_EJECT)
920 /* Note that a field written to a place where a field has
921 already been written truncates the record. `PRINT /A B
922 (T10,F8,T1,F8).' only outputs B. */
923 for (i = t->spec; i; i = i->next)
927 if (t->writer == NULL)
930 tab_output_text (TAT_FIX | TAT_NOWRAP, buf);
934 if ((t->options & PRT_CMD_MASK) == PRT_PRINT
935 || !(t->options & PRT_BINARY))
937 /* PORTME: Line ends. */
944 dfm_put_record (t->writer, buf, len);
947 memset (buf, ' ', t->max_width);
952 /* FIXME: Should be revised to keep track of the string's
953 length outside the loop, probably in i->u.c[0]. */
954 memcpy (&buf[i->fc], i->u.c, strlen (i->u.c));
955 len = i->fc + strlen (i->u.c);
959 data_out (&buf[i->fc], &i->u.v.f, case_data (c, i->u.v.v->fv));
960 len = i->fc + i->u.v.f.w;
964 /* PRT_SPACE always immediately follows PRT_VAR. */
976 /* Frees all the data inside print_trns T. Does not free T. */
978 print_trns_free (struct trns_header * t)
980 struct prt_out_spec *i, *n;
982 for (i = ((struct print_trns *) t)->spec; i; i = n)
1001 free (((struct print_trns *) t)->line);
1006 /* PRINT SPACE transformation. */
1007 struct print_space_trns
1009 struct trns_header h;
1011 struct dfm_writer *writer; /* Output data file. */
1012 struct expression *e; /* Number of lines; NULL=1. */
1016 static trns_proc_func print_space_trns_proc;
1017 static trns_free_func print_space_trns_free;
1020 cmd_print_space (void)
1022 struct print_space_trns *t;
1023 struct file_handle *fh;
1024 struct expression *e;
1025 struct dfm_writer *writer;
1027 if (lex_match_id ("OUTFILE"))
1041 e = expr_parse (EXPR_NUMERIC);
1045 lex_error (_("expecting end of command"));
1054 writer = dfm_open_writer (fh);
1064 t = xmalloc (sizeof *t);
1065 t->h.proc = print_space_trns_proc;
1067 t->h.free = print_space_trns_free;
1073 add_transformation ((struct trns_header *) t);
1078 print_space_trns_proc (struct trns_header * trns, struct ccase * c,
1079 int case_num UNUSED)
1081 struct print_space_trns *t = (struct print_space_trns *) trns;
1088 expr_evaluate (t->e, c, case_num, &v);
1092 msg (SW, _("The expression on PRINT SPACE evaluated to %d. It's "
1093 "not possible to PRINT SPACE a negative number of "
1102 if (t->writer == NULL)
1107 char buf[LINE_END_WIDTH];
1109 /* PORTME: Line ends. */
1117 dfm_put_record (t->writer, buf, LINE_END_WIDTH);
1124 print_space_trns_free (struct trns_header * trns)
1126 expr_free (((struct print_space_trns *) trns)->e);