options.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /* Copyright (C) 1995,1996,1998,2000,2001, 2006, 2008, 2009, 2010 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/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[])
  92. {
  93. unsigned int i;
  94. SCM ls = SCM_EOL;
  95. for (i = 0; options[i].name; ++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[])
  118. {
  119. SCM ans = SCM_EOL;
  120. unsigned int i;
  121. for (i = 0; options[i].name; ++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 scm_reverse_x (ans, SCM_UNDEFINED);
  139. }
  140. static int
  141. options_length (scm_t_option options[])
  142. {
  143. unsigned int i = 0;
  144. for (; options[i].name != NULL; ++i)
  145. ;
  146. return i;
  147. }
  148. /* Alters options according to the given option setting 'args'. The value of
  149. * args is known to be a list, but it is not known whether the list is a well
  150. * formed option setting, i. e. if for every non-boolean option a value is
  151. * given. For this reason, the function applies all changes to a copy of the
  152. * original setting in memory. Only if 'args' was successfully processed,
  153. * the new setting will overwrite the old one.
  154. *
  155. * If DRY_RUN is set, don't change anything. This is useful for trying out an option
  156. * before entering a critical section.
  157. */
  158. static void
  159. change_option_setting (SCM args, scm_t_option options[], const char *s,
  160. int dry_run)
  161. {
  162. unsigned int i;
  163. SCM locally_protected_args = args;
  164. SCM malloc_obj = scm_malloc_obj (options_length (options) * sizeof (scm_t_bits));
  165. scm_t_bits *flags = (scm_t_bits *) SCM_MALLOCDATA (malloc_obj);
  166. for (i = 0; options[i].name; ++i)
  167. {
  168. if (options[i].type == SCM_OPTION_BOOLEAN)
  169. flags[i] = 0;
  170. else
  171. flags[i] = options[i].val;
  172. }
  173. while (!SCM_NULL_OR_NIL_P (args))
  174. {
  175. SCM name = SCM_CAR (args);
  176. int found = 0;
  177. for (i = 0; options[i].name && !found; ++i)
  178. {
  179. if (scm_is_eq (name, SCM_PACK (options[i].name)))
  180. {
  181. switch (options[i].type)
  182. {
  183. case SCM_OPTION_BOOLEAN:
  184. flags[i] = 1;
  185. break;
  186. case SCM_OPTION_INTEGER:
  187. args = SCM_CDR (args);
  188. flags[i] = scm_to_size_t (scm_car (args));
  189. break;
  190. case SCM_OPTION_SCM:
  191. args = SCM_CDR (args);
  192. flags[i] = SCM_UNPACK (scm_car (args));
  193. break;
  194. }
  195. found = 1;
  196. }
  197. }
  198. if (!found)
  199. scm_misc_error (s, "Unknown option name: ~S", scm_list_1 (name));
  200. args = SCM_CDR (args);
  201. }
  202. if (dry_run)
  203. return;
  204. for (i = 0; options[i].name; ++i)
  205. {
  206. if (options[i].type == SCM_OPTION_SCM)
  207. {
  208. SCM old = SCM_PACK (options[i].val);
  209. SCM new = SCM_PACK (flags[i]);
  210. if (!SCM_IMP (old))
  211. protected_objects = scm_delq1_x (old, protected_objects);
  212. if (!SCM_IMP (new))
  213. protected_objects = scm_cons (new, protected_objects);
  214. }
  215. options[i].val = flags[i];
  216. }
  217. scm_remember_upto_here_2 (locally_protected_args, malloc_obj);
  218. }
  219. SCM
  220. scm_options (SCM args, scm_t_option options[], const char *s)
  221. {
  222. return scm_options_try (args, options, s, 0);
  223. }
  224. SCM
  225. scm_options_try (SCM args, scm_t_option options[], const char *s,
  226. int dry_run)
  227. {
  228. if (SCM_UNBNDP (args))
  229. return get_option_setting (options);
  230. else if (!SCM_NULL_OR_NIL_P (args) && !scm_is_pair (args))
  231. /* Dirk:FIXME:: This criterion should be improved. IMO it is better to
  232. * demand that args is #t if documentation should be shown than to say
  233. * that every argument except a list will print out documentation. */
  234. return get_documented_option_setting (options);
  235. else
  236. {
  237. SCM old_setting;
  238. SCM_ASSERT (scm_is_true (scm_list_p (args)), args, 1, s);
  239. old_setting = get_option_setting (options);
  240. change_option_setting (args, options, s, dry_run);
  241. return old_setting;
  242. }
  243. }
  244. void
  245. scm_init_opts (SCM (*func) (SCM), scm_t_option options[])
  246. {
  247. unsigned int i;
  248. for (i = 0; options[i].name; ++i)
  249. {
  250. SCM name = scm_from_locale_symbol (options[i].name);
  251. options[i].name = (char *) SCM_UNPACK (name);
  252. }
  253. func (SCM_UNDEFINED);
  254. }
  255. void
  256. scm_init_options ()
  257. {
  258. scm_gc_register_root (&protected_objects);
  259. #include "libguile/options.x"
  260. }
  261. /*
  262. Local Variables:
  263. c-file-style: "gnu"
  264. End:
  265. */