include busybox after libc includes
[oweals/busybox.git] / shell / cmdedit.c
index b6e743eb4118f33532758d84d687043c7c01f2e6..2e102e3512ee075d75f877014bea04a6db03e647 100644 (file)
 
 
 /* Maximum length of the linked list for the command line history */
-#define MAX_HISTORY 15
+#ifndef CONFIG_FEATURE_COMMAND_HISTORY
+#define MAX_HISTORY   15
+#else
+#define MAX_HISTORY   CONFIG_FEATURE_COMMAND_HISTORY
+#endif
+
 #if MAX_HISTORY < 1
 #warning cmdedit: You set MAX_HISTORY < 1. The history algorithm switched off.
 #else
@@ -1125,6 +1130,59 @@ static int get_next_history(void)
                return 0;
        }
 }
+
+#ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
+extern void load_history ( const char *fromfile )
+{
+       FILE *fp;
+       int hi;
+
+       /* cleanup old */
+
+       for(hi = n_history; hi > 0; ) {
+               hi--;
+               free ( history [hi] );
+       }
+
+       if (( fp = fopen ( fromfile, "r" ))) {
+       
+               for ( hi = 0; hi < MAX_HISTORY; ) {
+                       char * hl = get_line_from_file(fp);
+                       int l;
+
+                       if(!hl)
+                               break;
+                       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 = hi;
+}
+
+extern void save_history ( const char *tofile )
+{
+       FILE *fp = fopen ( tofile, "w" );
+       
+       if ( fp ) {
+               int i;
+               
+               for ( i = 0; i < n_history; i++ ) {
+                       fputs ( history [i], fp );
+                       fputc ( '\n', fp );
+               }
+               fclose ( fp );
+       }
+}
+#endif
+
 #endif
 
 enum {