home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / ftpbatch.zip / ftpbatch.cmd
OS/2 REXX Batch file  |  2002-12-10  |  7KB  |  227 lines

  1. /* ftpbatch.cmd -- put and get files via rexx ftp */
  2. /* Copyright (C) 1997, Jack J. Woehr jax@well.com */
  3. /* PO Box 51, Golden, Colorado 80402-0051         */
  4. /* http://www.well.com/user/jax/rcfb/             */
  5. /* You may use this file freely, but it is        */
  6. /* Freeware -- There Is No Warranty At All.       */
  7.  
  8. parse source platform launcher program
  9. parse arg host userid password batchfile defaultMode
  10.  
  11. /* Is user asking for help? */
  12. if host="/?" | host="-?"  then do
  13.    call usage
  14.    exit 0
  15. end /* do */
  16.  
  17. /* Enough args? */
  18. if batchfile='' then do
  19.    call usage
  20.    exit 1
  21. end /* do */
  22.  
  23. /* Set the default transfer mode */
  24. defaultMode = translate(defaultMode)
  25. select
  26.    when defaultMode=''   then defaultMode='Binary'
  27.    when defaultMode='/B' then defaultMode='Binary'
  28.    when defaultMode='/A' then defaultMode='ASCII'
  29.    when defaultMode='-B' then defaultMode='Binary'
  30.    when defaultMode='-A' then defaultMode='ASCII'
  31. otherwise
  32.    Say "Invalid default transfer mode" defaultMode
  33.    call usage
  34.    exit 2
  35.    end
  36.  
  37. /* Open batch file destination */
  38. result=stream(batchfile, 'c', 'open read')
  39. if result \="READY:" then do
  40.   say "Couldn't open" batchfile "as batchfile."
  41.   call usage
  42.   exit 3
  43.   end
  44.  
  45. /* Parse batchfile */
  46. Say "Parsing batchfile" batchfile'.'
  47. batch.0 = 0
  48. do while lines(batchfile)
  49.   curr=(batch.0)+1
  50.   batchLine = linein(batchfile)
  51.   parse value batchLine with batch.curr.cmd batch.curr.remote batch.curr.local batch.curr.mode
  52.   theCommand = translate(batch.curr.cmd)
  53.   batch.curr.cmd = theCommand  /* write back command for later upcase comparison */
  54.  
  55.   /* Skip an empty line */
  56.   if theCommand = '' then iterate
  57.  
  58.   /* Skip an invalid command */
  59.   if theCommand \= 'GET' & theCommand \= 'PUT' & theCommand \= 'CD' then do
  60.      Say "Skipping" batchLine',' "invalid command" theCommand'.'
  61.      iterate
  62.   end
  63.  
  64.   /* No local file? Nothing to do */
  65.   if batch.curr.remote  = '' then do
  66.      Say "Skipping" batchline'.' "no remote file name."
  67.      iterate
  68.   end
  69.  
  70.   /* No local file? Then becomes same as remote. */
  71.   if batch.curr.local = '' then batch.curr.local = batch.curr.remote
  72.  
  73.   /* Default mode is binary */
  74.   aMode=translate(batch.curr.mode)
  75.   select
  76.      when aMode=''   then aMode= defaultMode
  77.      when aMode='/B' then aMode='Binary'
  78.      when aMode='/A' then aMode='ASCII'
  79.      when aMode='-B' then aMode='Binary'
  80.      when aMode='-A' then aMode='ASCII'
  81.   otherwise do
  82.      Say "Skipping" batchLine',' "invalid mode" aMode
  83.      iterate
  84.      end
  85.   end
  86.  
  87.   /* Now edit the command */
  88.   batch.curr.mode = aMode
  89.  
  90.   /* We've got a command, increment count */
  91.   batch.0 = (batch.0)+1
  92.   Say batch.0 "command lines so far."
  93.  
  94. end  /* do while lines(batchfile) */
  95.  
  96. /* close our file */
  97. call stream batchfile, 'c', 'CLOSE'
  98.  
  99. if batch.0 = 0 then do
  100.   Say "No commands in batchfile" batchfile'.'
  101.   exit 0
  102. end
  103.  
  104. /* Load FTP API */
  105. result = RxFuncAdd("FtpLoadFuncs","rxFtp","FtpLoadFuncs")
  106. result = FtpLoadFuncs()
  107. if result = 1 then do
  108.   Say "Error loading FTP Functions was"  result'.'
  109.   exit 4
  110. end
  111.  
  112. /* Announce purpose */
  113. say program "will contact host" host "on behalf of userid" userid "doing batch" batchfile "..."
  114.  
  115. /* Identifies the host, user ID, password, account for the remote host */
  116. Say "Identifying user" userid "to host" host
  117. Say "(This does not guarantee connection ..."
  118. Say " check return values from subsequent fuctions.)"
  119.  
  120. result = FtpSetUser(host,userid,password /* , account */)
  121. if result = 0 then do
  122.    say "Passed-in host-user-password not syntactically valid."
  123.    call usage
  124.    exit 5
  125.    end
  126.  
  127. /* Iterate through our commands */
  128. /* TRACE ?A */
  129. say FTPERRNO "is result of login."
  130.  
  131. do i = 1 to batch.0
  132.  
  133.   theCommand = batch.i.cmd
  134.  
  135.   /* These two variables are poorly named, */
  136.   theLocalFile = batch.i.local
  137.   theRemoteFile = batch.i.remote
  138.   /* as are their instancings. They should */
  139.   /* have been called something like       */
  140.   /* "positionalArg1" and "positionalArg2".*/
  141.  
  142.   theMode = batch.i.mode
  143.  
  144.   select
  145.      when theCommand = 'CD' then do
  146.         Say "Changing dir to" theLocalFile
  147.         result = FtpChDir(theLocalFile)
  148.         Say "Result of CD is" result'.'
  149.         if result \= 0 then say "FTP Error Number is" FTPERRNO'.'
  150.         end
  151.      when theCommand = 'PUT' then do
  152.         /* Set transfer mode */
  153.         call FtpSetBinary(theMode)
  154.         /* Send  the file */
  155.         Say "Putting" theRemoteFile "as" theLocalFile "in mode" theMode'.'
  156.         result = FtpPut(theRemoteFile, theLocalFile, theMode)
  157.         Say "Result of PUT is" result'.'
  158.         if result \= 0 then say "FTP Error Number is" FTPERRNO'.'
  159.         end
  160.      when theCommand = 'GET' then do
  161.         /* Set transfer mode */
  162.         call FtpSetBinary(theMode)
  163.         /* Get the file */
  164.         Say "Getting" theLocalFile "from" theRemoteFile "in mode" theMode'.'
  165.         result = FtpGet(theLocalFile, theRemoteFile, theMode)
  166.         Say "Result of GET is" result'.'
  167.         if result \= 0 then say "FTP Error Number is" FTPERRNO'.'
  168.         end
  169.   end
  170. end
  171.  
  172. /* Logoff host */
  173. Say "Logging off" host'.'
  174. result = FtpLogoff()
  175. Say "Result of Logoff is" result'.'
  176. if result \= 0 then say "FTP Error Number is" FTPERRNO'.'
  177.  
  178. /* End of program */
  179. call cleanup
  180. exit 0   /* Phew! */
  181.  
  182. /* Clean up Rexx name space */
  183. cleanup:
  184.    result = FtpDropFuncs()
  185.    return
  186.  
  187.  
  188. /* Inform user of command syntax */
  189. usage:
  190.    PROCEDURE EXPOSE program
  191.    csi = '1b'X
  192.    emp = csi'[7m'
  193.    nrm = csi'[m'
  194.    program = Filespec('NAME', program)
  195.    Say "Usage:" program" "emp"host "nrm" "emp"userid"nrm" "emp"password"nrm" "emp"batchfile"nrm" "emp"[default_transfer_mode]"nrm
  196.    Say
  197.    Say "     "program" executes "emp"batchfile"nrm" against the "emp"host"nrm" on behalf of "emp"userid"nrm"."
  198.    Say
  199.    Say "     The default transfer mode (/A or /B for ASCII or Binary) is"
  200.    Say "     "emp"default_transfer_mode"nrm", or Binary if "emp"default_transfer_mode"nrm
  201.    Say "     is omitted."
  202.    Say
  203.    Say "     "emp"batchfile"nrm" consists either of multiple lines of the form:"
  204.    Say
  205.    Say "              "emp"cmd"nrm" "emp"local|remote"nrm" "emp"[remote|local]"nrm" "emp"[mode]"nrm
  206.    Say
  207.    Say "     Supported commands are GET PUT and CD."
  208.    Say "     e.g.:"
  209.    Say
  210.    Say "       CD /public/os2"
  211.    Say "       GET REMOTEFILE.TXT LOCALFILE.TXT /A"
  212.    Say "       PUT LOCALFILE.BIN REMOTEFILE.BIN /B"
  213.    Say
  214.    Say "     With GET and PUT, if the second file argument, "emp"remote|local"nrm" and "emp"mode"nrm" are missing,"
  215.    Say "     the second file argument representing the remote (PUT) or local (GET) file name is assumed to be the same"
  216.    Say "     as the first file argument, and the default transfer mode is the one represented by the optional "
  217.    Say "     "emp"default_transfer_mode"nrm" argument to" program", or Binary if none specified. The latter is also"
  218.    Say "     true when "emp"mode"nrm" only is omitted."
  219.    Say
  220.    Say program ""emp"/?"nrm" or "emp"-h"nrm" gives this help."
  221.    return
  222.  
  223. exit
  224.  
  225. /* end of ftpbatch.cmd */
  226.  
  227.