c - 向 shell 添加历史记录功能

标签 c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define MAX_LINE 80 /* 80 chars per line, per command, should be enough. */

/**
 * setup() reads in the next command line, separating it into distinct tokens
 * using whitespace as delimiters. It also sets the args parameter as a 
 * null-terminated string.
 */

typedef struct list
{
   int num;
   int *ptr;
   struct history * next;
}history;

void setup(char inputBuffer[], char *args[],int *background)
{
    int length, /* Number  of characters in the command line */
        i,      /* Loop index for inputBuffer array */
        start,  /* Index where beginning of next command parameter is */
        ct;     /* Index of where to place the next parameter into args[] */

    ct = 0;

    /* Read what the user enters on the command line */
    length = read(STDIN_FILENO, inputBuffer, MAX_LINE);  

    start = -1;
    if (length == 0)
        exit(0);            /* ^d was entered, end of user command stream */
    if (length < 0){
        perror("error reading command");
    exit(-1);           /* terminate with error code of -1 */
    }

    /* Examine every character in the inputBuffer */
    for (i = 0; i < length; i++) { 
        switch (inputBuffer[i]){
        case ' ':
        case '\t' :               /* argument separators */
            if(start != -1){
                args[ct] = &inputBuffer[start];    /* set up pointer */
                ct++;
            }
            inputBuffer[i] = '\0'; /* add a null char; make a C string */
            start = -1;
            break;

        case '\n':                 /* should be the final char examined */
            if (start != -1){
                args[ct] = &inputBuffer[start];     
                ct++;
            }
            inputBuffer[i] = '\0';
            args[ct] = NULL; /* no more arguments to this command */
            break;

        case '&':
            *background = 1;
            inputBuffer[i] = '\0';
            break;

        default :             /* some other character */
            if (start == -1)
                start = i;
    } 
    }    
    args[ct] = NULL; /* just in case the input line was > 80 */
} 

int main(void)
{
    char inputBuffer[MAX_LINE]; /* Buffer to hold the command entered */
    int background;             /* Equals 1 if a command is followed by '&' */
    char *args[MAX_LINE/2+1];/* Command line (of 80) has max of 40 arguments */


    while (1){            /* program terminates normally inside setup */
    background = 0;
    printf("CSE2431Sh->");
        fflush(0);
        setup(inputBuffer, args, &background);       /* get next command */

    /* the steps are:
     (1) fork a child process using fork()
     (2) the child process will invoke execvp()
     (3) if background == 0, the parent will wait, 
        otherwise returns to the setup() function. */

        int child_pid;
        int status;
        int ph;
        history *history = NULL;

        child_pid = fork();

        if(child_pid == 0)
        {
                ph++;
                history->num = ph;
                history->ptr = args;
                execvp(args[0],args);
                /* If execvp returns, it must have failed. */

                printf("Execvp Failed\n");
                exit(0);
        }
        else
        {
                if(background == 0)
                {
                        int parent_pid;
                        while ((parent_pid = wait(&status)) != -1 && parent_$
                                ;
                }
                else
                {
                        setup(inputBuffer, args, &background);
                }
        }
   }
}

我正在尝试向 shell 添加历史记录功能。 shell 应该存储命令和编号。它还应该能够恢复最后 8 个命令以再次运行。例如,如果用户 28-35 输入了 35 个命令,则应该能够恢复。用户应该能够通过键入历史记录来查看最后 8 个命令,并通过键入 x num 来运行上一个命令,其中 num 是命令的编号,或者 xr 来运行最近的命令。我的计划是使用链接列表,但我在使用它时遇到了麻烦,并且只有几个小时的时间来完成它。

最佳答案

正如我们在您的 previous post 中所建议的那样:

How about a linked list where you additionally store the length, first and last item of the list? The items being the commands from your inputBuffer.

关于c - 向 shell 添加历史记录功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19258622/

相关文章:

c - 使用 RSA_PKCS1_OAEP_PADDING 进行 RSA 签名

c - fgets() 和 sscanf() 只存储数组中的第一个整数

c - 使用cgroup_new_cgroup创建cgroup时出现错误50007

c - 给函数内部的指针赋值

c - 使用 pow 函数时了解什么是函数重载概念

C、Windows API在注册表项后创建文件夹结构

python - 在嵌入式 Python 解释器中跟踪代码执行

c - 输出utf-8到控制台

c - "Undefined reference to mknod "如何解决?

c - Azure物联网中心: HTTP Device-to-Cloud Messages without SDK?