As I was doing a project, I came to know about how command line can be read using ncurses and GNU's readline library. However I could not find either in Ubuntu (16.04). I am curious to know how Ubuntu processes the command as the user types? For eg: how does it detect up/ down arrow being pressed, how is Tab detected etc?
1 Answer
Ubuntu the operating system does not read command lines. Some programs read command lines. For example, bash is a command interpreter (also known as a shell) which reads command lines. When the shell is interactive and reads command lines from a terminal it uses the GNU readline library.
$ 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()"
-
(Right, I should have meant the shell! My mistake). Well even my understanding was that it uses GNU readline().But I was surprised that I could not find the the readline folder. I was trying to write my own shell using readline () but it gave an error saying it could not find readline. So I am a really puzzled.hrushi– hrushi2017-02-22 14:13:24 +00:00Commented Feb 22, 2017 at 14:13
-
@hrushi:
sudo apt-get install libreadline5-dev(orlibreadline6-dev, depending on what version you want). In general, if you want to use a system component in your own programs you need to install the corresponding-dev(for development) package to get the header files and manual pages.AlexP– AlexP2017-02-22 15:20:27 +00:00Commented Feb 22, 2017 at 15:20 -
"In general, if you want to use a system component in your own programs you need to install..." this was what I wasn't knowing and looking for! I have now installed it but I still having issue with linking. This command did not help: gcc -o sample -lreadline sample.c. How do I get the binary file?hrushi– hrushi2017-02-22 16:47:28 +00:00Commented Feb 22, 2017 at 16:47
-
Well it seems the readline was not installed?! I do not see it in /usr/local/lib... What am I missinghrushi– hrushi2017-02-22 16:55:36 +00:00Commented Feb 22, 2017 at 16:55
-
@hrushi: I have edited the answer to include a full example.AlexP– AlexP2017-02-22 17:11:05 +00:00Commented Feb 22, 2017 at 17:11