hooks.c 7.8 KB

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