浏览代码

:sparkles: Creation macrofonction ajout erreur

Loquicom 6 年之前
父节点
当前提交
4a932b771e
共有 4 个文件被更改,包括 36 次插入25 次删除
  1. 2 2
      error.c
  2. 12 3
      error.h
  3. 4 2
      mysh.c
  4. 18 18
      parser.c

+ 2 - 2
error.c

@@ -10,8 +10,8 @@
 #include "error.h"
 
 /* --- Extern --- */
-int serrorno = 0;
-char* serrorlib[] = {
+int serrno = 0;
+char* serrlib[] = {
     "Reussite",
     "Ligne de commande mal terminée",
     "& inattendue",

+ 12 - 3
error.h

@@ -8,8 +8,16 @@
 #ifndef ERROR_H
 #define ERROR_H
 
+#include <stdio.h>
+#include <errno.h>
+
+
 /* --- Macro --- */
-#define serror(str) fprintf(stderr, str" : %s\n", serrorlib[serrorno])
+#define serror(str) fprintf(stderr, str" : %s\n", serrlib[serrno])
+#define strserror(index) serrlib[index]
+#define adderror(str) fprintf(stderr, "Erreur dans le fichier "__FILE__" ligne %d, "str"\n", __LINE__)
+#define addperror(str) fprintf(stderr, "Erreur dans le fichier "__FILE__" ligne %d, "str" : %s\n", __LINE__, strerror(errno))
+#define addserror(str) fprintf(stderr, "Erreur dans le fichier "__FILE__" ligne %d, "str" : %s\n", __LINE__, strserror(serrno))
 
 /* --- Constantes Generales --- */
 #define ERR -1
@@ -25,8 +33,9 @@
 #define SEADDWC 8
 
 /* --- Extern --- */
-extern int serrorno;
-extern char* serrorlib[];
+extern int errno;
+extern int serrno; //Shell Errno
+extern char* serrlib[];
 
 #endif /* ERROR_H */
 

+ 4 - 2
mysh.c

@@ -7,7 +7,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <fcntl.h>
 #include "error.h"
+#include "str.h"
 #include "parser.h"
 #include "mysh.h"
 
@@ -50,13 +52,13 @@ int main(int argc, char* argv[]) {
     //Separe les commandes
     a = parse_line(&ct, str);
     if(a == SHELL_ERR){
-        serror("Erreur lors du parse de la ligne");
+        addserror("Erreur lors du parse de la ligne");
         exit(EXIT_FAILURE);
     }
     //Parse les commandes
     a = parse_all_command(&ct);
     if(a == SHELL_FAIL){
-        serror("Erreur lors du parse des commandes");
+        addserror("Erreur lors du parse des commandes");
         exit(EXIT_FAILURE);
     }
     //Affiche resultat

+ 18 - 18
parser.c

@@ -18,7 +18,7 @@
 #include "parser.h"
 
 /* --- Extern --- */
-extern int serrorno;
+extern int serrno;
 
 /* --- Fonctions privées --- */
 /**
@@ -39,14 +39,14 @@ int nb_commands(char* line){
         else if(*line == '|'){
             //Verif que ce n'est pas le dernier carac
             if(*(line + 1) == '\0'){
-                serrorno = SEBADEND;
+                serrno = SEBADEND;
                 return SHELL_ERR;
             }
             //Si un || on avance de 1 en plus
             else if(*(line + 1) == '|'){
                 //Si à la fin de la chaine
                 if(*(line + 2) == '\0'){
-                    serrorno = SEBADEND;
+                    serrno = SEBADEND;
                     return SHELL_ERR;
                 }
                 line++;
@@ -62,7 +62,7 @@ int nb_commands(char* line){
             }
             //Sinon il doit y avoir un vide pour etre le & du bck ou un > avant
             else if(*(line + 1) != '\0' && *(line - 1) != '>'){
-                serrorno = SEBADET;
+                serrno = SEBADET;
                 return SHELL_ERR;
             }
         }
@@ -148,7 +148,7 @@ int set_io(Command* c, char* filename, int redir){
     file = open(filename, O_CREAT | O_RDWR, S_IRWXU);
     if(file == ERR){
         perror("Erreur lors de l'ouverture du fichier pour la redirection : ");
-        serrorno = SEOPENF;
+        serrno = SEOPENF;
         return SHELL_ERR;
     }
     //Analyse dans quel descripteur il doit etre mis
@@ -215,7 +215,7 @@ int set_io(Command* c, char* filename, int redir){
             }
             break;
         default :
-            serrorno = SEREDIRTYPE;
+            serrno = SEREDIRTYPE;
             return SHELL_ERR;
     }
     //Si on arrive ici tous est ok
@@ -239,7 +239,7 @@ int set_redirection(Command* c){
             if(*buffer == '<'){
                 //Si il n'y a rien apres
                 if(!(*(buffer + 1))){
-                    serrorno = SEBADREDIR;
+                    serrno = SEBADREDIR;
                     return SHELL_ERR;
                 }
                 buffer++;
@@ -250,7 +250,7 @@ int set_redirection(Command* c){
             else if (*buffer == '>'){
                 //Si il n'y a rien apres
                 if(!(*(buffer + 1))){
-                    serrorno = SEBADREDIR;
+                    serrno = SEBADREDIR;
                     return SHELL_ERR;
                 } else {
                     buffer++;
@@ -258,7 +258,7 @@ int set_redirection(Command* c){
                     if(*buffer == '>'){
                         //Si il n'y a rien apres
                         if(!(*(buffer + 1))){
-                            serrorno = SEBADREDIR;
+                            serrno = SEBADREDIR;
                             return SHELL_ERR;
                         } else {
                             buffer++;
@@ -266,7 +266,7 @@ int set_redirection(Command* c){
                             if(*buffer == '&'){
                                 //Si il n'y a rien apres
                                 if(!(*(buffer + 1))){
-                                    serrorno = SEBADREDIR;
+                                    serrno = SEBADREDIR;
                                     return SHELL_ERR;
                                 }
                                 buffer++;
@@ -282,7 +282,7 @@ int set_redirection(Command* c){
                     else if(*buffer == '&'){
                         //Si il n'y a rien apres
                         if(!(*(buffer + 1))){
-                            serrorno = SEBADREDIR;
+                            serrno = SEBADREDIR;
                             return SHELL_ERR;
                         }
                         redir = SHELLRE_ALL;
@@ -300,7 +300,7 @@ int set_redirection(Command* c){
                 buffer++;
                 //Si il n'y a rien apres
                 if(!(*(buffer + 1))){
-                    serrorno = SEBADREDIR;
+                    serrno = SEBADREDIR;
                     return SHELL_ERR;
                 } else {
                     buffer++;
@@ -309,7 +309,7 @@ int set_redirection(Command* c){
                         buffer++;
                         //Si il n'y a rien apres
                         if(!(*(buffer + 1))){
-                            serrorno = SEBADREDIR;
+                            serrno = SEBADREDIR;
                             return SHELL_ERR;
                         }
                         redir = SHELLR_ERR;
@@ -340,7 +340,7 @@ int set_redirection(Command* c){
                 return finCmd + 1;
             }
             //Sinon on est dans un redirection non terminée
-            serrorno = SEBADCMD;
+            serrno = SEBADCMD;
             return SHELL_ERR;
         }
         //Regarde si le nom du fichier est entre ""
@@ -363,7 +363,7 @@ int set_redirection(Command* c){
         }
         //Si fin de la commande et guillemet alors erreur
         if(!(*buffer) && guillemet){
-            serrorno = SEBADCMD;
+            serrno = SEBADCMD;
             return SHELL_ERR;
         }
         //Retire guillement si besoin
@@ -413,7 +413,7 @@ int split_command(Command* c, char* cmd){
             if(*cmd == '"'){
                 //Verif que ce n'est pas la fin
                 if(!(*(cmd + 1))){
-                    serrorno = SEBADCMD;
+                    serrno = SEBADCMD;
                     return SHELL_ERR;
                 }
                 cmd++;
@@ -542,13 +542,13 @@ int parse_command(Command* c){
             nbWildcard = wildcard(c->argv[i], nbWildcard, wildcardTab);
             //Verif retour
             if(nbWildcard == ERR){
-                serrorno = SEWC;
+                serrno = SEWC;
                 return SHELL_ERR;
             }
             //Ajoute les wildcard dans argv (le +1 est la pour garder le NULL à la fin)
             c->argv = insert_array(i, c->argv, c->argc + 1, wildcardTab, nbWildcard, &res);
             if(res == ERR){
-                serrorno = SEADDWC;
+                serrno = SEADDWC;
                 return SHELL_ERR;
             }
             c->argc = res - 1;  //On ne compte pas le NULL final