kernel - cleanup vfs_cache debugging
[dragonfly.git] / contrib / dialog / inputstr.c
blob080ec1f901e1759f13ef133a4da567da9b870600
1 /*
2 * $Id: inputstr.c,v 1.84 2014/09/01 16:11:08 tom Exp $
4 * inputstr.c -- functions for input/display of a string
6 * Copyright 2000-2013,2014 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.
24 #include <dialog.h>
25 #include <dlg_keys.h>
27 #include <errno.h>
29 #ifdef HAVE_SETLOCALE
30 #include <locale.h>
31 #endif
33 #if defined(HAVE_SEARCH_H) && defined(HAVE_TSEARCH)
34 #include <search.h>
35 #else
36 #undef HAVE_TSEARCH
37 #endif
39 #ifdef NEED_WCHAR_H
40 #include <wchar.h>
41 #endif
43 #if defined(USE_WIDE_CURSES)
44 #define USE_CACHING 1
45 #elif defined(HAVE_XDIALOG)
46 #define USE_CACHING 1 /* editbox really needs caching! */
47 #else
48 #define USE_CACHING 0
49 #endif
51 typedef struct _cache {
52 struct _cache *next;
53 #if USE_CACHING
54 int cache_num; /* tells what type of data is in list[] */
55 const char *string_at; /* unique: associate caches by char* */
56 #endif
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 */
61 } CACHE;
63 #if USE_CACHING
64 #define SAME_CACHE(c,s,l) (c->string != 0 && memcmp(c->string,s,l) == 0)
66 static CACHE *cache_list;
68 typedef enum {
69 cInxCols
70 ,cCntWideBytes
71 ,cCntWideChars
72 ,cInxWideChars
73 ,cMAX
74 } CACHE_USED;
76 #ifdef HAVE_TSEARCH
77 static void *sorted_cache;
78 #endif
80 #ifdef USE_WIDE_CURSES
81 static int
82 have_locale(void)
84 static int result = -1;
85 if (result < 0) {
86 char *test = setlocale(LC_ALL, 0);
87 if (test == 0 || *test == 0) {
88 result = FALSE;
89 } else if (strcmp(test, "C") && strcmp(test, "POSIX")) {
90 result = TRUE;
91 } else {
92 result = FALSE;
95 return result;
97 #endif
99 #ifdef HAVE_TSEARCH
101 #if 0
102 static void
103 show_tsearch(const void *nodep, const VISIT which, const int depth)
105 const CACHE *p = *(CACHE * const *) nodep;
106 (void) depth;
107 if (which == postorder || which == leaf) {
108 dlg_trace_msg("\tcache %p %p:%s\n", p, p->string, p->string);
112 static void
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);
119 #else
120 #define trace_cache(fn, ln) /* nothing */
121 #endif
123 #define CMP(a,b) (((a) > (b)) ? 1 : (((a) < (b)) ? -1 : 0))
125 static int
126 compare_cache(const void *a, const void *b)
128 const CACHE *p = (const CACHE *) a;
129 const CACHE *q = (const CACHE *) b;
130 int result = CMP(p->cache_num, q->cache_num);
131 if (result == 0)
132 result = CMP(p->string_at, q->string_at);
133 return result;
135 #endif
137 static CACHE *
138 find_cache(int cache_num, const char *string)
140 CACHE *p;
142 #ifdef HAVE_TSEARCH
143 void *pp;
144 CACHE find;
146 memset(&find, 0, sizeof(find));
147 find.cache_num = cache_num;
148 find.string_at = string;
150 if ((pp = tfind(&find, &sorted_cache, compare_cache)) != 0) {
151 p = *(CACHE **) pp;
152 } else {
153 p = 0;
155 #else
156 for (p = cache_list; p != 0; p = p->next) {
157 if (p->string_at == string) {
158 break;
161 #endif
162 return p;
165 static CACHE *
166 make_cache(int cache_num, const char *string)
168 CACHE *p;
170 p = dlg_calloc(CACHE, 1);
171 assert_ptr(p, "load_cache");
172 p->next = cache_list;
173 cache_list = p;
175 p->cache_num = cache_num;
176 p->string_at = string;
178 #ifdef HAVE_TSEARCH
179 (void) tsearch(p, &sorted_cache, compare_cache);
180 #endif
181 return p;
184 static CACHE *
185 load_cache(int cache_num, const char *string)
187 CACHE *p;
189 if ((p = find_cache(cache_num, string)) == 0) {
190 p = make_cache(cache_num, string);
192 return p;
194 #else
195 static CACHE my_cache;
196 #define SAME_CACHE(c,s,l) (c->string != 0)
197 #define load_cache(cache, string) &my_cache
198 #endif /* USE_CACHING */
201 * If the given string has not changed, we do not need to update the index.
202 * If we need to update the index, allocate enough memory for it.
204 static bool
205 same_cache2(CACHE * cache, const char *string, unsigned i_len)
207 unsigned need;
208 size_t s_len = strlen(string);
209 bool result = TRUE;
211 if (cache->s_len == 0
212 || cache->s_len < s_len
213 || cache->list == 0
214 || !SAME_CACHE(cache, string, (size_t) s_len)) {
216 need = (i_len + 1);
217 if (cache->list == 0) {
218 cache->list = dlg_malloc(int, need);
219 } else if (cache->i_len < i_len) {
220 cache->list = dlg_realloc(int, need, cache->list);
222 assert_ptr(cache->list, "load_cache");
223 cache->i_len = i_len;
225 if (cache->s_len >= s_len && cache->string != 0) {
226 strcpy(cache->string, string);
227 } else {
228 if (cache->string != 0)
229 free(cache->string);
230 cache->string = dlg_strclone(string);
232 cache->s_len = s_len;
234 result = FALSE;
236 return result;
239 #ifdef USE_WIDE_CURSES
241 * Like same_cache2(), but we are only concerned about caching a copy of the
242 * string and its associated length.
244 static bool
245 same_cache1(CACHE * cache, const char *string, size_t i_len)
247 size_t s_len = strlen(string);
248 bool result = TRUE;
250 if (cache->s_len != s_len
251 || !SAME_CACHE(cache, string, (size_t) s_len)) {
253 if (cache->s_len >= s_len && cache->string != 0) {
254 strcpy(cache->string, string);
255 } else {
256 if (cache->string != 0)
257 free(cache->string);
258 cache->string = dlg_strclone(string);
260 cache->s_len = s_len;
261 cache->i_len = i_len;
263 result = FALSE;
265 return result;
267 #endif /* USE_CACHING */
270 * Counts the number of bytes that make up complete wide-characters, up to byte
271 * 'len'. If there is no locale set, simply return the original length.
273 #ifdef USE_WIDE_CURSES
274 static int
275 dlg_count_wcbytes(const char *string, size_t len)
277 int result;
279 if (have_locale()) {
280 CACHE *cache = load_cache(cCntWideBytes, string);
281 if (!same_cache1(cache, string, len)) {
282 while (len != 0) {
283 size_t code = 0;
284 const char *src = cache->string;
285 mbstate_t state;
286 char save = cache->string[len];
288 cache->string[len] = '\0';
289 memset(&state, 0, sizeof(state));
290 code = mbsrtowcs((wchar_t *) 0, &src, len, &state);
291 cache->string[len] = save;
292 if ((int) code >= 0) {
293 break;
295 --len;
297 cache->i_len = len;
299 result = (int) cache->i_len;
300 } else {
301 result = (int) len;
303 return result;
305 #endif /* USE_WIDE_CURSES */
308 * Counts the number of wide-characters in the string.
311 dlg_count_wchars(const char *string)
313 int result;
314 #ifdef USE_WIDE_CURSES
316 if (have_locale()) {
317 size_t len = strlen(string);
318 CACHE *cache = load_cache(cCntWideChars, string);
320 if (!same_cache1(cache, string, len)) {
321 const char *src = cache->string;
322 mbstate_t state;
323 int part = dlg_count_wcbytes(cache->string, len);
324 char save = cache->string[part];
325 size_t code;
326 wchar_t *temp = dlg_calloc(wchar_t, len + 1);
328 if (temp != 0) {
329 cache->string[part] = '\0';
330 memset(&state, 0, sizeof(state));
331 code = mbsrtowcs(temp, &src, (size_t) part, &state);
332 cache->i_len = ((int) code >= 0) ? wcslen(temp) : 0;
333 cache->string[part] = save;
334 free(temp);
335 } else {
336 cache->i_len = 0;
339 result = (int) cache->i_len;
340 } else
341 #endif /* USE_WIDE_CURSES */
343 result = (int) strlen(string);
345 return result;
349 * Build an index of the wide-characters in the string, so we can easily tell
350 * which byte-offset begins a given wide-character.
352 const int *
353 dlg_index_wchars(const char *string)
355 unsigned len = (unsigned) dlg_count_wchars(string);
356 unsigned inx;
357 CACHE *cache = load_cache(cInxWideChars, string);
359 if (!same_cache2(cache, string, len)) {
360 const char *current = string;
362 cache->list[0] = 0;
363 for (inx = 1; inx <= len; ++inx) {
364 #ifdef USE_WIDE_CURSES
365 if (have_locale()) {
366 mbstate_t state;
367 int width;
368 memset(&state, 0, sizeof(state));
369 width = (int) mbrlen(current, strlen(current), &state);
370 if (width <= 0)
371 width = 1; /* FIXME: what if we have a control-char? */
372 current += width;
373 cache->list[inx] = cache->list[inx - 1] + width;
374 } else
375 #endif /* USE_WIDE_CURSES */
377 (void) current;
378 cache->list[inx] = (int) inx;
382 return cache->list;
386 * Given the character-offset to find in the list, return the corresponding
387 * array index.
390 dlg_find_index(const int *list, int limit, int to_find)
392 int result;
393 for (result = 0; result <= limit; ++result) {
394 if (to_find == list[result]
395 || result == limit
396 || ((result < limit) && (to_find < list[result + 1]))) {
397 break;
400 return result;
404 * Build a list of the display-columns for the given string's characters.
406 const int *
407 dlg_index_columns(const char *string)
409 unsigned len = (unsigned) dlg_count_wchars(string);
410 unsigned inx;
411 CACHE *cache = load_cache(cInxCols, string);
413 if (!same_cache2(cache, string, len)) {
414 cache->list[0] = 0;
415 #ifdef USE_WIDE_CURSES
416 if (have_locale()) {
417 size_t num_bytes = strlen(string);
418 const int *inx_wchars = dlg_index_wchars(string);
419 mbstate_t state;
421 for (inx = 0; inx < len; ++inx) {
422 wchar_t temp[2];
423 size_t check;
424 int result;
426 if (string[inx_wchars[inx]] == TAB) {
427 result = ((cache->list[inx] | 7) + 1) - cache->list[inx];
428 } else {
429 memset(&state, 0, sizeof(state));
430 memset(temp, 0, sizeof(temp));
431 check = mbrtowc(temp,
432 string + inx_wchars[inx],
433 num_bytes - (size_t) inx_wchars[inx],
434 &state);
435 if ((int) check <= 0) {
436 result = 1;
437 } else {
438 result = wcwidth(temp[0]);
440 if (result < 0) {
441 const wchar_t *printable;
442 cchar_t temp2, *temp2p = &temp2;
443 setcchar(temp2p, temp, 0, 0, 0);
444 printable = wunctrl(temp2p);
445 result = printable ? (int) wcslen(printable) : 1;
448 cache->list[inx + 1] = result;
449 if (inx != 0)
450 cache->list[inx + 1] += cache->list[inx];
452 } else
453 #endif /* USE_WIDE_CURSES */
455 for (inx = 0; inx < len; ++inx) {
456 chtype ch = UCH(string[inx]);
458 if (ch == TAB)
459 cache->list[inx + 1] =
460 ((cache->list[inx] | 7) + 1) - cache->list[inx];
461 else if (isprint(ch))
462 cache->list[inx + 1] = 1;
463 else {
464 const char *printable;
465 printable = unctrl(ch);
466 cache->list[inx + 1] = (printable
467 ? (int) strlen(printable)
468 : 1);
470 if (inx != 0)
471 cache->list[inx + 1] += cache->list[inx];
475 return cache->list;
479 * Returns the number of columns used for a string. That happens to be the
480 * end-value of the cols[] array.
483 dlg_count_columns(const char *string)
485 int result = 0;
486 int limit = dlg_count_wchars(string);
487 if (limit > 0) {
488 const int *cols = dlg_index_columns(string);
489 result = cols[limit];
490 } else {
491 result = (int) strlen(string);
493 dlg_finish_string(string);
494 return result;
498 * Given a column limit, count the number of wide characters that can fit
499 * into that limit. The offset is used to skip over a leading character
500 * that was already written.
503 dlg_limit_columns(const char *string, int limit, int offset)
505 const int *cols = dlg_index_columns(string);
506 int result = dlg_count_wchars(string);
508 while (result > 0 && (cols[result] - cols[offset]) > limit)
509 --result;
510 return result;
514 * Updates the string and character-offset, given various editing characters
515 * or literal characters which are inserted at the character-offset.
517 bool
518 dlg_edit_string(char *string, int *chr_offset, int key, int fkey, bool force)
520 int i;
521 int len = (int) strlen(string);
522 int limit = dlg_count_wchars(string);
523 const int *indx = dlg_index_wchars(string);
524 int offset = dlg_find_index(indx, limit, *chr_offset);
525 int max_len = dlg_max_input(MAX_LEN);
526 bool edit = TRUE;
528 /* transform editing characters into equivalent function-keys */
529 if (!fkey) {
530 fkey = TRUE; /* assume we transform */
531 switch (key) {
532 case 0:
533 break;
534 case ESC:
535 case TAB:
536 fkey = FALSE; /* this is used for navigation */
537 break;
538 default:
539 fkey = FALSE; /* ...no, we did not transform */
540 break;
544 if (fkey) {
545 switch (key) {
546 case 0: /* special case for loop entry */
547 edit = force;
548 break;
549 case DLGK_GRID_LEFT:
550 if (*chr_offset && offset > 0)
551 *chr_offset = indx[offset - 1];
552 break;
553 case DLGK_GRID_RIGHT:
554 if (offset < limit)
555 *chr_offset = indx[offset + 1];
556 break;
557 case DLGK_BEGIN:
558 if (*chr_offset)
559 *chr_offset = 0;
560 break;
561 case DLGK_FINAL:
562 if (offset < limit)
563 *chr_offset = indx[limit];
564 break;
565 case DLGK_DELETE_LEFT:
566 if (offset) {
567 int gap = indx[offset] - indx[offset - 1];
568 *chr_offset = indx[offset - 1];
569 if (gap > 0) {
570 for (i = *chr_offset;
571 (string[i] = string[i + gap]) != '\0';
572 i++) {
577 break;
578 case DLGK_DELETE_RIGHT:
579 if (limit) {
580 if (--limit == 0) {
581 string[*chr_offset = 0] = '\0';
582 } else {
583 int gap = ((offset <= limit)
584 ? (indx[offset + 1] - indx[offset])
585 : 0);
586 if (gap > 0) {
587 for (i = indx[offset];
588 (string[i] = string[i + gap]) != '\0';
589 i++) {
592 } else if (offset > 0) {
593 string[indx[offset - 1]] = '\0';
595 if (*chr_offset > indx[limit])
596 *chr_offset = indx[limit];
599 break;
600 case DLGK_DELETE_ALL:
601 string[*chr_offset = 0] = '\0';
602 break;
603 case DLGK_ENTER:
604 edit = 0;
605 break;
606 #ifdef KEY_RESIZE
607 case KEY_RESIZE:
608 edit = 0;
609 break;
610 #endif
611 case DLGK_GRID_UP:
612 case DLGK_GRID_DOWN:
613 case DLGK_FIELD_NEXT:
614 case DLGK_FIELD_PREV:
615 edit = 0;
616 break;
617 case ERR:
618 edit = 0;
619 break;
620 default:
621 beep();
622 break;
624 } else {
625 if (key == ESC || key == ERR) {
626 edit = 0;
627 } else {
628 if (len < max_len) {
629 for (i = ++len; i > *chr_offset; i--)
630 string[i] = string[i - 1];
631 string[*chr_offset] = (char) key;
632 *chr_offset += 1;
633 } else {
634 (void) beep();
638 return edit;
641 static void
642 compute_edit_offset(const char *string,
643 int chr_offset,
644 int x_last,
645 int *p_dpy_column,
646 int *p_scroll_amt)
648 const int *cols = dlg_index_columns(string);
649 const int *indx = dlg_index_wchars(string);
650 int limit = dlg_count_wchars(string);
651 int offset = dlg_find_index(indx, limit, chr_offset);
652 int offset2;
653 int dpy_column;
654 int n;
656 for (n = offset2 = 0; n <= offset; ++n) {
657 if ((cols[offset] - cols[n]) < x_last
658 && (offset == limit || (cols[offset + 1] - cols[n]) < x_last)) {
659 offset2 = n;
660 break;
664 dpy_column = cols[offset] - cols[offset2];
666 if (p_dpy_column != 0)
667 *p_dpy_column = dpy_column;
668 if (p_scroll_amt != 0)
669 *p_scroll_amt = offset2;
673 * Given the character-offset in the string, returns the display-offset where
674 * we will position the cursor.
677 dlg_edit_offset(char *string, int chr_offset, int x_last)
679 int result;
681 compute_edit_offset(string, chr_offset, x_last, &result, 0);
683 return result;
687 * Displays the string, shifted as necessary, to fit within the box and show
688 * the current character-offset.
690 void
691 dlg_show_string(WINDOW *win,
692 const char *string, /* string to display (may be multibyte) */
693 int chr_offset, /* character (not bytes) offset */
694 chtype attr, /* window-attributes */
695 int y_base, /* beginning row on screen */
696 int x_base, /* beginning column on screen */
697 int x_last, /* number of columns on screen */
698 bool hidden, /* if true, do not echo */
699 bool force) /* if true, force repaint */
701 x_last = MIN(x_last + x_base, getmaxx(win)) - x_base;
703 if (hidden && !dialog_vars.insecure) {
704 if (force) {
705 (void) wmove(win, y_base, x_base);
706 wrefresh(win);
708 } else {
709 const int *cols = dlg_index_columns(string);
710 const int *indx = dlg_index_wchars(string);
711 int limit = dlg_count_wchars(string);
713 int i, j, k;
714 int input_x;
715 int scrollamt;
717 compute_edit_offset(string, chr_offset, x_last, &input_x, &scrollamt);
719 (void) wattrset(win, attr);
720 (void) wmove(win, y_base, x_base);
721 for (i = scrollamt, k = 0; i < limit && k < x_last; ++i) {
722 int check = cols[i + 1] - cols[scrollamt];
723 if (check <= x_last) {
724 for (j = indx[i]; j < indx[i + 1]; ++j) {
725 chtype ch = UCH(string[j]);
726 if (hidden && dialog_vars.insecure) {
727 waddch(win, '*');
728 } else if (ch == TAB) {
729 int count = cols[i + 1] - cols[i];
730 while (--count >= 0)
731 waddch(win, ' ');
732 } else {
733 waddch(win, ch);
736 k = check;
737 } else {
738 break;
741 while (k++ < x_last)
742 waddch(win, ' ');
743 (void) wmove(win, y_base, x_base + input_x);
744 wrefresh(win);
749 * Discard cached data for the given string.
751 void
752 dlg_finish_string(const char *string)
754 #if USE_CACHING
755 if ((string != 0) && dialog_state.finish_string) {
756 CACHE *p = cache_list;
757 CACHE *q = 0;
758 CACHE *r;
760 while (p != 0) {
761 if (p->string_at == string) {
762 #ifdef HAVE_TSEARCH
763 if (tdelete(p, &sorted_cache, compare_cache) == 0) {
764 continue;
766 trace_cache(__FILE__, __LINE__);
767 #endif
768 if (p->string != 0)
769 free(p->string);
770 if (p->list != 0)
771 free(p->list);
772 if (p == cache_list) {
773 cache_list = p->next;
774 r = cache_list;
775 } else {
776 q->next = p->next;
777 r = q;
779 free(p);
780 p = r;
781 } else {
782 q = p;
783 p = p->next;
787 #else
788 (void) string;
789 #endif
792 #ifdef NO_LEAKS
793 void
794 _dlg_inputstr_leaks(void)
796 #if USE_CACHING
797 dialog_state.finish_string = TRUE;
798 trace_cache(__FILE__, __LINE__);
799 while (cache_list != 0) {
800 dlg_finish_string(cache_list->string_at);
802 #endif /* USE_CACHING */
804 #endif /* NO_LEAKS */