Minishell 1.0
42 School Minishell Project - A simple shell implementation
Yüklüyor...
Arıyor...
Eşleşme Yok
executor_heredoc_utils.c
Bu dosyanın dokümantasyonuna git.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* executor_heredoc_utils.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: hgenc <hgenc@student.42kocaeli.com.tr> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2026/02/16 18:00:00 by hgenc #+# #+# */
9/* Updated: 2026/02/16 18:00:00 by hgenc ### ########.fr */
10/* */
11/* ************************************************************************** */
12
14#include <fcntl.h>
15#include <stdlib.h>
16#include <unistd.h>
17
18/**
19 * @brief Heredoc için pipe oluştur
20 * @param pipefd Pipe file descriptors
21 * @return int başarı: 1, hata: 0
22 */
23int create_heredoc_pipe(int *pipefd)
24{
25 if (!pipefd)
26 return (0);
27 if (pipe(pipefd) < 0)
28 {
29 shell_error(NULL, NULL, "pipe failed");
30 return (0);
31 }
32 return (1);
33}
34
35/**
36 * @brief Heredoc satırını pipe'a yaz
37 * @param fd File descriptor
38 * @param line Yazılacak satır
39 * @return int başarı: 1, hata: 0
40 */
41int write_heredoc_line(int fd, char *line)
42{
43 if (fd < 0 || !line)
44 return (0);
45 ft_putstr_fd(line, fd);
46 ft_putchar_fd('\n', fd);
47 return (1);
48}
49
50/**
51 * @brief Tek karakteri string sonuna ekle
52 */
53char *append_char(char *str, char c)
54{
55 char buf[2];
56 char *new_str;
57
58 buf[0] = c;
59 buf[1] = '\0';
60 new_str = ft_strjoin(str, buf);
61 free(str);
62 return (new_str);
63}
64
65/**
66 * @brief Tek bir $VAR genişletmesi yap ve result'a ekle
67 * @return char* Güncellenmiş result string
68 */
69char *expand_dollar(t_shell *shell, char *line, char *result, int *i)
70{
71 int name_len;
72 char *var_name;
73 char *var_value;
74 char *tmp;
75
76 name_len = get_var_name_len(line + *i + 1);
77 if (name_len <= 0)
78 return (append_char(result, line[(*i)++]));
79 var_name = ft_substr(line + *i + 1, 0, name_len);
80 var_value = get_var_value(shell, var_name);
81 free(var_name);
82 if (var_value)
83 {
84 tmp = ft_strjoin(result, var_value);
85 free(result);
86 free(var_value);
87 result = tmp;
88 }
89 *i += name_len + 1;
90 return (result);
91}
92
93/**
94 * @brief Heredoc satırındaki $VAR değişkenlerini genişlet
95 * @param shell Shell yapısı (env değişkenleri için)
96 * @param line Genişletilecek satır
97 * @return char* Yeni genişletilmiş satır (malloc), veya orijinal line
98 */
99char *expand_heredoc_line(t_shell *shell, char *line)
100{
101 char *result;
102 int i;
103
104 result = ft_strdup("");
105 if (!result)
106 return (line);
107 i = 0;
108 while (line[i])
109 {
110 if (line[i] == '$' && can_expand(line[i + 1]))
111 result = expand_dollar(shell, line, result, &i);
112 else
113 result = append_char(result, line[i++]);
114 }
115 free(line);
116 return (result);
117}
void shell_error(char *cmd, char *arg, char *msg)
char * append_char(char *str, char c)
Tek karakteri string sonuna ekle.
int create_heredoc_pipe(int *pipefd)
Heredoc için pipe oluştur.
char * expand_heredoc_line(t_shell *shell, char *line)
Heredoc satırındaki $VAR değişkenlerini genişlet.
char * expand_dollar(t_shell *shell, char *line, char *result, int *i)
Tek bir $VAR genişletmesi yap ve result'a ekle.
int write_heredoc_line(int fd, char *line)
Heredoc satırını pipe'a yaz.
char * get_var_value(t_shell *shell, char *var_name)
Bir değişkenin değerini bulur ve kopyasını döndürür.
int get_var_name_len(char *str)
Değişken isminin uzunluğunu hesaplar.
int can_expand(char next_c)
'$' işaretinden sonra gelen karakterin genişletmeye uygun olup olmadığını kontrol eder.
Minishell ana header dosyası
struct s_shell t_shell