1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/wait.h>
#include <signal.h>
int resCommande =0;
int resp=0;
char resP[];
int status;
char com[];
int pid;
int status;
char *delimiteurs=";&<>|";
char mot[50];
//char c;
char param;
int symboleP;
int execute;
int lettre=0;
int parsing(){
char c;
int i=0;
int cmot=0;
fprintf(stderr,"debut parsing\n");
//if(eof == 1){printf("DAUPHINE> ");fflush(stdout);eof=0;}
while(1){
c = getchar();
fprintf(stderr,"debut while\n");
//if(xc == c && c == 10){eof=1;fprintf(stderr,"car double\n");return;}
fprintf(stderr,"cartographie-lu %d %c\n",c,c);
if (c == '\n') {symboleP = 0;return 0;}
else if (c == ';') {symboleP = 1;return 1;}
else if (c == '&') {symboleP = 2;return 2;}
else if (c == '<') {symboleP = 3;return 3;}
else if (c == '>') {symboleP = 4;return 4;}
else if (c == '|') {symboleP = 5;return 5;}
//else if (c == ' ') {symboleP = 6;return;}
else if (c == EOF) {symboleP = 7;return 7;}
else if (c != ' ') {
fprintf(stderr,"debut second while\n");
// on rentre ds une seq.
symboleP = 10;
//fprintf(stderr,"car-lu 10\n");
while(c != '\n' && !strchr(delimiteurs,c)){
i=0;
fprintf(stderr,"debut second while\n");
while(c != 32 ){
fprintf(stderr,"debut troisième while\n");
if((c != '\n') && !strchr(delimiteurs,c)){
fprintf(stderr,"debut if\n");
//fprintf(stderr,"boucle\n");
mot[i]=c;i++;
c=getchar();/*fprintf(stderr,"val carac %d\n",c);*/
}
//fprintf(stderr,"fin de mot\n");
else {/*fprintf(stderr,"break\n");*/break;}
}
break;
}
while(c == ' '){c=getchar();}com[
ungetc(c,stdin);
mot[i]='\0';
resP[cmot++]=strdup(mot);
fprintf(stderr,"element comm lue %s %s\n",resP[0],resP[1]);
if(c == '\n' || strchr(delimiteurs,c)){resP[cmot]=0;if(c!='\n')ungetc(c,stdin);return 10;}
}
}// attention on a consommé le delimiteur
fprintf(stderr,"elt comm lue %s %s\n",resP[0],resP[1]);
}
int commande(int fin,int fout,char * com, char * param, int *bg){
int s = parsing();
fprintf(stderr,"résultat parsing %d\n",s);
switch(symboleP){
case 0: // NL
// exécution commande ?com[
pid=fork();
if(pid==0){
resCommande = 2;
execvp(resP[0],resp);
}else{
if(bg==0){ //pas de bg on attend le fils
waitpid(pid, NULL, 0);
}
execute=1;
}
break;
// exécution commande ? res= ?
case 1: // ;
case 2: // &
case 3: // <
case 4: // >
case 5: // |
case 7: // EOF
case 10:
p=fork();
if(p==0){ //fils
//pas de redirection
printf("truc1");
execvp(resP[0], resP);
}else{ //pere
if(bg==0){ //pas de bg on attend le fils
waitpid(p, NULL, 0);
}
}
/*eltsCommande[cmot++]=strdup(mot);
/*execvp(mot[]); // mot
execvp(mot)?*/
default:
printf(" ");
}
return 0 ;
}
int main(int argc, char* argv[]) {
int fmess = open("imp", O_WRONLY|O_TRUNC|O_CREAT, 0640);
close(2);
dup(fmess);
close(fmess);
char *com[20];
int status;
int bg;
//int eof= 0;
printf("DAUPHINE> ");
fflush(stdout);
while(1){
if(resCommande ==2){
resCommande = 0;
//eof = 0;
printf("DAUPHINE> ");
fflush(stdout);
}
else{
commande(0,1,&com,¶m,&bg);//c est ou param?
}
return 0;
}
} |
Partager