home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / getopt.zip / demo2.cmd < prev    next >
OS/2 REXX Batch file  |  1994-04-21  |  8KB  |  309 lines

  1. /*-------------------------------------------------------------------------
  2.  *    Demo2 - Demonstration of GetOpt/SetupArg subroutines
  3.  *
  4.  *    This program demonsrates the usage of the GetOpt subroutine when 
  5.  *    called from a subroutine using PROCEDURE EXPOSE.
  6.  *
  7.  *    Copyright (c) 1994 Lawrence R Buchanan.  ALL RIGHTS RESERVED.
  8.  *
  9.  *    This program is free software; you are free to do whatever you 
  10.  *    want with it.  The only requirement is that if you use these 
  11.  *    subroutines in code that you distribute, that you leave the 
  12.  *    copyright messages that appear in the headers of the GetOpt and 
  13.  *    SetupArg subroutines.
  14.  *    
  15.  *    This program is distributed in the hope that it will be useful, 
  16.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of 
  17.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  18.  *
  19.  *    Usage: demo [-dFnt] [-h header] [-l lines] [-w width] file ...
  20.  -------------------------------------------------------------------------*/
  21.  
  22.  
  23. /* Check for uninitialized variables. */
  24. signal on NOVALUE name SIG_NoValue
  25.  
  26.  
  27. /*-------------------------------------------------------------------------
  28.     Setup GetOpt. stem variable for GetOpt subroutine.
  29.  
  30.     These two statements MUST appear at the beginning of any program
  31.     that uses GetOpt.
  32.  -------------------------------------------------------------------------*/
  33. parse arg args
  34. call SetupArg args
  35.  
  36. /* If no parameters issue usage message and exit. */
  37. if GetOpt.0 = 0 then do
  38.     call Usage
  39.     exit 1
  40. end
  41.  
  42.  
  43. /* Begin main program */
  44.  
  45. /* Initialize option character flags. */
  46. length = 66
  47. width  = 72
  48. custom_header = ''
  49. dflg = 0
  50. Fflg = 0
  51. hflg = 0
  52. nflg = 0
  53. tflg = 0
  54.  
  55. /* Get the option flags and arguments and set up the program environment. */
  56. call DecodeSwitches
  57.  
  58. say 'Contents of GetOpt.'
  59. do i = 0 to GetOpt.0
  60.     say '    GetOpt.' || i '=' GetOpt.i
  61. end
  62.  
  63. say
  64. call PrintParms
  65. say
  66. say 'GetOpt._optind =' GetOpt._optind
  67. say
  68.  
  69. say 'Remaining parameters are:'
  70. do i = GetOpt._optind to GetOpt.0
  71.     say '    GetOpt.' || i '=' GetOpt.i
  72. end
  73.  
  74. exit
  75. /* End of main program */
  76.  
  77.  
  78.  
  79. /*-------------------------------------------------------------------------
  80.     DecodeSwitches - decodes command-line options
  81.  -------------------------------------------------------------------------*/
  82. DecodeSwitches:
  83.     errflag = 0
  84.     optstr = 'Fdh:l:ntw:'
  85.     c = GetOpt(optstr)
  86.     do while c <> -1
  87.         select
  88.             when c = 'F' then
  89.                 Fflg = 1
  90.             when c = 'd' then
  91.                 dflg = 1
  92.             when c = 'h' then do
  93.                 hflg = 1
  94.                 custom_header = GetOpt._optarg
  95.                 end
  96.             when c = 'l' then
  97.                 if datatype(GetOpt._optarg, 'N') then
  98.                     length = trunc(GetOpt._optarg)
  99.                 else
  100.                     errflag = 1
  101.             when c = 'n' then 
  102.                 nflg = 1
  103.             when c = 't' then 
  104.                 tflg = 1
  105.             when c = 'w' then 
  106.                 if datatype(GetOpt._optarg, 'N') then
  107.                     width = trunc(GetOpt._optarg)
  108.                 else
  109.                     errflag = 1
  110.             otherwise 
  111.                 do
  112.                     call Usage
  113.                     exit 2
  114.                 end
  115.         end
  116.     
  117.         if errflag then do
  118.             say GetOpt._program ': Invalid argument for option' c 
  119.             exit 2
  120.         end
  121.         c = GetOpt(optstr)
  122.     end
  123.  
  124. return
  125. /* End of DecodeSwitches */
  126.  
  127.  
  128. /*-------------------------------------------------------------------------
  129.     Print program parameters
  130.  -------------------------------------------------------------------------*/
  131. PrintParms:
  132.     say 'Program status:    Before GetOpt       After Getopt'
  133.     say '   Page length:          66           ' center( length, 12 )
  134.     say '   Page width:           72           ' center( width, 12 )
  135.     say '   Flags (dFhnt):      00000          ',
  136.         center( dflg||Fflg||hflg||nflg||tflg, 12 )
  137.     say '   Header:                            ' custom_header
  138.  
  139. return
  140. /* End of PrintParms */
  141.  
  142.  
  143. /*-------------------------------------------------------------------------
  144.     Usage - Print usage message.
  145.  -------------------------------------------------------------------------*/
  146. Usage:
  147.     say 'Usage:' ,
  148.         GetOpt._program '[-dFnt] [-h header] [-l lines] [-w width] file ...'
  149. return
  150. /* End of Usage */
  151.  
  152.  
  153.  
  154. /*-------------------------------------------------------------------------
  155.     GetOpt - parse options from REXX program command line
  156.  
  157.     Copyright (c) 1994 Lawrence R Buchanan.  ALL RIGHTS RESERVED.
  158.  -------------------------------------------------------------------------*/
  159. GetOpt: procedure expose GetOpt.
  160.     parse arg optstr
  161.  
  162.     i = GetOpt._optind
  163.     if GetOpt._sp = 1 then do
  164.         if GetOpt._optind > GetOpt.0 | ,
  165.            substr(GetOpt.i, 1, 1, '00'x) <> '-' | ,
  166.            substr(GetOpt.i, 2, 1, '00'x) = '00'x then
  167.             return -1
  168.         else 
  169.             if GetOpt.i =  '--' then do
  170.                 GetOpt._optind = GetOpt._optind + 1
  171.                 return -1
  172.             end
  173.     end
  174.  
  175.     c = substr(GetOpt.i, GetOpt._sp+1, 1, '00'x)
  176.     GetOpt._optopt = c
  177.     cp = pos(c, optstr)
  178.  
  179.     if c = ':' | cp = 0 then do
  180.         if GetOpt._opterr = 1 then 
  181.             say GetOpt._program ': illegal option --' c
  182.         GetOpt._sp = GetOpt._sp + 1
  183.         if substr(GetOpt.i, GetOpt._sp+1, 1, '00'x) = '00'x then do
  184.             GetOpt._optind = GetOpt._optind + 1
  185.             GetOpt._sp = 1
  186.         end
  187.         return '?'
  188.     end
  189.  
  190.     cp = cp + 1
  191.     if substr(optstr, cp, 1, '00'x) = ':' then do
  192.         if substr(GetOpt.i, GetOpt._sp+2, 1, '00'x) <> '00'x then do
  193.             GetOpt._optarg = substr(GetOpt.i, GetOpt._sp+2)
  194.             GetOpt._optind = GetOpt._optind + 1
  195.         end
  196.         else do
  197.             GetOpt._optind = GetOpt._optind + 1
  198.             i = GetOpt._optind
  199.             if GetOpt._optind > GetOpt.0 then do
  200.                 if GetOpt._opterr = 1 then 
  201.                     say GetOpt._program ': option requires an argument --' c
  202.                 GetOpt._sp = 1
  203.                 return '?'
  204.             end
  205.             else do
  206.                 GetOpt._optarg = GetOpt.i
  207.                 GetOpt._optind = GetOpt._optind + 1
  208.             end
  209.         end
  210.  
  211.         GetOpt._sp = 1
  212.     end
  213.     else do
  214.         GetOpt._sp = GetOpt._sp + 1
  215.         if substr(GetOpt.i, GetOpt._sp+1, 1, '00'x) = '00'x then do
  216.             GetOpt._sp = 1
  217.             GetOpt._optind = GetOpt._optind + 1
  218.         end
  219.  
  220.         GetOpt._optarg = ''
  221.     end
  222.  
  223. return c
  224. /* End of GetOpt */
  225.  
  226.  
  227. /*-------------------------------------------------------------------------
  228.     SetupArg - Parse command-line arguments and store in stem GetOpt.
  229.  
  230.     Copyright (c) 1994 Lawrence R Buchanan.  ALL RIGHTS RESERVED.
  231.  -------------------------------------------------------------------------*/
  232. SetupArg: procedure expose GetOpt.
  233.     parse arg arglist
  234.  
  235.     /* Initialize variables used in GetOpt subroutine. */
  236.     GetOpt. = ''
  237.     GetOpt._opterr = 1
  238.     GetOpt._optind = 1
  239.     GetOpt._sp   = 1
  240.  
  241.     /* Place program name in GetOpt._program. */
  242.     parse source os . GetOpt._program .
  243.     if os = 'OS/2' then do
  244.         GetOpt._program = filespec('N', GetOpt._program)
  245.         GetOpt._program = delstr(GetOpt._program, lastpos('.', GetOpt._program))
  246.     end
  247.  
  248.     /* Make sure the command-line contains an even number of 
  249.         quotation characters.  If it doesn't, I can't continue. */
  250.     if __SetupArg_CntQuo(arglist) // 2 then do
  251.         say GetOpt._program ': Unbalanced quotation marks in command-line'
  252.         exit 255
  253.     end
  254.  
  255.     i = 0
  256.     /* Load command-line options into GetOpt.1 through GetOpt.n. */    
  257.     do while arglist <> ''
  258.         i = i + 1
  259.         parse var arglist GetOpt.i arglist
  260.  
  261.         /* If quoted argument, make sure we get it all from command-line. */
  262.         if pos('"', GetOpt.i) > 0 then do
  263.             cnt = __SetupArg_CntQuo(GetOpt.i)
  264.             parse var GetOpt.i opt '"' tmparg
  265.             GetOpt.i = opt || strip(tmparg, 'T', '"')
  266.             if cnt = 1 then do
  267.                 parse var arglist remarg '"' arglist
  268.                 GetOpt.i = GetOpt.i remarg
  269.             end
  270.         end
  271.     end
  272.     GetOpt.0 = i
  273.  
  274. return GetOpt.0
  275. /* End of SetupArg */
  276.  
  277.  
  278. /*-------------------------------------------------------------------------
  279.     __SetupArg_CntQuo - Count number of occurrences of '"' in str
  280.  
  281.     Copyright (c) 1994 Lawrence R Buchanan.  ALL RIGHTS RESERVED.
  282.  -------------------------------------------------------------------------*/
  283. __SetupArg_CntQuo: procedure
  284.     parse arg str
  285.     
  286.     cnt = 0
  287.     pos = pos('"', str)
  288.     do while pos > 0
  289.         cnt = cnt + 1
  290.         pos = pos('"', str, pos+1)
  291.     end
  292.  
  293. return cnt
  294. /* End of __SetupArg_CntQuo */
  295.  
  296.  
  297.  
  298. /*-------------------------------------------------------------------------
  299.     This subroutine, in conjunction with a SIGNAL ON NOVALUE statement, 
  300.     will display an error message (in sort-of Microsoft format) if the 
  301.     program encounters an uninitialized variable.
  302.  -------------------------------------------------------------------------*/
  303. SIG_NoValue:
  304.     parse source . . source_file .
  305.     say GetOpt._program '(' || sigl || '): Error: Variable' condition('D'),
  306.         'was not initialized prior to use'
  307.  
  308.     exit
  309.