14297a3b0SGarrett D'Amore /*
2*2d08521bSGarrett D'Amore * Copyright 2013 Garrett D'Amore <garrett@damore.org>
34297a3b0SGarrett D'Amore * Copyright (c) 1989, 1993
44297a3b0SGarrett D'Amore * The Regents of the University of California. All rights reserved.
54297a3b0SGarrett D'Amore * (c) UNIX System Laboratories, Inc.
64297a3b0SGarrett D'Amore * All or some portions of this file are derived from material licensed
74297a3b0SGarrett D'Amore * to the University of California by American Telephone and Telegraph
84297a3b0SGarrett D'Amore * Co. or Unix System Laboratories, Inc. and are reproduced herein with
94297a3b0SGarrett D'Amore * the permission of UNIX System Laboratories, Inc.
104297a3b0SGarrett D'Amore *
114297a3b0SGarrett D'Amore * This code is derived from software contributed to Berkeley by
124297a3b0SGarrett D'Amore * Paul Borman at Krystal Technologies.
134297a3b0SGarrett D'Amore *
144297a3b0SGarrett D'Amore * Redistribution and use in source and binary forms, with or without
154297a3b0SGarrett D'Amore * modification, are permitted provided that the following conditions
164297a3b0SGarrett D'Amore * are met:
174297a3b0SGarrett D'Amore * 1. Redistributions of source code must retain the above copyright
184297a3b0SGarrett D'Amore * notice, this list of conditions and the following disclaimer.
194297a3b0SGarrett D'Amore * 2. Redistributions in binary form must reproduce the above copyright
204297a3b0SGarrett D'Amore * notice, this list of conditions and the following disclaimer in the
214297a3b0SGarrett D'Amore * documentation and/or other materials provided with the distribution.
224297a3b0SGarrett D'Amore * 4. Neither the name of the University nor the names of its contributors
234297a3b0SGarrett D'Amore * may be used to endorse or promote products derived from this software
244297a3b0SGarrett D'Amore * without specific prior written permission.
254297a3b0SGarrett D'Amore *
264297a3b0SGarrett D'Amore * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
274297a3b0SGarrett D'Amore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
284297a3b0SGarrett D'Amore * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
294297a3b0SGarrett D'Amore * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
304297a3b0SGarrett D'Amore * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
314297a3b0SGarrett D'Amore * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
324297a3b0SGarrett D'Amore * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
334297a3b0SGarrett D'Amore * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
344297a3b0SGarrett D'Amore * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
354297a3b0SGarrett D'Amore * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
364297a3b0SGarrett D'Amore * SUCH DAMAGE.
374297a3b0SGarrett D'Amore */
384297a3b0SGarrett D'Amore
394297a3b0SGarrett D'Amore /*
404297a3b0SGarrett D'Amore * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
414297a3b0SGarrett D'Amore * Use is subject to license terms.
424297a3b0SGarrett D'Amore */
434297a3b0SGarrett D'Amore
444297a3b0SGarrett D'Amore #include "lint.h"
454297a3b0SGarrett D'Amore #include <wchar.h>
464297a3b0SGarrett D'Amore #include "_ctype.h"
474297a3b0SGarrett D'Amore #include "runetype.h"
48*2d08521bSGarrett D'Amore #include "localeimpl.h"
494297a3b0SGarrett D'Amore
504297a3b0SGarrett D'Amore #undef wcwidth
514297a3b0SGarrett D'Amore
524297a3b0SGarrett D'Amore int
wcwidth_l(wchar_t wc,locale_t loc)53*2d08521bSGarrett D'Amore wcwidth_l(wchar_t wc, locale_t loc)
544297a3b0SGarrett D'Amore {
554297a3b0SGarrett D'Amore unsigned int x;
56*2d08521bSGarrett D'Amore const _RuneLocale *rl = loc->runelocale;
574297a3b0SGarrett D'Amore
584297a3b0SGarrett D'Amore if (wc == 0)
594297a3b0SGarrett D'Amore return (0);
604297a3b0SGarrett D'Amore
61*2d08521bSGarrett D'Amore x = ((wc < 0 || wc >= _CACHED_RUNES) ? __runetype(rl, wc) :
62*2d08521bSGarrett D'Amore rl->__runetype[wc]) & (_CTYPE_SWM|_CTYPE_R);
634297a3b0SGarrett D'Amore
644297a3b0SGarrett D'Amore if ((x & _CTYPE_SWM) != 0)
654297a3b0SGarrett D'Amore return ((x & _CTYPE_SWM) >> _CTYPE_SWS);
664297a3b0SGarrett D'Amore return ((x & _CTYPE_R) != 0 ? 1 : -1);
674297a3b0SGarrett D'Amore }
684297a3b0SGarrett D'Amore
69*2d08521bSGarrett D'Amore int
wcwidth(wchar_t wc)70*2d08521bSGarrett D'Amore wcwidth(wchar_t wc)
71*2d08521bSGarrett D'Amore {
72*2d08521bSGarrett D'Amore return (wcwidth_l(wc, uselocale(NULL)));
73*2d08521bSGarrett D'Amore }
74*2d08521bSGarrett D'Amore
754297a3b0SGarrett D'Amore #pragma weak _scrwidth = scrwidth
764297a3b0SGarrett D'Amore
774297a3b0SGarrett D'Amore /*
785ffb5900SGarrett D'Amore * This is a Solaris extension. It never returns a negative width, even for
795ffb5900SGarrett D'Amore * non-printable characters. It is used internally by the printf
805ffb5900SGarrett D'Amore * implementation for %ws.
814297a3b0SGarrett D'Amore */
824297a3b0SGarrett D'Amore int
scrwidth(wchar_t wc)834297a3b0SGarrett D'Amore scrwidth(wchar_t wc)
844297a3b0SGarrett D'Amore {
855ffb5900SGarrett D'Amore int v = wcwidth(wc);
865ffb5900SGarrett D'Amore return (v > 0 ? v : 0);
874297a3b0SGarrett D'Amore }
88