]>
git.ipfire.org Git - thirdparty/bird.git/blob - client/birdc.c
2 * BIRD Client - Readline variant I/O
4 * (c) 1999--2004 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
14 #include <readline/readline.h>
15 #include <readline/history.h>
18 #include "nest/bird.h"
19 #include "lib/resource.h"
20 #include "lib/string.h"
21 #include "client/client.h"
23 static int input_hidden_end
;
24 static int prompt_active
;
28 /* HACK: libreadline internals we need to access */
29 extern int _rl_vis_botlin
;
30 extern void _rl_move_vert(int);
32 #define HISTORY "/.birdc_history"
33 static char *history_file
;
36 add_history_dedup(char *cmd
)
38 /* Add history line if it differs from the last one */
39 HIST_ENTRY
*he
= history_get(history_length
);
40 if (!he
|| strcmp(he
->line
, cmd
))
45 input_got_line(char *cmd_buffer
)
55 add_history_dedup(cmd_buffer
);
56 submit_command(cmd_buffer
);
63 input_start_list(void)
65 /* Leave the currently edited line and make space for listing */
66 _rl_move_vert(_rl_vis_botlin
);
75 /* Reprint the currently edited line after listing */
81 input_complete(int arg UNUSED
, int key UNUSED
)
83 static int complete_flag
;
86 if (rl_last_func
!= input_complete
)
88 switch (cmd_complete(rl_line_buffer
, rl_point
, buf
, complete_flag
))
106 input_help(int arg
, int key UNUSED
)
108 int i
, in_string
, in_bracket
;
111 return rl_insert(arg
, '?');
113 in_string
= in_bracket
= 0;
114 for (i
= 0; i
< rl_point
; i
++)
117 if (rl_line_buffer
[i
] == '"')
118 in_string
= ! in_string
;
119 else if (! in_string
)
121 if (rl_line_buffer
[i
] == '[')
123 else if (rl_line_buffer
[i
] == ']')
128 /* `?' inside string or path -> insert */
129 if (in_string
|| in_bracket
)
130 return rl_insert(1, '?');
132 rl_begin_undo_group(); /* HACK: We want to display `?' at point position */
137 cmd_help(rl_line_buffer
, rl_point
);
138 rl_undo_command(1, 0);
146 const char *homedir
= getenv("HOME");
149 history_file
= malloc(strlen(homedir
) + sizeof(HISTORY
));
151 die("couldn't alloc enough memory for history file name");
153 sprintf(history_file
, "%s%s", homedir
, HISTORY
);
154 read_history(history_file
);
162 rl_readline_name
= "birdc";
163 rl_add_defun("bird-complete", input_complete
, '\t');
164 rl_add_defun("bird-help", input_help
, '?');
165 rl_callback_handler_install("bird> ", input_got_line
);
167 // rl_get_screen_size();
173 // readline library does strange things when stdin is nonblocking.
174 // if (fcntl(0, F_SETFL, O_NONBLOCK) < 0)
181 /* need this, otherwise some lib seems to eat pending output when
182 the prompt is displayed */
184 tcdrain(STDOUT_FILENO
);
186 rl_end
= input_hidden_end
;
187 rl_expand_prompt("bird> ");
188 rl_forced_update_display();
196 input_hidden_end
= rl_end
;
198 rl_expand_prompt("");
205 input_notify(int prompt
)
207 if (prompt
== prompt_active
)
219 rl_callback_read_char();
240 write_history(history_file
);
241 rl_callback_handler_remove();