Skip to content

Commit

Permalink
remplissage de la structure de base
Browse files Browse the repository at this point in the history
  • Loading branch information
JT117 committed Dec 26, 2011
1 parent d17f0da commit 1c9e543
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 28 deletions.
Binary file modified cmd
Binary file not shown.
129 changes: 119 additions & 10 deletions cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,135 @@ void parse_args( cmd * c )
{
int j = 0;

char* copie = (char*)malloc( strlen( (c->membres_cmd[i] )+1) * sizeof( char ) );
strcpy( copie, c->membres_cmd[i] );

c->cmd_args[i] = (char**)malloc( 1 * sizeof( char* ) );
pch = strtok ( c->membres_cmd[i]," ");
pch = strtok ( copie," ");

while (pch != NULL)
while (pch != NULL )
{
c->cmd_args[i][j] = (char*)malloc( 250 * sizeof( char ) );
strcpy( c->cmd_args[i][j], pch );
if( strcmp( pch, ">") != 0 && strcmp( pch, "<") != 0 && strcmp( pch, ">>") != 0) // on analyse pas si redirection cela sera fait dans parse redirection
{
c->cmd_args[i][j] = (char*)malloc( 250 * sizeof( char ) );
strcpy( c->cmd_args[i][j], pch );

printf ("%s\n",pch);
pch = strtok (NULL, " ");
j++;
printf ("%s\n",pch);
pch = strtok (NULL, " ");
j++;

if( pch != NULL )
if( pch != NULL )
{
c->cmd_args[i] = (char**)realloc( c->cmd_args[i], j * sizeof( char* ) );
}
}
else
{
c->cmd_args[i] = (char**)realloc( c->cmd_args[i], j * sizeof( char* ) );
pch = NULL;
}
}
c->nb_args_membres[i] = j;

free( copie );
}

}

void parse_membres( char* chaine, cmd* cmd ) // Attention la fonction detruit l'argument chaine
{
char* pch;
unsigned int i = 0;

cmd->initial_cmd = (char*)malloc( strlen(chaine) * sizeof( char ) ); //Allocation de la chaine initial_cmd de la structure CMD
strcpy( cmd->initial_cmd, chaine ) ; //Copie de la commande initial dans la structure

pch = strtok ( chaine, "|" );

while (pch != NULL)
{
i++;
cmd->membres_cmd = realloc( cmd->membres_cmd, i * sizeof( char* ));
cmd->membres_cmd[i-1] = (char*)malloc( strlen( pch ) * sizeof( char ));
strcpy( cmd->membres_cmd[i-1], pch );

pch = strtok ( NULL, "|" );
}

cmd->nb_membres = i;
cmd->nb_args_membres = (int*)malloc( cmd->nb_membres * sizeof( int ) ); // initialisation du tableau d'entier nb_args_membres

cmd->redir = (char***)malloc( cmd->nb_membres * sizeof( char** ) );
cmd->type_redir = (int**)malloc( cmd->nb_membres * sizeof( int* ));

for( i = 0; i < cmd->nb_membres; i++ )
{
cmd->redir[i] = (char**)malloc( 3 * sizeof( char* ) );

cmd->redir[i][STDIN] = (char*)malloc( 250 * sizeof( char ) );
cmd->redir[i][STDOUT] = (char*)malloc( 250 * sizeof( char ) );
cmd->redir[i][STDERR] = (char*)malloc( 250 * sizeof( char ) );

cmd->type_redir[i] = (int*)malloc( 3 * sizeof( int ));

cmd->type_redir[i][STDIN] = -1;
cmd->type_redir[i][STDOUT] = -1;
cmd->type_redir[i][STDERR] = -1;
}
}

int parse_redir( unsigned int i, cmd* cmd )
{
char* pch = NULL;
int cas = 0;

char* copie = (char*)malloc( strlen( (cmd->membres_cmd[i] )+1) * sizeof( char ) );
strcpy( copie, cmd->membres_cmd[i] );

pch = strtok( copie, " " );

while( pch != NULL ) // au besin mettre un NULL ou valeur special dans les autres case de la redirection
{
if( cas == 1)
{
strcpy( cmd->redir[i][STDOUT], pch );
printf( "Redirection sortie standard .%s.\n", cmd->redir[i][STDOUT] );
}
else if( cas == 2 )
{
strcpy( cmd->redir[i][STDIN], pch );
printf( "Redirection entrée standard .%s.\n", cmd->redir[i][STDIN] );
}
else if( cas == 3 )
{
strcpy( cmd->redir[i][STDOUT], pch );
cmd->type_redir[i][STDOUT] = RAPPEND;
printf( "Redirection et append sortie standard .%s.\n", cmd->redir[i][STDOUT] );
printf( "%d\n", cmd->type_redir[i][STDOUT] );
}

if( strcmp( pch, ">") == 0 )
{
cas = 1;
}
else if( strcmp( pch, "<") == 0 )
{
cas = 2;
}
else if( strcmp( pch, ">>") == 0 )
{
cas = 3;
}

pch = strtok( NULL, " " );

}

free( copie );
}








7 changes: 5 additions & 2 deletions cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#include <stdio.h>
#include <stdlib.h>

enum{ STDIN = 0, STDOUT = 1, STDERR = 2 };
enum{ RAPPEND = 3 };

typedef struct
{
char *initial_cmd; // Chaine initiale tapee par l'utilisateur
Expand All @@ -16,9 +19,9 @@ typedef struct
void aff_args( cmd* c ); // ToDo
void free_args( cmd* c ); // ToDo
void parse_args( cmd* c );
void parse_membres( char* chaine, cmd* ma_cmd ); // ToDo
void parse_membres( char* chaine, cmd* ma_cmd );
void aff_membres( cmd* ma_cmd ); // ToDo
void free_membres( cmd* ma_cmd ); // ToDo
int parse_redir( unsigned int i, cmd* c ); // ToDo
int parse_redir( unsigned int i, cmd* cmd );
void free_redir( cmd* c );
void aff_redir( cmd c, int i ); // ToDo
33 changes: 17 additions & 16 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
#include <stdlib.h>
#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>

#include "cmd.h"

int main( int argc, const char* argv[] )
{
while(1)
{
char commande[500];

/*while(1)
{*/
char* commande;
int i = 0;
cmd* cmd = malloc( 1 * sizeof( cmd ) );
cmd->nb_membres = 1;
cmd->membres_cmd = (char**)malloc( cmd->nb_membres * sizeof( char* ) );
cmd->membres_cmd[0] = (char*) malloc( 250 * sizeof( char ) );
cmd->nb_args_membres = (int*)malloc( cmd->nb_membres * sizeof( int ) );

printf("Veuillez entrer votre commande\n");
scanf( "%s", commande );

cmd->initial_cmd = (char*)malloc( 1 * sizeof( commande ) ); //Allocation de la chaine initial_cmd de la structure CMD
strcpy( cmd->initial_cmd, commande ) ; //Copie du tampon dans la chaine allouée
strcpy( cmd->membres_cmd[0], cmd->initial_cmd );

commande = readline( "Veuillez entrer votre commande\n" );

parse_membres( commande, cmd );
parse_args( cmd );

for( i = 0; i < cmd->nb_membres; i++ )
{
parse_redir( i, cmd );
}

free( commande );
//free cmd and co
}
//flush le clavier
//}
}

0 comments on commit 1c9e543

Please sign in to comment.