home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume10 / cbw / part02 / gblock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-06-16  |  3.9 KB  |  195 lines

  1. /*
  2.  * Guess block and its label.
  3.  *
  4.  * Robert W. Baldwin, December 1984.
  5.  */
  6.  
  7.  
  8. #include    <stdio.h>
  9. #include    "window.h"
  10. #include    "terminal.h"
  11. #include    "layout.h"
  12. #include    "specs.h"
  13. #include    "cipher.h"        /* RWB */
  14. #include    "autotri.h"
  15.  
  16.  
  17.  
  18. /* Private buffers for plaintext guessing. */
  19.  
  20. char    gcbuf[BLOCKSIZE+1];
  21. int        gpbuf[BLOCKSIZE+1];
  22. int        gperm[BLOCKSIZE+1];
  23. ecinfo    gecinfo;            /* RWB */
  24.  
  25. extern    atrinfo    gatrinfo;
  26. extern    char    mcbuf[];
  27.  
  28. /* Window for the guess block label. */
  29.  
  30. displine    gblline1 = {
  31.         GBLROW,1,        /* Origin. */
  32.         1,GBWIDTH,        /* Height and width. */
  33.         1,1,            /* Initial (relative) cursor pos. */
  34.         NULL,            /* No private data. */
  35.         wl_setcur,        /* Firstime = restore cursor pos. */
  36.         wl_noop,        /* Lasttime = do nothing. */
  37.         wl_dldraw,        /* Default dispaly line draw routine */
  38.         dokey,            /* Default keystroke handler. */
  39.         arwktab,        /* Basic arrow keystroke handler. */
  40.         1,GBWIDTH,        /* Min and Max col for cursor line */
  41. };
  42.  
  43. displine    *gbllines[] = {        /* List of display lines for the label. */
  44.             &gblline1,
  45.             NULL,
  46.             };
  47.  
  48. twindow        gblabel = {
  49.             GBLROW,1,        /* Origin. */
  50.             1,GBWIDTH,        /* Height and width. */
  51.             1,1,            /* Initial (relative) cursor position */
  52.             NULL,            /* No private data. */
  53.             wl_setcur,        /* Firstime = restore cursor position. */
  54.             wl_noop,        /* Lasttime = do nothing. */
  55.             wl_twdraw,        /* Simple draw routine. */
  56.             dokey,            /* Default keystroke handler. */
  57.             arwktab,        /* Basic arrow keystroke handler. */
  58.             gbllines,
  59.             };
  60.  
  61.  
  62. /* Window for the guess block. */
  63.  
  64. gwindow    gbstore = {
  65.         GBSROW,1,        /* Origin. */
  66.         GBHEIGHT,GBWIDTH,    /* Height and width. */
  67.         1,1,            /* Initial cursor position */
  68.         NULL,            /* Private data. */
  69.         wl_setcur,        /* Firstime = accept cursor pos. */
  70.         wl_noop,        /* Lasttime = do nothing. */
  71.         wl_outline,        /* Simple draw routine. */
  72.         dokey,            /* Default keystroke handler. */
  73.         arwktab,        /* Keystroke table. */
  74. };
  75.  
  76.  
  77.  
  78. /* Initialize the guess block label, and return a ptr to it.
  79.  */
  80. gwindow    *(igblabel())
  81. {
  82.     displine    *line;
  83.  
  84.     line = gblabel.dlines[0];
  85.     setadline(line, GBLTEXT);
  86.     return ((gwindow *) &gblabel);
  87. }
  88.  
  89.  
  90. /* Set the label to indicate the given string.
  91.  * Redisplay the label.
  92.  */
  93. gblset(label, str)
  94. twindow    *label;
  95. char    *str;
  96. {
  97.     int        row,col;
  98.     displine    *line;
  99.  
  100.     row = rowcursor();
  101.     col = colcursor();
  102.  
  103.     line = label->dlines[0];
  104.     setadline(line, str);
  105.     (*(line->wredraw))(line);
  106.  
  107.     setcursor(row, col);
  108. }
  109.  
  110.  
  111.  
  112. /* Initialize the guess block storage, and return a ptr to it.
  113.  * Start with the Trigram guessing window.
  114.  */
  115. gwindow    *(igbstore())
  116. {
  117.     extern    atrdraw();
  118.     extern    atr_init();
  119.     atrinfo    *atri;
  120.  
  121.     atri = &gatrinfo;
  122.     atr_init(mcbuf, refperm(dbsgetblk(&dbstore)), atri);
  123.     gbsswitch(&gbstore, ((char *) atri), arwktab, wl_setcur, wl_noop, atrdraw);
  124.     return (&gbstore);
  125. }
  126.  
  127.  
  128. /* Switch the guessing window to the desired functionality
  129.  * by changing the keytable, private data pointer, firsttime,
  130.  * and lasttime function routines.
  131.  */
  132. gbsswitch(gbs, private, keytable, firsttime, lasttime, draw)
  133. gwindow    *gbs;
  134. char    *private;
  135. keyer    keytable[];
  136. int        (*firsttime)(), (*lasttime)();
  137. int        (*draw)();
  138. {
  139.     gbs->wprivate = private;
  140.     gbs->wkeyprocs = keytable;
  141.     gbs->wfirst = firsttime;
  142.     gbs->wlast = lasttime;
  143.     gbs->wredraw = draw;
  144. }
  145.  
  146.  
  147. /* Convert block position to relative row/column coordinate.
  148.  */
  149. int    gbspos2row(pos)
  150. int    pos;
  151. {
  152.     return(1 + (pos/LINELEN));
  153. }
  154.  
  155.  
  156. /* Convert block position to relative row/column coordinate.
  157.  */
  158. int    gbspos2col(pos)
  159. int    pos;
  160. {
  161.     return(1 + (pos%LINELEN));
  162. }
  163.  
  164.  
  165. /* Clear out the guess block marking all character positions as unknown.
  166.  */
  167. gbsclear(gbs)
  168. gwindow    *gbs;
  169. {
  170.     int            i;
  171.     int            row, col;
  172.  
  173.     row = 1;
  174.     col = 1;
  175.  
  176.     for (i = 0 ; i < BLOCKSIZE ; i++)  {
  177.         if (i%LINELEN == 0) {
  178.             wl_setcur(gbs, gbspos2row(i), gbspos2col(i));
  179.             }
  180.         plnchars(1, char2sym(NONE));
  181.         }
  182.  
  183.     for (i = gbspos2row(BLOCKSIZE) ; i <= GBHEIGHT ; i++) {
  184.         wl_setcur(gbs, i, 1);
  185.         plnchars(LINELEN, ' ');
  186.         }
  187.  
  188.     for (i = 1 ; i <= GBHEIGHT ; i++) {
  189.         wl_setcur(gbs, i, LINELEN+1);
  190.         plnchars(gbs->wwidth - LINELEN, ' ');
  191.         }
  192.  
  193.     wl_setcur(gbs, row, col);
  194. }
  195.