Perform some cleanups on the value of $VARNAME (interpreted as a path):
- empty paths are changed to '.'
- trailing slashes are removed
- repeated slashes are squeezed except a leading doubled slash '//'
(which might indicate a networked disk on some OS).
REFERENCE_STRING is used to turn '/' into '\' and vice-versa:
if REFERENCE_STRING contains some backslashes, all slashes and backslashes
are turned into backslashes, otherwise they are all turned into slashes.
This makes processing of DOS filenames quite easier, because you
can turn a filename to the Unix notation, make your processing, and
turn it back to original notation.
# Switch to the unix notation
# now we have $filename = 'A:/FOO/BAR' and we can process it as if
# it was a Unix path. For instance let's say that you want
# to append '/subpath':
# finally switch back to the original notation
# now $filename equals to 'A:\FOO\BAR\subpath'
One good reason to make all path processing with the unix convention
is that backslashes have a special meaning in many cases. For instance
expr 'A:\FOO' : 'A:\Foo'
will return 0 because the second argument is a regex in which
backslashes have to be backslashed. In other words, to have the
two strings to match you should write this instead:
expr 'A:\Foo' : 'A:\\Foo'
Such behavior makes DOS filenames extremely unpleasant to work with.
So temporary turn your paths to the Unix notation, and revert
them to the original notation after the processing. See the
macro adl_COMPUTE_RELATIVE_PATHS for a concrete example of this.
REFERENCE_STRING defaults to $VARIABLE, this means that slashes
will be converted to backslashes if $VARIABLE already contains
some backslashes (see $thirddir below).
# $firstdir = '/usr/local/share'
# $seconddir = 'C:\Program Files'
# $thirddir = 'C:\home\usr'