Commit | Line | Data |
---|---|---|
11d49f54 | 1 | /* Copyright (C) 1995,1996,1998,2000,2001 Free Software Foundation |
1a413ab9 | 2 | * |
73be1d9e MV |
3 | * This library is free software; you can redistribute it and/or |
4 | * modify it under the terms of the GNU Lesser General Public | |
5 | * License as published by the Free Software Foundation; either | |
6 | * version 2.1 of the License, or (at your option) any later version. | |
1a413ab9 | 7 | * |
73be1d9e MV |
8 | * This library is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
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 | |
15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
16 | */ | |
6e8d25a6 | 17 | |
1a413ab9 MD |
18 | \f |
19 | ||
a0599745 | 20 | #include "libguile/_scm.h" |
11d49f54 | 21 | #include "libguile/mallocs.h" |
a0599745 | 22 | #include "libguile/strings.h" |
c96d76b8 | 23 | #include "libguile/lang.h" |
1a413ab9 | 24 | |
a0599745 | 25 | #include "libguile/options.h" |
1a413ab9 MD |
26 | \f |
27 | ||
28 | /* {Run-time options} | |
29 | * | |
30 | * This is the basic interface for low-level configuration of the | |
31 | * Guile library. It is used for configuring the reader, evaluator, | |
32 | * printer and debugger. | |
cbff1d89 MD |
33 | * |
34 | * Motivation: | |
35 | * | |
36 | * 1. Altering option settings can have side effects. | |
37 | * 2. Option values can be stored in native format. | |
38 | * (Important for efficiency in, e. g., the evaluator.) | |
39 | * 3. Doesn't use up name space. | |
40 | * 4. Options can be naturally grouped => ease of use. | |
1a413ab9 MD |
41 | */ |
42 | ||
cbff1d89 MD |
43 | /* scm_options is the core of all options interface procedures. |
44 | * | |
45 | * Some definitions: | |
46 | * | |
47 | * Run time options in Guile are arranged in groups. Each group | |
48 | * affects a certain aspect of the behaviour of the library. | |
49 | * | |
50 | * An "options interface procedure" manages one group of options. It | |
51 | * can be used to check or set options, or to get documentation for | |
52 | * all options of a group. The options interface procedure is not | |
53 | * intended to be called directly by the user. The user should | |
54 | * instead call | |
55 | * | |
56 | * (<group>-options) | |
57 | * (<group>-options 'help) | |
58 | * (<group>-options 'full) | |
59 | * | |
60 | * to display current option settings (The second version also | |
61 | * displays documentation. The third version also displays | |
62 | * information about programmer's options.), and | |
63 | * | |
64 | * (<group>-enable '<option-symbol>) | |
65 | * (<group>-disable '<option-symbol>) | |
66 | * (<group>-set! <option-symbol> <value>) | |
67 | * (<group>-options <option setting>) | |
68 | * | |
69 | * to alter the state of an option (The last version sets all | |
70 | * options according to <option setting>.) where <group> is the name | |
71 | * of the option group. | |
72 | * | |
73 | * An "option setting" represents the state of all low-level options | |
74 | * managed by one options interface procedure. It is a list of | |
75 | * single symbols and symbols followed by a value. | |
76 | * | |
77 | * For boolean options, the presence of the symbol of that option in | |
78 | * the option setting indicates a true value. If the symbol isn't a | |
a0fcb308 | 79 | * member of the option setting this represents a false value. |
cbff1d89 | 80 | * |
a0fcb308 | 81 | * Other options are represented by a symbol followed by the value. |
cbff1d89 MD |
82 | * |
83 | * If scm_options is called without arguments, the current option | |
84 | * setting is returned. If the argument is an option setting, options | |
85 | * are altered and the old setting is returned. If the argument isn't | |
86 | * a list, a list of sublists is returned, where each sublist contains | |
87 | * option name, value and documentation string. | |
88 | */ | |
89 | ||
90 | SCM_SYMBOL (scm_yes_sym, "yes"); | |
91 | SCM_SYMBOL (scm_no_sym, "no"); | |
92 | ||
11d49f54 | 93 | static SCM protected_objects = SCM_EOL; |
1cc91f1b | 94 | |
11d49f54 DH |
95 | |
96 | /* Return a list of the current option setting. The format of an option | |
97 | * setting is described in the above documentation. */ | |
98 | static SCM | |
99 | get_option_setting (const scm_t_option options[], unsigned int n) | |
100 | { | |
101 | unsigned int i; | |
102 | SCM ls = SCM_EOL; | |
103 | for (i = 0; i != n; ++i) | |
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: | |
93ccaef0 | 112 | ls = scm_cons (SCM_I_MAKINUM (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 | |
127 | get_documented_option_setting (const scm_t_option options[], unsigned int n) | |
1a413ab9 | 128 | { |
11d49f54 DH |
129 | SCM ans = SCM_EOL; |
130 | unsigned int i; | |
131 | ||
132 | for (i = 0; i != n; ++i) | |
1a413ab9 | 133 | { |
11d49f54 | 134 | SCM ls = scm_cons (scm_str2string (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: | |
93ccaef0 | 141 | ls = scm_cons (SCM_I_MAKINUM (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 | } | |
149 | return ans; | |
150 | } | |
151 | ||
152 | ||
153 | /* Alters options according to the given option setting 'args'. The value of | |
154 | * args is known to be a list, but it is not known whether the list is a well | |
155 | * formed option setting, i. e. if for every non-boolean option a value is | |
156 | * given. For this reason, the function applies all changes to a copy of the | |
157 | * original setting in memory. Only if 'args' was successfully processed, | |
158 | * the new setting will overwrite the old one. */ | |
159 | static void | |
160 | change_option_setting (SCM args, scm_t_option options[], unsigned int n, const char *s) | |
161 | { | |
162 | unsigned int i; | |
163 | SCM locally_protected_args = args; | |
164 | SCM malloc_obj = scm_malloc_obj (n * sizeof (scm_t_bits)); | |
165 | scm_t_bits *flags = (scm_t_bits *) SCM_MALLOCDATA (malloc_obj); | |
166 | ||
167 | for (i = 0; i != n; ++i) | |
168 | { | |
169 | if (options[i].type == SCM_OPTION_BOOLEAN) | |
170 | flags[i] = 0; | |
171 | else | |
172 | flags[i] = options[i].val; | |
cbff1d89 | 173 | } |
11d49f54 | 174 | |
c96d76b8 | 175 | while (!SCM_NULL_OR_NIL_P (args)) |
cbff1d89 | 176 | { |
11d49f54 DH |
177 | SCM name = SCM_CAR (args); |
178 | int found = 0; | |
179 | ||
180 | for (i = 0; i != n && !found; ++i) | |
1a413ab9 | 181 | { |
11d49f54 DH |
182 | if (SCM_EQ_P (name, SCM_PACK (options[i].name))) |
183 | { | |
1a413ab9 MD |
184 | switch (options[i].type) |
185 | { | |
186 | case SCM_OPTION_BOOLEAN: | |
187 | flags[i] = 1; | |
11d49f54 | 188 | break; |
1a413ab9 | 189 | case SCM_OPTION_INTEGER: |
11d49f54 DH |
190 | args = SCM_CDR (args); |
191 | SCM_ASSERT (SCM_CONSP (args), args, SCM_ARG1, s); | |
192 | SCM_ASSERT (SCM_INUMP (SCM_CAR (args)), args, SCM_ARG1, s); | |
193 | flags[i] = SCM_INUM (SCM_CAR (args)); | |
194 | break; | |
cbff1d89 | 195 | case SCM_OPTION_SCM: |
11d49f54 DH |
196 | args = SCM_CDR (args); |
197 | SCM_ASSERT (SCM_CONSP (args), args, SCM_ARG1, s); | |
198 | flags[i] = SCM_UNPACK (SCM_CAR (args)); | |
199 | break; | |
1a413ab9 | 200 | } |
11d49f54 DH |
201 | found = 1; |
202 | } | |
1a413ab9 | 203 | } |
11d49f54 DH |
204 | |
205 | if (!found) | |
206 | scm_misc_error (s, "Unknown option name: ~S", scm_list_1 (name)); | |
207 | ||
208 | args = SCM_CDR (args); | |
209 | } | |
210 | ||
211 | for (i = 0; i != n; ++i) | |
212 | { | |
213 | if (options[i].type == SCM_OPTION_SCM) | |
263a691f | 214 | { |
11d49f54 DH |
215 | SCM old = SCM_PACK (options[i].val); |
216 | SCM new = SCM_PACK (flags[i]); | |
217 | if (!SCM_IMP (old)) | |
218 | protected_objects = scm_delq1_x (old, protected_objects); | |
219 | if (!SCM_IMP (new)) | |
220 | protected_objects = scm_cons (new, protected_objects); | |
263a691f | 221 | } |
11d49f54 DH |
222 | options[i].val = flags[i]; |
223 | } | |
224 | ||
225 | scm_remember_upto_here_2 (locally_protected_args, malloc_obj); | |
226 | } | |
227 | ||
228 | ||
229 | SCM | |
230 | scm_options (SCM args, scm_t_option options[], unsigned int n, const char *s) | |
231 | { | |
232 | if (SCM_UNBNDP (args)) | |
233 | return get_option_setting (options, n); | |
c96d76b8 | 234 | else if (!SCM_NULL_OR_NIL_P (args) && !SCM_CONSP (args)) |
11d49f54 DH |
235 | /* Dirk:FIXME:: This criterion should be improved. IMO it is better to |
236 | * demand that args is #t if documentation should be shown than to say | |
237 | * that every argument except a list will print out documentation. */ | |
238 | return get_documented_option_setting (options, n); | |
239 | else | |
240 | { | |
241 | SCM old_setting; | |
7888309b | 242 | SCM_ASSERT (scm_is_true (scm_list_p (args)), args, 1, s); |
11d49f54 DH |
243 | old_setting = get_option_setting (options, n); |
244 | change_option_setting (args, options, n, s); | |
245 | return old_setting; | |
1a413ab9 | 246 | } |
1a413ab9 MD |
247 | } |
248 | ||
1cc91f1b | 249 | |
1a413ab9 | 250 | void |
11d49f54 | 251 | scm_init_opts (SCM (*func) (SCM), scm_t_option options[], unsigned int n) |
1a413ab9 | 252 | { |
11d49f54 | 253 | unsigned int i; |
1a413ab9 | 254 | |
11d49f54 | 255 | for (i = 0; i != n; ++i) |
cbff1d89 | 256 | { |
11d49f54 DH |
257 | SCM name = scm_str2symbol (options[i].name); |
258 | options[i].name = (char *) SCM_UNPACK (name); | |
85db4a2c | 259 | scm_permanent_object (name); |
cbff1d89 | 260 | } |
1a413ab9 MD |
261 | func (SCM_UNDEFINED); |
262 | } | |
263 | ||
1cc91f1b | 264 | |
1a413ab9 MD |
265 | void |
266 | scm_init_options () | |
1a413ab9 | 267 | { |
11d49f54 DH |
268 | scm_gc_register_root (&protected_objects); |
269 | ||
a0599745 | 270 | #include "libguile/options.x" |
1a413ab9 | 271 | } |
89e00824 ML |
272 | |
273 | /* | |
274 | Local Variables: | |
275 | c-file-style: "gnu" | |
276 | End: | |
277 | */ |