split_append Subroutine

public subroutine split_append(argv, str, join_spaced, ok)

Split a command fragment into argv tokens with the OS-native lexer and append them to argv. fortran-shlex is not reentrant, so the split runs in a critical region. Both lexers remove the quoting as a shell does, so -DMSG=‘“hi”’ gives -DMSG=”hi”. join_spaced applies to the POSIX lexer.

Arguments

Type IntentOptional Attributes Name
type(string_t), intent(inout), allocatable :: argv(:)
character(len=*), intent(in) :: str
logical, intent(in) :: join_spaced
logical, intent(inout) :: ok

Variables

Type Visibility Attributes Name Initial
integer, public :: k
character(len=:), public, allocatable :: parts(:)

Source Code

subroutine split_append(argv, str, join_spaced, ok)
    type(string_t), allocatable, intent(inout) :: argv(:)
    character(len=*), intent(in) :: str
    logical, intent(in) :: join_spaced
    logical, intent(inout) :: ok

    character(len=:), allocatable :: parts(:)
    integer :: k

    if (.not.ok) return

    ! fortran-shlex is not reentrant, and the deferred-length parts(:) result it
    ! returns must be consumed before another thread re-enters the lexer. Hold the
    ! lock across both the split and the readback into argv.
    !$omp critical(fpm_shlex_split)
    if (os_is_unix()) then
        parts = sh_split(str, join_spaced=join_spaced, keep_quotes=.false., success=ok)
    else
        parts = ms_split(str, success=ok)
    end if
    if (ok) then
        do k = 1, size(parts)
            if (len_trim(parts(k)) == 0) cycle
            call add_strings(argv, string_t(trim(parts(k))))
        end do
    end if
    !$omp end critical(fpm_shlex_split)
end subroutine split_append