我打算在CentOS中用C语言开发一个命令行工具,代码如下:
// client.c
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <unistd.h>
# include <termios.h>
int main(int argc, char *argv[])
{
char command[128];
while (1)
{
memset(command, 0, 128);
printf("cli > ");
if (fgets(command, 128, stdin) != NULL)
{
if (strcmp(command, "exit\n") == 0)
break;
if (strcmp(command, "\n") == 0)
continue;
printf("do something ... %s", command);
}
}
return 0;
}
程序可以工作,但当我按下箭头键时,它没有按我期望的方式执行。
我已经输入了一个简单的SQL,现在光标停留在分号后面。
[root@olap tests]# gcc client.c
[root@olap tests]# ./a.out
cli > selectt * from table_001;
但是我拼错了第一个关键字,应该是select
,而不是selectt
。我现在按左箭头键(←)试图纠正这个错误。但是光标并没有像我预期的那样正确移动,它变成了下面的样子。
[root@olap tests]# gcc client.c
[root@olap tests]# ./a.out
cli > selectt * from table_001;^[[D^[[D^[[D^[[D^[[D^[[D^[[D
我应该如何修改程序来解决这个问题?
我希望能得到擅长C语言开发的人的帮助。
谢谢大家
2条答案
按热度按时间6psbrbz91#
您可能需要尝试使用getch的ncurses库
此示例代码演示了如何检测箭头键并显示CLI提示符:
f45qwnt82#
考虑使用
readline
库,你可以找到它here。它支持箭头键,输入历史和任何长度的输入(如果配置)。