command.c 10 KB

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