Giter VIP home page Giter VIP logo

42cursus_03_minishell's Introduction

Hi there🕺 I'm zhy2on

Anurag's GitHub stats Top Langs Solved.ac프로필

Blog (۶•̀ᴗ•́)۶ ✍

42cursus_03_minishell's People

Contributors

maindishes avatar zhy2on avatar

Watchers

 avatar

Forkers

maindishes

42cursus_03_minishell's Issues

pipe 구현

  • fork 전 파이프 연결
  • 입력스트림을 pd[0]으로 연결
  • 출력스트림을 pd[1]로 연결
  • 부모 프로세스 시그널 핸들러 다시 세팅
  • 자식 프로세스 시그널 인터럽트 종료일 경우 따로 처리
  • 가장 마지막에 fork한 자식프로세스의 exit_code가 최종적으로 업데이트 되도록 하기
    • waitpid의 반환값으로 pid값 받아서 비교하면 됨
  • 다음 파이프에서 프로세스가 실행되면 이전 pipe를 close 하고 close한 파이프에 이전 프로세스가 접근하려고 할 때 SIGPIPE가 전달되어 종료될 수 있게 해야함. --> 자식 프로세스에서만 dup2하고 부모프로세스에선 pipe한 fd값 백업해두고 실행할 때마다 이전 pipe fd값 close
  • 파이프여도 무조건 heredoc이 먼저 검사 됨
  • 이 에러 생각해보기 --> 흠 근데 다시 하니까 잘 됨
bash-3.2$ echo a | cat <<eof | cat <<abc | echo b
> eof
> abc
b
bash: echo: write error: Broken pipe

export 수정 필요

  • export a; export b=c; export c 했을 시 다음 minishell로 진입할 때는 b=c만 환경변수가 전달돼야함

cmdline parsing 구현

  • 토큰 리스트 구현
    • 따옴표 안일 경우에만 널문자열 토큰으로 저장
  • 따옴표 처리
    • unclosed qutation mark 일 경우 에러 처리
  • $ 처리
    • 작은 따옴표 안이 아닌 경우에만 효력 있음
  • token to args
  • $ 치환
    • env list에서 찾아서 치환
    • value값이 널인 경우도 있기 때문에 value를 리턴하는 게 아니라 key가 존재하는 경우 env 주소를 리턴하게 하고, 없는 경우 널 리턴하게 해서 구분
    • $? 처리
  • << >> 처리
  • > 다음에 바로 | 오는 건 무시 그냥 >로 처리
  • bash: syntax error near unexpected 어쩌구 에러 처리 구현
  • export a="cho hi" e$a 경우 echo hi 가 실행되도록 구현

builtin 구현

  • echo
    • n 옵션
      • -nnnnnn 도 -n으로 인식
      • 아닐 경우 옵션 아닌 그냥 문자열로 인식
  • cd
    • OLDPWD 업데이트
    • cd ~ 또는 그냥 cd 홈디렉토리 이동
    • cd - 이전 디렉토리 이동
  • pwd
    • pwd실행 후 exit_code = 0으로 업데이트
  • export
    • export a // export a= 구분하기. 전자는 그냥 key만 등록. value 포인터 자체가 널. 후자는 value에 널문자열 등록. 전자는 env시에는 출력 안 돼야 함. 후자는 env시 출력 돼야 함
    • =만 들어왔을 때 invalid key로 처리해야 함
    • key 값이 원래 있는 경우 value를 업데이트. but value값이 있는 경우 '=' 없이 그냥 export key 로 등록할 경우 값 업데이트 되지 않음.
    • ascii 상으로 key값이 정렬된 상태로 출력
  • unset
  • env
    • value가 널이 아닌 경우만 출력(= 널문자열인 경우는 출력).
  • exit
    • exit 인자로 exit status 받게 하기
    • exit 다음에 numeric 아니면 bash: exit: djdj: numeric argument required
    • exit 다음에 numeric인데 다음이 널이 아니면 exit: too many arguments

error case

minishell(4496,0x10e3d1dc0) malloc: Incorrect checksum for freed object 0x7f949b408320: probably modified after being freed.
Corrupt value: 0x200000000007f949
minishell(4496,0x10e3d1dc0) malloc: *** set a breakpoint in malloc_error_break to debug

정적 변수 선언 없이 함수 인자에 리턴값으로 할당한 값을 넘겨줄 때 오류 발생.
error 케이스로 실행시
환경변수가 총 16 글자 일때 만 에러가 발생 ( 나머지 경우는 운이 좋아서 된거인가요?)

minishell$ export TEST=/Users/zhy2on/t
minishell$ echo $TEST
/Users/zhy2on/t
minishell$ echo $TEST
minishell(10528,0x100d38580) malloc: Heap corruption detected, free list is damaged at 0x6000005b0190
*** Incorrect guard value: 75818854908160
minishell(10528,0x100d38580) malloc: *** set a breakpoint in malloc_error_break to debug
zsh: abort      ./minishell

특정 문자열 길이에서 free 오류 발생;;

/* error case */
void	str_to_token_sub(t_mini *mini, char *str, char *ret)
{
	char	*value;
	char	*s;

	while (*str)
	{
		if (*str == - '$')
		{
			value = search_dollar_value(mini, str);
			s = value;
			while (s && *s)
				*ret++ = *s++;
			free(value);
			str = end_of_dollar(str);
		}
		else if (*str == - '>' || *str == - '<'
			|| *str == - '&' || *str == - '|')
		{
			*str = -(*str);
			*ret++ = *str;
		}
		*ret++ = *str++;
	}
	*ret = '\0';
}

echo "$TEST$TEST=lol$TEST"

it's now working friend!

bash output : =lol

your output : �TEST=lol

if i set TEST env to abc then,

bash output : abc�TEST=lolabc

your output : abcabc=lolabc

exit

exit print 하는 fd STDERR로 안 돼있는 것들 바꿔야 함

execve 구현

  • which 명령어에 사용할 envs list 2차원 문자열배열로 바꾸기
  • fork 후 execve로 which 명령어 실행하여 명령어 절대 경로 찾기
  • which로 찾아지지 않았을 때
    • stat 함수로 검사 후 권한이 없으면 Permission denied 에러 출력 후 126 리턴
    • 디렉토리였으면 is a directory 에러 출력 후 126 리턴
    • 그게 아니라면 command not found 에러 출력 후 127 리턴
  • which로 찾아졌을 때
    • pipe로 실행하는 경우가 아니라면 fork 후 자식프로세스에서 execve 실행
    • pipe로 실행하는 경우라면 fork 없이 그냥 execve 실행
  • 절대 경로 커맨드인 경우 == ft_strchr("/")이 1인 경우, which로 찾지 않고 바로 실행. 또한 안 찾아지면 command not found가 아니라 No such file or directory로 에러처리 해야함.
  • PATH unset한 경우는 무조건 No such file or directory 에러로 처리됨. 다시 찾아보기

redirection 구현

  • < 처리
  • > 처리
  • << 처리
    • stdin에서 heredoc 먼저 처리 하고 heredoc fd 저장해놓은 상태에서 handle_redirect 쭉 진행
    • stdin에서 heredoc 처리하다가 ctrl+c 들어오면 모든 입력 멈춤. 뒤에 파싱 진행 안 함
    • 파이프든 뭐든 통틀어서 제일 먼저 인풋스트림 heredoc부터 검사해야 함
  • >> 처리

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.