如何使用C编程语言创建一个可以实现历史捕获箭头的shell?

时间:2015-03-03 04:24:01

标签: c linux shell

我已将先前命令的历史记录保存在二维数组中。但我不知道如何查看向上箭头。

如何使用实现此功能的C编程语言(在Linux中)?

2 个答案:

答案 0 :(得分:2)

感谢 n.m。提供了很好的建议。

以下是使用libreadline的示例:

// rltest.c
#include <stdio.h>
#include <stdlib.h>
#include <readline.h>
#include <history.h>

int main(void)
{
    char* input, shell_prompt[100];
    // Configure readline to auto-complete paths when the tab key is hit.
    rl_bind_key('\t', rl_complete);
    // while work is not 0 program executes the loop
    int work = 1;
    printf("Commands to use: name, ver, exit \n");
    // loop for working with commands
    while(work) {
        // Build prompt string.
        snprintf(shell_prompt, sizeof(shell_prompt), "your command $ ");
        // Display prompt and read input
        input = readline(shell_prompt);
        // Check for EOF.
        if (!input)
            break;
        // Add input to history.
        add_history(input);
        // Command analysis and execution
        if( 0 == strcmp(input, "exit") )
        {
            printf("Bye!\n");
            work = 0;
        }
        if( 0 == strcmp(input, "name") )
        {
            printf("I'm readline example\n");
        }
        if( 0 == strcmp(input, "ver") )
        {
            printf("My version is 0.1\n");
        }
        // ...
        // Free input for future use
        free(input);
    }
    return 0;
}

编译这个例子:

1)安装readline库

 apt-get install libreadline-dev

2)编译程序为

 gcc rltest.c -I/usr/include/readline -lreadline

答案 1 :(得分:0)

尝试以下示例:

#include <stdio.h>
#include <termios.h>

// constants for sizes
#define LINES 5
#define MAX_LEN 50

// this is just for demonstration
// read more about screen cleaning http://www.cplusplus.com/articles/4z18T05o/
void ClearScreen()
{
    printf("\033[2J");
    printf("\033[0;0f");
}

// function based on termios library
// see more at http://stackoverflow.com/questions/7469139/what-is-equivalent-to-getch-getche-in-linux

static struct termios old, new;

/* Initialize new terminal i/o settings */
void initTermios(int echo)
{
  tcgetattr(0, &old); /* grab old terminal i/o settings */
  new = old; /* make new settings same as old settings */
  new.c_lflag &= ~ICANON; /* disable buffered i/o */
  new.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */
  tcsetattr(0, TCSANOW, &new); /* use these new terminal i/o settings now */
}

/* Restore old terminal i/o settings */
void resetTermios(void)
{
  tcsetattr(0, TCSANOW, &old);
}

/* Read 1 character - echo defines echo mode */
char getch_(int echo)
{
  char ch;
  initTermios(echo);
  ch = getchar();
  resetTermios();
  return ch;
}

int main(void)
{
    char strBuff[LINES][MAX_LEN] = {"The first line",
            "This is the longest line in this buffer",
            "Just a middle line",
            "Short",
            "The last line"};
    char ch; // one character input buffer
    int cnt = 0; // number of current line to be shown
    // loop for showing lines
    ClearScreen();
    do{
        // show current line
        printf("%s", strBuff[cnt]); 
        // read input (arrows. characters, etc.
        ch = getch_(0);
        // arrows detection in input
        if(ch == 27)
        {
            ch = getch_(0);
            if( ch == 91)
            {
                ch = getch_(0);
                if(ch == 66)  // up arrow
                {
                    cnt++;
                    if(cnt >= LINES)
                        cnt = 0;
                }
                else
                    if(ch == 65)  // down arrow
                    {
                        cnt--;
                        if(cnt < 0)
                            cnt = LINES-1;
                    }
            }
        }
        // cleaning screen before next output
        ClearScreen();

    }while(ch != 'q'); // press 'q' for exit
}