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/>. */
25 #include "data/file-handle-def.h"
26 #include "libpspp/assertion.h"
27 #include "libpspp/compiler.h"
28 #include "libpspp/i18n.h"
29 #include "libpspp/string-map.h"
30 #include "output/ascii.h"
31 #include "output/driver.h"
32 #include "output/output-item.h"
35 #include "gl/progname.h"
36 #include "gl/xalloc.h"
37 #include "gl/xvasprintf.h"
39 /* --emphasis: ASCII driver emphasis option. */
41 static bool underline;
43 /* --box: ASCII driver box option. */
46 /* ASCII driver, for ASCII driver test mode. */
47 static struct output_driver *ascii_driver;
49 static const char *parse_options (int argc, char **argv);
50 static void usage (void) NO_RETURN;
51 static void draw (FILE *);
54 main (int argc, char **argv)
56 set_program_name (argv[0]);
58 output_engine_push ();
59 const char *input_file_name = parse_options (argc, argv);
61 FILE *input = (!strcmp (input_file_name, "-")
63 : fopen (input_file_name, "r"));
65 error (1, errno, "%s: open failed", input_file_name);
78 static struct output_driver *
79 configure_driver (int width, int min_break)
81 struct string_map options = STRING_MAP_INITIALIZER (options);
82 string_map_insert (&options, "format", "txt");
83 string_map_insert (&options, "output-file", "-");
84 string_map_insert_nocopy (&options, xstrdup ("width"),
85 xasprintf ("%d", width));
87 string_map_insert_nocopy (&options, xstrdup ("min-hbreak"),
88 xasprintf ("%d", min_break));
89 if (bold || underline)
90 string_map_insert (&options, "emphasis", "true");
92 string_map_insert (&options, "box", box);
94 struct output_driver *driver = output_driver_create (&options);
95 string_map_destroy (&options);
98 output_driver_register (driver);
103 parse_options (int argc, char **argv)
111 OPT_WIDTH = UCHAR_MAX + 1,
118 static const struct option options[] =
120 {"width", required_argument, NULL, OPT_WIDTH},
121 {"length", required_argument, NULL, OPT_LENGTH},
122 {"emphasis", required_argument, NULL, OPT_EMPHASIS},
123 {"box", required_argument, NULL, OPT_BOX},
124 {"help", no_argument, NULL, OPT_HELP},
128 int c = getopt_long (argc, argv, "o:", options, NULL);
135 width = atoi (optarg);
139 if (!strcmp (optarg, "bold"))
144 else if (!strcmp (optarg, "underline"))
149 else if (!strcmp (optarg, "none"))
151 bold = underline = false;
154 error (1, 0, "argument to --emphasis must be \"bold\" or "
155 "\"underline\" or \"none\"");
178 ascii_driver = configure_driver (width, min_break);
180 if (optind + 1 != argc)
181 error (1, 0, "exactly one non-option argument required; "
182 "use --help for help");
189 printf ("%s, to test PSPP ASCII driver drawing\n"
190 "usage: %s [OPTIONS] INPUT\n"
192 " --width=WIDTH set page width in characters\n"
193 " --length=LINE set page length in lines\n",
194 program_name, program_name);
204 while (fgets (buffer, sizeof buffer, stream))
206 char text[sizeof buffer];
212 if (strchr ("#\r\n", buffer[0]))
215 if (sscanf (buffer, "%d %d %d %[^\n]", &x, &y, &emph, text) == 4)
216 ascii_test_write (ascii_driver, text, x, y, emph ? bold : false,
217 emph ? underline : false);
218 else if (sscanf (buffer, "set-length %d %d", &y, &length) == 2)
219 ascii_test_set_length (ascii_driver, y, length);
221 error (1, 0, "line %d has invalid format", line);
223 ascii_test_flush (ascii_driver);