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

  1. /*-------------------------------------------------------------------------
  2.  *    Demo3 - Demonstration of GetOpt/SetupArg subroutines
  3.  *
  4.  *    This program demonsrates the usage of the GetOpt subroutine when 
  5.  *    called from another subroutine.
  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: procedure expose GetOpt. length width custom_header dflg,
  83.                 Fflg hflg nflg tflg
  84.  
  85.     errflag = 0
  86.     optstr = 'Fdh:l:ntw:'
  87.     c = GetOpt(optstr)
  88.     do while c <> -1
  89.         select
  90.             when c = 'F' then
  91.                 Fflg = 1
  92.             when c = 'd' then
  93.                 dflg = 1
  94.             when c = 'h' then do
  95.                 hflg = 1
  96.                 custom_header = GetOpt._optarg
  97.                 end
  98.             when c = 'l' then
  99.                 if datatype(GetOpt._optarg, 'N') then
  100.                     length = trunc(GetOpt._optarg)
  101.                 else
  102.                     errflag = 1
  103.             when c = 'n' then 
  104.                 nflg = 1
  105.             when c = 't' then 
  106.                 tflg = 1
  107.             when c = 'w' then 
  108.                 if datatype(GetOpt._optarg, 'N') then
  109.                     width = trunc(GetOpt._optarg)
  110.                 else
  111.                     errflag = 1
  112.             otherwise 
  113.                 do
  114.                     call Usage
  115.                     exit 2
  116.                 end
  117.         end
  118.     
  119.         if errflag then do
  120.             say GetOpt._program ': Invalid argument for option' c 
  121.             exit 2
  122.         end
  123.         c = GetOpt(optstr)
  124.     end
  125.  
  126. return
  127. /* End of DecodeSwitches */
  128.  
  129.  
  130. /*-------------------------------------------------------------------------
  131.     Print program parameters
  132.  -------------------------------------------------------------------------*/
  133. PrintParms:
  134.     say 'Program status:    Before GetOpt       After Getopt'
  135.     say '   Page length:          66           ' center( length, 12 )
  136.     say '   Page width:           72           ' center( width, 12 )
  137.     say '   Flags (dFhnt):      00000          ',
  138.         center( dflg||Fflg||hflg||nflg||tflg, 12 )
  139.     say '   Header:                            ' custom_header
  140.  
  141. return
  142. /* End of PrintParms */
  143.  
  144.  
  145. /*-------------------------------------------------------------------------
  146.     Usage - Print usage message.
  147.  -------------------------------------------------------------------------*/
  148. Usage:
  149.     say 'Usage:' ,
  150.         GetOpt._program '[-dFnt] [-h header] [-l lines] [-w width] file ...'
  151. return
  152. /* End of Usage */
  153.  
  154.  
  155.  
  156. /*-------------------------------------------------------------------------
  157.     GetOpt - parse options from REXX program command line
  158.  
  159.     Copyright (c) 1994 Lawrence R Buchanan.  ALL RIGHTS RESERVED.
  160.  -------------------------------------------------------------------------*/
  161. GetOpt: procedure expose GetOpt.
  162.     parse arg optstr
  163.  
  164.     i = GetOpt._optind
  165.     if GetOpt._sp = 1 then do
  166.         if GetOpt._optind > GetOpt.0 | ,
  167.            substr(GetOpt.i, 1, 1, '00'x) <> '-' | ,
  168.            substr(GetOpt.i, 2, 1, '00'x) = '00'x then
  169.             return -1
  170.         else 
  171.             if GetOpt.i =  '--' then do
  172.                 GetOpt._optind = GetOpt._optind + 1
  173.                 return -1
  174.             end
  175.     end
  176.  
  177.     c = substr(GetOpt.i, GetOpt._sp+1, 1, '00'x)
  178.     GetOpt._optopt = c
  179.     cp = pos(c, optstr)
  180.  
  181.     if c = ':' | cp = 0 then do
  182.         if GetOpt._opterr = 1 then 
  183.             say GetOpt._program ': illegal option --' c
  184.         GetOpt._sp = GetOpt._sp + 1
  185.         if substr(GetOpt.i, GetOpt._sp+1, 1, '00'x) = '00'x then do
  186.             GetOpt._optind = GetOpt._optind + 1
  187.             GetOpt._sp = 1
  188.         end
  189.         return '?'
  190.     end
  191.  
  192.     cp = cp + 1
  193.     if substr(optstr, cp, 1, '00'x) = ':' then do
  194.         if substr(GetOpt.i, GetOpt._sp+2, 1, '00'x) <> '00'x then do
  195.             GetOpt._optarg = substr(GetOpt.i, GetOpt._sp+2)
  196.             GetOpt._optind = GetOpt._optind + 1
  197.         end
  198.         else do
  199.             GetOpt._optind = GetOpt._optind + 1
  200.             i = GetOpt._optind
  201.             if GetOpt._optind > GetOpt.0 then do
  202.                 if GetOpt._opterr = 1 then 
  203.                     say GetOpt._program ': option requires an argument --' c
  204.                 GetOpt._sp = 1
  205.                 return '?'
  206.             end
  207.             else do
  208.                 GetOpt._optarg = GetOpt.i
  209.                 GetOpt._optind = GetOpt._optind + 1
  210.             end
  211.         end
  212.  
  213.         GetOpt._sp = 1
  214.     end
  215.     else do
  216.         GetOpt._sp = GetOpt._sp + 1
  217.         if substr(GetOpt.i, GetOpt._sp+1, 1, '00'x) = '00'x then do
  218.             GetOpt._sp = 1
  219.             GetOpt._optind = GetOpt._optind + 1
  220.         end
  221.  
  222.         GetOpt._optarg = ''
  223.     end
  224.  
  225. return c
  226. /* End of GetOpt */
  227.  
  228.  
  229. /*-------------------------------------------------------------------------
  230.     SetupArg - Parse command-line arguments and store in stem GetOpt.
  231.  
  232.     Copyright (c) 1994 Lawrence R Buchanan.  ALL RIGHTS RESERVED.
  233.  -------------------------------------------------------------------------*/
  234. SetupArg: procedure expose GetOpt.
  235.     parse arg arglist
  236.  
  237.     /* Initialize variables used in GetOpt subroutine. */
  238.     GetOpt. = ''
  239.     GetOpt._opterr = 1
  240.     GetOpt._optind = 1
  241.     GetOpt._sp   = 1
  242.  
  243.     /* Place program name in GetOpt._program. */
  244.     parse source os . GetOpt._program .
  245.     if os = 'OS/2' then do
  246.         GetOpt._program = filespec('N', GetOpt._program)
  247.         GetOpt._program = delstr(GetOpt._program, lastpos('.', GetOpt._program))
  248.     end
  249.  
  250.     /* Make sure the command-line contains an even number of 
  251.         quotation characters.  If it doesn't, I can't continue. */
  252.     if __SetupArg_CntQuo(arglist) // 2 then do
  253.         say GetOpt._program ': Unbalanced quotation marks in command-line'
  254.         exit 255
  255.     end
  256.  
  257.     i = 0
  258.     /* Load command-line options into GetOpt.1 through GetOpt.n. */    
  259.     do while arglist <> ''
  260.         i = i + 1
  261.         parse var arglist GetOpt.i arglist
  262.  
  263.         /* If quoted argument, make sure we get it all from command-line. */
  264.         if pos('"', GetOpt.i) > 0 then do
  265.             cnt = __SetupArg_CntQuo(GetOpt.i)
  266.             parse var GetOpt.i opt '"' tmparg
  267.             GetOpt.i = opt || strip(tmparg, 'T', '"')
  268.             if cnt = 1 then do
  269.                 parse var arglist remarg '"' arglist
  270.                 GetOpt.i = GetOpt.i remarg
  271.             end
  272.         end
  273.     end
  274.     GetOpt.0 = i
  275.  
  276. return GetOpt.0
  277. /* End of SetupArg */
  278.  
  279.  
  280. /*-------------------------------------------------------------------------
  281.     __SetupArg_CntQuo - Count number of occurrences of '"' in str
  282.  
  283.     Copyright (c) 1994 Lawrence R Buchanan.  ALL RIGHTS RESERVED.
  284.  -------------------------------------------------------------------------*/
  285. __SetupArg_CntQuo: procedure
  286.     parse arg str
  287.     
  288.     cnt = 0
  289.     pos = pos('"', str)
  290.     do while pos > 0
  291.         cnt = cnt + 1
  292.         pos = pos('"', str, pos+1)
  293.     end
  294.  
  295. return cnt
  296. /* End of __SetupArg_CntQuo */
  297.  
  298.  
  299.  
  300. /*-------------------------------------------------------------------------
  301.     This subroutine, in conjunction with a SIGNAL ON NOVALUE statement, 
  302.     will display an error message (in sort-of Microsoft format) if the 
  303.     program encounters an uninitialized variable.
  304.  -------------------------------------------------------------------------*/
  305. SIG_NoValue:
  306.     parse source . . source_file .
  307.     say GetOpt._program '(' || sigl || '): Error: Variable' condition('D'),
  308.         'was not initialized prior to use'
  309.  
  310.     exit
  311.