Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
ast: ongoing...
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyungjun Lee committed Apr 8, 2024
1 parent b0163bb commit 2880d41
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 30 deletions.
8 changes: 4 additions & 4 deletions codes/includes/loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* loader.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* By: nicknamemohaji <nicknamemohaji@student. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/13 16:47:14 by nicknamemoh #+# #+# */
/* Updated: 2024/04/05 11:56:44 by kyungjle ### ########.fr */
/* Updated: 2024/04/08 17:23:58 by nicknamemoh ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -66,9 +66,9 @@ char *ldexec_exec_find_f(char *cmd, t_bool *need_free, const char *path);

// ldexec_run.c

pid_t ldexec_run_bin(t_ld_exec exec);
pid_t ldexec_run_bin(t_ld_exec exec, pid_t pid);
void ldexec_select_type(t_ld_exec exec, t_ld_exec_nodes *node,
t_ld_map_env *env);
t_ld_map_env *env, pid_t pid);

// ldpre_ast.c

Expand Down
29 changes: 13 additions & 16 deletions codes/loader/ldexec_run.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,38 @@
/* ::: :::::::: */
/* ldexec_run.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* By: nicknamemohaji <nicknamemohaji@student. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/14 01:11:55 by nicknamemoh #+# #+# */
/* Updated: 2024/04/05 16:25:50 by kyungjle ### ########.fr */
/* Updated: 2024/04/08 17:27:06 by nicknamemoh ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"
#include "loader.h"
#include "utils.h"

pid_t ldexec_run_bin(t_ld_exec exec);
pid_t ldexec_run_bin(t_ld_exec exec, pid_t pid);
void ldexec_select_type(t_ld_exec exec, t_ld_exec_nodes *node,
t_ld_map_env *env);
t_ld_map_env *env, pid_t pid);

pid_t ldexec_run_bin(t_ld_exec exec)
pid_t ldexec_run_bin(t_ld_exec exec, pid_t pid)
{
pid_t pid;

if (exec.path == NULL)
{
printf("command not found\n");
return (-127);
}
pid = fork();
// TODO pipe subshell인지 감지, fork를 하지 않음
if (pid < 0)
pid = fork();
if (pid < 0)
do_exit("ldexec_run_bin.fork");
else if (pid == 0)
{
if (signal(SIGINT, SIG_DFL) < 0)
do_exit("ldexec_run_bin.signal");
if (signal(SIGKILL, SIG_DFL) < 0)
errno = 0;
signal(SIGINT, SIG_DFL);
signal(SIGKILL, SIG_DFL);
if (errno != 0)
do_exit("ldexec_run_bin.signal");
if (execve(exec.path, exec.argv, exec.envp) < 0)
do_exit("ldexec_run_bin.execve");
Expand All @@ -44,16 +43,14 @@ pid_t ldexec_run_bin(t_ld_exec exec)
}

void ldexec_select_type(t_ld_exec exec, t_ld_exec_nodes *node,
t_ld_map_env *env)
t_ld_map_env *env, pid_t pid)
{
pid_t pid;

if (builtin_isbuiltin(exec.argv[0]))
{
pid = -1;
node->exitcode = builtin_wrapper(exec, env);
}
else
pid = ldexec_run_bin(exec);
pid = ldexec_run_bin(exec, pid);
node->pid = pid;
}
19 changes: 11 additions & 8 deletions codes/loader/ldpre_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* ldpre_ast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* By: nicknamemohaji <nicknamemohaji@student. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/04 11:20:45 by kyungjle #+# #+# */
/* Updated: 2024/04/05 15:51:07 by kyungjle ### ########.fr */
/* Updated: 2024/04/08 17:25:28 by nicknamemoh ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -23,13 +23,12 @@ int ldpre_ast(t_ast_node *ast, t_ld_map_env *env,
{
case NODE_COMMAND:
{
// TODO 커맨드 실행 구조 검토
// 파이프일때 손자까지 만들 필요가 있나...
t_ld_exec_nodes *node;
t_bool free_flag;
pid_t pid;
int exitcode;


// 1. 실행정보 기입
node = malloc(1 * sizeof(t_ld_exec_nodes));
if (node == NULL)
do_exit("ldpre_ast.malloc");
Expand All @@ -38,6 +37,9 @@ int ldpre_ast(t_ast_node *ast, t_ld_map_env *env,
(node->exec).envp = ldpre_env_toenvp_f(env);
(node->exec).path = ldexec_exec_find_f(
(node->exec).argv[0], &free_flag, ldpre_env_fetch("PATH", env));

// 2. 실행
// 2-1. 파이프이면 fork
if (exec != NULL)
{
pid = fork();
Expand All @@ -46,23 +48,24 @@ int ldpre_ast(t_ast_node *ast, t_ld_map_env *env,
}
else
pid = -1;
// 2-2. !PIPE || (PIPE && CHILD)
if (pid <= 0)
{
ldexec_select_type(node->exec, node, env);
ldexec_select_type(node->exec, node, env, pid);
exitcode = exec_cleanup(node, env, free_flag);
}
// PIPE && !CHILD
else
{
exec->pid = pid;
while (exec->next != NULL)
exec = exec->next;
exec->next = node;
return (-1);
if (free_flag)
free((node->exec).path);
}
if (pid == 0)
exit(exitcode);
// 3. 정리
return (exitcode);
}
break;
Expand Down
4 changes: 2 additions & 2 deletions codes/loader/loader_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* loader_wrapper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* By: nicknamemohaji <nicknamemohaji@student. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/01 13:16:24 by nicknamemoh #+# #+# */
/* Updated: 2024/04/04 18:04:20 by kyungjle ### ########.fr */
/* Updated: 2024/04/08 16:52:02 by nicknamemoh ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down

0 comments on commit 2880d41

Please sign in to comment.