Merge commit 'origin/stable'
[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 <output/output.h>
24 #include <data/file-name.h>
25 #include <libpspp/getl.h>
26 #include <language/syntax-file.h>
27 #include <stdlib.h>
28 #include <libpspp/llx.h>
29 #include <data/por-file-reader.h>
30 #include <data/sys-file-reader.h>
31 #include <libpspp/message.h>
32 #include <ui/syntax-gen.h>
33 #include <language/syntax-string-source.h>
34 #include <data/file-name.h>
35 #include <data/settings.h>
36
37 #include "gettext.h"
38 #define _(msgid) gettext (msgid)
39 #define N_(msgid) msgid
40
41 static const struct argp_option post_init_options [] = {
42   {"algorithm", 'a', "{compatible|enhanced}", 0, N_("set to `compatible' if you want output calculated from broken algorithms"), 0},
43   {"include", 'I', "DIR", 0, N_("Append DIR to include path"), 0},
44   {"no-include", 'I', 0, 0, N_("Clear include path"), 0},
45   {"no-statrc", 'r', 0, 0, N_("Disable execution of .pspp/rc at startup"), 0},
46   {"config-dir", 'B', "DIR", 0, N_("Set configuration directory to DIR"), 0},
47   {"safer", 's', 0, 0,  N_("Don't allow some unsafe operations"), 0},
48   {"syntax", 'x', "{compatible|enhanced}", 0, N_("Set to `compatible' if you want only to accept SPSS compatible syntax"), 0},
49   { 0, 0, 0, 0, 0, 0 }
50 };
51
52 static error_t
53 parse_post_init_opts (int key, char *arg, struct argp_state *state)
54 {
55   struct source_init
56   {
57     bool process_statrc;
58   };
59
60   struct source_init *sip = state->hook;
61
62   struct source_stream *ss = state->input;
63
64   if ( state->input == NULL)
65     return 0;
66
67   switch (key)
68     {
69     case ARGP_KEY_INIT:
70       state->hook = sip = xzalloc (sizeof (struct source_init));
71       sip->process_statrc = true;
72       break;
73     case ARGP_KEY_FINI:
74       free (sip);
75       break;
76     case  'a':
77       if ( 0 == strcmp (arg, "compatible") )
78         settings_set_algorithm (COMPATIBLE);
79       else if ( 0 == strcmp (arg, "enhanced"))
80         settings_set_algorithm (ENHANCED);
81       else
82         {
83           argp_failure (state, 1, 0, _("Algorithm must be either \"compatible\" or \"enhanced\"."));
84         }
85       break;
86     case 'B':
87       config_path = arg;
88       break;
89     case 'I':
90       if (arg == NULL || !strcmp (arg, "-"))
91         getl_clear_include_path (ss);
92       else
93         getl_add_include_dir (ss, arg);
94       break;
95     case 'r':
96       sip->process_statrc = false;
97       break;
98     case ARGP_KEY_SUCCESS:
99       if (sip->process_statrc)
100         {
101           char *pspprc_fn = fn_search_path ("rc", config_path);
102           if (pspprc_fn != NULL)
103             {
104               getl_append_source (ss,
105                                   create_syntax_file_source (pspprc_fn),
106                                   GETL_BATCH,
107                                   ERRMODE_CONTINUE
108                                   );
109
110               free (pspprc_fn);
111             }
112         }
113       break;
114     case 's':
115       settings_set_safer_mode ();
116       break;
117     case 'x':
118       if ( 0 == strcmp (arg, "compatible") )
119         settings_set_syntax (COMPATIBLE);
120       else if ( 0 == strcmp (arg, "enhanced"))
121         settings_set_syntax (ENHANCED);
122       else
123         {
124           argp_failure (state, 1, 0, _("Syntax must be either \"compatible\" or \"enhanced\"."));
125         }
126       break;
127     default:
128       return ARGP_ERR_UNKNOWN;
129     }
130
131   return 0;
132 }
133
134 const struct argp post_init_argp =
135   {post_init_options, parse_post_init_opts, 0, 0, 0, 0, 0};
136