hooks.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /* Copyright (C) 1995,1996,1998,1999,2000,2001, 2003 Free Software Foundation, Inc.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2, or (at your option)
  6. * any later version.
  7. *
  8. * This program 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
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; see the file COPYING. If not, write to
  15. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  16. * Boston, MA 02110-1301 USA
  17. *
  18. * As a special exception, the Free Software Foundation gives permission
  19. * for additional uses of the text contained in its release of GUILE.
  20. *
  21. * The exception is that, if you link the GUILE library with other files
  22. * to produce an executable, this does not by itself cause the
  23. * resulting executable to be covered by the GNU General Public License.
  24. * Your use of that executable is in no way restricted on account of
  25. * linking the GUILE library code into it.
  26. *
  27. * This exception does not however invalidate any other reasons why
  28. * the executable file might be covered by the GNU General Public License.
  29. *
  30. * This exception applies only to the code released by the
  31. * Free Software Foundation under the name GUILE. If you copy
  32. * code from other Free Software Foundation releases into a copy of
  33. * GUILE, as the General Public License permits, the exception does
  34. * not apply to the code that you add in this way. To avoid misleading
  35. * anyone as to the status of such modified files, you must delete
  36. * this exception notice from them.
  37. *
  38. * If you write modifications of your own for GUILE, it is your choice
  39. * whether to permit this exception to apply to your modifications.
  40. * If you do not wish that, delete this exception notice. */
  41. #include <stdio.h>
  42. #include "libguile/_scm.h"
  43. #include "libguile/eval.h"
  44. #include "libguile/ports.h"
  45. #include "libguile/procprop.h"
  46. #include "libguile/root.h"
  47. #include "libguile/smob.h"
  48. #include "libguile/strings.h"
  49. #include "libguile/validate.h"
  50. #include "libguile/hooks.h"
  51. /* C level hooks
  52. *
  53. * Currently, this implementation is separate from the Scheme level
  54. * hooks. The possibility exists to implement the Scheme level hooks
  55. * using C level hooks.
  56. */
  57. void
  58. scm_c_hook_init (scm_t_c_hook *hook, void *hook_data, scm_t_c_hook_type type)
  59. {
  60. hook->first = 0;
  61. hook->type = type;
  62. hook->data = hook_data;
  63. }
  64. void
  65. scm_c_hook_add (scm_t_c_hook *hook,
  66. scm_t_c_hook_function func,
  67. void *func_data,
  68. int appendp)
  69. {
  70. scm_t_c_hook_entry *entry = scm_must_malloc (sizeof (scm_t_c_hook_entry),
  71. "C level hook entry");
  72. scm_t_c_hook_entry **loc = &hook->first;
  73. if (appendp)
  74. while (*loc)
  75. loc = &(*loc)->next;
  76. entry->next = *loc;
  77. entry->func = func;
  78. entry->data = func_data;
  79. *loc = entry;
  80. }
  81. void
  82. scm_c_hook_remove (scm_t_c_hook *hook,
  83. scm_t_c_hook_function func,
  84. void *func_data)
  85. {
  86. scm_t_c_hook_entry **loc = &hook->first;
  87. while (*loc)
  88. {
  89. if ((*loc)->func == func && (*loc)->data == func_data)
  90. {
  91. scm_t_c_hook_entry *entry = *loc;
  92. *loc = (*loc)->next;
  93. scm_must_free (entry);
  94. return;
  95. }
  96. loc = &(*loc)->next;
  97. }
  98. fprintf (stderr, "Attempt to remove non-existent hook function\n");
  99. abort ();
  100. }
  101. void *
  102. scm_c_hook_run (scm_t_c_hook *hook, void *data)
  103. {
  104. scm_t_c_hook_entry *entry = hook->first;
  105. scm_t_c_hook_type type = hook->type;
  106. void *res = 0;
  107. while (entry)
  108. {
  109. res = (entry->func) (hook->data, entry->data, data);
  110. if (res)
  111. {
  112. if (type == SCM_C_HOOK_OR)
  113. break;
  114. }
  115. else
  116. {
  117. if (type == SCM_C_HOOK_AND)
  118. break;
  119. }
  120. entry = entry->next;
  121. }
  122. return res;
  123. }
  124. /* Scheme level hooks
  125. *
  126. * A hook is basically a list of procedures to be called at well defined
  127. * points in time.
  128. *
  129. * Hook arity is not a full member of this type and therefore lacks an
  130. * accessor. It exists to aid debugging and is not intended to be used in
  131. * programs.
  132. */
  133. scm_t_bits scm_tc16_hook;
  134. static int
  135. hook_print (SCM hook, SCM port, scm_print_state *pstate)
  136. {
  137. SCM ls, name;
  138. scm_puts ("#<hook ", port);
  139. scm_intprint (SCM_HOOK_ARITY (hook), 10, port);
  140. scm_putc (' ', port);
  141. scm_intprint (SCM_UNPACK (hook), 16, port);
  142. ls = SCM_HOOK_PROCEDURES (hook);
  143. while (SCM_NIMP (ls))
  144. {
  145. scm_putc (' ', port);
  146. name = scm_procedure_name (SCM_CAR (ls));
  147. if (!SCM_FALSEP (name))
  148. scm_iprin1 (name, port, pstate);
  149. else
  150. scm_putc ('?', port);
  151. ls = SCM_CDR (ls);
  152. }
  153. scm_putc ('>', port);
  154. return 1;
  155. }
  156. #if (SCM_DEBUG_DEPRECATED == 0)
  157. SCM
  158. scm_create_hook (const char *name, int n_args)
  159. {
  160. SCM hook = scm_make_hook (SCM_MAKINUM (n_args));
  161. scm_c_define (name, hook);
  162. return scm_permanent_object (hook);
  163. }
  164. #endif
  165. SCM_DEFINE (scm_make_hook, "make-hook", 0, 1, 0,
  166. (SCM n_args),
  167. "Create a hook for storing procedure of arity @var{n_args}.\n"
  168. "@var{n_args} defaults to zero. The returned value is a hook\n"
  169. "object to be used with the other hook procedures.")
  170. #define FUNC_NAME s_scm_make_hook
  171. {
  172. int n;
  173. if (SCM_UNBNDP (n_args))
  174. {
  175. n = 0;
  176. }
  177. else
  178. {
  179. SCM_VALIDATE_INUM_COPY (SCM_ARG1, n_args, n);
  180. if (n < 0 || n > 16)
  181. SCM_OUT_OF_RANGE (SCM_ARG1, n_args);
  182. }
  183. SCM_RETURN_NEWSMOB (scm_tc16_hook + (n << 16), SCM_UNPACK (SCM_EOL));
  184. }
  185. #undef FUNC_NAME
  186. SCM_DEFINE (scm_hook_p, "hook?", 1, 0, 0,
  187. (SCM x),
  188. "Return @code{#t} if @var{x} is a hook, @code{#f} otherwise.")
  189. #define FUNC_NAME s_scm_hook_p
  190. {
  191. return SCM_BOOL (SCM_HOOKP (x));
  192. }
  193. #undef FUNC_NAME
  194. SCM_DEFINE (scm_hook_empty_p, "hook-empty?", 1, 0, 0,
  195. (SCM hook),
  196. "Return @code{#t} if @var{hook} is an empty hook, @code{#f}\n"
  197. "otherwise.")
  198. #define FUNC_NAME s_scm_hook_empty_p
  199. {
  200. SCM_VALIDATE_HOOK (1, hook);
  201. return SCM_BOOL (SCM_NULLP (SCM_HOOK_PROCEDURES (hook)));
  202. }
  203. #undef FUNC_NAME
  204. SCM_DEFINE (scm_add_hook_x, "add-hook!", 2, 1, 0,
  205. (SCM hook, SCM proc, SCM append_p),
  206. "Add the procedure @var{proc} to the hook @var{hook}. The\n"
  207. "procedure is added to the end if @var{append_p} is true,\n"
  208. "otherwise it is added to the front. The return value of this\n"
  209. "procedure is not specified.")
  210. #define FUNC_NAME s_scm_add_hook_x
  211. {
  212. SCM arity, rest;
  213. int n_args;
  214. SCM_VALIDATE_HOOK (1,hook);
  215. SCM_ASSERT (!SCM_FALSEP (arity = scm_i_procedure_arity (proc)),
  216. proc, SCM_ARG2, FUNC_NAME);
  217. n_args = SCM_HOOK_ARITY (hook);
  218. if (SCM_INUM (SCM_CAR (arity)) > n_args
  219. || (SCM_FALSEP (SCM_CADDR (arity))
  220. && (SCM_INUM (SCM_CAR (arity)) + SCM_INUM (SCM_CADR (arity))
  221. < n_args)))
  222. scm_wrong_type_arg (FUNC_NAME, 2, proc);
  223. rest = scm_delq_x (proc, SCM_HOOK_PROCEDURES (hook));
  224. SCM_SET_HOOK_PROCEDURES (hook,
  225. (!SCM_UNBNDP (append_p) && !SCM_FALSEP (append_p)
  226. ? scm_append_x (scm_list_2 (rest, scm_list_1 (proc)))
  227. : scm_cons (proc, rest)));
  228. return SCM_UNSPECIFIED;
  229. }
  230. #undef FUNC_NAME
  231. SCM_DEFINE (scm_remove_hook_x, "remove-hook!", 2, 0, 0,
  232. (SCM hook, SCM proc),
  233. "Remove the procedure @var{proc} from the hook @var{hook}. The\n"
  234. "return value of this procedure is not specified.")
  235. #define FUNC_NAME s_scm_remove_hook_x
  236. {
  237. SCM_VALIDATE_HOOK (1, hook);
  238. SCM_SET_HOOK_PROCEDURES (hook,
  239. scm_delq_x (proc, SCM_HOOK_PROCEDURES (hook)));
  240. return SCM_UNSPECIFIED;
  241. }
  242. #undef FUNC_NAME
  243. SCM_DEFINE (scm_reset_hook_x, "reset-hook!", 1, 0, 0,
  244. (SCM hook),
  245. "Remove all procedures from the hook @var{hook}. The return\n"
  246. "value of this procedure is not specified.")
  247. #define FUNC_NAME s_scm_reset_hook_x
  248. {
  249. SCM_VALIDATE_HOOK (1,hook);
  250. SCM_SET_HOOK_PROCEDURES (hook, SCM_EOL);
  251. return SCM_UNSPECIFIED;
  252. }
  253. #undef FUNC_NAME
  254. SCM_DEFINE (scm_run_hook, "run-hook", 1, 0, 1,
  255. (SCM hook, SCM args),
  256. "Apply all procedures from the hook @var{hook} to the arguments\n"
  257. "@var{args}. The order of the procedure application is first to\n"
  258. "last. The return value of this procedure is not specified.")
  259. #define FUNC_NAME s_scm_run_hook
  260. {
  261. SCM_VALIDATE_HOOK (1,hook);
  262. if (scm_ilength (args) != SCM_HOOK_ARITY (hook))
  263. SCM_MISC_ERROR ("Hook ~S requires ~A arguments",
  264. scm_list_2 (hook, SCM_MAKINUM (SCM_HOOK_ARITY (hook))));
  265. scm_c_run_hook (hook, args);
  266. return SCM_UNSPECIFIED;
  267. }
  268. #undef FUNC_NAME
  269. void
  270. scm_c_run_hook (SCM hook, SCM args)
  271. {
  272. SCM procs = SCM_HOOK_PROCEDURES (hook);
  273. while (SCM_NIMP (procs))
  274. {
  275. scm_apply_0 (SCM_CAR (procs), args);
  276. procs = SCM_CDR (procs);
  277. }
  278. }
  279. SCM_DEFINE (scm_hook_to_list, "hook->list", 1, 0, 0,
  280. (SCM hook),
  281. "Convert the procedure list of @var{hook} to a list.")
  282. #define FUNC_NAME s_scm_hook_to_list
  283. {
  284. SCM_VALIDATE_HOOK (1, hook);
  285. return scm_list_copy (SCM_HOOK_PROCEDURES (hook));
  286. }
  287. #undef FUNC_NAME
  288. void
  289. scm_init_hooks ()
  290. {
  291. scm_tc16_hook = scm_make_smob_type ("hook", 0);
  292. scm_set_smob_mark (scm_tc16_hook, scm_markcdr);
  293. scm_set_smob_print (scm_tc16_hook, hook_print);
  294. #include "libguile/hooks.x"
  295. }
  296. /*
  297. Local Variables:
  298. c-file-style: "gnu"
  299. End:
  300. */