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

  1.  
  2. /***************************************************************************
  3. *
  4. * Winsnd
  5. *
  6. * File send module for Windows Kermit
  7. *
  8. ***************************************************************************/
  9. #include <windows.h>
  10. #include "winkrm.h"
  11. #include <string.h>
  12.      
  13. /* these can be near procedures */
  14. int NEAR sendsw();
  15. char NEAR sinit();
  16. char NEAR sfile();
  17. char NEAR sdata();
  18. char NEAR seof();
  19. char NEAR sbreak();
  20. int NEAR bufill(char*);
  21.      
  22. static char filelist[100];    /* file names to send */
  23. static char *nextfile;        /* pointer to next file to be sent */
  24.      
  25. /***************************************************************************
  26. *
  27. * send
  28. *
  29. * This routine creates the send popup window and calls the send state machine
  30. *
  31. * Entry: None
  32. *
  33. * Exit: File(s) sent or failure.
  34. *
  35. ***************************************************************************/
  36. int FAR send()
  37. {
  38.      
  39.     RECT rect;        /* will contain screen location of main window */
  40.     char TXTitle[20];    /* popup window title */
  41.     int result;
  42.      
  43. /* create a dialog box to get file to be sent */
  44.     result = DialogBox(hInst,MAKEINTRESOURCE(SENDBOX),hKermWnd,lpprocSendBox);
  45.     if (result <= 0) {
  46.         MessageBox(hKermWnd, (LPSTR)"No filename(s) specified",
  47.                 (LPSTR)szWinTitle, MB_OK | MB_ICONEXCLAMATION);
  48.         SendMessage(hKermWnd, WM_COMMAND, SENDSTATE, (LONG)NULL);
  49.     return;
  50.     }
  51.      
  52.     nextfile = strtok(filelist, " ,");    /* parse file list */
  53.      
  54.     eol = CR;                /* eol for outgoing packet */
  55.     quote = MYQUOTE;            /* standard quote */
  56.     pad = MYPAD;            /* no padding */
  57.     padchar = MYPCHAR;            /* if any, use this */
  58.      
  59.     StateStr[0] = '\0';        /* initialize popup window parameters */
  60.     PacketStr[0] = '\0';
  61.     filnam1[0] = '\0';
  62.      
  63.     debug = FALSE;        /* need to make these menu items */
  64.     pktdeb = FALSE;
  65.      
  66.     ThisState = SENDSTATE;
  67.      
  68.     GetWindowRect(hKermWnd, (LPRECT)&rect);    /* set up popup parameters */
  69.     strcpy(TXTitle,szWinTitle);
  70.     strcat(TXTitle," Send");
  71.      
  72. /* create the send popup window */
  73.      
  74.     hRXWnd = CreateWindow((LPSTR)szAppName,
  75.                         (LPSTR)TXTitle,
  76.             WS_POPUP | WS_CAPTION,
  77.             5*CharWidth + rect.left,
  78.             4*CharHeight + rect.top,
  79.             21*CharWidth,
  80.             5*CharHeight - CharHeight/2,
  81.                         (HWND)hKermWnd,
  82.                         (HMENU)NULL,
  83.                         (HANDLE)hInst,
  84.                         (LPSTR)NULL
  85.                         );
  86.      
  87. /* kill off the menu, and show the window */
  88.      
  89.     SetMenu(hRXWnd, (HANDLE)NULL);
  90.     ShowWindow(hRXWnd, SHOW_OPENNOACTIVATE);
  91.     UpdateWindow(hRXWnd);
  92.      
  93.     if (pktdeb) {
  94.     dpfp = fopen("PACKET.LOG", "wb");
  95.         if (dpfp == NULL)
  96.             MessageBox(hKermWnd, (LPSTR)"Cannot open packet debug file",
  97.                 (LPSTR)szWinTitle, MB_OK | MB_ICONEXCLAMATION);
  98.     }
  99.      
  100.     if (sendsw() == FALSE)        /* send it */
  101.     printmsg("Send failed");
  102.     else
  103.     printmsg("Send Done");
  104.      
  105.     if (pktdeb)
  106.         fclose(dpfp);
  107.      
  108.     DestroyWindow(hRXWnd);        /* get rid of popup window */
  109.      
  110.     if (CurrentState == SENDSTATE)    /* simulate a mouse press */
  111.     SendMessage(hKermWnd, WM_COMMAND, SENDSTATE, (LONG)NULL);
  112.      
  113. }
  114.      
  115. /***************************************************************************
  116. *
  117. * sendsw
  118. *
  119. * State table switcher for send
  120. *
  121. * Entry: None.
  122. *
  123. * Exit: File sent or aborted.
  124. *
  125. ***************************************************************************/
  126. int NEAR sendsw()
  127. {
  128.      
  129.     state = 'S';
  130.     n = 0;
  131.     numtry = 0;
  132.     PacketCount = 0;
  133.      
  134.     while (TRUE) {
  135.     if (debug)
  136.         printf("rendsw state %c\n", state);
  137.         StateStr[0] = state;
  138.     strcpy(PacketStr, itoa(PacketCount, PacketStr,10));
  139.     hRXWndDC = GetDC(hRXWnd);
  140.     TextOut(hRXWndDC,9*CharWidth,2,(LPSTR)StateStr,1);
  141.     TextOut(hRXWndDC,10*CharWidth,CharHeight+2,(LPSTR)PacketStr,strlen(PacketStr));
  142.     ReleaseDC(hRXWnd, hRXWndDC);
  143.     PacketCount += 1;
  144.     switch (state) {
  145.         case 'S':
  146.         state = sinit();
  147.         break;
  148.         case 'F':
  149.         state = sfile();
  150.         break;
  151.         case 'D':
  152.         state = sdata();
  153.         break;
  154.         case 'Z':
  155.         state = seof();
  156.         break;
  157.         case 'B':
  158.         state = sbreak();
  159.         break;
  160.         case 'C':
  161.         return (TRUE);
  162.         case 'A':
  163.         return (FALSE);
  164.     }
  165.     }
  166. }
  167.      
  168. /***************************************************************************
  169. *
  170. * sinit
  171. *
  172. * Send init packet
  173. *
  174. * Entry: none.
  175. *
  176. * Exit: next state.
  177. *
  178. ***************************************************************************/
  179. char NEAR sinit()
  180. {
  181.     int num, len;
  182.      
  183.     if (debug)
  184.     printf("In sinit retries: %d\n", numtry);
  185.      
  186.     if (numtry++ > MAXTRY)
  187.     return('A');
  188.     spar(packet);
  189.      
  190.     FlushComm(cid, 1);
  191.      
  192.     spack('S',n,6, packet);
  193.     switch(rpack(&len, &num, recpkt)) {
  194.     case 'N':
  195.         return(state);
  196.      
  197.     case 'Y':
  198.         if (n != num)
  199.         return (state);
  200.         rpar(recpkt);
  201.         if (eol == 0)
  202.         eol = CR;
  203.         if (quote == 0)
  204.         quote = '#';
  205.         numtry = 0;
  206.         n = (n+1) % 64;
  207.         return('F');
  208.      
  209.     case 'E':
  210.         prerrpkt(recpkt);
  211.         return('A');
  212.      
  213.     case FALSE:
  214.         return(state);
  215.      
  216.     default:
  217.         return('A');
  218.     }
  219. }
  220.     
  221. /***************************************************************************
  222. *
  223. * sfile
  224. *
  225. * Send file name.
  226. *
  227. * Entry: none.
  228. *
  229. * Exit: next state.
  230. *
  231. ***************************************************************************/
  232. char NEAR sfile()
  233. {
  234.     int num, len;
  235.     char *temp;
  236.      
  237.     if (numtry++ > MAXTRY)
  238.     return('A');
  239.      
  240.     if (fp == NULL)
  241.     if (debug)
  242.         printf("    Opening %s for sending.\n", nextfile);
  243.     fp = fopen(nextfile, "rb");
  244.     if (fp == NULL) {
  245.             MessageBox(hKermWnd, (LPSTR)"Cannot send file",
  246.                 (LPSTR)szWinTitle, MB_OK | MB_ICONEXCLAMATION);
  247.         return('A');
  248.     }
  249.      
  250.     temp = strpbrk(nextfile,":\\");
  251.     if (temp != NULL) {
  252.         nextfile = temp + 1;
  253.         temp = strrchr(nextfile, '\\');
  254.         if (temp != NULL)
  255.             nextfile = temp+1;
  256.     }
  257.     strncpy(filnam1, strupr(nextfile), NAMESIZE);    
  258.     len = strlen(filnam1);
  259.     hRXWndDC = GetDC(hRXWnd);
  260.     TextOut(hRXWndDC,8*CharWidth,2*CharHeight+2,
  261.         (LPSTR)"            ", 12);
  262.     TextOut(hRXWndDC,8*CharWidth,2*CharHeight+2,(LPSTR)filnam1,strlen(filnam1));
  263.     ReleaseDC(hRXWnd, hRXWndDC);
  264.     FlashWindow(hRXWnd, FALSE);
  265.     spack('F', n, len, filnam1);
  266.     switch(rpack(&len, &num, recpkt)) {
  267.     case 'N':
  268.         num = (--num < 0 ? 63:num);
  269.         if (n != num)
  270.         return('S');
  271.      
  272.     case 'Y':
  273.         if (n != num)
  274.         return(state);
  275.         numtry = 0;
  276.         n = (n+1)%64;
  277.         size = bufill(packet);
  278.         return('D');
  279.     
  280.     case 'E':
  281.         prerrpkt(recpkt);
  282.         return('A');
  283.      
  284.     case FALSE:
  285.         return(state);
  286.      
  287.     default:
  288.         return('A');
  289.     }
  290. }
  291.      
  292. /***************************************************************************
  293. *
  294. * sdata
  295. *
  296. * Send a data packet
  297. *
  298. * Entry: none.
  299. *
  300. * Exit: next state.
  301. *
  302. ***************************************************************************/
  303. char NEAR sdata()
  304. {
  305.     int num, len;
  306.      
  307.     if (numtry++ > MAXTRY)
  308.     return('A');
  309.      
  310.     spack('D',n,size,packet);
  311.     switch(rpack(&len, &num, recpkt)) {
  312.      
  313.     case 'N' :
  314.         num = (--num < 0 ? 63 : num);
  315.         if (n != num)
  316.         return(state);
  317.      
  318.     case 'Y':
  319.         if (n != num)
  320.         return(state);
  321.         numtry = 0;
  322.         n = (n+1)%64;
  323.         if ((size = bufill(packet)) == EOF)
  324.         return('Z');
  325.         return('D');
  326.      
  327.     case 'E':
  328.         prerrpkt(recpkt);
  329.         return('A');
  330.      
  331.     case FALSE:
  332.         return(state);
  333.      
  334.     default:
  335.         return('A');
  336.     }
  337. }
  338.      
  339. /***************************************************************************
  340. *
  341. * seof
  342. *
  343. * Send end of file
  344. *
  345. * Entry: none.
  346. *
  347. * Exit: next state.
  348. *
  349. ***************************************************************************/
  350. char NEAR seof()
  351. {
  352.      
  353.     int num, len;
  354.      
  355.     if (numtry++ > MAXTRY)
  356.     return('A');
  357.      
  358.     spack('Z',n,0,packet);
  359.     switch(rpack(&len, &num, recpkt)) {
  360.      
  361.     case 'N' :
  362.         num = (--num < 0 ? 63 : num);
  363.         if (n != num)
  364.         return(state);
  365.      
  366.     case 'Y':
  367.         if (n != num)
  368.         return(state);
  369.         numtry = 0;
  370.         n = (n+1)%64;
  371.         if (debug)
  372.         printf("    Closing input file %s\n", nextfile);
  373.         fclose(fp);
  374.         fp == NULL;
  375.         if((nextfile = strtok(NULL, " ,")) == NULL)
  376.         return('B');
  377.         if (debug)
  378.         printf("    New file is %s\n", nextfile);
  379.         return('F');
  380.      
  381.     case 'E':
  382.         prerrpkt(recpkt);
  383.         return('A');
  384.      
  385.     case FALSE:
  386.         return(state);
  387.      
  388.     default:
  389.         return('A');
  390.     }
  391. }
  392.      
  393. /***************************************************************************
  394. *
  395. * sbreak
  396. *
  397. * send a break packet
  398. *
  399. * Entry: none.
  400. *
  401. * Exit: next state.
  402. *
  403. ***************************************************************************/
  404. char NEAR sbreak()
  405. {
  406.      
  407.     int num, len;
  408.      
  409.     if (numtry++ > MAXTRY)
  410.     return('A');
  411.      
  412.     spack('B',n,0,packet);
  413.     switch(rpack(&len, &num, recpkt)) {
  414.      
  415.     case 'N' :
  416.         num = (--num < 0 ? 63 : num);
  417.         if (n != num)
  418.         return(state);
  419.      
  420.     case 'Y':
  421.         if (n != num)
  422.         return(state);
  423.         numtry = 0;
  424.         n = (n+1)%64;
  425.         return('C');
  426.      
  427.     case 'E':
  428.         prerrpkt(recpkt);
  429.         return('A');
  430.      
  431.     case FALSE:
  432.         return(state);
  433.      
  434.     default:
  435.         return('A');
  436.     }
  437. }
  438.      
  439.      
  440. /***************************************************************************
  441. *
  442. * GetSendFile
  443. *
  444. * Windows function for get-file-name dialogue box.
  445. *
  446. * Entry: Usual windows function variables.
  447. *
  448. * Exit: box taken down or filenames list created.
  449. *
  450. ***************************************************************************/
  451. BOOL FAR PASCAL GetSendFile(hDlg, message, wParam, lParam)
  452. HWND hDlg;
  453. unsigned message;
  454. WORD wParam;
  455. LONG lParam;
  456. {
  457.      
  458.     int count;
  459.      
  460.     if (message == WM_COMMAND) {
  461.     switch(wParam) {
  462.         case IDOK:
  463.         count = GetDlgItemText(hDlg,SNDFILENAME,(LPSTR)filelist,100);
  464.         EndDialog(hDlg, count);
  465.         break;
  466.         case IDCANCEL:
  467.         EndDialog(hDlg, FALSE);
  468.         break;
  469.         default:
  470.         return FALSE;
  471.         }
  472.     return TRUE;
  473.     }
  474.     else if (message == WM_INITDIALOG)
  475.     return TRUE;
  476.     else
  477.     return FALSE;
  478. }
  479.      
  480. /***************************************************************************
  481. *
  482. * bufill
  483. *
  484. * Fill a buffer from file to be sent to remote Kermit.
  485. *
  486. * Entry: buffer to be filled.
  487. *
  488. * Exit: size of buffer.
  489. *
  490. * Side effects: buffer filled.
  491. *
  492. ***************************************************************************/
  493. int NEAR bufill(buffer)
  494. char buffer[];
  495. {
  496.     int i, t;
  497.     char t7;
  498.      
  499.     i = 0;
  500.     while ((t = getc(fp)) != EOF) {
  501.     t7 = unpar(t);
  502.     if (t7 < SP || t7==DEL || t7==quote) {
  503.         buffer[i++] = quote;
  504.         if (t7 != quote) {
  505.         /*    t = ctl(t); */
  506.         t7 = ctl(t7);
  507.         }
  508.     }
  509.     buffer[i++] = t7;
  510.     if (i >= spsiz - 8)
  511.         return(i);
  512.     }
  513.     if (i == 0)
  514.     return(EOF);
  515.     return(i);
  516. }
  517.