[minishell] Design init_job

pinelancer·2021년 10월 26일
0

minishell

목록 보기
6/6

job을 실행시키기 위해서는 입력 문자열을 적절하게 가공해서 리스트를 생성해야 한다.

Roughly written init_job flowchart


Bash Reference Manual, 3.1.1 Shell Operation, 2-4

init_job

t_job init_job(char *input, envp)
{
	return (parser(lexer(input, envp)))
}

Lexer

기능

입력 문자열을 blank 구분자 단위로 끊어 token type을 판단, token node를 생성해 리스트에 추가한다.

Argument: (char *) input string
return: (t_token *)token list

구현 아이디어

  1. (char *)strtok(char *str, char *sep)를 구현하여 입력 문자열을 (char *)blank_separators 단위로 잘라낸다.
  2. token이 word operator 유형(매크로 또는 열거형)을 포함하는 목록을 만든다.
typedef struct s_token
{
	char		*token
	int		type
	struct s_token	*next
}	t_token

고민중

단일 연결리스트, 이중 연결리스트, 큐 중 어떤 자료구조를 사용할 지.
quoting을 어떻게 적절하게 끼워넣을지.

Parser

기능

token리스트를 순회하며 프로세스 구조체에 파싱한다.

Argument: (t_token *)token_list
return: (t_job *)job_list

구현 아이디어

iterates token-list  {

if	token == redirection operator
	redirection type과 함께 노드를 생성하고 file 리스트에 추가. redirection type은
	'<' '>' '<<' '>>' 인지에 따라 결정된다.
	에러처리
	operator의 next token이 word가 아니면 syntax error
	

else if token == control operator '|'
	순회할 때 인풋이 파싱될 프로세스 구조체를 새로 생성한다.
    
else // if token == word
	words의 수에 맞게 문자열 배열을 생성해 순서대로 넣어준다.
	argv[0]은 command 나머지는 command의 argument

}
typedef struct s_file
{
	char		*name
	int		type
	struct s_file	*next
}	t_file
typedef struct s_process
{
	char 			**args
	pid_t 			pid
	char 			completed;
	int 			status;
	t_file			*file;
	struct s_process	*next;
}	t_process

고민중

file type을 macro를 쓸지, enum을 쓸지.
job 구조체 설계.
token리스트 노드를 file리스트 및 process리스트에서 어떻게 재사용할지.

Reference

https://ruslanspivak.com/lsbasi-part13/
https://www.gnu.org/software/bash/manual/html_node/Shell-Operation.html
https://www.gnu.org/software/libc/manual/html_node/Data-Structures.html#Data-Structures

profile
🏃🏾

0개의 댓글