diff --git a/apps/sh.c b/apps/sh.c index 87c686ee..b3235677 100644 --- a/apps/sh.c +++ b/apps/sh.c @@ -227,6 +227,7 @@ void tab_complete_func(rline_context_t * c) { #define COMPLETE_FILE 1 #define COMPLETE_COMMAND 2 #define COMPLETE_CUSTOM 3 +#define COMPLETE_VARIABLE 4 int complete_mode = COMPLETE_FILE; int command_adj = 0; @@ -248,6 +249,11 @@ void tab_complete_func(rline_context_t * c) { complete_mode = COMPLETE_CUSTOM; } + /* complete variable names */ + if (*prefix == '$') { + complete_mode = COMPLETE_VARIABLE; + } + if (complete_mode == COMPLETE_COMMAND) { /* Complete a command name */ @@ -331,6 +337,25 @@ void tab_complete_func(rline_context_t * c) { completions++; } + } else if (complete_mode == COMPLETE_VARIABLE) { + + char ** envvar = environ; + free_matches = 1; + + while (*envvar) { + char * tmp = strdup(*envvar); + char * c = strchr(tmp, '='); + *c = '\0'; + if (strstr(tmp, prefix+1) == tmp) { + char * m = malloc(strlen(tmp)+2); + sprintf(m, "$%s", tmp); + list_insert(matches, m); + match = m; + } + free(tmp); + envvar++; + } + } if (matches->length == 1) {