Logo AND Algorithmique Numérique Distribuée

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