// File: /workspace/github_c_makefile_star2_O3/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, 0x575c(%rip)      # 0x8324
leaq -0x1e1(%rip), %rsi      # 0x29ee
movl $0x11, %edi
callq 0x36cd
leaq -0x1cc(%rip), %rsi      # 0x2a14
movl $0x2, %edi
callq 0x36cd
leaq -0x1dc(%rip), %rsi      # 0x2a15
movl $0x14, %edi
callq 0x2270
leaq 0x2454(%rip), %rdi      # 0x5056
xorl %eax, %eax
callq 0x2180
callq 0x2f24
movq %rax, 0x5ef3(%rip)      # 0x8b08
testq %rax, %rax
je 0x2d6f
movq %rax, %rbp
leaq 0x2442(%rip), %r14      # 0x506a
leaq 0x2440(%rip), %r15      # 0x506f
movq (%rbp), %rsi
testq %rsi, %rsi
je 0x2c4b
leaq 0x2420(%rip), %rdi      # 0x505f
xorl %eax, %eax
callq 0x2180
jmp 0x2d49
movq 0x18(%rbp), %rax
movq (%rax), %rbx
testq %rbx, %rbx
je 0x2d49
xorl %r12d, %r12d
movq (%rbx), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x2260
testl %eax, %eax
je 0x2cbf
cmpb $0x66, (%r13)
jne 0x2c89
cmpb $0x67, 0x1(%r13)
jne 0x2c89
cmpb $0x0, 0x2(%r13)
je 0x2d28
movq %r13, %rdi
movq %r15, %rsi
callq 0x2260
testl %eax, %eax
je 0x2cc6
movq %r13, %rdi
leaq 0x23d2(%rip), %rsi      # 0x5074
callq 0x2260
testl %eax, %eax
je 0x2ccd
movl 0x5e53(%rip), %eax      # 0x8b04
testq %r12, %r12
je 0x2cd4
testl %eax, %eax
jne 0x2cf4
jmp 0x2dcd
callq 0x2b7b
jmp 0x2d2d
callq 0x2a95
jmp 0x2d2d
callq 0x2ae2
jmp 0x2d2d
movl %eax, 0x5e36(%rip)      # 0x8b10
callq 0x25a0
movl %eax, 0x5e1f(%rip)      # 0x8b04
movq 0x5e1c(%rip), %rbp      # 0x8b08
testl %eax, %eax
je 0x2d82
cmpl $0x0, 0x20(%rbp)
jne 0x2d1c
movq 0x18(%rbp), %rax
movq $-0x1, %rcx
cmpq $0x0, 0x8(%rax,%rcx,8)
leaq 0x1(%rcx), %rcx
jne 0x2d05
cmpq $0x1, %rcx
ja 0x2d1c
callq 0x2330
movl $0x2710, %edi           # imm = 0x2710
callq 0x25b0
jmp 0x2d2d
callq 0x2b30
movq 0x5dd4(%rip), %rbp      # 0x8b08
movq 0x18(%rbp), %rax
movq 0x8(%rax,%r12,8), %rbx
incq %r12
testq %rbx, %rbx
jne 0x2c5e
leaq 0x2306(%rip), %rdi      # 0x5056
xorl %eax, %eax
callq 0x2180
callq 0x2f24
movq %rax, %rbp
movq %rax, 0x5da2(%rip)      # 0x8b08
testq %rax, %rax
jne 0x2c2f
leaq 0x23b0(%rip), %rdi      # 0x5126
callq 0x20c0
xorl %edi, %edi
callq 0x24e0
movq 0x8(%rbp), %rdi
testq %rdi, %rdi
je 0x2dcd
movl $0x4, %esi
callq 0x2440
movq 0x5d6c(%rip), %rcx      # 0x8b08
movq 0x8(%rcx), %rsi
cmpl $-0x1, %eax
jne 0x2dae
leaq 0x22cd(%rip), %rdi      # 0x5079
jmp 0x2e19
movq %rsi, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x2430
movl %eax, %r13d
movl %eax, %edi
xorl %esi, %esi
callq 0x2170
movq 0x5d3b(%rip), %rbp      # 0x8b08
movq 0x10(%rbp), %rdi
testq %rdi, %rdi
je 0x2e8e
xorl %esi, %esi
callq 0x2440
movq 0x5d20(%rip), %rcx      # 0x8b08
movq 0x10(%rcx), %rdi
cmpl $-0x1, %eax
jne 0x2df8
movl $0x1a5, %esi            # imm = 0x1A5
jmp 0x2e5c
movl $0x2, %esi
callq 0x2440
cmpl $-0x1, %eax
jne 0x2e25
movq 0x5cfa(%rip), %rax      # 0x8b08
movq 0x10(%rax), %rsi
leaq 0x2279(%rip), %rdi      # 0x5092
xorl %eax, %eax
callq 0x2180
jmp 0x2f19
movq 0x5cdc(%rip), %rax      # 0x8b08
movq 0x10(%rax), %rdi
leaq 0x28(%rsp), %r14
movq %r14, %rsi
callq 0x22c0
movq 0x5cc4(%rip), %rax      # 0x8b08
movq 0x10(%rax), %rdi
callq 0x2090
movq 0x5cb4(%rip), %rax      # 0x8b08
movq 0x10(%rax), %rdi
movl 0x18(%r14), %esi
callq 0x24a0
movq 0x5ca0(%rip), %rax      # 0x8b08
movq 0x10(%rax), %rdi
movl $0x1, %esi
xorl %eax, %eax
callq 0x2430
movl %eax, %r13d
movl %eax, %edi
movl $0x1, %esi
callq 0x2170
movq 0x5c7a(%rip), %rbp      # 0x8b08
cmpl $0x1, 0x20(%rbp)
jne 0x2eb0
callq 0x2120
leaq 0x220b(%rip), %rdi      # 0x50ab
movl %eax, %esi
xorl %eax, %eax
callq 0x2180
movq 0x5c58(%rip), %rbp      # 0x8b08
movq 0x18(%rbp), %rax
movq $-0x1, %rcx
cmpq $0x0, 0x8(%rax,%rcx,8)
leaq 0x1(%rcx), %rcx
jne 0x2ebb
cmpq $0x2, %rcx
jb 0x2eee
movq 0x20(%rbp), %rax
movq %rax, 0x20(%rsp)
movups (%rbp), %xmm0
movups 0x10(%rbp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x2931
jmp 0x2f11
movq (%rbx,%r12,8), %rdi
movq %rbx, %rsi
callq 0x2490
cmpl $-0x1, %eax
jne 0x2f11
movq (%rbx,%r12,8), %rsi
leaq 0x20fe(%rip), %rdi      # 0x5008
xorl %eax, %eax
callq 0x2180
movl %r13d, %edi
callq 0x21d0
movl $0x1, %edi
callq 0x24e0
nop