Rewrite PSPP output engine.
[pspp-builds.git] / src / ui / source-init-opts.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2008  Free Software Foundation
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18 #include <argp.h>
19 #include "source-init-opts.h"
20 #include <stdbool.h>
21 #include <xalloc.h>
22 #include <string.h>
23 #include <data/file-name.h>
24 #include <libpspp/getl.h>
25 #include <language/syntax-file.h>
26 #include <stdlib.h>
27 #include <libpspp/llx.h>
28 #include <data/por-file-reader.h>
29 #include <data/sys-file-reader.h>
30 #include <libpspp/message.h>
31 #include <ui/syntax-gen.h>
32 #include <language/syntax-string-source.h>
33 #include <data/file-name.h>
34 #include <data/settings.h>
35
36 #include "gettext.h"
37 #define _(msgid) gettext (msgid)
38 #define N_(msgid) msgid
39
40 static const struct argp_option post_init_options [] = {
41   {"algorithm", 'a', "{compatible|enhanced}", 0, N_("set to `compatible' if you want output calculated from broken algorithms"), 0},
42   {"include", 'I', "DIR", 0, N_("Append DIR to include path"), 0},
43   {"no-include", 'I', 0, 0, N_("Clear include path"), 0},
44   {"no-statrc", 'r', 0, 0, N_("Disable execution of .pspp/rc at startup"), 0},
45   {"config-dir", 'B', "DIR", 0, N_("Set configuration directory to DIR"), 0},
46   {"safer", 's', 0, 0,  N_("Don't allow some unsafe operations"), 0},
47   {"syntax", 'x', "{compatible|enhanced}", 0, N_("Set to `compatible' if you want only to accept SPSS compatible syntax"), 0},
48   { 0, 0, 0, 0, 0, 0 }
49 };
50
51 static error_t
52 parse_post_init_opts (int key, char *arg, struct argp_state *state)
53 {
54   struct source_init
55   {
56     bool process_statrc;
57   };
58
59   struct source_init *sip = state->hook;
60
61   struct source_stream *ss = state->input;
62
63   if ( state->input == NULL)
64     return 0;
65
66   switch (key)
67     {
68     case ARGP_KEY_INIT:
69       state->hook = sip = xzalloc (sizeof (struct source_init));
70       sip->process_statrc = true;
71       break;
72     case ARGP_KEY_FINI:
73       free (sip);
74       break;
75     case  'a':
76       if ( 0 == strcmp (arg, "compatible") )
77         settings_set_algorithm (COMPATIBLE);
78       else if ( 0 == strcmp (arg, "enhanced"))
79         settings_set_algorithm (ENHANCED);
80       else
81         {
82           argp_failure (state, 1, 0, _("Algorithm must be either \"compatible\" or \"enhanced\"."));
83         }
84       break;
85     case 'B':
86       config_path = arg;
87       break;
88     case 'I':
89       if (arg == NULL || !strcmp (arg, "-"))
90         getl_clear_include_path (ss);
91       else
92         getl_add_include_dir (ss, arg);
93       break;
94     case 'r':
95       sip->process_statrc = false;
96       break;
97     case ARGP_KEY_SUCCESS:
98       if (sip->process_statrc)
99         {
100           char *pspprc_fn = fn_search_path ("rc", config_path);
101           if (pspprc_fn != NULL)
102             {
103               getl_append_source (ss,
104                                   create_syntax_file_source (pspprc_fn),
105                                   GETL_BATCH,
106                                   ERRMODE_CONTINUE
107                                   );
108
109               free (pspprc_fn);
110             }
111         }
112       break;
113     case 's':
114       settings_set_safer_mode ();
115       break;
116     case 'x':
117       if ( 0 == strcmp (arg, "compatible") )
118         settings_set_syntax (COMPATIBLE);
119       else if ( 0 == strcmp (arg, "enhanced"))
120         settings_set_syntax (ENHANCED);
121       else
122         {
123           argp_failure (state, 1, 0, _("Syntax must be either \"compatible\" or \"enhanced\"."));
124         }
125       break;
126     default:
127       return ARGP_ERR_UNKNOWN;
128     }
129
130   return 0;
131 }
132
133 const struct argp post_init_argp =
134   {post_init_options, parse_post_init_opts, 0, 0, 0, 0, 0};
135