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