|
@@ -114,7 +114,6 @@ int get_command(Command* c, char* line){
|
|
|
c->error = STDERR;
|
|
|
c->erase[0] = false;
|
|
|
c->erase[1] = false;
|
|
|
- c->bck = false;
|
|
|
//Trim et supprime l'ancienne chaine
|
|
|
old = c->cmd;
|
|
|
c->cmd = rtrim(c->cmd, ' ');
|
|
@@ -339,7 +338,8 @@ int set_redirection(Command* c){
|
|
|
buffer++;
|
|
|
}
|
|
|
//Allocation file et copie nom fichier
|
|
|
- file = malloc(sizeof(char) * compteur);
|
|
|
+ file = malloc(sizeof(char) * compteur);
|
|
|
+ memset(file, 0, compteur);
|
|
|
strncpy(file, deb, compteur);
|
|
|
//Redirection
|
|
|
if(set_io(c, file, redir) == SHELL_ERR){
|
|
@@ -356,6 +356,90 @@ int set_redirection(Command* c){
|
|
|
return finCmd;
|
|
|
}
|
|
|
|
|
|
+int split_command(Command* c, char* cmd){
|
|
|
+ printf("%s\n", cmd);
|
|
|
+ //Declaration variable
|
|
|
+ char* deb = cmd;
|
|
|
+ int nb = 0, length = 0, i = 0;
|
|
|
+ char delim = ' ';
|
|
|
+ boolean chercheFin = false;
|
|
|
+ //Compte le nombre d'argument
|
|
|
+ while(*cmd != '\0' && *cmd != '&'){
|
|
|
+ //Cherche debut d'un mot
|
|
|
+ if(*cmd != ' ' && !chercheFin){
|
|
|
+ chercheFin = true;
|
|
|
+ //Guillemet
|
|
|
+ if(*cmd == '"'){
|
|
|
+ //Verif que ce n'est pas la fin
|
|
|
+ if(!(*(cmd + 1))){
|
|
|
+ return SHELL_ERR;
|
|
|
+ }
|
|
|
+ cmd++;
|
|
|
+ delim = '"';
|
|
|
+ }
|
|
|
+ //Mot sans guillemet autour
|
|
|
+ else {
|
|
|
+ length = 0;
|
|
|
+ delim = ' ';
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //Fin d'un mot
|
|
|
+ else if(*cmd == delim && chercheFin){
|
|
|
+ //Adapte si il y avait des guillemet
|
|
|
+ chercheFin = false;
|
|
|
+ nb++;
|
|
|
+ }
|
|
|
+ //Incremente
|
|
|
+ cmd++;
|
|
|
+ }
|
|
|
+ //Si on termine sur un mot sans "
|
|
|
+ if(chercheFin){
|
|
|
+ nb++;
|
|
|
+ }
|
|
|
+ //Allocation + retour au debut
|
|
|
+ c->argv = malloc(sizeof(char*) * (nb + 1));
|
|
|
+ c->argc = nb;
|
|
|
+ cmd = deb;
|
|
|
+ //Parcours chaine et decoupe
|
|
|
+ while(*cmd != '\0' && *cmd != '&'){
|
|
|
+ //Cherche debut d'un mot
|
|
|
+ if(*cmd != ' ' && !chercheFin){
|
|
|
+ chercheFin = true;
|
|
|
+ //Guillemet
|
|
|
+ if(*cmd == '"'){
|
|
|
+ cmd++;
|
|
|
+ deb = cmd;
|
|
|
+ length = 0;
|
|
|
+ delim = '"';
|
|
|
+ }
|
|
|
+ //Mot sans guillemet autour
|
|
|
+ else {
|
|
|
+ deb = cmd;
|
|
|
+ length = 0;
|
|
|
+ delim = ' ';
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //Fin d'un mot
|
|
|
+ else if(*cmd == delim && chercheFin){
|
|
|
+ chercheFin = false;
|
|
|
+ //Recup le mot
|
|
|
+ c->argv[i] = malloc(sizeof(char) * length);
|
|
|
+ strncpy(c->argv[i++], deb, length);
|
|
|
+ }
|
|
|
+ //Incremente
|
|
|
+ cmd++;
|
|
|
+ length++;
|
|
|
+ }
|
|
|
+ //Recup le dernier mot si besoin
|
|
|
+ if(chercheFin){
|
|
|
+ c->argv[i] = malloc(sizeof(char) * length);
|
|
|
+ strncpy(c->argv[i++], deb, length);
|
|
|
+ }
|
|
|
+ //Set la dernière case du tableau à null
|
|
|
+ c->argv[i] = NULL;
|
|
|
+ return SHELL_OK;
|
|
|
+}
|
|
|
+
|
|
|
/* --- Fonctions publiques --- */
|
|
|
int parse_line(CommandTab* ct, char* line){
|
|
|
//Declaration variable
|
|
@@ -370,6 +454,7 @@ int parse_line(CommandTab* ct, char* line){
|
|
|
//Initialisation structure
|
|
|
ct->cmd = malloc(sizeof(Command*) * compteur);
|
|
|
ct->length = compteur;
|
|
|
+ ct->bck = line[strlen(line) - 1] == '&';
|
|
|
//Recupération de chaque commande
|
|
|
for(int i = 0; i < compteur; i++){
|
|
|
ct->cmd[i] = malloc(sizeof(Command));
|
|
@@ -390,14 +475,14 @@ int parse_command(Command* c){
|
|
|
char* cmd;
|
|
|
//Parse les redirections
|
|
|
length = set_redirection(c);
|
|
|
- if(length == SHELL_ERR){
|
|
|
+ if(length == SHELL_ERR || length == 0){
|
|
|
return SHELL_ERR;
|
|
|
}
|
|
|
//Recup la commande (sans redirection)
|
|
|
- cmd = malloc(length);
|
|
|
+ cmd = malloc(sizeof(char) * length);
|
|
|
strncpy(cmd, c->cmd, length);
|
|
|
- //Split en un tableau
|
|
|
- c->argv = str_split(cmd, ' ', &c->argc); //ToDo changer par un methode de split qui prend en compte les " " et les \espaces
|
|
|
+ //Split la commande
|
|
|
+ split_command(c, cmd);
|
|
|
c->name = c->argv[0];
|
|
|
//Analyse wildcard
|
|
|
/* Todo */
|
|
@@ -424,6 +509,8 @@ void clean_command(CommandTab* ct){
|
|
|
if(ct->cmd[i]->argc > 0){
|
|
|
ct->cmd[i]->name = NULL;
|
|
|
for(int j = 0; j < ct->cmd[i]->argc; j++){
|
|
|
+ printf("free argv %d : %s\n", j, ct->cmd[i]->argv[j]);
|
|
|
+ fflush(stdout);
|
|
|
free(ct->cmd[i]->argv[j]);
|
|
|
}
|
|
|
}
|