1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2008, 2010 Free Software Foundation
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/>. */
19 #include "terminal-opts.h"
26 #include "data/settings.h"
27 #include "data/file-name.h"
28 #include "language/lexer/include-path.h"
29 #include "libpspp/argv-parser.h"
30 #include "libpspp/assertion.h"
31 #include "libpspp/cast.h"
32 #include "libpspp/compiler.h"
33 #include "libpspp/llx.h"
34 #include "libpspp/str.h"
35 #include "libpspp/string-array.h"
36 #include "libpspp/string-map.h"
37 #include "libpspp/string-set.h"
38 #include "libpspp/version.h"
39 #include "output/driver.h"
40 #include "output/driver-provider.h"
41 #include "output/msglog.h"
44 #include "gl/localcharset.h"
45 #include "gl/progname.h"
46 #include "gl/version-etc.h"
47 #include "gl/xmemdup0.h"
48 #include "gl/xalloc.h"
51 #define _(msgid) gettext (msgid)
52 #define N_(msgid) msgid
56 struct string_map options; /* Output driver options. */
57 bool has_output_driver;
58 bool has_terminal_driver;
60 enum lex_syntax_mode *syntax_mode;
62 char **syntax_encoding;
81 static struct argv_option terminal_argv_options[N_TERMINAL_OPTIONS] =
83 {"testing-mode", 0, no_argument, OPT_TESTING_MODE},
84 {"error-file", 'e', required_argument, OPT_ERROR_FILE},
85 {"output", 'o', required_argument, OPT_OUTPUT},
86 {NULL, 'O', required_argument, OPT_OUTPUT_OPTION},
87 {"no-output", 0, no_argument, OPT_NO_OUTPUT},
88 {"batch", 'b', no_argument, OPT_BATCH},
89 {"interactive", 'i', no_argument, OPT_INTERACTIVE},
90 {"syntax-encoding", 0, required_argument, OPT_SYNTAX_ENCODING},
91 {"no-statrc", 'r', no_argument, OPT_NO_STATRC},
92 {"help", 'h', no_argument, OPT_HELP},
93 {"version", 'V', no_argument, OPT_VERSION},
97 register_output_driver (struct terminal_opts *to)
99 if (!string_map_is_empty (&to->options))
101 struct output_driver *driver;
103 driver = output_driver_create (&to->options);
106 output_driver_register (driver);
108 to->has_output_driver = true;
109 if (driver->device_type == SETTINGS_DEVICE_TERMINAL)
110 to->has_terminal_driver = true;
112 string_map_clear (&to->options);
117 parse_output_option (struct terminal_opts *to, const char *option)
122 equals = strchr (option, '=');
125 error (0, 0, _("%s: output option missing `='"), option);
129 key = xmemdup0 (option, equals - option);
130 if (string_map_contains (&to->options, key))
132 error (0, 0, _("%s: output option specified more than once"), key);
137 value = xmemdup0 (equals + 1, strlen (equals + 1));
138 string_map_insert_nocopy (&to->options, key, value);
142 get_supported_formats (void)
144 const struct string_set_node *node;
145 struct string_array format_array;
146 struct string_set format_set;
151 /* Get supported formats as unordered set. */
152 string_set_init (&format_set);
153 output_get_supported_formats (&format_set);
155 /* Converted supported formats to sorted array. */
156 string_array_init (&format_array);
157 STRING_SET_FOR_EACH (format, node, &format_set)
158 string_array_append (&format_array, format);
159 string_array_sort (&format_array);
160 string_set_destroy (&format_set);
162 /* Converted supported formats to string. */
163 format_string = string_array_join (&format_array, " ");
164 string_array_destroy (&format_array);
165 return format_string;
171 char *supported_formats = get_supported_formats ();
172 char *inc_path = string_array_join (include_path_default (), " ");
175 PSPP, a program for statistical analysis of sample data.\n\
176 Usage: %s [OPTION]... FILE...\n\
178 Arguments to long options also apply to equivalent short options.\n\
181 -o, --output=FILE output to FILE, default format from FILE's name\n\
182 -O format=FORMAT override format for previous -o\n\
183 -O OPTION=VALUE set output option to customize previous -o\n\
184 -O device={terminal|listing} override device type for previous -o\n\
185 -e, --error-file=FILE append errors, warnings, and notes to FILE\n\
186 --no-output disable default output driver\n\
187 Supported output formats: %s\n\
190 -I, --include=DIR append DIR to search path\n\
191 -I-, --no-include clear search path\n\
192 -r, --no-statrc disable running rc file at startup\n\
193 -a, --algorithm={compatible|enhanced}\n\
194 set to `compatible' if you want output\n\
195 calculated from broken algorithms\n\
196 -x, --syntax={compatible|enhanced}\n\
197 set to `compatible' to disable PSPP extensions\n\
198 -b, --batch interpret syntax in batch mode\n\
199 -i, --interactive interpret syntax in interactive mode\n\
200 --syntax-encoding=ENCODING specify encoding for syntax files\n\
201 -s, --safer don't allow some unsafe operations\n\
202 Default search path: %s\n\
204 Informative output:\n\
205 -h, --help display this help and exit\n\
206 -V, --version output version information and exit\n\
208 Non-option arguments are interpreted as syntax files to execute.\n"),
209 program_name, supported_formats, inc_path);
211 free (supported_formats);
214 emit_bug_reporting_address ();
219 terminal_option_callback (int id, void *to_)
221 struct terminal_opts *to = to_;
225 case OPT_TESTING_MODE:
226 settings_set_testing_mode (true);
230 if (!strcmp (optarg, "none") || msglog_create (optarg))
231 to->has_error_file = true;
235 register_output_driver (to);
236 string_map_insert (&to->options, "output-file", optarg);
239 case OPT_OUTPUT_OPTION:
240 parse_output_option (to, optarg);
244 /* Pretend that we already have an output driver, which disables adding
245 one in terminal_opts_done() when we don't already have one. */
246 to->has_output_driver = true;
250 *to->syntax_mode = LEX_SYNTAX_BATCH;
253 case OPT_INTERACTIVE:
254 *to->syntax_mode = LEX_SYNTAX_INTERACTIVE;
257 case OPT_SYNTAX_ENCODING:
258 *to->syntax_encoding = optarg;
262 *to->process_statrc = false;
270 version_etc (stdout, "pspp", PACKAGE_NAME, PACKAGE_VERSION,
271 "Ben Pfaff", "John Darrington", "Jason Stover",
280 struct terminal_opts *
281 terminal_opts_init (struct argv_parser *ap,
282 enum lex_syntax_mode *syntax_mode, bool *process_statrc,
283 char **syntax_encoding)
285 struct terminal_opts *to;
287 *syntax_mode = LEX_SYNTAX_AUTO;
288 *process_statrc = true;
289 *syntax_encoding = "Auto";
291 to = xzalloc (sizeof *to);
292 to->syntax_mode = syntax_mode;
293 string_map_init (&to->options);
294 to->has_output_driver = false;
295 to->has_error_file = false;
296 to->syntax_mode = syntax_mode;
297 to->process_statrc = process_statrc;
298 to->syntax_encoding = syntax_encoding;
300 argv_parser_add_options (ap, terminal_argv_options, N_TERMINAL_OPTIONS,
301 terminal_option_callback, to);
305 /* Return true iff the terminal appears to be an xterm with
306 UTF-8 capabilities */
308 term_is_utf8_xterm (void)
312 if ( (s = getenv ("TERM")) && (0 == strcmp ("xterm", s)) )
313 if ( (s = getenv ("XTERM_LOCALE")) )
314 return strcasestr (s, "utf8") || strcasestr (s, "utf-8");
320 terminal_opts_done (struct terminal_opts *to, int argc, char *argv[])
322 register_output_driver (to);
323 if (!to->has_output_driver)
325 if ((0 == strcmp (locale_charset (), "UTF-8"))
327 (term_is_utf8_xterm ()) )
329 string_map_insert (&to->options, "box", "unicode");
332 string_map_insert (&to->options, "output-file", "-");
333 string_map_insert (&to->options, "format", "txt");
334 register_output_driver (to);
337 if (!to->has_terminal_driver && !to->has_error_file)
340 string_map_destroy (&to->options);