diff --git a/minimake/src/minimake.c b/minimake/src/minimake.c index 6e23a75..3e37319 100644 --- a/minimake/src/minimake.c +++ b/minimake/src/minimake.c @@ -7,6 +7,7 @@ #include #include #include +#include #include "hash_maps/hash_maps.h" #include "lines/lines.h" @@ -32,8 +33,32 @@ static void hashmap_deep_free(struct hash_map *hash_map) while (entry != NULL) { struct pair_list *next = entry->next; - free(entry->key); - free(entry->value); + // Ok c moche mais eh, ça fonctionne + if (hash_map == rules) + { + struct rule *r = entry->value; + free(r->name); + list_destroy(r->dependencies); + list_destroy(r->recipe); + free(r); + free(entry->key); + } + if (hash_map == variables) + { + struct variable *v = entry->value; + free(v->name); + free(v->value); + + free(entry->key); + free(entry->value); + } + else + { + printf("DEBUG: attempting to free a generic hashmap"); + free(entry->key); + free(entry->value); + } + free(entry); entry = next; } @@ -54,6 +79,21 @@ static void free_all() // hashmap_dump(rules); // } +static void exit_on_error(int status, char *message) +{ + char *full_msg; + int err = sprintf(full_msg, "%s: %s\n", program_name, message); + free(message); + if (err == -1) + exit(status); + + write(STDERR_FILENO, full_msg, strlen(full_msg)); + + free(full_msg); + free_all(); + exit(status); +} + // ==== Parsing ==== // Registers a new rule in the hashmap @@ -70,8 +110,11 @@ static void register_rule(char *name, struct list *dependencies, int err = hashmap_insert(variables, name, rule, NULL); if (!err) { - errx(1, "Internal Error: Couln't add entry for '%s' in the hashmap", - name); + char *err_msg; + sprintf(err_msg, + "Internal Error: Couln't add entry for '%s' in the hashmap", + name); + exit_on_error(GENERIC_ERR, err_msg); } } @@ -83,8 +126,11 @@ static void register_variable(char *name, char *value) int err = hashmap_insert(variables, name, value, NULL); if (!err) { - errx(2, "Internal Error: Couln't add entry for '%s' in the hashmap", - name); + char *err_msg; + sprintf(err_msg, + "Internal Error: Couln't add entry for '%s' in the hashmap", + name); + exit_on_error(GENERIC_ERR, err_msg); } } @@ -115,10 +161,12 @@ static struct list *read_deps(struct line *l, size_t offset) if (!ischar(buf[i]) && !isblank(buf[i]) && buf[i] != '\0' && buf[i] != '\n') { - printf("DEBUG: line: %s", l->buffer); - errx(2, - "Unexpected character '%c' after rule declaration at %lu:%lu", - buf[i], l->number, i); + char *err_msg; + sprintf( + err_msg, + "Unexpected character '%c' after rule declaration at %lu:%lu", + buf[i], l->number, i); + exit_on_error(GENERIC_ERR, err_msg); } } @@ -153,8 +201,13 @@ static struct list *read_recipe(struct line *l) { char *command = strdup(buf + 1); if (command == NULL) - errx(1, "Internal error: couldn't duplicate string (%lu:1)", - l->number); + { + char *err_msg; + sprintf(err_msg, + "Internal error: couldn't duplicate string (%lu:1)", + l->number); + exit_on_error(GENERIC_ERR, err_msg); + } res = list_append(res, command); } @@ -179,7 +232,11 @@ static char *read_value(char *buf, size_t buf_size) str_buf_size += STRING_BUFFER_SIZE; str_buf = realloc(str_buf, str_buf_size); if (str_buf == NULL) - errx(2, "Could not realloc"); + { + char *err_msg; + sprintf(err_msg, "Could not realloc"); + exit_on_error(GENERIC_ERR, err_msg); + } } str_buf[i] = buf[i]; @@ -217,6 +274,7 @@ static void parse_line(struct line *current_line) struct list *dependencies; struct list *recipe; char *value; + char *err_msg; // Definition type switch (buf[i]) @@ -246,15 +304,21 @@ static void parse_line(struct line *current_line) case '\0': case '#': if (name != NULL) - errx(2, - "Unexpected character '%c' after declaration '%s' at line %lu", - buf[i], name, current_line->number); + { + sprintf( + err_msg, + "Unexpected character '%c' after declaration '%s' at line %lu", + buf[i], name, current_line->number); + exit_on_error(GENERIC_ERR, err_msg); + } else break; default: - errx(2, "Unexpected character '%c' after declaration '%s' at line %lu", - buf[i], name, current_line->number); + sprintf(err_msg, + "Unexpected character '%c' after declaration '%s' at line %lu", + buf[i], name, current_line->number); + exit_on_error(GENERIC_ERR, err_msg); } // free(name); @@ -277,7 +341,11 @@ void make_parse(char *path) size_t buf_size = BUFFER_SIZE; char *buf = malloc(sizeof(char) * buf_size); if (buf == NULL) - errx(2, "Could not allocate more memory"); + { + char *err_msg; + sprintf(err_msg, "Could not allocate more memory"); + exit_on_error(GENERIC_ERR, err_msg); + } // Parse line by line ssize_t nread; @@ -287,7 +355,11 @@ void make_parse(char *path) while ((nread = getline(&buf, &buf_size, stream)) != -1) { if (nread == -1) - errx(1, "Could not get line %lu", current_line.number); + { + char *err_msg; + sprintf(err_msg, "Could not get line %lu", current_line.number); + exit_on_error(GENERIC_ERR, err_msg); + } current_line.buffer = buf; current_line.length = nread;