Command line history changes, lastpatch_71 from Vladimir N. Oleynik
authorbug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277>
Mon, 9 Dec 2002 11:10:40 +0000 (11:10 +0000)
committerbug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277>
Mon, 9 Dec 2002 11:10:40 +0000 (11:10 +0000)
git-svn-id: svn://busybox.net/trunk/busybox@6140 69ca8d6d-28ef-0310-b511-8ec308f3f277

shell/Config.in
shell/ash.c
shell/cmdedit.c
shell/cmdedit.h

index 69f8482..b643c8f 100644 (file)
@@ -3,7 +3,7 @@
 # see scripts/kbuild/config-language.txt.
 #
 
-menu "Bourne Shell"
+menu "Another Bourne-like Shell"
 
 choice
        prompt "Choose your default shell"
@@ -95,12 +95,6 @@ config CONFIG_ASH_OPTIMIZE_FOR_SIZE
        help
          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
        config CONFIG_HUSH
@@ -158,6 +152,13 @@ config CONFIG_FEATURE_COMMAND_EDITING
        help
          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
        bool "tab completion"
        default n
index bec37cf..7151690 100644 (file)
@@ -1196,6 +1196,10 @@ static struct var vlc_all;
 static struct var vlc_ctype;
 #endif
 
+#ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
+static struct var vhistfile;
+#endif
+
 struct varinit {
        struct var *var;
        int flags;
@@ -1241,6 +1245,10 @@ static const struct varinit varinit[] = {
         change_lc_all},
        {&vlc_ctype, VSTRFIXED | VTEXTFIXED | VUNSET, "LC_CTYPE=",
         change_lc_ctype},
+#endif
+#ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
+       {&vhistfile, VSTRFIXED | VTEXTFIXED | VUNSET, "HISTFILE=",
+        NULL},
 #endif
        {NULL, 0, NULL,
         NULL}
@@ -7312,6 +7320,20 @@ int ash_main(int argc, char **argv)
        init();
        setstackmark(&smark);
        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] == '-')
                isloginsh = 1;
        if (isloginsh) {
@@ -7357,8 +7379,12 @@ int ash_main(int argc, char **argv)
        if (sflag || minusc == NULL) {
          state4:                       /* XXX ??? - why isn't this before the "if" statement */
 #ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
-           if ( iflag )
-               load_history ( ".ash_history" );
+           if ( iflag ) {
+               const char *hp = lookupvar("HISTFILE");
+
+               if(hp != NULL )
+                       load_history ( hp );
+           }
 #endif
                cmdloop(1);
        }
@@ -7550,10 +7576,6 @@ static int exitcmd(int argc, char **argv)
 {
        if (stoppedjobs())
                return 0;
-#ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
-       if ( iflag )
-               save_history ( ".ash_history" );
-#endif                 
                
        if (argc > 1)
                exitstatus = number(argv[1]);
@@ -11615,12 +11637,22 @@ static void exitshell(int status)
                trap[0] = NULL;
                evalstring(p, 0);
        }
-  l1:handler = &loc2;  /* probably unnecessary */
+l1:
+       handler = &loc2;   /* probably unnecessary */
        flushall();
 #ifdef CONFIG_ASH_JOB_CONTROL
        setjobctl(0);
 #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 */
 }
 
index 73378e6..2e102e3 100644 (file)
@@ -1131,40 +1131,44 @@ static int get_next_history(void)
        }
 }
 
-
-extern void load_history ( char *fromfile )
-{
 #ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
+extern void load_history ( const char *fromfile )
+{
        FILE *fp;
+       int hi;
 
-       // cleanup old
-       while ( n_history ) {
-               if ( history [n_history - 1] )
-                       free ( history [n_history - 1] );
-               n_history--;
+       /* cleanup old */
+
+       for(hi = n_history; hi > 0; ) {
+               hi--;
+               free ( history [hi] );
        }
 
        if (( fp = fopen ( fromfile, "r" ))) {
-               char buffer [256];
-               int i, l;
        
-               for ( i = 0; i < MAX_HISTORY; i++ ) {
-                       if ( !fgets ( buffer, sizeof( buffer ) - 1, fp ))
+               for ( hi = 0; hi < MAX_HISTORY; ) {
+                       char * hl = get_line_from_file(fp);
+                       int l;
+
+                       if(!hl)
                                break;
-                       l = xstrlen     ( buffer );
-                       if ( l && buffer [l - 1] == '\n' )
-                               buffer [l - 1] = 0;
-                       history [n_history++] = xstrdup ( buffer );
+                       chomp(hl);
+                       l = strlen(hl);
+                       if(l >= BUFSIZ)
+                               hl[BUFSIZ-1] = 0;
+                       if(l == 0 || hl[0] == ' ') {
+                               free(hl);
+                               continue;
+                       }
+                       history [hi++] = hl;
                }
                fclose ( fp );
        }
-       cur_history = n_history;
-#endif
+       cur_history = n_history = hi;
 }
 
-extern void save_history ( char *tofile )
+extern void save_history ( const char *tofile )
 {
-#ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
        FILE *fp = fopen ( tofile, "w" );
        
        if ( fp ) {
@@ -1176,8 +1180,8 @@ extern void save_history ( char *tofile )
                }
                fclose ( fp );
        }
-#endif
 }
+#endif
 
 #endif
 
index 045588d..991dafc 100644 (file)
@@ -3,7 +3,9 @@
 
 int     cmdedit_read_input(char* promptStr, char* command);
 
-void    load_history ( char *fromfile );
-void    save_history ( char *tofile );
+#ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
+void    load_history ( const char *fromfile );
+void    save_history ( const char *tofile );
+#endif
 
 #endif /* CMDEDIT_H */