mysh.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 "error.h"
  12. #include "str.h"
  13. #include "parser.h"
  14. #include "command.h"
  15. #include "execute.h"
  16. #include "mysh.h"
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. /* --- Extern --- */
  20. extern Error error;
  21. /* --- Fonctions privées --- */
  22. void test_write() {
  23. char* a = "azerty\n";
  24. int tmp = write(1, a, strlen(a));
  25. printf("%d\n", tmp);
  26. }
  27. /* --- Fonctions utilitaires --- */
  28. void show_current_dir(const char* before, const char* after) {
  29. char buffer[BUFFER_SIZE];
  30. if (getcwd(buffer, sizeof (buffer)) == NULL) {
  31. addperror("Erreur getcwd()");
  32. } else {
  33. if(before == NULL && after == NULL){
  34. printf("%s", buffer);
  35. } else if(before == NULL){
  36. printf("%s%s", buffer, after);
  37. } else if(after == NULL){
  38. printf("%s%s", before, buffer);
  39. } else {
  40. printf("%s%s%s", before, buffer, after);
  41. }
  42. }
  43. }
  44. /* --- Main --- */
  45. int main(int argc, char* argv[]) {
  46. CommandTab ct;
  47. char str[BUFFER_SIZE];
  48. int a;
  49. //Initialisation erreur
  50. error_finit("mysh.log");
  51. printf("%d\n", is_executable_file("../TP4/exo1"));
  52. error.exit();
  53. //Recup ligne
  54. //printf("%s\n", fgets(str, 500, stdin));&
  55. memset(str, 0, 500);
  56. a = read(STDIN, str, 500);
  57. printf("%s\n", str);
  58. //Separe les commandes
  59. a = parse_line(&ct, str);
  60. if(a == SHELL_ERR){
  61. addserror("Erreur lors du parse de la ligne");
  62. error.exit_err();
  63. }
  64. //Parse les commandes
  65. a = parse_all_command(&ct);
  66. if(a == SHELL_FAIL){
  67. addserror("Erreur lors du parse des commandes");
  68. error.exit_err();
  69. }
  70. //Affiche resultat
  71. for (int i = 0; i < ct.length; i++) {
  72. Command* c = ct.cmd[i];
  73. printf("Commande %d (%s) : \n", i, c->name);
  74. for (int j = 0; j < c->argc; j++) {
  75. printf(" Argument %d : %s\n", j, c->argv[j]);
  76. }
  77. printf("Commande en fond : %d\n\n", ct.bck);
  78. //Si c'est une commande interne on l'execute
  79. if(is_internal_cmd(ct.cmd[i]->name)){
  80. show_current_dir(NULL, "\n");
  81. printf("Result : %d\n", launch_internal_command(ct.cmd[i]));
  82. show_current_dir(NULL, "\n");
  83. }
  84. }
  85. //Supprime
  86. clean_command(&ct);
  87. error.exit();
  88. }