diff options
-rw-r--r-- | src/cli.c | 14 | ||||
-rw-r--r-- | src/disp.c | 81 | ||||
-rw-r--r-- | src/disp.h | 3 | ||||
-rw-r--r-- | src/ray.c | 14 | ||||
-rw-r--r-- | src/tui.c | 16 |
5 files changed, 71 insertions, 57 deletions
@@ -23,16 +23,16 @@ #include "_readline.h" #include "disp.h" -void init(void) {} +void cli_init(void) {} void -display_calc(int x, int y) +cli_display_calc(int x, int y) { printf("%d + %d = ", x, y); } int -read_input(void) +cli_read_input(void) { char *txt = readline(""); @@ -49,14 +49,14 @@ read_input(void) } void -display_res(bool ok, int ms) +cli_display_res(bool ok, int ms) { printf("%s\t%dms\n", ok ? "RIGHT" : "WRONG", ms); } -void destroy(void) {} +void cli_destroy(void) {} #ifndef NDEBUG -void *pre_reload(void) {return NULL;} -void post_reload(void *) {} +void *cli_pre_reload(void) {return NULL;} +void cli_post_reload(void *) {} #endif @@ -25,17 +25,30 @@ #include "disp.h" -#define dlsym_and_check(dest, name) \ - dest->name = dlsym(dest->dl_handle, #name); \ - if ((!dest->name) || (dest->name == dest->dl_handle)) { \ - char *msg = dlerror(); \ - fprintf(stderr, "calculer: %s\n", msg); \ - dlclose(dest->dl_handle); \ - free(dest); \ - return -1; \ +static void * +load_generic_symbol(struct disp *disp, char *base_name) +{ + char full_name[64] = {0}; + void *ptr; + + strcpy(full_name, disp->name); + strcat(full_name, "_"); + strcat(full_name, base_name); + + ptr = dlsym(disp->dl_handle, full_name); + + if (!ptr) + { + fprintf(stderr, "calculer: %s\n", dlerror()); + destroy_disp(disp); + exit(1); } -static int + return ptr; +} + + +static void load_symbols(struct disp *disp) { void *handle = dlopen(disp->so_path, RTLD_LAZY); @@ -43,24 +56,21 @@ load_symbols(struct disp *disp) if (!handle) { fprintf(stderr, "calculer: %s\n", dlerror()); + destroy_disp(disp); exit(1); } -#ifndef NDEBUG - printf("INFO: successfully loaded symbols from \"%s\"\n", disp->so_path); -#endif + disp->dl_handle = handle; + disp->init = load_generic_symbol(disp, "init"); + disp->display_calc = load_generic_symbol(disp, "display_calc"); + disp->read_input = load_generic_symbol(disp, "read_input"); + disp->display_res = load_generic_symbol(disp, "display_res"); + disp->destroy = load_generic_symbol(disp, "destroy"); + disp->pre_reload = load_generic_symbol(disp, "pre_reload"); + disp->post_reload = load_generic_symbol(disp, "post_reload"); - disp->dl_handle = handle; - dlsym_and_check(disp, init); - dlsym_and_check(disp, display_calc); - dlsym_and_check(disp, read_input); - dlsym_and_check(disp, display_res); - dlsym_and_check(disp, destroy); -#ifndef NDEBUG - dlsym_and_check(disp, pre_reload); - dlsym_and_check(disp, post_reload); -#endif - return 0; + printf("INFO: successfully loaded symbols from \"%s\"\n", + disp->so_path); } struct disp * @@ -74,8 +84,11 @@ get_disp(char *disp_name) strcat(so_path, ".so.0.0.0"); struct disp *disp = malloc(sizeof(*disp)); disp->so_path = strdup(so_path); + disp->name = disp_name; + + load_symbols(disp); - return load_symbols(disp) == 0 ? disp : NULL; + return disp; #else # error "not available yet" #endif @@ -85,8 +98,12 @@ get_disp(char *disp_name) void destroy_disp(struct disp *disp) { - disp->destroy(); - dlclose(disp->dl_handle); + if (disp->destroy) + disp->destroy(); + + if (disp->dl_handle) + dlclose(disp->dl_handle); + free(disp->so_path); free(disp); } @@ -95,22 +112,18 @@ destroy_disp(struct disp *disp) void disp_reload(struct disp *disp) { - int status; printf("INFO: saving state\n"); void *state = disp->pre_reload(); + printf("INFO: closing shared library\n"); dlclose(disp->dl_handle); - printf("INFO: reloading symbols\n"); - status = load_symbols(disp); - if (status == -1) - { - printf("calculer: %s\n", dlerror()); - exit(1); - } + printf("INFO: reloading symbols\n"); + load_symbols(disp); printf("INFO: loading state\n"); disp->post_reload(state); + printf("INFO: reloading done\n"); } #endif @@ -33,13 +33,14 @@ #define DISP_ERR (INT_MAX - 2) struct disp { - void *dl_handle; void (*init)(void); void (*display_calc)(int, int); int (*read_input)(void); void (*display_res)(bool, int); void (*destroy)(void); #ifndef NDEBUG + char *name; + void *dl_handle; char *so_path; void *(*pre_reload)(void); void (*post_reload)(void *); @@ -36,7 +36,7 @@ static struct calc_data calcs[CALC_COUNT] = {0}; static int current_idx = 0; void -init(void) +ray_init(void) { #ifndef NDEBUG SetTraceLogLevel(LOG_DEBUG); @@ -195,7 +195,7 @@ redraw(void) } void -display_calc(int x, int y) +ray_display_calc(int x, int y) { current_idx = (current_idx + 1)%CALC_COUNT; calcs[current_idx].x = x; @@ -226,7 +226,7 @@ check_digits(void) } int -read_input(void) +ray_read_input(void) { TraceLog(LOG_INFO, "waiting for user input"); calcs[current_idx].input = 0; @@ -258,7 +258,7 @@ read_input(void) } void -display_res(bool right, int ms) +ray_display_res(bool right, int ms) { calcs[current_idx].right = right; calcs[current_idx].ms = ms; @@ -266,14 +266,14 @@ display_res(bool right, int ms) } void -destroy(void) +ray_destroy(void) { CloseWindow(); } #ifndef NDEBUG void * -pre_reload(void) +ray_pre_reload(void) { char *data = malloc(sizeof(calcs) + sizeof(current_idx)); memcpy(data, &calcs, sizeof(calcs)); @@ -283,7 +283,7 @@ pre_reload(void) } void -post_reload(void *state) +ray_post_reload(void *state) { char *data = state; memcpy(&calcs, data, sizeof(calcs)); @@ -65,7 +65,7 @@ redraw(void) } void -init(void) +tui_init(void) { win = initscr(); noecho(); @@ -74,7 +74,7 @@ init(void) } void -display_calc(int x, int y) +tui_display_calc(int x, int y) { current_idx = next_calc(current_idx); calcs[current_idx].x = x; @@ -92,7 +92,7 @@ display_calc(int x, int y) break; int -read_input(void) +tui_read_input(void) { int c = getch(); while (c != ERR) @@ -136,7 +136,7 @@ read_input(void) } void -display_res(bool right, int ms) +tui_display_res(bool right, int ms) { calcs[current_idx].right = right; calcs[current_idx].ms = ms; @@ -144,14 +144,14 @@ display_res(bool right, int ms) } void -destroy(void) +tui_destroy(void) { endwin(); } #ifndef NDEBUG void * -pre_reload(void) +tui_pre_reload(void) { char *data = malloc(sizeof(calcs) + sizeof(current_idx)); memcpy(data, &calcs, sizeof(calcs)); @@ -161,13 +161,13 @@ pre_reload(void) } void -post_reload(void *state) +tui_post_reload(void *state) { char *data = state; memcpy(&calcs, data, sizeof(calcs)); memcpy(¤t_idx, data + sizeof(calcs), sizeof(current_idx)); free(data); - init(); + tui_init(); redraw(); } #endif |