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
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 */
35int 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 */
52static 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 }
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 */
91int 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);
104 shell.cmd_line = "";
105 while (shell.cmd_line != NULL)
106 {
108 shell.cmd_line = readline("minishell$ ");
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}
void cleanup_shell(t_shell *shell)
Tüm t_shell kaynaklarını serbest bırakır.
Definition cleanup.c:51
int init_env_list(t_shell *shell)
Ortam değişkenleri listesini başlatır.
int executor(t_shell *shell)
Ana executor entry point.
Definition executor.c:52
int expander(t_shell *shell)
Expander modülünün giriş noktası ve ana döngüsü.
Definition expander.c:94
char * complete_input(t_shell *shell, char *line)
Eksik girdiyi tamamlamak için ek satırlar okur.
int is_input_complete(char *line)
Girdi satırının tamamlanıp tamamlanmadığını kontrol eder.
int lexer(t_shell *shell)
Lexer ana fonksiyonu — komut satırını token'lara ayırır.
Definition lexer.c:60
void merge_adjacent_tokens(t_shell *shell)
int main(int argc, char **argv, char **envp)
Program giriş noktası
Definition main.c:91
static void process_line(t_shell *shell)
Tek bir komut satırını işler.
Definition main.c:52
int init_shell(t_shell *shell, char **envp)
Shell yapısını başlatır.
Definition main.c:35
Minishell ana header dosyası
struct s_shell t_shell
int parser(t_shell *shell)
Parser ana fonksiyonu.
Definition parser.c:141
void clean_ast(t_ast *node)
AST ağacını recursive olarak temizler.
volatile sig_atomic_t g_readline_active
Definition signals.c:33
volatile sig_atomic_t g_signal
Definition signals.c:27
void setup_signals(void)
Definition signals.c:58
t_ast * ast_root
Definition minishell.h:150
char ** envp
Definition minishell.h:151
t_list * token_list
Definition minishell.h:148
char * cmd_line
Definition minishell.h:152
int exit_status
Definition minishell.h:153
void free_token_content(void *content)
Definition utils.c:36