aboutsummaryrefslogtreecommitdiff
path: root/src/format_string.c
blob: 06e52105583a402728012d0eb3e4a96a5ab51e35 (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   format_string.c                                    :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2024/12/17 19:30:11 by chuhlig           #+#    #+#             */
/*   Updated: 2025/01/23 14:34:35 by chuhlig          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "get_next_line.h"
#include "libft.h"
#include "minishell.h"

void	append_var_exit_code(char **dst, t_env *env)
{
	char	*exit_code;
	char	*result;

	exit_code = env_get(env, "?");
	if (exit_code)
	{
		result = ft_strjoin(*dst, exit_code);
		free(*dst);
		*dst = result;
	}
}

static void	append_slice(char **dst, char *src, int start, int end)
{
	char	*result;
	int		len;
	int		i;

	if (*dst != NULL)
		len = ft_strlen(*dst);
	else
		len = 0;
	result = malloc(len + (end - start) + 1);
	if (!result)
		return ;
	ft_strncpy(result, *dst, len);
	i = 0;
	while (start + i < end)
	{
		result[len + i] = src[start + i];
		i++;
	}
	result[len + i] = '\0';
	// if (*dst != NULL)
	// 	free(*dst);
	*dst = result;
	// free(src);
	// src = *dst;
}

static void	append_var(char **dst, char *src, int *pos, t_env *env)
{
	int		i;
	char	*var;
	char	*value;
	char	*result;

	i = 0;
	*pos += 1;
	while (ft_isalnum(src[*pos + i]) || src[*pos + i] == '_')
		i++;
	if (i == 0)
		return ;
	var = ft_substr(src, *pos, i);
	value = env_get(env, var);
	if (value)
	{
		result = ft_strjoin(*dst, value);
		free(*dst);
		*dst = result;
	}
	*pos += i;
	free(var);
}

static void	handle_dollar_sign(char **result, char *str, int *pos, t_env *env)
{
	if (str[*pos + 1] == '?')
	{
		append_var_exit_code(result, env);
		*pos += 2;
	}
	else if (ft_isalnum(str[*pos + 1]) || str[*pos + 1] == '_')
		append_var(result, str, pos, env);
	else
	{
		append_slice(result, str, *pos, *pos + 1);
		(*pos)++;
	}
}

char	*format_string(char *str, t_env *env, int is_literal)
{
	char	*result;
	int		pos;
	int		start;

	pos = 0;
	start = 0;
	result = NULL;
	if (!str)
		return (NULL);
	while (str[pos])
	{
		if (str[pos] == '\'' || (str[pos] == '\"' && !is_literal)
			|| (str[pos] == '$' && !is_literal))
		{
			append_slice(&result, str, start, pos);
			if (str[pos] == '$')
				handle_dollar_sign(&result, str, &pos, env);
			else
				is_literal ^= (str[pos++] == '\'');
			start = pos;
			continue ;
		}
		pos++;
	}
	return (append_slice(&result, str, start, pos), result);
}