C----------------------------------------------------------------------
C  UWORDL -- FIND LIMITS OF NEXT SYNTACTIC "WORD" IN STRING
C
C  A WORD IS A BLANK OR COMMA DELIMMETED STRING, OR ANY STRING IMBEDDED
C  IN DBL QUOTES.  DBL QUOTE STRINGS WORK LIKE THIS:
C
C  "" TRANSLATES TO THE NULL WORD
C  "hello" TRANSLATES TO hello (LOWER CASE)
C  """" TRANSLATES TO A SINGLE DOUBLE QUOTE
C  """this is a test"" END OF TEST" TRANSLATES TO
C         "this is a test" END OF TEST
C  AS ONE WORD
C
C  THIS ROUTINE SIMPLY RETURNS THE INDEX LIMITS OF THE NEXT WORD STRING
C  IN THE PASSED STRING
C
      SUBROUTINE UWORDL(STR,ISTART,IL1,IL2,ICODE)
      USE USERIO
C
C  INPUT STRING:
      CHARACTER*(*) STR
C
C  ISTART-- INCREMENT TO WHERE TO START SEARCH FOR A WORD
C    NB if ISTART=-1, treat as if ISTART=0 but add uppercase conversion
C  OUTPUT
C  IL1,IL2  LIMITS OF WORD FOUND
C  ICODE    FLAG INDICATING TYPE OF END-DELIMITER
C   =0 MEANS END OF LINE
C   =1 MEANS A BLANK OR TAB
C   =2 MEANS A COMMA
C   =4 MEANS USER PUNCTUATION
C
      ISI=max(0,ISTART)

      iup=0
      if(istart.lt.0) iup=1

      ILEN=LEN(STR)
C
      ICODE=0
C
      IL1=0
      IL2=0
C
C  1.  FIND START OF WORD
 10   CONTINUE
      ISI=ISI+1
      IF(ISI.GT.ILEN) GO TO 500  ! **WITHOUT FINDING WORD**
C
      CALL UDELIM(STR(ISI:ISI),JCODE)
      IF(JCODE.EQ.1) GO TO 10
C
      IF(JCODE.EQ.2) THEN  ! OLD STYLE UREAD COMMA DELIMITER
        IL1=ISI
        ICODE=2
        GO TO 500  ! **NULL WORD**
      ENDIF
C
      IL1=ISI
C
      IF(STR(ISI:ISI).EQ.'"') GO TO 100  ! QUOTE STRING DETECTED
C
      IF(JCODE.EQ.4) THEN    ! USER PUNCTUATION SYMBOL DETECTED
        ICODE=4
        IL2=IL1
        GO TO 500
      ENDIF
C
      if(iup.eq.1) call uspcap(str(isi:isi))
C
C  2.  FIND END OF STRING (NOT A QUOTE STRING)
 20   CONTINUE
      ISI=ISI+1
      IF(ISI.GT.ILEN) THEN
        IL2=ILEN
        GO TO 500
      ENDIF
C
      IF(STR(ISI:ISI).EQ.'"') GO TO 100  ! QUOTE STRING DETECTED
C
      CALL UDELIM(STR(ISI:ISI),ICODE)
      IF(ICODE.EQ.0) then
         if(iup.eq.1) call uspcap(str(isi:isi))
         GO TO 20
      endif
C
      IL2=ISI-1
      GO TO 500
C
C  3.  FIND END OF STRING (QUOTE STRING)
 100  CONTINUE
C
C  END OF QUOTE STRING IS WHEN AN EVEN NO.'D DBL QUOTE CHARACTER IS
C  FOUND NOT IMMEDIATELY FOLLOWED BY ANOTHER DBL QUOTE CHARACTER
C
      IEVEN=0
 110  CONTINUE
      ISI=ISI+1
      IF(ISI.GT.ILEN) THEN
        ILX=MIN0(72,ILNURD(STR))
        WRITE(LUNTRM,9001) STR(1:ILX)
 9001   FORMAT(
     >' %UREAD (SR UWORDL):  TRYING TO INSERT MISSING "-DELIMITER:'/
     >1X,A)
        CALL UPAUST(0)
        IF((IL1.GT.(ILEN-2)).OR.(STR(ILEN:ILEN).NE.' ')) THEN
          IL2=ILEN
          GO TO 500
        ELSE
          DO 220 IC=ILEN,IL1+2,-1
            IF(STR(IC:IC).NE.' ') GO TO 222
 220      CONTINUE
          IC=IL1+1
 222      CONTINUE
          IC=IC+1
          STR(IC:IC)='"'
          ISI=IC
        ENDIF
      ENDIF
      IF(STR(ISI:ISI).NE.'"') GO TO 110
      IEVEN=1-IEVEN
      IF(IEVEN.EQ.1) THEN
        IF(ISI.EQ.ILEN) THEN
          IL2=ILEN
          GO TO 500
        ELSE
          ISP1=ISI+1
C  EVEN QUOTE ENDS STRING UNLESS ANOTHER QUOTE FOLLOWS...
          IF(STR(ISP1:ISP1).NE.'"') THEN
            GO TO 20
          ENDIF
        ENDIF
      ENDIF
      GO TO 110
C
C-------------------
C  EXIT
C
 500  CONTINUE
      RETURN
      END
