Skip to content

Commit

Permalink
Merge pull request #7 from SizzleUnrlsd/Garbage-Collector
Browse files Browse the repository at this point in the history
Overhaul of the garbage collector system + bug correction
  • Loading branch information
SizzleUnrlsd committed Aug 11, 2023
2 parents 60d3104 + deb0328 commit bc07475
Show file tree
Hide file tree
Showing 32 changed files with 146 additions and 549 deletions.
43 changes: 0 additions & 43 deletions include/garbage_collector.h

This file was deleted.

1 change: 0 additions & 1 deletion include/shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "ast.h"
#include "bultin_list.h"
#include "basic_function.h"
#include "garbage_collector.h"
#include "garbage-collector.h"
#include "bucket.h"
#include "alias.h"
Expand Down
2 changes: 0 additions & 2 deletions include/struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@

#ifndef STRUCT_H_
#define STRUCT_H_
#include "garbage_collector.h"
#include "bultin_global_env.h"
#include "history.h"
#include "alias.h"

typedef struct shell_s {
history_t *history;
set_env_t *set_env;
garbage_collector_t **garbage;
call_alias_t *call_alias;
char **local_env;
char **cmd;
Expand Down
16 changes: 7 additions & 9 deletions src/builtin/cd/cd.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@ set_new_old_pwd(shell_t *shell, char *path, char *mess_env)
char *final = DEFAULT(final);
char **diff = DEFAULT(diff);

free_attribut(shell->get_line, shell);
concat = concat_char_str(' ', mess_env, fmall, 1);
garbage_collector(concat, shell);
final = _strcat(concat, path);
final = concat_char_str('\n', final, 1, 1);
shell->get_line = final;
shell->line = final;

diff = _str_to_word_array_custom(shell, mess_env, ' ');
diff = _str_to_word_array_custom(mess_env, ' ');

if (pwd_already_ini_env(shell, diff[1]) == 1)
add_line_to_array(shell, 3, 0);
Expand All @@ -54,7 +52,7 @@ build_cd_foo_extend(shell_t *shell, char **arg, char *home, int32_t len_arg)
if ((home != NULL && len_arg == 1) ||
((_strcmp(arg[1], "~") == 0) && home != NULL)) {
shell->cd_old_path = getcwd(NULL, 4096);
garbage_collector(shell->cd_old_path, shell);
garbage_backup_bucket_ptr(shell->cd_old_path);
if (chdir(home) == -1) {
EXIT_W_ECHO_ERROR_("cd: Can't change to home directory.", 1);
}
Expand Down Expand Up @@ -101,14 +99,14 @@ build_cd_bar(shell_t *shell, char **arg)
EXIT_W_ECHO_ERROR_(": No such file or directory.", 1);
} else {
char *path = getcwd(NULL, 4096);
garbage_collector(path, shell);
garbage_backup_ptr(path);
set_new_old_pwd(shell, path, "setenv OLDPWD");
if (chdir(shell->cd_old_path) == -1) {
EXIT_W_ECHO_ERROR_("chdir: Error.", 1);
}
shell->cd_old_path = shell->cd_new_path;
shell->cd_new_path = getcwd(NULL, 4096);
garbage_collector(shell->cd_new_path, shell);
garbage_backup_bucket_ptr(shell->cd_new_path);
set_new_old_pwd(shell, shell->cd_new_path, "setenv PWD");
}
return 1;
Expand All @@ -125,11 +123,11 @@ build_cd_foobar(shell_t *shell, char **arg,

if (len_arg == 2 && check_perm_dir(shell, arg[1], invalid_cd) == 0) {
shell->cd_old_path = getcwd(NULL, 4096);
garbage_collector(shell->cd_old_path, shell);
garbage_backup_bucket_ptr(shell->cd_old_path);
set_new_old_pwd(shell, shell->cd_old_path, "setenv OLDPWD");
result = chdir(arg[1]);
shell->cd_new_path = getcwd(NULL, 4096);
garbage_collector(shell->cd_new_path, shell);
garbage_backup_bucket_ptr(shell->cd_new_path);
set_new_old_pwd(shell, shell->cd_new_path, "setenv PWD");
if (is_dir(arg[1]) == 1) {
print_str(arg[1], 0, true, 2);
Expand Down
28 changes: 4 additions & 24 deletions src/builtin/cd/find_home.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,11 @@
#include "shell.h"

static char *
_strdup_bool(char const *src, shell_t *shell, bool stat)
{
int32_t a = DEFAULT(a);
char *dest = DEFAULT(dest);

if (stat == true)
dest = (char*)malloc_attribut
(sizeof(char) * (_strlen(src) + 1), shell);
else
dest = malloc(sizeof(char) * (_strlen(src) + 1));

while (src[a] != '\0') {
dest[a] = src[a];
a++;
}
dest[a] = '\0';
return (dest);
}

static char *
second_chance(shell_t *shell)
second_chance(shell_t *shell UNUSED_ARG)
{
struct passwd *pw = getpwuid(getuid());
if (pw != NULL && pw->pw_dir != NULL) {
return _strdup_bool(pw->pw_dir, shell, true);
return _strdup(pw->pw_dir);
} else {
return NULL;
}
Expand All @@ -57,9 +37,9 @@ find_home(shell_t *shell, char *home)
char **env = shell->set_env->env_array;

for (int32_t i = 0; env[i]; i++) {
first_step = _str_to_word_array_custom(shell, env[i], '=');
first_step = _str_to_word_array_custom(env[i], '=');
if (_strcmp(first_step[0], "HOME") == 0) {
home = _strdup_bool(first_step[1], shell, true);
home = _strdup(first_step[1]);
return home;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/builtin/echo/echo.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ dollar_engine_echo(char **arg, int32_t i, shell_t *shell)
int32_t
_echo(shell_t *shell)
{
char **arg = parse_stdin(shell->get_line, shell);
char **arg = parse_stdin(shell->line);
int32_t len_arg = len_array(arg);

COMMAND_FOUND;
Expand Down
2 changes: 1 addition & 1 deletion src/builtin/history/exclamation_point.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ __attribute__((deprecated)) int32_t
builtin_exclamation(shell_t *shell)
{
int32_t future_shell_value = DEFAULT(future_shell_value);
char *cmd = strdup(shell->get_line);
char *cmd = strdup(shell->line);
COMMAND_FOUND;

if (cmd[0] == '!') {
Expand Down
9 changes: 4 additions & 5 deletions src/builtin/history/history.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "shell.h"

__attribute__((deprecated)) int32_t
display_history(shell_t *shell, char *str)
display_history(shell_t *shell UNUSED_ARG, char *str)
{
struct stat buff;
int32_t fd = open(str, O_RDONLY);
Expand All @@ -34,23 +34,22 @@ display_history(shell_t *shell, char *str)
}
stat(str, &buff);
size = buff.st_size;
temp = malloc_attribut(sizeof(char) * (size + 1), shell);
temp = _malloc(sizeof(char) * (size + 1));
if (read(fd, temp, size) == -1)
return 1;
if (write(1, temp, size) == -1) {
return !!_WRITE_ERROR;
}
temp[size] = '\0';
close(fd);
free_attribut(temp, shell);
return 0;
}

int32_t
builtin_history(shell_t *shell)
{
char *_default = "history";
char *cmd = shell->get_line + _strlen(_default) + 1;
char *cmd = shell->line + _strlen(_default) + 1;

COMMAND_FOUND;
using_history();
Expand All @@ -67,7 +66,7 @@ builtin_history(shell_t *shell)
write_history("history.shell");
return 0;
}
if (strcmp(shell->get_line, _default) == 0) {
if (strcmp(shell->line, _default) == 0) {
HIST_ENTRY** history_entries = history_list();
if (history_entries != NULL) {
for (uint64_t i = 0; history_entries[i] != NULL; i++) {
Expand Down
2 changes: 1 addition & 1 deletion src/builtin/history/init_history.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
__attribute__((deprecated)) void
init_history(shell_t *shell)
{
shell->history = malloc_attribut(sizeof(history_t), shell);
shell->history = _mallocbucket(sizeof(history_t));
shell->history->count_n = 1;
}
7 changes: 3 additions & 4 deletions src/builtin/history/write_history.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ void check_replace_in_file(int found, FILE *fd, int pos, shell_t *shell)
if (found) {
fseek(fd, pos, SEEK_SET);
dprintf(fileno(fd), "%d\t%d:%02d\t%s\n", shell->history->count_n,
shell->history->hour, shell->history->minute, shell->get_line);
shell->history->hour, shell->history->minute, shell->line);
} else
dprintf(fileno(fd), "%d\t%d:%02d\t%s\n", shell->history->count_n,
shell->history->hour, shell->history->minute, shell->get_line);
shell->history->hour, shell->history->minute, shell->line);
}

int replace_line_in_file(shell_t *shell, FILE *fd, int *pos, int *found)
Expand All @@ -47,7 +47,7 @@ int replace_line_in_file(shell_t *shell, FILE *fd, int *pos, int *found)
command = strtok(NULL, "\n");
if (command == NULL)
return 1;
if (strcmp(command, shell->get_line) == 0) {
if (strcmp(command, shell->line) == 0) {
*pos = ftell(fd) - shell->history->read;
*found = 1;
return 2;
Expand Down Expand Up @@ -80,6 +80,5 @@ int write_in_file(shell_t *shell)
check_replace_in_file(found, fd, pos, shell);
fclose(fd);
shell->history->count_n++;
free(shell->history->line);
return 0;
}
10 changes: 5 additions & 5 deletions src/builtin/primitive_builtin/builtin_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
int32_t
builtin_exit(shell_t *shell)
{
char **arg = parse_stdin(shell->get_line, shell);
char **arg = parse_stdin(shell->line);
int32_t len_arg = len_array(arg);

COMMAND_FOUND;
Expand All @@ -35,8 +35,8 @@ builtin_cd(shell_t *shell)
{
char *home = DEFAULT(home);
uint32_t invalid_cd = DEFAULT(invalid_cd);
char **arg = parse_stdin(shell->get_line, shell);
int32_t len_arg = len_array(parse_stdin(shell->get_line, shell));
char **arg = parse_stdin(shell->line);
int32_t len_arg = len_array(arg);

COMMAND_FOUND;
if (build_cd_foo(shell, home, arg, len_arg) == 1) {
Expand All @@ -59,7 +59,7 @@ int32_t
builtin_setenv(shell_t *shell)
{
int32_t in_env = DEFAULT(in_env), mode_builtin = 1;
char **arg = parse_stdin(shell->get_line, shell);
char **arg = parse_stdin(shell->line);
int32_t len_arg = len_array(arg);
COMMAND_FOUND;

Expand All @@ -84,7 +84,7 @@ int32_t
builtin_unsetenv(shell_t *shell)
{
int32_t count = DEFAULT(count), in_env = DEFAULT(in_env), mode_builtin = DEFAULT(mode_builtin);
char **arg = parse_stdin(shell->get_line, shell);
char **arg = parse_stdin(shell->line);
int32_t len_arg = len_array(arg);
int32_t len_env = len_array(shell->set_env->env_array);
shell->mode = DEFAULT(shell->mode);
Expand Down
35 changes: 18 additions & 17 deletions src/builtin/set_unset/check_setenv.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"
#include "../../../include/set_unset.h"
Expand All @@ -27,11 +29,10 @@ check_char_num(char c)
}

int32_t
check_malloc(char **new_env, char *n_key)
check_malloc(char **new_env, char *n_key UNUSED_ARG)
{
if (new_env == NULL) {
perror("Error allocating memory");
free(n_key);
return 84;
}
return 0;
Expand Down
10 changes: 3 additions & 7 deletions src/builtin/set_unset/my_set.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ is_input_valid_local(shell_t *shell)
void
init_local_env(shell_t *shell)
{
shell->local_env = malloc(sizeof(char *) * 2);
if (!shell->local_env) {
_p_error(_MEM_ALLOCA_ERROR);
}
garbage_collector(shell->local_env, shell);
shell->local_env = _mallocbucket(sizeof(char *) * 2);

shell->local_env[0] = NULL;
shell->local_env[1] = NULL;
Expand Down Expand Up @@ -78,7 +74,7 @@ my_set(shell_t *shell)
char *k_v = DEFAULT(k_v);

COMMAND_FOUND;
shell->cmd = parse_stdin(shell->get_line, shell);
shell->cmd = parse_stdin(shell->line);
if (shell->local_env == NULL) {
init_local_env(shell);
return 1;
Expand All @@ -94,7 +90,7 @@ my_set(shell_t *shell)
return 1;
len = my_envlen(shell->local_env);
k_v = my_strconcat(shell->cmd[1], my_strconcat("=", shell->cmd[2]));
shell->local_env = realloc(shell->local_env, sizeof(char *) * (len + 2));
shell->local_env = _realloc(shell->local_env, sizeof(char *) * (len + 2));
shell->local_env[len] = k_v;
shell->local_env[len + 1] = NULL; return 1;
}
Loading

0 comments on commit bc07475

Please sign in to comment.