c                        
c   Copyright (c) 1990  by  Dwight Meglan                 
c
      subroutine ParseLine( line,CMND )
      implicit none

      include 'tel_cons.inc'
      include 'tel_strc.inc'

      character line*80
      record /CmndLine/ CMND

      integer*4 i,j,k,nc,LastChar,ns,ob,oe,slash(6),MaxCmnd,MaxOpt,MaxLine
      character MakeUpperCase*80,ACmnd*3
      logical*4 temp,MakeReal

      MaxCmnd= 50
      MaxOpt=  5
      MaxLine= 5
c
c     First check whether this is a continuation line, a line to be continued,
c     or just a new line.  Also, find the first and last characters in the
c     line.
c
      if ( .not. CMND.Continue ) then ! zero out the CMND structure
        CMND.NumCmnd= 0
        CMND.NumLine= 0
        do i= 1, MaxCmnd
          CMND.CmndText(i)= '   '
          CMND.CmndNumOK(i)= .false.
          CMND.CmndIntVal(i)= 0
          CMND.CmndRealVal(i)= 0.0
          CMND.NumOpt(i)=  0
          CMND.CmndEnd(i)= 0
          CMND.CmndStart(i)= 0
          do j= 1, MaxOpt
            CMND.OptText(j,i)= '   '
            CMND.OptNumOK(j,i)= .false.
            CMND.OptIntVal(j,i)= 0
            CMND.OptRealVal(j,i)= 0.0
            CMND.OptStart(j,i)= 0
            CMND.OptEnd(j,i)= 0
          end do
        end do
        do i= 1, MaxLine
          do j= 1, 80
            CMND.Text(i)(j:j)= ' '
          end do
        end do
      end if 
      if ( CMND.NumLine .ge. MaxLine ) return ! no more than MaxLine lines 
      CMND.NumLine= CMND.NumLine + 1          ! of text allowed
      line= MakeUpperCase( line )             
      CMND.Text(CMND.NumLine)= line           
      i= 80   
      do while( (i .gt. 0) .and. (line(i:i) .eq. ' ') )
        i= i - 1
      end do
      if ( i .le. 0 ) i= 1
      if ( line(i:i) .eq. '_' ) then !
        CMND.Continue= .true.
        LastChar= i - 1                 
      else
        CMND.Continue= .false.
        LastChar= i
      end if
c
c     Now process each command checking each for options.  The commands
c     have the form: CMND1/OPT1/OPT2=3/OPT3/OPT4/OPT5 CMND2/OPT CMND3/...
c     where the options can be text, numbers or text=numbers depending upon
c     the situation.  Store the results for each in the CMND structure.
c     AT present the maximum number of commands is 50 with 5 options per
c     command allowable.
c
c     First isolate the command text
c
      i= 1
      do while ( (i .lt. LastChar) .and. (CMND.NumCmnd .lt. MaxCmnd) )
        do while ( (i .lt. LastChar) .and. (line(i:i) .eq. ' ') )
          i= i + 1
        end do     
        CMND.NumCmnd= CMND.NumCmnd + 1
        nc= CMND.NumCmnd
        CMND.CmndStart(nc)= i
        do while ( (i .le. LastChar) .and. (line(i:i) .ne. ' ') )
          i= i + 1
        end do
        CMND.CmndEnd(nc)= i - 1
        CMND.CmndText(nc)= '   '
        if ( (CMND.CmndEnd(nc)-CMND.CmndStart(nc)) .lt. 2 ) then
          CMND.CmndText(nc)= line(CMND.CmndStart(nc):CMND.CmndEnd(nc))
        else
          ACmnd= line(CMND.CmndStart(nc):CMND.CmndStart(nc)+2)
          if ( index(Acmnd,'/') .eq. 0 ) then
            CMND.CmndText(nc)= ACmnd
          else
            CMND.CmndText(nc)= ACmnd(1:index(ACmnd,'/')-1)
          end if
        end if
c
c       Now isolate the option text
c             
        j= CMND.CmndStart(nc)
        ns= 0 ! number of /'s
        do while ( (j .le. CMND.CmndEnd(nc)) .and. (ns .lt. MaxOpt+1) )
          if ( (line(j:j) .eq. '/') .and. (ns .lt. MaxOpt+1) ) then
            ns= ns + 1
            slash(ns)=j
          else if( (j .eq. CMND.CmndEnd(nc)) .and. (ns .lt. MaxOpt+1) ) then
            ns= ns + 1
            slash(ns)= j+1
          end if
          j= j + 1
        end do ! find option delimiters- /'s
        if ( ns .gt. 1 ) then ! there are options present
          j= 1
          do while (j .lt. ns-1) ! check for zero length options
            if ( (slash(j+1)-slash(j)) .le. 1 ) then ! delete option
              do k= j, ns-1
                slash(k)= slash(k+1)
              end do
              ns= ns - 1
            end if
            j= j + 1
          end do
          CMND.NumOpt(nc)= ns-1
          if ( ns .lt. 1 ) CMND.NumOpt(nc)= 0
c
c         Check for = sign and if present isolate number following it
c
          do j= 1, ns-1
            ob= slash(j) + 1   ! option beginning position
            oe= slash(j+1) - 1 ! option ending position
            CMND.OptText(j,nc)= '   '
            CMND.OptStart(j,nc)= ob
            CMND.OptEnd(j,nc)=   oe
            k= ob + index(line(ob:oe),'=') - 1
            if ( k .gt. ob ) then         ! = present
              if ( (k - ob) .gt. 3 ) then
                CMND.OptText(j,nc)= line(ob:ob+2)
              else
                CMND.OptText(j,nc)= line(ob:k-1)
              end if
              CMND.OptNumOK(j,nc)= MakeReal(line,k+1,oe,CMND.OptRealVal(j,nc))
              if ( CMND.OptRealVal(j,nc) .gt. 2.0e9 ) then
                CMND.OptIntVal(j,nc)=  2e9
              else if ( CMND.OptRealVal(j,nc) .lt. -2.0e9 ) then
                CMND.OptIntVal(j,nc)= -2e9
              else
                CMND.OptIntVal(j,nc)= nint(CMND.OptRealVal(j,nc))
              end if
c              CMND.OptEnd(j,nc)= k - 1 ! this line causes optend to ignore
            else                        ! any test after the =
              if ( (oe - ob) .gt. 2 ) then
                CMND.OptText(j,nc)= line(ob:ob+2)
              else
                CMND.OptText(j,nc)= line(ob:oe)
              end if
              CMND.OptNumOK(j,nc)= MakeReal(line,ob,oe,CMND.OptRealVal(j,nc))
              if ( CMND.OptRealVal(j,nc) .gt. 2.0e9 ) then
                CMND.OptIntVal(j,nc)=  2e9
              else if ( CMND.OptRealVal(j,nc) .lt. -2.0e9 ) then
                CMND.OptIntVal(j,nc)= -2e9
              else
                CMND.OptIntVal(j,nc)= nint(CMND.OptRealVal(j,nc))
              end if
            end if
          end do ! interpret option
        else ! no options
          CMND.NumOpt(nc)= 0
        end if ! options are present
        if ( CMND.NumOpt(nc) .gt. 0 ) CMND.CmndEnd(nc)= CMND.OptStart(1,nc)-2
        CMND.CmndNumOK(nc)= MakeReal(line,CMND.CmndStart(nc),CMND.CmndEnd(nc),
     &                               CMND.CmndRealVal(nc))     
        if ( CMND.CmndRealVal(nc) .gt. 2.0e9 ) then
          CMND.CmndIntVal(nc)=  2e9
        else if ( CMND.CmndRealVal(nc) .lt. -2.0e9 ) then
          CMND.CmndIntVal(nc)= -2e9
        else
          CMND.CmndIntVal(nc)= nint(CMND.CmndRealVal(nc))
        end if
      end do ! next command
      if ( (CMND.NumCmnd .eq. 1) .and. ( CMND.CmndText(1)(1:1) .eq. ' ') )
     &   CMND.NumCmnd= 0
      CMND.Continue= .false.  ! added for anz only to prevent continued lines
      return
      end                              
c                     
c   Copyright (c) 1990  by  Dwight Meglan             
c                                                   
      function MakeUpperCase( string )
                
      character*(*) string,MakeUpperCase
      integer*4   i                          

      do i= 1, len(string)
        if ( (ichar(string(i:i)) .ge. ichar('a')) .and. 
     &       (ichar(string(i:i)) .le. ichar('z'))       )
     &     string(i:i)= char( ichar(string(i:i)) - ichar('a') + ichar('A') )
        MakeUpperCase(i:i)= string(i:i)
      end do                        
      return
      end
c
c   Copyright (c) 1990  by  Dwight Meglan                                   
c                 
      logical*4 function MakeReal(string,start,end,number)
      implicit none

      character string*80
      integer*4 start,end
      real*4    number,sign

      integer*4 first_char,last_char,numeral,decimal,i,power
                  
      MakeReal= .false. ! error
      sign= 1.0
      first_char= start
      last_char=  end
      if ( string(first_char:first_char) .eq. '-' ) then
        sign= -1.0       
        first_char= first_char + 1
      end if
      if ( first_char .gt. last_char ) goto 999 ! trap case of a - only
      power= last_char - first_char
      decimal= index(string(first_char:last_char),'.')
      if ( decimal .ne. 0 ) power= decimal - 2
      number= 0.0
      do i= first_char, last_char
        if ( ((string(i:i) .lt. '0') .or. (string(i:i) .gt. '9')) .and.
     &       (string(i:i) .ne. '.')                                   ) goto 999
        if ( string(i:i) .ne. '.' ) then
          numeral= ichar(string(i:i)) - ichar('0')
          number= number + float(numeral)*10**float(power)
          power= power - 1              
        end if
      end do
      number= sign*number
      MakeReal= .true. ! no error   
      return
999   number= -999.0
      return
      end
