Skip to content

Commit d876c70

Browse files
escape backslashes on output
1 parent 571dd51 commit d876c70

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

shell.cpp

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,36 @@ char *shell_utf8_to_native(const char *ubuf, int ubytes, int *len)
609609

610610
#endif
611611

612+
char *shell_escape_backslashes(const char *str)
613+
{
614+
// count the backslashes
615+
t_ptr_size ct = 0;
616+
const char *c = str;
617+
char *newstr = NULL;
618+
char *n;
619+
t_bool hasslash = false;
620+
621+
while (c && *c) {
622+
if (*c++ == '\\') {
623+
ct++;
624+
hasslash = true;
625+
}
626+
ct++;
627+
}
628+
if (!hasslash) return NULL;
629+
newstr = (char *)sysmem_newptr(ct + 1);
630+
c = str;
631+
n = newstr;
632+
while (c && *c) {
633+
if (*c == '\\') {
634+
*n++ = '\\';
635+
}
636+
*n++ = *c++;
637+
}
638+
*n = '\0';
639+
return newstr;
640+
}
641+
612642
Boolean shell_readline(t_shell *x)
613643
{
614644
char stream[MAX_MESSAGELEN];
@@ -619,7 +649,9 @@ Boolean shell_readline(t_shell *x)
619649
t_atom a;
620650
char *readstream = stream;
621651
int charsize = 1;
622-
652+
char *escape;
653+
t_string *str;
654+
623655
#ifdef WIN_VERSION
624656
WCHAR *unicodestream = NULL;
625657

@@ -665,7 +697,14 @@ Boolean shell_readline(t_shell *x)
665697
sysmem_copyptr(lp2, line, (long)cbytes);
666698
line[cbytes] = '\0';
667699
lp2 = lp1 + 1;
668-
atom_setobj(&a, string_new(line));
700+
701+
escape = shell_escape_backslashes(line);
702+
str = string_new(escape ? escape : line);
703+
if (escape) {
704+
sysmem_freeptr(escape);
705+
escape = NULL;
706+
}
707+
atom_setobj(&a, str);
669708
defer(x, (method)shell_output, NULL, 1, &a);
670709
}
671710
if (lp2 && *lp2) { // there's an incomplete line, rewrite it to the front of the
@@ -697,7 +736,13 @@ Boolean shell_readline(t_shell *x)
697736
}
698737
}
699738
if (offset) {
700-
atom_setobj(&a, string_new(line));
739+
escape = shell_escape_backslashes(line);
740+
str = string_new(escape ? escape : line);
741+
if (escape) {
742+
sysmem_freeptr(escape);
743+
escape = NULL;
744+
}
745+
atom_setobj(&a, str);
701746
defer(x, (method)shell_output, NULL, 1, &a);
702747
}
703748
#ifdef WIN_VERSION

0 commit comments

Comments
 (0)