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/syntax-file.h"
29 #include "libpspp/argv-parser.h"
30 #include "libpspp/assertion.h"
31 #include "libpspp/compiler.h"
32 #include "libpspp/getl.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"
42 #include "ui/terminal/msg-ui.h"
43 #include "ui/terminal/read-line.h"
46 #include "gl/progname.h"
47 #include "gl/version-etc.h"
48 #include "gl/xmemdup0.h"
51 #define _(msgid) gettext (msgid)
52 #define N_(msgid) msgid
56 struct source_stream *source_stream;
57 enum syntax_mode syntax_mode;
58 struct string_map options; /* Output driver options. */
59 bool has_output_driver;
60 bool has_terminal_driver;
78 static struct argv_option terminal_argv_options[N_TERMINAL_OPTIONS] =
80 {"testing-mode", 0, no_argument, OPT_TESTING_MODE},
81 {"error-file", 'e', required_argument, OPT_ERROR_FILE},
82 {"output", 'o', required_argument, OPT_OUTPUT},
83 {NULL, 'O', required_argument, OPT_OUTPUT_OPTION},
84 {"interactive", 'i', no_argument, OPT_INTERACTIVE},
85 {"no-statrc", 'r', no_argument, OPT_NO_STATRC},
86 {"help", 'h', no_argument, OPT_HELP},
87 {"version", 'V', no_argument, OPT_VERSION},
91 register_output_driver (struct terminal_opts *to)
93 if (!string_map_is_empty (&to->options))
95 struct output_driver *driver;
97 driver = output_driver_create (&to->options);
100 output_driver_register (driver);
102 to->has_output_driver = true;
103 if (driver->device_type == SETTINGS_DEVICE_TERMINAL)
104 to->has_terminal_driver = true;
106 string_map_clear (&to->options);
111 parse_output_option (struct terminal_opts *to, const char *option)
116 equals = strchr (option, '=');
119 error (0, 0, _("%s: output option missing `='"), option);
123 key = xmemdup0 (option, equals - option);
124 if (string_map_contains (&to->options, key))
126 error (0, 0, _("%s: output option specified more than once"), key);
131 value = xmemdup0 (equals + 1, strlen (equals + 1));
132 string_map_insert_nocopy (&to->options, key, value);
136 get_supported_formats (void)
138 const struct string_set_node *node;
139 struct string_array format_array;
140 struct string_set format_set;
145 /* Get supported formats as unordered set. */
146 string_set_init (&format_set);
147 output_get_supported_formats (&format_set);
149 /* Converted supported formats to sorted array. */
150 string_array_init (&format_array);
151 STRING_SET_FOR_EACH (format, node, &format_set)
152 string_array_append (&format_array, format);
153 string_array_sort (&format_array);
154 string_set_destroy (&format_set);
156 /* Converted supported formats to string. */
157 format_string = string_array_join (&format_array, " ");
158 string_array_destroy (&format_array);
159 return format_string;
163 get_default_include_path (void)
165 struct source_stream *ss;
170 ss = create_source_stream ();
171 path = getl_include_path (ss);
172 ds_init_empty (&dst);
173 for (i = 0; path[i] != NULL; i++)
174 ds_put_format (&dst, " %s", path[i]);
175 destroy_source_stream (ss);
177 return ds_steal_cstr (&dst);
183 char *supported_formats = get_supported_formats ();
184 char *default_include_path = get_default_include_path ();
187 PSPP, a program for statistical analysis of sample data.\n\
188 Usage: %s [OPTION]... FILE...\n\
190 Arguments to long options also apply to equivalent short options.\n\
193 -o, --output=FILE output to FILE, default format from FILE's name\n\
194 -O format=FORMAT override format for previous -o\n\
195 -O OPTION=VALUE set output option to customize previous -o\n\
196 -O device={terminal|listing} override device type for previous -o\n\
197 -e, --error-file=FILE append errors, warnings, and notes to FILE\n\
198 Supported output formats: %s\n\
201 -I, --include=DIR append DIR to search path\n\
202 -I-, --no-include clear search path\n\
203 -r, --no-statrc disable running rc file at startup\n\
204 -a, --algorithm={compatible|enhanced}\n\
205 set to `compatible' if you want output\n\
206 calculated from broken algorithms\n\
207 -x, --syntax={compatible|enhanced}\n\
208 set to `compatible' to disable PSPP extensions\n\
209 -i, --interactive interpret syntax in interactive mode\n\
210 -s, --safer don't allow some unsafe operations\n\
211 Default search path:%s\n\
213 Informative output:\n\
214 -h, --help display this help and exit\n\
215 -V, --version output version information and exit\n\
217 Non-option arguments are interpreted as syntax files to execute.\n",
218 program_name, supported_formats, default_include_path);
220 free (supported_formats);
221 free (default_include_path);
223 emit_bug_reporting_address ();
228 terminal_option_callback (int id, void *to_)
230 struct terminal_opts *to = to_;
234 case OPT_TESTING_MODE:
235 settings_set_testing_mode (true);
239 if (!strcmp (optarg, "none") || msglog_create (optarg))
240 to->has_error_file = true;
244 register_output_driver (to);
245 string_map_insert (&to->options, "output-file", optarg);
248 case OPT_OUTPUT_OPTION:
249 parse_output_option (to, optarg);
252 case OPT_INTERACTIVE:
253 to->syntax_mode = GETL_INTERACTIVE;
257 to->process_statrc = false;
265 version_etc (stdout, "pspp", PACKAGE_NAME, PACKAGE_VERSION,
266 "Ben Pfaff", "John Darrington", "Jason Stover",
275 struct terminal_opts *
276 terminal_opts_init (struct argv_parser *ap, struct source_stream *ss)
278 struct terminal_opts *to;
280 to = xmalloc (sizeof *to);
281 to->source_stream = ss;
282 to->syntax_mode = GETL_BATCH;
283 string_map_init (&to->options);
284 to->has_output_driver = false;
285 to->has_error_file = false;
286 to->process_statrc = true;
288 argv_parser_add_options (ap, terminal_argv_options, N_TERMINAL_OPTIONS,
289 terminal_option_callback, to);
294 add_syntax_file (struct terminal_opts *to, const char *file_name)
296 if (!strcmp (file_name, "-") && isatty (STDIN_FILENO))
297 getl_append_source (to->source_stream, create_readln_source (),
298 GETL_INTERACTIVE, ERRMODE_CONTINUE);
300 getl_append_source (to->source_stream,
301 create_syntax_file_source (file_name),
302 to->syntax_mode, ERRMODE_CONTINUE);
306 terminal_opts_done (struct terminal_opts *to, int argc, char *argv[])
308 if (to->process_statrc)
310 char *rc = fn_search_path ("rc", getl_include_path (to->source_stream));
313 getl_append_source (to->source_stream,
314 create_syntax_file_source (rc), GETL_BATCH,
324 for (i = optind; i < argc; i++)
325 add_syntax_file (to, argv[i]);
328 add_syntax_file (to, "-");
330 register_output_driver (to);
331 if (!to->has_output_driver)
333 string_map_insert (&to->options, "output-file", "-");
334 string_map_insert (&to->options, "format", "txt");
335 register_output_driver (to);
338 if (!to->has_terminal_driver && !to->has_error_file)
341 string_map_destroy (&to->options);