Commit | Line | Data |
---|---|---|
1a413ab9 MD |
1 | /* Copyright (C) 1995,1996 Mikael Djurfeldt |
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 | |
15 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
16 | * | |
17 | * As a special exception, the Free Software Foundation gives permission | |
18 | * for additional uses of the text contained in its release of GUILE. | |
19 | * | |
20 | * The exception is that, if you link the GUILE library with other files | |
21 | * to produce an executable, this does not by itself cause the | |
22 | * resulting executable to be covered by the GNU General Public License. | |
23 | * Your use of that executable is in no way restricted on account of | |
24 | * linking the GUILE library code into it. | |
25 | * | |
26 | * This exception does not however invalidate any other reasons why | |
27 | * the executable file might be covered by the GNU General Public License. | |
28 | * | |
29 | * This exception applies only to the code released by the | |
30 | * Free Software Foundation under the name GUILE. If you copy | |
31 | * code from other Free Software Foundation releases into a copy of | |
32 | * GUILE, as the General Public License permits, the exception does | |
33 | * not apply to the code that you add in this way. To avoid misleading | |
34 | * anyone as to the status of such modified files, you must delete | |
35 | * this exception notice from them. | |
36 | * | |
37 | * If you write modifications of your own for GUILE, it is your choice | |
38 | * whether to permit this exception to apply to your modifications. | |
39 | * If you do not wish that, delete this exception notice. | |
40 | * | |
41 | * The author can be reached at djurfeldt@nada.kth.se | |
42 | * Mikael Djurfeldt, SANS/NADA KTH, 10044 STOCKHOLM, SWEDEN | |
43 | */ | |
44 | \f | |
45 | ||
46 | #include <stdio.h> | |
47 | #include "_scm.h" | |
48 | ||
49 | \f | |
50 | ||
51 | /* {Run-time options} | |
52 | * | |
53 | * This is the basic interface for low-level configuration of the | |
54 | * Guile library. It is used for configuring the reader, evaluator, | |
55 | * printer and debugger. | |
cbff1d89 MD |
56 | * |
57 | * Motivation: | |
58 | * | |
59 | * 1. Altering option settings can have side effects. | |
60 | * 2. Option values can be stored in native format. | |
61 | * (Important for efficiency in, e. g., the evaluator.) | |
62 | * 3. Doesn't use up name space. | |
63 | * 4. Options can be naturally grouped => ease of use. | |
1a413ab9 MD |
64 | */ |
65 | ||
cbff1d89 MD |
66 | /* scm_options is the core of all options interface procedures. |
67 | * | |
68 | * Some definitions: | |
69 | * | |
70 | * Run time options in Guile are arranged in groups. Each group | |
71 | * affects a certain aspect of the behaviour of the library. | |
72 | * | |
73 | * An "options interface procedure" manages one group of options. It | |
74 | * can be used to check or set options, or to get documentation for | |
75 | * all options of a group. The options interface procedure is not | |
76 | * intended to be called directly by the user. The user should | |
77 | * instead call | |
78 | * | |
79 | * (<group>-options) | |
80 | * (<group>-options 'help) | |
81 | * (<group>-options 'full) | |
82 | * | |
83 | * to display current option settings (The second version also | |
84 | * displays documentation. The third version also displays | |
85 | * information about programmer's options.), and | |
86 | * | |
87 | * (<group>-enable '<option-symbol>) | |
88 | * (<group>-disable '<option-symbol>) | |
89 | * (<group>-set! <option-symbol> <value>) | |
90 | * (<group>-options <option setting>) | |
91 | * | |
92 | * to alter the state of an option (The last version sets all | |
93 | * options according to <option setting>.) where <group> is the name | |
94 | * of the option group. | |
95 | * | |
96 | * An "option setting" represents the state of all low-level options | |
97 | * managed by one options interface procedure. It is a list of | |
98 | * single symbols and symbols followed by a value. | |
99 | * | |
100 | * For boolean options, the presence of the symbol of that option in | |
101 | * the option setting indicates a true value. If the symbol isn't a | |
102 | * member of the option setting list, this represents a false value. | |
103 | * | |
104 | * Other options are represented by a symbol followed by that options | |
105 | * value. | |
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 | ||
114 | SCM_SYMBOL (scm_yes_sym, "yes"); | |
115 | SCM_SYMBOL (scm_no_sym, "no"); | |
116 | ||
1a413ab9 MD |
117 | #ifdef __STDC__ |
118 | SCM | |
cbff1d89 | 119 | scm_options (SCM new_mode, scm_option options[], int n, char *s) |
1a413ab9 MD |
120 | #else |
121 | SCM | |
cbff1d89 | 122 | scm_options (new_mode, options, n, s) |
1a413ab9 MD |
123 | SCM new_mode; |
124 | scm_option options[]; | |
125 | int n; | |
126 | char *s; | |
127 | #endif | |
128 | { | |
cbff1d89 MD |
129 | int i, docp = (!SCM_UNBNDP (new_mode) |
130 | && (SCM_IMP (new_mode) || SCM_NCONSP (new_mode))); | |
131 | SCM 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 | { | |
168 | SCM_ASSERT (SCM_NIMP (new_mode) && SCM_CONSP (new_mode), | |
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); | |
181 | SCM_ASSERT (SCM_NIMP (new_mode) | |
182 | && SCM_CONSP (new_mode) | |
183 | && SCM_INUMP (SCM_CAR (new_mode)), | |
184 | new_mode, | |
185 | SCM_ARG1, | |
186 | s); | |
cbff1d89 MD |
187 | flags[i] = (unsigned long) SCM_INUM (SCM_CAR (new_mode)); |
188 | goto cont; | |
189 | case SCM_OPTION_SCM: | |
190 | new_mode = SCM_CDR (new_mode); | |
191 | flags[i] = SCM_CAR (new_mode); | |
1a413ab9 MD |
192 | goto cont; |
193 | } | |
194 | #ifndef RECKLESS | |
195 | scm_must_free ((char *) flags); | |
196 | scm_wta (SCM_CAR (new_mode), "Unknown mode flag", s); | |
197 | #endif | |
198 | cont: | |
199 | new_mode = SCM_CDR (new_mode); | |
200 | } | |
201 | for (i = 0; i < n; ++i) options[i].val = flags[i]; | |
202 | scm_must_free ((char *) flags); | |
203 | } | |
cbff1d89 | 204 | return ans; |
1a413ab9 MD |
205 | } |
206 | ||
207 | #ifdef __STDC__ | |
208 | void | |
209 | scm_init_opts (SCM (*func) (SCM), scm_option options[], int n) | |
210 | #else | |
211 | void | |
212 | scm_init_opts (func, options, n) | |
213 | SCM (*func) (SCM); | |
214 | scm_option options[]; | |
215 | int n; | |
216 | #endif | |
217 | { | |
218 | int i; | |
219 | ||
220 | for (i = 0; i < n; ++i) | |
cbff1d89 MD |
221 | { |
222 | options[i].name = (char *) SCM_CAR (scm_sysintern (options[i].name, | |
223 | SCM_UNDEFINED)); | |
224 | options[i].doc = (char *) scm_permanent_object (scm_take0str | |
225 | (options[i].doc)); | |
226 | } | |
1a413ab9 MD |
227 | func (SCM_UNDEFINED); |
228 | } | |
229 | ||
230 | #ifdef __STDC__ | |
231 | void | |
232 | scm_init_options (void) | |
233 | #else | |
234 | void | |
235 | scm_init_options () | |
236 | #endif | |
237 | { | |
238 | #include "options.x" | |
239 | } |