From 6d7f10ef2010a7b0c31712e06d7db66c3ab6a2ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Fri, 5 May 2023 20:28:11 +0200 Subject: [PATCH] core: fix execution of multiple commands separated by newline when there are no spaces For example typing this on core buffer: /t1 /t2 was not executing the two commands but sent the text to the buffer instead. This is because WeeChat thinks it's a path, and the newline should indicate it's not (like a space before the next slash: "/t1 /t2" is a command, not a path, but "/t1/t2" is considered a path). --- src/core/wee-string.c | 12 ++++++++---- tests/unit/core/test-core-string.cpp | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/core/wee-string.c b/src/core/wee-string.c index a6634ba7c..2f6a7b3e8 100644 --- a/src/core/wee-string.c +++ b/src/core/wee-string.c @@ -3909,7 +3909,7 @@ string_is_command_char (const char *string) const char * string_input_for_buffer (const char *string) { - char *pos_slash, *pos_space; + char *pos_slash, *pos_space, *pos_newline; const char *next_char; if (!string) @@ -3925,18 +3925,22 @@ string_input_for_buffer (const char *string) /* * special case if string starts with '/': to allow to paste a path line - * "/path/to/file.txt", we check if next '/' is after a space or not + * "/path/to/file.txt", we check if next '/' is after a space/newline + * or not */ if (string[0] == '/') { pos_slash = strchr (string + 1, '/'); pos_space = strchr (string + 1, ' '); + pos_newline = strchr (string + 1, '\n'); /* - * if there are no other '/' or if '/' is after first space, + * if there are no other '/' or if '/' is after first space/newline, * then it is a command, and return NULL */ - if (!pos_slash || (pos_space && pos_slash > pos_space)) + if (!pos_slash + || (pos_space && (pos_slash > pos_space)) + || (pos_newline && (pos_slash > pos_newline))) return NULL; return (string[1] == '/') ? string + 1 : string; diff --git a/tests/unit/core/test-core-string.cpp b/tests/unit/core/test-core-string.cpp index 87ab40dd2..a74e0cca3 100644 --- a/tests/unit/core/test-core-string.cpp +++ b/tests/unit/core/test-core-string.cpp @@ -2490,6 +2490,7 @@ TEST(CoreString, InputForBuffer) POINTERS_EQUAL(NULL, string_input_for_buffer ("/")); POINTERS_EQUAL(NULL, string_input_for_buffer ("/abc")); + /* not commands */ str = strdup (""); STRCMP_EQUAL(str, string_input_for_buffer (str)); free (str); @@ -2511,6 +2512,27 @@ TEST(CoreString, InputForBuffer) str = strdup ("//abc"); STRCMP_EQUAL(str + 1, string_input_for_buffer (str)); free (str); + str = strdup ("/abc/def /ghi"); + STRCMP_EQUAL(str, string_input_for_buffer (str)); + free (str); + str = strdup ("/abc/def /ghi"); + STRCMP_EQUAL(str, string_input_for_buffer (str)); + free (str); + + /* commands */ + POINTERS_EQUAL(NULL, string_input_for_buffer (NULL)); + str = strdup ("/"); + POINTERS_EQUAL(NULL, string_input_for_buffer (str)); + free (str); + str = strdup ("/abc"); + POINTERS_EQUAL(NULL, string_input_for_buffer (str)); + free (str); + str = strdup ("/abc /def"); + POINTERS_EQUAL(NULL, string_input_for_buffer (str)); + free (str); + str = strdup ("/abc\n/def"); + POINTERS_EQUAL(NULL, string_input_for_buffer (str)); + free (str); /* test with custom command chars */ config_file_option_set (config_look_command_chars, "öï", 1);