#define CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
#define CONFIG_FEATURE_CLEAN_UP
-#endif /* TEST */
+#endif /* TEST */
#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
#include <dirent.h>
#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
#include "pwd_.h"
-#endif /* advanced FEATURES */
+#endif /* advanced FEATURES */
/* Maximum length of the linked list for the command line history */
}
#ifdef CONFIG_FEATURE_COMMAND_EDITING_VI
-static char delbuf[BUFSIZ]; /* a place to store deleted characters */
-static char *delp = delbuf;
-static int newdelflag; /* whether delbuf should be reused yet */
+#define DELBUFSIZ 128
+static char *delbuf; /* a (malloced) place to store deleted characters */
+static char *delp;
+static char newdelflag; /* whether delbuf should be reused yet */
#endif
/* Delete the char in front of the cursor, optionally saving it
#ifdef CONFIG_FEATURE_COMMAND_EDITING_VI
if (save) {
if (newdelflag) {
+ if (!delbuf)
+ delbuf = malloc(DELBUFSIZ);
+ /* safe if malloc fails */
delp = delbuf;
newdelflag = 0;
}
- if (delp - delbuf < BUFSIZ)
+ if (delbuf && (delp - delbuf < DELBUFSIZ))
*delp++ = command_ps[j];
}
#endif
#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
+static char **matches;
+static int num_matches;
+static char *add_char_to_match;
+
+static void add_match(char *matched, int add_char)
+{
+ int nm = num_matches;
+ int nm1 = nm + 1;
+
+ matches = xrealloc(matches, nm1 * sizeof(char *));
+ add_char_to_match = xrealloc(add_char_to_match, nm1);
+ matches[nm] = matched;
+ add_char_to_match[nm] = (char)add_char;
+ num_matches++;
+}
+
static int is_execute(const struct stat *st)
{
if ((!my_euid && (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) ||
#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
-static char **username_tab_completion(char *ud, int *num_matches)
+static void username_tab_completion(char *ud, char *with_shash_flg)
{
struct passwd *entry;
int userlen;
- char *temp;
-
ud++; /* ~user/... to user/... */
userlen = strlen(ud);
- if (num_matches == 0) { /* "~/..." or "~user/..." */
+ if (with_shash_flg) { /* "~/..." or "~user/..." */
char *sav_ud = ud - 1;
char *home = 0;
+ char *temp;
if (*ud == '/') { /* "~/..." */
home = home_pwd_buf;
strcpy(sav_ud, temp2);
}
}
- return 0; /* void, result save to argument :-) */
} else {
/* "~[^/]*" */
- char **matches = (char **) NULL;
- int nm = 0;
-
setpwent();
while ((entry = getpwent()) != NULL) {
/* Null usernames should result in all users as possible completions. */
if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) {
-
- bb_xasprintf(&temp, "~%s/", entry->pw_name);
- matches = xrealloc(matches, (nm + 1) * sizeof(char *));
-
- matches[nm++] = temp;
+ add_match(bb_xasprintf("~%s", entry->pw_name), '/');
}
}
endpwent();
- (*num_matches) = nm;
- return (matches);
}
}
#endif /* CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION */
return npth;
}
-static char *add_quote_for_spec_chars(char *found)
+static char *add_quote_for_spec_chars(char *found, int add)
{
int l = 0;
char *s = xmalloc((strlen(found) + 1) * 2);
s[l++] = '\\';
s[l++] = *found++;
}
+ if(add)
+ s[l++] = (char)add;
s[l] = 0;
return s;
}
-static char **exe_n_cwd_tab_completion(char *command, int *num_matches,
- int type)
+static void exe_n_cwd_tab_completion(char *command, int type)
{
-
- char **matches = 0;
DIR *dir;
struct dirent *next;
char dirbuf[BUFSIZ];
- int nm = *num_matches;
struct stat st;
char *path1[1];
char **paths = path1;
dirbuf[(pfind - command) + 1] = 0;
#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
if (dirbuf[0] == '~') /* ~/... or ~user/... */
- username_tab_completion(dirbuf, 0);
+ username_tab_completion(dirbuf, dirbuf);
#endif
/* "strip" dirname in command */
pfind++;
while ((next = readdir(dir)) != NULL) {
char *str_found = next->d_name;
+ int add_chr = 0;
/* matched ? */
if (strncmp(str_found, pfind, strlen(pfind)))
strcpy(found, next->d_name); /* only name */
if (S_ISDIR(st.st_mode)) {
/* name is directory */
- str_found = found;
- found = concat_path_file(found, "");
- free(str_found);
- str_found = add_quote_for_spec_chars(found);
+ char *e = found + strlen(found) - 1;
+
+ add_chr = '/';
+ if(*e == '/')
+ *e = '\0';
} else {
/* not put found file if search only dirs for cd */
if (type == FIND_DIR_ONLY)
goto cont;
- str_found = add_quote_for_spec_chars(found);
if (type == FIND_FILE_ONLY ||
(type == FIND_EXE_ONLY && is_execute(&st)))
- strcat(str_found, " ");
+ add_chr = ' ';
}
/* Add it to the list */
- matches = xrealloc(matches, (nm + 1) * sizeof(char *));
-
- matches[nm++] = str_found;
+ add_match(found, add_chr);
+ continue;
cont:
free(found);
}
free(paths[0]); /* allocated memory only in first member */
free(paths);
}
- *num_matches = nm;
- return (matches);
-}
-
-static int match_compare(const void *a, const void *b)
-{
- return strcmp(*(char **) a, *(char **) b);
}
-
#define QUOT (UCHAR_MAX+1)
#define collapse_pos(is, in) { \
display by column original ideas from ls applet,
very optimize by my :)
*/
-static void showfiles(char **matches, int nfiles)
+static void showfiles(void)
{
int ncols, row;
int column_width = 0;
+ int nfiles = num_matches;
int nrows = nfiles;
+ char str_add_chr[2];
+ int l;
/* find the longest file name- use that as the column width */
for (row = 0; row < nrows; row++) {
- int l = strlen(matches[row]);
-
+ l = strlen(matches[row]);
+ if(add_char_to_match[row])
+ l++;
if (column_width < l)
column_width = l;
}
nrows /= ncols;
if(nfiles % ncols)
nrows++; /* round up fractionals */
- column_width = -column_width; /* for printf("%-Ns", ...); */
} else {
ncols = 1;
}
+ str_add_chr[1] = 0;
for (row = 0; row < nrows; row++) {
int n = row;
int nc;
-
- for(nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++)
- printf("%*s", column_width, matches[n]);
- printf("%s\n", matches[n]);
+ int acol;
+
+ for(nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
+ 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]);
+ while(l < acol) {
+ putchar(' ');
+ l++;
+ }
+ }
+ str_add_chr[0] = add_char_to_match[n];
+ printf("%s%s\n", matches[n], str_add_chr);
}
}
static void input_tab(int *lastWasTab)
{
/* Do TAB completion */
- static int num_matches;
- static char **matches;
-
if (lastWasTab == 0) { /* free all memory */
if (matches) {
while (num_matches > 0)
free(matches[--num_matches]);
free(matches);
matches = (char **) NULL;
+ free(add_char_to_match);
+ add_char_to_match = NULL;
}
return;
}
if (! *lastWasTab) {
- char *tmp;
+ char *tmp, *tmp1;
int len_found;
char matchBuf[BUFSIZ];
int find_type;
* then try completing this word as a username. */
if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
- matches = username_tab_completion(matchBuf, &num_matches);
+ username_tab_completion(matchBuf, NULL);
+ if (!matches)
#endif
/* Try to match any executable in our path and everything
* in the current working directory that matches. */
- if (!matches)
- matches =
- exe_n_cwd_tab_completion(matchBuf,
- &num_matches, find_type);
- /* Remove duplicate found */
+ exe_n_cwd_tab_completion(matchBuf, find_type);
+ /* Remove duplicate found and sort */
if(matches) {
- int i, j;
+ int i, j, n, srt;
/* bubble */
- for(i=0; i<(num_matches-1); i++)
- for(j=i+1; j<num_matches; j++)
- if(matches[i]!=0 && matches[j]!=0 &&
- strcmp(matches[i], matches[j])==0) {
- free(matches[j]);
- matches[j]=0;
- }
- j=num_matches;
- num_matches = 0;
- for(i=0; i<j; i++)
- if(matches[i]) {
- if(!strcmp(matches[i], "./"))
- matches[i][1]=0;
- else if(!strcmp(matches[i], "../"))
- matches[i][2]=0;
- matches[num_matches++]=matches[i];
+ n = num_matches;
+ for(i=0; i<(n-1); i++)
+ for(j=i+1; j<n; j++)
+ if(matches[i]!=NULL && matches[j]!=NULL) {
+ srt = strcmp(matches[i], matches[j]);
+ if(srt == 0) {
+ free(matches[j]);
+ matches[j]=0;
+ } else if(srt > 0) {
+ tmp1 = matches[i];
+ matches[i] = matches[j];
+ matches[j] = tmp1;
+ srt = add_char_to_match[i];
+ add_char_to_match[i] = add_char_to_match[j];
+ add_char_to_match[j] = srt;
+ }
}
+ j = n;
+ n = 0;
+ for(i=0; i<j; i++)
+ if(matches[i]) {
+ matches[n]=matches[i];
+ add_char_to_match[n]=add_char_to_match[i];
+ n++;
+ }
+ num_matches = n;
}
/* Did we find exactly one match? */
if (!matches || num_matches > 1) {
- char *tmp1;
beep();
if (!matches)
return; /* not found */
- /* sort */
- qsort(matches, num_matches, sizeof(char *), match_compare);
-
/* find minimal match */
- tmp = bb_xstrdup(matches[0]);
- for (tmp1 = tmp; *tmp1; tmp1++)
+ tmp1 = bb_xstrdup(matches[0]);
+ for (tmp = tmp1; *tmp; tmp++)
for (len_found = 1; len_found < num_matches; len_found++)
- if (matches[len_found][(tmp1 - tmp)] != *tmp1) {
- *tmp1 = 0;
+ if (matches[len_found][(tmp - tmp1)] != *tmp) {
+ *tmp = 0;
break;
}
- if (*tmp == 0) { /* have unique */
- free(tmp);
+ if (*tmp1 == 0) { /* have unique */
+ free(tmp1);
return;
}
+ tmp = add_quote_for_spec_chars(tmp1, 0);
+ free(tmp1);
} else { /* one match */
- tmp = matches[0];
+ tmp = add_quote_for_spec_chars(matches[0], add_char_to_match[0]);
/* for next completion current found */
*lastWasTab = FALSE;
}
-
len_found = strlen(tmp);
/* have space to placed match? */
if ((len_found - strlen(matchBuf) + len) < BUFSIZ) {
/* write out the matched command */
redraw(cmdedit_y, len - recalc_pos);
}
- if (tmp != matches[0])
- free(tmp);
+ free(tmp);
} else {
/* Ok -- the last char was a TAB. Since they
* just hit TAB again, print a list of all the
/* Go to the next line */
goto_new_line();
- showfiles(matches, num_matches);
+ showfiles();
redraw(0, len - sav_cursor);
}
}
}
#ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
-extern void load_history ( const char *fromfile )
+void load_history ( const char *fromfile )
{
FILE *fp;
int hi;
cur_history = n_history = hi;
}
-extern void save_history ( const char *tofile )
+void save_history ( const char *tofile )
{
FILE *fp = fopen ( tofile, "w" );
vi_mode = viflag;
}
-void
+static void
vi_Word_motion(char *command, int eat)
{
while (cursor < len && !isspace(command[cursor]))
input_forward();
}
-void
+static void
vi_word_motion(char *command, int eat)
{
if (isalnum(command[cursor]) || command[cursor] == '_') {
input_forward();
}
-void
+static void
vi_End_motion(char *command)
{
input_forward();
input_forward();
}
-void
+static void
vi_end_motion(char *command)
{
if (cursor >= len-1)
}
}
-void
+static void
vi_Back_motion(char *command)
{
while (cursor > 0 && isspace(command[cursor-1]))
input_backward(1);
}
-void
+static void
vi_back_motion(char *command)
{
if (cursor <= 0)
}
#endif
-/*
- * the normal emacs mode and vi's insert mode are the same.
- * commands entered when in vi command mode ("escape mode") get
- * an extra bit added to distinguish them. this lets them share
- * much of the code in the big switch and while loop. i
- * experimented with an ugly macro to make the case labels for
- * these cases go away entirely when vi mode isn't configured, in
- * hopes of letting the jump tables get smaller:
- * #define vcase(caselabel) caselabel
- * and then
- * case CNTRL('A'):
- * case vcase(VICMD('0'):)
- * but it didn't seem to make any difference in code size,
- * and the macro-ized code was too ugly.
+/*
+ * the emacs and vi modes share much of the code in the big
+ * command loop. commands entered when in vi's command mode (aka
+ * "escape mode") get an extra bit added to distinguish them --
+ * this keeps them from being self-inserted. this clutters the
+ * big switch a bit, but keeps all the code in one place.
*/
-#define VI_cmdbit 0x100
-#define VICMD(somecmd) ((somecmd)|VI_cmdbit)
+#define vbit 0x100
+
+/* leave out the "vi-mode"-only case labels if vi editing isn't
+ * configured. */
+#define vi_case(caselabel) USE_FEATURE_COMMAND_EDITING(caselabel)
/* convert uppercase ascii to equivalent control char, for readability */
#define CNTRL(uc_char) ((uc_char) - 0x40)
int break_out = 0;
int lastWasTab = FALSE;
unsigned char c;
+ unsigned int ic;
#ifdef CONFIG_FEATURE_COMMAND_EDITING_VI
- unsigned int ic, prevc;
+ unsigned int prevc;
int vi_cmdmode = 0;
#endif
/* prepare before init handlers */
/* if we can't read input then exit */
goto prepare_to_die;
+ ic = c;
+
#ifdef CONFIG_FEATURE_COMMAND_EDITING_VI
newdelflag = 1;
- ic = c;
if (vi_cmdmode)
- ic |= VI_cmdbit;
- switch (ic)
-#else
- switch (c)
+ ic |= vbit;
#endif
+ switch (ic)
{
case '\n':
case '\r':
- case VICMD('\n'):
- case VICMD('\r'):
+ vi_case( case '\n'|vbit: )
+ vi_case( case '\r'|vbit: )
/* Enter */
goto_new_line();
break_out = 1;
break;
case CNTRL('A'):
- case VICMD('0'):
+ vi_case( case '0'|vbit: )
/* Control-a -- Beginning of line */
input_backward(cursor);
break;
case CNTRL('B'):
- case VICMD('h'):
- case VICMD('\b'):
- case VICMD(DEL):
+ vi_case( case 'h'|vbit: )
+ vi_case( case '\b'|vbit: )
+ vi_case( case DEL|vbit: )
/* Control-b -- Move back one character */
input_backward(1);
break;
case CNTRL('C'):
- case VICMD(CNTRL('C')):
+ vi_case( case CNTRL('C')|vbit: )
/* Control-c -- stop gathering input */
goto_new_line();
#ifndef CONFIG_ASH
}
break;
case CNTRL('E'):
- case VICMD('$'):
+ vi_case( case '$'|vbit: )
/* Control-e -- End of line */
input_end();
break;
case CNTRL('F'):
- case VICMD('l'):
- case VICMD(' '):
+ vi_case( case 'l'|vbit: )
+ vi_case( case ' '|vbit: )
/* Control-f -- Move forward one character */
input_forward();
break;
printf("\033[J");
break;
case CNTRL('L'):
- case VICMD(CNTRL('L')):
+ vi_case( case CNTRL('L')|vbit: )
/* Control-l -- clear screen */
printf("\033[H");
redraw(0, len-cursor);
break;
#if MAX_HISTORY >= 1
case CNTRL('N'):
- case VICMD(CNTRL('N')):
- case VICMD('j'):
+ vi_case( case CNTRL('N')|vbit: )
+ vi_case( case 'j'|vbit: )
/* Control-n -- Get next command in history */
if (get_next_history())
goto rewrite_line;
break;
case CNTRL('P'):
- case VICMD(CNTRL('P')):
- case VICMD('k'):
+ vi_case( case CNTRL('P')|vbit: )
+ vi_case( case 'k'|vbit: )
/* Control-p -- Get previous command from history */
if (cur_history > 0) {
get_previous_history();
break;
#endif
case CNTRL('U'):
- case VICMD(CNTRL('U')):
+ vi_case( case CNTRL('U')|vbit: )
/* Control-U -- Clear line before cursor */
if (cursor) {
strcpy(command, command + cursor);
}
break;
case CNTRL('W'):
- case VICMD(CNTRL('W')):
+ vi_case( case CNTRL('W')|vbit: )
/* Control-W -- Remove the last word */
while (cursor > 0 && isspace(command[cursor-1]))
input_backspace();
input_backspace();
break;
#if CONFIG_FEATURE_COMMAND_EDITING_VI
- case VICMD('i'):
+ case 'i'|vbit:
vi_cmdmode = 0;
break;
- case VICMD('I'):
+ case 'I'|vbit:
input_backward(cursor);
vi_cmdmode = 0;
break;
- case VICMD('a'):
+ case 'a'|vbit:
input_forward();
vi_cmdmode = 0;
break;
- case VICMD('A'):
+ case 'A'|vbit:
input_end();
vi_cmdmode = 0;
break;
- case VICMD('x'):
+ case 'x'|vbit:
input_delete(1);
break;
- case VICMD('X'):
+ case 'X'|vbit:
if (cursor > 0) {
input_backward(1);
input_delete(1);
}
break;
- case VICMD('W'):
+ case 'W'|vbit:
vi_Word_motion(command, 1);
break;
- case VICMD('w'):
+ case 'w'|vbit:
vi_word_motion(command, 1);
break;
- case VICMD('E'):
+ case 'E'|vbit:
vi_End_motion(command);
break;
- case VICMD('e'):
+ case 'e'|vbit:
vi_end_motion(command);
break;
- case VICMD('B'):
+ case 'B'|vbit:
vi_Back_motion(command);
break;
- case VICMD('b'):
+ case 'b'|vbit:
vi_back_motion(command);
break;
- case VICMD('C'):
+ case 'C'|vbit:
vi_cmdmode = 0;
/* fall through */
- case VICMD('D'):
+ case 'D'|vbit:
goto clear_to_eol;
- case VICMD('c'):
+ case 'c'|vbit:
vi_cmdmode = 0;
/* fall through */
- case VICMD('d'):
- {
+ case 'd'|vbit:
+ {
int nc, sc;
sc = cursor;
prevc = ic;
case 'E':
switch (c) {
case 'w': /* "dw", "cw" */
- vi_word_motion(command, vi_cmdmode);
+ vi_word_motion(command, vi_cmdmode);
break;
case 'W': /* 'dW', 'cW' */
vi_Word_motion(command, vi_cmdmode);
}
}
break;
- case VICMD('p'):
+ case 'p'|vbit:
input_forward();
/* fallthrough */
- case VICMD('P'):
+ case 'P'|vbit:
put();
break;
- case VICMD('r'):
+ case 'r'|vbit:
if (safe_read(0, &c, 1) < 1)
goto prepare_to_die;
if (c == 0)
}
break;
#endif /* CONFIG_FEATURE_COMMAND_EDITING_VI */
- case ESC:{
+
+ case ESC:
+
#if CONFIG_FEATURE_COMMAND_EDITING_VI
if (vi_mode) {
/* ESC: insert mode --> command mode */
goto prepare_to_die;
/* different vt100 emulations */
if (c == '[' || c == 'O') {
- case VICMD('['):
- case VICMD('O'):
+ vi_case( case '['|vbit: )
+ vi_case( case 'O'|vbit: )
if (safe_read(0, &c, 1) < 1)
goto prepare_to_die;
}
beep();
}
break;
- }
default: /* If it's regular input, do the normal thing */
#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT