fixed some norm

This commit is contained in:
whaffman 2025-02-13 15:05:17 +01:00
parent 3c04887ffb
commit 0784d3cf04
13 changed files with 166 additions and 93 deletions

2
.gitignore vendored
View File

@ -4,3 +4,5 @@ a.out
*.o *.o
obj/ obj/
.minishell_history .minishell_history
pipetester.c
pipetest

View File

@ -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 &&, ||, *

View File

@ -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);

View File

@ -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

View File

@ -6,56 +6,94 @@
void error(char *msg) void error(char *msg)
{ {
write(2, msg, strlen(msg)); write(2, msg, strlen(msg));
} }
int main(void) int main(void)
{ {
int fd_in = STDIN_FILENO; int fd_in = STDIN_FILENO;
int fd_out = STDOUT_FILENO; int fd_out = STDOUT_FILENO;
struct stat statbuf_in; struct stat statbuf_in;
struct stat statbuf_out; struct stat statbuf_out;
if (fstat(fd_in, &statbuf_in) == -1) char c;
{
perror("fstat");
return (1);
}
error("STDIN File type: ");
switch (statbuf_in.st_mode & S_IFMT) { if (fstat(fd_in, &statbuf_in) == -1)
case S_IFBLK: error("block device\n"); break; {
case S_IFCHR: error("character device\n"); break; perror("fstat");
case S_IFDIR: error("directory\n"); break; return (1);
case S_IFIFO: error("FIFO/pipe\n"); break; }
case S_IFLNK: error("symlink\n"); break; error("STDIN File type: ");
case S_IFREG: error("regular file\n"); break;
case S_IFSOCK: error("socket\n"); break; switch (statbuf_in.st_mode & S_IFMT)
default: error("unknown?\n"); break; {
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)
{
if (fstat(fd_out, &statbuf_out) == -1) perror("fstat");
{ return (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;
} }
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);
} }

View File

@ -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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -6,20 +6,20 @@
/* 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void executor_child(t_command *command) void executor_child(t_command *command)
{ {
char *path; char *path;
if (command->fd_in != 0) if (command->fd_in != 0)
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));
} }

View 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++;
}
}

View File

@ -1,11 +0,0 @@
void executor_close_pipes(int n_pipes)
{
int i;
i = 3;
while (i < n_pipes + 3)
{
close(i);
i++;
}
}

View File

@ -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.
t_list *current; *
t_command *command; * This function sets up the necessary pipes for inter-process communication
int fd[2]; * within the minishell. It initializes the pipes based on the requirements
int fd_in; * 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; fd_in = 0;
current = minishell->commands; current = minishell->commands;
while (current) while (current)
@ -34,8 +43,8 @@ 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;
} }
} }

View File

@ -6,18 +6,18 @@
/* 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #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; t_command *command;
executer_create_pipes(minishell); executor_create_pipes(minishell);
current = minishell->commands; current = minishell->commands;
while (current) while (current)
{ {
@ -26,4 +26,4 @@ void executor_execute_pipeline(t_minishell *minishell)
executor_fork(command); executor_fork(command);
current = current->next; current = current->next;
} }
} }

View File

@ -6,23 +6,23 @@
/* 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int executor_fork(t_command *command) int executor_fork(t_command *command)
{ {
pid_t pid; pid_t pid;
int status; int status;
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);
} }

View File

@ -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);