Implemented the SHOW command and massaged the SET command to fit
[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 <assert.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     {"command", required_argument, NULL, 'c'},
54     {"config-directory", required_argument, NULL, 'B'},
55     {"device", required_argument, NULL, 'o'},
56     {"dry-run", no_argument, NULL, 'n'},
57     {"edit", no_argument, NULL, 'n'},
58     {"help", no_argument, NULL, 'h'},
59     {"include-directory", required_argument, NULL, 'I'},
60     {"interactive", no_argument, NULL, 'i'},
61     {"just-print", no_argument, NULL, 'n'},
62     {"list", no_argument, NULL, 'l'},
63     {"no-include", no_argument, NULL, 'I'},
64     {"no-statrc", no_argument, NULL, 'r'},
65     {"out-file", required_argument, NULL, 'f'},
66     {"pipe", no_argument, NULL, 'p'},
67     {"recon", no_argument, NULL, 'n'},
68     {"safer", no_argument, NULL, 's'},
69     {"testing-mode", no_argument, &testing_mode, 1},
70     {"verbose", no_argument, NULL, 'v'},
71     {"version", no_argument, NULL, 'V'},
72     {0, 0, 0, 0},
73   };
74
75   int c, i;
76
77   int cleared_device_defaults = 0;
78
79   int no_statrc = 0;
80
81   for (;;)
82     {
83       c = getopt_long (argc, argv, "B:c:f:hiI:lno:prsvV", long_options, NULL);
84       if (c == -1)
85         break;
86
87       switch (c)
88         {
89         case 'c':
90           {
91             static int n_cmds;
92             
93             struct getl_script *script = xmalloc (sizeof *script);
94             
95             {
96               struct getl_line_list *line;
97
98               script->first_line = line = xmalloc (sizeof *line);
99               line->line = xstrdup ("commandline");
100               line->len = --n_cmds;
101               line = line->next = xmalloc (sizeof *line);
102               line->line = xstrdup (optarg);
103               line->len = strlen (optarg);
104               line->next = NULL;
105             }
106
107             getl_add_virtual_file (script);
108           }
109           break;
110         case 'B':
111           config_path = optarg;
112           break;
113         case 'f':
114           printf(_("%s is not yet implemented."), "-f");
115           putchar('\n');
116           break;
117         case 'h':
118           usage ();
119           assert (0);
120         case 'i':
121           getl_interactive = 2;
122           break;
123         case 'I':
124           if (optarg == NULL || !strcmp (optarg, "-"))
125             getl_clear_include_path ();
126           else
127             getl_add_include_dir (optarg);
128           break;
129         case 'l':
130           outp_list_classes ();
131           err_hcf (1);
132         case 'n':
133           printf (_("%s is not yet implemented."),"-n");
134           putchar('\n');
135           break;
136         case 'o':
137           if (!cleared_device_defaults)
138             {
139               outp_configure_clear ();
140               cleared_device_defaults = 1;
141             }
142           outp_configure_add (optarg);
143           break;
144         case 'p':
145           printf (_("%s is not yet implemented."),"-p");
146           putchar('\n');
147           break;
148         case 'r':
149           no_statrc = 1;
150           break;
151         case 's':
152           make_safe();
153           break;
154         case 'v':
155           err_verbosity++;
156           break;
157         case 'V':
158           puts (version);
159           puts (_("\nCopyright (C) 1997-9, 2000 Free Software Foundation, "
160                   "Inc.\n"
161                   "This is free software; see the source for copying "
162                   "conditions.  There is NO\n"
163                   "WARRANTY; not even for MERCHANTABILITY or FITNESS "
164                   "FOR A PARTICULAR PURPOSE.\n\n"
165                   "Written by Ben Pfaff <blp@gnu.org>."));
166           err_hcf (1);
167         case '?':
168           usage ();
169           assert (0);
170         case 0:
171           break;
172         default:
173           assert (0);
174         }
175     }
176
177
178   if (testing_mode)
179     {
180       /* FIXME: Later this option should do some other things, too. */
181       force_long_view();
182     }
183     
184
185   for (i = optind; i < argc; i++)
186     {
187       int separate = 1;
188
189       if (!strcmp (argv[i], "+"))
190         {
191           separate = 0;
192           if (++i >= argc)
193             usage ();
194         }
195       else if (strchr (argv[i], '='))
196         {
197           outp_configure_macro (argv[i]);
198           continue;
199         }
200       getl_add_file (argv[i], separate, 0);
201     }
202   if (getl_head)
203     getl_head->separate = 0;
204
205   if (getl_am_interactive)
206     getl_interactive = 1;
207
208   if (!no_statrc)
209     {
210       char *pspprc_fn = fn_search_path ("rc", config_path, NULL);
211
212       if (pspprc_fn)
213         getl_add_file (pspprc_fn, 0, 1);
214
215       free (pspprc_fn);
216     }
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 "  -B, --config-dir=DIR      set configuration directory to DIR\n"
227 "  -o, --device=DEVICE       select output driver DEVICE and disable defaults\n"
228 "  -d, --define=VAR[=VALUE]  set environment variable VAR to VALUE, or empty\n"
229 "  -u, --undef=VAR           undefine environment variable VAR\n"
230 "\nInput and output:\n"
231 "  -f, --out-file=FILE       send output to FILE (overwritten)\n"
232 "  -p, --pipe                read script 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 "  -c, --command=COMMAND     execute COMMAND before .pspp/rc at startup\n"
236 "\nLanguage modifiers:\n"
237 "  -i, --interactive         interpret scripts in interactive mode\n"
238 "  -n, --edit                just check syntax; don't actually run the code\n"
239 "  -r, --no-statrc           disable execution of .pspp/rc at startup\n"
240 "  -s, --safer               don't allow some unsafe operations\n"
241 "\nInformative output:\n"
242 "  -h, --help                print this help, then exit\n"
243 "  -l, --list                print a list of known driver classes, then exit\n"
244 "  -V, --version             show PSPP version, then exit\n"
245 "  -v, --verbose             increments verbosity level\n"
246 "\nNon-option arguments:\n"
247 " FILE1 FILE2                run FILE1, clear the dictionary, run FILE2\n"
248 " FILE1 + FILE2              run FILE1 then FILE2 without clearing dictionary\n"
249 " KEY=VALUE                  overrides macros in output initialization file\n"
250 "\n");
251
252 /* Message that describes PSPP command-line syntax, continued. */
253 static const char post_syntax_message[] = N_("\nReport bugs to <%s>.\n");
254
255 /* Writes a syntax description to stdout and terminates. */
256 static void
257 usage (void)
258 {
259   printf (gettext (pre_syntax_message), pgmname);
260   outp_list_classes ();
261   printf (gettext (post_syntax_message),PACKAGE_BUGREPORT);
262
263   err_hcf (1);
264 }