[1350] | 1 | /* |
---|
| 2 | MEDERNACH Emmanuel |
---|
| 3 | Aug 8, 2000 |
---|
| 4 | */ |
---|
| 5 | |
---|
| 6 | #include <stdio.h> |
---|
| 7 | #include <unistd.h> |
---|
| 8 | #include <sys/types.h> |
---|
| 9 | #include <sys/wait.h> |
---|
| 10 | |
---|
| 11 | /* |
---|
| 12 | TODO: |
---|
| 13 | Add support for 'error management' |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | pid_t wait(int *status) ; |
---|
| 17 | |
---|
| 18 | void read_choice(int *choice) |
---|
| 19 | { |
---|
| 20 | |
---|
| 21 | if (choice == NULL) return ; |
---|
| 22 | (*choice) = -1; |
---|
| 23 | |
---|
| 24 | printf("Possible volumes are :\n"); |
---|
| 25 | printf("1: BOX\n"); |
---|
| 26 | printf("2: TUBS\n"); |
---|
| 27 | printf("3: CONE\n"); |
---|
| 28 | printf("4: PCON\n"); |
---|
| 29 | printf("5: PGON\n"); |
---|
| 30 | printf("6: PGON2\n"); |
---|
| 31 | printf("7: CONE2 (Emm)\n"); |
---|
| 32 | printf("8: THINBOX (Emm)\n"); |
---|
| 33 | while (((*choice) < 1) || ((*choice) > 8)) |
---|
| 34 | { |
---|
| 35 | scanf("%d",choice); |
---|
| 36 | } |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | void write_ini(const char *init,int choice,double x,double y,double z,double dx,double dy,double dz) |
---|
| 40 | { |
---|
| 41 | FILE *fi= NULL; |
---|
| 42 | |
---|
| 43 | fi = fopen(init,"w"); |
---|
| 44 | if (fi == NULL) |
---|
| 45 | { |
---|
| 46 | perror("Some problems to write in the Init.fred file"); |
---|
| 47 | exit (1); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | switch (choice) |
---|
| 51 | { |
---|
| 52 | case 1: |
---|
| 53 | fprintf(fi,"/fred/volume BOX\n"); |
---|
| 54 | break; |
---|
| 55 | case 2: |
---|
| 56 | fprintf(fi,"/fred/volume TUBS\n"); |
---|
| 57 | break; |
---|
| 58 | case 3: |
---|
| 59 | fprintf(fi,"/fred/volume CONE\n"); |
---|
| 60 | break; |
---|
| 61 | case 4: |
---|
| 62 | fprintf(fi,"/fred/volume PCON\n"); |
---|
| 63 | break; |
---|
| 64 | case 5: |
---|
| 65 | fprintf(fi,"/fred/volume PGON\n"); |
---|
| 66 | break; |
---|
| 67 | case 6: |
---|
| 68 | fprintf(fi,"/fred/volume PGON2\n"); |
---|
| 69 | break; |
---|
| 70 | case 7: |
---|
| 71 | fprintf(fi,"/fred/volume CONE2\n"); |
---|
| 72 | break; |
---|
| 73 | case 8: |
---|
| 74 | fprintf(fi,"/fred/volume THINBOX\n"); |
---|
| 75 | break; |
---|
| 76 | default: |
---|
| 77 | fprintf(stderr,"%d is not between 1 and 8\n",choice); |
---|
| 78 | exit(2); |
---|
| 79 | break; |
---|
| 80 | } |
---|
| 81 | fprintf(fi,"/fred/gun G4\n"); |
---|
| 82 | fprintf(fi,"/gun/position %f %f %f\n",x,y,z); |
---|
| 83 | fprintf(fi,"/gun/direction %f %f %f\n",dx,dy,dz); |
---|
| 84 | fprintf(fi,"/run/initialize \n"); |
---|
| 85 | fprintf(fi,"/run/beamOn\n"); |
---|
| 86 | fclose(fi); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | int main () |
---|
| 90 | { |
---|
| 91 | pid_t son = -1; |
---|
| 92 | int status = 0; |
---|
| 93 | char init[]="Init.fred"; |
---|
| 94 | char *param [3]; |
---|
| 95 | |
---|
| 96 | int choice = -1 ; |
---|
| 97 | double posx,posy,posz; |
---|
| 98 | double dirx,diry,dirz; |
---|
| 99 | |
---|
| 100 | double startx,starty,startz; |
---|
| 101 | double beginx,beginy,beginz; |
---|
| 102 | double endx,endy,endz; |
---|
| 103 | double deltax,deltay,deltaz; |
---|
| 104 | double begindirx,enddirx,stepdirx; |
---|
| 105 | double begindiry,enddiry,stepdiry; |
---|
| 106 | double begindirz,enddirz,stepdirz; |
---|
| 107 | |
---|
| 108 | int TakeOtherPoint = 0; |
---|
| 109 | |
---|
| 110 | |
---|
| 111 | /* write an init file */ |
---|
| 112 | |
---|
| 113 | read_choice(&choice); |
---|
| 114 | |
---|
| 115 | |
---|
| 116 | posx = posy = posz = 0; |
---|
| 117 | dirx = diry = dirz = 1; |
---|
| 118 | |
---|
| 119 | |
---|
| 120 | printf("You could choose a delta <= 0 to have only a plane\n"); |
---|
| 121 | /* rotation in a cone for example could be performed */ |
---|
| 122 | /* to have only a y=0 plane to search */ |
---|
| 123 | printf("Choose a beginning, an end and a step for x (0 200 10)\n"); |
---|
| 124 | scanf("%lf %lf %lf",&beginx,&endx,&deltax); |
---|
| 125 | printf("Choose a beginning, an end and a step for y (0 200 10)\n"); |
---|
| 126 | scanf("%lf %lf %lf",&beginy,&endy,&deltay); |
---|
| 127 | printf("Choose a beginning, an end and a step for z (0 200 10)\n"); |
---|
| 128 | scanf("%lf %lf %lf",&beginz,&endz,&deltaz); |
---|
| 129 | |
---|
| 130 | /* you could want to continue after a previous crash |
---|
| 131 | (X could hangs if swap is full) |
---|
| 132 | */ |
---|
| 133 | |
---|
| 134 | printf("you could want to continue after a previous crash\n"); |
---|
| 135 | printf("In this case just indicate from where do you want to start (x y z)\n\n"); |
---|
| 136 | scanf("%lf %lf %lf",&startx,&starty,&startz); |
---|
| 137 | |
---|
| 138 | printf("you could now have a grid of direction to point\n"); |
---|
| 139 | printf("Choose a beginning, an end and a step for all direction (-30 30 1)\n"); |
---|
| 140 | printf("\nfor direction x : "); |
---|
| 141 | scanf("%lf %lf %lf",&begindirx,&enddirx,&stepdirx); |
---|
| 142 | printf("\nfor direction y : "); |
---|
| 143 | scanf("%lf %lf %lf",&begindiry,&enddiry,&stepdiry); |
---|
| 144 | printf("\nfor direction z : "); |
---|
| 145 | scanf("%lf %lf %lf",&begindirz,&enddirz,&stepdirz); |
---|
| 146 | |
---|
| 147 | /* we want also != step for each direction */ |
---|
| 148 | |
---|
| 149 | posx = startx ; |
---|
| 150 | posy = starty ; |
---|
| 151 | posz = startz ; |
---|
| 152 | |
---|
| 153 | |
---|
| 154 | printf("We start at %f %f %f\n",posx,posy,posz); |
---|
| 155 | |
---|
| 156 | while (posx < endx) |
---|
| 157 | { /* loop on x */ |
---|
| 158 | |
---|
| 159 | printf("Posx = %f\n",posx); |
---|
| 160 | while (posy < endy) |
---|
| 161 | { /* loop on y */ |
---|
| 162 | |
---|
| 163 | printf("Posy = %f\n",posy); |
---|
| 164 | while (posz < endz) |
---|
| 165 | { /* loop on z */ |
---|
| 166 | |
---|
| 167 | printf("\tPosz = %f\n",posz); |
---|
| 168 | TakeOtherPoint = 0; |
---|
| 169 | for (dirx=begindirx;(dirx<enddirx) && (TakeOtherPoint == 0);dirx+=stepdirx) |
---|
| 170 | for (diry=begindiry;(diry<enddiry) && (TakeOtherPoint == 0);diry+=stepdiry) |
---|
| 171 | for (dirz=begindirz;(dirz<enddirz) && (TakeOtherPoint == 0);dirz+=stepdirz) |
---|
| 172 | { |
---|
| 173 | write_ini(init,choice,posx,posy,posz,dirx,diry,dirz); |
---|
| 174 | |
---|
| 175 | /* call to Fred */ |
---|
| 176 | son = fork(); |
---|
| 177 | switch(son) |
---|
| 178 | { |
---|
| 179 | case -1: |
---|
| 180 | perror("Fork process error"); |
---|
| 181 | exit(1); |
---|
| 182 | break; |
---|
| 183 | case 0: |
---|
| 184 | /* execvp (Emm)fred with init file */ |
---|
| 185 | param[0] = "fred"; |
---|
| 186 | param[1] = init; |
---|
| 187 | param[2] = NULL; |
---|
| 188 | |
---|
| 189 | /* no output nor input */ |
---|
| 190 | close (0); |
---|
| 191 | close (1); |
---|
| 192 | close (2); |
---|
| 193 | |
---|
| 194 | if (execvp("fred",param) == -1) |
---|
| 195 | { |
---|
| 196 | fprintf(stderr,"fred program not found ..\n"); |
---|
| 197 | exit(1); |
---|
| 198 | } |
---|
| 199 | break; |
---|
| 200 | default: |
---|
| 201 | /* wait for the other process */ |
---|
| 202 | waitpid(son,&status,0); |
---|
| 203 | if (WIFEXITED(status) == 0) |
---|
| 204 | { |
---|
| 205 | /* some errors */ |
---|
| 206 | /* keep the current init */ |
---|
| 207 | fprintf(stderr,"I catch an error : bad return value %f %f %f %f %f %f\n",posx,posy,posz,dirx,diry,dirz); |
---|
| 208 | /* we take another point */ |
---|
| 209 | /* Ok this is not really the way to do ..*/ |
---|
| 210 | TakeOtherPoint = 1; |
---|
| 211 | } |
---|
| 212 | else |
---|
| 213 | if (WIFSIGNALED(status)) |
---|
| 214 | { |
---|
| 215 | /* some errors */ |
---|
| 216 | /* keep the current init */ |
---|
| 217 | fprintf(stderr,"I catch an error : Signal terminated %f %f %f %f %f %f\n",posx,posy,posz,dirx,diry,dirz); |
---|
| 218 | /* we take another point */ |
---|
| 219 | /* Ok this is not really the way to do ..*/ |
---|
| 220 | TakeOtherPoint = 1; |
---|
| 221 | } |
---|
| 222 | break; |
---|
| 223 | } |
---|
| 224 | } |
---|
| 225 | /* loop on z */ |
---|
| 226 | posz += deltaz ; |
---|
| 227 | } |
---|
| 228 | posz = beginz ; |
---|
| 229 | |
---|
| 230 | /* loop on y */ |
---|
| 231 | posy += deltay ; |
---|
| 232 | } |
---|
| 233 | posy = beginy ; |
---|
| 234 | |
---|
| 235 | /* loop on x */ |
---|
| 236 | posx += deltax ; |
---|
| 237 | } |
---|
| 238 | posx = beginx ; |
---|
| 239 | |
---|
| 240 | } |
---|
| 241 | |
---|