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 if file "config.nml"
 ! setup for xclock 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(30) :: background='black'
CHARACTER(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)

! after reading config file
do i10=1,number
   call mysystem('xterm -bg '//background//' -fg '//foreground//'&')
enddo

end program config
   subroutine mysystem(string)
   character(len=*) :: string
   write(*,*)'COMMAND:'//string
   call system(string)
return
end subroutine mysystem

program output

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