start set_cmd

This commit is contained in:
kinou-p 2022-03-08 15:22:02 +01:00
parent 914d6ceafd
commit 0d864ea39b
5 changed files with 110 additions and 27 deletions

View File

@ -6,16 +6,17 @@
# 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/06 17:29:26 by apommier ### ########.fr # # Updated: 2022/03/08 15:20:19 by apommier ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
NAME = minishell NAME = minishell
SRCS = main.c SRCS = main.c\
set_cmd.c
OBJS = ${SRCS:.c=.o} OBJS = ${SRCS:.c=.o}
CC = clang CC = clang
#CFLAGS = -Wall -Wextra CFLAGS = -Wall -Wextra
LIB = -lreadline LIB = -lreadline
#CFLAGS = -Wall -Wextra -Werror #CFLAGS = -Wall -Wextra -Werror
RM = rm -rf RM = rm -rf

10
main.c
View File

@ -6,28 +6,24 @@
/* 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/07 11:57:14 by apommier ### ########.fr */ /* Updated: 2022/03/08 15:20:48 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void color()
{
printf("\033[0m");
}
int main(int ac, char **av, char **path) int main(int ac, char **av, char **path)
{ {
char *input; char *input;
printf("---MINISHELL START---\n"); printf("---MINISHELL START---\n");
execute(t_cmd *cmd, *infile, *outfile); execute(t_cmd *cmd, *infile, *outfile);
while (1) while (1)
{ {
color();
input = readline("$~ "); input = readline("$~ ");
add_history(input); add_history(input);
set_cmd(input);
} }
return (0); return (0);
} }

View File

@ -13,27 +13,35 @@
#ifndef MINISHELL_H #ifndef MINISHELL_H
# define MINISHELL_H # define MINISHELL_H
# include "./libft/libft.h"
# include <stdio.h> # include <stdio.h>
# include <unistd.h> # include <unistd.h>
# include <stdlib.h> # include <stdlib.h>
# include <readline/readline.h> # include <readline/readline.h>
# include <readline/history.h> # include <readline/history.h>
# include "./libft/libft.h" # include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <sys/types.h>
# include <stdio.h>
# include <errno.h>
// Command Data Structure // Command Data Structure
// Describes a simple command and arguments // Describes a simple command and arguments
typedef struct s_simple { typedef struct s_simple {
int nb_args; int nb_args;
char *infile;
char *outfile;
char **args; char **args;
char *cmd; char *cmd;
} t_simple_cmd; } t_s_cmd;
// Describes a complete command with the multiple pipes if any // Describes a complete command with the multiple pipes if any
// and input/output redirection if any. // and input/output redirection if any.
typedef struct s_command { typedef struct s_command {
int nb_s_cmd; int nb_s_cmd;
struct s_simple **simple_cmds; struct s_simple **s_cmds;
char *out_file; char *out_file;
char *input_file; char *input_file;
char *err_file; char *err_file;
@ -44,6 +52,9 @@ typedef struct s_command {
int main();//int ac, char **av, char **path); int main();//int ac, char **av, char **path);
//pipe.c //pipe.c
void execute(t_cmd *cmd, char *infile, char *outfile); void execute(t_cmd *cmd);
//set_command
t_cmd *set_cmd(char *input);
#endif #endif

37
pipe.c
View File

@ -6,13 +6,13 @@
/* 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/07 11:52:26 by apommier ### ########.fr */ /* Updated: 2022/03/08 14:53:42 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void execute(t_cmd *cmd, char *infile, char *outfile) void execute(t_cmd *cmd)
{ {
//save in/out //save in/out
int ret; int ret;
@ -25,21 +25,27 @@ void execute(t_cmd *cmd, char *infile, char *outfile)
i = 0; i = 0;
//set the initial input //set the initial input
if (infile) if (current_s_cmd->cmd->infile)
fdin = open(infile,O_READ); fdin = open(current_s_cmd->cmd->infile, O_READ);
else //Use default input else if (cmd->infile)
fdin = open(cmd->infile,O_READ);
else
fdin=dup(tmpin); fdin=dup(tmpin);
while( i < cmd->nb_s_cmd)
while( i < numsimplecommands)
{ {
if (i != 0 && current_s_cmd->cmd->infile)
fdin = open(current_s_cmd->cmd->infile,O_READ);
//redirect input //redirect input
dup2(fdin, 0); dup2(fdin, 0);
close(fdin); close(fdin);
//setup output //setup output
if (i == numsimplecommands - 1) if (i == cmd->nb_s_cmd - 1)
{ {
if(outfile)// Last simple command // Last simple command
fdout=open(outfile,â¦â¦); if (current_s_cmd->cmd->outfile)
fdout = open(current_s_cmd->cmd->outfile, â¦â¦);
else if(cmd->outfile)
fdout=open(cmd->outfile, â¦â¦);
else// Use default output else// Use default output
fdout=dup(tmpout); fdout=dup(tmpout);
} }
@ -75,10 +81,15 @@ void execute(t_cmd *cmd, char *infile, char *outfile)
close(tmpin); close(tmpin);
close(tmpout); close(tmpout);
if (!background) { if (!background)
// Wait for last command {
waitpid(ret, NULL); // Wait for last command
waitpid(ret, NULL);
} }
} // execute } // execute
int main(int argc, char **argv)
{
execute(t_cmd *cmd, char *infile, char *outfile);
}

64
set_cmd.c Normal file
View File

@ -0,0 +1,64 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* set_cmd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/08 15:19:42 by apommier #+# #+# */
/* Updated: 2022/03/08 15:19:47 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
int double_size(char **tab)
{
int i;
i = 0;
while (tab[i])
i++;
return (i);
}
s_cmds *set_s_cmd(char *line)
{
t_s_cmd s_cmd;
tmp **split_line;
int i;
i = 0;
split_line = ft_split(line, ' ');
s_cmd.cmd = split_line[i];
}
t_cmd *split_cmd(t_cmd *cmd, char **cmds)
{
int i;
i = 0;
while (cmd->s_cmds[i])
{
cmd->s_cmds[i] = set_s_cmd(cmds[i])
i++;
}
return (cmd);
}
t_cmd *set_cmd(char *input)
{
t_cmd *cmd;
char **cmds;
cmds = ft_split(input, '|');
if (!cmds)
return (0);
cmd = malloc(sizeof(t_cmd));
if (!cmd)
return (0);
cmd->s_cmds = malloc(sizeof(t_s_cmds) * double_size(cmds));
if (!cmd->s_cmds)
return (0);
cmd->nb_s_cmd = double_size(cmds);
cmd = split_cmd(cmd, cmds); //split each cmd into args in s_cmd
}