1 /* Copyright (C) 1995,1996,1998,2000,2001 Free Software Foundation
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
8 * This program 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
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice.
42 * The author can be reached at djurfeldt@nada.kth.se
43 * Mikael Djurfeldt, SANS/NADA KTH, 10044 STOCKHOLM, SWEDEN */
47 #include "libguile/_scm.h"
48 #include "libguile/mallocs.h"
49 #include "libguile/strings.h"
50 #include "libguile/lang.h"
52 #include "libguile/options.h"
57 * This is the basic interface for low-level configuration of the
58 * Guile library. It is used for configuring the reader, evaluator,
59 * printer and debugger.
63 * 1. Altering option settings can have side effects.
64 * 2. Option values can be stored in native format.
65 * (Important for efficiency in, e. g., the evaluator.)
66 * 3. Doesn't use up name space.
67 * 4. Options can be naturally grouped => ease of use.
70 /* scm_options is the core of all options interface procedures.
74 * Run time options in Guile are arranged in groups. Each group
75 * affects a certain aspect of the behaviour of the library.
77 * An "options interface procedure" manages one group of options. It
78 * can be used to check or set options, or to get documentation for
79 * all options of a group. The options interface procedure is not
80 * intended to be called directly by the user. The user should
84 * (<group>-options 'help)
85 * (<group>-options 'full)
87 * to display current option settings (The second version also
88 * displays documentation. The third version also displays
89 * information about programmer's options.), and
91 * (<group>-enable '<option-symbol>)
92 * (<group>-disable '<option-symbol>)
93 * (<group>-set! <option-symbol> <value>)
94 * (<group>-options <option setting>)
96 * to alter the state of an option (The last version sets all
97 * options according to <option setting>.) where <group> is the name
98 * of the option group.
100 * An "option setting" represents the state of all low-level options
101 * managed by one options interface procedure. It is a list of
102 * single symbols and symbols followed by a value.
104 * For boolean options, the presence of the symbol of that option in
105 * the option setting indicates a true value. If the symbol isn't a
106 * member of the option setting this represents a false value.
108 * Other options are represented by a symbol followed by the value.
110 * If scm_options is called without arguments, the current option
111 * setting is returned. If the argument is an option setting, options
112 * are altered and the old setting is returned. If the argument isn't
113 * a list, a list of sublists is returned, where each sublist contains
114 * option name, value and documentation string.
117 SCM_SYMBOL (scm_yes_sym
, "yes");
118 SCM_SYMBOL (scm_no_sym
, "no");
120 static SCM protected_objects
= SCM_EOL
;
123 /* Return a list of the current option setting. The format of an option
124 * setting is described in the above documentation. */
126 get_option_setting (const scm_t_option options
[], unsigned int n
)
130 for (i
= 0; i
!= n
; ++i
)
132 switch (options
[i
].type
)
134 case SCM_OPTION_BOOLEAN
:
136 ls
= scm_cons (SCM_PACK (options
[i
].name
), ls
);
138 case SCM_OPTION_INTEGER
:
139 ls
= scm_cons (SCM_MAKINUM (options
[i
].val
), ls
);
140 ls
= scm_cons (SCM_PACK (options
[i
].name
), ls
);
143 ls
= scm_cons (SCM_PACK (options
[i
].val
), ls
);
144 ls
= scm_cons (SCM_PACK (options
[i
].name
), ls
);
151 /* Return a list of sublists, where each sublist contains option name, value
152 * and documentation string. */
154 get_documented_option_setting (const scm_t_option options
[], unsigned int n
)
159 for (i
= 0; i
!= n
; ++i
)
161 SCM ls
= scm_cons (scm_str2string (options
[i
].doc
), SCM_EOL
);
162 switch (options
[i
].type
)
164 case SCM_OPTION_BOOLEAN
:
165 ls
= scm_cons (options
[i
].val
? scm_yes_sym
: scm_no_sym
, ls
);
167 case SCM_OPTION_INTEGER
:
168 ls
= scm_cons (SCM_MAKINUM (options
[i
].val
), ls
);
171 ls
= scm_cons (SCM_PACK (options
[i
].val
), ls
);
173 ls
= scm_cons (SCM_PACK (options
[i
].name
), ls
);
174 ans
= scm_cons (ls
, ans
);
180 /* Alters options according to the given option setting 'args'. The value of
181 * args is known to be a list, but it is not known whether the list is a well
182 * formed option setting, i. e. if for every non-boolean option a value is
183 * given. For this reason, the function applies all changes to a copy of the
184 * original setting in memory. Only if 'args' was successfully processed,
185 * the new setting will overwrite the old one. */
187 change_option_setting (SCM args
, scm_t_option options
[], unsigned int n
, const char *s
)
190 SCM locally_protected_args
= args
;
191 SCM malloc_obj
= scm_malloc_obj (n
* sizeof (scm_t_bits
));
192 scm_t_bits
*flags
= (scm_t_bits
*) SCM_MALLOCDATA (malloc_obj
);
194 for (i
= 0; i
!= n
; ++i
)
196 if (options
[i
].type
== SCM_OPTION_BOOLEAN
)
199 flags
[i
] = options
[i
].val
;
202 while (!SCM_NULL_OR_NIL_P (args
))
204 SCM name
= SCM_CAR (args
);
207 for (i
= 0; i
!= n
&& !found
; ++i
)
209 if (SCM_EQ_P (name
, SCM_PACK (options
[i
].name
)))
211 switch (options
[i
].type
)
213 case SCM_OPTION_BOOLEAN
:
216 case SCM_OPTION_INTEGER
:
217 args
= SCM_CDR (args
);
218 SCM_ASSERT (SCM_CONSP (args
), args
, SCM_ARG1
, s
);
219 SCM_ASSERT (SCM_INUMP (SCM_CAR (args
)), args
, SCM_ARG1
, s
);
220 flags
[i
] = SCM_INUM (SCM_CAR (args
));
223 args
= SCM_CDR (args
);
224 SCM_ASSERT (SCM_CONSP (args
), args
, SCM_ARG1
, s
);
225 flags
[i
] = SCM_UNPACK (SCM_CAR (args
));
233 scm_misc_error (s
, "Unknown option name: ~S", scm_list_1 (name
));
235 args
= SCM_CDR (args
);
238 for (i
= 0; i
!= n
; ++i
)
240 if (options
[i
].type
== SCM_OPTION_SCM
)
242 SCM old
= SCM_PACK (options
[i
].val
);
243 SCM
new = SCM_PACK (flags
[i
]);
245 protected_objects
= scm_delq1_x (old
, protected_objects
);
247 protected_objects
= scm_cons (new, protected_objects
);
249 options
[i
].val
= flags
[i
];
252 scm_remember_upto_here_2 (locally_protected_args
, malloc_obj
);
257 scm_options (SCM args
, scm_t_option options
[], unsigned int n
, const char *s
)
259 if (SCM_UNBNDP (args
))
260 return get_option_setting (options
, n
);
261 else if (!SCM_NULL_OR_NIL_P (args
) && !SCM_CONSP (args
))
262 /* Dirk:FIXME:: This criterion should be improved. IMO it is better to
263 * demand that args is #t if documentation should be shown than to say
264 * that every argument except a list will print out documentation. */
265 return get_documented_option_setting (options
, n
);
269 SCM_ASSERT (!SCM_FALSEP (scm_list_p (args
)), args
, 1, s
);
270 old_setting
= get_option_setting (options
, n
);
271 change_option_setting (args
, options
, n
, s
);
278 scm_init_opts (SCM (*func
) (SCM
), scm_t_option options
[], unsigned int n
)
282 for (i
= 0; i
!= n
; ++i
)
284 SCM name
= scm_str2symbol (options
[i
].name
);
285 options
[i
].name
= (char *) SCM_UNPACK (name
);
286 scm_permanent_object (name
);
288 func (SCM_UNDEFINED
);
295 scm_gc_register_root (&protected_objects
);
297 #include "libguile/options.x"