C LAST UPDATE: 
C+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C
C**************************
C**    KWD_OPFUN         **
C**************************
C
C
      SUBROUTINE KWD_OPFUN
C
      INCLUDE 'implicit_none.finc'
C
C Purpose: Output string for KWD_WRITEKDM
C
C Author:  John W. Campbell, July 1995
C
C **** KWD SERVICE ROUTINE ONLY ****
C
C
C Arguments:
C
C      None
C
C====== Parameter statements
C
      INTEGER MAXC
      PARAMETER (MAXC = 120) 
C
C====== GLOBALS:
C
      INCLUDE 'kwd_outstring.finc'
C
C====== FUNCTION TYPES:
C
      INTEGER LENSTR
      INTEGER NXTTOK
C
C====== LOCALS:
C
      LOGICAL TERM
      LOGICAL FIRST
      INTEGER IUN
      INTEGER IL
      INTEGER M1
      INTEGER M2
      INTEGER NCH
      INTEGER NXT
      INTEGER I
C
C-------------------------------------------------------------------------------
C
      IF (KUNIT.GT.0) THEN
         IUN = KUNIT
         TERM = .FALSE.
      ELSE
         IUN = -KUNIT
         TERM = .TRUE.
      END IF

10    IF (LENSTR(OUTSTR).LE.MAXC) THEN
         IF (TERM) THEN
            CALL TTSEND (IUN, OUTSTR(1:LENSTR(OUTSTR)), 1)
         ELSE
            WRITE(IUN,1000) OUTSTR(1:LENSTR(OUTSTR))
         END IF
         RETURN
      ELSE
         IL = 0
         NCH = 0
         NXT = 0
         FIRST = .TRUE.
20       I = NXTTOK (OUTSTR, ' ', IL, M1, M2)
         IF (I.GT.0) THEN
            IF (FIRST.OR.M2.LT.MAXC) THEN
               NCH = M2
               FIRST = .FALSE.
               GO TO 20
            ELSE
               NXT = M1
            END IF
         END IF             
         OUTSTR(NCH+1:NCH+1) = '&'
         IF (TERM) THEN
            CALL TTSEND (IUN, OUTSTR(1:NCH+1), 1)
         ELSE
            WRITE(IUN,1000) OUTSTR(1:NCH+1)
         END IF
         IF (NXT.GT.0) THEN
            OUTSTR = OUTSTR(NXT:)
            GO TO 10
         END IF
      END IF

      RETURN
C
C====== Format statements
C
1000  FORMAT (A)
      END
