| 1 |
/* |
| 2 |
* Copyright (c) 2004-2015 Stefan Ulrich and the xdvik development team |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
* of this software and associated documentation files (the "Software"), to |
| 6 |
* deal in the Software without restriction, including without limitation the |
| 7 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 8 |
* sell copies of the Software, and to permit persons to whom the Software is |
| 9 |
* furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice shall be included in |
| 12 |
* all copies or substantial portions of the Software. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 |
* PAUL VOJTA OR ANY OTHER AUTHOR OF THIS SOFTWARE BE LIABLE FOR ANY CLAIM, |
| 18 |
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 19 |
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 20 |
* OTHER DEALINGS IN THE SOFTWARE. |
| 21 |
* |
| 22 |
*/ |
| 23 |
|
| 24 |
/* |
| 25 |
Exit handlers: Procedures taking a void * that can be registered |
| 26 |
via register_exit_handler() and will be called fromx xdvi_exit(). |
| 27 |
*/ |
| 28 |
|
| 29 |
#include "xdvi-config.h" |
| 30 |
#include "xdvi.h" |
| 31 |
#include "exit-handlers.h" |
| 32 |
#include "util.h" |
| 33 |
|
| 34 |
static struct exit_list { |
| 35 |
exit_procedure proc; |
| 36 |
void *arg; |
| 37 |
struct exit_list *next; |
| 38 |
} *exit_procs = NULL; |
| 39 |
|
| 40 |
void register_exit_handler(exit_procedure proc, void *arg) |
| 41 |
{ |
| 42 |
struct exit_list *ep; |
| 43 |
|
| 44 |
/* first check for duplicates (needed because of remove_tmp_dvi_file) */ |
| 45 |
for (ep = exit_procs; ep != NULL; ep = ep->next) { |
| 46 |
if (ep->proc == proc && ep->arg == arg) |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
ep = xmalloc(sizeof *ep); |
| 51 |
ep->proc = proc; |
| 52 |
ep->arg = arg; |
| 53 |
ep->next = exit_procs; |
| 54 |
exit_procs = ep; |
| 55 |
} |
| 56 |
|
| 57 |
#if 0 /* This is currently unused. */ |
| 58 |
void unregister_exit_handler(exit_procedure proc) |
| 59 |
{ |
| 60 |
size_t i; |
| 61 |
for (i = 0; i < exit_procs_size; i++) { |
| 62 |
if (exit_procs[i].proc == proc) { |
| 63 |
fprintf(stderr, "unregistering %p!\n", (void *)proc); |
| 64 |
exit_procs[i].proc = NULL; |
| 65 |
exit_procs[i].arg = NULL; |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
#endif |
| 70 |
|
| 71 |
void call_exit_handlers(void) |
| 72 |
{ |
| 73 |
struct exit_list *ep; |
| 74 |
|
| 75 |
for (ep = exit_procs; ep != NULL; ep = ep->next) { |
| 76 |
ep->proc(ep->arg); |
| 77 |
/* fprintf(stderr, "calling exit proc %lu\n", (unsigned long)i); */ |
| 78 |
} |
| 79 |
|
| 80 |
/* free them */ |
| 81 |
while (exit_procs != NULL) { |
| 82 |
ep = exit_procs; |
| 83 |
exit_procs = exit_procs->next; |
| 84 |
free(ep); |
| 85 |
} |
| 86 |
} |