*** empty log message ***
[bpt/guile.git] / libguile / options.c
CommitLineData
7dc6e754 1/* Copyright (C) 1995, 1996, 1998 Free Software Foundation
1a413ab9
MD
2 *
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)
6 * any later version.
7 *
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.
12 *
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
82892bed
JB
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
1a413ab9
MD
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
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.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
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.
37 *
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.
41 *
42 * The author can be reached at djurfeldt@nada.kth.se
82892bed 43 * Mikael Djurfeldt, SANS/NADA KTH, 10044 STOCKHOLM, SWEDEN */
6e8d25a6
GB
44
45/* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
46 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
47
1a413ab9
MD
48\f
49
50#include <stdio.h>
51#include "_scm.h"
7ab3fdd5 52#include "strings.h"
1a413ab9 53
20e6290e 54#include "options.h"
1a413ab9
MD
55\f
56
57/* {Run-time options}
58 *
59 * This is the basic interface for low-level configuration of the
60 * Guile library. It is used for configuring the reader, evaluator,
61 * printer and debugger.
cbff1d89
MD
62 *
63 * Motivation:
64 *
65 * 1. Altering option settings can have side effects.
66 * 2. Option values can be stored in native format.
67 * (Important for efficiency in, e. g., the evaluator.)
68 * 3. Doesn't use up name space.
69 * 4. Options can be naturally grouped => ease of use.
1a413ab9
MD
70 */
71
cbff1d89
MD
72/* scm_options is the core of all options interface procedures.
73 *
74 * Some definitions:
75 *
76 * Run time options in Guile are arranged in groups. Each group
77 * affects a certain aspect of the behaviour of the library.
78 *
79 * An "options interface procedure" manages one group of options. It
80 * can be used to check or set options, or to get documentation for
81 * all options of a group. The options interface procedure is not
82 * intended to be called directly by the user. The user should
83 * instead call
84 *
85 * (<group>-options)
86 * (<group>-options 'help)
87 * (<group>-options 'full)
88 *
89 * to display current option settings (The second version also
90 * displays documentation. The third version also displays
91 * information about programmer's options.), and
92 *
93 * (<group>-enable '<option-symbol>)
94 * (<group>-disable '<option-symbol>)
95 * (<group>-set! <option-symbol> <value>)
96 * (<group>-options <option setting>)
97 *
98 * to alter the state of an option (The last version sets all
99 * options according to <option setting>.) where <group> is the name
100 * of the option group.
101 *
102 * An "option setting" represents the state of all low-level options
103 * managed by one options interface procedure. It is a list of
104 * single symbols and symbols followed by a value.
105 *
106 * For boolean options, the presence of the symbol of that option in
107 * the option setting indicates a true value. If the symbol isn't a
a0fcb308 108 * member of the option setting this represents a false value.
cbff1d89 109 *
a0fcb308 110 * Other options are represented by a symbol followed by the value.
cbff1d89
MD
111 *
112 * If scm_options is called without arguments, the current option
113 * setting is returned. If the argument is an option setting, options
114 * are altered and the old setting is returned. If the argument isn't
115 * a list, a list of sublists is returned, where each sublist contains
116 * option name, value and documentation string.
117 */
118
119SCM_SYMBOL (scm_yes_sym, "yes");
120SCM_SYMBOL (scm_no_sym, "no");
121
263a691f 122static SCM protected_objects;
1cc91f1b 123
1a413ab9 124SCM
6e8d25a6 125scm_options (SCM arg, scm_option options[], int n, const char *s)
1a413ab9 126{
263a691f
MD
127 int i, docp = (!SCM_UNBNDP (arg)
128 && !SCM_NULLP (arg)
129 && (SCM_IMP (arg) || SCM_NCONSP (arg)));
130 /* Let `arg' GC protect the arguments */
131 SCM new_mode = arg, ans = SCM_EOL, ls;
1a413ab9 132 for (i = 0; i < n; ++i)
1a413ab9 133 {
cbff1d89
MD
134 ls = docp ? scm_cons ((SCM) options[i].doc, SCM_EOL) : ans;
135 switch (options[i].type)
136 {
137 case SCM_OPTION_BOOLEAN:
138 if (docp)
139 ls = scm_cons ((int) options[i].val
140 ? scm_yes_sym
141 : scm_no_sym,
142 ls);
143 break;
144 case SCM_OPTION_INTEGER:
145 ls = scm_cons (SCM_MAKINUM ((int) options[i].val), ls);
146 break;
147 case SCM_OPTION_SCM:
148 ls = scm_cons ((SCM) options[i].val, ls);
149 }
150 if (!((options[i].type == SCM_OPTION_BOOLEAN)
151 && !docp
152 && ! (int) options[i].val))
153 ls = scm_cons ((SCM) options[i].name, ls);
154 ans = docp ? scm_cons (ls, ans) : ls;
155 }
156 if (!(SCM_UNBNDP (new_mode) || docp))
157 {
158 unsigned long *flags;
159 flags = (unsigned long *) scm_must_malloc (n * sizeof (unsigned long),
160 "mode buffer");
1a413ab9
MD
161 for (i = 0; i < n; ++i)
162 if (options[i].type == SCM_OPTION_BOOLEAN)
163 flags[i] = 0;
164 else
cbff1d89 165 flags[i] = (unsigned long) options[i].val;
1a413ab9
MD
166 while (SCM_NNULLP (new_mode))
167 {
0c95b57d 168 SCM_ASSERT (SCM_CONSP (new_mode),
1a413ab9
MD
169 new_mode,
170 SCM_ARG1,
171 s);
172 for (i = 0; i < n; ++i)
cbff1d89 173 if (SCM_CAR (new_mode) == (SCM) options[i].name)
1a413ab9
MD
174 switch (options[i].type)
175 {
176 case SCM_OPTION_BOOLEAN:
177 flags[i] = 1;
178 goto cont;
179 case SCM_OPTION_INTEGER:
180 new_mode = SCM_CDR (new_mode);
0b5f3f34 181 SCM_ASSERT ( SCM_CONSP (new_mode)
1a413ab9
MD
182 && SCM_INUMP (SCM_CAR (new_mode)),
183 new_mode,
184 SCM_ARG1,
185 s);
cbff1d89
MD
186 flags[i] = (unsigned long) SCM_INUM (SCM_CAR (new_mode));
187 goto cont;
188 case SCM_OPTION_SCM:
189 new_mode = SCM_CDR (new_mode);
f1267706 190 flags[i] = SCM_UNPACK_CAR (new_mode);
1a413ab9
MD
191 goto cont;
192 }
cf7c17e9 193#ifndef SCM_RECKLESS
1a413ab9
MD
194 scm_must_free ((char *) flags);
195 scm_wta (SCM_CAR (new_mode), "Unknown mode flag", s);
196#endif
197 cont:
198 new_mode = SCM_CDR (new_mode);
199 }
263a691f
MD
200 for (i = 0; i < n; ++i)
201 {
c209c88e 202 // scm_option doesn't know if its a long or an SCM
263a691f
MD
203 if (options[i].type == SCM_OPTION_SCM)
204 SCM_SETCDR (protected_objects,
f1267706
MD
205 scm_cons (SCM_PACK(flags[i]),
206 scm_delq1_x (SCM_PACK(options[i].val),
263a691f
MD
207 SCM_CDR (protected_objects))));
208 options[i].val = flags[i];
209 }
1a413ab9
MD
210 scm_must_free ((char *) flags);
211 }
cbff1d89 212 return ans;
1a413ab9
MD
213}
214
1cc91f1b 215
1a413ab9 216void
6e8d25a6 217scm_init_opts (SCM (*func) (SCM), scm_option options[], int n)
1a413ab9
MD
218{
219 int i;
220
221 for (i = 0; i < n; ++i)
cbff1d89 222 {
5aab5d96 223 options[i].name = (char *) SCM_CAR (scm_sysintern0 (options[i].name));
cbff1d89
MD
224 options[i].doc = (char *) scm_permanent_object (scm_take0str
225 (options[i].doc));
263a691f
MD
226 if (options[i].type == SCM_OPTION_SCM)
227 SCM_SETCDR (protected_objects,
f1267706 228 scm_cons (SCM_PACK(options[i].val), SCM_CDR (protected_objects)));
cbff1d89 229 }
1a413ab9
MD
230 func (SCM_UNDEFINED);
231}
232
1cc91f1b 233
1a413ab9
MD
234void
235scm_init_options ()
1a413ab9 236{
263a691f 237 protected_objects = scm_permanent_object (scm_cons (SCM_UNDEFINED, SCM_EOL));
1a413ab9
MD
238#include "options.x"
239}