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
|
||||
obj/
|
||||
.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] Simple builtin export
|
||||
- Preliminary signals
|
||||
- Define struct for commands, something like (
|
||||
-[x] Define struct for commands, something like (
|
||||
```c
|
||||
typedef struct s_command
|
||||
{
|
||||
@ -24,9 +24,16 @@ A lot of amazing shell stuff
|
||||
} t_command;
|
||||
```
|
||||
)
|
||||
- Make the `executor`, run a command
|
||||
- [x] Make the `executor`, run a command
|
||||
- Make a parser to create a command list
|
||||
- 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 \$?
|
||||
- CB command to change banner
|
||||
- __Bonus:__ Command tree for &&, ||, *
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: willem <willem@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* 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);
|
||||
void executor_create_pipes(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> +#+ */
|
||||
/* +#+ */
|
||||
/* 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;
|
||||
int fd_in;
|
||||
int fd_out;
|
||||
int n_pipes;
|
||||
int n_fds;
|
||||
int exit_status;
|
||||
} t_command;
|
||||
|
||||
typedef struct s_minishell
|
||||
|
||||
118
pipetester.c
118
pipetester.c
@ -6,56 +6,94 @@
|
||||
|
||||
void error(char *msg)
|
||||
{
|
||||
write(2, msg, strlen(msg));
|
||||
write(2, msg, strlen(msg));
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int fd_in = STDIN_FILENO;
|
||||
int fd_out = STDOUT_FILENO;
|
||||
struct stat statbuf_in;
|
||||
struct stat statbuf_out;
|
||||
int fd_in = STDIN_FILENO;
|
||||
int fd_out = STDOUT_FILENO;
|
||||
struct stat statbuf_in;
|
||||
struct stat statbuf_out;
|
||||
|
||||
if (fstat(fd_in, &statbuf_in) == -1)
|
||||
{
|
||||
perror("fstat");
|
||||
return (1);
|
||||
}
|
||||
error("STDIN File type: ");
|
||||
char c;
|
||||
|
||||
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_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;
|
||||
if (fstat(fd_in, &statbuf_in) == -1)
|
||||
{
|
||||
perror("fstat");
|
||||
return (1);
|
||||
}
|
||||
error("STDIN File type: ");
|
||||
|
||||
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_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)
|
||||
{
|
||||
perror("fstat");
|
||||
return (1);
|
||||
}
|
||||
|
||||
error("STDOUT File type: ");
|
||||
|
||||
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_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;
|
||||
if (fstat(fd_out, &statbuf_out) == -1)
|
||||
{
|
||||
perror("fstat");
|
||||
return (1);
|
||||
}
|
||||
|
||||
error("STDOUT File type: ");
|
||||
|
||||
return (0);
|
||||
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_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);
|
||||
}
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: willem <willem@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* 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,20 +6,20 @@
|
||||
/* By: willem <willem@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
void executor_child(t_command *command)
|
||||
void executor_child(t_command *command)
|
||||
{
|
||||
char *path;
|
||||
if (command->fd_in != 0)
|
||||
dup2(command->fd_in, 0);
|
||||
if (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);
|
||||
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> +#+ */
|
||||
/* +#+ */
|
||||
/* 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"
|
||||
|
||||
void executer_create_pipes(t_minishell *minishell)
|
||||
{
|
||||
t_list *current;
|
||||
t_command *command;
|
||||
int fd[2];
|
||||
int fd_in;
|
||||
|
||||
/**
|
||||
* @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_command *command;
|
||||
int fd[2];
|
||||
int fd_in;
|
||||
|
||||
|
||||
fd_in = 0;
|
||||
current = minishell->commands;
|
||||
while (current)
|
||||
@ -34,8 +43,8 @@ void executer_create_pipes(t_minishell *minishell)
|
||||
else
|
||||
command->fd_out = 1;
|
||||
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];
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -6,18 +6,18 @@
|
||||
/* By: willem <willem@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
void executor_execute_pipeline(t_minishell *minishell)
|
||||
void executor_execute_pipeline(t_minishell *minishell)
|
||||
{
|
||||
t_list *current;
|
||||
t_list *current;
|
||||
t_command *command;
|
||||
|
||||
executer_create_pipes(minishell);
|
||||
executor_create_pipes(minishell);
|
||||
current = minishell->commands;
|
||||
while (current)
|
||||
{
|
||||
@ -26,4 +26,4 @@ void executor_execute_pipeline(t_minishell *minishell)
|
||||
executor_fork(command);
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,23 +6,23 @@
|
||||
/* By: willem <willem@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
int executor_fork(t_command *command)
|
||||
int executor_fork(t_command *command)
|
||||
{
|
||||
pid_t pid;
|
||||
pid_t pid;
|
||||
int status;
|
||||
|
||||
pid = fork();
|
||||
if (pid > 0)
|
||||
{
|
||||
if(command->fd_in != 0)
|
||||
if (command->fd_in != 0)
|
||||
close(command->fd_in);
|
||||
if(command->fd_out != 1)
|
||||
if (command->fd_out != 1)
|
||||
close(command->fd_out);
|
||||
waitpid(pid, &status, 0);
|
||||
return (((status) & 0xff00) >> 8);
|
||||
@ -31,5 +31,5 @@ int executor_fork(t_command *command)
|
||||
executor_child(command);
|
||||
else
|
||||
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> +#+ */
|
||||
/* +#+ */
|
||||
/* 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 "utils.h"
|
||||
|
||||
// static void token_print(void *param)
|
||||
// {
|
||||
// t_token *token;
|
||||
static void token_print(void *param)
|
||||
{
|
||||
t_token *token;
|
||||
|
||||
// token = (t_token *)param;
|
||||
// printf("token type %i, value %s\n", token->type, token->value);
|
||||
// }
|
||||
token = (t_token *)param;
|
||||
printf("token type %i, value %s\n", token->type, token->value);
|
||||
}
|
||||
|
||||
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->lexer = ft_lexer_new(minishell->line);
|
||||
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);
|
||||
simple_builtins(minishell);
|
||||
free_minishell_line(minishell);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user