// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xd0, %rsp
movl $0x0, -0x4(%rbp)
movl $0x0, 0x6748(%rip)      # 0x9324
movl $0x11, %edi
leaq -0x1c8(%rip), %rsi      # 0x2a20
callq 0x3d50
movl $0x2, %edi
leaq -0x199(%rip), %rsi      # 0x2a60
callq 0x3d50
movl $0x14, %edi
leaq -0x19a(%rip), %rsi      # 0x2a70
callq 0x2270
leaq 0x3493(%rip), %rdi      # 0x60a9
movb $0x0, %al
callq 0x2170
callq 0x3030
movq %rax, 0x6edf(%rip)      # 0x9b08
cmpq $0x0, 0x6ed7(%rip)      # 0x9b08
jne 0x2c48
leaq 0x3478(%rip), %rdi      # 0x60b2
movb $0x0, %al
callq 0x2170
xorl %edi, %edi
callq 0x24e0
movq 0x6eb9(%rip), %rax      # 0x9b08
cmpq $0x0, (%rax)
je 0x2c6f
movq 0x6eac(%rip), %rax      # 0x9b08
movq (%rax), %rsi
leaq 0x3452(%rip), %rdi      # 0x60b8
movb $0x0, %al
callq 0x2170
jmp 0x2c0f
movl $0x0, -0x8(%rbp)
movq 0x6e8b(%rip), %rax      # 0x9b08
movq 0x18(%rax), %rax
movslq -0x8(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x3020
movq 0x6e71(%rip), %rax      # 0x9b08
movq 0x18(%rax), %rax
movslq -0x8(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax), %rdi
leaq 0x3416(%rip), %rsi      # 0x60c3
callq 0x2260
cmpl $0x0, %eax
jne 0x2cc1
callq 0x2b90
jmp 0x3012
movq 0x6e40(%rip), %rax      # 0x9b08
movq 0x18(%rax), %rax
movslq -0x8(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax), %rdi
leaq 0x33ea(%rip), %rsi      # 0x60c8
callq 0x2260
cmpl $0x0, %eax
jne 0x2cf2
callq 0x2b70
jmp 0x3012
movq 0x6e0f(%rip), %rax      # 0x9b08
movq 0x18(%rax), %rax
movslq -0x8(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax), %rdi
leaq 0x33bc(%rip), %rsi      # 0x60cb
callq 0x2260
cmpl $0x0, %eax
jne 0x2d23
callq 0x2ae0
jmp 0x3012
movq 0x6dde(%rip), %rax      # 0x9b08
movq 0x18(%rax), %rax
movslq -0x8(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax), %rdi
leaq 0x3390(%rip), %rsi      # 0x60d0
callq 0x2260
cmpl $0x0, %eax
jne 0x2d54
callq 0x2b30
jmp 0x3012
movq 0x6dad(%rip), %rax      # 0x9b08
movq 0x18(%rax), %rax
movslq -0x8(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x10(%rbp)
movl $0x1a5, -0x14(%rbp)     # imm = 0x1A5
cmpl $0x0, -0x8(%rbp)
jne 0x2d8f
movl 0x6d86(%rip), %eax      # 0x9b04
movl %eax, 0x6d8c(%rip)      # 0x9b10
callq 0x25a0
movl %eax, 0x6d75(%rip)      # 0x9b04
cmpl $0x0, 0x6d6e(%rip)      # 0x9b04
jne 0x2fc6
cmpl $0x0, -0x8(%rbp)
jne 0x2e0e
movq 0x6d5f(%rip), %rax      # 0x9b08
cmpq $0x0, 0x8(%rax)
je 0x2e0e
movq 0x6d51(%rip), %rax      # 0x9b08
movq 0x8(%rax), %rdi
movl $0x4, %esi
callq 0x2440
cmpl $-0x1, %eax
jne 0x2ded
movq 0x6d37(%rip), %rax      # 0x9b08
movq 0x8(%rax), %rsi
leaq 0x32f9(%rip), %rdi      # 0x60d5
movb $0x0, %al
callq 0x2170
movl $0x1, %edi
callq 0x24e0
movq 0x6d14(%rip), %rax      # 0x9b08
movq 0x8(%rax), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x2430
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %edi
xorl %esi, %esi
callq 0x2160
movq 0x6cf3(%rip), %rax      # 0x9b08
cmpq $0x0, 0x10(%rax)
je 0x2f0b
movq 0x6ce1(%rip), %rax      # 0x9b08
movq 0x10(%rax), %rdi
xorl %esi, %esi
callq 0x2440
cmpl $-0x1, %eax
je 0x2ed1
movq 0x6cc6(%rip), %rax      # 0x9b08
movq 0x10(%rax), %rdi
movl $0x2, %esi
callq 0x2440
cmpl $-0x1, %eax
jne 0x2e78
movq 0x6cac(%rip), %rax      # 0x9b08
movq 0x10(%rax), %rsi
leaq 0x3287(%rip), %rdi      # 0x60ee
movb $0x0, %al
callq 0x2170
movl $0x1, %edi
callq 0x24e0
movq 0x6c89(%rip), %rax      # 0x9b08
movq 0x10(%rax), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x22c0
movq 0x6c72(%rip), %rax      # 0x9b08
movq 0x10(%rax), %rdi
callq 0x2080
movq 0x6c62(%rip), %rax      # 0x9b08
movq 0x10(%rax), %rdi
movl -0x90(%rbp), %esi
callq 0x24a0
movq 0x6c4c(%rip), %rax      # 0x9b08
movq 0x10(%rax), %rdi
movl $0x1, %esi
movb $0x0, %al
callq 0x2430
movl %eax, -0x18(%rbp)
jmp 0x2efe
movq 0x6c30(%rip), %rax      # 0x9b08
movq 0x10(%rax), %rdi
movl -0x14(%rbp), %esi
callq 0x24a0
movq 0x6c1d(%rip), %rax      # 0x9b08
movq 0x10(%rax), %rdi
movl $0x1, %esi
movb $0x0, %al
callq 0x2430
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %edi
movl $0x1, %esi
callq 0x2160
movq 0x6bf6(%rip), %rax      # 0x9b08
cmpl $0x1, 0x20(%rax)
jne 0x2f2d
callq 0x2110
movl %eax, %esi
leaq 0x31e1(%rip), %rdi      # 0x6107
movb $0x0, %al
callq 0x2170
movq 0x6bd4(%rip), %rcx      # 0x9b08
movq 0x20(%rcx), %rdx
movq %rsp, %rax
movq %rdx, 0x20(%rax)
movups (%rcx), %xmm0
movups 0x10(%rcx), %xmm1
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x28e0
cmpl $0x1, %eax
jle 0x2f7e
movq 0x6baa(%rip), %rcx      # 0x9b08
movq 0x20(%rcx), %rdx
movq %rsp, %rax
movq %rdx, 0x20(%rax)
movups (%rcx), %xmm0
movups 0x10(%rcx), %xmm1
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x2920
jmp 0x2fb4
movq -0x10(%rbp), %rax
movslq -0x8(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x10(%rbp), %rsi
callq 0x2490
cmpl $-0x1, %eax
jne 0x2fb2
movq -0x10(%rbp), %rax
movslq -0x8(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
leaq 0x30b0(%rip), %rdi      # 0x605b
movb $0x0, %al
callq 0x2170
jmp 0x2fb4
movl -0x18(%rbp), %edi
callq 0x21d0
movl $0x1, %edi
callq 0x24e0
movq 0x6b3b(%rip), %rax      # 0x9b08
cmpl $0x0, 0x20(%rax)
jne 0x3004
movq 0x6b2e(%rip), %rcx      # 0x9b08
movq 0x20(%rcx), %rdx
movq %rsp, %rax
movq %rdx, 0x20(%rax)
movups (%rcx), %xmm0
movups 0x10(%rcx), %xmm1
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x28e0
cmpl $0x2, %eax
jge 0x3004
callq 0x2330
jmp 0x3006
jmp 0x3006
movl $0x2710, %edi           # imm = 0x2710
callq 0x25b0
jmp 0x3012
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x2c76
jmp 0x2c0f
nopw %cs:(%rax,%rax)
nop