5 #include <readline/readline.h>
6 #include <readline/history.h>
16 #define HISTORY_LEN 10
22 char **completion_list
= NULL
;
24 /* completion function */
26 char *generator (const char *text
, int state
)
28 static int index
, len
;
36 while ((name
= completion_list
[index
++])) {
37 if (strncmp (name
, text
, len
) == 0) {
45 char **completion (const char *text
, __attribute__((unused
)) int start
, __attribute__((unused
)) int end
)
47 rl_attempted_completion_over
= 1;
48 return rl_completion_matches (text
, generator
);
52 char *edit_line
= NULL
;
65 return rl_insert_text (edit_line
);
68 /* history management */
72 HIST_ENTRY
**entries
= history_list ();
73 if (entries
== NULL
) {
74 VERBOSE (WARNING
, fprintf (stdout
, "no history\n"));
77 while (entries
[i
] != NULL
) {
78 printf ("%d: %s\n", history_length
- i
, entries
[i
]->line
);
86 void init_read_line ()
89 completion_list
= generate_completion_list ();
90 rl_attempted_completion_function
= completion
;
93 rl_startup_hook
= edit_hook
;
95 /* readline parameters */
96 rl_variable_bind ("blink-matching-paren", "On");
97 //rl_set_screen_size (50, 40);
102 int read_line (char **buffer
, char *prompt
)
104 if ((*buffer
= readline (prompt
)) == NULL
) {
108 /* check empty line */
109 if (strlen (*buffer
) == 0) {
119 void manage_history (char *buffer
)
121 /* add line into history */
122 add_history (buffer
);
123 VERBOSE (INFO
, fprintf (stdout
, "line (%d/%d): '%s'\n",
124 where_history (), history_length
, buffer
));
125 if (history_length
> HISTORY_LEN
) {
126 HIST_ENTRY
*last
= remove_history (0);
128 free_history_entry (last
);
135 void clean_read_line (char *buffer
)
137 free_completion_list (completion_list
);
143 /* generate completion list */
145 char **generate_completion_list ()
148 char **list
= (char **) callocordie (NB_OPERATORS
+ NB_FUNCTIONS
+ NB_CONSTANTS
+ NB_SYMBOLS
+ 1, sizeof (char *));
150 for (i
= 0; i
< NB_OPERATORS
; i
++) {
151 list
[l
] = strdup ((operators
+ i
)->keyword
);
152 for (j
= 0; j
< (int)strlen (list
[l
]); j
++) {
153 if (list
[i
][j
] == '\t') {
157 if (list
[l
] != NULL
) {
162 for (i
= 0; i
< NB_FUNCTIONS
; i
++) {
163 list
[l
] = strdup ((functions
+ i
)->keyword
);
164 if (list
[l
] != NULL
) {
169 for (i
= 0; i
< NB_CONSTANTS
; i
++) {
170 list
[l
] = strdup ((constants
+ i
)->keyword
);
171 if (list
[l
] != NULL
) {
176 for (i
= 0; i
< NB_SYMBOLS
; i
++) {
177 list
[l
] = strdup (symbols
[i
]);
178 if (list
[l
] != NULL
) {
186 /* free completion list */
188 void free_completion_list (char **list
)
193 for (i
= 0; i
< NB_OPERATORS
+ NB_FUNCTIONS
+ NB_CONSTANTS
+ NB_SYMBOLS
+ 1; i
++) {
194 if (list
[i
] != NULL
) {
202 /* vim: set ts=4 sw=4 et: */