From 030704970dcfc29c7c82e2aefb4f2646634cc24c Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Mon, 5 Jun 2023 07:58:06 +0100 Subject: Copy the filename to track location --- tr8as.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) (limited to 'tr8as.c') diff --git a/tr8as.c b/tr8as.c index 9cdb11a..e150aeb 100644 --- a/tr8as.c +++ b/tr8as.c @@ -123,6 +123,15 @@ static uint8_t palette[16][3] = { 0xff, 0xff, 0xff }, }; +static const char *mstrdup(const char *src) +{ + char *p = malloc(strlen(src) + 1); + if (p) + strcpy(p, src); + + return p; +} + static uint8_t error_l(const char *msg, Location *loc, const char *reason) { fprintf(stderr, "%s (%s:%d): %s\n", msg, loc->filename, loc->line, reason); @@ -237,7 +246,7 @@ static uint8_t new_define(As *as, char *id, char *value) as->defs[as->dcnt].id = str_hash(id); strcpy(as->defs[as->dcnt].value, value); - as->defs[as->dcnt].loc.filename = as->loc.filename; + as->defs[as->dcnt].loc.filename = mstrdup(as->loc.filename); as->defs[as->dcnt++].loc.line = as->loc.line; if (as->dcnt == MAX_DEFS) @@ -385,7 +394,7 @@ static uint8_t new_label(As *as, char *word) as->labels[as->lcnt].id = str_hash(word); - as->labels[as->lcnt].loc.filename = as->loc.filename; + as->labels[as->lcnt].loc.filename = mstrdup(as->loc.filename); as->labels[as->lcnt].loc.line = as->loc.line; as->labels[as->lcnt++].addr = as->addr; @@ -421,7 +430,7 @@ static uint8_t new_ref(As *as, char *word, uint16_t mask, uint16_t addr) strcpy(as->refs[as->rcnt].label, pt); as->refs[as->rcnt].mod = mod; as->refs[as->rcnt].mask = mask; - as->refs[as->rcnt].loc.filename = as->loc.filename; + as->refs[as->rcnt].loc.filename = mstrdup(as->loc.filename); as->refs[as->rcnt].loc.line = as->loc.line; as->refs[as->rcnt++].addr = addr; @@ -1641,6 +1650,8 @@ static uint8_t parse(As *as, char *c) static uint8_t asm(As *as, const char *filename, FILE *in) { + uint16_t i; + uint8_t rc; char line[MAX_LINE]; memset(as, 0, sizeof(As)); @@ -1653,7 +1664,16 @@ static uint8_t asm(As *as, const char *filename, FILE *in) return 0; } - return resolve(as); + rc = resolve(as); + + for (i = 0; i < as->rcnt; i++) + free((void *)as->refs[i].loc.filename); + for (i = 0; i < as->dcnt; i++) + free((void *)as->defs[i].loc.filename); + for (i = 0; i < as->lcnt; i++) + free((void *)as->labels[i].loc.filename); + + return rc; } #ifdef DO_MAIN -- cgit v1.2.3