用C编写命令行shell;第一次尝试使用 ncurses/C
我正在开发一个类项目,其中我必须编写一个具有以下要求的命令行 shell:
- shell 必须能够读取缓冲的输入
- 缓冲区应为 64 个字符
- 应处理错误条件
- 超出缓冲区大小
- 中断(当信号到达时) - 请参阅 read() 的手册页
- 无效输入(无法解析的字符、空行等)
- 可能遇到的任何其他错误。
- Shell 必须具有至少 20 项的历史记录,并且历史记录的大小不能是静态的。当历史缓冲区已满时,将删除最旧的项目并添加最新的项目。
- 程序应该能够在前台或后台运行。 (使用 &)
- Ctrl-D 将退出 shell
- Ctrl-C 将打印完整的历史记录
- 命令“history”也将打印完整的历史记录。最新的项目将位于列表的底部。
- 所有其他信号将被捕获并在 shell 中显示给用户
- 程序将使用 read() 命令读取输入,除非支持箭头键
我选择实现历史循环的箭头键,所以我使用 ncurses用于输入,而不是 read()。我认为我使用 strtok() 解析输入,使用 fork() 和 execvp() 运行进程做得很好,但我没有正确实现 ncurses。到目前为止,我所做的就是初始化一个新屏幕,显示提示,然后在按下任何键时出现段错误。不好。
我想问题一定出在我的设计上。我不太了解 ncurses。我应该在这个项目中使用什么类型的数据结构?我应该如何处理 ncurses 设置、拆卸以及其间的所有事情?窗口和屏幕有什么关系?我应该使用一个可全局访问的窗口/屏幕吗?另外,我一直在尝试使用 char* 作为输入缓冲区,使用 char** 作为命令历史记录,但我没有 C 经验,所以尽管阅读了 malloc、calloc 和 realloc,但我还是不确定在缓冲区和历史记录中存储命令的最佳方式。关于管理这些字符数组有什么建议吗?
tl;dr:如何正确使用 ncurses 来制作命令行 shell,以及如何使用 C 处理命令内存管理?
我意识到这是一个相当沉重的问题。 :(
编辑:我已经看到 http: //www.gnu.org/software/libc/manual/html_node/Implementing-a-Shell.html 和 http://www.linuxinfor.com/english/NCURSES-Programming/ 但 ncurses 文档实际上有太多开销,我只是想使用它识别箭头键的功能。
I'm working on a class project in which I must write a command line shell with the following requirements:
- The shell must able to read buffered input
- Buffer should be 64 characters
- Error conditions should be handled
- Exceeded buffer size
- Interruptions (when a signal arrives) – see the man page for read()
- Invalid input (unparsable characters, blank lines, etc)
- Any other error that may be encountered.
- Shell must have a history of at least 20 items, and the history must not be of a static size. When the history buffer is full, the oldest item is removed and the newest item added.
- Programs should be able to run in the foreground or background. (using &)
- Ctrl-D will exit the shell
- Ctrl-C will print the complete history
- The Command ‘history’ will also print the complete history. Newest items will be at the bottom of the list.
- All other signals will be trapped and displayed to the user in the shell
- Program will use the read() command to read in input, unless the arrow keys are supported
I have opted to implement arrow keys for history cycling, so I'm using ncurses for input, rather than read(). I think I'm doing all right using strtok() to parse input, and fork() and execvp() to run the processes, but I'm not doing all right implementing ncurses correctly. All I've gotten it to do so far is init a new screen, display the prompt, then segfault upon any key press. Not good.
I reckon the problem must be in my design. I'm not wrapping my head around ncurses too well. What sort of data structures should I be using for this project? How should I handle the ncurses setup, teardown, and everything in between? What's the deal with windows and screens, and should I have a single globally accessible window/screen that I work with? Also, I've been trying to use a char* for the input buffer, and a char** for the command history, but I have no experience in C, so despite reading up on malloc, calloc, and realloc, I'm not sure of the best way to store commands in the buffer and the history. Any tips on managing these char arrays?
tl;dr: How do I use ncurses correctly to make a command line shell, and how do I handle the command memory management with C?
I realize this is a pretty hefty question. :(
edit: I have already seen http://www.gnu.org/software/libc/manual/html_node/Implementing-a-Shell.html and http://www.linuxinfor.com/english/NCURSES-Programming/ but the ncurses documentation has actually too much overhead. I just want to use its ability to recognize arrow keys.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
下面是一些示例代码:
执行动态内存分配。
以非阻塞模式从控制台读取。
使用 VT100 代码将帧缓冲区打印到控制台。
它使用 GCC 在 Linux 上编译,没有警告或错误。它远非没有错误,但它应该给您一些关于可能性的想法。编译并运行它,按[向上]和[向下]将打印消息,输入字符并按[输入]将“执行”命令。
Here's some sample code which:
Performs dynamic memory allocation.
Reads from the console in non-blocking mode.
Uses VT100 codes to print a frame buffer to the console.
It compiles on Linux using GCC without warnings or errors. It's far from bug free, but it should give you some ideas of what's possible. Compile and run it, pressing [up] and [down] will print messages, typing characters and hitting [enter] will "execute" the command.
如果您的输入缓冲区定义为 64 个字符,那么我建议使用
char
数组而不是char*
。像char input_buffer[65];
这样的东西应该可以满足您的目的(为尾随的'\0'
添加一个额外的字符)。就命令历史记录而言,您可以使用二维数组。像
char command_history[20][65];
这样的东西应该可以让您存储 20 个旧命令,每个命令有 64 个字符。静态分配这些缓冲区应该会让事情变得更容易,因为您不必担心
malloc
和朋友。如果没有看到你的代码,很难给你太多具体的建议。我有一种感觉,您正在犯与初次学习 C 的人所犯的常见错误相同类型的错误。您能否发布给您带来问题的代码部分,以便我们可以更多地了解您在做什么?
发布提供的代码后进行更新:
我看到的一个问题是函数
takeInput
没有 return 语句。当您在主函数中使用input = takeInput();
时,input
的值并未设置为您认为的值。它可能不是一个有效的指针,这会导致您的input[j]
行出现段错误。您对
cmdHistory
的使用也需要重新审视。您可以使用 cmdHistory = (char**)calloc(21,sizeof(int)); 来分配它,这样您就有足够的空间来存储 21 个整数。在函数printHistory
中,您将cmdHistory
的元素传递给printw
,就好像它们是字符串一样(它们只是整数)。这绝对不是在做你想做的事。相反,cmdHistory
的分配逻辑需要看起来更像您的解除分配逻辑(向后除外)。分配一个char**
数组,然后迭代该数组,将每个指针分配给新分配的缓冲区。就像数组中的每个元素都有一个free
语句,加上整个数组有一个free
语句一样,您也应该有一个malloc
每个元素加上一个malloc
作为整个数组。即使您无法使用静态分配的堆栈,也请尝试使用静态分配的堆栈来编写程序。这将使您解决关键检测逻辑等问题,而不必担心程序的动态内存部分。一旦其余部分正常工作,返回并交换静态内存以进行动态内存分配。这样,您一次只需调试一点点。
If your input buffer is defined to be 64 characters, then I would recommend using a
char
array instead of achar*
. Something likechar input_buffer[65];
should serve your purposes (add an extra character for the trailing'\0'
).As far as command history goes, you can use a two-dimensional array for that. Something like
char command_history[20][65];
should let you store 20 old commands of 64 characters each.Allocating these buffers statically should make things a bit easier for you, as you won't have to worry about
malloc
and friends.It's hard to give you too much specific advice without seeing your code. I have a feeling that you are making the same type of mistakes that are typical to people first learning C. Can you post the part of your code that is giving you problems so that we can learn more about what you are doing?
Update after posted provided code:
One problem I'm seeing is that the function
takeInput
doesn't have a return statement. When you useinput = takeInput();
inside your main function, the value ofinput
isn't being set to what you think it is. It's probably not a valid pointer, which is causing your line that saysinput[j]
to segfault.Your usage of
cmdHistory
also needs revisiting. You allocate it withcmdHistory = (char**)calloc(21,sizeof(int));
, which gives you enough space to store 21 integers. In the functionprintHistory
, you pass elements ofcmdHistory
toprintw
as if they were strings (they're only integers). This is most definitely not doing what you want it to do. Instead, your allocation logic forcmdHistory
needs to look more like your de-allocation logic (except backwards). Allocate an array ofchar**
, then iterate through the array, assigning each pointer to a newly-allocated buffer. Just like you have onefree
statement for each element in the array plus afree
for the array as a whole, you should have onemalloc
for each element plus onemalloc
for the array as a whole.Even if you can't use a statically-allocated stack, try writing your program using one anyway. This will let you work the kinks out of your key detection logic, etc without having to worry about the dynamic memory part of the program. Once the rest of it is working, go back in and swap out the static memory for dynamic memory allocation. That way, you're only having to debug a little bit at a time.
你看过 Readline 图书馆吗?它非常适合在您的项目中使用。
http://cnswww.cns.cwru.edu/php/chet/readline /rltop.html
Have you looked at the Readline library? It's ideal for use in your project.
http://cnswww.cns.cwru.edu/php/chet/readline/rltop.html