hooks.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 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. void
  38. scm_c_hook_init (scm_t_c_hook *hook, void *hook_data, scm_t_c_hook_type type)
  39. {
  40. hook->first = 0;
  41. hook->type = type;
  42. hook->data = hook_data;
  43. }
  44. void
  45. scm_c_hook_add (scm_t_c_hook *hook,
  46. scm_t_c_hook_function func,
  47. void *fn_data,
  48. int appendp)
  49. {
  50. scm_t_c_hook_entry *entry = scm_malloc (sizeof (scm_t_c_hook_entry));
  51. scm_t_c_hook_entry **loc = &hook->first;
  52. if (appendp)
  53. while (*loc)
  54. loc = &(*loc)->next;
  55. entry->next = *loc;
  56. entry->func = func;
  57. entry->data = fn_data;
  58. *loc = entry;
  59. }
  60. void
  61. scm_c_hook_remove (scm_t_c_hook *hook,
  62. scm_t_c_hook_function func,
  63. void *fn_data)
  64. {
  65. scm_t_c_hook_entry **loc = &hook->first;
  66. while (*loc)
  67. {
  68. if ((*loc)->func == func && (*loc)->data == fn_data)
  69. {
  70. scm_t_c_hook_entry *entry = *loc;
  71. *loc = (*loc)->next;
  72. free (entry);
  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_NIMP (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 arity, rest;
  177. int n_args;
  178. SCM_VALIDATE_HOOK (1, hook);
  179. SCM_ASSERT (scm_is_true (arity = scm_i_procedure_arity (proc)),
  180. proc, SCM_ARG2, FUNC_NAME);
  181. n_args = SCM_HOOK_ARITY (hook);
  182. if (scm_to_int (SCM_CAR (arity)) > n_args
  183. || (scm_is_false (SCM_CADDR (arity))
  184. && (scm_to_int (SCM_CAR (arity)) + scm_to_int (SCM_CADR (arity))
  185. < n_args)))
  186. scm_wrong_type_arg (FUNC_NAME, 2, proc);
  187. rest = scm_delq_x (proc, SCM_HOOK_PROCEDURES (hook));
  188. SCM_SET_HOOK_PROCEDURES (hook,
  189. (!SCM_UNBNDP (append_p) && scm_is_true (append_p)
  190. ? scm_append_x (scm_list_2 (rest, scm_list_1 (proc)))
  191. : scm_cons (proc, rest)));
  192. return SCM_UNSPECIFIED;
  193. }
  194. #undef FUNC_NAME
  195. SCM_DEFINE (scm_remove_hook_x, "remove-hook!", 2, 0, 0,
  196. (SCM hook, SCM proc),
  197. "Remove the procedure @var{proc} from the hook @var{hook}. The\n"
  198. "return value of this procedure is not specified.")
  199. #define FUNC_NAME s_scm_remove_hook_x
  200. {
  201. SCM_VALIDATE_HOOK (1, hook);
  202. SCM_SET_HOOK_PROCEDURES (hook,
  203. scm_delq_x (proc, SCM_HOOK_PROCEDURES (hook)));
  204. return SCM_UNSPECIFIED;
  205. }
  206. #undef FUNC_NAME
  207. SCM_DEFINE (scm_reset_hook_x, "reset-hook!", 1, 0, 0,
  208. (SCM hook),
  209. "Remove all procedures from the hook @var{hook}. The return\n"
  210. "value of this procedure is not specified.")
  211. #define FUNC_NAME s_scm_reset_hook_x
  212. {
  213. SCM_VALIDATE_HOOK (1, hook);
  214. SCM_SET_HOOK_PROCEDURES (hook, SCM_EOL);
  215. return SCM_UNSPECIFIED;
  216. }
  217. #undef FUNC_NAME
  218. SCM_DEFINE (scm_run_hook, "run-hook", 1, 0, 1,
  219. (SCM hook, SCM args),
  220. "Apply all procedures from the hook @var{hook} to the arguments\n"
  221. "@var{args}. The order of the procedure application is first to\n"
  222. "last. The return value of this procedure is not specified.")
  223. #define FUNC_NAME s_scm_run_hook
  224. {
  225. SCM_VALIDATE_HOOK (1, hook);
  226. if (scm_ilength (args) != SCM_HOOK_ARITY (hook))
  227. SCM_MISC_ERROR ("Hook ~S requires ~A arguments",
  228. scm_list_2 (hook, scm_from_int (SCM_HOOK_ARITY (hook))));
  229. scm_c_run_hook (hook, args);
  230. return SCM_UNSPECIFIED;
  231. }
  232. #undef FUNC_NAME
  233. void
  234. scm_c_run_hook (SCM hook, SCM args)
  235. {
  236. SCM procs = SCM_HOOK_PROCEDURES (hook);
  237. while (SCM_NIMP (procs))
  238. {
  239. scm_apply_0 (SCM_CAR (procs), args);
  240. procs = SCM_CDR (procs);
  241. }
  242. }
  243. SCM_DEFINE (scm_hook_to_list, "hook->list", 1, 0, 0,
  244. (SCM hook),
  245. "Convert the procedure list of @var{hook} to a list.")
  246. #define FUNC_NAME s_scm_hook_to_list
  247. {
  248. SCM_VALIDATE_HOOK (1, hook);
  249. return scm_list_copy (SCM_HOOK_PROCEDURES (hook));
  250. }
  251. #undef FUNC_NAME
  252. void
  253. scm_init_hooks ()
  254. {
  255. scm_tc16_hook = scm_make_smob_type ("hook", 0);
  256. scm_set_smob_mark (scm_tc16_hook, scm_markcdr);
  257. scm_set_smob_print (scm_tc16_hook, hook_print);
  258. #include "libguile/hooks.x"
  259. }
  260. /*
  261. Local Variables:
  262. c-file-style: "gnu"
  263. End:
  264. */