hooks.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * hook.c -- handles:
  3. *
  4. * hooks
  5. *
  6. */
  7. #include "common.h"
  8. #include "hooks.h"
  9. /*
  10. * Various hooks & things
  11. */
  12. /* The REAL hooks, when these are called, a return of 0 indicates unhandled
  13. * 1 is handled
  14. */
  15. struct hook_entry *hook_list[REAL_HOOKS];
  16. void
  17. hooks_init()
  18. {
  19. int i;
  20. for (i = 0; i < REAL_HOOKS; i++)
  21. hook_list[i] = NULL;
  22. }
  23. int
  24. call_hook_cccc(int hooknum, char *a, char *b, char *c, char *d)
  25. {
  26. struct hook_entry *p, *pn;
  27. int f = 0;
  28. if (hooknum >= REAL_HOOKS)
  29. return 0;
  30. p = hook_list[hooknum];
  31. for (p = hook_list[hooknum]; p && !f; p = pn) {
  32. pn = p->next;
  33. f = p->func(a, b, c, d);
  34. }
  35. return f;
  36. }
  37. /* Hooks, various tables of functions to call on ceratin events
  38. */
  39. void
  40. add_hook(int hook_num, Function func)
  41. {
  42. if (hook_num < REAL_HOOKS) {
  43. struct hook_entry *p = NULL;
  44. for (p = hook_list[hook_num]; p; p = p->next)
  45. if (p->func == func)
  46. return; /* Don't add it if it's already there */
  47. p = calloc(1, sizeof(struct hook_entry));
  48. p->next = hook_list[hook_num];
  49. hook_list[hook_num] = p;
  50. p->func = func;
  51. }
  52. }
  53. void
  54. del_hook(int hook_num, Function func)
  55. {
  56. if (hook_num < REAL_HOOKS) {
  57. struct hook_entry *p = hook_list[hook_num], *o = NULL;
  58. while (p) {
  59. if (p->func == func) {
  60. if (o == NULL)
  61. hook_list[hook_num] = p->next;
  62. else
  63. o->next = p->next;
  64. free(p);
  65. break;
  66. }
  67. o = p;
  68. p = p->next;
  69. }
  70. }
  71. }