home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / uniflex.zip / ufcutl.c < prev    next >
C/C++ Source or Header  |  1993-08-23  |  9KB  |  260 lines

  1. #include "ufk.h"
  2.  
  3. /*
  4.  *  s p a r
  5.  *
  6.  *  Fill the data array with my send-init parameters
  7.  *
  8.  */
  9.  
  10. spar(data,len)
  11. char data[];
  12. int  *len;
  13. {
  14.    data[0] = tochar(maxpacksiz);          /* Biggest packet I can receive */
  15.    data[1] = tochar(mytime);              /* When I want to be timed out */
  16.    data[2] = tochar(mypad);               /* How much padding I need */
  17.    data[3] = ctl(mypchar);                /* Padding character I want */
  18.    data[4] = tochar(myeol);               /* End-Of-Line character I want */
  19.    data[5] = myquote;                     /* Control-Quote character I send */
  20.    if ((data[6] = myeightquote) == 0)
  21.       data[6] = 'N';                      /* No eight bit quoting */
  22.    else if (config > 3)                   /* 8 bits link */
  23.       data[6] = 'Y';                      /* 8th bit quoting on request */
  24.    data[7] = myblock_check_type + '0';    /* Type of block check */
  25.    if ((data[8] = myrptquote) == 0)       /* Repeat count quote character */
  26.       data[8] = ' ';                      /* No repeat quoting done */
  27.    *len = 10;
  28.    if (allowattr)
  29.       data[9] = tochar(0x08);             /* Set capability bit for attr */
  30.    else
  31.       data[9] = tochar(0);                /* Not used */
  32.    if (maxpacksiz > 94)                   /* Use extended packet length ? */
  33.    {
  34.       data[0] = tochar(I_MYPACKSIZE);     /* Use default if long packets */
  35.       data[9] = tochar(0x0a);             /* Set capability bit for this */
  36.       data[10] = tochar(0);               /* No sliding windows */
  37.       data[11] = tochar(maxpacksiz / 95); /* Send packet length */
  38.       data[12] = tochar(maxpacksiz % 95);
  39.       *len = 13;                          /* Count all this */
  40.    }
  41.    data[*len] = '\0';                     /* For debug printout */
  42. }
  43.  
  44.  
  45. /*  r p a r
  46.  *
  47.  *  Get the other host's send-init parameters
  48.  *
  49.  */
  50.  
  51. rpar(data,len)
  52. char data[];
  53. int  len;
  54. {
  55.    char c;
  56.  
  57.    if (len > 0)
  58.       spsiz = unchar(data[0]);            /* Maximum send packet size */
  59.    else
  60.       spsiz = I_MYPACKSIZE;               /* Use default */
  61.    if (len > 1)
  62.       timint = unchar(data[1]);           /* When I should time out */
  63.    else
  64.       timint = I_MYTIME;
  65.    if (len > 2)
  66.       pad = unchar(data[2]);              /* Number of pads to send */
  67.    else
  68.       pad = I_MYPAD;
  69.    if (len > 3)
  70.       padchar = ctl(data[3]);             /* Padding character to send */
  71.    else
  72.       padchar = I_MYPCHAR;
  73.    if (len > 4)
  74.       eol = unchar(data[4]);              /* EOL character I must send */
  75.    else
  76.       eol = I_MYEOL;
  77.    if (len > 5)
  78.       quote = data[5];                    /* Incoming data quote character */
  79.    else
  80.       quote = I_MYQUOTE;
  81.    if (len > 6)
  82.    {
  83.       if ((c = data[6]) == 'N')
  84.          eight_quote = 0;                 /* Don't use eight_bit quoting */
  85.       else if (c == 'Y')
  86.       {
  87.          if (config <= 3)                 /* 7 bits link */
  88.             eight_quote = myeightquote;   /* Use default */
  89.          else
  90.             eight_quote = 0;              /* Don't use it */
  91.       }
  92.       else
  93.          eight_quote = c;          /* Use this char for eight bit quoting */
  94.    }
  95.    else
  96.       eight_quote = 0;                    /* Don't use it */
  97.    if ((len > 7) && ((c = data[7] - '0') == myblock_check_type))
  98.       block_check_type = c;               /* Setup block check type */
  99.    else
  100.       block_check_type = I_BLOCKCHECKTYPE;/* Set initial value */
  101.    if ((len > 8) && ((c = data[8]) == myrptquote) && (c != ' '))
  102.       repeat_quote = c;                   /* Setup repeat quote character */
  103.    else
  104.       repeat_quote = 0;                   /* Don't use repeat quoting */
  105.    if (len > 9)
  106.    {
  107.       if ((unchar(data[9]) & 0x08) &&   /* Check for attribute packets */
  108.           allowattr)
  109.          attribute = TRUE;              /* We can handle it */
  110.       if (unchar(data[9]) & 0x02)
  111.       {            /* The remote kermit can handle extended length packets */
  112.          c = 9;                         /* Check for more capability bytes */
  113.          while (c < len)
  114.             if (!(unchar(data[c++]) & 0x01))
  115.                break;                     /* End of capability bytes */
  116.          spsiz = maxpacksiz;              /* Use default */
  117.          if (maxpacksiz > 94)             /* Do I want extended length ? */
  118.          {                                /* Yes */
  119.             if ((c == len) || (c == (len - 1))) /* No length specified */
  120.                spsiz = I_DEFLONGSIZE;     /* Use default long size */
  121.             else
  122.             {
  123.                c++;                   /* Skip window size (not implemented) */
  124.                spsiz = 95 * unchar(data[c]) + /* Re-construct packet length */
  125.                             unchar(data[c+1]);
  126.             }
  127.          }
  128.       }
  129.    }
  130.    disp_params();                         /* Show all selected parameters */
  131. }
  132.  
  133. alloc_pkt(type)
  134. char type;
  135. {
  136.    char *p, *q, *malloc();
  137.  
  138.    dealloc_pkt();                             /* Get rid of old memory */
  139.    if (((p = malloc(BIG_SIZE)) == NULL) ||    /* Allocate packet buffers */
  140.        ((q = malloc(SMALL_SIZE)) == NULL))    /* for send and receive */
  141.    {
  142.       prterr(ER_NOMEM);                       /* No memory */
  143.       return(FALSE);
  144.    }
  145.    if (type == RECEIVE)
  146.    {
  147.       recpkt = p;                               /* Receive buffer is large */
  148.       sndpkt = q;                               /* Send buffer is small */
  149.    }
  150.    else if (type == SEND)
  151.    {
  152.       recpkt = q;                               /* Receive buffer is small */
  153.       sndpkt = p;                               /* Send buffer is large */
  154.    }
  155.    return (TRUE);
  156. }
  157.  
  158. dealloc_pkt()
  159. {
  160.    if (recpkt)                          /* De-allocate use receive packet */
  161.    {
  162.       free(recpkt);
  163.       recpkt = 0;
  164.    }
  165.    if (sndpkt)                          /* De-allocate used send packet */
  166.    {
  167.       free(sndpkt);
  168.       sndpkt = 0;
  169.    }
  170. }
  171.  
  172. set_default_comm()
  173. {
  174.    eol = myeol;                           /* EOL for outgoing packets */
  175.    quote = myquote;                       /* Standard control-quote char "#" */
  176.    pad = mypad;                           /* No padding */
  177.    padchar = mypchar;                     /* Use null if any padding wanted */
  178.    timint = mytime;                       /* Timeout value */
  179.    eight_quote = myeightquote;            /* Set eight_bit quote character */
  180.    repeat_quote = myrptquote;             /* Set repeat quote character */
  181.    block_check_type = I_BLOCKCHECKTYPE;   /* Set initial value */
  182.    attribute = FALSE;                     /* No attribute packets yet */
  183.    binfil = image;                        /* Reset file type */
  184. }
  185.  
  186. disp_params()                             /* Show parameter settings */
  187. {                                         /* After negotiation */
  188.    char tmp1, tmp2;
  189.  
  190.    if (debug && screen && (sflg || rflg)) /* Only if sending or receiving */
  191.    {
  192.       posit(0,15);
  193.       printf("Packet length: %4d   Timeout:       %2d   Block check type:  %d",
  194.              spsiz, timint, block_check_type);
  195.       posit(0,16);
  196.   printf("End of line:     %02x   Padding char:  %02x   Pad count:        %2d",
  197.              eol, padchar, pad);
  198.       if ((tmp1 = repeat_quote) == 0)
  199.          tmp1 = 'N';
  200.       if ((tmp2 = eight_quote) == 0)
  201.          tmp2 = 'N';
  202.       posit(0,17);
  203.     printf("Control quote:    %c   Repeat quote:   %c   Eight bit quote:   %c",
  204.              quote, tmp1, tmp2);
  205.    }
  206. }
  207.  
  208. disp_size_transferred()
  209. {
  210.    float xmitted,
  211.          percentage;
  212.  
  213.    if (!remote && (transmit_chr_count != 0) && screen && !nooutput)
  214.    {
  215.       xmitted = transmit_chr_count / 1024.; /* Size transferred so far in K */
  216.       posit(18,4);
  217.       printf("%6.2f K", xmitted);
  218.       if (sflg || (rflg && attribute && (file_size != 0)))
  219.       {
  220.          percentage = 100. * ((float)transmit_chr_count / (float)file_size);
  221.          posit(39,4);
  222.          printf("%3.0f %%", percentage);
  223.       }
  224.    }
  225. }
  226.  
  227. init_xfer()
  228. {
  229.    chr_sent = dchr_sent = chr_rec = dchr_rec = 0; /* zero counters */
  230.    nak_sent = nak_rec = pack_sent = pack_rec = 0;
  231.    data_rate = 0;
  232.    start_time = time(0);
  233. }
  234.  
  235. fin_xfer()
  236. {
  237.    int numbits;
  238.  
  239.    t_chr_sent += chr_sent;                /* adjust performance counters */
  240.    t_dchr_sent += dchr_sent;
  241.    t_nak_sent += nak_sent;
  242.    t_pack_sent += pack_sent;
  243.    t_chr_rec += chr_rec;
  244.    t_dchr_rec += dchr_rec;
  245.    t_nak_rec += nak_rec;
  246.    t_pack_rec += pack_rec;
  247.  
  248.    end_time = time(0);              /* Get current time */
  249.  
  250.    /* calculate number of bits in transmission */
  251.  
  252.    numbits = (config == 2 || config == 3 || config == 5) ? 10 : 11;
  253.    if (rflg)                        /* received data */
  254.       data_rate = (chr_rec / (end_time - start_time)) * (100 / numbits);
  255.    else if (sflg)                   /* transmitted data */
  256.       data_rate = (chr_sent / (end_time - start_time)) * (100 / numbits);
  257.    t_data_rate = (((t_data_rate == 0) ? data_rate : t_data_rate)
  258.                  + data_rate) / 2;
  259. }
  260.