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