Эх сурвалжийг харах

:construction: Base de run

Loquicom 6 жил өмнө
parent
commit
d77542e9c5
2 өөрчлөгдсөн 111 нэмэгдсэн , 70 устгасан
  1. 5 6
      command.c
  2. 106 64
      mysh.c

+ 5 - 6
command.c

@@ -92,7 +92,6 @@ void clean_pid(pid_list* pl) {
 boolean is_internal_cmd(const char* cmd) {
     //Parcours tableau pour trouver correspondance
     for (int i = 0; cmdlist[i] != NULL; i++) {
-        printf("%s - %s\n", cmd, cmdlist[i]);
         if (strncmp(cmd, cmdlist[i], strlen(cmdlist[i]) + 1) == 0) {
             return true;
         }
@@ -117,23 +116,23 @@ int cd(int argc, char** argv) {
     //Si trop d'arguments
     if (argc > 2) {
         error.print("too many arguments : 1 required, %d given\n", argc - 1);
-        return SHELL_FAIL;
+        return EXIT_FAILURE;
     } else {
         //Si aucun argument on va à la racine
         if (argc == 1) {
             if (chdir("/") == ERR) {
                 addperror("Erreur chdir()");
-                return SHELL_ERR;
+                return EXIT_FAILURE;
             }
         }
-            //Sinon on va dans le dossier indiqué par l'utilisateur
+        //Sinon on va dans le dossier indiqué par l'utilisateur
         else {
             if (chdir(argv[1]) == ERR) {
                 error.print("path does not exist\n");
-                return SHELL_ERR;
+                return EXIT_FAILURE;
             }
         }
     }
-    return SHELL_OK;
+    return EXIT_SUCCESS;
     //show_current_dir("current working directory is: ", "\n");
 }

+ 106 - 64
mysh.c

@@ -48,35 +48,7 @@ void test_tmp_file(){
     close(fd);
 }
 
-/* --- Fonctions utilitaires --- */
-void show_current_dir(const char* before, const char* after) {
-    char buffer[BUFFER_SIZE];
-    if (getcwd(buffer, sizeof (buffer)) == NULL) {
-        addperror("Erreur getcwd()");
-    } else {
-        if(before == NULL && after == NULL){
-            printf("%s", buffer);
-        } else if(before == NULL){
-            printf("%s%s", buffer, after);
-        } else if(after == NULL){
-            printf("%s%s", before, buffer);
-        } else {
-            printf("%s%s%s", before, buffer, 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[]) { 
+void test(){
     CommandTab ct;
     char str[BUFFER_SIZE];
     int a;
@@ -84,9 +56,6 @@ int main(int argc, char* argv[]) {
     error_finit("mysh.log");
     ini_pid_list(&pidlist);
     
-    
-    
-    error.exit();
     //Recup ligne
     //printf("%s\n", fgets(str, 500, stdin));&
     memset(str, 0, 500);
@@ -125,59 +94,132 @@ int main(int argc, char* argv[]) {
     error.exit();
 }
 
+/* --- Fonctions utilitaires --- */
+void show_current_dir(const char* before, const char* after) {
+    char buffer[BUFFER_SIZE];
+    if (getcwd(buffer, sizeof (buffer)) == NULL) {
+        addperror("Erreur getcwd()");
+    } else {
+        if(before == NULL && after == NULL){
+            printf(":%s", buffer);
+        } else if(before == NULL){
+            printf(":%s%s", buffer, after);
+        } else if(after == NULL){
+            printf("%s:%s", before, buffer);
+        } else {
+            printf("%s:%s%s", before, buffer, after);
+        }
+    }
+    fflush(stdout);
+}
+
+int get_line(char* buffer){
+    memset(buffer, 0, BUFFER_SIZE);
+    if(read(STDIN, buffer, BUFFER_SIZE) == ERR){
+        addperror("Impossible de lire dans STDIN");
+        return SHELL_ERR;
+    }
+    return SHELL_OK;
+}
+
+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[]) { 
+    //Declaration variables
+    CommandTab ct;
+    int result;
+    char line[BUFFER_SIZE];
+    //Initialisation structures
+    error_finit("mysh.log");
+    ini_pid_list(&pidlist);
+    //BOucle infini de lecture
+    while(1){
+        //Affichage repertoire
+        show_current_dir(getlogin(), "> ");      
+        //Lecture ligne
+        if(get_line(line) == SHELL_ERR){
+            error.print("Impossible de lire les commandes");
+            clean_pid(&pidlist);
+            error.exit_err();
+        }
+        //Parse la ligne et commandes
+        result = parse_line(&ct, line);
+        if(result == SHELL_ERR){
+            error.print("Impossible d'analyser la ligne");
+            addserror("Erreur lors du parse de la ligne");
+            continue;
+        }
+        //Parse les commandes
+        result = parse_all_command(&ct);
+        if(result == SHELL_FAIL){
+            error.print("Impossible d'analyser la commande");
+            addserror("Erreur lors du parse des commandes");
+            continue;
+        }
+        //Execute
+        result = run(ct);
+        printf("Result : %d\n", result);
+    }
+    //Nettoyage
+    clean_pid(&pidlist);
+    error.end();
+    return EXIT_SUCCESS;
+}
+
 int run(CommandTab ct){
     pid_t pid;
     int result;
-    pid_node* pnode;
-    //Fork pour executer la commande
-    pid = fork();
-    if(pid == ERR){
-        addperror("Erreur lors du fork pour l'execution des commandes");
-        error.print("Erreur systeme, impossible de continuer\n");
-        return SHELL_ERR;
-    } 
-    // --- Pere ---
-    else if(pid != 0){
-        //Ajoute du pid dans la liste des pid actifs
-        pnode = add_pid(&pidlist, pid);
-        //Si on attend le retour de la commande
-        if(!ct.bck){
-            //Ajout du pid comme étant le pid actif et attente
-            active = pnode;
-            wait(&result);
-            //Le processus n'est plus actif et analyse retour
-            active = NULL;
-            remove_pid(&pidlist, pnode);
-            if(WIFEXITED(result)){
-                return WEXITSTATUS(result);
-            } else {
-                return SHELL_FAIL;
+    //Si en fond creation d'un fork pour executer les commandes
+    printf("bck : %d\n", ct.bck);
+    if(ct.bck && false/*ToDo - Test plus tard*/){
+        pid = fork();
+        if(pid == ERR){
+            addperror("Erreur lors du fork pour l'execution des commandes");
+            error.print("Erreur systeme, impossible de continuer\n");
+            return SHELL_ERR;
+        }
+        //Fils
+        if(pid == 0){
+            ct.bck = 0;
+            result = run(ct);
+            if(result == SHELL_FAIL){
+                exit(EXIT_FAILURE);
             }
+            exit(EXIT_SUCCESS);
         }
-        //Sinon ok
+        //Ajout du fils dans la liste des pid
+        add_pid(&pidlist, pid);
+        //Pour le pere c'est fini
         return SHELL_OK;
     }
-    // --- Fils ---
-    //Reset sortie erreur
-    error.end();
-    //Execute chaque commande
+    //Sinon execution de chaque commande
     Command* c;
-    int tmpfd;
+    //int tmpfd;
+    //Parcours les commandes
     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){
+        /*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);
+            printf("%d\n", result);
         } else if(is_executable_file(c->name)){
             
         } else {