Minishell
1.0
42 School Minishell Project - A simple shell implementation
Yüklüyor...
Arıyor...
Eşleşme Yok
main.c
Bu dosyanın dokümantasyonuna git.
1
/* ************************************************************************** */
2
/* */
3
/* ::: :::::::: */
4
/* main.c :+: :+: :+: */
5
/* +:+ +:+ +:+ */
6
/* By: hgenc@student.42kocaeli.com.tr <hgenc@s +#+ +:+ +#+ */
7
/* +#+#+#+#+#+ +#+ */
8
/* Created: 2025/11/13 15:28:14 by bkinali #+# #+# */
9
/* Updated: 2025/11/29 23:46:09 by hgenc. ### ########.fr */
10
/* */
11
/* ************************************************************************** */
12
13
/**
14
* @file main.c
15
* @brief Minishell ana dosyası
16
*
17
* Shell başlatma, ana döngü ve temizlik işlemleri.
18
* REPL (Read-Eval-Print Loop) döngüsünü yönetir.
19
*/
20
21
#include "
../include/minishell.h
"
22
#include <readline/history.h>
23
#include <readline/readline.h>
24
#include <stdio.h>
// printf
25
#include <stdlib.h>
// free
26
#include <unistd.h>
// STDOUT_FILENO
27
28
/**
29
* @brief Shell yapısını başlatır
30
* @param shell Shell yapısı pointer'ı
31
* @param envp Ortam değişkenleri dizisi
32
*
33
* Shell'i sıfırlar, exit_status'u ayarlar ve ortam değişkenlerini yükler.
34
*/
35
int
init_shell
(
t_shell
*shell,
char
**envp)
36
{
37
ft_bzero(shell,
sizeof
(
t_shell
));
38
shell->
exit_status
= 0;
39
shell->
envp
= envp;
40
if
(
init_env_list
(shell) == -1)
41
return
(0);
42
return
(1);
43
}
44
45
/**
46
* @brief Tek bir komut satırını işler
47
* @param shell Shell yapısı pointer'ı
48
*
49
* Pipeline: Lexer -> Expander -> Parser -> Executor
50
* Her adımda başarısızlık durumunda döner.
51
*/
52
static
void
process_line
(
t_shell
*shell)
53
{
54
if
(shell->
cmd_line
[0] ==
'\0'
)
55
return ;
56
add_history(shell->
cmd_line
);
57
if
(!
lexer
(shell))
58
{
59
ft_lstclear(&shell->
token_list
, &
free_token_content
);
60
return ;
61
}
62
if
(!
expander
(shell))
63
{
64
ft_lstclear(&shell->
token_list
, &
free_token_content
);
65
return ;
66
}
67
merge_adjacent_tokens
(shell);
68
if
(
parser
(shell))
69
{
70
executor
(shell);
71
clean_ast
(shell->
ast_root
);
72
shell->
ast_root
= NULL;
73
}
74
ft_lstclear(&shell->
token_list
, &
free_token_content
);
75
}
76
77
78
79
/**
80
* @brief Program giriş noktası
81
* @param argc Argüman sayısı (kullanılmıyor)
82
* @param argv Argüman dizisi (kullanılmıyor)
83
* @param envp Ortam değişkenleri
84
* @return int Çıkış durumu
85
*
86
* REPL döngüsü:
87
* 1. readline ile girdi al
88
* 2. process_line ile işle
89
* 3. Ctrl-D (EOF) gelene kadar tekrarla
90
*/
91
int
main
(
int
argc,
char
**argv,
char
**envp)
92
{
93
t_shell
shell;
94
95
(void)argv;
96
if
(argc > 1)
97
{
98
ft_putendl_fd(
"minishell: too many arguments"
, 2);
99
return
(1);
100
}
101
if
(!
init_shell
(&shell, envp))
102
return
(1);
103
setup_signals
();
104
shell.
cmd_line
=
""
;
105
while
(shell.
cmd_line
!= NULL)
106
{
107
g_readline_active
= 1;
108
shell.
cmd_line
= readline(
"minishell$ "
);
109
g_readline_active
= 0;
110
if
(
g_signal
)
111
{
112
shell.
exit_status
= 128 +
g_signal
;
113
g_signal
= 0;
114
}
115
if
(!shell.
cmd_line
)
116
break ;
117
if
(!
is_input_complete
(shell.
cmd_line
))
118
{
119
shell.
cmd_line
=
complete_input
(&shell, shell.
cmd_line
);
120
if
(!shell.
cmd_line
)
121
break ;
122
}
123
process_line
(&shell);
124
free(shell.
cmd_line
);
125
}
126
cleanup_shell
(&shell);
127
ft_putendl_fd(
"exit"
, STDOUT_FILENO);
128
return
(shell.
exit_status
);
129
}
cleanup_shell
void cleanup_shell(t_shell *shell)
Tüm t_shell kaynaklarını serbest bırakır.
Definition
cleanup.c:51
init_env_list
int init_env_list(t_shell *shell)
Ortam değişkenleri listesini başlatır.
Definition
env_initialization.c:148
executor
int executor(t_shell *shell)
Ana executor entry point.
Definition
executor.c:52
expander
int expander(t_shell *shell)
Expander modülünün giriş noktası ve ana döngüsü.
Definition
expander.c:94
complete_input
char * complete_input(t_shell *shell, char *line)
Eksik girdiyi tamamlamak için ek satırlar okur.
Definition
input_completion.c:94
is_input_complete
int is_input_complete(char *line)
Girdi satırının tamamlanıp tamamlanmadığını kontrol eder.
Definition
input_completion.c:40
lexer
int lexer(t_shell *shell)
Lexer ana fonksiyonu — komut satırını token'lara ayırır.
Definition
lexer.c:60
merge_adjacent_tokens
void merge_adjacent_tokens(t_shell *shell)
Definition
lexer_token_utils.c:76
main
int main(int argc, char **argv, char **envp)
Program giriş noktası
Definition
main.c:91
process_line
static void process_line(t_shell *shell)
Tek bir komut satırını işler.
Definition
main.c:52
init_shell
int init_shell(t_shell *shell, char **envp)
Shell yapısını başlatır.
Definition
main.c:35
minishell.h
Minishell ana header dosyası
t_shell
struct s_shell t_shell
parser
int parser(t_shell *shell)
Parser ana fonksiyonu.
Definition
parser.c:141
clean_ast
void clean_ast(t_ast *node)
AST ağacını recursive olarak temizler.
Definition
parser_cleanup.c:72
g_readline_active
volatile sig_atomic_t g_readline_active
Definition
signals.c:33
g_signal
volatile sig_atomic_t g_signal
Definition
signals.c:27
setup_signals
void setup_signals(void)
Definition
signals.c:58
s_shell::ast_root
t_ast * ast_root
Definition
minishell.h:150
s_shell::envp
char ** envp
Definition
minishell.h:151
s_shell::token_list
t_list * token_list
Definition
minishell.h:148
s_shell::cmd_line
char * cmd_line
Definition
minishell.h:152
s_shell::exit_status
int exit_status
Definition
minishell.h:153
free_token_content
void free_token_content(void *content)
Definition
utils.c:36
src
main.c
Oluşturan
1.16.1