Skip to content

Commit dc336eb

Browse files
authored
Merge pull request #270 from cacapon/feature/255-return-failed-redirect-sts
リダイレクトエラー時にminishのlast-statusにセットするように修正
2 parents e25d984 + 39b4dbf commit dc336eb

File tree

4 files changed

+20
-23
lines changed

4 files changed

+20
-23
lines changed

inc/tokenizer/tokenizer.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/04/15 12:36:01 by ttsubo #+# #+# */
9-
/* Updated: 2025/04/17 15:13:49 by ttsubo ### ########.fr */
9+
/* Updated: 2025/05/17 16:18:06 by ttsubo ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#ifndef TOKENIZER_H
1414
# define TOKENIZER_H
1515

1616
# include "libft.h"
17+
# include "common.h"
1718
# include <stdio.h>
1819

1920
# define ERR_UNEXPECTED_STR "unexpected error occureed.\n"
@@ -59,7 +60,7 @@ char *read_token(t_tokenizer *tkn);
5960
void show_tokenizer_error(t_tokenizer_errors err_code);
6061
size_t get_token_capa(char *str);
6162
int is_quote_closed(char *str);
62-
int is_redirect_validate(char *str);
63-
char **tokenizer(char *str);
63+
int is_redirect_validate(char *str, t_minish *minish);
64+
char **tokenizer(char *str, t_minish *minish);
6465

6566
#endif

src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static bool prompt(t_minish *minish)
5858
if (res == READ_EMPTY)
5959
return (free_str(&line), true);
6060
_set_g_sig_sts(minish);
61-
tokens = tokenizer(line);
61+
tokens = tokenizer(line, minish);
6262
cmds = parser(tokens, minish);
6363
if (!cmds)
6464
return (free_prompt(&tokens, &cmds, &line), true);

src/tokenizer/is_redirect_validate.c

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,32 @@
66
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/04/15 13:59:15 by ttsubo #+# #+# */
9-
/* Updated: 2025/04/27 15:03:26 by ttsubo ### ########.fr */
9+
/* Updated: 2025/05/17 16:19:18 by ttsubo ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include "tokenizer.h"
1414

15+
static void _show_err_and_set_sts(t_tokenizer_errors err_code, t_minish *minish)
16+
{
17+
show_tokenizer_error(err_code);
18+
minish->last_status = 2;
19+
}
20+
1521
/**
1622
* @brief Checks if the redirect symbol is a valid value.
1723
* @param str
1824
* @return int
1925
* @note For now, this function only checks for consecutive redirect symbols.
2026
*/
21-
int is_redirect_validate(char *str)
27+
int is_redirect_validate(char *str, t_minish *minish)
2228
{
2329
while (*str)
2430
{
25-
if (!ft_strncmp(str, ">>>", 3))
26-
return (show_tokenizer_error(ERR_UNEXPECTED_TOKEN_R1), 0);
27-
else if (!ft_strncmp(str, "<<<", 3))
28-
return (show_tokenizer_error(ERR_UNEXPECTED_TOKEN_L1), 0);
29-
if (*str == '>')
30-
{
31-
if (*(str + 1) == '<')
32-
return (show_tokenizer_error(ERR_UNEXPECTED_TOKEN_L1), 0);
33-
}
34-
if (*str == '<')
35-
{
36-
if (*(str + 1) == '>')
37-
return (show_tokenizer_error(ERR_UNEXPECTED_TOKEN_R1), 0);
38-
}
31+
if (!ft_strncmp(str, ">>>", 3) || (*str == '<' && *(str + 1) == '>'))
32+
return (_show_err_and_set_sts(ERR_UNEXPECTED_TOKEN_R1, minish), 0);
33+
if (!ft_strncmp(str, "<<<", 3) || (*str == '>' && *(str + 1) == '<'))
34+
return (_show_err_and_set_sts(ERR_UNEXPECTED_TOKEN_L1, minish), 0);
3935
str++;
4036
}
4137
return (1);

src/tokenizer/tokenizer.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/04/15 12:35:38 by ttsubo #+# #+# */
9-
/* Updated: 2025/04/29 14:58:23 by ttsubo ### ########.fr */
9+
/* Updated: 2025/05/17 16:10:10 by ttsubo ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -24,14 +24,14 @@ void skip_spaces(t_tokenizer *tkn)
2424
* @param str
2525
* @return char**
2626
*/
27-
char **tokenizer(char *str)
27+
char **tokenizer(char *str, t_minish *minish)
2828
{
2929
int token_i;
3030
char **tokens;
3131
t_tokenizer tkn;
3232

3333
tkn = (t_tokenizer){.input = str, .pos = 0, .in_squote = 0, .in_dquote = 0};
34-
if (!is_redirect_validate(str))
34+
if (!is_redirect_validate(str, minish))
3535
return (NULL);
3636
tokens = ft_calloc(get_token_capa(str) + 1, sizeof(char *));
3737
if (!tokens)

0 commit comments

Comments
 (0)