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