name_match.c revision 174993
150276Speter/****************************************************************************
2174993Srafan * Copyright (c) 1999-2005,2007 Free Software Foundation, Inc.              *
350276Speter *                                                                          *
450276Speter * Permission is hereby granted, free of charge, to any person obtaining a  *
550276Speter * copy of this software and associated documentation files (the            *
650276Speter * "Software"), to deal in the Software without restriction, including      *
750276Speter * without limitation the rights to use, copy, modify, merge, publish,      *
850276Speter * distribute, distribute with modifications, sublicense, and/or sell       *
950276Speter * copies of the Software, and to permit persons to whom the Software is    *
1050276Speter * furnished to do so, subject to the following conditions:                 *
1150276Speter *                                                                          *
1250276Speter * The above copyright notice and this permission notice shall be included  *
1350276Speter * in all copies or substantial portions of the Software.                   *
1450276Speter *                                                                          *
1550276Speter * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
1650276Speter * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
1750276Speter * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
1850276Speter * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
1950276Speter * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
2050276Speter * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
2150276Speter * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
2250276Speter *                                                                          *
2350276Speter * Except as contained in this notice, the name(s) of the above copyright   *
2450276Speter * holders shall not be used in advertising or otherwise to promote the     *
2550276Speter * sale, use or other dealings in this Software without prior written       *
2650276Speter * authorization.                                                           *
2750276Speter ****************************************************************************/
2850276Speter
2950276Speter/****************************************************************************
3050276Speter *  Author: Thomas E. Dickey <dickey@clark.net> 1999                        *
3150276Speter ****************************************************************************/
3250276Speter
3350276Speter#include <curses.priv.h>
3450276Speter#include <term.h>
3550276Speter#include <tic.h>
3650276Speter
37174993SrafanMODULE_ID("$Id: name_match.c,v 1.16 2007/04/21 21:28:13 tom Exp $")
3850276Speter
3950276Speter/*
4050276Speter *	_nc_first_name(char *names)
4150276Speter *
4250276Speter *	Extract the primary name from a compiled entry.
4350276Speter */
44174993Srafan#define FirstName _nc_globals.first_name
4550276Speter
4676726SpeterNCURSES_EXPORT(char *)
4776726Speter_nc_first_name(const char *const sp)
4850276Speter/* get the first name from the given name list */
4950276Speter{
50174993Srafan    unsigned n;
5150276Speter
52166124Srafan#if NO_LEAKS
53166124Srafan    if (sp == 0) {
54174993Srafan	if (FirstName != 0)
55174993Srafan	    FreeAndNull(FirstName);
56166124Srafan	return 0;
57166124Srafan    }
58166124Srafan#endif
59166124Srafan
60174993Srafan    if (FirstName == 0)
61174993Srafan	FirstName = typeMalloc(char, MAX_NAME_SIZE + 1);
6297049Speter    for (n = 0; n < MAX_NAME_SIZE; n++) {
63174993Srafan	if ((FirstName[n] = sp[n]) == '\0'
64174993Srafan	    || (FirstName[n] == '|'))
6576726Speter	    break;
6676726Speter    }
67174993Srafan    FirstName[n] = '\0';
68174993Srafan    return (FirstName);
6950276Speter}
7050276Speter
7150276Speter/*
7250276Speter *	int _nc_name_match(namelist, name, delim)
7350276Speter *
7450276Speter *	Is the given name matched in namelist?
7550276Speter */
7650276Speter
7776726SpeterNCURSES_EXPORT(int)
78166124Srafan_nc_name_match(const char *const namelst, const char *const name, const char *const delim)
7950276Speter{
8076726Speter    const char *s, *d, *t;
8176726Speter    int code, found;
8250276Speter
8376726Speter    if ((s = namelst) != 0) {
8476726Speter	while (*s != '\0') {
8576726Speter	    for (d = name; *d != '\0'; d++) {
8676726Speter		if (*s != *d)
8776726Speter		    break;
8876726Speter		s++;
8976726Speter	    }
9076726Speter	    found = FALSE;
9176726Speter	    for (code = TRUE; *s != '\0'; code = FALSE, s++) {
9276726Speter		for (t = delim; *t != '\0'; t++) {
9376726Speter		    if (*s == *t) {
9476726Speter			found = TRUE;
9576726Speter			break;
9676726Speter		    }
9750276Speter		}
9876726Speter		if (found)
9976726Speter		    break;
10076726Speter	    }
10176726Speter	    if (code && *d == '\0')
10276726Speter		return code;
10376726Speter	    if (*s++ == 0)
10476726Speter		break;
10550276Speter	}
10676726Speter    }
10776726Speter    return FALSE;
10850276Speter}
109