2021-04-20 16:12:44 +00:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include "readcmd.h"
|
|
|
|
#include "jobs.h"
|
|
|
|
|
2021-04-21 14:18:53 +00:00
|
|
|
extern int errno;
|
|
|
|
|
2021-04-21 12:15:14 +00:00
|
|
|
struct cmdline *cmd;
|
2021-04-20 16:12:44 +00:00
|
|
|
|
2021-04-21 14:18:53 +00:00
|
|
|
int pid_fils;
|
|
|
|
|
|
|
|
int job_id = 0;
|
|
|
|
list jobs;
|
|
|
|
|
|
|
|
int prompting = 0;
|
|
|
|
jmp_buf goto_prompt;
|
|
|
|
|
|
|
|
char initcd[256], currentcd[256];
|
|
|
|
|
2021-04-20 20:19:58 +00:00
|
|
|
|
2021-04-21 14:18:53 +00:00
|
|
|
|
|
|
|
void handler_sigchld(int sig_num, siginfo_t *info)
|
2021-04-20 16:50:27 +00:00
|
|
|
{
|
2021-04-21 14:18:53 +00:00
|
|
|
cell *job = trouver(&jobs, info->si_pid);
|
2021-04-21 13:12:19 +00:00
|
|
|
if (job)
|
2021-04-21 14:18:53 +00:00
|
|
|
{
|
2021-04-20 16:50:27 +00:00
|
|
|
if (prompting)
|
2021-04-21 14:18:53 +00:00
|
|
|
{
|
2021-04-20 16:50:27 +00:00
|
|
|
printf("\n");
|
|
|
|
}
|
2021-04-21 11:50:58 +00:00
|
|
|
|
2021-04-21 14:18:53 +00:00
|
|
|
printf("[%d] %d done: %s\n", job->id, job->pid, job->cmd);
|
2021-04-21 13:12:19 +00:00
|
|
|
supprimer(&jobs, job->pid);
|
2021-04-20 16:50:27 +00:00
|
|
|
job_id--;
|
2021-04-21 11:50:58 +00:00
|
|
|
|
|
|
|
if (prompting)
|
2021-04-21 14:18:53 +00:00
|
|
|
{
|
|
|
|
siglongjmp(goto_prompt, sig_num);
|
2021-04-21 11:50:58 +00:00
|
|
|
}
|
2021-04-20 16:50:27 +00:00
|
|
|
}
|
2021-04-20 16:12:44 +00:00
|
|
|
}
|
|
|
|
|
2021-04-21 14:18:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
void handler_sigint(int sig_num)
|
2021-04-20 19:42:13 +00:00
|
|
|
{
|
2021-04-20 20:19:58 +00:00
|
|
|
printf("\n");
|
2021-04-21 14:18:53 +00:00
|
|
|
siglongjmp(goto_prompt, sig_num);
|
2021-04-20 19:42:13 +00:00
|
|
|
}
|
|
|
|
|
2021-04-21 14:18:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
void handler_sigtstp(int sig_num)
|
2021-04-21 11:50:58 +00:00
|
|
|
{
|
|
|
|
if (!prompting)
|
|
|
|
{
|
2021-04-21 14:18:53 +00:00
|
|
|
kill(pid_fils, SIGTSTP);
|
|
|
|
ajouter(&jobs, pid_fils, ++job_id, *(cmd->seq), 1);
|
|
|
|
printf("[%d] %d suspended\n", job_id, pid_fils);
|
|
|
|
siglongjmp(goto_prompt, sig_num);
|
2021-04-21 11:50:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-04-21 14:18:53 +00:00
|
|
|
handler_sigint(sig_num);
|
2021-04-21 11:50:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-21 14:18:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
void handler_sigcont(int sig_num)
|
2021-04-20 16:50:27 +00:00
|
|
|
{
|
2021-04-20 16:12:44 +00:00
|
|
|
|
2021-04-21 14:18:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2021-04-20 16:50:27 +00:00
|
|
|
initialiser(&jobs);
|
2021-04-21 14:18:53 +00:00
|
|
|
getcwd(initcd, sizeof(initcd));
|
2021-04-20 16:12:44 +00:00
|
|
|
|
2021-04-21 13:12:19 +00:00
|
|
|
// gestion des signaux
|
2021-04-20 16:50:27 +00:00
|
|
|
struct sigaction action;
|
2021-04-20 20:19:58 +00:00
|
|
|
sigemptyset(&action.sa_mask);
|
2021-04-21 11:50:58 +00:00
|
|
|
action.sa_flags = SA_SIGINFO | SA_RESTART;
|
2021-04-21 14:18:53 +00:00
|
|
|
action.sa_handler = handler_sigchld;
|
2021-04-20 16:50:27 +00:00
|
|
|
sigaction(SIGCHLD, &action, NULL);
|
2021-04-21 14:18:53 +00:00
|
|
|
action.sa_handler = handler_sigint;
|
2021-04-20 20:19:58 +00:00
|
|
|
sigaction(SIGINT, &action, NULL);
|
2021-04-21 14:18:53 +00:00
|
|
|
action.sa_handler = handler_sigtstp;
|
2021-04-21 11:50:58 +00:00
|
|
|
sigaction(SIGTSTP, &action, NULL);
|
2021-04-20 16:12:44 +00:00
|
|
|
|
2021-04-20 16:54:36 +00:00
|
|
|
// loop principal
|
2021-04-20 16:50:27 +00:00
|
|
|
while (1)
|
|
|
|
{
|
2021-04-21 14:18:53 +00:00
|
|
|
sigsetjmp(goto_prompt, 1);
|
2021-04-20 16:54:36 +00:00
|
|
|
|
2021-04-20 16:50:27 +00:00
|
|
|
prompting = 1;
|
2021-04-21 14:18:53 +00:00
|
|
|
getcwd(currentcd, sizeof(currentcd));
|
|
|
|
printf("%s >>> ", currentcd);
|
2021-04-21 12:15:14 +00:00
|
|
|
cmd = readcmd();
|
2021-04-20 16:50:27 +00:00
|
|
|
prompting = 0;
|
2021-04-20 16:12:44 +00:00
|
|
|
|
2021-04-20 20:19:58 +00:00
|
|
|
if (cmd == NULL)
|
2021-04-21 14:18:53 +00:00
|
|
|
{ // EOF
|
2021-04-20 20:19:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (cmd->seq[0] == NULL)
|
2021-04-21 14:18:53 +00:00
|
|
|
{ // empty
|
2021-04-21 11:50:58 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-04-20 16:50:27 +00:00
|
|
|
else if (!strcmp(cmd->seq[0][0], "exit"))
|
2021-04-21 14:18:53 +00:00
|
|
|
{ // "exit"
|
2021-04-20 16:50:27 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-04-21 14:18:53 +00:00
|
|
|
else if (!strcmp(cmd->seq[0][0], "jobs"))
|
|
|
|
{ // "jobs"
|
|
|
|
afficher(&jobs);
|
|
|
|
continue;
|
|
|
|
}
|
2021-04-20 16:50:27 +00:00
|
|
|
else if (!strcmp(cmd->seq[0][0], "cd"))
|
2021-04-21 14:18:53 +00:00
|
|
|
{ // "cd"
|
2021-04-20 17:03:25 +00:00
|
|
|
int ret = 0;
|
2021-04-20 16:50:27 +00:00
|
|
|
if (cmd->seq[0][1] == NULL)
|
2021-04-21 14:18:53 +00:00
|
|
|
{ // no path
|
2021-04-20 17:03:25 +00:00
|
|
|
ret = chdir(initcd);
|
2021-04-20 16:50:27 +00:00
|
|
|
}
|
|
|
|
else
|
2021-04-21 14:18:53 +00:00
|
|
|
{ // with path
|
2021-04-20 17:03:25 +00:00
|
|
|
ret = chdir(cmd->seq[0][1]);
|
2021-04-20 16:50:27 +00:00
|
|
|
}
|
2021-04-20 17:03:25 +00:00
|
|
|
if (ret)
|
2021-04-21 14:18:53 +00:00
|
|
|
{ // wrong path
|
2021-04-20 17:00:16 +00:00
|
|
|
fprintf(stderr, "ERROR: cd failed, (%d) %s\n", errno, strerror(errno));
|
|
|
|
}
|
2021-04-20 16:50:27 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-04-21 14:18:53 +00:00
|
|
|
pid_fils = fork();
|
|
|
|
if (pid_fils == -1)
|
|
|
|
{ // fork fail ?
|
2021-04-20 17:00:16 +00:00
|
|
|
fprintf(stderr, "ERROR: forking failed, (%d) %s\n", errno, strerror(errno));
|
2021-04-20 16:50:27 +00:00
|
|
|
exit(errno);
|
2021-04-20 16:12:44 +00:00
|
|
|
}
|
|
|
|
|
2021-04-21 14:18:53 +00:00
|
|
|
if (pid_fils == 0)
|
2021-04-20 16:54:36 +00:00
|
|
|
{ // instructions du fils
|
2021-04-21 14:18:53 +00:00
|
|
|
action.sa_handler = SIG_DFL;
|
|
|
|
sigaction(SIGTSTP, &action, NULL); // on default SIGTSTP
|
|
|
|
sigaction(SIGCONT, &action, NULL); // on default SIGCONT
|
|
|
|
|
2021-04-21 11:50:58 +00:00
|
|
|
if (cmd->backgrounded)
|
2021-04-21 14:18:53 +00:00
|
|
|
{ // background
|
2021-04-21 11:50:58 +00:00
|
|
|
action.sa_handler = SIG_IGN;
|
|
|
|
sigaction(SIGINT, &action, NULL); // on ignore SIGINT
|
2021-04-21 14:18:53 +00:00
|
|
|
sigaction(SIGTSTP, &action, NULL); // on ignore SIGTSTP
|
|
|
|
|
2021-04-21 11:50:58 +00:00
|
|
|
}
|
2021-04-21 14:18:53 +00:00
|
|
|
|
2021-04-20 17:03:49 +00:00
|
|
|
execvp(cmd->seq[0][0], cmd->seq[0]);
|
2021-04-20 16:50:27 +00:00
|
|
|
exit(errno); // si execlp échoue on exit avec une erreur
|
|
|
|
}
|
2021-04-20 16:54:36 +00:00
|
|
|
else
|
|
|
|
{ // instructions du père
|
2021-04-20 16:50:27 +00:00
|
|
|
if (cmd->backgrounded)
|
2021-04-21 14:18:53 +00:00
|
|
|
{ // background
|
|
|
|
ajouter(&jobs, pid_fils, ++job_id, *(cmd->seq), 0);
|
|
|
|
printf("[%d] %d\n", job_id, pid_fils);
|
2021-04-20 16:50:27 +00:00
|
|
|
}
|
|
|
|
else
|
2021-04-21 14:18:53 +00:00
|
|
|
{ // foreground
|
|
|
|
int wait_code;
|
|
|
|
pid_t id_fils = waitpid(pid_fils, &wait_code, 0); // on attend la fin de l'exec du fils
|
2021-04-20 16:50:27 +00:00
|
|
|
|
2021-04-21 14:18:53 +00:00
|
|
|
if (id_fils == -1)
|
|
|
|
{ // wait fail ?
|
|
|
|
fprintf(stderr, "ERROR: waiting for %d failed, (%d) %s\n", wait_code, errno, strerror(errno));
|
2021-04-21 11:50:58 +00:00
|
|
|
exit(errno);
|
2021-04-20 16:50:27 +00:00
|
|
|
}
|
|
|
|
|
2021-04-21 14:18:53 +00:00
|
|
|
if (wait_code)
|
|
|
|
{ // execvp fail ?
|
|
|
|
fprintf(stderr, "ERROR: %d's execution failed, (%d) %s\n", pid_fils, wait_code, strerror(wait_code));
|
2021-04-20 16:50:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-20 16:12:44 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 16:50:27 +00:00
|
|
|
return EXIT_SUCCESS;
|
2021-04-20 16:12:44 +00:00
|
|
|
}
|