// File: ryuichiueda[P]GlueLang/src/Pipeline.cc
// Function: Pipeline::waitCommands(int)
void Pipeline::waitCommands(int pid)
{
	int options = 0;
	int status;
	int wpid = waitpid(pid,&status,options);
	if(wpid < 1){
		m_error_msg = "Command wait error";
		m_exit_status = 2;
		throw this;
	}
		
	if(!WIFEXITED(status)){
		if(WIFSIGNALED(status) && WTERMSIG(status) == 13){//ignore sigpipe
			// The exit status of the command that is suffered a sigpipe
			// should be zero because the command itself does not anything bad. 
			m_exit_status = 0;
		}else if(WIFSIGNALED(status) && WTERMSIG(status) == SIGUSR1){
			exit(0);
		}else{
			m_error_msg = "Irregular command termination";
			m_exit_status = 2;
			throw this;
		}
	}else{
		m_exit_status = WEXITSTATUS(status);
	}

	if(m_env->m_v_opt)
		cerr << "+ pid " << pid << " exit " << m_exit_status << endl;

	// judged at the upper level if !> is used
	if(m_has_or || m_exit_status == 0)
		return;

	if((m_has_and or m_has_then) and m_exit_status == 1)
		return;

	if(m_is_then and m_exit_status > 0){
		m_error_msg = "Error at then part";
		m_exit_status = 8;
		throw this;
	}

	m_error_msg = m_nodes.size() > 1 ? "Pipeline error" : "Command error";
	m_command_error = true;
	throw this;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x20(%rbp)
movl $0x0, -0x10(%rbp)
movl -0xc(%rbp), %edi
movl -0x10(%rbp), %edx
leaq -0x14(%rbp), %rsi
callq 0x6330
movl %eax, -0x18(%rbp)
cmpl $0x1, -0x18(%rbp)
jge 0x1aacc
movq -0x20(%rbp), %rdi
addq $0x60, %rdi
leaq 0x2a3c(%rip), %rsi      # 0x1d4d1
callq 0x6580
movq -0x20(%rbp), %rax
movl $0x2, 0x80(%rax)
movl $0x8, %edi
callq 0x6200
movq %rax, %rdi
movq -0x20(%rbp), %rax
movq %rax, (%rdi)
leaq 0xe0ed(%rip), %rsi      # 0x28bb0
xorl %eax, %eax
movl %eax, %edx
callq 0x66d0
movl -0x14(%rbp), %eax
andl $0x7f, %eax
cmpl $0x0, %eax
je 0x1ab76
movl -0x14(%rbp), %eax
andl $0x7f, %eax
addl $0x1, %eax
movsbl %al, %eax
sarl %eax
cmpl $0x0, %eax
jle 0x1ab09
movl -0x14(%rbp), %eax
andl $0x7f, %eax
cmpl $0xd, %eax
jne 0x1ab09
movq -0x20(%rbp), %rax
movl $0x0, 0x80(%rax)
jmp 0x1ab74
movl -0x14(%rbp), %eax
andl $0x7f, %eax
addl $0x1, %eax
movsbl %al, %eax
sarl %eax
cmpl $0x0, %eax
jle 0x1ab2e
movl -0x14(%rbp), %eax
andl $0x7f, %eax
cmpl $0xa, %eax
jne 0x1ab2e
xorl %edi, %edi
callq 0x64f0
movq -0x20(%rbp), %rdi
addq $0x60, %rdi
leaq 0x2bdb(%rip), %rsi      # 0x1d718
callq 0x6580
movq -0x20(%rbp), %rax
movl $0x2, 0x80(%rax)
movl $0x8, %edi
callq 0x6200
movq %rax, %rdi
movq -0x20(%rbp), %rax
movq %rax, (%rdi)
leaq 0xe045(%rip), %rsi      # 0x28bb0
xorl %eax, %eax
movl %eax, %edx
callq 0x66d0
jmp 0x1ab8c
movq -0x20(%rbp), %rax
movl -0x14(%rbp), %ecx
andl $0xff00, %ecx           # imm = 0xFF00
sarl $0x8, %ecx
movl %ecx, 0x80(%rax)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
testb $0x1, 0xe0(%rax)
je 0x1abeb
movq 0xe444(%rip), %rdi      # 0x28fe8
leaq 0x26c8(%rip), %rsi      # 0x1d273
callq 0x6410
movq %rax, %rdi
movl -0xc(%rbp), %esi
callq 0x66f0
movq %rax, %rdi
leaq 0x2b71(%rip), %rsi      # 0x1d736
callq 0x6410
movq %rax, %rdi
movq -0x20(%rbp), %rax
movl 0x80(%rax), %esi
callq 0x66f0
movq %rax, %rdi
movq 0xe3c2(%rip), %rsi      # 0x28fa8
callq 0x6460
movq -0x20(%rbp), %rax
testb $0x1, 0x87(%rax)
jne 0x1ac05
movq -0x20(%rbp), %rax
cmpl $0x0, 0x80(%rax)
jne 0x1ac0a
jmp 0x1acf8
movq -0x20(%rbp), %rax
testb $0x1, 0x85(%rax)
jne 0x1ac24
movq -0x20(%rbp), %rax
testb $0x1, 0x86(%rax)
je 0x1ac36
movq -0x20(%rbp), %rax
cmpl $0x1, 0x80(%rax)
jne 0x1ac36
jmp 0x1acf8
movq -0x20(%rbp), %rax
testb $0x1, 0x88(%rax)
je 0x1ac96
movq -0x20(%rbp), %rax
cmpl $0x0, 0x80(%rax)
jle 0x1ac96
movq -0x20(%rbp), %rdi
addq $0x60, %rdi
leaq 0x2ade(%rip), %rsi      # 0x1d73d
callq 0x6580
movq -0x20(%rbp), %rax
movl $0x8, 0x80(%rax)
movl $0x8, %edi
callq 0x6200
movq %rax, %rdi
movq -0x20(%rbp), %rax
movq %rax, (%rdi)
leaq 0xdf23(%rip), %rsi      # 0x28bb0
xorl %eax, %eax
movl %eax, %edx
callq 0x66d0
movq -0x20(%rbp), %rdi
addq $0x18, %rdi
callq 0xd900
movq -0x20(%rbp), %rdi
movq %rax, %rcx
leaq 0x2895(%rip), %rsi      # 0x1d546
leaq 0x2a98(%rip), %rax      # 0x1d750
cmpq $0x1, %rcx
cmovaq %rax, %rsi
addq $0x60, %rdi
callq 0x6580
movq -0x20(%rbp), %rax
movb $0x1, 0x84(%rax)
movl $0x8, %edi
callq 0x6200
movq %rax, %rdi
movq -0x20(%rbp), %rax
movq %rax, (%rdi)
leaq 0xdec1(%rip), %rsi      # 0x28bb0
xorl %eax, %eax
movl %eax, %edx
callq 0x66d0
addq $0x20, %rsp
popq %rbp
retq
nop