Using Fortran NAMELIST I/O for configuration files

Since a read of a Fortran NAMELIST scans a file for a match you can use this feature very easily to maintain a single configuration file for several programs. For example, if you have an input file that looks like:

   This is file "config.nml" which contains setup information for 
   programs that launch xclock(1) and the xterm(1) program. 
   &xclock
    background='black'
    foreground='yellow'
    geometry='64x64-0+0'
    /
    ! setup for xterm program
   &xterm
    background='green'
    foreground='black'
    number=3
    /

This contrived sample shows a single file ("config.nml") holding multiple NAMELIST input sets ("xterm" and "xclock") and a small program that reads the first "xterm" set (ignoring other content in the file) and builds and executes system commands using the information.

program to read and use "NML=xterm" data

   program config
   
   character(len=30) :: background='black'
   character(len=30) :: foreground='white'
   integer :: number=1
   namelist /xterm/ background, foreground, number
   
      ! before reading config file
      call mysystem('xterm -bg '//background//' -fg '//foreground//'&')
      
      open(10,file='config.nml',delim='apostrophe')
      read(10,nml=xterm,iostat=ios)
      ! notice how the READ skipped down to the xterm NAMELIST group!!
      
      ! after reading config file
      do i10=1,number
         call mysystem('xterm -bg '//background//' -fg '//foreground//'&')
      enddo
   
   contains
      subroutine mysystem(string)
      character(len=*) :: string
         write(*,*)'COMMAND:'//string
         call execute_command(string)
      end subroutine mysystem
   
   end program config

program output

   COMMAND:xterm -bg black          -fg white      &
   COMMAND:xterm -bg green          -fg black      &