hooks.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /* Copyright (C) 1995,1996,1998,1999,2000,2001, 2003, 2006, 2008, 2009, 2011 Free Software Foundation, Inc.
  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 <stdio.h>
  22. #include "libguile/_scm.h"
  23. #include "libguile/eval.h"
  24. #include "libguile/ports.h"
  25. #include "libguile/procprop.h"
  26. #include "libguile/root.h"
  27. #include "libguile/smob.h"
  28. #include "libguile/strings.h"
  29. #include "libguile/validate.h"
  30. #include "libguile/hooks.h"
  31. /* C level hooks
  32. *
  33. * Currently, this implementation is separate from the Scheme level
  34. * hooks. The possibility exists to implement the Scheme level hooks
  35. * using C level hooks.
  36. */
  37. /* Hint for `scm_gc_malloc ()' and friends. */
  38. static const char hook_entry_gc_hint[] = "hook entry";
  39. void
  40. scm_c_hook_init (scm_t_c_hook *hook, void *hook_data, scm_t_c_hook_type type)
  41. {
  42. hook->first = 0;
  43. hook->type = type;
  44. hook->data = hook_data;
  45. }
  46. void
  47. scm_c_hook_add (scm_t_c_hook *hook,
  48. scm_t_c_hook_function func,
  49. void *fn_data,
  50. int appendp)
  51. {
  52. scm_t_c_hook_entry *entry;
  53. scm_t_c_hook_entry **loc = &hook->first;
  54. entry = scm_gc_malloc (sizeof (scm_t_c_hook_entry), hook_entry_gc_hint);
  55. if (appendp)
  56. while (*loc)
  57. loc = &(*loc)->next;
  58. entry->next = *loc;
  59. entry->func = func;
  60. entry->data = fn_data;
  61. *loc = entry;
  62. }
  63. void
  64. scm_c_hook_remove (scm_t_c_hook *hook,
  65. scm_t_c_hook_function func,
  66. void *fn_data)
  67. {
  68. scm_t_c_hook_entry **loc = &hook->first;
  69. while (*loc)
  70. {
  71. if ((*loc)->func == func && (*loc)->data == fn_data)
  72. {
  73. *loc = (*loc)->next;
  74. return;
  75. }
  76. loc = &(*loc)->next;
  77. }
  78. fprintf (stderr, "Attempt to remove non-existent hook function\n");
  79. abort ();
  80. }
  81. void *
  82. scm_c_hook_run (scm_t_c_hook *hook, void *data)
  83. {
  84. scm_t_c_hook_entry *entry = hook->first;
  85. scm_t_c_hook_type type = hook->type;
  86. void *res = 0;
  87. while (entry)
  88. {
  89. res = (entry->func) (hook->data, entry->data, data);
  90. if (res)
  91. {
  92. if (type == SCM_C_HOOK_OR)
  93. break;
  94. }
  95. else
  96. {
  97. if (type == SCM_C_HOOK_AND)
  98. break;
  99. }
  100. entry = entry->next;
  101. }
  102. return res;
  103. }
  104. /* Scheme level hooks
  105. *
  106. * A hook is basically a list of procedures to be called at well defined
  107. * points in time.
  108. *
  109. * Hook arity is not a full member of this type and therefore lacks an
  110. * accessor. It exists to aid debugging and is not intended to be used in
  111. * programs.
  112. */
  113. scm_t_bits scm_tc16_hook;
  114. static int
  115. hook_print (SCM hook, SCM port, scm_print_state *pstate)
  116. {
  117. SCM ls, name;
  118. scm_puts_unlocked ("#<hook ", port);
  119. scm_intprint (SCM_HOOK_ARITY (hook), 10, port);
  120. scm_putc_unlocked (' ', port);
  121. scm_uintprint (SCM_UNPACK (hook), 16, port);
  122. ls = SCM_HOOK_PROCEDURES (hook);
  123. while (scm_is_pair (ls))
  124. {
  125. scm_putc_unlocked (' ', port);
  126. name = scm_procedure_name (SCM_CAR (ls));
  127. if (scm_is_true (name))
  128. scm_iprin1 (name, port, pstate);
  129. else
  130. scm_putc_unlocked ('?', port);
  131. ls = SCM_CDR (ls);
  132. }
  133. scm_putc_unlocked ('>', port);
  134. return 1;
  135. }
  136. SCM_DEFINE (scm_make_hook, "make-hook", 0, 1, 0,
  137. (SCM n_args),
  138. "Create a hook for storing procedure of arity @var{n_args}.\n"
  139. "@var{n_args} defaults to zero. The returned value is a hook\n"
  140. "object to be used with the other hook procedures.")
  141. #define FUNC_NAME s_scm_make_hook
  142. {
  143. unsigned int n;
  144. if (SCM_UNBNDP (n_args))
  145. n = 0;
  146. else
  147. n = scm_to_unsigned_integer (n_args, 0, 16);
  148. SCM_RETURN_NEWSMOB (scm_tc16_hook + (n << 16), SCM_UNPACK (SCM_EOL));
  149. }
  150. #undef FUNC_NAME
  151. SCM_DEFINE (scm_hook_p, "hook?", 1, 0, 0,
  152. (SCM x),
  153. "Return @code{#t} if @var{x} is a hook, @code{#f} otherwise.")
  154. #define FUNC_NAME s_scm_hook_p
  155. {
  156. return scm_from_bool (SCM_HOOKP (x));
  157. }
  158. #undef FUNC_NAME
  159. SCM_DEFINE (scm_hook_empty_p, "hook-empty?", 1, 0, 0,
  160. (SCM hook),
  161. "Return @code{#t} if @var{hook} is an empty hook, @code{#f}\n"
  162. "otherwise.")
  163. #define FUNC_NAME s_scm_hook_empty_p
  164. {
  165. SCM_VALIDATE_HOOK (1, hook);
  166. return scm_from_bool (scm_is_null (SCM_HOOK_PROCEDURES (hook)));
  167. }
  168. #undef FUNC_NAME
  169. SCM_DEFINE (scm_add_hook_x, "add-hook!", 2, 1, 0,
  170. (SCM hook, SCM proc, SCM append_p),
  171. "Add the procedure @var{proc} to the hook @var{hook}. The\n"
  172. "procedure is added to the end if @var{append_p} is true,\n"
  173. "otherwise it is added to the front. The return value of this\n"
  174. "procedure is not specified.")
  175. #define FUNC_NAME s_scm_add_hook_x
  176. {
  177. SCM rest;
  178. int n_args, p_req, p_opt, p_rest;
  179. SCM_VALIDATE_HOOK (1, hook);
  180. SCM_ASSERT (scm_i_procedure_arity (proc, &p_req, &p_opt, &p_rest),
  181. proc, SCM_ARG2, FUNC_NAME);
  182. n_args = SCM_HOOK_ARITY (hook);
  183. if (p_req > n_args || (!p_rest && p_req + p_opt < n_args))
  184. scm_wrong_type_arg (FUNC_NAME, 2, proc);
  185. rest = scm_delq_x (proc, SCM_HOOK_PROCEDURES (hook));
  186. SCM_SET_HOOK_PROCEDURES (hook,
  187. (!SCM_UNBNDP (append_p) && scm_is_true (append_p)
  188. ? scm_append_x (scm_list_2 (rest, scm_list_1 (proc)))
  189. : scm_cons (proc, rest)));
  190. return SCM_UNSPECIFIED;
  191. }
  192. #undef FUNC_NAME
  193. SCM_DEFINE (scm_remove_hook_x, "remove-hook!", 2, 0, 0,
  194. (SCM hook, SCM proc),
  195. "Remove the procedure @var{proc} from the hook @var{hook}. The\n"
  196. "return value of this procedure is not specified.")
  197. #define FUNC_NAME s_scm_remove_hook_x
  198. {
  199. SCM_VALIDATE_HOOK (1, hook);
  200. SCM_SET_HOOK_PROCEDURES (hook,
  201. scm_delq_x (proc, SCM_HOOK_PROCEDURES (hook)));
  202. return SCM_UNSPECIFIED;
  203. }
  204. #undef FUNC_NAME
  205. SCM_DEFINE (scm_reset_hook_x, "reset-hook!", 1, 0, 0,
  206. (SCM hook),
  207. "Remove all procedures from the hook @var{hook}. The return\n"
  208. "value of this procedure is not specified.")
  209. #define FUNC_NAME s_scm_reset_hook_x
  210. {
  211. SCM_VALIDATE_HOOK (1, hook);
  212. SCM_SET_HOOK_PROCEDURES (hook, SCM_EOL);
  213. return SCM_UNSPECIFIED;
  214. }
  215. #undef FUNC_NAME
  216. SCM_DEFINE (scm_run_hook, "run-hook", 1, 0, 1,
  217. (SCM hook, SCM args),
  218. "Apply all procedures from the hook @var{hook} to the arguments\n"
  219. "@var{args}. The order of the procedure application is first to\n"
  220. "last. The return value of this procedure is not specified.")
  221. #define FUNC_NAME s_scm_run_hook
  222. {
  223. SCM_VALIDATE_HOOK (1, hook);
  224. if (scm_ilength (args) != SCM_HOOK_ARITY (hook))
  225. SCM_MISC_ERROR ("Hook ~S requires ~A arguments",
  226. scm_list_2 (hook, scm_from_int (SCM_HOOK_ARITY (hook))));
  227. scm_c_run_hook (hook, args);
  228. return SCM_UNSPECIFIED;
  229. }
  230. #undef FUNC_NAME
  231. void
  232. scm_c_run_hook (SCM hook, SCM args)
  233. {
  234. SCM procs = SCM_HOOK_PROCEDURES (hook);
  235. while (scm_is_pair (procs))
  236. {
  237. scm_apply_0 (SCM_CAR (procs), args);
  238. procs = SCM_CDR (procs);
  239. }
  240. }
  241. void
  242. scm_c_run_hookn (SCM hook, SCM *argv, size_t nargs)
  243. {
  244. SCM procs = SCM_HOOK_PROCEDURES (hook);
  245. while (scm_is_pair (procs))
  246. {
  247. scm_call_n (SCM_CAR (procs), argv, nargs);
  248. procs = SCM_CDR (procs);
  249. }
  250. }
  251. SCM_DEFINE (scm_hook_to_list, "hook->list", 1, 0, 0,
  252. (SCM hook),
  253. "Convert the procedure list of @var{hook} to a list.")
  254. #define FUNC_NAME s_scm_hook_to_list
  255. {
  256. SCM_VALIDATE_HOOK (1, hook);
  257. return scm_list_copy (SCM_HOOK_PROCEDURES (hook));
  258. }
  259. #undef FUNC_NAME
  260. void
  261. scm_init_hooks ()
  262. {
  263. scm_tc16_hook = scm_make_smob_type ("hook", 0);
  264. scm_set_smob_print (scm_tc16_hook, hook_print);
  265. #include "libguile/hooks.x"
  266. }
  267. /*
  268. Local Variables:
  269. c-file-style: "gnu"
  270. End:
  271. */