hook.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. Hooks.
  3. Slavaz: Warning! this file is deprecated and should be replaced
  4. by mcevents functional.
  5. Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
  6. 2004, 2005, 2007, 2009, 2010, 2011
  7. The Free Software Foundation, Inc.
  8. Written by:
  9. Miguel de Icaza, 1994, 1995, 1996
  10. Janne Kukonlehto, 1994, 1995, 1996
  11. Dugan Porter, 1994, 1995, 1996
  12. Jakub Jelinek, 1994, 1995, 1996
  13. Mauricio Plaza, 1994, 1995, 1996
  14. The file_date routine is mostly from GNU's fileutils package,
  15. written by Richard Stallman and David MacKenzie.
  16. This file is part of the Midnight Commander.
  17. The Midnight Commander is free software: you can redistribute it
  18. and/or modify it under the terms of the GNU General Public License as
  19. published by the Free Software Foundation, either version 3 of the License,
  20. or (at your option) any later version.
  21. The Midnight Commander is distributed in the hope that it will be useful,
  22. but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. GNU General Public License for more details.
  25. You should have received a copy of the GNU General Public License
  26. along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. */
  28. /** \file
  29. * \brief Source: hooks
  30. */
  31. #include <config.h>
  32. #include "lib/global.h"
  33. #include "lib/hook.h"
  34. /*** global variables ****************************************************************************/
  35. /*** file scope macro definitions ****************************************************************/
  36. /*** file scope type declarations ****************************************************************/
  37. /*** file scope variables ************************************************************************/
  38. /*** file scope functions ************************************************************************/
  39. /* --------------------------------------------------------------------------------------------- */
  40. /*** public functions ****************************************************************************/
  41. /* --------------------------------------------------------------------------------------------- */
  42. void
  43. add_hook (hook_t ** hook_list, void (*hook_fn) (void *), void *data)
  44. {
  45. hook_t *new_hook = g_new (hook_t, 1);
  46. new_hook->hook_fn = hook_fn;
  47. new_hook->next = *hook_list;
  48. new_hook->hook_data = data;
  49. *hook_list = new_hook;
  50. }
  51. /* --------------------------------------------------------------------------------------------- */
  52. void
  53. execute_hooks (hook_t * hook_list)
  54. {
  55. hook_t *new_hook = NULL;
  56. hook_t *p;
  57. /* We copy the hook list first so tahat we let the hook
  58. * function call delete_hook
  59. */
  60. while (hook_list != NULL)
  61. {
  62. add_hook (&new_hook, hook_list->hook_fn, hook_list->hook_data);
  63. hook_list = hook_list->next;
  64. }
  65. p = new_hook;
  66. while (new_hook != NULL)
  67. {
  68. new_hook->hook_fn (new_hook->hook_data);
  69. new_hook = new_hook->next;
  70. }
  71. for (hook_list = p; hook_list != NULL;)
  72. {
  73. p = hook_list;
  74. hook_list = hook_list->next;
  75. g_free (p);
  76. }
  77. }
  78. /* --------------------------------------------------------------------------------------------- */
  79. void
  80. delete_hook (hook_t ** hook_list, void (*hook_fn) (void *))
  81. {
  82. hook_t *new_list = NULL;
  83. hook_t *current, *next;
  84. for (current = *hook_list; current != NULL; current = next)
  85. {
  86. next = current->next;
  87. if (current->hook_fn == hook_fn)
  88. g_free (current);
  89. else
  90. add_hook (&new_list, current->hook_fn, current->hook_data);
  91. }
  92. *hook_list = new_list;
  93. }
  94. /* --------------------------------------------------------------------------------------------- */
  95. gboolean
  96. hook_present (hook_t * hook_list, void (*hook_fn) (void *))
  97. {
  98. hook_t *p;
  99. for (p = hook_list; p != NULL; p = p->next)
  100. if (p->hook_fn == hook_fn)
  101. return TRUE;
  102. return FALSE;
  103. }
  104. /* --------------------------------------------------------------------------------------------- */