fix endless-loop-at-shutdown bug now that abstract UI request buffers are used
[ardour.git] / libs / pbd / pathexpand.cc
1 /*
2     Copyright (C) 2013-2014 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/fileutils.h>
29 #include <glibmm/miscutils.h>
30
31 #include "pbd/compose.h"
32 #include "pbd/debug.h"
33 #include "pbd/pathexpand.h"
34 #include "pbd/strsplit.h"
35 #include "pbd/tokenizer.h"
36
37 using std::string;
38 using std::vector;
39
40 #ifdef COMPILER_MINGW
41
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <glibmm.h>
45
46 /****************************************************************
47  * Emulate POSIX realpath() using Win32 _fullpath() since realpath()
48  * is not available.
49  *
50  * Returns:
51  *    On Success: A pointer to the resolved (absolute) path
52  *    On Failure: 0 (NULL)
53  */
54
55 static char*
56 realpath (const char *original_path, char resolved_path[_MAX_PATH+1])
57 {
58         char *rpath = 0;
59         bool bIsSymLink = false; // We'll probably need to test the incoming path
60                                  // to find out if it points to a Windows shortcut
61                                  // (or a hard link) and set this appropriately.
62
63         if (bIsSymLink) {
64                 // At the moment I'm not sure if Windows '_fullpath()' is directly
65                 // equivalent to POSIX 'realpath()' - in as much as the latter will
66                 // resolve the supplied path if it happens to point to a symbolic
67                 // link ('_fullpath()' probably DOESN'T do this but I'm not really
68                 // sure if Ardour needs such functionality anyway). Therefore we'll
69                 // possibly need to add that functionality here at a later date.
70         } else {
71                 char temp[(_MAX_PATH+1)*6]; // Allow for maximum length of a path in wchar characters
72
73                 // POSIX 'realpath()' requires that the buffer size is at
74                 // least PATH_MAX+1, so assume that the user knew this !!
75
76                 rpath = _fullpath (temp, Glib::locale_from_utf8 (original_path).c_str(), _MAX_PATH);
77
78                 if (0 != rpath) {
79                         snprintf (resolved_path, _MAX_PATH+1, "%s", Glib::locale_to_utf8 (temp).c_str());
80                 }
81
82         }
83
84         return (rpath);
85 }
86
87 #endif  // COMPILER_MINGW
88
89 string
90 PBD::canonical_path (const std::string& path)
91 {
92         char buf[PATH_MAX+1];
93
94         if (realpath (path.c_str(), buf) == NULL) {
95                 DEBUG_TRACE (DEBUG::FileUtils,
96                                 string_compose("PBD::canonical_path: Unable to resolve %1: %2\n", path, g_strerror(errno)));
97                 return path;
98         }
99
100         DEBUG_TRACE (DEBUG::FileUtils,
101                         string_compose("PBD::canonical_path %1 resolved to: %2\n", path, string(buf)));
102
103         return string (buf);
104 }
105
106 string
107 PBD::path_expand (string path)
108 {
109         if (path.empty()) {
110                 return path;
111         }
112
113         /* tilde expansion */
114
115         if (path[0] == '~') {
116                 if (path.length() == 1) {
117                         return Glib::get_home_dir();
118                 }
119
120                 if (path[1] == '/') {
121                         path.replace (0, 1, Glib::get_home_dir());
122                 } else {
123                         /* can't handle ~roger, so just leave it */
124                 }
125         }
126
127         /* now do $VAR substitution, since wordexp isn't reliable */
128
129         regex_t compiled_pattern;
130         const int nmatches = 100;
131         regmatch_t matches[nmatches];
132
133         if (regcomp (&compiled_pattern, "\\$([a-zA-Z_][a-zA-Z0-9_]*|\\{[a-zA-Z_][a-zA-Z0-9_]*\\})", REG_EXTENDED)) {
134                 std::cerr << "bad regcomp\n";
135                 return path;
136         }
137
138         while (true) {
139
140                 if (regexec (&compiled_pattern, path.c_str(), nmatches, matches, 0)) {
141                         break;
142                 }
143
144                 /* matches[0] gives the entire match */
145
146                 string match = path.substr (matches[0].rm_so, matches[0].rm_eo - matches[0].rm_so);
147
148                 /* try to get match from the environment */
149
150                 if (match[1] == '{') {
151                         /* ${FOO} form */
152                         match = match.substr (2, match.length() - 3);
153                 }
154
155                 char* matched_value = getenv (match.c_str());
156
157                 if (matched_value) {
158                         path.replace (matches[0].rm_so, matches[0].rm_eo - matches[0].rm_so, matched_value);
159                 } else {
160                         path.replace (matches[0].rm_so, matches[0].rm_eo - matches[0].rm_so, string());
161                 }
162
163                 /* go back and do it again with whatever remains after the
164                  * substitution
165                  */
166         }
167
168         regfree (&compiled_pattern);
169
170         /* canonicalize */
171
172         return canonical_path (path);
173 }
174
175 string
176 PBD::search_path_expand (string path)
177 {
178         if (path.empty()) {
179                 return path;
180         }
181
182         vector<string> s;
183         vector<string> n;
184
185         split (path, s, G_SEARCHPATH_SEPARATOR);
186
187         for (vector<string>::iterator i = s.begin(); i != s.end(); ++i) {
188                 string exp = path_expand (*i);
189                 if (!exp.empty()) {
190                         n.push_back (exp);
191                 }
192         }
193
194         string r;
195
196         for (vector<string>::iterator i = n.begin(); i != n.end(); ++i) {
197                 if (!r.empty()) {
198                         r += G_SEARCHPATH_SEPARATOR;
199                 }
200                 r += *i;
201         }
202
203         return r;
204 }
205
206 std::vector <std::string>
207 PBD::parse_path(std::string path, bool check_if_exists)
208 {
209         vector <std::string> pathlist;
210         vector <std::string> tmp;
211         PBD::tokenize (path, string(G_SEARCHPATH_SEPARATOR_S), std::back_inserter (tmp));
212
213         for(vector<std::string>::const_iterator i = tmp.begin(); i != tmp.end(); ++i) {
214                 if ((*i).empty()) continue;
215                 std::string dir;
216 #ifndef PLATFORM_WINDOWS
217                 if ((*i).substr(0,1) == "~") {
218                         dir = Glib::build_filename(Glib::get_home_dir(), (*i).substr(1));
219                 }
220                 else
221 #endif
222                 {
223                         dir = *i;
224                 }
225                 if (!check_if_exists || Glib::file_test (dir, Glib::FILE_TEST_IS_DIR)) {
226                         pathlist.push_back(dir);
227                 }
228         }
229   return pathlist;
230 }