/*REXX*/
/* TRACE I */
/*-------------------------------------------------------------------
  This SPF Macro is used to display a help screen for a keyword based
  on the extension of the filename such as SPF, REX, CMD, PHP, etc.
  Works with web pages (on or off-line) and with Windows
  Compiled HTML Manuals (CHM).

  Using SPFPRO/SPFPC4: Assign a key to the H3.SPF macro, place cursor
  over keyword in document and press key.

  SPF/PC, SPFSE, XE or non-REXX integrated editor, use the editor's DOS command
  to call the REXX interpreter passing it the macro path/name, language and
  keyword.
  Example:  SPF/PC:  TSO c:\path\REXX.EXE C:\PATH\H3.SPF PHP REQUIRE
             SPFSE:  DOS c:\path\REXX.EXE C:\PATH\H3.SPF PHP REQUIRE
                XE:  cmd c:\path\REXX.EXE C:\PATH\H3.SPF PHP REQUIRE

  NOTE1: For SPF/PC, XE or other non-REXX integrated editor it makes
         calling this macro easier if you set up a batch script to call
         the REXX interpreter.
         EXAMPLE:  @ECHO OFF
                   REM ---------------------------------------------------------
                   REM H.BAT Calls my Help System
                   REM Presumes that H.BAT resides somewhere in the command path
                   REM ---------------------------------------------------------
                   H:\U\PCDOS\REXX.EXE H:\U\SPFPC40\MACROS\H3.SPF %1 %2
         so SPF/PC would be:   TSO H PHP REQUIRE
             SPFSE would be:   DOS H php require    (yes, spfse via a batch file can execute rexx macros)
                XE would be:   CMD h PHP REQUIRE

  For HTML or CHM type helps Internet Explorer is Invoked.  Press ALT F4
  to close that window when done.

  Change the THELP variable to point to your .BDX files.

  NOTE2:  The normal path for IE contains spaces which will not work
          for SPFPC 4.x (DOS) or any DOS based REXX interpreter.
          Hence the reason you need a DOS friendly path to IEXPLORE.
          Copy IEXPLORE.EXE to directory without spaces.  Be sure to
          change the TBROWSER path in this macro to match the new path.

  NOTE3: You can call info from the Help file or from any help file
         by entering the resquest on the command line.
         EXAMPLE:
         H3 PBW strings      regardless of what type of code is
                             being edited in from the PBW strings
                             will be displayed.
         H3 strings          Will display the strings info of the
                             current code type (Extension BAS REX)

  NOTE4: For SPFPRO users, when editing .spf/isp files the SFPRO help
         system is called.

  NOTE5: To call this macro with a double left moust click:
         SPFPRO: Edit the keyboard mouse value by selecting =0.k
                 then edit (e) the keyboard profile (such as DEFAULT)
                 then selection option 6 (Mouse)
                 then change LEFT-Doubleclick from [Cursor-mouse-select]
                                              to h3.spf

         SPFPCv4: Same changing Button-1-Double from [CURSOR-MOUSE-SELECT]
                                                to h3.spf

  NOTE6: If using OOREXX you need to delete the SPFPCv/SPFPRO ISREDIT code
         or it will refuse to execute.  This is due to OOREXX scanning the code
         for errors before it executes the code.  A debatable benefit.
  By Mark McDonald  - Last Update 18 Mar 2008
  -------------------------------------------------------------------*/
/*--- Determine IF SPFPRO, SPFPC v4.x  or Stand-alone REXX Interpreter ---*/
      TSDRV = XDETSDRV()
/*--  Determine which REXX interpreter is being used ---*/
      TSPF = 0; TSPFL = " "; parse version name level date1 date2 date3
      IF (name = "REXXSAA" & date3 = "1994") THEN TSPF = 0        /* Using PCDOS REXX*/
      IF (name = "REXX-r4") THEN TSPF = 1                         /* Using R4 */
      IF (name = "REXXPC88" & date3 = "1987") THEN TSPF = 2       /* Using REXXIBM (1987)*/
      IF (name = "REXX/Personal" & date3 = "1992") THEN TSPF = 3  /* Using Personal REXX v4*/
      IF (name = "REXX/2" & date3 = "1995") THEN TSPF = 4         /* Using SPFPC v4 */
      IF (name = "REXX/2" & date3 = "1996") THEN TSPF = 5         /* Using SPFPRO */
      IF (name = "REXX-ooRexx_3.2.0(MT)") THEN TSPF = 6           /* Using OOREXX */
      IF (name = "REXX-Regina_3.7(MT)") THEN TSPF = 7             /* Using Regina */
      TSPFL = TSPF' 'name
/*--- Determine Path of Browser for Interpreter ---*/
      IF (TSPF < 5 ) THEN TBROWSER = " IEXPLORE "              /* USE FOR SPC/PC 4.x or Stand Alone REXX Interpreter*/
      IF (TSPF > 4 ) THEN TBROWSER = " START c:\windows\system32\IEXPLORE.EXE "
/*--- Path of Help File Indexs (BDX) ---*/
      THELP = TSDRV":\mnt\home\U\MHELP\"
      TFILEOUT = TSDRV"c:\TEMP\DRET.HTM"
      TBATCHIN = TSDRV"c:\TEMP\DRET.DAT"
/*--- Use for stand-alone REXX */
      IF (TSPF <> 4 & TSPF <> 5) THEN
         DO
           PARSE UPPER ARG EXNT PARMS
/*         TRACE R
           say EXNT; say parms;parse pull z;    */
           /*--- Remember DOS can not handle a parm with a < or > in it --*/
           IF EXNT = "HTML" | EXNT = "HTM" | EXNT = "HTA" THEN PARMS = '<'PARMS
           IF EXNT = "SPFSE" THEN DO
              column = WORD(PARMS,1)
              FULLNAME = WORD(PARMS,2)
              EXNT = SUBSTR(FULLNAME,POS('.',FULLNAME)+1,3)
              text = TRANSLATE(WORD(PARMS,3),' %<>|@"=',"Çüéâäàåç")
              PARMS = TUNMASK(text, column)
           END
         END
/*--- This section used for SPFPRO/SPFPCv3/4 Only ---*/
      IF (TSPF = 4 | TSPF = 5) THEN
        DO
         'ISREDIT MACRO(PARMS)'
         IF WORDS(PARMS) = 1 & TRANSLATE(PARMS) = "H" THEN
           DO
             ERRLINE = "H3.SPF PARMS: H3 EXT KEYWORD    H3 KEYWORD    H3 PHP SUBSTR    H3 HTM <TABLE"
             'ISREDIT RESET'
             "ISREDIT LINE_BEFORE " 1 "= NOTELINE  '"ERRLINE"'"
             "ISREDIT TOP"
             EXIT
           END
          /*--- If source has not been saved, save it just in case ---*/
          'ISREDIT (MODIFIED) = DATA_CHANGED'
          IF (MODIFIED=YES) THEN
            'ISREDIT SAVE'
          /*--- Retrieve File Name of Member ---*/
          'ISREDIT (FULLNAME) = MEMBER'
          FULLNAME = TRANSLATE(FULLNAME)
          /*--- Get Extenstion of File Name ---*/
          EXNT = SUBSTR(FULLNAME,POS('.',FULLNAME)+1,3)
          /*--- Get Passed Parms ---*/
          PARMS = TRANSLATE(PARMS)
          /*--- If One Word PASSED from cmd line use current Filename Extension Related Help File ---*/
          IF WORDS(PARMS) = 1 THEN SIGNAL TJUMP
          /*--- If more than one line passed use 1st word as help extent, send as keyword ---*/
          IF WORDS(PARMS) > 1 THEN
            DO
              EXNT = WORD(PARMS,1)
              PARMS = WORD(PARMS,2)
              SIGNAL TJUMP
            END
          NWRDS = WORDS(PARMS)
          PASSED = ''
          /*--- Get Cursor Position and Line Data ---*/
          'isredit (line, column) = cursor'
          'isredit (text)         = line .zcsr'
          'isredit (lrecl)        = lrecl'
          'isredit (cur_file)     = member'
          /*--- Is Cursor on Command Line or On a Keyword? ---*/
          IF column = 0 THEN
           NOP
          ELSE DO
            IF NWRDS = 0 THEN DO
              PARMS = TUNMASK(text, column)
              NWRDS = 3
            END
          END
        END
/*--- End SPFPRO/SPFPC4 Only Section ---*/
TJUMP:
/* PARMS; PARSE PULL Z      */
     /*--- Type 1 Plain Text MHELP File? ---*/
      IF POS(EXNT,"ASM C WBT ZAS") > 0 THEN
        DO
         TTYPE = 1
         TPRE = THELP
         IF EXNT = 'ASM' THEN TFILEIN = TPRE'ASM'    /*PC Intel Assembler Reference*/
         IF EXNT = 'C'   THEN TFILEIN = TPRE'C'      /*Generic C*/
         IF EXNT = 'WBT' THEN TFILEIN = TPRE'WB'
         IF EXNT = 'ZBS' THEN TFILEIN = TPRE'ZB'
        END
/*--- Type 2 Web Page? --*/
      IF POS(EXNT,"ADS REX CMD SPF ISP PBW LBS COB CBL PBS DL6 ") > 0 THEN
        DO
          TTYPE = 2
          TPRE = THELP
          IF EXNT = "ADS"  THEN TFILEIN = TPRE'ADS'    /*IDMS ADSO Reference*/
          IF EXNT = 'PBW'  THEN TFILEIN = TPRE'PBWIN'  /*PowerBASIC Windows Compiler*/
          IF EXNT = 'REX'  THEN TFILEIN = TPRE'OOREXX' /*OOREXX Reference*/
          IF EXNT = 'CMD'  THEN TFILEIN = TPRE'REXXC'  /*Classic REXX Reference*/
          IF EXNT = 'SPF'  THEN TFILEIN = TPRE'SPFPRO' /*Uses SPFPROINDX.HTM on manmark*/
          IF EXNT = 'ISP'  THEN TFILEIN = TPRE'SPFPRO'
          IF EXNT = 'LBS'  THEN TFILEIN = TPRE'LB'
          IF EXNT = 'COB'  THEN TFILEIN = TPRE'COBOL'
          IF EXNT = 'CBL'  THEN TFILEIN = TPRE'COBOL'
          IF EXNT = 'PBS'  THEN TFILEIN = TPRE'PBS'
/*        IF EXNT = 'PBC'  THEN TFILEIN = TPRE'PBCC5'  */
          IF EXNT = 'DL6'  THEN TFILEIN = TPRE'PBCC5'
        END
/*--- Type 3 Windows CHM Help File? ---*/
      IF POS(EXNT," HTA HTM HTML PHP CSS JAVA WINREF VBS FBS BAS") > 0 THEN
        DO
          TTYPE = 3
          TPRE = THELP
          IF EXNT = 'PHP'    THEN TFILEIN = TPRE'PHP'      /*PHP  Compiled Windows Reference*/
          IF EXNT = 'HTA'    THEN TFILEIN = TPRE'HTML40'   /*HTML Compiled Windows Reference*/
          IF EXNT = 'HTM'    THEN TFILEIN = TPRE'HTML40'   /*HTML Compiled Windows Reference*/
          IF EXNT = 'HTML'   THEN TFILEIN = TPRE'HTML40'   /*HTML Compiled Windows Reference*/
          IF EXNT = 'CSS'    THEN TFILEIN = TPRE'CSS2'     /*CSS2 Compiled Windows Reference*/
          IF EXNT = 'JAVA'   THEN TFILEIN = TPRE'JAVA'     /*JAVA Compiled Windows Reference*/
          IF EXNT = 'WINREF' THEN TFILEIN = TPRE'WINREF'   /*Windows CMD Reference*/
          IF EXNT = 'VBS'    THEN TFILEIN = TPRE'VBS'      /*VBScript Reference*/
          IF EXNT = 'FBS'    THEN TFILEIN = TPRE'FBASIC'   /*FreeBASIC Reference*/
          IF EXNT = 'BAS'    THEN TFILEIN = TPRE'FBASIC'   /*FreeBASIC Reference*/
        END
/*--- Type 4 SPFPRO HELP File (Change the path to where you have SPFPRO ---*/
      IF (POS(EXNT,"SPF ISP") > 0 & TSPF = 5) THEN
        DO
          ADDRESS ISREDIT TSDRV':\mnt\home\U\SPFPRO\REXX\SPF5HELP.ISP 'PARMS
          EXIT 0
        END
/*--- Type 5 Raw HTML Link File ---*/
      IF EXNT = "PBC" | EXNT = "INC" THEN
        DO
          PARMS = "> "TRANSLATE(PARMS)" "
          IF EXNT = "PBC" THEN
            DO
              TRAWHTML = TSDRV':\mnt\home\U\PBC\BIN\PBC.HTM'
              T = TRANSLATE(LINEIN(TRAWHTML),1)
              DO WHILE LINES(TRAWHTML) > 0
                IF POS(PARMS, T, 1) > 0 THEN
                  DO
                    SP = POS('<A HREF="..',T)
                    TR = SUBSTR(T,SP+12)
                    EP = POS('"',TR,1)
                    TR = SUBSTR(TR,1,EP-1)
                    T  = LINEOUT(TRAWHTML)
                    TCMD = TBROWSER" mk:@MSITStore:"TSDRV":mnt\home\U\PBC\bin\PBCC.chm::/"TR
                    CALL TOSCMD TSPF, TCMD
                    EXIT 0
                  END
                T = TRANSLATE(LINEIN(TRAWHTML))
              END
            END
          EXIT 0
        END
/*--- Main Loop --*/
  DO WHILE LINES(TFILEIN'.BDX') > 0
    T = LINEIN(TFILEIN'.BDX')
    /*--- Type 1 Plain Text File on PC - Puts line in code as note (SPFPRO/SPFPC4 Only) ---*/
    IF (TTYPE = 1 & (TSPF = 4 | TSPF = 5)) THEN
     DO
      IF PARMS = WORD(T,4) THEN
       DO
        DATALINE = SUBSTR(T,24)
        CALL FIXQUOTES
        T = LINEOUT(TFILEIN'.BDX')
        EXIT 0
       END
     END
    /*--- Type 2 Web Page - Calls Browser ---*/
    IF TTYPE = 2 THEN
     DO
      IF PARMS = TRANSLATE(WORD(T,1)) THEN
       DO
        TCMD = TBROWSER' 'WORD(T,2)
        T = LINEOUT(TFILEIN'.BDX')
        CALL RDOS TSPF, TCMD
        EXIT 0
       END
     END
    /*--- Type 3 Compiled HTML Manual (CHM) --*/
    IF TTYPE = 3 THEN
     DO
      IF PARMS = TRANSLATE(WORD(T,1)) THEN DO
        IF POS(EXNT,"BAS FBS",1) > 0 THEN DO
           IF LEFT(WORD(T,2),1) = "*" THEN
              TCMD = TBROWSER" C:\U\CODE\FBASIC\HTML\"WORD(T,1)".HTM"
           ELSE
             TCMD = TBROWSER"  mk:@MSITStore:"THELP''WORD(T,2)
           CALL RDOS TSPF, TCMD
           EXIT 0
        END
        IF POS(EXNT, " HTA HTM HTML",1) > 0 THEN DO
          IF WORD(T,2) = "*CSS" THEN DO
             TV = STRIP(TRANSLATE(WORD(T,1)," ",":"),"B"," ")
             CSS = TSDRV':\mnt\home\U\MHELP\CSS\CSS\en\css\reference\'TV'\index.html'
             TCMD = TBROWSER" "CSS
             CALL RDOS TSPF, TCMD
             EXIT 0
          END
          IF TRANSLATE(LEFT(WORD(T,2),4)) = TSDRV":\U" THEN DO
            IF WORD(T,2) = "*CSS" THEN DO
              TV = TRANSLATE(WORD(T,1)," ",":")
              CSS = TSDRV':\mnt\home\U\MHELP\CSS\CSS\en\css\reference\'TT'\index.html'
            END
            TCMD = TBROWSER" "WORD(T,2)
            CALL RDOS TSPF, TCMD
            EXIT 0
          END
        END
        TCMD = TBROWSER"  mk:@MSITStore:"THELP''WORD(T,2)
        CALL RDOS TSPF, TCMD
        EXIT 0
      END
     END
  END
EXIT 0
/*--- Unmask Keyword ------------------------------------------------------*/
  TUNMASK: PROCEDURE
    PARSE ARG text, column
    /*--- Periods Used with Keywords? ---*/
    IF POS(".",text,1) > 0 THEN DO
       SP = 1
       TLEN = LENGTH(text)
       DO WHILE SP < TLEN
          S = POS(".",text,SP)
          IF S = 0 THEN LEAVE
          IF S > 0 THEN DO
             TL = SUBSTR(text,1,S-1)
             TR = SUBSTR(text,S+1)
             text = TL' .'TR
             SP = S + 3
          END
       END
    END
    /* --- Get first previous text position ---*/
    TableYes = '_\:*?<#$.-' || XRANGE('0','9') || XRANGE('A','Z') || XRANGE('a','z')
    TableNo  = TRANSLATE(XRANGE(D2C(32),D2C(255)),,TableYes,' ')
    wordmask = TRANSLATE(text,,TableNo, ' ')
    wordmask = TRANSLATE(wordmask,,TableYes,'*')
    /* --- Get wordindex for the cursor position when function key pressed ---*/
    n  = 1
    Wi = 0
    Wn = 0
    DO FOREVER;
      i = WORDINDEX(wordmask,n)
      IF (i = 0) THEN LEAVE
      IF (i <= column) THEN DO
        Wi = i
        Wn = n
       END
      ELSE
        LEAVE
      n = n+1
    END
    /*--- Get word text ---*/
    Wm = WORD(wordmask,Wn)
    TL = LENGTH(Wm)
    WFile = SUBSTR(text,Wi,TL)
    PARMS = TRANSLATE(WFile)
    PARMS = STRIP(PARMS)
  RETURN (PARMS)
/*--- Pass Command to OS --------------------------------------------------*/
  TOSCMD: PROCEDURE
    PARSE ARG TSPF, TCMD
    /* say tspf;say tcmd;parse pull z; */
    IF TSPF = 0 THEN ADDRESS COMMAND TCMD     ;/*DOSREXX*/
    IF (TSPF = 4) THEN ADDRESS COMMAND TCMD   ;/*SPFPC4/SPFPRO - Delete if using OOREXX*/
    IF (TSPF = 5 ) THEN ADDRESS SYSTEM TCMD   ;/*SPFPC4/SPFPRO - Delete if using OOREXX*/
    IF (TSPF = 1 | TSPF = 6) THEN TCMD        ;/*OOREXX/R4*/
    IF TSPF = 7 THEN ADDRESS SYSTEM TCMD      ;/*Regina*/
  RETURN
/*--- Change quotes(single/double) to tildes - prevents REXX errors - SPFPRO/SPFPCv3/4 - Delete if using OOREXX ---*/
   FIXQUOTES:
     ERRLINE = TRANSLATE(DATALINE,"~","'")
     ERRLINE = TRANSLATE(ERRLINE,'~','"')
     "ISREDIT LINE_BEFORE " line "= NOTELINE  '"ERRLINE"'"
   RETURN
/*-- Determine Source Path ----------------------------------*/
XDETSDRV: PROCEDURE
 PARSE SOURCE ENV TYPE NAME
  /* SAY ENV
     SAY TYPE
     SAY NAME
     EXIT  */
 TSDRV = LEFT(NAME,1,1)
 RETURN (TSDRV)
/*---------------------------------------------------------------
  RDOS - Builds Batch File To Execute OS Commands
  REQUIRES EXISTENCE OF C:\TEMP  XWORDS  XWORD  XLATE UNXLATE
  Commands Separated by ;
  CMD =    "IF EXIST C:\TEMP\RDOS.TXT DEL C:\TEMP\RDOS.TXT;"
  CMD = CMD"ECHO Gathing data...;"
  CMD = CMD"CALL DIR C:\SPFPRO\MACROS\*.*  /N /ON > C:\TEMP\RDOS.TXT;"
  CMD = CMD"START IEXPLORE  C:\TEMP\RDOS.TXT"
  CALL RDOS CMD
  EXIT
  ---------------------------------------------------------------*/
 RDOS: PROCEDURE
  PARSE ARG TSPF, CMD
  /*--- Build Semi-Unique Result File Name ---*/
  TBAT = "C:\TEMP\RDOS.BAT"
  /*--- Build Batch File ---*/
  T = LINEOUT(TBAT,"@ECHO OFF",1)        /*Turn off Command Echo*/
  TCNT = XWORDS(CMD,";")                 /*Det # OS Commands*/
  DO CNT = 1 TO TCNT                     /*Loop Through Commands*/
    TCMD = XWORD(CMD,CNT,";")            /*Extract OS Command*/
    T = LINEOUT(TBAT,TCMD)               /*Write to Batch File*/
  END
  T = LINEOUT(TBAT,'EXIT')               /*Tell Batch to End*/
  T = LINEOUT(TBAT)                      /*Close C:\TEMP\RDOS.BAT*/
  /*--- Execute Batch Program ---*/
  IF (TSPF = 5 | TSPF = 7) THEN ADDRESS SYSTEM TBAT
  IF (TSPF = 1 | TSPF = 6) THEN TBAT
  IF (TSPF = 0 | TSPF = 4) THEN ADDRESS COMMAND TBAT
/* ADDRESS SYSTEM TBAT  */
 RETURN
/*---------------------------------------------------------------*/
   XWORDS: PROCEDURE
     PARSE ARG X, XDL
     X = XLATE(X,XDL)
     NWRDS = WORDS(X)
   RETURN (NWRDS)
/*---------------------------------------------------------------*/
   XWORD: PROCEDURE
     PARSE ARG X, N ,XDL
     IF N > XWORDS(X,XDL) THEN RETURN ("")
     X = XLATE(X,XDL)
     X = WORD(X,N)
     X = XUNLATE(X)
   RETURN (X)
/*---------------------------------------------------------------*/
   XLATE: PROCEDURE
     PARSE ARG X, XDL
     X = TRANSLATE(X,D2C(253)," ")
     X = TRANSLATE(X," ",XDL)
   RETURN (X)
/*---------------------------------------------------------------*/
   XUNLATE: PROCEDURE
     PARSE ARG X
     X = TRANSLATE(X," ",D2C(253))
   RETURN (X)
/*---------------------------------------------------------------*/
/* Removes Leftmost n characters                                 */
/* T = XTRIML("--ONE",2)   returns "ONE"                         */
/*---------------------------------------------------------------*/
  XTRIML:  PROCEDURE
    PARSE ARG X, N
    IF LENGTH(X) - 1 >= N THEN
      XR = RIGHT(X,LENGTH(X) - N)
    ELSE
      XR = X
  RETURN (XR)
/*---------------------------------------------------------------*/