summaryrefslogtreecommitdiffstats
path: root/src/s_init.c
blob: 1e8672738061c154e5fd6ef33e37252723ca25b3 (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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   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>
#ifdef __linux__
# include <linux/limits.h>
#else
# include <limits.h>
#endif
#include <stdlib.h>
#include <unistd.h>

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

static void	s_init_sqb_ref_one(t_msh *msh)
{
	(void)ft_memcpy(msh->sqb_ref[B_ID_SQB_B], B_SQB_B,
		(ft_strlen(B_SQB_B) + 1) * sizeof(char));
	(void)ft_memcpy(msh->sqb_ref[B_ID_SQB_C], B_SQB_C,
		(ft_strlen(B_SQB_C) + 1) * sizeof(char));
	(void)ft_memcpy(msh->sqb_ref[B_ID_SQB_D], B_SQB_D,
		(ft_strlen(B_SQB_D) + 1) * sizeof(char));
	(void)ft_memcpy(msh->sqb_ref[B_ID_SQB_E], B_SQB_E,
		(ft_strlen(B_SQB_E) + 1) * sizeof(char));
	(void)ft_memcpy(msh->sqb_ref[B_ID_SQB_F], B_SQB_F,
		(ft_strlen(B_SQB_F) + 1) * sizeof(char));
	(void)ft_memcpy(msh->sqb_ref[B_ID_SQB_H], B_SQB_H,
		(ft_strlen(B_SQB_H) + 1) * sizeof(char));
	(void)ft_memcpy(msh->sqb_ref[B_ID_SQB_N], B_SQB_N,
		(ft_strlen(B_SQB_N) + 1) * sizeof(char));
	(void)ft_memcpy(msh->sqb_ref[B_ID_SQB_P], B_SQB_P,
		(ft_strlen(B_SQB_P) + 1) * sizeof(char));
	(void)ft_memcpy(msh->sqb_ref[B_ID_SQB_R], B_SQB_R,
		(ft_strlen(B_SQB_R) + 1) * sizeof(char));
	(void)ft_memcpy(msh->sqb_ref[B_ID_SQB_S], B_SQB_S,
		(ft_strlen(B_SQB_S) + 1) * sizeof(char));
	(void)ft_memcpy(msh->sqb_ref[B_ID_SQB_W], B_SQB_W,
		(ft_strlen(B_SQB_W) + 1) * sizeof(char));
	(void)ft_memcpy(msh->sqb_ref[B_ID_SQB_X], B_SQB_X,
		(ft_strlen(B_SQB_X) + 1) * sizeof(char));
}

static void	s_init_sqb_ref_two(t_msh *msh)
{
	(void)ft_memcpy(msh->sqb_ref[B_ID_SQB_Z], B_SQB_Z,
		(ft_strlen(B_SQB_Z) + 1) * sizeof(char));
	(void)ft_memcpy(msh->sqb_ref[B_ID_SQB_L_MAJ], B_SQB_L_MAJ,
		(ft_strlen(B_SQB_L_MAJ) + 1) * sizeof(char));
	(void)ft_memcpy(msh->sqb_ref[B_ID_SQB_S_MAJ], B_SQB_S_MAJ,
		(ft_strlen(B_SQB_S_MAJ) + 1) * sizeof(char));
	(void)ft_memcpy(msh->sqb_ref[B_ID_SQB_NT], B_SQB_NT,
		(ft_strlen(B_SQB_NT) + 1) * sizeof(char));
	(void)ft_memcpy(msh->sqb_ref[B_ID_SQB_OT], B_SQB_OT,
		(ft_strlen(B_SQB_OT) + 1) * sizeof(char));
	(void)ft_memcpy(msh->sqb_ref[B_ID_SQB_EQUA], B_SQB_EQUA,
		(ft_strlen(B_SQB_EQUA) + 1) * sizeof(char));
	(void)ft_memcpy(msh->sqb_ref[B_ID_SQB_DIFF], B_SQB_DIFF,
		(ft_strlen(B_SQB_DIFF) + 1) * sizeof(char));
	(void)ft_memcpy(msh->sqb_ref[B_ID_SQB_EQ], B_SQB_EQ,
		(ft_strlen(B_SQB_EQ) + 1) * sizeof(char));
	(void)ft_memcpy(msh->sqb_ref[B_ID_SQB_NE], B_SQB_NE,
		(ft_strlen(B_SQB_NE) + 1) * sizeof(char));
	(void)ft_memcpy(msh->sqb_ref[B_ID_SQB_GT], B_SQB_GT,
		(ft_strlen(B_SQB_GT) + 1) * sizeof(char));
	(void)ft_memcpy(msh->sqb_ref[B_ID_SQB_GE], B_SQB_GE,
		(ft_strlen(B_SQB_GE) + 1) * sizeof(char));
	(void)ft_memcpy(msh->sqb_ref[B_ID_SQB_LT], B_SQB_LT,
		(ft_strlen(B_SQB_LT) + 1) * sizeof(char));
}

static void	s_init_sqb_ref_thr(t_msh *msh)
{
	(void)ft_memcpy(msh->sqb_ref[B_ID_SQB_LE], B_SQB_LE,
		(ft_strlen(B_SQB_LE) + 1) * sizeof(char));
}

static void	s_null_some(t_msh *msh)
{
	msh->fd = 0;
	msh->curr = NULL;
	msh->com = NULL;
	msh->pipes = NULL;
	msh->vars = NULL;
	msh->alias = NULL;
	msh->prev_hist = NULL;
}

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

	if ((msh = (t_msh*)malloc(sizeof(t_msh))) == NULL)
		return (NULL);
	msh->envp = NULL;
	(void)envp;
	if ((msh->envp = s_dupenv(envp)) == NULL)
	{
		ft_memdel((void*)&msh);
		return (NULL);
	}
	msh->argc = argc - 1;
	msh->argv = (char**)argv;
	msh->ret = 0;
	m_init_buptr(msh);
	s_null_some(msh);
	s_set_cwd(cwd, msh);
	if (cwd[0] != C_NUL && (msh->cwd = ft_strdup(cwd)) == NULL)
	{
		ft_delwords(msh->envp);
		ft_memdel((void*)&msh);
		return (NULL);
	}
	msh->env_fork_tmp[0][0] = '\0';
	if (s_inc_shlvl(msh) == 1)
	{
		ft_memdel((void*)&msh->cwd);
		ft_memdel((void*)&msh);
		return (NULL);
	}
	s_init_sqb_ref_one(msh);
	s_init_sqb_ref_two(msh);
	s_init_sqb_ref_thr(msh);
	return (msh);
}