feat: parsing a token list into a command list

This commit is contained in:
Quinten Mennen 2025-02-11 16:29:31 +01:00
parent d2f3b5a1c2
commit 0992ae20cf
9 changed files with 189 additions and 15 deletions

View File

@ -1,12 +1,12 @@
# **************************************************************************** #
# #
# :::::::: #
# Makefile :+: :+: #
# +:+ #
# By: qmennen <qmennen@student.codam.nl> +#+ #
# +#+ #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/15 11:48:46 by whaffman #+# #+# #
# Updated: 2025/02/08 17:13:50 by willem ######## odam.nl #
# Updated: 2025/02/11 15:06:22 by qmennen ### ########.fr #
# #
# **************************************************************************** #

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/11 14:03:03 by qmennen #+# #+# */
/* Updated: 2025/02/11 14:03:36 by qmennen ### ########.fr */
/* Updated: 2025/02/11 16:25:16 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,4 +15,8 @@
#include "minishell.h"
t_command *parser_command_new(char *cmd);
char **parser_get_arguments(t_list *list);
t_list *parser_get_commands(t_list *list);
#endif

View File

@ -15,6 +15,7 @@
void free_minishell_line(t_minishell *minishell);
void free_minishell(t_minishell *minishell);
void free_command_list(void *content);
t_minishell *init_minishell(void);
void print_banner(void);
void print_list(void *content);

View File

@ -12,10 +12,10 @@
#include "minishell.h"
int main(int argc, char **argv, char **envp)
{
t_minishell *minishell;
t_list *commands;
(void)argc;
(void)argv;
@ -28,9 +28,11 @@ 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);
commands = parser_get_commands(minishell->tokens);
simple_builtins(minishell);
ft_lstiter(minishell->tokens, print_list);
//ft_lstiter(minishell->tokens, print_list);
free_minishell_line(minishell);
ft_lstclear(&commands, free_command_list);
}
free_minishell(minishell);
return (EXIT_SUCCESS);

View File

@ -0,0 +1,56 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser_get_arguments.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/11 16:20:09 by qmennen #+# #+# */
/* Updated: 2025/02/11 16:26:41 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int count_cmds(t_list *list)
{
int cmds;
t_list *current;
cmds = 0;
current = list;
while (current)
{
if (((t_token *)current->content)->type != T_WORD)
break ;
cmds++;
current = current->next;
}
return (cmds - 1);
}
char **parser_get_arguments(t_list *list)
{
t_list *current;
char **args;
int cmds;
int i;
cmds = count_cmds(list);
args = malloc((cmds + 1) * sizeof(char *));
if (!args)
{
perror("malloc");
exit(EXIT_FAILURE);
}
current = list->next;
i = -1;
while ((++i) < cmds && current)
{
if (((t_token *)current->content)->type == T_WORD)
args[i] = ft_strdup(((t_token *)current->content)->value);
current = current->next;
}
args[i] = 0;
return (args);
}

View File

@ -0,0 +1,56 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser_get_commands.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/11 14:06:02 by qmennen #+# #+# */
/* Updated: 2025/02/11 16:25:36 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static void print_cmds(void *content)
{
t_command *cmd;
int i;
cmd = (t_command *)content;
printf("cmd: %s\n", cmd->command);
printf("args: ");
i = 0;
while (cmd->args[i])
{
printf("%s ", cmd->args[i]);
i++;
}
printf("\n");
}
t_list *parser_get_commands(t_list *list)
{
t_list *command_list;
t_list *current;
t_command *command;
t_token *token;
command_list = NULL;
if (!list)
return (NULL);
current = list;
while (current)
{
token = (t_token *) current->content;
command = parser_command_new(ft_strdup(token->value));
command->args = parser_get_arguments(current);
ft_lstadd_back(&command_list, ft_lstnew(command));
while (current && ((t_token *)current->content)->type == T_WORD)
current = current->next;
if (current && ((t_token *)current->content)->type != T_WORD)
current = current->next;
}
ft_lstiter(command_list, print_cmds);
return (command_list);
}

View File

@ -1,13 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser.c :+: :+: :+: */
/* parser_new_command.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/11 14:06:02 by qmennen #+# #+# */
/* Updated: 2025/02/11 14:06:06 by qmennen ### ########.fr */
/* Created: 2025/02/11 16:18:21 by qmennen #+# #+# */
/* Updated: 2025/02/11 16:25:12 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser.h"
#include "minishell.h"
t_command *parser_command_new(char *cmd)
{
t_command *command;
command = malloc(sizeof(t_command));
if (!command)
{
perror("minishell malloc error");
exit(EXIT_FAILURE);
}
command->args = NULL;
command->fd_in = 0;
command->fd_out = 1;
command->command = cmd;
return (command);
}

View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* free_command_list.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/11 14:24:05 by qmennen #+# #+# */
/* Updated: 2025/02/11 14:27:02 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static void free_args(char **args)
{
int i;
i = 0;
while (args[i])
{
free(args[i]);
i++;
}
free(args);
}
void free_command_list(void *content)
{
t_command *command;
command = (t_command *)content;
if (command->command)
free(command->command);
if (command->args)
free_args(command->args);
free(command);
}