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