A summary of some usage of readlien installation and functions under unbuntu

Recently, when writing myshell, one of the basic requirements is to realize the completion and completion of commands, and the up and down keys to turn commands, which requires the use of the readline library.


  1. If you install readline
    , the problem is not too big, it is a matter of order. . .
    Under Ubuntu, it is generally the libreadline_dev package
$ sudo apt-get install libreadline6 libreadline6-dev

Then I looked at the files under my /usr/include... and sure enough, there is this library. .
write picture description here

  1. readline function
    Completing this involves a function called readline( ). You can check the specific use of this function in the man page, but I still didn’t quite understand how to use it.
    write picture description here
    One can know that it returns a pointer of type char, readline will read a line from the terminal, and use prompt to return the string pointed to by the prompt pointer as a prompt. If prompt is NULL or an empty string, no prompt is
    issued . The returned line needs to be dynamically allocated to the accepted pointer, and it needs to be free after use.
char *line;
char *str=">>";
line = (char *)malloc(256);
line = readline(str);
free(line);
/*此时从终端读取的字符就返回到了line中,str只是一个输出,显示*/

readline only supports the completion of files in the current directory. For example, built-in commands cannot be completed, problems, and the handling of whitespace problems. In fact, it is more convenient to use readline by doing some processing, and quoting a code that handles whitespace.

char* stripwhite (char *line)
{
    register char *s, *t;

    for (s = string; whitespace (*s); s++)
        ;

    if (*s == 0)
        return (s);

    t = s + strlen (s) - 1;
    while (t > s && whitespace (*t))
        t--;

    *++t = '\0';
    return s;
}
/*line就是使用readline函数的返回指针,whitespace去除空格*/
  1. Link library Remember to link the readline library when
    compiling
gcc myshell.c -lreadlien

Then it works very well. If you want to parse the characters returned by readlien below, it is recommended to use strcpy(buf,line) to copy, and then operate buf.

  1. Use of readline
void input (char *buf)
{
    char *line, *s;
    struct passwd *pwd;
    struct hostent *hp;
    char *path;
    char *home;
    char st[256] = {0};
    char host[100] = {0};
    char N_path[2560]={0};//N_path[0]='~';
    char *p;
    char *q;
     home = (char *)malloc(20);
    line = (char *)malloc(256);
     path = (char *)malloc(2560);
    /*获取主机名称和用户名称*/
    if(gethostname(host,sizeof(host))<0)
    {
        perror("gethostname");
    }
    hp = gethostbyname(host);
    pwd = getpwuid(getuid());
    /*获取当前目录显示*/
    home = getenv("HOME");
    home[strlen(home)+1]='\0';
    p = home;
    getcwd(path,256);
    path[strlen(path)]='\0';
    q = path;
    while(*p!='\0')
    {
        p++;//home
        q++;//path
    }
    strcpy(N_path,q);
    N_path[strlen(N_path)+1]='\0';
    sprintf(st,"\033[;36m %s@%s \033[0m:\033[;34m ~%s \033[0m",pwd->pw_name,hp->h_name,N_path);
    initialize_readline();
    line = readline (st);
    //strcpy(buf,line);
    if (!line)
        return;
    s = stripwhite (line);
    strcpy(buf,s);
    if (*s)
    {
        add_history(s);
    }
    free(line);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326362845&siteId=291194637