From 5a98927effb5e16d7127a4ba47492436753f1314 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Fri, 5 Nov 2021 14:38:11 +0100 Subject: [PATCH] ajout des descriptions des functions de readline --- README.md | 19 ++++++++---- test_rl.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ trap | 13 ++++++++ 3 files changed, 118 insertions(+), 6 deletions(-) create mode 100644 test_rl.c create mode 100644 trap diff --git a/README.md b/README.md index 51dc27d..27e4f4e 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,9 @@ +## understanding some things : +- [how to send EOF to stdin of a shell process](https://unix.stackexchange.com/questions/493578/how-to-send-d-eot-character-to-stdin-of-a-shell-process) + - [get root access 1](https://vitux.com/how-to-become-root-user-in-ubuntu-command-line-using-su-and-sudo/) + - [get root access using sudo su](https://superuser.com/questions/408990/how-do-i-log-out-of-sudo-su) +- [what is a tty](https://www.howtogeek.com/428174/what-is-a-tty-on-linux-and-how-to-use-the-tty-command/) + ## sommaire : --- - [1. todo list :](#markdown-header-1-todo-list) @@ -62,11 +68,11 @@ (extracts of manuals) ### readline : - **readline** : `char *readline (const char *prompt);` will read a line from the terminal and return it, using prompt as a prompt -- **rl_clear_history** : ` ` -- **rl_on_new_line** : ` ` -- **rl_replace_line** : ` ` -- **rl_redisplay** : ` ` -- **add_history** : ` ` +- **rl_clear_history** : `void rl_clear_history (void)` clear the history list by deleting all of the entries, in the same manner as the History library's clear_history() function +- **rl_on_new_line** : `int rl_on_new_line (void)` tell the update functions that we have moved onto a new (empty) line, usually after outputting a newline +- **rl_replace_line** : `void rl_replace_line (const char *text, int clear_undo)` replace the contents of rl_line_buffer with text. The point and mark are preserved, if possible. If clear_undo is non-zero, the undo list associated with the current line is cleared +- **rl_redisplay** : `void rl_redisplay (void)` change what's displayed on the screen to reflect the current contents of rl_line_buffer +- **add_history** : `bool readline_add_history (string $prompt)` save the line away in a history list of such lines ### files : - **access** : `int access(const char *pathname, int mode);` checks whether the calling process can access the file pathname - **open** : `int open(const char *pathname, int flags, [mode_t mode]);` system call opens the file specified by pathname @@ -101,7 +107,8 @@ - **strerror** : `char *strerror(int errnum);` returns a pointer to a string that describes the error code passed in the argument errnum - **perror** : `void perror(const char *s);` produces a message on standard error describing the last error encountered during a call to a system or library function ### termcap : -The termcap data base describes the capabilities of hundreds of different display terminals in great detail The termcap library is provided for easy access this data base in programs that want to do terminal-independent character-based display output +The termcap data base describes the capabilities of hundreds of different display terminals in great detail +The termcap library is provided for easy access this data base in programs that want to do terminal-independent character-based display output - **tgetent** : `int tgetent (char *buffer, char *termtype);` finds the description of the terminal type and remembers it internally so that you can interrogate it about specific terminal capabilities diff --git a/test_rl.c b/test_rl.c new file mode 100644 index 0000000..d0046c7 --- /dev/null +++ b/test_rl.c @@ -0,0 +1,92 @@ +# include +# include +# include +# include +# include +//# include +//# include +//# include +//# include +//# include +//# include +//# include +//# include +//# include +//# include +//# include +//# include +//# include +//# include + +void handler_sigint(int id) +{ + (void)id; + write(1, "\n", 1); +} + +int main(int argc, char *argv[], char *envp[]) +{ + (void)argc; + (void)argv; + char *line_input; + char *prompt; + +// t_cmd **cmd_arr; +// char **envp; +// char *prompt_base; +// char *prompt; +// t_token *token_list; +// int last_exit_status; +// struct termios ori_termios; +// struct termios interactive_termios; +// int termios_changed; +// struct sigaction ori_signal_behaviour; +// struct sigaction signal_behaviour; + + signal(SIGINT, handler_sigint); + line_input = NULL; + prompt = strdup("\e[0;31mtest>\e[0m "); + while (1) + { + if (line_input) + free(line_input); + line_input = readline(prompt); + if (line_input && *line_input) + { + // A faire aprés être sortie du mode interactif + // - Ignorer tout les signaux + // - Remettre ori_termios +// c->signal_behaviour.sa_handler = SIG_IGN; +// sigaction(SIGINT, &c->signal_behaviour, NULL); +// sigaction(SIGQUIT, &c->signal_behaviour, NULL); +// tcsetattr(STDIN_FILENO, TCSANOW, &c->ori_termios); +// if (!fork()) +// { +// char *arg_test[3]; +// +// arg_test[0] = ft_strdup("sleep"); +// arg_test[1] = ft_strdup("3"); +// arg_test[2] = NULL; +// sigaction(SIGQUIT, &c->ori_signal_behaviour, NULL); +// sigaction(SIGINT, &c->ori_signal_behaviour, NULL); +// execve("/bin/sleep", arg_test, c->envp); +// } +// else +// { +// int wait_test; +// wait(&wait_test); +// c->signal_behaviour.sa_handler = sigint_handler; +// sigaction(SIGINT, &c->signal_behaviour, NULL); +// c->signal_behaviour.sa_handler = SIG_IGN; +// sigaction(SIGQUIT, &c->signal_behaviour, NULL); +// tcsetattr(STDIN_FILENO, TCSANOW, &c->interactive_termios); +// } + + printf("%s\n", line_input); + } + else if (!line_input) + write(1, "\n", 1); + } + + return (0); +} diff --git a/trap b/trap new file mode 100644 index 0000000..f89ed5a --- /dev/null +++ b/trap @@ -0,0 +1,13 @@ +#!/bin/bash + +# trap ctrl-c and call ctrl_c() +trap ctrl_c INT + +function ctrl_c() { + echo "** Trapped CTRL-C" +} + +for i in `seq 1 5`; do + sleep 1 + echo -n "." +done