Skip to content

Commit

Permalink
Merge pull request #8 from SizzleUnrlsd/Garbage-Collector
Browse files Browse the repository at this point in the history
Overhaul of the garbage collector system
  • Loading branch information
SizzleUnrlsd committed Aug 11, 2023
2 parents bc07475 + 362e976 commit 131911d
Show file tree
Hide file tree
Showing 20 changed files with 94 additions and 121 deletions.
1 change: 0 additions & 1 deletion src/init_shell/init_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ void
shell_requirement(shell_t *shell, char **env,
call_alias_t **call_alias UNUSED_ARG)
{
init_garbage(shell);
init_env(shell, env);
init_alias(shell);
init_local_env(shell);
Expand Down
2 changes: 1 addition & 1 deletion src/prompt_function/prompt_engine/autocompletion_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ my_completion_display_matches(char **matches,
}

if (( a%2 ) == 0 && first_comp == NULL && last_comp == NULL) {
first_comp = strdup(rl_line_buffer);
first_comp = _strdup(rl_line_buffer);
++a;
goto looser;
}
Expand Down
2 changes: 1 addition & 1 deletion src/prompt_function/prompt_engine/char_stream_formatting.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ formatting_brackets(char *command)
}
}

result = (char *)malloc(newLength + 1); // allocating memory for the new string
result = (char *)_malloc(newLength + 1); // allocating memory for the new string

for (int i = 0; i < length; i++) {
if (command[i] == '(') {
Expand Down
1 change: 1 addition & 0 deletions src/prompt_function/prompt_engine/dollar_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ int32_t
dollar_engine(char **arg, int32_t i, shell_t *shell)
{
char *tmp = NULL;

if (arg[i][0] == '$') {
if (_strcmp(arg[i], "$?") == 0) {
_printfe("%d", shell->status);
Expand Down
6 changes: 4 additions & 2 deletions src/prompt_function/prompt_engine/git_ref.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ getgit_branch()
{
char _buf[128] = {0};
char *branch = NULL;
char *tmp = NULL;
size_t len = DEFAULT(len);
FILE *fp = fopen(".git/HEAD", "r");
if (!fp) {
Expand All @@ -40,6 +41,7 @@ getgit_branch()
}
}
fclose(fp);

return strdup(branch);
tmp = strdup(branch);
garbage_backup_ptr(tmp);
return tmp;
}
37 changes: 19 additions & 18 deletions src/prompt_function/prompt_engine/open_directory.c
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
/**
* {{ project }}
* Copyright (C) {{ year }} {{ organization }}
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
* Copyright (C) 2023 hugo
*
* This file is part of TekSH.
*
* TekSH is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TekSH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TekSH. If not, see <http://www.gnu.org/licenses/>.
*/

#include "shell.h"

Expand Down Expand Up @@ -86,11 +88,10 @@ open_directory(shell_t *shell, char **path, char **arg)
for (int32_t i = 0; path[i]; i++) {
fmall = _strlen(without_n);
concat_first_step = concat_char_str('/', path[i], fmall, 1);
concat_second_step = (char *)malloc_attribut(sizeof(char)
* (_strlen(concat_first_step) + 1), shell);
concat_second_step = (char *)_malloc(sizeof(char)
* (_strlen(concat_first_step) + 1));
concat_second_step = _strcat(concat_first_step, without_n);
execve(concat_second_step, arg, shell->set_env->env_array);
free(concat_second_step);
}
return 0;
}
49 changes: 27 additions & 22 deletions src/prompt_function/prompt_engine/parse_stdin.c
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
/**
* {{ project }}
* Copyright (C) {{ year }} {{ organization }}
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
* Copyright (C) 2023 hugo
*
* This file is part of TekSH.
*
* TekSH is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TekSH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TekSH. If not, see <http://www.gnu.org/licenses/>.
*/

#include "shell.h"

Expand All @@ -38,7 +40,7 @@ add_space_after_exclamation(char *buf)
if (buf[i] == '!')
count++;
}
new_buf = malloc(len + count + 1);
new_buf = _malloc(len + count + 1);
if (!new_buf) {
_p_error(_MEM_ALLOCA_ERROR);
}
Expand All @@ -63,27 +65,30 @@ little_inibitor(char **command)
}

char **
parse_stdin(char *command, shell_t *shell)
parse_stdin(char *command)
{
char **clean_arg = DEFAULT(clean_arg), **arg = DEFAULT(arg);
int32_t a = DEFAULT(a), count = DEFAULT(count);

little_inibitor(&command);

if (command[0] == '!')
command = add_space_after_exclamation(command);
arg = _str_to_word_array_custom_double(shell, command, ' ', '\t');

arg = _str_to_word_array_custom_double(command, ' ', '\t');
for (int32_t i = 0; arg[i]; i++)
if ((_strcmp(arg[i], " ") != 0) || (_strcmp(arg[i], "\t") != 0))
a++;
clean_arg = (char **)malloc_attribut(sizeof(char *) * (a + 1), shell);

clean_arg = (char **)_malloc(sizeof(char *) * (a + 1));
for (int32_t i = 0; arg[i]; i++) {
if ((_strcmp(arg[i], " ") != 0) || (_strcmp(arg[i], "\t") != 0)) {
clean_arg[count] = _strdup(arg[i], shell);
clean_arg[count] = _strdup(arg[i]);
count++;
}
}
clean_arg[a] = NULL;
free_ptr(arg);
arg = NULL;
_free(arg);

return clean_arg;
}
14 changes: 7 additions & 7 deletions src/prompt_function/prompt_engine/prompt_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ concat_str(const char *str1, const char *str2, ...)
size_t len2 = strlen(str2);
const char *str = DEFAULT(str);

char *result = (char *)malloc(sizeof(char) * (len1 + len2 + 1));
char *result = (char *)_mallocbucket(sizeof(char) * (len1 + len2 + 1));
if (!result) {
exit(EXIT_FAILURE);
}
Expand All @@ -58,7 +58,7 @@ concat_str(const char *str1, const char *str2, ...)

while ((str = va_arg(args, const char *)) != NULL) {
size_t len = strlen(str);
result = (char *)realloc(result, sizeof(char) * (len1 + len2 + len + 1));
result = (char *)_realloc(result, sizeof(char) * (len1 + len2 + len + 1));
if (!result) {
exit(EXIT_FAILURE);
}
Expand All @@ -85,7 +85,8 @@ char *set_promt(void)
sprintf(prompt, " ");

_detail = concat_str("\033[0;34m[", reset, get_dir(), "\033[0;34m]", reset, prompt, NULL);
return strdup(_detail);
garbage_backup_ptr(_detail);
return _strdup(_detail);
}

/* Sigint management */
Expand Down Expand Up @@ -151,7 +152,6 @@ prompt_shell(shell_t *shell)
if (!detail) {
_p_error(_MEM_ALLOCA_ERROR);
}
garbage_collector(detail, shell);

do {
if (shell->status == 0 && RD_TTY) {
Expand All @@ -173,10 +173,10 @@ prompt_shell(shell_t *shell)
if (line = readline(_gbuf), !line) {
return 42;
}
garbage_backup_ptr((void*)line);
count_readline++;
}

garbage_collector(line, shell);
if (errno == EINTR) {
continue;
}
Expand All @@ -186,8 +186,8 @@ prompt_shell(shell_t *shell)
}
if (line && *line) {
add_history(line);
shell->get_line = line;
ENV_PATH = cut_path_env(shell, ENV_SET_ARRAY);
shell->line = line;
ENV_PATH = cut_path_env(ENV_SET_ARRAY);
save_history("history.txt");
} else if (line) {
print_str(detail, 0, RD_TTY, 1);
Expand Down
2 changes: 1 addition & 1 deletion src/prompt_function/prompt_engine/reset_var_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ reset_var_shell(shell_t *shell)
shell->binary = 0;
shell->built_shell_type_env = 0;
shell->built_exit = 0;
shell->get_line = NULL;
shell->line = NULL;
shell->len_arg = 0;
job_control = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/prompt_function/prompt_error/set_error_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ saving_error_file(shell_t *shell)
FILE* file = fopen(path, mode);
char log[256] = {0};

sprintf(log, "%s/%s", shell->get_line, ARCH);
sprintf(log, "%s/%s", shell->line, ARCH);
fprintf(file, "%s\n", log);
fclose(file);
}
36 changes: 4 additions & 32 deletions src/prompt_function/prompt_tools/cut_path_env.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,7 @@

#include "shell.h"

void free_wh_garbage(char **full_env)
{
for (uint32_t i = 0; full_env[i]; i++) {
free(full_env[i]);
full_env[i] = NULL;
}
free(full_env);

return;
}

void free_w_garbage(shell_t *shell, char **path)
{
for (uint32_t i = 0; path[i]; i++)
garbage_collector(path[i], shell);
garbage_collector(path, shell);
return;
}

void free_specific(shell_t *shell, char **full_env)
{
for (uint32_t i = 0; full_env[i]; i++) {
free_attribut(full_env[i], shell);
full_env[i] = NULL;
}
free_attribut(full_env, shell);
}

char **cut_path_env(shell_t *shell, char **array)
char **cut_path_env(char **array)
{
char **path = DEFAULT(path);
char **full_env = DEFAULT(full_env);
Expand All @@ -59,16 +31,16 @@ char **cut_path_env(shell_t *shell, char **array)
}

for (uint32_t i = 0; array[i] != NULL; i++) {
full_env = _str_to_word_array_custom(shell, array[i], '=');
full_env = _str_to_word_array_custom(array[i], '=');
if (_strcmp(full_env[0], "PATH") == 0) {
integer_path = 1;
break;
}
free_specific(shell, full_env);
// free_specific(shell, full_env);
}
if (integer_path == 0)
return full_env;
path = _str_to_word_array_custom(shell, full_env[1], ':');
path = _str_to_word_array_custom(full_env[1], ':');
full_env = NULL;
return path;
}
4 changes: 2 additions & 2 deletions src/prompt_function/prompt_tools/get_dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ get_dir(void)
if (getcwd(cwd, sizeof(cwd)) != NULL) {
replace_substr(cwd, get_home(), "~");
if (strlen(cwd) == 1)
return strdup(strcat(cwd, "/"));
return strdup(cwd);
return _strdup(strcat(cwd, "/"));
return _strdup(cwd);
} else {
return "UNDEFINE";
}
Expand Down
2 changes: 1 addition & 1 deletion src/prompt_function/prompt_tools/get_home.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ get_home(void)
struct passwd *pw = getpwuid(getuid());

if (pw != NULL && pw->pw_dir != NULL) {
return strdup(pw->pw_dir);
return _strdup(pw->pw_dir);
} else {
return NULL;
}
Expand Down
18 changes: 1 addition & 17 deletions src/prompt_function/prompt_tools/rm_space_bf_str.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,6 @@

#include "shell.h"

static char *
_str_dup(char const *src)
{
int32_t a = DEFAULT(a);
char *dest = NULL;

dest = (char*)malloc(sizeof(char) * (_strlen(src) + 1));
while (src[a] != '\0') {
dest[a] = src[a];
a++;
}
dest[a] = '\0';

return (dest);
}

char *
remove_space_before_string(const char *command)
{
Expand All @@ -45,5 +29,5 @@ remove_space_before_string(const char *command)
command++;
}

return _str_dup(command);
return _strdup(command);
}
4 changes: 2 additions & 2 deletions src/redirection/cut_command_for_dup_in.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ cut_into_command(char *command)
if (end == NULL) {
return NULL;
}
str = (char*)malloc(sizeof(char) *(end - start + 1));
str = (char*)_malloc(sizeof(char) *(end - start + 1));
strncpy(str, start, end - start);
str[end - start] = '\0';
ptr_command = strdup(str);
ptr_command = _strdup(str);
format_command(&ptr_command);

return ptr_command;
Expand Down
Loading

0 comments on commit 131911d

Please sign in to comment.