Skip to content
Merged
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
e34a592
is_exportedフラグを追加、初期化時に1にする設定も追加
Apr 21, 2025
8647494
split_key_valueをset_envpに変更
Apr 21, 2025
806a01c
add builtin export commands(WIP)
Apr 21, 2025
8fce4cd
valueの出力にダブルクオートを追加
Apr 21, 2025
b686319
wip
Apr 21, 2025
dd96cf2
Merge branch 'main' into feature/26
Apr 28, 2025
5144489
add print_sorted_env
Apr 28, 2025
fea22a0
fix norm
Apr 28, 2025
b4628a3
add function
Apr 28, 2025
7d40d68
statusを返すように修正
Apr 28, 2025
587cd85
構造体の各値毎に代入する形に修正
Apr 28, 2025
79154a7
出力方法を修正
Apr 28, 2025
5ac1641
Merge branch 'main' into feature/26
Apr 28, 2025
793ced0
分岐作成
Apr 28, 2025
813cd2e
fix norm
Apr 28, 2025
8f7720e
add_key
Apr 28, 2025
6db47f4
add _add_key_value
Apr 28, 2025
8706124
utils_3.hに関数プロトタイプを分割
Apr 28, 2025
2546d75
append_value実装
Apr 28, 2025
ef79824
add ft_strndup
Apr 28, 2025
1aff241
fix split_key_value
Apr 28, 2025
a01858e
fix norm
Apr 28, 2025
43365c0
add _update value
Apr 28, 2025
f679fa4
fix build error
Apr 28, 2025
e83a4f4
fix compile error
Apr 28, 2025
92ff7aa
use export_exec
Apr 28, 2025
538d78b
minor corrections
Apr 28, 2025
dd63c5d
Merge branch 'main' into feature/26
cacapon Apr 28, 2025
c3081f6
envでexportされていないものを表示しない対応を追加
Apr 29, 2025
7cbde97
ビルドエラー修正
Apr 29, 2025
b595343
validate_keyとして共通部に抽出
Apr 29, 2025
9b13a4a
有効値チェック追加
Apr 29, 2025
cdf5b69
テスト修正
Apr 29, 2025
bb8c2d6
fix norm
Apr 29, 2025
aeab749
Merge branch 'feature/26' of github.com:cacapon/minishell-prv into fe…
Apr 29, 2025
3eb79b8
Merge branch 'main' into feature/26
cacapon Apr 29, 2025
3fe3753
複数入力対応
Apr 29, 2025
a1314d1
error_mesを共通部に切り出し
Apr 29, 2025
0204b91
不正なKEYのとき、エラーを出力する関数を追加
Apr 29, 2025
47cdb96
error出力を追加
Apr 29, 2025
94395ba
Merge branch 'main' into feature/26
cacapon May 1, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: ttsubo <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2025/04/03 12:55:20 by ttsubo #+# #+# #
# Updated: 2025/04/26 11:30:48 by ttsubo ### ########.fr #
# Updated: 2025/04/29 14:44:30 by ttsubo ### ########.fr #
# #
# **************************************************************************** #

Expand All @@ -20,6 +20,7 @@ L_FLG = -lreadline -lft
# 1.Add more directories as they are added.
SRC_DIR = src/
PARSER_SRC_DIR = src/parser/
COMMON_SRC_DIR = src/common/
TOKENIZER_SRC_DIR = src/tokenizer/
INVOKE_CMD_SRC_DIR = src/invoke_cmd/
BUILTIN_SRC_DIR = src/builtin/
Expand All @@ -46,17 +47,20 @@ endef

# 2.Add the source code when it is added
ROOT_SRC = main.c minish_signal.c initialize.c
COMMON_SRC = validate_key.c error_mes.c
INVOKE_CMD_SRC = create_envp.c exec_pipeline.c execute_cmd.c \
execute_cmd_helper.c invoke_command.c pipeline_helper.c
TOKENIZER_SRC = tokenizer.c tokenizer_error.c read_token.c \
is_quote_closed.c get_token_capa.c is_redirect_validate.c
PARSER_SRC = allocate_cmds.c parser.c parser_utils.c setup_cmds.c \
expand_env.c expand_env_utils.c
BUILTIN_SRC = cd.c exit.c pwd.c echo.c env.c unset.c \
env_utils.c env_utils_2.c builtin_utils.c
expand_env.c
BUILTIN_SRC = cd.c exit.c pwd.c echo.c env.c unset.c export.c \
env_utils.c env_utils_2.c env_utils_3.c builtin_utils.c \
export_exec.c export_print_sorted_env.c export_error.c

# 3.Add more directories as they are added.
$(eval $(call add_module,root))
$(eval $(call add_module,common))
$(eval $(call add_module,invoke_cmd))
$(eval $(call add_module,tokenizer))
$(eval $(call add_module,parser))
Expand Down
3 changes: 2 additions & 1 deletion inc/builtin.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/13 17:19:01 by ttsubo #+# #+# */
/* Updated: 2025/04/20 19:48:02 by ttsubo ### ########.fr */
/* Updated: 2025/04/21 11:20:16 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -24,5 +24,6 @@ int builtin_exit(int argc, char *argv[]);
int builtin_cd(int argc, char *argv[]);
int builtin_env(int argc, char **argv, t_minish *minish);
int builtin_unset(int argc, char *argv[], t_minish *minish);
int builtin_export(int argc, char **argv, t_minish *minish);

#endif
17 changes: 15 additions & 2 deletions inc/builtin_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,26 @@
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/20 19:47:50 by ttsubo #+# #+# */
/* Updated: 2025/04/20 19:55:44 by ttsubo ### ########.fr */
/* Updated: 2025/04/29 14:48:10 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef BUILTIN_UTILS_H
# define BUILTIN_UTILS_H

void error_mes(char *name, char *mes);
# include <stdbool.h>
# include <stddef.h>

typedef struct s_export_exec_bool
{
bool exists;
bool has_plus;
bool has_eq;
} t_export_exec_bool;

char *ft_strndup(const char *s1, size_t n);
int export_exec(int argc, char **argv, t_minish *minish);
int print_sorted_env(t_env *head);
void export_err_invalid(char *sh, char *arg);

#endif
25 changes: 25 additions & 0 deletions inc/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* common.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/29 11:44:19 by ttsubo #+# #+# */
/* Updated: 2025/04/29 14:12:51 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef COMMON_H
# define COMMON_H

# include <stdbool.h>
# include <stddef.h>
# include "libft.h"

bool is_key_start(int c);
bool is_key_char(int c);
bool is_valid_key(char *str);
void error_mes(char *name, char *mes);

#endif
3 changes: 1 addition & 2 deletions inc/env_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 10:25:47 by dayano #+# #+# */
/* Updated: 2025/04/26 10:19:56 by ttsubo ### ########.fr */
/* Updated: 2025/04/28 15:47:53 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -15,7 +15,6 @@

# include "struct.h"

char *get_env_value(t_env *current, const char *key);
void free_env_content(char *value);
void remove_env_node(t_env **env_lst, t_env *target);

Expand Down
5 changes: 3 additions & 2 deletions inc/env_utils_2.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* env_utils_2.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dayano <dayano@student.42.fr> +#+ +:+ +#+ */
/* By: ttsubo <ttsubo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:40:36 by dayano #+# #+# */
/* Updated: 2025/04/17 11:42:07 by dayano ### ########.fr */
/* Updated: 2025/04/21 12:14:53 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -18,5 +18,6 @@
void free_env_list(t_env *head);
void cleanup_minish(t_minish *minish);
void handle_error_and_exit(const char *func_name, t_minish *minish);
int split_key_value(char *str, char **key_out, char **val_out);

#endif
23 changes: 23 additions & 0 deletions inc/env_utils_3.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* env_utils_3.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:40:36 by dayano #+# #+# */
/* Updated: 2025/04/28 16:51:16 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef ENV_UTILS_3_H
# define ENV_UTILS_3_H

# include "struct.h"

void add_env_back(t_env **lst, t_env *new);
t_env *get_env_node(t_env *current, const char *key);
char *get_env_value(t_env *current, const char *key);
bool has_env_key(t_env *current, const char *key);

#endif
7 changes: 3 additions & 4 deletions inc/expand_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/26 11:24:25 by ttsubo #+# #+# */
/* Updated: 2025/04/26 11:50:18 by ttsubo ### ########.fr */
/* Updated: 2025/04/29 11:45:55 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef EXPAND_ENV_H
# define EXPAND_ENV_H

# include "env_utils.h"
# include "common.h"
# include "env_utils_3.h"
# include "struct.h"
# include <stdbool.h>

Expand All @@ -34,7 +35,5 @@ typedef struct s_expand_temp
} t_expand_temp;

char *expand_env(char *token, t_minish *minish);
bool is_key_start(int c);
bool is_key_char(int c);

#endif
3 changes: 2 additions & 1 deletion inc/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dayano <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/03 12:50:38 by ttsubo #+# #+# */
/* Updated: 2025/04/28 12:00:56 by dayano ### ########.fr */
/* Updated: 2025/04/28 15:48:34 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -19,6 +19,7 @@
# include "create_envp.h"
# include "env_utils.h"
# include "env_utils_2.h"
# include "env_utils_3.h"
# include "exec_pipeline.h"
# include "execute_cmd.h"
# include "initialize.h"
Expand Down
1 change: 1 addition & 0 deletions inc/struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ typedef struct s_env
{
char *key;
char *value;
int is_exported;
struct s_env *next;
} t_env;

Expand Down
25 changes: 16 additions & 9 deletions src/builtin/builtin_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,28 @@
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/20 19:46:40 by ttsubo #+# #+# */
/* Updated: 2025/04/27 14:55:58 by ttsubo ### ########.fr */
/* Updated: 2025/04/29 14:12:25 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

#include "main.h"

/**
* @brief Output to STDERR in the format "<name>: <mes>\n".
*
* @param name
* @param mes
* @brief TODO: Retrieve as utils function later
*
* @param s1
* @param n
* @return char*
*/
void error_mes(char *name, char *mes)
char *ft_strndup(const char *s1, size_t n)
{
ft_putstr_fd(name, STDERR_FILENO);
ft_putstr_fd(": ", STDERR_FILENO);
ft_putendl_fd(mes, STDERR_FILENO);
char *d;

if (!s1)
return (NULL);
d = malloc(n + 1);
if (!d)
return (NULL);
ft_strlcpy(d, s1, n + 1);
return (d);
}
7 changes: 4 additions & 3 deletions src/builtin/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/14 16:19:11 by dayano #+# #+# */
/* Updated: 2025/04/20 19:53:20 by ttsubo ### ########.fr */
/* Updated: 2025/04/29 11:25:22 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -23,8 +23,9 @@ int builtin_env(int argc, char **argv, t_minish *minish)
while (current)
{
next = current->next;
if (printf("%s=%s\n", current->key, current->value) < 0)
handle_error_and_exit("printf", minish);
if (current->is_exported)
if (printf("%s=%s\n", current->key, current->value) < 0)
handle_error_and_exit("printf", minish);
current = next;
}
return (EXIT_SUCCESS);
Expand Down
23 changes: 1 addition & 22 deletions src/builtin/env_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,12 @@
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 10:26:59 by dayano #+# #+# */
/* Updated: 2025/04/26 10:19:02 by ttsubo ### ########.fr */
/* Updated: 2025/04/28 16:32:43 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

#include "main.h"

/**
* @brief Get the env value
*
* @param lst
* @param key
* @return char*
* @return NULL: if the key is not found or the key is NULL
*/
char *get_env_value(t_env *current, const char *key)
{
if (!current || !key)
return (NULL);
while (current)
{
if (!ft_strcmp(current->key, key))
return (current->value);
current = current->next;
}
return (NULL);
}

void free_env_content(char *value)
{
free(value);
Expand Down
50 changes: 49 additions & 1 deletion src/builtin/env_utils_2.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:17:22 by dayano #+# #+# */
/* Updated: 2025/04/20 19:36:26 by ttsubo ### ########.fr */
/* Updated: 2025/04/28 16:32:00 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -40,3 +40,51 @@ void handle_error_and_exit(const char *func_name, t_minish *minish)
perror(func_name);
exit(EXIT_FAILURE);
}

static void _set_len_and_pos(char *str, size_t *key_len, size_t *val_start)
{
const char *sep;

sep = ft_strnstr(str, "+=", ft_strlen(str));
if (sep)
{
*key_len = sep - str;
*val_start = *key_len + 2;
}
else
{
sep = ft_strchr(str, '=');
if (sep)
{
*key_len = sep - str;
*val_start = *key_len + 1;
}
else
{
*key_len = ft_strlen(str);
*val_start = 0;
}
}
}

int split_key_value(char *str, char **key_out, char **val_out)
{
size_t key_len;
size_t val_start;

if (!str || !key_out || !val_out)
return (1);
*key_out = NULL;
*val_out = NULL;
_set_len_and_pos(str, &key_len, &val_start);
*key_out = ft_substr(str, 0, key_len);
if (!*key_out)
return (1);
if (val_start > 0)
{
*val_out = ft_substr(str, val_start, ft_strlen(str) - val_start);
if (!*val_out)
return (free(*key_out), *key_out = NULL, 1);
}
return (0);
}
Loading