2014-12-31 97 views
1

如何在unix shell中添加歷史記錄功能以允許用戶訪問最近輸入的 命令,用戶將能夠通過使用 功能訪問最多10條命令。UNIX Shell和歷史記錄功能

這一評論從項目解釋歷史的一部分:

用戶將能夠通過使用 功能來訪問多達10個命令。命令將從1開始連續編號, 編號將繼續超過10.例如,如果用戶輸入了命令,則最近的10個命令將編號爲26到35. 用戶將能夠列出通過在osh>提示符下輸入命令 歷史記錄 來執行命令歷史記錄。例如,假設歷史由 命令組成(從最多到最近): ps,ls -l,top,cal,who,date 命令歷史將輸出: 6 ps 5 ls -l 4頂 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] = '\0'; 
     start = -1; 
     break; 

    case '\n':     
     if (start != -1){ 
      args[ct] = &inputBuffer[start];  
      ct++; 
      } 
     inputBuffer[i] = '\0'; 
     args[ct] = NULL; 
     break; 

    case '&': 
     *background = 1; 
     inputBuffer[i] = '\0'; 
     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 buffer\n"); 
      /* 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); 
      } 
     } 
    } 
+1

「我有錯誤,不知道如何解決它」 - 錯誤是什麼? – alfasin

+1

一如既往:用所有警告和調試信息編譯('gcc -Wall -Wextra -g')。然後**使用調試器**('gdb') –

+0

@alfasin感謝您的回覆,,,錯誤是這樣的一個梨:'os @ debian:〜/ Desktop/gh $ gcc sc -o s.out /tmp/cc5jEhHg.o:在主函數': sc :(。text + 0x1e7):未定義的參考displayHistory' collect2:ld返回1退出狀態' – Manar

回答

1

我會使用GNU readline庫。它給你線條版本和history支持,你也可以完成。