aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/builtin/export.c61
-rw-r--r--src/builtin/unset.c23
-rw-r--r--src/env.c34
-rw-r--r--src/error.c (renamed from src/eval/error.c)12
-rw-r--r--src/eval/cmd.c6
-rw-r--r--src/ms_glob.c3
-rw-r--r--src/path.c3
-rw-r--r--src/utils.c15
8 files changed, 105 insertions, 52 deletions
diff --git a/src/builtin/export.c b/src/builtin/export.c
index f41bcb0..809b809 100644
--- a/src/builtin/export.c
+++ b/src/builtin/export.c
@@ -6,7 +6,7 @@
/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/01 17:11:34 by charles #+# #+# */
-/* Updated: 2020/06/19 11:21:50 by nahaddac ### ########.fr */
+/* Updated: 2020/07/15 13:22:18 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,35 +15,56 @@
** \brief `export` builtin
*/
-// modify existing
-// set with no string without '='
-// TODO: multiple exported variable (e.g export A=a B=b C=c)
-
#include "minishell.h"
-int builtin_export(char **argv, t_env env)
+static void st_put_declare_x(char *s)
{
- char *temp;
+ char *equal_ptr;
+
+ if (s == NULL)
+ return ;
+ ft_putstr("declare -x ");
+ if ((equal_ptr = ft_strchr(s, '=')) == NULL)
+ equal_ptr = ft_strchr(s, '\0');
+ write(STDOUT_FILENO, s, equal_ptr - s);
+ ft_putchar('=');
+ ft_putchar('"');
+ ft_putstr(equal_ptr + 1);
+ ft_putchar('"');
+ ft_putchar('\n');
+}
+
+int builtin_export(char **argv, t_env env)
+{
+ int status;
size_t i;
+ char *equal_ptr;
+ bool skip;
- (void)env;
if (argv[1] == NULL)
+ {
+ ft_veciter(env, (void (*)(void*))st_put_declare_x);
return (0);
- if(ft_isdigit(argv[1][0]))
- return(0);
+ }
+ status = 0;
i = 0;
- temp = argv[1];
- while(temp[i] != '\0')
+ while (argv[++i] != NULL)
{
- if(temp[i] == ' ' || ft_isalnum(temp[i]) == 0)
- return(0);
- if (temp[i] == '=')
+ skip = (equal_ptr = ft_strchr(argv[i], '=')) == NULL;
+ if (!skip)
+ *equal_ptr = '\0';
+ if (!utils_valid_identifier(argv[i]))
{
- temp[i] = '\0';
- env_export(env, temp, &argv[1][i + 1]);
- return(0);
+ if (!skip)
+ *equal_ptr = '=';
+ error_put_invalid_identifier("export", argv[i]);
+ status = 1;
+ continue;
}
- i++;
+ if (skip)
+ continue;
+ if (env_export(env, argv[i], equal_ptr + 1) == NULL)
+ return (127); // malloc error
}
- return (0);
+ return (status);
}
diff --git a/src/builtin/unset.c b/src/builtin/unset.c
index b1e2d5b..b7b6d58 100644
--- a/src/builtin/unset.c
+++ b/src/builtin/unset.c
@@ -6,7 +6,7 @@
/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/01 17:10:51 by charles #+# #+# */
-/* Updated: 2020/06/17 12:41:45 by nahaddac ### ########.fr */
+/* Updated: 2020/07/15 13:14:38 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -20,18 +20,23 @@
int builtin_unset(char **argv, t_env env)
{
size_t i;
+ int found_index;
+ int status;
- if (argv[1] == NULL)
- return (1);
+ status = 0;
i = 0;
- while (i < env->size)
+ while (argv[++i] != NULL)
{
- if (env_keycmp(env->data[i],argv[1]) == 0)
+ if (!utils_valid_identifier(argv[i]))
{
- ft_vecremove(env, i, free);
- return (0);
+ error_put_invalid_identifier("unset", argv[i]);
+ status = 1;
+ continue; // put invalid identifier
}
- i++;
+ found_index = env_search_index(env, argv[i]);
+ if (found_index == -1)
+ continue;
+ ft_vecremove(env, found_index, free);
}
- return (1);
+ return (status);
}
diff --git a/src/env.c b/src/env.c
index bdc2498..1ed5162 100644
--- a/src/env.c
+++ b/src/env.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/28 09:21:24 by cacharle #+# #+# */
-/* Updated: 2020/07/13 11:09:39 by charles ### ########.fr */
+/* Updated: 2020/07/15 13:02:04 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -77,6 +77,20 @@ char *env_search(t_env env, char *key)
return (NULL);
}
+int env_search_index(t_env env, char *key)
+{
+ size_t i;
+
+ i = 0;
+ while (i < env->size - 1)
+ {
+ if (env_keycmp(env->data[i], key) == 0)
+ return (i);
+ i++;
+ }
+ return (-1);
+}
+
char *env_search_first_match(t_env env, const char *haystack)
{
size_t len;
@@ -107,28 +121,20 @@ char *env_search_first_match(t_env env, const char *haystack)
char *env_export(t_env env, char *key, char *value)
{
char *joined;
- size_t i;
+ int i;
if ((joined = ft_strjoin3(key, "=", value)) == NULL)
return (NULL);
- if (env_search(env, key) == NULL)
+ i = env_search_index(env, key);
+ if (i == -1)
{
if (ft_vecinsert(env, env->size - 1, joined) == NULL)
return (NULL);
}
else
{
- i = 0;
- while (i < env->size - 1)
- {
- if (env_keycmp(env->data[i], key) == 0)
- {
- free(env->data[i]);
- env->data[i] = joined;
- break;
- }
- i++;
- }
+ free(env->data[i]);
+ env->data[i] = joined;
}
return (joined);
}
diff --git a/src/eval/error.c b/src/error.c
index 10f5740..13a1443 100644
--- a/src/eval/error.c
+++ b/src/error.c
@@ -6,7 +6,7 @@
/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/14 11:02:52 by charles #+# #+# */
-/* Updated: 2020/07/14 11:10:12 by nahaddac ### ########.fr */
+/* Updated: 2020/07/15 13:11:13 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -53,3 +53,13 @@ int error_status(enum e_error id)
{
return (st_error_get(id)->status);
}
+
+void error_put_invalid_identifier(char *prefix, char *identifier)
+{
+ ft_putstr_fd(g_basename, STDERR_FILENO);
+ ft_putstr_fd(": ", STDERR_FILENO);
+ ft_putstr_fd(prefix, STDERR_FILENO);
+ ft_putstr_fd(": `", STDERR_FILENO);
+ ft_putstr_fd(identifier, STDERR_FILENO);
+ ft_putstr_fd("': not a valid identifier\n", STDERR_FILENO);
+}
diff --git a/src/eval/cmd.c b/src/eval/cmd.c
index b3cdc15..196d810 100644
--- a/src/eval/cmd.c
+++ b/src/eval/cmd.c
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/14 10:41:31 by charles #+# #+# */
-/* Updated: 2020/07/14 11:07:00 by charles ### ########.fr */
+/* Updated: 2020/07/15 13:03:20 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -92,8 +92,8 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast)
return (-1); // return error status
}
}
- /* else if (!param.builtin->child_process) // solved with pipeline */
- /* return (param.builtin->func(argv, env)); */
+ else// solved with pipeline
+ return (param.builtin->func(argv, env));
param.argv = argv;
param.env = env;
diff --git a/src/ms_glob.c b/src/ms_glob.c
index dae2bd8..7603286 100644
--- a/src/ms_glob.c
+++ b/src/ms_glob.c
@@ -6,12 +6,11 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/05 11:44:07 by charles #+# #+# */
-/* Updated: 2020/06/17 14:39:52 by charles ### ########.fr */
+/* Updated: 2020/07/15 12:12:02 by charles ### ########.fr */
/* */
/* ************************************************************************** */
#include "ms_glob.h"
-#include <limits.h>
/*
** \brief Match vector start size
diff --git a/src/path.c b/src/path.c
index d768d07..579fd81 100644
--- a/src/path.c
+++ b/src/path.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/27 15:51:01 by cacharle #+# #+# */
-/* Updated: 2020/04/05 12:09:05 by charles ### ########.fr */
+/* Updated: 2020/07/15 12:11:48 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -16,7 +16,6 @@
*/
#include "minishell.h"
-#include "utils.h"
/*
** \brief Number of buckets of a path hash table
diff --git a/src/utils.c b/src/utils.c
index 4a9ecb8..b7d4404 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -6,7 +6,7 @@
/* By: cacharle <cacharle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/28 11:56:31 by cacharle #+# #+# */
-/* Updated: 2020/07/14 10:40:59 by nahaddac ### ########.fr */
+/* Updated: 2020/07/15 12:54:28 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -63,3 +63,16 @@ size_t utils_var_end(char *name)
i++;
return (i + 1);
}
+
+bool utils_valid_identifier(char *name)
+{
+ if (ft_isdigit(*name) || *name == '\0')
+ return (false);
+ while (*name != '\0')
+ {
+ if (!ft_isalnum(*name) && *name != '_')
+ return (false);
+ name++;
+ }
+ return (true);
+}