hooks.c 7.7 KB

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