home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / windows / winkerm.zip / WINMSC.C < prev    next >
C/C++ Source or Header  |  1986-10-10  |  11KB  |  405 lines

  1.  
  2. /***************************************************************************
  3. *
  4. * Winmsc.c
  5. *
  6. * This module contains utility routines used by other modules
  7. *
  8. ***************************************************************************/
  9. #include <windows.h>
  10. #include "winkrm.h"
  11.      
  12. /***************************************************************************
  13. *
  14. * About
  15. *
  16. * Displays an 'About' box
  17. *
  18. * Entry: Standard windows function parameters
  19. *
  20. * Exit: About box taken down (WM_COMMAND message) or default action.
  21. *
  22. * Note: This is the window procedure for the About dialog box.
  23. *
  24. ***************************************************************************/
  25. BOOL FAR PASCAL About(hDlg, message, wParam,lParam)
  26. HWND hDlg;
  27. unsigned message;
  28. WORD wParam;
  29. LONG lParam;
  30. {
  31.      
  32.     if (message == WM_COMMAND) {
  33.     EndDialog(hDlg, TRUE);
  34.     return(TRUE);
  35.     }
  36.     else if (message == WM_INITDIALOG)
  37.     return(TRUE);
  38.     else
  39.     return(FALSE);
  40.      
  41. }
  42.      
  43. /***************************************************************************
  44. *
  45. * cinchr
  46. *
  47. * cinchr gets a character from the communications port and adds its value
  48. * to the current checksum.
  49. *
  50. * Entry: None
  51. *
  52. * Exit: Next character returned and checksum updated.
  53. *
  54. * Notes: This reoutine is called during a protocol file transfer
  55. *
  56. ***************************************************************************/
  57. cinchr()
  58. {
  59.     char ch;
  60.      
  61.     ch = inchar();        /* get the character */
  62.     cchksum += ch;        /* add to checksum */
  63.     return(ch);
  64. }
  65.      
  66. /***************************************************************************
  67. *
  68. * inchar
  69. *
  70. * inchar is called during a protocol file transfer to return the next
  71. * character in the holding buffer.  If the buffer is empty, inchar attempts
  72. * to replenish it from the communications buffer.
  73. *
  74. * Entry: None
  75. *
  76. * Exit: Returns next character, or EOF if no more available.
  77. *
  78. ***************************************************************************/
  79. inchar()
  80. {
  81.      
  82.     static char RXBuf[100];        /* holding buffer */
  83.     static int RXBufCount = 0;        /* holding buffer counter */
  84.     static int RXBufPos;        /* holding buffer pointer */
  85.      
  86.     if (RXBufCount <= 0) {
  87.     if ((RXBufCount = GetBuf(RXBuf)) == 0)
  88.         return(EOF);
  89.     RXBufPos = 0;
  90.     }
  91.      
  92.     RXBufCount -= 1;
  93.     return(RXBuf[RXBufPos++]);
  94.      
  95. }
  96.      
  97. /***************************************************************************
  98. *
  99. * GetBuf
  100. *
  101. * This important routine fills a buffer passed to it from the commications
  102. * port buffer.
  103. *
  104. * Entry: character buffer
  105. *
  106. * Exit: number of characters placed in buffer or zero.
  107. *
  108. * Notes: GetBuf is also responsible for calling the Windows message function.
  109. *     If there are no messages, it checks the port for characters, and
  110. *     if found, it fills the buffer with as many characters as possible.
  111. *
  112. *     If there are no data from the commications port, GetBuf will simply
  113. *     read and dispatch messages.  The function never returns a zero
  114. *     value unless CurrentState, as selected by the user from the state menu
  115. *     is no longer equal to the state (RECEIVE or SEND) of the calling
  116. *     module.  When the latter occurs, GetBuf returns with 0.  Any function
  117. *     calling GetBuf, such as inchar above, must then act accordingly
  118. *     (such as returning EOF) since this is a signal that the user has
  119. *     selected a new state or is toggling the current state off.
  120. *
  121. *     Any errors in reading the comm port are ignored.
  122. ***************************************************************************/
  123. GetBuf(Buffer)
  124. char Buffer[];
  125. {
  126.      
  127.     COMSTAT ComStatus;
  128.     int num, result;
  129.      
  130.   /*
  131.      wait in this loop until either the buffer can be filled or
  132.      the selected state is changed.  If there are messages to process,
  133.      do them first.
  134.   */
  135.    while (CurrentState == ThisState) {
  136.     if (!DoMessage()) {        /* read messages */
  137.       /* see if anything in comm buffer */
  138.         GetCommError(cid, (COMSTAT FAR *)&ComStatus);
  139.       /* if found, retrieve them */
  140.         if ((num = ComStatus.cbInQue) != 0) {
  141.             result = ReadComm(cid,(LPSTR)Buffer,num >RXSIZE ? RXSIZE : num);
  142.         if (result)
  143.       /*
  144.          non-zero result means data found.  If negative, error
  145.          occurred, but ignore it in this implementation and return
  146.          number of characters found.
  147.       */
  148.             return(result > 0 ? result : -result);
  149.         }
  150.     }
  151.     }
  152.     return(0);    /* forced out of while loop, return 0 */
  153. }
  154.      
  155. /***************************************************************************
  156. *
  157. * spar
  158. *
  159. * fill a data packet with my send-init characters.
  160. *
  161. * Entry: data packet to be filled.
  162. *
  163. * Exit: data packet filled with my parameters.
  164. *
  165. * Note: used by foreign host to prepare a packet sent to me
  166. *    during a protocol file transfer.
  167. *
  168. ***************************************************************************/
  169. spar(data)
  170. char data[];
  171. {
  172.     data[0] = tochar(MAXPACKSIZ);    /* my max packet size */
  173.     data[1] = tochar(MYTIME);        /* my time out */
  174.     data[2] = tochar(MYPAD);        /* my number of pad characters */
  175.     data[3] = ctl(MYPCHAR);        /* my padding character */
  176.     data[4] = tochar(MYEOL);        /* my end of line character */
  177.     data[5] = MYQUOTE;            /* my quote character */
  178. }
  179.      
  180. /***************************************************************************
  181. *
  182. * rpar
  183. *
  184. * get the other side's send-init parameters.
  185. *
  186. * Entry: a data packet filled with the send-init parameters wanted
  187. *     by the other side during a protocol file transfer.
  188. *
  189. * Exit: packet variables updated to those needed by the other side.
  190. *
  191. ***************************************************************************/
  192. rpar(data)
  193. char data[];
  194. {
  195.     spsiz = unchar(data[0]);        /* max size of packet to send */
  196.     timint = unchar(data[1]);  /* wait this long before timing out other side */
  197.     pad = unchar(data[2]);        /* no. of pad chars to send */
  198.     padchar = ctl(data[3]);        /* which padding char to send */
  199.     eol = unchar(data[4]);        /* end of line char to send */
  200.     quote = data[5];            /* quote char other size wants */
  201. }
  202.      
  203. /***************************************************************************
  204. *
  205. * dopar
  206. *
  207. * currently a dummy routine
  208. *
  209. ***************************************************************************/
  210. dopar(ch)
  211. char ch;
  212. {
  213.     return (ch);
  214. }
  215.      
  216. /***************************************************************************
  217. *
  218. *
  219. ***************************************************************************/
  220. spack(type, num ,len, data)
  221. char type, *data;
  222. int num, len;
  223. {
  224.      
  225.     int i, k;
  226.     BYTE chksum;
  227.     char buffer[100];
  228.     char ShortStr[2];
  229.      
  230.     if (debug) {
  231.     if (data != NULL)
  232.         data[len] = '\0';
  233.         printf("spack type:  %c\n", type);
  234.     printf("       num:  %d\n", num);
  235.     printf("       len:  %d\n", len);
  236.     if (data != NULL)
  237.         printf("      data: \"%s\"\n", data);
  238.     }
  239.      
  240.     k = 0;
  241.     ShortStr[1] = '\0';
  242.     for (i = 1; i <= pad; i++) {
  243.     ShortStr[0] = padchar;
  244.         WriteComm(cid, (LPSTR)ShortStr,1);
  245.     }
  246.      
  247.     buffer[k++] = dopar(SOH);
  248.     buffer[k++] = dopar(tochar(len+3));
  249.     chksum = tochar(len+3);
  250.     buffer[k++] = dopar(tochar(num));
  251.     chksum += tochar(num);
  252.     buffer[k++] = dopar(type);
  253.     chksum += type;
  254.      
  255.     for (i = 0; i < len; i++) {
  256.     buffer[k++] = dopar(data[i]);
  257.     chksum += data[i];
  258.     }
  259.      
  260.     chksum = (((chksum & 0300) >> 6) + chksum)&077;
  261.     buffer[k++] = dopar(tochar(chksum));
  262.     buffer[k++] = dopar(eol);
  263.     buffer[k] = '\0';
  264.      
  265.     if (pktdeb)
  266.         fprintf(dpfp,"\r\nSpack:%s", buffer);
  267.      
  268.     WriteComm(cid, (LPSTR)buffer, strlen(buffer));
  269. }
  270.      
  271. /***************************************************************************
  272. *
  273. *
  274. ***************************************************************************/
  275. rpack(len,num,data)
  276. int *len, *num;
  277. char data[];
  278. {
  279.     int i, done;
  280.     int t;
  281.     char type;
  282.     BYTE rchksum;
  283.      
  284.     if (pktdeb)
  285.     fprintf(dpfp, "\r\nRpack:");
  286.      
  287.     while ((t = inchar()) != SOH)
  288.     if (t == EOF)
  289.         return(FALSE);
  290.      
  291.     done = FALSE;
  292.     while (!done) {
  293.     cchksum = 0;
  294.     if ((t = cinchr()) == SOH)
  295.         continue;
  296.     if (t == EOF)
  297.         return(FALSE);
  298.     *len = unchar(t) - 3;
  299.      
  300.     if ((t = cinchr()) == SOH)
  301.         continue;
  302.     if (t == EOF)
  303.         return(FALSE);
  304.     *num = unchar(t);
  305.      
  306.     if ((t = cinchr()) == SOH)
  307.         continue;
  308.     if (t == EOF)
  309.         return(FALSE);
  310.     type = t;
  311.      
  312.     for (i = 0; i < *len; i++) {
  313.         if ((t = cinchr()) == SOH)
  314.         continue;
  315.         if (t == EOF)
  316.         return(FALSE);
  317.         data[i] = t;
  318.     }
  319.     data[*len] = '\0';
  320.      
  321.     if ((t = inchar()) == SOH)
  322.         continue;
  323.     if (t == EOF)
  324.         return(FALSE);
  325.     rchksum = unchar(t);
  326.     done = TRUE;
  327.     }
  328.      
  329.     if (debug) {
  330.     if (data != NULL)
  331.         data[*len] = '\0';
  332.     printf("rpack type: %c\n", type);
  333.     printf("       num: %d\n", *num);
  334.     printf("       len: %d\n", *len);
  335.     if (data != NULL)
  336.         printf("      data: \"%s\"\n", data);
  337.     }
  338.      
  339.     cchksum = (((cchksum&0300) >> 6)+cchksum)&077;
  340.     if (cchksum != rchksum)
  341.     return (FALSE);
  342.     return(type);
  343.     
  344. }
  345.      
  346. /***************************************************************************
  347. *
  348. *
  349. ***************************************************************************/
  350. printmsg(mess)
  351. char *mess;
  352. {
  353.      
  354.     MessageBox(hKermWnd, (LPSTR)mess,
  355.         (LPSTR)szWinTitle, MB_OK | MB_ICONEXCLAMATION);
  356. }
  357.      
  358.      
  359. /***************************************************************************
  360. *
  361. *
  362. ***************************************************************************/
  363. prerrpkt(msg)
  364. char msg[];
  365. {
  366.      
  367.     MessageBox(hKermWnd, (LPSTR)msg,
  368.             (LPSTR)"Kermit Abort", MB_OK | MB_ICONEXCLAMATION);
  369. }
  370.      
  371. /***************************************************************************
  372. *
  373. *
  374. ***************************************************************************/
  375. GrayMenuItems(hWnd)
  376. HWND hWnd;
  377. {
  378.      
  379.     HMENU hMenu = GetMenu(hWnd);
  380.      
  381.     EnableMenuItem(hMenu, CONNECTSTATE, MF_GRAYED);
  382.     EnableMenuItem(hMenu, RECEIVESTATE, MF_GRAYED);
  383.     EnableMenuItem(hMenu, SENDSTATE, MF_GRAYED);
  384.     EnableMenuItem(hMenu, BAUD2400, MF_GRAYED);
  385.     EnableMenuItem(hMenu, BAUD1200, MF_GRAYED);
  386.     EnableMenuItem(hMenu, BAUD300, MF_GRAYED);
  387.      
  388. }
  389.      
  390. /***************************************************************************
  391. *
  392. *
  393. ***************************************************************************/
  394. UnGrayMenuItems(hMenu)
  395. HANDLE hMenu;
  396. {
  397.      
  398.     EnableMenuItem(hMenu, CONNECTSTATE, MF_ENABLED);
  399.     EnableMenuItem(hMenu, RECEIVESTATE, MF_ENABLED);
  400.     EnableMenuItem(hMenu, SENDSTATE, MF_ENABLED);
  401.     EnableMenuItem(hMenu, BAUD2400, MF_ENABLED);
  402.     EnableMenuItem(hMenu, BAUD1200, MF_ENABLED);
  403.     EnableMenuItem(hMenu, BAUD300, MF_ENABLED);
  404. }
  405.