home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d07xx / d0744.lha / Hextract / Source / extract.c < prev    next >
C/C++ Source or Header  |  1992-10-12  |  10KB  |  317 lines

  1. /****************************************************************************
  2. *   Extract.c                                    *
  3. *                                           *
  4. *   Chas A. Wyndham     18th Aug 1992.                         *
  5. *                                        *    
  6. *   Compiles under Lattice 5 and links with Decomp.o to make Hextract, a    *
  7. *   programme for accessing header-file data.                    *
  8. *                                        *
  9. *   This code is Freeware like Hextract itself.  You are welcome to improve *
  10. *   it - but please send me a copy.                        *
  11. *                                        *
  12. ****************************************************************************/
  13.           
  14. #include "exec/types.h"
  15. #include "exec/memory.h"
  16. #include "graphics/gfxbase.h"
  17. #include "libraries/dos.h"
  18. #include "intuition/intuition.h"
  19. #include "intuition/intuitionbase.h"
  20. #include "string.h"
  21. #include "stdio.h"
  22.  
  23. struct IntuitionBase *IntuitionBase ;
  24. struct GfxBase     *GfxBase ;
  25. struct Screen      *screendata ; 
  26. struct FileHandle  *window, *fi ;
  27. char               buffer[100], line[256], *febuf=0, *inbuf=0 ;
  28. int                found = 0, located = 0 ;
  29. int                fesize, insize, n, libh = 0 ;
  30.  
  31. USHORT chip ptrImage[] = {  
  32.      0x0000, 0x0000,
  33.      0x6018, 0x0000,
  34.      0xc00c, 0x0000,
  35.      0xc30c, 0x0000,
  36.      0xc78c, 0x0000,
  37.      0xcccc, 0x0000,
  38.      0x7878, 0x0000,
  39.      0x3030, 0x0000,
  40.      0x0000, 0x0000,  };
  41.  
  42. void   search(), showlib(), getwin(), cleanup() ;
  43.  
  44. main(argc, argv)
  45. int   argc ;
  46. char  *argv[] ;
  47. {
  48.    char              ch = NULL, header[2], path[100] ;
  49.    int               i = 0, result = 1, lockset = 0, win = 1, cont, iscomp = 0 ;
  50.    struct FileLock   *lock, *oldlock ;
  51.    struct FileHandle *fum ;
  52.    BPTR              pathlock ;
  53.    
  54.    IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 0) ;
  55.    GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0) ;
  56.  
  57.    screendata = (struct Screen *)AllocMem(30, MEMF_PUBLIC) ;
  58.    GetScreenData (screendata, 30, WBENCHSCREEN, NULL) ;
  59.         
  60.    stcgfp (path, argv[0]) ;
  61.    if (strcmp (path, "")) 
  62.     if (path[strlen(path)-1] !=':')
  63.       strcat (path, "/") ;
  64.    strcat (path, "headers.z") ;
  65.    if ((lock = (struct FileLock *)Lock(path, ACCESS_READ)) == 0)
  66.     { if ((pathlock = findpath("headers.z")) != -1)
  67.        { oldlock = (struct FileLock *)CurrentDir("df0:", ACCESS_READ) ;
  68.          CurrentDir((struct FileLock *)pathlock, ACCESS_READ) ;
  69.          lockset = 1 ;
  70.        }
  71.     }     
  72.    else UnLock (lock) ;   
  73.    
  74.    if ((fi = (struct FileHandle *)Open(path, MODE_OLDFILE)) == NULL) 
  75.      { puts ("No headers.z file\n") ;
  76.        cleanup()  ;
  77.      }
  78.  
  79.    Seek (fi, 0, 1) ;
  80.    insize = Seek (fi, 0, -1) ;
  81.    Read (fi, &header, 2); 
  82.    if ((header[0] == 'L') && (header[1] == 'H'))
  83.      { iscomp = 1 ;
  84.        Read (fi, &fesize, 4) ;
  85.        fesize = fesize + 4 ;
  86.        Seek (fi, 2, -1) ;
  87.        insize = insize - 2 ;
  88.      }
  89.    else   Seek (fi, 0, -1) ;
  90.  
  91.    if ((inbuf = (char *)AllocMem(insize, MEMF_PUBLIC)) == NULL)
  92.      { puts ("Insufficient memory\n") ;
  93.        cleanup() ;
  94.      }
  95.     
  96.    Read (fi, inbuf, insize) ;
  97.  
  98.    if (iscomp)
  99.     { if ((febuf = (char *)AllocMem((fesize), MEMF_PUBLIC)) == NULL)
  100.        { puts ("Insufficient memory\n") ;
  101.          cleanup() ;
  102.        }
  103.       if (iscomp)
  104.         {  SetPointer (IntuitionBase->ActiveWindow, &ptrImage, 7, 16, -4, -4) ;
  105.            result = DECOMPA (inbuf, febuf) ;
  106.            ClearPointer(IntuitionBase->ActiveWindow) ;
  107.            FreeMem (inbuf, insize) ;
  108.         }           
  109.     }
  110.    else
  111.        { febuf = inbuf ;
  112.          fesize = insize ;
  113.        }
  114.  
  115.    Close (fi) ;
  116.    if      (result == 0)  puts ("Decompression failed\n") ;
  117.    else if (result == 2)  puts ("Out of memory\n") ;
  118.     
  119.    if (argc == 2)
  120.     { fum = (struct FileHandle *)Open(argv[1], MODE_NEWFILE) ;
  121.       Write (fum, febuf, fesize) ;
  122.       Close (fum) ;
  123.     } 
  124.  
  125.    window = (struct FileHandle *)Open
  126.        ("CON:100/0/344/30/Hextract V1.2 - Enter 'quit' to exit", MODE_NEWFILE) ;
  127.    Write (window, "\n Enter symbol: ", 15) ;
  128.    if (lockset)
  129.      { CurrentDir (oldlock, ACCESS_READ) ;
  130.        UnLock ((struct FileLock *)pathlock) ;
  131.      }
  132.    
  133.    while (1)
  134.     { cont = 0 ;  n = 0 ;
  135.       if (WaitForChar (window, 999999999)) 
  136.       { Read (window, &ch, 1) ;
  137.         buffer[i++] = ch ;
  138.         if (ch == '\n')
  139.          { /*Ctime (1) ;*/
  140.           buffer[--i] = '\0' ;
  141.           if (i)
  142.            { if (!stricmp (buffer, "quit"))   cleanup() ;
  143.              else
  144.               { if (!stricmp (buffer, "ok"))
  145.                 {
  146.                  if (!win) 
  147.                   { Close(window) ;
  148.                     window = (struct FileHandle *)Open("CON:100/0/400/30/Hextract V1.2", MODE_NEWFILE) ;
  149.                     Write (window, "\n  Enter symbol: ", 17) ;
  150.                     win = 1 ;  cont = 1 ;
  151.                   }
  152.                  else { i = 0 ; continue ; }
  153.                 }
  154.                 if (!cont)
  155.                  { if (win)
  156.                     { Close (window) ;
  157.                       getwin() ;
  158.                       win = 0 ;
  159.                       Write (window, "\n     ", 6) ;
  160.                       Write (window, buffer, strlen(buffer)) ;
  161.                       Write (window, "\n", 1) ;
  162.                     }  
  163.                    SetPointer (IntuitionBase->ActiveWindow, &ptrImage, 7, 16, -4, -4) ;
  164.                    search () ; 
  165.                    memset (buffer, '\0', 100) ;
  166.                    ClearPointer(IntuitionBase->ActiveWindow) ;
  167.                    Write (window, "\n  Enter symbol\n\n   ", 19) ;
  168.               } }
  169.              i = 0 ; 
  170.             } /*Cend(1) ;*/
  171.           } 
  172.      } }
  173.  
  174.    cleanup() ;
  175. }
  176.  
  177. void getwin()
  178. {
  179.    if (screendata->Height > 511)
  180.     window = (struct FileHandle *)Open("CON:0/0/640/512/Hextract V1.2", MODE_NEWFILE) ;
  181.    else if (screendata->Height >399)
  182.     window = (struct FileHandle *)Open("CON:0/0/640/400/Hextract V1.2", MODE_NEWFILE) ;
  183.    else if (screendata->Height >255)
  184.     window = (struct FileHandle *)Open("CON:0/0/640/256/Hextract V1.2", MODE_NEWFILE) ;
  185.    else 
  186.     window = (struct FileHandle *)Open("CON:0/0/640/200/Hextract V1.2", MODE_NEWFILE) ;
  187. }
  188.  
  189.  
  190. void cleanup ()
  191. {
  192.      FreeMem (screendata, 30) ;
  193.      if (febuf)  FreeMem (febuf, fesize) ;
  194.      if (window)  Close (window) ;
  195.      if (GfxBase != NULL) CloseLibrary(GfxBase);
  196.      if (IntuitionBase != NULL) CloseLibrary(IntuitionBase);
  197.      /*Creport() ;*/
  198.      exit(0);
  199. }
  200.  
  201. void search()
  202. {  
  203.    char   *tok, *tok2, temp[256], *buf1, source[40], change = 0 ; 
  204.    int    numbrackets = 0, libh ;
  205.  
  206.    located = 0 ;
  207.    libh = 0 ;  
  208.    while (fgts (line) != 0)
  209.     { /*Ctime(2) ;*/ 
  210.       onbreak(&cleanup) ;
  211.       chkabort() ;
  212.       buf1 = stpblk(line) ;
  213.       if (!change)
  214.         if (!strcmp (buf1, "FF\n"))   change = 1 ; 
  215.  
  216.       if (change)
  217.        { if (stcpmw (buffer,".??????"))
  218.           { showlib() ;
  219.             break ;
  220.           }  
  221.          if (stcpmw (buf1, ".??????")) 
  222.           { tok = strtok (buf1, " \n") ;
  223.             strcpy (source, tok) ;
  224.             libh = 1 ;
  225.           }
  226.        }
  227.       /*Cend(2) ;*/  
  228.       strcpy (temp, buf1) ;
  229.       tok = strtok (temp, " \n") ;
  230.       if (stcpmwa (temp,buffer))
  231.        { located = 1 ;        
  232.          strcpy (temp, line) ;
  233.          tok = strtok (line, " \n") ;
  234.          tok2 = strtok (NULL, "\n") ;
  235.          Write (window, "\n   ", 4) ; 
  236.          Write (window, tok2, strlen(tok2)) ;
  237.          if (libh)
  238.           { Write (window, "  ", 2) ;
  239.             Write (window, source, strlen(source)) ;
  240.           }
  241.          Write (window, "\n", 1) ;
  242.  
  243.          if (stcpmw (temp, "struct")) 
  244.           { fgts (temp) ;
  245.             if (strchr (temp, '{'))
  246.              { numbrackets++ ;
  247.                Write (window, "    ", 4) ; 
  248.                Write (window, temp, strlen(temp)) ;
  249.                while  (numbrackets) 
  250.                 { fgts (temp) ;
  251.                   if (strchr (temp, '{'))
  252.                    { numbrackets++ ;
  253.                      Write (window, "    ", 4) ;
  254.                      Write (window, temp, strlen(temp)) ;
  255.                      continue ;
  256.                    }
  257.                   else if (strchr (temp, '}'))
  258.                    { numbrackets-- ;
  259.                      Write (window, temp, strlen(temp)) ;
  260.                      continue ;
  261.                    }
  262.  
  263.                   tok = strtok (temp, " \0") ;
  264.                   tok2 = strtok (NULL, " \0") ;
  265.                   Write (window, "    ", 4) ;
  266.                   Write (window, tok2, strlen(tok2)) ;
  267.  
  268.                   do
  269.                    { tok2 = strtok (NULL, " \0") ;
  270.                      if (stcpmw (tok2, "struct"))
  271.                        { Write (window, "\n", 1) ;
  272.                          break ;
  273.                        } 
  274.                      Write (window, " ", 1) ;
  275.                      Write (window, tok2, strlen(tok2)) ;
  276.                    } while (tok2) ; 
  277.              }  }
  278.           }
  279.  
  280.          tok = strtok (line, " \n") ;
  281.          tok2 = strtok (NULL, "\n") ;
  282.          Write (window, "\n       ", 8) ; 
  283.          Write (window, tok2, strlen(tok2)) ;
  284.       } 
  285.     }
  286.    if (!located)  Write (window, "        Not found\n", 18) ;
  287. }
  288.  
  289.  
  290. void showlib()
  291. {
  292.   char  *tok1, *tok2, buf[256] ;
  293.   int num, i ;
  294.  
  295.   
  296.   if (stcpmw (buffer,".device") || stcpmw (buffer,".resource")
  297.                                       || stcpmw (buffer,".library"))
  298.     { while (!stcpmw (line, buffer))
  299.        if (!fgts (line)) {  libh = 0 ;  return ; }
  300.     }
  301.   else { libh = 0 ;
  302.          return ;
  303.        } 
  304.    
  305.   tok1 = strtok (line, " \n") ;
  306.   tok2 = strtok (NULL, " \n") ;
  307.   num = atoi (tok2) ;
  308.   for (i = 0 ; i < num ; ++i)
  309.    { fgts (buf) ;
  310.      tok1 = strtok (buf, " \n") ;
  311.      tok2 = strtok (NULL, "\0") ;
  312.      Write (window, "   ", 3) ;
  313.      Write (window, tok2, strlen(tok2)) ;
  314.    }
  315.   located = 1 ;
  316. }
  317.