Browse Source

Restore saved history from the file

Serj Kalichev 11 years ago
parent
commit
afe2cd995a
6 changed files with 37 additions and 17 deletions
  1. 1 1
      clish/shell.h
  2. 1 1
      clish/shell/shell_tinyrl.c
  3. 1 1
      tinyrl/history.h
  4. 32 12
      tinyrl/history/history.c
  5. 1 1
      tinyrl/tinyrl.c
  6. 1 1
      tinyrl/tinyrl.h

+ 1 - 1
clish/shell.h

@@ -350,7 +350,7 @@ void clish_shell__set_wdog_timeout(clish_shell_t *instance,
 	unsigned int timeout);
 unsigned int clish_shell__get_wdog_timeout(const clish_shell_t *instance);
 int clish_shell__save_history(const clish_shell_t *instance, const char *fname);
-int clish_shell__restore_history(const clish_shell_t *instance, const char *fname);
+int clish_shell__restore_history(clish_shell_t *instance, const char *fname);
 void clish_shell__stifle_history(clish_shell_t *instance, unsigned int stifle);
 
 _END_C_DECL

+ 1 - 1
clish/shell/shell_tinyrl.c

@@ -576,7 +576,7 @@ int clish_shell__save_history(const clish_shell_t *this, const char *fname)
 }
 
 /*----------------------------------------------------------*/
-int clish_shell__restore_history(const clish_shell_t *this, const char *fname)
+int clish_shell__restore_history(clish_shell_t *this, const char *fname)
 {
 	return tinyrl__restore_history(this->tinyrl, fname);
 }

+ 1 - 1
tinyrl/history.h

@@ -72,7 +72,7 @@ extern unsigned tinyrl_history_unstifle(tinyrl_history_t * instance);
 extern bool_t tinyrl_history_is_stifled(const tinyrl_history_t * instance);
 
 extern int tinyrl_history_save(const tinyrl_history_t *instance, const char *fname);
-extern int tinyrl_history_restore(const tinyrl_history_t *instance, const char *fname);
+extern int tinyrl_history_restore(tinyrl_history_t *instance, const char *fname);
 
     /*
        INFORMATION ABOUT THE HISTORY LIST 

+ 32 - 12
tinyrl/history/history.c

@@ -435,27 +435,47 @@ int tinyrl_history_save(const tinyrl_history_t *this, const char *fname)
 
 /*-------------------------------------*/
 /* Restore command history from specified file */
-int tinyrl_history_restore(const tinyrl_history_t *this, const char *fname)
+int tinyrl_history_restore(tinyrl_history_t *this, const char *fname)
 {
-/*
-	tinyrl_history_entry_t *entry;
-	tinyrl_history_iterator_t iter;
 	FILE *f;
+	char *p;
+	int part_len = 300;
+	char *buf;
+	int buf_len = part_len;
+	int res = 0;
 
 	if (!fname) {
 		errno = EINVAL;
 		return -1;
 	}
-	if (!(f = fopen(fname, "w")))
-		return -1;
-	for (entry = tinyrl_history_getfirst(this, &iter);
-		entry; entry = tinyrl_history_getnext(&iter)) {
-		if (fprintf(f, "%s\n", tinyrl_history_entry__get_line(entry)) < 0)
-			return -1;
+	if (!(f = fopen(fname, "r")))
+		return 0; /* Can't find history file */
+
+	buf = malloc(buf_len);
+	p = buf;
+	while (fgets(p, buf_len - (p - buf), f)) {
+		char *ptmp = NULL;
+		char *el = strchr(buf, '\n');
+		if (el) { /* The whole line was readed */
+			*el = '\0';
+			tinyrl_history_add(this, buf);
+			p = buf;
+			continue;
+		}
+		buf_len += part_len;
+		ptmp = realloc(buf, buf_len);
+		if (!ptmp) {
+			res = -1;
+			goto end;
+		}
+		buf = ptmp;
+		p = buf + buf_len - part_len - 1;
 	}
+end:
+	free(buf);
 	fclose(f);
-*/
-	return 0;
+
+	return res;
 }
 
 

+ 1 - 1
tinyrl/tinyrl.c

@@ -1404,7 +1404,7 @@ int tinyrl__save_history(const tinyrl_t *this, const char *fname)
 }
 
 /*----------------------------------------------------------*/
-int tinyrl__restore_history(const tinyrl_t *this, const char *fname)
+int tinyrl__restore_history(tinyrl_t *this, const char *fname)
 {
 	return tinyrl_history_restore(this->history, fname);
 }

+ 1 - 1
tinyrl/tinyrl.h

@@ -217,7 +217,7 @@ extern void tinyrl_limit_line_length(
 extern unsigned tinyrl__get_width(const tinyrl_t *instance);
 extern unsigned tinyrl__get_height(const tinyrl_t *instance);
 extern int tinyrl__save_history(const tinyrl_t *instance, const char *fname);
-extern int tinyrl__restore_history(const tinyrl_t *instance, const char *fname);
+extern int tinyrl__restore_history(tinyrl_t *instance, const char *fname);
 extern void tinyrl__stifle_history(tinyrl_t *instance, unsigned int stifle);
 
 _END_C_DECL