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