update: added extremely basic parsing, only for separated environment variables

This commit is contained in:
2025-10-23 14:10:48 +02:00
parent 6a9d0027a7
commit 4546c7c252
4 changed files with 149 additions and 1 deletions

20
include/chardefs.h Normal file
View File

@@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* chardefs.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/10/23 13:23:25 by sede-san #+# #+# */
/* Updated: 2025/10/23 13:24:41 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef CHARDEFS_H
# define CHARDEFS_H
# define DOLLAR '$'
# define SINGLE_QUOTE '\''
# define DOUBLE_QUOTE '\"'
#endif /* CHARDEFS_H */

View File

@@ -6,7 +6,7 @@
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/10/20 16:35:10 by sede-san #+# #+# */
/* Updated: 2025/10/22 19:19:46 by sede-san ### ########.fr */
/* Updated: 2025/10/23 13:25:25 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,6 +15,8 @@
# include "libft.h"
# include "core.h"
# include "parser.h"
# include "chardefs.h"
# include <readline/readline.h> // readline(3), rl_clear_history(),
// rl_on_new_line(), rl_replace_line(),
// rl_redisplay()

38
include/parser.h Normal file
View File

@@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/10/22 19:03:51 by sede-san #+# #+# */
/* Updated: 2025/10/23 13:31:09 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PARSER_H
# define PARSER_H
# include "minishell.h"
/******************************************************************************/
/* Structures & Data Types */
/******************************************************************************/
typedef struct s_command t_command;
typedef struct s_command
{
int argc;
char **argv;
} t_command;
/******************************************************************************/
/* Functions */
/******************************************************************************/
// parser.c
extern char *parse(char *line, t_minishell *minishell);
#endif /* PARSER_H */

88
src/parser/parser.c Normal file
View File

@@ -0,0 +1,88 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/10/22 18:37:38 by sede-san #+# #+# */
/* Updated: 2025/10/23 14:07:20 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static char **expand_paths(char **argv);
static char **expand_envs(char **argv);
static int count_argv(char **argv);
char *parse(
char *line,
t_minishell *minishell
){
t_command command;
if (!line || !*line)
return (NULL);
command.argv = expand_envs(ft_split(line, SPACE));
command.argc = count_argv(command.argv);
if (!command.argc)
return (NULL);
////////////////////////////////////////////////////////////////////////////////
//* DEBUG
int i = -1;
while (command.argv[++i])
printf("argv[%i]: %s\n", i, command.argv[i]);
ft_free_split(command.argv);
////////////////////////////////////////////////////////////////////////////////
return (NULL);
}
// not sure of this step here
static char **expand_paths(
char **argv
){
if (!argv)
return (NULL);
return (argv);
}
static char **expand_envs(
char **argv
){
int i;
char *env;
if (!argv)
return (NULL);
else if (!*argv) // check if ft_split returned and empty matrix
{
ft_free_split(argv);
return (NULL);
}
i = -1;
while (argv[++i])
{
if (!ft_strchr(argv[i], DOLLAR)
|| (ft_strchr(argv[i], DOLLAR) && ft_strchr(argv[i], SINGLE_QUOTE) && ft_strchr(argv[i] + (ft_strchr(argv[i], SINGLE_QUOTE) + 1 - argv[i]), SINGLE_QUOTE))) // env is surrounded by single quote
continue ;
env = getenv(ft_strchr(argv[i], DOLLAR) + 1);
free(argv[i]);
if (env)
argv[i] = ft_strdup(env);
else
argv[i] = ft_strdup("");
}
return (argv);
}
static int count_argv(
char **argv
){
int i;
i = 0;
while (argv[i])
i++;
return (i);
}