refactor: put parsing of command and debugging out of main loop

This commit is contained in:
Khaïs COLIN 2025-03-19 16:26:45 +01:00
parent 36c1b72eff
commit 0584757446
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo

View file

@ -6,7 +6,7 @@
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/06 13:44:06 by kcolin #+# #+# */ /* Created: 2025/02/06 13:44:06 by kcolin #+# #+# */
/* Updated: 2025/03/19 12:12:39 by khais ### ########.fr */ /* Updated: 2025/03/19 16:30:32 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,12 +17,37 @@
#include "parser/wordsplit/wordsplit.h" #include "parser/wordsplit/wordsplit.h"
#include <stdlib.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);
}
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);
}
int main(int argc, char *argv[], char **envp) int main(int argc, char *argv[], char **envp)
{ {
char *line; char *line;
t_cmdgroup *cmd; t_cmdgroup *cmd;
t_wordlist *words;
t_buffer *indent;
(void)argc; (void)argc;
(void)argv; (void)argv;
@ -30,14 +55,8 @@ int main(int argc, char *argv[], char **envp)
line = get_command(); line = get_command();
while (line != NULL) while (line != NULL)
{ {
words = minishell_wordsplit(line); cmd = parse_command(line);
free(line); debug_command(cmd);
cmd = cmdgroup_from_wordlist(words);
wordlist_destroy(words);
indent = ft_buffer_new();
cmdgroup_debug(cmd, indent, true);
ft_buffer_free(indent);
cmdgroup_destroy(cmd);
line = get_command(); line = get_command();
} }
return (0); return (0);