command_program Function

public function command_program(cmd) result(prog)

Return the program invoked by a command line (its first shell token), OS-aware. Uses fpm’s shell lexer so quoted paths stay intact and trailing arguments (e.g. a wrapper’s “mpifort -fc=gfortran”) are dropped. Returns ‘’ if empty.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: cmd

Return Value character(len=:), allocatable


Source Code

function command_program(cmd) result(prog)
    character(*), intent(in) :: cmd
    character(:), allocatable :: prog
    character(len=:), allocatable :: tokens(:)

    prog = ''
    if (os_is_unix()) then
        tokens = sh_split(cmd)               ! POSIX lexer
    else
        tokens = ms_split(cmd, ucrt=.true.)  ! Windows lexer
    end if
    ! The program is always reliably the first token. 
    if (allocated(tokens)) then
        if (size(tokens) > 0) prog = trim(adjustl(tokens(1)))
    end if

end function command_program