chiark
/
gitweb
/
~ianmdlvl
/
elogind.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
tests: add tests for environment serialization
[elogind.git]
/
src
/
basic
/
escape.c
diff --git
a/src/basic/escape.c
b/src/basic/escape.c
index 127ca2ecb2ced04c1d5a81177f7f8a6e536edbd4..d43bdaebfc9922e0294a00a8b8dc4e2e45b3193c 100644
(file)
--- a/
src/basic/escape.c
+++ b/
src/basic/escape.c
@@
-333,7
+333,7
@@
int cunescape_length_with_prefix(const char *s, size_t length, const char *prefi
assert(remaining > 0);
if (*f != '\\') {
assert(remaining > 0);
if (*f != '\\') {
- /* A literal
literal
, copy verbatim */
+ /* A literal, copy verbatim */
*(t++) = *f;
continue;
}
*(t++) = *f;
continue;
}
@@
-442,10
+442,16
@@
char *octescape(const char *s, size_t len) {
}
}
-static char *strcpy_backslash_escaped(char *t, const char *s, const char *bad) {
+static char *strcpy_backslash_escaped(char *t, const char *s, const char *bad
, bool escape_tab_nl
) {
assert(bad);
for (; *s; s++) {
assert(bad);
for (; *s; s++) {
+ if (escape_tab_nl && IN_SET(*s, '\n', '\t')) {
+ *(t++) = '\\';
+ *(t++) = *s == '\n' ? 'n' : 't';
+ continue;
+ }
+
if (*s == '\\' || strchr(bad, *s))
*(t++) = '\\';
if (*s == '\\' || strchr(bad, *s))
*(t++) = '\\';
@@
-462,20
+468,21
@@
char *shell_escape(const char *s, const char *bad) {
if (!r)
return NULL;
if (!r)
return NULL;
- t = strcpy_backslash_escaped(r, s, bad);
+ t = strcpy_backslash_escaped(r, s, bad
, false
);
*t = 0;
return r;
}
*t = 0;
return r;
}
-char
*shell_maybe_quote(const char *s
) {
+char
* shell_maybe_quote(const char *s, EscapeStyle style
) {
const char *p;
char *r, *t;
assert(s);
const char *p;
char *r, *t;
assert(s);
- /* Encloses a string in double quotes if necessary to make it
- * OK as shell string. */
+ /* Encloses a string in quotes if necessary to make it OK as a shell
+ * string. Note that we treat benign UTF-8 characters as needing
+ * escaping too, but that should be OK. */
for (p = s; *p; p++)
if (*p <= ' ' ||
for (p = s; *p; p++)
if (*p <= ' ' ||
@@
-486,17
+493,30
@@
char *shell_maybe_quote(const char *s) {
if (!*p)
return strdup(s);
if (!*p)
return strdup(s);
- r = new(char,
1+strlen(s)*2+1+
1);
+ r = new(char,
(style == ESCAPE_POSIX) + 1 + strlen(s)*2 + 1 +
1);
if (!r)
return NULL;
t = r;
if (!r)
return NULL;
t = r;
- *(t++) = '"';
+ if (style == ESCAPE_BACKSLASH)
+ *(t++) = '"';
+ else if (style == ESCAPE_POSIX) {
+ *(t++) = '$';
+ *(t++) = '\'';
+ } else
+ assert_not_reached("Bad EscapeStyle");
+
t = mempcpy(t, s, p - s);
t = mempcpy(t, s, p - s);
- t = strcpy_backslash_escaped(t, p, SHELL_NEED_ESCAPE);
+ if (style == ESCAPE_BACKSLASH)
+ t = strcpy_backslash_escaped(t, p, SHELL_NEED_ESCAPE, false);
+ else
+ t = strcpy_backslash_escaped(t, p, SHELL_NEED_ESCAPE_POSIX, true);
- *(t++)= '"';
+ if (style == ESCAPE_BACKSLASH)
+ *(t++) = '"';
+ else
+ *(t++) = '\'';
*t = 0;
return r;
*t = 0;
return r;