mysh.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. //Recup ligne
  56. //printf("%s\n", fgets(str, 500, stdin));&
  57. memset(str, 0, 500);
  58. a = read(STDIN, str, 500);
  59. printf("%s\n", str);
  60. //Separe les commandes
  61. a = parse_line(&ct, str);
  62. if(a == SHELL_ERR){
  63. addserror("Erreur lors du parse de la ligne");
  64. error.exit_err();
  65. }
  66. //Parse les commandes
  67. a = parse_all_command(&ct);
  68. if(a == SHELL_FAIL){
  69. addserror("Erreur lors du parse des commandes");
  70. error.exit_err();
  71. }
  72. //Affiche resultat
  73. for (int i = 0; i < ct.length; i++) {
  74. Command* c = ct.cmd[i];
  75. printf("Commande %d (%s) : \n", i, c->name);
  76. for (int j = 0; j < c->argc; j++) {
  77. printf(" Argument %d : %s\n", j, c->argv[j]);
  78. }
  79. printf("Commande en fond : %d\n\n", ct.bck);
  80. //Si c'est une commande interne on l'execute
  81. if(is_internal_cmd(ct.cmd[i]->name)){
  82. show_current_dir(NULL, "\n");
  83. printf("Result : %d\n", launch_internal_command(ct.cmd[i]));
  84. show_current_dir(NULL, "\n");
  85. }
  86. }
  87. //Nettoyage structures
  88. clean_command(&ct);
  89. clean_pid(&pidlist);
  90. error.exit();
  91. }
  92. int run(CommandTab ct){
  93. pid_t pid;
  94. int result;
  95. pid_node* pnode;
  96. //Fork pour executer la commande
  97. pid = fork();
  98. if(pid == ERR){
  99. addperror("Erreur lors du fork pour l'execution des commandes");
  100. error.print("Erreur systeme, impossible de continuer\n");
  101. return SHELL_ERR;
  102. }
  103. //Pere
  104. else if(pid != 0){
  105. //Ajoute du pid dans la liste des pid actifs
  106. pnode = add_pid(&pidlist, pid);
  107. //Si on attend le retour de la commande
  108. if(!ct.bck){
  109. //Ajout du pid comme étant le pid actif et attente
  110. active = pnode;
  111. wait(&result);
  112. //Le processus n'est plus actif et analyse retour
  113. active = NULL;
  114. remove_pid(&pidlist, pnode);
  115. if(WIFEXITED(result)){
  116. return WEXITSTATUS(result);
  117. } else {
  118. return SHELL_FAIL;
  119. }
  120. }
  121. //Sinon ok
  122. return SHELL_OK;
  123. }
  124. //Fils
  125. return SHELL_OK;
  126. }