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