subroutine trmpi_syntax(str_test,legal_chars,caller,descr)

  use trmpi
  implicit NONE

  !  syntax check (internal) -- NOTE, entire length of str_test is
  !  examined -- caller must trim if trailing blanks are to be ignored!

  character*(*), intent(in) :: str_test     ! string to examine
  character*(*), intent(in) :: legal_chars  ! allowed characters in str_test

  !  these for error messages:
  character*(*), intent(in) :: caller       ! name of calling routine
  character*(*), intent(in) :: descr        ! short description of str_test

  !------------------------
  !  local:

  integer :: ic,ilen,indx

  !------------------------

  ilen = len(str_test)

  do ic=1,ilen
     indx = index(legal_chars,str_test(ic:ic))
     if(indx.eq.0) then
        write(0,*) ' ?'//trim(caller)//': illegal character "', &
             str_test(ic:ic),'" at char position ',ic
        write(0,*) '  in '//trim(descr)//' on cpu# ',trmpi_myid
        write(0,*) '  received value: "'//str_test(1:ilen)//'".'
        write(0,*) '  allowed characters: "',legal_chars,'".'
        call bad_exit
     endif
  enddo

end subroutine trmpi_syntax
