@@ -5,7 +5,7 @@ module fpm_filesystem
5
5
use fpm_environment, only: get_os_type, &
6
6
OS_UNKNOWN, OS_LINUX, OS_MACOS, OS_WINDOWS, &
7
7
OS_CYGWIN, OS_SOLARIS, OS_FREEBSD, OS_OPENBSD
8
- use fpm_environment, only: separator, get_env
8
+ use fpm_environment, only: separator, get_env, os_is_unix
9
9
use fpm_strings, only: f_string, replace, string_t, split, notabs
10
10
use iso_c_binding, only: c_char, c_ptr, c_int, c_null_char, c_associated, c_f_pointer
11
11
use fpm_error, only : fpm_stop
@@ -15,7 +15,7 @@ module fpm_filesystem
15
15
mkdir, exists, get_temp_filename, windows_path, unix_path, getline, delete_file
16
16
public :: fileopen, fileclose, filewrite, warnwrite, parent_dir
17
17
public :: read_lines, read_lines_expanded
18
- public :: which, LINE_BUFFER_LEN
18
+ public :: which, run, LINE_BUFFER_LEN
19
19
20
20
integer , parameter :: LINE_BUFFER_LEN = 1000
21
21
@@ -850,4 +850,73 @@ function which(command) result(pathname)
850
850
enddo SEARCH
851
851
end function which
852
852
853
+ ! > echo command string and pass it to the system for execution
854
+ subroutine run (cmd ,echo ,exitstat ,verbose ,redirect )
855
+ character (len=* ), intent (in ) :: cmd
856
+ logical ,intent (in ),optional :: echo
857
+ integer , intent (out ),optional :: exitstat
858
+ logical , intent (in ), optional :: verbose
859
+ character (* ), intent (in ), optional :: redirect
860
+
861
+ logical :: echo_local, verbose_local
862
+ character (:), allocatable :: redirect_str
863
+ character (:), allocatable :: line
864
+ integer :: stat, fh, ios
865
+
866
+
867
+ if (present (echo))then
868
+ echo_local= echo
869
+ else
870
+ echo_local= .true.
871
+ end if
872
+
873
+ if (present (verbose))then
874
+ verbose_local= verbose
875
+ else
876
+ verbose_local= .true.
877
+ end if
878
+
879
+ if (present (redirect)) then
880
+ redirect_str = " >" // redirect// " 2>&1"
881
+ else
882
+ if (verbose_local)then
883
+ ! No redirection but verbose output
884
+ redirect_str = " "
885
+ else
886
+ ! No redirection and non-verbose output
887
+ if (os_is_unix()) then
888
+ redirect_str = " >/dev/null 2>&1"
889
+ else
890
+ redirect_str = " >NUL 2>&1"
891
+ end if
892
+ end if
893
+ end if
894
+
895
+ if (echo_local) print * , ' + ' , cmd
896
+
897
+ call execute_command_line(cmd// redirect_str, exitstat= stat)
898
+
899
+ if (verbose_local.and. present (redirect)) then
900
+
901
+ open (newunit= fh,file= redirect,status= ' old' )
902
+ do
903
+ call getline(fh, line, ios)
904
+ if (ios /= 0 ) exit
905
+ write (* ,' (A)' ) trim (line)
906
+ end do
907
+ close (fh)
908
+
909
+ end if
910
+
911
+ if (present (exitstat)) then
912
+ exitstat = stat
913
+ else
914
+ if (stat /= 0 ) then
915
+ call fpm_stop(1 ,' *run*:Command failed' )
916
+ end if
917
+ end if
918
+
919
+ end subroutine run
920
+
921
+
853
922
end module fpm_filesystem
0 commit comments