summaryrefslogtreecommitdiffstats
path: root/src/b_export.c
blob: 9fe5709fc246788e904a07e4721f5eebaed56197 (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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   b_export.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 <stdlib.h>
#include <stdint.h>

#include "d_enum.h"
#include "b_env.h"
#include "f_fail.h"
#include "s_destroy.h"
#include "s_lcom.h"
#include "s_lvars.h"
#include "s_struct.h"
#include "u_utils.h"
#include "u_vars.h"

static t_bool
	check_valid_identifier(const char *arg)
{
	char	*ptr;

	ptr = (char*)arg;
	if (ft_isalpha(ptr[0]))
	{
		return (TRUE);
	}
	return (FALSE);
}

static t_bool
	check_equals(const char *arg)
{
	char	*ptr;

	ptr = (char*)arg;
	while (*ptr != '\0')
	{
		if (*ptr == '=')
			return (TRUE);
		ptr++;
	}
	return (FALSE);
}

static void
	b_add_to_env_from_globals(const char varname[],
							const char var[],
							t_msh *msh)
{
	size_t	i;
	char	**nenvp;

	i = 0;
	while (msh->envp[i] != NULL)
		i++;
	if (!(nenvp = (char**)malloc((i + 2) * sizeof(char*))))
	{
		lcom_clear(&msh->curr);
		s_destroy(msh);
		f_fail_alloc(msh);
	}
	i = 0;
	while (msh->envp[i] != NULL)
	{
		if (!(nenvp[i] = ft_strdup(msh->envp[i])))
		{
			lcom_clear(&msh->curr);
			s_destroy(msh);
			f_fail_alloc(msh);
		}
		i++;
	}
	if (!(nenvp[i] = ft_strdup(var)))
	{
		lcom_clear(&msh->curr);
		s_destroy(msh);
		f_fail_alloc(msh);
	}
	nenvp[i + 1] = 0;
	ft_delwords(msh->envp);
	lvars_delone(&msh->vars, varname);
	msh->envp = nenvp;
}

uint8_t
	b_export(char *args[],
			t_msh *msh)
{
	/* TODO: norme */
	const uint64_t	argc = get_argc((const char**)args);
	char			**ptr;
	char			*varval;
	char			fmt[255];
	t_bool			next;
	uint8_t			r;

	if (argc == 0)
	{
		return (b_env(NULL, msh));
	}
	ptr = args;
	r = 0;
	while (*ptr)
	{
		next = FALSE;
		if (check_valid_identifier(*ptr) == FALSE)
		{
			f_fail_identifier("export", *ptr, msh);
			next = TRUE;
			r = 1;
		}
		if (next == FALSE && check_equals(*ptr) == FALSE)
		{
			next = TRUE;
			ft_sprintf(fmt, "$%s", *ptr);
			varval = get_cstm_vr(fmt, msh);
			if (varval != NULL)
			{
				ft_sprintf(fmt, "%s=%s", *ptr, varval);
				b_add_to_env_from_globals(*ptr, fmt, msh);
			}
		}
		else if (next == FALSE && check_equals(*ptr) == TRUE)
		{
			/* TODO: verify global vars, delete if needed, export the thing */
		}
		ptr++;
	}
	 /* TODO: finish export */
	return (r);
}