1 /*
2 * (c) 2004 by Kalle Wallin <kaw@linux.se>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 */
19 #include "wreadln.h"
20 #include "charset.h"
21 #include "screen_utils.h"
22 #include "config.h"
24 #include <stdlib.h>
25 #include <string.h>
26 #include <glib.h>
28 #define KEY_CTRL_A 1
29 #define KEY_CTRL_B 2
30 #define KEY_CTRL_C 3
31 #define KEY_CTRL_D 4
32 #define KEY_CTRL_E 5
33 #define KEY_CTRL_F 6
34 #define KEY_CTRL_G 7
35 #define KEY_CTRL_K 11
36 #define KEY_CTRL_N 14
37 #define KEY_CTRL_P 16
38 #define KEY_CTRL_U 21
39 #define KEY_CTRL_Z 26
40 #define KEY_BCKSPC 8
41 #define TAB 9
43 #define WRLN_MAX_LINE_SIZE 1024
44 #define WRLN_MAX_HISTORY_LENGTH 32
46 guint wrln_max_line_size = WRLN_MAX_LINE_SIZE;
47 guint wrln_max_history_length = WRLN_MAX_HISTORY_LENGTH;
48 void *wrln_completion_callback_data = NULL;
49 wrln_gcmp_pre_cb_t wrln_pre_completion_callback = NULL;
50 wrln_gcmp_post_cb_t wrln_post_completion_callback = NULL;
52 /* move the cursor one step to the right */
53 static inline void cursor_move_right(gint *cursor,
54 gint *start,
55 gint width,
56 gint x0,
57 gint x1,
58 gchar *line)
59 {
60 if (*cursor < (int)strlen(line) &&
61 *cursor < (int)wrln_max_line_size - 1) {
62 (*cursor)++;
63 if (*cursor + x0 >= x1 && *start < *cursor - width + 1)
64 (*start)++;
65 }
66 }
68 /* move the cursor one step to the left */
69 static inline void cursor_move_left(gint *cursor,
70 gint *start)
71 {
72 if (*cursor > 0) {
73 if (*cursor == *start && *start > 0)
74 (*start)--;
75 (*cursor)--;
76 }
77 }
79 /* move the cursor to the end of the line */
80 static inline void cursor_move_to_eol(gint *cursor,
81 gint *start,
82 gint width,
83 gint x0,
84 gint x1,
85 gchar *line)
86 {
87 *cursor = strlen(line);
88 if (*cursor + x0 >= x1)
89 *start = *cursor - width + 1;
90 }
92 /* draw line buffer and update cursor position */
93 static inline void drawline(gint cursor,
94 gint start,
95 gint width,
96 gint x0,
97 gint y,
98 gboolean masked,
99 gchar *line,
100 WINDOW *w)
101 {
102 wmove(w, y, x0);
103 /* clear input area */
104 whline(w, ' ', width);
105 /* print visible part of the line buffer */
106 if(masked == TRUE)
107 whline(w, '*', utf8_width(line) - start);
108 else
109 waddnstr(w, line+start, width);
110 /* move the cursor to the correct position */
111 wmove(w, y, x0 + cursor-start);
112 /* tell ncurses to redraw the screen */
113 doupdate();
114 }
116 /* libcurses version */
118 static gchar *
119 _wreadln(WINDOW *w,
120 const gchar *prompt,
121 const gchar *initial_value,
122 gint x1,
123 GList **history,
124 GCompletion *gcmp,
125 gboolean masked)
126 {
127 GList *hlist = NULL, *hcurrent = NULL;
128 gchar *line;
129 gint x0, y, width;
130 gint cursor = 0, start = 0;
131 gint key = 0, i;
133 /* allocate a line buffer */
134 line = g_malloc0(wrln_max_line_size);
135 /* turn off echo */
136 noecho();
137 /* make shure the cursor is visible */
138 curs_set(1);
139 /* print prompt string */
140 if (prompt)
141 waddstr(w, prompt);
142 /* retrive y and x0 position */
143 getyx(w, y, x0);
144 /* check the x1 value */
145 if (x1 <= x0 || x1 > COLS)
146 x1 = COLS;
147 width = x1 - x0;
148 /* clear input area */
149 mvwhline(w, y, x0, ' ', width);
151 if (history) {
152 /* append the a new line to our history list */
153 *history = g_list_append(*history, g_malloc0(wrln_max_line_size));
154 /* hlist points to the current item in the history list */
155 hlist = g_list_last(*history);
156 hcurrent = hlist;
157 }
159 if (initial_value == (char *)-1) {
160 /* get previous history entry */
161 if (history && hlist->prev) {
162 if (hlist == hcurrent)
163 /* save the current line */
164 g_strlcpy(hlist->data, line, wrln_max_line_size);
166 /* get previous line */
167 hlist = hlist->prev;
168 g_strlcpy(line, hlist->data, wrln_max_line_size);
169 }
170 cursor_move_to_eol(&cursor, &start, width, x0, x1, line);
171 drawline(cursor, start, width, x0, y, masked, line, w);
172 } else if (initial_value) {
173 /* copy the initial value to the line buffer */
174 g_strlcpy(line, initial_value, wrln_max_line_size);
175 cursor_move_to_eol(&cursor, &start, width, x0, x1, line);
176 drawline(cursor, start, width, x0, y, masked, line, w);
177 }
179 while (key != 13 && key != '\n') {
180 key = wgetch(w);
182 /* check if key is a function key */
183 for (i = 0; i < 63; i++)
184 if (key == KEY_F(i)) {
185 key = KEY_F(1);
186 i = 64;
187 }
189 switch (key) {
190 #ifdef HAVE_GETMOUSE
191 case KEY_MOUSE: /* ignore mouse events */
192 #endif
193 case ERR: /* ingnore errors */
194 break;
196 case TAB:
197 if (gcmp) {
198 char *prefix = NULL;
199 GList *list;
201 if (wrln_pre_completion_callback)
202 wrln_pre_completion_callback(gcmp, line,
203 wrln_completion_callback_data);
204 list = g_completion_complete(gcmp, line, &prefix);
205 if (prefix) {
206 g_strlcpy(line, prefix, wrln_max_line_size);
207 cursor_move_to_eol(&cursor, &start, width, x0, x1, line);
208 g_free(prefix);
209 } else
210 screen_bell();
212 if (wrln_post_completion_callback)
213 wrln_post_completion_callback(gcmp, line, list,
214 wrln_completion_callback_data);
215 }
216 break;
218 case KEY_CTRL_G:
219 screen_bell();
220 g_free(line);
221 if (history) {
222 g_free(hcurrent->data);
223 hcurrent->data = NULL;
224 *history = g_list_delete_link(*history, hcurrent);
225 }
226 return NULL;
228 case KEY_LEFT:
229 case KEY_CTRL_B:
230 cursor_move_left(&cursor, &start);
231 break;
232 case KEY_RIGHT:
233 case KEY_CTRL_F:
234 cursor_move_right(&cursor, &start, width, x0, x1, line);
235 break;
236 case KEY_HOME:
237 case KEY_CTRL_A:
238 cursor = 0;
239 start = 0;
240 break;
241 case KEY_END:
242 case KEY_CTRL_E:
243 cursor_move_to_eol(&cursor, &start, width, x0, x1, line);
244 break;
245 case KEY_CTRL_K:
246 line[cursor] = 0;
247 break;
248 case KEY_CTRL_U:
249 cursor = utf8_width(line);
250 for (i = 0;i < cursor; i++)
251 line[i] = '\0';
252 cursor = 0;
253 break;
254 case 127:
255 case KEY_BCKSPC: /* handle backspace: copy all */
256 case KEY_BACKSPACE: /* chars starting from curpos */
257 if( cursor > 0 ) {/* - 1 from buf[n+1] to buf */
258 for (i = cursor - 1; line[i] != 0; i++)
259 line[i] = line[i + 1];
260 cursor_move_left(&cursor, &start);
261 }
262 break;
263 case KEY_DC: /* handle delete key. As above */
264 case KEY_CTRL_D:
265 if (cursor <= (gint)utf8_width(line) - 1) {
266 for (i = cursor; line[i] != 0; i++)
267 line[i] = line[i + 1];
268 }
269 break;
270 case KEY_UP:
271 case KEY_CTRL_P:
272 /* get previous history entry */
273 if (history && hlist->prev) {
274 if (hlist == hcurrent)
275 /* save the current line */
276 g_strlcpy(hlist->data, line, wrln_max_line_size);
278 /* get previous line */
279 hlist = hlist->prev;
280 g_strlcpy(line, hlist->data, wrln_max_line_size);
281 }
282 cursor_move_to_eol(&cursor, &start, width, x0, x1, line);
283 break;
284 case KEY_DOWN:
285 case KEY_CTRL_N:
286 /* get next history entry */
287 if (history && hlist->next) {
288 /* get next line */
289 hlist = hlist->next;
290 g_strlcpy(line, hlist->data, wrln_max_line_size);
291 }
292 cursor_move_to_eol(&cursor, &start, width, x0, x1, line);
293 break;
295 case '\n':
296 case 13:
297 case KEY_IC:
298 case KEY_PPAGE:
299 case KEY_NPAGE:
300 case KEY_F(1):
301 /* ignore char */
302 break;
303 default:
304 if (key >= 32) {
305 if (strlen (line + cursor)) { /* if the cursor is */
306 /* not at the last pos */
307 gchar *tmp = NULL;
308 gsize size = strlen(line + cursor) + 1;
310 tmp = g_malloc0(size);
311 g_strlcpy (tmp, line + cursor, size);
312 line[cursor] = key;
313 line[cursor + 1] = 0;
314 g_strlcat (&line[cursor + 1], tmp, size);
315 g_free(tmp);
316 cursor_move_right(&cursor, &start, width, x0, x1, line);
317 } else {
318 line[cursor + 1] = 0;
319 line[cursor] = key;
320 cursor_move_right(&cursor, &start, width, x0, x1, line);
321 }
322 }
323 }
325 drawline(cursor, start, width, x0, y, masked, line, w);
326 }
328 /* update history */
329 if (history) {
330 if (strlen(line)) {
331 /* update the current history entry */
332 size_t size = strlen(line)+1;
333 hcurrent->data = g_realloc(hcurrent->data, size);
334 g_strlcpy(hcurrent->data, line, size);
335 } else {
336 /* the line was empty - remove the current history entry */
337 g_free(hcurrent->data);
338 hcurrent->data = NULL;
339 *history = g_list_delete_link(*history, hcurrent);
340 }
342 while (g_list_length(*history) > wrln_max_history_length) {
343 GList *first = g_list_first(*history);
345 /* remove the oldest history entry */
346 g_free(first->data);
347 first->data = NULL;
348 *history = g_list_delete_link(*history, first);
349 }
350 }
352 return g_realloc(line, strlen(line)+1);
353 }
355 gchar *
356 wreadln(WINDOW *w,
357 const gchar *prompt,
358 const gchar *initial_value,
359 gint x1,
360 GList **history,
361 GCompletion *gcmp)
362 {
363 return _wreadln(w, prompt, initial_value, x1, history, gcmp, FALSE);
364 }
366 gchar *
367 wreadln_masked(WINDOW *w,
368 const gchar *prompt,
369 const gchar *initial_value,
370 gint x1,
371 GList **history,
372 GCompletion *gcmp)
373 {
374 return _wreadln(w, prompt, initial_value, x1, history, gcmp, TRUE);
375 }