// File: /workspace/github_cpp_makefile_star2_O3/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 $0x208, %rsp            # imm = 0x208
leaq -0x19(%rip), %rsi       # 0x246c
movl $0x2, %edi
callq 0x21d0
leaq 0x68(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x1d0(%rsp), %rbx
movl $0x32, %esi
movq %rbx, %rdi
callq 0x2250
leaq 0x190(%rsp), %rdi
movl $0x32, %esi
callq 0x22e0
leaq 0x150(%rsp), %r13
movq 0x4aef(%rip), %r15      # 0x6fc8
leaq 0x18(%rsp), %r14
movabsq $0x100000601, %rbp      # imm = 0x100000601
movl $0x32, %esi
movq %r13, %rdi
callq 0x20a0
movl $0x7, %edx
movq %r15, %rdi
leaq 0x2b1c(%rip), %rsi      # 0x5020
callq 0x21b0
leaq 0x190(%rsp), %r12
movq %r12, %rdi
callq 0x2090
movq %r15, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x21b0
movl $0x1, %edx
movq %r15, %rdi
leaq 0x2af2(%rip), %rsi      # 0x5028
callq 0x21b0
movq %rbx, %rdi
callq 0x2090
movq %r15, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x21b0
movl $0x4, %edx
movq %r15, %rdi
leaq 0x2aca(%rip), %rsi      # 0x502a
callq 0x21b0
movl $0x8, %edx
movq %r15, %rdi
leaq 0x2abb(%rip), %rsi      # 0x502f
callq 0x21b0
movq %r13, %rdi
callq 0x2090
movq %r15, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x21b0
movl $0x7, %edx
movq %r15, %rdi
leaq 0x2a9a(%rip), %rsi      # 0x5038
callq 0x21b0
movq 0x4a36(%rip), %rcx      # 0x6fe0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %edx
movq 0x4a18(%rip), %rdi      # 0x6fe0
leaq 0x58(%rsp), %rsi
callq 0x2310
leaq 0x58(%rsp), %r15
movq %r15, %rdi
leaq 0x2a5f(%rip), %rsi      # 0x5040
callq 0x2060
testl %eax, %eax
je 0x2aee
movq %r15, %rdi
leaq 0x2a4d(%rip), %rsi      # 0x5045
callq 0x2060
testl %eax, %eax
je 0x27c8
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
xorl %ecx, %ecx
movq %rcx, 0x10(%rsp)
leaq 0x28(%rsp), %rax
movq %rax, 0x18(%rsp)
movq %rcx, 0x20(%rsp)
movb $0x0, 0x28(%rsp)
cmpq %rcx, 0x60(%rsp)
je 0x2690
xorl %r13d, %r13d
movslq %r13d, %r12
leal 0x1(%r12), %r13d
movq 0x58(%rsp), %rax
movzbl (%rax,%r12), %eax
cmpq $0x20, %rax
ja 0x264f
btq %rax, %rbp
jb 0x2662
movsbl %al, %esi
movq %r14, %rdi
callq 0x2130
incq %r12
incl %r13d
jmp 0x2639
movq %rsp, %rdi
movq %r14, %rsi
callq 0x4390
movq 0x20(%rsp), %rdx
movq %r14, %rdi
xorl %esi, %esi
leaq 0x2a4e(%rip), %rcx      # 0x50cc
xorl %r8d, %r8d
callq 0x22b0
movslq %r13d, %rax
cmpq %rax, 0x60(%rsp)
ja 0x2631
leaq 0x138(%rsp), %r15
movq %r15, %rdi
movq %rsp, %rsi
callq 0x443e
movq %r15, %rdi
callq 0x43cd
movq %r15, %rdi
callq 0x44c8
movq (%rsp), %rax
leaq 0x48(%rsp), %rcx
movq %rcx, 0x38(%rsp)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x38(%rsp), %r15
movq %r15, %rdi
callq 0x4532
movq %r15, %rdi
leaq 0x296c(%rip), %rsi      # 0x504e
callq 0x2060
testl %eax, %eax
je 0x27e8
movq %r15, %rdi
leaq 0x2958(%rip), %rsi      # 0x5051
callq 0x2060
testl %eax, %eax
leaq 0x108(%rsp), %r12
je 0x2808
movq %r15, %rdi
leaq 0x293d(%rip), %rsi      # 0x5055
callq 0x2060
testl %eax, %eax
je 0x2823
movq %r15, %rdi
leaq 0x2929(%rip), %rsi      # 0x5058
callq 0x2060
testl %eax, %eax
je 0x2848
movq %r15, %rdi
leaq 0x291c(%rip), %rsi      # 0x5062
callq 0x2060
testl %eax, %eax
je 0x286d
movq %r15, %rdi
leaq 0x2903(%rip), %rsi      # 0x5060
callq 0x2060
testl %eax, %eax
je 0x29b3
movq (%rsp), %rcx
movq 0x8(%rsp), %rax
cmpq $0x1, -0x18(%rax)
jne 0x2892
movq %rax, %rdx
subq %rcx, %rdx
movq -0x20(%rcx,%rdx), %rdx
cmpb $0x26, (%rdx)
jne 0x2892
leaq -0x20(%rax), %rdx
movq %rdx, 0x8(%rsp)
movq -0x20(%rax), %rdi
addq $-0x10, %rax
movb $0x1, %r12b
cmpq %rax, %rdi
je 0x2ae6
movq (%rax), %rsi
incq %rsi
callq 0x21a0
movq (%rsp), %rcx
movq 0x8(%rsp), %rax
jmp 0x2895
movl $0x2, %edx
movq 0x47f4(%rip), %r15      # 0x6fc8
movq %r15, %rdi
leaq 0x286d(%rip), %rsi      # 0x504b
callq 0x21b0
jmp 0x24e8
leaq 0x120(%rsp), %r15
movq %r15, %rdi
movq %rsp, %rsi
callq 0x443e
movq %r15, %rdi
callq 0x2bee
jmp 0x29d3
movq %r12, %rdi
movq %rsp, %rsi
callq 0x443e
movq %r12, %rdi
callq 0x3033
movq %r12, %rdi
jmp 0x29d6
leaq 0xd8(%rsp), %rdi
movq %rsp, %rsi
callq 0x443e
leaq 0xd8(%rsp), %r15
movq %r15, %rdi
callq 0x3210
jmp 0x29d3
leaq 0xc0(%rsp), %rdi
movq %rsp, %rsi
callq 0x443e
leaq 0xc0(%rsp), %r15
movq %r15, %rdi
callq 0x3324
jmp 0x29d3
leaq 0xa8(%rsp), %rdi
movq %rsp, %rsi
callq 0x443e
leaq 0xa8(%rsp), %r15
movq %r15, %rdi
callq 0x35bf
jmp 0x29d3
xorl %r12d, %r12d
cmpq %rcx, %rax
je 0x2923
xorl %r13d, %r13d
xorl %edx, %edx
subq %rcx, %rax
sarq $0x5, %rax
movq %r13, %rsi
shlq $0x5, %rsi
addq %rsi, %rcx
addq $0x8, %rcx
leaq 0xf0(%rsp), %r15
cmpq $0x1, (%rcx)
jne 0x28cf
movq -0x8(%rcx), %rsi
cmpb $0x7c, (%rsi)
je 0x28dd
incq %r13
addq $0x20, %rcx
cmpq %r13, %rax
ja 0x28c0
jmp 0x291a
movq %r15, %rdi
movq %rsp, %rsi
callq 0x443e
movq %r15, %rdi
callq 0x39de
movq %r15, %rdi
callq 0x44c8
movq (%rsp), %rcx
movq 0x8(%rsp), %rax
incq %r13
movq %rax, %rsi
subq %rcx, %rsi
sarq $0x5, %rsi
movb $0x1, %dl
cmpq %r13, %rsi
ja 0x28a3
jmp 0x29db
testb $0x1, %dl
jne 0x29db
callq 0x2040
testl %eax, %eax
jle 0x2a8f
testb %r12b, %r12b
jne 0x2940
movl %eax, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2110
cmpb $0x1, 0x4879(%rip)      # 0x71c0
jne 0x297a
movq 0x4658(%rip), %rax      # 0x6fa8
movq (%rax), %rdi
callq 0x21c0
movl 0x485a(%rip), %edi      # 0x71b8
movl $0x1, %esi
callq 0x2150
movl 0x484a(%rip), %edi      # 0x71b8
callq 0x2320
movb $0x0, 0x4846(%rip)      # 0x71c0
cmpb $0x1, 0x4840(%rip)      # 0x71c1
jne 0x29db
movq 0x4636(%rip), %rax      # 0x6fc0
movq (%rax), %rdi
callq 0x21c0
movl 0x4824(%rip), %edi      # 0x71bc
xorl %esi, %esi
callq 0x2150
movl 0x4817(%rip), %edi      # 0x71bc
callq 0x2320
movb $0x0, 0x4810(%rip)      # 0x71c1
jmp 0x29db
leaq 0x90(%rsp), %rdi
movq %rsp, %rsi
callq 0x443e
leaq 0x90(%rsp), %r15
movq %r15, %rdi
callq 0x3810
movq %r15, %rdi
callq 0x44c8
movq (%rsp), %r12
movq 0x8(%rsp), %r13
cmpq %r12, %r13
je 0x2a1f
leaq 0x10(%r12), %r15
movq -0x10(%r15), %rdi
cmpq %rdi, %r15
je 0x2a02
movq (%r15), %rsi
incq %rsi
callq 0x21a0
leaq 0x20(%r15), %rax
addq $0x10, %r15
cmpq %r13, %r15
movq %rax, %r15
jne 0x29ee
movq %r12, 0x8(%rsp)
leaq 0x1d0(%rsp), %rbx
movl $0x1, %eax
leaq 0x150(%rsp), %r13
testl %eax, %eax
jle 0x2a43
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
xorl %esi, %esi
movl $0x1, %edx
callq 0x2110
jmp 0x2a2c
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x2a5f
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21a0
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x2a7b
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x21a0
movq %rsp, %rdi
callq 0x44c8
movq 0x453e(%rip), %r15      # 0x6fc8
jmp 0x24e8
je 0x2aae
movl $0x11, %edx
movq 0x452b(%rip), %rdi      # 0x6fc8
leaq 0x25cb(%rip), %rsi      # 0x506f
callq 0x21b0
jmp 0x29db
leaq 0x25b4(%rip), %rdi      # 0x5069
leaq 0x150(%rsp), %rsi
movl $0x1, %edx
callq 0x2270
leaq 0x78(%rsp), %rdi
movq %rsp, %rsi
callq 0x443e
leaq 0x78(%rsp), %r15
movq %r15, %rdi
callq 0x3b5c
jmp 0x29d3
movq %rdx, %rax
jmp 0x2895
xorl %edi, %edi
callq 0x21e0
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
jmp 0x2b5c
jmp 0x2b80
movq %rax, %rbx
leaq 0xf0(%rsp), %rdi
jmp 0x2b5c
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
jmp 0x2b5c
jmp 0x2b80
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
jmp 0x2b5c
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
jmp 0x2b5c
movq %rax, %rbx
leaq 0xd8(%rsp), %rdi
jmp 0x2b5c
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
jmp 0x2b5c
movq %rax, %rbx
leaq 0x120(%rsp), %rdi
callq 0x44c8
jmp 0x2b83
jmp 0x2b80
jmp 0x2ba3
movq %rax, %rbx
leaq 0x138(%rsp), %rdi
callq 0x44c8
jmp 0x2ba6
jmp 0x2ba3
movq %rax, %rbx
jmp 0x2bca
movq %rax, %rbx
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x2ba6
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21a0
jmp 0x2ba6
jmp 0x2ba3
movq %rax, %rbx
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x2bc2
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x21a0
movq %rsp, %rdi
callq 0x44c8
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x2be6
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x21a0
movq %rbx, %rdi
callq 0x22f0