Commit | Line | Data |
---|---|---|
8c5bb729 | 1 | /* Copyright (C) 1995,1996,1998,2000,2001, 2006, 2008, 2009, 2010, 2011 Free Software Foundation |
1a413ab9 | 2 | * |
73be1d9e | 3 | * This library is free software; you can redistribute it and/or |
53befeb7 NJ |
4 | * modify it under the terms of the GNU Lesser General Public License |
5 | * as published by the Free Software Foundation; either version 3 of | |
6 | * the License, or (at your option) any later version. | |
1a413ab9 | 7 | * |
53befeb7 NJ |
8 | * This library is distributed in the hope that it will be useful, but |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
73be1d9e MV |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
11 | * Lesser General Public License for more details. | |
1a413ab9 | 12 | * |
73be1d9e MV |
13 | * You should have received a copy of the GNU Lesser General Public |
14 | * License along with this library; if not, write to the Free Software | |
53befeb7 NJ |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
16 | * 02110-1301 USA | |
73be1d9e | 17 | */ |
6e8d25a6 | 18 | |
1a413ab9 | 19 | \f |
dbb605f5 LC |
20 | #ifdef HAVE_CONFIG_H |
21 | # include <config.h> | |
22 | #endif | |
1a413ab9 | 23 | |
a0599745 | 24 | #include "libguile/_scm.h" |
11d49f54 | 25 | #include "libguile/mallocs.h" |
a0599745 | 26 | #include "libguile/strings.h" |
1a413ab9 | 27 | |
a0599745 | 28 | #include "libguile/options.h" |
1a413ab9 MD |
29 | \f |
30 | ||
31 | /* {Run-time options} | |
32 | * | |
33 | * This is the basic interface for low-level configuration of the | |
34 | * Guile library. It is used for configuring the reader, evaluator, | |
35 | * printer and debugger. | |
cbff1d89 MD |
36 | * |
37 | * Motivation: | |
38 | * | |
39 | * 1. Altering option settings can have side effects. | |
40 | * 2. Option values can be stored in native format. | |
41 | * (Important for efficiency in, e. g., the evaluator.) | |
42 | * 3. Doesn't use up name space. | |
43 | * 4. Options can be naturally grouped => ease of use. | |
1a413ab9 MD |
44 | */ |
45 | ||
cbff1d89 MD |
46 | /* scm_options is the core of all options interface procedures. |
47 | * | |
48 | * Some definitions: | |
49 | * | |
50 | * Run time options in Guile are arranged in groups. Each group | |
51 | * affects a certain aspect of the behaviour of the library. | |
52 | * | |
53 | * An "options interface procedure" manages one group of options. It | |
54 | * can be used to check or set options, or to get documentation for | |
55 | * all options of a group. The options interface procedure is not | |
56 | * intended to be called directly by the user. The user should | |
57 | * instead call | |
58 | * | |
59 | * (<group>-options) | |
60 | * (<group>-options 'help) | |
61 | * (<group>-options 'full) | |
62 | * | |
63 | * to display current option settings (The second version also | |
64 | * displays documentation. The third version also displays | |
65 | * information about programmer's options.), and | |
66 | * | |
67 | * (<group>-enable '<option-symbol>) | |
68 | * (<group>-disable '<option-symbol>) | |
69 | * (<group>-set! <option-symbol> <value>) | |
70 | * (<group>-options <option setting>) | |
71 | * | |
72 | * to alter the state of an option (The last version sets all | |
73 | * options according to <option setting>.) where <group> is the name | |
74 | * of the option group. | |
75 | * | |
76 | * An "option setting" represents the state of all low-level options | |
77 | * managed by one options interface procedure. It is a list of | |
78 | * single symbols and symbols followed by a value. | |
79 | * | |
80 | * For boolean options, the presence of the symbol of that option in | |
81 | * the option setting indicates a true value. If the symbol isn't a | |
a0fcb308 | 82 | * member of the option setting this represents a false value. |
cbff1d89 | 83 | * |
a0fcb308 | 84 | * Other options are represented by a symbol followed by the value. |
cbff1d89 MD |
85 | * |
86 | * If scm_options is called without arguments, the current option | |
87 | * setting is returned. If the argument is an option setting, options | |
88 | * are altered and the old setting is returned. If the argument isn't | |
89 | * a list, a list of sublists is returned, where each sublist contains | |
90 | * option name, value and documentation string. | |
91 | */ | |
92 | ||
93 | SCM_SYMBOL (scm_yes_sym, "yes"); | |
94 | SCM_SYMBOL (scm_no_sym, "no"); | |
95 | ||
e11e83f3 MV |
96 | /* Return a list of the current option setting. The format of an |
97 | * option setting is described in the above documentation. */ | |
11d49f54 | 98 | static SCM |
62560650 | 99 | get_option_setting (const scm_t_option options[]) |
11d49f54 DH |
100 | { |
101 | unsigned int i; | |
102 | SCM ls = SCM_EOL; | |
62560650 | 103 | for (i = 0; options[i].name; ++i) |
11d49f54 DH |
104 | { |
105 | switch (options[i].type) | |
106 | { | |
107 | case SCM_OPTION_BOOLEAN: | |
108 | if (options[i].val) | |
109 | ls = scm_cons (SCM_PACK (options[i].name), ls); | |
110 | break; | |
111 | case SCM_OPTION_INTEGER: | |
e11e83f3 | 112 | ls = scm_cons (scm_from_unsigned_integer (options[i].val), ls); |
11d49f54 DH |
113 | ls = scm_cons (SCM_PACK (options[i].name), ls); |
114 | break; | |
115 | case SCM_OPTION_SCM: | |
116 | ls = scm_cons (SCM_PACK (options[i].val), ls); | |
117 | ls = scm_cons (SCM_PACK (options[i].name), ls); | |
118 | } | |
119 | } | |
120 | return ls; | |
121 | } | |
122 | ||
123 | ||
124 | /* Return a list of sublists, where each sublist contains option name, value | |
125 | * and documentation string. */ | |
126 | static SCM | |
62560650 | 127 | get_documented_option_setting (const scm_t_option options[]) |
1a413ab9 | 128 | { |
11d49f54 DH |
129 | SCM ans = SCM_EOL; |
130 | unsigned int i; | |
131 | ||
62560650 | 132 | for (i = 0; options[i].name; ++i) |
1a413ab9 | 133 | { |
25d50a05 | 134 | SCM ls = scm_cons (scm_from_utf8_string (options[i].doc), SCM_EOL); |
cbff1d89 MD |
135 | switch (options[i].type) |
136 | { | |
137 | case SCM_OPTION_BOOLEAN: | |
11d49f54 | 138 | ls = scm_cons (options[i].val ? scm_yes_sym : scm_no_sym, ls); |
cbff1d89 MD |
139 | break; |
140 | case SCM_OPTION_INTEGER: | |
e11e83f3 | 141 | ls = scm_cons (scm_from_unsigned_integer (options[i].val), ls); |
cbff1d89 MD |
142 | break; |
143 | case SCM_OPTION_SCM: | |
11d49f54 | 144 | ls = scm_cons (SCM_PACK (options[i].val), ls); |
cbff1d89 | 145 | } |
11d49f54 DH |
146 | ls = scm_cons (SCM_PACK (options[i].name), ls); |
147 | ans = scm_cons (ls, ans); | |
148 | } | |
edca9d6e | 149 | return scm_reverse_x (ans, SCM_UNDEFINED); |
11d49f54 DH |
150 | } |
151 | ||
152 | ||
62560650 HWN |
153 | static int |
154 | options_length (scm_t_option options[]) | |
155 | { | |
156 | unsigned int i = 0; | |
157 | for (; options[i].name != NULL; ++i) | |
158 | ; | |
159 | ||
160 | return i; | |
161 | } | |
162 | ||
11d49f54 DH |
163 | /* Alters options according to the given option setting 'args'. The value of |
164 | * args is known to be a list, but it is not known whether the list is a well | |
165 | * formed option setting, i. e. if for every non-boolean option a value is | |
166 | * given. For this reason, the function applies all changes to a copy of the | |
167 | * original setting in memory. Only if 'args' was successfully processed, | |
03347a97 HWN |
168 | * the new setting will overwrite the old one. |
169 | * | |
170 | * If DRY_RUN is set, don't change anything. This is useful for trying out an option | |
171 | * before entering a critical section. | |
172 | */ | |
11d49f54 | 173 | static void |
03347a97 HWN |
174 | change_option_setting (SCM args, scm_t_option options[], const char *s, |
175 | int dry_run) | |
11d49f54 DH |
176 | { |
177 | unsigned int i; | |
086063aa AW |
178 | scm_t_bits *new_vals; |
179 | ||
180 | new_vals = scm_gc_malloc (options_length (options) * sizeof (scm_t_bits), | |
181 | "new-options"); | |
11d49f54 | 182 | |
62560650 | 183 | for (i = 0; options[i].name; ++i) |
11d49f54 DH |
184 | { |
185 | if (options[i].type == SCM_OPTION_BOOLEAN) | |
086063aa | 186 | new_vals[i] = 0; |
11d49f54 | 187 | else |
086063aa | 188 | new_vals[i] = options[i].val; |
cbff1d89 | 189 | } |
11d49f54 | 190 | |
c96d76b8 | 191 | while (!SCM_NULL_OR_NIL_P (args)) |
cbff1d89 | 192 | { |
11d49f54 DH |
193 | SCM name = SCM_CAR (args); |
194 | int found = 0; | |
195 | ||
62560650 | 196 | for (i = 0; options[i].name && !found; ++i) |
1a413ab9 | 197 | { |
bc36d050 | 198 | if (scm_is_eq (name, SCM_PACK (options[i].name))) |
11d49f54 | 199 | { |
1a413ab9 MD |
200 | switch (options[i].type) |
201 | { | |
202 | case SCM_OPTION_BOOLEAN: | |
086063aa | 203 | new_vals[i] = 1; |
11d49f54 | 204 | break; |
1a413ab9 | 205 | case SCM_OPTION_INTEGER: |
11d49f54 | 206 | args = SCM_CDR (args); |
086063aa | 207 | new_vals[i] = scm_to_size_t (scm_car (args)); |
11d49f54 | 208 | break; |
cbff1d89 | 209 | case SCM_OPTION_SCM: |
11d49f54 | 210 | args = SCM_CDR (args); |
086063aa | 211 | new_vals[i] = SCM_UNPACK (scm_car (args)); |
11d49f54 | 212 | break; |
1a413ab9 | 213 | } |
11d49f54 DH |
214 | found = 1; |
215 | } | |
1a413ab9 | 216 | } |
11d49f54 DH |
217 | |
218 | if (!found) | |
219 | scm_misc_error (s, "Unknown option name: ~S", scm_list_1 (name)); | |
220 | ||
221 | args = SCM_CDR (args); | |
222 | } | |
223 | ||
03347a97 HWN |
224 | if (dry_run) |
225 | return; | |
226 | ||
62560650 | 227 | for (i = 0; options[i].name; ++i) |
086063aa | 228 | options[i].val = new_vals[i]; |
11d49f54 DH |
229 | } |
230 | ||
231 | ||
232 | SCM | |
62560650 | 233 | scm_options (SCM args, scm_t_option options[], const char *s) |
03347a97 HWN |
234 | { |
235 | return scm_options_try (args, options, s, 0); | |
236 | } | |
237 | ||
238 | SCM | |
239 | scm_options_try (SCM args, scm_t_option options[], const char *s, | |
240 | int dry_run) | |
11d49f54 DH |
241 | { |
242 | if (SCM_UNBNDP (args)) | |
62560650 | 243 | return get_option_setting (options); |
d2e53ed6 | 244 | else if (!SCM_NULL_OR_NIL_P (args) && !scm_is_pair (args)) |
11d49f54 DH |
245 | /* Dirk:FIXME:: This criterion should be improved. IMO it is better to |
246 | * demand that args is #t if documentation should be shown than to say | |
247 | * that every argument except a list will print out documentation. */ | |
62560650 | 248 | return get_documented_option_setting (options); |
11d49f54 DH |
249 | else |
250 | { | |
251 | SCM old_setting; | |
7888309b | 252 | SCM_ASSERT (scm_is_true (scm_list_p (args)), args, 1, s); |
62560650 | 253 | old_setting = get_option_setting (options); |
03347a97 | 254 | change_option_setting (args, options, s, dry_run); |
11d49f54 | 255 | return old_setting; |
1a413ab9 | 256 | } |
1a413ab9 MD |
257 | } |
258 | ||
1cc91f1b | 259 | |
1a413ab9 | 260 | void |
62560650 | 261 | scm_init_opts (SCM (*func) (SCM), scm_t_option options[]) |
1a413ab9 | 262 | { |
11d49f54 | 263 | unsigned int i; |
1a413ab9 | 264 | |
62560650 | 265 | for (i = 0; options[i].name; ++i) |
cbff1d89 | 266 | { |
25d50a05 | 267 | SCM name = scm_from_utf8_symbol (options[i].name); |
11d49f54 | 268 | options[i].name = (char *) SCM_UNPACK (name); |
cbff1d89 | 269 | } |
1a413ab9 MD |
270 | func (SCM_UNDEFINED); |
271 | } | |
272 | ||
1cc91f1b | 273 | |
1a413ab9 MD |
274 | void |
275 | scm_init_options () | |
1a413ab9 | 276 | { |
a0599745 | 277 | #include "libguile/options.x" |
1a413ab9 | 278 | } |
89e00824 ML |
279 | |
280 | /* | |
281 | Local Variables: | |
282 | c-file-style: "gnu" | |
283 | End: | |
284 | */ |