Merge branch 'master' into cairocanvas
[ardour.git] / libs / pbd / pathexpand.cc
1 /*
2     Copyright (C) 2013 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <vector>
21 #include <iostream>
22 #include <climits>
23 #include <cerrno>
24 #include <cstdlib>
25
26 #include <regex.h>
27
28 #include <glibmm/miscutils.h>
29
30 #include "pbd/pathexpand.h"
31 #include "pbd/strsplit.h"
32
33 using std::string;
34 using std::vector;
35
36 #ifdef COMPILER_MINGW
37
38 #include <stdlib.h>
39 #include <glibmm.h>
40
41 /****************************************************************
42  * Emulate POSIX realpath() using Win32 _fullpath() since realpath()
43  * is not available.
44  *
45  * Returns:
46  *    On Success: A pointer to the resolved (absolute) path
47  *    On Failure: 0 (NULL)
48  */
49
50 static char* 
51 realpath (const char *original_path, char resolved_path[_MAX_PATH+1])
52 {
53         char *rpath = 0;
54         bool bIsSymLink = false; // We'll probably need to test the incoming path
55                                  // to find out if it points to a Windows shortcut
56                                  // (or a hard link) and set this appropriately.
57
58         if (bIsSymLink) {
59                 // At the moment I'm not sure if Windows '_fullpath()' is directly
60                 // equivalent to POSIX 'realpath()' - in as much as the latter will
61                 // resolve the supplied path if it happens to point to a symbolic
62                 // link ('_fullpath()' probably DOESN'T do this but I'm not really
63                 // sure if Ardour needs such functionality anyway). Therefore we'll
64                 // possibly need to add that functionality here at a later date.
65         } else {
66                 char temp[(_MAX_PATH+1)*6]; // Allow for maximum length of a path in wchar characters
67                 
68                 // POSIX 'realpath()' requires that the buffer size is at
69                 // least PATH_MAX+1, so assume that the user knew this !!
70
71                 rpath = _fullpath (temp, Glib::locale_from_utf8 (original_path).c_str(), _MAX_PATH);
72
73                 if (0 != rpath) {
74                         snprintf (resolved_path, _MAX_PATH+1, "%s", Glib::locale_to_utf8 (temp).c_str());
75                 }
76                         
77         }
78         
79         return (rpath);
80 }
81
82 #endif  // COMPILER_MINGW
83
84 string
85 PBD::canonical_path (const std::string& path)
86 {
87         char buf[PATH_MAX+1];
88
89         if (!realpath (path.c_str(), buf)) {
90                 return path;
91         }
92
93         return string (buf);
94 }
95
96 string
97 PBD::path_expand (string path)
98 {
99         if (path.empty()) {
100                 return path;
101         }
102
103         /* tilde expansion */
104
105         if (path[0] == '~') {
106                 if (path.length() == 1) {
107                         return Glib::get_home_dir();
108                 }
109
110                 if (path[1] == '/') {
111                         path.replace (0, 1, Glib::get_home_dir());
112                 } else {
113                         /* can't handle ~roger, so just leave it */
114                 }
115         }
116
117         /* now do $VAR substitution, since wordexp isn't reliable */
118
119         regex_t compiled_pattern;
120         const int nmatches = 100;
121         regmatch_t matches[nmatches];
122         
123         if (regcomp (&compiled_pattern, "\\$([a-zA-Z_][a-zA-Z0-9_]*|\\{[a-zA-Z_][a-zA-Z0-9_]*\\})", REG_EXTENDED)) {
124                 std::cerr << "bad regcomp\n";
125                 return path;
126         }
127
128         while (true) { 
129
130                 if (regexec (&compiled_pattern, path.c_str(), nmatches, matches, 0)) {
131                         break;
132                 }
133                 
134                 /* matches[0] gives the entire match */
135                 
136                 string match = path.substr (matches[0].rm_so, matches[0].rm_eo - matches[0].rm_so);
137                 
138                 /* try to get match from the environment */
139
140                 if (match[1] == '{') {
141                         /* ${FOO} form */
142                         match = match.substr (2, match.length() - 3);
143                 }
144
145                 char* matched_value = getenv (match.c_str());
146
147                 if (matched_value) {
148                         path.replace (matches[0].rm_so, matches[0].rm_eo - matches[0].rm_so, matched_value);
149                 } else {
150                         path.replace (matches[0].rm_so, matches[0].rm_eo - matches[0].rm_so, string());
151                 }
152
153                 /* go back and do it again with whatever remains after the
154                  * substitution 
155                  */
156         }
157
158         regfree (&compiled_pattern);
159
160         /* canonicalize */
161
162         return canonical_path (path);
163 }
164
165 string
166 PBD::search_path_expand (string path)
167 {
168         if (path.empty()) {
169                 return path;
170         }
171
172         vector<string> s;
173         vector<string> n;
174
175         split (path, s, ':');
176
177         for (vector<string>::iterator i = s.begin(); i != s.end(); ++i) {
178                 string exp = path_expand (*i);
179                 if (!exp.empty()) {
180                         n.push_back (exp);
181                 }
182         }
183
184         string r;
185
186         for (vector<string>::iterator i = n.begin(); i != n.end(); ++i) {
187                 if (!r.empty()) {
188                         r += ':';
189                 }
190                 r += *i;
191         }
192
193         return r;
194 }