(enhancement) Printing an error on unclosed quote and corrected a typo.

This commit is contained in:
Jérôme Guélen 2025-04-24 16:55:16 +02:00
parent 3b5df9ec10
commit a86616f910
No known key found for this signature in database
5 changed files with 70 additions and 12 deletions

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* ft_errno.c :+: :+: :+: */ /* ft_errno.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/21 12:40:46 by khais #+# #+# */ /* Created: 2025/02/21 12:40:46 by khais #+# #+# */
/* Updated: 2025/04/17 11:39:56 by khais ### ########.fr */ /* Updated: 2025/04/24 16:49:17 by jguelen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -56,7 +56,10 @@ char *ft_strerror(t_errno errno)
[FT_ENOMEM] = "Cannot allocate memory", [FT_ENOMEM] = "Cannot allocate memory",
[FT_STATFAIL] = "minishell: stat: internal failure", [FT_STATFAIL] = "minishell: stat: internal failure",
[FT_ISDIR] = "Is a directory", [FT_ISDIR] = "Is a directory",
[FT_EHEREDOC_FAILED] = "Failed to aquire here-document", [FT_EHEREDOC_FAILED] = "Failed to acquire here-document",
[FT_EUNCLOSED_DBLE_QUOTE]
= "unexpected EOF while looking for matching `\"'",
[FT_EUNCLOSED_SMPL_QUOTE] = "unexpected EOF while looking for matching `''",
}; };
if (errno >= 0 && errno < FT_EMAXERRNO) if (errno >= 0 && errno < FT_EMAXERRNO)

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* ft_errno.h :+: :+: :+: */ /* ft_errno.h :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:40/12 by khais #+# #+# */ /* Created: 2025/04/17 11:40:12 by khais #+# #+# */
/* Updated: 2025/04/17 11:40:12 by khais ### ########.fr */ /* Updated: 2025/04/24 16:45:56 by jguelen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,6 +26,8 @@ typedef enum e_errno
FT_STATFAIL, FT_STATFAIL,
FT_ISDIR, FT_ISDIR,
FT_EHEREDOC_FAILED, FT_EHEREDOC_FAILED,
FT_EUNCLOSED_DBLE_QUOTE,
FT_EUNCLOSED_SMPL_QUOTE,
FT_EMAXERRNO, FT_EMAXERRNO,
} t_errno; } t_errno;

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* cmd_parsing.c :+: :+: :+: */ /* cmd_parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: khais <marvin@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/17 11:55:27 by khais ### ########.fr */ /* Updated: 2025/04/24 16:39:14 by jguelen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -40,6 +40,16 @@ static void parse_error_newline(t_minishell *app)
worddesc_destroy(token); worddesc_destroy(token);
} }
static t_cmd *expecting_quote_error(t_minishell *app)
{
if (ft_errno_get() != FT_ESUCCESS)
{
app->last_return_value = 2;
ft_perror("minishell");
}
return (NULL);
}
/* /*
** TODO check if we need to differentiate the cause of a NULL return. ** TODO check if we need to differentiate the cause of a NULL return.
*/ */
@ -53,7 +63,7 @@ t_cmd *minishell_parse(t_minishell *app, char *command_line)
return (NULL); return (NULL);
tokens = minishell_wordsplit(command_line); tokens = minishell_wordsplit(command_line);
if (!tokens) if (!tokens)
return (NULL); return (expecting_quote_error(app));
root_cmd = minishell_cmds_parse(app, &tokens); root_cmd = minishell_cmds_parse(app, &tokens);
if (root_cmd == NULL && ft_errno_get() != FT_ESUCCESS) if (root_cmd == NULL && ft_errno_get() != FT_ESUCCESS)
{ {

View file

@ -3,16 +3,25 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* wordsplit.c :+: :+: :+: */ /* wordsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/21 12:29:36 by khais #+# #+# */ /* Created: 2025/02/21 12:29:36 by khais #+# #+# */
/* Updated: 2025/02/21 12:42:46 by khais ### ########.fr */ /* Updated: 2025/04/24 16:48:19 by jguelen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../../ft_errno.h"
#include "wordsplit.h" #include "wordsplit.h"
#include "libft.h" #include "libft.h"
static void set_quote_error(char quote)
{
if (quote == '"')
ft_errno(FT_EUNCLOSED_DBLE_QUOTE);
else if (quote == '\'')
ft_errno(FT_EUNCLOSED_SMPL_QUOTE);
}
/* /*
** split a string into words, respecting quotes etc. ** split a string into words, respecting quotes etc.
** **
@ -47,6 +56,7 @@ t_wordlist *minishell_wordsplit(char *original)
rule_new_word(&token_build, original); rule_new_word(&token_build, original);
} }
if (token_build.quote != '\0') if (token_build.quote != '\0')
return (wordlist_destroy(token_build.wordlist), NULL); return (wordlist_destroy(token_build.wordlist),
set_quote_error(token_build.quote), NULL);
return (token_build.wordlist); return (token_build.wordlist);
} }

33
test.sh
View file

@ -977,4 +977,37 @@ minishell: /root/outfile: Permission denied
1 1
EOF EOF
when_run <<"EOF" "print error when unclosed quotes"
echo " kjlhgsaf
echo $?
echo ' HGIFUY
echo $?
echo '"HGIFUY"
echo $?
echo '"HGIFUY
echo $?
echo "'HGIFUY
echo $?
echo "' HGI'FUY
echo $?
echo boubou
echo $?
EOF
expecting <<"EOF"
minishell: unexpected EOF while looking for matching `"'
2
minishell: unexpected EOF while looking for matching `''
2
minishell: unexpected EOF while looking for matching `''
2
minishell: unexpected EOF while looking for matching `''
2
minishell: unexpected EOF while looking for matching `"'
2
minishell: unexpected EOF while looking for matching `"'
2
boubou
0
EOF
finalize finalize