1/* $OpenBSD: name_match.c,v 1.5 2023/10/17 09:52:09 nicm Exp $ */
2
3/****************************************************************************
4 * Copyright 2020 Thomas E. Dickey                                          *
5 * Copyright 1998-2013,2016 Free Software Foundation, Inc.                  *
6 *                                                                          *
7 * Permission is hereby granted, free of charge, to any person obtaining a  *
8 * copy of this software and associated documentation files (the            *
9 * "Software"), to deal in the Software without restriction, including      *
10 * without limitation the rights to use, copy, modify, merge, publish,      *
11 * distribute, distribute with modifications, sublicense, and/or sell       *
12 * copies of the Software, and to permit persons to whom the Software is    *
13 * furnished to do so, subject to the following conditions:                 *
14 *                                                                          *
15 * The above copyright notice and this permission notice shall be included  *
16 * in all copies or substantial portions of the Software.                   *
17 *                                                                          *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
21 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
24 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
25 *                                                                          *
26 * Except as contained in this notice, the name(s) of the above copyright   *
27 * holders shall not be used in advertising or otherwise to promote the     *
28 * sale, use or other dealings in this Software without prior written       *
29 * authorization.                                                           *
30 ****************************************************************************/
31
32/****************************************************************************
33 *  Author: Thomas E. Dickey                    1999-on                     *
34 ****************************************************************************/
35
36#include <curses.priv.h>
37#include <tic.h>
38
39MODULE_ID("$Id: name_match.c,v 1.5 2023/10/17 09:52:09 nicm Exp $")
40
41#define FirstName _nc_globals.first_name
42
43#if NCURSES_USE_TERMCAP && NCURSES_XNAMES
44static const char *
45skip_index(const char *name)
46{
47    if ((_nc_syntax == SYN_TERMCAP) && _nc_user_definable) {
48	const char *bar = strchr(name, '|');
49	if (bar != 0 && (bar - name) == 2)
50	    name = bar + 1;
51    }
52    return name;
53}
54#endif
55
56/*
57 * Get the primary name from the given name list.  For terminfo, this is the
58 * first name.  For termcap, this may be the second name, if the first one
59 * happens to be two characters.
60 */
61NCURSES_EXPORT(char *)
62_nc_first_name(const char *const sp)
63{
64#if NO_LEAKS
65    if (sp == 0) {
66	if (FirstName != 0) {
67	    FreeAndNull(FirstName);
68	}
69    } else
70#endif
71    {
72	if (FirstName == 0)
73	    FirstName = typeMalloc(char, MAX_NAME_SIZE + 1);
74
75	if (FirstName != 0) {
76	    unsigned n;
77	    const char *src = sp;
78#if NCURSES_USE_TERMCAP && NCURSES_XNAMES
79	    src = skip_index(sp);
80#endif
81	    for (n = 0; n < MAX_NAME_SIZE; n++) {
82		if ((FirstName[n] = src[n]) == '\0'
83		    || (FirstName[n] == '|'))
84		    break;
85	    }
86	    FirstName[n] = '\0';
87	}
88    }
89    return (FirstName);
90}
91
92/*
93 * Is the given name matched in namelist?
94 */
95NCURSES_EXPORT(int)
96_nc_name_match(const char *const namelst, const char *const name, const char *const delim)
97{
98    const char *s;
99
100    if ((s = namelst) != 0) {
101	while (*s != '\0') {
102	    const char *d, *t;
103	    int code, found;
104
105	    for (d = name; *d != '\0'; d++) {
106		if (*s != *d)
107		    break;
108		s++;
109	    }
110	    found = FALSE;
111	    for (code = TRUE; *s != '\0'; code = FALSE, s++) {
112		for (t = delim; *t != '\0'; t++) {
113		    if (*s == *t) {
114			found = TRUE;
115			break;
116		    }
117		}
118		if (found)
119		    break;
120	    }
121	    if (code && *d == '\0')
122		return code;
123	    if (*s++ == 0)
124		break;
125	}
126    }
127    return FALSE;
128}
129