xref: /freebsd/lib/libc/locale/wctrans.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
146450799STim J. Robbins /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3d915a14eSPedro F. Giffuni  *
446450799STim J. Robbins  * Copyright (c) 2002 Tim J. Robbins.
546450799STim J. Robbins  * All rights reserved.
646450799STim J. Robbins  *
73c87aa1dSDavid Chisnall  * Copyright (c) 2011 The FreeBSD Foundation
85b5fa75aSEd Maste  *
93c87aa1dSDavid Chisnall  * Portions of this software were developed by David Chisnall
103c87aa1dSDavid Chisnall  * under sponsorship from the FreeBSD Foundation.
113c87aa1dSDavid Chisnall  *
1246450799STim J. Robbins  * Redistribution and use in source and binary forms, with or without
1346450799STim J. Robbins  * modification, are permitted provided that the following conditions
1446450799STim J. Robbins  * are met:
1546450799STim J. Robbins  * 1. Redistributions of source code must retain the above copyright
1646450799STim J. Robbins  *    notice, this list of conditions and the following disclaimer.
1746450799STim J. Robbins  * 2. Redistributions in binary form must reproduce the above copyright
1846450799STim J. Robbins  *    notice, this list of conditions and the following disclaimer in the
1946450799STim J. Robbins  *    documentation and/or other materials provided with the distribution.
2046450799STim J. Robbins  *
2146450799STim J. Robbins  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2246450799STim J. Robbins  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2346450799STim J. Robbins  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2446450799STim J. Robbins  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2546450799STim J. Robbins  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2646450799STim J. Robbins  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2746450799STim J. Robbins  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2846450799STim J. Robbins  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2946450799STim J. Robbins  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3046450799STim J. Robbins  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3146450799STim J. Robbins  * SUCH DAMAGE.
3246450799STim J. Robbins  */
3346450799STim J. Robbins 
3446450799STim J. Robbins #include <errno.h>
3546450799STim J. Robbins #include <string.h>
3646450799STim J. Robbins #include <wctype.h>
373c87aa1dSDavid Chisnall #include "xlocale_private.h"
3846450799STim J. Robbins 
3946450799STim J. Robbins enum {
4046450799STim J. Robbins 	_WCT_ERROR	= 0,
4146450799STim J. Robbins 	_WCT_TOLOWER	= 1,
4246450799STim J. Robbins 	_WCT_TOUPPER	= 2
4346450799STim J. Robbins };
4446450799STim J. Robbins 
4546450799STim J. Robbins wint_t
towctrans_l(wint_t wc,wctrans_t desc,locale_t locale)463c87aa1dSDavid Chisnall towctrans_l(wint_t wc, wctrans_t desc, locale_t locale)
4746450799STim J. Robbins {
4846450799STim J. Robbins 	switch (desc) {
4946450799STim J. Robbins 	case _WCT_TOLOWER:
503c87aa1dSDavid Chisnall 		wc = towlower_l(wc, locale);
5146450799STim J. Robbins 		break;
5246450799STim J. Robbins 	case _WCT_TOUPPER:
533c87aa1dSDavid Chisnall 		wc = towupper_l(wc, locale);
5446450799STim J. Robbins 		break;
5546450799STim J. Robbins 	case _WCT_ERROR:
5646450799STim J. Robbins 	default:
5746450799STim J. Robbins 		errno = EINVAL;
5846450799STim J. Robbins 		break;
5946450799STim J. Robbins 	}
6046450799STim J. Robbins 
6146450799STim J. Robbins 	return (wc);
6246450799STim J. Robbins }
633c87aa1dSDavid Chisnall wint_t
towctrans(wint_t wc,wctrans_t desc)643c87aa1dSDavid Chisnall towctrans(wint_t wc, wctrans_t desc)
653c87aa1dSDavid Chisnall {
663c87aa1dSDavid Chisnall 	return towctrans_l(wc, desc, __get_locale());
673c87aa1dSDavid Chisnall }
6846450799STim J. Robbins 
693c87aa1dSDavid Chisnall /*
703c87aa1dSDavid Chisnall  * wctrans() calls this will a 0 locale.  If this is ever modified to actually
713c87aa1dSDavid Chisnall  * use the locale, wctrans() must be modified to call __get_locale().
723c87aa1dSDavid Chisnall  */
7346450799STim J. Robbins wctrans_t
wctrans_l(const char * charclass,locale_t locale)743c87aa1dSDavid Chisnall wctrans_l(const char *charclass, locale_t locale)
7546450799STim J. Robbins {
7646450799STim J. Robbins 	struct {
7746450799STim J. Robbins 		const char	*name;
7846450799STim J. Robbins 		wctrans_t	 trans;
7946450799STim J. Robbins 	} ccls[] = {
8046450799STim J. Robbins 		{ "tolower",	_WCT_TOLOWER },
8146450799STim J. Robbins 		{ "toupper",	_WCT_TOUPPER },
8246450799STim J. Robbins 		{ NULL,		_WCT_ERROR },		/* Default */
8346450799STim J. Robbins 	};
8446450799STim J. Robbins 	int i;
8546450799STim J. Robbins 
8646450799STim J. Robbins 	i = 0;
8746450799STim J. Robbins 	while (ccls[i].name != NULL && strcmp(ccls[i].name, charclass) != 0)
8846450799STim J. Robbins 		i++;
8946450799STim J. Robbins 
9046450799STim J. Robbins 	if (ccls[i].trans == _WCT_ERROR)
9146450799STim J. Robbins 		errno = EINVAL;
9246450799STim J. Robbins 	return (ccls[i].trans);
9346450799STim J. Robbins }
943c87aa1dSDavid Chisnall 
953c87aa1dSDavid Chisnall wctrans_t
wctrans(const char * charclass)963c87aa1dSDavid Chisnall wctrans(const char *charclass)
973c87aa1dSDavid Chisnall {
983c87aa1dSDavid Chisnall 	return wctrans_l(charclass, 0);
993c87aa1dSDavid Chisnall }
1003c87aa1dSDavid Chisnall 
101