Browse Source

:tada: Creation fichier pour les commandes internes

Loquicom 6 years ago
parent
commit
6eacf19728
5 changed files with 115 additions and 34 deletions
  1. 72 0
      command.c
  2. 30 0
      command.h
  3. 5 4
      makefile
  4. 8 22
      mysh.c
  5. 0 8
      mysh.h

+ 72 - 0
command.c

@@ -0,0 +1,72 @@
+/* 
+ * File:   command.c
+ * Author: Arthur Brandao
+ *
+ * Created on 9 novembre 2018
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include "error.h"
+#include "str.h"
+#include "parser.h"
+#include "command.h"
+
+/* --- Extern --- */
+extern Error error;
+char* cmdlist[2]  = {
+    "cd",
+    NULL
+};
+
+/* --- Fonctions publiques --- */
+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;
+        }
+    }
+    return false;
+}
+
+int launch_internal_command(Command* cmd){
+    int result, length = strlen(cmd->name) + 1;
+    //cd
+    if(strncmp(cmd->name, cmdlist[0], length) == 0){
+        result = cd(cmd->argc, cmd->argv);
+        return result;
+    }
+    //Aucune commande
+    else {
+        return SHELL_FAIL;
+    }
+}
+
+/* --- Commandes --- */
+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;
+    } else {
+        //Si aucun argument on va à la racine
+        if (argc == 1) {
+            if (chdir("/") == ERR) {
+                addperror("Erreur chdir()");
+                return SHELL_ERR;
+            }
+        }            
+        //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 SHELL_OK;
+    //show_current_dir("current working directory is: ", "\n");
+}

+ 30 - 0
command.h

@@ -0,0 +1,30 @@
+/* 
+ * File:   command.h
+ * Author: Arthur Brandao
+ *
+ * Created on 9 novembre 2018
+ */
+
+#ifndef COMMAND_H
+#define COMMAND_H
+
+/* --- Include --- */
+#include "constante.h"
+
+/* --- Extern --- */
+extern char* cmdlist[2];
+
+/* --- Fonctions --- */
+boolean is_internal_cmd(const char*);
+int launch_internal_command(Command*);
+
+/* --- Commandes --- */
+/**
+ * Change le dossier de travail actuel
+ * @param int argc
+ * @param char** argv
+ */
+int cd(int, char**);
+
+#endif /* COMMAND_H */
+

+ 5 - 4
makefile

@@ -3,7 +3,7 @@
 #
 
 EXEC = mysh
-OBJETS = error.o str.o parser.o wildcard.o
+OBJETS = error.o str.o parser.o wildcard.o command.o
 NOM_PROJET = mini-shell
 
 #
@@ -88,8 +88,9 @@ archive: clean
 	@echo "Termine."
 
 # DEPENDANCES
-error.o: error.c error.h
+error.o: error.c str.h error.h
 str.o: str.c str.h
-parser.o: parser.c error.h wildcard.h constante.h parser.h str.h
+parser.o: parser.c error.h str.h wildcard.h constante.h parser.h
 wildcard.o: wildcard.c error.h wildcard.h constante.h
-mysh.o: mysh.c error.h parser.h constante.h str.h mysh.h
+command.o: command.c str.h command.h constante.h
+mysh.o: mysh.c error.h str.h parser.h constante.h command.h mysh.h

+ 8 - 22
mysh.c

@@ -11,6 +11,7 @@
 #include "error.h"
 #include "str.h"
 #include "parser.h"
+#include "command.h"
 #include "mysh.h"
 
 #include <sys/types.h>
@@ -50,7 +51,7 @@ int main(int argc, char* argv[]) {
     char str[BUFFER_SIZE];
     int a;
     //Initialisation erreur
-    error_finit("mysh.log");
+    error_finit("mysh.log");   
     //Recup ligne
     //printf("%s\n", fgets(str, 500, stdin));&
     memset(str, 0, 500);
@@ -76,29 +77,14 @@ int main(int argc, char* argv[]) {
             printf("    Argument %d : %s\n", j, c->argv[j]);
         }
         printf("Commande en fond : %d\n\n", ct.bck);
+        //Si c'est une commande interne on l'execute
+        if(is_internal_cmd(ct.cmd[i]->name)){
+            show_current_dir(NULL, "\n");
+            printf("Result : %d\n", launch_internal_command(ct.cmd[i]));
+            show_current_dir(NULL, "\n");
+        }
     }
     //Supprime
     clean_command(&ct);
     error.exit();
-}
-
-/* --- Commandes internes --- */
-void cd(int argc, char** argv) {
-    //Si trop d'arguments
-    if (argc > 2) {
-        error.print("too many arguments : 1 required, %d given\n", argc - 1);
-    } else {
-        //Si aucun argument on va à la racine
-        if (argc == 1) {
-            if (chdir("/") == ERR) {
-                addperror("Erreur chdir()");
-            }
-        }            //Sinon on va dans le dossier indiqué par l'utilisateur
-        else {
-            if (chdir(argv[1]) == ERR) {
-                error.print("path does not exist\n");
-            }
-        }
-    }
-    //show_current_dir("current working directory is: ", "\n");
 }

+ 0 - 8
mysh.h

@@ -16,13 +16,5 @@
  */
 void show_current_dir(const char*, const char*);
 
-/* --- Commandes internes --- */
-/**
- * Change le dossier de travail actuel
- * @param int argc
- * @param char** argv
- */
-void cd(int, char**);
-
 #endif /* MYSH_H */