Back to the Main Page.


Download the M4 Source.




1.1 (2001/07/26)     Miscellaneous @ ac-archive-0.5.39


Alexandre Duret-Lutz <>


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
   adl_NORMALIZE_PATH([filename], ["/"])
   # 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
   adl_NORMALIZE_PATH([filename], ["$old_filename"])
   # 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).

   seconddir='C:\Program Files\\'
   # $firstdir = '/usr/local/share'
   # $seconddir = 'C:\Program Files'
   # $thirddir = 'C:\home\usr'

M4 Source Code
[case ":[$]$1:" in
# change empty paths to '.'
  ::) $1='.' ;;
# strip trailing slashes
  :*[[\\/]]:) $1=`echo "[$]$1" | sed 's,[[\\/]]*[$],,'` ;;
  :*:) ;;
# squeze repeated slashes
case ifelse($2,,"[$]$1",$2) in
# if the path contains any backslashes, turn slashes into backslashes
 *\\*) $1=`echo "[$]$1" | sed 's,\(.\)[[\\/]][[\\/]]*,\1\\\\,g'` ;;
# if the path contains slashes, also turn backslashes into slashes
 *) $1=`echo "[$]$1" | sed 's,\(.\)[[\\/]][[\\/]]*,\1/,g'` ;;