command.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * File: command.c
  3. * Author: Arthur Brandao
  4. *
  5. * Created on 9 novembre 2018
  6. */
  7. #define _POSIX_C_SOURCE 200112L
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #include "error.h"
  12. #include "str.h"
  13. #include "parser.h"
  14. #include "mysh.h"
  15. #include "execute.h"
  16. #include "ipc.h"
  17. #include "variable.h"
  18. #include "command.h"
  19. /* --- Extern --- */
  20. extern Error error;
  21. extern pid_t last;
  22. extern int status_cmd;
  23. extern int result_cmd;
  24. char* cmdlist[] = {
  25. "cd",
  26. "exit",
  27. "status",
  28. "setenv",
  29. "unsetenv",
  30. "set",
  31. "unset",
  32. NULL
  33. };
  34. boolean exitsh = false;
  35. /* --- Fonctions publiques --- */
  36. void ini_pid_list(pid_list* pl) {
  37. pl->first = NULL;
  38. pl->last = NULL;
  39. }
  40. pid_node* add_pid(pid_list* pl, pid_t pid, int job) {
  41. pid_node* pn = malloc(sizeof (pid_node));
  42. pn->pid = pid;
  43. pn->job = job;
  44. pn->next = NULL;
  45. if (pl->first == NULL) {
  46. pn->prev = NULL;
  47. pl->first = pn;
  48. pl->last = pn;
  49. } else {
  50. pn->prev = pl->last;
  51. pl->last->next = pn;
  52. pl->last = pn;
  53. }
  54. return pn;
  55. }
  56. pid_node* search_pid(pid_list* pl, pid_t pid) {
  57. pid_node* pn = pl->first;
  58. while (pn != NULL) {
  59. if (pn->pid == pid) {
  60. return pn;
  61. }
  62. pn = pn->next;
  63. }
  64. return NULL;
  65. }
  66. void remove_pid(pid_list* pl, pid_node* pn) {
  67. //Si 1er et seul
  68. if (pn->prev == NULL && pn->next == NULL) {
  69. pl->first = NULL;
  70. pl->last = NULL;
  71. }
  72. //Si 1er et non seul
  73. else if (pn->prev == NULL && pn->next != NULL) {
  74. pn->next->prev = NULL;
  75. pl->first = pn->next;
  76. }
  77. //Si dernier
  78. else if (pn->next == NULL) {
  79. pn->prev->next = NULL;
  80. pl->last = pn->prev;
  81. }
  82. //Sinon si il est au milieu
  83. else {
  84. pn->prev->next = pn->next;
  85. pn->next->prev = pn->prev;
  86. }
  87. //Free
  88. free(pn);
  89. }
  90. void clean_pid(pid_list* pl) {
  91. pid_node* tmp, * pn = pl->first;
  92. while (pn != NULL) {
  93. tmp = pn->next;
  94. free(pn);
  95. pn = tmp;
  96. }
  97. pl->first = NULL;
  98. pl->last = NULL;
  99. }
  100. boolean is_internal_cmd(const char* cmd) {
  101. //Parcours tableau pour trouver correspondance
  102. for (int i = 0; cmdlist[i] != NULL; i++) {
  103. if (strncmp(cmd, cmdlist[i], strlen(cmd)) == 0) {
  104. return true;
  105. }
  106. }
  107. return false;
  108. }
  109. int launch_internal_command(Command* cmd) {
  110. int result, length = strlen(cmd->name) + 1;
  111. //cd
  112. if (strncmp(cmd->name, cmdlist[0], length) == 0) {
  113. result = cd(cmd->argc, cmd->argv);
  114. return result;
  115. }
  116. //exit
  117. else if (strncmp(cmd->name, cmdlist[1], length) == 0) {
  118. result = exit_sh(cmd->argc, cmd->argv);
  119. return result;
  120. }
  121. //status
  122. else if (strncmp(cmd->name, cmdlist[2], length) == 0) {
  123. result = status(cmd->argc, cmd->argv);
  124. return result;
  125. }
  126. //setenv
  127. else if (strncmp(cmd->name, cmdlist[3], length) == 0) {
  128. result = set_env(cmd->argc, cmd->argv);
  129. return result;
  130. }
  131. //unsetenv
  132. else if (strncmp(cmd->name, cmdlist[4], length) == 0) {
  133. result = unset_env(cmd->argc, cmd->argv);
  134. return result;
  135. }
  136. //set
  137. else if (strncmp(cmd->name, cmdlist[5], length) == 0) {
  138. result = set_local(cmd->argc, cmd->argv);
  139. return result;
  140. }
  141. //unset
  142. else if (strncmp(cmd->name, cmdlist[6], length) == 0) {
  143. result = unset_local(cmd->argc, cmd->argv);
  144. return result;
  145. }
  146. //Aucune commande
  147. else {
  148. return SHELL_FAIL;
  149. }
  150. }
  151. /* --- Commandes --- */
  152. int cd(int argc, char** argv) {
  153. //Si trop d'arguments
  154. if (argc > 2) {
  155. error.print("too many arguments : 1 required, %d given\n", argc - 1);
  156. return EXIT_FAILURE;
  157. } else {
  158. //Si aucun argument on va à la racine
  159. if (argc == 1) {
  160. if (chdir("/") == ERR) {
  161. addperror("Erreur chdir()");
  162. return EXIT_FAILURE;
  163. }
  164. }
  165. //Sinon on va dans le dossier indiqué par l'utilisateur
  166. else {
  167. if (chdir(argv[1]) == ERR) {
  168. error.print("path does not exist\n");
  169. return EXIT_FAILURE;
  170. }
  171. }
  172. }
  173. return EXIT_SUCCESS;
  174. //show_current_dir("current working directory is: ", "\n");
  175. }
  176. int exit_sh(int argc, char** argv) {
  177. exitsh = true;
  178. return EXIT_SUCCESS;
  179. }
  180. int status(int argc, char** argv) {
  181. if (!(last != -1 && status_cmd != -1)) {
  182. error.print("Aucune commande executée\n");
  183. return EXIT_FAILURE;
  184. }
  185. //En fonction du status
  186. if (result_cmd == SHELL_FAIL) {
  187. printf("%d terminé anormalement\n", last);
  188. } else {
  189. printf("%d terminé avec comme code de retour %d\n", last, status_cmd);
  190. }
  191. return EXIT_SUCCESS;
  192. }
  193. int set_env(int argc, char** argv) {
  194. char* str, * key, * val;
  195. int length, pos = 0;
  196. //Verif les arguments
  197. if (argc < 2) {
  198. error.print("too few arguments : 1 required, 0 given\n");
  199. return EXIT_FAILURE;
  200. }
  201. if (argc > 2) {
  202. error.print("too many arguments : 1 required, %d given\n", argc - 1);
  203. return EXIT_FAILURE;
  204. }
  205. str = argv[1];
  206. length = strlen(str);
  207. //Verif que chaine est correcte
  208. for (int i = 0; i < length; i++) {
  209. if (str[i] == '=') {
  210. break;
  211. }
  212. pos++;
  213. }
  214. if (pos >= length - 1) {
  215. error.print("Argument invalide : clef=valeur attendu, %s donnée\n", str);
  216. return EXIT_FAILURE;
  217. }
  218. //Decoupe la chaine
  219. key = malloc(sizeof (char) * (pos + 1));
  220. memset(key, 0, pos + 1);
  221. strncpy(key, str, pos);
  222. val = str + pos + 1;
  223. //Ajoute la chaine en shm
  224. if (!add_shm_data(str)) {
  225. error.print("Erreur interne impossible d'ajouter la variable\n");
  226. free(key);
  227. return EXIT_FAILURE;
  228. }
  229. //Ajoute sur le système
  230. if (setenv(key, val, true) == ERR) {
  231. addperror("Impossible d'ajouter la variable d'environnement");
  232. error.print("Erreur interne impossible d'ajouter la variable\n");
  233. free(key);
  234. return EXIT_FAILURE;
  235. }
  236. free(key);
  237. return EXIT_SUCCESS;
  238. }
  239. int unset_env(int argc, char** argv) {
  240. //Verif les arguments
  241. if (argc < 2) {
  242. error.print("too few arguments : 1 required, 0 given\n");
  243. return EXIT_FAILURE;
  244. }
  245. if (argc > 2) {
  246. error.print("too many arguments : 1 required, %d given\n", argc - 1);
  247. return EXIT_FAILURE;
  248. }
  249. //Supprime de la shm
  250. if (!remove_shm_data(argv[1])) {
  251. error.print("Erreur interne impossible de supprimer la variable\n");
  252. return EXIT_FAILURE;
  253. }
  254. //Supprime du systeme
  255. if (unsetenv(argv[1]) == ERR) {
  256. addperror("Impossible de supprimer la variable d'environnement");
  257. error.print("Erreur interne impossible de supprimer la variable\n");
  258. return EXIT_FAILURE;
  259. }
  260. return EXIT_SUCCESS;
  261. }
  262. int set_local(int argc, char** argv){
  263. char* str;
  264. int length, pos = 0;
  265. //Verif les arguments
  266. if (argc < 2) {
  267. error.print("too few arguments : 1 required, 0 given\n");
  268. return EXIT_FAILURE;
  269. }
  270. if (argc > 2) {
  271. error.print("too many arguments : 1 required, %d given\n", argc - 1);
  272. return EXIT_FAILURE;
  273. }
  274. str = argv[1];
  275. length = strlen(str);
  276. //Verif que chaine est correcte
  277. for (int i = 0; i < length; i++) {
  278. if (str[i] == '=') {
  279. break;
  280. }
  281. pos++;
  282. }
  283. if (pos >= length - 1) {
  284. error.print("Argument invalide : clef=valeur attendu, %s donnée\n", str);
  285. return EXIT_FAILURE;
  286. }
  287. //Ajoute la chaine en shm
  288. if (!add_local_data(str)) {
  289. error.print("Erreur interne impossible d'ajouter la variable\n");
  290. return EXIT_FAILURE;
  291. }
  292. return EXIT_SUCCESS;
  293. }
  294. int unset_local(int argc, char** argv){
  295. //Verif les arguments
  296. if (argc < 2) {
  297. error.print("too few arguments : 1 required, 0 given\n");
  298. return EXIT_FAILURE;
  299. }
  300. if (argc > 2) {
  301. error.print("too many arguments : 1 required, %d given\n", argc - 1);
  302. return EXIT_FAILURE;
  303. }
  304. //Supprime de la shm
  305. if (!remove_local_data(argv[1])) {
  306. error.print("Erreur interne impossible de supprimer la variable\n");
  307. return EXIT_FAILURE;
  308. }
  309. return EXIT_SUCCESS;
  310. }