// File: /workspace/github_c_makefile_star2_O1/johanDa9u[P]MiniShell/src/shell.c
// Function: main
int main()
{

	suspendedJobs = 0;

	/*----------------------------- Signal handlers ---------------------------- */
	Signal(SIGCHLD, handle_sigchld);
	Signal(SIGINT, handle_sigint);
	signal(SIGTSTP, handle_stp);

	while (1)
	{
		int i;
		printf("shell > ");
		l = readcmd();
		/*If input stream closed, normal termination */
		if (!l)
		{
			printf("exit\n");
			exit(0);
		}

		if (l->err)
		{ /*Syntax error, read another command */
			printf("error: %s\n", l->err);
			continue;
		}

		/*Visit each command of the pipe */
		for (i = 0; l->seq[i] != 0; i++)
		{
			if (strcmp(*(l->seq[i]), "quit") == 0)
			{
				quit();
				continue;
			}
			if (strcmp(*(l->seq[i]), "fg") == 0)
			{
				fg();
				continue;
			}
			if (strcmp(*(l->seq[i]), "jobs") == 0)
			{
				get_jobs();
				continue;
			}
			if (strcmp(*(l->seq[i]), "stop") == 0)
			{
				to_stop();
				continue;
			}
			char **cmd = l->seq[i];
			mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_WRONLY | O_RDONLY;
			if (i == 0)
			{
				aux_pid = child_pid;
				child_pid = fork();
			}
			if (child_pid == 0)
			{
				int file_desc;
				if (i == 0 && l->in)
				{
					if ((access(l->in, R_OK) == -1))
					{
						printf("Erreur while loading %s\n", l->in);
						exit(1);
					}
					file_desc = open((l->in), O_RDONLY);
					dup2(file_desc, STDIN_FILENO);
				}

				if (l->out)
				{

					/*------------------------------ File exists? ------------------------------ */
					if ((access(l->out, F_OK)) != -1)
					{

						/*--------------------------- Can we write in it? -------------------------- */

						if ((access(l->out, W_OK) == -1))
						{
							printf("%s : Permission denied.\n", l->out);
							exit(1);
						}
						else
						{

							/*-------------------------- File exists. We recreate it with relevant permissions in order to overwrite its contetns. -------------------------- */

							struct stat fileStat;
							stat(l->out, &fileStat);
							remove((l->out));
							creat(l->out, fileStat.st_mode);
							file_desc = open((l->out), O_WRONLY);
						}
					}
					else
					{

						/*--------------------------- File does not exist. -------------------------- */

						creat(l->out, mode);
						file_desc = open((l->out), O_WRONLY);
					}
					dup2(file_desc, STDOUT_FILENO);
				}
				if (l->in_background == 1)
				{

					/*--------------------  Run ps command to see the running children processes. -------------------- */
					printf("Process[%d] is running in the background\n", getpid());
				}

				/*------------------------ Count number of commands ------------------------ */

				if (count(*l) > 1)
				{

					/*------------------ More than 1 command<=> piped command ----------------- */

					runPipes(*l);
				}
				else
				{

					/*--------------------- Only 1 command to be executed. --------------------- */

					if (execvp(cmd[i], cmd) == -1)
					{
						printf("%s : command not found.\n", cmd[i]);
					}
				}

				/*------------- We close our file descriptor and exit the child ------------ */
				close(file_desc);
				exit(1);
			}
			else
			{

				/*--------------------------------- PARENT --------------------------------- */
				if (l->in_background == 0 && count(*l) < 2)
				{

					/*------------ Not a piped command NOR running in the background ----------- */
					pause();
				}
				else {}
				/*--- We stop the program for 0.1 seconds in order to re-maintain the order of the execution. ---*/
				usleep(10000);
			}
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movl $0x0, 0x575b(%rip)      # 0x8324
leaq -0x1e2(%rip), %rsi      # 0x29ee
movl $0x11, %edi
callq 0x3725
leaq -0x1cd(%rip), %rsi      # 0x2a14
movl $0x2, %edi
callq 0x3725
leaq -0x1dd(%rip), %rsi      # 0x2a15
movl $0x14, %edi
callq 0x2270
leaq 0x2453(%rip), %rdi      # 0x5056
xorl %eax, %eax
callq 0x2180
callq 0x2f34
movq %rax, 0x5ef2(%rip)      # 0x8b08
testq %rax, %rax
je 0x2d75
leaq 0x2444(%rip), %r14      # 0x506a
leaq 0x2442(%rip), %r15      # 0x506f
leaq 0x243e(%rip), %r12      # 0x5072
movq (%rax), %rsi
testq %rsi, %rsi
je 0x2c4f
leaq 0x241c(%rip), %rdi      # 0x505f
xorl %eax, %eax
callq 0x2180
jmp 0x2d52
movq 0x5eb2(%rip), %rax      # 0x8b08
movq 0x18(%rax), %rax
movq (%rax), %rbx
testq %rbx, %rbx
je 0x2d52
xorl %r13d, %r13d
movq (%rbx), %rbp
movq %rbp, %rdi
movq %r14, %rsi
callq 0x2260
testl %eax, %eax
je 0x2d1c
movq %rbp, %rdi
movq %r15, %rsi
callq 0x2260
testl %eax, %eax
je 0x2d23
movq %rbp, %rdi
movq %r12, %rsi
callq 0x2260
testl %eax, %eax
je 0x2d2a
movq %rbp, %rdi
leaq 0x23c8(%rip), %rsi      # 0x5077
callq 0x2260
testl %eax, %eax
je 0x2d31
testq %r13, %r13
jne 0x2cd4
movl 0x5e41(%rip), %eax      # 0x8b04
movl %eax, 0x5e47(%rip)      # 0x8b10
callq 0x25a0
movl %eax, 0x5e30(%rip)      # 0x8b04
cmpl $0x0, 0x5e29(%rip)      # 0x8b04
je 0x2d88
movq 0x5e20(%rip), %rax      # 0x8b08
cmpl $0x0, 0x20(%rax)
jne 0x2d10
movq 0x18(%rax), %rax
movq $-0x1, %rcx
cmpq $0x0, 0x8(%rax,%rcx,8)
leaq 0x1(%rcx), %rcx
jne 0x2cf9
cmpq $0x1, %rcx
ja 0x2d10
callq 0x2330
movl $0x2710, %edi           # imm = 0x2710
callq 0x25b0
jmp 0x2d36
callq 0x2b7c
jmp 0x2d36
callq 0x2b31
jmp 0x2d36
callq 0x2a95
jmp 0x2d36
callq 0x2ae2
movq 0x5dcb(%rip), %rax      # 0x8b08
movq 0x18(%rax), %rax
movq 0x8(%rax,%r13,8), %rbx
incq %r13
testq %rbx, %rbx
jne 0x2c69
leaq 0x22fd(%rip), %rdi      # 0x5056
xorl %eax, %eax
callq 0x2180
callq 0x2f34
movq %rax, 0x5d9c(%rip)      # 0x8b08
testq %rax, %rax
jne 0x2c34
leaq 0x23ad(%rip), %rdi      # 0x5129
callq 0x20c0
xorl %edi, %edi
callq 0x24e0
testq %r13, %r13
jne 0x2dd7
movq 0x5d74(%rip), %rax      # 0x8b08
movq 0x8(%rax), %rdi
testq %rdi, %rdi
je 0x2dd7
movl $0x4, %esi
callq 0x2440
movq 0x5d5a(%rip), %rcx      # 0x8b08
movq 0x8(%rcx), %rsi
cmpl $-0x1, %eax
jne 0x2dc0
leaq 0x22be(%rip), %rdi      # 0x507c
jmp 0x2e2a
movq %rsi, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x2430
movl %eax, %ebp
movl %eax, %edi
xorl %esi, %esi
callq 0x2170
movq 0x5d2a(%rip), %rax      # 0x8b08
movq 0x10(%rax), %rdi
testq %rdi, %rdi
je 0x2e97
xorl %esi, %esi
callq 0x2440
movq 0x5d0f(%rip), %rcx      # 0x8b08
movq 0x10(%rcx), %rdi
cmpl $-0x1, %eax
jne 0x2e09
movl $0x1a5, %esi            # imm = 0x1A5
jmp 0x2e6d
movl $0x2, %esi
callq 0x2440
cmpl $-0x1, %eax
jne 0x2e36
movq 0x5ce9(%rip), %rax      # 0x8b08
movq 0x10(%rax), %rsi
leaq 0x226b(%rip), %rdi      # 0x5095
xorl %eax, %eax
callq 0x2180
jmp 0x2f27
movq 0x5ccb(%rip), %rax      # 0x8b08
movq 0x10(%rax), %rdi
leaq 0x28(%rsp), %r14
movq %r14, %rsi
callq 0x22c0
movq 0x5cb3(%rip), %rax      # 0x8b08
movq 0x10(%rax), %rdi
callq 0x2090
movq 0x5ca3(%rip), %rax      # 0x8b08
movq 0x10(%rax), %rdi
movl 0x18(%r14), %esi
callq 0x24a0
movq 0x5c8f(%rip), %rax      # 0x8b08
movq 0x10(%rax), %rdi
movl $0x1, %esi
xorl %eax, %eax
callq 0x2430
movl %eax, %ebp
movl %eax, %edi
movl $0x1, %esi
callq 0x2170
movq 0x5c6a(%rip), %rax      # 0x8b08
cmpl $0x1, 0x20(%rax)
jne 0x2eb9
callq 0x2120
leaq 0x21fe(%rip), %rdi      # 0x50ae
movl %eax, %esi
xorl %eax, %eax
callq 0x2180
movq 0x5c48(%rip), %rax      # 0x8b08
movq 0x18(%rax), %rcx
movq $-0x1, %rdx
cmpq $0x0, 0x8(%rcx,%rdx,8)
leaq 0x1(%rdx), %rdx
jne 0x2ecb
cmpq $0x2, %rdx
jb 0x2efd
movq 0x20(%rax), %rcx
movq %rcx, 0x20(%rsp)
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x2931
jmp 0x2f20
movq (%rbx,%r13,8), %rdi
movq %rbx, %rsi
callq 0x2490
cmpl $-0x1, %eax
jne 0x2f20
movq (%rbx,%r13,8), %rsi
leaq 0x20ef(%rip), %rdi      # 0x5008
xorl %eax, %eax
callq 0x2180
movl %ebp, %edi
callq 0x21d0
movl $0x1, %edi
callq 0x24e0
nopl (%rax)