mysh.c 2.4 KB

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