Make interactive output go to the terminal (bug #17213), by
[pspp-builds.git] / src / ui / terminal / command-line.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2007 Free Software Foundation, Inc.
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 #include "command-line.h"
19 #include "msg-ui.h"
20 #include <libpspp/message.h>
21 #include <ctype.h>
22 #include <stdio.h>
23 #include <errno.h>
24 #include <getopt.h>
25 #include <stdlib.h>
26 #include <libpspp/alloc.h>
27 #include <libpspp/assertion.h>
28 #include <libpspp/copyleft.h>
29 #include <libpspp/message.h>
30 #include <language/syntax-file.h>
31 #include "progname.h"
32 #include <data/settings.h>
33 #include <output/output.h>
34 #include <data/file-name.h>
35 #include <libpspp/getl.h>
36 #include <libpspp/str.h>
37 #include <libpspp/version.h>
38 #include <libpspp/verbose-msg.h>
39 #include "read-line.h"
40
41 #include "gettext.h"
42 #define _(msgid) gettext (msgid)
43 #define N_(msgid) msgid
44
45 static void usage (void);
46
47 /* Parses the command line specified by ARGC and ARGV as received by
48    main().  Returns true if normal execution should proceed,
49    false if the command-line indicates that PSPP should exit. */
50 bool
51 parse_command_line (int argc, char **argv, struct source_stream *ss)
52 {
53   static struct option long_options[] =
54   {
55     {"algorithm", required_argument, NULL, 'a'},
56     {"command", required_argument, NULL, 'c'},
57     {"config-directory", required_argument, NULL, 'B'},
58     {"device", required_argument, NULL, 'o'},
59     {"dry-run", no_argument, NULL, 'n'},
60     {"edit", no_argument, NULL, 'n'},
61     {"error-file", required_argument, NULL, 'e'},
62     {"help", no_argument, NULL, 'h'},
63     {"include-directory", required_argument, NULL, 'I'},
64     {"interactive", no_argument, NULL, 'i'},
65     {"just-print", no_argument, NULL, 'n'},
66     {"list", no_argument, NULL, 'l'},
67     {"no-include", no_argument, NULL, 'I'},
68     {"no-statrc", no_argument, NULL, 'r'},
69     {"out-file", required_argument, NULL, 'f'},
70     {"pipe", no_argument, NULL, 'p'},
71     {"recon", no_argument, NULL, 'n'},
72     {"safer", no_argument, NULL, 's'},
73     {"syntax", required_argument, NULL, 'x'},
74     {"testing-mode", no_argument, NULL, 'T'},
75     {"verbose", no_argument, NULL, 'v'},
76     {"version", no_argument, NULL, 'V'},
77     {0, 0, 0, 0},
78   };
79
80   int c, i;
81
82   bool cleared_device_defaults = false;
83   bool process_statrc = true;
84   bool interactive_mode = false;
85   int syntax_files = 0;
86
87   for (;;)
88     {
89       c = getopt_long (argc, argv, "a:x:B:c:e:f:hiI:lno:prsvV", long_options, NULL);
90       if (c == -1)
91         break;
92
93       switch (c)
94         {
95           /* Compatibility options */
96         case 'a':
97           if ( 0 == strcmp(optarg,"compatible") )
98               set_algorithm(COMPATIBLE);
99           else if ( 0 == strcmp(optarg,"enhanced"))
100               set_algorithm(ENHANCED);
101           else
102             {
103               usage ();
104               return false;
105             }
106           break;
107
108         case 'x':
109           if ( 0 == strcmp(optarg,"compatible") )
110             set_syntax(COMPATIBLE);
111           else if ( 0 == strcmp(optarg,"enhanced"))
112             set_syntax(ENHANCED);
113           else
114             {
115               usage ();
116               return false;
117             }
118           break;
119         case 'e':
120           msg_ui_set_error_file (optarg);
121           break;
122         case 'B':
123           config_path = optarg;
124           break;
125         case 'f':
126           printf (_("%s is not yet implemented."), "-f");
127           putchar('\n');
128           break;
129         case 'h':
130           usage ();
131           return false;
132         case 'i':
133           interactive_mode = true;
134           break;
135         case 'I':
136           if (optarg == NULL || !strcmp (optarg, "-"))
137             getl_clear_include_path (ss);
138           else
139             getl_add_include_dir (ss, optarg);
140           break;
141         case 'l':
142           outp_list_classes ();
143           return false;
144         case 'n':
145           printf (_("%s is not yet implemented."),"-n");
146           putchar('\n');
147           break;
148         case 'o':
149           if (!cleared_device_defaults)
150             {
151               outp_configure_clear ();
152               cleared_device_defaults = true;
153             }
154           outp_configure_add (optarg);
155           break;
156         case 'p':
157           printf (_("%s is not yet implemented."),"-p");
158           putchar('\n');
159           break;
160         case 'r':
161           process_statrc = false;
162           break;
163         case 's':
164           set_safer_mode ();
165           break;
166         case 'v':
167           verbose_increment_level ();
168           break;
169         case 'V':
170           puts (version);
171           puts (legal);
172           return false;
173         case 'T':
174           force_long_view ();
175           set_testing_mode (true);
176           break;
177         case '?':
178           usage ();
179           return false;
180         case 0:
181           break;
182         default:
183           NOT_REACHED ();
184         }
185     }
186
187   if (process_statrc)
188     {
189       char *pspprc_fn = fn_search_path ("rc", config_path);
190       if (pspprc_fn != NULL)
191         {
192           getl_append_source (ss, create_syntax_file_source (pspprc_fn));
193
194           free (pspprc_fn);
195         }
196     }
197
198   for (i = optind; i < argc; i++)
199     if (strchr (argv[i], '='))
200       outp_configure_macro (argv[i]);
201     else
202       {
203         getl_append_source (ss, create_syntax_file_source (argv[i]));
204         syntax_files++;
205       }
206
207   if (!syntax_files || interactive_mode)
208     {
209       getl_append_source (ss, create_readln_source () );
210       if (!cleared_device_defaults)
211         outp_configure_add ("interactive");
212     }
213
214   return true;
215 }
216
217 /* Message that describes PSPP command-line syntax. */
218 static const char pre_syntax_message[] =
219 N_("PSPP, a program for statistical analysis of sample data.\n"
220 "\nUsage: %s [OPTION]... FILE...\n"
221 "\nIf a long option shows an argument as mandatory, then it is mandatory\n"
222 "for the equivalent short option also.  Similarly for optional arguments.\n"
223 "\nConfiguration:\n"
224 "  -a, --algorithm={compatible|enhanced}\n"
225 "                            set to `compatible' if you want output\n"
226 "                            calculated from broken algorithms\n"
227 "  -B, --config-dir=DIR      set configuration directory to DIR\n"
228 "  -o, --device=DEVICE       select output driver DEVICE and disable defaults\n"
229 "\nInput and output:\n"
230 "  -e, --error-file=FILE     send error messages to FILE (appended)\n"
231 "  -f, --out-file=FILE       send output to FILE (overwritten)\n"
232 "  -p, --pipe                read syntax from stdin, send output to stdout\n"
233 "  -I-, --no-include         clear include path\n"
234 "  -I, --include=DIR         append DIR to include path\n"
235 "\nLanguage modifiers:\n"
236 "  -i, --interactive         interpret syntax in interactive mode\n"
237 "  -n, --edit                just check syntax; don't actually run the code\n"
238 "  -r, --no-statrc           disable execution of .pspp/rc at startup\n"
239 "  -s, --safer               don't allow some unsafe operations\n"
240 "  -x, --syntax={compatible|enhanced}\n"
241 "                            set to `compatible' if you want only to accept\n"
242 "                            spss compatible syntax\n"
243 "\nInformative output:\n"
244 "  -h, --help                print this help, then exit\n"
245 "  -l, --list                print a list of known driver classes, then exit\n"
246 "  -V, --version             show PSPP version, then exit\n"
247 "  -v, --verbose             increments verbosity level\n"
248 "\nNon-option arguments:\n"
249 " FILE                       syntax file to execute\n"
250 " KEY=VALUE                  overrides macros in output initialization file\n"
251 "\n");
252
253 /* Message that describes PSPP command-line syntax, continued. */
254 static const char post_syntax_message[] = N_("\nReport bugs to <%s>.\n");
255
256 /* Writes a syntax description to stdout. */
257 static void
258 usage (void)
259 {
260   printf (gettext (pre_syntax_message), program_name);
261   outp_list_classes ();
262   printf (gettext (post_syntax_message), PACKAGE_BUGREPORT);
263 }