home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / repeat_.zip / Repeat.CMD < prev   
OS/2 REXX Batch file  |  1993-09-29  |  9KB  |  321 lines

  1. /*-----------------------------------------------------------------------------
  2.  
  3.   Name:                   REPEAT.cmd
  4.  
  5.   OS/2 REXX command file that uses MultiMedia REXX functions
  6.   to play a file a certain number of times (or continuously).
  7.  
  8.  -----------------------------------------------------------------------------*/
  9.  
  10. address cmd      /* Send commands to OS/2 command processor.  */
  11. signal on error   /* When commands fail, call "error" routine. */
  12.  
  13. trace off
  14. /*trace ?r*/
  15.  
  16. parse arg arguments        /* Fetch command line parms */
  17. arguments = STRIP(arguments)
  18.  
  19. if (arguments='' | arguments='?' | arguments='/?') then
  20.    do
  21.      call Help
  22.      exit 0
  23.    end
  24.  
  25. /****** Special argument parsing routine *******
  26.  
  27.         Examine the argument string character by character:
  28.         extract the individual arguments, considering
  29.         strings delimited by single- or double-quote characters as a unit.
  30.         This is needed to accommodate filenames with spaces in them.
  31. *******/
  32.  
  33. n   = 0       /* counts # of arguments */
  34. pos = 1       /* counts characters in the argument string */
  35. if LENGTH(arguments) > 0 then do
  36.   n = 1
  37.   arg.n = ''  /* Results in arg.1, arg.2, ... */
  38.  
  39.   do while pos <= LENGTH(arguments)
  40.     char = SUBSTR(arguments, pos, 1)
  41.  
  42.     if (char = '"') | (char = '''') then do    /* Find corresponding ending delimiter */
  43.       delimiter = char
  44.  
  45.       do FOREVER   /* until delimiter encountered not directly followed by another delimiter */
  46.         do FOREVER /* until delimiter encountered */
  47.           pos = pos + 1
  48.           if pos > LENGTH(arguments) then LEAVE
  49.           char = SUBSTR(arguments, pos, 1)
  50.           if char = delimiter then LEAVE
  51.           arg.n = arg.n || char
  52.         end
  53.         if pos > LENGTH(arguments) then LEAVE   /* End of string encountered without ending delimiter */
  54.         pos = pos + 1                           /* Skip ending delimiter */
  55.         if pos > LENGTH(arguments) then LEAVE   /* End of string encountered */
  56.         char = SUBSTR(arguments, pos, 1)        /* Get character after ending delimiter */
  57.         if char <> delimiter then LEAVE         /* If two delimiters in a row, continue... */
  58.         arg.n = arg.n || char                   /* ...including one delimiter in the argument */
  59.       end
  60.  
  61.       ITERATE
  62.     end
  63.  
  64.     if char = ' ' then do
  65.       n = n + 1
  66.       arg.n = ''
  67.       do until (pos > LENGTH(arguments)) | (SUBSTR(arguments, pos, 1) <> ' ')
  68.         pos = pos + 1
  69.       end
  70.       ITERATE
  71.     end
  72.  
  73.     arg.n = arg.n || char
  74.     pos = pos + 1
  75.   end
  76. end
  77.  
  78. do i = n + 1 to 6
  79.    arg.i = ''
  80. end
  81.  
  82. parse var arg.1 arg1a'='arg1b
  83. parse var arg.2 arg2a'='arg2b
  84. parse var arg.3 arg3a'='arg3b
  85. parse var arg.4 arg4a'='arg4b
  86. parse var arg.5 arg5a'='arg5b
  87. parse var arg.6 arg6a'='arg6b
  88.  
  89. /* initialize variables */
  90. FILE=''
  91. FROM=''
  92. TO=''
  93. DEV=''
  94. TIMEFMT=''
  95. COUNT=''
  96.  
  97. /* Set the variables. */
  98. call keyword arg1a, arg1b
  99. call keyword arg2a, arg2b
  100. call keyword arg3a, arg3b
  101. call keyword arg4a, arg4b
  102. call keyword arg5a, arg5b
  103. call keyword arg6a, arg6b
  104.  
  105. /* Load the DLL, initialize MCI REXX support */
  106. rc = RXFUNCADD('mciRxInit','MCIAPI','mciRxInit')
  107. InitRC = mciRxInit()
  108. MciCmd = 'open'
  109.  
  110. /*
  111. ** Check to see if the FILE && DEV variables are valid.
  112. */
  113.      if FILE<>'' then
  114.         do
  115.           if DEV<>'' then
  116.              MciCmd = MciCmd '"'||FILE||'"' 'type' DEV
  117.           else
  118.              MciCmd = MciCmd '"'||FILE||'"'
  119.         end
  120.      else if DEV<>'' then
  121.         MciCmd = MciCmd DEV
  122.      else
  123.            do
  124.              call Help
  125.              exit 0
  126.            end
  127.  
  128. /*
  129. ** Append the rest of the command line.
  130. */
  131.     MciCmd = MciCmd 'alias rexxalias wait'
  132.  
  133. /*
  134. ** Issue the open command.
  135. */
  136.     MacRC = SendString(MciCmd)
  137.      if MacRC <> 0 then signal ErrExit
  138.      else
  139.      do
  140.        if DEV='' then    /* device not specified */
  141.          do     /* determine the device type */
  142.           MacRC = SendString("capability rexxalias device type wait")
  143.           if MacRC <> 0 then
  144.               do
  145.                  junk = SendString("close rexxalias wait")
  146.                  signal ErrExit
  147.               end
  148.          end
  149.        else   /* set the device specified as the device type */
  150.          RetSt = DEV
  151.  
  152.        /* If a wave file is to be played then do a status length */
  153.        /* to determine if the wave file exists.  A wave file is  */
  154.        /* the only type of device that if it doesn't exist and   */
  155.        /* you play it, it won't come back as file not found      */
  156.        if TRANSLATE(RetSt) = 'WAVEAUDIO' then
  157.          do
  158.             MacRC = SendString("status rexxalias length wait")      /* If length is 0 no file exists */
  159.              if MacRC <> 0 then
  160.               do
  161.                  junk = SendString("close rexxalias wait")
  162.                  signal ErrExit
  163.               end
  164.              if RetSt = 0 then
  165.               do
  166.                  junk = SendString("close rexxalias wait")
  167.                  ErrRC = 70555
  168.                  MacRC = mciRxGetErrorString(ErrRC, 'ErrStVar')
  169.                  say 'mciRxGetErrorString('ErrRC') =' ErrStVar
  170.                  signal ErrExit
  171.               end
  172.          end
  173.      end
  174.  
  175. /*
  176. ** Exercise mciRxGetDeviceID function
  177. */
  178. DeviceID = mciRxGetDeviceID(""rexxalias"")
  179.  
  180. /*
  181. **  Check to see if a time format was given.
  182. */
  183. if TIMEFMT <> '' then
  184. do
  185.     MciCmd = 'set rexxalias time format' TIMEFMT 'wait'
  186.     MacRC = SendString(MciCmd)
  187.      if MacRC <> 0 then
  188.         do
  189.          junk = SendString("close rexxalias wait")
  190.          signal ErrExit
  191.         end
  192. end
  193.  
  194. /*
  195. ** Formulate the play command.
  196. */
  197. MciCmd = 'play rexxalias'
  198.  
  199. /*
  200. ** check to see if an origin was set.
  201. */
  202.  if FROM<>'' then
  203.         MciCmd = MciCmd 'from' FROM
  204.  else   MciCmd = MciCmd 'from 1'
  205.  
  206. /*
  207. ** check to see if a terminating point was given.
  208. */
  209.  if TO<>'' then
  210.         MciCmd = MciCmd 'to' TO
  211.  
  212. /*
  213. ** append a wait onto the end of the play string.
  214. */
  215. MciCmd = MciCmd 'wait'
  216.  
  217. /*
  218. ** actually send the play string.
  219. */
  220.  
  221. do while (COUNT = '') | (COUNT > 0)
  222.    MacRC = SendString(MciCmd)
  223.      if MacRC <> 0 then
  224.         do
  225.          junk = SendString("close rexxalias wait")
  226.          signal ErrExit
  227.         end
  228.      if COUNT <> '' then COUNT = COUNT - 1
  229. end
  230.  
  231. /*
  232. ** close the instance.
  233. */
  234. MacRC = SendString("close rexxalias wait")
  235. if MacRC <> 0 then signal ErrExit
  236.  
  237. /*
  238. ** Exit, return code = 0.
  239. */
  240. exit 0
  241.  
  242. /*   --- SendString --
  243. ** Call DLL function.  Pass the command to process and the
  244. ** name of a REXX variable that will receive textual return
  245. ** information.
  246. */
  247. SendString:
  248.    arg CmndTxt
  249.    /* Last two parameters are reserved, must be set to 0           */
  250.    /* Future use of last two parms are for notify window handle    */
  251.    /* and userparm.                                                 */
  252.    MacRC = mciRxSendString(CmndTxt, 'RetSt', '0', '0')
  253.    if MacRC<>0 then
  254.       do
  255.       ErrRC = MacRC
  256.       say 'MciCmd=' CmndTxt
  257.       say 'Err:mciRxSendString RC=' ErrRC RetSt
  258.       MacRC = mciRxGetErrorString(ErrRC, 'ErrStVar')
  259.       say 'mciRxGetErrorString('ErrRC') =' ErrStVar
  260.       MacRC = ErrRC /* return the error rc */
  261.       end
  262.    return MacRC
  263.  
  264. /* -- keywords --
  265. **
  266. ** Parse the arguments according to the keywords.
  267. */
  268. keyword:
  269.         arg key, value
  270.         if key='FILE' then
  271.             FILE=value
  272.         else if key='DEV' then
  273.             DEV=value
  274.         else if key='FROM' then
  275.              FROM=value
  276.         else if key='TO' then
  277.               TO=value
  278.         else if key='TIMEFMT' then
  279.                 TIMEFMT=value
  280.         else if key='COUNT' then
  281.                 COUNT=value
  282.  
  283. return
  284.  
  285. /*  -- help --
  286. ** Display help text
  287. */
  288. Help:
  289.    say
  290.    say 'This command file plays a file or device a number of times in a row'
  291.    say 'using the MultiMedia REXX string interface.'
  292.    say
  293.    say 'repeat [FILE=filename] [DEV=device] [TIMEFMT=timefmt]'
  294.    say '         [FROM=from_position] [TO=to_position]'
  295.    say '         [COUNT=no_of_repetitions]'
  296.    say
  297.    say 'If COUNT is omitted, the file is played continuously.'
  298. return
  299.  
  300. /*  --- ErrExit --
  301. ** Common routine for error clean up/program exit.
  302. ** Gets called when commands to DLL fail.
  303. */
  304. ErrExit:
  305.    MacRC = mciRxExit()   /* Tell the DLL we're going away        */
  306.    exit 1;               /* exit, tell caller things went poorly */
  307.  
  308.  
  309. /*   ---- error --
  310. ** Routine gets control when any command to the external
  311. ** environment (usually OS/2) returns a non-zero RC.
  312. ** This routine does not get called when the macapi.dll
  313. ** returns non-zero as it is a function provider rather
  314. ** than a command environment.
  315. */
  316. error:
  317.    ErrRC = rc
  318.    say 'Error' ErrRC 'at line' sigl ', sourceline:' sourceline(sigl)
  319.    MacRC = mciRxExit()       /* Tell the DLL we're going away */
  320.    exit ErrRC                /* exit, tell caller things went poorly */
  321.