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