Skip to content

Commit

Permalink
replaced hash table with linked list
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminHThomas committed Jul 15, 2024
1 parent 25f7d12 commit e05efe1
Show file tree
Hide file tree
Showing 15 changed files with 321 additions and 309 deletions.
54 changes: 0 additions & 54 deletions include/hash_table.h

This file was deleted.

4 changes: 3 additions & 1 deletion include/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: bthomas <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/27 19:30:56 by bthomas #+# #+# */
/* Updated: 2024/07/14 15:22:01 by bthomas ### ########.fr */
/* Updated: 2024/07/15 17:10:07 by bthomas ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -61,13 +61,15 @@ void free_lexmem(t_data *data);
void free_strarray(char **array);
char *get_env_var(char *var_str);
char *get_exec_path(char *input, size_t start_idx);
void expand_string_var(t_data *data, char *str);

/* utilities - bools */
bool is_builtin(char *input, size_t start_idx);
bool is_space(unsigned char c);
bool in(unsigned char c, const char *str);
bool empty_quote(char *input, size_t start_idx);
bool is_executable(char *input, size_t start_idx);
bool var_in_str(char *str);

/* input validation */
bool valid_input(char *input);
Expand Down
29 changes: 22 additions & 7 deletions include/minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: bthomas <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/25 13:21:34 by bthomas #+# #+# */
/* Updated: 2024/07/14 17:04:01 by bthomas ### ########.fr */
/* Updated: 2024/07/15 15:26:29 by bthomas ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -39,21 +39,28 @@
# include "parser.h"
# include "lexer.h"
# include "execution.h"
# include "hash_table.h"

typedef struct s_heredoc
{
int fd;
char path[22];
} t_heredoc;

typedef struct s_varlist
{
char *key;
char *val;
t_varlist *next;
t_varlist *prev;
} t_varlist;

typedef struct s_data
{
char *input;
char **env;
t_token *token;
t_hash_table *env_vars;
t_hash_table *local_vars;
char *input;
char **env;
t_token *token;
t_varlist *env_vars;
t_varlist *local_vars;
} t_data;

void handle_signals(void);
Expand All @@ -64,4 +71,12 @@ int clean_exit(t_data *data, int exit_code);
void sh_echo(t_token *token);
void sh_cd(t_token *token);

/* local & env vars */
t_varlist *get_varlist(char *key, char *val);
char *get_varval(t_varlist *vlist, char *key);
int add_var(t_varlist **vlist, char *key, char *val);
void del_varlist(t_varlist *head);
void del_varlist_node(t_varlist **head, t_varlist *node);
void del_varlist_key(t_varlist *vlist_head, char *key);

#endif
43 changes: 23 additions & 20 deletions src/builtins/export.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,47 @@
/* By: bthomas <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/12 20:32:33 by bthomas #+# #+# */
/* Updated: 2024/07/14 17:35:31 by bthomas ### ########.fr */
/* Updated: 2024/07/15 17:13:34 by bthomas ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

void ht_delete_key(t_hash_table *table, char *key)
{

}

/* I remember there was something to do with using env, will need to discuss */
static void print_env(t_data *data)
{
size_t i;
t_varlist *curr;

i = 0;
while (data->env && data->env[i])
curr = data->env_vars;
while (curr)
{
ft_printf("declare -x %s\n", data->env[i]);
i++;
if (curr->key)
ft_printf("declare -x %s=\"%s\"\n", curr->key, curr->val);
curr = curr->next;
}
}

/* if in local, move to env, if not in either, create in env */

/*
export var1=wow
or
$var1=wow
export $var1
*/
void export(t_data *data, t_token *token)
{
char *key;
char *val;

if (token->type == TK_BUILTIN)
token = token->next;
if (!token)
return (print_env(data));
if (token == TK_OPERATOR && token->lexstr[0] == '=')
token = token->next;
if (!token)
token = token->next;
if (token->lexstr[0] == '=')
{
printf("export: '=': not a valid identifier\n");
ft_printf("minishell: export: '%s': not a valid identifier\n");
return ;
}
if (token->lexstr[0] == '$')
{

}
}
34 changes: 34 additions & 0 deletions src/builtins/varlist_add.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* varlist_add.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bthomas <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/15 12:38:28 by bthomas #+# #+# */
/* Updated: 2024/07/15 15:16:08 by bthomas ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

int add_var(t_varlist **vlist, char *key, char *val)
{
t_varlist *node;
t_varlist *curr;

node = get_varlist(key, val);
if (!node)
return (1);
if (!*vlist)
{
*vlist = node;
return (0);
}
curr = *vlist;
while (curr->next)
curr = curr->next;
curr->next = node;
node->prev = curr;
return (0);
}
73 changes: 73 additions & 0 deletions src/builtins/varlist_del.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* varlist_del.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bthomas <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/15 12:38:41 by bthomas #+# #+# */
/* Updated: 2024/07/15 15:15:20 by bthomas ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

void del_varlist(t_varlist *head)
{
t_varlist *next;

if (!head)
return ;
while (head)
{
next = head->next;
if (head->key)
{
free(head->key);
head->key = NULL;
}
if (head->val)
{
free(head->val);
head->val = NULL;
}
free(head);
head = next;
}
}

void del_varlist_node(t_varlist **head, t_varlist *node)
{
if (!node || !head || !*head)
return ;
if (*head == node)
*head = node->next;
if (node->prev)
node->prev->next = node->next;
if (node->next)
node->next->prev = node->prev;
if (node->key)
{
free(node->key);
node->key = NULL;
}
if (node->val)
{
free(node->val);
node->val = NULL;
}
free(node);
node = NULL;
}

void del_varlist_key(t_varlist *vlist_head, char *key)
{
t_varlist *p;

p = vlist_head;
while (p && ft_strcmp(p->key, key) != 0)
p = p->next;
if (!p)
return ;
del_varlist_node(&vlist_head, p);
}
38 changes: 38 additions & 0 deletions src/builtins/varlist_utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* varlist_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bthomas <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/15 13:01:52 by bthomas #+# #+# */
/* Updated: 2024/07/15 15:24:41 by bthomas ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

t_varlist *get_varlist(char *key, char *val)
{
t_varlist *node;

node = malloc(sizeof(t_varlist));
if (!node)
return (1);
node->key = key;
node->val = val;
node->next = NULL;
node->prev = NULL;
return (node);
}

char *get_varval(t_varlist *vlist, char *key)
{
char *val;

while (vlist && ft_strcmp(key, vlist->key) != 0)
vlist = vlist->next;
if (!vlist)
return (NULL);
return (vlist->val);
}
2 changes: 1 addition & 1 deletion src/cleanup.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: bthomas <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/07 12:30:45 by bthomas #+# #+# */
/* Updated: 2024/07/14 17:03:42 by bthomas ### ########.fr */
/* Updated: 2024/07/15 16:59:17 by bthomas ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
Loading

0 comments on commit e05efe1

Please sign in to comment.