mysh.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * File: mysh.c
  3. * Author: Arthur Brandao
  4. *
  5. * Created on 31 octobre 2018, 12:43
  6. */
  7. #define _POSIX_C_SOURCE 2
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #include <fcntl.h>
  12. #include <sys/stat.h>
  13. #include <wait.h>
  14. #include "error.h"
  15. #include "str.h"
  16. #include "parser.h"
  17. #include "command.h"
  18. #include "execute.h"
  19. #include "mysh.h"
  20. /* --- Extern --- */
  21. extern Error error;
  22. extern boolean exitsh;
  23. /* --- Global --- */
  24. pid_list pidlist;
  25. pid_node* active = NULL;
  26. /* --- Fonctions privées --- */
  27. void test_write() {
  28. char* a = "azerty\n";
  29. int tmp = write(1, a, strlen(a));
  30. printf("%d\n", tmp);
  31. }
  32. void test_tmp_file(){
  33. int a;
  34. FILE* f = tmpfile();
  35. printf("F : %d\n", f == NULL);
  36. int fd = fileno(f);
  37. printf("%d : %ld\n", fd, write(fd, "bonjour", 8));
  38. a = lseek(fd, 0L, SEEK_SET);
  39. sleep(2);
  40. char buf[10];
  41. memset(buf, 0 , 10);
  42. a = read(fd, buf, 10);
  43. printf("%d : %s\n", a, buf);
  44. close(fd);
  45. }
  46. void test(){
  47. CommandTab ct;
  48. char str[BUFFER_SIZE];
  49. int a;
  50. //Initialisation structures
  51. error_finit("mysh.log");
  52. ini_pid_list(&pidlist);
  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. //Nettoyage structures
  86. clean_command(&ct);
  87. clean_pid(&pidlist);
  88. error.exit();
  89. }
  90. /* --- Fonctions utilitaires --- */
  91. void show_current_dir(const char* before, const char* after) {
  92. char buffer[BUFFER_SIZE];
  93. if (getcwd(buffer, sizeof (buffer)) == NULL) {
  94. addperror("Erreur getcwd()");
  95. } else {
  96. if(before == NULL && after == NULL){
  97. printf("%s", buffer);
  98. } else if(before == NULL){
  99. printf("%s%s", buffer, after);
  100. } else if(after == NULL){
  101. printf("%s%s", before, buffer);
  102. } else {
  103. printf("%s%s%s", before, buffer, after);
  104. }
  105. }
  106. fflush(stdout);
  107. }
  108. int get_line(char* buffer){
  109. memset(buffer, 0, BUFFER_SIZE);
  110. if(read(STDIN, buffer, BUFFER_SIZE) == ERR){
  111. addperror("Impossible de lire dans STDIN");
  112. return SHELL_ERR;
  113. }
  114. return SHELL_OK;
  115. }
  116. int get_tmp_file(){
  117. FILE* f = tmpfile();
  118. if(f == NULL){
  119. adderror("Impossible de créer un fichier temporaire");
  120. return SHELL_ERR;
  121. }
  122. return fileno(f);
  123. }
  124. /* --- Main --- */
  125. int main(int argc, char* argv[]) {
  126. //Declaration variables
  127. CommandTab ct;
  128. int result;
  129. char line[BUFFER_SIZE], before[BUFFER_SIZE];
  130. //Initialisation structures
  131. error_finit("mysh.log");
  132. ini_pid_list(&pidlist);
  133. //Preparation affichage
  134. sprintf(before, "\x1b[32m%s:\x1b[36m", getlogin());
  135. //Boucle infini de lecture
  136. while(!exitsh){
  137. //Affichage repertoire
  138. show_current_dir(before, ">\x1b[0m ");
  139. //Lecture ligne
  140. if(get_line(line) == SHELL_ERR){
  141. error.print("Impossible de lire les commandes\n");
  142. clean_pid(&pidlist);
  143. error.exit_err();
  144. }
  145. //Parse la ligne et commandes
  146. result = parse_line(&ct, line);
  147. if(result == SHELL_ERR){
  148. error.print("Impossible d'analyser la ligne\n");
  149. addserror("Erreur lors du parse de la ligne");
  150. continue;
  151. }
  152. //Si aucune commande on passe
  153. printf("%d\n", ct.length);
  154. if(ct.length == 0){
  155. clean_command(&ct);
  156. continue;
  157. }
  158. //Parse les commandes
  159. result = parse_all_command(&ct);
  160. if(result == SHELL_FAIL){
  161. error.print("Impossible d'analyser la commande\n");
  162. addserror("Erreur lors du parse des commandes");
  163. continue;
  164. }
  165. //Execute
  166. result = run(ct);
  167. printf("Result : %d\n", result);
  168. //Vide le resultat du parse de la ligne de commande
  169. clean_command(&ct);
  170. }
  171. //Nettoyage
  172. clean_pid(&pidlist);
  173. error.end();
  174. return EXIT_SUCCESS;
  175. }
  176. int run(CommandTab ct){
  177. pid_t pid;
  178. int result = 0;
  179. //Si en fond creation d'un fork pour executer les commandes
  180. printf("bck : %d\n", ct.bck);
  181. if(ct.bck && false/*ToDo - Test plus tard*/){
  182. pid = fork();
  183. if(pid == ERR){
  184. addperror("Erreur lors du fork pour l'execution des commandes");
  185. error.print("Erreur systeme, impossible de continuer\n");
  186. return SHELL_ERR;
  187. }
  188. //Fils
  189. if(pid == 0){
  190. ct.bck = 0;
  191. result = run(ct);
  192. if(result == SHELL_FAIL){
  193. exit(EXIT_FAILURE);
  194. }
  195. exit(EXIT_SUCCESS);
  196. }
  197. //Ajout du fils dans la liste des pid
  198. add_pid(&pidlist, pid);
  199. //Pour le pere c'est fini
  200. return SHELL_OK;
  201. }
  202. //Sinon execution de chaque commande
  203. Command* c;
  204. int tube[ct.length][2];
  205. int tubepos = 0;
  206. int infd = -1, outfd = -1, errfd = -1;
  207. boolean bpipe = false, nextpipe = false;
  208. //Parcours les commandes
  209. for(int i = 0; i < ct.length; i++){
  210. c = ct.cmd[i];
  211. printf("Cmd : %s\n", c->name);
  212. //Si pipe avant
  213. if(nextpipe){
  214. //Si fin chaine pipe
  215. if(c->next != SHELL_PIPE){
  216. nextpipe = false;
  217. }
  218. //Passe la commande
  219. continue;
  220. }
  221. //Si pipe creation d'un fichier commun
  222. nextpipe = c->next == SHELL_PIPE ;
  223. if(c->next == SHELL_PIPE && c->output == STDOUT){
  224. bpipe = true;
  225. //Creation tube
  226. if(pipe(tube[tubepos]) == ERR){
  227. addperror("Impossible de créer un tube");
  228. return SHELL_FAIL;
  229. }
  230. //Redirection
  231. c->output = tube[tubepos][TUBE_ECRITURE];
  232. if(ct.cmd[i + 1]->input == STDIN){
  233. ct.cmd[i + 1]->input = tube[tubepos][TUBE_LECTURE];
  234. }
  235. }
  236. //Effectue les redirections IO
  237. if(c->input != STDIN){
  238. infd = redirect_fd2(STDIN, c->input);
  239. if(infd == ERR){
  240. return SHELL_FAIL;
  241. }
  242. }
  243. if(c->output != STDOUT){
  244. outfd = redirect_fd2(STDOUT, c->output);
  245. if(outfd == ERR){
  246. return SHELL_FAIL;
  247. }
  248. }
  249. if(c->error != STDERR){
  250. errfd = redirect_fd2(STDERR, c->error);
  251. if(errfd == ERR){
  252. return SHELL_FAIL;
  253. }
  254. }
  255. //Execute la commande
  256. if(is_internal_cmd(c->name)){
  257. result = launch_internal_command(c);
  258. } else if(is_executable_file(c->name)){
  259. result = exec_file(c->name, c->argv);
  260. } else {
  261. result = exec_shell(c->name, c->argv);
  262. }
  263. printf("%d\n", result);
  264. //Reset IO
  265. if(c->input != STDIN){
  266. infd = redirect_fd(STDIN, infd);
  267. if(infd == ERR){
  268. return SHELL_FAIL;
  269. }
  270. }
  271. if(c->output != STDOUT){
  272. outfd = redirect_fd(STDOUT, outfd);
  273. if(outfd == ERR){
  274. return SHELL_FAIL;
  275. }
  276. }
  277. if(c->error != STDERR){
  278. errfd = redirect_fd(STDERR, errfd);
  279. if(errfd == ERR){
  280. return SHELL_FAIL;
  281. }
  282. }
  283. //Fermeture tube
  284. if(bpipe){
  285. bpipe = false;
  286. if(close(outfd) == ERR){
  287. addperror("Impossible de fermer tube ecriture");
  288. return SHELL_FAIL;
  289. }
  290. c->output = STDOUT;
  291. }
  292. //Agit en fonction de la jointure avec la prochaine commande
  293. /*ToDo*/
  294. }
  295. if(result != EXIT_SUCCESS){
  296. return SHELL_FAIL;
  297. }
  298. return SHELL_OK;
  299. }