Browse Source

:construction: Base execution commande

Loquicom 6 years ago
parent
commit
49bd0110cb
1 changed files with 63 additions and 3 deletions
  1. 63 3
      mysh.c

+ 63 - 3
mysh.c

@@ -4,6 +4,8 @@
  *
  * Created on 31 octobre 2018, 12:43
  */
+#define _POSIX_C_SOURCE 2
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -31,6 +33,21 @@ void test_write() {
     printf("%d\n", tmp);
 }
 
+void test_tmp_file(){
+    int a;
+    FILE* f = tmpfile();
+    printf("F : %d\n", f == NULL);
+    int fd = fileno(f);
+    printf("%d : %ld\n", fd, write(fd, "bonjour", 8));
+    a = lseek(fd, 0L, SEEK_SET);
+    sleep(2);
+    char buf[10];
+    memset(buf, 0 , 10);
+    a = read(fd, buf, 10);
+    printf("%d : %s\n", a, buf);
+    close(fd);
+}
+
 /* --- Fonctions utilitaires --- */
 void show_current_dir(const char* before, const char* after) {
     char buffer[BUFFER_SIZE];
@@ -49,6 +66,15 @@ void show_current_dir(const char* before, const char* after) {
     }
 }
 
+int get_tmp_file(){
+    FILE* f = tmpfile();
+    if(f == NULL){
+        adderror("Impossible de créer un fichier temporaire");
+        return SHELL_ERR;
+    }
+    return fileno(f);
+}
+
 /* --- Main --- */
 int main(int argc, char* argv[]) { 
     CommandTab ct;
@@ -56,7 +82,11 @@ int main(int argc, char* argv[]) {
     int a;
     //Initialisation structures
     error_finit("mysh.log");
-    ini_pid_list(&pidlist);   
+    ini_pid_list(&pidlist);
+    
+    
+    
+    error.exit();
     //Recup ligne
     //printf("%s\n", fgets(str, 500, stdin));&
     memset(str, 0, 500);
@@ -106,7 +136,7 @@ int run(CommandTab ct){
         error.print("Erreur systeme, impossible de continuer\n");
         return SHELL_ERR;
     } 
-    //Pere
+    // --- Pere ---
     else if(pid != 0){
         //Ajoute du pid dans la liste des pid actifs
         pnode = add_pid(&pidlist, pid);
@@ -127,6 +157,36 @@ int run(CommandTab ct){
         //Sinon ok
         return SHELL_OK;
     }
-    //Fils
+    // --- Fils ---
+    //Reset sortie erreur
+    error.end();
+    //Execute chaque commande
+    Command* c;
+    int tmpfd;
+    for(int i = 0; i < ct.length; i++){
+        c = ct.cmd[i];
+        //Si il y a un pipe creation d'un fichier temporaire à la place de la sortie standard
+        if(c->next == SHELL_PIPE){
+            tmpfd = get_tmp_file();
+            if(tmpfd == SHELL_ERR){
+                exit(EXIT_FAILURE);
+            }
+            tmpfd = redirect_fd(STDOUT, tmpfd);
+        }
+        //Effectue les redirections IO
+        /*ToDo*/
+        //Execute la commande
+        if(is_internal_cmd(c->name)){
+            result = launch_internal_command(c);
+        } else if(is_executable_file(c->name)){
+            
+        } else {
+            
+        }
+        //Reset IO
+        /*ToDo*/
+        //Agit en fonction de la jointure avec la prochaine commande
+        /*ToDo*/
+    }
     return SHELL_OK;
 }