Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion inc/common/struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 17:10:22 by dayano #+# #+# */
/* Updated: 2025/05/04 19:16:10 by ttsubo ### ########.fr */
/* Updated: 2025/05/08 16:53:17 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -15,6 +15,13 @@

# include <sys/wait.h>

typedef enum e_read_result
{
READ_EOF,
READ_EMPTY,
READ_OK
} t_read_result;

typedef enum e_redir_type
{
REDIR_NONE,
Expand Down
4 changes: 3 additions & 1 deletion inc/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/03 12:50:38 by ttsubo #+# #+# */
/* Updated: 2025/05/04 12:26:20 by ttsubo ### ########.fr */
/* Updated: 2025/05/08 16:22:31 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -22,4 +22,6 @@
# include <readline/history.h>
# include <readline/readline.h>

extern volatile sig_atomic_t g_sig_sts;

#endif
43 changes: 31 additions & 12 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/03 12:50:11 by ttsubo #+# #+# */
/* Updated: 2025/05/05 18:56:10 by ttsubo ### ########.fr */
/* Updated: 2025/05/08 17:00:17 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -18,6 +18,26 @@ static void destroy_minish(t_minish *minish)
rl_clear_history();
}

static t_read_result _read_user_input(char **line)
{
*line = readline("$ ");
if (!*line)
return (READ_EOF);
if ((*line)[0] == '\0')
return (READ_EMPTY);
add_history(*line);
return (READ_OK);
}

static void _set_g_sig_sts(t_minish *minish)
{
if (g_sig_sts)
{
minish->last_status = g_sig_sts;
g_sig_sts = 0;
}
}

/**
* @brief I used bool type since it seems to be available.
* @param program_name
Expand All @@ -27,18 +47,17 @@ static void destroy_minish(t_minish *minish)
*/
static bool prompt(char *program_name, t_minish *minish)
{
char *line;
char **tokens;
t_cmd **cmds;
char *line;
char **tokens;
t_cmd **cmds;
t_read_result res;

tokens = NULL;
cmds = NULL;
line = readline("$ ");
if (!line)
return (free_prompt(&tokens, &cmds, &line), false);
if (!line[0])
return (free(line), true);
add_history(line);
res = _read_user_input(&line);
if (res == READ_EOF)
return (free_str(&line), false);
if (res == READ_EMPTY)
return (free_str(&line), true);
_set_g_sig_sts(minish);
tokens = tokenizer(line);
cmds = parser(tokens, minish);
if (!cmds)
Expand Down
7 changes: 5 additions & 2 deletions src/minish_signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
/* ::: :::::::: */
/* minish_signal.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dayano <dayano@student.42.fr> +#+ +:+ +#+ */
/* By: ttsubo <ttsubo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/05 13:35:06 by ttsubo #+# #+# */
/* Updated: 2025/05/05 17:07:53 by dayano ### ########.fr */
/* Updated: 2025/05/08 17:00:52 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

#include "minish_signal.h"

volatile sig_atomic_t g_sig_sts = 0;

/**
* @brief Handler function when ctrl+c is pressed
*
Expand All @@ -24,6 +26,7 @@ static void ctrl_c(int signum)
rl_on_new_line();
rl_replace_line("", 0);
rl_redisplay();
g_sig_sts = 128 + signum;
}

/**
Expand Down