Browse Source

fix: variables issues

master
Michael Ea 3 years ago
parent
commit
3c32405a13
  1. 7
      Makefile
  2. 6
      README.md
  3. 2
      caller.c
  4. 33
      env.c
  5. 53
      lexer.c
  6. 14
      minishell.h
  7. 0
      utils_1.c
  8. 2
      utils_2.c
  9. 76
      utils_3.c

7
Makefile

@ -1,6 +1,7 @@
NAME = minishell
NAME = minishell
LIBFT = libft.a
SRCS = minishell.c lexer.c parser.c caller.c built-in.c env.c utils.c utils_2.c
SRCS = minishell.c lexer.c parser.c caller.c built-in.c env.c
SRCS += utils_1.c utils_2.c utils_3.c
OBJS = ${SRCS:.c=.o}
@ -16,7 +17,7 @@ endif
$(NAME): $(LIBFT) $(OBJS)
gcc -g ${OBJS} ${LIB} -o ${NAME}
$(LIBFT):
$(LIBFT):
${MAKE} -C ./libft
cp ./libft/libft.a .

6
README.md

@ -9,6 +9,10 @@
-
## Issues :
- cat | ls ; cat | cat | ls ...
- syntax error for unclosed quotes
- stderror syntax
- export without arg -> env + declare.
- segfault when last cmd empty
- too many pipes is a problem
## Notes :

2
caller.c

@ -6,7 +6,7 @@
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/03 11:48:16 by narnaud #+# #+# */
/* Updated: 2022/05/12 11:34:26 by mea ### ########.fr */
/* Updated: 2022/05/12 13:36:30 by mea ### ########.fr */
/* */
/* ************************************************************************** */

33
env.c

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* env.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/16 09:41:29 by narnaud #+# #+# */
/* Updated: 2022/05/07 23:38:40 by narnaud@stude ### ########.fr */
/* Updated: 2022/05/12 14:32:41 by mea ### ########.fr */
/* */
/* ************************************************************************** */
@ -61,8 +61,6 @@ int ft_env(t_datas *datas, t_command *cmd)
return (0);
}
static int is_valid_identifier(char *ident);
int ft_export(t_datas *datas, t_command *cmd)
{
char **new;
@ -114,30 +112,3 @@ static void clean_env(t_datas *datas, int i)
i++;
}
}
static int is_valid_identifier(char *ident)
{
int i;
i = 0;
if (ft_isalpha(ident[0]) || ident[0] == '_')
{
while (ident[i])
{
if (!ft_isalpha(ident[i]) && !ft_isdigit(ident[i]) \
&& ident[i] != '_')
{
ft_putstr_fd("export: not an identifier: ", 2);
ft_putstr_fd(ident, 2);
ft_putchar_fd('\n', 2);
return (0);
}
i++;
}
return (1);
}
ft_putstr_fd("export: not an identifier: ", 2);
ft_putstr_fd(ident, 2);
ft_putchar_fd('\n', 2);
return (0);
}

53
lexer.c

@ -6,15 +6,13 @@
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 13:44:57 by narnaud #+# #+# */
/* Updated: 2022/05/12 13:07:38 by narnaud ### ########.fr */
/* Updated: 2022/05/12 14:53:06 by mea ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int create_wc(t_lexer *lex, char *tmp);
static int check_state(t_lexer *lex, char **line);
static int create_token(t_lexer *lex, char str[]);
static int check_register(t_lexer *lex, char **line, char *tmp);
t_token *lexer(t_datas *datas, char *line)
@ -32,8 +30,12 @@ t_token *lexer(t_datas *datas, char *line)
{
if (check_state(lex, &line))
continue ;
if (lex->state != S_QUOTE_ST && lex->deep == 0 && *line == '$')
if (lex->state != S_QUOTE_ST && lex->deep == 0 && *line == '$'
&& (line[1] != '\0' && !isspace(line[1])))
{
tmp_i = replace_var(datas, &line, tmp, tmp_i);
lex->empty = 1;
}
else if (check_register(lex, &line, tmp))
tmp_i = (ft_bzero(tmp, STR_MAX_SIZE), 0);
else
@ -80,7 +82,7 @@ static int check_state(t_lexer *lex, char **line)
return (0);
}
static int create_token(t_lexer *lex, char str[])
int create_token(t_lexer *lex, char str[])
{
t_token *tok;
t_token *tmp;
@ -166,44 +168,3 @@ static int check_register(t_lexer *lex, char **line, char *tmp)
}
return (0);
}
int create_wc(t_lexer *lex, char *tmp)
{
int i;
int j;
int skip ;
DIR * direct;
struct dirent *file;
direct = opendir(".");
file = readdir(direct);
file = readdir(direct);
file = readdir(direct);
while (file)
{
i = 0;
j = 0;
skip = (tmp[0] != '.' && file->d_name[0] == '.');
while (tmp[i] && !skip)
{
if (tmp[i] == '*')
{
while (file->d_name[j] && file->d_name[j] != tmp[i + 1])
j++;
}
if (tmp[i] != '*' && file->d_name[j] != tmp[i])
skip = 1;
else
{
i++;
j++;
}
}
if (!skip)
create_token(lex, file->d_name);
file = readdir(direct);
}
closedir(direct);
lex->wc = 0;
return (1);
}

14
minishell.h

@ -6,16 +6,16 @@
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 13:50:44 by narnaud #+# #+# */
/* Updated: 2022/05/12 11:33:39 by mea ### ########.fr */
/* Updated: 2022/05/12 14:52:06 by mea ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MINISHELL_H
# define MINISHELL_H
# define ENVP_MAX_SIZE 1024
# define STR_MAX_SIZE 1024
# define ENVP_MAX_SIZE 4096
# define STR_MAX_SIZE 4096
# define PATHS_MAX_SIZE 126
# define DEBUG 1
# define DEBUG 0
# include "libft/libft.h"
# include <fcntl.h>
@ -49,7 +49,7 @@ typedef struct s_datas
int silent;
} t_datas;
// ----------------------------------Utils.c
// ----------------------------------Utils_1.c
int is_empty(char *line);
int handle_status(t_datas *datas, int status);
void nothing(int sig_num);
@ -62,6 +62,9 @@ int file_error(char *cmd, char *file, char *msg);
int replace_var(t_datas *datas, char **line, char *tmp, int tmp_i);
char *expend_str(t_datas *datas, char *line);
int set_redir(t_lexer *lex, char **line, char ch);
// ----------------------------------Utils_3.c
int is_valid_identifier(char *ident);
int create_wc(t_lexer *lex, char *tmp);
// ----------------------------------Builtins.c
int ft_echo(t_command *cmd);
@ -123,5 +126,6 @@ typedef struct s_lex
} t_lexer;
t_token *lexer(t_datas *datas, char *line);
int create_token(t_lexer *lex, char str[]);
#endif

0
utils.c → utils_1.c

2
utils_2.c

@ -6,7 +6,7 @@
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/06 13:15:59 by narnaud #+# #+# */
/* Updated: 2022/05/10 10:16:58 by mea ### ########.fr */
/* Updated: 2022/05/12 14:29:56 by mea ### ########.fr */
/* */
/* ************************************************************************** */

76
utils_3.c

@ -0,0 +1,76 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils_3.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/12 14:33:58 by mea #+# #+# */
/* Updated: 2022/05/12 14:51:17 by mea ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int is_valid_identifier(char *ident)
{
int i;
i = 0;
if (ft_isalpha(ident[0]) || ident[0] == '_')
{
while (ident[i])
{
if (!ft_isalpha(ident[i]) && !ft_isdigit(ident[i]
&& ident[i] != '_'))
{
ft_putstr_fd("export: not an identifier: ", 2);
ft_putstr_fd(ident, 2);
ft_putchar_fd('\n', 2);
return (0);
}
i++;
}
return (1);
}
ft_putstr_fd("export: not an identifier: ", 2);
ft_putstr_fd(ident, 2);
ft_putchar_fd('\n', 2);
return (0);
}
int create_wc(t_lexer *lex, char *tmp)
{
int i;
int j;
int skip ;
DIR * direct;
struct dirent *file;
direct = opendir(".");
file = (readdir(direct), readdir(direct), readdir(direct));
while (file)
{
i = 0;
j = 0;
skip = (tmp[0] != '.' && file->d_name[0] == '.');
while (tmp[i] && !skip)
{
if (tmp[i] == '*')
while (file->d_name[j] && file->d_name[j] != tmp[i + 1])
j++;
if (tmp[i] != '*' && file->d_name[j] != tmp[i])
skip = 1;
else if (++i)
++j;
}
if (!skip)
create_token(lex, file->d_name);
file = readdir(direct);
}
closedir(direct);
lex->wc = 0;
return (1);
}
Loading…
Cancel
Save