%META:TOPICINFO{author="ChrisBartram" date="1149893866" format="1.1" version="1.1"}% %META:TOPICPARENT{name="Hp3000MpexXeqs"}%
PARM STRING1, AUTOABORT='NOABORT',EOF="NEW",RESET="RESET", &
     SEARCHPARMS="", ENTRY_POINT = "MAIN"

# JOBSRCH - allows a job to search itself.
# James Wilkinson - from the November 1991 "Interact" Magazine
# NOTE: Requires MPEX!
#
# A brief summary of sytax:
#
# JOBSRCH "search string",[ABORT],[SAME|ALL],[NORESET],[searchparms]
#
# search string - any string you specify.  Use the syntax of the SEARCH
# option of the MPEX PRINT command.  i.e. "*W*", '"*E*" and "Data access"'
#
# Use ~filename instead of the search string to search for all the strings
# in the referenced file.
#
# ABORT - specify this to automatically abort the job if the string is found
#
# SAME,ALL - by default JOBSRCH searches from the beginning of the $STDLIST
# or from the last JOBSRCH statement.  Use SAME to search the same lines as
# the previous JOBSRCH statement.  Use ALL to force a search from the
# beginning.
#
# NORESET - JOBSRCH sets the variable FOUND to the number of occurrences of
# the string.  Use NORESET to make FOUND cumulative instead of resetting it.
# There is also the variable FOUNDALL which is automatically cumulative.
#
# searchparms - specify any options of the ;SEARCH paramater
# of the MPEX PRINT command.
#
# Use JOBSRCH ~ABORT to kill the job

# In a job it essentially works like this:
#
# !(program runs)
# !JOBSRCH "ERROR STRING"
# !IF FOUND > 0
# !  TELLOP Job is aborting due to error!
# !  JOBSRCH ~ABORT (or !EOF)
# !ENDIF

IF "!ENTRY_POINT" = "MAIN"

  IF BOUND(STARTSPOOLREC) = FALSE
    SETVAR STARTSPOOLREC 1
    SETVAR FOUNDALL 0
  ENDIF

  IF LFT('!STRING1',1) <> '"' AND ![POS('(','!STRING1')] = 0
    SETVAR STRING2 '"!STRING1"'
  ELSE
    SETVAR STRING2 '!STRING1'
  ENDIF

  SETJCW CIERROR 0
  MPEX.PUB.VESOFT;PARM=1; INFO= &
  "JOBSRCH ' ',!AUTOABORT,!EOF,!RESET,!SEARCHPARMS,'NOT MAIN'"

  IF CIERROR <> 0
    ECHO JOBSRCH Error: Command failed; FOUND automatically set at 0
    SETVAR FOUND 0
  ENDIF

  IF ups('!STRING2') = '"~ABORT"'
    ECHO
    ECHO **********************************************************
    ECHO ****        JOBSRCH - Job string search utility       ****
    ECHO **********************************************************
    ECHO ****              JOB ABORTED ON REQUEST              ****
    ECHO **********************************************************
    ECHO
    IF !HPUSERCMDEPTH = 1
      ESCAPE
    ELSE
      RUN CI.PUB.SYS;INFO="SETJCW JCW FATAL" >$NULL
    ENDIF
    IF "!ERRFILE" = "$NULL"
      ECHO JOBSRCH Error: file not found.
    ENDIF
  ENDIF

ELSE

  SETVAR PRELIM_FOUND 0

  IF ups('!STRING2') <> '"~ABORT"'
    SETVAR DFID VEJOBINFO("#J!HPJOBNUM").OUTSPOOLFILENUM
    SETVAR SPOOLEOF VEFINFO("O!DFID.OUT.HPSPOOL").EOF

    IF LFT('!STRING2',2) = '"~'
      IF FEXISTS("![STR(!STRING2,2,26)]")
        SETVAR ERRFILE STR(!STRING2,2,26) + ",OLD"
      ELSE
        SETVAR ERRFILE "$NULL"
      ENDIF
    ELSE
      ECHO !STRING2 >TEMPERR
      SETVAR ERRFILE "TEMPERR,OLDTEMP"
    ENDIF

    IF ups("!EOF") = "NEW"
      SETVAR START_REC  STARTSPOOLREC
      SETVAR LAST_START STARTSPOOLREC
    ELSEIF ups("!EOF") = "ALL"
      SETVAR START_REC 1
    ELSEIF ups("!EOF") = "SAME"
      SETVAR START_REC LAST_START
    ENDIF

    NOMSG :PURGE TEMPFILE,TEMP

    SETJCW CIERROR 0
    PRINT O!DFID.OUT.HPSPOOL; START=!START_REC; &
      SEARCH=NOT cl "JOBSRCH ";OUT=(TEMPFILE,NEW;TEMP;DISC=10000)

    REPEAT
      SETVAR STRING2 STRRTRIM('!CURRENTREC'[0:72])
      CONTINUE
      PRINT TEMPFILE;SEARCH=!SEARCHPARMS !STRING2;OUT=$NULL
      IF CIERROR <> 0
        SETJCW CIERROR 0
        PRINT TEMPFILE;SEARCH=!STRING2;OUT=$NULL
      ENDIF
      SETVAR FOUNDALL FOUNDALL + MPEXPRINTLINESFOUND
      SETVAR PRELIM_FOUND PRELIM_FOUND + MPEXPRINTLINESFOUND
    FORRECS CURRENTREC=!ERRFILE

    SETVAR STARTSPOOLREC SPOOLEOF + 1

  ENDIF

  IF PRELIM_FOUND > 0 AND "!AUTOABORT" = "ABORT"
    SETVAR STRING2 '"~ABORT"'
  ELSE
    IF ups("!RESET") = "RESET"
      SETVAR FOUND PRELIM_FOUND
    ELSE
      SETVAR FOUND FOUND + PRELIM_FOUND
    ENDIF
  ENDIF

ENDIF

-- Main.ChrisBartram - 09 Jun 2006