Neale Pickett
·
2017-04-21
rread.c
1/*
2 * Contains functions for dealing with things that happen in the
3 * future.
4 *
5 * @(#)rread.c 4.7 (Berkeley) 02/05/99
6 */
7
8#include <curses.h>
9#include "netprot.h"
10
11#define EMPTY 0
12#define DAEMON -1
13#define MAXDAEMONS 20
14
15#define _X_ { EMPTY }
16
17struct delayed_action {
18 int d_type;
19 void (*d_func)(int);
20 int d_arg;
21 int d_time;
22} d_list[MAXDAEMONS] = {
23 _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_,
24 _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_,
25};
26
27/*
28 * d_slot:
29 * Find an empty slot in the daemon/fuse list
30 */
31struct delayed_action *
32d_slot(void)
33{
34 struct delayed_action *dev;
35
36 for (dev = d_list; dev < &d_list[MAXDAEMONS]; dev++)
37 if (dev->d_type == EMPTY)
38 return dev;
39#ifdef MASTER
40 debug("Ran out of fuse slots");
41#endif
42 return NULL;
43}
44
45/*
46 * find_slot:
47 * Find a particular slot in the table
48 */
49struct delayed_action *
50find_slot(void (*func)(int))
51{
52 struct delayed_action *dev;
53
54 for (dev = d_list; dev < &d_list[MAXDAEMONS]; dev++)
55 if (dev->d_type != EMPTY && func == dev->d_func)
56 return dev;
57 return NULL;
58}
59
60/*
61 * start_daemon:
62 * Start a daemon, takes a function.
63 */
64void
65start_daemon(void (*func)(int), int arg, int type)
66{
67 struct delayed_action *dev;
68
69 dev = d_slot();
70 dev->d_type = type;
71 dev->d_func = func;
72 dev->d_arg = arg;
73 dev->d_time = DAEMON;
74}
75
76/*
77 * kill_daemon:
78 * Remove a daemon from the list
79 */
80void
81kill_daemon(void (*func)(int))
82{
83 struct delayed_action *dev;
84
85 if ((dev = find_slot(func)) == NULL)
86 return;
87 /*
88 * Take it out of the list
89 */
90 dev->d_type = EMPTY;
91}
92
93/*
94 * do_daemons:
95 * Run all the daemons that are active with the current flag,
96 * passing the argument to the function.
97 */
98void
99do_daemons(int flag)
100{
101 struct delayed_action *dev;
102
103 /*
104 * Loop through the devil list
105 */
106 for (dev = d_list; dev < &d_list[MAXDAEMONS]; dev++)
107 /*
108 * Executing each one, giving it the proper arguments
109 */
110 if (dev->d_type == flag && dev->d_time == DAEMON)
111 (*dev->d_func)(dev->d_arg);
112}
113
114/*
115 * fuse:
116 * Start a fuse to go off in a certain number of turns
117 */
118void
119fuse(void (*func)(int), int arg, int tm, int type)
120{
121 struct delayed_action *wire;
122
123 wire = d_slot();
124 wire->d_type = type;
125 wire->d_func = func;
126 wire->d_arg = arg;
127 wire->d_time = tm;
128}
129
130/*
131 * lengthen:
132 * Increase the time until a fuse goes off
133 */
134void
135lengthen(void (*func)(int), int xtime)
136{
137 struct delayed_action *wire;
138
139 if ((wire = find_slot(func)) == NULL)
140 return;
141 wire->d_time += xtime;
142}
143
144/*
145 * extinguish:
146 * Put out a fuse
147 */
148void
149extinguish(void (*func)(int))
150{
151 struct delayed_action *wire;
152
153 if ((wire = find_slot(func)) == NULL)
154 return;
155 wire->d_type = EMPTY;
156}
157
158/*
159 * do_fuses:
160 * Decrement counters and start needed fuses
161 */
162void
163do_fuses(int flag)
164{
165 struct delayed_action *wire;
166
167 /*
168 * Step though the list
169 */
170 for (wire = d_list; wire < &d_list[MAXDAEMONS]; wire++)
171 /*
172 * Decrementing counters and starting things we want. We also need
173 * to remove the fuse from the list once it has gone off.
174 */
175 if (flag == wire->d_type && wire->d_time > 0 && --wire->d_time == 0)
176 {
177 wire->d_type = EMPTY;
178 (*wire->d_func)(wire->d_arg);
179 }
180}