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