111695Sache/*-
211695Sache * Copyright (c) 1993
311695Sache *	The Regents of the University of California.  All rights reserved.
411695Sache *
511695Sache * This code is derived from software contributed to Berkeley by
611695Sache * Paul Borman at Krystal Technologies.
711695Sache *
8227753Stheraven * Copyright (c) 2011 The FreeBSD Foundation
9227753Stheraven * All rights reserved.
10227753Stheraven * Portions of this software were developed by David Chisnall
11227753Stheraven * under sponsorship from the FreeBSD Foundation.
12227753Stheraven *
1311695Sache * Redistribution and use in source and binary forms, with or without
1411695Sache * modification, are permitted provided that the following conditions
1511695Sache * are met:
1611695Sache * 1. Redistributions of source code must retain the above copyright
1711695Sache *    notice, this list of conditions and the following disclaimer.
1811695Sache * 2. Redistributions in binary form must reproduce the above copyright
1911695Sache *    notice, this list of conditions and the following disclaimer in the
2011695Sache *    documentation and/or other materials provided with the distribution.
2111695Sache * 4. Neither the name of the University nor the names of its contributors
2211695Sache *    may be used to endorse or promote products derived from this software
2311695Sache *    without specific prior written permission.
2411695Sache *
2511695Sache * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2611695Sache * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2711695Sache * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2811695Sache * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2911695Sache * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3011695Sache * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3111695Sache * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3211695Sache * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3311695Sache * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3411695Sache * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3511695Sache * SUCH DAMAGE.
3611695Sache */
3711695Sache
3892986Sobrien#include <sys/cdefs.h>
3992986Sobrien__FBSDID("$FreeBSD$");
4092986Sobrien
41150065Sstefanf#include <ctype.h>
423050Sache#include <stdio.h>
43132820Stjr#include <runetype.h>
44227753Stheraven#include <wchar.h>
45227753Stheraven#include "mblocal.h"
463050Sache
473050Sacheunsigned long
48227753Stheraven___runetype_l(__ct_rune_t c, locale_t locale)
493050Sache{
50129065Stjr	size_t lim;
51227753Stheraven	FIX_LOCALE(locale);
52227753Stheraven	_RuneRange *rr = &(XLOCALE_CTYPE(locale)->runes->__runetype_ext);
53129065Stjr	_RuneEntry *base, *re;
543050Sache
5514811Sache	if (c < 0 || c == EOF)
5614811Sache		return(0L);
5712022Sache
58129065Stjr	/* Binary search -- see bsearch.c for explanation. */
59130961Stjr	base = rr->__ranges;
60130961Stjr	for (lim = rr->__nranges; lim != 0; lim >>= 1) {
61129065Stjr		re = base + (lim >> 1);
62130961Stjr		if (re->__min <= c && c <= re->__max) {
63130961Stjr			if (re->__types)
64130961Stjr			    return(re->__types[c - re->__min]);
653050Sache			else
66130961Stjr			    return(re->__map);
67130961Stjr		} else if (c > re->__max) {
68129065Stjr			base = re + 1;
69129065Stjr			lim--;
703050Sache		}
713050Sache	}
7261218Sache
733050Sache	return(0L);
743050Sache}
75227753Stheravenunsigned long
76227753Stheraven___runetype(__ct_rune_t c)
77227753Stheraven{
78227753Stheraven	return ___runetype_l(c, __get_locale());
79227753Stheraven}
80227753Stheraven
81227753Stheravenint ___mb_cur_max(void)
82227753Stheraven{
83227753Stheraven	return XLOCALE_CTYPE(__get_locale())->__mb_cur_max;
84227753Stheraven}
85227753Stheravenint ___mb_cur_max_l(locale_t locale)
86227753Stheraven{
87227753Stheraven	FIX_LOCALE(locale);
88227753Stheraven	return XLOCALE_CTYPE(locale)->__mb_cur_max;
89227753Stheraven}
90