~ubuntu-branches/ubuntu/trusty/heroes/trusty

« back to all changes in this revision

Viewing changes to m4/normpath.m4

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Burrows
  • Date: 2002-03-31 20:42:30 UTC
  • Revision ID: james.westby@ubuntu.com-20020331204230-v7i07q3km2dzu0et
Tags: upstream-0.21
ImportĀ upstreamĀ versionĀ 0.21

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
dnl @synopsis adl_NORMALIZE_PATH(VARNAME, [REFERENCE_STRING])
 
2
dnl
 
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).
 
8
dnl
 
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.
 
12
dnl
 
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.
 
16
dnl
 
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'
 
28
dnl
 
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
 
31
dnl
 
32
dnl   expr 'A:\FOO' : 'A:\Foo'
 
33
dnl
 
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:
 
37
dnl
 
38
dnl   expr 'A:\Foo' : 'A:\\Foo'
 
39
dnl
 
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.
 
44
dnl
 
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).
 
48
dnl
 
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'
 
58
dnl
 
59
dnl @author Alexandre Duret-Lutz <duret_g@epita.fr>
 
60
dnl @version $Id: $
 
61
AC_DEFUN([adl_NORMALIZE_PATH],
 
62
[case ":[$]$1:" in
 
63
# change empty paths to '.'
 
64
  ::) $1='.' ;;
 
65
# strip trailing slashes
 
66
  :*[[\\/]]:) $1=`echo "[$]$1" | sed 's,[[\\/]]*[$],,'` ;;
 
67
  :*:) ;;
 
68
esac
 
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'` ;;
 
75
esac])