summaryrefslogtreecommitdiffstats
path: root/src/s_init.c
blob: b229cda96c3cac6177612689fffdf135cb922a14 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   s_init.c                                           :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/14 17:19:27 by rbousset          #+#    #+#             */
/*   Updated: 2020/02/14 17:19:29 by rbousset         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <libft.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <dirent.h>
#include <errno.h>

#include "b_export_next.h"
#include "d_define.h"
#include "f_fail.h"
#include "m_funptr.h"
#include "s_init.h"
#include "u_vars.h"
#include "u_vars_next.h"

static void
	s_init_sqb_ref_one(t_msh *msh)
{
	ft_memcpy(msh->sqb_ref[FT_ID_SQB_B], FT_SQB_B, ft_strlen(FT_SQB_B) + 1);
	ft_memcpy(msh->sqb_ref[FT_ID_SQB_C], FT_SQB_C, ft_strlen(FT_SQB_C) + 1);
	ft_memcpy(msh->sqb_ref[FT_ID_SQB_D], FT_SQB_D, ft_strlen(FT_SQB_D) + 1);
	ft_memcpy(msh->sqb_ref[FT_ID_SQB_E], FT_SQB_E, ft_strlen(FT_SQB_E) + 1);
	ft_memcpy(msh->sqb_ref[FT_ID_SQB_F], FT_SQB_F, ft_strlen(FT_SQB_F) + 1);
	ft_memcpy(msh->sqb_ref[FT_ID_SQB_H], FT_SQB_H, ft_strlen(FT_SQB_H) + 1);
	ft_memcpy(msh->sqb_ref[FT_ID_SQB_N], FT_SQB_N, ft_strlen(FT_SQB_N) + 1);
	ft_memcpy(msh->sqb_ref[FT_ID_SQB_P], FT_SQB_P, ft_strlen(FT_SQB_P) + 1);
	ft_memcpy(msh->sqb_ref[FT_ID_SQB_R], FT_SQB_R, ft_strlen(FT_SQB_R) + 1);
	ft_memcpy(msh->sqb_ref[FT_ID_SQB_S], FT_SQB_S, ft_strlen(FT_SQB_S) + 1);
	ft_memcpy(msh->sqb_ref[FT_ID_SQB_W], FT_SQB_W, ft_strlen(FT_SQB_W) + 1);
	ft_memcpy(msh->sqb_ref[FT_ID_SQB_X], FT_SQB_X, ft_strlen(FT_SQB_X) + 1);
	ft_memcpy(msh->sqb_ref[FT_ID_SQB_Z], FT_SQB_Z, ft_strlen(FT_SQB_Z) + 1);
	ft_memcpy(msh->sqb_ref[FT_ID_SQB_L_MAJ],
		FT_SQB_L_MAJ, ft_strlen(FT_SQB_L_MAJ) + 1);
	ft_memcpy(msh->sqb_ref[FT_ID_SQB_S_MAJ],
		FT_SQB_S_MAJ, ft_strlen(FT_SQB_S_MAJ) + 1);
	ft_memcpy(msh->sqb_ref[FT_ID_SQB_NT], FT_SQB_NT, ft_strlen(FT_SQB_NT) + 1);
	ft_memcpy(msh->sqb_ref[FT_ID_SQB_OT], FT_SQB_OT, ft_strlen(FT_SQB_OT) + 1);
	ft_memcpy(msh->sqb_ref[FT_ID_SQB_EQUA],
		FT_SQB_EQUA, ft_strlen(FT_SQB_EQUA) + 1);
	ft_memcpy(msh->sqb_ref[FT_ID_SQB_DIFF],
		FT_SQB_DIFF, ft_strlen(FT_SQB_DIFF) + 1);
}

static void
	s_init_sqb_ref_two(t_msh *msh)
{
	ft_memcpy(msh->sqb_ref[FT_ID_SQB_EQ], FT_SQB_EQ, ft_strlen(FT_SQB_EQ) + 1);
	ft_memcpy(msh->sqb_ref[FT_ID_SQB_NE], FT_SQB_NE, ft_strlen(FT_SQB_NE) + 1);
	ft_memcpy(msh->sqb_ref[FT_ID_SQB_GT], FT_SQB_GT, ft_strlen(FT_SQB_GT) + 1);
	ft_memcpy(msh->sqb_ref[FT_ID_SQB_GE], FT_SQB_GE, ft_strlen(FT_SQB_GE) + 1);
	ft_memcpy(msh->sqb_ref[FT_ID_SQB_LT], FT_SQB_LT, ft_strlen(FT_SQB_LT) + 1);
	ft_memcpy(msh->sqb_ref[FT_ID_SQB_LE], FT_SQB_LE, ft_strlen(FT_SQB_LE) + 1);
}

static char
	**dupenv_del(char **nenvp,
					uint64_t i)
{
	while (i > 0)
	{
		ft_memdel((void*)&nenvp[i]);
		i--;
	}
	ft_memdel((void*)&nenvp);
	return (NULL);
}

static char
	**dupenv(char *const envp[])
{
	uint64_t	i;
	char		**nenvp;

	i = 0;
	while (envp[i])
	{
		i++;
	}
	if (!(nenvp = (char**)malloc((i + 1) * sizeof(char*))))
	{
		return (NULL);
	}
	i = 0;
	while (envp[i])
	{
		if (!(nenvp[i] = ft_strdup(envp[i])))
			return (dupenv_del(nenvp, i));
		i++;
	}
	nenvp[i] = NULL;
	return (nenvp);
}

static void
	inc_shlvl(t_msh *msh)
{
	char	fmt[8];
	char	tmp[255];
	char	*str;
	int32_t	shlvl;

	u_get_var_value(tmp, "$SHLVL", 255, msh);
	if (tmp[0] == C_NUL)
	{
		ft_memcpy(fmt, "SHLVL=1", 8);
		b_export_with_equals(fmt, msh);
	}
	else
	{
		shlvl = ft_atoi(tmp);
		if (shlvl >= 999)
			f_shlvl_too_high(shlvl);
		shlvl = (shlvl >= 999) ? 0 : shlvl;
		shlvl = (shlvl < 0) ? 0 : shlvl + 1;
		str = ft_itoa(shlvl);
		u_subst_var_value("$SHLVL", str, msh);
		ft_memdel((void*)&str);
	}
}

static void
	set_cwd(char cwd[], t_msh *msh)
{
	char	fmt[PATH_MAX];
	DIR		*dir;

	u_get_var_value(cwd, "$PWD", PATH_MAX, msh);
	if (cwd[0] == C_NUL)
	{
		getcwd(cwd, PATH_MAX);
		ft_sprintf(fmt, "%s=%s", "PWD", cwd);
		b_export_with_equals(fmt, msh);
		return ;
	}
	if ((dir = opendir(cwd)) != NULL)
		closedir(dir);
	else if (errno == ENOENT)
	{
		getcwd(cwd, PATH_MAX);
		ft_sprintf(fmt, "%s=%s", "PWD", cwd);
		b_export_with_equals(fmt, msh);
		return ;
	}
}

t_msh
	*init_msh(char *const argv[], char *const envp[])
{
	char	cwd[PATH_MAX];
	t_msh	*msh;

	if ((msh = (t_msh*)malloc(sizeof(t_msh))) == NULL)
		return (NULL);
	if ((msh->shname = ft_strdup(argv[0])) == NULL)
		return (NULL);
	msh->envp = NULL;
	if ((msh->envp = dupenv(envp)) == NULL)
		return (NULL);
	msh->ret = 0;
	init_buptr(msh);
	msh->curr = NULL;
	msh->com = NULL;
	msh->pipes = NULL;
	msh->vars = NULL;
	msh->alias = NULL;
	set_cwd(cwd, msh);
	if ((msh->cwd = ft_strdup(cwd)) == NULL)
		return (NULL);
	msh->env_fork_tmp[0][0] = '\0';
	inc_shlvl(msh);
	s_init_sqb_ref_one(msh);
	s_init_sqb_ref_two(msh);
	return (msh);
}