Git Product home page Git Product logo

42-minishell's Introduction

42-minishell

貝殻のように美しい。

Peek 2021-07-02 03-46

42-minishell's People

Contributors

junnetworks avatar torus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

junnetworks

42-minishell's Issues

`unset PWD` などの時の挙動に対応する

$PWD はシェルの初回起動時に設定する.
一度 unset PWD したら, それ以降の cd . ではシェル変数としての $PWD が設定される. $OLDPWD

image

トヤ氏曰く, 環境変数はリストで持っておくと楽. シェル変数の実装も楽.

今回の課題では hoge=fuga のようなシェル変数への代入は仕様に無いので気にしなくて良さそう.

ctrl-D で終了したらminishellに変な表示が出現 & メモリリーク検出 する

何かしらのコマンドを実行した後に ctrl-DEOFを送信すると以下のような表示が出ます.

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

リダイレクト2つ以上の場合に正しく動作するようにする

リダイレクト2つの場合(echo "HELLO > inner.txt > output.txt とか)に正しく動作するようにする

ちなみに echo "HELLO > inner.txt > output.txt の場合の動作としては

  • inner.txt はファイルは作成されるが何も書き込まれない. また, 既にファイルが作成されている場合は中身を削除(truncate O_TRUNC)する.
  • output.txt には通常通り出力が書き込まれる.

image

入力方向に2つのリダイレクションがある場合

cat < inner.txt < outer.txt

image

入力リダイレクションの一部のファイルが存在しない時

cat < notexists.txt < outer.txt

image

エスケープされたクオーテーションが処理できない

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);

`echo hello 2147483648> file` の修正

[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

Expander をつくる。

基本的に arguments を構成するそれぞれの string に対して変数の展開をするだけ。
変数の値に空白が含まれている場合、その変数がダブルクォートされていなければ、空白で引数を区切る。

リダイレクト先のファイル名の部分に空白が含まれていたらエラーを出力する。
image

`cat | ls` を実行時に bash と同じ挙動になるようにする

cat | ls をbashで実行した時, ls の実行結果が表示された後, cat の標準入力待ちが始まる.

これ即ちbashのパイプはコマンドを順番に実行するのではなく, ただ単にパイプで入手出力をプロセス間でつなぐだけのようだ.

また, cat標準入力待ちの状態で Ctrl+C (SIGINT) を押しても $? が0になることから, $? には最後のコマンドの終了ステータスが入るようだ.

ビルトインコマンド `export` の挙動を bash に合わせる

[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

複数fdに対しての出力リダイレクトの対応

[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

`-c` オプションの実装

-c を実装すると他の方が作ってくれたテスターが使えるようになるので, 実装すると良さそう.

シェルの初期設定を行う

env -i bash を実行すると全ての環境変数を削除した状態でbashが起動される. この状態で起動したbashに存在する環境変数がbashが初期設定で設定する環境変数.

exit の値を int型以上, long型以下 の値に対応させる.

以下のようなケース

[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

`$?` の実装

コマンド実行結果を保持する $? を実装する

セミコロンで終わる行がパースできない

#26

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 がパースできると思うけど、バックトラックを作るのは大変なので、ルールの方を修正して対応したい。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.