      subroutine openfile()
!
      implicit none
!
      logical exi
      character*132 jobname,fnin,fndat,fnfrd,fnsta
      integer i

      write(*,*) 'please enter input file name'
      read(*,*) fnin
!
      inquire(file=fnin,exist=exi)
      if(exi) then
         open(1,file=fnin,status='old')
      else
         write(*,*) '*ERROR in openfile: input file',fnin
         write(*,*) 'does not exist'
         stop
      endif
!
      return
      end


      subroutine getnewline(istat,in,n,key)
!
      implicit none
!
!     parser for abainput
!
!     n = # comma's +1,
!
      integer istat,in,n,key,i,j,k
!
      character*40 textpart(16)
      character*132 text
!
!     reading a new line
!
      do
         text=''
         read(in,'(a40)',iostat=istat) text
         if(istat.lt.0) then
            if(text.eq.'') then
               if(in.ne.1) then
                  close(2)
                  in=1
                  cycle
               else
                  return
               endif
            else
               istat=0
            endif
         endif
         if((text(1:8).eq.'*include').or.
     &          (text(1:8).eq.'*INCLUDE')) then
!           call include(text,in)
            write(*,*)'should not have include'
         elseif(text(1:2).eq.'**') then
         else
            exit
         endif
      enddo
!
      key=0
!
!     only free format is supported
!
      if((text(1:1).eq.'*').and.(text(2:2).ne.'*')) then
         key=1
      endif
!
!     initialize all textpart fields...
!
      do i=1,16
         textpart(i)='                                        '
      enddo

      
      n=1
      j=0
      do i=1,40
         if(text(i:i).ne.',') then
            if(j.eq.0) then
               if(text(i:i).eq.' ') cycle
            endif
            j=j+1
            if(j.le.40) textpart(n)(j:j)=text(i:i)
         else
            do k=j+1,40
               textpart(n)(k:k)=' '
            enddo
            n=n+1
            j=0
         endif
      enddo
      if(j.eq.0) n=n-1
      write(*,*) '----------------------------------------'
      write(*,*)'n = ',n
      write(*,'('' n = '',I5,''  text = '',a40)') n,text
      do i = 1, n
        write(*,'('' textpart('',I2,'') = '',A40)') i,textpart(i)
      enddo

!
!     clearing all textpart fields not used
!
      do i=n+1,16
         textpart(i)='                                        '
      enddo
!
      return
      end


      PROGRAM TEST_READ
      IMPLICIT NONE

      integer istat, in, n, key
      integer i
      
      in = 1

      call openfile()

      do i = 1, 10
        call getnewline(istat, in, n, key)
      enddo

      END PROGRAM TEST_READ
