mysh.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 "mysh.h"
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. /* --- Extern --- */
  18. extern Error error;
  19. /* --- Fonctions privées --- */
  20. void test_write() {
  21. char* a = "azerty\n";
  22. int tmp = write(1, a, strlen(a));
  23. printf("%d\n", tmp);
  24. }
  25. /* --- Fonctions utilitaires --- */
  26. void show_current_dir(const char* before, const char* after) {
  27. char buffer[BUFFER_SIZE];
  28. if (getcwd(buffer, sizeof (buffer)) == NULL) {
  29. addperror("Erreur getcwd()");
  30. } else {
  31. if(before == NULL && after == NULL){
  32. printf("%s", buffer);
  33. } else if(before == NULL){
  34. printf("%s%s", buffer, after);
  35. } else if(after == NULL){
  36. printf("%s%s", before, buffer);
  37. } else {
  38. printf("%s%s%s", before, buffer, after);
  39. }
  40. }
  41. }
  42. /* --- Main --- */
  43. int main(int argc, char* argv[]) {
  44. CommandTab ct;
  45. char str[BUFFER_SIZE];
  46. int a;
  47. //Initialisation erreur
  48. error_finit("mysh.log");
  49. //Recup ligne
  50. //printf("%s\n", fgets(str, 500, stdin));&
  51. memset(str, 0, 500);
  52. a = read(STDIN, str, 500);
  53. printf("%s\n", str);
  54. //Separe les commandes
  55. a = parse_line(&ct, str);
  56. if(a == SHELL_ERR){
  57. addserror("Erreur lors du parse de la ligne");
  58. error.exit_err();
  59. }
  60. //Parse les commandes
  61. a = parse_all_command(&ct);
  62. if(a == SHELL_FAIL){
  63. addserror("Erreur lors du parse des commandes");
  64. error.exit_err();
  65. }
  66. //Affiche resultat
  67. for (int i = 0; i < ct.length; i++) {
  68. Command* c = ct.cmd[i];
  69. printf("Commande %d (%s) : \n", i, c->name);
  70. for (int j = 0; j < c->argc; j++) {
  71. printf(" Argument %d : %s\n", j, c->argv[j]);
  72. }
  73. printf("Commande en fond : %d\n\n", ct.bck);
  74. }
  75. //Supprime
  76. clean_command(&ct);
  77. error.exit();
  78. }
  79. /* --- Commandes internes --- */
  80. void cd(int argc, char** argv) {
  81. //Si trop d'arguments
  82. if (argc > 2) {
  83. error.print("too many arguments : 1 required, %d given\n", argc - 1);
  84. } else {
  85. //Si aucun argument on va à la racine
  86. if (argc == 1) {
  87. if (chdir("/") == ERR) {
  88. addperror("Erreur chdir()");
  89. }
  90. } //Sinon on va dans le dossier indiqué par l'utilisateur
  91. else {
  92. if (chdir(argv[1]) == ERR) {
  93. error.print("path does not exist\n");
  94. }
  95. }
  96. }
  97. //show_current_dir("current working directory is: ", "\n");
  98. }