Commit | Line | Data |
---|---|---|
e976486e DM |
1 | #!/bin/sh |
2 | # Take a list of MSYS-compatible paths and convert them to native | |
3 | # MS-Windows format. | |
4 | # Status is zero if successful, nonzero otherwise. | |
5 | ||
ba318903 | 6 | # Copyright (C) 2013-2014 Free Software Foundation, Inc. |
e976486e DM |
7 | |
8 | # This program is free software: you can redistribute it and/or modify | |
9 | # it under the terms of the GNU General Public License as published by | |
10 | # the Free Software Foundation, either version 3 of the License, or | |
11 | # (at your option) any later version. | |
12 | ||
13 | # This program is distributed in the hope that it will be useful, | |
14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | # GNU General Public License for more details. | |
17 | ||
18 | # You should have received a copy of the GNU General Public License | |
19 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 | ||
21 | # Take only the basename from the full pathname | |
22 | me=${0//*\//} | |
23 | ||
24 | usage="usage: ${me} PATHLIST [MUSTEXIST] [SEPARATOR [SEPARATOR2]]" | |
25 | ||
26 | help="$usage | |
27 | or: ${me} OPTION | |
28 | ||
29 | Convert MSYS-compatible paths to MS-Windows native format. | |
30 | ||
31 | PATHLIST should be a list of paths separated by SEPARATOR. This list | |
32 | will be written to the standard output after performing the following | |
33 | transformations: | |
34 | 1. Discard empty paths. | |
35 | 2. Replace backslashes with forward slashes. | |
36 | 3. Replace two consecutive slashes with single ones. | |
37 | 4. Translate to Windows-native format those paths that are not in such | |
38 | format already. The translated paths will not end with a slash, | |
a1209bc3 DM |
39 | except for root directories (e.g. 'c:/' or 'c:/foo'). Paths |
40 | starting with '%emacs_dir%' will not be translated. | |
48a2a0b4 | 41 | 5. Escape with backslashes every occurrence of SEPARATOR2 within the paths. |
e976486e DM |
42 | 6. Concatenate the translated paths with SEPARATOR2. |
43 | ||
44 | If MUSTEXIST is 'Y' or not supplied, then each path in PATHLIST must | |
45 | exist. Otherwise, only some part of each path is required to exist | |
46 | (the deepest existing subpath will be translated and the remainder | |
47 | concatenated to the translation). | |
48 | ||
49 | If SEPARATOR is not supplied, PATHLIST will be regarded as a single | |
50 | path. | |
51 | ||
52 | If SEPARATOR2 is not supplied, it will take the same value as | |
53 | SEPARATOR. | |
54 | ||
55 | Options: | |
56 | --help display this help and exit | |
57 | ||
58 | Report bugs to <bug-gnu-emacs@gnu.org>." | |
59 | ||
60 | for arg | |
61 | do | |
62 | case $arg in | |
63 | --help | --hel | --he | --h) | |
64 | exec echo "$help" ;; | |
65 | --) | |
66 | shift | |
67 | break ;; | |
68 | -*) | |
69 | echo "${me}: invalid option: $arg" >&2 | |
70 | exit 1 ;; | |
71 | *) | |
72 | break ;; | |
73 | esac | |
74 | done | |
75 | ||
76 | { test $# -ge 1 && test $# -le 4; } || | |
77 | { echo "${me}: $usage" >&2; exit 1; } | |
78 | ||
79 | # Arguments | |
80 | pathlist="$1" | |
81 | mustexist="${2:-Y}" | |
82 | separator="$3" | |
83 | separator2="${4:-${separator}}" | |
84 | ||
85 | # Split pathlist into its path components | |
86 | if test -n "$separator" | |
87 | then | |
88 | IFS=${separator} patharray=( $pathlist ) | |
89 | else | |
90 | patharray=( "$pathlist" ) | |
91 | fi | |
92 | ||
93 | w32pathlist="" | |
94 | ||
95 | for p in "${patharray[@]}" | |
96 | do | |
97 | # Skip empty paths | |
98 | test "$p" = "" && continue | |
99 | ||
100 | # Replace '\' with '/' and '//' with '/' | |
101 | p="${p//\\//}" | |
102 | p="${p//\/\///}" | |
103 | ||
a1209bc3 DM |
104 | if test "${p:0:11}" = "%emacs_dir%" |
105 | then | |
106 | # Paths starting with "%emacs_dir%" will not be translated | |
107 | w32p=$p | |
108 | elif test -d "$p" | |
e976486e DM |
109 | then |
110 | # The path exists, so just translate it | |
111 | w32p=`cd "$p" && pwd -W` | |
112 | else | |
34a731f8 | 113 | # The path does not exist. So, try to guess the |
e976486e DM |
114 | # Windows-native translation, by looking for the deepest |
115 | # existing directory in this path, and then translating the | |
116 | # existing part and concatenating the remainder. | |
117 | ||
118 | test "${mustexist}" = "Y" && | |
119 | { echo "${me}: invalid path: $p" >&2; exit 1; } | |
120 | ||
121 | p1=$p | |
122 | IFS=/ pcomponents=( $p ) | |
123 | ||
124 | for (( i=${#pcomponents[@]}-1 ; i>=0 ; i-- )) | |
125 | do | |
126 | ||
127 | if test "${pcomponents[i]}" = "" | |
128 | then | |
129 | # The path component is empty. This can only mean | |
130 | # that the path starts with "/" and all components | |
131 | # have been stripped out already. So in this case we | |
132 | # want to test with the MSYS root directory | |
133 | p1="/" | |
134 | else | |
135 | p1="${p1%/}" | |
136 | p1="${p1%${pcomponents[i]}}" | |
137 | fi | |
138 | ||
139 | if test -d "${p1}" | |
140 | then | |
141 | ||
142 | # Existing path found | |
143 | ||
144 | # Translate the existing part and concatenate the | |
145 | # remainder (ensuring that only one slash is used in | |
146 | # the join, and no trailing slash is left) | |
147 | w32p1=`cd "${p1}" && pwd -W` | |
148 | remainder="${p#${p1}}" | |
149 | remainder="${remainder#/}" | |
150 | remainder="${remainder%/}" | |
151 | w32p="${w32p1%/}/${remainder}" | |
152 | ||
153 | break | |
154 | fi | |
155 | ||
156 | done | |
157 | ||
158 | # If no existing directory was found, error out | |
159 | test -e "${p1}" || | |
160 | { echo "${me}: invalid path: ${p}" >&2; exit 1; } | |
161 | fi | |
162 | ||
163 | # Concatenate the translated path to the translated pathlist | |
164 | test "${w32pathlist}" = "" || w32pathlist="${w32pathlist}${separator2}" | |
165 | w32pathlist="${w32pathlist}${w32p//${separator2}/\\${separator2}}" | |
166 | ||
167 | done | |
168 | ||
169 | # Write the translated pathlist to the standard output | |
a1209bc3 | 170 | printf "%s" "${w32pathlist}" |