// File: /workspace/github_cpp_makefile_star2_O1/sanaa-khan[P]mini-linux-shell/gbsh.cpp
// Function: main
int main(int argc, char *argv[]) {

	signal(SIGINT, handle_signal); // program will ignore ctrl+c

	int len = 50;
	char host[len], user[len], cwd[len];
	string command;

	gethostname(host, len);
	getlogin_r(user, len);

	while (true) {
		getcwd(cwd, len);
		cout << "\033[1;36m" << user << "@" << host << "\033[0m";
		cout << "\033[1;35m " << cwd << "\033[0m > ";
		
		getline(cin, command);

		if (command == "exit") {
			exit(0);
		}

		else if (command == "clear")
			cout << "\033c";

		// building vector of commands
		else {
			vector<string> tokens;
			string temp;

			for (int i = 0; i < command.size(); i++) {

				while (command[i] != ' ' and command[i] != '\n' and command[i] != '\t' and command[i] != '\0') {
					temp += command[i];
					i++;
				}

				tokens.push_back(temp);
				temp = "";
			}

			displayVector(tokens);

			string cmd = tokens.front();

			if (cmd == "ls")
				listDirectory(tokens);

			else if (cmd == "pwd")
				printWorkingDirectory(tokens);

			else if (cmd == "cd") 
				changeDirectory(tokens);

			else if (cmd == "environ")
				listEnvVars(tokens);

			else if (cmd == "setenv")
				setEnvVar(tokens);

			else if (cmd == "unsetenv")
				unsetEnvVar(tokens);

			else {
				bool background = false;

				// for background execution
				if (tokens[tokens.size() - 1].size() == 1 and tokens[tokens.size() - 1][0] == '&') {
					background = true;
					tokens.pop_back();
				}

				bool skip = false;

				/// detecting pipe
				for (int i = 0; i < tokens.size(); i++) {
					if (tokens[i].size() == 1 and tokens[i][0] == '|') {
						pipeCommands(tokens);
						skip = true;
					}
				}

				if (!skip) {
					pid_t pid = fork();

					if (pid > 0) { // parent process
						if (!background)
							waitpid(pid, 0, 0); // wait until child of pid is terminated

						if (oRedirect) { // redirect output back to stdout 
							fflush(stdout); 

							dup2(output, 1);
							close(output);

							oRedirect = false;
						}

						if (iRedirect) { // redirect input back to stdin
							fflush(stdin); 

							dup2(input, 0);
							close(input);

							iRedirect = false;
						}
					}

					else if (pid == 0) { // child process
						setenv("SHELL", cwd, 1);
						executeProgram(tokens);
					}

					else
						cout << "Fork unsuccessful";
				}
			}

			tokens.clear();

			pid_t anyChild = 1;

			while (anyChild > 0) { // reaping zombie processes (that may have been running in background previously)
				anyChild = waitpid(-1, 0, WNOHANG);
			}
		} 
	}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x218, %rsp            # imm = 0x218
leaq -0x19(%rip), %rsi       # 0x246c
movl $0x2, %edi
callq 0x21d0
leaq 0x78(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x1e0(%rsp), %rbx
movl $0x32, %esi
movq %rbx, %rdi
callq 0x2250
leaq 0x1a0(%rsp), %rdi
movl $0x32, %esi
callq 0x22e0
movq 0x4af7(%rip), %r12      # 0x6fc8
leaq 0x28(%rsp), %r14
movabsq $0x100000601, %rbp      # imm = 0x100000601
movl $0x32, %esi
leaq 0x160(%rsp), %rdi
callq 0x20a0
movl $0x7, %edx
movq %r12, %rdi
leaq 0x2b1f(%rip), %rsi      # 0x5020
callq 0x21b0
leaq 0x1a0(%rsp), %r12
movq %r12, %rdi
callq 0x2090
movq 0x4aab(%rip), %rdi      # 0x6fc8
movq %r12, %rsi
movq 0x4aa1(%rip), %r12      # 0x6fc8
movq %rax, %rdx
callq 0x21b0
movl $0x1, %edx
movq %r12, %rdi
leaq 0x2aea(%rip), %rsi      # 0x5028
callq 0x21b0
movq %rbx, %rdi
callq 0x2090
movq %r12, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x21b0
movl $0x4, %edx
movq %r12, %rdi
leaq 0x2ac2(%rip), %rsi      # 0x502a
callq 0x21b0
movl $0x8, %edx
movq %r12, %rdi
leaq 0x2ab3(%rip), %rsi      # 0x502f
callq 0x21b0
leaq 0x160(%rsp), %r12
movq %r12, %rdi
callq 0x2090
movq 0x4a30(%rip), %rdi      # 0x6fc8
movq %r12, %rsi
movq 0x4a26(%rip), %r12      # 0x6fc8
movq %rax, %rdx
callq 0x21b0
movl $0x7, %edx
movq %r12, %rdi
leaq 0x2a7f(%rip), %rsi      # 0x5038
callq 0x21b0
movq 0x4a1b(%rip), %rcx      # 0x6fe0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %edx
movq 0x49fd(%rip), %rdi      # 0x6fe0
leaq 0x68(%rsp), %rsi
callq 0x2310
leaq 0x68(%rsp), %r13
movq %r13, %rdi
leaq 0x2a44(%rip), %rsi      # 0x5040
callq 0x2060
testl %eax, %eax
je 0x2ad3
movq %r13, %rdi
leaq 0x2a32(%rip), %rsi      # 0x5045
callq 0x2060
testl %eax, %eax
je 0x27e3
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
xorl %eax, %eax
movq %rax, 0x10(%rsp)
leaq 0x38(%rsp), %rcx
movq %rcx, 0x28(%rsp)
movq %rax, 0x30(%rsp)
movb $0x0, 0x38(%rsp)
cmpq %rax, 0x70(%rsp)
je 0x26aa
xorl %r12d, %r12d
movslq %r12d, %r13
leal 0x1(%r13), %r12d
movq 0x68(%rsp), %rax
movzbl (%rax,%r13), %eax
cmpq $0x20, %rax
ja 0x2669
btq %rax, %rbp
jb 0x267c
movsbl %al, %esi
movq %r14, %rdi
callq 0x2130
incq %r13
incl %r12d
jmp 0x2653
movq %rsp, %rdi
movq %r14, %rsi
callq 0x43b0
movq 0x30(%rsp), %rdx
movq %r14, %rdi
xorl %esi, %esi
leaq 0x2a34(%rip), %rcx      # 0x50cc
xorl %r8d, %r8d
callq 0x22b0
movslq %r12d, %rax
cmpq %rax, 0x70(%rsp)
ja 0x264c
leaq 0x148(%rsp), %r12
movq %r12, %rdi
movq %rsp, %rsi
callq 0x445e
movq %r12, %rdi
callq 0x43ed
movq %r12, %rdi
callq 0x44e8
movq (%rsp), %rax
leaq 0x58(%rsp), %rcx
movq %rcx, 0x48(%rsp)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x48(%rsp), %r13
movq %r13, %rdi
callq 0x454e
movq %r13, %rdi
leaq 0x2952(%rip), %rsi      # 0x504e
callq 0x2060
testl %eax, %eax
je 0x27fc
movq %r13, %rdi
leaq 0x293e(%rip), %rsi      # 0x5051
callq 0x2060
testl %eax, %eax
movq 0x48a7(%rip), %r12      # 0x6fc8
je 0x281c
movq %r13, %rdi
leaq 0x2924(%rip), %rsi      # 0x5055
callq 0x2060
testl %eax, %eax
je 0x283c
movq %r13, %rdi
leaq 0x2910(%rip), %rsi      # 0x5058
callq 0x2060
testl %eax, %eax
je 0x285c
movq %r13, %rdi
leaq 0x2903(%rip), %rsi      # 0x5062
callq 0x2060
testl %eax, %eax
je 0x287c
movq %r13, %rdi
leaq 0x28ea(%rip), %rsi      # 0x5060
callq 0x2060
testl %eax, %eax
je 0x29b5
movq 0x8(%rsp), %rax
cmpq $0x1, -0x18(%rax)
leaq 0x88(%rsp), %r13
jne 0x289c
movq (%rsp), %rcx
movq %rax, %rdx
subq %rcx, %rdx
movq -0x20(%rcx,%rdx), %rcx
cmpb $0x26, (%rcx)
jne 0x289c
leaq -0x20(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq -0x20(%rax), %rdi
addq $-0x10, %rax
movb $0x1, %cl
movl %ecx, 0x24(%rsp)
cmpq %rax, %rdi
je 0x28a4
movq (%rax), %rsi
incq %rsi
callq 0x21a0
jmp 0x28a4
movl $0x2, %edx
movq %r12, %rdi
leaq 0x2859(%rip), %rsi      # 0x504b
callq 0x21b0
jmp 0x24e0
leaq 0x130(%rsp), %r12
movq %r12, %rdi
movq %rsp, %rsi
callq 0x445e
movq %r12, %rdi
callq 0x2bd6
jmp 0x29d0
leaq 0x118(%rsp), %r12
movq %r12, %rdi
movq %rsp, %rsi
callq 0x445e
movq %r12, %rdi
callq 0x3021
jmp 0x29d0
leaq 0x100(%rsp), %r12
movq %r12, %rdi
movq %rsp, %rsi
callq 0x445e
movq %r12, %rdi
callq 0x31fe
jmp 0x29d0
leaq 0xe8(%rsp), %r12
movq %r12, %rdi
movq %rsp, %rsi
callq 0x445e
movq %r12, %rdi
callq 0x3312
jmp 0x29d0
leaq 0xd0(%rsp), %r12
movq %r12, %rdi
movq %rsp, %rsi
callq 0x445e
movq %r12, %rdi
callq 0x3598
jmp 0x29d0
movl $0x0, 0x24(%rsp)
movq (%rsp), %rcx
cmpq %rcx, 0x8(%rsp)
je 0x2923
movl $0x8, %r13d
xorl %r12d, %r12d
xorl %eax, %eax
cmpq $0x1, (%rcx,%r13)
jne 0x28f0
movq -0x8(%rcx,%r13), %rcx
cmpb $0x7c, (%rcx)
jne 0x28f0
leaq 0xa0(%rsp), %r15
movq %r15, %rdi
movq %rsp, %rsi
callq 0x445e
movq %r15, %rdi
callq 0x39b7
movq %r15, %rdi
callq 0x44e8
movb $0x1, %al
incq %r12
movq (%rsp), %rcx
movq 0x8(%rsp), %rdx
subq %rcx, %rdx
sarq $0x5, %rdx
addq $0x20, %r13
cmpq %r12, %rdx
ja 0x28ba
testb $0x1, %al
movq 0x46b3(%rip), %r12      # 0x6fc8
leaq 0x88(%rsp), %r13
jne 0x29d8
callq 0x2040
testl %eax, %eax
jle 0x2a84
cmpb $0x0, 0x24(%rsp)
jne 0x2942
movl %eax, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2110
cmpb $0x1, 0x4877(%rip)      # 0x71c0
jne 0x297c
movq 0x4656(%rip), %rax      # 0x6fa8
movq (%rax), %rdi
callq 0x21c0
movl 0x4858(%rip), %edi      # 0x71b8
movl $0x1, %esi
callq 0x2150
movl 0x4848(%rip), %edi      # 0x71b8
callq 0x2320
movb $0x0, 0x4844(%rip)      # 0x71c0
cmpb $0x1, 0x483e(%rip)      # 0x71c1
jne 0x29d8
movq 0x4634(%rip), %rax      # 0x6fc0
movq (%rax), %rdi
callq 0x21c0
movl 0x4822(%rip), %edi      # 0x71bc
xorl %esi, %esi
callq 0x2150
movl 0x4815(%rip), %edi      # 0x71bc
callq 0x2320
movb $0x0, 0x480e(%rip)      # 0x71c1
jmp 0x29d8
leaq 0xb8(%rsp), %r12
movq %r12, %rdi
movq %rsp, %rsi
callq 0x445e
movq %r12, %rdi
callq 0x37e9
movq %r12, %rdi
callq 0x44e8
movq (%rsp), %r12
movq 0x8(%rsp), %r13
cmpq %r12, %r13
je 0x2a1c
leaq 0x10(%r12), %r15
movq -0x10(%r15), %rdi
cmpq %rdi, %r15
je 0x29ff
movq (%r15), %rsi
incq %rsi
callq 0x21a0
leaq 0x20(%r15), %rax
addq $0x10, %r15
cmpq %r13, %r15
movq %rax, %r15
jne 0x29eb
movq %r12, 0x8(%rsp)
leaq 0x1e0(%rsp), %rbx
movl $0x1, %eax
movq 0x45a0(%rip), %r12      # 0x6fc8
testl %eax, %eax
jle 0x2a3f
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
xorl %esi, %esi
movl $0x1, %edx
callq 0x2110
jmp 0x2a28
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x2a5b
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x21a0
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x2a77
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21a0
movq %rsp, %rdi
callq 0x44e8
jmp 0x24e0
je 0x2a9f
movl $0x11, %edx
movq %r12, %rdi
leaq 0x25da(%rip), %rsi      # 0x506f
callq 0x21b0
jmp 0x29d8
leaq 0x25c3(%rip), %rdi      # 0x5069
leaq 0x160(%rsp), %rsi
movl $0x1, %edx
callq 0x2270
movq %r13, %rdi
movq %rsp, %rsi
callq 0x445e
movq %r13, %rdi
callq 0x3b35
movq %r13, %rdi
jmp 0x29d3
xorl %edi, %edi
callq 0x21e0
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
jmp 0x2b44
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
jmp 0x2b44
jmp 0x2b68
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
jmp 0x2b44
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
jmp 0x2b44
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
jmp 0x2b44
movq %rax, %rbx
leaq 0x118(%rsp), %rdi
jmp 0x2b44
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
jmp 0x2b44
jmp 0x2b68
movq %rax, %rbx
leaq 0x130(%rsp), %rdi
callq 0x44e8
jmp 0x2b6b
jmp 0x2b68
jmp 0x2b8b
movq %rax, %rbx
leaq 0x148(%rsp), %rdi
callq 0x44e8
jmp 0x2b8e
jmp 0x2b8b
movq %rax, %rbx
jmp 0x2bb2
movq %rax, %rbx
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x2b8e
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x21a0
jmp 0x2b8e
jmp 0x2b8b
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x2baa
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21a0
movq %rsp, %rdi
callq 0x44e8
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x2bce
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x21a0
movq %rbx, %rdi
callq 0x22f0