GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libcurses/base/tries.c Lines: 0 40 0.0 %
Date: 2017-11-07 Branches: 0 36 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: tries.c,v 1.5 2010/01/12 23:22:06 nicm Exp $ */
2
3
/****************************************************************************
4
 * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc.              *
5
 *                                                                          *
6
 * Permission is hereby granted, free of charge, to any person obtaining a  *
7
 * copy of this software and associated documentation files (the            *
8
 * "Software"), to deal in the Software without restriction, including      *
9
 * without limitation the rights to use, copy, modify, merge, publish,      *
10
 * distribute, distribute with modifications, sublicense, and/or sell       *
11
 * copies of the Software, and to permit persons to whom the Software is    *
12
 * furnished to do so, subject to the following conditions:                 *
13
 *                                                                          *
14
 * The above copyright notice and this permission notice shall be included  *
15
 * in all copies or substantial portions of the Software.                   *
16
 *                                                                          *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20
 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24
 *                                                                          *
25
 * Except as contained in this notice, the name(s) of the above copyright   *
26
 * holders shall not be used in advertising or otherwise to promote the     *
27
 * sale, use or other dealings in this Software without prior written       *
28
 * authorization.                                                           *
29
 ****************************************************************************/
30
31
/****************************************************************************
32
 *  Author: Thomas E. Dickey <dickey@clark.net> 1997                        *
33
 ****************************************************************************/
34
35
/*
36
**	tries.c
37
**
38
**	Functions to manage the tree of partial-completions for keycodes.
39
**
40
*/
41
42
#include <curses.priv.h>
43
44
MODULE_ID("$Id: tries.c,v 1.5 2010/01/12 23:22:06 nicm Exp $")
45
46
/*
47
 * Expand a keycode into the string that it corresponds to, returning null if
48
 * no match was found, otherwise allocating a string of the result.
49
 */
50
NCURSES_EXPORT(char *)
51
_nc_expand_try(TRIES * tree, unsigned code, int *count, size_t len)
52
{
53
    TRIES *ptr = tree;
54
    char *result = 0;
55
56
    if (code != 0) {
57
	while (ptr != 0) {
58
	    if ((result = _nc_expand_try(ptr->child, code, count, len + 1))
59
		!= 0) {
60
		break;
61
	    }
62
	    if (ptr->value == code) {
63
		*count -= 1;
64
		if (*count == -1) {
65
		    result = typeCalloc(char, len + 2);
66
		    break;
67
		}
68
	    }
69
	    ptr = ptr->sibling;
70
	}
71
    }
72
    if (result != 0) {
73
	if (ptr != 0 && (result[len] = (char) ptr->ch) == 0)
74
	    *((unsigned char *) (result + len)) = 128;
75
#ifdef TRACE
76
	if (len == 0 && USE_TRACEF(TRACE_MAXIMUM)) {
77
	    _tracef("expand_key %s %s", _nc_tracechar(SP, code), _nc_visbuf(result));
78
	    _nc_unlock_global(tracef);
79
	}
80
#endif
81
    }
82
    return result;
83
}
84
85
/*
86
 * Remove a code from the specified tree, freeing the unused nodes.  Returns
87
 * true if the code was found/removed.
88
 */
89
NCURSES_EXPORT(int)
90
_nc_remove_key(TRIES ** tree, unsigned code)
91
{
92
    T((T_CALLED("_nc_remove_key(%p,%d)"), tree, code));
93
94
    if (code == 0)
95
	returnCode(FALSE);
96
97
    while (*tree != 0) {
98
	if (_nc_remove_key(&(*tree)->child, code)) {
99
	    returnCode(TRUE);
100
	}
101
	if ((*tree)->value == code) {
102
	    if ((*tree)->child) {
103
		/* don't cut the whole sub-tree */
104
		(*tree)->value = 0;
105
	    } else {
106
		TRIES *to_free = *tree;
107
		*tree = (*tree)->sibling;
108
		free(to_free);
109
	    }
110
	    returnCode(TRUE);
111
	}
112
	tree = &(*tree)->sibling;
113
    }
114
    returnCode(FALSE);
115
}
116
117
/*
118
 * Remove a string from the specified tree, freeing the unused nodes.  Returns
119
 * true if the string was found/removed.
120
 */
121
NCURSES_EXPORT(int)
122
_nc_remove_string(TRIES ** tree, const char *string)
123
{
124
    T((T_CALLED("_nc_remove_string(%p,%s)"), tree, _nc_visbuf(string)));
125
126
    if (string == 0 || *string == 0)
127
	returnCode(FALSE);
128
129
    while (*tree != 0) {
130
	if (UChar((*tree)->ch) == UChar(*string)) {
131
	    if (string[1] != 0)
132
		returnCode(_nc_remove_string(&(*tree)->child, string + 1));
133
	    if ((*tree)->child == 0) {
134
		TRIES *to_free = *tree;
135
		*tree = (*tree)->sibling;
136
		free(to_free);
137
		returnCode(TRUE);
138
	    } else {
139
		returnCode(FALSE);
140
	    }
141
	}
142
	tree = &(*tree)->sibling;
143
    }
144
    returnCode(FALSE);
145
}