// File: /workspace/github_c_makefile_star2_O2/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, 0x5907(%rip)      # 0x8324
leaq -0x15f(%rip), %rsi      # 0x28c5
pushq $0x11
popq %rdi
callq 0x347d
leaq -0x140(%rip), %rsi      # 0x28f3
pushq $0x2
popq %rdi
callq 0x347d
leaq -0x14e(%rip), %rsi      # 0x28f4
pushq $0x14
popq %rdi
callq 0x2270
leaq 0x2619(%rip), %r15      # 0x506a
leaq 0x2617(%rip), %r12      # 0x506f
leaq 0x25f7(%rip), %rdi      # 0x5056
xorl %eax, %eax
callq 0x2180
callq 0x2d24
movq %rax, 0x6096(%rip)      # 0x8b08
testq %rax, %rax
je 0x2b7b
movq %rax, %rbp
movq (%rax), %rsi
testq %rsi, %rsi
je 0x2a96
leaq 0x25d2(%rip), %rdi      # 0x505f
xorl %eax, %eax
callq 0x2180
jmp 0x2a58
xorl %r13d, %r13d
movq 0x18(%rbp), %rax
movq (%rax,%r13,8), %rbx
testq %rbx, %rbx
je 0x2a58
movq (%rbx), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x2260
testl %eax, %eax
je 0x2b06
cmpb $0x66, (%r14)
jne 0x2ad0
cmpb $0x67, 0x1(%r14)
jne 0x2ad0
cmpb $0x0, 0x2(%r14)
je 0x2b67
movq %r14, %rdi
movq %r12, %rsi
callq 0x2260
testl %eax, %eax
je 0x2b0d
movq %r14, %rdi
leaq 0x258b(%rip), %rsi      # 0x5074
callq 0x2260
testl %eax, %eax
je 0x2b14
movl 0x600c(%rip), %eax      # 0x8b04
testq %r13, %r13
je 0x2b1b
testl %eax, %eax
jne 0x2b37
jmp 0x2bd7
callq 0x29d0
jmp 0x2b6c
callq 0x2941
jmp 0x2b6c
callq 0x2982
jmp 0x2b6c
movl %eax, 0x5fef(%rip)      # 0x8b10
callq 0x25a0
movl %eax, 0x5fd8(%rip)      # 0x8b04
movq 0x5fd5(%rip), %rbp      # 0x8b08
testl %eax, %eax
je 0x2b8e
cmpl $0x0, 0x20(%rbp)
jne 0x2b5b
movq 0x18(%rbp), %rax
pushq $-0x1
popq %rcx
cmpq $0x0, 0x8(%rax,%rcx,8)
leaq 0x1(%rcx), %rcx
jne 0x2b44
cmpq $0x1, %rcx
ja 0x2b5b
callq 0x2330
movl $0x2710, %edi           # imm = 0x2710
callq 0x25b0
jmp 0x2b6c
callq 0x29bf
incq %r13
movq 0x5f92(%rip), %rbp      # 0x8b08
jmp 0x2a99
leaq 0x25a4(%rip), %rdi      # 0x5126
callq 0x20c0
xorl %edi, %edi
callq 0x24e0
movq 0x8(%rbp), %rdi
testq %rdi, %rdi
je 0x2bd7
pushq $0x4
popq %rsi
callq 0x2440
movq 0x5f62(%rip), %rcx      # 0x8b08
movq 0x8(%rcx), %rsi
cmpl $-0x1, %eax
jne 0x2bb8
leaq 0x24c3(%rip), %rdi      # 0x5079
jmp 0x2c21
movq %rsi, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x2430
movl %eax, %r14d
movl %eax, %edi
xorl %esi, %esi
callq 0x2170
movq 0x5f31(%rip), %rbp      # 0x8b08
movq 0x10(%rbp), %rdi
testq %rdi, %rdi
je 0x2c92
xorl %esi, %esi
callq 0x2440
movq 0x5f16(%rip), %rcx      # 0x8b08
movq 0x10(%rcx), %rdi
cmpl $-0x1, %eax
jne 0x2c02
movl $0x1a5, %esi            # imm = 0x1A5
jmp 0x2c64
pushq $0x2
popq %rsi
callq 0x2440
cmpl $-0x1, %eax
jne 0x2c2d
movq 0x5ef2(%rip), %rax      # 0x8b08
movq 0x10(%rax), %rsi
leaq 0x2471(%rip), %rdi      # 0x5092
xorl %eax, %eax
callq 0x2180
jmp 0x2d19
movq 0x5ed4(%rip), %rax      # 0x8b08
movq 0x10(%rax), %rdi
leaq 0x28(%rsp), %r14
movq %r14, %rsi
callq 0x22c0
movq 0x5ebc(%rip), %rax      # 0x8b08
movq 0x10(%rax), %rdi
callq 0x2090
movq 0x5eac(%rip), %rax      # 0x8b08
movq 0x10(%rax), %rdi
movl 0x18(%r14), %esi
callq 0x24a0
movq 0x5e98(%rip), %rax      # 0x8b08
movq 0x10(%rax), %rdi
pushq $0x1
popq %rsi
xorl %eax, %eax
callq 0x2430
movl %eax, %r14d
pushq $0x1
popq %rsi
movl %eax, %edi
callq 0x2170
movq 0x5e76(%rip), %rbp      # 0x8b08
cmpl $0x1, 0x20(%rbp)
jne 0x2cb4
callq 0x2120
leaq 0x2407(%rip), %rdi      # 0x50ab
movl %eax, %esi
xorl %eax, %eax
callq 0x2180
movq 0x5e54(%rip), %rbp      # 0x8b08
movq 0x18(%rbp), %rax
pushq $-0x1
popq %rcx
cmpq $0x0, 0x8(%rax,%rcx,8)
leaq 0x1(%rcx), %rcx
jne 0x2cbb
cmpq $0x2, %rcx
jb 0x2cee
movq 0x20(%rbp), %rax
movq %rax, 0x20(%rsp)
movups (%rbp), %xmm0
movups 0x10(%rbp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x2819
jmp 0x2d11
movq (%rbx,%r13,8), %rdi
movq %rbx, %rsi
callq 0x2490
cmpl $-0x1, %eax
jne 0x2d11
movq (%rbx,%r13,8), %rsi
leaq 0x22fe(%rip), %rdi      # 0x5008
xorl %eax, %eax
callq 0x2180
movl %r14d, %edi
callq 0x21d0
pushq $0x1
popq %rdi
callq 0x24e0
nopl (%rax)