shell_wrap Function

public function shell_wrap(cmd) result(wrapped)

Wrap a command for execution by the platform shell.

  • Windows: cmd /c "..." with doubled quotes
  • POSIX: sh -c '...' with standard single-quote escaping

Arguments

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

Return Value character(len=:), allocatable


Calls

proc~~shell_wrap~~CallsGraph proc~shell_wrap shell_wrap proc~is_windows is_windows proc~shell_wrap->proc~is_windows get_os_type get_os_type proc~is_windows->get_os_type

Source Code

   function shell_wrap(cmd) result(wrapped)
      character(len=*), intent(in) :: cmd
      character(len=:), allocatable :: wrapped

      if (is_windows()) then
         wrapped = 'cmd /c "' // escape_quotes_win(trim(cmd)) // '"'
      else
         wrapped = "sh -c '" // escape_quotes_sh(trim(cmd)) // "'"
      end if

   contains

      function escape_quotes_win(s) result(r)
         character(len=*), intent(in) :: s
         character(len=:), allocatable :: r
         integer :: i
         r = ""
         do i = 1, len_trim(s)
            if (s(i:i) == '"') then
               r = r // '""'
            else
               r = r // s(i:i)
            end if
         end do
      end function escape_quotes_win

      function escape_quotes_sh(s) result(r)
         character(len=*), intent(in) :: s
         character(len=:), allocatable :: r
         integer :: i
         r = ""
         do i = 1, len_trim(s)
            if (s(i:i) == "'") then
               r = r // "'""'""'"
            else
               r = r // s(i:i)
            end if
         end do
      end function escape_quotes_sh

   end function shell_wrap