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., 51 Franklin Street, Fifth Floor, Boston, MA
20 /* FIXME: seems like a lot of code duplication with data-list.c. */
28 #include "dfm-write.h"
30 #include "expressions/public.h"
31 #include "file-handle.h"
39 #define _(msgid) gettext (msgid)
41 /* Describes what to do when an output field is encountered. */
44 PRT_ERROR, /* Invalid value. */
45 PRT_NEWLINE, /* Newline. */
46 PRT_CONST, /* Constant string. */
47 PRT_VAR, /* Variable. */
48 PRT_SPACE /* A single space. */
51 /* Describes how to output one field. */
54 struct prt_out_spec *next;
55 int type; /* PRT_* constant. */
56 int fc; /* 0-based first column. */
59 char *c; /* PRT_CONST: Associated string. */
62 struct variable *v; /* PRT_VAR: Associated variable. */
63 struct fmt_spec f; /* PRT_VAR: Output spec. */
70 /* Enums for use with print_trns's `options' field. */
73 PRT_CMD_MASK = 1, /* Command type mask. */
74 PRT_PRINT = 0, /* PRINT transformation identifier. */
75 PRT_WRITE = 1, /* WRITE transformation identifier. */
76 PRT_EJECT = 002, /* Can be combined with CMD_PRINT only. */
77 PRT_BINARY = 004 /* File is binary, omit newlines. */
80 /* PRINT, PRINT EJECT, WRITE private data structure. */
83 struct dfm_writer *writer; /* Output file, NULL=listing file. */
84 int options; /* PRT_* bitmapped field. */
85 struct prt_out_spec *spec; /* Output specifications. */
86 int max_width; /* Maximum line width including null. */
87 char *line; /* Buffer for sticking lines in. */
90 /* PRT_PRINT or PRT_WRITE. */
93 /* Holds information on parsing the data file. */
94 static struct print_trns prt;
96 /* Last prt_out_spec in the chain. Used for building the linked-list. */
97 static struct prt_out_spec *next;
99 /* Number of records. */
102 static int internal_cmd_print (int flags);
103 static trns_proc_func print_trns_proc;
104 static trns_free_func print_trns_free;
105 static int parse_specs (void);
106 static void dump_table (const struct file_handle *);
107 static void append_var_spec (struct prt_out_spec *);
108 static void alloc_line (void);
112 /* Parses PRINT command. */
116 return internal_cmd_print (PRT_PRINT);
119 /* Parses PRINT EJECT command. */
121 cmd_print_eject (void)
123 return internal_cmd_print (PRT_PRINT | PRT_EJECT);
126 /* Parses WRITE command. */
130 return internal_cmd_print (PRT_WRITE);
133 /* Parses the output commands. F is PRT_PRINT, PRT_WRITE, or
134 PRT_PRINT|PRT_EJECT. */
136 internal_cmd_print (int f)
138 int table = 0; /* Print table? */
139 struct print_trns *trns; /* malloc()'d transformation. */
140 struct file_handle *fh = NULL;
142 /* Fill in prt to facilitate error-handling. */
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 (print_trns_proc, print_trns_free, trns);
214 print_trns_free (&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 size_t nv; /* number of variables in list */
250 size_t 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;
290 if (lex_is_number ())
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. */
352 if (lex_is_number ())
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_is_integer () || 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_is_integer ())
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))
437 if (lex_is_number ())
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 /* Verifies that FORMAT doesn't need a variable wider than WIDTH.
482 Returns true iff that is the case. */
484 check_string_width (const struct fmt_spec *format, const struct variable *v)
486 if (get_format_var_width (format) > v->width)
488 msg (SE, _("Variable %s has width %d so it cannot be output "
490 v->name, v->width, fmt_to_string (format));
496 /* Parses a column specification for parse_specs(). */
498 fixed_parse_compatible (void)
500 int individual_var_width;
504 type = fx.v[0]->type;
505 for (i = 1; i < fx.nv; i++)
506 if (type != fx.v[i]->type)
508 msg (SE, _("%s is not of the same type as %s. To specify "
509 "variables of different types in the same variable "
510 "list, use a FORTRAN-like format specifier."),
511 fx.v[i]->name, fx.v[0]->name);
515 if (!lex_force_int ())
517 fx.fc = lex_integer () - 1;
520 msg (SE, _("Column positions for fields must be positive."));
525 lex_negative_to_dash ();
528 if (!lex_force_int ())
530 fx.lc = lex_integer () - 1;
533 msg (SE, _("Column positions for fields must be positive."));
536 else if (fx.lc < fx.fc)
538 msg (SE, _("The ending column for a field must not "
539 "be less than the starting column."));
547 fx.spec.u.v.f.w = fx.lc - fx.fc + 1;
550 struct fmt_desc *fdp;
556 fx.spec.u.v.f.type = parse_format_specifier_name (&cp, 0);
557 if (fx.spec.u.v.f.type == -1)
561 msg (SE, _("A format specifier on this line "
562 "has extra characters on the end."));
569 fx.spec.u.v.f.type = FMT_F;
571 if (lex_is_number ())
573 if (!lex_force_int ())
575 if (lex_integer () < 1)
577 msg (SE, _("The value for number of decimal places "
578 "must be at least 1."));
581 fx.spec.u.v.f.d = lex_integer ();
587 fdp = &formats[fx.spec.u.v.f.type];
588 if (fdp->n_args < 2 && fx.spec.u.v.f.d)
590 msg (SE, _("Input format %s doesn't accept decimal places."),
594 if (fx.spec.u.v.f.d > 16)
595 fx.spec.u.v.f.d = 16;
597 if (!lex_force_match (')'))
602 fx.spec.u.v.f.type = FMT_F;
608 if ((fx.lc - fx.fc + 1) % fx.nv)
610 msg (SE, _("The %d columns %d-%d can't be evenly divided into %u "
612 fx.lc - fx.fc + 1, fx.fc + 1, fx.lc + 1, (unsigned) fx.nv);
616 individual_var_width = (fx.lc - fx.fc + 1) / fx.nv;
617 fx.spec.u.v.f.w = individual_var_width;
618 if (!check_output_specifier (&fx.spec.u.v.f, true)
619 || !check_specifier_type (&fx.spec.u.v.f, type, true))
623 for (i = 0; i < fx.nv; i++)
624 if (!check_string_width (&fx.spec.u.v.f, fx.v[i]))
628 fx.spec.type = PRT_VAR;
629 for (i = 0; i < fx.nv; i++)
631 fx.spec.fc = fx.fc + individual_var_width * i;
632 fx.spec.u.v.v = fx.v[i];
633 append_var_spec (&fx.spec);
638 /* Destroy a format list and, optionally, all its sublists. */
640 destroy_fmt_list (struct fmt_list *f, int recurse)
642 struct fmt_list *next;
647 if (recurse && f->f.type == FMT_DESCEND)
648 destroy_fmt_list (f->down, 1);
653 /* Recursively puts the format list F (which represents a set of
654 FORTRAN-like format specifications, like 4(F10,2X)) into the
657 dump_fmt_list (struct fmt_list *f)
661 for (; f; f = f->next)
662 if (f->f.type == FMT_X)
664 else if (f->f.type == FMT_T)
666 else if (f->f.type == FMT_NEWREC)
668 fx.recno += f->count;
670 fx.spec.type = PRT_NEWLINE;
671 for (i = 0; i < f->count; i++)
672 append_var_spec (&fx.spec);
675 for (i = 0; i < f->count; i++)
676 if (f->f.type == FMT_DESCEND)
678 if (!dump_fmt_list (f->down))
687 msg (SE, _("The number of format "
688 "specifications exceeds the number of variable "
694 if (!check_output_specifier (&f->f, true)
695 || !check_specifier_type (&f->f, v->type, true)
696 || !check_string_width (&f->f, v))
699 fx.spec.type = PRT_VAR;
701 fx.spec.u.v.f = f->f;
702 fx.spec.fc = fx.sc - 1;
703 append_var_spec (&fx.spec);
710 /* Recursively parses a list of FORTRAN-like format specifiers. Calls
711 itself to parse nested levels of parentheses. Returns to its
712 original caller NULL, to indicate error, non-NULL, but nothing
713 useful, to indicate success (it returns a free()'d block). */
714 static struct fmt_list *
715 fixed_parse_fortran (void)
717 struct fmt_list *head = NULL;
718 struct fmt_list *fl = NULL;
720 lex_get (); /* skip opening parenthesis */
724 fl = fl->next = xmalloc (sizeof *fl);
726 head = fl = xmalloc (sizeof *fl);
728 if (lex_is_number ())
730 if (!lex_is_integer ())
732 fl->count = lex_integer ();
740 fl->f.type = FMT_DESCEND;
742 fl->down = fixed_parse_fortran ();
747 else if (lex_match ('/'))
748 fl->f.type = FMT_NEWREC;
749 else if (!parse_format_specifier (&fl->f, FMTP_ALLOW_XT)
750 || !check_output_specifier (&fl->f, 1))
762 dump_fmt_list (head);
763 destroy_fmt_list (head, 1);
766 msg (SE, _("There aren't enough format specifications "
767 "to match the number of variable names given."));
774 destroy_fmt_list (head, 0);
779 /* Prints the table produced by the TABLE subcommand to the listing
782 dump_table (const struct file_handle *fh)
784 struct prt_out_spec *spec;
789 for (nspec = 0, spec = prt.spec; spec; spec = spec->next)
790 if (spec->type == PRT_CONST || spec->type == PRT_VAR)
792 t = tab_create (4, nspec + 1, 0);
793 tab_columns (t, TAB_COL_DOWN, 1);
794 tab_box (t, TAL_1, TAL_1, TAL_0, TAL_1, 0, 0, 3, nspec);
795 tab_hline (t, TAL_2, 0, 3, 1);
796 tab_headers (t, 0, 0, 1, 0);
797 tab_text (t, 0, 0, TAB_CENTER | TAT_TITLE, _("Variable"));
798 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Record"));
799 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Columns"));
800 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Format"));
801 tab_dim (t, tab_natural_dimensions);
802 for (nspec = recno = 0, spec = prt.spec; spec; spec = spec->next)
810 int len = strlen (spec->u.c);
812 tab_text (t, 0, nspec, TAB_LEFT | TAT_FIX | TAT_PRINTF,
813 "\"%s\"", spec->u.c);
814 tab_text (t, 1, nspec, TAT_PRINTF, "%d", recno + 1);
815 tab_text (t, 2, nspec, TAT_PRINTF, "%3d-%3d",
816 spec->fc + 1, spec->fc + len);
817 tab_text (t, 3, nspec, TAB_LEFT | TAT_FIX | TAT_PRINTF,
824 tab_text (t, 0, nspec, TAB_LEFT, spec->u.v.v->name);
825 tab_text (t, 1, nspec, TAT_PRINTF, "%d", recno + 1);
826 tab_text (t, 2, nspec, TAT_PRINTF, "%3d-%3d",
827 spec->fc + 1, spec->fc + spec->u.v.f.w);
828 tab_text (t, 3, nspec, TAB_LEFT | TAT_FIX,
829 fmt_to_string (&spec->u.v.f));
839 tab_title (t, 1, _("Writing %d record(s) to file %s."),
840 recno, handle_get_filename (fh));
842 tab_title (t, 1, _("Writing %d record(s) to the listing file."), recno);
846 /* PORTME: The number of characters in a line terminator. */
848 #define LINE_END_WIDTH 2 /* \r\n */
850 #define LINE_END_WIDTH 1 /* \n */
853 /* Calculates the maximum possible line width and allocates a buffer
854 big enough to contain it */
858 /* Cumulative maximum line width (excluding null terminator) so far. */
861 /* Width required by current this prt_out_spec. */
862 int pot_w; /* Potential w. */
865 struct prt_out_spec *i;
867 for (i = prt.spec; i; i = i->next)
875 pot_w = i->fc + strlen (i->u.c);
878 pot_w = i->fc + i->u.v.f.w;
891 prt.max_width = w + LINE_END_WIDTH + 1;
892 prt.line = xmalloc (prt.max_width);
895 /* Transformation. */
897 /* Performs the transformation inside print_trns T on case C. */
899 print_trns_proc (void *trns_, struct ccase *c, int case_num UNUSED)
901 /* Transformation. */
902 struct print_trns *t = trns_;
905 struct prt_out_spec *i;
910 /* Length of the line in buf. */
912 memset (buf, ' ', t->max_width);
914 if (t->options & PRT_EJECT)
917 /* Note that a field written to a place where a field has
918 already been written truncates the record. `PRINT /A B
919 (T10,F8,T1,F8).' only outputs B. */
920 for (i = t->spec; i; i = i->next)
924 if (t->writer == NULL)
927 tab_output_text (TAT_FIX | TAT_NOWRAP, buf);
931 if ((t->options & PRT_CMD_MASK) == PRT_PRINT
932 || !(t->options & PRT_BINARY))
934 /* PORTME: Line ends. */
941 dfm_put_record (t->writer, buf, len);
944 memset (buf, ' ', t->max_width);
949 /* FIXME: Should be revised to keep track of the string's
950 length outside the loop, probably in i->u.c[0]. */
951 memcpy (&buf[i->fc], i->u.c, strlen (i->u.c));
952 len = i->fc + strlen (i->u.c);
956 data_out (&buf[i->fc], &i->u.v.f, case_data (c, i->u.v.v->fv));
957 len = i->fc + i->u.v.f.w;
961 /* PRT_SPACE always immediately follows PRT_VAR. */
973 /* Frees all the data inside print_trns T. Does not free T. */
975 print_trns_free (void *prt_)
977 struct print_trns *prt = prt_;
978 struct prt_out_spec *i, *n;
980 for (i = prt->spec; i; i = n)
999 if (prt->writer != NULL)
1000 dfm_close_writer (prt->writer);
1007 /* PRINT SPACE transformation. */
1008 struct print_space_trns
1010 struct dfm_writer *writer; /* Output data file. */
1011 struct expression *e; /* Number of lines; NULL=1. */
1015 static trns_proc_func print_space_trns_proc;
1016 static trns_free_func print_space_trns_free;
1019 cmd_print_space (void)
1021 struct print_space_trns *t;
1022 struct file_handle *fh;
1023 struct expression *e;
1024 struct dfm_writer *writer;
1026 if (lex_match_id ("OUTFILE"))
1040 e = expr_parse (default_dict, EXPR_NUMBER);
1044 lex_error (_("expecting end of command"));
1053 writer = dfm_open_writer (fh);
1063 t = xmalloc (sizeof *t);
1067 add_transformation (print_space_trns_proc, print_space_trns_free, t);
1072 print_space_trns_proc (void *t_, struct ccase *c,
1073 int case_num UNUSED)
1075 struct print_space_trns *t = t_;
1080 n = expr_evaluate_num (t->e, c, case_num);
1082 msg (SW, _("The expression on PRINT SPACE evaluated to the "
1083 "system-missing value."));
1085 msg (SW, _("The expression on PRINT SPACE evaluated to %g."), n);
1089 if (t->writer == NULL)
1094 char buf[LINE_END_WIDTH];
1096 /* PORTME: Line ends. */
1104 dfm_put_record (t->writer, buf, LINE_END_WIDTH);
1111 print_space_trns_free (void *trns_)
1113 struct print_space_trns *trns = trns_;
1114 expr_free (trns->e);