mysh.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * File: mysh.c
  3. * Author: Arthur Brandao
  4. *
  5. * Created on 31 octobre 2018, 12:43
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <fcntl.h>
  11. #include <sys/stat.h>
  12. #include <wait.h>
  13. #include "error.h"
  14. #include "str.h"
  15. #include "parser.h"
  16. #include "command.h"
  17. #include "execute.h"
  18. #include "mysh.h"
  19. /* --- Extern --- */
  20. extern Error error;
  21. /* --- Global --- */
  22. pid_list pidlist;
  23. pid_node* active = NULL;
  24. /* --- Fonctions privées --- */
  25. void test_write() {
  26. char* a = "azerty\n";
  27. int tmp = write(1, a, strlen(a));
  28. printf("%d\n", tmp);
  29. }
  30. /* --- Fonctions utilitaires --- */
  31. void show_current_dir(const char* before, const char* after) {
  32. char buffer[BUFFER_SIZE];
  33. if (getcwd(buffer, sizeof (buffer)) == NULL) {
  34. addperror("Erreur getcwd()");
  35. } else {
  36. if(before == NULL && after == NULL){
  37. printf("%s", buffer);
  38. } else if(before == NULL){
  39. printf("%s%s", buffer, after);
  40. } else if(after == NULL){
  41. printf("%s%s", before, buffer);
  42. } else {
  43. printf("%s%s%s", before, buffer, after);
  44. }
  45. }
  46. }
  47. /* --- Main --- */
  48. int main(int argc, char* argv[]) {
  49. CommandTab ct;
  50. char str[BUFFER_SIZE];
  51. int a;
  52. //Initialisation structures
  53. error_finit("mysh.log");
  54. ini_pid_list(&pidlist);
  55. printf("%d\n", is_executable_file("../TP4/exo1"));
  56. error.exit();
  57. //Recup ligne
  58. //printf("%s\n", fgets(str, 500, stdin));&
  59. memset(str, 0, 500);
  60. a = read(STDIN, str, 500);
  61. printf("%s\n", str);
  62. //Separe les commandes
  63. a = parse_line(&ct, str);
  64. if(a == SHELL_ERR){
  65. addserror("Erreur lors du parse de la ligne");
  66. error.exit_err();
  67. }
  68. //Parse les commandes
  69. a = parse_all_command(&ct);
  70. if(a == SHELL_FAIL){
  71. addserror("Erreur lors du parse des commandes");
  72. error.exit_err();
  73. }
  74. //Affiche resultat
  75. for (int i = 0; i < ct.length; i++) {
  76. Command* c = ct.cmd[i];
  77. printf("Commande %d (%s) : \n", i, c->name);
  78. for (int j = 0; j < c->argc; j++) {
  79. printf(" Argument %d : %s\n", j, c->argv[j]);
  80. }
  81. printf("Commande en fond : %d\n\n", ct.bck);
  82. //Si c'est une commande interne on l'execute
  83. if(is_internal_cmd(ct.cmd[i]->name)){
  84. show_current_dir(NULL, "\n");
  85. printf("Result : %d\n", launch_internal_command(ct.cmd[i]));
  86. show_current_dir(NULL, "\n");
  87. }
  88. }
  89. //Nettoyage structures
  90. clean_command(&ct);
  91. clean_pid(&pidlist);
  92. error.exit();
  93. }
  94. int run(CommandTab ct){
  95. pid_t pid;
  96. int result;
  97. pid_node* pnode;
  98. //Fork pour executer la commande
  99. pid = fork();
  100. if(pid == ERR){
  101. addperror("Erreur lors du fork pour l'execution des commandes");
  102. error.print("Erreur systeme, impossible de continuer\n");
  103. return SHELL_ERR;
  104. }
  105. //Pere
  106. else if(pid != 0){
  107. //Ajoute du pid dans la liste des pid actifs
  108. pnode = add_pid(&pidlist, pid);
  109. //Si on attend le retour de la commande
  110. if(!ct.bck){
  111. //Ajout du pid comme étant le pid actif et attente
  112. active = pnode;
  113. wait(&result);
  114. //Le processus n'est plus actif et analyse retour
  115. active = NULL;
  116. remove_pid(&pidlist, pnode);
  117. if(WIFEXITED(result)){
  118. return WEXITSTATUS(result);
  119. } else {
  120. return SHELL_FAIL;
  121. }
  122. }
  123. //Sinon ok
  124. return SHELL_OK;
  125. }
  126. //Fils
  127. return SHELL_OK;
  128. }