options.c 8.4 KB

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