Imported Upstream version 1.15.2
[platform/upstream/krb5.git] / src / config / ac-archive / relpaths.m4
1 dnl @synopsis adl_COMPUTE_RELATIVE_PATHS(PATH_LIST)
2 dnl
3 dnl PATH_LIST is a space-separated list of colon-separated triplets of
4 dnl the form 'FROM:TO:RESULT'. This function iterates over these
5 dnl triplets and set $RESULT to the relative path from $FROM to $TO.
6 dnl Note that $FROM and $TO needs to be absolute filenames for this
7 dnl macro to success.
8 dnl
9 dnl For instance,
10 dnl
11 dnl    first=/usr/local/bin
12 dnl    second=/usr/local/share
13 dnl    adl_COMPUTE_RELATIVE_PATHS([first:second:fs second:first:sf])
14 dnl    # $fs is set to ../share
15 dnl    # $sf is set to ../bin
16 dnl
17 dnl $FROM and $TO are both eval'ed recursively and normalized, this
18 dnl means that you can call this macro with autoconf's dirnames like
19 dnl `prefix' or `datadir'. For example:
20 dnl
21 dnl    adl_COMPUTE_RELATIVE_PATHS([bindir:datadir:bin_to_data])
22 dnl
23 dnl adl_COMPUTE_RELATIVE_PATHS should also works with DOS filenames.
24 dnl
25 dnl You may want to use this macro in order to make your package
26 dnl relocatable. Instead of hardcoding $datadir into your programs just
27 dnl encode $bin_to_data and try to determine $bindir at run-time.
28 dnl
29 dnl This macro requires adl_NORMALIZE_PATH.
30 dnl
31 dnl @category Misc
32 dnl @author Alexandre Duret-Lutz <duret_g@epita.fr>
33 dnl @version 2001-05-25
34 dnl @license GPLWithACException
35
36 AC_DEFUN([adl_COMPUTE_RELATIVE_PATHS],
37 [for _lcl_i in $1; do
38   _lcl_from=\[$]`echo "[$]_lcl_i" | sed 's,:.*$,,'`
39   _lcl_to=\[$]`echo "[$]_lcl_i" | sed 's,^[[^:]]*:,,' | sed 's,:[[^:]]*$,,'`
40   _lcl_result_var=`echo "[$]_lcl_i" | sed 's,^.*:,,'`
41   adl_RECURSIVE_EVAL([[$]_lcl_from], [_lcl_from])
42   adl_RECURSIVE_EVAL([[$]_lcl_to], [_lcl_to])
43   _lcl_notation="$_lcl_from$_lcl_to"
44   adl_NORMALIZE_PATH([_lcl_from],['/'])
45   adl_NORMALIZE_PATH([_lcl_to],['/'])
46   adl_COMPUTE_RELATIVE_PATH([_lcl_from], [_lcl_to], [_lcl_result_tmp])
47   adl_NORMALIZE_PATH([_lcl_result_tmp],["[$]_lcl_notation"])
48   eval $_lcl_result_var='[$]_lcl_result_tmp'
49 done])
50
51 ## Note:
52 ## *****
53 ## The following helper macros are too fragile to be used out
54 ## of adl_COMPUTE_RELATIVE_PATHS (mainly because they assume that
55 ## paths are normalized), that's why I'm keeping them in the same file.
56 ## Still, some of them maybe worth to reuse.
57
58 dnl adl_COMPUTE_RELATIVE_PATH(FROM, TO, RESULT)
59 dnl ===========================================
60 dnl Compute the relative path to go from $FROM to $TO and set the value
61 dnl of $RESULT to that value.  This function work on raw filenames
62 dnl (for instead it will considerate /usr//local and /usr/local as
63 dnl two distinct paths), you should really use adl_COMPUTE_REALTIVE_PATHS
64 dnl instead to have the paths sanitized automatically.
65 dnl
66 dnl For instance:
67 dnl    first_dir=/somewhere/on/my/disk/bin
68 dnl    second_dir=/somewhere/on/another/disk/share
69 dnl    adl_COMPUTE_RELATIVE_PATH(first_dir, second_dir, first_to_second)
70 dnl will set $first_to_second to '../../../another/disk/share'.
71 AC_DEFUN([adl_COMPUTE_RELATIVE_PATH],
72 [adl_COMPUTE_COMMON_PATH([$1], [$2], [_lcl_common_prefix])
73 adl_COMPUTE_BACK_PATH([$1], [_lcl_common_prefix], [_lcl_first_rel])
74 adl_COMPUTE_SUFFIX_PATH([$2], [_lcl_common_prefix], [_lcl_second_suffix])
75 $3="[$]_lcl_first_rel[$]_lcl_second_suffix"])
76
77 dnl adl_COMPUTE_COMMON_PATH(LEFT, RIGHT, RESULT)
78 dnl ============================================
79 dnl Compute the common path to $LEFT and $RIGHT and set the result to $RESULT.
80 dnl
81 dnl For instance:
82 dnl    first_path=/somewhere/on/my/disk/bin
83 dnl    second_path=/somewhere/on/another/disk/share
84 dnl    adl_COMPUTE_COMMON_PATH(first_path, second_path, common_path)
85 dnl will set $common_path to '/somewhere/on'.
86 AC_DEFUN([adl_COMPUTE_COMMON_PATH],
87 [$3=''
88 _lcl_second_prefix_match=''
89 while test "[$]_lcl_second_prefix_match" != 0; do
90   _lcl_first_prefix=`expr "x[$]$1" : "x\([$]$3/*[[^/]]*\)"`
91   _lcl_second_prefix_match=`expr "x[$]$2" : "x[$]_lcl_first_prefix"`
92   if test "[$]_lcl_second_prefix_match" != 0; then
93     if test "[$]_lcl_first_prefix" != "[$]$3"; then
94       $3="[$]_lcl_first_prefix"
95     else
96       _lcl_second_prefix_match=0
97     fi
98   fi
99 done])
100
101 dnl adl_COMPUTE_SUFFIX_PATH(PATH, SUBPATH, RESULT)
102 dnl ==============================================
103 dnl Substrack $SUBPATH from $PATH, and set the resulting suffix
104 dnl (or the empty string if $SUBPATH is not a subpath of $PATH)
105 dnl to $RESULT.
106 dnl
107 dnl For instace:
108 dnl    first_path=/somewhere/on/my/disk/bin
109 dnl    second_path=/somewhere/on
110 dnl    adl_COMPUTE_SUFFIX_PATH(first_path, second_path, common_path)
111 dnl will set $common_path to '/my/disk/bin'.
112 AC_DEFUN([adl_COMPUTE_SUFFIX_PATH],
113 [$3=`expr "x[$]$1" : "x[$]$2/*\(.*\)"`])
114
115 dnl adl_COMPUTE_BACK_PATH(PATH, SUBPATH, RESULT)
116 dnl ============================================
117 dnl Compute the relative path to go from $PATH to $SUBPATH, knowing that
118 dnl $SUBPATH is a subpath of $PATH (any other words, only repeated '../'
119 dnl should be needed to move from $PATH to $SUBPATH) and set the value
120 dnl of $RESULT to that value.  If $SUBPATH is not a subpath of PATH,
121 dnl set $RESULT to the empty string.
122 dnl
123 dnl For instance:
124 dnl    first_path=/somewhere/on/my/disk/bin
125 dnl    second_path=/somewhere/on
126 dnl    adl_COMPUTE_BACK_PATH(first_path, second_path, back_path)
127 dnl will set $back_path to '../../../'.
128 AC_DEFUN([adl_COMPUTE_BACK_PATH],
129 [adl_COMPUTE_SUFFIX_PATH([$1], [$2], [_lcl_first_suffix])
130 $3=''
131 _lcl_tmp='xxx'
132 while test "[$]_lcl_tmp" != ''; do
133   _lcl_tmp=`expr "x[$]_lcl_first_suffix" : "x[[^/]]*/*\(.*\)"`
134   if test "[$]_lcl_first_suffix" != ''; then
135      _lcl_first_suffix="[$]_lcl_tmp"
136      $3="../[$]$3"
137   fi
138 done])
139
140
141 dnl adl_RECURSIVE_EVAL(VALUE, RESULT)
142 dnl =================================
143 dnl Interpolate the VALUE in loop until it doesn't change,
144 dnl and set the result to $RESULT.
145 dnl WARNING: It's easy to get an infinite loop with some unsane input.
146 AC_DEFUN([adl_RECURSIVE_EVAL],
147 [_lcl_receval="$1"
148 $2=`(test "x$prefix" = xNONE && prefix="$ac_default_prefix"
149      test "x$exec_prefix" = xNONE && exec_prefix="${prefix}"
150      _lcl_receval_old=''
151      while test "[$]_lcl_receval_old" != "[$]_lcl_receval"; do
152        _lcl_receval_old="[$]_lcl_receval"
153        eval _lcl_receval="\"[$]_lcl_receval\""
154      done
155      echo "[$]_lcl_receval")`])