home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d5xx / d534 / term.lha / Term / Source.LZH / termTransfer.c < prev    next >
C/C++ Source or Header  |  1991-07-17  |  11KB  |  559 lines

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