[PATCH] shell tab completion: print only basename of completions

Rich Felker dalias at aerifal.cx
Tue Jul 18 23:58:54 PDT 2006


during shell command line editing, when the user hits tabs and there
are multiple possible completions, busybox currently shows the full
pathname for each one, rather than just the final component. for long
pathnames this fills up the screen very quickly. the attached patch
causes bb to print only the final component, like bash/readline and
other shells with tab completion.

notes:
possibly could use basename() rather than strrchr+conditional, but
basename is allowed to modify the string passed to it so i'm not sure
it's safe. is there safe bb basename function to use?


rich

-------------- next part --------------
Index: shell/cmdedit.c
===================================================================
--- shell/cmdedit.c	(revision 15108)
+++ shell/cmdedit.c	(working copy)
@@ -991,10 +991,13 @@
 	int nrows = nfiles;
 	char str_add_chr[2];
 	int l;
+	char *base;
 
 	/* find the longest file name-  use that as the column width */
 	for (row = 0; row < nrows; row++) {
-		l = strlen(matches[row]);
+		base = strrchr(matches[row], '/');
+		if (base) base++; else base = matches[row];
+		l = strlen(base);
 		if(add_char_to_match[row])
 		    l++;
 		if (column_width < l)
@@ -1017,17 +1020,21 @@
 		int acol;
 
 		for(nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
+			base = strrchr(matches[n], '/');
+			if (base) base++; else base = matches[n];
 			str_add_chr[0] = add_char_to_match[n];
 			acol = str_add_chr[0] ? column_width - 1 : column_width;
-			printf("%s%s", matches[n], str_add_chr);
-			l = strlen(matches[n]);
+			printf("%s%s", base, str_add_chr);
+			l = strlen(base);
 			while(l < acol) {
 			    putchar(' ');
 			    l++;
 			}
 		}
+		base = strrchr(matches[n], '/');
+		if (base) base++; else base = matches[n];
 		str_add_chr[0] = add_char_to_match[n];
-		printf("%s%s\n", matches[n], str_add_chr);
+		printf("%s%s\n", base, str_add_chr);
 	}
 }
 


More information about the busybox mailing list