home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 634.lha / CyberCron_v1.3 / rexx / at.rexx next >
Encoding:
OS/2 REXX Batch file  |  1992-05-06  |  5.2 KB  |  195 lines

  1. /*rx
  2.  *  at.rexx --- simulate UNIX at command.  For use with CyberCron.
  3.  *  By Loren J. Rittle (l-rittle@uiuc.edu)   Tue May  5 01:47:11 1992
  4.  *  Updated to work with any AmigaOS shell - Wed May  6 03:32:00 1992
  5.  *
  6.  *  Copyright © 1992  Loren J. Rittle
  7.  *  Use as you will, just document your changes and keep my copyright
  8.  *  notice intact.  Feel free to mail enhancements to me.
  9.  *
  10.  *  This is not fancy yet - only handles 1 - 4 digit form of
  11.  *  time specification.  1, 2 digits means hour only. 3, 4
  12.  *  digits means 1 or 2 digits of hour and 2 digits of minute.
  13.  *  This means that 'at' jobs can only be added for the period
  14.  *  of the next 24 hours.
  15.  *
  16.  *  Usage:
  17.  *    at [-q[ac-z]] time
  18.  *    <shell script>
  19.  *    <EOF>
  20.  *
  21.  *    at -qb
  22.  *    <shell script>
  23.  *    <EOF>
  24.  *
  25.  *    at -l
  26.  *      [for list current 'at' jobs]
  27.  *    not implemented yet.
  28.  *
  29.  *    at -r <task name as given at issue time, or from list>
  30.  *      [remove an 'at' job as shown in list]
  31.  *    not implemented yet.
  32.  */
  33.  
  34. /* USER MODIFIABLE DEFAULT, EDIT TO TASTE */
  35.  
  36. /* AT_FILES should be somewhere that only at writes to.
  37.    T: is not a suitable location, IMHO.  In the future, at.rexx
  38.    and some startup code magic might know how to requeue jobs
  39.    that should have been run while power was off or after a crash
  40.    occurred.  To prepare for this, AT_FILES should be on a real
  41.    file system. */
  42. AT_FILES = 'usr:spool/at/'
  43.  
  44. /* NO GENERAL USER MODIFIABLE PARTS BELOW THIS COMMENT. */
  45.  
  46. /* Default queue to place jobs in.  Remember queue 'b' is special. */
  47. QUEUE = 'a'
  48.  
  49. HOUR = '00'
  50. MINUTE = '00'
  51.  
  52. options results
  53.  
  54. parse arg option line xline
  55.  
  56. if left(option, 2) == '-q' then
  57.   do
  58.     if length(option) ~= 3 then
  59.       do
  60.     say 'at: invalid ''-q'' parameter'
  61.     exit 10
  62.       end
  63.     QUEUE = right(option, 1)
  64.     if pos(QUEUE, xrange('a','z')) == 0 then
  65.       do
  66.     say 'at: invalid queue name,' QUEUE
  67.     exit 10
  68.       end
  69.   end
  70. else
  71.   parse arg line xline
  72.  
  73. if QUEUE == 'b' then
  74.   do
  75.     if line ~= '' then
  76.       do
  77.     say 'at: time can''t be given for use with b[atch] queue'
  78.     exit 10
  79.       end
  80.     HOUR = '*'
  81.     MINUTE = '*'
  82.   end
  83. else
  84.   do
  85.     if line == '' then
  86.       do
  87.     say 'at: time must be given for use with' QUEUE 'queue'
  88.     exit 10
  89.       end
  90.     if length(line) > 4 | xline ~= '' | ~datatype(line, 'N') then
  91.       do
  92.     say 'at: "'line'" is not a valid time, must be of form: H, HH, HMM, HHMM'
  93.     exit 10
  94.       end
  95.     if length(line) > 2 then
  96.       do
  97.     MINUTE = right(line, 2)
  98.     HOUR = left(line, length(line) - 2)
  99.       end
  100.     else
  101.       HOUR = line
  102.     if MINUTE < 0 | MINUTE > 59 | HOUR < 0 | HOUR > 23 then
  103.       do
  104.     say 'at: "'line'" is not in the range of a valid time'
  105.     exit 10
  106.       end
  107.   end
  108.  
  109. if ~open('id','id:','R') then
  110.   do
  111.     say 'at: requires the ID: device to function properly'
  112.     exit 10
  113.   end
  114. ID = readln('id')
  115. call close('id')
  116.  
  117.  
  118. if QUEUE == 'b' then
  119.   scriptfile = AT_FILES'at.'ID'.'QUEUE
  120. else
  121.   scriptfile = AT_FILES'at.'ID'.'QUEUE'.'HOUR||MINUTE
  122.  
  123. if ~open('scriptfile', scriptfile, 'W') then
  124.   do
  125.     say 'at: can''t open' scriptfile 'for output'
  126.     exit 10
  127.   end
  128. call writeln('scriptfile', 'cd' '22'x||pragma('D')||'22'x)
  129. call writeln('scriptfile', 'stack' pragma('S', 4000))
  130. if left(address(), 4) == 'WSH_' then
  131.   do
  132.     /*
  133.      *  If we have a WShell under us, then try to propagate
  134.      *  local environment variables.  If you don't, I'm not
  135.      *  sorry for you, read comments below. :-)
  136.      */
  137.     'set | execio stem VARS.'
  138.     do i = 1 to VARS.0
  139.       if word(VARS.i, 1) ~= 'process' then
  140.     call writeln('scriptfile', 'set' VARS.i)
  141.     end
  142.   end
  143. else
  144.   say 'warning: WShell not present under your ARexx, local environment variables can''t be propagated'
  145. do forever
  146.   line = readln(stdin)
  147.   if eof(stdin) then
  148.     leave
  149.   call writeln('scriptfile', line)
  150. end
  151. call writeln('scriptfile', 'run <nil: >nil: delete' scriptfile)
  152. call close('scriptfile')
  153.  
  154. address command 'protect' scriptfile '+s'
  155.  
  156. USER = getenv('USER')
  157. if left(address(), 4) == 'WSH_' then
  158.   do
  159.     /*
  160.      *  If we have a WShell under us, then try to get a local
  161.      *  environment variable with the name USER to override
  162.      *  the global USER setting.
  163.      *  If you don't have a WShell under you, then why not? :-)
  164.      *  Basically, you are less than human if you don't have a WShell
  165.      *  under you.  If everyone had a WShell under their ARexx,
  166.      *  I wouldn't have to write such weird ARexx code.  I hate you
  167.      *  if you don't WShell under your ARexx, cause you make my life
  168.      *  Hell!  Does anyone read these comments, or I'm I wasting
  169.      *  my time here? :-)
  170.      */
  171.     /* AmigaDOG braindamage: */
  172.     'get USER >nil:'
  173.     if rc == 0 then
  174.       'get USER | execio var USERX'
  175.     else
  176.       USERX = ''
  177.     if USERX ~= '' then
  178.       USER = USERX
  179.   end
  180. else
  181.   say 'warning: WShell not present under your ARexx, local environment variable USER can''t be checked'
  182.  
  183. if USER == '' then
  184.   do
  185.     say 'warning: USER environment variable not set, mail will be sent to root'
  186.     USER = 'root'
  187.   end
  188.  
  189. address CYBERCRON 'ADD_EVENT :MAILUSER' USER ':EXECONCE :OBEYQUEUE' QUEUE MINUTE HOUR '* * *' scriptfile
  190.  
  191. DATE = date('i')
  192. TIME = time()
  193. say 'warning: commands will be executed using your UserShell'
  194. say 'job' scriptfile 'at' left(date('w', DATE), 3) date('m', DATE) left(date('e', DATE), 2) TIME left(date('s', DATE), 4)
  195.