|  | @@ -48,35 +48,7 @@ void test_tmp_file(){
 | 
											
												
													
														|  |      close(fd);
 |  |      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;
 |  |      CommandTab ct;
 | 
											
												
													
														|  |      char str[BUFFER_SIZE];
 |  |      char str[BUFFER_SIZE];
 | 
											
												
													
														|  |      int a;
 |  |      int a;
 | 
											
										
											
												
													
														|  | @@ -84,9 +56,6 @@ int main(int argc, char* argv[]) {
 | 
											
												
													
														|  |      error_finit("mysh.log");
 |  |      error_finit("mysh.log");
 | 
											
												
													
														|  |      ini_pid_list(&pidlist);
 |  |      ini_pid_list(&pidlist);
 | 
											
												
													
														|  |      
 |  |      
 | 
											
												
													
														|  | -    
 |  | 
 | 
											
												
													
														|  | -    
 |  | 
 | 
											
												
													
														|  | -    error.exit();
 |  | 
 | 
											
												
													
														|  |      //Recup ligne
 |  |      //Recup ligne
 | 
											
												
													
														|  |      //printf("%s\n", fgets(str, 500, stdin));&
 |  |      //printf("%s\n", fgets(str, 500, stdin));&
 | 
											
												
													
														|  |      memset(str, 0, 500);
 |  |      memset(str, 0, 500);
 | 
											
										
											
												
													
														|  | @@ -125,59 +94,132 @@ int main(int argc, char* argv[]) {
 | 
											
												
													
														|  |      error.exit();
 |  |      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){
 |  |  int run(CommandTab ct){
 | 
											
												
													
														|  |      pid_t pid;
 |  |      pid_t pid;
 | 
											
												
													
														|  |      int result;
 |  |      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;
 |  |          return SHELL_OK;
 | 
											
												
													
														|  |      }
 |  |      }
 | 
											
												
													
														|  | -    // --- Fils ---
 |  | 
 | 
											
												
													
														|  | -    //Reset sortie erreur
 |  | 
 | 
											
												
													
														|  | -    error.end();
 |  | 
 | 
											
												
													
														|  | -    //Execute chaque commande
 |  | 
 | 
											
												
													
														|  | 
 |  | +    //Sinon execution de chaque commande
 | 
											
												
													
														|  |      Command* c;
 |  |      Command* c;
 | 
											
												
													
														|  | -    int tmpfd;
 |  | 
 | 
											
												
													
														|  | 
 |  | +    //int tmpfd;
 | 
											
												
													
														|  | 
 |  | +    //Parcours les commandes
 | 
											
												
													
														|  |      for(int i = 0; i < ct.length; i++){
 |  |      for(int i = 0; i < ct.length; i++){
 | 
											
												
													
														|  |          c = ct.cmd[i];
 |  |          c = ct.cmd[i];
 | 
											
												
													
														|  |          //Si il y a un pipe creation d'un fichier temporaire à la place de la sortie standard
 |  |          //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();
 |  |              tmpfd = get_tmp_file();
 | 
											
												
													
														|  |              if(tmpfd == SHELL_ERR){
 |  |              if(tmpfd == SHELL_ERR){
 | 
											
												
													
														|  |                  exit(EXIT_FAILURE);
 |  |                  exit(EXIT_FAILURE);
 | 
											
												
													
														|  |              }
 |  |              }
 | 
											
												
													
														|  |              tmpfd = redirect_fd(STDOUT, tmpfd);
 |  |              tmpfd = redirect_fd(STDOUT, tmpfd);
 | 
											
												
													
														|  | -        }
 |  | 
 | 
											
												
													
														|  | 
 |  | +        }*/
 | 
											
												
													
														|  |          //Effectue les redirections IO
 |  |          //Effectue les redirections IO
 | 
											
												
													
														|  |          /*ToDo*/
 |  |          /*ToDo*/
 | 
											
												
													
														|  |          //Execute la commande
 |  |          //Execute la commande
 | 
											
												
													
														|  |          if(is_internal_cmd(c->name)){
 |  |          if(is_internal_cmd(c->name)){
 | 
											
												
													
														|  |              result = launch_internal_command(c);
 |  |              result = launch_internal_command(c);
 | 
											
												
													
														|  | 
 |  | +            printf("%d\n", result);
 | 
											
												
													
														|  |          } else if(is_executable_file(c->name)){
 |  |          } else if(is_executable_file(c->name)){
 | 
											
												
													
														|  |              
 |  |              
 | 
											
												
													
														|  |          } else {
 |  |          } else {
 |