1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000, 2006 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/data-out.h>
26 #include <data/procedure.h>
27 #include <data/transformations.h>
28 #include <data/variable.h>
29 #include <language/command.h>
30 #include <language/data-io/data-writer.h>
31 #include <language/data-io/file-handle.h>
32 #include <language/data-io/placement-parser.h>
33 #include <language/lexer/format-parser.h>
34 #include <language/lexer/lexer.h>
35 #include <language/lexer/variable-parser.h>
36 #include <libpspp/alloc.h>
37 #include <libpspp/assertion.h>
38 #include <libpspp/compiler.h>
39 #include <libpspp/ll.h>
40 #include <libpspp/message.h>
41 #include <libpspp/misc.h>
42 #include <libpspp/pool.h>
43 #include <output/manager.h>
44 #include <output/table.h>
47 #define _(msgid) gettext (msgid)
49 /* Describes what to do when an output field is encountered. */
52 PRT_LITERAL, /* Literal string. */
53 PRT_VAR /* Variable. */
56 /* Describes how to output one field. */
60 struct ll ll; /* In struct print_trns `specs' list. */
61 enum field_type type; /* What type of field this is. */
62 int record; /* 1-based record number. */
63 int first_column; /* 0-based first column. */
66 struct variable *var; /* Associated variable. */
67 struct fmt_spec format; /* Output spec. */
68 bool add_space; /* Add trailing space? */
69 bool sysmis_as_spaces; /* Output SYSMIS as spaces? */
71 /* PRT_LITERAL only. */
72 struct string string; /* String to output. */
75 static inline struct prt_out_spec *
76 ll_to_prt_out_spec (struct ll *ll)
78 return ll_data (ll, struct prt_out_spec, ll);
81 /* PRINT, PRINT EJECT, WRITE private data structure. */
84 struct pool *pool; /* Stores related data. */
85 bool eject; /* Eject page before printing? */
86 bool include_prefix; /* Prefix lines with space? */
87 struct dfm_writer *writer; /* Output file, NULL=listing file. */
88 struct ll_list specs; /* List of struct prt_out_specs. */
89 size_t record_cnt; /* Number of records to write. */
90 struct string line; /* Output buffer. */
99 static int internal_cmd_print (struct lexer *, struct dataset *ds,
100 enum which_formats, bool eject);
101 static trns_proc_func print_trns_proc;
102 static trns_free_func print_trns_free;
103 static bool parse_specs (struct lexer *, struct pool *tmp_pool, struct print_trns *,
104 struct dictionary *dict, enum which_formats);
105 static void dump_table (struct print_trns *, const struct file_handle *);
109 /* Parses PRINT command. */
111 cmd_print (struct lexer *lexer, struct dataset *ds)
113 return internal_cmd_print (lexer, ds, PRINT, false);
116 /* Parses PRINT EJECT command. */
118 cmd_print_eject (struct lexer *lexer, struct dataset *ds)
120 return internal_cmd_print (lexer, ds, PRINT, true);
123 /* Parses WRITE command. */
125 cmd_write (struct lexer *lexer, struct dataset *ds)
127 return internal_cmd_print (lexer, ds, WRITE, false);
130 /* Parses the output commands. */
132 internal_cmd_print (struct lexer *lexer, struct dataset *ds,
133 enum which_formats which_formats, bool eject)
135 bool print_table = 0;
136 struct print_trns *trns;
137 struct file_handle *fh = NULL;
138 struct pool *tmp_pool;
140 /* Fill in prt to facilitate error-handling. */
141 trns = pool_create_container (struct print_trns, pool);
144 trns->record_cnt = 0;
145 ll_init (&trns->specs);
146 ds_init_empty (&trns->line);
147 ds_register_pool (&trns->line, trns->pool);
149 tmp_pool = pool_create_subpool (trns->pool);
151 /* Parse the command options. */
152 while (lex_token (lexer) != '/' && lex_token (lexer) != '.')
154 if (lex_match_id (lexer, "OUTFILE"))
156 lex_match (lexer, '=');
158 fh = fh_parse (lexer, FH_REF_FILE);
162 else if (lex_match_id (lexer, "RECORDS"))
164 lex_match (lexer, '=');
165 lex_match (lexer, '(');
166 if (!lex_force_int (lexer))
168 trns->record_cnt = lex_integer (lexer);
170 lex_match (lexer, ')');
172 else if (lex_match_id (lexer, "TABLE"))
174 else if (lex_match_id (lexer, "NOTABLE"))
178 lex_error (lexer, _("expecting a valid subcommand"));
183 /* When PRINT or PRINT EJECT writes to an external file, we
184 prefix each line with a space for compatibility. */
185 trns->include_prefix = which_formats == PRINT && fh != NULL;
187 /* Parse variables and strings. */
188 if (!parse_specs (lexer, tmp_pool, trns, dataset_dict (ds), which_formats))
191 if (lex_end_of_command (lexer) != CMD_SUCCESS)
196 trns->writer = dfm_open_writer (fh);
197 if (trns->writer == NULL)
201 /* Output the variable table if requested. */
203 dump_table (trns, fh);
205 /* Put the transformation in the queue. */
206 add_transformation (ds, print_trns_proc, print_trns_free, trns);
208 pool_destroy (tmp_pool);
213 print_trns_free (trns);
217 static bool parse_string_argument (struct lexer *, struct print_trns *,
218 int record, int *column);
219 static bool parse_variable_argument (struct lexer *, const struct dictionary *,
221 struct pool *tmp_pool,
222 int *record, int *column,
225 /* Parses all the variable and string specifications on a single
226 PRINT, PRINT EJECT, or WRITE command into the prt structure.
229 parse_specs (struct lexer *lexer, struct pool *tmp_pool, struct print_trns *trns,
230 struct dictionary *dict,
231 enum which_formats which_formats)
236 if (lex_token (lexer) == '.')
238 trns->record_cnt = 1;
242 while (lex_token (lexer) != '.')
246 if (!parse_record_placement (lexer, &record, &column))
249 if (lex_token (lexer) == T_STRING)
250 ok = parse_string_argument (lexer, trns, record, &column);
252 ok = parse_variable_argument (lexer, dict, trns, tmp_pool, &record, &column,
257 lex_match (lexer, ',');
260 if (trns->record_cnt != 0 && trns->record_cnt != record)
261 msg (SW, _("Output calls for %d records but %d specified on RECORDS "
263 record, trns->record_cnt);
264 trns->record_cnt = record;
269 /* Parses a string argument to the PRINT commands. Returns success. */
271 parse_string_argument (struct lexer *lexer, struct print_trns *trns, int record, int *column)
273 struct prt_out_spec *spec = pool_alloc (trns->pool, sizeof *spec);
274 spec->type = PRT_LITERAL;
275 spec->record = record;
276 spec->first_column = *column;
277 ds_init_string (&spec->string, lex_tokstr (lexer));
278 ds_register_pool (&spec->string, trns->pool);
281 /* Parse the included column range. */
282 if (lex_is_number (lexer))
284 int first_column, last_column;
285 bool range_specified;
287 if (!parse_column_range (lexer, &first_column, &last_column, &range_specified))
290 spec->first_column = first_column;
292 ds_set_length (&spec->string, last_column - first_column + 1, ' ');
294 *column = spec->first_column + ds_length (&spec->string);
296 ll_push_tail (&trns->specs, &spec->ll);
300 /* Parses a variable argument to the PRINT commands by passing it off
301 to fixed_parse_compatible() or fixed_parse_fortran() as appropriate.
304 parse_variable_argument (struct lexer *lexer, const struct dictionary *dict,
305 struct print_trns *trns, struct pool *tmp_pool,
306 int *record, int *column,
307 enum which_formats which_formats)
309 struct variable **vars;
310 size_t var_cnt, var_idx;
311 struct fmt_spec *formats, *f;
315 if (!parse_variables_pool (lexer, tmp_pool, dict,
316 &vars, &var_cnt, PV_DUPLICATE))
319 if (lex_is_number (lexer) || lex_token (lexer) == '(')
321 if (!parse_var_placements (lexer, tmp_pool, var_cnt, false,
322 &formats, &format_cnt))
330 lex_match (lexer, '*');
332 formats = pool_nmalloc (tmp_pool, var_cnt, sizeof *formats);
333 format_cnt = var_cnt;
334 for (i = 0; i < var_cnt; i++)
336 struct variable *v = vars[i];
337 formats[i] = (which_formats == PRINT
338 ? *var_get_print_format (v)
339 : *var_get_write_format (v));
341 add_space = which_formats == PRINT;
345 for (f = formats; f < &formats[format_cnt]; f++)
346 if (!execute_placement_format (f, record, column))
348 struct variable *var;
349 struct prt_out_spec *spec;
351 var = vars[var_idx++];
352 if (!fmt_check_width_compat (f, var_get_width (var)))
355 spec = pool_alloc (trns->pool, sizeof *spec);
356 spec->type = PRT_VAR;
357 spec->record = *record;
358 spec->first_column = *column;
361 spec->add_space = add_space;
363 /* This is a completely bizarre twist for compatibility:
364 WRITE outputs the system-missing value as a field
365 filled with spaces, instead of using the normal format
366 that usually contains a period. */
367 spec->sysmis_as_spaces = (which_formats == WRITE
368 && var_is_numeric (var)
369 && (fmt_get_category (spec->format.type)
372 ll_push_tail (&trns->specs, &spec->ll);
374 *column += f->w + add_space;
376 assert (var_idx == var_cnt);
381 /* Prints the table produced by the TABLE subcommand to the listing
384 dump_table (struct print_trns *trns, const struct file_handle *fh)
386 struct prt_out_spec *spec;
391 spec_cnt = ll_count (&trns->specs);
392 t = tab_create (4, spec_cnt + 1, 0);
393 tab_columns (t, TAB_COL_DOWN, 1);
394 tab_box (t, TAL_1, TAL_1, TAL_0, TAL_1, 0, 0, 3, spec_cnt);
395 tab_hline (t, TAL_2, 0, 3, 1);
396 tab_headers (t, 0, 0, 1, 0);
397 tab_text (t, 0, 0, TAB_CENTER | TAT_TITLE, _("Variable"));
398 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Record"));
399 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Columns"));
400 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Format"));
401 tab_dim (t, tab_natural_dimensions);
403 ll_for_each (spec, struct prt_out_spec, ll, &trns->specs)
405 char fmt_string[FMT_STRING_LEN_MAX + 1];
410 tab_text (t, 0, row, TAB_LEFT | TAB_FIX | TAT_PRINTF, "\"%.*s\"",
411 (int) ds_length (&spec->string), ds_data (&spec->string));
412 width = ds_length (&spec->string);
415 tab_text (t, 0, row, TAB_LEFT, var_get_name (spec->var));
416 tab_text (t, 3, row, TAB_LEFT | TAB_FIX,
417 fmt_to_string (&spec->format, fmt_string));
418 width = spec->format.w;
423 tab_text (t, 1, row, TAT_PRINTF, "%d", spec->record);
424 tab_text (t, 2, row, TAT_PRINTF, "%3d-%3d",
425 spec->first_column, spec->first_column + width - 1);
430 tab_title (t, ngettext ("Writing %d record to %s.",
431 "Writing %d records to %s.", trns->record_cnt),
432 trns->record_cnt, fh_get_name (fh));
434 tab_title (t, ngettext ("Writing %d record.",
435 "Writing %d records.", trns->record_cnt),
440 /* Transformation. */
442 static void flush_records (struct print_trns *, int target_record,
443 bool *eject, int *record);
445 /* Performs the transformation inside print_trns T on case C. */
447 print_trns_proc (void *trns_, struct ccase *c, casenumber case_num UNUSED)
449 struct print_trns *trns = trns_;
450 bool eject = trns->eject;
452 struct prt_out_spec *spec;
454 ds_clear (&trns->line);
455 ds_put_char (&trns->line, ' ');
456 ll_for_each (spec, struct prt_out_spec, ll, &trns->specs)
458 flush_records (trns, spec->record, &eject, &record);
460 ds_set_length (&trns->line, spec->first_column, ' ');
461 if (spec->type == PRT_VAR)
463 const union value *input = case_data (c, spec->var->fv);
464 char *output = ds_put_uninit (&trns->line, spec->format.w);
465 if (!spec->sysmis_as_spaces || input->f != SYSMIS)
466 data_out (input, &spec->format, output);
468 memset (output, ' ', spec->format.w);
470 ds_put_char (&trns->line, ' ');
473 ds_put_substring (&trns->line, ds_ss (&spec->string));
475 flush_records (trns, trns->record_cnt + 1, &eject, &record);
477 if (trns->writer != NULL && dfm_write_error (trns->writer))
479 return TRNS_CONTINUE;
482 /* Advance from *RECORD to TARGET_RECORD, outputting records
483 along the way. If *EJECT is true, then the first record
484 output is preceded by ejecting the page (and *EJECT is set
487 flush_records (struct print_trns *trns, int target_record,
488 bool *eject, int *record)
490 for (; target_record > *record; (*record)++)
492 char *line = ds_cstr (&trns->line);
493 size_t length = ds_length (&trns->line);
499 if (trns->writer == NULL)
506 if (trns->writer == NULL)
507 tab_output_text (TAB_FIX | TAT_NOWRAP, &line[1]);
510 if (!trns->include_prefix)
515 dfm_put_record (trns->writer, line, length);
518 ds_truncate (&trns->line, 1);
524 print_trns_free (void *trns_)
526 struct print_trns *trns = trns_;
529 if (trns->writer != NULL)
530 ok = dfm_close_writer (trns->writer);
531 pool_destroy (trns->pool);