Selaa lähdekoodia

:sparkles: Gestion Ctrl+C

Loquicom 6 vuotta sitten
vanhempi
sitoutus
b3b57a6409
4 muutettua tiedostoa jossa 61 lisäystä ja 4 poistoa
  1. 5 0
      execute.c
  2. 4 0
      execute.h
  3. 50 4
      mysh.c
  4. 2 0
      mysh.h

+ 5 - 0
execute.c

@@ -15,6 +15,7 @@
 
 /* --- Extern --- */
 extern Error error;
+pid_t active = -1;
 
 /* --- Fonctions publiques --- */
 boolean is_executable_file(const char * cmd) {
@@ -50,7 +51,9 @@ int exec_shell(char* name, char** argv){
         exit(EXIT_FAILURE);
     }
     //Pere
+    active = pid;
     wait(&result);
+    active = -1;
     //Retourne retour fils
     if(WIFEXITED(result)){
         return WEXITSTATUS(result);
@@ -82,7 +85,9 @@ int exec_file(char* name, char** argv){
         exit(EXIT_FAILURE);
     }
     //Pere
+    active = pid;
     wait(&result);
+    active = -1;
     //Retourne retour fils
     if(WIFEXITED(result)){
         return WEXITSTATUS(result);

+ 4 - 0
execute.h

@@ -9,8 +9,12 @@
 #define EXECUTE_H
 
 /* --- Include --- */
+#include <sys/types.h>
 #include "constante.h"
 
+/* --- Extern --- */
+extern pid_t active;
+
 /* --- Fonctions publiques --- */
 boolean is_executable_file(const char *);
 int exec_shell(char*, char**);

+ 50 - 4
mysh.c

@@ -24,10 +24,12 @@
 /* --- Extern --- */
 extern Error error;
 extern boolean exitsh;
+extern pid_t active;
 
 /* --- Global --- */
 pid_list pidlist;
-pid_node* active = NULL;
+boolean fond = false;
+
 int job = 1;
 
 /* --- Fonctions privées --- */
@@ -170,6 +172,8 @@ int main(int argc, char* argv[]) {
         clean_pid(&pidlist);
         error.exit_err();
     }
+    //Gestion interuption
+    signal(SIGINT, handler);
     //Boucle infini de lecture
     while(!exitsh){
         //On regarde si un fils en fond est mort
@@ -182,9 +186,8 @@ int main(int argc, char* argv[]) {
         show_current_dir(before, ">\x1b[0m ");      
         //Lecture ligne
         if(get_line(line) == SHELL_ERR){
-            error.print("Impossible de lire les commandes\n");
-            clean_pid(&pidlist);
-            error.exit_err();
+            //error.print("Impossible de lire les commandes\n");
+            continue;
         }
         //Parse la ligne et commandes
         result = parse_line(&ct, line);
@@ -234,6 +237,7 @@ int run(CommandTab ct, int* status){
         if(pid == 0){
             int stat = 0;
             ct.bck = 0;
+            fond = true;
             result = run(ct, &stat);
             //Message de fin + retour
             if(result == SHELL_FAIL){
@@ -366,4 +370,46 @@ int run(CommandTab ct, int* status){
         return SHELL_FAIL;
     }
     return SHELL_OK;
+}
+
+void handler(int sig){
+    char reponse = ' ';
+    pid_node* pn;
+    //Repositionne le gestionnaire (Ne marche plus apres 1 utilisation)
+    signal(SIGINT, handler);
+    //Si on est en fond on est non concerné
+    if(fond){
+        return;
+    }
+    //Si il y a un process actif on le coupe
+    printf("Active : %d\n", active);
+    if(active != -1){
+        if(kill(active, SIGINT) == ERR){
+            addperror("Impossible de tuer le processus en cours");
+        }
+        active = -1;
+        return;
+    }
+    //Sinon demande comfirmation pour finir le programme
+    while(reponse != 'o' && reponse != 'O' && reponse != 'n' && reponse != 'N'){
+        printf("Voulez vous vraiment quitter ? [O/N] ");
+        if(scanf("%c", &reponse) == 0){
+            reponse = ' ';
+        }
+    }
+    //Si oui
+    if(reponse == 'n' || reponse == 'N'){
+        return;
+    }
+    //Coupe tous les processus en fond
+    pn = pidlist.first;
+    while(pn != NULL){
+        if(kill(pn->pid, SIGINT) == ERR){
+            addperror("Impossible de tuer le processus en fond");
+        }
+        pn = pn->next;
+    }
+    //Termine l'execution
+    clean_pid(&pidlist);
+    error.exit();
 }

+ 2 - 0
mysh.h

@@ -23,5 +23,7 @@ void show_current_dir(const char*, const char*);
  */
 int run(CommandTab, int*);
 
+void handler(int);
+
 #endif /* MYSH_H */