Compare commits

...

2 commits

Author SHA1 Message Date
Jérôme Guélen
926774846f
parse-cmd: Almost done but stopped by the gong 2025-04-08 19:56:46 +02:00
Jérôme Guélen
d0cea8828d
parse-cmd: Mainly adding destructors and a token type. 2025-04-08 16:29:57 +02:00
6 changed files with 267 additions and 65 deletions

View file

@ -41,7 +41,7 @@ OPT_PIPELINE -> | GROUP_OR_SIMPLE OPT_PIPELINE
OPT_PIPELINE -> ε OPT_PIPELINE -> ε
GROUP_OR_SIMPLE -> (CMDS) REDIR GROUP_OR_SIMPLE -> (CMDS) REDIR
GROUP_OR_SIMPLE -> SIMPLE GROUP_OR_SIMPLE -> SIMPLE
SIMPLE -> REDIR word REDIR SIMPLST SIMPLE -> REDIR word REDIR SIMPLE_LST
SIMPLE_LST -> word REDIR SIMPLE_LST SIMPLE_LST -> word REDIR SIMPLE_LST
SIMPLE_LST -> ε SIMPLE_LST -> ε
REDIR -> > word REDIR REDIR -> > word REDIR

84
src/destroy_cmds.c Normal file
View file

@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* destroy_cmds.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/08 14:26:06 by jguelen #+# #+# */
/* Updated: 2025/04/08 19:06:42 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void simple_cmd_destroy(t_simple_cmd *cmd)
{
if (!cmd)
return ;
wordlist_destroy(cmd->words);
redirect_destroy(cmd->redirections);
free(cmd);
}
void group_cmd_destroy(t_group_cmd *cmd)
{
if (!cmd)
return ;
t_cmd_destroy(cmd->cmd);
redirect_destroy(cmd->redirects);
free(cmd);
}
void connec_cmd_destroy(t_connec_cmd *cmd)
{
if (!cmd)
return ;
t_cmd_destroy(cmd->first);
t_cmd_destroy(cmd->second);
free(cmd);
}
/*
** here_doc_eof is currently unused so it might disappear.
*/
void redirect_destroy(t_redirect *redirections)
{
t_redirect *tmp;
while (redirections)
{
free(redirections->here_doc_eof);
if (redirections->type == FT_HEREDOC)
close(redirections->redirectee.dest);
else
worddesc_destroy(redirections->redirectee.filename);
tmp = redirections;
redirections = redirections->next;
free(tmp);
}
}
/*
** This function recursively destroys the whole command tree.
*/
void t_cmd_destroy(t_cmd *cmd)
{
if (!cmd)
return ;
if (cmd->type == FT_CONNECTION)
{
t_cmd_destroy(cmd->value.connection->first);
t_cmd_destroy(cmd->value.connection->second);
free(cmd);
return ;
}
if (cmd->type == FT_GROUP)
{
redirect_destroy(cmd->value.group->redirects);
t_cmd_destroy(cmd->value.group->cmd);
free(cmd);
return ;
}
simple_cmd_destroy(cmd->value.simple);
}

View file

@ -6,7 +6,7 @@
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */ /* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/28 14:55:31 by khais #+# #+# */ /* Created: 2025/03/28 14:55:31 by khais #+# #+# */
/* Updated: 2025/04/07 18:09:12 by jguelen ### ########.fr */ /* Updated: 2025/04/08 19:46:43 by jguelen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -67,7 +67,6 @@ typedef struct s_cmd
** Line number the command starts on -> will probably be unused. ** Line number the command starts on -> will probably be unused.
*/ */
int line; int line;
t_redirect *redirects;
union u_value union u_value
{ {
struct s_connec_cmd *connection; struct s_connec_cmd *connection;
@ -121,4 +120,14 @@ typedef struct s_minishell
int last_return_value; int last_return_value;
} t_minishell; } t_minishell;
t_redirect *t_redirect_add_back(t_redirect **init, t_redirect *back);
void simple_cmd_destroy(t_simple_cmd *cmd);
void group_cmd_destroy(t_group_cmd *cmd);
void connec_cmd_destroy(t_connec_cmd *cmd);
void redirect_destroy(t_redirect *redirections);
/*
** This function recursively destroys the whole command tree.
*/
void t_cmd_destroy(t_cmd *cmd);
#endif #endif

View file

@ -6,7 +6,7 @@
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */ /* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/31 10:28:28 by jguelen #+# #+# */ /* Created: 2025/03/31 10:28:28 by jguelen #+# #+# */
/* Updated: 2025/04/08 13:01:32 by jguelen ### ########.fr */ /* Updated: 2025/04/08 19:56:28 by jguelen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,7 +16,10 @@
** NOTE: This file will temporarily include way more functions than allowed. ** NOTE: This file will temporarily include way more functions than allowed.
*/ */
/* /*
** Maybe include desctruction of builder contents here. ** TODO Check if builder grows that destruction is appropriate.
** TODO Remove debugs
** TODO Add a function to write a message and set errno in case of allocation
** error.
*/ */
void parse_error(t_minishell *app, t_worddesc *token) void parse_error(t_minishell *app, t_worddesc *token)
{ {
@ -25,10 +28,20 @@ void parse_error(t_minishell *app, t_worddesc *token)
app->last_return_value = 2; app->last_return_value = 2;
} }
t_connector which_connector_type(t_worddesc *token) t_redirect *t_redirect_add_back(t_redirect **init, t_redirect *back)
{ {
t_redirect *tmp;
return ((t_connector)0); if (!(*init))
{
*init = back;
return (*init);
}
tmp = *init;
while (tmp->next)
tmp = tmp->next;
tmp->next = back;
return (*init);
} }
t_cmd *minishell_simple_lst_parse(t_minishell *app, t_cmd_builder *builder) t_cmd *minishell_simple_lst_parse(t_minishell *app, t_cmd_builder *builder)
@ -40,49 +53,87 @@ t_cmd *minishell_simple_lst_parse(t_minishell *app, t_cmd_builder *builder)
t_redirect *minishell_redir_parse(t_minishell *app, t_cmd_builder *builder) t_redirect *minishell_redir_parse(t_minishell *app, t_cmd_builder *builder)
{ {
ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word); ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word);
t_cmd *subtree;
// subtree = minishell_group_or_smp_parse(app, builder);
return ((t_redirect *)NULL); return ((t_redirect *)NULL);
} }
#define FT_TOKENTYPE_WORD 65
/*
** TODO
*/
t_cmd *minishell_simple_parse(t_minishell *app, t_cmd_builder *builder) t_cmd *minishell_simple_parse(t_minishell *app, t_cmd_builder *builder)
{ {
ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word); ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word);
t_cmd *subtree; t_cmd *simple;
t_worddesc *old_worddesc;
subtree = minishell_redir_parse(app, builder); simple = ft_calloc(1, sizeof(t_cmd));
if (!simple)
// a virer {
builder->tokens->word->token_type = FT_TOKENTYPE_WORD; ft_errno(FT_ENOMEM);
return (NULL);
if (builder->tokens->word->token_type != FT_TOKENTYPE_WORD) }
return (parse_error(app, builder->tokens->word), NULL); simple->type = FT_SIMPLE;
simple->value.simple = ft_calloc(1, sizeof(t_simple_cmd));
old_worddesc = wordlist_pop(builder->tokens); if (simple->value.simple == NULL)
{
ft_errno(FT_ENOMEM);
return (NULL);
subtree = minishell_redir_parse(app, builder); }
////////////////
return (subtree); return (simple);
} }
t_cmd *minishell_opt_pipeline_parse(t_minishell *app, t_cmd_builder *builder) t_cmd *minishell_opt_pipeline_parse(t_minishell *app, t_cmd_builder *builder)
{ {
return ((t_cmd *)NULL); t_cmd *opt;
t_worddesc *token;
token = builder->tokens->word;
if (token->token_type == PIPE_TOKEN)
{
token = wordlist_pop(builder->tokens);
worddesc_destroy(token);
opt = minishell_group_or_smp_parse(app, builder);
if (!opt)
ft_errno(FT_EERRNO);
return (opt);
}
return (NULL);
} }
/*
** TODO NORM -> function is too long
*/
t_cmd *minishell_group_or_smp_parse(t_minishell *app, t_cmd_builder *builder) t_cmd *minishell_group_or_smp_parse(t_minishell *app, t_cmd_builder *builder)
{ {
ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word); ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word);
t_cmd *subtree; t_cmd *subtree;
t_cmd *group;
if (builder->tokens->word->token_type == OPEN_PARENTH_TOKEN)
{
worddesc_destroy(wordlist_pop(builder->tokens));
subtree = minishell_cmds_parse(app, builder);
if (!subtree
|| builder->tokens->word->token_type != CLOSE_PARENTH_TOKEN)
{
ft_errno(FT_EERRNO);
if (builder->tokens->word->token_type != CLOSE_PARENTH_TOKEN)
parse_error(app, builder->tokens->word);
return (t_cmd_destroy(subtree), NULL);
}
worddesc_destroy(wordlist_pop(builder->tokens));
group = ft_calloc(1, sizeof(t_cmd));
if (!group)
return (ft_errno(FT_ENOMEM), t_cmd_destroy(subtree), NULL);
group->value.group->cmd = subtree;
group->value.group->redirects = minishell_redir_parse(app, builder);
if (group->value.group->redirects == NULL && ft_errno_get() != FT_ESUCCESS)
return (t_cmd_destroy(group), NULL);
return (group);
}
subtree = minishell_simple_parse(app, builder); subtree = minishell_simple_parse(app, builder);
if (!subtree)
ft_errno(FT_EERRNO);
return (subtree); return (subtree);
} }
@ -91,37 +142,44 @@ t_cmd *minishell_opt_cmds_parse(t_minishell *app, t_cmd_builder *builder,
{ {
ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word); ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word);
t_worddesc *token;
t_cmd *opt;
return ((t_cmd *)NULL); token = builder->tokens->word;
if (token->token_type == OR_TOKEN || token->token_type == AND_TOKEN)
{
if (token->token_type == OR_TOKEN)
*connec = FT_OR;
else
*connec = FT_AND;
token = wordlist_pop(builder->tokens);
worddesc_destroy(token);
opt = minishell_pipeline_parse(app, builder);
if (!opt)
ft_errno(FT_EERRNO);
return (opt);
}
return (NULL);
} }
t_cmd *minishell_pipeline_parse(t_minishell *app, t_cmd_builder *builder) static t_cmd *connec_reorient_subtree(t_cmd **list, t_cmd **subtree,
{
ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word);
t_cmd *subtree;
subtree = minishell_group_or_smp_parse(app, builder);
return (subtree);
}
static t_cmd *cmds_parse_reorient_subtree(t_cmd **list, t_cmd **subtree,
t_cmd **opt, t_connector connec) t_cmd **opt, t_connector connec)
{ {
*list = ft_calloc(1, sizeof(t_cmd)); *list = ft_calloc(1, sizeof(t_cmd));
if (!*list) if (!*list)
{ {
ft_errno(FT_ENOMEM); ft_errno(FT_ENOMEM);
return (ft_perror("minishell_cmds_parse"), NULL); t_cmd_destroy(*subtree);
t_cmd_destroy(*opt);
return (NULL);
} }
(*list)->value.connection = ft_calloc(1, sizeof(t_connec_cmd)); (*list)->value.connection = ft_calloc(1, sizeof(t_connec_cmd));
if (!(*list)->value.connection) if (!(*list)->value.connection)
{ {
ft_errno(FT_ENOMEM); ft_errno(FT_ENOMEM);
return (ft_perror("minishell_cmds_parse"), NULL); t_cmd_destroy(*subtree);
t_cmd_destroy(*opt);
return (free(*list), NULL);
} }
(*list)->type = FT_CONNECTION; (*list)->type = FT_CONNECTION;
(*list)->value.connection->connector = connec; (*list)->value.connection->connector = connec;
@ -131,6 +189,36 @@ static t_cmd *cmds_parse_reorient_subtree(t_cmd **list, t_cmd **subtree,
return (*subtree); return (*subtree);
} }
t_cmd *minishell_pipeline_parse(t_minishell *app, t_cmd_builder *builder)
{
t_cmd *subtree;
t_cmd *opt;
t_cmd *pipeline;
ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word);
subtree = minishell_group_or_smp_parse(app, builder);
if (!subtree)
return (NULL);
opt = minishell_opt_pipeline_parse(app, builder);
if (!opt && ft_errno_get() != FT_ESUCCESS)
return (NULL);
if (!opt)
return (subtree);
while (opt)
{
if (connec_reorient_subtree(&pipeline, &subtree, &opt, FT_PIPE) == NULL)
{
app->last_return_value = 1;
return (ft_perror("minishell_pipeline_parse"), NULL);
}
opt = minishell_opt_pipeline_parse(app, builder);
}
if (ft_errno_get() != FT_ESUCCESS)
return (t_cmd_destroy(subtree), NULL);
return (pipeline);
}
/* /*
** Parse list of commands or pipeline. ** Parse list of commands or pipeline.
*/ */
@ -143,31 +231,30 @@ t_cmd *minishell_cmds_parse(t_minishell *app, t_cmd_builder *builder)
t_connector connec; t_connector connec;
ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word); ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word);
subtree = minishell_pipeline_parse(app, builder); subtree = minishell_pipeline_parse(app, builder);
if (!subtree) if (!subtree)
return (NULL); //ft_errno?
opt = minishell_opt_cmds_parse(app, builder, &connec);
if (!opt && ft_errno_get() != FT_ESUCCESS) //error probably already signaled
return (NULL); return (NULL);
if (!opt) //Simple Pipeline of some form opt = minishell_opt_cmds_parse(app, builder, &connec);
if (!opt && ft_errno_get() != FT_ESUCCESS)
return (t_cmd_destroy(subtree), NULL);
if (!opt)
return (subtree); return (subtree);
while (opt) while (opt)
{ {
if (cmds_parse_reorient_subtree(&list, &subtree, &opt, connec) == NULL) if (connec_reorient_subtree(&list, &subtree, &opt, connec) == NULL)
{ {
app->last_return_value = 1; app->last_return_value = 1;
return (NULL); return (ft_perror("minishell_cmds_parse"), NULL);
} }
opt = minishell_opt_cmds_parse(app, builder, &connec); opt = minishell_opt_cmds_parse(app, builder, &connec);
} }
if (ft_errno_get() != FT_ESUCCESS) if (ft_errno_get() != FT_ESUCCESS)
return (NULL); return (destroy(subtree), NULL);
return (list); return (list);
} }
/* /*
** TODO Include destruction of builder contents if necessary. ** TODO check if we need to differentiate the cause of a NULL return.
*/ */
t_cmd *minishell_parse(t_minishell *app, char *command_line) t_cmd *minishell_parse(t_minishell *app, char *command_line)
{ {
@ -175,6 +262,7 @@ t_cmd *minishell_parse(t_minishell *app, char *command_line)
t_cmd *root_cmd; t_cmd *root_cmd;
ft_errno(FT_ESUCCESS); ft_errno(FT_ESUCCESS);
app->last_return_value = 0;
if (!command_line) if (!command_line)
return (NULL); return (NULL);
builder.tokens = minishell_wordsplit(command_line); builder.tokens = minishell_wordsplit(command_line);
@ -182,11 +270,17 @@ t_cmd *minishell_parse(t_minishell *app, char *command_line)
return (NULL); return (NULL);
root_cmd = minishell_cmds_parse(app, &builder); root_cmd = minishell_cmds_parse(app, &builder);
if (!root_cmd) if (!root_cmd)
return (NULL); // destroy builder contents? {
if (ft_errno_get() != FT_ESUCCESS && !app->last_return_value)
app->last_return_value = 1;
return (wordlist_destroy(builder.tokens), NULL);
}
if (builder.tokens) if (builder.tokens)
{ {
parse_error(app, builder.tokens->word); parse_error(app, builder.tokens->word);
return (NULL); /////// destroy builder contents? wordlist_destroy(builder.tokens);
t_cmd_destroy(root_cmd);
return (NULL);
} }
return (root_cmd); return (root_cmd);
} }

View file

@ -6,7 +6,7 @@
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */ /* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/28 17:49:55 by jguelen #+# #+# */ /* Created: 2025/03/28 17:49:55 by jguelen #+# #+# */
/* Updated: 2025/04/07 17:59:08 by jguelen ### ########.fr */ /* Updated: 2025/04/08 13:55:42 by jguelen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */ /* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/27 13:57:44 by khais #+# #+# */ /* Created: 2025/03/27 13:57:44 by khais #+# #+# */
/* Updated: 2025/04/07 18:40:53 by jguelen ### ########.fr */ /* Updated: 2025/04/08 16:25:37 by jguelen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -25,6 +25,21 @@
# define W_HASQUOTEDNULL 0b10000 /* word contains a quoted null character */ # define W_HASQUOTEDNULL 0b10000 /* word contains a quoted null character */
# define W_DQUOTE 0b100000 /* word should be treated as if double-quoted */ # define W_DQUOTE 0b100000 /* word should be treated as if double-quoted */
typedef enum e_token_type
{
NOT_TOKEN,
WORD_TOKEN,
OPEN_PARENTH_TOKEN,
CLOSE_PARENTH_TOKEN,
PIPE_TOKEN,
OR_TOKEN,
AND_TOKEN,
OUT_TRUNC_REDIR_TOKEN,
OUT_APPEND_REDIR_TOKEN,
IN_REDIR_TOKEN,
HERE_DOC_REDIR_TOKEN,
} t_token_type;
/* /*
** A logical word for the parser. ** A logical word for the parser.
** **
@ -37,13 +52,13 @@ typedef struct s_worddesc
/* /*
** The word itself ** The word itself
*/ */
char *word; char *word;
/* /*
** flags for this word ** flags for this word
**s_worddesc **s_worddesc
** See above for flag definitions ** See above for flag definitions
*/ */
char flags; char flags;
/* /*
** a character mask for word to designate the status ** a character mask for word to designate the status
** of its characters and wether or not they are subject to modifications ** of its characters and wether or not they are subject to modifications
@ -55,8 +70,8 @@ typedef struct s_worddesc
** '"' corresponding character is double-quoted ** '"' corresponding character is double-quoted
** '$' corresponding character is a result of $var expansion ** '$' corresponding character is a result of $var expansion
*/ */
char *marker; char *marker;
int token_type; t_token_type token_type;
} t_worddesc; } t_worddesc;
t_worddesc *worddesc_create(char *word, char flags, char *marker); t_worddesc *worddesc_create(char *word, char flags, char *marker);