Skip to content

Conversation

@cacapon
Copy link
Owner

@cacapon cacapon commented Apr 26, 2025

fixed #17, #9, #8
構文解析の際に$から始まる文字列は環境変数を展開するようにしました。
以下も対応しています。

  • トークン内に複数環境変数がある e.g. "$HOME:$SHELL"
  • シングルクオートだと展開しない e.g. '$ENV'

./test_parser.out <文字列>で動作確認可能です

@cacapon cacapon requested a review from dayano74 April 26, 2025 12:18
@cacapon cacapon self-assigned this Apr 26, 2025
@cacapon
Copy link
Owner Author

cacapon commented Apr 26, 2025

ボリューム多いので、解説用にコメント書いておきます。
ご参考ください。

@cacapon cacapon changed the title Feature/17 expand env 環境変数の展開機能の追加 Apr 26, 2025
Copy link
Owner Author

@cacapon cacapon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

以下解説になります。
わからないところがあったら教えてください〜


# include "struct.h"

char *get_env_value(t_env *current, const char *key);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utilsにgetenv相当の関数を追加しました。

…もしかしたらすでに関数があって見逃しているかもしれません。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このヘッダーでは環境変数展開用の関数・構造体を宣言しています。

# include "expand_env.h"

t_cmd **parser(char **tokens);
t_cmd **parser(char **tokens, t_minish *minish);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

envにアクセスしたかったのでminish構造体を渡すように変更しています。

void free_cmds(t_cmd **cmds, size_t count);
t_cmd **allocate_cmds(char **tokens);
t_cmd **setup_cmds(t_cmd **cmds, char **tokens);
t_cmd **setup_cmds(t_cmd **cmds, char **tokens, t_minish *minish);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parserと同様

* @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)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keyを前から探して見つかったらvalueを返すという流れで動作します。

* @param minish
* @return t_tokenizer*
*/
char *expand_env(char *token, t_minish *minish)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

環境変数を展開する関数のキモとなる部分です。
次のようなフローで動作します。

  • $文字がないか探す
  • $文字までの長さを設定する
  • prefix, value, suffixをくっつける
  • くっつけたのを返す。不要なメモリ領域は解放する。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expand_env_utilsは環境変数の文字列をチェックするための関数を作成しました。
Bashの場合、環境変数の最初の文字はアルファベットか_で始まるので、is_key_startはそのチェックを行います。
それ以降の文字はさらに数字も使えるので半角英数字か_なのかをチェックする関数is_key_charを用意しました。

cmd->type = REDIR_NONE;
}

static char *_expand_arg(char *token, t_minish *minish)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tokenを環境変数の値に展開した状態でcmdsのargvに代入していくための関数です。
$が含まれていると展開後、改めて初めからチェックし始めるので、$が無くなるまで展開し続けます。


test_%.out: tests/parser/test_%.c
$(CC) $^ src/parser/*.c src/tokenizer/*.c $(L_FLG) $(I_FLG) -o $@
$(CC) $^ src/parser/*.c src/tokenizer/*.c src/builtin/*.c src/initialize.c $(L_FLG) $(I_FLG) -o $@
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minishを使う関係で依存関係にあたるファイルが増加したので、それに合わせて実行コマンドを変更しました。

/* */
/* ************************************************************************** */

#include "initialize.h"
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initializecleanup_minishを使いたかったので追加しています。

@dayano74 dayano74 merged commit d5f8bf9 into main Apr 28, 2025
1 check passed
@dayano74 dayano74 deleted the feature/17_expand_env branch April 28, 2025 02:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

$<文字列>形式の環境変数を処理します。

3 participants