* chars.c (scm_lowers, scm_uppers, scm_charnames, scm_charnums),
[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 */
1a413ab9
MD
44\f
45
46#include <stdio.h>
47#include "_scm.h"
48
20e6290e 49#include "options.h"
1a413ab9
MD
50\f
51
52/* {Run-time options}
53 *
54 * This is the basic interface for low-level configuration of the
55 * Guile library. It is used for configuring the reader, evaluator,
56 * printer and debugger.
cbff1d89
MD
57 *
58 * Motivation:
59 *
60 * 1. Altering option settings can have side effects.
61 * 2. Option values can be stored in native format.
62 * (Important for efficiency in, e. g., the evaluator.)
63 * 3. Doesn't use up name space.
64 * 4. Options can be naturally grouped => ease of use.
1a413ab9
MD
65 */
66
cbff1d89
MD
67/* scm_options is the core of all options interface procedures.
68 *
69 * Some definitions:
70 *
71 * Run time options in Guile are arranged in groups. Each group
72 * affects a certain aspect of the behaviour of the library.
73 *
74 * An "options interface procedure" manages one group of options. It
75 * can be used to check or set options, or to get documentation for
76 * all options of a group. The options interface procedure is not
77 * intended to be called directly by the user. The user should
78 * instead call
79 *
80 * (<group>-options)
81 * (<group>-options 'help)
82 * (<group>-options 'full)
83 *
84 * to display current option settings (The second version also
85 * displays documentation. The third version also displays
86 * information about programmer's options.), and
87 *
88 * (<group>-enable '<option-symbol>)
89 * (<group>-disable '<option-symbol>)
90 * (<group>-set! <option-symbol> <value>)
91 * (<group>-options <option setting>)
92 *
93 * to alter the state of an option (The last version sets all
94 * options according to <option setting>.) where <group> is the name
95 * of the option group.
96 *
97 * An "option setting" represents the state of all low-level options
98 * managed by one options interface procedure. It is a list of
99 * single symbols and symbols followed by a value.
100 *
101 * For boolean options, the presence of the symbol of that option in
102 * the option setting indicates a true value. If the symbol isn't a
a0fcb308 103 * member of the option setting this represents a false value.
cbff1d89 104 *
a0fcb308 105 * Other options are represented by a symbol followed by the value.
cbff1d89
MD
106 *
107 * If scm_options is called without arguments, the current option
108 * setting is returned. If the argument is an option setting, options
109 * are altered and the old setting is returned. If the argument isn't
110 * a list, a list of sublists is returned, where each sublist contains
111 * option name, value and documentation string.
112 */
113
114SCM_SYMBOL (scm_yes_sym, "yes");
115SCM_SYMBOL (scm_no_sym, "no");
116
263a691f 117static SCM protected_objects;
1cc91f1b 118
1a413ab9 119SCM
263a691f
MD
120scm_options (arg, options, n, s)
121 SCM arg;
1a413ab9
MD
122 scm_option options[];
123 int n;
3eeba8d4 124 const char *s;
1a413ab9 125{
263a691f
MD
126 int i, docp = (!SCM_UNBNDP (arg)
127 && !SCM_NULLP (arg)
128 && (SCM_IMP (arg) || SCM_NCONSP (arg)));
129 /* Let `arg' GC protect the arguments */
130 SCM new_mode = arg, ans = SCM_EOL, ls;
1a413ab9 131 for (i = 0; i < n; ++i)
1a413ab9 132 {
cbff1d89
MD
133 ls = docp ? scm_cons ((SCM) options[i].doc, SCM_EOL) : ans;
134 switch (options[i].type)
135 {
136 case SCM_OPTION_BOOLEAN:
137 if (docp)
138 ls = scm_cons ((int) options[i].val
139 ? scm_yes_sym
140 : scm_no_sym,
141 ls);
142 break;
143 case SCM_OPTION_INTEGER:
144 ls = scm_cons (SCM_MAKINUM ((int) options[i].val), ls);
145 break;
146 case SCM_OPTION_SCM:
147 ls = scm_cons ((SCM) options[i].val, ls);
148 }
149 if (!((options[i].type == SCM_OPTION_BOOLEAN)
150 && !docp
151 && ! (int) options[i].val))
152 ls = scm_cons ((SCM) options[i].name, ls);
153 ans = docp ? scm_cons (ls, ans) : ls;
154 }
155 if (!(SCM_UNBNDP (new_mode) || docp))
156 {
157 unsigned long *flags;
158 flags = (unsigned long *) scm_must_malloc (n * sizeof (unsigned long),
159 "mode buffer");
1a413ab9
MD
160 for (i = 0; i < n; ++i)
161 if (options[i].type == SCM_OPTION_BOOLEAN)
162 flags[i] = 0;
163 else
cbff1d89 164 flags[i] = (unsigned long) options[i].val;
1a413ab9
MD
165 while (SCM_NNULLP (new_mode))
166 {
167 SCM_ASSERT (SCM_NIMP (new_mode) && SCM_CONSP (new_mode),
168 new_mode,
169 SCM_ARG1,
170 s);
171 for (i = 0; i < n; ++i)
cbff1d89 172 if (SCM_CAR (new_mode) == (SCM) options[i].name)
1a413ab9
MD
173 switch (options[i].type)
174 {
175 case SCM_OPTION_BOOLEAN:
176 flags[i] = 1;
177 goto cont;
178 case SCM_OPTION_INTEGER:
179 new_mode = SCM_CDR (new_mode);
180 SCM_ASSERT (SCM_NIMP (new_mode)
181 && SCM_CONSP (new_mode)
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);
190 flags[i] = SCM_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 {
202 if (options[i].type == SCM_OPTION_SCM)
203 SCM_SETCDR (protected_objects,
204 scm_cons (flags[i],
205 scm_delq1_x (options[i].val,
206 SCM_CDR (protected_objects))));
207 options[i].val = flags[i];
208 }
1a413ab9
MD
209 scm_must_free ((char *) flags);
210 }
cbff1d89 211 return ans;
1a413ab9
MD
212}
213
1cc91f1b 214
1a413ab9
MD
215void
216scm_init_opts (func, options, n)
217 SCM (*func) (SCM);
218 scm_option options[];
219 int n;
1a413ab9
MD
220{
221 int i;
222
223 for (i = 0; i < n; ++i)
cbff1d89 224 {
5aab5d96 225 options[i].name = (char *) SCM_CAR (scm_sysintern0 (options[i].name));
cbff1d89
MD
226 options[i].doc = (char *) scm_permanent_object (scm_take0str
227 (options[i].doc));
263a691f
MD
228 if (options[i].type == SCM_OPTION_SCM)
229 SCM_SETCDR (protected_objects,
230 scm_cons (options[i].val, SCM_CDR (protected_objects)));
cbff1d89 231 }
1a413ab9
MD
232 func (SCM_UNDEFINED);
233}
234
1cc91f1b 235
1a413ab9
MD
236void
237scm_init_options ()
1a413ab9 238{
263a691f 239 protected_objects = scm_permanent_object (scm_cons (SCM_UNDEFINED, SCM_EOL));
1a413ab9
MD
240#include "options.x"
241}