set_cmd + ecex
This commit is contained in:
parent
0d864ea39b
commit
35f6c8bbaa
5
Makefile
5
Makefile
@ -6,13 +6,14 @@
|
|||||||
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
|
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2022/03/06 12:50:24 by apommier #+# #+# #
|
# Created: 2022/03/06 12:50:24 by apommier #+# #+# #
|
||||||
# Updated: 2022/03/08 15:20:19 by apommier ### ########.fr #
|
# Updated: 2022/03/08 17:30:48 by apommier ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
|
|
||||||
NAME = minishell
|
NAME = minishell
|
||||||
SRCS = main.c\
|
SRCS = main.c\
|
||||||
|
pipe.c\
|
||||||
|
pipex_utils.c\
|
||||||
set_cmd.c
|
set_cmd.c
|
||||||
OBJS = ${SRCS:.c=.o}
|
OBJS = ${SRCS:.c=.o}
|
||||||
CC = clang
|
CC = clang
|
||||||
|
|||||||
8
main.c
8
main.c
@ -6,7 +6,7 @@
|
|||||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/03/06 13:27:11 by apommier #+# #+# */
|
/* Created: 2022/03/06 13:27:11 by apommier #+# #+# */
|
||||||
/* Updated: 2022/03/08 15:20:48 by apommier ### ########.fr */
|
/* Updated: 2022/03/08 17:28:41 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -15,15 +15,15 @@
|
|||||||
int main(int ac, char **av, char **path)
|
int main(int ac, char **av, char **path)
|
||||||
{
|
{
|
||||||
char *input;
|
char *input;
|
||||||
|
t_cmd *cmd;
|
||||||
|
|
||||||
printf("---MINISHELL START---\n");
|
printf("---MINISHELL START---\n");
|
||||||
|
|
||||||
execute(t_cmd *cmd, *infile, *outfile);
|
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
input = readline("$~ ");
|
input = readline("$~ ");
|
||||||
add_history(input);
|
add_history(input);
|
||||||
set_cmd(input);
|
cmd = set_cmd(input, path);
|
||||||
|
execute(cmd);
|
||||||
}
|
}
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|||||||
16
minishell.h
16
minishell.h
@ -21,8 +21,8 @@
|
|||||||
# include <readline/history.h>
|
# include <readline/history.h>
|
||||||
# include <sys/types.h>
|
# include <sys/types.h>
|
||||||
# include <sys/stat.h>
|
# include <sys/stat.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
# include <fcntl.h>
|
# include <fcntl.h>
|
||||||
# include <sys/types.h>
|
|
||||||
# include <stdio.h>
|
# include <stdio.h>
|
||||||
# include <errno.h>
|
# include <errno.h>
|
||||||
|
|
||||||
@ -42,10 +42,11 @@ typedef struct s_simple {
|
|||||||
typedef struct s_command {
|
typedef struct s_command {
|
||||||
int nb_s_cmd;
|
int nb_s_cmd;
|
||||||
struct s_simple **s_cmds;
|
struct s_simple **s_cmds;
|
||||||
char *out_file;
|
char *outfile;
|
||||||
char *input_file;
|
char *infile;
|
||||||
char *err_file;
|
char *err_file;
|
||||||
struct s_simple *current_s_cmd;
|
struct s_simple *current_s_cmd;
|
||||||
|
char **path;
|
||||||
} t_cmd;
|
} t_cmd;
|
||||||
|
|
||||||
//main.c
|
//main.c
|
||||||
@ -54,7 +55,12 @@ int main();//int ac, char **av, char **path);
|
|||||||
//pipe.c
|
//pipe.c
|
||||||
void execute(t_cmd *cmd);
|
void execute(t_cmd *cmd);
|
||||||
|
|
||||||
//set_command
|
//set_cmd.c
|
||||||
t_cmd *set_cmd(char *input);
|
t_cmd *set_cmd(char *input, char **path);
|
||||||
|
|
||||||
|
//pipex_utils.c
|
||||||
|
char **get_path(char **env);
|
||||||
|
char *get_command(char **exec, char **env);
|
||||||
|
void print_double(char **tab);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
60
pipe.c
60
pipe.c
@ -6,7 +6,7 @@
|
|||||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/03/07 11:13:32 by apommier #+# #+# */
|
/* Created: 2022/03/07 11:13:32 by apommier #+# #+# */
|
||||||
/* Updated: 2022/03/08 14:53:42 by apommier ### ########.fr */
|
/* Updated: 2022/03/08 17:16:44 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -24,72 +24,78 @@ void execute(t_cmd *cmd)
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
|
printf("in execute\n");
|
||||||
//set the initial input
|
//set the initial input
|
||||||
if (current_s_cmd->cmd->infile)
|
if (cmd->current_s_cmd->infile)
|
||||||
fdin = open(current_s_cmd->cmd->infile, O_READ);
|
{
|
||||||
|
fdin = open(cmd->current_s_cmd->infile, O_RDONLY);
|
||||||
|
printf("good chosse -redirect infile- fdin= %d\n", fdin);
|
||||||
|
}
|
||||||
else if (cmd->infile)
|
else if (cmd->infile)
|
||||||
fdin = open(cmd->infile,O_READ);
|
{
|
||||||
|
fdin = open(cmd->infile, O_RDONLY);
|
||||||
|
printf("good chosse -infile- fdin= %d\n", fdin);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
fdin=dup(tmpin);
|
fdin=dup(tmpin);
|
||||||
|
printf("good chosse -standar in- fdin= %d\n", fdin);
|
||||||
|
}
|
||||||
while( i < cmd->nb_s_cmd)
|
while( i < cmd->nb_s_cmd)
|
||||||
{
|
{
|
||||||
if (i != 0 && current_s_cmd->cmd->infile)
|
printf("enter while\n");
|
||||||
fdin = open(current_s_cmd->cmd->infile,O_READ);
|
if (i != 0 && cmd->current_s_cmd->infile)
|
||||||
|
fdin = open(cmd->current_s_cmd->infile, O_RDONLY);
|
||||||
//redirect input
|
//redirect input
|
||||||
dup2(fdin, 0);
|
dup2(fdin, 0);
|
||||||
close(fdin);
|
close(fdin);
|
||||||
//setup output
|
//setup output
|
||||||
if (i == cmd->nb_s_cmd - 1)
|
if (i == cmd->nb_s_cmd - 1)
|
||||||
{
|
{
|
||||||
|
printf("if last cmd\n");
|
||||||
// Last simple command
|
// Last simple command
|
||||||
if (current_s_cmd->cmd->outfile)
|
if (cmd->current_s_cmd->outfile)
|
||||||
fdout = open(current_s_cmd->cmd->outfile, ……);
|
fdout = open(cmd->current_s_cmd->outfile, O_RDWR | O_CREAT | O_TRUNC, 0666);
|
||||||
else if(cmd->outfile)
|
else if(cmd->outfile)
|
||||||
fdout=open(cmd->outfile, ……);
|
fdout=open(cmd->outfile, O_RDWR | O_CREAT | O_TRUNC, 0666);
|
||||||
else// Use default output
|
else// Use default output
|
||||||
fdout=dup(tmpout);
|
fdout=dup(tmpout);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Not last
|
//not last
|
||||||
//simple command
|
//simple command
|
||||||
//create pipe
|
//create pipe
|
||||||
//int fdpipe[2];
|
//int fdpipe[2];
|
||||||
pipe(fdpipe);
|
pipe(fdpipe);
|
||||||
fdout=fdpipe[1];
|
fdout=fdpipe[1];
|
||||||
fdin=fdpipe[0];
|
fdin=fdpipe[0];
|
||||||
}// if/else
|
}
|
||||||
|
printf("select if last or not done \n");
|
||||||
// Redirect output
|
// Redirect output
|
||||||
dup2(fdout,1);
|
dup2(fdout,1);
|
||||||
close(fdout);
|
close(fdout);
|
||||||
|
|
||||||
// Create child process
|
// Create child process
|
||||||
ret=fork();
|
ret=fork();
|
||||||
|
int return_exec;
|
||||||
if(ret==0)
|
if(ret==0)
|
||||||
{
|
{
|
||||||
execvp(scmd[i].args[0], scmd[i].args);
|
return_exec = execvp(cmd->current_s_cmd->cmd, cmd->current_s_cmd->args);
|
||||||
perror(“execvpâ€);
|
printf("exec done ret exec= %d\n", return_exec);
|
||||||
_exit(1);
|
_exit(1);
|
||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
|
printf("good i = %d\n", i);
|
||||||
} //while
|
} //while
|
||||||
|
printf("quit while\n");
|
||||||
//restore in/out defaults
|
//restore in/out defaults
|
||||||
dup2(tmpin,0);
|
dup2(tmpin,0);
|
||||||
dup2(tmpout,1);
|
dup2(tmpout,1);
|
||||||
close(tmpin);
|
close(tmpin);
|
||||||
close(tmpout);
|
close(tmpout);
|
||||||
|
|
||||||
if (!background)
|
// Wait for last command
|
||||||
{
|
waitpid(ret, NULL, 0);
|
||||||
// Wait for last command
|
|
||||||
waitpid(ret, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // execute
|
} // execute
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
|
||||||
{
|
|
||||||
execute(t_cmd *cmd, char *infile, char *outfile);
|
|
||||||
}
|
|
||||||
119
pipex_utils.c
Normal file
119
pipex_utils.c
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* pipex_utils.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/03/08 16:58:40 by apommier #+# #+# */
|
||||||
|
/* Updated: 2022/03/08 18:02:15 by apommier ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
void free_double(char **tab)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
if (tab)
|
||||||
|
{
|
||||||
|
while (tab[i])
|
||||||
|
free(tab[i++]);
|
||||||
|
free(tab);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_double(char **tab)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
if (tab)
|
||||||
|
{
|
||||||
|
while (tab[i])
|
||||||
|
{
|
||||||
|
printf("%d -%s-\n", i, tab[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
printf("end double\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
char **get_path(char **env)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
char **line;
|
||||||
|
char *swap;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
line = 0;
|
||||||
|
swap = 0;
|
||||||
|
while (env[i++] && env[i])
|
||||||
|
{
|
||||||
|
if (!ft_strncmp(env[i], "PATH=", 5))
|
||||||
|
{
|
||||||
|
swap = ft_substr(env[i], 5, ft_strlen(env[i]));
|
||||||
|
if (!swap)
|
||||||
|
exit(1);
|
||||||
|
line = ft_split(swap, ':');
|
||||||
|
if (!line)
|
||||||
|
exit(1);
|
||||||
|
free(swap);
|
||||||
|
return (line);
|
||||||
|
}
|
||||||
|
free(line);
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//verify access with cmd access
|
||||||
|
char *does_access(char **path, char **exec)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
char *cmd;
|
||||||
|
char *swap;
|
||||||
|
|
||||||
|
swap = 0;
|
||||||
|
cmd = 0;
|
||||||
|
i = 0;
|
||||||
|
if (exec[0][0] != '/')
|
||||||
|
swap = ft_strjoin(path[i], "/");
|
||||||
|
while (access(swap, F_OK) && path[i++])
|
||||||
|
{
|
||||||
|
free(swap);
|
||||||
|
swap = ft_strjoin(path[i], "/");
|
||||||
|
cmd = swap;
|
||||||
|
swap = ft_strjoin(swap, exec[0]);
|
||||||
|
free(cmd);
|
||||||
|
}
|
||||||
|
if (path[i])
|
||||||
|
return (swap);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *get_command(char **exec, char **path)
|
||||||
|
{
|
||||||
|
char *swap;
|
||||||
|
|
||||||
|
swap = 0;
|
||||||
|
printf("in get command exec=\n");
|
||||||
|
print_double(exec);
|
||||||
|
if (exec[0][0] == '/' && !access(exec[0], F_OK))
|
||||||
|
{
|
||||||
|
printf("first\n");
|
||||||
|
free_double(path);
|
||||||
|
return (exec[0]);
|
||||||
|
}
|
||||||
|
else if (exec[0][0] == '/')
|
||||||
|
{
|
||||||
|
printf("BAD PATH FOR CMD\n");
|
||||||
|
free_double(path);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
swap = does_access(path, exec);
|
||||||
|
printf("swap bfore ret= %s\n", swap);
|
||||||
|
return (swap);
|
||||||
|
}
|
||||||
39
set_cmd.c
39
set_cmd.c
@ -6,10 +6,12 @@
|
|||||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/03/08 15:19:42 by apommier #+# #+# */
|
/* Created: 2022/03/08 15:19:42 by apommier #+# #+# */
|
||||||
/* Updated: 2022/03/08 15:19:47 by apommier ### ########.fr */
|
/* Updated: 2022/03/08 18:04:06 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
int double_size(char **tab)
|
int double_size(char **tab)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
@ -20,16 +22,28 @@ int double_size(char **tab)
|
|||||||
return (i);
|
return (i);
|
||||||
}
|
}
|
||||||
|
|
||||||
s_cmds *set_s_cmd(char *line)
|
t_s_cmd *set_s_cmd(char *line, t_cmd *cmd)
|
||||||
{
|
{
|
||||||
t_s_cmd s_cmd;
|
t_s_cmd *s_cmd;
|
||||||
tmp **split_line;
|
char **split_line;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
split_line = ft_split(line, ' ');
|
split_line = ft_split(line, ' ');
|
||||||
s_cmd.cmd = split_line[i];
|
s_cmd = malloc(sizeof(t_s_cmd));
|
||||||
|
s_cmd->cmd = get_command(split_line, cmd->path);
|
||||||
|
printf("scmd= %s\n", s_cmd->cmd);
|
||||||
|
if (!s_cmd->cmd)
|
||||||
|
{
|
||||||
|
printf("get command crash");
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
s_cmd->infile = 0;
|
||||||
|
s_cmd->nb_args = double_size(split_line);
|
||||||
|
s_cmd->args = split_line;
|
||||||
|
printf("args= %s\n", *(split_line));
|
||||||
|
s_cmd->outfile = 0;
|
||||||
|
return (s_cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
t_cmd *split_cmd(t_cmd *cmd, char **cmds)
|
t_cmd *split_cmd(t_cmd *cmd, char **cmds)
|
||||||
@ -37,15 +51,15 @@ t_cmd *split_cmd(t_cmd *cmd, char **cmds)
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (cmd->s_cmds[i])
|
while (cmds[i])
|
||||||
{
|
{
|
||||||
cmd->s_cmds[i] = set_s_cmd(cmds[i])
|
cmd->s_cmds[i] = set_s_cmd(cmds[i], cmd);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return (cmd);
|
return (cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
t_cmd *set_cmd(char *input)
|
t_cmd *set_cmd(char *input, char **env)
|
||||||
{
|
{
|
||||||
t_cmd *cmd;
|
t_cmd *cmd;
|
||||||
char **cmds;
|
char **cmds;
|
||||||
@ -56,9 +70,14 @@ t_cmd *set_cmd(char *input)
|
|||||||
cmd = malloc(sizeof(t_cmd));
|
cmd = malloc(sizeof(t_cmd));
|
||||||
if (!cmd)
|
if (!cmd)
|
||||||
return (0);
|
return (0);
|
||||||
cmd->s_cmds = malloc(sizeof(t_s_cmds) * double_size(cmds));
|
cmd->s_cmds = malloc(sizeof(t_s_cmd) * double_size(cmds));
|
||||||
if (!cmd->s_cmds)
|
if (!cmd->s_cmds)
|
||||||
return (0);
|
return (0);
|
||||||
|
cmd->path = get_path(env);
|
||||||
|
cmd->outfile = 0;
|
||||||
|
cmd->infile = 0;
|
||||||
cmd->nb_s_cmd = double_size(cmds);
|
cmd->nb_s_cmd = double_size(cmds);
|
||||||
cmd = split_cmd(cmd, cmds); //split each cmd into args in s_cmd
|
cmd = split_cmd(cmd, cmds); //split each cmd into args in s_cmd
|
||||||
|
cmd->current_s_cmd = cmd->s_cmds[0];
|
||||||
|
return (cmd);
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user