diff options
| author | Warner Losh <imp@FreeBSD.org> | 2018-06-27 23:44:37 +0000 |
|---|---|---|
| committer | Warner Losh <imp@FreeBSD.org> | 2018-06-27 23:44:37 +0000 |
| commit | 54aa4076257f72b962d71b447bcd028de2b43a66 (patch) | |
| tree | aa4821ffd96ed0785a923a69d8245a537e13b6d1 /sbin/devd/devd.cc | |
| parent | 37e29448ea19a6e2d7933e9f876d5f1e962dcf75 (diff) | |
Notes
Diffstat (limited to 'sbin/devd/devd.cc')
| -rw-r--r-- | sbin/devd/devd.cc | 29 |
1 files changed, 26 insertions, 3 deletions
diff --git a/sbin/devd/devd.cc b/sbin/devd/devd.cc index f57f41eaa7ea..bb7195cafee6 100644 --- a/sbin/devd/devd.cc +++ b/sbin/devd/devd.cc @@ -636,6 +636,30 @@ config::is_id_char(char ch) const ch == '-')); } +string +config::shell_quote(const string &s) +{ + string buffer; + + /* + * Enclose the string in $' ' with escapes for ' and / characters making + * it one argument and ensuring the shell won't be affected by its + * usual list of candidates. + */ + buffer.reserve(s.length() * 3 / 2); + buffer += '$'; + buffer += '\''; + for (const char &c : s) { + if (c == '\'' || c == '\\') { + buffer += '\\'; + } + buffer += c; + } + buffer += '\''; + + return buffer; +} + void config::expand_one(const char *&src, string &dst) { @@ -650,8 +674,7 @@ config::expand_one(const char *&src, string &dst) } // $(foo) -> $(foo) - // Not sure if I want to support this or not, so for now we just pass - // it through. + // This is the escape hatch for passing down shell subcommands if (*src == '(') { dst += '$'; count = 1; @@ -677,7 +700,7 @@ config::expand_one(const char *&src, string &dst) do { buffer += *src++; } while (is_id_char(*src)); - dst.append(get_variable(buffer)); + dst.append(shell_quote(get_variable(buffer))); } const string |
