output: Make errors, warnings, and notes into a new "message_item".
[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 <xalloc.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25
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"
44
45 #include "gl/error.h"
46 #include "gl/progname.h"
47 #include "gl/version-etc.h"
48 #include "gl/xmemdup0.h"
49
50 #include "gettext.h"
51 #define _(msgid) gettext (msgid)
52 #define N_(msgid) msgid
53
54 struct terminal_opts
55   {
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;
61     bool has_error_file;
62     bool process_statrc;
63   };
64
65 enum
66   {
67     OPT_TESTING_MODE,
68     OPT_ERROR_FILE,
69     OPT_OUTPUT,
70     OPT_OUTPUT_OPTION,
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     {"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},
88   };
89
90 static void
91 register_output_driver (struct terminal_opts *to)
92 {
93   if (!string_map_is_empty (&to->options))
94     {
95       struct output_driver *driver;
96
97       driver = output_driver_create (&to->options);
98       if (driver != NULL)
99         {
100           output_driver_register (driver);
101
102           to->has_output_driver = true;
103           if (driver->device_type == SETTINGS_DEVICE_TERMINAL)
104             to->has_terminal_driver = true;
105         }
106       string_map_clear (&to->options);
107     }
108 }
109
110 static void
111 parse_output_option (struct terminal_opts *to, const char *option)
112 {
113   const char *equals;
114   char *key, *value;
115
116   equals = strchr (option, '=');
117   if (equals == NULL)
118     {
119       error (0, 0, _("%s: output option missing `='"), option);
120       return;
121     }
122
123   key = xmemdup0 (option, equals - option);
124   if (string_map_contains (&to->options, key))
125     {
126       error (0, 0, _("%s: output option %s specified more than twice"), key);
127       free (key);
128       return;
129     }
130
131   value = xmemdup0 (equals + 1, strlen (equals + 1));
132   string_map_insert_nocopy (&to->options, key, value);
133 }
134
135 static char *
136 get_supported_formats (void)
137 {
138   const struct string_set_node *node;
139   struct string_array format_array;
140   struct string_set format_set;
141   char *format_string;
142   const char *format;
143   size_t i;
144
145   /* Get supported formats as unordered set. */
146   string_set_init (&format_set);
147   output_get_supported_formats (&format_set);
148
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);
155
156   /* Converted supported formats to string. */
157   format_string = string_array_join (&format_array, " ");
158   string_array_destroy (&format_array);
159   return format_string;
160 }
161
162 static char *
163 get_default_include_path (void)
164 {
165   struct source_stream *ss;
166   struct string dst;
167   char **path;
168   size_t i;
169
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);
176
177   return ds_steal_cstr (&dst);
178 }
179
180 static void
181 usage (void)
182 {
183   char *supported_formats = get_supported_formats ();
184   char *default_include_path = get_default_include_path ();
185
186   printf ("\
187 PSPP, a program for statistical analysis of sample data.\n\
188 Usage: %s [OPTION]... FILE...\n\
189 \n\
190 Arguments to long options also apply to equivalent short options.\n\
191 \n\
192 Output 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\
199 \n\
200 Language options:\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\
212 \n\
213 Informative output:\n\
214   -h, --help                display this help and exit\n\
215   -V, --version             output version information and exit\n\
216 \n\
217 Non-option arguments are interpreted as syntax files to execute.\n",
218           program_name, supported_formats, default_include_path);
219
220   free (supported_formats);
221   free (default_include_path);
222
223   emit_bug_reporting_address ();
224   exit (EXIT_SUCCESS);
225 }
226
227 static void
228 terminal_option_callback (int id, void *to_)
229 {
230   struct terminal_opts *to = to_;
231
232   switch (id)
233     {
234     case OPT_TESTING_MODE:
235       settings_set_testing_mode (true);
236       break;
237
238     case OPT_ERROR_FILE:
239       if (!strcmp (optarg, "none") || msglog_create (optarg))
240         to->has_error_file = true;
241       break;
242
243     case OPT_OUTPUT:
244       register_output_driver (to);
245       string_map_insert (&to->options, "output-file", optarg);
246       break;
247
248     case OPT_OUTPUT_OPTION:
249       parse_output_option (to, optarg);
250       break;
251
252     case OPT_INTERACTIVE:
253       to->syntax_mode = GETL_INTERACTIVE;
254       break;
255
256     case OPT_NO_STATRC:
257       to->process_statrc = false;
258       break;
259
260     case OPT_HELP:
261       usage ();
262       exit (EXIT_SUCCESS);
263
264     case OPT_VERSION:
265       version_etc (stdout, "pspp", PACKAGE_NAME, PACKAGE_VERSION,
266                    "Ben Pfaff", "John Darrington", "Jason Stover",
267                    (char *) NULL);
268       exit (EXIT_SUCCESS);
269
270     default:
271       NOT_REACHED ();
272     }
273 }
274
275 struct terminal_opts *
276 terminal_opts_init (struct argv_parser *ap, struct source_stream *ss)
277 {
278   struct terminal_opts *to;
279
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;
287
288   argv_parser_add_options (ap, terminal_argv_options, N_TERMINAL_OPTIONS,
289                            terminal_option_callback, to);
290   return to;
291 }
292
293 static void
294 add_syntax_file (struct terminal_opts *to, const char *file_name)
295 {
296   if (!strcmp (file_name, "-") && isatty (STDIN_FILENO))
297     getl_append_source (to->source_stream, create_readln_source (),
298                         GETL_INTERACTIVE, ERRMODE_CONTINUE);
299   else
300     getl_append_source (to->source_stream,
301                         create_syntax_file_source (file_name),
302                         to->syntax_mode, ERRMODE_CONTINUE);
303 }
304
305 void
306 terminal_opts_done (struct terminal_opts *to, int argc, char *argv[])
307 {
308   if (to->process_statrc)
309     {
310       char *rc = fn_search_path ("rc", getl_include_path (to->source_stream));
311       if (rc != NULL)
312         {
313           getl_append_source (to->source_stream,
314                               create_syntax_file_source (rc), GETL_BATCH,
315                               ERRMODE_CONTINUE);
316           free (rc);
317         }
318     }
319
320   if (optind < argc)
321     {
322       int i;
323
324       for (i = optind; i < argc; i++)
325         add_syntax_file (to, argv[i]);
326     }
327   else
328     add_syntax_file (to, "-");
329
330   register_output_driver (to);
331   if (!to->has_output_driver)
332     {
333       string_map_insert (&to->options, "output-file", "-");
334       string_map_insert (&to->options, "format", "txt");
335       register_output_driver (to);
336     }
337
338   if (to->has_terminal_driver && !to->has_error_file)
339     msglog_create ("-");
340
341   string_map_destroy (&to->options);
342   free (to);
343 }