| 1 |
!----------------------------------------------------------------- |
| 2 |
! |
| 3 |
! This file is (or was) part of SPLASH, a visualisation tool |
| 4 |
! for Smoothed Particle Hydrodynamics written by Daniel Price: |
| 5 |
! |
| 6 |
! http://users.monash.edu.au/~dprice/splash |
| 7 |
! |
| 8 |
! SPLASH comes with ABSOLUTELY NO WARRANTY. |
| 9 |
! This is free software; and you are welcome to redistribute |
| 10 |
! it under the terms of the GNU General Public License |
| 11 |
! (see LICENSE file for details) and the provision that |
| 12 |
! this notice remains intact. If you modify this file, please |
| 13 |
! note section 2a) of the GPLv2 states that: |
| 14 |
! |
| 15 |
! a) You must cause the modified files to carry prominent notices |
| 16 |
! stating that you changed the files and the date of any change. |
| 17 |
! |
| 18 |
! Copyright (C) 2005-2021 Daniel Price. All rights reserved. |
| 19 |
! Contact: [email protected] |
| 20 |
! |
| 21 |
!----------------------------------------------------------------- |
| 22 |
|
| 23 |
! |
| 24 |
! this module contains certain useful utilities which |
| 25 |
! depend on system commands (in the system_commands module) |
| 26 |
! |
| 27 |
module system_utils |
| 28 |
use asciiutils, only:lcase,cstring |
| 29 |
implicit none |
| 30 |
public :: ienvironment,lenvironment,renvironment,lenvstring,ienvstring |
| 31 |
public :: envlist,ienvlist,lenvlist,renvlist,get_command_option,count_matching_args |
| 32 |
public :: get_command_flag,get_user,get_copyright,get_environment_or_flag |
| 33 |
public :: set_environment_variable |
| 34 |
|
| 35 |
private |
| 36 |
|
| 37 |
contains |
| 38 |
|
| 39 |
! |
| 40 |
!--this routine returns a variable |
| 41 |
! from EITHER a command line flag --foo=bar |
| 42 |
! OR from an environment variable MY_FOO=bar |
| 43 |
! |
| 44 |
! The flag takes priority over the environment variable |
| 45 |
! |
| 46 |
subroutine get_environment_or_flag(variable,string) |
| 47 |
character(len=*), intent(in) :: variable |
| 48 |
character(len=*), intent(out) :: string |
| 49 |
integer :: ierr,iloc |
| 50 |
|
| 51 |
! try as command flag, excluding everything before the last |
| 52 |
! underscore, e.g. MY_GOOD_FOO becomes --foo |
| 53 |
iloc = index(variable,'_',back=.true. ) |
| 54 |
call get_option(variable(iloc+1:),string,ierr) |
| 55 |
|
| 56 |
! then try as environment variable, including underscores |
| 57 |
! e.g. MY_GOOD_FOO=bar |
| 58 |
if (ierr /= 0) call get_environment_variable(variable,string) |
| 59 |
!print*,' GOT ',trim(variable),' = ',trim(string) |
| 60 |
|
| 61 |
end subroutine get_environment_or_flag |
| 62 |
|
| 63 |
! |
| 64 |
!--this routine sets an environment variable by calling |
| 65 |
! the intrinsic C routine setenv |
| 66 |
! |
| 67 |
subroutine set_environment_variable(variable,string,stat) |
| 68 |
use system_commands, only:setenv |
| 69 |
character(len=*), intent(in) :: variable,string |
| 70 |
integer, optional, intent(out) :: stat |
| 71 |
integer :: ierr |
| 72 |
|
| 73 |
ierr = setenv(cstring(variable),cstring(string),1) |
| 74 |
if (present(stat)) stat = ierr |
| 75 |
|
| 76 |
end subroutine set_environment_variable |
| 77 |
|
| 78 |
! |
| 79 |
!--this routine returns an integer variable |
| 80 |
! from an environment variable setting |
| 81 |
! |
| 82 |
! if the errval argument is present then this is the |
| 83 |
! value assigned when an error has occurred |
| 84 |
! (default is zero) |
| 85 |
! |
| 86 |
integer function ienvironment(variable,errval) |
| 87 |
character(len=*), intent(in) :: variable |
| 88 |
character(len=30) :: string |
| 89 |
integer, intent(in), optional :: errval |
| 90 |
|
| 91 |
call get_environment_or_flag(variable,string) |
| 92 |
if (present(errval)) then |
| 93 |
ienvironment = ienvstring(string,errval) |
| 94 |
else |
| 95 |
ienvironment = ienvstring(string) |
| 96 |
endif |
| 97 |
|
| 98 |
end function ienvironment |
| 99 |
|
| 100 |
! |
| 101 |
!--this routine returns a floating point (real) variable |
| 102 |
! from an environment variable setting |
| 103 |
! |
| 104 |
! if the errval argument is present then this is the |
| 105 |
! value assigned when an error has occurred |
| 106 |
! (default is zero) |
| 107 |
! |
| 108 |
real function renvironment(variable,errval) |
| 109 |
character(len=*), intent(in) :: variable |
| 110 |
character(len=30) :: string |
| 111 |
real, intent(in), optional :: errval |
| 112 |
integer :: ierr |
| 113 |
|
| 114 |
call get_environment_or_flag(variable,string) |
| 115 |
if (len_trim(string) > 0) then |
| 116 |
read(string,*,iostat=ierr) renvironment |
| 117 |
else |
| 118 |
ierr = 1 |
| 119 |
endif |
| 120 |
|
| 121 |
if (ierr /= 0) then |
| 122 |
if (present(errval)) then |
| 123 |
renvironment = errval |
| 124 |
else |
| 125 |
renvironment = 0. |
| 126 |
endif |
| 127 |
endif |
| 128 |
|
| 129 |
end function renvironment |
| 130 |
|
| 131 |
! |
| 132 |
!--this routine returns a logical variable |
| 133 |
! from an environment variable setting |
| 134 |
! |
| 135 |
logical function lenvironment(variable) |
| 136 |
character(len=*), intent(in) :: variable |
| 137 |
character(len=30) :: string |
| 138 |
|
| 139 |
call get_environment_or_flag(variable,string) |
| 140 |
lenvironment = lenvstring(string) |
| 141 |
|
| 142 |
end function lenvironment |
| 143 |
|
| 144 |
! |
| 145 |
!--utility routine to determine whether a string |
| 146 |
! should be interpreted as true or false |
| 147 |
! |
| 148 |
logical function lenvstring(string) |
| 149 |
character(len=*), intent(in) :: string |
| 150 |
|
| 151 |
if (string(1:1)=='y'.or.string(1:1)=='Y' & |
| 152 |
.or.string(1:1)=='t'.or.string(1:1)=='T' & |
| 153 |
.or.trim(string)=='on'.or.trim(string)=='ON' & |
| 154 |
.or.trim(string)=='1') then |
| 155 |
lenvstring = .true. |
| 156 |
else |
| 157 |
lenvstring = .false. |
| 158 |
endif |
| 159 |
|
| 160 |
end function lenvstring |
| 161 |
|
| 162 |
! |
| 163 |
!--utility routine to extract integer value from string |
| 164 |
! |
| 165 |
integer function ienvstring(string,errval) |
| 166 |
character(len=*), intent(in) :: string |
| 167 |
integer, intent(in), optional :: errval |
| 168 |
character(len=5) :: fmtstring |
| 169 |
integer :: ierr |
| 170 |
|
| 171 |
if (len_trim(string) > 0) then |
| 172 |
!--use a formatted read - this is to avoid a compiler bug |
| 173 |
! should in general be more robust anyway |
| 174 |
write(fmtstring,"(a,i2,a)",iostat=ierr) '(i',len_trim(string),')' |
| 175 |
read(string,fmtstring,iostat=ierr) ienvstring |
| 176 |
else |
| 177 |
ierr = 1 |
| 178 |
endif |
| 179 |
|
| 180 |
if (ierr /= 0) then |
| 181 |
if (present(errval)) then |
| 182 |
ienvstring = errval |
| 183 |
else |
| 184 |
ienvstring = 0 |
| 185 |
endif |
| 186 |
endif |
| 187 |
|
| 188 |
end function ienvstring |
| 189 |
! |
| 190 |
!--utility routine to extract real value from string |
| 191 |
! |
| 192 |
real function renvstring(string,errval) |
| 193 |
character(len=*), intent(in) :: string |
| 194 |
real, intent(in), optional :: errval |
| 195 |
character(len=5) :: fmtstring |
| 196 |
integer :: ierr |
| 197 |
|
| 198 |
if (len_trim(string) > 0) then |
| 199 |
read(string,*,iostat=ierr) renvstring |
| 200 |
else |
| 201 |
ierr = 1 |
| 202 |
endif |
| 203 |
|
| 204 |
if (ierr /= 0) then |
| 205 |
if (present(errval)) then |
| 206 |
renvstring = errval |
| 207 |
else |
| 208 |
renvstring = 0. |
| 209 |
endif |
| 210 |
endif |
| 211 |
|
| 212 |
end function renvstring |
| 213 |
! |
| 214 |
!--this routine returns an arbitrary number of |
| 215 |
! comma separated strings |
| 216 |
! |
| 217 |
subroutine envlist(variable,nlist,list) |
| 218 |
character(len=*), intent(in) :: variable |
| 219 |
integer, intent(out) :: nlist |
| 220 |
character(len=*), dimension(:), intent(out), optional :: list |
| 221 |
character(len=120) :: string |
| 222 |
character(len=10) :: dummy |
| 223 |
integer :: i1,i2,ierr |
| 224 |
logical :: notlistfull |
| 225 |
|
| 226 |
!--set list to blank strings if argument is present |
| 227 |
if (present(list)) then |
| 228 |
list = ' ' |
| 229 |
endif |
| 230 |
|
| 231 |
!--get envlist from the environment |
| 232 |
call get_environment_or_flag(variable,string) |
| 233 |
|
| 234 |
!--split the string on commas |
| 235 |
i1 = 1 |
| 236 |
i2 = index(string,',')-1 |
| 237 |
if (i2==-1) i2 = len_trim(string) |
| 238 |
nlist = 0 |
| 239 |
ierr = 0 |
| 240 |
notlistfull = .true. |
| 241 |
|
| 242 |
!--for each comma separated string, add a list member |
| 243 |
do while(i2 >= i1 .and. notlistfull .and. ierr==0) |
| 244 |
nlist = nlist + 1 |
| 245 |
!print*,'i1,i2,stringfrag= ',i1,i2,trim(string(i1:)) |
| 246 |
if (present(list)) then |
| 247 |
read(string(i1:i2),"(a)",iostat=ierr) list(nlist) |
| 248 |
notlistfull = (nlist < size(list)) |
| 249 |
else |
| 250 |
read(string(i1:i2),"(a)",iostat=ierr) dummy ! to get ierr at end of string |
| 251 |
notlistfull = .true. |
| 252 |
endif |
| 253 |
i1 = i2 + 2 |
| 254 |
i2 = index(string(i1:),',') |
| 255 |
if (i2==0) then |
| 256 |
i2 = len_trim(string) |
| 257 |
else |
| 258 |
i2 = i2 + i1 - 2 |
| 259 |
endif |
| 260 |
enddo |
| 261 |
|
| 262 |
end subroutine envlist |
| 263 |
! |
| 264 |
!--return comma separated list of integers |
| 265 |
! |
| 266 |
function ienvlist(variable,nlist,errval) |
| 267 |
character(len=*), intent(in) :: variable |
| 268 |
integer, intent(in) :: nlist |
| 269 |
integer, intent(in), optional :: errval |
| 270 |
character(len=30), dimension(nlist) :: list |
| 271 |
integer :: ienvlist(nlist),i,ngot |
| 272 |
|
| 273 |
ngot = nlist |
| 274 |
call envlist(variable,ngot,list) |
| 275 |
|
| 276 |
do i=1,nlist |
| 277 |
if (present(errval)) then |
| 278 |
ienvlist(i) = ienvstring(list(i),errval=errval) |
| 279 |
else |
| 280 |
ienvlist(i) = ienvstring(list(i)) |
| 281 |
endif |
| 282 |
enddo |
| 283 |
|
| 284 |
end function ienvlist |
| 285 |
! |
| 286 |
!--return comma separated list of logicals |
| 287 |
! |
| 288 |
function lenvlist(variable,nlist) |
| 289 |
character(len=*), intent(in) :: variable |
| 290 |
integer, intent(in) :: nlist |
| 291 |
character(len=30), dimension(nlist) :: list |
| 292 |
logical :: lenvlist(nlist) |
| 293 |
integer :: i,ngot |
| 294 |
|
| 295 |
ngot = nlist |
| 296 |
call envlist(variable,ngot,list) |
| 297 |
|
| 298 |
do i=1,nlist |
| 299 |
lenvlist(i) = lenvstring(list(i)) |
| 300 |
enddo |
| 301 |
|
| 302 |
end function lenvlist |
| 303 |
! |
| 304 |
!--return comma separated list of reals |
| 305 |
! |
| 306 |
function renvlist(variable,nlist,errval) |
| 307 |
character(len=*), intent(in) :: variable |
| 308 |
integer, intent(in) :: nlist |
| 309 |
real, intent(in), optional :: errval |
| 310 |
character(len=30), dimension(nlist) :: list |
| 311 |
real :: renvlist(nlist) |
| 312 |
integer :: i,ngot |
| 313 |
|
| 314 |
ngot = nlist |
| 315 |
call envlist(variable,ngot,list) |
| 316 |
|
| 317 |
do i=1,nlist |
| 318 |
if (present(errval)) then |
| 319 |
renvlist(i) = renvstring(list(i),errval=errval) |
| 320 |
else |
| 321 |
renvlist(i) = renvstring(list(i)) |
| 322 |
endif |
| 323 |
enddo |
| 324 |
|
| 325 |
end function renvlist |
| 326 |
|
| 327 |
! |
| 328 |
!--find logical-valued option from command line arguments |
| 329 |
! as in --arg (true if present, false if not) |
| 330 |
! |
| 331 |
subroutine get_option(variable,value,err) |
| 332 |
character(len=*), intent(in) :: variable |
| 333 |
character(len=*), intent(out) :: value |
| 334 |
character(len=80) :: string |
| 335 |
integer, intent(out) :: err |
| 336 |
integer :: nargs,iarg,ieq |
| 337 |
|
| 338 |
err = 1 |
| 339 |
nargs = command_argument_count() |
| 340 |
do iarg=1,nargs |
| 341 |
call get_command_argument(iarg,string) |
| 342 |
if (string(1:2)=='--' .and. index(lcase(string),lcase(variable)) > 0) then |
| 343 |
err = 0 |
| 344 |
ieq = index(string,'=',back=.true.) |
| 345 |
if (ieq > 0) then |
| 346 |
value = string(ieq+1:) |
| 347 |
else |
| 348 |
value = 'True' ! raw flag --foo, equivalent to --foo=True |
| 349 |
endif |
| 350 |
endif |
| 351 |
enddo |
| 352 |
|
| 353 |
end subroutine get_option |
| 354 |
! |
| 355 |
!--find real-valued option from command line arguments |
| 356 |
! as in --arg=blah |
| 357 |
! |
| 358 |
real function get_command_option(variable,default) result(val) |
| 359 |
character(len=*), intent(in) :: variable |
| 360 |
real, intent(in), optional :: default |
| 361 |
character(len=80) :: string |
| 362 |
integer :: ierr,nargs,ieq,iarg |
| 363 |
|
| 364 |
val = 0. |
| 365 |
if (present(default)) val = default |
| 366 |
nargs = command_argument_count() |
| 367 |
do iarg=1,nargs |
| 368 |
call get_command_argument(iarg,string) |
| 369 |
ieq = index(string,'=') |
| 370 |
if (string(1:1)=='-' .and. index(string,variable) > 0 .and. ieq > 0) then |
| 371 |
read(string(ieq+1:),*,iostat=ierr) val |
| 372 |
endif |
| 373 |
enddo |
| 374 |
|
| 375 |
end function get_command_option |
| 376 |
|
| 377 |
! |
| 378 |
!--find logical-valued option from command line arguments |
| 379 |
! as in --arg (true if present, false if not) |
| 380 |
! |
| 381 |
logical function get_command_flag(variable) result(val) |
| 382 |
character(len=*), intent(in) :: variable |
| 383 |
character(len=80) :: string |
| 384 |
integer :: nargs,iarg |
| 385 |
|
| 386 |
val = .false. |
| 387 |
nargs = command_argument_count() |
| 388 |
do iarg=1,nargs |
| 389 |
call get_command_argument(iarg,string) |
| 390 |
if (string(1:1)=='-' .and. index(string,variable) > 0) val = .true. |
| 391 |
enddo |
| 392 |
|
| 393 |
end function get_command_flag |
| 394 |
|
| 395 |
! |
| 396 |
!--count the number of arguments matching a certain substring |
| 397 |
! e.g. with particular filename extension |
| 398 |
! |
| 399 |
integer function count_matching_args(string,id) result(n) |
| 400 |
character(len=*), intent(in) :: string |
| 401 |
integer, intent(out), optional :: id(:) |
| 402 |
character(len=1024) :: myarg |
| 403 |
integer :: iarg,nargs |
| 404 |
|
| 405 |
n = 0 |
| 406 |
nargs = command_argument_count() |
| 407 |
do iarg=1,nargs |
| 408 |
call get_command_argument(iarg,myarg) |
| 409 |
if (index(myarg,string) > 0) then |
| 410 |
n = n + 1 |
| 411 |
if (present(id)) then |
| 412 |
if (size(id) >= n) id(n) = iarg |
| 413 |
endif |
| 414 |
endif |
| 415 |
enddo |
| 416 |
|
| 417 |
end function count_matching_args |
| 418 |
|
| 419 |
! |
| 420 |
!--get the name of the logged in user |
| 421 |
! |
| 422 |
subroutine get_user(string) |
| 423 |
character(len=*), intent(out) :: string |
| 424 |
character(len=*), parameter :: tempfile = '/tmp/splash.username' |
| 425 |
logical :: iexist |
| 426 |
integer :: ierr,iu |
| 427 |
|
| 428 |
string = '' |
| 429 |
inquire(file='/proc/cpuinfo',exist=iexist) ! exists only in Linux |
| 430 |
if (.not.iexist) then |
| 431 |
call system('id -F $USER > '//trim(tempfile)) ! Mac |
| 432 |
else |
| 433 |
call system('getent passwd "$USER" | cut -d: -f5 | cut -d, -f1 > '//trim(tempfile)) ! Linux |
| 434 |
endif |
| 435 |
inquire(file=tempfile,exist=iexist) |
| 436 |
if (iexist) then |
| 437 |
open(newunit=iu,file=tempfile,action='read',status='old',iostat=ierr) |
| 438 |
read(iu,"(a)",iostat=ierr) string |
| 439 |
close(iu,status='delete',iostat=ierr) |
| 440 |
endif |
| 441 |
|
| 442 |
end subroutine get_user |
| 443 |
|
| 444 |
! |
| 445 |
!--get copyright string involving logged in user and current year |
| 446 |
! |
| 447 |
function get_copyright() result(string) |
| 448 |
character(len=40) :: string |
| 449 |
character(len=8) :: year |
| 450 |
|
| 451 |
call get_user(string) |
| 452 |
call date_and_time(date=year) |
| 453 |
|
| 454 |
string = '(c) '//year(1:4)//' '//trim(string) |
| 455 |
|
| 456 |
end function get_copyright |
| 457 |
|
| 458 |
pure function to_utf8_safe(str) result(out) |
| 459 |
character(len=*), intent(in) :: str |
| 460 |
character(len=:), allocatable :: out |
| 461 |
character(len=:), allocatable :: tmp |
| 462 |
integer :: i, j, ilen, b |
| 463 |
|
| 464 |
ilen = len_trim(str) |
| 465 |
allocate(character(len=4*ilen) :: tmp) |
| 466 |
j = 0; i = 1 |
| 467 |
do while (i <= ilen) |
| 468 |
b = iachar(str(i:i)) |
| 469 |
select case (b) |
| 470 |
case (0:127) ! ASCII |
| 471 |
j=j+1; tmp(j:j)=achar(b); i=i+1 |
| 472 |
case (192:223) ! 2-byte UTF-8 start? |
| 473 |
if (i<ilen .and. iachar(str(i+1:i+1))>=128 .and. iachar(str(i+1:i+1))<=191) then |
| 474 |
j=j+1; tmp(j:j)=achar(b) |
| 475 |
j=j+1; tmp(j:j)=achar(iachar(str(i+1:i+1))) |
| 476 |
i=i+2 |
| 477 |
else ! treat as Latin-1 |
| 478 |
j=j+1; tmp(j:j)=achar(192 + ishft(b,-6)) |
| 479 |
j=j+1; tmp(j:j)=achar(128 + iand(b,63)) |
| 480 |
i=i+1 |
| 481 |
endif |
| 482 |
case (224:247) ! 3/4-byte starts: assume already UTF-8, copy bytes |
| 483 |
! minimally pass through remaining continuation bytes |
| 484 |
j=j+1; tmp(j:j)=achar(b); i=i+1 |
| 485 |
case default ! Latin-1 byte (128-191 or 248-255) |
| 486 |
j=j+1; tmp(j:j)=achar(192 + ishft(b,-6)) |
| 487 |
j=j+1; tmp(j:j)=achar(128 + iand(b,63)) |
| 488 |
i=i+1 |
| 489 |
end select |
| 490 |
enddo |
| 491 |
out = tmp(1:j) |
| 492 |
|
| 493 |
end function to_utf8_safe |
| 494 |
|
| 495 |
end module system_utils |