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