UNIX 外壳和历史记录功能



如何在Unix shell中添加历史功能以允许用户访问最近输入的命令,用户将能够通过使用特征。

此评论解释了该项目的历史部分:

用户将能够使用特征。命令将从 1 开始连续编号,并且编号将持续到 10 点以后。例如,如果用户输入了 35命令,则最近的 10 个命令将编号为 26 到 35。用户将能够通过输入命令列出命令历史记录历史在OSH>提示符下。例如,假设历史记录由命令(从最新到最新):PS, ls -l, 顶部, cal, 谁, 日期命令历史记录将输出:6 页/秒5 ls -l4 顶部3 卡2 谁1 日期程序应支持两种检索命令的技术从命令历史记录:1. 当用户输入!,时,历史记录中的最新命令是执行。2.当用户输入单个!后跟一个整数 N,即第 N 个执行历史记录中的命令。

这是我的代码,包括历史记录部分,但我有错误,不知道如何修复它。 请帮忙

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define MAX_LINE 80 
char *history[10][MAX_LINE];
int po;


void setup(char inputBuffer[], char *args[],int *background)
{
     int length, 
      i,      
      start,  
      ct;     
  ct = 0;
   length = read(STDIN_FILENO, inputBuffer, MAX_LINE);  
   start = -1;
   if (length == 0)
     exit(0);            
  if (length < 0){
     perror("error ");
     exit(-1);           
     }

  for (i = 0; i < length; i++) { 
     switch (inputBuffer[i]){
     case ' ':
     case 't' :              
         if(start != -1){
            args[ct] = &inputBuffer[start];   
            ct++;
         }
         inputBuffer[i] = ''; 
       start = -1;
         break;
     case 'n':                
         if (start != -1){
             args[ct] = &inputBuffer[start];     
             ct++;
          }
         inputBuffer[i] = '';
         args[ct] = NULL; 
         break;
     case '&':
         *background = 1;
         inputBuffer[i] = '';
         break;
     default :             
         if (start == -1)
             start = i;
       } 
   }    
 args[ct] = NULL; 
 } 

int main(void)
 {
     char inputBuffer[MAX_LINE]; 
     int background;             
     char *args[MAX_LINE/2+1];
while (1){            
    background = 0;
    printf("os>");
        fflush(0);
        setup(inputBuffer, args, &background);       
    /**
 * After reading user input, the steps are:
 * (1) fork a child process using fork()
 * (2) the child process will invoke execvp()
 * (3) if command included &, parent will invoke wait()
 */
    pid_t pid = fork();
    printf("Fork created.n");
/*
 For example, if the
 user enters the command ps -ael at the osh> prompt, the values stored in the
 args array are:
 args[0] = "ps"
 args[1] = "-ael"
 args[2] = NULL
 This args array will be passed to the execvp() function, which has the
 following prototype:
 execvp(char *command, char *params[]);
 */
    if(pid < 0){
        printf("Fork failed.n");
    }else if(pid == 0){
        if( strcmp(args[0],"history") == 0){ /*  Print History */
            displayHistory();
        }else if(strcmp(args[0],"r") == 0){ /*  r num */
            int index = (int) args[1];
            /*runHistoryAt( index - 1);*/
        }else if(strcmp(args[0],"rr") == 0){ /*  Run recent */
            /*runHistoryAt(0);*/
        }else{  /*  Execute normally */
            printf("executing..., adding to history buffern");
            /* Add args to history buffer */
            int j;
            for (j = 0; j < sizeof(args); j++) {
                history[po][j] = args[j];
            }
            po = (po + 1) % 10;
            /* Execute!  */
            execvp(args[0],args);
        }
    }
        if(background == 0){
            wait(NULL);
            }else{
               setup(inputBuffer, args, &background);
           }
         }
    }

我会使用GNU阅读线库。它为您提供行版本和历史记录支持,您也可以完成。

最新更新