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/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]); } 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)) diff --git a/src/filemanager/command.c b/src/filemanager/command.c index 1b8eb1897..eed9affe1 100644 --- a/src/filemanager/command.c +++ b/src/filemanager/command.c @@ -80,88 +80,137 @@ WInput *cmdline; * they want the behavior they are used to in the shell. */ -static int +static char * examine_cd (const char *_path) { - int result, qlen; + typedef enum { copy_sym, subst_var } state_t; + + state_t state = copy_sym; + char *q; + size_t qlen; char *path_tilde, *path; - char *p, *q, *r, *s, c; - const char *t; + char *p, *r; /* 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 + + switch (state) { - 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) - { - strcpy (r, t); - r = strchr (r, 0); - } - if (*s == '}') - p = s + 1; + char *s; + char c; + const char *t; + + /* skip dollar */ + p++; + + if (p[0] != '{') + s = NULL; else + { + 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; } } } - *r = 0; - result = do_cd (q, cd_parse_command); + g_free (path_tilde); + + *r = '\0'; + + return q; +} + +/* --------------------------------------------------------------------------------------------- */ + +/* CDPATH handling */ +static gboolean +handle_cdpath (const char *path) +{ + gboolean result = FALSE; /* CDPATH handling */ - if (*q != PATH_SEP && !result) + if (*path != PATH_SEP) { - char *const cdpath = g_strdup (getenv ("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); + s = strchr (p, '\0'); c = *s; - *s = 0; - if (*p) + *s = '\0'; + if (*p != '\0') { - r = concat_dir_and_file (p, q); + char *r; + + r = mc_build_filename (p, path, (char *) NULL); result = do_cd (r, cd_parse_command); g_free (r); } @@ -170,9 +219,7 @@ examine_cd (const char *_path) } g_free (cdpath); } - g_free (q); - g_free (path_tilde); - g_free (path); + return result; } @@ -359,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); } } 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 ****************************************************************************/ 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; +} + +/* --------------------------------------------------------------------------------------------- */