From a9dd76bbcacb3f684f631ef16b8ed4cb02c7ef3c Mon Sep 17 00:00:00 2001 From: Daniel Beer Date: Fri, 30 Apr 2010 22:17:43 +1200 Subject: [PATCH] Removed static data from sym commands. --- stab.c | 4 ++-- stab.h | 5 +++-- sym.c | 59 ++++++++++++++++++++++++++++++++-------------------------- 3 files changed, 38 insertions(+), 30 deletions(-) diff --git a/stab.c b/stab.c index 609997b..f005844 100644 --- a/stab.c +++ b/stab.c @@ -188,7 +188,7 @@ int stab_del(const char *name) return 0; } -int stab_enum(stab_callback_t cb) +int stab_enum(stab_callback_t cb, void *user_data) { int ret; struct addr_key akey; @@ -196,7 +196,7 @@ int stab_enum(stab_callback_t cb) ret = btree_select(addr_table, NULL, BTREE_FIRST, &akey, NULL); while (!ret) { - if (cb(akey.name, akey.addr) < 0) + if (cb(user_data, akey.name, akey.addr) < 0) return -1; ret = btree_select(addr_table, NULL, BTREE_NEXT, &akey, NULL); diff --git a/stab.h b/stab.h index cf3bb8f..eb996d3 100644 --- a/stab.h +++ b/stab.h @@ -52,9 +52,10 @@ int stab_get(const char *name, int *value); int stab_del(const char *name); /* Enumerate all symbols in the table */ -typedef int (*stab_callback_t)(const char *name, u_int16_t value); +typedef int (*stab_callback_t)(void *user_data, + const char *name, u_int16_t value); -int stab_enum(stab_callback_t cb); +int stab_enum(stab_callback_t cb, void *user_data); /* Parse an address expression, storing the result in the integer * pointed to. Returns 0 if parsed successfully, -1 if not. diff --git a/sym.c b/sym.c index 925f408..d6dab39 100644 --- a/sym.c +++ b/sym.c @@ -85,10 +85,10 @@ static int cmd_sym_load_add(cproc_t cp, int clear, char **arg) return result; } -static FILE *savemap_out; - -static int savemap_cb(const char *name, u_int16_t value) +static int savemap_cb(void *user_data, const char *name, u_int16_t value) { + FILE *savemap_out = (FILE *)user_data; + if (fprintf(savemap_out, "%04x t %s\n", value, name) < 0) { perror("sym: can't write to file"); return -1; @@ -99,6 +99,7 @@ static int savemap_cb(const char *name, u_int16_t value) static int cmd_sym_savemap(cproc_t cp, char **arg) { + FILE *savemap_out; char *fname = get_arg(arg); if (!fname) { @@ -113,7 +114,7 @@ static int cmd_sym_savemap(cproc_t cp, char **arg) return -1; } - if (stab_enum(savemap_cb) < 0) { + if (stab_enum(savemap_cb, savemap_out) < 0) { fclose(savemap_out); return -1; } @@ -127,17 +128,17 @@ static int cmd_sym_savemap(cproc_t cp, char **arg) return 0; } -static int print_sym(const char *name, u_int16_t value) +static int print_sym(void *user_data, const char *name, u_int16_t value) { printf("0x%04x: %s\n", value, name); return 0; } -static regex_t find_preg; - -static int find_sym(const char *name, u_int16_t value) +static int find_sym(void *user_data, const char *name, u_int16_t value) { - if (!regexec(&find_preg, name, 0, NULL, 0)) + regex_t *find_preg = (regex_t *)user_data; + + if (!regexec(find_preg, name, 0, NULL, 0)) printf("0x%04x: %s\n", value, name); return 0; @@ -145,10 +146,11 @@ static int find_sym(const char *name, u_int16_t value) static int cmd_sym_find(cproc_t cp, char **arg) { + regex_t find_preg; char *expr = get_arg(arg); if (!expr) { - stab_enum(print_sym); + stab_enum(print_sym, NULL); return 0; } @@ -157,7 +159,7 @@ static int cmd_sym_find(cproc_t cp, char **arg) return -1; } - stab_enum(find_sym); + stab_enum(find_sym, &find_preg); regfree(&find_preg); return 0; } @@ -167,16 +169,19 @@ struct rename_record { int start, end; }; -struct vector renames_vec; +struct rename_data { + struct vector list; + regex_t preg; +}; -static int renames_do(const char *replace) +static int renames_do(struct rename_data *rename, const char *replace) { int i; int count = 0; - for (i = 0; i < renames_vec.size; i++) { + for (i = 0; i < rename->list.size; i++) { struct rename_record *r = - VECTOR_PTR(renames_vec, i, struct rename_record); + VECTOR_PTR(rename->list, i, struct rename_record); char new_name[128]; int len = r->start; int value; @@ -211,11 +216,12 @@ static int renames_do(const char *replace) return 0; } -static int find_renames(const char *name, u_int16_t value) +static int find_renames(void *user_data, const char *name, u_int16_t value) { + struct rename_data *rename = (struct rename_data *)user_data; regmatch_t pmatch; - if (!regexec(&find_preg, name, 1, &pmatch, 0) && + if (!regexec(&rename->preg, name, 1, &pmatch, 0) && pmatch.rm_so >= 0 && pmatch.rm_eo > pmatch.rm_so) { struct rename_record r; @@ -224,7 +230,7 @@ static int find_renames(const char *name, u_int16_t value) r.start = pmatch.rm_so; r.end = pmatch.rm_eo; - return vector_push(&renames_vec, &r, 1); + return vector_push(&rename->list, &r, 1); } return 0; @@ -235,29 +241,30 @@ static int cmd_sym_rename(cproc_t cp, char **arg) const char *expr = get_arg(arg); const char *replace = get_arg(arg); int ret; + struct rename_data rename; if (!(expr && replace)) { fprintf(stderr, "sym: expected pattern and replacement\n"); return -1; } - if (regcomp(&find_preg, expr, REG_EXTENDED)) { + if (regcomp(&rename.preg, expr, REG_EXTENDED)) { fprintf(stderr, "sym: failed to compile: %s\n", expr); return -1; } - vector_init(&renames_vec, sizeof(struct rename_record)); + vector_init(&rename.list, sizeof(struct rename_record)); - if (stab_enum(find_renames) < 0) { + if (stab_enum(find_renames, &rename) < 0) { fprintf(stderr, "sym: rename failed\n"); - regfree(&find_preg); - vector_destroy(&renames_vec); + regfree(&rename.preg); + vector_destroy(&rename.list); return -1; } - regfree(&find_preg); - ret = renames_do(replace); - vector_destroy(&renames_vec); + regfree(&rename.preg); + ret = renames_do(&rename, replace); + vector_destroy(&rename.list); if (ret > 0) cproc_modify(cp, CPROC_MODIFY_SYMS);