프로젝트를 진행하면서 ncurses와 GNU의 readline 라이브러리를 사용하여 명령줄을 읽는 방법을 배우기 시작했습니다. 하지만 우분투(16.04)에서는 찾을 수 없습니다. 사용자가 입력한 명령을 Ubuntu가 어떻게 처리하는지 궁금합니다. 예를 들어 위쪽/아래쪽 화살표를 눌렀는지 어떻게 감지하고 탭 등을 감지합니까?
답변1
Ubuntu 운영 체제는 명령줄을 읽지 않습니다. 일부 프로그램은 명령줄을 읽습니다. 예를 들어,bash
명령 해석기(또는껍데기) 명령줄을 읽습니다. 쉘이 대화형이고 터미널에서 명령줄을 읽을 때 GNU를 사용합니다.readline
도서관.
$ sudo apt-get -y install libreadline6-dev readline-doc
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
libtinfo-dev
The following NEW packages will be installed:
libreadline6-dev libtinfo-dev readline-doc
0 upgraded, 3 newly installed, 0 to remove and 1 not upgraded.
Need to get 299 kB of archives.
After this operation, 1,233 kB of additional disk space will be used.
...
$ sudo dpkg-query -l '*readline*'
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
+++-==============-============-============-=================================
un libreadline-co <none> <none> (no description available)
un libreadline-gp <none> <none> (no description available)
un libreadline4 <none> <none> (no description available)
ii libreadline5:a 5.2+dfsg-3bu amd64 GNU readline and history librarie
un libreadline5-d <none> <none> (no description available)
ii libreadline6:a 6.3-8ubuntu2 amd64 GNU readline and history librarie
ii libreadline6-d 6.3-8ubuntu2 amd64 GNU readline and history librarie
un libterm-readli <none> <none> (no description available)
un libterm-readli <none> <none> (no description available)
un php-readline <none> <none> (no description available)
ii php7.0-readlin 7.0.13-0ubun amd64 readline module for PHP
ii readline-commo 6.3-8ubuntu2 all GNU readline and history librarie
ii readline-doc 6.3-8ubuntu2 all GNU readline and history librarie
un tcl-tclreadlin <none> <none> (no description available)
$ cat trl.c
#include <stdio.h>
#include <stdlib.h>
#include <readline/readline.h>
int main(void)
{
char * line = readline("Enter some text: ");
if (line) {
printf("You have entered \"%s\"\n", line);
}
return EXIT_SUCCESS;
}
$ gcc -Wall trl.c -o trl -lreadline
$ ./trl
Enter some text: Some text to be read by readline()
You have entered "Some text to be read by readline()"