remove direct of realpath(2), replace with canonical_path() which is a no-op on windows
[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 string
37 PBD::canonical_path (const std::string& path)
38 {
39 #ifdef WIN32
40         return path;
41 #endif
42         char buf[PATH_MAX+1];
43
44         if (!realpath (path.c_str(), buf) && (errno != ENOENT)) {
45                 return path;
46         }
47
48         return string (buf);
49 }
50
51 string
52 PBD::path_expand (string path)
53 {
54         if (path.empty()) {
55                 return path;
56         }
57
58         /* tilde expansion */
59
60         if (path[0] == '~') {
61                 if (path.length() == 1) {
62                         return Glib::get_home_dir();
63                 }
64
65                 if (path[1] == '/') {
66                         path.replace (0, 1, Glib::get_home_dir());
67                 } else {
68                         /* can't handle ~roger, so just leave it */
69                 }
70         }
71
72         /* now do $VAR substitution, since wordexp isn't reliable */
73
74         regex_t compiled_pattern;
75         const int nmatches = 100;
76         regmatch_t matches[nmatches];
77         
78         if (regcomp (&compiled_pattern, "\\$([a-zA-Z_][a-zA-Z0-9_]*|\\{[a-zA-Z_][a-zA-Z0-9_]*\\})", REG_EXTENDED)) {
79                 std::cerr << "bad regcomp\n";
80                 return path;
81         }
82
83         while (true) { 
84
85                 if (regexec (&compiled_pattern, path.c_str(), nmatches, matches, 0)) {
86                         break;
87                 }
88                 
89                 /* matches[0] gives the entire match */
90                 
91                 string match = path.substr (matches[0].rm_so, matches[0].rm_eo - matches[0].rm_so);
92                 
93                 /* try to get match from the environment */
94
95                 if (match[1] == '{') {
96                         /* ${FOO} form */
97                         match = match.substr (2, match.length() - 3);
98                 }
99
100                 char* matched_value = getenv (match.c_str());
101
102                 if (matched_value) {
103                         path.replace (matches[0].rm_so, matches[0].rm_eo - matches[0].rm_so, matched_value);
104                 } else {
105                         path.replace (matches[0].rm_so, matches[0].rm_eo - matches[0].rm_so, string());
106                 }
107
108                 /* go back and do it again with whatever remains after the
109                  * substitution 
110                  */
111         }
112
113         regfree (&compiled_pattern);
114
115         /* canonicalize */
116
117         return canonical_path (path);
118 }
119
120 string
121 PBD::search_path_expand (string path)
122 {
123         if (path.empty()) {
124                 return path;
125         }
126
127         vector<string> s;
128         vector<string> n;
129
130         split (path, s, ':');
131
132         for (vector<string>::iterator i = s.begin(); i != s.end(); ++i) {
133                 string exp = path_expand (*i);
134                 if (!exp.empty()) {
135                         n.push_back (exp);
136                 }
137         }
138
139         string r;
140
141         for (vector<string>::iterator i = n.begin(); i != n.end(); ++i) {
142                 if (!r.empty()) {
143                         r += ':';
144                 }
145                 r += *i;
146         }
147
148         return r;
149 }