fixed some norm
This commit is contained in:
parent
3c04887ffb
commit
0784d3cf04
2
.gitignore
vendored
2
.gitignore
vendored
@ -4,3 +4,5 @@ a.out
|
|||||||
*.o
|
*.o
|
||||||
obj/
|
obj/
|
||||||
.minishell_history
|
.minishell_history
|
||||||
|
pipetester.c
|
||||||
|
pipetest
|
||||||
|
|||||||
11
README.md
11
README.md
@ -12,7 +12,7 @@ A lot of amazing shell stuff
|
|||||||
-[x] Set environment variable (export)
|
-[x] Set environment variable (export)
|
||||||
-[x] Simple builtin export
|
-[x] Simple builtin export
|
||||||
- Preliminary signals
|
- Preliminary signals
|
||||||
- Define struct for commands, something like (
|
-[x] Define struct for commands, something like (
|
||||||
```c
|
```c
|
||||||
typedef struct s_command
|
typedef struct s_command
|
||||||
{
|
{
|
||||||
@ -24,9 +24,16 @@ A lot of amazing shell stuff
|
|||||||
} t_command;
|
} t_command;
|
||||||
```
|
```
|
||||||
)
|
)
|
||||||
- Make the `executor`, run a command
|
- [x] Make the `executor`, run a command
|
||||||
- Make a parser to create a command list
|
- Make a parser to create a command list
|
||||||
- Add redirects, appends, pipe etc. File descriptor functions
|
- Add redirects, appends, pipe etc. File descriptor functions
|
||||||
|
a command can have multiple redirects but only the last is used for stdout
|
||||||
|
Redirects take precedence over pipes, but pipes are still created and managed.
|
||||||
|
should it close the unused pipe-end?
|
||||||
|
all redirects are opened and closed, but only last fd is used.
|
||||||
|
multiple HEREDOCs can be nested, only last is used.
|
||||||
|
|
||||||
|
|
||||||
- Expand \$ vars & support \$?
|
- Expand \$ vars & support \$?
|
||||||
- CB command to change banner
|
- CB command to change banner
|
||||||
- __Bonus:__ Command tree for &&, ||, *
|
- __Bonus:__ Command tree for &&, ||, *
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: willem <willem@student.codam.nl> +#+ */
|
/* By: willem <willem@student.codam.nl> +#+ */
|
||||||
/* +#+ */
|
/* +#+ */
|
||||||
/* Created: 2025/02/08 17:06:07 by willem #+# #+# */
|
/* Created: 2025/02/08 17:06:07 by willem #+# #+# */
|
||||||
/* Updated: 2025/02/12 20:24:20 by willem ######## odam.nl */
|
/* Updated: 2025/02/13 14:57:13 by whaffman ######## odam.nl */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -20,6 +20,8 @@ void executor_child(t_command *command);
|
|||||||
int executor_fork(t_command *command);
|
int executor_fork(t_command *command);
|
||||||
void executor_create_pipes(t_minishell *minishell);
|
void executor_create_pipes(t_minishell *minishell);
|
||||||
void executor_execute_pipeline(t_minishell *minishell);
|
void executor_execute_pipeline(t_minishell *minishell);
|
||||||
|
void executor_close_fds(int n_fds);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
/* +#+ */
|
/* +#+ */
|
||||||
/* Created: 2025/02/05 12:36:08 by whaffman #+# #+# */
|
/* Created: 2025/02/05 12:36:08 by whaffman #+# #+# */
|
||||||
/* Updated: 2025/02/12 20:35:09 by willem ######## odam.nl */
|
/* Updated: 2025/02/13 14:27:07 by whaffman ######## odam.nl */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -53,7 +53,8 @@ typedef struct s_command
|
|||||||
t_list *environment;
|
t_list *environment;
|
||||||
int fd_in;
|
int fd_in;
|
||||||
int fd_out;
|
int fd_out;
|
||||||
int n_pipes;
|
int n_fds;
|
||||||
|
int exit_status;
|
||||||
} t_command;
|
} t_command;
|
||||||
|
|
||||||
typedef struct s_minishell
|
typedef struct s_minishell
|
||||||
|
|||||||
80
pipetester.c
80
pipetester.c
@ -16,6 +16,8 @@ int main(void)
|
|||||||
struct stat statbuf_in;
|
struct stat statbuf_in;
|
||||||
struct stat statbuf_out;
|
struct stat statbuf_out;
|
||||||
|
|
||||||
|
char c;
|
||||||
|
|
||||||
if (fstat(fd_in, &statbuf_in) == -1)
|
if (fstat(fd_in, &statbuf_in) == -1)
|
||||||
{
|
{
|
||||||
perror("fstat");
|
perror("fstat");
|
||||||
@ -23,18 +25,34 @@ int main(void)
|
|||||||
}
|
}
|
||||||
error("STDIN File type: ");
|
error("STDIN File type: ");
|
||||||
|
|
||||||
switch (statbuf_in.st_mode & S_IFMT) {
|
switch (statbuf_in.st_mode & S_IFMT)
|
||||||
case S_IFBLK: error("block device\n"); break;
|
{
|
||||||
case S_IFCHR: error("character device\n"); break;
|
case S_IFBLK:
|
||||||
case S_IFDIR: error("directory\n"); break;
|
error("block device\n");
|
||||||
case S_IFIFO: error("FIFO/pipe\n"); break;
|
break;
|
||||||
case S_IFLNK: error("symlink\n"); break;
|
case S_IFCHR:
|
||||||
case S_IFREG: error("regular file\n"); break;
|
error("character device\n");
|
||||||
case S_IFSOCK: error("socket\n"); break;
|
break;
|
||||||
default: error("unknown?\n"); break;
|
case S_IFDIR:
|
||||||
|
error("directory\n");
|
||||||
|
break;
|
||||||
|
case S_IFIFO:
|
||||||
|
error("FIFO/pipe\n");
|
||||||
|
break;
|
||||||
|
case S_IFLNK:
|
||||||
|
error("symlink\n");
|
||||||
|
break;
|
||||||
|
case S_IFREG:
|
||||||
|
error("regular file\n");
|
||||||
|
break;
|
||||||
|
case S_IFSOCK:
|
||||||
|
error("socket\n");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
error("unknown?\n");
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
error("\n");
|
error("\n");
|
||||||
|
|
||||||
if (fstat(fd_out, &statbuf_out) == -1)
|
if (fstat(fd_out, &statbuf_out) == -1)
|
||||||
@ -43,19 +61,39 @@ int main(void)
|
|||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
error("STDOUT File type: ");
|
error("STDOUT File type: ");
|
||||||
|
|
||||||
switch (statbuf_out.st_mode & S_IFMT) {
|
switch (statbuf_out.st_mode & S_IFMT)
|
||||||
case S_IFBLK: error("block device\n"); break;
|
{
|
||||||
case S_IFCHR: error("character device\n"); break;
|
case S_IFBLK:
|
||||||
case S_IFDIR: error("directory\n"); break;
|
error("block device\n");
|
||||||
case S_IFIFO: error("FIFO/pipe\n"); break;
|
break;
|
||||||
case S_IFLNK: error("symlink\n"); break;
|
case S_IFCHR:
|
||||||
case S_IFREG: error("regular file\n"); break;
|
error("character device\n");
|
||||||
case S_IFSOCK: error("socket\n"); break;
|
break;
|
||||||
default: error("unknown?\n"); break;
|
case S_IFDIR:
|
||||||
|
error("directory\n");
|
||||||
|
break;
|
||||||
|
case S_IFIFO:
|
||||||
|
error("FIFO/pipe\n");
|
||||||
|
break;
|
||||||
|
case S_IFLNK:
|
||||||
|
error("symlink\n");
|
||||||
|
break;
|
||||||
|
case S_IFREG:
|
||||||
|
error("regular file\n");
|
||||||
|
break;
|
||||||
|
case S_IFSOCK:
|
||||||
|
error("socket\n");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
error("unknown?\n");
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
while (read(fd_in, &c, 1) > 0)
|
||||||
|
{
|
||||||
|
write(fd_out, &c, 1);
|
||||||
|
}
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: willem <willem@student.codam.nl> +#+ */
|
/* By: willem <willem@student.codam.nl> +#+ */
|
||||||
/* +#+ */
|
/* +#+ */
|
||||||
/* Created: 2025/02/08 17:00:24 by willem #+# #+# */
|
/* Created: 2025/02/08 17:00:24 by willem #+# #+# */
|
||||||
/* Updated: 2025/02/11 17:20:17 by whaffman ######## odam.nl */
|
/* Updated: 2025/02/13 15:04:09 by whaffman ######## odam.nl */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: willem <willem@student.codam.nl> +#+ */
|
/* By: willem <willem@student.codam.nl> +#+ */
|
||||||
/* +#+ */
|
/* +#+ */
|
||||||
/* Created: 2025/02/12 21:25:10 by willem #+# #+# */
|
/* Created: 2025/02/12 21:25:10 by willem #+# #+# */
|
||||||
/* Updated: 2025/02/12 21:25:11 by willem ######## odam.nl */
|
/* Updated: 2025/02/13 15:04:12 by whaffman ######## odam.nl */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -19,7 +19,7 @@ void executor_child(t_command *command)
|
|||||||
dup2(command->fd_in, 0);
|
dup2(command->fd_in, 0);
|
||||||
if (command->fd_out != 1)
|
if (command->fd_out != 1)
|
||||||
dup2(command->fd_out, 1);
|
dup2(command->fd_out, 1);
|
||||||
executor_close_pipes(command->n_pipes);
|
executor_close_fds(command->n_fds);
|
||||||
path = executor_absolute_path(command->environment, command->command);
|
path = executor_absolute_path(command->environment, command->command);
|
||||||
execve(path, command->args, environment_get_arr(command->environment));
|
execve(path, command->args, environment_get_arr(command->environment));
|
||||||
}
|
}
|
||||||
25
src/executor/executor_close_fds.c
Normal file
25
src/executor/executor_close_fds.c
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* :::::::: */
|
||||||
|
/* executor_close_fds.c :+: :+: */
|
||||||
|
/* +:+ */
|
||||||
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
|
/* +#+ */
|
||||||
|
/* Created: 2025/02/13 13:31:25 by whaffman #+# #+# */
|
||||||
|
/* Updated: 2025/02/13 15:04:15 by whaffman ######## odam.nl */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
void executor_close_fds(int n_fds)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 3;
|
||||||
|
while (i < n_fds + 3)
|
||||||
|
{
|
||||||
|
close(i);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,11 +0,0 @@
|
|||||||
void executor_close_pipes(int n_pipes)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
i = 3;
|
|
||||||
while (i < n_pipes + 3)
|
|
||||||
{
|
|
||||||
close(i);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,26 +1,35 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* :::::::: */
|
/* :::::::: */
|
||||||
/* executer_create_pipes.c :+: :+: */
|
/* executor_create_pipes.c :+: :+: */
|
||||||
/* +:+ */
|
/* +:+ */
|
||||||
/* By: willem <willem@student.codam.nl> +#+ */
|
/* By: willem <willem@student.codam.nl> +#+ */
|
||||||
/* +#+ */
|
/* +#+ */
|
||||||
/* Created: 2025/02/12 21:25:22 by willem #+# #+# */
|
/* Created: 2025/02/12 21:25:22 by willem #+# #+# */
|
||||||
/* Updated: 2025/02/12 21:25:23 by willem ######## odam.nl */
|
/* Updated: 2025/02/13 14:58:49 by whaffman ######## odam.nl */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
void executer_create_pipes(t_minishell *minishell)
|
/**
|
||||||
|
* @brief Creates pipes for the executor in the minishell.
|
||||||
|
*
|
||||||
|
* This function sets up the necessary pipes for inter-process communication
|
||||||
|
* within the minishell. It initializes the pipes based on the requirements
|
||||||
|
* of the minishell structure.
|
||||||
|
*
|
||||||
|
* @param minishell A pointer to the t_minishell structure containing the
|
||||||
|
* necessary information for pipe creation.
|
||||||
|
*/
|
||||||
|
|
||||||
|
void executor_create_pipes(t_minishell *minishell)
|
||||||
{
|
{
|
||||||
t_list *current;
|
t_list *current;
|
||||||
t_command *command;
|
t_command *command;
|
||||||
int fd[2];
|
int fd[2];
|
||||||
int fd_in;
|
int fd_in;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
fd_in = 0;
|
fd_in = 0;
|
||||||
current = minishell->commands;
|
current = minishell->commands;
|
||||||
while (current)
|
while (current)
|
||||||
@ -34,7 +43,7 @@ void executer_create_pipes(t_minishell *minishell)
|
|||||||
else
|
else
|
||||||
command->fd_out = 1;
|
command->fd_out = 1;
|
||||||
command->fd_in = fd_in;
|
command->fd_in = fd_in;
|
||||||
command->n_pipes = (ft_lstsize(minishell->commands) - 1) * 2;
|
command->n_fds = (ft_lstsize(minishell->commands) - 1) * 2;
|
||||||
fd_in = fd[0];
|
fd_in = fd[0];
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: willem <willem@student.codam.nl> +#+ */
|
/* By: willem <willem@student.codam.nl> +#+ */
|
||||||
/* +#+ */
|
/* +#+ */
|
||||||
/* Created: 2025/02/12 21:25:02 by willem #+# #+# */
|
/* Created: 2025/02/12 21:25:02 by willem #+# #+# */
|
||||||
/* Updated: 2025/02/12 21:25:03 by willem ######## odam.nl */
|
/* Updated: 2025/02/13 15:04:18 by whaffman ######## odam.nl */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -17,7 +17,7 @@ void executor_execute_pipeline(t_minishell *minishell)
|
|||||||
t_list *current;
|
t_list *current;
|
||||||
t_command *command;
|
t_command *command;
|
||||||
|
|
||||||
executer_create_pipes(minishell);
|
executor_create_pipes(minishell);
|
||||||
current = minishell->commands;
|
current = minishell->commands;
|
||||||
while (current)
|
while (current)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: willem <willem@student.codam.nl> +#+ */
|
/* By: willem <willem@student.codam.nl> +#+ */
|
||||||
/* +#+ */
|
/* +#+ */
|
||||||
/* Created: 2025/02/12 21:24:52 by willem #+# #+# */
|
/* Created: 2025/02/12 21:24:52 by willem #+# #+# */
|
||||||
/* Updated: 2025/02/12 21:24:53 by willem ######## odam.nl */
|
/* Updated: 2025/02/13 15:03:19 by whaffman ######## odam.nl */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -20,9 +20,9 @@ int executor_fork(t_command *command)
|
|||||||
pid = fork();
|
pid = fork();
|
||||||
if (pid > 0)
|
if (pid > 0)
|
||||||
{
|
{
|
||||||
if(command->fd_in != 0)
|
if (command->fd_in != 0)
|
||||||
close(command->fd_in);
|
close(command->fd_in);
|
||||||
if(command->fd_out != 1)
|
if (command->fd_out != 1)
|
||||||
close(command->fd_out);
|
close(command->fd_out);
|
||||||
waitpid(pid, &status, 0);
|
waitpid(pid, &status, 0);
|
||||||
return (((status) & 0xff00) >> 8);
|
return (((status) & 0xff00) >> 8);
|
||||||
@ -31,5 +31,5 @@ int executor_fork(t_command *command)
|
|||||||
executor_child(command);
|
executor_child(command);
|
||||||
else
|
else
|
||||||
perror("fork");
|
perror("fork");
|
||||||
return (EXIT_FAILURE);
|
return (-1);
|
||||||
}
|
}
|
||||||
16
src/main.c
16
src/main.c
@ -6,7 +6,7 @@
|
|||||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
/* +#+ */
|
/* +#+ */
|
||||||
/* Created: 2025/02/04 16:19:22 by whaffman #+# #+# */
|
/* Created: 2025/02/04 16:19:22 by whaffman #+# #+# */
|
||||||
/* Updated: 2025/02/12 20:46:11 by willem ######## odam.nl */
|
/* Updated: 2025/02/13 13:36:23 by whaffman ######## odam.nl */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -14,13 +14,13 @@
|
|||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
// static void token_print(void *param)
|
static void token_print(void *param)
|
||||||
// {
|
{
|
||||||
// t_token *token;
|
t_token *token;
|
||||||
|
|
||||||
// token = (t_token *)param;
|
token = (t_token *)param;
|
||||||
// printf("token type %i, value %s\n", token->type, token->value);
|
printf("token type %i, value %s\n", token->type, token->value);
|
||||||
// }
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv, char **envp)
|
int main(int argc, char **argv, char **envp)
|
||||||
{
|
{
|
||||||
@ -37,7 +37,7 @@ int main(int argc, char **argv, char **envp)
|
|||||||
minishell->line = ft_prompt(minishell);
|
minishell->line = ft_prompt(minishell);
|
||||||
minishell->lexer = ft_lexer_new(minishell->line);
|
minishell->lexer = ft_lexer_new(minishell->line);
|
||||||
minishell->tokens = ft_parse_input(minishell->lexer);
|
minishell->tokens = ft_parse_input(minishell->lexer);
|
||||||
//ft_lstiter(minishell->tokens, token_print);
|
ft_lstiter(minishell->tokens, token_print);
|
||||||
minishell->commands = parser_get_commands(minishell->tokens);
|
minishell->commands = parser_get_commands(minishell->tokens);
|
||||||
simple_builtins(minishell);
|
simple_builtins(minishell);
|
||||||
free_minishell_line(minishell);
|
free_minishell_line(minishell);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user