readline库实现命令行自动补全
linux下应用程序可能需要交互式输入命令,但普通的标准io进行命令输入显得有些呆板,人性化不足。本文讲述使用libreadline库,实现类似sh的交换终端:支持命令自动补全,支持历史命令等。
part1: readline安装
(1) 下载readline源码:
(2) 解压后, 在源码目录依次执行 ./configure, make, sudo make install 完成安装
part2:readline使用举例
点击(此处)折叠或打开
-
#include <stdlib.h>
-
#include <stdio.h> //注意,readline.h中可能需要调用标准io库的内容,所以stdio.h必须在readline.h之前被包含
-
#include <readline/readline.h>
-
#include <readline/history.h>
-
-
-
/*
-
* 真正的命令执行函数
-
* 测试时,被定义为桩函数了
-
*/
-
int com_list(char *para)
-
{
-
printf("do com_list:%s\n", para);
-
return 0;
-
}
-
-
int com_view(char *para)
-
{
-
printf("do com_view:%s\n", para);
-
return 0;
-
}
-
-
int com_rename(char *para)
-
{
-
printf("do com_rename:%s\n", para);
-
return 0;
-
}
-
int com_stat(char *para)
-
{
-
printf("do com_stat:%s\n", para);
-
return 0;
-
}
-
-
int com_pwd(char *para)
-
{
-
printf("do com_pwd:%s\n", para);
-
return 0;
-
}
-
int com_delete(char *para)
-
{
-
printf("do com_delete:%s\n", para);
-
return 0;
-
}
-
int com_help(char *para)
-
{
-
printf("do com_help:%s\n", para);
-
return 0;
-
}
-
-
int com_cd(char *para)
-
{
-
printf("do com_cd:%s\n", para);
-
return 0;
-
}
-
int com_quit(char *para)
-
{
-
printf("do com_quit:%s\n", para);
-
exit(0);
-
}
-
-
-
/*
-
* a structure which contains information on the commands this program
-
* can understand.
-
*/
-
typedef struct {
-
char *name; /* user printable name of the function. */
-
rl_icpfunc_t *func; /* function to call to do the job. */
-
char *doc; /* documentation for this function. */
-
} command;
-
-
command commands[] = {
-
{ "cd", com_cd, "change to directory dir" },
-
{ "delete", com_delete, "delete file" },
-
{ "help", com_help, "display this text" },
-
{ "?", com_help, "synonym for `help'" },
-
{ "list", com_list, "list files in dir" },
-
{ "ls", com_list, "synonym for `list'" },
-
{ "pwd", com_pwd, "print the current working directory" },
-
{ "quit", com_quit, "quit using fileman" },
-
{ "rename", com_rename, "rename file to newname" },
-
{ "stat", com_stat, "print out statistics on file" },
-
{ "view", com_view, "view the contents of file" },
-
{ (char *)null, (rl_icpfunc_t *)null, (char *)null }
-
};
-
-
-
char* dupstr(char *s)
-
{
-
char *r;
-
-
r = malloc (strlen (s) 1);
-
strcpy(r, s);
-
return (r);
-
}
-
-
// clear up white spaces
-
char* stripwhite (char *string)
-
{
-
register char *s, *t;
-
-
for (s = string; whitespace (*s); s)
-
;
-
-
if (*s == 0)
-
return (s);
-
-
t = s strlen (s) - 1;
-
while (t > s && whitespace (*t))
-
t--;
-
-
*t = '\0';
-
-
return s;
-
}
-
-
-
/*
-
* look up name as the name of a command, and return a pointer to that
-
* command. return a null pointer if name isn't a command name.
-
*/
-
command *find_command (char *name)
-
{
-
register int i;
-
-
for (i = 0; commands[i].name; i)
-
if (strcmp (name, commands[i].name) == 0)
-
return (&commands[i]);
-
-
return ((command *)null);
-
}
-
-
/* execute a command line. */
-
int execute_line (char *line)
-
{
-
register int i;
-
command *command;
-
char *word;
-
-
/* isolate the command word. */
-
i = 0;
-
while (line[i] && whitespace (line[i]))
-
i;
-
word = line i;
-
-
while (line[i] && !whitespace (line[i]))
-
i;
-
-
if (line[i])
-
line[i] = '\0';
-
-
command = find_command (word);
-
-
if (!command)
-
{
-
fprintf (stderr, "%s: no such command for fileman.\n", word);
-
return (-1);
-
}
-
-
/* get argument to command, if any. */
-
while (whitespace (line[i]))
-
i;
-
-
word = line i;
-
-
/* call the function. */
-
return ((*(command->func)) (word));
-
}
-
-
/*
-
* generator function for command completion. state lets us know whether
-
* to start from scratch; without any state (i.e. state == 0), then we
-
* start at the top of the list.
-
*/
-
char* command_generator (const char *text, int state)
-
{
-
static int list_index, len;
-
char *name;
-
-
/*
-
* if this is a new word to complete, initialize now. this includes
-
* saving the length of text for efficiency, and initializing the index
-
* variable to 0.
-
*/
-
if (!state)
-
{
-
list_index = 0;
-
len = strlen (text);
-
}
-
-
/* return the next name which partially matches from the command list. */
-
while (name = commands[list_index].name)
-
{
-
list_index;
-
-
if (strncmp (name, text, len) == 0)
-
return (dupstr(name));
-
}
-
-
/* if no names matched, then return null. */
-
return ((char *)null);
-
}
-
-
/*
-
* attempt to complete on the contents of text. start and end bound the
-
* region of rl_line_buffer that contains the word to complete. text is
-
* the word to complete. we can use the entire contents of rl_line_buffer
-
* in case we want to do some simple parsing. return the array of matches,
-
* or null if there aren't any.
-
*/
-
char** fileman_completion (const char *text, int start, int end)
-
{
-
char **matches;
-
-
matches = (char **)null;
-
-
/*
-
* if this word is at the start of the line, then it is a command
-
* to complete. otherwise it is the name of a file in the current
-
* directory.
-
*/
-
if (start == 0)
-
matches = rl_completion_matches (text, command_generator);
-
-
return (matches);
-
}
-
-
-
/*
-
* tell the gnu readline library how to complete. we want to try to complete
-
* on command names if this is the first word in the line, or on filenames
-
* if not.
-
*/
-
void initialize_readline ()
-
{
-
/* allow conditional parsing of the ~/.inputrc file. */
-
rl_readline_name = ">";
-
-
/* tell the completer that we want a crack first. */
-
rl_attempted_completion_function = fileman_completion;
-
}
-
-
-
-
-
int main (int argc, char **argv)
-
{
-
char *line, *s;
-
-
initialize_readline(); /* bind our completer. */
-
-
-
/* loop reading and executing lines until the user quits. */
-
for ( ; ;)
-
{
-
line = readline (">: ");
-
-
if (!line)
-
break;
-
-
/*
-
* remove leading and trailing whitespace from the line.
-
* then, if there is anything left, add it to the history list
-
* and execute it.
-
*/
-
s = stripwhite (line);
-
if (*s)
-
{
-
add_history(s);
-
execute_line(s);
-
}
-
-
free(line);
-
}
-
exit(0);
-
}
注意,编译的时候需要连接readline库,例如:gcc -o irlt ireadlinetest.c -lreadline
part3: readline下的io复用
但是如果我们有多个 io 要处理,比如既要从一个网络 io 中读数据,又要从标准输入中读取命令,上面的方法就不合适了。为了解决这个问题,我们需要自己用 select 函数监控这两个 io,当它们可读的时候通知这两个模块中的输入函数。形象地说,就是把数据“喂给”这两个模块。这样的模式需要 readline 提供“被动喂给”的工作方式。这种工作方式在 readline 中已有实现。首先,我们需要往 readline 注册回调函数,当 readline 读取到一行后,这个回调函数将被调用:
rl_callback_handler_install ("prompt> ", handle_command);
接下去,在主事件循环中,我们需要调用 rl_callback_read_char() 通知 readline 去读取一个字符。
下面给一个例子:
点击(此处)折叠或打开
-
static void handle_command (char *line)
-
{
-
...
-
}
-
-
int
-
main (int argc, char **argv)
-
{
-
int netfd
-
fd_set allfd;
-
int maxfd;
-
-
netfd = connect_to_server ();
-
-
-
fd_zero (&allfd);
-
fd_set (fileno(stdin), &allfd);
-
fd_set (netfd, &allfd);
-
maxfd = netfd;
-
-
rl_callback_handler_install ("ccnet> ", handle_command);
-
-
while (1) {
-
fd_set rfds;
-
int retval;
-
-
rfds = allfd;
-
-
retval = select (maxfd 1, &rfds, null, null, null);
-
if (retval < 0)
-
perror ("select");
-
-
if (fd_isset(0, &rfds))
-
rl_callback_read_char();
-
-
if (fd_isset(netfd, &rfds))
-
read_from_network (netfd);
-
}
-
}
关于readline更多的使用请自行阅读readline的man文件。
阅读(7422) | 评论(0) | 转发(0) |