5 #include <readline/readline.h>
6 #include <readline/history.h>
15 #define HISTORY_LEN 10
21 char **completion_list
= NULL
;
23 /* completion function */
25 char *generator (const char *text
, int state
)
27 static int index
, len
;
35 while ((name
= completion_list
[index
++])) {
36 if (strncmp (name
, text
, len
) == 0) {
44 char **completion (const char *text
, __attribute__((unused
)) int start
, __attribute__((unused
)) int end
)
46 rl_attempted_completion_over
= 1;
47 return rl_completion_matches (text
, generator
);
51 char *edit_line
= NULL
;
64 return rl_insert_text (edit_line
);
67 /* history management */
71 HIST_ENTRY
**entries
= history_list ();
72 if (entries
== NULL
) {
73 VERBOSE (WARNING
, fprintf (stdout
, "no history\n"));
76 while (entries
[i
] != NULL
) {
77 printf ("%d: %s\n", history_length
- i
, entries
[i
]->line
);
85 void init_read_line ()
88 completion_list
= generate_completion_list ();
89 rl_attempted_completion_function
= completion
;
92 rl_startup_hook
= edit_hook
;
94 /* readline parameters */
95 rl_variable_bind ("blink-matching-paren", "On");
96 //rl_set_screen_size (50, 40);
101 int read_line (char **buffer
, char *prompt
)
103 if ((*buffer
= readline (prompt
)) == NULL
) {
107 /* check empty line */
108 if (strlen (*buffer
) == 0) {
118 void manage_history (char *buffer
)
120 /* add line into history */
121 add_history (buffer
);
122 VERBOSE (INFO
, fprintf (stdout
, "line (%d/%d): '%s'\n",
123 where_history (), history_length
, buffer
));
124 if (history_length
> HISTORY_LEN
) {
125 HIST_ENTRY
*last
= remove_history (0);
127 free_history_entry (last
);
134 void clean_read_line (char *buffer
)
136 free_completion_list (completion_list
);