Skip to content

Commit

Permalink
norminette
Browse files Browse the repository at this point in the history
  • Loading branch information
emSali committed Aug 12, 2023
1 parent 5c78b97 commit 8da7074
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 59 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ http://brandonwamboldt.ca/how-linux-pipes-work-under-the-hood-1518/

# TODO

SEGFAULTS:
- [] Ctrl-D in Heredoc

- [] norminette
- [] export zz=hello=zz
- [] cat | cat | ls
Expand All @@ -26,8 +29,8 @@ http://brandonwamboldt.ca/how-linux-pipes-work-under-the-hood-1518/
- [X] export several objects
- [X] unset several objects
- [X] decrease exit nr with 255 (limit long max int)
- [] SEGFAULTS:
- [] Redirections
- [X] SEGFAULTS:
- [X] Redirections
- [X] 'cd'
- [X] cd '../../../../../.../../../../..'
- [X] 'export n' -> 'export n'
Expand Down
17 changes: 11 additions & 6 deletions commands/cd.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* cd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: simao <simao@student.42.fr> +#+ +:+ +#+ */
/* By: esali <esali@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/12 16:57:39 by simao #+# #+# */
/* Updated: 2023/08/10 22:32:27 by simao ### ########.fr */
/* Updated: 2023/08/12 16:48:50 by esali ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -17,6 +17,14 @@
// X_OK: Verifica se o arquivo pode ser executado.
// F_OK: Verifica se o arquivo existe.

void replace_pwd(char *oldpwd)
{
char cwd[1024];

replace_env_var("OLDPWD", oldpwd);
replace_env_var("PWD", getcwd(cwd, sizeof(cwd)));
}

void ft_chdir(char *parsed_path)
{
char cwd[1024];
Expand All @@ -42,10 +50,7 @@ void ft_chdir(char *parsed_path)
}
}
else
{
replace_env_var("OLDPWD", oldpwd);
replace_env_var("PWD", getcwd(cwd, sizeof(cwd)));
}
replace_pwd(oldpwd);
}

/*
Expand Down
4 changes: 2 additions & 2 deletions commands/exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: esali <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/12 16:57:31 by simao #+# #+# */
/* Updated: 2023/08/12 15:35:17 by esali ### ########.fr */
/* Updated: 2023/08/12 16:44:45 by esali ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -36,7 +36,7 @@ int calc_exit(int exit)
return (0);
else if (exit < 0)
{
while(exit < 0)
while (exit < 0)
exit = exit + 256;
}
else
Expand Down
4 changes: 2 additions & 2 deletions commands/unset.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: esali <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/12 16:57:50 by simao #+# #+# */
/* Updated: 2023/08/10 20:23:38 by esali ### ########.fr */
/* Updated: 2023/08/12 16:50:16 by esali ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -25,7 +25,7 @@ void cmd_unset(char **variable)
if (!variable[1])
return ;
i = 1;
while(variable[i])
while (variable[i])
{
lst = get_env();
while (lst->nxt != NULL)
Expand Down
38 changes: 22 additions & 16 deletions executor/heredoc.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* heredoc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: simao <simao@student.42.fr> +#+ +:+ +#+ */
/* By: esali <esali@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/21 18:41:23 by esali #+# #+# */
/* Updated: 2023/08/09 12:10:15 by simao ### ########.fr */
/* Updated: 2023/08/12 16:38:11 by esali ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -52,11 +52,29 @@ void exec_heredoc(t_list *cur)
heredoc_to_append(cur);
}

void write_heredoc(t_list *cur, char *new_line, int fd)
{
int len;

len = maxlen(new_line, cur->next->token[0]);
while (new_line && ft_strncmp(cur->next->token[0], new_line, len) != 0)
{
new_line = check_env(new_line);
write(fd, new_line, ft_strlen(new_line));
write(fd, "\n", 1);
free(new_line);
new_line = get_next_prompt();
if (new_line == NULL)
ft_printf("warning: here-doc delimiter missing\n");
len = maxlen(new_line, cur->next->token[0]);
}
free(new_line);
}

void heredoc(t_list *cur)
{
char *new_line;
t_heredoc *hdoc;
int len;

hdoc = get_hdoc();
hdoc->fd = open(cur->next->token[0], O_APPEND | O_RDWR | O_CREAT, 0644);
Expand All @@ -66,19 +84,7 @@ void heredoc(t_list *cur)
ft_printf("warning: here-doc delimiter missing\n");
return ;
}
if (ft_strlen(cur->next->token[0]) > ft_strlen(new_line))
len = ft_strlen(cur->next->token[0]);
else
len = ft_strlen(new_line);
while (ft_strncmp(cur->next->token[0], new_line, len) != 0)
{
new_line = check_env(new_line);
write(hdoc->fd, new_line, ft_strlen(new_line));
write(hdoc->fd, "\n", 1);
free(new_line);
new_line = get_next_prompt();
}
free(new_line);
write_heredoc(cur, new_line, hdoc->fd);
exec_heredoc(cur);
close(hdoc->fd);
}
4 changes: 1 addition & 3 deletions minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: esali <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/20 17:16:06 by esali #+# #+# */
/* Updated: 2023/08/12 16:02:35 by esali ### ########.fr */
/* Updated: 2023/08/12 16:49:55 by esali ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -185,7 +185,6 @@ void print_export_error(char *msg);
int maxlen(char *s1, char *s2);
void increase_shlvl(void);


/* File Handling */

void reset_original_std(void);
Expand All @@ -198,5 +197,4 @@ int its_a_pipe(t_list *node);
int its_append(t_list *node);
int its_heredoc(t_list *node);


#endif
5 changes: 2 additions & 3 deletions parse/scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* scanner.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: simao <simao@student.42.fr> +#+ +:+ +#+ */
/* By: esali <esali@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/13 15:37:09 by esali #+# #+# */
/* Updated: 2023/08/09 02:26:56 by simao ### ########.fr */
/* Updated: 2023/08/12 16:20:51 by esali ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -89,7 +89,6 @@ int parse(char *input)
i = i + len;
}
change_order();
//print_lists();
data = get_data();
data->exit = check_syntax(token, input);
free_keys(token);
Expand Down
15 changes: 14 additions & 1 deletion parse/syntax.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,25 @@
/* By: esali <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/20 18:04:06 by esali #+# #+# */
/* Updated: 2023/08/06 11:20:59 by esali ### ########.fr */
/* Updated: 2023/08/12 16:43:25 by esali ### ########.fr */
/* */
/* ************************************************************************** */

#include "../minishell.h"

int is_pipe(char *token)
{
int len;

if (ft_strlen(token) > 1)
len = ft_strlen(token);
else
len = 1;
if (ft_strncmp(token, "|", len) == 0)
return (1);
return (0);
}

// checks if token is redirection
int is_red(char *token)
{
Expand Down
5 changes: 3 additions & 2 deletions parse/var_expansion.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: esali <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/17 00:00:40 by esali #+# #+# */
/* Updated: 2023/08/07 15:43:56 by esali ### ########.fr */
/* Updated: 2023/08/12 16:20:35 by esali ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -105,13 +105,14 @@ char *change_env(char *input, int i)

/* checks if there is a valid char after $,
and changes counter to the position after var expansion */
// && str[c[0] + 1] != '"' -> 117)
char *manage_env(char *str, int *c)
{
int len_diff;
char *ret;

ret = ft_strdup(str);
if (str[c[0] + 1] && str[c[0] + 1] != ' ') // && str[c[0] + 1] != '"')
if (str[c[0] + 1] && str[c[0] + 1] != ' ')
{
len_diff = get_env_len_diff(ret, c[0]);
ret = change_env(ret, c[0]);
Expand Down
7 changes: 3 additions & 4 deletions utils/shlvl.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
/* By: esali <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/12 15:06:00 by esali #+# #+# */
/* Updated: 2023/08/12 16:06:05 by esali ### ########.fr */
/* Updated: 2023/08/12 16:40:56 by esali ### ########.fr */
/* */
/* ************************************************************************** */

#include "../minishell.h"


int is_digit(const char *s)
{
int i;
Expand All @@ -22,7 +21,7 @@ int is_digit(const char *s)
is_minus = 0;
if (s[i] == '-' || s[i] == '+')
{
if(s[i] == '-')
if (s[i] == '-')
is_minus = 1;
i++;
}
Expand All @@ -32,7 +31,7 @@ int is_digit(const char *s)
return (0);
i++;
}
if(is_minus)
if (is_minus)
return (2);
return (1);
}
Expand Down
20 changes: 2 additions & 18 deletions utils/symbols.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* symbols.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: simao <simao@student.42.fr> +#+ +:+ +#+ */
/* By: esali <esali@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/01 11:27:25 by smagalha #+# #+# */
/* Updated: 2023/08/09 12:25:29 by simao ### ########.fr */
/* Updated: 2023/08/12 16:43:06 by esali ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -30,22 +30,6 @@ int its_input(t_list *node)
return (0);
}

/*
- This is the function used in parsing. DIfferent from "its_a_pipe"
*/
int is_pipe(char *token)
{
int len;

if (ft_strlen(token) > 1)
len = ft_strlen(token);
else
len = 1;
if (ft_strncmp(token, "|", len) == 0)
return (1);
return (0);
}

int its_a_pipe(t_list *node)
{
if (!node || !node->token)
Expand Down

0 comments on commit 8da7074

Please sign in to comment.