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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
|
/* main.c */
/* VIS-5D version 4.0 */
/*
VIS-5D system for visualizing five dimensional gridded data sets
Copyright (C) 1990, 1991, 1992, 1993, 1994 Bill Hibbard, Brian Paul,
Dave Santek, and Andre Battaiola.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/stat.h>
char cc[1000];
int Argc;
char *Argv[100]; /* up to 100 arguments (converted to uppercase) */
char *Argvl[100]; /* up to 100 arguments (in unchanged case) */
char **Argvector;
extern int lwopen( char *, int );
extern int main0(void);
extern int main0_(void);
/***************************************************************************/
main(argc,argv)
int argc;
char *argv[];
{
int i;
struct rlimit rl;
#ifndef hp
/* disable core dumps */
rl.rlim_cur = rl.rlim_max = 0;
setrlimit( RLIMIT_CORE, &rl );
#endif
Argc = argc;
Argvector = argv;
/* Convert arguments to uppercase and store in Argv */
for (i=0; i<argc; i++) {
char *cp;
int len;
len = strlen( argv[i] );
Argv[i] = (char *) malloc( len + 1 );
Argvl[i] = (char *) malloc( len + 1 );
strcpy( Argv[i], argv[i] );
strcpy( Argvl[i], argv[i] );
for (cp=Argv[i]; *cp; cp++) {
if (*cp >= 'a' && *cp <= 'z')
*cp = *cp + 'A' - 'a';
}
}
#ifdef UNDERSCORE
i=main0_();
#else
i=main0();
#endif
printf("%s done\n", Argv[0] );
return(0);
}
/***************************************************************************/
#define SUBSTR(sub, str) (memcmp(sub, str, strlen(sub)) ? 0 : 1)
#ifdef UNDERSCORE
nkwp_(ckw,lenkw)
#else
nkwp(ckw,lenkw)
#endif
int lenkw;
char *ckw;
{
char ck[20],*cmd;
int i,j,n;
strncpy(ck,ckw,lenkw);
ck[lenkw]=0;
if (strcmp(ck, " ") == 0) {
j = 1;
n = 0;
cmd = Argv[j];
while (j < Argc) {
if (cmd[0] == '-' && (cmd[1] < '0' || cmd[1] > '9'))
break;
cmd = Argv[++j];
n++;
}
}
else {
n = 0;
for (i = 1; i < Argc; i++) {
cmd = Argv[i];
if (cmd[0] == '-' && (cmd[1] < '0' || cmd[1] > '9') &&
SUBSTR(cmd + 1, ck)) {
j = i + 1;
n = 0;
cmd = Argv[j];
while (j < Argc) {
if (cmd[0] == '-' && (cmd[1] < '0' || cmd[1] > '9'))
break;
cmd = Argv[++j];
n++;
}
if (n == 0) n = 1;
break;
}
}
}
return(n);
}
/*
* Return a string command line argument.
* Input: arg - argument number
* Output: str - null terminated string
* len - length of str
*/
#ifdef UNDERSCORE
strarg_( result, resultmax, arg )
#else
strarg( result, resultmax, arg )
#endif
char *result;
int resultmax, *arg;
{
strncpy( result, Argvl[*arg], resultmax );
}
/* cres = result string, len = length limit for cres
ckw = keyword string, of length lenkw
ip = parameter # after keyword
cin = default value of length lenc
NOTE strings are FORTRAN strings, so no
null termination and blank padded to length
*/
#ifdef UNDERSCORE
ckwp_(cres,len,ckw,ip,cin,lenkw,lenc)
#else
ckwp(cres,len,ckw,ip,cin,lenkw,lenc)
#endif
int *ip,len,lenkw,lenc;
char *cres,*ckw,*cin;
{
char *d,c[20],ck[20],*cmd;
int i,j,k,n;
/* printf("cpp: %d %d %s %d\n",len,ip[0],cin,lenc); */
strncpy(c,cin,lenc);
c[lenc]=0;
strncpy(ck,ckw,lenkw);
ck[lenkw]=0;
d=c;
if (strcmp(ck, " ") == 0) {
j = 1;
n = 1;
cmd = Argv[j];
while (j < Argc) {
if (cmd[0] == '-' && (cmd[1] < '0' || cmd[1] > '9'))
break;
if (n == ip[0]) {
d = Argv[j];
break;
}
cmd = Argv[++j];
n++;
}
}
else {
for (i = 1; i < Argc; i++) {
cmd = Argv[i];
if (cmd[0] == '-' && (cmd[1] < '0' || cmd[1] > '9') &&
SUBSTR(cmd + 1, ck)) {
j = i + 1;
n = 1;
cmd = Argv[j];
while (j < Argc) {
if (cmd[0] == '-' && (cmd[1] < '0' || cmd[1] > '9'))
break;
if (n == ip[0]) {
d = Argv[j];
break;
}
cmd = Argv[++j];
n++;
}
break;
}
}
}
k=strlen(d);
if (len<k)
k=len;
for (j=0; j<k; j++)
cres[j]=d[j];
for (j=k; j<len; j++)
cres[j]=' ';
return(0);
}
/****************************************************************************/
#ifdef LITTLE
/*
* Flip the order of the 4 bytes in an array of 4-byte words.
*/
static void flip_words( src, dest, nwords )
unsigned int *src, *dest;
int nwords;
{
int i;
for (i=0;i<nwords;i++) {
register unsigned int n = src[i];
dest[i] = (n & 0xff000000) >> 24
| (n & 0x00ff0000) >> 8
| (n & 0x0000ff00) << 8
| (n & 0x000000ff) << 24;
}
}
#endif
#ifdef LITTLE
/* buffer used for byte-swapping: */
# define TEMP_SIZE 10000
static unsigned int Temp[TEMP_SIZE];
#endif
#define NFILES 8
int ld=0,nlw=0;
char f[NFILES][256];
int fd[NFILES];
/*
* Read an array of 4-byte words from a file.
* Input: cf - filename
* iw - position to read in file (in words)
* nw - number of words to read
* ibuf - address of buffer
* length - length of cf string
* Return: 0 = ok
* -1 = error opening file
* -2 = error seeking
* -3 = error reading
*/
#ifdef UNDERSCORE
lwi_(cf,iw,nw,ibuf,length)
#else
lwi(cf,iw,nw,ibuf,length)
#endif
int *iw,*nw,*ibuf,length;
char *cf;
{
int nbytes,ifile,iword,nwords,len,kword;
long offset;
char cfile[256];
len = length;
strncpy(cfile,cf,len);
cfile[len]=0;
len = len-1;
while (cfile[len] == ' ' && len > 0) {
cfile[len] = 0;
len = len-1;
}
iword=iw[0];
nwords=nw[0];
if ( (ifile=lwopen(cfile, 1)) <0) {
for ( kword = 0; kword < nwords; kword++)
ibuf[kword] = 0x80808080;
return(-1);
}
offset=4*iword;
if (lseek(ifile,offset,0)<0) {
for ( kword = 0; kword < nwords; kword++)
ibuf[kword] = 0x80808080;
return(-2);
}
nbytes=4*nwords;
if (read(ifile,ibuf,nbytes)<=0) {
for ( kword = 0; kword < nwords; kword++)
ibuf[kword] = 0x80808080;
return(-3);
}
#ifdef LITTLE
flip_words( ibuf, ibuf, nwords );
#endif
return(0);
}
/*
* Write an array of 4-byte words to a file.
* Input: cf - pointer to filename
* iw - file position in words
* nw - number of words to write.
* ibuf - address of first work to write.
* length - length of cf string.
* Return: 0 = ok
* -1 = unable to open file
* -2 = seek failed
* -3 = write failed
*/
#ifdef UNDERSCORE
lwo_(cf,iw,nw,ibuf,length)
#else
lwo(cf,iw,nw,ibuf,length)
#endif
int *iw,*nw,length;
char *cf,*ibuf;
{
int nbytes,ifile,iword,nwords,len,kword;
int nblocks, iblk;
long offset,filelen, gapword;
char cfile[256];
long int nullbuf[512];
static int nbuf = 512;
/* put filename into cfile and remove trailing spaces */
len = length;
strncpy(cfile,cf,len);
cfile[len]=0;
len = len-1;
while (cfile[len] == ' ' && len > 0) {
cfile[len] = 0;
len = len-1;
}
iword=iw[0];
nwords=nw[0];
if ( (ifile=lwopen(cfile, 0)) <0)
return(-1);
offset=4*iword;
filelen = lseek(ifile, (long) 0, 2);
/* check to see if we write past eof */
if (offset > filelen) {
for (kword = 0; kword < nbuf; kword++)
nullbuf[kword] = 0x80808080;
gapword = (offset - filelen)/4;
nblocks = gapword/nbuf;
if( nblocks != 0) {
nbytes = nbuf * 4;
for ( iblk = 0; iblk < nblocks; iblk++)
write(ifile, nullbuf, nbytes);
}
nbytes = 4 * (gapword % nbuf);
if ( nbytes != 0 )
write(ifile, nullbuf, nbytes);
}
/* seek to file positio and write buffer */
if (lseek(ifile,offset,0)<0)
return(-2);
nbytes=4*nwords;
#ifdef LITTLE
if (nwords>TEMP_SIZE) {
printf("Internal error in LWO, increase TEMP_SIZE\n");
exit(0);
}
flip_words( ibuf, Temp, nwords );
if (write(ifile,Temp,nbytes)<0)
return(-3);
#else
if (write(ifile,ibuf,nbytes)<0)
return(-3);
#endif
return(0);
}
/*
* Read an array of bytes from a file.
* Input: cf - filename
* ib - offset of first byte to read in file
* nb - number of bytes to read
* ibuf - address to buffer to store bytes
* length - length of cf string
* Return: 0 = ok
* -1 = unable to open file
* -2 = seek failed
* -3 = write failed
*/
#ifdef UNDERSCORE
lbi_(cf,ib,nb,ibuf,length)
#else
lbi(cf,ib,nb,ibuf,length)
#endif
int *ib,*nb,length;
char *cf,*ibuf;
{
int nbytes,ifile,len;
long offset;
char cfile[256];
len = length;
strncpy(cfile,cf,len);
cfile[len]=0;
len = len-1;
while (cfile[len] == ' ' && len > 0) {
cfile[len] = 0;
len = len-1;
}
if ( (ifile=lwopen(cfile, 1)) <0) {
ibuf[0] = 0X80;
return(-1);
}
offset=ib[0];
if (lseek(ifile,offset,0)<0)
return(-2);
nbytes=nb[0];
if (read(ifile,ibuf,nbytes)<=0)
return(-3);
return(0);
}
/*
* Write an array of bytes to a file.
* Input: cf - filename
* ib - file offset at which to write first byte
* nb - number of bytes to write.
* ibuf - address of byte buffer
* length - length of cf string
* Return: 0 = ok
* -1 = unable to open file
* -2 = seek failed
* -3 = write failed
*/
#ifdef UNDERSCORE
lbo_(cf,ib,nb,ibuf,length)
#else
lbo(cf,ib,nb,ibuf,length)
#endif
int *ib,*nb,length;
char *cf,*ibuf;
{
int nbytes,ifile,len;
long offset;
char cfile[256];
len = length;
strncpy(cfile,cf,len);
cfile[len]=0;
len = len-1;
while (cfile[len] == ' ' && len > 0) {
cfile[len] = 0;
len = len-1;
}
if ( (ifile=lwopen(cfile, 0)) <0)
return(-1);
offset=ib[0];
if (lseek(ifile,offset,0)<0)
return(-2);
nbytes=nb[0];
if (write(ifile,ibuf,nbytes)<0)
return(-3);
return(0);
}
/*
* Open a file for I/O.
* Input: cfile - name of file to open
* rdflag = 1 = open for read, else open for write.
* Return: file descriptor or -1 if error.
*/
lwopen(cfile, rdflag)
char *cfile;
int rdflag;
{
int id,j,len;
len=strlen(cfile);
/* printf("\nfile: %s length: %d\n",cfile,len); */
for (id=0; id<nlw; id++) {
for (j=0; j<len; j++) {
if (cfile[j]!=f[id][j])
goto notf;
}
return(fd[id]);
notf: ;
}
if(ld < nlw) close(fd[ld]);
for (j=0; j<=len; j++)
f[ld][j]=cfile[j];
umask(0);
if (rdflag) fd[ld]=open(cfile,O_RDWR,0666);
else fd[ld]=open(cfile,O_RDWR|O_CREAT,0666);
if (fd[ld]<0) {
if (!rdflag) printf("\ncannot open file: %s\n",cfile);
return(-1);
}
id=ld;
++ld;
if (ld>nlw)
nlw=ld;
if (ld>NFILES-1)
ld=0;
return(fd[id]);
}
/****************************************************************************/
/*
* Write a string and integer to stdout.
* Input: c - the string
* i - the integer
* len - length of c
*/
#ifdef UNDERSCORE
cdest_(c,i,len)
#else
cdest(c,i,len)
#endif
int *i,len;
char *c;
{
int j;
c[len]='\0';
j=i[0];
printf("%s",c);
if (j!=0)
printf("%8d\n",j);
else
printf("\n");
return(0);
}
/*
* Move a block of bytes.
* Input: n - number of bytes
* c - source address
* d - dest address
* k - dest offset
*/
#ifdef UNDERSCORE
movb_(n,c,d,k)
#else
movb(n,c,d,k)
#endif
int *n,*k;
char *c,*d;
{
int i;
for (i=0; i<n[0]; i++)
d[k[0]+i]=c[i];
return(0);
}
/*****************************************************************************/
/*
* nothing
*/
#ifdef UNDERSCORE
luc_(index)
#else
luc(index)
#endif
int *index;
{
return(0);
}
/*
* nothing
*/
#ifdef UNDERSCORE
puc_(i, index)
#else
puc(i, index)
#endif
int *i, *index;
{
return(0);
}
#if defined(ibm) || defined(hp)
/* IDATE not supported on IBM or HP? */
idate( imon, iday, iyear )
int *imon, *iday, *iyear;
{
*imon = 1;
*iday = 1;
*iyear = 92;
return 0;
}
#endif
/*
* If we're running on a little endian machine we need to occasionally
* re-flip the 4-bytes in a word if it's a character string.
*/
#ifdef UNDERSCORE
int swapchar_( i )
#else
int swapchar( i )
#endif
unsigned int *i;
{
#ifdef LITTLE
unsigned int tmp = *i;
*i = (tmp & 0xff000000) >> 24
| (tmp & 0x00ff0000) >> 8
| (tmp & 0x0000ff00) << 8
| (tmp & 0x000000ff) << 24;
#endif
return 0;
}
|