Commit c1b5ab95 authored by pêle-mêle's avatar pêle-mêle
Browse files

one file per builtin, and some corrections

parent a59ca595
......@@ -12,9 +12,13 @@ SRC += prepare_args.c
SRC += launch_command.c
SRC += options.c
SRC += echo.c
SRC += pwd_cd.c
SRC += export_unset_env.c
SRC += ft_echo.c
SRC += ft_pwd.c
SRC += ft_cd.c
SRC += var_utils.c
SRC += ft_export.c
SRC += ft_unset.c
SRC += ft_env.c
SRC += ft_exit.c
......
......@@ -53,5 +53,6 @@ int ft_env(char **args, t_xe *xe);
*/
t_byte shift_options(char ***args, char *op_chars);
int check_var_name(char *var, ssize_t name_len);
#endif
j'ai ajouté un 'make lib' comme raccourci, quand on change un truc dans la lib
j'ai ajouté un 'make lib' comme raccourci, quand on change un truc dans la lib, ou qu'on veut recompiler tout src sans recompiler la lib (par ex pour mettre/retirer d=0)
use "bash --posix" to test!
export in alpha order
export in alpha order -> merge env + exported...?
besoin de proteger ft_strcmp? (comments)
export d=\$ vs export d=\1\f ou echo \$
export d='\' f=" la. ok. sf. non"
export
echo $d $f
......@@ -17,6 +18,7 @@ ou quand on valide une commande vide
commande "."
builtins: set stat_loc?
mkdir ok; cd ok; rmdir ../ok; pwd
header file sections https://www.doc.ic.ac.uk/lab/cplus/cstyle.html#N100D8
......
......@@ -22,18 +22,3 @@ int ft_cd(char **args, t_xe *xe)
free(path);
return (SUCCESS);
}
int ft_pwd(char **args, t_xe *xe)
{
char *buf;
(void)args;
(void)xe;
buf = getcwd(NULL, 0);
if (buf == NULL)
return (FAILURE);
ft_putstr_fd(buf, STDOUT_FILENO);
ft_putchar_fd('\n', STDOUT_FILENO);
free(buf);
return (SUCCESS);
}
#include "execution.h"
int ft_env(char **args, t_xe *xe)
{
(void)args;
ft_printarray_fd(xe->env, STDOUT_FILENO); // change fd for pipes
return (SUCCESS);
}
#include "execution.h"
int check_var_name(char *var, ssize_t name_len)
{
size_t i;
if (!ft_isdigit(var[0]) && var[0] != '=' && var[0] != '+' && var[0] != '\0')
{
i = 0;
while (ft_isalnum(var[i]) || var[i] == '_')
i++;
if ((name_len == ENTIRE_STR && var[i] == '\0')
|| i == (size_t)name_len
|| (i == (size_t)name_len - 1 && var[i] == '+'))
return (SUCCESS);
}
return (FAILURE);
}
void print_export(char **env, char **exported)
{
size_t equalsign_pos;
......@@ -42,14 +25,13 @@ void print_export(char **env, char **exported)
}
}
static int ft_env_move_from_exp(char **args, t_xe *xe,
ssize_t equalsign_pos, size_t i)
static int ft_env_append_from_exp(char *var, t_xe *xe, ssize_t equalsign_pos)
{
ssize_t var_pos;
char **new_array;
char *new_var;
var_pos = get_var_pos(xe->exported, args[i], equalsign_pos - 1);
var_pos = get_var_pos(xe->exported, var, equalsign_pos - 1);
if (var_pos != NOT_FOUND)
{
new_array = rm_str_from_array(xe->exported, (size_t)var_pos);
......@@ -57,12 +39,12 @@ static int ft_env_move_from_exp(char **args, t_xe *xe,
return (MALLOC_ERR);
xe->exported = new_array;
}
new_var = (char *)malloc(sizeof(char) * ft_strlen(args[i]));
new_var = (char *)malloc(sizeof(char) * ft_strlen(var));
if (new_var == NULL)
return (MALLOC_ERR);
ft_memcpy(new_var, args[i], equalsign_pos - 1);
ft_memcpy(new_var, var, equalsign_pos - 1);
ft_strlcpy(new_var + equalsign_pos - 1,
args[i] + equalsign_pos, ft_strlen(args[i]));
var + equalsign_pos, ft_strlen(var));
new_array = push_str_to_array(xe->env, new_var);
if (new_array == NULL)
return (MALLOC_ERR);
......@@ -71,17 +53,15 @@ static int ft_env_move_from_exp(char **args, t_xe *xe,
return (SUCCESS);
}
static int ft_env_append(char **args, t_xe *xe,
ssize_t equalsign_pos, size_t i)
static int ft_env_append(char *var, t_xe *xe, ssize_t equalsign_pos)
{
ssize_t var_pos;
char *new_var;
var_pos = get_var_pos(xe->env, args[i], equalsign_pos - 1);
var_pos = get_var_pos(xe->env, var, equalsign_pos - 1);
if (var_pos != NOT_FOUND)
{
new_var = ft_strjoin(xe->env[var_pos],
args[i] + equalsign_pos + 1); // ft_strjoin_free?
new_var = ft_strjoin(xe->env[var_pos], var + equalsign_pos + 1); // ft_strjoin_free?
if (new_var == NULL)
return (MALLOC_ERR);
free(xe->env[var_pos]);
......@@ -89,19 +69,18 @@ static int ft_env_append(char **args, t_xe *xe,
}
else
{
if (ft_env_move_from_exp(args, xe, equalsign_pos, i) != SUCCESS)
if (ft_env_append_from_exp(var, xe, equalsign_pos) != SUCCESS)
return (MALLOC_ERR);
}
return (SUCCESS);
}
static int ft_env_set_from_exp(char **args, t_xe *xe,
ssize_t equalsign_pos, size_t i)
static int ft_env_set_from_exp(char *var, t_xe *xe, ssize_t equalsign_pos)
{
char **new_array;
ssize_t var_pos;
var_pos = get_var_pos(xe->exported, args[i], equalsign_pos);
var_pos = get_var_pos(xe->exported, var, equalsign_pos);
if (var_pos != NOT_FOUND)
{
new_array = rm_str_from_array(xe->exported, (size_t)var_pos);
......@@ -109,58 +88,56 @@ static int ft_env_set_from_exp(char **args, t_xe *xe,
return (MALLOC_ERR);
xe->exported = new_array;
}
new_array = push_str_to_array(xe->env, args[i]);
new_array = push_str_to_array(xe->env, var);
if (new_array == NULL)
return (MALLOC_ERR);
xe->env = new_array;
return (SUCCESS);
}
static int ft_env_set(char **args, t_xe *xe, ssize_t equalsign_pos, size_t i)
static int ft_env_set(char *var, t_xe *xe, ssize_t equalsign_pos)
{
ssize_t var_pos;
var_pos = get_var_pos(xe->env, args[i], equalsign_pos);
var_pos = get_var_pos(xe->env, var, equalsign_pos);
if (var_pos != NOT_FOUND)
{
free(xe->env[var_pos]);
xe->env[var_pos] = ft_strdup(args[i]);
xe->env[var_pos] = ft_strdup(var);
if (xe->env[var_pos] == NULL)
return (MALLOC_ERR);
}
else
{
if (ft_env_set_from_exp(args, xe, equalsign_pos, i) != SUCCESS)
if (ft_env_set_from_exp(var, xe, equalsign_pos) != SUCCESS)
return (MALLOC_ERR);
}
return (SUCCESS);
}
static int ft_env_export(char **args, t_xe *xe,
ssize_t equalsign_pos, size_t i)
static int ft_env_export(char *var, t_xe *xe, ssize_t equalsign_pos)
{
if (args[i][equalsign_pos - 1] == '+')
if (var[equalsign_pos - 1] == '+')
{
if (ft_env_append(args, xe, equalsign_pos, i) != SUCCESS)
if (ft_env_append(var, xe, equalsign_pos) != SUCCESS)
return (MALLOC_ERR);
}
else
{
if (ft_env_set(args, xe, equalsign_pos, i) != SUCCESS)
if (ft_env_set(var, xe, equalsign_pos) != SUCCESS)
return (MALLOC_ERR);
}
return (SUCCESS);
}
static int ft_exported_export(char **args, t_xe *xe, size_t i)
static int ft_exported_export(char *var, t_xe *xe)
{
char **new_array;
if (get_var_pos(xe->env, args[i], ft_strlen(args[i])) == NOT_FOUND
&& get_var_pos(xe->exported, args[i], ft_strlen(args[i]))
== NOT_FOUND)
if (get_var_pos(xe->env, var, ft_strlen(var)) == NOT_FOUND
&& get_var_pos(xe->exported, var, ft_strlen(var)) == NOT_FOUND)
{
new_array = push_str_to_array(xe->exported, args[i]);
new_array = push_str_to_array(xe->exported, var);
if (new_array == NULL)
return (MALLOC_ERR);
xe->exported = new_array;
......@@ -168,98 +145,41 @@ static int ft_exported_export(char **args, t_xe *xe, size_t i)
return (SUCCESS);
}
static int ft_export_checks(char **args, t_xe *xe, size_t nb_args)
static int ft_export_checks(char **args, t_xe *xe)
{
size_t i;
ssize_t equalsign_pos;
i = 1;
while (i < nb_args)
while (*args != NULL)
{
equalsign_pos = ft_index(args[i], '=');
if (check_var_name(args[i], equalsign_pos) == SUCCESS)
equalsign_pos = ft_index(*args, '=');
if (check_var_name(*args, equalsign_pos) == SUCCESS)
{
if (equalsign_pos != NOT_FOUND)
{
if (ft_env_export(args, xe, equalsign_pos, i) != SUCCESS)
if (ft_env_export(*args, xe, equalsign_pos) != SUCCESS)
return (MALLOC_ERR);
}
else
{
if (ft_exported_export(args, xe, i) != SUCCESS)
if (ft_exported_export(*args, xe) != SUCCESS)
return (MALLOC_ERR);
}
}
else
putstr_stderr("export: Variable identifier (name) invalid\n"); // leaks si le write marche pas
i++;
args++;
}
return (SUCCESS);
}
int ft_export(char **args, t_xe *xe)
{
size_t nb_args;
nb_args = ft_arraylen(args);
if (nb_args == 1)
if (ft_arraylen(args) == 1)
print_export(xe->env, xe->exported);
if (ft_export_checks(args, xe, nb_args) != SUCCESS)
return (MALLOC_ERR);
return (SUCCESS);
}
static int ft_unset_loop(char **args, t_xe *xe, size_t i)
{
ssize_t var_pos;
char **new_array;
var_pos = get_var_pos(xe->env, args[i], ft_strlen(args[i]));
if (var_pos != NOT_FOUND)
{
new_array = rm_str_from_array(xe->env, (size_t)var_pos);
if (new_array == NULL)
return (MALLOC_ERR);
xe->env = new_array;
}
else
{
var_pos = get_var_pos(xe->exported, args[i], ft_strlen(args[i]));
if (var_pos != NOT_FOUND)
{
new_array = rm_str_from_array(xe->exported, (size_t)var_pos);
if (new_array == NULL)
return (MALLOC_ERR);
xe->exported = new_array;
}
}
return (SUCCESS);
}
int ft_unset(char **args, t_xe *xe)
{
size_t nb_args;
size_t i;
nb_args = ft_arraylen(args);
i = 1;
while (i < nb_args)
{
if (check_var_name(args[i], ENTIRE_STR) == SUCCESS)
{
if (ft_unset_loop(args, xe, i) != SUCCESS)
return (MALLOC_ERR);
}
else
putstr_stderr("unset: Variable identifier (name) invalid\n"); // leaks si le write marche pas
i++;
if (ft_export_checks(args + 1, xe) != SUCCESS)
return (MALLOC_ERR);
}
return (SUCCESS);
}
int ft_env(char **args, t_xe *xe)
{
(void)args;
ft_printarray_fd(xe->env, STDOUT_FILENO); // change fd for pipes
return (SUCCESS);
}
#include "execution.h"
int ft_pwd(char **args, t_xe *xe)
{
char *buf;
(void)args;
(void)xe;
buf = getcwd(NULL, 0);
if (buf == NULL)
return (FAILURE); // other code!
ft_putstr_fd(buf, STDOUT_FILENO);
ft_putchar_fd('\n', STDOUT_FILENO);
free(buf);
return (SUCCESS);
}
#include "execution.h"
static int unset_one_var(char *var, t_xe *xe)
{
ssize_t var_pos;
char **new_array;
var_pos = get_var_pos(xe->env, var, ft_strlen(var));
if (var_pos != NOT_FOUND)
{
new_array = rm_str_from_array(xe->env, (size_t)var_pos);
if (new_array == NULL)
return (MALLOC_ERR);
xe->env = new_array;
}
else
{
var_pos = get_var_pos(xe->exported, var, ft_strlen(var));
if (var_pos != NOT_FOUND)
{
new_array = rm_str_from_array(xe->exported, (size_t)var_pos);
if (new_array == NULL)
return (MALLOC_ERR);
xe->exported = new_array;
}
}
return (SUCCESS);
}
int ft_unset(char **args, t_xe *xe)
{
args++;
while (*args != NULL)
{
if (check_var_name(*args, ENTIRE_STR) == SUCCESS)
{
if (unset_one_var(*args, xe) != SUCCESS)
return (MALLOC_ERR);
}
else
putstr_stderr("unset: Variable identifier (name) invalid\n"); // leaks si le write marche pas
args++;
}
return (SUCCESS);
}
......@@ -56,7 +56,7 @@ static int launch_ext(char **args, t_xe *xe)
char *tmp;
char *cmd;
char **path;
int program_dir;
int dir_index;
tmp = get_var_value(xe->env, "PATH"); // and with PATH unset?
if (tmp == NULL)
......@@ -71,13 +71,13 @@ static int launch_ext(char **args, t_xe *xe)
if (ft_strchr(args[0], '/') != NULL)
if (execve(args[0], args, xe->env) == ERROR)
perror("External function error:");
program_dir = search_exec(path, args[0]);
if (program_dir == NOT_FOUND)
dir_index = search_exec(path, args[0]);
if (dir_index == NOT_FOUND)
{
free_str_array(path);
return (FAILURE); // other error code
}
cmd = ft_strjoin(path[program_dir], "/");
cmd = ft_strjoin(path[dir_index], "/");
tmp = cmd;
cmd = ft_strjoin(cmd, args[0]);
free(tmp);
......
#include "execution.h"
int check_var_name(char *var, ssize_t name_len)
{
size_t i;
if (!ft_isdigit(var[0]) && var[0] != '=' && var[0] != '+' && var[0] != '\0')
{
i = 0;
while (ft_isalnum(var[i]) || var[i] == '_')
i++;
if ((name_len == ENTIRE_STR && var[i] == '\0')
|| i == (size_t)name_len
|| (i == (size_t)name_len - 1 && var[i] == '+'))
return (SUCCESS);
}
return (FAILURE);
}
......@@ -99,7 +99,6 @@ static int main_loop(t_xe *xe)
fd_backup = -1;
ret = get_input(&xe->commands);
// write(1, "OK\n", 3);
if (ret == SUCCESS)
ret = handle_execution(xe);
return (ret);
......
......@@ -63,16 +63,15 @@ size_t resize_token(char *str, t_list *var_positions, char **var_values)
char *expand_token_vars(t_token *token, char **env, int stat_loc)
{
char ***ptr;
char **var_values;
char *ret;
size_t token_len;
t_list *var_positions;
t_var_pos *var;
size_t i_ret;
size_t j_input;
size_t k_value;
size_t l_values;
size_t i_ret; // temp name
size_t j_input; // temp name
size_t k_value; // temp name
size_t l_values; // temp name
// if (token->var_positions == NULL)
// return (ft_strdup(token->str));
......@@ -89,37 +88,30 @@ char *expand_token_vars(t_token *token, char **env, int stat_loc)
var_positions = token->var_positions;
while (i_ret < token_len)
{
if (var_positions != NULL)
var = var_positions->content;
if (var != NULL && j_input == var->start)
if (var_positions != NULL) // && if imbriqué?
{
if (var_values[l_values] != NULL)
var = var_positions->content;
if (/*var != NULL && */j_input == var->start)
{
j_input = var->start + var->len;
if (ft_strlen(var_values[l_values]) != 0)
{
k_value = 0;
while (var_values[l_values][k_value])
{
ret[i_ret] = var_values[l_values][k_value];
k_value++;
i_ret++;
}
j_input = var->start + var->len;
}
//if (var_values[l_values] != NULL)
//{
//j_input = var->start + var->len;
//if (ft_strlen(var_values[l_values]) != 0)
//{
k_value = 0;
while (var_values[l_values][k_value] != '\0')
ret[i_ret++] = var_values[l_values][k_value++]; // strcpy? memccpy?
j_input /*= var->start + var->len*/+= var->len;
//}
//}
var_positions = var_positions->next;
l_values++;
}
var_positions = var_positions->next;
l_values++;
}
else
{
ret[i_ret] = token->str[j_input];
i_ret++;
j_input++;
}
ret[i_ret++] = token->str[j_input++];
}
ret[i_ret] = '\0';
ptr = &var_values; // ??
free_str_array(var_values);
return (ret);
}
......@@ -171,7 +163,7 @@ char **prepare_args(t_command *command, char **env, int stat_loc)
while (tokens != NULL)
{
cur_token = ft_lstshift(&tokens);
cur_arg = expand_token_vars(cur_token, env, stat_loc);
cur_arg = expand_token_vars(cur_token, env, stat_loc);
if (cur_arg == NULL)
{
free_str_array(args);
......@@ -187,6 +179,6 @@ char **prepare_args(t_command *command, char **env, int stat_loc)
free_token(cur_token);
}
free(command);
// ft_printarray_fd(args, 1);
// ft_printarray_fd(args, 1);
return (args);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment