-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompt.c
89 lines (82 loc) · 2.16 KB
/
prompt.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
84
85
86
87
88
89
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* prompt.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vilibert <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/19 09:36:00 by vilibert #+# #+# */
/* Updated: 2024/01/29 10:53:37 by vilibert ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int g_signal;
/**
* @brief Get the prompt object
*
* @param data
* @return char*
*/
char *get_prompt(t_data *data)
{
char *tmp;
char *cwd;
char *prompt;
prompt = ft_strjoin(get_env_var(data->env, "USER"), " ");
if (!prompt)
return (ft_strdup("minishell % "));
cwd = getcwd(NULL, 0);
if (cwd)
{
tmp = ft_strrchr(cwd, '/');
tmp = ft_strjoin(prompt, ++tmp);
free(cwd);
free(prompt);
}
else
tmp = prompt;
if (!tmp)
ft_crash(data);
prompt = ft_strjoin(tmp, " % ");
free(tmp);
if (!prompt)
ft_crash(data);
return (prompt);
}
static void prompt_reader_handler(t_data *data)
{
expander(data);
parse(data);
if (data->exec && data->exec[0].argv[0])
executer(data);
else
data->status = 0;
}
/**
* @brief main while for each command cycle
*
* @param data
* @return int always NULL
*/
int prompt_reader(t_data *data)
{
int again;
char *prompt;
again = 1;
while (again)
{
prompt = get_prompt(data);
data->line = readline(prompt);
free(prompt);
if (!data->line)
ft_crash(data);
if (!check_syntax_error(data, data->line))
lexer(data, &(data->line));
add_history(data->line);
if (g_signal != SIGINT && data->list
&& !syntax_checker(data, data->list))
prompt_reader_handler(data);
ft_free_cycle(data);
}
return (0);
}