start set_cmd
This commit is contained in:
parent
914d6ceafd
commit
0d864ea39b
7
Makefile
7
Makefile
@ -6,16 +6,17 @@
|
||||
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# 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
|
||||
SRCS = main.c
|
||||
SRCS = main.c\
|
||||
set_cmd.c
|
||||
OBJS = ${SRCS:.c=.o}
|
||||
CC = clang
|
||||
#CFLAGS = -Wall -Wextra
|
||||
CFLAGS = -Wall -Wextra
|
||||
LIB = -lreadline
|
||||
#CFLAGS = -Wall -Wextra -Werror
|
||||
RM = rm -rf
|
||||
|
||||
10
main.c
10
main.c
@ -6,28 +6,24 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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"
|
||||
|
||||
void color()
|
||||
{
|
||||
printf("\033[0m");
|
||||
}
|
||||
|
||||
int main(int ac, char **av, char **path)
|
||||
{
|
||||
char *input;
|
||||
|
||||
printf("---MINISHELL START---\n");
|
||||
|
||||
execute(t_cmd *cmd, *infile, *outfile);
|
||||
while (1)
|
||||
{
|
||||
color();
|
||||
input = readline("$~ ");
|
||||
add_history(input);
|
||||
set_cmd(input);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
19
minishell.h
19
minishell.h
@ -13,27 +13,35 @@
|
||||
#ifndef MINISHELL_H
|
||||
# define MINISHELL_H
|
||||
|
||||
# include "./libft/libft.h"
|
||||
# include <stdio.h>
|
||||
# include <unistd.h>
|
||||
# include <stdlib.h>
|
||||
# include <readline/readline.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
|
||||
|
||||
// Describes a simple command and arguments
|
||||
typedef struct s_simple {
|
||||
int nb_args;
|
||||
char *infile;
|
||||
char *outfile;
|
||||
char **args;
|
||||
char *cmd;
|
||||
} t_simple_cmd;
|
||||
} t_s_cmd;
|
||||
|
||||
// Describes a complete command with the multiple pipes if any
|
||||
// and input/output redirection if any.
|
||||
typedef struct s_command {
|
||||
int nb_s_cmd;
|
||||
struct s_simple **simple_cmds;
|
||||
struct s_simple **s_cmds;
|
||||
char *out_file;
|
||||
char *input_file;
|
||||
char *err_file;
|
||||
@ -44,6 +52,9 @@ typedef struct s_command {
|
||||
int main();//int ac, char **av, char **path);
|
||||
|
||||
//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
|
||||
33
pipe.c
33
pipe.c
@ -6,13 +6,13 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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"
|
||||
|
||||
void execute(t_cmd *cmd, char *infile, char *outfile)
|
||||
void execute(t_cmd *cmd)
|
||||
{
|
||||
//save in/out
|
||||
int ret;
|
||||
@ -25,21 +25,27 @@ void execute(t_cmd *cmd, char *infile, char *outfile)
|
||||
|
||||
i = 0;
|
||||
//set the initial input
|
||||
if (infile)
|
||||
fdin = open(infile,O_READ);
|
||||
else //Use default input
|
||||
if (current_s_cmd->cmd->infile)
|
||||
fdin = open(current_s_cmd->cmd->infile, O_READ);
|
||||
else if (cmd->infile)
|
||||
fdin = open(cmd->infile,O_READ);
|
||||
else
|
||||
fdin=dup(tmpin);
|
||||
|
||||
while( i < numsimplecommands)
|
||||
while( i < cmd->nb_s_cmd)
|
||||
{
|
||||
if (i != 0 && current_s_cmd->cmd->infile)
|
||||
fdin = open(current_s_cmd->cmd->infile,O_READ);
|
||||
//redirect input
|
||||
dup2(fdin, 0);
|
||||
close(fdin);
|
||||
//setup output
|
||||
if (i == numsimplecommands - 1)
|
||||
if (i == cmd->nb_s_cmd - 1)
|
||||
{
|
||||
if(outfile)// Last simple command
|
||||
fdout=open(outfile,……);
|
||||
// Last simple command
|
||||
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
|
||||
fdout=dup(tmpout);
|
||||
}
|
||||
@ -75,10 +81,15 @@ void execute(t_cmd *cmd, char *infile, char *outfile)
|
||||
close(tmpin);
|
||||
close(tmpout);
|
||||
|
||||
if (!background) {
|
||||
if (!background)
|
||||
{
|
||||
// Wait for last command
|
||||
waitpid(ret, NULL);
|
||||
}
|
||||
|
||||
} // execute
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
execute(t_cmd *cmd, char *infile, char *outfile);
|
||||
}
|
||||
64
set_cmd.c
Normal file
64
set_cmd.c
Normal 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
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user