-
Notifications
You must be signed in to change notification settings - Fork 1
/
interactive_shell.c
83 lines (75 loc) · 1.69 KB
/
interactive_shell.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
#include "minishell.h"
static void execute_seqcmd(t_parse_ast *cmdline)
{
t_parse_ast *seqcmd;
seqcmd = cmdline->content.command_line->seqcmd_node;
set_sighandlers_during_execution();
invoke_sequential_commands(seqcmd);
if (g_shell.signal_child_received)
{
if (g_shell.signal_child_received == SIGQUIT)
write(STDOUT_FILENO, "Quit (core dumped)", 18);
write(STDOUT_FILENO, "\n", 1);
set_status(128 + g_shell.signal_child_received);
}
set_shell_sighandlers();
}
static t_parse_ast *get_cmdline_from_input_str(char *input_str)
{
t_token tok;
t_parse_buffer buf;
t_parse_ast *cmdline;
init_buffer_with_string(&buf, input_str);
buf.size++;
buf.buffer[ft_strlen(input_str)] = '\n';
lex_init_token(&tok);
lex_get_token(&buf, &tok);
cmdline = parse_command_line(&buf, &tok);
free(tok.text);
return (cmdline);
}
static bool is_invalid_input_str(char *input_str)
{
while (*input_str)
{
if (*input_str & 0x80 || (unsigned char)*input_str > ' ')
return (true);
input_str++;
}
return (false);
}
static void show_parse_err(char *input_str)
{
if (is_invalid_input_str(input_str))
{
put_err_msg("Parse error.");
set_status(1);
}
}
int interactive_shell(void)
{
char *input_str;
t_parse_ast *cmdline;
set_shell_sighandlers();
input_str = readline(MINISHELL_PROMPT);
while (input_str)
{
if (*input_str)
add_history(input_str);
cmdline = get_cmdline_from_input_str(input_str);
if (!cmdline)
show_parse_err(input_str);
else
{
execute_seqcmd(cmdline);
parse_free_all_ast();
}
free(input_str);
input_str = readline(MINISHELL_PROMPT);
}
write(1, "exit\n", 5);
return (0);
}