minishell/src/minishell.c

85 lines
2.3 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* minishell.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/06 13:44:06 by kcolin #+# #+# */
/* Updated: 2025/03/20 17:36:58 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include "buffer/buffer.h"
#include "get_command.h"
#include "parser/cmdgroup/cmdgroup.h"
#include "parser/wordlist/wordlist.h"
#include "parser/wordsplit/wordsplit.h"
#include "postprocess/redirections/redirection_parsing.h"
#include "parser/remove_quotes/remove_quotes.h"
#include <stdlib.h>
/*
** Parse shell commands from line.
**
** Frees line before exiting
*/
static t_cmdgroup *parse_command(char *line)
{
t_wordlist *words;
t_cmdgroup *cmd;
words = minishell_wordsplit(line);
free(line);
cmd = cmdgroup_from_wordlist(words);
wordlist_destroy(words);
return (cmd);
}
/*
** debug-print a cmdgroup
*/
static void debug_command(t_cmdgroup *cmd)
{
t_buffer *indent;
indent = ft_buffer_new();
cmdgroup_debug(cmd, indent, true);
ft_buffer_free(indent);
cmdgroup_destroy(cmd);
}
/*
** Do all the post-processing steps relating to a command.
**
** Currently, this is the following:
** 1. redirection parsing
** 2. quote removal
*/
static t_cmdgroup *post_process_command(t_cmdgroup *cmd)
{
if (cmdgroup_parse_redirections(cmd) == NULL)
return (cmdgroup_destroy(cmd), NULL);
if (cmdgroup_remove_quotes(cmd) == NULL)
return (cmdgroup_destroy(cmd), NULL);
return (cmd);
}
int main(int argc, char *argv[], char **envp)
{
char *line;
t_cmdgroup *cmd;
(void)argc;
(void)argv;
(void)envp;
line = get_command();
while (line != NULL)
{
cmd = parse_command(line);
cmd = post_process_command(cmd);
debug_command(cmd);
line = get_command();
}
return (0);
}