Made DEBUG commands only available in testing mode.
[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., 59 Temple Place - Suite 330, Boston, MA
18    02111-1307, 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 "getline.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 void welcome (void);
42 static void usage (void);
43
44 char *subst_vars (char *);
45
46 static int testing_mode=0;
47
48 /* Parses the command line specified by ARGC and ARGV as received by
49    main(). */
50 void
51 parse_command_line (int argc, char **argv)
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     {"help", no_argument, NULL, 'h'},
62     {"include-directory", required_argument, NULL, 'I'},
63     {"interactive", no_argument, NULL, 'i'},
64     {"just-print", no_argument, NULL, 'n'},
65     {"list", no_argument, NULL, 'l'},
66     {"no-include", no_argument, NULL, 'I'},
67     {"no-statrc", no_argument, NULL, 'r'},
68     {"out-file", required_argument, NULL, 'f'},
69     {"pipe", no_argument, NULL, 'p'},
70     {"recon", no_argument, NULL, 'n'},
71     {"safer", no_argument, NULL, 's'},
72     {"syntax", required_argument, NULL, 'x'},
73     {"testing-mode", no_argument, &testing_mode, 1},
74     {"verbose", no_argument, NULL, 'v'},
75     {"version", no_argument, NULL, 'V'},
76     {0, 0, 0, 0},
77   };
78
79   int c, i;
80
81   int cleared_device_defaults = 0;
82
83   int no_statrc = 0;
84
85   for (;;)
86     {
87       c = getopt_long (argc, argv, "a:x:B:c:f:hiI:lno:prsvV", long_options, NULL);
88       if (c == -1)
89         break;
90
91       switch (c)
92         {
93           /* Compatibility options */
94         case 'a':
95           if ( 0 == strcmp(optarg,"compatible") )
96               set_algorithm(COMPATIBLE);
97           else if ( 0 == strcmp(optarg,"enhanced"))
98               set_algorithm(ENHANCED);
99           else
100             {
101               usage();
102               assert(0);
103             }
104           break;
105
106         case 'x':         
107           if ( 0 == strcmp(optarg,"compatible") )
108             set_syntax(COMPATIBLE);
109           else if ( 0 == strcmp(optarg,"enhanced"))
110             set_syntax(ENHANCED);
111           else
112             {
113               usage();
114               assert(0);
115             }
116           break;
117
118         case 'c':
119           {
120             static int n_cmds;
121             
122             struct getl_script *script = xmalloc (sizeof *script);
123             
124             {
125               struct getl_line_list *line;
126
127               script->first_line = line = xmalloc (sizeof *line);
128               line->line = xstrdup ("commandline");
129               line->len = --n_cmds;
130               line = line->next = xmalloc (sizeof *line);
131               line->line = xstrdup (optarg);
132               line->len = strlen (optarg);
133               line->next = NULL;
134             }
135
136             getl_add_virtual_file (script);
137           }
138           break;
139         case 'B':
140           config_path = optarg;
141           break;
142         case 'f':
143           printf(_("%s is not yet implemented."), "-f");
144           putchar('\n');
145           break;
146         case 'h':
147           usage ();
148           assert (0);
149         case 'i':
150           getl_interactive = 2;
151           break;
152         case 'I':
153           if (optarg == NULL || !strcmp (optarg, "-"))
154             getl_clear_include_path ();
155           else
156             getl_add_include_dir (optarg);
157           break;
158         case 'l':
159           outp_list_classes ();
160           err_hcf (1);
161         case 'n':
162           printf (_("%s is not yet implemented."),"-n");
163           putchar('\n');
164           break;
165         case 'o':
166           if (!cleared_device_defaults)
167             {
168               outp_configure_clear ();
169               cleared_device_defaults = 1;
170             }
171           outp_configure_add (optarg);
172           break;
173         case 'p':
174           printf (_("%s is not yet implemented."),"-p");
175           putchar('\n');
176           break;
177         case 'r':
178           no_statrc = 1;
179           break;
180         case 's':
181           make_safe();
182           break;
183         case 'v':
184           err_verbosity++;
185           break;
186         case 'V':
187           puts (version);
188           puts (legal);
189           err_hcf (1);
190         case '?':
191           usage ();
192           assert (0);
193         case 0:
194           break;
195         default:
196           assert (0);
197         }
198     }
199
200
201   if (testing_mode)
202     {
203       /* FIXME: Later this option should do some other things, too. */
204       force_long_view();
205       test_mode = 1;
206     }
207     
208
209   for (i = optind; i < argc; i++)
210     {
211       int separate = 1;
212
213       if (!strcmp (argv[i], "+"))
214         {
215           separate = 0;
216           if (++i >= argc)
217             usage ();
218         }
219       else if (strchr (argv[i], '='))
220         {
221           outp_configure_macro (argv[i]);
222           continue;
223         }
224       getl_add_file (argv[i], separate, 0);
225     }
226   if (getl_head)
227     getl_head->separate = 0;
228
229   if (getl_am_interactive)
230     getl_interactive = 1;
231
232   if (!no_statrc)
233     {
234       char *pspprc_fn = fn_search_path ("rc", config_path, NULL);
235
236       if (pspprc_fn)
237         getl_add_file (pspprc_fn, 0, 1);
238
239       free (pspprc_fn);
240     }
241 }
242
243 /* Message that describes PSPP command-line syntax. */
244 static const char pre_syntax_message[] =
245 N_("PSPP, a program for statistical analysis of sample data.\n"
246 "\nUsage: %s [OPTION]... FILE...\n"
247 "\nIf a long option shows an argument as mandatory, then it is mandatory\n"
248 "for the equivalent short option also.  Similarly for optional arguments.\n"
249 "\nConfiguration:\n"
250 "  -a, --algorithm={compatible|enhanced}\n"
251 "                            set to `compatible' if you want output\n"
252 "                            calculated from broken algorithms\n"
253 "  -B, --config-dir=DIR      set configuration directory to DIR\n"
254 "  -o, --device=DEVICE       select output driver DEVICE and disable defaults\n"
255 "  -d, --define=VAR[=VALUE]  set environment variable VAR to VALUE, or empty\n"
256 "  -u, --undef=VAR           undefine environment variable VAR\n"
257 "\nInput and output:\n"
258 "  -f, --out-file=FILE       send output to FILE (overwritten)\n"
259 "  -p, --pipe                read script from stdin, send output to stdout\n"
260 "  -I-, --no-include         clear include path\n"
261 "  -I, --include=DIR         append DIR to include path\n"
262 "  -c, --command=COMMAND     execute COMMAND before .pspp/rc at startup\n"
263 "\nLanguage modifiers:\n"
264 "  -i, --interactive         interpret scripts in interactive mode\n"
265 "  -n, --edit                just check syntax; don't actually run the code\n"
266 "  -r, --no-statrc           disable execution of .pspp/rc at startup\n"
267 "  -s, --safer               don't allow some unsafe operations\n"
268 "  -x, --syntax={compatible|enhanced}\n"
269 "                            set to `compatible' if you want only to accept\n"
270 "                            spss compatible syntax\n"
271 "\nInformative output:\n"
272 "  -h, --help                print this help, then exit\n"
273 "  -l, --list                print a list of known driver classes, then exit\n"
274 "  -V, --version             show PSPP version, then exit\n"
275 "  -v, --verbose             increments verbosity level\n"
276 "\nNon-option arguments:\n"
277 " FILE1 FILE2                run FILE1, clear the dictionary, run FILE2\n"
278 " FILE1 + FILE2              run FILE1 then FILE2 without clearing dictionary\n"
279 " KEY=VALUE                  overrides macros in output initialization file\n"
280 "\n");
281
282 /* Message that describes PSPP command-line syntax, continued. */
283 static const char post_syntax_message[] = N_("\nReport bugs to <%s>.\n");
284
285 /* Writes a syntax description to stdout and terminates. */
286 static void
287 usage (void)
288 {
289   printf (gettext (pre_syntax_message), pgmname);
290   outp_list_classes ();
291   printf (gettext (post_syntax_message),PACKAGE_BUGREPORT);
292
293   err_hcf (1);
294 }