2 * $Id: inputstr.c,v 1.72 2012/12/30 22:11:37 tom Exp $
4 * inputstr.c -- functions for input/display of a string
6 * Copyright 2000-2011,2012 Thomas E. Dickey
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License, version 2.1
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to
19 * Free Software Foundation, Inc.
20 * 51 Franklin St., Fifth Floor
21 * Boston, MA 02110, USA.
33 #if defined(HAVE_SEARCH_H) && defined(HAVE_TSEARCH)
43 #if defined(USE_WIDE_CURSES)
45 #elif defined(HAVE_XDIALOG)
46 #define USE_CACHING 1 /* editbox really needs caching! */
51 typedef struct _cache {
54 struct _cache *cache_at; /* unique: associate caches by CACHE */
55 const char *string_at; /* unique: associate caches by char* */
57 size_t s_len; /* strlen(string) - we add 1 for EOS */
58 size_t i_len; /* length(list) - we add 1 for EOS */
59 char *string; /* a copy of the last-processed string */
60 int *list; /* indices into the string */
64 #define SAME_CACHE(c,s,l) (c->string != 0 && memcmp(c->string,s,l) == 0)
66 static CACHE *cache_list;
69 static void *sorted_cache;
72 #ifdef USE_WIDE_CURSES
76 static int result = -1;
78 char *test = setlocale(LC_ALL, 0);
79 if (test == 0 || *test == 0) {
81 } else if (strcmp(test, "C") && strcmp(test, "POSIX")) {
93 compare_cache(const void *a, const void *b)
95 const CACHE *p = (const CACHE *) a;
96 const CACHE *q = (const CACHE *) b;
98 result = (int) (p->cache_at - q->cache_at);
100 result = (int) (p->string_at - q->string_at);
106 find_cache(CACHE * cache, const char *string)
114 memset(&find, 0, sizeof(find));
115 find.cache_at = cache;
116 find.string_at = string;
118 if ((pp = tfind(&find, &sorted_cache, compare_cache)) != 0) {
124 for (p = cache_list; p != 0; p = p->next) {
125 if (p->cache_at == cache
126 && p->string_at == string) {
135 make_cache(CACHE * cache, const char *string)
139 p = dlg_calloc(CACHE, 1);
140 assert_ptr(p, "load_cache");
141 p->next = cache_list;
145 p->string_at = string;
149 (void) tsearch(p, &sorted_cache, compare_cache);
154 load_cache(CACHE * cache, const char *string)
158 if ((p = find_cache(cache, string)) != 0) {
161 make_cache(cache, string);
166 save_cache(CACHE * cache, const char *string)
170 if ((p = find_cache(cache, string)) != 0) {
177 #define SAME_CACHE(c,s,l) (c->string != 0)
178 #define load_cache(cache, string) /* nothing */
179 #define save_cache(cache, string) /* nothing */
180 #endif /* USE_WIDE_CURSES */
183 * If the given string has not changed, we do not need to update the index.
184 * If we need to update the index, allocate enough memory for it.
187 same_cache2(CACHE * cache, const char *string, unsigned i_len)
190 size_t s_len = strlen(string);
192 if (cache->s_len != 0
193 && cache->s_len >= s_len
195 && SAME_CACHE(cache, string, (size_t) s_len)) {
200 if (cache->list == 0) {
201 cache->list = dlg_malloc(int, need);
202 } else if (cache->i_len < i_len) {
203 cache->list = dlg_realloc(int, need, cache->list);
205 cache->i_len = i_len;
207 if (cache->s_len >= s_len && cache->string != 0) {
208 strcpy(cache->string, string);
210 if (cache->string != 0)
212 cache->string = dlg_strclone(string);
214 cache->s_len = s_len;
219 #ifdef USE_WIDE_CURSES
221 * Like same_cache2(), but we are only concerned about caching a copy of the
222 * string and its associated length.
225 same_cache1(CACHE * cache, const char *string, size_t i_len)
227 size_t s_len = strlen(string);
229 if (cache->s_len == s_len
230 && SAME_CACHE(cache, string, (size_t) s_len)) {
234 if (cache->s_len >= s_len && cache->string != 0) {
235 strcpy(cache->string, string);
237 if (cache->string != 0)
239 cache->string = dlg_strclone(string);
241 cache->s_len = s_len;
242 cache->i_len = i_len;
246 #endif /* USE_CACHING */
249 * Counts the number of bytes that make up complete wide-characters, up to byte
250 * 'len'. If there is no locale set, simply return the original length.
252 #ifdef USE_WIDE_CURSES
254 dlg_count_wcbytes(const char *string, size_t len)
261 load_cache(&cache, string);
262 if (!same_cache1(&cache, string, len)) {
265 const char *src = cache.string;
267 char save = cache.string[len];
269 cache.string[len] = '\0';
270 memset(&state, 0, sizeof(state));
271 code = mbsrtowcs((wchar_t *) 0, &src, len, &state);
272 cache.string[len] = save;
273 if ((int) code >= 0) {
279 save_cache(&cache, string);
281 result = (int) cache.i_len;
287 #endif /* USE_WIDE_CURSES */
290 * Counts the number of wide-characters in the string.
293 dlg_count_wchars(const char *string)
297 #ifdef USE_WIDE_CURSES
300 size_t len = strlen(string);
302 load_cache(&cache, string);
303 if (!same_cache1(&cache, string, len)) {
304 const char *src = cache.string;
306 int part = dlg_count_wcbytes(cache.string, len);
307 char save = cache.string[part];
309 wchar_t *temp = dlg_calloc(wchar_t, len + 1);
312 cache.string[part] = '\0';
313 memset(&state, 0, sizeof(state));
314 code = mbsrtowcs(temp, &src, (size_t) part, &state);
315 cache.i_len = ((int) code >= 0) ? wcslen(temp) : 0;
316 cache.string[part] = save;
318 save_cache(&cache, string);
323 result = (int) cache.i_len;
325 #endif /* USE_WIDE_CURSES */
327 result = (int) strlen(string);
333 * Build an index of the wide-characters in the string, so we can easily tell
334 * which byte-offset begins a given wide-character.
337 dlg_index_wchars(const char *string)
340 unsigned len = (unsigned) dlg_count_wchars(string);
343 load_cache(&cache, string);
344 if (!same_cache2(&cache, string, len)) {
345 const char *current = string;
348 for (inx = 1; inx <= len; ++inx) {
349 #ifdef USE_WIDE_CURSES
353 memset(&state, 0, sizeof(state));
354 width = (int) mbrlen(current, strlen(current), &state);
356 width = 1; /* FIXME: what if we have a control-char? */
358 cache.list[inx] = cache.list[inx - 1] + width;
360 #endif /* USE_WIDE_CURSES */
363 cache.list[inx] = (int) inx;
366 save_cache(&cache, string);
372 * Given the character-offset to find in the list, return the corresponding
376 dlg_find_index(const int *list, int limit, int to_find)
379 for (result = 0; result <= limit; ++result) {
380 if (to_find == list[result]
382 || ((result < limit) && (to_find < list[result + 1]))) {
390 * Build a list of the display-columns for the given string's characters.
393 dlg_index_columns(const char *string)
396 unsigned len = (unsigned) dlg_count_wchars(string);
399 load_cache(&cache, string);
400 if (!same_cache2(&cache, string, len)) {
402 #ifdef USE_WIDE_CURSES
404 size_t num_bytes = strlen(string);
405 const int *inx_wchars = dlg_index_wchars(string);
408 for (inx = 0; inx < len; ++inx) {
413 if (string[inx_wchars[inx]] == TAB) {
414 result = ((cache.list[inx] | 7) + 1) - cache.list[inx];
416 memset(&state, 0, sizeof(state));
417 memset(temp, 0, sizeof(temp));
418 check = mbrtowc(temp,
419 string + inx_wchars[inx],
420 num_bytes - (size_t) inx_wchars[inx],
422 if ((int) check <= 0) {
425 result = wcwidth(temp[0]);
428 const wchar_t *printable;
429 cchar_t temp2, *temp2p = &temp2;
430 setcchar(temp2p, temp, 0, 0, 0);
431 printable = wunctrl(temp2p);
432 result = printable ? (int) wcslen(printable) : 1;
435 cache.list[inx + 1] = result;
437 cache.list[inx + 1] += cache.list[inx];
440 #endif /* USE_WIDE_CURSES */
442 for (inx = 0; inx < len; ++inx) {
443 chtype ch = UCH(string[inx]);
446 cache.list[inx + 1] =
447 ((cache.list[inx] | 7) + 1) - cache.list[inx];
448 else if (isprint(ch))
449 cache.list[inx + 1] = 1;
451 const char *printable;
452 printable = unctrl(ch);
453 cache.list[inx + 1] = (printable
454 ? (int) strlen(printable)
458 cache.list[inx + 1] += cache.list[inx];
461 save_cache(&cache, string);
467 * Returns the number of columns used for a string. That happens to be the
468 * end-value of the cols[] array.
471 dlg_count_columns(const char *string)
474 int limit = dlg_count_wchars(string);
476 const int *cols = dlg_index_columns(string);
477 result = cols[limit];
479 result = (int) strlen(string);
485 * Given a column limit, count the number of wide characters that can fit
486 * into that limit. The offset is used to skip over a leading character
487 * that was already written.
490 dlg_limit_columns(const char *string, int limit, int offset)
492 const int *cols = dlg_index_columns(string);
493 int result = dlg_count_wchars(string);
495 while (result > 0 && (cols[result] - cols[offset]) > limit)
501 * Updates the string and character-offset, given various editing characters
502 * or literal characters which are inserted at the character-offset.
505 dlg_edit_string(char *string, int *chr_offset, int key, int fkey, bool force)
508 int len = (int) strlen(string);
509 int limit = dlg_count_wchars(string);
510 const int *indx = dlg_index_wchars(string);
511 int offset = dlg_find_index(indx, limit, *chr_offset);
512 int max_len = dlg_max_input(MAX_LEN);
515 /* transform editing characters into equivalent function-keys */
517 fkey = TRUE; /* assume we transform */
523 fkey = FALSE; /* this is used for navigation */
526 fkey = FALSE; /* ...no, we did not transform */
533 case 0: /* special case for loop entry */
537 if (*chr_offset && offset > 0)
538 *chr_offset = indx[offset - 1];
540 case DLGK_GRID_RIGHT:
542 *chr_offset = indx[offset + 1];
550 *chr_offset = indx[limit];
552 case DLGK_DELETE_LEFT:
554 int gap = indx[offset] - indx[offset - 1];
555 *chr_offset = indx[offset - 1];
557 for (i = *chr_offset;
558 (string[i] = string[i + gap]) != '\0';
565 case DLGK_DELETE_RIGHT:
568 string[*chr_offset = 0] = '\0';
570 int gap = ((offset <= limit)
571 ? (indx[offset + 1] - indx[offset])
574 for (i = indx[offset];
575 (string[i] = string[i + gap]) != '\0';
579 } else if (offset > 0) {
580 string[indx[offset - 1]] = '\0';
582 if (*chr_offset > indx[limit])
583 *chr_offset = indx[limit];
587 case DLGK_DELETE_ALL:
588 string[*chr_offset = 0] = '\0';
600 case DLGK_FIELD_NEXT:
601 case DLGK_FIELD_PREV:
612 if (key == ESC || key == ERR) {
616 for (i = ++len; i > *chr_offset; i--)
617 string[i] = string[i - 1];
618 string[*chr_offset] = (char) key;
629 compute_edit_offset(const char *string,
635 const int *cols = dlg_index_columns(string);
636 const int *indx = dlg_index_wchars(string);
637 int limit = dlg_count_wchars(string);
638 int offset = dlg_find_index(indx, limit, chr_offset);
643 for (n = offset2 = 0; n <= offset; ++n) {
644 if ((cols[offset] - cols[n]) < x_last
645 && (offset == limit || (cols[offset + 1] - cols[n]) < x_last)) {
651 dpy_column = cols[offset] - cols[offset2];
653 if (p_dpy_column != 0)
654 *p_dpy_column = dpy_column;
655 if (p_scroll_amt != 0)
656 *p_scroll_amt = offset2;
660 * Given the character-offset in the string, returns the display-offset where
661 * we will position the cursor.
664 dlg_edit_offset(char *string, int chr_offset, int x_last)
668 compute_edit_offset(string, chr_offset, x_last, &result, 0);
674 * Displays the string, shifted as necessary, to fit within the box and show
675 * the current character-offset.
678 dlg_show_string(WINDOW *win,
679 const char *string, /* string to display (may be multibyte) */
680 int chr_offset, /* character (not bytes) offset */
681 chtype attr, /* window-attributes */
682 int y_base, /* beginning row on screen */
683 int x_base, /* beginning column on screen */
684 int x_last, /* number of columns on screen */
685 bool hidden, /* if true, do not echo */
686 bool force) /* if true, force repaint */
688 x_last = MIN(x_last + x_base, getmaxx(win)) - x_base;
690 if (hidden && !dialog_vars.insecure) {
692 (void) wmove(win, y_base, x_base);
696 const int *cols = dlg_index_columns(string);
697 const int *indx = dlg_index_wchars(string);
698 int limit = dlg_count_wchars(string);
704 compute_edit_offset(string, chr_offset, x_last, &input_x, &scrollamt);
706 (void) wattrset(win, attr);
707 (void) wmove(win, y_base, x_base);
708 for (i = scrollamt, k = 0; i < limit && k < x_last; ++i) {
709 int check = cols[i + 1] - cols[scrollamt];
710 if (check <= x_last) {
711 for (j = indx[i]; j < indx[i + 1]; ++j) {
712 chtype ch = UCH(string[j]);
713 if (hidden && dialog_vars.insecure) {
715 } else if (ch == TAB) {
716 int count = cols[i + 1] - cols[i];
730 (void) wmove(win, y_base, x_base + input_x);
737 _dlg_inputstr_leaks(void)
740 while (cache_list != 0) {
741 CACHE *next = cache_list->next;
743 tdelete(cache_list, &sorted_cache, compare_cache);
745 if (cache_list->string != 0)
746 free(cache_list->string);
747 if (cache_list->list != 0)
748 free(cache_list->list);
752 #endif /* USE_CACHING */
754 #endif /* NO_LEAKS */