Browse Source

-fix: unclosed pipes, uncorrect wildcards. -norminage

master
narnaud 3 years ago
parent
commit
d29c9a0319
  1. 26
      Makefile
  2. 2
      README.md
  3. 23
      built-in.c
  4. 120
      caller.c
  5. 3
      lexer.c
  6. 109
      lexer_2.c
  7. 58
      lexer_3.c
  8. 14
      libft/Makefile
  9. 7
      minishell.c
  10. 12
      minishell.h
  11. 6
      parser.c
  12. 7
      utils_1.c
  13. 99
      utils_2.c
  14. 76
      utils_3.c

26
Makefile

@ -1,7 +1,7 @@
NAME = minishell NAME = minishell
LIBFT = libft.a LIBFT = libft.a
SRCS = minishell.c lexer.c parser.c caller.c built-in.c env.c SRCS = minishell.c lexer.c lexer_2.c lexer_3.c parser.c caller.c
SRCS += utils_1.c utils_2.c utils_3.c SRCS += built-in.c env.c utils_1.c utils_2.c
OBJS = ${SRCS:.c=.o} OBJS = ${SRCS:.c=.o}
@ -15,24 +15,30 @@ ifeq ($(UNAME_S), Darwin)
endif endif
$(NAME): $(LIBFT) $(OBJS) $(NAME): $(LIBFT) $(OBJS)
gcc -g ${OBJS} ${LIB} -o ${NAME} @echo "Making Minishell."
@gcc -g ${OBJS} ${LIB} -o ${NAME}
@echo "Done."
$(LIBFT): $(LIBFT):
${MAKE} -C ./libft @${MAKE} -C ./libft
cp ./libft/libft.a . @cp ./libft/libft.a .
all: $(NAME) all: $(NAME)
%.o: %.c %.o: %.c
gcc -Werror -Wextra -Wall -g -c $< ${READLINE_INC} @gcc -Werror -Wextra -Wall -g -c $< ${READLINE_INC}
clean: clean:
rm -rf ${OBJS} @echo "Cleaning objects."
@rm -rf ${OBJS}
@echo "Done."
fclean: clean fclean: clean
${MAKE} -C ./libft fclean @${MAKE} -C ./libft fclean
rm -rf libft.a @rm -rf libft.a
rm -rf ${NAME} @echo "Cleaning binary."
@rm -rf ${NAME}
@echo "Done."
re: fclean all re: fclean all

2
README.md

@ -9,7 +9,7 @@
- -
## Issues : ## Issues :
- cat | ls ; cat | cat | ls ... - cat | ls ; cat | cat | ls ...
- syntax error for unclosed quotes - syntax error for unclosed quotes -> handled as closed at end of line;
- stderror syntax - stderror syntax
- export without arg -> env + declare. - export without arg -> env + declare.
- segfault when last cmd empty - segfault when last cmd empty

23
built-in.c

@ -6,12 +6,29 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/06 09:02:57 by narnaud #+# #+# */ /* Created: 2022/01/06 09:02:57 by narnaud #+# #+# */
/* Updated: 2022/05/11 01:27:56 by narnaud@stude ### ########.fr */ /* Updated: 2022/05/16 09:17:34 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int is_builtin(char *cmd)
{
int cmd_len;
cmd_len = ft_strlen(cmd) + 1;
if (!ft_strncmp(cmd, "echo", ft_min(cmd_len, 5))
|| !ft_strncmp(cmd, "pwd", ft_min(cmd_len, 4))
|| !ft_strncmp(cmd, "cd", ft_min(cmd_len, 3))
|| !ft_strncmp(cmd, "export", ft_min(cmd_len, 7))
|| !ft_strncmp(cmd, "unset", ft_min(cmd_len, 6))
|| !ft_strncmp(cmd, "env", ft_min(cmd_len, 4))
|| !ft_strncmp(cmd, "exit", ft_min(cmd_len, 5)))
return (1);
else
return (0);
}
int ft_echo(t_command *cmd) int ft_echo(t_command *cmd)
{ {
int no_nl; int no_nl;
@ -62,10 +79,10 @@ int ft_cd(t_command *cmd)
if (cmd->argv[1]) if (cmd->argv[1])
{ {
if (access(cmd->argv[1], X_OK)) if (access(cmd->argv[1], X_OK))
return (file_error(cmd->argv[0], cmd->argv[1], \ return (file_error(cmd->argv[0], cmd->argv[1],
"Permission denied\n")); "Permission denied\n"));
else if (access(cmd->argv[1], F_OK)) else if (access(cmd->argv[1], F_OK))
return (file_error(cmd->argv[0], cmd->argv[1], \ return (file_error(cmd->argv[0], cmd->argv[1],
"Not a directory\n")); "Not a directory\n"));
chdir(cmd->argv[1]); chdir(cmd->argv[1]);
return (1); return (1);

120
caller.c

@ -6,33 +6,16 @@
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */ /* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/03 11:48:16 by narnaud #+# #+# */ /* Created: 2022/05/03 11:48:16 by narnaud #+# #+# */
/* Updated: 2022/05/12 13:36:30 by mea ### ########.fr */ /* Updated: 2022/05/16 08:12:12 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
static void exe(t_datas *datas, t_command *cmd); static void call_binary(t_datas *datas, t_command *cmd);
static pid_t caller(t_datas *datas, t_command *cmd); static pid_t call(t_datas *datas, t_command *cmd);
int is_builtin(char *cmd) int call_builtin(t_datas *datas, t_command *cmd)
{
int cmd_len;
cmd_len = ft_strlen(cmd) + 1;
if (!ft_strncmp(cmd, "echo", ft_min(cmd_len, 5)) \
|| !ft_strncmp(cmd, "pwd", ft_min(cmd_len, 4)) \
|| !ft_strncmp(cmd, "cd", ft_min(cmd_len, 3)) \
|| !ft_strncmp(cmd, "export", ft_min(cmd_len, 7)) \
|| !ft_strncmp(cmd, "unset", ft_min(cmd_len, 6)) \
|| !ft_strncmp(cmd, "env", ft_min(cmd_len, 4)) \
|| !ft_strncmp(cmd, "exit", ft_min(cmd_len, 5)))
return (1);
else
return (0);
}
int builtin_call(t_datas *datas, t_command *cmd)
{ {
if (ft_strncmp(cmd->argv[0], "echo", 5) == 0) if (ft_strncmp(cmd->argv[0], "echo", 5) == 0)
return (ft_echo(cmd)); return (ft_echo(cmd));
@ -52,50 +35,7 @@ int builtin_call(t_datas *datas, t_command *cmd)
return (-1); return (-1);
} }
int check_skip(t_datas *datas, t_command *cmd) static void call_binary(t_datas *datas, t_command *cmd)
{
if (cmd->next && ((cmd->ope == AND && !datas->exit_code) \
|| (cmd->ope == OR && datas->exit_code)))
return (0);
return (1);
}
int piper(t_datas *datas, t_command *cmd)
{
int pip[2];
pid_t pid;
int status;
if (cmd->ope == PIPE)
{
pipe(pip);
if (!cmd->fd[1])
cmd->fd[1] = pip[1];
if (!cmd->next->fd[0])
cmd->next->fd[0] = pip[0];
}
pid = caller(datas, cmd);
if (cmd->fd[1])
close(cmd->fd[1]);
if (cmd->fd[0])
close(cmd->fd[0]);
if (DEBUG)
printf("%s -ope: %d, argc: %d, pid: %d, fdin: %d, fdout: %d\n", cmd->argv[0], cmd->ope, cmd->argc, pid, cmd->fd[0], cmd->fd[1]);
if (cmd->ope == PIPE)
pid = piper(datas, cmd->next);
waitpid(pid, &status, 0);
datas->exit_code = handle_status(datas, status);
if (cmd->ope != PIPE)
{
while (cmd->next && check_skip(datas, cmd))
cmd = cmd->next;
if (cmd->next)
piper(datas, cmd->next);
}
return (1);
}
static void exe(t_datas *datas, t_command *cmd)
{ {
char **path_dirs; char **path_dirs;
char *path; char *path;
@ -121,14 +61,14 @@ static void exe(t_datas *datas, t_command *cmd)
} }
} }
static pid_t caller(t_datas *datas, t_command *cmd) static pid_t call(t_datas *datas, t_command *cmd)
{ {
pid_t pid; pid_t pid;
pid = fork(); pid = fork();
if (!pid) if (!pid)
{ {
if (builtin_call(datas, cmd) == -1) if (call_builtin(datas, cmd) == -1)
{ {
if (cmd->fd[0]) if (cmd->fd[0])
{ {
@ -140,9 +80,53 @@ static pid_t caller(t_datas *datas, t_command *cmd)
dup2(cmd->fd[1], STDOUT_FILENO); dup2(cmd->fd[1], STDOUT_FILENO);
close(cmd->fd[1]); close(cmd->fd[1]);
} }
exe(datas, cmd); if (cmd->ope == PIPE)
close(cmd->next->fd[0]);
call_binary(datas, cmd);
} }
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
return (pid); return (pid);
} }
void create_pipe(t_command *cmd)
{
int pip[2];
if (cmd->ope != PIPE)
return ;
pipe(pip);
if (!cmd->fd[1])
cmd->fd[1] = pip[1];
else
close(pip[1]);
if (!cmd->next->fd[0])
cmd->next->fd[0] = pip[0];
else
close(pip[0]);
}
int caller(t_datas *datas, t_command *cmd)
{
int status;
create_pipe(cmd);
cmd->pid = call(datas, cmd);
if (cmd->fd[0])
close(cmd->fd[0]);
if (cmd->fd[1])
close(cmd->fd[1]);
if (cmd->ope == PIPE)
caller(datas, cmd->next);
waitpid(cmd->pid, &status, 0);
datas->exit_code = handle_status(datas, status);
if (cmd->ope != PIPE)
{
while (cmd->next && !((cmd->ope == AND && !datas->exit_code) \
|| (cmd->ope == OR && datas->exit_code)))
cmd = cmd->next;
if (cmd->next)
caller(datas, cmd->next);
}
return (1);
}

3
lexer.c

@ -6,13 +6,14 @@
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */ /* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 13:44:57 by narnaud #+# #+# */ /* Created: 2022/05/02 13:44:57 by narnaud #+# #+# */
/* Updated: 2022/05/12 14:53:06 by mea ### ########.fr */ /* Updated: 2022/05/16 08:15:13 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
static int check_state(t_lexer *lex, char **line); static int check_state(t_lexer *lex, char **line);
static int check_register(t_lexer *lex, char **line, char *tmp); static int check_register(t_lexer *lex, char **line, char *tmp);
t_token *lexer(t_datas *datas, char *line) t_token *lexer(t_datas *datas, char *line)

109
lexer_2.c

@ -0,0 +1,109 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lexer_2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/16 07:53:17 by narnaud #+# #+# */
/* Updated: 2022/05/16 09:20:23 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static char *get_var_value(t_datas *datas, char **line, int name_len);
int replace_var(t_datas *datas, char **line, char *tmp, int tmp_i)
{
int name_len;
int i;
char *value;
if ((*line)[1] == '?')
{
value = ft_itoa(datas->exit_code);
name_len = 2;
}
else
{
name_len = 1;
while (ft_isalpha((*line)[name_len]) \
|| ft_isdigit((*line)[name_len]) || (*line)[name_len] == '_')
name_len++;
value = get_var_value(datas, line, name_len);
}
i = 0;
while (value[i])
tmp[tmp_i++] = value[i++];
tmp[tmp_i] = 0;
*line += name_len;
free(value);
return (tmp_i);
}
char *expend_str(t_datas *datas, char *line)
{
char *tmp;
int tmp_i;
tmp = ft_calloc(STR_MAX_SIZE, sizeof(*tmp));
if (!tmp)
free(tmp);
tmp_i = 0;
while (*line)
{
if (*line == '$')
tmp_i = replace_var(datas, &line, tmp, tmp_i);
else
tmp[tmp_i++] = *(line++);
}
return (tmp);
}
static char *get_var_value(t_datas *datas, char **line, int name_len)
{
int i;
char **env;
char *name;
char *value;
name = ft_substr(*line, 1, name_len - 1);
i = 0;
value = NULL;
while (datas->envp[i] && !value)
{
env = ft_split(datas->envp[i], '=');
if (!ft_strncmp(name, env[0], name_len + 1))
value = (free(env[0]), env[1]);
else if (++i)
ft_free_split(env);
}
free(name);
if (value)
free(env);
else
value = strdup("");
return (value);
}
int set_redir(t_lexer *lex, char **line, char ch)
{
static t_type type_out[2] = {OUT, ADD};
static t_type type_in[2] = {IN, HD};
t_type *type;
if (ch == '>')
type = type_out;
else
type = type_in;
if (**line == ch && (*line)++)
{
if (**line == ch && (*line)++)
lex->next_type = type[1];
else
lex->next_type = type[0];
return (1);
}
return (0);
}

58
lexer_3.c

@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lexer_3.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/16 09:18:30 by narnaud #+# #+# */
/* Updated: 2022/05/16 09:56:05 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int wc_str_cmp(char *wc, char *str)
{
int i;
int j;
int skip ;
i = 0;
j = 0;
skip = (wc[0] != '.' && str[0] == '.');
while (wc[i] && !skip)
{
if (wc[i] == '*' && ++i)
while (str[j] && str[j] != wc[i])
j++;
else if (str[j] != wc[i])
skip = 1;
else
{
i++;
j++;
}
}
if (str[j] != wc[i])
skip = 1;
return (skip);
}
int create_wc(t_lexer *lex, char *wc)
{
DIR *direct;
struct dirent *file;
direct = opendir(".");
file = (readdir(direct), readdir(direct), readdir(direct));
while (file)
{
if (!wc_str_cmp(wc, file->d_name))
create_token(lex, file->d_name);
file = readdir(direct);
}
closedir(direct);
lex->wc = 0;
return (1);
}

14
libft/Makefile

@ -37,19 +37,23 @@ RM = rm -rf
CFLAGS = -Wall -Wextra -Werror CFLAGS = -Wall -Wextra -Werror
.c.o: .c.o:
${CC} ${CFLAGS} -c $< -o ${<:.c=.o} @${CC} ${CFLAGS} -c $< -o ${<:.c=.o}
all : $(NAME) all : $(NAME)
$(NAME): $(OBJS) $(NAME): $(OBJS)
${AR} ${NAME} ${OBJS} @echo "Making libft."
@${AR} ${NAME} ${OBJS}
@echo "Done."
clean: clean:
${RM} ${OBJS} @echo "Cleaning libft."
${RM} ${OBJS_BONUS} @${RM} ${OBJS}
@${RM} ${OBJS_BONUS}
@echo "Done."
fclean: clean fclean: clean
${RM} ${NAME} @${RM} ${NAME}
re: fclean all re: fclean all

7
minishell.c

@ -6,7 +6,7 @@
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */ /* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 12:14:09 by narnaud #+# #+# */ /* Created: 2022/05/02 12:14:09 by narnaud #+# #+# */
/* Updated: 2022/05/11 00:45:01 by narnaud@stude ### ########.fr */ /* Updated: 2022/05/16 08:16:48 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -31,7 +31,6 @@ void termios(int ctl)
signal(SIGINT, sigs_handler); signal(SIGINT, sigs_handler);
signal(SIGQUIT, sigs_handler); signal(SIGQUIT, sigs_handler);
} }
termios_p.c_cc[VQUIT] = ctl * 28;
tcsetattr(tty, TCSANOW, &termios_p); tcsetattr(tty, TCSANOW, &termios_p);
} }
@ -66,9 +65,9 @@ void prompt(t_datas *datas, char *line)
return ; return ;
cmd = parser(datas, lexer(datas, line), NULL); cmd = parser(datas, lexer(datas, line), NULL);
if (!cmd->prev && !cmd->next && is_builtin(cmd->argv[0])) if (!cmd->prev && !cmd->next && is_builtin(cmd->argv[0]))
builtin_call(datas, cmd); call_builtin(datas, cmd);
else else
piper(datas, cmd); caller(datas, cmd);
free_cmd(cmd); free_cmd(cmd);
add_history(line); add_history(line);
} }

12
minishell.h

@ -6,7 +6,7 @@
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */ /* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 13:50:44 by narnaud #+# #+# */ /* Created: 2022/05/02 13:50:44 by narnaud #+# #+# */
/* Updated: 2022/05/12 14:52:06 by mea ### ########.fr */ /* Updated: 2022/05/16 09:22:48 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,7 +15,7 @@
# define ENVP_MAX_SIZE 4096 # define ENVP_MAX_SIZE 4096
# define STR_MAX_SIZE 4096 # define STR_MAX_SIZE 4096
# define PATHS_MAX_SIZE 126 # define PATHS_MAX_SIZE 126
# define DEBUG 0 # define DEBUG 1
# include "libft/libft.h" # include "libft/libft.h"
# include <fcntl.h> # include <fcntl.h>
@ -34,6 +34,7 @@
typedef struct s_command typedef struct s_command
{ {
pid_t pid;
char **argv; char **argv;
int argc; int argc;
int ope; int ope;
@ -67,6 +68,7 @@ int is_valid_identifier(char *ident);
int create_wc(t_lexer *lex, char *tmp); int create_wc(t_lexer *lex, char *tmp);
// ----------------------------------Builtins.c // ----------------------------------Builtins.c
int is_builtin(char *cmd);
int ft_echo(t_command *cmd); int ft_echo(t_command *cmd);
int ft_pwd(t_command *cmd); int ft_pwd(t_command *cmd);
int ft_cd(t_command *cmd); int ft_cd(t_command *cmd);
@ -78,9 +80,8 @@ int ft_env(t_datas *datas, t_command *cmd);
int ft_export(t_datas *datas, t_command *cmd); int ft_export(t_datas *datas, t_command *cmd);
// ----------------------------------Caller.c // ----------------------------------Caller.c
int is_builtin(char *cmd); int call_builtin(t_datas *datas, t_command *cmd);
int builtin_call(t_datas *datas, t_command *cmd); int caller(t_datas *datas, t_command *cmd);
int piper(t_datas *datas, t_command *cmd);
// ----------------------------------Parser.c // ----------------------------------Parser.c
typedef enum e_type typedef enum e_type
@ -128,4 +129,5 @@ typedef struct s_lex
t_token *lexer(t_datas *datas, char *line); t_token *lexer(t_datas *datas, char *line);
int create_token(t_lexer *lex, char str[]); int create_token(t_lexer *lex, char str[]);
int create_wc(t_lexer *lex, char *wc);
#endif #endif

6
parser.c

@ -6,7 +6,7 @@
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */ /* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 16:09:25 by narnaud #+# #+# */ /* Created: 2022/05/02 16:09:25 by narnaud #+# #+# */
/* Updated: 2022/05/11 13:53:47 by narnaud@stude ### ########.fr */ /* Updated: 2022/05/16 08:17:31 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -30,6 +30,7 @@ t_command *parser(t_datas *datas, t_token *tok, t_command *prev)
tok = parse_cmd(datas, tok, cmd); tok = parse_cmd(datas, tok, cmd);
if (tok) if (tok)
{ {
if (tok->next)
cmd->ope = tok->type; cmd->ope = tok->type;
cmd->next = parser(datas, tok->next, cmd); cmd->next = parser(datas, tok->next, cmd);
free(tok->value); free(tok->value);
@ -63,7 +64,8 @@ static t_token *parse_cmd(t_datas *datas, t_token *tok, t_command *cmd)
if (tok->value[0] == '(') if (tok->value[0] == '(')
{ {
cmd->argv[cmd->argc++] = ft_strdup("./minishell"); cmd->argv[cmd->argc++] = ft_strdup("./minishell");
cmd->argv[cmd->argc++] = ft_substr(tok->value, 1, ft_strlen(tok->value) - 2); cmd->argv[cmd->argc++] = ft_substr(tok->value,
1, ft_strlen(tok->value) - 2);
} }
else if (tok->type) else if (tok->type)
update_redir(datas, cmd, tok); update_redir(datas, cmd, tok);

7
utils_1.c

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* utils.c :+: :+: :+: */ /* utils_1.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */ /* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/03 08:57:53 by narnaud #+# #+# */ /* Created: 2022/05/03 08:57:53 by narnaud #+# #+# */
/* Updated: 2022/05/12 08:45:47 by narnaud ### ########.fr */ /* Updated: 2022/05/16 08:18:23 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,6 +26,7 @@ int is_empty(char *line)
int handle_status(t_datas *datas, int status) int handle_status(t_datas *datas, int status)
{ {
int ret; int ret;
if (WIFSIGNALED(status)) if (WIFSIGNALED(status))
{ {
ret = 128 + WTERMSIG(status); ret = 128 + WTERMSIG(status);
@ -34,7 +35,7 @@ int handle_status(t_datas *datas, int status)
} }
else else
ret = WEXITSTATUS(status); ret = WEXITSTATUS(status);
return ret; return (ret);
} }
void nothing(int sig_num) void nothing(int sig_num)

99
utils_2.c

@ -6,7 +6,7 @@
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */ /* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/06 13:15:59 by narnaud #+# #+# */ /* Created: 2022/05/06 13:15:59 by narnaud #+# #+# */
/* Updated: 2022/05/12 14:29:56 by mea ### ########.fr */ /* Updated: 2022/05/16 08:19:28 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -24,98 +24,29 @@ int file_error(char *cmd, char *file, char *msg)
return (0); return (0);
} }
static char *get_var_value(t_datas *datas, char **line, int name_len); int is_valid_identifier(char *ident)
int replace_var(t_datas *datas, char **line, char *tmp, int tmp_i)
{ {
int name_len;
int i; int i;
char *value;
if ((*line)[1] == '?') i = 0;
if (ft_isalpha(ident[0]) || ident[0] == '_')
{ {
value = ft_itoa(datas->exit_code); while (ident[i])
name_len = 2;
}
else
{ {
name_len = 1; if (!ft_isalpha(ident[i]) && !ft_isdigit(ident[i]
while (ft_isalpha((*line)[name_len]) \ && ident[i] != '_'))
|| ft_isdigit((*line)[name_len]) || (*line)[name_len] == '_')
name_len++;
value = get_var_value(datas, line, name_len);
}
i = 0;
while (value[i])
tmp[tmp_i++] = value[i++];
tmp[tmp_i] = 0;
*line += name_len;
free(value);
return (tmp_i);
}
char *expend_str(t_datas *datas, char *line)
{
char *tmp;
int tmp_i;
tmp = ft_calloc(STR_MAX_SIZE, sizeof(*tmp));
if (!tmp)
free(tmp);
tmp_i = 0;
while (*line)
{ {
if (*line == '$') ft_putstr_fd("export: not an identifier: ", 2);
tmp_i = replace_var(datas, &line, tmp, tmp_i); ft_putstr_fd(ident, 2);
else ft_putchar_fd('\n', 2);
tmp[tmp_i++] = *(line++); return (0);
} }
return (tmp); i++;
}
static char *get_var_value(t_datas *datas, char **line, int name_len)
{
int i;
char **env;
char *name;
char *value;
name = ft_substr(*line, 1, name_len - 1);
i = 0;
value = NULL;
while (datas->envp[i] && !value)
{
env = ft_split(datas->envp[i], '=');
if (!ft_strncmp(name, env[0], name_len + 1))
value = (free(env[0]), env[1]);
else if (++i)
ft_free_split(env);
} }
free(name);
if (value)
free(env);
else
value = strdup("");
return (value);
}
int set_redir(t_lexer *lex, char **line, char ch)
{
static t_type type_out[2] = {OUT, ADD};
static t_type type_in[2] = {IN, HD};
t_type *type;
if (ch == '>')
type = type_out;
else
type = type_in;
if (**line == ch && (*line)++)
{
if (**line == ch && (*line)++)
lex->next_type = type[1];
else
lex->next_type = type[0];
return (1); return (1);
} }
ft_putstr_fd("export: not an identifier: ", 2);
ft_putstr_fd(ident, 2);
ft_putchar_fd('\n', 2);
return (0); return (0);
} }

76
utils_3.c

@ -1,76 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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