2 * $Id: inputstr.c,v 1.83 2013/09/23 23:19:26 tom Exp $
4 * inputstr.c -- functions for input/display of a string
6 * Copyright 2000-2012,2013 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 int cache_num; /* tells what type of data is in list[] */
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;
77 static void *sorted_cache;
80 #ifdef USE_WIDE_CURSES
84 static int result = -1;
86 char *test = setlocale(LC_ALL, 0);
87 if (test == 0 || *test == 0) {
89 } else if (strcmp(test, "C") && strcmp(test, "POSIX")) {
103 show_tsearch(const void *nodep, const VISIT which, const int depth)
105 const CACHE *p = *(CACHE * const *) nodep;
107 if (which == postorder || which == leaf) {
108 dlg_trace_msg("\tcache %p %p:%s\n", p, p->string, p->string);
113 trace_cache(const char *fn, int ln)
115 dlg_trace_msg("trace_cache %s@%d\n", fn, ln);
116 twalk(sorted_cache, show_tsearch);
120 #define trace_cache(fn, ln) /* nothing */
124 compare_cache(const void *a, const void *b)
126 const CACHE *p = (const CACHE *) a;
127 const CACHE *q = (const CACHE *) b;
128 int result = (p->cache_num - q->cache_num);
130 result = (int) (p->string_at - q->string_at);
136 find_cache(int cache_num, const char *string)
144 memset(&find, 0, sizeof(find));
145 find.cache_num = cache_num;
146 find.string_at = string;
148 if ((pp = tfind(&find, &sorted_cache, compare_cache)) != 0) {
154 for (p = cache_list; p != 0; p = p->next) {
155 if (p->string_at == string) {
164 make_cache(int cache_num, const char *string)
168 p = dlg_calloc(CACHE, 1);
169 assert_ptr(p, "load_cache");
170 p->next = cache_list;
173 p->cache_num = cache_num;
174 p->string_at = string;
177 (void) tsearch(p, &sorted_cache, compare_cache);
183 load_cache(int cache_num, const char *string)
187 if ((p = find_cache(cache_num, string)) == 0) {
188 p = make_cache(cache_num, string);
193 static CACHE my_cache;
194 #define SAME_CACHE(c,s,l) (c->string != 0)
195 #define load_cache(cache, string) &my_cache
196 #endif /* USE_CACHING */
199 * If the given string has not changed, we do not need to update the index.
200 * If we need to update the index, allocate enough memory for it.
203 same_cache2(CACHE * cache, const char *string, unsigned i_len)
206 size_t s_len = strlen(string);
209 if (cache->s_len == 0
210 || cache->s_len < s_len
212 || !SAME_CACHE(cache, string, (size_t) s_len)) {
215 if (cache->list == 0) {
216 cache->list = dlg_malloc(int, need);
217 } else if (cache->i_len < i_len) {
218 cache->list = dlg_realloc(int, need, cache->list);
220 assert_ptr(cache->list, "load_cache");
221 cache->i_len = i_len;
223 if (cache->s_len >= s_len && cache->string != 0) {
224 strcpy(cache->string, string);
226 if (cache->string != 0)
228 cache->string = dlg_strclone(string);
230 cache->s_len = s_len;
237 #ifdef USE_WIDE_CURSES
239 * Like same_cache2(), but we are only concerned about caching a copy of the
240 * string and its associated length.
243 same_cache1(CACHE * cache, const char *string, size_t i_len)
245 size_t s_len = strlen(string);
248 if (cache->s_len != s_len
249 || !SAME_CACHE(cache, string, (size_t) s_len)) {
251 if (cache->s_len >= s_len && cache->string != 0) {
252 strcpy(cache->string, string);
254 if (cache->string != 0)
256 cache->string = dlg_strclone(string);
258 cache->s_len = s_len;
259 cache->i_len = i_len;
265 #endif /* USE_CACHING */
268 * Counts the number of bytes that make up complete wide-characters, up to byte
269 * 'len'. If there is no locale set, simply return the original length.
271 #ifdef USE_WIDE_CURSES
273 dlg_count_wcbytes(const char *string, size_t len)
278 CACHE *cache = load_cache(cCntWideBytes, string);
279 if (!same_cache1(cache, string, len)) {
282 const char *src = cache->string;
284 char save = cache->string[len];
286 cache->string[len] = '\0';
287 memset(&state, 0, sizeof(state));
288 code = mbsrtowcs((wchar_t *) 0, &src, len, &state);
289 cache->string[len] = save;
290 if ((int) code >= 0) {
297 result = (int) cache->i_len;
303 #endif /* USE_WIDE_CURSES */
306 * Counts the number of wide-characters in the string.
309 dlg_count_wchars(const char *string)
312 #ifdef USE_WIDE_CURSES
315 size_t len = strlen(string);
316 CACHE *cache = load_cache(cCntWideChars, string);
318 if (!same_cache1(cache, string, len)) {
319 const char *src = cache->string;
321 int part = dlg_count_wcbytes(cache->string, len);
322 char save = cache->string[part];
324 wchar_t *temp = dlg_calloc(wchar_t, len + 1);
327 cache->string[part] = '\0';
328 memset(&state, 0, sizeof(state));
329 code = mbsrtowcs(temp, &src, (size_t) part, &state);
330 cache->i_len = ((int) code >= 0) ? wcslen(temp) : 0;
331 cache->string[part] = save;
337 result = (int) cache->i_len;
339 #endif /* USE_WIDE_CURSES */
341 result = (int) strlen(string);
347 * Build an index of the wide-characters in the string, so we can easily tell
348 * which byte-offset begins a given wide-character.
351 dlg_index_wchars(const char *string)
353 unsigned len = (unsigned) dlg_count_wchars(string);
355 CACHE *cache = load_cache(cInxWideChars, string);
357 if (!same_cache2(cache, string, len)) {
358 const char *current = string;
361 for (inx = 1; inx <= len; ++inx) {
362 #ifdef USE_WIDE_CURSES
366 memset(&state, 0, sizeof(state));
367 width = (int) mbrlen(current, strlen(current), &state);
369 width = 1; /* FIXME: what if we have a control-char? */
371 cache->list[inx] = cache->list[inx - 1] + width;
373 #endif /* USE_WIDE_CURSES */
376 cache->list[inx] = (int) inx;
384 * Given the character-offset to find in the list, return the corresponding
388 dlg_find_index(const int *list, int limit, int to_find)
391 for (result = 0; result <= limit; ++result) {
392 if (to_find == list[result]
394 || ((result < limit) && (to_find < list[result + 1]))) {
402 * Build a list of the display-columns for the given string's characters.
405 dlg_index_columns(const char *string)
407 unsigned len = (unsigned) dlg_count_wchars(string);
409 CACHE *cache = load_cache(cInxCols, string);
411 if (!same_cache2(cache, string, len)) {
413 #ifdef USE_WIDE_CURSES
415 size_t num_bytes = strlen(string);
416 const int *inx_wchars = dlg_index_wchars(string);
419 for (inx = 0; inx < len; ++inx) {
424 if (string[inx_wchars[inx]] == TAB) {
425 result = ((cache->list[inx] | 7) + 1) - cache->list[inx];
427 memset(&state, 0, sizeof(state));
428 memset(temp, 0, sizeof(temp));
429 check = mbrtowc(temp,
430 string + inx_wchars[inx],
431 num_bytes - (size_t) inx_wchars[inx],
433 if ((int) check <= 0) {
436 result = wcwidth(temp[0]);
439 const wchar_t *printable;
440 cchar_t temp2, *temp2p = &temp2;
441 setcchar(temp2p, temp, 0, 0, 0);
442 printable = wunctrl(temp2p);
443 result = printable ? (int) wcslen(printable) : 1;
446 cache->list[inx + 1] = result;
448 cache->list[inx + 1] += cache->list[inx];
451 #endif /* USE_WIDE_CURSES */
453 for (inx = 0; inx < len; ++inx) {
454 chtype ch = UCH(string[inx]);
457 cache->list[inx + 1] =
458 ((cache->list[inx] | 7) + 1) - cache->list[inx];
459 else if (isprint(ch))
460 cache->list[inx + 1] = 1;
462 const char *printable;
463 printable = unctrl(ch);
464 cache->list[inx + 1] = (printable
465 ? (int) strlen(printable)
469 cache->list[inx + 1] += cache->list[inx];
477 * Returns the number of columns used for a string. That happens to be the
478 * end-value of the cols[] array.
481 dlg_count_columns(const char *string)
484 int limit = dlg_count_wchars(string);
486 const int *cols = dlg_index_columns(string);
487 result = cols[limit];
489 result = (int) strlen(string);
491 dlg_finish_string(string);
496 * Given a column limit, count the number of wide characters that can fit
497 * into that limit. The offset is used to skip over a leading character
498 * that was already written.
501 dlg_limit_columns(const char *string, int limit, int offset)
503 const int *cols = dlg_index_columns(string);
504 int result = dlg_count_wchars(string);
506 while (result > 0 && (cols[result] - cols[offset]) > limit)
512 * Updates the string and character-offset, given various editing characters
513 * or literal characters which are inserted at the character-offset.
516 dlg_edit_string(char *string, int *chr_offset, int key, int fkey, bool force)
519 int len = (int) strlen(string);
520 int limit = dlg_count_wchars(string);
521 const int *indx = dlg_index_wchars(string);
522 int offset = dlg_find_index(indx, limit, *chr_offset);
523 int max_len = dlg_max_input(MAX_LEN);
526 /* transform editing characters into equivalent function-keys */
528 fkey = TRUE; /* assume we transform */
534 fkey = FALSE; /* this is used for navigation */
537 fkey = FALSE; /* ...no, we did not transform */
544 case 0: /* special case for loop entry */
548 if (*chr_offset && offset > 0)
549 *chr_offset = indx[offset - 1];
551 case DLGK_GRID_RIGHT:
553 *chr_offset = indx[offset + 1];
561 *chr_offset = indx[limit];
563 case DLGK_DELETE_LEFT:
565 int gap = indx[offset] - indx[offset - 1];
566 *chr_offset = indx[offset - 1];
568 for (i = *chr_offset;
569 (string[i] = string[i + gap]) != '\0';
576 case DLGK_DELETE_RIGHT:
579 string[*chr_offset = 0] = '\0';
581 int gap = ((offset <= limit)
582 ? (indx[offset + 1] - indx[offset])
585 for (i = indx[offset];
586 (string[i] = string[i + gap]) != '\0';
590 } else if (offset > 0) {
591 string[indx[offset - 1]] = '\0';
593 if (*chr_offset > indx[limit])
594 *chr_offset = indx[limit];
598 case DLGK_DELETE_ALL:
599 string[*chr_offset = 0] = '\0';
611 case DLGK_FIELD_NEXT:
612 case DLGK_FIELD_PREV:
623 if (key == ESC || key == ERR) {
627 for (i = ++len; i > *chr_offset; i--)
628 string[i] = string[i - 1];
629 string[*chr_offset] = (char) key;
640 compute_edit_offset(const char *string,
646 const int *cols = dlg_index_columns(string);
647 const int *indx = dlg_index_wchars(string);
648 int limit = dlg_count_wchars(string);
649 int offset = dlg_find_index(indx, limit, chr_offset);
654 for (n = offset2 = 0; n <= offset; ++n) {
655 if ((cols[offset] - cols[n]) < x_last
656 && (offset == limit || (cols[offset + 1] - cols[n]) < x_last)) {
662 dpy_column = cols[offset] - cols[offset2];
664 if (p_dpy_column != 0)
665 *p_dpy_column = dpy_column;
666 if (p_scroll_amt != 0)
667 *p_scroll_amt = offset2;
671 * Given the character-offset in the string, returns the display-offset where
672 * we will position the cursor.
675 dlg_edit_offset(char *string, int chr_offset, int x_last)
679 compute_edit_offset(string, chr_offset, x_last, &result, 0);
685 * Displays the string, shifted as necessary, to fit within the box and show
686 * the current character-offset.
689 dlg_show_string(WINDOW *win,
690 const char *string, /* string to display (may be multibyte) */
691 int chr_offset, /* character (not bytes) offset */
692 chtype attr, /* window-attributes */
693 int y_base, /* beginning row on screen */
694 int x_base, /* beginning column on screen */
695 int x_last, /* number of columns on screen */
696 bool hidden, /* if true, do not echo */
697 bool force) /* if true, force repaint */
699 x_last = MIN(x_last + x_base, getmaxx(win)) - x_base;
701 if (hidden && !dialog_vars.insecure) {
703 (void) wmove(win, y_base, x_base);
707 const int *cols = dlg_index_columns(string);
708 const int *indx = dlg_index_wchars(string);
709 int limit = dlg_count_wchars(string);
715 compute_edit_offset(string, chr_offset, x_last, &input_x, &scrollamt);
717 (void) wattrset(win, attr);
718 (void) wmove(win, y_base, x_base);
719 for (i = scrollamt, k = 0; i < limit && k < x_last; ++i) {
720 int check = cols[i + 1] - cols[scrollamt];
721 if (check <= x_last) {
722 for (j = indx[i]; j < indx[i + 1]; ++j) {
723 chtype ch = UCH(string[j]);
724 if (hidden && dialog_vars.insecure) {
726 } else if (ch == TAB) {
727 int count = cols[i + 1] - cols[i];
741 (void) wmove(win, y_base, x_base + input_x);
747 * Discard cached data for the given string.
750 dlg_finish_string(const char *string)
753 if ((string != 0) && dialog_state.finish_string) {
754 CACHE *p = cache_list;
759 if (p->string_at == string) {
761 if (tdelete(p, &sorted_cache, compare_cache) == 0) {
764 trace_cache(__FILE__, __LINE__);
770 if (p == cache_list) {
771 cache_list = p->next;
792 _dlg_inputstr_leaks(void)
795 dialog_state.finish_string = TRUE;
796 trace_cache(__FILE__, __LINE__);
797 while (cache_list != 0) {
798 dlg_finish_string(cache_list->string_at);
800 #endif /* USE_CACHING */
802 #endif /* NO_LEAKS */