Command line history changes, lastpatch_71 from Vladimir N. Oleynik

This commit is contained in:
Glenn L McGrath 2002-12-09 11:10:40 +00:00
parent 6b5bd0e5ab
commit fdbbb04893
4 changed files with 77 additions and 38 deletions

View File

@ -3,7 +3,7 @@
# see scripts/kbuild/config-language.txt. # see scripts/kbuild/config-language.txt.
# #
menu "Bourne Shell" menu "Another Bourne-like Shell"
choice choice
prompt "Choose your default shell" prompt "Choose your default shell"
@ -95,12 +95,6 @@ config CONFIG_ASH_OPTIMIZE_FOR_SIZE
help help
Please submit a patch to add help text for this item. Please submit a patch to add help text for this item.
config CONFIG_FEATURE_COMMAND_SAVEHISTORY
bool " history saving"
default n
depends on CONFIG_ASH
help
Please submit a patch to add help text for this item.
if CONFIG_FEATURE_SH_IS_HUSH if CONFIG_FEATURE_SH_IS_HUSH
config CONFIG_HUSH config CONFIG_HUSH
@ -158,6 +152,13 @@ config CONFIG_FEATURE_COMMAND_EDITING
help help
Please submit a patch to add help text for this item. Please submit a patch to add help text for this item.
config CONFIG_FEATURE_COMMAND_SAVEHISTORY
bool " history saving"
default n
depends on CONFIG_ASH
help
Please submit a patch to add help text for this item.
config CONFIG_FEATURE_COMMAND_TAB_COMPLETION config CONFIG_FEATURE_COMMAND_TAB_COMPLETION
bool "tab completion" bool "tab completion"
default n default n

View File

@ -1196,6 +1196,10 @@ static struct var vlc_all;
static struct var vlc_ctype; static struct var vlc_ctype;
#endif #endif
#ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
static struct var vhistfile;
#endif
struct varinit { struct varinit {
struct var *var; struct var *var;
int flags; int flags;
@ -1241,6 +1245,10 @@ static const struct varinit varinit[] = {
change_lc_all}, change_lc_all},
{&vlc_ctype, VSTRFIXED | VTEXTFIXED | VUNSET, "LC_CTYPE=", {&vlc_ctype, VSTRFIXED | VTEXTFIXED | VUNSET, "LC_CTYPE=",
change_lc_ctype}, change_lc_ctype},
#endif
#ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
{&vhistfile, VSTRFIXED | VTEXTFIXED | VUNSET, "HISTFILE=",
NULL},
#endif #endif
{NULL, 0, NULL, {NULL, 0, NULL,
NULL} NULL}
@ -7312,6 +7320,20 @@ int ash_main(int argc, char **argv)
init(); init();
setstackmark(&smark); setstackmark(&smark);
procargs(argc, argv); procargs(argc, argv);
#ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
if ( iflag ) {
const char *hp = lookupvar("HISTFILE");
if(hp == NULL ) {
hp = lookupvar("HOME");
if(hp != NULL) {
char *defhp = concat_path_file(hp, ".ash_history");
setvar("HISTFILE", defhp, 0);
free(defhp);
}
}
}
#endif
if (argv[0] && argv[0][0] == '-') if (argv[0] && argv[0][0] == '-')
isloginsh = 1; isloginsh = 1;
if (isloginsh) { if (isloginsh) {
@ -7357,8 +7379,12 @@ int ash_main(int argc, char **argv)
if (sflag || minusc == NULL) { if (sflag || minusc == NULL) {
state4: /* XXX ??? - why isn't this before the "if" statement */ state4: /* XXX ??? - why isn't this before the "if" statement */
#ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY #ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
if ( iflag ) if ( iflag ) {
load_history ( ".ash_history" ); const char *hp = lookupvar("HISTFILE");
if(hp != NULL )
load_history ( hp );
}
#endif #endif
cmdloop(1); cmdloop(1);
} }
@ -7550,10 +7576,6 @@ static int exitcmd(int argc, char **argv)
{ {
if (stoppedjobs()) if (stoppedjobs())
return 0; return 0;
#ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
if ( iflag )
save_history ( ".ash_history" );
#endif
if (argc > 1) if (argc > 1)
exitstatus = number(argv[1]); exitstatus = number(argv[1]);
@ -11615,12 +11637,22 @@ static void exitshell(int status)
trap[0] = NULL; trap[0] = NULL;
evalstring(p, 0); evalstring(p, 0);
} }
l1:handler = &loc2; /* probably unnecessary */ l1:
handler = &loc2; /* probably unnecessary */
flushall(); flushall();
#ifdef CONFIG_ASH_JOB_CONTROL #ifdef CONFIG_ASH_JOB_CONTROL
setjobctl(0); setjobctl(0);
#endif #endif
l2:_exit(status); #ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
if (iflag && rootshell) {
const char *hp = lookupvar("HISTFILE");
if(hp != NULL )
save_history ( hp );
}
#endif
l2:
_exit(status);
/* NOTREACHED */ /* NOTREACHED */
} }

View File

@ -1131,40 +1131,44 @@ static int get_next_history(void)
} }
} }
extern void load_history ( char *fromfile )
{
#ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY #ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
extern void load_history ( const char *fromfile )
{
FILE *fp; FILE *fp;
int hi;
// cleanup old /* cleanup old */
while ( n_history ) {
if ( history [n_history - 1] ) for(hi = n_history; hi > 0; ) {
free ( history [n_history - 1] ); hi--;
n_history--; free ( history [hi] );
} }
if (( fp = fopen ( fromfile, "r" ))) { if (( fp = fopen ( fromfile, "r" ))) {
char buffer [256];
int i, l;
for ( i = 0; i < MAX_HISTORY; i++ ) { for ( hi = 0; hi < MAX_HISTORY; ) {
if ( !fgets ( buffer, sizeof( buffer ) - 1, fp )) char * hl = get_line_from_file(fp);
int l;
if(!hl)
break; break;
l = xstrlen ( buffer ); chomp(hl);
if ( l && buffer [l - 1] == '\n' ) l = strlen(hl);
buffer [l - 1] = 0; if(l >= BUFSIZ)
history [n_history++] = xstrdup ( buffer ); hl[BUFSIZ-1] = 0;
if(l == 0 || hl[0] == ' ') {
free(hl);
continue;
}
history [hi++] = hl;
} }
fclose ( fp ); fclose ( fp );
} }
cur_history = n_history; cur_history = n_history = hi;
#endif
} }
extern void save_history ( char *tofile ) extern void save_history ( const char *tofile )
{ {
#ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
FILE *fp = fopen ( tofile, "w" ); FILE *fp = fopen ( tofile, "w" );
if ( fp ) { if ( fp ) {
@ -1176,8 +1180,8 @@ extern void save_history ( char *tofile )
} }
fclose ( fp ); fclose ( fp );
} }
#endif
} }
#endif
#endif #endif

View File

@ -3,7 +3,9 @@
int cmdedit_read_input(char* promptStr, char* command); int cmdedit_read_input(char* promptStr, char* command);
void load_history ( char *fromfile ); #ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
void save_history ( char *tofile ); void load_history ( const char *fromfile );
void save_history ( const char *tofile );
#endif
#endif /* CMDEDIT_H */ #endif /* CMDEDIT_H */