(util_finish): Check event queue if the
state has changed. (util_register_event, util_event_remove, util_run_events): New functions.
Showing
1 changed file
with
57 additions
and
0 deletions
... | @@ -430,7 +430,10 @@ util_finish (struct imap4d_command *command, int rc, const char *format, ...) | ... | @@ -430,7 +430,10 @@ util_finish (struct imap4d_command *command, int rc, const char *format, ...) |
430 | new_state = STATE_NONE; | 430 | new_state = STATE_NONE; |
431 | 431 | ||
432 | if (new_state != STATE_NONE) | 432 | if (new_state != STATE_NONE) |
433 | { | ||
434 | util_run_events (state, new_state); | ||
433 | state = new_state; | 435 | state = new_state; |
436 | } | ||
434 | 437 | ||
435 | return status; | 438 | return status; |
436 | } | 439 | } |
... | @@ -1199,3 +1202,57 @@ util_bye () | ... | @@ -1199,3 +1202,57 @@ util_bye () |
1199 | list_do (atexit_list, atexit_run, 0); | 1202 | list_do (atexit_list, atexit_run, 0); |
1200 | } | 1203 | } |
1201 | 1204 | ||
1205 | struct state_event { | ||
1206 | int old_state; | ||
1207 | int new_state; | ||
1208 | list_action_t *action; | ||
1209 | void *data; | ||
1210 | }; | ||
1211 | |||
1212 | static list_t event_list; | ||
1213 | |||
1214 | void | ||
1215 | util_register_event (int old_state, int new_state, | ||
1216 | list_action_t *action, void *data) | ||
1217 | { | ||
1218 | struct state_event *evp = malloc (sizeof (*evp)); | ||
1219 | if (!evp) | ||
1220 | imap4d_bye (ERR_NO_MEM); | ||
1221 | evp->old_state = old_state; | ||
1222 | evp->new_state = new_state; | ||
1223 | evp->action = action; | ||
1224 | evp->data = data; | ||
1225 | if (!event_list) | ||
1226 | list_create (&event_list); | ||
1227 | list_append (event_list, (void*)evp); | ||
1228 | } | ||
1229 | |||
1230 | void | ||
1231 | util_event_remove (void *id) | ||
1232 | { | ||
1233 | list_remove (event_list, id); | ||
1234 | } | ||
1235 | |||
1236 | static int | ||
1237 | event_exec (void *item, void *data) | ||
1238 | { | ||
1239 | struct state_event *ev = data, *elem = item; | ||
1240 | |||
1241 | if (ev->old_state == elem->old_state && ev->new_state == elem->new_state) | ||
1242 | return elem->action (item, elem->data); | ||
1243 | return 0; | ||
1244 | } | ||
1245 | |||
1246 | void | ||
1247 | util_run_events (int old_state, int new_state) | ||
1248 | { | ||
1249 | if (event_list) | ||
1250 | { | ||
1251 | struct state_event ev; | ||
1252 | |||
1253 | ev.old_state = old_state; | ||
1254 | ev.new_state = new_state; | ||
1255 | list_do (event_list, event_exec, &ev); | ||
1256 | } | ||
1257 | } | ||
1258 | ... | ... |
-
Please register or sign in to post a comment