Compare commits
No commits in common. "018bae577a9ba0abafe5d8176e55ed4fc1f523a9" and "8e227e429893799407e44902cefc459275595771" have entirely different histories.
018bae577a
...
8e227e4298
21 changed files with 157 additions and 911 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -7,4 +7,4 @@
|
||||||
*.class
|
*.class
|
||||||
*.log
|
*.log
|
||||||
*.core
|
*.core
|
||||||
minimake/minimake
|
minimake
|
||||||
|
|
|
||||||
1
AUTHORS
1
AUTHORS
|
|
@ -3,4 +3,3 @@ guillem.george
|
||||||
Special thanks to
|
Special thanks to
|
||||||
|
|
||||||
- Timothée Battefort for the parser
|
- Timothée Battefort for the parser
|
||||||
- Amadéo Heaulme for the error handling
|
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ DBG_LDFLAGS= -fsanitize=address
|
||||||
|
|
||||||
|
|
||||||
SRC_DIR = src
|
SRC_DIR = src
|
||||||
LIB_SRCS = lines/lines.c hash_maps/hash_maps.c lists/lists.c files/files.c
|
LIB_SRCS = lines/lines.c hash_maps/hash_maps.c lists/lists.c
|
||||||
MAIN_SRCS = main.c minimake.c
|
MAIN_SRCS = main.c minimake.c
|
||||||
|
|
||||||
# SRCS = $(patsubst %,$(SRC_DIR)/%, $(MAIN_SRCS))
|
# SRCS = $(patsubst %,$(SRC_DIR)/%, $(MAIN_SRCS))
|
||||||
|
|
|
||||||
|
|
@ -1,94 +0,0 @@
|
||||||
#include "files.h"
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <sys/wait.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
// #include "../lines/lines.h"
|
|
||||||
|
|
||||||
char *get_makefile(struct list *files)
|
|
||||||
{
|
|
||||||
struct list *file = files;
|
|
||||||
int found = 0;
|
|
||||||
|
|
||||||
// Test file presence
|
|
||||||
while (file != NULL && !found)
|
|
||||||
{
|
|
||||||
FILE *test = fopen(file->data, "r");
|
|
||||||
if (test != NULL)
|
|
||||||
{
|
|
||||||
fclose(test);
|
|
||||||
found = 1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
file = file->next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
char *res = NULL;
|
|
||||||
if (found)
|
|
||||||
res = file->data;
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
ssize_t get_file_lastmodiftime(char *filename)
|
|
||||||
{
|
|
||||||
struct stat stat;
|
|
||||||
if (lstat(filename, &stat) == -1)
|
|
||||||
return -1;
|
|
||||||
return stat.st_mtime;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calls a shell that will run the given command
|
|
||||||
// It will first print the command to run unless the it starts with '@'
|
|
||||||
// Returns the return code o the command or -1 if a syscall fails
|
|
||||||
int run_command(char *command)
|
|
||||||
{
|
|
||||||
if (command[0] == '@')
|
|
||||||
command++;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
puts(command);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Just in case
|
|
||||||
fflush(stdout);
|
|
||||||
fflush(stderr);
|
|
||||||
|
|
||||||
int id = fork();
|
|
||||||
if (id < 0)
|
|
||||||
{
|
|
||||||
perror("fork");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (id == 0)
|
|
||||||
{
|
|
||||||
int res = execl("/bin/sh", "(make)", "-c", command, NULL);
|
|
||||||
exit(res);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
int res = 0;
|
|
||||||
wait(&res);
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns 1 if file exists (and is readable), 0 otherwise
|
|
||||||
int file_exists(char *filename)
|
|
||||||
{
|
|
||||||
if (filename == NULL)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
FILE *test = fopen(filename, "r");
|
|
||||||
if (test == NULL)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
fclose(test);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
@ -1,15 +0,0 @@
|
||||||
#ifndef FILES_H
|
|
||||||
#define FILES_H
|
|
||||||
|
|
||||||
#define _XOPEN_SOURCE 500
|
|
||||||
|
|
||||||
#include <sys/types.h>
|
|
||||||
|
|
||||||
#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);
|
|
||||||
|
|
||||||
#endif // FILES_H
|
|
||||||
|
|
@ -4,11 +4,8 @@
|
||||||
#include <err.h>
|
#include <err.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "../minimake.h"
|
|
||||||
|
|
||||||
// Helps to match a string (excludes blanks and special characters)
|
// Helps to match a string (excludes blanks and special characters)
|
||||||
int ischar(char c)
|
int ischar(char c)
|
||||||
{
|
{
|
||||||
|
|
@ -53,7 +50,13 @@ size_t readword(char *buf, size_t buf_len, char **word)
|
||||||
while (i < buf_len && ischar(buf[i]))
|
while (i < buf_len && ischar(buf[i]))
|
||||||
{
|
{
|
||||||
// Reallocate more space if necessary
|
// Reallocate more space if necessary
|
||||||
resize_buf(&res, res_size, i);
|
if (i >= res_size - 1)
|
||||||
|
{
|
||||||
|
res_size += STRING_BUFFER_SIZE;
|
||||||
|
res = realloc(res, res_size);
|
||||||
|
if (res == NULL)
|
||||||
|
errx(2, "Could not realloc");
|
||||||
|
}
|
||||||
|
|
||||||
res[i] = buf[i];
|
res[i] = buf[i];
|
||||||
i++;
|
i++;
|
||||||
|
|
@ -70,194 +73,3 @@ size_t readword(char *buf, size_t buf_len, char **word)
|
||||||
*word = res;
|
*word = res;
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resizes buf if index is greater or equal than buf_size-1
|
|
||||||
// Returns new buffer size
|
|
||||||
size_t resize_buf(char **buf, size_t buf_size, size_t index)
|
|
||||||
{
|
|
||||||
if (index >= buf_size - 1)
|
|
||||||
{
|
|
||||||
buf_size += BUFFER_SIZE;
|
|
||||||
*buf = realloc(*buf, buf_size);
|
|
||||||
if (buf == NULL)
|
|
||||||
drop(2, "Could not realloc");
|
|
||||||
}
|
|
||||||
return buf_size;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Inserts str into base at index and returns the resulting string
|
|
||||||
// WARNING allocates the result on the heap, do not forget to free it
|
|
||||||
char *insert_str(char *base, char *str, size_t index)
|
|
||||||
{
|
|
||||||
size_t res_size = BUFFER_SIZE;
|
|
||||||
char *res = malloc(res_size * sizeof(char));
|
|
||||||
if (res == NULL)
|
|
||||||
drop(GENERIC_ERR, "Could not allocate more memory");
|
|
||||||
|
|
||||||
// Copy until index
|
|
||||||
size_t i = 0;
|
|
||||||
while (i < index)
|
|
||||||
{
|
|
||||||
// Reallocate more space if necessary
|
|
||||||
res_size = resize_buf(&res, res_size, i);
|
|
||||||
|
|
||||||
res[i] = base[i];
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Copy str
|
|
||||||
size_t str_i = 0;
|
|
||||||
while (str[str_i] != '\0')
|
|
||||||
{
|
|
||||||
// Reallocate more space if necessary
|
|
||||||
res_size = resize_buf(&res, res_size, i);
|
|
||||||
|
|
||||||
res[i] = str[str_i];
|
|
||||||
i++;
|
|
||||||
str_i++;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Copy rest
|
|
||||||
while (base[i] != '\0')
|
|
||||||
{
|
|
||||||
// Reallocate more space if necessary
|
|
||||||
res_size = resize_buf(&res, res_size, i);
|
|
||||||
|
|
||||||
res[i] = base[i];
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
|
|
||||||
res[i] = '\0';
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Appends str into base at index and stores the resulting string in *result
|
|
||||||
// Returns the size of the resulting buffer
|
|
||||||
// WARNING allocates the result on the heap, do not forget to free it
|
|
||||||
size_t append_str(char *base, char *str, size_t index, char **result)
|
|
||||||
{
|
|
||||||
size_t res_size = BUFFER_SIZE;
|
|
||||||
char *res = malloc(res_size * sizeof(char));
|
|
||||||
if (res == NULL)
|
|
||||||
drop(GENERIC_ERR, "Could not allocate more memory");
|
|
||||||
|
|
||||||
// Copy until index
|
|
||||||
size_t i = 0;
|
|
||||||
while (i < index)
|
|
||||||
{
|
|
||||||
// Reallocate more space if necessary
|
|
||||||
res_size = resize_buf(&res, res_size, i);
|
|
||||||
|
|
||||||
res[i] = base[i];
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Copy str
|
|
||||||
size_t str_i = 0;
|
|
||||||
while (str[str_i] != '\0')
|
|
||||||
{
|
|
||||||
// Reallocate more space if necessary
|
|
||||||
res_size = resize_buf(&res, res_size, i);
|
|
||||||
|
|
||||||
res[i] = str[str_i];
|
|
||||||
i++;
|
|
||||||
str_i++;
|
|
||||||
}
|
|
||||||
res[i] = '\0';
|
|
||||||
|
|
||||||
*result = res;
|
|
||||||
return res_size;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read word until ':', '=' and blanks and expands variable if any is found
|
|
||||||
// Returns the number of skipped chars and stores the resulting string in word
|
|
||||||
size_t read_declaration(struct line *line, size_t index, char **word)
|
|
||||||
{
|
|
||||||
size_t i = index;
|
|
||||||
size_t buf_len = line->length;
|
|
||||||
char *buf = line->buffer;
|
|
||||||
|
|
||||||
size_t res_size = STRING_BUFFER_SIZE;
|
|
||||||
char *res = malloc(sizeof(char) * res_size);
|
|
||||||
if (res == NULL)
|
|
||||||
drop(GENERIC_ERR, "Could not allocate more memory");
|
|
||||||
|
|
||||||
size_t res_i = 0;
|
|
||||||
while (i < buf_len && ischar(buf[i]))
|
|
||||||
{
|
|
||||||
// Reallocate more space if necessary
|
|
||||||
res_size = resize_buf(&res, res_size, i);
|
|
||||||
|
|
||||||
// Expand variable
|
|
||||||
if (buf[i] == '$')
|
|
||||||
{
|
|
||||||
char *expanded_var;
|
|
||||||
char *tmp_res_buf;
|
|
||||||
i += expand_variable(line, i + 1, &expanded_var);
|
|
||||||
res_size = append_str(res, expanded_var, i, &tmp_res_buf);
|
|
||||||
// Replace res with res + expanded_variable
|
|
||||||
res_i = strlen(tmp_res_buf);
|
|
||||||
free(res);
|
|
||||||
// free(expanded_var);
|
|
||||||
res = tmp_res_buf;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
res[res_i] = buf[i];
|
|
||||||
i++;
|
|
||||||
res_i++;
|
|
||||||
}
|
|
||||||
res[res_i] = '\0';
|
|
||||||
|
|
||||||
if (i == index)
|
|
||||||
{
|
|
||||||
free(res);
|
|
||||||
*word = NULL;
|
|
||||||
return i - index;
|
|
||||||
}
|
|
||||||
|
|
||||||
*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;
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -4,27 +4,23 @@
|
||||||
#define STRING_BUFFER_SIZE 32
|
#define STRING_BUFFER_SIZE 32
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
// Holds line information
|
// Holds line information
|
||||||
// Exists only because of EPITA's annoying 4 parameters limit
|
// Exists only because of EPITA's annoying 4 parameters limit
|
||||||
// WARNING buffer must be freed after use
|
// WARNING buffer must be freed after use
|
||||||
struct line
|
struct line
|
||||||
{
|
{
|
||||||
char *buffer; // Line content
|
char* buffer; // Line content
|
||||||
ssize_t length; // Line length
|
ssize_t length; // Line length
|
||||||
size_t number; // Line number in file
|
size_t number; // Line number in file
|
||||||
FILE *file_stream; // Full file stream
|
FILE* file_stream; // Full file stream
|
||||||
};
|
};
|
||||||
|
|
||||||
int ischar(char c);
|
int ischar(char c);
|
||||||
int isblankline(struct line *l);
|
int isblankline(struct line *l);
|
||||||
int skipblanks(char *buf, size_t buf_len);
|
int skipblanks(char *buf, size_t buf_len);
|
||||||
size_t readword(char *buf, size_t buf_len, char **word);
|
size_t readword(char *buf, size_t buf_len, char **word);
|
||||||
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);
|
|
||||||
|
|
||||||
#endif // LINES_H
|
#endif // LINES_H
|
||||||
|
|
|
||||||
1
minimake/src/lists/.gitignore
vendored
Normal file
1
minimake/src/lists/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
list.c
|
||||||
|
|
@ -331,16 +331,3 @@ struct list *list_split(struct list *list, size_t index)
|
||||||
elt->next = NULL;
|
elt->next = NULL;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
void list_deep_destroy(struct list *l)
|
|
||||||
{
|
|
||||||
struct list *elt = l;
|
|
||||||
struct list *next_elt;
|
|
||||||
while (elt != NULL)
|
|
||||||
{
|
|
||||||
next_elt = elt->next;
|
|
||||||
free(elt->data);
|
|
||||||
free(elt);
|
|
||||||
elt = next_elt;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
#ifndef LISTS_H
|
#ifndef LIST_H
|
||||||
#define LISTS_H
|
#define LIST_H
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
struct list
|
struct list
|
||||||
{
|
{
|
||||||
void *data;
|
void* data;
|
||||||
struct list *next;
|
struct list *next;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -13,7 +13,7 @@ struct list
|
||||||
** Insert a node containing `value` at the beginning of the list.
|
** Insert a node containing `value` at the beginning of the list.
|
||||||
** Return `NULL` if an error occured.
|
** 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.
|
** Return the lenght of the list.
|
||||||
|
|
@ -33,18 +33,12 @@ void list_print(struct list *list);
|
||||||
*/
|
*/
|
||||||
void list_destroy(struct list *list);
|
void list_destroy(struct list *list);
|
||||||
|
|
||||||
/*
|
|
||||||
** Release the memory used by the list and its content
|
|
||||||
** Does nothing if `list` is `NULL`.
|
|
||||||
*/
|
|
||||||
void list_deep_destroy(struct list *l);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Append a node containing `value` at the end of the list.
|
** Append a node containing `value` at the end of the list.
|
||||||
** Return `NULL` if an error occured.
|
** Return `NULL` if an error occured.
|
||||||
*/
|
*/
|
||||||
// START PROTO list_append
|
// 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
|
// END PROTO list_append
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -54,7 +48,7 @@ struct list *list_append(struct list *list, void *value);
|
||||||
** Return `NULL` if an error occured.
|
** Return `NULL` if an error occured.
|
||||||
*/
|
*/
|
||||||
// START PROTO list_insert
|
// 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
|
// END PROTO list_insert
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -70,7 +64,7 @@ struct list *list_remove(struct list *list, size_t index);
|
||||||
** Return `-1` if nothing is found.
|
** Return `-1` if nothing is found.
|
||||||
*/
|
*/
|
||||||
// START PROTO list_find
|
// START PROTO list_find
|
||||||
int list_find(struct list *list, void *value);
|
int list_find(struct list *list, void* value);
|
||||||
// END PROTO list_find
|
// END PROTO list_find
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -78,7 +72,7 @@ int list_find(struct list *list, void *value);
|
||||||
** Return `list2` if `list` is `NULL`.
|
** Return `list2` if `list` is `NULL`.
|
||||||
*/
|
*/
|
||||||
// START PROTO list_concat
|
// START PROTO list_concat
|
||||||
// struct list *list_concat(struct list *list, struct list *list2);
|
struct list *list_concat(struct list *list, struct list *list2);
|
||||||
// END PROTO list_concat
|
// END PROTO list_concat
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -86,7 +80,7 @@ int list_find(struct list *list, void *value);
|
||||||
** Return the new list.
|
** Return the new list.
|
||||||
*/
|
*/
|
||||||
// START PROTO list_sort
|
// START PROTO list_sort
|
||||||
// struct list *list_sort(struct list *list);
|
struct list *list_sort(struct list *list);
|
||||||
// END PROTO list_sort
|
// END PROTO list_sort
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -94,7 +88,7 @@ int list_find(struct list *list, void *value);
|
||||||
** Return the new list.
|
** Return the new list.
|
||||||
*/
|
*/
|
||||||
// START PROTO list_reverse
|
// START PROTO list_reverse
|
||||||
// struct list *list_reverse(struct list *list);
|
struct list *list_reverse(struct list *list);
|
||||||
// END PROTO list_reverse
|
// END PROTO list_reverse
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -104,7 +98,7 @@ int list_find(struct list *list, void *value);
|
||||||
** Return `NULL` if `list` is `NULL` or `index` is invalid.
|
** Return `NULL` if `list` is `NULL` or `index` is invalid.
|
||||||
*/
|
*/
|
||||||
// START PROTO list_split
|
// START PROTO list_split
|
||||||
// struct list *list_split(struct list *list, size_t index);
|
struct list *list_split(struct list *list, size_t index);
|
||||||
// END PROTO list_split
|
// END PROTO list_split
|
||||||
|
|
||||||
#endif /* ! LISTS_H */
|
#endif /* ! LIST_H */
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "files/files.h"
|
|
||||||
#include "lists/lists.h"
|
#include "lists/lists.h"
|
||||||
#include "minimake.h"
|
#include "minimake.h"
|
||||||
|
|
||||||
|
|
@ -41,7 +40,7 @@ static int handle_args(int argc, char **argv, struct list **minimake_files,
|
||||||
// Treat as rules
|
// Treat as rules
|
||||||
for (int j = i; j < argc; j++)
|
for (int j = i; j < argc; j++)
|
||||||
{
|
{
|
||||||
rules = list_append(rules, argv[i]);
|
files = list_append(rules, argv[i]);
|
||||||
}
|
}
|
||||||
*minimake_files = files;
|
*minimake_files = files;
|
||||||
*minimake_rules = rules;
|
*minimake_rules = rules;
|
||||||
|
|
@ -51,17 +50,13 @@ static int handle_args(int argc, char **argv, struct list **minimake_files,
|
||||||
else if (strcmp(argv[i], "-h") == 0)
|
else if (strcmp(argv[i], "-h") == 0)
|
||||||
{
|
{
|
||||||
print_help(argv[0]);
|
print_help(argv[0]);
|
||||||
list_destroy(files);
|
|
||||||
list_destroy(rules);
|
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
// Custom file
|
// Custom file
|
||||||
else if (strcmp(argv[i], "-f") == 0)
|
else if (strcmp(argv[i], "-f") == 0)
|
||||||
{
|
{
|
||||||
flags |= FLAGS_CUSTOM_FILE;
|
flags |= FLAGS_CUSTOM_FILE;
|
||||||
i++;
|
if (i + 1 == argc || argv[i + 1][0] == '-')
|
||||||
|
|
||||||
if (i == argc || argv[i][0] == '-')
|
|
||||||
errx(INVALID_ARG, "No file specified after '-f'");
|
errx(INVALID_ARG, "No file specified after '-f'");
|
||||||
|
|
||||||
files = list_prepend(files, argv[i]);
|
files = list_prepend(files, argv[i]);
|
||||||
|
|
@ -76,14 +71,12 @@ static int handle_args(int argc, char **argv, struct list **minimake_files,
|
||||||
{
|
{
|
||||||
printf("Unknown option '%s'", argv[i]);
|
printf("Unknown option '%s'", argv[i]);
|
||||||
print_help(argv[0]);
|
print_help(argv[0]);
|
||||||
list_destroy(files);
|
|
||||||
list_destroy(rules);
|
|
||||||
exit(INVALID_ARG);
|
exit(INVALID_ARG);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else // Rules
|
else // Rules
|
||||||
{
|
{
|
||||||
rules = list_append(rules, argv[i]);
|
files = list_append(rules, argv[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -95,18 +88,40 @@ static int handle_args(int argc, char **argv, struct list **minimake_files,
|
||||||
return flags;
|
return flags;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *getMakefile(struct list *files)
|
||||||
|
{
|
||||||
|
struct list *file = files;
|
||||||
|
int found = 0;
|
||||||
|
|
||||||
|
// Test file presence
|
||||||
|
while (file != NULL && !found)
|
||||||
|
{
|
||||||
|
FILE *test = fopen(file->data, "r");
|
||||||
|
if (test != NULL)
|
||||||
|
found = 1;
|
||||||
|
|
||||||
|
fclose(test);
|
||||||
|
if (!found)
|
||||||
|
file = file->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *res = NULL;
|
||||||
|
if (found)
|
||||||
|
res = file->data;
|
||||||
|
|
||||||
|
list_destroy(files);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
struct list *files;
|
struct list *files;
|
||||||
struct list *rules;
|
struct list *rules;
|
||||||
int flags = handle_args(argc, argv, &files, &rules);
|
int flags = handle_args(argc, argv, &files, &rules);
|
||||||
|
|
||||||
char *filename = get_makefile(files);
|
char *filename = getMakefile(files);
|
||||||
if (filename == NULL)
|
if (filename == NULL)
|
||||||
errx(GENERIC_ERR, "No Makefile found");
|
errx(GENERIC_ERR, "No Makefile found");
|
||||||
|
|
||||||
int res = make(filename, rules, argv[0], flags);
|
make(filename, flags, argv[0]);
|
||||||
list_destroy(files);
|
|
||||||
list_destroy(rules);
|
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,28 +4,19 @@
|
||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <err.h>
|
#include <err.h>
|
||||||
#include <stdarg.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "files/files.h"
|
|
||||||
#include "hash_maps/hash_maps.h"
|
#include "hash_maps/hash_maps.h"
|
||||||
#include "lines/lines.h"
|
#include "lines/lines.h"
|
||||||
#include "lists/lists.h"
|
#include "lists/lists.h"
|
||||||
|
|
||||||
// Static variables
|
// Static variables
|
||||||
|
struct hash_map *variables = NULL;
|
||||||
static char *program_name;
|
struct hash_map *rules = NULL;
|
||||||
|
char *program_name;
|
||||||
static struct hash_map *variables = NULL;
|
|
||||||
static struct hash_map *rules = NULL;
|
|
||||||
|
|
||||||
// Keeps track of variables and rules order
|
|
||||||
static struct list *variables_list = NULL;
|
|
||||||
static struct list *rules_list = NULL;
|
|
||||||
|
|
||||||
// ==== Misc ====
|
// ==== Misc ====
|
||||||
|
|
||||||
|
|
@ -47,19 +38,23 @@ static void hashmap_deep_free(struct hash_map *hash_map)
|
||||||
{
|
{
|
||||||
struct rule *r = entry->value;
|
struct rule *r = entry->value;
|
||||||
free(r->name);
|
free(r->name);
|
||||||
list_deep_destroy(r->dependencies);
|
list_destroy(r->dependencies);
|
||||||
list_deep_destroy(r->recipe);
|
list_destroy(r->recipe);
|
||||||
free(r);
|
free(r);
|
||||||
|
free(entry->key);
|
||||||
}
|
}
|
||||||
else if (hash_map == variables)
|
if (hash_map == variables)
|
||||||
{
|
{
|
||||||
// printf("Att to free: %s\n", entry->key);
|
struct variable *v = entry->value;
|
||||||
|
free(v->name);
|
||||||
|
free(v->value);
|
||||||
|
|
||||||
free(entry->key);
|
free(entry->key);
|
||||||
free(entry->value);
|
free(entry->value);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("DEBUG: attempting to free a generic hashmap\n");
|
printf("DEBUG: attempting to free a generic hashmap");
|
||||||
free(entry->key);
|
free(entry->key);
|
||||||
free(entry->value);
|
free(entry->value);
|
||||||
}
|
}
|
||||||
|
|
@ -72,14 +67,33 @@ static void hashmap_deep_free(struct hash_map *hash_map)
|
||||||
free(hash_map);
|
free(hash_map);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void free_all(void)
|
static void free_all()
|
||||||
{
|
{
|
||||||
list_destroy(rules_list);
|
|
||||||
list_destroy(variables_list);
|
|
||||||
hashmap_deep_free(rules);
|
hashmap_deep_free(rules);
|
||||||
hashmap_deep_free(variables);
|
hashmap_deep_free(variables);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// static void dump_database()
|
||||||
|
// {
|
||||||
|
// hashmap_dump(variables);
|
||||||
|
// 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 ====
|
// ==== Parsing ====
|
||||||
|
|
||||||
// Registers a new rule in the hashmap
|
// Registers a new rule in the hashmap
|
||||||
|
|
@ -93,14 +107,15 @@ static void register_rule(char *name, struct list *dependencies,
|
||||||
rule->dependencies = dependencies;
|
rule->dependencies = dependencies;
|
||||||
rule->recipe = recipe;
|
rule->recipe = recipe;
|
||||||
|
|
||||||
int err = hashmap_insert(rules, name, rule, NULL);
|
int err = hashmap_insert(variables, name, rule, NULL);
|
||||||
if (!err)
|
if (!err)
|
||||||
{
|
{
|
||||||
drop(GENERIC_ERR,
|
char *err_msg;
|
||||||
"Internal Error: Couln't add entry for '%s' in the hashmap", name);
|
sprintf(err_msg,
|
||||||
|
"Internal Error: Couln't add entry for '%s' in the hashmap",
|
||||||
|
name);
|
||||||
|
exit_on_error(GENERIC_ERR, err_msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
rules_list = list_append(rules_list, name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Registers a new variable in the hashmap
|
// Registers a new variable in the hashmap
|
||||||
|
|
@ -111,11 +126,12 @@ static void register_variable(char *name, char *value)
|
||||||
int err = hashmap_insert(variables, name, value, NULL);
|
int err = hashmap_insert(variables, name, value, NULL);
|
||||||
if (!err)
|
if (!err)
|
||||||
{
|
{
|
||||||
drop(GENERIC_ERR,
|
char *err_msg;
|
||||||
"Internal Error: Couln't add entry for '%s' in the hashmap", name);
|
sprintf(err_msg,
|
||||||
|
"Internal Error: Couln't add entry for '%s' in the hashmap",
|
||||||
|
name);
|
||||||
|
exit_on_error(GENERIC_ERR, err_msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
variables_list = list_append(variables_list, name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse dependencies from buf and returns them inside a chained list
|
// Parse dependencies from buf and returns them inside a chained list
|
||||||
|
|
@ -145,9 +161,12 @@ static struct list *read_deps(struct line *l, size_t offset)
|
||||||
if (!ischar(buf[i]) && !isblank(buf[i]) && buf[i] != '\0'
|
if (!ischar(buf[i]) && !isblank(buf[i]) && buf[i] != '\0'
|
||||||
&& buf[i] != '\n')
|
&& buf[i] != '\n')
|
||||||
{
|
{
|
||||||
drop(GENERIC_ERR,
|
char *err_msg;
|
||||||
|
sprintf(
|
||||||
|
err_msg,
|
||||||
"Unexpected character '%c' after rule declaration at %lu:%lu",
|
"Unexpected character '%c' after rule declaration at %lu:%lu",
|
||||||
buf[i], l->number, i);
|
buf[i], l->number, i);
|
||||||
|
exit_on_error(GENERIC_ERR, err_msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -180,17 +199,16 @@ static struct list *read_recipe(struct line *l)
|
||||||
}
|
}
|
||||||
else // Add recipe to list
|
else // Add recipe to list
|
||||||
{
|
{
|
||||||
size_t offset = 1 + skipblanks(buf + 1, l->length - 1);
|
char *command = strdup(buf + 1);
|
||||||
|
|
||||||
char *command = strdup(buf + offset);
|
|
||||||
if (command == NULL)
|
if (command == NULL)
|
||||||
{
|
{
|
||||||
drop(GENERIC_ERR,
|
char *err_msg;
|
||||||
|
sprintf(err_msg,
|
||||||
"Internal error: couldn't duplicate string (%lu:1)",
|
"Internal error: couldn't duplicate string (%lu:1)",
|
||||||
l->number);
|
l->number);
|
||||||
|
exit_on_error(GENERIC_ERR, err_msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
command[l->length - offset - 1] = '\0';
|
|
||||||
res = list_append(res, command);
|
res = list_append(res, command);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -199,16 +217,14 @@ static struct list *read_recipe(struct line *l)
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
// === Variable Expansion ===
|
|
||||||
|
|
||||||
// Reads the value after a variable declaration
|
// Reads the value after a variable declaration
|
||||||
static char *read_variable_value(char *buf, size_t buf_len)
|
static char *read_value(char *buf, size_t buf_size)
|
||||||
{
|
{
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
size_t str_buf_size = STRING_BUFFER_SIZE;
|
size_t str_buf_size = STRING_BUFFER_SIZE;
|
||||||
char *str_buf = malloc(sizeof(char) * str_buf_size);
|
char *str_buf = malloc(sizeof(char) * str_buf_size);
|
||||||
|
|
||||||
while (i < buf_len && buf[i] != '\n' && buf[i] != '#' && buf[i] != '\0')
|
while (i < buf_size && buf[i] != '\0' && buf[i] != '#')
|
||||||
{
|
{
|
||||||
// Reallocate more space if necessary
|
// Reallocate more space if necessary
|
||||||
if (i >= str_buf_size - 1)
|
if (i >= str_buf_size - 1)
|
||||||
|
|
@ -216,7 +232,11 @@ static char *read_variable_value(char *buf, size_t buf_len)
|
||||||
str_buf_size += STRING_BUFFER_SIZE;
|
str_buf_size += STRING_BUFFER_SIZE;
|
||||||
str_buf = realloc(str_buf, str_buf_size);
|
str_buf = realloc(str_buf, str_buf_size);
|
||||||
if (str_buf == NULL)
|
if (str_buf == NULL)
|
||||||
drop(GENERIC_ERR, "Could not realloc");
|
{
|
||||||
|
char *err_msg;
|
||||||
|
sprintf(err_msg, "Could not realloc");
|
||||||
|
exit_on_error(GENERIC_ERR, err_msg);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
str_buf[i] = buf[i];
|
str_buf[i] = buf[i];
|
||||||
|
|
@ -228,124 +248,7 @@ static char *read_variable_value(char *buf, size_t buf_len)
|
||||||
return str_buf;
|
return str_buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reads variable name from buf until ')' and stores it into *result
|
// static char *expand_variable();
|
||||||
// Returns its length on success and -1 on fail
|
|
||||||
// WARNING allocates memory on the heap, free *result after use
|
|
||||||
static size_t read_variable_name(struct line *line, size_t index, char **result)
|
|
||||||
{
|
|
||||||
size_t i = index;
|
|
||||||
char *buf = line->buffer;
|
|
||||||
|
|
||||||
// Alloc var_buf
|
|
||||||
size_t var_buf_size = STRING_BUFFER_SIZE;
|
|
||||||
char *var_buf = malloc(sizeof(char) * var_buf_size);
|
|
||||||
if (var_buf == NULL)
|
|
||||||
drop(GENERIC_ERR, "Could not allocate more memory");
|
|
||||||
|
|
||||||
size_t var_i = 0;
|
|
||||||
while (buf[i] != '\n' && buf[i] != '\0' && buf[i] != '#' && buf[i] != ')')
|
|
||||||
{
|
|
||||||
// Reallocate more space if necessary
|
|
||||||
var_buf_size = resize_buf(&var_buf, var_buf_size, i);
|
|
||||||
|
|
||||||
// Copy
|
|
||||||
var_buf[var_i] = buf[i];
|
|
||||||
i++;
|
|
||||||
var_i++;
|
|
||||||
}
|
|
||||||
var_buf[var_i] = '\0';
|
|
||||||
|
|
||||||
// Mismatched parenthesis
|
|
||||||
if (buf[i] != ')')
|
|
||||||
{
|
|
||||||
free(var_buf);
|
|
||||||
*result = NULL;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
i += 2;
|
|
||||||
*result = var_buf;
|
|
||||||
return i - index;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Searches for variable in database and returns its value
|
|
||||||
// if no value is found, it drops an error and exit the program
|
|
||||||
// WARNING variable must be allocated on the heap as this function may attempt
|
|
||||||
// to free it
|
|
||||||
// NOTE Takes line_number to print an helpful error message
|
|
||||||
static char *get_variable_value(char *variable_name, size_t line_number)
|
|
||||||
{
|
|
||||||
char *value = hashmap_get(variables, variable_name);
|
|
||||||
if (value == NULL)
|
|
||||||
{
|
|
||||||
// Adds tmp_buf to the list of items to free
|
|
||||||
variables_list = list_append(variables_list, variable_name);
|
|
||||||
drop(GENERIC_ERR, "Could not find specified variable '%s' at line %lu",
|
|
||||||
variable_name, line_number);
|
|
||||||
}
|
|
||||||
|
|
||||||
free(variable_name);
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Gets the corresponding value of the variable and stores it in *value
|
|
||||||
// Returns the number of skipped characters
|
|
||||||
size_t expand_variable(struct line *line, size_t index, char **value)
|
|
||||||
{
|
|
||||||
size_t i = index;
|
|
||||||
char *buf = line->buffer;
|
|
||||||
|
|
||||||
// Name in parenthesis
|
|
||||||
if (buf[i] == '(')
|
|
||||||
{
|
|
||||||
// Read variable
|
|
||||||
char *var_name;
|
|
||||||
i++;
|
|
||||||
i += read_variable_name(line, i, &var_name);
|
|
||||||
if (var_name == NULL)
|
|
||||||
drop(GENERIC_ERR, "Mismatched parenthesis at %lu:%lu", i,
|
|
||||||
line->number);
|
|
||||||
|
|
||||||
// Get corresponding value
|
|
||||||
*value = get_variable_value(var_name, line->number);
|
|
||||||
|
|
||||||
// Return
|
|
||||||
return i - index;
|
|
||||||
}
|
|
||||||
else // Single char variable
|
|
||||||
{
|
|
||||||
// '$ '
|
|
||||||
if (isblank(buf[i]) || isspace(buf[i]))
|
|
||||||
drop(2, "Unauthorized character '%c' at %lu:%lu", buf[i],
|
|
||||||
line->number, i);
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Allocate temporary buffer to hold variable name
|
|
||||||
char *var_name = malloc(2 * sizeof(char));
|
|
||||||
if (var_name == NULL)
|
|
||||||
// Seriously, not even two bytes
|
|
||||||
drop(GENERIC_ERR, "Could not allocate memory");
|
|
||||||
|
|
||||||
var_name[0] = buf[i];
|
|
||||||
var_name[1] = '\0';
|
|
||||||
|
|
||||||
// Escape '$$'
|
|
||||||
if (buf[i] == '$')
|
|
||||||
{
|
|
||||||
*value = var_name;
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get corresponding value
|
|
||||||
*value = get_variable_value(var_name, line->number);
|
|
||||||
free(var_name);
|
|
||||||
}
|
|
||||||
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// =================
|
|
||||||
|
|
||||||
// Takes a buffer containing the line to parse and it length
|
// Takes a buffer containing the line to parse and it length
|
||||||
// As well as the line number in the file for error handling
|
// As well as the line number in the file for error handling
|
||||||
|
|
@ -360,8 +263,7 @@ static void parse_line(struct line *current_line)
|
||||||
|
|
||||||
// Read name
|
// Read name
|
||||||
char *name;
|
char *name;
|
||||||
// i += readword(buf + i, line_size - i, &name);
|
i += readword(buf + i, line_size - i, &name);
|
||||||
i += read_declaration(current_line, i, &name);
|
|
||||||
// if (name == NULL)
|
// if (name == NULL)
|
||||||
// errx(1, "Il s'est passé quoi là ? \nUnexpected character at %lu:%lu",
|
// errx(1, "Il s'est passé quoi là ? \nUnexpected character at %lu:%lu",
|
||||||
// current_line->number, i);
|
// current_line->number, i);
|
||||||
|
|
@ -372,14 +274,14 @@ static void parse_line(struct line *current_line)
|
||||||
struct list *dependencies;
|
struct list *dependencies;
|
||||||
struct list *recipe;
|
struct list *recipe;
|
||||||
char *value;
|
char *value;
|
||||||
|
char *err_msg;
|
||||||
|
|
||||||
// Definition type
|
// Definition type
|
||||||
switch (buf[i])
|
switch (buf[i])
|
||||||
{
|
{
|
||||||
// Variable
|
// Variable
|
||||||
case '=':
|
case '=':
|
||||||
i += skipblanks(buf + i + 1, line_size - i) + 1;
|
value = read_value(buf + i, line_size);
|
||||||
value = read_variable_value(buf + i, line_size - i);
|
|
||||||
register_variable(name, value);
|
register_variable(name, value);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
@ -403,16 +305,20 @@ static void parse_line(struct line *current_line)
|
||||||
case '#':
|
case '#':
|
||||||
if (name != NULL)
|
if (name != NULL)
|
||||||
{
|
{
|
||||||
drop(GENERIC_ERR,
|
sprintf(
|
||||||
|
err_msg,
|
||||||
"Unexpected character '%c' after declaration '%s' at line %lu",
|
"Unexpected character '%c' after declaration '%s' at line %lu",
|
||||||
buf[i], name, current_line->number);
|
buf[i], name, current_line->number);
|
||||||
|
exit_on_error(GENERIC_ERR, err_msg);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
drop(GENERIC_ERR,
|
sprintf(err_msg,
|
||||||
"Unexpected character '%c' after declaration '%s' at line %lu",
|
"Unexpected character '%c' after declaration '%s' at line %lu",
|
||||||
buf[i], name, current_line->number);
|
buf[i], name, current_line->number);
|
||||||
|
exit_on_error(GENERIC_ERR, err_msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
// free(name);
|
// free(name);
|
||||||
|
|
@ -435,7 +341,11 @@ void make_parse(char *path)
|
||||||
size_t buf_size = BUFFER_SIZE;
|
size_t buf_size = BUFFER_SIZE;
|
||||||
char *buf = malloc(sizeof(char) * buf_size);
|
char *buf = malloc(sizeof(char) * buf_size);
|
||||||
if (buf == NULL)
|
if (buf == NULL)
|
||||||
drop(GENERIC_ERR, "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
|
// Parse line by line
|
||||||
ssize_t nread;
|
ssize_t nread;
|
||||||
|
|
@ -445,7 +355,11 @@ void make_parse(char *path)
|
||||||
while ((nread = getline(&buf, &buf_size, stream)) != -1)
|
while ((nread = getline(&buf, &buf_size, stream)) != -1)
|
||||||
{
|
{
|
||||||
if (nread == -1)
|
if (nread == -1)
|
||||||
drop(GENERIC_ERR, "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.buffer = buf;
|
||||||
current_line.length = nread;
|
current_line.length = nread;
|
||||||
|
|
@ -462,213 +376,16 @@ void make_parse(char *path)
|
||||||
|
|
||||||
// ==== Runtime ====
|
// ==== Runtime ====
|
||||||
|
|
||||||
// Return 1 if rule is up to date, 0 otherwise
|
|
||||||
static int uptodate(struct rule *rule)
|
|
||||||
{
|
|
||||||
struct stat path_stat;
|
|
||||||
|
|
||||||
if (lstat(rule->name, &path_stat) != 0) // target doesn't exists
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
struct list *dependencies = rule->dependencies;
|
|
||||||
while (dependencies != NULL)
|
|
||||||
{
|
|
||||||
char *depname = dependencies->data;
|
|
||||||
if (lstat(depname, &path_stat) != 0) // Dependecy doesn't exists
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Expands and run a specified rule
|
|
||||||
static int run_rule(struct rule *rule)
|
|
||||||
{
|
|
||||||
// Check if p to date
|
|
||||||
if (uptodate(rule))
|
|
||||||
{
|
|
||||||
printf("%s: '%s' is up to date.", program_name, rule->name);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct list *commands = rule->recipe;
|
|
||||||
struct list *dependencies = rule->dependencies;
|
|
||||||
|
|
||||||
// Build dependencies
|
|
||||||
while (dependencies != NULL)
|
|
||||||
{
|
|
||||||
// Expand variable
|
|
||||||
// TODO
|
|
||||||
|
|
||||||
// Check file existence
|
|
||||||
if (!file_exists(dependencies->data))
|
|
||||||
{
|
|
||||||
// Check rule existence instead
|
|
||||||
struct rule *dep_rule = hashmap_get(rules, dependencies->data);
|
|
||||||
if (dep_rule != NULL)
|
|
||||||
{
|
|
||||||
int res = run_rule(dep_rule);
|
|
||||||
if (res != 0) // Exit on error
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
drop(GENERIC_ERR, "No rule to make target '%s'",
|
|
||||||
dependencies->data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies = dependencies->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Empty recipe
|
|
||||||
if (commands == NULL)
|
|
||||||
{
|
|
||||||
printf("%s: Nothing to be done for '%s'.\n", program_name, rule->name);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (commands != NULL)
|
|
||||||
{
|
|
||||||
// Expand command variables
|
|
||||||
// TODO
|
|
||||||
|
|
||||||
// Run
|
|
||||||
int res = run_command(commands->data);
|
|
||||||
if (res != 0) // Exit on error
|
|
||||||
return res;
|
|
||||||
|
|
||||||
commands = commands->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run the given rules after parsing
|
|
||||||
int make_run(struct list *given_rules)
|
|
||||||
{
|
|
||||||
if (given_rules == NULL)
|
|
||||||
{
|
|
||||||
// No rule specified => run first rule found in the Makefile
|
|
||||||
if (rules_list == NULL)
|
|
||||||
drop(GENERIC_ERR, "No targets");
|
|
||||||
|
|
||||||
struct rule *full_rule = hashmap_get(rules, rules_list->data);
|
|
||||||
if (full_rule == NULL)
|
|
||||||
drop(GENERIC_ERR,
|
|
||||||
"Internal error: Could not retrieve target '%s' in database",
|
|
||||||
rules_list->data);
|
|
||||||
|
|
||||||
return run_rule(full_rule);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
while (given_rules != NULL)
|
|
||||||
{
|
|
||||||
struct rule *full_rule = hashmap_get(rules, given_rules->data);
|
|
||||||
if (full_rule == NULL)
|
|
||||||
drop(GENERIC_ERR, "No rule to make target '%s'",
|
|
||||||
given_rules->data);
|
|
||||||
|
|
||||||
int res = run_rule(full_rule);
|
|
||||||
if (res != 0)
|
|
||||||
return res;
|
|
||||||
|
|
||||||
given_rules = given_rules->next;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ==== MAKE ====
|
// ==== MAKE ====
|
||||||
|
|
||||||
int make(char *path, struct list *rules, char *exec_name, int flags)
|
void make(char *path, int flags, char *argv0)
|
||||||
{
|
{
|
||||||
program_name = exec_name;
|
program_name = argv0;
|
||||||
|
|
||||||
|
if (flags & FLAGS_PRINT)
|
||||||
|
errx(GENERIC_ERR, "Not Implemented");
|
||||||
|
|
||||||
make_parse(path);
|
make_parse(path);
|
||||||
|
// dump_database();
|
||||||
// Print
|
|
||||||
if (flags & FLAGS_PRINT)
|
|
||||||
{
|
|
||||||
dump_database();
|
|
||||||
free_all();
|
free_all();
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run
|
|
||||||
int status = make_run(rules);
|
|
||||||
free_all();
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Prints an error message before exiting gracefully (by freeing all variables)
|
|
||||||
void drop(int status, char *format, ...)
|
|
||||||
{
|
|
||||||
// Print
|
|
||||||
va_list args;
|
|
||||||
va_start(args, format);
|
|
||||||
fprintf(stderr, "%s: *** ", program_name);
|
|
||||||
vfprintf(stderr, format, args);
|
|
||||||
fprintf(stderr, ". Stop.\n");
|
|
||||||
va_end(args);
|
|
||||||
|
|
||||||
free_all();
|
|
||||||
exit(status);
|
|
||||||
}
|
|
||||||
|
|
||||||
void dump_database(void)
|
|
||||||
{
|
|
||||||
struct list *elt;
|
|
||||||
|
|
||||||
// Dump variables
|
|
||||||
elt = variables_list;
|
|
||||||
puts("# variables");
|
|
||||||
while (elt != NULL)
|
|
||||||
{
|
|
||||||
// Get var
|
|
||||||
char *val = hashmap_get(variables, elt->data);
|
|
||||||
if (val == NULL)
|
|
||||||
drop(GENERIC_ERR, "Could not get variable '%s' in database",
|
|
||||||
elt->data);
|
|
||||||
|
|
||||||
// Print
|
|
||||||
char *key = elt->data;
|
|
||||||
printf("'%s' = '%s'\n", key, val);
|
|
||||||
|
|
||||||
elt = elt->next;
|
|
||||||
}
|
|
||||||
// Dump rules
|
|
||||||
elt = rules_list;
|
|
||||||
puts("# rules");
|
|
||||||
while (elt != NULL)
|
|
||||||
{
|
|
||||||
// Get rule
|
|
||||||
struct rule *rule = hashmap_get(rules, elt->data);
|
|
||||||
if (rule == NULL)
|
|
||||||
drop(GENERIC_ERR, "Could not get rule '%s' in database", elt->data);
|
|
||||||
|
|
||||||
// Print name
|
|
||||||
printf("(%s):", rule->name);
|
|
||||||
|
|
||||||
// Print dependencies
|
|
||||||
struct list *dep = rule->dependencies;
|
|
||||||
while (dep != NULL)
|
|
||||||
{
|
|
||||||
char *dep_str = dep->data;
|
|
||||||
printf(" [%s]", dep_str);
|
|
||||||
dep = dep->next;
|
|
||||||
}
|
|
||||||
putchar('\n');
|
|
||||||
|
|
||||||
// Print recipe
|
|
||||||
struct list *rcp = rule->recipe;
|
|
||||||
while (rcp != NULL)
|
|
||||||
{
|
|
||||||
char *rcp_str = rcp->data;
|
|
||||||
printf("\t'%s'\n", rcp_str);
|
|
||||||
rcp = rcp->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
elt = elt->next;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,15 +10,14 @@
|
||||||
#define FLAGS_PRINT 2
|
#define FLAGS_PRINT 2
|
||||||
|
|
||||||
// Default values
|
// Default values
|
||||||
#define DEFAULT_MAKEFILE "makefile"
|
#define DEFAULT_MAKEFILE "Makefile"
|
||||||
#define DEFAULT_MAKEFILE_2 "Makefile"
|
#define DEFAULT_MAKEFILE_2 "makefile"
|
||||||
#define BUFFER_SIZE 1024
|
#define BUFFER_SIZE 1024
|
||||||
#define HASHMAP_SIZE 32
|
#define HASHMAP_SIZE 32
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
#include "lines/lines.h"
|
|
||||||
#include "lists/lists.h"
|
#include "lists/lists.h"
|
||||||
|
|
||||||
// Holds variable information
|
// Holds variable information
|
||||||
|
|
@ -33,16 +32,12 @@ struct variable
|
||||||
// WARNING its values must be freed after use
|
// WARNING its values must be freed after use
|
||||||
struct rule
|
struct rule
|
||||||
{
|
{
|
||||||
char *name;
|
char* name;
|
||||||
struct list *dependencies;
|
struct list *dependencies;
|
||||||
struct list *recipe;
|
struct list *recipe;
|
||||||
};
|
};
|
||||||
|
|
||||||
int make(char *path, struct list *rules, char *argv0, int flags);
|
void make(char *path, int flags, char* program_name);
|
||||||
void make_parse(char *path);
|
void make_parse(char *path);
|
||||||
int make_run(struct list *rules);
|
|
||||||
void drop(int status, char *format, ...);
|
|
||||||
void dump_database(void);
|
|
||||||
size_t expand_variable(struct line *line, size_t index, char **value);
|
|
||||||
|
|
||||||
#endif // ! MINIMAKE_H
|
#endif // ! MINIMAKE_H
|
||||||
|
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
empty_target:
|
|
||||||
|
|
@ -1,35 +0,0 @@
|
||||||
SIMPLE_VAR = coucou
|
|
||||||
SIMPLE_VAR_COMMENT = the comment is gone # comment
|
|
||||||
|
|
||||||
# the following line starts with a space then a tab
|
|
||||||
SPACES_BEFORE_TAB = var_beginning var_end
|
|
||||||
|
|
||||||
sparse_rule: depa depb
|
|
||||||
|
|
||||||
command 1
|
|
||||||
|
|
||||||
command 2
|
|
||||||
|
|
||||||
B = B_var_beginning B_var_end
|
|
||||||
|
|
||||||
packed_rule: depa depb
|
|
||||||
command 1
|
|
||||||
command 2
|
|
||||||
|
|
||||||
silent_rule: depa depb
|
|
||||||
@ command 1
|
|
||||||
@command 2
|
|
||||||
|
|
||||||
rule_comment: depa depb # comment
|
|
||||||
|
|
||||||
command_space_rule: depa depb
|
|
||||||
echo spaces before
|
|
||||||
echo spaces after
|
|
||||||
echo this is a # comment
|
|
||||||
|
|
||||||
simple_rule: simple_dep
|
|
||||||
|
|
||||||
no_dep_rule:
|
|
||||||
|
|
||||||
variable_rule: beginning $(SIMPLE_VAR) end
|
|
||||||
echo "shouldn't be expanded: $(SIMPLE_VAR)"
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
VAR = 1
|
|
||||||
|
|
@ -1,7 +0,0 @@
|
||||||
rule1:
|
|
||||||
echo Toto
|
|
||||||
echo Tata
|
|
||||||
echo Tutu
|
|
||||||
|
|
||||||
rule2:
|
|
||||||
@echo Toto encore
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
V=2
|
|
||||||
|
|
||||||
main:
|
|
||||||
echo $V
|
|
||||||
|
|
@ -1,36 +0,0 @@
|
||||||
SIMPLE_VAR = coucou
|
|
||||||
SIMPLE_VAR_COMMENT = the comment is gone # comment
|
|
||||||
$(SIMPLE_VAR) = 1
|
|
||||||
|
|
||||||
# the following line starts with a space then a tab
|
|
||||||
SPACES_BEFORE_TAB = var_beginning var_end
|
|
||||||
|
|
||||||
sparse_rule: depa depb
|
|
||||||
|
|
||||||
command 1
|
|
||||||
|
|
||||||
command 2
|
|
||||||
|
|
||||||
B = B_var_beginning B_var_end
|
|
||||||
|
|
||||||
packed_rule: depa depb
|
|
||||||
command 1
|
|
||||||
command 2
|
|
||||||
|
|
||||||
silent_rule: depa depb
|
|
||||||
@ command 1
|
|
||||||
@command 2
|
|
||||||
|
|
||||||
rule_comment: depa depb # comment
|
|
||||||
|
|
||||||
command_space_rule: depa depb
|
|
||||||
echo spaces before
|
|
||||||
echo spaces after
|
|
||||||
echo this is a # comment
|
|
||||||
|
|
||||||
simple_rule: simple_dep
|
|
||||||
|
|
||||||
no_dep_rule:
|
|
||||||
|
|
||||||
variable_rule: beginning $(SIMPLE_VAR) end
|
|
||||||
echo "shouldn't be expanded: $(SIMPLE_VAR)"
|
|
||||||
|
|
@ -1,77 +0,0 @@
|
||||||
#!/bin/sh
|
|
||||||
|
|
||||||
###########################################
|
|
||||||
# PIERRE PAGNOUX EXPERIENCE TESTSUITE #
|
|
||||||
###########################################
|
|
||||||
|
|
||||||
#----------------- COLOR -----------------#
|
|
||||||
# 0 - No style | 1 - Bold
|
|
||||||
RED="\e[0;31m"
|
|
||||||
BRED="\e[1;31m"
|
|
||||||
GRN="\e[0;32m"
|
|
||||||
BGRN="\e[1;32m"
|
|
||||||
YEL="\e[0;33m"
|
|
||||||
BYEL="\e[1;33m"
|
|
||||||
BLU="\e[0;34m"
|
|
||||||
BBLU="\e[1;34m"
|
|
||||||
PUR="\e[0;35m"
|
|
||||||
BPUR="\e[1;35m"
|
|
||||||
CYA="\e[0;36m"
|
|
||||||
BCYA="\e[1;36m"
|
|
||||||
WHI="\e[0;37m"
|
|
||||||
BWHI="\e[1;37m"
|
|
||||||
GRE="\e[2;37m"
|
|
||||||
|
|
||||||
#----------------- GLOBV -----------------#
|
|
||||||
ref_out=/tmp/ref_out.out
|
|
||||||
my_out=/tmp/my_out.out
|
|
||||||
|
|
||||||
#----------------- TESTS -----------------#
|
|
||||||
|
|
||||||
tit_wrap()
|
|
||||||
{
|
|
||||||
echo -e $@$WHI
|
|
||||||
}
|
|
||||||
|
|
||||||
func_test()
|
|
||||||
{
|
|
||||||
tit_wrap - $PUR $1
|
|
||||||
shift
|
|
||||||
printf $@ > $ref_out
|
|
||||||
../../tinyprintf $@ > $my_out
|
|
||||||
|
|
||||||
diff $ref_out $my_out > /tmp/null
|
|
||||||
|
|
||||||
if [ $? -eq 0 ]; then
|
|
||||||
tit_wrap $GRN GOOD
|
|
||||||
else
|
|
||||||
tit_wrap $RED EXPECTED: $BRED$(cat $ref_out) \|$RED GOT: $BRED$(cat $my_out)
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
func_file()
|
|
||||||
{
|
|
||||||
for file in *; do
|
|
||||||
if [ -f $file ]; then
|
|
||||||
func_test $file $(cat $file)
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
func_dir()
|
|
||||||
{
|
|
||||||
cd tests
|
|
||||||
for dir in *; do
|
|
||||||
if [ -d $dir ]; then
|
|
||||||
tit_wrap == $dir ==
|
|
||||||
cd $dir
|
|
||||||
func_file
|
|
||||||
cd ..
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
cd ..
|
|
||||||
}
|
|
||||||
|
|
||||||
# func_dir
|
|
||||||
|
|
||||||
echo "Tests should be here"
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue