xref: /freebsd/contrib/dialog/inputstr.c (revision 4c1a82cea504df7a79f5bd8f7d0a41cacccff16e)
1 /*
2  *  $Id: inputstr.c,v 1.88 2018/06/18 22:10:54 tom Exp $
3  *
4  *  inputstr.c -- functions for input/display of a string
5  *
6  *  Copyright 2000-2017,2018	Thomas E. Dickey
7  *
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.
11  *
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.
16  *
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.
22  */
23 
24 #include <dialog.h>
25 #include <dlg_keys.h>
26 
27 #include <errno.h>
28 
29 #ifdef HAVE_SETLOCALE
30 #include <locale.h>
31 #endif
32 
33 #if defined(HAVE_SEARCH_H) && defined(HAVE_TSEARCH)
34 #include <search.h>
35 #else
36 #undef HAVE_TSEARCH
37 #endif
38 
39 #ifdef NEED_WCHAR_H
40 #include <wchar.h>
41 #endif
42 
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
50 
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;
62 
63 #if USE_CACHING
64 #define SAME_CACHE(c,s,l) (c->string != 0 && memcmp(c->string,s,l) == 0)
65 
66 static CACHE *cache_list;
67 
68 typedef enum {
69     cInxCols
70     ,cCntWideBytes
71     ,cCntWideChars
72     ,cInxWideChars
73     ,cMAX
74 } CACHE_USED;
75 
76 #ifdef HAVE_TSEARCH
77 static void *sorted_cache;
78 #endif
79 
80 #ifdef USE_WIDE_CURSES
81 static int
82 have_locale(void)
83 {
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;
93 	}
94     }
95     return result;
96 }
97 #endif
98 
99 #ifdef HAVE_TSEARCH
100 
101 #if 0
102 static void
103 show_tsearch(const void *nodep, const VISIT which, const int depth)
104 {
105     const CACHE *p = *(CACHE * const *) nodep;
106     (void) depth;
107     if (which == postorder || which == leaf) {
108 	DLG_TRACE(("# cache %p %p:%s\n", p, p->string, p->string));
109     }
110 }
111 
112 static void
113 trace_cache(const char *fn, int ln)
114 {
115     DLG_TRACE(("# trace_cache %s@%d\n", fn, ln));
116     twalk(sorted_cache, show_tsearch);
117 }
118 
119 #else
120 #define trace_cache(fn, ln)	/* nothing */
121 #endif
122 
123 #define CMP(a,b) (((a) > (b)) ? 1 : (((a) < (b)) ? -1 : 0))
124 
125 static int
126 compare_cache(const void *a, const void *b)
127 {
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;
134 }
135 #endif
136 
137 static CACHE *
138 find_cache(int cache_num, const char *string)
139 {
140     CACHE *p;
141 
142 #ifdef HAVE_TSEARCH
143     void *pp;
144     CACHE find;
145 
146     memset(&find, 0, sizeof(find));
147     find.cache_num = cache_num;
148     find.string_at = string;
149 
150     if ((pp = tfind(&find, &sorted_cache, compare_cache)) != 0) {
151 	p = *(CACHE **) pp;
152     } else {
153 	p = 0;
154     }
155 #else
156     for (p = cache_list; p != 0; p = p->next) {
157 	if (p->string_at == string) {
158 	    break;
159 	}
160     }
161 #endif
162     return p;
163 }
164 
165 static CACHE *
166 make_cache(int cache_num, const char *string)
167 {
168     CACHE *p;
169 
170     p = dlg_calloc(CACHE, 1);
171     assert_ptr(p, "load_cache");
172     p->next = cache_list;
173     cache_list = p;
174 
175     p->cache_num = cache_num;
176     p->string_at = string;
177 
178 #ifdef HAVE_TSEARCH
179     (void) tsearch(p, &sorted_cache, compare_cache);
180 #endif
181     return p;
182 }
183 
184 static CACHE *
185 load_cache(int cache_num, const char *string)
186 {
187     CACHE *p;
188 
189     if ((p = find_cache(cache_num, string)) == 0) {
190 	p = make_cache(cache_num, string);
191     }
192     return p;
193 }
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 */
199 
200 /*
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.
203  */
204 static bool
205 same_cache2(CACHE * cache, const char *string, unsigned i_len)
206 {
207     unsigned need;
208     size_t s_len = strlen(string);
209     bool result = TRUE;
210 
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)) {
215 
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);
221 	}
222 	assert_ptr(cache->list, "load_cache");
223 	cache->i_len = i_len;
224 
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);
231 	}
232 	cache->s_len = s_len;
233 
234 	result = FALSE;
235     }
236     return result;
237 }
238 
239 #ifdef USE_WIDE_CURSES
240 /*
241  * Like same_cache2(), but we are only concerned about caching a copy of the
242  * string and its associated length.
243  */
244 static bool
245 same_cache1(CACHE * cache, const char *string, size_t i_len)
246 {
247     size_t s_len = strlen(string);
248     bool result = TRUE;
249 
250     if (cache->s_len != s_len
251 	|| !SAME_CACHE(cache, string, (size_t) s_len)) {
252 
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);
259 	}
260 	cache->s_len = s_len;
261 	cache->i_len = i_len;
262 
263 	result = FALSE;
264     }
265     return result;
266 }
267 #endif /* USE_CACHING */
268 
269 /*
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.
272  */
273 #ifdef USE_WIDE_CURSES
274 static int
275 dlg_count_wcbytes(const char *string, size_t len)
276 {
277     int result;
278 
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];
287 
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;
294 		}
295 		--len;
296 	    }
297 	    cache->i_len = len;
298 	}
299 	result = (int) cache->i_len;
300     } else {
301 	result = (int) len;
302     }
303     return result;
304 }
305 #endif /* USE_WIDE_CURSES */
306 
307 /*
308  * Counts the number of wide-characters in the string.
309  */
310 int
311 dlg_count_wchars(const char *string)
312 {
313     int result;
314 #ifdef USE_WIDE_CURSES
315 
316     if (have_locale()) {
317 	size_t len = strlen(string);
318 	CACHE *cache = load_cache(cCntWideChars, string);
319 
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);
327 
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;
337 	    }
338 	}
339 	result = (int) cache->i_len;
340     } else
341 #endif /* USE_WIDE_CURSES */
342     {
343 	result = (int) strlen(string);
344     }
345     return result;
346 }
347 
348 /*
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.
351  */
352 const int *
353 dlg_index_wchars(const char *string)
354 {
355     unsigned len = (unsigned) dlg_count_wchars(string);
356     unsigned inx;
357     CACHE *cache = load_cache(cInxWideChars, string);
358 
359     if (!same_cache2(cache, string, len)) {
360 	const char *current = string;
361 
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 */
376 	    {
377 		(void) current;
378 		cache->list[inx] = (int) inx;
379 	    }
380 	}
381     }
382     return cache->list;
383 }
384 
385 /*
386  * Given the character-offset to find in the list, return the corresponding
387  * array index.
388  */
389 int
390 dlg_find_index(const int *list, int limit, int to_find)
391 {
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;
398 	}
399     }
400     return result;
401 }
402 
403 /*
404  * Build a list of the display-columns for the given string's characters.
405  */
406 const int *
407 dlg_index_columns(const char *string)
408 {
409     unsigned len = (unsigned) dlg_count_wchars(string);
410     unsigned inx;
411     CACHE *cache = load_cache(cInxCols, string);
412 
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;
420 
421 	    for (inx = 0; inx < len; ++inx) {
422 		wchar_t temp[2];
423 		size_t check;
424 		int result;
425 
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]);
439 		    }
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;
446 		    }
447 		}
448 		cache->list[inx + 1] = result;
449 		if (inx != 0)
450 		    cache->list[inx + 1] += cache->list[inx];
451 	    }
452 	} else
453 #endif /* USE_WIDE_CURSES */
454 	{
455 	    for (inx = 0; inx < len; ++inx) {
456 		chtype ch = UCH(string[inx]);
457 
458 		if (ch == TAB)
459 		    cache->list[inx + 1] =
460 			((cache->list[inx] | 7) + 1) - cache->list[inx];
461 		else if (isprint(UCH(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);
469 		}
470 		if (inx != 0)
471 		    cache->list[inx + 1] += cache->list[inx];
472 	    }
473 	}
474     }
475     return cache->list;
476 }
477 
478 /*
479  * Returns the number of columns used for a string.  That happens to be the
480  * end-value of the cols[] array.
481  */
482 int
483 dlg_count_columns(const char *string)
484 {
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);
492     }
493     dlg_finish_string(string);
494     return result;
495 }
496 
497 /*
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.
501  */
502 int
503 dlg_limit_columns(const char *string, int limit, int offset)
504 {
505     const int *cols = dlg_index_columns(string);
506     int result = dlg_count_wchars(string);
507 
508     while (result > 0 && (cols[result] - cols[offset]) > limit)
509 	--result;
510     return result;
511 }
512 
513 /*
514  * Updates the string and character-offset, given various editing characters
515  * or literal characters which are inserted at the character-offset.
516  */
517 bool
518 dlg_edit_string(char *string, int *chr_offset, int key, int fkey, bool force)
519 {
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;
527 
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;
541 	}
542     }
543 
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++) {
573 			;
574 		    }
575 		}
576 	    }
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++) {
590 			    ;
591 			}
592 		    } else if (offset > 0) {
593 			string[indx[offset - 1]] = '\0';
594 		    }
595 		    if (*chr_offset > indx[limit])
596 			*chr_offset = indx[limit];
597 		}
598 	    }
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;
623 	}
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();
635 	    }
636 	}
637     }
638     return edit;
639 }
640 
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)
647 {
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;
655 
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;
661 	}
662     }
663 
664     dpy_column = cols[offset] - cols[offset2];
665 
666     if (p_dpy_column != 0)
667 	*p_dpy_column = dpy_column;
668     if (p_scroll_amt != 0)
669 	*p_scroll_amt = offset2;
670 }
671 
672 /*
673  * Given the character-offset in the string, returns the display-offset where
674  * we will position the cursor.
675  */
676 int
677 dlg_edit_offset(char *string, int chr_offset, int x_last)
678 {
679     int result;
680 
681     compute_edit_offset(string, chr_offset, x_last, &result, 0);
682 
683     return result;
684 }
685 
686 /*
687  * Displays the string, shifted as necessary, to fit within the box and show
688  * the current character-offset.
689  */
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 */
700 {
701     x_last = MIN(x_last + x_base, getmaxx(win)) - x_base;
702 
703     if (hidden && !dialog_vars.insecure) {
704 	if (force) {
705 	    (void) wmove(win, y_base, x_base);
706 	    wrefresh(win);
707 	}
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);
712 
713 	int i, j, k;
714 	int input_x;
715 	int scrollamt;
716 
717 	compute_edit_offset(string, chr_offset, x_last, &input_x, &scrollamt);
718 
719 	dlg_attrset(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);
734 		    }
735 		}
736 		k = check;
737 	    } else {
738 		break;
739 	    }
740 	}
741 	while (k++ < x_last)
742 	    waddch(win, ' ');
743 	(void) wmove(win, y_base, x_base + input_x);
744 	wrefresh(win);
745     }
746 }
747 
748 /*
749  * Discard cached data for the given string.
750  */
751 void
752 dlg_finish_string(const char *string)
753 {
754 #if USE_CACHING
755     if ((string != 0) && dialog_state.finish_string) {
756 	CACHE *p = cache_list;
757 	CACHE *q = 0;
758 	CACHE *r;
759 
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;
765 		}
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;
778 		}
779 		free(p);
780 		p = r;
781 	    } else {
782 		q = p;
783 		p = p->next;
784 	    }
785 	}
786     }
787 #else
788     (void) string;
789 #endif
790 }
791 
792 #ifdef NO_LEAKS
793 void
794 _dlg_inputstr_leaks(void)
795 {
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);
801     }
802 #endif /* USE_CACHING */
803 }
804 #endif /* NO_LEAKS */
805