사용 가능한 외부 함수 |
---|
readline, rl_on_new_line, rl_replace_line, rl_redisplay, add_history, printf, malloc, free, write, open, read, close, fork, wait, waitpid, wait3, wait4, signal, kill, exit, getcwd, chdir, stat, lstat, fstat, unlink, execve, dup, dup2, pipe, opendir, readdir, closedir, strerror, errno, isatty, ttyname, ttyslot, ioctl, getenv, tcsetattr, tcgetattr, tgetent, tgetflag, tgetnum, tgetstr, tgoto, tputs |
아래 명령어를 통해 쉽게 설치할 수 있습니다.
brew install readline
GNU Library
의 readline
을 이용할 때 FILE
구조체를 이용하는데, 이 구조체는 <stdio.h>
내에 존재합니다. 그렇기 <readline/readline.h>
를 포함하기 전에 <stdio.h>
가 선언되어야 호풀하려는 함수들이 FILE 구조체를 적절하게 이용할 수 있습니다.
입력받은 문자열을 저장하고 그 메모리 주소를 반환합니다.
#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
char *readline(const char *prompt);
한 줄의 문자를 입력받고 반환합니다.
malloc()
에 의해 할당되므로 free()
해야 합니다.EOF
를 만나면 NULL
을 반환합니다.lreadline
컴파일 플러그를 사용하여 컴파일 해야합니다. (gcc *.c -lreadline
)vi
혹은 emacs
의 Editing
을 지원합니다.Ctrl + D
를 입력해도 EOF
로써 인식이 안되는데 이는 readline
이 Editing
방식에 따른 Key Binding
을 지원하기 때문입니다.readline디렉토리 내에서 update와 관련된 함수들에게 커서가 개행 문자를 통해 다음 줄로 이동했음을 알려줄 때 이용되는 함수입니다.
#include <readline/readline.h>
int rl_on_new_line (void);
개행 문자 출력 이후에 이용됩니다.
rl_line_buffer
의 내용을 text
라는 문자열로 바꿉니다. 가능한 경우 포인트와 마크가 유지됩니다.
#include <readline/readline.h>
void rl_replace_line(const char *text, int clear_undo);
clear_undo
는 내부적으로 유지 중인 undo_list를 초기화할 지의 여부를 결정 합니다.
clear_undo
의 값이 0
이라면 초기화 하지 않습니다.clear_undo
의 값이 0
이외의 값이라면 초기화 합니다.사용자가 입력하여 유지 중인 rl_line_buffer
의 값을 프롬프트와 함께 출력합니다. 이 때 프롬프트 값은 readline 함수에 propt로 준 문자열로 이동합니다.
#include <readline/readline.h>
void rl_redisplay(void);
시그널을 받았을 때의 상황에서 rl_redisplay를 이용하게 됩니다.
사용자가 입력했던 문자열을 방향키를 통해 readline
함수 실행 도중에 다시 얻을 수 있게 합니다.
#include <readline/history.h>
int add_history(const char *line);
void add_history(const char *line);
Unix 계열에 내정된 readline
디렉토리를 이용할 경우 int 타입을 반환합니다.
0
을 반환합니다.-1
을 반환합니다.#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <readline/readline.h>
#include <readline/history.h>
int main()
{
char* input, shell_prompt[100];
// Configure readline to auto-complete paths when the tab key is hit.
rl_bind_key('\t', rl_complete);
while(1) {
// Create prompt string from user name and current working directory.
snprintf(shell_prompt, sizeof(shell_prompt), "%s:%s $ ", getenv("USER"), getcwd(NULL, 1024));
// Display prompt and read input (n.b. input must be freed after use)...
input = readline(shell_prompt);
// Check for EOF.
if (!input)
break;
// Add input to history.
add_history(input);
// Do stuff...
// Free input.
free(input);
}
}