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
24 #include <data/case.h>
25 #include <data/procedure.h>
26 #include <data/transformations.h>
27 #include <data/variable.h>
28 #include <language/command.h>
29 #include <language/data-io/data-writer.h>
30 #include <language/data-io/file-handle.h>
31 #include <language/data-io/placement-parser.h>
32 #include <language/lexer/format-parser.h>
33 #include <language/lexer/lexer.h>
34 #include <language/lexer/variable-parser.h>
35 #include <libpspp/alloc.h>
36 #include <libpspp/assertion.h>
37 #include <libpspp/compiler.h>
38 #include <libpspp/ll.h>
39 #include <libpspp/message.h>
40 #include <libpspp/misc.h>
41 #include <libpspp/pool.h>
42 #include <output/manager.h>
43 #include <output/table.h>
46 #define _(msgid) gettext (msgid)
48 /* Describes what to do when an output field is encountered. */
51 PRT_LITERAL, /* Literal string. */
52 PRT_VAR /* Variable. */
55 /* Describes how to output one field. */
59 struct ll ll; /* In struct print_trns `specs' list. */
60 enum field_type type; /* What type of field this is. */
61 int record; /* 1-based record number. */
62 int first_column; /* 0-based first column. */
65 struct variable *var; /* Associated variable. */
66 struct fmt_spec format; /* Output spec. */
67 bool add_space; /* Add trailing space? */
69 /* PRT_LITERAL only. */
70 struct string string; /* String to output. */
73 static inline struct prt_out_spec *
74 ll_to_prt_out_spec (struct ll *ll)
76 return ll_data (ll, struct prt_out_spec, ll);
79 /* PRINT, PRINT EJECT, WRITE private data structure. */
82 struct pool *pool; /* Stores related data. */
83 bool eject; /* Eject page before printing? */
84 bool omit_new_lines; /* Omit new-line characters? */
85 struct dfm_writer *writer; /* Output file, NULL=listing file. */
86 struct ll_list specs; /* List of struct prt_out_specs. */
87 size_t record_cnt; /* Number of records to write. */
88 struct string line; /* Output buffer. */
97 static int internal_cmd_print (enum which_formats, bool eject);
98 static trns_proc_func print_trns_proc;
99 static trns_free_func print_trns_free;
100 static bool parse_specs (struct pool *tmp_pool, struct print_trns *,
102 static void dump_table (struct print_trns *, const struct file_handle *);
106 /* Parses PRINT command. */
110 return internal_cmd_print (PRINT, false);
113 /* Parses PRINT EJECT command. */
115 cmd_print_eject (void)
117 return internal_cmd_print (PRINT, true);
120 /* Parses WRITE command. */
124 return internal_cmd_print (WRITE, false);
127 /* Parses the output commands. */
129 internal_cmd_print (enum which_formats which_formats, bool eject)
131 bool print_table = 0;
132 struct print_trns *trns;
133 struct file_handle *fh = NULL;
134 struct pool *tmp_pool;
136 /* Fill in prt to facilitate error-handling. */
137 trns = pool_create_container (struct print_trns, pool);
140 trns->record_cnt = 0;
141 ll_init (&trns->specs);
142 ds_init_empty (&trns->line);
143 ds_register_pool (&trns->line, trns->pool);
145 tmp_pool = pool_create_subpool (trns->pool);
147 /* Parse the command options. */
150 if (lex_match_id ("OUTFILE"))
154 fh = fh_parse (FH_REF_FILE);
158 else if (lex_match_id ("RECORDS"))
162 if (!lex_force_int ())
164 trns->record_cnt = lex_integer ();
168 else if (lex_match_id ("TABLE"))
170 else if (lex_match_id ("NOTABLE"))
174 lex_error (_("expecting a valid subcommand"));
179 /* Parse variables and strings. */
180 if (!parse_specs (tmp_pool, trns, which_formats))
183 if (lex_end_of_command () != CMD_SUCCESS)
188 trns->writer = dfm_open_writer (fh);
189 if (trns->writer == NULL)
192 trns->omit_new_lines = (which_formats == WRITE
193 && fh_get_mode (fh) == FH_MODE_BINARY);
196 /* Output the variable table if requested. */
198 dump_table (trns, fh);
200 /* Put the transformation in the queue. */
201 add_transformation (print_trns_proc, print_trns_free, trns);
203 pool_destroy (tmp_pool);
208 print_trns_free (trns);
212 static bool parse_string_argument (struct print_trns *,
213 int record, int *column);
214 static bool parse_variable_argument (struct print_trns *,
215 struct pool *tmp_pool,
216 int *record, int *column,
219 /* Parses all the variable and string specifications on a single
220 PRINT, PRINT EJECT, or WRITE command into the prt structure.
223 parse_specs (struct pool *tmp_pool, struct print_trns *trns,
224 enum which_formats which_formats)
233 if (!parse_record_placement (&record, &column))
236 if (token == T_STRING)
237 ok = parse_string_argument (trns, record, &column);
239 ok = parse_variable_argument (trns, tmp_pool, &record, &column,
247 if (trns->record_cnt != 0 && trns->record_cnt != record)
248 msg (SW, _("Output calls for %d records but %d specified on RECORDS "
250 record, trns->record_cnt);
251 trns->record_cnt = record;
256 /* Parses a string argument to the PRINT commands. Returns success. */
258 parse_string_argument (struct print_trns *trns, int record, int *column)
260 struct prt_out_spec *spec = pool_alloc (trns->pool, sizeof *spec);
261 spec->type = PRT_LITERAL;
262 spec->record = record;
263 spec->first_column = *column;
264 ds_init_string (&spec->string, &tokstr);
265 ds_register_pool (&spec->string, trns->pool);
268 /* Parse the included column range. */
269 if (lex_is_number ())
271 int first_column, last_column;
272 bool range_specified;
274 if (!parse_column_range (&first_column, &last_column, &range_specified))
277 spec->first_column = first_column;
279 ds_set_length (&spec->string, last_column - first_column + 1, ' ');
281 *column = spec->first_column + ds_length (&spec->string);
283 ll_push_tail (&trns->specs, &spec->ll);
287 /* Parses a variable argument to the PRINT commands by passing it off
288 to fixed_parse_compatible() or fixed_parse_fortran() as appropriate.
291 parse_variable_argument (struct print_trns *trns, struct pool *tmp_pool,
292 int *record, int *column,
293 enum which_formats which_formats)
295 struct variable **vars;
296 size_t var_cnt, var_idx;
297 struct fmt_spec *formats, *f;
301 if (!parse_variables_pool (tmp_pool,
302 default_dict, &vars, &var_cnt, PV_DUPLICATE))
305 if (lex_is_number () || token == '(')
307 if (!parse_var_placements (tmp_pool, var_cnt, &formats, &format_cnt))
317 formats = pool_nmalloc (tmp_pool, var_cnt, sizeof *formats);
318 format_cnt = var_cnt;
319 for (i = 0; i < var_cnt; i++)
321 struct variable *v = vars[i];
322 formats[i] = which_formats == PRINT ? v->print : v->write;
328 for (f = formats; f < &formats[format_cnt]; f++)
329 if (!execute_placement_format (f, record, column))
331 struct variable *var;
332 struct prt_out_spec *spec;
334 var = vars[var_idx++];
335 if (!check_specifier_width (f, var->width, true))
338 spec = pool_alloc (trns->pool, sizeof *spec);
339 spec->type = PRT_VAR;
340 spec->record = *record;
341 spec->first_column = *column;
344 spec->add_space = add_space;
345 ll_push_tail (&trns->specs, &spec->ll);
347 *column += f->w + add_space;
349 assert (var_idx == var_cnt);
354 /* Prints the table produced by the TABLE subcommand to the listing
357 dump_table (struct print_trns *trns, const struct file_handle *fh)
359 struct prt_out_spec *spec;
364 spec_cnt = ll_count (&trns->specs);
365 t = tab_create (4, spec_cnt + 1, 0);
366 tab_columns (t, TAB_COL_DOWN, 1);
367 tab_box (t, TAL_1, TAL_1, TAL_0, TAL_1, 0, 0, 3, spec_cnt);
368 tab_hline (t, TAL_2, 0, 3, 1);
369 tab_headers (t, 0, 0, 1, 0);
370 tab_text (t, 0, 0, TAB_CENTER | TAT_TITLE, _("Variable"));
371 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Record"));
372 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Columns"));
373 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Format"));
374 tab_dim (t, tab_natural_dimensions);
376 ll_for_each (spec, struct prt_out_spec, ll, &trns->specs)
382 tab_text (t, 0, row, TAB_LEFT | TAB_FIX | TAT_PRINTF, "\"%.*s\"",
383 (int) ds_length (&spec->string), ds_data (&spec->string));
384 width = ds_length (&spec->string);
387 tab_text (t, 0, row, TAB_LEFT, spec->var->name);
388 tab_text (t, 3, row, TAB_LEFT | TAB_FIX,
389 fmt_to_string (&spec->format));
390 width = spec->format.w;
395 tab_text (t, 1, row, TAT_PRINTF, "%d", spec->record);
396 tab_text (t, 2, row, TAT_PRINTF, "%3d-%3d",
397 spec->first_column, spec->first_column + width - 1);
402 tab_title (t, ngettext ("Writing %d record to %s.",
403 "Writing %d records to %s.", trns->record_cnt),
404 trns->record_cnt, fh_get_name (fh));
406 tab_title (t, ngettext ("Writing %d record.",
407 "Writing %d records.", trns->record_cnt),
412 /* Transformation. */
414 static void flush_records (struct print_trns *,
415 int target_record, int *record);
417 /* Performs the transformation inside print_trns T on case C. */
419 print_trns_proc (void *trns_, struct ccase *c, int case_num UNUSED)
421 struct print_trns *trns = trns_;
422 struct prt_out_spec *spec;
429 ds_clear (&trns->line);
430 ll_for_each (spec, struct prt_out_spec, ll, &trns->specs)
432 flush_records (trns, spec->record, &record);
434 ds_set_length (&trns->line, spec->first_column - 1, ' ');
435 if (spec->type == PRT_VAR)
437 data_out (ds_put_uninit (&trns->line, spec->format.w),
438 &spec->format, case_data (c, spec->var->fv));
440 ds_put_char (&trns->line, ' ');
443 ds_put_substring (&trns->line, ds_ss (&spec->string));
445 flush_records (trns, trns->record_cnt + 1, &record);
447 if (trns->writer != NULL && dfm_write_error (trns->writer))
449 return TRNS_CONTINUE;
453 flush_records (struct print_trns *trns, int target_record, int *record)
455 while (target_record > *record)
457 if (trns->writer == NULL)
458 tab_output_text (TAB_FIX | TAT_NOWRAP, ds_cstr (&trns->line));
461 if (!trns->omit_new_lines)
462 ds_put_char (&trns->line, '\n');
464 dfm_put_record (trns->writer,
465 ds_data (&trns->line), ds_length (&trns->line));
467 ds_clear (&trns->line);
475 print_trns_free (void *trns_)
477 struct print_trns *trns = trns_;
480 if (trns->writer != NULL)
481 ok = dfm_close_writer (trns->writer);
482 pool_destroy (trns->pool);