1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
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.
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.
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/>. */
26 #include "data/file-handle-def.h"
27 #include "language/lexer/lexer.h"
28 #include "language/lexer/format-parser.h"
29 #include "libpspp/assertion.h"
30 #include "libpspp/compiler.h"
31 #include "libpspp/i18n.h"
32 #include "libpspp/string-map.h"
33 #include "output/driver.h"
34 #include "output/message-item.h"
35 #include "output/options.h"
36 #include "output/pivot-table.h"
37 #include "output/table-item.h"
40 #include "gl/progname.h"
41 #include "gl/xalloc.h"
42 #include "gl/xvasprintf.h"
44 /* --emphasis: Enable emphasis in ASCII driver? */
47 /* --box: ASCII driver box option. */
50 /* -o, --output: Base name for output files. */
51 static const char *output_base = "render";
53 static const char *parse_options (int argc, char **argv);
54 static void usage (void) NO_RETURN;
55 static struct pivot_table *read_table (struct lexer *);
56 static void output_msg (const struct msg *, void *);
59 main (int argc, char **argv)
61 const char *input_file_name;
63 set_program_name (argv[0]);
65 output_engine_push ();
66 input_file_name = parse_options (argc, argv);
70 struct lex_reader *reader = lex_reader_for_file (input_file_name, NULL,
76 struct lexer *lexer = lex_create ();
77 msg_set_handler (output_msg, lexer);
78 lex_include (lexer, reader);
83 while (lex_match (lexer, T_ENDCMD))
85 if (lex_match (lexer, T_STOP))
88 struct pivot_table *pt = read_table (lexer);
89 pivot_table_dump (pt, 0);
90 pivot_table_submit (pt);
100 static void PRINTF_FORMAT (2, 3)
101 register_driver (struct string_map *options,
102 const char *output_file, ...)
105 va_start (args, output_file);
106 string_map_insert_nocopy (options, xstrdup ("output-file"),
107 xvasprintf (output_file, args));
110 struct output_driver *driver = output_driver_create (options);
113 output_driver_register (driver);
117 configure_drivers (int width, int length, int min_break)
119 /* Render to stdout. */
120 struct string_map options = STRING_MAP_INITIALIZER (options);
121 string_map_insert (&options, "format", "txt");
122 string_map_insert_nocopy (&options, xstrdup ("width"),
123 xasprintf ("%d", width));
125 string_map_insert_nocopy (&options, xstrdup ("min-hbreak"),
126 xasprintf ("%d", min_break));
127 string_map_insert (&options, "emphasis", emphasis ? "true" : "false");
129 string_map_insert (&options, "box", box);
130 register_driver (&options, "-");
134 /* Render to <base>.pdf. */
135 string_map_insert (&options, "top-margin", "0");
136 string_map_insert (&options, "bottom-margin", "0");
137 string_map_insert (&options, "left-margin", "0");
138 string_map_insert (&options, "right-margin", "0");
139 string_map_insert_nocopy (&options, xstrdup ("paper-size"),
140 xasprintf ("%dx%dpt", width * 5, length * 8));
143 string_map_insert_nocopy (&options, xstrdup ("min-hbreak"),
144 xasprintf ("%d", min_break * 5));
145 string_map_insert_nocopy (&options, xstrdup ("min-vbreak"),
146 xasprintf ("%d", min_break * 8));
148 register_driver (&options, "%s.pdf", output_base);
151 register_driver (&options, "%s.txt", output_base);
152 register_driver (&options, "%s.csv", output_base);
153 register_driver (&options, "%s.odt", output_base);
154 register_driver (&options, "%s.spv", output_base);
156 string_map_destroy (&options);
160 parse_options (int argc, char **argv)
169 OPT_WIDTH = UCHAR_MAX + 1,
177 static const struct option options[] =
179 {"width", required_argument, NULL, OPT_WIDTH},
180 {"length", required_argument, NULL, OPT_LENGTH},
181 {"min-break", required_argument, NULL, OPT_MIN_BREAK},
182 {"emphasis", no_argument, NULL, OPT_EMPHASIS},
183 {"box", required_argument, NULL, OPT_BOX},
184 {"output", required_argument, NULL, 'o'},
185 {"table-look", required_argument, NULL, OPT_TABLE_LOOK},
186 {"help", no_argument, NULL, OPT_HELP},
190 int c = getopt_long (argc, argv, "o:", options, NULL);
197 width = atoi (optarg);
201 length = atoi (optarg);
205 min_break = atoi (optarg);
217 output_base = optarg;
222 struct pivot_table_look *look;
223 char *err = pivot_table_look_read (optarg, &look);
225 error (1, 0, "%s", err);
226 pivot_table_look_set_default (look);
227 pivot_table_look_unref (look);
247 configure_drivers (width, length, min_break);
249 if (optind + 1 != argc)
250 error (1, 0, "exactly one non-option argument required; "
251 "use --help for help");
258 printf ("%s, to test rendering of PSPP tables\n"
259 "usage: %s [OPTIONS] INPUT\n"
261 " --width=WIDTH set page width in characters\n"
262 " --length=LINE set page length in lines\n",
263 program_name, program_name);
268 parse_settings_value_show (struct lexer *lexer, const char *name,
269 enum settings_value_show *show)
271 if (lex_match_id (lexer, name))
273 if (!lex_force_match (lexer, T_EQUALS))
276 if (lex_match_id (lexer, "DEFAULT"))
277 *show = SETTINGS_VALUE_SHOW_DEFAULT;
278 else if (lex_match_id (lexer, "VALUE"))
279 *show = SETTINGS_VALUE_SHOW_VALUE;
280 else if (lex_match_id (lexer, "LABEL"))
281 *show = SETTINGS_VALUE_SHOW_LABEL;
282 else if (lex_match_id (lexer, "BOTH"))
283 *show = SETTINGS_VALUE_SHOW_BOTH;
286 lex_error_expecting (lexer, "DEFAULT", "VALUE", "LABEL", "BOTH");
297 parse_string_setting (struct lexer *lexer, const char *name, char **stringp)
299 if (lex_match_id (lexer, name))
301 lex_match (lexer, T_EQUALS);
302 if (!lex_force_string (lexer))
306 *stringp = xstrdup (lex_tokcstr (lexer));
316 read_value_option (struct lexer *lexer, struct pivot_value *value)
318 enum settings_value_show *show
319 = (value->type == PIVOT_VALUE_NUMERIC ? &value->numeric.show
320 : value->type == PIVOT_VALUE_STRING ? &value->string.show
321 : value->type == PIVOT_VALUE_VARIABLE ? &value->variable.show
323 if (show && parse_settings_value_show (lexer, "SHOW", show))
327 = (value->type == PIVOT_VALUE_NUMERIC ? &value->numeric.var_name
328 : value->type == PIVOT_VALUE_STRING ? &value->string.var_name
330 if (var_name && parse_string_setting (lexer, "VAR", var_name))
334 = (value->type == PIVOT_VALUE_NUMERIC ? &value->numeric.value_label
335 : value->type == PIVOT_VALUE_STRING ? &value->string.value_label
336 : value->type == PIVOT_VALUE_VARIABLE ? &value->variable.var_label
338 if (label && parse_string_setting (lexer, "LABEL", label))
341 if (value->type == PIVOT_VALUE_STRING && lex_match_id (lexer, "HEX"))
343 value->string.hex = true;
347 if (value->type == PIVOT_VALUE_NUMERIC)
351 bool ok = parse_format_specifier (lexer, &fmt);
356 if (!fmt_check_output (&fmt)
357 || !fmt_check_type_compat (&fmt, VAL_NUMERIC))
360 value->numeric.format = fmt;
365 if (lex_match_id (lexer, "SUBSCRIPTS"))
367 lex_match (lexer, T_EQUALS);
368 size_t allocated_subscripts = value->n_subscripts;
369 while (lex_token (lexer) == T_STRING)
371 if (value->n_subscripts >= allocated_subscripts)
372 value->subscripts = x2nrealloc (value->subscripts,
373 &allocated_subscripts,
374 sizeof *value->subscripts);
376 value->subscripts[value->n_subscripts++] = xstrdup (
377 lex_tokcstr (lexer));
383 lex_error (lexer, "Expecting valid value option");
387 static struct pivot_value *
388 read_value (struct lexer *lexer)
390 struct pivot_value *value;
391 if (lex_is_number (lexer))
393 value = pivot_value_new_number (lex_number (lexer));
396 else if (lex_is_string (lexer))
398 value = xmalloc (sizeof *value);
399 *value = (struct pivot_value) {
400 .type = PIVOT_VALUE_STRING,
401 .string = { .s = xstrdup (lex_tokcstr (lexer)) },
405 else if (lex_token (lexer) == T_ID)
407 value = xmalloc (sizeof *value);
408 *value = (struct pivot_value) {
409 .type = PIVOT_VALUE_VARIABLE,
410 .variable = { .var_name = xstrdup (lex_tokcstr (lexer)) },
416 msg (SE, "Expecting pivot_value");
420 while (lex_match (lexer, T_LBRACK))
422 read_value_option (lexer, value);
424 if (!lex_force_match (lexer, T_RBRACK))
432 read_group (struct lexer *lexer, struct pivot_table *pt,
433 struct pivot_category *group)
435 if (lex_match (lexer, T_ASTERISK))
436 group->show_label = true;
438 if (!lex_force_match (lexer, T_LPAREN))
441 if (lex_match (lexer, T_RPAREN))
446 struct pivot_value *name = read_value (lexer);
447 if (lex_token (lexer) == T_ASTERISK
448 || lex_token (lexer) == T_LPAREN)
449 read_group (lexer, pt, pivot_category_create_group__ (group, name));
453 if (lex_token (lexer) == T_ID
454 && is_pivot_result_class (lex_tokcstr (lexer)))
456 rc = xstrdup (lex_tokcstr (lexer));
462 pivot_category_create_leaf_rc (group, name, rc);
467 while (lex_match (lexer, T_COMMA));
468 if (!lex_force_match (lexer, T_RPAREN))
473 read_dimension (struct lexer *lexer, struct pivot_table *pt,
474 enum pivot_axis_type a)
476 lex_match (lexer, T_EQUALS);
478 struct pivot_value *name = read_value (lexer);
479 struct pivot_dimension *dim = pivot_dimension_create__ (pt, a, name);
480 read_group (lexer, pt, dim->root);
484 parse_bool_setting (struct lexer *lexer, const char *name,
485 const char *true_kw, const char *false_kw,
488 if (lex_match_id (lexer, name))
490 if (!lex_force_match (lexer, T_EQUALS))
493 if (lex_match_id (lexer, true_kw))
495 else if (lex_match_id (lexer, false_kw))
499 lex_error_expecting (lexer, true_kw, false_kw);
510 read_look (struct lexer *lexer, struct pivot_table *pt)
512 lex_match (lexer, T_EQUALS);
514 if (lex_is_string (lexer))
516 struct pivot_table_look *look;
517 char *error = pivot_table_look_read (lex_tokcstr (lexer), &look);
520 msg (SE, "%s", error);
525 pivot_table_set_look (pt, look);
526 pivot_table_look_unref (look);
529 struct pivot_table_look *look = pivot_table_look_unshare (
530 pivot_table_look_ref (pt->look));
533 if (!parse_bool_setting (lexer, "EMPTY", "HIDE", "SHOW",
535 && !parse_bool_setting (lexer, "ROWLABELS", "CORNER", "NESTED",
536 &look->row_labels_in_corner)
537 && !parse_bool_setting (lexer, "MARKERS", "NUMERIC", "ALPHA",
538 &look->show_numeric_markers)
539 && !parse_bool_setting (lexer, "LEVEL", "SUPER", "SUB",
540 &look->footnote_marker_superscripts)
541 && !parse_bool_setting (lexer, "LAYERS", "CURRENT", "ALL",
542 &look->print_all_layers)
543 && !parse_bool_setting (lexer, "PAGINATELAYERS", "YES", "NO",
544 &look->paginate_layers)
545 && !parse_bool_setting (lexer, "HSHRINK", "YES", "NO",
546 &look->shrink_to_fit[TABLE_HORZ])
547 && !parse_bool_setting (lexer, "VSHRINK", "YES", "NO",
548 &look->shrink_to_fit[TABLE_VERT])
549 && !parse_bool_setting (lexer, "TOPCONTINUATION", "YES", "NO",
550 &look->top_continuation)
551 && !parse_bool_setting (lexer, "BOTTOMCONTINUATION", "YES", "NO",
552 &look->bottom_continuation)
553 && !parse_string_setting (lexer, "CONTINUATION",
554 &look->continuation))
557 pivot_table_set_look (pt, look);
558 pivot_table_look_unref (look);
561 static enum table_stroke
562 read_stroke (struct lexer *lexer)
564 for (int stroke = 0; stroke < TABLE_N_STROKES; stroke++)
565 if (lex_match_id (lexer, table_stroke_to_string (stroke)))
568 lex_error (lexer, "expecting stroke");
572 static struct cell_color
573 read_color (struct lexer *lexer)
575 struct cell_color color;
576 if (!parse_color__ (lex_tokcstr (lexer), &color))
578 msg (SE, "%s: unknown color", lex_tokcstr (lexer));
586 parse_value_setting (struct lexer *lexer, const char *name,
587 struct pivot_value **valuep)
589 if (lex_match_id (lexer, name))
591 lex_match (lexer, T_EQUALS);
593 pivot_value_destroy (*valuep);
594 *valuep = read_value (lexer);
603 read_border (struct lexer *lexer, struct pivot_table *pt)
605 static const char *const pivot_border_ids[PIVOT_N_BORDERS] = {
606 [PIVOT_BORDER_TITLE] = "title",
607 [PIVOT_BORDER_OUTER_LEFT] = "outer-left",
608 [PIVOT_BORDER_OUTER_TOP] = "outer-top",
609 [PIVOT_BORDER_OUTER_RIGHT] = "outer-right",
610 [PIVOT_BORDER_OUTER_BOTTOM] = "outer-bottom",
611 [PIVOT_BORDER_INNER_LEFT] = "inner-left",
612 [PIVOT_BORDER_INNER_TOP] = "inner-top",
613 [PIVOT_BORDER_INNER_RIGHT] = "inner-right",
614 [PIVOT_BORDER_INNER_BOTTOM] = "inner-bottom",
615 [PIVOT_BORDER_DATA_LEFT] = "data-left",
616 [PIVOT_BORDER_DATA_TOP] = "data-top",
617 [PIVOT_BORDER_DIM_ROW_HORZ] = "dim-row-horz",
618 [PIVOT_BORDER_DIM_ROW_VERT] = "dim-row-vert",
619 [PIVOT_BORDER_DIM_COL_HORZ] = "dim-col-horz",
620 [PIVOT_BORDER_DIM_COL_VERT] = "dim-col-vert",
621 [PIVOT_BORDER_CAT_ROW_HORZ] = "cat-row-horz",
622 [PIVOT_BORDER_CAT_ROW_VERT] = "cat-row-vert",
623 [PIVOT_BORDER_CAT_COL_HORZ] = "cat-col-horz",
624 [PIVOT_BORDER_CAT_COL_VERT] = "cat-col-vert",
627 lex_match (lexer, T_EQUALS);
629 struct pivot_table_look *look = pivot_table_look_unshare (
630 pivot_table_look_ref (pt->look));
631 while (lex_token (lexer) == T_STRING)
633 char *s = xstrdup (lex_tokcstr (lexer));
635 if (!lex_force_match (lexer, T_LPAREN))
638 struct table_border_style style = TABLE_BORDER_STYLE_INITIALIZER;
639 style.stroke = read_stroke (lexer);
640 if (lex_is_string (lexer))
641 style.color = read_color (lexer);
642 if (!lex_force_match (lexer, T_RPAREN))
646 for (int b = 0; b < PIVOT_N_BORDERS; b++)
648 if (!strncmp (s, pivot_border_ids[b], strlen (s)))
650 look->borders[b] = style;
656 msg (SE, "%s: no matching borders", s);
661 pivot_table_set_look (pt, look);
662 pivot_table_look_unref (look);
665 static struct pivot_table *
666 read_table (struct lexer *lexer)
668 struct pivot_table *pt = pivot_table_create__ (NULL, NULL);
669 while (lex_match (lexer, T_SLASH))
671 assert (!pivot_table_is_shared (pt));
673 if (lex_match_id (lexer, "ROW"))
674 read_dimension (lexer, pt, PIVOT_AXIS_ROW);
675 else if (lex_match_id (lexer, "COLUMN"))
676 read_dimension (lexer, pt, PIVOT_AXIS_COLUMN);
677 else if (lex_match_id (lexer, "LAYER"))
678 read_dimension (lexer, pt, PIVOT_AXIS_LAYER);
679 else if (lex_match_id (lexer, "LOOK"))
680 read_look (lexer, pt);
681 else if (lex_match_id (lexer, "ROTATE"))
683 lex_match (lexer, T_EQUALS);
684 while (lex_token (lexer) == T_ID)
685 if (!parse_bool_setting (lexer, "INNERCOLUMNS", "YES", "NO",
686 &pt->rotate_inner_column_labels)
687 && !parse_bool_setting (lexer, "OUTERROWS", "YES", "NO",
688 &pt->rotate_outer_row_labels))
691 else if (lex_match_id (lexer, "DISPLAY"))
693 lex_match (lexer, T_EQUALS);
694 while (lex_token (lexer) == T_ID)
696 if (parse_bool_setting (lexer, "GRID", "YES", "NO",
697 &pt->show_grid_lines)
698 || parse_bool_setting (lexer, "CAPTION", "YES", "NO",
700 || parse_bool_setting (lexer, "TITLE", "YES", "NO",
704 if (parse_settings_value_show (lexer, "VALUES", &pt->show_values)
705 || parse_settings_value_show (lexer, "VARIABLES",
706 &pt->show_variables))
712 else if (parse_value_setting (lexer, "TITLE", &pt->title)
713 || parse_value_setting (lexer, "SUBTYPE", &pt->subtype)
714 || parse_value_setting (lexer, "CORNER", &pt->corner_text)
715 || parse_value_setting (lexer, "CAPTION", &pt->caption)
716 || parse_string_setting (lexer, "NOTES", &pt->notes))
720 else if (lex_match_id (lexer, "BORDER"))
721 read_border (lexer, pt);
724 msg (SE, "Expecting keyword");
729 size_t *dindexes = xcalloc (pt->n_dimensions, sizeof *dindexes);
730 for (size_t i = 0; ; i++)
732 pivot_table_put (pt, dindexes, pt->n_dimensions,
733 pivot_value_new_integer (i));
735 for (size_t j = 0; j < pt->n_dimensions; j++)
737 if (++dindexes[j] < pt->dimensions[j]->n_leaves)
746 if (!lex_force_match (lexer, T_ENDCMD))
752 output_msg (const struct msg *m_, void *lexer_)
754 struct lexer *lexer = lexer_;
757 if (m.file_name == NULL)
759 m.file_name = CONST_CAST (char *, lex_get_file_name (lexer));
760 m.first_line = lex_get_first_line_number (lexer, 0);
761 m.last_line = lex_get_last_line_number (lexer, 0);
764 m.command_name = output_get_command_name ();
766 message_item_submit (message_item_create (&m));
768 free (m.command_name);