Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
hanoi2005
redis
提交
99628c1a
R
redis
项目概览
hanoi2005
/
redis
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
redis
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
99628c1a
编写于
7月 07, 2010
作者:
A
antirez
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
redis-cli history saved across sessions
上级
185cabda
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
54 addition
and
7 deletion
+54
-7
src/linenoise.c
src/linenoise.c
+42
-6
src/linenoise.h
src/linenoise.h
+3
-1
src/redis-cli.c
src/redis-cli.c
+9
-0
未找到文件。
src/linenoise.c
浏览文件 @
99628c1a
...
...
@@ -69,7 +69,6 @@
*
*/
#include "fmacros.h"
#include <termios.h>
#include <unistd.h>
#include <stdlib.h>
...
...
@@ -81,13 +80,14 @@
#include <sys/ioctl.h>
#include <unistd.h>
#define LINENOISE_DEFAULT_HISTORY_MAX_LEN 100
#define LINENOISE_MAX_LINE 4096
static
char
*
unsupported_term
[]
=
{
"dumb"
,
"cons25"
,
NULL
};
static
struct
termios
orig_termios
;
/* in order to restore at exit */
static
int
rawmode
=
0
;
/* for atexit() function to check if restore is needed*/
static
int
atexit_registered
=
0
;
/* register atexit just 1 time */
static
int
history_max_len
=
100
;
static
int
history_max_len
=
LINENOISE_DEFAULT_HISTORY_MAX_LEN
;
static
int
history_len
=
0
;
char
**
history
=
NULL
;
...
...
@@ -219,11 +219,10 @@ static int linenoisePrompt(int fd, char *buf, size_t buflen, const char *prompt)
if
(
nread
<=
0
)
return
len
;
switch
(
c
)
{
case
13
:
/* enter */
history_len
--
;
return
len
;
case
4
:
/* ctrl-d */
history_len
--
;
return
(
len
==
0
)
?
-
1
:
(
int
)
len
;
free
(
history
[
history_len
]);
return
(
len
==
0
&&
c
==
4
)
?
-
1
:
(
int
)
len
;
case
3
:
/* ctrl-c */
errno
=
EAGAIN
;
return
-
1
;
...
...
@@ -396,7 +395,7 @@ int linenoiseHistoryAdd(const char *line) {
char
*
linecopy
;
if
(
history_max_len
==
0
)
return
0
;
if
(
history
==
0
)
{
if
(
history
==
NULL
)
{
history
=
malloc
(
sizeof
(
char
*
)
*
history_max_len
);
if
(
history
==
NULL
)
return
0
;
memset
(
history
,
0
,(
sizeof
(
char
*
)
*
history_max_len
));
...
...
@@ -404,6 +403,7 @@ int linenoiseHistoryAdd(const char *line) {
linecopy
=
strdup
(
line
);
if
(
!
linecopy
)
return
0
;
if
(
history_len
==
history_max_len
)
{
free
(
history
[
0
]);
memmove
(
history
,
history
+
1
,
sizeof
(
char
*
)
*
(
history_max_len
-
1
));
history_len
--
;
}
...
...
@@ -431,3 +431,39 @@ int linenoiseHistorySetMaxLen(int len) {
history_len
=
history_max_len
;
return
1
;
}
/* Save the history in the specified file. On success 0 is returned
* otherwise -1 is returned. */
int
linenoiseHistorySave
(
char
*
filename
)
{
FILE
*
fp
=
fopen
(
filename
,
"w"
);
int
j
;
if
(
fp
==
NULL
)
return
-
1
;
for
(
j
=
0
;
j
<
history_len
;
j
++
)
fprintf
(
fp
,
"%s
\n
"
,
history
[
j
]);
fclose
(
fp
);
return
0
;
}
/* Load the history from the specified file. If the file does not exist
* zero is returned and no operation is performed.
*
* If the file exists and the operation succeeded 0 is returned, otherwise
* on error -1 is returned. */
int
linenoiseHistoryLoad
(
char
*
filename
)
{
FILE
*
fp
=
fopen
(
filename
,
"r"
);
char
buf
[
LINENOISE_MAX_LINE
];
if
(
fp
==
NULL
)
return
-
1
;
while
(
fgets
(
buf
,
LINENOISE_MAX_LINE
,
fp
)
!=
NULL
)
{
char
*
p
;
p
=
strchr
(
buf
,
'\r'
);
if
(
!
p
)
p
=
strchr
(
buf
,
'\n'
);
if
(
p
)
*
p
=
'\0'
;
linenoiseHistoryAdd
(
buf
);
}
fclose
(
fp
);
return
0
;
}
src/linenoise.h
浏览文件 @
99628c1a
...
...
@@ -35,7 +35,9 @@
#define __LINENOISE_H
char
*
linenoise
(
const
char
*
prompt
);
int
linenoiseHistoryAdd
(
char
*
line
);
int
linenoiseHistoryAdd
(
c
onst
c
har
*
line
);
int
linenoiseHistorySetMaxLen
(
int
len
);
int
linenoiseHistorySave
(
char
*
filename
);
int
linenoiseHistoryLoad
(
char
*
filename
);
#endif
/* __LINENOISE_H */
src/redis-cli.c
浏览文件 @
99628c1a
...
...
@@ -61,6 +61,7 @@ static struct config {
int
pubsub_mode
;
int
raw_output
;
char
*
auth
;
char
*
historyfile
;
}
config
;
static
int
cliReadReply
(
int
fd
);
...
...
@@ -439,6 +440,7 @@ static void repl() {
if
(
line
[
0
]
!=
'\0'
)
{
argv
=
splitArguments
(
line
,
&
argc
);
linenoiseHistoryAdd
(
line
);
if
(
config
.
historyfile
)
linenoiseHistorySave
(
config
.
historyfile
);
if
(
argc
>
0
)
{
if
(
strcasecmp
(
argv
[
0
],
"quit"
)
==
0
||
strcasecmp
(
argv
[
0
],
"exit"
)
==
0
)
...
...
@@ -472,6 +474,13 @@ int main(int argc, char **argv) {
config
.
pubsub_mode
=
0
;
config
.
raw_output
=
0
;
config
.
auth
=
NULL
;
config
.
historyfile
=
NULL
;
if
(
getenv
(
"HOME"
)
!=
NULL
)
{
config
.
historyfile
=
malloc
(
256
);
snprintf
(
config
.
historyfile
,
256
,
"%s/.rediscli_history"
,
getenv
(
"HOME"
));
linenoiseHistoryLoad
(
config
.
historyfile
);
}
firstarg
=
parseOptions
(
argc
,
argv
);
argc
-=
firstarg
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录