0

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?

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
hrushi
  • 111

1 Answers1

1

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()"
AlexP
  • 10,455
  • (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 Feb 22 '17 at 14:13
  • @hrushi: sudo apt-get install libreadline5-dev (or libreadline6-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 Feb 22 '17 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 Feb 22 '17 at 16:47
  • Well it seems the readline was not installed?! I do not see it in /usr/local/lib... What am I missing – hrushi Feb 22 '17 at 16:55
  • @hrushi: I have edited the answer to include a full example. – AlexP Feb 22 '17 at 17:11
  • @hrushi: The easiest way to find out what files are installed where by a package on Ubuntu is to use Ubuntu Packages Search. – AlexP Feb 22 '17 at 17:23
  • thanks a ton for the detailed example @AlexP! It finally works for me – hrushi Feb 22 '17 at 17:48