home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3.4.17 [SPARC, PA-RISC] / nextstep33_risc.iso / NextLibrary / TeX / tex / src / texview / random.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-15  |  5.4 KB  |  228 lines

  1. /*
  2.  *   This software is Copyright 1988 by Radical Eye Software.
  3.  *   All Rights Reserved.
  4.  */
  5. /*
  6.  *   Random page motion file for dviamiga.c (now called preview.c.)
  7.  */
  8. #include "structures.h"
  9. /*
  10.  *   The data structure we need for the linked list of pages.
  11.  */
  12. struct pagetype {
  13.    struct pagetype *next ;
  14.    integer page, no, pos ;
  15. } *pages = NULL ;
  16. struct pagetype *last = NULL ;
  17. struct pagetype *currentpage = NULL ;
  18. /*
  19.  *   Some externals we need to get to.
  20.  */
  21. extern integer curpos ;
  22. extern FILE *dvifile ;
  23. extern integer prevpage, thispage, rthispage ;
  24. extern integer pagenum, seq ;
  25. extern integer numalloc, memalloc ;
  26. extern struct Screen *myscreen ;
  27. extern Boolean pageinterrupted ;
  28. extern struct pagetype *mymalloc() ;
  29. extern integer signedquad() ;
  30. extern int skipnop() ;
  31. extern int dvierror ;
  32. extern long lseek() ;
  33. extern void free() ;
  34. extern void abortdvi() ;
  35. extern void skipover() ;
  36. extern void skippage() ;
  37. extern int debugon ;
  38. extern void qstatus() ;
  39. extern Boolean dualpage ;
  40. extern Boolean pagedrawn ;
  41. extern integer fakeeof ;
  42. /*
  43.  *   This 'dviseek' routine should be faster than the usual one.
  44.  *   Now we also need to check for writing, and some code needs
  45.  *   to be changed to handle a no page dvi file.
  46.  */
  47. int lastwaswrite ;
  48. void dviseek(where)
  49. register integer where ;
  50. {
  51.    if (debugon > 6)
  52.       printf("Seeking to %ld\n", where) ;
  53.    fseek(dvifile, where, 0) ;
  54.    curpos = where ;
  55. }
  56. /*
  57.  *   This routine is called when a dvi file is closed to release all memory
  58.  *   associated with the pages and to reset the variables to NULL.
  59.  */
  60. void freepages()
  61. {
  62.    register struct pagetype *p ;
  63.  
  64.    while (pages != NULL) {
  65.       p = pages->next ;
  66.       free(pages) ;
  67.       pages = p ;
  68.    }
  69.    pages = last = currentpage = NULL ;
  70. }
  71. /*
  72.  *   This routine adds a new page to the list we are constructing.
  73.  */
  74. struct pagetype *addpage(page, where)
  75. integer page ;
  76. register integer where ;
  77. {
  78.    register struct pagetype *p ;
  79.    register integer c = 0 ;
  80.  
  81.    if (last == NULL || where > last->pos) {
  82.       p = mymalloc(sizeof(struct pagetype), MEMF_CLEAR) ;
  83.       if (last == NULL)
  84.          pages = p ;
  85.       else
  86.          last->next = p ;
  87.       last = p ;
  88.       p->page = page ;
  89.       p->pos = where ;
  90.       p->next = NULL ;
  91.       for (p=pages; p!=NULL; p=p->next)
  92.          if (page==p->page)
  93.             c++ ;
  94.       last->no = c - 1 ;
  95.       seq = c - 1 ;
  96.       return(last) ;
  97.    } else {
  98.       for (p=pages; p!=NULL; p=p->next)
  99.          if (p->pos == where) {
  100.             seq = p->no ;
  101.             return(p) ;
  102.          }
  103.    }
  104.    return 0 ;
  105. }
  106. /*
  107.  *   Here we add a page to the list the first time we scan it.  Only if it
  108.  *   is the first time, though; we check it by looking at the current
  109.  *   position and the position of the last page.   (By the way, the position
  110.  *   we are referring to is always the location of the bop.)
  111.  */
  112. int processbop()
  113. {
  114.    register int cmd ;
  115.    register long waswhere = curpos ;
  116.  
  117.    if (debugon > 6)
  118.       printf("Processbop at %ld\n", curpos) ;
  119.    cmd = skipnop() ;
  120.    if (cmd==248 || cmd < 0) {
  121.       dviseek(waswhere) ;
  122.       return 0 ;
  123.    } else if (cmd!=139) {
  124.       abortdvi() ;
  125.    }
  126.    thispage = curpos - 1 ;
  127.    pagenum = signedquad() ;
  128.    currentpage = addpage(pagenum, thispage) ;
  129.    skipover(36) ;
  130.    prevpage = signedquad() ;
  131.    rthispage = curpos ;
  132.    return(1) ;
  133. }
  134.  
  135. void missbop() {
  136.    if (! processbop())
  137.       abortdvi() ;
  138. }
  139. /*
  140.  *   Here we look for a page and a number.  If it is not found, we skip to
  141.  *   the end and scan forward until we find it.  When we find it, we seek
  142.  *   to the correct location and return.  If it is not found, we return false.
  143.  */
  144. Boolean seekpage(page, no)
  145. integer page, no ;
  146. {
  147.    register struct pagetype *p ;
  148.  
  149.    for (p=pages; p!=NULL; p=p->next) {
  150.       if (page==p->page && no==p->no) {
  151.          thispage = p->pos ;
  152.          dviseek(thispage) ;
  153.          if (!processbop())
  154.             abortdvi() ;
  155.          pagedrawn = 0 ;
  156.          return(1) ;
  157.       }
  158.    }
  159.    qstatus("Looking for page") ;
  160.    if (last == NULL)
  161.       dviseek(thispage) ;
  162.    else
  163.       dviseek(last->pos) ;
  164.    for (;;) {
  165.       if (!processbop()) {
  166.          dviseek(thispage) ;
  167.          missbop() ;
  168.          pagedrawn = 0 ;
  169.          return(0) ;
  170.       }
  171.       if (last->page == page && last->no == no) {
  172.          pagedrawn = 0 ;
  173.          return(1) ;
  174.       }
  175.       skippage() ;
  176.    }
  177. }
  178. /*
  179.  *   This one goes to a particular page.  -1 means back one, 0 means same
  180.  *   page, 1 means next page.
  181.  */
  182. int relativepage(loc)
  183. register int loc ;
  184. {
  185. #ifdef DEBUG
  186.         if (debugon > 7) {
  187.            printf("\nRelativepage %d %ld\n", loc, curpos) ;
  188.            fflush(stdout) ;
  189.         }
  190. #endif
  191.    if (loc == 0) {
  192.       dviseek(thispage) ;
  193.       missbop() ;
  194.       return 1 ;
  195.    } else if (loc == 1) {
  196. /*    if (pageinterrupted) {
  197.          if (currentpage == NULL || currentpage->next == NULL) {
  198.             dviseek(curpos) ;
  199.             skippage() ;
  200.          } else {
  201.             dviseek(currentpage->next->pos) ;
  202.          }
  203.       }
  204.       if (dualpage && ! pageinterrupted) {
  205.          if (currentpage == NULL)
  206.             error("! this can't happen foo") ;
  207.          if (currentpage != NULL && currentpage->next != NULL)
  208.             dviseek(currentpage->next->pos) ;
  209.       } */
  210.       if (!processbop()) {
  211.          return 0 ;
  212.       } else
  213.          pagedrawn = 0 ;
  214.    } else if (loc == -1) {
  215.       if (prevpage > 0) {
  216.          dviseek(prevpage) ;
  217.          missbop() ;
  218.          pagedrawn = 0 ;
  219.       } else
  220.          return 0 ;
  221.    } else {
  222.       dviseek(pages->pos) ;
  223.       missbop() ;
  224.       pagedrawn = 0 ;
  225.    }
  226.    return 1 ;
  227. }
  228.