home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / TIERRA40.ZIP / BEAGLE / PROBE.C < prev    next >
C/C++ Source or Header  |  1992-09-11  |  16KB  |  569 lines

  1. /*-----------------------------------------------------------------------*/
  2. /* 92-3-23 a new tool to slice trough the genebank files on disk ...     */
  3. /* Tierra Simulator V3.1: Copyright (c) 1992 Dan Pirone & Virtual Life   */
  4. /*-----------------------------------------------------------------------*/
  5.  
  6. /* #define _DBG_  */
  7. /* #define X  */    /* use xterms for unix shells */
  8. #ifdef __TURBOC__
  9. #define SLASH '\\'
  10. #else
  11. #define SLASH '/'
  12. #endif /* else of __TURBOC__ */
  13.  
  14. #include <stdio.h>
  15. #include <sys/types.h>
  16. #ifdef __TURBOC__
  17. #include <dir.h>
  18. #include <dos.h>
  19. #else
  20. #include <dirent.h>
  21. #ifndef ultrix
  22. #include <sys/dirent.h>
  23. #endif /* ndef ultrix */
  24. #endif /* else of __TURBOC__ */
  25.  
  26. #include "tierra.h"
  27. #include "declare.h"
  28. #include "arginst.h"
  29.  
  30. I32s usize = 80L
  31.      ,uLbl = 1L
  32.      ,umpp = 50
  33.      ,umpi = 50;
  34. Event uOeventB,uOeventE,uMeventB,uMeventE;
  35. I8s file_type[4]="gen",
  36. #ifdef __TURBOC__
  37.     gb_in[255] = "gb\\",
  38.     gb_out[255] = "td\\",
  39.     viewer[255] = "more < ",
  40. #else
  41.     gb_in[255] = "gb/",
  42.     gb_out[255] = "td/",
  43.     viewer[255] = "vi",
  44. #endif
  45.     imap[255] = "opcode.map",
  46.     data[600],
  47.     ubits[33] =  "????????????????????????????????";
  48. FILE *afp;
  49. /*-----------------------------------------------------------------------*/
  50. #define ARG
  51. #include "portable.c"
  52. #include "genio.c"
  53. #undef ARG
  54. /*-----------------------------------------------------------------------*/
  55. I8s * bit_names[] = {
  56. "permanent genotype name, saved in .gen file ",
  57. "swapped out to disk from the rambank, saved in .mem file ",
  58. "EXs = executes own instructions (self) ",
  59. "EXd = executes daughter's instructions ",
  60. "EXo = executes other cell's instructions ",
  61. "EXf = executes instructions in free memory ",
  62. "EXh = own instructions are executed by other creature (host) ",
  63. "TCs = matches template complement of self ",
  64. "TCd = matches template complement of daughter ",
  65. "TCo = matches template complement of other ",
  66. "TCf = matches template complement of free memory ",
  67. "TCh = own template complement is matched by other creature (host) ",
  68. "TPs = uses template pattern of self ",
  69. "TPd = uses template pattern of daughter ",
  70. "TPo = uses template pattern of other ",
  71. "TPf = uses template pattern of free memory ",
  72. "TPh = own template pattern is used by other creature (host) ",
  73. "MFs = moves instruction from self ",
  74. "MFd = moves instruction from daughter ",
  75. "MFo = moves instruction from other cell ",
  76. "MFf = moves instruction from free memory ",
  77. "MFh = own instructions are moved by other creature (host) ",
  78. "MTs = moves instruction to self ",
  79. "MTd = moves instruction to daughter ",
  80. "MTo = moves instruction to other cell ",
  81. "MTf = moves instruction to free memory ",
  82. "MTh = is written on by another creature (host) ",
  83. "MBs = executing other creatures code, moves inst from self ",
  84. "MBd = executing other creatures code, moves inst from daughter ",
  85. "MBo = executing other creatures code, moves inst from other cell ",
  86. "MBf = executing other creatures code, moves inst from free memory ",
  87. "MBh = other creature uses another cpu to move your instructions "};
  88.  
  89. /*-----------------------------------------------------------------------*/
  90. void FEClrscr()
  91. {
  92. #ifdef __TURBOC__
  93. clrscr();
  94. #else
  95. system("clear");
  96. #endif
  97. }
  98. /*-----------------------------------------------------------------------*/
  99. int FEGetch()
  100. {
  101. #ifdef __TURBOC__
  102.     return printf("\n"),getch();
  103. #else
  104.     char data[84],c;
  105.     fgets(data,84,stdin);
  106.     sscanf(data,"%c",&c);
  107.     return c;
  108. #endif
  109. }
  110.  
  111. /*-----------------------------------------------------------------------*/
  112. init_uparams(mode)
  113. I32s mode;
  114. {
  115. if(mode == 0)
  116.    {
  117.    usize = -1L;
  118.    uLbl = -1L;
  119.    umpp = -1L;
  120.    umpi = -1L;
  121.    uOeventB.m = -1L;
  122.    uOeventB.i = -1L;
  123.    uOeventE.m = -1L;
  124.    uOeventE.i = -1L;
  125.    uMeventB.m = -1L;
  126.    uMeventB.i = -1L;
  127.    uMeventE.m = -1L;
  128.    uMeventE.i = -1L;
  129.    strcpy(ubits, "????????????????????????????????");
  130.    strcpy(file_type,"gen");
  131.    /* strcpy(gb_in,"gb/");
  132.    strcpy(gb_out,"td/");
  133.    strcpy(viewer,"vi"); */
  134.    }
  135.  
  136. }
  137.  
  138. /*-----------------------------------------------------------------------*/
  139. set_imap()
  140. {   I32s i;
  141.  
  142.     sprintf(data,"%s/%s",gb_in,imap);
  143. #ifdef _DBG_
  144.     printf("SET_IMAP: file is %s\n",data);
  145. #endif
  146.     GetAMap(data);
  147. #ifdef _DBG_
  148.     for (i=0; i < INSTNUM; i++)
  149.         printf("SET_IMAP: Aid [%ld] = %hx, %s\n",i,aid[i].op,aid[i].mn);
  150.     FEGetch();
  151. #endif
  152. }
  153. /*-----------------------------------------------------------------------*/
  154. I16s Lbl2Int(s)
  155.     I8s *s;
  156. {
  157.     if (s[0] == '-')
  158.     return -1;
  159.     return (s[2] - 'a') + (26 * (s[1] - 'a')) + (676 * (s[0] - 'a'));
  160. }
  161.  
  162. /*-----------------------------------------------------------------------*/
  163. I8s *Int2Lbl(i)
  164.     I32s i;
  165. {
  166.     static I8s s[4];
  167.  
  168.     if (i < 0) {
  169.     strcpy(s, "---");
  170.     return s;
  171.     }
  172.     s[0] = 'a' + (I16s) i / 676;
  173.     i %= 676;
  174.     s[1] = 'a' + (I16s) i / 26;
  175.     i %= 26;
  176.     s[2] = 'a' + (I16s) i;
  177.     s[3] = 0;
  178.     return s;
  179. }
  180.  
  181. /*-----------------------------------------------------------------------*/
  182. I8s IsInBitSet(bstr,bits)
  183. I8s bstr[32];
  184. I32s bits;
  185. {
  186. I32s b,p;
  187. /* WARNING first 2 bits are not watch bits! */
  188. for(b=1,p=0; p < 32;p++, b = b << 1L)
  189.    {
  190.    if ((bstr[p] == '?') ||
  191.        (bstr[p] == '1' && (b&bits)) ||
  192.        (bstr[p] == '0' && !(b&bits)) )
  193.        continue;
  194.    return 0;
  195.    }
  196. return 1;
  197. }
  198. /*-----------------------------------------------------------------------*/
  199. void WritEcoFuzzyB(bits, buf)
  200.     I8s bits[33];
  201.     I8s *buf;   
  202. {
  203.     I32s b,i, j;
  204.  
  205.     if(!buf) return;
  206.     sprintf(buf,"EX      TC      TP      MF      MT      MB      ");
  207.     for (b=0,i = 0, j = 0; i < 6; i++, j = 0) {
  208.         if (bits[b++]!='0')
  209.             buf[2+(i*8)+j++] = 's';
  210.         if (bits[b++]!='0')
  211.             buf[2+(i*8)+j++] = 'd';
  212.         if (bits[b++]!='0')
  213.             buf[2+(i*8)+j++] = 'o';
  214.         if (bits[b++]!='0')
  215.             buf[2+(i*8)+j++] = 'f';
  216.         if (bits[b++]!='0')
  217.             buf[2+(i*8)+j++] = 'h';
  218.     }
  219. }
  220.  
  221. /*-----------------------------------------------------------------------*/
  222.  
  223. I8s MatchesProfile(usize,uLbl,umpp,umpi,uOeventB,uOeventE,uMeventB,uMeventE,
  224.                    ubits,Gen)
  225. I32s usize,uLbl,umpp,umpi;
  226. Event uOeventB,uOeventE,uMeventB,uMeventE;
  227. I8s ubits[32];
  228. Pgl Gen;
  229. {
  230.  
  231. if (
  232.     (Gen == NULL) ||
  233.     ((usize > -1L) && (Gen->gen.size != usize)) ||
  234.     ((uLbl > -1L) && (Lbl2Int(Gen->gen.label) != uLbl)) ||  
  235.     ((uOeventB.m > -1L) && (Gen->originI.m < uOeventB.m)) ||
  236.     ((uOeventB.i > -1L) && (Gen->originI.i < uOeventB.i)) ||
  237.     ((uOeventE.m > -1L) && (Gen->originI.m > uOeventE.m)) ||
  238.     ((uOeventE.i > -1L) && (Gen->originI.i > uOeventE.i)) ||
  239.     ((uMeventB.m > -1L) && (GFormat  < 2L)) || 
  240.     ((uMeventB.m > -1L) && (Gen->mpp_time.m < uMeventB.m)) ||
  241.     ((uMeventB.i > -1L) && (GFormat < 2L)) || 
  242.     ((uMeventB.i > -1L) && (Gen->mpp_time.i < uMeventB.i)) ||
  243.     ((uMeventE.m > -1L) && (GFormat < 2L)) ||
  244.     ((uMeventE.m > -1L) && (Gen->mpp_time.m > uMeventE.m)) ||
  245.     ((uMeventE.i > -1L) && (GFormat < 2L)) ||
  246.     ((uMeventE.i > -1L) && (Gen->mpp_time.i > uMeventE.i)) ||
  247.     ((umpp > -1L) && (100.0*Gen->MaxPropPop < umpp)) ||
  248.     ((umpi > -1L) && (100.0*Gen->MaxPropInst < umpi)) ||
  249.     !IsInBitSet(ubits,Gen->bits)) return 0;
  250. else return 1;
  251.  
  252. }
  253. /*-----------------------------------------------------------------------*/
  254. void Probe()
  255. {
  256. I32s i,done=0L;
  257. I8s c, file[255];
  258. head_t head;
  259. indx_t *indx, *tindx, gindx;
  260. GList *g = NULL;
  261. #ifdef __TURBOC__
  262. struct ffblk ffblk;
  263. #else /* unix ... */
  264. DIR *dirp;
  265. struct dirent *dep;
  266. #endif /* unix ... */
  267.  
  268. printf("\nBEGIN Probing Genebank\n\n");
  269.  
  270. #ifdef __TURBOC__
  271. sprintf(file,"%s%c*.%3s",gb_in,SLASH,file_type);
  272. if (findfirst(file,&ffblk,0))
  273.       {perror("PROBE :");exit(-666);}
  274.  
  275. while(!done)
  276.   {
  277.    if (kbhit() && ( FEGetch() == 0x1b)) return;
  278.    sprintf(file,"%s%c%s",gb_in,SLASH,ffblk.ff_name);
  279.  
  280. #else /* unix ... */
  281. if ((dirp = opendir(gb_in)) == NULL) 
  282.       {perror("PROBE :");exit(-666);}
  283.  
  284. while ( (dep = readdir(dirp)) != NULL)
  285.    {
  286.    if ((dep->d_namlen <= 3)  || 
  287.        (strcmp(&dep->d_name[dep->d_namlen-3],file_type))) continue;
  288.         sprintf(file,"%s%c%s",gb_in,SLASH,dep->d_name);
  289.  
  290. #endif /* unix ... */
  291.    printf(" scanning archive  |%s|\r",file);
  292.    fflush(stdout);
  293.  
  294.         if (!(afp = fopen(file, "rb"))) {
  295.             perror("PROBE open archive ");
  296.             exit(-666);
  297.         }
  298.         head = read_head(afp);
  299.         if (strncmp(head.magic, "tie", 3))
  300.             {
  301.             fprintf(stderr, "%s: bad magic number", file);
  302.         fclose(afp);
  303.             continue;
  304.             }
  305. #ifdef __TURBOC__
  306.         indx = &gindx;
  307. #else  /* __TURBOC__ */
  308.         indx = read_indx(afp, &head);
  309. #endif /* __TURBOC__ */
  310.            for (i=0; i<head.n; i++) 
  311.              {
  312. #ifdef __TURBOC__
  313.                 find_gen(afp, indx, "---", i);
  314.                 tindx = indx;
  315. #else  /* __TURBOC__ */
  316.                 tindx = &indx[i];
  317. #endif /* __TURBOC__ */
  318.                 g = get_gen(afp, &head, tindx, i);
  319.  
  320.         if(!MatchesProfile(usize,uLbl,umpp,umpi,uOeventB,uOeventE,
  321.                    uMeventB,uMeventE, ubits,g)) continue;
  322.                 printf("\n\t%04ld%3s\n",g->gen.size,g->gen.label);
  323.                 printf(
  324.     "Enter y to show , ESC to break out, any other key to continue ->");
  325.               c = FEGetch();
  326.                 if (c == 0x1b)
  327.                 {  if (g)
  328.                    {  if (g->genome)
  329.                       {  free(g->genome);
  330.                          g->genome = NULL;
  331.                       }
  332.                       if (g->gbits)
  333.                       {  free(g->gbits);
  334.                          g->gbits = NULL;
  335.                       }
  336.                       free(g);
  337.                       g = NULL;
  338.                    }
  339.                    fclose(afp);
  340.                    return;
  341.                 }
  342.                 
  343.                 if (c == 'y')
  344.                    {
  345.                    sprintf(file, 
  346.                   "%s%c%04hd%3s", gb_out, SLASH,
  347.               head.size, g->gen.label); 
  348.                    WritAscFile(g, file);
  349. #ifdef __TURBOC__
  350.                    sprintf(file, 
  351.               "%s  %s%c%04hd%3s", 
  352.                    viewer,gb_out,SLASH,head.size, g->gen.label);
  353. #else
  354. #ifdef X
  355.                    sprintf(file, 
  356.               "xterm -title \"%04hd%3s\" -e %s  %s%c%04hd%3s &", 
  357.                    head.size, g->gen.label, viewer,gb_out,SLASH,
  358.                head.size, g->gen.label);
  359. #else
  360.                    sprintf(file, 
  361.               "%s  %s%c%04hd%3s ", 
  362.                    viewer,gb_out,SLASH,head.size, g->gen.label);
  363. #endif /* X */
  364. #endif
  365.                    system(file);
  366.                    }
  367.                 if (g)
  368.                 {  if (g->genome)
  369.                    {  free(g->genome);
  370.                       g->genome = NULL;
  371.                    }
  372.                    if (g->gbits)
  373.                    {  free(g->gbits);
  374.                       g->gbits = NULL;
  375.                    }
  376.                    free(g);
  377.                    g = NULL;
  378.                 }
  379.              }
  380.     fclose(afp);
  381.    
  382.  
  383. #ifdef __TURBOC__
  384.    done = findnext(&ffblk);
  385. #endif
  386.    }
  387.  
  388.  
  389.  
  390. printf("\n\nEND   Probing Genebank\n\n");
  391. printf("Enter to continue \n");
  392. c = FEGetch();
  393.  
  394. }
  395. /*-----------------------------------------------------------------------*/
  396. void GetBits()
  397. {
  398. I32s b;
  399. I8s c;
  400. for(b = 0; b < 32; b++)
  401.   {
  402.   printf("Bit %2ld %65.65s (%c%)>",b,bit_names[b],ubits[b]);
  403.   c= FEGetch();
  404.   if( c == 0x1b) return; 
  405.   if( c == '1') ubits[b] = '1'; 
  406.   else
  407.   if( c == '0') ubits[b] = '0'; 
  408.   else
  409.   if( c == '?') ubits[b] = '?'; 
  410.  
  411. }
  412. }
  413. /*-----------------------------------------------------------------------*/
  414. void Probe_ascii_io()
  415. {
  416. I8s  answer = ' ',
  417.      bbuf[4],
  418.      data[120];
  419.      int ll;
  420. while(1)
  421.   {
  422. FEClrscr();
  423. printf("\nTIERRA Genebank Probe\t\t\tby Daniel Pirone\n\n");
  424. printf("1 - Size                                     (%ld)\n",usize);
  425. printf("2 - Label                                    (%3s)\n",
  426.     (uLbl > -1)? Int2Lbl(uLbl) :"-1");
  427. printf("3 - MaxPropPop  threshold [0-100]%%           (%ld) \n",umpp);
  428. printf("4 - MaxPropInst threshold [0-100]%%           (%ld) \n",umpi);
  429. printf("5 - Begin Time of MaxPropPop threshold cross (%ld,%ld)\n",
  430.     uMeventB.m,uMeventB.i);
  431. printf("6 - End   Time of MaxPropPop threshold cross (%ld,%ld)\n",
  432.     uMeventE.m,uMeventE.i);
  433. printf("7 - Begin Time of Origin                     (%ld,%ld) \n",
  434.     uOeventB.m,uOeventB.i);
  435. printf("8 - End   Time of Origin                     (%ld,%ld) \n",
  436.     uOeventE.m,uOeventE.i);
  437. printf("9 - Watch Bit String [0,1,?] (%32s) \n",ubits);
  438. WritEcoFuzzyB(ubits,Buff); 
  439. printf("    Watch Bit String (%s) \n",Buff);
  440. printf("f - Toggle file type                         (%3s)\n",file_type);
  441. printf("i - Genebank Input directory                 (%s)\n",gb_in);
  442. printf("o - Match    Output directory                (%s)\n",gb_out);
  443. printf("m - Opcode - Instruction Map file            (%s)\n",imap);
  444. printf("v - Set Gene Output viewer                   (%s)\n",viewer);
  445. printf("G - Begin Probe\n\n");
  446. printf(
  447. "PROBE | (1-9) = edit param, 0 = init params, G=go <ESC> = quit      -> ");
  448. answer = FEGetch();
  449.     switch (answer)
  450.        {
  451.        case 0x1b : {exit(-1);}
  452.        case 'G' : {set_imap();Probe(); break;}
  453.        case '5' : 
  454.        case '6' : 
  455.        case '7' : 
  456.        case '8' : 
  457.          printf("Enter millions , insts (-1 = don't care)  -> ");
  458.                  fgets(data,84,stdin);
  459.     }
  460.     switch (answer)
  461.        {
  462.        case 'f' : {
  463.                   (file_type[0] == 'g') ? strcpy(file_type,"tmp") 
  464.                     : strcpy(file_type,"gen");
  465.           break;
  466.           }
  467.        case 'i' : {
  468.           printf("Enter Input Direcotry -> ");
  469.                   fgets(data,84,stdin);
  470.           sscanf(data,"%s",gb_in);
  471.           set_imap();
  472.           break;
  473.           }
  474.        case 'o' : {
  475.           printf("Enter Output Direcotry -> ");
  476.                   fgets(data,84,stdin);
  477.           sscanf(data,"%s",gb_out);
  478.           break;
  479.           }
  480.        case 'm' : {
  481.           printf(
  482.           "Enter Imap filename (assumed to be in Genebank dir)-> ");
  483.                   fgets(data,84,stdin);
  484.           sscanf(data,"%s",imap);
  485.           set_imap();
  486.           break;
  487.           }
  488.        case 'v' : {
  489.           printf("Enter text viewer -> ");
  490.                   fgets(data,84,stdin);
  491.           /* sscanf(data,"%s",viewer); */
  492.           for(ll=0;ll < 80;viewer[ll++] = '\0');
  493.           strncpy(viewer,data,(strlen(data)-1));
  494.           break;
  495.           }
  496.        case '0' : {
  497.           init_uparams(0L);
  498.           break;
  499.           }
  500.        case '1' : {
  501.           printf("Enter Size [-1 = don't care] -> ");
  502.                   fgets(data,84,stdin);
  503.           sscanf(data,"%ld",&usize);
  504.           break;
  505.           }
  506.        case '2' : {
  507.           printf("Enter Label [-1 = don't care,aaa-zzz] -> ");
  508.                   fgets(data,84,stdin);
  509.           sscanf(data,"%s",bbuf);
  510.           if (atoi(bbuf) < 0) {uLbl = -1;break;}
  511.           uLbl = Lbl2Int(bbuf);
  512.           break;
  513.           }
  514.        case '3' : {
  515.           printf( "Enter threshold [-1 = don't care, 0-100]%% -> ");
  516.                   fgets(data,84,stdin);
  517.           sscanf(data,"%ld",&umpp);
  518.           break;
  519.           }
  520.        case '4' : {
  521.           printf("Enter threshold [-1 = don't care, 0-100]%% -> ");
  522.                   fgets(data,84,stdin);
  523.           sscanf(data,"%ld",&umpi);
  524.           break;
  525.           }
  526.        case '5' : {
  527.           sscanf(data,"%ld,%ld",&uMeventB.m,&uMeventB.i);
  528.           break;
  529.           }
  530.        case '6' : {
  531.           sscanf(data,"%ld,%ld",&uMeventE.m,&uMeventE.i);
  532.           break;
  533.           }
  534.        case '7' : {
  535.           sscanf(data,"%ld,%ld",&uOeventB.m,&uOeventB.i);
  536.           break;
  537.           }
  538.        case '8' : {
  539.           sscanf(data,"%ld,%ld",&uOeventE.m,&uOeventE.i);
  540.           break;
  541.           }
  542.        case '9' : {
  543.           /* 
  544.                   printf("Enter Watch Bits [? = don't care,0-1] -> ");
  545.                   fgets(data,84,stdin);
  546.           sscanf(data,"%32s",ubits);
  547.                   */
  548.                   FEClrscr();
  549.                   GetBits();
  550.           break;
  551.           }
  552.           
  553.        default : answer = ' ';
  554.        }
  555.   }
  556. }
  557. /*-----------------------------------------------------------------------*/
  558. int main (argc,argv)
  559. int argc;
  560. char *argv[];
  561. {
  562. FEClrscr();
  563. init_uparams(0L);
  564.  
  565. Probe_ascii_io();
  566. FEClrscr();
  567. }
  568. /*-----------------------------------------------------------------------*/
  569.