home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / rftp.zip / rftp.cmd < prev   
OS/2 REXX Batch file  |  1995-08-15  |  16KB  |  544 lines

  1. /*-------------------------------------------------------------------------*\
  2.  * VERSION CONTROL:
  3.  *        $Id: rftp.cmd,v 1.3 1995/08/09 14:49:49 rodb Exp rodb $
  4.  *        $Tabs: 4 7 $
  5.  *-------------------------------------------------------------------------*
  6.  * NAME
  7.  *        rftp --- Command-line FTP using REXX rxftp.dll
  8.  *
  9.  * SYNOPSIS
  10.  *        rftp -h host [-l uid] [-p passwd] -G|-P [-a|-b] [-HvV] l_file r_file
  11.  *
  12.  * DESCRIPTION
  13.  *        The rftp command allows you to transfer a file between your PC and a 
  14.  *        foreign host.
  15.  *
  16.  *        This REXX program requires the REXX File Transfer Protocol (FTP) 
  17.  *        Application Program Interface (API) package supplied with IBM TCP/IP 
  18.  *        Version 2.0 for OS/2 and OS/2 Warp Connect.
  19.  *
  20.  * OPTIONS
  21.  *        -h host
  22.  *                Specifies the foreign host to which you are connecting
  23.  *        -l uid
  24.  *                User ID specifies the name associated with you by the foreign
  25.  *                host to which you are establishing a connection.  If not 
  26.  *                specified, rftp will check the environment for the presence of 
  27.  *                the variable USER, and if found use the value assigned to it.
  28.  *        -p passwd
  29.  *                Password specifies your unique password, which is associated 
  30.  *                with your user ID by the foreign host to which you are 
  31.  *                establishing a connection.  If not specified, rftp will check 
  32.  *                the environment for the presence of the variable THE_WORD, and 
  33.  *                if found use the value assigned to it.
  34.  *        -G
  35.  *                Get (receive) a file from the remote system
  36.  *        -P
  37.  *                Put (send) a file to the remote system
  38.  *        -H
  39.  *                Display help text and exit.  No other actions are taken.
  40.  *        -a
  41.  *                Sets the file transfer type to ASCII
  42.  *        -b
  43.  *                Sets the file transfer type to binary (default)
  44.  *        -v
  45.  *                Display version of RxFTP DLL and exit.  No other actions are 
  46.  *                taken.
  47.  *        -V
  48.  *                Operate in verbose mode
  49.  *        l_file
  50.  *                The name of the file on the local PC
  51.  *        r_file
  52.  *                The name of the file on the foreign host
  53.  *-------------------------------------------------------------------------*
  54.  *    Copyright (c) 1994-95 Lawrence R Buchanan.  ALL RIGHTS RESERVED.
  55.  *
  56.  *    This program is free software; you are free to do whatever you want 
  57.  * with it.  The only requirement is that if you use the getopt() option 
  58.  * parsing subroutines in code that you distribute, that you also include 
  59.  * the copyright messages that appear in the headers of those subroutines.
  60.  *    
  61.  *    This program is distributed in the hope that it will be useful, 
  62.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of 
  63.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  64. \*-------------------------------------------------------------------------*/
  65.  
  66.  
  67. /*-------------------------------------------------------------------------*\
  68.  * Initialize getopt. stem with commandline parameters.
  69.  *
  70.  * These statements MUST appear at the beginning of any program that uses 
  71.  * getopt().
  72. \*-------------------------------------------------------------------------*/
  73. parse arg argv
  74. retcd =  init_getopt( argv )
  75. if retcd = -1 then do
  76.     say getopt.!program ": Unbalanced quotation marks in command-line"
  77.     exit -1
  78. end
  79.  
  80.  
  81. /* If no parameters issue usage message and exit. */
  82. if getopt.0 = 0 then do
  83.     call Usage
  84.     exit 1
  85. end
  86.  
  87.  
  88. /* Begin Main program */
  89.  
  90. /* Setup program defaults. */
  91. Verb      = ""
  92. host_name = ""
  93. login_id  = ""
  94. password  = ""
  95. AscOrBin  = "B"                /* (A)scii or (B)inary */
  96. Verbose   = 0
  97. prtver    = 0
  98.  
  99. rxFtpErr = 0
  100. rxFtp.FTPSERVICE    = 10
  101. rxFtp.FTPHOST       = 11
  102. rxFtp.FTPSOCKET     = 12
  103. rxFtp.FTPCONNECT    = 13
  104. rxFtp.FTPLOGIN      = 14
  105. rxFtp.FTPABORT      = 15
  106. rxFtp.FTPLOCALFILE  = 16
  107. rxFtp.FTPDATACONN   = 17
  108. rxFtp.FTPCOMMAND    = 18
  109. rxFtp.FTPPROXYTHIRD = 19
  110. rxFtp.FTPNOPRIMARY  = 20
  111.  
  112.  
  113. /* Get the option flags and arguments and set up the program environment. */
  114. call DecodeSwitches
  115.  
  116. /* Register rxFtp DLL */
  117. if RxFuncQuery( "FtpLoadFuncs" ) then do
  118.     rc = RxFuncAdd( "FtpLoadFuncs", "RxFtp", "FtpLoadFuncs" )
  119.     rc = FtpLoadFuncs( "Quiet" )
  120. end
  121.  
  122. /* If -v specified, print RxFTP DLL version and exit. */
  123. if prtver then do
  124.     rc = FtpVersion( prtver )
  125.     say "RxFtp DLL version is" prtver
  126.     exit 0
  127. end
  128.  
  129. /* If no files are given to be printed issue message and exit. */
  130. if (getopt.0 - getopt.!optind) <> 1 then do
  131.     say getopt.!program ": Missing local or remote file name"
  132.     exit 2
  133. end
  134.  
  135. /* If not specified, get the default login ID and password from the environment. */
  136. if login_id = "" then
  137.     login_id   = value( "USER",, "OS2ENVIRONMENT" )
  138. if password = "" then
  139.     password  = value( "THE_WORD",, "OS2ENVIRONMENT" )
  140.  
  141. /* Get the local/remote file names from getopt. */
  142. i = getopt.!optind
  143. local_file  = getopt.i
  144. i = getopt.0
  145. remote_file = getopt.i
  146.  
  147. if Verbose then do
  148.     call time "R"
  149.     say "Start Time:" time( "N" )
  150.     say
  151.     say "Local File = " local_file
  152.     say "Host       = " host_name
  153.     say "Host File  = " remote_file
  154.     say "For User   = " login_id
  155.     say "Copy Type  = " AscOrBin
  156. end
  157.  
  158. rc = FtpSetUser( host_name, login_id, password )
  159. if rc = 0 then do
  160.     say getopt.!program ": Error setting host, userid, or password"
  161.     exit 1
  162. end
  163.  
  164. if Verb = "Get" then
  165.     rc = FtpGet( local_file, remote_file, AscOrBin )
  166. else
  167.     rc = FtpPut( local_file, remote_file, AscOrBin )
  168.  
  169. /* Save the return codes. */
  170. rxFtpRc  = rc
  171. rxFtpErr = FTPERRNO
  172.  
  173. /* Terminate the FTP session and reset the host/userid/password. */
  174. rc = FtpLogoff()
  175.  
  176. if Verbose then do
  177.     ElapsedTime = time("E")
  178.     say "Request Complete"
  179.     say "End Time  :" time("N") "    Elapsed Time  :" ElapsedTime
  180.     say
  181.     say "Ftp"Verb"(): rc =" rxFtpRc "    FTPERRNO =" rxFtpErr
  182. end
  183.  
  184. if rc = -1 & rxFtpErr = 0 then
  185.     exit 0
  186. else 
  187.     exit rxFtpRc
  188.  
  189. /* End of Main */
  190.  
  191.  
  192.  
  193. /*-------------------------------------------------------------------------*\
  194.  * NAME:
  195.  *    DecodeSwitches - decodes command-line options
  196.  *
  197.  * PARAMETERS:
  198.  *    *NONE
  199.  *
  200.  * RETURNS:
  201.  *    *NONE
  202. \*-------------------------------------------------------------------------*/
  203. DecodeSwitches:
  204.     errflg = 0
  205.     optstr = "abGHh:p:Pu:vV"
  206.     c = getopt(optstr)
  207.     do while c <> -1
  208.         select
  209.             when c = "a" then 
  210.                 AscOrBin = "A"            /* ASCII transfer */
  211.  
  212.             when c = "b" then 
  213.                 AscOrBin = "B"            /* Binary transfer */
  214.  
  215.             when c = "G" then
  216.                 if Verb = "" then
  217.                     Verb = "Get"
  218.                 else
  219.                     errflg = 1
  220.  
  221.             when c = "H" then do
  222.                 call PrintHelp
  223.                 exit 1
  224.                 end
  225.  
  226.             when c = "h" then 
  227.                 host_name = getopt.!optarg
  228.  
  229.             when c = "p" then 
  230.                 password = getopt.!optarg
  231.  
  232.             when c = "P" then
  233.                 if Verb = "" then
  234.                     Verb = "Put"
  235.                 else
  236.                     errflg = 1
  237.  
  238.             when c = "u" then 
  239.                 login_id = getopt.!optarg
  240.  
  241.             when c = "v" then
  242.                 prtver = 1
  243.  
  244.             when c = "V" then 
  245.                 Verbose = 1
  246.  
  247.             otherwise 
  248.                 do
  249.                     call Usage
  250.                     exit 2
  251.                 end
  252.         end
  253.     
  254.         if errflg then do
  255.             say getopt.!program ":" getopt.!optarg "is an invalid argument for option" c 
  256.             exit 2
  257.         end
  258.         c = getopt(optstr)
  259.     end
  260.  
  261. return
  262. /* End of DecodeSwitches */
  263.  
  264.  
  265. /*-------------------------------------------------------------------------*\
  266.  * NAME:
  267.  *    PrintHelp - Print detailed help message.
  268.  *
  269.  * PARAMETERS:
  270.  *    *NONE
  271.  *
  272.  * RETURNS:
  273.  *    *NONE
  274. \*-------------------------------------------------------------------------*/
  275. PrintHelp:
  276.     call Usage
  277.  
  278.     say
  279.     say "       -h host"
  280.     say "            Specifies the foreign host to which you are connecting"
  281.     say "       -l uid"
  282.     say "            Login ID specifies the name associated with you by the foreign"
  283.     say "            host to which you are establishing a connection"
  284.     say "       -p passwd"
  285.     say "            Password specifies your unique password, which is associated with"
  286.     say "            your user ID by the foreign host to which you are establishing a "
  287.     say "            connection"
  288.     say "       -G"
  289.     say "            Get (receive) a file from the remote system"
  290.     say "       -P"
  291.     say "            Put (send) a file to the remote system"
  292.     say "       -H"
  293.     say "            Display help text and exit.  No other actions are taken."
  294.     say "       -a"
  295.     say "            Sets the file transfer type to ASCII"
  296.     say "       -b"
  297.     say "            Sets the file transfer type to binary (default)"
  298.     say "       -v"
  299.     say "            Display version of RxFTP DLL and exit.  No other actions are"
  300.     say "            taken."
  301.     say "       -V"
  302.     say "            Operate in verbose mode"
  303.     say "       l_file"
  304.     say "            The name of the file on the local PC"
  305.     say "       r_file"
  306.     say "            The name of the file on the foreign host"
  307. return
  308. /* End of Usage */
  309.  
  310.  
  311. /*-------------------------------------------------------------------------*\
  312.  * NAME:
  313.  *    Usage - Print usage message.
  314.  *
  315.  * PARAMETERS:
  316.  *    *NONE
  317.  *
  318.  * RETURNS:
  319.  *    *NONE
  320. \*-------------------------------------------------------------------------*/
  321. Usage:
  322.     say "Usage:" getopt.!program "-h host [-l uid] [-p passwd] -G | -P [-a|-b] [-HvV] l_file r_file"
  323. return
  324. /* End of Usage */
  325.  
  326.  
  327.  
  328. /*-------------------------------------------------------------------------*\
  329.  * NAME:
  330.  *    getopt - Scan elements of stem variable getopt. for option characters
  331.  *
  332.  * PARAMETERS:
  333.  *    optstr - Specifies a string of recognized flag letters. If a letter 
  334.  *             is followed by a : (colon), the flag is expected to take a 
  335.  *             parameter that may or may not be separated from it by one or 
  336.  *             more spaces.
  337.  *
  338.  * RETURNS:
  339.  *    The getopt subroutine returns the next flag letter specified on the 
  340.  *    command line.  A value of -1 (negative one) is returned when all 
  341.  *    command line flags have been parsed.
  342.  *
  343.  *    If the getopt subroutine encounters an option character that is not 
  344.  *    contained in optstr, or a missing option argument, it returns a ? 
  345.  *    (question mark).
  346.  *
  347.  * NOTES:
  348.  *    The getopt. stem variable must have been prepared by a call to 
  349.  *    init_getopt() prior to calling getopt().
  350.  *
  351.  *    See getopt(3r) for complete information.
  352.  *-------------------------------------------------------------------------*
  353.  *    Copyright (c) 1994-95 Lawrence R Buchanan.  ALL RIGHTS RESERVED.
  354.  *
  355.  *    This program is free software; you are free to do whatever you want 
  356.  * with it.  The only requirement is that if you use this subroutine in 
  357.  * code that you distribute, that you also include this copyright message.
  358.  *    
  359.  *    This program is distributed in the hope that it will be useful, 
  360.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of 
  361.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  362. \*-------------------------------------------------------------------------*/
  363. getopt: procedure expose getopt.
  364.    parse arg optstr
  365.  
  366.    NULL = "00"x
  367.    i = getopt.!optind
  368.    getopt.!optopt = ""
  369.    getopt.!errmsg = ""
  370.  
  371.    if getopt.!sp = 1 then do
  372.       if getopt.!optind > getopt.0 | ,
  373.          substr(getopt.i, 1, 1, NULL) <> "-" | ,
  374.          substr(getopt.i, 2, 1, NULL) = NULL then
  375.          return -1
  376.       else 
  377.          if getopt.i =  "--" then do
  378.             getopt.!optind = getopt.!optind + 1
  379.             return -1
  380.          end
  381.    end
  382.  
  383.    c = substr(getopt.i, getopt.!sp+1, 1, NULL)
  384.    getopt.!optopt = c
  385.    cp = pos(c, optstr)
  386.  
  387.    if c = ":" | cp = 0 then do
  388.       getopt.!errmsg = getopt.!program ": illegal option --" c
  389.       if getopt.!opterr = 1 then 
  390.          say getopt.!errmsg
  391.       getopt.!sp = getopt.!sp + 1
  392.       if substr(getopt.i, getopt.!sp+1, 1, NULL) = NULL then do
  393.          getopt.!optind = getopt.!optind + 1
  394.          getopt.!sp = 1
  395.       end
  396.       return "?"
  397.    end
  398.  
  399.    cp = cp + 1
  400.    if substr(optstr, cp, 1, NULL) = ":" then do
  401.       if substr(getopt.i, getopt.!sp+2, 1, NULL) <> NULL then do
  402.          getopt.!optarg = substr(getopt.i, getopt.!sp+2)
  403.          getopt.!optind = getopt.!optind + 1
  404.       end
  405.       else do
  406.          getopt.!optind = getopt.!optind + 1
  407.          i = getopt.!optind
  408.          if getopt.!optind > getopt.0 then do
  409.             getopt.!errmsg = getopt.!program ": option requires an argument --" c
  410.             if getopt.!opterr = 1 then 
  411.                say getopt.!errmsg
  412.             getopt.!sp = 1
  413.             return "?"
  414.          end
  415.          else do
  416.             getopt.!optarg = getopt.i
  417.             getopt.!optind = getopt.!optind + 1
  418.          end
  419.       end
  420.  
  421.       getopt.!sp = 1
  422.    end
  423.    else do
  424.       getopt.!sp = getopt.!sp + 1
  425.       if substr(getopt.i, getopt.!sp+1, 1, NULL) = NULL then do
  426.          getopt.!sp = 1
  427.          getopt.!optind = getopt.!optind + 1
  428.       end
  429.  
  430.       getopt.!optarg = ""
  431.    end
  432.  
  433. return c
  434. /* End of getopt */
  435.  
  436.  
  437.  
  438. /*-------------------------------------------------------------------------*\
  439.  * NAME:
  440.  *    init_getopt - Initialize getopt. stem variable for getopt()
  441.  *
  442.  * PARAMETERS:
  443.  *    argv - Command line to be parsed.
  444.  *
  445.  * RETURNS:
  446.  *    The init_getopt subroutine returns the number of arguments entered 
  447.  *    on the command line.  If it detects unbalanced double quotes it 
  448.  *    returns -1.
  449.  *
  450.  *    See init_getopt(3r) for complete information.
  451.  *-------------------------------------------------------------------------*
  452.  *    Copyright (c) 1994-95 Lawrence R Buchanan.  ALL RIGHTS RESERVED.
  453.  *
  454.  *    This program is free software; you are free to do whatever you want 
  455.  * with it.  The only requirement is that if you use this subroutine in 
  456.  * code that you distribute, that you also include this copyright message.
  457.  *    
  458.  *    This program is distributed in the hope that it will be useful, 
  459.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of 
  460.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  461. \*-------------------------------------------------------------------------*/
  462. init_getopt: procedure expose getopt.
  463.    parse arg argv
  464.  
  465.    /* Initialize variables used in getopt(). */
  466.    getopt. = ""
  467.    getopt.!opterr = 1
  468.    getopt.!optind = 1
  469.    getopt.!sp     = 1
  470.  
  471.    /* Place program name in getopt.!program. */
  472.     parse source os . getopt.!program .
  473.     if os = 'OS/2' then do
  474.         getopt.!program = filespec('N', getopt.!program)
  475.         i = lastpos('.', getopt.!program)
  476.         if i > 0 then
  477.             getopt.!program = delstr(getopt.!program, i)
  478.     end
  479.  
  480.    /* Make sure the command line contains an even number of 
  481.       double quotes.  If it doesn't, return -1 to indicate an error. */
  482.    if init_getopt_count_quotes(argv) // 2 then do
  483.       getopt.0 = 0
  484.       return -1
  485.    end
  486.  
  487.    i = 0
  488.    /* Load command line arguments into getopt.1 through getopt.n. */
  489.    do while argv <> ""
  490.       i = i + 1
  491.       parse var argv getopt.i argv
  492.  
  493.       /* If quoted argument, make sure we get all of it from the command line. */
  494.       if pos('"', getopt.i) > 0 then do
  495.          cnt = init_getopt_count_quotes(getopt.i)
  496.          parse var getopt.i opt '"' tmparg
  497.          getopt.i = opt || strip(tmparg, "T", '"')
  498.          if cnt = 1 then do
  499.             parse var argv remarg '"' argv
  500.             getopt.i = getopt.i remarg
  501.          end
  502.       end
  503.    end
  504.    getopt.0 = i
  505.  
  506. return getopt.0
  507. /* End of init_getopt */
  508.  
  509.  
  510.  
  511. /*-------------------------------------------------------------------------*\
  512.  * NAME:
  513.  *    init_getopt_count_quotes - Count number of double quotation marks (") 
  514.  *                               in a string
  515.  *
  516.  * PARAMETERS:
  517.  *    str - The string to count double quotation marks (") in
  518.  *
  519.  * RETURNS:
  520.  *    The number of double quotation marks (") contained in the string.
  521.  *-------------------------------------------------------------------------*
  522.  *    Copyright (c) 1994-95 Lawrence R Buchanan.  ALL RIGHTS RESERVED.
  523.  *
  524.  *    This program is free software; you are free to do whatever you want 
  525.  * with it.  The only requirement is that if you use this subroutine in 
  526.  * code that you distribute, that you also include this copyright message.
  527.  *    
  528.  *    This program is distributed in the hope that it will be useful, 
  529.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of 
  530.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  531. \*-------------------------------------------------------------------------*/
  532. init_getopt_count_quotes: procedure
  533.     parse arg str
  534.     
  535.     cnt = 0
  536.     pos = pos('"', str)
  537.     do while pos > 0
  538.         cnt = cnt + 1
  539.         pos = pos('"', str, pos+1)
  540.     end
  541.  
  542. return cnt
  543. /* End of init_getopt_count_quotes */
  544.