sniper-fly / minishell Goto Github PK
View Code? Open in Web Editor NEWimplementing shell from scratch
implementing shell from scratch
2/25を最終防衛ライン
2/21を目標にissueを片付ける
my_execveにおいて
while (path_db_ptr[i])
{
command_path = get_command_path(cmd[0], path_db_ptr[i]);
//2)prefixをつけてファイルが存在するか(openできるか)検索
//3)ファイルが見つかったらprefixをつけてarguments二次元ポインタに格納する
if (is_there_execute_file_at(command_path))
{
cmd[0] = command_path; // TODO:cmd[0]free必要?
if (execve(command_path, cmd, envp) == ERROR)
occur_an_error_at_execve();
}
free(command_path);
++i;
}
このループを回してコマンドを探して実行しています.
しかし,試しにcmd[0] = command_pathの行をコメントアウトして実行してみたところ特に問題なくコマンドが実行されました.
この行は消すべきでしょうか.
my_pwdのunit_testでセグフォが起きているようです。
グローバル変数が散らばっているとどこでどれだけ定義されているかわからなくなってしまうので、
グローバル変数はmainに定義したほうが良さそうです。
waitのstatusをstatic変数にする
exec_pipe関数の引数にstatusを渡す
議題項目
・utilsに関数がたまり過ぎ
・グローバル変数の定義場所
・自動テスト
・1週間のスケジュール(完成予定日決め)
環境変数PWDがunsetされたときにcdコマンドを使うとmalloc errorと出ます。
execve(char* arg1st, char** arg, char** envp)
my_execve(t_cmd_list *cmd)
このような形式の関数を作る。
中で環境変数リストを環境変数の文字列ポインタ配列に加工し、execveの第三引数に渡す。
execveは絶対パスで渡さないといけない。
PATHを:でsplitして、prefixとしてディレクトリを追加したときにファイルがOPENできるかどうかで判定する。
PATHは左から順に検査する。
ex)
cat -nを実行したい。
PATH=/bin:/usr/bin
/bin/cat openできない。
/usr/bin/cat openできた。
"/usr/bin/cat", "-n", NULL
を格納した二次元ポインタを作成し、execveに渡せば実行可能。
※PATHの検索対象のディレクトリの名前に:が含まれていたらどう動くのか??要検証
exitステータスがおかしいケースがあったのでメモしておきます
minishell> ./minishell
minishell > exit 1
exit
minishell> echo $?
256
create_env_listまたはmake_new_env_node関数でメモリリークが発生してる模様
出力リダイレクトの数だけファイルを作成。実行部分に渡すのは最後のリダイレクトファイル名(と追記フラグ)のみ
リダイレクトのテストで起こったバグを載せておきます
ただコマンド実行上のバグの可能性もあるので,あまり深く探しすぎなくても大丈夫です.
exit 1 1 1 1
のように渡されたとき、shell本体はexitしませんが、my_exitではexitされています。
また、普通にexitしたとき、コマンドラインには
exit
と表示されますが、これは標準出力ではなくエラー出力のようです。
unsetやcdの時に特定の環境変数を探すときに使用します.
前後が番兵 -> 実行
それ以外 ->
export引数無し -> 実行
それ以外 -> 何もしない関数を実行
echo hello | /bin";
echo hogehoge | noexist
これらのコマンドの実行結果が異なるようです。
https://gist.github.com/sasakiyudai/f7cb5ccc5cd85ae7f23d37748bfb1cfa
下記のページは、上記syudaiさんの並列パイプからシステムコールのエラー処理を抜いたものです。
minishell/study/rnakai/multi_pipe_with_same_parent.c
Lines 58 to 59 in 16ce837
ここのwaitが意味ないだろうと思っていたら意味がありありでした。
試しに、この2行を抜いてみるとcat | lsは一瞬で終了しますが、
2行を追加するとcat | lsはcatの終了をwaitするようになります。
waitは親プロセスから作成されたプロセスを、作成した数だけすべて待ってくれるようです。
ちなみに、親プロセスから孫プロセスをwaitすることはできないようです。
https://stackoverflow.com/questions/12822611/fork-and-wait-how-to-wait-for-all-grandchildren-to-finish
https://pubs.opengroup.org/onlinepubs/9699919799/functions/wait.html
A call to the wait() or waitpid() function only returns status on an immediate child process of the calling process; that is, a child that was produced by a single fork() call (perhaps followed by an exec or other function calls) from the parent. If a child produces grandchildren by further use of fork(), none of those grandchildren nor any of their descendants affect the behavior of a wait() from the original parent process. Nothing in this volume of POSIX.1-2017 prevents an implementation from providing extensions that permit a process to get status from a grandchild or any other process, but a process that does not use such extensions must be guaranteed to see status from only its direct children.
1/21木~1/27水の予定
rnakai
複数プロセスでのシグナルの動作を調べる
文字列処理のフローチャート清書
ビルトインコマンドの例外的な動作を調べてまとめる
(echo hello | cd .. | echo world など)
できたら全体の処理の設計
yabumoto
入出力リダイレクトの実装
minishell自体の終了ステータスが、常に0で返ってきているようです。
>hoge >fuga
など、これら単体で入力したときにセグフォします。
parseでこれらの文字列を一度空白文字に置き換えているため、procにはNULLのみが格納されるためNULLアクセスでセグフォになっているようです。
コマンド実行前にシグナル設定を上書きして、コマンドが適切にSIG_INT、SIG_QUITされるようにする
入力待ちの時に使用されるシグナルの設定
入力を受け付ける
stdin, stdout, append
minishellにコマンドライン引数をとるようにして、第一引数のコマンドを実行してくれる処理があるとシェルスクリプトで自動テストを書くことができるのでテストが楽になると思います。
ex)
./minishell 'echo "hello world" '
ただし必然的に空白を含む引数になるため、ダブルクォーテーションかシングルクォーテーションで囲む必要があり、", 'を含んだ例外的で複雑なコマンドを実行するときにめんどくさそうです。
環境変数のグローバルのリスト作成
環境変数のリストから2次元ポインタに加工する関数
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.