I have saved the history of the previous commands in a two-dimensional array. But I don't know how to check the up arrow.
How to use the C programming language implementing this feature (in Linux)?
I have saved the history of the previous commands in a two-dimensional array. But I don't know how to check the up arrow.
How to use the C programming language implementing this feature (in Linux)?
Thanks to n.m. for good advice.
The following is the example of usage 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;
}
to compile this example:
1) install readline library
apt-get install libreadline-dev
2) compile program as
gcc rltest.c -I/usr/include/readline -lreadline
Try the following example:
#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
}