summaryrefslogtreecommitdiffstats
path: root/lib/hook.c
blob: c67d75c61301e29ab9999fdda4f8541029b2f1b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
   Hooks.

   Slavaz: Warning! this file is deprecated and should be replaced
   by mcevents functional.

   Copyright (C) 1994-2023
   Free Software Foundation, Inc.

   Written by:
   Miguel de Icaza, 1994, 1995, 1996
   Janne Kukonlehto, 1994, 1995, 1996
   Dugan Porter, 1994, 1995, 1996
   Jakub Jelinek, 1994, 1995, 1996
   Mauricio Plaza, 1994, 1995, 1996

   This file is part of the Midnight Commander.

   The Midnight Commander is free software: you can redistribute it
   and/or modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation, either version 3 of the License,
   or (at your option) any later version.

   The Midnight Commander is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/** \file
 *  \brief Source: hooks
 */

#include <config.h>

#include "lib/global.h"
#include "lib/hook.h"

/*** global variables ****************************************************************************/

/*** file scope macro definitions ****************************************************************/

/*** file scope type declarations ****************************************************************/

/*** file scope variables ************************************************************************/

/*** file scope functions ************************************************************************/

/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */

void
add_hook (hook_t ** hook_list, void (*hook_fn) (void *), void *data)
{
    hook_t *new_hook = g_new (hook_t, 1);

    new_hook->hook_fn = hook_fn;
    new_hook->next = *hook_list;
    new_hook->hook_data = data;

    *hook_list = new_hook;
}

/* --------------------------------------------------------------------------------------------- */

void
execute_hooks (hook_t * hook_list)
{
    hook_t *new_hook = NULL;
    hook_t *p;

    /* We copy the hook list first so tahat we let the hook
     * function call delete_hook
     */

    while (hook_list != NULL)
    {
        add_hook (&new_hook, hook_list->hook_fn, hook_list->hook_data);
        hook_list = hook_list->next;
    }
    p = new_hook;

    while (new_hook != NULL)
    {
        new_hook->hook_fn (new_hook->hook_data);
        new_hook = new_hook->next;
    }

    for (hook_list = p; hook_list != NULL;)
    {
        p = hook_list;
        hook_list = hook_list->next;
        g_free (p);
    }
}

/* --------------------------------------------------------------------------------------------- */

void
delete_hook (hook_t ** hook_list, void (*hook_fn) (void *))
{
    hook_t *new_list = NULL;
    hook_t *current, *next;

    for (current = *hook_list; current != NULL; current = next)
    {
        next = current->next;
        if (current->hook_fn == hook_fn)
            g_free (current);
        else
            add_hook (&new_list, current->hook_fn, current->hook_data);
    }
    *hook_list = new_list;
}

/* --------------------------------------------------------------------------------------------- */

gboolean
hook_present (hook_t * hook_list, void (*hook_fn) (void *))
{
    hook_t *p;

    for (p = hook_list; p != NULL; p = p->next)
        if (p->hook_fn == hook_fn)
            return TRUE;
    return FALSE;
}

/* --------------------------------------------------------------------------------------------- */