From 66231b1ac9e3d9d960c04e7b176036a12a59813d Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Wed, 12 Oct 2011 13:53:32 +0400 Subject: [PATCH 1/6] Ticket #2451: allow do not variable substite in the command line. Environment variable should not be substituted if escaping of dollar sign is used: \$VAR. Initial steps: (do_cd): changed return type from int to gboolean. (examine_cd): likewise and some cleanup. Signed-off-by: Andrew Borodin --- src/filemanager/command.c | 47 ++++++++++++++++++++++----------------- src/main.c | 4 ++-- src/main.h | 2 +- 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/src/filemanager/command.c b/src/filemanager/command.c index 1b8eb1897..87ad58ee2 100644 --- a/src/filemanager/command.c +++ b/src/filemanager/command.c @@ -80,29 +80,32 @@ WInput *cmdline; * they want the behavior they are used to in the shell. */ -static int +static gboolean examine_cd (const char *_path) { - int result, qlen; + gboolean result; + size_t qlen; char *path_tilde, *path; char *p, *q, *r, *s, c; - const char *t; /* Tilde expansion */ path = strutils_shell_unescape (_path); path_tilde = tilde_expand (path); + g_free (path); /* Leave space for further expansion */ qlen = strlen (path_tilde) + MC_MAXPATHLEN; q = g_malloc (qlen); /* Variable expansion */ - for (p = path_tilde, r = q; *p && r < q + MC_MAXPATHLEN;) + for (p = path_tilde, r = q; *p != '\0' && r < q + MC_MAXPATHLEN;) { if (*p != '$' || (p[1] == '[' || p[1] == '(')) *(r++) = *(p++); else { + const char *t; + p++; if (*p == '{') { @@ -114,9 +117,9 @@ examine_cd (const char *_path) if (s == NULL) s = strchr (p, PATH_SEP); if (s == NULL) - s = strchr (p, 0); + s = strchr (p, '\0'); c = *s; - *s = 0; + *s = '\0'; t = getenv (p); *s = c; if (t == NULL) @@ -130,38 +133,42 @@ examine_cd (const char *_path) if (r + strlen (t) < q + MC_MAXPATHLEN) { strcpy (r, t); - r = strchr (r, 0); + r = strchr (r, '\0'); } + p = s; if (*s == '}') - p = s + 1; - else - p = s; + p++; } } } - *r = 0; + + g_free (path_tilde); + + *r = '\0'; result = do_cd (q, cd_parse_command); /* CDPATH handling */ - if (*q != PATH_SEP && !result) + if (!result && *q != PATH_SEP) { - char *const cdpath = g_strdup (getenv ("CDPATH")); + char *cdpath; + + cdpath = g_strdup (getenv ("CDPATH")); p = cdpath; if (p == NULL) - c = 0; + c = '\0'; else c = ':'; while (!result && c == ':') { s = strchr (p, ':'); if (s == NULL) - s = strchr (p, 0); + s = strchr (p, '\0'); c = *s; - *s = 0; - if (*p) + *s = '\0'; + if (*p != '\0') { - r = concat_dir_and_file (p, q); + r = mc_build_filename (p, q, (char *) NULL); result = do_cd (r, cd_parse_command); g_free (r); } @@ -170,9 +177,9 @@ examine_cd (const char *_path) } g_free (cdpath); } + g_free (q); - g_free (path_tilde); - g_free (path); + return result; } diff --git a/src/main.c b/src/main.c index 0346acdc5..58a03f681 100644 --- a/src/main.c +++ b/src/main.c @@ -258,7 +258,7 @@ init_sigchld (void) /*** public functions ****************************************************************************/ /* --------------------------------------------------------------------------------------------- */ -int +gboolean do_cd (const char *new_dir, enum cd_enum exact) { gboolean res; @@ -284,7 +284,7 @@ do_cd (const char *new_dir, enum cd_enum exact) } #endif /* HAVE_CHARSET */ - return res ? 1 : 0; + return res; } /* --------------------------------------------------------------------------------------------- */ diff --git a/src/main.h b/src/main.h index 6f0e9f8b1..b24ff2759 100644 --- a/src/main.h +++ b/src/main.h @@ -89,7 +89,7 @@ gboolean do_load_prompt (void); int load_prompt (int fd, void *unused); #endif -int do_cd (const char *new_dir, enum cd_enum cd_type); +gboolean do_cd (const char *new_dir, enum cd_enum cd_type); void update_xterm_title_path (void); /*** inline functions ****************************************************************************/ From 9f898d76dd503483ca06da88d80f4e1f597aab57 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Wed, 12 Oct 2011 14:04:05 +0400 Subject: [PATCH 2/6] (strutils_unescape): allow disable shell variable substitution. Signed-off-by: Andrew Borodin --- lib/strutil/strescape.c | 54 +++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/lib/strutil/strescape.c b/lib/strutil/strescape.c index 8068fdaf3..e99191c94 100644 --- a/lib/strutil/strescape.c +++ b/lib/strutil/strescape.c @@ -105,39 +105,51 @@ strutils_unescape (const char *src, gsize src_len, const char *unescaped_chars, if (*src == '\0') return strdup (""); - ret = g_string_new (""); + ret = g_string_sized_new (16); - if (src_len == (gsize) - 1) + if (src_len == (gsize) (-1)) src_len = strlen (src); + src_len--; - for (curr_index = 0; curr_index < src_len - 1; curr_index++) + for (curr_index = 0; curr_index < src_len; curr_index++) { if (src[curr_index] != '\\') { g_string_append_c (ret, src[curr_index]); continue; } + curr_index++; - if (unescape_non_printable) + + if (unescaped_chars == ESCAPE_SHELL_CHARS && src[curr_index] == '$') { - switch (src[curr_index]) - { - case 'n': - g_string_append_c (ret, '\n'); - continue; - case 't': - g_string_append_c (ret, '\t'); - continue; - case 'b': - g_string_append_c (ret, '\b'); - continue; - case '0': - g_string_append (ret, '\0'); - continue; - } - } - if (strchr (unescaped_chars, (int) src[curr_index]) == NULL) + /* special case: \$ is used to disallow variable substitution */ g_string_append_c (ret, '\\'); + } + else + { + if (unescape_non_printable) + { + switch (src[curr_index]) + { + case 'n': + g_string_append_c (ret, '\n'); + continue; + case 't': + g_string_append_c (ret, '\t'); + continue; + case 'b': + g_string_append_c (ret, '\b'); + continue; + case '0': + g_string_append_c (ret, '\0'); + continue; + } + } + + if (strchr (unescaped_chars, (int) src[curr_index]) == NULL) + g_string_append_c (ret, '\\'); + } g_string_append_c (ret, src[curr_index]); } From 0a5065cb848b82ccd6ad847bce9ae4066707120c Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Wed, 12 Oct 2011 16:16:43 +0400 Subject: [PATCH 3/6] Support of disable of shell variables substitution in the command line. Signed-off-by: Andrew Borodin --- src/filemanager/command.c | 87 +++++++++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 30 deletions(-) diff --git a/src/filemanager/command.c b/src/filemanager/command.c index 87ad58ee2..9f969814e 100644 --- a/src/filemanager/command.c +++ b/src/filemanager/command.c @@ -83,6 +83,9 @@ WInput *cmdline; static gboolean examine_cd (const char *_path) { + typedef enum { copy_sym, subst_var } state_t; + + state_t state = copy_sym; gboolean result; size_t qlen; char *path_tilde, *path; @@ -100,44 +103,68 @@ examine_cd (const char *_path) /* Variable expansion */ for (p = path_tilde, r = q; *p != '\0' && r < q + MC_MAXPATHLEN;) { - if (*p != '$' || (p[1] == '[' || p[1] == '(')) - *(r++) = *(p++); - else + switch (state) { - const char *t; - - p++; - if (*p == '{') + case copy_sym: + if (p[0] == '\\' && p[1] == '$') { + /* skip backslash */ p++; - s = strchr (p, '}'); + /* copy dollar */ + *(r++) = *(p++); } + else if (p[0] != '$' || p[1] == '[' || p[1] == '(') + *(r++) = *(p++); else - s = NULL; - if (s == NULL) - s = strchr (p, PATH_SEP); - if (s == NULL) - s = strchr (p, '\0'); - c = *s; - *s = '\0'; - t = getenv (p); - *s = c; - if (t == NULL) + state = subst_var; + break; + + case subst_var: { - *(r++) = '$'; - if (*(p - 1) != '$') - *(r++) = '{'; - } - else - { - if (r + strlen (t) < q + MC_MAXPATHLEN) + const char *t; + + /* skip dollar */ + p++; + + if (p[0] != '{') + s = NULL; + else { - strcpy (r, t); - r = strchr (r, '\0'); - } - p = s; - if (*s == '}') p++; + s = strchr (p, '}'); + } + if (s == NULL) + s = strchr (p, PATH_SEP); + if (s == NULL) + s = strchr (p, '\0'); + c = *s; + *s = '\0'; + t = getenv (p); + *s = c; + if (t == NULL) + { + *(r++) = '$'; + if (p[-1] != '$') + *(r++) = '{'; + } + else + { + size_t tlen; + + tlen = strlen (t); + + if (r + tlen < q + MC_MAXPATHLEN) + { + strncpy (r, t, tlen + 1); + r += tlen; + } + p = s; + if (*s == '}') + p++; + } + + state = copy_sym; + break; } } } From 5531b25dd752c95ec3983cd0ca71b8ff194a5396 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Wed, 12 Oct 2011 16:41:58 +0400 Subject: [PATCH 4/6] (examine_cd): split in two functions to be unit test friendly. Signed-off-by: Andrew Borodin --- src/filemanager/command.c | 63 ++++++++++++++++++++++++++++----------- 1 file changed, 45 insertions(+), 18 deletions(-) diff --git a/src/filemanager/command.c b/src/filemanager/command.c index 9f969814e..eed9affe1 100644 --- a/src/filemanager/command.c +++ b/src/filemanager/command.c @@ -80,16 +80,16 @@ WInput *cmdline; * they want the behavior they are used to in the shell. */ -static gboolean +static char * examine_cd (const char *_path) { typedef enum { copy_sym, subst_var } state_t; state_t state = copy_sym; - gboolean result; + char *q; size_t qlen; char *path_tilde, *path; - char *p, *q, *r, *s, c; + char *p, *r; /* Tilde expansion */ path = strutils_shell_unescape (_path); @@ -103,6 +103,7 @@ examine_cd (const char *_path) /* Variable expansion */ for (p = path_tilde, r = q; *p != '\0' && r < q + MC_MAXPATHLEN;) { + switch (state) { case copy_sym: @@ -121,6 +122,8 @@ examine_cd (const char *_path) case subst_var: { + char *s; + char c; const char *t; /* skip dollar */ @@ -173,21 +176,31 @@ examine_cd (const char *_path) *r = '\0'; - result = do_cd (q, cd_parse_command); + return q; +} + +/* --------------------------------------------------------------------------------------------- */ + +/* CDPATH handling */ +static gboolean +handle_cdpath (const char *path) +{ + gboolean result = FALSE; /* CDPATH handling */ - if (!result && *q != PATH_SEP) + if (*path != PATH_SEP) { - char *cdpath; + char *cdpath, *p; + char c; cdpath = g_strdup (getenv ("CDPATH")); p = cdpath; - if (p == NULL) - c = '\0'; - else - c = ':'; + c = (p == NULL) ? '\0' : ':'; + while (!result && c == ':') { + char *s; + s = strchr (p, ':'); if (s == NULL) s = strchr (p, '\0'); @@ -195,7 +208,9 @@ examine_cd (const char *_path) *s = '\0'; if (*p != '\0') { - r = mc_build_filename (p, q, (char *) NULL); + char *r; + + r = mc_build_filename (p, path, (char *) NULL); result = do_cd (r, cd_parse_command); g_free (r); } @@ -205,8 +220,6 @@ examine_cd (const char *_path) g_free (cdpath); } - g_free (q); - return result; } @@ -393,12 +406,26 @@ do_cd_command (char *orig_cmd) g_free (new); } } - else if (!examine_cd (&cmd[operand_pos])) + else { - char *d = strip_password (g_strdup (&cmd[operand_pos]), 1); - message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\"\n%s"), d, unix_error_string (errno)); - g_free (d); - return; + char *path; + gboolean ok; + + path = examine_cd (&cmd[operand_pos]); + ok = do_cd (path, cd_parse_command); + if (!ok) + ok = handle_cdpath (path); + + if (!ok) + { + char *d; + + d = strip_password (path, 1); + message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\"\n%s"), d, + unix_error_string (errno)); + } + + g_free (path); } } From ec476d0b8861619f9d7a56556cc96bfca00773ef Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Wed, 12 Oct 2011 17:08:50 +0400 Subject: [PATCH 5/6] Added tests for examine_cd() function. Signed-off-by: Andrew Borodin --- configure.ac | 2 + tests/Makefile.am | 2 +- tests/src/Makefile.am | 1 + tests/src/filemanager/Makefile.am | 10 ++ tests/src/filemanager/examine_cd.c | 186 +++++++++++++++++++++++++++++ 5 files changed, 200 insertions(+), 1 deletion(-) create mode 100644 tests/src/Makefile.am create mode 100644 tests/src/filemanager/Makefile.am create mode 100644 tests/src/filemanager/examine_cd.c diff --git a/configure.ac b/configure.ac index f1f492694..852b53d50 100644 --- a/configure.ac +++ b/configure.ac @@ -624,6 +624,8 @@ tests/lib/Makefile tests/lib/mcconfig/Makefile tests/lib/search/Makefile tests/lib/vfs/Makefile +tests/src/Makefile +tests/src/filemanager/Makefile ]) fi diff --git a/tests/Makefile.am b/tests/Makefile.am index 0262e4ddc..45996340f 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1 +1 @@ -SUBDIRS = lib +SUBDIRS = lib src diff --git a/tests/src/Makefile.am b/tests/src/Makefile.am new file mode 100644 index 000000000..c2c82a497 --- /dev/null +++ b/tests/src/Makefile.am @@ -0,0 +1 @@ +SUBDIRS = filemanager diff --git a/tests/src/filemanager/Makefile.am b/tests/src/filemanager/Makefile.am new file mode 100644 index 000000000..0fafa1c19 --- /dev/null +++ b/tests/src/filemanager/Makefile.am @@ -0,0 +1,10 @@ +AM_CFLAGS = $(GLIB_CFLAGS) -I$(top_srcdir) @CHECK_CFLAGS@ +LIBS=@CHECK_LIBS@ $(top_builddir)/lib/libmc.la + +TESTS = \ + examine_cd + +check_PROGRAMS = $(TESTS) + +examine_cd_SOURCES = \ + examine_cd.c diff --git a/tests/src/filemanager/examine_cd.c b/tests/src/filemanager/examine_cd.c new file mode 100644 index 000000000..4e758f8ab --- /dev/null +++ b/tests/src/filemanager/examine_cd.c @@ -0,0 +1,186 @@ +/* + src/filemanager - examine_cd() function testing + + Copyright (C) 2012 + The Free Software Foundation, Inc. + + Written by: + Andrew Borodin , 2012 + + This file is part of the Midnight Commander. + + The Midnight Commander is free software: you can redistribute it + and/or modify it under the terms of the GNU General Public License as + published by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + The Midnight Commander is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#define TEST_SUITE_NAME "/src/filemanager" + +#include + +#include + +#include + +#include "src/filemanager/layout.h" +#include "src/filemanager/midnight.h" +#include "src/filemanager/tree.h" +#ifdef HAVE_SUBSHELL_SUPPORT +#include "src/subshell.h" +#endif /* HAVE_SUBSHELL_SUPPORT */ + +#include "src/filemanager/command.c" + +/* --------------------------------------------------------------------------------------------- */ + +gboolean command_prompt = FALSE; +#ifdef HAVE_SUBSHELL_SUPPORT +enum subshell_state_enum subshell_state = INACTIVE; +#endif /* HAVE_SUBSHELL_SUPPORT */ +int quit = 0; +WPanel *current_panel = NULL; + +panel_view_mode_t +get_current_type (void) +{ + return view_listing; +} + +gboolean +do_cd (const char *new_dir, enum cd_enum cd_type) +{ + (void) new_dir; + (void) cd_type; + + return TRUE; +} + +gboolean +quiet_quit_cmd (void) +{ + return FALSE; +} + +char * +expand_format (struct WEdit *edit_widget, char c, gboolean do_quote) +{ + (void) edit_widget; + (void) c; + (void) do_quote; + + return NULL; +} + +#ifdef HAVE_SUBSHELL_SUPPORT +void +init_subshell (void) +{ +} + +gboolean +do_load_prompt (void) +{ + return TRUE; +} +#endif /* HAVE_SUBSHELL_SUPPORT */ + +void +shell_execute (const char *command, int flags) +{ + (void) command; + (void) flags; +} + +void +sync_tree (const char *pathname) +{ + (void) pathname; +} + +/* --------------------------------------------------------------------------------------------- */ + +static void +setup (void) +{ +} + +static void +teardown (void) +{ +} + +/* --------------------------------------------------------------------------------------------- */ +#define check_examine_cd(input, etalon) \ +{ \ + result = examine_cd (input); \ + fail_unless (strcmp (result, etalon) == 0, \ + "\ninput (%s)\nactial (%s) not equal to\netalon (%s)", input, result, etalon); \ + g_free (result); \ +} + +START_TEST (test_examine_cd) +{ + char *result; + + g_setenv ("AAA", "aaa", TRUE); + + check_examine_cd ("/test/path", "/test/path"); + + check_examine_cd ("/test/path/$AAA", "/test/path/aaa"); + check_examine_cd ("/test/path/$AAA/test2", "/test/path/aaa/test2"); + check_examine_cd ("/test/path/test1$AAA/test2", "/test/path/test1aaa/test2"); + + check_examine_cd ("/test/path/${AAA}", "/test/path/aaa"); + check_examine_cd ("/test/path/${AAA}/test2", "/test/path/aaa/test2"); + check_examine_cd ("/test/path/${AAA}test2", "/test/path/aaatest2"); + check_examine_cd ("/test/path/test1${AAA}", "/test/path/test1aaa"); + check_examine_cd ("/test/path/test1${AAA}test2", "/test/path/test1aaatest2"); + + check_examine_cd ("/test/path/\\$AAA", "/test/path/$AAA"); + check_examine_cd ("/test/path/\\$AAA/test2", "/test/path/$AAA/test2"); + check_examine_cd ("/test/path/test1\\$AAA", "/test/path/test1$AAA"); + + check_examine_cd ("/test/path/\\${AAA}", "/test/path/${AAA}"); + check_examine_cd ("/test/path/\\${AAA}/test2", "/test/path/${AAA}/test2"); + check_examine_cd ("/test/path/\\${AAA}test2", "/test/path/${AAA}test2"); + check_examine_cd ("/test/path/test1\\${AAA}test2", "/test/path/test1${AAA}test2"); +} +END_TEST + +/* --------------------------------------------------------------------------------------------- */ + +int +main (void) +{ + int number_failed; + + Suite *s = suite_create (TEST_SUITE_NAME); + TCase *tc_core = tcase_create ("Core"); + SRunner *sr; + + tcase_add_checked_fixture (tc_core, setup, teardown); + + /* Add new tests here: *************** */ + tcase_add_test (tc_core, test_examine_cd); + /* *********************************** */ + + suite_add_tcase (s, tc_core); + sr = srunner_create (s); + srunner_set_log (sr, "examine_cd.log"); + srunner_run_all (sr, CK_NORMAL); + number_failed = srunner_ntests_failed (sr); + srunner_free (sr); + + return (number_failed == 0) ? 0 : 1; +} + +/* --------------------------------------------------------------------------------------------- */ From bff2736906ef4c25ae9bb641f7642a61bca36ed1 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Sat, 18 Feb 2012 18:43:45 +0300 Subject: [PATCH 6/6] Support of disable of shell variables substitution in autocompletion. Signed-off-by: Andrew Borodin --- lib/widget/input_complete.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/widget/input_complete.c b/lib/widget/input_complete.c index 5fc030d46..05f1d7baa 100644 --- a/lib/widget/input_complete.c +++ b/lib/widget/input_complete.c @@ -875,7 +875,22 @@ try_complete (char *text, int *lc_start, int *lc_end, input_complete_t flags) if (flags & INPUT_COMPLETE_COMMANDS) p = strrchr (word, '`'); if (flags & (INPUT_COMPLETE_COMMANDS | INPUT_COMPLETE_VARIABLES)) + { q = strrchr (word, '$'); + + /* don't substitute variable in \$ case */ + if (q != NULL && q != word && q[-1] == '\\') + { + size_t qlen; + + qlen = strlen (q); + /* drop '\\' */ + memmove (q - 1, q, qlen + 1); + /* adjust flags */ + flags &= ~INPUT_COMPLETE_VARIABLES; + q = NULL; + } + } if (flags & INPUT_COMPLETE_HOSTNAMES) r = strrchr (word, '@'); if (q && q[1] == '(' && (flags & INPUT_COMPLETE_COMMANDS))