home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: SysTools / SysTools.zip / chrdmo.zip / CHRONAT.CMD < prev    next >
OS/2 REXX Batch file  |  1994-01-17  |  14KB  |  492 lines

  1. /**
  2. ***  ┌───────────────────────────────────────────────────────────────────┐
  3. ***  │ ChronAt v1.0                                                      │
  4. ***  │═══════════════════════════════════════════════════════════════════│
  5. ***  │ This will parse the command line parameters, validate the input   │
  6. ***  │ and call the EXE to pass the command line information to the      │
  7. ***  │ Chron application.  REXX was chosen to handle the parsing since   │
  8. ***  │ it is considerably more adept than C at this.  It is the intent   │
  9. ***  │ for the functionality of this code to be incorporated into the    │
  10. ***  │ executable.                                                       │
  11. ***  │═══════════════════════════════════════════════════════════════════│
  12. ***  │               Copyright (c) 1993, Hilbert Computing               │
  13. ***  └───────────────────────────────────────────────────────────────────┘
  14. **/
  15.  
  16. call LoadFunctions
  17.  
  18. parse arg Options
  19.  
  20. /* Set default options */
  21.  
  22. Opt. = ''
  23. Opt.Flag.F = 'o'
  24. Opt.Parm.1 = date('U')
  25. Opt.Parm.2 = left(time(),5)
  26.  
  27. call ParseOptions Options
  28.  
  29. if Opt.Parm.1 = '=' then Opt.Parm.1 = date('U')
  30. if Opt.Parm.2 = '=' then Opt.Parm.2 = left(time(),5)
  31.  
  32. if Opt.Flag.SYNTAX = '+' then
  33.    call Syntax
  34.  
  35. /* Determine the event type */
  36.  
  37. select
  38.    when Opt.Flag.P <> '' then
  39.       EventType = 'P'
  40.    when Opt.Flag.M <> '' then
  41.       EventType = 'M'
  42.    otherwise
  43.       do
  44.       say 'You must enter a required parameter flag of "-P" (for program) or "-M" (for'
  45.       say 'message) event.'
  46.       say
  47.       call Syntax
  48.       end
  49. end /* select */
  50.  
  51. StartDate = ValidateDate(Opt.Parm.1)
  52. StartTime = ValidateTime(Opt.Parm.2)
  53. Freq      = ValidateFreq(Opt.Flag.F)
  54. Shell     = ValidateShell(Opt.Flag.S)
  55.  
  56. /* Make sure we can find the CHRONCLI command */
  57.  
  58. Cli = SysSearchPath("PATH","CHRONCLI.EXE")
  59. if Cli = '' then
  60.    do
  61.    /* Look in the same directory as this exec */
  62.  
  63.    Cli = ThisDirectory() || "\CHRONCLI.EXE"
  64.    if Exists(Cli) = 0 then
  65.       do
  66.       say "Cannot locate the CHRONCLI.EXE file."
  67.       exit
  68.       end
  69.    end
  70.  
  71. /* Handle the string values */
  72.  
  73. code = value("CHRON.PGM",    Opt.Flag.P,"OS2ENVIRONMENT")
  74. code = value("CHRON.PARM",   Opt.Flag.A,"OS2ENVIRONMENT")
  75. code = value("CHRON.DIR",    Opt.Flag.D,"OS2ENVIRONMENT")
  76. code = value("CHRON.MESSAGE",Opt.Flag.M,"OS2ENVIRONMENT")
  77. code = value("CHRON.NAME",   Opt.Flag.N,"OS2ENVIRONMENT")
  78.  
  79. '@'Cli '"'EventType StartDate StartTime Freq Shell'"'
  80. exit
  81.  
  82.  
  83. ValidateFreq: procedure
  84.    /**
  85.    ***  This will validate the frequency
  86.    **/
  87.  
  88.    arg Freq 2 Ord
  89.  
  90.    select
  91.       when Freq = 'O' then nop
  92.       when Freq = 'H' then nop
  93.       when Freq = 'K' then nop
  94.       when Freq = 'D' then nop
  95.       when Freq = 'W' then nop
  96.       when Freq = 'M' then nop
  97.       when Freq = 'Y' then nop
  98.       otherwise
  99.          do
  100.          say "Invalid frequency"
  101.          say
  102.          call Syntax
  103.          end
  104.    end /* select */
  105.  
  106.    if Ord = '' then
  107.       Ord = 1
  108.  
  109.    if datatype(Ord,"N") = 0 then
  110.       do
  111.       say "Invalid frequency"
  112.       say
  113.       call Syntax
  114.       end
  115.  
  116.    if Ord <= 0 then
  117.       do
  118.       say "Invalid frequency"
  119.       say
  120.       call Syntax
  121.       end
  122.  
  123.    return Freq Ord
  124.  
  125.  
  126. ValidateDate: procedure
  127.    /**
  128.    ***  This will validate the date passed and reformat it to a consistent
  129.    ***  format.
  130.    **/
  131.  
  132.    parse arg PassedDate
  133.  
  134.    select
  135.       when pos('-',PassedDate) > 0 then
  136.          parse var PassedDate mm '-' dd '-' yy
  137.       when pos('/',PassedDate) > 0 then
  138.          parse var PassedDate mm '/' dd '/' yy
  139.       when pos('.',PassedDate) > 0 then
  140.          parse var PassedDate mm '.' dd '.' yy
  141.       otherwise
  142.          do
  143.          say 'Invalid date.'
  144.          say
  145.          call Syntax
  146.          end
  147.    end /* select */
  148.  
  149.    /* Make sure the year is numeric */
  150.  
  151.    Numeric = datatype(mm,"N") + datatype(dd,"N") + datatype(yy,"N")
  152.    if Numeric <> 3 then
  153.       do
  154.       say "Invalid date."
  155.       say
  156.       call Syntax
  157.       end
  158.  
  159.    /* Make sure the year is the full 4 digits */
  160.  
  161.    if yy < 100 then
  162.       yy = 1900 + yy
  163.    /* Check Ranges */
  164.  
  165.    if mm < 1 | mm > 12 then
  166.       do
  167.       say "Invalid date."
  168.       say
  169.       call Syntax
  170.       end
  171.  
  172.    select
  173.       when mm =  1 then MaxDays = 31
  174.       when mm =  2 then MaxDays = 29
  175.       when mm =  3 then MaxDays = 31
  176.       when mm =  4 then MaxDays = 30
  177.       when mm =  5 then MaxDays = 31
  178.       when mm =  6 then MaxDays = 30
  179.       when mm =  7 then MaxDays = 31
  180.       when mm =  8 then MaxDays = 31
  181.       when mm =  9 then MaxDays = 30
  182.       when mm = 10 then MaxDays = 31
  183.       when mm = 11 then MaxDays = 30
  184.       when mm = 12 then MaxDays = 31
  185.       otherwise
  186.          nop
  187.    end /* select */
  188.  
  189.    /* Validate the days */
  190.  
  191.    if dd < 1 | dd > MaxDays then
  192.       do
  193.       say "Invalid date."
  194.       say
  195.       call Syntax
  196.       end
  197.    return mm'/'dd'/'yy
  198.  
  199.  
  200. ValidateTime: procedure
  201.    /**
  202.    ***  This will validate the time and return a time of consistent format
  203.    **/
  204.  
  205.    parse arg PassedTime
  206.  
  207.    select
  208.       when pos(':',PassedTime) > 0 then
  209.          parse var PassedTime hh ':' mm
  210.       when pos('.',PassedTime) > 0 then
  211.          parse var PassedTime hh '.' mm
  212.       otherwise
  213.          do
  214.          say 'Invalid time.'
  215.          say
  216.          call Syntax
  217.          end
  218.    end /* select */
  219.  
  220.    Numeric = datatype(hh,"N") + datatype(mm,"N")
  221.    if Numeric <> 2 then
  222.       do
  223.       say "Invalid time."
  224.       say
  225.       call Syntax
  226.       end
  227.  
  228.    if hh < 0 | hh > 23 then
  229.       do
  230.       say "Invalid time."
  231.       say
  232.       call Syntax
  233.       end
  234.  
  235.    if mm < 0 | mm > 59 then
  236.       do
  237.       say "Invalid time."
  238.       say
  239.       call Syntax
  240.       end
  241.    return hh':'mm
  242.  
  243.  
  244. ValidateShell: procedure
  245.    /**
  246.    ***  Validate the shell option
  247.    **/
  248.  
  249.    parse arg Shell
  250.  
  251.    select
  252.       when Shell = ''  then Shell = '-'
  253.       when Shell = '-' then Shell = '-'
  254.       when Shell = '+' then Shell = '+'
  255.       otherwise
  256.          do
  257.          say 'Invalid shell switch.'
  258.          say
  259.          call Syntax
  260.          end
  261.    end /* Select */
  262.    return Shell
  263.  
  264.  
  265. Syntax: procedure
  266.    /**
  267.    *** Display the syntax of the ChronAt command
  268.    **/
  269.  
  270.    say 'Syntax - CHRONAT [mm-dd-yy|= [hh:ss]|=] [-fo|h|k|d|w|m|y[<n>]] [-p<pgmname>]'
  271.    say '                [-a<cmdlineparms>][-d<workingdir>][-m<message>][-n<name>][-s]'
  272.    say
  273.    say 'where:  -f  -- Scheduling frequency as follows (default is O - One time):'
  274.    say '                  o - One Time     h - Hours   k - Weekdays'
  275.    say '                  d - Days         w - Weeks   m - Months'
  276.    say '                  y - Years'
  277.    say '        -p  -- Executable program name'
  278.    say '        -a  -- Command like parameters to the program'
  279.    say '        -d  -- Working directory for the program'
  280.    say '        -m  -- Message text'
  281.    say '        -n  -- Name of the event'
  282.    say '        -s  -- Run under command shell. Req''d for command files'
  283.    say
  284.    say 'Any parameter containing a blank must be surrounded by double quotes. A date'
  285.    say 'or time of "=" defaults to the current date or time.'
  286.    say
  287.    say 'Example:  CHRONAT 11/22/93 4:53 -fk3 -m"This is a test message"'
  288.    say '          will schedule a message event for every 3 weekdays.'
  289.    exit
  290.  
  291. /**
  292. *** ┌────────────────────────────────────────────────────────────────────────┐
  293. *** │ Included routines                                                      │
  294. *** └────────────────────────────────────────────────────────────────────────┘
  295. **/
  296.  
  297.  
  298. /* #include <io.rex> */
  299.  
  300. Close: procedure
  301.  
  302.    arg file
  303.    message = stream(file,c,'CLOSE')
  304.    if (message \= 'READY:') & (message \= '') then
  305.       do
  306.       say 'Error: Close failure on' file'.' message
  307.       exit
  308.       end
  309.    return file
  310.  
  311. Exists: procedure
  312.  
  313.    arg file
  314.  
  315.    file = stream(file,c,'QUERY EXIST')
  316.    if (file = '') then
  317.       return 0
  318.    else
  319.       return 1
  320.  
  321. Open: procedure
  322.  
  323.    parse arg file, rw
  324.    rw = translate(rw)
  325.  
  326.    select
  327.       when rw = 'WRITE' then
  328.          do
  329.          file_ = stream(file,c,'QUERY EXIST')
  330.          if file_ <> '' then
  331.             '@erase "'file'"'
  332.          end
  333.       when rw = 'APPEND' then
  334.          rw = ''
  335.       when rw = 'READ' then
  336.          nop
  337.       otherwise
  338.          rw = 'READ'
  339.    end /* select */
  340.  
  341.    message = stream(file,c,'OPEN' rw)
  342.    if (message \= 'READY:') then
  343.       do
  344.       say 'Error: Open failure on' file'.' message
  345.       return message
  346.       end
  347.    return file
  348.  
  349. /* #include <system.rex> */
  350.  
  351. /**
  352. *** ┌───────────────────────────────────────────────────────────────────────┐
  353. *** │ Misc system functions                                                 │
  354. *** └───────────────────────────────────────────────────────────────────────┘
  355. **/
  356.  
  357. ThisDirectory: procedure
  358.    /**
  359.    ***  This will return the directory from which this exec was run
  360.    **/
  361.  
  362.    parse source . . ThisFile
  363.    LastSlash = lastpos('\', ThisFile)
  364.    ThisDir = left(ThisFile, (LastSlash-1))
  365.    return ThisDir
  366.  
  367. /* #include <parseopt.rex> */
  368.  
  369. ParseOptions: procedure expose Opt.
  370.    /**
  371.    ***  This will parse the command line options.  Those parameters that
  372.    ***  begin with a minus (-) or forward slash (/) are considered flags
  373.    ***  and are placed in Opt.Flag.   The remaining options are placed
  374.    ***  into Opt.parm.<x>.
  375.    ***
  376.    ***  NOTE:  This code does not clear out the 'Opt.' stem variable since
  377.    ***         the caller may want to establish defaults prior to calling
  378.    ***         this code.
  379.    ***
  380.    ***  LIMITATIONS:  The code currently only looks for the double quote
  381.    ***         character (").  The apostrophe is treated like any other
  382.    ***         character.  The way this is currently coded, multiple blanks
  383.    ***         in a quoted string are compressed to a single blanks and
  384.    ***         probably should not be.
  385.    ***
  386.    **/
  387.  
  388.    parse arg arguments
  389.  
  390.    Opt.Flag.List = ''
  391.    Opt.State = 'Normal'
  392.    j = 0
  393.    do i = 1 to words(arguments)
  394.       argument = word(arguments, i)
  395.  
  396.       select
  397.          when Opt.State = 'Quoted Positional' then
  398.             do
  399.             /* Keep appending the words to this parm until an ending quote */
  400.             /* is found.                                                   */
  401.  
  402.             Opt.Parm.j = Opt.Parm.j argument
  403.             if right(argument,1) = '"' then
  404.                do
  405.                Opt.Parm.j = strip(Opt.Parm.j, 'Both', '"')
  406.                Opt.State = 'Normal'
  407.                end
  408.             end
  409.          when Opt.State = 'Quoted Flag' then
  410.             do
  411.             /* Keep appending until the terminating quote is found */
  412.  
  413.             Opt.Flag.Flagname = Opt.Flag.FlagName argument
  414.             if right(argument,1) = '"' then
  415.                do
  416.                Opt.Flag.Flagname = strip(Opt.Flag.Flagname, 'Both', '"')
  417.                Opt.State = 'Normal'
  418.                end
  419.             end
  420.          when Opt.State = 'Normal' then
  421.             do
  422.             FirstChar = left(argument, 1)
  423.             if ((FirstChar = '-') | (FirstChar = '/')) then
  424.                do
  425.                /*  This is a flag.  The value of the flag is the remainder of  */
  426.                /*  the string.  If the remainder is the null string, then it   */
  427.                /*  has an implicit value of '+' implying "on" or "true"        */
  428.  
  429.                FlagName = substr(argument, 2, 1)   /* Second character     */
  430.                FlagName = translate(FlagName)      /* Convert to uppercase */
  431.  
  432.                /* See if this flag parm is quoted */
  433.  
  434.                if substr(argument, 3, 1) = '"' then
  435.                   Opt.State = 'Quoted Flag'
  436.  
  437.                /* If any of the flag names are not a valid character for a REXX */
  438.                /* variable, we have to translate into a mnemonic.               */
  439.  
  440.                if ((FlagName < 'A') | (FlagName > 'Z')) then
  441.                   do
  442.                   select
  443.                      when FlagName = '?' then FlagName = 'SYNTAX'
  444.                      when FlagName = '!' then FlagName = 'BANG'
  445.                      when FlagName = '*' then FlagName = 'STAR'
  446.                      when FlagName = '#' then FlagName = 'POUND'
  447.                      when FlagName = '$' then FlagName = 'DOLLAR'
  448.                      when FlagName = '%' then FlagName = 'PERCENT'
  449.                      when FlagName = '^' then FlagName = 'HAT'
  450.                      when FlagName = '&' then FlagName = 'AMP'
  451.                      when FlagName = '(' then FlagName = 'LPAR'
  452.                      when FlagName = ')' then FlagName = 'RPAR'
  453.                      when FlagName = '-' then FlagName = 'DASH'
  454.                      when FlagName = '=' then FlagName = 'EQUAL'
  455.                      otherwise /* Force a syntax message */
  456.                         FlagName = 'SYNTAX'
  457.                   end /* select */
  458.                   end /* if */
  459.  
  460.                FlagValue = substr(argument, 3)     /* Remainder of string */
  461.                if FlagValue = '' then
  462.                   FlagValue = '+'
  463.  
  464.                Opt.Flag.FlagName = FlagValue
  465.                Opt.Flag.List = FlagName Opt.Flag.List
  466.                end
  467.             else /* it is a positional parameter */
  468.                do
  469.                j = j + 1
  470.                Opt.Parm.j = argument
  471.                if left(argument,1) = '"' then
  472.                   Opt.State = 'Quoted Positional'
  473.                end
  474.          end /* 'Normal' */
  475.       otherwise
  476.          nop
  477.       end /* select */
  478.    end /* do i... */
  479.    Opt.Parm.0 = j
  480.    return
  481.  
  482. /* #include LoadFunctions.rex */
  483.  
  484. LoadFunctions: procedure
  485.    /**
  486.    ***   This will load the DLL for the Rexx system functions supplied
  487.    ***   with OS/2 v2.0
  488.    **/
  489.    call RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
  490.    call SysLoadFuncs
  491.    return
  492.