71791ba59541573b8b2f192f2627ea4182962bca
[pspp] / src / language / data-io / print.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <stdlib.h>
20
21 #include "data/case.h"
22 #include "data/dataset.h"
23 #include "data/data-out.h"
24 #include "data/format.h"
25 #include "data/transformations.h"
26 #include "data/variable.h"
27 #include "language/command.h"
28 #include "language/data-io/data-writer.h"
29 #include "language/data-io/file-handle.h"
30 #include "language/data-io/placement-parser.h"
31 #include "language/lexer/format-parser.h"
32 #include "language/lexer/lexer.h"
33 #include "language/lexer/variable-parser.h"
34 #include "libpspp/assertion.h"
35 #include "libpspp/compiler.h"
36 #include "libpspp/i18n.h"
37 #include "libpspp/ll.h"
38 #include "libpspp/message.h"
39 #include "libpspp/misc.h"
40 #include "libpspp/pool.h"
41 #include "output/tab.h"
42 #include "output/text-item.h"
43
44 #include "gl/xalloc.h"
45
46 #include "gettext.h"
47 #define _(msgid) gettext (msgid)
48
49 /* Describes what to do when an output field is encountered. */
50 enum field_type
51   {
52     PRT_LITERAL,                /* Literal string. */
53     PRT_VAR                     /* Variable. */
54   };
55
56 /* Describes how to output one field. */
57 struct prt_out_spec
58   {
59     /* All fields. */
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. */
64
65     /* PRT_VAR only. */
66     const 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? */
70
71     /* PRT_LITERAL only. */
72     struct string string;       /* String to output. */
73   };
74
75 static inline struct prt_out_spec *
76 ll_to_prt_out_spec (struct ll *ll)
77 {
78   return ll_data (ll, struct prt_out_spec, ll);
79 }
80
81 /* PRINT, PRINT EJECT, WRITE private data structure. */
82 struct print_trns
83   {
84     struct pool *pool;          /* Stores related data. */
85     bool eject;                 /* Eject page before printing? */
86     bool include_prefix;        /* Prefix lines with space? */
87     const char *encoding;       /* Encoding to use for output. */
88     struct dfm_writer *writer;  /* Output file, NULL=listing file. */
89     struct ll_list specs;       /* List of struct prt_out_specs. */
90     size_t record_cnt;          /* Number of records to write. */
91     struct u8_line line;        /* Output buffer. */
92
93     int unit;                   /* Unit width, in bytes. */
94     char one[MAX_UNIT];         /* '1' in encoding, 'unit' bytes long. */
95     char space[MAX_UNIT];       /* \n in encoding, 'unit' bytes long. */
96   };
97
98 enum which_formats
99   {
100     PRINT,
101     WRITE
102   };
103
104 static int internal_cmd_print (struct lexer *, struct dataset *ds,
105                                enum which_formats, bool eject);
106 static trns_proc_func print_trns_proc;
107 static trns_free_func print_trns_free;
108 static bool parse_specs (struct lexer *, struct pool *tmp_pool, struct print_trns *,
109                          struct dictionary *dict, enum which_formats);
110 static void dump_table (struct print_trns *, const struct file_handle *);
111 \f
112 /* Basic parsing. */
113
114 /* Parses PRINT command. */
115 int
116 cmd_print (struct lexer *lexer, struct dataset *ds)
117 {
118   return internal_cmd_print (lexer, ds, PRINT, false);
119 }
120
121 /* Parses PRINT EJECT command. */
122 int
123 cmd_print_eject (struct lexer *lexer, struct dataset *ds)
124 {
125   return internal_cmd_print (lexer, ds, PRINT, true);
126 }
127
128 /* Parses WRITE command. */
129 int
130 cmd_write (struct lexer *lexer, struct dataset *ds)
131 {
132   return internal_cmd_print (lexer, ds, WRITE, false);
133 }
134
135 /* Parses the output commands. */
136 static int
137 internal_cmd_print (struct lexer *lexer, struct dataset *ds,
138                     enum which_formats which_formats, bool eject)
139 {
140   bool print_table = 0;
141   struct print_trns *trns;
142   struct file_handle *fh = NULL;
143   char *encoding = NULL;
144   struct pool *tmp_pool;
145
146   /* Fill in prt to facilitate error-handling. */
147   trns = pool_create_container (struct print_trns, pool);
148   trns->eject = eject;
149   trns->writer = NULL;
150   trns->record_cnt = 0;
151   ll_init (&trns->specs);
152   u8_line_init (&trns->line);
153   u8_line_register_pool (&trns->line, trns->pool);
154
155   tmp_pool = pool_create_subpool (trns->pool);
156
157   /* Parse the command options. */
158   while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
159     {
160       if (lex_match_id (lexer, "OUTFILE"))
161         {
162           lex_match (lexer, T_EQUALS);
163
164           fh = fh_parse (lexer, FH_REF_FILE, NULL);
165           if (fh == NULL)
166             goto error;
167         }
168       else if (lex_match_id (lexer, "ENCODING"))
169         {
170           lex_match (lexer, T_EQUALS);
171           if (!lex_force_string (lexer))
172             goto error;
173
174           free (encoding);
175           encoding = ss_xstrdup (lex_tokss (lexer));
176
177           lex_get (lexer);
178         }
179       else if (lex_match_id (lexer, "RECORDS"))
180         {
181           lex_match (lexer, T_EQUALS);
182           lex_match (lexer, T_LPAREN);
183           if (!lex_force_int (lexer))
184             goto error;
185           trns->record_cnt = lex_integer (lexer);
186           lex_get (lexer);
187           lex_match (lexer, T_RPAREN);
188         }
189       else if (lex_match_id (lexer, "TABLE"))
190         print_table = true;
191       else if (lex_match_id (lexer, "NOTABLE"))
192         print_table = false;
193       else
194         {
195           lex_error (lexer, _("expecting a valid subcommand"));
196           goto error;
197         }
198     }
199
200   /* When PRINT or PRINT EJECT writes to an external file, we
201      prefix each line with a space for compatibility. */
202   trns->include_prefix = which_formats == PRINT && fh != NULL;
203
204   /* Parse variables and strings. */
205   if (!parse_specs (lexer, tmp_pool, trns, dataset_dict (ds), which_formats))
206     goto error;
207
208   if (lex_end_of_command (lexer) != CMD_SUCCESS)
209     goto error;
210
211   if (fh != NULL)
212     {
213       trns->writer = dfm_open_writer (fh, encoding);
214       if (trns->writer == NULL)
215         goto error;
216       trns->encoding = dfm_writer_get_encoding (trns->writer);
217     }
218   else
219     trns->encoding = UTF8;
220
221   /* Output the variable table if requested. */
222   if (print_table)
223     dump_table (trns, fh);
224
225   /* Put the transformation in the queue. */
226   add_transformation (ds, print_trns_proc, print_trns_free, trns);
227
228   pool_destroy (tmp_pool);
229   fh_unref (fh);
230
231   return CMD_SUCCESS;
232
233  error:
234   print_trns_free (trns);
235   fh_unref (fh);
236   return CMD_FAILURE;
237 }
238 \f
239 static bool parse_string_argument (struct lexer *, struct print_trns *,
240                                    int record, int *column);
241 static bool parse_variable_argument (struct lexer *, const struct dictionary *,
242                                      struct print_trns *,
243                                      struct pool *tmp_pool,
244                                      int *record, int *column,
245                                      enum which_formats);
246
247 /* Parses all the variable and string specifications on a single
248    PRINT, PRINT EJECT, or WRITE command into the prt structure.
249    Returns success. */
250 static bool
251 parse_specs (struct lexer *lexer, struct pool *tmp_pool, struct print_trns *trns,
252              struct dictionary *dict,
253              enum which_formats which_formats)
254 {
255   int record = 0;
256   int column = 1;
257
258   if (lex_token (lexer) == T_ENDCMD)
259     {
260       trns->record_cnt = 1;
261       return true;
262     }
263
264   while (lex_token (lexer) != T_ENDCMD)
265     {
266       bool ok;
267
268       if (!parse_record_placement (lexer, &record, &column))
269         return false;
270
271       if (lex_is_string (lexer))
272         ok = parse_string_argument (lexer, trns, record, &column);
273       else
274         ok = parse_variable_argument (lexer, dict, trns, tmp_pool, &record, &column,
275                                       which_formats);
276       if (!ok)
277         return 0;
278
279       lex_match (lexer, T_COMMA);
280     }
281
282   if (trns->record_cnt != 0 && trns->record_cnt != record)
283     msg (SW, _("Output calls for %d records but %zu specified on RECORDS "
284                "subcommand."),
285          record, trns->record_cnt);
286   trns->record_cnt = record;
287
288   return true;
289 }
290
291 /* Parses a string argument to the PRINT commands.  Returns success. */
292 static bool
293 parse_string_argument (struct lexer *lexer, struct print_trns *trns, int record, int *column)
294 {
295   struct prt_out_spec *spec = pool_alloc (trns->pool, sizeof *spec);
296   spec->type = PRT_LITERAL;
297   spec->record = record;
298   spec->first_column = *column;
299   ds_init_substring (&spec->string, lex_tokss (lexer));
300   ds_register_pool (&spec->string, trns->pool);
301   lex_get (lexer);
302
303   /* Parse the included column range. */
304   if (lex_is_number (lexer))
305     {
306       int first_column, last_column;
307       bool range_specified;
308
309       if (!parse_column_range (lexer, 1,
310                                &first_column, &last_column, &range_specified))
311         return false;
312
313       spec->first_column = first_column;
314       if (range_specified)
315         ds_set_length (&spec->string, last_column - first_column + 1, ' ');
316     }
317   *column = spec->first_column + ds_length (&spec->string);
318
319   ll_push_tail (&trns->specs, &spec->ll);
320   return true;
321 }
322
323 /* Parses a variable argument to the PRINT commands by passing it off
324    to fixed_parse_compatible() or fixed_parse_fortran() as appropriate.
325    Returns success. */
326 static bool
327 parse_variable_argument (struct lexer *lexer, const struct dictionary *dict,
328                          struct print_trns *trns, struct pool *tmp_pool,
329                          int *record, int *column,
330                          enum which_formats which_formats)
331 {
332   const struct variable **vars;
333   size_t var_cnt, var_idx;
334   struct fmt_spec *formats, *f;
335   size_t format_cnt;
336   bool add_space;
337
338   if (!parse_variables_const_pool (lexer, tmp_pool, dict,
339                              &vars, &var_cnt, PV_DUPLICATE))
340     return false;
341
342   if (lex_is_number (lexer) || lex_token (lexer) == T_LPAREN)
343     {
344       if (!parse_var_placements (lexer, tmp_pool, var_cnt, FMT_FOR_OUTPUT,
345                                  &formats, &format_cnt))
346         return false;
347       add_space = false;
348     }
349   else
350     {
351       size_t i;
352
353       lex_match (lexer, T_ASTERISK);
354
355       formats = pool_nmalloc (tmp_pool, var_cnt, sizeof *formats);
356       format_cnt = var_cnt;
357       for (i = 0; i < var_cnt; i++)
358         {
359           const struct variable *v = vars[i];
360           formats[i] = (which_formats == PRINT
361                         ? *var_get_print_format (v)
362                         : *var_get_write_format (v));
363         }
364       add_space = which_formats == PRINT;
365     }
366
367   var_idx = 0;
368   for (f = formats; f < &formats[format_cnt]; f++)
369     if (!execute_placement_format (f, record, column))
370       {
371         const struct variable *var;
372         struct prt_out_spec *spec;
373
374         var = vars[var_idx++];
375         if (!fmt_check_width_compat (f, var_get_width (var)))
376           return false;
377
378         spec = pool_alloc (trns->pool, sizeof *spec);
379         spec->type = PRT_VAR;
380         spec->record = *record;
381         spec->first_column = *column;
382         spec->var = var;
383         spec->format = *f;
384         spec->add_space = add_space;
385
386         /* This is a completely bizarre twist for compatibility:
387            WRITE outputs the system-missing value as a field
388            filled with spaces, instead of using the normal format
389            that usually contains a period. */
390         spec->sysmis_as_spaces = (which_formats == WRITE
391                                   && var_is_numeric (var)
392                                   && (fmt_get_category (spec->format.type)
393                                       != FMT_CAT_BINARY));
394
395         ll_push_tail (&trns->specs, &spec->ll);
396
397         *column += f->w + add_space;
398       }
399   assert (var_idx == var_cnt);
400
401   return true;
402 }
403
404 /* Prints the table produced by the TABLE subcommand to the listing
405    file. */
406 static void
407 dump_table (struct print_trns *trns, const struct file_handle *fh)
408 {
409   struct prt_out_spec *spec;
410   struct tab_table *t;
411   int spec_cnt;
412   int row;
413
414   spec_cnt = ll_count (&trns->specs);
415   t = tab_create (4, spec_cnt + 1);
416   tab_box (t, TAL_1, TAL_1, TAL_0, TAL_1, 0, 0, 3, spec_cnt);
417   tab_hline (t, TAL_2, 0, 3, 1);
418   tab_headers (t, 0, 0, 1, 0);
419   tab_text (t, 0, 0, TAB_CENTER | TAT_TITLE, _("Variable"));
420   tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Record"));
421   tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Columns"));
422   tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Format"));
423   row = 1;
424   ll_for_each (spec, struct prt_out_spec, ll, &trns->specs)
425     {
426       char fmt_string[FMT_STRING_LEN_MAX + 1];
427       int width;
428       switch (spec->type)
429         {
430         case PRT_LITERAL:
431           tab_text_format (t, 0, row, TAB_LEFT | TAB_FIX, "`%.*s'",
432                            (int) ds_length (&spec->string),
433                            ds_data (&spec->string));
434           width = ds_length (&spec->string);
435           break;
436         case PRT_VAR:
437           tab_text (t, 0, row, TAB_LEFT, var_get_name (spec->var));
438           tab_text (t, 3, row, TAB_LEFT | TAB_FIX,
439                     fmt_to_string (&spec->format, fmt_string));
440           width = spec->format.w;
441           break;
442         default:
443           NOT_REACHED ();
444         }
445       tab_text_format (t, 1, row, 0, "%d", spec->record);
446       tab_text_format (t, 2, row, 0, "%3d-%3d",
447                        spec->first_column, spec->first_column + width - 1);
448       row++;
449     }
450
451   if (fh != NULL)
452     tab_title (t, ngettext ("Writing %zu record to %s.",
453                             "Writing %zu records to %s.", trns->record_cnt),
454                trns->record_cnt, fh_get_name (fh));
455   else
456     tab_title (t, ngettext ("Writing %zu record.",
457                             "Writing %zu records.", trns->record_cnt),
458                trns->record_cnt);
459   tab_submit (t);
460 }
461 \f
462 /* Transformation. */
463
464 static void flush_records (struct print_trns *, int target_record,
465                            bool *eject, int *record);
466
467 /* Performs the transformation inside print_trns T on case C. */
468 static int
469 print_trns_proc (void *trns_, struct ccase **c, casenumber case_num UNUSED)
470 {
471   struct print_trns *trns = trns_;
472   bool eject = trns->eject;
473   char encoded_space = recode_byte (trns->encoding, C_ENCODING, ' ');
474   int record = 1;
475   struct prt_out_spec *spec;
476
477   u8_line_clear (&trns->line);
478
479   ds_put_byte (&trns->line.s, ' ');
480   trns->line.width = 0;
481
482   ll_for_each (spec, struct prt_out_spec, ll, &trns->specs)
483     {
484       flush_records (trns, spec->record, &eject, &record);
485
486       if (spec->type == PRT_VAR)
487         {
488           const union value *input = case_data (*c, spec->var);
489           if (!spec->sysmis_as_spaces || input->f != SYSMIS)
490             {
491               char *s = data_out (input, var_get_encoding (spec->var),
492                                   &spec->format);
493               int width = u8_strwidth (s);
494               size_t n = strlen (s);
495               u8_line_put (&trns->line, spec->first_column,
496                            spec->first_column + width, s, n);
497               free (s);
498             }
499           else
500             {
501               memset (u8_line-record
502
503             }
504             ds_put_byte_multiple (&trns->line, encoded_space, spec->format.w);
505           if (spec->add_space)
506             ds_put_byte (&trns->line, encoded_space);
507         }
508       else
509         {
510           ds_put_substring (&trns->line, ds_ss (&spec->string));
511           if (0 != strcmp (trns->encoding, UTF8))
512             {
513               size_t length = ds_length (&spec->string);
514               char *data = ss_data (ds_tail (&trns->line, length));
515               char *s = recode_string (trns->encoding, UTF8, data, length);
516               memcpy (data, s, length);
517               free (s);
518             }
519         }
520     }
521   flush_records (trns, trns->record_cnt + 1, &eject, &record);
522
523   if (trns->writer != NULL && dfm_write_error (trns->writer))
524     return TRNS_ERROR;
525   return TRNS_CONTINUE;
526 }
527
528 /* Advance from *RECORD to TARGET_RECORD, outputting records
529    along the way.  If *EJECT is true, then the first record
530    output is preceded by ejecting the page (and *EJECT is set
531    false). */
532 static void
533 flush_records (struct print_trns *trns, int target_record,
534                bool *eject, int *record)
535 {
536   for (; target_record > *record; (*record)++)
537     {
538       char *line = ds_cstr (&trns->line);
539       size_t length = ds_length (&trns->line);
540       char leader = ' ';
541
542       if (*eject)
543         {
544           *eject = false;
545           if (trns->writer == NULL)
546             text_item_submit (text_item_create (TEXT_ITEM_EJECT_PAGE, ""));
547           else
548             leader = '1';
549         }
550       line[0] = recode_byte (trns->encoding, C_ENCODING, leader);
551
552       if (trns->writer == NULL)
553         tab_output_text (TAB_FIX, &line[1]);
554       else
555         {
556           if (!trns->include_prefix)
557             {
558               line++;
559               length--;
560             }
561           dfm_put_record (trns->writer, line, length);
562         }
563
564       ds_truncate (&trns->line, 1);
565     }
566 }
567
568 /* Frees TRNS. */
569 static bool
570 print_trns_free (void *trns_)
571 {
572   struct print_trns *trns = trns_;
573   bool ok = true;
574
575   if (trns->writer != NULL)
576     ok = dfm_close_writer (trns->writer);
577   pool_destroy (trns->pool);
578
579   return ok;
580 }
581