home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 500-599 / ff589.lza / Term / TermSrc.lha / termTransfer.c < prev    next >
C/C++ Source or Header  |  1991-12-01  |  11KB  |  578 lines

  1. /* $Revision Header * Header built automatically - do not edit! *************
  2.  *
  3.  *    (C) Copyright 1991 by Olaf 'Olsen' Barthel & MXM
  4.  *
  5.  *    Name .....: termTransfer.c
  6.  *    Created ..: Friday 03-May-91 19:49
  7.  *    Revision .: 0
  8.  *
  9.  *    Date            Author          Comment
  10.  *    =========       ========        ====================
  11.  *    03-May-91       Olsen           Created this file!
  12.  *
  13.  * $Revision Header ********************************************************/
  14.  
  15. #include "TermGlobal.h"
  16.  
  17.     /* The action strings to display. */
  18.  
  19. STATIC UBYTE *SendQuery[3] =
  20. {
  21.     "Upload File(s)",
  22.     "Upload Text",
  23.     "Upload ASCII"
  24. };
  25.  
  26. STATIC UBYTE *ReceiveQuery[3] =
  27. {
  28.     "Download File(s)",
  29.     "Download Text",
  30.     "Download ASCII"
  31. };
  32.  
  33.     /* The transfer types to display. */
  34.  
  35. STATIC UBYTE *TransferTypes[3] =
  36. {
  37.     "binary",
  38.     "text",
  39.     "ASCII"
  40. };
  41.  
  42.     /* StartXprReceive():
  43.      *
  44.      *    Receive files via xpr.
  45.      */
  46.  
  47. VOID
  48. StartXprReceive(BYTE Type)
  49. {
  50.     struct FileRequester    *FileRequest;
  51.     UBYTE             DummyBuffer[256];
  52.     BYTE             OldStatus = Status;
  53.  
  54.         /* Select the download path. */
  55.  
  56.     switch(Type)
  57.     {
  58.         case TRANSFER_BINARY:    DownloadPath = &Config . BinaryDownloadPath[0];
  59.                     break;
  60.  
  61.         case TRANSFER_TEXT:    DownloadPath = &Config . TextDownloadPath[0];
  62.                     break;
  63.  
  64.         case TRANSFER_ASCII:    DownloadPath = &Config . ASCIIDownloadPath[0];
  65.                     break;
  66.     }
  67.  
  68.     BlockWindows();
  69.  
  70.         /* Set up the library if necessary. */
  71.  
  72.     if(!XProtocolBase)
  73.     {
  74.         NewLibrary = FALSE;
  75.  
  76.         xpr_options(0,NULL);
  77.  
  78.         if(NewLibrary)
  79.         {
  80.             if(ProtocolSetup())
  81.             {
  82.                 SaveProtocolOpts();
  83.  
  84.                 strcpy(Config . Protocol,LastXprLibrary);
  85.             }
  86.         }
  87.     }
  88.  
  89.     if(XProtocolBase)
  90.     {
  91.             /* Do we need to ask the user for
  92.              * the destination file name?
  93.              */
  94.  
  95.         if(TransferBits & XPRS_NORECREQ)
  96.         {
  97.                 /* Obviously not, let's open
  98.                  * the transfer info window as
  99.                  * usual and download the file(s).
  100.                  */
  101.  
  102.             if(TransferPanel(ReceiveQuery[Type]))
  103.             {
  104.                 Status = STATUS_DOWNLOAD;
  105.  
  106.                 ClearSerial();
  107.  
  108.                 LogAction("Initiate %s download.",TransferTypes[Type]);
  109.  
  110.                     /* Receive the data. */
  111.  
  112.                 XProtocolReceive(XprIO);
  113.  
  114.                     /* Wake the user up. */
  115.  
  116.                 if(TransferWindow)
  117.                     WakeUp(TransferWindow);
  118.  
  119.                 Say("Transfer completed.");
  120.  
  121.                 Status = OldStatus;
  122.  
  123.                     /* Queue another read request. */
  124.  
  125.                 if(ReadRequest)
  126.                 {
  127.                     ReadRequest -> IOSer . io_Command    = CMD_READ;
  128.                     ReadRequest -> IOSer . io_Data        = ReadBuffer;
  129.                     ReadRequest -> IOSer . io_Length    = 1;
  130.  
  131.                     SendIO(ReadRequest);
  132.                 }
  133.  
  134.                 DeleteTransferPanel();
  135.             }
  136.         }
  137.         else
  138.         {
  139.                 /* Download the file(s). */
  140.  
  141.             if(FileRequest = GetFile(ReceiveQuery[Type],DownloadPath,"",DummyBuffer,NULL,TRUE,FALSE,FALSE,"Receive"))
  142.             {
  143.                     /* Save the download path. */
  144.  
  145.                 strcpy(DownloadPath,FileRequest -> rf_Dir);
  146.  
  147.                     /* Install the name of the file to receive. */
  148.  
  149.                 XprIO -> xpr_filename = DummyBuffer;
  150.  
  151.                     /* Open the transfer panel. */
  152.  
  153.                 if(TransferPanel(ReceiveQuery[Type]))
  154.                 {
  155.                     Status = STATUS_DOWNLOAD;
  156.  
  157.                     ClearSerial();
  158.  
  159.                     LogAction("Initiate %s download.",TransferTypes[Type]);
  160.  
  161.                         /* Receive the file. */
  162.  
  163.                     XProtocolReceive(XprIO);
  164.  
  165.                         /* Wake the user up. */
  166.  
  167.                     if(TransferWindow)
  168.                         WakeUp(TransferWindow);
  169.  
  170.                     Say("Transfer completed.");
  171.  
  172.                     Status = OldStatus;
  173.  
  174.                         /* Close the transfer panel. */
  175.  
  176.                     DeleteTransferPanel();
  177.  
  178.                         /* Queue another read
  179.                          * request.
  180.                          */
  181.  
  182.                     if(ReadRequest)
  183.                     {
  184.                         ReadRequest -> IOSer . io_Command    = CMD_READ;
  185.                         ReadRequest -> IOSer . io_Data        = ReadBuffer;
  186.                         ReadRequest -> IOSer . io_Length    = 1;
  187.  
  188.                         SendIO(ReadRequest);
  189.                     }
  190.                 }
  191.  
  192.                 FreeAslRequest(FileRequest);
  193.             }
  194.         }
  195.     }
  196.  
  197.     ReleaseWindows();
  198.  
  199.     DownloadPath = NULL;
  200. }
  201.  
  202.     /* StartXprSend():
  203.      *
  204.      *    Send files via xpr.
  205.      */
  206.  
  207. BYTE
  208. StartXprSend(BYTE Type)
  209. {
  210.     struct FileRequester    *FileRequest;
  211.     UBYTE             DummyBuffer[256];
  212.     BYTE             OldStatus = Status;
  213.     UBYTE            *UploadPath;
  214.     BYTE             DidSend = TRUE;
  215.  
  216.         /* We are uploading data. */
  217.  
  218.     Uploading = TRUE;
  219.  
  220.         /* Select the upload path. */
  221.  
  222.     switch(Type)
  223.     {
  224.         case TRANSFER_BINARY:    UploadPath = &Config . BinaryUploadPath[0];
  225.                     break;
  226.  
  227.         case TRANSFER_TEXT:    UploadPath = &Config . TextUploadPath[0];
  228.                     break;
  229.  
  230.         case TRANSFER_ASCII:    UploadPath = &Config . ASCIIUploadPath[0];
  231.                     break;
  232.     }
  233.  
  234.     BlockWindows();
  235.  
  236.         /* If not initialized, try to set up a new
  237.          * external transfer protocol.
  238.          */
  239.  
  240.     if(!XProtocolBase)
  241.     {
  242.         NewLibrary = FALSE;
  243.  
  244.         xpr_options(0,NULL);
  245.  
  246.         if(NewLibrary)
  247.         {
  248.             if(ProtocolSetup())
  249.             {
  250.                 SaveProtocolOpts();
  251.  
  252.                 strcpy(Config . Protocol,LastXprLibrary);
  253.             }
  254.         }
  255.     }
  256.  
  257.     if(XProtocolBase)
  258.     {
  259.             /* Clear the pattern match anchor structure. */
  260.  
  261.         memset(FileAnchor,0,sizeof(struct AnchorPath));
  262.  
  263.         FileMatch = FALSE;
  264.  
  265.             /* Do we need to use our own file requester or
  266.              * will xpr handle this job for us?
  267.              */
  268.  
  269.         if(TransferBits & XPRS_NOSNDREQ)
  270.         {
  271.                 /* Open the transfer info window. */
  272.  
  273.             if(TransferPanel(SendQuery[Type]))
  274.             {
  275.                 Status = STATUS_UPLOAD;
  276.  
  277.                     /* Shut up the serial line. */
  278.  
  279.                 ClearSerial();
  280.  
  281.                 LogAction("Initiate %s upload.",TransferTypes[Type]);
  282.  
  283.                     /* Perform upload. */
  284.  
  285.                 XProtocolSend(XprIO);
  286.  
  287.                 if(TransferWindow)
  288.                     WakeUp(TransferWindow);
  289.  
  290.                 Say("Transfer completed.");
  291.  
  292.                 Status = OldStatus;
  293.  
  294.                     /* Close the info window. */
  295.  
  296.                 DeleteTransferPanel();
  297.  
  298.                     /* And request another character. */
  299.  
  300.                 if(ReadRequest)
  301.                 {
  302.                     ReadRequest -> IOSer . io_Command    = CMD_READ;
  303.                     ReadRequest -> IOSer . io_Data        = ReadBuffer;
  304.                     ReadRequest -> IOSer . io_Length    = 1;
  305.  
  306.                     SendIO(ReadRequest);
  307.                 }
  308.             }
  309.         }
  310.         else
  311.         {
  312.                 /* We will need the file requester to find
  313.                  * out which file(s) are to be transferred.
  314.                  * Multiple files and wildcards are
  315.                  * supported as well as plain file names.
  316.                  */
  317.  
  318.             if(FileRequest = GetFile(SendQuery[Type],UploadPath,"",DummyBuffer,"",FALSE,TRUE,FALSE,"Send"))
  319.             {
  320.                 strcpy(UploadPath,FileRequest -> rf_Dir);
  321.  
  322.                 if(FileRequest -> rf_NumArgs <= 1)
  323.                 {
  324.                     BPTR OldLock,NewLock;
  325.  
  326.                     MultipleFiles = FALSE;
  327.  
  328.                         /* Put the name of the first selected
  329.                          * file into the buffer.
  330.                          */
  331.  
  332.                     if(FileRequest -> rf_NumArgs == 1)
  333.                         strcpy(DummyBuffer,FileRequest -> rf_ArgList[0] . wa_Name);
  334.  
  335.                     XprIO -> xpr_filename = DummyBuffer;
  336.  
  337.                         /* To have a valid directory to
  338.                          * read the files from we'll jump
  339.                          * to the directory given in the file
  340.                          * requester. This is due to the fact
  341.                          * that all xpr protocols have referenced
  342.                          * files by their file names (sans path)
  343.                          * yet.
  344.                          */
  345.  
  346.                     if(NewLock = Lock(FileRequest -> rf_Dir,ACCESS_READ))
  347.                     {
  348.                         OldLock = CurrentDir(NewLock);
  349.  
  350.                         if(TransferPanel(SendQuery[Type]))
  351.                         {
  352.                             Status = STATUS_UPLOAD;
  353.  
  354.                             ClearSerial();
  355.  
  356.                             LogAction("Initiate %s upload.",TransferTypes[Type]);
  357.  
  358.                             XProtocolSend(XprIO);
  359.  
  360.                             if(TransferWindow)
  361.                                 WakeUp(TransferWindow);
  362.  
  363.                             Say("Transfer completed.");
  364.  
  365.                             Status = OldStatus;
  366.  
  367.                             DeleteTransferPanel();
  368.  
  369.                             if(ReadRequest)
  370.                             {
  371.                                 ReadRequest -> IOSer . io_Command    = CMD_READ;
  372.                                 ReadRequest -> IOSer . io_Data        = ReadBuffer;
  373.                                 ReadRequest -> IOSer . io_Length    = 1;
  374.  
  375.                                 SendIO(ReadRequest);
  376.                             }
  377.                         }
  378.                         else
  379.                             MyEasyRequest(Window,"Failed to locate\ndirectory \"%s\"!","Continue",FileRequest -> rf_Dir);
  380.  
  381.                         CurrentDir(OldLock);
  382.  
  383.                         UnLock(NewLock);
  384.                     }
  385.                 }
  386.                 else
  387.                 {
  388.                         /* This looks like a batch file request. */
  389.  
  390.                     if(FileRequest -> rf_NumArgs > 1)
  391.                     {
  392.                         BPTR OldLock,NewLock;
  393.  
  394.                             /* Set up the array of file names. */
  395.  
  396.                         FileCountMax    = FileRequest -> rf_NumArgs;
  397.                         FileArg        = FileRequest -> rf_ArgList;
  398.  
  399.                         MultipleFiles    = TRUE;
  400.  
  401.                             /* If this protocol doesn't support
  402.                              * batch file upload, make sure that
  403.                              * at least the first selected file
  404.                              * is transferred.
  405.                              */
  406.  
  407.                         XprIO -> xpr_filename = FileRequest -> rf_ArgList[0] . wa_Name;
  408.  
  409.                         if(NewLock = Lock(FileRequest -> rf_Dir,ACCESS_READ))
  410.                         {
  411.                             OldLock = CurrentDir(NewLock);
  412.  
  413.                             if(TransferPanel(SendQuery[Type]))
  414.                             {
  415.                                 Status = STATUS_UPLOAD;
  416.  
  417.                                 ClearSerial();
  418.  
  419.                                 LogAction("Initiate %s upload.",TransferTypes[Type]);
  420.  
  421.                                 XProtocolSend(XprIO);
  422.  
  423.                                 if(TransferWindow)
  424.                                     WakeUp(TransferWindow);
  425.  
  426.                                 Say("Transfer completed.");
  427.  
  428.                                 Status = OldStatus;
  429.  
  430.                                 DeleteTransferPanel();
  431.  
  432.                                 if(ReadRequest)
  433.                                 {
  434.                                     ReadRequest -> IOSer . io_Command    = CMD_READ;
  435.                                     ReadRequest -> IOSer . io_Data        = ReadBuffer;
  436.                                     ReadRequest -> IOSer . io_Length    = 1;
  437.  
  438.                                     SendIO(ReadRequest);
  439.                                 }
  440.                             }
  441.                             else
  442.                                 MyEasyRequest(Window,"Failed to locate\ndirectory \"%s\"!","Continue",FileRequest -> rf_Dir);
  443.  
  444.                             CurrentDir(OldLock);
  445.  
  446.                             UnLock(NewLock);
  447.                         }
  448.                     }
  449.                 }
  450.  
  451.                 FreeAslRequest(FileRequest);
  452.             }
  453.             else
  454.                 DidSend = FALSE;
  455.         }
  456.  
  457.         if(FileMatch)
  458.             MatchEnd(FileAnchor);
  459.     }
  460.  
  461.     ReleaseWindows();
  462.  
  463.     Uploading = FALSE;
  464.  
  465.     return(DidSend);
  466. }
  467.  
  468.     /* ASCIISetup():
  469.      *
  470.      *    Set up xprascii.library for plain ASCII file
  471.      *    transfer. This routine temporarily selects a
  472.      *    different protocol than currently set.
  473.      */
  474.  
  475. BYTE
  476. ASCIISetup()
  477. {
  478.     UBYTE Options[256];
  479.  
  480.         /* Close the currently opened xpr.library. */
  481.  
  482.     if(XProtocolBase)
  483.     {
  484.         XProtocolCleanup(XprIO);
  485.  
  486.         CloseLibrary(XProtocolBase);
  487.     }
  488.  
  489.         /* Clear the interface buffer. */
  490.  
  491.     memset(XprIO,0,sizeof(struct XPR_IO));
  492.  
  493.         /* Try to obtain the ASCII settings. */
  494.  
  495.     if(!GetEnvDOS("xprascii",Options))
  496.         Options[0] = 0;
  497.  
  498.         /* Initialize the interface buffer. */
  499.  
  500.     XprIO -> xpr_filename    = Options;
  501.     XprIO -> xpr_fopen    = (APTR)xpr_fopen;
  502.     XprIO -> xpr_fclose    = (APTR)xpr_fclose;
  503.     XprIO -> xpr_fread    = (APTR)xpr_fread;
  504.     XprIO -> xpr_fwrite    = (APTR)xpr_fwrite;
  505.     XprIO -> xpr_sread    = (APTR)xpr_sread;
  506.     XprIO -> xpr_swrite    = (APTR)xpr_swrite;
  507.     XprIO -> xpr_sflush    = (APTR)xpr_sflush;
  508.     XprIO -> xpr_update    = (APTR)xpr_update;
  509.     XprIO -> xpr_chkabort    = (APTR)xpr_chkabort;
  510.     XprIO -> xpr_gets    = (APTR)xpr_gets;
  511.     XprIO -> xpr_setserial    = (APTR)xpr_setserial;
  512.     XprIO -> xpr_ffirst    = (APTR)xpr_ffirst;
  513.     XprIO -> xpr_fnext    = (APTR)xpr_fnext;
  514.     XprIO -> xpr_finfo    = (APTR)xpr_finfo;
  515.     XprIO -> xpr_fseek    = (APTR)xpr_fseek;
  516.     XprIO -> xpr_extension    = 4;
  517.     XprIO -> xpr_options    = (APTR)xpr_options;
  518.     XprIO -> xpr_unlink    = (APTR)xpr_unlink;
  519.     XprIO -> xpr_squery    = (APTR)xpr_squery;
  520.     XprIO -> xpr_getptr    = (APTR)xpr_getptr;
  521.  
  522.         /* Open xprascii.library... */
  523.  
  524.     if(XProtocolBase = (struct Library *)OpenLibrary("xprascii.library",0))
  525.     {
  526.             /* Initialize it. */
  527.  
  528.         TransferBits = XProtocolSetup(XprIO);
  529.  
  530.             /* Successful initialization? */
  531.  
  532.         if(!(TransferBits & XPRS_SUCCESS))
  533.         {
  534.             MyEasyRequest(Window,"Failed to set up protocol\n\"%s\"!","Continue","xprascii.library");
  535.  
  536.             CloseLibrary(XProtocolBase);
  537.  
  538.             XProtocolBase = NULL;
  539.  
  540.             ProtocolSetup();
  541.  
  542.             return(FALSE);
  543.         }
  544.     }
  545.     else
  546.     {
  547.         MyEasyRequest(Window,"Failed to open protocol\n\"%s\"!","Continue","xprascii.library");
  548.  
  549.         ProtocolSetup();
  550.  
  551.         return(FALSE);
  552.     }
  553.  
  554.     BinaryTransfer = FALSE;
  555.  
  556.     return(TRUE);
  557. }
  558.  
  559.     /* ASCIIShutdown():
  560.      *
  561.      *    Close down xprascii.library and reopen the library
  562.      *    set in the current configuration.
  563.      */
  564.  
  565. VOID
  566. ASCIIShutdown()
  567. {
  568.     XProtocolCleanup(XprIO);
  569.  
  570.     CloseLibrary(XProtocolBase);
  571.  
  572.     XProtocolBase = NULL;
  573.  
  574.     BinaryTransfer = TRUE;
  575.  
  576.     ProtocolSetup();
  577. }
  578.