torus / 42-minishell Goto Github PK
View Code? Open in Web Editor NEW貝殻のように美しい。
貝殻のように美しい。
echo hoge$ABC"hoge \\\"hoge"'$ABC' \n
という入力が処理できていなさそうなので, 修正お願いしたいです. (自分のテストケースのコードが間違っている可能性ももちろんあるのですが)
t_parse_buffer buf;
init_buf_with_string(&buf, "echo hoge$ABC\"hoge \\\"hoge\"'$ABC' \n");
t_token tok;
lex_get_token(&buf, &tok);
t_parse_ast *node = parse_command(&buf, &tok);
-c
を実装すると他の方が作ってくれたテスターが使えるようになるので, 実装すると良さそう.
リダイレクションに環境変数が渡されていた場合, コマンドの引数の場合と少し違う挙動をするので別Issueを立てた.
[KO] case: echo hello 2147483648> file
---------------------------------
# minishell: stdout
# bash : stdout
# minishell: stderr
Error: error dup2(fd, STDOUT_NO)
Error: error parent input/output file
# bash : stderr
# minishell: exit status = 255
# bash : exit status = 0
子プロセスを起動するところで設定する。
[KO] case: export APPEND=1; export APPEND+=2; export | grep APPEND
---------------------------------
# minishell: stdout
declare -x APPEND="1"
declare -x APPEND+="2"
# bash : stdout
declare -x APPEND="12"
# minishell: stderr
# bash : stderr
# minishell: exit status = 0
# bash : exit status = 0
[KO] case: export APPEND_NONE=1; export APPEND_NONE+=; export | grep APPEND_NONE
---------------------------------
# minishell: stdout
declare -x APPEND_NONE="1"
declare -x APPEND_NONE+=""
# bash : stdout
declare -x APPEND_NONE="1"
# minishell: stderr
# bash : stderr
# minishell: exit status = 0
# bash : exit status = 0
[KO] case: export =
---------------------------------
# minishell: stdout
# bash : stdout
# minishell: stderr
# bash : stderr
minishell: export: `=': not a valid identifier
# minishell: exit status = 0
# bash : exit status = 1
[KO] case: ./a.out 3> file3 2> file2 1> file1; cat file3 file2 file1 [setup: printf '#include<unistd.h>\nint main(){write(1, "fd1\\n", 4);write(2, "fd2\\n", 4);write(3, "fd3\\n", 4);}' > a.c && gcc a.c]
---------------------------------
# minishell: stdout
fd1
# bash : stdout
fd3
fd2
fd1
# minishell: stderr
fd2
# bash : stderr
# minishell: exit status = 0
# bash : exit status = 0
リダイレクト2つの場合(echo "HELLO > inner.txt > output.txt
とか)に正しく動作するようにする
ちなみに echo "HELLO > inner.txt > output.txt
の場合の動作としては
inner.txt
はファイルは作成されるが何も書き込まれない. また, 既にファイルが作成されている場合は中身を削除(truncate O_TRUNC
)する.output.txt
には通常通り出力が書き込まれる.入力方向に2つのリダイレクションがある場合
cat < inner.txt < outer.txt
入力リダイレクションの一部のファイルが存在しない時
cat < notexists.txt < outer.txt
Backspace \x7F
Del ESC[\x33~
execvp(3)
が今回やりたいことに近い.
command_line ::=
"\n"
| sequential_commands delimiter "\n"
| sequential_commands "\n"
上のルールで a ; \n
をパースすると、まず sequential_commands のパーサが走る。
sequential_commands ::=
piped_commands delimiter sequential_commands
| piped_commands
ここで、a
を読み込んだ後 ;
をこのルールで解釈してしまい、その次に sequential_commands がこないので、パース失敗となってしまう。
この原因は、パーサが 1 個までしかトークンを先読み出来ないからなのでバックトラックを許せばこのままのルールでも多分 a ; \n
がパースできると思うけど、バックトラックを作るのは大変なので、ルールの方を修正して対応したい。
もともとあったシグナルハンドラと端末制御に関する処理を統合する。
何かしらのコマンドを実行した後に ctrl-D
でEOF
を送信すると以下のような表示が出ます.
minish > Error: Parse error.
minish > Error: Parse error.
minish > minishell: : command not found
minishell: =RADDRESS: command not found
minishell: =Rp`: command not found
minishell: =R: command not found
minishell: m+: command not found
minishell: =R: command not found
minish > minishell: !#m+: command not found
minishell: =R: command not found
minishell: =R: command not found
minishell: =R: command not found
minishell: m+: command not found
minish > minishell: =R: command not found
minishell: =R: command not found
minish > AddressSanitizer:DEADLYSIGNAL
=================================================================
==2901292==ERROR: AddressSanitizer: stack-overflow on address 0x7fff523db000 (pc 0x565182afbddf bp 0x7fff523d7190 sp 0x7fff523d7170 T0)
#0 0x565182afbdde in lex_read_word /home/jun/Desktop/work_space/42tokyo/minishell/lexer2.c:30
#1 0x565182afbc8a in lex_get_token /home/jun/Desktop/work_space/42tokyo/minishell/lexer1.c:88
#2 0x565182afc9cf in parse_string /home/jun/Desktop/work_space/42tokyo/minishell/parse2.c:121
#3 0x565182afc9e2 in parse_string /home/jun/Desktop/work_space/42tokyo/minishell/parse2.c:122
#4 0x565182afc9e2 in parse_string /home/jun/Desktop/work_space/42tokyo/minishell/parse2.c:122
#5 0x565182afc85d in parse_arguments /home/jun/Desktop/work_space/42tokyo/minishell/parse2.c:79
#6 0x565182afc889 in parse_arguments /home/jun/Desktop/work_space/42tokyo/minishell/parse2.c:82
#7 0x565182afc7be in parse_command /home/jun/Desktop/work_space/42tokyo/minishell/parse2.c:51
#8 0x565182afc6cb in parse_piped_commands /home/jun/Desktop/work_space/42tokyo/minishell/parse2.c:18
#9 0x565182afc5d2 in parse_sequential_commands /home/jun/Desktop/work_space/42tokyo/minishell/parse1.c:71
#10 0x565182afc48f in parse_command_line /home/jun/Desktop/work_space/42tokyo/minishell/parse1.c:19
#11 0x565182afc411 in main /home/jun/Desktop/work_space/42tokyo/minishell/minishell.c:88
SUMMARY: AddressSanitizer: stack-overflow /home/jun/Desktop/work_space/42tokyo/minishell/lexer2.c:30 in lex_read_word
==2901292==ABORTING
env -i bash
を実行すると全ての環境変数を削除した状態でbashが起動される. この状態で起動したbashに存在する環境変数がbashが初期設定で設定する環境変数.
https://github.com/torus/42-minishell/blob/main/cmd_exec_command.c#L40
このあたり。
umask の値を反映させる必要がある?
cat | ls
をbashで実行した時, ls
の実行結果が表示された後, cat
の標準入力待ちが始まる.
これ即ちbashのパイプはコマンドを順番に実行するのではなく, ただ単にパイプで入手出力をプロセス間でつなぐだけのようだ.
また, cat
標準入力待ちの状態で Ctrl+C (SIGINT) を押しても $?
が0になることから, $?
には最後のコマンドの終了ステータスが入るようだ.
exit -- 42
こういうのが実行された時, GNUのパーサーの関係で--
がオプションとして扱われて, 正常に実行されるが, 今回の課題ではオプション対応は必要ないので無視して良いと判断.
#26 いまはメモリリークしまくり。
コマンド実行結果を保持する $?
を実装する
2> file
の形式。
123> file
とかの場合は、呼び出されたプログラムで write(123, "hoge", 4)
とかが出来ないといけない?(要確認)
#86 の実装に必要かもしれない
以下のようなケース
[KO] case: exit -1
---------------------------------
# minishell: stdout
# bash : stdout
# minishell: stderr
minishell: exit: -1: numeric argument required
# bash : stderr
# minishell: exit status = 2
# bash : exit status = 255
[KO] case: exit 2147483648
---------------------------------
# minishell: stdout
# bash : stdout
# minishell: stderr
minishell: exit: 2147483648: numeric argument required
# bash : stderr
# minishell: exit status = 2
# bash : exit status = 0
[KO] case: exit -2147483648
---------------------------------
# minishell: stdout
# bash : stdout
# minishell: stderr
minishell: exit: -2147483648: numeric argument required
# bash : stderr
# minishell: exit status = 2
# bash : exit status = 0
[KO] case: exit 9223372036854775807
---------------------------------
# minishell: stdout
# bash : stdout
# minishell: stderr
minishell: exit: 9223372036854775807: numeric argument required
# bash : stderr
# minishell: exit status = 2
# bash : exit status = 255
[KO] case: exit " 42 "
---------------------------------
# minishell: stdout
# bash : stdout
# minishell: stderr
minishell: exit: 42 : numeric argument required
# bash : stderr
# minishell: exit status = 2
# bash : exit status = 42
#92 の変更に対するコマンド実行側の対応
-n
オプションcat < a.txt < b.txt
や echo "HELLO" > a.txt > b.txt
のような入力のparserのテストケースを作成する.
終了コードやエラーメッセージなどをbashと同じ挙動にする.
#26 で漏れていた項目。
環境変数をどのタイミングで展開するか迷いどころ.
ASTの構築が完了しコマンドを実行形式(t_command_invocation
)に変換するときか, execvp()
を呼ぶ直前か
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.