/* REXX LDOW determine n-th weekday of the month Use '(routine name) ?' for HELP-text. Written by Frank Clarke rexxhead@yahoo.com 20031002 Impact Analysis . SYSEXEC TRAPOUT Modification History 20040120 fxc REXXSKEL to v.20040120; 20050110 fxc correct error message; 20050125 fxc enable STACK 20230726 fxc adjust HELP; 20230806 fxc chg SYSPROC to SYSEXEC in Impact Analysis; */ arg argline address TSO /* REXXSKEL ver.20040120 */ arg parms "((" opts signal on syntax signal on novalue call TOOLKIT_INIT /* conventional start-up -*/ rc = Trace("O"); rc = Trace(tv) info = parms /* to enable parsing */ call A_INIT /* -*/ call B_CALC /* -*/ retdate = result if retdate = 0 then, if \sw.0terse then, msg = "There is no matching date in" ccyy"/"monum else msg = 0 else do /* Example using 3TU200307 */ cardinal = WordPos(seq,"1 2 3 4 5 L") /* 3 (for 3rd) */ ordinal = Word(ordlist,cardinal) /* rd */ daypoint = WordPos(dow,daylist) /* 3 (TU) */ dayname = Word(daynames,daypoint) /* Tuesday */ parse var mo ccyy 5 monum . /* 2003 07 */ mthname = Word(mthlist,monum) /* July (7th month) */ msg = "The" seq""ordinal dayname "of" mthname"," ccyy, "is" ccyy"-"monum"-"Right(result,2,0) if sw.0terse then , msg = ccyy"-"monum"-"Right(result,2,0) end push msg /* */ if \sw.stack_ret then call DUMP_QUEUE("QUIET") /* -*/ exit /*@ LDOW */ /* Initialization . ----------------------------------------------------------------- */ A_INIT: /*@ */ if branch then call BRANCH address TSO result = 0 ordlist = "st nd rd th th ast" daylist = "SU MO TU WE TH FR SA SU MO TU WE TH FR SA" daynames = "Sunday Monday Tuesday Wednesday Thursday Friday", "Saturday" mthlist = "January February March April May June", "July August September October November December" days. = 31 /* jan mar may jul aug oct dec*/ days.02 = 28 /* feb */ days.04 = 30 /* apr */ days.06 = 30 /* jun */ days.09 = 30 /* sep */ days.11 = 30 /* nov */ sw.0Terse = SWITCH("TERSE") return /*@ A_INIT */ /* . ----------------------------------------------------------------- */ B_CALC: /*@ */ if branch then call BRANCH address TSO today = Date("S") /* 20030923 */ parse var today ccyy 5 mm 7 dd . parse var info tag . /* 3TH200308 */ parse var tag seq 2 dow 4 mo . /* 3 TH 200308 */ if WordPos(seq,"1 2 3 4 5 L") = 0 then do helpmsg =, "The 1st character of the parm must be 1 through 5 or L", "designating the n-th or Last weekday of the month." call HELP /* ...and don't come back */ end /* bad seq */ if Length(mo) = 6 then do /* req for specific year */ end else, if Length(mo) = 2 then do /* req for this year */ if mm > mo then ccyy = ccyy + 1 /* calc for next year */ mo = ccyy""mo end else do /* not 2 or 6 */ helpmsg =, "Parm must be either 5 or 9 characters consisting of", "a single digit (or L) plus a two-character day", "plus an optional 4-digit year", "plus a 2-digit month." call HELP /* ...and don't come back */ end monum = Right(mo,2) days.02 = 28 + (ccyy//4=0) - (ccyy//100=0) + (ccyy//400=0) base_start = Date("B",mo""01,"S") day_start = Left( Date("W",base_start,"B") ,2) upper day_start point = WordPos(day_start,daylist) /* starting point */ target = WordPos(dow,daylist,point) offset = target - point /* could be zero to 6 */ datelist = "" date = 1 + offset /* date of 1st xxxday */ do while(date <= days.monum) datelist = datelist date date = date + 7 end /* date */ /* datelist now contains the dates of all the xxxdays in the month*/ if seq = "L" then, /* Last */ date = Word(datelist,Words(datelist)) else, if seq > Words(datelist) then date = 0 else, date = Word(datelist,seq) return(date) /*@ B_CALC */ /* . ----------------------------------------------------------------- */ LOCAL_PREINIT: /*@ customize opts */ address TSO sw.stack_ret = SWITCH("STACK") return /*@ LOCAL_PREINIT */ /* subroutines below LOCAL_PREINIT are not selected by SHOWFLOW */ /* . ----------------------------------------------------------------- */ HELP: /*@ */ address TSO;"CLEAR" ; say "" if helpmsg <> "" then say helpmsg; say "" ex_nam = Left(exec_name,8) /* predictable size */ say " "ex_nam" calculates the n-th day of a given month. " say " " say " Syntax: "ex_nam" identifier " say " TERSE " say " (( STACK " say " " say " identifier specifies the answer to be returned. It is in " say " the form: #DDyyyyMM where: " say " # is the week of the month: 1 2 3 4 5 or L (last) " say " DD is the day-abbreviation: SU MO TU WE TH FR SA " say " yyyy is an optional 4-digit year " say " MM is the 2-digit month, 01-12 " say " " say " Thus, 3TU200306 is the 3rd Tuesday of June, 2003. " say " LFR12 is the last Friday of the December to come " say " (or this month if called in December). " say " " say " TERSE returns only the ISO-date for the identifier. If no" say " date qualifies, '0' is returned. " say " " say " STACK causes" exec_name "to return its answer by pushing " say " it onto the stack. " say " " "NEWSTACK"; pull ; "CLEAR" ; "DELSTACK" say " " say " Debugging tools provided include: " say " " say " BRANCH show all paragraph entries. " say " " say " TRACE tv will use value following TRACE to place the " say " execution in REXX TRACE Mode. " say " " say " " say " Debugging tools can be accessed in the following manner: " say " " say " TSO "ex_nam" parameters (( debug-options " say " " say " For example " say " " say " TSO "ex_nam" (( MONITOR TRACE ?R " if sysvar("SYSISPF") = "ACTIVE" then, address ISPEXEC "CONTROL DISPLAY REFRESH" exit /*@ HELP */ /* . ----------------------------------------------------------------- */ BRANCH: Procedure expose, /*@ */ sigl exec_name rc = trace("O") /* we do not want to see this */ arg brparm . origin = sigl /* where was I called from ? */ do currln = origin to 1 by -1 /* inch backward to label */ if Right(Word(Sourceline(currln),1),1) = ":" then do parse value sourceline(currln) with pgfname ":" . /* Label */ leave ; end /* name */ end /* currln */ select when brparm = "NAME" then return(pgfname) /* Return full name */ when brparm = "ID" then do /* wants the prefix */ parse var pgfname pgfpref "_" . /* get the prefix */ return(pgfpref) end /* brparm = "ID" */ otherwise say left(sigl,6) left(pgfname,40) exec_name "Time:" time("L") end /* select */ return /*@ BRANCH */ /* . ----------------------------------------------------------------- */ DUMP_QUEUE: /*@ Take whatever is in stack */ rc = trace("O") /* and write to the screen */ address TSO arg mode . "QSTACK" /* how many stacks? */ stk2dump = rc - tk_init_stacks /* remaining stacks */ if stk2dump = 0 & queued() = 0 then return if mode <> "QUIET" then, say "Total Stacks" rc , /* rc = #of stacks */ " Begin Stacks" tk_init_stacks , /* Stacks present at start */ " Excess Stacks to dump" stk2dump do dd = rc to tk_init_stacks by -1 /* empty each one. */ if mode <> "QUIET" then, say "Processing Stack #" dd " Total Lines:" queued() do queued();parse pull line;say line;end /* pump to the screen */ "DELSTACK" /* remove stack */ end /* dd = 1 to rc */ return /*@ DUMP_QUEUE */ /* Handle CLIST-form keywords added 20020513 . ----------------------------------------------------------------- */ CLKWD: Procedure expose info /*@ hide all except info */ arg kw kw = kw"(" /* form is 'KEY(DATA)' */ kw_pos = Pos(kw,info) /* find where it is, maybe */ if kw_pos = 0 then return "" /* send back a null, not found*/ rtpt = Pos(") ",info" ",kw_pos) /* locate end-paren */ slug = Substr(info,kw_pos,rtpt-kw_pos+1) /* isolate */ info = Delstr(info,kw_pos,rtpt-kw_pos+1) /* excise */ parse var slug (kw) slug /* drop kw */ slug = Reverse(Substr(Reverse(Strip(slug)),2)) return slug /*@CLKWD */ /* Handle multi-word keys 20020513 . ----------------------------------------------------------------- */ KEYWD: Procedure expose info /*@ hide all vars, except info*/ arg kw kw_pos = wordpos(kw,info) /* find where it is, maybe */ if kw_pos = 0 then return "" /* send back a null, not found*/ kw_val = word(info,kw_pos+Words(kw))/* get the next word */ info = Delword(info,kw_pos,2) /* remove both */ return kw_val /*@ KEYWD */ /* . ----------------------------------------------------------------- */ KEYPHRS: Procedure expose, /*@ */ info helpmsg exec_name /* except these three */ arg kp wp = wordpos(kp,info) /* where is it? */ if wp = 0 then return "" /* not found */ front = subword(info,1,wp-1) /* everything before kp */ back = subword(info,wp+1) /* everything after kp */ parse var back dlm back /* 1st token must be 2 bytes */ if length(dlm) <> 2 then /* Must be two bytes */ helpmsg = helpmsg, "Invalid length for delimiter("dlm") with KEYPHRS("kp")" if wordpos(dlm,back) = 0 then /* search for ending delimiter*/ helpmsg = helpmsg, "No matching second delimiter("dlm") with KEYPHRS("kp")" if helpmsg <> "" then call HELP /* Something is wrong */ parse var back kpval (dlm) back /* get everything b/w delim */ info = front back /* restore remainder */ return Strip(kpval) /*@ KEYPHRS */ /* . ----------------------------------------------------------------- */ NOVALUE: /*@ */ say exec_name "raised NOVALUE at line" sigl say " " say "The referenced variable is" condition("D") say " " zsigl = sigl signal SHOW_SOURCE /*@ NOVALUE */ /* . ----------------------------------------------------------------- */ SHOW_SOURCE: /*@ */ call DUMP_QUEUE /* Spill contents of stacks -*/ if sourceline() <> "0" then /* to screen */ say sourceline(zsigl) rc = trace("?R") nop exit /*@ SHOW_SOURCE */ /* . ----------------------------------------------------------------- */ SS: Procedure /*@ Show Source */ arg ssbeg ssend . if ssend = "" then ssend = 10 if \datatype(ssbeg,"W") | \datatype(ssend,"W") then return ssend = ssbeg + ssend do ssii = ssbeg to ssend ; say Strip(sourceline(ssii),'T') ; end return /*@ SS */ /* . ----------------------------------------------------------------- */ SWITCH: Procedure expose info /*@ */ arg kw sw_val = Wordpos(kw,info) > 0 /* exists = 1; not found = 0 */ if sw_val then /* exists */ info = Delword(info,Wordpos(kw,info),1) /* remove it */ return sw_val /*@ SWITCH */ /* . ----------------------------------------------------------------- */ SYNTAX: /*@ */ errormsg = exec_name "encountered REXX error" rc "in line" sigl":", errortext(rc) say errormsg zsigl = sigl signal SHOW_SOURCE /*@ SYNTAX */ /* Can call TRAPOUT. . ----------------------------------------------------------------- */ TOOLKIT_INIT: /*@ */ address TSO info = Strip(opts,"T",")") /* clip trailing paren */ parse source sys_id how_invokt exec_name DD_nm DS_nm, as_invokt cmd_env addr_spc usr_tokn parse value "" with tv helpmsg . parse value 0 "ISR00000 YES" "Error-Press PF1" with, sw. zerrhm zerralrm zerrsm if SWITCH("TRAPOUT") then do "TRAPOUT" exec_name parms "(( TRACE R" info exit end /* trapout */ sw.nested = sysvar("SYSNEST") = "YES" sw.batch = sysvar("SYSENV") = "BACK" sw.inispf = sysvar("SYSISPF") = "ACTIVE" if Word(parms,1) = "?" then call HELP /* I won't be back */ "QSTACK" ; tk_init_stacks = rc /* How many stacks? */ parse value SWITCH("BRANCH") SWITCH("MONITOR") SWITCH("NOUPDT") with, branch monitor noupdt . parse value mvsvar("SYSNAME") sysvar("SYSNODE") with, #tk_cpu node . parse value KEYWD("TRACE") "N" with tv . tk_globalvars = "exec_name tv helpmsg sw. zerrhm zerralrm ", "zerrsm zerrlm tk_init_stacks branch monitor ", "noupdt" call LOCAL_PREINIT /* for more opts -*/ return /*@ TOOLKIT_INIT */