1
dnl @synopsis adl_NORMALIZE_PATH(VARNAME, [REFERENCE_STRING])
3
dnl Perform some cleanups on the value of $VARNAME (interpreted as a path):
4
dnl - empty paths are changed to '.'
5
dnl - trailing slashes are removed
6
dnl - repeated slashes are squeezed except a leading doubled slash '//'
7
dnl (which might indicate a networked disk on some OS).
9
dnl REFERENCE_STRING is used to turn '/' into '\' and vice-versa:
10
dnl if REFERENCE_STRING contains some backslashes, all slashes and backslashes
11
dnl are turned into backslashes, otherwise they are all turned into slashes.
13
dnl This makes processing of DOS filenames quite easier, because you
14
dnl can turn a filename to the Unix notation, make your processing, and
15
dnl turn it back to original notation.
17
dnl filename='A:\FOO\\BAR\'
18
dnl old_filename="$filename"
19
dnl # Switch to the unix notation
20
dnl adl_NORMALIZE_PATH([filename], ["/"])
21
dnl # now we have $filename = 'A:/FOO/BAR' and we can process it as if
22
dnl # it was a Unix path. For instance let's say that you want
23
dnl # to append '/subpath':
24
dnl filename="$filename/subpath"
25
dnl # finally switch back to the original notation
26
dnl adl_NORMALIZE_PATH([filename], ["$old_filename"])
27
dnl # now $filename equals to 'A:\FOO\BAR\subpath'
29
dnl One good reason to make all path processing with the unix convention
30
dnl is that backslashes have a special meaning in many cases. For instance
32
dnl expr 'A:\FOO' : 'A:\Foo'
34
dnl will return 0 because the second argument is a regex in which
35
dnl backslashes have to be backslashed. In other words, to have the
36
dnl two strings to match you should write this instead:
38
dnl expr 'A:\Foo' : 'A:\\Foo'
40
dnl Such behavior makes DOS filenames extremely unpleasant to work with.
41
dnl So temporary turn your paths to the Unix notation, and revert
42
dnl them to the original notation after the processing. See the
43
dnl macro adl_COMPUTE_RELATIVE_PATHS for a concrete example of this.
45
dnl REFERENCE_STRING defaults to $VARIABLE, this means that slashes
46
dnl will be converted to backslashes if $VARIABLE already contains
47
dnl some backslashes (see $thirddir below).
49
dnl firstdir='/usr/local//share'
50
dnl seconddir='C:\Program Files\\'
51
dnl thirddir='C:\home/usr/'
52
dnl adl_NORMALIZE_PATH([firstdir])
53
dnl adl_NORMALIZE_PATH([seconddir])
54
dnl adl_NORMALIZE_PATH([thirddir])
55
dnl # $firstdir = '/usr/local/share'
56
dnl # $seconddir = 'C:\Program Files'
57
dnl # $thirddir = 'C:\home\usr'
59
dnl @author Alexandre Duret-Lutz <duret_g@epita.fr>
61
AC_DEFUN([adl_NORMALIZE_PATH],
63
# change empty paths to '.'
65
# strip trailing slashes
66
:*[[\\/]]:) $1=`echo "[$]$1" | sed 's,[[\\/]]*[$],,'` ;;
69
# squeze repeated slashes
70
case ifelse($2,,"[$]$1",$2) in
71
# if the path contains any backslashes, turn slashes into backslashes
72
*\\*) $1=`echo "[$]$1" | sed 's,\(.\)[[\\/]][[\\/]]*,\1\\\\,g'` ;;
73
# if the path contains slashes, also turn backslashes into slashes
74
*) $1=`echo "[$]$1" | sed 's,\(.\)[[\\/]][[\\/]]*,\1/,g'` ;;