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 */
48 #include "libguile/_scm.h"
49 #include "libguile/strings.h"
51 #include "libguile/options.h"
56 * This is the basic interface for low-level configuration of the
57 * Guile library. It is used for configuring the reader, evaluator,
58 * printer and debugger.
62 * 1. Altering option settings can have side effects.
63 * 2. Option values can be stored in native format.
64 * (Important for efficiency in, e. g., the evaluator.)
65 * 3. Doesn't use up name space.
66 * 4. Options can be naturally grouped => ease of use.
69 /* scm_options is the core of all options interface procedures.
73 * Run time options in Guile are arranged in groups. Each group
74 * affects a certain aspect of the behaviour of the library.
76 * An "options interface procedure" manages one group of options. It
77 * can be used to check or set options, or to get documentation for
78 * all options of a group. The options interface procedure is not
79 * intended to be called directly by the user. The user should
83 * (<group>-options 'help)
84 * (<group>-options 'full)
86 * to display current option settings (The second version also
87 * displays documentation. The third version also displays
88 * information about programmer's options.), and
90 * (<group>-enable '<option-symbol>)
91 * (<group>-disable '<option-symbol>)
92 * (<group>-set! <option-symbol> <value>)
93 * (<group>-options <option setting>)
95 * to alter the state of an option (The last version sets all
96 * options according to <option setting>.) where <group> is the name
97 * of the option group.
99 * An "option setting" represents the state of all low-level options
100 * managed by one options interface procedure. It is a list of
101 * single symbols and symbols followed by a value.
103 * For boolean options, the presence of the symbol of that option in
104 * the option setting indicates a true value. If the symbol isn't a
105 * member of the option setting this represents a false value.
107 * Other options are represented by a symbol followed by the value.
109 * If scm_options is called without arguments, the current option
110 * setting is returned. If the argument is an option setting, options
111 * are altered and the old setting is returned. If the argument isn't
112 * a list, a list of sublists is returned, where each sublist contains
113 * option name, value and documentation string.
116 SCM_SYMBOL (scm_yes_sym
, "yes");
117 SCM_SYMBOL (scm_no_sym
, "no");
119 static SCM protected_objects
;
122 scm_options (SCM arg
, scm_t_option options
[], int n
, const char *s
)
124 int i
, docp
= (!SCM_UNBNDP (arg
) && !SCM_NULLP (arg
) && !SCM_CONSP (arg
));
125 /* Let `arg' GC protect the arguments */
126 SCM new_mode
= arg
, ans
= SCM_EOL
, ls
;
127 for (i
= 0; i
< n
; ++i
)
129 ls
= docp
? scm_cons ((SCM
) options
[i
].doc
, SCM_EOL
) : ans
;
130 switch (options
[i
].type
)
132 case SCM_OPTION_BOOLEAN
:
134 ls
= scm_cons ((int) options
[i
].val
139 case SCM_OPTION_INTEGER
:
140 ls
= scm_cons (SCM_MAKINUM (options
[i
].val
), ls
);
143 ls
= scm_cons ((SCM
) options
[i
].val
, ls
);
145 if (!((options
[i
].type
== SCM_OPTION_BOOLEAN
)
147 && ! (int) options
[i
].val
))
148 ls
= scm_cons ((SCM
) options
[i
].name
, ls
);
149 ans
= docp
? scm_cons (ls
, ans
) : ls
;
151 if (!(SCM_UNBNDP (new_mode
) || docp
))
153 unsigned long *flags
;
154 flags
= (unsigned long *) scm_must_malloc (n
* sizeof (unsigned long),
156 for (i
= 0; i
< n
; ++i
)
157 if (options
[i
].type
== SCM_OPTION_BOOLEAN
)
160 flags
[i
] = (unsigned long) options
[i
].val
;
161 while (SCM_NNULLP (new_mode
))
163 SCM_ASSERT (SCM_CONSP (new_mode
),
167 for (i
= 0; i
< n
; ++i
)
168 if (SCM_CAR (new_mode
) == (SCM
) options
[i
].name
)
169 switch (options
[i
].type
)
171 case SCM_OPTION_BOOLEAN
:
174 case SCM_OPTION_INTEGER
:
175 new_mode
= SCM_CDR (new_mode
);
176 SCM_ASSERT ( SCM_CONSP (new_mode
)
177 && SCM_INUMP (SCM_CAR (new_mode
)),
181 flags
[i
] = (unsigned long) SCM_INUM (SCM_CAR (new_mode
));
184 new_mode
= SCM_CDR (new_mode
);
185 flags
[i
] = SCM_UNPACK (SCM_CAR (new_mode
));
189 scm_must_free ((char *) flags
);
190 scm_misc_error (s
, "Unknown mode flag: ~S",
191 scm_list_1 (SCM_CAR (new_mode
)));
194 new_mode
= SCM_CDR (new_mode
);
196 for (i
= 0; i
< n
; ++i
)
198 /* scm_option doesn't know if its a long or an SCM */
199 if (options
[i
].type
== SCM_OPTION_SCM
)
200 SCM_SETCDR (protected_objects
,
201 scm_cons (SCM_PACK(flags
[i
]),
202 scm_delq1_x (SCM_PACK(options
[i
].val
),
203 SCM_CDR (protected_objects
))));
204 options
[i
].val
= flags
[i
];
206 scm_must_free ((char *) flags
);
213 scm_init_opts (SCM (*func
) (SCM
), scm_t_option options
[], int n
)
217 for (i
= 0; i
< n
; ++i
)
222 name
= scm_str2symbol (options
[i
].name
);
223 options
[i
].name
= (char *) name
;
224 scm_permanent_object (name
);
225 doc
= scm_take0str (options
[i
].doc
);
226 options
[i
].doc
= (char *) doc
;
227 scm_permanent_object (doc
);
228 if (options
[i
].type
== SCM_OPTION_SCM
)
229 SCM_SETCDR (protected_objects
,
230 scm_cons (SCM_PACK(options
[i
].val
), SCM_CDR (protected_objects
)));
232 func (SCM_UNDEFINED
);
239 protected_objects
= scm_permanent_object (scm_cons (SCM_UNDEFINED
, SCM_EOL
));
240 #ifndef SCM_MAGIC_SNARFER
241 #include "libguile/options.x"