123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- /*
- * File: mysh.c
- * Author: Arthur Brandao
- *
- * Created on 31 octobre 2018, 12:43
- */
- #define _POSIX_C_SOURCE 2
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <wait.h>
- #include "error.h"
- #include "str.h"
- #include "parser.h"
- #include "command.h"
- #include "execute.h"
- #include "mysh.h"
- /* --- Extern --- */
- extern Error error;
- extern boolean exitsh;
- /* --- Global --- */
- pid_list pidlist;
- pid_node* active = NULL;
- /* --- Fonctions privées --- */
- void test_write() {
- char* a = "azerty\n";
- int tmp = write(1, a, strlen(a));
- printf("%d\n", tmp);
- }
- void test_tmp_file(){
- int a;
- FILE* f = tmpfile();
- printf("F : %d\n", f == NULL);
- int fd = fileno(f);
- printf("%d : %ld\n", fd, write(fd, "bonjour", 8));
- a = lseek(fd, 0L, SEEK_SET);
- sleep(2);
- char buf[10];
- memset(buf, 0 , 10);
- a = read(fd, buf, 10);
- printf("%d : %s\n", a, buf);
- close(fd);
- }
- void test(){
- CommandTab ct;
- char str[BUFFER_SIZE];
- int a;
- //Initialisation structures
- error_finit("mysh.log");
- ini_pid_list(&pidlist);
-
- //Recup ligne
- //printf("%s\n", fgets(str, 500, stdin));&
- memset(str, 0, 500);
- a = read(STDIN, str, 500);
- printf("%s\n", str);
- //Separe les commandes
- a = parse_line(&ct, str);
- if(a == SHELL_ERR){
- addserror("Erreur lors du parse de la ligne");
- error.exit_err();
- }
- //Parse les commandes
- a = parse_all_command(&ct);
- if(a == SHELL_FAIL){
- addserror("Erreur lors du parse des commandes");
- error.exit_err();
- }
- //Affiche resultat
- for (int i = 0; i < ct.length; i++) {
- Command* c = ct.cmd[i];
- printf("Commande %d (%s) : \n", i, c->name);
- for (int j = 0; j < c->argc; j++) {
- printf(" Argument %d : %s\n", j, c->argv[j]);
- }
- printf("Commande en fond : %d\n\n", ct.bck);
- //Si c'est une commande interne on l'execute
- if(is_internal_cmd(ct.cmd[i]->name)){
- show_current_dir(NULL, "\n");
- printf("Result : %d\n", launch_internal_command(ct.cmd[i]));
- show_current_dir(NULL, "\n");
- }
- }
- //Nettoyage structures
- clean_command(&ct);
- clean_pid(&pidlist);
- error.exit();
- }
- /* --- Fonctions utilitaires --- */
- void show_current_dir(const char* before, const char* after) {
- char buffer[BUFFER_SIZE];
- if (getcwd(buffer, sizeof (buffer)) == NULL) {
- addperror("Erreur getcwd()");
- } else {
- if(before == NULL && after == NULL){
- printf("%s", buffer);
- } else if(before == NULL){
- printf("%s%s", buffer, after);
- } else if(after == NULL){
- printf("%s%s", before, buffer);
- } else {
- printf("%s%s%s", before, buffer, after);
- }
- }
- fflush(stdout);
- }
- int get_line(char* buffer){
- memset(buffer, 0, BUFFER_SIZE);
- if(read(STDIN, buffer, BUFFER_SIZE) == ERR){
- addperror("Impossible de lire dans STDIN");
- return SHELL_ERR;
- }
- return SHELL_OK;
- }
- int get_tmp_file(){
- FILE* f = tmpfile();
- if(f == NULL){
- adderror("Impossible de créer un fichier temporaire");
- return SHELL_ERR;
- }
- return fileno(f);
- }
- /* --- Main --- */
- int main(int argc, char* argv[]) {
- //Declaration variables
- CommandTab ct;
- int result;
- char line[BUFFER_SIZE], before[BUFFER_SIZE];
- //Initialisation structures
- error_finit("mysh.log");
- ini_pid_list(&pidlist);
- //Preparation affichage
- sprintf(before, "\x1b[32m%s:\x1b[36m", getlogin());
- //Boucle infini de lecture
- while(!exitsh){
- //Affichage repertoire
- show_current_dir(before, ">\x1b[0m ");
- //Lecture ligne
- if(get_line(line) == SHELL_ERR){
- error.print("Impossible de lire les commandes\n");
- clean_pid(&pidlist);
- error.exit_err();
- }
- //Parse la ligne et commandes
- result = parse_line(&ct, line);
- if(result == SHELL_ERR){
- error.print("Impossible d'analyser la ligne\n");
- addserror("Erreur lors du parse de la ligne");
- continue;
- }
- //Si aucune commande on passe
- printf("%d\n", ct.length);
- if(ct.length == 0){
- clean_command(&ct);
- continue;
- }
- //Parse les commandes
- result = parse_all_command(&ct);
- if(result == SHELL_FAIL){
- error.print("Impossible d'analyser la commande\n");
- addserror("Erreur lors du parse des commandes");
- continue;
- }
- //Execute
- result = run(ct);
- printf("Result : %d\n", result);
- //Vide le resultat du parse de la ligne de commande
- clean_command(&ct);
- }
- //Nettoyage
- clean_pid(&pidlist);
- error.end();
- return EXIT_SUCCESS;
- }
- int run(CommandTab ct){
- pid_t pid;
- int result = 0;
- //Si en fond creation d'un fork pour executer les commandes
- printf("bck : %d\n", ct.bck);
- if(ct.bck && false/*ToDo - Test plus tard*/){
- pid = fork();
- if(pid == ERR){
- addperror("Erreur lors du fork pour l'execution des commandes");
- error.print("Erreur systeme, impossible de continuer\n");
- return SHELL_ERR;
- }
- //Fils
- if(pid == 0){
- ct.bck = 0;
- result = run(ct);
- if(result == SHELL_FAIL){
- exit(EXIT_FAILURE);
- }
- exit(EXIT_SUCCESS);
- }
- //Ajout du fils dans la liste des pid
- add_pid(&pidlist, pid);
- //Pour le pere c'est fini
- return SHELL_OK;
- }
- //Sinon execution de chaque commande
- Command* c;
- int tube[ct.length][2];
- int tubepos = 0;
- int infd = -1, outfd = -1, errfd = -1;
- boolean bpipe = false, nextpipe = false;
- //Parcours les commandes
- for(int i = 0; i < ct.length; i++){
- c = ct.cmd[i];
- printf("Cmd : %s\n", c->name);
- //Si pipe avant
- if(nextpipe){
- //Si fin chaine pipe
- if(c->next != SHELL_PIPE){
- nextpipe = false;
- }
- //Passe la commande
- continue;
- }
- //Si pipe creation d'un fichier commun
- nextpipe = c->next == SHELL_PIPE ;
- if(c->next == SHELL_PIPE && c->output == STDOUT){
- bpipe = true;
- //Creation tube
- if(pipe(tube[tubepos]) == ERR){
- addperror("Impossible de créer un tube");
- return SHELL_FAIL;
- }
- //Redirection
- c->output = tube[tubepos][TUBE_ECRITURE];
- if(ct.cmd[i + 1]->input == STDIN){
- ct.cmd[i + 1]->input = tube[tubepos][TUBE_LECTURE];
- }
- }
- //Effectue les redirections IO
- if(c->input != STDIN){
- infd = redirect_fd2(STDIN, c->input);
- if(infd == ERR){
- return SHELL_FAIL;
- }
- }
- if(c->output != STDOUT){
- outfd = redirect_fd2(STDOUT, c->output);
- if(outfd == ERR){
- return SHELL_FAIL;
- }
- }
- if(c->error != STDERR){
- errfd = redirect_fd2(STDERR, c->error);
- if(errfd == ERR){
- return SHELL_FAIL;
- }
- }
- //Execute la commande
- if(is_internal_cmd(c->name)){
- result = launch_internal_command(c);
- } else if(is_executable_file(c->name)){
- result = exec_file(c->name, c->argv);
- } else {
- result = exec_shell(c->name, c->argv);
- }
- printf("%d\n", result);
- //Reset IO
- if(c->input != STDIN){
- infd = redirect_fd(STDIN, infd);
- if(infd == ERR){
- return SHELL_FAIL;
- }
- }
- if(c->output != STDOUT){
- outfd = redirect_fd(STDOUT, outfd);
- if(outfd == ERR){
- return SHELL_FAIL;
- }
- }
- if(c->error != STDERR){
- errfd = redirect_fd(STDERR, errfd);
- if(errfd == ERR){
- return SHELL_FAIL;
- }
- }
- //Fermeture tube
- if(bpipe){
- bpipe = false;
- if(close(outfd) == ERR){
- addperror("Impossible de fermer tube ecriture");
- return SHELL_FAIL;
- }
- c->output = STDOUT;
- }
- //Agit en fonction de la jointure avec la prochaine commande
- /*ToDo*/
- }
- if(result != EXIT_SUCCESS){
- return SHELL_FAIL;
- }
- return SHELL_OK;
- }
|