d55a8a2e9607d5622dcb1f52b192a78263f7f28b
[pspp-builds.git] / src / ui / terminal / terminal-opts.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2008, 2010  Free Software Foundation
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include "terminal-opts.h"
20
21 #include <stdbool.h>
22 #include <stdlib.h>
23
24 #include "data/settings.h"
25 #include "data/file-name.h"
26 #include "language/syntax-file.h"
27 #include "libpspp/argv-parser.h"
28 #include "libpspp/assertion.h"
29 #include "libpspp/cast.h"
30 #include "libpspp/compiler.h"
31 #include "libpspp/getl.h"
32 #include "libpspp/llx.h"
33 #include "libpspp/str.h"
34 #include "libpspp/string-array.h"
35 #include "libpspp/string-map.h"
36 #include "libpspp/string-set.h"
37 #include "libpspp/version.h"
38 #include "output/driver.h"
39 #include "output/driver-provider.h"
40 #include "output/msglog.h"
41 #include "ui/terminal/msg-ui.h"
42 #include "ui/terminal/read-line.h"
43
44 #include "gl/error.h"
45 #include "gl/progname.h"
46 #include "gl/version-etc.h"
47 #include "gl/xmemdup0.h"
48 #include "gl/xalloc.h"
49
50 #include "gettext.h"
51 #define _(msgid) gettext (msgid)
52 #define N_(msgid) msgid
53
54 struct terminal_opts
55   {
56     enum syntax_mode *syntax_mode;
57     struct string_map options;  /* Output driver options. */
58     bool has_output_driver;
59     bool has_terminal_driver;
60     bool has_error_file;
61     bool *process_statrc;
62   };
63
64 enum
65   {
66     OPT_TESTING_MODE,
67     OPT_ERROR_FILE,
68     OPT_OUTPUT,
69     OPT_OUTPUT_OPTION,
70     OPT_NO_OUTPUT,
71     OPT_INTERACTIVE,
72     OPT_NO_STATRC,
73     OPT_HELP,
74     OPT_VERSION,
75     N_TERMINAL_OPTIONS
76   };
77
78 static struct argv_option terminal_argv_options[N_TERMINAL_OPTIONS] =
79   {
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     {"no-output", 0, no_argument, OPT_NO_OUTPUT},
85     {"interactive", 'i', no_argument, OPT_INTERACTIVE},
86     {"no-statrc", 'r', no_argument, OPT_NO_STATRC},
87     {"help", 'h', no_argument, OPT_HELP},
88     {"version", 'V', no_argument, OPT_VERSION},
89   };
90
91 static void
92 register_output_driver (struct terminal_opts *to)
93 {
94   if (!string_map_is_empty (&to->options))
95     {
96       struct output_driver *driver;
97
98       driver = output_driver_create (&to->options);
99       if (driver != NULL)
100         {
101           output_driver_register (driver);
102
103           to->has_output_driver = true;
104           if (driver->device_type == SETTINGS_DEVICE_TERMINAL)
105             to->has_terminal_driver = true;
106         }
107       string_map_clear (&to->options);
108     }
109 }
110
111 static void
112 parse_output_option (struct terminal_opts *to, const char *option)
113 {
114   const char *equals;
115   char *key, *value;
116
117   equals = strchr (option, '=');
118   if (equals == NULL)
119     {
120       error (0, 0, _("%s: output option missing `='"), option);
121       return;
122     }
123
124   key = xmemdup0 (option, equals - option);
125   if (string_map_contains (&to->options, key))
126     {
127       error (0, 0, _("%s: output option specified more than once"), key);
128       free (key);
129       return;
130     }
131
132   value = xmemdup0 (equals + 1, strlen (equals + 1));
133   string_map_insert_nocopy (&to->options, key, value);
134 }
135
136 static char *
137 get_supported_formats (void)
138 {
139   const struct string_set_node *node;
140   struct string_array format_array;
141   struct string_set format_set;
142   char *format_string;
143   const char *format;
144   size_t i;
145
146   /* Get supported formats as unordered set. */
147   string_set_init (&format_set);
148   output_get_supported_formats (&format_set);
149
150   /* Converted supported formats to sorted array. */
151   string_array_init (&format_array);
152   STRING_SET_FOR_EACH (format, node, &format_set)
153     string_array_append (&format_array, format);
154   string_array_sort (&format_array);
155   string_set_destroy (&format_set);
156
157   /* Converted supported formats to string. */
158   format_string = string_array_join (&format_array, " ");
159   string_array_destroy (&format_array);
160   return format_string;
161 }
162
163 static char *
164 get_default_include_path (void)
165 {
166   struct source_stream *ss;
167   struct string dst;
168   char **path;
169   size_t i;
170
171   ss = create_source_stream ();
172   path = getl_include_path (ss);
173   ds_init_empty (&dst);
174   for (i = 0; path[i] != NULL; i++)
175     ds_put_format (&dst, " %s", path[i]);
176   destroy_source_stream (ss);
177
178   return ds_steal_cstr (&dst);
179 }
180
181 static void
182 usage (void)
183 {
184   char *supported_formats = get_supported_formats ();
185   char *default_include_path = get_default_include_path ();
186
187   printf (_("\
188 PSPP, a program for statistical analysis of sample data.\n\
189 Usage: %s [OPTION]... FILE...\n\
190 \n\
191 Arguments to long options also apply to equivalent short options.\n\
192 \n\
193 Output options:\n\
194   -o, --output=FILE         output to FILE, default format from FILE's name\n\
195   -O format=FORMAT          override format for previous -o\n\
196   -O OPTION=VALUE           set output option to customize previous -o\n\
197   -O device={terminal|listing}  override device type for previous -o\n\
198   -e, --error-file=FILE     append errors, warnings, and notes to FILE\n\
199   --no-output               disable default output driver\n\
200 Supported output formats: %s\n\
201 \n\
202 Language options:\n\
203   -I, --include=DIR         append DIR to search path\n\
204   -I-, --no-include         clear search path\n\
205   -r, --no-statrc           disable running rc file at startup\n\
206   -a, --algorithm={compatible|enhanced}\n\
207                             set to `compatible' if you want output\n\
208                             calculated from broken algorithms\n\
209   -x, --syntax={compatible|enhanced}\n\
210                             set to `compatible' to disable PSPP extensions\n\
211   -i, --interactive         interpret syntax in interactive mode\n\
212   -s, --safer               don't allow some unsafe operations\n\
213 Default search path:%s\n\
214 \n\
215 Informative output:\n\
216   -h, --help                display this help and exit\n\
217   -V, --version             output version information and exit\n\
218 \n\
219 Non-option arguments are interpreted as syntax files to execute.\n"),
220           program_name, supported_formats, default_include_path);
221
222   free (supported_formats);
223   free (default_include_path);
224
225   emit_bug_reporting_address ();
226   exit (EXIT_SUCCESS);
227 }
228
229 static void
230 terminal_option_callback (int id, void *to_)
231 {
232   struct terminal_opts *to = to_;
233
234   switch (id)
235     {
236     case OPT_TESTING_MODE:
237       settings_set_testing_mode (true);
238       break;
239
240     case OPT_ERROR_FILE:
241       if (!strcmp (optarg, "none") || msglog_create (optarg))
242         to->has_error_file = true;
243       break;
244
245     case OPT_OUTPUT:
246       register_output_driver (to);
247       string_map_insert (&to->options, "output-file", optarg);
248       break;
249
250     case OPT_OUTPUT_OPTION:
251       parse_output_option (to, optarg);
252       break;
253
254     case OPT_NO_OUTPUT:
255       /* Pretend that we already have an output driver, which disables adding
256          one in terminal_opts_done() when we don't already have one. */
257       to->has_output_driver = true;
258       break;
259
260     case OPT_INTERACTIVE:
261       *to->syntax_mode = GETL_INTERACTIVE;
262       break;
263
264     case OPT_NO_STATRC:
265       *to->process_statrc = false;
266       break;
267
268     case OPT_HELP:
269       usage ();
270       exit (EXIT_SUCCESS);
271
272     case OPT_VERSION:
273       version_etc (stdout, "pspp", PACKAGE_NAME, PACKAGE_VERSION,
274                    "Ben Pfaff", "John Darrington", "Jason Stover",
275                    NULL_SENTINEL);
276       exit (EXIT_SUCCESS);
277
278     default:
279       NOT_REACHED ();
280     }
281 }
282
283 struct terminal_opts *
284 terminal_opts_init (struct argv_parser *ap,
285                     enum syntax_mode *syntax_mode, bool *process_statrc)
286 {
287   struct terminal_opts *to;
288
289   *syntax_mode = GETL_BATCH;
290   *process_statrc = true;
291
292   to = xzalloc (sizeof *to);
293   to->syntax_mode = syntax_mode;
294   string_map_init (&to->options);
295   to->has_output_driver = false;
296   to->has_error_file = false;
297   to->process_statrc = process_statrc;
298
299   argv_parser_add_options (ap, terminal_argv_options, N_TERMINAL_OPTIONS,
300                            terminal_option_callback, to);
301   return to;
302 }
303
304 void
305 terminal_opts_done (struct terminal_opts *to, int argc, char *argv[])
306 {
307   register_output_driver (to);
308   if (!to->has_output_driver)
309     {
310       string_map_insert (&to->options, "output-file", "-");
311       string_map_insert (&to->options, "format", "txt");
312       register_output_driver (to);
313     }
314
315   if (!to->has_terminal_driver && !to->has_error_file)
316     msglog_create ("-");
317
318   string_map_destroy (&to->options);
319   free (to);
320 }