[c] GNU Readline, History Library

숭글·2022년 10월 10일
0

readline

#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>

char *readline (const char *prompt);

readline will read a line from the terminal and return it, using prompt as a prompt. If prompt is NULL or the empty string, no prompt is issued. The line returned is allocated with malloc(3); the caller must free it when finished. The line returned has the final newline removed, so only the text of the line remains.

RETURN VALUE

: readline returns the text of the line read. A blank line returns the empty string. If EOF is encountered while reading a line, and the line is empty, NULL is returned. If an EOF is read with a non-empty line, it is treated as a newline.

EXAMPLE

char *line = readline ("Enter a line: ");

Introduction to Line Editing

The text C-k is read as ‘Control-K’ and describes the character produced when the k key is pressed while the Control key is depressed.

The text M-k is read as ‘Meta-K’ and describes the character produced when the Meta key (if you have one) is depressed, and the k key is pressed. The Meta key is labeled ALT(= Meta key mostly) on many keyboards. On keyboards with two keys labeled ALT (usually to either side of the space bar), the ALT on the left side is generally set to work as a Meta key. The ALT key on the right may also be configured to work as a Meta key or may be configured as some other modifier, such as a Compose key for typing accented characters.

more info

Commands For Manipulating The History

readline alse provides some key combination to move history.

  • previous-history (C-p)
    Move ‘back’ through the history list, fetching the previous command.

  • next-history (C-n)
    Move ‘forward’ through the history list, fetching the next command.

  • beginning-of-history (M-<)
    Move to the first line in the history.

  • end-of-history (M->)
    Move to the end of the input history, i.e., the line currently being entered.

  • reverse-search-history (C-r)
    Search backward starting at the current line and moving ‘up’ through the history as necessary. This is an incremental search. This command sets the region to the matched text and activates the mark.

  • forward-search-history (C-s)
    Search forward starting at the current line and moving ‘down’ through the history as necessary. This is an incremental search. This command sets the region to the matched text and activates the mark.

GNU History Library

The GNU History library is able to keep track of those lines, associate arbitrary data with each line, and utilize information from previous lines in composing new ones.

#include <readline/history.h>

History Storage

The history list is an array of history entries. A history entry is declared as follows:

typedef void * histdata_t;

       typedef struct _hist_entry {
         char *line;
         char *timestamp;
         histdata_t data;
       } HIST_ENTRY;

HIST_ENTRY ** the_history_list;

The history list itself might therefore be declared as

HIST_ENTRY **the_history_list;

History List Management Functions

void add_history (char *string)

Place string at the end of the history list. The associated data field (if any) is set to NULL.

HIST_ENTRY * remove_history (int which)

Remove history entry at offset which from the history. The removed element is returned so you can free the line, data, and containing structure.

... more functions

History Variables

int history_base

The logical offset of the first entry in the history list.

int history_length

The number of entries currently stored in the history list.

int max_input_history

The maximum number of history entries. This must be changed using stifle_history ().

... more variables


📖 GNU Readline Library
📖 Programming with GNU History

profile
Hi!😁 I'm Soongle. Welcome to my Velog!!!

0개의 댓글