* srcprop.c: use double cell for storing source-properties. Put
[bpt/guile.git] / libguile / options.c
CommitLineData
2b829bbb 1/* Copyright (C) 1995,1996,1998,2000,2001, 2006 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
92205699 15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
73be1d9e 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
90SCM_SYMBOL (scm_yes_sym, "yes");
91SCM_SYMBOL (scm_no_sym, "no");
92
11d49f54 93static SCM protected_objects = SCM_EOL;
1cc91f1b 94
e11e83f3
MV
95/* Return a list of the current option setting. The format of an
96 * option setting is described in the above documentation. */
11d49f54 97static SCM
62560650 98get_option_setting (const scm_t_option options[])
11d49f54
DH
99{
100 unsigned int i;
101 SCM ls = SCM_EOL;
62560650 102 for (i = 0; options[i].name; ++i)
11d49f54
DH
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:
e11e83f3 111 ls = scm_cons (scm_from_unsigned_integer (options[i].val), ls);
11d49f54
DH
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. */
125static SCM
62560650 126get_documented_option_setting (const scm_t_option options[])
1a413ab9 127{
11d49f54
DH
128 SCM ans = SCM_EOL;
129 unsigned int i;
130
62560650 131 for (i = 0; options[i].name; ++i)
1a413ab9 132 {
cc95e00a 133 SCM ls = scm_cons (scm_from_locale_string (options[i].doc), SCM_EOL);
cbff1d89
MD
134 switch (options[i].type)
135 {
136 case SCM_OPTION_BOOLEAN:
11d49f54 137 ls = scm_cons (options[i].val ? scm_yes_sym : scm_no_sym, ls);
cbff1d89
MD
138 break;
139 case SCM_OPTION_INTEGER:
e11e83f3 140 ls = scm_cons (scm_from_unsigned_integer (options[i].val), ls);
cbff1d89
MD
141 break;
142 case SCM_OPTION_SCM:
11d49f54 143 ls = scm_cons (SCM_PACK (options[i].val), ls);
cbff1d89 144 }
11d49f54
DH
145 ls = scm_cons (SCM_PACK (options[i].name), ls);
146 ans = scm_cons (ls, ans);
147 }
148 return ans;
149}
150
151
62560650
HWN
152static int
153options_length (scm_t_option options[])
154{
155 unsigned int i = 0;
156 for (; options[i].name != NULL; ++i)
157 ;
158
159 return i;
160}
161
11d49f54
DH
162/* Alters options according to the given option setting 'args'. The value of
163 * args is known to be a list, but it is not known whether the list is a well
164 * formed option setting, i. e. if for every non-boolean option a value is
165 * given. For this reason, the function applies all changes to a copy of the
166 * original setting in memory. Only if 'args' was successfully processed,
167 * the new setting will overwrite the old one. */
168static void
62560650 169change_option_setting (SCM args, scm_t_option options[], const char *s)
11d49f54
DH
170{
171 unsigned int i;
172 SCM locally_protected_args = args;
62560650 173 SCM malloc_obj = scm_malloc_obj (options_length (options) * sizeof (scm_t_bits));
11d49f54
DH
174 scm_t_bits *flags = (scm_t_bits *) SCM_MALLOCDATA (malloc_obj);
175
62560650 176 for (i = 0; options[i].name; ++i)
11d49f54
DH
177 {
178 if (options[i].type == SCM_OPTION_BOOLEAN)
179 flags[i] = 0;
180 else
181 flags[i] = options[i].val;
cbff1d89 182 }
11d49f54 183
c96d76b8 184 while (!SCM_NULL_OR_NIL_P (args))
cbff1d89 185 {
11d49f54
DH
186 SCM name = SCM_CAR (args);
187 int found = 0;
188
62560650 189 for (i = 0; options[i].name && !found; ++i)
1a413ab9 190 {
bc36d050 191 if (scm_is_eq (name, SCM_PACK (options[i].name)))
11d49f54 192 {
1a413ab9
MD
193 switch (options[i].type)
194 {
195 case SCM_OPTION_BOOLEAN:
196 flags[i] = 1;
11d49f54 197 break;
1a413ab9 198 case SCM_OPTION_INTEGER:
11d49f54 199 args = SCM_CDR (args);
a2902ecb 200 flags[i] = scm_to_size_t (scm_car (args));
11d49f54 201 break;
cbff1d89 202 case SCM_OPTION_SCM:
11d49f54 203 args = SCM_CDR (args);
a2902ecb 204 flags[i] = SCM_UNPACK (scm_car (args));
11d49f54 205 break;
1a413ab9 206 }
11d49f54
DH
207 found = 1;
208 }
1a413ab9 209 }
11d49f54
DH
210
211 if (!found)
212 scm_misc_error (s, "Unknown option name: ~S", scm_list_1 (name));
213
214 args = SCM_CDR (args);
215 }
216
62560650 217 for (i = 0; options[i].name; ++i)
11d49f54
DH
218 {
219 if (options[i].type == SCM_OPTION_SCM)
263a691f 220 {
11d49f54
DH
221 SCM old = SCM_PACK (options[i].val);
222 SCM new = SCM_PACK (flags[i]);
223 if (!SCM_IMP (old))
224 protected_objects = scm_delq1_x (old, protected_objects);
225 if (!SCM_IMP (new))
226 protected_objects = scm_cons (new, protected_objects);
263a691f 227 }
11d49f54
DH
228 options[i].val = flags[i];
229 }
230
231 scm_remember_upto_here_2 (locally_protected_args, malloc_obj);
232}
233
234
235SCM
62560650 236scm_options (SCM args, scm_t_option options[], const char *s)
11d49f54
DH
237{
238 if (SCM_UNBNDP (args))
62560650 239 return get_option_setting (options);
d2e53ed6 240 else if (!SCM_NULL_OR_NIL_P (args) && !scm_is_pair (args))
11d49f54
DH
241 /* Dirk:FIXME:: This criterion should be improved. IMO it is better to
242 * demand that args is #t if documentation should be shown than to say
243 * that every argument except a list will print out documentation. */
62560650 244 return get_documented_option_setting (options);
11d49f54
DH
245 else
246 {
247 SCM old_setting;
7888309b 248 SCM_ASSERT (scm_is_true (scm_list_p (args)), args, 1, s);
62560650
HWN
249 old_setting = get_option_setting (options);
250 change_option_setting (args, options, s);
11d49f54 251 return old_setting;
1a413ab9 252 }
1a413ab9
MD
253}
254
1cc91f1b 255
1a413ab9 256void
62560650 257scm_init_opts (SCM (*func) (SCM), scm_t_option options[])
1a413ab9 258{
11d49f54 259 unsigned int i;
1a413ab9 260
62560650 261 for (i = 0; options[i].name; ++i)
cbff1d89 262 {
cc95e00a 263 SCM name = scm_from_locale_symbol (options[i].name);
11d49f54 264 options[i].name = (char *) SCM_UNPACK (name);
85db4a2c 265 scm_permanent_object (name);
cbff1d89 266 }
1a413ab9
MD
267 func (SCM_UNDEFINED);
268}
269
1cc91f1b 270
1a413ab9
MD
271void
272scm_init_options ()
1a413ab9 273{
11d49f54
DH
274 scm_gc_register_root (&protected_objects);
275
a0599745 276#include "libguile/options.x"
1a413ab9 277}
89e00824
ML
278
279/*
280 Local Variables:
281 c-file-style: "gnu"
282 End:
283*/