| 1 | #include <signal.h> |
| 2 | /* EFL-Ratfor-Fortran command */ |
| 3 | |
| 4 | char *setsuf(),*copy(); |
| 5 | extern int fin, fout; |
| 6 | char ts[4000]; |
| 7 | char *tsp = ts; |
| 8 | char *av[500]; |
| 9 | char *rlist[500]; |
| 10 | int nr = 0; |
| 11 | char *llist[500]; |
| 12 | int nl = 0; |
| 13 | int nxo = 0; |
| 14 | int bdcount = 0; /* count block data files generated */ |
| 15 | int rflag = 0; /* Ratfor or EFL ony, no compile */ |
| 16 | int dflag = 0; /* Compile EFL DEBUG statements if set */ |
| 17 | int tflag = 0; /* Trace operation of command if set */ |
| 18 | int vflag = 1; /* Verify files compiled if set */ |
| 19 | int mflag = 0; /* Ratfor macro pre-pass if set */ |
| 20 | int fflag = 0; /* Save Fortran intermediate files if set */ |
| 21 | int cflag = 0; /* Compile only if set */ |
| 22 | int Uflag = 0; /* Add IMPLICIT UNDEFINED to generated fortran */ |
| 23 | int Cflag = 0; /* Copy Ratfor comments if set */ |
| 24 | int errcnt; |
| 25 | char *arg0; |
| 26 | char *complr = "/usr/fort/fc1"; |
| 27 | char *ratfor = "/usr/bin/ratfor"; |
| 28 | char *ratout = "ratjunk"; |
| 29 | char *rattmp = "ratjunk.r"; |
| 30 | char *ratopt = "-1&"; |
| 31 | char *efl = "/usr/bin/efl"; |
| 32 | char *eflout = "efljunk"; |
| 33 | char *eflopt = "-u "; |
| 34 | char *macro = "/usr/bin/m4"; |
| 35 | char *undecl = "implicit undefined /a-z,A-Z/\n"; |
| 36 | |
| 37 | # define BADOPEN 127 |
| 38 | main(argc, argv) |
| 39 | char *argv[]; { |
| 40 | char *t; |
| 41 | int i, j, c; |
| 42 | int dexit(); |
| 43 | |
| 44 | arg0 = argv[0]; |
| 45 | for(i=0; ++i < argc; ) { |
| 46 | if(*argv[i] == '-') |
| 47 | for(j=1; argv[i][j]; j++) { |
| 48 | switch (argv[i][j]) { |
| 49 | default: |
| 50 | if(j == 1) goto passa; |
| 51 | else continue; |
| 52 | case 'm': |
| 53 | mflag = 1; |
| 54 | break; |
| 55 | case 't': |
| 56 | tflag = 1; |
| 57 | break; |
| 58 | case 'v': |
| 59 | vflag = 0; |
| 60 | break; |
| 61 | case 'd': |
| 62 | eflopt[7] = 'd'; |
| 63 | break; |
| 64 | case 'g': |
| 65 | eflopt[2] = 'g'; |
| 66 | eflopt[3] = argv[i][j+1]; |
| 67 | rflag = cflag = fflag = 1; |
| 68 | break; |
| 69 | case 'e': |
| 70 | case 'r': |
| 71 | rflag = fflag = cflag = 1; |
| 72 | break; |
| 73 | case 'f': |
| 74 | fflag = 1; |
| 75 | break; |
| 76 | case 'c': |
| 77 | cflag = 1; |
| 78 | break; |
| 79 | case 'U': |
| 80 | Uflag = 1; |
| 81 | break; |
| 82 | case 'C': |
| 83 | Cflag = 1; |
| 84 | break; |
| 85 | case '2': |
| 86 | complr = "/usr/fort/fc2"; |
| 87 | break; |
| 88 | case '6': |
| 89 | ratopt[1] = '6'; |
| 90 | ratopt[2] = argv[i][j+1]; |
| 91 | rflag = cflag = fflag = 1; |
| 92 | break; |
| 93 | case '9': |
| 94 | eflopt[4] = '9'; |
| 95 | break; |
| 96 | case '#': |
| 97 | eflopt[5] = '#'; |
| 98 | break; |
| 99 | case 'w': |
| 100 | eflopt[6] = 'w'; |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | else { |
| 105 | passa: |
| 106 | t = argv[i]; |
| 107 | switch( getsuf(t) ) { |
| 108 | |
| 109 | case 'e': |
| 110 | eflcomp(t); |
| 111 | break; |
| 112 | |
| 113 | case 'r': |
| 114 | ratcomp(t); |
| 115 | break; |
| 116 | |
| 117 | case 'f': |
| 118 | fortcomp(t); |
| 119 | llenter(setsuf(copy(t),'o')); |
| 120 | break; |
| 121 | |
| 122 | default: |
| 123 | llenter(copy(t)); |
| 124 | break; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | if(rflag) |
| 129 | dexit(0); |
| 130 | if (signal(SIGINT, SIG_IGN) != SIG_IGN) |
| 131 | signal(SIGINT, dexit); |
| 132 | if(tflag) |
| 133 | printf("errcnt=%d, nl=%d\n", errcnt, nl); |
| 134 | if (errcnt==0 & cflag==0 && nl!=0) { |
| 135 | i = 0; |
| 136 | av[0] = "ld"; |
| 137 | av[1] = "-x"; |
| 138 | av[2] = "/lib/fr0.o"; |
| 139 | j = 3; |
| 140 | while(i<nl) |
| 141 | av[j++] = llist[i++]; |
| 142 | av[j++] = "-lf"; |
| 143 | av[j++] = "/usr/lib/filib.a"; |
| 144 | av[j++] = "-l"; |
| 145 | av[j++] = 0; |
| 146 | callsys("/bin/ld", av); |
| 147 | } |
| 148 | dexit(errcnt); |
| 149 | } |
| 150 | |
| 151 | dexit(n) |
| 152 | int n; |
| 153 | { |
| 154 | cunlink(ratout); |
| 155 | cunlink(rattmp); |
| 156 | cunlink(eflout); |
| 157 | cunlink("f.tmp1"); |
| 158 | if(tflag) |
| 159 | printf("%s status=%d\n", arg0, n); |
| 160 | exit(n); |
| 161 | } |
| 162 | |
| 163 | eflcomp(s) char *s; { |
| 164 | nr = 0; |
| 165 | if(vflag) |
| 166 | printf("%s:\n",s); |
| 167 | if( callprep( efl, s, eflout, eflopt, 0, 0 ) == 0 ) { |
| 168 | splitup(eflout); |
| 169 | dorlist(s); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | |
| 174 | |
| 175 | ratcomp(s) char *s; { |
| 176 | int i, j, t; |
| 177 | nr = 0; |
| 178 | if(vflag) |
| 179 | printf("%s:\n",s); |
| 180 | if (mflag) { |
| 181 | if( ( t = callprep( macro, s, rattmp, 0, 0, 0 ) ) < BADOPEN ) |
| 182 | t = callprep( ratfor, rattmp, ratout, ratopt, Cflag?"-C":0, 0 ); |
| 183 | } else |
| 184 | t = callprep( ratfor, s, ratout, ratopt, Cflag?"-C":0, 0 ); |
| 185 | if( t < BADOPEN ) { |
| 186 | splitup(ratout); |
| 187 | dorlist(s); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | callprep( prep, file, output, opt1, opt2, opt3 ) |
| 192 | char *prep, *file, *output, *opt1, *opt2, *opt3; |
| 193 | { |
| 194 | int t, status, i, j; |
| 195 | |
| 196 | av[0] = prep; |
| 197 | j = 1; |
| 198 | if (opt1) av[j++] = opt1; |
| 199 | if (opt2) av[j++] = opt2; |
| 200 | if (opt3) av[j++] = opt3; |
| 201 | av[j] = 0; |
| 202 | if( tflag ) { |
| 203 | printf("%s <%s ", av[0], file); |
| 204 | for (i=1; av[i]; i++) |
| 205 | printf("%s ", av[i]); |
| 206 | printf("\n"); |
| 207 | } |
| 208 | if( (t=fork())==0 ){ |
| 209 | close(1); |
| 210 | if( (fout=creat(output, 0666)) < 0) { |
| 211 | error( "can't open %s", output ); |
| 212 | dexit(BADOPEN); |
| 213 | } |
| 214 | close(0); |
| 215 | if( (fin=open(file, 0)) < 0) { |
| 216 | error( "can't open %s", file ); |
| 217 | dexit(BADOPEN); |
| 218 | } |
| 219 | execv(prep, av); |
| 220 | error("can't execute %s", prep); |
| 221 | dexit(1); |
| 222 | } |
| 223 | while( t!=wait(&status) ); |
| 224 | if( (t=(status&0377)) != 0 && t!=14 ) |
| 225 | dexit(1); |
| 226 | t = (status>>8) & 0377; |
| 227 | if( tflag ) |
| 228 | printf("status = %d\n", t); |
| 229 | if( t ) ++errcnt; |
| 230 | return ( t ); |
| 231 | } |
| 232 | |
| 233 | dorlist(s) char *s; { |
| 234 | |
| 235 | int i, j, t; |
| 236 | int fstat; |
| 237 | |
| 238 | if( rflag ) return; |
| 239 | fstat = 0; |
| 240 | for(i=0; i<nr; i++){ |
| 241 | if( vflag ) printf(" "); |
| 242 | if( fortcomp(rlist[i]) ) |
| 243 | fstat++; |
| 244 | } |
| 245 | if( fstat ) { |
| 246 | for(i=0; i<nr; i++) { |
| 247 | cunlink( setsuf( rlist[i], 'o' ) ); |
| 248 | if( fflag==0 ) cunlink( setsuf( rlist[i], 'f' ) ); |
| 249 | } |
| 250 | return; |
| 251 | } |
| 252 | av[0] = "ld"; |
| 253 | av[1] = "-r"; |
| 254 | av[2] = "-x"; |
| 255 | j = 3; |
| 256 | for(i=0; i<nr; i++) |
| 257 | av[j++] = rlist[i]; |
| 258 | av[j] = 0; |
| 259 | callsys("/bin/ld", av); |
| 260 | t = setsuf(copy(s),'o'); |
| 261 | if( move( "a.out", t) ) |
| 262 | errcnt++; |
| 263 | llenter(t); |
| 264 | for(i=0; i<nr; i++) { |
| 265 | if( nodup(llist,rlist[i]) ) |
| 266 | cunlink(rlist[i]); |
| 267 | if( fflag==0 ) |
| 268 | cunlink(setsuf(rlist[i],'f')); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | fortcomp(s) char *s; { |
| 273 | int t; |
| 274 | if( vflag ) printf("%s:\n", s); |
| 275 | av[0] = complr; |
| 276 | av[1] = s; |
| 277 | av[2] = 0; |
| 278 | if( callsys(complr, av) ) |
| 279 | return(++errcnt); |
| 280 | av[0] = "as"; |
| 281 | av[1] = "-"; |
| 282 | av[2] = "-o"; |
| 283 | av[3] = setsuf(s, 'o'); |
| 284 | av[4] = "f.tmp1"; |
| 285 | av[5] = 0; |
| 286 | callsys("/bin/as", av); |
| 287 | return(0); |
| 288 | } |
| 289 | |
| 290 | getsuf(s) |
| 291 | char s[]; |
| 292 | { |
| 293 | int c; |
| 294 | char t, *os; |
| 295 | |
| 296 | c = 0; |
| 297 | os = s; |
| 298 | while(t = *s++) |
| 299 | if (t=='/') |
| 300 | c = 0; |
| 301 | else |
| 302 | c++; |
| 303 | s -= 3; |
| 304 | if (c<=14 && c>2 && *s++=='.') |
| 305 | return(*s); |
| 306 | return(0); |
| 307 | } |
| 308 | |
| 309 | char * |
| 310 | setsuf(s, ch) |
| 311 | char s[]; |
| 312 | { |
| 313 | char *os; |
| 314 | |
| 315 | os = s; |
| 316 | while( *s ) |
| 317 | if( *s++ == '/' ) |
| 318 | os = s; |
| 319 | s[-1] = ch; |
| 320 | return(os); |
| 321 | } |
| 322 | |
| 323 | move(s,t) char *s, *t; { |
| 324 | cunlink(t); |
| 325 | if(link(s, t) || cunlink(s)) { |
| 326 | printf("move failed: %s\n", t); |
| 327 | return(1); |
| 328 | } |
| 329 | return(0); |
| 330 | } |
| 331 | |
| 332 | callsys(f, v) |
| 333 | char f[], *v[]; { |
| 334 | int i, t, status; |
| 335 | |
| 336 | if(tflag){ |
| 337 | printf("%s ", f); |
| 338 | for(i=0; v[i]; i++) |
| 339 | printf("%s ", v[i]); |
| 340 | putchar('\n'); |
| 341 | } |
| 342 | if ((t=fork())==0) { |
| 343 | execv(f, v); |
| 344 | printf("Can't find %s\n", f); |
| 345 | dexit(1); |
| 346 | } else |
| 347 | if (t == -1) { |
| 348 | printf("Try again\n"); |
| 349 | return(1); |
| 350 | } |
| 351 | while(t!=wait(&status)); |
| 352 | if ((t=(status&0377)) != 0 && t!=14) { |
| 353 | if (t!=2) /* interrupt */ |
| 354 | printf("Fatal error in %s\n", f); |
| 355 | dexit(1); |
| 356 | } |
| 357 | t = (status>>8) & 0377; |
| 358 | if( tflag ) |
| 359 | printf("status = %d\n", t); |
| 360 | return(t); |
| 361 | } |
| 362 | |
| 363 | char * |
| 364 | copy(s) |
| 365 | char s[]; { |
| 366 | char *otsp; |
| 367 | |
| 368 | otsp = tsp; |
| 369 | while(*tsp++ = *s++); |
| 370 | return(otsp); |
| 371 | } |
| 372 | |
| 373 | nodup(l, s) |
| 374 | char **l, s[]; { |
| 375 | char *t, *os, c; |
| 376 | |
| 377 | if (getsuf(s) != 'o') |
| 378 | return(1); |
| 379 | os = s; |
| 380 | while(t = *l++) { |
| 381 | s = os; |
| 382 | while(c = *s++) |
| 383 | if (c != *t++) |
| 384 | break; |
| 385 | if (*t++ == '\0') |
| 386 | return(0); |
| 387 | } |
| 388 | return(1); |
| 389 | } |
| 390 | |
| 391 | llenter(t) char *t; { |
| 392 | if (nodup(llist, t)) { |
| 393 | llist[nl++] = t; |
| 394 | if (getsuf(t)=='o') |
| 395 | nxo++; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | cunlink(f) |
| 400 | char *f; |
| 401 | { |
| 402 | if( tflag ) |
| 403 | printf("unlink %s\n", f); |
| 404 | if (f==0) |
| 405 | return(0); |
| 406 | return(unlink(f)); |
| 407 | } |
| 408 | |
| 409 | splitup(file) char *file; { |
| 410 | char in[1500], fname[20]; |
| 411 | int buf[259]; |
| 412 | int i,fd,mainsw,c; |
| 413 | if( (fin=open(file, 0)) < 0) |
| 414 | error("can't open %s", file); |
| 415 | while( gets(in) ){ |
| 416 | if( *in == 'c' || *in == 'C' ) continue; |
| 417 | mainsw = getname(in, fname); |
| 418 | savename(fname); |
| 419 | if( (fd = fcreat(fname, buf)) < 0) |
| 420 | error("can't open %s", fname); |
| 421 | if(mainsw && Uflag) { |
| 422 | puts(undecl,buf); |
| 423 | puts(in,buf); |
| 424 | } else { |
| 425 | puts(in,buf); |
| 426 | if( Uflag ) |
| 427 | puts(undecl,buf); |
| 428 | } |
| 429 | while( ! endcard(in) ){ |
| 430 | gets(in); |
| 431 | puts(in,buf); |
| 432 | } |
| 433 | fflush(buf); |
| 434 | close(fd); |
| 435 | } |
| 436 | close(fin); |
| 437 | } |
| 438 | |
| 439 | gets(s) char *s; { |
| 440 | int c; |
| 441 | while( (*s++=c=getchar()) != '\n' && c != '\0' ); |
| 442 | *s = '\0'; |
| 443 | return(c); |
| 444 | } |
| 445 | |
| 446 | puts(s,b) char *s; int *b; { |
| 447 | while( *s ) |
| 448 | putc(*s++, b); |
| 449 | } |
| 450 | |
| 451 | savename(s) char *s; { |
| 452 | rlist[nr++] = copy(s); |
| 453 | } |
| 454 | |
| 455 | getname(s,f) char *s,*f; { |
| 456 | int i,j,c; |
| 457 | loop: |
| 458 | while( *s == ' ' || *s == '\t' ) |
| 459 | s++; |
| 460 | if( compar(s,"subroutine") ){ s += 10; goto bot; } |
| 461 | else if( compar( s,"function") ){ s += 8; goto bot; } |
| 462 | else if( compar(s,"real") ){ s += 4; goto loop; } |
| 463 | else if( compar(s,"integer") ){ s += 7; goto loop; } |
| 464 | else if( compar(s,"logical") ){ s += 7; goto loop; } |
| 465 | else if( compar(s,"double") ){ s += 6; goto loop; } |
| 466 | else if( compar(s,"precision") ){ s += 9; goto loop; } |
| 467 | else if( compar(s,"complex") ){ s += 7; goto loop; } |
| 468 | else if( compar(s,"*") ){ /* integer *16 */ |
| 469 | ++s; |
| 470 | while( (*s >= '0' && *s <= '9') || *s == ' ' || *s == '\t' ) |
| 471 | s++; |
| 472 | goto loop; |
| 473 | } |
| 474 | else if( compar(s,"block") ){ |
| 475 | s = "BLOCKDATA "; |
| 476 | s[9] = (bdcount++) + '0'; |
| 477 | goto bot; |
| 478 | } |
| 479 | else { |
| 480 | for(i=0; f[i]="MAIN.f"[i]; i++); |
| 481 | return(1); |
| 482 | } |
| 483 | bot: |
| 484 | while( *s == ' ' || *s == '\t' ) |
| 485 | s++; |
| 486 | for(i=0; alphanum(s[i]); i++) |
| 487 | f[i] = s[i]; |
| 488 | f[i++] = '.'; |
| 489 | f[i++] = 'f'; |
| 490 | f[i++] = '\0'; |
| 491 | return(0); |
| 492 | } |
| 493 | |
| 494 | compar(s,t) char *s,*t; { |
| 495 | while( *t ) |
| 496 | if( *s++ != *t++ ) |
| 497 | return(0); |
| 498 | return(1); |
| 499 | } |
| 500 | |
| 501 | alphanum(c) int c; { |
| 502 | return( (c>='a' && c<='z') |
| 503 | || (c>='A' && c<='Z') |
| 504 | || (c>='0' && c<='9') ); |
| 505 | } |
| 506 | |
| 507 | endcard(s) char *s; { |
| 508 | if( *s==0 ) |
| 509 | return(1); |
| 510 | while( *s==' ' || *s=='\t' ) |
| 511 | s++; |
| 512 | if( *s!='e' || *(s+1)!='n' || *(s+2)!='d' || *(s+3)!='\n' ) |
| 513 | return(0); |
| 514 | return(1); |
| 515 | } |
| 516 | |
| 517 | error(s1, s2){ |
| 518 | fout = 2; |
| 519 | printf(s1,s2); |
| 520 | putchar('\n'); |
| 521 | flush(1); |
| 522 | errcnt++; |
| 523 | } |