Commit 9a986552d553b07ce94348f04ae659a0dd56850f

antirez 2010-03-23T13:49:32

now the API to add an history entry has a const argument

diff --git a/README.markdown b/README.markdown
index 5edb2b2..41e35c7 100644
--- a/README.markdown
+++ b/README.markdown
@@ -23,7 +23,7 @@ Since it's so young I guess there are a few bugs, or the lib may not compile or 
 
 The library is currently less than 400 lines of code. In order to use it in your project just look at the *example.c* file in the source distribution, it is trivial. Linenoise is BSD code, so you can use both in free software and commercial software.
 
-## Tested in...
+## Tested with...
 
  * Linux text only console ($TERM = linux)
  * Linux KDE terminal application ($TERM = xterm)
@@ -31,6 +31,7 @@ The library is currently less than 400 lines of code. In order to use it in your
  * Mac OS X iTerm ($TERM = xterm)
  * Mac OS X default Terminal.app ($TERM = xterm)
  * OpenBSD 4.5 through an OSX Terminal.app ($TERM = screen)
+ * IBM AIX 6.1
 
 Please test it everywhere you can and report back!
 
diff --git a/linenoise.c b/linenoise.c
index 5950d49..8139630 100644
--- a/linenoise.c
+++ b/linenoise.c
@@ -90,7 +90,7 @@ static int history_len = 0;
 char **history = NULL;
 
 static void linenoiseAtExit(void);
-int linenoiseHistoryAdd(char *line);
+int linenoiseHistoryAdd(const char *line);
 
 static void freeHistory(void) {
     if (history) {
@@ -357,20 +357,22 @@ char *linenoise(const char *prompt) {
 }
 
 /* Using a circular buffer is smarter, but a bit more complex to handle. */
-int linenoiseHistoryAdd(char *line) {
+int linenoiseHistoryAdd(const char *line) {
+    char *linecopy;
+
     if (history_max_len == 0) return 0;
     if (history == 0) {
         history = malloc(sizeof(char*)*history_max_len);
         if (history == NULL) return 0;
         memset(history,0,(sizeof(char*)*history_max_len));
     }
-    line = strdup(line);
-    if (!line) return 0;
+    linecopy = strdup(line);
+    if (!linecopy) return 0;
     if (history_len == history_max_len) {
         memmove(history,history+1,sizeof(char*)*(history_max_len-1));
         history_len--;
     }
-    history[history_len] = line;
+    history[history_len] = linecopy;
     history_len++;
     return 1;
 }
diff --git a/linenoise.h b/linenoise.h
index ff45e2c..57bf9d1 100644
--- a/linenoise.h
+++ b/linenoise.h
@@ -35,7 +35,7 @@
 #define __LINENOISE_H
 
 char *linenoise(const char *prompt);
-int linenoiseHistoryAdd(char *line);
+int linenoiseHistoryAdd(const char *line);
 int linenoiseHistorySetMaxLen(int len);
 
 #endif /* __LINENOISE_H */