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/>. */
27 #include "data/file-handle-def.h"
28 #include "language/lexer/lexer.h"
29 #include "language/lexer/format-parser.h"
30 #include "libpspp/assertion.h"
31 #include "libpspp/compiler.h"
32 #include "libpspp/i18n.h"
33 #include "libpspp/string-map.h"
34 #include "output/driver.h"
35 #include "output/options.h"
36 #include "output/output-item.h"
37 #include "output/pivot-table.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 void read_table (struct lexer *);
56 static void output_msg (const struct msg *, struct lexer *);
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 lex_set_message_handler (lexer, output_msg);
78 lex_include (lexer, reader);
83 while (lex_match (lexer, T_ENDCMD))
85 if (lex_match (lexer, T_STOP))
98 static void PRINTF_FORMAT (2, 3)
99 register_driver (struct string_map *options,
100 const char *output_file, ...)
103 va_start (args, output_file);
104 string_map_insert_nocopy (options, xstrdup ("output-file"),
105 xvasprintf (output_file, args));
108 struct output_driver *driver = output_driver_create (options);
111 output_driver_register (driver);
115 configure_drivers (int width, int length UNUSED, int min_break)
117 /* Render to stdout. */
118 struct string_map options = STRING_MAP_INITIALIZER (options);
119 string_map_insert (&options, "format", "txt");
120 string_map_insert_nocopy (&options, xstrdup ("width"),
121 xasprintf ("%d", width));
123 string_map_insert_nocopy (&options, xstrdup ("min-hbreak"),
124 xasprintf ("%d", min_break));
125 string_map_insert (&options, "emphasis", emphasis ? "true" : "false");
127 string_map_insert (&options, "box", box);
128 register_driver (&options, "-");
131 /* Render to <base>.pdf. */
132 string_map_insert (&options, "top-margin", "0");
133 string_map_insert (&options, "bottom-margin", "0");
134 string_map_insert (&options, "left-margin", "0");
135 string_map_insert (&options, "right-margin", "0");
136 string_map_insert (&options, "paper-size", "99x99in");
137 string_map_insert (&options, "trim", "true");
138 register_driver (&options, "%s.pdf", output_base);
140 string_map_insert (&options, "box", "unicode");
141 register_driver (&options, "%s.txt", output_base);
143 string_map_insert (&options, "box", "ascii");
144 register_driver (&options, "%s-ascii.txt", output_base);
146 register_driver (&options, "%s.csv", output_base);
147 register_driver (&options, "%s.odt", output_base);
148 register_driver (&options, "%s.spv", output_base);
149 register_driver (&options, "%s.html", output_base);
150 register_driver (&options, "%s.tex", output_base);
152 string_map_destroy (&options);
156 parse_options (int argc, char **argv)
165 OPT_WIDTH = UCHAR_MAX + 1,
173 static const struct option options[] =
175 {"width", required_argument, NULL, OPT_WIDTH},
176 {"length", required_argument, NULL, OPT_LENGTH},
177 {"min-break", required_argument, NULL, OPT_MIN_BREAK},
178 {"emphasis", no_argument, NULL, OPT_EMPHASIS},
179 {"box", required_argument, NULL, OPT_BOX},
180 {"output", required_argument, NULL, 'o'},
181 {"table-look", required_argument, NULL, OPT_TABLE_LOOK},
182 {"help", no_argument, NULL, OPT_HELP},
186 int c = getopt_long (argc, argv, "o:", options, NULL);
193 width = atoi (optarg);
197 length = atoi (optarg);
201 min_break = atoi (optarg);
213 output_base = optarg;
218 struct pivot_table_look *look;
219 char *err = pivot_table_look_read (optarg, &look);
221 error (1, 0, "%s", err);
222 pivot_table_look_set_default (look);
223 pivot_table_look_unref (look);
243 configure_drivers (width, length, min_break);
245 if (optind + 1 != argc)
246 error (1, 0, "exactly one non-option argument required; "
247 "use --help for help");
254 printf ("%s, to test rendering of PSPP tables\n"
255 "usage: %s [OPTIONS] INPUT\n"
257 " --width=WIDTH set page width in characters\n"
258 " --length=LINE set page length in lines\n",
259 program_name, program_name);
264 force_match (struct lexer *lexer, enum token_type type)
266 if (!lex_force_match (lexer, type))
271 force_string (struct lexer *lexer)
273 if (!lex_force_string (lexer))
278 force_int (struct lexer *lexer)
280 if (!lex_force_int (lexer))
285 force_num (struct lexer *lexer)
287 if (!lex_force_num (lexer))
292 parse_settings_value_show (struct lexer *lexer, const char *name,
293 enum settings_value_show *show)
295 if (lex_match_id (lexer, name))
297 lex_match (lexer, T_EQUALS);
299 if (lex_match_id (lexer, "DEFAULT"))
300 *show = SETTINGS_VALUE_SHOW_DEFAULT;
301 else if (lex_match_id (lexer, "VALUE"))
302 *show = SETTINGS_VALUE_SHOW_VALUE;
303 else if (lex_match_id (lexer, "LABEL"))
304 *show = SETTINGS_VALUE_SHOW_LABEL;
305 else if (lex_match_id (lexer, "BOTH"))
306 *show = SETTINGS_VALUE_SHOW_BOTH;
309 lex_error_expecting (lexer, "DEFAULT", "VALUE", "LABEL", "BOTH");
320 parse_string_setting (struct lexer *lexer, const char *name, char **stringp)
322 if (lex_match_id (lexer, name))
324 lex_match (lexer, T_EQUALS);
325 force_string (lexer);
328 *stringp = xstrdup (lex_tokcstr (lexer));
338 match_kw (struct lexer *lexer, const char *kw)
340 return (!strcmp (kw, "ALL")
341 ? lex_match (lexer, T_ALL)
342 : lex_match_id (lexer, kw));
346 parse_bool_setting_with_default (struct lexer *lexer, const char *name,
347 const char *true_kw, const char *false_kw,
348 int default_value, bool *out)
350 if (lex_match_id (lexer, name))
352 if (default_value >= 0)
354 if (!lex_match (lexer, T_EQUALS))
355 *out = default_value;
359 force_match (lexer, T_EQUALS);
361 if (match_kw (lexer, true_kw))
363 else if (match_kw (lexer, false_kw))
367 lex_error_expecting (lexer, true_kw, false_kw);
378 parse_bool_setting (struct lexer *lexer, const char *name,
379 const char *true_kw, const char *false_kw,
382 return parse_bool_setting_with_default (lexer, name, true_kw, false_kw, -1,
387 parse_yesno_setting (struct lexer *lexer, const char *name, bool *out)
389 return parse_bool_setting_with_default (lexer, name, "YES", "NO", true, out);
392 static struct cell_color
393 read_color (struct lexer *lexer)
395 struct cell_color color;
396 if (!parse_color__ (lex_tokcstr (lexer), &color))
398 msg (SE, "%s: unknown color", lex_tokcstr (lexer));
406 parse_color_pair_setting (struct lexer *lexer, const char *name,
407 struct cell_color out[2])
409 if (lex_match_id (lexer, name))
411 lex_match (lexer, T_EQUALS);
412 out[0] = read_color (lexer);
413 out[1] = lex_is_string (lexer) ? read_color (lexer) : out[0];
421 parse_int_setting (struct lexer *lexer, const char *name, int *out)
423 if (lex_match_id (lexer, name))
425 lex_match (lexer, T_EQUALS);
427 *out = lex_integer (lexer);
436 read_font_style (struct lexer *lexer, struct font_style *fs)
438 while (parse_yesno_setting (lexer, "BOLD", &fs->bold)
439 || parse_yesno_setting (lexer, "ITALIC", &fs->italic)
440 || parse_yesno_setting (lexer, "UNDERLINE", &fs->underline)
441 || parse_yesno_setting (lexer, "MARKUP", &fs->markup)
442 || parse_color_pair_setting (lexer, "FG", fs->fg)
443 || parse_color_pair_setting (lexer, "BG", fs->bg)
444 || parse_string_setting (lexer, "FACE", &fs->typeface)
445 || parse_int_setting (lexer, "SIZE", &fs->size))
450 parse_halign_setting (struct lexer *lexer, enum table_halign *halign,
451 double *decimal_offset)
453 if (lex_match_id (lexer, "RIGHT"))
454 *halign = TABLE_HALIGN_RIGHT;
455 else if (lex_match_id (lexer, "LEFT"))
456 *halign = TABLE_HALIGN_LEFT;
457 else if (lex_match_id (lexer, "CELL"))
458 *halign = TABLE_HALIGN_CENTER;
459 else if (lex_match_id (lexer, "MIXED"))
460 *halign = TABLE_HALIGN_MIXED;
461 else if (lex_match_id (lexer, "DECIMAL"))
463 if (lex_is_number (lexer))
465 *decimal_offset = lex_number (lexer);
476 parse_valign_setting (struct lexer *lexer, enum table_valign *valign)
478 if (lex_match_id (lexer, "TOP"))
479 *valign = TABLE_VALIGN_TOP;
480 else if (lex_match_id (lexer, "MIDDLE"))
481 *valign = TABLE_VALIGN_CENTER;
482 else if (lex_match_id (lexer, "BOTTOM"))
483 *valign = TABLE_VALIGN_BOTTOM;
491 parse_margin_setting (struct lexer *lexer, int margin[TABLE_N_AXES][2])
493 if (lex_match_id (lexer, "MARGINS"))
498 lex_match (lexer, T_EQUALS);
500 while (lex_is_number (lexer) && n < 4)
502 values[n++] = lex_number (lexer);
508 margin[TABLE_HORZ][0] = margin[TABLE_HORZ][1] = values[0];
509 margin[TABLE_VERT][0] = margin[TABLE_VERT][1] = values[0];
513 margin[TABLE_HORZ][0] = margin[TABLE_HORZ][1] = values[1];
514 margin[TABLE_VERT][0] = margin[TABLE_VERT][1] = values[0];
518 margin[TABLE_VERT][0] = values[0];
519 margin[TABLE_HORZ][0] = margin[TABLE_HORZ][1] = values[1];
520 margin[TABLE_VERT][1] = values[2];
525 margin[TABLE_VERT][0] = values[0];
526 margin[TABLE_HORZ][1] = values[1];
527 margin[TABLE_VERT][1] = values[2];
528 margin[TABLE_HORZ][0] = values[3];
538 read_cell_style (struct lexer *lexer, struct cell_style *cs)
540 while (parse_halign_setting (lexer, &cs->halign, &cs->decimal_offset)
541 || parse_valign_setting (lexer, &cs->valign)
542 || parse_margin_setting (lexer, cs->margin))
547 read_value_option (struct lexer *lexer, const struct pivot_table *pt,
548 struct pivot_value *value,
549 const struct table_area_style *base_style)
551 enum settings_value_show *show
552 = (value->type == PIVOT_VALUE_NUMERIC ? &value->numeric.show
553 : value->type == PIVOT_VALUE_STRING ? &value->string.show
554 : value->type == PIVOT_VALUE_VARIABLE ? &value->variable.show
556 if (show && parse_settings_value_show (lexer, "SHOW", show))
560 = (value->type == PIVOT_VALUE_NUMERIC ? &value->numeric.var_name
561 : value->type == PIVOT_VALUE_STRING ? &value->string.var_name
563 if (var_name && parse_string_setting (lexer, "VAR", var_name))
567 = (value->type == PIVOT_VALUE_NUMERIC ? &value->numeric.value_label
568 : value->type == PIVOT_VALUE_STRING ? &value->string.value_label
569 : value->type == PIVOT_VALUE_VARIABLE ? &value->variable.var_label
571 if (label && parse_string_setting (lexer, "LABEL", label))
574 if (value->type == PIVOT_VALUE_STRING && lex_match_id (lexer, "HEX"))
576 value->string.hex = true;
580 if (value->type == PIVOT_VALUE_NUMERIC)
584 bool ok = parse_format_specifier (lexer, &fmt);
589 if (!fmt_check_output (&fmt)
590 || !fmt_check_type_compat (&fmt, VAL_NUMERIC))
593 value->numeric.format = fmt;
598 if (lex_match_id (lexer, "SUBSCRIPTS"))
600 lex_match (lexer, T_EQUALS);
602 struct pivot_value_ex *ex = pivot_value_ex_rw (value);
603 size_t allocated_subscripts = ex->n_subscripts;
604 while (lex_token (lexer) == T_STRING)
606 if (ex->n_subscripts >= allocated_subscripts)
607 ex->subscripts = x2nrealloc (ex->subscripts, &allocated_subscripts,
608 sizeof *ex->subscripts);
610 ex->subscripts[ex->n_subscripts++] = xstrdup (lex_tokcstr (lexer));
616 if (lex_match_id (lexer, "FONT") && base_style)
618 lex_match (lexer, T_EQUALS);
620 struct pivot_value_ex *ex = pivot_value_ex_rw (value);
623 ex->font_style = xmalloc (sizeof *ex->font_style);
624 font_style_copy (NULL, ex->font_style, &base_style->font_style);
626 read_font_style (lexer, ex->font_style);
630 if (lex_match_id (lexer, "CELL") && base_style)
632 lex_match (lexer, T_EQUALS);
634 struct pivot_value_ex *ex = pivot_value_ex_rw (value);
637 ex->cell_style = xmalloc (sizeof *ex->cell_style);
638 *ex->cell_style = base_style->cell_style;
640 read_cell_style (lexer, ex->cell_style);
644 if (lex_match_id (lexer, "FOOTNOTE"))
646 lex_match (lexer, T_EQUALS);
648 while (lex_is_integer (lexer))
650 size_t idx = lex_integer (lexer);
653 if (idx >= pt->n_footnotes)
655 msg (SE, "Footnote %zu not available "
656 "(only %zu footnotes defined)", idx, pt->n_footnotes);
659 pivot_value_add_footnote (value, pt->footnotes[idx]);
664 lex_error (lexer, "Expecting valid value option");
668 static struct pivot_value *
669 read_value (struct lexer *lexer, const struct pivot_table *pt,
670 const struct table_area_style *base_style)
672 struct pivot_value *value;
673 if (lex_is_number (lexer))
675 value = pivot_value_new_number (lex_number (lexer));
678 else if (lex_is_string (lexer))
680 value = xmalloc (sizeof *value);
681 *value = (struct pivot_value) {
683 .type = PIVOT_VALUE_STRING,
684 .s = xstrdup (lex_tokcstr (lexer))
689 else if (lex_token (lexer) == T_ID)
691 value = xmalloc (sizeof *value);
692 *value = (struct pivot_value) {
694 .type = PIVOT_VALUE_VARIABLE,
695 .var_name = xstrdup (lex_tokcstr (lexer))
702 msg (SE, "Expecting pivot_value");
706 while (lex_match (lexer, T_LBRACK))
708 read_value_option (lexer, pt, value, base_style);
709 force_match (lexer, T_RBRACK);
716 read_group (struct lexer *lexer, struct pivot_table *pt,
717 struct pivot_category *group,
718 const struct table_area_style *label_style)
720 if (lex_match (lexer, T_ASTERISK))
721 group->show_label = true;
723 force_match (lexer, T_LPAREN);
724 if (lex_match (lexer, T_RPAREN))
729 struct pivot_value *name = read_value (lexer, pt, label_style);
730 if (lex_token (lexer) == T_ASTERISK
731 || lex_token (lexer) == T_LPAREN)
732 read_group (lexer, pt, pivot_category_create_group__ (group, name),
737 if (lex_token (lexer) == T_ID
738 && is_pivot_result_class (lex_tokcstr (lexer)))
740 rc = xstrdup (lex_tokcstr (lexer));
746 pivot_category_create_leaf_rc (group, name, rc);
751 while (lex_match (lexer, T_COMMA));
752 force_match (lexer, T_RPAREN);
756 read_dimension (struct lexer *lexer, struct pivot_table *pt,
757 enum pivot_axis_type a,
758 const struct table_area_style *label_style)
760 if (!pivot_table_is_empty (pt))
761 error (1, 0, "can't add dimensions after adding data");
763 lex_match (lexer, T_EQUALS);
765 struct pivot_value *name = read_value (lexer, pt, label_style);
766 struct pivot_dimension *dim = pivot_dimension_create__ (pt, a, name);
767 read_group (lexer, pt, dim->root, label_style);
771 read_look (struct lexer *lexer, struct pivot_table *pt)
773 lex_match (lexer, T_EQUALS);
775 if (lex_is_string (lexer))
777 struct pivot_table_look *look;
778 char *error = pivot_table_look_read (lex_tokcstr (lexer), &look);
781 msg (SE, "%s", error);
786 pivot_table_set_look (pt, look);
787 pivot_table_look_unref (look);
790 struct pivot_table_look *look = pivot_table_look_unshare (
791 pivot_table_look_ref (pt->look));
794 if (!parse_bool_setting (lexer, "EMPTY", "HIDE", "SHOW",
796 && !parse_bool_setting (lexer, "ROWLABELS", "CORNER", "NESTED",
797 &look->row_labels_in_corner)
798 && !parse_bool_setting (lexer, "MARKERS", "NUMERIC", "ALPHA",
799 &look->show_numeric_markers)
800 && !parse_bool_setting (lexer, "LEVEL", "SUPER", "SUB",
801 &look->footnote_marker_superscripts)
802 && !parse_bool_setting (lexer, "LAYERS", "ALL", "CURRENT",
803 &look->print_all_layers)
804 && !parse_bool_setting (lexer, "PAGINATELAYERS", "YES", "NO",
805 &look->paginate_layers)
806 && !parse_bool_setting (lexer, "HSHRINK", "YES", "NO",
807 &look->shrink_to_fit[TABLE_HORZ])
808 && !parse_bool_setting (lexer, "VSHRINK", "YES", "NO",
809 &look->shrink_to_fit[TABLE_VERT])
810 && !parse_bool_setting (lexer, "TOPCONTINUATION", "YES", "NO",
811 &look->top_continuation)
812 && !parse_bool_setting (lexer, "BOTTOMCONTINUATION", "YES", "NO",
813 &look->bottom_continuation)
814 && !parse_string_setting (lexer, "CONTINUATION",
815 &look->continuation))
818 pivot_table_set_look (pt, look);
819 pivot_table_look_unref (look);
822 static enum table_stroke
823 read_stroke (struct lexer *lexer)
825 for (int stroke = 0; stroke < TABLE_N_STROKES; stroke++)
826 if (lex_match_id (lexer, table_stroke_to_string (stroke)))
829 lex_error (lexer, "expecting stroke");
834 parse_value_setting (struct lexer *lexer, const struct pivot_table *pt,
836 struct pivot_value **valuep,
837 struct table_area_style *base_style)
839 if (lex_match_id (lexer, name))
841 lex_match (lexer, T_EQUALS);
843 pivot_value_destroy (*valuep);
844 *valuep = read_value (lexer, pt, base_style);
853 read_border (struct lexer *lexer, struct pivot_table *pt)
855 static const char *const pivot_border_ids[PIVOT_N_BORDERS] = {
856 [PIVOT_BORDER_TITLE] = "title",
857 [PIVOT_BORDER_OUTER_LEFT] = "outer-left",
858 [PIVOT_BORDER_OUTER_TOP] = "outer-top",
859 [PIVOT_BORDER_OUTER_RIGHT] = "outer-right",
860 [PIVOT_BORDER_OUTER_BOTTOM] = "outer-bottom",
861 [PIVOT_BORDER_INNER_LEFT] = "inner-left",
862 [PIVOT_BORDER_INNER_TOP] = "inner-top",
863 [PIVOT_BORDER_INNER_RIGHT] = "inner-right",
864 [PIVOT_BORDER_INNER_BOTTOM] = "inner-bottom",
865 [PIVOT_BORDER_DATA_LEFT] = "data-left",
866 [PIVOT_BORDER_DATA_TOP] = "data-top",
867 [PIVOT_BORDER_DIM_ROW_HORZ] = "dim-row-horz",
868 [PIVOT_BORDER_DIM_ROW_VERT] = "dim-row-vert",
869 [PIVOT_BORDER_DIM_COL_HORZ] = "dim-col-horz",
870 [PIVOT_BORDER_DIM_COL_VERT] = "dim-col-vert",
871 [PIVOT_BORDER_CAT_ROW_HORZ] = "cat-row-horz",
872 [PIVOT_BORDER_CAT_ROW_VERT] = "cat-row-vert",
873 [PIVOT_BORDER_CAT_COL_HORZ] = "cat-col-horz",
874 [PIVOT_BORDER_CAT_COL_VERT] = "cat-col-vert",
877 lex_match (lexer, T_EQUALS);
879 struct pivot_table_look *look = pivot_table_look_unshare (
880 pivot_table_look_ref (pt->look));
881 while (lex_token (lexer) == T_STRING)
883 char *s = xstrdup (lex_tokcstr (lexer));
885 force_match (lexer, T_LPAREN);
887 struct table_border_style style = TABLE_BORDER_STYLE_INITIALIZER;
888 style.stroke = read_stroke (lexer);
889 if (lex_is_string (lexer))
890 style.color = read_color (lexer);
891 force_match (lexer, T_RPAREN);
894 for (int b = 0; b < PIVOT_N_BORDERS; b++)
896 if (!fnmatch (s, pivot_border_ids[b], 0))
898 look->borders[b] = style;
904 msg (SE, "%s: no matching borders", s);
909 pivot_table_set_look (pt, look);
910 pivot_table_look_unref (look);
914 read_footnote (struct lexer *lexer, struct pivot_table *pt)
917 if (lex_match (lexer, T_LBRACK))
921 idx = lex_integer (lexer);
924 force_match (lexer, T_RBRACK);
927 idx = pt->n_footnotes;
928 lex_match (lexer, T_EQUALS);
930 struct pivot_value *content
931 = read_value (lexer, pt, &pt->look->areas[PIVOT_AREA_FOOTER]);
933 struct pivot_value *marker;
934 if (lex_match_id (lexer, "MARKER"))
936 lex_match (lexer, T_EQUALS);
937 marker = read_value (lexer, pt, &pt->look->areas[PIVOT_AREA_FOOTER]);
942 bool show = !lex_match_id (lexer, "HIDE");
943 pivot_table_create_footnote__ (pt, idx, marker, content)->show = show;
947 read_cell (struct lexer *lexer, struct pivot_table *pt)
949 force_match (lexer, T_LBRACK);
951 size_t *lo = xnmalloc (pt->n_dimensions, sizeof *lo);
952 size_t *hi = xnmalloc (pt->n_dimensions, sizeof *hi);
953 for (size_t i = 0; i < pt->n_dimensions; i++)
955 const struct pivot_dimension *d = pt->dimensions[i];
958 force_match (lexer, T_COMMA);
962 msg (SE, "can't define data because dimension %zu has no categories",
967 if (lex_match (lexer, T_ALL))
970 hi[i] = d->n_leaves - 1;
975 lo[i] = hi[i] = lex_integer (lexer);
978 if (lex_match_id (lexer, "THRU"))
981 hi[i] = lex_integer (lexer);
987 msg (SE, "%zu THRU %zu is not a valid range", lo[i], hi[i]);
990 if (hi[i] >= d->n_leaves)
992 msg (SE, "dimension %zu (%s) has only %zu categories",
993 i, pivot_value_to_string (d->root->name, pt),
999 force_match (lexer, T_RBRACK);
1001 struct pivot_value *value = NULL;
1002 bool delete = false;
1003 if (lex_match (lexer, T_EQUALS))
1005 if (lex_match_id (lexer, "DELETE"))
1008 value = read_value (lexer, pt, &pt->look->areas[PIVOT_AREA_DATA]);
1011 size_t *dindexes = xmemdup (lo, pt->n_dimensions * sizeof *lo);
1012 for (size_t i = 0; ; i++)
1015 pivot_table_delete (pt, dindexes);
1017 pivot_table_put (pt, dindexes, pt->n_dimensions,
1019 ? pivot_value_clone (value)
1020 : pivot_value_new_integer (i)));
1022 for (size_t j = 0; j < pt->n_dimensions; j++)
1024 if (++dindexes[j] <= hi[j])
1026 dindexes[j] = lo[j];
1033 pivot_value_destroy (value);
1039 static struct pivot_dimension *
1040 parse_dim_name (struct lexer *lexer, struct pivot_table *table)
1042 force_string (lexer);
1043 for (size_t i = 0; i < table->n_dimensions; i++)
1045 struct pivot_dimension *dim = table->dimensions[i];
1047 struct string s = DS_EMPTY_INITIALIZER;
1048 pivot_value_format_body (dim->root->name, table, &s);
1049 bool match = !strcmp (ds_cstr (&s), lex_tokcstr (lexer));
1059 lex_error (lexer, "unknown dimension");
1063 static enum pivot_axis_type
1064 parse_axis_type (struct lexer *lexer)
1066 if (lex_match_id (lexer, "ROW"))
1067 return PIVOT_AXIS_ROW;
1068 else if (lex_match_id (lexer, "COLUMN"))
1069 return PIVOT_AXIS_COLUMN;
1070 else if (lex_match_id (lexer, "LAYER"))
1071 return PIVOT_AXIS_LAYER;
1074 lex_error_expecting (lexer, "ROW", "COLUMN", "LAYER");
1080 move_dimension (struct lexer *lexer, struct pivot_table *table)
1082 struct pivot_dimension *dim = parse_dim_name (lexer, table);
1084 enum pivot_axis_type axis = parse_axis_type (lexer);
1087 if (lex_is_integer (lexer))
1089 position = lex_integer (lexer);
1095 pivot_table_move_dimension (table, dim, axis, position);
1099 swap_axes (struct lexer *lexer, struct pivot_table *table)
1101 enum pivot_axis_type a = parse_axis_type (lexer);
1102 enum pivot_axis_type b = parse_axis_type (lexer);
1103 pivot_table_swap_axes (table, a, b);
1107 read_current_layer (struct lexer *lexer, struct pivot_table *table)
1109 lex_match (lexer, T_EQUALS);
1111 const struct pivot_axis *layer_axis = &table->axes[PIVOT_AXIS_LAYER];
1112 for (size_t i = 0; i < layer_axis->n_dimensions; i++)
1114 const struct pivot_dimension *dim = layer_axis->dimensions[i];
1117 size_t index = lex_integer (lexer);
1118 if (index >= dim->n_leaves)
1120 lex_error (lexer, "only %zu dimensions", dim->n_leaves);
1125 table->current_layer[i] = index;
1130 read_table (struct lexer *lexer)
1132 bool displayed = false;
1134 struct pivot_table *pt = pivot_table_create ("Default Title");
1135 while (lex_match (lexer, T_SLASH))
1137 assert (!pivot_table_is_shared (pt));
1140 if (lex_match_id (lexer, "ROW"))
1141 read_dimension (lexer, pt, PIVOT_AXIS_ROW,
1142 &pt->look->areas[PIVOT_AREA_ROW_LABELS]);
1143 else if (lex_match_id (lexer, "COLUMN"))
1144 read_dimension (lexer, pt, PIVOT_AXIS_COLUMN,
1145 &pt->look->areas[PIVOT_AREA_COLUMN_LABELS]);
1146 else if (lex_match_id (lexer, "LAYER"))
1147 read_dimension (lexer, pt, PIVOT_AXIS_LAYER,
1148 &pt->look->areas[PIVOT_AREA_LAYERS]);
1149 else if (lex_match_id (lexer, "LOOK"))
1150 read_look (lexer, pt);
1151 else if (lex_match_id (lexer, "ROTATE"))
1153 lex_match (lexer, T_EQUALS);
1154 while (lex_token (lexer) == T_ID)
1155 if (!parse_bool_setting (lexer, "INNERCOLUMNS", "YES", "NO",
1156 &pt->rotate_inner_column_labels)
1157 && !parse_bool_setting (lexer, "OUTERROWS", "YES", "NO",
1158 &pt->rotate_outer_row_labels))
1161 else if (lex_match_id (lexer, "SHOW"))
1163 lex_match (lexer, T_EQUALS);
1164 while (lex_token (lexer) == T_ID)
1166 if (parse_bool_setting (lexer, "GRID", "YES", "NO",
1167 &pt->show_grid_lines)
1168 || parse_bool_setting (lexer, "CAPTION", "YES", "NO",
1170 || parse_bool_setting (lexer, "TITLE", "YES", "NO",
1174 if (parse_settings_value_show (lexer, "VALUES", &pt->show_values)
1175 || parse_settings_value_show (lexer, "VARIABLES",
1176 &pt->show_variables))
1179 if (lex_match_id (lexer, "LAYER"))
1180 read_current_layer (lexer, pt);
1185 else if (parse_value_setting (lexer, pt, "TITLE", &pt->title,
1186 &pt->look->areas[PIVOT_AREA_TITLE])
1187 || parse_value_setting (lexer, pt, "SUBTYPE", &pt->subtype,
1189 || parse_value_setting (lexer, pt, "CORNER", &pt->corner_text,
1190 &pt->look->areas[PIVOT_AREA_CORNER])
1191 || parse_value_setting (lexer, pt, "CAPTION", &pt->caption,
1192 &pt->look->areas[PIVOT_AREA_CAPTION])
1193 || parse_string_setting (lexer, "NOTES", &pt->notes))
1197 else if (lex_match_id (lexer, "BORDER"))
1198 read_border (lexer, pt);
1199 else if (lex_match_id (lexer, "TRANSPOSE"))
1200 pivot_table_transpose (pt);
1201 else if (lex_match_id (lexer, "SWAP"))
1202 swap_axes (lexer, pt);
1203 else if (lex_match_id (lexer, "MOVE"))
1204 move_dimension (lexer, pt);
1205 else if (lex_match_id (lexer, "CELLS"))
1206 read_cell (lexer, pt);
1207 else if (lex_match_id (lexer, "FOOTNOTE"))
1208 read_footnote (lexer, pt);
1209 else if (lex_match_id (lexer, "DUMP"))
1210 pivot_table_dump (pt, 0);
1211 else if (lex_match_id (lexer, "DISPLAY"))
1213 pivot_table_submit (pivot_table_ref (pt));
1214 pt = pivot_table_unshare (pt);
1219 msg (SE, "Expecting keyword");
1225 pivot_table_submit (pt);
1227 pivot_table_unref (pt);
1229 force_match (lexer, T_ENDCMD);
1233 output_msg (const struct msg *m_, struct lexer *lexer)
1236 .category = m_->category,
1237 .severity = m_->severity,
1238 .location = (m_->location ? m_->location
1239 : lexer ? lex_get_location (lexer, 0, 0)
1241 .command_name = output_get_uppercase_command_name (),
1245 output_item_submit (message_item_create (&m));
1247 free (m.command_name);
1248 if (m.location != m_->location)
1249 msg_location_destroy (m.location);