在简单的shell中实现历史记录功能(在C中)

时间:2014-03-13 15:18:06

标签: c shell unix

我是C语言编程的新手,目前参加C / UNIX课程。我在开始这项任务时遇到了一些问题......这是我们分配的第一个C程序,但是教授在解释完成任务或从哪里开始所需要的时间非常少,并且要求他没有取得多大成就。因为我做了很少的C,我不知道从哪里开始。

该程序是一个简单的shell,我应该实现一些历史功能。已经给出了shell的源代码。假设添加这些能力:

history - 在命令行输入历史记录,最新的shell命令在前面显示数字(从1开始)。命令将存储在文本文件中。我假设历史命令每次都会打印这个文本文件。

!number - 在命令行输入,带有该号码的文件中的命令将被重新执行

!string - 在命令行输入,重新执行以该字符串开头的最后一个命令

!! - 在命令行输入,重新执行上一个命令

源代码(这是shell的三个文件之一,但我相信那个应该被编辑的文件):

#include "parser.h"
#include "shell.h"
#include <stdio.h>
#include <stdlib.h>

int main(void) {
char input[MAXINPUTLINE];

signal_c_init();

printf("Welcome to the sample shell!  You may enter commands here, one\n");
printf("per line.  When you're finished, press Ctrl+D on a line by\n");
printf("itself.  I understand basic commands and arguments separated by\n");
printf("spaces, redirection with < and >, up to two commands joined\n");
printf("by a pipe, tilde expansion, and background commands with &.\n\n");


while (fgets(input, sizeof(input), stdin)) {
stripcrlf(input);
parse(input);
printf("\n$ ");
}
return 0;
}

我只想说明我不会要求任何人在这里做我的作业。我只是字面上不知道从哪里开始,并没有给出任何迹象。在课前没有太多这种语言的经验,我对这种类型的事物的实现方式不具备可比性。我可以开始的任何类型的资源或功能都会有所帮助。

1 个答案:

答案 0 :(得分:1)

要处理历史记录(以及命令行编辑和...),请查看readline或editline库。

相关问题