options.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /* Copyright (C) 1995,1996,1998,2000,2001, 2006, 2008 Free Software Foundation
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2.1 of the License, or (at your option) any later version.
  7. *
  8. * This library 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 GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. # include <config.h>
  19. #endif
  20. #include "libguile/_scm.h"
  21. #include "libguile/mallocs.h"
  22. #include "libguile/strings.h"
  23. #include "libguile/lang.h"
  24. #include "libguile/options.h"
  25. /* {Run-time options}
  26. *
  27. * This is the basic interface for low-level configuration of the
  28. * Guile library. It is used for configuring the reader, evaluator,
  29. * printer and debugger.
  30. *
  31. * Motivation:
  32. *
  33. * 1. Altering option settings can have side effects.
  34. * 2. Option values can be stored in native format.
  35. * (Important for efficiency in, e. g., the evaluator.)
  36. * 3. Doesn't use up name space.
  37. * 4. Options can be naturally grouped => ease of use.
  38. */
  39. /* scm_options is the core of all options interface procedures.
  40. *
  41. * Some definitions:
  42. *
  43. * Run time options in Guile are arranged in groups. Each group
  44. * affects a certain aspect of the behaviour of the library.
  45. *
  46. * An "options interface procedure" manages one group of options. It
  47. * can be used to check or set options, or to get documentation for
  48. * all options of a group. The options interface procedure is not
  49. * intended to be called directly by the user. The user should
  50. * instead call
  51. *
  52. * (<group>-options)
  53. * (<group>-options 'help)
  54. * (<group>-options 'full)
  55. *
  56. * to display current option settings (The second version also
  57. * displays documentation. The third version also displays
  58. * information about programmer's options.), and
  59. *
  60. * (<group>-enable '<option-symbol>)
  61. * (<group>-disable '<option-symbol>)
  62. * (<group>-set! <option-symbol> <value>)
  63. * (<group>-options <option setting>)
  64. *
  65. * to alter the state of an option (The last version sets all
  66. * options according to <option setting>.) where <group> is the name
  67. * of the option group.
  68. *
  69. * An "option setting" represents the state of all low-level options
  70. * managed by one options interface procedure. It is a list of
  71. * single symbols and symbols followed by a value.
  72. *
  73. * For boolean options, the presence of the symbol of that option in
  74. * the option setting indicates a true value. If the symbol isn't a
  75. * member of the option setting this represents a false value.
  76. *
  77. * Other options are represented by a symbol followed by the value.
  78. *
  79. * If scm_options is called without arguments, the current option
  80. * setting is returned. If the argument is an option setting, options
  81. * are altered and the old setting is returned. If the argument isn't
  82. * a list, a list of sublists is returned, where each sublist contains
  83. * option name, value and documentation string.
  84. */
  85. SCM_SYMBOL (scm_yes_sym, "yes");
  86. SCM_SYMBOL (scm_no_sym, "no");
  87. static SCM protected_objects = SCM_EOL;
  88. /* Return a list of the current option setting. The format of an
  89. * option setting is described in the above documentation. */
  90. static SCM
  91. get_option_setting (const scm_t_option options[], unsigned int n)
  92. {
  93. unsigned int i;
  94. SCM ls = SCM_EOL;
  95. for (i = 0; i != n; ++i)
  96. {
  97. switch (options[i].type)
  98. {
  99. case SCM_OPTION_BOOLEAN:
  100. if (options[i].val)
  101. ls = scm_cons (SCM_PACK (options[i].name), ls);
  102. break;
  103. case SCM_OPTION_INTEGER:
  104. ls = scm_cons (scm_from_unsigned_integer (options[i].val), ls);
  105. ls = scm_cons (SCM_PACK (options[i].name), ls);
  106. break;
  107. case SCM_OPTION_SCM:
  108. ls = scm_cons (SCM_PACK (options[i].val), ls);
  109. ls = scm_cons (SCM_PACK (options[i].name), ls);
  110. }
  111. }
  112. return ls;
  113. }
  114. /* Return a list of sublists, where each sublist contains option name, value
  115. * and documentation string. */
  116. static SCM
  117. get_documented_option_setting (const scm_t_option options[], unsigned int n)
  118. {
  119. SCM ans = SCM_EOL;
  120. unsigned int i;
  121. for (i = 0; i != n; ++i)
  122. {
  123. SCM ls = scm_cons (scm_from_locale_string (options[i].doc), SCM_EOL);
  124. switch (options[i].type)
  125. {
  126. case SCM_OPTION_BOOLEAN:
  127. ls = scm_cons (options[i].val ? scm_yes_sym : scm_no_sym, ls);
  128. break;
  129. case SCM_OPTION_INTEGER:
  130. ls = scm_cons (scm_from_unsigned_integer (options[i].val), ls);
  131. break;
  132. case SCM_OPTION_SCM:
  133. ls = scm_cons (SCM_PACK (options[i].val), ls);
  134. }
  135. ls = scm_cons (SCM_PACK (options[i].name), ls);
  136. ans = scm_cons (ls, ans);
  137. }
  138. return ans;
  139. }
  140. /* Alters options according to the given option setting 'args'. The value of
  141. * args is known to be a list, but it is not known whether the list is a well
  142. * formed option setting, i. e. if for every non-boolean option a value is
  143. * given. For this reason, the function applies all changes to a copy of the
  144. * original setting in memory. Only if 'args' was successfully processed,
  145. * the new setting will overwrite the old one. */
  146. static void
  147. change_option_setting (SCM args, scm_t_option options[], unsigned int n, const char *s)
  148. {
  149. unsigned int i;
  150. SCM locally_protected_args = args;
  151. SCM malloc_obj = scm_malloc_obj (n * sizeof (scm_t_bits));
  152. scm_t_bits *flags = (scm_t_bits *) SCM_MALLOCDATA (malloc_obj);
  153. for (i = 0; i != n; ++i)
  154. {
  155. if (options[i].type == SCM_OPTION_BOOLEAN)
  156. flags[i] = 0;
  157. else
  158. flags[i] = options[i].val;
  159. }
  160. while (!SCM_NULL_OR_NIL_P (args))
  161. {
  162. SCM name = SCM_CAR (args);
  163. int found = 0;
  164. for (i = 0; i != n && !found; ++i)
  165. {
  166. if (scm_is_eq (name, SCM_PACK (options[i].name)))
  167. {
  168. switch (options[i].type)
  169. {
  170. case SCM_OPTION_BOOLEAN:
  171. flags[i] = 1;
  172. break;
  173. case SCM_OPTION_INTEGER:
  174. args = SCM_CDR (args);
  175. flags[i] = scm_to_size_t (scm_car (args));
  176. break;
  177. case SCM_OPTION_SCM:
  178. args = SCM_CDR (args);
  179. flags[i] = SCM_UNPACK (scm_car (args));
  180. break;
  181. }
  182. found = 1;
  183. }
  184. }
  185. if (!found)
  186. scm_misc_error (s, "Unknown option name: ~S", scm_list_1 (name));
  187. args = SCM_CDR (args);
  188. }
  189. for (i = 0; i != n; ++i)
  190. {
  191. if (options[i].type == SCM_OPTION_SCM)
  192. {
  193. SCM old = SCM_PACK (options[i].val);
  194. SCM new = SCM_PACK (flags[i]);
  195. if (!SCM_IMP (old))
  196. protected_objects = scm_delq1_x (old, protected_objects);
  197. if (!SCM_IMP (new))
  198. protected_objects = scm_cons (new, protected_objects);
  199. }
  200. options[i].val = flags[i];
  201. }
  202. scm_remember_upto_here_2 (locally_protected_args, malloc_obj);
  203. }
  204. SCM
  205. scm_options (SCM args, scm_t_option options[], unsigned int n, const char *s)
  206. {
  207. if (SCM_UNBNDP (args))
  208. return get_option_setting (options, n);
  209. else if (!SCM_NULL_OR_NIL_P (args) && !scm_is_pair (args))
  210. /* Dirk:FIXME:: This criterion should be improved. IMO it is better to
  211. * demand that args is #t if documentation should be shown than to say
  212. * that every argument except a list will print out documentation. */
  213. return get_documented_option_setting (options, n);
  214. else
  215. {
  216. SCM old_setting;
  217. SCM_ASSERT (scm_is_true (scm_list_p (args)), args, 1, s);
  218. old_setting = get_option_setting (options, n);
  219. change_option_setting (args, options, n, s);
  220. return old_setting;
  221. }
  222. }
  223. void
  224. scm_init_opts (SCM (*func) (SCM), scm_t_option options[], unsigned int n)
  225. {
  226. unsigned int i;
  227. for (i = 0; i != n; ++i)
  228. {
  229. SCM name = scm_from_locale_symbol (options[i].name);
  230. options[i].name = (char *) SCM_UNPACK (name);
  231. scm_permanent_object (name);
  232. }
  233. func (SCM_UNDEFINED);
  234. }
  235. void
  236. scm_init_options ()
  237. {
  238. scm_gc_register_root (&protected_objects);
  239. #include "libguile/options.x"
  240. }
  241. /*
  242. Local Variables:
  243. c-file-style: "gnu"
  244. End:
  245. */