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