1101313Stjr/*-
2101313Stjr * Copyright (c) 2002 Tim J. Robbins.
3101313Stjr * All rights reserved.
4101313Stjr *
5227753Stheraven * Copyright (c) 2011 The FreeBSD Foundation
6227753Stheraven * All rights reserved.
7227753Stheraven * Portions of this software were developed by David Chisnall
8227753Stheraven * under sponsorship from the FreeBSD Foundation.
9227753Stheraven *
10101313Stjr * Redistribution and use in source and binary forms, with or without
11101313Stjr * modification, are permitted provided that the following conditions
12101313Stjr * are met:
13101313Stjr * 1. Redistributions of source code must retain the above copyright
14101313Stjr *    notice, this list of conditions and the following disclaimer.
15101313Stjr * 2. Redistributions in binary form must reproduce the above copyright
16101313Stjr *    notice, this list of conditions and the following disclaimer in the
17101313Stjr *    documentation and/or other materials provided with the distribution.
18101313Stjr *
19101313Stjr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20101313Stjr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21101313Stjr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22101313Stjr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23101313Stjr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24101313Stjr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25101313Stjr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26101313Stjr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27101313Stjr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28101313Stjr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29101313Stjr * SUCH DAMAGE.
30101313Stjr */
31101313Stjr
32101313Stjr#include <sys/cdefs.h>
33101313Stjr__FBSDID("$FreeBSD$");
34101313Stjr
35101313Stjr#include <errno.h>
36101313Stjr#include <string.h>
37101313Stjr#include <wctype.h>
38227753Stheraven#include "xlocale_private.h"
39101313Stjr
40101313Stjrenum {
41101313Stjr	_WCT_ERROR	= 0,
42101313Stjr	_WCT_TOLOWER	= 1,
43101313Stjr	_WCT_TOUPPER	= 2
44101313Stjr};
45101313Stjr
46101313Stjrwint_t
47227753Stheraventowctrans_l(wint_t wc, wctrans_t desc, locale_t locale)
48101313Stjr{
49101313Stjr	switch (desc) {
50101313Stjr	case _WCT_TOLOWER:
51227753Stheraven		wc = towlower_l(wc, locale);
52101313Stjr		break;
53101313Stjr	case _WCT_TOUPPER:
54227753Stheraven		wc = towupper_l(wc, locale);
55101313Stjr		break;
56101313Stjr	case _WCT_ERROR:
57101313Stjr	default:
58101313Stjr		errno = EINVAL;
59101313Stjr		break;
60101313Stjr	}
61101313Stjr
62101313Stjr	return (wc);
63101313Stjr}
64227753Stheravenwint_t
65227753Stheraventowctrans(wint_t wc, wctrans_t desc)
66227753Stheraven{
67227753Stheraven	return towctrans_l(wc, desc, __get_locale());
68227753Stheraven}
69101313Stjr
70227753Stheraven/*
71227753Stheraven * wctrans() calls this will a 0 locale.  If this is ever modified to actually
72227753Stheraven * use the locale, wctrans() must be modified to call __get_locale().
73227753Stheraven */
74101313Stjrwctrans_t
75227753Stheravenwctrans_l(const char *charclass, locale_t locale)
76101313Stjr{
77101313Stjr	struct {
78101313Stjr		const char	*name;
79101313Stjr		wctrans_t	 trans;
80101313Stjr	} ccls[] = {
81101313Stjr		{ "tolower",	_WCT_TOLOWER },
82101313Stjr		{ "toupper",	_WCT_TOUPPER },
83101313Stjr		{ NULL,		_WCT_ERROR },		/* Default */
84101313Stjr	};
85101313Stjr	int i;
86101313Stjr
87101313Stjr	i = 0;
88101313Stjr	while (ccls[i].name != NULL && strcmp(ccls[i].name, charclass) != 0)
89101313Stjr		i++;
90101313Stjr
91101313Stjr	if (ccls[i].trans == _WCT_ERROR)
92101313Stjr		errno = EINVAL;
93101313Stjr	return (ccls[i].trans);
94101313Stjr}
95227753Stheraven
96227753Stheravenwctrans_t
97227753Stheravenwctrans(const char *charclass)
98227753Stheraven{
99227753Stheraven	return wctrans_l(charclass, 0);
100227753Stheraven}
101227753Stheraven
102