Apply patch #5225, assertions.
[pspp-builds.git] / src / ui / terminal / command-line.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 "command-line.h"
22 #include <libpspp/message.h>
23 #include <ctype.h>
24 #include <stdio.h>
25 #include <errno.h>
26 #include <getopt.h>
27 #include <stdlib.h>
28 #include <libpspp/alloc.h>
29 #include <libpspp/assertion.h>
30 #include <libpspp/copyleft.h>
31 #include <libpspp/message.h>
32 #include <language/line-buffer.h>
33 #include "progname.h"
34 #include <data/settings.h>
35 #include "read-line.h"
36 #include <output/output.h>
37 #include <data/file-name.h>
38 #include <libpspp/str.h>
39 #include <libpspp/version.h>
40 #include <libpspp/verbose-msg.h>
41
42 #include "gettext.h"
43 #define _(msgid) gettext (msgid)
44 #define N_(msgid) msgid
45
46 void welcome (void);
47 static void usage (void);
48
49 char *subst_vars (char *);
50
51 /* Parses the command line specified by ARGC and ARGV as received by
52    main().  Returns true if normal execution should proceed,
53    false if the command-line indicates that PSPP should exit. */
54 bool
55 parse_command_line (int argc, char **argv)
56 {
57   static struct option long_options[] =
58   {
59     {"algorithm", required_argument, NULL, 'a'},
60     {"command", required_argument, NULL, 'c'},
61     {"config-directory", required_argument, NULL, 'B'},
62     {"device", required_argument, NULL, 'o'},
63     {"dry-run", no_argument, NULL, 'n'},
64     {"edit", no_argument, NULL, 'n'},
65     {"help", no_argument, NULL, 'h'},
66     {"include-directory", required_argument, NULL, 'I'},
67     {"interactive", no_argument, NULL, 'i'},
68     {"just-print", no_argument, NULL, 'n'},
69     {"list", no_argument, NULL, 'l'},
70     {"no-include", no_argument, NULL, 'I'},
71     {"no-statrc", no_argument, NULL, 'r'},
72     {"out-file", required_argument, NULL, 'f'},
73     {"pipe", no_argument, NULL, 'p'},
74     {"recon", no_argument, NULL, 'n'},
75     {"safer", no_argument, NULL, 's'},
76     {"syntax", required_argument, NULL, 'x'},
77     {"testing-mode", no_argument, NULL, 'T'},
78     {"verbose", no_argument, NULL, 'v'},
79     {"version", no_argument, NULL, 'V'},
80     {0, 0, 0, 0},
81   };
82
83   int c, i;
84
85   bool cleared_device_defaults = false;
86   bool process_statrc = true;
87   bool interactive_mode = false;
88   int syntax_files = 0;
89
90   for (;;)
91     {
92       c = getopt_long (argc, argv, "a:x:B:c:f:hiI:lno:prsvV", long_options, NULL);
93       if (c == -1)
94         break;
95
96       switch (c)
97         {
98           /* Compatibility options */
99         case 'a':
100           if ( 0 == strcmp(optarg,"compatible") )
101               set_algorithm(COMPATIBLE);
102           else if ( 0 == strcmp(optarg,"enhanced"))
103               set_algorithm(ENHANCED);
104           else
105             {
106               usage ();
107               return false;
108             }
109           break;
110
111         case 'x':         
112           if ( 0 == strcmp(optarg,"compatible") )
113             set_syntax(COMPATIBLE);
114           else if ( 0 == strcmp(optarg,"enhanced"))
115             set_syntax(ENHANCED);
116           else
117             {
118               usage ();
119               return false;
120             }
121           break;
122
123         case 'B':
124           config_path = optarg;
125           break;
126         case 'f':
127           printf (_("%s is not yet implemented."), "-f");
128           putchar('\n');
129           break;
130         case 'h':
131           usage ();
132           return false;
133         case 'i':
134           interactive_mode = true;
135           break;
136         case 'I':
137           if (optarg == NULL || !strcmp (optarg, "-"))
138             getl_clear_include_path ();
139           else
140             getl_add_include_dir (optarg);
141           break;
142         case 'l':
143           outp_list_classes ();
144           return false;
145         case 'n':
146           printf (_("%s is not yet implemented."),"-n");
147           putchar('\n');
148           break;
149         case 'o':
150           if (!cleared_device_defaults)
151             {
152               outp_configure_clear ();
153               cleared_device_defaults = true;
154             }
155           outp_configure_add (optarg);
156           break;
157         case 'p':
158           printf (_("%s is not yet implemented."),"-p");
159           putchar('\n');
160           break;
161         case 'r':
162           process_statrc = false;
163           break;
164         case 's':
165           set_safer_mode ();
166           break;
167         case 'v':
168           verbose_increment_level ();
169           break;
170         case 'V':
171           puts (version);
172           puts (legal);
173           return false;
174         case 'T':
175           force_long_view ();
176           set_testing_mode (true);
177           break;
178         case '?':
179           usage ();
180           return false;
181         case 0:
182           break;
183         default:
184           NOT_REACHED ();
185         }
186     }
187
188   if (process_statrc)
189     {
190       char *pspprc_fn = fn_search_path ("rc", config_path, NULL);
191       if (pspprc_fn != NULL) 
192         {
193           getl_append_syntax_file (pspprc_fn);
194           free (pspprc_fn); 
195         }
196     }
197
198   for (i = optind; i < argc; i++)
199     if (strchr (argv[i], '='))
200       outp_configure_macro (argv[i]);
201     else 
202       {
203         getl_append_syntax_file (argv[i]);
204         syntax_files++;
205       }
206
207   if (!syntax_files || interactive_mode)
208     getl_append_interactive (readln_read);
209
210   return true;
211 }
212
213 /* Message that describes PSPP command-line syntax. */
214 static const char pre_syntax_message[] =
215 N_("PSPP, a program for statistical analysis of sample data.\n"
216 "\nUsage: %s [OPTION]... FILE...\n"
217 "\nIf a long option shows an argument as mandatory, then it is mandatory\n"
218 "for the equivalent short option also.  Similarly for optional arguments.\n"
219 "\nConfiguration:\n"
220 "  -a, --algorithm={compatible|enhanced}\n"
221 "                            set to `compatible' if you want output\n"
222 "                            calculated from broken algorithms\n"
223 "  -B, --config-dir=DIR      set configuration directory to DIR\n"
224 "  -o, --device=DEVICE       select output driver DEVICE and disable defaults\n"
225 "\nInput and output:\n"
226 "  -f, --out-file=FILE       send output to FILE (overwritten)\n"
227 "  -p, --pipe                read script from stdin, send output to stdout\n"
228 "  -I-, --no-include         clear include path\n"
229 "  -I, --include=DIR         append DIR to include path\n"
230 "\nLanguage modifiers:\n"
231 "  -i, --interactive         interpret scripts in interactive mode\n"
232 "  -n, --edit                just check syntax; don't actually run the code\n"
233 "  -r, --no-statrc           disable execution of .pspp/rc at startup\n"
234 "  -s, --safer               don't allow some unsafe operations\n"
235 "  -x, --syntax={compatible|enhanced}\n"
236 "                            set to `compatible' if you want only to accept\n"
237 "                            spss compatible syntax\n"
238 "\nInformative output:\n"
239 "  -h, --help                print this help, then exit\n"
240 "  -l, --list                print a list of known driver classes, then exit\n"
241 "  -V, --version             show PSPP version, then exit\n"
242 "  -v, --verbose             increments verbosity level\n"
243 "\nNon-option arguments:\n"
244 " FILE                       syntax file to execute\n"
245 " KEY=VALUE                  overrides macros in output initialization file\n"
246 "\n");
247
248 /* Message that describes PSPP command-line syntax, continued. */
249 static const char post_syntax_message[] = N_("\nReport bugs to <%s>.\n");
250
251 /* Writes a syntax description to stdout. */
252 static void
253 usage (void)
254 {
255   printf (gettext (pre_syntax_message), program_name);
256   outp_list_classes ();
257   printf (gettext (post_syntax_message), PACKAGE_BUGREPORT);
258 }