verbose-msg: Remove.
[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 <argp.h>
22 #include <stdbool.h>
23 #include <xalloc.h>
24 #include <stdlib.h>
25
26 #include <data/settings.h>
27 #include <data/file-name.h>
28 #include <language/syntax-file.h>
29 #include <libpspp/getl.h>
30 #include <libpspp/llx.h>
31 #include <libpspp/string-map.h>
32 #include <libpspp/string-set.h>
33 #include <output/driver.h>
34 #include <ui/command-line.h>
35 #include <ui/terminal/msg-ui.h>
36 #include <ui/terminal/read-line.h>
37
38 #include "gl/error.h"
39
40 #include "gettext.h"
41 #define _(msgid) gettext (msgid)
42 #define N_(msgid) msgid
43
44 static const struct argp_option test_options [] =
45   {
46     {"testing-mode", 'T', 0, OPTION_HIDDEN, 0, 0},
47
48     { 0, 0, 0, 0, 0, 0 }
49   };
50
51 static error_t
52 parse_test_opts (int key, char *arg, struct argp_state *state)
53 {
54   switch (key)
55     {
56     case 'T':
57       settings_set_testing_mode (true);
58       break;
59     default:
60       return ARGP_ERR_UNKNOWN;
61     }
62
63   return 0;
64 }
65
66 static const struct argp_option io_options [] =
67   {
68     {"error-file", 'e', "FILE", 0,
69      N_("Send error messages to FILE (appended)"), 0},
70
71     {"device", 'o', "DEVICE", 0,
72      N_("Select output driver DEVICE and disable defaults"), 0},
73
74     {"list", 'l', 0, 0,
75      N_("Print a list of known driver classes, then exit"), 0},
76
77     {"interactive", 'i', 0, 0, N_("Start an interactive session"), 0},
78
79     { 0, 0, 0, 0, 0, 0 }
80   };
81
82
83 static error_t
84 parse_io_opts (int key, char *arg, struct argp_state *state)
85 {
86   struct source_init
87   {
88     struct llx_list file_list;
89     bool interactive;
90
91     /* Output devices. */
92     struct string_map macros;
93     struct string_set drivers;
94   };
95
96   struct fn_element {
97     struct ll ll;
98     const char *fn;
99   };
100
101   struct source_init *sip = state->hook;
102
103   struct source_stream *ss = state->input;
104
105   struct command_line_processor *clp = get_subject (state);
106
107   switch (key)
108     {
109     case ARGP_KEY_INIT:
110       state->hook = sip = xzalloc (sizeof (struct source_init));
111       llx_init (&sip->file_list);
112       string_map_init (&sip->macros);
113       string_set_init (&sip->drivers);
114       break;
115     case ARGP_KEY_ARG:
116       if (strchr (arg, '='))
117         {
118           if (!output_define_macro (arg, &sip->macros))
119             error (0, 0, _("\"%s\" is not a valid macro definition"), arg);
120         }
121       else
122         {
123           llx_push_tail (&sip->file_list, arg, &llx_malloc_mgr);
124         }
125       break;
126     case ARGP_KEY_SUCCESS:
127       {
128       struct llx *llx = llx_null (&sip->file_list);
129       while ((llx = llx_next (llx)) != llx_null (&sip->file_list))
130         {
131           const char *fn = llx_data (llx);
132           /* Assume it's a syntax file */
133           getl_append_source (ss,
134                               create_syntax_file_source (fn),
135                               GETL_BATCH,
136                               ERRMODE_CONTINUE
137                               );
138
139         }
140
141       if (sip->interactive || llx_is_empty (&sip->file_list))
142         {
143           getl_append_source (ss, create_readln_source (),
144                               GETL_INTERACTIVE,
145                               ERRMODE_CONTINUE
146                               );
147
148           string_set_insert (&sip->drivers, "interactive");
149         }
150
151       if (!settings_get_testing_mode ())
152         output_read_configuration (&sip->macros, &sip->drivers);
153       else
154         output_configure_driver ("csv:csv::");
155
156       string_map_destroy (&sip->macros);
157       string_set_destroy (&sip->drivers);
158       }
159       break;
160     case ARGP_KEY_FINI:
161       free (sip);
162       break;
163     case 'e':
164       msg_ui_set_error_file (arg);
165       break;
166     case 'i':
167       sip->interactive = true;
168       break;
169     case 'l':
170       output_list_classes ();
171       break;
172     case 'o':
173       string_set_insert (&sip->drivers, arg);
174       break;
175     default:
176       return ARGP_ERR_UNKNOWN;
177     }
178
179   return 0;
180 }
181
182 const struct argp io_argp =  {io_options, parse_io_opts, 0, 0, 0, 0, 0};
183 const struct argp test_argp =  {test_options, parse_test_opts, 0, 0, 0, 0, 0};
184
185 #if 0
186 static const struct argp_child children [] =
187   {
188     {&io_argp, 0, N_("Options affecting input and output locations:"), 0},
189     {&test_argp, 0, N_("Diagnostic options:"), 0},
190     {0, 0, 0, 0}
191   };
192
193
194 static error_t
195 propagate_aux (int key, char *arg, struct argp_state *state)
196 {
197   if ( key == ARGP_KEY_INIT)
198     {
199       int i;
200       for (i = 0 ; i < sizeof (children) / sizeof (children[0]) - 1 ; ++i)
201         state->child_inputs[i] = state->input;
202     }
203
204   return ARGP_ERR_UNKNOWN;
205 }
206
207 const struct argp terminal_argp =  {NULL, propagate_aux, 0, 0, children, 0, 0};
208
209 #endif