Re: prompt, spaces, `cd' and system commands..



Here is my suggestion for a patch. Assuming you have a directory called "test ", here are the strings that work [+] and the ones that don't work [-]. I have put a $ to mark the end of the strings.

[-] cd test$
[-] cd test $
[-] cd test  $
[+] cd test   $
[+] cd test \ $
[+] cd test\ \ $
[+] cd test\ \  $
[+] cd "test  "$
[+] cd "test  " $
[+] cd 'test  '$
[+] cd 'test  ' $

The only surprising ones are in line 3 and 4. This is the trailing space issue that appears when you insert the current file using C-enter.

I casted some arguments to (const char *) to make clear that those functions will not modify the string. If you find that these casts make the code too unreadable, please tell me.

Roland
Index: src/command.c
===================================================================
RCS file: /cvsroot/mc/mc/src/command.c,v
retrieving revision 1.28
diff -u -p -u -p -r1.28 command.c
--- src/command.c	3 Nov 2004 19:43:17 -0000	1.28
+++ src/command.c	1 Dec 2004 00:44:15 -0000
@@ -53,7 +53,7 @@ WInput *cmdline;
  * they want the behavior they are used to in the shell.
  */
 static int
-examine_cd (char *path)
+examine_cd (const char *path)
 {
     int result, qlen;
     char *path_tilde;
@@ -136,55 +136,36 @@ examine_cd (char *path)
 }
 
 /* Execute the cd command on the command line */
-void do_cd_command (char *cmd)
+void do_cd_command (const char *cmd)
 {
-    int len;
+    char *allocated_dir = NULL, *unquoted_dir = NULL;
+    char *space, *dir;
 
-    /* Any final whitespace should be removed here
-       (to see why, try "cd fred "). */
-    /* NOTE: I think we should not remove the extra space,
-       that way, we can cd into hidden directories */
-    /* FIXME: what about interpreting quoted strings like the shell.
-       so one could type "cd <tab> M-a <enter>" and it would work. */
-    len = strlen (cmd) - 1;
-    while (len >= 0 &&
-	   (cmd [len] == ' ' || cmd [len] == '\t' || cmd [len] == '\n')){
-	cmd [len] = 0;
-	len --;
+    allocated_dir = g_strdup (cmd + 2);   /* skip "cd" in cmd */
+    dir = allocated_dir;
+    if (*dir == ' ') {
+        dir++;
     }
-    
-    if (cmd [2] == 0)
-	cmd = "cd ";
-
-    if (get_current_type () == view_tree){
-	if (cmd [0] == 0){
-	    sync_tree (home_dir);
-	} else if (strcmp (cmd+3, "..") == 0){
-	    char *dir = current_panel->cwd;
-	    int len = strlen (dir);
-	    while (len && dir [--len] != PATH_SEP);
-	    dir [len] = 0;
-	    if (len)
-		sync_tree (dir);
-	    else
-		sync_tree (PATH_SEP_STR);
-	} else if (cmd [3] == PATH_SEP){
-	    sync_tree (cmd+3);
-	} else {
-	    char *old = current_panel->cwd;
-	    char *new;
-	    new = concat_dir_and_file (old, cmd+3);
-	    sync_tree (new);
-	    g_free (new);
-	}
-    } else
-	if (!examine_cd (&cmd [3])) {
-	    char *d = strip_password (g_strdup (&cmd [3]), 1);
-	    message (1, MSG_ERROR, _(" Cannot chdir to \"%s\" \n %s "),
-		     d, unix_error_string (errno));
-	    g_free (d);
-	    return;
-	}
+    space = strrchr (dir, ' ');
+    if (space != NULL && space - dir >= 1
+        && space[-1] != '\\' && space[1] == '\0') {
+        *space = '\0';
+    }
+    unquoted_dir = g_shell_unquote ((const char *) dir, NULL);
+
+    if (unquoted_dir == NULL) {
+	message (1, MSG_ERROR, _(" Cannot unquote shell string "));
+    } else if (examine_cd ((const char *) unquoted_dir)) {
+        /* examine_cd() already did the work for us. */
+    } else {
+	char *d = strip_password (g_strdup ((const char *) unquoted_dir), 1);
+	message (1, MSG_ERROR, _(" Cannot chdir to \"%s\" \n %s "),
+	    d, unix_error_string (errno));
+	g_free (d);
+    }
+
+    g_free (allocated_dir);
+    g_free (unquoted_dir);
 }
 
 /* Handle Enter on the command line */
Index: src/command.h
===================================================================
RCS file: /cvsroot/mc/mc/src/command.h,v
retrieving revision 1.4
diff -u -p -u -p -r1.4 command.h
--- src/command.h	18 Aug 2004 08:58:37 -0000	1.4
+++ src/command.h	1 Dec 2004 00:44:15 -0000
@@ -4,7 +4,7 @@
 extern WInput *cmdline;
 
 WInput *command_new (int y, int x, int len);
-void do_cd_command (char *cmd);
+void do_cd_command (const char *cmd);
 void command_insert (WInput * in, const char *text, int insert_extra_space);
 
 #endif /* __COMMAND_H */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]