From 1efc35a59ec7c8617378e5765c61e7c831d8c640 Mon Sep 17 00:00:00 2001 From: "Gu://em_" Date: Fri, 31 Oct 2025 22:31:08 +0100 Subject: [PATCH] JUSTOCASOUUUU --- minimake/Makefile | 8 +++--- minimake/src/files/files.h | 7 ++--- minimake/src/hash_maps/hash_maps.h | 2 +- minimake/src/lines/lines.c | 42 ++++++++++++++++++++++++++++++ minimake/src/lines/lines.h | 12 ++++----- minimake/src/lists/.gitignore | 1 - minimake/src/lists/lists.h | 16 ++++++------ minimake/src/minimake.c | 5 ++-- minimake/src/minimake.h | 6 ++--- 9 files changed, 70 insertions(+), 29 deletions(-) delete mode 100644 minimake/src/lists/.gitignore diff --git a/minimake/Makefile b/minimake/Makefile index db29234..cb0ae0a 100644 --- a/minimake/Makefile +++ b/minimake/Makefile @@ -21,10 +21,10 @@ $(TARGET): $(OBJS) $(CC) -o $@ $^ $(LDFLAGS) $(LDLIBS) @echo $(OBJS) -debug: CFLAGS += $(DBG_CFLAGS) -debug: LDFLAGS += $(DBG_LDFLAGS) -debug: $(OBJS) - $(CC) -o $(TARGET) $^ $(LDFLAGS) $(LDLIBS) +# debug: CFLAGS += $(DBG_CFLAGS) +# debug: LDFLAGS += $(DBG_LDFLAGS) +# debug: $(OBJS) +# $(CC) -o $(TARGET) $^ $(LDFLAGS) $(LDLIBS) check: dash ./tests/run.sh diff --git a/minimake/src/files/files.h b/minimake/src/files/files.h index da7c555..d4893c3 100644 --- a/minimake/src/files/files.h +++ b/minimake/src/files/files.h @@ -3,11 +3,12 @@ #define _XOPEN_SOURCE 500 -#include "../lists/lists.h" #include -char* get_makefile(struct list *files); -ssize_t get_file_lastmodiftime(char* filename); +#include "../lists/lists.h" + +char *get_makefile(struct list *files); +ssize_t get_file_lastmodiftime(char *filename); int run_command(char *command); int file_exists(char *filename); diff --git a/minimake/src/hash_maps/hash_maps.h b/minimake/src/hash_maps/hash_maps.h index 97a84da..634c993 100644 --- a/minimake/src/hash_maps/hash_maps.h +++ b/minimake/src/hash_maps/hash_maps.h @@ -24,7 +24,7 @@ struct hash_map size_t hash(const char *str); struct hash_map *hashmap_init(size_t size); bool hashmap_insert(struct hash_map *hash_map, char *key, void *value, - bool *updated); + bool *updated); void hashmap_free(struct hash_map *hash_map); void hashmap_dump(struct hash_map *hash_map); void *hashmap_get(const struct hash_map *hash_map, char *key); diff --git a/minimake/src/lines/lines.c b/minimake/src/lines/lines.c index fcd33e1..238b576 100644 --- a/minimake/src/lines/lines.c +++ b/minimake/src/lines/lines.c @@ -219,3 +219,45 @@ size_t read_declaration(struct line *line, size_t index, char **word) *word = res; return i - index; } + +// Takes a string, expands its variables and returns the resulting buffer +// WARNING allocates the result on the heap, free result after use +char *expand_str(char *str) +{ + size_t str_i = 0; + size_t buf_i = 0; + size_t buf_size = BUFFER_SIZE; + char *buf = malloc(BUFFER_SIZE * sizeof(char)); + while (str[str_i] != '\0') + { + // Reallocate more space if necessary + resize_buf(&buf, buf_size, buf_i); + + // Expand variable + if (buf[buf_i] == '$') + { + // Temporary variables + char *expanded_var; + char *tmp_res_buf; + struct line false_line = { + .buffer = buf, .file_stream = NULL, .length = 0, .number = 0 + }; + str_i += expand_variable(&false_line, str_i + 1, &expanded_var); + buf_size = append_str(buf, expanded_var, buf_i, &tmp_res_buf); + // Replace buf with buf + expanded_variable + buf_i = strlen(tmp_res_buf); + free(buf); + // free(expanded_var); + buf = tmp_res_buf; + continue; + } + + buf[buf_i] = str[str_i]; + buf_i++; + str_i++; + } + + buf[buf_i] = '\0'; + + return buf; +} diff --git a/minimake/src/lines/lines.h b/minimake/src/lines/lines.h index 081d305..5782327 100644 --- a/minimake/src/lines/lines.h +++ b/minimake/src/lines/lines.h @@ -4,25 +4,25 @@ #define STRING_BUFFER_SIZE 32 #include -#include #include +#include // Holds line information // Exists only because of EPITA's annoying 4 parameters limit // WARNING buffer must be freed after use struct line { - char* buffer; // Line content - ssize_t length; // Line length - size_t number; // Line number in file - FILE* file_stream; // Full file stream + char *buffer; // Line content + ssize_t length; // Line length + size_t number; // Line number in file + FILE *file_stream; // Full file stream }; int ischar(char c); int isblankline(struct line *l); int skipblanks(char *buf, size_t buf_len); size_t readword(char *buf, size_t buf_len, char **word); -size_t resize_buf(char** buf, size_t buf_size, size_t index); +size_t resize_buf(char **buf, size_t buf_size, size_t index); char *insert_str(char *base, char *str, size_t index); size_t read_declaration(struct line *line, size_t index, char **word); size_t append_str(char *base, char *str, size_t index, char **res); diff --git a/minimake/src/lists/.gitignore b/minimake/src/lists/.gitignore deleted file mode 100644 index 3309b20..0000000 --- a/minimake/src/lists/.gitignore +++ /dev/null @@ -1 +0,0 @@ -list.c diff --git a/minimake/src/lists/lists.h b/minimake/src/lists/lists.h index 8c4114f..116bd1f 100644 --- a/minimake/src/lists/lists.h +++ b/minimake/src/lists/lists.h @@ -1,11 +1,11 @@ -#ifndef LIST_H -#define LIST_H +#ifndef LISTS_H +#define LISTS_H #include struct list { - void* data; + void *data; struct list *next; }; @@ -13,7 +13,7 @@ struct list ** Insert a node containing `value` at the beginning of the list. ** Return `NULL` if an error occured. */ -struct list *list_prepend(struct list *list, void* value); +struct list *list_prepend(struct list *list, void *value); /* ** Return the lenght of the list. @@ -44,7 +44,7 @@ void list_deep_destroy(struct list *l); ** Return `NULL` if an error occured. */ // START PROTO list_append -struct list *list_append(struct list *list, void* value); +struct list *list_append(struct list *list, void *value); // END PROTO list_append /* @@ -54,7 +54,7 @@ struct list *list_append(struct list *list, void* value); ** Return `NULL` if an error occured. */ // START PROTO list_insert -struct list *list_insert(struct list *list, void* value, size_t index); +struct list *list_insert(struct list *list, void *value, size_t index); // END PROTO list_insert /* @@ -70,7 +70,7 @@ struct list *list_remove(struct list *list, size_t index); ** Return `-1` if nothing is found. */ // START PROTO list_find -int list_find(struct list *list, void* value); +int list_find(struct list *list, void *value); // END PROTO list_find /* @@ -107,4 +107,4 @@ int list_find(struct list *list, void* value); // struct list *list_split(struct list *list, size_t index); // END PROTO list_split -#endif /* ! LIST_H */ +#endif /* ! LISTS_H */ diff --git a/minimake/src/minimake.c b/minimake/src/minimake.c index 675702c..c28aed3 100644 --- a/minimake/src/minimake.c +++ b/minimake/src/minimake.c @@ -466,12 +466,11 @@ static int run_rule(struct rule *rule) struct list *commands = rule->recipe; struct list *dependencies = rule->dependencies; - // Expand dependencies variables - // TODO - // Build dependencies while (dependencies != NULL) { + // Expand variables + // Check file existence if (!file_exists(dependencies->data)) { diff --git a/minimake/src/minimake.h b/minimake/src/minimake.h index f6ed073..7bb5f39 100644 --- a/minimake/src/minimake.h +++ b/minimake/src/minimake.h @@ -18,8 +18,8 @@ #include #include -#include "lists/lists.h" #include "lines/lines.h" +#include "lists/lists.h" // Holds variable information // WARNING its values must be freed after use @@ -33,12 +33,12 @@ struct variable // WARNING its values must be freed after use struct rule { - char* name; + char *name; struct list *dependencies; struct list *recipe; }; -int make(char *path, struct list* rules, char *argv0, int flags); +int make(char *path, struct list *rules, char *argv0, int flags); void make_parse(char *path); int make_run(struct list *rules); void drop(int status, char *format, ...);