Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
yet another attempt to handle MPI special values in fortran
[simgrid.git] / src / smpi / smpitools.sh
1 #!/usr/bin/env sh
2
3 # Copyright (c) 2013-2019. The SimGrid Team.
4 # All rights reserved.
5
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the license (GNU LGPL) which comes with this package.
8
9 SAVEIFS="$IFS"
10 LISTSEP="$(printf '\b')"
11
12 # Create a temporary file, with its name of the form $1_XXX$2, where XXX is replaced by an unique string.
13 # $1: prefix, $2: suffix
14 mymktemp () {
15     tmp=$(mktemp --suffix="$2" "$1_XXXXXXXXXX" 2> /dev/null)
16     if [ -z "$tmp" ]; then
17         # mktemp failed (unsupported --suffix ?), try unsafe mode
18         tmp=$(mktemp -u "$1_XXXXXXXXXX" 2> /dev/null)
19         if [ -z "$tmp" ]; then
20             # mktemp failed again (doesn't exist ?), try very unsafe mode
21             if [ -z "${mymktemp_seq}" ]; then
22                 mymktemp_seq=$(date +%d%H%M%S)
23             fi
24             tmp="$1_$$x${mymktemp_seq}"
25             mymktemp_seq=$((mymktemp_seq + 1))
26         fi
27         tmp="${tmp}$2"
28         # create temp file, and exit if it existed before
29         sh -C -c "true > \"${tmp}\"" || exit 1
30     fi
31     echo "${tmp}"
32 }
33
34 # Add a word to the end of a list (words separated by LISTSEP)
35 # $1: list, $2...: words to add
36 list_add () {
37     local list content newcontent
38     list="$1"
39     shift
40     if [ $# -gt 0 ]; then
41         eval content=\"\${$list}\"
42         IFS="$LISTSEP"
43         newcontent="$*"
44         IFS="$SAVEIFS"
45         if [ -z "$content" ]; then
46             content="$newcontent"
47         else
48             content="$content${LISTSEP}$newcontent"
49         fi
50         eval $list=\"\${content}\"
51     fi
52 }
53
54 # Like list_add, but only if first word to add ($2) is not empty
55 list_add_not_empty () {
56     if [ -n "$2" ]; then
57         list_add "$@"
58     fi
59 }
60
61 # Set contents of a list (words separated by LISTSEP)
62 # $1: list, $2...: words to set
63 list_set () {
64     eval $1=""
65     list_add "$@"
66 }
67
68 # Get the content of a list: positional parameters ($1, $2, ...) are set to the content of the list
69 # $1: list
70 # usage:  eval $(list_get list)
71 list_get () {
72     printf 'IFS="'\$'LISTSEP"; eval set -- \$%s; IFS="'\$'SAVEIFS"\n' "$1"
73 }