fix: export builtin and other things *

fix: parse_command: syntaxes like: "hello"'hello'hello
fix: export builtin: "NAME=VALUE" syntax
fix: variables can contain digits
add: variables_nameisvalid()
change: dev/valgrind.sh
This commit is contained in:
mcolonna 2024-07-03 12:10:35 +02:00
parent b0aa3da8ce
commit 6862a1913b
8 changed files with 102 additions and 44 deletions

View file

@ -6,8 +6,9 @@ SRCS = src/
INCLUDES = include/ libtf/ libft/
# .c files in src/ without the extension
CODE = main ask_command error path parse_command exec_command builtin builtin2\
signals cool_readline variables parse_command_utils \
parse_command_read_string parse_command_read_element variables_utils
signals cool_readline variables variables2 parse_command_utils \
parse_command_read_string parse_command_read_element variables_utils \
export_builtin
# directories to 'make'
LIBRARIES = libtf libft
# .a files to include

View file

@ -1,2 +1,2 @@
#!/bin/bash
valgrind --leak-check=full --show-leak-kinds=all --suppressions=dev/supp ./minishell
valgrind --track-origins=yes --leak-check=full --show-leak-kinds=all --suppressions=dev/supp ./minishell

View file

@ -6,13 +6,16 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/27 13:43:21 by mcolonna #+# #+# */
/* Updated: 2024/06/28 14:22:50 by mcolonna ### ########.fr */
/* Updated: 2024/07/03 11:34:12 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef VARIABLES_H
# define VARIABLES_H
# define SYMBOL_CHARS \
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_0123456789"
typedef struct s_variable
{
const char *name;
@ -36,4 +39,7 @@ void variables_unset(t_list *variables, const char *name);
// Get a envp of all exported variables.
char *const *variables_envp(t_list *variables, t_memclass mc);
// Return true if 'name' is a valid variable name.
bool variables_nameisvalid(const char *name);
#endif

View file

@ -6,41 +6,12 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/25 13:28:29 by jschaft #+# #+# */
/* Updated: 2024/07/02 11:21:53 by mcolonna ### ########.fr */
/* Updated: 2024/07/03 12:16:48 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "include.h"
int export_builtin(t_env *env, t_call call, int std[2])
{
int i;
char *name;
char *value;
char **splitted;
const t_memclass mc = mem_subclass(fatal_error, env->mc_global);
(void)std;
i = 0;
while (call.argv[++i])
{
if (char_isin('=', call.argv[i]))
{
splitted = str_split(fatal_error, mc, call.argv[i], "=");
name = splitted[0];
value = splitted[1];
if (!value)
value = "";
variables_set(env->variables, name, value);
}
else
name = call.argv[i];
variables_export(env->variables, name);
}
mem_freeall(mc);
return (0);
}
int exit_builtin(t_env *env, t_call call, int std[2])
{
int r;

60
src/export_builtin.c Normal file
View file

@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* export_builtin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/25 13:28:29 by jschaft #+# #+# */
/* Updated: 2024/07/03 12:17:03 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "include.h"
static bool export_builtin_2(t_env *env, t_memclass mc, char **name,
const char *arg)
{
int i;
i = -1;
while (arg[++i] != '=')
;
*name = mem_alloc(fatal_error, mc, (i + 1) * sizeof(char));
i = -1;
while (arg[++i] != '=')
(*name)[i] = arg[i];
(*name)[i] = '\0';
if (!variables_nameisvalid(*name))
return (false);
variables_set(env->variables, *name, arg + i + 1);
return (true);
}
int export_builtin(t_env *env, t_call call, int std[2])
{
int i;
char *name;
const t_memclass mc = mem_subclass(fatal_error, env->mc_global);
int r;
(void)std;
r = 0;
i = 0;
while (call.argv[++i])
{
if (char_isin('=', call.argv[i]) && export_builtin_2(
env, mc, &name, call.argv[i]));
else if (variables_nameisvalid(call.argv[i]))
name = call.argv[i];
else
{
print_line(fatal_error, 2, "builtin: bad variable name");
r = 1;
continue ;
}
variables_export(env->variables, name);
}
mem_freeall(mc);
return (r);
}

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 13:47:40 by mcolonna #+# #+# */
/* Updated: 2024/06/27 14:32:37 by mcolonna ### ########.fr */
/* Updated: 2024/07/03 12:08:12 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -52,10 +52,11 @@ static void read_string_noquote(t_parsing_args *args, const char **dest,
const char *stop_charset)
{
const char *real_stop_charset
= str_join(fatal_error, args->mc, stop_charset, "$");
= str_join(fatal_error, args->mc, stop_charset, "$\"'");
while (stream_read(&args->stream)
&& !char_isin(stream_read(&args->stream), stop_charset))
&& !char_isin(stream_read(&args->stream), stop_charset)
&& !char_isin(stream_read(&args->stream), "\"'"))
{
read_until(args, dest, real_stop_charset);
if (stream_read(&args->stream) == '$')
@ -117,10 +118,7 @@ static bool read_string_doublequote(t_parsing_args *args, const char **dest)
}
// Read the string, stop if the char is in stop_charset.
// Possible syntaxes:
// - /[^(stop_charset)]+/
// - /'.*'/
// - /".*"/
// Syntax: /([^{stop_charset}]|'.*'|".*")+/
// Change args->r.error and return NULL if error.
const char *read_string(t_parsing_args *args, const char *stop_charset)
{

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 13:47:40 by mcolonna #+# #+# */
/* Updated: 2024/07/01 11:15:33 by mcolonna ### ########.fr */
/* Updated: 2024/07/03 11:33:12 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,8 +15,6 @@
# include "include.h"
# define SYMBOL_CHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_"
// Global variables for all the parsing functions
typedef struct s_parsing_args
{

24
src/variables2.c Normal file
View file

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* variables2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/02 15:39:55 by mcolonna #+# #+# */
/* Updated: 2024/07/02 15:41:42 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "include.h"
bool variables_nameisvalid(const char *name)
{
int i;
i = -1;
while (name[++i])
if (!char_isin(name[i], SYMBOL_CHARS))
return (false);
return (true);
}