home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / pibterm / pibt41s4.arc / SENDYMOD.MOD < prev    next >
Text File  |  1988-03-06  |  18KB  |  459 lines

  1. (*----------------------------------------------------------------------*)
  2. (*        Send_Ymodem_File --- Uploads file with Ymodem or Sealink      *)
  3. (*----------------------------------------------------------------------*)
  4.  
  5. PROCEDURE Send_Ymodem_File( Batch_Mode : BOOLEAN );
  6.  
  7. (*----------------------------------------------------------------------*)
  8. (*                                                                      *)
  9. (*     Procedure:  Send_Ymodem_File                                     *)
  10. (*                                                                      *)
  11. (*     Purpose:    Uploads files using Ymodem or SeaLink                *)
  12. (*                                                                      *)
  13. (*     Calling Sequence:                                                *)
  14. (*                                                                      *)
  15. (*        Send_Ymodem_File( Batch_Mode : BOOLEAN );                     *)
  16. (*                                                                      *)
  17. (*           Batch_Mode --- TRUE to send files using Ymodem batch,      *)
  18. (*                          Else just one file using ordinary Ymodem.   *)
  19. (*                                                                      *)
  20. (*     Calls:   PibTerm_KeyPressed                                              *)
  21. (*              Async_Send                                              *)
  22. (*              Async_Receive_With_TimeOut                              *)
  23. (*              RvsVideoOn                                              *)
  24. (*              RvsVideoOff                                             *)
  25. (*              Wait_For_Nak                                            *)
  26. (*              Perform_Upload                                          *)
  27. (*                                                                      *)
  28. (*      Remarks:                                                        *)
  29. (*                                                                      *)
  30. (*         This routine performs wildcard directory searches and        *)
  31. (*         implements the Ymodem batch file transfer protocol.          *)
  32. (*                                                                      *)
  33. (*         Note that the header constructed here contains the           *)
  34. (*         file name, file size, and file creation time.                *)
  35. (*                                                                      *)
  36. (*----------------------------------------------------------------------*)
  37.  
  38. VAR
  39.    File_Pattern : AnyStr;
  40.    SFileName    : PACKED ARRAY[1..11] OF CHAR;
  41.    Int_Ch       : INTEGER;
  42.    Ch           : CHAR;
  43.    CheckSum     : INTEGER;
  44.    EndFName     : BOOLEAN;
  45.    I            : INTEGER;
  46.    J            : INTEGER;
  47.    File_Entry   : SearchRec;
  48.    Ack_OK       : BOOLEAN;
  49.    OK_File      : BOOLEAN;
  50.  
  51. (*----------------------------------------------------------------------*)
  52. (*          Make_Ymodem_Header --- Send special YMODEM header block     *)
  53. (*----------------------------------------------------------------------*)
  54.  
  55. PROCEDURE Make_Ymodem_Header;
  56.  
  57. (*----------------------------------------------------------------------*)
  58. (*                                                                      *)
  59. (*       Procedure:  Make_Ymodem_Header                                 *)
  60. (*                                                                      *)
  61. (*       Purpose:    Makes special Ymodem header block                  *)
  62. (*                                                                      *)
  63. (*       Calling sequence:                                              *)
  64. (*                                                                      *)
  65. (*          Make_Ymodem_Header;                                         *)
  66. (*                                                                      *)
  67. (*       Calls:  None                                                   *)
  68. (*                                                                      *)
  69. (*       Remarks:                                                       *)
  70. (*                                                                      *)
  71. (*          This version of PibTerm DOES send the file creation time.   *)
  72. (*                                                                      *)
  73. (*          Format of Ymodem block:                                     *)
  74. (*                                                                      *)
  75. (*             Bytes         Contents                                   *)
  76. (*             -----       ---------------------------------------      *)
  77. (*                                                                      *)
  78. (*               1           SOH                                        *)
  79. (*               2             0                                        *)
  80. (*               3           255                                        *)
  81. (*              4-j          File name in lower case                    *)
  82. (*            j+1-k          File size in bytes                         *)
  83. (*            k+1-l          File creation time/date in Unix format     *)
  84. (*            132-133        CRC of block                               *)
  85. (*                                                                      *)
  86. (*          The first three bytes are added later by the Xmodem send    *)
  87. (*          routine.                                                    *)
  88. (*                                                                      *)
  89. (*----------------------------------------------------------------------*)
  90.  
  91. VAR
  92.    I            : INTEGER;
  93.    J            : INTEGER;
  94.    K            : INTEGER;
  95.    L            : INTEGER;
  96.    CRC          : INTEGER;
  97.    ACK_Ok       : BOOLEAN;
  98.    Int_Ch       : INTEGER;
  99.    Fs1          : LONGINT;
  100.    Fs2          : LONGINT;
  101.    S_File_Size  : LONGINT;
  102.    C_File_Size  : STRING[10];
  103.    OK_File      : BOOLEAN;
  104.  
  105.    DTRec        : DateTime;
  106.  
  107.    Date         : LONGINT;
  108.    OctD         : STRING[20];
  109.    RemO         : LONGINT;
  110.    Quot         : LONGINT;
  111.  
  112.    FullName     : AnyStr;
  113.  
  114. (*----------------------------------------------------------------------*)
  115. (*              LowerCase --- convert character to lower case           *)
  116. (*----------------------------------------------------------------------*)
  117.  
  118. FUNCTION LowerCase( C: CHAR ): CHAR;
  119.  
  120. BEGIN (* LowerCase *)
  121.  
  122.    IF ( C IN ['A'..'Z'] ) THEN
  123.       LowerCase := CHR( ORD( C ) + 32 )
  124.    ELSE
  125.       LowerCase := C;
  126.  
  127. END   (* LowerCase *);
  128.  
  129. (*----------------------------------------------------------------------*)
  130.  
  131. BEGIN (* Make_Ymodem_Header *)
  132.                                    (* Zero out block *)
  133.    FOR I := 1 TO 130 DO
  134.       Sector_Data[I] := 0;
  135.                                    (* File name      *)
  136.    CASE Use_Full_Path_Name OF
  137.       TRUE : Add_Path( FileName, Upload_Dir_Path, FullName );
  138.       FALSE: FullName := FileName;
  139.    END (* CASE *);
  140.  
  141.    L := LENGTH( FullName );
  142.  
  143.    FOR I := 1 TO L DO
  144.       Sector_Data[I] := ORD( LowerCase(FullName[I]) );
  145.  
  146.                                    (* File size in Ascii *)
  147.  
  148.    STR( File_Entry.Size , C_File_Size );
  149.  
  150.                                    (* Insert file size in block *)
  151.    I      := L + 2;
  152.  
  153.    FOR K := 1 TO LENGTH( C_File_Size ) DO
  154.      BEGIN
  155.         Sector_Data[I] := ORD( C_File_Size[K] );
  156.         I              := I + 1;
  157.      END;
  158.                                    (* Get file date and time *)
  159.  
  160.    UnPackTime( File_Entry.Time , DTRec );
  161.  
  162.                                    (* Convert DOS time and date to *)
  163.                                    (* number of seconds since      *)
  164.                                    (* January 1, 1970.             *)
  165.  
  166.    WITH DTRec DO
  167.       Set_Unix_Style_Date( Date, Year, Month, Day, Hour, Min, Sec );
  168.  
  169.                                    (* Convert date to octal string *)
  170.    OctD := '';
  171.  
  172.    REPEAT
  173.  
  174.       Quot := Date DIV 8;
  175.       Remo := Date - 8 * Quot;
  176.  
  177.       OctD := CHR( TRUNC( Remo ) + ORD( '0' ) ) + OctD;
  178.  
  179.       Date := Quot;
  180.  
  181.    UNTIL( Date <= 0 );
  182.                                    (* Insert octal date into Ymodem block *)
  183.    Sector_Data[I] := ORD(' ');
  184.  
  185.    FOR K := 1 TO LENGTH( OctD ) DO
  186.       BEGIN
  187.          I              := I + 1;
  188.          Sector_Data[I] := ORD(OctD[K]);
  189.       END;
  190.  
  191. END   (* Make_Ymodem_Header *);
  192.  
  193. {====================
  194. (*----------------------------------------------------------------------*)
  195. (*          Make_SEALink_Header --- Make special SEALink header block   *)
  196. (*----------------------------------------------------------------------*)
  197.  
  198. PROCEDURE Make_SEALink_Header( File_Entry : SearchRec );
  199.  
  200. (*----------------------------------------------------------------------*)
  201. (*                                                                      *)
  202. (*       Procedure:  Make_SEALink_Header                                *)
  203. (*                                                                      *)
  204. (*       Purpose:    Makes special SEALink header block                 *)
  205. (*                                                                      *)
  206. (*       Calling sequence:                                              *)
  207. (*                                                                      *)
  208. (*          Make_SEALink_Header( File_Entry : SearchRec );              *)
  209. (*                                                                      *)
  210. (*       Calls:  None                                                   *)
  211. (*                                                                      *)
  212. (*          Format of SEALink block 0:                                  *)
  213. (*                                                                      *)
  214. (*             Bytes         Contents                                   *)
  215. (*             -----       ---------------------------------------      *)
  216. (*                                                                      *)
  217. (*               1           SOH                                        *)
  218. (*               2             0                                        *)
  219. (*               3           255                                        *)
  220. (*              4-7          File size in bytes (4-byte integer)        *)
  221. (*              8-11         Creation time/date in seconds since        *)
  222. (*                           January 1, 1970 (4-byte integer)           *)
  223. (*             12-27         Name of file in 'name.ext' form            *)
  224. (*              28           Version number (always zero here)          *)
  225. (*             29-43         PIBTERM  -- sending program's name         *)
  226. (*              44           Overdrive flag.                            *)
  227. (*             45-131        All zeroes                                 *)
  228. (*            132-133        CRC of block                               *)
  229. (*                                                                      *)
  230. (*----------------------------------------------------------------------*)
  231.  
  232. VAR
  233.    I            : INTEGER;
  234.    J            : INTEGER;
  235.    L            : INTEGER;
  236.    CheckSum     : INTEGER;
  237.    ACK_Ok       : BOOLEAN;
  238.    Int_Ch       : INTEGER;
  239.    DTRec        : DateTime;
  240.    C            : STRING[1];
  241.  
  242.    Date         : LONGINT;
  243.  
  244. BEGIN (* Make_SEALink_Header *)
  245.                                    (* Zero out block *)
  246.  
  247.    FillChar( Sector_Data[1], 130, 0 );
  248.  
  249.                                    (* File size in 32-bit MS DOS form *)
  250.  
  251.    MOVE( File_Entry.Size , Sector_Data[1], 4 );
  252.  
  253.                                    (* Creation time/date in UNIX form *)
  254.  
  255.    UnPackTime( File_Entry.Time , DTRec );
  256.  
  257.                                    (* Convert DOS time and date to *)
  258.                                    (* number of seconds since      *)
  259.                                    (* January 1, 1970.             *)
  260.  
  261.    WITH DTRec DO
  262.       Set_Unix_Style_Date( Date, Year, Month, Day, Hour, Min, Sec );
  263.  
  264.                                    (* Insert date into sector data *)
  265.  
  266.    MOVE( Date, Sector_Data[5], 4 );
  267.  
  268.                                    (* File name *)
  269.    J := 1;
  270.  
  271.    FOR I := 1 TO LENGTH( File_Entry.Name ) DO
  272.       BEGIN
  273.          Sector_Data[J+8] := ORD( File_Entry.Name[J] );
  274.          J                := SUCC( J );
  275.       END;
  276.  
  277.    FOR I := J TO 16 DO
  278.       Sector_Data[I+8] := ORD(' ');
  279.  
  280.                                    (* Sending program's name *)
  281.    FOR I := 1 TO 15 DO
  282.       BEGIN
  283.          C                 := COPY( 'PIBTERM        ', I, 1 );
  284.          Sector_Data[I+25] := ORD( C[1] );
  285.       END;
  286.  
  287. END   (* Make_SEALink_Header *);
  288. ===========}
  289.  
  290. (*----------------------------------------------------------------------*)
  291. (*          Get_Ymodem_File_Name  --- get file name for upload          *)
  292. (*----------------------------------------------------------------------*)
  293.  
  294. PROCEDURE Get_Ymodem_File_Name( VAR OK_File : BOOLEAN );
  295.  
  296. VAR
  297.    I : INTEGER;
  298.  
  299. BEGIN (* Get_Ymodem_File_Name *)
  300.  
  301.    FileName := File_Entry.Name;
  302.  
  303.    OK_File := ( File_Entry.Attr AND
  304.                 ( VolumeID + Directory ) = 0 );
  305.  
  306.                                    (* If host mode, make sure file *)
  307.                                    (* is on xferlist!              *)
  308.    IF Host_Mode THEN
  309.       IF ( Privilege <> 'S' ) THEN
  310.          OK_File := OK_File AND ( Scan_Xfer_List( FileName ) > 0 );
  311.  
  312. END   (* Get_Ymodem_File_Name *);
  313.  
  314. (*----------------------------------------------------------------------*)
  315. (*                Perform_Upload --- Do the upload                      *)
  316. (*----------------------------------------------------------------------*)
  317.  
  318. PROCEDURE Perform_Upload;
  319.  
  320. BEGIN (* Perform_Upload *)
  321.  
  322.    IF Batch_Mode THEN
  323.       BEGIN
  324.          IF Display_Status THEN
  325.             WRITELN('  Uploading: ' + FileName);
  326.          Write_Log('  Uploading: ' + FileName , FALSE, FALSE );
  327.       END;
  328.  
  329.    Send_Xmodem_File( TRUE );
  330.  
  331.    IF Batch_Mode THEN
  332.       BEGIN
  333.          TextColor( Menu_Text_Color );
  334.          TextBackGround( BLACK );
  335.       END;
  336.  
  337. END   (* Perform_Upload *);
  338.  
  339. (*----------------------------------------------------------------------*)
  340. (*  Send_Null_File_Name --- Send null file name to stop batch transfer  *)
  341. (*----------------------------------------------------------------------*)
  342.  
  343. PROCEDURE Send_Null_File_Name;
  344.  
  345. VAR
  346.    I: INTEGER;
  347.  
  348. BEGIN (* Send_Null_File_Name *)
  349.                                    (* Purge reception *)
  350.    REPEAT
  351.       Async_Receive_With_Timeout( One_Second , Int_Ch );
  352.    UNTIL ( Int_Ch = TimeOut );
  353.  
  354.                                    (* Send null file name block 0 *)
  355.    Async_Send( CHR( SOH ) );
  356.    Async_Send( CHR(   0 ) );
  357.    Async_Send( CHR( 255 ) );
  358.  
  359.    FOR I := 1 TO 130 DO
  360.       Async_Send( CHR( 0 ) );
  361.  
  362.    IF ( NOT Display_Status ) THEN
  363.       Display_Batch_Window;
  364.  
  365.    TextColor( Menu_Text_Color );
  366.    TextBackGround( BLACK );
  367.  
  368.    WRITELN(' ');
  369.    WRITELN('  Sending null file name to terminate batch transfer ...');
  370.    Write_Log('Sending null file name to terminate batch transfer.', FALSE, FALSE);
  371.  
  372.                                    (* Wait for ACK                    *)
  373.  
  374.    Async_Receive_With_TimeOut( Ten_Seconds , Int_Ch );
  375.  
  376.    IF ( Int_Ch = ACK ) THEN
  377.       BEGIN
  378.          WRITELN;
  379.          WRITELN('  Host system ACKnowledged end of batch.');
  380.          Write_Log('Host system ACKnowledged end of batch.', TRUE, FALSE);
  381.       END;
  382.  
  383. END   (* Send_Null_File_Name *);
  384.  
  385. (*----------------------------------------------------------------------*)
  386.  
  387. BEGIN (* Send_Ymodem_File *)
  388.                                    (* Open batch transfer window    *)
  389.    IF ( Batch_Mode ) THEN
  390.       Display_Batch_Window;
  391.                                    (* Get file name pattern to send *)
  392.    File_Pattern  := FileName;
  393.                                    (* Pick up drive and path name    *)
  394.    IF ( NOT Host_Mode ) THEN
  395.       Extract_Upload_Path_Name( File_Pattern , Upload_Dir_Path );
  396.  
  397.                                    (* See if we can find anything to *)
  398.                                    (* be sent.                       *)
  399.  
  400.    FindFirst( File_Pattern, AnyFile, File_Entry );
  401.  
  402.    Stop_Send    := ( DosError <> 0 );
  403.  
  404.    IF Stop_Send THEN
  405.       IF Batch_Mode THEN
  406.          BEGIN
  407.             WRITELN('  No files found to send.');
  408.             Write_Log('No files found to send.' , FALSE, FALSE );
  409.          END;
  410.  
  411.                                    (* Loop over file names         *)
  412.    WHILE( NOT Stop_Send ) DO
  413.       BEGIN
  414.                                    (* Get file name *)
  415.  
  416.          Get_Ymodem_File_Name( OK_File );
  417.  
  418.                                    (* Get Ymodem header block       *)
  419.          IF OK_File THEN
  420.             BEGIN
  421.  
  422.                IF ( NOT Stop_Send ) THEN
  423. {
  424.                   IF ( Transfer_Protocol = SeaLink ) THEN
  425.                      Make_SEAlink_Header( File_Entry )
  426.                   ELSE
  427. }
  428.                      IF ( Batch_Mode OR Use_Ymodem_Header ) THEN
  429.                         Make_Ymodem_Header;
  430.  
  431.                                    (* Send the file itself          *)
  432.  
  433.                IF ( NOT Stop_Send ) THEN
  434.                   Perform_Upload;
  435.  
  436.             END;
  437.                                   (* See if more files to transfer *)
  438.  
  439.          FindNext( File_Entry );
  440.  
  441.          Stop_Send := Stop_Send OR ( NOT Batch_Mode ) OR
  442.                       ( DosError <> 0 );
  443.  
  444.       END (* WHILE *);
  445.                                    (* Send null file name to indicate *)
  446.                                    (* no more files                   *)
  447.    IF Batch_Mode THEN
  448.       BEGIN
  449.                                    (* Send null file name to stop transfer *)
  450.          Send_Null_File_Name;
  451.  
  452.          End_Batch_Transfer;
  453.  
  454.       END;
  455.                                    (* Restore colors *)
  456.    Reset_Global_Colors;
  457.  
  458. END   (* Send_Ymodem_File *);
  459.