command.c 10 KB

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