-
Notifications
You must be signed in to change notification settings - Fork 1
環境変数の展開機能の追加 #168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
環境変数の展開機能の追加 #168
Conversation
|
ボリューム多いので、解説用にコメント書いておきます。 |
cacapon
left a comment
There was a problem hiding this 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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
utilsにgetenv相当の関数を追加しました。
…もしかしたらすでに関数があって見逃しているかもしれません。
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
環境変数を展開する関数のキモとなる部分です。
次のようなフローで動作します。
- $文字がないか探す
- $文字までの長さを設定する
- prefix, value, suffixをくっつける
- くっつけたのを返す。不要なメモリ領域は解放する。
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 $@ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minishを使う関係で依存関係にあたるファイルが増加したので、それに合わせて実行コマンドを変更しました。
| /* */ | ||
| /* ************************************************************************** */ | ||
|
|
||
| #include "initialize.h" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
initializeとcleanup_minishを使いたかったので追加しています。
fixed #17, #9, #8
構文解析の際に$から始まる文字列は環境変数を展開するようにしました。
以下も対応しています。
"$HOME:$SHELL"'$ENV'./test_parser.out <文字列>で動作確認可能です