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. */
30 #include "file-handle.h"
37 /* Describes what to do when an output field is encountered. */
40 PRT_ERROR, /* Invalid value. */
41 PRT_NEWLINE, /* Newline. */
42 PRT_CONST, /* Constant string. */
43 PRT_VAR, /* Variable. */
44 PRT_SPACE /* A single space. */
47 /* Describes how to output one field. */
50 struct prt_out_spec *next;
51 int type; /* PRT_* constant. */
52 int fc; /* 0-based first column. */
55 char *c; /* PRT_CONST: Associated string. */
58 struct variable *v; /* PRT_VAR: Associated variable. */
59 struct fmt_spec f; /* PRT_VAR: Output spec. */
66 /* Enums for use with print_trns's `options' field. */
69 PRT_CMD_MASK = 1, /* Command type mask. */
70 PRT_PRINT = 0, /* PRINT transformation identifier. */
71 PRT_WRITE = 1, /* WRITE transformation identifier. */
72 PRT_EJECT = 002 /* Can be combined with CMD_PRINT only. */
75 /* PRINT, PRINT EJECT, WRITE private data structure. */
79 struct file_handle *handle; /* Output file, NULL=listing file. */
80 int options; /* PRT_* bitmapped field. */
81 struct prt_out_spec *spec; /* Output specifications. */
82 int max_width; /* Maximum line width including null. */
83 char *line; /* Buffer for sticking lines in. */
86 /* PRT_PRINT or PRT_WRITE. */
89 /* Holds information on parsing the data file. */
90 static struct print_trns prt;
92 /* Last prt_out_spec in the chain. Used for building the linked-list. */
93 static struct prt_out_spec *next;
95 /* Number of records. */
98 static int internal_cmd_print (int flags);
99 static trns_proc_func print_trns_proc;
100 static trns_free_func print_trns_free;
101 static int parse_specs (void);
102 static void dump_table (void);
103 static void append_var_spec (struct prt_out_spec *spec);
104 static void alloc_line (void);
108 /* Parses PRINT command. */
112 return internal_cmd_print (PRT_PRINT);
115 /* Parses PRINT EJECT command. */
117 cmd_print_eject (void)
119 return internal_cmd_print (PRT_PRINT | PRT_EJECT);
122 /* Parses WRITE command. */
126 return internal_cmd_print (PRT_WRITE);
129 /* Parses the output commands. F is PRT_PRINT, PRT_WRITE, or
130 PRT_PRINT|PRT_EJECT. */
132 internal_cmd_print (int f)
134 /* 0=print no table, 1=print table. (TABLE subcommand.) */
137 /* malloc()'d transformation. */
138 struct print_trns *trns;
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"))
159 prt.handle = fh_parse_file_handle ();
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. */
188 if (prt.handle != NULL && !dfm_open_for_writing (prt.handle))
191 /* Output the variable table if requested. */
195 /* Count the maximum line width. Allocate linebuffer if
199 /* Put the transformation in the queue. */
200 trns = xmalloc (sizeof *trns);
201 memcpy (trns, &prt, sizeof *trns);
202 add_transformation ((struct trns_header *) trns);
207 print_trns_free ((struct trns_header *) & prt);
211 /* Appends the field output specification SPEC to the list maintained
214 append_var_spec (struct prt_out_spec *spec)
217 prt.spec = next = xmalloc (sizeof *spec);
219 next = next->next = xmalloc (sizeof *spec);
221 memcpy (next, spec, sizeof *spec);
225 /* Field parsing. Mostly stolen from data-list.c. */
227 /* Used for chaining together fortran-like format specifiers. */
230 struct fmt_list *next;
233 struct fmt_list *down;
236 /* Used as "local" variables among the fixed-format parsing funcs. If
237 it were guaranteed that PSPP were going to be compiled by gcc,
238 I'd make all these functions a single set of nested functions. */
241 struct variable **v; /* variable list */
242 int nv; /* number of variables in list */
243 int cv; /* number of variables from list used up so far
244 by the FORTRAN-like format specifiers */
246 int recno; /* current 1-based record number */
247 int sc; /* 1-based starting column for next variable */
249 struct prt_out_spec spec; /* next format spec to append to list */
250 int fc, lc; /* first, last 1-based column number of current
253 int level; /* recursion level for FORTRAN-like format
258 static int fixed_parse_compatible (void);
259 static struct fmt_list *fixed_parse_fortran (void);
261 static int parse_string_argument (void);
262 static int parse_variable_argument (void);
264 /* Parses all the variable and string specifications on a single
265 PRINT, PRINT EJECT, or WRITE command into the prt structure.
270 /* Return code from called function. */
278 while (lex_match ('/'))
280 int prev_recno = fx.recno;
285 if (!lex_force_int ())
287 if (lex_integer () < fx.recno)
289 msg (SE, _("The record number specified, %ld, is "
290 "before the previous record, %d. Data "
291 "fields must be listed in order of "
292 "increasing record number."),
293 lex_integer (), fx.recno - 1);
296 fx.recno = lex_integer ();
300 fx.spec.type = PRT_NEWLINE;
301 while (prev_recno++ < fx.recno)
302 append_var_spec (&fx.spec);
307 if (token == T_STRING)
308 code = parse_string_argument ();
310 code = parse_variable_argument ();
314 fx.spec.type = PRT_NEWLINE;
315 append_var_spec (&fx.spec);
319 else if (fx.recno > nrec)
321 msg (SE, _("Variables are specified on records that "
322 "should not exist according to RECORDS subcommand."));
328 lex_error (_("expecting end of command"));
335 /* Parses a string argument to the PRINT commands. Returns success. */
337 parse_string_argument (void)
339 fx.spec.type = PRT_CONST;
340 fx.spec.fc = fx.sc - 1;
341 fx.spec.u.c = xstrdup (ds_value (&tokstr));
344 /* Parse the included column range. */
347 /* Width of column range in characters. */
350 /* Width of constant string in characters. */
353 /* 1-based index of last column in range. */
356 if (!lex_integer_p () || lex_integer () <= 0)
358 msg (SE, _("%g is not a valid column location."), tokval);
361 fx.spec.fc = lex_integer () - 1;
364 lex_negative_to_dash ();
367 if (!lex_integer_p ())
369 msg (SE, _("Column location expected following `%d-'."),
373 if (lex_integer () <= 0)
375 msg (SE, _("%g is not a valid column location."), tokval);
378 if (lex_integer () < fx.spec.fc + 1)
380 msg (SE, _("%d-%ld is not a valid column range. The second "
381 "column must be greater than or equal to the first."),
382 fx.spec.fc + 1, lex_integer ());
385 lc = lex_integer () - 1;
390 /* If only a starting location is specified then the field is
391 the width of the provided string. */
392 lc = fx.spec.fc + strlen (fx.spec.u.c) - 1;
394 /* Apply the range. */
395 c_len = lc - fx.spec.fc + 1;
396 s_len = strlen (fx.spec.u.c);
398 fx.spec.u.c[c_len] = 0;
399 else if (s_len < c_len)
401 fx.spec.u.c = xrealloc (fx.spec.u.c, c_len + 1);
402 memset (&fx.spec.u.c[s_len], ' ', c_len - s_len);
403 fx.spec.u.c[c_len] = 0;
409 /* If nothing is provided then the field is the width of the
411 fx.sc += strlen (fx.spec.u.c);
413 append_var_spec (&fx.spec);
421 /* Parses a variable argument to the PRINT commands by passing it off
422 to fixed_parse_compatible() or fixed_parse_fortran() as appropriate.
425 parse_variable_argument (void)
427 if (!parse_variables (default_dict, &fx.v, &fx.nv, PV_DUPLICATE))
432 if (!fixed_parse_compatible ())
435 else if (token == '(')
439 if (!fixed_parse_fortran ())
444 /* User wants dictionary format specifiers. */
448 for (i = 0; i < fx.nv; i++)
451 fx.spec.type = PRT_VAR;
452 fx.spec.fc = fx.sc - 1;
453 fx.spec.u.v.v = fx.v[i];
454 fx.spec.u.v.f = fx.v[i]->print;
455 append_var_spec (&fx.spec);
456 fx.sc += fx.v[i]->print.w;
459 fx.spec.type = PRT_SPACE;
460 fx.spec.fc = fx.sc - 1;
461 append_var_spec (&fx.spec);
474 /* Parses a column specification for parse_specs(). */
476 fixed_parse_compatible (void)
482 type = fx.v[0]->type;
483 for (i = 1; i < fx.nv; i++)
484 if (type != fx.v[i]->type)
486 msg (SE, _("%s is not of the same type as %s. To specify "
487 "variables of different types in the same variable "
488 "list, use a FORTRAN-like format specifier."),
489 fx.v[i]->name, fx.v[0]->name);
493 if (!lex_force_int ())
495 fx.fc = lex_integer () - 1;
498 msg (SE, _("Column positions for fields must be positive."));
503 lex_negative_to_dash ();
506 if (!lex_force_int ())
508 fx.lc = lex_integer () - 1;
511 msg (SE, _("Column positions for fields must be positive."));
514 else if (fx.lc < fx.fc)
516 msg (SE, _("The ending column for a field must not "
517 "be less than the starting column."));
525 fx.spec.u.v.f.w = fx.lc - fx.fc + 1;
528 struct fmt_desc *fdp;
534 fx.spec.u.v.f.type = parse_format_specifier_name (&cp, 0);
535 if (fx.spec.u.v.f.type == -1)
539 msg (SE, _("A format specifier on this line "
540 "has extra characters on the end."));
547 fx.spec.u.v.f.type = FMT_F;
551 if (!lex_force_int ())
553 if (lex_integer () < 1)
555 msg (SE, _("The value for number of decimal places "
556 "must be at least 1."));
559 fx.spec.u.v.f.d = lex_integer ();
565 fdp = &formats[fx.spec.u.v.f.type];
566 if (fdp->n_args < 2 && fx.spec.u.v.f.d)
568 msg (SE, _("Input format %s doesn't accept decimal places."),
572 if (fx.spec.u.v.f.d > 16)
573 fx.spec.u.v.f.d = 16;
575 if (!lex_force_match (')'))
580 fx.spec.u.v.f.type = FMT_F;
586 if ((fx.lc - fx.fc + 1) % fx.nv)
588 msg (SE, _("The %d columns %d-%d can't be evenly divided into %d "
589 "fields."), fx.lc - fx.fc + 1, fx.fc + 1, fx.lc + 1, fx.nv);
593 dividend = (fx.lc - fx.fc + 1) / fx.nv;
594 fx.spec.u.v.f.w = dividend;
595 if (!check_output_specifier (&fx.spec.u.v.f))
597 if ((type == ALPHA) ^ (formats[fx.spec.u.v.f.type].cat & FCAT_STRING))
599 msg (SE, _("%s variables cannot be displayed with format %s."),
600 type == ALPHA ? _("String") : _("Numeric"),
601 fmt_to_string (&fx.spec.u.v.f));
605 /* Check that, for string variables, the user didn't specify a width
606 longer than an actual string width. */
609 /* Minimum width of all the string variables specified. */
610 int min_len = fx.v[0]->width;
612 for (i = 1; i < fx.nv; i++)
613 min_len = min (min_len, fx.v[i]->width);
614 if (!check_string_specifier (&fx.spec.u.v.f, min_len))
618 fx.spec.type = PRT_VAR;
619 for (i = 0; i < fx.nv; i++)
621 fx.spec.fc = fx.fc + dividend * i;
622 fx.spec.u.v.v = fx.v[i];
623 append_var_spec (&fx.spec);
628 /* Destroy a format list and, optionally, all its sublists. */
630 destroy_fmt_list (struct fmt_list * f, int recurse)
632 struct fmt_list *next;
637 if (recurse && f->f.type == FMT_DESCEND)
638 destroy_fmt_list (f->down, 1);
643 /* Recursively puts the format list F (which represents a set of
644 FORTRAN-like format specifications, like 4(F10,2X)) into the
647 dump_fmt_list (struct fmt_list * f)
651 for (; f; f = f->next)
652 if (f->f.type == FMT_X)
654 else if (f->f.type == FMT_T)
656 else if (f->f.type == FMT_NEWREC)
658 fx.recno += f->count;
660 fx.spec.type = PRT_NEWLINE;
661 for (i = 0; i < f->count; i++)
662 append_var_spec (&fx.spec);
665 for (i = 0; i < f->count; i++)
666 if (f->f.type == FMT_DESCEND)
668 if (!dump_fmt_list (f->down))
677 msg (SE, _("The number of format "
678 "specifications exceeds the number of variable "
684 if ((v->type == ALPHA) ^ (formats[f->f.type].cat & FCAT_STRING))
686 msg (SE, _("Display format %s may not be used with a "
687 "%s variable."), fmt_to_string (&f->f),
688 v->type == ALPHA ? _("string") : _("numeric"));
691 if (!check_string_specifier (&f->f, v->width))
694 fx.spec.type = PRT_VAR;
696 fx.spec.u.v.f = f->f;
697 fx.spec.fc = fx.sc - 1;
698 append_var_spec (&fx.spec);
705 /* Recursively parses a list of FORTRAN-like format specifiers. Calls
706 itself to parse nested levels of parentheses. Returns to its
707 original caller NULL, to indicate error, non-NULL, but nothing
708 useful, to indicate success (it returns a free()'d block). */
709 static struct fmt_list *
710 fixed_parse_fortran (void)
712 struct fmt_list *head = NULL;
713 struct fmt_list *fl = NULL;
715 lex_get (); /* skip opening parenthesis */
719 fl = fl->next = xmalloc (sizeof *fl);
721 head = fl = xmalloc (sizeof *fl);
725 if (!lex_integer_p ())
727 fl->count = lex_integer ();
735 fl->f.type = FMT_DESCEND;
737 fl->down = fixed_parse_fortran ();
742 else if (lex_match ('/'))
743 fl->f.type = FMT_NEWREC;
744 else if (!parse_format_specifier (&fl->f, 1)
745 || !check_output_specifier (&fl->f))
757 dump_fmt_list (head);
758 destroy_fmt_list (head, 1);
761 msg (SE, _("There aren't enough format specifications "
762 "to match the number of variable names given."));
769 destroy_fmt_list (head, 0);
774 /* Prints the table produced by the TABLE subcommand to the listing
779 struct prt_out_spec *spec;
784 for (nspec = 0, spec = prt.spec; spec; spec = spec->next)
785 if (spec->type == PRT_CONST || spec->type == PRT_VAR)
787 t = tab_create (4, nspec + 1, 0);
788 tab_columns (t, TAB_COL_DOWN, 1);
789 tab_box (t, TAL_1, TAL_1, TAL_0, TAL_1, 0, 0, 3, nspec);
790 tab_hline (t, TAL_2, 0, 3, 1);
791 tab_headers (t, 0, 0, 1, 0);
792 tab_text (t, 0, 0, TAB_CENTER | TAT_TITLE, _("Variable"));
793 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Record"));
794 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Columns"));
795 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Format"));
796 tab_dim (t, tab_natural_dimensions);
797 for (nspec = recno = 0, spec = prt.spec; spec; spec = spec->next)
805 int len = strlen (spec->u.c);
807 tab_text (t, 0, nspec, TAB_LEFT | TAT_FIX | TAT_PRINTF,
808 "\"%s\"", spec->u.c);
809 tab_text (t, 1, nspec, TAT_PRINTF, "%d", recno + 1);
810 tab_text (t, 2, nspec, TAT_PRINTF, "%3d-%3d",
811 spec->fc + 1, spec->fc + len);
812 tab_text (t, 3, nspec, TAB_LEFT | TAT_FIX | TAT_PRINTF,
819 tab_text (t, 0, nspec, TAB_LEFT, spec->u.v.v->name);
820 tab_text (t, 1, nspec, TAT_PRINTF, "%d", recno + 1);
821 tab_text (t, 2, nspec, TAT_PRINTF, "%3d-%3d",
822 spec->fc + 1, spec->fc + spec->u.v.f.w);
823 tab_text (t, 3, nspec, TAB_LEFT | TAT_FIX,
824 fmt_to_string (&spec->u.v.f));
833 if (prt.handle != NULL)
834 tab_title (t, 1, _("Writing %d record(s) to file %s."),
835 recno, handle_get_filename (prt.handle));
837 tab_title (t, 1, _("Writing %d record(s) to the listing file."), recno);
841 /* PORTME: The number of characters in a line terminator. */
843 #define LINE_END_WIDTH 2 /* \r\n */
845 #define LINE_END_WIDTH 1 /* \n */
848 /* Calculates the maximum possible line width and allocates a buffer
849 big enough to contain it */
853 /* Cumulative maximum line width (excluding null terminator) so far. */
856 /* Width required by current this prt_out_spec. */
857 int pot_w; /* Potential w. */
860 struct prt_out_spec *i;
862 for (i = prt.spec; i; i = i->next)
870 pot_w = i->fc + strlen (i->u.c);
873 pot_w = i->fc + i->u.v.f.w;
886 prt.max_width = w + LINE_END_WIDTH + 1;
887 prt.line = xmalloc (prt.max_width);
890 /* Transformation. */
892 /* Performs the transformation inside print_trns T on case C. */
894 print_trns_proc (struct trns_header * trns, struct ccase * c,
897 /* Transformation. */
898 struct print_trns *t = (struct print_trns *) trns;
901 struct prt_out_spec *i;
906 /* Length of the line in buf. */
908 memset (buf, ' ', t->max_width);
910 if (t->options & PRT_EJECT)
913 /* Note that a field written to a place where a field has already
914 been written truncates the record. `PRINT /A B (T10,F8,T1,F8).'
915 only outputs B. This is an example of bug-for-bug compatibility,
916 in the author's opinion. */
917 for (i = t->spec; i; i = i->next)
921 if (t->handle == NULL)
924 tab_output_text (TAT_FIX | TAT_NOWRAP, buf);
928 if ((t->options & PRT_CMD_MASK) == PRT_PRINT
929 || handle_get_mode (t->handle) != MODE_BINARY)
931 /* PORTME: Line ends. */
938 dfm_put_record (t->handle, buf, len);
941 memset (buf, ' ', t->max_width);
946 /* FIXME: Should be revised to keep track of the string's
947 length outside the loop, probably in i->u.c[0]. */
948 memcpy (&buf[i->fc], i->u.c, strlen (i->u.c));
949 len = i->fc + strlen (i->u.c);
953 data_out (&buf[i->fc], &i->u.v.f, &c->data[i->u.v.v->fv]);
954 len = i->fc + i->u.v.f.w;
958 /* PRT_SPACE always immediately follows PRT_VAR. */
970 /* Frees all the data inside print_trns T. Does not free T. */
972 print_trns_free (struct trns_header * t)
974 struct prt_out_spec *i, *n;
976 for (i = ((struct print_trns *) t)->spec; i; i = n)
995 free (((struct print_trns *) t)->line);
1000 /* PRINT SPACE transformation. */
1001 struct print_space_trns
1003 struct trns_header h;
1005 struct file_handle *handle; /* Output file, NULL=listing file. */
1006 struct expression *e; /* Number of lines; NULL=1. */
1010 static trns_proc_func print_space_trns_proc;
1011 static trns_free_func print_space_trns_free;
1014 cmd_print_space (void)
1016 struct print_space_trns *t;
1017 struct file_handle *handle;
1018 struct expression *e;
1020 if (lex_match_id ("OUTFILE"))
1024 handle = fh_parse_file_handle ();
1034 e = expr_parse (EXPR_NUMERIC);
1038 lex_error (_("expecting end of command"));
1045 if (handle != NULL && !dfm_open_for_writing (handle))
1051 t = xmalloc (sizeof *t);
1052 t->h.proc = print_space_trns_proc;
1054 t->h.free = print_space_trns_free;
1060 add_transformation ((struct trns_header *) t);
1065 print_space_trns_proc (struct trns_header * trns, struct ccase * c,
1066 int case_num UNUSED)
1068 struct print_space_trns *t = (struct print_space_trns *) trns;
1075 expr_evaluate (t->e, c, case_num, &v);
1079 msg (SW, _("The expression on PRINT SPACE evaluated to %d. It's "
1080 "not possible to PRINT SPACE a negative number of "
1089 if (t->handle == NULL)
1094 char buf[LINE_END_WIDTH];
1096 /* PORTME: Line ends. */
1104 dfm_put_record (t->handle, buf, LINE_END_WIDTH);
1111 print_space_trns_free (struct trns_header * trns)
1113 expr_free (((struct print_space_trns *) trns)->e);