* tags.h, deprecated.h (SCM_EQ_P): Deprecated by moving it into
[bpt/guile.git] / libguile / options.c
1 /* Copyright (C) 1995,1996,1998,2000,2001 Free Software Foundation
2 *
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.
7 *
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.
12 *
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 */
17
18 \f
19
20 #include "libguile/_scm.h"
21 #include "libguile/mallocs.h"
22 #include "libguile/strings.h"
23 #include "libguile/lang.h"
24
25 #include "libguile/options.h"
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.
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.
41 */
42
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
79 * member of the option setting this represents a false value.
80 *
81 * Other options are represented by a symbol followed by the value.
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
93 static SCM protected_objects = SCM_EOL;
94
95 /* Return a list of the current option setting. The format of an
96 * option setting is described in the above documentation. */
97 static SCM
98 get_option_setting (const scm_t_option options[], unsigned int n)
99 {
100 unsigned int i;
101 SCM ls = SCM_EOL;
102 for (i = 0; i != n; ++i)
103 {
104 switch (options[i].type)
105 {
106 case SCM_OPTION_BOOLEAN:
107 if (options[i].val)
108 ls = scm_cons (SCM_PACK (options[i].name), ls);
109 break;
110 case SCM_OPTION_INTEGER:
111 ls = scm_cons (scm_from_unsigned_integer (options[i].val), ls);
112 ls = scm_cons (SCM_PACK (options[i].name), ls);
113 break;
114 case SCM_OPTION_SCM:
115 ls = scm_cons (SCM_PACK (options[i].val), ls);
116 ls = scm_cons (SCM_PACK (options[i].name), ls);
117 }
118 }
119 return ls;
120 }
121
122
123 /* Return a list of sublists, where each sublist contains option name, value
124 * and documentation string. */
125 static SCM
126 get_documented_option_setting (const scm_t_option options[], unsigned int n)
127 {
128 SCM ans = SCM_EOL;
129 unsigned int i;
130
131 for (i = 0; i != n; ++i)
132 {
133 SCM ls = scm_cons (scm_str2string (options[i].doc), SCM_EOL);
134 switch (options[i].type)
135 {
136 case SCM_OPTION_BOOLEAN:
137 ls = scm_cons (options[i].val ? scm_yes_sym : scm_no_sym, ls);
138 break;
139 case SCM_OPTION_INTEGER:
140 ls = scm_cons (scm_from_unsigned_integer (options[i].val), ls);
141 break;
142 case SCM_OPTION_SCM:
143 ls = scm_cons (SCM_PACK (options[i].val), ls);
144 }
145 ls = scm_cons (SCM_PACK (options[i].name), ls);
146 ans = scm_cons (ls, ans);
147 }
148 return ans;
149 }
150
151
152 /* Alters options according to the given option setting 'args'. The value of
153 * args is known to be a list, but it is not known whether the list is a well
154 * formed option setting, i. e. if for every non-boolean option a value is
155 * given. For this reason, the function applies all changes to a copy of the
156 * original setting in memory. Only if 'args' was successfully processed,
157 * the new setting will overwrite the old one. */
158 static void
159 change_option_setting (SCM args, scm_t_option options[], unsigned int n, const char *s)
160 {
161 unsigned int i;
162 SCM locally_protected_args = args;
163 SCM malloc_obj = scm_malloc_obj (n * sizeof (scm_t_bits));
164 scm_t_bits *flags = (scm_t_bits *) SCM_MALLOCDATA (malloc_obj);
165
166 for (i = 0; i != n; ++i)
167 {
168 if (options[i].type == SCM_OPTION_BOOLEAN)
169 flags[i] = 0;
170 else
171 flags[i] = options[i].val;
172 }
173
174 while (!SCM_NULL_OR_NIL_P (args))
175 {
176 SCM name = SCM_CAR (args);
177 int found = 0;
178
179 for (i = 0; i != n && !found; ++i)
180 {
181 if (scm_is_eq (name, SCM_PACK (options[i].name)))
182 {
183 switch (options[i].type)
184 {
185 case SCM_OPTION_BOOLEAN:
186 flags[i] = 1;
187 break;
188 case SCM_OPTION_INTEGER:
189 args = SCM_CDR (args);
190 SCM_ASSERT (SCM_CONSP (args), args, SCM_ARG1, s);
191 flags[i] = scm_to_size_t (SCM_CAR (args));
192 break;
193 case SCM_OPTION_SCM:
194 args = SCM_CDR (args);
195 SCM_ASSERT (SCM_CONSP (args), args, SCM_ARG1, s);
196 flags[i] = SCM_UNPACK (SCM_CAR (args));
197 break;
198 }
199 found = 1;
200 }
201 }
202
203 if (!found)
204 scm_misc_error (s, "Unknown option name: ~S", scm_list_1 (name));
205
206 args = SCM_CDR (args);
207 }
208
209 for (i = 0; i != n; ++i)
210 {
211 if (options[i].type == SCM_OPTION_SCM)
212 {
213 SCM old = SCM_PACK (options[i].val);
214 SCM new = SCM_PACK (flags[i]);
215 if (!SCM_IMP (old))
216 protected_objects = scm_delq1_x (old, protected_objects);
217 if (!SCM_IMP (new))
218 protected_objects = scm_cons (new, protected_objects);
219 }
220 options[i].val = flags[i];
221 }
222
223 scm_remember_upto_here_2 (locally_protected_args, malloc_obj);
224 }
225
226
227 SCM
228 scm_options (SCM args, scm_t_option options[], unsigned int n, const char *s)
229 {
230 if (SCM_UNBNDP (args))
231 return get_option_setting (options, n);
232 else if (!SCM_NULL_OR_NIL_P (args) && !SCM_CONSP (args))
233 /* Dirk:FIXME:: This criterion should be improved. IMO it is better to
234 * demand that args is #t if documentation should be shown than to say
235 * that every argument except a list will print out documentation. */
236 return get_documented_option_setting (options, n);
237 else
238 {
239 SCM old_setting;
240 SCM_ASSERT (scm_is_true (scm_list_p (args)), args, 1, s);
241 old_setting = get_option_setting (options, n);
242 change_option_setting (args, options, n, s);
243 return old_setting;
244 }
245 }
246
247
248 void
249 scm_init_opts (SCM (*func) (SCM), scm_t_option options[], unsigned int n)
250 {
251 unsigned int i;
252
253 for (i = 0; i != n; ++i)
254 {
255 SCM name = scm_str2symbol (options[i].name);
256 options[i].name = (char *) SCM_UNPACK (name);
257 scm_permanent_object (name);
258 }
259 func (SCM_UNDEFINED);
260 }
261
262
263 void
264 scm_init_options ()
265 {
266 scm_gc_register_root (&protected_objects);
267
268 #include "libguile/options.x"
269 }
270
271 /*
272 Local Variables:
273 c-file-style: "gnu"
274 End:
275 */