hooks.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /* Copyright (C) 1995, 1996, 1998, 1999, 2000, 2002 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., 59 Temple Place, Suite 330,
  16. * Boston, MA 02111-1307 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/objprop.h"
  46. #include "libguile/procprop.h"
  47. #include "libguile/root.h"
  48. #include "libguile/smob.h"
  49. #include "libguile/strings.h"
  50. #include "libguile/validate.h"
  51. #include "libguile/hooks.h"
  52. /* C level hooks
  53. *
  54. * Currently, this implementation is separate from the Scheme level
  55. * hooks. The possibility exists to implement the Scheme level hooks
  56. * using C level hooks.
  57. */
  58. void
  59. scm_c_hook_init (scm_c_hook_t *hook, void *hook_data, scm_c_hook_type_t type)
  60. {
  61. hook->first = 0;
  62. hook->type = type;
  63. hook->data = hook_data;
  64. }
  65. void
  66. scm_c_hook_add (scm_c_hook_t *hook,
  67. scm_c_hook_function_t func,
  68. void *func_data,
  69. int appendp)
  70. {
  71. scm_c_hook_entry_t *entry = scm_must_malloc (sizeof (scm_c_hook_entry_t),
  72. "C level hook entry");
  73. scm_c_hook_entry_t **loc = &hook->first;
  74. if (appendp)
  75. while (*loc)
  76. *loc = (*loc)->next;
  77. entry->next = *loc;
  78. entry->func = func;
  79. entry->data = func_data;
  80. *loc = entry;
  81. }
  82. void
  83. scm_c_hook_remove (scm_c_hook_t *hook,
  84. scm_c_hook_function_t func,
  85. void *func_data)
  86. {
  87. scm_c_hook_entry_t **loc = &hook->first;
  88. while (*loc)
  89. {
  90. if ((*loc)->func == func && (*loc)->data == func_data)
  91. {
  92. scm_c_hook_entry_t *entry = *loc;
  93. *loc = (*loc)->next;
  94. scm_must_free (entry);
  95. return;
  96. }
  97. loc = &(*loc)->next;
  98. }
  99. fprintf (stderr, "Attempt to remove non-existent hook function\n");
  100. abort ();
  101. }
  102. void *
  103. scm_c_hook_run (scm_c_hook_t *hook, void *data)
  104. {
  105. scm_c_hook_entry_t *entry = hook->first;
  106. scm_c_hook_type_t type = hook->type;
  107. void *res = 0;
  108. while (entry)
  109. {
  110. res = (entry->func) (hook->data, entry->data, data);
  111. if (res)
  112. {
  113. if (type == SCM_C_HOOK_OR)
  114. break;
  115. }
  116. else
  117. {
  118. if (type == SCM_C_HOOK_AND)
  119. break;
  120. }
  121. entry = entry->next;
  122. }
  123. return res;
  124. }
  125. /* Scheme level hooks
  126. *
  127. * A hook is basically a list of procedures to be called at well defined
  128. * points in time.
  129. *
  130. * Hook arity is not a full member of this type and therefore lacks an
  131. * accessor. It exists to aid debugging and is not intended to be used in
  132. * programs.
  133. */
  134. long scm_tc16_hook;
  135. static SCM
  136. make_hook (SCM n_args, const char *subr)
  137. {
  138. int n;
  139. if (SCM_UNBNDP (n_args))
  140. {
  141. n = 0;
  142. }
  143. else
  144. {
  145. SCM_ASSERT (SCM_INUMP (n_args), n_args, SCM_ARGn, subr);
  146. n = SCM_INUM (n_args);
  147. SCM_ASSERT (n >= 0 && n <= 16, n_args, SCM_OUTOFRANGE, subr);
  148. }
  149. SCM_RETURN_NEWSMOB (scm_tc16_hook + (n << 16), SCM_EOL);
  150. }
  151. static int
  152. print_hook (SCM hook, SCM port, scm_print_state *pstate)
  153. {
  154. SCM ls, name;
  155. scm_puts ("#<hook ", port);
  156. scm_intprint (SCM_HOOK_ARITY (hook), 10, port);
  157. scm_putc (' ', port);
  158. scm_intprint (SCM_UNPACK (hook), 16, port);
  159. ls = SCM_HOOK_PROCEDURES (hook);
  160. while (SCM_NIMP (ls))
  161. {
  162. scm_putc (' ', port);
  163. name = scm_procedure_name (SCM_CAR (ls));
  164. if (SCM_NFALSEP (name))
  165. scm_iprin1 (name, port, pstate);
  166. else
  167. scm_putc ('?', port);
  168. ls = SCM_CDR (ls);
  169. }
  170. scm_putc ('>', port);
  171. return 1;
  172. }
  173. SCM_SYMBOL (symbol_name, "name");
  174. SCM
  175. scm_create_hook (const char* name, int n_args)
  176. {
  177. SCM vcell = scm_sysintern0 (name);
  178. SCM hook = make_hook (SCM_MAKINUM (n_args), "scm_create_hook");
  179. SCM_SETCDR (vcell, hook);
  180. scm_set_object_property_x (hook, symbol_name, scm_makfrom0str (name));
  181. scm_protect_object (hook);
  182. return hook;
  183. }
  184. #if (SCM_DEBUG_DEPRECATED == 0)
  185. SCM_DEFINE (scm_make_hook_with_name, "make-hook-with-name", 1, 1, 0,
  186. (SCM name, SCM n_args),
  187. "")
  188. #define FUNC_NAME s_scm_make_hook_with_name
  189. {
  190. SCM hook = make_hook (n_args, FUNC_NAME);
  191. scm_set_object_property_x (hook, scm_makfrom0str ("name"), name);
  192. return hook;
  193. }
  194. #undef FUNC_NAME
  195. #endif /* SCM_DEBUG_DEPRECATED == 0 */
  196. SCM_DEFINE (scm_make_hook, "make-hook", 0, 1, 0,
  197. (SCM n_args),
  198. "")
  199. #define FUNC_NAME s_scm_make_hook
  200. {
  201. return make_hook (n_args, FUNC_NAME);
  202. }
  203. #undef FUNC_NAME
  204. SCM_DEFINE (scm_hook_p, "hook?", 1, 0, 0,
  205. (SCM x),
  206. "")
  207. #define FUNC_NAME s_scm_hook_p
  208. {
  209. return SCM_BOOL (SCM_HOOKP (x));
  210. }
  211. #undef FUNC_NAME
  212. SCM_DEFINE (scm_hook_empty_p, "hook-empty?", 1, 0, 0,
  213. (SCM hook),
  214. "")
  215. #define FUNC_NAME s_scm_hook_empty_p
  216. {
  217. SCM_VALIDATE_HOOK (1, hook);
  218. return SCM_BOOL (SCM_NULLP (SCM_HOOK_PROCEDURES (hook)));
  219. }
  220. #undef FUNC_NAME
  221. SCM_DEFINE (scm_add_hook_x, "add-hook!", 2, 1, 0,
  222. (SCM hook, SCM proc, SCM append_p),
  223. "")
  224. #define FUNC_NAME s_scm_add_hook_x
  225. {
  226. SCM arity, rest;
  227. int n_args;
  228. SCM_VALIDATE_HOOK (1,hook);
  229. SCM_ASSERT (SCM_NFALSEP (arity = scm_i_procedure_arity (proc)),
  230. proc, SCM_ARG2, FUNC_NAME);
  231. n_args = SCM_HOOK_ARITY (hook);
  232. if (SCM_INUM (SCM_CAR (arity)) > n_args
  233. || (SCM_FALSEP (SCM_CADDR (arity))
  234. && (SCM_INUM (SCM_CAR (arity)) + SCM_INUM (SCM_CADR (arity))
  235. < n_args)))
  236. scm_wrong_type_arg (FUNC_NAME, 2, proc);
  237. rest = scm_delq_x (proc, SCM_HOOK_PROCEDURES (hook));
  238. SCM_SET_HOOK_PROCEDURES (hook,
  239. (!SCM_UNBNDP (append_p) && !SCM_FALSEP (append_p)
  240. ? scm_append_x (SCM_LIST2 (rest, SCM_LIST1 (proc)))
  241. : scm_cons (proc, rest)));
  242. return SCM_UNSPECIFIED;
  243. }
  244. #undef FUNC_NAME
  245. SCM_DEFINE (scm_remove_hook_x, "remove-hook!", 2, 0, 0,
  246. (SCM hook, SCM proc),
  247. "")
  248. #define FUNC_NAME s_scm_remove_hook_x
  249. {
  250. SCM_VALIDATE_HOOK (1, hook);
  251. SCM_SET_HOOK_PROCEDURES (hook,
  252. scm_delq_x (proc, SCM_HOOK_PROCEDURES (hook)));
  253. return SCM_UNSPECIFIED;
  254. }
  255. #undef FUNC_NAME
  256. SCM_DEFINE (scm_reset_hook_x, "reset-hook!", 1, 0, 0,
  257. (SCM hook),
  258. "")
  259. #define FUNC_NAME s_scm_reset_hook_x
  260. {
  261. SCM_VALIDATE_HOOK (1,hook);
  262. SCM_SET_HOOK_PROCEDURES (hook, SCM_EOL);
  263. return SCM_UNSPECIFIED;
  264. }
  265. #undef FUNC_NAME
  266. SCM_DEFINE (scm_run_hook, "run-hook", 1, 0, 1,
  267. (SCM hook, SCM args),
  268. "")
  269. #define FUNC_NAME s_scm_run_hook
  270. {
  271. SCM_VALIDATE_HOOK (1,hook);
  272. if (scm_ilength (args) != SCM_HOOK_ARITY (hook))
  273. SCM_MISC_ERROR ("Hook ~S requires ~A arguments",
  274. SCM_LIST2 (hook,SCM_MAKINUM (SCM_HOOK_ARITY (hook))));
  275. scm_c_run_hook (hook, args);
  276. return SCM_UNSPECIFIED;
  277. }
  278. #undef FUNC_NAME
  279. void
  280. scm_c_run_hook (SCM hook, SCM args)
  281. {
  282. SCM procs = SCM_HOOK_PROCEDURES (hook);
  283. while (SCM_NIMP (procs))
  284. {
  285. scm_apply (SCM_CAR (procs), args, SCM_EOL);
  286. procs = SCM_CDR (procs);
  287. }
  288. }
  289. SCM_DEFINE (scm_hook_to_list, "hook->list", 1, 0, 0,
  290. (SCM hook),
  291. "")
  292. #define FUNC_NAME s_scm_hook_to_list
  293. {
  294. SCM_VALIDATE_HOOK (1, hook);
  295. return scm_list_copy (SCM_HOOK_PROCEDURES (hook));
  296. }
  297. #undef FUNC_NAME
  298. void
  299. scm_init_hooks ()
  300. {
  301. scm_tc16_hook = scm_make_smob_type ("hook", 0);
  302. scm_set_smob_mark (scm_tc16_hook, scm_markcdr);
  303. scm_set_smob_print (scm_tc16_hook, print_hook);
  304. #include "libguile/hooks.x"
  305. }
  306. /*
  307. Local Variables:
  308. c-file-style: "gnu"
  309. End:
  310. */