Skip to content

Commit 2f68abd

Browse files
veractormtwebster
authored andcommitted
eel: Merge shell character escape functions
eel_str_escape_spaces and eel_str_escape_non_space_special_characters are complementary to each other, as they are both used to escape special characters for the shell. Merge them into etc_str_escape_shell_characters to simplify usage.
1 parent 2c0ed66 commit 2f68abd

File tree

3 files changed

+53
-67
lines changed

3 files changed

+53
-67
lines changed

eel/eel-string.c

Lines changed: 38 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ eel_str_double_underscores (const char *string)
7070
}
7171

7272
char *
73-
eel_str_escape_spaces (const char *string)
73+
eel_str_escape_shell_characters (const char *string)
7474
{
75-
int spaces;
75+
int escape_characters;
7676
const char *p;
7777
char *q;
7878
char *escaped;
@@ -81,61 +81,53 @@ eel_str_escape_spaces (const char *string)
8181
return NULL;
8282
}
8383

84-
spaces = 0;
84+
escape_characters = 0;
8585
for (p = string; *p != '\0'; p++) {
86-
spaces += (*p == ' ');
87-
}
86+
switch (*p) {
87+
case '\n':
88+
escape_characters += 2;
89+
break;
8890

89-
if (spaces == 0) {
90-
return g_strdup (string);
91-
}
91+
case ' ':
92+
case '\t':
93+
case '\'':
94+
case '"':
95+
case '\\':
96+
case '#':
97+
escape_characters++;
98+
break;
9299

93-
escaped = g_new (char, strlen (string) + spaces + 1);
94-
for (p = string, q = escaped; *p != '\0'; p++, q++) {
95-
/* Add an extra underscore. */
96-
if (*p == ' ') {
97-
*q++ = '\\';
100+
default:
101+
break;
98102
}
99-
*q = *p;
100-
}
101-
*q = '\0';
102-
103-
return escaped;
104-
}
105-
106-
char *
107-
eel_str_escape_non_space_special_characters (const char *string)
108-
{
109-
int special_characters;
110-
int newlines;
111-
const char *p;
112-
char *q;
113-
char *escaped;
114-
115-
if (string == NULL) {
116-
return NULL;
117103
}
118104

119-
special_characters = 0;
120-
newlines = 0;
121-
for (p = string; *p != '\0'; p++) {
122-
special_characters += (*p == '\'') || (*p == '\"') || (*p == '\t') || (*p == '\\') || (*p == '#') ;
123-
newlines += (*p == '\n');
124-
}
125-
126-
if (special_characters + newlines == 0) {
105+
if (escape_characters == 0) {
127106
return g_strdup (string);
128107
}
129108

130-
escaped = g_new (char, strlen (string) + special_characters + newlines * 2 + 1);
109+
escaped = g_new (char, strlen (string) + escape_characters + 1);
131110
for (p = string, q = escaped; *p != '\0'; p++, q++) {
132-
if ((*p == '\'') || (*p == '\"') || (*p == '\t') || (*p == '\\') || (*p == '#')) {
133-
*q++ = '\\';
134-
} else if (*p == '\n') {
135-
*q++ = '\'';
136-
*q++ = *p;
137-
*q = '\'';
111+
switch (*p) {
112+
case '\n':
113+
/* Quote newlines, as backslash-newline becomes nothing */
114+
q[0] = '\'';
115+
q[1] = '\n';
116+
q[2] = '\'';
117+
q += 3;
138118
continue;
119+
120+
case ' ':
121+
case '\t':
122+
case '\'':
123+
case '"':
124+
case '\\':
125+
case '#':
126+
*q++ = '\\';
127+
break;
128+
129+
default:
130+
break;
139131
}
140132
*q = *p;
141133
}

eel/eel-string.h

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,32 +39,30 @@
3939
/* NULL is allowed for all the str parameters to these functions. */
4040

4141
/* Escape function for '_' character. */
42-
char * eel_str_double_underscores (const char *str);
43-
/* Escape function for spaces */
44-
char * eel_str_escape_spaces (const char *str);
45-
/* Escape function for non-space special characters in a GLib shell context. */
46-
char * eel_str_escape_non_space_special_characters (const char *str);
42+
char * eel_str_double_underscores (const char *str);
43+
/* Escape function for special characters in a GLib shell context. */
44+
char * eel_str_escape_shell_characters (const char *str);
4745
/* Escape function for content within double quotes in a GLib shell context. */
48-
char * eel_str_escape_double_quoted_content (const char *str);
46+
char * eel_str_escape_double_quoted_content (const char *str);
4947
/* Capitalize a string */
50-
char * eel_str_capitalize (const char *str);
48+
char * eel_str_capitalize (const char *str);
5149

5250
/* Middle truncate a string to a maximum of truncate_length characters.
5351
* The resulting string will be truncated in the middle with a "..."
5452
* delimiter.
5553
*/
56-
char * eel_str_middle_truncate (const char *str,
57-
guint truncate_length);
54+
char * eel_str_middle_truncate (const char *str,
55+
guint truncate_length);
5856

5957

6058
/* Remove all characters after the passed-in substring. */
61-
char * eel_str_strip_substring_and_after (const char *str,
62-
const char *substring);
59+
char * eel_str_strip_substring_and_after (const char *str,
60+
const char *substring);
6361

6462
/* Replace all occurrences of substring with replacement. */
65-
char * eel_str_replace_substring (const char *str,
66-
const char *substring,
67-
const char *replacement);
63+
char * eel_str_replace_substring (const char *str,
64+
const char *substring,
65+
const char *replacement);
6866

6967
typedef char * eel_ref_str;
7068

libnemo-private/nemo-action.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,9 +1223,7 @@ get_path (NemoAction *action, NemoFile *file)
12231223
// Replace literal ' with a close ', a \', and an open '
12241224
ret = eel_str_replace_substring (orig, "'", "'\\''");
12251225
} else {
1226-
escaped = eel_str_escape_non_space_special_characters (orig);
1227-
ret = eel_str_escape_spaces (escaped);
1228-
g_free (escaped);
1226+
ret = eel_str_escape_shell_characters (orig);
12291227
}
12301228

12311229
g_free (orig);
@@ -1287,7 +1285,7 @@ get_device_path (NemoAction *action, NemoFile *file)
12871285
g_return_val_if_fail (mount != NULL, NULL);
12881286

12891287
GVolume *volume = g_mount_get_volume (mount);
1290-
gchar *ret, *escaped, *id;
1288+
gchar *ret, *id;
12911289

12921290
id = g_volume_get_identifier (volume, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
12931291

@@ -1297,9 +1295,7 @@ get_device_path (NemoAction *action, NemoFile *file)
12971295
// Replace literal ' with a close ', a \', and an open '
12981296
ret = eel_str_replace_substring (id, "'", "'\\''");
12991297
} else {
1300-
escaped = eel_str_escape_non_space_special_characters (id);
1301-
ret = eel_str_escape_spaces (escaped);
1302-
g_free (escaped);
1298+
ret = eel_str_escape_shell_characters (id);
13031299
}
13041300

13051301
g_free (id);

0 commit comments

Comments
 (0)